Advertisement
hd1

Untitled

hd1
Oct 16th, 2014
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. #!/usr/local/bin/python
  2. import argparse
  3. import bz2
  4. import calendar
  5. import csv
  6. import datetime
  7. import locale
  8. import logging
  9.  
  10. class EnUSCookedTest:
  11.     # A cooked "en_US" locale
  12.  
  13.     cooked_values = {
  14.         'currency_symbol': '$',
  15.         'decimal_point': '.',
  16.         'frac_digits': 2,
  17.         'grouping': [3, 3, 0],
  18.         'int_curr_symbol': 'USD ',
  19.         'int_frac_digits': 2,
  20.         'mon_decimal_point': '.',
  21.         'mon_grouping': [3, 3, 0],
  22.         'mon_thousands_sep': ',',
  23.         'n_cs_precedes': 1,
  24.         'n_sep_by_space': 0,
  25.         'n_sign_posn': 1,
  26.         'negative_sign': '-',
  27.         'p_cs_precedes': 1,
  28.         'p_sep_by_space': 0,
  29.         'p_sign_posn': 1,
  30.         'positive_sign': '',
  31.         'thousands_sep': ','
  32.     }
  33.  
  34. def from_utc(utcTime,fmt="%Y-%m-%dT%H:%M:%S"):
  35.     """
  36.    Convert UTC time string to time.struct_time
  37.    """
  38.     # change datetime.datetime to time, return time.struct_time type
  39.     return datetime.datetime.strptime(utcTime, fmt)
  40.  
  41. if __name__ == '__main__':
  42.     args = argparse.ArgumentParser(description='Reads the file put out by the receipts processor put out by procmail.py and gets monthly totals')
  43.     args.add_argument('-v', '--verbose', action='store_true', help='turn debugging up')
  44.     args.add_argument('--month', type=int, help='Month whose total we want', action='store', default=0)
  45.     args.add_argument('--day', type=int, help='Billing End date', action='store', default=22)
  46.     args.add_argument('--locale', help="Locale for you, python doesn't support currency formatting in the default locale", action='store', default='en_US.ISO8859-1')
  47.     parsed = args.parse_args()
  48.  
  49.     if parsed.verbose:
  50.         logging.basicConfig(level=logging.DEBUG)
  51.     else:
  52.         logging.basicConfig(level=logging.FATAL)
  53.    
  54.     yesterday = datetime.date.today()
  55.     yesterday = yesterday.replace(month=yesterday.month-parsed.month, day=parsed.day)
  56.     logging.debug('Locale is {}'.format(parsed.locale))
  57.     locale._override_localeconv = EnUSCookedTest.cooked_values
  58.     monthly_total = 0;
  59.     with bz2.BZ2File('rcpts.bz2') as fin:
  60.         reader = csv.DictReader(fin, fieldnames = ['Store', 'Amount', 'Timestamp'])
  61.         reader.next()
  62.         for transaction in list(reader):
  63.             logging.debug('Transaction Date is {}'.format(transaction['Timestamp']))
  64.             logging.debug('Target Date: {}'.format(yesterday.strftime('%c')))
  65.             logging.debug(type(from_utc(transaction['Timestamp'])))
  66.             if yesterday >= from_utc(transaction['Timestamp']).date():
  67.                 monthly_total = monthly_total + float(transaction['Amount'])
  68.     print 'Total for the month of {} is {}'.format(yesterday.strftime('%B'), locale.currency(monthly_total))
  69.     logging.debug(str(yesterday))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement