klaatu-python 2.5.0__py3-none-any.whl → 2.7.0__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.
- klaatu_python/utils/__init__.py +7 -1
- klaatu_python/utils/iterables.py +25 -6
- klaatu_python/utils/time.py +13 -0
- {klaatu_python-2.5.0.dist-info → klaatu_python-2.7.0.dist-info}/METADATA +3 -1
- {klaatu_python-2.5.0.dist-info → klaatu_python-2.7.0.dist-info}/RECORD +7 -7
- {klaatu_python-2.5.0.dist-info → klaatu_python-2.7.0.dist-info}/WHEEL +0 -0
- {klaatu_python-2.5.0.dist-info → klaatu_python-2.7.0.dist-info}/licenses/LICENSE +0 -0
klaatu_python/utils/__init__.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from .dicts import filter_values_not_null, group_by, group_dicts, zip_dict_lists, zip_dicts
|
|
2
2
|
from .iterables import (
|
|
3
3
|
circulate,
|
|
4
|
+
count_distinct,
|
|
4
5
|
first_not_null,
|
|
5
6
|
first_not_null_or_null,
|
|
6
7
|
getitem0,
|
|
@@ -10,6 +11,7 @@ from .iterables import (
|
|
|
10
11
|
most_common,
|
|
11
12
|
nonulls,
|
|
12
13
|
partition,
|
|
14
|
+
seq_contains_seq,
|
|
13
15
|
)
|
|
14
16
|
from .numbers import (
|
|
15
17
|
can_coerce_to_int,
|
|
@@ -25,7 +27,7 @@ from .numbers import (
|
|
|
25
27
|
)
|
|
26
28
|
from .paths import deepest_common_path, sanitize_path
|
|
27
29
|
from .strings import fancy_join
|
|
28
|
-
from .time import daterange, round_up_timedelta
|
|
30
|
+
from .time import date_to_timestamp_ms, daterange, round_to_whole_hour, round_up_timedelta
|
|
29
31
|
from .urls import append_query_to_url, strip_url_query
|
|
30
32
|
from .various import get_binary_reply, is_truthy
|
|
31
33
|
|
|
@@ -35,6 +37,8 @@ __all__ = [
|
|
|
35
37
|
"can_coerce_to_int",
|
|
36
38
|
"circulate",
|
|
37
39
|
"coerce_between",
|
|
40
|
+
"count_distinct",
|
|
41
|
+
"date_to_timestamp_ms",
|
|
38
42
|
"daterange",
|
|
39
43
|
"deepest_common_path",
|
|
40
44
|
"fancy_join",
|
|
@@ -58,9 +62,11 @@ __all__ = [
|
|
|
58
62
|
"percent_rounded",
|
|
59
63
|
"roman",
|
|
60
64
|
"round_to_n",
|
|
65
|
+
"round_to_whole_hour",
|
|
61
66
|
"round_up_timedelta",
|
|
62
67
|
"rounded_percentage",
|
|
63
68
|
"sanitize_path",
|
|
69
|
+
"seq_contains_seq",
|
|
64
70
|
"strip_url_query",
|
|
65
71
|
"to_int",
|
|
66
72
|
"zip_dict_lists",
|
klaatu_python/utils/iterables.py
CHANGED
|
@@ -20,6 +20,21 @@ def circulate(lst: Iterable[_T], rounds: int) -> list[_T]:
|
|
|
20
20
|
return lst
|
|
21
21
|
|
|
22
22
|
|
|
23
|
+
def count_distinct(values: Iterable[_HashableT]) -> dict[_HashableT, int]:
|
|
24
|
+
"""
|
|
25
|
+
Returns a dict where the keys are distinct values from `values`, and the
|
|
26
|
+
values are the number of occurances of each value.
|
|
27
|
+
"""
|
|
28
|
+
occurrences: dict[_HashableT, int] = {}
|
|
29
|
+
|
|
30
|
+
for value in values:
|
|
31
|
+
if value not in occurrences:
|
|
32
|
+
occurrences[value] = 0
|
|
33
|
+
occurrences[value] += 1
|
|
34
|
+
|
|
35
|
+
return occurrences
|
|
36
|
+
|
|
37
|
+
|
|
23
38
|
def first_not_null(*values: _T | None) -> _T:
|
|
24
39
|
ret = first_not_null_or_null(*values)
|
|
25
40
|
if ret is None:
|
|
@@ -104,12 +119,7 @@ def most_common(values: Iterable[_HashableT]) -> tuple[_HashableT | None, int]:
|
|
|
104
119
|
|
|
105
120
|
Given an _empty_ iterable, it will return `(None, 0)`.
|
|
106
121
|
"""
|
|
107
|
-
occurrences
|
|
108
|
-
|
|
109
|
-
for value in values:
|
|
110
|
-
if value not in occurrences:
|
|
111
|
-
occurrences[value] = 0
|
|
112
|
-
occurrences[value] += 1
|
|
122
|
+
occurrences = count_distinct(values)
|
|
113
123
|
|
|
114
124
|
if not occurrences:
|
|
115
125
|
return None, 0
|
|
@@ -128,3 +138,12 @@ def partition(items: Sequence[_T], length: int) -> Iterator[Sequence[_T]]:
|
|
|
128
138
|
while offset == 0 or offset < len(items):
|
|
129
139
|
yield items[offset : offset + length]
|
|
130
140
|
offset += length
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def seq_contains_seq(container: Sequence, containee: Sequence) -> bool:
|
|
144
|
+
if len(containee) > len(container):
|
|
145
|
+
return False
|
|
146
|
+
for idx in range(len(container) - len(containee) + 1):
|
|
147
|
+
if container[idx : idx + len(containee)] == containee:
|
|
148
|
+
return True
|
|
149
|
+
return False
|
klaatu_python/utils/time.py
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
import datetime
|
|
1
2
|
from collections.abc import Iterator
|
|
2
3
|
from datetime import date, timedelta
|
|
3
4
|
from math import ceil
|
|
4
5
|
|
|
5
6
|
|
|
7
|
+
def date_to_timestamp_ms(date: datetime.date, tzinfo: datetime.tzinfo = datetime.timezone.utc) -> int:
|
|
8
|
+
epoch = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
|
|
9
|
+
dt = datetime.datetime(date.year, date.month, date.day, tzinfo=tzinfo)
|
|
10
|
+
|
|
11
|
+
return int((dt - epoch).total_seconds() * 1000)
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
def daterange(start_date: date, end_date: date) -> Iterator[date]:
|
|
7
15
|
"""
|
|
8
16
|
Iterates the dates between `start_date` (inclusive) and `end_date`
|
|
@@ -12,6 +20,11 @@ def daterange(start_date: date, end_date: date) -> Iterator[date]:
|
|
|
12
20
|
yield start_date + timedelta(days=n)
|
|
13
21
|
|
|
14
22
|
|
|
23
|
+
def round_to_whole_hour(d: datetime.datetime | None = None) -> datetime.datetime:
|
|
24
|
+
d = d or datetime.datetime.now()
|
|
25
|
+
return datetime.datetime(year=d.year, month=d.month, day=d.day, hour=d.hour, tzinfo=d.tzinfo)
|
|
26
|
+
|
|
27
|
+
|
|
15
28
|
def round_up_timedelta(td: timedelta) -> timedelta:
|
|
16
29
|
"""
|
|
17
30
|
If td > 30 min, round up to nearest hour. Otherwise, to nearest 10
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: klaatu-python
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.7.0
|
|
4
4
|
Author: Robert Huselius
|
|
5
5
|
Author-email: Robert Huselius <robert@huseli.us>
|
|
6
6
|
License-Expression: GPL-3.0-only
|
|
@@ -13,6 +13,8 @@ Description-Content-Type: text/markdown
|
|
|
13
13
|
|
|
14
14
|
# klaatu-python
|
|
15
15
|
|
|
16
|
+
<img width="500" alt="thisprojectwascodedbyahumanbeing-wordart" src="https://github.com/user-attachments/assets/83071cfd-5aec-4a4d-897d-8aa4a933fa31" />
|
|
17
|
+
|
|
16
18
|
A bunch of more or less useful Python utils without _any_ 3rd party dependencies.
|
|
17
19
|
|
|
18
20
|
## Development install
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
klaatu_python/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
2
|
klaatu_python/case.py,sha256=6FJuXXOWsJg786ftEafunjngjeOvpB3F3WP1a2wlQ9Y,1798
|
|
3
3
|
klaatu_python/lock.py,sha256=D--5HjqAgtflOtNjp0klF8C_gXUjOrrT9YpKx4vFWn8,1393
|
|
4
|
-
klaatu_python/utils/__init__.py,sha256=
|
|
4
|
+
klaatu_python/utils/__init__.py,sha256=o12-aSe4iewttGYx9IgNh2bu9yFP2uLPNxWbtosXApk,1678
|
|
5
5
|
klaatu_python/utils/dicts.py,sha256=xeYvL_4ox9ykrUv964JwpGhacmGbm1Oyz03sq78CyoI,2702
|
|
6
|
-
klaatu_python/utils/iterables.py,sha256=
|
|
6
|
+
klaatu_python/utils/iterables.py,sha256=WA0yFYj5LFHS5N-5MEADjBaQfjBo5ADTaoVSncJ-j60,4391
|
|
7
7
|
klaatu_python/utils/numbers.py,sha256=b0_Eiwumw8-zoKrjNx70KDO-jGGKNbG8gGHQ_ACitwo,4787
|
|
8
8
|
klaatu_python/utils/paths.py,sha256=8Yr92R5EE8tU_nE3_mjCmhZ3ftkl8FilMoCp9agT71c,1935
|
|
9
9
|
klaatu_python/utils/strings.py,sha256=GTZb6gUeqhS-7rjwkrwynZ9kLFj0f55mSmFwEI1d5wQ,469
|
|
10
|
-
klaatu_python/utils/time.py,sha256=
|
|
10
|
+
klaatu_python/utils/time.py,sha256=EwuCBzTaLBWmcPXd9Vbgbpfwc4eQN-XbmJD_A--LQDU,1369
|
|
11
11
|
klaatu_python/utils/urls.py,sha256=vV10t9nsX6bFCtHwtOqo0TEbsdFPJ73P2PALw_hyT8s,931
|
|
12
12
|
klaatu_python/utils/various.py,sha256=cBNtf5AC7T7tCcIvxb5Q1AYQsGZMUlPFdu27wwQ_up0,775
|
|
13
|
-
klaatu_python-2.
|
|
14
|
-
klaatu_python-2.
|
|
15
|
-
klaatu_python-2.
|
|
16
|
-
klaatu_python-2.
|
|
13
|
+
klaatu_python-2.7.0.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
14
|
+
klaatu_python-2.7.0.dist-info/WHEEL,sha256=s49dN1sxqzkgPplo4QuUaKomil-_cbDzeLK4-pZKD-A,81
|
|
15
|
+
klaatu_python-2.7.0.dist-info/METADATA,sha256=8nz7V5TTgsldYbGd3hVAsbnspMqGdjh7v7obXutn34I,917
|
|
16
|
+
klaatu_python-2.7.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|