String method conversion.

This commit is contained in:
Eric S. Raymond 2001-02-09 10:48:30 +00:00
parent be18552874
commit be9b507bdd
2 changed files with 42 additions and 42 deletions

View File

@ -11,7 +11,7 @@
""" """
import string, sys import sys
# Try importing the _locale module. # Try importing the _locale module.
# #
@ -140,24 +140,24 @@ def str(val):
"""Convert float to integer, taking the locale into account.""" """Convert float to integer, taking the locale into account."""
return format("%.12g",val) return format("%.12g",val)
def atof(str,func=string.atof): def atof(str,func=float):
"Parses a string as a float according to the locale settings." "Parses a string as a float according to the locale settings."
#First, get rid of the grouping #First, get rid of the grouping
ts = localeconv()['thousands_sep'] ts = localeconv()['thousands_sep']
if ts: if ts:
s=string.split(str,ts) s=str.split(ts)
str=string.join(s, "") str="".join(s)
#next, replace the decimal point with a dot #next, replace the decimal point with a dot
dd = localeconv()['decimal_point'] dd = localeconv()['decimal_point']
if dd: if dd:
s=string.split(str,dd) s=str.split(dd)
str=string.join(s, '.') str='.'.join(s)
#finally, parse the string #finally, parse the string
return func(str) return func(str)
def atoi(str): def atoi(str):
"Converts a string to an integer according to the locale settings." "Converts a string to an integer according to the locale settings."
return atof(str,string.atoi) return atof(str, int)
def _test(): def _test():
setlocale(LC_ALL, "") setlocale(LC_ALL, "")
@ -194,12 +194,12 @@ def normalize(localename):
""" """
# Normalize the locale name and extract the encoding # Normalize the locale name and extract the encoding
fullname = string.lower(localename) fullname = localename.lower()
if ':' in fullname: if ':' in fullname:
# ':' is sometimes used as encoding delimiter. # ':' is sometimes used as encoding delimiter.
fullname = string.replace(fullname, ':', '.') fullname = fullname.replace(':', '.')
if '.' in fullname: if '.' in fullname:
langname, encoding = string.split(fullname, '.')[:2] langname, encoding = fullname.split('.')[:2]
fullname = langname + '.' + encoding fullname = langname + '.' + encoding
else: else:
langname = fullname langname = fullname
@ -214,7 +214,7 @@ def normalize(localename):
code = locale_alias.get(langname, None) code = locale_alias.get(langname, None)
if code is not None: if code is not None:
if '.' in code: if '.' in code:
langname, defenc = string.split(code, '.') langname, defenc = code.split('.')
else: else:
langname = code langname = code
defenc = '' defenc = ''
@ -246,7 +246,7 @@ def _parse_localename(localename):
""" """
code = normalize(localename) code = normalize(localename)
if '.' in code: if '.' in code:
return string.split(code, '.')[:2] return code.split('.')[:2]
elif code == 'C': elif code == 'C':
return None, None return None, None
else: else:

View File

