handle more fixmes, add make_dict

This commit is contained in:
Dima Gerasimov 2020-03-24 20:12:44 +00:00
parent 3365f979ed
commit c3812fc2e7
7 changed files with 24 additions and 11 deletions

View file

@ -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')