Package: lexicon
WHATLexicon is a simple collection of Pythondictsubclasses providing extra
power:AliasDict, a dictionary supporting both simple and complex key aliasing:Alias a single key to another key, so that e.g.mydict['bar']points
tomydict['foo'], for both reads and writes.Alias a single key to a list of other keys, for writing only, e.g. withactive_groups =AliasDict({'ops':True, 'biz': True, 'dev': True, 'product': True})one can make an alias'tech'mapping to('ops', 'dev')and then e.g.active_groups['tech']= False.Aliasing is recursive: an alias pointing to another alias will behave as
if it points to the other alias’ target.AttributeDict, supporting attribute read & write access, e.g.mydict =AttributeDict({'foo':'bar'})exhibitsmydict.fooandmydict.foo = 'new value'.Lexicon, a subclass of both of the above which exhibits both sets of
behavior.HOWpip install lexiconfrom lexicon import Lexicon(or one of the superclasses)Use as needed.If you have a clone of the source repository, you can run the tests like so:pip install-rdev-requirements.txtinv testAPIAliasDictIn all examples,'myalias'is the alias and'realkey'is the “real”,
unaliased key.alias(from_'myalias',to='realkey'): Aliasmyaliastorealkeysod['myalias']behaves exactly liked['realkey']for both reads and
writes.from_is the first keyword argument, but typically it can be omitted
and still reads fine. See below examples for this usage. See below for
details on how an alias affects other dict operations.alias('myalias',to=('realkey','otherrealkey')): Aliasmyaliasto
bothrealkeyandotherrealkey. As you might expect, this only works
well for writes, as there is never any guarantee that all targets of the
alias will contain the same value.unalias('myalias'): Removes themyaliasalias; any subsequent
reads/writes tomyaliaswill behave as normal for a regulardict.'myalias' in d(aka__contains__): Returns True when given an alias,
so ifmyaliasis an alias to some other key, dictionary membership tests
will behave as ifmyaliasis set.deld['myalias'](aka__delitem__): This effectively becomesdeld['realkey']– to remove the alias itself, useunalias().deld['realkey']: Deletes the real key/value pair (i.e. it callsdict.__del__) but doesn’t touch any aliases pointing torealkey.As a result, “dangling” aliases pointing to nonexistent keys will raiseKeyErroron access, but will continue working if the target key is
repopulated later.Caveats:Because of the single-key/multi-key duality,AliasDictis incapable of
honoring non-string-type keys when aliasing (it must testisinstance(key, basestring)to tell strings apart from non-string iterables).AliasDictinstances may stillusenon-string keys, of course – it
just can’t use them as alias targets.AttributeDictd.key = 'value'(aka__setattr__): Maps directly tod['key']= 'value'.d.key(aka__getattr__): Maps directly tod['key'].del d.key(aka__delattr__): Maps directly todeld['key'].Collisions between “real” or pre-existing attributes, and
attributes-as-dict-keys, always results in the real attribute winning. Thus
it isn’t possible to use attribute access to access e.g.d['get'].LexiconLexicon subclasses fromAttributeDictfirst, thenAliasDict, with the
end result that attribute access will honor aliases. E.g.:d = Lexicon()
d.alias(‘myalias’, to=’realkey’)
d.myalias = ‘foo’
print d.realkey # prints ‘foo’