dycw-utilities 0.166.1__py3-none-any.whl → 0.166.3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dycw-utilities
3
- Version: 0.166.1
3
+ Version: 0.166.3
4
4
  Author-email: Derek Wan <d.wan@icloud.com>
5
5
  License-File: LICENSE
6
6
  Requires-Python: >=3.12
@@ -12,7 +12,7 @@ Provides-Extra: logging
12
12
  Requires-Dist: coloredlogs<15.1,>=15.0.1; extra == 'logging'
13
13
  Provides-Extra: test
14
14
  Requires-Dist: dycw-pytest-only<2.2,>=2.1.1; extra == 'test'
15
- Requires-Dist: hypothesis<6.139,>=6.138.2; extra == 'test'
15
+ Requires-Dist: hypothesis<6.139,>=6.138.3; extra == 'test'
16
16
  Requires-Dist: pytest-asyncio<1.2,>=1.1.0; extra == 'test'
17
17
  Requires-Dist: pytest-cov<6.3,>=6.2.1; extra == 'test'
18
18
  Requires-Dist: pytest-instafail<0.6,>=0.5.0; extra == 'test'
@@ -1,4 +1,4 @@
1
- utilities/__init__.py,sha256=pk8s8uyCf9TfamkXymCVi1C6C_xH9yc5UV3b1hbP1Nk,60
1
+ utilities/__init__.py,sha256=UJOTjSCJcrHgKEbkrpzbpKR9zl61qcqWMWYPiX3nvjI,60
2
2
  utilities/aeventkit.py,sha256=ddoleSwW9zdc2tjX5Ge0pMKtYwV_JMxhHYOxnWX2AGM,12609
3
3
  utilities/altair.py,sha256=92E2lCdyHY4Zb-vCw6rEJIsWdKipuu-Tu2ab1ufUfAk,9079
4
4
  utilities/asyncio.py,sha256=PUedzQ5deqlSECQ33sam9cRzI9TnygHz3FdOqWJWPTM,15288
@@ -75,21 +75,21 @@ utilities/threading.py,sha256=GvBOp4CyhHfN90wGXZuA2VKe9fGzMaEa7oCl4f3nnPU,1009
75
75
  utilities/timer.py,sha256=oXfTii6ymu57niP0BDGZjFD55LEHi2a19kqZKiTgaFQ,2588
76
76
  utilities/traceback.py,sha256=b1nSvlyrGmI1MyZLkkoLVET3DQBSGt9qqIlAAQbyjEw,9629
77
77
  utilities/typed_settings.py,sha256=SFWqS3lAzV7IfNRwqFcTk0YynTcQ7BmrcW2mr_KUnos,4466
78
- utilities/types.py,sha256=oeH-hEC3-67Eja4nLz-Nj9WvK6Z9-3T1zobO_XJpuVg,18735
78
+ utilities/types.py,sha256=IlRrCtPdLkGYVfpe-QIg2qNUgBr8OJNN7BhTKxnhh-M,18817
79
79
  utilities/typing.py,sha256=QYoCIc71e_u5W-kBeiNzrD-GXxpLtMOGc9PqgZjMXeE,25274
80
80
  utilities/tzdata.py,sha256=fgNVj66yUbCSI_-vrRVzSD3gtf-L_8IEJEPjP_Jel5Y,266
81
81
  utilities/tzlocal.py,sha256=KyCXEgCTjqGFx-389JdTuhMRUaT06U1RCMdWoED-qro,728
82
82
  utilities/uuid.py,sha256=nQZs6tFX4mqtc2Ku3KqjloYCqwpTKeTj8eKwQwh3FQI,1572
83
83
  utilities/version.py,sha256=ipBj5-WYY_nelp2uwFlApfWWCzTLzPwpovUi9x_OBMs,5085
84
84
  utilities/warnings.py,sha256=un1LvHv70PU-LLv8RxPVmugTzDJkkGXRMZTE2-fTQHw,1771
85
- utilities/whenever.py,sha256=8jQZkE3_8E4A10oQM-OYIJpWtvBBtYTf-WIv3cVxL9w,57555
85
+ utilities/whenever.py,sha256=0N5JcZU9cPyIrkTN3wqdcR86PsmIKVlLI1yU1aEbIgk,59198
86
86
  utilities/zipfile.py,sha256=24lQc9ATcJxHXBPc_tBDiJk48pWyRrlxO2fIsFxU0A8,699
87
87
  utilities/zoneinfo.py,sha256=tdIScrTB2-B-LH0ukb1HUXKooLknOfJNwHk10MuMYvA,3619
