Coder Social home page Coder Social logo

kkhadse31 / typepy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thombashi/typepy

0.0 1.0 0.0 280 KB

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

Home Page: https://typepy.rtfd.io/

License: MIT License

Shell 0.30% Makefile 1.01% Python 98.68%

typepy's Introduction

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

PyPI package version Supported Python versions Linux/macOS CI status Windows CI status Test coverage GitHub stars
  • 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
Examples:
>>> from typepy import Integer
>>> Integer(1).is_type()
True
>>> Integer(1.1).is_type()
False
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'>

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'>

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.

pip install typepy

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

pip install typepy[datetime]
sudo add-apt-repository ppa:thombashi/ppa
sudo apt update
sudo apt install python3-typepy

Python 2.7+ or 3.5+

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

https://typepy.rtfd.io/

typepy's People

Contributors

thombashi avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.