FileTools

About the FileTools class

The WaveBlocks Project

@author: R. Bourquin @copyright: Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 R. Bourquin @license: Modified BSD License

Class documentation

The WaveBlocks Project

This file contains various functions for finding and retrieving the files that contain parameter settings and simulation results. Note: The terms ‘path’ and ‘ID’ are used as synonyms here. Each simulation ID is just the base name of the path or the configuration file.

@author: R. Bourquin @copyright: Copyright (C) 2010, 2011, 2012, 2013, 2014, 2016 R. Bourquin @license: Modified BSD License

FileTools.compare_by(namea, nameb, pattern, ldel='[', mdel='=', rdel=']', as_string=True)[source]

Compare two simulation IDs with respect to a (numerical) value in the ID.

Parameters:
  • namea – The first name in the comparison.
  • nameb – The second name in the comparison.
  • pattern – The pattern whose (numerical) value is used for sorting.
  • ldel – Left delimiter of the pattern.
  • mdel – Middle delimiter of the pattern.
  • rdel – Right delimiter of the pattern.
  • as_string – Determines if the values for pattern get converted to floats.
Returns:

A boolean answer if the IDs are the same w.r.t the pattern.

FileTools.exclude(lista, listb)[source]

Remove from a list of simulation IDs another such list of IDs.

Parameters:
  • lista – A first list with the simulation IDs.
  • listb – A second list with the simulation IDs.
Returns:

A list of all simulation IDs present in lista but not in listb.

FileTools.gather_all(stringlist, pattern)[source]

Collects all simulation IDs which contain a specific pattern from a given list.

Parameters:
  • stringlist – A list with the simulation IDs.
  • pattern – The pattern.
Returns:

A list of simulation IDs that contain the given pattern.

FileTools.get_by_item(stringlist, item)[source]

Get a filtered list of simulation IDs containing a given key=value item.

Parameters:
  • stringlist – A list with the simulation IDs.
  • item – The key=value item to search for.
Returns:

A list of simulation IDs.

FileTools.get_by_value(stringlist, pattern, value, ldel='[', mdel='=', rdel=']')[source]

Get a filtered list of simulation IDs by specifying the key and value of some key=value pair.

Parameters:
  • stringlist – A list with the simulation IDs.
  • pattern – The pattern that is used for key.
  • value – The value that is used for value.
  • ldel – Left delimiter of the pattern.
  • mdel – Middle delimiter of the pattern.
  • rdel – Right delimiter of the pattern.
Returns:

A list of IDs that contain a key with given value.

FileTools.get_item(name, pattern, ldel='[', mdel='=', rdel=']', unpack=True)[source]

Get a single key=value item out of the name. The pattern specifies the key part.

Parameters:
  • name – The name from which to get the item.
  • pattern – The pattern whose value is used for key.
  • unpack (Boolean, default is True.) – Whether to unpack a single unique result instead of returning it inside a list.
Returns:

A (list of the) item(s) whose key part matches pattern.

FileTools.get_items(name, ldel='[', mdel='=', rdel=']')[source]

Get a list of all the key=value items in the name.

Parameters:name – The name from which to get the items.
Returns:A list of all key=value items present in the name.
FileTools.get_max_by(stringlist, pattern, ldel='[', mdel='=', rdel=']', as_string=False)[source]

Get the maximum of a list with simulation IDs with respect to a (numerical) value in the ID. This is just a simple convenience function so that the user needs not to remember if the sort order is ascending or descending which plays no role for iteration.

Parameters:
  • stringlist – A list with the simulation IDs.
  • pattern – The pattern whose (numerical) value is used for sorting.
  • ldel – Left delimiter of the pattern.
  • mdel – Middle delimiter of the pattern.
  • rdel – Right delimiter of the pattern.
  • as_string – Determines if the values for pattern get converted to floats.
Returns:

A sorted list of simulation IDs.

FileTools.get_min_by(stringlist, pattern, ldel='[', mdel='=', rdel=']', as_string=False)[source]

Get the minimum of a list with simulation IDs with respect to a (numerical) value in the ID. This is just a simple convenience function so that the user needs not to remember if the sort order is ascending or descending which plays no role for iteration.

Parameters:
  • stringlist – A list with the simulation IDs.
  • pattern – The pattern whose (numerical) value is used for sorting.
  • ldel – Left delimiter of the pattern.
  • mdel – Middle delimiter of the pattern.
  • rdel – Right delimiter of the pattern.
  • as_string – Determines if the values for pattern get converted to floats.
Returns:

A sorted list of simulation IDs.

FileTools.get_number_simulations(path)[source]

Get the number of simulations at hand below the given path.

