5 <meta name=
"viewport" content=
"width=device-width, initial-scale=1, minimum-scale=1" />
6 <meta name=
"generator" content=
"pdoc 0.9.2" />
7 <title>szyfrow.support.text_prettify API documentation
</title>
8 <meta name=
"description" content=
"Various functions for prettifying text, useful when cipher routines generate
9 strings of letters without spaces." />
10 <link rel=
"preload stylesheet" as=
"style" href=
"https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity=
"sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin
>
11 <link rel=
"preload stylesheet" as=
"style" href=
"https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity=
"sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin
>
12 <link rel=
"stylesheet preload" as=
"style" href=
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin
>
13 <style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:
1.5em}#content{padding:
20px}#sidebar{padding:
30px;overflow:hidden}#sidebar
> *:last-child{margin-bottom:
2cm}.http-server-breadcrumbs{font-size:
130%;margin:
0 0 15px
0}#footer{font-size:
.75em;padding:
5px
30px;border-top:
1px solid #ddd;text-align:right}#footer p{margin:
0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:
30px}h1,h2,h3,h4,h5{font-weight:
300}h1{font-size:
2.5em;line-height:
1.1em}h2{font-size:
1.75em;margin:
1em
0 .50em
0}h3{font-size:
1.4em;margin:
25px
0 10px
0}h4{margin:
0;font-size:
105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:
.2em
0}a{color:#
058;text-decoration:none;transition:color
.3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^=
"header-"]{margin-top:
2em}.ident{color:#
900}pre code{background:#f8f8f8;font-size:
.8em;line-height:
1.4em}code{background:#f2f2f1;padding:
1px
4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:
0;border-top:
1px solid #ccc;border-bottom:
1px solid #ccc;margin:
1em
0;padding:
1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:
10%}#http-server-module-list p{margin-top:
0}.toc ul,#index{list-style-type:none;margin:
0;padding:
0}#index code{background:transparent}#index h3{border-bottom:
1px solid #ddd}#index ul{padding:
0}#index h4{margin-top:
.6em;font-weight:bold}@media (min-width:
200ex){#index .two-column{column-count:
2}}@media (min-width:
300ex){#index .two-column{column-count:
3}}dl{margin-bottom:
2em}dl dl:last-child{margin-bottom:
4em}dd{margin:
0 0 1em
3em}#header-classes + dl
> dd{margin-bottom:
3em}dd dd{margin-left:
2em}dd p{margin:
10px
0}.name{background:#eee;font-weight:bold;font-size:
.85em;padding:
5px
10px;display:inline-block;min-width:
40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name
> span:first-child{white-space:nowrap}.name.class
> span:nth-child(
2){margin-left:
.4em}.inherited{color:#
999;border-left:
5px solid #eee;padding-left:
1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:
400;font-size:
1.25em}.desc h3{font-size:
1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#
666;text-align:right;font-weight:
400;font-size:
.8em;text-transform:uppercase}.source summary
> *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:
1em}.source pre{max-height:
500px;overflow:auto;margin:
0}.source pre code{font-size:
12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\
2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:
1em}img{max-width:
100%}td{padding:
0 .5em}.admonition{padding:
.1em
.5em;margin-bottom:
1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}
</style>
14 <style media=
"screen and (min-width: 700px)">@media screen and (min-width:
700px){#sidebar{width:
30%;height:
100vh;overflow:auto;position:sticky;top:
0}#content{width:
70%;max-width:
100ch;padding:
3em
4em;border-left:
1px solid #ddd}pre code{font-size:
1em}.item .name{font-size:
1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:
1.5em}.toc
> ul
> li{margin-top:
.5em}}
</style>
15 <style media=
"print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#
000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:
" (" attr(href)
")";font-size:
90%}a[href][title]:after{content:none}abbr[title]:after{content:
" (" attr(title)
")"}.ir a:after,a[href^=
"javascript:"]:after,a[href^=
"#"]:after{content:
""}pre,blockquote{border:
1px solid #
999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:
100% !important}@page{margin:
0.5cm}p,h2,h3{orphans:
3;widows:
3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}
</style>
16 <script defer
src=
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity=
"sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin
></script>
17 <script>window.addEventListener('DOMContentLoaded', () =
> hljs.initHighlighting())
</script>
21 <article id=
"content">
23 <h1 class=
"title">Module
<code>szyfrow.support.text_prettify
</code></h1>
25 <section id=
"section-intro">
26 <p>Various functions for prettifying text, useful when cipher routines generate
27 strings of letters without spaces.
</p>
28 <details class=
"source">
30 <span>Expand source code
</span>
32 <pre><code class=
"python">"""Various functions for prettifying text, useful when cipher routines generate
33 strings of letters without spaces.
37 from szyfrow.support.segment import segment
38 from szyfrow.support.utilities import cat, lcat, sanitise
41 def prettify(text, width=
100):
42 """Segment a text into words, then pack into lines, and combine the lines
43 into a single string for printing.
"""
44 return lcat(tpack(segment(text), width=width))
47 def tpack(text, width=
100):
48 """Pack a list of words into lines, so long as each line (including
49 intervening spaces) is no longer than _width_
"""
52 if len(lines[-
1]) +
1 + len(word)
<= width:
53 lines[-
1] += (
' ' + word)
59 def depunctuate_character(c):
60 """Record the punctuation of a character
"""
61 if c in string.ascii_uppercase:
62 return
'UPPER
'
63 elif c in string.ascii_lowercase:
64 return
'LOWER
'
69 def depunctuate(text):
70 """Record the punctuation of a string, so it can be applied to a converted
71 version of the string.
74 punct = depunctuate(ciphertext)
75 plaintext = decipher(sanitise(ciphertext))
76 readable_plaintext = repunctuate(plaintext, punct)
78 return [depunctuate_character(c) for c in text]
81 def repunctuate_character(letters, punctuation):
82 """Apply the recorded punctuation to a character. The letters must be
83 an iterator of base characters.
"""
84 if punctuation ==
'UPPER
':
85 return next(letters).upper()
86 elif punctuation ==
'LOWER
':
87 return next(letters).lower()
92 def repunctuate(text, punctuation):
93 """Apply the recored punctuation to a sanitised string.
96 punct = depunctuate(ciphertext)
97 plaintext = decipher(sanitise(ciphertext))
98 readable_plaintext = repunctuate(plaintext, punct)
100 letters = iter(sanitise(text))
101 return cat(repunctuate_character(letters, p) for p in punctuation)
</code></pre>
109 <h2 class=
"section-title" id=
"header-functions">Functions
</h2>
111 <dt id=
"szyfrow.support.text_prettify.cat"><code class=
"name flex">
112 <span>def
<span class=
"ident">cat
</span></span>(
<span>iterable, /)
</span>
115 <div class=
"desc"><p>Concatenate any number of strings.
</p>
116 <p>The string whose method is called is inserted in between each given string.
117 The result is returned as a new string.
</p>
118 <p>Example: '.'.join(['ab', 'pq', 'rs']) -
> 'ab.pq.rs'
</p></div>
120 <dt id=
"szyfrow.support.text_prettify.depunctuate"><code class=
"name flex">
121 <span>def
<span class=
"ident">depunctuate
</span></span>(
<span>text)
</span>
124 <div class=
"desc"><p>Record the punctuation of a string, so it can be applied to a converted
125 version of the string.
</p>
127 punct = depunctuate(ciphertext)
128 plaintext = decipher(sanitise(ciphertext))
129 readable_plaintext = repunctuate(plaintext, punct)
</p></div>
130 <details class=
"source">
132 <span>Expand source code
</span>
134 <pre><code class=
"python">def depunctuate(text):
135 """Record the punctuation of a string, so it can be applied to a converted
136 version of the string.
139 punct = depunctuate(ciphertext)
140 plaintext = decipher(sanitise(ciphertext))
141 readable_plaintext = repunctuate(plaintext, punct)
143 return [depunctuate_character(c) for c in text]
</code></pre>
146 <dt id=
"szyfrow.support.text_prettify.depunctuate_character"><code class=
"name flex">
147 <span>def
<span class=
"ident">depunctuate_character
</span></span>(
<span>c)
</span>
150 <div class=
"desc"><p>Record the punctuation of a character
</p></div>
151 <details class=
"source">
153 <span>Expand source code
</span>
155 <pre><code class=
"python">def depunctuate_character(c):
156 """Record the punctuation of a character
"""
157 if c in string.ascii_uppercase:
158 return
'UPPER
'
159 elif c in string.ascii_lowercase:
160 return
'LOWER
'
162 return c
</code></pre>
165 <dt id=
"szyfrow.support.text_prettify.lcat"><code class=
"name flex">
166 <span>def
<span class=
"ident">lcat
</span></span>(
<span>iterable, /)
</span>
169 <div class=
"desc"><p>Concatenate any number of strings.
</p>
170 <p>The string whose method is called is inserted in between each given string.
171 The result is returned as a new string.
</p>
172 <p>Example: '.'.join(['ab', 'pq', 'rs']) -
> 'ab.pq.rs'
</p></div>
174 <dt id=
"szyfrow.support.text_prettify.prettify"><code class=
"name flex">
175 <span>def
<span class=
"ident">prettify
</span></span>(
<span>text, width=
100)
</span>
178 <div class=
"desc"><p>Segment a text into words, then pack into lines, and combine the lines
179 into a single string for printing.
</p></div>
180 <details class=
"source">
182 <span>Expand source code
</span>
184 <pre><code class=
"python">def prettify(text, width=
100):
185 """Segment a text into words, then pack into lines, and combine the lines
186 into a single string for printing.
"""
187 return lcat(tpack(segment(text), width=width))
</code></pre>
190 <dt id=
"szyfrow.support.text_prettify.repunctuate"><code class=
"name flex">
191 <span>def
<span class=
"ident">repunctuate
</span></span>(
<span>text, punctuation)
</span>
194 <div class=
"desc"><p>Apply the recored punctuation to a sanitised string.
</p>
196 punct = depunctuate(ciphertext)
197 plaintext = decipher(sanitise(ciphertext))
198 readable_plaintext = repunctuate(plaintext, punct)
</p></div>
199 <details class=
"source">
201 <span>Expand source code
</span>
203 <pre><code class=
"python">def repunctuate(text, punctuation):
204 """Apply the recored punctuation to a sanitised string.
207 punct = depunctuate(ciphertext)
208 plaintext = decipher(sanitise(ciphertext))
209 readable_plaintext = repunctuate(plaintext, punct)
211 letters = iter(sanitise(text))
212 return cat(repunctuate_character(letters, p) for p in punctuation)
</code></pre>
215 <dt id=
"szyfrow.support.text_prettify.repunctuate_character"><code class=
"name flex">
216 <span>def
<span class=
"ident">repunctuate_character
</span></span>(
<span>letters, punctuation)
</span>
219 <div class=
"desc"><p>Apply the recorded punctuation to a character. The letters must be
220 an iterator of base characters.
</p></div>
221 <details class=
"source">
223 <span>Expand source code
</span>
225 <pre><code class=
"python">def repunctuate_character(letters, punctuation):
226 """Apply the recorded punctuation to a character. The letters must be
227 an iterator of base characters.
"""
228 if punctuation ==
'UPPER
':
229 return next(letters).upper()
230 elif punctuation ==
'LOWER
':
231 return next(letters).lower()
233 return punctuation
</code></pre>
236 <dt id=
"szyfrow.support.text_prettify.tpack"><code class=
"name flex">
237 <span>def
<span class=
"ident">tpack
</span></span>(
<span>text, width=
100)
</span>
240 <div class=
"desc"><p>Pack a list of words into lines, so long as each line (including
241 intervening spaces) is no longer than
<em>width
</em></p></div>
242 <details class=
"source">
244 <span>Expand source code
</span>
246 <pre><code class=
"python">def tpack(text, width=
100):
247 """Pack a list of words into lines, so long as each line (including
248 intervening spaces) is no longer than _width_
"""
250 for word in text[
1:]:
251 if len(lines[-
1]) +
1 + len(word)
<= width:
252 lines[-
1] += (
' ' + word)
255 return lines
</code></pre>
269 <li><h3>Super-module
</h3>
271 <li><code><a title=
"szyfrow.support" href=
"index.html">szyfrow.support
</a></code></li>
274 <li><h3><a href=
"#header-functions">Functions
</a></h3>
276 <li><code><a title=
"szyfrow.support.text_prettify.cat" href=
"#szyfrow.support.text_prettify.cat">cat
</a></code></li>
277 <li><code><a title=
"szyfrow.support.text_prettify.depunctuate" href=
"#szyfrow.support.text_prettify.depunctuate">depunctuate
</a></code></li>
278 <li><code><a title=
"szyfrow.support.text_prettify.depunctuate_character" href=
"#szyfrow.support.text_prettify.depunctuate_character">depunctuate_character
</a></code></li>
279 <li><code><a title=
"szyfrow.support.text_prettify.lcat" href=
"#szyfrow.support.text_prettify.lcat">lcat
</a></code></li>
280 <li><code><a title=
"szyfrow.support.text_prettify.prettify" href=
"#szyfrow.support.text_prettify.prettify">prettify
</a></code></li>
281 <li><code><a title=
"szyfrow.support.text_prettify.repunctuate" href=
"#szyfrow.support.text_prettify.repunctuate">repunctuate
</a></code></li>
282 <li><code><a title=
"szyfrow.support.text_prettify.repunctuate_character" href=
"#szyfrow.support.text_prettify.repunctuate_character">repunctuate_character
</a></code></li>
283 <li><code><a title=
"szyfrow.support.text_prettify.tpack" href=
"#szyfrow.support.text_prettify.tpack">tpack
</a></code></li>
290 <p>Generated by
<a href=
"https://pdoc3.github.io/pdoc"><cite>pdoc
</cite> 0.9.2</a>.
</p>