Ported map and select
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 5a8bc31 · Browse files · 3 files·+75 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+75 added−20 removed
- tests:+29 added−0 removed
- other:+46 added−20 removed
Provenance
- Commit
f7bb60a- Tree
b2d7250- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5a8bc31
3 files changed+75 added−20 removed
modified:jinja2/asyncfilters.py+23 added-2 removed
85 unmodified lines86 async def do_rejectattr(*args, **kwargs):87 return async_select_or_reject(args, kwargs, lambda x: not x, True)88 89 90+@asyncfiltervariant(filters.do_select)91+async def do_select(*args, **kwargs):92+ return async_select_or_reject(args, kwargs, lambda x: x, False)93+94+95+@asyncfiltervariant(filters.do_selectattr)96+async def do_selectattr(*args, **kwargs):97+ return async_select_or_reject(args, kwargs, lambda x: x, True)98+99+100+@asyncfiltervariant(filters.do_map)101+async def do_map(*args, **kwargs):102+ seq, func = filters.prepare_map(args, kwargs)103+ if seq:104+ async for item in seq:105+ yield func(item)106+107+108 ASYNC_FILTERS = {109 'first': do_first,110 'groupby': do_groupby,111 'join': do_join,112 # we intentionally do not support do_last because that would be113 # ridiculous96- 'reject': do_reject,97- 'rejectattr': do_rejectattr,114+ 'reject': do_reject,115+ 'rejectattr': do_rejectattr,116+ 'map': do_map,117+ 'select': do_select,118+ 'selectattr': do_selectattr,119 }modified:jinja2/filters.py+23 added-18 removed
819 unmodified lines820 Users on this page: {{ titles|map('lower')|join(', ') }}821 822 .. versionadded:: 2.7823 """824- context = args[0]825- seq = args[1]826-827- if len(args) == 2 and 'attribute' in kwargs:828- attribute = kwargs.pop('attribute')829- if kwargs:830- raise FilterArgumentError('Unexpected keyword argument %r' %831- next(iter(kwargs)))832- func = make_attrgetter(context.environment, attribute)833- else:834- try:835- name = args[2]836- args = args[3:]837- except LookupError:838- raise FilterArgumentError('map requires a filter argument')839- func = lambda item: context.environment.call_filter(840- name, item, args, kwargs, context=context)841-824+ seq, func = prepare_map(args, kwargs)825 if seq:826 for item in seq:827 yield func(item)828 74 unmodified lines903 """904 return select_or_reject(args, kwargs, lambda x: not x, True)905 906 907+def prepare_map(args, kwargs):908+ context = args[0]909+ seq = args[1]910+911+ if len(args) == 2 and 'attribute' in kwargs:912+ attribute = kwargs.pop('attribute')913+ if kwargs:914+ raise FilterArgumentError('Unexpected keyword argument %r' %915+ next(iter(kwargs)))916+ func = make_attrgetter(context.environment, attribute)917+ else:918+ try:919+ name = args[2]920+ args = args[3:]921+ except LookupError:922+ raise FilterArgumentError('map requires a filter argument')923+ func = lambda item: context.environment.call_filter(924+ name, item, args, kwargs, context=context)925+926+ return seq, func927+928+929 def prepare_select_or_reject(args, kwargs, modfunc, lookup_attr):930 context = args[0]931 seq = args[1]932 if lookup_attr:80 unmodified linesmodified:tests/test_asyncfilters.py+29 added
128 unmodified lines129 tmpl = env_async.from_string(130 '{{ items()|reject|join("|") }}'131 )132 assert tmpl.render(items=items) == 'None|False|0'133+134+135+def test_simple_select(env_async):136+ tmpl = env_async.from_string('{{ [1, 2, 3, 4, 5]|select("odd")|join("|") }}')137+ assert tmpl.render() == '1|3|5'138+139+140+def test_bool_select(env_async):141+ tmpl = env_async.from_string(142+ '{{ [none, false, 0, 1, 2, 3, 4, 5]|select|join("|") }}'143+ )144+ assert tmpl.render() == '1|2|3|4|5'145+146+147+def test_simple_select_attr(env_async):148+ class User(object):149+ def __init__(self, name, is_active):150+ self.name = name151+ self.is_active = is_active152+ users = [153+ User('john', True),154+ User('jane', True),155+ User('mike', False),156+ ]157+ tmpl = env_async.from_string(158+ '{{ users|selectattr("is_active")|'159+ 'map(attribute="name")|join("|") }}'160+ )161+ assert tmpl.render(users=users) == 'john|jane'