-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrute_force.py
More file actions
101 lines (78 loc) · 2.94 KB
/
Copy pathbrute_force.py
File metadata and controls
101 lines (78 loc) · 2.94 KB
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
import csv
from time import perf_counter
from itertools import combinations
def measure(func):
def wrapper(*args, **kwargs):
start = perf_counter()
try:
return func(*args, **kwargs)
finally:
end_ = perf_counter() - start
print(f"{func.__name__} execution time: {end_ if end_ >0 else 0} s")
return wrapper
def extract_percent(param):
return float(param.split("%")[0])
class Equity:
def __init__(self, name: "str", cost: "float", percent_profit: "float"):
self.name = name
self.cost = cost
self.percent_profit = percent_profit
@property
def profit(self):
return self.cost * self.percent_profit / 100
def __repr__(self):
return f"{self.name}"
class Repository:
def __init__(self):
self.equities = []
def _list_of_combination(self):
list_of_combination_with_sum= []
for i in range(len(self.equities)):
combinations_ = combinations(self.equities, r=i + 1)
for combination in combinations_:
list_of_combination_with_sum.append(self._populate_sum_combination(combination))
print("il y a ", len(list_of_combination_with_sum), "combinations")
return sorted(
list_of_combination_with_sum,
key=lambda element: element["profit"],
reverse=True,
)
def _populate_sum_combination(self, combination):
return {
"equities": combination,
"cost": self._get_sum_cost(combination),
"profit": self._get_sum_profit(combination),
}
def _get_sum_profit(self, combination):
return sum([equity.profit for equity in combination])
def _get_sum_cost(self, combination):
return sum([equity.cost for equity in combination])
def add_equity(self, equity: "Equity"):
self.equities.append(equity)
@measure
def get_best_combination_by_brute_force(self, max_investment):
for item in self._list_of_combination():
if item["cost"] <= max_investment:
return item
return False
class App:
def __init__(self):
self.actions_repository = Repository()
def populate_equities_with_csv(self, csv_enquiries):
with open(csv_enquiries, newline="") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
self.actions_repository.add_equity(
Equity(
row["action"],
float(row["cost"]),
extract_percent(row["percent"]),
)
)
if __name__ == "__main__":
app = App()
app.populate_equities_with_csv("equities.csv")
result = app.actions_repository.get_best_combination_by_brute_force(500)
print(
f"{result['equities']}: for profit of {result['profit']} from cost {result['cost']} "
)