A lightweight library for tabulating dictionaries
Project description
unpact
A lightweight library for tabulating dictionaries.
Usage
A basic example:
from unpact import unwind, ColumnDef
columns: List[ColumnDef] = [
'calendar.year',
'calendar.date',
'locations.location',
'locations.x',
'locations.y'
]
# columns of the same child and the same length are considered 'adjacent'
# adjacent columns are zipped together.
# here, 'x' and 'y' are considered 'adjacent'
data = {
'calendar': {'year': 2022, 'date': 'Aug 14'},
'locations': [
{'location': 'Loc1', 'x': [1,2,3,4], 'y': [1,2,3,4]},
{'location': 'Loc2', 'x': [11,22,33,44], 'y': [11,22,33,44]},
{'location': 'Loc3', 'x': [11], 'y': [11]},
],
'ignored': "This isn't in the ColumDefs so won't be included"
}
table = unwind(data, columns)
print(pl.from_dicts(table))
--
shape: (9, 5)
┌──────┬────────┬──────────┬─────┬─────┐
│ year ┆ date ┆ location ┆ x ┆ y │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str ┆ i64 ┆ i64 │
╞══════╪════════╪══════════╪═════╪═════╡
│ 2022 ┆ Aug 14 ┆ Loc1 ┆ 1 ┆ 1 │
│ 2022 ┆ Aug 14 ┆ Loc1 ┆ 2 ┆ 2 │
│ 2022 ┆ Aug 14 ┆ Loc1 ┆ 3 ┆ 3 │
│ 2022 ┆ Aug 14 ┆ Loc1 ┆ 4 ┆ 4 │
│ 2022 ┆ Aug 14 ┆ Loc2 ┆ 11 ┆ 11 │
│ 2022 ┆ Aug 14 ┆ Loc2 ┆ 22 ┆ 22 │
│ 2022 ┆ Aug 14 ┆ Loc2 ┆ 33 ┆ 33 │
│ 2022 ┆ Aug 14 ┆ Loc2 ┆ 44 ┆ 44 │
│ 2022 ┆ Aug 14 ┆ Loc2 ┆ 11 ┆ 11 │
└──────┴────────┴──────────┴─────┴─────┘
A more complex example using ColumnSpecs:
from typing import List
import polars as pl
from unpact import ColumnDef, ColumnSpec, unwind
def format_coordinate_pair(
coords: list[int], index: int | None
) -> dict: # Formatter functions must return a dictionary
# Terminal value is passed to the "formatter" function
# "index" is optionally injected if the value is a member of a list
return {"x": coords[0], "y": coords[1], "frame": index} if coords else {"x": None, "y": None, "frame": index}
# You can pass in a pass in a 'ColumnSpec' to change the behavior of a column
# current values are 'formatter' which accepts a callable and 'name', a string which will rename the column
columns: List[ColumnDef] = [
ColumnSpec(path="calendar.year", name="Year"), # You can rename the column using the optional `name` kwarg
ColumnSpec(path="calendar.date"), # Otherwise the column will be named after the last part of the path
ColumnSpec(path="locations.location", name="location name"),
ColumnSpec(path="locations.coords", formatter=lambda coords: {"x": coords[0], "y": coords[1]}),
ColumnSpec(path="locations.coords", formatter=format_coordinate_pair),
]
data = {
"calendar": {"year": 2022, "date": "Aug 14"},
"locations": [
{"location": "Loc1", "coords": [[1, 1], [2, 2], [3, 3]]},
{"location": "Loc2", "coords": [[1, 1], [2, 2], [3, 3]]},
{"location": "Loc3", "coords": [[1, 1], [2, 2], [3, 3]]},
],
"ignored": "This isn't in the ColumDefs so won't be included",
}
table = unwind(data, columns)
print(pl.from_dicts(table))
---
shape: (9, 6)
┌──────┬────────┬───────────────┬─────┬─────┬───────┐
│ Year ┆ date ┆ location name ┆ x ┆ y ┆ frame │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str ┆ i64 ┆ i64 ┆ i64 │
╞══════╪════════╪═══════════════╪═════╪═════╪═══════╡
│ 2022 ┆ Aug 14 ┆ Loc1 ┆ 1 ┆ 1 ┆ 0 │
│ 2022 ┆ Aug 14 ┆ Loc1 ┆ 2 ┆ 2 ┆ 1 │
│ 2022 ┆ Aug 14 ┆ Loc1 ┆ 3 ┆ 3 ┆ 2 │
│ 2022 ┆ Aug 14 ┆ Loc2 ┆ 1 ┆ 1 ┆ 0 │
│ 2022 ┆ Aug 14 ┆ Loc2 ┆ 2 ┆ 2 ┆ 1 │
│ 2022 ┆ Aug 14 ┆ Loc2 ┆ 3 ┆ 3 ┆ 2 │
│ 2022 ┆ Aug 14 ┆ Loc3 ┆ 1 ┆ 1 ┆ 0 │
│ 2022 ┆ Aug 14 ┆ Loc3 ┆ 2 ┆ 2 ┆ 1 │
│ 2022 ┆ Aug 14 ┆ Loc3 ┆ 3 ┆ 3 ┆ 2 │
└──────┴────────┴───────────────┴─────┴─────┴───────┘
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
unpact-0.0.17.tar.gz
(9.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
unpact-0.0.17-py3-none-any.whl
(11.2 kB
view details)
File details
Details for the file unpact-0.0.17.tar.gz.
File metadata
- Download URL: unpact-0.0.17.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.0 CPython/3.11.0 Darwin/21.0.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb757aa860280d9fc9f0f6b76d6e8ddc4557bd2d8f6890667446e7336641fa82
|
|
| MD5 |
fee5430689afd755aed489fb4275f72e
|
|
| BLAKE2b-256 |
0fdbd9fabfc28a488789776f240975779e79283126e0da044685205a0f8273eb
|
File details
Details for the file unpact-0.0.17-py3-none-any.whl.
File metadata
- Download URL: unpact-0.0.17-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.0 CPython/3.11.0 Darwin/21.0.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82426197aa2401a2f4b52645b8a8f896efc823e88bcbfdef21d3f637501c029e
|
|
| MD5 |
8c83538029a259f0a22209a14477b27b
|
|
| BLAKE2b-256 |
49889967a6d80bf9ba5874fedfdc62ceb0757449c4f4fa61ea988b35268f8fa5
|