From de03e00253ebd51c90850cc56de40172a7e8d8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20B=C3=A9langer?= Date: Sat, 17 Sep 2022 08:43:05 -0400 Subject: [PATCH 1/2] Add csv example. --- examples/csv.scm | 22 ++++++++++++++++++++++ examples/data.csv | 2 ++ 2 files changed, 24 insertions(+) create mode 100644 examples/csv.scm create mode 100644 examples/data.csv diff --git a/examples/csv.scm b/examples/csv.scm new file mode 100644 index 0000000..4fb1d0c --- /dev/null +++ b/examples/csv.scm @@ -0,0 +1,22 @@ +(import (_six python) + (github.com/gambit/python)) + +\import csv + +(define (read-csv path) + \f=open(`path) + \reader=csv.reader(f) + (let loop ((acc '())) + (with-exception-catcher + (lambda (e) + ;; The exception will be a pair (PyObject* . repr(PyObject*)) + \f.close() + (if \isinstance(`(car e), StopIteration) + (reverse acc) ;; Return the result + (write e))) ;; Propagate the exception + ;; Iterate using __next__() until StopIteration is raised + (lambda () (loop (cons \reader.__next__() acc)))))) + +(pretty-print (read-csv "./data.csv")) + +;; (("A" "B" "C") ("1" "2" "3")) diff --git a/examples/data.csv b/examples/data.csv new file mode 100644 index 0000000..12ad13e --- /dev/null +++ b/examples/data.csv @@ -0,0 +1,2 @@ +A,B,C +1,2,3 From fd6fb984da644fb457fc78783c0dde7afae52a5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20B=C3=A9langer?= Date: Sat, 17 Sep 2022 08:50:31 -0400 Subject: [PATCH 2/2] Proper relative path. --- examples/csv.scm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/csv.scm b/examples/csv.scm index 4fb1d0c..f7439f2 100644 --- a/examples/csv.scm +++ b/examples/csv.scm @@ -17,6 +17,6 @@ ;; Iterate using __next__() until StopIteration is raised (lambda () (loop (cons \reader.__next__() acc)))))) -(pretty-print (read-csv "./data.csv")) +(pretty-print (read-csv (path-expand "~~userlib/github.com/gambit/python/@/examples/data.csv"))) ;; (("A" "B" "C") ("1" "2" "3"))