From 23070f348015fcc72ccdb8cd452ac4596a08c8fa Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Fri, 29 Apr 2022 13:34:27 +0200 Subject: [PATCH] changed operation signature to return iterables for 1 -> n compatibility --- pyinfra/rest.py | 15 +++++++++++---- test/exploration_tests/partial_response_test.py | 8 ++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pyinfra/rest.py b/pyinfra/rest.py index 0766ced..845292f 100644 --- a/pyinfra/rest.py +++ b/pyinfra/rest.py @@ -1,8 +1,10 @@ +from itertools import chain from operator import itemgetter -from funcy import compose +from funcy import compose, first, rcompose +from more_itertools import flatten -from pyinfra.utils.func import star, lift, lstarlift +from pyinfra.utils.func import star, lift, lstarlift, starlift from test.utils.server import bytes_to_string, string_to_bytes @@ -22,8 +24,13 @@ def bundle(data: bytes, metadata: dict): def unpack_op_pack(operation): - return compose(star(pack), star(operation), unpack) + return compose(first, lstarlift(pack), star(operation), unpack) def unpack_batchop_pack(operation): - return compose(lstarlift(pack), operation, lift(unpack)) + return rcompose( + lift(unpack), # unpack the buffer items + operation, # apply operation on unpacked items + flatten, # operations may be 1 -> 1, 1 -> n or n -> 1, hence flatten + lstarlift(pack), + ) diff --git a/test/exploration_tests/partial_response_test.py b/test/exploration_tests/partial_response_test.py index dd4b547..1f73c09 100644 --- a/test/exploration_tests/partial_response_test.py +++ b/test/exploration_tests/partial_response_test.py @@ -6,13 +6,13 @@ from typing import Iterable import pytest import requests -from funcy import rcompose, compose, rpartial, identity, lmap, ilen +from funcy import rcompose, compose, rpartial, identity, lmap, ilen, first from pyinfra.rest import pack, unpack, bundle -from pyinfra.utils.func import lift, starlift, parallel_map, star +from pyinfra.utils.func import lift, starlift, parallel_map, star, lstarlift logger = logging.getLogger("PIL.PngImagePlugin") -logger.setLevel(logging.INFO) +logger.setLevel(logging.WARNING) def dispatch_methods(input_data): @@ -44,7 +44,7 @@ def post_partial(url, input_data: Iterable[bytes], metadata): @pytest.mark.parametrize("item_type", ["string", "image"]) def test_sending_partial_request(url, data_items, metadata, operation, server_process): - expected = lmap(compose(star(bundle), partial(operation, metadata=metadata)), data_items) + expected = lmap(compose(first, lstarlift(bundle), partial(operation, metadata=metadata)), data_items) output = list(post_partial(f"{url}/process", data_items, metadata)) assert output == expected