From 1918350d122569c72521379dda9e9f76e7fa0864 Mon Sep 17 00:00:00 2001 From: Johann Queuniet Date: Tue, 6 Apr 2021 13:00:36 +0200 Subject: [PATCH 1/3] Add a next method to complete the iterator protocol --- pynetbox/core/response.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pynetbox/core/response.py b/pynetbox/core/response.py index c0b7b977..b1d0d907 100644 --- a/pynetbox/core/response.py +++ b/pynetbox/core/response.py @@ -83,6 +83,12 @@ def __iter__(self): for i in self.response: yield self.endpoint.return_obj(i, self.endpoint.api, self.endpoint) + def __next__(self): + for i in self._response_cache: + return self.endpoint.return_obj(i, self.endpoint.api, self.endpoint) + for i in self.response: + return self.endpoint.return_obj(i, self.endpoint.api, self.endpoint) + def __len__(self): try: return self.request.count From ade47c706120dd276bc8d4898f683179ce179fe4 Mon Sep 17 00:00:00 2001 From: zmoody Date: Tue, 6 Apr 2021 20:09:15 -0500 Subject: [PATCH 2/3] Rework iter and add next --- pynetbox/core/response.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pynetbox/core/response.py b/pynetbox/core/response.py index b1d0d907..3a0950de 100644 --- a/pynetbox/core/response.py +++ b/pynetbox/core/response.py @@ -78,16 +78,16 @@ def __init__(self, endpoint, request, **kwargs): self._response_cache = [] def __iter__(self): - for i in self._response_cache: - yield self.endpoint.return_obj(i, self.endpoint.api, self.endpoint) + if self._response_cache: + yield self.endpoint.return_obj( + self._response_cache.pop(), self.endpoint.api, self.endpoint + ) for i in self.response: yield self.endpoint.return_obj(i, self.endpoint.api, self.endpoint) def __next__(self): - for i in self._response_cache: - return self.endpoint.return_obj(i, self.endpoint.api, self.endpoint) - for i in self.response: - return self.endpoint.return_obj(i, self.endpoint.api, self.endpoint) + for i in self: + return i def __len__(self): try: From 90ee2f370dc7af7d399d7834895dee00dbc5e8b7 Mon Sep 17 00:00:00 2001 From: zmoody Date: Tue, 6 Apr 2021 20:09:26 -0500 Subject: [PATCH 3/3] remove iter from get method --- pynetbox/core/endpoint.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pynetbox/core/endpoint.py b/pynetbox/core/endpoint.py index 2031cbdb..ab1359dc 100644 --- a/pynetbox/core/endpoint.py +++ b/pynetbox/core/endpoint.py @@ -133,7 +133,7 @@ def get(self, *args, **kwargs): key = None if not key: - return next(iter(self.filter(**kwargs)), None) + return next(self.filter(**kwargs), None) req = Request( key=key, @@ -143,7 +143,7 @@ def get(self, *args, **kwargs): http_session=self.api.http_session, ) try: - return next(iter(RecordSet(self, req)), None) + return next(RecordSet(self, req), None) except RequestError as e: if e.req.status_code == 404: return None