Skip to main content

graphtype

Type hinting for networkx Graphs

Installation

pip install graphtype

Usage

There are two parts in graphtype: the type-hinting part, and the validation. You can use type-hinting with the provided class to indicate attributes graphs should possess, and the validation decorator to additionally ensure the format is respected in every function call.

Type-Hinting Graph

from graphtype import Graph

def do_something_to_graph(g: Graph)
    pass

Type-Hinting Graph Attributes GraphData

from graphtype import Graph, GraphData


def do_something_to_graph(g: Graph[GraphData["name"]])
    pass

# Each node must have a "name" attribute

def do_something_to_graph(g: Graph[GraphData["name": str]])
    pass

# type(g.graph["name"]) == str must be True

Type-Hinting Graph Attributes NodeData

from graphtype import Graph, NodeData

def do_something_to_graph(g: Graph[NodeData["feature"]])
    pass

# Each node must have a "feature" attribute

def do_something_to_graph(g: Graph[NodeData["feature": np.ndarray]])
    pass

# for n, d in g.nodes(data=True):
#   type(d["feature"]) == np.ndarray must be True

Type-Hinting Graph Attributes NodeData

from graphtype import Graph, EdgeData

def do_something_to_graph(g: Graph[EdgeData["feature"]])
    pass

# Each edge must have a "feature" attribute

def do_something_to_graph(g: Graph[EdgeData["feature": pd.DataFrame]])
    pass

# for u, v, d in g.edges(data=True):
#   type(d["feature"]) == pd.DataFrame must be True

Enforcing: @validate

The @validate decorator ensures that input Graphs have the right format when the function is called, otherwise raises TypeError.

@validate
def func(g: Graph[NodeData["feature1": pd.DataFrame, "feature2": int],
                  EdgeData["length": float, "counts": np.ndarray],
                  GraphData["name": str]],
         h: Graph[NodeData["feature1": pd.DataFrame, "feature2": int]],
         ):
    pass

This package is heavily inspired by Dataenforce.

Metadata

Release files for graphtype 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graphtype 0.1.0
File Size Uploaded
graphtype-0.1.0.tar.gz 9.1 kB Details

Release files / graphtype-0.1.0.tar.gz

Download URL graphtype-0.1.0.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d4a68ca80b97a306451458a6297acaa4bf196cc39d3d2603bdc1365ffcb8d330
BLAKE2b-256 checksum
How to use checksums
0456e6703057114e1d3e5cb8626032ab68cfdab039f64174e94f8c1c8d0acb16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.13

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page