Skip to content

Commit

Permalink
init & general read-file for csv/txt/dat
Browse files Browse the repository at this point in the history
  • Loading branch information
hkulyc committed Jun 18, 2022
1 parent 02ca164 commit 6a015fe
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/target
/classes
/checkouts
profiles.clj
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
/.prepl-port
.hgignore
.hg/
.clj-kondo/
.lsp/
.DS_Store
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Clojask-io

A Clojure library designed to extend the file support for Clojask. This library can also be used alone to read in and output dataset files.











## License

Copyright © 2022 Clojask-io

This program and the accompanying materials are made available under the
terms of the Eclipse Public License 2.0 which is available at
http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the Eclipse
Public License, v. 2.0 are satisfied: GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or (at your
option) any later version, with the GNU Classpath Exception which is available
at https://www.gnu.org/software/classpath/license.html.
8 changes: 8 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(defproject clojask-io "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.1"]
[clojure-interop/java.net "1.0.5"]]
:repl-options {:init-ns clojask-io.core})
9 changes: 9 additions & 0 deletions src/clojask_io/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns clojask-io.core
(:require [clojure.java.io :as io]
[clojure.string :as str]
[clojask-io.input :refer :all]))

(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!"))
52 changes: 52 additions & 0 deletions src/clojask_io/input.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(ns clojask-io.input
(:require [clojure.java.io :as io]
[clojure.string :as str]
[jdk.net.URL :refer [->url open-connection]]
[jdk.net.URLConnection :refer [get-content-length]]))


(defn get-online-size
"get the size of the response file"
[url]
(try
(let [url (->url url)
conn (open-connection url)]
(get-content-length conn))
(catch Exception e nil))
)

(defn csv-local
"read in a local csv dataset"
[path & {:keys [sep stat] :or {sep #"," stat false}}]
(let [sep (if (string? sep) (re-pattern sep) sep)
reader (io/reader path)
data (line-seq reader)
data (map #(str/split % sep -1) data)]
(if stat
{:data (fn [] data) :size (.length (io/file path))}
(fn [] data))))

(defn csv-online
[path & {:keys [sep stat] :or {sep #"," stat false}}]
(let [data (csv-local path :sep sep :stat false)]
(if stat
{:data data :size (get-online-size path)}
data)))

(defn infer-format
"infer the file format from a path"
[path]
(let [index (str/last-index-of path ".")
format (if (not= index nil) (subs path (inc (str/last-index-of path "."))) nil)]
format))

(defn read-file
[path & {:keys [sep format stat] :or {sep #"," format "txt" stat false}}]
(let [format (infer-format path)]
(if (.contains ["csv" "txt" "dat" nil] format)
(do
(if (= format nil) (println "WARNING: The format of the file cannot be inferred. Use \"csv\" by default"))
(if (or (str/starts-with? path "https://") (str/starts-with? path "http://"))
(csv-online path :sep sep :stat stat)
(csv-local path :sep sep :stat stat)))))
)
7 changes: 7 additions & 0 deletions src/clojask_io/output.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns clojask-io.output
(:require [clojure.java.io :as io]
[clojure.string :as str]))

(defn csv
"output to a csv file using a vector of vectors"
[])
7 changes: 7 additions & 0 deletions test/clojask_io/core_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns clojask-io.core-test
(:require [clojure.test :refer :all]
[clojask-io.core :refer :all]))

(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))

0 comments on commit 6a015fe

Please sign in to comment.