Python library to store connected nodes and their properties on cache storage
Project description
graphcache
Python Library to store connected nodes and their properties on cache storage (redis)
Installation
To install graphcache
, simply:
pip install graphcache
Development
- Install
pyenv
andpyenv-virtualenv
- Run
pyenv install 3.7.0 --skip-existing pyenv virtualenv 3.7.0 graphcache
- Update requirements
pip install -r requirements.txt
- Install
Redis
Basic Use
Ensure you have redis
service running
On macos
redis-server /usr/local/etc/redis.conf
To use graphcache, you must first create an instance of GraphCache, and construct your nodes and edges:
# Import GraphCache
from graphcache import GraphCache
# default
# host = localhost
# port = 6379
# db = 0
g = GraphCache(host='localhost', port=6379, db=0)
# Add optimisation keys
g.optimise_for('bananas')
g.optimise_for('apples')
# Create Nodes (need 'apples', 'bananas' keys in all nodes, as they have been added to optimisation_keys)
n1 = g.add_vertex({
'name': 'Tom',
'age': 24,
'bananas': 5,
'apples': 4
})
n2 = g.add_vertex({
'name': 'Bob',
'bananas': 0,
'apples': 8
})
n3 = g.add_vertex({
'name': 'Harry',
'gender': 'Male',
'bananas': 3,
'apples': 1
})
n4 = g.add_vertex({
'name': 'Jill',
'bananas': 8,
'apples': 1
})
# Connect them
g.add_edge(g.entry, n2) # g.entry specifies the entry point for graph
g.add_edge(g.entry, n3)
g.add_edge(n2, n3)
g.add_edge(n3, n4)
g.add_edge(n2, n1)
Then you can perform filter/sort operations on any of the node to get the required adjacent nodes from that node:
# Filter By
# Get all outgoing nodes (only adjacent) from n2 which have only 1 apples
nodes1 = n2.get_outgoing().filter_by('apples', [1]).get_all_nodes()
# Sort By
# Get all incoming nodes (only adjacent) to n3 sorted by number of bananas they have
nodes2 = n3.get_incoming().sort_by('bananas').get_all_nodes()
# Get first incoming node to n1 sorted by bananas
node1 = n1.get_incoming().sort_by('bananas').get_node_indexed_at(0)
Also you can perform chained complex operations:
# Filter By, Sort By
# Get all outgoing nodes (only adjacent) from n2 with apples less than 5 and sorted by bananas
nodes1 = n2.get_outgoing().filter_by('apples', 5, "lt").sort_by('bananas').get_all_nodes()
# Sort By, Filter By, Filter By
nodes2 = n3.get_incoming().sort_by('bananas').filter_by('bananas', [1, 5], "in").filter_by('apples', [1]).get_all_nodes()
Get the key for the graphcache object
g.cache_key
# graphcache-MZ5SQR
Retrieve previously stored graphcache object from redis cache (cache_key = graphcache-MZ5SQR
)
# using default redis connection
# host = localhost
# port = 6379
# db = 0
g1 = GraphCache(graphcache_ref='graphcache-MZ5SQR')
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
File details
Details for the file graphcache-0.1.4.tar.gz
.
File metadata
- Download URL: graphcache-0.1.4.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68eaacbf0a3813d5205e261ebd798a8cf263c3f87768131b0195d43b65fe8a23 |
|
MD5 | 29af15759c1e1a12f0d44c292ff75086 |
|
BLAKE2b-256 | 2b11c4ecdd69850165b353b2cff1153cbfc365450ddfa8b921892bd9d268b843 |