Last active
May 29, 2017 12:10
-
-
Save newlawrence/4e8b684f68a09827438a553d43b4cfe9 to your computer and use it in GitHub Desktop.
A quick and dirty implementation of a math expression parser in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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