-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffered_writer.py
65 lines (53 loc) · 2.01 KB
/
buffered_writer.py
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
from os import path
from time import sleep
class BufferedWriter():
"""
For each file out of an arbitrary large amount of files the BufferedWriter stores a
list of lines. Only when they exceed a predefined limit their contents are
collectively written to the filesystem. By preventing repeated opening and closing of
files the BufferedWriter reduces computation time.
"""
def __init__(self, out_dir=".", lines_per_file=25):
"""
Args:
out_dir (str, optional): Base path for all files you want to write to.
Defaults to ".".
lines_per_file (int, optional): Maximum number of lines to buffer for each file.
Defaults to 25.
"""
self.buffer = {}
self.lines_per_file = lines_per_file
self.out_dir = out_dir
def __enter__(self):
return self
def __exit__(self, exec_type, exec_val, exec_tb):
self.flush()
def flush_file(self, file_path: str):
"""
Write buffered content to chosen file
Args:
file_path (str):
"""
with open(path.join(self.out_dir, file_path), "a") as file:
for line in self.buffer[file_path]:
file.write(line+"\n")
self.buffer[file_path] = []
def flush(self):
"""
Write all buffered content to their respective files
"""
for file_path in self.buffer.keys():
self.flush_file(file_path)
def write_line(self, file_path: str, line: str):
"""
Add a line to the buffer
Args:
file_path (str):
line (str):
"""
try:
self.buffer[file_path].append(line)
except:
self.buffer[file_path] = [line]
if len(self.buffer[file_path]) > self.lines_per_file:
self.flush_file(file_path)