Parameters:path – The path under which we search for a output file.
Returns:The number of simulations result directories.
FileTools.get_parameters_file(path, unpack=True)[source]

Search for a configuration file containing the simulation parameters under a given path. Note that in case there are more than one .py file under the given path we return a list of all.

Parameters:
  • path – The path under which we search for a configuration file.
  • unpack (Boolean, default is True.) – Whether to unpack a single unique result instead of returning it inside a list.
Returns:

The path (file name) of the configuration file.

FileTools.get_result_dirs(path)[source]

Lists all simulations (IDs) that can be found under the given path.

Parameters:path – The file system path under which we search for simulations.
Returns:A list of simulation IDs.
FileTools.get_results_file(path, fileext='.hdf5', unpack=True)[source]

Search for a file containing the simulation results under a given path. Note that in case there are more than one .hdf5 file under the given path we return a list of all.

Parameters:
  • path – The path under which we search for a output file.
  • unpack (Boolean, default is True.) – Whether to unpack a single unique result instead of returning it inside a list.
Returns:

The path (file name) of the output file.

FileTools.get_value(item, ldel='[', mdel='=', rdel=']')[source]

Get the value part of a given key=value pair.

Parameters:
  • item – The key=value item.
  • ldel – Left delimiter of the pattern.
  • mdel – Middle delimiter of the pattern.
  • rdel – Right delimiter of the pattern.
Returns:

The value part of the given item.

FileTools.get_value_of(name, pattern, ldel='[', mdel='=', rdel=']', unpack=True)[source]

Get the value part corresponding to a given key of an item.

Parameters:
  • name – The name from which to get the value.
  • pattern – The pattern whose value is used for key.
  • ldel – Left delimiter of the pattern.
  • mdel – Middle delimiter of the pattern.
  • rdel – Right delimiter of the pattern.
  • unpack (Boolean, default is True.) – Whether to unpack a single unique result instead of returning it inside a list.
Returns:

The value part of the found item. (If there are multiple matching items, return all values.)

FileTools.group_by(stringlist, pattern, ldel='[', mdel='=', rdel=']', as_string=True)[source]

Groups simulation IDs with respect to a pattern.

Parameters:
  • stringlist – A list with the simulation IDs.
  • pattern – The pattern used for grouping.
  • ldel – Left delimiter of the pattern.
  • mdel – Middle delimiter of the pattern.
  • rdel – Right delimiter of the pattern.
  • as_string – Determines if the values for pattern get converted to floats. Not used here.
Returns:

A list of groups of simulation IDs.

FileTools.intersect_by(lista, listb, pattern, ldel='[', mdel='=', rdel=']', as_string=True)[source]

Find the intersection of two lists containing simulation IDs.

Parameters:
  • lista – A first list with the simulation IDs.
  • listb – A second list with the simulation IDs.
  • pattern – The pattern whose numerical value is used for sorting.
  • ldel – Left delimiter of the pattern.
  • mdel – Middle delimiter of the pattern.
  • rdel – Right delimiter of the pattern.
  • as_string – Determines if the values for pattern get converted to floats.
Returns:

A sorted list of simulation IDs.

FileTools.name_contains(name, pattern)[source]

Checks if a simulation ID contains a given pattern.

Parameters:
  • name – The full simulation ID.
  • pattern – The pattern in question.
Returns:

A boolean answer.

FileTools.sort_and_group_by(stringlist, pattern, ldel='[', mdel='=', rdel=']', as_string=False)[source]

Sorts and groups simulation IDs with respect to a pattern.

Parameters:
  • stringlist – A list with the simulation IDs.
  • pattern – The pattern used for sorting and grouping.
  • ldel – Left delimiter of the pattern.
  • mdel – Middle delimiter of the pattern.
  • rdel – Right delimiter of the pattern.
  • as_string – Determines if the values for pattern get converted to floats. Not used here.
Returns:

A list of groups of simulation IDs.

FileTools.sort_by(stringlist, pattern, ldel='[', mdel='=', rdel=']', as_string=False)[source]

Sorts simulation IDs with respect to a (numerical) value in the ID.

Parameters:
  • stringlist – A list with the simulation IDs.
  • pattern – The pattern whose (numerical) value is used for sorting.
  • ldel – Left delimiter of the pattern.
  • mdel – Middle delimiter of the pattern.
  • rdel – Right delimiter of the pattern.
  • as_string – Determines if the values for pattern get converted to floats.
Returns:

A sorted list of simulation IDs.

FileTools.split_list(alist, n)[source]

Split a list into n parts.

Parameters:
  • alist – The list to split.
  • n – The number of parts.
Returns:

A list containing the pieces of the original list.

Note

The pieces are not continuous sublists of the given list.