add Files to handle opening and iteration over multiple files
This commit is contained in:
parent
d8c3df88ab
commit
71fee9897b
@ -159,4 +159,31 @@ def dotted_key_set(dict_or_list, dotted_key, value, separator='.'):
|
|||||||
elif isinstance(dict_or_list, (tuple, list)):
|
elif isinstance(dict_or_list, (tuple, list)):
|
||||||
newkey = separator.join(keys[i:])
|
newkey = separator.join(keys[i:])
|
||||||
for item in dict_or_list:
|
for item in dict_or_list:
|
||||||
dotted_key_set(item, newkey, value, separator)
|
dotted_key_set(item, newkey, value, separator)
|
||||||
|
|
||||||
|
#
|
||||||
|
# utility classes
|
||||||
|
#
|
||||||
|
|
||||||
|
class Files(object):
|
||||||
|
|
||||||
|
def __init__(self, file_open_callback=None, *args):
|
||||||
|
self.paths = []
|
||||||
|
for arg in args:
|
||||||
|
self.add(arg)
|
||||||
|
self.file_open_callback = file_open_callback
|
||||||
|
|
||||||
|
def add(self, path):
|
||||||
|
self.paths.append(path)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self.linereader()
|
||||||
|
|
||||||
|
def linereader(self):
|
||||||
|
for path in iter(self.paths):
|
||||||
|
if os.path.exists(path):
|
||||||
|
self.file_open_callback(path)
|
||||||
|
f = open(path)
|
||||||
|
for line in f:
|
||||||
|
yield line
|
||||||
|
f.close()
|
Loading…
Reference in New Issue
Block a user