hypothesis strategies for geometric objects (points, polygons, etc.).
Project description
hypothesis_geometry
In what follows
python
is an alias forpython3.5
or any later version (python3.6
and so on),pypy
is an alias forpypy3.5
or any later version (pypy3.6
and so on).
Installation
Install the latest pip
& setuptools
packages versions:
- with
CPython
python -m pip install --upgrade pip setuptools
- with
PyPy
pypy -m pip install --upgrade pip setuptools
User
Download and install the latest stable version from PyPI
repository:
- with
CPython
python -m pip install --upgrade hypothesis_geometry
- with
PyPy
pypy -m pip install --upgrade hypothesis_geometry
Developer
Download the latest version from GitHub
repository
git clone https://github.com/lycantropos/hypothesis_geometry.git
cd hypothesis_geometry
Install dependencies:
- with
CPython
python -m pip install --force-reinstall -r requirements.txt
- with
PyPy
pypy -m pip install --force-reinstall -r requirements.txt
Install:
- with
CPython
python setup.py install
- with
PyPy
pypy setup.py install
Usage
With setup
>>> from hypothesis import strategies
>>> from hypothesis_geometry import planar
>>> min_coordinate, max_coordinate = -100, 100
>>> coordinates_type = int
>>> coordinates = strategies.integers(min_coordinate, max_coordinate)
>>> import warnings
>>> from hypothesis.errors import NonInteractiveExampleWarning
>>> # ignore hypothesis warnings caused by `example` method call
... warnings.filterwarnings('ignore', category=NonInteractiveExampleWarning)
let's take a look at what can be generated and how.
Points
>>> points = planar.points(coordinates)
>>> point = points.example()
>>> isinstance(point, tuple)
True
>>> len(point) == 2
True
>>> all(isinstance(coordinate, coordinates_type) for coordinate in point)
True
>>> all(min_coordinate <= coordinate <= max_coordinate for coordinate in point)
True
Segments
>>> segments = planar.segments(coordinates)
>>> segment = segments.example()
>>> isinstance(segment, tuple)
True
>>> len(segment) == 2
True
>>> all(isinstance(endpoint, tuple) for endpoint in segment)
True
>>> all(len(endpoint) == 2 for endpoint in segment)
True
>>> all(all(isinstance(coordinate, coordinates_type)
... for coordinate in endpoint)
... for endpoint in segment)
True
>>> all(all(min_coordinate <= coordinate <= max_coordinate
... for coordinate in endpoint)
... for endpoint in segment)
True
Polylines
>>> min_size, max_size = 5, 10
>>> polylines = planar.polylines(coordinates,
... min_size=min_size,
... max_size=max_size)
>>> polyline = polylines.example()
>>> isinstance(polyline, list)
True
>>> min_size <= len(polyline) <= max_size
True
>>> all(isinstance(vertex, tuple) for vertex in polyline)
True
>>> all(len(vertex) == 2 for vertex in polyline)
True
>>> all(all(isinstance(coordinate, coordinates_type)
... for coordinate in vertex)
... for vertex in polyline)
True
>>> all(all(min_coordinate <= coordinate <= max_coordinate
... for coordinate in vertex)
... for vertex in polyline)
True
Contours
>>> min_size, max_size = 5, 10
>>> contours = planar.contours(coordinates,
... min_size=min_size,
... max_size=max_size)
>>> contour = contours.example()
>>> isinstance(contour, list)
True
>>> min_size <= len(contour) <= max_size
True
>>> all(isinstance(vertex, tuple) for vertex in contour)
True
>>> all(len(vertex) == 2 for vertex in contour)
True
>>> all(all(isinstance(coordinate, coordinates_type)
... for coordinate in vertex)
... for vertex in contour)
True
>>> all(all(min_coordinate <= coordinate <= max_coordinate
... for coordinate in vertex)
... for vertex in contour)
True
also planar.concave_contours
& planar.convex_contours
options are available.
Polygons
>>> min_size, max_size = 5, 10
>>> min_holes_size, max_holes_size = 1, 3
>>> min_hole_size, max_hole_size = 4, 8
>>> polygons = planar.polygons(coordinates,
... min_size=min_size,
... max_size=max_size,
... min_holes_size=min_holes_size,
... max_holes_size=max_holes_size,
... min_hole_size=min_hole_size,
... max_hole_size=max_hole_size)
>>> polygon = polygons.example()
>>> isinstance(polygon, tuple)
True
>>> len(polygon) == 2
True
>>> border, holes = polygon
>>> isinstance(border, list)
True
>>> all(isinstance(hole, list) for hole in holes)
True
>>> min_size <= len(border) <= max_size
True
>>> min_holes_size <= len(holes) <= max_holes_size
True
>>> all(min_hole_size <= len(hole) <= max_hole_size for hole in holes)
True
>>> contours = [border, *holes]
>>> all(isinstance(vertex, tuple)
... for contour in contours
... for vertex in contour)
True
>>> all(len(vertex) == 2
... for contour in contours
... for vertex in contour)
True
>>> all(all(isinstance(coordinate, coordinates_type)
... for coordinate in vertex)
... for contour in contours
... for vertex in contour)
True
>>> all(all(min_coordinate <= coordinate <= max_coordinate
... for coordinate in vertex)
... for contour in contours
... for vertex in contour)
True
Caveats
-
Strategies may be slow depending on domain, so it may be necessary to add
HealthCheck.filter_too_much
,HealthCheck.too_slow
insuppress_health_check
and setdeadline
toNone
. -
Unbounded floating point strategies for coordinates (like
hypothesis.strategies.floats
with unsetmin_value
/max_value
) do not play well with bounded sizes and may cause a lot of searching iterations with no success, so it is recommended to use bounded floating point coordinates with bounded sizes or unbounded coordinates with unbounded sizes. -
decimal.Decimal
coordinates are not supported, because they seem to be too hard to work with correctly (e.g. sometimes self-intersecting contours arise), so it is suggested to usefloat
orfractions.Fraction
instead.
Development
Bumping version
Preparation
Install bump2version.
Pre-release
Choose which version number category to bump following semver specification.
Test bumping version
bump2version --dry-run --verbose $CATEGORY
where $CATEGORY
is the target version number category name, possible
values are patch
/minor
/major
.
Bump version
bump2version --verbose $CATEGORY
This will set version to major.minor.patch-alpha
.
Release
Test bumping version
bump2version --dry-run --verbose release
Bump version
bump2version --verbose release
This will set version to major.minor.patch
.
Running tests
Install dependencies:
- with
CPython
python -m pip install --force-reinstall -r requirements-tests.txt
- with
PyPy
pypy -m pip install --force-reinstall -r requirements-tests.txt
Plain
pytest
Inside Docker
container:
- with
CPython
docker-compose --file docker-compose.cpython.yml up
- with
PyPy
docker-compose --file docker-compose.pypy.yml up
Bash
script (e.g. can be used in Git
hooks):
-
with
CPython
./run-tests.sh
or
./run-tests.sh cpython
-
with
PyPy
./run-tests.sh pypy
PowerShell
script (e.g. can be used in Git
hooks):
- with
CPython
.\run-tests.ps1
or.\run-tests.ps1 cpython
- with
PyPy
.\run-tests.ps1 pypy
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for hypothesis_geometry-0.9.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04250d186a646a2bd8c0af0887452b1929b28d13c25c86f6454dff9a73bf7fcb |
|
MD5 | 6309d0da708617d6fa69dc1d6e6ac8b5 |
|
BLAKE2b-256 | 293a472dec8f2c3c1a820cc3ed4792323fa1d11e396cfb2e2c9b8f7bfb7a112b |
Hashes for hypothesis_geometry-0.9.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccffb046c1635d11d8faf92a65e5fca0fed92e00a9f87e325e9a3f2e7368f147 |
|
MD5 | 1722aac47176075cd8efbb8680d49ca9 |
|
BLAKE2b-256 | 20fc5e48d67a1c551764050780d65e9d9c0f1acb773512e3b345b88262fd05d4 |