def test_data_to_list(): source = """<Data format="integer">972,561</Data>""" node = et.fromstring(source) assert utils.matml_to_dict(node) == {'Data': [972, 561]} source = "<Data>23,1370</Data>" node = et.fromstring(source) assert utils.matml_to_dict(node) == {'Data': [23, 1370]} source = """<Data format="float">+11.5,+8.5,+7,+6.5,+6.5</Data>""" node = et.fromstring(source) assert utils.matml_to_dict(node) == {'Data': [11.5, 8.5, 7, 6.5, 6.5]}
def test_node_with_multiple_children_of_same_name(): source = """<?xml version="1.0"?> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> """ target = { 'country': { '@name': "Liechtenstein", 'rank': 1, 'year': 2008, 'gdppc': 141100, 'neighbor': [{ '@name': 'Austria', '@direction': 'E' }, { '@name': 'Switzerland', '@direction': 'W' }] } } node = et.fromstring(source) temp = utils.matml_to_dict(node) assert temp == target
def test_node_with_attrib(): source = """<?xml version="1.0"?> <country name="Liechtenstein"> Some text... </country> """ target = {'country': {'@name': "Liechtenstein", '#text': "Some text..."}} node = et.fromstring(source) temp = utils.matml_to_dict(node) assert temp == target
def test_node_with_children(): source = """<?xml version="1.0"?> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> </country> """ target = { 'country': { '@name': "Liechtenstein", 'rank': 1, 'year': 2008, 'gdppc': 141100, } } node = et.fromstring(source) temp = utils.matml_to_dict(node) assert temp == target
def test_countries_complete(): source = """<?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data> """ target = { 'data': { 'country': [{ '@name': "Liechtenstein", 'rank': 1, 'year': 2008, 'gdppc': 141100, 'neighbor': [{ '@name': 'Austria', '@direction': 'E' }, { '@name': 'Switzerland', '@direction': 'W' }] }, { '@name': "Singapore", 'rank': 4, 'year': 2011, 'gdppc': 59900, 'neighbor': { '@name': 'Malaysia', '@direction': 'N' } }, { '@name': "Panama", 'rank': 68, 'year': 2011, 'gdppc': 13600, 'neighbor': [{ '@name': 'Costa Rica', '@direction': 'W' }, { '@name': 'Colombia', '@direction': 'E' }] }] } } node = et.fromstring(source) temp = utils.matml_to_dict(node) pp.pprint(temp) assert temp == target
def test_node_with_text(): source = "<textnode>Some text...</textnode>" target = {'textnode': "Some text..."} node = et.fromstring(source) temp = utils.matml_to_dict(node) assert temp == target
filepath = sys.argv[1] print(filepath) except: print("Please pass filename when calling the script.") sys.exit() filename = path.basename(filepath).split('.')[0] with open(filepath, 'r', encoding="utf-8") as fp: orig_data = fp.read() # get root node from string rootnode = et.fromstring(orig_data) # transform xml into json material = mu.matml_to_dict(rootnode) # from the dict, get the correct node (MatML_Doc) try: material = material.get('EngineeringData').get('Materials') except: pass matml_dict = material.get('MatML_Doc') # create a pretty dict matson = mu.make_matson(matml_dict) # save both versions as json to seperate files with open("sandbox/" + filename + ".json", 'w', encoding='utf-8') as fp: json.dump(matml_dict, fp, sort_keys=True, indent=2)