Repository navigation
DAT-1243 Retail Calendar port to Python - #1
Conversation
DAT-993 Add partitioning to ACG metrics table
Please fill out the below fields:
@alican s notes:
DAT-1243 Port retail-calendar to retail-calendar-python
Completely port retail-calendar to retail-calendar-python. |
|
Thanks for this, it is very thorough. I ran the port against the JS (4.1.2, UTC) on all 351 weekly/daily calendar configs we have in prod (146 tenants, including the customLeapYear ones), 2015-2035. 384,639 weeks compared, 0 differences. One change I'd like before toolio-ml depends on it:
Smaller things:
Answers to the caveats:
|
|
Thanks @cihancimen, running it against all 351 prod configs is exactly the check I hoped for. 384,639 weeks with 0 differences is reassuring. Done in 7ba5462:
Later:
Follow-ups from your caveat answers:
|
Summary
https://linear.app/toolio/issue/DAT-1243/port-retail-calendar-to-retail-calendar-python
A Python port of the
retail-calendarnpm package (v4.1.2), so toolio-ml can do retail-calendar date conversions in-process instead of calling retail-calendar-api over HTTP.calendarconfigurationtable and retail-calendar-api, so tenant configs work as they are (RetailCalendarOptions.from_dict(row)reads DB rows and API payloads directly).What's included
enums.pyWeekGrouping,LastDayOfWeek,LastMonthOfYear,WeekCalculationasIntEnums with the JS values, plus.label/from_label()for DB and API strings ("4-4-5","Saturday","lastDayNearestEndOfMonth")options.pyRetailCalendarOptions(frozen, hashable, validated),from_dict/to_dict,NRF_CALENDAR_OPTIONSstrategies.pycalendar.pyRetailCalendar(weeks, months, days built on first access) and the cachedget_retail_calendar()lookup.pyweek_of_gregorian_date, plusdate_to_segment/segment_to_date/leap_years, which mirror the retail-calendar-api endpointsscripts/golden/Tooling: uv, ruff, mypy
--strict, pytest with 100% line and branch coverage, Hypothesis, pre-commit and GitHub Actions. The README has a JS-to-Python name table and a note telling contributors and agents to keep both repos in sync.Parity with the JS package
tests/test_golden.pycompares against reference data fromretail-calendar@4.1.2running in UTC:weekOfGregorianDateEverything matches. All JS test cases are ported as well.
CI regenerates the reference data from npm with
scripts/golden/check.shand fails if it differs from what's committed. The check compares decompressed contents, because gzip headers record the OS that wrote them, so the.gzbytes differ between macOS and Linux even when the data is identical.Intentional differences from the JS package
datetime.date, with inclusive ends. The JS returnsDateobjects that end at 23:59:59.999.Noneinstead of-1for its month and quarter fields.addLeapWeekToMonthoutside 0–11, missing custom leap year options and unsupported years (outside 2–9997) raiseValueErrorstraight away. The JS fails later or silently gives wrong results.Caveats to discuss with Cihan // @cihancimen
The JS output depends on the host timezone. I ran 21,420 calendars (5 strategies × 7 week-end days × 12 months × 1990–2040) in other timezones. Compared with UTC, 9 differ in New York, 38 in Sydney and 54 in Istanbul. Example:
LastDayNearestEOM, weeks ending Sunday, year ending October, 2012 has 53 weeks in UTC but 52 in New York. The cause is local-timeDatemath floored to whole days across DST changes.WEBSITE_TIME_ZONEoverride)?LastDayBeforeEomExceptLeapYearonly works for years ending in December. It takesendOfYear(...)(Dec 31) as next year's end instead of the same month a year later. For any other month it behaves exactly likeLastDayBeforeEOM; I verified this for January and August year-ends. The port copies the bug for parity. Fix it in both, or document it as December-only?PenultimateDayOfWeekNearestEOMcheckslastDayOfIsoWeek === 0, which never happens with ISO days 1–7; it should be=== 1(Monday). The results are still correct, so this is only cleanup. The Python version uses the correct check and matches the JS everywhere.CustomLeapYearignoreslastDayOfWeek. IfleapYearEndDateisn't on that weekday, weeks silently end on a different day. Should both packages reject such configs? We'd first need to check the existing tenant configs.The JS package accepts invalid input silently:
addLeapWeekToMonthabove 11 drops the 53rd week without an error;lastMonthOfYearabove 11 rolls over into the next year;undefined.Python raises instead. Should the JS validate the same way?
JS memoization hands out shared, mutable calendar objects, and its cache key depends on the order of option keys.
retail-calendar-api: in
calendar-service.js, unknown labels are meant to fall back to defaults, butWeekCalculation,WeekGrouping,LastDayOfWeekandLastMonthOfYeararen't imported there. Those requests throw aReferenceErrorinstead.A JS test fixture has malformed dates. About 100 dates in
__tests__/data/last_day_nearest_eom_445_arbitrary_leap_year_weeks.tscontain stray spaces ("2024-01 -08","2024 -08-04"). The JS test parser tolerates them; they're fixed in this repo's copy.The JS repo's LICENSE names "Fatih Kadir Akın", who never committed to it. It looks copied from another project. This repo's LICENSE says Toolio.
Keeping the repos in sync: who releases the JS package and bumps the pin in
scripts/golden/package.jsonwhen calendar behavior changes? The README spells out the steps.Out of scope / follow-ups
TemporalDateUtilityand add pandas helpers there, so the per-row loop becomes a vectorized lookup (about 0.5 s for 1M rows in a prototype). The pandas helpers stay out of this repo to keep it dependency-free.retail-calendar @ git+ssh://…@v1.0.0). Azure Artifacts and a Databricks wheel are the alternatives.Testing
To regenerate the reference data:
cd scripts/golden && npm ci && npm run generate. To only check it:./check.sh.🤖 Generated with Claude Code
https://claude.ai/code/session_01GaY7VR1MJXPdTtk1WjfoK2