3 """A user-defined wrapper around string objects
5 Note: string objects have grown methods in Python 1.6
6 This module requires Python 1.6 or later.
8 from types
import StringType
, UnicodeType
12 def __init__(self
, seq
):
13 if isinstance(seq
, StringType
) or isinstance(seq
, UnicodeType
):
15 elif isinstance(seq
, UserString
):
16 self
.data
= seq
.data
[:]
19 def __str__(self
): return str(self
.data
)
20 def __repr__(self
): return repr(self
.data
)
21 def __int__(self
): return int(self
.data
)
22 def __long__(self
): return long(self
.data
)
23 def __float__(self
): return float(self
.data
)
24 def __complex__(self
): return complex(self
.data
)
25 def __hash__(self
): return hash(self
.data
)
27 def __cmp__(self
, string
):
28 if isinstance(string
, UserString
):
29 return cmp(self
.data
, string
.data
)
31 return cmp(self
.data
, string
)
32 def __contains__(self
, char
):
33 return char
in self
.data
35 def __len__(self
): return len(self
.data
)
36 def __getitem__(self
, index
): return self
.__class
__(self
.data
[index
])
37 def __getslice__(self
, start
, end
):
38 start
= max(start
, 0); end
= max(end
, 0)
39 return self
.__class
__(self
.data
[start
:end
])
41 def __add__(self
, other
):
42 if isinstance(other
, UserString
):
43 return self
.__class
__(self
.data
+ other
.data
)
44 elif isinstance(other
, StringType
) or isinstance(other
, UnicodeType
):
45 return self
.__class
__(self
.data
+ other
)
47 return self
.__class
__(self
.data
+ str(other
))
48 def __radd__(self
, other
):
49 if isinstance(other
, StringType
) or isinstance(other
, UnicodeType
):
50 return self
.__class
__(other
+ self
.data
)
52 return self
.__class
__(str(other
) + self
.data
)
53 def __iadd__(self
, other
):
54 if isinstance(other
, UserString
):
55 self
.data
+= other
.data
56 elif isinstance(other
, StringType
) or isinstance(other
, UnicodeType
):
59 self
.data
+= str(other
)
62 return self
.__class
__(self
.data
*n
)
64 def __imull__(self
, n
):
68 # the following methods are defined in alphabetical order:
69 def capitalize(self
): return self
.__class
__(self
.data
.capitalize())
70 def center(self
, width
): return self
.__class
__(self
.data
.center(width
))
71 def count(self
, sub
, start
=0, end
=sys
.maxint
):
72 return self
.data
.count(sub
, start
, end
)
73 def encode(self
, encoding
=None, errors
=None): # XXX improve this?
76 return self
.__class
__(self
.data
.encode(encoding
, errors
))
78 return self
.__class
__(self
.data
.encode(encoding
))
80 return self
.__class
__(self
.data
.encode())
81 def endswith(self
, suffix
, start
=0, end
=sys
.maxint
):
82 return self
.data
.endswith(suffix
, start
, end
)
83 def expandtabs(self
, tabsize
=8):
84 return self
.__class
__(self
.data
.expandtabs(tabsize
))
85 def find(self
, sub
, start
=0, end
=sys
.maxint
):
86 return self
.data
.find(sub
, start
, end
)
87 def index(self
, sub
, start
=0, end
=sys
.maxint
):
88 return self
.data
.index(sub
, start
, end
)
89 def isalpha(self
): return self
.data
.isalpha()
90 def isalnum(self
): return self
.data
.isalnum()
91 def isdecimal(self
): return self
.data
.isdecimal()
92 def isdigit(self
): return self
.data
.isdigit()
93 def islower(self
): return self
.data
.islower()
94 def isnumeric(self
): return self
.data
.isnumeric()
95 def isspace(self
): return self
.data
.isspace()
96 def istitle(self
): return self
.data
.istitle()
97 def isupper(self
): return self
.data
.isupper()
98 def join(self
, seq
): return self
.data
.join(seq
)
99 def ljust(self
, width
): return self
.__class
__(self
.data
.ljust(width
))
100 def lower(self
): return self
.__class
__(self
.data
.lower())
101 def lstrip(self
): return self
.__class
__(self
.data
.lstrip())
102 def replace(self
, old
, new
, maxsplit
=-1):
103 return self
.__class
__(self
.data
.replace(old
, new
, maxsplit
))
104 def rfind(self
, sub
, start
=0, end
=sys
.maxint
):
105 return self
.data
.rfind(sub
, start
, end
)
106 def rindex(self
, sub
, start
=0, end
=sys
.maxint
):
107 return self
.data
.rindex(sub
, start
, end
)
108 def rjust(self
, width
): return self
.__class
__(self
.data
.rjust(width
))
109 def rstrip(self
): return self
.__class
__(self
.data
.rstrip())
110 def split(self
, sep
=None, maxsplit
=-1):
111 return self
.data
.split(sep
, maxsplit
)
112 def splitlines(self
, keepends
=0): return self
.data
.splitlines(keepends
)
113 def startswith(self
, prefix
, start
=0, end
=sys
.maxint
):
114 return self
.data
.startswith(prefix
, start
, end
)
115 def strip(self
): return self
.__class
__(self
.data
.strip())
116 def swapcase(self
): return self
.__class
__(self
.data
.swapcase())
117 def title(self
): return self
.__class
__(self
.data
.title())
118 def translate(self
, *args
):
119 return self
.__class
__(self
.data
.translate(*args
))
120 def upper(self
): return self
.__class
__(self
.data
.upper())
122 class MutableString(UserString
):
123 """mutable string objects
125 Python strings are immutable objects. This has the advantage, that
126 strings may be used as dictionary keys. If this property isn't needed
127 and you insist on changing string values in place instead, you may cheat
128 and use MutableString.
130 But the purpose of this class is an educational one: to prevent
131 people from inventing their own mutable string class derived
132 from UserString and than forget thereby to remove (override) the
133 __hash__ method inherited from ^UserString. This would lead to
134 errors that would be very hard to track down.
136 A faster and better solution is to rewrite your program using lists."""
137 def __init__(self
, string
=""):
140 raise TypeError, "unhashable type (it is mutable)"
141 def __setitem__(self
, index
, sub
):
142 if index
< 0 or index
>= len(self
.data
): raise IndexError
143 self
.data
= self
.data
[:index
] + sub
+ self
.data
[index
+1:]
144 def __delitem__(self
, index
):
145 if index
< 0 or index
>= len(self
.data
): raise IndexError
146 self
.data
= self
.data
[:index
] + self
.data
[index
+1:]
147 def __setslice__(self
, start
, end
, sub
):
148 start
= max(start
, 0); end
= max(end
, 0)
149 if isinstance(sub
, UserString
):
150 self
.data
= self
.data
[:start
]+sub
.data
+self
.data
[end
:]
151 elif isinstance(sub
, StringType
) or isinstance(sub
, UnicodeType
):
152 self
.data
= self
.data
[:start
]+sub
+self
.data
[end
:]
154 self
.data
= self
.data
[:start
]+str(sub
)+self
.data
[end
:]
155 def __delslice__(self
, start
, end
):
156 start
= max(start
, 0); end
= max(end
, 0)
157 self
.data
= self
.data
[:start
] + self
.data
[end
:]
159 return UserString(self
.data
)
161 if __name__
== "__main__":
162 # execute the regression test to stdout, if called as a script:
164 called_in_dir
, called_as
= os
.path
.split(sys
.argv
[0])
165 called_in_dir
= os
.path
.abspath(called_in_dir
)
166 called_as
, py
= os
.path
.splitext(called_as
)
167 sys
.path
.append(os
.path
.join(called_in_dir
, 'test'))
170 test_support
.verbose
= 0
171 __import__('test_' + called_as
.lower())