-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency_comapre_threaded.py
80 lines (60 loc) · 2.04 KB
/
currency_comapre_threaded.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
from forex_python.converter import CurrencyRates
from openpyxl import Workbook
from codetiming import Timer
from threading import Thread
from helper_functions import *
currency_list = ["USD",
"PLN",
"GBP",
"EUR",
"CHF",
"DKK",
"SEK",
"NOK",
"CZK",
"HUF",
"JPY"]
################################ ACTUAL CODE ################################
def ask_for_currency(curr_1, curr_2, sheet):
crc1 = currency_list[curr_1]
crc2 = currency_list[curr_2]
# print(f"Asking for conversionf between {crc1} and {crc2}")
if curr_1 != curr_2:
try:
var = round(
float(CurrencyRates().get_rate(crc1, crc2)),
4)
except:
var = "0"
else:
var = "-"
localization = toExclNot(curr_2+2, curr_1+2)
sheet[localization] = var
def fill_using_threading():
workbook = Workbook()
sheet = workbook.active
column = 1
row = 1
sheet[toExclNot(1,1)] = "One ↓ ="
for i in range(len(currency_list)):
localization = toExclNot(column + i + 1, row) # header row
sheet[localization] = currency_list[i]
localization = toExclNot(column, row + i + 1) # header column
sheet[localization] = currency_list[i]
threads = []
for curr_1 in range(len(currency_list)):
for curr_2 in range(len(currency_list)):
t = Thread(target=ask_for_currency, args=[curr_1, curr_2, sheet])
t.start()
threads.append(t)
working = [True]
while any(working):
working = []
for t in threads:
working.append(t.is_alive())
return workbook
################################# EXECUTION #################################
if __name__ == "__main__":
with Timer(text="\nTotal elapsed time: {:.2f}s"):
workbook = fill_using_threading()
workbook.save(filename="currencies_threded_solution.xlsx")