Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/utils.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import re | import re | ||||
from datetime import datetime, timezone | from datetime import datetime, timezone | ||||
from typing import Dict, Optional, Tuple | from typing import Callable, Dict, Optional, Tuple, TypeVar | ||||
from swh.model.hashutil import hash_to_bytes, hash_to_hex, DEFAULT_ALGORITHMS | from swh.model.hashutil import hash_to_bytes, hash_to_hex, DEFAULT_ALGORITHMS | ||||
def now() -> datetime: | def now() -> datetime: | ||||
return datetime.now(tz=timezone.utc) | return datetime.now(tz=timezone.utc) | ||||
T1 = TypeVar("T1") | |||||
T2 = TypeVar("T2") | |||||
def map_optional(f: Callable[[T1], T2], x: Optional[T1]) -> Optional[T2]: | |||||
if x is None: | |||||
return None | |||||
else: | |||||
ardumont: Too bad it's not core python.
I called something similar swh.web `fmap` (wayback ;)
[1] https… | |||||
return f(x) | |||||
def _is_power_of_two(n: int) -> bool: | def _is_power_of_two(n: int) -> bool: | ||||
return n > 0 and n & (n - 1) == 0 | return n > 0 and n & (n - 1) == 0 | ||||
def get_partition_bounds_bytes( | def get_partition_bounds_bytes( | ||||
i: int, n: int, nb_bytes: int | i: int, n: int, nb_bytes: int | ||||
) -> Tuple[bytes, Optional[bytes]]: | ) -> Tuple[bytes, Optional[bytes]]: | ||||
r"""Splits the range [0; 2^(nb_bytes*8)) into n same-length intervals, | r"""Splits the range [0; 2^(nb_bytes*8)) into n same-length intervals, | ||||
▲ Show 20 Lines • Show All 63 Lines • Show Last 20 Lines |
Too bad it's not core python.
I called something similar swh.web fmap (wayback ;)
[1] https://forge.softwareheritage.org/source/swh-web/browse/master/swh/web/common/converters.py$29