| 1 | n/a | #!/usr/bin/env python |
|---|
| 2 | n/a | ## vim:ts=4:et:nowrap |
|---|
| 3 | n/a | """A user-defined wrapper around string objects |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | Note: string objects have grown methods in Python 1.6 |
|---|
| 6 | n/a | This module requires Python 1.6 or later. |
|---|
| 7 | 1 | """ |
|---|
| 8 | 1 | import sys |
|---|
| 9 | 1 | import collections |
|---|
| 10 | n/a | |
|---|
| 11 | 1 | __all__ = ["UserString","MutableString"] |
|---|
| 12 | n/a | |
|---|
| 13 | 2 | class UserString(collections.Sequence): |
|---|
| 14 | 1 | def __init__(self, seq): |
|---|
| 15 | 88609 | if isinstance(seq, basestring): |
|---|
| 16 | 88609 | self.data = seq |
|---|
| 17 | 0 | elif isinstance(seq, UserString): |
|---|
| 18 | 0 | self.data = seq.data[:] |
|---|
| 19 | n/a | else: |
|---|
| 20 | 0 | self.data = str(seq) |
|---|
| 21 | 1 | def __str__(self): return str(self.data) |
|---|
| 22 | 1 | def __repr__(self): return repr(self.data) |
|---|
| 23 | 1 | def __int__(self): return int(self.data) |
|---|
| 24 | 1 | def __long__(self): return long(self.data) |
|---|
| 25 | 1 | def __float__(self): return float(self.data) |
|---|
| 26 | 1 | def __complex__(self): return complex(self.data) |
|---|
| 27 | 9 | def __hash__(self): return hash(self.data) |
|---|
| 28 | n/a | |
|---|
| 29 | 1 | def __cmp__(self, string): |
|---|
| 30 | 19510 | if isinstance(string, UserString): |
|---|
| 31 | 485 | return cmp(self.data, string.data) |
|---|
| 32 | n/a | else: |
|---|
| 33 | 19025 | return cmp(self.data, string) |
|---|
| 34 | 1 | def __contains__(self, char): |
|---|
| 35 | 530002 | return char in self.data |
|---|
| 36 | n/a | |
|---|
| 37 | 130563 | def __len__(self): return len(self.data) |
|---|
| 38 | 920 | def __getitem__(self, index): return self.__class__(self.data[index]) |
|---|
| 39 | 1 | def __getslice__(self, start, end): |
|---|
| 40 | 16318 | start = max(start, 0); end = max(end, 0) |
|---|
| 41 | 16318 | return self.__class__(self.data[start:end]) |
|---|
| 42 | n/a | |
|---|
| 43 | 1 | def __add__(self, other): |
|---|
| 44 | 6 | if isinstance(other, UserString): |
|---|
| 45 | 6 | return self.__class__(self.data + other.data) |
|---|
| 46 | 0 | elif isinstance(other, basestring): |
|---|
| 47 | 0 | return self.__class__(self.data + other) |
|---|
| 48 | n/a | else: |
|---|
| 49 | 0 | return self.__class__(self.data + str(other)) |
|---|
| 50 | 1 | def __radd__(self, other): |
|---|
| 51 | 0 | if isinstance(other, basestring): |
|---|
| 52 | 0 | return self.__class__(other + self.data) |
|---|
| 53 | n/a | else: |
|---|
| 54 | 0 | return self.__class__(str(other) + self.data) |
|---|
| 55 | 1 | def __mul__(self, n): |
|---|
| 56 | 10 | return self.__class__(self.data*n) |
|---|
| 57 | 1 | __rmul__ = __mul__ |
|---|
| 58 | 1 | def __mod__(self, args): |
|---|
| 59 | 12058 | return self.__class__(self.data % args) |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | # the following methods are defined in alphabetical order: |
|---|
| 62 | 13 | def capitalize(self): return self.__class__(self.data.capitalize()) |
|---|
| 63 | 1 | def center(self, width, *args): |
|---|
| 64 | 10 | return self.__class__(self.data.center(width, *args)) |
|---|
| 65 | 1 | def count(self, sub, start=0, end=sys.maxint): |
|---|
| 66 | 130104 | return self.data.count(sub, start, end) |
|---|
| 67 | 1 | def decode(self, encoding=None, errors=None): # XXX improve this? |
|---|
| 68 | 12 | if encoding: |
|---|
| 69 | 12 | if errors: |
|---|
| 70 | 0 | return self.__class__(self.data.decode(encoding, errors)) |
|---|
| 71 | n/a | else: |
|---|
| 72 | 12 | return self.__class__(self.data.decode(encoding)) |
|---|
| 73 | n/a | else: |
|---|
| 74 | 0 | return self.__class__(self.data.decode()) |
|---|
| 75 | 1 | def encode(self, encoding=None, errors=None): # XXX improve this? |
|---|
| 76 | 12 | if encoding: |
|---|
| 77 | 12 | if errors: |
|---|
| 78 | 0 | return self.__class__(self.data.encode(encoding, errors)) |
|---|
| 79 | n/a | else: |
|---|
| 80 | 12 | return self.__class__(self.data.encode(encoding)) |
|---|
| 81 | n/a | else: |
|---|
| 82 | 0 | return self.__class__(self.data.encode()) |
|---|
| 83 | 1 | def endswith(self, suffix, start=0, end=sys.maxint): |
|---|
| 84 | 76 | return self.data.endswith(suffix, start, end) |
|---|
| 85 | 1 | def expandtabs(self, tabsize=8): |
|---|
| 86 | 16 | return self.__class__(self.data.expandtabs(tabsize)) |
|---|
| 87 | 1 | def find(self, sub, start=0, end=sys.maxint): |
|---|
| 88 | 265030 | return self.data.find(sub, start, end) |
|---|
| 89 | 1 | def index(self, sub, start=0, end=sys.maxint): |
|---|
| 90 | 28 | return self.data.index(sub, start, end) |
|---|
| 91 | 15 | def isalpha(self): return self.data.isalpha() |
|---|
| 92 | 17 | def isalnum(self): return self.data.isalnum() |
|---|
| 93 | 1 | def isdecimal(self): return self.data.isdecimal() |
|---|
| 94 | 11 | def isdigit(self): return self.data.isdigit() |
|---|
| 95 | 23 | def islower(self): return self.data.islower() |
|---|
| 96 | 1 | def isnumeric(self): return self.data.isnumeric() |
|---|
| 97 | 17 | def isspace(self): return self.data.isspace() |
|---|
| 98 | 23 | def istitle(self): return self.data.istitle() |
|---|
| 99 | 19 | def isupper(self): return self.data.isupper() |
|---|
| 100 | 45 | def join(self, seq): return self.data.join(seq) |
|---|
| 101 | 1 | def ljust(self, width, *args): |
|---|
| 102 | 10 | return self.__class__(self.data.ljust(width, *args)) |
|---|
| 103 | 7 | def lower(self): return self.__class__(self.data.lower()) |
|---|
| 104 | 9 | def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars)) |
|---|
| 105 | 1 | def partition(self, sep): |
|---|
| 106 | 14 | return self.data.partition(sep) |
|---|
| 107 | 1 | def replace(self, old, new, maxsplit=-1): |
|---|
| 108 | 129800 | return self.__class__(self.data.replace(old, new, maxsplit)) |
|---|
| 109 | 1 | def rfind(self, sub, start=0, end=sys.maxint): |
|---|
| 110 | 265020 | return self.data.rfind(sub, start, end) |
|---|
| 111 | 1 | def rindex(self, sub, start=0, end=sys.maxint): |
|---|
| 112 | 30 | return self.data.rindex(sub, start, end) |
|---|
| 113 | 1 | def rjust(self, width, *args): |
|---|
| 114 | 10 | return self.__class__(self.data.rjust(width, *args)) |
|---|
| 115 | 1 | def rpartition(self, sep): |
|---|
| 116 | 14 | return self.data.rpartition(sep) |
|---|
| 117 | 9 | def rstrip(self, chars=None): return self.__class__(self.data.rstrip(chars)) |
|---|
| 118 | 1 | def split(self, sep=None, maxsplit=-1): |
|---|
| 119 | 122 | return self.data.split(sep, maxsplit) |
|---|
| 120 | 1 | def rsplit(self, sep=None, maxsplit=-1): |
|---|
| 121 | 120 | return self.data.rsplit(sep, maxsplit) |
|---|
| 122 | 15 | def splitlines(self, keepends=0): return self.data.splitlines(keepends) |
|---|
| 123 | 1 | def startswith(self, prefix, start=0, end=sys.maxint): |
|---|
| 124 | 68 | return self.data.startswith(prefix, start, end) |
|---|
| 125 | 15 | def strip(self, chars=None): return self.__class__(self.data.strip(chars)) |
|---|
| 126 | 5 | def swapcase(self): return self.__class__(self.data.swapcase()) |
|---|
| 127 | 15 | def title(self): return self.__class__(self.data.title()) |
|---|
| 128 | 1 | def translate(self, *args): |
|---|
| 129 | 18 | return self.__class__(self.data.translate(*args)) |
|---|
| 130 | 7 | def upper(self): return self.__class__(self.data.upper()) |
|---|
| 131 | 25 | def zfill(self, width): return self.__class__(self.data.zfill(width)) |
|---|
| 132 | n/a | |
|---|
| 133 | 2 | class MutableString(UserString, collections.MutableSequence): |
|---|
| 134 | n/a | """mutable string objects |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | Python strings are immutable objects. This has the advantage, that |
|---|
| 137 | n/a | strings may be used as dictionary keys. If this property isn't needed |
|---|
| 138 | n/a | and you insist on changing string values in place instead, you may cheat |
|---|
| 139 | n/a | and use MutableString. |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | But the purpose of this class is an educational one: to prevent |
|---|
| 142 | n/a | people from inventing their own mutable string class derived |
|---|
| 143 | n/a | from UserString and than forget thereby to remove (override) the |
|---|
| 144 | n/a | __hash__ method inherited from UserString. This would lead to |
|---|
| 145 | n/a | errors that would be very hard to track down. |
|---|
| 146 | n/a | |
|---|
| 147 | 1 | A faster and better solution is to rewrite your program using lists.""" |
|---|
| 148 | 1 | def __init__(self, string=""): |
|---|
| 149 | 88899 | from warnings import warnpy3k |
|---|
| 150 | 88899 | warnpy3k('the class UserString.MutableString has been removed in ' |
|---|
| 151 | 88899 | 'Python 3.0', stacklevel=2) |
|---|
| 152 | 88899 | self.data = string |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | # We inherit object.__hash__, so we must deny this explicitly |
|---|
| 155 | 1 | __hash__ = None |
|---|
| 156 | n/a | |
|---|
| 157 | 1 | def __setitem__(self, index, sub): |
|---|
| 158 | 304 | if isinstance(index, slice): |
|---|
| 159 | 300 | if isinstance(sub, UserString): |
|---|
| 160 | 0 | sub = sub.data |
|---|
| 161 | 300 | elif not isinstance(sub, basestring): |
|---|
| 162 | 0 | sub = str(sub) |
|---|
| 163 | 300 | start, stop, step = index.indices(len(self.data)) |
|---|
| 164 | 300 | if step == -1: |
|---|
| 165 | 100 | start, stop = stop+1, start+1 |
|---|
| 166 | 100 | sub = sub[::-1] |
|---|
| 167 | 200 | elif step != 1: |
|---|
| 168 | n/a | # XXX(twouters): I guess we should be reimplementing |
|---|
| 169 | n/a | # the extended slice assignment/deletion algorithm here... |
|---|
| 170 | 0 | raise TypeError, "invalid step in slicing assignment" |
|---|
| 171 | 300 | start = min(start, stop) |
|---|
| 172 | 300 | self.data = self.data[:start] + sub + self.data[stop:] |
|---|
| 173 | n/a | else: |
|---|
| 174 | 4 | if index < 0: |
|---|
| 175 | 2 | index += len(self.data) |
|---|
| 176 | 4 | if index < 0 or index >= len(self.data): raise IndexError |
|---|
| 177 | 2 | self.data = self.data[:index] + sub + self.data[index+1:] |
|---|
| 178 | 1 | def __delitem__(self, index): |
|---|
| 179 | 305 | if isinstance(index, slice): |
|---|
| 180 | 300 | start, stop, step = index.indices(len(self.data)) |
|---|
| 181 | 300 | if step == -1: |
|---|
| 182 | 100 | start, stop = stop+1, start+1 |
|---|
| 183 | 200 | elif step != 1: |
|---|
| 184 | n/a | # XXX(twouters): see same block in __setitem__ |
|---|
| 185 | 0 | raise TypeError, "invalid step in slicing deletion" |
|---|
| 186 | 300 | start = min(start, stop) |
|---|
| 187 | 300 | self.data = self.data[:start] + self.data[stop:] |
|---|
| 188 | n/a | else: |
|---|
| 189 | 5 | if index < 0: |
|---|
| 190 | 2 | index += len(self.data) |
|---|
| 191 | 5 | if index < 0 or index >= len(self.data): raise IndexError |
|---|
| 192 | 3 | self.data = self.data[:index] + self.data[index+1:] |
|---|
| 193 | 1 | def __setslice__(self, start, end, sub): |
|---|
| 194 | 4 | start = max(start, 0); end = max(end, 0) |
|---|
| 195 | 4 | if isinstance(sub, UserString): |
|---|
| 196 | 1 | self.data = self.data[:start]+sub.data+self.data[end:] |
|---|
| 197 | 3 | elif isinstance(sub, basestring): |
|---|
| 198 | 2 | self.data = self.data[:start]+sub+self.data[end:] |
|---|
| 199 | n/a | else: |
|---|
| 200 | 1 | self.data = self.data[:start]+str(sub)+self.data[end:] |
|---|
| 201 | 1 | def __delslice__(self, start, end): |
|---|
| 202 | 2 | start = max(start, 0); end = max(end, 0) |
|---|
| 203 | 2 | self.data = self.data[:start] + self.data[end:] |
|---|
| 204 | 1 | def immutable(self): |
|---|
| 205 | 1 | return UserString(self.data) |
|---|
| 206 | 1 | def __iadd__(self, other): |
|---|
| 207 | 3 | if isinstance(other, UserString): |
|---|
| 208 | 1 | self.data += other.data |
|---|
| 209 | 2 | elif isinstance(other, basestring): |
|---|
| 210 | 1 | self.data += other |
|---|
| 211 | n/a | else: |
|---|
| 212 | 1 | self.data += str(other) |
|---|
| 213 | 3 | return self |
|---|
| 214 | 1 | def __imul__(self, n): |
|---|
| 215 | 3 | self.data *= n |
|---|
| 216 | 3 | return self |
|---|
| 217 | 1 | def insert(self, index, value): |
|---|
| 218 | 0 | self[index:index] = value |
|---|
| 219 | n/a | |
|---|
| 220 | 1 | if __name__ == "__main__": |
|---|
| 221 | n/a | # execute the regression test to stdout, if called as a script: |
|---|
| 222 | 0 | import os |
|---|
| 223 | 0 | called_in_dir, called_as = os.path.split(sys.argv[0]) |
|---|
| 224 | 0 | called_as, py = os.path.splitext(called_as) |
|---|
| 225 | 0 | if '-q' in sys.argv: |
|---|
| 226 | 0 | from test import test_support |
|---|
| 227 | 0 | test_support.verbose = 0 |
|---|
| 228 | 0 | __import__('test.test_' + called_as.lower()) |
|---|