Skip to content
Snippets Groups Projects
graph_id.py 1.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • angloth's avatar
    angloth committed
    """
    B-ASIC Graph ID module for handling IDs of different objects in a graph.
    TODO: More info
    """
    
    
    angloth's avatar
    angloth committed
    from collections import defaultdict
    from typing import Union, DefaultDict
    
    class GraphIDGenerator:
        """
        A class that generates Graph IDs for objects. 
        """
    
    angloth's avatar
    angloth committed
        _next_id_number: DefaultDict(str, int)
    
        def __init__(self):
    
            self._next_id_number = defaultdict(lambda: 1) # Initalises every key element to 1
    
    angloth's avatar
    angloth committed
    
        def get_next_id(self, graph_id_type: str):
    
            """
            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
    
    angloth's avatar
    angloth committed
            return graph_id
    
    
    
    angloth's avatar
    angloth committed
    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.
            """ 
    
    angloth's avatar
    angloth committed
            return GraphID(self.graph_id_type, self.graph_id_number + 1)