Skip to content
Myr

Added min and max filters

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

authored by Sebastian Noack · committed by David Lord · parent 532171d · Browse files · 2 files+76 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+76 added−1 removed

  • tests:+24 added−0 removed
  • other:+52 added−1 removed

2 files+76 added−1 removed

modified:jinja2/filters.py+52 added-1 removed
10 unmodified lines
11import re12import math13import random1415from itertools import groupby15from itertools import groupby, chain16from collections import namedtuple17from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode, \18 unicode_urlencode, htmlsafe_json_dumps19from jinja2.runtime import Undefined
292 unmodified lines
312 seen.add(key)313 yield item314315316def _min_or_max(func, value, environment, attribute, case_sensitive):317 it = iter(value)318 try:319 first = next(it)320 except StopIteration:321 return environment.undefined('No aggregated item, sequence was empty')322323 key_func = make_attrgetter(environment, attribute, not case_sensitive)324 return func(chain([first], it), key=key_func)325326327@environmentfilter328def do_min(environment, value, attribute=None, case_sensitive=False):329 """Return the smallest item from the sequence.330331 .. sourcecode:: jinja332333 {{ [1, 2, 3]|min }}334 -> 1335336 It is also possible to get the item providing the smallest value for a337 certain attribute:338339 .. sourcecode:: jinja340341 {{ users|min('last_login') }}342 """343 return _min_or_max(min, value, environment, attribute, case_sensitive)344345346@environmentfilter347def do_max(environment, value, attribute=None, case_sensitive=False):348 """Return the largest item from the sequence.349350 .. sourcecode:: jinja351352 {{ [1, 2, 3]|max }}353 -> 3354355 It is also possible to get the item providing the largest value for a356 certain attribute:357358 .. sourcecode:: jinja359360 {{ users|max('last_login') }}361 """362 return _min_or_max(max, value, environment, attribute, case_sensitive)363364365def do_default(value, default_value=u'', boolean=False):366 """If the value is undefined it will return the passed default value,367 otherwise the value of the variable:368
776 unmodified lines
1145 'length': len,1146 'list': do_list,1147 'lower': do_lower,1148 'map': do_map,1149 'min': do_min,1150 'max': do_max,1151 'pprint': do_pprint,1152 'random': do_random,1153 'reject': do_reject,1154 'rejectattr': do_rejectattr,
23 unmodified lines
modified:tests/test_filters.py+24 added
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_min1(self, env):395 tmpl = env.from_string('{{ ["a", "B"]|min }}')396 assert tmpl.render() == 'a'397398 def test_min2(self, env):399 tmpl = env.from_string('{{ []|min }}')400 assert tmpl.render() == ''401402 def test_min3(self, env):403 tmpl = env.from_string('{{ items|min("value") }}')404 assert tmpl.render(items=map(Magic, [5, 1, 9])) == '1'405406 def test_max1(self, env):407 tmpl = env.from_string('{{ ["a", "B"]|max }}')408 assert tmpl.render() == 'B'409410 def test_max2(self, env):411 tmpl = env.from_string('{{ []|max }}')412 assert tmpl.render() == ''413414 def test_max3(self, env):415 tmpl = env.from_string('{{ items|max("value") }}')416 assert tmpl.render(items=map(Magic, [5, 9, 1])) == '9'417418 def test_unique(self, env):419 t = env.from_string('{{ "".join(["b", "A", "a", "b"]|unique) }}')420 assert t.render() == "bA"421
219 unmodified lines