-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
115 lines (86 loc) · 3.17 KB
/
main.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
import argparse
import json
import weights as wt
from weights import Weights
def inventory():
weight_counts = dict()
input_string = input("Enter different weights (separated by space, decimal-symbol . ): ")
string_weights = input_string.split()
print(string_weights)
weights = [float(x) for x in string_weights]
for w in weights:
count = int(input("How many times do you have {} kg: ".format(w)))
weight_counts[w] = count
print(weight_counts)
with open("weights.json", "w") as jfile:
jfile.write(json.dumps(weight_counts))
def load_weights():
weight_counts = dict()
try:
with open("weights.json", "r") as jfile:
tmp_weight_counts = json.loads(jfile.read())
except FileNotFoundError as e:
print(e)
print("run 'inventory' command first")
exit(e.errno)
return
for k, v in tmp_weight_counts.items():
weight_counts[float(k)] = int(v)
# print(weight_counts)
return weight_counts
def configure_weights():
weight_counts = load_weights()
weights = Weights(weight_counts)
weight_objects = [weights]
possible_total_weights = wt.possible_weights(weight_objects)
double_weights = wt.possible_double_weights(weight_objects)
input_weights = []
keep_going = True
while len(possible_total_weights) > 1 and keep_going:
print()
print("possible total weights:")
for w in possible_total_weights:
double_indicator = " "
if w in double_weights:
double_indicator = "* "
print("{}{:4.1f} kg".format(double_indicator, w))
while True:
try:
# enter any non-number to stop
total_weight = float(input("choose total weight: "))
if total_weight in possible_total_weights:
input_weights.append(total_weight)
break
print("invalid input")
except ValueError:
# non-number entered, stopping
total_weight = 0
keep_going = False
break
if keep_going:
weight_objects = wt.reduced_weight_objects(weight_objects, total_weight)
possible_total_weights = wt.possible_weights(weight_objects)
double_weights = wt.possible_double_weights(weight_objects)
#
print("===========================")
print("selected weights:")
print(input_weights)
print()
print("weight combos:")
weight_counts = load_weights()
weights = Weights(weight_counts)
if len(input_weights):
combos = wt.get_combos(weights, input_weights)
for w, c in combos:
weight = "{:4.1f} kg".format(w)
# weights_asc = sorted(c)
weights_dsc = sorted(c, reverse=True)
print("{}: {}".format(weight, weights_dsc))
if __name__ == "__main__":
FUNCTION_MAP = {"inventory": inventory,
"config": configure_weights}
parser = argparse.ArgumentParser()
parser.add_argument("command", choices=FUNCTION_MAP.keys())
args = parser.parse_args()
# print(args)
FUNCTION_MAP[args.command]()