proper reprs for Hy models
This commit is contained in:
parent
51fb807cc9
commit
7674bf3267
@ -66,7 +66,7 @@
|
|||||||
(or (is t HyKeyword) (and (is t str-type) (.startswith x HyKeyword.PREFIX)))
|
(or (is t HyKeyword) (and (is t str-type) (.startswith x HyKeyword.PREFIX)))
|
||||||
(cut x 1)
|
(cut x 1)
|
||||||
(in t [str-type HyString bytes-type HyBytes]) (do
|
(in t [str-type HyString bytes-type HyBytes]) (do
|
||||||
(setv r (.lstrip (repr x) "ub"))
|
(setv r (.lstrip (base-repr x) "ub"))
|
||||||
(+ (if (in t [bytes-type HyBytes]) "b" "") (if (.startswith "\"" r)
|
(+ (if (in t [bytes-type HyBytes]) "b" "") (if (.startswith "\"" r)
|
||||||
; If Python's built-in repr produced a double-quoted string, use
|
; If Python's built-in repr produced a double-quoted string, use
|
||||||
; that.
|
; that.
|
||||||
@ -75,9 +75,9 @@
|
|||||||
; convert it.
|
; convert it.
|
||||||
(+ "\"" (.replace (cut r 1 -1) "\"" "\\\"") "\""))))
|
(+ "\"" (.replace (cut r 1 -1) "\"" "\\\"") "\""))))
|
||||||
(and (not PY3) (is t int))
|
(and (not PY3) (is t int))
|
||||||
(.format "(int {})" (repr x))
|
(.format "(int {})" (base-repr x))
|
||||||
(and (not PY3) (in t [long_type HyInteger]))
|
(and (not PY3) (in t [long_type HyInteger]))
|
||||||
(.rstrip (repr x) "L")
|
(.rstrip (base-repr x) "L")
|
||||||
(and (in t [float HyFloat]) (isnan x))
|
(and (in t [float HyFloat]) (isnan x))
|
||||||
"NaN"
|
"NaN"
|
||||||
(and (in t [float HyFloat]) (= x Inf))
|
(and (in t [float HyFloat]) (= x Inf))
|
||||||
@ -85,9 +85,18 @@
|
|||||||
(and (in t [float HyFloat]) (= x -Inf))
|
(and (in t [float HyFloat]) (= x -Inf))
|
||||||
"-Inf"
|
"-Inf"
|
||||||
(in t [complex HyComplex])
|
(in t [complex HyComplex])
|
||||||
(.replace (.replace (.strip (repr x) "()") "inf" "Inf") "nan" "NaN")
|
(.replace (.replace (.strip (base-repr x) "()") "inf" "Inf") "nan" "NaN")
|
||||||
(is t fraction)
|
(is t fraction)
|
||||||
(.format "{}/{}" (f x.numerator q) (f x.denominator q))
|
(.format "{}/{}" (f x.numerator q) (f x.denominator q))
|
||||||
; else
|
; else
|
||||||
(repr x))))
|
(base-repr x))))
|
||||||
(f obj False))
|
(f obj False))
|
||||||
|
|
||||||
|
(defn base-repr [x]
|
||||||
|
(unless (instance? HyObject x)
|
||||||
|
(return (repr x)))
|
||||||
|
; Call (.repr x) using the first class of x that doesn't inherit from
|
||||||
|
; HyObject.
|
||||||
|
(.__repr__
|
||||||
|
(next (genexpr t [t (. (type x) __mro__)] (not (issubclass t HyObject))))
|
||||||
|
x))
|
||||||
|
38
hy/models.py
38
hy/models.py
@ -25,6 +25,9 @@ class HyObject(object):
|
|||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "%s(%s)" % (self.__class__.__name__, super(HyObject, self).__repr__())
|
||||||
|
|
||||||
|
|
||||||
_wrappers = {}
|
_wrappers = {}
|
||||||
|
|
||||||
@ -59,6 +62,10 @@ def replace_hy_obj(obj, other):
|
|||||||
% type(obj))
|
% type(obj))
|
||||||
|
|
||||||
|
|
||||||
|
def repr_indent(obj):
|
||||||
|
return repr(obj).replace('\n','\n ')
|
||||||
|
|
||||||
|
|
||||||
class HyString(HyObject, str_type):
|
class HyString(HyObject, str_type):
|
||||||
"""
|
"""
|
||||||
Generic Hy String object. Helpful to store string literals from Hy
|
Generic Hy String object. Helpful to store string literals from Hy
|
||||||
@ -106,6 +113,9 @@ class HyKeyword(HyObject, str_type):
|
|||||||
obj = str_type.__new__(cls, value)
|
obj = str_type.__new__(cls, value)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "HyKeyword(%s)" % (repr(self[1:]))
|
||||||
|
|
||||||
|
|
||||||
def strip_digit_separators(number):
|
def strip_digit_separators(number):
|
||||||
return (number.replace("_", "").replace(",", "")
|
return (number.replace("_", "").replace(",", "")
|
||||||
@ -213,7 +223,12 @@ class HyList(HyObject, list):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "[%s]" % (" ".join([repr(x) for x in self]))
|
if self:
|
||||||
|
return "%s([\n %s])" % (
|
||||||
|
self.__class__.__name__,
|
||||||
|
",\n ".join([repr_indent(x) for x in self]))
|
||||||
|
else:
|
||||||
|
return self.__class__.__name__ + "()"
|
||||||
|
|
||||||
_wrappers[list] = lambda l: HyList(wrap_value(x) for x in l)
|
_wrappers[list] = lambda l: HyList(wrap_value(x) for x in l)
|
||||||
_wrappers[tuple] = lambda t: HyList(wrap_value(x) for x in t)
|
_wrappers[tuple] = lambda t: HyList(wrap_value(x) for x in t)
|
||||||
@ -225,7 +240,16 @@ class HyDict(HyList):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "{%s}" % (" ".join([repr(x) for x in self]))
|
if self:
|
||||||
|
pairs = []
|
||||||
|
for k, v in zip(self[::2],self[1::2]):
|
||||||
|
k, v = repr_indent(k), repr_indent(v)
|
||||||
|
pairs.append("%s, %s," % (k, v))
|
||||||
|
if len(self) % 2 == 1:
|
||||||
|
pairs.append("%s # odd\n" % repr_indent(self[-1]))
|
||||||
|
return "HyDict([\n %s])" % ("\n ".join(pairs),)
|
||||||
|
else:
|
||||||
|
return "HyDict()"
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
return self[0::2]
|
return self[0::2]
|
||||||
@ -244,9 +268,6 @@ class HyExpression(HyList):
|
|||||||
Hy S-Expression. Basically just a list.
|
Hy S-Expression. Basically just a list.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return "(%s)" % (" ".join([repr(x) for x in self]))
|
|
||||||
|
|
||||||
_wrappers[HyExpression] = lambda e: HyExpression(wrap_value(x) for x in e)
|
_wrappers[HyExpression] = lambda e: HyExpression(wrap_value(x) for x in e)
|
||||||
_wrappers[Fraction] = lambda e: HyExpression(
|
_wrappers[Fraction] = lambda e: HyExpression(
|
||||||
[HySymbol("fraction"), wrap_value(e.numerator), wrap_value(e.denominator)])
|
[HySymbol("fraction"), wrap_value(e.numerator), wrap_value(e.denominator)])
|
||||||
@ -257,9 +278,6 @@ class HySet(HyList):
|
|||||||
Hy set (just a representation of a set)
|
Hy set (just a representation of a set)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return "#{%s}" % (" ".join([repr(x) for x in self]))
|
|
||||||
|
|
||||||
_wrappers[set] = lambda s: HySet(wrap_value(x) for x in s)
|
_wrappers[set] = lambda s: HySet(wrap_value(x) for x in s)
|
||||||
|
|
||||||
|
|
||||||
@ -335,9 +353,9 @@ class HyCons(HyObject):
|
|||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
if isinstance(self.cdr, self.__class__):
|
if isinstance(self.cdr, self.__class__):
|
||||||
return "(%s %s)" % (repr(self.car), repr(self.cdr)[1:-1])
|
return "<HyCons (%s %s)>" % (repr(self.car), repr(self.cdr)[9:-2])
|
||||||
else:
|
else:
|
||||||
return "(%s . %s)" % (repr(self.car), repr(self.cdr))
|
return "<HyCons (%s . %s)>" % (repr(self.car), repr(self.cdr))
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return (
|
return (
|
||||||
|
Loading…
Reference in New Issue
Block a user