4 from operator
import itemgetter
6 upck8 =
lambda x: struct.unpack(
'B', x)[0]
7 upck16 =
lambda x: struct.unpack(
'H', x)[0]
8 upck32 =
lambda x: struct.unpack(
'I', x)[0]
9 upck64 =
lambda x: struct.unpack(
'Q', x)[0]
10 pck8 =
lambda x: struct.pack(
'B', x)
11 pck16 =
lambda x: struct.pack(
'H', x)
12 pck32 =
lambda x: struct.pack(
'I', x)
13 pck64 =
lambda x: struct.pack(
'Q', x)
28 [(len(repr(chr(x))) == 3)
and chr(x)
or '.' for x
in range(256)])
30 for c
in xrange(0, len(src), length):
31 chars = src[c:c + length]
32 hexa =
' '.join([
"%02x" % ord(x)
for x
in chars])
34 [
"%s" % ((ord(x) <= 127
and FILTER[ord(x)])
or '.')
for x
in chars])
35 lines.append(
"%04x %-*s %s\n" % (c, length * 3, hexa, printable))
46 if self.default_factory
is None:
48 value = self[key] = self.default_factory(key)
52 return inspect.stack()[2][3]
56 """Limited in size dictionnary.
58 To reduce combinatory cost, once an upper limit @max_size is reached,
59 @max_size - @min_size elements are suppressed.
60 The targeted elements are the less accessed.
62 One can define a callback called when an element is removed
65 def __init__(self, max_size, min_size=None, initialdata=None,
67 """Create a BoundedDict
68 @max_size: maximum size of the dictionnary
69 @min_size: (optional) number of most used element to keep when resizing
70 @initialdata: (optional) dict instance with initial data
71 @delete_cb: (optional) callback called when an element is removed
73 self.
_data = initialdata.copy()
if initialdata
else {}
74 self.
_min_size = min_size
if min_size
else max_size / 3
82 if asked_key
not in self.
_data:
88 most_common = sorted(self._counter.iteritems(),
89 key=itemgetter(1), reverse=
True)
93 for key, _
in most_common[self.
_min_size - 1:]:
98 for key, _
in most_common[:self.
_min_size - 1]}
109 self.
_data[asked_key] = value
113 return key
in self.
_data
116 return key
in self.
_data
119 "Return the list of dict's keys"
120 return self._data.keys()
124 "Return the current instance as a dictionnary"
129 data = self.
_data[key]
142 """Ensure the callback is called when last reference is lost"""
144 for key
in self.
_data: