Skip to content

Instantly share code, notes, and snippets.

@anishpatel
Created October 24, 2015 18:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anishpatel/e32f9cdc1df55692f418 to your computer and use it in GitHub Desktop.
Save anishpatel/e32f9cdc1df55692f418 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
def echoeval(expr_str):
global sys, pprint
try: sys
except NameError: import sys
try: pprint
except NameError: from pprint import pprint
calling_frame = sys._getframe().f_back
print(expr_str, '=', end=' ')
pprint(eval(expr_str, calling_frame.f_locals, calling_frame.f_globals))
if __name__ == '__main__':
num = 3.14
class A:
hello = 'Hello World!'
def f(x):
echoeval('num')
echoeval('num * 2')
echoeval('A.hello')
echoeval('x')
f([1, 2, 3])
# Output:
# num = 3.14
# num * 2 = 6.28
# A.hello = 'Hello World!'
# x = [1, 2, 3]
@linuxCowboy
Copy link

I use this Log function:

#!/usr/bin/python

from __future__ import print_function

import sys

from pprint import pprint

def Log(var_str='', pre='', post=''):
    if pre:
        print(pre, end=' ')
    if var_str:
        print(var_str, end=': ')
        calling_frame = sys._getframe().f_back
        pprint(eval(var_str, calling_frame.f_locals, calling_frame.f_globals))
    print(post)

test = "okok!"

Log('test', pre="pre", post="post")

# Output:
# pre test: 'okok!'
# post

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment