Skip to content

Reference

dsg_lib.common_functions.calendar_functions

This module provides two main functions to convert between month numbers and their corresponding names.

Functions:

Name Description
get_month

int) -> str: Converts an integer month number to its corresponding month name.

Args: month (int): An integer between 1 and 12 representing the month number.

Returns: str: The full name of the month corresponding to the input month number. If the input is not within the range of 1-12, returns "Invalid month number". If the input is not an integer, returns "Invalid input, integer is required".

get_month_number

str) -> int: Converts a month name to its corresponding month number.

Args: month_name (str): A string containing the full name of a month.

Returns: int: The month number corresponding to the input month name. If the input is not a valid month name, returns -1. If the input is not a string, returns "Invalid input, string is required".

Example:

from dsg_lib.common_functions.calendar_functions import get_month,

get_month_number print(get_month(1))

# Outputs: 'January'

print(get_month_number('January'))

# Outputs: 1

This module is part of the dsg_lib package and is used for handling and converting between month numbers and names.

Author: Mike Ryan Date: 2024/05/16 License: MIT

get_month(month)

Converts an integer month number to its corresponding month name.

Parameters:

Name Type Description Default
month int

An integer or integer-like float between 1 and 12

required

Returns:

Name Type Description
str str

The full name of the month corresponding to the input month number. If the input is not within the range of 1-12, returns "Invalid month number". If the input is not an integer or integer-like float, returns "Invalid input, integer is required".

Source code in dsg_lib/common_functions/calendar_functions.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def get_month(month: int) -> str:
    """
    Converts an integer month number to its corresponding month name.

    Args:
        month (int): An integer or integer-like float between 1 and 12
        representing the month number.

    Returns:
        str: The full name of the month corresponding to the input month number.
             If the input is not within the range of 1-12, returns "Invalid
             month number". If the input is not an integer or integer-like
             float, returns "Invalid input, integer is required".
    """
    # Convert integer-like floats to integers
    if isinstance(month, float) and month.is_integer():
        month = int(month)

    # Check if the input month is an integer
    if not isinstance(month, int):
        logger.error("Invalid input: %s, integer is required", month)
        return "Invalid input, integer is required"

    # Check if the input month is within the range of 1-12
    if 1 <= month <= 12:
        logger.info("Returning month name for month number: %s", month)
        return MONTHS[month - 1]
    else:
        logger.error(
            "Invalid input: %s, month number should be between 1 and 12", month
        )
        return "Invalid month number"

get_month_number(month_name)

Converts a month name to its corresponding month number.

Parameters:

Name Type Description Default
month_name str

A string containing the full name of a month.

required

Returns:

Name Type Description
int int

The month number corresponding to the input month name. If the input is not a valid month name or not a string, returns -1.

Source code in dsg_lib/common_functions/calendar_functions.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def get_month_number(month_name: str) -> int:
    """
    Converts a month name to its corresponding month number.

    Args:
        month_name (str): A string containing the full name of a month.

    Returns:
        int: The month number corresponding to the input month name.
             If the input is not a valid month name or not a string, returns -1.
    """

    # Check if the input month name is a string
    if not isinstance(month_name, str):
        logger.error("Invalid input, string is required")
        return -1

    # Convert the input string to title case and remove leading/trailing spaces
    month_name = month_name.strip().title()

    # Check if the input month name is a valid key in the lookup
    if month_name in MONTH_NUMBERS:
        return MONTH_NUMBERS[month_name]
    else:
        logger.error("Invalid month name: %s", month_name)
        return -1