aquaduct.utils.helpers module

Collection of helpers - functions and decorators.

combine(seqin)[source]

This is an alien function. It is not extensively used.

Directly taken form http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478/index_txt

Returns a list of all combinations of argument sequences. For example, following call:

combine(((1,2),(3,4)))

gives following list of combinations:

[[1, 3], [1, 4], [2, 3], [2, 4]]
Parameters

seqin (tuple) – Tuple of sequences to combine.

Returns

All possible combinations of all input sequences.

Return type

list of lists

lind(l, ind)[source]

Indexes lists using lists of integers as identificators. For example:

lind(['a','b','c','d','e'],[1,4,2])

returns:

['b', 'e', 'c']
Parameters
  • l (list) – List to be indexed.

  • ind (list) – Integer indexes.

Returns

Reindexed list.

Return type

list

glind(l, ind)[source]

Indexes lists using iterable of integers as identificators. For example:

glind(['a','b','c','d','e'],[1,4,2])

returns:

['b', 'e', 'c']
Parameters
  • l (list) – List to be indexed.

  • ind (list) – Integer indexes.

Returns

Reindexed list.

Return type

list

class Auto[source]

Bases: object

Auto type definition. The class is used as an alternative value for options (if particular option supports it). If options (or variables/parameters etc.) have value of Auto it means that an automatic process for parametrization should be performed.

For example, if the input parameter is set to Auto it is supposed that its value is calculated on the basis of input data or other parameters.

create_tmpfile(ext=None, dir=None)[source]

Creates temporary file. File is created, closed and its file name is returned.

Note

It is responsibility of the caller to delete the file.

Parameters
  • ext (str) – Optional extension of the file.

  • dir (str) – Optional path to the directory where the file should be created.

Returns

File name of created temporary file.

Return type

str

range2int(r, uniq=True)[source]

Transforms a string range in to a list of integers (with added missing elements from given ranges).

For example, a following string:

'0:2 4:5 7 9'

is transformed into:

[0,1,2,4,5,7,9]
Parameters
  • r (str) – String of input range.

  • uniq (bool) – Optional parameter, if set to True only unique and sorted integers are returned.

Returns

List of integers.

Return type

list of int

int2range(l)[source]

Transforms a list of integers in to a string of ranges.

For example, a following list:

[0,1,2,4,5,7,9]

is transformed into:

0:2 4:5 7 9
Parameters

l (list) – input list of int

Returns

String of ranges.

Return type

str

fractionof(l, f=None)[source]
Parameters
  • l (list) – input list

  • f (float) – fraction

Returns

Fraction of input list.

Return type

generator

make_fraction(frac, size)[source]
Parameters

frac (float) – Fraction to make, can be also None. In that case, returned value is None as well.

Size int size

Size of set for which fraction is made.

Returns

Fracion in range (0,1).

make_fractionof(l, f=None)[source]

Wrapper of make_fraction and fractionof functions.

chop(l, n=1)[source]
Parameters

l (list) – input list

Returns

Chunks of input list, each chunk is of maximal size of n.

Return type

generator

chunk(l, n=1)[source]
Parameters

l (list) – input list

Returns

n chunks of input list.

Return type

generator

is_iterable(l)[source]

Checks if provided object is iterable. Returns True is it is iterable, otherwise returns False.

Parameters

l (list) – input object

Returns

True if submitted object is iterable otherwise returns False.

Return type

bool

sortify(gen)[source]

Decorator to convert functions’ outputs into a sorted list. If the output is iterable it is converted in to a list of appropriate length. If the output is not iterable it is converted in to a list of length 1.

Written on the basis of listify().

Returns

Output of decorated function converted to a sorted list.

Return type

list

uniqify(gen)[source]

Decorator to convert functions’ outputs into a sorted list of unique objects. If the output is iterable it is converted in to a list of appropriate length. If the output is not iterable it is converted in to a list of length 1.

Written on the basis of listify().

Returns

Output of decorated function converted to a sorted list of unique objects.

Return type

list

listify(gen)[source]

Decorator to convert functions’ outputs into a list. If the output is iterable it is converted in to a list of appropriate length. If the output is not iterable it is converted in to a list of length 1.

This function was copied from:

http://argandgahandapandpa.wordpress.com/2009/03/29/python-generator-to-list-decorator/

and further improved by tljm@wp.pl.

Returns

Output of decorated function converted to a list.

Return type

list

tupleify(gen)[source]

Decorator to convert functions’ outputs into a tuple. If the output is iterable it is converted in to a tuple of apropriate length. If the output is not iterable it is converted in to a tuple of length 1.

Written on the basis of listify().

Returns

Output of decorated function converted to a tuple.

Return type

tuple

dictify(gen)[source]

Decorator to convert functions’ outputs into a tuple. If the output is iterable it is converted in to a tuple of apropriate length. If the output is not iterable it is converted in to a tuple of length 1.

Written on the basis of listify().

Returns

Output of decorated function converted to a tuple.

Return type

tuple

arrayify1(gen)[source]

Decorator to convert functions’ outputs into a 1D numpy array. If the output is iterable it is converted in to a 2D numpy array of appropriate shape. If the output is not iterable it is converted in to a 2D numpy array of shape 1x1.

Written on the basis of listify().

Returns

Output of decorated function converted to a 1D numpy array.

Return type

numpy.ndarray

list_blocks_to_slices(l)[source]

Slices list in to block according to its elements identity. Resulting slices correspond to blocks of identical elements.

Parameters

l (list) – List of any objects.

Returns

Generator of slices.

Return type

generator

what2what(what, towhat)[source]

This function search if elements of the one list (:attr: ‘what’) are present in the other list (:attr: ‘towhat’) and returns indices of elements form :attr:’what’ list as a tuple. If elements from the first list are not present in the second list the tuple is empty. :param list what: Input list for which indices of elements present in towhat are returned. :param list towhat: List of elements which input list is indexed to. :return: Indices of what list that are present in towhat list. :rtype: tuple

make_iterable(something)[source]

If input object is not iterable returns it as one element list. Otherwise returns the object.

Parameters

something (object) – Input object.

Returns

Iterable object.

Return type

iterable or list

concatenate(*args)[source]

Concatenates input iterable arguments in to one generator.

class Bunch(**kwds)[source]

Bases: object

http://code.activestate.com/recipes/52308 foo=Bunch(a=1,b=2)

class SmartRangeFunction(element, times)[source]

Bases: object

Base class for all SmartRangeFunction based classes.

class SmartRangeEqual(element, times)[source]

Bases: aquaduct.utils.helpers.SmartRangeFunction

class SmartRangeIncrement(element, times)[source]

Bases: aquaduct.utils.helpers.SmartRangeFunction

class SmartRangeDecrement(element, times)[source]

Bases: aquaduct.utils.helpers.SmartRangeFunction