Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31626bc

Browse files
committed
re -> regex conversions by Sjoerd.
1 parent f81e5b9 commit 31626bc

3 files changed

Lines changed: 100 additions & 95 deletions

File tree

Lib/fpformat.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
import re
1515

1616
# Compiled regular expression to "decode" a number
17-
decoder = re.compile( \
18-
'^([-+]?)0*([0-9]*)((\.[0-9]*)?)(([eE][-+]?[0-9]+)?)$')
17+
decoder = re.compile(r'^([-+]?)0*(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$')
1918
# \0 the whole thing
2019
# \1 leading sign or empty
2120
# \2 digits left of decimal point
2221
# \3 fraction (empty or begins with point)
23-
# \5 exponent part (empty or begins with 'e' or 'E')
22+
# \4 exponent part (empty or begins with 'e' or 'E')
2423

2524
NotANumber = 'fpformat.NotANumber'
2625

@@ -30,9 +29,9 @@
3029
# fraction is 0 or more digits
3130
# expo is an integer
3231
def extract(s):
33-
m = decoder.match(s)
34-
if not m: raise NotANumber
35-
sign, intpart, fraction, exppart = m.group(1, 2, 3, 5)
32+
res = decoder.match(s)
33+
if res is None: raise NotANumber
34+
sign, intpart, fraction, exppart = res.group(1,2,3,4)
3635
if sign == '+': sign = ''
3736
if fraction: fraction = fraction[1:]
3837
if exppart: expo = eval(exppart[1:])
@@ -135,3 +134,4 @@ def test():
135134
print x, fix(x, digs), sci(x, digs)
136135
except (EOFError, KeyboardInterrupt):
137136
pass
137+

Lib/mimify.py

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,16 @@
2727
QUOTE = '> ' # string replies are quoted with
2828
# End configure
2929

30-
import regex, regsub, string
31-
32-
qp = regex.compile('^content-transfer-encoding:[ \t]*quoted-printable',
33-
regex.casefold)
34-
base64_re = regex.compile('^content-transfer-encoding:[ \t]*base64',
35-
regex.casefold)
36-
mp = regex.compile('^content-type:[\000-\377]*multipart/[\000-\377]*boundary="?\\([^;"\n]*\\)',
37-
regex.casefold)
38-
chrset = regex.compile('^\\(content-type:.*charset="\\)\\(us-ascii\\|iso-8859-[0-9]+\\)\\("[\000-\377]*\\)',
39-
regex.casefold)
40-
he = regex.compile('^-*$')
41-
mime_code = regex.compile('=\\([0-9a-f][0-9a-f]\\)', regex.casefold)
42-
mime_head = regex.compile('=\\?iso-8859-1\\?q\\?\\([^? \t\n]+\\)\\?=',
43-
regex.casefold)
44-
repl = regex.compile('^subject:[ \t]+re: ', regex.casefold)
30+
import re, string
31+
32+
qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I)
33+
base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I)
34+
mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)', re.I|re.S)
35+
chrset = re.compile('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)', re.I|re.S)
36+
he = re.compile('^-*\n')
37+
mime_code = re.compile('=([0-9a-f][0-9a-f])', re.I)
38+
mime_head = re.compile('=\\?iso-8859-1\\?q\\?([^? \t\n]+)\\?=', re.I)
39+
repl = re.compile('^subject:\\s+re: ', re.I)
4540

