Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ## vim:ts=4:et:nowrap |
| 3 | """A user-defined wrapper around string objects |
| 4 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 5 | Note: string objects have grown methods in Python 1.6 |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 6 | This module requires Python 1.6 or later. |
| 7 | """ |
Michael W. Hudson | f207277 | 2002-05-20 14:48:16 +0000 | [diff] [blame] | 8 | from types import StringTypes |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 9 | import sys |
| 10 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 11 | __all__ = ["UserString","MutableString"] |
| 12 | |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 13 | class UserString: |
| 14 | def __init__(self, seq): |
Michael W. Hudson | f207277 | 2002-05-20 14:48:16 +0000 | [diff] [blame] | 15 | if isinstance(seq, StringTypes): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 16 | self.data = seq |
| 17 | elif isinstance(seq, UserString): |
| 18 | self.data = seq.data[:] |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 19 | else: |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 20 | self.data = str(seq) |
| 21 | def __str__(self): return str(self.data) |
| 22 | def __repr__(self): return repr(self.data) |
| 23 | def __int__(self): return int(self.data) |
| 24 | def __long__(self): return long(self.data) |
| 25 | def __float__(self): return float(self.data) |
| 26 | def __complex__(self): return complex(self.data) |
| 27 | def __hash__(self): return hash(self.data) |
| 28 | |
| 29 | def __cmp__(self, string): |
| 30 | if isinstance(string, UserString): |
| 31 | return cmp(self.data, string.data) |
| 32 | else: |
| 33 | return cmp(self.data, string) |
| 34 | def __contains__(self, char): |
| 35 | return char in self.data |
| 36 | |
| 37 | def __len__(self): return len(self.data) |
| 38 | def __getitem__(self, index): return self.__class__(self.data[index]) |
| 39 | def __getslice__(self, start, end): |
| 40 | start = max(start, 0); end = max(end, 0) |
| 41 | return self.__class__(self.data[start:end]) |
| 42 | |
| 43 | def __add__(self, other): |
| 44 | if isinstance(other, UserString): |
| 45 | return self.__class__(self.data + other.data) |
Michael W. Hudson | f207277 | 2002-05-20 14:48:16 +0000 | [diff] [blame] | 46 | elif isinstance(other, StringTypes): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 47 | return self.__class__(self.data + other) |
| 48 | else: |
| 49 | return self.__class__(self.data + str(other)) |
| 50 | def __radd__(self, other): |
Michael W. Hudson | f207277 | 2002-05-20 14:48:16 +0000 | [diff] [blame] | 51 | if isinstance(other, StringTypes): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 52 | return self.__class__(other + self.data) |
| 53 | else: |
| 54 | return self.__class__(str(other) + self.data) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 55 | def __iadd__(self, other): |
| 56 | if isinstance(other, UserString): |
| 57 | self.data += other.data |
Michael W. Hudson | f207277 | 2002-05-20 14:48:16 +0000 | [diff] [blame] | 58 | elif isinstance(other, StringTypes): |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 59 | self.data += other |
Peter Schneider-Kamp | fa12e13 | 2000-08-24 21:47:34 +0000 | [diff] [blame] | 60 | else: |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 61 | self.data += str(other) |
| 62 | return self |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 63 | def __mul__(self, n): |
| 64 | return self.__class__(self.data*n) |
| 65 | __rmul__ = __mul__ |
Guido van Rossum | add8d86 | 2000-10-25 21:58:20 +0000 | [diff] [blame] | 66 | def __imul__(self, n): |
| 67 | self.data *= n |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 68 | return self |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 69 | |
| 70 | # the following methods are defined in alphabetical order: |
| 71 | def capitalize(self): return self.__class__(self.data.capitalize()) |
| 72 | def center(self, width): return self.__class__(self.data.center(width)) |
| 73 | def count(self, sub, start=0, end=sys.maxint): |
| 74 | return self.data.count(sub, start, end) |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 75 | def decode(self, encoding=None, errors=None): # XXX improve this? |
| 76 | if encoding: |
| 77 | if errors: |
| 78 | return self.__class__(self.data.decode(encoding, errors)) |
| 79 | else: |
| 80 | return self.__class__(self.data.decode(encoding)) |
| 81 | else: |
| 82 | return self.__class__(self.data.decode()) |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 83 | def encode(self, encoding=None, errors=None): # XXX improve this? |
| 84 | if encoding: |
| 85 | if errors: |
| 86 | return self.__class__(self.data.encode(encoding, errors)) |
| 87 | else: |
| 88 | return self.__class__(self.data.encode(encoding)) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 89 | else: |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 90 | return self.__class__(self.data.encode()) |
| 91 | def endswith(self, suffix, start=0, end=sys.maxint): |
| 92 | return self.data.endswith(suffix, start, end) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 93 | def expandtabs(self, tabsize=8): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 94 | return self.__class__(self.data.expandtabs(tabsize)) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 95 | def find(self, sub, start=0, end=sys.maxint): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 96 | return self.data.find(sub, start, end) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 97 | def index(self, sub, start=0, end=sys.maxint): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 98 | return self.data.index(sub, start, end) |
Jeremy Hylton | fd54757 | 2000-07-10 17:07:17 +0000 | [diff] [blame] | 99 | def isalpha(self): return self.data.isalpha() |
| 100 | def isalnum(self): return self.data.isalnum() |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 101 | def isdecimal(self): return self.data.isdecimal() |
| 102 | def isdigit(self): return self.data.isdigit() |
| 103 | def islower(self): return self.data.islower() |
| 104 | def isnumeric(self): return self.data.isnumeric() |
| 105 | def isspace(self): return self.data.isspace() |
| 106 | def istitle(self): return self.data.istitle() |
| 107 | def isupper(self): return self.data.isupper() |
| 108 | def join(self, seq): return self.data.join(seq) |
| 109 | def ljust(self, width): return self.__class__(self.data.ljust(width)) |
| 110 | def lower(self): return self.__class__(self.data.lower()) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 111 | def lstrip(self, sep=None): return self.__class__(self.data.lstrip(sep)) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 112 | def replace(self, old, new, maxsplit=-1): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 113 | return self.__class__(self.data.replace(old, new, maxsplit)) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 114 | def rfind(self, sub, start=0, end=sys.maxint): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 115 | return self.data.rfind(sub, start, end) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 116 | def rindex(self, sub, start=0, end=sys.maxint): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 117 | return self.data.rindex(sub, start, end) |
| 118 | def rjust(self, width): return self.__class__(self.data.rjust(width)) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 119 | def rstrip(self, sep=None): return self.__class__(self.data.rstrip(sep)) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 120 | def split(self, sep=None, maxsplit=-1): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 121 | return self.data.split(sep, maxsplit) |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 122 | def splitlines(self, keepends=0): return self.data.splitlines(keepends) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 123 | def startswith(self, prefix, start=0, end=sys.maxint): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 124 | return self.data.startswith(prefix, start, end) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 125 | def strip(self, sep=None): return self.__class__(self.data.strip(sep)) |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 126 | def swapcase(self): return self.__class__(self.data.swapcase()) |
| 127 | def title(self): return self.__class__(self.data.title()) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 128 | def translate(self, *args): |
Fred Drake | a893957 | 2000-08-21 21:47:20 +0000 | [diff] [blame] | 129 | return self.__class__(self.data.translate(*args)) |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 130 | def upper(self): return self.__class__(self.data.upper()) |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 131 | def zfill(self, width): return self.__class__(self.data.zfill(width)) |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 132 | |
| 133 | class MutableString(UserString): |
| 134 | """mutable string objects |
| 135 | |
| 136 | Python strings are immutable objects. This has the advantage, that |
| 137 | strings may be used as dictionary keys. If this property isn't needed |
| 138 | and you insist on changing string values in place instead, you may cheat |
| 139 | and use MutableString. |
| 140 | |
| 141 | But the purpose of this class is an educational one: to prevent |
| 142 | people from inventing their own mutable string class derived |
| 143 | from UserString and than forget thereby to remove (override) the |
| 144 | __hash__ method inherited from ^UserString. This would lead to |
| 145 | errors that would be very hard to track down. |
| 146 | |
| 147 | A faster and better solution is to rewrite your program using lists.""" |
| 148 | def __init__(self, string=""): |
| 149 | self.data = string |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 150 | def __hash__(self): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 151 | raise TypeError, "unhashable type (it is mutable)" |
| 152 | def __setitem__(self, index, sub): |
| 153 | if index < 0 or index >= len(self.data): raise IndexError |
| 154 | self.data = self.data[:index] + sub + self.data[index+1:] |
| 155 | def __delitem__(self, index): |
| 156 | if index < 0 or index >= len(self.data): raise IndexError |
| 157 | self.data = self.data[:index] + self.data[index+1:] |
| 158 | def __setslice__(self, start, end, sub): |
| 159 | start = max(start, 0); end = max(end, 0) |
| 160 | if isinstance(sub, UserString): |
| 161 | self.data = self.data[:start]+sub.data+self.data[end:] |
Michael W. Hudson | f207277 | 2002-05-20 14:48:16 +0000 | [diff] [blame] | 162 | elif isinstance(sub, StringTypes): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 163 | self.data = self.data[:start]+sub+self.data[end:] |
| 164 | else: |
| 165 | self.data = self.data[:start]+str(sub)+self.data[end:] |
| 166 | def __delslice__(self, start, end): |
| 167 | start = max(start, 0); end = max(end, 0) |
| 168 | self.data = self.data[:start] + self.data[end:] |
| 169 | def immutable(self): |
| 170 | return UserString(self.data) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 171 | |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 172 | if __name__ == "__main__": |
| 173 | # execute the regression test to stdout, if called as a script: |
| 174 | import os |
| 175 | called_in_dir, called_as = os.path.split(sys.argv[0]) |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 176 | called_as, py = os.path.splitext(called_as) |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 177 | if '-q' in sys.argv: |
Barry Warsaw | 408b6d3 | 2002-07-30 23:27:12 +0000 | [diff] [blame] | 178 | from test import test_support |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 179 | test_support.verbose = 0 |
Barry Warsaw | 408b6d3 | 2002-07-30 23:27:12 +0000 | [diff] [blame] | 180 | __import__('test.test_' + called_as.lower()) |