timelog

GTimeLog compatible command line tools
git clone https://git.ce9e.org/timelog.git

commit
c1df28af1b921643fdd931d64fba9a66e8d31984
parent
926db4475d8a41e29c8072295182d3a3250a0069
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2022-02-25 14:49
refactor LazyTimelog

Diffstat

M vtimelog 49 +++++++++++++++++++------------------------------

1 files changed, 19 insertions, 30 deletions


diff --git a/vtimelog b/vtimelog

@@ -20,7 +20,6 @@ import argparse
   20    20 import os
   21    21 from datetime import datetime, timedelta
   22    22 
   23    -1 EMPTY_LINE = object()
   24    23 DT_FORMAT = '%Y-%m-%d %H:%M'
   25    24 
   26    25 
@@ -30,39 +29,29 @@ def pairwise(items):
   30    29 		yield items[i], items[i + 1]
   31    30 
   32    31 
   33    -1 class LazyTuple(tuple):
   34    -1 	def __init__(self, src):
   35    -1 		self._src = tuple(src)
   36    -1 		self._data = []
   37    -1 		for line in self._src:
   38    -1 			self._data.append(None)
   -1    32 class LazyMap:
   -1    33 	def __init__(self, src, fn):
   -1    34 		self._src = src
   -1    35 		self._fn = fn
   -1    36 		self._cache = {}
   39    37 
   40    38 	def __len__(self):
   41    39 		return len(self._src)
   42    40 
   43    -1 	def __iter__(self):
   44    -1 		for i in range(len(self)):
   45    -1 			yield self[i]
   -1    41 	def __getitem__(self, key):
   -1    42 		if isinstance(key, int) and key < 0:
   -1    43 			key += len(self)
   -1    44 		if key not in self._cache:
   -1    45 			self._cache[key] = self._fn(self._src[key])
   -1    46 		return self._cache[key]
   46    47 
   47    -1 	def __getitem__(self, i):
   48    -1 		if self._data[i] is None:
   49    -1 			self._data[i] = self.parse(i)
   50    -1 		return self._data[i]
   51    48 
   52    -1 	def parse(self, i):
   53    -1 		raise NotImplementedError()
   54    -1 
   55    -1 
   56    -1 class LazyTimelog(LazyTuple):
   57    -1 	def parse(self, i):
   58    -1 		s = self._src[i]
   59    -1 		if s == '':
   60    -1 			return EMPTY_LINE
   61    -1 		dt, comment = s.split(': ', 1)
   62    -1 		return {
   63    -1 			'dt': datetime.strptime(dt, DT_FORMAT),
   64    -1 			'comment': comment
   65    -1 		}
   -1    49 def parse_line(line):
   -1    50 	dt, comment = line.split(': ', 1)
   -1    51 	return {
   -1    52 		'dt': datetime.strptime(dt, DT_FORMAT),
   -1    53 		'comment': comment
   -1    54 	}
   66    55 
   67    56 
   68    57 def datetime_add(dt, years=0, months=0, weeks=0, days=0, hours=0, minutes=0,
@@ -158,7 +147,7 @@ class Query:
  158   147 
  159   148 class Extractor:
  160   149 	def __init__(self, data):
  161    -1 		self.data = tuple(data)
   -1   150 		self.data = data
  162   151 
  163   152 	def sum(self):
  164   153 		return sum((
@@ -232,7 +221,7 @@ if __name__ == '__main__':
  232   221 	# load data from file
  233   222 	with open(args.file) as fh:
  234   223 		l = [line.strip() for line in fh.readlines() if line.strip()]
  235    -1 		data = LazyTimelog(l)
   -1   224 		data = LazyMap(l, parse_line)
  236   225 
  237   226 	# filter
  238   227 	q = Query(data)