Skip to content

Instantly share code, notes, and snippets.

@minrk
Created October 7, 2016 12:52
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/33baf09503a02e592b24673bf3c16838 to your computer and use it in GitHub Desktop.
Save minrk/33baf09503a02e592b24673bf3c16838 to your computer and use it in GitHub Desktop.
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