Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: UserString.py
#!/usr/bin/env python
[0] Fix | Delete
## vim:ts=4:et:nowrap
[1] Fix | Delete
"""A user-defined wrapper around string objects
[2] Fix | Delete
[3] Fix | Delete
Note: string objects have grown methods in Python 1.6
[4] Fix | Delete
This module requires Python 1.6 or later.
[5] Fix | Delete
"""
[6] Fix | Delete
import sys
[7] Fix | Delete
import collections
[8] Fix | Delete
[9] Fix | Delete
__all__ = ["UserString","MutableString"]
[10] Fix | Delete
[11] Fix | Delete
class UserString(collections.Sequence):
[12] Fix | Delete
def __init__(self, seq):
[13] Fix | Delete
if isinstance(seq, basestring):
[14] Fix | Delete
self.data = seq
[15] Fix | Delete
elif isinstance(seq, UserString):
[16] Fix | Delete
self.data = seq.data[:]
[17] Fix | Delete
else:
[18] Fix | Delete
self.data = str(seq)
[19] Fix | Delete
def __str__(self): return str(self.data)
[20] Fix | Delete
def __repr__(self): return repr(self.data)
[21] Fix | Delete
def __int__(self): return int(self.data)
[22] Fix | Delete
def __long__(self): return long(self.data)
[23] Fix | Delete
def __float__(self): return float(self.data)
[24] Fix | Delete
def __complex__(self): return complex(self.data)
[25] Fix | Delete
def __hash__(self): return hash(self.data)
[26] Fix | Delete
[27] Fix | Delete
def __cmp__(self, string):
[28] Fix | Delete
if isinstance(string, UserString):
[29] Fix | Delete
return cmp(self.data, string.data)
[30] Fix | Delete
else:
[31] Fix | Delete
return cmp(self.data, string)
[32] Fix | Delete
def __contains__(self, char):
[33] Fix | Delete
return char in self.data
[34] Fix | Delete
[35] Fix | Delete
def __len__(self): return len(self.data)
[36] Fix | Delete
def __getitem__(self, index): return self.__class__(self.data[index])
[37] Fix | Delete
def __getslice__(self, start, end):
[38] Fix | Delete
start = max(start, 0); end = max(end, 0)
[39] Fix | Delete
return self.__class__(self.data[start:end])
[40] Fix | Delete
[41] Fix | Delete
def __add__(self, other):
[42] Fix | Delete
if isinstance(other, UserString):
[43] Fix | Delete
return self.__class__(self.data + other.data)
[44] Fix | Delete
elif isinstance(other, basestring):
[45] Fix | Delete
return self.__class__(self.data + other)
[46] Fix | Delete
else:
[47] Fix | Delete
return self.__class__(self.data + str(other))
[48] Fix | Delete
def __radd__(self, other):
[49] Fix | Delete
if isinstance(other, basestring):
[50] Fix | Delete
return self.__class__(other + self.data)
[51] Fix | Delete
else:
[52] Fix | Delete
return self.__class__(str(other) + self.data)
[53] Fix | Delete
def __mul__(self, n):
[54] Fix | Delete
return self.__class__(self.data*n)
[55] Fix | Delete
__rmul__ = __mul__
[56] Fix | Delete
def __mod__(self, args):
[57] Fix | Delete
return self.__class__(self.data % args)
[58] Fix | Delete
[59] Fix | Delete
# the following methods are defined in alphabetical order:
[60] Fix | Delete
def capitalize(self): return self.__class__(self.data.capitalize())
[61] Fix | Delete
def center(self, width, *args):
[62] Fix | Delete
return self.__class__(self.data.center(width, *args))
[63] Fix | Delete
def count(self, sub, start=0, end=sys.maxint):
[64] Fix | Delete
return self.data.count(sub, start, end)
[65] Fix | Delete
def decode(self, encoding=None, errors=None): # XXX improve this?
[66] Fix | Delete
if encoding:
[67] Fix | Delete
if errors:
[68] Fix | Delete
return self.__class__(self.data.decode(encoding, errors))
[69] Fix | Delete
else:
[70] Fix | Delete
return self.__class__(self.data.decode(encoding))
[71] Fix | Delete
else:
[72] Fix | Delete
return self.__class__(self.data.decode())
[73] Fix | Delete
def encode(self, encoding=None, errors=None): # XXX improve this?
[74] Fix | Delete
if encoding:
[75] Fix | Delete
if errors:
[76] Fix | Delete
return self.__class__(self.data.encode(encoding, errors))
[77] Fix | Delete
else:
[78] Fix | Delete
return self.__class__(self.data.encode(encoding))
[79] Fix | Delete
else:
[80] Fix | Delete
return self.__class__(self.data.encode())
[81] Fix | Delete
def endswith(self, suffix, start=0, end=sys.maxint):
[82] Fix | Delete
return self.data.endswith(suffix, start, end)
[83] Fix | Delete
def expandtabs(self, tabsize=8):
[84] Fix | Delete
return self.__class__(self.data.expandtabs(tabsize))
[85] Fix | Delete
def find(self, sub, start=0, end=sys.maxint):
[86] Fix | Delete
return self.data.find(sub, start, end)
[87] Fix | Delete
def index(self, sub, start=0, end=sys.maxint):
[88] Fix | Delete
return self.data.index(sub, start, end)
[89] Fix | Delete
def isalpha(self): return self.data.isalpha()
[90] Fix | Delete
def isalnum(self): return self.data.isalnum()
[91] Fix | Delete
def isdecimal(self): return self.data.isdecimal()
[92] Fix | Delete
def isdigit(self): return self.data.isdigit()
[93] Fix | Delete
def islower(self): return self.data.islower()
[94] Fix | Delete
def isnumeric(self): return self.data.isnumeric()
[95] Fix | Delete
def isspace(self): return self.data.isspace()
[96] Fix | Delete
def istitle(self): return self.data.istitle()
[97] Fix | Delete
def isupper(self): return self.data.isupper()
[98] Fix | Delete
def join(self, seq): return self.data.join(seq)
[99] Fix | Delete
def ljust(self, width, *args):
[100] Fix | Delete
return self.__class__(self.data.ljust(width, *args))
[101] Fix | Delete
def lower(self): return self.__class__(self.data.lower())
[102] Fix | Delete
def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
[103] Fix | Delete
def partition(self, sep):
[104] Fix | Delete
return self.data.partition(sep)
[105] Fix | Delete
def replace(self, old, new, maxsplit=-1):
[106] Fix | Delete
return self.__class__(self.data.replace(old, new, maxsplit))
[107] Fix | Delete
def rfind(self, sub, start=0, end=sys.maxint):
[108] Fix | Delete
return self.data.rfind(sub, start, end)
[109] Fix | Delete
def rindex(self, sub, start=0, end=sys.maxint):
[110] Fix | Delete
return self.data.rindex(sub, start, end)
[111] Fix | Delete
def rjust(self, width, *args):
[112] Fix | Delete
return self.__class__(self.data.rjust(width, *args))
[113] Fix | Delete
def rpartition(self, sep):
[114] Fix | Delete
return self.data.rpartition(sep)
[115] Fix | Delete
def rstrip(self, chars=None): return self.__class__(self.data.rstrip(chars))
[116] Fix | Delete
def split(self, sep=None, maxsplit=-1):
[117] Fix | Delete
return self.data.split(sep, maxsplit)
[118] Fix | Delete
def rsplit(self, sep=None, maxsplit=-1):
[119] Fix | Delete
return self.data.rsplit(sep, maxsplit)
[120] Fix | Delete
def splitlines(self, keepends=0): return self.data.splitlines(keepends)
[121] Fix | Delete
def startswith(self, prefix, start=0, end=sys.maxint):
[122] Fix | Delete
return self.data.startswith(prefix, start, end)
[123] Fix | Delete
def strip(self, chars=None): return self.__class__(self.data.strip(chars))
[124] Fix | Delete
def swapcase(self): return self.__class__(self.data.swapcase())
[125] Fix | Delete
def title(self): return self.__class__(self.data.title())
[126] Fix | Delete
def translate(self, *args):
[127] Fix | Delete
return self.__class__(self.data.translate(*args))
[128] Fix | Delete
def upper(self): return self.__class__(self.data.upper())
[129] Fix | Delete
def zfill(self, width): return self.__class__(self.data.zfill(width))
[130] Fix | Delete
[131] Fix | Delete
class MutableString(UserString, collections.MutableSequence):
[132] Fix | Delete
"""mutable string objects
[133] Fix | Delete
[134] Fix | Delete
Python strings are immutable objects. This has the advantage, that
[135] Fix | Delete
strings may be used as dictionary keys. If this property isn't needed
[136] Fix | Delete
and you insist on changing string values in place instead, you may cheat
[137] Fix | Delete
and use MutableString.
[138] Fix | Delete
[139] Fix | Delete
But the purpose of this class is an educational one: to prevent
[140] Fix | Delete
people from inventing their own mutable string class derived
[141] Fix | Delete
from UserString and than forget thereby to remove (override) the
[142] Fix | Delete
__hash__ method inherited from UserString. This would lead to
[143] Fix | Delete
errors that would be very hard to track down.
[144] Fix | Delete
[145] Fix | Delete
A faster and better solution is to rewrite your program using lists."""
[146] Fix | Delete
def __init__(self, string=""):
[147] Fix | Delete
from warnings import warnpy3k
[148] Fix | Delete
warnpy3k('the class UserString.MutableString has been removed in '
[149] Fix | Delete
'Python 3.0', stacklevel=2)
[150] Fix | Delete
self.data = string
[151] Fix | Delete
[152] Fix | Delete
# We inherit object.__hash__, so we must deny this explicitly
[153] Fix | Delete
__hash__ = None
[154] Fix | Delete
[155] Fix | Delete
def __setitem__(self, index, sub):
[156] Fix | Delete
if isinstance(index, slice):
[157] Fix | Delete
if isinstance(sub, UserString):
[158] Fix | Delete
sub = sub.data
[159] Fix | Delete
elif not isinstance(sub, basestring):
[160] Fix | Delete
sub = str(sub)
[161] Fix | Delete
start, stop, step = index.indices(len(self.data))
[162] Fix | Delete
if step == -1:
[163] Fix | Delete
start, stop = stop+1, start+1
[164] Fix | Delete
sub = sub[::-1]
[165] Fix | Delete
elif step != 1:
[166] Fix | Delete
# XXX(twouters): I guess we should be reimplementing
[167] Fix | Delete
# the extended slice assignment/deletion algorithm here...
[168] Fix | Delete
raise TypeError, "invalid step in slicing assignment"
[169] Fix | Delete
start = min(start, stop)
[170] Fix | Delete
self.data = self.data[:start] + sub + self.data[stop:]
[171] Fix | Delete
else:
[172] Fix | Delete
if index < 0:
[173] Fix | Delete
index += len(self.data)
[174] Fix | Delete
if index < 0 or index >= len(self.data): raise IndexError
[175] Fix | Delete
self.data = self.data[:index] + sub + self.data[index+1:]
[176] Fix | Delete
def __delitem__(self, index):
[177] Fix | Delete
if isinstance(index, slice):
[178] Fix | Delete
start, stop, step = index.indices(len(self.data))
[179] Fix | Delete
if step == -1:
[180] Fix | Delete
start, stop = stop+1, start+1
[181] Fix | Delete
elif step != 1:
[182] Fix | Delete
# XXX(twouters): see same block in __setitem__
[183] Fix | Delete
raise TypeError, "invalid step in slicing deletion"
[184] Fix | Delete
start = min(start, stop)
[185] Fix | Delete
self.data = self.data[:start] + self.data[stop:]
[186] Fix | Delete
else:
[187] Fix | Delete
if index < 0:
[188] Fix | Delete
index += len(self.data)
[189] Fix | Delete
if index < 0 or index >= len(self.data): raise IndexError
[190] Fix | Delete
self.data = self.data[:index] + self.data[index+1:]
[191] Fix | Delete
def __setslice__(self, start, end, sub):
[192] Fix | Delete
start = max(start, 0); end = max(end, 0)
[193] Fix | Delete
if isinstance(sub, UserString):
[194] Fix | Delete
self.data = self.data[:start]+sub.data+self.data[end:]
[195] Fix | Delete
elif isinstance(sub, basestring):
[196] Fix | Delete
self.data = self.data[:start]+sub+self.data[end:]
[197] Fix | Delete
else:
[198] Fix | Delete
self.data = self.data[:start]+str(sub)+self.data[end:]
[199] Fix | Delete
def __delslice__(self, start, end):
[200] Fix | Delete
start = max(start, 0); end = max(end, 0)
[201] Fix | Delete
self.data = self.data[:start] + self.data[end:]
[202] Fix | Delete
def immutable(self):
[203] Fix | Delete
return UserString(self.data)
[204] Fix | Delete
def __iadd__(self, other):
[205] Fix | Delete
if isinstance(other, UserString):
[206] Fix | Delete
self.data += other.data
[207] Fix | Delete
elif isinstance(other, basestring):
[208] Fix | Delete
self.data += other
[209] Fix | Delete
else:
[210] Fix | Delete
self.data += str(other)
[211] Fix | Delete
return self
[212] Fix | Delete
def __imul__(self, n):
[213] Fix | Delete
self.data *= n
[214] Fix | Delete
return self
[215] Fix | Delete
def insert(self, index, value):
[216] Fix | Delete
self[index:index] = value
[217] Fix | Delete
[218] Fix | Delete
if __name__ == "__main__":
[219] Fix | Delete
# execute the regression test to stdout, if called as a script:
[220] Fix | Delete
import os
[221] Fix | Delete
called_in_dir, called_as = os.path.split(sys.argv[0])
[222] Fix | Delete
called_as, py = os.path.splitext(called_as)
[223] Fix | Delete
if '-q' in sys.argv:
[224] Fix | Delete
from test import test_support
[225] Fix | Delete
test_support.verbose = 0
[226] Fix | Delete
__import__('test.test_' + called_as.lower())
[227] Fix | Delete
[228] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function