Last active
December 20, 2017 19:33
-
-
Save joelbecker/1969c85d5fa063fd5f732657e589a104 to your computer and use it in GitHub Desktop.
An implementation of XKCD 1930: Calendar Facts in Python 3.
This file contains 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
import random | |
class Statement: | |
def __init__(self, *args, suffix=''): | |
for item in args: | |
assert ( | |
isinstance(item, (str, Branch)) | |
) | |
self.words = args | |
self.suffix = suffix | |
def __str__(self): | |
return ' '.join([str(w) for w in self.words]) + self.suffix | |
class Branch: | |
def __init__(self, *args, suffix=''): | |
for item in args: | |
assert ( | |
isinstance(item, (str, Statement)) | |
) | |
self.choices = args | |
self.suffix = suffix | |
def __str__(self): | |
return str(random.choice(self.choices)) + self.suffix | |
chunk_one = Branch( | |
Statement('the', Branch('fall', 'spring'), 'equinox'), | |
Statement('the', Branch('winter', 'summer'), Branch('solstice', 'olympics')), | |
Statement('the', Branch('earliest', 'latest'), Branch('sunrise', 'sunset')), | |
Statement('daylight', Branch('saving', 'savings'), 'time'), | |
Statement('leap', Branch('day', 'year')), | |
Statement('Easter'), | |
Statement('the', Branch('harvest', 'super', 'blood'), 'moon'), | |
Statement('Toyota truck month'), | |
Statement('Shark Week') | |
) | |
chunk_two = Branch( | |
Statement('happens', Branch('earlier', 'later', 'at the wrong time'), 'every year'), | |
Statement( | |
'drifts out of sync with the', | |
Branch( | |
'sun', 'moon', 'zodiac', 'atomic clock in colorado', | |
Statement(Branch('Gregorian', 'Mayan', 'lunar', 'iPhone'), 'calendar') | |
) | |
), | |
Statement('might', Branch('not happen', 'happen twice'), 'this year') | |
) | |
chunk_three = Branch( | |
Statement('time zone legislation in', Branch('indiana', 'arizona', 'russia')), | |
Statement('a decree by the Pope in the 1500s'), | |
Statement( | |
Branch( | |
'precession', 'libration', 'nutation', 'libation', | |
'eccentricity', 'obliquity' | |
), | |
'of the', | |
Branch( | |
'Moon', 'Sun', 'Earth\'s axis', | |
'Equator', 'Prime Meridian', | |
Statement(Branch('international date', 'Mason-Dixon'), 'line') | |
) | |
), | |
Statement('magnetic field reversal'), | |
Statement( | |
'an arbitrary decision by', | |
Branch('Benjamin Franklin', 'Isaac Newton', 'FDR') | |
), | |
suffix='?' | |
) | |
chunk_four = Branch( | |
Statement('it causes a predictable increase in car accidents.'), | |
Statement('that\'s why we have leap seconds.'), | |
Statement('scientists are really worried.'), | |
Statement( | |
'it was even more extreme during the', | |
Branch('Bronze Age', 'Ice Age', 'Cretaceous', '1990s'), | |
'.' | |
), | |
Statement( | |
'there\'s a proposal to fix it, but it', | |
Branch( | |
'will never happen.', | |
'actually makes things worse.', | |
'is stalled in congress.', | |
'might be unconstitutional.' | |
) | |
), | |
Statement('it\'s getting worse and no one knows why.') | |
) | |
calendar_facts = Statement( | |
'Did you know that', | |
chunk_one, | |
chunk_two, | |
'because of', | |
chunk_three, | |
'Apparently', | |
chunk_four | |
) | |
def make_calendar_fact(): | |
return str(calendar_facts) | |
if __name__ == '__main__': | |
print(make_calendar_fact()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment