Random AttibuteError on python3 - python

I am facing a strange problem.
I changed a piece of python2 I had to python3 using 2to3.
After this, my code is randomly running/failing...
The Error I get when it fails is related to an Attribute Error...
But in python2 and in random python3 trials it works, and find the attribute...
I tested this behavior on 2 machines, running both python3.4.
Would you have any clue of what is going wrong (I am puzzled by this "random" failure) ?
Here is the trace I get :
Traceback (most recent call last):
File "tools/ciregs.py", line 1596, in <module>
module = Module(json_data)
File "tools/ciregs.py", line 1147, in \__init__
self.registers = [Register(json_reg, parent_module=self) for json_reg in json_module[key]]
File "tools/ciregs.py", line 1147, in <listcomp>
self.registers = [Register(json_reg, parent_module=self) for json_reg in json_module[key]]
File "tools/ciregs.py", line 1260, in \__init__
self.fields = [Field(json_field, self) for json_field in json_reg[key]]
File "tools/ciregs.py", line 1260, in <listcomp>
self.fields = [Field(json_field, self) for json_field in json_reg[key]]
File "tools/ciregs.py", line 1443, in \__init__
self.check()
File "tools/ciregs.py", line 1453, in check
if(self.bitWidth <= 0 or self.bitWidth > self.parent_reg.size()):
File "tools/ciregs.py", line 1326, in size return self.parent_module_.width
AttributeError: 'Module' object has no attribute 'width'
I have a dictionary that contains a "width" key. In a sub-element, I want to evaluate the size of the said sub-element against the width of the top to see if it fits.
That's why I try to access to self.parent_reg.size() that access to the width and that randomly issues that attribute width doesn't exist in the top level dictionary.
Thanks for your help!

Ok guys, thanks for your help, #Bryan Oakley, your help helped me.
In the end the problem is that I get my keys from a json I load, and the load was done on a dictionary and not a sorted dictionary.
One of my key, last in json, then calls functions that expect other keys (width in the Error reported) to be set.
Having the dictionary not sorted makes the width key to be set in some cases before the setting of the last key, and not set in some other cases.
I am still puzzled why it worked for python2....
Thanks all for your help !

Related

python strftime format variable

I'm using this prometheus json exporter https://github.com/catawiki/json_exporter and
I can't figure out how to use the strftime parameter. It is required to use the format from https://docs.python.org/3/library/time.html#time.strftime. As I understand a string should be defined. If I set:
strftime: '%Y-%m-%d'
and use the variable as
params:
start_date: $strftime
I have this error
Traceback (most recent call last):
File "/usr/local/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/local/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/local/lib/python2.7/site-packages/json_exporter/main.py", line 257, in run
self.scrape()
File "/usr/local/lib/python2.7/site-packages/json_exporter/main.py", line 279, in scrape
data = render(self.body, variables)
File "/usr/local/lib/python2.7/site-packages/json_exporter/main.py", line 125, in render
return Template(tmpl).safe_substitute(variables)
File "/usr/local/lib/python2.7/string.py", line 207, in safe_substitute
return self.pattern.sub(convert, self.template)
TypeError: expected string or buffer
What is the correct way to use this ?
Thank you.
Everything seems to match with documentation, even if the documentation is not really clear about wether datetime.strftime or time.strftime fields are to be used (but interestingly, both know about %Y, event the latter).
Some things to try:
remove the quotes around your format template.
specify your template directly in the params, instead of using a link
Also, from the error stacktrace, you are running Python 2.7, which means:
the documentation you need is on this page. Not sure there would be a difference though.
you're 2 years out of active support period, and should seriously consider updating your system.

TypeError: file must have a 'write' attribute

