redis_graph: Graph database for Python

Redis logo

redis_graph is a graph database implemented in Python. It shows how awesome Redis is as the implementation is under 40 lines of code.

The perfomance should be excellent, while scaling it might be an issue. I would not recommend using it if you are storing nodes in the millions range.


You can get redis_graph from PyPi or on github:

Example of creating edges between nodes

from redis_graph import *

add_edge(from_node='frodo', to_node='gandalf')

assert has_edge(from_node='frodo',
                to_node='gandalf') == True

assert list(neighbors('frodo')) == ['gandalf']


assert has_edge(from_node='frodo',
                to_node='gandalf') == False

Example of setting node and edge values

set_node_value('frodo', '1')
assert get_node_value('frodo') == '1'

set_edge_value('frodo_baggins', '2')
assert get_edge_value('frodo_baggins') == '2'

The full implementation

To create a graph database you don't really need that much code. This is it:

from redis_wrap import *

#--- Edges ----------------------------------------------
def add_edge(from_node, to_node, system='default'):
    edges = get_set( from_node, system=system )
    edges.add( to_node )

def delete_edge(from_node, to_node, system='default'):
    edges = get_set( from_node, system=system )

    key_node_y = to_node
    if key_node_y in edges:
        edges.remove( key_node_y )

def has_edge(from_node, to_node, system='default'):
    edges = get_set( from_node, system=system )
    return to_node in edges

def neighbors(node_x, system='default'):
    return get_set( node_x, system=system )

#--- Node values ----------------------------------------------
def get_node_value(node_x, system='default'):
    node_key = 'nv:%s' % node_x
    return get_redis(system).get( node_key )

def set_node_value(node_x, value, system='default'):
    node_key = 'nv:%s' % node_x
    return get_redis(system).set( node_key, value )

#--- Edge values ----------------------------------------------
def get_edge_value(edge_x, system='default'):
    edge_key = 'ev:%s' % edge_x
    return get_redis(system).get( edge_key )

def set_edge_value(edge_x, value, system='default'):
    edge_key = 'ev:%s' % edge_x
    return get_redis(system).set( edge_key, value )
30. Nov 2010 Announcements · Code · Database · Python
© Amir Salihefendic