forked from Aunsiels/pyformlang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformal_object.py
More file actions
45 lines (34 loc) · 1.11 KB
/
formal_object.py
File metadata and controls
45 lines (34 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
""" General object representation """
from typing import Hashable, Optional, Any
from abc import abstractmethod
class FormalObject:
""" General object representation """
def __init__(self, value: Hashable) -> None:
self._value = value
self._hash = None
self.index: Optional[int] = None
@property
def value(self) -> Hashable:
""" Gets the value of the object
Returns
---------
value : any
The value of the object
"""
return self._value
def __eq__(self, other: Any) -> bool:
if not isinstance(other, FormalObject):
return self.value == other
return self._is_equal_to(other) and other._is_equal_to(self)
def __hash__(self) -> int:
if self._hash is None:
self._hash = hash(self._value)
return self._hash
def __str__(self) -> str:
return str(self._value)
@abstractmethod
def __repr__(self) -> str:
raise NotImplementedError
@abstractmethod
def _is_equal_to(self, other: "FormalObject") -> bool:
raise NotImplementedError