Skip to content
Myr

Got rid of redundant sorting in groupby filter

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

authored by Sebastian Noack · committed by Sebastian Noack · parent 50c6cd7 · Browse files · 1 file+4 added−12 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+4 added−12 removed

  • other:+4 added−12 removed

1 file+4 added−12 removed

modified:jinja2/filters.py+4 added-12 removed
10 unmodified lines
11import re12import math1314from random import choice15from operator import itemgetter15from itertools import groupby16from collections import namedtuple17from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode, \18 unicode_urlencode19from jinja2.runtime import Undefined20from jinja2.exceptions import FilterArgumentError
647 unmodified lines
668 func = getattr(math, method)669 return func(value * (10 ** precision)) / (10 ** precision)670671672_GroupTuple = namedtuple('_GroupTuple', ['grouper', 'list'])673674@environmentfilter675def do_groupby(environment, value, attribute):676 """Group a sequence of objects by a common attribute.677
32 unmodified lines
710 It's now possible to use dotted notation to group by the child711 attribute of another attribute.712 """713 expr = make_attrgetter(environment, attribute)712 return sorted(map(_GroupTuple, groupby(sorted(value, key=expr), expr)))714 return [_GroupTuple(key, list(values)) for key, values in groupby(sorted(value, key=expr), expr)]715716715class _GroupTuple(tuple):716 __slots__ = ()717 grouper = property(itemgetter(0))718 list = property(itemgetter(1))719720 def __new__(cls, xxx_todo_changeme):721 (key, value) = xxx_todo_changeme722 return tuple.__new__(cls, (key, list(value)))723724717@environmentfilter718def do_sum(environment, iterable, attribute=None, start=0):719 """Returns the sum of a sequence of numbers plus the value of parameter720 'start' (which defaults to 0). When the sequence is empty it returns
268 unmodified lines