FormatDefault
str.format but with default values
Ideal for building a CLI program with format-able filepath arguments
Many existing string formatting tools use regex to find sections like %(param)s, but regex can be fiddly and is not as flexible as str.format when it comes to validating format spec.
Why reinvent the wheel when one can just pass an fstring-looking argument and allow Python (and this mini library) to do the heavy lifting, no regex required!
Example Usage
from FormatDefault import format_default
s = "Hello {x:#02x} {y} {y:#02x} {z}"
params = {"x": 100, "y": "hi"}
print(format_default(s, params))
# Hello 0x64 hi NA NA
import argparse
from FormatDefault import format_default
parser = argparse.ArgumentParser()
parser.add_argument("-o",
default = "YouTube/{uploader}/{title} - {camera_type} - {id}.{ext}"
)
args = parser.parse_args()
video_data = {
"id": "jNQXAC9IVRw", "title": "Me at the zoo",
"uploader": "jawed", "ext": "webm",
}
print(format_default(args.o, video_data))
# YouTube/jawed/Me at the zoo - NA - jNQXAC9IVRw.webm
How it works
format_default(s, params, default = "NA") tries str.format in a while-loop with a copy of params.
format_default catches KeyErrors from missing parameters and the parameter name is added to params with the value default.
The values in params are wrapped in a wrapper class _DefaultWrapper before being passed to str.format. This wrapper class implements the __format__(self, fmt) dunder method to intercept the format specifiers (parts after the colon, eg "#02x" in "{foo = :#02x}").
_DefaultWrapper.__format__ catches ValueErrors from invalid format specifiers for its wrapped value and returns default in this case.
Metadata
Release files for FormatDefault 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| FormatDefault-1.0.1.tar.gz | 39.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| FormatDefault-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 67.0 kB
Release files / FormatDefault-1.0.1.tar.gz
| Download URL | FormatDefault-1.0.1.tar.gz |
|---|---|
| Size | 39.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ec1cbae190b50ed13d84d52127f654938d8e53eb5ad364a3e08c71be5a8ebda4
|
|
BLAKE2b-256 checksum How to use checksums |
9bd0d5a3f62b446e5af96863c23960be6b70f89874dcba424c86f67464ef9163
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / FormatDefault-1.0.1-py3-none-any.whl
| Download URL | FormatDefault-1.0.1-py3-none-any.whl |
|---|---|
| Size | 27.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
87c9a59a2c2f26727960585722d5d274b477d01ac9cf708a46ed6a8d2e032be2
|
|
BLAKE2b-256 checksum How to use checksums |
45425566c1a7ec0817131a6fb7eb92541e45e96c6b1a7a1e561ca363935507ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|