groc-prog / pyneo4j-ogm Goto Github PK
View Code? Open in Web Editor NEWA asynchronous Object-Graph-Mapper for Neo4j 5+ and Python 3.10+
License: MIT License
A asynchronous Object-Graph-Mapper for Neo4j 5+ and Python 3.10+
License: MIT License
When passing "*" explicitly as in snippet below, the following error is raised by pydantic:
TypeError: '>=' not supported between instances of 'str' and 'int'
It can be easily prevented by not passing the "*" and it will work correct, but in docs is mentined the possibility to do like this, so i decided to create an issue. I have ideas how to fix that, so i'll try in a few days and if i succeed i'll open a PR.
Simple snippet to reproduce my issue:
import uuid
from pyneo4j_ogm import (
Pyneo4jClient,
NodeModel,
RelationshipProperty,
RelationshipPropertyCardinality,
RelationshipPropertyDirection,
RelationshipModel,
)
class Default(RelationshipModel):
class Settings:
type = "DEFAULT"
class Resource(NodeModel):
...
class Company(NodeModel):
resources: RelationshipProperty["Resource", Default] = RelationshipProperty(
target_model=Resource,
relationship_model=Default,
direction=RelationshipPropertyDirection.OUTGOING,
cardinality=RelationshipPropertyCardinality.ZERO_OR_MORE,
allow_multiple=False,
)
class User(NodeModel):
companies: RelationshipProperty["Company", Default] = RelationshipProperty(
target_model="Company",
relationship_model=Default,
direction=RelationshipPropertyDirection.OUTGOING,
cardinality=RelationshipPropertyCardinality.ZERO_OR_MORE,
allow_multiple=False,
)
async def main():
client = Pyneo4jClient()
await client.connect(
uri="neo4j://localhost:7687", auth=("neo4j", "password"), database="neo4j"
)
await client.register_models([Default, Resource, Company, User])
user = User()
await user.create()
resource = Resource()
await resource.create()
company = Company()
await company.create()
await user.companies.connect(company)
await company.resources.connect(resource)
reachable_resources = await user.find_connected_nodes( # here TypeError is raised
{
"$maxHops": "*",
"$node": {
"$labels": resource._settings.labels,
},
}
)
client.close()
print(reachable_resources)
if __name__ == "__main__":
import asyncio
asyncio.run(main())
Output like below:
[Resource(element_id=4:0db21407-e480-4da7-9c4b-c9ddf43d9694:4, destroyed=False)]
TypeError: '>=' not supported between instances of 'str' and 'int'
I see that memgraph support is on the README -- wondering if any of the contributors to this package know off the top of their head the things that would need to change in this library to enable support for Memgraph.
At the very least, version parsing here needs to be updated (looks like memgraph returns v5
instead of a semver).
I really like and appreciate the use of Pydantic at the core of this library, and would be down to contribute this feature so I dont have to use gqlalchemy.
Hey,
I have tried to implement multiple classes that inherit one from another and tried to connect them by relationships.
package version: >=0.4.0
The example below:
class VehicleNode(NodeModel):
engine: str
class CarNode(VehicleNode):
has_windows: bool
class Consumed(RelationshipModel):
liters_consumed: int
class GasStationNode(NodeModel):
volume_in_litres: int
clients: RelationshipProperty[
VehicleNode, Consumed
] = RelationshipProperty(
target_model=VehicleNode,
relationship_model=Consumed,
direction=RelationshipPropertyDirection.OUTGOING,
cardinality=RelationshipPropertyCardinality.ZERO_OR_MORE,
allow_multiple=True
)
I tried to connect a CarNode
and a GasStationNode
with a basic relationship like the example below:
client: Pyneo4jClient # Already initialized
await client.register_models([VehicleNode, CarNode, GasStationNode, Consumed])
car = await CarNode(engine='v8', has_windows=True).create()
gas_station = await GasStationNode(volume_in_litres=6000).create()
# Trying to connect them by basic relationship
await gas_station.clients.connect(car, { 'liters_consumed': 50 })
The operation failed with an exception.
Exception print:
self = RelationshipProperty(target_model_name=VehicleNode, relationship_model=Consumed, direction=OUTGOING, cardinality=ZERO_OR_MORE, allow_multiple=True)
nodes = CarNode(element_id=4:218fdb95-3060-4d65-ab59-de3bc9a5722f:1, destroyed=False)
def _ensure_alive(self, nodes: Union[T, List[T]]) -> None:
"""
Ensures that the provided nodes are alive.
Args:
nodes (T | List[T]): Nodes to check for hydration and alive.
Raises:
InstanceNotHydrated: Raised if a node is not hydrated yet.
InstanceDestroyed: Raised if a node has been marked as destroyed.
"""
nodes_to_check = nodes if isinstance(nodes, list) else [nodes]
for node in nodes_to_check:
logger.debug(
"Checking if node %s is alive and of correct type",
getattr(node, "_element_id", None),
)
if getattr(node, "_element_id", None) is None or getattr(node, "_id", None) is None:
raise InstanceNotHydrated()
if getattr(node, "_destroyed", True):
raise InstanceDestroyed()
if cast(Type[T], self._target_model).__name__ != node.__class__.__name__:
> raise InvalidTargetNode(
expected_type=cast(Type[T], self._target_model).__name__,
actual_type=node.__class__.__name__,
)
E pyneo4j_ogm.exceptions.InvalidTargetNode: Expected target node to be of type VehicleNode, but got CarNode
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.