mirror of https://github.com/mahmoud/boltons.git
funcutils: Add partial_ordering(), decorator similar to functools.total_ordering()
This commit is contained in:
parent
19596be986
commit
b385816cf9
|
@ -98,6 +98,24 @@ def copy_function(orig, copy_dict=True):
|
|||
return ret
|
||||
|
||||
|
||||
def partial_ordering(cls):
|
||||
"""Class decorator. Similar to functools.total_ordering, except it
|
||||
is used to define partial orderings (ie. it is possible that x is niether
|
||||
greater than, equal to or less than y). It assumes the presence
|
||||
of a <= (__le__) and >= (__ge__) method, but nothing else.
|
||||
It will not override any existing additional comparison methods.
|
||||
"""
|
||||
def __lt__(self, other): return self <= other and not self >= other
|
||||
def __gt__(self, other): return self >= other and not self <= other
|
||||
def __eq__(self, other): return self >= other and self <= other
|
||||
|
||||
if not hasattr(cls, '__lt__'): cls.__lt__ = __lt__
|
||||
if not hasattr(cls, '__gt__'): cls.__gt__ = __gt__
|
||||
if not hasattr(cls, '__eq__'): cls.__eq__ = __eq__
|
||||
|
||||
return cls
|
||||
|
||||
|
||||
class InstancePartial(functools.partial):
|
||||
""":class:`functools.partial` is a huge convenience for anyone
|
||||
working with Python's great first-class functions. It allows
|
||||
|
|
Loading…
Reference in New Issue