Skip to content

Instantly share code, notes, and snippets.

@petersohn petersohn/facts.py
Last active Dec 19, 2017

Embed
What would you like to do?
Calendar Facts
#!/usr/bin/env python3
# Data source: https://xkcd.com/1930/
import random
import re
class Or(list):
def __init__(self, *args):
super(Or, self).__init__(args)
def generate(x):
if type(x) is str:
return x
if type(x) is Or:
return generate(random.choice(x))
else:
return str.join(' ', map(generate, x))
result = generate([
'Did you know that',
Or(
['the', Or('Fall', 'Spring'), 'equinox'],
['the', Or('Winter', 'Summer'), Or('solstice', 'Olympics')],
['the', Or('earliest', 'latest'), Or('sunrise', 'sunset')],
['daylight', Or('saving', 'savings'), 'time'],
['leap', Or('year', 'day')],
'easter',
['the', Or('harvest', 'super', 'blood'), 'moon'],
'Toyota Truck Month',
'Shark Week'
),
Or(
['happens', Or('earlier', 'later', 'at the wrong time'), 'every year'],
[
'drifts out of sync with the',
Or(
'Sun',
'Moon',
'Zodiac',
[Or('Gregorian', 'Mayan', 'Lunar', 'iPhone'), 'calendar'],
'atomic clock in Colorado'
)
],
['might', Or('not happen', 'happen twice'), 'this year']
),
'because of',
Or(
['time zone legislation in', Or('Indiana', 'Arizona', 'Russia')],
'a decree by the pope in the 1500s',
[
Or(
'precession',
'libration',
'nutation',
'libation',
'eccentricity',
'obliquity'),
'of the',
Or(
'Moon',
'Sun',
'Earth\'s axis',
'equator',
'prime meridian',
[Or('international date', 'mason-dixon'), 'line'],
)
],
'magnetic field reversal',
['an arbitrary decision by ', Or('Benjamin Franklin', 'Isaac Newton', 'FDR')]
),
'? Apparently,',
Or(
'it causes a predictable increase in car accidents',
'that\'s why we have leap seconds',
'that\'s why we have leap seconds',
[
'it was even more extreme during the',
Or('Bronze Age', 'Ice Age', 'Cretaceous', '1990s')
],
[
'there\'s a proposal to fix it, but it',
Or(
'will never happen',
'actually makes things worse',
'is stalled in congress',
'might be unconstitutional'
)
],
'it\'s getting worse and no one knows why'
),
'.',
'While it may seem like trivia, it',
Or(
'causes huge headaches for software developers',
'is taken advantage of by high-speed traders',
'triggered the 2003 Northeast Blackout',
'has to be corrected for by GPS satellites',
'is now recognized as a major cause of World War I'
),
'.'
])
print(re.sub(r' ([.?])', r'\1', result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.