python-utils 4.1.0__tar.gz → 4.1.2__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.
Files changed (43) hide show
  1. {python_utils-4.1.0 → python_utils-4.1.2}/PKG-INFO +1 -1
  2. python_utils-4.1.2/_python_utils_tests/test_formatters.py +377 -0
  3. {python_utils-4.1.0 → python_utils-4.1.2}/pyproject.toml +1 -1
  4. {python_utils-4.1.0 → python_utils-4.1.2}/pyproject.toml.orig +1 -1
  5. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/formatters.py +19 -7
  6. python_utils-4.1.0/_python_utils_tests/test_formatters.py +0 -80
  7. {python_utils-4.1.0 → python_utils-4.1.2}/LICENSE +0 -0
  8. {python_utils-4.1.0 → python_utils-4.1.2}/README.md +0 -0
  9. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/__init__.py +0 -0
  10. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/clock.py +0 -0
  11. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/requirements.txt +0 -0
  12. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_aio.py +0 -0
  13. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_aliases.py +0 -0
  14. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_containers.py +0 -0
  15. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_converters.py +0 -0
  16. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_decorators.py +0 -0
  17. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_generators.py +0 -0
  18. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_import.py +0 -0
  19. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_import_footprint.py +0 -0
  20. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_lazy_imports.py +0 -0
  21. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_logged.py +0 -0
  22. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_logger.py +0 -0
  23. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_python_utils.py +0 -0
  24. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_time.py +0 -0
  25. {python_utils-4.1.0 → python_utils-4.1.2}/_python_utils_tests/test_types.py +0 -0
  26. {python_utils-4.1.0 → python_utils-4.1.2}/conftest.py +0 -0
  27. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/__about__.py +0 -0
  28. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/__init__.py +0 -0
  29. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/_aliases.py +0 -0
  30. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/aio.py +0 -0
  31. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/containers.py +0 -0
  32. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/converters.py +0 -0
  33. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/decorators.py +0 -0
  34. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/exceptions.py +0 -0
  35. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/generators.py +0 -0
  36. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/import_.py +0 -0
  37. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/logger.py +0 -0
  38. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/loguru.py +0 -0
  39. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/py.typed +0 -0
  40. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/terminal.py +0 -0
  41. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/time.py +0 -0
  42. {python_utils-4.1.0 → python_utils-4.1.2}/python_utils/types.py +0 -0
  43. {python_utils-4.1.0 → python_utils-4.1.2}/tox.ini +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-utils
3
- Version: 4.1.0
3
+ Version: 4.1.2
4
4
  Summary: Python Utils is a module with some convenient utilities not included with the standard Python install
5
5
  Keywords: utils,utilities,helpers,typing,typed,async,lazy-imports
6
6
  Author: Rick van Hattem
