snakeoil.mappings module¶
Miscellaneous mapping related classes and functionality
- class snakeoil.mappings.AttrAccessible[source]¶
Bases:
dict
Simple dict class allowing instance.x and instance[‘x’] access.
- class snakeoil.mappings.DictMixin(iterable=None, **kwargs)[source]¶
Bases:
object
new style class replacement for
UserDict.DictMixin()
designed around iter* methods rather then forcing lists as DictMixin doesTo use this mixin, you need to define the following methods:
__delitem__
__setitem__
__getitem__
keys
It’s suggested for performance reasons, it might be worth defining values and items in addition.
- get(key, default=None)¶
return
default
ifkey
is not in self, else the value associated withkey
- pop(k[, d]) v, remove specified key and return the corresponding value. [source]¶
If the key is not found, return the default if given; otherwise, raise a KeyError.
- popitem()[source]¶
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.
- setdefault(key, default=None)[source]¶
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
- class snakeoil.mappings.ImmutableDict(data=None)[source]¶
Bases:
Mapping
Immutable dict, unchangeable after instantiating.
Because this is immutable, it’s hashable.
- class snakeoil.mappings.IndeterminantDict(pull_func, starter_dict=None)[source]¶
Bases:
object
A wrapped dict with constant defaults, and a function for other keys.
The primary use for this class is to make a JIT loaded mapping- for instance, a mapping representing the filesystem that loads keys/values as it goes.
- clear()¶
- func = 'items'¶
- items()¶
- keys()¶
- pop(key, val=None)¶
- popitem()¶
- setdefault()¶
- update()¶
- values()¶
- class snakeoil.mappings.LazyFullValLoadDict(get_keys_func, get_val_func)[source]¶
Bases:
LazyValDict
Lazily load all keys for this mapping in a single load.
This is essentially the same thing as
LazyValDict
, just that the load function must return all keys in a single request.The val function must still return values one by one per key.
- class snakeoil.mappings.LazyValDict(get_keys_func, get_val_func)[source]¶
Bases:
DictMixin
Mapping that loads values via a callable.
given a function to get keys, and to look up the val for those keys, it’ll lazily load key definitions and values as requested
- class snakeoil.mappings.ProtectedDict(orig)[source]¶
Bases:
DictMixin
Mapping wrapper storing changes to a dict without modifying the original.
Changes are stored in a secondary dict, protecting the underlying mapping from changes.
- blacklist¶
- new¶
- orig¶
- class snakeoil.mappings.ProxiedAttrs(target)[source]¶
Bases:
DictMixin
Proxy mapping protocol to an object’s attributes.
Example usage:
>>> class foo: ... pass >>> obj = foo() >>> obj.x, obj.y = 1, 2 >>> d = ProxiedAttrs(obj) >>> print(d['x']) 1 >>> del d['x'] >>> print(hasattr(obj, 'x')) False
- Parameters:
target – The object to wrap.
- class snakeoil.mappings.StackedDict(*dicts)[source]¶
Bases:
DictMixin
An unmodifiable dict that makes multiple dicts appear as one
- clear() None. Remove all items from D. ¶
- class snakeoil.mappings.defaultdictkey(default_factory)[source]¶
Bases:
defaultdict
defaultdict
derivative that automatically stores any missing key/value pairs.Specifically, if instance[missing_key] is accessed, the __missing__ method automatically store self[missing_key] = self.default_factory(key).