Skip to content

Commit 4d1f0a6

Browse files
oglop1oglop1
oglop
authored andcommittedMar 2, 2016
added 2 new functions:
non_eur_convert from_eur_convert removed one function convert_amount updated readme.md
1 parent f697bb6 commit 4d1f0a6

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed
 

‎currency_converter.py

+44-17
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ def update_local_rates():
1515
return tree
1616

1717

18-
def convert_amount(amount, in_curr, currency_root, out_curr):
19-
path = "{{http://www.ecb.int/vocabulary/2002-08-01/eurofxref}}Cube/.[@currency='{c}']"
20-
18+
def non_eur_convert(amount, in_curr, currency_root, out_curr, path):
2119
in_curr = currency_root.find(path.format(c=in_curr)).attrib
2220
in_curr_rate = float(in_curr['rate'])
2321
in_curr_currency = in_curr['currency']
@@ -40,19 +38,41 @@ def convert_amount(amount, in_curr, currency_root, out_curr):
4038
else:
4139
print('Cannot convert from {fr} to {to}'.format(fr=in_curr_currency, to=oc), file=sys.stderr)
4240
continue
43-
# a
41+
42+
43+
def from_eur_convert(amount, in_curr, currency_root, out_curr, path):
44+
for oc in out_curr:
45+
try:
46+
tmp_out_curr = currency_root.find(path.format(c=oc)).attrib
47+
# print(tmp_out_curr)
48+
oc_rate = float(tmp_out_curr['rate'])
49+
oc_currency = tmp_out_curr['currency']
50+
wanted = oc_rate * amount
51+
# print("{am} {fr} = {res} {to}".format(am=amount, fr=in_curr_currency, res=wanted, to=oc_currency))
52+
yield oc_currency, wanted
53+
except (KeyError, AttributeError):
54+
if oc == 'EUR':
55+
# print("{am} {fr} = {res} {to}".format(am=amount, fr=in_curr_currency, res=wanted, to=oc))
56+
yield in_curr, amount
57+
else:
58+
print('Cannot convert from {fr} to {to}'.format(fr=in_curr, to=oc), file=sys.stderr)
59+
continue
60+
61+
4462
def cmd_args():
4563
parser = argparse.ArgumentParser(description='Currency Converter, source file fom ECB')
4664
parser.add_argument('amount', metavar='<amount>', type=float,
47-
help='Amount to be converted')
65+
help='Amount to be converted')
4866
parser.add_argument('fr', metavar='<from>',
49-
help='Input currency')
67+
help='Input currency')
5068

5169
parser.add_argument('to', metavar='<to>', nargs='*',
52-
default= ['EUR', 'USD', 'JPY', 'BGN', 'CZK', 'DKK', 'GBP', 'HUF', 'PLN', 'RON', 'SEK', 'CHF', 'NOK',
53-
'HRK', 'RUB', 'TRY', 'AUD', 'BRL', 'CAD', 'CNY', 'HKD', 'IDR', 'ILS', 'INR', 'KRW', 'MXN',
54-
'MYR', 'NZD', 'PHP', 'SGD', 'THB', 'ZAR'],
55-
help='Output currency(ies), EUR, CZK...')
70+
default=['EUR', 'USD', 'JPY', 'BGN', 'CZK', 'DKK', 'GBP', 'HUF', 'PLN', 'RON', 'SEK', 'CHF',
71+
'NOK',
72+
'HRK', 'RUB', 'TRY', 'AUD', 'BRL', 'CAD', 'CNY', 'HKD', 'IDR', 'ILS', 'INR', 'KRW',
73+
'MXN',
74+
'MYR', 'NZD', 'PHP', 'SGD', 'THB', 'ZAR'],
75+
help='Output currency(ies), EUR, CZK...')
5676

5777
parser.add_argument('-json',
5878
action='store_true',
@@ -63,7 +83,6 @@ def cmd_args():
6383

6484

6585
def main():
66-
6786
args = cmd_args()
6887
args.to = list(map(lambda x: x.strip(',;'), args.to))
6988

@@ -78,17 +97,26 @@ def main():
7897

7998
now = dt.datetime.now()
8099

81-
if (currency_time != now.date().isoformat()) and now.hour > 16: # updated today after 16:00
100+
# update local XML in case of old info
101+
if (currency_time != now.date().isoformat()) and now.hour > 16 and now.weekday() not in (5, 6):
102+
print(currency_time)
82103
tree = update_local_rates()
83104
root = tree.getroot()
84105

85106
cube = root[2][0]
86107

87-
converted_dict = dict((k, v) for (k, v) in convert_amount(10, args.fr , cube, args.to))
108+
path = "{{http://www.ecb.int/vocabulary/2002-08-01/eurofxref}}Cube/.[@currency='{c}']"
109+
110+
if args.fr == 'EUR':
111+
res_gen = from_eur_convert(args.amount, args.fr, cube, args.to, path)
112+
else:
113+
res_gen = non_eur_convert(args.amount, args.fr, cube, args.to, path)
114+
115+
converted_dict = dict((k, v) for (k, v) in res_gen)
88116

89-
output = { "input": { "amount": args.amount, "currency": args.fr},
90-
"output": converted_dict
91-
}
117+
output = {"input": {"amount": args.amount, "currency": args.fr},
118+
"output": converted_dict
119+
}
92120

93121
if args.json:
94122
with open('currency.json', 'w') as oj:
@@ -98,6 +126,5 @@ def main():
98126
print(json.dumps(output, sort_keys=True, indent=4))
99127

100128

101-
102129
if __name__ == '__main__':
103130
main()

0 commit comments

Comments
 (0)
Please sign in to comment.