Skip to content

Instantly share code, notes, and snippets.

@bebraw
Created January 9, 2010 13:20
Show Gist options
  • Save bebraw/272888 to your computer and use it in GitHub Desktop.
Save bebraw/272888 to your computer and use it in GitHub Desktop.
class Python:
priority = 'low'
def matches(self, expression):
return True
def execute(self, expression, variables):
'''
>>> python = Python()
>>> python.execute('5+13', {})
18
>>> python.execute('a+7', {'a': 13, })
20
>>> python.execute('foobar', {})
"NameError: name 'foobar' is not defined"
'''
try:
return eval(expression, {}, variables)
except Exception, e:
class_name = e.__class__.__name__
return class_name + ': ' + str(e)
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment