Class around PixMap objects that allows more python-like access. By Joe Strout.
[python/dscho.git] / Demo / scripts / unbirthday.py
blob2c515c4b703ac1b939c5eda35ed8f7a405d875ff
1 #! /usr/bin/env python
3 # Calculate your unbirthday count (see Alice in Wonderland).
4 # This is defined as the number of days from your birth until today
5 # that weren't your birthday. (The day you were born is not counted).
6 # Leap years make it interesting.
8 import sys
9 import time
10 import calendar
12 def main():
13 # Note that the range checks below also check for bad types,
14 # e.g. 3.14 or (). However syntactically invalid replies
15 # will raise an exception.
16 if sys.argv[1:]:
17 year = eval(sys.argv[1])
18 else:
19 year = input('In which year were you born? ')
20 if year in range(100):
21 print 'I\'ll assume that by', year,
22 year = year + 1900
23 print 'you mean', year, 'and not the early Christian era'
24 elif year not in range(1850, 2000):
25 print 'It\'s hard to believe you were born in', year
26 return
28 if sys.argv[2:]:
29 month = eval(sys.argv[2])
30 else:
31 month = input('And in which month? (1-12) ')
32 if month not in range(1, 13):
33 print 'There is no month numbered', month
34 return
36 if sys.argv[3:]:
37 day = eval(sys.argv[3])
38 else:
39 day = input('And on what day of that month? (1-31) ')
40 if month == 2 and calendar.isleap(year):
41 maxday = 29
42 else:
43 maxday = calendar.mdays[month]
44 if day not in range(1, maxday+1):
45 print 'There are no', day, 'days in that month!'
46 return
48 bdaytuple = (year, month, day)
49 bdaydate = mkdate(bdaytuple)
50 print 'You were born on', format(bdaytuple)
52 todaytuple = time.localtime(time.time())[:3]
53 todaydate = mkdate(todaytuple)
54 print 'Today is', format(todaytuple)
56 if bdaytuple > todaytuple:
57 print 'You are a time traveler. Go back to the future!'
58 return
60 if bdaytuple == todaytuple:
61 print 'You were born today. Have a nice life!'
62 return
64 days = todaydate - bdaydate
65 print 'You have lived', days, 'days'
67 age = 0
68 for y in range(year, todaytuple[0] + 1):
69 if bdaytuple < (y, month, day) <= todaytuple:
70 age = age + 1
72 print 'You are', age, 'years old'
74 if todaytuple[1:] == bdaytuple[1:]:
75 print 'Congratulations! Today is your', nth(age), 'birthday'
76 print 'Yesterday was your',
77 else:
78 print 'Today is your',
79 print nth(days - age), 'unbirthday'
81 def format((year, month, day)):
82 return '%d %s %d' % (day, calendar.month_name[month], year)
84 def nth(n):
85 if n == 1: return '1st'
86 if n == 2: return '2nd'
87 if n == 3: return '3rd'
88 return '%dth' % n
90 def mkdate((year, month, day)):
91 # Januari 1st, in 0 A.D. is arbitrarily defined to be day 1,
92 # even though that day never actually existed and the calendar
93 # was different then...
94 days = year*365 # years, roughly
95 days = days + (year+3)/4 # plus leap years, roughly
96 days = days - (year+99)/100 # minus non-leap years every century
97 days = days + (year+399)/400 # plus leap years every 4 centirues
98 for i in range(1, month):
99 if i == 2 and calendar.isleap(year):
100 days = days + 29
101 else:
102 days = days + calendar.mdays[i]
103 days = days + day
104 return days
106 main()