Skip to content

Commit 6d6cab1

Browse files
committed
prettify: internal lambda expressions moved to named functions for better readability and maintanability
1 parent c8dcbe6 commit 6d6cab1

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

string_utils.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -471,18 +471,35 @@ def strip_html(string, keep_tag_content=False):
471471

472472

473473
def prettify(string):
474-
# turns first letter after ".", "?", "!" into uppercase
475-
def uppercase_after_sign(regex_match):
474+
def remove_duplicates(regex_match):
475+
return regex_match.group(1)[0]
476+
477+
def uppercase_first_letter_after_sign(regex_match):
476478
match = regex_match.group(1)
477479
return match[:-1] + match[2].upper()
478480

479-
p = PRETTIFY_RE['DUPLICATES'].sub(lambda m: m.group(1)[0], string)
480-
p = PRETTIFY_RE['RIGHT_SPACE'].sub(lambda m: m.group(1).strip() + ' ', p)
481-
p = PRETTIFY_RE['LEFT_SPACE'].sub(lambda m: ' ' + m.group(1).strip(), p)
482-
p = PRETTIFY_RE['SPACES_AROUND'].sub(lambda m: ' ' + m.group(1).strip() + ' ', p)
483-
p = PRETTIFY_RE['SPACES_INSIDE'].sub(lambda m: m.group(1).strip(), p)
484-
p = PRETTIFY_RE['UPPERCASE_AFTER_SIGN'].sub(uppercase_after_sign, p)
485-
p = PRETTIFY_RE['SAXON_GENITIVE'].sub(lambda m: m.group(1).replace(' ', '') + ' ', p)
481+
def ensure_right_space_only(regex_match):
482+
return regex_match.group(1).strip() + ' '
483+
484+
def ensure_left_space_only(regex_match):
485+
return ' ' + regex_match.group(1).strip()
486+
487+
def ensure_spaces_around(regex_match):
488+
return ' ' + regex_match.group(1).strip() + ' '
489+
490+
def remove_internal_spaces(regex_match):
491+
return regex_match.group(1).strip()
492+
493+
def fix_saxon_genitive(regex_match):
494+
return regex_match.group(1).replace(' ', '') + ' '
495+
496+
p = PRETTIFY_RE['DUPLICATES'].sub(remove_duplicates, string)
497+
p = PRETTIFY_RE['RIGHT_SPACE'].sub(ensure_right_space_only, p)
498+
p = PRETTIFY_RE['LEFT_SPACE'].sub(ensure_left_space_only, p)
499+
p = PRETTIFY_RE['SPACES_AROUND'].sub(ensure_spaces_around, p)
500+
p = PRETTIFY_RE['SPACES_INSIDE'].sub(remove_internal_spaces, p)
501+
p = PRETTIFY_RE['UPPERCASE_AFTER_SIGN'].sub(uppercase_first_letter_after_sign, p)
502+
p = PRETTIFY_RE['SAXON_GENITIVE'].sub(fix_saxon_genitive, p)
486503
p = p.strip()
487504
try:
488505
return p[0].capitalize() + p[1:]

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