#!/usr/bin/env python # Exercise 12e # cal command import argparse import calendar from datetime import datetime calendar.setfirstweekday(calendar.SUNDAY) parser = argparse.ArgumentParser() parser.add_argument("-1", "--one", default=1, action="store_true") parser.add_argument("-3", "--three", default=False, action="store_true") parser.add_argument("-n", "--months", type=int) args = parser.parse_args() past = 3 if args.three else 0 calendars = [] def get_date(i): year = datetime.now().year month = datetime.now().month tmp = month + i if tmp < 1 or tmp > 12: year += int(tmp / 12) month = tmp % 12 if month == 0: year -= 1 month = 12 if tmp < 1 and month < 12: year -= 1 else: month += i return year, month for i in range(args.months or past or args.one): if args.three: i -= 1 year, month = get_date(i) calendars.append(calendar.month(year, month)) for i in range(2): calendars.append("\n" * 8) for group in list(zip(*[iter(calendars)] * 3)): group = list(map(lambda x: tuple(x.split("\n")), group)) group = list(zip(*group)) line_i = 0 for row in group: line = "" for col in row: spaces = 20 - len(col) line += col + " " + (" " * spaces) if len(line.strip()) > 0: print(line) line_i += 1 if line_i == 7 and len(calendars) - 2 > 3: print()