I have some data in the pickled format with instances of below objects:
Point = namedtuple('Point', ['x',])
Now I want to extend the Point object to add another variable 'y' but also keep this compatible with the data I already have pickled.
Below is what I tried, but seems to fail. I also tried playing around with the code to make a Point class by setting the y parameter as optional when making the Point object, but that did not work as well. Any ideas on how to proceed?
from collections import namedtuple
Point = namedtuple('Point', ['x'])
i = Point(1)
import cPickle
pick=cPickle.dumps(i)
Point = namedtuple('Point', ['x', 'y'])
cPickle.loads(pick)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 22, in __repr__
TypeError: not enough arguments for format string
Tuples are immutable.
Unpickle first, than make a new point with a different namedtuple class and use the attribute x
from the old point.
Using the same name for the class Point
should have the effect you are after:
Point = namedtuple('Point', ['x'])
i = Point(1)
pick=cPickle.dumps(i)
def unpickle_point(dumped_obj):
Point = namedtuple('Point', ['x'])
point = cPickle.loads(pick)
Point = namedtuple('Point', ['x', 'y'])
return Point(point.x, 2)
new_point = unpickle_point(pick)
Now:
>>> new_point
Point(x=1, y=2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With