mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-14 22:49:23 +01:00
Lots of updates; most importantly, queue() and set() now take iterables as their single argument. Note that this will cause configuration file breakages.
This commit is contained in:
parent
bc56b5bcc9
commit
a3c4b9bc0b
62
src/fix.py
62
src/fix.py
@ -93,9 +93,10 @@ True = int.__new__(bool, 1)
|
|||||||
|
|
||||||
|
|
||||||
class set(object):
|
class set(object):
|
||||||
def __init__(self, *args):
|
__slots__ = ('d',)
|
||||||
|
def __init__(self, seq=()):
|
||||||
self.d = {}
|
self.d = {}
|
||||||
for x in args:
|
for x in seq:
|
||||||
self.d[x] = None
|
self.d[x] = None
|
||||||
|
|
||||||
def __contains__(self, x):
|
def __contains__(self, x):
|
||||||
@ -105,7 +106,8 @@ class set(object):
|
|||||||
return self.d.iterkeys()
|
return self.d.iterkeys()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%s)' % (self.__class__.__name__, repr(self.d.keys())[1:-1])
|
return '%s([%s])' % (self.__class__.__name__,
|
||||||
|
', '.join(map(repr, self.d.iterkeys())))
|
||||||
|
|
||||||
def __nonzero__(self):
|
def __nonzero__(self):
|
||||||
if self.d:
|
if self.d:
|
||||||
@ -138,26 +140,26 @@ class set(object):
|
|||||||
|
|
||||||
class queue(dict):
|
class queue(dict):
|
||||||
__slots__ = ('first', 'last')
|
__slots__ = ('first', 'last')
|
||||||
def __init__(self, *args):
|
def __init__(self, seq=()):
|
||||||
self.first = 0
|
self.first = 0
|
||||||
self.last = 0
|
self.last = 0
|
||||||
for elt in args:
|
for elt in seq:
|
||||||
self.enqueue(elt)
|
self.enqueue(elt)
|
||||||
|
|
||||||
def enqueue(self, elt):
|
def enqueue(self, elt):
|
||||||
self[self.last] = elt
|
dict.__setitem__(self, self.last, elt)
|
||||||
self.last += 1
|
self.last += 1
|
||||||
|
|
||||||
def peek(self):
|
def peek(self):
|
||||||
try:
|
try:
|
||||||
return self[self.first]
|
return dict.__getitem__(self, self.first)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise IndexError, 'peek into empty queue'
|
raise IndexError, 'peek into empty queue'
|
||||||
|
|
||||||
def dequeue(self):
|
def dequeue(self):
|
||||||
try:
|
try:
|
||||||
ret = self[self.first]
|
ret = dict.__getitem__(self, self.first)
|
||||||
del self[self.first]
|
dict.__delitem__(self, self.first)
|
||||||
self.first += 1
|
self.first += 1
|
||||||
return ret
|
return ret
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@ -171,7 +173,7 @@ class queue(dict):
|
|||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for i in xrange(self.first, self.last):
|
for i in xrange(self.first, self.last):
|
||||||
yield self[i]
|
yield dict.__getitem__(self, i)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if len(self) == len(other):
|
if len(self) == len(other):
|
||||||
@ -183,8 +185,26 @@ class queue(dict):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return 'queue(%s)' % ', '.join(map(repr, self.itervalues()))
|
return 'queue([%s])' % ', '.join(map(repr, self.itervalues()))
|
||||||
|
|
||||||
|
def __getitem__(self, i):
|
||||||
|
try:
|
||||||
|
if i >= 0:
|
||||||
|
return dict.__getitem__(self, i+self.first)
|
||||||
|
else:
|
||||||
|
return dict.__getitem__(self, i+self.last)
|
||||||
|
except KeyError:
|
||||||
|
raise IndexError, i
|
||||||
|
|
||||||
|
def __setitem__(self, i, v):
|
||||||
|
try:
|
||||||
|
if i >= 0:
|
||||||
|
dict.__setitem__(self, i+self.first, v)
|
||||||
|
else:
|
||||||
|
dict.__setitem__(self, i+self.last, v)
|
||||||
|
except KeyError:
|
||||||
|
raise IndexError, i
|
||||||
|
|
||||||
|
|
||||||
class IterableMap(object):
|
class IterableMap(object):
|
||||||
"""Define .iteritems() in a class and subclass this to get the other iters.
|
"""Define .iteritems() in a class and subclass this to get the other iters.
|
||||||
@ -201,22 +221,13 @@ class IterableMap(object):
|
|||||||
yield value
|
yield value
|
||||||
|
|
||||||
def items(self):
|
def items(self):
|
||||||
ret = []
|
return list(self.iteritems())
|
||||||
for t in self.iteritems():
|
|
||||||
ret.append(t)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
ret = []
|
return list(self.iterkeys())
|
||||||
for key in self.iterkeys():
|
|
||||||
ret.append(key)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def values(self):
|
def values(self):
|
||||||
ret = []
|
return list(self.itervalues())
|
||||||
for value in self.itervalues():
|
|
||||||
ret.append(value)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
ret = 0
|
ret = 0
|
||||||
@ -224,6 +235,11 @@ class IterableMap(object):
|
|||||||
ret += 1
|
ret += 1
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
def __nonzero__(self):
|
||||||
|
for _ in self.iteritems():
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def mktemp(suffix=''):
|
def mktemp(suffix=''):
|
||||||
import sha
|
import sha
|
||||||
import md5
|
import md5
|
||||||
|
Loading…
Reference in New Issue
Block a user