def cmp_using(
eq=None,
lt=None,
le=None,
gt=None,
ge=None,
require_same_type=True,
class_name="Comparable",
): """
Create a class that can be passed into `attrs.field`'s ``eq``, ``order``, and ``cmp`` arguments to customize field comparison.
The resulting class will have a full set of ordering methods if at least
one of ``{lt, le, gt, ge}`` and ``eq`` are provided.
Args:
eq (typing.Callable | None):
Callable used to evaluate equality of two objects.
lt (typing.Callable | None):
Callable used to evaluate whether one object is less than another
object.
le (typing.Callable | None):
Callable used to evaluate whether one object is less than or equal
to another object.
gt (typing.Callable | None):
Callable used to evaluate whether one object is greater than
another object.
ge (typing.Callable | None):
Callable used to evaluate whether one object is greater than or
equal to another object.
require_same_type (bool):
When `True`, equality and ordering methods will return
`NotImplemented` if objects are not of the same type.
class_name (str | None): Name of class. Defaults to "Comparable".
# Add same type requirement. if require_same_type:
type_._requirements.append(_check_same_type)
# Add total ordering if at least one operation was defined. if0 < num_order_functions < 4: ifnot has_eq_function: # functools.total_ordering requires __eq__ to be defined, # so raise early error here to keep a nice stack.
msg = "eq must be define is order to complete ordering from lt, le, gt, ge." raise ValueError(msg)
type_ = functools.total_ordering(type_)
return type_
def _make_init(): """
Create __init__ method. """
def __init__(self, value): """
Initialize object with *value*. """
self.value = value
result = func(self.value, other.value) if result is NotImplemented: return NotImplemented
return result
method.__name__ = f"__{name}__"
method.__doc__ = (
f"Return a {_operation_names[name]} b. Computed by attrs."
)
return method
def _is_comparable_to(self, other): """
Check whether `other` is comparable to `self`. """ return all(func(self, other) for func in self._requirements)
def _check_same_type(self, other): """ ReturnTrueif *self* and *other* are of the same type, False otherwise. """ return other.value.__class__ is self.value.__class__
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet am 2026-08-26)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.