Created
May 31, 2016 15:34
-
-
Save galvez/8509f3c8ed52959a1d715144ba69b08a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ 3000 Salary | |
- 100 Expense | |
- 200 Expense | |
- 50 Expense | |
= Group | |
- 100 Expense | |
- 200 Expense | |
- 50 Expense |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def budget(): | |
output['status'] = False | |
groups = [] | |
group = None | |
with open('Budget.txt') as handler: | |
for line in handler: | |
if line[0] in '=+' and group is None: | |
value = line[1:8].strip() | |
value = int(value) if len(value) else 0 | |
label = line[8:] | |
group = [[line[0], value, label]] | |
elif re.search('^[-+]', line): | |
op = line[0] | |
value = line[1:8].strip() | |
value = int(value) if len(value) else 0 | |
label = line[8:] | |
group.append([op, value, label]) | |
elif re.match('^\s*$', line) and group is not None: | |
groups.append(group) | |
group = None | |
if group is not None and groups[-1] != group: | |
groups.append(group) | |
for group in groups: | |
if group[0][0] == '=': | |
value = 0 | |
for op in group[1:]: | |
value += op[1] | |
elif group[0][0] == '+': | |
value = group[0][1] | |
for op in group[1:]: | |
if op[0] == '+': | |
value += op[1] | |
elif op[0] == '-': | |
value -= op[1] | |
group[1:] = sorted(group[1:], key=lambda l: l[1]) | |
if group[0][0] == '=': | |
group[0][1] = value | |
else: | |
group.append(['=', value, '\n']) | |
updated = ['\n'] | |
for group in groups: | |
for op in group: | |
updated.append('%s %5d %s' % tuple(op)) | |
updated.append('\n') | |
with open('Budget.txt', 'w') as handler: | |
for line in updated: | |
handler.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment