-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoneycheck.py
More file actions
163 lines (150 loc) · 5.43 KB
/
Copy pathMoneycheck.py
File metadata and controls
163 lines (150 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
import time
filepath = "C:\\Users\\Charles Turvey\\Documents\\Money\\"
print(filepath)
reserves = dict()
files = dict()
def openreserve(name, createnew=True):
while True:
try:
try:
print("- Attempting to open %s" % name)
file = os.open(filepath + name + ".casct", os.O_APPEND | os.O_RDWR)
except FileNotFoundError:
if createnew:
print("- ...Creating %s first" % name)
file = os.open(filepath + name + ".casct", os.O_APPEND | os.O_RDWR | os.O_CREAT)
os.write(file, bytes("Transaction,In,Total,Date,Details", "UTF-8"))
os.close(file)
file = os.open(filepath + name + ".casct", os.O_APPEND | os.O_RDWR)
else:
print("- File not found")
return False
break
except PermissionError:
input("YOU MUST CLOSE THE FILE FIRST!\nPress Enter to try again. >> ")
print("- Opened %s" % name)
print("- Reading from file")
raw = ""
while True:
part = str(os.read(file, 100))[2:-1]
# print(part)
if part == "":
break
else:
raw += part
print("- Interpreting file data")
reserves[name] = [i.split(",") for i in raw.split(";")]
files[name] = file
return True
def addrow(name, item, amt, when):
try:
total = reserves[name][-1][2]
transaction = reserves[name][-1][0]
except KeyError:
openreserve(name)
# print(reserves[name])
total = reserves[name][-1][2]
transaction = reserves[name][-1][0]
if total == "Total":
# print(reserves[name])
total = 0
transaction = 0
else:
total = float(total)
transaction = int(transaction)
if when.lower() in ["", "now"]:
when = time.strftime("%Y%m%dGMT", time.gmtime())
row = [str(transaction + 1), str(round(float(amt), 3)), str(round(total + float(amt), 3)), when, str(item)]
# print(row)
reserves[name].append(row)
os.write(files[name], bytes(";" + ",".join(row), "UTF-8"))
def addmoneys(amt, dest):
reason = input("Why was %s added to %s?\n>> " %(amt, dest))
when = input("When was this?\n>> ")
addrow(dest, reason, amt, when)
print("- Added £%s to %s" % (amt, dest))
def removemoneys(amt, orig):
reason = input("Why was %s removed from %s?\n>> " % (amt, orig))
when = input("When was this?\n>> ")
addrow(orig, reason, -amt, when)
print("- Removed £%s from %s" % (amt, orig))
def transfermoneys(amt, orig, dest):
reason = input("Why was %s transferred from %s to %s?\n>> " % (amt, orig, dest))
when = input("When was this?\n>> ")
addrow(orig, reason, -amt, when)
addrow(dest, reason, amt, when)
print("- Transferred £%s from %s to %s" % (amt, orig, dest))
while True:
command = input("Input command\n>> ").lower().split()
while len(command) > 0:
word = command.pop(0)
if word == "add":
amt = float(command.pop(0))
dest = command.pop(0)
if dest == "to":
dest = command.pop(0)
if amt < 0:
removemoneys(-amt, dest)
else:
addmoneys(amt, dest)
elif word == "remove":
amt = float(command.pop(0))
orig = command.pop(0)
if orig == "from":
orig = command.pop(0)
if amt < 0:
addmoneys(-amt, orig)
else:
removemoneys(amt, orig)
elif word == "transfer":
amt = float(command.pop(0))
orig = command.pop(0)
if orig == "from":
orig = command.pop(0)
dest = command.pop(0)
if dest == "to":
dest = command.pop(0)
if amt < 0:
orig, dest = dest, orig
amt = -amt
transfermoneys(amt, orig, dest)
elif word == "shop":
location = command.pop(0)
if location == "at":
location = command.pop(0)
reserve = command.pop(0)
if reserve == "using":
reserve = command.pop(0)
when = input("When did you go?\n>> ")
while True:
item = input("Input item\n>> ")
if item == "":
break
price = float(input("Input price\n>> "))
addrow(reserve, "Bought %s at %s" % (item, location), -price, when)
elif word == "view":
name = command.pop(0)
try:
for row in reserves[name]:
output = ""
for item in row:
output += item + (" " * (15 - len(item)) * (len(item) < 15))
print(output)
except KeyError:
if openreserve(name, False):
for row in reserves[name]:
output = ""
for item in row:
output += item + (" " * (15 - len(item)) * (len(item) < 15))
print(output)
else:
print("No such thing available")
elif word == "spam":
name = command.pop(0)
for i in range(50):
addmoneys(i, name)
elif word == "exit":
exit()
else:
print("Eh?")