<commit_before>from constants import *
from lemongadget import LemonGadget
import psutil
import time
import os

NETWORK_TEST_DELAY = 0.0001
BATTERY_TEST_DELAY = 0.001


def cpu_percentage():
    return psutil.cpu_percent()

def memory_percentage():
    return psutil.virtual_memory().percent


def network_up_down():
    net = psutil.net_io_counters()
    time.sleep(NETWORK_TEST_DELAY)
    net2 = psutil.net_io_counters()

    up = (net2.bytes_sent - net.bytes_sent) / NETWORK_TEST_DELAY
    down = (net2.bytes_recv - net.bytes_recv) / NETWORK_TEST_DELAY
    return up, down


def network_units(bytes_moved):
    if bytes_moved > (1 << 30):
        return bytes_moved, (bytes_moved / (1 << 30)), "GiB"
    elif bytes_moved > (1 << 20):
        return bytes_moved, (bytes_moved / (1 << 20)), "MiB"
    elif bytes_moved > (1 << 10):
        return bytes_moved, (bytes_moved / (1 << 10)), "KiB"
    else:
        return bytes_moved, bytes_moved, "bytes"


def network_units_kb(bytes_moved):
    if bytes_moved > (1 << 30):
        return bytes_moved, (bytes_moved / (1 << 30)), "GiB"
    elif bytes_moved > (1 << 20):
        return bytes_moved, (bytes_moved / (1 << 20)), "MiB"
    else:
        return bytes_moved, (bytes_moved / (1 << 10)), "KiB"


def network_units_mb(bytes_moved):
    if bytes_moved > (1 << 30):
        return bytes_moved, (bytes_moved / (1 << 30)), "GiB"
    else:
        return bytes_moved, (bytes_moved / (1 << 20)), "MiB"


def get_directories(directory):
    if not os.path.exists(directory):
        return []

    gen = os.walk(directory)
    if hasattr(gen, "next"):
        return gen.next()[1]
    else:
        return gen.__next__()[1]


def is_charging():
    devices = get_directories("/sys/bus/acpi/drivers/ac")
    if not devices:
        return False

    interface = os.path.join("/sys/bus/acpi/drivers/ac", devices[0], "power_supply/AC/online")
    with open(interface, 'r') as fh:
        return online == "1\n"


def get_battery_stats(interface):
    with open(interface, 'r') as fh:
        stats = {}
        for line in fh.readlines():
            key, value = line.split("=")
            if value.isdigit():
                value = int(value)
            stats[key] = value

    return stats


def battery_percentage(battery):
    devices = get_directories("/sys/bus/acpi/drivers/battery")
    if not devices:
        return False

    interface = os.path.join("/sys/bus/acpi/drivers/battery", devices[0], "power_supply", battery, "uevent")
    stats = get_battery_stats(interface)
    return stats["POWER_SUPPLY_ENERGY_NOW"] / stats["POWER_SUPPLY_ENERGY_FULL"]


def formatted_time():
    return time.strftime("%A, %b %d %Y %l:%M:%S %p")


class CPUGadget(LemonGadget):
    def update(self):
        self.append_light_separator()
        perc = int(cpu_percentage())

        lastfg = self._lastfg
        if perc >= CPU_PERC_ALERT:
            self.add_format(fg=ALERT_COLOR)<commit_msg>Fix int detection.<commit_after>from constants import *
from lemongadget import LemonGadget
import psutil
import time
import os

NETWORK_TEST_DELAY = 0.0001
BATTERY_TEST_DELAY = 0.001


def cpu_percentage():
    return psutil.cpu_percent()

def memory_percentage():
    return psutil.virtual_memory().percent


def network_up_down():
    net = psutil.net_io_counters()
    time.sleep(NETWORK_TEST_DELAY)
    net2 = psutil.net_io_counters()

    up = (net2.bytes_sent - net.bytes_sent) / NETWORK_TEST_DELAY
    down = (net2.bytes_recv - net.bytes_recv) / NETWORK_TEST_DELAY
    return up, down


def network_units(bytes_moved):
    if bytes_moved > (1 << 30):
        return bytes_moved, (bytes_moved / (1 << 30)), "GiB"
    elif bytes_moved > (1 << 20):
        return bytes_moved, (bytes_moved / (1 << 20)), "MiB"
    elif bytes_moved > (1 << 10):
        return bytes_moved, (bytes_moved / (1 << 10)), "KiB"
    else:
        return bytes_moved, bytes_moved, "bytes"


def network_units_kb(bytes_moved):
    if bytes_moved > (1 << 30):
        return bytes_moved, (bytes_moved / (1 << 30)), "GiB"
    elif bytes_moved > (1 << 20):
        return bytes_moved, (bytes_moved / (1 << 20)), "MiB"
    else:
        return bytes_moved, (bytes_moved / (1 << 10)), "KiB"


def network_units_mb(bytes_moved):
    if bytes_moved > (1 << 30):
        return bytes_moved, (bytes_moved / (1 << 30)), "GiB"
    else:
        return bytes_moved, (bytes_moved / (1 << 20)), "MiB"


def get_directories(directory):
    if not os.path.exists(directory):
        return []

    gen = os.walk(directory)
    if hasattr(gen, "next"):
        return gen.next()[1]
    else:
        return gen.__next__()[1]


def is_charging():
    devices = get_directories("/sys/bus/acpi/drivers/ac")
    if not devices:
        return False

    interface = os.path.join("/sys/bus/acpi/drivers/ac", devices[0], "power_supply/AC/online")
    with open(interface, 'r') as fh:
        return online == "1\n"


def get_battery_stats(interface):
    with open(interface, 'r') as fh:
        stats = {}
        for line in fh.readlines():
            key, value = line.split("=")
            value = value.rstrip()
            if value.isdigit():
                value = int(value)
            stats[key] = value

    return stats


def battery_percentage(battery):
    devices = get_directories("/sys/bus/acpi/drivers/battery")
    if not devices:
        return False

    interface = os.path.join("/sys/bus/acpi/drivers/battery", devices[0], "power_supply", battery, "uevent")
    stats = get_battery_stats(interface)
    return stats["POWER_SUPPLY_ENERGY_NOW"] / stats["POWER_SUPPLY_ENERGY_FULL"]


def formatted_time():
    return time.strftime("%A, %b %d %Y %l:%M:%S %p")


class CPUGadget(LemonGadget):
    def update(self):
        self.append_light_separator()
        perc = int(cpu_percentage())

        lastfg = self._lastfg
        if perc >= CPU_PERC_ALERT:
            self.add_format(fg=ALERT_COLOR)
{'fraction_non_alphanumeric': 0.1327807848443843, 'fraction_numerical': 0.019959404600811907, 'mean_word_length': 2.90554821664465, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 17, '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': '3727660', 'n_tokens_mistral': 2202, 'n_tokens_neox': 2072, 'n_words': 463}