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
Release history Release notifications | RSS feed
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 details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pygraphs-0.0.1.tar.gz.
File metadata
- Download URL: pygraphs-0.0.1.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61fb52edb3977172b2b22a2b6adee7eb7c72088ba929d591bba6c89f774cd122
|
|
| MD5 |
a5bc71451466d03dcc8169c8145eae21
|
|
| BLAKE2b-256 |
30a4a9603e432a6083d5ccf87f2b7d7d9be0317276db042db350faba1d801194
|
File details
Details for the file pygraphs-0.0.1-py3-none-any.whl.
File metadata
- Download URL: pygraphs-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f0b4ed8f264d6197fda0a7c15e9947787ce1877f6715cb1516f346152d690e0
|
|
| MD5 |
467e61d07ea73f768dd24df5ae07143c
|
|
| BLAKE2b-256 |
b1962db60d4a30d9f85ea031a5e5fd38ea692c01cdae48817c8119a09db1463e
|