Skip to content

Commit

Permalink
new: make edge keyification optional (#103)
Browse files Browse the repository at this point in the history
* new: make edge keyification optional

* fix: mypy

* fix: mypy (support 3.8)
  • Loading branch information
aMahanna authored Jan 6, 2025
1 parent 819ea52 commit 2a630f8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions adbnx_adapter/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-

from abc import ABC
from typing import Any, Dict, List, Optional, Set
from typing import Any, Dict, List, Optional, Set, Union

from arango.graph import Graph as ADBGraph
from networkx.classes.graph import Graph as NXGraph
Expand Down Expand Up @@ -92,7 +92,7 @@ def _keyify_networkx_edge(
to_node_id: NxId,
nx_map: Dict[NxId, str],
col: str,
) -> str:
) -> Union[str, None]:
raise NotImplementedError # pragma: no cover

def _prepare_networkx_node(
Expand Down
12 changes: 8 additions & 4 deletions adbnx_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,10 @@ def __process_nx_node(
key = self.__cntrl._keyify_networkx_node(i, nx_id, nx_node, col)

nx_node["_key"] = key
nx_map[nx_id] = f"{col}/{key}"

_id = f"{col}/{key}"
if _id != nx_id:
nx_map[nx_id] = _id

self.__cntrl._prepare_networkx_node(nx_node, col)
adb_docs[col].append(nx_node)
Expand Down Expand Up @@ -702,9 +705,10 @@ def __process_nx_edge(
col,
)

nx_edge["_key"] = key
nx_edge["_from"] = nx_map[from_node_id]
nx_edge["_to"] = nx_map[to_node_id]
nx_edge["_from"] = nx_map.get(from_node_id, from_node_id)
nx_edge["_to"] = nx_map.get(to_node_id, to_node_id)
if key:
nx_edge["_key"] = key

self.__cntrl._prepare_networkx_edge(nx_edge, col)
adb_docs[col].append(nx_edge)
Expand Down
10 changes: 5 additions & 5 deletions adbnx_adapter/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Dict, List, Tuple

from .abc import Abstract_ADBNX_Controller
from .typings import Json, NxData, NxId
from .typings import Json, NxData, NxId, Union


class ADBNX_Controller(Abstract_ADBNX_Controller):
Expand Down Expand Up @@ -140,9 +140,9 @@ def _keyify_networkx_edge(
to_node_id: NxId,
nx_map: Dict[NxId, str],
col: str,
) -> str:
) -> Union[str, None]:
"""Given a NetworkX edge, its collection, and its pair of nodes, derive
its ArangoDB key.
its ArangoDB key. If None is returned, an auto-generated key will be used.
NOTE #1: You must override this function if you want to create custom ArangoDB
_key values for your NetworkX edges.
Expand All @@ -169,8 +169,8 @@ def _keyify_networkx_edge(
i.e, nx_map[from_node_id] will give you the ArangoDB _from value,
and nx_map[to_node_id] will give you the ArangoDB _to value.
:type nx_map: Dict[NxId, str]
:return: A valid ArangoDB _key value.
:rtype: str
:return: A valid ArangoDB _key value, or None if you want an auto-generated key.
:rtype: str | None
"""
return str(i)

Expand Down

0 comments on commit 2a630f8

Please sign in to comment.