本文整理汇总了Python中py2neo.Graph.match_one方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.match_one方法的具体用法?Python Graph.match_one怎么用?Python Graph.match_one使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类py2neo.Graph
的用法示例。
在下文中一共展示了Graph.match_one方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TwitterGraph
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import match_one [as 别名]
class TwitterGraph():
def __init__(self):
self.graph = Graph("http://neo4j:[email protected]:7474/db/data/")
self.popularity_heap = []
self.reassess_popularity()
def add_user(self, user):
new_user = Node("User", token=user.token.session_id, user_id=user.id)
return self.graph.create(new_user)
def is_cached(self, screen_name):
twitter_user = self.graph.find_one("TwitterUser", 'screen_name', screen_name)
if twitter_user is not None:
return True
def get_RT_recommendations(self, user):
recommendations = Counter()
user_node = self.graph.find_one("User", 'user_id', user.id)
following = user_node.match_outgoing("FOLLOWS", limit=5)
for rel in following:
retweets = rel.end_node.match_outgoing("RETWEETED", limit=5)
for r in retweets:
recommendations[r.end_node.properties['screen_name']] += 1
return [str for (str, count) in recommendations.most_common(10)]
def get_generic_recommendations(self):
return [screen_name for (count, screen_name) in heapq.nlargest(10, self.popularity_heap)]
def reassess_popularity(self):
# NOTE: expensive calculation, to be run threaded at multiples of x actions to graph or hourly/daily job
all_twitter_users = self.graph.find("TwitterUser")
for tu in all_twitter_users:
incoming_count = sum(1 for _ in tu.match_incoming())
heapq.heappush(self.popularity_heap, (incoming_count, tu.properties['screen_name']))
def add_twitter_user(self, screen_name):
twitter_user = self.graph.find_one("TwitterUser", 'screen_name', screen_name)
if twitter_user is None:
new_twitter_user = Node("TwitterUser", screen_name=screen_name)
self.graph.create(new_twitter_user)
def add_follow(self, screen_name, user):
user_node = self.graph.find_one("User", 'user_id', user.id)
if user_node is None:
# this shouldn't happen, just for testing while transitioning db
self.add_user(user)
user_node = self.graph.find_one("User", 'user_id', user.id)
twitter_user = self.graph.find_one("TwitterUser", 'screen_name', screen_name)
if twitter_user is None:
# this shouldn't happen, just for testing while transitioning db
self.add_twitter_user(screen_name)
twitter_user = self.graph.find_one("TwitterUser", 'screen_name', screen_name)
follow_relationship = Relationship(user_node, "FOLLOWS", twitter_user)
self.graph.create(follow_relationship)
self.reassess_popularity()
def remove_follow(self, screen_name, user):
user_node = self.graph.find_one("User", 'user_id', user.id)
if user_node is None:
# this shouldn't happen, just for testing while transitioning db
self.add_user(user)
user_node = self.graph.find_one("User", 'user_id', user.id)
twitter_user = self.graph.find_one("TwitterUser", 'screen_name', screen_name)
if twitter_user is None:
# this shouldn't happen, just for testing while transitioning db
self.add_twitter_user(screen_name)
twitter_user = self.graph.find_one("TwitterUser", 'screen_name', screen_name)
follow_relationship = self.graph.match_one(user_node, "FOLLOWS", twitter_user)
if follow_relationship is not None:
self.graph.delete(follow_relationship)
def add_retweet(self, screen_name, retweeted_screen_name):
twitter_user = self.graph.find_one("TwitterUser", 'screen_name', screen_name)
if twitter_user is None:
# this shouldn't happen, just for testing while transitioning db
self.add_twitter_user(screen_name)
twitter_user = self.graph.find_one("TwitterUser", 'screen_name', screen_name)
self.add_twitter_user(retweeted_screen_name)
retweeted_twitter_user = self.graph.find_one("TwitterUser", 'screen_name', retweeted_screen_name)
retweet = self.graph.match_one(twitter_user, "RETWEETED", retweeted_twitter_user)
if retweet is None:
retweet_relationship = Relationship(twitter_user, "RETWEETED", retweeted_twitter_user)
retweet_relationship.properties['count'] = 1
self.graph.create(retweet_relationship)
elif retweet.properties['count'] is None:
# this shouldn't happen, just for testing while transitioning db
retweet.properties['count'] = 1
retweet.push()
else:
retweet.properties['count'] = retweet.properties['count'] + 1
retweet.push()
示例2: PopItToNeo
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import match_one [as 别名]
class PopItToNeo(object):
def __init__(self):
config = yaml.load(open("config.yaml"))
self.endpoint = "https://sinar-malaysia.popit.mysociety.org/api/v0.1"
# you know so that you can override this. why? I am not sure
self.membership_field = "memberships"
self.person_field = "persons"
self.organization_field = "organizations"
self.post_field = "posts"
self.graph = Graph(config["graph_db"])
if config["refresh"] == True:
self.graph.delete_all()
# Because I am still not familiar to query with cypher
# So lets cache here. Hopefully the memory usage don't kill me
self.organization_processed = {}
self.person_processed = {}
self.post_processed = {}
def process_membership(self):
# So lets start from membership
membership_url = "%s/%s" % (self.endpoint, self.membership_field)
while True:
logging.warning("Processing %s" % membership_url)
data = self.fetch_entity(membership_url)
logging.warning("Processing membership")
entries = data["result"]
for entry in entries:
# a membership have 3 important field, person_id, organization_id, posts_id
if not (entry.get("person_id") and entry.get("organization_id")):
continue
person = self.fetch_person(entry["person_id"])
if not person:
continue
role = entry.get("role","member")
if not role:
role = "member"
logging.warning("Role: %s" % role)
params = []
# This happens only once anyway
kwparams = {}
kwparams["popit_id"] = entry["id"]
start_date = get_timestamp(entry.get("start_date"))
if start_date:
kwparams["start_date"] = start_date
end_date = get_timestamp(entry.get("end_date"))
if end_date:
kwparams["end_date"] = end_date
post_exist = False
if entry.get("post_id"):
post = self.fetch_post(entry["post_id"])
if not post:
continue
if self.graph.match_one(person, role, post):
post_exist = True
logging.warning("Already exist, skipping")
if not post_exist:
relationship = Relationship(person, role, post, **kwparams)
self.graph.create(relationship)
organization_exist = False
if entry.get("organization_id"):
organization = self.fetch_organization(entry["organization_id"])
if not organization:
continue
if self.graph.match_one(person, role, organization):
logging.warning("Already exist, skipping")
organization_exist = True
if not organization_exist:
relationship = Relationship(person, role, organization, **kwparams)
self.graph.create(relationship)
if data.get("next_url"):
membership_url = data.get("next_url")
else:
break
def fetch_person(self, person_id):
if person_id in self.person_processed:
logging.warning("Person %s fetch from cache" % person_id)
return self.person_processed[person_id]
node = self.graph.find_one("Persons", "popit_id", person_id)
if node:
logging.warning("Already exist, skipping")
self.person_processed[person_id] = node
return node
#.........这里部分代码省略.........
示例3: GraphDB
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import match_one [as 别名]
class GraphDB():
def __init__(self, user=NEO4J_USER, pwd=NEO4J_PWD, host=NEO4J_HOST):
self.graph = Graph("http://%s:%[email protected]%s/db/data/" % (user, pwd, host))
def query(self, query_str, stream=False):
if stream:
return self.graph.cypher.stream(query_str)
else:
return self.graph.cypher.execute(query_str)
def create_relation_user_to_topic(self, user, relation, topic_name):
userNode = self.graph.find_one("user", 'id', user.id_str)
if not userNode:
userNode = self.create_node_from_user(user)
self.graph.create(userNode)
topicNode = self.graph.find_one("topic_name", 'name', topic_name)
if not topicNode:
topicNode = Node("topic_name", name = topic_name)
self.graph.create(topicNode)
relationship = self.graph.match_one(userNode, relation, topicNode)
if not relationship:
relationship = Relationship(userNode, relation, topicNode, count = 1)
self.graph.create(relationship)
else:
relationship.properties['count'] += 1
relationship.push()
# Relations: follows eventuell favourites, retweets
def create_relation_user_to_user(self, userA, relation, userB):
userANode = self.graph.find_one("user", 'id', userA.id_str)
userBNode = self.graph.find_one("user", 'id', userB.id_str)
if not userANode:
userANode = self.create_node_from_user(userA)
self.graph.create(userANode)
if not userBNode:
userBNode = self.create_node_from_user(userB)
self.graph.create(userBNode)
relationship = self.graph.match_one(userANode, relation, userBNode)
if not relationship:
relationship = Relationship(userANode, relation, userBNode, count = 1)
self.graph.create(relationship)
else:
relationship.properties['count'] += 1
relationship.push()
def increment_user_counter(self, user, counter, n):
userNode = self.graph.find_one("user", 'id', user.id_str)
if not userNode:
userNode = self.create_node_from_user(user)
self.graph.create(userNode)
if counter in userNode.properties:
userNode.properties[counter] += n
else:
userNode.properties[counter] = n
userNode.push()
def get_all_users(self):
users = []
for u in self.graph.find('user'):
users.append({'name': u.properties['screen_name'], 'id_str': u.properties['id']})
return users
def create_node_from_user(self, user):
userNode = Node("user", name=user.screen_name, id=user.id_str, followers_count=user.followers_count,
friends_count=user.friends_count, statuses_count=user.statuses_count, favourites_count=user.favourites_count)
return userNode
def quicksearch(self, username, limit=10):
cql_query = "match(u:user) WHERE u.name =~ '%s.*' RETURN DISTINCT u.name LIMIT %s;"
return self.query(cql_query % (username, limit))
def get_user_count(self):
cql_query = "match(u:user) RETURN count(DISTINCT u) AS c;"
for row in self.query(cql_query):
return row['c']
return 0
示例4: Cq
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import match_one [as 别名]
class Cq(object):
def __init__(self):
"""
:return:
"""
self.id = ''
self.subject = ''
self.message = ''
self.created_date = ''
self._graph_db = Graph(settings.DATABASE_URL)
@property
def cq_properties(self):
"""
:return:
"""
properties_dict = dict(self.__dict__)
del properties_dict['_graph_db']
return properties_dict
@property
def cq_node(self):
"""
:return:
"""
if self.id != '':
return self._graph_db.find_one(GraphLabel.CQ,
property_key='id',
property_value=self.id)
@property
def response_list(self):
"""
list of responses to this CQ
:return: list of responses
"""
cq_response_relationship = self._graph_db.match(start_node=self.cq_node,
rel_type=GraphRelationship.TO,
end_node=None)
response_list = []
for rel in cq_response_relationship:
response = rel.end_node.properties
user_response_relationship = self._graph_db.match_one(start_node=None,
rel_type=GraphRelationship.RESPONDED,
end_node=self.cq_node)
user_node = user_response_relationship.start_node
response['by'] = '%s / %s' % (user_node.properties['name'],
user_node.properties['call_sign'])
response_list.append(response)
return response_list
@staticmethod
def create_cq(user_node, cq_dict):
cq_dict['id'] = str(uuid.uuid4())
cq_dict['created_date'] = datetime.date.today()
cq_node = Node.cast(GraphLabel.CQ,
cq_dict)
cq_node, = Graph(settings.DATABASE_URL).create(cq_node)
cq_relationship = Relationship(user_node,
GraphRelationship.SENT,
cq_node)
Graph(settings.DATABASE_URL).create_unique(cq_relationship)
@staticmethod
def most_recent_cqs():
params = {
}
cypher_str = ""
match_results = Graph(settings.DATABASE_URL).cypher.execute(statement=cypher_str,
parameters=params)
cq_list = []
cq = {}
for item in match_results:
cq['id'] = item.id
cq['subject'] = item.subject
cq['message'] = item.message
cq['created_date'] = item.created_date
cq_list.append(cq)
root = {}
root['cqs'] = cq_list
return root
def response(self, response_id):
"""
response dictionary details including user details
:param response_id:
:return: dict with response details and a dict of the user who made the response
"""
response_node = self._graph_db.find_one(GraphLabel.RESPONSE,
property_key='id',
property_value=response_id)
response_user_relationship = self._graph_db.match_one(start_node=None,
rel_type=GraphRelationship.RESPONDED,
end_node=response_node)
#.........这里部分代码省略.........
示例5: User
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import match_one [as 别名]
#.........这里部分代码省略.........
# else:
# interest_users_dict[interest] = []
# cur_users_list.append(user)
# interest_users_dict[interest] = cur_users_list
# interest_users_dict[interest] = interest_users_dict.get(interest)
# user_details = (user_node['name'], user_node['email'], user_node['id'])
# user_list.append(user_details)
return interest_users_dict
def add_interest(self, interest_id, experience_properties_dict=None):
""" Add interest to user
:param interest id:string uuid
:return: List of interests
"""
#TODO add exception handling
interest = Interest()
interest.id = interest_id
interest_node = interest.interest_node_by_id
user_interest_relationship = Relationship(self.user_node,
GraphRelationship.INTERESTED_IN,
interest_node)
for key, value in experience_properties_dict.iteritems():
user_interest_relationship[key] = value
try:
self._graph_db.create_unique(user_interest_relationship)
except:
pass
return self.user_interests
def update_interest(self, interest_id, experience_properties_dict):
interest = Interest()
interest.id = interest_id
interest_node = interest.interest_node_by_id
user_interest_relationship = self._graph_db.match_one(start_node=self.user_node,
rel_type=GraphRelationship.INTERESTED_IN,
end_node=interest_node)
for key, value in experience_properties_dict.iteritems():
user_interest_relationship.properties[key] = value
user_interest_relationship.push()
def delete_interest(self, interest_id):
"""
drop interest relationship from user given the interest_id
:param interest_id: str(uuid.uuid4())
:return:
"""
#TODO exception handling
interest = Interest()
interest.id = interest_id
interest_node = interest.interest_node_by_id
user_interest_relationship = self._graph_db.match_one(start_node=self.user_node,
rel_type=GraphRelationship.INTERESTED_IN,
end_node=interest_node)
self._graph_db.delete(user_interest_relationship)
def update_user(self):
user_node = self.user_node
user_properties = dict(self.user_properties)
for key, value in user_properties.iteritems():
user_node[key] = value # user_properties[key]
user_node.push()
# def make_admin(self):
# #new_user = self.graph_db.get_or_create_indexed_node(index_name=GraphLabel.USER, key='email', value=self.email)
# self.user_node.add_labels(GraphLabel.ADMIN)
示例6: list
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import match_one [as 别名]
# | >> > selected = selector.select("Person", name="Keanu Reeves")
# | >> > list(selected)
# | [(f9726ea:Person{born: 1964, name: "Keanu Reeves"})]
# | >> > selected = selector.select("Person").where("_.name =~ 'J.*'", "1960 <= _.born < 1970")
# | >> > list(selected)
# | [(a03f6eb:Person{born: 1967, name: "James Marshall"}),
# | (e59993d:Person{born: 1966, name: "John Cusack"}),
# | (c44901e:Person{born: 1960, name: "John Goodman"}),
# | (b141775:Person{born: 1965, name: "John C. Reilly"}),
# | (e40244b:Person{born: 1967, name: "Julia Roberts"})]
# 六、通过节点/关系查找相关联的节点/关系
# 如果已经确定了一个节点或者关系,想找到和它相关的关系和节点,就可以使用match和match_one。
find_relationship = test_graph.match_one(start_node=find_code_1, end_node=find_code_2, bidirectional=False)
print(find_relationship)
# 如以上代码所示,match和match_one的参数包括start_node,Relationship,end_node中的至少一个。
# bidirectional参数的意义是指关系是否可以双向。
# 如果为False,则起始节点必须为start_node,结束节点必须为end_node。如果有Relationship参数,则一定按照Relationship对应的方向。
# 如果为True,则不需要关心方向问题,会把两个方向的数据都返回。
match_relation = test_graph.match(start_node=find_code_1, bidirectional=True)
for i in match_relation:
print(i)
i['count'] += 1
test_graph.push(i)
# 如以上代码所示,查找和find_code_1相关的关系。
# match里面的参数只写了start_node,bidirectional的值为True,则不会考虑方向问题,返回的是以find_code_1为起始节点和结束节点的所有关联关系。
# 如果,bidirectional的值为False,则只会返回以find_code_1为起始节点的所有关联关系。
示例7: Node
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import match_one [as 别名]
j = i + 1
fromAuthor = graph.find_one("Author", "name", authors[i])
if fromAuthor is None:
fromAuthor = Node("Author", name=authors[i])
author_publish_paper = Relationship(fromAuthor, "PUBLISH", paper)
graph.create(author_publish_paper)
while (j < len(authors)):
toAuthor = graph.find_one("Author", "name", authors[j])
if toAuthor is None:
toAuthor = Node("Author", name=authors[j])
j = j + 1
graph.create(toAuthor)
ifExist = graph.match_one(fromAuthor, "CO", toAuthor)
if(ifExist is None):
fromAuthor_To_toAuthor = Relationship(fromAuthor, "CO", toAuthor)
toAuthor_To_fromAuthor = Relationship(toAuthor, "CO", fromAuthor)
graph.create(fromAuthor_To_toAuthor)
graph.create(toAuthor_To_fromAuthor)
else:
continue
示例8: Relationship
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import match_one [as 别名]
tempUser = row[0]
userNeo = neo4jGraph.find_one("User", property_key="userID", property_value=row[0])
#Caching persona for quick retreival
if row[1] not in personaDict:
personaNeo = neo4jGraph.find_one("Persona", property_key="personaID", property_value=row[1])
personaDict[row[1]] = personaNeo
else:
personaNeo = personaDict[row[1]]
#avoiding having empty user or persona as not all users come in
if not userNeo or not personaNeo:
continue
#Find if relationship already exists
relationshipNeo = neo4jGraph.match_one(start_node=userNeo, rel_type="IS_A", end_node=personaNeo)
#makes relationship if not
if not relationshipNeo:
relationship = Relationship(userNeo, "IS_A", personaNeo)
if args.verbose:
printToLog("Creating", relationship)
neo4jGraph.create(relationship)
##Users_Friends
if not mysqlConnection.is_connected():
mysqlConnection.reconnect(attempts=2, delay=1)
cursor.execute("select user_id, friend_id from user_friends where friend_id in (select user_id from user) and user_id in (select user_id from user where join_date between %s and %s)", (twodays, yesterday))
if args.verbose: