Created
October 7, 2016 12:52
-
-
Save minrk/33baf09503a02e592b24673bf3c16838 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 import Unicode | |
from traitlets.config import Configurable, PyFileConfigLoader | |
class MyClass(Configurable): | |
thing = Unicode('default').tag(config=True) | |
# you can pick a default config file location: | |
_default_config_file = os.path.expanduser('~/.myconfig.py') | |
def load_config_file(self, config_file=None): | |
"""Load a config file. Load default config file if unspecified.""" | |
if config_file is None: | |
config_file = self._default_config_file | |
loader = PyFileConfigLoader(config_file) | |
config = loader.load_config() | |
# trigger config-loading | |
self.update_config(config) | |
c = MyClass() | |
print('before: thing=%s' % c.thing) | |
c.load_config_file('test_config.py') | |
# test_config.py has just : c.MyClass.thing = 'from file' | |
print('after: thing=%s' % c.thing) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment