Repository navigation
Expand file tree
/
Copy pathtext_tools.py
More file actions
154 lines (117 loc) · 5.54 KB
/
Copy pathtext_tools.py
File metadata and controls
154 lines (117 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""Tools for building and reshaping text: join, textwrap, translate and string.
Strings are immutable, so "adding to" a string really makes a new one. For a
few pieces that is fine. For many pieces, collect them in a list and call
`"".join(parts)` once at the end; it is the standard Python idiom.
The rest of this file is a tour of small tools that save you from writing
fiddly loops:
textwrap wrap, fill, shorten and indent paragraphs of text
str.translate replace or delete many single characters in one pass
string ready-made character sets such as string.ascii_letters
string.Template simple $name substitution, safe for user-written templates
"""
import string
import textwrap
from string import Template
def render_receipt(items: dict[str, float]) -> str:
"""Build a multi-line string by collecting lines, then joining them."""
lines = ["RECEIPT"]
for name, price in items.items():
lines.append(f"{name:<10}{price:>8.2f}")
lines.append(f"{'total':<10}{sum(items.values()):>8.2f}")
return "\n".join(lines)
def building_strings_demo() -> None:
# += works, but each step copies everything built so far. With thousands
# of pieces that copying adds up; a list and one join does not.
slow = ""
for word in ["build", "it", "up"]:
slow += word + " "
print(f"with +=: {slow.strip()!r}")
print(f"with join: {' '.join(['build', 'it', 'up'])!r}")
print(render_receipt({"coffee": 3.5, "bagel": 2.25, "juice": 4.0}))
# join only accepts strings, so convert numbers first.
numbers = [3, 1, 4, 1, 5]
print(", ".join(str(n) for n in numbers))
SAMPLE = (
"Python's standard library is often described as batteries included: "
"it ships with modules for files, dates, text, networking and much more, "
"so many small programs need nothing beyond a fresh install."
)
def textwrap_demo() -> None:
# wrap() returns a list of lines no wider than width.
lines = textwrap.wrap(SAMPLE, width=40)
print(f"wrap -> {len(lines)} lines, longest {max(map(len, lines))} chars")
# fill() is wrap() joined with newlines: ready to print.
print("fill(width=40):")
print(textwrap.fill(SAMPLE, width=40))
# shorten() collapses whitespace and cuts at a word boundary.
print(f"shorten: {textwrap.shorten(SAMPLE, width=50, placeholder=' [...]')}")
# indent() prefixes every line, handy for nesting output.
print("indent:")
print(textwrap.indent(textwrap.fill(SAMPLE, width=36), " | "))
# dedent() removes the common leading spaces from an indented block.
block = """
name: demo
tags:
- text
"""
print("dedent:")
print(textwrap.dedent(block).strip())
def translate_demo() -> None:
# maketrans(from, to) pairs characters by position; translate applies
# every swap in a single pass over the string.
leet = str.maketrans("aeiost", "4310$7")
print(f"leet speak: {'translate is fast'.translate(leet)}")
# A third argument lists characters to delete.
no_punct = str.maketrans("", "", ",.!?;:")
print(f"punctuation removed: {'Hello, world! Ready?'.translate(no_punct)}")
# A dict works too, and can map one character to a longer string.
escapes = str.maketrans({"<": "<", ">": ">", "&": "&"})
print(f"escaped: {'a < b & b > c'.translate(escapes)}")
# Compare with chained replace(): fine for two swaps, clumsy for ten,
# and wrong when one swap's output feeds the next.
swapped = "left right".replace("left", "right").replace("right", "left")
print(f"chained replace: {swapped!r} <- both became 'left'")
lr = str.maketrans({"L": "R", "R": "L"})
print(f"translate swaps safely: {'L R L'.translate(lr)!r}")
def is_valid_username(name: str) -> bool:
"""Letters, digits and underscore only, 3 to 12 characters."""
allowed = set(string.ascii_letters + string.digits + "_")
return 3 <= len(name) <= 12 and set(name) <= allowed
def string_constants_demo() -> None:
print(f"ascii_lowercase = {string.ascii_lowercase}")
print(f"ascii_uppercase = {string.ascii_uppercase}")
print(f"digits = {string.digits}")
print(f"hexdigits = {string.hexdigits}")
print(f"punctuation = {string.punctuation}")
for name in ["ada_99", "no spaces", "x", "Grace"]:
print(f" {name!r:<12} valid? {is_valid_username(name)}")
# capwords: split on spaces, capitalize each word, join again.
print(f"capwords: {string.capwords('the quick brown fox')}")
def template_demo() -> None:
# $name placeholders, filled in from keyword arguments or a dict.
greeting = Template("Hello $name, your order #$order ships ${day}day.")
print(greeting.substitute(name="Ada", order=42, day="Mon"))
# substitute() insists on every value; safe_substitute() leaves gaps.
partial = Template("Dear $name, your code is $code.")
try:
partial.substitute(name="Alan")
except KeyError as exc:
print(f"KeyError: missing {exc}")
print(partial.safe_substitute(name="Alan"))
# Why use Template at all, when f-strings exist? A template can come from
# a file or a user. It can only fill in names, never run code, unlike
# str.format, whose fields can reach into attributes of what you pass.
user_text = "Thanks, $who! ($$5 off next time)"
print(Template(user_text).substitute(who="Grace"))
def main() -> None:
building_strings_demo()
print()
textwrap_demo()
print()
translate_demo()
print()
string_constants_demo()
print()
template_demo()
if __name__ == "__main__":
main()