• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python py2neo.Graph类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中py2neo.Graph的典型用法代码示例。如果您正苦于以下问题:Python Graph类的具体用法?Python Graph怎么用?Python Graph使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Graph类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:xenron,项目名称:sandbox-dev-python,代码行数:28,代码来源:CreateRelationship.py


示例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)
开发者ID:CrackerCat,项目名称:Agriculture_KnowledgeGraph,代码行数:33,代码来源:neo_models.py


示例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
开发者ID:cltl,项目名称:ELBaselines,代码行数:29,代码来源:my_system.py


示例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
开发者ID:susurrant-audio,项目名称:susurrant-py,代码行数:7,代码来源:track_info.py


示例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())))
开发者ID:implicit-explicit,项目名称:social_genius_backend,代码行数:30,代码来源:social_genius_backend.py


示例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)
开发者ID:pombredanne,项目名称:python-server,代码行数:27,代码来源:Grapher.py


示例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))
开发者ID:davidmeza1,项目名称:KA_Interns,代码行数:7,代码来源:Scraper3.py


示例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
开发者ID:MoizRauf,项目名称:OQuant_Wiki_Clustering,代码行数:27,代码来源:NeoConnector.py


示例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))
开发者ID:VadyaVL,项目名称:labs,代码行数:33,代码来源:loadSPDB.py


示例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
开发者ID:susurrant-audio,项目名称:susurrant-py,代码行数:7,代码来源:track_info.py


示例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
开发者ID:uym2,项目名称:dlab-api,代码行数:28,代码来源:graph.py


示例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))
开发者ID:VadyaVL,项目名称:labs,代码行数:33,代码来源:loadMyDB.py


示例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
开发者ID:pashadude,项目名称:nogoodgamez2,代码行数:33,代码来源:VideoSequenceCreation.py


示例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()
开发者ID:jjmiranda,项目名称:edx-platform,代码行数:58,代码来源:dump_to_neo4j.py


示例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()
开发者ID:Caleydo,项目名称:pathfinder_graph,代码行数:58,代码来源:import_utils.py


示例16: get_graph

def get_graph():
    global NEO4J_URL,NEO4J_HOST,NEO4J_PORT,NEO4J_AUTH

    # Connect to graph
    creds = NEO4J_AUTH.split('/')
    graph = Graph(user=creds[0], password=creds[1], host=NEO4J_HOST)

    graph.run('match (t:Tweet) return COUNT(t)')
    return graph
开发者ID:neo4j-contrib,项目名称:twitter-neo4j,代码行数:9,代码来源:import_user.py


示例17: Person

class Person(object):

    def __init__(self):
        self.graph = Graph()
        #do nothing

    def createPerson(self, name, fullname, occupation, homepage, wikipedia_link):
        person = Node('Person', name=name, fullname=fullname, occupation=occupation, homepage=homepage, wikipedia_link=wikipedia_link)
        self.graph.create(person)
        return person
开发者ID:anishdeena,项目名称:europa,代码行数:10,代码来源:testneo4j.py


示例18: add2neo

def add2neo(courses):
	authenticate("localhost:7474", 'neo4j', 'admin')
	graph = Graph()
	for course in courses:
		name = course.prefix+course.number

		for pre in course.prereqs:
			print 'adding rel', name, pre
			try:
				graph.create(Relationship(courseNodes[name]['node'], 'REQUIRES', courseNodes[pre]['node']))
			except:
				print 'could not add', name, pre
开发者ID:birdman7260,项目名称:skilltree,代码行数:12,代码来源:process_courses.py


示例19: expot_data

