chessai.util.comparable

 1import edq.util.json
 2
 3class SimpleComparable:
 4    """
 5    The base class for something that holds simple data and can be compared
 6    accurately using just it's chessai JSON representation.
 7    """
 8
 9    def __eq__(self, other: object) -> bool:
10        """
11        Attempt to override the default Python `==` operator so nodes can be used in dicts and sets.
12        This will not work for all subclasses, see _to_json_string().
13        """
14
15        # Note the hard type check (done so we can keep this method general).
16        if (type(self) != type(other)):  # pylint: disable=unidiomatic-typecheck
17            return False
18
19        return bool(self._to_json_string() == other._to_json_string())  # type: ignore[attr-defined]
20
21    def __hash__(self) -> int:
22        """
23        Attempt to override the default Python hash function so nodes can be used in dicts and sets.
24        This will not work for all subclasses, see _to_json_string().
25        """
26
27        return hash(self._to_json_string())
28
29    def __lt__(self, other: object) -> bool:
30        """
31        Attempt to override the default Python `<` operator so nodes can be sorted.
32        This will not work for all subclasses, see _to_json_string().
33        """
34
35        # Note the hard type check (done so we can keep this method general).
36        if (type(self) != type(other)):  # pylint: disable=unidiomatic-typecheck
37            return False
38
39        return bool(self._to_json_string() < other._to_json_string()) # type: ignore[attr-defined]
40
41    def _to_json_string(self) -> str:
42        """
43        Attempt to convert this object into a JSON string.
44
45        This method will generally only be used by low-level methods that are trying their best to be general.
46        We want to target a string, because it has well-defined semantics for most builtin Python operations (like `==` and `<`).
47
48        The general nature of this method will come at the cost of performance (i.e., this will be relatively slow).
49        If a subclass has complex data that this method won't work on (or needs to speed things up),
50        then they should implement any method in this class that uses this method
51        (see comments for notes on who uses this method).
52        """
53
54        return edq.util.json.dumps(self)
class SimpleComparable:
 4class SimpleComparable:
 5    """
 6    The base class for something that holds simple data and can be compared
 7    accurately using just it's chessai JSON representation.
 8    """
 9
10    def __eq__(self, other: object) -> bool:
11        """
12        Attempt to override the default Python `==` operator so nodes can be used in dicts and sets.
13        This will not work for all subclasses, see _to_json_string().
14        """
15
16        # Note the hard type check (done so we can keep this method general).
17        if (type(self) != type(other)):  # pylint: disable=unidiomatic-typecheck
18            return False
19
20        return bool(self._to_json_string() == other._to_json_string())  # type: ignore[attr-defined]
21
22    def __hash__(self) -> int:
23        """
24        Attempt to override the default Python hash function so nodes can be used in dicts and sets.
25        This will not work for all subclasses, see _to_json_string().
26        """
27
28        return hash(self._to_json_string())
29
30    def __lt__(self, other: object) -> bool:
31        """
32        Attempt to override the default Python `<` operator so nodes can be sorted.
33        This will not work for all subclasses, see _to_json_string().
34        """
35
36        # Note the hard type check (done so we can keep this method general).
37        if (type(self) != type(other)):  # pylint: disable=unidiomatic-typecheck
38            return False
39
40        return bool(self._to_json_string() < other._to_json_string()) # type: ignore[attr-defined]
41
42    def _to_json_string(self) -> str:
43        """
44        Attempt to convert this object into a JSON string.
45
46        This method will generally only be used by low-level methods that are trying their best to be general.
47        We want to target a string, because it has well-defined semantics for most builtin Python operations (like `==` and `<`).
48
49        The general nature of this method will come at the cost of performance (i.e., this will be relatively slow).
50        If a subclass has complex data that this method won't work on (or needs to speed things up),
51        then they should implement any method in this class that uses this method
52        (see comments for notes on who uses this method).
53        """
54
55        return edq.util.json.dumps(self)

The base class for something that holds simple data and can be compared accurately using just it's chessai JSON representation.