import pickle
class CustomEx(Exception):
def __init__(self, *args, **kwargs):
self.a = kwargs.get('a')
assert self.a, "a must present"
s = pickle.dumps(CustomEx(a='a'))
pickle.loads(str(s)) # this fails
I'm using python version 2.7.13. Please advise how can I make it work. It fails with following stack trace:
Traceback (most recent call last):
File "/Users/app/tmp/pickle_user_purchase_required.py", line 10, in <module>
pickle.loads(str(s))
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1388, in loads
return Unpickler(file).load()
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 864, in load
dispatch[key](self)
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1139, in load_reduce
value = func(*args)
File "/Users/app/tmp/pickle_user_purchase_required.py", line 6, in __init__
assert self.a, "a must present"
AssertionError: a must present
Related
When I try to override a method that has an argument with Literal as its type hint, I get a RecursionError from the overrides module (see stack trace below). I'm not sure why this is happening, or if it is possible to override such methods.
edit: The overrides module I'm talking about is this third party module: github.com/mkorpela/overrides
Here is a toy example that reproduces my error:
from typing import Literal
from overrides import overrides
class Base:
def foo(self, mode: Literal["train"]) -> None:
raise NotImplementedError()
class Child(Base):
#overrides
def foo(self, mode: Literal["train"]) -> None:
pass
Removing the #overrides decorator is a workaround since it doesn't change my code's behavior, but I would prefer to keep it if possible. My plan is to change the arg type to an Enum which is probably a better design decision anyway. But I'm curious why this combination of #overrides and Literal doesn't work.
Here is the stack trace:
Traceback (most recent call last):
File "model-autotraining/temp.py", line 12, in <module>
class ChildModel(BaseClass):
File "model-autotraining/temp.py", line 14, in ChildModel
def foo(self, mode: Literal["train"]) -> None:
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/overrides/overrides.py", line 88, in overrides
return _overrides(method, check_signature, check_at_runtime)
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/overrides/overrides.py", line 114, in _overrides
_validate_method(method, super_class, check_signature)
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/overrides/overrides.py", line 135, in _validate_method
ensure_signature_is_compatible(super_method, method, is_static)
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/overrides/signature.py", line 94, in ensure_signature_is_compatible
ensure_all_kwargs_defined_in_sub(
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/overrides/signature.py", line 153, in ensure_all_kwargs_defined_in_sub
and not _issubtype(super_type_hints[name], sub_type_hints[name])
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/overrides/signature.py", line 42, in _issubtype
return issubtype(left, right)
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/typing_utils/__init__.py", line 428, in issubtype
return _is_normal_subtype(normalize(left), normalize(right), forward_refs)
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/typing_utils/__init__.py", line 251, in normalize
args = _normalize_args(args)
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/typing_utils/__init__.py", line 232, in _normalize_args
return tuple(_normalize_args(type_) for type_ in tps)
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/typing_utils/__init__.py", line 232, in <genexpr>
return tuple(_normalize_args(type_) for type_ in tps)
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/typing_utils/__init__.py", line 232, in _normalize_args
return tuple(_normalize_args(type_) for type_ in tps)
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/typing_utils/__init__.py", line 232, in <genexpr>
... repeated many times ...
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/typing_utils/__init__.py", line 232, in _normalize_args
return tuple(_normalize_args(type_) for type_ in tps)
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/typing_utils/__init__.py", line 232, in <genexpr>
return tuple(_normalize_args(type_) for type_ in tps)
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/site-packages/typing_utils/__init__.py", line 231, in _normalize_args
if isinstance(tps, collections.abc.Sequence):
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/abc.py", line 98, in __instancecheck__
return _abc_instancecheck(cls, instance)
File "/opt/homebrew/Caskroom/miniforge/base/envs/worker/lib/python3.8/abc.py", line 102, in __subclasscheck__
return _abc_subclasscheck(cls, subclass)
RecursionError: maximum recursion depth exceeded in comparison
As others have noted in the comments, this behavior is a bug in the third party overrides module I'm using https://github.com/mkorpela/overrides). It is a known bug: https://github.com/mkorpela/overrides/issues/94
Bug in the package is now fixed.
I have the following factory:
class ContactFactory(DjangoModelFactory):
name = Faker('company')
industry = Iterator(Industry.objects.all())
class Meta:
model = 'sales.contact'
#post_generation
def requested_devices(self, create, extracted, **kwargs):
if create:
self.requested_devices.add(MSize.objects.first())
And I'm trying to write a test such as:
class TestUserCanAskQuestion(TestCase):
#classmethod
def setUpTestData(cls):
call_command('insert_initial_data')
def setUp(self):
self.contact = ContactFactory()
But everytime I run the test, it results in "StopIteration" error.
Here is the full stack trace:
ERROR: test_dummy (comminquiry.tests.test_views.TestUserCanAskQuestion)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/mnt/c/Users/Alire/Projects/mm/mm-bpmui/src/comminquiry/tests/test_views.py", line 32, in test_dummy
a = ContactFactory.build()
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/base.py", line 546, in build
return cls._generate(enums.BUILD_STRATEGY, kwargs)
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/base.py", line 500, in _generate
return step.build()
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/builder.py", line 272, in build
step.resolve(pre)
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/builder.py", line 221, in resolve
self.attributes[field_name] = getattr(self.stub, field_name)
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/builder.py", line 375, in __getattr__
extra=context,
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/declarations.py", line 196, in evaluate
value = next(iter(self.iterator))
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/utils.py", line 136, in __iter__
value = next(self.iterator)
StopIteration
----------------------------------------------------------------------
Ran 1 test in 2.433s
If I move the ContactFactory() outside of class, the error disappears.
I'm I missing something? or it's a bug in factory boy or django?
(I'm using factory_boy==2.11.1 and django==2.1.2)
As #dirkgroten had suggested, one the fields was returning an empty queryset. This was the root cause of the error.
I am using savez to save the weights. Following is my code:
class vgg16:
def __init__(self, imgs1,imgs2, weights=None, sess=None):
.........
self.weight_list=[]
self.keys=[]
........
self.SaveWeights()
....neural network............
def SaveWeights(self):
tmp = file("vgg16_predict.npz",'wb')
np.savez(self,**dict(zip(self.keys, self.weight_list)))
tmp.close
I keep getting the pickling error. There are different solutions provided. But is there an easiest way to make this happen?
Here is the traceback:
Traceback (most recent call last):
File "f.py", line 350, in <module>
vgg = vgg16(imgs1,imgs2, 'vgg16_weights.npz', sess)
File "f.py", line 43, in __init__
self.SaveWeights()
File "f.py", line 339, in SaveWeights
np.savez(self,**dict(zip(self.keys, self.weight_list)))
File "/usr/local/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 574, in savez
_savez(file, args, kwds, False)
File "/usr/local/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 639, in _savez
pickle_kwargs=pickle_kwargs)
File "/usr/local/lib/python2.7/dist-packages/numpy/lib/format.py", line 573, in write_array
pickle.dump(array, fp, protocol=2, **pickle_kwargs)
cPickle.PicklingError: Can't pickle <type 'module'>: attribute lookup __builtin__.module failed
Exception AttributeError: "vgg16 instance has no attribute 'tell'" in <bound method ZipFile.__del__ of <zipfile.ZipFile object at 0x7f812dec99d0>> ignored
You need to pickle to a file. Just use the path directly:
np.savez("vgg16_predict.npz", **dict(zip(self.keys, self.weight_list)))
So, this should be you full method:
def SaveWeights(self):
np.savez("vgg16_predict.npz", **dict(zip(self.keys, self.weight_list)))
I have the following code, which decorates the class:
import dill
from collections import namedtuple
from multiprocessing import Process
def proxified(to_be_proxied):
b = namedtuple('d', [])
class Proxy(to_be_proxied, b):
pass
Proxy.__name__ = to_be_proxied.__name__
return Proxy
#proxified
class MyClass:
def f(self):
print('hello!')
pickled_cls = dill.dumps(MyClass)
def new_process(clazz):
dill.loads(clazz)().f()
p = Process(target=new_process, args=(pickled_cls,))
p.start()
p.join()
When I am trying to pickle decorated class I am getting the following error:
Traceback (most recent call last):
File "/usr/lib/python3.5/pickle.py", line 907, in save_global
obj2, parent = _getattribute(module, name)
File "/usr/lib/python3.5/pickle.py", line 265, in _getattribute
.format(name, obj))
AttributeError: Can't get local attribute 'proxified.<locals>.Proxy' on <function proxified at 0x7fbf7de4b8c8>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/carbolymer/example.py", line 108, in <module>
pickled_cls = dill.dumps(MyClass)
File "/usr/lib/python3.5/site-packages/dill/dill.py", line 243, in dumps
dump(obj, file, protocol, byref, fmode, recurse)#, strictio)
File "/usr/lib/python3.5/site-packages/dill/dill.py", line 236, in dump
pik.dump(obj)
File "/usr/lib/python3.5/pickle.py", line 408, in dump
self.save(obj)
File "/usr/lib/python3.5/pickle.py", line 475, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python3.5/site-packages/dill/dill.py", line 1189, in save_type
StockPickler.save_global(pickler, obj)
File "/usr/lib/python3.5/pickle.py", line 911, in save_global
(obj, module_name, name))
_pickle.PicklingError: Can't pickle <class '__main__.proxified.<locals>.Proxy'>: it's not found as __main__.proxified.<locals>.Proxy
How can I pickle the decorated class using dill? I would like pass to pass this class to a separate process as an argument - maybe is there a simpler way to do it?
A good explanation of "Why pickling decorated functions is painful", provided by Gaƫl Varoquaux can be found here.
Basically rewriting the class using functools.wraps can avoid these issues :)
I have MBP13'R running the latest OSX.
For some reason I can't run doctest...
Python code:
def area_tri(base, height):
"""
>>>area_tri(10, 10)
50
"""
return base / 2 * height
import doctest
doctest.testmod()
Error message:
Traceback (most recent call last):
File "/Users/name/Documents/doctest.py", line 8, in <module>
import doctest
File "/Users/name/Documents/doctest.py", line 9, in <module>
doctest.testmod()
AttributeError: 'module' object has no attribute 'testmod'
I only just started programming on my Mac and I can't fix it...
Python version is 3.3.4
Your file naming is confusing the interpreter. You've named your file "/Users/name/Documents/doctest.py" which has the same name as the doctest module.
Change the name and try again (make sure you remove the old doctest.pyc as well).
Demo:
Your code breaks as expected:
msvalkon#Lunkwill:/tmp$ python doctest.py
Traceback (most recent call last):
File "doctest.py", line 8, in <module>
import doctest
File "/tmp/doctest.py", line 9, in <module>
doctest.testmod()
AttributeError: 'module' object has no attribute 'testmod'
After renaming and removing the .pyc
msvalkon#Lunkwill:/tmp$ mv doctest.py dtest.py
msvalkon#Lunkwill:/tmp$ rm doctest.pyc
msvalkon#Lunkwill:/tmp$ python dtest.py
Traceback (most recent call last):
File "dtest.py", line 9, in <module>
doctest.testmod()
File "/usr/lib/python2.7/doctest.py", line 1885, in testmod
for test in finder.find(m, name, globs=globs, extraglobs=extraglobs):
File "/usr/lib/python2.7/doctest.py", line 900, in find
self._find(tests, obj, name, module, source_lines, globs, {})
File "/usr/lib/python2.7/doctest.py", line 954, in _find
globs, seen)
File "/usr/lib/python2.7/doctest.py", line 942, in _find
test = self._get_test(obj, name, module, globs, source_lines)
File "/usr/lib/python2.7/doctest.py", line 1026, in _get_test
filename, lineno)
File "/usr/lib/python2.7/doctest.py", line 645, in get_doctest
return DocTest(self.get_examples(string, name), globs,
File "/usr/lib/python2.7/doctest.py", line 659, in get_examples
return [x for x in self.parse(string, name)
File "/usr/lib/python2.7/doctest.py", line 621, in parse
self._parse_example(m, name, lineno)
File "/usr/lib/python2.7/doctest.py", line 679, in _parse_example
self._check_prompt_blank(source_lines, indent, name, lineno)
File "/usr/lib/python2.7/doctest.py", line 766, in _check_prompt_blank
line[indent:indent+3], line))
ValueError: line 2 of the docstring for __main__.area_tri lacks blank after >>>: '>>>area_tri(10, 10)'
Notice that you are not following doctest rules but this is another problem.