Skip to content

Commit 87d6227

Browse files
committed
Added features to program which allows user to print out calendars of different months and years. Also added comments to code to improve readability
1 parent ee5703d commit 87d6227

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

cal.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22
import argparse
33
from datetime import datetime
44

5-
parser = argparse.ArgumentParser()
5+
# Setting calendar start day
6+
calendar.setfirstweekday(calendar.SUNDAY)
67

7-
# Add an argument
8+
# Allowing flags to be passed during execution
9+
parser = argparse.ArgumentParser()
810
parser.add_argument('-y', '--year', type=int, required=False)
9-
# Parse the argument
11+
parser.add_argument('-m', '--month')
1012
args = parser.parse_args()
11-
# Print "Hello" + the user input argument
12-
# print('Hello,', args.name)
1313

14+
# Setting variables
15+
cal_month = None
16+
17+
if args.month:
18+
if args.month not in [str(month_number) for month_number in list(range(1,13))]:
19+
cal_month = list(calendar.month_name).index(args.month)
20+
else:
21+
cal_month = int(args.month)
22+
23+
# Creating custom calendar
1424
class CustomCalendar(calendar.TextCalendar):
1525
def formatday(self, day, weekday, width):
1626
if day == 0:
@@ -24,13 +34,9 @@ def cal(month=None, year=None):
2434
month = datetime.now().month
2535
if not year:
2636
year = datetime.now().year
27-
cal = CustomCalendar()
37+
cal = CustomCalendar(calendar.SUNDAY)
2838
print(cal.formatmonth(year, month))
2939

30-
# cal()
31-
32-
# input_year = int(input("Input year: "))
33-
3440
def print_calendar(year: int):
3541
cal = calendar.TextCalendar(calendar.SUNDAY)
3642
today = datetime.now()
@@ -68,8 +74,18 @@ def print_calendar(year: int):
6874
quarter_str[i] += ' ' + month_str[i]
6975
print('\n'.join(quarter_str))
7076

71-
if args.year:
77+
# Code to determine the calendar to print out
78+
if cal_month and args.year:
79+
if cal_month == datetime.now().month and args.year == datetime.now().year:
80+
cal()
81+
else:
82+
print(calendar.month(args.year, cal_month))
83+
elif args.year:
7284
print_calendar(args.year)
85+
elif cal_month:
86+
if cal_month != datetime.now().month:
87+
print(calendar.month(datetime.now().year, cal_month))
88+
else:
89+
cal()
7390
else:
74-
cal()
75-
# print_calendar(input_year)
91+
cal()

0 commit comments

Comments
 (0)