Miki Tebeka  committed 2832fcd

Using keywords and start of writing

  • Participants
  • Parent commits a784092
  • Branches default

Comments (0)

Files changed (3)

       (println ((first records) 0)))
     ; With headers
-    (let [records (csv-read "log.csv" true)]
+    (let [records (csv-read "log.csv" {:headers true})]
       (println ((first records) "IP")))

File src/csvlib.clj

 (ns csvlib
-  (import com.csvreader.CsvReader))
+  (:import (com.csvreader CsvReader CsvWriter)
+            java.nio.charset.Charset))
-(defn make-converter
+(defn- make-converter
   "Make a converter function from a conversion table"
   (let [convert (fn [key value] ((get converison key identity) value))]
   With header map will be header->value, otherwise it'll be position->value.
   `conversions` is an optional map from header to a function that convert the
-  ([filename] (read-csv filename false nil))
-  ([filename headers?] (read-csv filename headers? nil))
-  ([filename headers? conversion]
+  [filename & {:keys [headers? conversion]}]
    (let [records (record-seq filename)
          convert (make-converter conversion)
          headers (if headers? (first records) (range (count (first records))))]
      (map convert
-          (map #(zipmap headers %) (if headers? (rest records) records))))))
+          (map #(zipmap headers %) (if headers? (rest records) records)))))
+(defn- indexes [coll]
+  (range (count coll)))
+(defn- gen-headers 
+  "Generate headers for combinations of headers supplied by the user (which can
+  be a nil, map or a vector, and the first record (which can also be nil, map or
+  vector)."
+  [headers record]
+  (cond
+    (and (nil? headers) (nil? record)) nil
+    (and (nil? headers) (vector? record)) nil
+    (vector? headers) (zipmap headers (indexes headers))
+    (map? headers) headers
+    (map? record) (zipmap (keys record) (indexes record))))
+(defn make-line [record headers]
+  (if (vector? record)
+    record
+    (if headers
+      (sort-by :record headers)
+      record)))
+; (defn write-csv
+;   ([filename] (write-csv filename records nil))
+;   [filename records opts]
+;   (let [delimiter (:delimiter opts \,)
+;         charset (Charset/fromName (:charset opts "UTF-8"))
+;         headers (gen-headers (:headers opts) (first records))]
+;   (let [writer (CsvWriter. filename delimiter (Charset/fromName charset))
+;         headers? (not (nil? headers))
+;         header 
+;         headers+ (if headers headers (zipmap (range

File test/csvlib_test.clj

     (is (= (last records) { 0 "Elmer" 1 "Pig" 2 "No"}))))
 (deftest test-headers
-  (let [records (read-csv "test/toons.csv" true)]
+  (let [records (read-csv "test/toons.csv" :headers? true)]
     (is (= (count records) 3))
     (is (= (first records) {"Name" "Duffy" "Animal" "Duck" "Funny" "Yes"}))
     (is (= (last records) { "Name" "Elmer" "Animal" "Pig" "Funny" "No"}))))
 (deftest test-convert
-  (let [conversion {"Funny" {"Yes" true "No" false}}
-        records (read-csv "test/toons.csv" true conversion)]
+  (let [conv {"Funny" {"Yes" true "No" false}}
+        records (read-csv "test/toons.csv" :headers? true :conversion conv)]
     (is (= (count records) 3))
     (is (= ((first records) "Funny") true))))