AI Skill Report Card
Working with Calendar Module
Working with Python's calendar Module
Quick Start14 / 15
Pythonimport calendar # Print a text calendar for a month print(calendar.month(2024, 3)) # Print a full year calendar print(calendar.calendar(2024)) # Check leap year calendar.isleap(2024) # True # Get weekday of a specific date (0=Monday) calendar.weekday(2024, 3, 15) # Friday -> 4 # Get (first_weekday, number_of_days) for a month calendar.monthrange(2024, 2) # (3, 29) -> Feb 2024 starts Thu, has 29 days
Recommendation▾
Add an example showing a bad/common mistake output (e.g., forgetting to filter padding zeros) to contrast good vs bad outcomes explicitly
Workflow13 / 15
Progress:
- Step 1: Decide output format needed (text, HTML, or raw data structures)
- Step 2: Choose the right class/function (
Calendar,TextCalendar,HTMLCalendar, or module-level convenience functions) - Step 3: Set locale/first-weekday if non-default behavior required
- Step 4: Generate data (iterators for weeks/months/years, or formatted strings)
- Step 5: Post-process output (e.g., strip empty days marked
0, style HTML)
Choosing the right tool
- Quick print/debug →
calendar.month(year, month),calendar.calendar(year) - Iterate raw day numbers (0 = day outside month) →
Calendar.itermonthdays() - Iterate
(day, weekday)tuples →Calendar.itermonthdays2() - Iterate actual
datetime.dateobjects →Calendar.itermonthdates()(note: can spill into adjacent years at Dec/Jan boundaries) - Group days into weeks →
Calendar.monthdayscalendar()/monthdatescalendar() - HTML output →
HTMLCalendarclass,formatmonth()/formatyear() - Locale-specific names →
LocaleTextCalendar/LocaleHTMLCalendar
Setting the first day of the week
Pythonc = calendar.Calendar(firstweekday=6) # Sunday-first calendar.setfirstweekday(calendar.SUNDAY) # affects module-level functions globally
Default is Monday (0). Constants: MONDAY=0 ... SUNDAY=6.
Recommendation▾
Include a brief example of LocaleTextCalendar usage since it's mentioned but never demonstrated
Examples17 / 20
Example 1: Get weeks of a month as date objects
Input:
Pythonimport calendar cal = calendar.Calendar() list(cal.monthdatescalendar(2024, 2))
Output:
Python[[datetime.date(2024, 1, 29), ..., datetime.date(2024, 2, 4)], [datetime.date(2024, 2, 5), ..., datetime.date(2024, 2, 11)], ... [datetime.date(2024, 2, 26), ..., datetime.date(2024, 3, 3)]]
Each inner list has exactly 7 dates; padding days come from adjacent months.
Example 2: Generate an HTML calendar for a month
Input:
Pythonimport calendar hc = calendar.HTMLCalendar(firstweekday=calendar.SUNDAY) html = hc.formatmonth(2024, 12)
Output: A string of <table class="month">...</table> HTML with <td class="mon">, <td class="noday"> for padding cells, etc. — ready to embed in a page or style with CSS classes (month, year, noday, mon, tue, ... , sun).
Example 3: Count weekday occurrences in a month
Input:
Pythoncalendar.monthrange(2024, 2)
Output:
Python(3, 29) # Feb 2024 starts on a Thursday (index 3), has 29 days
Use this to compute how many Mondays/Fridays/etc. occur in a month without iterating.
Recommendation▾
Consider trimming the Best Practices and Common Pitfalls sections slightly since there's some overlap/redundancy between them
Best Practices
- Prefer
Calendariterator methods over string parsing ofcalendar.month()/calendar.calendar()output when you need structured data — the text output is for display only. - Use
itermonthdates()cautiously: it can raiseOverflowErrorneardatetime.MAXYEAR/MINYEARboundaries since it includes adjacent-month dates. Useitermonthdays()(plain ints,0= padding) if you need to avoid date-range edge cases. - Use
calendar.isleap(year)andcalendar.leapdays(y1, y2)rather than reimplementing leap-year math. - For locale-aware month/day names, use
LocaleTextCalendar/LocaleHTMLCalendarwith a(locale, encoding)tuple, or the module-levelcalendar.month_name/calendar.day_namearrays (locale-independent, English by default). - Remember
month_nameandday_nameare 1-indexed for months (month_name[0]is'') butday_nameis 0-indexed starting Monday. - For CLI-style output,
python -m calendar 2024works directly from the command line — useful for quick checks without writing a script.
Common Pitfalls
- Assuming week lists always belong to the queried month —
monthdatescalendar/monthdayscalendarpad with adjacent-month days (represented as0in the days variant); filter these out if you only want the target month. - Confusing
weekday()(needs date) withfirstweekday()(calendar setting) —calendar.weekday(y, m, d)returns which day of week a specific date falls on;Calendar.getfirstweekday()returns the configured start-of-week for layout purposes. - Forgetting
setfirstweekday()is global/stateful for module-level functions — prefer instantiating your ownCalendar/TextCalendar/HTMLCalendarobject withfirstweekdayset explicitly to avoid side effects across code that shares the module. - Mixing up month indices —
calendarmonths are 1–12 (no month 0), unlike some other date libraries. - Using
calendar.calendar()/month()for programmatic parsing — these produce human-formatted strings with locale-dependent spacing; use the iterator/data methods instead.