From b3de0a2fccf5cf3a33426b9a85c84b7f307994e8 Mon Sep 17 00:00:00 2001 From: Stephen Diehl Date: Wed, 18 Apr 2012 11:18:48 -0400 Subject: [PATCH] Added ndict test suite. --- zipline/protocol_utils.py | 10 ++++++++- zipline/test/test_ndict.py | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 zipline/test/test_ndict.py diff --git a/zipline/protocol_utils.py b/zipline/protocol_utils.py index 578cf2d5..8411ef54 100644 --- a/zipline/protocol_utils.py +++ b/zipline/protocol_utils.py @@ -147,7 +147,7 @@ class ndict(MutableMapping): def __iter__(self): return self.__internal.iterkeys() - def __len__(self, key): + def __len__(self): return len(self.__internal) # Compatability with namedicts @@ -163,9 +163,15 @@ class ndict(MutableMapping): """ 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. @@ -212,3 +218,5 @@ class ndict(MutableMapping): #return False #return True + +namedict = ndict diff --git a/zipline/test/test_ndict.py b/zipline/test/test_ndict.py new file mode 100644 index 00000000..b214ac74 --- /dev/null +++ b/zipline/test/test_ndict.py @@ -0,0 +1,46 @@ +from zipline.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') == None + assert nd.get('y', 'fizzpop') == 'fizzpop' + assert nd.has_key('x') == True + assert nd.has_key('y') == False + + assert 'x' in nd + assert 'y' not in nd + + # Class isolation + assert '__init__' not in nd + assert '__iter__' not in nd + assert not nd.__dict__.has_key('x') + assert nd.get('__init__') is None + + # 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 not nd.has_key('x') + assert nd.get('x') is None