예제 #1
0
    def _parse_args(self, args, engine, error_message):
        if len(args) == 0:
            raise TypeError(error_message.format(len(args)))
        if len(args) == 1:
            try:
                if type(args[0]).__name__ == 'DataFrame':
                    import pandas
                    if isinstance(args[0], pandas.DataFrame):
                        return Sequence(args[0].values,
                                        engine=engine,
                                        max_repr_items=self.max_repr_items)
            except ImportError:  # pragma: no cover
                pass

        if len(args) > 1:
            return Sequence(list(args),
                            engine=engine,
                            max_repr_items=self.max_repr_items)
        elif is_primitive(args[0]):
            return Sequence([args[0]],
                            engine=engine,
                            max_repr_items=self.max_repr_items)
        else:
            return Sequence(args[0],
                            engine=engine,
                            max_repr_items=self.max_repr_items)
예제 #2
0
    def __call__(self, *args):
        """
        Create a Sequence using a sequential ExecutionEngine.

        If args has more than one argument then the argument list becomes the sequence.

        If args[0] is primitive, a sequence wrapping it is created.

        If args[0] is a list, tuple, iterable, or Sequence it is wrapped as a Sequence.

        :param args: Sequence to wrap
        :return: Wrapped sequence
        """
        # pylint: disable=no-self-use
        engine = ExecutionEngine()
        if len(args) == 0:
            raise TypeError(
                "seq() takes at least 1 argument ({0} given)".format(
                    len(args)))
        elif len(args) > 1:
            return Sequence(list(args), engine=engine)
        elif is_primitive(args[0]):
            return Sequence([args[0]], engine=engine)
        else:
            return Sequence(args[0], engine=engine)
예제 #3
0
    def __call__(self, *args, **kwargs):
        """
        Create a Sequence using a parallel ExecutionEngine.

        If args has more than one argument then the argument list becomes the sequence.

        If args[0] is primitive, a sequence wrapping it is created.

        If args[0] is a list, tuple, iterable, or Sequence it is wrapped as a Sequence.

        :param args: Sequence to wrap
        :return: Wrapped sequence
        """
        processes = kwargs.get('processes') or self.processes
        partition_size = kwargs.get('partition_size') or self.partition_size
        engine = ParallelExecutionEngine(processes=processes,
                                         partition_size=partition_size)
        if len(args) == 0:
            raise TypeError(
                'pseq() takes at least 1 argument ({0} given)'.format(
                    len(args)))
        elif len(args) > 1:
            return Sequence(list(args), engine=engine)
        elif is_primitive(args[0]):
            return Sequence([args[0]], engine=engine)
        else:
            return Sequence(args[0], engine=engine)
예제 #4
0
def seq(*args):
    """
    Primary entrypoint for the functional package. Returns a functional.pipeline.Sequence wrapping
    the original sequence.

    Additionally it parses various types of input to a Sequence as best it can.

    >>> seq([1, 2, 3])
    [1, 2, 3]

    >>> seq(1, 2, 3)
    [1, 2, 3]

    >>> seq(1)
    [1]

    >>> seq(range(4))
    [0, 1, 2, 3]

    >>> type(seq([1, 2]))
    functional.pipeline.Sequence

    >>> type(Sequence([1, 2]))
    functional.pipeline.Sequence

    :param args: Three types of arguments are valid.
        1) Iterable which is then directly wrapped as a Sequence
        2) A list of arguments is converted to a Sequence
        3) A single non-iterable is converted to a single element Sequence
    :return: wrapped sequence

    """
    if len(args) == 0:
        raise TypeError("seq() takes at least 1 argument ({0} given)".format(
            len(args)))
    elif len(args) > 1:
        return Sequence(list(args))
    elif is_primitive(args[0]):
        return Sequence([args[0]])
    else:
        return Sequence(args[0])
def Seq(listlike):
    # type: (...) -> Sequence
    """
    indirection to the (unconditional) wrapping of a sequence-like python builtin via the functional streams API.
    """
    return Sequence(listlike)
예제 #6
0
 def test_constructor(self):
     self.assertRaises(TypeError, lambda: Sequence(1))