Class decorator that fills in missing ordering methods
(cls)
| 186 | } |
| 187 | |
| 188 | def total_ordering(cls): |
| 189 | """Class decorator that fills in missing ordering methods""" |
| 190 | # Find user-defined comparisons (not those inherited from object). |
| 191 | roots = {op for op in _convert if getattr(cls, op, None) is not getattr(object, op, None)} |
| 192 | if not roots: |
| 193 | raise ValueError('must define at least one ordering operation: < > <= >=') |
| 194 | root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__ |
| 195 | for opname, opfunc in _convert[root]: |
| 196 | if opname not in roots: |
| 197 | opfunc.__name__ = opname |
| 198 | setattr(cls, opname, opfunc) |
| 199 | return cls |
| 200 | |
| 201 | |
| 202 | ################################################################################ |