Skip to content

Commit

Permalink
v. 0.11 - Added rowsastrings and colsasstrings features to csv-floats…
Browse files Browse the repository at this point in the history
…-in.
  • Loading branch information
Jose Henrique Padovani committed Sep 19, 2019
1 parent d412253 commit 5dffc59
Show file tree
Hide file tree
Showing 4 changed files with 1,020 additions and 29 deletions.
39 changes: 25 additions & 14 deletions OMio.lisp
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
;; OMio - 0.1 - OM input/output: designed to make easier to import/export data
;; OMio - 0.11 - OM input/output: designed to make easier to import/export data
;; 14/sept/2019


(defvar omio)

(defpackage omio)

(in-package :omio)


(mapc 'om::compile&load (list
;(make-pathname :directory (append (pathname-directory *load-pathname*) (list "sources")) :name "xmls" :type "lisp")
(make-pathname :directory (append (pathname-directory *load-pathname*) (list "sources")) :name "omiocsv" :type "lisp")
))



;; menu/sub-menu structure
;("sub-pack-name" subpacke-lists class-list function-list class-alias-list)

; (defvar omio)
;
; (defpackage omio)

(defvar *subpackages-list* nil)
(setf *subpackages-list*
'( ("csv" nil nil (csv-floats-in) nil)
;("csv" nil nil (csv-floats-in csv-floats-in2) nil)
))


(defvar *subpackages-omio-var* nil)
(setf *subpackages-omio-var*
'(("csv" nil nil (

csv-floats-in


) nil)))

;--------------------------------------------------
;filling packages
;--------------------------------------------------
(om::fill-library *subpackages-omio-var*)

(om::fill-library *subpackages-list*)


(om::set-lib-release 0.1)
(om::set-lib-release 0.11)

(om::fill-library
'(("csv" nil nil
(om::csv-floats-in) nil)
))
(print "
;;;-----------------
;;; OMio 0.1 - in/out functions for OpenMusic
;;;
;;; José Henrique Padovani (2019)
;;;===========================================================================
")
6 changes: 3 additions & 3 deletions examples/csv/1. csv-floats-in.omp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; OM File Header - Saved 2019/09/14 17:51:18
; (6.15 :patc (om-make-point 10 260) (om-make-point 50 205) (om-make-point 1390 534) "" 183 0 nil "2019/09/14 17:51:18")
; OM File Header - Saved 2019/09/19 9:13:32
; (6.15 :patc (om-make-point 10 260) (om-make-point 0 23) (om-make-point 1440 855) "" 183 0 nil "2019/09/19 9:13:32")
; End File Header
(in-package :om)(load-lib-for (quote ("OMio")))(setf *om-current-persistent* (om-load-patch1 "1. csv-floats-in" (quote ((let ((box (om-load-boxcomment "comment" (om-make-point 375 34) (quote "2. Convert it to a list (csv table) of lists (csv rows)") "" (om-make-point 224 183) nil (om-make-color 0 0 0) (om-make-font "Verdana" 12 :family "Verdana" :style (quote (:plain)) :mode (quote nil))))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcomment "comment" (om-make-point 375 34) (quote "1. Choose a valid .csv file (must have only floats/integers)") "" (om-make-point 236 63) nil (om-make-color 0 0 0) (om-make-font "Verdana" 12 :family "Verdana" :style (quote (:plain)) :mode (quote nil))))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcall (quote genfun) "FILE-CHOOSER" (quote file-chooser) (quote nil) (om-make-point 137 51) nil nil nil nil 1))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcall (quote genfun) "CSV-FLOATS-IN" (quote csv-floats-in) (quote ((om-load-inputfun (quote input-funbox) "<n>" "FILENAME" 5))) (om-make-point 132 166) nil nil nil nil 1))) (when (fboundp (quote set-active)) (set-active box nil)) box))) (quote ((2 0 3 0 nil 0))) nil 6.15))
(in-package :om)(load-lib-for (quote ("OMio")))(setf *om-current-persistent* (om-load-patch1 "1. csv-floats-in" (quote ((let ((box (om-load-boxcomment "comment" (om-make-point 386 85) (quote "You can change the keyword :defaultformat to short-floats if you don't need long floats precision. (see documentation of the function)") "" (om-make-point 167 449) nil (om-make-color 0 0 0) (om-make-font "Verdana" 12 :family "Verdana" :style (quote (:plain)) :mode (quote nil))))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcall (quote genfun) "CSV-FLOATS-IN" (quote csv-floats-in) (quote ((om-load-inputfun (quote input-funbox) "<n>" "FILENAME" 5) (om-load-inputfun (quote input-funbox) "<n>" "ROWSASSTRINGS" nil) (om-load-inputfun (quote input-funbox) "<n>" "COLSASSTRINGS" nil) (om-load-inputkeyword (quote input-keyword) "" "defaultformat" (quote :defaultformat) (quote long-float) nil))) (om-make-point 57 450) nil nil nil nil 1))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcall (quote bastype) "aux 2" (quote t) (quote nil) (om-make-point 129 297) (om-make-point 48 30) (list 0 4) "(0 4)" nil))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcomment "comment" (om-make-point 407 42) (quote "You can specify which rows should be read as strings instead of numbers (the header of the table, for example)") "" (om-make-point 123 216) nil (om-make-color 0 0 0) (om-make-font "Verdana" 12 :family "Verdana" :style (quote (:plain)) :mode (quote nil))))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcall (quote bastype) "aux" (quote t) (quote nil) (om-make-point 79 224) (om-make-point 37 30) (list 0) "(0)" nil))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcall (quote genfun) "CSV-FLOATS-IN" (quote csv-floats-in) (quote ((om-load-inputfun (quote input-funbox) "<n>" "FILENAME" 5) (om-load-inputfun (quote input-funbox) "<n>" "ROWSASSTRINGS" nil) (om-load-inputfun (quote input-funbox) "<n>" "COLSASSTRINGS" nil))) (om-make-point 57 338) nil nil nil nil 1))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcomment "comment" (om-make-point 386 85) (quote "Likewise, you can specify which columns of the table should be read as strings instead of numbers. In this example, the entire first row will be read as a list of strings. From the second row onwards, the first and the fifth columns (nth 0 and 4) will also be read as strings.") "" (om-make-point 188 286) nil (om-make-color 0 0 0) (om-make-font "Verdana" 12 :family "Verdana" :style (quote (:plain)) :mode (quote nil))))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcomment "comment" (om-make-point 375 34) (quote "2. Convert it to a list (csv table) of lists (csv rows)") "" (om-make-point 140 119) nil (om-make-color 0 0 0) (om-make-font "Verdana" 12 :family "Verdana" :style (quote (:plain)) :mode (quote nil))))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcomment "comment" (om-make-point 375 34) (quote "1. Choose a valid .csv file ") "" (om-make-point 138 34) nil (om-make-color 0 0 0) (om-make-font "Verdana" 12 :family "Verdana" :style (quote (:plain)) :mode (quote nil))))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcall (quote genfun) "FILE-CHOOSER" (quote file-chooser) (quote nil) (om-make-point 64 24) nil nil nil nil 1))) (when (fboundp (quote set-active)) (set-active box nil)) box) (let ((box (om-load-boxcall (quote genfun) "CSV-FLOATS-IN" (quote csv-floats-in) (quote ((om-load-inputfun (quote input-funbox) "<n>" "FILENAME" 5))) (om-make-point 60 100) nil nil nil nil 1))) (when (fboundp (quote set-active)) (set-active box nil)) box))) (quote ((4 0 5 1 nil 0) (2 0 5 2 nil 0) (9 0 10 0 nil 0))) nil 6.15))
58 changes: 46 additions & 12 deletions sources/omiocsv.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,49 @@

