Merge pull request #1360 from lisongmin/add-default-value-to-groupby
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Ability to set default value in groupby filter
authored by David Lord · committed by GitHub · parents 4a941ca, 34fbec9 · Browse files · 4 files·+36 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+36 added−3 removed
- tests:+15 added−0 removed
- other:+21 added−3 removed
Provenance
- Commit
c752dc8- Tree
0c6bb6b- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
4a941ca- Parent 2
34fbec9
4 files changed+36 added−3 removed
modified:src/jinja2/asyncfilters.py+2 added-1 removed
105 unmodified lines106 async def do_groupby(107 environment: "Environment",108 value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",109 attribute: t.Union[str, int],110+ default: t.Optional[t.Any] = None,111 ) -> "t.List[t.Tuple[t.Any, t.List[V]]]":111- expr = filters.make_attrgetter(environment, attribute)112+ expr = filters.make_attrgetter(environment, attribute, default=default)113 return [114 filters._GroupTuple(key, await auto_to_seq(values))115 for key, values in groupby(sorted(await auto_to_seq(value), key=expr), expr)116 ]133 unmodified linesmodified:src/jinja2/filters.py+17 added-2 removed
1114 unmodified lines1115 1116 1117 @environmentfilter1118 def do_groupby(1119- environment: "Environment", value: "t.Iterable[V]", attribute: t.Union[str, int]1119+ environment: "Environment",1120+ value: "t.Iterable[V]",1121+ attribute: t.Union[str, int],1122+ default: t.Optional[t.Any] = None,1123 ) -> "t.List[t.Tuple[t.Any, t.List[V]]]":1124 """Group a sequence of objects by an attribute using Python's1125 :func:`itertools.groupby`. The attribute can use dot notation for1126 nested access, like ``"address.city"``. Unlike Python's ``groupby``,23 unmodified lines1150 <ul>{% for group in users|groupby("city") %}1151 <li>{{ group.grouper }}: {{ group.list|join(", ") }}1152 {% endfor %}</ul>1153 1154+ You can specify a ``default`` value to use if an object in the list1155+ does not have the given attribute.1156+1157+ .. sourcecode:: jinja1158+1159+ <ul>{% for city, items in users|groupby("city", default="NY") %}1160+ <li>{{ city }}: {{ items|map(attribute="name")|join(", ") }}</li>1161+ {% endfor %}</ul>1162+1163+ .. versionchanged:: 3.01164+ Added the ``default`` parameter.1165+1166 .. versionchanged:: 2.61167 The attribute supports dot notation for nested access.1168 """1154- expr = make_attrgetter(environment, attribute)1169+ expr = make_attrgetter(environment, attribute, default=default)1170 return [1171 _GroupTuple(key, list(values))1172 for key, values in groupby(sorted(value, key=expr), expr)1173 ]450 unmodified linesmodified:tests/test_filters.py+15 added
591 unmodified lines592 "1971[totally not]",593 "",594 ]595 596+ def test_groupby_default(self, env):597+ tmpl = env.from_string(598+ "{% for city, items in users|groupby('city', default='NY') %}"599+ "{{ city }}: {{ items|map(attribute='name')|join(', ') }}\n"600+ "{% endfor %}"601+ )602+ out = tmpl.render(603+ users=[604+ {"name": "emma", "city": "NY"},605+ {"name": "smith", "city": "WA"},606+ {"name": "john"},607+ ]608+ )609+ assert out == "NY: emma, john\nWA: smith\n"610+611 def test_filtertag(self, env):612 tmpl = env.from_string(613 "{% filter upper|replace('FOO', 'foo') %}foobar{% endfilter %}"614 )229 unmodified linesmodified:CHANGES.rst+2 added
56 unmodified lines57 - ``Undefined`` is iterable in an async environment. :issue:`1294`58 - ``NativeEnvironment`` supports async mode. :issue:`1362`59 - Template rendering only treats ``\n``, ``\r\n`` and ``\r`` as line60 breaks. Other characters are left unchanged. :issue:`769, 952, 1313`61+- ``|groupby`` filter takes an optional ``default`` argument.62+ :issue:`1359`63 64 65 Version 2.11.366 --------------780 unmodified lines