提交 deff95dc authored 作者: Olivier Breuleux's avatar Olivier Breuleux

pprint improvements

上级 cd80466d
...@@ -3,7 +3,7 @@ from .. import tensor as T ...@@ -3,7 +3,7 @@ from .. import tensor as T
from .. import scalar as S from .. import scalar as S
from .. import gof from .. import gof
from copy import copy from copy import copy
import sys
class PrinterState(gof.utils.scratchpad): class PrinterState(gof.utils.scratchpad):
...@@ -157,12 +157,27 @@ class SubtensorPrinter: ...@@ -157,12 +157,27 @@ class SubtensorPrinter:
sidxs.append(str(entry)) sidxs.append(str(entry))
elif isinstance(entry, S.Scalar): elif isinstance(entry, S.Scalar):
sidxs.append(inbrack_pstate.pprinter.process(inputs.pop())) sidxs.append(inbrack_pstate.pprinter.process(inputs.pop()))
elif isinstance(entry, slice):
sidxs.append("%s:%s%s" % ("" if entry.start is None or entry.start == 0 else entry.start,
"" if entry.stop is None or entry.stop == sys.maxint else entry.stop,
"" if entry.step is None else ":%s" % entry.step))
return "%s[%s]" % (pstate.clone(precedence = 1000).pprinter.process(input), return "%s[%s]" % (pstate.clone(precedence = 1000).pprinter.process(input),
", ".join(sidxs)) ", ".join(sidxs))
else: else:
raise TypeError("Can only print Subtensor.") raise TypeError("Can only print Subtensor.")
class MakeVectorPrinter:
def process(self, r, pstate):
if r.owner is None:
raise TypeError("Can only print make_vector.")
elif isinstance(r.owner.op, T.MakeVector):
return "[%s]" % ", ".join(pstate.clone(precedence = 1000).pprinter.process(input) for input in r.owner.inputs)
else:
raise TypeError("Can only print make_vector.")
class DefaultPrinter: class DefaultPrinter:
def __init__(self): def __init__(self):
...@@ -269,6 +284,8 @@ def pprinter(): ...@@ -269,6 +284,8 @@ def pprinter():
pp.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, T.Subtensor), SubtensorPrinter()) pp.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, T.Subtensor), SubtensorPrinter())
pp.assign(T.shape, MemberPrinter('shape')) pp.assign(T.shape, MemberPrinter('shape'))
pp.assign(T.fill, FunctionPrinter('fill')) pp.assign(T.fill, FunctionPrinter('fill'))
pp.assign(T.vertical_stack, FunctionPrinter('vstack'))
pp.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, T.MakeVector), MakeVectorPrinter())
return pp return pp
pp = pprinter() pp = pprinter()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论