datastructures.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """
  2. Custom Datastructures
  3. """
  4. from UserList import UserList
  5. import traceback
  6. class PositionQueue(UserList):
  7. """A positional queue of a specific length, with slots that are either
  8. filled or unfilled. When all of the positions are filled, the queue
  9. is considered :meth:`full`.
  10. :param length: see :attr:`length`.
  11. .. attribute:: length
  12. The number of items required for the queue to be considered full.
  13. """
  14. class UnfilledPosition(object):
  15. """Describes an unfilled slot."""
  16. def __init__(self, position):
  17. self.position = position
  18. def __init__(self, length):
  19. self.length = length
  20. self.data = map(self.UnfilledPosition, xrange(length))
  21. def full(self):
  22. """Returns ``True`` if all of the slots has been filled."""
  23. return len(self) >= self.length
  24. def __len__(self):
  25. """``len(self)`` -> number of slots filled with real values."""
  26. return len(self.filled)
  27. @property
  28. def filled(self):
  29. """Returns the filled slots as a list."""
  30. return filter(lambda v: not isinstance(v, self.UnfilledPosition),
  31. self.data)
  32. class ExceptionInfo(object):
  33. def __init__(self, exc_info):
  34. type_, exception, tb = exc_info
  35. self.exception = exception
  36. self.traceback = '\n'.join(traceback.format_exception(*exc_info))
  37. def __str__(self):
  38. return str(self.exception)