parameterfile¶
Module that reads a parameter file and returns a dictionary with the variable names and values (all as strings)
The file format is as follows:
comments start with
#
in the first column :# My Comment
each line can have one or more name, value pairs, separated by semi colons
name1 = value1; name2 = value2
In the following example assume that value1
is a float:
>>> pf = parameterfile.pfile('my_file')
As an alternative the data can also be provided in a list of strings. This is useful if parameters are stored in a datafile or are provided from another source.
Example: the data are stored in the string array my_data:
>>> pf = parameterfile.pfile('', data = my_data)
Accessing the values of a parameters file:
>>> value1 = float(pf.data['name1'])
Values can also be retrieved using the get_value(name, dtype) function:
>>> value2 = pf.get_value('name2', var_type = float)
Possible data types (dtype) are:
float
int
parameterfile.Bool ( converts ASCII True, False to a python bool value True, False)
If var_type
keyword agrument is not used, the function tries to convert to float or bool. If both fails it
returns the string value.
Variable names are also called keys.
-
class
LT.parameterfile.
pfile
(filename, data=None)¶ Open a parameter file, read it and create a pfile object
example:
>>> pf=pfile('my_datafile')
-
Bool
(self, x)¶ convert ascii True and False to bool values. Any other value will result in False example:
>>> d.Bool('True')
returns:
>>> True
-
get_all_data
(self)¶ return a list of all data stored
-
get_data
(self, keylist)¶ return all data according to the key list which has the following format:
key1:key2:key3: …
-
get_value
(self, key, var_type=None)¶ return a value of a given data type for key.
>>> x = get_valye('my_var', var_type = float)
prints an error message and returns none if it cannot convert.
possible data types are: float, int, parameterfile.Bool
-
get_variable_names
(self)¶ return a list of keys
-
make_attr
(self)¶ Save values and names as attributes
-
name
(self)¶ print the filename associate with this instance
-
show_all_data
(self)¶ print all data and keys stored
-
show_variable_names
(self)¶ print a list of variable names in the dictionary
-
write_all
(self, fp)¶ write all parameter file data into a new file using the provided file pointer fp which has been obtained from the open statment:
>>> fp = open('new_file.par', 'w') >>> pf.write_all(fp)
-