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
Provenance
- Commit
5435d35- Tree
4261a0a- Author
Sebastian Noack <sebastian.noack@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
532171d
2 files changed+76 added−1 removed
modified:jinja2/filters.py+52 added-1 removed
10 unmodified lines11 import re12 import math13 import random14 15-from itertools import groupby15+from itertools import groupby, chain16 from collections import namedtuple17 from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode, \18 unicode_urlencode, htmlsafe_json_dumps19 from jinja2.runtime import Undefined292 unmodified lines312 seen.add(key)313 yield item314 315 316+def _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')322+323+ key_func = make_attrgetter(environment, attribute, not case_sensitive)324+ return func(chain([first], it), key=key_func)325+326+327+@environmentfilter328+def do_min(environment, value, attribute=None, case_sensitive=False):329+ """Return the smallest item from the sequence.330+331+ .. sourcecode:: jinja332+333+ {{ [1, 2, 3]|min }}334+ -> 1335+336+ It is also possible to get the item providing the smallest value for a337+ certain attribute:338+339+ .. sourcecode:: jinja340+341+ {{ users|min('last_login') }}342+ """343+ return _min_or_max(min, value, environment, attribute, case_sensitive)344+345+346+@environmentfilter347+def do_max(environment, value, attribute=None, case_sensitive=False):348+ """Return the largest item from the sequence.349+350+ .. sourcecode:: jinja351+352+ {{ [1, 2, 3]|max }}353+ -> 3354+355+ It is also possible to get the item providing the largest value for a356+ certain attribute:357+358+ .. sourcecode:: jinja359+360+ {{ users|max('last_login') }}361+ """362+ return _min_or_max(max, value, environment, attribute, case_sensitive)363+364+365 def 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 lines1145 '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 linesmodified:tests/test_filters.py+24 added
389 unmodified lines390 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'393 394+ def test_min1(self, env):395+ tmpl = env.from_string('{{ ["a", "B"]|min }}')396+ assert tmpl.render() == 'a'397+398+ def test_min2(self, env):399+ tmpl = env.from_string('{{ []|min }}')400+ assert tmpl.render() == ''401+402+ def test_min3(self, env):403+ tmpl = env.from_string('{{ items|min("value") }}')404+ assert tmpl.render(items=map(Magic, [5, 1, 9])) == '1'405+406+ def test_max1(self, env):407+ tmpl = env.from_string('{{ ["a", "B"]|max }}')408+ assert tmpl.render() == 'B'409+410+ def test_max2(self, env):411+ tmpl = env.from_string('{{ []|max }}')412+ assert tmpl.render() == ''413+414+ def test_max3(self, env):415+ tmpl = env.from_string('{{ items|max("value") }}')416+ assert tmpl.render(items=map(Magic, [5, 9, 1])) == '9'417+418 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