-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathentity_instance_id.py
More file actions
42 lines (35 loc) · 1.27 KB
/
entity_instance_id.py
File metadata and controls
42 lines (35 loc) · 1.27 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
class EntityInstanceId:
def __init__(self, entity: str, key: str):
self.entity = entity
self.key = key
def __str__(self) -> str:
return f"@{self.entity}@{self.key}"
def __eq__(self, other):
if not isinstance(other, EntityInstanceId):
return False
return self.entity == other.entity and self.key == other.key
def __lt__(self, other):
if not isinstance(other, EntityInstanceId):
return self < other
return str(self) < str(other)
@staticmethod
def parse(entity_id: str) -> "EntityInstanceId":
"""Parse a string representation of an entity ID into an EntityInstanceId object.
Parameters
----------
entity_id : str
The string representation of the entity ID, in the format '@entity@key'.
Returns
-------
EntityInstanceId
The parsed EntityInstanceId object.
Raises
------
ValueError
If the input string is not in the correct format.
"""
try:
_, entity, key = entity_id.split("@", 2)
return EntityInstanceId(entity=entity, key=key)
except ValueError as ex:
raise ValueError(f"Invalid entity ID format: {entity_id}", ex)