Skip to content
Myr

Merge pull request #473 from snoack/redundant-sorting

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

Got rid of redundant sorting in groupby filter

authored by Jeff Widman · committed by Jeff Widman · parents 080a5de, 6d356f1 · 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
650 unmodified lines
671 func = getattr(math, method)672 return func(value * (10 ** precision)) / (10 ** precision)673674675_GroupTuple = namedtuple('_GroupTuple', ['grouper', 'list'])676677@environmentfilter678def do_groupby(environment, value, attribute):679 """Group a sequence of objects by a common attribute.680
32 unmodified lines
713 It's now possible to use dotted notation to group by the child714 attribute of another attribute.715 """716 expr = make_attrgetter(environment, attribute)715 return sorted(map(_GroupTuple, groupby(sorted(value, key=expr), expr)))717 return [_GroupTuple(key, list(values)) for key, values in groupby(sorted(value, key=expr), expr)]718719718class _GroupTuple(tuple):719 __slots__ = ()720 grouper = property(itemgetter(0))721 list = property(itemgetter(1))722723 def __new__(cls, xxx_todo_changeme):724 (key, value) = xxx_todo_changeme725 return tuple.__new__(cls, (key, list(value)))726727720@environmentfilter721def do_sum(environment, iterable, attribute=None, start=0):722 """Returns the sum of a sequence of numbers plus the value of parameter723 'start' (which defaults to 0). When the sequence is empty it returns
268 unmodified lines