Skip to main content

A DSL for graph writing

Project description

GraphDSL - A Python DSL for Graph Manipulation

GraphDSL is a Python library that allows you to define, manipulate, and generate graphs (directed or undirected) in a simple and readable way using a Domain-Specific Language (DSL) embedded directly in Python.

With this library, you can create graphs leveraging Python's syntax, making it easy to integrate into any Python script or Jupyter notebook. The defined graphs can then be manipulated with libraries such as NetworkX or iGraph.

Installation

pip install graphdsl

Usage

Declaring a Graph

To define a graph, you need to create a function decorated with the @Graph decorator. This decorator allows you to specify whether the graph is directed or undirected.

  • For an undirected graph: @Graph(directed=False)
  • For a directed graph: @Graph(directed=True)

Within the function, you can define nodes and edges using a simple syntax based on parentheses and dashes.

Example: Undirected Graph

@Graph(directed=False)
def g():
    (42) -{}- (72)

Example: Directed Graph

@Graph(directed=True)
def g():
    (42) -{}> (72)  # Directed edge from 42 to 72
    (72) <{}- (42)  # Directed edge from 72 to 42

DSL Syntax

The DSL offers an intuitive syntax for defining graphs, nodes, edges, and their properties.

Nodes

  • A node is defined by parentheses: (42)

  • A node can have a value and additional properties: (42, {weight: 3, color: 'red'})

    • Properties are optional, and keys do not need to be quoted.

Edges

Edges connect two nodes. The direction depends on whether the graph is directed or undirected.

Undirected Graph:
@Graph(directed=False)
def g():
    (1) -{}- (2)
Directed Graph
@Graph(directed=True)
def g():
    (1) -{}> (2)  # From 1 to 2
    (2) <{}- (1)  # From 2 to 1

Node and Edge Properties

You can add properties to both nodes and edges using a dictionary format.

Example with Properties:
@Graph(directed=True)
def g():
    (42, {color: 'blue'}) -{length: 3}> (72, {color: 'green'})

Chaining Nodes

You can chain multiple nodes and edges in a single line:

@Graph(directed=True)
def g():
    (1) -{}> (2) -{}> (3)
    (4) -{}> (5) -{}> (6)

Nodes Variables

You can assign nodes to variables for reuse:

@Graph(directed=True)
def g():
    central_node = ('Center', {weight: 100})
    central_node -{}> (42)
    (72) -{}> central_node

Default Properties

You can define default properties for nodes and edges. These properties will be applied unless explicitly overridden.

@Graph(directed=True, default_edge_params={'color': 'red'}, default_node_params={'size': 10})
def g():
    (42) -{}> (72) -{length: 3}> (93)

In this example, all nodes have the default property size=10, and edges have the default property color='red'. The edge between 72 and 93 has a redefined length of 3.

Generating the Graph

Backends

To generate the graph defined using the DSL, simply call the corresponding function. By default, the NetworkX backend is used, but you can also specify other backends such as iGraph.

Example with NetworkX (default):
p = g()  # Returns a NetworkX graph object
print(p.nodes)
Example with iGraph:
p = g(backend=backend.igraph)  # Returns an iGraph object
print(p.average_path_length())

Initial Graph

If you want to start from a pre-generated graph, you can use the graph_init option to provide a graph generation function:

p = g(graph_init=nx.house_graph)

Or with parameters:

p = g(graph_init=(nx.star_graph, 5))

Function Parameters

Graph functions are isolated from the rest of the Python script. If the properties of nodes or edges need to depend on external variables, you can pass them as parameters to the graph function:

@Graph(directed=True)
def g(c, l):
    (1, {color: c}) -{length: l}> (2, {color: c})

p = g(parameters={'c': 'red', 'l': 42})

License

This project is licensed under the MIT License.

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

graphdsl-0.0.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

GraphDSL-0.0.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file graphdsl-0.0.1.tar.gz.

File metadata

  • Download URL: graphdsl-0.0.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for graphdsl-0.0.1.tar.gz
Algorithm Hash digest
SHA256 641b5cbee5597134fd38a2172a45f1fe9d821efc988e35a31d46d09f8f9b2af9
MD5 a32c5781b6b5ce91325b417fec9e510a
BLAKE2b-256 5ee4dc8ec3f1bc89d1fc7d167808d047973d237ff766d7241b2f2f9b7055784a

See more details on using hashes here.

File details

Details for the file GraphDSL-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: GraphDSL-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for GraphDSL-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3bc5423423ba263c21c9d68e98308e3bfe357eb6dd441a982cc7bdfd69d7be44
MD5 f581e379277437b13816cf7e0b472ec4
BLAKE2b-256 edef85931cd64ba38aeac43aebe40c8f0c58e688444ad328fa38999462e27892

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