dycw-utilities 0.146.2__py3-none-any.whl → 0.178.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of dycw-utilities might be problematic. Click here for more details.
- dycw_utilities-0.178.1.dist-info/METADATA +34 -0
- dycw_utilities-0.178.1.dist-info/RECORD +105 -0
- dycw_utilities-0.178.1.dist-info/WHEEL +4 -0
- {dycw_utilities-0.146.2.dist-info → dycw_utilities-0.178.1.dist-info}/entry_points.txt +1 -0
- utilities/__init__.py +1 -1
- utilities/altair.py +10 -7
- utilities/asyncio.py +129 -50
- utilities/atomicwrites.py +1 -1
- utilities/atools.py +64 -4
- utilities/cachetools.py +9 -6
- utilities/click.py +144 -49
- utilities/concurrent.py +1 -1
- utilities/contextlib.py +4 -2
- utilities/contextvars.py +20 -1
- utilities/cryptography.py +3 -3
- utilities/dataclasses.py +15 -28
- utilities/docker.py +387 -0
- utilities/enum.py +2 -2
- utilities/errors.py +17 -3
- utilities/fastapi.py +8 -3
- utilities/fpdf2.py +2 -2
- utilities/functions.py +20 -297
- utilities/git.py +19 -0
- utilities/grp.py +28 -0
- utilities/hypothesis.py +361 -79
- utilities/importlib.py +17 -1
- utilities/inflect.py +1 -1
- utilities/iterables.py +33 -58
- utilities/jinja2.py +148 -0
- utilities/json.py +1 -1
- utilities/libcst.py +7 -7
- utilities/logging.py +131 -93
- utilities/math.py +8 -4
- utilities/more_itertools.py +4 -6
- utilities/operator.py +1 -1
- utilities/orjson.py +86 -34
- utilities/os.py +49 -2
- utilities/packaging.py +115 -0
- utilities/parse.py +2 -2
- utilities/pathlib.py +66 -34
- utilities/permissions.py +298 -0
- utilities/platform.py +5 -4
- utilities/polars.py +934 -420
- utilities/polars_ols.py +1 -1
- utilities/postgres.py +317 -153
- utilities/pottery.py +10 -86
- utilities/pqdm.py +3 -3
- utilities/pwd.py +28 -0
- utilities/pydantic.py +4 -51
- utilities/pydantic_settings.py +240 -0
- utilities/pydantic_settings_sops.py +76 -0
- utilities/pyinstrument.py +5 -5
- utilities/pytest.py +100 -126
- utilities/pytest_plugins/pytest_randomly.py +1 -1
- utilities/pytest_plugins/pytest_regressions.py +7 -3
- utilities/pytest_regressions.py +27 -8
- utilities/random.py +11 -6
- utilities/re.py +1 -1
- utilities/redis.py +101 -64
- utilities/sentinel.py +10 -0
- utilities/shelve.py +4 -1
- utilities/shutil.py +25 -0
- utilities/slack_sdk.py +9 -4
- utilities/sqlalchemy.py +422 -352
- utilities/sqlalchemy_polars.py +28 -52
- utilities/string.py +1 -1
- utilities/subprocess.py +1977 -0
- utilities/tempfile.py +112 -4
- utilities/testbook.py +50 -0
- utilities/text.py +174 -42
- utilities/throttle.py +158 -0
- utilities/timer.py +2 -2
- utilities/traceback.py +59 -38
- utilities/types.py +68 -22
- utilities/typing.py +479 -19
- utilities/uuid.py +42 -5
- utilities/version.py +27 -26
- utilities/whenever.py +663 -178
- utilities/zoneinfo.py +80 -22
- dycw_utilities-0.146.2.dist-info/METADATA +0 -41
- dycw_utilities-0.146.2.dist-info/RECORD +0 -99
- dycw_utilities-0.146.2.dist-info/WHEEL +0 -4
- dycw_utilities-0.146.2.dist-info/licenses/LICENSE +0 -21
- utilities/aiolimiter.py +0 -25
- utilities/eventkit.py +0 -388
- utilities/period.py +0 -237
- utilities/python_dotenv.py +0 -101
- utilities/streamlit.py +0 -105
- utilities/typed_settings.py +0 -144
utilities/python_dotenv.py
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from dataclasses import dataclass
|
|
4
|
-
from os import environ
|
|
5
|
-
from pathlib import Path
|
|
6
|
-
from typing import TYPE_CHECKING, override
|
|
7
|
-
|
|
8
|
-
from dotenv import dotenv_values
|
|
9
|
-
|
|
10
|
-
from utilities.dataclasses import _ParseDataClassMissingValuesError, parse_dataclass
|
|
11
|
-
from utilities.iterables import MergeStrMappingsError, merge_str_mappings
|
|
12
|
-
from utilities.pathlib import get_root
|
|
13
|
-
from utilities.reprlib import get_repr
|
|
14
|
-
from utilities.types import Dataclass
|
|
15
|
-
|
|
16
|
-
if TYPE_CHECKING:
|
|
17
|
-
from collections.abc import Mapping
|
|
18
|
-
from collections.abc import Set as AbstractSet
|
|
19
|
-
|
|
20
|
-
from utilities.types import MaybeCallablePathLike, ParseObjectExtra, StrMapping
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def load_settings[T: Dataclass](
|
|
24
|
-
cls: type[T],
|
|
25
|
-
/,
|
|
26
|
-
*,
|
|
27
|
-
path: MaybeCallablePathLike | None = Path.cwd,
|
|
28
|
-
globalns: StrMapping | None = None,
|
|
29
|
-
localns: StrMapping | None = None,
|
|
30
|
-
warn_name_errors: bool = False,
|
|
31
|
-
head: bool = False,
|
|
32
|
-
case_sensitive: bool = False,
|
|
33
|
-
extra_parsers: ParseObjectExtra | None = None,
|
|
34
|
-
) -> T:
|
|
35
|
-
"""Load a set of settings from the `.env` file."""
|
|
36
|
-
path = get_root(path=path).joinpath(".env")
|
|
37
|
-
if not path.exists():
|
|
38
|
-
raise _LoadSettingsFileNotFoundError(path=path) from None
|
|
39
|
-
maybe_values_dotenv = dotenv_values(path)
|
|
40
|
-
try:
|
|
41
|
-
maybe_values: Mapping[str, str | None] = merge_str_mappings(
|
|
42
|
-
maybe_values_dotenv, environ, case_sensitive=case_sensitive
|
|
43
|
-
)
|
|
44
|
-
except MergeStrMappingsError as error:
|
|
45
|
-
raise _LoadSettingsDuplicateKeysError(
|
|
46
|
-
path=path,
|
|
47
|
-
values=error.mapping,
|
|
48
|
-
counts=error.counts,
|
|
49
|
-
case_sensitive=case_sensitive,
|
|
50
|
-
) from None
|
|
51
|
-
values = {k: v for k, v in maybe_values.items() if v is not None}
|
|
52
|
-
try:
|
|
53
|
-
return parse_dataclass(
|
|
54
|
-
values,
|
|
55
|
-
cls,
|
|
56
|
-
globalns=globalns,
|
|
57
|
-
localns=localns,
|
|
58
|
-
warn_name_errors=warn_name_errors,
|
|
59
|
-
head=head,
|
|
60
|
-
case_sensitive=case_sensitive,
|
|
61
|
-
allow_extra_keys=True,
|
|
62
|
-
extra_parsers=extra_parsers,
|
|
63
|
-
)
|
|
64
|
-
except _ParseDataClassMissingValuesError as error:
|
|
65
|
-
raise _LoadSettingsMissingKeysError(path=path, fields=error.fields) from None
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
@dataclass(kw_only=True, slots=True)
|
|
69
|
-
class LoadSettingsError(Exception):
|
|
70
|
-
path: Path
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
@dataclass(kw_only=True, slots=True)
|
|
74
|
-
class _LoadSettingsDuplicateKeysError(LoadSettingsError):
|
|
75
|
-
values: StrMapping
|
|
76
|
-
counts: Mapping[str, int]
|
|
77
|
-
case_sensitive: bool = False
|
|
78
|
-
|
|
79
|
-
@override
|
|
80
|
-
def __str__(self) -> str:
|
|
81
|
-
return f"Mapping {get_repr(dict(self.values))} keys must not contain duplicates (modulo case); got {get_repr(self.counts)}"
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
@dataclass(kw_only=True, slots=True)
|
|
85
|
-
class _LoadSettingsFileNotFoundError(LoadSettingsError):
|
|
86
|
-
@override
|
|
87
|
-
def __str__(self) -> str:
|
|
88
|
-
return f"Path {str(self.path)!r} must exist"
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
@dataclass(kw_only=True, slots=True)
|
|
92
|
-
class _LoadSettingsMissingKeysError(LoadSettingsError):
|
|
93
|
-
fields: AbstractSet[str]
|
|
94
|
-
|
|
95
|
-
@override
|
|
96
|
-
def __str__(self) -> str:
|
|
97
|
-
desc = ", ".join(map(repr, sorted(self.fields)))
|
|
98
|
-
return f"Unable to load {str(self.path)!r}; missing value(s) for {desc}"
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
__all__ = ["LoadSettingsError", "load_settings"]
|
utilities/streamlit.py
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from hmac import compare_digest
|
|
4
|
-
from typing import TYPE_CHECKING, Literal
|
|
5
|
-
|
|
6
|
-
from streamlit import (
|
|
7
|
-
button,
|
|
8
|
-
empty,
|
|
9
|
-
error,
|
|
10
|
-
form,
|
|
11
|
-
form_submit_button,
|
|
12
|
-
markdown,
|
|
13
|
-
secrets,
|
|
14
|
-
session_state,
|
|
15
|
-
stop,
|
|
16
|
-
text_input,
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
if TYPE_CHECKING:
|
|
20
|
-
from collections.abc import Callable
|
|
21
|
-
|
|
22
|
-
from streamlit.elements.lib.utils import Key
|
|
23
|
-
from streamlit.runtime.state import WidgetArgs, WidgetCallback, WidgetKwargs
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
def centered_button(
|
|
27
|
-
label: str,
|
|
28
|
-
/,
|
|
29
|
-
*,
|
|
30
|
-
key: Key | None = None,
|
|
31
|
-
help: str | None = None, # noqa: A002
|
|
32
|
-
on_click: WidgetCallback | None = None,
|
|
33
|
-
args: WidgetArgs | None = None,
|
|
34
|
-
kwargs: WidgetKwargs | None = None,
|
|
35
|
-
type: Literal["primary", "secondary"] = "secondary", # noqa: A002
|
|
36
|
-
disabled: bool = False,
|
|
37
|
-
use_container_width: bool = False,
|
|
38
|
-
) -> bool:
|
|
39
|
-
"""Create a centered button."""
|
|
40
|
-
style = r"<style>.row-widget.stButton {text-align: center;}</style>"
|
|
41
|
-
_ = markdown(style, unsafe_allow_html=True)
|
|
42
|
-
with empty():
|
|
43
|
-
return button(
|
|
44
|
-
label,
|
|
45
|
-
key=key,
|
|
46
|
-
help=help,
|
|
47
|
-
on_click=on_click,
|
|
48
|
-
args=args,
|
|
49
|
-
kwargs=kwargs,
|
|
50
|
-
type=type,
|
|
51
|
-
disabled=disabled,
|
|
52
|
-
use_container_width=use_container_width,
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
_USERNAME = "username"
|
|
57
|
-
_PASSWORD = "password" # noqa: S105
|
|
58
|
-
_PASSWORD_CORRECT = "password_correct" # noqa: S105
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
def ensure_logged_in(
|
|
62
|
-
*,
|
|
63
|
-
skip: bool = False,
|
|
64
|
-
before_form: Callable[..., None] | None = None,
|
|
65
|
-
after_form: Callable[..., None] | None = None,
|
|
66
|
-
) -> None:
|
|
67
|
-
"""Ensure the user is logged in."""
|
|
68
|
-
if not (skip or _check_password(before_form=before_form, after_form=after_form)):
|
|
69
|
-
stop()
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def _check_password(
|
|
73
|
-
*,
|
|
74
|
-
before_form: Callable[..., None] | None = None,
|
|
75
|
-
after_form: Callable[..., None] | None = None,
|
|
76
|
-
) -> bool:
|
|
77
|
-
"""Return `True` if the user had a correct password."""
|
|
78
|
-
if session_state.get("password_correct", False):
|
|
79
|
-
return True
|
|
80
|
-
if before_form is not None:
|
|
81
|
-
before_form()
|
|
82
|
-
with form("Credentials"):
|
|
83
|
-
_ = text_input("Username", key=_USERNAME)
|
|
84
|
-
_ = text_input("Password", type="password", key=_PASSWORD)
|
|
85
|
-
_ = form_submit_button("Log in", on_click=_password_entered)
|
|
86
|
-
if after_form is not None:
|
|
87
|
-
after_form()
|
|
88
|
-
if _PASSWORD_CORRECT in session_state:
|
|
89
|
-
_ = error("Username/password combination invalid or incorrect")
|
|
90
|
-
return False
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
def _password_entered() -> None:
|
|
94
|
-
"""Check whether a password entered by the user is correct."""
|
|
95
|
-
if (session_state[_USERNAME] in secrets["passwords"]) and compare_digest(
|
|
96
|
-
session_state[_PASSWORD], secrets.passwords[session_state[_USERNAME]]
|
|
97
|
-
):
|
|
98
|
-
session_state[_PASSWORD_CORRECT] = True
|
|
99
|
-
del session_state[_PASSWORD]
|
|
100
|
-
del session_state[_USERNAME]
|
|
101
|
-
else:
|
|
102
|
-
session_state[_PASSWORD_CORRECT] = False
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
__all__ = ["ensure_logged_in"]
|
utilities/typed_settings.py
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from dataclasses import dataclass
|
|
4
|
-
from functools import partial
|
|
5
|
-
from ipaddress import IPv4Address, IPv6Address
|
|
6
|
-
from os import environ
|
|
7
|
-
from pathlib import Path
|
|
8
|
-
from re import search
|
|
9
|
-
from typing import TYPE_CHECKING, Any, assert_never, override
|
|
10
|
-
|
|
11
|
-
import typed_settings
|
|
12
|
-
from typed_settings import EnvLoader, FileLoader, find
|
|
13
|
-
from typed_settings.converters import TSConverter
|
|
14
|
-
from typed_settings.loaders import TomlFormat
|
|
15
|
-
from whenever import (
|
|
16
|
-
Date,
|
|
17
|
-
DateDelta,
|
|
18
|
-
DateTimeDelta,
|
|
19
|
-
MonthDay,
|
|
20
|
-
PlainDateTime,
|
|
21
|
-
Time,
|
|
22
|
-
TimeDelta,
|
|
23
|
-
YearMonth,
|
|
24
|
-
ZonedDateTime,
|
|
25
|
-
)
|
|
26
|
-
|
|
27
|
-
from utilities.iterables import always_iterable
|
|
28
|
-
from utilities.pathlib import get_path
|
|
29
|
-
from utilities.string import substitute_environ
|
|
30
|
-
|
|
31
|
-
if TYPE_CHECKING:
|
|
32
|
-
from collections.abc import Callable
|
|
33
|
-
|
|
34
|
-
from typed_settings.loaders import Loader
|
|
35
|
-
from typed_settings.processors import Processor
|
|
36
|
-
|
|
37
|
-
from utilities.types import MaybeCallablePathLike, MaybeIterable, PathLike
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
##
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
class ExtendedTSConverter(TSConverter):
|
|
44
|
-
"""An extension of the TSConverter for custom types."""
|
|
45
|
-
|
|
46
|
-
@override
|
|
47
|
-
def __init__(
|
|
48
|
-
self,
|
|
49
|
-
*,
|
|
50
|
-
resolve_paths: bool = True,
|
|
51
|
-
strlist_sep: str | Callable[[str], list] | None = ":",
|
|
52
|
-
) -> None:
|
|
53
|
-
super().__init__(resolve_paths=resolve_paths, strlist_sep=strlist_sep)
|
|
54
|
-
cases: list[tuple[type[Any], Callable[..., Any]]] = [
|
|
55
|
-
(Date, Date.parse_common_iso),
|
|
56
|
-
(DateDelta, DateDelta.parse_common_iso),
|
|
57
|
-
(DateTimeDelta, DateTimeDelta.parse_common_iso),
|
|
58
|
-
(IPv4Address, IPv4Address),
|
|
59
|
-
(IPv6Address, IPv6Address),
|
|
60
|
-
(MonthDay, MonthDay.parse_common_iso),
|
|
61
|
-
(Path, partial(_parse_path, resolve=resolve_paths, pwd=Path.cwd())),
|
|
62
|
-
(PlainDateTime, PlainDateTime.parse_common_iso),
|
|
63
|
-
(Time, Time.parse_common_iso),
|
|
64
|
-
(TimeDelta, TimeDelta.parse_common_iso),
|
|
65
|
-
(YearMonth, YearMonth.parse_common_iso),
|
|
66
|
-
(ZonedDateTime, ZonedDateTime.parse_common_iso),
|
|
67
|
-
]
|
|
68
|
-
extras = {cls: _make_converter(cls, func) for cls, func in cases}
|
|
69
|
-
self.scalar_converters |= extras
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def _make_converter[T](
|
|
73
|
-
cls: type[T], parser: Callable[[str], T], /
|
|
74
|
-
) -> Callable[[Any, type[Any]], Any]:
|
|
75
|
-
def hook(value: T | str, _: type[T] = cls, /) -> Any:
|
|
76
|
-
if not isinstance(value, (cls, str)): # pragma: no cover
|
|
77
|
-
msg = f"Invalid type {type(value).__name__!r}; expected '{cls.__name__}' or 'str'"
|
|
78
|
-
raise TypeError(msg)
|
|
79
|
-
if isinstance(value, str):
|
|
80
|
-
return parser(value)
|
|
81
|
-
return value
|
|
82
|
-
|
|
83
|
-
return hook
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
def _parse_path(
|
|
87
|
-
path: str, /, *, resolve: bool = False, pwd: MaybeCallablePathLike | None = None
|
|
88
|
-
) -> Path:
|
|
89
|
-
path = substitute_environ(path, **environ)
|
|
90
|
-
match resolve:
|
|
91
|
-
case True:
|
|
92
|
-
return get_path(path=pwd).joinpath(path).resolve()
|
|
93
|
-
case False:
|
|
94
|
-
return Path(path)
|
|
95
|
-
case _ as never:
|
|
96
|
-
assert_never(never)
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
##
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
_BASE_DIR: Path = Path()
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
def load_settings[T](
|
|
106
|
-
cls: type[T],
|
|
107
|
-
app_name: str,
|
|
108
|
-
/,
|
|
109
|
-
*,
|
|
110
|
-
filenames: MaybeIterable[str] = "settings.toml",
|
|
111
|
-
start_dir: PathLike | None = None,
|
|
112
|
-
loaders: MaybeIterable[Loader] | None = None,
|
|
113
|
-
processors: MaybeIterable[Processor] = (),
|
|
114
|
-
base_dir: Path = _BASE_DIR,
|
|
115
|
-
) -> T:
|
|
116
|
-
if not search(r"^[A-Za-z]+(?:_[A-Za-z]+)*$", app_name):
|
|
117
|
-
raise LoadSettingsError(appname=app_name)
|
|
118
|
-
filenames_use = list(always_iterable(filenames))
|
|
119
|
-
start_dir_use = None if start_dir is None else Path(start_dir)
|
|
120
|
-
files = [find(filename, start_dir=start_dir_use) for filename in filenames_use]
|
|
121
|
-
file_loader = FileLoader(formats={"*.toml": TomlFormat(app_name)}, files=files)
|
|
122
|
-
env_loader = EnvLoader(f"{app_name.upper()}__", nested_delimiter="__")
|
|
123
|
-
loaders_use: list[Loader] = [file_loader, env_loader]
|
|
124
|
-
if loaders is not None:
|
|
125
|
-
loaders_use.extend(always_iterable(loaders))
|
|
126
|
-
return typed_settings.load_settings(
|
|
127
|
-
cls,
|
|
128
|
-
loaders_use,
|
|
129
|
-
processors=list(always_iterable(processors)),
|
|
130
|
-
converter=ExtendedTSConverter(),
|
|
131
|
-
base_dir=base_dir,
|
|
132
|
-
)
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
@dataclass(kw_only=True, slots=True)
|
|
136
|
-
class LoadSettingsError(Exception):
|
|
137
|
-
appname: str
|
|
138
|
-
|
|
139
|
-
@override
|
|
140
|
-
def __str__(self) -> str:
|
|
141
|
-
return f"Invalid app name; got {self.appname!r}"
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
__all__ = ["ExtendedTSConverter", "LoadSettingsError", "load_settings"]
|