Skip to content

Commit 1abcb34

Browse files
author
peterhbromley
authored
Merge pull request #2638 from quantopian/fix-examples-imports
MAINT: Stop always importing all zipline examples
2 parents 008d719 + eee999c commit 1abcb34

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

tests/test_examples.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
matplotlib.use('Agg')
3838

39+
EXAMPLE_MODULES = examples.load_example_modules()
40+
3941

4042
class ExamplesTests(WithTmpDir, ZiplineTestCase):
4143
# some columns contain values with unique ids that will not be the same
@@ -62,9 +64,10 @@ def init_class_fixtures(cls):
6264
cls.tmpdir.getpath('example_data/root/data/SPY_benchmark.csv'),
6365
)
6466

65-
@parameterized.expand(sorted(examples.EXAMPLE_MODULES))
67+
@parameterized.expand(sorted(EXAMPLE_MODULES))
6668
def test_example(self, example_name):
6769
actual_perf = examples.run_example(
70+
EXAMPLE_MODULES,
6871
example_name,
6972
# This should match the invocation in
7073
# zipline/tests/resources/rebuild_example_data

zipline/examples/__init__.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@
88

99

1010
# These are used by test_examples.py to discover the examples to run.
11-
EXAMPLE_MODULES = {}
12-
for f in os.listdir(os.path.dirname(__file__)):
13-
if not f.endswith('.py') or f == '__init__.py':
14-
continue
15-
modname = f[:-len('.py')]
16-
mod = import_module('.' + modname, package=__name__)
17-
EXAMPLE_MODULES[modname] = mod
18-
globals()[modname] = mod
11+
def load_example_modules():
12+
example_modules = {}
13+
for f in os.listdir(os.path.dirname(__file__)):
14+
if not f.endswith('.py') or f == '__init__.py':
15+
continue
16+
modname = f[:-len('.py')]
17+
mod = import_module('.' + modname, package=__name__)
18+
example_modules[modname] = mod
19+
globals()[modname] = mod
1920

20-
# Remove noise from loop variables.
21-
del f, modname, mod
21+
# Remove noise from loop variables.
22+
del f, modname, mod
23+
return example_modules
2224

2325

2426
# Columns that we expect to be able to reliably deterministic
@@ -61,11 +63,11 @@
6163
]
6264

6365

64-
def run_example(example_name, environ):
66+
def run_example(example_modules, example_name, environ):
6567
"""
6668
Run an example module from zipline.examples.
6769
"""
68-
mod = EXAMPLE_MODULES[example_name]
70+
mod = example_modules[example_name]
6971

7072
register_calendar("YAHOO", get_calendar("NYSE"), force=True)
7173

zipline/examples/dual_ema_talib.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@
2727
from zipline.api import order, record, symbol
2828
from zipline.finance import commission, slippage
2929
# Import exponential moving average from talib wrapper
30-
from talib import EMA
30+
try:
31+
from talib import EMA
32+
except ImportError:
33+
msg = "Unable to import module TA-lib. Use `pip install TA-lib` to "\
34+
"install. Note: if installation fails, you might need to install "\
35+
"the underlying TA-lib library (more information can be found in "\
36+
"the zipline installation documentation)."
37+
raise ImportError(msg)
3138

3239

3340
def initialize(context):

0 commit comments

Comments
 (0)