Created
August 23, 2012 12:48
-
-
Save amitsaha/3436320 to your computer and use it in GitHub Desktop.
Python Calendar module use demo (also uses clint, argparse)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| """ Example use of the calendar module | |
| http://docs.python.org/dev/library/calendar.html | |
| Uses clint for p(r)etty coloring | |
| https://github.com/kennethreitz/clint. | |
| Will silently ignore if not installed | |
| and so no colors for you. | |
| TODO: | |
| ---- | |
| Bad input checking | |
| Better CLI | |
| More stuff? | |
| """ | |
| import argparse | |
| import sys | |
| from calendar import day_name | |
| from calendar import weekday | |
| from calendar import TextCalendar | |
| from clint.textui import colored | |
| def getday(year, month, day): | |
| """ Get the weekday for a given YYYY-MM-DD """ | |
| return day_name[weekday(year, month, day)] | |
| def getyear(year): | |
| """ Get a year's worth of days """ | |
| mycal = TextCalendar() | |
| return mycal.formatyear(year) | |
| def getmonth(year, month): | |
| """ Get a year/month's worth of days""" | |
| mycal = TextCalendar() | |
| return mycal.formatmonth(year, month) | |
| def myprint(args): | |
| """ color my print """ | |
| # Recipe: http://stackoverflow.com/a/4527622/59634 | |
| try: | |
| __import__('clint') | |
| print(colored.green(args)) | |
| except ImportError: | |
| print(args) | |
| if __name__=='__main__': | |
| parser = argparse.ArgumentParser(prog='cal.py',description='A simple calendar utility') | |
| date = parser.add_argument_group('date','DD MM YYYY') | |
| date.add_argument('integers', metavar='date', type=int, nargs='+', | |
| help='DD MM YYYY prints the weekday on this date, \ | |
| MM YY prints the month\'s calendar and \ | |
| DD MM YY prints the year\'s calendar') | |
| args = parser.parse_args() | |
| args = args.integers | |
| if len(args) == 0 or len(args) > 3: | |
| parser.print_usage() | |
| if len(args) == 1: | |
| year = args[0] | |
| myprint(getyear(int(year))) | |
| if len(args) == 2: | |
| month, year = args[0:] | |
| myprint(getmonth(int(year), int(month))) | |
| if len(args) == 3 : | |
| day, month, year = args[0:] | |
| myprint('{0:d}-{1:d}-{2:d} :: {3:s}'.format(day,month,year,getday(year, month, day))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment