-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
128 lines (109 loc) · 3.45 KB
/
backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import json
import urllib.request
rate = 0
def getting_rate():
'''
Gets a ruble exchange course from openexchangerates.org
'''
global rate
for attempt in range(10):
try:
curr_page = urllib.request.urlopen('http://openexchangerates.org/api/latest.json?app_id=9f0710764c064370932f4f2496968c62')
obj = curr_page.read().decode(encoding='UTF-8')
content = json.loads(obj)
rate = round(content['rates']['RUB'], 2)
except:
pass
if rate:
break
def fill_entries(currency, column, value):
"""
Fills values
:param currency: 'rub' or 'dol'
:param column: int 1-5
:param value: float
:return: (entries_dol, entries_rub)
"""
# calculating the salary per hour
per_hour = 0
if column == 0: # hour
per_hour = value
elif column == 1: # day
per_hour = value/8
elif column == 2: # week
per_hour = value/40
elif column == 3: # month
per_hour = value/22/8
elif column == 4: # year
per_hour = value/52/40
# calculating all types of salary
row = [per_hour, per_hour*8, per_hour*40, per_hour*22*8, per_hour*52*40]
if currency == 'dol':
result = (row, [dol * rate for dol in row])
else:
result = ([rub/rate for rub in row], row)
# rounding salary
rounded_result = (list(map(lambda x: round(x, 2), result[0])), list(map(lambda x: round(x, 2), result[1])))
return rounded_result
def check_input(entries1, entries2):
"""
Checks the incoming values
:param entries1: list of floats (dol entries)
:param entries2: list of floats (rub entries)
:return: True if params are OK, else False
"""
# check the length
if len(entries1)!=5 or len(entries2)!=5:
return False
# check the float
try:
entries1 = list(map(lambda x: float(x) if x else None, entries1))
entries2 = list(map(lambda x: float(x) if x else None, entries2))
except ValueError:
return False
# check for a single positive value
values_count = 0
for row in [entries1, entries2]:
for entry in row:
if entry:
values_count += 1
if entry < 0:
return False
if values_count != 1:
return False
return True
def convert_salary(entries_dol, entries_rub):
"""
Converts salary
:param entries_dol: list of float. Len = 5
:param entries_rub: list of float. Len = 5
:return: (entries_dol, entries_rub)
or 'ErrorURL' if fail to get ration
or 'ErrorIncome' if got wrong params
"""
# trying to get rate
if not rate:
getting_rate()
if not rate:
return 'ErrorURL'
# checking for wrong input
if not check_input(entries_dol, entries_rub):
return 'ErrorIncome'
#convert to float
entries_dol = list(map(lambda x: float(x) if x else 0.0, entries_dol))
entries_rub = list(map(lambda x: float(x) if x else 0.0, entries_rub))
# getting converted values
column = 0
result = []
for entry in entries_dol:
if entry:
result = fill_entries('dol', column, entry)
column += 1
column = 0
for entry in entries_rub:
if entry:
result = fill_entries('rub', column, entry)
column += 1
return result
if __name__ == '__main__':
print(convert_salary(['1','0','','',''],['0','0','','','']))