Comprehensive application configuration may occur in code, config-files and command-line interfaces… and a robust Python object system should support all 3

Coming fresh off the heels of the frequently discussed topic of should config files be plain Python and coming fresh off the heels of the announcement of 2 new command-line interface generators, I felt it was time to weigh in on the simple fact that:

An industrial strength application may need to be seamlessly configured in 3 ways: the command-line, config-files and the code itself. AFAIK, only Traitlets provides seamless configuration via all 3 methods.

Traitlets builds command-interfaces for free, directly from your object definitions

TowardsDataScience has an excellent example of how simply defining Traitlets objects gives you a command-line interface for free. The only thing I have to add is that it is so wonderful to simply define my application objects and then tag a few of the attributes and then they automatically can be configured from the command-line.

Plagiarizing directly from the Traitlets documentation here is a class with 3 attributes (name, ranking and value) and we want to be able to configure name and ranking from the command-line:

class MyClass(Configurable):
    name = Unicode('defaultname'
        help="the name of the object"
    ).tag(config=True)
    ranking = Integer(0, help="the class's ranking").tag(config=True)
    value = Float(99.0)

And yes, the implementation is robust because Traitlets is what iPython is implemented in.

Traitlets objects can be configured from configured from config files

Traitlets supports loading config from files as well. And the beauty of this is that you can have your default values in a config file and override them with command-line arguments dynamically.

The synergy between command-line and config-file configuration in Traitlets with no need for custom code is the bees knees… or is that the Python’s fangs???

Traitlets objects can be configured from configured from JSON!

So you just made a call to a REST interface and got back some JSON. Biff! bang! pow! Again, no custom code but yet another way to configure your Traitlets objects!

Of course you can configure your objects from code

I have no idea why someone felt that you could not configure Python config files from code… it’s just writing more code! But again, with Traitlets the common patterns of marshalling external data into your Python objects have already been developed and battle-tested.

Traitlets is my favorite Python class definition system

I have looked high and low at Python object systems. Traitlets has almost as many features as any other that I have seen. And the ability to seamlessly configure my objects from multiple sources is yet another winning feature for me.

import traitlets

traitlets.all_hail()

Leave a Reply

Your email address will not be published. Required fields are marked *