Conflict between pandas and erlport? - python

I am passing data between erlang and python using erlport, following the example here:
http://erlport.org/docs/python.html
the python file I'm calling only contains the line:
import pandas as pd
I am getting the error:
** exception error: {python,'exceptions.AttributeError',
"'function' object has no attribute 'lower'",
[{<<"/anaconda/lib/python2.7/site-packages/pandas/core/format.py">>,
1701,<<"detect_console_encoding">>,
<<"if not encoding or 'ascii' in encoding.lower(): # try again for something bette"...>>},
{<<"/anaconda/lib/python2.7/site-packages/pandas/core/config_init.py">>,
234,<<"<module>">>,
<<"cf.register_option('encoding', detect_console_encoding(), pc_encoding_doc,">>},
{<<"/anaconda/lib/python2.7/site-packages/pandas/__init__.py">>,
25,<<"<module>">>,<<"import pandas.core.config_init">>},
{<<"/Documents/data-algorithms/Alg"...>>,
3,<<"<module>">>,<<"import pandas as pd">>},
{<<"/Documents/testki"...>>,
237,<<"_incoming_call">>,
<<"f = __import__(module, {}, {}, [objects[0]])">>},
{<<"/Documents/te"...>>,
245,<<"_call_with_error_handler">>,<<"function(*args)">>}]}
in function erlport:call/3 (src/erlport.erl, line 234)
in call from algo_tester:start/0 (src/algo_tester.erl, line 27)
I can get rid of the error by commenting out the following two lines in /anaconda/lib/python2.7/site-packages/pandas/core/config_init.py:
234 cf.register_option('encoding', detect_console_encoding(), pc_encoding_doc,
235 validator=is_text)
but then print doesn't work any longer.
Has anyone encountered this before?

It was answered on GitHub:
ErlPort's issue: https://github.com/hdima/erlport/issues/11
Pandas' issue: https://github.com/pydata/pandas/issues/5687

Related

Python gingerit module example does not work

I want to do a python script that is correcting spelling and grammar mistakes of a text.
I found the gigerit library but when I try to run the code from their documentation, I receive an ugly error.
This is the code:
from gingerit.gingerit import GingerIt
text = 'The smelt of fliwers bring back memories.'
parser = GingerIt()
parser.parse(text)
And this is a part of the error:
JSONDecodeError Traceback (most recent call last)
During handling of the above exception, another exception occurred:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
JSONDecodeError: [Errno Expecting value] <!DOCTYPE html>
I also created another empty environment where I installed this library in order to avoid conflicts. But whatever I do, I can't manage to make this code run.
Had the same issue and fixed it with the following solution provided by MythicalMAxX on https://github.com/Azd325/gingerit/issues/24:
"It was cloudflare antibot which was blocking request.
So you all can fix it by importing cloudscraper
and replacing line 16
session = requests.Session()
TO
session = cloudscraper.create_scraper()
In gingerit.py"

Package function returns: "Value error: cannot insert 'column name', already exists"?

I downloaded and have been trying to run the following package: https://pypi.org/project/ecomplexity/#description
Which is also available on github here: https://github.com/cid-harvard/py-ecomplexity
The readme page has an example of how the code is supposed to work, given here:
from ecomplexity import ecomplexity
from ecomplexity import proximity
# Import trade data from CID Atlas
data_url = "https://intl-atlas-downloads.s3.amazonaws.com/country_hsproduct2digit_year.csv.zip"
data = pd.read_csv(data_url, compression="zip", low_memory=False)
data = data[['year','location_code','hs_product_code','export_value']]
# Calculate complexity
trade_cols = {'time':'year', 'loc':'location_code', 'prod':'hs_product_code', 'val':'export_value'}
cdata = ecomplexity(data, trade_cols)
# Calculate proximity matrix
prox_df = proximity(data, trade_cols)
I added import pandas as pd so that the .csv file could be read and added print(prox_df) to provide the results of the second function. I expected the second function to produce a matrix of values in which the elements are the minimum conditional probability of any two products coming from any location.
However, when running this example from the readme page, I am presented with the following error:
Traceback (most recent call last):
File "filepath/project.py", line 16, in <module>
prox_df = proximity(data, trade_cols)
File "filepath\venv\lib\site-packages\ecomplexity\proximity.py", line 79, in proximity
output = output.reset_index()
File "filepath\venv\lib\site-packages\pandas\core\frame.py", line 4849, in reset_index
new_obj.insert(0, name, level_values)
File "filepath\venv\lib\site-packages\pandas\core\frame.py", line 3618, in insert
self._mgr.insert(loc, column, value, allow_duplicates=allow_duplicates)
File "filepath\venv\lib\site-packages\pandas\core\internals\managers.py", line 1147, in insert
raise ValueError(f"cannot insert {item}, already exists")
ValueError: cannot insert prod, already exists
What causes this error? How can I prevent this error from occurring so that I can use the 'proximity' function of this package?
I just tried with pandas 1.1.0 and it failed with same error message. I then downgraded to pandas 0.25.0 and it worked.
You should consider contacting library author to upgrade to work with latest versions of pandas
pip3 install -Iv pandas==0.25.0

AttributeError: 'FFmpegWriter' object has no attribute '_proc'

