Skip to content

Conversation

@mathieudpnt
Copy link
Contributor

Modified slightly timestamp_utils script to accept non zero-padded datetime

@mathieudpnt mathieudpnt requested a review from Gautzilla January 22, 2026 11:42
Parameters
----------
datetime : tuple[str]
A tuple of datetime component strings (e.g., ('2024', '1', '15')).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just add codeblocks to the values in the docstring to improve the readability of the doc:

    """Convert a datetime and its template with non-zero padded parts.

    Parameters
    ----------
    datetime : tuple[str]
        A tuple of datetime component strings (e.g., ``('2024', '1', '15')``).
    template : str
        A datetime template string with format specifiers (e.g., ``'%Y_%-m_%d'``).
        Format specifiers starting with ``'%-'`` indicate non-zero-padded values
        that will be converted to zero-padded format.

    Returns
    -------
    tuple[str, str]
        A tuple containing:
        - A normalized template string with all format specifiers zero-padded
          (e.g., ``'%Y_%m_%d'``)
        - A normalized datetime string with all values zero-padded
          (e.g., ``'2024_01_15'``)
"""

>>> normalize_datetime(('2024', '3', '5'), '%Y_%-m_%-d')
('%Y_%m_%d', '2024_03_05')
"""
datetime_parts = list(datetime)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this cast really necessary? Can't you stick with the datetime tuple?

"""
strftime_identifiers = [key.lstrip("%") for key in _REGEX_BUILDER]
strftime_identifier_lengths = {len(id) for id in strftime_identifiers}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id isn't a recommended var name as it shadows a python buildin, you might go for something like strftime_id

Comment on lines +838 to +839
with expected as e:
assert normalize_datetime(datetime, template) == e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's not the correct syntax for pytest raises: normalize_datetime(datetime, template) is expected to raise, so the normalize_datetime(datetime, template) == e will not be tested.
Use:

    with expected:
        normalize_datetime(datetime, template)

pytest.param(
("5", "3", "1998"),
"%m_%-m_%Y",
pytest.raises(ValueError),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set the match parameter of pytest.raises to specify the expected error message

),
],
)
def test_normalize_datetime_errors(datetime, template, expected):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please annotate return and parameters types

Please GifGIF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants