def initNeo4jConnection(self, config): """Connect to a Neo4j Server config.URL is expected to be the URL to a connectable Server. For example: http://localhost:7474/db/data/ """ try: self.neo4jConnection = cypher.Session(config['URL']) except (neo4j.ClientError, neo4j.ServerError) as e: print "Can not connect to Neo4j: %s" % str(e)
def main(): reload(sys); sys.setdefaultencoding("utf8") myhome="/usr/local/twm" logging.basicConfig(filename=myhome+'/logs/thw.log',level=logging.DEBUG) session = cypher.Session("http://localhost:7474") tx = session.create_transaction() fh=open(sys.argv[1],"r") fhlines=fh.readlines()
def main(): # Abraham Bloemaert;1566;25-12-1566;1566-12-25;1651;27-01-1651;1651-01-27;Hollandsk;original # Andrea Proccaccini;(?);(?);(?);(?);(?);(?);(?);original #Abraham de Haen d.Y.;1707 - 1748; Hollandsk #Abraham de Verwer;Ca. 1585 - 1650; Hollandsk #Michael Kvium;15-11-1955 - (?); Dansk # MATCH (y:Year { year:1860}) MERGE (a:Artist {name:'Alphonse Mucha',nation:'Tjekkisk'}) CREATE (a)-[:BORN]->(y); # MATCH (y:Year { year:1894}), (a:Artist {name:'Alphonse Mucha'}) CREATE (a)-[:DIED]->(y); # MATCH (x) WHERE HAS (x.x) MERGE (y { y:"y" }) CREATE (y)-[:REL]->(x); # MATCH (y:Year { year:1858}), (x:Year {year:1895}) MERGE (a:Artist {name:'Axel Schmidt',nation:'Dansk'}) CREATE (a)-[:BORN]->(y), (a)-[:DIED]->(x); reload(sys); sys.setdefaultencoding("utf8") myhome="/home/thw" artistList=[] limit=1777 dummyB=4321 dummyD=4391 logging.basicConfig(filename=myhome+'/logs/thw.log',level=logging.DEBUG) session = cypher.Session("http://192.168.10.31:7474") fh=open(sys.argv[1],"r") fhlines=fh.readlines() for line in fhlines: tx = session.create_transaction() cyP="MERGE " cyAr=" (a:Artist {name:'" cyBE=" CREATE (a)-[:BORN]->(y)" cyDE=" (a)-[:DIED]->(x)" cyA="MATCH (y:Year { year:" logging.debug(line) tmpLine=line.split(";") artist=doName(tmpLine.pop(0).rstrip()) if (re.search('ubekendt',artist,re.IGNORECASE)): logging.debug('unkown ' + line) continue if artist in artistList: logging.debug('Already done: ' + artist) continue #Abraham de Verwer;Ca. 1585 - 1650; Hollandsk print "A:artist:" + artist cyAr = cyAr + ("%s'," % artist) artistList.append(artist) nation=tmpLine.pop().rstrip() print "A:nation:" + nation cyAr = cyAr + ("nation:'%s'})" % nation)
def main(): reload(sys) sys.setdefaultencoding("utf8") myhome = "/usr/local/twm" logging.basicConfig(filename=myhome + '/logs/thw.log', level=logging.DEBUG) session = cypher.Session("http://*****:*****@") tmpacq_date = tmpLine.pop().rstrip() acq_date = re.sub('[^0-9-]', '', tmpacq_date) print "D:acq:" + acq_date lateD = tmpLine.pop() print "D:lateD:" + lateD earlD = tmpLine.pop() print "D:earlD:" + earlD title = tmpLine.pop(0) print "T:title: " + title artist = tmpLine.pop().rstrip() print "A:artist:" + artist realDate = doDate(earlD, lateD) print "RD:realDate:" + realDate print "\n" # send three statements to for execution but leave the transaction open tx.append("MERGE (a:Person {name:'Alice'}) " "RETURN a") tx.append("MERGE (b:Person {name:'Bob'}) " "RETURN b") tx.append("MATCH (a:Person), (b:Person) " "WHERE a.name = 'Alice' AND b.name = 'Bob' " "CREATE UNIQUE (a)-[ab:KNOWS]->(b) " "RETURN ab") #tx.execute() # send another three statements and commit the transaction tx.append("MERGE (c:Person {name:'Carol'}) " "RETURN c") tx.append("MERGE (d:Person {name:'Dave'}) " "RETURN d") tx.append("MATCH (c:Person), (d:Person) " "WHERE c.name = 'Carol' AND d.name = 'Dave' " "CREATE UNIQUE (c)-[cd:KNOWS]->(d) " "RETURN cd")
def initialize(self): self.session = cypher.Session(self.url) self.regexp = re.compile(ur'\"([^"]*?)\"\:') if self.truncateLabel and self.resourceType == 'node': tx = self.session.create_transaction() cmd1 = 'MATCH (n:`%s`)-[r]-() DELETE n, r' % (self.label) tx.append(cmd1) cmd2 = 'MATCH (c:`%s`) DELETE c' % (self.label) tx.append(cmd2) tx.commit() elif self.truncateLabel and self.resourceType == 'relation': tx = self.session.create_transaction() cmd = 'MATCH (a)-[r:`%s`]-(b) DELETE r' % (self.label) tx.append(cmd) tx.commit() return super(Neo4jTarget, self).initialize()
def begin(self): if hasattr(self, 'tx_session'): raise SystemError("Transaction already in progress") self.tx_session = py2neo_cypher.Session(self.url).create_transaction()
# # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import unicode_literals import pytest from py2neo import cypher try: session = cypher.Session() except NotImplementedError: supports_transactions = False else: supports_transactions = True @pytest.mark.skipif(not supports_transactions, reason="Transactions not supported by this server version") def test_can_execute_single_statement_transaction(): tx = session.create_transaction() assert not tx.finished tx.append("CREATE (a) RETURN a") results = tx.commit() assert len(results) == 1 for result in results:
def session(request): session = cypher.Session() return session
from py2neo import cypher, neo4j from progressbar import Percentage, Bar, RotatingMarker, ETA, ProgressBar session = cypher.Session('http://localhost:7474') source = 2389 end = 5116 widgets = [ 'Progress: ', Percentage(), ' ', Bar(marker=RotatingMarker()), ' ', ETA(), ' ' ] ## OPTIONS: Stop when path found, Stop at depth Found, Full Depth #def Dijkstra(graph_db, source): # source id# for prop in ['dist', 'on_path']: tx = session.create_transaction() tx.append("MATCH (n) WHERE HAS(n.%s) REMOVE n.%s" % (prop, prop)) dummy = tx.commit() ### Source Node tx = session.create_transaction() tx.append("start n = node(%i) SET n.dist=0" % source) dummy = tx.commit() for dist in range(0, 11): tx = session.create_transaction() tx.append(
from py2neo import cypher session = cypher.Session("http://localhost:7474") tx = session.create_transaction() # send three statements to for execution but leave the transaction open tx.append("MERGE (a:Person {name:'Alice'}) " "RETURN a") tx.append("MERGE (b:Person {name:'Bob'}) " "RETURN b") tx.append("MATCH (a:Person), (b:Person) " "WHERE a.name = 'Alice' AND b.name = 'Bob' " "CREATE UNIQUE (a)-[ab:KNOWS]->(b) " "RETURN ab") tx.execute() # send another three statements and commit the transaction tx.append("MERGE (c:Person {name:'Carol'}) " "RETURN c") tx.append("MERGE (d:Person {name:'Dave'}) " "RETURN d") tx.append("MATCH (c:Person), (d:Person) " "WHERE c.name = 'Carol' AND d.name = 'Dave' " "CREATE UNIQUE (c)-[cd:KNOWS]->(d) " "RETURN cd") tx.commit()
def main(): # Abraham Bloemaert;1566;25-12-1566;1566-12-25;1651;27-01-1651;1651-01-27;Hollandsk;original # Andrea Proccaccini;(?);(?);(?);(?);(?);(?);(?);original # MATCH (y:Year { year:1860}) MERGE (a:Artist {name:'Alphonse Mucha',nation:'Tjekkisk'}) CREATE (a)-[:BORN]->(y); # MATCH (y:Year { year:1894}), (a:Artist {name:'Alphonse Mucha'}) CREATE (a)-[:DIED]->(y); # MATCH (x) WHERE HAS (x.x) MERGE (y { y:"y" }) CREATE (y)-[:REL]->(x); # MATCH (y:Year { year:1858}), (x:Year {year:1895}) MERGE (a:Artist {name:'Axel Schmidt',nation:'Dansk'}) CREATE (a)-[:BORN]->(y), (a)-[:DIED]->(x); reload(sys); sys.setdefaultencoding("utf8") myhome="/usr/local/twm" artistList=[] limit=1777 dummyB=4321 dummyD=4391 logging.basicConfig(filename=myhome+'/logs/thw.log',level=logging.DEBUG) session = cypher.Session("http://*****:*****@") tmpacq_date=tmpLine.pop().rstrip() acq_date=re.sub('[^0-9-]','',tmpacq_date) print "D:acq:" + acq_date lateD=tmpLine.pop() print "D:lateD:" + lateD earlD=tmpLine.pop() print "D:earlD:" + earlD title=tmpLine.pop(0) print "T:title: " + title artist=tmpLine.pop().rstrip() print "A:artist:" + artist realDate=doDate(earlD,lateD) print "RD:realDate:" + realDate print "\n" # ---------- # MATCH (a:Artist { name:'Kasper Heiberg'}),(y:Year {year:1922}),(x:Year {year:1999}) MERGE (w:Artwork {title:'Axel Schmidt'}) CREATE (a)-[:PAINTED]->(w), (w)-[:CRY]->(y), w-[:ACQY]->(x); cyP="MERGE " cyAr=" (w:Artwork {title:'" cyPA=" CREATE (a)-[:PAINTED]->(w)" cyPD=" (w)-[:CRY]->(y)" cyAD=" (w)-[:ACQY]->(x)" cyA="MATCH (a:Artist { year:" logging.debug(line) tmpLine=line.split(";") artist=doName(tmpLine.pop(0).rstrip()) if (re.search('ubekendt',artist,re.IGNORECASE)): logging.debug('unkown ' + line) continue if artist in artistList: logging.debug('Already done: ' + artist) continue print "A:artist:" + artist cyAr = cyAr + ("%s'," % artist) artistList.append(artist) trash=tmpLine.pop() nation=tmpLine.pop().rstrip() print "A:nation:" + nation cyAr = cyAr + ("nation:'%s'})" % nation) # BIRTH tmpbirthY=tmpLine.pop(0).rstrip() if not tmpbirthY.find("(?)"): logging.debug("unknown data on %s" % artist) continue birthY=re.sub('[^0-9-]','',tmpbirthY) try: birthYint=int(birthY) except ValueError: birthYint=dummyB; print "D:BY:" + birthY if (birthYint < limit): logging.debug("too old %s" % birthY) continue cyA = cyA + ("%s}) " % birthY) #cyA="MATCH (y:Year { year:" #cyF = cyA + cyP + cyAr + cyBE tmpbirthD=tmpLine.pop(0).rstrip() birthD=re.sub('[^0-9-]','',tmpbirthD) print "D:BD:" + birthD # DEATH # MATCH (y:Year { year:1858}), (x:Year {year:1895}) MERGE (a:Artist {name:'Axel Schmidt',nation:'Dansk'}) CREATE (a)-[:BORN]->(y), (a)-[:DIED]->(x); cyB="(x:Year { year:" trash=tmpLine.pop(0).rstrip() tmpDeathY=tmpLine.pop(0).rstrip() if not tmpDeathY.find("(?)"): logging.debug("still alive %s" % artist) continue deathY=re.sub('[^0-9-]','',tmpDeathY) print "D:DY:" + deathY if (deathY < limit): logging.debug("too old %s" % deathY) continue cyB = cyB + ("%s}) " % deathY) cyF = cyA + ", " + cyB + cyP + cyAr + cyBE + ", " + cyDE + ";" tmpDeathD=tmpLine.pop(0).rstrip() deathD=re.sub('[^0-9-]','',tmpDeathD) print "D:DD:" + deathD print "cyNF:" + cyF tx.append(cyF) tx.execute() tx.commit()