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 does

To 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.

clear() None.  Remove all items from D.[source]
get(key, default=None)

return default if key is not in self, else the value associated with key

items() a set-like object providing a view on D's items[source]
keys() a set-like object providing a view on D's keys[source]
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.

update([E, ]**F) None.  Update D from dict/iterable E and F.[source]

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values[source]
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'
get(key, val=None)[source]
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

items() a set-like object providing a view on D's items[source]
keys() a set-like object providing a view on D's keys[source]
values() an object providing a view on D's values[source]
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
keys() a set-like object providing a view on D's keys[source]
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.

keys() a set-like object providing a view on D's keys[source]
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.
keys() a set-like object providing a view on D's keys[source]
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).

snakeoil.mappings.make_SlottedDict_kls(keys)[source]

Create a space efficient mapping class with a limited set of keys.