footprints.util

Utility functions of the footprints package.

Functions

footprints.util.expand(desc)[source]

Expand the given description according to iterable or expandable arguments.

List expansion:

>>> expand({'test': 'alpha'}) == [{'test': 'alpha', 'index_expansion': 1}]
True

>>> (expand({ 'test': 'alpha', 'niv2': [ 'a', 'b', 'c' ]}) ==
...  [{'test': 'alpha', 'niv2': 'a', 'index_expansion': 1},
...   {'test': 'alpha', 'niv2': 'b', 'index_expansion': 2},
...   {'test': 'alpha', 'niv2': 'c', 'index_expansion': 3}])
True

>>> (expand({'test': 'alpha', 'niv2': 'x,y,z'}) ==
...  [{'test': 'alpha', 'niv2': 'x', 'index_expansion': 1},
...   {'test': 'alpha', 'niv2': 'y', 'index_expansion': 2},
...   {'test': 'alpha', 'niv2': 'z', 'index_expansion': 3}])
True

>>> (expand({'test': 'alpha', 'niv2': 'range(1,3)'}) ==
...  [{'test': 'alpha', 'niv2': 1, 'index_expansion': 1},
...   {'test': 'alpha', 'niv2': 2, 'index_expansion': 2},
...   {'test': 'alpha', 'niv2': 3, 'index_expansion': 3}])
True
>>> (expand({'test': 'alpha', 'niv2': 'range(0,6,3)'}) ==
...  [{'test': u'alpha', 'niv2': 0, 'index_expansion': 1},
...   {'test': 'alpha', 'niv2': 3, 'index_expansion': 2},
...   {'test': 'alpha', 'niv2': 6, 'index_expansion': 3}])
True

List expansion + dictionary matching:

>>> (expand({'test': 'alpha', 'niv2': ['x', 'y'], 'niv3': {'niv2': {'x': 'niv2 is x', 'y': 'niv2 is y'}}}) ==
...  [{'test': 'alpha', 'niv3': 'niv2 is x', 'niv2': 'x', 'index_expansion': 1},
...   {'test': 'alpha', 'niv3': 'niv2 is y', 'niv2': 'y', 'index_expansion': 2}])
True

Globbing:

>>> # Let's assume that the following files are present in the current working directory: 
... # - testfile_abc_1
... # - testfile_abc_2
... # - testfile_def_2
... # - testfile_def_3
... # - testfile_a_trap
>>> expand({'fname': r'testfile_{glob:i:\w+}_{glob:n:\d+}', 'id':'[glob:i]', 'n':'[glob:n]'}) 
[{'id': 'abc', 'fname': 'testfile_abc_1', 'n': '1', 'index_expansion': 1},
 {'id': 'abc', 'fname': 'testfile_abc_2', 'n': '2', 'index_expansion': 2},
 {'id': 'def', 'fname': 'testfile_def_2', 'n': '2', 'index_expansion': 3},
 {'id': 'def', 'fname': 'testfile_def_3', 'n': '3', 'index_expansion': 4}
 ]

Explanation: The files currently in the working directory are matched using regular expressions. If the filename matches, some matching parts may be re-used to fill other keys in the dictionary.

footprints.util.inplace(desc, key, value, globs=None, globalindex=None)[source]

Redefine the key value in a deep copy of the description desc.

Examples:

>>> (inplace({'test':'alpha'}, 'ajout', 'beta') ==
...  {'test': 'alpha', 'ajout': 'beta'})
True

>>> (inplace({'test':'alpha', 'recurs':{'a':1, 'b':2}}, 'ajout', 'beta') ==
...  {'test': 'alpha', 'ajout': 'beta', 'recurs': {'a': 1, 'b': 2}})
True
footprints.util.list2dict(a, klist)[source]

Reshape any entry of a specified in klist as a dictionary of the iterable contents of these entries.

Classes

class footprints.util.FoxyFormatter[source]

Bases: Formatter

A string formatter that may try to call an argument-less method.

get_field(field_name, args, kwargs)[source]

Given a field_name, find the object it references.