Ticket #17851: total_ordering.diff

File total_ordering.diff, 2.0 KB (added by Simon Charette, 12 years ago)

Added total_ordering decorator to utils.functionnal

  • django/utils/functional.py

    diff --git a/django/utils/functional.py b/django/utils/functional.py
    index befe3e9..a41753a 100644
    a b def partition(predicate, values):  
    310310    for item in values:
    311311        results[predicate(item)].append(item)
    312312    return results
     313
     314try:
     315    from functools import total_ordering
     316except ImportError:
     317    # For Python < 2.7
     318    def total_ordering(cls):
     319        """Class decorator that fills in missing ordering methods"""
     320        convert = {
     321            '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)),
     322                       ('__le__', lambda self, other: self < other or self == other),
     323                       ('__ge__', lambda self, other: not self < other)],
     324            '__le__': [('__ge__', lambda self, other: not self <= other or self == other),
     325                       ('__lt__', lambda self, other: self <= other and not self == other),
     326                       ('__gt__', lambda self, other: not self <= other)],
     327            '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)),
     328                       ('__ge__', lambda self, other: self > other or self == other),
     329                       ('__le__', lambda self, other: not self > other)],
     330            '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other),
     331                       ('__gt__', lambda self, other: self >= other and not self == other),
     332                       ('__lt__', lambda self, other: not self >= other)]
     333        }
     334        roots = set(dir(cls)) & set(convert)
     335        if not roots:
     336            raise ValueError('must define at least one ordering operation: < > <= >=')
     337        root = max(roots)       # prefer __lt__ to __le__ to __gt__ to __ge__
     338        for opname, opfunc in convert[root]:
     339            if opname not in roots:
     340                opfunc.__name__ = opname
     341                opfunc.__doc__ = getattr(int, opname).__doc__
     342                setattr(cls, opname, opfunc)
     343        return cls
Back to Top