TagExpression#
Module providing routines to check and convert between tag expressions. This simplfies the handling of list of files from using regular expressions to tag expression.
A tag is a simple placeholder for a string or number in a file name. An expression is a filename with any number of tags.
- A tag has the format <Name,Type,Width>.
Name : str that specifies the tag name
Type : ‘I’ or ‘S’ for integer or string, optional and defaults to ‘I’
Width : int, if given indicates a fixed width and missing digist or chars are replaced by trailing zeros or spaces.
The module also provides functions to infer the tag expressions from a list of
file names via the function detect()
.
Expressions can also be converted to glob or regular expressions.
Example
An example tag expression would be ‘file_<X,2>_<Y,3>.npy’ and would for example match the filename ‘file_01_013.npy’
>>> import ClearMap.Utils.TagExpression as te
>>> e = te.Expression('file_<X,2>_<Y,3>.npy')
>>> e.tag_names()
e.tag_names()
>>> e.glob()
'file_[0-9][0-9]_[0-9][0-9].npy'
>>> e.indices('file_01_013.npy')
[1, 13]
>>> e.re()
'file_(?P<X>\d{2})_(?P<Y>\d{3})\.npy'
>>> e.string({'X':1, 'Y':10})
'file_01_010.npy'
See also