-
Notifications
You must be signed in to change notification settings - Fork 7
take into account non zero-padded datetime #326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| Parameters | ||
| ---------- | ||
| datetime : tuple[str] | ||
| A tuple of datetime component strings (e.g., ('2024', '1', '15')). |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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} |
There was a problem hiding this comment.
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
| with expected as e: | ||
| assert normalize_datetime(datetime, template) == e |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.

Modified slightly timestamp_utils script to accept non zero-padded datetime