Skip to content

Instantly share code, notes, and snippets.

@hectorddmx
Created August 2, 2011 09:19
Show Gist options
  • Save hectorddmx/1119872 to your computer and use it in GitHub Desktop.
Save hectorddmx/1119872 to your computer and use it in GitHub Desktop.
This was a test I had some years ago, I used it as motivation to learn Python basics.
#!/usr/bin/env python
#-*- coding:utf-8 -*-
"""Prints a calendar by month, the user provides the year in a four digit format
and the month.
Exported Classes:
MonthCalendar - Formats and does the calculations for the CLI output of the
calendar
Cli - Recieves the data input and validates it
Author - Lecksfrawen <[email protected]>
Date: Tue Apr 13 16:23:55 CDT 2010
"""
import calendar
class MonthCalendar:
""" Prints the title of the desired month calendar, the week order starts
from Sunday. """
def __init__(self, year = None, month = None):
self.months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
self.year = int(year)
self.month = int(month)
self.weeksday = calendar.setfirstweekday(6)
def cal_header(self):
month_year = "%15s %3d " %(self.months[self.month-1], self.year)
return month_year
def week_order(self):
week_order = "%3s %3s %3s %3s %3s %3s %3s" %("Su","Mo","Tu","We","Th","Fr","Sa")
return week_order
def create_calendar(self):
month = calendar.monthcalendar(self.year, self.month)
nweeks = len(month)
""" Knowing the number of weeks in the month, we can create a for loop
which counts through a range() from 0 to the number of weeks. As it does,
it will print out the rest of the calendar. """
output_cal = ""
for w in range(0,nweeks):
week = month[w]
output_cal += "\n"
for x in xrange(0,7):
day = week[x]
if day == 0:
empty_day = ' '
output_cal += '%3s ' %(empty_day)
else:
output_cal += '%3s ' %(day)
output_cal += "\n"
return output_cal
class Cli:
""" Provides de input interface """
def __init__(self):
pass
def get_year(self):
""" Obtains the year from the cmd """
error = True
while error:
sel_year = raw_input('Introduce el año: ')
if sel_year.isdigit():
sel_year = int(sel_year)
if 1899 < sel_year < 3000:
error = False
else:
print "\nEl año no puede ser menor a 1900 o mayor a 2999.\n \
Introduzca un año valido.\n"
else:
print "Porfavor dame un entero, no un string"
return sel_year
def get_month(self):
""" Obtains the month from the cmd """
error = True
while error:
sel_month = raw_input('Introduce el mes: ')
if sel_month.isdigit():
sel_month = int(sel_month)
if 0 < sel_month and sel_month < 13:
print ""
error = False
else:
print "\nEl mes no puede ser menor a 01 o mayor que 12.\n \
Introduzca un mes valido.\n"
else:
print "Porfavor dame un entero, no un string"
return sel_month
def main():
""" Asks for the month and year and proceeds to print the calendar
for that specific month.
"""
myCli = Cli()
year = myCli.get_year()
month = myCli.get_month()
myMonthCal = MonthCalendar(year, month)
print myMonthCal.cal_header()
print myMonthCal.week_order()
print myMonthCal.create_calendar()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment