-
Notifications
You must be signed in to change notification settings - Fork 17
AI Rewrite: ESM Calendar #1300
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: release
Are you sure you want to change the base?
AI Rewrite: ESM Calendar #1300
Conversation
b515a67 to
3d2ae71
Compare
|
This is as much a proof of concept as anything else, but I put First the plus points:
Minus points:
I would like to try similar AI refactors of some of the other modules, if we find this to be useful. |
|
It's almost there, something is still wrong with the delta dates. I need to restore the old behaviour and write some target unit tests to see how it should look. |
|
OK, the old behaviour doesn't seem to make sense to me at all: >>> d1 = Date("2024-02-16T12:30:00")
>>> d2 = Date("2024-02-16T11:30:00")
>>> d1 - d2
[0, 0, 0, 0, 0, 0]
>>> d1
Date(2024-02-16T12:30:00)
>>> d1.hour
12
>>> d1.year
2024
>>> d1.time_between(d2)
0
>>> d1.time_between(d2, "days")
0
>>> d1 = Date("2001-01-01T00:00:00")
>>> d1
Date(2001-01-01T00:00:00)
>>> d2 = Date("2000-01-01T00:00:00")
>>> d1 - d2
[1, 12, 366, 8784, 527040, 31622400]
>>> d1.time_between(d2)
31622400Subtraction gives back 1 year in all possible combinations? How is that sensible? |
|
Even worse: >>> d1 = Date("2024-02-16T12:30:00")
>>> d2 = Date("2024-02-16T11:30:00")
>>> d1.hour
12
>>> d2.hour
11
>>> d1 - d2
[0, 0, 0, 0, 0, 0]So dates that are obviously 1 hour apart are calculated to be 0 apart. |
|
In the new implementation, this makes much more sense: ❯ python
Python 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:41:52) [Clang 15.0.7 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from esm_calendar import Date
>>> d1 = Date("2024-02-16T12:30:00")
>>> d2 = Date("2024-02-16T11:30:00")
>>> d1 - d2
[0, 0, 0, 1, 0, 0]
>>> d1.time_between(d2)
3600
>>> d1.time_between(d2, unit="hours")
1
>>> d1.time_between(d2, unit="days")
0
>>> d1.time_between(d2, unit="years")
0
>>> d1.time_between(d2, unit="seconds")
3600
>>> d1 = Date("2001-01-01T00:00:00")
>>> d2 = Date("2000-01-01T00:00:00")
>>> d1 - d2
[1, 0, 0, 0, 0, 0] |
|
This seems to be by design: esm_tools/src/esm_calendar/esm_calendar.py Lines 613 to 660 in 6552cee
Since that is the case, maybe the refactored version also should take this into account, however, I think it would make more sense to name this something more intuitive, or provide a specific DeltaESMDate, similar to how Python's built in |
|
Differences in esm-tests are now only due to a different repr: |
Using the Artificial Intelligence to rewrite some of our more horrible insides. Part 1: The Calendar.
This pull request includes several changes aimed at improving the codebase and adding new functionality. The most important changes involve updating dependencies, refactoring code for better readability, and adding new tests.
Rewrite:
Dependency updates:
setup.py: Added thedeprecatedlibrary to the dependencies.Code refactoring:
src/esm_runscripts/batch_system.py: Moved the import ofloguruto the top of the file for consistency.src/esm_runscripts/oasis.py: Reordered imports to maintain consistency and readability.src/esm_runscripts/prepare.py: Moved the import ofloguruto the top of the file for consistency.src/esm_runscripts/prev_run.py: Moved the import ofloguruto the top of the file for consistency.Functionality improvements:
src/esm_runscripts/batch_system.py,src/esm_runscripts/oasis.py,src/esm_runscripts/prepare.py,src/esm_runscripts/prev_run.py,src/esm_runscripts/resubmit.py,src/esm_runscripts/yac.py: Updated theformatmethod calls to useprint_hours,print_minutes, andprint_secondsinstead ofgivenph,givenpm, andgivenpsfor better clarity. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]New tests:
tests/test_esm_calendar/test_esm_calendar.py: Added comprehensive tests for theesm_calendarmodule, including tests forfind_remaining_minutes,is_leap_year,days_in_year,days_in_month, andDateclass methods.