Skip to main content

Formatter

test codecov pypi version python support version size gh license code style: black type check: mypy pre-commit

Lightweight formatter objects, this fmtutil package was created for parse and format any string values that match a format pattern which created base on Python regular expression.

:dart: First objective of this project is include necessary formatter objects for any data components package which mean we can parse any complicate names on data source and ingest the right names to in-house or data target.

:round_pushpin: Installation

pip install -U fmtutil

Python version supported:

Python Version Installation Support Fixed Bug
== 3.8 pip install "fmtutil>=0.4,<0.5.0" :x:
>=3.9,<3.14 pip install -U fmtutil :heavy_check_mark:

[!NOTE] This package has one dependency package, python-dateutil, this package use for support add and sub datetime value on the Datetime formatter only. If you do not want to install this package, you can use pip install -U fmtutil.

[!NOTE] The Datetime formatter able to compare with the relativedelta object if you already installed python-dateutil package.

:beers: Introduction

For example, we want to get filename with the format like, filename_20220101.csv, on the file system storage, and we want to incremental ingest the latest file with date 2022-03-25 date. So we will implement Datetime object and parse that filename to it,

assert (
    Datetime.parse('filename_20220101.csv', 'filename_%Y%m%d.csv').value
    == datetime.datetime(2022, 1, 1, 0)
)

The above example is :yawning_face: NOT SURPRISE!!! for you right? Because the Python already provide the build-in datetime to parse by datetime.strptime and format by {dt}.strftime :banana:.

This package will be the special thing when we group more than one format-able objects together as Naming, Version, and Datetime. For a complex filename format like :triumph:;

{filename:%s}_{datetime:%Y_%m_%d}.{version:%m.%n.%c}.csv

[!WARNING] Disclaimer: The above filename format, the datetime package that already build-in in Python does not enough for this scenario :snake: but you can handle by your code function or create the better package than this project :dash:.

[!NOTE] Any formatter object was implemented the self.valid method for help us validate format string value like the above the example scenario,

this_date = Datetime.parse('20220101', '%Y%m%d')
assert this_date.valid('any_files_20220101.csv', 'any_files_%Y%m%d.csv')

:tada: Usage

If you have multi-format filenames on the data source directory, and you want to dynamic getting max datetime on these filenames to your app, you can use a formatter group.

from fmtutil import (
  make_group, Naming, Datetime, FormatterGroup, FormatterGroupType, FormatterArgumentError,
)

name: Naming = Naming.parse('Google Map', fmt='%t')

fmt_group: FormatterGroupType = make_group({
    "naming": name.to_const(),
    "timestamp": Datetime,
})

rs: list[FormatterGroup] = []
for file in (
    'googleMap_20230101.json',
    'googleMap_20230103.json',
    'googleMap_20230103_bk.json',
    'googleMap_with_usage_20230105.json',
    'googleDrive_with_usage_20230105.json',
):
    try:
        rs.append(
            fmt_group.parse(file, fmt=r'{naming:c}_{timestamp:%Y%m%d}\.json')
        )
    except FormatterArgumentError:
        continue

repr(max(rs).groups['timestamp'])
>>> <Datetime.parse('2023-01-03 00:00:00.000000', '%Y-%m-%d %H:%M:%S.%f')>

[!TIP] The above Example will convert the name, Naming instance, to Constant instance before passing to the Formatter Group because it does not want to dynamic parsing this format when find any matching filenames at destination path.

:dart: Next Step

I will change formatter object construction from changing with inside method to assert design. The code already implement and testing stage at file __assets.py.

That mean, you can create any formatter object by dynamic asset changed strategy.

class Datetime(Formatter, asset=DATETIME_ASSET, config=DATETIME_CONF, level=10):
    """Datetime Formatter object."""
    ...

:speech_balloon: Contribute

I do not think this project will go around the world because it has specific propose and you can create by your coding without this project dependency for long term solution. So, on this time, you can open the GitHub issue on this project :raised_hands: for fix bug or request new feature if you want it.

Release files for fmtutil 1.0.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fmtutil 1.0.15
File Size Uploaded
fmtutil-1.0.15.tar.gz 43.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fmtutil 1.0.15
File Interpreter ABI Platform
fmtutil-1.0.15-py3-none-any.whl Python 3 none any Details

Total release size: 87.5 kB

Release files / fmtutil-1.0.15.tar.gz

Download URL fmtutil-1.0.15.tar.gz
Size 43.5 kB
Tags Source
SHA-256 checksum
How to use checksums
0072e202b37a1e86f40b881a1e44b2e69fa611814020ff0abcc59676cb94389a
BLAKE2b-256 checksum
How to use checksums
8dade48ff6eb6ff88b19beb50f2336b39fa65b375554a96f1d1c8e25dd22a2ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 19, 2025.

Transparency log

Release files / fmtutil-1.0.15-py3-none-any.whl

Download URL fmtutil-1.0.15-py3-none-any.whl
Size 44.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
061edee8b5d819ca998de4fe9174bb2fcaef028ed946df8fae7c02321f03c210
BLAKE2b-256 checksum
How to use checksums
627b0b0132d9739a34cb37e750aadf10a64ed96deb5898d133aff45fae955361
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 19, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.15 This release

2 release files

1.0.14

2 release files

1.0.13

2 release files

1.0.12

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page