~homeworkprod/byceps

9d9c28866952d4656e772521ef2090dc0afd1e36 — Jochen Kupperschmidt 1 year, 1 month ago 84e7fd3
Flip argument order of `util.iterables.find`
M byceps/blueprints/admin/shop/order/models.py => byceps/blueprints/admin/shop/order/models.py +1 -1
@@ 47,4 47,4 @@ class OrderStateFilter(Enum):
                and order_state_filter.shipped is None
            )

        return iterables.find(match, cls)
        return iterables.find(cls, match)

M byceps/blueprints/site/ticketing/views.py => byceps/blueprints/site/ticketing/views.py +1 -1
@@ 51,7 51,7 @@ def index_mine():
    tickets = [ticket for ticket in tickets if not ticket.revoked]

    current_user_uses_any_ticket = find(
        lambda t: t.used_by_id == current_user.id, tickets
        tickets, lambda t: t.used_by_id == current_user.id
    )

    return {

M byceps/services/news/html_service.py => byceps/services/news/html_service.py +1 -1
@@ 37,7 37,7 @@ def _render_image(
    height: Optional[int] = None,
) -> str:
    """Render HTML for image."""
    image = find(lambda image: image.number == number, images)
    image = find(images, lambda image: image.number == number)

    if image is None:
        raise Exception(f'Unknown image number "{number}"')

M byceps/util/iterables.py => byceps/util/iterables.py +1 -1
@@ 15,7 15,7 @@ T = TypeVar('T')
Predicate = Callable[[T], bool]


def find(predicate: Predicate, iterable: Iterable[T]) -> Optional[T]:
def find(iterable: Iterable[T], predicate: Predicate) -> Optional[T]:
    """Return the first element in the iterable that matches the
    predicate.


M tests/integration/services/shop/order/test_service_mark_order_as_paid.py => tests/integration/services/shop/order/test_service_mark_order_as_paid.py +1 -1
@@ 63,7 63,7 @@ def test_additional_event_data(order, admin_user):
    )

    events = event_service.get_events_for_order(order.id)
    paid_event = find(lambda e: e.event_type == 'order-paid', events)
    paid_event = find(events, lambda e: e.event_type == 'order-paid')

    # Internal properties must not be overridden by additional event
    # data passed to the service.

M tests/unit/util/iterables/test_find.py => tests/unit/util/iterables/test_find.py +6 -6
@@ 9,25 9,25 @@ from byceps.util.iterables import find


@pytest.mark.parametrize(
    'predicate, iterable, expected',
    'iterable, predicate, expected',
    [
        (
            lambda x: x > 3,
            [],
            lambda x: x > 3,
            None,
        ),
        (
            lambda x: x > 3,
            [-2, 0, 1, 3],
            lambda x: x > 3,
            None,
        ),
        (
            lambda x: x > 3,
            [2, 3, 4, 5],
            lambda x: x > 3,
            4,
        ),
    ],
)
def test_find(predicate, iterable, expected):
    actual = find(predicate, iterable)
def test_find(iterable, predicate, expected):
    actual = find(iterable, predicate)
    assert actual == expected