| 1 |
|
| 2 |
from functools import wraps |
| 3 |
|
| 4 |
|
| 5 |
# Punctuation characters that should not be duplicated when two of them end up |
| 6 |
# adjacent at a concatenation seam (e.g. a suffix '.' following text that |
| 7 |
# already ends in '.'). |
| 8 |
SEAM_PUNCTUATION = frozenset('.,;:!?') |
| 9 |
|
| 10 |
|
| 11 |
def normalize_seam(left, other): |
| 12 |
"""Adjust the leading characters of `other` so that concatenating it onto |
| 13 |
`left` does not introduce a double space or a duplicated punctuation mark. |
| 14 |
|
| 15 |
`left` is the accumulated string so far and `other` the piece about to be |
| 16 |
appended. Only the boundary between them is considered; the interior of |
| 17 |
either side (e.g. an ellipsis inside a title) is never touched. Markup |
| 18 |
segments start with '<', so they are left alone. Returns `other`, possibly |
| 19 |
with its first character removed.""" |
| 20 |
left = str(left) |
| 21 |
other_str = str(other) |
| 22 |
if not left or not other_str: |
| 23 |
return other |
| 24 |
last, first = left[-1], other_str[0] |
| 25 |
# collapse a double space |
| 26 |
if last == ' ' and first == ' ': |
| 27 |
return _strip_first_char(other) |
| 28 |
# drop a duplicated punctuation mark ('..' -> '.', ',,' -> ',', ...) |
| 29 |
if last == first and first in SEAM_PUNCTUATION: |
| 30 |
return _strip_first_char(other) |
| 31 |
return other |
| 32 |
|
| 33 |
|
| 34 |
def _strip_first_char(other): |
| 35 |
"""Return a copy of `other` with its first character removed, preserving |
| 36 |
the (Mixed)String segment structure.""" |
| 37 |
if isinstance(other, MixedString): |
| 38 |
segments = list(other) |
| 39 |
head = segments[0] |
| 40 |
stripped = type(head)(str(head)[1:]) |
| 41 |
segments[0:1] = [stripped] if stripped != '' else [] |
| 42 |
return MixedString(segments) |
| 43 |
return type(other)(str(other)[1:]) |
| 44 |
|
| 45 |
|
| 46 |
def discard_empty_other(method): |
| 47 |
"""Decorator for addition operator methods that returns the object itself if |
| 48 |
`other` is the empty string.""" |
| 49 |
@wraps(method) |
| 50 |
def wrapper(obj, other): |
| 51 |
if other == '': |
| 52 |
return obj |
| 53 |
else: |
| 54 |
return method(obj, other) |
| 55 |
return wrapper |
| 56 |
|
| 57 |
|
| 58 |
class String(str): |
| 59 |
@discard_empty_other |
| 60 |
def __radd__(self, other): |
| 61 |
return MixedString([other]).__add__(self) |
| 62 |
|
| 63 |
@discard_empty_other |
| 64 |
def __add__(self, other): |
| 65 |
return MixedString([self]).__add__(other) |
| 66 |
|
| 67 |
def __iadd__(self, other): |
| 68 |
return self.__add__(other) |
| 69 |
|
| 70 |
def replace(self, *args, **kwargs): |
| 71 |
return self.__class__(super(String, self).replace(*args, **kwargs)) |
| 72 |
|
| 73 |
def rstrip(self, *args, **kwargs): |
| 74 |
return self.__class__(super(String, self).rstrip(*args, **kwargs)) |
| 75 |
|
| 76 |
def lower(self): |
| 77 |
return self.__class__(super(String, self).lower()) |
| 78 |
|
| 79 |
def upper(self): |
| 80 |
return self.__class__(super(String, self).upper()) |
| 81 |
|
| 82 |
def soft_lower(self): |
| 83 |
return self.lower() |
| 84 |
|
| 85 |
def soft_upper(self): |
| 86 |
return self.upper() |
| 87 |
|
| 88 |
def capitalize_first(self): |
| 89 |
return self.__class__(self[0].upper() + self[1:]) |
| 90 |
|
| 91 |
def words(self): |
| 92 |
for word in self.split(): |
| 93 |
yield self.__class__(word) |
| 94 |
|
| 95 |
|
| 96 |
class MixedString(list): |
| 97 |
@discard_empty_other |
| 98 |
def __add__(self, other): |
| 99 |
other = normalize_seam(self, other) |
| 100 |
if other == '' or other == []: |
| 101 |
return self |
| 102 |
super_obj = super(MixedString, self) |
| 103 |
try: |
| 104 |
return self.__class__(super_obj.__add__(other)) |
| 105 |
except TypeError: |
| 106 |
return self.__class__(super_obj.__add__(MixedString([other]))) |
| 107 |
|
| 108 |
@discard_empty_other |
| 109 |
def __radd__(self, other): |
| 110 |
return self.__class__([other]).__add__(self) |
| 111 |
|
| 112 |
def __iadd__(self, other): |
| 113 |
return self.__add__(other) |
| 114 |
|
| 115 |
def __str__(self): |
| 116 |
return ''.join(map(str, self)) |
| 117 |
|
| 118 |
def __getitem__(self, index): |
| 119 |
return str(self)[index] |
| 120 |
|
| 121 |
def replace(self, *args): |
| 122 |
return self.__class__([string.replace(*args) for string in self]) |
| 123 |
|
| 124 |
def translate(self, table): |
| 125 |
return self.__class__([string.translate(table) for string in self]) |
| 126 |
|
| 127 |
def lower(self): |
| 128 |
return self.__class__([string.lower() for string in self]) |
| 129 |
|
| 130 |
def upper(self): |
| 131 |
return self.__class__([string.upper() for string in self]) |
| 132 |
|
| 133 |
def title(self): |
| 134 |
return self.__class__([string.title() for string in self]) |
| 135 |
|
| 136 |
def capitalize_first(self): |
| 137 |
self_iter = iter(self) |
| 138 |
output = [next(self_iter).capitalize_first()] |
| 139 |
output += [string for string in self_iter] |
| 140 |
return self.__class__(output) |
| 141 |
|
| 142 |
def isupper(self): |
| 143 |
return all(string.isupper() for string in self) |
| 144 |
|
| 145 |
def split(self, *args, **kwargs): |
| 146 |
return str(self).split(*args, **kwargs) |
| 147 |
|
| 148 |
def rstrip(self, *args, **kwargs): |
| 149 |
rev_iter = reversed(self) |
| 150 |
output = [next(rev_iter).rstrip(*args, **kwargs)] |
| 151 |
output += [string for string in rev_iter] |
| 152 |
return self.__class__(reversed(output)) |
| 153 |
|
| 154 |
def words(self): |
| 155 |
for string in self: |
| 156 |
for word in string.words(): |
| 157 |
yield word |
| 158 |
|
| 159 |
|
| 160 |
class NoCase(String): |
| 161 |
def __repr__(self): |
| 162 |
return '{}({})'.format(self.__class__.__name__, str(self)) |
| 163 |
|
| 164 |
def soft_lower(self): |
| 165 |
return self |
| 166 |
|
| 167 |
def soft_upper(self): |
| 168 |
return self |
| 169 |
|
| 170 |
def capitalize_first(self): |
| 171 |
return self |
| 172 |
|
| 173 |
|
| 174 |
def join(items, delimiter=''): |
| 175 |
items = iter(items) |
| 176 |
try: |
| 177 |
output = next(items) |
| 178 |
except StopIteration: |
| 179 |
return String('') |
| 180 |
|
| 181 |
for item in items: |
| 182 |
delimiter_part = normalize_seam(output, delimiter) |
| 183 |
output = output + delimiter_part |
| 184 |
output = output + normalize_seam(output, item) |
| 185 |
return output |