Skip to main content

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')

Release files for pygraphs 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygraphs 0.0.1
File Size Uploaded
pygraphs-0.0.1.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pygraphs 0.0.1
File Interpreter ABI Platform
pygraphs-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.4 kB

Release files / pygraphs-0.0.1.tar.gz

Download URL pygraphs-0.0.1.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
61fb52edb3977172b2b22a2b6adee7eb7c72088ba929d591bba6c89f774cd122
BLAKE2b-256 checksum
How to use checksums
30a4a9603e432a6083d5ccf87f2b7d7d9be0317276db042db350faba1d801194
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

Release files / pygraphs-0.0.1-py3-none-any.whl

Download URL pygraphs-0.0.1-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f0b4ed8f264d6197fda0a7c15e9947787ce1877f6715cb1516f346152d690e0
BLAKE2b-256 checksum
How to use checksums
b1962db60d4a30d9f85ea031a5e5fd38ea692c01cdae48817c8119a09db1463e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page