mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-06 13:00:45 +08:00
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:
+55
-4
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user