4641
class File:
4742
'''A simple fake file object that knows about limited
@@ -81,7 +76,7 @@ def readline(self):
8176
line = self.file.readline()
8277
if not line:
8378
return line
84-
if he.match(line) >= 0:
79+
if he.match(line):
8580
return line
8681
while 1:
8782
self.peek = self.file.readline()
@@ -95,26 +90,26 @@ def mime_decode(line):
9590
'''Decode a single line of quoted-printable text to 8bit.'''
9691
newline = ''
9792
while 1:
98-
i = mime_code.search(line)
99-
if i < 0:
93+
res = mime_code.search(line)
94+
if res is None:
10095
break
101-
newline = newline + line[:i] + \
102-
chr(string.atoi(mime_code.group(1), 16))
103-
line = line[i+3:]
96+
newline = newline + line[:res.start(0)] + \
97+
chr(string.atoi(res.group(1), 16))
98+
line = line[res.end(0):]
10499
return newline + line
105100

106101
def mime_decode_header(line):
107102
'''Decode a header line to 8bit.'''
108103
newline = ''
109104
while 1:
110-
i = mime_head.search(line)
111-
if i < 0:
105+
res = mime_head.search(line)
106+
if res is None:
112107
break
113-
match0, match1 = mime_head.group(0, 1)
108+
match = res.group(1)
114109
# convert underscores to spaces (before =XX conversion!)
115-
match1 = string.join(string.split(match1, '_'), ' ')
116-
newline = newline + line[:i] + mime_decode(match1)
117-
line = line[i + len(match0):]
110+
match = string.join(string.split(match, '_'), ' ')
111+
newline = newline + line[:res.start(0)] + mime_decode(match)
112+
line = line[res.end(0):]
118113
return newline + line
119114

120115
def unmimify_part(ifile, ofile, decode_base64 = 0):
@@ -140,19 +135,20 @@ def unmimify_part(ifile, ofile, decode_base64 = 0):
140135
else:
141136
pref = ''
142137
line = mime_decode_header(line)
143-
if qp.match(line) >= 0:
138+
if qp.match(line):
144139
quoted_printable = 1
145140
continue # skip this header
146-
if decode_base64 and base64_re.match(line) >= 0:
141+
if decode_base64 and base64_re.match(line):
147142
is_base64 = 1
148143
continue
149144
ofile.write(pref + line)
150-
if not prefix and repl.match(line) >= 0:
145+
if not prefix and repl.match(line):
151146
# we're dealing with a reply message
152147
is_repl = 1
153-
if mp.match(line) >= 0:
154-
multipart = '--' + mp.group(1)
155-
if he.match(line) >= 0:
148+
mp_res = mp.match(line)
149+
if mp_res:
150+
multipart = '--' + mp_res.group(1)
151+
if he.match(line):
156152
break
157153
if is_repl and (quoted_printable or multipart):
158154
is_repl = 0
@@ -162,7 +158,7 @@ def unmimify_part(ifile, ofile, decode_base64 = 0):
162158
line = ifile.readline()
163159
if not line:
164160
return
165-
line = regsub.gsub(mime_head, '\\1', line)
161+
line = re.sub(mime_head, '\\1', line)
166162
if prefix and line[:len(prefix)] == prefix:
167163
line = line[len(prefix):]
168164
pref = prefix
@@ -216,8 +212,8 @@ def unmimify(infile, outfile, decode_base64 = 0):
216212
unmimify_part(nifile, ofile, decode_base64)
217213
ofile.flush()
218214

219-
mime_char = regex.compile('[=\240-\377]') # quote these chars in body
220-
mime_header_char = regex.compile('[=?\240-\377]') # quote these in header
215+
mime_char = re.compile('[=\240-\377]') # quote these chars in body
216+
mime_header_char = re.compile('[=?\240-\377]') # quote these in header
221217

222218
def mime_encode(line, header):
223219
'''Code a single line as quoted-printable.
@@ -232,12 +228,12 @@ def mime_encode(line, header):
232228
newline = string.upper('=%02x' % ord('F'))
233229
line = line[1:]
234230
while 1:
235-
i = reg.search(line)
236-
if i < 0:
231+
res = reg.search(line)
232+
if res is None:
237233
break
238-
newline = newline + line[:i] + \
239-
string.upper('=%02x' % ord(line[i]))
240-
line = line[i+1:]
234+
newline = newline + line[:res.start(0)] + \
235+
string.upper('=%02x' % ord(line[res.group(0)]))
236+
line = line[res.end(0):]
241237
line = newline + line
242238

243239
newline = ''
@@ -250,25 +246,25 @@ def mime_encode(line, header):
250246
line = line[i:]
251247
return newline + line
252248

253-
mime_header = regex.compile('\\([ \t(]\\|^\\)\\([-a-zA-Z0-9_+]*[\240-\377][-a-zA-Z0-9_+\240-\377]*\\)\\([ \t)]\\|$\\)')
249+
mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\240-\377][-a-zA-Z0-9_+\240-\377]*)([ \t)]|\n)')
254250

