ByKey

class rolumns.ByKey(source: Optional[Union[Source, str]] = None)[source]

Groups rows by each key in a dictionary.

from rolumns import ByKey, Columns
from rolumns.renderers import RowsRenderer

data = {
    "today": {
        "event": "Bought sausages",
    },
    "yesterday": {
        "event": "Bought a train set",
    },
}

columns = Columns(ByKey())
columns.add("When", ByKey.key())
columns.add("Event", ByKey.value("event"))

print(list(RowsRenderer(columns).render(data)))
[['When',      'Event'],
 ['today',     'Bought sausages'],
 ['yesterday', 'Bought a train set']]
from rolumns import ByKey, Columns
from rolumns.renderers import RowsRenderer

data = {
    "today": [
        {"event": "Bought sausages"},
        {"event": "Bought bread"},
    ],
    "yesterday": [
        {"event": "Bought a train set"},
        {"event": "Bought a book"},
    ],
}

columns = Columns(ByKey())
columns.add("When", ByKey.key())

values = columns.group(ByKey.values())
values.add("Event", "event")

print(list(RowsRenderer(columns).render(data)))
[['When',      'Event'],
 ['today',     'Bought sausages'],
 ['today',     'Bought bread'],
 ['yesterday', 'Bought a train set'],
 ['yesterday', 'Bought a book']]
classmethod key() str[source]

Gets the path to the key name.

name() str[source]

Gets the name of this group unique to its parent column set.

resolve(data: Dict[Any, Any]) Iterable[Dict[str, Any]][source]

Resolves data to an iterable list of KeyValuePair.

classmethod value(path: str) str[source]

Gets the path to a value within each dictionary key’s value.

Use this if each key’s value is a flat object. If the value is iterable then use ByKey.values() to get a group instead.

from rolumns import ByKey, Columns
from rolumns.renderers import RowsRenderer

data = {
    "today": {
        "event": "Bought sausages",
    },
    "yesterday": {
        "event": "Bought a train set",
    },
}

columns = Columns(ByKey())
columns.add("When", ByKey.key())
columns.add("Event", ByKey.value("event"))

print(list(RowsRenderer(columns).render(data)))
[['When',      'Event'],
 ['today',     'Bought sausages'],
 ['yesterday', 'Bought a train set']]
classmethod values() ByValue[source]

Gets a grouper for each dictionary key’s values.

Use this if each key’s value is iterable. If the value is a flat object then use ByKey.value() to describe a path instead.

from rolumns import ByKey, Columns
from rolumns.renderers import RowsRenderer

data = {
    "today": [
        {"event": "Bought sausages"},
        {"event": "Bought bread"},
    ],
    "yesterday": [
        {"event": "Bought a train set"},
        {"event": "Bought a book"},
    ],
}

columns = Columns(ByKey())
columns.add("When", ByKey.key())

values = columns.group(ByKey.values())
values.add("Event", "event")

print(list(RowsRenderer(columns).render(data)))
[['When',      'Event'],
 ['today',     'Bought sausages'],
 ['today',     'Bought bread'],
 ['yesterday', 'Bought a train set'],
 ['yesterday', 'Bought a book']]