Skip to main content

Basic Node/Edge model for SQLAlchemy

Project description

Graph node-edge relationships in SQLAlchemy (and soon more orms) + networkx integration.

The Goal

Set up a generalized abstraction/interface for a graph that can be used across various platforms and within various frameworks that is easy and simple to use, extensible, and that easily hooks into other graphing tools (like gephi networkx, etc.)

What’s here

A basic set of node/edge abstractions + many-to-one relationships for a graph represented in SQL with SQLAlchemy

Using this package

Documentation is available at http://graphalchemy.readthedocs.org

It’s very simple to use (and examples are to come). But for now the best thing to do is to go read the docs on SQLAlchemy

A very minimal example (using an SQLite database):

from graphalchemy.sqlmodels import create_base_classes, sqlite_connect
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
Node, Edge = create_base_classes(NodeClass="Node", EdgeClass="Edge", Base=Base)
engine, session = sqlite_connect("database.db", metadata=Base.metadata)

# be sure to use unicode!!
node1 = Node(label=u"First node!")
node2 = Node(label=u"Second node!")
node3 = Node(label=u"Third node!")
edge1 = Edge.connect_nodes(node1, node2)
edge2 = Edge.connect_nodes(node1, node3)

session.add_all([node1, node2, node3, edge1, edge2])
session.commit()


# now we can graph it

import networkx as nx
G = nx.Graph()
G.add_edges_from([edge1, edge2])

# now we can draw this! (if you had pylab, matplotlib, etc)

And you’d get a picture that looked something like this (clearly, we haven’t added all the traits and such in, but you get the idea):

https://github.com/jtratner/graphalchemy/raw/master/docs/images/readme-example.png

Now, obviously this is a pretty minimal example, but it shows how you can take advantage of the power of SQL joins, queries, etc, but also very easily

What’s going to be here

  1. networkx integration

  2. testing for multiple sql databases and adapters

  3. abstractions for Google App Engine, mongoalchemy, and possibly Django ORM

  4. adapter between networkx and web service requests (maybe?)

Testing coverage

Basic test suite that gets 100% line coverage for SQLAlchemy models and base models (still missing a test for Flask-SQLAlchemy). I’ve only run it on SQLite so far, but presumably it should work with other SQL databases just fine (since it uses SQLAlchemy’s declarative base)

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

graph-alchemy-0.1.0.tar.gz (12.2 kB view details)

Uploaded Source

File details

Details for the file graph-alchemy-0.1.0.tar.gz.

File metadata

  • Download URL: graph-alchemy-0.1.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for graph-alchemy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 54481aa54345c74fe926519a6bb93ab4c0c2f1ae6f2514e4ba637c594c3d2d46
MD5 057c3f7ce49c9304277aec935e5338bc
BLAKE2b-256 ec0fb82e94205882daa67d9fd3d847e0bd876ed1c4b50a2e9300065272256967

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page