Skip to content

Commit e7d3927

Browse files
committed
better prettify implementation + new tests + docs
1 parent 6d6cab1 commit e7d3927

File tree

9 files changed

+91
-12
lines changed

9 files changed

+91
-12
lines changed
98 Bytes
Binary file not shown.

docs/_build/doctrees/index.doctree

6.87 KB
Binary file not shown.

docs/_build/html/genindex.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ <h1 id="index">Index</h1>
112112
<div class="genindex-jumpbox">
113113
<a href="#C"><strong>C</strong></a>
114114
| <a href="#I"><strong>I</strong></a>
115+
| <a href="#P"><strong>P</strong></a>
115116
| <a href="#R"><strong>R</strong></a>
116117
| <a href="#S"><strong>S</strong></a>
117118
| <a href="#U"><strong>U</strong></a>
@@ -178,6 +179,16 @@ <h2 id="I">I</h2>
178179
</dl></td>
179180
</tr></table>
180181

182+
<h2 id="P">P</h2>
183+
<table style="width: 100%" class="indextable genindextable"><tr>
184+
<td style="width: 33%" valign="top"><dl>
185+
186+
<dt><a href="index.html#string_utils.prettify">prettify() (in module string_utils)</a>
187+
</dt>
188+
189+
</dl></td>
190+
</tr></table>
191+
181192
<h2 id="R">R</h2>
182193
<table style="width: 100%" class="indextable genindextable"><tr>
183194
<td style="width: 33%" valign="top"><dl>

docs/_build/html/index.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,35 @@ <h1>Welcome to Python String Utils&#8217;s documentation!<a class="headerlink" h
498498
</table>
499499
</dd></dl>
500500

