Skip to main content

typepy is a Python library for variable type checker/validator/converter at a run time.

Project description

Summary

typepy is a Python library for variable type checker/validator/converter at a run time.

PyPI package version Supported Python versions Supported Python implementations Linux/macOS CI status Windows CI status Test coverage GitHub stars

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

Install from PyPI

pip install typepy

Install additional dependency packages with the following command if using typepy.DateTime class

pip install typepy[datetime]

Install from PPA (for Ubuntu)

sudo add-apt-repository ppa:thombashi/ppa
sudo apt update
sudo apt install python3-typepy

Dependencies

Python 3.5+

Optioal dependencies

These packages can be installed via pip install typepy[datetime]:

Test dependencies

Documentation

https://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-1.0.0.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

typepy-1.0.0-py3-none-any.whl (30.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typepy-1.0.0.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.44.0 CPython/3.8.2

File hashes

Hashes for typepy-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e422165afc5632ef8006ae6b35a3984a132f60287809752099f8d0a16d3561dd
MD5 9ce1f793579533d91e58a1321613ea39
BLAKE2b-256 4562612eb628aee3c7159bf905e4598d9c5e2152b0cb02be62d9a66b6a0715b8

See more details on using hashes here.

File details

Details for the file typepy-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: typepy-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 30.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.44.0 CPython/3.8.2

File hashes

Hashes for typepy-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4d10fb9f88524bcf1292e9a1256c8f88345390f8c06d47a18b3b669d7017b90f
MD5 12d1aece7e81aeffd032be560d8b7b1e
BLAKE2b-256 47af7b68641a0e93fc0e69ba7a88e98ede5f9a1b24e4f07ca31e9c4088a1a4c7

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