@ -195,9 +195,9 @@ class XMLParser:
rescan = 0 rescan = 0
if str[0] == '#': if str[0] == '#':
if str[1] == 'x': if str[1] == 'x':
str = chr(string.atoi(str[2:], 16)) str = chr(int(str[2:], 16))
else: else:
str = chr(string.atoi(str[1:])) str = chr(int(str[1:]))
if data[i - 1] != ';': if data[i - 1] != ';':
self.syntax_error("`;' missing after char reference") self.syntax_error("`;' missing after char reference")
i = i-1 i = i-1
@ -245,7 +245,7 @@ class XMLParser:
if self.nomoretags: if self.nomoretags:
data = rawdata[i:n] data = rawdata[i:n]
self.handle_data(data) self.handle_data(data)
self.lineno = self.lineno + string.count(data, '\n') self.lineno = self.lineno + data.count('\n')
i = n i = n
break break
res = interesting.search(rawdata, i) res = interesting.search(rawdata, i)
@ -263,7 +263,7 @@ class XMLParser:
if not self.__accept_utf8 and illegal.search(data): if not self.__accept_utf8 and illegal.search(data):
self.syntax_error('illegal character in content') self.syntax_error('illegal character in content')
self.handle_data(data) self.handle_data(data)
self.lineno = self.lineno + string.count(data, '\n') self.lineno = self.lineno + data.count('\n')
i = j i = j
if i == n: break if i == n: break
if rawdata[i] == '<': if rawdata[i] == '<':
@ -271,37 +271,37 @@ class XMLParser:
if self.literal: if self.literal:
data = rawdata[i] data = rawdata[i]
self.handle_data(data) self.handle_data(data)
self.lineno = self.lineno + string.count(data, '\n') self.lineno = self.lineno + data.count('\n')
i = i+1 i = i+1
continue continue
k = self.parse_starttag(i) k = self.parse_starttag(i)
if k < 0: break if k < 0: break
self.__seen_starttag = 1 self.__seen_starttag = 1
self.lineno = self.lineno + string.count(rawdata[i:k], '\n') self.lineno = self.lineno + rawdata[i:k].count('\n')
i = k i = k
continue continue
if endtagopen.match(rawdata, i): if endtagopen.match(rawdata, i):
k = self.parse_endtag(i) k = self.parse_endtag(i)
if k < 0: break if k < 0: break
self.lineno = self.lineno + string.count(rawdata[i:k], '\n') self.lineno = self.lineno + rawdata[i:k].count('\n')
i = k i = k
continue continue
if commentopen.match(rawdata, i): if commentopen.match(rawdata, i):
if self.literal: if self.literal:
data = rawdata[i] data = rawdata[i]
self.handle_data(data) self.handle_data(data)
self.lineno = self.lineno + string.count(data, '\n') self.lineno = self.lineno + data.count('\n')
i = i+1 i = i+1
continue continue
k = self.parse_comment(i) k = self.parse_comment(i)
if k < 0: break if k < 0: break
self.lineno = self.lineno + string.count(rawdata[i:k], '\n') self.lineno = self.lineno + rawdata[i:k].count('\n')
i = k i = k
continue continue
if cdataopen.match(rawdata, i): if cdataopen.match(rawdata, i):
k = self.parse_cdata(i) k = self.parse_cdata(i)
if k < 0: break if k < 0: break
self.lineno = self.lineno + string.count(rawdata[i:k], '\n') self.lineno = self.lineno + rawdata[i:k].count('\n')
i = k i = k
continue continue
res = xmldecl.match(rawdata, i) res = xmldecl.match(rawdata, i)
@ -322,7 +322,7 @@ class XMLParser:
if res: if res:
k = self.parse_proc(i) k = self.parse_proc(i)
if k < 0: break if k < 0: break
self.lineno = self.lineno + string.count(rawdata[i:k], '\n') self.lineno = self.lineno + rawdata[i:k].count('\n')
i = k i = k
continue continue
res = doctype.match(rawdata, i) res = doctype.match(rawdata, i)
@ -330,7 +330,7 @@ class XMLParser:
if self.literal: if self.literal:
data = rawdata[i] data = rawdata[i]
self.handle_data(data) self.handle_data(data)
self.lineno = self.lineno + string.count(data, '\n') self.lineno = self.lineno + data.count('\n')
i = i+1 i = i+1
continue continue
if self.__seen_doctype: if self.__seen_doctype:
@ -341,8 +341,8 @@ class XMLParser:
if k < 0: break if k < 0: break
self.__seen_doctype = res.group('name') self.__seen_doctype = res.group('name')
if self.__map_case: if self.__map_case:
self.__seen_doctype = string.lower(self.__seen_doctype) self.__seen_doctype = self.__seen_doctype.lower()
self.lineno = self.lineno + string.count(rawdata[i:k], '\n') self.lineno = self.lineno + rawdata[i:k].count('\n')
i = k i = k
continue continue
elif rawdata[i] == '&': elif rawdata[i] == '&':
@ -360,7 +360,7 @@ class XMLParser:
if not self.stack: if not self.stack:
self.syntax_error('data not in content') self.syntax_error('data not in content')
self.handle_charref(res.group('char')[:-1]) self.handle_charref(res.group('char')[:-1])
self.lineno = self.lineno + string.count(res.group(0), '\n') self.lineno = self.lineno + res.group(0).count('\n')
continue continue
res = entityref.match(rawdata, i) res = entityref.match(rawdata, i)
if res is not None: if res is not None:
@ -370,14 +370,14 @@ class XMLParser:
i = i-1 i = i-1
name = res.group('name') name = res.group('name')
if self.__map_case: if self.__map_case:
name = string.lower(name) name = name.lower()
if self.entitydefs.has_key(name): if self.entitydefs.has_key(name):
self.rawdata = rawdata = rawdata[:res.start(0)] + self.entitydefs[name] + rawdata[i:] self.rawdata = rawdata = rawdata[:res.start(0)] + self.entitydefs[name] + rawdata[i:]
n = len(rawdata) n = len(rawdata)
i = res.start(0) i = res.start(0)
else: else:
self.unknown_entityref(name) self.unknown_entityref(name)
self.lineno = self.lineno + string.count(res.group(0), '\n') self.lineno = self.lineno + res.group(0).count('\n')
continue continue
elif rawdata[i] == ']': elif rawdata[i] == ']':
if self.literal: if self.literal:
@ -406,7 +406,7 @@ class XMLParser:
if not self.__accept_utf8 and illegal.search(data): if not self.__accept_utf8 and illegal.search(data):
self.syntax_error('illegal character in content') self.syntax_error('illegal character in content')
self.handle_data(data) self.handle_data(data)
self.lineno = self.lineno + string.count(data, '\n') self.lineno = self.lineno + data.count('\n')
self.rawdata = rawdata[i+1:] self.rawdata = rawdata[i+1:]
return self.goahead(end) return self.goahead(end)
self.rawdata = rawdata[i:] self.rawdata = rawdata[i:]
@ -442,11 +442,11 @@ class XMLParser:
n = len(rawdata) n = len(rawdata)
name = res.group('name') name = res.group('name')
if self.__map_case: if self.__map_case:
name = string.lower(name) name = name.lower()
pubid, syslit = res.group('pubid', 'syslit') pubid, syslit = res.group('pubid', 'syslit')
if pubid is not None: if pubid is not None:
pubid = pubid[1:-1] # remove quotes pubid = pubid[1:-1] # remove quotes
pubid = string.join(string.split(pubid)) # normalize pubid = ' '.join(pubid.split()) # normalize
if syslit is not None: syslit = syslit[1:-1] # remove quotes if syslit is not None: syslit = syslit[1:-1] # remove quotes
j = k = res.end(0) j = k = res.end(0)
if k >= n: if k >= n:
@ -516,7 +516,7 @@ class XMLParser:
k = res.end(0) k = res.end(0)
name = res.group(0) name = res.group(0)
if self.__map_case: if self.__map_case:
name = string.lower(name) name = name.lower()
if name == 'xml:namespace': if name == 'xml:namespace':
self.syntax_error('old-fashioned namespace declaration') self.syntax_error('old-fashioned namespace declaration')
self.__use_namespaces = -1 self.__use_namespaces = -1
@ -541,7 +541,7 @@ class XMLParser:
self.syntax_error('xml:namespace prefix not unique') self.syntax_error('xml:namespace prefix not unique')
self.__namespaces[prefix] = attrdict['ns'] self.__namespaces[prefix] = attrdict['ns']
else: else:
if string.lower(name) == 'xml': if name.lower() == 'xml':
self.syntax_error('illegal processing instruction target name') self.syntax_error('illegal processing instruction target name')
self.handle_proc(name, rawdata[k:j]) self.handle_proc(name, rawdata[k:j])
return end.end(0) return end.end(0)
@ -557,7 +557,7 @@ class XMLParser:
break break
attrname, attrvalue = res.group('name', 'value') attrname, attrvalue = res.group('name', 'value')
if self.__map_case: if self.__map_case:
attrname = string.lower(attrname) attrname = attrname.lower()
i = res.end(0) i = res.end(0)
if attrvalue is None: if attrvalue is None:
self.syntax_error("no value specified for attribute `%s'" % attrname) self.syntax_error("no value specified for attribute `%s'" % attrname)
@ -579,7 +579,7 @@ class XMLParser:
self.syntax_error("`<' illegal in attribute value") self.syntax_error("`<' illegal in attribute value")
if attrdict.has_key(attrname): if attrdict.has_key(attrname):
self.syntax_error("attribute `%s' specified twice" % attrname) self.syntax_error("attribute `%s' specified twice" % attrname)
attrvalue = string.translate(attrvalue, attrtrans) attrvalue = attrvalue.translate(attrtrans)
attrdict[attrname] = self.translate_references(attrvalue) attrdict[attrname] = self.translate_references(attrvalue)
return attrdict, namespace, i return attrdict, namespace, i
@ -596,7 +596,7 @@ class XMLParser:
return end.end(0) return end.end(0)
nstag = tagname = tag.group('tagname') nstag = tagname = tag.group('tagname')
if self.__map_case: if self.__map_case:
nstag = tagname = string.lower(nstag) nstag = tagname = nstag.lower()
if not self.__seen_starttag and self.__seen_doctype and \ if not self.__seen_starttag and self.__seen_doctype and \
tagname != self.__seen_doctype: tagname != self.__seen_doctype:
self.syntax_error('starttag does not match DOCTYPE') self.syntax_error('starttag does not match DOCTYPE')
@ -636,7 +636,7 @@ class XMLParser:
if res is not None: if res is not None:
aprefix, key = res.group('prefix', 'local') aprefix, key = res.group('prefix', 'local')
if self.__map_case: if self.__map_case:
key = string.lower(key) key = key.lower()
if aprefix is None: if aprefix is None:
aprefix = '' aprefix = ''
ans = None ans = None
@ -686,7 +686,7 @@ class XMLParser:
else: else:
tag = res.group(0) tag = res.group(0)
if self.__map_case: if self.__map_case:
tag = string.lower(tag) tag = tag.lower()
if self.literal: if self.literal:
if not self.stack or tag != self.stack[-1][0]: if not self.stack or tag != self.stack[-1][0]:
self.handle_data(rawdata[i]) self.handle_data(rawdata[i])
@ -754,10 +754,10 @@ class XMLParser:
def handle_charref(self, name): def handle_charref(self, name):
try: try:
if name[0] == 'x': if name[0] == 'x':
n = string.atoi(name[1:], 16) n = int(name[1:], 16)
else: else:
n = string.atoi(name) n = int(name)
except string.atoi_error: except ValueError:
self.unknown_charref(name) self.unknown_charref(name)
return return
if not 0 <= n <= 255: if not 0 <= n <= 255: