Skip to content

Instantly share code, notes, and snippets.

@whusnoopy
Last active August 29, 2015 14:04
Show Gist options
  • Save whusnoopy/3857656991091d1a7f91 to your computer and use it in GitHub Desktop.
Save whusnoopy/3857656991091d1a7f91 to your computer and use it in GitHub Desktop.
grep money count in wiki
# coding: utf8
from argparse import ArgumentParser
def color_str(o_str, color_num):
return '\033[1;{}m{}\033[m'.format(color_num, o_str)
def red_str(o_str):
return color_str(o_str, 31)
def green_str(o_str):
return color_str(o_str, 32)
def output_line(order_date, order_sum, notes='', with_receipt=None):
out_line = '{:>8} | {:>8.2f} | {:<}'.format(order_date, order_sum, notes)
if with_receipt is None:
print out_line
elif with_receipt:
print green_str(out_line)
else:
print red_str(out_line)
def grep_money(filename, pay_people):
fp = file(filename, 'r')
sum_paid = 0
receipt_sum = 0
non_sum = 0
for line in fp:
cols = line.split('||')
cols = map(lambda c:c.strip(), cols)
if len(cols) >= 7 and cols[4].lower() == pay_people:
val = float(cols[3])
with_receipt = (cols[5] == '有')
if with_receipt:
receipt_sum += val
else:
non_sum += val
sum_paid += val
output_line(cols[1], float(cols[3]), cols[2], with_receipt)
fp.close()
print '-'*78
output_line('SUM', sum_paid)
output_line('', receipt_sum, '有发票', with_receipt=True)
output_line('', non_sum, '没发票', with_receipt=False)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('money_filename',
help="which file to read")
parser.add_argument('-p', '--pay', default='叶文',
help="grep paid by who")
args = parser.parse_args()
grep_money(args.money_filename, args.pay.lower())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment