Pregunta

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.

¿Fue útil?

Solución

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

Otros consejos

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:])
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top