Thank you so much Theron, this is spot-on! So, it was lambda after all (although not Pyramid). You've solved my problem, I can move on to the next 😅
Thanks again, Laurent. Le jeu. 14 mars 2024 à 06:57, Theron Luhn <[email protected]> a écrit : > That’s a result of the lack of block scoping in Python. `op` is going to > resolve when the lambda first executes, which is always after the loop has > finished, and scoped to the containing function so `op` will always be the > final loop value. > > Easiest workaround is to just wrap the loop contents in a function, so you > get a fresh scope for each permutation. > > def make_routes(config, cat, op): > config.add_route… > > for cat in ('actes', 'constats'): > for op in ('archive', 'unarchive’): > make_routes(config, cat, op) > > > — Theron > > > > On Mar 13, 2024, at 4:36 PM, Laurent Daverio <[email protected]> wrote: > > Thank you everybody, I'll try to set up a working example tomorrow. > > Actually you're right, my question was formulated incorrectly, it may not > be a question of lambdas. Basically, I was trying to create a series of > routes in a reasonably concise way, something like this : 2 types of > objects (actes, constats), 2 operations (archive, unarchive), 2 variants > (handle one object, or handle a list of ids): > > - /actes/{id}/archive > - /actes/{id}/unarchive > - /constats/{id}/archive > - /constats/{id}/unarchive > - /actes/-/archive_selection > - /actes/-/unarchive_selection > - /constats/-/archive_selection > - /constats/-/unarchive_selection > > ``` > > > factories = {'actes': {'fact': acte_factory, > 'rsrc': ActeResource}, > 'constats': {'fact': constat_factory, > 'rsrc': ConstatResource}} > > functions = {'archive': (_archive, _archive_multi), > 'unarchive': (_unarchive, _unarchive_multi)} > > for cat in ('actes', 'constats'): > for op in ('archive', 'unarchive'): > > route_name: str = f'api.{cat}.{op}' > > config.add_route(route_name, f'/{cat}/{{id}}/{op}', > factory=factories[cat]['fact']) > config.add_view(functions[op][0], route_name=route_name, > request_method='GET', renderer='json') > > config.add_route(route_name + '_selection', > f'/{cat}/-/{op}_selection') > config.add_view( > lambda request: functions[op][1](request, factories[cat]['rsrc']), > route_name=route_name + '_selection', request_method='POST', > renderer='json') > > ``` > > The first 4 routes (those with "{id}" in them) worked fine, but last 4 > (those using lambdas) were all mapped to the same function > (`_unarchive_multi`) in the end. > > In order to make them work, I had to unroll the loops and enumerate > everything. Ugly :( > > ``` > > config.add_route('api.actes.archive_selection', '/actes/-/archive_selection') > config.add_route('api.constats.archive_selection', > '/constats/-/archive_selection') > config.add_view( > lambda request: _archive_multi(request, ActeResource), > route_name='api.actes.archive_selection', request_method='POST', > renderer='json') > config.add_view( > lambda request: _archive_multi(request, ConstatResource), > route_name='api.constats.archive_selection', request_method='POST', > renderer='json') > > config.add_route('api.actes.unarchive_selection', > '/actes/-/unarchive_selection') > config.add_route('api.constats.unarchive_selection', > '/constats/-/unarchive_selection') > config.add_view( > lambda request: _unarchive_multi(request, ActeResource), > route_name='api.actes.unarchive_selection', request_method='POST', > renderer='json') > config.add_view( > lambda request: _unarchive_multi(request, ConstatResource), > route_name='api.constats.unarchive_selection', request_method='POST', > renderer='json') > > ``` > > Laurent. > > > > -- > You received this message because you are subscribed to the Google Groups > "pylons-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/pylons-discuss/CAB7cU6wqp1fSXJ_6ngOBqOw2RSSTxzjAZwGoyYsoDa58OdJkPg%40mail.gmail.com > <https://groups.google.com/d/msgid/pylons-discuss/CAB7cU6wqp1fSXJ_6ngOBqOw2RSSTxzjAZwGoyYsoDa58OdJkPg%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > > > -- > You received this message because you are subscribed to the Google Groups > "pylons-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/pylons-discuss/EFA14C22-E728-43D0-A25A-30157E698935%40luhn.com > <https://groups.google.com/d/msgid/pylons-discuss/EFA14C22-E728-43D0-A25A-30157E698935%40luhn.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/CAB7cU6zDtGmSP5RRiw%3DpqugZUcSwPA6og%3DWyBMNyzSh%3DyhnE2A%40mail.gmail.com.
