Created
October 5, 2017 23:02
-
-
Save novel-yet-trivial/e72abf167a36d3f746fdecce35ed0959 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# unit.py | |
# | |
# Copyright September 2015 | |
# | |
# Documentation is like sex. | |
# When it's good, it's very good. | |
# When it's bad, it's better than nothing. | |
# When it lies to you, it may be a while before you realize something's wrong. | |
# | |
# | |
''' | |
run without arguments to enter a loop. | |
first loop must have 3 arguments: number, from unit, to unit | |
further loops can have 1, 2, or 3 arguments: | |
1: a new number to convert with previous from and to units | |
2: a new number and new from unit, with previous to unit | |
3: all new arguments | |
enter without arguments to exit the loop. | |
run with 2 arguments to set the from and to units | |
run with 3 arguments for a single conversion, then exit | |
''' | |
import subprocess | |
import sys | |
if sys.version_info < (3,): | |
input = raw_input | |
def get_input(prompt): | |
try: | |
return input(prompt) | |
except EOFError: #catch ctrl-D (Alt-F4) to quit | |
print() | |
quit() | |
def convert(number, from_unit, to_unit): | |
subprocess.call([ | |
'units', '-v', | |
"%s %s"%(number, from_unit), | |
to_unit]) | |
def looper(from_unit = None, to_unit = None): | |
if from_unit is None and to_unit is None: | |
while True: | |
user_in = get_input("(? to ?) > ") | |
user_in = user_in.split() | |
if len(user_in) not in (0,2,3): | |
print("Error: first run must have 2 or 3 arguments") | |
print("Enter a value, a from unit, and a to unit") | |
print('e.g. "9 m ft" converts 9 meters to feet') | |
print('(which is the world record for a longjump)') | |
print("Or enter an initializing from unit and to unit") | |
print("Or enter nothing to quit") | |
continue | |
elif len(user_in) ==0: | |
quit() | |
elif len(user_in) == 2: | |
from_unit, to_unit = user_in | |
elif len(user_in) == 3: | |
number, from_unit, to_unit = user_in | |
convert(number, from_unit, to_unit) | |
break | |
while True: | |
user_in = get_input("(%s to %s) > "%(from_unit, to_unit)) | |
user_in = user_in.split() | |
if len(user_in) == 3: | |
number, from_unit, to_unit = user_in | |
elif len(user_in) == 2: | |
number, from_unit = user_in | |
elif len(user_in) == 1: | |
number = user_in[0] | |
else: | |
quit() | |
convert(number, from_unit, to_unit) | |
def main(): | |
if len(sys.argv) in (1,3): | |
looper(*sys.argv[1:]) | |
elif len(sys.argv) == 4: | |
convert(*sys.argv[1:]) | |
else: | |
print("Error: 0, 2 or 3 arguments expected; got %d"%(len(sys.argv)-1)) | |
print(__doc__) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment