by fspeech · 8y ago · 0 comments
For example the top node for accounting is like this:
#@killcolor
# accounting
from datetime import date
import pprint
import math
<<defs>>
year = 2006
<<2006>>
inc_year()
<<2007>>
inc_year()
<<2008>>
... more data
inc_year()
<<2018>>
inc_year(0) #update certain yearly balance records without
advancing
def runacct(argv):
if len(argv) > 1:
if argv[1] == 'q':
transquery(argv[2].upper())
elif argv[1] == 'g':
groupquery(argv[2].upper())
elif argv[1] == 'h':
holdingquery(argv[2].upper())
elif argv[1] == 'm':
if len(argv) == 3:
monthquery(int(argv[2]))
else:
monthquery(int(argv[2]), int(argv[3]))
elif ... many other query options
else:
balancequery()
if __name__ == '__main__':
from sys import argv
runacct(argv) class acct(object):
# bank and brokerage account
allacct = []
def __init__(self, name, balance, holdings=None):
self.name = name
self.balance = balance
self.holdings = holdings
self.xferamt = 0
self.accum = 0 # used to track cumulation ad hoc values
acct.allacct.append(self)
def buy(self, sec, amt, cost, day):
rec = trans(('buy', sec, amt), -cost, day, self)
self.balance -= cost
if not self.holdings.has_key(sec):
self.holdings[sec] = [0]
self.holdings[sec][0] += amt
self.holdings[sec].append(rec)
......
class trans(object):
# transaction record object track cash invovled in each transaction
allrec = []
def __init__(self, t, amt, day, where):
try:
self.type = t
self.amt = amt
self.date = date(year, month, day)
self.where = where
trans.allrec.append(self)
except Exception as err:
print t, amt, year, month, day, where
raise err
Then for each data line I just do any of the following: SOMEACCT.buy(...)
SOMEACCT.sell(...)
SOMEACCT.credit(...)
SOMEACCT.debit(...)
SOMEACCT.xfer(...)
# classified by tax categories:
SOMEACCT.int(...)
SOMEACCT.div(...)
SOMEACCT.fdiv(...)
SOMEACCT.mint(...)
SOMEACCT.mdiv(...)
Then it is easy to query and do computations in any way one cares to write a function to do.