annotate Lib/UserString.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents f9b80a820a25
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
45212
feb6dd36d53f Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs.
Raymond Hettinger <python@rcn.com>
parents: 42951
diff changeset
9 import collections
12214
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
45212
feb6dd36d53f Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs.
Raymond Hettinger <python@rcn.com>
parents: 42951
diff changeset
13 class UserString(collections.Sequence):
12214
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14 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
15 if isinstance(seq, basestring):
12214
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)
38761
41570f099a4c Cleanup: Remove import of types to get StringTypes, we can just use basestring.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38399
diff changeset
46 elif isinstance(other, basestring):
12214
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):
38761
41570f099a4c Cleanup: Remove import of types to get StringTypes, we can just use basestring.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38399
diff changeset
51 if isinstance(other, basestring):
12214
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)
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 def __mul__(self, n):
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 return self.__class__(self.data*n)
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 __rmul__ = __mul__
26277
3ac464c37374 Add __mod__ method to UserString.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24684
diff changeset
58 def __mod__(self, args):
3ac464c37374 Add __mod__ method to UserString.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24684
diff changeset
59 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
60
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61 # 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
62 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
63 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
64 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
65 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
66 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
67 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
68 if encoding:
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 16070
diff changeset
69 if errors:
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 16070
diff changeset
70 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
71 else:
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 16070
diff changeset
72 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
73 else:
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 16070
diff changeset
74 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
75 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
76 if encoding:
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
77 if errors:
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, errors))
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(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
81 else:
12214
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82 return self.__class__(self.data.encode())
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
83 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
84 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
85 def expandtabs(self, tabsize=8):
12214
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86 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
87 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
88 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
89 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
90 return self.data.index(sub, start, end)
13516
e44ca61db715 add isalpha and isalnum methods
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12369
diff changeset
91 def isalpha(self): return self.data.isalpha()
e44ca61db715 add isalpha and isalnum methods
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12369
diff changeset
92 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
93 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 def partition(self, sep):
dbb195c9dd88 added rpartition method to UserString class
Fredrik Lundh <fredrik@pythonware.com>
parents: 38344
diff changeset
106 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
107 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
108 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
109 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
110 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
111 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
112 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
113 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
114 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
115 def rpartition(self, sep):
dbb195c9dd88 added rpartition method to UserString class
Fredrik Lundh <fredrik@pythonware.com>
parents: 38344
diff changeset
116 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
117 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
118 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
119 return self.data.split(sep, maxsplit)
31202
02ebe3456595 Add rsplit method for UserString, too.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
120 def rsplit(self, sep=None, maxsplit=-1):
02ebe3456595 Add rsplit method for UserString, too.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
121 return self.data.rsplit(sep, maxsplit)
12369
68e48bb94071 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12214
diff changeset
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)
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26277
diff changeset
125 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
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())
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
131 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
132
45212
feb6dd36d53f Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs.
Raymond Hettinger <python@rcn.com>
parents: 42951
diff changeset
133 class MutableString(UserString, collections.MutableSequence):
12214
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
134 """mutable string objects
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 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
137 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
138 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
139 and use MutableString.
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
140
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
141 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
142 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
143 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
144 __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
145 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
146
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
147 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
148 def __init__(self, string=""):
47362
156746a63a5c UserString.MutableString has been removed in Python 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 45354
diff changeset
149 from warnings import warnpy3k
156746a63a5c UserString.MutableString has been removed in Python 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 45354
diff changeset
150 warnpy3k('the class UserString.MutableString has been removed in '
156746a63a5c UserString.MutableString has been removed in Python 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 45354
diff changeset
151 'Python 3.0', stacklevel=2)
12214
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
152 self.data = string
47961
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47362
diff changeset
153
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47362
diff changeset
154 # We inherit object.__hash__, so we must deny this explicitly
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47362
diff changeset
155 __hash__ = None
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47362
diff changeset
156
12214
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
157 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
158 if isinstance(index, slice):
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
159 if isinstance(sub, UserString):
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
160 sub = sub.data
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
161 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
162 sub = str(sub)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
163 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
164 if step == -1:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
165 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
166 sub = sub[::-1]
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
167 elif step != 1:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
168 # 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
169 # 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
170 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
171 start = min(start, stop)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
172 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
173 else:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
174 if index < 0:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
175 index += len(self.data)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
176 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
177 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
178 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
179 if isinstance(index, slice):
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
180 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
181 if step == -1:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
182 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
183 elif step != 1:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
184 # 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
185 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
186 start = min(start, stop)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
187 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
188 else:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
189 if index < 0:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
190 index += len(self.data)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 38761
diff changeset
191 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
192 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
193 def __setslice__(self, start, end, sub):
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
194 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
195 if isinstance(sub, UserString):
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
196 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
197 elif isinstance(sub, basestring):
12214
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
198 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
199 else:
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
200 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
201 def __delslice__(self, start, end):
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
202 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
203 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
204 def immutable(self):
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
205 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
206 def __iadd__(self, other):
274e42a691e0 Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents: 24442
diff changeset
207 if isinstance(other, UserString):
274e42a691e0 Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents: 24442
diff changeset
208 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
209 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
210 self.data += other
274e42a691e0 Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents: 24442
diff changeset
211 else:
274e42a691e0 Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents: 24442
diff changeset
212 self.data += str(other)
274e42a691e0 Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents: 24442
diff changeset
213 return self
274e42a691e0 Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents: 24442
diff changeset
214 def __imul__(self, n):
274e42a691e0 Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents: 24442
diff changeset
215 self.data *= n
274e42a691e0 Moved inplace add and multiply methods from UserString to MutableString.
Raymond Hettinger <python@rcn.com>
parents: 24442
diff changeset
216 return self
45212
feb6dd36d53f Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs.
Raymond Hettinger <python@rcn.com>
parents: 42951
diff changeset
217 def insert(self, index, value):
feb6dd36d53f Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs.
Raymond Hettinger <python@rcn.com>
parents: 42951
diff changeset
218 self[index:index] = value
feb6dd36d53f Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs.
Raymond Hettinger <python@rcn.com>
parents: 42951
diff changeset
219
12214
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
220 if __name__ == "__main__":
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
221 # 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
222 import os
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
223 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
224 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
225 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
226 from test import test_support
12214
a216fe79ae00 UserString class from Peter Funk <pf@artcom-gmbh.de>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
227 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
228 __import__('test.test_' + called_as.lower())