A python library for variable type checker/validator/converter at run time.
Project description
typepy
Summary
A python library for variable type checker/validator/converter at run time.
Usage
Type Check Method
>>> from typepy.type import Integer
>>> Integer(1).is_type()
True
>>> Integer(1.1).is_type()
False
Type Validation Method
>>> 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
>>> 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 method will raise TypeConversionError when conversion failed
...
failed to convert from float to INTEGER
try_convert
>>> from typepy.type import Integer
>>> Integer("1").try_convert()
1
>>> print(Integer(1.1).try_convert()) # try_convert method will return None when conversion failed
None
force_convert
>>> from typepy.type import Integer
>>> Integer("1").force_convert() # force_convert will forcibly convert the value
1
>>> Integer(1.1).force_convert()
1
For more information
Type check/validate/convert results will be changed according to strict_level value which can be passed to constructors as an argument. More information can be found in the API reference.
Features
The association between Python types and typepy classes is as follows:
Python Type |
typepy Class |
---|---|
bool |
|
datetime |
|
dict |
|
inf |
|
int |
|
float |
|
NaN |
|
None |
|
str (not null) |
|
str (null) |
Installation
pip install typepy
Dependencies
Python 2.7+ or 3.3+
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
typepy-0.0.2.tar.gz
(15.7 kB
view hashes)
Built Distribution
typepy-0.0.2-py2.py3-none-any.whl
(26.4 kB
view hashes)
Close
Hashes for typepy-0.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe909c47912007db8868592c131ad2a2504cc28a27d6bf4f88d0b77be18cecc1 |
|
MD5 | ddeb46a44909607ec8d9ad0df34d9af7 |
|
BLAKE2b-256 | 6173b81b19df8785f2cf1d89d922022af41aaa77e3e44c79e50f6da61de0773d |