Sought: A Python 3 library (hopefully also supporting Python 2) that takes a Python file containing assignments of literal values (specifically, anything parseable by ast.literal_eval
) to variables and returns a dict
of the resulting namespace. If the file contains any non-literal assignments, the library either raises an error or ignores them.
For example, the library should be able to turn a file like this:
foo = 42
bar = [{"quux": None, "glarch": True}]
foo = 'reassigned'
into the value:
{
"bar": [{"quux": None, "glarch": True}],
"foo": "reassigned"
}
and a file like this:
foo = range(42)
bar = 2 + 2
quux = input()
glarch = 'Hi!'
would either produce an error or end up as just {"glarch": "Hi!"}
without actually running the range()
or input()
(Bonus points if it can parse the bar = 2 + 2
, though).
(I could've sworn I'd seen a library like this before, possibly as part of Django, but I can't find it anymore.)