Python For Grammar - python

I'm newbie to programming python. I built Beremiz program on Linux, I got this error.
File "Beremiz.py", line 164, in <module>
from ProjectController import ProjectController, MATIEC_ERROR_MODEL, ITEM_CONFNODE
File "/DATA1/UTILITY/Beremiz/beremiz/ProjectController.py", line 16, in <module>
import connectors
File "/DATA1/UTILITY/Beremiz/beremiz/connectors/__init__.py", line 34
for name in listdir(_base_path)
^
connectors = {name:_GetLocalConnectorClassFactory(name)
for name in listdir(_base_path)
if path.isdir(path.join(_base_path, name))
and not name.startswith("__")}
This syntax is not build of python. What is problem?
Thanks for all.

You need to make sure that your version of Python supports the dictionary comprehension syntax. This requires Python >= 2.7 or Python >= 3.
Otherwise you can modify the code like this:
connectors = dict((name, _GetLocalConnectorClassFactory(name))
for name in listdir(_base_path)
if path.isdir(path.join(_base_path, name))
and not name.startswith("__"))

Related

python 3 definition problem import string module

I am not well versed in python... my professor posted a piece of code that includes the following lines:
def formatOptions(options):
from string import joinfields, strip, split
options = joinfields(map(strip, split(strip(options), '\n')), ':')
return options
When I run this using idle 2.7, I do not get an error message. But when I run it using python 3, I get an error message. Is this a difference in the two versions, or do I have a problem with the python 3 build? This is the error message:
File "ml_exercise.py", line 46, in <module>
formatOptions(options))
File "ml_exercise.py", line 28, in formatOptions
from string import joinfields, strip, split
ImportError: cannot import name 'joinfields'
string functions where already mostly deprecated (in favor of the str class methods) in Python 1.6.0 (that is some 18 years ago). The idiomatic way to write this code is
options = ':'.join(part.strip() for part in options.strip().splitlines())
joinfields removed from Python in version 3. Just use the string join function like:
options = ':'.join(map(strip, split(strip(options), '\n')))

Python3.6 and wanish issues

I am trying to run the Wanish library in python and getting the following instead of the result expected.
>>> from wanish import Wanish
>>> wanish = Wanish()
>>> wanish.perform_url("http://www.bbc.com/news/uk-england-london-40269625")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python36\lib\site-packages\wanish-0.6.3-py3.6.egg\wanish\__init__.py", line 167, in perform_url
File "C:\Python36\lib\site-packages\wanish-0.6.3-py3.6.egg\wanish\summarizer.py", line 55, in get_plain_text
File "C:\Python36\lib\site-packages\wanish-0.6.3-py3.6.egg\wanish\summarizer.py", line 91, in create_referat
File "C:\Python36\lib\site-packages\wanish-0.6.3-py3.6.egg\wanish\summarizer.py", line 68, in textrank
File "C:\Python36\lib\site-packages\wanish-0.6.3-py3.6.egg\wanish\langid.py", line 175, in classify
File "C:\Python36\lib\site-packages\wanish-0.6.3-py3.6.egg\wanish\langid.py", line 148, in instance2fv
TypeError: 'float' object cannot be interpreted as an integer
I have tried this using Python 3.6 and even with python 2.7
I came across the same issues.
Actually if you visit pypi wanish page you will see that it located only in Python 3.x categories:
Programming Language :: Python
Programming Language :: Python :: 3.3
Programming Language :: Python :: 3.4
Programming Language :: Python :: 3.5
It won't work in Python 2 because in code there is from urllib.parse import urlparse, urljoin line. But from docs:
The urlparse module is renamed to urllib.parse in Python 3
The offending line is this one* in LanguageIdentifier.instance2fv():
arr = np.zeros((self.nb_numfeats,), dtype='uint32')
In LanguageIdentifier.from_modelstring(), the factory method used to instantiate this class, you can find this:
nb_numfeats = len(nb_ptc) / len(nb_pc)
In python3, division using the / operator always returns a float, where in python2 it returns an int if both operants are ints. So this is a bug.
The langid.py file seems to actually come from a different project, where this error was fixed a while ago.
*I would normally link to the code directly, but someone decided to drop a ~2.4MB blob of base64 in the source file, so github doesn't allow linking.

Hashlib MemoryError in Python 3.5 but not in 2.7

