Merge pull request #956 from revelt/patch-1
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Tweak docs grouper chapter
authored by David Lord · committed by GitHub · parents 8e3c0e7, d8f1eb1 · Browse files · 1 file·+23 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+23 added−26 removed
- other:+23 added−26 removed
Provenance
- Commit
47e5c95- Tree
4fd06e8- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
8e3c0e7- Parent 2
d8f1eb1
1 file changed+23 added−26 removed
modified:jinja2/filters.py+23 added-26 removed
866 unmodified lines867 _GroupTuple = namedtuple('_GroupTuple', ['grouper', 'list'])868 _GroupTuple.__repr__ = tuple.__repr__869 _GroupTuple.__str__ = tuple.__str__870 871+872 @environmentfilter873 def do_groupby(environment, value, attribute):873- """Group a sequence of objects by a common attribute.874+ """Group a sequence of objects by an attribute using Python's875+ :func:`itertools.groupby`. The attribute can use dot notation for876+ nested access, like ``"address.city"``. Unlike Python's ``groupby``,877+ the values are sorted first so only one group is returned for each878+ unique value.879 875- If you for example have a list of dicts or objects that represent persons876- with `gender`, `first_name` and `last_name` attributes and you want to877- group all users by genders you can do something like the following878- snippet:880+ For example, a list of ``User`` objects with a ``city`` attribute881+ can be rendered in groups. In this example, ``grouper`` refers to882+ the ``city`` value of the group.883 884 .. sourcecode:: html+jinja885 882- <ul>883- {% for group in persons|groupby('gender') %}884- <li>{{ group.grouper }}<ul>885- {% for person in group.list %}886- <li>{{ person.first_name }} {{ person.last_name }}</li>887- {% endfor %}</ul></li>888- {% endfor %}889- </ul>886+ <ul>{% for city, items in users|groupby("city") %}887+ <li>{{ city }}888+ <ul>{% for user in items %}889+ <li>{{ user.name }}890+ {% endfor %}</ul>891+ </li>892+ {% endfor %}</ul>893 891- Additionally it's possible to use tuple unpacking for the grouper and892- list:894+ ``groupby`` yields namedtuples of ``(grouper, list)``, which895+ can be used instead of the tuple unpacking above. ``grouper`` is the896+ value of the attribute, and ``list`` is the items with that value.897 898 .. sourcecode:: html+jinja899 896- <ul>897- {% for grouper, list in persons|groupby('gender') %}898- ...899- {% endfor %}900- </ul>900+ <ul>{% for group in users|groupby("city") %}901+ <li>{{ group.grouper }}: {{ group.list|join(", ") }}902+ {% endfor %}</ul>903 902- As you can see the item we're grouping by is stored in the `grouper`903- attribute and the `list` contains all the objects that have this grouper904- in common.905-904 .. versionchanged:: 2.6907- It's now possible to use dotted notation to group by the child908- attribute of another attribute.905+ The attribute supports dot notation for nested access.906 """907 expr = make_attrgetter(environment, attribute)908 return [_GroupTuple(key, list(values)) for key, values909 in groupby(sorted(value, key=expr), expr)]350 unmodified lines