<commit_before>import sys

from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from django.core.cache import cache

from forecasts.lib.forecast import Forecast
from registrations.models import Registration

class Command(BaseCommand):

    def chunks(self, list, chunk_size):
        """ Yield successive chunks from a list """
        for i in xrange(0, len(list), chunk_size):
            yield list[i:i+chunk_size]

    def handle(self, *args, **options):

        latlong_list = []
        for registration in Registration.objects.filter(status=1):
            # Check the cache to see if this lat/long has already been fetched.
            cache_key = "%s,%s" % (registration.latitude, registration.longitude)
            entry = cache.get(cache_key)
            if not entry:            
                latlong = (registration.latitude, registration.longitude)
                latlong_list.append(latlong)
        
        # Now that we have a list of all the lat/long tuples we need to look up, 
        # let's cut them into chunks of 10
        for chunk in self.chunks(latlong_list, 25):        
            location_forecasts = Forecast.get_multiple_forecasts(chunk)
            for location in location_forecasts:
                latlong = location['location']
                forecasts = location['forecasts']
                cache_key = "%s,%s" % (latlong[0], latlong[1])
                print "Caching forecast for %s, %s" % (latlong[0], latlong[1])
                cache.set(cache_key, forecasts, 3600)<commit_msg>Changed forecast fetch to pull 200 locations at a time per NWS docs.<commit_after>import sys

from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from django.core.cache import cache

from forecasts.lib.forecast import Forecast
from registrations.models import Registration

class Command(BaseCommand):

    def chunks(self, list, chunk_size):
        """ Yield successive chunks from a list """
        for i in xrange(0, len(list), chunk_size):
            yield list[i:i+chunk_size]

    def handle(self, *args, **options):

        latlong_list = []
        for registration in Registration.objects.filter(status=1):
            # Check the cache to see if this lat/long has already been fetched.
            cache_key = "%s,%s" % (registration.latitude, registration.longitude)
            entry = cache.get(cache_key)
            if not entry:            
                latlong = (registration.latitude, registration.longitude)
                latlong_list.append(latlong)
        
        # Now that we have a list of all the lat/long tuples we need to look up, 
        # let's cut them into chunks of 200 (the max the service allows)
        for chunk in self.chunks(latlong_list, 200):        
            location_forecasts = Forecast.get_multiple_forecasts(chunk)
            for location in location_forecasts:
                latlong = location['location']
                forecasts = location['forecasts']
                cache_key = "%s,%s" % (latlong[0], latlong[1])
                print "Caching forecast for %s, %s" % (latlong[0], latlong[1])
                cache.set(cache_key, forecasts, 3600)
{'fraction_non_alphanumeric': 0.0879086983343615, 'fraction_numerical': 0.010178901912399754, 'mean_word_length': 2.417281348788198, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3826178', 'n_tokens_mistral': 923, 'n_tokens_neox': 851, 'n_words': 276}