1

I have the variable r of type dict. Variable item holds a list of items, and total_results is an int. As long as the condition is met that the number of items is less than the total amount of items, the function is called recursively. However, if I test whether the items are equal to the total results, the return statement returns None. Changing this elif to if does return the correct value.

Could someone point me into the right direction to find out why returning r yields None in the elif or else block?

Many thanks !!

Relevant code snippet:

if len(items) < total_results:
    params['start_index'] = len(items)

    self.fetch_company_officers(company_number=company_number, items=items, **params)

elif len(items) == total_results:
    r['items'] = items
    return r

Full code:

def fetch_company_officers(self, company_number, items=None, **kwargs):
    uri = 'company/{}/officers'.format(company_number)
    params = kwargs

    r = self.make_request(uri=uri, **params)

    # Test if items are set
    if items is None:
        items = r['items']
    else:
        items.extend(r['items'])

    # Iterate multiple pages
    total_results = r['total_results']

    if len(items) < total_results:
        params['start_index'] = len(items)

        self.fetch_company_officers(company_number=company_number, items=items, **params)

    elif len(items) == total_results: # TO DO: ??
        r['items'] = items
        return r

2 Answers2

0

Your function is missing logic for the scenario len(items) > total_results.

So the function is probably reaching end of line and returning None by default.

AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
  • Hello. I was thinking about that too, but since the code cannot get more items than there are, that shouldn't be the cause of the problem, I think. I tried adding the conditional but still I'm receiving None. –  Jan 07 '18 at 03:29
0

I think I understand what you're trying to do, but recursion is not the way to go here. You want to keep getting the items from the next page for as long as (while) the number of items you've retrieved smaller than the total number of results you expect. So.. lets use a while loop!

This is my version of your code. Note that I've not been able to test this since I don't have the rest of your code.

def fetch_company_officers(self, company_number, **params):

    # Start with an empty list of items
    items = []

    # Make the first request
    uri = f"company/{company_number}/officers"
    response = self.make_request(uri=uri, **params)

    # Extract information from the first batch
    items += response.get("items", [])
    total_results = response.get("total_results")

    # Stop iterating when we don't know the total number of results to expect
    if total_results is None:
        return items

    # Keep getting more results until our list of items
    # is as long as the total number of results we expect
    while len(items) < total_results:

        # Get the next page
        response = self.make_request(uri=uri, start_index=len(items), **params)

        # Extract information from this batch
        items += response.get("items", [])

    # We've now retrieved everything
    return items
Gijs Wobben
  • 1,974
  • 1
  • 10
  • 13