File: usage.txt

package info (click to toggle)
python-typepy 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 624 kB
  • sloc: python: 2,886; makefile: 78; sh: 7
file content (83 lines) | stat: -rw-r--r-- 2,406 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Type Check Method
----------------------
:Examples:
    .. code-block:: pycon

        >>> from typepy import Integer
        >>> Integer(1).is_type()
        True
        >>> Integer(1.1).is_type()
        False


Type Validation Method
--------------------------------------------
:Examples:
    .. code-block:: pycon

        >>> 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:
    .. code-block:: pycon

        >>> 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:
    .. code-block:: pycon

        >>> 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:
    .. code-block:: pycon

        >>> 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 was 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 class constructors as an argument.
More information can be found in the
`API reference <https://typepy.rtfd.io/en/latest/pages/reference/index.html>`__.