501+
<dl class="function">
502+
<dt id="string_utils.prettify">
503+
<code class="descclassname">string_utils.</code><code class="descname">prettify</code><span class="sig-paren">(</span><em>string</em><span class="sig-paren">)</span><a class="headerlink" href="#string_utils.prettify" title="Permalink to this definition"></a></dt>
504+
<dd><p>Turns an ugly text string into a beautiful one by applying a regex pipeline which ensures the following:</p>
505+
<ul class="simple">
506+
<li>String cannot start or end with spaces</li>
507+
<li>String cannot have multiple sequential spaces, empty lines or punctuation (except for &#8221;?&#8221;, &#8221;!&#8221; and &#8221;.&#8221;)</li>
508+
<li>Arithmetic operators (&#8220;+&#8221;, &#8220;-&#8221;, &#8220;/&#8221;, &#8220;*&#8221;, &#8220;=&#8221;) must have one, and only one space before and after themselves</li>
509+
<li>The first letter after a dot, an exclamation or a question mark must be uppercase</li>
510+
<li>One, and only one space should follow a dot, an exclamation or a question mark</li>
511+
<li>Text inside double quotes cannot start or end with spaces, but one, and only one space must come first and after quotes (foo&#8221; bar&#8221;baz -&gt; foo &#8220;bar&#8221; baz)</li>
512+
<li>Text inside round brackets cannot start or end with spaces, but one, and only one space must come first and after brackets (&#8220;foo(bar )baz&#8221; -&gt; &#8220;foo (bar) baz&#8221;)</li>
513+
<li>Percentage sign (&#8220;%&#8221;) cannot be preceded by a space if there is a number before (&#8220;100 %&#8221; -&gt; &#8220;100%&#8221;)</li>
514+
<li>Saxon genitive is correct (&#8220;Dave&#8217; s dog&#8221; -&gt; &#8220;Dave&#8217;s dog&#8221;)</li>
515+
</ul>
516+
<table class="docutils field-list" frame="void" rules="none">
517+
<col class="field-name" />
518+
<col class="field-body" />
519+
<tbody valign="top">
520+
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>string</strong> &#8211; String to manipulate</td>
521+
</tr>
522+
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body">Prettified string.</td>
523+
</tr>
524+
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body">str</td>
525+
</tr>
526+
</tbody>
527+
</table>
528+
</dd></dl>
529+
501530
</div>
502531
<div class="section" id="indices-and-tables">
503532
<h1>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline"></a></h1>

docs/_build/html/objects.inv

11 Bytes
Binary file not shown.

docs/_build/html/searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='python-string-utils',
8-
version='0.2.0',
8+
version='0.3.0',
99
description='Utility functions for strings checking and manipulation.',
1010
long_description=long_description,
1111
author='Davide Zanotti',

string_utils.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import random
77

88
# module settings
9-
__version__ = '0.2.0'
9+
__version__ = '0.3.0'
1010
__all__ = [
1111
'is_string',
1212
'is_url',
@@ -81,11 +81,11 @@
8181
'RIGHT_SPACE': re.compile(
8282
r'('
8383
r'(?<=[^\s\d]),(?=[^\s\d])|\s,\s|\s,(?=[^\s\d])|\s,(?!.)|' # comma (,)
84-
r'(?<=[^\s\d])\.(?=[^\s\d])|\s\.\s|\s\.(?=[^\s\d])|\s\.(?!.)|' # dot (.)
84+
r'(?<=[^\s\d\.])\.+(?=[^\s\d\.])|\s\.+\s|\s\.+(?=[^\s\d])|\s\.+(?!\.)|' # dot (.)
8585
r'(?<=\S);(?=\S)|\s;\s|\s;(?=\S)|\s;(?!.)|' # semicolon (;)
8686
r'(?<=\S):(?=\S)|\s:\s|\s:(?=\S)|\s:(?!.)|' # colon (:)
87-
r'(?<=\S)!(?=\S)|\s!\s|\s!(?=\S)|\s!(?!.)|' # exclamation (!)
88-
r'(?<=\S)\?(?=\S)|\s\?\s|\s\?(?=\S)|\s\?(?!.)|' # question (?)
87+
r'(?<=[^\s!])!+(?=[^\s!])|\s!+\s|\s!+(?=[^\s!])|\s!+(?!!)|' # exclamation (!)
88+
r'(?<=[^\s\?])\?+(?=[^\s\?])|\s\?+\s|\s\?+(?=[^\s\?])|\s\?+(?!\?)|' # question (?)
8989
r'\d%(?=\S)|(?<=\d)\s%\s|(?<=\d)\s%(?=\S)|(?<=\d)\s%(?!.)' # percentage (%)
9090
r')',
9191
re.MULTILINE | re.DOTALL
@@ -471,6 +471,27 @@ def strip_html(string, keep_tag_content=False):
471471

472472

473473
def prettify(string):
474+
"""
475+
Turns an ugly text string into a beautiful one by applying a regex pipeline which ensures the following:
476+
477+
- String cannot start or end with spaces
478+
- String cannot have multiple sequential spaces, empty lines or punctuation (except for "?", "!" and ".")
479+
- Arithmetic operators ("+", "-", "/", "*", "=") must have one, and only one space before and after themselves
480+
- The first letter after a dot, an exclamation or a question mark must be uppercase
481+
- One, and only one space should follow a dot, an exclamation or a question mark
482+
- Text inside double quotes cannot start or end with spaces, but one, and only one space must come first and \
483+
after quotes (foo" bar"baz -> foo "bar" baz)
484+
- Text inside round brackets cannot start or end with spaces, but one, and only one space must come first and \
485+
after brackets ("foo(bar )baz" -> "foo (bar) baz")
486+
- Percentage sign ("%") cannot be preceded by a space if there is a number before ("100 %" -> "100%")
487+
- Saxon genitive is correct ("Dave' s dog" -> "Dave's dog")
488+
489+
490+
:param string: String to manipulate
491+
:return: Prettified string.
492+
:rtype: str
493+
"""
494+
474495
def remove_duplicates(regex_match):
475496
return regex_match.group(1)[0]
476497

tests.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,29 +1080,35 @@ def test_should_replace_multiple_percentage_with_single_ones(self):
10801080
self.assertEqual('%', prettify('%%%'))
10811081
self.assertEqual('A % b % c', prettify('a %% b %%%%%% c'))
10821082

1083-
def test_should_put_space_after_comma_if_missing(self):
1083+
def test_should_add_space_after_comma_if_missing(self):
10841084
self.assertEqual('One, two, three', prettify('one,two,three'))
10851085

1086+
def test_should_not_add_right_space_after_dot_for_numbers(self):
1087+
self.assertEqual('12,55', prettify('12,55'))
1088+
10861089
def test_should_remove_space_before_comma(self):
10871090
self.assertEqual('One, two, three', prettify('one , two , three'))
10881091

10891092
def test_should_uppercase_first_letter_after_period(self):
10901093
self.assertEqual('Foo. Bar', prettify('Foo. bar'))
10911094

1092-
def test_should_put_space_after_period_if_missing(self):
1095+
def test_should_add_space_after_period_if_missing(self):
10931096
self.assertEqual('One. Two. Three', prettify('one.two.three'))
10941097

1098+
def test_should_not_add_right_space_after_comma_for_numbers(self):
1099+
self.assertEqual('12.55', prettify('12.55'))
1100+
10951101
def test_should_remove_space_before_period(self):
10961102
self.assertEqual('One. Two. Three', prettify('one . two . three'))
10971103

1098-
def test_should_put_space_after_colon_if_missing(self):
1104+
def test_should_add_space_after_colon_if_missing(self):
10991105
self.assertEqual('Test: this', prettify('Test:this'))
11001106

11011107
def test_should_remove_space_before_colon(self):
11021108
self.assertEqual('Test: this', prettify('Test :this'))
11031109
self.assertEqual('Test:', prettify('Test :'))
11041110

1105-
def test_should_put_space_after_semicolon_if_missing(self):
1111+
def test_should_add_space_after_semicolon_if_missing(self):
11061112
self.assertEqual('Test; this', prettify('Test;this'))
11071113

11081114
def test_should_remove_space_before_semicolon(self):
@@ -1112,7 +1118,7 @@ def test_should_remove_space_before_semicolon(self):
11121118
def test_should_uppercase_first_letter_after_exclamation(self):
11131119
self.assertEqual('Foo! Bar', prettify('Foo! bar'))
11141120

1115-
def test_should_put_space_after_exclamation_if_missing(self):
1121+
def test_should_add_space_after_exclamation_if_missing(self):
11161122
self.assertEqual('Test! This', prettify('Test!this'))
11171123

11181124
def test_should_remove_space_before_exclamation(self):
@@ -1122,7 +1128,7 @@ def test_should_remove_space_before_exclamation(self):
11221128
def test_should_uppercase_first_letter_after_question(self):
11231129
self.assertEqual('Foo? Bar', prettify('Foo? bar'))
11241130

1125-
def test_should_put_space_after_question_if_missing(self):
1131+
def test_should_add_space_after_question_if_missing(self):
11261132
self.assertEqual('Test? This', prettify('Test?this'))
11271133

11281134
def test_should_remove_space_before_question(self):
@@ -1165,6 +1171,18 @@ def test_should_add_spaces_around_multiplication_if_missing(self):
11651171
self.assertEqual('5 * 2 = 10', prettify('5 *2 = 10'))
11661172
self.assertEqual('5 * 2 = 10', prettify('5*2 = 10'))
11671173

1174+
def test_triple_dot_preserved(self):
1175+
self.assertEqual('Test...', prettify('Test...'))
1176+
self.assertEqual('Test... This', prettify('Test...This'))
1177+
1178+
def test_triple_exclamation_preserved(self):
1179+
self.assertEqual('Test!!!', prettify('Test!!!'))
1180+
self.assertEqual('Test!!! This', prettify('Test!!!This'))
1181+
1182+
def test_triple_question_preserved(self):
1183+
self.assertEqual('Test???', prettify('Test???'))
1184+
self.assertEqual('Test??? This', prettify('Test???This'))
1185+
11681186
def test_should_prettify_string_as_expected(self):
11691187
original = ' unprettified string ,, like this one,will be"prettified" .it\' s awesome!( like python)) '
11701188
pretty = 'Unprettified string, like this one, will be "prettified". It\'s awesome! (like python)'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy