Like python dicts but don’t like to type quotes and brackets? Try an AttributeDict!

I prefer the attribute-style method of accessing variables in a python object, and so I searched for a way to convert a Dict into an object with attribute-accessible variables automatically created from a nested Dict. This means you access a variable with object.value instead of object["value"] – and I want to go with object.value.subvalue instead of object["value"]["subvalue"]. Those brackets and quotes get annoying after awhile. I found several possible solutions, but nothing both could correctly handle multiple layers and was very lightweight. So I made up my own solution:

The idea is to load a yaml config file, and then use an object representation to make it easy to interact with those configuration options in a python script.

To use, just load up a yaml file (or any Dict, actually), and pass it to AttributeDict.

Now you can access nested yaml variables object-style! To make it even more useful, write a child class that extends AttributeDict and can do even more stuff with your variables. I’ve included a slightly more feature-rich version of this in pypiper. If you search around for awhile, you can find various other implementations of this, but this was the only one that was both: 1. small enough to just paste at the top of something and start using right away; and 2. handles nested dictionaries correctly.