Esempio n. 1
0
    def testTypes(self):
        """Test that types are correctly derived and enforced."""
        with self.assertRaises(TypeError):
            repeated.repeated(1, "foo")

        with self.assertRaises(TypeError):
            repeated.meld(1, "foo")
Esempio n. 2
0
    def testTypes(self):
        """Test that types are correctly derived and enforced."""
        with self.assertRaises(TypeError):
            repeated.repeated(1, "foo")

        with self.assertRaises(TypeError):
            repeated.meld(1, "foo")
Esempio n. 3
0
    def testMaterialize(self):
        self.__generator_has_run = False

        def _gen():
            if self.__generator_has_run:
                raise ValueError("This should only run once.")

            self.__generator_has_run = True
            yield 1
            yield 2
            yield 3

        lazyseq = repeated.lazy(_gen)
        self.assertValuesEqual(lazyseq, repeated.repeated(1, 2, 3))

        # Accessing this for a second time should blow up.
        with self.assertRaises(ValueError):
            core.Materialize()(lazyseq)

        # So let's reset and do this with a materialized seq.
        self.__generator_has_run = False
        materialized = core.Materialize()(lazyseq)
        self.assertEqual(materialized, repeated.repeated(1, 2, 3))

        # And a second time.
        self.assertEqual(materialized, repeated.repeated(1, 2, 3))
Esempio n. 4
0
File: core.py Progetto: Onager/dotty
    def testMaterialize(self):
        self.__generator_has_run = False

        def _gen():
            if self.__generator_has_run:
                raise ValueError("This should only run once.")

            self.__generator_has_run = True
            yield 1
            yield 2
            yield 3

        lazyseq = repeated.lazy(_gen)
        self.assertValuesEqual(lazyseq, repeated.repeated(1, 2, 3))

        # Accessing this for a second time should blow up.
        with self.assertRaises(ValueError):
            core.Materialize()(lazyseq)

        # So let's reset and do this with a materialized seq.
        self.__generator_has_run = False
        materialized = core.Materialize()(lazyseq)
        self.assertEqual(materialized, repeated.repeated(1, 2, 3))

        # And a second time.
        self.assertEqual(materialized, repeated.repeated(1, 2, 3))
Esempio n. 5
0
    def testApplication(self):
        """Test function application across values."""
        self.assertEqual(
            repeated.repeated(2, 4),
            repeated.value_apply(repeated.repeated(1, 2), lambda x: x * 2))

        # As everything working on values, this should also work on scalars.
        applied = repeated.value_apply(5, lambda x: x * 2)
        self.assertValueEq(10, applied)
Esempio n. 6
0
    def testApplication(self):
        """Test function application across values."""
        self.assertEqual(
            repeated.repeated(2, 4),
            repeated.value_apply(
                repeated.repeated(1, 2),
                lambda x: x * 2))

        # As everything working on values, this should also work on scalars.
        applied = repeated.value_apply(5, lambda x: x * 2)
        self.assertValueEq(10, applied)
Esempio n. 7
0
    def testSuperpositions(self):
        """Superpositions can be manipulated using IRepeated."""
        s = superposition.superposition("foo", "bar")
        r = repeated.repeated("foo", "bar")

        # Comparison should work in both directions.
        self.assertValueEq(r, s)
        self.assertValueEq(s, r)

        # Application should preserve container type.
        s = superposition.superposition(1, 2)
        applied = repeated.value_apply(s, lambda x: x * 2)
        self.assertValueEq(applied, repeated.repeated(2, 4))
        self.assertIsInstance(applied, superposition.ISuperposition)
