proper reprs for Hy models

This commit is contained in:
gilch 2017-08-03 15:26:07 -06:00 committed by Kodi Arfer
parent 51fb807cc9
commit 7674bf3267
2 changed files with 42 additions and 15 deletions

View File

@ -66,7 +66,7 @@
(or (is t HyKeyword) (and (is t str-type) (.startswith x HyKeyword.PREFIX)))
(cut x 1)
(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 Python's built-in repr produced a double-quoted string, use
; that.
@ -75,9 +75,9 @@
; convert it.
(+ "\"" (.replace (cut r 1 -1) "\"" "\\\"") "\""))))
(and (not PY3) (is t int))
(.format "(int {})" (repr x))
(.format "(int {})" (base-repr x))
(and (not PY3) (in t [long_type HyInteger]))
(.rstrip (repr x) "L")
(.rstrip (base-repr x) "L")
(and (in t [float HyFloat]) (isnan x))
"NaN"
(and (in t [float HyFloat]) (= x Inf))
@ -85,9 +85,18 @@
(and (in t [float HyFloat]) (= x -Inf))
"-Inf"
(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)
(.format "{}/{}" (f x.numerator q) (f x.denominator q))
; else
(repr x))))
(base-repr x))))
(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))

View File

@ -25,6 +25,9 @@ class HyObject(object):
return self
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, super(HyObject, self).__repr__())
_wrappers = {}
@ -59,6 +62,10 @@ def replace_hy_obj(obj, other):
% type(obj))
def repr_indent(obj):
return repr(obj).replace('\n','\n ')
class HyString(HyObject, str_type):
"""
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)
return obj
def __repr__(self):
return "HyKeyword(%s)" % (repr(self[1:]))
def strip_digit_separators(number):
return (number.replace("_", "").replace(",", "")
@ -213,7 +223,12 @@ class HyList(HyObject, list):
return ret
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[tuple] = lambda t: HyList(wrap_value(x) for x in t)
@ -225,7 +240,16 @@ class HyDict(HyList):
"""
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):
return self[0::2]
@ -244,9 +268,6 @@ class HyExpression(HyList):
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[Fraction] = lambda e: HyExpression(
[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)
"""
def __repr__(self):
return "#{%s}" % (" ".join([repr(x) for x in self]))
_wrappers[set] = lambda s: HySet(wrap_value(x) for x in s)
@ -335,9 +353,9 @@ class HyCons(HyObject):
def __repr__(self):
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:
return "(%s . %s)" % (repr(self.car), repr(self.cdr))
return "<HyCons (%s . %s)>" % (repr(self.car), repr(self.cdr))
def __eq__(self, other):
return (