Changeset 2592 for trunk/packages


Ignore:
Timestamp:
Dec 14, 2009, 8:50:43 PM (15 years ago)
Author:
broder
Message:

Subclass invirt.common.struct from dict, instead of rolling our own
almost-dict thing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/invirt-base/python/invirt/common.py

    r2551 r2592  
    99    pass
    1010
    11 class struct(object):
     11class struct(dict):
    1212    'A simple namespace object.'
    1313    def __init__(self, d = {}, __prefix = None, **kwargs):
    14         'd is the dictionary or the items-iterable to update my __dict__ with.'
    15         dct = {}
    16         dct.update(d)
    17         dct.update(kwargs)
    18         self.__dict__.update(dct)
    19         self.__keys = set(dct)
     14        super(struct, self).__init__(d)
    2015        self.__prefix = __prefix
     16        self.update(kwargs)
    2117    def __getattr__(self, key):
    22         # XX ideally these would point a frame higher on the stack.
    23         prefix = self.__prefix
    24         if prefix is not None:
    25             raise InvirtConfigError('missing configuration variable %s%s'
    26                                     % (prefix, key))
    27         else:
    28             raise AttributeError("anonymous struct has no member '%s'"
    29                                  % (key,))
    30     def __iter__(self):
    31         for i in self.__keys:
    32             yield i
     18        try:
     19            return self[key]
     20        except KeyError:
     21            # XX ideally these would point a frame higher on the stack.
     22            prefix = self.__prefix
     23            if prefix is not None:
     24                raise InvirtConfigError('missing configuration variable %s%s'
     25                                        % (prefix, key))
     26            else:
     27                raise AttributeError("anonymous struct has no member '%s'"
     28                                     % (key,))
    3329
    3430def dicts2struct(x, prefix = None):
Note: See TracChangeset for help on using the changeset viewer.