Skip to content
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

gh-103636: add enums for days and months in calendar module #103642

Merged
merged 7 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions Lib/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import sys
import datetime
from enum import IntEnum, global_enum
import locale as _locale
from itertools import repeat

Expand All @@ -16,6 +17,9 @@
"timegm", "month_name", "month_abbr", "day_name", "day_abbr",
"Calendar", "TextCalendar", "HTMLCalendar", "LocaleTextCalendar",
"LocaleHTMLCalendar", "weekheader",
"Weekday", "Month", "JANUARY", "FEBRUARY", "MARCH",
"APRIL", "MAY", "JUNE", "JULY",
"AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMEMBER", "DECEMBER",
"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY",
"SATURDAY", "SUNDAY"]

Expand All @@ -37,6 +41,35 @@ def __str__(self):
return "bad weekday number %r; must be 0 (Monday) to 6 (Sunday)" % self.weekday


# Constants for months referenced later
@global_enum
class Month(IntEnum):
JANUARY = 1
FEBRUARY = 2
MARCH = 3
APRIL = 4
MAY = 5
JUNE = 6
JULY = 7
AUGUST = 8
SEPTEMBER = 9
OCTOBER = 10
NOVEMEMBER = 11
Agent-Hellboy marked this conversation as resolved.
Show resolved Hide resolved
DECEMBER = 12


# Constants for weekdays
Agent-Hellboy marked this conversation as resolved.
Show resolved Hide resolved
@global_enum
class Weekday(IntEnum):
Agent-Hellboy marked this conversation as resolved.
Show resolved Hide resolved
MONDAY = 0
TUESDAY = 1
WEDNESDAY = 2
THURSDAY = 3
FRIDAY = 4
SATURDAY = 5
SUNDAY = 6


# Constants for months referenced later
January = 1
February = 2
Expand Down Expand Up @@ -95,9 +128,6 @@ def __len__(self):
month_name = _localized_month('%B')
month_abbr = _localized_month('%b')

# Constants for weekdays
(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)


def isleap(year):
"""Return True for leap years, False for non-leap years."""
Expand Down Expand Up @@ -260,10 +290,7 @@ def yeardatescalendar(self, year, width=3):
Each month contains between 4 and 6 weeks and each week contains 1-7
days. Days are datetime.date objects.
"""
months = [
self.monthdatescalendar(year, i)
for i in range(January, January+12)
]
months = [self.monthdatescalendar(year, m) for m in Month]
return [months[i:i+width] for i in range(0, len(months), width) ]

def yeardays2calendar(self, year, width=3):
Expand All @@ -273,10 +300,7 @@ def yeardays2calendar(self, year, width=3):
(day number, weekday number) tuples. Day numbers outside this month are
zero.
"""
months = [
self.monthdays2calendar(year, i)
for i in range(January, January+12)
]
months = [self.monthdays2calendar(year, m) for m in Month]
return [months[i:i+width] for i in range(0, len(months), width) ]

def yeardayscalendar(self, year, width=3):
Expand All @@ -285,10 +309,7 @@ def yeardayscalendar(self, year, width=3):
yeardatescalendar()). Entries in the week lists are day numbers.
Day numbers outside this month are zero.
"""
months = [
self.monthdayscalendar(year, i)
for i in range(January, January+12)
]
months = [self.monthdayscalendar(year, m) for m in Month]
return [months[i:i+width] for i in range(0, len(months), width) ]


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added Enum for Month and Weekday in the calendar module
Agent-Hellboy marked this conversation as resolved.
Show resolved Hide resolved