aboutsummaryrefslogtreecommitdiffstats
path: root/utils/misc.py
blob: f7743d17bb8b7760d6edd565f7571405fdf457e1 (plain)
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
# author : S. Mandalia
#          shivesh.mandalia@outlook.com
#
# date   : March 19, 2020

"""
Miscellaneous utility methods.
"""

from typing import Any, Union


__all__ = ['Number', 'is_num', 'is_pos']


_T = (int, float)
Number = Union[int, float]


def is_num(val: Any) -> bool:
    """
    Check if the input value is a finite number.

    Parameters
    ----------
    val : object
        Value to check.

    Returns
    -------
    bool

    Examples
    --------
    >>> from utils.misc import is_num
    >>> print(is_num(10))
    True
    >>> print(is_num(None))
    False

    """
    if not isinstance(val, _T):
        return False
    return True


def is_pos(val: Any) -> bool:
    """
    Check if the input value is a finite positive number.

    Parameters
    ----------
    val : object
        Value to check.

    Returns
    -------
    bool

    Examples
    --------
    >>> from utils.misc import is_pos
    >>> print(is_pos(10))
    True
    >>> print(is_pos(-10))
    False

    """
    if not is_num(val):
        return False
    if not val >= 0:
        return False
    return True