AutoFramework/venv/Lib/site-packages/pip/_internal/utils/models.py

40 lines
1.2 KiB
Python
Raw Normal View History

2022-07-17 01:48:29 +08:00
"""Utilities for defining models
"""
import operator
from typing import Any, Callable, Type
class KeyBasedCompareMixin:
"""Provides comparison capabilities that is based on a key"""
__slots__ = ["_compare_key", "_defining_class"]
2022-07-21 08:44:10 +08:00
def __init__(self, key: Any, defining_class: Type["KeyBasedCompareMixin"]) -> None:
2022-07-17 01:48:29 +08:00
self._compare_key = key
self._defining_class = defining_class
2022-07-21 08:44:10 +08:00
def __hash__(self) -> int:
2022-07-17 01:48:29 +08:00
return hash(self._compare_key)
2022-07-21 08:44:10 +08:00
def __lt__(self, other: Any) -> bool:
2022-07-17 01:48:29 +08:00
return self._compare(other, operator.__lt__)
2022-07-21 08:44:10 +08:00
def __le__(self, other: Any) -> bool:
2022-07-17 01:48:29 +08:00
return self._compare(other, operator.__le__)
2022-07-21 08:44:10 +08:00
def __gt__(self, other: Any) -> bool:
2022-07-17 01:48:29 +08:00
return self._compare(other, operator.__gt__)
2022-07-21 08:44:10 +08:00
def __ge__(self, other: Any) -> bool:
2022-07-17 01:48:29 +08:00
return self._compare(other, operator.__ge__)
2022-07-21 08:44:10 +08:00
def __eq__(self, other: Any) -> bool:
2022-07-17 01:48:29 +08:00
return self._compare(other, operator.__eq__)
2022-07-21 08:44:10 +08:00
def _compare(self, other: Any, method: Callable[[Any, Any], bool]) -> bool:
2022-07-17 01:48:29 +08:00
if not isinstance(other, self._defining_class):
return NotImplemented
return method(self._compare_key, other._compare_key)