nanoutils.schema¶
A module with schema
-related utility functions.
See also
schema is a library for validating Python data structures, such as those obtained from config-files, forms, external services or command-line parsing, converted from JSON/YAML (or something else) to Python data-types.
Index¶
|
A validation class akin to the likes of |
|
A |
|
Check if a float-like object has been passed ( |
|
Check if an int-like object has been passed ( |
|
Return a function which checks if the passed object is an instance of class_or_tuple. |
|
Return a function which checks if the passed class is a subclass of class_or_tuple. |
|
Return a function which calls |
API¶
- class nanoutils.Default(value, call=True)[source]¶
A validation class akin to the likes of
schemas.Use
.Upon executing
Default.validate()
returns the storedvalue
. Ifcall
isTrue
and the value is a callable, then it is called before its return.Examples
>>> from schema import Schema, And >>> from nanoutils import Default >>> schema1 = Schema(And(int, Default(True))) >>> schema1.validate(1) True >>> schema2 = Schema(And(int, Default(dict))) >>> schema2.validate(1) {} >>> schema3 = Schema(And(int, Default(dict, call=False))) >>> schema3.validate(1) <class 'dict'>
- value¶
The to-be return value for when
Default.validate()
is called. IfDefault.call
isTrue
then the value is called (if possible) before its return.- Type:
- call¶
Whether to call
Default.value
before its return (if possible) or not.- Type:
- validate(*args, **kwargs)[source]¶
Validate the passed data.
- Parameters:
*args/**kwargs – Variadic (keyword) arguments to ensure signature compatibility. Supplied values will not be used.
- Returns:
Return
Default.value
. The to-be returned value will be called if it is a callable andDefault.call
isTrue
.- Return type:
- class nanoutils.Formatter(msg)[source]¶
A
str
subclass used for creatingschema
error messages.Examples
>>> from nanoutils import Formatter >>> string = Formatter("{name}: {type} = {value}") >>> string.format(1) 'value: int = 1'
- nanoutils.supports_float(value)[source]¶
Check if a float-like object has been passed (
SupportsFloat
).Examples
>>> from nanoutils import supports_float >>> supports_float(1.0) True >>> supports_float(1) True >>> supports_float('1.0') True >>> supports_float('not a float') False
- nanoutils.supports_int(value)[source]¶
Check if an int-like object has been passed (
SupportsInt
).Examples
>>> from nanoutils import supports_int >>> supports_int(1.0) True >>> supports_int(1.5) False >>> supports_int(1) True >>> supports_int('1') True >>> supports_int('not a int') False
- nanoutils.isinstance_factory(class_or_tuple, module='nanoutils.schema')[source]¶
Return a function which checks if the passed object is an instance of class_or_tuple.
Examples
>>> from nanoutils import isinstance_factory >>> func = isinstance_factory(int) >>> func(1) # isinstance(1, int) True >>> func(1.0) # isinstance(1.0, int) False
- Parameters:
class_or_tuple (
type
orTuple[type, ...]
) – A type object or tuple of type objects.module (
str
) – The__module__
of the to-be returned function.
- Returns:
A function which asserts the passed object is an instance of class_or_tuple.
- Return type:
See also
isinstance()
Return whether an object is an instance of a class or of a subclass thereof.
- nanoutils.issubclass_factory(class_or_tuple, module='nanoutils.schema')[source]¶
Return a function which checks if the passed class is a subclass of class_or_tuple.
Examples
>>> from nanoutils import issubclass_factory >>> func = issubclass_factory(int) >>> func(bool) # issubclass(bool, int) True >>> func(float) # issubclass(float, int) False
- Parameters:
class_or_tuple (
type
orTuple[type, ...]
) – A type object or tuple of type objects.module (
str
) – The__module__
of the to-be returned function.
- Returns:
A function which asserts the passed type is a subclass of class_or_tuple.
- Return type:
See also
issubclass()
Return whether cls is a derived from another class or is the same class.
- nanoutils.import_factory(validate, module='nanoutils.schema')[source]¶
Return a function which calls
nanoutils.get_importable()
with the validate argument.Examples
>>> from inspect import isclass >>> from nanoutils import import_factory >>> func = import_factory(isclass) >>> func('builtins.dict') <class 'dict'> >>> func('builtins.len') Traceback (most recent call last): ... RuntimeError: Passing <built-in function len> to isclass() failed to return True
- Parameters:
validate (
Callable[[T], bool]
) – A callable used for validating the passed object.module (
str
) – The__module__
of the to-be returned function.
- Returns:
A function for importing the passed object and validating it using validate.
- Return type:
See also
nanoutils.get_importable()
Import an object and, optionally, validate it using validate.