rfc1982_serial_number.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python3
  2. class Serial:
  3. modulus = 2 ** 32
  4. def __init__(self, val):
  5. self._val = int(val)
  6. if self._val < 0 or self._val >= self.modulus:
  7. raise ValueError
  8. def __add__(self, val):
  9. val = int(val)
  10. if val < 0 or val > (self.modulus - 1) >> 1:
  11. raise ValueError
  12. return type(self)((self._val + val) & (self.modulus - 1))
  13. def __le__(self, other):
  14. return (self.modulus - int(self) + int(other)) & (self.modulus >> 1) == 0
  15. def __ge__(self, other):
  16. return (self.modulus + int(self) - int(other)) & (self.modulus >> 1) == 0
  17. def __eq__(self, other):
  18. return int(self) == int(other)
  19. def __ne__(self, other):
  20. return int(self) != int(other)
  21. def __lt__(self, other):
  22. return self != other and self <= other
  23. def __gt__(self, other):
  24. return self != other and self >= other
  25. def __int__(self):
  26. return self._val
  27. def __str__(self):
  28. return f"{int(self):{len(str(self.modulus))}d}"
  29. def step(start, finish):
  30. if start < finish:
  31. return start, finish
  32. else:
  33. midpoint = start + ((Serial.modulus >> 1) - 2)
  34. assert start < midpoint and midpoint < finish
  35. return start, midpoint, finish
  36. if __name__ == "__main__":
  37. from random import randint
  38. for test in range(10000):
  39. i1 = Serial(randint(0, Serial.modulus - 1))
  40. i2 = Serial(randint(0, Serial.modulus - 1))
  41. assert i1 == i2 or \
  42. (i1 < i2 and not (i1 > i2)) or \
  43. (i1 > i2 and not (i1 < i2)) or \
  44. int(i1) & int(i2) == Serial.modulus >> 1
  45. print(f"{i1} => {i2}: {', '.join(str(s) for s in step(i1, i2))}")