0
unsampled_date_ranges = []

def recursive(start, end, datelist):

    results = ga.GAnalytics().create_query(profile_id, 
                             metrics, 
                             start,
                             end,
                             dimensions).execute()

    if results.get("containsSampledData") is True:

        x = len(datelist) / 2

        recursive(datelist[0],datelist[:x][-1],datelist[:x])
        recursive(datelist[x:][0],datelist[-1],datelist[x:])

    else:

        global unsampled_date_ranges

        unsampled_date_ranges.append((start, end))


recursive(start_date, end_date, date_list)
print unsampled_date_ranges

The function above takes a start date, end date and an inclusive list of dates based on the start and end dates. If first checks if the data returned for the initial date range is sampled, if it is sampled then the date range is split in half then checked, and so on.

This is currently working as expected, but I know that using global variables is not best practice, so I am looking for guidance on how to forego the use of a global variable and how to have my function return a list.

0

2 Answers 2

2

change:

def recursive(start, end, datelist)
...
recursive(datelist[0],datelist[:x][-1],datelist[:x])
recursive(datelist[x:][0],datelist[-1],datelist[x:])
...
recursive(start_date, end_date, date_list)
print unsampled_date_ranges

to:

def recursive(start, end, datelist, unsampled_date_ranges)
...
recursive(datelist[0],datelist[:x][-1],datelist[:x], unsampled_date_ranges)
recursive(datelist[x:][0],datelist[-1],datelist[x:], unsampled_date_ranges)
...
unsampled_date_ranges = []
recursive(start_date, end_date, date_list, unsampled_date_ranges)
print unsampled_date_ranges

Then remove the global line

Sign up to request clarification or add additional context in comments.

Comments

1

As an alternative to passing in a list object that will be appended to, you can create one on the fly and combine results together.

def recursive(start, end, datelist):

    results = ga.GAnalytics().create_query(profile_id, 
                             metrics, 
                             start,
                             end,
                             dimensions).execute()

    if results.get("containsSampledData") is True:

        x = len(datelist) / 2
        return recursive(datelist[0],datelist[:x][-1],datelist[:x]) + recursive(datelist[x:][0],datelist[-1],datelist[x:])

    else:
        return [start, end]


unsampled_date_ranges = recursive(start_date, end_date, date_list)
print unsampled_date_ranges

This way requires more memory since it's creating temporary lists in each step, but you might consider that negligible anyway, depending on the size of your data.

By the way, it's conventional to put the base case first in recursive functions, so you may want to swap the logic of your if/else block.

def recursive(start, end, datelist):

    results = ga.GAnalytics().create_query(profile_id, 
                             metrics, 
                             start,
                             end,
                             dimensions).execute()

    if not results.get("containsSampledData"):
        return [start, end]    
    else:
        x = len(datelist) / 2
        return recursive(datelist[0],datelist[:x][-1],datelist[:x]) + recursive(datelist[x:][0],datelist[-1],datelist[x:])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.