-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
50 lines (34 loc) · 1.23 KB
/
__init__.py
File metadata and controls
50 lines (34 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import datetime
from typing import Callable, NamedTuple, TypeVar
import pytz
from tzlocal import get_localzone
class VersionInfo(NamedTuple):
version: str
commit_sha: str
commit_timestamp: int
# will be updated by CI
version_info = VersionInfo(
version="0.0.1",
commit_sha="abcdefgh",
commit_timestamp=1234567890,
)
def version_info_formatted() -> str:
vi = version_info
ts = datetime.datetime.fromtimestamp(vi.commit_timestamp, tz=pytz.utc).astimezone(
get_localzone()
)
return f"version:{vi.version}, commit sha:{vi.commit_sha}, commit timestamp:{ts:%c %Z}".replace(" ", " ")
def bytes_decode(encoding: str) -> Callable[[bytes], str]:
def _internal(inp: bytes) -> str:
return inp.decode(encoding)
return _internal
T_in = TypeVar("T_in")
T_out = TypeVar("T_out")
def wrap_param_in_exception(caption: str, func: Callable[[T_in], T_out]) -> Callable[[T_in], T_out]:
"""Decorator to wrap excpetion into ValueError with the dump of the input parameter"""
def _internal(input: T_in) -> T_out:
try:
return func(input)
except Exception as err:
raise ValueError(f"Invalid Input ({caption}): {input!r}") from err
return _internal