<commit_before>import os
import queue
import struct
import asyncio
import logging
import collections
from enum import Enum

logger = logging.getLogger(__name__)


EVENT_FORMAT = 'llHHi'
EVENT_SIZE   = struct.calcsize(EVENT_FORMAT)

MSC_PULSELED    = 0x01

class EventType(Enum):
    """
    Types of Events
    """
    #Internal
    PUSH   = 0x01
    ROTATE = 0x02
    MISC   = 0x04
    #API Defined
    STOP   = 0x06
    INT    = 0x08

class Event:
    """
    A Powermate Event

    Parameters
    ---------
    tv_sec : int
        Number of elapsed whole seconds

    tv_usec : int
        Remainder of elapsed time, expressed in microseconds

    type: :class:`EventType`
        Type of event

    code:
        Associated event code

    value: int
        Associated value with event

    """
    def __init__(self, tv_sec, tv_usec, type, code, value):
        self.tv_sec   = tv_sec
        self.tv_usec  = tv_usec
        self.type     = type
        self.code     = code
        self.value    = value


    @property
    def raw(self):
        """
        Converted event information expressed as binary 
        """
        return struct.pack(EVENT_FORMAT, self.tv_sec, self.tv_usec,
                       self.type.value(), self.code, self.value)


    @classmethod
    def stop(cls):
        """
        Return an Event to stop the EventStream

        Returns
        -------
        event : :class:`.LedEvent`
        """
        return cls(0, 0,  EventType.STOP, 0, 0)


    @classmethod
    def from_raw(cls, data):
        """
        Generate an ``Event`` object from packed binary data

        Parameters
        ---------
        data : int
            Binary information to unpack
        
        Returns
        -------
        event : :class:`.Event`
        """
        tv_sec, tv_usec, type, code, value = struct.unpack(EVENT_FORMAT, data)
        return cls(tv_sec, tv_usec, EventType(type), code, value)


    def __repr__(self):
        return '{}({})'.format(self.__class__.__name__,
                               ', '.join('{}={}'.format(k, getattr(self, k))
                               for k in self.__dict__ if not k.startswith('_')))


class LedEvent(Event):
    """
    Implementation of a Powermate event specifically for sending and receiving
    instructions for LED
    
    Parameters
    ----------
    brightness : int

    speed : int

    pulse_type

    asleep

    awake : 
    """
    MAX_BRIGHTNESS  = 255
    MAX_PULSE_SPEED = 255

    def __init__(self, brightness=0, speed=0,
           pulse_type=0, asleep=0, awake=0):
        self.brightness = brightness or self.MAX_BRIGHTNESS
        self.speed      = speed
        self.pulse_type = pulse_type
        self.asleep     = asleep
        self.awake      = awake
        super().__init__(0, 0, EventType.MISC, MSC_PULSELED, 0)


    @property
    def value(self):
        """
        Packed binary instruction for the Powermate LED
        """
        return (self.brightness |
               (self.speed << 8) |
               (self.pulse_type << 17) |
               (self.asleep << 19) |
               (self.awake << 20))


    @classmethod
    def pulse(cls):
        """
        Return an Event to pulse the Powermate

        Returns
        -------
        event : :class:`.LedEvent`
        """
        return cls(speed=self.MAX_PULSE_SPEED, pulse_type=2, asleep=1, awake=1)


    @classmethod
    def max(cls):
        """
        Return an Event to set the Powermate to the maximum brightness

        Returns
        -------
        event : :class:`.LedEvent`
        """
        return cls(brightness=self.MAX_BRIGHTNESS)


    @classmethod
    def off(cls):
        """
        Return an event to turn off the Powermate LED

        Returns
        -------
        event : :class:`.LedEvent`
        """
        return cls(brightness=0)


    @classmethod
    def percent(cls, percent):
        """
        Return an event to set the Powermate to a percentage of its maximum
        brightness

        Parameters
        ----------
        percent : float
            Desired perecentage of maximum brightness

        Returns
        -------
        event : :class:`.LedEvent`
        """
        return cls(brightness=int(percent/100. * self.MAX_BRIGHTNESS))


class EventStream:
    """
    Event Stream from the Powermate

    The EventStream provides a generator object that receivies and sends data
    from the Powermate.

    Parameters
    ----------
    path : str
        Filepath to Powermate USB
    """
    _event_size = EVENT_SIZE
<commit_msg>round instead of int on LedEvent.percentage<commit_after>import os
import queue
import struct
import asyncio
import logging
import collections
from enum import Enum

logger = logging.getLogger(__name__)


