Skip to content

Commit

Permalink
Merge pull request #1970 from quantopian/better-short-repr
Browse files Browse the repository at this point in the history
ENH: Better short_repr for RecarrayField.
  • Loading branch information
Scott Sanderson committed Oct 2, 2017
2 parents fbdfaf8 + bdf6c8a commit d79c828
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/pipeline/test_factor.py
Expand Up @@ -29,6 +29,7 @@
from zipline.lib.rank import masked_rankdata_2d
from zipline.lib.normalize import naive_grouped_rowwise_apply as grouped_apply
from zipline.pipeline import Classifier, Factor, Filter
from zipline.pipeline.data import DataSet, Column
from zipline.pipeline.factors import (
CustomFactor,
Returns,
Expand Down Expand Up @@ -1175,6 +1176,36 @@ def test_winsorize(self):
r = F().winsorize(min_percentile=.05, max_percentile=.95).short_repr()
self.assertEqual(r, "GroupedRowTransform('winsorize')")

def test_recarray_field_repr(self):
class MultipleOutputs(CustomFactor):
outputs = ['a', 'b']
inputs = ()
window_length = 5

def short_repr(self):
return "CustomRepr()"

a = MultipleOutputs().a
b = MultipleOutputs().b

self.assertEqual(a.short_repr(), "CustomRepr().a")
self.assertEqual(b.short_repr(), "CustomRepr().b")

def test_latest_repr(self):

class SomeDataSet(DataSet):
a = Column(dtype=float64_dtype)
b = Column(dtype=float64_dtype)

self.assertEqual(
SomeDataSet.a.latest.short_repr(),
"SomeDataSet.a.latest"
)
self.assertEqual(
SomeDataSet.b.latest.short_repr(),
"SomeDataSet.b.latest"
)


class TestWindowSafety(TestCase):

Expand Down
3 changes: 3 additions & 0 deletions zipline/pipeline/factors/factor.py
Expand Up @@ -1615,6 +1615,9 @@ def _static_identity(cls, attribute, *args, **kwargs):
def _compute(self, windows, dates, assets, mask):
return windows[0][self._attribute]

def short_repr(self):
return "{}.{}".format(self.inputs[0].short_repr(), self._attribute)


class Latest(LatestMixin, CustomFactor):
"""
Expand Down
3 changes: 3 additions & 0 deletions zipline/pipeline/mixins.py
Expand Up @@ -241,6 +241,9 @@ def _validate(self):
)
)

def short_repr(self):
return "{}.latest".format(self.inputs[0].short_repr())


class AliasedMixin(SingleInputMixin):
"""
Expand Down
4 changes: 4 additions & 0 deletions zipline/pipeline/term.py
Expand Up @@ -348,6 +348,10 @@ def dependencies(self):
"""
raise NotImplementedError('dependencies')

def short_repr(self):
# Default short_repr is just the full repr.
return repr(self)


class AssetExists(Term):
"""
Expand Down

0 comments on commit d79c828

Please sign in to comment.