traitlets.config: common or global parameters?

G Jones <[email protected]> Tue, 21 Feb 2017 10:01:31 -0500
Newsgroups gmane.comp.python.ipython.devel
Message-ID <CAGK_ABdKkL5pJ4kykoXAG3Lg6=SvVgO4U8QewtqUiBjAFW4D8A@mail.gmail.com>
Hello,

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)
        self.pipeline.initialize()


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:
self.parent.common_dir

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
not.

Thanks,
Glenn

_______________________________________________
IPython-dev mailing list
[email protected]
https://mail.scipy.org/mailman/listinfo/ipython-dev