<commit_before>#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# =============================================================================
# title           : magicbluelib.py
# description     : Python library to control Magic Blue bulbs over Bluetooth
# author          : <NAME>
# date            : 23/11/2015
# python_version  : 3.4
# =============================================================================
import functools
import logging
import random
from datetime import datetime, date, time
from enum import Enum

from bluepy import btle


__all__ = ['MagicBlue', 'Effect']


logger = logging.getLogger(__name__)


UUID_CHARACTERISTIC_RECV = btle.UUID('ffe4')
UUID_CHARACTERISTIC_WRITE = btle.UUID('ffe9')
UUID_CHARACTERISTIC_DEVICE_NAME = btle.UUID('2a00')


def connection_required(func):
    """Raise an exception before calling the actual function if the device is
    not connection.
    """
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
        if self._connection is None:
            raise Exception("Not connected")

        return func(self, *args, **kwargs)

    return wrapper


def _figure_addr_type(mac_address=None, version=None, addr_type=None):
    # addr_type rules all
    if addr_type is not None:
        return addr_type

    # prefer version
    if version == 9 or version == 10:
        return btle.ADDR_TYPE_PUBLIC

    # try using mac_address
    if mac_address is not None:
        mac_address_num = int(mac_address.replace(':', ''), 16)
        if mac_address_num & 0xF00000000000 == 0xF00000000000:
            return btle.ADDR_TYPE_PUBLIC

    return btle.ADDR_TYPE_RANDOM


class Effect(Enum):
    """
    Effect
    """
    seven_color_cross_fade = 0x25
    red_gradual_change = 0x26
    green_gradual_change = 0x27
    blue_gradual_change = 0x28
    yellow_gradual_change = 0x29
    cyan_gradual_change = 0x2a
    purple_gradual_change = 0x2b
    white_gradual_change = 0x2c
    red_green_cross_fade = 0x2d
    red_blue_cross_fade = 0x2e
    green_blue_cross_fade = 0x2f
    seven_color_stobe_flash = 0x30
    red_strobe_flash = 0x31
    green_strobe_flash = 0x32<commit_msg>Improved compatibility with v8<commit_after>#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# =============================================================================
# title           : magicbluelib.py
# description     : Python library to control Magic Blue bulbs over Bluetooth
# author          : <NAME>
# date            : 23/11/2015
# python_version  : 3.4
# =============================================================================
import functools
import logging
import random
from datetime import datetime, date, time
from enum import Enum

from bluepy import btle


__all__ = ['MagicBlue', 'Effect']


logger = logging.getLogger(__name__)


UUID_CHARACTERISTIC_RECV = btle.UUID('ffe4')
UUID_CHARACTERISTIC_WRITE = btle.UUID('ffe9')
UUID_CHARACTERISTIC_DEVICE_NAME = btle.UUID('2a00')


def connection_required(func):
    """Raise an exception before calling the actual function if the device is
    not connection.
    """
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
        if self._connection is None:
            raise Exception("Not connected")

        return func(self, *args, **kwargs)

    return wrapper


def _figure_addr_type(mac_address=None, version=None, addr_type=None):
    # addr_type rules all
    if addr_type is not None:
        return addr_type

    # prefer version
    if version == 9 or version == 10:
        return btle.ADDR_TYPE_PUBLIC

    if version == 8:
        return btle.ADDR_TYPE_RANDOM
    
    # try using mac_address
    if mac_address is not None:
        mac_address_num = int(mac_address.replace(':', ''), 16)
        if mac_address_num & 0xF00000000000 == 0xF00000000000:
            return btle.ADDR_TYPE_PUBLIC

    return btle.ADDR_TYPE_RANDOM


class Effect(Enum):
    """
    Effect
    """
    seven_color_cross_fade = 0x25
    red_gradual_change = 0x26
    green_gradual_change = 0x27
    blue_gradual_change = 0x28
    yellow_gradual_change = 0x29
    cyan_gradual_change = 0x2a
    purple_gradual_change = 0x2b
    white_gradual_change = 0x2c
    red_green_cross_fade = 0x2d
    red_blue_cross_fade = 0x2e
    green_blue_cross_fade = 0x2f
    seven_color_stobe_flash = 0x30
    red_strobe_flash = 0x31
    green_strobe_flash = 0x32
{'fraction_non_alphanumeric': 0.14612918002748512, 'fraction_numerical': 0.03756298671552909, 'mean_word_length': 3.456122448979592, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '1520182', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1355, 'n_words': 329}