Skip to content
Myr

Merge pull request #692 from dnmvisser/dictsort_reverse

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

Dictsort reverse

authored by David Lord · committed by GitHub · parents 07c0b87, 34ca7f1 · Browse files · 3 files+25 added−16 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+25 added−16 removed

  • tests:+10 added−10 removed
  • other:+15 added−6 removed

3 files+25 added−16 removed

modified:jinja2/filters.py+13 added-6 removed
201 unmodified lines
202 for item in _word_beginning_split_re.split(soft_unicode(s))203 if item])204205206def do_dictsort(value, case_sensitive=False, by='key'):206def do_dictsort(value, case_sensitive=False, by='key', reverse=False):207 """Sort a dict and yield (key, value) pairs. Because python dicts are208 unsorted you may want to use this function to order them by either209 key or value:210
1 unmodified line
212213 {% for item in mydict|dictsort %}214 sort the dict by key, case insensitive215216 {% for item in mydict|dictsort(reverse=true) %}217 sort the dict by key, case insensitive, reverse order218219 {% for item in mydict|dictsort(true) %}220 sort the dict by key, case sensitive221222 {% for item in mydict|dictsort(false, 'value') %}
3 unmodified lines
226 pos = 0227 elif by == 'value':228 pos = 1229 else:227 raise FilterArgumentError('You can only sort by either '228 '"key" or "value"')230 raise FilterArgumentError(231 'You can only sort by either "key" or "value"'232 )233234 def sort_func(item):235 value = item[pos]231 if isinstance(value, string_types) and not case_sensitive:232 value = value.lower()236237 if not case_sensitive:238 value = ignore_case(value)239240 return value241235 return sorted(value.items(), key=sort_func)242 return sorted(value.items(), key=sort_func, reverse=reverse)243244245@environmentfilter246def do_sort(
944 unmodified lines
modified:tests/test_filters.py+10 added-10 removed
43 unmodified lines
44 "{{ false|default('no', true) }}|{{ given|default('no') }}"45 )46 assert tmpl.render(given='yes') == 'no|False|no|yes'4748 def test_dictsort(self, env):49 tmpl = env.from_string(50 '{{ foo|dictsort }}|'51 '{{ foo|dictsort(true) }}|'52 '{{ foo|dictsort(false, "value") }}'53 )54 out = tmpl.render(foo={"aa": 0, "b": 1, "c": 2, "AB": 3})55 assert out == ("[('aa', 0), ('AB', 3), ('b', 1), ('c', 2)]|"56 "[('AB', 3), ('aa', 0), ('b', 1), ('c', 2)]|"57 "[('aa', 0), ('b', 1), ('c', 2), ('AB', 3)]")48 @pytest.mark.parametrize('args,expect', (49 ('', "[('aa', 0), ('AB', 3), ('b', 1), ('c', 2)]"),50 ('true', "[('AB', 3), ('aa', 0), ('b', 1), ('c', 2)]"),51 ('by="value"', "[('aa', 0), ('b', 1), ('c', 2), ('AB', 3)]"),52 ('reverse=true', "[('c', 2), ('b', 1), ('AB', 3), ('aa', 0)]")53 ))54 def test_dictsort(self, env, args, expect):55 t = env.from_string('{{{{ foo|dictsort({args}) }}}}'.format(args=args))56 out = t.render(foo={"aa": 0, "b": 1, "c": 2, "AB": 3})57 assert out == expect5859 def test_batch(self, env):60 tmpl = env.from_string("{{ foo|batch(3)|list }}|"61 "{{ foo|batch(3, 'X')|list }}")
590 unmodified lines
modified:CHANGES+2 added
28 unmodified lines
29- Add tests for all comparison operators: ``eq``, ``ne``, ``lt``, ``le``,30 ``gt``, ``ge``. (`#665`_)31- ``import`` statement cannot end with a trailing comma. (`#617`_, `#618`_)32- ``indent`` filter will not indent blank lines by default. (`#685`_)33- Add ``reverse`` argument for ``dictsort`` filter. (`#692`_)3435.. _#469: https://github.com/pallets/jinja/pull/46936.. _#475: https://github.com/pallets/jinja/pull/47537.. _#478: https://github.com/pallets/jinja/pull/47838.. _#617: https://github.com/pallets/jinja/pull/61739.. _#618: https://github.com/pallets/jinja/pull/61840.. _#665: https://github.com/pallets/jinja/pull/66541.. _#685: https://github.com/pallets/jinja/pull/68542.. _#692: https://github.com/pallets/jinja/pull/6924344Version 2.9.645-------------46(bugfix release, released on April 3rd 2017)
504 unmodified lines