Skip to main content

A flexible filename generation library with various policies.

Project description

ufilename

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ufilename-0.1.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ufilename-0.1.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file ufilename-0.1.1.tar.gz.

File metadata

  • Download URL: ufilename-0.1.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ufilename-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0f496e00fc6a55fc9028fc60fbcad17d6547a8a8457066c4e5ecd60a1a49880d
MD5 9ca09b0938eef3cf4cdcf11caa1c1b0b
BLAKE2b-256 36eeedb163d6e8c76b323206184a140db4096a8bd8e0b44ab11bb027ef8b31be

See more details on using hashes here.

Provenance

The following attestation bundles were made for ufilename-0.1.1.tar.gz:

Publisher: python-app.yml on christoph2/filename

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ufilename-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ufilename-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ufilename-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5072a7a610032b1c5b5051eac459c35b51db987556596e40e24b50fcd4cc7039
MD5 afb7122e07c4e73f7c2e7a984b6e650a
BLAKE2b-256 3449c712075d3a07ea2caffdeaba215664d7ec51984c6a9f58b32714554a6a10

See more details on using hashes here.

Provenance

The following attestation bundles were made for ufilename-0.1.1-py3-none-any.whl:

Publisher: python-app.yml on christoph2/filename

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page