dhp.structures package

Module contents

dhp data structures

class dhp.structures.ComparableMixin

Bases: object

Mixin to give proper comparisons.

Example:

class Comparable(ComparableMixin):
    def __init__(self, value):
        self.value = value

    def _cmpkey(self):
        return self.value

Returns NotImplemented if the object being compared doesn’t support the comparison.

Raises NotImplementedError if you have not overridden the _cmpkey method.

Code is from Lennart Regebro https://regebro.wordpress.com/2010/12/13/python-implementing-rich-comparison-the-correct-way/

class dhp.structures.DictDot(*args, **kwargs)

Bases: dict

A subclass of Python’s dictionary that provides dot-style access.

Nested dictionaries are recursively converted to DictDot. There are a number of similar libraries on PyPI. However, I feel this one does just enough to make things work as expected without trying to do too much.

Example:

dicdot = DictDot({
    'foo': {
        'bar': {
            'baz': 'hovercraft',
            'x': 'eels'
        }
    }
})
assert dicdot.foo.bar.baz == 'hovercraft'
assert dicdot['foo'].bar.x == 'eels'
assert dicdot.foo['bar'].baz == 'hovercraft'
dicdot.bouncy = 'bouncy'
assert dictdot['bouncy'] == 'bouncy'

DictDot raises an AttributeError when you try to read a non-existing attribute while also allowing you to create new key/value pairs using dot notation.

DictDot also supports keyword arguments on instantiation and is built to be subclass’able.