SimplifiersΒΆ

When making assertions about the structure of data or parsing and rendering with different formats, it can be easier if each datum involved is a simple dict mapping names to their values, and the data is presented as a list of these mappings.

The Simplifier protocol provides a way to take objects of any type and turn them into just such mappings or lists of mappings.

An ObjectSimplifier is included that can be used to simplify most Python objects:

>>> from chide.simplifiers import ObjectSimplifier
>>> simplifier = ObjectSimplifier()

This includes dataclasses:

from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int
>>> simplifier.one(Point(1, 2))
{'x': 1, 'y': 2}

Objects that store their attributes in slots can also be simplified:

from dataclasses import dataclass

class Small:
    __slots__ = 'a', 'b'
>>> small = Small()
>>> simplifier.one(small)
{}
>>> small.a = 'a thing'
>>> small.b = 'b thing'
>>> simplifier.one(small)
{'a': 'a thing', 'b': 'b thing'}

If you have an iterable of objects, these can all be simplified in one go:

def objects():
    yield Point(42, 13)
    small = Small()
    small.a = 0.001
    yield small
>>> simplifier.many(objects())
[{'x': 42, 'y': 13}, {'a': 0.001}]

Simplifiers for SQLAlchemy rows and ORM-mapped objects are also included.