ufilename
A friendly, lightweight Python library for generating consistent, collision‑resistant filenames through composable policies. It is designed to keep your naming rules explicit, testable, and easy to evolve as your project grows.
Why ufilename?
- Composable policies: Combine multiple strategies without duplicating logic.
- Predictable outputs: Generate filenames deterministically when you need repeatability.
- Practical defaults: Built‑in policies cover common use cases like timestamps, UUIDs, and incremental numbering.
- Minimal surface area: One simple helper to build filenames in a clean, consistent way.
Installation
pip install ufilename
Quick Start
from ufilename import build_filename, TimestampPolicy
policy = TimestampPolicy()
filename = build_filename(policy, "report", ".pdf")
print(filename) # e.g., report_20231027_123456.pdf
Core Concepts
ufilename separates what you want to name from how you want to name it:
- Base name: the human‑readable portion (e.g.,
report). - Extension: the file type (e.g.,
.pdf). - Policy: a strategy that transforms the base name (e.g., add a timestamp or UUID).
Policies operate on a context object that can optionally include a target directory and metadata. This makes it easy to add logic like “find the next free filename in this folder” or “hash a specific metadata field.”
Available Policies
IdentityPolicy: Returns the base name unchanged.PrefixPolicy: Prepends a prefix to the base name.SuffixPolicy: Appends a suffix to the base name.RandomHexPolicy: Appends a random hex token.TimestampPolicy: Appends a timestamp.IncrementPolicy: Finds the next available numbered file in a directory.UUIDPolicy: Appends a UUID.HostnamePolicy: Appends the system hostname.MetadataHashPolicy: Appends a hash of specific metadata.CompositePolicy: Chains multiple policies in order.
Examples
Prefix + Timestamp
from ufilename import build_filename, CompositePolicy, PrefixPolicy, TimestampPolicy
policy = CompositePolicy([
PrefixPolicy("daily_"),
TimestampPolicy("%Y%m%d"),
])
name = build_filename(policy, "report", ".csv")
print(name) # daily_report_20240217.csv
Increment in a Directory
from pathlib import Path
from ufilename import build_filename, IncrementPolicy
policy = IncrementPolicy(width=3, start=1)
name = build_filename(policy, "backup", ".zip", directory=Path("./backups"))
print(name) # backup_001.zip (if not taken yet)
Metadata Hash for Reproducibility
from ufilename import build_filename, MetadataHashPolicy
policy = MetadataHashPolicy(key="params", length=8)
name = build_filename(policy, "experiment", ".json", metadata={"params": {"lr": 0.01}})
print(name) # experiment_1a2b3c4d.json
Serialization
Policies can be serialized to dictionaries and restored later. This is handy when you want to store configuration in JSON/YAML or pass it between services.
from ufilename import policy_from_dict, TimestampPolicy
data = {"type": "timestamp", "fmt": "%Y"}
policy = policy_from_dict(data)
assert isinstance(policy, TimestampPolicy)
Contributing
Contributions are welcome! If you have ideas for additional policies or improvements, feel free to open an issue or submit a pull request.
License
This project is licensed under the BSD 2‑Clause "Simplified" License. See the LICENSE file for details.
Release files for ufilename 0.1.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 | |
|---|---|---|---|
| ufilename-0.1.1.tar.gz | 7.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ufilename-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.9 kB
Release files / ufilename-0.1.1.tar.gz
| Download URL | ufilename-0.1.1.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0f496e00fc6a55fc9028fc60fbcad17d6547a8a8457066c4e5ecd60a1a49880d
|
|
BLAKE2b-256 checksum How to use checksums |
36eeedb163d6e8c76b323206184a140db4096a8bd8e0b44ab11bb027ef8b31be
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 17, 2026.
Transparency logRelease files / ufilename-0.1.1-py3-none-any.whl
| Download URL | ufilename-0.1.1-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5072a7a610032b1c5b5051eac459c35b51db987556596e40e24b50fcd4cc7039
|
|
BLAKE2b-256 checksum How to use checksums |
3449c712075d3a07ea2caffdeaba215664d7ec51984c6a9f58b32714554a6a10
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 17, 2026.
Transparency log