From 739558c6ced7a62fe7caaaf1e96cc9ae1148380b Mon Sep 17 00:00:00 2001 From: Steven Ward Date: Sat, 18 Nov 2023 11:24:46 -0500 Subject: [PATCH 1/8] Add option to calendar module CLI to specify the weekday to start each week --- Doc/library/calendar.rst | 7 +++++++ Lib/calendar.py | 12 +++++++++++- Lib/test/test_calendar.py | 4 ++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst index 157a7537f97dc6..405018502aa241 100644 --- a/Doc/library/calendar.rst +++ b/Doc/library/calendar.rst @@ -579,6 +579,13 @@ The following options are accepted: or as an HTML document. +.. option:: --firstweekday WEEKDAY, -f WEEKDAY + + The weekday to start each week. + Must be a number between 0 and 6. + Defaults to 0. + + .. option:: year The year to print the calendar for. diff --git a/Lib/calendar.py b/Lib/calendar.py index 03469d8ac96bcd..9c1867e4f8d509 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -193,7 +193,7 @@ class Calendar(object): """ def __init__(self, firstweekday=0): - self.firstweekday = firstweekday # 0 = Monday, 6 = Sunday + self.firstweekday = firstweekday def getfirstweekday(self): return self._firstweekday % 7 @@ -734,6 +734,11 @@ def main(args=None): choices=("text", "html"), help="output type (text or html)" ) + parser.add_argument( + "-f", "--firstweekday", + type=int, default=0, + help="weekday to start each week (default 0)" + ) parser.add_argument( "year", nargs='?', type=int, @@ -753,6 +758,9 @@ def main(args=None): locale = options.locale, options.encoding + if not MONDAY <= options.firstweekday <= SUNDAY: + raise IllegalWeekdayError(options.firstweekday) + if options.type == "html": if options.month: parser.error("incorrect number of arguments") @@ -761,6 +769,7 @@ def main(args=None): cal = LocaleHTMLCalendar(locale=locale) else: cal = HTMLCalendar() + cal.setfirstweekday(options.firstweekday) encoding = options.encoding if encoding is None: encoding = sys.getdefaultencoding() @@ -775,6 +784,7 @@ def main(args=None): cal = LocaleTextCalendar(locale=locale) else: cal = TextCalendar() + cal.setfirstweekday(options.firstweekday) optdict = dict(w=options.width, l=options.lines) if options.month is None: optdict["c"] = options.spacing diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py index 1f9ffc5e9a5c33..7805015d194b44 100644 --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -1001,8 +1001,12 @@ def test_illegal_arguments(self): self.assertFailure('2004', 'spam') self.assertFailure('2004', '1', 'spam') self.assertFailure('2004', '1', '1') + self.assertFailure('2004', '0') + self.assertFailure('2004', '13') self.assertFailure('2004', '1', '1', 'spam') self.assertFailure('-t', 'html', '2004', '1') + self.assertFailure('--firstweekday', '-1') + self.assertFailure('--firstweekday', '7') def test_output_current_year(self): for run in self.runners: From 88bd9a9e846add639f2bfe95ad6f69e67e10490d Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 18 Nov 2023 16:30:22 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-11-18-16-30-21.gh-issue-112240.YXS0tj.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-11-18-16-30-21.gh-issue-112240.YXS0tj.rst diff --git a/Misc/NEWS.d/next/Library/2023-11-18-16-30-21.gh-issue-112240.YXS0tj.rst b/Misc/NEWS.d/next/Library/2023-11-18-16-30-21.gh-issue-112240.YXS0tj.rst new file mode 100644 index 00000000000000..9a2caf38ca12a3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-11-18-16-30-21.gh-issue-112240.YXS0tj.rst @@ -0,0 +1 @@ +Add option to calendar module CLI to specify the weekday to start each week From 6331766fc8dcc655642f1d02b28f3d1abc9e2d54 Mon Sep 17 00:00:00 2001 From: Steven Ward Date: Tue, 5 Dec 2023 09:19:28 -0500 Subject: [PATCH 3/8] Remove year restriction from docs This is not applicable because the weekday is properly calculated for years outside of [datetime.MINYEAR, datetime.MAXYEAR] --- Doc/library/calendar.rst | 1 - Lib/calendar.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst index 405018502aa241..538f75b3b7c015 100644 --- a/Doc/library/calendar.rst +++ b/Doc/library/calendar.rst @@ -589,7 +589,6 @@ The following options are accepted: .. option:: year The year to print the calendar for. - Must be a number between 1 and 9999. Defaults to the current year. diff --git a/Lib/calendar.py b/Lib/calendar.py index 9c1867e4f8d509..912c78a5015f41 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -742,7 +742,7 @@ def main(args=None): parser.add_argument( "year", nargs='?', type=int, - help="year number (1-9999)" + help="year number" ) parser.add_argument( "month", From 7a5e02e742d1ebade083d300fe0e552f3087d943 Mon Sep 17 00:00:00 2001 From: Steven Ward Date: Tue, 5 Dec 2023 09:20:19 -0500 Subject: [PATCH 4/8] Remove validation of CLI option firstweekday --- Lib/calendar.py | 3 --- Lib/test/test_calendar.py | 4 ---- 2 files changed, 7 deletions(-) diff --git a/Lib/calendar.py b/Lib/calendar.py index 912c78a5015f41..def9f22247d339 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -758,9 +758,6 @@ def main(args=None): locale = options.locale, options.encoding - if not MONDAY <= options.firstweekday <= SUNDAY: - raise IllegalWeekdayError(options.firstweekday) - if options.type == "html": if options.month: parser.error("incorrect number of arguments") diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py index 7805015d194b44..1f9ffc5e9a5c33 100644 --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -1001,12 +1001,8 @@ def test_illegal_arguments(self): self.assertFailure('2004', 'spam') self.assertFailure('2004', '1', 'spam') self.assertFailure('2004', '1', '1') - self.assertFailure('2004', '0') - self.assertFailure('2004', '13') self.assertFailure('2004', '1', '1', 'spam') self.assertFailure('-t', 'html', '2004', '1') - self.assertFailure('--firstweekday', '-1') - self.assertFailure('--firstweekday', '7') def test_output_current_year(self): for run in self.runners: From 3a15bde9025ba6e190f5c1eda5897a898621bc8c Mon Sep 17 00:00:00 2001 From: Steven Ward Date: Sun, 28 Jan 2024 14:27:28 -0500 Subject: [PATCH 5/8] Rename CLI long option to --first-weekday --- Doc/library/calendar.rst | 2 +- Lib/calendar.py | 6 +++--- .../Library/2023-11-18-16-30-21.gh-issue-112240.YXS0tj.rst | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst index 85ca5b2d3cd0f7..a1639b561c9690 100644 --- a/Doc/library/calendar.rst +++ b/Doc/library/calendar.rst @@ -586,7 +586,7 @@ The following options are accepted: or as an HTML document. -.. option:: --firstweekday WEEKDAY, -f WEEKDAY +.. option:: --first-weekday WEEKDAY, -f WEEKDAY The weekday to start each week. Must be a number between 0 and 6. diff --git a/Lib/calendar.py b/Lib/calendar.py index def9f22247d339..a6074222a32114 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -735,7 +735,7 @@ def main(args=None): help="output type (text or html)" ) parser.add_argument( - "-f", "--firstweekday", + "-f", "--first-weekday", type=int, default=0, help="weekday to start each week (default 0)" ) @@ -766,7 +766,7 @@ def main(args=None): cal = LocaleHTMLCalendar(locale=locale) else: cal = HTMLCalendar() - cal.setfirstweekday(options.firstweekday) + cal.setfirstweekday(options.first_weekday) encoding = options.encoding if encoding is None: encoding = sys.getdefaultencoding() @@ -781,7 +781,7 @@ def main(args=None): cal = LocaleTextCalendar(locale=locale) else: cal = TextCalendar() - cal.setfirstweekday(options.firstweekday) + cal.setfirstweekday(options.first_weekday) optdict = dict(w=options.width, l=options.lines) if options.month is None: optdict["c"] = options.spacing diff --git a/Misc/NEWS.d/next/Library/2023-11-18-16-30-21.gh-issue-112240.YXS0tj.rst b/Misc/NEWS.d/next/Library/2023-11-18-16-30-21.gh-issue-112240.YXS0tj.rst index 9a2caf38ca12a3..686f0311e80dcb 100644 --- a/Misc/NEWS.d/next/Library/2023-11-18-16-30-21.gh-issue-112240.YXS0tj.rst +++ b/Misc/NEWS.d/next/Library/2023-11-18-16-30-21.gh-issue-112240.YXS0tj.rst @@ -1 +1,2 @@ -Add option to calendar module CLI to specify the weekday to start each week +Add option to calendar module CLI to specify the weekday to start each week. +Patch by Steven Ward. From 56c6ed41e15d9a34c37f3b680d1d756ccf87e199 Mon Sep 17 00:00:00 2001 From: Steven Ward Date: Sun, 28 Jan 2024 14:38:23 -0500 Subject: [PATCH 6/8] Describe weekday number convention in docs and help message --- Doc/library/calendar.rst | 2 +- Lib/calendar.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst index a1639b561c9690..d0c2d2648eb80d 100644 --- a/Doc/library/calendar.rst +++ b/Doc/library/calendar.rst @@ -588,7 +588,7 @@ The following options are accepted: .. option:: --first-weekday WEEKDAY, -f WEEKDAY - The weekday to start each week. + The weekday (``0`` is Monday, ``6`` is Sunday) to start each week. Must be a number between 0 and 6. Defaults to 0. diff --git a/Lib/calendar.py b/Lib/calendar.py index a6074222a32114..aa2a597b495dfe 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -737,7 +737,7 @@ def main(args=None): parser.add_argument( "-f", "--first-weekday", type=int, default=0, - help="weekday to start each week (default 0)" + help="weekday (0 is Monday, 6 is Sunday) to start each week (default 0)" ) parser.add_argument( "year", From 7cc4291018d21b37ad0783cfa8d80ea062135c13 Mon Sep 17 00:00:00 2001 From: Steven Ward Date: Sun, 28 Jan 2024 21:02:50 -0500 Subject: [PATCH 7/8] Update Doc/library/calendar.rst Co-authored-by: Serhiy Storchaka --- Doc/library/calendar.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst index d0c2d2648eb80d..c4dcf5641d6066 100644 --- a/Doc/library/calendar.rst +++ b/Doc/library/calendar.rst @@ -588,8 +588,8 @@ The following options are accepted: .. option:: --first-weekday WEEKDAY, -f WEEKDAY - The weekday (``0`` is Monday, ``6`` is Sunday) to start each week. - Must be a number between 0 and 6. + The weekday to start each week. + Must be a number between 0 (Monday) and 6 (Sunday). Defaults to 0. From 022ce9a49385483121b887c9531ee8a68cab6d79 Mon Sep 17 00:00:00 2001 From: Steven Ward Date: Mon, 29 Jan 2024 08:42:48 -0500 Subject: [PATCH 8/8] Restore comment per PR feedback --- Lib/calendar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/calendar.py b/Lib/calendar.py index aa2a597b495dfe..833ce331b14a0c 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -193,7 +193,7 @@ class Calendar(object): """ def __init__(self, firstweekday=0): - self.firstweekday = firstweekday + self.firstweekday = firstweekday # 0 = Monday, 6 = Sunday def getfirstweekday(self): return self._firstweekday % 7