-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency.py
57 lines (42 loc) · 1.48 KB
/
currency.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
import requests
import json
from abc import ABC
from abc import abstractmethod
from functools import wraps
def get_daily_json(): # updates JSON with different currencies values
resp = requests.get(
'https://www.cbr-xml-daily.ru/daily_json.js'
)
with open('request_result.json', 'w', encoding='utf8') as handler:
json.dump(resp.json(), handler, indent=4, ensure_ascii=False)
def update(func): # wrapper. Runs get_daily_json inb4 func
@wraps(func)
def wrap(*args, **kwargs):
print('Updating daily JSON...')
get_daily_json()
print(f'Daily JSON updated. running {func.__name__} function.')
func(*args, **kwargs)
print(f'Function {func.__name__} stopped running.')
return func(*args, **kwargs)
return wrap
class Currency(ABC): # abstract class, describes currency
def __init__(self, amount):
super().__init__()
self.amount = float(amount)
self._output = self.get_rub_value()
self.rub_value = self._output[0]*self.amount
self.prev_rub_value = self._output[1]*self.amount
self.diff = self._output[0] - self._output[1]
@update
def get_rub_value(self): # parses JSON to get rouble equivalents
with open('request_result.json', 'r', encoding='utf8') as handler:
cb_dict = json.load(handler)
rub_value = cb_dict['Valute'][self.CHAR_CODE]['Value']
prev_rub_value = cb_dict['Valute'][self.CHAR_CODE]['Previous']
return [rub_value, prev_rub_value]
def update():
get_daily_json()
class Dollar(Currency):
CHAR_CODE = 'USD'
class Euro(Currency):
CHAR_CODE = 'EUR'