Skip to main content

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.

Simply a more convenient wrapper around humanize, humanfriendly, and strif.

Installation

# Use pip
pip install prettyfmt
# Or poetry
poetry add prettyfmt

Usage

from prettyfmt import *

abbrev_str("very " * 100 + "long", 32)
🢂 'very very very very very very v…'

# 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 but don't break words. Combine with slugifiers.
abbrev_on_words("very " * 100 + "long", 30)
🢂 'very very very very very very…'

# My favorite, very good for abbreviating a long title to get a shorter one,
# or good filename.
abbrev_phrase_in_middle("very " * 100 + "long", 40)
🢂 'very very very v

# Useful for cleaning up document titles and filenames.
ugly_title = "A  Very\tVery 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'

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.
fmt_age(60 * 60 * 24 * 23)
🢂 '3 weeks and 2 days ago'

fmt_age(60 * 60 *24 * 23, brief=True)
🢂 '3w 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.
            key_filter={
               "title": 64,
               "file_path": 0,
               "url": 128,
            },
      )

See pydoc 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

prettyfmt-0.1.2.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

prettyfmt-0.1.2-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file prettyfmt-0.1.2.tar.gz.

File metadata

  • Download URL: prettyfmt-0.1.2.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for prettyfmt-0.1.2.tar.gz
Algorithm Hash digest
SHA256 aef02800df2710f9b39527c3089a088aebed182be566b2206a2a498baa21e20f
MD5 82e3612d24d00041626e6a1f3d5013a9
BLAKE2b-256 acc2616fa3c2319659a8db77942e23831b1f2f5ba66f992f3cd6e4b6f44a0d92

See more details on using hashes here.

Provenance

The following attestation bundles were made for prettyfmt-0.1.2.tar.gz:

Publisher: publish.yml on jlevy/prettyfmt

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

File details

Details for the file prettyfmt-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: prettyfmt-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for prettyfmt-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 31947af735d37f599f8a1b0eb930651b6d0e9584dcda0a42ec86b4ae00d5e2b0
MD5 c3a40f4708025bc95d26679f053a6a1a
BLAKE2b-256 499b1078b7f79ef22a09653a292855f126f2768da8218d12fbc669fa59e91ca3

See more details on using hashes here.

Provenance

The following attestation bundles were made for prettyfmt-0.1.2-py3-none-any.whl:

Publisher: publish.yml on jlevy/prettyfmt

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