add def to load material data into python object, add def to print quantity and add some get options

This commit is contained in:
2019-02-10 12:36:09 +01:00
parent 6ff467a528
commit a2ec5ca0fb

View File

@@ -77,15 +77,23 @@ def read_material(materialname, fullpath=True):
sys.exit(2)
def print_data(data):
def load_material(materialname):
"""\
Convert string data to json data and print it.
Read material file and convert it to a Python object.
"""
from json import loads
from pprint import pprint
import json
return json.loads(read_material(materialname))
def print_material(data):
"""\
Print material data (Python object).
"""
import pprint
#print(data)
pprint(loads(data))
pprint.pprint(data)
def search_keys(data, keys):
@@ -118,16 +126,24 @@ def search_keys(data, keys):
def print_dict(data_dict, print_keys=True, prespaces=0):
"""\
Search for json keys in string data
Print one dimensional dict data.
"""
for key, values in data_dict.items():
for value in values:
if print_keys:
print(key + ' = ', end='')
if isinstance(value, list) and len(value) == 2:
print(prespaces*' ' + '%s' % value[0] + ' ' + value[1])
else:
print(prespaces*' ' + '%s' % value)
print_quantity(value, prespaces)
def print_quantity(quantity, prespaces=0):
"""\
Print quantity.
Either a number or a list of two elements, the first the magnitude and the second the unit
"""
if isinstance(quantity, list) and len(quantity) == 2:
print(prespaces*' ' + '%s' % quantity[0] + ' ' + quantity[1])
else:
print(prespaces*' ' + '%s' % quantity)
def main():
@@ -160,10 +176,16 @@ def main():
help='list available material information')
parser_get.add_argument('-r', '--rho', dest='const_collection',
action='append_const', const="rho",
help='get density from material')
parser_get.add_argument('-T', '--Tm', dest='const_collection',
action='append_const', const="T_m",
help='get melting point temperature')
help='get density')
parser_get.add_argument('-E', '--E', dest='const_collection',
action='append_const', const="E",
help='get Young\'s modulus, module of elasticity')
parser_get.add_argument('-R', '--R_p02', dest='const_collection',
action='append_const', const="R_p02",
help='get yield strength R_e, R_{p0.2}')
parser_get.add_argument('--R_m', dest='const_collection',
action='append_const', const="R_m",
help='get ultimate tensile strength R_m')
parser_get.add_argument('--search', action='store',
help='search for material information, comma delimited keys')
@@ -182,23 +204,34 @@ def main():
print_dict(data_dict, False, 2)
elif args.command == 'get':
data = read_material(args.materialname)
if args.debug:
print("filename: " + filename)
if args.const_collection:
data = load_material(args.materialname)
if args.debug:
print("data: " + data)
if "rho" in args.const_collection:
args.const_collection.append("rho(T)")
if args.debug:
print(args)
#search_keys(data, keys=args.const_collection)
#args.const_collection.append("rho(T)")
#f args.debug:
# print(args)
print_quantity(data['Mechanical Properties']['rho'])
if "E" in args.const_collection:
print_quantity(data['Mechanical Properties']['E'])
if "R_p02" in args.const_collection:
print_quantity(data['Mechanical Properties']['R_p02'])
if "R_m" in args.const_collection:
print_quantity(data['Mechanical Properties']['R_m'])
elif args.search:
search_list = [element for element in args.search.split(',')]
if args.debug:
print("search_list: " + str(search_list))
data = read_material(args.materialname)
if args.debug:
print("data: " + data)
data_dict = search_keys(data, keys=search_list)
print_dict(data_dict)
else:
print_data(data)
print_material(data)
return 0