Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def post_create(self):
""" Called by StructuredNode class on creation of new instance. Will
build relationship from parent to child (this) node.
"""
if self.__parent__ and isinstance(self, StructuredNode):
self.client.create(
(self.__parent__.__node__, self.relationship_type(), self.__node__, {"__child__": True})
)
from neomodel.core import StructuredNode
from neomodel.exceptions import InflateConflict, DeflateConflict
from neomodel.util import _get_node_properties
class SemiStructuredNode(StructuredNode):
"""
A base class allowing properties to be stored on a node that aren't
specified in its definition. Conflicting properties are signaled with the
:class:`DeflateConflict` exception::
class Person(SemiStructuredNode):
name = StringProperty()
age = IntegerProperty()
def hello(self):
print("Hi my names " + self.name)
tim = Person(name='Tim', age=8, weight=11).save()
tim.hello = "Hi"
tim.save() # DeflateConflict
"""
def __eq__(self, other):
if not isinstance(other, (StructuredNode,)):
raise TypeError("Cannot compare neomodel node with a " + other.__class__.__name__)
return self.__node__ == other.__node__
def __contains__(self, obj):
if isinstance(obj, StructuredNode):
if hasattr(obj, 'id'):
return self.query_cls(self).build_ast()._contains(int(obj.id))
raise ValueError("Unsaved node: " + repr(obj))
else:
raise ValueError("Expecting StructuredNode instance")
def __init__(self, source, name, definition):
"""
Create a traversal
"""
self.source = source
if isinstance(source, Traversal):
self.source_class = source.target_class
elif inspect.isclass(source) and issubclass(source, StructuredNode):
self.source_class = source
elif isinstance(source, StructuredNode):
self.source_class = source.__class__
elif isinstance(source, NodeSet):
self.source_class = source.source_class
else:
raise TypeError("Bad source for traversal: "
"{}".format(type(source)))
invalid_keys = (
set(definition) - {'direction', 'model', 'node_class', 'relation_type'}
)
if invalid_keys:
raise ValueError(
'Unallowed keys in Traversal definition: {invalid_keys}'
.format(invalid_keys=invalid_keys)
def children(self, cls):
if isinstance(self, StructuredNode):
child_nodes = [
rel.end_node
for rel in self.__node__.match_outgoing(cls.relationship_type())
if rel["__child__"]
]
return [cls.inflate(node) for node in child_nodes]
else:
return []
def __init__(self, *args, **kwargs):
self.__node__ = None
super(StructuredNode, self).__init__(*args, **kwargs)
elif isinstance(source, NodeSet):
if inspect.isclass(source.source) and issubclass(source.source, StructuredNode):
ident = self.build_label(source.source.__label__.lower(), source.source)
else:
ident = self.build_source(source.source)
self.build_additional_match(ident, source)
if hasattr(source, '_order_by'):
self.build_order_by(ident, source)
if source.filters:
self.build_where_stmt(ident, source.filters)
return ident
elif isinstance(source, StructuredNode):
return self.build_node(source)
else:
raise ValueError("Unknown source type " + repr(source))
def build_source(self, source):
if isinstance(source, Traversal):
return self.build_traversal(source)
elif isinstance(source, NodeSet):
if inspect.isclass(source.source) and issubclass(source.source, StructuredNode):
ident = self.build_label(source.source.__label__.lower(), source.source)
else:
ident = self.build_source(source.source)
self.build_additional_match(ident, source)
if hasattr(source, '_order_by'):
self.build_order_by(ident, source)
if source.filters:
self.build_where_stmt(ident, source.filters)
return ident
elif isinstance(source, StructuredNode):
return self.build_node(source)
else: