示例#1
0
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
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
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
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)))