Newer
Older
"""
B-ASIC Graph ID module for handling IDs of different objects in a graph.
TODO: More info
"""
from collections import defaultdict
from typing import Union, DefaultDict
class GraphIDGenerator:
"""
A class that generates Graph IDs for objects.
"""
_next_id_number: DefaultDict(str, int)
def __init__(self):
self._next_id_number = defaultdict(lambda: 1) # Initalises every key element to 1
"""
Retrns the next graph id for a certain graph id type.
"""
graph_id = GraphID(graph_id_type, self._next_id_number[graph_id_type])
self._next_id_number[graph_id_type] += 1 # Increment the current number
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class GraphID:
"""
Graph ID class that handles the id of an object in a graph.
"""
graph_id_type: str
graph_id_number: int
def __init__(self, graph_id_type: str, graph_id_number: int):
self.graph_id_type = graph_id_type
self.graph_id_number = graph_id_number
def __str__(self) -> str:
return graph_id_type + str(graph_id_number)
def __repr__(self) -> str:
return str(self)
def __hash__(self) -> int:
return hash(str(self))
def __eq__(self, other: graphID) -> bool:
return self.graph_id_type == other.graph_id_type and \
self.graph_id_number == other.graph_id_number
def get_next_id(self) -> GraphID:
"""
Returns a new GraphID of the same type with an incremented id number.
"""
return GraphID(self.graph_id_type, self.graph_id_number + 1)