Skip to main content

A graph database based on Python

Project description

pygraphs

A graph database based on Python

纯Python实现的图数据库

  • 完备增删改查
  • 改善复杂查询的体验
  • 支持 CQL 语句

使用文档

初始化一个空的图数据库

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)

# 按照主键来查询
print(G.vertexes['Frank Darabont'])

# 按特定过滤条件查询节点
young_people = G.query(condition_vertex=lambda x: ('born' in x) and x['born'] > '1975')
print(young_people)

# 按特定过滤条件查询边
relation_son = G.query(condition_edge=lambda x: 'relation' in x and x['relation'] == 'son')
print(relation_son)

复杂查询

for edge in G.edges:
    if 'type' in edge.val and edge.val['type'] == 'acted_in':
        src = edge.src
        if 'born' in src.val and src.val['born'] > '1975':
            print(src, ';', edge)

清除所有节点和边

G.clear()
# 删边,G.del_edges 批量删,G.del_edge 单个删
G.del_edges(edges_to_del=relation_son)

# 删节点,G.del_vertexes 批量删,G.del_vertex 单个删
G.del_vertex(vertex_to_del=G.vertexes['Tom'])

改节点属性,已有的属性被覆盖,没有的属性新建

G.set_val(G.vertexes['Kitty'], {'sex': 'male', 'height': '1.8m'})
print(G.vertexes['Kitty'].val)

改边的属性,已有的属性被覆盖,没有的属性新建

edge_to_set = list(G.vertexes['Kitty'].dst)[0]
self = G.set_val(edge_to_set, {'relation': 'husband'})
print(edge_to_set.val)

持久化

把图数据库存到本地

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

从本地读图数据库

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygraphs-0.0.1.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

pygraphs-0.0.1-py3-none-any.whl (4.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page