-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
43 lines (37 loc) · 1.28 KB
/
utils.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
import time
import csv
from typing import Any, Dict, List, Tuple
def time_it(function):
"""
Encapsule la fonction et affiche le temps d'exécutation (decorateur)
"""
def wrapper(*args, **kwargs):
start = time.time()
res = function(*args, **kwargs)
elapsed = time.time() - start
print(f"{function.__name__} --- {elapsed:.5f} seconds ---")
return res
return wrapper
def load_shares(file_name: str) -> List[Dict]:
"""
Lit un csv à l'aide du dict reader et retourne une liste d'actions
"""
shares = []
with open(file_name, 'r', encoding='utf-8-sig') as file:
reader = csv.DictReader(file, delimiter=',')
next(reader)
for share in reader:
share['price'] = float(share['price'])
share['gain'] = share['price']*float(share['profit'])/100
shares.append(share)
return shares
def convert_shares(shares: List[Dict[str, Any]], coefficient: int) -> List[Tuple[str, int, int]]:
"""
Return une liste de tuple
"""
shares_opti = []
for share in shares:
if share['price'] > 0:
tuple = (share['name'], int(share['price']*coefficient), int(share['gain']*coefficient))
shares_opti.append(tuple)
return shares_opti