Mercurial > cpython-lru_cache-weakref
annotate Lib/UserString.py @ 14164:dba4aec970e6 legacy-trunk
Support for augmented assignment in the UserList, UserDict, UserString and
rfc822 (Addresslist) modules. Also a preliminary testcase for augmented
assignment, which should actually be merged with the test_class testcase I
added last week.
| author | Thomas Wouters <thomas@python.org> |
|---|---|
| date | Thu, 24 Aug 2000 20:14:10 +0000 |
| parents | 3148efe5606f |
| children | 51a33a3b2ba9 |
| rev | line source |
|---|---|
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
2 ## vim:ts=4:et:nowrap |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
3 """A user-defined wrapper around string objects |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
4 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
5 Note: string objects have grown methods in Python 1.6 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
6 This module requires Python 1.6 or later. |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
7 """ |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
8 from types import StringType, UnicodeType |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 import sys |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
10 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
11 class UserString: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
12 def __init__(self, seq): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
13 if isinstance(seq, StringType) or isinstance(seq, UnicodeType): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
14 self.data = seq |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 elif isinstance(seq, UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 self.data = seq.data[:] |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
17 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
18 self.data = str(seq) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
19 def __str__(self): return str(self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
20 def __repr__(self): return repr(self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
21 def __int__(self): return int(self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
22 def __long__(self): return long(self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
23 def __float__(self): return float(self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
24 def __complex__(self): return complex(self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
25 def __hash__(self): return hash(self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
26 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
27 def __cmp__(self, string): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
28 if isinstance(string, UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
29 return cmp(self.data, string.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
30 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
31 return cmp(self.data, string) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
32 def __contains__(self, char): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
33 return char in self.data |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
34 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
35 def __len__(self): return len(self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
36 def __getitem__(self, index): return self.__class__(self.data[index]) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
37 def __getslice__(self, start, end): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
38 start = max(start, 0); end = max(end, 0) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
39 return self.__class__(self.data[start:end]) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
40 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
41 def __add__(self, other): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
42 if isinstance(other, UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
43 return self.__class__(self.data + other.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
44 elif isinstance(other, StringType) or isinstance(other, UnicodeType): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
45 return self.__class__(self.data + other) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
46 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
47 return self.__class__(self.data + str(other)) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
48 def __radd__(self, other): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
49 if isinstance(other, StringType) or isinstance(other, UnicodeType): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
50 return self.__class__(other + self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
51 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
52 return self.__class__(str(other) + self.data) |
|
14164
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
53 def __iadd__(self, other): |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
54 if isinstance(other, UserString): |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
55 self.data += other.data |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
56 elif isinstance(other, StringType) or isinstance(other, UnicodeType): |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
57 self.data += other |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
58 else |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
59 self.data += str(other) |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
60 return self |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
61 def __mul__(self, n): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
62 return self.__class__(self.data*n) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
63 __rmul__ = __mul__ |
|
14164
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
64 def __imull__(self, n): |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
65 self.data += n |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
66 return self |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
67 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
68 # the following methods are defined in alphabetical order: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 def capitalize(self): return self.__class__(self.data.capitalize()) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
70 def center(self, width): return self.__class__(self.data.center(width)) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
71 def count(self, sub, start=0, end=sys.maxint): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
72 return self.data.count(sub, start, end) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
73 def encode(self, encoding=None, errors=None): # XXX improve this? |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
74 if encoding: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
75 if errors: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
76 return self.__class__(self.data.encode(encoding, errors)) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
77 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
78 return self.__class__(self.data.encode(encoding)) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
79 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
80 return self.__class__(self.data.encode()) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
81 def endswith(self, suffix, start=0, end=sys.maxint): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
82 return self.data.endswith(suffix, start, end) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
83 def expandtabs(self, tabsize=8): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
84 return self.__class__(self.data.expandtabs(tabsize)) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
85 def find(self, sub, start=0, end=sys.maxint): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
86 return self.data.find(sub, start, end) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
87 def index(self, sub, start=0, end=sys.maxint): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
88 return self.data.index(sub, start, end) |
|
13516
e44ca61db715
add isalpha and isalnum methods
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12369
diff
changeset
|
89 def isalpha(self): return self.data.isalpha() |
|
e44ca61db715
add isalpha and isalnum methods
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12369
diff
changeset
|
90 def isalnum(self): return self.data.isalnum() |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
91 def isdecimal(self): return self.data.isdecimal() |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
92 def isdigit(self): return self.data.isdigit() |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
93 def islower(self): return self.data.islower() |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
94 def isnumeric(self): return self.data.isnumeric() |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
95 def isspace(self): return self.data.isspace() |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
96 def istitle(self): return self.data.istitle() |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
97 def isupper(self): return self.data.isupper() |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
98 def join(self, seq): return self.data.join(seq) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
99 def ljust(self, width): return self.__class__(self.data.ljust(width)) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
100 def lower(self): return self.__class__(self.data.lower()) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
101 def lstrip(self): return self.__class__(self.data.lstrip()) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
102 def replace(self, old, new, maxsplit=-1): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
103 return self.__class__(self.data.replace(old, new, maxsplit)) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
104 def rfind(self, sub, start=0, end=sys.maxint): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
105 return self.data.rfind(sub, start, end) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
106 def rindex(self, sub, start=0, end=sys.maxint): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
107 return self.data.rindex(sub, start, end) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
108 def rjust(self, width): return self.__class__(self.data.rjust(width)) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
109 def rstrip(self): return self.__class__(self.data.rstrip()) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
110 def split(self, sep=None, maxsplit=-1): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
111 return self.data.split(sep, maxsplit) |
| 12369 | 112 def splitlines(self, keepends=0): return self.data.splitlines(keepends) |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
113 def startswith(self, prefix, start=0, end=sys.maxint): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
114 return self.data.startswith(prefix, start, end) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
115 def strip(self): return self.__class__(self.data.strip()) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
116 def swapcase(self): return self.__class__(self.data.swapcase()) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
117 def title(self): return self.__class__(self.data.title()) |
|
14109
3148efe5606f
Denis S. Otkidach <den@analyt.chem.msu.ru>:
Fred Drake <fdrake@acm.org>
parents:
13516
diff
changeset
|
118 def translate(self, *args): |
|
3148efe5606f
Denis S. Otkidach <den@analyt.chem.msu.ru>:
Fred Drake <fdrake@acm.org>
parents:
13516
diff
changeset
|
119 return self.__class__(self.data.translate(*args)) |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
120 def upper(self): return self.__class__(self.data.upper()) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
121 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
122 class MutableString(UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
123 """mutable string objects |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
124 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
125 Python strings are immutable objects. This has the advantage, that |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
126 strings may be used as dictionary keys. If this property isn't needed |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
127 and you insist on changing string values in place instead, you may cheat |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
128 and use MutableString. |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
129 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
130 But the purpose of this class is an educational one: to prevent |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
131 people from inventing their own mutable string class derived |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
132 from UserString and than forget thereby to remove (override) the |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
133 __hash__ method inherited from ^UserString. This would lead to |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
134 errors that would be very hard to track down. |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
135 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
136 A faster and better solution is to rewrite your program using lists.""" |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
137 def __init__(self, string=""): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
138 self.data = string |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
139 def __hash__(self): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
140 raise TypeError, "unhashable type (it is mutable)" |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
141 def __setitem__(self, index, sub): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
142 if index < 0 or index >= len(self.data): raise IndexError |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
143 self.data = self.data[:index] + sub + self.data[index+1:] |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
144 def __delitem__(self, index): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
145 if index < 0 or index >= len(self.data): raise IndexError |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
146 self.data = self.data[:index] + self.data[index+1:] |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
147 def __setslice__(self, start, end, sub): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
148 start = max(start, 0); end = max(end, 0) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
149 if isinstance(sub, UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
150 self.data = self.data[:start]+sub.data+self.data[end:] |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
151 elif isinstance(sub, StringType) or isinstance(sub, UnicodeType): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
152 self.data = self.data[:start]+sub+self.data[end:] |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
153 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
154 self.data = self.data[:start]+str(sub)+self.data[end:] |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
155 def __delslice__(self, start, end): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
156 start = max(start, 0); end = max(end, 0) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
157 self.data = self.data[:start] + self.data[end:] |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
158 def immutable(self): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
159 return UserString(self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
160 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
161 if __name__ == "__main__": |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
162 # execute the regression test to stdout, if called as a script: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
163 import os |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
164 called_in_dir, called_as = os.path.split(sys.argv[0]) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
165 called_in_dir = os.path.abspath(called_in_dir) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
166 called_as, py = os.path.splitext(called_as) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
167 sys.path.append(os.path.join(called_in_dir, 'test')) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
168 if '-q' in sys.argv: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
169 import test_support |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
170 test_support.verbose = 0 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
171 __import__('test_' + called_as.lower()) |
