- commit
- a200c59b906294246b9f90dc7580585175dce56c
- parent
- 80cd2bbd6858e2bd860a3ff6307f50849efea17a
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2022-02-25 14:08
refactor: use pairwise
Diffstat
| M | vtimelog | 33 | +++++++++++++++------------------ |
1 files changed, 15 insertions, 18 deletions
diff --git a/vtimelog b/vtimelog
@@ -24,6 +24,12 @@ EMPTY_LINE = object() 24 24 DT_FORMAT = '%Y-%m-%d %H:%M' 25 25 26 26 -1 27 def pairwise(items): -1 28 # backport of itertools.pairwise from python 3.10 -1 29 for i in range(len(items) - 1): -1 30 yield items[i], items[i + 1] -1 31 -1 32 27 33 class ParseError(Exception): 28 34 def __init__(self, line, msg=''): 29 35 self.line = line @@ -165,27 +171,18 @@ class Extractor: 165 171 self.data = tuple(data) 166 172 167 173 def sum(self):168 -1 x = timedelta()169 -1 last = None170 -1 for entry in self.data:171 -1 if last is not None:172 -1 if '**' not in entry['comment']:173 -1 x += entry['dt'] - last['dt']174 -1 last = entry175 -1 return x-1 174 return sum(( -1 175 entry['dt'] - prev['dt'] -1 176 for prev, entry in pairwise(self.data) -1 177 if '**' not in entry['comment'] -1 178 ), start=timedelta()) 176 179 177 180 def by_comment(self): 178 181 d = {}179 -1 last = None180 -1 for entry in self.data:181 -1 if last is not None:182 -1 if '**' not in entry['comment']:183 -1 delta = entry['dt'] - last['dt']184 -1 if entry['comment'] in d:185 -1 d[entry['comment']] += delta186 -1 else:187 -1 d[entry['comment']] = delta188 -1 last = entry-1 182 for prev, entry in pairwise(self.data): -1 183 if '**' not in entry['comment']: -1 184 d.setdefault(entry['comment'], timedelta()) -1 185 d[entry['comment']] += entry['dt'] - prev['dt'] 189 186 return d 190 187 191 188