Skip to content

Instantly share code, notes, and snippets.

@csaez
Created September 27, 2015 07:14
Show Gist options
  • Save csaez/1b33a76b3843c1845095 to your computer and use it in GitHub Desktop.
Save csaez/1b33a76b3843c1845095 to your computer and use it in GitHub Desktop.
Stupid workaround to set variables programmaticaly in the main namespace while working with jedi/rope autocomplete.
def _getConstants():
return {"FOO": "foo", "BAR": "bar", "BAZ": "baz"}
def _updateConstants():
import os
# assemble code
code = "# This code is automatically generated by constants.py\n\n"
for k, v in _getConstants().iteritems():
code += "{0} = '{1}'\n".format(k, v)
# diff
filepath = os.path.join(os.path.dirname(__file__), "_constants.py")
with open(filepath) as fp:
old_code = fp.read()
if old_code != code:
# update
with open(filepath, "w") as fp:
fp.write(code)
if __name__ != "__main__":
_updateConstants()
from _constants import *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment