Lists.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # QList:
  2. # basically a python 2.3 compatible interface if you want deque
  3. #
  4. # SizedList:
  5. # handy class for keeping a fixed-length history
  6. # uses deque if available
  7. #
  8. # The contents of this file are subject to the Python Software Foundation
  9. # License Version 2.3 (the License). You may not copy or use this file, in
  10. # either source code or executable form, except in compliance with the License.
  11. # You may obtain a copy of the License at http://www.python.org/license.
  12. #
  13. # Software distributed under the License is distributed on an AS IS basis,
  14. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. # for the specific language governing rights and limitations under the
  16. # License.
  17. #
  18. # by Greg Hazel
  19. try:
  20. from collections import deque
  21. base_list_class = deque
  22. popleft = deque.popleft
  23. clear = deque.clear
  24. appendleft = deque.appendleft
  25. def insert(q, n, item):
  26. if n == len(q):
  27. deque.append(q, item)
  28. else:
  29. q.rotate(-(n + 1))
  30. q.appendleft(item)
  31. q.rotate(n + 1)
  32. def pop(q, n):
  33. q.rotate(-n)
  34. q.popleft()
  35. q.rotate(n)
  36. def remove(q, item):
  37. for i, v in enumerate(q):
  38. if v == item:
  39. q.pop(i)
  40. break
  41. else:
  42. raise ValueError(q.__class__.__name__ + ".remove(x): x not in list")
  43. except ImportError:
  44. from UserList import UserList
  45. base_list_class = UserList
  46. def popleft(l):
  47. return l.pop(0)
  48. def clear(l):
  49. l[:] = []
  50. def appendleft(l, item):
  51. l.insert(0, item)
  52. insert = UserList.insert
  53. pop = UserList.pop
  54. remove = UserList.remove
  55. class QList(base_list_class):
  56. clear = clear
  57. pop = pop
  58. popleft = popleft
  59. remove = remove
  60. appendleft = appendleft
  61. insert = insert
  62. def __init__(self, *a, **kw):
  63. base_list_class.__init__(self, *a, **kw)
  64. # dequeu doesn't have __add__ ?
  65. # overload anyway to get a base_list_class
  66. def __add__(self, l):
  67. n = base_list_class(self)
  68. n.extend(l)
  69. return n
  70. # I use QList becuase deque.popleft is faster than list.pop(0)
  71. class SizedList(QList):
  72. def __init__(self, max_items):
  73. self.max_items = max_items
  74. QList.__init__(self)
  75. def append(self, v):
  76. QList.append(self, v)
  77. if len(self) > self.max_items:
  78. self.popleft()
  79. def collapse(seq):
  80. start = None
  81. current = None
  82. for i in seq:
  83. if start is not None and i > (current + 1):
  84. yield start, current + 1
  85. start = i
  86. elif start is None:
  87. start = i
  88. current = i
  89. if start is not None:
  90. yield start, current + 1
  91. if __name__ == '__main__':
  92. l = SizedList(10)
  93. for i in xrange(50):
  94. l.append(i)
  95. assert list(l) == range(40, 50)
  96. l.appendleft(39)
  97. assert list(l) == range(39, 50)