Decorators

Function (and class) decorators which constitute the primary Paranoid Scientist interface

paranoid.decorators.accepts(*argtypes, **kwargtypes)[source]

A function decorator to specify argument types of the function.

Types may be specified either in the order that they appear in the function or via keyword arguments (just as if you were calling the function).

Example usage:

@accepts(Positive0)
def square_root(x):
paranoid.decorators.requires(condition, description=None)[source]

A function decorator to specify entry conditions for the function.

Entry conditions should be a string, which will be evaluated as Python code. Arguments of the function may be accessed by their name.

Optionally, the second argument (“description”) may describe the requirement in human-understandable language. This will be displayed in the error message, and may make it easier to debug software, or for users to know without looking at the code what may be wrong with the function call.

The special syntax “–>” and “<–>” may be used to mean “if” and “if and only if”, respectively. They may not be contained within sub-expressions.

Note that globals will not be included by default, and must be manually included using the “namespace” setting, set via settings.Settings.

Example usage:

@requires(“x >= y”)
def subtract(x, y):
@accepts(l=List(Number), log_transform=Boolean)
@requires(“log_transform == True –> min(l) > 0”)
def process_list(l, log_transform=False):
paranoid.decorators.returns(returntype)[source]

A function decorator to specify return type of the function.

Example usage:

@accepts(Positive0)
@returns(Positive0)
def square_root(x):
paranoid.decorators.ensures(condition)[source]

A function decorator to specify exit conditions for the function.

Exit conditions should be a string, which will be evaluated as Python code. Arguments of the function may be accessed by their name. The return value of the function may be accessed using the special variable name “return”.

The special syntax “–>” and “<–>” may be used to mean “if” and “if and only if”, respectively. They may not be contained within sub-expressions.

Values may be compared to previous executions of the function by including a “`” or “``” after them to check for higher order properties of the function.

Note that globals will not be included by default, and must be manually included using the “namespace” setting, set via settings.Settings.

Example usage:

@ensures(“lower_bound <= return <= upper_bound”)
def search(lower_bound, upper_bound):
@ensures(“x <= x` –> return <= return`”)
def monotonic(x):
paranoid.decorators.paranoidclass(cls)[source]

A class decorator to specify that class methods contain paranoid decorators.

Example usage:

@paranoidclass
class Point:
def __init__(self, x, y):
@returns(Number)
def distance_from_zero():
paranoid.decorators.paranoidconfig(**kwargs)[source]

A function decorator to set a local setting.

Settings may be set either globally (using settings.Settings.set()) or locally using this decorator. The setting name should be passed as a keyword argument, and the value to assign the setting should be passed as the value. See settings.Settings for the different settings which can be set.

Example usage:

@returns(Number)
@paranoidconfig(enabled=False)
def slow_function():