5.1.4.6. gromacs.fileformats.convert — converting entries of tables
The Autoconverter converts input values to appropriate Python
types.
It is mainly used by gromacs.fileformats.xpm.XPM
to automagically generate useful NumPy arrays from xpm files. Custom
conversions beyond the default ones in Autoconverter can be
provided with the constructor keyword mapping.
See also
The Autoconverter class was taken and slightly adapted from
recsql.converter in RecSQL.
- class gromacs.fileformats.convert.Autoconverter(mode='fancy', mapping=None, active=True, sep=False, **kwargs)[source]
Automatically convert an input value to a special python object.
The
Autoconverter.convert()method turns the value into a special python value and casts strings to the “best” type (seebesttype()).The defaults for the conversion of a input field value to a special python value are:
value
python
‘
---’None‘’
None‘True’
True‘x’
True‘X’
True‘yes’
True‘Present’
True‘False’
False‘-’
False‘no’
False‘None’
False‘none’
FalseIf the sep keyword is set to a string instead of
Falsethen values are split into tuples. Probably the most convenient way to use this is to set sep =True(orNone) because this splits on all white space whereas sep = ‘ ‘ would split multiple spaces.Example
With sep =
True: ‘foo bar 22 boing---’ –> (‘foo’, ‘bar’, 22, ‘boing’, None)With sep = ‘,’: 1,2,3,4 –> (1,2,3,4)
Initialize the converter.
- Arguments:
- mode
defines what the converter does
- “simple”
convert entries with
besttype()- “singlet”
convert entries with
besttype()and apply mappings- “fancy”
first splits fields into lists, tries mappings, and does the stuff that “singlet” does
- “unicode”
convert all entries with
to_unicode()
- mapping
any dict-like mapping that supports lookup. If``None`` then the hard-coded defaults are used
- active or autoconvert
initial state of the
Autoconverter.activetoggle.Falsedeactivates any conversion. [True]- sep
character to split on (produces lists); use
TrueorNone(!) to split on all white space.
Changed in version 0.7.0: removed encoding keyword argument
- convert(x)
Convert x (if in the active state)
- active
If set to
Truethen conversion takes place;Falsejust returnsbesttype()applid to the value.
- property active
Toggle the state of the Autoconverter.
Trueuses the mode,Falsedoes nothing
- gromacs.fileformats.convert.besttype(x)[source]
Convert string x to the most useful type, i.e. int, float or unicode string.
If x is a quoted string (single or double quotes) then the quotes are stripped and the enclosed string returned.
Note
Strings will be returned as Unicode strings (using
to_unicode()).Changed in version 0.7.0: removed encoding keyword argument