<commit_before>import xmltodict
import argparse
from datetime import datetime
import redis
import hashlib
import os

OBSERVED_ROUTE_IDS = set([
             '11',  # Vail-Vail Pass
             '10',  # Vail Pass-Copper Mtn
             '9',  # Copper Mtn-Silverthorne
             '6',  # Silverthorne-Eisenhower/Johnson tunnels
             '5060',  # Eisenhower/Johnson tunnels-Georgetown
             '4',  # Georgetown-Idaho Springs
             '3',  # Idaho Springs-Genesee
             '2'  # Genesee-C470
             ])

VAIL_ROUTES = set([11,10,9,6,5060,4,3,2])
COPPER_BRECK_ROUTES = set([9,6,5060,4,3,2])
KEYSTONE_ABASIN_ROUTES = set([6,5060,4,3,2])

cache = None 

class Resort:

    def __init__(self, name, watched_routes, raw_routes):
        self.name = name
        self.watched_routes = watched_routes
        self.raw_routes = raw_routes
        
        self.closed_routes = []
        self.hazardous_routes = []
        self.dates = [] 

        for route in self.raw_routes:
            if int(route['rc:WeatherRouteId']) in self.watched_routes:
                route_state = {
                        'id' : int(route['rc:WeatherRouteId']),
                        'route_name' : route['rc:RouteName'],
                        'date' : datetime.strptime(route['rc:CalculatedDate'].split('.')[0], '%Y-%m-%dT%H:%M:%S'),
                        'condition' : route['rc:RoadConditionCategoryTxt'],
                        'condition_code' : float(route['rc:RoadConditionCategoryCd']),
                        'hazardous' : bool(route['rc:IsHazardousCondition']),
                        }
 
                if route_state['condition'] == 'Closed' or route_state['condition_code'] == 1:
                    self.closed_routes.append(route_state['route_name'])
                    self.dates.append(route_state['date'])
                
                elif route_state['hazardous']:   
                    self.hazardous_routes.append(route_state['route_name'])
                    self.dates.append(route_state['date'])

    def generate_message(self):
        if len(self.closed_routes) != 0:
            message = 'I70 is CLOSED between {aggregate_route}. This affects {resort}. Calculated on {date}.'.format(
                    aggregate_route= route_summarizer(self.closed_routes), resort=self.name, date=earliest_date(self.dates)) 
        
        elif len(self.hazardous_routes) != 0:
            message = 'I70 is OPEN, but is being impacted by weather between {aggregate_route}. This affects {resort}. Calculated on {date}.'.format(
                     aggregate_route= route_summarizer(self.hazardous_routes), resort=self.name, date=earliest_date(self.dates)) 

        else:  
            message = 'I70 is OPEN, and unaffected by weather. This affects {resort}. Calculated on {date}.'.format(
                    resort=self.name, date=earliest_date(self.dates))
        
        print('{} - {}'.format(self.name, message))
        cache.set(self.name, message)


def route_summarizer(route_names):
    first = route_names[0].split('-')[0]
    last = route_names[-1].split('-')[1]
    return '{0} and {1}'.format(first, last)

def earliest_date(dates):
    earliest = dates[0]
    for date in dates:
        if date < earliest:
            earliest = date

    return earliest.strftime('%m/%d @ %I:%M %p')


def gather_observed_routes(local):
    """
    Gathers and reurns an array of parsed route dictionaries based on route_ids.
    """
 
    observed_routes = []
    if local:
        print('GATHERING_LOCAL')
        weather_routes = xmltodict.parse(
                open('../schema/road_conditions.xml', 'r').read())
    
    else: 
        import requests
        import urllib

        username = os.getenv('username')
        password = <PASSWORD>('password')

        url = "https://{0}:{1}@data.cotrip.org/xml/road_conditions.xml".format(
                username, urllib.quote(password, safe=''))
    
        r = requests.get(url) 
        if not content_update(r.content):
            return []
        
        weather_routes = xmltodict.parse(r.content) 
 
    for route in weather_routes["rc:RoadConditionsDetails"]["rc:WeatherRoute"]:
        if route["rc:WeatherRouteId"] in OBSERVED_ROUTE_IDS:
            observed_routes.append(route)

    return observed_routes

def content_update(content):
    new_hash = hashlib.sha224(content).hexdigest()
    old_hash = cache.get('hash')
    if old_hash == new_hash: 
        return False
    
    else:
        cache.set('hash', new_hash)
        return True
<commit_msg>proper returns<commit_after>import xmltodict
import argparse
from datetime import datetime
import redis
import hashlib
import os

