Skip to content

intutils API Reference

intutils is a basic Python integer manipulation library.

days_in_month(month_number, year_int=0)

Returns the number of days in the given month.

Parameters:

Name Type Description Default
month_number int

The month.

required
year_int

The year to check (for leap years).

0

Returns:

Type Description
int

The day count.

Source code in intutils/__init__.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def days_in_month(month_number: int, year_int=0) -> int:
    """
    Returns the number of days in the given month.

    Arguments:
        month_number: The month.
        year_int: The year to check (for leap years).

    Returns:
        The day count.
    """
    if month_number >= 13:
        raise ValueError("There isn't a month with a number bigger then 12!!")

    if is_even(month_number) and not month_number == 2:
        # even month, not Febrary
        return 30

    if is_odd(month_number):
        # odd month
        return 31

    if divisible_by_no_decimals(year_int, 4):
        # leap year
        return 29

    # typical year
    return 28

divisible_by_no_decimals(number, divisor)

Checks if the number can be divided by the divisor without the resulting number having decimals in it.

Parameters:

Name Type Description Default
number int

The base number.

required
divisor int

The number to divide the number by.

required

Returns:

Type Description
bool

If it is possible.

Source code in intutils/__init__.py
14
15
16
17
18
19
20
21
22
23
24
25
26
def divisible_by_no_decimals(number: int, divisor: int) -> bool:
    """
    Checks if the `number` can be divided by the `divisor` without the
    resulting number having decimals in it.

    Arguments:
        number: The base number.
        divisor: The number to divide the `number` by.

    Returns:
        If it is possible.
    """
    return (number % divisor) == 0

is_even(number)

Returns if the number is even.

Parameters:

Name Type Description Default
number int

The number to check.

required

Returns:

Type Description
bool

If the number is even.

Source code in intutils/__init__.py
29
30
31
32
33
34
35
36
37
38
39
def is_even(number: int) -> bool:
    """
    Returns if the `number` is even.

    Arguments:
        number: The number to check.

    Returns:
        If the number is even.
    """
    return divisible_by_no_decimals(number, 2)

is_int(testable)

Returns if the passed item is an integer.

Parameters:

Name Type Description Default
number

The item to check.

required

Returns:

Type Description
bool

If the object is an integer or a string that can be an integer.

Source code in intutils/__init__.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def is_int(testable: Any) -> bool:
    """
    Returns if the passed item is an integer.

    Arguments:
        number: The item to check.

    Returns:
        If the object is an integer or a string that can be an integer.
    """
    if type(testable) == int:
        return True
    elif type(testable) == str:
        try:
            int(testable)
            return True
        except ValueError:
            return False
    return False

is_odd(number)

Returns if the number is odd.

Parameters:

Name Type Description Default
number int

The number to check.

required

Returns:

Type Description
bool

If the number is odd.

Source code in intutils/__init__.py
42
43
44
45
46
47
48
49
50
51
52
def is_odd(number: int) -> bool:
    """
    Returns if the `number` is odd.

    Arguments:
        number: The number to check.

    Returns:
        If the number is odd.
    """
    return not is_even(number)

sort_greatest_to_least(mylist)

Sorts the passed list of integers from greatest to least.

Parameters:

Name Type Description Default
mylist List[int]

The list to sort.

required

Returns:

Type Description
List[int]

The sorted list.

Source code in intutils/__init__.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def sort_greatest_to_least(mylist: List[int]) -> List[int]:
    """
    Sorts the passed list of integers from greatest to least.

    Arguments:
        mylist: The list to sort.

    Returns:
        The sorted list.
    """
    return list(
        reversed(
            _Sorting().quick_sort(mylist, 0, len(mylist) - 1)
        )
    )

Last update: