##// END OF EJS Templates
added deque with maxlen for py2.5 compat
marcink -
r2729:e9e7c40b beta
parent child Browse files
Show More
@@ -25,7 +25,7 b''
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26
26
27 import os
27 import os
28 from rhodecode import __platform__, PLATFORM_WIN
28 from rhodecode import __platform__, PLATFORM_WIN, __py_version__
29
29
30 #==============================================================================
30 #==============================================================================
31 # json
31 # json
@@ -404,3 +404,129 b' try:'
404 from io import BytesIO
404 from io import BytesIO
405 except ImportError:
405 except ImportError:
406 from cStringIO import StringIO as BytesIO
406 from cStringIO import StringIO as BytesIO
407
408
409 #==============================================================================
410 # deque
411 #==============================================================================
412
413 if __py_version__ >= (2, 6):
414 from collections import deque
415 else:
416 #need to implement our own deque with maxlen
417 class deque(object):
418
419 def __init__(self, iterable=(), maxlen=-1):
420 if not hasattr(self, 'data'):
421 self.left = self.right = 0
422 self.data = {}
423 self.maxlen = maxlen
424 self.extend(iterable)
425
426 def append(self, x):
427 self.data[self.right] = x
428 self.right += 1
429 if self.maxlen != -1 and len(self) > self.maxlen:
430 self.popleft()
431
432 def appendleft(self, x):
433 self.left -= 1
434 self.data[self.left] = x
435 if self.maxlen != -1 and len(self) > self.maxlen:
436 self.pop()
437
438 def pop(self):
439 if self.left == self.right:
440 raise IndexError('cannot pop from empty deque')
441 self.right -= 1
442 elem = self.data[self.right]
443 del self.data[self.right]
444 return elem
445
446 def popleft(self):
447 if self.left == self.right:
448 raise IndexError('cannot pop from empty deque')
449 elem = self.data[self.left]
450 del self.data[self.left]
451 self.left += 1
452 return elem
453
454 def clear(self):
455 self.data.clear()
456 self.left = self.right = 0
457
458 def extend(self, iterable):
459 for elem in iterable:
460 self.append(elem)
461
462 def extendleft(self, iterable):
463 for elem in iterable:
464 self.appendleft(elem)
465
466 def rotate(self, n=1):
467 if self:
468 n %= len(self)
469 for i in xrange(n):
470 self.appendleft(self.pop())
471
472 def __getitem__(self, i):
473 if i < 0:
474 i += len(self)
475 try:
476 return self.data[i + self.left]
477 except KeyError:
478 raise IndexError
479
480 def __setitem__(self, i, value):
481 if i < 0:
482 i += len(self)
483 try:
484 self.data[i + self.left] = value
485 except KeyError:
486 raise IndexError
487
488 def __delitem__(self, i):
489 size = len(self)
490 if not (-size <= i < size):
491 raise IndexError
492 data = self.data
493 if i < 0:
494 i += size
495 for j in xrange(self.left + i, self.right - 1):
496 data[j] = data[j + 1]
497 self.pop()
498
499 def __len__(self):
500 return self.right - self.left
501
502 def __cmp__(self, other):
503 if type(self) != type(other):
504 return cmp(type(self), type(other))
505 return cmp(list(self), list(other))
506
507 def __repr__(self, _track=[]):
508 if id(self) in _track:
509 return '...'
510 _track.append(id(self))
511 r = 'deque(%r, maxlen=%s)' % (list(self), self.maxlen)
512 _track.remove(id(self))
513 return r
514
515 def __getstate__(self):
516 return (tuple(self),)
517
518 def __setstate__(self, s):
519 self.__init__(s[0])
520
521 def __hash__(self):
522 raise TypeError
523
524 def __copy__(self):
525 return self.__class__(self)
526
527 def __deepcopy__(self, memo={}):
528 from copy import deepcopy
529 result = self.__class__()
530 memo[id(self)] = result
531 result.__init__(deepcopy(tuple(self), memo))
532 return result
@@ -25,7 +25,7 b' If not, see <http://www.gnu.org/licenses'
25 import os
25 import os
26 import subprocess
26 import subprocess
27 import threading
27 import threading
28 from collections import deque
28 from rhodecode.lib.compat import deque
29
29
30
30
31 class StreamFeeder(threading.Thread):
31 class StreamFeeder(threading.Thread):
General Comments 0
You need to be logged in to leave comments. Login now