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
are_rows_uniq(some_array)[source]
is_number(s)[source]
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

class Auto[source]

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.

__repr__()[source]
Returns:String Auto.
Return type:str
__str__()[source]

Calls __repr__().

create_tmpfile(ext=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.
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
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

Warning

Current implementation cannot be used with generators!

Todo

Current implementation is primitive and HAVE TO be replaced.

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
arrayify(gen)[source]

Decorator to convert functions’ outputs into a 2D 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 2D numpy array.
Return type:numpy.ndarray
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
split_list(l, s)[source]
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
strech_zip(*args)[source]
compress_zip(*args)[source]
zip_zip(*args, **kwargs)[source]
xzip_xzip(*args, **kwargs)[source]
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)

__init__(**kwds)[source]
class SmartRangeFunction(element, times)[source]

Bases: object

__init__(element, times)[source]
__repr__()[source]
get()[source]
rev()[source]
isin(element)[source]
class SmartRangeEqual(element, times)[source]

Bases: aquaduct.utils.helpers.SmartRangeFunction

type = ‘e’
get()[source]
rev()[source]
isin(element)[source]
class SmartRangeIncrement(element, times)[source]

Bases: aquaduct.utils.helpers.SmartRangeFunction

type = ‘i’
get()[source]
rev()[source]
isin(element)[source]
class SmartRangeDecrement(element, times)[source]

Bases: aquaduct.utils.helpers.SmartRangeFunction

type = ‘d’
get()[source]
rev()[source]
isin(element)[source]
class SmartRange(iterable=None)[source]

Bases: object

__init__(iterable=None)[source]
last_element()[source]
last_times()[source]
raw
append(element)[source]
get()[source]
rev()[source]
__len__()[source]
min()[source]
max()[source]
isin(element)[source]