nanoutils.schema

A module with schema-related utility functions.

See also

https://badge.fury.io/py/schema.svg

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

Default(value[, call])

A validation class akin to the likes of schemas.Use.

Formatter(msg)

A str subclass used for creating schema error messages.

supports_float(value)

Check if a float-like object has been passed (SupportsFloat).

supports_int(value)

Check if an int-like object has been passed (SupportsInt).

isinstance_factory(class_or_tuple[, module])

Return a function which checks if the passed object is an instance of class_or_tuple.

issubclass_factory(class_or_tuple[, module])

Return a function which checks if the passed class is a subclass of class_or_tuple.

import_factory(validate[, module])

Return a function which calls nanoutils.get_importable() with the validate argument.

API

class nanoutils.Default(value, call=True)[source]

A validation class akin to the likes of schemas.Use.

Upon executing Default.validate() returns the stored value. If call is True 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. If Default.call is True then the value is called (if possible) before its return.

Type:

object

call

Whether to call Default.value before its return (if possible) or not.

Type:

bool

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 and Default.call is True.

Return type:

object

class nanoutils.Formatter(msg)[source]

A str subclass used for creating schema error messages.

Examples

>>> from nanoutils import Formatter

>>> string = Formatter("{name}: {type} = {value}")
>>> string.format(1)
'value: int = 1'
format(obj)[source]

Return a formatted version of Formatter._msg.

Parameters:

obj (object) – The to-be formatted object.

Returns:

A formatted string.

Return type:

str

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
Parameters:

value (object) – The to-be evaluated object.

Returns:

Whether or not the passed value is float-like or not.

Return type:

bool

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
Parameters:

value (object) – The to-be evaluated object.

Returns:

Whether or not the passed value is int-like or not.

Return type:

bool

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 or Tuple[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:

Callable[[object], bool]

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 or Tuple[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:

Callable[[type], bool]

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:

Callable[[str], T]

See also

nanoutils.get_importable()

Import an object and, optionally, validate it using validate.