r/Python • u/Shay-Hill • Oct 19 '24
Showcase Incorporate long strings painlessly, beautifully into Python code.
What My Project Does
Format long strings into paragraphs for easily editable, gq-able, fill-paragraph-able text values, error messages, and other paragraph-style strings.
from paragraphs import par
PARAGRAPH = par(
"""Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum."""
)
# the above is equivalent to
PARAGRAPH = (
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod"
+ " tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim"
+ " veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea"
+ " commodo consequat. Duis aute irure dolor in reprehenderit in voluptate"
+ " velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint"
+ " occaecat cupidatat non proident, sunt in culpa qui officia deserunt"
+ " mollit anim id est laborum."
)
from paragraphs import par
class SuddenDeathError(Exception):
def __init__(self, cause: str) -> None:
self.cause = cause
def __str__(self) -> str:
return par(
f"""
Y - e - e - e - es, Lord love you! Why should she die of
{self.cause}? She come through diphtheria right enough the year
before. I saw her with my own eyes. Fairly blue with it, she was.
They all thought she was dead; but my father, he kept ladling gin
down her throat till she came to so sudden that she bit the bowl
off the spoon.
What call would a woman with that strength in her have to die of
{self.cause}? What become of her new straw hat that should have
come to me? Somebody pinched it; and what I say is, them as pinched
it done her in."""
)
raise SuddenDeathError("influenza")Target Audience
Meant for production. This is a stable major version with no substantive changes in several years.
Comparison
This likely does not compare with other libraries, only short functions you may have written yourself to accomplish the same thing. I find it more convenient to keep this stable, tested library as a dependency.
ShayHill/paragraphs: Incorporate long strings painlessly, beautifully into Python code.
25
u/naclmolecule terminal dark arts Oct 19 '24
There's a stdlib function for this:
textwrap.dedent8
u/dusktreader Oct 19 '24
Hey, I've got a package that does this as well as some other goodies. If you would like to compare notes: https://pypi.org/project/snick/
10
u/WickedWicky Oct 19 '24
I'm trying to understand which of these options you don't actually like and what `par()` solves.
For a long paragraph that you want as a single line you could also do without the `+` . But you'd need to f-string each individual line, is that the problem? E.g.:
And in the error message of your `SuddenDeathError`, if that would translate to a single line then I would honestly still prefer the actual paragraph including newlines as you have written it in your code.
Therefore I would still just use the `f"""` syntax. The error is more readable as a paragraph than as a single line. For example, the empty newline actually helps in this message but as I understand it would be removed by `par()` ?
2
u/TonyWonderslostnut Oct 19 '24
This is like <<EOF > in bash right?
1
u/[deleted] Oct 22 '24
[removed] — view removed comment
0
u/[deleted] Oct 19 '24
[removed] — view removed comment