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
)
54 return self
.__class
__(self
.data
*n
)
57 # the following methods are defined in alphabetical order:
58 def capitalize(self
): return self
.__class
__(self
.data
.capitalize())
59 def center(self
, width
): return self
.__class
__(self
.data
.center(width
))
60 def count(self
, sub
, start
=0, end
=sys
.maxint
):
61 return self
.data
.count(sub
, start
, end
)
62 def encode(self
, encoding
=None, errors
=None): # XXX improve this?
65 return self
.__class
__(self
.data
.encode(encoding
, errors
))
67 return self
.__class
__(self
.data
.encode(encoding
))
69 return self
.__class
__(self
.data
.encode())
70 def endswith(self
, suffix
, start
=0, end
=sys
.maxint
):
71 return self
.data
.endswith(suffix
, start
, end
)
72 def expandtabs(self
, tabsize
=8):
73 return self
.__class
__(self
.data
.expandtabs(tabsize
))
74 def find(self
, sub
, start
=0, end
=sys
.maxint
):
75 return self
.data
.find(sub
, start
, end
)
76 def index(self
, sub
, start
=0, end
=sys
.maxint
):
77 return self
.data
.index(sub
, start
, end
)
78 def isdecimal(self
): return self
.data
.isdecimal()
79 def isdigit(self
): return self
.data
.isdigit()
80 def islower(self
): return self
.data
.islower()
81 def isnumeric(self
): return self
.data
.isnumeric()
82 def isspace(self
): return self
.data
.isspace()
83 def istitle(self
): return self
.data
.istitle()
84 def isupper(self
): return self
.data
.isupper()
85 def join(self
, seq
): return self
.data
.join(seq
)
86 def ljust(self
, width
): return self
.__class
__(self
.data
.ljust(width
))
87 def lower(self
): return self
.__class
__(self
.data
.lower())
88 def lstrip(self
): return self
.__class
__(self
.data
.lstrip())
89 def replace(self
, old
, new
, maxsplit
=-1):
90 return self
.__class
__(self
.data
.replace(old
, new
, maxsplit
))
91 def rfind(self
, sub
, start
=0, end
=sys
.maxint
):
92 return self
.data
.rfind(sub
, start
, end
)
93 def rindex(self
, sub
, start
=0, end
=sys
.maxint
):
94 return self
.data
.rindex(sub
, start
, end
)
95 def rjust(self
, width
): return self
.__class
__(self
.data
.rjust(width
))
96 def rstrip(self
): return self
.__class
__(self
.data
.rstrip())
97 def split(self
, sep
=None, maxsplit
=-1):
98 return self
.data
.split(sep
, maxsplit
)
99 def splitlines(self
, keepends
=0): return self
.data
.splitlines(keepends
)
100 def startswith(self
, prefix
, start
=0, end
=sys
.maxint
):
101 return self
.data
.startswith(prefix
, start
, end
)
102 def strip(self
): return self
.__class
__(self
.data
.strip())
103 def swapcase(self
): return self
.__class
__(self
.data
.swapcase())
104 def title(self
): return self
.__class
__(self
.data
.title())
105 def translate(self
, table
, deletechars
=""):
106 return self
.__class
__(self
.data
.translate(table
, deletechars
))
107 def upper(self
): return self
.__class
__(self
.data
.upper())
109 class MutableString(UserString
):
110 """mutable string objects
112 Python strings are immutable objects. This has the advantage, that
113 strings may be used as dictionary keys. If this property isn't needed
114 and you insist on changing string values in place instead, you may cheat
115 and use MutableString.
117 But the purpose of this class is an educational one: to prevent
118 people from inventing their own mutable string class derived
119 from UserString and than forget thereby to remove (override) the
120 __hash__ method inherited from ^UserString. This would lead to
121 errors that would be very hard to track down.
123 A faster and better solution is to rewrite your program using lists."""
124 def __init__(self
, string
=""):
127 raise TypeError, "unhashable type (it is mutable)"
128 def __setitem__(self
, index
, sub
):
129 if index
< 0 or index
>= len(self
.data
): raise IndexError
130 self
.data
= self
.data
[:index
] + sub
+ self
.data
[index
+1:]
131 def __delitem__(self
, index
):
132 if index
< 0 or index
>= len(self
.data
): raise IndexError
133 self
.data
= self
.data
[:index
] + self
.data
[index
+1:]
134 def __setslice__(self
, start
, end
, sub
):
135 start
= max(start
, 0); end
= max(end
, 0)
136 if isinstance(sub
, UserString
):
137 self
.data
= self
.data
[:start
]+sub
.data
+self
.data
[end
:]
138 elif isinstance(sub
, StringType
) or isinstance(sub
, UnicodeType
):
139 self
.data
= self
.data
[:start
]+sub
+self
.data
[end
:]
141 self
.data
= self
.data
[:start
]+str(sub
)+self
.data
[end
:]
142 def __delslice__(self
, start
, end
):
143 start
= max(start
, 0); end
= max(end
, 0)
144 self
.data
= self
.data
[:start
] + self
.data
[end
:]
146 return UserString(self
.data
)
148 if __name__
== "__main__":
149 # execute the regression test to stdout, if called as a script:
151 called_in_dir
, called_as
= os
.path
.split(sys
.argv
[0])
152 called_in_dir
= os
.path
.abspath(called_in_dir
)
153 called_as
, py
= os
.path
.splitext(called_as
)
154 sys
.path
.append(os
.path
.join(called_in_dir
, 'test'))
157 test_support
.verbose
= 0
158 __import__('test_' + called_as
.lower())