Skip to content

Commit

Permalink
Update graph_utilities.py
Browse files Browse the repository at this point in the history
Make graphfcn registry a dict
  • Loading branch information
Dobson committed Feb 28, 2024
1 parent 682bbe4 commit c529443
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions swmmanywhere/graph_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,24 @@ def add_graphfcn(self,
node_attributes = node_attributes.union(self.adds_node_attributes)
return edge_attributes, node_attributes

class GraphFunctionRegistry:
class GraphFunctionRegistry(dict):
"""Registry object."""
pass

def register(self, cls):
"""Register a graph function."""
if cls.__name__ in self:
raise ValueError(f"{cls.__name__} already in the graph functions registry!")

self[cls.__name__] = cls()
return cls

def __getattr__(self, name):
"""Get a graph function from the graphfcn dict."""
try:
return self[name]
except KeyError:
raise AttributeError(f"{name} NOT in the graph functions registry!")


graphfcns = GraphFunctionRegistry()

Expand All @@ -136,7 +151,7 @@ def register_graphfcn(cls) -> Callable:
Returns:
cls (Callable): The same class
"""
setattr(graphfcns, cls.__name__, cls())
graphfcns.register(cls)
return cls

def get_osmid_id(data: dict) -> Hashable:
Expand Down

0 comments on commit c529443

Please sign in to comment.