From d6ad73e0648317fe3ccc27e6b984826f7b622073 Mon Sep 17 00:00:00 2001 From: Scott Sanderson Date: Wed, 7 Sep 2016 20:42:19 -0400 Subject: [PATCH] MAINT: Updates from Joe's PR feedback. --- tests/calendars/test_calendar_dispatcher.py | 8 ++++---- tests/data/bundles/test_core.py | 2 +- zipline/__init__.py | 6 ++++-- zipline/data/bundles/core.py | 3 --- zipline/data/bundles/quandl.py | 7 ++++++- zipline/data/bundles/yahoo.py | 3 +++ zipline/utils/calendars/calendar_utils.py | 14 ++++++++------ 7 files changed, 26 insertions(+), 17 deletions(-) diff --git a/tests/calendars/test_calendar_dispatcher.py b/tests/calendars/test_calendar_dispatcher.py index 50e3ee33..0fcca97d 100644 --- a/tests/calendars/test_calendar_dispatcher.py +++ b/tests/calendars/test_calendar_dispatcher.py @@ -37,12 +37,12 @@ class CalendarAliasTestCase(ZiplineTestCase): def test_follow_alias_chain(self): self.assertIs( - self.dispatcher.get_calendar('ICE'), self.dispatcher.get_calendar('ICE_ALIAS'), + self.dispatcher.get_calendar('ICE'), ) self.assertIs( - self.dispatcher.get_calendar('ICE'), self.dispatcher.get_calendar('ICE_ALIAS_ALIAS'), + self.dispatcher.get_calendar('ICE'), ) def test_add_new_aliases(self): @@ -52,8 +52,8 @@ class CalendarAliasTestCase(ZiplineTestCase): self.dispatcher.register_calendar_alias('NOT_ICE', 'ICE') self.assertIs( - self.dispatcher.get_calendar('ICE'), self.dispatcher.get_calendar('NOT_ICE'), + self.dispatcher.get_calendar('ICE'), ) self.dispatcher.register_calendar_alias( @@ -61,8 +61,8 @@ class CalendarAliasTestCase(ZiplineTestCase): 'ICE_ALIAS_ALIAS' ) self.assertIs( - self.dispatcher.get_calendar('ICE'), self.dispatcher.get_calendar('ICE_ALIAS_ALIAS_ALIAS'), + self.dispatcher.get_calendar('ICE'), ) def test_remove_aliases(self): diff --git a/tests/data/bundles/test_core.py b/tests/data/bundles/test_core.py index 6529b40e..aea7fab0 100644 --- a/tests/data/bundles/test_core.py +++ b/tests/data/bundles/test_core.py @@ -369,7 +369,7 @@ class BundleCoreTestCase(WithInstanceTmpDir, """ if not self.bundles: @self.register('bundle', - calendar_name=('NYSE'), + calendar_name='NYSE', start_session=pd.Timestamp('2014', tz='UTC'), end_session=pd.Timestamp('2014', tz='UTC')) def _(environ, diff --git a/zipline/__init__.py b/zipline/__init__.py index 671e91c1..765e28d7 100644 --- a/zipline/__init__.py +++ b/zipline/__init__.py @@ -41,14 +41,16 @@ from . import utils from .utils.calendars import get_calendar from .utils.run_algo import run_algorithm from ._version import get_versions -__version__ = get_versions()['version'] -del get_versions # These need to happen after the other imports. from . algorithm import TradingAlgorithm from . import api +__version__ = get_versions()['version'] +del get_versions + + def load_ipython_extension(ipython): from .__main__ import zipline_magic ipython.register_magic_function(zipline_magic, 'line_cell', 'zipline') diff --git a/zipline/data/bundles/core.py b/zipline/data/bundles/core.py index e3c56279..85f57c24 100644 --- a/zipline/data/bundles/core.py +++ b/zipline/data/bundles/core.py @@ -619,6 +619,3 @@ def _make_bundle_core(): return BundleCore(bundles, register, unregister, ingest, load, clean) bundles, register, unregister, ingest, load, clean = _make_bundle_core() - -register_calendar_alias("YAHOO", "NYSE") -register_calendar_alias("QUANDL", "NYSE") diff --git a/zipline/data/bundles/quandl.py b/zipline/data/bundles/quandl.py index 9e0d530d..d13bb36b 100644 --- a/zipline/data/bundles/quandl.py +++ b/zipline/data/bundles/quandl.py @@ -12,9 +12,11 @@ import pandas as pd import requests from six.moves.urllib.parse import urlencode -from . import core as bundles +from zipline.utils.calendars import register_calendar_alias from zipline.utils.cli import maybe_show_progress +from . import core as bundles + log = Logger(__name__) seconds_per_call = (pd.Timedelta('10 minutes') / 2000).total_seconds() # Invalid symbols that quandl has had in its metadata: @@ -402,3 +404,6 @@ def quantopian_quandl_bundle(environ, if show_progress: print("Writing data to %s." % output_dir) tar.extractall(output_dir) + + +register_calendar_alias("QUANDL", "NYSE") diff --git a/zipline/data/bundles/yahoo.py b/zipline/data/bundles/yahoo.py index 0304113e..ace6a405 100644 --- a/zipline/data/bundles/yahoo.py +++ b/zipline/data/bundles/yahoo.py @@ -5,6 +5,7 @@ import pandas as pd from pandas_datareader.data import DataReader import requests +from zipline.utils.calendars import register_calendar_alias from zipline.utils.cli import maybe_show_progress from .core import register @@ -198,3 +199,5 @@ register( pd.Timestamp('2015-01-01', tz='utc'), ), ) + +register_calendar_alias("YAHOO", "NYSE") diff --git a/zipline/utils/calendars/calendar_utils.py b/zipline/utils/calendars/calendar_utils.py index a3175799..32a59aab 100644 --- a/zipline/utils/calendars/calendar_utils.py +++ b/zipline/utils/calendars/calendar_utils.py @@ -48,8 +48,9 @@ class TradingCalendarDispatcher(object): calendars : dict[str -> TradingCalendar] Initial set of calendars. calendar_factories : dict[str -> function] - Factories for lazy + Factories for lazy calendar creation. aliases : dict[str -> str] + Calendar name aliases. """ def __init__(self, calendars, calendar_factories, aliases): self._calendars = calendars @@ -211,17 +212,18 @@ class TradingCalendarDispatcher(object): """ # Use an OrderedDict as an ordered set so that we can return the order # of aliases in the event of a cycle. - seen = OrderedDict({name: None}) + seen = [] while name in self._aliases: - seen[name] = None + seen.append(name) name = self._aliases[name] + # This is O(N ** 2), but if there's an alias chain longer than 2, + # something strange has happened. if name in seen: - cycle = seen.keys() - cycle.append(name) + seen.append(name) raise CyclicCalendarAlias( - cycle=" -> ".join([repr(k) for k in cycle]) + cycle=" -> ".join(repr(k) for k in seen) ) return name