Skip to content

Instantly share code, notes, and snippets.

@minrk
Created August 18, 2017 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minrk/bb359a56039eabde31c388250cbfe9e4 to your computer and use it in GitHub Desktop.
Save minrk/bb359a56039eabde31c388250cbfe9e4 to your computer and use it in GitHub Desktop.
import os
from traitlets.config import Config, Configurable, Application
from traitlets import Unicode
os.environ['FOO_VALUE'] = 'foo-from-env'
os.environ['APP_VALUE'] = 'app-from-env'
class Foo(Configurable):
value = Unicode('default', envvar='FOO_VALUE', config=True)
class MyApp(Application):
value = Unicode('app-default', envvar='APP_VALUE', config=True)
skip_env = False
def initialize(self, argv=None):
super().initialize(argv)
self.load_config_file(__file__, skip_env=self.skip_env)
self.foo = Foo(parent=self)
def start(self):
print('app value', self.value)
print('foo value', self.foo.value)
if __name__ == '__main__':
argv = ['--MyApp.value=app-cli', '--Foo.value=foo-cli']
for skip_env in (True, False):
MyApp.skip_env = skip_env
print("no cli (skip_env=%s)" % skip_env)
MyApp.launch_instance([])
print('cli args and env, cli should win (skip_env=%s)' % skip_env)
MyApp.launch_instance(argv)
else:
# I'm my own config file!
try:
c = get_config()
except NameError:
pass
else:
# I'm a config file!
c.MyApp.value = 'app_cfg_file'
c.Foo.value = 'app_foo_file'
@minrk
Copy link
Author

minrk commented Aug 18, 2017

Results with ipython/traitlets@6f05aad

no cli (skip_env=True):
app value app_cfg_file # correct
foo value foo-from-env # incorrect
cli args and env, cli should win (skip_env=True):
app value app-cli # correct
foo value foo-from-env # incorrect
no cli (skip_env=False):
app value app-from-env # correct
foo value foo-from-env # correct
cli args and env, cli should win (skip_env=False):
app value app-from-env # incorrect
foo value foo-from-env # incorrect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment