from datetime import date from datetime import timedelta ##def apply_rules_to_new_transaction(transaction): ## ntl = new transaction list ## for nt in ntl: ## apply_rules_t(nt,ruleslist) ## ##def try_all_rules(nt,ruleslist): ## for rule in rulelist: ## apply_rule_to_t(t,rule) ## ##def apply_rule_to_t(t,rule): ## if eval(condition): ## for transform in rule.transform: ## transform.func(t,transparm) ## for action in rule.actionlist: ## action.actfunction(t,parms) ## ## return eval(conditionlist) def csv2json(csvstring,headers=False): jsonlist=[] if headers: headerlist=csvstring[0].split(",") for index, line in enumerate(csvstring): linedict={} datalist=line.split("\",\"") if index!=0: for x in range(len(datalist)): linedict[headerlist[x].replace("\"","")]=datalist[x].replace("\"","") y=x+1 jsonlist.append(linedict) return jsonlist #dictionary keyed by day ##daydict = {} ##for line in val: ## if not line["Date"] in daydict: ## daydict[line["Date"]]=[line] ## else: ## daydict[line["Date"]].append(line) # sum function. not needed? def sum(searchstring,searchcat): totalmoney = 0.0 for day in daydict: for trans in daydict[day]: # if searchstring in trans[searchcat]: if trans["Transaction Type"]=="debit": totalmoney -= float(trans["Amount"]) else: totalmoney += float(trans["Amount"]) print searchstring + " = " + str(totalmoney) #sum("Cash & ATM","Category") def current_build_options(case): buttons_to_add = [] #1 if case == "build_condition": buttons_to_add=["NOT","BOOL","NESTEDCONDITION"] #2 if case == "build_after_condition": buttons_to_add=["OR","AND","DONE"] #3 if case == "build_left_bool" or case == "build_right_bool": buttons_to_add=["STRING","NUMBER","BOOLEANVAL","FUNCTION","VARIABLE","NESTEDBOOLEAN","DONE"] #4 if case == "build_middle_bool": buttons_to_add=["EQUAL","NOT EQUAL","GT","LT","GTE","LTE"] return buttons_to_add def get_all_buttons(): buttons=["NOT","BOOL","NESTEDCONDITION","OR","AND","DONE","STRING","NUMBER","BOOLEANVAL","FUNCTION","VARIABLE","NESTEDBOOLEAN","DONE","EQUAL","NOT EQUAL","GT","LT","GTE","LTE"] return buttons def build_condition(): stage = "build_condition" command = "" condstring = "(" level=1 while (command != "QUIT"): buttonlist = current_build_options(stage) print buttonlist print "Condition so far: " + condstring command = raw_input("Enter something: ") #1 if command == "NOT": condstring = condstring + "not " stage = "build_condition" if command == "BOOL": stage = "build_left_bool" if command == "NESTEDCONDITION": condstring = condstring + "( " stage = "build_condition" level +=1 #2 if command == "OR": condstring = condstring + " or " stage = "build_condition" if command == "AND": condstring = condstring + " and " stage = "build_condition" if command == "DONE": if level>0: condstring = condstring + ")" level -=1 if level == 0: command="QUIT" #3 # buttons_to_add=["STRING","NUMBER","BOOLEANVAL","FUNCTION","VARIABLE","NESTEDBOOLEAN","DONE"] if command == "STRING": userinput = raw_input("Enter string: ") if stage == "build_left_bool": condstring = condstring + "('" + userinput + "'" stage = "build_middle_bool" else: condstring = condstring + "'" + userinput + "')" stage = "build_after_condition" if command == "NUMBER": userinput = raw_input("Enter number: ") if stage == "build_left_bool": condstring = condstring + "(" + userinput + "" stage = "build_middle_bool" else: condstring = condstring + "" + userinput + ")" stage = "build_after_condition" if command == "BOOLEANVAL": userinput = raw_input("Enter True or False: ") if stage == "build_left_bool": condstring = condstring + "(" + userinput + "" stage = "build_middle_bool" else: condstring = condstring + "" + userinput + ")" stage = "build_after_condition" if command == "NESTEDBOOLEAN": condstring = condstring + "( " level +=1 stage = "build_left_bool" if command == "FUNCTION": userinput = raw_input("Enter function: ") if stage == "build_left_bool": condstring = condstring + "(" + userinput stage = "build_middle_bool" else: condstring = condstring + userinput + ")" stage = "build_after_condition" if command == "VARIABLE": userinput = raw_input("Enter variable: ") if stage == "build_left_bool": condstring = condstring + "(" + userinput stage = "build_middle_bool" else: condstring = condstring + userinput + ")" stage = "build_after_condition" #4 #['EQUAL', 'NOTEQUAL', 'GT', 'LT', 'GTE', 'LTE'] if command == "EQUAL": condstring = condstring + "==" stage = "build_right_bool" if command == "NOTEQUAL": condstring = condstring + "!=" stage = "build_right_bool" if command == "GT": condstring = condstring + ">" stage = "build_right_bool" if command == "LT": condstring = condstring + "<" stage = "build_right_bool" if command == "GTE": condstring = condstring + ">=" stage = "build_right_bool" if command == "LTE": condstring = condstring + "<=" stage = "build_right_bool" print "Final: " + condstring condstring = "(True)" filename1="C:\\Users\\Desktop\\Downloads\\transactions.csv" #f = open(filename1, 'r') x1=""""Date","Description","Original Description","Amount","Transaction Type","Category","Account Name","Labels","Notes" "7/25/2013","Devices","CHECKCARD GOOGLE *Devices GOOGLE.COM/CH CA ON 07/24","42.69","debit","Uncategorized","Checking Orig","","" "7/24/2013","Gh Github Com","GH *GITHUB.COM 1G9SH","7.00","debit","Uncategorized","BankAmericard Cash Rewards Signature Visa","","" "7/23/2013","Valve Software","VALVE SOFTWARE","5.00","debit","Electronics & Software","BankAmericard Cash Rewards Signature Visa","","" """ #csvstring1=f.read().split("\n") csvstring1=x1.split("\n") #print csvstring1 #remove last one which is a blank line csvstring1.pop() val = csv2json(csvstring1,True) filename1="C:\\Users\\Desktop\\Downloads\\transactions2.csv" #f = open(filename1, 'r') x2=""""Date","Description","Original Description","Amount","Transaction Type","Category","Account Name","Labels","Notes" "7/28/2013","Devices","CHECKCARD GOOGLE *Devices GOOGLE.COM/CH CA ON 07/24","42.69","debit","Uncategorized","Checking Orig","","" "7/28/2013","Devices","CHECKCARD GOOGLE *Devices GOOGLE.COM/CH CA ON 07/24","43.69","debit","Uncategorized","Checking Orig","","" "7/24/2013","Gh Github Com","GH *GITHUB.COM 1G9SH","7.00","debit","Uncategorized","BankAmericard Cash Rewards Signature Visa","","" "7/23/2013","Valve Software","VALVE SOFTWARE","5.00","debit","Electronics & Software","BankAmericard Cash Rewards Signature Visa","","" """ #csvstring1=f.read().split("\n") csvstring1=x2.split("\n") #remove last one which is a blank line csvstring1.pop() val2 = csv2json(csvstring1,True) def tran1_not_in_tran2_list(tranlist1,tranlist2): retlist = [] for tran1 in tranlist1: if not tran1 in tranlist2: retlist.append(tran1) return retlist oldtranlist=val newtranlist=val2 missing_in_new=tran1_not_in_tran2_list(oldtranlist,newtranlist) added_since_old=tran1_not_in_tran2_list(val2,val) def run_rules(rulelist,tranlist): for t in tranlist: for rule in rulelist: if eval(rule["condition"]): for action in rule["actionlist"]: exec(action) rulelist = [{"condition":condstring,"actionlist":["printval=str(t['Amount']) + '-'","printval=printval + str(t['Description'])","print printval"]}] def destroy_trans(trans_to_remove): for tran1 in trans_to_remove: print "removed " + str(tran1) destroy_trans(missing_in_new) run_rules(rulelist,added_since_old) def split_transaction_days(tran, days): datelist= tran['Date'].split("/") date1 = date(int(datelist[2]),int(datelist[0]),int(datelist[1])) print str(date1) + " is start date" retlist = [] for count in range(1,days+2): date2=date1 + datetime.timedelta(hours=36*count) print str(date2) + " is day " + str(count) split_transaction_days(added_since_old[0],5)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question