add setup
This commit is contained in:
175
src/materials.py
Executable file
175
src/materials.py
Executable file
@@ -0,0 +1,175 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""\
|
||||
Material database.
|
||||
"""
|
||||
__author__ = "Daniel Weschke"
|
||||
__copyright__ = "Copyright 2019 Daniel Weschke"
|
||||
__credits__ = ["Daniel Weschke"]
|
||||
__license__ = "MIT"
|
||||
__version__ = "2019.02.03"
|
||||
__maintainer__ = "Daniel Weschke"
|
||||
__email__ = "daniel.weschke@directbox.de"
|
||||
__status__ = "Production" # "Prototype", "Development", "Production"
|
||||
|
||||
VERSION = """\
|
||||
%(prog)s version {version} {copyright}
|
||||
""".format(
|
||||
version=__version__, copyright=__copyright__)
|
||||
|
||||
EPILOG = """\
|
||||
"""
|
||||
|
||||
|
||||
def read_dir(dir, exclude=[]):
|
||||
"""\
|
||||
Read all files in directory as list
|
||||
"""
|
||||
from os import listdir
|
||||
|
||||
result = []
|
||||
for data in listdir(dir):
|
||||
file_name = data.split('.')[0]
|
||||
if file_name not in exclude:
|
||||
result.append(file_name)
|
||||
return result
|
||||
|
||||
|
||||
def print_list(list):
|
||||
"""\
|
||||
Print list
|
||||
"""
|
||||
for data in list:
|
||||
print(data.title())
|
||||
|
||||
|
||||
def read_file(input_file):
|
||||
"""\
|
||||
Read file as string
|
||||
"""
|
||||
try:
|
||||
with open(input_file) as data_file:
|
||||
return data_file.read()
|
||||
except (OSError, IOError) as err:
|
||||
print(str(err))
|
||||
sys.exit(2)
|
||||
|
||||
|
||||
def print_data(data):
|
||||
"""\
|
||||
Convert string data to json data and print it.
|
||||
"""
|
||||
from json import loads
|
||||
from pprint import pprint
|
||||
|
||||
#print(data)
|
||||
pprint(loads(data))
|
||||
|
||||
|
||||
def search_keys(data, keys=None):
|
||||
"""\
|
||||
Search for json keys in string data
|
||||
"""
|
||||
from json import loads
|
||||
|
||||
def find_values(key, json_repr):
|
||||
results = []
|
||||
def _decode_dict(a_dict):
|
||||
try:
|
||||
results.append(a_dict[key])
|
||||
except KeyError:
|
||||
pass
|
||||
return a_dict
|
||||
|
||||
loads(json_repr, object_hook=_decode_dict) # return value ignored
|
||||
return results
|
||||
|
||||
if keys is not None:
|
||||
for key in keys:
|
||||
found_values = find_values(key, data)
|
||||
#print(found_values)
|
||||
for value in found_values:
|
||||
print(key + ' = ' + '%s' % value[0] + ' ' + value[1])
|
||||
|
||||
#def getLength(element):
|
||||
# try:
|
||||
# element.__iter__
|
||||
# return sum([getLength(i) for i in element])
|
||||
# except:
|
||||
# return 1
|
||||
#print(len(data))
|
||||
#print(getLength(json.loads(data)))
|
||||
|
||||
|
||||
def main():
|
||||
"""\
|
||||
Main function
|
||||
"""
|
||||
from os import path
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description=__doc__, prefix_chars='-', epilog=EPILOG,
|
||||
#usage="%(prog)s [OPTION]... NAME",
|
||||
formatter_class=argparse.RawTextHelpFormatter,
|
||||
)
|
||||
parser.add_argument('-v', '--verbose', action="store_true", help="Verbose output")
|
||||
parser.add_argument('-V', '--version', action='version', version=VERSION)
|
||||
parser.add_argument('-D', '--debug', dest='debug', action='store_true', help=argparse.SUPPRESS)
|
||||
|
||||
subparsers = parser.add_subparsers(help='commands', dest='command')
|
||||
|
||||
# list materials
|
||||
parser_list = subparsers.add_parser('list',
|
||||
description='List materials.',
|
||||
help='list all available materials')
|
||||
|
||||
# get material
|
||||
parser_get = subparsers.add_parser('get',
|
||||
description='Get material information.',
|
||||
help='get material information')
|
||||
parser_get.add_argument('materialname', action='store',
|
||||
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')
|
||||
parser_get.add_argument('--search', action='store',
|
||||
help='search for material information, comma delimited keys')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.debug:
|
||||
print(args)
|
||||
|
||||
if args.command == 'list':
|
||||
list = read_dir("./data", "INFO")
|
||||
print_list(list)
|
||||
|
||||
elif args.command == 'get':
|
||||
filename = path.join('data', args.materialname + '.json')
|
||||
data = read_file(filename)
|
||||
if args.debug:
|
||||
print("filename: " + filename)
|
||||
if args.const_collection:
|
||||
if "rho" in args.const_collection:
|
||||
args.const_collection.append("rho(T)")
|
||||
if args.debug:
|
||||
print(args)
|
||||
#search_keys(data, keys=args.const_collection)
|
||||
elif args.search:
|
||||
search_list = [element for element in args.search.split(',')]
|
||||
if args.debug:
|
||||
print("search_list: " + str(search_list))
|
||||
search_keys(data, keys=search_list)
|
||||
else:
|
||||
print_data(data)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user