Skip to main content

A pattern matching and template library.

Project description

Matcho

A pattern matching and template library.

Matcho was originally written by a need to convert hierarchical JSON data into flattish data frames. It may yet transcend this purpose.

Installation

pip install matcho

Quick Start

from matcho import build_matcher, build_template, bind, insert

# match a list of any length and bind "x" to its items
matcher = build_matcher([bind("x"), ...])

# match some data
bindings = matcher([1, 2, 3])

# a template that reconstructs the original list
template = build_template([insert("x"), ...])

assert template(bindings) == [1, 2, 3]

Motivating example

What if you want to convert data from a deeply nested structure like JSON to a flat tabular format?

For example, say we want to extract the columns "date", "time", "station" and "event_type" from the following structure:

data = {
    "date": "2022-02-20",
    "uid": "DEADBEEF",
    "reports": [
        {
            "station": 7,
            "events": [
                {"time": 1300, "type": "ON"},
                {"time": 1700, "type": "OFF"}
            ]
        },
        {
            "station": 5,
            "events": [
                {"time": 1100, "type": "ON"},
                {"time": 1800, "type": "OFF"}
            ]
        }
    ]
}

That's how a Matcho would do it:

from matcho import build_matcher, build_template, bind, insert

pattern = {
        "date": bind("date"),
        "reports": [
            {
                "station": bind("station"),
                "events": [{"time": bind("time"), "type": bind("event_type")}, ...],
            },
            ...,  # note that the ... really are Python syntax
        ],
    }

template_spec = [
        [insert("date"), insert("time"), insert("station"), insert("event_type")],
        ...,
        ...,  # note that the number of ... matches the pattern
    ]

matcher = build_matcher(pattern)
bindings = matcher(data)

template = build_template(template_spec)
table = template(bindings)

assert table == [
    ["2022-02-20", 1300, 7, "ON"],
    ["2022-02-20", 1700, 7, "OFF"],
    ["2022-02-20", 1100, 5, "ON"],
    ["2022-02-20", 1800, 5, "OFF"],
]

Inspiration

Matcho was inspired by Scheme's syntax-rules pattern language. The Lisp dialect Scheme allows programmers to define macros using pattern matching and template substitution. Since code in Scheme consists of list this enables cool syntax transformations. In Python we are limited to transforming data, but that's cool enough.

Why not just use Python 3.10's match syntax instead?

The new match syntax is great and it's even used by the implementation of Macho. However, it has one shortcoming: names can only capture one value. While it's possible to match an arbitary number of list items with [*items], it's not possible to do something like [*{"nested": item}], where we would like to capture the values of in a sequence of dictionaries. In Matcho, this is possible with a pattern of the form [{"nested": item}, ...].

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

matcho-0.2.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

matcho-0.2.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file matcho-0.2.1.tar.gz.

File metadata

  • Download URL: matcho-0.2.1.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for matcho-0.2.1.tar.gz
Algorithm Hash digest
SHA256 372a91239e23980e5049479f37374e3cf40e662d78a3ad435eaf9240330df8b7
MD5 33d345fe5a343e2b9549e7877659273d
BLAKE2b-256 1ddebcae725fd7e3464ac5ec87c7c9903cce0c96e5f088efbd7f11bfa32ea794

See more details on using hashes here.

File details

Details for the file matcho-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: matcho-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for matcho-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5cf12ff4ea33b9418e079b7f8204fed1939ebb18189487d1893888f934bcc376
MD5 69742b5163983c74c9497d3663c7ba58
BLAKE2b-256 2ecb0732d29bca0a83d00e71a694ed33320666a81e2f005e53460b40898d967e

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