Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /spraakbanken_karp-backend /src /karp /cliapp /utility.py
| # pyright: strict | |
| import functools | |
| import itertools | |
| import logging | |
| import time | |
| from typing import Callable, ParamSpec, Sequence, TypeVar | |
| import typer | |
| P = ParamSpec("P") | |
| R = TypeVar("R") | |
| def cli_error_handler(func: Callable[P, R]) -> Callable[P, R]: | |
| def func_wrapper(*args: P.args, **kwargs: P.kwargs) -> R: | |
| from karp.lex.domain.errors import LexDomainError | |
| from karp.main.errors import KarpError | |
| logger = logging.getLogger("karp") | |
| try: | |
| return func(*args, **kwargs) | |
| except KarpError as e: | |
| logger.error(e.message) | |
| raise typer.Exit(e.code) from None | |
| except LexDomainError as e: | |
| logger.error(str(e)) | |
| raise typer.Exit(1) from None | |
| return func_wrapper | |
| def cli_timer(func: Callable[P, R]) -> Callable[P, R]: | |
| def func_wrapper(*args: P.args, **kwargs: P.kwargs) -> R: | |
| before_t = time.time() | |
| result = func(*args, **kwargs) | |
| typer.echo("Command took: %0.1fs" % (time.time() - before_t)) | |
| return result | |
| return func_wrapper | |
| def tabulate(rows: Sequence[Sequence[object]], headers: Sequence[str] = ()): | |
| """ | |
| Simple tabulation utility. Will break in undefined ways if length of each | |
| row and headers are not the same. | |
| """ | |
| # rows are used twice, do this to make generators work as input | |
| rows = list(rows) | |
| if rows: | |
| no_columns = len(rows[0]) | |
| else: | |
| no_columns = len(headers) | |
| cols: list[list[object]] = [[] for _ in range(0, no_columns)] | |
| widths: list[int] = [] | |
| start_end_sep: list[str] = [] | |
| for tmp in itertools.chain((headers,), rows): | |
| for idx, val in enumerate(tmp): | |
| cols[idx].append(val) | |
| for col in cols: | |
| col_width = max((len(str(x)) for x in col)) | |
| widths.append(col_width) | |
| start_end_sep.append("-" * col_width) | |
| start_end_sep_str = " ".join(start_end_sep) | |
| row: list[str] = [] | |
| for width, val in zip(widths, headers, strict=False): | |
| row.append(f"{val:<{width}}") | |
| res: list[str] = [] | |
| res.append(" ".join(row)) | |
| res.append(start_end_sep_str) | |
| for columns in rows: | |
| row = [] | |
| for width, val in zip(widths, columns, strict=False): | |
| row.append(f"{val:<{width}}") | |
| res.append(" ".join(row)) | |
| res.append(start_end_sep_str) | |
| return "\n".join(res) | |
Xet Storage Details
- Size:
- 2.43 kB
- Xet hash:
- 1ba66879dd05a1c58b474a3861e6dfb33284c98995b221a3994088631a9e53e8
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.