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

Matthias Bussonnier bussonniermatthias at gmail.com
Tue Feb 21 11:39:22 EST 2017


Hi Glenn,

I think the idea is to make a Common dummy subclass:

class MyConfigurable(configurable):
    # class can be empty, or you can move the
    # common_dir = Unicode('').tag(config=True)
    # in it, up to you.
    pass


Then just inherit from it:

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

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

  class Pipeline(MyConfigurable):
      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


Now you just need to configure MyConfigurable for it to affect all classes.

    c.MyConfigurable.common_dir = '/output'

You can target one class in particular with what you do above.

If you add parent=self

    self.pipeline = Pipeline(config=self.config, parent=self)

Then you can narrow down configuration by using parent/child selector;

    c.PipelineApp.MyConfigurable.common_dir = '/output'

It will target only MyConfigurable where `parent=self` is passed and
parent is an
instance of PipelineApp. (This is a rare case use in nbconvert to allow
configuration of each exporter independently)

Does that answer your questions ?

-- 

Matthias

On Tue, Feb 21, 2017 at 7:01 AM, G Jones <glenn.caltech at gmail.com> wrote:
> 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
> IPython-dev at scipy.org
> https://mail.scipy.org/mailman/listinfo/ipython-dev
>



More information about the IPython-dev mailing list