-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_io_stats_wrapper.py
More file actions
35 lines (28 loc) · 906 Bytes
/
text_io_stats_wrapper.py
File metadata and controls
35 lines (28 loc) · 906 Bytes
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
import io
from typing import List
class TextIOStatsWrapper(io.TextIOWrapper):
def __init__(self, *args, **kw):
super().__init__(*args, **kw)
self._char_num = 0
self._line_num = -1
def read(self, *args, **kw):
data = super().read(*args, **kw)
self._char_num += len(data)
self._line_num += data.count("\n")
return data
def readline(self, limit=None):
data = super().readline(limit if limit is not None else -1)
self._char_num += len(data)
self._line_num += 1
return data
def readlines(self, hint=-1) -> List[str]:
data = super().readlines(hint)
self._char_num += sum(len(d) for d in data)
self._line_num += len(data)
return data
@property
def char_num(self):
return self._char_num
@property
def line_num(self):
return self._line_num