Skip to main content

A Hypothesis strategy for generating NetworkX graphs

Project description

Build Status codecov

Hypothesis-networkx

This module provides a Hypothesis strategy for generating networkx graphs. This can be used to efficiently and thoroughly test your code.

Installation

This module can be installed via pip:

pip install hypothesis-networkx

User guide

The module exposes a single function: graph_builder. This function is a hypothesis composite strategy for building graphs. You can use it as follows:

from hypothesis_networkx import graph_builder
from hypothesis import strategies as st
import networkx as nx

node_data = st.fixed_dictionaries({'name': st.text(),
                                   'number': st.integers()})
edge_data = st.fixed_dictionaries({'weight': st.floats(allow_nan=False,
                                                       allow_infinity=False)})


builder = graph_builder(graph_type=nx.Graph,
                        node_keys=st.integers(),
                        node_data=node_data,
                        edge_data=edge_data,
                        min_nodes=2, max_nodes=10,
                        min_edges=1, max_edges=None,
                        self_loops=False,
                        connected=True)

graph = builder.example()
print(graph.nodes(data=True))
print(graph.edges(data=True))

Of course this builder is a valid hypothesis strategy, and using it to just make examples is not super useful. Instead, you can (and should) use it in your testing framework:

from hypothesis import given

@given(graph=builder)
def test_my_function(graph):
    assert my_function(graph) == known_function(graph)

The meaning of the arguments given to graph_builder are pretty self-explanatory, but they must be given as keyword arguments.

  • node_data: The strategy from which node attributes will be drawn.
  • edge_data: The strategy from which edge attributes will be drawn.
  • node_keys: Either the strategy from which node keys will be draw, or None. If None, node keys will be integers from the range (0, number of nodes).
  • min_nodes and max_nodes: The minimum and maximum number of nodes the produced graphs will contain.
  • min_edges and max_edges: The minimum and maximum number of edges the produced graphs will contain. Note that less edges than min_edges may be added if there are not enough nodes, and more than max_edges if connected is True.
  • graph_type: This function (or class) will be called without arguments to create an empty initial graph.
  • connected: If True, the generated graph is guaranteed to be a single connected component.
  • self_loops: If False, there will be no self-loops in the generated graph. Self-loops are edges between a node and itself.

Known limitations

There are a few (minor) outstanding issues with this module:

  • Graph generation may be slow for large graphs.
  • The min_edges argument is not always respected when the produced graph is too small.
  • The max_edges argument is not always respected if connected is True.
  • It currently works for Python 2.7, but this is considered deprecated and may stop working without notice.

See also

Networkx

Hypothesis

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

hypothesis_networkx-0.2.2.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

hypothesis_networkx-0.2.2-py2.py3-none-any.whl (10.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file hypothesis_networkx-0.2.2.tar.gz.

File metadata

  • Download URL: hypothesis_networkx-0.2.2.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for hypothesis_networkx-0.2.2.tar.gz
Algorithm Hash digest
SHA256 2690c7d8578ff6e5a3cece793a79a5d11994f5a760d2354f23e6cf0625bf2bde
MD5 5a86950f01e4633c70db5f318793e7d6
BLAKE2b-256 7911b27cc7c6a521ef7b713a8bad460bcd6dcc6c76b7f1c3c41ab2f188c009b7

See more details on using hashes here.

File details

Details for the file hypothesis_networkx-0.2.2-py2.py3-none-any.whl.

File metadata

  • Download URL: hypothesis_networkx-0.2.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for hypothesis_networkx-0.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 10d53c6c526570d809235f5a21ae1f1e031a334f6d57c97389cec14b39bad0f8
MD5 35d6fef99e859ad3391e0e6e14ae80dc
BLAKE2b-256 b55629c720778e8bde6b66a408d9927e2d1caaaa1636f7621d9cccba33e39f43

See more details on using hashes here.

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