Esempio n. 8
0
    def _generate_rows(self, args, kwargs):
        # instantiate the plugin with arguments.
        self.plugin_obj = self.plugin_cls(session=self.session,
                                          *args,
                                          **kwargs)
        output_header = getattr(self.plugin_cls, "table_header", None)
        collector = getattr(self.plugin_obj, "collect_as_dicts", None)

        if callable(collector) and output_header is not None:
            # The plugin supports the collect API and declares its output ahead
            # of time. This is the ideal case.
            self.columns = output_header
            return repeated.lazy(
                lambda: self._order_columns(output_header, collector))

        else:
            # TODO: Should we not support these kind of plugins?

            # We don't know enough about the plugin to do the easy thing. We
            # need to create a shim renderer that will cache the plugin output
            # and then use that.
            renderer = identity_renderer.IdentityRenderer(session=self.session)
            with renderer.start():
                self.session.RunPlugin(self.plugin_cls.name,
                                       format=renderer,
                                       *args,
                                       **kwargs)

            # The identity renderer will now contain the plugin output and
            # columns.
            self.columns = renderer.columns
            return repeated.repeated(*list(renderer.rows))
Esempio n. 9
0
    def apply(self, args, kwargs):
        """Instantiate the plugin with given args and run it.

        This caches the output of the plugin. Subsequently, table_header,
        rows and columns will be populated.

        The CommmandWrapper must not be applied twice with different
        arguments - each instance represents a unique application.

        Arguments:
            args, kwargs: Arguments to the plugin.
        """
        if self._applied_args is not None:
            # Called before. Return what we cached.
            if self._applied_args != (args, kwargs):
                raise ValueError(
                    "%r was previously called with %r but is now being called"
                    " with %r. This should never happen." %
                    (self, self._applied_args, (args, kwargs)))

            return self.rows

        kwargs = kwargs.copy()
        kwargs.pop("vars", None)
        self._applied_args = (args, kwargs)

        # First time - instantiate the plugin with arguments.
        plugin_curry = getattr(self.session.plugins, self.plugin_cls.name)
        self.plugin_obj = plugin_curry(session=self.session, *args, **kwargs)

        output_header = getattr(self.plugin_cls, "table_header", None)
        collector = getattr(self.plugin_obj, "collect_as_dicts", None)

        if callable(collector) and output_header is not None:
            # The plugin supports the collect API and declares its output ahead
            # of time. This is the ideal case.
            self.columns = output_header
            self.rows = repeated.lazy(collector)
        else:
            # We don't know enough about the plugin to do the easy thing. We
            # need to create a shim renderer that will cache the plugin output
            # and then use that.
            renderer = identity_renderer.IdentityRenderer(session=self.session)
            with renderer.start():
                self.session.RunPlugin(self.plugin_cls.name,
                                       format=renderer,
                                       *args,
                                       **kwargs)

            # The identity renderer will now contain the plugin output and
            # columns.
            self.columns = renderer.columns
            self.rows = repeated.repeated(*list(renderer.rows))

        return self.rows
Esempio n. 10
0
    def apply(self, args, kwargs):
        """Instantiate the plugin with given args and run it.

        This caches the output of the plugin. Subsequently, table_header,
        rows and columns will be populated.

        The CommmandWrapper must not be applied twice with different
        arguments - each instance represents a unique application.

        Arguments:
            args, kwargs: Arguments to the plugin.
        """
        if self._applied_args is not None:
            # Called before. Return what we cached.
            if self._applied_args != (args, kwargs):
                raise ValueError(
                    "%r was previously called with %r but is now being called"
                    " with %r. This should never happen."
                    % (self, self._applied_args, (args, kwargs)))

            return self.rows

        kwargs = kwargs.copy()
        kwargs.pop("vars", None)
        self._applied_args = (args, kwargs)

        # First time - instantiate the plugin with arguments.
        plugin_curry = getattr(self.session.plugins, self.plugin_cls.name)
        self.plugin_obj = plugin_curry(session=self.session,
                                       *args, **kwargs)

        output_header = getattr(self.plugin_cls, "table_header", None)
        collector = getattr(self.plugin_obj, "collect_as_dicts", None)

        if callable(collector) and output_header is not None:
            # The plugin supports the collect API and declares its output ahead
            # of time. This is the ideal case.
            self.columns = output_header
            self.rows = repeated.lazy(collector)
        else:
            # We don't know enough about the plugin to do the easy thing. We
            # need to create a shim renderer that will cache the plugin output
            # and then use that.
            renderer = identity_renderer.IdentityRenderer(session=self.session)
            with renderer.start():
                self.session.RunPlugin(self.plugin_cls.name, format=renderer,
                                       *args, **kwargs)

            # The identity renderer will now contain the plugin output and
            # columns.
            self.columns = renderer.columns
            self.rows = repeated.repeated(*list(renderer.rows))

        return self.rows
