def _replacedata(self, data): _odict.clear(self) if isinstance(data, dict): data = [ i for i in data.items() ] data = make_safe(data) _odict.update(self, data) if data: self.__dict__.update(data)
def __setattr__(self, key, value): from types import StringType if type(key) is not StringType: raise KeyError, 'Key was not a string or integer' if key in self and _odict.__getattribute__(self, '__lock'): raise KeyError, 'Dictionary is locked, delete item to reassign to key' _odict.__setattr__(self, key, value) if key in ['_OrderedDict__end', '_OrderedDict__map'] or key.startswith('__'): return # ignore internal attributes used by ordereddict implementation _odict.__setitem__(self, key, value)
def __init__(self, data=None, lock=False): ''' A dictionary or list of tuples of key/value pairs. If lock=True, keys cannot be reassigned without first deleting the item ''' # super(ListDict, self).__setattr__('__lock', lock) _odict.__setattr__(self, '__lock', lock) #self.__lock = lock #setattr__(self, '__lock', lock) if isinstance(data, dict): data = [ i for i in data.items() ] data = make_safe(data) _odict.__init__(self, data) if data: self.__dict__.update(data)
def __delitem__(self, key): ''' Key can be a number (integer) in which case set value at index of key list ''' from types import StringType, IntType if type(key) is IntType: if key > len(self): raise IndexError, 'Key was too large' key = _odict.keys(self)[key] if type(key) is StringType: _odict.__delitem__(self, key) _odict.__delattr__(self, key) else: raise KeyError, 'Key was not a string or integer'
def __str__(self): s = "" for k in self: #_odict.__iter__(self): s += "('" + k + "', " + str(self[k]) + "), " if len(s) > 0: s = s[:-2] return _odict.__class__(_odict(self)).__name__ + "([" + s + "])"
def __repr__(self): s = "" for k in self.__odict__: #_odict.__iter__(self): s += "('" + k + "', " + str(self[k]) + "), " if len(s) > 0: s = s[:-2] return _odict.__class__(_odict(self)).__name__ + "([" + s + "])"
def __setitem__(self, key, value): ''' Key can be a number (integer) in which case set value at index of key list ''' from types import StringType, IntType if type(key) is IntType: if key > len(self): raise IndexError, 'Key was too large' key = _odict.keys(self)[key] if type(key) is StringType: key = sanitise_name(key) if key in self and _odict.__getattribute__(self, '__lock'): raise KeyError, 'Dictionary is locked, delete item to reassign to key' _odict.__setitem__(self, key, value) _odict.__setattr__(self, key, value) else: raise KeyError, 'Key was not a string or integer'