I've been porting a set of Python 2.7 scripts to Python 3.5 so that I can use some libraries that aren't available in 2.7, but I'm getting MemoryError from this code that worked previously:
import hashlib, functools
sha2h = hashlib.sha256()
with open('/path/to/any/file', 'rb') as f:
[sha2h.update(chunk) for chunk in iter(functools.partial(f.read, 256), '')]
As far as I can tell, this is the proper way to get a SHA256 hash of a file. I can't seem to find anything about this issue. If it helps, here's the traceback when the above code is run from the shell:
File "<stdin>", line 2, in <module>
File "<stdin>", line 2, in <listcomp>
Also tried replacing
sha2h = hashlib.sha256()
with
sha2h = hashlib.new('sha256')
to match the documentation for hashlib, but this yielded no change in outcome.
Any help or insight would be greatly appreciated!
On Python 3, you need to set b'' instead of '' as the sentinel value for the iter call:
iter(functools.partial(f.read, 256), b'')
You also really shouldn't be using a list comprehension for side effects like this, but if you're porting existing code that does that, it's probably best to just leave that for now.

AlchemyAPI gives an error when compiling

I'm trying out AlchemyAPI_Python-0.6 - PyXml module. I was trying to run the keyword extractor feature of it, but got the following error when trying to compile. I used the keywords.py file given in the examples. I copied all the files underneath the python directory (AlchemyAPI.py, keywords.py, api_key.txt).
Traceback (most recent call last):
File "C:\Python26\keywords.py", line 4, in <module>
import AlchemyAPI
File "C:\Python26\AlchemyAPI.py", line 6, in <module>
from xml import xpath
File "C:\Python26\lib\site-packages\_xmlplus\xpath\__init__.py", line 112, in <module>
from pyxpath import ExprParserFactory
File "C:\Python26\lib\site-packages\_xmlplus\xpath\pyxpath.py", line 59, in <module>
from xml.xpath.ParsedAbbreviatedRelativeLocationPath import ParsedAbbreviatedRelativeLocationPath
File "C:\Python26\lib\site-packages\_xmlplus\xpath\ParsedAbbreviatedRelativeLocationPath.py", line 31
as = ParsedAxisSpecifier.ParsedAxisSpecifier('descendant-or-self')
^
SyntaxError: invalid syntax
Can some one help me out with this issue, please?
Thank you in advance!
I work for AlchemyAPI.
We just revamped our Python SDK, removing dependencies on PyXML (if you're using Python 2.4 and earlier, you'll need lxml).
Please find the new SDK here: http://www.alchemyapi.com/tools/
It supports all versions of Python 2 and Python 3.
PyXML was written for Python 2.4, and the as keyword was introduced gradually in Python 2.5 and 2.6. In the last line of the stack trace above, the as keyword is used as a variable name, which conflicts with the syntax of Python 2.6.
You can solve this issue by editing two files, changing the name of the as variable to something else (e.g. axis):
C:\Python26\lib\site-packages\_xmlplus\xpath\ParsedAbbreviatedRelativeLocationPath.py, lines 31 and 32:
axis = ParsedAxisSpecifier.ParsedAxisSpecifier('descendant-or-self')
self._middle = ParsedStep.ParsedStep(axis, nt, ppl)
C:\Python26\lib\site-packages\_xmlplus\xpath\ParsedAbbreviatedAbsoluteLocationPath.py, lines 27 and 28:
axis = ParsedAxisSpecifier.ParsedAxisSpecifier('descendant-or-self')
self._step = ParsedStep.ParsedStep(axis, nt, ppl)

py2app with postgres/psycopg2

So I'm trying to use py2app for my project. Now I don't get any errors from py2app. It is writing near the end the following:
/usr/bin/strip: for architecture x86_64 object: /Users/bogdan/Documents/TVB/tvb-root/gemenos/trunk/dist/run.app/Contents/Frameworks/libgfortran.2.dylib malformed object (load command 3 cmdsize not a multiple of 8)
But it build the dist/build folders. But when I try to run my application I get the error:
File "project/core/storage/dao.pyc", line 13, in <module>
File "sqlalchemy/engine/__init__.pyc", line 263, in create_engine
File "sqlalchemy/engine/strategies.pyc", line 50, in create
File "sqlalchemy/engine/url.pyc", line 116, in get_dialect
sqlalchemy.exc.ArgumentError: Could not determine dialect for 'postgresql+psycopg2'.
2011-07-12 15:20:06.680 run[4310:10b] run Error
I tried googling around and the only thing related was:
http://osdir.com/ml/sqlalchemy/2011-05/msg00104.html
but I don't really understand that answer nor do I know if it actually helped at all.
Any suggestion?
Regards,
Bogdan
This may help!! I just struggled through the same problem.
Found this site: (not in english unfortunately, but it worked for me)
http://bancaldo.altervista.org/2011/07/py2exe-errori-post-freeze/
What I did was to add an include to my .py
For MS SQL
from sqlalchemy.dialects.mssql.base import dialect
Or for SQL lite
from sqlalchemy.dialects.sqlite.base import dialect

Categories

Resources