Esempio n. 11
0
    def testCreation(self):
        """Test that creation is reasonable."""
        # This should make a repeated var of two values.
        r = repeated.repeated("foo", "bar")
        # It should be a repeated var.
        self.assertIsInstance(r, repeated.IRepeated)
        # And also have more than one value.
        self.assertTrue(repeated.isrepeating(r))

        # Repeating a single value will still create a repeated var.
        r = repeated.repeated("foo")
        self.assertIsInstance(r, repeated.IRepeated)
        # But it won't be repeating (have more than one value).
        self.assertFalse(repeated.isrepeating(r))

        # Using meld will just return a scalar on one value.
        r = repeated.meld("foo")
        self.assertIsInstance(r, six.string_types)

        # Meld on two values has the same behavior as repeated.
        r = repeated.meld("foo", "foo")
        self.assertIsInstance(r, repeated.IRepeated)
Esempio n. 12
0
    def testCreation(self):
        """Test that creation is reasonable."""
        # This should make a repeated var of two values.
        r = repeated.repeated("foo", "bar")
        # It should be a repeated var.
        self.assertIsInstance(r, repeated.IRepeated)
        # And also have more than one value.
        self.assertTrue(repeated.isrepeating(r))

        # Repeating a single value will still create a repeated var.
        r = repeated.repeated("foo")
        self.assertIsInstance(r, repeated.IRepeated)
        # But it won't be repeating (have more than one value).
        self.assertFalse(repeated.isrepeating(r))

        # Using meld will just return a scalar on one value.
        r = repeated.meld("foo")
        self.assertIsInstance(r, six.string_types)

        # Meld on two values has the same behavior as repeated.
        r = repeated.meld("foo", "foo")
        self.assertIsInstance(r, repeated.IRepeated)
Esempio n. 13
0
    def testNesting(self):
        """Test that repeated vars remain flat."""
        r = repeated.repeated("foo", "bar")
        r = repeated.repeated(r, "baz")
        self.assertValueEq(repeated.repeated("foo", "bar", "baz"), r)

        r = repeated.repeated("zoo", r)
        self.assertValueEq(repeated.repeated("zoo", "foo", "bar", "baz"), r)

        # value_eq should ignore order.
        self.assertValueEq(repeated.repeated("bar", "foo", "baz", "zoo"), r)

        # Order should be preserved for getvalues, though.
        self.assertEqual(repeated.getvalues(r), ["zoo", "foo", "bar", "baz"])

        self.assertEqual(repeated.value_type(r), type("foo"))
Esempio n. 14
0
    def testNesting(self):
        """Test that repeated vars remain flat."""
        r = repeated.repeated("foo", "bar")
        r = repeated.repeated(r, "baz")
        self.assertValueEq(repeated.repeated("foo", "bar", "baz"), r)

        r = repeated.repeated("zoo", r)
        self.assertValueEq(repeated.repeated("zoo", "foo", "bar", "baz"), r)

        # value_eq should ignore order.
        self.assertValueEq(repeated.repeated("bar", "foo", "baz", "zoo"), r)

        # Order should be preserved for getvalues, though.
        self.assertEqual(repeated.getvalues(r), ["zoo", "foo", "bar", "baz"])

        self.assertEqual(repeated.value_type(r), type("foo"))
Esempio n. 15
0
 def __call__(self, rv):
     return repeated.repeated(*list(rv))
Esempio n. 16
0
 def value_apply(self, f):
     return repeated.repeated(*[f(x) for x in self.getvalues()])
Esempio n. 17
0
File: core.py Progetto: Onager/dotty
 def __call__(self, rv):
     return repeated.repeated(*list(rv))