3

I'm frequently finding myself writing repetitive-feeling code in the style below (as it happens, inside a Django settings.py, but the question is meant much more generally):

STACKEXCHANGE_CLIENT_ID = os.getenv('STACKEXCHANGE_CLIENT_ID')
STACKEXCHANGE_CLIENT_SECRET = os.getenv('STACKEXCHANGE_CLIENT_SECRET')
# et cetera

Naturally there are plenty of occasions where I don't want my local variable name to match the name of the environment variable, but it's happening enough that I'm wondering if there's a nice way to avoid the name duplication.

The below code works for me:

_locals = locals()
def f(x):
    _locals[x] = os.getenv(x)

f('TERM')

print TERM

but I have no intention of using this in production, as, to quote the Python documentation on locals():

Note: The contents of this dictionary should not be modified;

so I'm wondering if there exists a valid "supported"/"approved" solution, and if so, what it might look like?

2 Answers 2

4

Referencing: How to programmatically set a global (module) variable?

import sys, os

opts = (
    'STACKEXCHANGE_CLIENT_ID', 
    'STACKEXCHANGE_CLIENT_SECRET'
)

module = sys.modules[__name__]
for o in opts:
    setattr(module, o, os.getenv(o))

In the comments of that referenced answer, it links to another that suggests globals() is reliable for updating directly as well...

_g = globals()
for o in opts:
    _g[o] = os.getenv(o)
Sign up to request clarification or add additional context in comments.

1 Comment

That'd do it, thanks very much - had a hard time trying to find the right vocabulary for what I wanted to do, so totally missed the other question
0

I guess, it's partly a matter of taste, but I would tend to put these in a dictionary, so:

STACKEXCHANGE_CLIENT_ID = os.getenv('STACKEXCHANGE_CLIENT_ID')
STACKEXCHANGE_CLIENT_SECRET = os.getenv('STACKEXCHANGE_CLIENT_SECRET')

would become:

envars['STACKEXCHANGE_CLIENT_ID'] = os.getenv('STACKEXCHANGE_CLIENT_ID')
envars['STACKEXCHANGE_CLIENT_SECRET'] = os.getenv('STACKEXCHANGE_CLIENT_SECRET')

Or, you could just use the os.environ dictionary directly.

2 Comments

This would totally work in a general case. Unfortunately the OP mentions the settings.py of a django project which is expecting them to be global variables. Kinda has him by the ghoulies on this one.
Yeah I was perhaps insufficiently clear with my requirements, I just didn't want it to be interpreted too narrowly as a Django-related question, but the global-ness is definitely a requirement; thanks though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.