I am trying to train a FastText model.
from gensim.test.utils import datapath
from gensim.models.fasttext import FastText
from gensim.test.utils import get_tmpfile
embedding_size = 300
window_size = 10
min_word = 5
down_sampling = 1e-2
corpus_file = datapath('data.cor')
model = FastText(size=embedding_size,
window=window_size,
min_count=min_word,
sample=down_sampling,
sg=1,
iter=100)
model.build_vocab(corpus_file=corpus_file)
total_words = model.corpus_total_words
model.train(corpus_file=corpus_file, total_words=total_words, epochs=5)
fname = get_tmpfile("fasttext.model")
model.save(fname)
However, the following error appears:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\gensim\utils.py", line 692, in save
_pickle.dump(self, fname_or_handle, protocol=pickle_protocol)
TypeError: file must have a 'write' attribute
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:/untitled1/dfgd.py", line 21, in <module>
model.save(fname)
File "C:\ProgramData\Anaconda3\lib\site-packages\gensim\models\fasttext.py", line 1017, in save
super(FastText, self).save(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\gensim\models\base_any2vec.py", line 621, in save
super(BaseAny2VecModel, self).save(fname_or_handle, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\gensim\utils.py", line 695, in save
self._smart_save(fname_or_handle, separately, sep_limit, ignore, pickle_protocol=pickle_protocol)
File "C:\ProgramData\Anaconda3\lib\site-packages\gensim\utils.py", line 547, in _smart_save
compress, subname)
File "C:\ProgramData\Anaconda3\lib\site-packages\gensim\utils.py", line 609, in _save_specials
restores.extend(val._save_specials(cfname, None, sep_limit, ignore, pickle_protocol, compress, subname))
File "C:\ProgramData\Anaconda3\lib\site-packages\gensim\utils.py", line 621, in _save_specials
np.save(subname(fname, attrib), np.ascontiguousarray(val))
File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 536, in save
pickle_kwargs=pickle_kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\format.py", line 644, in write_array
array.tofile(fp)
OSError: 418859700 requested and 0 written
I have previously trained the model with the window size set to be 9 and less. There was no problem at that time and no error appeared.
How can I fix this error?
Why do you want to write your large model file to a "temporary" directory? Wouldn't you prefer to save it to an explicit place under your control? The .save() method will take any path of your choosing.
Also note that anytime you get such an error, it may be hiding some other disk-IO problem, like insufficient privileges for the current user, insufficient space for the data, an illegal path type for the named volume, an existing unchangeable file, etc.
Thus is can be helpful to:
check what actual exact path was involved (the value of fname)
try other more-simple operations on that same path, either from Python code, or outside of it. (Can you use basic Python operations to write a single line to that path? Can you use shell operations to list that path's enclosing directory, or create a stub file at that location? etc.) You may get a more clear error from those other attempts, which shows the real problem.
Finally, though things like Gensim, Python, & the various scientific-computing libraries they depend upon are all officially supported on Windows, they tend to get far more developer attention, real-world-use, & testing on Linux/Unix-based systems – so lots of the example/tutorial code, and suggestions from quality answers, will work best there. If now or in the future you have the choice to use a non-Windows system for this kind of work, lots of little problems may be avoided.

In urwid 'Frame' object has no attribute 'rows'

I am searching any simple way to make urwid.Frame work in python without success, as example, I try this MWE:
frame = urwid.Frame(urwid.Text(('some text'), align='center'))
filler = urwid.Filler(frame, "top")
loop = urwid.MainLoop(filler)
loop.run()
And I get the following unuseful error message:
Traceback (most recent call last):
File "pycurses.py", line 596, in <module>
loop.run()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 287, in run
self._run()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 385, in _run
self.event_loop.run()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 790, in run
self._loop()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 818, in _loop
self._entering_idle()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 779, in _entering_idle
callback()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 574, in entering_idle
self.draw_screen()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 588, in draw_screen
canvas = self._topmost_widget.render(self.screen_size, focus=True)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/widget.py", line 145, in cached_render
canv = fn(self, size, focus=focus)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/decoration.py", line 814, in render
top, bottom = self.filler_values(size, focus)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/decoration.py", line 799, in filler_values
height = self._original_widget.rows((maxcol,),focus=focus)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/widget.py", line 205, in cached_rows
return fn(self, size, focus)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/container.py", line 1611, in rows
return sum(self.get_item_rows(size, focus))
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/container.py", line 1512, in get_item_rows
l.append(w.rows((maxcol,),
AttributeError: 'Frame' object has no attribute 'rows'
I try many variations, such putting the frame inside an urwid.Pile before giving it to the Filler widget, but I still get similar error messages.
So, is their any minimalist example work using urwid.Frame?
And what doese mean this “Frame' object has no attribute 'rows'”? I never ask in my example any .rows method, so why the error message have a behavior like if I used it?
The error is indeed not very good. The actual problem here is that the nesting of the Frame and the Filler should be the other way around:
import urwid
text = urwid.Text('some text', align='center')
filler = urwid.Filler(text, "top")
frame = urwid.Frame(filler)
loop = urwid.MainLoop(frame)
loop.run()
A flow widget (like Text) gets to decide how many rows it should be. The main loop expects a box widget (which does not get to pick its own width or height). The Filler widget bridges the two: it asks the wrapped flow widget how many rows it takes up (given a number of columns) and then fills up the rest of the rows.
The Frame is a box widget that contains another box widget in the center.
If you haven't seen this page already, I highly recommend looking at the diagrams here: http://urwid.org/manual/widgets.html#included-widgets The colors describe what kinds of widgets nest inside what kinds of widgets.
(Also I suspect it would be technically feasible for Urwid to detect that the widget is of the wrong type and say that instead in the error message, but I haven't dived that deep into the implementation.)

ndb.get_multi returning AssertionError

Over the last 48 hours or so my small python GAE app has started getting AssertionErrors from ndb.get_multi calls.
Full traceback is appended, and the errors are being generated on the production server in _BaseValue's __init__ on line 734 of /base/data/.../ndb/model.py, and the failing assertion is b_val is not None with message "Cannot wrap None"
The error doesn't appear to be related to a particular entity or entities, but I've only seen it with one entity type so far (yet to test others).
The get_multi call is for only up to a dozen keys, and the error is intermittent so that repeating it will sometimes succeed. Or not...
I'm not seeing this error via remote shell, but I note that my local install is 1.9.23 while the log entry says the production server is 1.9.25 (GoogleAppEngineLauncher says my local install is up to date)
I'm adding a workaround to catch the exception and iterate through the keys to get them individually but I'm still seeing an upstream warning about a "suspended generator get" on line 744 of context.py.
The warning appears on the first get of this entity type from the list, for at least 2 different lists of keys (as well as preceding the AssertionError).
I don't want to have to wrap all get_multi calls in this way.
What's going on?
TRACEBACK:
Cannot wrap None
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~thegapnetball/115.386356111937586421/handlers/assess.py", line 50, in get
rs = ndb.get_multi(t.players)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3905, in get_multi
for future in get_multi_async(keys, **ctx_options)]
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 326, in get_result
self.check_success()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 372, in _help_tasklet_along
value = gen.send(val)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/context.py", line 751, in get
pbs = entity._to_pb(set_key=False).SerializePartialToString()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3147, in _to_pb
prop._serialize(self, pb, projection=self._projection)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 2379, in _serialize
projection=projection)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1405, in _serialize
values = self._get_base_value_unwrapped_as_list(entity)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1175, in _get_base_value_unwrapped_as_list
wrapped = self._get_base_value(entity)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1163, in _get_base_value
return self._apply_to_values(entity, self._opt_call_to_base_type)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1335, in _apply_to_values
value[:] = map(function, value)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1217, in _opt_call_to_base_type
value = _BaseValue(self._call_to_base_type(value))
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 734, in \__init__
assert b_val is not None, "Cannot wrap None"
AssertionError: Cannot wrap None
Tim Hoffman and Patrick Costello put me on the right track to solve this.
I incremented version to protect some changes but took longer to finish than I expected.
One change added a repeated StructuredProperty to a model derived from ndb.Model, and I put several entities with the extra property (about 30 out of 1100 total).
The previous version without the extra property was still the default and was being lightly used, so the entities became just inconsistent enough to produce the intermittent AssertionError.
The main lesson is to take note of the recommendations in Google's schema update article, particularly changing the underlying parent to Expando and/or disabling datastore edits until any migration is complete.
https://cloud.google.com/appengine/articles/update_schema
The fix was to add the property to the previous version, get all the entities and then put them.
Thanks Tim and Patrick for the pointer!

epydoc AttributeError: 'Text' object has no attribute 'data'

I've not used epydoc in the last 2 years but I found it very handy to take track of my classes and methods with a very little effort.
Today I installed latest version 3.0.1 but I get this error and searching around seems no solutions are provided.
Traceback (most recent call last):-] Parsing docstrings: pyramid.reques...
File "/home/neurino/apps/env/bin/epydoc", line 13, in <module>
cli()
File "/home/neurino/apps/env/lib/python2.7/site-packages/epydoc/cli.py", line 965, in cli
main(options, names)
File "/home/neurino/apps/env/lib/python2.7/site-packages/epydoc/cli.py", line 757, in main
exclude_parse=exclude_parse)
File "/home/neurino/apps/env/lib/python2.7/site-packages/epydoc/docbuilder.py", line 275, in build_doc_index
parse_docstring(val_doc, docindex, suppress_warnings)
File "/home/neurino/apps/env/lib/python2.7/site-packages/epydoc/docstringparser.py", line 265, in parse_docstring
api_doc.summary, api_doc.other_docs = api_doc.descr.summary()
File "/home/neurino/apps/env/lib/python2.7/site-packages/epydoc/markup/restructuredtext.py", line 179, in summary
try: self._document.walk(visitor)
File "/home/neurino/apps/env/lib/python2.7/site-packages/docutils/nodes.py", line 137, in walk
if child.walk(visitor):
File "/home/neurino/apps/env/lib/python2.7/site-packages/docutils/nodes.py", line 129, in walk
visitor.dispatch_visit(self)
File "/home/neurino/apps/env/lib/python2.7/site-packages/docutils/nodes.py", line 1604, in dispatch_visit
return method(node)
File "/home/neurino/apps/env/lib/python2.7/site-packages/epydoc/markup/restructuredtext.py", line 307, in visit_paragraph
m = self._SUMMARY_RE.match(child.data)
AttributeError: 'Text' object has no attribute 'data'
Is epydoc project dead?
I found a patch on epydoc tracker, it was outdated anyway this part solves the problem:
markup/restructuredtext.py
307c307,310
< m = self._SUMMARY_RE.match(child.data)
---
> try:
> m = self._SUMMARY_RE.match(child.data)
> except AttributeError:
> m = None
Epydoc has not been maintained for long time and the last release is not quite compatible with current Pyton and docutils. It is still a useful tool, though, but needs some patching.
Here are some patches I used with Epydoc to build documentation for my Python 2.7 code: http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/packages/epydoc/ (they are part of the PLD-Linux Epydoc package).
I wish someone takes over the code and continue the development…
Objects can be tested against null (i.e. None) so that the exception could not be occurred.
if object is None:

Categories

Resources