types.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. from __future__ import absolute_import
  2. from six import string_types
  3. def cmp(a, b):
  4. return (a > b) - (a < b)
  5. class Bit(object):
  6. """
  7. Represents a single Bit.
  8. """
  9. def __init__(self, number, is_set=True):
  10. self.number = number
  11. self.is_set = bool(is_set)
  12. self.mask = 2 ** int(number)
  13. self.children = []
  14. if not self.is_set:
  15. self.mask = ~self.mask
  16. def __repr__(self):
  17. return "<%s: number=%d, is_set=%s>" % (
  18. self.__class__.__name__,
  19. self.number,
  20. self.is_set,
  21. )
  22. # def __str__(self):
  23. # if self.is_set:
  24. # return 'Yes'
  25. # return 'No'
  26. def __int__(self):
  27. return self.mask
  28. def __bool__(self):
  29. return self.is_set
  30. __nonzero__ = __bool__
  31. def __eq__(self, value):
  32. if isinstance(value, Bit):
  33. return value.number == self.number and value.is_set == self.is_set
  34. elif isinstance(value, bool):
  35. return value == self.is_set
  36. elif isinstance(value, int):
  37. return value == self.mask
  38. return value == self.is_set
  39. def __ne__(self, value):
  40. return not self == value
  41. def __coerce__(self, value):
  42. return (self.is_set, bool(value))
  43. def __invert__(self):
  44. return self.__class__(self.number, not self.is_set)
  45. def __and__(self, value):
  46. if isinstance(value, Bit):
  47. value = value.mask
  48. return value & self.mask
  49. def __rand__(self, value):
  50. if isinstance(value, Bit):
  51. value = value.mask
  52. return self.mask & value
  53. def __or__(self, value):
  54. if isinstance(value, Bit):
  55. value = value.mask
  56. return value | self.mask
  57. def __ror__(self, value):
  58. if isinstance(value, Bit):
  59. value = value.mask
  60. return self.mask | value
  61. def __lshift__(self, value):
  62. if isinstance(value, Bit):
  63. value = value.mask
  64. return value << self.mask
  65. def __rlshift__(self, value):
  66. if isinstance(value, Bit):
  67. value = value.mask
  68. return self.mask << value
  69. def __rshift__(self, value):
  70. if isinstance(value, Bit):
  71. value = value.mask
  72. return value >> self.mask
  73. def __rrshift__(self, value):
  74. if isinstance(value, Bit):
  75. value = value.mask
  76. return self.mask >> value
  77. def __xor__(self, value):
  78. if isinstance(value, Bit):
  79. value = value.mask
  80. return value ^ self.mask
  81. def __rxor__(self, value):
  82. if isinstance(value, Bit):
  83. value = value.mask
  84. return self.mask ^ value
  85. def __sentry__(self):
  86. return repr(self)
  87. def evaluate(self, evaluator, qn, connection):
  88. return self.mask, []
  89. def prepare(self, evaluator, query, allow_joins):
  90. return evaluator.prepare_node(self, query, allow_joins)
  91. class BitHandler(object):
  92. """
  93. Represents an array of bits, each as a ``Bit`` object.
  94. """
  95. def __init__(self, value, keys, labels=None):
  96. # TODO: change to bitarray?
  97. if value:
  98. self._value = int(value)
  99. else:
  100. self._value = 0
  101. self._keys = keys
  102. self._labels = labels is not None and labels or keys
  103. def __eq__(self, other):
  104. if not isinstance(other, BitHandler):
  105. return False
  106. return self._value == other._value
  107. def __lt__(self, other):
  108. return int(self._value) < other
  109. def __le__(self, other):
  110. return int(self._value) <= other
  111. def __gt__(self, other):
  112. return int(self._value) > other
  113. def __ge__(self, other):
  114. return int(self._value) >= other
  115. def __cmp__(self, other):
  116. return cmp(self._value, other)
  117. def __repr__(self):
  118. return "<%s: %s>" % (
  119. self.__class__.__name__,
  120. ", ".join(
  121. "%s=%s" % (k, self.get_bit(n).is_set) for n, k in enumerate(self._keys)
  122. ),
  123. )
  124. def __str__(self):
  125. return str(self._value)
  126. def __int__(self):
  127. return self._value
  128. def __bool__(self):
  129. return bool(self._value)
  130. __nonzero__ = __bool__
  131. def __and__(self, value):
  132. return BitHandler(self._value & int(value), self._keys)
  133. def __or__(self, value):
  134. return BitHandler(self._value | int(value), self._keys)
  135. def __add__(self, value):
  136. return BitHandler(self._value + int(value), self._keys)
  137. def __sub__(self, value):
  138. return BitHandler(self._value - int(value), self._keys)
  139. def __lshift__(self, value):
  140. return BitHandler(self._value << int(value), self._keys)
  141. def __rshift__(self, value):
  142. return BitHandler(self._value >> int(value), self._keys)
  143. def __xor__(self, value):
  144. return BitHandler(self._value ^ int(value), self._keys)
  145. def __contains__(self, key):
  146. bit_number = self._keys.index(key)
  147. return bool(self.get_bit(bit_number))
  148. def __getattr__(self, key):
  149. if key.startswith("_"):
  150. return object.__getattribute__(self, key)
  151. if key not in self._keys:
  152. raise AttributeError("%s is not a valid flag" % key)
  153. return self.get_bit(self._keys.index(key))
  154. def __setattr__(self, key, value):
  155. if key.startswith("_"):
  156. return object.__setattr__(self, key, value)
  157. if key not in self._keys:
  158. raise AttributeError("%s is not a valid flag" % key)
  159. self.set_bit(self._keys.index(key), value)
  160. def __iter__(self):
  161. return self.iteritems()
  162. def __sentry__(self):
  163. return repr(self)
  164. def _get_mask(self):
  165. return self._value
  166. mask = property(_get_mask)
  167. def evaluate(self, evaluator, qn, connection):
  168. return self.mask, []
  169. def get_bit(self, bit_number):
  170. mask = 2 ** int(bit_number)
  171. return Bit(bit_number, self._value & mask != 0)
  172. def set_bit(self, bit_number, true_or_false):
  173. mask = 2 ** int(bit_number)
  174. if true_or_false:
  175. self._value |= mask
  176. else:
  177. self._value &= ~mask
  178. return Bit(bit_number, self._value & mask != 0)
  179. def keys(self):
  180. return self._keys
  181. def iterkeys(self):
  182. return iter(self._keys)
  183. def items(self):
  184. return list(self.iteritems())
  185. def iteritems(self):
  186. for k in self._keys:
  187. yield (k, getattr(self, k).is_set)
  188. def get_label(self, flag):
  189. if isinstance(flag, string_types):
  190. flag = self._keys.index(flag)
  191. if isinstance(flag, Bit):
  192. flag = flag.number
  193. return self._labels[flag]