diff options
author | Guido van Rossum <guido@python.org> | 1997-10-24 14:46:16 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1997-10-24 14:46:16 (GMT) |
commit | 31626bce66635ee3f3950730eee20d096ddb3bde (patch) | |
tree | 2b628469731b944b67fdd09b64138b21968ea929 /Lib | |
parent | f81e5b9c780e47c8a312a69a7ff71169276a9720 (diff) | |
download | cpython-31626bce66635ee3f3950730eee20d096ddb3bde.zip cpython-31626bce66635ee3f3950730eee20d096ddb3bde.tar.gz cpython-31626bce66635ee3f3950730eee20d096ddb3bde.tar.bz2 |
re -> regex conversions by Sjoerd.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/fpformat.py | 12 | ||||
-rwxr-xr-x | Lib/mimify.py | 131 | ||||
-rw-r--r-- | Lib/pyclbr.py | 52 |
3 files changed, 100 insertions, 95 deletions
diff --git a/Lib/fpformat.py b/Lib/fpformat.py index 404738d..8ddd73c 100644 --- a/Lib/fpformat.py +++ b/Lib/fpformat.py @@ -14,13 +14,12 @@ import re # Compiled regular expression to "decode" a number -decoder = re.compile( \ - '^([-+]?)0*([0-9]*)((\.[0-9]*)?)(([eE][-+]?[0-9]+)?)$') +decoder = re.compile(r'^([-+]?)0*(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$') # \0 the whole thing # \1 leading sign or empty # \2 digits left of decimal point # \3 fraction (empty or begins with point) -# \5 exponent part (empty or begins with 'e' or 'E') +# \4 exponent part (empty or begins with 'e' or 'E') NotANumber = 'fpformat.NotANumber' @@ -30,9 +29,9 @@ NotANumber = 'fpformat.NotANumber' # fraction is 0 or more digits # expo is an integer def extract(s): - m = decoder.match(s) - if not m: raise NotANumber - sign, intpart, fraction, exppart = m.group(1, 2, 3, 5) + res = decoder.match(s) + if res is None: raise NotANumber + sign, intpart, fraction, exppart = res.group(1,2,3,4) if sign == '+': sign = '' if fraction: fraction = fraction[1:] if exppart: expo = eval(exppart[1:]) @@ -135,3 +134,4 @@ def test(): print x, fix(x, digs), sci(x, digs) except (EOFError, KeyboardInterrupt): pass + diff --git a/Lib/mimify.py b/Lib/mimify.py index 4c62c24..1b4af28 100755 --- a/Lib/mimify.py +++ b/Lib/mimify.py @@ -27,21 +27,16 @@ CHARSET = 'ISO-8859-1' # default charset for non-US-ASCII mail QUOTE = '> ' # string replies are quoted with # End configure -import regex, regsub, string - -qp = regex.compile('^content-transfer-encoding:[ \t]*quoted-printable', - regex.casefold) -base64_re = regex.compile('^content-transfer-encoding:[ \t]*base64', - regex.casefold) -mp = regex.compile('^content-type:[\000-\377]*multipart/[\000-\377]*boundary="?\\([^;"\n]*\\)', - regex.casefold) -chrset = regex.compile('^\\(content-type:.*charset="\\)\\(us-ascii\\|iso-8859-[0-9]+\\)\\("[\000-\377]*\\)', - regex.casefold) -he = regex.compile('^-*$') -mime_code = regex.compile('=\\([0-9a-f][0-9a-f]\\)', regex.casefold) -mime_head = regex.compile('=\\?iso-8859-1\\?q\\?\\([^? \t\n]+\\)\\?=', - regex.casefold) -repl = regex.compile('^subject:[ \t]+re: ', regex.casefold) +import re, string + +qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I) +base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I) +mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)', re.I|re.S) +chrset = re.compile('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)', re.I|re.S) +he = re.compile('^-*\n') +mime_code = re.compile('=([0-9a-f][0-9a-f])', re.I) +mime_head = re.compile('=\\?iso-8859-1\\?q\\?([^? \t\n]+)\\?=', re.I) +repl = re.compile('^subject:\\s+re: ', re.I) class File: '''A simple fake file object that knows about limited @@ -81,7 +76,7 @@ class HeaderFile: line = self.file.readline() if not line: return line - if he.match(line) >= 0: + if he.match(line): return line while 1: self.peek = self.file.readline() @@ -95,26 +90,26 @@ def mime_decode(line): '''Decode a single line of quoted-printable text to 8bit.''' newline = '' while 1: - i = mime_code.search(line) - if i < 0: + res = mime_code.search(line) + if res is None: break - newline = newline + line[:i] + \ - chr(string.atoi(mime_code.group(1), 16)) - line = line[i+3:] + newline = newline + line[:res.start(0)] + \ + chr(string.atoi(res.group(1), 16)) + line = line[res.end(0):] return newline + line def mime_decode_header(line): '''Decode a header line to 8bit.''' newline = '' while 1: - i = mime_head.search(line) - if i < 0: + res = mime_head.search(line) + if res is None: break - match0, match1 = mime_head.group(0, 1) + match = res.group(1) # convert underscores to spaces (before =XX conversion!) - match1 = string.join(string.split(match1, '_'), ' ') - newline = newline + line[:i] + mime_decode(match1) - line = line[i + len(match0):] + match = string.join(string.split(match, '_'), ' ') + newline = newline + line[:res.start(0)] + mime_decode(match) + line = line[res.end(0):] return newline + line def unmimify_part(ifile, ofile, decode_base64 = 0): @@ -140,19 +135,20 @@ def unmimify_part(ifile, ofile, decode_base64 = 0): else: pref = '' line = mime_decode_header(line) - if qp.match(line) >= 0: + if qp.match(line): quoted_printable = 1 continue # skip this header - if decode_base64 and base64_re.match(line) >= 0: + if decode_base64 and base64_re.match(line): is_base64 = 1 continue ofile.write(pref + line) - if not prefix and repl.match(line) >= 0: + if not prefix and repl.match(line): # we're dealing with a reply message is_repl = 1 - if mp.match(line) >= 0: - multipart = '--' + mp.group(1) - if he.match(line) >= 0: + mp_res = mp.match(line) + if mp_res: + multipart = '--' + mp_res.group(1) + if he.match(line): break if is_repl and (quoted_printable or multipart): is_repl = 0 @@ -162,7 +158,7 @@ def unmimify_part(ifile, ofile, decode_base64 = 0): line = ifile.readline() if not line: return - line = regsub.gsub(mime_head, '\\1', line) + line = re.sub(mime_head, '\\1', line) if prefix and line[:len(prefix)] == prefix: line = line[len(prefix):] pref = prefix @@ -216,8 +212,8 @@ def unmimify(infile, outfile, decode_base64 = 0): unmimify_part(nifile, ofile, decode_base64) ofile.flush() -mime_char = regex.compile('[=\240-\377]') # quote these chars in body -mime_header_char = regex.compile('[=?\240-\377]') # quote these in header +mime_char = re.compile('[=\240-\377]') # quote these chars in body +mime_header_char = re.compile('[=?\240-\377]') # quote these in header def mime_encode(line, header): '''Code a single line as quoted-printable. @@ -232,12 +228,12 @@ def mime_encode(line, header): newline = string.upper('=%02x' % ord('F')) line = line[1:] while 1: - i = reg.search(line) - if i < 0: + res = reg.search(line) + if res is None: break - newline = newline + line[:i] + \ - string.upper('=%02x' % ord(line[i])) - line = line[i+1:] + newline = newline + line[:res.start(0)] + \ + string.upper('=%02x' % ord(line[res.group(0)])) + line = line[res.end(0):] line = newline + line newline = '' @@ -250,25 +246,25 @@ def mime_encode(line, header): line = line[i:] return newline + line -mime_header = regex.compile('\\([ \t(]\\|^\\)\\([-a-zA-Z0-9_+]*[\240-\377][-a-zA-Z0-9_+\240-\377]*\\)\\([ \t)]\\|$\\)') +mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\240-\377][-a-zA-Z0-9_+\240-\377]*)([ \t)]|\n)') def mime_encode_header(line): '''Code a single header line as quoted-printable.''' newline = '' while 1: - i = mime_header.search(line) - if i < 0: + res = mime_header.search(line) + if res is None: break - newline = newline + line[:i] + mime_header.group(1) + \ + newline = newline + line[:res.start(0)] + res.group(1) + \ '=?' + CHARSET + '?Q?' + \ - mime_encode(mime_header.group(2), 1) + \ - '?=' + mime_header.group(3) - line = line[i+len(mime_header.group(0)):] + mime_encode(res.group(2), 1) + \ + '?=' + res.group(3) + line = line[res.end(0):] return newline + line -mv = regex.compile('^mime-version:', regex.casefold) -cte = regex.compile('^content-transfer-encoding:', regex.casefold) -iso_char = regex.compile('[\240-\377]') +mv = re.compile('^mime-version:', re.I) +cte = re.compile('^content-transfer-encoding:', re.I) +iso_char = re.compile('[\240-\377]') def mimify_part(ifile, ofile, is_mime): '''Convert an 8bit part of a MIME mail message to quoted-printable.''' @@ -286,19 +282,20 @@ def mimify_part(ifile, ofile, is_mime): line = hfile.readline() if not line: break - if not must_quote_header and iso_char.search(line) >= 0: + if not must_quote_header and iso_char.search(line): must_quote_header = 1 - if mv.match(line) >= 0: + if mv.match(line): is_mime = 1 - if cte.match(line) >= 0: + if cte.match(line): has_cte = 1 - if qp.match(line) >= 0: + if qp.match(line): is_qp = 1 - elif base64_re.match(line) >= 0: + elif base64_re.match(line): is_base64 = 1 - if mp.match(line) >= 0: - multipart = '--' + mp.group(1) - if he.match(line) >= 0: + mp_res = mp.match(line) + if mp_res: + multipart = '--' + mp_res.group(1) + if he.match(line): header_end = line break header.append(line) @@ -328,7 +325,7 @@ def mimify_part(ifile, ofile, is_mime): line = mime_decode(line) message.append(line) if not has_iso_chars: - if iso_char.search(line) >= 0: + if iso_char.search(line): has_iso_chars = must_quote_body = 1 if not must_quote_body: if len(line) > MAXLEN: @@ -338,16 +335,17 @@ def mimify_part(ifile, ofile, is_mime): for line in header: if must_quote_header: line = mime_encode_header(line) - if chrset.match(line) >= 0: + chrset_res = chrset.match(line) + if chrset_res: if has_iso_chars: # change us-ascii into iso-8859-1 - if string.lower(chrset.group(2)) == 'us-ascii': - line = chrset.group(1) + \ - CHARSET + chrset.group(3) + if string.lower(chrset_res.group(2)) == 'us-ascii': + line = chrset_res.group(1) + \ + CHARSET + chrset_res.group(3) else: # change iso-8859-* into us-ascii - line = chrset.group(1) + 'us-ascii' + chrset.group(3) - if has_cte and cte.match(line) >= 0: + line = chrset_res.group(1) + 'us-ascii' + chrset_res.group(3) + if has_cte and cte.match(line): line = 'Content-Transfer-Encoding: ' if is_base64: line = line + 'base64\n' @@ -445,3 +443,4 @@ if __name__ == '__main__' or (len(sys.argv) > 0 and sys.argv[0] == 'mimify'): if decode_base64: encode_args = encode_args + (decode_base64,) apply(encode, encode_args) + diff --git a/Lib/pyclbr.py b/Lib/pyclbr.py index a8a51af..cf93f3e 100644 --- a/Lib/pyclbr.py +++ b/Lib/pyclbr.py @@ -35,17 +35,17 @@ the hell out of the parser, but it usually works.''' import os import sys import imp -import regex +import re import string -id = '\\(<id>[A-Za-z_][A-Za-z0-9_]*\\)' # match identifier -blank_line = regex.compile('^[ \t]*\\($\\|#\\)') -is_class = regex.symcomp('^class[ \t]+'+id+'[ \t]*\\(<sup>([^)]*)\\)?[ \t]*:') -is_method = regex.symcomp('^[ \t]+def[ \t]+'+id+'[ \t]*(') -is_import = regex.symcomp('^import[ \t]*\\(<imp>[^#]+\\)') -is_from = regex.symcomp('^from[ \t]+'+id+'[ \t]+import[ \t]+\\(<imp>[^#]+\\)') -dedent = regex.compile('^[^ \t]') -indent = regex.compile('^[^ \t]*') +id = '(?P<id>[A-Za-z_][A-Za-z0-9_]*)' # match identifier +blank_line = re.compile('^[ \t]*($|#)') +is_class = re.compile('^class[ \t]+'+id+'[ \t]*(?P<sup>\([^)]*\))?[ \t]*:') +is_method = re.compile('^[ \t]+def[ \t]+'+id+'[ \t]*\(') +is_import = re.compile('^import[ \t]*(?P<imp>[^#]+)') +is_from = re.compile('^from[ \t]+'+id+'[ \t]+import[ \t]+(?P<imp>[^#]+)') +dedent = re.compile('^[^ \t]') +indent = re.compile('^[^ \t]*') _modules = {} # cache of modules we've seen @@ -116,14 +116,16 @@ def readmodule(module, path = []): break lineno = lineno + 1 # count lines line = line[:-1] # remove line feed - if blank_line.match(line) >= 0: + if blank_line.match(line): # ignore blank (and comment only) lines continue -## if indent.match(line) >= 0: -## indentation = len(string.expandtabs(indent.group(0), 8)) - if is_import.match(line) >= 0: +## res = indent.match(line) +## if res: +## indentation = len(string.expandtabs(res.group(0), 8)) + res = is_import.match(line) + if res: # import module - for n in string.splitfields(is_import.group('imp'), ','): + for n in string.splitfields(res.group('imp'), ','): n = string.strip(n) try: # recursively read the @@ -133,10 +135,11 @@ def readmodule(module, path = []): print 'module',n,'not found' pass continue - if is_from.match(line) >= 0: + res = is_from.match(line) + if res: # from module import stuff - mod = is_from.group('id') - names = string.splitfields(is_from.group('imp'), ',') + mod = res.group('id') + names = string.splitfields(res.group('imp'), ',') try: # recursively read the imported module d = readmodule(mod, path) @@ -161,10 +164,11 @@ def readmodule(module, path = []): not dict.has_key(n): dict[n] = d[n] continue - if is_class.match(line) >= 0: + res = is_class.match(line) + if res: # we found a class definition - class_name = is_class.group('id') - inherit = is_class.group('sup') + class_name = res.group('id') + inherit = res.group('sup') if inherit: # the class inherits from other classes inherit = string.strip(inherit[1:-1]) @@ -194,15 +198,17 @@ def readmodule(module, path = []): cur_class = Class(module, class_name, inherit, file, lineno) dict[class_name] = cur_class continue - if is_method.match(line) >= 0: + res = is_method.match(line) + if res: # found a method definition if cur_class: # and we know the class it belongs to - meth_name = is_method.group('id') + meth_name = res.group('id') cur_class._addmethod(meth_name, lineno) continue - if dedent.match(line) >= 0: + if dedent.match(line): # end of class definition cur_class = None f.close() return dict + |