<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from datetime import date, datetime
import calendar
import requests
import json

from settings import API_GW_BASE_URL


def partial_by_index(list_object, index):
    def iter(l, result):
        if l == []:
            return result
        elif len(l) < index:
            result.append(l)
            return result

        result.append(l[:index])
        return iter(l[index:], result)

    return iter(list_object, [])


def response_text_from_request(base_url, resource):
    try:
        response = requests.get(base_url+resource)
    except requests.ConnectionError:
        text = '{"error": "ConnectionError"}'
        return text

    if response.status_code != 200:
        text = '{"error": "Statuscode %s"}' % response.status_code
 
        return text

    text = response.text
 
    return text


def objects_from_request(base_url, resource):
    text = response_text_from_request(base_url, resource)
    objects = json.loads(text)
    return objects


def is_empty_objects(objects):
    result = False
    if isinstance(objects, list) and objects == []:
        result = True

    return result


def is_error_in_objects(objects):
    result = False
    if isinstance(objects, dict):
        for key in objects.keys():
            if isinstance(objects[key], (list, str)):
                continue

            if objects[key].get('error'):
                result = True

    return result


def get_template_name_from_exception_check(objects, default='index.html'):
    template_name = default
    if is_error_in_objects(objects):
        template_name = 'error.html'
    if is_empty_objects(objects):
        template_name = 'no-data.html'

    return template_name


def map_weather_icons_css_with_weather_text():
    weather_icons = {
        '구름조금': 'wi-cloud',
        '구름많음': 'wi-cloudy',
        '흐림': 'wi-fog',
        '흐리고 비': 'wi-rain',
        '구름많고 비': 'wi-rain',
        '비': 'wi-rain',
        '소나기': 'wi-showers',
        '눈': 'wi-snow',
        '맑음': 'wi-day-sunny',
        'other': 'wi-cloud',
    }
    return weather_icons


def get_weather_objects(date=''):
    def get_publish_resources(date):
        publish_weather_resource = '/weather/today/weather/'
        publish_resource = '/weather/today/'

        if date != '':
            publish_weather_resource = '/weather/%s/weather/' % date
            publish_resource = '/weather/%s/' % date
        return publish_weather_resource, publish_resource

    publish_weather_resource, publish_resource = get_publish_resources(date)

    objects = dict()

    # these exception handlings are hardcoded. it should be refactored.
    publish_weather = objects_from_request(API_GW_BASE_URL,
        publish_weather_resource)
    if publish_weather == []:
        return publish_weather

    publish = objects_from_request(API_GW_BASE_URL, publish_resource)
    if publish == []:
        return publish

    '''
    weather cities are seperated by 7.
    for example,
    weather[0:7] is seoul weekdays weather
    '''
    num_weekdays = 7
    partial_publish_weather = partial_by_index(publish_weather,
        num_weekdays)

    first_date = datetime.strptime(publish_weather[0]['date'], '%Y-%m-%d')

    first_weekday = first_date.weekday()

    weekdays = calendar.day_abbr[first_weekday:] +\
        calendar.day_abbr[:first_weekday]

    objects['partial_weather'] = partial_publish_weather
    objects['publish'] = publish
    objects['first_weekday'] = weekdays[0]
    objects['weekdays'] = weekdays
    objects['main_weather'] = publish_weather[0]
    objects['weather_icons'] = map_weather_icons_css_with_weather_text()<commit_msg>no empty variable return, jsut return []<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from datetime import date, datetime
import calendar
import requests
import json

from settings import API_GW_BASE_URL


def partial_by_index(list_object, index):
    def iter(l, result):
        if l == []:
            return result
        elif len(l) < index:
            result.append(l)
            return result

        result.append(l[:index])
        return iter(l[index:], result)

    return iter(list_object, [])


def response_text_from_request(base_url, resource):
    try:
        response = requests.get(base_url+resource)
    except requests.ConnectionError:
        text = '{"error": "ConnectionError"}'
        return text

    if response.status_code != 200:
        text = '{"error": "Statuscode %s"}' % response.status_code
 
        return text

    text = response.text
 
    return text


def objects_from_request(base_url, resource):
    text = response_text_from_request(base_url, resource)
    objects = json.loads(text)
    return objects


def is_empty_objects(objects):
    result = False
    if isinstance(objects, list) and objects == []:
        result = True

    return result


def is_error_in_objects(objects):
    result = False
    if isinstance(objects, dict):
        for key in objects.keys():
            if isinstance(objects[key], (list, str)):
                continue

            if objects[key].get('error'):
                result = True

    return result


def get_template_name_from_exception_check(objects, default='index.html'):
    template_name = default
    if is_error_in_objects(objects):
        template_name = 'error.html'
    if is_empty_objects(objects):
        template_name = 'no-data.html'

    return template_name


def map_weather_icons_css_with_weather_text():
    weather_icons = {
        '구름조금': 'wi-cloud',
        '구름많음': 'wi-cloudy',
        '흐림': 'wi-fog',
        '흐리고 비': 'wi-rain',
        '구름많고 비': 'wi-rain',
        '비': 'wi-rain',
        '소나기': 'wi-showers',
        '눈': 'wi-snow',
        '맑음': 'wi-day-sunny',
        'other': 'wi-cloud',
    }
    return weather_icons


def get_weather_objects(date=''):
    def get_publish_resources(date):
        publish_weather_resource = '/weather/today/weather/'
        publish_resource = '/weather/today/'

        if date != '':
            publish_weather_resource = '/weather/%s/weather/' % date
            publish_resource = '/weather/%s/' % date
        return publish_weather_resource, publish_resource

    publish_weather_resource, publish_resource = get_publish_resources(date)

    objects = dict()

    # these exception handlings are hardcoded. it should be refactored.
    publish_weather = objects_from_request(API_GW_BASE_URL,
        publish_weather_resource)
    if publish_weather == []:
        return []

    publish = objects_from_request(API_GW_BASE_URL, publish_resource)
    if publish == []:
        return []

    '''
    weather cities are seperated by 7.
    for example,
    weather[0:7] is seoul weekdays weather
    '''
    num_weekdays = 7
    partial_publish_weather = partial_by_index(publish_weather,
        num_weekdays)

    first_date = datetime.strptime(publish_weather[0]['date'], '%Y-%m-%d')

    first_weekday = first_date.weekday()

    weekdays = calendar.day_abbr[first_weekday:] +\
        calendar.day_abbr[:first_weekday]

    objects['partial_weather'] = partial_publish_weather
    objects['publish'] = publish
    objects['first_weekday'] = weekdays[0]
    objects['weekdays'] = weekdays
    objects['main_weather'] = publish_weather[0]
    objects['weather_icons'] = map_weather_icons_css_with_weather_text()
{'fraction_non_alphanumeric': 0.11198375084631008, 'fraction_numerical': 0.002979011509817197, 'mean_word_length': 2.856919060052219, 'pattern_counts': {'":': 4, '<': 5, '<?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': '2848485', 'n_tokens_mistral': 2376, 'n_tokens_neox': 2266, 'n_words': 483}