ej
String generation
To install: pip install ej
Overview
The ej package provides advanced string formatting capabilities that extend Python's built-in string formatting methods. It includes functionality for partial and incremental formatting, transforming format strings, and applying transformation pipelines directly within format specifications.
Features
- Partial Formatting: Allows string templates to be formatted even when not all placeholders are provided values. Unprovided fields remain as placeholders in the output.
- Incremental Formatting: Supports formatting strings in multiple passes, which is useful for configurations or templates where some values depend on others.
- Format String Transformation: Functions to transform format strings by auto-generating field names or converting them to use automatic field numbering.
- Pipeline Formatting: A powerful feature where values can be processed through a series of functions directly specified in the format string.
Usage Examples
Partial Formatting
You can use PartialFormatter to handle cases where not all template fields are provided. It's useful for building strings step-by-step in applications such as configuration files or complex outputs where some values are optional:
from ej import partial_formatter
template = "Hello, {name}! Your balance is {balance}."
formatted = partial_formatter.format(template, name="Alice")
print(formatted) # Output: "Hello, Alice! Your balance is {balance}."
Incremental Formatting
This feature allows for strings to be formatted in stages, which is useful when some values depend on others:
from ej import format_str_vals_of_dict
data = {
'path': '{base_path}/images/{image_id}.jpg',
'base_path': '/user/data',
'image_id': '12345'
}
formatted_data = format_str_vals_of_dict(data)
print(formatted_data['path']) # Output: '/user/data/images/12345.jpg'
Transforming Format Strings
Convert a format string to use automatic field numbering or name fields explicitly:
from ej import auto_field_format_str, name_fields_in_format_str
format_str = "User: {0}, ID: {1}"
auto_format = auto_field_format_str(format_str)
print(auto_format) # Output: "User: {}, ID: {}"
named_format = name_fields_in_format_str("Hello, {0}!", ["name"])
print(named_format) # Output: "Hello, {name}!"
Pipeline Formatting
Apply a series of transformations to a value directly within the format string:
from ej import PipelineTemplate
pipeline = PipelineTemplate(
double=lambda x: x * 2,
increment=lambda x: x + 1
)
result = pipeline.format("{value:double|increment}", value=3)
print(result) # Output: '7'
Documentation
Classes and Functions
PartialFormatter: A formatter that allows partial formatting of strings.format_str_vals_of_dict: Formats the string values of a dictionary, handling nested formatting needs.compile_str_from_parsed: Reconstructs a format string from parsed components.transform_format_str: Transforms a format string using a specified transformation function.match_format_string: Matches a string against a format string and extracts values.PipelineTemplate: A formatter that applies a sequence of transformations to format string fields.
These utilities make the ej package a robust tool for handling complex string formatting scenarios in Python applications.
Metadata
Release files for ej 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ej-0.0.5.tar.gz | 12.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ej-0.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.3 kB
Release files / ej-0.0.5.tar.gz
| Download URL | ej-0.0.5.tar.gz |
|---|---|
| Size | 12.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6e339d15b7e96e6af06c2829c9398deffc89fbaaf24da2479e9e8fd08c59033b
|
|
BLAKE2b-256 checksum How to use checksums |
e90990d0e0522490207fc6cca66efe6547e93cd868642d45ac3fd905018a7f24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / ej-0.0.5-py3-none-any.whl
| Download URL | ej-0.0.5-py3-none-any.whl |
|---|---|
| Size | 12.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a1bdcf6697d87c120cbef3fee8e7223825641159bc3c7257276450c3055018d1
|
|
BLAKE2b-256 checksum How to use checksums |
f0f94658a7a6288b155769d288295731f0e399214c22236cfd839c41ec483fce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|