EVENT_FORMAT = 'llHHi'
EVENT_SIZE   = struct.calcsize(EVENT_FORMAT)

MSC_PULSELED    = 0x01

class EventType(Enum):
    """
    Types of Events
    """
    #Internal
    PUSH   = 0x01
    ROTATE = 0x02
    MISC   = 0x04
    #API Defined
    STOP   = 0x06
    INT    = 0x08

class Event:
    """
    A Powermate Event

    Parameters
    ---------
    tv_sec : int
        Number of elapsed whole seconds

    tv_usec : int
        Remainder of elapsed time, expressed in microseconds

    type: :class:`EventType`
        Type of event

    code:
        Associated event code

    value: int
        Associated value with event

    """
    def __init__(self, tv_sec, tv_usec, type, code, value):
        self.tv_sec   = tv_sec
        self.tv_usec  = tv_usec
        self.type     = type
        self.code     = code
        self.value    = value


    @property
    def raw(self):
        """
        Converted event information expressed as binary 
        """
        return struct.pack(EVENT_FORMAT, self.tv_sec, self.tv_usec,
                       self.type.value(), self.code, self.value)


    @classmethod
    def stop(cls):
        """
        Return an Event to stop the EventStream

        Returns
        -------
        event : :class:`.Event`
        """
        return cls(0, 0,  EventType.STOP, 0, 0)


    @classmethod
    def from_raw(cls, data):
        """
        Generate an ``Event`` object from packed binary data

        Parameters
        ---------
        data : int
            Binary information to unpack
        
        Returns
        -------
        event : :class:`.Event`
        """
        tv_sec, tv_usec, type, code, value = struct.unpack(EVENT_FORMAT, data)
        return cls(tv_sec, tv_usec, EventType(type), code, value)


    def __repr__(self):
        return '{}({})'.format(self.__class__.__name__,
                               ', '.join('{}={}'.format(k, getattr(self, k))
                               for k in self.__dict__ if not k.startswith('_')))


class LedEvent(Event):
    """
    Implementation of a Powermate event specifically for sending and receiving
    instructions for LED
    
    Parameters
    ----------
    brightness : int

    speed : int

    pulse_type

    asleep

    awake : 
    """
    MAX_BRIGHTNESS  = 255
    MAX_PULSE_SPEED = 255

    def __init__(self, brightness=0, speed=0,
           pulse_type=0, asleep=0, awake=0):
        self.brightness = brightness or self.MAX_BRIGHTNESS
        self.speed      = speed
        self.pulse_type = pulse_type
        self.asleep     = asleep
        self.awake      = awake
        super().__init__(0, 0, EventType.MISC, MSC_PULSELED, 0)


    @property
    def value(self):
        """
        Packed binary instruction for the Powermate LED
        """
        return (self.brightness |
               (self.speed << 8) |
               (self.pulse_type << 17) |
               (self.asleep << 19) |
               (self.awake << 20))


    @classmethod
    def pulse(cls):
        """
        Return an Event to pulse the Powermate

        Returns
        -------
        event : :class:`.LedEvent`
        """
        return cls(speed=self.MAX_PULSE_SPEED, pulse_type=2, asleep=1, awake=1)


    @classmethod
    def max(cls):
        """
        Return an Event to set the Powermate to the maximum brightness

        Returns
        -------
        event : :class:`.LedEvent`
        """
        return cls(brightness=self.MAX_BRIGHTNESS)


    @classmethod
    def off(cls):
        """
        Return an event to turn off the Powermate LED

        Returns
        -------
        event : :class:`.LedEvent`
        """
        return cls(brightness=0)


    @classmethod
    def percent(cls, percent):
        """
        Return an event to set the Powermate to a percentage of its maximum
        brightness

        Parameters
        ----------
        percent : float
            Desired perecentage of maximum brightness

        Returns
        -------
        event : :class:`.LedEvent`
        """
        return cls(brightness=round(percent/100. * self.MAX_BRIGHTNESS))


class EventStream:
    """
    Event Stream from the Powermate

    The EventStream provides a generator object that receivies and sends data
    from the Powermate.

    Parameters
    ----------
    path : str
        Filepath to Powermate USB
    """
    _event_size = EVENT_SIZE

{'fraction_non_alphanumeric': 0.10318582146352072, 'fraction_numerical': 0.01087311079699902, 'mean_word_length': 1.6576134065299046, 'pattern_counts': {'":': 0, '<': 19, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4892783', 'n_tokens_mistral': 2810, 'n_tokens_neox': 2633, 'n_words': 739}