RiveScript gives an error for python object macros - python-3.x

Is it possible to access the object macros internally in RiveScript? I'm bit confused with that.
I have defined the following script:
> object small python
return "4"
< object
+ small
- <call>small</call>
Which gives me an error when I enter small:
error: [ERR: Object Not Found]

make a space after the object name just like that
- <call>small </call>

The definition of the object seems to be ok, but you are probably running it from https://play.rivescript.com/ or https://www.rivescript.com/try, which only allow JavaScript or CoffeeScript (see the about page).
You can use rivescript-python to run the code, which you can install by:
pip install rivescript
Then put your rivescript in a file (.rive), for example, in helloworld.rive.
Then in python:
from rivescript import RiveScript
bot = RiveScript()
bot.load_directory('.') # set your directory where the .rive file(s) is/are
bot.sort_replies()
Now you can use bot.reply to get the answer for a certain input:
>>> bot.reply('localuser','small')
'4'
Edit: I have tested this code on Ubuntu 14.04 with Python 3.4.3 and Python 2.7.12 and rivescript 1.14.4.

Related

how to list all the packages required for a program in python?

can i list the python packages that are actually required for running exiting python program running in linux.
I tried running following commands.
pip3 freeze
pip3 list
To find the modules used by a single python script, you can try to use ModuleFinder:
Create a new python script to analyze the modules your script is using:
New Script:
from modulefinder import ModuleFinder
finder = ModuleFinder()
finder.run_script('MultiProcess.py')
print('Loaded modules:')
for name, mod in finder.modules.items():
print(('%s: ' % name))
print((','.join(list(mod.globalnames.keys())[:3])))
print(('-'*50))
print('Modules not imported:')
print(('\n'.join(iter(finder.badmodules.keys()))))
The output is very verbose and detailed
reference:
https://docs.python.org/2/library/modulefinder.html

Invalid syntax with installed package python

I have installed a package named 'Spexxy' with python3 setup.py install which worked fine.
Now, I am trying to use it, so with ipython3 I import the package as follow import spexxy which also works fine, but when I try to use the different tools available in this package, I keep getting the error invalid syntax while I am following the documentation of the package, for example:
I want to creat a grid as specified in the documentation, using spexxytools grid create, here is what I do:
spexxytools grid create --from-filename "lte(?P<Teff>\d{5})-(?P<logg>\d\.\d\d)(?P<FeH>[+-]\d\.\d)(\.Alpha=(?P<Alpha>[+-]\d\.\d\d))?\.PHOENIX"
I get the error:
File "<ipython-input-17-e08367969322>", line 1
spexxytools grid create --from-filename "lte(?P<Teff>\d{5})-(?P<logg>\d\.\d\d)(?P<FeH>[+-]\d\.\d)(\.Alpha=(?P<Alpha>[+-]\d\.\d\d))?\.PHOENIX" .
^
SyntaxError: invalid syntax
The documentation is present here: https://spexxy.readthedocs.io/en/2.1/
Can you tell me why ? Thanks
That's shell syntax. Call it from IPython by prepending an exclamation mark:
!spexxytools grid create --from-filename ...
Based on the documentation, the output will be a file called grid.csv.
If you actually want to use spexxy's Python API, see the API Reference

How to resolve 'No module named 'cPickle'' exception in virtualenv

I'm trying to run a program which connects to all databases(mysql,sqlite) and fetch data from it .
Python version - 3.6.8
Since the code is too long ,i'm showing only particular snippets.
def show_columns_mysql(cursor,tbname):
cursor.execute("""show columns from %s"""%(tbname))
rs=cursor.fetchall()
colname=[]
for i in rs:
colname.append(i[0])
return colname
There is no problem or issue if i exexute the program in normal python environment . When i try to execute this in virtual environment ,it shows me No module named 'cPickle' .
I have tried all the solutions but none solved my problem .
What was the problem ?
There is no cPickle in Python 3. Just import pickle. pickle will automatically use the C accelerator.
Install pickle. Then do:
import pickle as cPickle

Check errors/warnings while installing python modules from code using pip

I want to report those modules which failed to install by checking for any errors. Suppose I have a code with wrong or misspelled module-name. I want my code to print the module name for which installation failed due to some reason. For example:
import pip
pip.main(['install', 'someMisspelledModuleName==1.2.3'])
I have a dictionary from which I am reading such module names and their versions and installing it from my code itself in Python 3.4. When I am executing this script in Python Shell, it just gives red-coloured warning, which is not an error and hence I can't handle it.
Please tell me how to handle such cases?
pip.main(…) returns a status code, one of the predefined. Just check if it's SUCCESS or no:
import sys, pip
rc = pip.main(['install', 'someMisspelledModuleName==1.2.3'])
sys.exit(rc)
I just exit here using the result code.

Mako: cannot import the Template class. Have a SyntaxError error in "\mako\template.py", line 622

I want to try Mako with Django instead of Django's default template language. But I'm having a problem when I try to import Mako's Template class as written in the manual:
from mako.template import Template
mytemplate = Template("hello world!")
print mytemplate.render()
I do this in Windows cmd and receive such an error:
C:\Documents and Settings\User>cd C:\py\project\vendor\template\Mako_73 // cd to where I unpacked Mako
C:\py\project\vendor\template\Mako_73>python // run Python interpreter
>>> from mako.template import Template // trying to import and getting an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".\mako\template.py", line 622
exec code in module.__dict__, module.__dict__
^
SyntaxError: invalid syntax
The code from that part:
def _compile_text(template, text, filename):
identifier = template.module_id
source, lexer = _compile(template, text, filename,
generate_magic_comment=template.disable_unicode)
cid = identifier
if not util.py3k and isinstance(cid, unicode):
cid = cid.encode()
module = types.ModuleType(cid)
code = compile(source, cid, 'exec')
exec code in module.__dict__, module.__dict__
return (source, module)
What can it be? I couldn't find anything in Google about this error.
I'm using Python 3.3.
I've downloaded Mako-0.7.3 as tar.gz file and just unzipped it in
C:\py\poject\vendor\template\Mako_73. I do not have this directory in the PYTHONPATH or paths.pth. C:\py\poject is a directory where my Django project lives and in \vendor\template I've decided to put Mako and import it from there.
UPD
I found the solution. I've installed the Pyramid Framework and have taken the Mako from there as the Mako is a default template system in it. And Pyramid's version works fine.
Your basic problem is that you are using Python 3, which is relatively new for large projects like Django.
Secondly, you need to find out how to install packages correctly. I don't know where you got Mako from, but you won't find anywhere that says "just unpack the tarball" - perhaps you are assuming that from your knowledge of PHP, but it's not correct.
On the Mako site, the suggested method of installation is pip.
If you go for downloading manually, you need to read instructions about installing Python packages, for example here: http://wiki.python.org/moin/CheeseShopTutorial
The reason Mako doesn't work for you is that the installation procedure (which you haven't run) converts all the provided Python 2 code so that it works on Python 3. It is not that someone didn't bother to check the code for basic syntax errors!
If you are trying to use Django, though, Python 3 is definitely the wrong choice - the installation instructions clearly say you need to be using Python 2.5 to 2.7: https://docs.djangoproject.com/en/1.4/intro/install/
Since you are new to Python, you should try to walk before you run, and go with the tried and tested path - which is Python 2.7 for Django. Ignoring installation instructions and requirements will only slow you down and make life hard.

Resources