Skip to content

Commit 30b813c

Browse files
author
Zach Moody
authored
Merge pull request #358 from jqueuniet/add_next_method
Add a __next__ method to complete the iterator protocol
2 parents 9f3f4a9 + 90ee2f3 commit 30b813c

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

pynetbox/core/endpoint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def get(self, *args, **kwargs):
133133
key = None
134134

135135
if not key:
136-
return next(iter(self.filter(**kwargs)), None)
136+
return next(self.filter(**kwargs), None)
137137

138138
req = Request(
139139
key=key,
@@ -143,7 +143,7 @@ def get(self, *args, **kwargs):
143143
http_session=self.api.http_session,
144144
)
145145
try:
146-
return next(iter(RecordSet(self, req)), None)
146+
return next(RecordSet(self, req), None)
147147
except RequestError as e:
148148
if e.req.status_code == 404:
149149
return None

pynetbox/core/response.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,17 @@ def __init__(self, endpoint, request, **kwargs):
7878
self._response_cache = []
7979

8080
def __iter__(self):
81-
for i in self._response_cache:
82-
yield self.endpoint.return_obj(i, self.endpoint.api, self.endpoint)
81+
if self._response_cache:
82+
yield self.endpoint.return_obj(
83+
self._response_cache.pop(), self.endpoint.api, self.endpoint
84+
)
8385
for i in self.response:
8486
yield self.endpoint.return_obj(i, self.endpoint.api, self.endpoint)
8587

88+
def __next__(self):
89+
for i in self:
90+
return i
91+
8692
def __len__(self):
8793
try:
8894
return self.request.count

0 commit comments

Comments
 (0)