code
Suppose I have:
import numpy
import pickle
class Test():
def __init__(self):
self.base = numpy.zeros(6)
self.view = self.base[-3:]
def __len__(self):
return len(self.view)
def update(self):
self.view[0] += 1
def add(self):
self.view = self.base[-len(self.view) - 1:]
self.view[0] = 1
def __repr__(self):
return str(self.view)
def serialize_data():
data = Test()
return pickle.dumps(data)
Note that a class Testis just a class containing a viewNumPy array base. This viewis just a slice of the last elements Nin the database ( N == 3during initialization).
Testhas a method update()that adds 1to the value in the 0view position and a method add()that resizes the view ( N = N + 1) and sets the value 0to 1.
The function serialize_datasimply instantiates Test()and then returns the serialized object with pickle.
Behavior
update , add - , , :
test = Test()
print(test)
test.update()
test.update()
print(test)
test.add()
print(test)
, , add 2 ( update ) :
data = pickle.loads(serialize_data())
print(data)
data.update()
data.update()
print(data)
data.add()
print(data)
?