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.
Related
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')))
Below is a minimal working example. I have tested this with Python 3.4, Python 3.6 32 bit and Python 3.6 64 bit.
import io
from lxml import etree
test_node = etree.fromstring('''
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://docs.oasis-open.org/ws-sx/ws-trust/200512">
<soap:Body>
<ns1:RequestSecurityToken/>
</soap:Body>
</soap:Envelope>''')
output = io.BytesIO(b'<?xml version="1.0" encoding="UTF-8"?>')
test_node.getroottree().write(output,
encoding="UTF-8",
xml_declaration=None,
default_namespace=None,
method="c14n",
short_empty_elements=False
)
output.seek(0)
print(output.read())
Result:
Traceback (most recent call last):
File "C:/not_telling/c14n.py", line 16, in <module>
short_empty_elements=False
File "lxml.etree.pyx", line 1869, in lxml.etree._ElementTree.write (src\lxml\lxml.etree.c:57004)
TypeError: write() got an unexpected keyword argument 'short_empty_elements'
I have just upgraded lxml version to 4.0.0. (But the same is the issue with 3.7.)
I need to export with C14N method, and (although not included in the example) I also need to specify a list of namespaces that need to appear in the resulting canonical form. E.g. I must use the inclusive_ns_prefixes parameter too.
UPDATE: actually, it seems to be a problem with Python's built in xml module, not lxml.
Here is the method I'm calling:
https://github.com/python/cpython/blob/master/Lib/xml/etree/ElementTree.py#L721
It does have a short_empty_elements parameter, but it does not accept it.
The default_namespace and short_empty_elements parameters are not supported by the _ElementTree.write() method in lxml. See http://lxml.de/api/lxml.etree._ElementTree-class.html#write.
However, both parameters are available in the ElementTree standard module since Python 3.4. See https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree.write.
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("__"))
Previously I've used PyVisa1.4 in Python2.7, and everything works fine.
Now I need to use Pyvisa1.4 in Python3.2.
I knew that some syntax are changed in Python3.2. Therefore I use the 2to3 to convert the originalPysiva .py files into the new format which are supposed to fit the Python3.2.
But now, unexpected error is generated which is related to ctypes. And I read through the Pyvisa package .py files and try to fix this but still don't know how to do deal with this.
I'm just trying to use the simple get_instruments_list() command like below:
>>> import visa
>>> get_instruments_list()
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
get_instruments_list()
File "C:\Python32\Lib\site-packages\pyvisa\visa.py", line 254, in get_instruments_list
vpp43.find_resources(resource_manager.session, "?*::INSTR")
File "C:\Python32\Lib\site-packages\pyvisa\vpp43.py", line 581, in find_resources
instrument_description)
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type
The MAIN problem I'm facing now is how to correctly use PyVisa in Python3.2.
PyVISA 1.5 (which is in beta now) provides, among other things, full Python 3 support. Take a look at the (new) documentation for instructions about how to download the latest development version at http://pyvisa.readthedocs.org/
The problem is the str that is passed as second argument.
In Python 3 the str was radically changed in order to support unicode.
To correct this problem all strings before being passed to the DLL library must be encoded in ASCII.
Conversely, the return strings are returned as bytes should be converted back into str.
I have corrected this on the visa.py, on the
CR = "\r" replaces CR = b"\r"
LF = "\n" replaces LF = b"\n"
ResourceTemplate init
self.vi = vpp43.open(resource_manager.session, resource_name.encode("ASCII"),
keyw.get("lock", VI_NO_LOCK))
instead of
self.vi = vpp43.open(resource_manager.session, resource_name,
keyw.get("lock", VI_NO_LOCK))
Instrument.write
vpp43.write(self.vi, message.encode("ASCII"))
instead of
vpp43.write(self.vi, message)
conversely on the read_raw the final return is replaced by
return str(buffer)
and on get_instruments_list()
vpp43.find_resources(resource_manager.session, "?*::INSTR".encode("ASCII"))
The newest version of Pyvisa doesn't support Python3.2
Even thouhg you convert the syntax of Pyvisa1.4 for Python2.X to Python3.X by using 2to3 tool, it still won't work
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)