Skip to main content

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 and pyenv-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)

graphcache

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


Download files

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

Source Distribution

graphcache-0.1.4.tar.gz (9.2 kB view hashes)

Uploaded Source

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