Created
August 18, 2017 08:59
-
-
Save minrk/bb359a56039eabde31c388250cbfe9e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results with ipython/traitlets@6f05aad