Created
October 22, 2020 05:08
-
-
Save tassaron/82f019bc0c8f369f2523f52e3fd3255c to your computer and use it in GitHub Desktop.
A pointless function that lets you ignore putting quotes around one-word strings
This file contains hidden or 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
""" | |
A pointless function that lets you ignore putting quotes around one-word strings | |
Not sure why I wrote this, but eval() is fun... right? | |
Input: "{cats: 3, dogs: [id, print]}" | |
Output: {'cats': 3, 'dogs': ['id', 'print']} | |
""" | |
def assume_quotes(data): | |
context_globals = {"__builtins__": {}} | |
context_locals = {} | |
finished = False | |
while not finished: | |
try: | |
# eval with as much isolation as possible (not much really) | |
converted_data = eval(data, context_globals, context_locals) | |
finished = True | |
except NameError as e: | |
tmp = str(e).split("'")[1] | |
context_locals[tmp] = tmp | |
continue | |
except AttributeError as e: | |
raise SyntaxError("Periods must be inside quotes") from e | |
except Exception as e: | |
if type(e) != SyntaxError: | |
raise SyntaxError(f"This must be in quotes") from e | |
raise | |
return converted_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment