forked from quantopian/zipline
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a new `get_column` method on `zipline.pipeline.data.DataSet` for looking up pipeline columns by name.
- Loading branch information
Scott Sanderson
committed
Apr 8, 2019
1 parent
2af3bc0
commit 9befcec
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""Tests for the zipline.pipeline.data.DataSet and related functionality. | ||
""" | ||
from textwrap import dedent | ||
|
||
from zipline.pipeline.data.dataset import Column, DataSet | ||
from zipline.testing import chrange, ZiplineTestCase | ||
from zipline.testing.predicates import assert_messages_equal | ||
|
||
|
||
class SomeDataSet(DataSet): | ||
a = Column(dtype=float) | ||
b = Column(dtype=object) | ||
c = Column(dtype=int, missing_value=-1) | ||
|
||
|
||
# A DataSet with lots of columns. | ||
class LargeDataSet(DataSet): | ||
locals().update({ | ||
name: Column(dtype=float) | ||
for name in chrange('a', 'z') | ||
}) | ||
|
||
|
||
class GetColumnTestCase(ZiplineTestCase): | ||
|
||
def test_get_column_success(self): | ||
a = SomeDataSet.a | ||
b = SomeDataSet.b | ||
c = SomeDataSet.c | ||
|
||
# Run multiple times to validate caching of descriptor return values. | ||
for _ in range(3): | ||
self.assertIs(SomeDataSet.get_column('a'), a) | ||
self.assertIs(SomeDataSet.get_column('b'), b) | ||
self.assertIs(SomeDataSet.get_column('c'), c) | ||
|
||
def test_get_column_failure(self): | ||
with self.assertRaises(AttributeError) as e: | ||
SomeDataSet.get_column('arglebargle') | ||
|
||
result = str(e.exception) | ||
expected = dedent( | ||
"""\ | ||
SomeDataSet has no column 'arglebargle': | ||
Possible choices are: | ||
- a | ||
- b | ||
- c""" | ||
) | ||
assert_messages_equal(result, expected) | ||
|
||
def test_get_column_failure_truncate_error_message(self): | ||
with self.assertRaises(AttributeError) as e: | ||
LargeDataSet.get_column('arglebargle') | ||
|
||
result = str(e.exception) | ||
expected = dedent( | ||
"""\ | ||
LargeDataSet has no column 'arglebargle': | ||
Possible choices are: | ||
- a | ||
- b | ||
- c | ||
- d | ||
- e | ||
- f | ||
- g | ||
- h | ||
- i | ||
- ... | ||
- z""" | ||
) | ||
assert_messages_equal(result, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
def bulleted_list(items, max_count=None, indent=2): | ||
"""Format a bulleted list of values. | ||
""" | ||
if max_count is not None and len(items) > max_count: | ||
item_list = list(items) | ||
items = item_list[:max_count - 1] | ||
items.append('...') | ||
items.append(item_list[-1]) | ||
|
||
line_template = (" " * indent) + "- {}" | ||
return "\n".join(map(line_template.format, items)) |