def test_parsers(self): # populate a property tree from an input file ptree = PropertyTree() # INFO parser info_file = 'input.info' today = 'april 8th, 2016' with open(info_file, 'w') as fout: fout.write('date "' + today + '"') ptree.parse_info(info_file) self.assertEqual(ptree.get_string('date'), today) remove(info_file) # XML parser xml_file = 'input.xml' pi = 3.14 with open(xml_file, 'w') as fout: fout.write('<pi>' + str(pi) + '</pi>') ptree.parse_xml(xml_file) self.assertEqual(ptree.get_double('pi'), pi) remove(xml_file) # JSON parser json_file = 'input.json' with open(json_file, 'w') as fout: fout.write('{') fout.write(' "foo":') fout.write(' {') fout.write(' "bar": false') fout.write(' }') fout.write('}') ptree.parse_json(json_file) self.assertFalse(ptree.get_bool('foo.bar')) remove(json_file)
def test_property_tree(self): # ptree as container to store int, double, string, and bool ptree = PropertyTree() ptree.put_int('dim', 3) self.assertEqual(ptree.get_int('dim'), 3) ptree.put_double('path.to.pi', 3.14) self.assertEqual(ptree.get_double('path.to.pi'), 3.14) ptree.put_string('good.news', 'it works') self.assertEqual(ptree.get_string('good.news'), 'it works') ptree.put_bool('is.that.a.good.idea', False) self.assertEqual(ptree.get_bool('is.that.a.good.idea'), False)
def test_get_children(self): ptree = PropertyTree() # put child child = PropertyTree() child.put_double('prune', 6.10) ptree.put_child('a.g', child) self.assertEqual(ptree.get_double('a.g.prune'), 6.10) # get child ptree.put_string('child.name', 'clement') ptree.put_int('child.age', -2) child = ptree.get_child('child') self.assertEqual(child.get_string('name'), 'clement') self.assertEqual(child.get_int('age'), -2)
for p in range(params): usage+=' --param_'+str(p)+' val_'+str(p) parser=OptionParser(usage) # register options for p in range(params): parser.add_option('--param_'+str(p),type=float) # parse the command line arguments (options,args)=parser.parse_args() # make device database device_database=PropertyTree() device_database.parse_xml('super_capacitor.xml') # adjust the parameters in the database options_dict=vars(options) for var in options_dict: path=uq_database.get_string('uq.'+var+'.name') # next line is there to ensure that path already exists old_value=device_database.get_double(path) new_value=options_dict[var] print var,path,new_value device_database.put_double(path,new_value) # build the energy storage device device=EnergyStorageDevice(device_database.get_child('device')) # parse the electrochmical impedance spectroscopy database eis_database=PropertyTree() eis_database.parse_xml('eis.xml') # measure the impedance impedance_spectrum_data=measure_impedance_spectrum(device,eis_database.get_child('eis')) # extract the results frequency=impedance_spectrum_data['frequency']
for p in range(params): usage += ' --param_' + str(p) + ' val_' + str(p) parser = OptionParser(usage) # register options for p in range(params): parser.add_option('--param_' + str(p), type=float) # parse the command line arguments (options, args) = parser.parse_args() # make device database device_database = PropertyTree() device_database.parse_xml('super_capacitor.xml') # adjust the parameters in the database options_dict = vars(options) for var in options_dict: path = uq_database.get_string('uq.' + var + '.name') # next line is there to ensure that path already exists old_value = device_database.get_double(path) new_value = options_dict[var] print var, path, new_value device_database.put_double(path, new_value) # build the energy storage device device = EnergyStorageDevice(device_database.get_child('device')) # parse the electrochmical impedance spectroscopy database eis_database = PropertyTree() eis_database.parse_xml('eis.xml') # measure the impedance impedance_spectrum_data = measure_impedance_spectrum( device, eis_database.get_child('eis')) # extract the results