G
Georg Brandl
Hello,
in follow-up to the recent "dictionary accumulator" thread, I wrote a
little module with several subclassed dicts.
Comments (e.g. makes it sense to use super), corrections, etc.? Is this
PEP material?
Docstrings, Documentation and test cases are to be provided later.
mfg
Georg
----------------------------------------------------------------------
class defaultdict(dict):
# _defaulttype: 0=no default, 1=defaultvalue, 2=defaultfactory
__slots__ = ['_defaulttype', '_default']
def __init__(self, *args, **kwargs):
self._defaulttype = 0
super(defaultdict, self).__init__(self, *args, **kwargs)
def setdefaultvalue(self, value):
self._defaulttype = 1
self._default = value
def setdefaultfactory(self, factory, *args, **kwargs):
if not callable(factory):
raise TypeError, 'default factory must be a callable'
self._defaulttype = 2
self._default = (factory, args, kwargs)
def cleardefault(self):
self._defaulttype = 0
def __getitem__(self, key):
try:
return super(defaultdict, self).__getitem__(key)
except KeyError:
if self._defaulttype == 0:
raise
elif self._defaulttype == 1:
return self.setdefault(key, self._default)
else:
return self.setdefault(key,
self._default[0](*self._default[1], **self._default[2]))
class keytransformdict(dict):
__slots__ = ['_transformer']
def __init__(self, *args, **kwargs):
self._transformer = lambda x: x
super(keytransformdict, self).__init__(self, *args, **kwargs)
def settransformer(self, transformer):
if not callable(transformer):
raise TypeError, 'transformer must be a callable'
self._transformer = transformer
def __setitem__(self, key, value):
super(keytransformdict, self).__setitem__(self,
self._transformer(key), value)
def __getitem__(self, key):
return super(keytransformdict, self).__getitem__(self,
self._transformer(key))
def __delitem__(self, key):
super(keytransformdict, self).__delitem__(self,
self._transformer(key))
class sorteddict(dict):
def __iter__(self):
for key in sorted(super(sorteddict, self).__iter__(self)):
yield key
def keys(self):
return list(self.iterkeys())
def items(self):
return list(self.iteritems())
def values(self):
return list(self.itervalues())
def iterkeys(self):
return iter(self)
def iteritems(self):
return ((key, self[key]) for key in self)
def itervalues(self):
return (self[key] for key in self)
if __name__ == '__main__':
x = sorteddict(a=1, b=3, c=2)
print x.keys()
print x.values()
print x.items()
in follow-up to the recent "dictionary accumulator" thread, I wrote a
little module with several subclassed dicts.
Comments (e.g. makes it sense to use super), corrections, etc.? Is this
PEP material?
Docstrings, Documentation and test cases are to be provided later.
mfg
Georg
----------------------------------------------------------------------
class defaultdict(dict):
# _defaulttype: 0=no default, 1=defaultvalue, 2=defaultfactory
__slots__ = ['_defaulttype', '_default']
def __init__(self, *args, **kwargs):
self._defaulttype = 0
super(defaultdict, self).__init__(self, *args, **kwargs)
def setdefaultvalue(self, value):
self._defaulttype = 1
self._default = value
def setdefaultfactory(self, factory, *args, **kwargs):
if not callable(factory):
raise TypeError, 'default factory must be a callable'
self._defaulttype = 2
self._default = (factory, args, kwargs)
def cleardefault(self):
self._defaulttype = 0
def __getitem__(self, key):
try:
return super(defaultdict, self).__getitem__(key)
except KeyError:
if self._defaulttype == 0:
raise
elif self._defaulttype == 1:
return self.setdefault(key, self._default)
else:
return self.setdefault(key,
self._default[0](*self._default[1], **self._default[2]))
class keytransformdict(dict):
__slots__ = ['_transformer']
def __init__(self, *args, **kwargs):
self._transformer = lambda x: x
super(keytransformdict, self).__init__(self, *args, **kwargs)
def settransformer(self, transformer):
if not callable(transformer):
raise TypeError, 'transformer must be a callable'
self._transformer = transformer
def __setitem__(self, key, value):
super(keytransformdict, self).__setitem__(self,
self._transformer(key), value)
def __getitem__(self, key):
return super(keytransformdict, self).__getitem__(self,
self._transformer(key))
def __delitem__(self, key):
super(keytransformdict, self).__delitem__(self,
self._transformer(key))
class sorteddict(dict):
def __iter__(self):
for key in sorted(super(sorteddict, self).__iter__(self)):
yield key
def keys(self):
return list(self.iterkeys())
def items(self):
return list(self.iteritems())
def values(self):
return list(self.itervalues())
def iterkeys(self):
return iter(self)
def iteritems(self):
return ((key, self[key]) for key in self)
def itervalues(self):
return (self[key] for key in self)
if __name__ == '__main__':
x = sorteddict(a=1, b=3, c=2)
print x.keys()
print x.values()
print x.items()