A tiny library to make your outputs more beautiful
Project description
prettyfmt
prettyfmt is a tiny library to make your outputs, logs, and __str__()
representations slightly more beautiful.
It offers simple but general functions for formatting and abbreviating objects and dataclasses, dicts, words and phrases, filenames, titles, long strings, timestamps, ages, and sizes.
It won't bloat your project.
It's <500 lines of code with just two dependencies:
humanize and
strif.
Installation
# Use pip
pip install prettyfmt
# Or poetry
poetry add prettyfmt
Usage
See pydoc for details on all functions.
from prettyfmt import *
# Simple abbreviations of objects:
abbrev_obj({"a": "very " * 100 + "long", "b": 23})
🢂 "{a='very very very very very very very very very very very very ver…', b=23}"
abbrev_obj(["word " * i for i in range(10)], field_max_len=10, list_max_len=4)
🢂 "['', 'word ', 'word word ', 'word word…', …]"
# Abbreviate by character length.
abbrev_str("very " * 100 + "long", 32)
🢂 'very very very very very very v…'
# Abbreviate by character length but don't break words.
abbrev_on_words("very " * 100 + "long", 30)
🢂 'very very very very very very…'
# My favorite, abbreviate but don't break words and keep a few words
# on the end since they might be useful.
abbrev_phrase_in_middle("very " * 100 + "long", 40)
🢂 'very very very very … very very very long'
# This makes it very handy for cleaning up document titles.
ugly_title = "A Very\tVery Very Needlessly Long {Strange} Document Title [final edited draft23]"
🢂 sanitize_title(ugly_title)
'A Very Very Very Needlessly Long Strange Document Title final edited draft23'
abbrev_phrase_in_middle(sanitize_title(ugly_title))
🢂 'A Very Very Very Needlessly Long Strange … final edited draft23'
# Then you can slugify to get nice filenames or URLs.
from slugify import slugify
slugify(abbrev_phrase_in_middle(sanitize_title(ugly_title)))
🢂 'a-very-very-very-needlessly-long-strange-final-edited-draft23'
# Ages in seconds or deltas.
# Note we use a sensible single numeral to keep things brief, e.g.
# "33 days ago" and not the messier "1 month and 3 days ago".
# This is important in file listings, etc, where we want to optimize
# for space and legibility.
fmt_age(60 * 60 * 24 * 33)
🢂 '33 days ago'
fmt_age(60 * 60 * 24 * 33, brief=True)
🢂 '33d ago'
# Sizes
fmt_size_human(12000000)
🢂 '11.4M'
fmt_size_dual(12000000)
🢂 '11.4M (12000000 bytes)'
# Helpful making __str__() methods or printing output:
fmt_words("Hello", None, "", "world!")
🢂 'Hello world!'
fmt_paras(fmt_words("Hello", "world!"), "", "Goodbye.")
🢂 'Hello world!\n\nGoodbye.'
# Example of `abbrev_obj` to customize __str__().
# Allows sorting and truncating based on key and value.
@dataclass
class MyThing:
file_path: Path
title: str
url: str
body: str
def __str__(self) -> str:
return abbrev_obj(
self,
# Put an abbreviated title first, then the file path, then the url.
# The `body` field will be omitted.
key_filter={
"title": 64,
"file_path": 0,
"url": 128,
},
)
This project was built from simple-modern-poetry.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file prettyfmt-0.1.7.tar.gz.
File metadata
- Download URL: prettyfmt-0.1.7.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b92317b932f46a2c937708294b79938efd9fd8964a85acd7b12a70936a306fc
|
|
| MD5 |
4f8d8fd30f242aa166e89b902ebab4d3
|
|
| BLAKE2b-256 |
5e3c357a07734844324467cbc0ce239761977acf241762b51fec31fb95ee0f94
|
Provenance
The following attestation bundles were made for prettyfmt-0.1.7.tar.gz:
Publisher:
publish.yml on jlevy/prettyfmt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
prettyfmt-0.1.7.tar.gz -
Subject digest:
9b92317b932f46a2c937708294b79938efd9fd8964a85acd7b12a70936a306fc - Sigstore transparency entry: 173625650
- Sigstore integration time:
-
Permalink:
jlevy/prettyfmt@9ad8c03214da8a88de58ec0d6386da880e03b80e -
Branch / Tag:
refs/tags/v0.1.7 - Owner: https://github.com/jlevy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9ad8c03214da8a88de58ec0d6386da880e03b80e -
Trigger Event:
release
-
Statement type:
File details
Details for the file prettyfmt-0.1.7-py3-none-any.whl.
File metadata
- Download URL: prettyfmt-0.1.7-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f911188eaf6154ed3a04520a23e1a129cdb61eb094f10ce2dcf49d7c31f15876
|
|
| MD5 |
0078d3f0e5df6b76a9b494832276a5cb
|
|
| BLAKE2b-256 |
5a4d790a3d867f0fe98550e379ec6f1ecf73c5ae26fbdd978aa96bb26eac1c82
|
Provenance
The following attestation bundles were made for prettyfmt-0.1.7-py3-none-any.whl:
Publisher:
publish.yml on jlevy/prettyfmt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
prettyfmt-0.1.7-py3-none-any.whl -
Subject digest:
f911188eaf6154ed3a04520a23e1a129cdb61eb094f10ce2dcf49d7c31f15876 - Sigstore transparency entry: 173625652
- Sigstore integration time:
-
Permalink:
jlevy/prettyfmt@9ad8c03214da8a88de58ec0d6386da880e03b80e -
Branch / Tag:
refs/tags/v0.1.7 - Owner: https://github.com/jlevy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9ad8c03214da8a88de58ec0d6386da880e03b80e -
Trigger Event:
release
-
Statement type: