rfc1982_serial_number.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. def show(steps):
  37. print(" => ".join(str(s) for s in steps))
  38. def main():
  39. from argparse import ArgumentParser
  40. ap = ArgumentParser()
  41. ap.add_argument("start", type = Serial)
  42. ap.add_argument("finish", type = Serial)
  43. args = ap.parse_args()
  44. show(step(args.start, args.finish))
  45. if __name__ == "__main__":
  46. main()