-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.hy
53 lines (44 loc) · 1.14 KB
/
db.hy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
(global db) ; sticking close to PCL
(setv db [])
(defn make-cd [title artist rating ripped]
(do
(setv cd {})
(assoc cd "title" title)
(assoc cd "artist" artist)
(assoc cd "rating" rating)
(assoc cd "ripped" ripped))
cd)
(defn add-record [cd]
(.append db cd))
; helper
(defn print-dict [d]
(for [key (.keys d)]
(print (+ key ": " (str (get d key))))))
(defn dump-db []
(for [cd db]
(print-dict cd)
(print)))
(defn y-or-n-p [prompt]
(do
(setv answer (get (.lower (raw_input prompt)) 0))
(if (in answer (, "y" "n"))
answer
(y-or-n-p prompt)
)))
(defn prompt-for-cd []
(make-cd
(raw_input "Title: ")
(raw_input "Artist: ")
(raw_input "Rating: ")
(y-or-n-p "Ripped [y/n]: ")))
; straying from PCL using while here
(defn add-cds []
(do
(add-record (prompt-for-cd))
(while (= "y" (y-or-n-p "Another? [y/n]: "))
(add-record (prompt-for-cd)))))
(import json) ; for context...
(defn save-db [fname]
(with [[f (open fname "w")]] (.write f (json.dumps db))))
(defn load-db [fname]
(with [[f (open fname)]] (setv db (json.loads (.read f)))))