Skip to content

Commit

Permalink
Added Support for numpy ndarrays and scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisTasevski committed Dec 2, 2022
1 parent 0f850bf commit 96bed59
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion gqlalchemy/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.

import math
import numpy as np

from datetime import datetime, date, time, timedelta
from enum import Enum
from typing import Any, Dict, List, Optional, Tuple, Union
Expand Down Expand Up @@ -88,13 +90,16 @@ def to_cypher_value(value: Any, config: NetworkXCypherConfig = None) -> str:
if value_type in [int, float, bool]:
return str(value)

if value_type in [list, set, tuple]:
if value_type in [list, set, tuple, np.ndarray]:
return f"[{', '.join(map(to_cypher_value, value))}]"

if value_type == dict:
lines = ", ".join(f"{k}: {to_cypher_value(v)}" for k, v in value.items())
return f"{{{lines}}}"

if isinstance(value, np.generic):
return to_cypher_value(value.item())

if value is None:
return "null"

Expand Down
5 changes: 5 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import datetime
import math
import numpy as np
import pytest

from gqlalchemy.utilities import (
Expand All @@ -37,6 +38,10 @@
({"k1": 123, "k2": {"subkey1": "abc"}}, "{k1: 123, k2: {subkey1: 'abc'}}"),
(None, "null"),
(True, "True"),
(np.array([1, 2, 3]), "[1, 2, 3]"),
(np.array([1.23, 2.34, 3.45]), "[1.23, 2.34, 3.45]"),
(np.array(["1", "2", "3"]), "['1', '2', '3']"),
(np.array([[1, 2], [3, 4], [4, 5]]), "[[1, 2], [3, 4], [4, 5]]"),
],
)
def test_to_cypher_value(value, cypher_value):
Expand Down

0 comments on commit 96bed59

Please sign in to comment.