Skip to content
Myr

make attrgetter take generic postprocess function

Before adoption: this commit came in with the history the project adopted, so no landing records it.

add changelog

authored by David Lord · committed by David Lord · parent f2fc06d · Browse files · 3 files+39 added−24 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+39 added−24 removed

  • tests:+9 added−9 removed
  • other:+30 added−15 removed

3 files+39 added−24 removed

modified:jinja2/filters.py+28 added-15 removed
50 unmodified lines
51 f.environmentfilter = True52 return f535455def ignore_case(value):56 """For use as a postprocessor for :func:`make_attrgetter`. Converts strings57 to lowercase and returns other types as-is."""58 return value.lower() if isinstance(value, string_types) else value596055def make_attrgetter(environment, attribute, lowercase=False):61def make_attrgetter(environment, attribute, postprocess=None):62 """Returns a callable that looks up the given attribute from a63 passed object with the rules of the environment. Dots are allowed64 to access attributes of attributes. Integer parts in paths are65 looked up as integers.
7 unmodified lines
7374 def attrgetter(item):75 for part in attribute:76 item = environment.getitem(item, part)71 if lowercase and isinstance(item, string_types):72 item = item.lower()7778 if postprocess is not None:79 item = postprocess(item)8081 return item8283 return attrgetter848586def do_forceescape(value):
147 unmodified lines
234 return sorted(value.items(), key=sort_func)235236237@environmentfilter229def do_sort(environment, value, reverse=False, case_sensitive=False,230 attribute=None):238def do_sort(239 environment, value, reverse=False, case_sensitive=False, attribute=None240):241 """Sort an iterable. Per default it sorts ascending, if you pass it242 true as first argument it will reverse the sorting.243244 If the iterable is made of strings the third parameter can be used to
17 unmodified lines
262263 .. versionchanged:: 2.6264 The `attribute` parameter was added.265 """256 key_func = make_attrgetter(environment, attribute, not case_sensitive)266 key_func = make_attrgetter(267 environment, attribute,268 postprocess=ignore_case if not case_sensitive else None269 )270 return sorted(value, key=key_func, reverse=reverse)271272273@environmentfilter
6 unmodified lines
280 -> ['foo', 'bar', 'foobar']281282 This filter complements the `groupby` filter, which sorts and groups an283 iterable by a certain attribute. The `unique` filter groups the items271 from the iterable by themself instead and always returns a flat list of272 unique items. That can be useuful for example when you need to concatenate284 from the iterable by themselves instead and always returns a flat list of285 unique items. That can be useful for example when you need to concatenate286 that items:287288 .. sourcecode:: jinja289290 {{ ['foo', 'bar', 'foobar', 'FooBar']|unique|join(',') }}291 -> foo,bar,foobar292293 Also note that the resulting list contains the items in the same order281 as their first occurence in the iterable passed to the filter. If sorting294 as their first occurrence in the iterable passed to the filter. If sorting295 is needed you can still chain the `unique` and `sort` filter:296297 .. sourcecode:: jinja298299 {{ ['foo', 'bar', 'foobar', 'FooBar']|unique|sort }}300 -> ['bar', 'foo', 'foobar']301 """289 getter = make_attrgetter(environment, attribute, not case_sensitive)290302 getter = make_attrgetter(303 environment, attribute,304 postprocess=ignore_case if not case_sensitive else None305 )306 seen = set()292 rv = []307308 for item in value:309 key = getter(item)310311 if key not in seen:312 seen.add(key)298 rv.append(item)313 yield item314300 return rv315302316def do_default(value, default_value=u'', boolean=False):317 """If the value is undefined it will return the passed default value,318 otherwise the value of the variable:319
807 unmodified lines
modified:tests/test_filters.py+9 added-9 removed
389 unmodified lines
390 def test_sort4(self, env):391 tmpl = env.from_string('''{{ items|sort(attribute='value')|join }}''')392 assert tmpl.render(items=map(Magic, [3, 2, 4, 1])) == '1234'393394 def test_unique1(self, env):395 tmpl = env.from_string('{{ "".join(["b", "A", "a", "b"]|unique) }}')396 assert tmpl.render() == "bA"394 def test_unique(self, env):395 t = env.from_string('{{ "".join(["b", "A", "a", "b"]|unique) }}')396 assert t.render() == "bA"397398 def test_unique2(self, env):399 tmpl = env.from_string('{{ "".join(["b", "A", "a", "b"]|unique(true)) }}')400 assert tmpl.render() == "bAa"398 def test_unique_case_sensitive(self, env):399 t = env.from_string('{{ "".join(["b", "A", "a", "b"]|unique(true)) }}')400 assert t.render() == "bAa"401402 def test_unique3(self, env):403 tmpl = env.from_string("{{ items|unique(attribute='value')|join }}")404 assert tmpl.render(items=map(Magic, [3, 2, 4, 1, 2])) == '3241'402 def test_unique_attribute(self, env):403 t = env.from_string("{{ items|unique(attribute='value')|join }}")404 assert t.render(items=map(Magic, [3, 2, 4, 1, 2])) == '3241'405406 def test_groupby(self, env):407 tmpl = env.from_string('''408 {%- for grouper, list in [{'foo': 1, 'bar': 2},
208 unmodified lines
modified:CHANGES+2 added
22 unmodified lines
23 surrounding whitespace. Also added a new policy to enable this for all24 `trans` blocks.25- The ``random`` filter is no longer incorrectly constant folded and will26 produce a new random choice each time the template is rendered. (`#478`_)27- Add a ``unique`` filter. (`#469`_)2829.. _#469: https://github.com/pallets/jinja/pull/46930.. _#478: https://github.com/pallets/jinja/pull/4783132Version 2.9.633-------------
505 unmodified lines