Simplify Namespace

This commit is contained in:
Tony Garnock-Jones 2021-08-16 12:37:31 -04:00
parent e2b27b619f
commit fff34b8d45
1 changed files with 12 additions and 17 deletions

View File

@ -359,29 +359,25 @@ def definition_not_found(module_path, name):
class Namespace:
def __init__(self, prefix):
super(Namespace, self).__setattr__('_items', {})
super(Namespace, self).__setattr__('_prefix', prefix)
def __getattr__(self, name):
return self[name]
def __setattr__(self, name, value):
self[name] = value
self._prefix = prefix
def __getitem__(self, name):
return self._items[Symbol(name)]
return safegetattr(self, Symbol(name).name)
def __setitem__(self, name, value):
name = Symbol(name)
if name in self._items:
raise ValueError('Name conflict: ' + module_path_str(self._prefix + (name.name,)))
self._items[name] = value
name = Symbol(name).name
if name in self.__dict__:
raise ValueError('Name conflict: ' + module_path_str(self._prefix + (name,)))
safesetattr(self, name, value)
def __contains__(self, name):
return Symbol(name) in self._items
return Symbol(name).name in self.__dict__
def _items(self):
return dict((k, v) for (k, v) in self.__dict__.items() if k[0] != '_')
def __repr__(self):
return repr(self._items)
return repr(self._items())
class Compiler:
def __init__(self):
@ -446,8 +442,7 @@ if __name__ == '__main__':
with open(path_bin_filename, 'rb') as f:
x = Decoder(f.read()).next()
print(meta.Schema.decode(x))
print(meta.Schema.decode(x) == meta.Schema.decode(x))
print(meta.Schema.decode(x)._encode())
assert meta.Schema.decode(x) == meta.Schema.decode(x)
assert meta.Schema.decode(x)._encode() == x
print()