def expot_data(cid, data):
    """
    将数据导入到neo4j,给每个导入的实体添加一个标签cid.
    :param cid: 
    :param data: 
    :return: 
    """
    title = data[0]
    host, http_port, bolt_port, user, password = 'localhost', 7474, 7687, 'neo4j', 'gswewf'
    graph = Graph(host=host, http_port=http_port, bolt_port=bolt_port, user=user, password=password)
    # title = ["_id", "_labels", "tagline", "title", "released", "name", "born", "_start", "_end", "_type", "roles"]
    _start_index = title.index('_start')
    node_property = title[2:_start_index]
    relation_property = title[_start_index + 3:]
    nodes = {}
    relationships = []
    tx = graph.begin()
    for line in data[1:]:
        _id, _labels = line[:2]
        node_property_value = line[2:_start_index]
        _start, _end, _type = line[_start_index:_start_index + 3]
        relation_property_value = line[_start_index + 3:]
        _labels = [label for label in _labels.strip().split(':') if label]
        _labels.append(cid.capitalize())
        # print(line)
        # nodes = {"a": Node("person", name="weiyudang", age=13), "b": Node("person", name="wangjiaqi")}
        if _id and not _start and not _end:
            property_dict = {k: v for k, v in zip(node_property, node_property_value) if v}
            _cid = "{}_{}".format(cid.lower(), _id)
            updatetime = int(time.time() * 1000)  # 与 neo4j的timestamp()一致
            node = Node(*_labels, _cid=_cid, updatetime=updatetime, **property_dict)
            # graph.merge(node)
            nodes.setdefault(_cid, node)
            tx.create(node)
        elif not _id and _start and _end:
            property_dict = {k: v for k, v in zip(relation_property, relation_property_value) if v}
            start_cid = "{}_{}".format(cid.lower(), _start)
            end_cid = "{}_{}".format(cid.lower(), _end)
            # a = Node(_cid=start_cid)
            # b = Node(_cid=end_cid)
            a = nodes.get(start_cid)
            b = nodes.get(end_cid)
            a_knows_b = Relationship(a, _type, b, **property_dict)
            # graph.merge(a_knows_b)
            relationships.append(a_knows_b)
            tx.create(a_knows_b)
        else:
            raise ValueError("数据有误: {}".format(line))
    print(len(nodes), len(relationships))
    # sub_graph = Subgraph(nodes=nodes, relationships=relationships)
    # graph.create(sub_graph)
    tx.commit()
开发者ID:gswyhq,项目名称:hello-world,代码行数:52,代码来源:通过py2neo将csv文件中的数据导入的neo4j.py


示例20: import_api_data2

def import_api_data2():
    authenticate("localhost:7474", "neo4j", "1111")
    graph = Graph()
    #graph.delete_all()

    # Uncomment on the first run!
    #graph.schema.create_uniqueness_constraint("Borjnuk", "id")
    #graph.schema.create_uniqueness_constraint("Obtaj", "id")
    #graph.schema.create_uniqueness_constraint("Property", "id")

    obtajenna = get_objects_art('obtaj')

    for api_obtaj in obtajenna:

        node_obtaj= graph.merge_one("Obtaj", "id", api_obtaj["id"])
        node_obtaj["reason_doc"] = api_obtaj["reason_doc"]
        node_obtaj["cost_size"] = api_obtaj["cost_size"]

        for api_author in api_obtaj["borjnuku"]:
            node_borjnuk = graph.merge_one("Borjnuk", "id", api_author["id"])
            node_borjnuk["name"] = api_author["name"]
            node_borjnuk["tel_number"] = api_author["tel_number"]
            node_borjnuk.push()
            graph.create_unique(Relationship(node_borjnuk, "obtajuetsa", node_obtaj))

        for api_property in api_obtaj["properties"]:
            node_property = graph.merge_one("Property", "id", api_property["id"])
            node_property["name"] = api_property["name_property"]
            node_property["ser_number"] = api_property["ser_number"]
            node_property.push()
            graph.create_unique(Relationship(node_property, "zakladena", node_obtaj))
        node_obtaj.push()
开发者ID:wlagur,项目名称:lab2_melach,代码行数:32,代码来源:neo4j.py



注:本文中的py2neo.Graph类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python py2neo.Node类代码示例发布时间:2022-05-25
下一篇:
Python py2neo.rel函数代码示例发布时间:2022-05-25
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap