Typerighter¶
Data Types for Cynical Humans.
Welcome to Typerighter, a project that makes it easy to structure and process data using concepts from type systems.
Example¶
A Type.
>>> st = types.StringType(max_length=12)
A Record.
>>> class Artist(types.Record):
... name = types.StringType(required=True)
... website = types.URLType()
...
>>> artist_type = Artist()
Validate data.
>>> data = 'Take Five'
>>> st.validate(data)
>>>
>>> data = {
... 'name': u'American Food',
... 'website': 'http://soundcloud.com/americanfood'
... })
>>> artist_type.validate(data)
Documentation¶
Installation¶
Tagged releases are available from PyPI:
$ pip install typerighter
Python Version¶
TypeRighter is strictly Python 3 and up.
Optional Packages¶
The optional packages are for running unittests or generation documentation and are explained in the Dev Environment doc.
Quickstart¶
Types¶
This is a simple Type.
>>> st = types.StringType(max_length=12)
A Type doesn’t store data, but it knows how to validate it.
>>> st.validate('short enough')
Errors¶
Exceptions are specific about errors
>>> st.validate('not short enough')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/jmsdnns/Projects/typerighter/typerighter/types/base.py", line 90, in validate
func(self, native)
File "/home/jmsdnns/Projects/typerighter/typerighter/types/domains.py", line 73, in validate_max_length
err_msg.format(value, instance.max_length)
typerighter.exceptions.ValidationException: Value length above max: short enoughhhh > 12
Records¶
A Record is a structure consisting of fields, or named type instances.
>>> from typerighter import types
>>> class Artist(types.Record):
... name = types.StringType(required=True)
... website = types.URLType()
...
>>> artist_type = Artist()
A Record is a Type, so it doesnt store data, but knows how to validate it.
>>> data = {
... 'name': u'American Food',
... 'website': 'http://soundcloud.com/americanfood'
... })
>>> artist_type.validate(data)
Views¶
A View is a mutable, configurable structure that stores Record data.
>>> artist_view = artist_type.make_view(artist)
>>> artist_view.website = 'https://soundcloud.com/americanfood/my-take-on-take-on-me'
It also knows how to validate data, but assumes it validates itself.
>>> artist_view.validate()
Type¶
-
class
typerighter.types.base.
Type
(*a, **kw)[source]¶ This class represents the top, and thus most ambiguous, point of the Typerighter hierarchy.
It’s purpose is to define the baseline expectations for every other Type in this library.
-
NATIVE
¶ alias of
builtins.object
-
is_coercible
(value)[source]¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)[source]¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)[source]¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)[source]¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
Primitives¶
-
class
typerighter.types.primitives.
BooleanType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.bool
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)[source]¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
-
class
typerighter.types.primitives.
FloatType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.float
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
-
class
typerighter.types.primitives.
IntegerType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.int
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
-
class
typerighter.types.primitives.
Number
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.object
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
-
class
typerighter.types.primitives.
Primitive
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.object
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
-
class
typerighter.types.primitives.
StringType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.str
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)[source]¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)[source]¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
Record¶
-
class
typerighter.types.records.
Record
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.dict
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_schematic
()¶ Returns a Type’s Schematic
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
Composite Types¶
-
class
typerighter.types.composites.
Container
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.object
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)[source]¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
-
class
typerighter.types.composites.
ListType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.list
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)[source]¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)[source]¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
-
class
typerighter.types.composites.
SumType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.object
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)[source]¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)[source]¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)[source]¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
Time Keeping¶
-
class
typerighter.types.timekeeping.
DateTimeType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
datetime.datetime
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_schematic
()¶ Returns a Type’s Schematic
-
-
class
typerighter.types.timekeeping.
TimeType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
datetime.time
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_schematic
()¶ Returns a Type’s Schematic
-
Network Addresses¶
-
class
typerighter.types.net.
EmailType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.str
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
-
class
typerighter.types.net.
IPAddressType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.str
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
-
class
typerighter.types.net.
IPv4Type
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.str
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
-
class
typerighter.types.net.
IPv6Type
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.str
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
-
class
typerighter.types.net.
MACAddressType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.str
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
-
class
typerighter.types.net.
URLType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.str
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
Spatial¶
-
class
typerighter.types.spatial.
GeoPointType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.object
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
Cryptography¶
-
class
typerighter.types.cryptography.
HashType
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.object
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
-
class
typerighter.types.cryptography.
MD5Type
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.object
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
-
class
typerighter.types.cryptography.
SHA1Type
(*a, **kw)[source]¶ -
NATIVE
¶ alias of
builtins.object
-
is_coercible
(value)¶ Checks a value for whether or not it can be converted to the correct type. Falls back to the stricter is_type_match if self.strict is True.
- Parameters
value (object) – The value to inspect
-
is_falsy
(value)¶ Checks a value and responds saying whether the Type considers it falsy.
- Parameters
value (object) – The value to inspect
- Returns
True or False
-
is_type_match
(value)¶ Checks if a value is an instance of this Type’s native type. :param object value: The value to inspect
-
to_native
(value)¶ Converts a value to the native form of this type
- Parameters
value (object) – The value to convert
-
to_primitive
(value)¶ Converts a value to the primitive form of this type
- Parameters
value (object) – The value to convert
-
to_schematic
()¶ Returns a Type’s Schematic
-
validate
(value)¶ This validation function is the primary function responsible for calling all associated validators and for managing any details related to aggregation of validation results.
- Parameters
value (object) – The value to convert
-
Views¶
-
class
typerighter.views.
Field
(name)[source]¶ A descriptor used to join a mutable View instance, that stores data, with the immutable Type instance, that only defines methods for operating on data.
Schematic¶
-
class
typerighter.schematics.
Schematic
(klass)[source]¶ A Schematic is a object that maintains a Type’s argspec. It exists as a class to provide a namespace for relevant values.
Dev Environment¶
First, install Typerighter from source and the packages we use for development.
$ git clone https://github.com/jmsdnns/typerighter
$ cd typerighter
$ pip install -e .[dev,docs]
Verify all tests are passing
$ pytest tests
=============================== test session starts ================================
...
================================ 69 passed in 0.16s ================================
Nice.
Testing¶
Typerighter uses pytest.
$ pytest tests
=============================== test session starts ================================
platform darwin -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: ...
plugins: cov-2.8.1
collected 69 items
tests/test_booleantypes.py .... [ 5%]
tests/test_cache.py . [ 7%]
tests/test_datetimetypes.py .. [ 10%]
tests/test_emailtype.py .. [ 13%]
tests/test_ipaddresstypes.py .... [ 18%]
tests/test_listtypes.py ...... [ 27%]
tests/test_macaddresses.py .. [ 30%]
tests/test_primitives.py ......... [ 43%]
tests/test_records.py ............ [ 60%]
tests/test_schematics.py . [ 62%]
tests/test_stringtypes.py ....... [ 72%]
tests/test_sumtypes.py .... [ 78%]
tests/test_types.py .......... [ 92%]
tests/test_urltypes.py .. [ 95%]
tests/test_views.py ... [100%]
================================ 69 passed in 0.16s ================================
Nice.
Architecture¶
TypeRighter is a toolkit for structuring, validating, and reshaping data.
Using the toolkit means using one or more of the following things:
Type
: a classification of some data which describes how to verify arbitrary data for coherence.Record
: a structure of data that has type instances, called _fields_, for attributes.Schematic
: the map of arguments used to instantiate either aType
or aRecord
.View
: a class that let’s you interact with aRecord
and some data as though it were an object instance.
Metaprogramming¶
The design of both Type
and Record
relies on metaprogramming to
collect information about the way you choose to use them.
Generally speaking, metaprogramming is a way for programs to treat code like data. We can read, generate, analyze, or transform code, or modify it while running.
More specifically, TypeRighter can inspect the attributes and functions on any type at the moment a user creates one. This allows it to:
make lists of all member variables
make a list of all functions that start with someprefix_
And with that metadata users can:
map out the steps for complex data validation
generate a SQL statement automatically
easily define datatype conversion pipelines
Attributes¶
All Type
and Record
definitions have values for:
_validate_functions
_schematic
Records use two extra fields:
_fields
_field_functions
Types¶
A type’s validate()
function will call each function listed in
_validate_functions
on its input.
The metaclass can be told about new validation functions by adding functions
with validate_
as a prefix, ie. validate_uppercase
.
class StrictStringType(StringType):
def validate_strict(self, value):
...
Records¶
Records introduce the concept of a field by associating a name with a type. It adds two fields of metadata to the class definition.
Let’s define a record with a string stored as field s
.
class Foo(Record):
s = StringType(required=True)
Fields defined like this are stored as _fields
.
It is also possible to use a function to generate field values.
class Foo(Record):
def field_s(self):
return 'an actual string'
Functions that behave like fields have a prefix field_
, similar to the
behavior for validation functions. This field is stored as
_field_functions
.