-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.py
More file actions
executable file
·93 lines (74 loc) · 2.22 KB
/
gen.py
File metadata and controls
executable file
·93 lines (74 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import fnmatch
import gzip
import bz2
import re
from io import TextIOWrapper
def gen_find(filepat, top):
'''
Find all filenames in a directory tree that match a shell wildcard
pattern
'''
for path, dirlist, filelist in os.walk(top):
for name in fnmatch.filter(filelist, filepat):
yield os.path.join(path, name)
def gen_opener(filenames):
'''
Open a sequence of filenames one at a time producing a file object.
The file is closed immediately when proceeding to the next iteration
'''
for filename in filenames:
if filename.endswith('.gz'):
f = gzip.open(filename, 'rt')
elif filename.endswith('.bz2'):
f = bz2.open(filename, 'rt')
else:
f = open(filename, 'rt')
yield f
f.close()
def gen_concatenate(iterators):
'''
Chain a sequence of iterators together into a single sequence.
'''
for it in iterators:
yield from it
def gen_grep(pattern, lines):
'''
Look for a regex pattern in a sequence of lines
'''
pat = re.compile(pattern)
for line in lines:
if pat.search(line):
yield line
if __name__ == '__main__':
expected_gen_find = (
f'test_gen/{folder}/{letter}.txt'
for folder in ('foo', 'bar')
for letter in ('a', 'b', 'c')
)
got_gen_find = gen_find('[a-z].txt', 'test_gen/')
assert set(expected_gen_find) == set(got_gen_find)
files = gen_opener(gen_find('[a-z].*', 'test_gen/'))
for file in files:
assert isinstance(file, TextIOWrapper)
def gen_generators():
'''
Este generador genera generador
gen_concatenate tiene el deber de aplanar dichos generador
para que actúe como un generador no anidado
'''
for i in range(3):
yield (i for _ in range(3))
got_concat = gen_concatenate(gen_generators())
expected_concat = (0, 0, 0, 1, 1, 1, 2, 2, 2)
assert expected_concat == tuple(got_concat)
lines = [
'c',
'c++',
'python',
'python3',
'java'
]
filtered = gen_grep('python', lines)
expected = ('python', 'python3')
assert tuple(filtered) == expected