-
-
Save Toshakins/633e0f50df98f937aa8b to your computer and use it in GitHub Desktop.
pylolo
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
{"decimalsDict": {"1": null, "2": "twenty", "3": "thirty", "4": "fourty", "5": "fifty", "6": "sixty", "7": "seventy", "8": "eighty", "9": "ninety"}, "teensDict": {"16": "sixteen", "17": "seventeen", "18": "eighteen", "19": "nineteen", "10": "ten", "11": "eleven", "12": "twelve", "13": "thirteen", "14": "fourteen", "15": "fifteen"}, "unitsDict": {"1": "one", "2": "two", "3": "three", "4": "four", "5": "five", "6": "six", "7": "seven", "8": "eight", "9": "nine"}} |
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
#!/bin/python | |
import sys, json, argparse | |
from operator import add | |
# Exit codes | |
OK = 0 | |
IOERROR = 1 | |
UNEXPECTED_ERROR = 2 | |
class Calc() : | |
dict = None | |
def __init__(self): | |
if self.dict == None: | |
with open('dict', 'r') as f: | |
self.dict = json.loads(f.read()) | |
# set up dictionaries shortcuts | |
self.unitsDict = self.dict['unitsDict'] | |
self.decimalsDict = self.dict['decimalsDict'] | |
self.teensDict = self.dict['teensDict'] | |
def get_args_expressions(self): | |
# use arg parse to get expressions | |
parser = argparse.ArgumentParser(description='Just calculator. \ | |
If you will specify no options, it enters interactive mode.') | |
parser.add_argument('expressions', nargs='*', | |
help="Your expressions. Calc evaluates them and \ | |
will exit.") | |
parsed_args = parser.parse_args() | |
return parsed_args.expressions | |
def loop(self): | |
# start shell | |
try: | |
while True: | |
input = raw_input('>>').split(',') | |
# new cycle on 'enter' hit | |
if input[0] == '': | |
continue | |
self.eval_args(input) | |
except KeyboardInterrupt: | |
print '\n\nBye!' | |
return OK | |
def run(self): | |
expressions = self.get_args_expressions() | |
if len(expressions): | |
args = reduce(add, expressions) | |
self.eval_args(args. split(',')) | |
return OK | |
return self.loop() | |
def print_decorator(f): | |
def wrapper(self, arr): | |
for s in f(self, arr): | |
print s | |
return wrapper | |
def itow(self, n): | |
def splitInDigits(n): | |
list = [int(d) for d in str(n)] | |
while len(list) != 3: | |
list = [0] + list | |
return list[::-1] | |
def process(digits): | |
unit = digits[0] | |
decimal = digits[1] | |
hundred = digits[2] | |
ret = self.unitsDict[str(hundred)] + ' hundred' if unit == decimal == 0 and hundred > 0 else \ | |
self.unitsDict[str(hundred)] + ' hundred and ' if hundred > 0 else '' | |
ret += self.decimalsDict[str(decimal)] + ' ' if decimal > 0 and decimal != 1 else \ | |
self.teensDict[str(decimal * 10 + unit)] if decimal == 1 else '' | |
if decimal == 1: return ret; | |
ret += self.unitsDict[str(unit)] + ' ' if unit > 0 else '' | |
return ret | |
return process(splitInDigits(n)) | |
@print_decorator | |
def eval_args(self, args): | |
return map(lambda x: self.itow(eval(x)), args) | |
def main(): | |
try: | |
return Calc().run() | |
except IOError as e: | |
print "I/O error({}): {}".format(e.errno, e.strerror) | |
return IOERROR | |
except SystemExit: | |
return OK | |
except: | |
print "Unexpected error(): {}".format(sys.exc_info()) | |
return UNEXPECTED_ERROR | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment