"""
Tools for managing iterables
"""
from __future__ import division
from builtins import range
from past.utils import old_div
import numpy as np
from . import logger
Logger = logger.Logger
[docs]def slicer(list_to_slice, slices):
"""
*Generator* Slice a list in individual slices
Args:
list_to_slice (list): a list of items
slices (int): how many lists will be sliced of
Returns:
list: slices of the given list
"""
#FIXME: there must be something in the std lib..
# implementing this because I am flying anyway
# right now and have nothing to do..
if slices == 0:
slices = 1 # prevent ZeroDivisionError
maxslice = old_div(len(list_to_slice),slices)
if (maxslice*slices) < len(list_to_slice) :
maxslice += 1
Logger.info("Sliced list in {} slices with a maximum slice index of {}" .format(slices,maxslice))
for index in range(0,slices):
lower_bound = index*maxslice
upper_bound = lower_bound + maxslice
thisslice = list_to_slice[lower_bound:upper_bound]
yield thisslice
#######################################################################
[docs]def flatten(iterable_of_iterables):
"""
Concat an iterable of iterables in a single iterable, chaining its elements
Args:
iterable_of_iterables (iterable): Currently supported is dimensionality of 2
Returns:
np.ndarray
"""
newarray = np.array([])
for k in iterable_of_iterables:
newarray = np.hstack((newarray,np.asarray(k)))
return newarray