Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance IceCream Debugger to Log Local Scope Variables #202

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions icecream/icecream.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def __init__(self, prefix=DEFAULT_PREFIX,
self.outputFunction = outputFunction
self.argToStringFunction = argToStringFunction
self.contextAbsPath = contextAbsPath
self.scope = False

def __call__(self, *args):
if self.enabled:
Expand All @@ -218,9 +219,11 @@ def _format(self, callFrame, *args):
prefix = callOrValue(self.prefix)

context = self._formatContext(callFrame)
if not args:
if not args and not self.scope:
time = self._formatTime()
out = prefix + context + time
elif not args and self.scope:
out = self._formatScope(callFrame, prefix, context)
else:
if not self.includeContext:
context = ''
Expand All @@ -229,6 +232,14 @@ def _format(self, callFrame, *args):

return out

def _formatScope(self, callFrame, prefix, context):
local_scope_vars = {n:v for n, v in callFrame.f_locals.items()
if not ((n.startswith("__") and n.endswith("__")) or isinstance(v, type(self)))}
scope_pairs = [(k, self.argToStringFunction(v)) for k, v in local_scope_vars.items()]
out = self._constructArgumentOutput(prefix, context, scope_pairs)
return out


def _formatArgs(self, callFrame, prefix, context, args):
callNode = Source.executing(callFrame).node
if callNode is not None:
Expand Down Expand Up @@ -336,14 +347,16 @@ def enable(self):
def disable(self):
self.enabled = False

def scope(self):
self.scope = not self.scope

def configureOutput(self, prefix=_absent, outputFunction=_absent,
argToStringFunction=_absent, includeContext=_absent,
contextAbsPath=_absent):
noParameterProvided = all(
v is _absent for k,v in locals().items() if k != 'self')
if noParameterProvided:
raise TypeError('configureOutput() missing at least one argument')

if prefix is not _absent:
self.prefix = prefix

Expand All @@ -355,7 +368,7 @@ def configureOutput(self, prefix=_absent, outputFunction=_absent,

if includeContext is not _absent:
self.includeContext = includeContext

if contextAbsPath is not _absent:
self.contextAbsPath = contextAbsPath

Expand Down