@@ -0,0 +1,377 @@
1
+ """Tests for the formatting helpers in ``python_utils.formatters``."""
2
+
3
+ import datetime
4
+ import types
5
+ import typing
6
+ from zoneinfo import ZoneInfo, available_timezones
7
+
8
+ import pytest
9
+
10
+ from python_utils import formatters
11
+
12
+
13
+ @pytest.mark.parametrize(
14
+ ('days', 'expected'),
15
+ [
16
+ (30, '1 month ago'),
17
+ (35, '1 month and 5 days ago'),
18
+ (60, '2 months ago'),
19
+ (365, '1 year ago'),
20
+ (730, '2 years ago'),
21
+ ],
22
+ )
23
+ def test_timesince_units_do_not_overlap(days: int, expected: str) -> None:
24
+ """Take the weeks and days from what the larger units left over."""
25
+ delta: datetime.timedelta = datetime.timedelta(days=days)
26
+ assert formatters.timesince(delta) == expected
27
+
28
+
29
+ @pytest.mark.parametrize(
30
+ ('delta', 'expected'),
31
+ [
32
+ (datetime.timedelta(seconds=-1), '1 second ago'),
33
+ (datetime.timedelta(seconds=-61), '1 minute and 1 second ago'),
34
+ (datetime.timedelta(days=-400), '1 year and 1 month ago'),
35
+ ],
36
+ )
37
+ def test_timesince_negative_timedelta(
38
+ delta: datetime.timedelta, expected: str
39
+ ) -> None:
40
+ """Describe a negative timedelta by its size, like a datetime."""
41
+ assert formatters.timesince(delta) == expected
42
+
43
+
44
+ @pytest.mark.parametrize(
45
+ 'timezone',
46
+ [
47
+ datetime.timezone.utc,
48
+ datetime.timezone(datetime.timedelta(hours=5, minutes=30)),
49
+ ],
50
+ )
51
+ def test_timesince_aware_datetime(timezone: datetime.timezone) -> None:
52
+ """Compare a timezone-aware datetime with an aware current time."""
53
+ # Only the two largest units are shown, so the time this test takes
54
+ # cannot change the result.
55
+ age: datetime.timedelta = datetime.timedelta(hours=1, minutes=30)
56
+ moment: datetime.datetime = datetime.datetime.now(timezone) - age
57
+ assert formatters.timesince(moment) == '1 hour and 30 minutes ago'
58
+
59
+
60
+ class _UnknownOffset(datetime.tzinfo):
61
+ """A timezone that does not know its UTC offset, as ``tzinfo`` allows."""
62
+
63
+ def utcoffset(self, dt: datetime.datetime | None) -> None:
64
+ """Report the offset as unknown."""
65
+ return
66
+
67
+ def dst(self, dt: datetime.datetime | None) -> None:
68
+ """Report the daylight saving offset as unknown."""
69
+ return
70
+
71
+ def tzname(self, dt: datetime.datetime | None) -> str:
72
+ """Name the timezone."""
73
+ return 'unknown'
74
+
75
+ def fromutc(self, dt: datetime.datetime) -> datetime.datetime:
76
+ """Use the UTC wall clock as the local one."""
77
+ return dt.replace(tzinfo=self)
78
+
79
+
80
+ class _OwnOffsetDateTime(datetime.datetime):
81
+ """A datetime subclass that reports an offset its tzinfo does not know."""
82
+
83
+ def utcoffset(self) -> datetime.timedelta:
84
+ """Report a fixed offset of three hours."""
85
+ return datetime.timedelta(hours=3)
86
+
87
+
88
+ def test_timesince_datetime_subclass_with_own_offset() -> None:
89
+ """Subtract a subclass with its own offset the way a datetime does."""
90
+ age: datetime.timedelta = datetime.timedelta(hours=1, minutes=30)
91
+ moment: datetime.datetime = _OwnOffsetDateTime.now(_UnknownOffset()) - age
92
+ assert formatters.timesince(moment) == '1 hour and 30 minutes ago'
93
+
94
+
95
+ class _ClockChange(datetime.tzinfo):
96
+ """A zone that sets its clock back an hour at 2026-10-25 01:00 UTC.
97
+
98
+ One instance serves both sides of the change, as a ``dateutil`` zone
99
+ does, so two datetimes in it share their tzinfo.
100
+ """
101
+
102
+ _CHANGE_UTC: datetime.datetime = datetime.datetime(2026, 10, 25, 1)
103
+ _CHANGE_WALL: datetime.datetime = datetime.datetime(2026, 10, 25, 2)
104
+
105
+ def utcoffset(self, dt: datetime.datetime | None) -> datetime.timedelta:
106
+ """Report two hours before the change and one hour after it."""
107
+ if dt is None or dt.replace(tzinfo=None) < self._CHANGE_WALL:
108
+ return datetime.timedelta(hours=2)
109
+ return datetime.timedelta(hours=1)
110
+
111
+ def dst(self, dt: datetime.datetime | None) -> datetime.timedelta:
112
+ """Report the daylight saving part of the offset."""
113
+ return self.utcoffset(dt) - datetime.timedelta(hours=1)
114
+
115
+ def tzname(self, dt: datetime.datetime | None) -> str:
116
+ """Name the timezone."""
117
+ return 'clock change'
118
+
119
+ def fromutc(self, dt: datetime.datetime) -> datetime.datetime:
120
+ """Convert from UTC by the offset in force at that instant."""
121
+ if dt.replace(tzinfo=None) < self._CHANGE_UTC:
122
+ return dt + datetime.timedelta(hours=2)
123
+ return dt + datetime.timedelta(hours=1)
124
+
125
+
126
+ def test_timesince_counts_elapsed_time_across_dst_in_any_zone(
127
+ monkeypatch: pytest.MonkeyPatch,
128
+ ) -> None:
129
+ """Count a clock change for a tzinfo that is not a ``ZoneInfo``."""
130
+ zone = _ClockChange()
131
+ # 23:00 at +02:00 is 21:00 UTC, six hours before the frozen current time.
132
+ moment = datetime.datetime(2026, 10, 24, 23, tzinfo=zone)
133
+ current = datetime.datetime(2026, 10, 25, 3, tzinfo=datetime.timezone.utc)
134
+
135
+ class FrozenDateTime(datetime.datetime):
136
+ """Return the chosen instant as the current time."""
137
+
138
+ @classmethod
139
+ def now(cls, tz: datetime.tzinfo | None = None) -> 'FrozenDateTime':
140
+ """Keep the same instant while returning the requested timezone."""
141
+ return cls.fromtimestamp(current.timestamp(), tz)
142
+
143
+ monkeypatch.setattr(
144
+ formatters,
145
+ 'datetime',
146
+ types.SimpleNamespace(
147
+ datetime=FrozenDateTime,
148
+ timedelta=datetime.timedelta,
149
+ timezone=datetime.timezone,
150
+ ),
151
+ )
152
+ assert formatters.timesince(moment) == '6 hours ago'
153
+
154
+
155
+ class _LookAlike:
156
+ """Not a datetime, but subtracts from one, as ``arrow.Arrow`` does."""
157
+
158
+ def __init__(self, moment: datetime.datetime) -> None:
159
+ """Wrap an aware datetime."""
160
+ self.moment: datetime.datetime = moment
161
+ self.tzinfo: datetime.tzinfo | None = moment.tzinfo
162
+
163
+ def __rsub__(self, other: datetime.datetime) -> datetime.timedelta:
164
+ """Subtract the wrapped datetime from ``other``."""
165
+ return other - self.moment
166
+
167
+
168
+ def test_timesince_datetime_look_alike() -> None:
169
+ """Let an object that is not a datetime do its own subtraction."""
170
+ age: datetime.timedelta = datetime.timedelta(hours=1, minutes=30)
171
+ # Not one of the documented types, so typed as Any like the date test.
172
+ moment: typing.Any = _LookAlike(
173
+ datetime.datetime.now(datetime.timezone.utc) - age
174
+ )
175
+ assert formatters.timesince(moment) == '1 hour and 30 minutes ago'
176
+
177
+
178
+ @pytest.mark.parametrize(
179
+ ('moment', 'current'),
180
+ [
181
+ (
182
+ datetime.datetime(2026, 11, 1, 1, 30, fold=0),
183
+ datetime.datetime(2026, 11, 1, 1, 30, fold=1),
184
+ ),
185
+ (
186
+ datetime.datetime(2026, 11, 1, 1, 30, fold=1),
187
+ datetime.datetime(2026, 11, 1, 1, 30, fold=0),
188
+ ),
189
+ (
190
+ datetime.datetime(2026, 3, 8, 1, 30),
191
+ datetime.datetime(2026, 3, 8, 3, 30),
192
+ ),
193
+ (
194
+ datetime.datetime(2026, 3, 8, 3, 30),
195
+ datetime.datetime(2026, 3, 8, 1, 30),
196
+ ),
197
+ ],
198
+ ids=['fall-back-past', 'fall-back-future', 'spring-past', 'spring-future'],
199
+ )
200
+ @pytest.mark.skipif(
201
+ 'America/New_York' not in available_timezones(),
202
+ reason='IANA timezone data is not available',
203
+ )
204
+ def test_timesince_counts_elapsed_time_across_dst(
205
+ moment: datetime.datetime,
206
+ current: datetime.datetime,
207
+ monkeypatch: pytest.MonkeyPatch,
208
+ ) -> None:
209
+ """Count elapsed time when both datetimes share a changing UTC offset."""
210
+ timezone = ZoneInfo('America/New_York')
211
+ current = current.replace(tzinfo=timezone)
212
+ moment = moment.replace(tzinfo=timezone)
213
+
214
+ class FrozenDateTime(datetime.datetime):
215
+ """Return the chosen instant as the current time."""
216
+
217
+ @classmethod
218
+ def now(cls, tz: datetime.tzinfo | None = None) -> 'FrozenDateTime':
219
+ """Keep the same instant while returning the requested timezone."""
220
+ return cls.fromtimestamp(current.timestamp(), tz)
221
+
222
+ monkeypatch.setattr(
223
+ formatters,
224
+ 'datetime',
225
+ types.SimpleNamespace(
226
+ datetime=FrozenDateTime,
227
+ timedelta=datetime.timedelta,
228
+ timezone=datetime.timezone,
229
+ ),
230
+ )
231
+ assert formatters.timesince(moment) == '1 hour ago'
232
+
233
+
234
+ class _ElapsedRsub(datetime.datetime):
235
+ """A subclass whose own subtraction counts elapsed time, as in pandas."""
236
+
237
+ def __rsub__(self, other: datetime.datetime) -> datetime.timedelta:
238
+ """Subtract by UTC timestamp, so a DST change is counted."""
239
+ seconds: float = other.timestamp() - datetime.datetime.timestamp(self)
240
+ return datetime.timedelta(seconds=seconds)
241
+
242
+
243
+ @pytest.mark.skipif(
244
+ 'America/New_York' not in available_timezones(),
245
+ reason='IANA timezone data is not available',
246
+ )
247
+ def test_timesince_subclass_subtraction_counts_dst_once(
248
+ monkeypatch: pytest.MonkeyPatch,
249
+ ) -> None:
250
+ """Leave a subclass that counts elapsed time itself to count it once."""
251
+ timezone = ZoneInfo('America/New_York')
252
+ current = datetime.datetime(2026, 11, 1, 1, 30, fold=1, tzinfo=timezone)
253
+ moment = _ElapsedRsub(2026, 11, 1, 1, 30, fold=0, tzinfo=timezone)
254
+
255
+ def frozen_now(tz: datetime.tzinfo | None = None) -> datetime.datetime:
256
+ """Return the chosen instant as a plain datetime, like the clock."""
257
+ return datetime.datetime.fromtimestamp(current.timestamp(), tz)
258
+
259
+ monkeypatch.setattr(
260
+ formatters,
261
+ 'datetime',
262
+ types.SimpleNamespace(
263
+ datetime=types.SimpleNamespace(now=frozen_now),
264
+ timedelta=datetime.timedelta,
265
+ timezone=datetime.timezone,
266
+ ),
267
+ )
268
+ assert formatters.timesince(moment) == '1 hour ago'
269
+
270
+
271
+ @pytest.mark.parametrize(
272
+ ('moment', 'expected'),
273
+ [
274
+ (
275
+ datetime.datetime.min.replace(
276
+ tzinfo=datetime.timezone(datetime.timedelta(hours=1))
277
+ ),
278
+ '2000 years and 3 months ago',
279
+ ),
280
+ (
281
+ datetime.datetime.max.replace(
282
+ tzinfo=datetime.timezone(datetime.timedelta(hours=-1))
283
+ ),
284
+ '8005 years and 3 months ago',
285
+ ),
286
+ ],
287
+ ids=['min-positive-offset', 'max-negative-offset'],
288
+ )
289
+ def test_timesince_aware_datetime_boundaries(
290
+ moment: datetime.datetime,
291
+ expected: str,
292
+ monkeypatch: pytest.MonkeyPatch,
293
+ ) -> None:
294
+ """Accept aware inputs whose corresponding UTC date is out of range."""
295
+ current = datetime.datetime(2000, 1, 1, tzinfo=datetime.timezone.utc)
296
+
297
+ class FrozenDateTime(datetime.datetime):
298
+ """Return the chosen instant as the current time."""
299
+
300
+ @classmethod
301
+ def now(cls, tz: datetime.tzinfo | None = None) -> 'FrozenDateTime':
302
+ """Keep the same instant while returning the requested timezone."""
303
+ return cls.fromtimestamp(current.timestamp(), tz)
304
+
305
+ monkeypatch.setattr(
306
+ formatters,
307
+ 'datetime',
308
+ types.SimpleNamespace(
309
+ datetime=FrozenDateTime,
310
+ timedelta=datetime.timedelta,
311
+ timezone=datetime.timezone,
312
+ ),
313
+ )
314
+ assert formatters.timesince(moment) == expected
315
+
316
+
317
+ @pytest.mark.parametrize(
318
+ ('name', 'expected'),
319
+ [
320
+ ('SPAM_EGGS', 'spam_eggs'),
321
+ ('HTTP_OK', 'http_ok'),
322
+ ('MAX_SIZE_LIMIT', 'max_size_limit'),
323
+ ('HTML5Parser', 'html5_parser'),
324
+ ('toUTF8String', 'to_utf8_string'),
325
+ ('ABCD_e', 'abcd_e'),
326
+ ],
327
+ )
328
+ def test_camel_to_underscore_keeps_acronyms_whole(
329
+ name: str, expected: str
330
+ ) -> None:
331
+ """Leave an acronym whole when an underscore or a digit follows it."""
332
+ assert formatters.camel_to_underscore(name) == expected
333
+
334
+
335
+ @pytest.mark.parametrize(
336
+ ('name', 'expected'),
337
+ [
338
+ ('ABc', 'a_bc'),
339
+ ('ABCd', 'ab_cd'),
340
+ ('IPad', 'i_pad'),
341
+ ('AName', 'a_name'),
342
+ ('__ABc__', '__a_bc__'),
343
+ ('XMLParser', 'xml_parser'),
344
+ ],
345
+ )
346
+ def test_camel_to_underscore_splits_short_acronym_boundary(
347
+ name: str, expected: str
348
+ ) -> None:
349
+ """Keep the final capital with the word following even a short acronym."""
350
+ assert formatters.camel_to_underscore(name) == expected
351
+
352
+
353
+ @pytest.mark.parametrize(
354
+ ('name', 'expected'),
355
+ [
356
+ ('IDs', 'ids'),
357
+ ('URLs', 'urls'),
358
+ ('UserIDs', 'user_ids'),
359
+ ('GetURLs', 'get_urls'),
360
+ ('IDsAndNames', 'ids_and_names'),
361
+ ('PDFs_v2', 'pdfs_v2'),
362
+ ('ISpam', 'i_spam'),
363
+ ('IPsec', 'i_psec'),
364
+ ],
365
+ )
366
+ def test_camel_to_underscore_keeps_plural_acronyms_whole(
367
+ name: str, expected: str
368
+ ) -> None:
369
+ """Keep a plural ``s`` that ends a word with the acronym before it."""
370
+ assert formatters.camel_to_underscore(name) == expected
371
+
372
+
373
+ def test_timesince_date_raises_type_error() -> None:
374
+ """Keep raising ``TypeError`` for a date, which has no time."""
375
+ today: typing.Any = datetime.date.today()
376
+ with pytest.raises(TypeError):
377
+ formatters.timesince(today)
@@ -125,7 +125,7 @@ source-include = [
125
125
 
126
126
  [project]
127
127
  name = "python-utils"
128
- version = "4.1.0"
128
+ version = "4.1.2"
129
129
  description = "Python Utils is a module with some convenient utilities not included with the standard Python install"
130
130
  readme = "README.md"
131
131
  requires-python = ">=3.10"
@@ -12,7 +12,7 @@ source-include = ['_python_utils_tests/**/*.py', 'conftest.py', 'tox.ini']
12
12
 
13
13
  [project]
14
14
  name = 'python-utils'
15
- version = "4.1.0" # static; the single source of truth. Bump via `uv version`.
15
+ version = "4.1.2" # static; the single source of truth. Bump via `uv version`.
16
16
  description = 'Python Utils is a module with some convenient utilities not included with the standard Python install'
17
17
  readme = 'README.md'
18
18
  requires-python = '>=3.10'
@@ -21,7 +21,8 @@ def camel_to_underscore(name: str) -> str:
21
21
 
22
22
  If there are existing underscores they will be collapsed with the
23
23
  to-be-added underscores. Multiple consecutive capital letters will not be
24
- split except for the last one.
24
+ split except for the last one. A plural ``s`` that ends a word stays with
25
+ the acronym before it.
25
26
 
26
27
  >>> camel_to_underscore('SpamEggsAndBacon')
27
28
  'spam_eggs_and_bacon'
@@ -42,12 +43,16 @@ def camel_to_underscore(name: str) -> str:
42
43
  # Uppercase and the previous character isn't upper/underscore?
43
44
  # Add the underscore
44
45
  output.append('_')
45
- elif i > 3 and c.islower():
46
- # Will return the last 3 letters to check if we are changing
47
- # case. Only a lowercase letter ends an acronym, an underscore
48
- # or a digit after it leaves the acronym whole.
49
- previous = name[i - 3 : i]
50
- if previous.isalpha() and previous.isupper():
46
+ elif i > 1 and c.islower():
47
+ # Two preceding capitals are enough to mark the boundary:
48
+ # the final capital belongs to the following word. Only a
49
+ # lowercase letter ends an acronym; an underscore or digit
50
+ # after it leaves the acronym whole.
51
+ previous = name[i - 2 : i]
52
+ # A lone ``s`` that ends the word makes the acronym plural
53
+ # (``IDs``, ``URLs``) and does not start a new word.
54
+ plural: bool = c == 's' and not name[i + 1 : i + 2].islower()
55
+ if previous.isalpha() and previous.isupper() and not plural:
51
56
  output.insert(len(output) - 1, '_')
52
57
 
53
58
  output.append(c.lower())
@@ -150,6 +155,13 @@ def timesince(
150
155
  # A date has no `tzinfo`, and no time to subtract either. It gets
151
156
  # the same `TypeError` from the subtraction as it always did.
152
157
  now = datetime.datetime.now(getattr(dt, 'tzinfo', None))
158
+ if now.utcoffset() is not None:
159
+ # Two aware datetimes that share a tzinfo subtract by wall clock,
160
+ # which misses a daylight saving change between them. The current
161
+ # time in UTC counts it. Only `now` is converted, because
162
+ # converting `dt` could overflow near `datetime.min` or
163
+ # `datetime.max`.
164
+ now = now.astimezone(datetime.timezone.utc)
153
165
  diff = abs(now - dt)
154
166
 
155
167
  # Every unit takes its share from what the larger units left over, so a
@@ -1,80 +0,0 @@
1
- """Tests for the formatting helpers in ``python_utils.formatters``."""
2
-
3
- import datetime
4
- import typing
5
-
6
- import pytest
7
-
8
- from python_utils import formatters
9
-
10
-
11
- @pytest.mark.parametrize(
12
- ('days', 'expected'),
13
- [
14
- (30, '1 month ago'),
15
- (35, '1 month and 5 days ago'),
16
- (60, '2 months ago'),
17
- (365, '1 year ago'),
18
- (730, '2 years ago'),
19
- ],
20
- )
21
- def test_timesince_units_do_not_overlap(days: int, expected: str) -> None:
22
- """Take the weeks and days from what the larger units left over."""
23
- delta: datetime.timedelta = datetime.timedelta(days=days)
24
- assert formatters.timesince(delta) == expected
25
-
26
-
27
- @pytest.mark.parametrize(
28
- ('delta', 'expected'),
29
- [
30
- (datetime.timedelta(seconds=-1), '1 second ago'),
31
- (datetime.timedelta(seconds=-61), '1 minute and 1 second ago'),
32
- (datetime.timedelta(days=-400), '1 year and 1 month ago'),
33
- ],
34
- )
35
- def test_timesince_negative_timedelta(
36
- delta: datetime.timedelta, expected: str
37
- ) -> None:
38
- """Describe a negative timedelta by its size, like a datetime."""
39
- assert formatters.timesince(delta) == expected
40
-
41
-
42
- @pytest.mark.parametrize(
43
- 'timezone',
44
- [
45
- datetime.timezone.utc,
46
- datetime.timezone(datetime.timedelta(hours=5, minutes=30)),
47
- ],
48
- )
49
- def test_timesince_aware_datetime(timezone: datetime.timezone) -> None:
50
- """Compare a timezone-aware datetime with an aware current time."""
51
- # Only the two largest units are shown, so the time this test takes
52
- # cannot change the result.
53
- age: datetime.timedelta = datetime.timedelta(hours=1, minutes=30)
54
- moment: datetime.datetime = datetime.datetime.now(timezone) - age
55
- assert formatters.timesince(moment) == '1 hour and 30 minutes ago'
56
-
57
-
58
- @pytest.mark.parametrize(
59
- ('name', 'expected'),
60
- [
61
- ('SPAM_EGGS', 'spam_eggs'),
62
- ('HTTP_OK', 'http_ok'),
63
- ('MAX_SIZE_LIMIT', 'max_size_limit'),
64
- ('HTML5Parser', 'html5_parser'),
65
- ('toUTF8String', 'to_utf8_string'),
66
- ('ABCD_e', 'abcd_e'),
67
- ],
68
- )
69
- def test_camel_to_underscore_keeps_acronyms_whole(
70
- name: str, expected: str
71
- ) -> None:
72
- """Leave an acronym whole when an underscore or a digit follows it."""
73
- assert formatters.camel_to_underscore(name) == expected
74
-
75
-
76
- def test_timesince_date_raises_type_error() -> None:
77
- """Keep raising ``TypeError`` for a date, which has no time."""
78
- today: typing.Any = datetime.date.today()
79
- with pytest.raises(TypeError):
80
- formatters.timesince(today)
File without changes
File without changes
File without changes
File without changes