Skip to content
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

added initial support for numpy ndarrays and scalars #208

Merged
merged 2 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
4 changes: 4 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,9 @@
({"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", "2", "3"]), "['1', '2', '3']"),
(np.array([[1, 2], [3, 4], [4, 5]]), "[[1, 2], [3, 4], [4, 5]]"),
Josipmrden marked this conversation as resolved.
Show resolved Hide resolved
],
)
def test_to_cypher_value(value, cypher_value):
Expand Down