Skip to content

Commit 48bb99d

Browse files
committed
chore: remove 'while True' because why not
1 parent 8f08e7f commit 48bb99d

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

tests/share/search/index_strategy/_common_trovesearch_tests.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ def test_cardsearch_pagination(self):
117117
}))
118118
self._index_indexcards(_cards)
119119
# gather all pages results:
120-
_querystring: str = f'page[size]={_page_size}'
120+
_querystring: str | None = f'page[size]={_page_size}'
121121
_result_iris: set[str] = set()
122122
_page_count = 0
123-
while True:
123+
while _querystring is not None:
124124
_cardsearch_handle = self.index_strategy.pls_handle_cardsearch(
125125
CardsearchParams.from_querystring(_querystring),
126126
)
@@ -133,9 +133,11 @@ def test_cardsearch_pagination(self):
133133
_result_iris.update(_page_iris)
134134
_page_count += 1
135135
_next_cursor = _cardsearch_handle.cursor.next_cursor()
136-
if _next_cursor is None:
137-
break
138-
_querystring = urlencode({'page[cursor]': _next_cursor.as_queryparam_value()})
136+
_querystring = (
137+
urlencode({'page[cursor]': _next_cursor.as_queryparam_value()})
138+
if _next_cursor is not None
139+
else None # done
140+
)
139141
self.assertEqual(_page_count, math.ceil(_total_count / _page_size))
140142
self.assertEqual(_result_iris, _expected_iris)
141143

tests/share/test_oaipmh_trove.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,9 @@ def _assert_full_list(self, verb, params, request_method, expected_count, page_s
232232
pages = 0
233233
count = 0
234234
token = None
235-
while True:
236-
if token:
237-
parsed = oai_request({'verb': verb, 'resumptionToken': token}, request_method)
238-
else:
239-
parsed = oai_request({'verb': verb, 'metadataPrefix': 'oai_dc', **params}, request_method)
235+
next_params: dict[str, str] | None = {'verb': verb, 'metadataPrefix': 'oai_dc', **params}
236+
while next_params is not None:
237+
parsed = oai_request(next_params, request_method)
240238
page = parsed.xpath('//oai:header/oai:identifier', namespaces=NAMESPACES)
241239
pages += 1
242240
count += len(page)
@@ -245,9 +243,10 @@ def _assert_full_list(self, verb, params, request_method, expected_count, page_s
245243
token = token[0].text
246244
if token:
247245
assert len(page) == page_size
246+
next_params = {'verb': verb, 'resumptionToken': token}
248247
else:
249248
assert len(page) <= page_size
250-
break
249+
next_params = None # done
251250

252251
assert count == expected_count
253252
assert pages == math.ceil(expected_count / page_size)

trove/render/jsonapi.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,11 @@
3838

3939
def _resource_ids_defaultdict() -> defaultdict[Any, str]:
4040
_prefix = str(time.time_ns())
41-
_ints = itertools.count()
42-
43-
def _iter_ids() -> Iterator[str]:
44-
while True:
45-
_id = next(_ints)
46-
yield f'{_prefix}-{_id}'
47-
48-
_ids = _iter_ids()
49-
return defaultdict(lambda: next(_ids))
41+
_infinite_ids = (
42+
f'{_prefix}-{_id}'
43+
for _id in itertools.count()
44+
)
45+
return defaultdict(_infinite_ids.__next__)
5046

5147

5248
@dataclasses.dataclass

trove/util/django.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@ def pk_chunked(queryset: QuerySet, chunksize: int) -> Generator[list]:
1616
'''
1717
_ordered_qs = queryset.order_by('pk')
1818
_prior_end_pk = None
19-
while True: # for each chunk:
20-
_qs = (
21-
_ordered_qs
22-
if _prior_end_pk is None
23-
else _ordered_qs.filter(pk__gt=_prior_end_pk)
24-
)
19+
_chunk_qs: QuerySet | None = _ordered_qs
20+
while _chunk_qs is not None: # for each chunk:
2521
# load primary key values only
26-
_pks = list(_qs.values_list('pk', flat=True)[:chunksize])
27-
if not _pks:
28-
break # done
29-
_end_pk = _pks[-1]
30-
if (_prior_end_pk is not None) and (_end_pk <= _prior_end_pk):
31-
raise RuntimeError(f'sentinel pks not ascending?? got {_end_pk} after {_prior_end_pk}')
32-
_prior_end_pk = _end_pk
33-
yield _pks
22+
_pks = list(_chunk_qs.values_list('pk', flat=True)[:chunksize])
23+
if _pks:
24+
_end_pk = _pks[-1]
25+
if (_prior_end_pk is not None) and (_end_pk <= _prior_end_pk):
26+
raise RuntimeError(f'sentinel pks not ascending?? got {_end_pk} after {_prior_end_pk}')
27+
yield _pks
28+
_prior_end_pk = _end_pk
29+
_chunk_qs = _ordered_qs.filter(pk__gt=_prior_end_pk)
30+
else:
31+
_chunk_qs = None # done

0 commit comments

Comments
 (0)