12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #!/usr/bin/env python3
- class Serial:
- modulus = 2 ** 32
- def __init__(self, val):
- self._val = int(val)
- if self._val < 0 or self._val >= self.modulus:
- raise ValueError
- def __add__(self, val):
- val = int(val)
- if val < 0 or val > (self.modulus - 1) >> 1:
- raise ValueError
- return type(self)((self._val + val) & (self.modulus - 1))
- def __le__(self, other):
- return (self.modulus - int(self) + int(other)) & (self.modulus >> 1) == 0
- def __ge__(self, other):
- return (self.modulus + int(self) - int(other)) & (self.modulus >> 1) == 0
- def __eq__(self, other):
- return int(self) == int(other)
- def __ne__(self, other):
- return int(self) != int(other)
- def __lt__(self, other):
- return self != other and self <= other
- def __gt__(self, other):
- return self != other and self >= other
- def __int__(self):
- return self._val
- def __str__(self):
- return f"{int(self):{len(str(self.modulus))}d}"
- def find_intermediate(start, finish):
- if start < finish:
- return None
- else:
- wrap = start + ((Serial.modulus >> 1) - 2)
- assert start < wrap and wrap < finish
- return wrap
- def main():
- from argparse import ArgumentParser
- ap = ArgumentParser()
- ap.add_argument("start", type = Serial)
- ap.add_argument("finish", type = Serial)
- args = ap.parse_args()
- wrap = find_intermediate(args.start, args.finish)
- print(f"Start at {args.start!s}")
- if wrap is None:
- print("No step needed")
- else:
- print(f"Step via {wrap!s}")
- print(f"End at {args.finish!s}")
- if __name__ == "__main__":
- main()
|