MAINT: Check attributes instead of contains for event fields.

In support of source that emits a subclass of Event which defines some
fields as properties instead of doubling the value in the
`Event.__dict__`

Use hasattr instead of the overridden __contains__ method of the Event
class, so that when non-algorithm facing code checks for field existence,
properties count.

Intentionally not touching the `__contains__` in Event, to avoid
changing, at the moment, any algo behavior that relies on the
`__contains__` behavior's use of `__dict__`
This commit is contained in:
Eddie Hebert
2014-09-08 11:09:41 -04:00
parent 6a5eaea835
commit 7eb1d719ed
3 changed files with 4 additions and 4 deletions
+2 -2
View File
@@ -53,8 +53,8 @@ class TestDataFrameSource(TestCase):
for event in source:
self.assertTrue('sid' in event)
self.assertTrue('arbitrary' in event)
self.assertTrue('volume' in event)
self.assertTrue('price' in event)
self.assertTrue(hasattr(event, 'volume'))
self.assertTrue(hasattr(event, 'price'))
self.assertEquals(event['arbitrary'], 1.)
self.assertEquals(event['sid'], 0)
self.assertTrue(isinstance(event['volume'], int))
+1 -1
View File
@@ -96,7 +96,7 @@ class ReturnsFromPriorClose(object):
We only allow events with a price field to be run through
the returns transform.
"""
if 'price' not in event:
if not hasattr(event, 'price'):
raise WrongDataForTransform(
transform="ReturnsEventWindow",
fields=['price'])
+1 -1
View File
@@ -280,7 +280,7 @@ class EventWindow(with_metaclass(ABCMeta)):
We only allow events with all of our tracked fields.
"""
# All events require a 'dt' field.
if 'dt' not in event:
if not hasattr(event, 'dt'):
raise WrongDataForTransform(
transform=self.__class__.__name__,
fields=['dt'],