- tangible.utils.connect_2d_shapes(shapes, layer_distance, orientation)¶
Convert a list of 2D shapes to a 3D shape.
Take a list of 2D shapes and create a 3D shape from it. Each layer is separated by the specified layer distance.
- shapes (Each shape in the list should be an AST object.) – List of shapes.
- layer_distance (int or float) – The distance between two layers.
- orientation (str or unicode) – Either ‘horizontal’ or ‘vertical’
Iterate over an iterable in pairs.
This is an implementation of a moving window over an iterable with 2 items. Each group in the resulting list contains 2 items. This means that the original iterable needs to contain at least 2 items, otherwise this function will return an empty list.
[1, 2, 3, 4] -> [(1, 2), (2, 3), (3, 4)]
Parameters: iterable (Any iterable type (e.g. a list or a tuple).) – An iterable containing at least 2 items. Returns: A generator returning pairwise items. Return type: itertools.izip
- tangible.utils.reduceby(iterable, keyfunc, reducefunc, init)¶
Combination of itertools.groupby() and reduce().
This generator iterates over the iterable. The values are reduced using reducefunc and init as long as keyfunc(item) returns the same value.
A possible use case would be to aggregate website visits and to group them by month. The corresponding SQL statement would be:
SELECT SUM(visit_count) FROM visits GROUP BY month;
>>> keyfunc = lambda x: x % 2 == 0 >>> reducefunc = lambda x, y: x + y >>> values = [1, 3, 5, 6, 8, 11] >>> groups = utils.reduceby(values, keyfunc, reducefunc, 0) >>> groups <generator object reduceby at 0xedc5a0> >>> list(groups) [9, 14, 11]
- iterable – An iterable to reduce. The iterable should be presorted.
- keyfunc – A key function. It should return the same value for all items belonging to the same group.
- reducefunc – The reduce function.
- init – The initial value for the reduction.
A generator returning the reduced groups.