コード例 #1
0
ファイル: functional.py プロジェクト: rajeshvv/mom
def ichunks(iterable, size, *args, **kwargs):
    """Splits an iterable into iterators for chunks each of specified size.

  :param iterable:
      The iterable to split. Must be an ordered sequence to guarantee order.
  :param size:
      Chunk size.
  :param padding:
      If a pad value is specified appropriate multiples of it will be
      appended to the end of the iterator if the size is not an integral
      multiple of the length of the iterable:

          map(tuple, ichunks("aaabccd", 3, "-"))
          -> [("a", "a", "a"), ("b", "c", "c"), ("d", "-", "-")]

          map(tuple, ichunks("aaabccd", 3, None))
          -> [("a", "a", "a"), ("b", "c", "c"), ("d", None, None)]

      If no padding is specified, nothing will be appended if the
      chunk size is not an integral multiple of the length of the
      iterable. That is, the last chunk will have chunk size less than
      the specified chunk size. :yields: Generator of chunk iterators.
  """
    length = len(iterable)
    if args or kwargs:
        padding = kwargs["padding"] if kwargs else args[0]
        for i in builtins.range(0, length, size):
            yield itertools.islice(
                chain(iterable,
                      itertools.repeat(padding, (size - (length % size)))), i,
                i + size)
    else:
        for i in builtins.range(0, length, size):
            yield itertools.islice(iterable, i, i + size)
コード例 #2
0
ファイル: functional.py プロジェクト: gorakhargosh/mom
def ichunks(iterable, size, *args, **kwargs):
  """Splits an iterable into iterators for chunks each of specified size.

  :param iterable:
      The iterable to split. Must be an ordered sequence to guarantee order.
  :param size:
      Chunk size.
  :param padding:
      If a pad value is specified appropriate multiples of it will be
      appended to the end of the iterator if the size is not an integral
      multiple of the length of the iterable:

          map(tuple, ichunks("aaabccd", 3, "-"))
          -> [("a", "a", "a"), ("b", "c", "c"), ("d", "-", "-")]

          map(tuple, ichunks("aaabccd", 3, None))
          -> [("a", "a", "a"), ("b", "c", "c"), ("d", None, None)]

      If no padding is specified, nothing will be appended if the
      chunk size is not an integral multiple of the length of the
      iterable. That is, the last chunk will have chunk size less than
      the specified chunk size. :yields: Generator of chunk iterators.
  """
  length = len(iterable)
  if args or kwargs:
    padding = kwargs["padding"] if kwargs else args[0]
    for i in builtins.range(0, length, size):
      yield itertools.islice(
          chain(iterable,
                itertools.repeat(padding, (size - (length % size)))),
          i, i + size)
  else:
    for i in builtins.range(0, length, size):
      yield itertools.islice(iterable, i, i + size)
コード例 #3
0
ファイル: functional.py プロジェクト: rajeshvv/mom
def union(iterable, *iterables):
    """Returns the union of given iterable sequences.

  :param iterables:
      Variable number of input iterable sequences.
  :returns:
      Union of the iterable sequences.
  """

    if not iterables:
        return iterable

    return unique(iter(chain(iterable, *iterables)))
コード例 #4
0
ファイル: functional.py プロジェクト: gorakhargosh/mom
def union(iterable, *iterables):
  """Returns the union of given iterable sequences.

  :param iterables:
      Variable number of input iterable sequences.
  :returns:
      Union of the iterable sequences.
  """

  if not iterables:
    return iterable

  return unique(iter(chain(iterable, *iterables)))