bricks.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2011 Yesudeep Mangalapilly <yesudeep@gmail.com>
  5. # Copyright 2012 Google, Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. """
  19. Utility collections or "bricks".
  20. :module: watchdog.utils.bricks
  21. :author: yesudeep@google.com (Yesudeep Mangalapilly)
  22. :author: lalinsky@gmail.com (Lukáš Lalinský)
  23. :author: python@rcn.com (Raymond Hettinger)
  24. Classes
  25. =======
  26. .. autoclass:: OrderedSetQueue
  27. :members:
  28. :show-inheritance:
  29. :inherited-members:
  30. .. autoclass:: OrderedSet
  31. """
  32. from .compat import queue
  33. class SkipRepeatsQueue(queue.Queue):
  34. """Thread-safe implementation of an special queue where a
  35. put of the last-item put'd will be dropped.
  36. The implementation leverages locking already implemented in the base class
  37. redefining only the primitives.
  38. Queued items must be immutable and hashable so that they can be used
  39. as dictionary keys. You must implement **only read-only properties** and
  40. the :meth:`Item.__hash__()`, :meth:`Item.__eq__()`, and
  41. :meth:`Item.__ne__()` methods for items to be hashable.
  42. An example implementation follows::
  43. class Item(object):
  44. def __init__(self, a, b):
  45. self._a = a
  46. self._b = b
  47. @property
  48. def a(self):
  49. return self._a
  50. @property
  51. def b(self):
  52. return self._b
  53. def _key(self):
  54. return (self._a, self._b)
  55. def __eq__(self, item):
  56. return self._key() == item._key()
  57. def __ne__(self, item):
  58. return self._key() != item._key()
  59. def __hash__(self):
  60. return hash(self._key())
  61. based on the OrderedSetQueue below
  62. """
  63. def _init(self, maxsize):
  64. queue.Queue._init(self, maxsize)
  65. self._last_item = None
  66. def _put(self, item):
  67. if item != self._last_item:
  68. queue.Queue._put(self, item)
  69. self._last_item = item
  70. else:
  71. # `put` increments `unfinished_tasks` even if we did not put
  72. # anything into the queue here
  73. self.unfinished_tasks -= 1
  74. def _get(self):
  75. item = queue.Queue._get(self)
  76. if item is self._last_item:
  77. self._last_item = None
  78. return item