Types¶
Base¶
-
paranoid.types.base.
TypeFactory
(v)[source]¶ Ensure v is a valid Type.
This function is used to convert user-specified types into internal types for the verification engine. It allows Type subclasses, Type subclass instances, Python type, and user-defined classes to be passed. Returns an instance of the type of v.
Users should never access this function directly.
-
class
paranoid.types.base.
Type
(*args, **kwargs)[source]¶ Bases:
object
The base Type, from which all variable types should inherit.
What is a Type? While “types” can include standard types built into the programming language (e.g. int, float, string), they can also be more flexible. For example, you can have a “natural numbers” type, or a “range” type, or even a “file path” type.
All types must inherit from this class. They must define the “test” and “generate” functions.
-
class
paranoid.types.base.
Constant
(const)[source]¶ Bases:
paranoid.types.base.Type
Only one valid value, which is passed at initialization
-
class
paranoid.types.base.
Unchecked
(typ=None)[source]¶ Bases:
paranoid.types.base.Type
Use type typ but do not check it.
-
class
paranoid.types.base.
Generic
(typ)[source]¶ Bases:
paranoid.types.base.Type
Wraps Python classes to turn them into Types.
Classes may optionally contain the “_test_(v)” or “_generate()” static methods; adding these two functions gives them the same power as traditional Types. “_test(v)” should check if v is a valid member of the class using assert statements only, and “_generate()” should yield a finite number of instances of the class.
-
class
paranoid.types.base.
InitGeneric
(typ)[source]¶ Bases:
paranoid.types.base.Type
For the self argument passed to __init__. Should not be used directly, use Self instead.
Before a class is initialized (i.e. __init__ is called), it may not be valid according to its _test method. Likewise, passing a fully initialized class value to __init__ through the self parameter may cause problems, as the self parameter for __init__ is the output of the __new__ method. Thus, the __init__ function must be handled separately. This tests only object identity, and generates types based on the __new__ method. This may fail for
-
class
paranoid.types.base.
Self
(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.Type
Used only as a placeholder for methods with a ‘self’ argument.
-
class
paranoid.types.base.
Nothing
(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.Type
The None type.
-
class
paranoid.types.base.
Function
(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.Type
Any function.
-
class
paranoid.types.base.
Boolean
(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.Type
True or False
-
class
paranoid.types.base.
And
(*types)[source]¶ Bases:
paranoid.types.base.Type
Conforms to all of the given types.
Any number of Types can be passed to And. The And of these types is the logical AND of them, i.e. a value must conform to each of the types.
-
class
paranoid.types.base.
Or
(*types)[source]¶ Bases:
paranoid.types.base.Type
Conforms to any of the given types.
Any number of Types can be passed to Or. The Or of these types is the logical OR of them, i.e. a value must conform to at least one the types.
-
class
paranoid.types.base.
Not
(typ)[source]¶ Bases:
paranoid.types.base.Type
Valid if the given type fails.
Takes one type as an argument. Valid values are not of this type.
Note that this should be avoided when possible, as it cannot generate values. It is most useful within an And clause.
-
class
paranoid.types.base.
Maybe
(typ)[source]¶ Bases:
paranoid.types.base.Or
Either the given type or None.
One Type may be passed to Maybe. Maybe checks to see whether it is either an element of the passed type or else None. This is useful for optional arguments which take None as the default parameter.
-
class
paranoid.types.base.
Void
(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.Type
Always fails.
Collections¶
-
class
paranoid.types.collections.
Set
(els)[source]¶ Bases:
paranoid.types.base.Type
Any element which is a member of els.
els can be one of several standard Python types, including a list, a tuple, or a set. Any object with the __contains__ function is valid. This ensures that a value is contained within els.
-
class
paranoid.types.collections.
List
(t)[source]¶ Bases:
paranoid.types.base.Type
A Python list.
-
class
paranoid.types.collections.
Tuple
(*args)[source]¶ Bases:
paranoid.types.base.Type
A Python tuple.
-
class
paranoid.types.collections.
Dict
(k, v)[source]¶ Bases:
paranoid.types.base.Type
A Python dictionary.
-
class
paranoid.types.collections.
ParametersDict
(params, all_mandatory=False)[source]¶ Bases:
paranoid.types.base.Type
A Python dictionary with limited keys.
Represents a set of parameters. params, the single argument, should be a dictionary, where the keys are strings representing the parameter names the values are Types. The only keys allowed in a ParametersDict are the keys in params. The values for each key must be of the type specified. Note that not all of the keys in params must be specified for this type to be valid.
Numeric¶
-
class
paranoid.types.numeric.
Numeric
(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.Type
Any integer or float, including inf, -inf, and nan.
-
class
paranoid.types.numeric.
ExtendedReal
(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.Type
Any integer or float, excluding nan.
-
class
paranoid.types.numeric.
Number
(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.Type
Any integer or float, excluding inf, -inf, and nan.
-
class
paranoid.types.numeric.
Integer
(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.Type
Any integer.
-
class
paranoid.types.numeric.
Natural0
(*args, **kwargs)[source]¶ Bases:
paranoid.types.numeric.Integer
Any natural number including 0.
-
class
paranoid.types.numeric.
Natural1
(*args, **kwargs)[source]¶ Bases:
paranoid.types.numeric.Integer
Any natural number excluding 0.
-
class
paranoid.types.numeric.
Range
(low, high)[source]¶ Bases:
paranoid.types.numeric.Number
Any integer or float from low to high, inclusive.
Note that this does NOT include correction for floating point roundoff errors. This is because, if there are floating point roundoff errors, some code may fail.
-
class
paranoid.types.numeric.
RangeClosedOpen
(low, high)[source]¶ Bases:
paranoid.types.numeric.Range
A half open interval from low (closed) to high (open).
-
class
paranoid.types.numeric.
RangeOpenClosed
(low, high)[source]¶ Bases:
paranoid.types.numeric.Range
A half open interval from low (open) to high (closed).
-
class
paranoid.types.numeric.
RangeOpen
(low, high)[source]¶ Bases:
paranoid.types.numeric.Range
Any number in the open interval from low to high.
-
class
paranoid.types.numeric.
Positive0
(*args, **kwargs)[source]¶ Bases:
paranoid.types.numeric.Number
A positive number, including zero.
-
class
paranoid.types.numeric.
Positive
(*args, **kwargs)[source]¶ Bases:
paranoid.types.numeric.Number
A positive number, excluding zero.
-
class
paranoid.types.numeric.
NDArray
(d=None, t=None)[source]¶ Bases:
paranoid.types.base.Type
A numpy ndarray of dimension d and type t.
String¶
-
class
paranoid.types.string.
String
(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.Type
Any string.
-
class
paranoid.types.string.
Identifier
(*args, **kwargs)[source]¶ Bases:
paranoid.types.string.String
Any non-empty alphanumeric string with underscores and hyphens.
-
class
paranoid.types.string.
Alphanumeric
(*args, **kwargs)[source]¶ Bases:
paranoid.types.string.Identifier
Any non-empty alphanumeric string
-
class
paranoid.types.string.
Latin
(*args, **kwargs)[source]¶ Bases:
paranoid.types.string.Alphanumeric
Any non-empty string with latin characters only