A Python library for variable type checker/validator/converter at a run time.
Project description
typepy
Summary
A Python library for variable type checker/validator/converter at a run time.
Features
checking a value type
validate a value for a type
convert a value from a type to the other type
The correspondence between Python types and typepy classes are as follows:
Python Type |
typepy: Type Class |
---|---|
bool |
|
datetime |
|
dict |
|
inf |
|
int |
|
list |
|
float |
|
NaN |
|
None |
|
str (not null) |
|
str (null) |
|
str (IP address) |
Usage
Type Check Method
- Examples:
>>> from typepy.type import Integer >>> Integer(1).is_type() True >>> Integer(1.1).is_type() False
Type Validation Method
- Examples:
>>> from typepy.type import Integer >>> Integer(1).validate() >>> try: ... Integer(1.1).validate() ... except TypeError as e: ... print(e) ... invalid value type: expected=INTEGER, actual=<type 'float'>
Type Conversion Methods
convert method
- Examples:
>>> from typepy.type import Integer >>> from typepy import TypeConversionError >>> Integer("1").convert() 1 >>> try: ... Integer(1.1).convert() ... except TypeConversionError as e: ... print(e) # convert() raised TypeConversionError when conversion failed ... failed to convert from float to INTEGER
try_convert method
- Examples:
>>> from typepy.type import Integer >>> Integer("1").try_convert() 1 >>> print(Integer(1.1).try_convert()) # try_convert() returned None when conversion failed None
force_convert
- Examples:
>>> from typepy.type import Integer >>> Integer("1").force_convert() # force_convert() forcibly convert the value 1 >>> Integer(1.1).force_convert() 1
For more information
Type check/validate/convert results differed according to strict_level value which can pass to typepy classes constructors as an argument. More information can be found in the API reference.
Installation
pip install typepy
Dependencies
Python 2.7+ or 3.4+
Test dependencies
Documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for typepy-0.0.25-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce178b3dc5b1d53e0bfab6a8dd8226e58f311b8ddbdefd60741c443a5643674a |
|
MD5 | 124fad66914590a593df44772cdf1c7f |
|
BLAKE2b-256 | 704401bbe807dcf4a085c8f39ae665e4f5e1d4554fae82d48d29958bfee3dd0a |