[![Build
Status](https://travis-ci.org/pckroon/hypothesis-networkx.svg?branch=master)](https://travis-ci.org/pckroon/hypothesis-networkx)
[![codecov](https://codecov.io/gh/pckroon/hypothesis-networkx/branch/master/graph/badge.svg)](https://codecov.io/gh/pckroon/hypothesis-networkx)
# 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:
```python3
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:
```python3
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](https://networkx.github.io/documentation/stable/index.html)
[Hypothesis](https://hypothesis.readthedocs.io/en/latest/index.html)
Raw data
{
"_id": null,
"home_page": "https://github.com/pckroon/hypothesis-networkx",
"name": "hypothesis-networkx",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "hypothesis networkx testing",
"author": "P C Kroon",
"author_email": "p.c.kroon@rug.nl",
"download_url": "https://files.pythonhosted.org/packages/6a/a1/9300093166310e734dad3396531c38a3e32f0da3b977884e7899d74f7a0d/hypothesis_networkx-0.3.0.tar.gz",
"platform": null,
"description": "[![Build\nStatus](https://travis-ci.org/pckroon/hypothesis-networkx.svg?branch=master)](https://travis-ci.org/pckroon/hypothesis-networkx)\n[![codecov](https://codecov.io/gh/pckroon/hypothesis-networkx/branch/master/graph/badge.svg)](https://codecov.io/gh/pckroon/hypothesis-networkx)\n\n# Hypothesis-networkx\n\nThis module provides a Hypothesis strategy for generating networkx graphs.\nThis can be used to efficiently and thoroughly test your code.\n\n## Installation\n\nThis module can be installed via `pip`:\n```\npip install hypothesis-networkx\n```\n\n## User guide\n\nThe module exposes a single function: `graph_builder`. This function is a\nhypothesis composite strategy for building graphs. You can use it as follows:\n\n```python3\nfrom hypothesis_networkx import graph_builder\nfrom hypothesis import strategies as st\nimport networkx as nx\n\nnode_data = st.fixed_dictionaries({'name': st.text(),\n 'number': st.integers()})\nedge_data = st.fixed_dictionaries({'weight': st.floats(allow_nan=False,\n allow_infinity=False)})\n\n\nbuilder = graph_builder(graph_type=nx.Graph,\n node_keys=st.integers(),\n node_data=node_data,\n edge_data=edge_data,\n min_nodes=2, max_nodes=10,\n min_edges=1, max_edges=None,\n self_loops=False,\n connected=True)\n\ngraph = builder.example()\nprint(graph.nodes(data=True))\nprint(graph.edges(data=True))\n```\n\nOf course this builder is a valid hypothesis strategy, and using it to just\nmake examples is not super useful. Instead, you can (and should) use it in\nyour testing framework:\n\n```python3\nfrom hypothesis import given\n\n@given(graph=builder)\ndef test_my_function(graph):\n assert my_function(graph) == known_function(graph)\n\n```\n\nThe meaning of the arguments given to `graph_builder` are pretty\nself-explanatory, but they *must* be given as keyword arguments. \n - `node_data`: The strategy from which node attributes will be drawn.\n - `edge_data`: The strategy from which edge attributes will be drawn.\n - `node_keys`: Either the strategy from which node keys will be draw, or\n None. If None, node keys will be integers from the range (0, number of nodes).\n - `min_nodes` and `max_nodes`: The minimum and maximum number of nodes the \n produced graphs will contain.\n - `min_edges` and `max_edges`: The minimum and maximum number of edges the\n produced graphs will contain. Note that less \n edges than `min_edges` may be added if there \n are not enough nodes, and more than\n `max_edges` if `connected` is True.\n - `graph_type`: This function (or class) will be called without arguments to\n create an empty initial graph.\n - `connected`: If True, the generated graph is guaranteed to be a single\n connected component.\n - `self_loops`: If False, there will be no self-loops in the generated graph.\n Self-loops are edges between a node and itself.\n\n## Known limitations\n\nThere are a few (minor) outstanding issues with this module:\n\n - Graph generation may be slow for large graphs.\n - The `min_edges` argument is not always respected when the produced graph\n is too small.\n - The `max_edges` argument is not always respected if `connected` is True.\n - It currently works for Python 2.7, but this is considered deprecated and\n may stop working without notice.\n\n## See also\n\n[Networkx](https://networkx.github.io/documentation/stable/index.html)\n\n[Hypothesis](https://hypothesis.readthedocs.io/en/latest/index.html)\n\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "A Hypothesis strategy for generating NetworkX graphs",
"version": "0.3.0",
"project_urls": {
"Homepage": "https://github.com/pckroon/hypothesis-networkx"
},
"split_keywords": [
"hypothesis",
"networkx",
"testing"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1dd20a05f16d030f96e91e4e89fa0be96f7554b7c0d9992c5ec716fc6608df93",
"md5": "a47c2521909ff6595942709604dddf98",
"sha256": "85ae8c4986cd311f0edebcf720b4b3a31ffa7800d560b6f1aa85efb38b4178eb"
},
"downloads": -1,
"filename": "hypothesis_networkx-0.3.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "a47c2521909ff6595942709604dddf98",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 10574,
"upload_time": "2023-09-15T12:49:31",
"upload_time_iso_8601": "2023-09-15T12:49:31.223144Z",
"url": "https://files.pythonhosted.org/packages/1d/d2/0a05f16d030f96e91e4e89fa0be96f7554b7c0d9992c5ec716fc6608df93/hypothesis_networkx-0.3.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6aa19300093166310e734dad3396531c38a3e32f0da3b977884e7899d74f7a0d",
"md5": "4b58575a4307edf3ee2b083589174535",
"sha256": "a8211dec83cbeea6b3fcddd9fc6879d852d15f7a3bc2896045057d0cd67ebfbe"
},
"downloads": -1,
"filename": "hypothesis_networkx-0.3.0.tar.gz",
"has_sig": false,
"md5_digest": "4b58575a4307edf3ee2b083589174535",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 12298,
"upload_time": "2023-09-15T12:49:32",
"upload_time_iso_8601": "2023-09-15T12:49:32.934505Z",
"url": "https://files.pythonhosted.org/packages/6a/a1/9300093166310e734dad3396531c38a3e32f0da3b977884e7899d74f7a0d/hypothesis_networkx-0.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-09-15 12:49:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pckroon",
"github_project": "hypothesis-networkx",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "hypothesis-networkx"
}