Make hy-repr support dictionary views

This commit is contained in:
Kodi Arfer 2017-11-02 13:35:52 -07:00
parent 90a09b5b44
commit f7ab9a6e7c
2 changed files with 22 additions and 6 deletions

View File

@ -7,6 +7,12 @@
[hy._compat [PY3 str-type bytes-type long-type]]
[hy.models [HyObject HyExpression HySymbol HyKeyword HyInteger HyFloat HyComplex HyList HyDict HySet HyString HyBytes]])
(try
(import [_collections_abc [dict-keys dict-values dict-items]])
(except [ImportError]
(defclass C)
(setv [dict-keys dict-values dict-items] [C C C])))
(setv -registry {})
(defn hy-repr-register [types f &optional placeholder]
(for [typ (if (instance? list types) types [types])]
@ -39,8 +45,6 @@
(when started-quoting
(setv -quoting False)))))
(hy-repr-register list :placeholder "[...]" (fn [x]
(+ "[" (-cat x) "]")))
(hy-repr-register tuple (fn [x]
(+ "(," (if x " " "") (-cat x) ")")))
(hy-repr-register dict :placeholder "{...}" (fn [x]
@ -55,10 +59,6 @@
(if (% (len x) 2)
(+= text (+ " " (hy-repr (get x -1)))))
(+ "{" text "}")))
(hy-repr-register [set HySet] (fn [x]
(+ "#{" (-cat x) "}")))
(hy-repr-register frozenset (fn [x]
(+ "(frozenset #{" (-cat x) "})")))
(hy-repr-register HyExpression (fn [x]
(setv syntax {
'quote "'"
@ -101,6 +101,17 @@
(hy-repr-register fraction (fn [x]
(.format "{}/{}" (hy-repr x.numerator) (hy-repr x.denominator))))
(for [[types fmt] (partition [
list "[...]"
[set HySet] "#{...}"
frozenset "(frozenset #{...})"
dict-keys "(dict-keys [...])"
dict-values "(dict-values [...])"
dict-items "(dict-items [...])"])]
(defn mkrepr [fmt]
(fn [x] (.replace fmt "..." (-cat x) 1)))
(hy-repr-register types :placeholder fmt (mkrepr fmt)))
(defn -cat [obj]
(.join " " (map hy-repr obj)))

View File

@ -87,6 +87,11 @@
(assert (= (hy-repr (str ':mykeyword)) ":mykeyword"))
(assert (= (hy-repr (.encode kw "UTF-8") #[[b"\xef\xb7\x90:hello"]])))))
(when PY3 (defn test-dict-views []
(assert (= (hy-repr (.keys {1 2})) "(dict-keys [1])"))
(assert (= (hy-repr (.values {1 2})) "(dict-values [2])"))
(assert (= (hy-repr (.items {1 2})) "(dict-items [(, 1 2)])"))))
(defn test-hy-model-constructors []
(import hy)
(assert (= (hy-repr (hy.HyInteger 7)) "'7"))