ptypes module

The pseudo-types defined below can be used in contracts of the PyContracts library or pexdoc.pcontracts module. As an example, with the latter:

>>> from __future__ import print_function
>>> import pexdoc
>>> from pexdoc.ptypes import non_negative_integer
>>> @pexdoc.pcontracts.contract(num='non_negative_integer')
... def myfunc(num):
...     print('Number received: '+str(num))
...
>>> myfunc(10)
Number received: 10
>>> myfunc('a')
Traceback (most recent call last):
    ...
RuntimeError: Argument `num` is not valid

Alternatively each pseudo-type has a checker function associated with it that can be used to verify membership. For example:

>>> import pexdoc
>>> # None is returned if object belongs to pseudo-type
>>> pexdoc.ptypes.non_negative_integer(10)
>>> # ValueError is raised if object does not belong to pseudo-type
>>> pexdoc.ptypes.non_negative_integer('a') 
Traceback (most recent call last):
    ...
ValueError: [START CONTRACT MSG: non_negative_integer]...

Description

FileName

Import as file_name. String with a valid file name

FileNameExists

Import as file_name_exists. String with a file name that exists in the file system

Function

Import as function. Callable pointer or None

NonNegativeInteger

Import as non_negative_integer. Integer greater or equal to zero

NonNullString

Import as non_null_string. String of length 1 or higher

OffsetRange

Import as offset_range. Number in the [0, 1] range

PositiveRealNum

Import as positive_real_num. Integer or float greater than zero or None

RealNum

Import as real_num. Integer, float or None

Checker functions

pexdoc.ptypes.file_name(obj)

Validate if an object is a legal name for a file.

Parameters:obj (any) – Object
Raises:RuntimeError (Argument `*[argument_name]*` is not valid). The token *[argument_name]* is replaced by the name of the argument the contract is attached to
Return type:None
pexdoc.ptypes.file_name_exists(obj)

Validate if an object is a legal name for a file and that the file exists.

Parameters:

obj (any) – Object

Raises:
  • OSError (File [fname] could not be found). The token *[fname]* is replaced by the value of the argument the contract is attached to
  • RuntimeError (Argument `*[argument_name]*` is not valid). The token *[argument_name]* is replaced by the name of the argument the contract is attached to
Return type:

None

pexdoc.ptypes.function(obj)

Validate if an object is a function pointer or None.

Parameters:obj (any) – Object
Raises:RuntimeError (Argument `*[argument_name]*` is not valid). The token *[argument_name]* is replaced by the name of the argument the contract is attached to
Return type:None
pexdoc.ptypes.non_negative_integer(obj)

Validate if an object is a non-negative (zero or positive) integer.

Parameters:obj (any) – Object
Raises:RuntimeError (Argument `*[argument_name]*` is not valid). The token *[argument_name]* is replaced by the name of the argument the contract is attached to
Return type:None
pexdoc.ptypes.non_null_string(obj)

Validate if an object is a non-null string.

Parameters:obj (any) – Object
Raises:RuntimeError (Argument `*[argument_name]*` is not valid). The token *[argument_name]* is replaced by the name of the argument the contract is attached to
Return type:None
pexdoc.ptypes.offset_range(obj)

Validate if an object is a number in the [0, 1] range.

Parameters:obj (any) – Object
Raises:RuntimeError (Argument `*[argument_name]*` is not valid). The token *[argument_name]* is replaced by the name of the argument the contract is attached to
Return type:None
pexdoc.ptypes.positive_real_num(obj)

Validate if an object is a positive integer, positive float or None.

Parameters:obj (any) – Object
Raises:RuntimeError (Argument `*[argument_name]*` is not valid). The token *[argument_name]* is replaced by the name of the argument the contract is attached to
Return type:None
pexdoc.ptypes.real_num(obj)

Validate if an object is an integer, float or None.

Parameters:obj (any) – Object
Raises:RuntimeError (Argument `*[argument_name]*` is not valid). The token *[argument_name]* is replaced by the name of the argument the contract is attached to
Return type:None