Skip to content
Myr

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

2 files+69 added−1 removed

modified:jinja2/asyncfilters.py+20 added-1 removed
75 unmodified lines
76async def do_join(eval_ctx, value, d=u'', attribute=None):77 return filters.do_join(eval_ctx, await auto_to_seq(value), d, attribute)787980@asyncfiltervariant(filters.do_list)81async def do_list(value):82 return await auto_to_seq(value)838485@asyncfiltervariant(filters.do_reject)86async def do_reject(*args, **kwargs):87 return async_select_or_reject(args, kwargs, lambda x: not x, False)88
16 unmodified lines
105@asyncfiltervariant(filters.do_map)106async 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)111112113@asyncfiltervariant(filters.do_sum)114async 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 rv123124125ASYNC_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 lines
164 '{{ users()|selectattr("is_active")|'165 'map(attribute="name")|join("|") }}'166 )167 assert tmpl.render(users=users) == 'john|jane'168169170@mark_dualiter('items', lambda: list('123'))171def test_simple_map(env_async, items):172 tmpl = env_async.from_string('{{ items()|map("int")|sum }}')173 assert tmpl.render(items=items) == '6'174175176@mark_dualiter('users', make_users)177def 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'180181182def test_empty_map(env_async):183 tmpl = env_async.from_string('{{ none|map("upper")|list }}')184 assert tmpl.render() == '[]'185186187def test_sum(env_async):188 tmpl = env_async.from_string('''{{ [1, 2, 3, 4, 5, 6]|sum }}''')189 assert tmpl.render() == '21'190191192def 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'199200201def 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'208209210def 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'