def incoming(calendars): net_by_day = defaultdict(int) tax_bill = 0.0 for c in calendars: r = requests.get(c.url) ical = Calendar.from_ical(r.text) for event in ical.walk(name="VEVENT"): if event['description'].startswith(u'£'): gross = int(event['description'].replace(u'£', '')) if c.taxed: net = gross else: net = gross * (1 - TAX_RATE) tax_bill += gross - net net_by_day[day] += net for event in ical.walk(name="VFREEBUSY"): start = event['dtstart'].dt end = event['dtend'].dt day = start.date() hours = (end - start).seconds / (60.0 * 60) gross = hours * c.hourly_rate if c.taxed: net = gross else: net = gross * (1 - TAX_RATE) tax_bill += gross - net net_by_day[day] += net return net_by_day, tax_bill
def main2(): total_gross = { 'today': 0.0, 'yesterday': 0.0, 'week': 0.0, 'month': 0.0, 'year': 0.0 } total_net = { 'today': 0.0, 'yesterday': 0.0, 'week': 0.0, 'month': 0.0, 'year': 0.0 } tax_savings = 0.0 today = date.today() yesterday = today - timedelta(days=1) week = today - timedelta(days=today.weekday()) month = today.replace(day=1) year = today.replace(month=4, day=6) if year > today: year = year - timedelta(years=1) for url, hourly_rate, tax_rate in calendars: r = requests.get(url) c = Calendar.from_ical(r.text) for event in c.walk(name="VFREEBUSY"): start = event['dtstart'].dt end = event['dtend'].dt hours = (end - start).seconds / (60.0 * 60) gross = hours * hourly_rate tax = gross * tax_rate net = gross - tax tax_savings += tax d = start.date() if d <= today: if d >= year: total_gross['year'] += gross total_net['year'] += net if d >= month: total_gross['month'] += gross total_net['month'] += net if d >= week: total_gross['week'] += gross total_net['week'] += net if d == yesterday: total_gross['yesterday'] += gross total_net['yesterday'] += net if d == today: total_gross['today'] += gross total_net['today'] += net projected_gross = total_gross['year'] * 365.25 / ((today - year).days - 60) tax_estimate = max(0, projected_gross - 10000) * 0.2 print 'This Month: £%.2f (of £%.2f)' % (total_net['month'], quotas['month']) print ' This Week: £%.2f (of £%.2f)' % (total_net['week'], quotas['week']) print ' Yesterday: £%.2f (of £%.2f)' % (total_net['yesterday'], quotas['yesterday']) print ' Today: £%.2f (of £%.2f)' % (total_net['today'], quotas['today']) print '' print 'Projected Earnings: £%.2f' % projected_gross print 'Estimated Tax Bill: £%.2f' % tax_estimate print ' Tax Savings: £%.2f' % tax_savings