Create a gist now

Instantly share code, notes, and snippets.

py2neo v3 create nodes and relationships
from py2neo import Node, Graph, Relationship, PropertyDict
from db.types import GraphLabel, GraphRelationship
graph_url = 'http://localhost:7474/db/data/'
graph_db = Graph(graph_url)
# delete everything
graph_db.delete_all()
publication_label = 'PUBLICATION'
paper_label = 'PAPER'
author_label = 'AUTHOR'
publication = {
'label':publication_label,
'properties': {
'title':'publication name',
'topics':['science', 'kittens', 'rainbows']
}
}
publication_node = Node(publication_label, **publication['properties'])
paper_1 = {
'label':paper_label,
'properties': {
'title':'paper 1 title',
'date_published': '9/9/2009'
}
}
paper_1_node = Node(paper_label, **paper_1['properties'])
paper_2 = {
'label':paper_label,
'properties':{
'title':'paper 2 title',
'date_published':'10/10/2010'
}
}
paper_2_node = Node(paper_label, **paper_2['properties'])
author_1 = {
'label':author_label,
'properties':{
'first_name':'First_1',
'last_name': 'Last_1'
}
}
author_1_node = Node(author_label, **author_1['properties'])
author_2 = {
'label':author_label,
'properties':{
'first_name': 'First_2',
'last_name': 'Last_2'
}
}
author_2_node = Node(author_label, **author_2['properties'])
# build up the nodes which are our publications, papers, and authors
# we could do this in a loop
# node_list = [publication, paper_1, paper_2, author_1, author_2]
# for node in node_list:
# new_node = Node(node['label'], **node['properties'])
# print new_node
# graph_db.merge(new_node)
cited = 'CITED'
published = 'PUBLISHED'
authored = 'AUTHORED'
# create publication and paper relationships
published_paper_relationship = Relationship(publication_node, published, paper_1_node)
graph_db.merge(published_paper_relationship)
published_paper_relationship = Relationship(publication_node, published, paper_2_node)
graph_db.merge(published_paper_relationship)
# create paper and author relationships
paper_author_relationship = Relationship(author_1_node, authored, paper_1_node)
graph_db.merge(paper_author_relationship)
paper_author_relationship = Relationship(author_2_node, authored, paper_2_node)
graph_db.merge(paper_author_relationship)
# create paper citation relationships
paper_citation_relationship = Relationship(paper_1_node, cited, paper_2_node)
graph_db.merge(paper_citation_relationship)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment