Alter coalesce() to raise exception if all None

pull/357/head
nyanpasu64 2018-11-16 23:52:53 -08:00
rodzic 1c050631eb
commit 9a47ca8990
1 zmienionych plików z 6 dodań i 5 usunięć

Wyświetl plik

@ -2,7 +2,7 @@ import os
from contextlib import contextmanager from contextlib import contextmanager
from itertools import chain from itertools import chain
from pathlib import Path from pathlib import Path
from typing import Callable, Tuple, TypeVar, Iterator, Union from typing import Callable, Tuple, TypeVar, Iterator, Union, Optional
import numpy as np import numpy as np
@ -11,21 +11,22 @@ def ceildiv(n, d):
return -(-n // d) return -(-n // d)
def coalesce(*args): T = TypeVar('T')
def coalesce(*args: Optional[T]) -> T:
if len(args) == 0: if len(args) == 0:
raise TypeError('coalesce expected 1 argument, got 0') raise TypeError('coalesce expected 1 argument, got 0')
for arg in args: for arg in args:
if arg is not None: if arg is not None:
return arg return arg
return args[-1] raise TypeError('coalesce() called with all None')
def obj_name(obj) -> str: def obj_name(obj) -> str:
return type(obj).__name__ return type(obj).__name__
T = TypeVar('T')
# Adapted from https://github.com/numpy/numpy/issues/2269#issuecomment-14436725 # Adapted from https://github.com/numpy/numpy/issues/2269#issuecomment-14436725
def find(a: 'np.ndarray[T]', predicate: 'Callable[[np.ndarray[T]], np.ndarray[bool]]', def find(a: 'np.ndarray[T]', predicate: 'Callable[[np.ndarray[T]], np.ndarray[bool]]',
chunk_size=1024) -> Iterator[Tuple[Tuple[int], T]]: chunk_size=1024) -> Iterator[Tuple[Tuple[int], T]]: