Skip to content

Instantly share code, notes, and snippets.

@chrisvest
Last active December 27, 2015 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvest/7304432 to your computer and use it in GitHub Desktop.
Save chrisvest/7304432 to your computer and use it in GitHub Desktop.
Notes from my talk on Py2Neo.

Notes from my talk on Py2Neo.

Using http://book.py2neo.org/en/latest/ and Neo4j 2.0-M06.

install

pip install py2neo

connect

from py2neo import neo4j
db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")

create nodes & rels

from py2neo import node, rel
skills = db.create(
    node(first_name="Andres", last_name="Taylor"),
    node(skill_name="Scala"),
    rel(0, "KNOWS_SKILL", 1)
)

peeking at stuff

andres = skills[0]
list(andres.match(rel_type='KNOWS_SKILL'))
r = list(andres.match(rel_type='KNOWS_SKILL'))[0]
r.type
r.end_node['skill_name']
list(andres.match(rel_type='KNOWS_SKILL'))[0].end_node['skill_name'] = 'Neo'

queries

db.node_labels
find_movies = neo4j.CypherQuery(db, "match (m:Movie) return m")
[m[0]['title'] for m in find_movies.execute()]

find_person = neo4j.CypherQuery(db, "match (p:Person) where p.name = {name} return p")
emil = [p[0] for p in find_person.execute(name="Emil Eifrem")][0]
[r.end_node['title'] for r in emil.match(rel_type='ACTED_IN')]
map(lambda r:r.delete(), emil.match(rel_type='ACTED_IN'))

paths

alice, bob, carol = node(name="Alice"), node(name="Bob"), node(name="Carol")
abc = neo4j.Path(alice, "KNOWS", bob, "KNOWS", carol)

dave, eve = node(name="Dave"), node(name="Eve")
de = neo4j.Path(dave, "KNOWS", eve)
de.nodes

abcde = neo4j.Path.join(abc, "KNOWS", de)
str(abcde)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment