0001 #!/usr/bin/env python 0002 ## vim:ts=4:et:nowrap 0003 """A user-defined wrapper around string objects 0004 0005 Note: string objects have grown methods in Python 1.6 0006 This module requires Python 1.6 or later. 0007 """ 0008 from types import StringTypes 0009 import sys 0010 0011 __all__ = ["UserString","MutableString"] 0012 0013 class UserString: 0014 def __init__(self, seq): 0015 if isinstance(seq, StringTypes): 0016 self.data = seq 0017 elif isinstance(seq, UserString): 0018 self.data = seq.data[:] 0019 else: 0020 self.data = str(seq) 0021 def __str__(self): return str(self.data) 0022 def __repr__(self): return repr(self.data) 0023 def __int__(self): return int(self.data) 0024 def __long__(self): return long(self.data) 0025 def __float__(self): return float(self.data) 0026 def __complex__(self): return complex(self.data) 0027 def __hash__(self): return hash(self.data) 0028 0029 def __cmp__(self, string): 0030 if isinstance(string, UserString): 0031 return cmp(self.data, string.data) 0032 else: 0033 return cmp(self.data, string) 0034 def __contains__(self, char): 0035 return char in self.data 0036 0037 def __len__(self): return len(self.data) 0038 def __getitem__(self, index): return self.__class__(self.data[index]) 0039 def __getslice__(self, start, end): 0040 start = max(start, 0); end = max(end, 0) 0041 return self.__class__(self.data[start:end]) 0042 0043 def __add__(self, other): 0044 if isinstance(other, UserString): 0045 return self.__class__(self.data + other.data) 0046 elif isinstance(other, StringTypes): 0047 return self.__class__(self.data + other) 0048 else: 0049 return self.__class__(self.data + str(other)) 0050 def __radd__(self, other): 0051 if isinstance(other, StringTypes): 0052 return self.__class__(other + self.data) 0053 else: 0054 return self.__class__(str(other) + self.data) 0055 def __mul__(self, n): 0056 return self.__class__(self.data*n) 0057 __rmul__ = __mul__ 0058 def __mod__(self, args): 0059 return self.__class__(self.data % args) 0060 0061 # the following methods are defined in alphabetical order: 0062 def capitalize(self): return self.__class__(self.data.capitalize()) 0063 def center(self, width, *args): 0064 return self.__class__(self.data.center(width, *args)) 0065 def count(self, sub, start=0, end=sys.maxint): 0066 return self.data.count(sub, start, end) 0067 def decode(self, encoding=None, errors=None): # XXX improve this? 0068 if encoding: 0069 if errors: 0070 return self.__class__(self.data.decode(encoding, errors)) 0071 else: 0072 return self.__class__(self.data.decode(encoding)) 0073 else: 0074 return self.__class__(self.data.decode()) 0075 def encode(self, encoding=None, errors=None): # XXX improve this? 0076 if encoding: 0077 if errors: 0078 return self.__class__(self.data.encode(encoding, errors)) 0079 else: 0080 return self.__class__(self.data.encode(encoding)) 0081 else: 0082 return self.__class__(self.data.encode()) 0083 def endswith(self, suffix, start=0, end=sys.maxint): 0084 return self.data.endswith(suffix, start, end) 0085 def expandtabs(self, tabsize=8): 0086 return self.__class__(self.data.expandtabs(tabsize)) 0087 def find(self, sub, start=0, end=sys.maxint): 0088 return self.data.find(sub, start, end) 0089 def index(self, sub, start=0, end=sys.maxint): 0090 return self.data.index(sub, start, end) 0091 def isalpha(self): return self.data.isalpha() 0092 def isalnum(self): return self.data.isalnum() 0093 def isdecimal(self): return self.data.isdecimal() 0094 def isdigit(self): return self.data.isdigit() 0095 def islower(self): return self.data.islower() 0096 def isnumeric(self): return self.data.isnumeric() 0097 def isspace(self): return self.data.isspace() 0098 def istitle(self): return self.data.istitle() 0099 def isupper(self): return self.data.isupper() 0100 def join(self, seq): return self.data.join(seq) 0101 def ljust(self, width, *args): 0102 return self.__class__(self.data.ljust(width, *args)) 0103 def lower(self): return self.__class__(self.data.lower()) 0104 def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars)) 0105 def replace(self, old, new, maxsplit=-1): 0106 return self.__class__(self.data.replace(old, new, maxsplit)) 0107 def rfind(self, sub, start=0, end=sys.maxint): 0108 return self.data.rfind(sub, start, end) 0109 def rindex(self, sub, start=0, end=sys.maxint): 0110 return self.data.rindex(sub, start, end) 0111 def rjust(self, width, *args): 0112 return self.__class__(self.data.rjust(width, *args)) 0113 def rstrip(self, chars=None): return self.__class__(self.data.rstrip(chars)) 0114 def split(self, sep=None, maxsplit=-1): 0115 return self.data.split(sep, maxsplit) 0116 def rsplit(self, sep=None, maxsplit=-1): 0117 return self.data.rsplit(sep, maxsplit) 0118 def splitlines(self, keepends=0): return self.data.splitlines(keepends) 0119 def startswith(self, prefix, start=0, end=sys.maxint): 0120 return self.data.startswith(prefix, start, end) 0121 def strip(self, chars=None): return self.__class__(self.data.strip(chars)) 0122 def swapcase(self): return self.__class__(self.data.swapcase()) 0123 def title(self): return self.__class__(self.data.title()) 0124 def translate(self, *args): 0125 return self.__class__(self.data.translate(*args)) 0126 def upper(self): return self.__class__(self.data.upper()) 0127 def zfill(self, width): return self.__class__(self.data.zfill(width)) 0128 0129 class MutableString(UserString): 0130 """mutable string objects 0131 0132 Python strings are immutable objects. This has the advantage, that 0133 strings may be used as dictionary keys. If this property isn't needed 0134 and you insist on changing string values in place instead, you may cheat 0135 and use MutableString. 0136 0137 But the purpose of this class is an educational one: to prevent 0138 people from inventing their own mutable string class derived 0139 from UserString and than forget thereby to remove (override) the 0140 __hash__ method inherited from UserString. This would lead to 0141 errors that would be very hard to track down. 0142 0143 A faster and better solution is to rewrite your program using lists.""" 0144 def __init__(self, string=""): 0145 self.data = string 0146 def __hash__(self): 0147 raise TypeError, "unhashable type (it is mutable)" 0148 def __setitem__(self, index, sub): 0149 if index < 0 or index >= len(self.data): raise IndexError 0150 self.data = self.data[:index] + sub + self.data[index+1:] 0151 def __delitem__(self, index): 0152 if index < 0 or index >= len(self.data): raise IndexError 0153 self.data = self.data[:index] + self.data[index+1:] 0154 def __setslice__(self, start, end, sub): 0155 start = max(start, 0); end = max(end, 0) 0156 if isinstance(sub, UserString): 0157 self.data = self.data[:start]+sub.data+self.data[end:] 0158 elif isinstance(sub, StringTypes): 0159 self.data = self.data[:start]+sub+self.data[end:] 0160 else: 0161 self.data = self.data[:start]+str(sub)+self.data[end:] 0162 def __delslice__(self, start, end): 0163 start = max(start, 0); end = max(end, 0) 0164 self.data = self.data[:start] + self.data[end:] 0165 def immutable(self): 0166 return UserString(self.data) 0167 def __iadd__(self, other): 0168 if isinstance(other, UserString): 0169 self.data += other.data 0170 elif isinstance(other, StringTypes): 0171 self.data += other 0172 else: 0173 self.data += str(other) 0174 return self 0175 def __imul__(self, n): 0176 self.data *= n 0177 return self 0178 0179 if __name__ == "__main__": 0180 # execute the regression test to stdout, if called as a script: 0181 import os 0182 called_in_dir, called_as = os.path.split(sys.argv[0]) 0183 called_as, py = os.path.splitext(called_as) 0184 if '-q' in sys.argv: 0185 from test import test_support 0186 test_support.verbose = 0 0187 __import__('test.test_' + called_as.lower()) 0188
Generated by PyXR 0.9.4