[enh][mod] moar list comprehension

This commit is contained in:
asciimoo 2012-05-27 13:49:22 +02:00
parent 8a9f5a5365
commit e4a301ca30
2 changed files with 3 additions and 12 deletions

View File

@ -3,7 +3,7 @@ EXREX
### Description ### Description
Exrex is a tool that generates all matching string to a given regular expression. Exrex is a tool that generates all matching strings to a given regular expression.
### Command line usage ### Command line usage

View File

@ -40,11 +40,7 @@ def _p(d, append=False):
for i in d: for i in d:
if len(ranges) and i[0] != 'range': if len(ranges) and i[0] != 'range':
if len(ret): if len(ret):
tmp_ret = [] ret = [r+char for char in ranges for r in ret]
for char in ranges:
for k,_ in enumerate(ret):
tmp_ret.append(ret[k]+char)
ret = tmp_ret
else: else:
ret = ranges ret = ranges
ranges = [] ranges = []
@ -67,15 +63,10 @@ def _p(d, append=False):
if len(tmp_ret): if len(tmp_ret):
ret = tmp_ret ret = tmp_ret
elif i[0] == 'in': elif i[0] == 'in':
tmp_ret = [] ret = [r+piece for piece in _p(list(i[1]), True) for r in ret]
for piece in _p(list(i[1]), True):
for k,_ in enumerate(ret):
tmp_ret.append(ret[k]+piece)
ret = tmp_ret
elif i[0] == 'range': elif i[0] == 'range':
ranges.extend(map(chr, range(i[1][0], i[1][1]+1))) ranges.extend(map(chr, range(i[1][0], i[1][1]+1)))
elif i[0] == 'max_repeat': elif i[0] == 'max_repeat':
tmp_ret = []
chars = [x for x in _p(list(i[1][2])) if x != ''] chars = [x for x in _p(list(i[1][2])) if x != '']
ret = [r+''.join(piece) for rep in range(i[1][0], i[1][1]+1) for piece in product(*repeat(chars, rep)) for r in ret] ret = [r+''.join(piece) for rep in range(i[1][0], i[1][1]+1) for piece in product(*repeat(chars, rep)) for r in ret]
# tmp_ret = [] # tmp_ret = []