139 lines
6.0 KiB
Python
139 lines
6.0 KiB
Python
|
import sys
|
||
|
import os
|
||
|
|
||
|
# defining some constants
|
||
|
|
||
|
r = 3 # round to n digits
|
||
|
column_width = 11 # set column width of final table
|
||
|
|
||
|
# checking if the file is provided correctly
|
||
|
|
||
|
if len(sys.argv) != 2:
|
||
|
print(f"Please, provide data file\nUsage example: python3 {sys.argv[0]} sample_file.txt")
|
||
|
sys.exit(1)
|
||
|
|
||
|
if not os.path.exists(sys.argv[1]):
|
||
|
print("File does not exist")
|
||
|
sys.exit(1)
|
||
|
|
||
|
# defining functions
|
||
|
|
||
|
def avg(a):
|
||
|
return sum(a) / len(a)
|
||
|
|
||
|
def get_max_len(a):
|
||
|
return max(list(map(lambda x: len(str(x)), a)))
|
||
|
|
||
|
# reading input data from the file
|
||
|
|
||
|
possible_units = {"mass": {"kilogram": 1, "gram": 0.001}}
|
||
|
|
||
|
selected_table = 0
|
||
|
|
||
|
tables = {"1": {"r2": 0, "r1": 0, "norms": {"mass": 1}, "stats": []},
|
||
|
"2": {"r2": 0, "r1": 0, "norms": {"mass": 1}, "stats": []}}
|
||
|
|
||
|
with open(sys.argv[1]) as source_file:
|
||
|
for raw_i in source_file:
|
||
|
i = raw_i.rstrip("\n\r")
|
||
|
table_line = i.split()
|
||
|
if len(table_line) > 0:
|
||
|
if table_line[0] == "#":
|
||
|
if table_line[1] == "table":
|
||
|
selected_table = table_line[2]
|
||
|
elif table_line[1] == "r1":
|
||
|
tables[selected_table]["r1"] = float(table_line[2])
|
||
|
elif table_line[1] == "r2":
|
||
|
tables[selected_table]["r2"] = float(table_line[2])
|
||
|
elif table_line[1] == "set_unit":
|
||
|
if table_line[2] in possible_units and table_line[3] in possible_units[table_line[2]]:
|
||
|
tables[selected_table]["norms"][table_line[2]] = possible_units[table_line[2]][table_line[3]]
|
||
|
else:
|
||
|
print(f"Unsupported unit defined in line: '{i}'")
|
||
|
else:
|
||
|
if len(table_line) == 7:
|
||
|
new_data = list(map(float, table_line))
|
||
|
new_data[0] = round(new_data[0] * tables[selected_table]["norms"]["mass"], r)
|
||
|
tables[selected_table]["stats"].append(new_data)
|
||
|
|
||
|
# processing inputs
|
||
|
|
||
|
required_data = {"1": {"stats": [], "Mt": 0, "Imin": 0},
|
||
|
"2": {"stats": [], "Mt": 0, "Imin": 0}}
|
||
|
|
||
|
for i in tables:
|
||
|
for j in tables[i]['stats']:
|
||
|
new_line = []
|
||
|
new_line.append(round(j[0]*tables[i]['r1']*9.81, r))
|
||
|
new_line.append(round(avg(j[1:4]), r))
|
||
|
new_line.append(round(2/(tables[i]['r1']*new_line[1]), r))
|
||
|
|
||
|
new_line.append(round(j[0]*tables[i]['r2']*9.81, r))
|
||
|
new_line.append(round(avg(j[4:8]), r))
|
||
|
new_line.append(round(2/(tables[i]['r2']*new_line[4]), r))
|
||
|
|
||
|
required_data[i]["stats"].append(new_line)
|
||
|
|
||
|
# printing out the results
|
||
|
|
||
|
|
||
|
'''
|
||
|
for i in tables:
|
||
|
table_widths = [get_max_len(list(zip(tables[i]['stats']))[0]),
|
||
|
get_max_len(list(zip(required_data[i]['stats']))[0]),
|
||
|
get_max_len(list(zip(tables[i]['stats']))[1]),
|
||
|
get_max_len(list(zip(tables[i]['stats']))[2]),
|
||
|
get_max_len(list(zip(tables[i]['stats']))[3]),
|
||
|
get_max_len(list(zip(required_data[i]['stats']))[1]),
|
||
|
get_max_len(list(zip(required_data[i]['stats']))[2]),
|
||
|
|
||
|
get_max_len(list(zip(tables[i]['stats']))[0]),
|
||
|
get_max_len(list(zip(required_data[i]['stats']))[3]),
|
||
|
get_max_len(list(zip(tables[i]['stats']))[4]),
|
||
|
get_max_len(list(zip(tables[i]['stats']))[5]),
|
||
|
get_max_len(list(zip(tables[i]['stats']))[6]),
|
||
|
get_max_len(list(zip(required_data[i]['stats']))[4]),
|
||
|
get_max_len(list(zip(required_data[i]['stats']))[5]),
|
||
|
]
|
||
|
print("|".join( list(map(lambda x: str(x[0].center(x[1])), zip(["m", "Mi", "t1", "t2", "t3", "❬t❭", "βi"]*2, table_widths)))))
|
||
|
for j in range(len(required_data[i]['stats'])):
|
||
|
print("|".join( list(map(lambda x: str(x[0].center(x[1])),
|
||
|
zip([tables[i]['stats'][j][0],
|
||
|
required_data[i]['stats'][j][0],
|
||
|
tables[i]['stats'][j][1],
|
||
|
tables[i]['stats'][j][2],
|
||
|
tables[i]['stats'][j][3],
|
||
|
required_data[i]['stats'][j][1],
|
||
|
required_data[i]['stats'][j][2],
|
||
|
|
||
|
tables[i]['stats'][j][0],
|
||
|
required_data[i]['stats'][j][3],
|
||
|
tables[i]['stats'][j][4],
|
||
|
tables[i]['stats'][j][5],
|
||
|
tables[i]['stats'][j][6],
|
||
|
required_data[i]['stats'][j][4],
|
||
|
required_data[i]['stats'][j][5]],
|
||
|
table_widths))) ))
|
||
|
'''
|
||
|
|
||
|
for i in tables:
|
||
|
print(f"Table #{i}")
|
||
|
print("|".join( list(map(lambda x: str(x).center(11), ["m", "Mi", "t1", "t2", "t3", "❬t❭", "βi"]*2))))
|
||
|
for j in range(len(required_data[i]['stats'])):
|
||
|
print("|".join( list(map(lambda x: str(x).center(11),
|
||
|
[tables[i]['stats'][j][0],
|
||
|
required_data[i]['stats'][j][0],
|
||
|
tables[i]['stats'][j][1],
|
||
|
tables[i]['stats'][j][2],
|
||
|
tables[i]['stats'][j][3],
|
||
|
required_data[i]['stats'][j][1],
|
||
|
required_data[i]['stats'][j][2],
|
||
|
|
||
|
tables[i]['stats'][j][0],
|
||
|
required_data[i]['stats'][j][3],
|
||
|
tables[i]['stats'][j][4],
|
||
|
tables[i]['stats'][j][5],
|
||
|
tables[i]['stats'][j][6],
|
||
|
required_data[i]['stats'][j][4],
|
||
|
required_data[i]['stats'][j][5]])) ))
|