diff --git a/tests/test_ndict.py b/tests/test_ndict.py deleted file mode 100644 index b3f88a09..00000000 --- a/tests/test_ndict.py +++ /dev/null @@ -1,107 +0,0 @@ -# -# Copyright 2012 Quantopian, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from datetime import datetime -import pytz - -from copy import deepcopy - -from zipline.utils.protocol_utils import ndict - - -def test_ndict(): - nd = ndict({}) - - # Properties - assert len(nd) == 0 - assert nd.keys() == [] - assert nd.values() == [] - assert list(nd.iteritems()) == [] - - # Accessors - nd['x'] = 1 - assert nd.x == 1 - assert nd['x'] == 1 - assert nd.get('y') is None - assert nd.get('y', 'fizzpop') == 'fizzpop' - assert 'x' in nd - assert 'y' not in nd - - assert 'x' in nd - assert 'y' not in nd - - # Mutability - nd2 = ndict({'x': 1}) - assert nd2.x == 1 - nd2.x = 2 - assert nd2.x == 2 - - # Class isolation - assert '__init__' not in nd - assert '__iter__' not in nd - assert 'x' not in nd.__dict__ - assert nd.get('__init__') is None - assert 'x' not in set(dir(nd)) - - # Comparison - nd2 = nd.copy() - assert id(nd2) != id(nd) - assert nd2 == nd - nd2['z'] = 3 - assert nd2 != nd - - class ndictlike(object): - x = 1 - - assert {'x': 1} == nd - assert ndictlike() != nd - - # Deletion - del nd['x'] - assert 'x' not in nd - assert nd.get('x') is None - - for n in xrange(1000): - dt = datetime.utcnow().replace(tzinfo=pytz.utc) - nd2 = ndict({"dt": dt, - "otherdata": "ishere" * 1000, - "maybeanint": 3}) - - nd2.dt2 = dt - - -def test_ndict_deepcopy(): - def assert_correctly_copied(orig, copy): - assert nd == nd_dc, \ - "Deepcopied ndict should have same keys and values." - - nd_dc.z = 3 - assert 'z' not in nd, "'z' also added to original ndict." - - nd_dc.y = 10 - assert nd_dc.y == 10, "value of copied ndict not correctly set." - assert nd.y != 10, "value also set of original ndict." - - nd = ndict({'x': 1, 'y': 2}) - nd_dc = deepcopy(nd) - assert_correctly_copied(nd, nd_dc) - - nd = ndict({'x': [1, 2, 3], - 'y': {1: 1}}) - nd_dc = deepcopy(nd) - assert_correctly_copied(nd, nd_dc) - nd_dc.x.append(4) - assert nd_dc.x[-1] == 4, "not correctly appended to copied." - assert nd.x[-1] != 4, "also copied to original." diff --git a/tests/test_transforms.py b/tests/test_transforms.py index b803fb36..3fd4948a 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -22,10 +22,9 @@ import pandas as pd from datetime import timedelta, datetime from unittest import TestCase -from zipline import ndict - from zipline.utils.test_utils import setup_logger +from zipline.protocol import Event from zipline.sources import SpecificEquityTrades from zipline.transforms.utils import StatefulTransform, EventWindow from zipline.transforms import MovingVWAP @@ -38,7 +37,7 @@ from zipline.test_algorithms import BatchTransformAlgorithm def to_dt(msg): - return ndict({'dt': msg}) + return Event({'dt': msg}) class NoopEventWindow(EventWindow): diff --git a/zipline/__init__.py b/zipline/__init__.py index a20d73f9..2e01cd18 100644 --- a/zipline/__init__.py +++ b/zipline/__init__.py @@ -5,7 +5,6 @@ Zipline # This is *not* a place to dump arbitrary classes/modules for convenience, # it is a place to expose the public interfaces. -from zipline.utils.protocol_utils import ndict import data import finance @@ -15,7 +14,6 @@ import utils from algorithm import TradingAlgorithm __all__ = [ - 'ndict', 'data', 'finance', 'gens', diff --git a/zipline/gens/utils.py b/zipline/gens/utils.py index de325cff..5cfdc01b 100644 --- a/zipline/gens/utils.py +++ b/zipline/gens/utils.py @@ -73,9 +73,3 @@ def assert_sort_unframe_protocol(event): """Same as above.""" assert isinstance(event.source_id, basestring) assert event.type in DATASOURCE_TYPE - - -def assert_merge_protocol(tnfm_ids, message): - """Merge should output an ndict with a field for each id - in its transform set.""" - assert set(tnfm_ids) == set(message.keys()) diff --git a/zipline/test_algorithms.py b/zipline/test_algorithms.py index c8a6134a..09fc45d0 100644 --- a/zipline/test_algorithms.py +++ b/zipline/test_algorithms.py @@ -32,8 +32,8 @@ The algorithm must expose methods: sids. List must have a length between 1 and 10. If None is returned the filter will block all events. - - handle_data: method that accepts a :py:class:`zipline.protocol_utils.ndict` - of the current state of the simulation universe. An example data ndict: + - handle_data: method that accepts a :py:class:`zipline.protocol.BarData` + of the current state of the simulation universe. An example data object: .. This outputs the table as an HTML table but for some reason there is no bounding box. Make the previous paragraph ending colon a diff --git a/zipline/transforms/mavg.py b/zipline/transforms/mavg.py index d07a632c..6168c23f 100644 --- a/zipline/transforms/mavg.py +++ b/zipline/transforms/mavg.py @@ -67,7 +67,7 @@ class MovingAverage(object): def update(self, event): """ - Update the event window for this event's sid. Return an ndict + Update the event window for this event's sid. Return a dict from tracked fields to moving averages. """ # This will create a new EventWindow if this is the first @@ -141,7 +141,7 @@ class MovingAverageEventWindow(EventWindow): def get_averages(self): """ - Return an ndict of all our tracked averages. + Return a dict of all our tracked averages. """ out = Averages() for field in self.fields: diff --git a/zipline/transforms/stddev.py b/zipline/transforms/stddev.py index 3600c3f4..dddfa745 100644 --- a/zipline/transforms/stddev.py +++ b/zipline/transforms/stddev.py @@ -63,7 +63,7 @@ class MovingStandardDev(object): def update(self, event): """ - Update the event window for this event's sid. Return an ndict + Update the event window for this event's sid. Return a dict from tracked fields to moving averages. """ # This will create a new EventWindow if this is the first diff --git a/zipline/utils/__init__.py b/zipline/utils/__init__.py index 1bdceb03..e69de29b 100644 --- a/zipline/utils/__init__.py +++ b/zipline/utils/__init__.py @@ -1,4 +0,0 @@ -from protocol_utils import ndict -__all__ = [ - ndict, -] diff --git a/zipline/utils/protocol_utils.py b/zipline/utils/protocol_utils.py index e9f6fe0d..f4531175 100644 --- a/zipline/utils/protocol_utils.py +++ b/zipline/utils/protocol_utils.py @@ -13,11 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. - -import copy -import pandas from ctypes import Structure, c_ubyte -from collections import MutableMapping def Enum(*options): @@ -30,144 +26,3 @@ def Enum(*options): _fields_ = [(o, c_ubyte) for o in options] __iter__ = lambda s: iter(range(len(options))) return cstruct(*range(len(options))) - - -class ndict(MutableMapping): - """ - Xtreme Namedicts 2.0 - - Ndicts are dict like objects that have fields accessible by attribute - lookup as well as being indexable and iterable. Done right - this time. - """ - - cls = None - __slots__ = ['cls', '__internal'] - - def __init__(self, dct=None, internal=None): - if internal is not None: - self.__internal = internal - else: - self.__internal = dict() - - if not ndict.cls: - ndict.cls = frozenset(dir(self)) - - if dct: - self.__internal.update(dct) - - # Abstact Overloads - # ----------------- - - def __deepcopy__(self, memo): - return ndict(copy.deepcopy(self.__internal)) - - def __setattr__(self, key, value): - if key == 'cls' or key == '__internal' or '_ndict' in key: - super(ndict, self).__setattr__(key, value) - else: - self.__internal[key] = value - return value - - def __setitem__(self, key, value): - """ - Required for use by pymongo as_class parameter to find. - """ - if key == '_id': - self.__internal['id'] = value - else: - self.__internal[key] = value - - def __getattr__(self, key): - if key in self.cls: - super(ndict, self).__getattr__(key) - else: - return self.__internal[key] - - def __getitem__(self, key): - return self.__internal[key] - - def __delitem__(self, key): - del self.__internal[key] - - def __iter__(self): - return self.__internal.iterkeys() - - def __len__(self): - return len(self.__internal) - - #TODO: Eddie please help! - def __contains__(self, key): - if hasattr(self, '_ndict_contains__'): - return self._ndict_contains__(key) - else: - return self.__internal.__contains__(key) - - # Compatability with namedicts - # ---------------------------- - - # for compat, not the Python way to do things though... - # Deprecated, use builtin ``del`` operator. - delete = __delitem__ - - def has_attr(self, key): - """ - Deprecated, use builtin ``in`` operator. - """ - return self.__contains__(key) - - def has_key(self, key): - return self.__contains__(key) - - # Custom Methods - # -------------- - - def copy(self): - return ndict(copy.copy(self.__internal)) - - def as_dataframe(self): - """ - Return the representation as a Pandas dataframe. - """ - d = pandas.DataFrame(self.__internal) - return d - - def as_series(self): - """ - Return the representation as a Pandas time series. - """ - s = pandas.Series(self.__internal) - s.name = self.sid - return s - - def as_dict(self): - """ - Return the representation as a vanilla Python dict. - """ - # shallow copy is O(n) - return copy.copy(self.__internal) - - def merge(self, other_nd): - """ - Merge in place with another ndict. - """ - assert isinstance(other_nd, ndict) - self.__internal.update(other_nd.__internal) - - def __repr__(self): - return "ndict(%s)" % str(self.__internal) - - # Faster dictionary comparison? - #def __eq__(self, other): - #assert isinstance(other, ndict) - - #keyeq = set(self.keys()) == set(other.keys()) - - #if not keyeq: - #return False - - #for i, j in izip(self.itervalues(), other.itervalues()): - #if i != j: - #return False - - #return True