本文整理汇总了Python中py2neo.Graph.merge方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.merge方法的具体用法?Python Graph.merge怎么用?Python Graph.merge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类py2neo.Graph
的用法示例。
在下文中一共展示了Graph.merge方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TwitterGraph
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import merge [as 别名]
class TwitterGraph(object):
'''A class for interfacing with the Neo4j Twitter network database'''
# Initial setup and linking into the database
def __init__(self, host_port, user, password):
'''Makes connection to Neo4j database'''
# set up authentication parameters
authenticate(host_port, user, password)
# connect to authenticated graph database
url = 'http://{}/db/data/'.format(host_port)
self.graph = Graph(url)
try:
self.graph.schema.create_uniqueness_constraint('User', 'id')
except: #ConstraintViolationException
print 'Unique id on Node User already exists'
# Functions to add data to the database
def add_following(self, user_id, following_ids, rec_count):
'''Given a unique user id, adds the relationship for who they follow.
Adds a User Node with the id if it doesn't exist.'''
user = Node('User', id=user_id)
self.graph.merge(user) # important to merge before doing anything
rec = 1 + rec_count
# preserving the order of the following. 1 = most recent
for fid in following_ids:
user2 = Node('User', id=fid)
self.graph.merge(user2)
self.graph.merge(Relationship(user, 'FOLLOWS', user2, rec=rec))
rec += 1
user['following_added'] = True
self.graph.push(user)
def add_followers(self, user_id, follower_ids, rec_count):
'''Given a unique user id, adds the relationship for follows them.
Adds a User Node with the id if it doesn't exist.'''
user = Node('User', id=user_id)
self.graph.merge(user)
rec = 1 + rec_count
for fid in follower_ids:
user2 = Node('User', id=fid)
self.graph.merge(user2)
self.graph.merge(Relationship(user2, 'FOLLOWS', user, rec=rec))
rec += 1
user['followers_added'] = True
self.graph.push(user)
def add_user_properties(self, user):
'''Given a unique user id, adds properties to the existing user Node'''
try:
user_id = user.id
existing_user = Node('User', id=user_id)
clean_prop_dict = self.__clean_user_dict(user.__dict__)
self.graph.merge(existing_user)
for k, v in clean_prop_dict.iteritems():
existing_user[k] = v
# add additional label to verified accounts
if clean_prop_dict['verified']:
print True
existing_user.add_label('Verified')
except:
# bad user id
user_id = user['user_id']
error = user['error']
existing_user = Node('User', id=user_id)
self.graph.merge(existing_user)
existing_user['screen_name'] = 'INVALID'
existing_user['error'] = error
print 'Found invalid user id'
self.graph.push(existing_user)
def __clean_user_dict(self, user_prop_dict):
'''Given the '''
keep = ['contributors_enabled', 'created_at', 'default_profile',
'default_profile_image', 'description', 'favourites_count',
'followers_count', 'friends_count', 'geo_enabled', 'id',
'id_str', 'is_translator', 'lang', 'listed_count', 'location',
'name', 'profile_image_url_https', 'protected', 'screen_name',
'statuses_count', 'time_zone', 'utc_offset', 'verified',
'withheld_in_countries', 'withheld_scope']
# only keep the above keys for inserting
clean = {k: v for k, v in user_prop_dict.iteritems() if k in keep}
image = os.path.splitext(clean['profile_image_url_https'])[0]
ext = os.path.splitext(clean['profile_image_url_https'])[1]
clean['profile_image_url_https'] = image.rstrip('_normal') + ext
# convert date time to string
clean['created_at_ord'] = clean['created_at'].toordinal()
clean['created_at'] = clean['created_at'].strftime('%Y-%m-%d %H:%M:%S')
return clean
# Functions to query database
def get_nodes_missing_props(self, limit=100):
'''Returns the first 100 ids of nodes without user properties'''
selector = NodeSelector(self.graph)
selected = selector.select('User').where("_.screen_name IS NULL").limit(limit)
return [s['id'] for s in selected]
def get_nodes_missing_props_follb(self, limit=100):
cypherq = """MATCH (n)-[r:FOLLOWS]->(m)
#.........这里部分代码省略.........
示例2: titlecase
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import merge [as 别名]
return titlecase(''.join(random.choice(chars) for _ in range(size))) + " " + titlecase(''.join(random.choice(chars) for _ in range(size)))
#for i in range(0,5):
# Author_Generator()
#a = Author_Generator()
#a1=a
#a = graph.merge_one("Author", "Name",a1 )
#r1 = random.randint(0,10)
for i in range(0,40):
Author_name.append(Author_Generator())
Author_id.append((i))
a=Node("Author", ID=Author_id[i],Name=Author_name[i])
#a.properties["Name"]=Author_name[i]
graph.merge(a)
for i in range(0,5):
r1 = random.randint(0,40)
Author_id.append((i+40))
Author_name.append(Author_name[r1])
a=Node("Author", ID=Author_id[i+40],Name=Author_name[i+40])
graph.merge(a)
for i in range(0,45):
for j in range(0,45):
r1 = random.randint(0,50)
r2 = random.randint(5,25)
if i == j:
Matrix[i][j]=r2
示例3: process
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import merge [as 别名]
def process (self, parameters={}, data={} ):
if 'verbose' in parameters:
self.config['verbose'] = parameters['verbose']
# for this facets, do not add additional entity to connect with, but write to properties of the entity
properties = ['content_type_ss', 'content_type_group_ss', 'language_ss', 'language_s']
host = 'localhost'
if 'neo4j_host' in parameters:
host = parameters['neo4j_host']
user = 'neo4j'
if 'neo4j_user' in parameters:
user = parameters['neo4j_user']
password = 'neo4j'
if 'neo4j_password' in parameters:
password = parameters['neo4j_password']
graph = Graph(host=host, user=user, password=password)
document_node = Node('Document', name = parameters['id'])
if 'title' in data:
document_node['title'] = data['title']
# add properties from facets
for entity_class in parameters['facets']:
if entity_class in data:
entity_class_label = parameters['facets'][entity_class]['label']
if entity_class in properties:
document_node[entity_class_label] = data[entity_class]
graph.merge(document_node)
# add / connect linked entities from facets
for entity_class in parameters['facets']:
if entity_class in data:
entity_class_label = entity_class
if parameters['facets'][entity_class]['label']:
entity_class_label = parameters['facets'][entity_class]['label']
if not entity_class in properties:
relationship_label = entity_class_label
if entity_class in ['person_ss','organization_ss', 'location_ss']:
relationship_label = "Named Entity Recognition"
# convert to array, if single entity / not multivalued field
if isinstance(data[entity_class], list):
entities = data[entity_class]
else:
entities = [ data[entity_class] ]
for entity in entities:
if self.config['verbose']:
print ("Export to Neo4j: Merging entity {} of class {}".format(entity, entity_class_label))
# if not yet there, add the entity to graph
entity_node = Node(entity_class_label, name = entity)
graph.merge(entity_node)
# if not yet there, add relationship to graph
relationship = Relationship(document_node, relationship_label, entity_node)
graph.merge(relationship)
return parameters, data
示例4: authenticate
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import merge [as 别名]
from py2neo import Graph, Node, Relationship, authenticate
authenticate("localhost:7474", "neo4j", "cloudchaser")
graph = Graph("http://localhost:7474/db/data/")
graph.delete_all()
alice = graph.merge("Person", "name", "Alice")
bob = graph.merge("Person", "name", "Bob")
chelsea = graph.merge("Person", "name", "Chelsea")
prof = {
'name': 'Dennis'
}
fav = {
'name': 'Emma'
}
query = (
'MERGE (profile:soundcloud {name: {profile}.name}) \
ON CREATE SET profile={profile} '
'MERGE (favorite:soundcloud {name: {favorite}.name}) \
ON CREATE SET favorite={favorite} '
)
graph.cypher.execute(query, {
'profile': prof,
'favorite': fav
}
)