Coder Social home page Coder Social logo

pygraphs's Introduction

pygraphs

A graph database based on Python

纯Python实现的图数据库

install

pip install pygraphs

使用文档

初始化一个空的图数据库

import pygraphs as pg

G = pg.Graph()

增加节点

# 从csv读取节点并加入图数据库
G.add_vertexes_from_file(filename='Vertexes.csv')

# 从list读取节点并加入图数据库
vertexes_list = [['Tom', {'age': 10}],
                 ['Kitty', {'sex': 'female'}],
                 ['Jimmy', {'sex': 'male', 'age': 35}]
                 ]
G.add_vertexes_from_list(vertexes_list=vertexes_list)

print(G.vertexes)

增加边

# 从csv读取关系并加入图数据库
G.add_edges_from_file(filename='Edges.csv')

# 从 list 读取并加入图数据库
edges_list = [['Tom', {'relation': 'son'}, 'Jimmy'],
              ['Kitty', {'relation': 'wife'}, 'Jimmy'],
              ]
G.add_edges_from_list(edges_list=edges_list)
print(G.edges)

# 1.return: '(src)' 返回节点列表,'[edge]' 返回 边的列表
G.match('(src)').where("src.born in ['1956', '1973'] and src.type == 'person'").returns('(src)')
G.match('[edge]').where("edge.type == 'acted_in' and edge.roles is not Null").returns('[edge]')

# 2. return: 'sub graph' 返回子图对象(Graph 对象)
G.match('(src)-[edge]->(dst)').where("src.type == 'person' and edge.type == 'acted_in' and dst.released > '2000'"). \
    returns('sub graph')

# 3. return: 指定属性,返回结构化数据(如果节点的某个属性不存在,对应单元格值为 Null)
G.match('(src)').where("src.born in ['1956', '1973'] and src.type == 'person'").returns('src.primary_key,src.born')
G.match('[edge]').where("edge.type == 'directed'").returns('edge.type,edge.roles')
G.match('(src)-[edge]->(dst)').where("src.type == 'person' and edge.type == 'acted_in' and dst.released > '2000'"). \
    returns('src.primary_key,src.born,edge.roles,dst.type,dst.released')

# 返回所有节点
G.vertexes
# 返回所有边
G.edges

改节点属性,mode='update'覆盖已有属性并新增没有的属性,mode='cover'抹除全部旧属性并新增

G.match('(src)').where("src.born in ['1956', '1973'] and src.type == 'person'"). \
    set({'status': 'old man'}, mode='update')

# 查看是否已经改好:
G.match('(src)').where("src.born in ['1956', '1973'] and src.type == 'person'").returns('src.born,src.type,src.status')

改边的属性,一样

G.match('[edge]').where("edge.type=='directed'").set({'partner': 'director'})

清除所有节点和边

G.clear()

删边,G.del_edges 批量删,G.del_edge 单个删

edges_to_del = G.match('[edge]').where("edge.relation=='son'").returns('[edge]')
G.del_edges(edges_to_del=edges_to_del)

删节点,G.del_vertexes 批量删,G.del_vertex 单个删

vertexes_to_del = G.match('(src)').where("src.primary_key=='Tom'").returns('(src)')
G.del_vertexes(vertexes_to_del=vertexes_to_del)

持久化

从内存把图数据库存到文件

pg.save_db(G, 'db_file.db')

从文件读图数据库到内存

G_new = pg.load_db('db_file.db')

pygraphs's People

Contributors

guofei9987 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.