Skip to content

Commit b9e91cc

Browse files
authored
Merge pull request zooko#3 from danc86/py3
finish porting to Python 3
2 parents a2bc71a + 8e5ce90 commit b9e91cc

37 files changed

Lines changed: 631 additions & 556 deletions

pyutil/PickleSaver.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
# from the Python Standard Library
1212
import os
13-
import cPickle as pickle
13+
try:
14+
import cPickle as pickle # PY2
15+
except ImportError:
16+
import pickle # PY3
1417
import warnings
1518

1619
# from the pyutil library

pyutil/cache.py

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,43 +42,50 @@ def __init__(self, c):
4242
self.i = c.d[c.hs][2]
4343
def __iter__(self):
4444
return self
45-
def next(self):
45+
def __next__(self):
4646
if self.i is self.c.ts:
4747
raise StopIteration()
4848
k = self.i
49-
precondition(self.c.d.has_key(k), "The iterated LRUCache doesn't have the next key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", k, self.c)
49+
precondition(k in self.c.d, "The iterated LRUCache doesn't have the next key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", k, self.c)
5050
(v, p, n,) = self.c.d[k]
5151
self.i = n
5252
return (k, v,)
53+
def next(self):
54+
return self.__next__()
5355

5456
class KeyIterator:
5557
def __init__(self, c):
5658
self.c = c
5759
self.i = c.d[c.hs][2]
5860
def __iter__(self):
5961
return self
60-
def next(self):
62+
def __next__(self):
6163
if self.i is self.c.ts:
6264
raise StopIteration()
6365
k = self.i
64-
precondition(self.c.d.has_key(k), "The iterated LRUCache doesn't have the next key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", k, self.c)
66+
precondition(k in self.c.d, "The iterated LRUCache doesn't have the next key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", k, self.c)
6567
(v, p, n,) = self.c.d[k]
6668
self.i = n
6769
return k
70+
def next(self):
71+
return self.__next__()
6872