88
88
  utilities/pytest_plugins/__init__.py,sha256=U4S_2y3zgLZVfMenHRaJFBW8yqh2mUBuI291LGQVOJ8,35
89
89
  utilities/pytest_plugins/pytest_randomly.py,sha256=B1qYVlExGOxTywq2r1SMi5o7btHLk2PNdY_b1p98dkE,409
90
90
  utilities/pytest_plugins/pytest_regressions.py,sha256=9v8kAXDM2ycIXJBimoiF4EgrwbUvxTycFWJiGR_GHhM,1466
91
- dycw_utilities-0.166.1.dist-info/METADATA,sha256=tFhmfKrHbWlljJMUoGWMcLeXSJiVMMXbAAqlqwE6rTQ,1696
92
- dycw_utilities-0.166.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
93
- dycw_utilities-0.166.1.dist-info/entry_points.txt,sha256=BOD_SoDxwsfJYOLxhrSXhHP_T7iw-HXI9f2WVkzYxvQ,135
94
- dycw_utilities-0.166.1.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
95
- dycw_utilities-0.166.1.dist-info/RECORD,,
91
+ dycw_utilities-0.166.3.dist-info/METADATA,sha256=icTUtPfUwAxKgqCxRokuMZ-Oc5KA1cCeawM_4WM5KQY,1696
92
+ dycw_utilities-0.166.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
93
+ dycw_utilities-0.166.3.dist-info/entry_points.txt,sha256=BOD_SoDxwsfJYOLxhrSXhHP_T7iw-HXI9f2WVkzYxvQ,135
94
+ dycw_utilities-0.166.3.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
95
+ dycw_utilities-0.166.3.dist-info/RECORD,,
utilities/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
  from __future__ import annotations
2
2
 
3
- __version__ = "0.166.1"
3
+ __version__ = "0.166.3"
utilities/types.py CHANGED
@@ -255,6 +255,7 @@ type DateTimeRoundMode = Literal[
255
255
  ]
256
256
  type Delta = DateDelta | TimeDelta | DateTimeDelta
257
257
  type MaybeCallableDateLike = MaybeCallable[DateLike]
258
+ type MaybeCallableTimeLike = MaybeCallable[TimeLike]
258
259
  type MaybeCallableZonedDateTimeLike = MaybeCallable[ZonedDateTimeLike]
259
260
  type MonthDayLike = MaybeStr[MonthDay]
260
261
  type PlainDateTimeLike = MaybeStr[PlainDateTime]
