| 1219 | # 返回一个进度指示条,传入总量,每走一格调用一次 next |
| 1220 | def progress (self, total): |
| 1221 | class ProgressIndicator (object): |
| 1222 | def __init__ (self, total): |
| 1223 | self.count = 0 |
| 1224 | self.percent = -1 |
| 1225 | self.total = total |
| 1226 | self.timestamp = time.time() |
| 1227 | self.counter = {} |
| 1228 | def next (self): |
| 1229 | if self.total: |
| 1230 | self.count += 1 |
| 1231 | pc = self.count * 100 / self.total |
| 1232 | if pc != self.percent: |
| 1233 | self.percent = pc |
| 1234 | print('progress: %d%%'%pc) |
| 1235 | def inc (self, name): |
| 1236 | if name not in self.counter: |
| 1237 | self.counter[name] = 1 |
| 1238 | else: |
| 1239 | self.counter[name] += 1 |
| 1240 | def done (self): |
| 1241 | t = (time.time() - self.timestamp) |
| 1242 | keys = list(self.counter.keys()) |
| 1243 | keys.sort() |
| 1244 | for key in keys: |
| 1245 | print('[%s] -> %d'%(key, self.counter[key])) |
| 1246 | print('[Finished in %d seconds (%d)]'%(t, self.count)) |
| 1247 | return ProgressIndicator(total) |
| 1248 | |
| 1249 | # 返回词典里所有词的 map,默认转为小写 |