ported list and sum
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent af74d2d · Browse files · 2 files·+69 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+69 added−1 removed
- tests:+49 added−0 removed
- other:+20 added−1 removed
Provenance
- Commit
8e08a21- Tree
2175f4a- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
af74d2d
2 files changed+69 added−1 removed
modified:jinja2/asyncfilters.py+20 added-1 removed
75 unmodified lines76 async def do_join(eval_ctx, value, d=u'', attribute=None):77 return filters.do_join(eval_ctx, await auto_to_seq(value), d, attribute)78 79 80+@asyncfiltervariant(filters.do_list)81+async def do_list(value):82+ return await auto_to_seq(value)83+84+85 @asyncfiltervariant(filters.do_reject)86 async def do_reject(*args, **kwargs):87 return async_select_or_reject(args, kwargs, lambda x: not x, False)88 16 unmodified lines105 @asyncfiltervariant(filters.do_map)106 async def do_map(*args, **kwargs):107 seq, func = filters.prepare_map(args, kwargs)108 if seq:104- async for item in seq:109+ async for item in auto_aiter(seq):110 yield func(item)111 112 113+@asyncfiltervariant(filters.do_sum)114+async def do_sum(environment, iterable, attribute=None, start=0):115+ rv = start116+ if attribute is not None:117+ func = filters.make_attrgetter(environment, attribute)118+ else:119+ func = lambda x: x120+ async for item in auto_aiter(iterable):121+ rv += func(item)122+ return rv123+124+125 ASYNC_FILTERS = {126 'first': do_first,127 'groupby': do_groupby,128 'join': do_join,129+ 'list': do_list,130 # we intentionally do not support do_last because that would be131 # ridiculous132 'reject': do_reject,133 'rejectattr': do_rejectattr,134 'map': do_map,135 'select': do_select,136 'selectattr': do_selectattr,137+ 'sum': do_sum,138 }modified:tests/test_asyncfilters.py+49 added
163 unmodified lines164 '{{ users()|selectattr("is_active")|'165 'map(attribute="name")|join("|") }}'166 )167 assert tmpl.render(users=users) == 'john|jane'168+169+170+@mark_dualiter('items', lambda: list('123'))171+def test_simple_map(env_async, items):172+ tmpl = env_async.from_string('{{ items()|map("int")|sum }}')173+ assert tmpl.render(items=items) == '6'174+175+176+@mark_dualiter('users', make_users)177+def test_attribute_map(env_async, users):178+ tmpl = env_async.from_string('{{ users()|map(attribute="name")|join("|") }}')179+ assert tmpl.render(users=users) == 'john|jane|mike'180+181+182+def test_empty_map(env_async):183+ tmpl = env_async.from_string('{{ none|map("upper")|list }}')184+ assert tmpl.render() == '[]'185+186+187+def test_sum(env_async):188+ tmpl = env_async.from_string('''{{ [1, 2, 3, 4, 5, 6]|sum }}''')189+ assert tmpl.render() == '21'190+191+192+def test_sum_attributes(env_async):193+ tmpl = env_async.from_string('''{{ values|sum('value') }}''')194+ assert tmpl.render(values=[195+ {'value': 23},196+ {'value': 1},197+ {'value': 18},198+ ]) == '42'199+200+201+def test_sum_attributes_nested(env_async):202+ tmpl = env_async.from_string('''{{ values|sum('real.value') }}''')203+ assert tmpl.render(values=[204+ {'real': {'value': 23}},205+ {'real': {'value': 1}},206+ {'real': {'value': 18}},207+ ]) == '42'208+209+210+def test_sum_attributes_tuple(env_async):211+ tmpl = env_async.from_string('''{{ values.items()|sum('1') }}''')212+ assert tmpl.render(values={213+ 'foo': 23,214+ 'bar': 1,215+ 'baz': 18,216+ }) == '42'