OBSERVED_ROUTE_IDS = set([
             '11',  # Vail-Vail Pass
             '10',  # Vail Pass-Copper Mtn
             '9',  # Copper Mtn-Silverthorne
             '6',  # Silverthorne-Eisenhower/Johnson tunnels
             '5060',  # Eisenhower/Johnson tunnels-Georgetown
             '4',  # Georgetown-Idaho Springs
             '3',  # Idaho Springs-Genesee
             '2'  # Genesee-C470
             ])

VAIL_ROUTES = set([11,10,9,6,5060,4,3,2])
COPPER_BRECK_ROUTES = set([9,6,5060,4,3,2])
KEYSTONE_ABASIN_ROUTES = set([6,5060,4,3,2])

cache = None 

class Resort:

    def __init__(self, name, watched_routes, raw_routes):
        self.name = name
        self.watched_routes = watched_routes
        self.raw_routes = raw_routes
        
        self.closed_routes = []
        self.hazardous_routes = []
        self.dates = [] 

        for route in self.raw_routes:
            if int(route['rc:WeatherRouteId']) in self.watched_routes:
                route_state = {
                        'id' : int(route['rc:WeatherRouteId']),
                        'route_name' : route['rc:RouteName'],
                        'date' : datetime.strptime(route['rc:CalculatedDate'].split('.')[0], '%Y-%m-%dT%H:%M:%S'),
                        'condition' : route['rc:RoadConditionCategoryTxt'],
                        'condition_code' : float(route['rc:RoadConditionCategoryCd']),
                        'hazardous' : bool(route['rc:IsHazardousCondition']),
                        }
 
                if route_state['condition'] == 'Closed' or route_state['condition_code'] == 1:
                    self.closed_routes.append(route_state['route_name'])
                    self.dates.append(route_state['date'])
                
                elif route_state['hazardous']:   
                    self.hazardous_routes.append(route_state['route_name'])
                    self.dates.append(route_state['date'])

    def generate_message(self):
        if len(self.closed_routes) != 0:
            message = 'I70 is CLOSED between {aggregate_route}. This affects {resort}. Calculated on {date}.'.format(
                    aggregate_route= route_summarizer(self.closed_routes), resort=self.name, date=earliest_date(self.dates)) 
        
        elif len(self.hazardous_routes) != 0:
            message = 'I70 is OPEN, but is being impacted by weather between {aggregate_route}. This affects {resort}. Calculated on {date}.'.format(
                     aggregate_route= route_summarizer(self.hazardous_routes), resort=self.name, date=earliest_date(self.dates)) 

        else:  
            message = 'I70 is OPEN, and unaffected by weather. This affects {resort}. Calculated on {date}.'.format(
                    resort=self.name, date=earliest_date(self.dates))
        
        print('{} - {}'.format(self.name, message))
        cache.set(self.name, message)


def route_summarizer(route_names):
    first = route_names[0].split('-')[0]
    last = route_names[-1].split('-')[1]
    return '{0} and {1}'.format(first, last)

def earliest_date(dates):
    earliest = dates[0]
    for date in dates:
        if date < earliest:
            earliest = date

    return earliest.strftime('%m/%d @ %I:%M %p')


def gather_observed_routes(local):
    """
    Gathers and reurns an array of parsed route dictionaries based on route_ids.
    """
 
    observed_routes = []
    if local:
        print('GATHERING_LOCAL')
        weather_routes = xmltodict.parse(
                open('../schema/road_conditions.xml', 'r').read())
    
    else: 
        import requests
        import urllib

        username = os.getenv('username')
        password = os.getenv('password')

        url = "https://{0}:{1}@data.cotrip.org/xml/road_conditions.xml".format(
                username, urllib.quote(password, safe=''))
    
        r = requests.get(url) 
        if not content_update(r.content):
            return []
        
        weather_routes = xmltodict.parse(r.content) 
 
    for route in weather_routes["rc:RoadConditionsDetails"]["rc:WeatherRoute"]:
        if route["rc:WeatherRouteId"] in OBSERVED_ROUTE_IDS:
            observed_routes.append(route)

    return observed_routes

def content_update(content):
    new_hash = hashlib.sha224(content).hexdigest()
    old_hash = cache.get('hash')

    if old_hash == new_hash: 
        return False
    
    else:
        cache.set('hash', new_hash)
        return True

{'fraction_non_alphanumeric': 0.12383663637359028, 'fraction_numerical': 0.014891054418044454, 'mean_word_length': 2.2184637068358, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '7445711', 'n_tokens_mistral': 2944, 'n_tokens_neox': 2791, 'n_words': 531}