Skip to main content

A lightweight library for tabulating dictionaries

Project description

unpact

A lightweight library for tabulating dictionaries.

Coverage

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


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.18.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unpact-0.0.18-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file unpact-0.0.18.tar.gz.

File metadata

  • Download URL: unpact-0.0.18.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

Hashes for unpact-0.0.18.tar.gz
Algorithm Hash digest
SHA256 e265c6c479afd75e8e50efd6994a6e7e1aabedcbef7001e5661e44bff1fcc9fd
MD5 23749e9e27fa6668c2494511fdd89cca
BLAKE2b-256 4c2a9a21731de6d65d4da938fbaa2e1ed90774c9fd31d4d667626b37b2c608ef

See more details on using hashes here.

File details

Details for the file unpact-0.0.18-py3-none-any.whl.

File metadata

  • Download URL: unpact-0.0.18-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

Hashes for unpact-0.0.18-py3-none-any.whl
Algorithm Hash digest
SHA256 24ffd20c157c48c3e6512c087f82fae1ccd6c798af0c194ceaf3e88095638e8a
MD5 3b48a739921fd16c3988f581070b0076
BLAKE2b-256 8cc3e06fea57a1290cbf950ac4e637f6af44b1e2cd17e771eda191cc41377c6c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page