From: Ben Steffen Date: Tue, 26 Nov 2019 19:20:36 +0000 (-0500) Subject: Remove struct X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-base.git/commitdiff_plain/54077f57e847f2a99d925b6eb1642f47055b5be5?ds=sidebyside Remove struct The advanced features of struct were not being used anywhere and accessing members using just a dot can be problematic (e.g. a key named "items" would break dict.items()) --- diff --git a/python/invirt/common.py b/python/invirt/common.py index 1c54a1e..ffd2e4c 100644 --- a/python/invirt/common.py +++ b/python/invirt/common.py @@ -6,50 +6,6 @@ import subprocess class InvirtConfigError(AttributeError): pass -class struct(dict): - 'A simple namespace object.' - def __init__(self, d = {}, __prefix = None, __default=None, **kwargs): - super(struct, self).__init__(d) - self.__prefix = __prefix - self.__default = __default - self.update(kwargs) - def __getattr__(self, key): - try: - return self[key] - except KeyError: - if self.__default is None: - # XX ideally these would point a frame higher on the stack. - prefix = self.__prefix - if prefix is not None: - raise InvirtConfigError('missing configuration variable ' - '%s%s' % (prefix, key)) - else: - raise AttributeError("anonymous struct has no member '%s'" - % (key,)) - else: - return struct({}, '', self.__default) - -def dicts2struct(x, prefix = None, default = None): - """ - Given a tree of lists/dicts, perform a deep traversal to transform all the - dicts to structs. - """ - if prefix is not None: - def newprefix(k): return prefix + str(k) + '.' - else: - def newprefix(k): return prefix - if type(x) == dict: - return struct(((k, dicts2struct(v, newprefix(k), default)) - for k,v in x.iteritems()), - prefix, - default) - elif type(x) == list: - return [dicts2struct(v, newprefix(i), default) - for i, v in enumerate(x)] - elif x is None: - return struct({}, prefix, default) - else: - return x @clib.contextmanager def lock_file(path, exclusive = True): @@ -112,20 +68,4 @@ class CodeError(Exception): # Tests. # -class common_tests(unittest.TestCase): - def test_dicts2structs(self): - dicts = { - 'atom': 0, - 'dict': { 'atom': 'atom', 'list': [1,2,3] }, - 'list': [ 'atom', {'key': 'value'} ] - } - structs = dicts2struct(dicts, '') - self.assertEqual(structs.atom, dicts['atom']) - self.assertEqual(structs.dict.atom, dicts['dict']['atom']) - self.assertEqual(structs.dict.list, dicts['dict']['list']) - self.assertEqual(structs.list[0], dicts['list'][0]) - self.assertEqual(structs.list[1].key, dicts['list'][1]['key']) - self.assertEqual(set(structs), set(['atom', 'dict', 'list'])) -if __name__ == '__main__': - unittest.main()