Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
keewenaw
GitHub Repository: keewenaw/ethereum-wallet-cracker
Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/utils/models.py
4804 views
1
"""Utilities for defining models
2
"""
3
4
import operator
5
from typing import Any, Callable, Type
6
7
8
class KeyBasedCompareMixin:
9
"""Provides comparison capabilities that is based on a key"""
10
11
__slots__ = ["_compare_key", "_defining_class"]
12
13
def __init__(self, key: Any, defining_class: Type["KeyBasedCompareMixin"]) -> None:
14
self._compare_key = key
15
self._defining_class = defining_class
16
17
def __hash__(self) -> int:
18
return hash(self._compare_key)
19
20
def __lt__(self, other: Any) -> bool:
21
return self._compare(other, operator.__lt__)
22
23
def __le__(self, other: Any) -> bool:
24
return self._compare(other, operator.__le__)
25
26
def __gt__(self, other: Any) -> bool:
27
return self._compare(other, operator.__gt__)
28
29
def __ge__(self, other: Any) -> bool:
30
return self._compare(other, operator.__ge__)
31
32
def __eq__(self, other: Any) -> bool:
33
return self._compare(other, operator.__eq__)
34
35
def _compare(self, other: Any, method: Callable[[Any, Any], bool]) -> bool:
36
if not isinstance(other, self._defining_class):
37
return NotImplemented
38
39
return method(self._compare_key, other._compare_key)
40
41