klaatu-python 2.6.0__tar.gz → 2.7.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: klaatu-python
3
- Version: 2.6.0
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
@@ -11,7 +11,7 @@ license-files = ["LICENSE"]
11
11
  name = "klaatu-python"
12
12
  readme = "README.md"
13
13
  requires-python = ">=3.10"
14
- version = "2.6.0"
14
+ version = "2.7.0"
15
15
 
16
16
  [project.urls]
17
17
  Homepage = "https://github.com/Eboreg/klaatu-python"
@@ -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",
@@ -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: dict[_HashableT, int] = {}
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
@@ -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
File without changes
File without changes