Skip to content

Instantly share code, notes, and snippets.

@odedlaz
Last active December 6, 2016 09:46
Show Gist options
  • Save odedlaz/0355863b5e26c15f0899442c9e99f7bd to your computer and use it in GitHub Desktop.
Save odedlaz/0355863b5e26c15f0899442c9e99f7bd to your computer and use it in GitHub Desktop.
eval_print.py
import os
import six
from time import time
from inspect import currentframe
from string import Formatter
string_formatter = Formatter()
devnull = open(os.devnull, 'w')
six.moves.builtins.std_print = print
def print(text, **kwargs):
caller = currentframe().f_back
keywords = {kw for _, kw, _, _
in string_formatter.parse(text) if kw}
for keyword in keywords:
value = eval(keyword, caller.f_globals, caller.f_locals)
text = text.replace("{{{}}}".format(keyword), str(value))
std_print(text, **kwargs)
name = "oded"
t = time()
for _ in range(50000):
print("my name is {name} & 2*3={2*3}",
file=devnull)
std_print("block took {0:.3f} seconds".format(time() - t))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment