Re: [Neo4j] Python Newbie Struggling Slightly

2010-07-30 Thread Peter Neubauer
Tom,
I just installed the python bindings and tried this very simple setup
to create a database and populate it within a transaction:

#!/usr/bin/env python

import sys, random, csv
from time import sleep
from random import randint

import neo4j


graphdb = neo4j.GraphDatabase(db)
with graphdb.transaction:
  person = graphdb.node(name=Person)
  peter = graphdb.node(name=Peter)
  peter.IS_A(person)
  print peter['name']
graphdb.shutdown()


If you try this, is that working for you?

Cheers,

/peter neubauer

COO and Sales, Neo Technology

GTalk:      neubauer.peter
Skype       peter.neubauer
Phone       +46 704 106975
LinkedIn   http://www.linkedin.com/in/neubauer
Twitter      http://twitter.com/peterneubauer

http://www.neo4j.org               - Your high performance graph database.
http://www.thoughtmade.com - Scandinavia's coolest Bring-a-Thing party.



On Tue, Jul 27, 2010 at 5:25 PM, Tom Smith tas...@york.ac.uk wrote:
 Hello,

 I really like the look of neo4j and would like to use it in a project I'm 
 working on http://pppeoplepppowered.blogspot.com/ .

 I'm new to graphs and how to work with them, it's all new, but really drawn 
 to them having banged my head on SQL schemas for years (and years). My 
 problem with working with python neo4j is that there aren't enough simple 
 (enough) examples. This is sort of thing is great ... 
 http://blog.neo4j.org/2010/03/modeling-categories-in-graph-database.html ... 
 but it doesn't explain a few things and goes wrong in places. For example, 
 from the example, if I try to execute...

 computers_node = graphdb.node(Name=Computers)

 ... I get...

 jpype._jexception.RuntimeExceptionPyRaisable: 
 org.neo4j.graphdb.NotInTransactionException: No transaction found for current 
 thread

 So I need to add...

 with db.transaction:
        computers_node = graphdb.node(Name=Computers)

 ... which raises the issue of transactions and database connections. I'm 
 unsure when to use transactions and how to use connections. Either way, I 
 regularly seem to bump into a...

 jpype._jexception.RuntimeExceptionPyRaisable: 
 org.neo4j.kernel.impl.transaction.TransactionFailureException: Could not 
 create data source [nioneodb], see nested exception for cause of error

 Because I like to use the interpreter and learn what objects can do, and 
 later create .py files and import them. It's not clear if I can have more 
 than one db connection open at once, or I should open and shutdown the 
 database everytime... or is it better to have one database connection hanging 
 around in a file somewhere?

 At the moment I'm trying to create a script that, takes a csv file of people, 
 adds them, then tries to get the data out somehow, like this...

 import stuff.neo_utils as neo # see below
 neo.import_people() #import a csv
 7051 #the id of the root_person
 neo.people(7051) #get the people out via the root_person
  #nothing!
neo.people(8224) # the id of the last_person
 Node id=7051

 My question is this... am I doing it all wrong? Could someone create a very 
 simple example that say, populates a graph, gets data out, manipulates that 
 data and then searches that data (say for an attribute, or to see if it 
 exists etc) in a single python file? So that I can begin to build up my 
 understanding,

 thanks for listening,

 tom





 #!/usr/bin/env python

 import sys, random, csv
 from time import sleep
 from random import randint

 import neo4j


 class Person(neo4j.Traversal):
    types = [ neo4j.Outgoing.is_a ]
    order = neo4j.BREADTH_FIRST
    stop = neo4j.STOP_AT_END_OF_GRAPH
    returnable = neo4j.RETURN_ALL_BUT_START_NODE

 def people(person_root_id ):
    try:graphdb.shutdown()
    except:pass

    graphdb = neo4j.GraphDatabase( neo_db )
    with graphdb.transaction:
        person_root = graphdb.node[person_root_id]

        for person_node in Person(person_root):
            try:
                print %s %s (@depth=%s) % ( person_node['family_name'], 
 person_node['email'],person_node.depth)
            except:
                print person_node

    graphdb.shutdown( )

 # The data is like this...
 #tas...@york.ac.uk Staff Mr T Smith  |Computing Service: | |Vanbrugh College 
 V/C/011| |+44 1904 433847|  
 https://www.york.ac.uk/directory/user.yrk/searchdetail.cfm?scope=staffref=M95%27%22%3DYBD8%5B%3ANEJ%27S%27I%2AX%20%20F%2D6Y2%3D%20SR%21A%409%2C%40E2%3D%205%2EFMOM6A%3A%3EWIHV4T%5D%5E%3B%0A%2B4%2D%2A%3EG%2D%2F6EUS%22BI0%20%0Areferrer=searchResults

 def import_people(name=Untitled, 
 file='/Users/tomsmith/pppeoplepppowered/staff/staff.csv', ):
    'load a lot of people into the database, connecting each to a root 
 Person object by a is_a relationship, spurious I know '
    graphdb = neo4j.GraphDatabase( neo_db )
    with graphdb.transaction:
        person_root = graphdb.node(name=Person) # create a root node of sorts
        person_root_id = person_root.id

        csvReader = csv.reader(open(file), delimiter=' ', quotechar='|')
        for row in csvReader:
           

