Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/attr/_cmp.py
7763 views
# SPDX-License-Identifier: MIT12from __future__ import absolute_import, division, print_function34import functools56from ._compat import new_class7from ._make import _make_ne8910_operation_names = {"eq": "==", "lt": "<", "le": "<=", "gt": ">", "ge": ">="}111213def cmp_using(14eq=None,15lt=None,16le=None,17gt=None,18ge=None,19require_same_type=True,20class_name="Comparable",21):22"""23Create a class that can be passed into `attr.ib`'s ``eq``, ``order``, and24``cmp`` arguments to customize field comparison.2526The resulting class will have a full set of ordering methods if27at least one of ``{lt, le, gt, ge}`` and ``eq`` are provided.2829:param Optional[callable] eq: `callable` used to evaluate equality30of two objects.31:param Optional[callable] lt: `callable` used to evaluate whether32one object is less than another object.33:param Optional[callable] le: `callable` used to evaluate whether34one object is less than or equal to another object.35:param Optional[callable] gt: `callable` used to evaluate whether36one object is greater than another object.37:param Optional[callable] ge: `callable` used to evaluate whether38one object is greater than or equal to another object.3940:param bool require_same_type: When `True`, equality and ordering methods41will return `NotImplemented` if objects are not of the same type.4243:param Optional[str] class_name: Name of class. Defaults to 'Comparable'.4445See `comparison` for more details.4647.. versionadded:: 21.1.048"""4950body = {51"__slots__": ["value"],52"__init__": _make_init(),53"_requirements": [],54"_is_comparable_to": _is_comparable_to,55}5657# Add operations.58num_order_functions = 059has_eq_function = False6061if eq is not None:62has_eq_function = True63body["__eq__"] = _make_operator("eq", eq)64body["__ne__"] = _make_ne()6566if lt is not None:67num_order_functions += 168body["__lt__"] = _make_operator("lt", lt)6970if le is not None:71num_order_functions += 172body["__le__"] = _make_operator("le", le)7374if gt is not None:75num_order_functions += 176body["__gt__"] = _make_operator("gt", gt)7778if ge is not None:79num_order_functions += 180body["__ge__"] = _make_operator("ge", ge)8182type_ = new_class(class_name, (object,), {}, lambda ns: ns.update(body))8384# Add same type requirement.85if require_same_type:86type_._requirements.append(_check_same_type)8788# Add total ordering if at least one operation was defined.89if 0 < num_order_functions < 4:90if not has_eq_function:91# functools.total_ordering requires __eq__ to be defined,92# so raise early error here to keep a nice stack.93raise ValueError(94"eq must be define is order to complete ordering from "95"lt, le, gt, ge."96)97type_ = functools.total_ordering(type_)9899return type_100101102def _make_init():103"""104Create __init__ method.105"""106107def __init__(self, value):108"""109Initialize object with *value*.110"""111self.value = value112113return __init__114115116def _make_operator(name, func):117"""118Create operator method.119"""120121def method(self, other):122if not self._is_comparable_to(other):123return NotImplemented124125result = func(self.value, other.value)126if result is NotImplemented:127return NotImplemented128129return result130131method.__name__ = "__%s__" % (name,)132method.__doc__ = "Return a %s b. Computed by attrs." % (133_operation_names[name],134)135136return method137138139def _is_comparable_to(self, other):140"""141Check whether `other` is comparable to `self`.142"""143for func in self._requirements:144if not func(self, other):145return False146return True147148149def _check_same_type(self, other):150"""151Return True if *self* and *other* are of the same type, False otherwise.152"""153return other.value.__class__ is self.value.__class__154155156