py2store.utils.explicit

utils to make stores based on a the input data itself

class py2store.utils.explicit.ExplicitKeymapReader(store, key_of_id=None, id_of_key=None)[source]

Wrap a store (instance) so that it gets it’s keys from an explicit iterable of keys.

>>> s = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> id_of_key = {'A': 'a', 'C': 'c'}
>>> ss = ExplicitKeymapReader(s, id_of_key=id_of_key)
>>> list(ss)
['A', 'C']
>>> ss['C']  # will look up 'C', find 'c', and call the store on that.
3
class py2store.utils.explicit.ExplicitKeys(key_collection: Collection)[source]

py2store.base.Keys implementation that gets it’s keys explicitly from a collection given at initialization time. The key_collection must be a collections.abc.Collection (such as list, tuple, set, etc.)

>>> keys = ExplicitKeys(key_collection=['foo', 'bar', 'alice'])
>>> 'foo' in keys
True
>>> 'not there' in keys
False
>>> list(keys)
['foo', 'bar', 'alice']
class py2store.utils.explicit.ExplicitKeysSource(key_collection: Collection, _obj_of_key: Callable)[source]

An object source that uses an explicit keys collection and a specified function to read contents for a key.

class py2store.utils.explicit.ExplicitKeysStore(store, key_collection)[source]

Wrap a store (instance) so that it gets it’s keys from an explicit iterable of keys.

>>> s = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> list(s)
['a', 'b', 'c', 'd']
>>> ss = ExplicitKeysStore(s, ['d', 'a'])
>>> len(ss)
2
>>> list(ss)
['d', 'a']
>>> list(ss.values())
[4, 1]
>>> ss.head()
('d', 4)
class py2store.utils.explicit.ExplicitKeysWithPrefixRelativization(key_collection, _prefix=None)[source]

py2store.base.Keys implementation that gets it’s keys explicitly from a collection given at initialization time. The key_collection must be a collections.abc.Collection (such as list, tuple, set, etc.)

>>> from py2store.base import Store
>>> s = ExplicitKeysWithPrefixRelativization(key_collection=['/root/of/foo', '/root/of/bar', '/root/for/alice'])
>>> keys = Store(store=s)
>>> 'of/foo' in keys
True
>>> 'not there' in keys
False
>>> list(keys)
['of/foo', 'of/bar', 'for/alice']
class py2store.utils.explicit.ObjReader(_obj_of_key: Callable)[source]

A reader that uses a specified function to get the contents for a given key.

>>> # define a contents_of_key that reads stuff from a dict
>>> data = {'foo': 'bar', 42: "everything"}
>>> def read_dict(k):
...     return data[k]
>>> pr = ObjReader(_obj_of_key=read_dict)
>>> pr['foo']
'bar'
>>> pr[42]
'everything'
>>>
>>> # define contents_of_key that reads stuff from a file given it's path
>>> def read_file(path):
...     with open(path) as fp:
...         return fp.read()
>>> pr = ObjReader(_obj_of_key=read_file)
>>> file_where_this_code_is = __file__  # it should be THIS file you're reading right now!
>>> print(pr[file_where_this_code_is][62:155])  # print some characters of this file
from collections.abc import Mapping
from typing import Callable, Collection as CollectionType
py2store.utils.explicit.invertible_maps(mapping=None, inv_mapping=None)[source]

Returns two maps that are inverse of each other. Raises an AssertionError iif both maps are None, or if the maps are not inverse of each other

Get a pair of invertible maps >>> invertible_maps({1: 11, 2: 22}) ({1: 11, 2: 22}, {11: 1, 22: 2}) >>> invertible_maps(None, {11: 1, 22: 2}) ({1: 11, 2: 22}, {11: 1, 22: 2})

If two maps are given and invertible, you just get them back >>> invertible_maps({1: 11, 2: 22}, {11: 1, 22: 2}) ({1: 11, 2: 22}, {11: 1, 22: 2})

Or if they’re not invertible >>> invertible_maps({1: 11, 2: 22}, {11: 1, 22: ‘ha, not what you expected!’}) Traceback (most recent call last):

AssertionError: mapping and inv_mapping are not inverse of each other!

>>> invertible_maps(None, None)
Traceback (most recent call last):
  ...
ValueError: You need to specify one or both maps