Sfoglia il codice sorgente

Refactor and add text for humans

Rob Austein 7 mesi fa
parent
commit
6bf095983b
2 ha cambiato i file con 22 aggiunte e 11 eliminazioni
  1. 15 9
      rfc1982_serial_number.py
  2. 7 2
      test.py

+ 15 - 9
rfc1982_serial_number.py

@@ -39,16 +39,13 @@ class Serial:
     def __str__(self):
         return f"{int(self):{len(str(self.modulus))}d}"
 
-def step(start, finish):
+def find_intermediate(start, finish):
     if start < finish:
-        return start, finish
+        return None
     else:
-        midpoint = start + ((Serial.modulus >> 1) - 2)
-        assert start < midpoint and midpoint < finish
-        return start, midpoint, finish
-
-def show(steps):
-    print(" => ".join(str(s) for s in steps))
+        wrap = start + ((Serial.modulus >> 1) - 2)
+        assert start < wrap and wrap < finish
+        return wrap
 
 def main():
     from argparse import ArgumentParser
@@ -58,7 +55,16 @@ def main():
     ap.add_argument("finish", type = Serial)
     args = ap.parse_args()
 
-    show(step(args.start, args.finish))
+    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()

+ 7 - 2
test.py

@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 
-from rfc1982_serial_number import Serial, step, show
+from rfc1982_serial_number import Serial, find_intermediate
 from random import randint
 
 for test in range(10000):
@@ -12,4 +12,9 @@ for test in range(10000):
         (i1 > i2 and not (i1 < i2)) or \
         int(i1) & int(i2) == Serial.modulus >> 1
     
-    show(step(i1, i2))
+    step = [i1, i2]
+    wrap = find_intermediate(i1, i2)
+    if wrap is not None:
+        step.insert(1, wrap)
+
+    print(" => ".join(str(s) for s in step))