Example #1
0
 def __init__(self,
              store=LocalBinaryStore(path_format=''),
              incoming_val_trans_for_key=imdict(dflt_incoming_val_trans_for_key),
              outgoing_val_trans_for_key=imdict(dflt_outgoing_val_trans_for_key),
              dflt_incoming_val_trans=identity_method,
              func_key=lambda k: os.path.splitext(k)[1]):
     self.store = store
     self.incoming_val_trans_for_key = incoming_val_trans_for_key
     self.outgoing_val_trans_for_key = outgoing_val_trans_for_key
     self.dflt_incoming_val_trans = dflt_incoming_val_trans
     self.func_key = func_key
Example #2
0
def set_obj(k, v, store=LocalBinaryStore(path_format=''),
            outgoing_val_trans_for_key=imdict(dflt_outgoing_val_trans_for_key),
            func_key=lambda k: os.path.splitext(k)[1]):
    """A quick way to get an object, with default... everything (but the key, you know, a clue of what you want)"""

    trans_func = outgoing_val_trans_for_key.get(func_key(k), dflt_outgoing_val_trans_for_key)
    store[k] = trans_func(v)
Example #3
0
def get_obj(k, store=LocalBinaryStore(path_format=''),
            incoming_val_trans_for_key=imdict(dflt_incoming_val_trans_for_key),
            dflt_incoming_val_trans=identity_method,
            func_key=lambda k: os.path.splitext(k)[1]):
    """A quick way to get an object, with default... everything (but the key, you know, a clue of what you want)"""

    trans_func = (incoming_val_trans_for_key or {}).get(func_key(k), dflt_incoming_val_trans)
    return trans_func(store[k])
Example #4
0
class MiscReaderMixin:
    """Mixin to transform incoming vals according to the key their under.
    Warning: If used as a subclass, this mixin should (in general) be placed before the store


    >>> # make a reader that will wrap a dict
    >>> class MiscReader(MiscReaderMixin, dict):
    ...     def __init__(self, d,
    ...                         incoming_val_trans_for_key=None,
    ...                         dflt_incoming_val_trans=None,
    ...                         func_key=None):
    ...         dict.__init__(self, d)
    ...         MiscReaderMixin.__init__(self, incoming_val_trans_for_key, dflt_incoming_val_trans, func_key)
    ...
    >>>
    >>> incoming_val_trans_for_key = dict(
    ...     MiscReaderMixin._incoming_val_trans_for_key,  # take the existing defaults...
    ...     **{'.bin': lambda v: [ord(x) for x in v.decode()], # ... override how to handle the .bin extension
    ...      '.reverse_this': lambda v: v[::-1]  # add a new extension (and how to handle it)
    ...     })
    >>>
    >>> import pickle
    >>> d = {
    ...     'a.bin': b'abc123',
    ...     'a.reverse_this': b'abc123',
    ...     'a.csv': b'event,year\\n Magna Carta,1215\\n Guido,1956',
    ...     'a.txt': b'this is not a text',
    ...     'a.pkl': pickle.dumps(['text', [str, map], {'a list': [1, 2, 3]}]),
    ...     'a.json': '{"str": "field", "int": 42, "float": 3.14, "array": [1, 2], "nested": {"a": 1, "b": 2}}',
    ... }
    >>>
    >>> s = MiscReader(d=d, incoming_val_trans_for_key=incoming_val_trans_for_key)
    >>> list(s)
    ['a.bin', 'a.reverse_this', 'a.csv', 'a.txt', 'a.pkl', 'a.json']
    >>> s['a.bin']
    [97, 98, 99, 49, 50, 51]
    >>> s['a.reverse_this']
    b'321cba'
    >>> s['a.csv']
    [['event', 'year'], [' Magna Carta', '1215'], [' Guido', '1956']]
    >>> s['a.pkl']
    ['text', [<class 'str'>, <class 'map'>], {'a list': [1, 2, 3]}]
    >>> s['a.json']
    {'str': 'field', 'int': 42, 'float': 3.14, 'array': [1, 2], 'nested': {'a': 1, 'b': 2}}
    """

    _func_key = lambda self, k: os.path.splitext(k)[1]
    _dflt_incoming_val_trans = staticmethod(identity_method)

    _incoming_val_trans_for_key = imdict(dflt_incoming_val_trans_for_key)

    def __init__(self, incoming_val_trans_for_key=None,
                 dflt_incoming_val_trans=None,
                 func_key=None):
        if incoming_val_trans_for_key is not None:
            self._incoming_val_trans_for_key = incoming_val_trans_for_key
        if dflt_incoming_val_trans is not None:
            self._dflt_incoming_val_trans = dflt_incoming_val_trans
        if func_key is not None:
            self._func_key = func_key

    def __getitem__(self, k):
        func_key = self._func_key(k)
        trans_func = self._incoming_val_trans_for_key.get(func_key, self._dflt_incoming_val_trans)
        return trans_func(super().__getitem__(k))