Skip to content

GA-169 | rename graph_name to name #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ os.environ["DATABASE_USERNAME"] = "root"
os.environ["DATABASE_PASSWORD"] = "password"
os.environ["DATABASE_NAME"] = "_system"

G = nxadb.Graph(graph_name="KarateGraph")
G = nxadb.Graph(name="KarateGraph")

G_nx = nx.karate_club_graph()
assert len(G.nodes) == len(G_nx.nodes)
Expand Down
2 changes: 1 addition & 1 deletion nx_arangodb/algorithms/shortest_paths/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def shortest_path(
bind_vars = {
"source": source,
"target": target,
"graph": G.graph_name,
"graph": G.name,
"weight": weight,
}

Expand Down
4 changes: 2 additions & 2 deletions nx_arangodb/classes/digraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def to_networkx_class(cls) -> type[nx.DiGraph]:

def __init__(
self,
graph_name: str | None = None,
name: str | None = None,
default_node_type: str | None = None,
edge_type_key: str = "_edge_type",
edge_type_func: Callable[[str, str], str] | None = None,
Expand All @@ -38,7 +38,7 @@ def __init__(
**kwargs: Any,
):
super().__init__(
graph_name,
name,
default_node_type,
edge_type_key,
edge_type_func,
Expand Down
42 changes: 28 additions & 14 deletions nx_arangodb/classes/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def to_networkx_class(cls) -> type[nx.Graph]:

def __init__(
self,
graph_name: str | None = None,
name: str | None = None,
default_node_type: str | None = None,
edge_type_key: str = "_edge_type",
edge_type_func: Callable[[str, str], str] | None = None,
Expand All @@ -59,12 +59,12 @@ def __init__(
**kwargs: Any,
):
self._db = None
self._graph_name = None
self.__name = None
self._graph_exists_in_db = False

self._set_db(db)
if self._db is not None:
self._set_graph_name(graph_name)
self._set_graph_name(name)

# We need to store the data transfer properties as some functions will need them
self.read_parallelism = read_parallelism
Expand Down Expand Up @@ -104,15 +104,15 @@ def __init__(
m = "Cannot pass **edge_type_func** if the graph already exists"
raise NotImplementedError(m)

self.adb_graph = self.db.graph(self._graph_name)
self.adb_graph = self.db.graph(self.__name)
vertex_collections = self.adb_graph.vertex_collections()
edge_definitions = self.adb_graph.edge_definitions()

if default_node_type is None:
default_node_type = list(vertex_collections)[0]
logger.info(f"Default node type set to '{default_node_type}'")
elif default_node_type not in vertex_collections:
m = f"Default node type '{default_node_type}' not found in graph '{graph_name}'" # noqa: E501
m = f"Default node type '{default_node_type}' not found in graph '{name}'" # noqa: E501
raise InvalidDefaultNodeType(m)

node_types_to_edge_type_map: dict[tuple[str, str], str] = {}
Expand All @@ -138,9 +138,9 @@ def edge_type_func(u: str, v: str) -> str:
self._set_factory_methods()
self._set_arangodb_backend_config()

elif self._graph_name:
elif self.__name:

prefix = f"{graph_name}_" if graph_name else ""
prefix = f"{name}_" if name else ""
if default_node_type is None:
default_node_type = f"{prefix}node"
if edge_type_func is None:
Expand All @@ -162,7 +162,7 @@ def edge_type_func(u: str, v: str) -> str:

if isinstance(incoming_graph_data, nx.Graph):
self.adb_graph = ADBNX_Adapter(self.db).networkx_to_arangodb(
self._graph_name,
self.__name,
incoming_graph_data,
edge_definitions=edge_definitions,
batch_size=self.write_batch_size,
Expand All @@ -174,15 +174,19 @@ def edge_type_func(u: str, v: str) -> str:

else:
self.adb_graph = self.db.create_graph(
self._graph_name,
self.__name,
edge_definitions=edge_definitions,
)

self._set_factory_methods()
self._set_arangodb_backend_config()
logger.info(f"Graph '{graph_name}' created.")
logger.info(f"Graph '{name}' created.")
self._graph_exists_in_db = True

# add graph name to kwargs if not none
if name is not None:
kwargs["name"] = name

super().__init__(*args, **kwargs)

#######################
Expand Down Expand Up @@ -260,11 +264,21 @@ def db(self) -> StandardDatabase:
return self._db

@property
def graph_name(self) -> str:
if self._graph_name is None:
def name(self) -> str:
if self.__name is None:
raise GraphNameNotSet("Graph name not set")

return self._graph_name
return self.__name

@name.setter
def name(self, s):
if self.__name is not None:
raise ValueError("Existing graph cannot be renamed")

self.__name = s
m = "Note that setting the graph name does not create the graph in the database" # noqa: E501
logger.warning(m)
nx._clear_cache(self)

@property
def graph_exists_in_db(self) -> bool:
Expand Down Expand Up @@ -317,7 +331,7 @@ def _set_graph_name(self, graph_name: str | None = None) -> None:
if not isinstance(graph_name, str):
raise TypeError("**graph_name** must be a string")

self._graph_name = graph_name
self.__name = graph_name
self._graph_exists_in_db = self.db.has_graph(graph_name)

logger.info(f"Graph '{graph_name}' exists: {self._graph_exists_in_db}")
Expand Down
4 changes: 2 additions & 2 deletions nx_arangodb/classes/multidigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def to_networkx_class(cls) -> type[nx.MultiDiGraph]:

def __init__(
self,
graph_name: str | None = None,
name: str | None = None,
default_node_type: str | None = None,
edge_type_key: str = "_edge_type",
edge_type_func: Callable[[str, str], str] | None = None,
Expand All @@ -37,7 +37,7 @@ def __init__(
**kwargs: Any,
):
super().__init__(
graph_name,
name,
default_node_type,
edge_type_key,
edge_type_func,
Expand Down
4 changes: 2 additions & 2 deletions nx_arangodb/classes/multigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def to_networkx_class(cls) -> type[nx.MultiGraph]:

def __init__(
self,
graph_name: str | None = None,
name: str | None = None,
default_node_type: str | None = None,
edge_type_key: str = "_edge_type",
edge_type_func: Callable[[str, str], str] | None = None,
Expand All @@ -37,7 +37,7 @@ def __init__(
**kwargs: Any,
):
super().__init__(
graph_name,
name,
default_node_type,
edge_type_key,
edge_type_func,
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ def create_line_graph(load_attributes: set[str]) -> nxadb.Graph:

return nxadb.Graph(
incoming_graph_data=G,
graph_name="LineGraph",
name="LineGraph",
edge_collections_attributes=load_attributes,
)
Loading