Skip to main content
https://travis-ci.org/mhallin/hesitate-py.svg?branch=master

Like assert, but less… assertive.


Are you skipping on writing your assert statements because of performance reasons? Skip no more! Hesitate will, through probabilistic means, make your Design by Contract-assertions faster!

How?

By not executing them.

…

Yeah.

Hesitate will measure the execution time on every assert statement you write, and execute the slow ones less frequently than the fast ones. You decide on what you think is a reasonable amount of time per assertion on average, and Hesitate will control how often your assertions run in order to match that.

It looks like this:

# worker.py

def do_work(data):
    assert data_is_valid(data)  # Super slow!

    return work_work(data)


# main.py

import hesitate
hesitate.attach_hook(
    target_timing=0.002,  # 2 milliseconds per assert on average
)

import worker
worker.do_work(invalid_data)  # Might not raise AssertionError! Who knows?

Hesitate works with an AST rewriter influenced to py.test. It hooks on to Python’s module loader where it intercepts and rewrites code on the fly as it is loaded. Hesitate stores assertion timings in a plain dictionary, so everything is forgotten when the application quits. This makes it most useful for long-living processes, such as application servers.

Installation

pip install hesitate

Call hesitate.attach_hook() as soon as possible when your application starts. It must be done before any modules you want Hesitate to plug in to are loaded. attach_hook takes three optional arguments:

  • initial_probability - the probability an assert is initially run, before its timing is known. The default is 1.0, which causes all asserts to trigger on their first run. Lower this value to avoid flooding the system with potentially slow assertions each time the application starts.

  • target_timing - the expected duration each assert should converge on, in seconds. The default is 0.1.

  • convergence_factor - the speed with which the measurements of an assert converges. The timing of an assert is low pass filtered, this controls how fast the filter follows. 1.0 means that it always uses the new value. 0.0 means that it will never change from its initial value. The default is 0.7.

Python Support

Hesitate is tested with Python 2.7, 3.2, 3.3, 3.4, and PyPy 2.4.

Django Integration

Hesitate comes pre-packaged with a Django application that integrates with your settings, and supports Django 1.6 and 1.7.

INSTALLED_APPS = (
    # Hesitate should be loaded first of all apps!
    'hesitate.wrappers.django',

    # The rest of your apps here
)

HESITATE_ENABLED = True  # Optional, defaults to the inverse of DEBUG

# These parameters reflect the attach_hook parameters and are all optional:
HESITATE_INITIAL_PROBABILITY = 1.0
HESITATE_TARGET_TIMING = 0.1
HESITATE_CONVERGENCE_FACTOR = 0.7

Metadata

Release files for hesitate 0.0.2

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

Source distribution (sdist)

Source distribution for hesitate 0.0.2
File Size Uploaded
hesitate-0.0.2.tar.gz 7.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hesitate 0.0.2
File Interpreter ABI Platform
hesitate-0.0.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 12.9 kB

Release files / hesitate-0.0.2.tar.gz

Download URL hesitate-0.0.2.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d2ad1735b96a61e0c4939a59df95e6a92d182bc0e1268e38414921d8b01c8290
BLAKE2b-256 checksum
How to use checksums
2191abd39bd7dbeeaa401ccfcf26b3b83ea6fe3164fe37cd9c9e4cca31cd8a3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / hesitate-0.0.2-py2.py3-none-any.whl

Download URL hesitate-0.0.2-py2.py3-none-any.whl
Size 5.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8e03af55f723819656809f726cfae29853df2fd8f112e69adb01a3782ae7861f
BLAKE2b-256 checksum
How to use checksums
928ca648ac346f500f235b79a27ffd1eab6f0f90a8665c1ecf92623b7deed1a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

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