Skip to main content

A python library for variable type checker/validator/converter at run time.

Project description

typepy

https://badge.fury.io/py/typepy.svg https://img.shields.io/pypi/pyversions/typepy.svg https://img.shields.io/travis/thombashi/typepy/master.svg?label=Linux https://img.shields.io/appveyor/ci/thombashi/typepy/master.svg?label=Windows https://coveralls.io/repos/github/thombashi/typepy/badge.svg?branch=master https://img.shields.io/github/stars/thombashi/typepy.svg?style=social&label=Star

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

Bool

datetime

DateTime

dict

Dictionary

inf

Infinity

int

Integer

float

RealNumber

NaN

Nan

None

None

str (not null)

String

str (null)

NullString

Installation

pip install typepy

Dependencies

Python 2.7+ or 3.3+

Test dependencies

Documentation

http://typepy.rtfd.io/

Project details


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)

Uploaded Source

Built Distribution

typepy-0.0.2-py2.py3-none-any.whl (26.4 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page