Improve structure ahead of library functionality
authorAlexander Vasarab <alexander@wylark.com>
Sat, 20 Jun 2020 03:46:32 +0000 (20:46 -0700)
committerAlexander Vasarab <alexander@wylark.com>
Sat, 20 Jun 2020 03:46:54 +0000 (20:46 -0700)
munter.py

index fd5e48509b9a79d3f5ca20b30eb843a92a781c32..8b23b25fd74a6b5010cfd9d697f28c266326d18f 100755 (executable)
--- a/munter.py
+++ b/munter.py
@@ -28,7 +28,7 @@ rates = {
 unit_choices = ['metric', 'imperial']
 travel_mode_choices = rates.keys()
 
 unit_choices = ['metric', 'imperial']
 travel_mode_choices = rates.keys()
 
-def munter(distance, elevation, rate, units):
+def time_calc(distance, elevation, rate, units):
     retval = {}
 
     if units not in unit_choices:
     retval = {}
 
     if units not in unit_choices:
@@ -50,28 +50,78 @@ def munter(distance, elevation, rate, units):
 
     return retval
 
 
     return retval
 
-def main():
+def print_ugly_estimate(est):
+    hours = int(est['time'])
+    minutes = int((est['time'] - hours) * 60)
+    print("{human_time}".format(
+            human_time="{hours} hours {minutes} minutes".format(
+                hours=hours,
+                minutes=minutes)))
+
+def print_pretty_estimate(est):
+    hours = int(est['time'])
+    minutes = int((est['time'] - hours) * 60)
+
+    # NOTE: Below, the line with the unicode up arrow uses an alignment
+    #       value of 31. In the future, consider using e.g. wcwidth
+    #       library so that this is more elegant.
+    print("\n\t╒═══════════════════════════════╕")
+    print("\t╎▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒╎╮")
+    print("\t╎▒{:^29}▒╎│".format(''))
+    print("\t╎▒{pace_readable:^31}▒╎│".format(
+            pace_readable="{units} {direction} @ {pace}".format(
+                units=round(est['unit_count']),
+                direction=est['direction'],
+                pace=est['pace'])))
+    print("\t╎▒{human_time:^29}▒╎│".format(
+            human_time="{hours} hours {minutes} minutes".format(
+                hours=hours,
+                minutes=minutes)))
+    print("\t╎▒{:^29}▒╎│".format(''))
+    print("\t╎▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒╎│")
+    print("\t╘═══════════════════════════════╛│")
+    print("\t └───────────────────────────────┘\n")
+
+def get_parser():
     parser = argparse.ArgumentParser(description='Munter Time Calculation')
 
     parser = argparse.ArgumentParser(description='Munter Time Calculation')
 
-    parser.add_argument('--distance', '-d', type=float,
+    parser.add_argument('--distance',
+        '-d',
+        type=float,
         required=True,
         help='Distance (in km, by default)')
 
         required=True,
         help='Distance (in km, by default)')
 
-    parser.add_argument('--elevation', '-e', type=float,
+    parser.add_argument('--elevation',
+        '-e',
+        type=float,
         required=True,
         help='Elevation change (in m, by default)')
 
         required=True,
         help='Elevation change (in m, by default)')
 
-    parser.add_argument('--travel-mode', '-t', type=str,
+    parser.add_argument('--travel-mode',
+        '-t',
+        type=str,
         default='uphill',
         choices=travel_mode_choices, required=False,
         help='Travel mode (flat, by default)')
 
         default='uphill',
         choices=travel_mode_choices, required=False,
         help='Travel mode (flat, by default)')
 
-    parser.add_argument('--units', '-u', type=str,
+    parser.add_argument('--units',
+        '-u',
+        type=str,
         default='imperial',
         required=False,
         choices=unit_choices,
         help='Units of input values')
 
         default='imperial',
         required=False,
         choices=unit_choices,
         help='Units of input values')
 
+    parser.add_argument('--pretty',
+        '-p',
+        action='store_true',
+        default=False,
+        required=False);
+
+    return parser
+
+def main():
+    parser = get_parser()
     opts = parser.parse_args()
 
     distance = opts.distance
     opts = parser.parse_args()
 
     distance = opts.distance
@@ -79,30 +129,12 @@ def main():
     units = opts.units
     travel_mode = opts.travel_mode
 
     units = opts.units
     travel_mode = opts.travel_mode
 
-    time_calc = munter(distance, elevation, travel_mode, units=units)
-
-    hours = int(time_calc['time'])
-    minutes = int((time_calc['time'] - hours) * 60)
+    time_estimate = time_calc(distance, elevation, travel_mode, units=units)
 
 
-    # NOTE: Below, the line with the unicode up arrow uses an alignment
-    #       value of 31. In the future, consider using e.g. wcwidth
-    #       library so that this is more elegant.
-    print("\n\t╒═══════════════════════════════╕")
-    print("\t╎▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒╎╮")
-    print("\t╎▒{:^29}▒╎│".format(''))
-    print("\t╎▒{pace_readable:^31}▒╎│".format(
-            pace_readable="{units} {direction} @ {pace}".format(
-                units=round(time_calc['unit_count']),
-                direction=time_calc['direction'],
-                pace=time_calc['pace'])))
-    print("\t╎▒{human_time:^29}▒╎│".format(
-            human_time="{hours} hours {minutes} minutes".format(
-                hours=hours,
-                minutes=minutes)))
-    print("\t╎▒{:^29}▒╎│".format(''))
-    print("\t╎▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒╎│")
-    print("\t╘═══════════════════════════════╛│")
-    print("\t └───────────────────────────────┘\n")
+    if opts.pretty:
+        print_pretty_estimate(time_estimate)
+    else:
+        print_ugly_estimate(time_estimate)
 
 if __name__ == "__main__":
     main()
 
 if __name__ == "__main__":
     main()