Mercurial > cpython-lru_cache-weakref
annotate Lib/UserString.py @ 23144:a36f9fb2c404 2.2
backport gvanrossum's patch:
Partially implement SF feature request 444708.
Add optional arg to string methods strip(), lstrip(), rstrip().
The optional arg specifies characters to delete.
Also for UserString.
Still to do:
- Misc/NEWS
- LaTeX docs (I did the docstrings though)
- Unicode methods, and Unicode support in the string methods.
Original patches were:
python/dist/src/Lib/UserString.py:1.11
| author | Anthony Baxter <anthonybaxter@gmail.com> |
|---|---|
| date | Thu, 18 Apr 2002 05:17:53 +0000 |
| parents | 9ea87c0a7c04 |
| children | 8b1268bc371a |
| 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 |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
5 Note: string objects have grown methods in Python 1.6 |
|
12214
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 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15874
diff
changeset
|
11 __all__ = ["UserString","MutableString"] |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15874
diff
changeset
|
12 |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
13 class UserString: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
14 def __init__(self, seq): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 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
|
16 self.data = seq |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
17 elif isinstance(seq, UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
18 self.data = seq.data[:] |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
19 else: |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
20 self.data = str(seq) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 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
|
28 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
29 def __cmp__(self, string): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
30 if isinstance(string, UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
31 return cmp(self.data, string.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
32 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
33 return cmp(self.data, string) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
34 def __contains__(self, char): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
35 return char in self.data |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
36 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
37 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
|
38 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
|
39 def __getslice__(self, start, end): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
40 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
|
41 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
|
42 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
43 def __add__(self, other): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
44 if isinstance(other, UserString): |
|
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.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
46 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
|
47 return self.__class__(self.data + other) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
48 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
49 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
|
50 def __radd__(self, other): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
51 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
|
52 return self.__class__(other + self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
53 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
54 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
|
55 def __iadd__(self, other): |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
56 if isinstance(other, UserString): |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
57 self.data += other.data |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
58 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
|
59 self.data += other |
|
14167
51a33a3b2ba9
simple typo that makes regression test test_userstring fail
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
14164
diff
changeset
|
60 else: |
|
14164
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
61 self.data += str(other) |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
62 return self |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
63 def __mul__(self, n): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 return self.__class__(self.data*n) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 __rmul__ = __mul__ |
|
15335
ec79800f4c27
Fix two typos in __imul__. Closes Bug #117745.
Guido van Rossum <guido@python.org>
parents:
14167
diff
changeset
|
66 def __imul__(self, n): |
|
ec79800f4c27
Fix two typos in __imul__. Closes Bug #117745.
Guido van Rossum <guido@python.org>
parents:
14167
diff
changeset
|
67 self.data *= n |
|
14164
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
14109
diff
changeset
|
68 return self |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
70 # 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
|
71 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
|
72 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
|
73 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
|
74 return self.data.count(sub, start, end) |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
75 def decode(self, encoding=None, errors=None): # XXX improve this? |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
76 if encoding: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
77 if errors: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
78 return self.__class__(self.data.decode(encoding, errors)) |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
79 else: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
80 return self.__class__(self.data.decode(encoding)) |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
81 else: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
82 return self.__class__(self.data.decode()) |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
83 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
|
84 if encoding: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
85 if errors: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
86 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
|
87 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
88 return self.__class__(self.data.encode(encoding)) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
89 else: |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
90 return self.__class__(self.data.encode()) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
91 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
|
92 return self.data.endswith(suffix, start, end) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
93 def expandtabs(self, tabsize=8): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
94 return self.__class__(self.data.expandtabs(tabsize)) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
95 def find(self, sub, start=0, end=sys.maxint): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
96 return self.data.find(sub, start, end) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
97 def index(self, sub, start=0, end=sys.maxint): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
98 return self.data.index(sub, start, end) |
|
13516
e44ca61db715
add isalpha and isalnum methods
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12369
diff
changeset
|
99 def isalpha(self): return self.data.isalpha() |
|
e44ca61db715
add isalpha and isalnum methods
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12369
diff
changeset
|
100 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
|
101 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
|
102 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
|
103 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 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
|
110 def lower(self): return self.__class__(self.data.lower()) |
|
23144
a36f9fb2c404
backport gvanrossum's patch:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
17835
diff
changeset
|
111 def lstrip(self, sep=None): return self.__class__(self.data.lstrip(sep)) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
112 def replace(self, old, new, maxsplit=-1): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
113 return self.__class__(self.data.replace(old, new, maxsplit)) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
114 def rfind(self, sub, start=0, end=sys.maxint): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
115 return self.data.rfind(sub, start, end) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
116 def rindex(self, sub, start=0, end=sys.maxint): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
117 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
|
118 def rjust(self, width): return self.__class__(self.data.rjust(width)) |
|
23144
a36f9fb2c404
backport gvanrossum's patch:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
17835
diff
changeset
|
119 def rstrip(self, sep=None): return self.__class__(self.data.rstrip(sep)) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
120 def split(self, sep=None, maxsplit=-1): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
121 return self.data.split(sep, maxsplit) |
| 12369 | 122 def splitlines(self, keepends=0): return self.data.splitlines(keepends) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
123 def startswith(self, prefix, start=0, end=sys.maxint): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
124 return self.data.startswith(prefix, start, end) |
|
23144
a36f9fb2c404
backport gvanrossum's patch:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
17835
diff
changeset
|
125 def strip(self, sep=None): return self.__class__(self.data.strip(sep)) |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
126 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
|
127 def title(self): return self.__class__(self.data.title()) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
128 def translate(self, *args): |
|
14109
3148efe5606f
Denis S. Otkidach <den@analyt.chem.msu.ru>:
Fred Drake <fdrake@acm.org>
parents:
13516
diff
changeset
|
129 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
|
130 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
|
131 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
132 class MutableString(UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
133 """mutable string objects |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
134 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 and use MutableString. |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
139 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
140 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
|
141 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
|
142 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
|
143 __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
|
144 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
|
145 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
146 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
|
147 def __init__(self, string=""): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
148 self.data = string |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
149 def __hash__(self): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
150 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
|
151 def __setitem__(self, index, sub): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
152 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
|
153 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
|
154 def __delitem__(self, index): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
155 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
|
156 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
|
157 def __setslice__(self, start, end, sub): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
158 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
|
159 if isinstance(sub, UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
160 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
|
161 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
|
162 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
|
163 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
164 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
|
165 def __delslice__(self, start, end): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
166 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
|
167 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
|
168 def immutable(self): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
169 return UserString(self.data) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
170 |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
171 if __name__ == "__main__": |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
172 # 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
|
173 import os |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
174 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
|
175 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
|
176 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
|
177 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
|
178 if '-q' in sys.argv: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
179 import test_support |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
180 test_support.verbose = 0 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
181 __import__('test_' + called_as.lower()) |
