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
+14 -1
View File
@@ -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()
+21 -2
View File
@@ -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):
+14 -1
View File
@@ -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