import datetime
from operator import sub
from dateutil.relativedelta import relativedelta
Pushing Daisies Age
python
Prototyped a Python function that would humanize the distance between two datetime objects as they do in the TV show Pushing Daisies.
def been_alive_for(
birthday: datetime.datetime,= None,
as_of: datetime.datetime
):"""Pushing Daisies been alive for."""
if as_of is None:
= datetime.datetime.now()
as_of
= relativedelta(as_of, birthday)
delta
if delta.months:
= 30 * delta.months
days else:
= delta.days
days
= {
data "years": delta.years,
"weeks": days / 7,
}
"days"] = days - (data["weeks"] * 7)
data[
if data["weeks"] < 1:
"days"] = days
data["weeks"] = None
data[else:
= data["weeks"] - round(data["weeks"], 0)
week_remainder "weeks"] = round(data["weeks"], 0)
data["days"] = data["days"] + (week_remainder * 7)
data[
if data["days"] < 0:
"weeks"] = data["weeks"] - 1
data["days"] = 7 + data["days"]
data[
"hours"] = delta.hours
data["minutes"] = delta.minutes
data[
= []
display
for key, value in data.items():
if value:
"{0} {1}".format(round(value), key)))
display.append((
return ", ".join(display)
= been_alive_for(
myles_alive_for =datetime.datetime(1986, 9, 19, 8, 22),
birthday
)print(myles_alive_for)
36 years, 21 weeks, 3 days, 6 hours, 34 minutes
Made by Myles Braithwaite with ❤️ in Toronto.