I have some problem which i cannot find answer to. After installing scikit-video and FFmpeg i got this error:
AttributeError: 'FFmpegWriter' object has no attribute '_proc'
can you help me to find solution to this.
from skvideo.io import FFmpegWriter
def main():
...
video_writer = FFmpegWriter('video.mp4')
...
if __name__ == '__main__':
main()
Already tried to install pyaudio, reinstall FFmpeg and skvideo and install different versions of packages. Does not help at all.
Edit: an example of the full traceback resulting from /skvideo/io/abstract.py is below.
Traceback (most recent call last):
File "run_modules.py", line 93, in <module>
Pipeline.create_videos(video_attr, args.output_path, args.padded)
File "/home/leuko/.local/lib/python3.6/site-packages/skvideo/io/abstract.py", line 474, in close
if self._proc is None: # pragma: no cover
AttributeError: 'FFmpegWriter' object has no attribute '_proc'
I just ran into this issue, and found that FFmpegWriter was actually masking another error. My code was roughly like this:
with FFmpegWriter('/tmp/tmp.webm') as writer:
for frame in frames:
<code to generate out>
writer.writeFrame(out)
The code block before writer.writeFrame was raising an error, but I didn't see that because I didn't look at the full stack trace. I couldn't figure out why FFmpegWriter was throwing this error, but it made sense once I realized I wasn't actually writing any frames but was then trying to close the writer.

Python jsonpickle error: 'OrderedDict' object has no attribute '_OrderedDict__root'

I'm hitting this exception with jsonpickle, when trying to pickle a rather complex object that unfortunately I'm not sure how to describe here. I know that makes it tough to say much, but for what it's worth:
>>> frozen = jsonpickle.encode(my_complex_object_instance)
>>> thawed = jsonpickle.decode(frozen)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/jsonpickle/__init__.py",
line 152, in decode
return unpickler.decode(string, backend=backend, keys=keys)
:
:
File "/Library/Python/2.7/site-packages/jsonpickle/unpickler.py",
line 336, in _restore_from_dict
instance[k] = value
File "/Library/Python/2.7/site-packages/botocore/vendored/requests/packages/urllib3/packages/ordered_dict.py",
line 49, in __setitem__
root = self.__root
AttributeError: 'OrderedDict' object has no attribute '_OrderedDict__root'
I don't find much of assistance when googling the error. I do see what looks like the same issue was resolved at some time past for simpler objects:
https://github.com/jsonpickle/jsonpickle/issues/33
The cited example in that report works for me:
>>> jsonpickle.decode(jsonpickle.encode(collections.OrderedDict()))
OrderedDict()
>>> jsonpickle.decode(jsonpickle.encode(collections.OrderedDict(a=1)))
OrderedDict([(u'a', 1)])
Has anyone ever run into this themselves and found a solution? I ask with the understanding that my case may be "differently idiosynchratic" than another known example.
The requests module for me seems to be running into problems when I .decode(). After looking at the jsonpickle code a bit, I decided to fork it and change the following lines to see what was going on (and I ended up keeping a private copy of jsonpickle with the changes so I can move forward).
In jsonpickle/unpickler.py (in my version it's line 368), search for the if statement section in the method _restore_from_dict():
if (util.is_noncomplex(instance) or
util.is_dictionary_subclass(instance)):
instance[k] = value
else:
setattr(instance, k, value)
and change it to this (it will logERROR the ones that are failing and then you can either keep the code in place or change your OrderedDict's version that have __root)
if (util.is_noncomplex(instance) or
util.is_dictionary_subclass(instance)):
# Currently requests.adapters.HTTPAdapter is using a non-standard
# version of OrderedDict which doesn't have a _OrderedDict__root
# attribute
try:
instance[k] = value
except AttributeError as e:
import logging
import pprint
warnmsg = 'Unable to unpickle {}[{}]={}'.format(pprint.pformat(instance), pprint.pformat(k), pprint.pformat(value))
logging.error(warnmsg)
else:
setattr(instance, k, value)

python odfpy AttributeError: Text instance has no attribute encode

I'm trying to read from an ods (Opendocument spreadsheet) document with the odfpy modules. So far I've been able to extract some data but whenever a cell contains non-standard input the script errors out with:
Traceback (most recent call last):
File "python/test.py", line 26, in <module>
print x.firstChild
File "/usr/lib/python2.7/site-packages/odf/element.py", line 247, in __str__
return self.data.encode()
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0105' in position 4: ordinal not in range(128)
I tried to force an encoding on the output but apparently it does not go well with print:
Traceback (most recent call last):
File "python/test.py", line 27, in <module>
print x.firstChild.encode('utf-8', 'ignore')
AttributeError: Text instance has no attribute 'encode'
What is the problem here and how could it be solved without editing the module code (which I'd like to avoid at all cost)? Is there an alternative to running encode on output that could work?
Here is my code:
from odf.opendocument import Spreadsheet
from odf.opendocument import load
from odf.table import Table,TableRow,TableCell
from odf.text import P
import sys,codecs
doc = load(sys.argv[1])
d = doc.spreadsheet
tables = d.getElementsByType(Table)
for table in tables:
tName = table.attributes[(u'urn:oasis:names:tc:opendocument:xmlns:table:1.0', u'name')]
print tName
rows = table.getElementsByType(TableRow)
for row in rows[:2]:
cells = row.getElementsByType(TableCell)
for cell in cells:
tps = cell.getElementsByType(P)
if len(tps)>0:
for x in tps:
#print x.firstChild
print x.firstChild.encode('utf-8', 'ignore')
Maybe you are not using the latest odfpy, in the latest verion, the __str__ method of Text is implemented as:
def __str__(self):
return self.data
Update odfpy to the latest version, and modify your code as:
print x.firstChild.__str__().encode('utf-8', 'ignore')
UPDATE
This is another method for getting the raw unicode data for Text: __unicode__. So if you don't want to update odfpy, modify your code as:
print x.firstChild.__unicode__().encode('utf-8', 'ignore')
Seems like the library itself is calling encode() -
return self.data.encode()
This uses the system default encoding , which in your case seems to be ascii. you can check that by using -
import sys
sys.getdefaultencoding()
From the traceback, seems like the actual data exists in a variable called data.
Try doing the below instead -
print x.firstChild.data

Categories