Permalink
Browse files

Barclays namespace and a more complete import and basic processing of…

… all statements
  • Loading branch information...
1 parent 672b2ed commit f2f66ec37e21f3a44bda410aaba4f900ab125203 @stathissideris committed Sep 14, 2011
Showing with 53 additions and 29 deletions.
  1. +1 −0 .gitignore
  2. +51 −0 src/moneyz/barclays.clj
  3. +1 −29 src/moneyz/core.clj
View
@@ -5,3 +5,4 @@ pom.xml
.lein-failures
.lein-deps-sum
/data/
+/src/moneyz/local.clj
View
@@ -0,0 +1,51 @@
+(ns moneyz.barclays
+ (:use (incanter core charts io))
+ (:require [clojure.set :as set])
+ (:import (java.text SimpleDateFormat)))
+
+(defn parse-date [date] (.parse (SimpleDateFormat. "dd/MM/yyyy") date))
+(defn parse-dates [dates] (map parse-date dates))
+(defn to-millis [dates] (map #(.getTime %) dates))
+(def process-dates (comp to-millis parse-dates))
+
+(defn get-csvs [path]
+ (filter #(.endsWith % ".csv")
+ (map #(.getAbsolutePath %)
+ (.listFiles (java.io.File. path)))))
+
+(defn merge-statements
+ [d1 d2]
+ (dataset
+ (:column-names d1)
+ (sort-by :Date (distinct (concat (:rows d1) (:rows d2))))))
+
+(defn split-by-account [statement]
+ (into {}
+ (map
+ (fn [[key d]] [key (dataset (:column-names statement) d)])
+ (group-by :Account (:rows statement)))))
+
+(defn calculate-balance-column [statement]
+ (reverse (reductions + (reverse (map :Amount (:rows statement))))))
+
+(defn add-balance-column [statement]
+ (let [balance-column (calculate-balance-column statement)]
+ (-> statement
+ (assoc :column-names (conj (:column-names statement) :Balance))
+ (assoc :rows
+ (into [] (map
+ (fn [row bal] (assoc row :Balance bal))
+ (:rows statement) balance-column))))))
+
+(defn read-statement [filename]
+ (read-dataset filename :header true))
+
+(defn import-statements [data-path & [aliases]]
+ (let [r (split-by-account
+ (reduce merge-statements
+ (map read-statement
+ (get-csvs data-path))))
+ r (zipmap (keys r) (map add-balance-column (vals r)))]
+ (if aliases
+ (set/rename-keys r aliases)
+ r)))
View
@@ -1,6 +1,5 @@
(ns moneyz.core
- (:use (incanter core charts io))
- (:import (java.text SimpleDateFormat)))
+ (:use (incanter core charts io)))
#_(defn read-data
"Use incanter's read-dataset instead."
@@ -11,10 +10,6 @@
rows (rest data)]
(incanter.core.Dataset. (into [] columns) rows)))
-(defn parse-date [date] (.parse (SimpleDateFormat. "dd/MM/yyyy") date))
-(defn parse-dates [dates] (map parse-date dates))
-(defn to-millis [dates] (map #(.getTime %) dates))
-(def process-dates (comp to-millis parse-dates))
(def d1
(read-dataset
@@ -26,29 +21,6 @@
"/Users/sideris/devel/moneyz/data/2011-09-05-statement export.csv"
:header true))
-(defn merge-statements
- [d1 d2]
- (dataset
- (:column-names d1)
- (sort-by :Date (distinct (concat (:rows d1) (:rows d2))))))
-
-(defn group-by-account [statement]
- (into {}
- (map
- (fn [[key d]] [key (dataset (:column-names statement) d)])
- (group-by :Account (:rows statement)))))
-
-(defn calculate-balance-column [statement]
- (reverse (reductions + (reverse (map :Amount (:rows statement))))))
-
-(defn add-balance-column [statement]
- (let [balance-column (calculate-balance-column statement)]
- (-> statement
- (assoc :column-names (conj (:column-names statement) :Balance))
- (assoc :rows
- (into [] (map
- (fn [row bal] (assoc row :Balance bal))
- (:rows statement) balance-column))))))
(def d (group-by-account (merge-statements d1 d2)))

0 comments on commit f2f66ec

Please sign in to comment.