255251
def mime_encode_header(line):
256252
'''Code a single header line as quoted-printable.'''
257253
newline = ''
258254
while 1:
259-
i = mime_header.search(line)
260-
if i < 0:
255+
res = mime_header.search(line)
256+
if res is None:
261257
break
262-
newline = newline + line[:i] + mime_header.group(1) + \
258+
newline = newline + line[:res.start(0)] + res.group(1) + \
263259
'=?' + CHARSET + '?Q?' + \
264-
mime_encode(mime_header.group(2), 1) + \
265-
'?=' + mime_header.group(3)
266-
line = line[i+len(mime_header.group(0)):]
260+
mime_encode(res.group(2), 1) + \
261+
'?=' + res.group(3)
262+
line = line[res.end(0):]
267263
return newline + line
268264

269-
mv = regex.compile('^mime-version:', regex.casefold)
270-
cte = regex.compile('^content-transfer-encoding:', regex.casefold)
271-
iso_char = regex.compile('[\240-\377]')
265+
mv = re.compile('^mime-version:', re.I)
266+
cte = re.compile('^content-transfer-encoding:', re.I)
267+
iso_char = re.compile('[\240-\377]')
272268

273269
def mimify_part(ifile, ofile, is_mime):
274270
'''Convert an 8bit part of a MIME mail message to quoted-printable.'''
@@ -286,19 +282,20 @@ def mimify_part(ifile, ofile, is_mime):
286282
line = hfile.readline()
287283
if not line:
288284
break
289-
if not must_quote_header and iso_char.search(line) >= 0:
285+
if not must_quote_header and iso_char.search(line):
290286
must_quote_header = 1
291-
if mv.match(line) >= 0:
287+
if mv.match(line):
292288
is_mime = 1
293-
if cte.match(line) >= 0:
289+
if cte.match(line):
294290
has_cte = 1
295-
if qp.match(line) >= 0:
291+
if qp.match(line):
296292
is_qp = 1
297-
elif base64_re.match(line) >= 0:
293+
elif base64_re.match(line):
298294
is_base64 = 1
299-
if mp.match(line) >= 0:
300-
multipart = '--' + mp.group(1)
301-
if he.match(line) >= 0:
295+
mp_res = mp.match(line)
296+
if mp_res:
297+
multipart = '--' + mp_res.group(1)
298+
if he.match(line):
302299
header_end = line
303300
break
304301
header.append(line)
@@ -328,7 +325,7 @@ def mimify_part(ifile, ofile, is_mime):
328325
line = mime_decode(line)
329326
message.append(line)
330327
if not has_iso_chars:
331-
if iso_char.search(line) >= 0:
328+
if iso_char.search(line):
332329
has_iso_chars = must_quote_body = 1
333330
if not must_quote_body:
334331
if len(line) > MAXLEN:
@@ -338,16 +335,17 @@ def mimify_part(ifile, ofile, is_mime):
338335
for line in header:
339336
if must_quote_header:
340337
line = mime_encode_header(line)
341-
if chrset.match(line) >= 0:
338+
chrset_res = chrset.match(line)
339+
if chrset_res:
342340
if has_iso_chars:
343341
# change us-ascii into iso-8859-1
344-
if string.lower(chrset.group(2)) == 'us-ascii':
345-
line = chrset.group(1) + \
346-
CHARSET + chrset.group(3)
342+
if string.lower(chrset_res.group(2)) == 'us-ascii':
343+
line = chrset_res.group(1) + \
344+
CHARSET + chrset_res.group(3)
347345
else:
348346
# change iso-8859-* into us-ascii
349-
line = chrset.group(1) + 'us-ascii' + chrset.group(3)
350-
if has_cte and cte.match(line) >= 0:
347+
line = chrset_res.group(1) + 'us-ascii' + chrset_res.group(3)
348+
if has_cte and cte.match(line):
351349
line = 'Content-Transfer-Encoding: '
352350
if is_base64:
353351
line = line + 'base64\n'
@@ -445,3 +443,4 @@ def mimify(infile, outfile):
445443
if decode_base64:
446444
encode_args = encode_args + (decode_base64,)
447445
apply(encode, encode_args)
446+

Lib/pyclbr.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@
3535
import os
3636
import sys
3737
import imp
38-
import regex
38+
import re
3939
import string
4040

