Name | fmtutil JSON |
Version |
1.0.12
JSON |
| download |
home_page | None |
Summary | Lightweight formatter objects |
upload_time | 2024-12-12 03:09:02 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.9.0 |
license | MIT |
keywords |
formatter
utility
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Formatter
[![test](https://github.com/korawica/fmtutil/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/korawica/fmtutil/actions/workflows/tests.yml)
[![codecov](https://codecov.io/gh/korawica/fmtutil/branch/main/graph/badge.svg?token=J2MN63IFT0)](https://codecov.io/gh/korawica/fmtutil)
[![pypi version](https://img.shields.io/pypi/v/fmtutil?logo=pypi&logoColor=white&label=pypi)](https://pypi.org/project/fmtutil/)
[![python support version](https://img.shields.io/pypi/pyversions/fmtutil?logo=pypi&logoColor=white)](https://pypi.org/project/fmtutil/)
[![size](https://img.shields.io/github/languages/code-size/korawica/fmtutil?logo=webpack&logoColor=white)](https://github.com/korawica/fmtutil)
[![gh license](https://img.shields.io/github/license/ddeutils/ddeutil-workflow)](https://github.com/ddeutils/ddeutil-workflow/blob/main/LICENSE)
[![code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![type check: mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/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
```shell
pip install -U fmtutil[all]
```
**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`.
## :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,
```python
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:;
```text
{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,
> ```python
> 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.
```python
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'])
```
```text
>>> <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.
```python
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:](https://github.com/korawica/fmtutil/issues)
for fix bug or request new feature if you want it.
Raw data
{
"_id": null,
"home_page": null,
"name": "fmtutil",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9.0",
"maintainer_email": null,
"keywords": "formatter, utility",
"author": null,
"author_email": "korawica <korawich.anu@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/21/1e/dca675b49fa7c7236c462403274e4eea00b24c9109ec41d86583d90f7021/fmtutil-1.0.12.tar.gz",
"platform": null,
"description": "# Formatter\n\n[![test](https://github.com/korawica/fmtutil/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/korawica/fmtutil/actions/workflows/tests.yml)\n[![codecov](https://codecov.io/gh/korawica/fmtutil/branch/main/graph/badge.svg?token=J2MN63IFT0)](https://codecov.io/gh/korawica/fmtutil)\n[![pypi version](https://img.shields.io/pypi/v/fmtutil?logo=pypi&logoColor=white&label=pypi)](https://pypi.org/project/fmtutil/)\n[![python support version](https://img.shields.io/pypi/pyversions/fmtutil?logo=pypi&logoColor=white)](https://pypi.org/project/fmtutil/)\n[![size](https://img.shields.io/github/languages/code-size/korawica/fmtutil?logo=webpack&logoColor=white)](https://github.com/korawica/fmtutil)\n[![gh license](https://img.shields.io/github/license/ddeutils/ddeutil-workflow)](https://github.com/ddeutils/ddeutil-workflow/blob/main/LICENSE)\n[![code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![type check: mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org)\n[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)\n\nLightweight formatter objects, this fmtutil package was created for `parse`\nand `format` any string values that match a format pattern which created base on\nPython regular expression.\n\n:dart: First objective of this project is include necessary formatter objects for\nany data components package which mean we can `parse` any complicate names on\ndata source and ingest the right names to in-house or data target.\n\n## :round_pushpin: Installation\n\n```shell\npip install -U fmtutil[all]\n```\n\n**Python version supported**:\n\n| Python Version | Installation | Support Fixed Bug |\n|----------------|-------------------------------------|--------------------|\n| `== 3.8` | `pip install \"fmtutil>=0.4,<0.5.0\"` | :x: |\n| `>=3.9,<3.14` | `pip install -U fmtutil` | :heavy_check_mark: |\n\n> [!NOTE]\n> This package has one dependency package, `python-dateutil`, this package use\n> for support add and sub datetime value on the Datetime formatter only.\n> If you do not want to install this package, you can use `pip install -U fmtutil`.\n\n## :beers: Introduction\n\nFor example, we want to get filename with the format like, `filename_20220101.csv`,\non the file system storage, and we want to incremental ingest the latest file with\ndate **2022-03-25** date. So we will implement `Datetime` object and parse\nthat filename to it,\n\n```python\nassert (\n Datetime.parse('filename_20220101.csv', 'filename_%Y%m%d.csv').value\n == datetime.datetime(2022, 1, 1, 0)\n)\n```\n\nThe above example is :yawning_face: **NOT SURPRISE!!!** for you right?\nBecause the Python already provide the build-in `datetime` to parse by `datetime.strptime`\nand format by `{dt}.strftime` :banana:.\n\nThis package will be the special thing when we group more than one format-able\nobjects together as `Naming`, `Version`, and `Datetime`.\nFor a complex filename format like :triumph:;\n\n```text\n{filename:%s}_{datetime:%Y_%m_%d}.{version:%m.%n.%c}.csv\n```\n\n> [!WARNING]\n> **Disclaimer**: The above filename format, the `datetime` package that already\n> build-in in Python does not enough for this scenario :snake: but you can handle by your\n> code function or create the better package than this project :dash:.\n\n> [!NOTE]\n> Any formatter object was implemented the `self.valid` method for help us validate\n> format string value like the above the example scenario,\n> ```python\n> this_date = Datetime.parse('20220101', '%Y%m%d')\n> assert this_date.valid('any_files_20220101.csv', 'any_files_%Y%m%d.csv')\n> ```\n\n## :tada: Usage\n\nIf you have multi-format filenames on the data source directory, and you want to\ndynamic getting max datetime on these filenames to your app, you can use a\nformatter group.\n\n```python\nfrom fmtutil import (\n make_group, Naming, Datetime, FormatterGroup, FormatterGroupType, FormatterArgumentError,\n)\n\nname: Naming = Naming.parse('Google Map', fmt='%t')\n\nfmt_group: FormatterGroupType = make_group({\n \"naming\": name.to_const(),\n \"timestamp\": Datetime,\n})\n\nrs: list[FormatterGroup] = []\nfor file in (\n 'googleMap_20230101.json',\n 'googleMap_20230103.json',\n 'googleMap_20230103_bk.json',\n 'googleMap_with_usage_20230105.json',\n 'googleDrive_with_usage_20230105.json',\n):\n try:\n rs.append(\n fmt_group.parse(file, fmt=r'{naming:c}_{timestamp:%Y%m%d}\\.json')\n )\n except FormatterArgumentError:\n continue\n\nrepr(max(rs).groups['timestamp'])\n```\n\n```text\n>>> <Datetime.parse('2023-01-03 00:00:00.000000', '%Y-%m-%d %H:%M:%S.%f')>\n```\n\n> [!TIP]\n> The above **Example** will convert the `name`, **Naming** instance, to **Constant**\n> instance before passing to the **Formatter Group** because it does not want\n> to dynamic parsing this format when find any matching filenames at destination\n> path.\n\n## :dart: Next Step\n\nI will change formatter object construction from changing with inside method to\nassert design. The code already implement and testing stage at file `__assets.py`.\n\nThat mean, you can create any formatter object by dynamic asset changed strategy.\n\n```python\nclass Datetime(Formatter, asset=DATETIME_ASSET, config=DATETIME_CONF, level=10):\n \"\"\"Datetime Formatter object.\"\"\"\n ...\n```\n\n## :speech_balloon: Contribute\n\nI do not think this project will go around the world because it has specific propose\nand you can create by your coding without this project dependency for long term\nsolution. So, on this time, you can open [the GitHub issue on this project :raised_hands:](https://github.com/korawica/fmtutil/issues)\nfor fix bug or request new feature if you want it.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Lightweight formatter objects",
"version": "1.0.12",
"project_urls": {
"Homepage": "https://github.com/korawica/fmtutil/",
"Source Code": "https://github.com/korawica/fmtutil/"
},
"split_keywords": [
"formatter",
" utility"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4033dec5b4c9f52989bf25e0f75fb61eee69368ecddef18a969819d0bf5de749",
"md5": "1f747ec8dc396c944f2b0a45da9d09de",
"sha256": "be9986ab4e1565b6a051076d25c4caf0ba0b6ca22e3f7c88663d9118554f45e1"
},
"downloads": -1,
"filename": "fmtutil-1.0.12-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1f747ec8dc396c944f2b0a45da9d09de",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9.0",
"size": 43870,
"upload_time": "2024-12-12T03:08:59",
"upload_time_iso_8601": "2024-12-12T03:08:59.976845Z",
"url": "https://files.pythonhosted.org/packages/40/33/dec5b4c9f52989bf25e0f75fb61eee69368ecddef18a969819d0bf5de749/fmtutil-1.0.12-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "211edca675b49fa7c7236c462403274e4eea00b24c9109ec41d86583d90f7021",
"md5": "c1541bacafdfffe5da97627deb8469fc",
"sha256": "277bc3498aff2333d883b6e149552e11dba9198b279d7a71e25edfb2a9d9bdf1"
},
"downloads": -1,
"filename": "fmtutil-1.0.12.tar.gz",
"has_sig": false,
"md5_digest": "c1541bacafdfffe5da97627deb8469fc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9.0",
"size": 43335,
"upload_time": "2024-12-12T03:09:02",
"upload_time_iso_8601": "2024-12-12T03:09:02.289856Z",
"url": "https://files.pythonhosted.org/packages/21/1e/dca675b49fa7c7236c462403274e4eea00b24c9109ec41d86583d90f7021/fmtutil-1.0.12.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-12 03:09:02",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "korawica",
"github_project": "fmtutil",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fmtutil"
}