1from .attribute_dict import AttributeDict
2from .alias_dict import AliasDict
3
4
5class Lexicon(AttributeDict, AliasDict):
6    def __init__(self, *args, **kwargs):
7        # Need to avoid combining AliasDict's initial attribute write on
8        # self.aliases, with AttributeDict's __setattr__. Doing so results in
9        # an infinite loop. Instead, just skip straight to dict() for both
10        # explicitly (i.e. we override AliasDict.__init__ instead of extending
11        # it.)
12        # NOTE: could tickle AttributeDict.__init__ instead, in case it ever
13        # grows one.
14        dict.__init__(self, *args, **kwargs)
15        dict.__setattr__(self, 'aliases', {})
16
17    def __getattr__(self, key):
18        # Intercept deepcopy/etc driven access to self.aliases when not
19        # actually set. (Only a problem for us, due to abovementioned combo of
20        # Alias and Attribute Dicts, so not solvable in a parent alone.)
21        if key == 'aliases' and key not in self.__dict__:
22            self.__dict__[key] = {}
23        return super(Lexicon, self).__getattr__(key)
24