; Functions definition

(om::defmethod! csv-floats-in ((filename pathname) &key (defaultformat 'double-float))
:initvals '(nil double-float)
:indoc '("<n>")
:icon 128
:doc "reads csv file at \"filename\" and returns a list of lists of floats (that corresponds to the rows of the csv file)"
(with-open-file (stream filename)
(loop for line = (read-line stream nil)
while line
collect (loop for start = 0 then (1+ finish)
for finish = (position #\, line :start start)
collecting (parse-float (string-trim " " (subseq line start finish)) :default-format defaultformat)
until (null finish)))))
; (om::defmethod! csv-floats-in ((filename pathname) &key (defaultformat 'long-float))
; :initvals '(nil long-float)
; :indoc '("<n>")
; :icon 128
; :doc
; "reads csv file at \"filename\" and returns a list of lists of floats (that corresponds to therows of the csv file)\n\nKeyword :defaultformat can be either long-float/double-float (default) or float/short-float/single-float."
; (with-open-file (stream filename)
; (loop for line = (read-line stream nil)
; for linecount from 0
; while line
; collect (loop for start = 0 then (1+ finish)
; for finish = (position #\, line :start start)
; for columncount from 0
; collecting (parse-float (string-trim " " (subseq line start finish)) :default-format defaultformat)
; until (null finish)))))


(om::defmethod! csv-floats-in ((filename pathname) &optional rowsasstrings colsasstrings &key (defaultformat 'long-float))
:initvals '(nil nil nil long-float)
:indoc '("<n>")
:icon 128
:doc
"reads csv file at \"filename\" and returns a list of lists of floats (that corresponds to therows of the csv file)\n\n
Optional parameters rowsasstrings and colsasstrings can be nil or lists: they can be used to specify rows or columns of the original csv file that should not be read as float numbers.\n\n
Keyword :defaultformat can be either long-float/double-float (default) or float/short-float/single-float."
(with-open-file (stream filename)
(loop for line = (read-line stream nil)
while line
for linecount from 0
collect
(loop for start = 0 then (1+ finish)
for columnnumber from 0
for finish = (position #\, line :start start)
collect
(cond
(colsasstrings ;;se colunas forem especificadas
(if rowsasstrings ;; primeiro ve se é para printar toda a linha como string
(if (position linecount rowsasstrings)(string-trim " " (subseq line start finish)) ;;linha como string
(if (position columnnumber colsasstrings)(string-trim " " (subseq line start finish))(parse-float (string-trim " " (subseq line start finish)) :default-format defaultformat));; senão, ve se a coluna atual deve ser lida como string ou como numero
)
(if (position columnnumber colsasstrings)(string-trim " " (subseq line start finish))(parse-float (string-trim " " (subseq line start finish)) :default-format defaultformat))
)
)
(rowsasstrings (if (position linecount rowsasstrings)(string-trim " " (subseq line start finish))(parse-float (string-trim " " (subseq line start finish)) :default-format defaultformat)))
(t (parse-float (string-trim " " (subseq line start finish)) :default-format defaultformat)))
until (null finish)))))
Loading

0 comments on commit 5dffc59

Please sign in to comment.