home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 1.5)
-
- '''Common string manipulations.
-
- Public module variables:
-
- whitespace -- a string containing all characters considered whitespace
- lowercase -- a string containing all characters considered lowercase letters
- uppercase -- a string containing all characters considered uppercase letters
- letters -- a string containing all characters considered letters
- digits -- a string containing all characters considered decimal digits
- hexdigits -- a string containing all characters considered hexadecimal digits
- octdigits -- a string containing all characters considered octal digits
-
- '''
- whitespace = ' \t\n\r\x0b\x0c'
- lowercase = 'abcdefghijklmnopqrstuvwxyz'
- uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- letters = lowercase + uppercase
- digits = '0123456789'
- hexdigits = digits + 'abcdef' + 'ABCDEF'
- octdigits = '01234567'
- _idmap = ''
- for i in range(256):
- _idmap = _idmap + chr(i)
-
- _lower = _idmap[:ord('A')] + lowercase + _idmap[ord('Z') + 1:]
- _upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z') + 1:]
- _swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z') + 1:]
- del i
- index_error = ValueError
- atoi_error = ValueError
- atof_error = ValueError
- atol_error = ValueError
-
- def lower(s):
- '''lower(s) -> string
-
- \tReturn a copy of the string s converted to lowercase.
-
- \t'''
- res = ''
- for c in s:
- res = res + _lower[ord(c)]
-
- return res
-
-
- def upper(s):
- '''upper(s) -> string
-
- \tReturn a copy of the string s converted to uppercase.
-
- \t'''
- res = ''
- for c in s:
- res = res + _upper[ord(c)]
-
- return res
-
-
- def swapcase(s):
- '''swapcase(s) -> string
-
- \tReturn a copy of the string s with upper case characters
- \tconverted to lowercase and vice versa.
-
- \t'''
- res = ''
- for c in s:
- res = res + _swapcase[ord(c)]
-
- return res
-
-
- def strip(s):
- '''strip(s) -> string
-
- \tReturn a copy of the string s with leading and trailing
- \twhitespace removed.
-
- \t'''
- (i, j) = (0, len(s))
- while i < j and s[i] in whitespace:
- i = i + 1
- while i < j and s[j - 1] in whitespace:
- j = j - 1
- return s[i:j]
-
-
- def lstrip(s):
- '''lstrip(s) -> string
-
- \tReturn a copy of the string s with leading whitespace removed.
-
- \t'''
- (i, j) = (0, len(s))
- while i < j and s[i] in whitespace:
- i = i + 1
- return s[i:j]
-
-
- def rstrip(s):
- '''rstrip(s) -> string
-
- \tReturn a copy of the string s with trailing whitespace
- \tremoved.
-
- \t'''
- (i, j) = (0, len(s))
- while i < j and s[j - 1] in whitespace:
- j = j - 1
- return s[i:j]
-
-
- def split(s, sep = None, maxsplit = 0):
- '''split(str [,sep [,maxsplit]]) -> list of strings
-
- \tReturn a list of the words in the string s, using sep as the
- \tdelimiter string. If maxsplit is nonzero, splits into at most
- \tmaxsplit words If sep is not specified, any whitespace string
- \tis a separator. Maxsplit defaults to 0.
-
- \t(split and splitfields are synonymous)
-
- \t'''
- if sep is not None:
- return splitfields(s, sep, maxsplit)
-
- res = []
- (i, n) = (0, len(s))
- if maxsplit <= 0:
- maxsplit = n
-
- count = 0
- while i < n:
- while i < n and s[i] in whitespace:
- i = i + 1
- if i == n:
- break
-
- if count >= maxsplit:
- res.append(s[i:])
- break
-
- j = i
- while j < n and s[j] not in whitespace:
- j = j + 1
- count = count + 1
- res.append(s[i:j])
- i = j
- return res
-
-
- def splitfields(s, sep = None, maxsplit = 0):
- '''splitfields(str [,sep [,maxsplit]]) -> list of strings
-
- \tReturn a list of the words in the string s, using sep as the
- \tdelimiter string. If maxsplit is nonzero, splits into at most
- \tmaxsplit words If sep is not specified, any whitespace string
- \tis a separator. Maxsplit defaults to 0.
-
- \t(split and splitfields are synonymous)
-
- \t'''
- if sep is None:
- return split(s, None, maxsplit)
-
- res = []
- nsep = len(sep)
- if nsep == 0:
- return [
- s]
-
- ns = len(s)
- if maxsplit <= 0:
- maxsplit = ns
-
- i = j = 0
- count = 0
- while j + nsep <= ns:
- if s[j:j + nsep] == sep:
- count = count + 1
- res.append(s[i:j])
- i = j = j + nsep
- if count >= maxsplit:
- break
-
- else:
- j = j + 1
- res.append(s[i:])
- return res
-
-
- def join(words, sep = ' '):
- '''join(list [,sep]) -> string
-
- \tReturn a string composed of the words in list, with
- \tintervening occurences of sep. Sep defaults to a single
- \tspace.
-
- \t(joinfields and join are synonymous)
-
- \t'''
- return joinfields(words, sep)
-
-
- def joinfields(words, sep = ' '):
- '''joinfields(list [,sep]) -> string
-
- \tReturn a string composed of the words in list, with
- \tintervening occurences of sep. The default separator is a
- \tsingle space.
-
- \t(joinfields and join are synonymous)
-
- \t'''
- res = ''
- for w in words:
- res = res + sep + w
-
- return res[len(sep):]
-
-
- def index(s, sub, i = 0, last = None):
- '''index(s, sub [,start [,end]]) -> int
-
- \tReturn the lowest index in s where substring sub is found,
- \tsuch that sub is contained within s[start,end]. Optional
- \targuments start and end are interpreted as in slice notation.
-
- \tRaise ValueError if not found.
-
- \t'''
- if last is None:
- last = len(s)
-
- res = find(s, sub, i, last)
- if res < 0:
- raise ValueError, 'substring not found in string.index'
-
- return res
-
-
- def rindex(s, sub, i = 0, last = None):
- '''rindex(s, sub [,start [,end]]) -> int
-
- \tReturn the highest index in s where substring sub is found,
- \tsuch that sub is contained within s[start,end]. Optional
- \targuments start and end are interpreted as in slice notation.
-
- \tRaise ValueError if not found.
-
- \t'''
- if last is None:
- last = len(s)
-
- res = rfind(s, sub, i, last)
- if res < 0:
- raise ValueError, 'substring not found in string.index'
-
- return res
-
-
- def count(s, sub, i = 0, last = None):
- '''count(s, sub[, start[,end]]) -> int
-
- \tReturn the number of occurrences of substring sub in string
- \ts[start:end]. Optional arguments start and end are
- \tinterpreted as in slice notation.
-
- \t'''
- Slen = len(s)
- if last is None:
- last = Slen
- elif last < 0:
- last = max(0, last + Slen)
- elif last > Slen:
- last = Slen
-
- if i < 0:
- i = max(0, i + Slen)
-
- n = len(sub)
- m = last + 1 - n
- if n == 0:
- return m - i
-
- r = 0
- while i < m:
- if sub == s[i:i + n]:
- r = r + 1
- i = i + n
- else:
- i = i + 1
- return r
-
-
- def find(s, sub, i = 0, last = None):
- '''find(s, sub [,start [,end]]) -> in
-
- \tReturn the lowest index in s where substring sub is found,
- \tsuch that sub is contained within s[start,end]. Optional
- \targuments start and end are interpreted as in slice notation.
-
- \tReturn -1 on failure.
-
- \t'''
- Slen = len(s)
- if last is None:
- last = Slen
- elif last < 0:
- last = max(0, last + Slen)
- elif last > Slen:
- last = Slen
-
- if i < 0:
- i = max(0, i + Slen)
-
- n = len(sub)
- m = last + 1 - n
- while i < m:
- if sub == s[i:i + n]:
- return i
-
- i = i + 1
- return -1
-
-
- def rfind(s, sub, i = 0, last = None):
- '''rfind(s, sub [,start [,end]]) -> int
-
- \tReturn the highest index in s where substring sub is found,
- \tsuch that sub is contained within s[start,end]. Optional
- \targuments start and end are interpreted as in slice notation.
-
- \tReturn -1 on failure.
-
- \t'''
- Slen = len(s)
- if last is None:
- last = Slen
- elif last < 0:
- last = max(0, last + Slen)
- elif last > Slen:
- last = Slen
-
- if i < 0:
- i = max(0, i + Slen)
-
- n = len(sub)
- m = last + 1 - n
- r = -1
- while i < m:
- if sub == s[i:i + n]:
- r = i
-
- i = i + 1
- return r
-
- _safe_env = {
- '__builtins__': { } }
- _re = None
-
- def atof(str):
- '''atof(s) -> float
-
- \tReturn the floating point number represented by the string s.
-
- \t'''
- global _re, _re
- if _re is None:
-
- try:
- import re
- except ImportError:
- _re = 0
-
- _re = re
-
- sign = ''
- s = strip(str)
- if s and s[0] in '+-':
- sign = s[0]
- s = s[1:]
-
- if not s:
- raise ValueError, 'non-float argument to string.atof'
-
- while s[0] == '0' and len(s) > 1 and s[1] in digits:
- s = s[1:]
- if _re and not _re.match('[0-9]*(\\.[0-9]*)?([eE][-+]?[0-9]+)?$', s):
- raise ValueError, 'non-float argument to string.atof'
-
-
- try:
- return float(eval(sign + s, _safe_env))
- except SyntaxError:
- raise ValueError, 'non-float argument to string.atof'
-
-
-
- def atoi(str, base = 10):
- '''atoi(s [,base]) -> int
-
- \tReturn the integer represented by the string s in the given
- \tbase, which defaults to 10. The string s must consist of one
- \tor more digits, possibly preceded by a sign. If base is 0, it
- \tis chosen from the leading characters of s, 0 for octal, 0x or
- \t0X for hexadecimal. If base is 16, a preceding 0x or 0X is
- \taccepted.
-
- \t'''
- if base != 10:
- raise ValueError, "this string.atoi doesn't support base != 10"
-
- sign = ''
- s = strip(str)
- if s and s[0] in '+-':
- sign = s[0]
- s = s[1:]
-
- if not s:
- raise ValueError, 'non-integer argument to string.atoi'
-
- while s[0] == '0' and len(s) > 1:
- s = s[1:]
- for c in s:
- pass
-
- return eval(sign + s, _safe_env)
-
-
- def atol(str, base = 10):
- '''atol(s [,base]) -> long
-
- \tReturn the long integer represented by the string s in the
- \tgiven base, which defaults to 10. The string s must consist
- \tof one or more digits, possibly preceded by a sign. If base
- \tis 0, it is chosen from the leading characters of s, 0 for
- \toctal, 0x or 0X for hexadecimal. If base is 16, a preceding
- \t0x or 0X is accepted. A trailing L or l is not accepted,
- \tunless base is 0.
-
- \t'''
- if base != 10:
- raise ValueError, "this string.atol doesn't support base != 10"
-
- sign = ''
- s = strip(str)
- if s and s[0] in '+-':
- sign = s[0]
- s = s[1:]
-
- if not s:
- raise ValueError, 'non-integer argument to string.atol'
-
- while s[0] == '0' and len(s) > 1:
- s = s[1:]
- for c in s:
- pass
-
- return eval(sign + s + 'L', _safe_env)
-
-
- def ljust(s, width):
- '''ljust(s, width) -> string
-
- \tReturn a left-justified version of s, in a field of the
- \tspecified width, padded with spaces as needed. The string is
- \tnever truncated.
-
- \t'''
- n = width - len(s)
- if n <= 0:
- return s
-
- return s + ' ' * n
-
-
- def rjust(s, width):
- '''rjust(s, width) -> string
-
- \tReturn a right-justified version of s, in a field of the
- \tspecified width, padded with spaces as needed. The string is
- \tnever truncated.
-
- \t'''
- n = width - len(s)
- if n <= 0:
- return s
-
- return ' ' * n + s
-
-
- def center(s, width):
- '''center(s, width) -> string
-
- \tReturn a center version of s, in a field of the specified
- \twidth. padded with spaces as needed. The string is never
- \ttruncated.
-
- \t'''
- n = width - len(s)
- if n <= 0:
- return s
-
- half = n / 2
- if n % 2 and width % 2:
- half = half + 1
-
- return ' ' * half + s + ' ' * (n - half)
-
-
- def zfill(x, width):
- '''zfill(x, width) -> string
-
- \tPad a numeric string x with zeros on the left, to fill a field
- \tof the specified width. The string x is never truncated.
-
- \t'''
- if type(x) == type(''):
- s = x
- else:
- s = `x`
- n = len(s)
- if n >= width:
- return s
-
- sign = ''
- if s[0] in ('-', '+'):
- (sign, s) = (s[0], s[1:])
-
- return sign + '0' * (width - n) + s
-
-
- def expandtabs(s, tabsize = 8):
- '''expandtabs(s [,tabsize]) -> string
-
- \tReturn a copy of the string s with all tab characters replaced
- \tby the appropriate number of spaces, depending on the current
- \tcolumn, and the tabsize (default 8).
-
- \t'''
- res = line = ''
- for c in s:
- line = line + c
- if c == '\n':
- res = res + line
- line = ''
-
-
- return res + line
-
-
- def translate(s, table, deletions = ''):
- '''translate(s,table [,deletechars]) -> string
-
- \tReturn a copy of the string s, where all characters occurring
- \tin the optional argument deletechars are removed, and the
- \tremaining characters have been mapped through the given
- \ttranslation table, which must be a string of length 256.
-
- \t'''
- if type(table) != type('') or len(table) != 256:
- raise TypeError, 'translation table must be 256 characters long'
-
- res = ''
- for c in s:
- pass
-
- return res
-
-
- def capitalize(s):
- '''capitalize(s) -> string
-
- \tReturn a copy of the string s with only its first character
- \tcapitalized.
-
- \t'''
- return upper(s[:1]) + lower(s[1:])
-
-
- def capwords(s, sep = None):
- '''capwords(s, [sep]) -> string
-
- \tSplit the argument into words using split, capitalize each
- \tword using capitalize, and join the capitalized words using
- \tjoin. Note that this replaces runs of whitespace characters by
- \ta single space.
-
- \t'''
- if not sep:
- pass
- return join(map(capitalize, split(s, sep)), ' ')
-
- _idmapL = None
-
- def maketrans(fromstr, tostr):
- '''maketrans(frm, to) -> string
-
- \tReturn a translation table (a string of 256 bytes long)
- \tsuitable for use in string.translate. The strings frm and to
- \tmust be of the same length.
-
- \t'''
- global _idmapL
- if len(fromstr) != len(tostr):
- raise ValueError, 'maketrans arguments must have same length'
-
- if not _idmapL:
- _idmapL = map(None, _idmap)
-
- L = _idmapL[:]
- fromstr = map(ord, fromstr)
- for i in range(len(fromstr)):
- L[fromstr[i]] = tostr[i]
-
- return joinfields(L, '')
-
-
- def replace(str, old, new, maxsplit = 0):
- '''replace (str, old, new[, maxsplit]) -> string
-
- \tReturn a copy of string str with all occurrences of substring
- \told replaced by new. If the optional argument maxsplit is
- \tgiven, only the first maxsplit occurrences are replaced.
-
- \t'''
- return joinfields(splitfields(str, old, maxsplit), new)
-
-
- try:
- *
- except ImportError:
- 0
- 0
- range(256)
- except:
- 0
-
-