[Neo4j] Python Newbie Struggling Slightly

2010-07-27 Thread Tom Smith
Hello,

I really like the look of neo4j and would like to use it in a project I'm 
working on http://pppeoplepppowered.blogspot.com/ .

I'm new to graphs and how to work with them, it's all new, but really drawn to 
them having banged my head on SQL schemas for years (and years). My problem 
with working with python neo4j is that there aren't enough simple (enough) 
examples. This is sort of thing is great ... 
http://blog.neo4j.org/2010/03/modeling-categories-in-graph-database.html ... 
but it doesn't explain a few things and goes wrong in places. For example, from 
the example, if I try to execute... 

computers_node = graphdb.node(Name=Computers)

... I get...

jpype._jexception.RuntimeExceptionPyRaisable: 
org.neo4j.graphdb.NotInTransactionException: No transaction found for current 
thread

So I need to add...

with db.transaction:
computers_node = graphdb.node(Name=Computers)

... which raises the issue of transactions and database connections. I'm unsure 
when to use transactions and how to use connections. Either way, I regularly 
seem to bump into a...

jpype._jexception.RuntimeExceptionPyRaisable: 
org.neo4j.kernel.impl.transaction.TransactionFailureException: Could not create 
data source [nioneodb], see nested exception for cause of error

Because I like to use the interpreter and learn what objects can do, and later 
create .py files and import them. It's not clear if I can have more than one db 
connection open at once, or I should open and shutdown the database 
everytime... or is it better to have one database connection hanging around in 
a file somewhere?

At the moment I'm trying to create a script that, takes a csv file of people, 
adds them, then tries to get the data out somehow, like this...

 import stuff.neo_utils as neo # see below
 neo.import_people() #import a csv
7051 #the id of the root_person
 neo.people(7051) #get the people out via the root_person
 #nothing!
neo.people(8224) # the id of the last_person
Node id=7051

My question is this... am I doing it all wrong? Could someone create a very 
simple example that say, populates a graph, gets data out, manipulates that 
data and then searches that data (say for an attribute, or to see if it exists 
etc) in a single python file? So that I can begin to build up my understanding,

thanks for listening,

tom





#!/usr/bin/env python

import sys, random, csv
from time import sleep
from random import randint

import neo4j

  
class Person(neo4j.Traversal):
types = [ neo4j.Outgoing.is_a ]
order = neo4j.BREADTH_FIRST
stop = neo4j.STOP_AT_END_OF_GRAPH
returnable = neo4j.RETURN_ALL_BUT_START_NODE

def people(person_root_id ):
try:graphdb.shutdown()
except:pass

graphdb = neo4j.GraphDatabase( neo_db )
with graphdb.transaction:
person_root = graphdb.node[person_root_id]

for person_node in Person(person_root):
try:
print %s %s (@depth=%s) % ( person_node['family_name'], 
person_node['email'],person_node.depth)
except:
print person_node

graphdb.shutdown( )

# The data is like this...
#tas...@york.ac.uk Staff Mr T Smith  |Computing Service: | |Vanbrugh College 
V/C/011| |+44 1904 433847|  
https://www.york.ac.uk/directory/user.yrk/searchdetail.cfm?scope=staffref=M95%27%22%3DYBD8%5B%3ANEJ%27S%27I%2AX%20%20F%2D6Y2%3D%20SR%21A%409%2C%40E2%3D%205%2EFMOM6A%3A%3EWIHV4T%5D%5E%3B%0A%2B4%2D%2A%3EG%2D%2F6EUS%22BI0%20%0Areferrer=searchResults
 

def import_people(name=Untitled, 
file='/Users/tomsmith/pppeoplepppowered/staff/staff.csv', ):
'load a lot of people into the database, connecting each to a root Person 
object by a is_a relationship, spurious I know '
graphdb = neo4j.GraphDatabase( neo_db )
with graphdb.transaction:
person_root = graphdb.node(name=Person) # create a root node of sorts
person_root_id = person_root.id

csvReader = csv.reader(open(file), delimiter=' ', quotechar='|')
for row in csvReader:
email = row[0]
kind = row[1] 
title = row[2]
given_name = row[3]
family_name = row[4]
department = row[5]
org_path = row[6]
full_address = row[7]
telephone = row[8]
src = row[9]
url = row[10]
external_url = row[11]

person = graphdb.node(email=email,title=title,
  given_name=given_name,family_name=family_name,
  telephone=telephone,department=department,
  org_path=org_path,src=src, url=url,
  external_url=external_url)
person.is_a( person_root )
print person, created and linked
print done importing!

graphdb.shutdown( )
return person_root_id













___