Skip to main content

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.

https://badge.fury.io/py/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

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:

Supported Types

Python Type

typepy: Type Class

bool

Bool

datetime

DateTime

dict

Dictionary

float/decimal.Decimal (not infinity/NaN)

RealNumber

float/decimal.Decimal (infinity)

Infinity

float/decimal.Decimal (NaN)

Nan

int

Integer

list

List

None

None

str (not null)

String

str (null)

NullString

str (IP address)

IpAddress

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

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.1.0.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

typepy-0.1.0-py2.py3-none-any.whl (28.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file typepy-0.1.0.tar.gz.

File metadata

  • Download URL: typepy-0.1.0.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for typepy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 948e8e4a94f10d74959508c2448cb3f13f9cf339959b16ad8640a8154ed12a51
MD5 1a18f92e98d8aff51c23ff2748f5af79
BLAKE2b-256 7fe5e24f212b57d5f67225f4bc6e9d335a2f2a9e3981c54dca2ff2cc6a3b99b7

See more details on using hashes here.

File details

Details for the file typepy-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for typepy-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 486abece46f96670d1399c93be7e77e80c82eaa0220e6bd25c394588833bd81e
MD5 c3bcb9b5334bde457508caa390f10c99
BLAKE2b-256 155102b89f18594c8dbb71ac04c8e167eea5d06cd0537114cc6152b63e40d79c

See more details on using hashes here.

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