Skip to content
Snippets Groups Projects
Commit 2c02dcce authored by angloth's avatar angloth
Browse files

Add new types for graph id type and graph id number

parent 47b8658e
No related branches found
No related tags found
1 merge request!2Integrated ID system, traversing and som signal tests
......@@ -4,37 +4,43 @@ TODO: More info
"""
from collections import defaultdict
from typing import Union, DefaultDict
from typing import NewType, Union, DefaultDict
GraphIDType = NewType("GraphIDType", str)
GraphIDNumber = NewType("GraphIDNumber", int)
class GraphIDGenerator:
"""
A class that generates Graph IDs for objects.
"""
_next_id_number: DefaultDict(str, int)
_next_id_number: DefaultDict(GraphIDType, GraphIDNumber)
def __init__(self):
self._next_id_number = defaultdict(lambda: 1) # Initalises every key element to 1
self._next_id_number = defaultdict(lambda: 1) # Initalises every key element to 1
def get_next_id(self, graph_id_type: str):
def get_next_id(self, graph_id_type: GraphIDType):
"""
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
self._next_id_number[graph_id_type] += 1 # Increase the current id number
return graph_id
class GraphID:
"""
Graph ID class that handles the id of an object in a graph.
Graph ID class that saves the id of for a graph object.
The ID consists of an id_type that is saved as a string and an
id_number that is saved as an integer.
"""
graph_id_type: str
graph_id_number: int
graph_id_type: GraphIDType
graph_id_number: GraphIDNumber
def __init__(self, graph_id_type: str, graph_id_number: int):
def __init__(self, graph_id_type: GraphIDType, graph_id_number: GraphIDNumber):
self.graph_id_type = graph_id_type
self.graph_id_number = graph_id_number
......@@ -51,7 +57,7 @@ class GraphID:
return hash(str(self))
def __eq__(self, other: graphID) -> bool:
def __eq__(self, other: GraphID) -> bool:
return self.graph_id_type == other.graph_id_type and \
self.graph_id_number == other.graph_id_number
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment