Skip to content

Commit

Permalink
Add interpretation logging controlled by FUNSOR_DEBUG=1 (#93)
Browse files Browse the repository at this point in the history
* Add interpretation logging controlled by FUNSOR_DEBUG

* Simplify debug printing

* Improve pretty printing of Stack and Joint
  • Loading branch information
fritzo authored and eb8680 committed Mar 22, 2019
1 parent 5d0a291 commit 270d168
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 20 additions & 2 deletions funsor/interpreter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, division, print_function

import os
import types
from collections import OrderedDict

Expand All @@ -11,11 +12,28 @@
from funsor.registry import KeyedRegistry
from funsor.six import singledispatch

_DEBUG = int(os.environ.get("FUNSOR_DEBUG", 0))
_STACK_SIZE = 0

_INTERPRETATION = None # To be set later in funsor.terms


def interpret(cls, *args):
return _INTERPRETATION(cls, *args)
if _DEBUG:
def interpret(cls, *args):
global _STACK_SIZE
indent = ' ' * _STACK_SIZE
typenames = [cls.__name__] + [type(arg).__name__ for arg in args]
print(indent + ' '.join(typenames))
_STACK_SIZE += 1
try:
result = _INTERPRETATION(cls, *args)
finally:
_STACK_SIZE -= 1
print(indent + '-> ' + type(result).__name__)
return result
else:
def interpret(cls, *args):
return _INTERPRETATION(cls, *args)


def set_interpretation(new):
Expand Down
4 changes: 4 additions & 0 deletions funsor/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ def _pretty(self, lines, indent=0):
for arg in self._ast_values:
if isinstance(arg, Funsor):
arg._pretty(lines, indent + 1)
elif type(arg) is tuple and all(isinstance(x, Funsor) for x in arg):
lines.append((indent + 1, 'tuple'))
for x in arg:
x._pretty(lines, indent + 2)
else:
lines.append((indent + 1, str(arg)))

Expand Down

0 comments on commit 270d168

Please sign in to comment.