ENH: Added versioning logic to objects.

In order to be able to load from saved state generated by old
code, we need to have a notion of the version of the saved state.
This commit is contained in:
Delaney Granizo-Mackenzie
2015-03-04 14:17:12 -05:00
parent 64eed84bff
commit c6596e2ee2
11 changed files with 297 additions and 17 deletions
+55 -4
View File
@@ -22,7 +22,10 @@ from . utils.math_utils import nanstd, nanmean, nansum
from zipline.finance.trading import with_environment
from zipline.utils.algo_instance import get_algo_instance
from zipline.utils.serialization_utils import SerializeableZiplineObject
from zipline.utils.serialization_utils import (
SerializeableZiplineObject,
VERSION_LABEL
)
# Datasource type should completely determine the other fields of a
# message with its type.
@@ -140,7 +143,23 @@ class Portfolio(SerializeableZiplineObject):
return "Portfolio({0})".format(self.__dict__)
def __getstate__(self):
return self.__dict__
state_dict = self.__dict__
STATE_VERSION = 1
state_dict[VERSION_LABEL] = STATE_VERSION
return state_dict
def __setstate__(self, state):
OLDEST_SUPPORTED_STATE = 1
version = state.pop(VERSION_LABEL)
if version < OLDEST_SUPPORTED_STATE:
raise BaseException("Portfolio saved state is too old.")
super(Portfolio, self).__setstate__(state)
class Account(SerializeableZiplineObject):
@@ -176,7 +195,23 @@ class Account(SerializeableZiplineObject):
return "Account({0})".format(self.__dict__)
def __getstate__(self):
return self.__dict__
state_dict = self.__dict__
STATE_VERSION = 1
state_dict[VERSION_LABEL] = STATE_VERSION
return state_dict
def __setstate__(self, state):
OLDEST_SUPPORTED_STATE = 1
version = state.pop(VERSION_LABEL)
if version < OLDEST_SUPPORTED_STATE:
raise BaseException("Account saved state is too old.")
super(Account, self).__setstate__(state)
class Position(SerializeableZiplineObject):
@@ -194,7 +229,23 @@ class Position(SerializeableZiplineObject):
return "Position({0})".format(self.__dict__)
def __getstate__(self):
return self.__dict__
state_dict = self.__dict__
STATE_VERSION = 1
state_dict[VERSION_LABEL] = STATE_VERSION
return state_dict
def __setstate__(self, state):
OLDEST_SUPPORTED_STATE = 1
version = state.pop(VERSION_LABEL)
if version < OLDEST_SUPPORTED_STATE:
raise BaseException("Protocol Position saved state is too old.")
super(Position, self).__setstate__(state)
class Positions(dict):