本文整理汇总了Python中py2neo.Graph类的典型用法代码示例。如果您正苦于以下问题:Python Graph类的具体用法?Python Graph怎么用?Python Graph使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Graph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: createRelationshipWithProperties
def createRelationshipWithProperties():
print("Start - Creating Relationships")
# Authenticate the user using py2neo.authentication
# Ensure that you change the password 'sumit' as per your database configuration.
py2neo.authenticate("localhost:7474", "neo4j", "sumit")
# Connect to Graph and get the instance of Graph
graph = Graph("http://localhost:7474/db/data/")
# Create Node with Properties
amy = Node("FEMALE", name="Amy")
# Create one more Node with Properties
kristine = Node("FEMALE",name="Kristine")
# Create one more Node with Properties
sheryl = Node("FEMALE",name="Sheryl")
#Define an Object of relationship which depicts the relationship between Amy and Kristine
#We have also defined the properties of the relationship - "since=2005"
#By Default the direction of relationships is left to right, i.e. the -->
kristine_amy = Relationship(kristine,"FRIEND",amy,since=2005)
#This relationship is exactly same as the earlier one but here we are using "Rev"
#"py2neo.Rev = It is used to define the reverse relationship (<--) between given nodes
amy_sheryl=Relationship(amy,Rev("FRIEND"),sheryl,since=2001)
#Finally use graph Object and Create Nodes and Relationship
#When we create Relationship between, then Nodes are also created.
resultNodes = graph.create(kristine_amy,amy_sheryl)
#Print the results (relationships)
print("Relationship Created - ",resultNodes)
示例2: Neo4j
class Neo4j():
graph = None
def __init__(self):
print("create neo4j class ...")
def connectDB(self):
self.graph = Graph("http://localhost:7474", username="neo4j", password="8313178")
print('connect successed')
def matchItembyTitle(self,value):
answer = self.graph.find_one(label="Item",property_key="title",property_value=value)
return answer
# 根据title值返回互动百科item
def matchHudongItembyTitle(self,value):
answer = self.graph.find_one(label="HudongItem",property_key="title",property_value=value)
return answer
# 返回限定个数的互动百科item
def getAllHudongItem(self, limitnum):
List = []
ge = self.graph.find(label="HudongItem", limit=limitnum)
for g in ge:
List.append(HudongItem(g))
print('load AllHudongItem over ...')
return List
#test = Neo4j()
#test.connectDB()
#a = test.getLabeledHudongItem('labels.txt')
#print(a[10].openTypeList)
示例3: computeShortestPathCoherence
def computeShortestPathCoherence(node1, node2, w):
"""Connects to graph database, then creates and sends query to graph
database. Returns the shortest path between two nodes.
Format: (67149)-[:'LINKS_TO']->(421)"""
if node1.strip()==node2.strip():
return w
fromCache=rds.get("%s:%s" % (node1, node2))
if fromCache:
return float(fromCache)*w
else:
g = Graph()
q="MATCH path=shortestPath((m:Page {name:\"%s\"})-[LINKS_TO*1..10]-(n:Page {name:\"%s\"})) RETURN LENGTH(path) AS length, path, m, n" % (node1, node2)
cursor=g.run(q)
path=None
for c in cursor:
path=c
#
if path:
rds.set("%s:%s" % (node1, node2), 1/path["length"])
rds.set("%s:%s" % (node2, node1), 1/path["length"])
return w/path["length"]
else:
rds.set("%s:%s" % (node1, node2), 0.0)
rds.set("%s:%s" % (node2, node1), 0.0)
return 0.0
示例4: get_track_comments
def get_track_comments():
track_comments = {}
graph = Graph()
for comment in graph.find("Comment"):
track_comments[comment.properties['id']] = inflate(comment.properties)
return track_comments
示例5: sync_meetup_data
def sync_meetup_data(group):
graph = Graph(host=config['neo4j']['host'], user=config['neo4j']['user'],
password=config['neo4j']['password'])
location = get_group_location(group)
tx = graph.begin()
location_node = Node('Location', city=location['city'], state=location['state'], country=location['country'])
tx.create(location_node)
tx.commit()
meetup_groups = get_groups_in_location(location, category=34)
logger.info('Finding upcoming meetup events at {} meetup groups'.format(len(meetup_groups)))
for group in meetup_groups:
time.sleep(2)
group, events = get_group_events(group)
tx = graph.begin()
group_node = Node("Group", name=group)
tx.create(group_node)
location_relation = Relationship(location_node, 'HAS MEETUP', group_node)
tx.create(location_relation)
for event in events:
event_node = Node('Event', name=event['name'], time=event['time'])
tx.create(event_node)
rel = Relationship(group_node, "HAS EVENT", event_node)
tx.create(rel)
tx.commit()
logger.info('Transaction ({}) status: {}'.format(group, str(tx.finished())))
示例6: __init__
class Grapher:
def __init__(self):
self.graph = Graph()
"""Accepts a dict of scanning results and adds the server, its ports and vulerabilities in Neo4jDB"""
def plot_scan_results(self, res):
for host in res.keys():
hostname = res[host][KEY_HOSTNAME]
server = Node(SERVER, id=host, address=host, hostname=hostname)
for attr in res[host].keys():
if attr not in top_keys:
for portno in res[host][attr]:
if res[host][attr][portno].get(KEY_STATE, "closed") == OPEN:
product = res[host][attr][portno][KEY_PRODUCT]
version = res[host][attr][portno][KEY_VERSION]
cpe = res[host][attr][portno][KEY_CPE]
vulnerabilities = res[host][attr][portno][KEY_VULNERABILITIES]
port = Node(PORT, id=portno, number=portno, protocol=attr, product=product, version=version, cpe=cpe, state=OPEN)
server_has_port = Relationship(server, HAS, port)
self.graph.create(server_has_port)
for vulnerability in vulnerabilities:
published = vulnerability[KEY_PUBLISHED]
cve = vulnerability[KEY_CVE]
summary = vulnerability[KEY_SUMMARY]
vuln = Node(VULNERABILITY, id=cve, cve=cve, summary=summary, published=published)
port_has_vuln = Relationship(port, HAS, vuln)
self.graph.create(port_has_vuln)
示例7: createRelationships
def createRelationships():
global relationships
graph = Graph('http://localhost:7474/db/data')
for r in relationships:
NodeA = graph.find_one(r["start"]["collection"],property_key = "_id", property_value = str(r["start"]["_id"]))
NodeB = graph.find_one(r["end"]["collection"],property_key = "_id", property_value = str(r["end"]["_id"]))
graph.create(rel(NodeA,r["name"],NodeB))
示例8: __init__
class Neo4jConnector:
_graph_connection = None
def __init__(self, connectionString=None):
# set up authentication parameters
authenticate("localhost:7474", "neo4j", "123")
self.connectionString = "http://localhost:7474/db/data/"
self._graph_connection = Graph(connectionString)
self.error_file = open("Dump/log/dberror.txt", "a")
return
def createNodes(self, wikiPage):
print ("creating node %s" %wikiPage.getTitle())
try:
if self._graph_connection is not None:
alice = Node("article2",name = wikiPage.title,content= wikiPage.getContent())
self._graph_connection.create(alice)
else:
self.error_file.write("create node failed: connection not avaialable".encode('utf-8'))
print 'create node failed: connection not avaialable'
return
except Exception, e:
self.error_file.write('Search failed: {%s} { %s } \n' % (wikiPage.getTitle(), e.message))
print 'create node failed: %s %s' % (e.message,e.args)
pass
示例9: SocialFormationLoader
class SocialFormationLoader(object):
def __init__(self):
authenticate("localhost:7474", "neo4j", "1111")
self.graph_db = Graph(graph_DB_url)
self.api_url = "http://127.0.0.1:9000/api/v1/socialformation/?format=json"
self.statement = "MERGE (n:SocialFormation {id: {ID}, Name:{N}, DateReg:{DR}, RegNumb:{RN}}) RETURN n"
def whipUp(self):
objects = json.load(urlopen(self.api_url))["objects"]
for object in objects:
args = {}
args["ID"]=object["id"]
args["N"]=object["Name"]
args["DR"]=object["DateReg"]
args["RN"]=object["RegNumb"]
#print args
db = self.graph_db.cypher.begin()
db.append(self.statement, args)
db.commit()
sf = getSocialFormation(args["ID"])
adr = getAddress(id = int(object["Address"]["id"]), Street = object["Address"]["Street"])
#per = getPerson(id = int(object["Person"]["id"]), Name = (object["Person"]["Surname"] + " " + object["Person"]["Name"]))
per = getPerson(Name = (object["Person"]["Surname"] + " " + object["Person"]["Name"]))
if adr != None:
self.graph_db.create(rel(sf.one, "HAVE_ADDRESS", adr.one))
if per != None:
self.graph_db.create(rel(sf.one, "SF_HAVE_PERSON", per.one))
示例10: get_tracks
def get_tracks():
track_metadata = {}
graph = Graph()
for track in graph.find("Track"):
track_metadata[track.properties['id']] = inflate(track.properties)
return track_metadata
示例11: Graph
class Graph(object):
def __init__(self, neo4j_uri):
self.graph = NeoGraph(neo4j_uri)
def find_node(self, label, node_id):
args = dict(property_key="node_id", property_value=node_id)
return self.graph.find_one(label, **args)
def create_user(self, args):
node = self.find_node("User", args["username"])
if not node:
properties = dict(
node_id=args["username"],
name=args["name"],
city=args["city"]
)
node = Node("User", **properties)
self.graph.create(node)
return node, True
return node, False
def delete_user(self, user):
node = self.find_node("User", user)
if node:
self.graph.delete(node)
return True
return False
示例12: PersonLoader
class PersonLoader(object):
def __init__(self):
authenticate("localhost:7474", "neo4j", "1111")
self.graph_db = Graph(graph_DB_url)
self.api_url = "http://127.0.0.1:8000/api/v1/person/?format=json"
self.statement = "MERGE (n:Person {Name:{N}, Identification:{I}, id: {ID}, NonResidentForeigner:{NR}," \
"MoreInformation:{MI}}) RETURN n"
def whipUp(self):
objects = json.load(urlopen(self.api_url))["objects"]
for object in objects:
args = {}
args["ID"]=object["id"]
args["I"]=object["Identification"]
args["NR"]=object["NonResidentForeigner"]
args["N"]=object["Name"]
args["MI"]=object["MoreInformation"]
#args["AD"]=object["Address"]["id"]
perCh = getPerson(Name=args["N"])
if perCh!=None:
continue
db = self.graph_db.cypher.begin()
db.append(self.statement, args)
db.commit()
address = getAddress(id=int(object["Address"]["id"]))
person = getPerson(id=int(args["ID"]))
self.graph_db.create(rel(person.one, "LIVED", address.one))
示例13: make_sequence
def make_sequence(self):
authenticate(settings.NeoHost, settings.NeoLog, settings.NeoPass)
graph = Graph("{0}/db/data/".format(settings.NeoHost))
query = """MATCH (start:Video)-[:Jaccard*5..10]->(sequence:Video)
WHERE start<>sequence MATCH p=shortestPath((start:Video)-[:Jaccard*]->(sequence:Video))
WHERE NONE (n IN nodes(p) WHERE size(filter(x IN nodes(p) WHERE n = x))> 1)
RETURN EXTRACT(n IN NODES(p)|[n.id, n.rating]) LIMIT 100000"""
r1 = graph.run(query).data()
k = 0
for i in r1:
#print(i.values)
for video in i['EXTRACT(n IN NODES(p)|[n.id, n.rating])']:
#print(video)
self.seq_ids.append(k)
self.video_ids.append(video[0])
self.ratings.append(video[1])
k+=1
data = {'sequence': self.seq_ids, 'video': self.video_ids, 'rating': self.ratings}
df = pd.DataFrame(data)
df = df[pd.notnull(df['video'])]
print(df)
dz = df.groupby('sequence')['rating'].std()
print(dz)
path = '{0}/{1}/'.format(settings.VideosDirPath, self.game)
if not os.path.exists(path):
os.makedirs(path)
file_name = '{0}/sequences.csv'.format(path)
df.to_csv(file_name, encoding='utf-8')
summary_data = '{0}/summary.csv'.format(path)
dz.to_csv(summary_data, encoding='utf-8')
return
示例14: handle
def handle(self, *args, **options): # pylint: disable=unused-argument
"""
Iterates through each course, serializes them into graphs, and saves
those graphs to neo4j.
"""
# first, make sure that there's a valid neo4j configuration
if settings.NEO4J_CONFIG is None:
raise CommandError(
"No neo4j configuration (NEO4J_CONFIG) defined in lms.auth.json."
)
auth_params = ["{host}:{https_port}", "{user}", "{password}"]
authenticate(*[param.format(**settings.NEO4J_CONFIG) for param in auth_params])
graph = Graph(**settings.NEO4J_CONFIG)
mss = ModuleStoreSerializer()
total_number_of_courses = len(mss.all_courses)
for index, course in enumerate(mss.all_courses):
# first, clear the request cache to prevent memory leaks
RequestCache.clear_request_cache()
log.info(
"Now exporting %s to neo4j: course %d of %d total courses",
course.id,
index + 1,
total_number_of_courses
)
nodes, relationships = mss.serialize_course(course.id)
log.info(
"%d nodes and %d relationships in %s",
len(nodes),
len(relationships),
course.id
)
transaction = graph.begin()
try:
# first, delete existing course
transaction.run(
"MATCH (n:item) WHERE n.course_key='{}' DETACH DELETE n".format(
six.text_type(course.id)
)
)
# now, re-add it
self.add_to_transaction(nodes, transaction)
self.add_to_transaction(relationships, transaction)
transaction.commit()
except Exception: # pylint: disable=broad-except
log.exception(
"Error trying to dump course %s to neo4j, rolling back",
six.text_type(course.id)
)
transaction.rollback()
示例15: GraphImporter
class GraphImporter(object):
def __init__(self, graphurl, commitEvery=100):
self.graph = Graph(graphurl)
self.commitEvery = commitEvery
self._act = None
self._actC = commitEvery
def delete_all(self):
self.graph.delete_all()
self.graph.cypher.run('CREATE INDEX ON :_Network_Node(id)')
self.graph.cypher.run('CREATE INDEX ON :_Set_Node(id)')
def _tx(self):
if self._act is not None:
return self._act
self._act = self.graph.cypher.begin()
self._actC = self.commitEvery
return self._act
def _done(self):
self._actC -= 1
if self._actC == 0: # commit
self._act.process()
self._act.commit()
sys.stdout.write('.')
self._act = None
def _close(self):
if self._act is not None: # commit last tx
self._actC = 1
self._done()
def add_node(self, labels, node_id, properties):
tx = self._tx()
add_node(tx, labels, node_id, properties)
self._done()
def done_nodes(self):
self._done()
def append(self, query):
tx = self._tx()
tx.append(query)
self._done()
def add_edge(self, label, source_node_id, target_node_id, properties, source_type=u'_Network_Node', update_only=False):
tx = self._tx()
add_edge(tx, label, source_node_id, target_node_id, properties, source_type, update_only)
self._done()
def __call__(self, query):
tx = self._tx()
tx.append(query)
self._done()
def finish(self):
self._close()