<commit_before>from collections import defaultdict
from string import Formatter
import re

from i3pystatus import IntervalModule
from i3pystatus.core.color import ColorRangeModule

try:
    from natsort import natsorted as sorted
except ImportError:
    pass


class CpuUsage(IntervalModule, ColorRangeModule):
    """
    Shows CPU usage.
    The first output will be inacurate.

    Linux only
    Requires the PyPI package 'colour'.

    .. rubric:: Available formatters

    * `{usage}`      — usage average of all cores
    * `{usage_cpu*}` — usage of one specific core. replace "*" by core number starting at 0
    * `{usage_all}`  — usage of all cores separate. usess natsort when available(relevant for more than 10 cores)

    """

    format = "{usage:02}%"
    format_all = "{core}:{usage:02}%"
    exclude_average = False
    interval = 1
    color = '#FFFFFF'
    dynamic_color = False
    upper_limit = 100
    settings = (
        ("format", "format string."),
        ("format_all", ("format string used for {usage_all} per core. "
                        "Available formaters are {core} and {usage}. ")),
        ("exclude_average", ("If True usage average of all cores will "
                             "not be in format_all.")),
        ("color", "HTML color code #RRGGBB"),
        ("dynamic_color", "Set color dynamically based on CPU usage. Note: this overrides color_up"),
        ("start_color", "Hex or English name for start of color range, eg '#00FF00' or 'green'"),
        ("end_color", "Hex or English name for end of color range, eg '#FF0000' or 'red'")
    )

    def init(self):
        self.prev_total = defaultdict(int)
        self.prev_busy = defaultdict(int)
        self.formatter = Formatter()

        self.key = re.findall('usage_cpu\d+', self.format)
        if len(self.key) == 1:
            self.key = self.key[0]
        else:
            self.key = 'usage_cpu'

        if not self.dynamic_color:
            self.start_color = self.color
            self.end_color = self.color
        self.colors = self.get_hex_color_range(self.start_color, self.end_color, int(self.upper_limit))

    def get_cpu_timings(self):
        """
        reads and parses /proc/stat
        returns dictionary with all available cores including global average
        """
        timings = {}
        with open('/proc/stat', 'r') as file_obj:
            for line in file_obj:
                if 'cpu' in line:
                    line = line.strip().split()
                    timings[line[0]] = [int(x) for x in line[1:]]<commit_msg>use raw string (#682)<commit_after>from collections import defaultdict
from string import Formatter
import re

from i3pystatus import IntervalModule
from i3pystatus.core.color import ColorRangeModule

try:
    from natsort import natsorted as sorted
except ImportError:
    pass


class CpuUsage(IntervalModule, ColorRangeModule):
    """
    Shows CPU usage.
    The first output will be inacurate.

    Linux only
    Requires the PyPI package 'colour'.

    .. rubric:: Available formatters

    * `{usage}`      — usage average of all cores
    * `{usage_cpu*}` — usage of one specific core. replace "*" by core number starting at 0
    * `{usage_all}`  — usage of all cores separate. usess natsort when available(relevant for more than 10 cores)

    """

    format = "{usage:02}%"
    format_all = "{core}:{usage:02}%"
    exclude_average = False
    interval = 1
    color = '#FFFFFF'
    dynamic_color = False
    upper_limit = 100
    settings = (
        ("format", "format string."),
        ("format_all", ("format string used for {usage_all} per core. "
                        "Available formaters are {core} and {usage}. ")),
        ("exclude_average", ("If True usage average of all cores will "
                             "not be in format_all.")),
        ("color", "HTML color code #RRGGBB"),
        ("dynamic_color", "Set color dynamically based on CPU usage. Note: this overrides color_up"),
        ("start_color", "Hex or English name for start of color range, eg '#00FF00' or 'green'"),
        ("end_color", "Hex or English name for end of color range, eg '#FF0000' or 'red'")
    )

    def init(self):
        self.prev_total = defaultdict(int)
        self.prev_busy = defaultdict(int)
        self.formatter = Formatter()

        self.key = re.findall(r'usage_cpu\d+', self.format)
        if len(self.key) == 1:
            self.key = self.key[0]
        else:
            self.key = 'usage_cpu'

        if not self.dynamic_color:
            self.start_color = self.color
            self.end_color = self.color
        self.colors = self.get_hex_color_range(self.start_color, self.end_color, int(self.upper_limit))

    def get_cpu_timings(self):
        """
        reads and parses /proc/stat
        returns dictionary with all available cores including global average
        """
        timings = {}
        with open('/proc/stat', 'r') as file_obj:
            for line in file_obj:
                if 'cpu' in line:
                    line = line.strip().split()
                    timings[line[0]] = [int(x) for x in line[1:]]
{'fraction_non_alphanumeric': 0.10834468002334176, 'fraction_numerical': 0.010309278350515464, 'mean_word_length': 2.338961038961039, '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': '2096134', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1468, 'n_words': 490}