Skip to content
Myr

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

3 files+75 added−20 removed

modified:jinja2/asyncfilters.py+23 added-2 removed
85 unmodified lines
86async def do_rejectattr(*args, **kwargs):87 return async_select_or_reject(args, kwargs, lambda x: not x, True)888990@asyncfiltervariant(filters.do_select)91async def do_select(*args, **kwargs):92 return async_select_or_reject(args, kwargs, lambda x: x, False)939495@asyncfiltervariant(filters.do_selectattr)96async def do_selectattr(*args, **kwargs):97 return async_select_or_reject(args, kwargs, lambda x: x, True)9899100@asyncfiltervariant(filters.do_map)101async 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)106107108ASYNC_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 lines
820 Users on this page: {{ titles|map('lower')|join(', ') }}821822 .. versionadded:: 2.7823 """824 context = args[0]825 seq = args[1]826827 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)841824 seq, func = prepare_map(args, kwargs)825 if seq:826 for item in seq:827 yield func(item)828
74 unmodified lines
903 """904 return select_or_reject(args, kwargs, lambda x: not x, True)905906907def prepare_map(args, kwargs):908 context = args[0]909 seq = args[1]910911 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)925926 return seq, func927928929def prepare_select_or_reject(args, kwargs, modfunc, lookup_attr):930 context = args[0]931 seq = args[1]932 if lookup_attr:
80 unmodified lines
modified:tests/test_asyncfilters.py+29 added
128 unmodified lines
129 tmpl = env_async.from_string(130 '{{ items()|reject|join("|") }}'131 )132 assert tmpl.render(items=items) == 'None|False|0'133134135def 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'138139140def 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'145146147def 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'