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:
objectThe 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.TypeOnly one valid value, which is passed at initialization
-
class
paranoid.types.base.Unchecked(typ=None)[source]¶ Bases:
paranoid.types.base.TypeUse type typ but do not check it.
-
class
paranoid.types.base.Generic(typ)[source]¶ Bases:
paranoid.types.base.TypeWraps 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.TypeFor 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.TypeUsed only as a placeholder for methods with a ‘self’ argument.
-
class
paranoid.types.base.Nothing(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.TypeThe None type.
-
class
paranoid.types.base.Function(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.TypeAny function.
-
class
paranoid.types.base.Boolean(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.TypeTrue or False
-
class
paranoid.types.base.And(*types)[source]¶ Bases:
paranoid.types.base.TypeConforms 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.TypeConforms 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.TypeValid 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.OrEither 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.TypeAlways fails.
Collections¶
-
class
paranoid.types.collections.Set(els)[source]¶ Bases:
paranoid.types.base.TypeAny 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.TypeA Python list.
-
class
paranoid.types.collections.Tuple(*args)[source]¶ Bases:
paranoid.types.base.TypeA Python tuple.
-
class
paranoid.types.collections.Dict(k, v)[source]¶ Bases:
paranoid.types.base.TypeA Python dictionary.
-
class
paranoid.types.collections.ParametersDict(params, all_mandatory=False)[source]¶ Bases:
paranoid.types.base.TypeA 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.TypeAny integer or float, including inf, -inf, and nan.
-
class
paranoid.types.numeric.ExtendedReal(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.TypeAny integer or float, excluding nan.
-
class
paranoid.types.numeric.Number(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.TypeAny integer or float, excluding inf, -inf, and nan.
-
class
paranoid.types.numeric.Integer(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.TypeAny integer.
-
class
paranoid.types.numeric.Natural0(*args, **kwargs)[source]¶ Bases:
paranoid.types.numeric.IntegerAny natural number including 0.
-
class
paranoid.types.numeric.Natural1(*args, **kwargs)[source]¶ Bases:
paranoid.types.numeric.IntegerAny natural number excluding 0.
-
class
paranoid.types.numeric.Range(low, high)[source]¶ Bases:
paranoid.types.numeric.NumberAny 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.RangeA half open interval from low (closed) to high (open).
-
class
paranoid.types.numeric.RangeOpenClosed(low, high)[source]¶ Bases:
paranoid.types.numeric.RangeA half open interval from low (open) to high (closed).
-
class
paranoid.types.numeric.RangeOpen(low, high)[source]¶ Bases:
paranoid.types.numeric.RangeAny number in the open interval from low to high.
-
class
paranoid.types.numeric.Positive0(*args, **kwargs)[source]¶ Bases:
paranoid.types.numeric.NumberA positive number, including zero.
-
class
paranoid.types.numeric.Positive(*args, **kwargs)[source]¶ Bases:
paranoid.types.numeric.NumberA positive number, excluding zero.
-
class
paranoid.types.numeric.NDArray(d=None, t=None)[source]¶ Bases:
paranoid.types.base.TypeA numpy ndarray of dimension d and type t.
String¶
-
class
paranoid.types.string.String(*args, **kwargs)[source]¶ Bases:
paranoid.types.base.TypeAny string.
-
class
paranoid.types.string.Identifier(*args, **kwargs)[source]¶ Bases:
paranoid.types.string.StringAny non-empty alphanumeric string with underscores and hyphens.
-
class
paranoid.types.string.Alphanumeric(*args, **kwargs)[source]¶ Bases:
paranoid.types.string.IdentifierAny non-empty alphanumeric string
-
class
paranoid.types.string.Latin(*args, **kwargs)[source]¶ Bases:
paranoid.types.string.AlphanumericAny non-empty string with latin characters only