Make hy-repr support regex match objects

This commit is contained in:
Kodi Arfer 2017-11-05 11:12:38 -08:00
parent f7ab9a6e7c
commit 3dbe05302e
2 changed files with 18 additions and 0 deletions

View File

@ -4,6 +4,7 @@
(import
[math [isnan]]
re
[hy._compat [PY3 str-type bytes-type long-type]]
[hy.models [HyObject HyExpression HySymbol HyKeyword HyInteger HyFloat HyComplex HyList HyDict HySet HyString HyBytes]])
@ -101,6 +102,14 @@
(hy-repr-register fraction (fn [x]
(.format "{}/{}" (hy-repr x.numerator) (hy-repr x.denominator))))
(setv matchobject-type (type (re.match "" "")))
(hy-repr-register matchobject-type (fn [x]
(.format "<{}.{} object; :span {} :match {}>"
matchobject-type.__module__
matchobject-type.__name__
(hy-repr (.span x))
(hy-repr (.group x 0)))))
(for [[types fmt] (partition [
list "[...]"
[set HySet] "#{...}"

View File

@ -113,6 +113,15 @@
(+ "{" (.join " " p) "}")
[p (permutations ["1 2" "3 [4 {...}]" "6 7"])]))))
(defn test-matchobject []
(import re)
(setv mo (re.search "b+" "aaaabbbccc"))
(assert (= (hy-repr mo)
(.format
#[[<{}.SRE_Match object; :span {} :match "bbb">]]
(. (type mo) __module__)
(if PY3 "(, 4 7)" "(, (int 4) (int 7))")))))
(defn test-hy-repr-custom []
(defclass C [object])