From c3812fc2e7455fcf75b0fa54789e60b8fd7b4379 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Tue, 24 Mar 2020 20:12:44 +0000 Subject: [PATCH] handle more fixmes, add make_dict --- .gitignore | 2 ++ my/coding/topcoder.py | 2 +- my/common.py | 17 ++++++++++++++++- my/jawbone/__init__.py | 2 +- my/media/youtube.py | 1 + my/reddit.py | 5 +---- my/rtm.py | 6 ++---- 7 files changed, 24 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index fc67fd5..3282944 100644 --- a/.gitignore +++ b/.gitignore @@ -208,3 +208,5 @@ pip-selfcheck.json # End of https://www.gitignore.io/api/python,emacs with_my + +cov diff --git a/my/coding/topcoder.py b/my/coding/topcoder.py index 14cf980..fa90af7 100644 --- a/my/coding/topcoder.py +++ b/my/coding/topcoder.py @@ -66,7 +66,7 @@ def iter_data() -> Iterator[Res[Competition]]: cont = res['content'].zoom() ignore(cont, 'handle', 'handleLower', 'userId', 'createdAt', 'updatedAt', 'createdBy', 'updatedBy') - cont['DEVELOP'].ignore() # TODO FIXME handle it?? + cont['DEVELOP'].ignore() # TODO handle it?? ds = cont['DATA_SCIENCE'].zoom() mar, srm = zoom(ds, 'MARATHON_MATCH', 'SRM') diff --git a/my/common.py b/my/common.py index fc72142..e80e736 100644 --- a/my/common.py +++ b/my/common.py @@ -1,7 +1,7 @@ from pathlib import Path import functools import types -from typing import Union, Callable, Dict, Iterable, TypeVar, Sequence, List, Optional, TYPE_CHECKING, Any +from typing import Union, Callable, Dict, Iterable, TypeVar, Sequence, List, Optional, Any, cast # some helper functions PathIsh = Union[Path, str] @@ -54,6 +54,21 @@ def group_by_key(l: Iterable[T], key: Callable[[T], K]) -> Dict[K, List[T]]: return res +def _identity(v: T) -> V: + return cast(V, v) + +def make_dict(l: Iterable[T], key: Callable[[T], K], value: Callable[[T], V]=_identity) -> Dict[K, V]: + res: Dict[K, V] = {} + for i in l: + k = key(i) + v = value(i) + pv = res.get(k, None) # type: ignore + if pv is not None: + raise RuntimeError(f"Duplicate key: {k}. Previous value: {pv}, new value: {v}") + res[k] = v + return res + + Cl = TypeVar('Cl') R = TypeVar('R') diff --git a/my/jawbone/__init__.py b/my/jawbone/__init__.py index d81fce9..4fd4080 100755 --- a/my/jawbone/__init__.py +++ b/my/jawbone/__init__.py @@ -187,7 +187,7 @@ def plot_one(sleep: SleepEntry, fig: Figure, axes: Axes, xlims=None, showtext=Tr axes.text(xlims[1] - timedelta(hours=1.5), 20, str(sleep),) # plt.text(sleep.asleep(), 0, hhmm(sleep.asleep())) -from kython import make_dict, group_by_key +from ..common import group_by_key def sleeps_by_date() -> Dict[date, SleepEntry]: logger = get_logger() diff --git a/my/media/youtube.py b/my/media/youtube.py index 9b17ce2..e8ee57f 100755 --- a/my/media/youtube.py +++ b/my/media/youtube.py @@ -2,6 +2,7 @@ from datetime import datetime from typing import NamedTuple, List +# TODO FIXME from kython.ktakeout import TakeoutHTMLParser from ..kython.kompress import kopen diff --git a/my/reddit.py b/my/reddit.py index e8d26c9..e2b78c2 100755 --- a/my/reddit.py +++ b/my/reddit.py @@ -7,7 +7,7 @@ from pathlib import Path from typing import List, Sequence, Mapping, Iterator from .kython.kompress import CPath -from .common import mcachew, get_files, LazyLogger +from .common import mcachew, get_files, LazyLogger, make_dict from mycfg import paths import mycfg.repos.rexport.dal as rexport @@ -110,8 +110,6 @@ def _get_state(bfile: Path) -> Dict[Sid, SaveWithDt]: bdt = _get_bdate(bfile) saves = [SaveWithDt(save, bdt) for save in rexport.DAL([bfile]).saved()] - # TODO FIXME remove kython? - from kython import make_dict return make_dict( sorted(saves, key=lambda p: p.save.created), key=lambda s: s.save.sid, @@ -195,7 +193,6 @@ def test_get_all_saves(): # TODO not sure if this is necesasry anymore? saves = list(saved()) # just check that they are unique.. - from kython import make_dict make_dict(saves, key=lambda s: s.sid) diff --git a/my/rtm.py b/my/rtm.py index a4ed2f6..fe41ab0 100755 --- a/my/rtm.py +++ b/my/rtm.py @@ -8,7 +8,7 @@ from pathlib import Path from typing import Dict, List, Optional, Iterator from datetime import datetime -from .common import LazyLogger, get_files, group_by_key, cproperty +from .common import LazyLogger, get_files, group_by_key, cproperty, make_dict from .kython.kompress import open as kopen from mycfg import rtm as config @@ -90,9 +90,7 @@ class DAL: def get_todos_by_uid(self) -> Dict[str, MyTodo]: todos = self.all_todos() - # TODO use make_dict? - res = {todo.uid: todo for todo in todos} - return res + return make_dict(todos, key=lambda t: t.uid) def get_todos_by_title(self) -> Dict[str, List[MyTodo]]: todos = self.all_todos()