ยปCore Development>Code coverage>Lib/UserString.py

Python code coverage for Lib/UserString.py

#countcontent
1n/a#!/usr/bin/env python
2n/a## vim:ts=4:et:nowrap
3n/a"""A user-defined wrapper around string objects
4n/a
5n/aNote: string objects have grown methods in Python 1.6
6n/aThis module requires Python 1.6 or later.
71"""
81import sys
91import collections
10n/a
111__all__ = ["UserString","MutableString"]
12n/a
132class UserString(collections.Sequence):
141 def __init__(self, seq):
1588609 if isinstance(seq, basestring):
1688609 self.data = seq
170 elif isinstance(seq, UserString):
180 self.data = seq.data[:]
19n/a else:
200 self.data = str(seq)
211 def __str__(self): return str(self.data)
221 def __repr__(self): return repr(self.data)
231 def __int__(self): return int(self.data)
241 def __long__(self): return long(self.data)
251 def __float__(self): return float(self.data)
261 def __complex__(self): return complex(self.data)
279 def __hash__(self): return hash(self.data)
28n/a
291 def __cmp__(self, string):
3019510 if isinstance(string, UserString):
31485 return cmp(self.data, string.data)
32n/a else:
3319025 return cmp(self.data, string)
341 def __contains__(self, char):
35530002 return char in self.data
36n/a
37130563 def __len__(self): return len(self.data)
38920 def __getitem__(self, index): return self.__class__(self.data[index])
391 def __getslice__(self, start, end):
4016318 start = max(start, 0); end = max(end, 0)
4116318 return self.__class__(self.data[start:end])
42n/a
431 def __add__(self, other):
446 if isinstance(other, UserString):
456 return self.__class__(self.data + other.data)
460 elif isinstance(other, basestring):
470 return self.__class__(self.data + other)
48n/a else:
490 return self.__class__(self.data + str(other))
501 def __radd__(self, other):
510 if isinstance(other, basestring):
520 return self.__class__(other + self.data)
53n/a else:
540 return self.__class__(str(other) + self.data)
551 def __mul__(self, n):
5610 return self.__class__(self.data*n)
571 __rmul__ = __mul__
581 def __mod__(self, args):
5912058 return self.__class__(self.data % args)
60n/a
61n/a # the following methods are defined in alphabetical order:
6213 def capitalize(self): return self.__class__(self.data.capitalize())
631 def center(self, width, *args):
6410 return self.__class__(self.data.center(width, *args))
651 def count(self, sub, start=0, end=sys.maxint):
66130104 return self.data.count(sub, start, end)
671 def decode(self, encoding=None, errors=None): # XXX improve this?
6812 if encoding:
6912 if errors:
700 return self.__class__(self.data.decode(encoding, errors))
71n/a else:
7212 return self.__class__(self.data.decode(encoding))
73n/a else:
740 return self.__class__(self.data.decode())
751 def encode(self, encoding=None, errors=None): # XXX improve this?
7612 if encoding:
7712 if errors:
780 return self.__class__(self.data.encode(encoding, errors))
79n/a else:
8012 return self.__class__(self.data.encode(encoding))
81n/a else:
820 return self.__class__(self.data.encode())
831 def endswith(self, suffix, start=0, end=sys.maxint):
8476 return self.data.endswith(suffix, start, end)
851 def expandtabs(self, tabsize=8):
8616 return self.__class__(self.data.expandtabs(tabsize))
871 def find(self, sub, start=0, end=sys.maxint):
88265030 return self.data.find(sub, start, end)
891 def index(self, sub, start=0, end=sys.maxint):
9028 return self.data.index(sub, start, end)
9115 def isalpha(self): return self.data.isalpha()
9217 def isalnum(self): return self.data.isalnum()
931 def isdecimal(self): return self.data.isdecimal()
9411 def isdigit(self): return self.data.isdigit()
9523 def islower(self): return self.data.islower()
961 def isnumeric(self): return self.data.isnumeric()
9717 def isspace(self): return self.data.isspace()
9823 def istitle(self): return self.data.istitle()
9919 def isupper(self): return self.data.isupper()
10045 def join(self, seq): return self.data.join(seq)
1011 def ljust(self, width, *args):
10210 return self.__class__(self.data.ljust(width, *args))
1037 def lower(self): return self.__class__(self.data.lower())
1049 def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
1051 def partition(self, sep):
10614 return self.data.partition(sep)
1071 def replace(self, old, new, maxsplit=-1):
108129800 return self.__class__(self.data.replace(old, new, maxsplit))
1091 def rfind(self, sub, start=0, end=sys.maxint):
110265020 return self.data.rfind(sub, start, end)
1111 def rindex(self, sub, start=0, end=sys.maxint):
11230 return self.data.rindex(sub, start, end)
1131 def rjust(self, width, *args):
11410 return self.__class__(self.data.rjust(width, *args))
1151 def rpartition(self, sep):
11614 return self.data.rpartition(sep)
1179 def rstrip(self, chars=None): return self.__class__(self.data.rstrip(chars))
1181 def split(self, sep=None, maxsplit=-1):
119122 return self.data.split(sep, maxsplit)
1201 def rsplit(self, sep=None, maxsplit=-1):
121120 return self.data.rsplit(sep, maxsplit)
12215 def splitlines(self, keepends=0): return self.data.splitlines(keepends)
1231 def startswith(self, prefix, start=0, end=sys.maxint):
12468 return self.data.startswith(prefix, start, end)
12515 def strip(self, chars=None): return self.__class__(self.data.strip(chars))
1265 def swapcase(self): return self.__class__(self.data.swapcase())
12715 def title(self): return self.__class__(self.data.title())
1281 def translate(self, *args):
12918 return self.__class__(self.data.translate(*args))
1307 def upper(self): return self.__class__(self.data.upper())
13125 def zfill(self, width): return self.__class__(self.data.zfill(width))
132n/a
1332class MutableString(UserString, collections.MutableSequence):
134n/a """mutable string objects
135n/a
136n/a Python strings are immutable objects. This has the advantage, that
137n/a strings may be used as dictionary keys. If this property isn't needed
138n/a and you insist on changing string values in place instead, you may cheat
139n/a and use MutableString.
140n/a
141n/a But the purpose of this class is an educational one: to prevent
142n/a people from inventing their own mutable string class derived
143n/a from UserString and than forget thereby to remove (override) the
144n/a __hash__ method inherited from UserString. This would lead to
145n/a errors that would be very hard to track down.
146n/a
1471 A faster and better solution is to rewrite your program using lists."""
1481 def __init__(self, string=""):
14988899 from warnings import warnpy3k
15088899 warnpy3k('the class UserString.MutableString has been removed in '
15188899 'Python 3.0', stacklevel=2)
15288899 self.data = string
153n/a
154n/a # We inherit object.__hash__, so we must deny this explicitly
1551 __hash__ = None
156n/a
1571 def __setitem__(self, index, sub):
158304 if isinstance(index, slice):
159300 if isinstance(sub, UserString):
1600 sub = sub.data
161300 elif not isinstance(sub, basestring):
1620 sub = str(sub)
163300 start, stop, step = index.indices(len(self.data))
164300 if step == -1:
165100 start, stop = stop+1, start+1
166100 sub = sub[::-1]
167200 elif step != 1:
168n/a # XXX(twouters): I guess we should be reimplementing
169n/a # the extended slice assignment/deletion algorithm here...
1700 raise TypeError, "invalid step in slicing assignment"
171300 start = min(start, stop)
172300 self.data = self.data[:start] + sub + self.data[stop:]
173n/a else:
1744 if index < 0:
1752 index += len(self.data)
1764 if index < 0 or index >= len(self.data): raise IndexError
1772 self.data = self.data[:index] + sub + self.data[index+1:]
1781 def __delitem__(self, index):
179305 if isinstance(index, slice):
180300 start, stop, step = index.indices(len(self.data))
181300 if step == -1:
182100 start, stop = stop+1, start+1
183200 elif step != 1:
184n/a # XXX(twouters): see same block in __setitem__
1850 raise TypeError, "invalid step in slicing deletion"
186300 start = min(start, stop)
187300 self.data = self.data[:start] + self.data[stop:]
188n/a else:
1895 if index < 0:
1902 index += len(self.data)
1915 if index < 0 or index >= len(self.data): raise IndexError
1923 self.data = self.data[:index] + self.data[index+1:]
1931 def __setslice__(self, start, end, sub):
1944 start = max(start, 0); end = max(end, 0)
1954 if isinstance(sub, UserString):
1961 self.data = self.data[:start]+sub.data+self.data[end:]
1973 elif isinstance(sub, basestring):
1982 self.data = self.data[:start]+sub+self.data[end:]
199n/a else:
2001 self.data = self.data[:start]+str(sub)+self.data[end:]
2011 def __delslice__(self, start, end):
2022 start = max(start, 0); end = max(end, 0)
2032 self.data = self.data[:start] + self.data[end:]
2041 def immutable(self):
2051 return UserString(self.data)
2061 def __iadd__(self, other):
2073 if isinstance(other, UserString):
2081 self.data += other.data
2092 elif isinstance(other, basestring):
2101 self.data += other
211n/a else:
2121 self.data += str(other)
2133 return self
2141 def __imul__(self, n):
2153 self.data *= n
2163 return self
2171 def insert(self, index, value):
2180 self[index:index] = value
219n/a
2201if __name__ == "__main__":
221n/a # execute the regression test to stdout, if called as a script:
2220 import os
2230 called_in_dir, called_as = os.path.split(sys.argv[0])
2240 called_as, py = os.path.splitext(called_as)
2250 if '-q' in sys.argv:
2260 from test import test_support
2270 test_support.verbose = 0
2280 __import__('test.test_' + called_as.lower())