Skip to content

Instantly share code, notes, and snippets.

@tassaron
Created October 22, 2020 05:08
Show Gist options
  • Save tassaron/82f019bc0c8f369f2523f52e3fd3255c to your computer and use it in GitHub Desktop.
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
"""
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