How to use decorator - 10 common examples

To help you get started, we’ve selected a few decorator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github smarie / python-pytest-steps / pytest_steps / steps_py3_api.py View on Github external
# Then create the final function that will be executed when the user calls
        def _execute_self_and_inner(f_with_new_api, *args, **kwargs):
            # First execute the function with the new api
            # Typically that function will be empty but this ensures that python does the args checking:
            # error messages will be the 'python 3' ones in case of mistake.
            #
            # In addition the new api functions can do further checks (not recommended, for consistency with inner func)
            # note that they should not produce results - they will be discarded if so.
            f_with_new_api(*args, **kwargs)

            # then execute the inner function
            return f_with_old_api(*args, **kwargs)

        # Use decorate to preserve everything (name, signature...)
        return decorate(f_with_new_api, _execute_self_and_inner)
github Jammy2211 / PyAutoLens / auto_lens / test_decorator.py View on Github external
def test__simple_assumptions(self, circular):
        array = decorator.array_function(circular.flux_at_coordinates)(x_min=0, x_max=101, y_min=0, y_max=101,
                                                                       pixel_scale=1)
        assert array.shape == (101, 101)
        assert array[51][51] > array[51][52]
        assert array[51][51] > array[52][51]
        assert all(map(lambda i: i > 0, array[0]))

        array = decorator.array_function(circular.flux_at_coordinates)(x_min=0, x_max=100, y_min=0, y_max=100,
                                                                       pixel_scale=0.5)
        assert array.shape == (200, 200)
github Jammy2211 / PyAutoLens / auto_lens / test_decorator.py View on Github external
def test_combined_array(self, circular):
        combined = profile.CombinedLightProfile(circular, circular)

        assert all(map(lambda i: i == 2,
                       decorator.array_function(combined.flux_at_coordinates)().flatten() / decorator.array_function(
                           circular.flux_at_coordinates)().flatten()))
github Jammy2211 / PyAutoLens / auto_lens / test_decorator.py View on Github external
def test_mask(self):
        mask = MockMask([(x, 0) for x in range(-5, 6)])
        array = decorator.array_function(lambda coordinates: 1)(-5, -5, 5, 5, 1, mask=mask)

        assert array[5][5] is None
        assert array[5][6] is not None
        assert array[6][5] is None
        assert array[0][0] is not None
        assert array[0][5] is None
github Jammy2211 / PyAutoLens / auto_lens / test_decorator.py View on Github external
def test__deflection_angle_array(self):
        mass_profile = profile.EllipticalIsothermalMassProfile(centre=(0, 0), axis_ratio=0.5, phi=45.0,
                                                               einstein_radius=2.0)
        # noinspection PyTypeChecker
        assert all(decorator.array_function(mass_profile.compute_deflection_angle)(-1, -1, -0.5, -0.5, 0.1)[0][
                       0] == mass_profile.compute_deflection_angle((-1, -1)))
github Jammy2211 / PyAutoLens / auto_lens / test_decorator.py View on Github external
def test_symmetric_profile(self, circular):
        circular.centre = (50, 50)
        array = decorator.array_function(circular.flux_at_coordinates)(x_min=0, x_max=100, y_min=0, y_max=100,
                                                                       pixel_scale=1.0)

        assert array[50][50] > array[50][51]
        assert array[50][50] > array[49][50]
        assert array[49][50] == array[50][51]
        assert array[50][51] == array[50][49]
        assert array[50][49] == array[51][50]

        array = decorator.array_function(circular.flux_at_coordinates)(x_min=0, x_max=100, y_min=0, y_max=100,
                                                                       pixel_scale=0.5)

        assert array[100][100] > array[100][101]
        assert array[100][100] > array[99][100]
        assert array[99][100] == array[100][101]
        assert array[100][101] == array[100][99]
        assert array[100][99] == array[101][100]
github Jammy2211 / PyAutoLens / auto_lens / test_decorator.py View on Github external
def test__flat_array(self, circular):
        array = decorator.array_function(circular.flux_at_coordinates)(x_min=0, x_max=100, y_min=0, y_max=100,
                                                                       pixel_scale=1)
        flat_array = decorator.array_function(circular.flux_at_coordinates)(x_min=0, x_max=100, y_min=0, y_max=100,
                                                                            pixel_scale=1).flatten()

        assert all(array[0] == flat_array[:100])
        assert all(array[1] == flat_array[100:200])
github Jammy2211 / PyAutoLens / auto_lens / test_decorator.py View on Github external
def test__flat_array(self, circular):
        array = decorator.array_function(circular.flux_at_coordinates)(x_min=0, x_max=100, y_min=0, y_max=100,
                                                                       pixel_scale=1)
        flat_array = decorator.array_function(circular.flux_at_coordinates)(x_min=0, x_max=100, y_min=0, y_max=100,
                                                                            pixel_scale=1).flatten()

        assert all(array[0] == flat_array[:100])
        assert all(array[1] == flat_array[100:200])
github Jammy2211 / PyAutoLens / auto_lens / test_decorator.py View on Github external
def test__ellipticity(self, circular, elliptical, vertical):
        array = decorator.array_function(circular.flux_at_coordinates)(x_min=0, x_max=101, y_min=0, y_max=101,
                                                                       pixel_scale=1)
        assert array[60][0] == array[0][60]

        array = decorator.array_function(elliptical.flux_at_coordinates)(x_min=0, x_max=100, y_min=0, y_max=100,
                                                                         pixel_scale=1)

        assert array[60][51] > array[51][60]

        array = decorator.array_function(vertical.flux_at_coordinates)(x_min=0, x_max=100, y_min=0, y_max=100,
                                                                       pixel_scale=1)
        assert array[60][51] < array[51][60]
github xiaowangwindow / scrapy-rotated-proxy / tests / test_downloadermiddleware_httpproxy.py View on Github external
    @contextmanager
    def _middleware(self, spider, settings, **kwargs):
        _crawler = Crawler(spider, settings)
        if 'auth_encoding' in kwargs:
            _mw = RotatedProxyMiddleware(_crawler,
                                         auth_encoding=kwargs['auth_encoding'])
        else:
            _mw = RotatedProxyMiddleware(_crawler)
        _mw.spider_opened(spider)
        try:
            yield _mw
        finally:
            _mw.spider_closed(spider)