@@ -308,6 +309,7 @@ __all__ = [
308
309
  "MaybeCallableDateLike",
309
310
  "MaybeCallablePathLike",
310
311
  "MaybeCallableStr",
312
+ "MaybeCallableTimeLike",
311
313
  "MaybeCallableUUIDLike",
312
314
  "MaybeCallableZonedDateTimeLike",
313
315
  "MaybeCollection",
utilities/whenever.py CHANGED
@@ -13,7 +13,6 @@ from typing import (
13
13
  Self,
14
14
  SupportsFloat,
15
15
  TypedDict,
16
- TypeVar,
17
16
  assert_never,
18
17
  cast,
19
18
  overload,
@@ -47,6 +46,7 @@ if TYPE_CHECKING:
47
46
  DateTimeRoundMode,
48
47
  Delta,
49
48
  MaybeCallableDateLike,
49
+ MaybeCallableTimeLike,
50
50
  MaybeCallableZonedDateTimeLike,
51
51
  TimeOrDateTimeDelta,
52
52
  TimeZoneLike,
@@ -146,9 +146,6 @@ def sub_year_month(x: YearMonth, /, *, years: int = 0, months: int = 0) -> YearM
146
146
  ##
147
147
 
148
148
 
149
- _TDate_co = TypeVar("_TDate_co", bound=Date | dt.date, covariant=True)
150
-
151
-
152
149
  @dataclass(repr=False, order=True, unsafe_hash=True, kw_only=False)
153
150
  class DatePeriod:
154
151
  """A period of dates."""
@@ -210,7 +207,7 @@ class DatePeriod:
210
207
  return f"{fc(start)}-{fc(end)}"
211
208
 
212
209
  @classmethod
213
- def from_dict(cls, mapping: PeriodDict[_TDate_co], /) -> Self:
210
+ def from_dict(cls, mapping: PeriodDict[Date] | PeriodDict[dt.date], /) -> Self:
214
211
  """Convert the dictionary to a period."""
215
212
  match mapping["start"]:
216
213
  case Date() as start:
@@ -238,6 +235,10 @@ class DatePeriod:
238
235
  """Convert the period to a dictionary."""
239
236
  return PeriodDict(start=self.start, end=self.end)
240
237
 
238
+ def to_py_dict(self) -> PeriodDict[dt.date]:
239
+ """Convert the period to a dictionary."""
240
+ return PeriodDict(start=self.start.py_date(), end=self.end.py_date())
241
+
241
242
 
242
243
  @dataclass(kw_only=True, slots=True)
243
244
  class DatePeriodError(Exception):
@@ -368,7 +369,7 @@ NOW_UTC = get_now(UTC)
368
369
 
369
370
 
370
371
  def get_now_local() -> ZonedDateTime:
371
- """Get the current local date-time."""
372
+ """Get the current zoned date-time in the local time-zone."""
372
373
  return get_now(LOCAL_TIME_ZONE)
373
374
 
374
375
 
@@ -376,7 +377,7 @@ NOW_LOCAL = get_now_local()
376
377
 
377
378
 
378
379
  def get_now_plain(time_zone: TimeZoneLike = UTC, /) -> PlainDateTime:
379
- """Get the current date-time as a plain date-time."""
380
+ """Get the current plain date-time."""
380
381
  return get_now(time_zone).to_plain()
381
382
 
382
383
 
@@ -384,7 +385,7 @@ NOW_PLAIN = get_now_plain()
384
385
 
385
386
 
386
387
  def get_now_local_plain() -> PlainDateTime:
387
- """Get the current local date-time as a plain date-time."""
388
+ """Get the current plain date-time in the local time-zone."""
388
389
  return get_now_local().to_plain()
389
390
 
390
391
 
@@ -394,6 +395,25 @@ NOW_LOCAL_PLAIN = get_now_local_plain()
394
395
  ##
395
396
 
396
397
 
398
+ def get_time(time_zone: TimeZoneLike = UTC, /) -> Time:
399
+ """Get the current time."""
400
+ return get_now(time_zone).time()
401
+
402
+
403
+ TIME_UTC = get_time(UTC)
404
+
405
+
406
+ def get_time_local() -> Time:
407
+ """Get the current time in the local time-zone."""
408
+ return get_time(LOCAL_TIME_ZONE)
409
+
410
+
411
+ TIME_LOCAL = get_time_local()
412
+
413
+
414
+ ##
415
+
416
+
397
417
  def get_today(time_zone: TimeZoneLike = UTC, /) -> Date:
398
418
  """Get the current, timezone-aware local date."""
399
419
  return get_now(time_zone).date()
@@ -788,9 +808,6 @@ class _RoundDateOrDateTimeDateTimeIntraDayWithWeekdayError(RoundDateOrDateTimeEr
788
808
  ##
789
809
 
790
810
 
791
- _TTime_co = TypeVar("_TTime_co", bound=Time | dt.time, covariant=True)
792
-
793
-
794
811
  @dataclass(repr=False, order=True, unsafe_hash=True, kw_only=False)
795
812
  class TimePeriod:
796
813
  """A period of times."""
@@ -817,7 +834,7 @@ class TimePeriod:
817
834
  return DatePeriod(start, end).at((self.start, self.end), time_zone=time_zone)
818
835
 
819
836
  @classmethod
820
- def from_dict(cls, mapping: PeriodDict[_TTime_co], /) -> Self:
837
+ def from_dict(cls, mapping: PeriodDict[Time] | PeriodDict[dt.time], /) -> Self:
821
838
  """Convert the dictionary to a period."""
822
839
  match mapping["start"]:
823
840
  case Time() as start:
@@ -845,6 +862,10 @@ class TimePeriod:
845
862
  """Convert the period to a dictionary."""
846
863
  return PeriodDict(start=self.start, end=self.end)
847
864
 
865
+ def to_py_dict(self) -> PeriodDict[dt.time]:
866
+ """Convert the period to a dictionary."""
867
+ return PeriodDict(start=self.start.py_time(), end=self.end.py_time())
868
+
848
869
 
849
870
  ##
850
871
 
@@ -1377,6 +1398,95 @@ class ToPyTimeDeltaError(Exception):
1377
1398
  ##
1378
1399
 
1379
1400
 
1401
+ def to_seconds(delta: Delta, /) -> int:
1402
+ """Compute the number of seconds in a delta."""
1403
+ match delta:
1404
+ case DateDelta():
1405
+ try:
1406
+ days = to_days(delta)
1407
+ except _ToDaysMonthsError as error:
1408
+ raise _ToSecondsMonthsError(delta=delta, months=error.months) from None
1409
+ return 24 * 60 * 60 * days
1410
+ case TimeDelta():
1411
+ nanos = to_nanoseconds(delta)
1412
+ seconds, remainder = divmod(nanos, int(1e9))
1413
+ if remainder != 0:
1414
+ raise _ToSecondsNanosecondsError(delta=delta, nanoseconds=remainder)
1415
+ return seconds
1416
+ case DateTimeDelta():
1417
+ try:
1418
+ return to_seconds(delta.date_part()) + to_seconds(delta.time_part())
1419
+ except _ToSecondsMonthsError as error:
1420
+ raise _ToSecondsMonthsError(delta=delta, months=error.months) from None
1421
+ except _ToSecondsNanosecondsError as error:
1422
+ raise _ToSecondsNanosecondsError(
1423
+ delta=delta, nanoseconds=error.nanoseconds
1424
+ ) from None
1425
+ case never:
1426
+ assert_never(never)
1427
+
1428
+
1429
+ @dataclass(kw_only=True, slots=True)
1430
+ class ToSecondsError(Exception): ...
1431
+
1432
+
1433
+ @dataclass(kw_only=True, slots=True)
1434
+ class _ToSecondsMonthsError(ToSecondsError):
1435
+ delta: DateOrDateTimeDelta
1436
+ months: int
1437
+
1438
+ @override
1439
+ def __str__(self) -> str:
1440
+ return f"Delta must not contain months; got {self.months}"
1441
+
1442
+
1443
+ @dataclass(kw_only=True, slots=True)
1444
+ class _ToSecondsNanosecondsError(ToSecondsError):
1445
+ delta: TimeOrDateTimeDelta
1446
+ nanoseconds: int
1447
+
1448
+ @override
1449
+ def __str__(self) -> str:
1450
+ return f"Delta must not contain extra nanoseconds; got {self.nanoseconds}"
1451
+
1452
+
1453
+ ##
1454
+
1455
+
1456
+ @overload
1457
+ def to_time(time: Sentinel, /, *, time_zone: TimeZoneLike = UTC) -> Sentinel: ...
1458
+ @overload
1459
+ def to_time(
1460
+ time: MaybeCallableTimeLike | None | dt.time = get_time,
1461
+ /,
1462
+ *,
1463
+ time_zone: TimeZoneLike = UTC,
1464
+ ) -> Time: ...
1465
+ def to_time(
1466
+ time: MaybeCallableTimeLike | dt.time | None | Sentinel = get_time,
1467
+ /,
1468
+ *,
1469
+ time_zone: TimeZoneLike = UTC,
1470
+ ) -> Time | Sentinel:
1471
+ """Convert to a time."""
1472
+ match time:
1473
+ case Time() | Sentinel():
1474
+ return time
1475
+ case None:
1476
+ return get_time(time_zone)
1477
+ case str():
1478
+ return Time.parse_common_iso(time)
1479
+ case dt.time():
1480
+ return Time.from_py_time(time)
1481
+ case Callable() as func:
1482
+ return to_time(func(), time_zone=time_zone)
1483
+ case never:
1484
+ assert_never(never)
1485
+
1486
+
1487
+ ##
1488
+
1489
+
1380
1490
  def to_time_delta(nanos: int, /) -> TimeDelta:
1381
1491
  """Construct a time delta."""
1382
1492
  components = _to_time_delta_components(nanos)
@@ -1455,61 +1565,6 @@ def _to_time_delta_components(nanos: int, /) -> _TimeDeltaComponents:
1455
1565
  ##
1456
1566
 
1457
1567
 
1458
- def to_seconds(delta: Delta, /) -> int:
1459
- """Compute the number of seconds in a delta."""
1460
- match delta:
1461
- case DateDelta():
1462
- try:
1463
- days = to_days(delta)
1464
- except _ToDaysMonthsError as error:
1465
- raise _ToSecondsMonthsError(delta=delta, months=error.months) from None
1466
- return 24 * 60 * 60 * days
1467
- case TimeDelta():
1468
- nanos = to_nanoseconds(delta)
1469
- seconds, remainder = divmod(nanos, int(1e9))
1470
- if remainder != 0:
1471
- raise _ToSecondsNanosecondsError(delta=delta, nanoseconds=remainder)
1472
- return seconds
1473
- case DateTimeDelta():
1474
- try:
1475
- return to_seconds(delta.date_part()) + to_seconds(delta.time_part())
1476
- except _ToSecondsMonthsError as error:
1477
- raise _ToSecondsMonthsError(delta=delta, months=error.months) from None
1478
- except _ToSecondsNanosecondsError as error:
1479
- raise _ToSecondsNanosecondsError(
1480
- delta=delta, nanoseconds=error.nanoseconds
1481
- ) from None
1482
- case never:
1483
- assert_never(never)
1484
-
1485
-
1486
- @dataclass(kw_only=True, slots=True)
1487
- class ToSecondsError(Exception): ...
1488
-
1489
-
1490
- @dataclass(kw_only=True, slots=True)
1491
- class _ToSecondsMonthsError(ToSecondsError):
1492
- delta: DateOrDateTimeDelta
1493
- months: int
1494
-
1495
- @override
1496
- def __str__(self) -> str:
1497
- return f"Delta must not contain months; got {self.months}"
1498
-
1499
-
1500
- @dataclass(kw_only=True, slots=True)
1501
- class _ToSecondsNanosecondsError(ToSecondsError):
1502
- delta: TimeOrDateTimeDelta
1503
- nanoseconds: int
1504
-
1505
- @override
1506
- def __str__(self) -> str:
1507
- return f"Delta must not contain extra nanoseconds; got {self.nanoseconds}"
1508
-
1509
-
1510
- ##
1511
-
1512
-
1513
1568
  def to_weeks(delta: Delta, /) -> int:
1514
1569
  """Compute the number of weeks in a delta."""
1515
1570
  try:
@@ -1726,11 +1781,6 @@ class WheneverLogRecord(LogRecord):
1726
1781
  ##
1727
1782
 
1728
1783
 
1729
- _TDateTime_co = TypeVar(
1730
- "_TDateTime_co", bound=ZonedDateTime | dt.datetime, covariant=True
1731
- )
1732
-
1733
-
1734
1784
  @dataclass(repr=False, order=True, unsafe_hash=True, kw_only=False)
1735
1785
  class ZonedDateTimePeriod:
1736
1786
  """A period of time."""
@@ -1833,7 +1883,9 @@ class ZonedDateTimePeriod:
1833
1883
  return f"{fc(start.to_plain())}-{fc(end, fmt='%Y%m%dT%H')}"
1834
1884
 
1835
1885
  @classmethod
1836
- def from_dict(cls, mapping: PeriodDict[_TDateTime_co], /) -> Self:
1886
+ def from_dict(
1887
+ cls, mapping: PeriodDict[ZonedDateTime] | PeriodDict[dt.datetime], /
1888
+ ) -> Self:
1837
1889
  """Convert the dictionary to a period."""
1838
1890
  match mapping["start"]:
1839
1891
  case ZonedDateTime() as start:
@@ -1869,6 +1921,10 @@ class ZonedDateTimePeriod:
1869
1921
  """Convert the period to a dictionary."""
1870
1922
  return PeriodDict(start=self.start, end=self.end)
1871
1923
 
1924
+ def to_py_dict(self) -> PeriodDict[dt.datetime]:
1925
+ """Convert the period to a dictionary."""
1926
+ return PeriodDict(start=self.start.py_datetime(), end=self.end.py_datetime())
1927
+
1872
1928
  def to_tz(self, time_zone: TimeZoneLike, /) -> Self:
1873
1929
  """Convert the time zone."""
1874
1930
  tz = to_time_zone_name(time_zone)
@@ -1933,6 +1989,8 @@ __all__ = [
1933
1989
  "SECOND",
1934
1990
  "TIME_DELTA_MAX",
1935
1991
  "TIME_DELTA_MIN",
1992
+ "TIME_LOCAL",
1993
+ "TIME_UTC",
1936
1994
  "TODAY_LOCAL",
1937
1995
  "TODAY_UTC",
1938
1996
  "WEEK",
@@ -1971,6 +2029,8 @@ __all__ = [
1971
2029
  "get_now_local",
1972
2030
  "get_now_local_plain",
1973
2031
  "get_now_plain",
2032
+ "get_time",
2033
+ "get_time_local",
1974
2034
  "get_today",
1975
2035
  "get_today_local",
1976
2036
  "mean_datetime",
@@ -1989,6 +2049,7 @@ __all__ = [
1989
2049
  "to_py_date_or_date_time",
1990
2050
  "to_py_time_delta",
1991
2051
  "to_seconds",
2052
+ "to_time",
1992
2053
  "to_weeks",
1993
2054
  "to_years",
1994
2055
  "to_zoned_date_time",