Mercurial > cpython-lru_cache-weakref
annotate Lib/UserString.py @ 44685:e511ccf2ffbe legacy-trunk
Tighten the definition of a named tuple.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Tue, 15 Jan 2008 03:07:42 +0000 |
| parents | 9620134010a5 |
| children | feb6dd36d53f |
| 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 import sys |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15874
diff
changeset
|
10 __all__ = ["UserString","MutableString"] |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15874
diff
changeset
|
11 |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
12 class UserString: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
13 def __init__(self, seq): |
|
38761
41570f099a4c
Cleanup: Remove import of types to get StringTypes, we can just use basestring.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38399
diff
changeset
|
14 if isinstance(seq, basestring): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 self.data = seq |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 elif isinstance(seq, UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
17 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
|
18 else: |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
19 self.data = str(seq) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
20 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
28 def __cmp__(self, string): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
29 if isinstance(string, UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
30 return cmp(self.data, string.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
31 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
32 return cmp(self.data, string) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
33 def __contains__(self, char): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
34 return char in self.data |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
35 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
36 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
|
37 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
|
38 def __getslice__(self, start, end): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
39 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
|
40 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
|
41 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
42 def __add__(self, other): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
43 if isinstance(other, UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
44 return self.__class__(self.data + other.data) |
|
38761
41570f099a4c
Cleanup: Remove import of types to get StringTypes, we can just use basestring.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38399
diff
changeset
|
45 elif isinstance(other, basestring): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
46 return self.__class__(self.data + other) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
47 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
48 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
|
49 def __radd__(self, other): |
|
38761
41570f099a4c
Cleanup: Remove import of types to get StringTypes, we can just use basestring.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38399
diff
changeset
|
50 if isinstance(other, basestring): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
51 return self.__class__(other + self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
52 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
53 return self.__class__(str(other) + self.data) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
54 def __mul__(self, n): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
55 return self.__class__(self.data*n) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
56 __rmul__ = __mul__ |
|
26277
3ac464c37374
Add __mod__ method to UserString.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24684
diff
changeset
|
57 def __mod__(self, args): |
|
3ac464c37374
Add __mod__ method to UserString.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24684
diff
changeset
|
58 return self.__class__(self.data % args) |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
59 |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
60 # 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
|
61 def capitalize(self): return self.__class__(self.data.capitalize()) |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30218
diff
changeset
|
62 def center(self, width, *args): |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30218
diff
changeset
|
63 return self.__class__(self.data.center(width, *args)) |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 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
|
65 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
|
66 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
|
67 if encoding: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
68 if errors: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
69 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
|
70 else: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
71 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
|
72 else: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
73 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
|
74 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
|
75 if encoding: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
76 if errors: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
77 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
|
78 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
79 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
|
80 else: |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
81 return self.__class__(self.data.encode()) |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
82 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
|
83 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
|
84 def expandtabs(self, tabsize=8): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
85 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
|
86 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
|
87 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
|
88 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
|
89 return self.data.index(sub, start, end) |
|
13516
e44ca61db715
add isalpha and isalnum methods
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12369
diff
changeset
|
90 def isalpha(self): return self.data.isalpha() |
|
e44ca61db715
add isalpha and isalnum methods
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12369
diff
changeset
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 def join(self, seq): return self.data.join(seq) |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30218
diff
changeset
|
100 def ljust(self, width, *args): |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30218
diff
changeset
|
101 return self.__class__(self.data.ljust(width, *args)) |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
102 def lower(self): return self.__class__(self.data.lower()) |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26277
diff
changeset
|
103 def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars)) |
|
38399
dbb195c9dd88
added rpartition method to UserString class
Fredrik Lundh <fredrik@pythonware.com>
parents:
38344
diff
changeset
|
104 def partition(self, sep): |
|
dbb195c9dd88
added rpartition method to UserString class
Fredrik Lundh <fredrik@pythonware.com>
parents:
38344
diff
changeset
|
105 return self.data.partition(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
|
106 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
|
107 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
|
108 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
|
109 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
|
110 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
|
111 return self.data.rindex(sub, start, end) |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30218
diff
changeset
|
112 def rjust(self, width, *args): |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30218
diff
changeset
|
113 return self.__class__(self.data.rjust(width, *args)) |
|
38399
dbb195c9dd88
added rpartition method to UserString class
Fredrik Lundh <fredrik@pythonware.com>
parents:
38344
diff
changeset
|
114 def rpartition(self, sep): |
|
dbb195c9dd88
added rpartition method to UserString class
Fredrik Lundh <fredrik@pythonware.com>
parents:
38344
diff
changeset
|
115 return self.data.rpartition(sep) |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26277
diff
changeset
|
116 def rstrip(self, chars=None): return self.__class__(self.data.rstrip(chars)) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
117 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
|
118 return self.data.split(sep, maxsplit) |
|
31202
02ebe3456595
Add rsplit method for UserString, too.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
119 def rsplit(self, sep=None, maxsplit=-1): |
|
02ebe3456595
Add rsplit method for UserString, too.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
120 return self.data.rsplit(sep, maxsplit) |
| 12369 | 121 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
|
122 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
|
123 return self.data.startswith(prefix, start, end) |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26277
diff
changeset
|
124 def strip(self, chars=None): return self.__class__(self.data.strip(chars)) |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
125 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
|
126 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
|
127 def translate(self, *args): |
|
14109
3148efe5606f
Denis S. Otkidach <den@analyt.chem.msu.ru>:
Fred Drake <fdrake@acm.org>
parents:
13516
diff
changeset
|
128 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
|
129 def upper(self): return self.__class__(self.data.upper()) |
|
23045
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
130 def zfill(self, width): return self.__class__(self.data.zfill(width)) |
|
12214
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 |
|
30218
8287f522bea1
Remove a typo. (Or is this some markup I don't understand? ;-)
Thomas Heller <theller@ctypes.org>
parents:
28575
diff
changeset
|
143 __hash__ method inherited from UserString. This would lead to |
|
12214
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): |
|
42951
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
152 if isinstance(index, slice): |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
153 if isinstance(sub, UserString): |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
154 sub = sub.data |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
155 elif not isinstance(sub, basestring): |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
156 sub = str(sub) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
157 start, stop, step = index.indices(len(self.data)) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
158 if step == -1: |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
159 start, stop = stop+1, start+1 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
160 sub = sub[::-1] |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
161 elif step != 1: |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
162 # XXX(twouters): I guess we should be reimplementing |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
163 # the extended slice assignment/deletion algorithm here... |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
164 raise TypeError, "invalid step in slicing assignment" |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
165 start = min(start, stop) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
166 self.data = self.data[:start] + sub + self.data[stop:] |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
167 else: |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
168 if index < 0: |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
169 index += len(self.data) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
170 if index < 0 or index >= len(self.data): raise IndexError |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
171 self.data = self.data[:index] + sub + self.data[index+1:] |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
172 def __delitem__(self, index): |
|
42951
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
173 if isinstance(index, slice): |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
174 start, stop, step = index.indices(len(self.data)) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
175 if step == -1: |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
176 start, stop = stop+1, start+1 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
177 elif step != 1: |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
178 # XXX(twouters): see same block in __setitem__ |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
179 raise TypeError, "invalid step in slicing deletion" |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
180 start = min(start, stop) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
181 self.data = self.data[:start] + self.data[stop:] |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
182 else: |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
183 if index < 0: |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
184 index += len(self.data) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
185 if index < 0 or index >= len(self.data): raise IndexError |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
38761
diff
changeset
|
186 self.data = self.data[:index] + self.data[index+1:] |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
187 def __setslice__(self, start, end, sub): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
188 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
|
189 if isinstance(sub, UserString): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
190 self.data = self.data[:start]+sub.data+self.data[end:] |
|
38761
41570f099a4c
Cleanup: Remove import of types to get StringTypes, we can just use basestring.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38399
diff
changeset
|
191 elif isinstance(sub, basestring): |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
192 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
|
193 else: |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
194 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
|
195 def __delslice__(self, start, end): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
196 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
|
197 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
|
198 def immutable(self): |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
199 return UserString(self.data) |
|
24684
274e42a691e0
Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents:
24442
diff
changeset
|
200 def __iadd__(self, other): |
|
274e42a691e0
Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents:
24442
diff
changeset
|
201 if isinstance(other, UserString): |
|
274e42a691e0
Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents:
24442
diff
changeset
|
202 self.data += other.data |
|
38761
41570f099a4c
Cleanup: Remove import of types to get StringTypes, we can just use basestring.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38399
diff
changeset
|
203 elif isinstance(other, basestring): |
|
24684
274e42a691e0
Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents:
24442
diff
changeset
|
204 self.data += other |
|
274e42a691e0
Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents:
24442
diff
changeset
|
205 else: |
|
274e42a691e0
Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents:
24442
diff
changeset
|
206 self.data += str(other) |
|
274e42a691e0
Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents:
24442
diff
changeset
|
207 return self |
|
274e42a691e0
Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents:
24442
diff
changeset
|
208 def __imul__(self, n): |
|
274e42a691e0
Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents:
24442
diff
changeset
|
209 self.data *= n |
|
274e42a691e0
Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents:
24442
diff
changeset
|
210 return self |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15335
diff
changeset
|
211 |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
212 if __name__ == "__main__": |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
213 # 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
|
214 import os |
|
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
215 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
|
216 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
|
217 if '-q' in sys.argv: |
|
24442
226ba34bd998
Complete the absolute import patch for the test suite. All relative
Barry Warsaw <barry@python.org>
parents:
23484
diff
changeset
|
218 from test import test_support |
|
12214
a216fe79ae00
UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
219 test_support.verbose = 0 |
|
24442
226ba34bd998
Complete the absolute import patch for the test suite. All relative
Barry Warsaw <barry@python.org>
parents:
23484
diff
changeset
|
220 __import__('test.test_' + called_as.lower()) |
