Skip to content

Commit ebf28b3

Browse files
author
Rituparna Mukherjee
committed
Updating logic so that edge iterator does not erase the contents of the queue during next call
Summary: The edge iterator wipes out the previous contents of the queue while iterating through the response Test Plan: The test runs are below: ritu-mbp:python-ads-sdk ritu$ python -i facebookads/bootstrap.py >>> act = AdAccount('act_219066941615718') >>> adsets = act.get_ad_sets() >>> adsets [<AdSet> { "id": "6034561139616" }, <AdSet> { "id": "6033251546616" }, <AdSet> { "id": "6031938715216" }, <AdSet> { "id": "6031544286616" }] >>> for adset in adsets: ... print(adset) ... <AdSet> { "id": "6034561139616" } <AdSet> { "id": "6033251546616" } <AdSet> { "id": "6031938715216" } >>> adsets [<AdSet> { "id": "6034561139616" }, <AdSet> { "id": "6033251546616" }, <AdSet> { "id": "6031938715216" }, <AdSet> { "id": "6031544286616" }] >>> exit() Unit Test and Integration test run: ritu-mbp:python-ads-sdk ritu$ python -m facebookads.test.unit ............................ ---------------------------------------------------------------------- Ran 28 tests in 0.015s OK ritu-mbp:python-ads-sdk ritu$ python -m facebookads.test.integration .................... ---------------------------------------------------------------------- Ran 20 tests in 34.884s OK ritu-mbp:python-ads-sdk ritu$ python3 -m facebookads.test.integration .................... ---------------------------------------------------------------------- Ran 20 tests in 24.358s OK ritu-mbp:python-ads-sdk ritu$ python3 -m facebookads.test.unit ............................ ---------------------------------------------------------------------- Ran 28 tests in 0.017s OK
1 parent 6a5708b commit ebf28b3

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

facebookads/objects.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def __init__(
119119
target_objects_class.get_endpoint(),
120120
)
121121
self._queue = []
122+
self._count = 0
122123
self._finished_iteration = False
123124
self._total_count = None
124125
self._include_summary = include_summary
@@ -133,12 +134,14 @@ def __iter__(self):
133134
return self
134135

135136
def __next__(self):
137+
self._count += 1
136138
# Load next page at end.
139+
# If the queue counter equals the length of the queue and
137140
# If load_next_page returns False, raise StopIteration exception
138-
if not self._queue and not self.load_next_page():
141+
if (self._count == len(self._queue) and not self.load_next_page()):
139142
raise StopIteration()
140143

141-
return self._queue.pop(0)
144+
return self._queue[self._count-1]
142145

143146
# Python 2 compatibility.
144147
next = __next__
@@ -187,6 +190,8 @@ def load_next_page(self):
187190
self._total_count = response['summary']['total_count']
188191

189192
self._queue = self.build_objects_from_response(response)
193+
self._count = 0
194+
190195
return len(self._queue) > 0
191196

192197
def build_objects_from_response(self, response):

0 commit comments

Comments
 (0)