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 |
|
float/decimal.Decimal (not infinity/NaN) |
|
float/decimal.Decimal (infinity) |
|
float/decimal.Decimal (NaN) |
|
int |
|
list |
|
None |
|
str (not null) |
|
str (null) |
|
str (IP address) |
Usage
Type Check Method
- Examples:
>>> from typepy import Integer >>> Integer(1).is_type() True >>> Integer(1.1).is_type() False
Type Validation Method
- Examples:
>>> from typepy import Integer >>> Integer(1).validate() >>> try: ... Integer(1.1).validate() ... except TypeError as e: ... # validate() raised TypeError when the value unmatched the type class ... print(e) ... invalid value type: expected=INTEGER, actual=<type 'float'>
Type Conversion Methods
convert method
- Examples:
>>> from typepy import Integer, TypeConversionError >>> Integer("1").convert() 1 >>> try: ... Integer(1.1).convert() ... except TypeConversionError as e: ... # convert() raised TypeConversionError when conversion failed ... print(e) ... failed to convert from float to INTEGER
try_convert method
- Examples:
>>> from typepy 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 import Integer, TypeConversionError >>> Integer("1").force_convert() # force_convert() forcibly convert the value 1 >>> Integer(1.1).force_convert() 1 >>> try: ... Integer("abc").force_convert() ... except TypeConversionError as e: ... # force_convert() raised TypeConversionError when the value not convertible ... print(e) ... failed to force_convert to int: type=<class 'str'>
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
File details
Details for the file typepy-0.2.0.tar.gz
.
File metadata
- Download URL: typepy-0.2.0.tar.gz
- Upload date:
- Size: 20.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 483d89e66b36f1d3350a2951beed385fcc3262109dd5a4b3f9e66cd23b010ddd |
|
MD5 | b685ebbbf420fdbf1022028facd79ddc |
|
BLAKE2b-256 | dea5ca2eeaa8c9fd07f3f6150f1f0c1d63067c00e73a7cd32cdbe8951a346f8b |
File details
Details for the file typepy-0.2.0-py2.py3-none-any.whl
.
File metadata
- Download URL: typepy-0.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 29.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dda6f5071bef35cbe7fd47a845c946c002a35df94df18a1ea7f89685ac320525 |
|
MD5 | 9b2f5d9160a4b2ad483beb1e48674916 |
|
BLAKE2b-256 | 9523afc548bd45ea67152122ef5c4cc3212011e9bd87d46d7a83b585595c9bd3 |