mirror of
https://github.com/wassname/catalyst.git
synced 2026-09-11 12:00:50 +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:
@@ -92,7 +92,10 @@ from six import iteritems, itervalues
|
||||
import zipline.protocol as zp
|
||||
from . position import positiondict
|
||||
|
||||
from zipline.utils.serialization_utils import SerializeableZiplineObject
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
log = logbook.Logger('Performance')
|
||||
TRADE_TYPE = zp.DATASOURCE_TYPE.TRADE
|
||||
@@ -595,9 +598,19 @@ class PerformancePeriod(SerializeableZiplineObject):
|
||||
state_dict['_positions_store'] = \
|
||||
self._positions_get_state(self._positions_store)
|
||||
|
||||
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("PerformancePeriod saved state is too old.")
|
||||
|
||||
super(PerformancePeriod, self).__setstate__(state)
|
||||
|
||||
self.initialize_position_calc_arrays()
|
||||
|
||||
@@ -41,7 +41,10 @@ from math import (
|
||||
import logbook
|
||||
import zipline.protocol as zp
|
||||
|
||||
from zipline.utils.serialization_utils import SerializeableZiplineObject
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
log = logbook.Logger('Performance')
|
||||
|
||||
@@ -210,7 +213,23 @@ last_sale_price: {last_sale_price}"
|
||||
}
|
||||
|
||||
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("Position saved state is too old.")
|
||||
|
||||
super(Position, self).__setstate__(state)
|
||||
|
||||
|
||||
class positiondict(dict):
|
||||
|
||||
@@ -71,7 +71,10 @@ from zipline.finance import trading
|
||||
from . period import PerformancePeriod
|
||||
|
||||
from zipline.finance.trading import with_environment
|
||||
from zipline.utils.serialization_utils import SerializeableZiplineObject
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
log = logbook.Logger('Performance')
|
||||
|
||||
@@ -493,9 +496,19 @@ class PerformanceTracker(SerializeableZiplineObject):
|
||||
|
||||
state_dict['_dividend_count'] = self._dividend_count
|
||||
|
||||
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("PerformanceTracker saved state is too old.")
|
||||
|
||||
super(PerformanceTracker, self).__setstate__(state)
|
||||
|
||||
# Handle the dividend frame specially
|
||||
|
||||
Reference in New Issue
Block a user