Skip to content

Commit

Permalink
Add Lexicon merge class + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bitprophet committed May 22, 2012
1 parent fefe489 commit 96d556c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lexicon/__init__.py
Expand Up @@ -3,3 +3,16 @@


__version__ = "0.1.0"


class Lexicon(AttributeDict, AliasDict):
def __init__(self, *args, **kwargs):
# Need to avoid combining AliasDict's initial attribute write on
# self.aliases, with AttributeDict's __setattr__. Doing so results in
# an infinite loop. Instead, just skip straight to dict() for both
# explicitly (i.e. we override AliasDict.__init__ instead of extending
# it.)
# NOTE: could tickle AttributeDict.__init__ instead, in case it ever
# grows one.
dict.__init__(self, *args, **kwargs)
dict.__setattr__(self, 'aliases', {})
29 changes: 29 additions & 0 deletions tests/lexicon_.py
@@ -0,0 +1,29 @@
from spec import Spec, eq_

from lexicon import Lexicon


class Lexicon_(Spec):
def attributes_work(self):
l = Lexicon()
l.foo = 'bar'
eq_(l['foo'], l.foo)

def aliases_work(self):
l = Lexicon()
l.alias('foo', to='bar')
l['bar'] = 'value'
assert l['foo'] == l['bar'] == 'value'

def aliases_appear_in_attributes(self):
l = Lexicon()
l.alias('foo', to='bar')
l.foo = 'value'
assert l.foo == l.bar == l['foo'] == l['bar'] == 'value'

def aliased_real_attributes_do_not_override_real_attributes(self):
l = Lexicon()
l.alias('get', to='notget')
l.notget = 'value'
assert callable(l.get)
assert l.get != 'value'

0 comments on commit 96d556c

Please sign in to comment.