Skip to content
Browse files

Use total_ordering to honor PEP-8 recommendation as suggested by @aka…

  • Loading branch information...
1 parent d76ac8b commit 8e9a24e59d85d8599feac63db4cb6e2caccec19a @charettes committed May 2, 2012
Showing with 39 additions and 1 deletion.
  1. +8 −1 django/db/models/fields/__init__.py
  2. +31 −0 django/utils/functional.py
View
9 django/db/models/fields/__init__.py
@@ -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 _
@@ -45,6 +45,7 @@ class FieldDoesNotExist(Exception):
#
# getattr(obj, opts.pk.attname)
+@total_ordering
class Field(object):
"""Base class for all field types"""
@@ -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):
View
31 django/utils/functional.py
@@ -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.
Something went wrong with that request. Please try again.