ordered-namespace

An easy-to-use Python namespace class derived from OrderedDict, includes tab-completion


Keywords
namespace, ordereddict, structure, dotdict, tab-completion
License
MIT
Install
pip install ordered-namespace==2019.6.8

Documentation

OrderedNamespace

Dot-accessible attributes and namespaces are great ideas and this one is mine.

New Release - 2019.6.8

This latest update includes:

  • Jupyter/iPython pretty printing works even better than before. Everything lines up and is easier to read at a glance.
  • Tab auto-completion now shows both class methods and data attribute. Previously it only showed data attributes.
  • Full support for pickle serialization/deserialization.

Description

What's the big deal? Python dicts are just fine, but in the Jupyter/IPython interactive environment I hate having to deal with brackets and quotes when using tab-based auto-completion to get a quick glance at the contents of an object.

OrderedNamespace has been especially designed to have a minimal number of things that bug me. More specifically, I wanted my namespace implementation to support the following functionality:

  • Access data contents as dot-style attributes or as dict keys
  • Predictable ordering of attributes/keys
  • Automatic support for tab-completion (especially within Jupyter Notebooks)
  • Nesting: auto-convert supplied dict data to OrderedNamespace instances
  • Nice pretty printing within Jupyter environment

Ultimately I decided to write a class from scratch and using an OrderedDict instance in place of the class' built-in __dict__. This meant writing my own methods for __setitem__, __getitem__, __getattr__ and __setattr__. I also had to learn about automatic tab completion as used in Jupyter/IPython. Overall it was more work than I originally anticipated, but it was all fun and I'm glade I did it.

Install this package with:

pip install ordered-nanmespace

And then use it like this:

import ordered_namespace as ons
import numpy as np

data = ons.Struct()

data.X = [1, 2, 3]

data['Y'] = {'hello': 'I am not a robot',
             'yikes': 75.4}

data.Z = np.arange(35).reshape(7,5)

Notice above that both dict key and attribute-style techniques were used to add new information to the namespace structure. Printing out the data contents shows nicely-formatted pretty text:

>>> data

[{X: [1, 2, 3],
  Y: [{hello: 'I am not a robot', yikes: 75.4}],
  Z: array([[ 0,  1,  2,  3,  4],
            [ 5,  6,  7,  8,  9],
            [10, 11, 12, 13, 14],
            [15, 16, 17, 18, 19],
            [20, 21, 22, 23, 24],
            [25, 26, 27, 28, 29],
            [30, 31, 32, 33, 34]])}]

Inspiration for this class came from parts of the following projects:

I learned about IPython's tab-completion at this link:

The follwing were extremely helpful in sorting through IPython's rich-text display framework: