Skip to content

Commit

Permalink
Use total_ordering to honor PEP-8 recommendation as suggested by @aka…
Browse files Browse the repository at this point in the history
  • Loading branch information
charettes committed May 2, 2012
1 parent d76ac8b commit 8e9a24e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
9 changes: 8 additions & 1 deletion django/db/models/fields/__init__.py
Expand Up @@ -12,7 +12,7 @@
from django.core import exceptions, validators
from django.utils.datastructures import DictWrapper
from django.utils.dateparse import parse_date, parse_datetime, parse_time
from django.utils.functional import curry
from django.utils.functional import curry, total_ordering
from django.utils.text import capfirst
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -45,6 +45,7 @@ class FieldDoesNotExist(Exception):
#
# getattr(obj, opts.pk.attname)

@total_ordering
class Field(object):
"""Base class for all field types"""

Expand Down Expand Up @@ -118,6 +119,12 @@ def __init__(self, verbose_name=None, name=None, primary_key=False,
messages.update(error_messages or {})
self.error_messages = messages

def __eq__(self, other):
# Needed for @total_ordering
if isinstance(other, Field):
return self.creation_counter == other.creation_counter
return NotImplemented

def __lt__(self, other):
# This is needed because bisect does not take a comparison function.
if isinstance(other, Field):
Expand Down
31 changes: 31 additions & 0 deletions django/utils/functional.py
Expand Up @@ -310,3 +310,34 @@ def partition(predicate, values):
for item in values:
results[predicate(item)].append(item)
return results

try:
from functools import total_ordering
except ImportError:
# For Python < 2.7
def total_ordering(cls):
"""Class decorator that fills in missing ordering methods"""
convert = {
'__lt__': [('__gt__', lambda self, other: not (self < other or self == other)),
('__le__', lambda self, other: self < other or self == other),
('__ge__', lambda self, other: not self < other)],
'__le__': [('__ge__', lambda self, other: not self <= other or self == other),
('__lt__', lambda self, other: self <= other and not self == other),
('__gt__', lambda self, other: not self <= other)],
'__gt__': [('__lt__', lambda self, other: not (self > other or self == other)),
('__ge__', lambda self, other: self > other or self == other),
('__le__', lambda self, other: not self > other)],
'__ge__': [('__le__', lambda self, other: (not self >= other) or self == other),
('__gt__', lambda self, other: self >= other and not self == other),
('__lt__', lambda self, other: not self >= other)]
}
roots = set(dir(cls)) & set(convert)
if not roots:
raise ValueError('must define at least one ordering operation: < > <= >=')
root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__
for opname, opfunc in convert[root]:
if opname not in roots:
opfunc.__name__ = opname
opfunc.__doc__ = getattr(int, opname).__doc__
setattr(cls, opname, opfunc)
return cls

0 comments on commit 8e9a24e

Please sign in to comment.