-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabaser.py
More file actions
78 lines (64 loc) · 3.06 KB
/
databaser.py
File metadata and controls
78 lines (64 loc) · 3.06 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
import json
import os
def get_material_info():
# Check if materials.json exists and load it
if os.path.exists('materials.json'):
with open('materials.json', 'r') as file:
materials_db = json.load(file)
else:
materials_db = {}
material = {}
# Get material name and check if it exists
while True:
material['Material_Name'] = input("Enter material name: ") or "undefined"
if material['Material_Name'] in materials_db:
# Show existing material properties
print("\nCurrent material properties:")
for key, value in materials_db[material['Material_Name']].items():
print(f"{key}: {value}")
print() # Empty line for better readability
overwrite = input(f"Material '{material['Material_Name']}' already exists. Do you want to overwrite it? (y/n): ").lower()
if overwrite == 'y':
break
print("Please enter a different material name.")
else:
break
material['Elastic_Modulus'] = input("Enter Elastic Modulus (GPa): ") or "undefined"
material['Yield_Strength'] = input("Enter Yield Strength (MPa): ") or "undefined"
material['Tensile_Strength'] = input("Enter Tensile Strength (MPa): ") or "undefined"
material['Density'] = input("Enter Density (g/cm³): ") or "undefined"
material['Shear_Modulus'] = input("Enter Shear Modulus (GPa): ") or "undefined"
material['Poisson_Ratio'] = input("Enter Poisson Ratio: ") or "undefined"
material['Compressive_Strength'] = input("Enter Compressive Strength (MPa): ") or "undefined"
material['Category'] = input("Enter Material Category: ") or "undefined"
return material
def main():
# Check if materials.json exists, if not create empty dict
if os.path.exists('materials.json'):
with open('materials.json', 'r') as file:
materials_db = json.load(file)
else:
materials_db = {}
while True:
material_info = get_material_info()
material_name = material_info.pop('Material_Name')
# Check if material already exists
if material_name in materials_db:
# Show existing material properties
print("\nCurrent material properties:")
for key, value in materials_db[material_name].items():
print(f"{key}: {value}")
print() # Empty line for better readability
overwrite = input(f"Material '{material_name}' already exists. Do you want to overwrite it? (y/n): ").lower()
if overwrite != 'y':
print("Skipping this material.")
continue
materials_db[material_name] = material_info
# Save to JSON file
with open('materials.json', 'w') as file:
json.dump(materials_db, file, indent=4)
continue_input = input("\nWould you like to add another material? (y/n): ").lower()
if continue_input != 'y':
break
if __name__ == "__main__":
main()