Skip to content

Instantly share code, notes, and snippets.

@newlawrence
Last active May 29, 2017 12:10
Show Gist options
  • Save newlawrence/4e8b684f68a09827438a553d43b4cfe9 to your computer and use it in GitHub Desktop.
Save newlawrence/4e8b684f68a09827438a553d43b4cfe9 to your computer and use it in GitHub Desktop.
A quick and dirty implementation of a math expression parser in Python
import math
def parse(expression, variables=None, context=vars(math)):
variables = ','.join(variables) if variables is not None else ''
context = dict(context) if context is not None else {}
return eval('lambda {}: {}'.format(variables, expression), None, context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment