/usr/lib/python3/dist-packages/zict/sieve.py is in python3-zict 0.1.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | from __future__ import absolute_import, division, print_function
from collections import defaultdict
from itertools import chain
import sys
from .common import ZictBase, close
class Sieve(ZictBase):
""" Store values in different mappings based on a selector's
output.
This creates a MutableMapping combining several underlying
MutableMappings for storage. Items are dispatched based on
a selector function provided by the user.
Parameters
----------
mappings: dict of {mapping key: MutableMapping}
selector: callable (key, value) -> mapping key
Examples
--------
>>> small = {}
>>> large = DataBase() # doctest: +SKIP
>>> mappings = {True: small, False: large} # doctest: +SKIP
>>> def is_small(key, value): # doctest: +SKIP
return sys.getsizeof(value) < 10000
>>> d = Sieve(mappings, is_small) # doctest: +SKIP
See Also
--------
Buffer
"""
def __init__(self, mappings, selector):
self.mappings = mappings
self.selector = selector
self.key_to_mapping = {}
def __getitem__(self, key):
return self.key_to_mapping[key][key]
def __setitem__(self, key, value):
old_mapping = self.key_to_mapping.get(key)
mapping = self.mappings[self.selector(key, value)]
if old_mapping is not None and old_mapping is not mapping:
del old_mapping[key]
mapping[key] = value
self.key_to_mapping[key] = mapping
def __delitem__(self, key):
del self.key_to_mapping.pop(key)[key]
def _do_update(self, items):
# Optimized update() implementation issuing a single update()
# call per underlying mapping.
to_delete = []
updates = defaultdict(list)
mapping_ids = dict((id(m), m) for m in self.mappings.values())
for key, value in items:
old_mapping = self.key_to_mapping.get(key)
mapping = self.mappings[self.selector(key, value)]
if old_mapping is not None and old_mapping is not mapping:
del old_mapping[key]
# Can't hash a mutable mapping, so use its id() instead
updates[id(mapping)].append((key, value))
for mid, mitems in updates.items():
mapping = mapping_ids[mid]
mapping.update(mitems)
for key, _ in mitems:
self.key_to_mapping[key] = mapping
def keys(self):
return chain.from_iterable(self.mappings.values())
def values(self):
return chain.from_iterable(m.values() for m in self.mappings.values())
def items(self):
return chain.from_iterable(m.items() for m in self.mappings.values())
def __len__(self):
return sum(map(len, self.mappings.values()))
__iter__ = keys
def __contains__(self, key):
return key in self.key_to_mapping
def __str__(self):
return 'Sieve<%s>' % (str(self.mappings),)
__repr__ = __str__
def flush(self):
for m in self.mappings.values():
m.flush()
def close(self):
for m in self.mappings.values():
close(m)
|