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.