1
votes

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
Can you present the whole recursive function? - Miket25
What get's returned if the execution flows through your if block instead of your elif block? (Keeping in mind that if nothing is explicitly returned then None is returned) - Patrick Haugh
Here's the full code, see my original post. - user9182913
I think that you should first check what is the value comming out of both 1. Len(items) 2. Total_results Using print function - Nishit Zinzuvadiya

2 Answers

0
votes

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

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

0
votes

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