-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytes_io_stats_wrapper.py
161 lines (121 loc) · 4.46 KB
/
bytes_io_stats_wrapper.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from io import IOBase, BufferedIOBase
from typing import List
class BytesIOStatsWrapper(IOBase):
def __init__(self, source: BufferedIOBase):
self._source = source
self._char_num = 0
self._line_num = -1
def read(self, *args, **kw):
data = self._source.read(*args, **kw)
self._char_num += len(data)
self._line_num += data.count(b"\n")
return data
def readline(self, limit=None):
data = self._source.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[bytes]:
data = self._source.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
def close(self):
"""
Flush and close the IO object.
This method has no effect if the file is already closed.
"""
return self._source.close()
def fileno(self):
"""
Returns underlying file descriptor if one exists.
OSError is raised if the IO object does not use a file descriptor.
"""
return self._source.fileno()
def flush(self):
"""
Flush write buffers, if applicable.
This is not implemented for read-only and non-blocking streams.
"""
return self._source.flush()
def isatty(self):
"""
Return whether this is an 'interactive' stream.
Return False if it can't be determined.
"""
return self._source.isatty()
def readable(self):
"""
Return whether object was opened for reading.
If False, read() will raise OSError.
"""
return self._source.readable()
def seek(self, *args, **kwargs):
"""
Change stream position.
Change the stream position to the given byte offset. The offset is
interpreted relative to the position indicated by whence. Values
for whence are:
* 0 -- start of stream (the default); offset should be zero or positive
* 1 -- current stream position; offset may be negative
* 2 -- end of stream; offset is usually negative
Return the new absolute position.
"""
return self._source.seek(*args, **kwargs)
def seekable(self):
"""
Return whether object supports random access.
If False, seek(), tell() and truncate() will raise OSError.
This method may need to do a test seek().
"""
return self._source.seekable()
def tell(self):
""" Return current stream position. """
return self._source.tell()
def truncate(self, *args, **kwargs):
"""
Truncate file to size bytes.
File pointer is left unchanged. Size defaults to the current IO
position as reported by tell(). Returns the new size.
"""
return self._source.truncate(*args, **kwargs)
def writable(self):
"""
Return whether object was opened for writing.
If False, write() will raise OSError.
"""
return self._source.writable()
def writelines(self, *args, **kwargs):
"""
Write a list of lines to stream.
Line separators are not added, so it is usual for each of the
lines provided to have a line separator at the end.
"""
return self._source.writelines(*args, **kwargs)
def _checkClosed(self, *args, **kwargs):
return self._source._checkClosed(*args, **kwargs)
def __del__(self, *args, **kwargs):
return self._source.__del__()
def __enter__(self, *args, **kwargs):
return self._source.__enter__()
def __exit__(self, *args, **kwargs):
return self._source.__exit__(*args, **kwargs)
def __iter__(self, *args, **kwargs):
""" Implement iter(self). """
return self._source.__iter__()
@staticmethod
def __new__(*args, **kwargs):
""" Create and return a new object. See help(type) for accurate signature. """
return IOBase.__new__(*args, **kwargs)
def __next__(self): # real signature unknown
""" Implement next(self). """
return self._source.__next__()
closed = property(lambda self: object(), lambda self, v: None,
lambda self: None) # default
__dict__ = None