mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-13 12:00:16 +08:00
Merge pull request #1471 from quantopian/fix-slow-startup
PERF: Remove import-time calendar creations.
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
"""
|
||||
Tests for TradingCalendarDispatcher.
|
||||
"""
|
||||
from zipline.errors import (
|
||||
CalendarNameCollision,
|
||||
CyclicCalendarAlias,
|
||||
InvalidCalendarName,
|
||||
)
|
||||
from zipline.testing import ZiplineTestCase
|
||||
from zipline.utils.calendars.calendar_utils import TradingCalendarDispatcher
|
||||
from zipline.utils.calendars.exchange_calendar_ice import ICEExchangeCalendar
|
||||
|
||||
|
||||
class CalendarAliasTestCase(ZiplineTestCase):
|
||||
|
||||
@classmethod
|
||||
def init_class_fixtures(cls):
|
||||
super(CalendarAliasTestCase, cls).init_class_fixtures()
|
||||
# Make a calendar once so that we don't spend time in every test
|
||||
# instantiating calendars.
|
||||
cls.dispatcher_kwargs = dict(
|
||||
calendars={'ICE': ICEExchangeCalendar()},
|
||||
calendar_factories={},
|
||||
aliases={
|
||||
'ICE_ALIAS': 'ICE',
|
||||
'ICE_ALIAS_ALIAS': 'ICE_ALIAS',
|
||||
},
|
||||
)
|
||||
|
||||
def init_instance_fixtures(self):
|
||||
super(CalendarAliasTestCase, self).init_instance_fixtures()
|
||||
self.dispatcher = TradingCalendarDispatcher(
|
||||
# Make copies here so that tests that mutate the dispatcher dicts
|
||||
# are isolated from one another.
|
||||
**{k: v.copy() for k, v in self.dispatcher_kwargs.items()}
|
||||
)
|
||||
|
||||
def test_follow_alias_chain(self):
|
||||
self.assertIs(
|
||||
self.dispatcher.get_calendar('ICE_ALIAS'),
|
||||
self.dispatcher.get_calendar('ICE'),
|
||||
)
|
||||
self.assertIs(
|
||||
self.dispatcher.get_calendar('ICE_ALIAS_ALIAS'),
|
||||
self.dispatcher.get_calendar('ICE'),
|
||||
)
|
||||
|
||||
def test_add_new_aliases(self):
|
||||
with self.assertRaises(InvalidCalendarName):
|
||||
self.dispatcher.get_calendar('NOT_ICE')
|
||||
|
||||
self.dispatcher.register_calendar_alias('NOT_ICE', 'ICE')
|
||||
|
||||
self.assertIs(
|
||||
self.dispatcher.get_calendar('NOT_ICE'),
|
||||
self.dispatcher.get_calendar('ICE'),
|
||||
)
|
||||
|
||||
self.dispatcher.register_calendar_alias(
|
||||
'ICE_ALIAS_ALIAS_ALIAS',
|
||||
'ICE_ALIAS_ALIAS'
|
||||
)
|
||||
self.assertIs(
|
||||
self.dispatcher.get_calendar('ICE_ALIAS_ALIAS_ALIAS'),
|
||||
self.dispatcher.get_calendar('ICE'),
|
||||
)
|
||||
|
||||
def test_remove_aliases(self):
|
||||
self.dispatcher.deregister_calendar('ICE_ALIAS_ALIAS')
|
||||
with self.assertRaises(InvalidCalendarName):
|
||||
self.dispatcher.get_calendar('ICE_ALIAS_ALIAS')
|
||||
|
||||
def test_reject_alias_that_already_exists(self):
|
||||
with self.assertRaises(CalendarNameCollision):
|
||||
self.dispatcher.register_calendar_alias('ICE', 'NOT_ICE')
|
||||
|
||||
with self.assertRaises(CalendarNameCollision):
|
||||
self.dispatcher.register_calendar_alias('ICE_ALIAS', 'NOT_ICE')
|
||||
|
||||
def test_allow_alias_override_with_force(self):
|
||||
self.dispatcher.register_calendar_alias('ICE', 'NOT_ICE', force=True)
|
||||
with self.assertRaises(InvalidCalendarName):
|
||||
self.dispatcher.get_calendar('ICE')
|
||||
|
||||
def test_reject_cyclic_aliases(self):
|
||||
add_alias = self.dispatcher.register_calendar_alias
|
||||
|
||||
add_alias('A', 'B')
|
||||
add_alias('B', 'C')
|
||||
|
||||
with self.assertRaises(CyclicCalendarAlias) as e:
|
||||
add_alias('C', 'A')
|
||||
|
||||
expected = "Cycle in calendar aliases: ['C' -> 'A' -> 'B' -> 'C']"
|
||||
self.assertEqual(str(e.exception), expected)
|
||||
@@ -39,8 +39,12 @@ from zipline.utils.calendars import(
|
||||
deregister_calendar,
|
||||
get_calendar,
|
||||
)
|
||||
from zipline.utils.calendars.calendar_utils import register_calendar_type, \
|
||||
_default_calendar_factories
|
||||
from zipline.utils.calendars.calendar_utils import (
|
||||
_default_calendar_aliases,
|
||||
_default_calendar_factories,
|
||||
register_calendar_type,
|
||||
|
||||
)
|
||||
from zipline.utils.calendars.trading_calendar import days_at_time, \
|
||||
TradingCalendar
|
||||
|
||||
@@ -123,7 +127,8 @@ class CalendarRegistrationTestCase(TestCase):
|
||||
|
||||
class DefaultsTestCase(TestCase):
|
||||
def test_default_calendars(self):
|
||||
for name in concat(_default_calendar_factories):
|
||||
for name in concat([_default_calendar_factories,
|
||||
_default_calendar_aliases]):
|
||||
self.assertIsNotNone(get_calendar(name),
|
||||
"get_calendar(%r) returned None" % name)
|
||||
|
||||
|
||||
@@ -155,7 +155,7 @@ class BundleCoreTestCase(WithInstanceTmpDir,
|
||||
|
||||
@self.register(
|
||||
'bundle',
|
||||
calendar=calendar,
|
||||
calendar_name='NYSE',
|
||||
start_session=self.START_DATE,
|
||||
end_session=self.END_DATE,
|
||||
)
|
||||
@@ -369,7 +369,7 @@ class BundleCoreTestCase(WithInstanceTmpDir,
|
||||
"""
|
||||
if not self.bundles:
|
||||
@self.register('bundle',
|
||||
calendar=get_calendar('NYSE'),
|
||||
calendar_name='NYSE',
|
||||
start_session=pd.Timestamp('2014', tz='UTC'),
|
||||
end_session=pd.Timestamp('2014', tz='UTC'))
|
||||
def _(environ,
|
||||
|
||||
@@ -5,6 +5,7 @@ import pandas as pd
|
||||
from toolz import merge
|
||||
import toolz.curried.operator as op
|
||||
|
||||
from zipline import get_calendar
|
||||
from zipline.data.bundles import ingest, load, bundles
|
||||
from zipline.data.bundles.quandl import (
|
||||
format_wiki_url,
|
||||
@@ -28,9 +29,9 @@ class QuandlBundleTestCase(ZiplineTestCase):
|
||||
asset_start = pd.Timestamp('2014-01', tz='utc')
|
||||
asset_end = pd.Timestamp('2015-01', tz='utc')
|
||||
bundle = bundles['quandl']
|
||||
calendar = bundle.calendar
|
||||
start_date = bundle.start_session
|
||||
end_date = bundle.end_session
|
||||
calendar = get_calendar(bundle.calendar_name)
|
||||
start_date = calendar.first_session
|
||||
end_date = calendar.last_session
|
||||
api_key = 'ayylmao'
|
||||
columns = 'open', 'high', 'low', 'close', 'volume'
|
||||
|
||||
|
||||
@@ -157,7 +157,7 @@ class YahooBundleTestCase(WithResponses, ZiplineTestCase):
|
||||
self.register(
|
||||
'bundle',
|
||||
yahoo_equities(self.symbols),
|
||||
calendar=self.calendar,
|
||||
calendar_name='NYSE',
|
||||
start_session=self.asset_start,
|
||||
end_session=self.asset_end,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user