Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: string.py
"""A collection of string operations (most are no longer used).
[0] Fix | Delete
[1] Fix | Delete
Warning: most of the code you see here isn't normally used nowadays.
[2] Fix | Delete
Beginning with Python 1.6, many of these functions are implemented as
[3] Fix | Delete
methods on the standard string object. They used to be implemented by
[4] Fix | Delete
a built-in module called strop, but strop is now obsolete itself.
[5] Fix | Delete
[6] Fix | Delete
Public module variables:
[7] Fix | Delete
[8] Fix | Delete
whitespace -- a string containing all characters considered whitespace
[9] Fix | Delete
lowercase -- a string containing all characters considered lowercase letters
[10] Fix | Delete
uppercase -- a string containing all characters considered uppercase letters
[11] Fix | Delete
letters -- a string containing all characters considered letters
[12] Fix | Delete
digits -- a string containing all characters considered decimal digits
[13] Fix | Delete
hexdigits -- a string containing all characters considered hexadecimal digits
[14] Fix | Delete
octdigits -- a string containing all characters considered octal digits
[15] Fix | Delete
punctuation -- a string containing all characters considered punctuation
[16] Fix | Delete
printable -- a string containing all characters considered printable
[17] Fix | Delete
[18] Fix | Delete
"""
[19] Fix | Delete
[20] Fix | Delete
# Some strings for ctype-style character classification
[21] Fix | Delete
whitespace = ' \t\n\r\v\f'
[22] Fix | Delete
lowercase = 'abcdefghijklmnopqrstuvwxyz'
[23] Fix | Delete
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
[24] Fix | Delete
letters = lowercase + uppercase
[25] Fix | Delete
ascii_lowercase = lowercase
[26] Fix | Delete
ascii_uppercase = uppercase
[27] Fix | Delete
ascii_letters = ascii_lowercase + ascii_uppercase
[28] Fix | Delete
digits = '0123456789'
[29] Fix | Delete
hexdigits = digits + 'abcdef' + 'ABCDEF'
[30] Fix | Delete
octdigits = '01234567'
[31] Fix | Delete
punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
[32] Fix | Delete
printable = digits + letters + punctuation + whitespace
[33] Fix | Delete
[34] Fix | Delete
# Case conversion helpers
[35] Fix | Delete
# Use str to convert Unicode literal in case of -U
[36] Fix | Delete
l = map(chr, xrange(256))
[37] Fix | Delete
_idmap = str('').join(l)
[38] Fix | Delete
del l
[39] Fix | Delete
[40] Fix | Delete
# Functions which aren't available as string methods.
[41] Fix | Delete
[42] Fix | Delete
# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
[43] Fix | Delete
def capwords(s, sep=None):
[44] Fix | Delete
"""capwords(s [,sep]) -> string
[45] Fix | Delete
[46] Fix | Delete
Split the argument into words using split, capitalize each
[47] Fix | Delete
word using capitalize, and join the capitalized words using
[48] Fix | Delete
join. If the optional second argument sep is absent or None,
[49] Fix | Delete
runs of whitespace characters are replaced by a single space
[50] Fix | Delete
and leading and trailing whitespace are removed, otherwise
[51] Fix | Delete
sep is used to split and join the words.
[52] Fix | Delete
[53] Fix | Delete
"""
[54] Fix | Delete
return (sep or ' ').join(x.capitalize() for x in s.split(sep))
[55] Fix | Delete
[56] Fix | Delete
[57] Fix | Delete
# Construct a translation string
[58] Fix | Delete
_idmapL = None
[59] Fix | Delete
def maketrans(fromstr, tostr):
[60] Fix | Delete
"""maketrans(frm, to) -> string
[61] Fix | Delete
[62] Fix | Delete
Return a translation table (a string of 256 bytes long)
[63] Fix | Delete
suitable for use in string.translate. The strings frm and to
[64] Fix | Delete
must be of the same length.
[65] Fix | Delete
[66] Fix | Delete
"""
[67] Fix | Delete
if len(fromstr) != len(tostr):
[68] Fix | Delete
raise ValueError, "maketrans arguments must have same length"
[69] Fix | Delete
global _idmapL
[70] Fix | Delete
if not _idmapL:
[71] Fix | Delete
_idmapL = list(_idmap)
[72] Fix | Delete
L = _idmapL[:]
[73] Fix | Delete
fromstr = map(ord, fromstr)
[74] Fix | Delete
for i in range(len(fromstr)):
[75] Fix | Delete
L[fromstr[i]] = tostr[i]
[76] Fix | Delete
return ''.join(L)
[77] Fix | Delete
[78] Fix | Delete
[79] Fix | Delete
[80] Fix | Delete
####################################################################
[81] Fix | Delete
import re as _re
[82] Fix | Delete
[83] Fix | Delete
class _multimap:
[84] Fix | Delete
"""Helper class for combining multiple mappings.
[85] Fix | Delete
[86] Fix | Delete
Used by .{safe_,}substitute() to combine the mapping and keyword
[87] Fix | Delete
arguments.
[88] Fix | Delete
"""
[89] Fix | Delete
def __init__(self, primary, secondary):
[90] Fix | Delete
self._primary = primary
[91] Fix | Delete
self._secondary = secondary
[92] Fix | Delete
[93] Fix | Delete
def __getitem__(self, key):
[94] Fix | Delete
try:
[95] Fix | Delete
return self._primary[key]
[96] Fix | Delete
except KeyError:
[97] Fix | Delete
return self._secondary[key]
[98] Fix | Delete
[99] Fix | Delete
[100] Fix | Delete
class _TemplateMetaclass(type):
[101] Fix | Delete
pattern = r"""
[102] Fix | Delete
%(delim)s(?:
[103] Fix | Delete
(?P<escaped>%(delim)s) | # Escape sequence of two delimiters
[104] Fix | Delete
(?P<named>%(id)s) | # delimiter and a Python identifier
[105] Fix | Delete
{(?P<braced>%(id)s)} | # delimiter and a braced identifier
[106] Fix | Delete
(?P<invalid>) # Other ill-formed delimiter exprs
[107] Fix | Delete
)
[108] Fix | Delete
"""
[109] Fix | Delete
[110] Fix | Delete
def __init__(cls, name, bases, dct):
[111] Fix | Delete
super(_TemplateMetaclass, cls).__init__(name, bases, dct)
[112] Fix | Delete
if 'pattern' in dct:
[113] Fix | Delete
pattern = cls.pattern
[114] Fix | Delete
else:
[115] Fix | Delete
pattern = _TemplateMetaclass.pattern % {
[116] Fix | Delete
'delim' : _re.escape(cls.delimiter),
[117] Fix | Delete
'id' : cls.idpattern,
[118] Fix | Delete
}
[119] Fix | Delete
cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE)
[120] Fix | Delete
[121] Fix | Delete
[122] Fix | Delete
class Template:
[123] Fix | Delete
"""A string class for supporting $-substitutions."""
[124] Fix | Delete
__metaclass__ = _TemplateMetaclass
[125] Fix | Delete
[126] Fix | Delete
delimiter = '$'
[127] Fix | Delete
idpattern = r'[_a-z][_a-z0-9]*'
[128] Fix | Delete
[129] Fix | Delete
def __init__(self, template):
[130] Fix | Delete
self.template = template
[131] Fix | Delete
[132] Fix | Delete
# Search for $$, $identifier, ${identifier}, and any bare $'s
[133] Fix | Delete
[134] Fix | Delete
def _invalid(self, mo):
[135] Fix | Delete
i = mo.start('invalid')
[136] Fix | Delete
lines = self.template[:i].splitlines(True)
[137] Fix | Delete
if not lines:
[138] Fix | Delete
colno = 1
[139] Fix | Delete
lineno = 1
[140] Fix | Delete
else:
[141] Fix | Delete
colno = i - len(''.join(lines[:-1]))
[142] Fix | Delete
lineno = len(lines)
[143] Fix | Delete
raise ValueError('Invalid placeholder in string: line %d, col %d' %
[144] Fix | Delete
(lineno, colno))
[145] Fix | Delete
[146] Fix | Delete
def substitute(*args, **kws):
[147] Fix | Delete
if not args:
[148] Fix | Delete
raise TypeError("descriptor 'substitute' of 'Template' object "
[149] Fix | Delete
"needs an argument")
[150] Fix | Delete
self, args = args[0], args[1:] # allow the "self" keyword be passed
[151] Fix | Delete
if len(args) > 1:
[152] Fix | Delete
raise TypeError('Too many positional arguments')
[153] Fix | Delete
if not args:
[154] Fix | Delete
mapping = kws
[155] Fix | Delete
elif kws:
[156] Fix | Delete
mapping = _multimap(kws, args[0])
[157] Fix | Delete
else:
[158] Fix | Delete
mapping = args[0]
[159] Fix | Delete
# Helper function for .sub()
[160] Fix | Delete
def convert(mo):
[161] Fix | Delete
# Check the most common path first.
[162] Fix | Delete
named = mo.group('named') or mo.group('braced')
[163] Fix | Delete
if named is not None:
[164] Fix | Delete
val = mapping[named]
[165] Fix | Delete
# We use this idiom instead of str() because the latter will
[166] Fix | Delete
# fail if val is a Unicode containing non-ASCII characters.
[167] Fix | Delete
return '%s' % (val,)
[168] Fix | Delete
if mo.group('escaped') is not None:
[169] Fix | Delete
return self.delimiter
[170] Fix | Delete
if mo.group('invalid') is not None:
[171] Fix | Delete
self._invalid(mo)
[172] Fix | Delete
raise ValueError('Unrecognized named group in pattern',
[173] Fix | Delete
self.pattern)
[174] Fix | Delete
return self.pattern.sub(convert, self.template)
[175] Fix | Delete
[176] Fix | Delete
def safe_substitute(*args, **kws):
[177] Fix | Delete
if not args:
[178] Fix | Delete
raise TypeError("descriptor 'safe_substitute' of 'Template' object "
[179] Fix | Delete
"needs an argument")
[180] Fix | Delete
self, args = args[0], args[1:] # allow the "self" keyword be passed
[181] Fix | Delete
if len(args) > 1:
[182] Fix | Delete
raise TypeError('Too many positional arguments')
[183] Fix | Delete
if not args:
[184] Fix | Delete
mapping = kws
[185] Fix | Delete
elif kws:
[186] Fix | Delete
mapping = _multimap(kws, args[0])
[187] Fix | Delete
else:
[188] Fix | Delete
mapping = args[0]
[189] Fix | Delete
# Helper function for .sub()
[190] Fix | Delete
def convert(mo):
[191] Fix | Delete
named = mo.group('named') or mo.group('braced')
[192] Fix | Delete
if named is not None:
[193] Fix | Delete
try:
[194] Fix | Delete
# We use this idiom instead of str() because the latter
[195] Fix | Delete
# will fail if val is a Unicode containing non-ASCII
[196] Fix | Delete
return '%s' % (mapping[named],)
[197] Fix | Delete
except KeyError:
[198] Fix | Delete
return mo.group()
[199] Fix | Delete
if mo.group('escaped') is not None:
[200] Fix | Delete
return self.delimiter
[201] Fix | Delete
if mo.group('invalid') is not None:
[202] Fix | Delete
return mo.group()
[203] Fix | Delete
raise ValueError('Unrecognized named group in pattern',
[204] Fix | Delete
self.pattern)
[205] Fix | Delete
return self.pattern.sub(convert, self.template)
[206] Fix | Delete
[207] Fix | Delete
[208] Fix | Delete
[209] Fix | Delete
####################################################################
[210] Fix | Delete
# NOTE: Everything below here is deprecated. Use string methods instead.
[211] Fix | Delete
# This stuff will go away in Python 3.0.
[212] Fix | Delete
[213] Fix | Delete
# Backward compatible names for exceptions
[214] Fix | Delete
index_error = ValueError
[215] Fix | Delete
atoi_error = ValueError
[216] Fix | Delete
atof_error = ValueError
[217] Fix | Delete
atol_error = ValueError
[218] Fix | Delete
[219] Fix | Delete
# convert UPPER CASE letters to lower case
[220] Fix | Delete
def lower(s):
[221] Fix | Delete
"""lower(s) -> string
[222] Fix | Delete
[223] Fix | Delete
Return a copy of the string s converted to lowercase.
[224] Fix | Delete
[225] Fix | Delete
"""
[226] Fix | Delete
return s.lower()
[227] Fix | Delete
[228] Fix | Delete
# Convert lower case letters to UPPER CASE
[229] Fix | Delete
def upper(s):
[230] Fix | Delete
"""upper(s) -> string
[231] Fix | Delete
[232] Fix | Delete
Return a copy of the string s converted to uppercase.
[233] Fix | Delete
[234] Fix | Delete
"""
[235] Fix | Delete
return s.upper()
[236] Fix | Delete
[237] Fix | Delete
# Swap lower case letters and UPPER CASE
[238] Fix | Delete
def swapcase(s):
[239] Fix | Delete
"""swapcase(s) -> string
[240] Fix | Delete
[241] Fix | Delete
Return a copy of the string s with upper case characters
[242] Fix | Delete
converted to lowercase and vice versa.
[243] Fix | Delete
[244] Fix | Delete
"""
[245] Fix | Delete
return s.swapcase()
[246] Fix | Delete
[247] Fix | Delete
# Strip leading and trailing tabs and spaces
[248] Fix | Delete
def strip(s, chars=None):
[249] Fix | Delete
"""strip(s [,chars]) -> string
[250] Fix | Delete
[251] Fix | Delete
Return a copy of the string s with leading and trailing
[252] Fix | Delete
whitespace removed.
[253] Fix | Delete
If chars is given and not None, remove characters in chars instead.
[254] Fix | Delete
If chars is unicode, S will be converted to unicode before stripping.
[255] Fix | Delete
[256] Fix | Delete
"""
[257] Fix | Delete
return s.strip(chars)
[258] Fix | Delete
[259] Fix | Delete
# Strip leading tabs and spaces
[260] Fix | Delete
def lstrip(s, chars=None):
[261] Fix | Delete
"""lstrip(s [,chars]) -> string
[262] Fix | Delete
[263] Fix | Delete
Return a copy of the string s with leading whitespace removed.
[264] Fix | Delete
If chars is given and not None, remove characters in chars instead.
[265] Fix | Delete
[266] Fix | Delete
"""
[267] Fix | Delete
return s.lstrip(chars)
[268] Fix | Delete
[269] Fix | Delete
# Strip trailing tabs and spaces
[270] Fix | Delete
def rstrip(s, chars=None):
[271] Fix | Delete
"""rstrip(s [,chars]) -> string
[272] Fix | Delete
[273] Fix | Delete
Return a copy of the string s with trailing whitespace removed.
[274] Fix | Delete
If chars is given and not None, remove characters in chars instead.
[275] Fix | Delete
[276] Fix | Delete
"""
[277] Fix | Delete
return s.rstrip(chars)
[278] Fix | Delete
[279] Fix | Delete
[280] Fix | Delete
# Split a string into a list of space/tab-separated words
[281] Fix | Delete
def split(s, sep=None, maxsplit=-1):
[282] Fix | Delete
"""split(s [,sep [,maxsplit]]) -> list of strings
[283] Fix | Delete
[284] Fix | Delete
Return a list of the words in the string s, using sep as the
[285] Fix | Delete
delimiter string. If maxsplit is given, splits at no more than
[286] Fix | Delete
maxsplit places (resulting in at most maxsplit+1 words). If sep
[287] Fix | Delete
is not specified or is None, any whitespace string is a separator.
[288] Fix | Delete
[289] Fix | Delete
(split and splitfields are synonymous)
[290] Fix | Delete
[291] Fix | Delete
"""
[292] Fix | Delete
return s.split(sep, maxsplit)
[293] Fix | Delete
splitfields = split
[294] Fix | Delete
[295] Fix | Delete
# Split a string into a list of space/tab-separated words
[296] Fix | Delete
def rsplit(s, sep=None, maxsplit=-1):
[297] Fix | Delete
"""rsplit(s [,sep [,maxsplit]]) -> list of strings
[298] Fix | Delete
[299] Fix | Delete
Return a list of the words in the string s, using sep as the
[300] Fix | Delete
delimiter string, starting at the end of the string and working
[301] Fix | Delete
to the front. If maxsplit is given, at most maxsplit splits are
[302] Fix | Delete
done. If sep is not specified or is None, any whitespace string
[303] Fix | Delete
is a separator.
[304] Fix | Delete
"""
[305] Fix | Delete
return s.rsplit(sep, maxsplit)
[306] Fix | Delete
[307] Fix | Delete
# Join fields with optional separator
[308] Fix | Delete
def join(words, sep = ' '):
[309] Fix | Delete
"""join(list [,sep]) -> string
[310] Fix | Delete
[311] Fix | Delete
Return a string composed of the words in list, with
[312] Fix | Delete
intervening occurrences of sep. The default separator is a
[313] Fix | Delete
single space.
[314] Fix | Delete
[315] Fix | Delete
(joinfields and join are synonymous)
[316] Fix | Delete
[317] Fix | Delete
"""
[318] Fix | Delete
return sep.join(words)
[319] Fix | Delete
joinfields = join
[320] Fix | Delete
[321] Fix | Delete
# Find substring, raise exception if not found
[322] Fix | Delete
def index(s, *args):
[323] Fix | Delete
"""index(s, sub [,start [,end]]) -> int
[324] Fix | Delete
[325] Fix | Delete
Like find but raises ValueError when the substring is not found.
[326] Fix | Delete
[327] Fix | Delete
"""
[328] Fix | Delete
return s.index(*args)
[329] Fix | Delete
[330] Fix | Delete
# Find last substring, raise exception if not found
[331] Fix | Delete
def rindex(s, *args):
[332] Fix | Delete
"""rindex(s, sub [,start [,end]]) -> int
[333] Fix | Delete
[334] Fix | Delete
Like rfind but raises ValueError when the substring is not found.
[335] Fix | Delete
[336] Fix | Delete
"""
[337] Fix | Delete
return s.rindex(*args)
[338] Fix | Delete
[339] Fix | Delete
# Count non-overlapping occurrences of substring
[340] Fix | Delete
def count(s, *args):
[341] Fix | Delete
"""count(s, sub[, start[,end]]) -> int
[342] Fix | Delete
[343] Fix | Delete
Return the number of occurrences of substring sub in string
[344] Fix | Delete
s[start:end]. Optional arguments start and end are
[345] Fix | Delete
interpreted as in slice notation.
[346] Fix | Delete
[347] Fix | Delete
"""
[348] Fix | Delete
return s.count(*args)
[349] Fix | Delete
[350] Fix | Delete
# Find substring, return -1 if not found
[351] Fix | Delete
def find(s, *args):
[352] Fix | Delete
"""find(s, sub [,start [,end]]) -> in
[353] Fix | Delete
[354] Fix | Delete
Return the lowest index in s where substring sub is found,
[355] Fix | Delete
such that sub is contained within s[start,end]. Optional
[356] Fix | Delete
arguments start and end are interpreted as in slice notation.
[357] Fix | Delete
[358] Fix | Delete
Return -1 on failure.
[359] Fix | Delete
[360] Fix | Delete
"""
[361] Fix | Delete
return s.find(*args)
[362] Fix | Delete
[363] Fix | Delete
# Find last substring, return -1 if not found
[364] Fix | Delete
def rfind(s, *args):
[365] Fix | Delete
"""rfind(s, sub [,start [,end]]) -> int
[366] Fix | Delete
[367] Fix | Delete
Return the highest index in s where substring sub is found,
[368] Fix | Delete
such that sub is contained within s[start,end]. Optional
[369] Fix | Delete
arguments start and end are interpreted as in slice notation.
[370] Fix | Delete
[371] Fix | Delete
Return -1 on failure.
[372] Fix | Delete
[373] Fix | Delete
"""
[374] Fix | Delete
return s.rfind(*args)
[375] Fix | Delete
[376] Fix | Delete
# for a bit of speed
[377] Fix | Delete
_float = float
[378] Fix | Delete
_int = int
[379] Fix | Delete
_long = long
[380] Fix | Delete
[381] Fix | Delete
# Convert string to float
[382] Fix | Delete
def atof(s):
[383] Fix | Delete
"""atof(s) -> float
[384] Fix | Delete
[385] Fix | Delete
Return the floating point number represented by the string s.
[386] Fix | Delete
[387] Fix | Delete
"""
[388] Fix | Delete
return _float(s)
[389] Fix | Delete
[390] Fix | Delete
[391] Fix | Delete
# Convert string to integer
[392] Fix | Delete
def atoi(s , base=10):
[393] Fix | Delete
"""atoi(s [,base]) -> int
[394] Fix | Delete
[395] Fix | Delete
Return the integer represented by the string s in the given
[396] Fix | Delete
base, which defaults to 10. The string s must consist of one
[397] Fix | Delete
or more digits, possibly preceded by a sign. If base is 0, it
[398] Fix | Delete
is chosen from the leading characters of s, 0 for octal, 0x or
[399] Fix | Delete
0X for hexadecimal. If base is 16, a preceding 0x or 0X is
[400] Fix | Delete
accepted.
[401] Fix | Delete
[402] Fix | Delete
"""
[403] Fix | Delete
return _int(s, base)
[404] Fix | Delete
[405] Fix | Delete
[406] Fix | Delete
# Convert string to long integer
[407] Fix | Delete
def atol(s, base=10):
[408] Fix | Delete
"""atol(s [,base]) -> long
[409] Fix | Delete
[410] Fix | Delete
Return the long integer represented by the string s in the
[411] Fix | Delete
given base, which defaults to 10. The string s must consist
[412] Fix | Delete
of one or more digits, possibly preceded by a sign. If base
[413] Fix | Delete
is 0, it is chosen from the leading characters of s, 0 for
[414] Fix | Delete
octal, 0x or 0X for hexadecimal. If base is 16, a preceding
[415] Fix | Delete
0x or 0X is accepted. A trailing L or l is not accepted,
[416] Fix | Delete
unless base is 0.
[417] Fix | Delete
[418] Fix | Delete
"""
[419] Fix | Delete
return _long(s, base)
[420] Fix | Delete
[421] Fix | Delete
[422] Fix | Delete
# Left-justify a string
[423] Fix | Delete
def ljust(s, width, *args):
[424] Fix | Delete
"""ljust(s, width[, fillchar]) -> string
[425] Fix | Delete
[426] Fix | Delete
Return a left-justified version of s, in a field of the
[427] Fix | Delete
specified width, padded with spaces as needed. The string is
[428] Fix | Delete
never truncated. If specified the fillchar is used instead of spaces.
[429] Fix | Delete
[430] Fix | Delete
"""
[431] Fix | Delete
return s.ljust(width, *args)
[432] Fix | Delete
[433] Fix | Delete
# Right-justify a string
[434] Fix | Delete
def rjust(s, width, *args):
[435] Fix | Delete
"""rjust(s, width[, fillchar]) -> string
[436] Fix | Delete
[437] Fix | Delete
Return a right-justified version of s, in a field of the
[438] Fix | Delete
specified width, padded with spaces as needed. The string is
[439] Fix | Delete
never truncated. If specified the fillchar is used instead of spaces.
[440] Fix | Delete
[441] Fix | Delete
"""
[442] Fix | Delete
return s.rjust(width, *args)
[443] Fix | Delete
[444] Fix | Delete
# Center a string
[445] Fix | Delete
def center(s, width, *args):
[446] Fix | Delete
"""center(s, width[, fillchar]) -> string
[447] Fix | Delete
[448] Fix | Delete
Return a center version of s, in a field of the specified
[449] Fix | Delete
width. padded with spaces as needed. The string is never
[450] Fix | Delete
truncated. If specified the fillchar is used instead of spaces.
[451] Fix | Delete
[452] Fix | Delete
"""
[453] Fix | Delete
return s.center(width, *args)
[454] Fix | Delete
[455] Fix | Delete
# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
[456] Fix | Delete
# Decadent feature: the argument may be a string or a number
[457] Fix | Delete
# (Use of this is deprecated; it should be a string as with ljust c.s.)
[458] Fix | Delete
def zfill(x, width):
[459] Fix | Delete
"""zfill(x, width) -> string
[460] Fix | Delete
[461] Fix | Delete
Pad a numeric string x with zeros on the left, to fill a field
[462] Fix | Delete
of the specified width. The string x is never truncated.
[463] Fix | Delete
[464] Fix | Delete
"""
[465] Fix | Delete
if not isinstance(x, basestring):
[466] Fix | Delete
x = repr(x)
[467] Fix | Delete
return x.zfill(width)
[468] Fix | Delete
[469] Fix | Delete
# Expand tabs in a string.
[470] Fix | Delete
# Doesn't take non-printing chars into account, but does understand \n.
[471] Fix | Delete
def expandtabs(s, tabsize=8):
[472] Fix | Delete
"""expandtabs(s [,tabsize]) -> string
[473] Fix | Delete
[474] Fix | Delete
Return a copy of the string s with all tab characters replaced
[475] Fix | Delete
by the appropriate number of spaces, depending on the current
[476] Fix | Delete
column, and the tabsize (default 8).
[477] Fix | Delete
[478] Fix | Delete
"""
[479] Fix | Delete
return s.expandtabs(tabsize)
[480] Fix | Delete
[481] Fix | Delete
# Character translation through look-up table.
[482] Fix | Delete
def translate(s, table, deletions=""):
[483] Fix | Delete
"""translate(s,table [,deletions]) -> string
[484] Fix | Delete
[485] Fix | Delete
Return a copy of the string s, where all characters occurring
[486] Fix | Delete
in the optional argument deletions are removed, and the
[487] Fix | Delete
remaining characters have been mapped through the given
[488] Fix | Delete
translation table, which must be a string of length 256. The
[489] Fix | Delete
deletions argument is not allowed for Unicode strings.
[490] Fix | Delete
[491] Fix | Delete
"""
[492] Fix | Delete
if deletions or table is None:
[493] Fix | Delete
return s.translate(table, deletions)
[494] Fix | Delete
else:
[495] Fix | Delete
# Add s[:0] so that if s is Unicode and table is an 8-bit string,
[496] Fix | Delete
# table is converted to Unicode. This means that table *cannot*
[497] Fix | Delete
# be a dictionary -- for that feature, use u.translate() directly.
[498] Fix | Delete
return s.translate(table + s[:0])
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function