41-
id = '\\(<id>[A-Za-z_][A-Za-z0-9_]*\\)' # match identifier
42-
blank_line = regex.compile('^[ \t]*\\($\\|#\\)')
43-
is_class = regex.symcomp('^class[ \t]+'+id+'[ \t]*\\(<sup>([^)]*)\\)?[ \t]*:')
44-
is_method = regex.symcomp('^[ \t]+def[ \t]+'+id+'[ \t]*(')
45-
is_import = regex.symcomp('^import[ \t]*\\(<imp>[^#]+\\)')
46-
is_from = regex.symcomp('^from[ \t]+'+id+'[ \t]+import[ \t]+\\(<imp>[^#]+\\)')
47-
dedent = regex.compile('^[^ \t]')
48-
indent = regex.compile('^[^ \t]*')
41+
id = '(?P<id>[A-Za-z_][A-Za-z0-9_]*)' # match identifier
42+
blank_line = re.compile('^[ \t]*($|#)')
43+
is_class = re.compile('^class[ \t]+'+id+'[ \t]*(?P<sup>\([^)]*\))?[ \t]*:')
44+
is_method = re.compile('^[ \t]+def[ \t]+'+id+'[ \t]*\(')
45+
is_import = re.compile('^import[ \t]*(?P<imp>[^#]+)')
46+
is_from = re.compile('^from[ \t]+'+id+'[ \t]+import[ \t]+(?P<imp>[^#]+)')
47+
dedent = re.compile('^[^ \t]')
48+
indent = re.compile('^[^ \t]*')
4949

5050
_modules = {} # cache of modules we've seen
5151

@@ -116,14 +116,16 @@ def readmodule(module, path = []):
116116
break
117117
lineno = lineno + 1 # count lines
118118
line = line[:-1] # remove line feed
119-
if blank_line.match(line) >= 0:
119+
if blank_line.match(line):
120120
# ignore blank (and comment only) lines
121121
continue
122-
## if indent.match(line) >= 0:
123-
## indentation = len(string.expandtabs(indent.group(0), 8))
124-
if is_import.match(line) >= 0:
122+
## res = indent.match(line)
123+
## if res:
124+
## indentation = len(string.expandtabs(res.group(0), 8))
125+
res = is_import.match(line)
126+
if res:
125127
# import module
126-
for n in string.splitfields(is_import.group('imp'), ','):
128+
for n in string.splitfields(res.group('imp'), ','):
127129
n = string.strip(n)
128130
try:
129131
# recursively read the
@@ -133,10 +135,11 @@ def readmodule(module, path = []):
133135
print 'module',n,'not found'
134136
pass
135137
continue
136-
if is_from.match(line) >= 0:
138+
res = is_from.match(line)
139+
if res:
137140
# from module import stuff
138-
mod = is_from.group('id')
139-
names = string.splitfields(is_from.group('imp'), ',')
141+
mod = res.group('id')
142+
names = string.splitfields(res.group('imp'), ',')
140143
try:
141144
# recursively read the imported module
142145
d = readmodule(mod, path)
@@ -161,10 +164,11 @@ def readmodule(module, path = []):
161164
not dict.has_key(n):
162165
dict[n] = d[n]
163166
continue
164-
if is_class.match(line) >= 0:
167+
res = is_class.match(line)
168+
if res:
165169
# we found a class definition
166-
class_name = is_class.group('id')
167-
inherit = is_class.group('sup')
170+
class_name = res.group('id')
171+
inherit = res.group('sup')
168172
if inherit:
169173
# the class inherits from other classes
170174
inherit = string.strip(inherit[1:-1])
@@ -194,15 +198,17 @@ def readmodule(module, path = []):
194198
cur_class = Class(module, class_name, inherit, file, lineno)
195199
dict[class_name] = cur_class
196200
continue
197-
if is_method.match(line) >= 0:
201+
res = is_method.match(line)
202+
if res:
198203
# found a method definition
199204
if cur_class:
200205
# and we know the class it belongs to
201-
meth_name = is_method.group('id')
206+
meth_name = res.group('id')
202207
cur_class._addmethod(meth_name, lineno)
203208
continue
204-
if dedent.match(line) >= 0:
209+
if dedent.match(line):
205210
# end of class definition
206211
cur_class = None
207212
f.close()
208213
return dict
214+

0 commit comments

Comments
 (0)