11# prettyfmt
22
3- ` prettyfmt ` is a tiny library to make your outputs, logs, and
4- ` __str__() ` representations slightly more beautiful.
3+ ` prettyfmt ` is a tiny library to make your outputs, logs, and ` __str__() `
4+ representations slightly more beautiful.
55
6- It offers simple but general functions for formatting and abbreviating
7- objects and dataclasses, dicts, words and phrases, filenames, titles, long
8- strings, timestamps, ages, and sizes.
6+ It offers simple but general functions for formatting and abbreviating objects and
7+ dataclasses, dicts, words and phrases, filenames, titles, long strings, timestamps,
8+ ages, and sizes.
99
10- Simply a more convenient wrapper around ` humanize ` , ` humanfriendly ` , and ` strif ` .
10+ Basically it's just a tiny set of convenience functions for
11+ [ ` humanize ` ] ( https://github.com/python-humanize/humanize ) ,
12+ [ ` humanfriendly ` ] ( https://github.com/xolox/python-humanfriendly ) , and
13+ [ ` strif ` ] ( https://github.com/jlevy/strif ) .
1114
1215## Installation
1316
@@ -20,33 +23,40 @@ poetry add prettyfmt
2023
2124## Usage
2225
26+ See [ pydoc] ( https://github.com/jlevy/prettyfmt/blob/main/src/prettyfmt/prettyfmt.py ) for
27+ details on all functions.
28+
2329``` python
2430from prettyfmt import *
2531
26- abbrev_str(" very " * 100 + " long" , 32 )
27- 🢂 ' very very very very very very v…'
28-
2932# Simple abbreviations of objects:
3033abbrev_obj({" a" : " very " * 100 + " long" , " b" : 23 })
3134🢂 " {a='very very very very very very very very very very very very ver…', b=23}"
3235
3336abbrev_obj([" word " * i for i in range (10 )], field_max_len = 10 , list_max_len = 4 )
3437🢂 " ['', 'word ', 'word word ', 'word word…', …]"
3538
36- # Abbreviate but don't break words. Combine with slugifiers.
39+ # Abbreviate by character length.
40+ abbrev_str(" very " * 100 + " long" , 32 )
41+ 🢂 ' very very very very very very v…'
42+
43+ # Abbreviate by character length but don't break words.
3744abbrev_on_words(" very " * 100 + " long" , 30 )
3845🢂 ' very very very very very very…'
3946
40- # My favorite, very good for abbreviating a long title to get a shorter one,
41- # or good filename .
47+ # My favorite, abbreviate but don't break words and keep a few words
48+ # on the end since they might be useful .
4249abbrev_phrase_in_middle(" very " * 100 + " long" , 40 )
43- 🢂 ' very very very v
50+ 🢂 ' very very very very … very very very long '
4451
45- # Useful for cleaning up document titles and filenames .
52+ # This makes it very handy for cleaning up document titles.
4653ugly_title = " A Very\t Very Very Needlessly Long {Strange} Document Title [final edited draft23]"
54+ 🢂 sanitize_title(ugly_title)
55+ ' A Very Very Very Needlessly Long Strange Document Title final edited draft23'
4756abbrev_phrase_in_middle(sanitize_title(ugly_title))
4857🢂 ' A Very Very Very Needlessly Long Strange … final edited draft23'
4958
59+ # Then you can slugify to get nice filenames or URLs.
5060from slugify import slugify
5161slugify(abbrev_phrase_in_middle(sanitize_title(ugly_title)))
5262🢂 ' a-very-very-very-needlessly-long-strange-final-edited-draft23'
@@ -91,7 +101,4 @@ class MyThing:
91101 " url" : 128 ,
92102 },
93103 )
94-
95104```
96-
97- See pydoc for details.
0 commit comments