From de8a4589e56812ef76bc3c38c76581a6c84736ba Mon Sep 17 00:00:00 2001 From: Stephen Diehl Date: Mon, 14 May 2012 14:44:06 -0400 Subject: [PATCH] Added namelookup. --- zipline/utils/protocol_utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/zipline/utils/protocol_utils.py b/zipline/utils/protocol_utils.py index 86ea1d2f..621d7ab9 100644 --- a/zipline/utils/protocol_utils.py +++ b/zipline/utils/protocol_utils.py @@ -152,3 +152,23 @@ class ndict(MutableMapping): #return False #return True + +# This is not neccesarily the most intuitive construction, but +# we're aiming for raw performance rather than readability. So +# we do things that we would not normally do in business logic. +def namelookup(dct): + ks = dct.keys() + vs = dct.values() + dct = {} + class _lookup: + __slots__ = ks + def __init__(self): + for k, v in zip(ks, vs): + setattr(self,k,v) + self.__setattr__ = self.locked + def locked(self,k,v): + raise Exception('Name lookups are fixed at init.') + def __repr__(self): + return '' % self.__slots__ + del dct + return _lookup()