add script for lab 51

This commit is contained in:
dymik739 2023-03-19 22:14:11 +02:00
parent 53b36e1d4d
commit d024b6d38d
2 changed files with 164 additions and 0 deletions

138
labs/51/processor.py Normal file
View File

@ -0,0 +1,138 @@
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]])) ))

26
labs/51/samples/v2.txt Normal file
View File

@ -0,0 +1,26 @@
# m t1 t2 t3 t1 t2 t3
# table 1
# set_unit mass kilogram
# r2 0.02
# r1 0.035
0.0852 15.91 16.57 16.62 8.11 8.09 8.00
0.11602 11.63 11.47 11.49 6.68 6.6 6.95
0.13156 10.68 11.02 11.2 6.38 6.37 6.37
0.1553 9.91 9.86 9.96 5.68 5.42 5.9
0.16238 9.77 9.61 10.0 5.38 5.6 5.4
0.17084 9.05 9.18 9.29 5.49 5.63 5.33
# table 2
# set_unit mass gram
# r2 0.02
# r1 0.035
85.2 6.51 6.3 6.61 3.6 3.8 3.93
116.02 5.27 5.25 5.47 2.86 3.02 2.99
131.56 4.85 4.98 4.92 2.95 2.82 2.57
155.3 4.41 4.4 4.6 2.3 2.6 2.5
162.38 4.3 4.36 4.4 2.4 2.52 2.57
170.84 4.14 4.25 4.28 2.29 2.28 2.4