Make hy-repr support dictionary views
This commit is contained in:
parent
90a09b5b44
commit
f7ab9a6e7c
@ -7,6 +7,12 @@
|
|||||||
[hy._compat [PY3 str-type bytes-type long-type]]
|
[hy._compat [PY3 str-type bytes-type long-type]]
|
||||||
[hy.models [HyObject HyExpression HySymbol HyKeyword HyInteger HyFloat HyComplex HyList HyDict HySet HyString HyBytes]])
|
[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 {})
|
(setv -registry {})
|
||||||
(defn hy-repr-register [types f &optional placeholder]
|
(defn hy-repr-register [types f &optional placeholder]
|
||||||
(for [typ (if (instance? list types) types [types])]
|
(for [typ (if (instance? list types) types [types])]
|
||||||
@ -39,8 +45,6 @@
|
|||||||
(when started-quoting
|
(when started-quoting
|
||||||
(setv -quoting False)))))
|
(setv -quoting False)))))
|
||||||
|
|
||||||
(hy-repr-register list :placeholder "[...]" (fn [x]
|
|
||||||
(+ "[" (-cat x) "]")))
|
|
||||||
(hy-repr-register tuple (fn [x]
|
(hy-repr-register tuple (fn [x]
|
||||||
(+ "(," (if x " " "") (-cat x) ")")))
|
(+ "(," (if x " " "") (-cat x) ")")))
|
||||||
(hy-repr-register dict :placeholder "{...}" (fn [x]
|
(hy-repr-register dict :placeholder "{...}" (fn [x]
|
||||||
@ -55,10 +59,6 @@
|
|||||||
(if (% (len x) 2)
|
(if (% (len x) 2)
|
||||||
(+= text (+ " " (hy-repr (get x -1)))))
|
(+= text (+ " " (hy-repr (get x -1)))))
|
||||||
(+ "{" text "}")))
|
(+ "{" 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]
|
(hy-repr-register HyExpression (fn [x]
|
||||||
(setv syntax {
|
(setv syntax {
|
||||||
'quote "'"
|
'quote "'"
|
||||||
@ -101,6 +101,17 @@
|
|||||||
(hy-repr-register fraction (fn [x]
|
(hy-repr-register fraction (fn [x]
|
||||||
(.format "{}/{}" (hy-repr x.numerator) (hy-repr x.denominator))))
|
(.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]
|
(defn -cat [obj]
|
||||||
(.join " " (map hy-repr obj)))
|
(.join " " (map hy-repr obj)))
|
||||||
|
|
||||||
|
@ -87,6 +87,11 @@
|
|||||||
(assert (= (hy-repr (str ':mykeyword)) ":mykeyword"))
|
(assert (= (hy-repr (str ':mykeyword)) ":mykeyword"))
|
||||||
(assert (= (hy-repr (.encode kw "UTF-8") #[[b"\xef\xb7\x90:hello"]])))))
|
(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 []
|
(defn test-hy-model-constructors []
|
||||||
(import hy)
|
(import hy)
|
||||||
(assert (= (hy-repr (hy.HyInteger 7)) "'7"))
|
(assert (= (hy-repr (hy.HyInteger 7)) "'7"))
|
||||||
|
Loading…
Reference in New Issue
Block a user