add def to load material data into python object, add def to print quantity and add some get options
This commit is contained in:
@@ -77,15 +77,23 @@ def read_material(materialname, fullpath=True):
|
|||||||
sys.exit(2)
|
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
|
import json
|
||||||
from pprint import pprint
|
|
||||||
|
return json.loads(read_material(materialname))
|
||||||
|
|
||||||
|
|
||||||
|
def print_material(data):
|
||||||
|
"""\
|
||||||
|
Print material data (Python object).
|
||||||
|
"""
|
||||||
|
import pprint
|
||||||
|
|
||||||
#print(data)
|
#print(data)
|
||||||
pprint(loads(data))
|
pprint.pprint(data)
|
||||||
|
|
||||||
|
|
||||||
def search_keys(data, keys):
|
def search_keys(data, keys):
|
||||||
@@ -118,16 +126,24 @@ def search_keys(data, keys):
|
|||||||
|
|
||||||
def print_dict(data_dict, print_keys=True, prespaces=0):
|
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 key, values in data_dict.items():
|
||||||
for value in values:
|
for value in values:
|
||||||
if print_keys:
|
if print_keys:
|
||||||
print(key + ' = ', end='')
|
print(key + ' = ', end='')
|
||||||
if isinstance(value, list) and len(value) == 2:
|
print_quantity(value, prespaces)
|
||||||
print(prespaces*' ' + '%s' % value[0] + ' ' + value[1])
|
|
||||||
else:
|
|
||||||
print(prespaces*' ' + '%s' % value)
|
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():
|
def main():
|
||||||
@@ -160,10 +176,16 @@ def main():
|
|||||||
help='list available material information')
|
help='list available material information')
|
||||||
parser_get.add_argument('-r', '--rho', dest='const_collection',
|
parser_get.add_argument('-r', '--rho', dest='const_collection',
|
||||||
action='append_const', const="rho",
|
action='append_const', const="rho",
|
||||||
help='get density from material')
|
help='get density')
|
||||||
parser_get.add_argument('-T', '--Tm', dest='const_collection',
|
parser_get.add_argument('-E', '--E', dest='const_collection',
|
||||||
action='append_const', const="T_m",
|
action='append_const', const="E",
|
||||||
help='get melting point temperature')
|
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',
|
parser_get.add_argument('--search', action='store',
|
||||||
help='search for material information, comma delimited keys')
|
help='search for material information, comma delimited keys')
|
||||||
|
|
||||||
@@ -182,23 +204,34 @@ def main():
|
|||||||
print_dict(data_dict, False, 2)
|
print_dict(data_dict, False, 2)
|
||||||
|
|
||||||
elif args.command == 'get':
|
elif args.command == 'get':
|
||||||
data = read_material(args.materialname)
|
|
||||||
if args.debug:
|
|
||||||
print("filename: " + filename)
|
|
||||||
if args.const_collection:
|
if args.const_collection:
|
||||||
|
data = load_material(args.materialname)
|
||||||
|
if args.debug:
|
||||||
|
print("data: " + data)
|
||||||
if "rho" in args.const_collection:
|
if "rho" in args.const_collection:
|
||||||
args.const_collection.append("rho(T)")
|
#args.const_collection.append("rho(T)")
|
||||||
if args.debug:
|
#f args.debug:
|
||||||
print(args)
|
# print(args)
|
||||||
#search_keys(data, keys=args.const_collection)
|
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:
|
elif args.search:
|
||||||
search_list = [element for element in args.search.split(',')]
|
search_list = [element for element in args.search.split(',')]
|
||||||
if args.debug:
|
if args.debug:
|
||||||
print("search_list: " + str(search_list))
|
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)
|
data_dict = search_keys(data, keys=search_list)
|
||||||
print_dict(data_dict)
|
print_dict(data_dict)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print_data(data)
|
print_material(data)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user