[IPython-dev] traitlets.config: common or global parameters?

G Jones glenn.caltech at gmail.com
Tue Feb 21 10:01:31 EST 2017


I am working on using traitlets.config in my project. The application has
this structure:

class Writer(Configurable):
    writer_trait = Int(4).tag(config=True)
    common_dir = Unicode('').tag(config=True)

class Acquire(Configurable):
    acquire_trait = Int(55).tag(config=True)
    common_dir = Unicode('').tag(config=True)

class Pipeline(Configurable):
    common_dir = Unicode('').tag(config=True)
    def initialize(self):
        self.writer = Writer()
        self.acquire = Acquire()

class PipelineApp(Application):
    classes = [Pipeline, Acquire, Writer]
    def initialize(self,argv=None):
        self.pipeline = Pipeline(config=self.config)

The idea is that the common_dir traits should be all the same. The code
works as written, but results in three possible lines in the settings file.
Since the settings file is just python, it's not too bad to do
c.Pipeline.common_dir = '/output'
c.Acquire.common_dir = c.Pipeline.common_dir
c.Writer.common_dir = c.Pipeline.common_dir

But it seems like there should be a better way.

I see that I could pass in a parent and access it that way:
self.writer = Writer(parent=self)

and then in writer:

But then if I want to use Writer on its own, I need to do a check if
self.parent.... or something.

Is there a better way? I hope the intent is clear, please let me know if

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20170221/89bf4a2d/attachment.html>

More information about the IPython-dev mailing list