| 1 |
from __future__ import absolute_import, print_function, division |
| 2 |
|
| 3 |
|
| 4 |
import abc |
| 5 |
import logging |
| 6 |
import sys |
| 7 |
import time |
| 8 |
|
| 9 |
|
| 10 |
from petl.compat import PY3 |
| 11 |
from petl.util.base import Table |
| 12 |
from petl.util.statistics import onlinestats |
| 13 |
|
| 14 |
|
| 15 |
def progress(table, batchsize=1000, prefix="", out=None): |
| 16 |
""" |
| 17 |
Report progress on rows passing through to a file or file-like object |
| 18 |
(defaults to sys.stderr). E.g.:: |
| 19 |
|
| 20 |
>>> import petl as etl |
| 21 |
>>> table = etl.dummytable(100000) |
| 22 |
>>> table.progress(10000).tocsv('example.csv') # doctest: +SKIP |
| 23 |
10000 rows in 0.13s (78363 row/s); batch in 0.13s (78363 row/s) |
| 24 |
20000 rows in 0.22s (91679 row/s); batch in 0.09s (110448 row/s) |
| 25 |
30000 rows in 0.31s (96573 row/s); batch in 0.09s (108114 row/s) |
| 26 |
40000 rows in 0.40s (99535 row/s); batch in 0.09s (109625 row/s) |
| 27 |
50000 rows in 0.49s (101396 row/s); batch in 0.09s (109591 row/s) |
| 28 |
60000 rows in 0.59s (102245 row/s); batch in 0.09s (106709 row/s) |
| 29 |
70000 rows in 0.68s (103221 row/s); batch in 0.09s (109498 row/s) |
| 30 |
80000 rows in 0.77s (103810 row/s); batch in 0.09s (108126 row/s) |
| 31 |
90000 rows in 0.90s (99465 row/s); batch in 0.13s (74516 row/s) |
| 32 |
100000 rows in 1.02s (98409 row/s); batch in 0.11s (89821 row/s) |
| 33 |
100000 rows in 1.02s (98402 row/s); batches in 0.10 +/- 0.02s [0.09-0.13] (100481 +/- 13340 rows/s [74516-110448]) |
| 34 |
|
| 35 |
See also :func:`petl.util.timing.clock`. |
| 36 |
|
| 37 |
""" |
| 38 |
|
| 39 |
return ProgressView(table, batchsize, prefix, out) |
| 40 |
|
| 41 |
|
| 42 |
def log_progress(table, batchsize=1000, prefix="", logger=None, level=logging.INFO): |
| 43 |
""" |
| 44 |
Report progress on rows passing through to a python logger. If logger is |
| 45 |
none, a new logger will be created that, by default, streams to stdout. E.g.:: |
| 46 |
|
| 47 |
>>> import petl as etl |
| 48 |
>>> table = etl.dummytable(100000) |
| 49 |
>>> table.log_progress(10000).tocsv('example.csv') # doctest: +SKIP |
| 50 |
10000 rows in 0.13s (78363 row/s); batch in 0.13s (78363 row/s) |
| 51 |
20000 rows in 0.22s (91679 row/s); batch in 0.09s (110448 row/s) |
| 52 |
30000 rows in 0.31s (96573 row/s); batch in 0.09s (108114 row/s) |
| 53 |
40000 rows in 0.40s (99535 row/s); batch in 0.09s (109625 row/s) |
| 54 |
50000 rows in 0.49s (101396 row/s); batch in 0.09s (109591 row/s) |
| 55 |
60000 rows in 0.59s (102245 row/s); batch in 0.09s (106709 row/s) |
| 56 |
70000 rows in 0.68s (103221 row/s); batch in 0.09s (109498 row/s) |
| 57 |
80000 rows in 0.77s (103810 row/s); batch in 0.09s (108126 row/s) |
| 58 |
90000 rows in 0.90s (99465 row/s); batch in 0.13s (74516 row/s) |
| 59 |
100000 rows in 1.02s (98409 row/s); batch in 0.11s (89821 row/s) |
| 60 |
100000 rows in 1.02s (98402 row/s); batches in 0.10 +/- 0.02s [0.09-0.13] (100481 +/- 13340 rows/s [74516-110448]) |
| 61 |
|
| 62 |
See also :func:`petl.util.timing.clock`. |
| 63 |
|
| 64 |
""" |
| 65 |
|
| 66 |
return LoggingProgressView(table, batchsize, prefix, logger, level=level) |
| 67 |
|
| 68 |
|
| 69 |
Table.progress = progress |
| 70 |
Table.log_progress = log_progress |
| 71 |
|
| 72 |
|
| 73 |
class ProgressViewBase(Table): |
| 74 |
""" |
| 75 |
Abstract base class for reporting on proecessing status |
| 76 |
""" |
| 77 |
|
| 78 |
def __init__(self, inner, batchsize, prefix): |
| 79 |
self.inner = inner |
| 80 |
self.batchsize = batchsize |
| 81 |
self.prefix = prefix |
| 82 |
|
| 83 |
@abc.abstractmethod |
| 84 |
def print_message(self, message): |
| 85 |
pass |
| 86 |
|
| 87 |
def __iter__(self): |
| 88 |
start = time.time() |
| 89 |
batchstart = start |
| 90 |
batchn = 0 |
| 91 |
batchtimemin, batchtimemax = None, None |
| 92 |
batchtimemean, batchtimevar = 0, 0 |
| 93 |
batchratemean, batchratevar = 0, 0 |
| 94 |
|
| 95 |
for n, r in enumerate(self.inner): |
| 96 |
if n % self.batchsize == 0 and n > 0: |
| 97 |
batchn += 1 |
| 98 |
batchend = time.time() |
| 99 |
batchtime = batchend - batchstart |
| 100 |
if batchtimemin is None or batchtime < batchtimemin: |
| 101 |
batchtimemin = batchtime |
| 102 |
if batchtimemax is None or batchtime > batchtimemax: |
| 103 |
batchtimemax = batchtime |
| 104 |
elapsedtime = batchend - start |
| 105 |
try: |
| 106 |
rate = int(n / elapsedtime) |
| 107 |
except ZeroDivisionError: |
| 108 |
rate = 0 |
| 109 |
try: |
| 110 |
batchrate = int(self.batchsize / batchtime) |
| 111 |
except ZeroDivisionError: |
| 112 |
batchrate = 0 |
| 113 |
v = (n, elapsedtime, rate, batchtime, batchrate) |
| 114 |
message = self.prefix + \ |
| 115 |
'%s rows in %.2fs (%s row/s); ' \ |
| 116 |
'batch in %.2fs (%s row/s)' % v |
| 117 |
self.print_message(message) |
| 118 |
batchstart = batchend |
| 119 |
batchtimemean, batchtimevar = \ |
| 120 |
onlinestats(batchtime, batchn, mean=batchtimemean, |
| 121 |
variance=batchtimevar) |
| 122 |
batchratemean, batchratevar = \ |
| 123 |
onlinestats(batchrate, batchn, mean=batchratemean, |
| 124 |
variance=batchratevar) |
| 125 |
yield r |
| 126 |
|
| 127 |
# compute total elapsed time and rate |
| 128 |
end = time.time() |
| 129 |
elapsedtime = end - start |
| 130 |
try: |
| 131 |
rate = int(n / elapsedtime) |
| 132 |
except ZeroDivisionError: |
| 133 |
rate = 0 |
| 134 |
|
| 135 |
# construct the final message |
| 136 |
if batchn > 1: |
| 137 |
if batchtimemin is None: |
| 138 |
batchtimemin = 0 |
| 139 |
if batchtimemax is None: |
| 140 |
batchtimemax = 0 |
| 141 |
try: |
| 142 |
batchratemin = int(self.batchsize / batchtimemax) |
| 143 |
except ZeroDivisionError: |
| 144 |
batchratemin = 0 |
| 145 |
try: |
| 146 |
batchratemax = int(self.batchsize / batchtimemin) |
| 147 |
except ZeroDivisionError: |
| 148 |
batchratemax = 0 |
| 149 |
v = (n, elapsedtime, rate, batchtimemean, batchtimevar**.5, |
| 150 |
batchtimemin, batchtimemax, int(batchratemean), |
| 151 |
int(batchratevar**.5), int(batchratemin), int(batchratemax)) |
| 152 |
message = self.prefix + '%s rows in %.2fs (%s row/s); batches in ' \ |
| 153 |
'%.2f +/- %.2fs [%.2f-%.2f] ' \ |
| 154 |
'(%s +/- %s rows/s [%s-%s])' % v |
| 155 |
else: |
| 156 |
v = (n, elapsedtime, rate) |
| 157 |
message = self.prefix + '%s rows in %.2fs (%s row/s)' % v |
| 158 |
|
| 159 |
self.print_message(message) |
| 160 |
|
| 161 |
|
| 162 |
class ProgressView(ProgressViewBase): |
| 163 |
""" |
| 164 |
Reports progress to a file_object like sys.stdout or a file handler |
| 165 |
""" |
| 166 |
|
| 167 |
def __init__(self, inner, batchsize, prefix, out): |
| 168 |
if out is None: |
| 169 |
self.file_object = sys.stderr |
| 170 |
else: |
| 171 |
self.file_object = out |
| 172 |
super(ProgressView, self).__init__(inner, batchsize, prefix) |
| 173 |
|
| 174 |
def print_message(self, message): |
| 175 |
print(message, file=self.file_object) |
| 176 |
if hasattr(self.file_object, 'flush'): |
| 177 |
self.file_object.flush() |
| 178 |
|
| 179 |
|
| 180 |
class LoggingProgressView(ProgressViewBase): |
| 181 |
""" |
| 182 |
Reports progress to a logger, log handler, or log adapter |
| 183 |
""" |
| 184 |
|
| 185 |
def __init__(self, inner, batchsize, prefix, logger, level=logging.INFO): |
| 186 |
if logger is None: |
| 187 |
self.logger = logging.getLogger(__name__) |
| 188 |
self.logger.setLevel(level) |
| 189 |
else: |
| 190 |
self.logger = logger |
| 191 |
self.level = level |
| 192 |
super(LoggingProgressView, self).__init__(inner, batchsize, prefix) |
| 193 |
|
| 194 |
def print_message(self, message): |
| 195 |
self.logger.log(self.level, message) |
| 196 |
|
| 197 |
|
| 198 |
def clock(table): |
| 199 |
""" |
| 200 |
Time how long is spent retrieving rows from the wrapped container. Enables |
| 201 |
diagnosis of which steps in a pipeline are taking the most time. E.g.:: |
| 202 |
|
| 203 |
>>> import petl as etl |
| 204 |
>>> t1 = etl.dummytable(100000) |
| 205 |
>>> c1 = etl.clock(t1) |
| 206 |
>>> t2 = etl.convert(c1, 'foo', lambda v: v**2) |
| 207 |
>>> c2 = etl.clock(t2) |
| 208 |
>>> p = etl.progress(c2, 10000) |
| 209 |
>>> etl.tocsv(p, 'example.csv') # doctest: +SKIP |
| 210 |
10000 rows in 0.23s (44036 row/s); batch in 0.23s (44036 row/s) |
| 211 |
20000 rows in 0.38s (52167 row/s); batch in 0.16s (63979 row/s) |
| 212 |
30000 rows in 0.54s (55749 row/s); batch in 0.15s (64624 row/s) |
| 213 |
40000 rows in 0.69s (57765 row/s); batch in 0.15s (64793 row/s) |
| 214 |
50000 rows in 0.85s (59031 row/s); batch in 0.15s (64707 row/s) |
| 215 |
60000 rows in 1.00s (59927 row/s); batch in 0.15s (64847 row/s) |
| 216 |
70000 rows in 1.16s (60483 row/s); batch in 0.16s (64051 row/s) |
| 217 |
80000 rows in 1.31s (61008 row/s); batch in 0.15s (64953 row/s) |
| 218 |
90000 rows in 1.47s (61356 row/s); batch in 0.16s (64285 row/s) |
| 219 |
100000 rows in 1.62s (61703 row/s); batch in 0.15s (65012 row/s) |
| 220 |
100000 rows in 1.62s (61700 row/s); batches in 0.16 +/- 0.02s [0.15-0.23] (62528 +/- 6173 rows/s [44036-65012]) |
| 221 |
>>> # time consumed retrieving rows from t1 |
| 222 |
... c1.time # doctest: +SKIP |
| 223 |
0.7243089999999492 |
| 224 |
>>> # time consumed retrieving rows from t2 |
| 225 |
... c2.time # doctest: +SKIP |
| 226 |
1.1704209999999766 |
| 227 |
>>> # actual time consumed by the convert step |
| 228 |
... c2.time - c1.time # doctest: +SKIP |
| 229 |
0.4461120000000274 |
| 230 |
|
| 231 |
See also :func:`petl.util.timing.progress`. |
| 232 |
|
| 233 |
""" |
| 234 |
|
| 235 |
return ClockView(table) |
| 236 |
|
| 237 |
|
| 238 |
Table.clock = clock |
| 239 |
|
| 240 |
|
| 241 |
class ClockView(Table): |
| 242 |
|
| 243 |
def __init__(self, wrapped): |
| 244 |
self.wrapped = wrapped |
| 245 |
|
| 246 |
def __iter__(self): |
| 247 |
self.time = 0 |
| 248 |
it = iter(self.wrapped) |
| 249 |
while True: |
| 250 |
before = time.perf_counter() if PY3 else time.clock() |
| 251 |
try: |
| 252 |
row = next(it) |
| 253 |
except StopIteration: |
| 254 |
return |
| 255 |
after = time.perf_counter() if PY3 else time.clock() |
| 256 |
self.time += (after - before) |
| 257 |
yield row |