<commit_before>from i3pystatus import IntervalModule
from i3pystatus.core.util import internet, require

from urllib.request import urlopen
import re
import xml.etree.ElementTree as ElementTree

WEATHER_COM_URL = 'http://wxdata.weather.com/wxdata/weather/local/%s?unit=%s&cc=*'

class Weather(IntervalModule):
    """
    This module gets the weather from weather.com.
    First, you need to get the code for the location from the www.weather.com

    .. rubric:: Available formatters

    * {current_temp}
    * {current_wind}
    * {humidity}

    """

    interval = 20

    settings = (
        "location_code",
        ("colorize", "Enable color with temperature and UTF-8 icons."),
        ("units", "Celsius (metric) or Fahrenheit (imperial)"),
        "format",
    )
    required = ("location_code",)

    location_code = None
    units = "metric"
    format = "{current_temp}"
    colorize = False
    color_icons = {
        "Fair": (u"\u2600", "#FFCC00"),
        "Cloudy": (u"\u2601", "#F8F8FF"),
        "Partly Cloudy": (u"\u2601", "#F8F8FF"),  # \u26c5 is not in many fonts
        "Rainy": (u"\u2614", "#CBD2C0"),
        "Sunny": (u"\u263C", "#FFFF00"),
        "Snow": (u"\u2603", "#FFFFFF"),
        "default": ("", None),
    }

    def fetch_weather(self):
        '''Fetches the current weather from wxdata.weather.com service.'''
        unit = '' if self.units == 'imperial' or self.units == '' else 'm'
        url = WEATHER_COM_URL % (self.location_code, unit)
        with urlopen(url) as handler:
            try:
                content_type = dict(handler.getheaders())['Content-Type']
                charset = re.search(r'charset=(.*)', content_type).group(1)
            except AttributeError:
                charset = 'utf-8'
            xml = handler.read().decode(charset)
        doc = ElementTree.XML(xml)
        return dict(
            current_conditions=dict(
                text=doc.findtext('cc/t'),
                temperature=doc.findtext('cc/tmp'),
                humidity=doc.findtext('cc/hmid'),
                wind=dict(text=doc.findtext('cc/wind/t'), speed=doc.findtext('cc/wind/s'))
                ),
            units=dict(
                temperature=doc.findtext('head/ut'),
                speed=doc.findtext('head/us'),
                ),
            )

    @require(internet)
    def run(self):
        result = self.fetch_weather()
        conditions = result["current_conditions"]
        temperature = conditions["temperature"]
        humidity = conditions["humidity"]
        wind = conditions["wind"]
        units = result["units"]
        color = None
        current_temp = "{t}°{d}".format(t=temperature, d=units["temperature"])
        current_wind = "{t} {s}{d}".format(t=wind["text"], s=wind["speed"], d=units["speed"])

        if self.colorize:
            icon, color = self.color_icons.get(conditions["text"],
                                               self.color_icons["default"])
            current_temp = "{t}°{d} {i}".format(t=temperature,
                                                d=units["temperature"],
                                                i=icon)
            color = color

        self.output = {
            "full_text": self.format.format(
                current_temp=current_temp,
                current_wind=current_wind,
                humidity=humidity,
                ),
            "color": color
        }<commit_msg>Fix PEP8 errors in weather module<commit_after>from i3pystatus import IntervalModule
from i3pystatus.core.util import internet, require

from urllib.request import urlopen
import re
import xml.etree.ElementTree as ElementTree

WEATHER_COM_URL = 'http://wxdata.weather.com/wxdata/weather/local/%s?unit=%s&cc=*'


class Weather(IntervalModule):
    """
    This module gets the weather from weather.com.
    First, you need to get the code for the location from the www.weather.com

    .. rubric:: Available formatters

    * {current_temp}
    * {current_wind}
    * {humidity}

    """

    interval = 20

    settings = (
        "location_code",
        ("colorize", "Enable color with temperature and UTF-8 icons."),
        ("units", "Celsius (metric) or Fahrenheit (imperial)"),
        "format",
    )
    required = ("location_code",)

    location_code = None
    units = "metric"
    format = "{current_temp}"
    colorize = False
    color_icons = {
        "Fair": (u"\u2600", "#FFCC00"),
        "Cloudy": (u"\u2601", "#F8F8FF"),
        "Partly Cloudy": (u"\u2601", "#F8F8FF"),  # \u26c5 is not in many fonts
        "Rainy": (u"\u2614", "#CBD2C0"),
        "Sunny": (u"\u263C", "#FFFF00"),
        "Snow": (u"\u2603", "#FFFFFF"),
        "default": ("", None),
    }

    def fetch_weather(self):
        '''Fetches the current weather from wxdata.weather.com service.'''
        unit = '' if self.units == 'imperial' or self.units == '' else 'm'
        url = WEATHER_COM_URL % (self.location_code, unit)
        with urlopen(url) as handler:
            try:
                content_type = dict(handler.getheaders())['Content-Type']
                charset = re.search(r'charset=(.*)', content_type).group(1)
            except AttributeError:
                charset = 'utf-8'
            xml = handler.read().decode(charset)
        doc = ElementTree.XML(xml)
        return dict(
            current_conditions=dict(
                text=doc.findtext('cc/t'),
                temperature=doc.findtext('cc/tmp'),
                humidity=doc.findtext('cc/hmid'),
                wind=dict(
                    text=doc.findtext('cc/wind/t'),
                    speed=doc.findtext('cc/wind/s'),
                ),
            ),
            units=dict(
                temperature=doc.findtext('head/ut'),
                speed=doc.findtext('head/us'),
            ),
        )

    @require(internet)
    def run(self):
        result = self.fetch_weather()
        conditions = result["current_conditions"]
        temperature = conditions["temperature"]
        humidity = conditions["humidity"]
        wind = conditions["wind"]
        units = result["units"]
        color = None
        current_temp = "{t}°{d}".format(t=temperature, d=units["temperature"])
        current_wind = "{t} {s}{d}".format(t=wind["text"], s=wind["speed"], d=units["speed"])

        if self.colorize:
            icon, color = self.color_icons.get(conditions["text"],
                                               self.color_icons["default"])
            current_temp = "{t}°{d} {i}".format(t=temperature,
                                                d=units["temperature"],
                                                i=icon)
            color = color

        self.output = {
            "full_text": self.format.format(
                current_temp=current_temp,
                current_wind=current_wind,
                humidity=humidity,
            ),
            "color": color
        }
{'fraction_non_alphanumeric': 0.1410274859692042, 'fraction_numerical': 0.012519787019715067, 'mean_word_length': 1.9140461215932913, 'pattern_counts': {'":': 18, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2091571', 'n_tokens_mistral': 2112, 'n_tokens_neox': 1977, 'n_words': 410}