6973
class ValIterator:
7074
def __init__(self, c):
7175
self.c = c
7276
self.i = c.d[c.hs][2]
7377
def __iter__(self):
7478
return self
75-
def next(self):
79+
def __next__(self):
7680
if self.i is self.c.ts:
7781
raise StopIteration()
78-
precondition(self.c.d.has_key(self.i), "The iterated LRUCache doesn't have the next key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c)
82+
precondition(self.i in self.c.d, "The iterated LRUCache doesn't have the next key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c)
7983
(v, p, n,) = self.c.d[self.i]
8084
self.i = n
8185
return v
86+
def next(self):
87+
return self.__next__()
88+
8289

8390
class Sentinel:
8491
def __init__(self, msg):
@@ -124,7 +131,7 @@ def _assert_invariants(self):
124131
_assert((len(self.d) > 2) == (self.d[self.hs][2] is not self.ts) == (self.d[self.ts][1] is not self.hs), "Head and tail point to something other than each other if and only if there is at least one element in the dictionary.", self.hs, self.ts, len(self.d))
125132
foundprevsentinel = 0
126133
foundnextsentinel = 0
127-
for (k, (v, p, n,)) in self.d.iteritems():
134+
for (k, (v, p, n,)) in self.d.items():
128135
_assert(v not in (self.hs, self.ts,))
129136
_assert(p is not self.ts, "A reference to the tail sentinel may not appear in prev.", k, v, p, n)
130137
_assert(n is not self.hs, "A reference to the head sentinel may not appear in next.", k, v, p, n)
@@ -150,7 +157,7 @@ def _assert_invariants(self):
150157
def freshen(self, k, strictkey=False):
151158
assert self._assert_invariants()
152159

153-
if not self.d.has_key(k):
160+
if k not in self.d:
154161
if strictkey:
155162
raise KeyError(k)
156163
return
@@ -227,7 +234,7 @@ def __delitem__(self, key, default=None, strictkey=True):
227234
that key and strictkey is False
228235
"""
229236
assert self._assert_invariants()
230-
if self.d.has_key(key):
237+
if key in self.d:
231238
node = self.d[key]
232239
# relink
233240
self.d[node[1]][2] = node[2]
@@ -244,7 +251,7 @@ def __delitem__(self, key, default=None, strictkey=True):
244251

245252
def has_key(self, key):
246253
assert self._assert_invariants()
247-
if self.d.has_key(key):
254+
if key in self.d:
248255
self.freshen(key)
249256
assert self._assert_invariants()
250257
return True
@@ -270,10 +277,10 @@ def update(self, otherdict):
270277
self.clear()
271278
assert self._assert_invariants()
272279

273-
i = otherdict.iteritems()
280+
i = iter(otherdict.items())
274281
try:
275282
while len(self.d) < self.m:
276-
(k, v,) = i.next()
283+
(k, v,) = next(i)
277284
assert self._assert_invariants()
278285
self[k] = v
279286
assert self._assert_invariants()
@@ -282,7 +289,7 @@ def update(self, otherdict):
282289
_assert(False, "Internal error -- this should never have happened since the while loop should have terminated first.")
283290
return self
284291

285-
for (k, v,) in otherdict.iteritems():
292+
for (k, v,) in otherdict.items():
286293
assert self._assert_invariants()
287294
self[k] = v
288295
assert self._assert_invariants()
@@ -401,10 +408,10 @@ def __iter__(self):
401408
return self
402409
def next(self):
403410
precondition(self.i <= len(self.c._lru), "The iterated SmallLRUCache doesn't have this many elements. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c)
404-
precondition(dict.has_key(self.c, self.c._lru[self.i]), "The iterated SmallLRUCache doesn't have this key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c._lru[self.i], self.c)
405411
if self.i == len(self.c._lru):
406412
raise StopIteration()
407-
k = self.i
413+
precondition(dict.__contains__(self.c, self.c._lru[self.i]), "The iterated SmallLRUCache doesn't have this key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c._lru[self.i], self.c)
414+
k = self.c._lru[self.i]
408415
self.i += 1
409416
return (k, dict.__getitem__(self.c, k),)
410417

@@ -416,10 +423,10 @@ def __iter__(self):
416423
return self
417424
def next(self):
418425
precondition(self.i <= len(self.c._lru), "The iterated SmallLRUCache doesn't have this many elements. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c)
419-
precondition(dict.has_key(self.c, self.c._lru[self.i]), "The iterated SmallLRUCache doesn't have this key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c._lru[self.i], self.c)
420426
if self.i == len(self.c._lru):
421427
raise StopIteration()
422-
k = self.i
428+
precondition(dict.__contains__(self.c, self.c._lru[self.i]), "The iterated SmallLRUCache doesn't have this key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c._lru[self.i], self.c)
429+
k = self.c._lru[self.i]
423430
self.i += 1
424431
return k
425432

@@ -431,27 +438,28 @@ def __iter__(self):
431438
return self
432439
def next(self):
433440
precondition(self.i <= len(self.c._lru), "The iterated SmallLRUCache doesn't have this many elements. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c)
434-
precondition(dict.has_key(self.c, self.c._lru[self.i]), "The iterated SmallLRUCache doesn't have this key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c._lru[self.i], self.c)
435441
if self.i == len(self.c._lru):
436442
raise StopIteration()
437-
k = self.i
443+
precondition(dict.__contains__(self.c, self.c._lru[self.i]), "The iterated SmallLRUCache doesn't have this key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c._lru[self.i], self.c)
444+
k = self.c._lru[self.i]
438445
self.i += 1
439446
return dict.__getitem__(self.c, k)
440447

441448
def __init__(self, initialdata={}, maxsize=128):
442449
dict.__init__(self, initialdata)
443-
self._lru = initialdata.keys() # contains keys
450+
self._lru = list(initialdata.keys()) # contains keys
444451
self._maxsize = maxsize
445452
over = len(self) - self._maxsize
446453
if over > 0:
447-
map(dict.__delitem__, [self]*over, self._lru[:over])
454+
for k in self._lru[:over]:
455+
dict.__delitem__(self, k)
448456
del self._lru[:over]
449457
assert self._assert_invariants()
450458

451459
def _assert_invariants(self):
452460
_assert(len(self._lru) <= self._maxsize, "Size is required to be <= maxsize.")
453-
_assert(len(filter(lambda x: dict.has_key(self, x), self._lru)) == len(self._lru), "Each key in self._lru is required to be in dict.", filter(lambda x: not dict.has_key(self, x), self._lru), len(self._lru), self._lru, len(self), self)
454-
_assert(len(filter(lambda x: x in self._lru, self.keys())) == len(self), "Each key in dict is required to be in self._lru.", filter(lambda x: x not in self._lru, self.keys()), len(self._lru), self._lru, len(self), self)
461+
_assert(len([x for x in self._lru if dict.__contains__(self, x)]) == len(self._lru), "Each key in self._lru is required to be in dict.", filter(lambda x: not dict.__contains__(self, x), self._lru), len(self._lru), self._lru, len(self), self)
462+
_assert(len([x for x in self.keys() if x in self._lru]) == len(self), "Each key in dict is required to be in self._lru.", [x for x in self.keys() if x not in self._lru], len(self._lru), self._lru, len(self), self)
455463
_assert(len(self._lru) == len(self), "internal consistency", filter(lambda x: x not in self.keys(), self._lru), len(self._lru), self._lru, len(self), self)
456464
_assert(len(self._lru) <= self._maxsize, "internal consistency", len(self._lru), self._lru, self._maxsize)
457465
return True
@@ -471,7 +479,7 @@ def setdefault(self, key, default=None):
471479

472480
def __setitem__(self, key, item=None):
473481
assert self._assert_invariants()
474-
if dict.has_key(self, key):
482+
if dict.__contains__(self, key):
475483
self._lru.remove(key)
476484
else:
477485
if len(self._lru) == self._maxsize:
@@ -501,7 +509,7 @@ def __delitem__(self, key, default=None, strictkey=True):
501509
that key and strictkey is False
502510
"""
503511
assert self._assert_invariants()
504-
if dict.has_key(self, key):
512+
if dict.__contains__(self, key):
505513
val = dict.__getitem__(self, key)
506514
dict.__delitem__(self, key)
507515
self._lru.remove(key)
@@ -535,16 +543,16 @@ def update(self, otherdict):
535543
while len(self) > self._maxsize:
536544
dict.popitem(self)
537545
else:
538-
for k, v, in otherdict.iteritems():
546+
for k, v, in otherdict.items():
539547
if len(self) == self._maxsize:
540548
break
541549
dict.__setitem__(self, k, v)
542550
self._lru = dict.keys(self)
543551
assert self._assert_invariants()
544552
return self
545553

546-
for k in otherdict.iterkeys():
547-
if dict.has_key(self, k):
554+
for k in otherdict:
555+
if dict.__contains__(self, k):
548556
self._lru.remove(k)
549557
self._lru.extend(otherdict.keys())
550558
dict.update(self, otherdict)
@@ -559,7 +567,7 @@ def update(self, otherdict):
559567

560568
def has_key(self, key):
561569
assert self._assert_invariants()
562-
if dict.has_key(self, key):
570+
if dict.__contains__(self, key):
563571
assert key in self._lru, "key: %s, self._lru: %s" % tuple(map(hr, (key, self._lru,)))
564572
self._lru.remove(key)
565573
self._lru.append(key)
@@ -574,7 +582,7 @@ def refresh(self, key, strictkey=True):
574582
@param strictkey: raise a KeyError exception if key isn't present
575583
"""
576584
assert self._assert_invariants()
577-
if not dict.has_key(self, key):
585+
if not dict.__contains__(self, key):
578586
if strictkey:
579587
raise KeyError(key)
580588
return
@@ -588,6 +596,15 @@ def popitem(self):
588596
obj = self.remove(k)
589597
return (k, obj,)
590598

599+
def iteritems(self):
600+
return SmallLRUCache.ItemIterator(self)
601+
602+
def iterkeys(self):
603+
return SmallLRUCache.KeyIterator(self)
604+
605+
def itervalues(self):
606+
return SmallLRUCache.ValueIterator(self)
607+
591608
class LinkedListLRUCache:
592609
"""
593610
This is slower and less featureful than LRUCache. It is included
@@ -608,14 +625,14 @@ def __init__(self, initialdata={}, maxsize=128):
608625
self.d = {}
609626
self.first = None
610627
self.last = None
611-
for key, value in initialdata.iteritems():
628+
for key, value in initialdata.items():
612629
self[key] = value
613630
def clear(self):
614631
self.d = {}
615632
self.first = None
616633
self.last = None
617634
def update(self, otherdict):
618-
for (k, v,) in otherdict.iteritems():
635+
for (k, v,) in otherdict.items():
619636
self[k] = v
620637
def setdefault(self, key, default=None):
621638
if not self.has_key(key):

0 commit comments

Comments
 (0)