Replies: 4 comments
-
|
I think this is related to how pickle works: https://docs.python.org/3/library/pickle.html#pickling-class-instances
Pickle cannot introspect the generated code from Python. Maybe try defining |
Beta Was this translation helpful? Give feedback.
-
|
This seems to work. Currently I can't return # pickling_kers.py
class Person:
def __init__(self, x : int):
self.x = x
def __getnewargs__(self):
return (self.x,)import pickle
import os
from pickling_kers import Person
person = Person(2)
with open(os.path.join(os.getcwd(), "person.bin"), 'wb') as f:
pickle.dump(person, f, pickle.HIGHEST_PROTOCOL) |
Beta Was this translation helpful? Give feedback.
-
|
Thanks @EmilyBourne - that helps! However, another problem seems to be that the pyccelized class does not have a import pickle
import os
from pickling_kers import Person
person = Person(3)
print(f"{person.x = }")
try:
print(f"{person.__dict__ = }")
except:
print(f"{person} has no __dict__")
with open(os.path.join(os.getcwd(), "person.bin"), 'wb') as f:
pickle.dump(person, f, pickle.HIGHEST_PROTOCOL)
with open(os.path.join(os.getcwd(), "person.bin"), 'rb') as f:
person_reloaded: Person = pickle.load(f)
print(f"{person_reloaded.x = }")
print(f"{person_reloaded.__dict__ = }")yields person.x = 3
<pickling_kers.Person object at 0x75b3d1a4cc00> has no __dict__
person_reloaded.x = 129415187020624
Traceback (most recent call last):
File "/home/IPP-AD/spossann/git_repos/struphy/pickling.py", line 19, in <module>
print(f"{person_reloaded.__dict__ = }")
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'pickling_kers.Person' object has no attribute '__dict__'. Did you mean: '__dir__'?I tried to add # pickling_kers.py
class Person:
def __init__(self, x : int):
self.x = x
def __getnewargs__(self):
return (self.x,)
@property
def __dict__(self):
dct = dict(x=self.x)
return dctbut it doesn't seem to compile for me. I am not really sure how to use dicts though with pyccel. |
Beta Was this translation helpful? Give feedback.
-
This is expected for a class found in an external library. It is not be possible to use In Python, each object normally has a However, Classes without a What are slots and why are they usedhttps://wiki.python.org/moin/UsingSlots
Python based exampleIf we look at this with the example: # pickling_kers.py
class Person:
def __init__(self, x : int):
self.x = x
def print_x(self):
print(self.x)is effectively translated to: # pickling_kers.py
class Person:
def __init__(self, x : int):
self._c_x = x
@property
def x(self):
self._c_x
@x.setter
def x(self, x : int):
self._c_x = x
def print_x(self):
print(self._c_x)where >>> p = Person(5)
>>> p.__dict__
{'_x': 5}
>>> p.x # Calls the getter
5
>>> p.__dict__.update(x=3)
>>> p.__dict__
{'_x': 5, 'x': 3}
>>> p.x # Calls the getter
5
>>> p.x = 6 # Calls the setter
>>> p.__dict__
{'_x': 6, 'x': 3}
>>> p.x # Calls the getter
6 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there an existing issue for this?
Feature Description
It seems that pickling classes that have been pyccelized is not possible at the moment.
After the
pyccel pickling_kers.py, the following codeyields
Any idea how to figure out what is going wrong?
Use Case
Being able to store pyccelized objects.
Benefits
No response
Add Screenshots
No response
Priority
Medium
Record
Beta Was this translation helpful? Give feedback.
All reactions