"""
Module for controlling/interface with Bluetooth devices
"""

import asyncio
import itertools
import logging
import re
from syslog import syslog, LOG_ERR, LOG_INFO
from typing import Any, Optional, List, Dict
from dbus_fast import Message, MessageType, Variant
from dbus_fast.aio.proxy_object import ProxyInterface
import falcon
from summit_rcm.dbus_manager import DBusManager
from summit_rcm_bluetooth.services import bt_plugin
from summit_rcm import definition
from summit_rcm_bluetooth.services.ble import (
    DBUS_PROP_IFACE,
    controller_pretty_name,
    find_device,
    DEVICE_IFACE,
    ADAPTER_IFACE,
    DBUS_OM_IFACE,
    AgentSingleton,
    BLUEZ_SERVICE_NAME,
    uri_to_uuid,
    GATT_MANAGER_IFACE,
    create_agent_singleton,
)
from summit_rcm_bluetooth.services.bt_controller_state import BluetoothControllerState
from summit_rcm.utils import Singleton, variant_to_python

PAIR_TIMEOUT_SECONDS = 60
CONNECT_TIMEOUT_SECONDS = 60

# These device properties can be directly set, without requiring any special-case logic.
SETTABLE_DEVICE_PROPS = [
    ("Trusted", bool, "b"),
    ("AutoConnect", bool, "b"),
    ("AutoConnectAutoDisable", bool, "b"),
]

CACHED_DEVICE_PROPS = ["connected", "autoConnect", "autoConnectAutoDisable"]

# These controller properties can be directly set, without requiring any special-case logic.
PASS_ADAPTER_PROPS = ["Discovering", "Powered", "Discoverable"]

# Additionally transportFilter is cached, but by special-case logic that confirms
# value is accepted.
CACHED_ADAPTER_PROPS = ["discovering", "powered", "discoverable"]

ADAPTER_PATH_PATTERN = re.compile("^/org/bluez/hci\\d+$")
DEVICE_PATH_PATTERN = re.compile("^/org/bluez/hci\\d+/dev_\\w+$")
DEVICE_ADAPTER_GROUP_PATTERN = re.compile("^(/org/bluez/hci\\d+)/dev_\\w+$")

bluetooth_plugins: List[bt_plugin.BluetoothPlugin] = []

try:
    from summit_rcm_bluetooth.hid.barcode_scanner import HidBarcodeScannerPlugin

    hid_barcode_scanner_plugin = HidBarcodeScannerPlugin()
    if hid_barcode_scanner_plugin not in bluetooth_plugins:
        bluetooth_plugins.append(hid_barcode_scanner_plugin)
    syslog("bluetooth: HidBarcodeScannerPlugin loaded")
except ImportError:
    syslog("bluetooth: HidBarcodeScannerPlugin NOT loaded")

try:
    from summit_rcm_bluetooth.vsp.vsp_connection import VspConnectionPlugin

    vsp_connection_plugin = VspConnectionPlugin()
    if vsp_connection_plugin not in bluetooth_plugins:
        bluetooth_plugins.append(vsp_connection_plugin)
    syslog("bluetooth: VspConnectionPlugin loaded")
except ImportError:
    syslog("bluetooth: VspConnectionPlugin NOT loaded")

try:
    bluetooth_ble_plugin = None
    from summit_rcm_bluetooth.services.bt_ble import BluetoothBlePlugin

    bluetooth_ble_plugin = BluetoothBlePlugin()
    if bluetooth_ble_plugin not in bluetooth_plugins:
        bluetooth_plugins.append(bluetooth_ble_plugin)
    syslog("bluetooth: BluetoothBlePlugin loaded")
except ImportError:
    syslog("bluetooth: BluetoothBlePlugin NOT loaded")

async def get_controller_obj(controller: str = ""):
    result = {}
    # get the system bus
    bus = await DBusManager().get_bus()
    # get the ble controller
    if not controller:
        result[
            "InfoMsg"
        ] = f"Controller {controller_pretty_name(controller)} not found."
        result["SDCERR"] = definition.SUMMIT_RCM_ERRORS.get("SDCERR_FAIL", 1)
        controller_obj = None
    else:
        controller_obj = bus.get_proxy_object(
            BLUEZ_SERVICE_NAME,
            controller,
            await bus.introspect(BLUEZ_SERVICE_NAME, controller),
        )

    return bus, controller_obj, result

def lower_camel_case(upper_camel_string: str):
    """Return supplied UpperCamelCase string in lowerCamelCase"""
    return upper_camel_string[:1].lower() + upper_camel_string[1:]

class Bluetooth(metaclass=Singleton):
    def __init__(self):
        self._controller_states: Dict[str, BluetoothControllerState] = {}
        """Controller state tracking - indexed by friendly (REST API) name"""
        self._controller_addresses: Dict[str, str] = {}
        """Controller addresses - indexed by friendly (REST API) name"""
        self._controller_callbacks_registered = False
        self._logger = logging.getLogger(__name__)
        self._devices_to_restore: Dict[str, type(None)] = {}
        """Map of device uuids to restore state due to associated controller reset"""
        self.setup_initiated: bool = False

    async def setup(self, app: falcon.asgi.App) -> None:
        """
        Run async logic required to setup Bluetooth intergration
        """
        if not self.setup_initiated:
            self.setup_initiated = True
            for plugin in bluetooth_plugins:
                if isinstance(plugin, BluetoothBlePlugin):
                    plugin.set_app(app)
                    break
            await self.discover_controllers()

    def add_ws_route(self, ws_route: str, is_legacy: bool = False):
        """Add a websocket route to the list of supported routes"""
        for plugin in bluetooth_plugins:
            if isinstance(plugin, BluetoothBlePlugin):
                plugin.add_ws_route(ws_route, is_legacy)
                return

    @property
    def device_commands(self) -> List[str]:
        return list(
            itertools.chain.from_iterable(
                plugin.device_commands for plugin in bluetooth_plugins
            )
        ) + ["getConnInfo"]

    @property
    def adapter_commands(self) -> List[str]:
        return list(
            itertools.chain.from_iterable(
                plugin.adapter_commands for plugin in bluetooth_plugins
            )
        )

    @property
    def controller_addresses(self) -> Dict[str, str]:
        return self._controller_addresses

    async def get_remapped_controller(self, controller_friendly_name: str):
        """Scan present controllers and find the controller with address associated with
        controller_friendly_name, in _controller_addresses dictionary.
        This allows for consistent referencing of controller by REST API name in the event
        the controller bluez object path changes in the system. (e.g. /org/bluez/hci5)
        """
        if controller_friendly_name not in self._controller_addresses.keys():
            return None
        address = self._controller_addresses[controller_friendly_name]
        bus = await DBusManager().get_bus()
        remote_om = bus.get_proxy_object(
            BLUEZ_SERVICE_NAME, "/", await bus.introspect(BLUEZ_SERVICE_NAME, "/")
        ).get_interface(DBUS_OM_IFACE)
        objects = await remote_om.call_get_managed_objects()

        for controller, props in objects.items():
            if GATT_MANAGER_IFACE in props.keys():
                if (
                    ADAPTER_IFACE in props.keys()
                    and "Address" in props[ADAPTER_IFACE].keys()
                ):
                    if address == variant_to_python(props[ADAPTER_IFACE]["Address"]):
                        return controller
        return None

    async def remapped_controller_to_friendly_name(self, controller: str) -> str:
        """Lookup the REST API name of the controller with the address matching the provided
        controller by dbus path.
        """
        bus = await DBusManager().get_bus()
        controller_obj = bus.get_proxy_object(
            BLUEZ_SERVICE_NAME,
            controller,
            await bus.introspect(BLUEZ_SERVICE_NAME, controller),
        )

        if not controller_obj:
            return ""
        adapter_interface = controller_obj.get_interface(ADAPTER_IFACE)
        requested_address = variant_to_python(await adapter_interface.get_address())
        for controller_friendly_name, address in self._controller_addresses.items():
            if requested_address == address:
                return controller_friendly_name
        return ""

    async def discover_controllers(self, renumber=True):
        """
        Find objects that have the bluez service and a GattManager1 interface,
        building _controller_addresses dictionary to later allow referencing of
        controllers by fixed name, even if their dbus object path changes.
        The assumption is that all controllers will be present in the system
        with the names the REST API wants to expose them under when Summit RCM starts,
        and that Summit RCM will never restart.
        If these assumptions are not true, renumber can be set to simply number
        discovered controllers as controller0 and up.
        """
        bus = await DBusManager().get_bus()
        remote_om = bus.get_proxy_object(
            BLUEZ_SERVICE_NAME, "/", await bus.introspect(BLUEZ_SERVICE_NAME, "/")
        ).get_interface(DBUS_OM_IFACE)
        objects = await remote_om.call_get_managed_objects()
        controller_number = len(self._controller_addresses.keys())

        for controller, props in objects.items():
            if GATT_MANAGER_IFACE in props.keys():
                if renumber:
                    controller_friendly_name: str = f"controller{controller_number}"
                else:
                    controller_friendly_name: str = controller_pretty_name(controller)
                if (
                    ADAPTER_IFACE in props.keys()
                    and "Address" in props[ADAPTER_IFACE].keys()
                ):
                    address = variant_to_python(props[ADAPTER_IFACE]["Address"])
                    if address not in self._controller_addresses.values():
                        self._controller_addresses[controller_friendly_name] = address
                        syslog(
                            LOG_INFO,
                            f"assigning controller {controller} at address {address} "
                            f"to REST API name {controller_friendly_name}",
                        )
                        controller_number += 1

    async def register_controller_callbacks(self):
        if not self._controller_callbacks_registered:
            try:
                self._controller_callbacks_registered = True

                bus = await DBusManager().get_bus()
                interface = bus.get_proxy_object(
                    BLUEZ_SERVICE_NAME,
                    "/",
                    await bus.introspect(BLUEZ_SERVICE_NAME, "/"),
                ).get_interface(DBUS_OM_IFACE)
                interface.on_interfaces_removed(self.interface_removed_cb)
                interface.on_interfaces_added(self.interface_added_cb)
            except Exception as exception:
                self.log_exception(exception)

    async def interface_added_cb(self, interface: str, *args):
        try:
            if ADAPTER_PATH_PATTERN.match(interface):
                syslog(LOG_INFO, f"Bluetooth interface added: {str(interface)}")
                # IF bluetoothd crashed, may want to AgentSingleton.clear_instance()
                # For now, assume the controller was previously removed and has been re-attached.
                await self.controller_restore(interface)
            elif DEVICE_PATH_PATTERN.match(interface):
                syslog(LOG_INFO, f"Bluetooth device added: {str(interface)}")
                await self.device_restore(interface)
        except Exception as exception:
            self.log_exception(exception)

    def log_exception(self, exception, message: str = ""):
        self._logger.exception(exception)
        syslog(LOG_ERR, message + str(exception))

    async def interface_removed_cb(self, interface: str, *args):
        try:
            if ADAPTER_PATH_PATTERN.match(interface):
                syslog(LOG_INFO, f"Bluetooth interface removed: {str(interface)}")
                _, adapter_obj, _ = await get_controller_obj(interface)
                for plugin in bluetooth_plugins:
                    try:
                        await plugin.ControllerRemovedNotify(interface, adapter_obj)
                    except Exception as exception:
                        self.log_exception(exception)
        except Exception as exception:
            self.log_exception(exception)

    async def controller_restore(self, controller: str = "/org/bluez/hci0"):
        """
        :param controller: controller whose state will be restored
        :return: None

        Call when the specified controller experienced a HW reset, for example, in case
        of a HW malfunction, or system power-save sleep.
        Attempts to re-establish previously commanded controller state.
        * Note the assumption is that this LCM process was used to establish controller state -
        assumption unsatisfied if prior run of LCM or another tool was used to alter controller
        state.
        """
        # we remove the bus path by convention, so the index names match that used by hosts
        # in REST API
        controller_friendly_name: str = await self.remapped_controller_to_friendly_name(
            controller
        )
        syslog(
            LOG_INFO,
            f"controller_restore: restoring controller state for REST API name "
            f"{controller_friendly_name}",
        )
        controller_state = self.get_controller_state(controller_friendly_name)

        _, adapter_obj, _ = await get_controller_obj(controller)

        if not adapter_obj:
            syslog(
                LOG_ERR,
                f"Reset notification received for controller {controller}, "
                "but adapter_obj not found",
            )
            return

        # First, set controller properties, powering it on if previously powered.
        adapter_interface = adapter_obj.get_interface(ADAPTER_IFACE)
        try:
            await self.set_adapter_properties(
                adapter_interface,
                controller,
                controller_state.properties,
            )
        except Exception as exception:
            self.log_exception(exception)

        # Second, schedule set of each device's properties.
        for device_uuid, _ in controller_state.device_properties_uuids.items():
            self._devices_to_restore.update({device_uuid: None})

    async def device_restore(self, device: str):
        """Set device's properties, and plugin protocol connections if applicable."""
        bus, device_obj, _ = await get_controller_obj(device)

        if not device_obj:
            syslog(
                LOG_ERR,
                f"Reset notification received for device {device}, "
                "but device_obj not found",
            )
            return
        device_interface = device_obj.get_interface(DEVICE_IFACE)
        device_uuid = uri_to_uuid(
            variant_to_python(await device_interface.get_address())
        )

        if device_uuid not in self._devices_to_restore.keys():
            return

        self._devices_to_restore.pop(device_uuid)

        match = DEVICE_ADAPTER_GROUP_PATTERN.match(device)
        if not match or match.lastindex < 1:
            syslog(
                LOG_ERR,
                f"device_restore couldn't determine controller of device {device}",
            )
            return
        controller = match[1]
        controller_friendly_name: str = await self.remapped_controller_to_friendly_name(
            controller
        )
        syslog(
            LOG_INFO,
            f"device_restore: restoring device state for {device} on controller "
            f"REST API name {controller_friendly_name}",
        )
        controller_state = self.get_controller_state(controller_friendly_name)
        bus, adapter_obj, _ = await get_controller_obj(controller)
        adapter_interface = adapter_obj.get_interface(ADAPTER_IFACE)

        cached_device_properties = self.get_device_properties(
            controller_state, device_uuid
        )
        device, _ = await find_device(bus, device_uuid)
        if device is not None:
            device_obj = bus.get_proxy_object(
                BLUEZ_SERVICE_NAME,
                device,
                await bus.introspect(BLUEZ_SERVICE_NAME, device),
            )
            device_interface = device_obj.get_interface(DEVICE_IFACE)
            try:
                await self.set_device_properties(
                    adapter_interface,
                    device_interface,
                    device_uuid,
                    cached_device_properties,
                )
            except Exception as exception:
                self.log_exception(exception, "failed setting device properties: ")
        else:
            syslog(
                LOG_ERR, f"***couldn't find device {device_uuid} to restore properties"
            )

        # Notify plugins, re-establishing protocol links.
        # We do not wait for BT connections to restore, so service discovery may not be complete
        # when plugins receive notification.
        for plugin in bluetooth_plugins:
            try:
                await plugin.DeviceAddedNotify(device, device_uuid, device_obj)
            except Exception as exception:
                self.log_exception(exception)

    async def get_device_property(
        self, obj_path: str, interface: str, property_name: str
    ) -> Any:
        """Get the specified interface property for the object at the given path."""
        bus = await DBusManager().get_bus()

        reply = await bus.call(
            Message(
                destination=BLUEZ_SERVICE_NAME,
                path=obj_path,
                interface=DBUS_PROP_IFACE,
                member="Get",
                signature="ss",
                body=[interface, property_name],
            )
        )

        if reply.message_type == MessageType.ERROR:
            raise Exception(reply.body[0])

        return variant_to_python(reply.body[0])

    async def set_device_property(
        self,
        obj_path: str,
        interface: str,
        property_name: str,
        value: Any,
        value_signature: str,
    ) -> None:
        """
        Set the specified interface property for the object at the given path to the defined value
        using the provided signature.
        """
        bus = await DBusManager().get_bus()

        reply = await bus.call(
            Message(
                destination=BLUEZ_SERVICE_NAME,
                path=obj_path,
                interface=DBUS_PROP_IFACE,
                member="Set",
                signature="ssv",
                body=[interface, property_name, Variant(value_signature, value)],
            )
        )

        if reply.message_type == MessageType.ERROR:
            raise Exception(reply.body[0])

    def get_controller_state(
        self, controller_friendly_name: str
    ) -> BluetoothControllerState:
        controller_friendly_name = controller_pretty_name(controller_friendly_name)
        if controller_friendly_name not in self._controller_states:
            self._controller_states[
                controller_friendly_name
            ] = BluetoothControllerState()
        return self._controller_states[controller_friendly_name]

    def get_device_properties(
        self, controller_state: BluetoothControllerState, device_uuid: str
    ):
        """
        :param controller_state: controller device is on
        :param device_uuid: see uri_to_uuid()
        :return: dictionary map of device property names and values
        """
        if device_uuid not in controller_state.device_properties_uuids:
            controller_state.device_properties_uuids[device_uuid] = {}
        return controller_state.device_properties_uuids[device_uuid]

    @staticmethod
    def result_parameter_not_one_of(parameter: str, not_one_of):
        return {
            "SDCERR": definition.SUMMIT_RCM_ERRORS["SDCERR_FAIL"],
            "InfoMsg": f"supplied {parameter} parameter must be one of {not_one_of}",
        }

    async def set_adapter_properties(
        self,
        adapter_interface: ProxyInterface,
        controller_friendly_name,
        post_data,
    ):
        """Set properties on an adapter (controller)"""
        result = {}
        powered = post_data.get("powered", None)
        discovering = post_data.get("discovering", None)
        discoverable = post_data.get("discoverable", None)
        transport_filter = post_data.get("transportFilter", None)
        if powered is not None:
            await adapter_interface.set_powered(bool(powered))
            if not powered:
                # Do not attempt to set discoverable or discovering state if powering off
                discoverable = discoverable if discoverable else None
                discovering = discovering if discovering else None

        if transport_filter is not None:
            result.update(
                await self.set_adapter_transport_filter(
                    adapter_interface, controller_friendly_name, transport_filter
                )
            )
            if (
                "SDCERR" in result
                and result["SDCERR"] != definition.SUMMIT_RCM_ERRORS["SDCERR_SUCCESS"]
            ):
                return result
        if discoverable is not None:
            await adapter_interface.set_discoverable(bool(discoverable))
        if discovering is not None:
            discovering_state = variant_to_python(
                await adapter_interface.get_discovering()
            )
            if discovering_state != discovering:
                if discovering:
                    await adapter_interface.call_start_discovery()
                else:
                    await adapter_interface.call_stop_discovery()

        if "SDCERR" not in result:
            result["SDCERR"] = definition.SUMMIT_RCM_ERRORS["SDCERR_SUCCESS"]

        return result

    def get_adapter_transport_filter(self, controller_friendly_name):
        controller_state = self.get_controller_state(controller_friendly_name)
        return controller_state.properties.get("transportFilter", None)

    async def set_adapter_transport_filter(
        self,
        adapter_interface: ProxyInterface,
        controller_friendly_name,
        transport_filter,
    ):
        """Set a transport filter on the controller.  Note that "When multiple clients call
        SetDiscoveryFilter, their filters are internally merged" """
        result = {}
        discovery_filters = {"Transport": Variant("s", str(transport_filter))}
        try:
            await adapter_interface.call_set_discovery_filter(discovery_filters)
        except Exception:
            result["SDCERR"] = definition.SUMMIT_RCM_ERRORS["SDCERR_FAIL"]
            result["InfoMsg"] = f"Transport filter {transport_filter} not accepted"
            return result

        controller_state = self.get_controller_state(controller_friendly_name)
        controller_state.properties["transportFilter"] = transport_filter
        return result

    async def set_device_properties(
        self,
        adapter_interface: ProxyInterface,
        device_interface: ProxyInterface,
        device_uuid: str,
        post_data,
    ):
        result = {}
        for settable_property in SETTABLE_DEVICE_PROPS:
            prop_name, prop_type, prop_signature = settable_property
            value = post_data.get(lower_camel_case(prop_name), None)
            if value is not None:
                await self.set_device_property(
                    obj_path=device_interface.path,
                    interface=DEVICE_IFACE,
                    property_name=prop_name,
                    value=prop_type(value),
                    value_signature=prop_signature,
                )
        auto_connect = post_data.get("autoConnect", None)
        if auto_connect == 1:
            await create_agent_singleton()
        paired = post_data.get("paired", None)
        if paired == 1:
            paired_state = (
                1 if variant_to_python(await device_interface.get_paired()) else 0
            )
            if paired_state != paired:
                await create_agent_singleton()
                await asyncio.wait_for(
                    device_interface.call_pair(), PAIR_TIMEOUT_SECONDS
                )
        elif paired == 0:
            await self.remove_device_method(adapter_interface, device_interface)
            result["SDCERR"] = definition.SUMMIT_RCM_ERRORS["SDCERR_SUCCESS"]
            return result
        connected = post_data.get("connected", None)
        connected_state = (
            1 if variant_to_python(await device_interface.get_connected()) else 0
        )
        if connected_state != connected:
            if connected == 1:
                # Note - device may need to be paired prior to connecting
                # AgentSingleton can be registered to allow BlueZ to auto-pair (without bonding)
                await create_agent_singleton()
                if bluetooth_ble_plugin:
                    await bluetooth_ble_plugin.initialize()
                if bluetooth_ble_plugin and bluetooth_ble_plugin.bt:
                    # Use ig BLE plugin if available, because it will refuse to
                    # interact with devices it has not explicitly connected to.
                    # Pass device path to ig BLE plugin, as it will fail
                    # to connect to devices it has not previously "discovered".
                    await bluetooth_ble_plugin.bt.connect(
                        device_uuid, device_interface.path
                    )
                else:
                    await asyncio.wait_for(
                        device_interface.call_connect(), CONNECT_TIMEOUT_SECONDS
                    )
            elif connected == 0:
                await device_interface.call_disconnect()
        passkey = post_data.get("passkey", None)
        if passkey is not None:
            agent_instance = await AgentSingleton.get_instance()
            if agent_instance:
                agent_instance.passkeys[device_interface.path] = passkey
        # Found device, set any requested properties.  Assume success.
        result["SDCERR"] = definition.SUMMIT_RCM_ERRORS["SDCERR_SUCCESS"]

        return result

    async def remove_device_method(
        self, adapter_interface: ProxyInterface, device_interface: ProxyInterface
    ):
        device_connected = variant_to_python(await device_interface.get_connected())
        if device_connected:
            await device_interface.call_disconnect()
        device_address = variant_to_python(await device_interface.get_address())
        await adapter_interface.call_remove_device(device_interface.path)
        # If RemoveDevice is successful, further work on device will not be possible.
        device_uuid = uri_to_uuid(device_address)
        for plugin in bluetooth_plugins:
            try:
                await plugin.DeviceRemovedNotify(device_uuid, device_interface)
            except Exception as exception:
                self.log_exception(exception)

    async def execute_device_command(
        self,
        bus,
        command,
        device_uuid: str,
        device_interface: Optional[ProxyInterface],
        adapter_interface: Optional[ProxyInterface],
        post_data: Optional[dict] = None,
    ):
        result = {}
        error_message = None
        processed = False
        if command == "getConnInfo":
            processed = True

            try:
                reply = await bus.call(
                    Message(
                        destination=BLUEZ_SERVICE_NAME,
                        path=device_interface.path,
                        interface=DEVICE_IFACE,
                        member="GetConnInfo",
                    )
                )

                if reply.message_type == MessageType.ERROR:
                    raise Exception(reply.body[0])

                result["rssi"] = reply.body[0]
                result["tx_power"] = reply.body[1]
                result["max_tx_power"] = reply.body[2]
            except Exception as exception:
                self.log_exception(exception)
                error_message = "Unable to get connection info"
        else:
            for plugin in bluetooth_plugins:
                try:
                    processed, error_message = await plugin.ProcessDeviceCommand(
                        bus,
                        command,
                        device_uuid,
                        device_interface,
                        adapter_interface,
                        post_data,
                        self.remove_device_method,
                    )
                except Exception as exception:
                    self.log_exception(exception)
                    processed = True
                    error_message = f"Command {command} failed with {str(exception)}"
                    break
                if processed:
                    break

        if not processed:
            result["SDCERR"] = definition.SUMMIT_RCM_ERRORS["SDCERR_FAIL"]
            result["InfoMsg"] = f"Unrecognized command {command}"
        elif error_message:
            result["SDCERR"] = definition.SUMMIT_RCM_ERRORS["SDCERR_FAIL"]
            result["InfoMsg"] = error_message
        else:
            result["SDCERR"] = definition.SUMMIT_RCM_ERRORS["SDCERR_SUCCESS"]
            result["InfoMsg"] = ""

        return result

    async def execute_adapter_command(
        self,
        bus,
        command,
        controller_friendly_name: str,
        adapter_interface: Optional[ProxyInterface],
        post_data: Optional[dict] = None,
    ):
        result = {}
        error_message = None
        processed = False
        for plugin in bluetooth_plugins:
            try:
                (
                    processed,
                    error_message,
                    process_result,
                ) = await plugin.ProcessAdapterCommand(
                    bus, command, controller_friendly_name, adapter_interface, post_data
                )
                if process_result:
                    result.update(process_result)
            except Exception as exception:
                self.log_exception(exception)
                processed = True
                error_message = f"Command {command} failed with {str(exception)}"
                break
            if processed:
                break

        if not processed:
            result["SDCERR"] = definition.SUMMIT_RCM_ERRORS["SDCERR_FAIL"]
            result["InfoMsg"] = f"Unrecognized command {command}"
        elif error_message:
            result["SDCERR"] = definition.SUMMIT_RCM_ERRORS["SDCERR_FAIL"]
            result["InfoMsg"] = error_message
        else:
            result["SDCERR"] = definition.SUMMIT_RCM_ERRORS["SDCERR_SUCCESS"]
            result["InfoMsg"] = ""

        return result


import os
import sys

curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)

import torch
import torch.nn as nn

class SqueezeChannels(nn.Module):
    def __init__(self):
        super(SqueezeChannels, self).__init__()

    def forward(self, x):
        return x.squeeze(2)

class FCN(nn.Module):
    def __init__(self, num_classes, input_size=1):
        super(FCN, self).__init__()

        self.num_classes = num_classes
        self.conv_block1 = nn.Sequential(
            nn.Conv1d(in_channels=input_size, out_channels=128, kernel_size=8, padding='same'),
            nn.BatchNorm1d(128),
            nn.ReLU()
        )

        self.conv_block2 = nn.Sequential(
            nn.Conv1d(in_channels=128, out_channels=256, kernel_size=5, padding='same'),
            nn.BatchNorm1d(256),
            nn.ReLU()
        )

        self.conv_block3 = nn.Sequential(
            nn.Conv1d(in_channels=256, out_channels=128, kernel_size=3, padding='same'),
            nn.BatchNorm1d(128),
            nn.ReLU()
        )

        self.network = nn.Sequential(
            self.conv_block1,
            self.conv_block2,
            self.conv_block3,
            nn.AdaptiveAvgPool1d(1),
            SqueezeChannels(),
        )

        self.projection_head = nn.Sequential(
            nn.Linear(128 * 4, 256),
            nn.BatchNorm1d(256),
            nn.ReLU(inplace=True),
            nn.Linear(256, 128),
        )

    def forward(self, x, scale_x=None, is_head=False):
        if is_head:
            embed_x = self.network(x)
            fea_concat = torch.cat((embed_x, scale_x, (embed_x - scale_x), (embed_x * scale_x)), dim=1)
            return self.projection_head(fea_concat)
        return self.network(x)

class Classifier(nn.Module):
    def __init__(self, input_dims, output_dims) -> None:
        super(Classifier, self).__init__()

        self.dense = nn.Linear(input_dims, output_dims)
        self.softmax = nn.Softmax(dim=1)

    def forward(self, x):
        return self.softmax(self.dense(x))


import numpy.random

def get_matrix():
    print('Enter the dimensions :')
    n = int(input())
    m = int(input())
    return numpy.random.randint(-1000, 1000, (n, m))

def compare_mat(m1, m2):
    print("Matrix by library function: ")
    print(m1)
    print("\n\n")
    print("Matrix by equation: ")
    print(m2)
    print("\n\n")
    if (numpy.allclose(m1, m2)):
        print("Pseudoinverse matrix matched!")
    else:
        print("Pseudoinverse matrix didn't match.")

def moore_penrose(matrix):
    U, D, Vt = numpy.linalg.svd(matrix, full_matrices=False)
    D = numpy.diag(D)
    D[D != 0] = 1 / D[D != 0]
    return numpy.linalg.pinv(matrix), numpy.matmul(numpy.matmul(Vt.T, D.T), U.T)

def main():
    pseudoinverse_by_lib, pseudoinverse_by_eqn = moore_penrose(get_matrix())
    compare_mat(pseudoinverse_by_lib, pseudoinverse_by_eqn)

if __name__ == "__main__":
    main()

Linear Algebra for Machine Learning/moore-penrose.py


# -*- coding: utf-8 -*-

import numpy as np
import pandas as pd
import glob
import urllib
import os

getlist = {}
compiler = {}
fetch = {}

compiler['file_name_appendix'] = 'bv'
compiler['intvl'] = 5
compiler['limit'] = 15

compiler['marker'] = compiler['intvl']
compiler['file'] = 0

compiler['size'] = 0
compiler['count'] = 0
compiler['rows'] = 0
compiler['size_rem'] = 0
compiler['count_rem'] = 0
compiler['rows_rem'] = 0

# sample inputs
#getlist['fieldName'] = 'Cymric'
#getlist['fieldName'] = 'Buena Vista'
#getlist['operatorCode'] = None
#getlist['fieldName'] = 'Midway-Sunset'

# inputs
getlist['fieldName'] = None
getlist['districtNumber'] = None
getlist['county'] = None
getlist['operatorCode'] = 'L2025'
getlist['leaseName'] = None
getlist['apiNum'] = None
getlist['activeWellsOnly'] = False
getlist['addr'] = None
getlist['loc'] = None
getlist['sec'] = None
getlist['twn'] = None
getlist['rge'] = None
getlist['bm'] = None

filenames = glob.glob('C:/Users/bvjs/Python/python-3.4.3.amd64/bvjs/doggr_scraper/cymmck_doggr_prod_*.pk1')
for file_ in filenames:
    os.remove(file_)

def compile_save(compile):

    compile['Cyclic Injected (bbl)'] = compile['Water or Steam Injected (bbl)']
    compile['Continous Injected (bbl)'] = compile['Water or Steam Injected (bbl)']
    compile['Water Injected (bbl)'] = compile['Water or Steam Injected (bbl)']

    compile.ix[compile['Well Type Inj'] != 'SC', 'Cyclic Injected (bbl)'] = 0
    compile.ix[compile['Well Type Inj'] != 'SF', 'Continous Injected (bbl)'] = 0
    compile.ix[compile['Well Type Inj'] != 'WD', 'Water Injected (bbl)'] = 0

    compile['Total Steam Injected (bbl)'] = compile['Cyclic Injected (bbl)'] + compile['Continous Injected (bbl)']

    compile = compile.drop(['Water or Steam Injected (bbl)'], axis=1)
    compile = compile.drop(['Gas or Air Injected (Mcf)'], axis=1)

    for compiler['product'] in ['Days Well Injected', 'Days Well Produced', 'Gas Produced (Mcf)', 'Oil Produced (bbl)', 'Water Produced (bbl)', 'Water Injected (bbl)', 'Continous Injected (bbl)', 'Cyclic Injected (bbl)', 'Total Steam Injected (bbl)']:
        try:
            compile[compiler['product']] = compile[compiler['product']].fillna(0)
        except:
            pass

    compile.to_pickle('C:/Users/bvjs/Python/python-3.4.3.amd64/bvjs/doggr_scraper/cymmck_doggr_prod_'+str(compiler['file'])+'.pk1')

def build_url():
    getlist['url'] = 'https://secure.conservation.ca.gov/WellSearch/Download/ExportWellSearchResults?'\
                +'districtNumber='+getlist['districtNumber']\
                +'&county='+getlist['county']\
                +'&fieldName='+getlist['fieldName']\
                +'&operatorCode='+getlist['operatorCode']\
                +'&leaseName='+getlist['leaseName']\
                +'&apiNum='+getlist['apiNum']\
                +'&activeWellsOnly='+getlist['activeWellsOnly']\
                +'&addr='+getlist['addr']\
                +'&loc='+getlist['loc']\
                +'&sec='+getlist['sec']\
                +'&twn='+getlist['twn']\
                +'&rge='+getlist['rge']\
                +'&bm='+getlist['bm']

getlist['districtNumber']=(str(getlist['districtNumber']) if getlist['districtNumber'] != None else '')
getlist['county']=(str(getlist['county']).replace(" ", "%20") if getlist['county'] != None else '')
getlist['fieldName']=(str(getlist['fieldName']).replace(" ", "%20") if getlist['fieldName'] != None else '')
getlist['operatorCode']=(str(getlist['operatorCode']) if getlist['operatorCode'] != None else '')
getlist['leaseName']=(str(getlist['leaseName']).replace(" ", "%20") if getlist['leaseName'] != None else '')
getlist['apiNum']=(str(getlist['apiNum']) if getlist['apiNum'] != None else '')
getlist['activeWellsOnly']=('false' if getlist['activeWellsOnly'] == False else 'true')
getlist['addr']=(str(getlist['addr']) if getlist['addr'] != None else '')
getlist['loc']=(str(getlist['loc']) if getlist['loc'] != None else '')
getlist['sec']=(str(getlist['sec']) if getlist['sec'] != None else '')
getlist['twn']=(str(getlist['twn']) if getlist['twn'] != None else '')
getlist['rge']=(str(getlist['rge']) if getlist['rge'] != None else '')
getlist['bm']=(str(getlist['bm']) if getlist['bm'] != None else '')

build_url()

getlist['apilist'] = pd.ExcelFile(urllib.request.urlopen(getlist['url']))\
            .parse('CA DOGGR Wells', header=None).iloc[4:,6].unique()

if getlist['fieldName'] == 'Cymric':
    getlist['fieldName'] = 'McKittrick'
    build_url()
    getlist['apilist'] = np.unique(np.append(getlist['apilist'], pd.ExcelFile(urllib.request.urlopen(getlist['url']))\
            .parse('CA DOGGR Wells', header=None).iloc[4:,6].unique()))

getlist['apilist_len'] = len(getlist['apilist'])

print(str(getlist['apilist_len'])+' unique wells found')

compile = pd.DataFrame()

for fetch['idx_api'], fetch['api'] in enumerate(getlist['apilist']):

    build = {}
    header = {}

    build['api'] = str(fetch['api'])

    build['prod_url'] = 'https://secure.conservation.ca.gov/WellSearch/Download/ToExcelProductionData?APInumber='+build['api']
    build['inj_url'] = 'https://secure.conservation.ca.gov/WellSearch/Download/ToExcelInjectionData?APInumber='+build['api']

    while True:
        try:
            build['prod'] = pd.ExcelFile(urllib.request.urlopen(build['prod_url']))\
                    .parse('Well Production', header=None)
            build['inj'] = pd.ExcelFile(urllib.request.urlopen(build['inj_url']))\
                    .parse('Well Injection', header=None)
        except:
            print('build fail: '+build['api'])
            continue
        break

    for build['idx_hdr'], build['data'] in enumerate(build['prod'].iloc[0,:15]):
        header[build['data']] = build['prod'].iloc[1][build['idx_hdr']]

    build['prod_dates'] = pd.DataFrame(data=build['prod'].iloc[4:,1].unique())
    build['inj_dates'] = pd.DataFrame(data=build['inj'].iloc[4:,1].unique())
    build['dates'] = pd.DataFrame(data=build['prod_dates'].append(build['inj_dates'])[0].unique())
    build['dates'] = build['dates'][~build['dates'][0].str.contains('Total')]

    build['prod_pool'] = pd.DataFrame(data=build['prod'].iloc[4:,15].unique())
    build['inj_pool'] = pd.DataFrame(data=build['inj'].iloc[4:,11].unique())
    build['pools'] = pd.DataFrame(data=build['prod_pool'].append(build['inj_pool'])[0].unique())
    build['pools'] = build['pools'][pd.notnull(build['pools'][0])]

    build['prodinj'] = pd.DataFrame(data=build['dates'][0], columns=['Month'])
    for build['col'] in ['Field Name',
                'Latitude',
                'Lease Name',
                'Longitude',
                'Operator Name',
                'Range',
                'Section',
                'Township',
                'Well #']:
        build['prodinj'][build['col']] = header[build['col']]
    build['prodinj']['API10'] = '04'+str(header['API #'])
    build['prodinj'] = build['prodinj'].rename(columns={'Well #':'Well'})

    build['prod_used'] = False
    build['inj_used'] = False

    if len(build['prod_dates']) > 0:
        build['prod_data'] = build['prod'].iloc[4:,:17]
        build['prod_data'].columns = build['prod'].iloc[3,:17]
        build['prod_data']['APIPC'] = '04'+header['API #']+build['prod_data']['Pool Code']
        build['prod_data'] = build['prod_data'].rename(columns={'Well Type':'Well Type Prod'})
        build['prod_data'] = build['prod_data'].drop(['Status', 'Pool Code', 'API Number', 'Gravity of Oil', 'Casing Pressure', 'Tubing Pressure', 'BTU', 'Method of Operation', 'Water Disposition', 'PWT Status', 'Reported Date'], axis=1)
        build['prodinj'] = build['prodinj'].merge(build['prod_data'], how='left', left_on=['Month'], right_on=['Production Date'])
        build['prodinj'] = build['prodinj'].drop(['Production Date'], axis=1)
        build['prod_used'] = True

    if len(build['inj_dates']) > 0:
        build['inj_data'] = build['inj'].iloc[4:,:13]
        build['inj_data'].columns = build['inj'].iloc[3,:13]
        build['inj_data']['APIPC'] = '04'+header['API #']+build['inj_data']['Pool Code']
        build['inj_data'] = build['inj_data'].rename(columns={'Well Type':'Well Type Inj'})
        build['inj_data'] = build['inj_data'].drop(['Status', 'Pool Code', 'API Number', 'Surface Injection Pressure', 'Source of Water', 'Kind of Water', 'PWT Status', 'Reported Date'], axis=1)
        if build['prod_used'] == True:
            build['prodinj'] = build['prodinj'].merge(build['inj_data'], how='left', left_on=['Month','APIPC'], right_on=['Injection Date','APIPC'])
        else:
            build['prodinj'] = build['prodinj'].merge(build['inj_data'], how='left', left_on=['Month'], right_on=['Injection Date'])
        build['prodinj'] = build['prodinj'].drop(['Injection Date'], axis=1)
        build['inj_used'] = True

    for build['product'] in ['Days Well Injected', 'Days Well Produced', 'Gas Produced (Mcf)', 'Gas or Air Injected (Mcf)', 'Oil Produced (bbl)', 'Water Produced (bbl)', 'Water or Steam Injected (bbl)']:
        try:
            build['prodinj'][build['product']] = build['prodinj'][build['product']].fillna(0)
        except:
            pass

    build['prodinj']['Month'] = pd.to_datetime(build['prodinj']['Month'], format='%b-%Y')

    build['prodinj']['Spud'] = pd.to_datetime(build['prodinj']['Month'].min(), format='%b-%Y')

    if build['prod_used'] == True:
        if build['inj_used'] == True:
            build['prodinj'] = build['prodinj'][(build['prodinj']['Days Well Injected'] > 0) | (build['prodinj']['Days Well Produced'] > 0)]
        else:
            build['prodinj'] = build['prodinj'][build['prodinj']['Days Well Produced'] > 0]
    else:
        if build['inj_used'] == True:
            build['prodinj'] = build['prodinj'][build['prodinj']['Days Well Injected'] > 0]

    build['prodinj'] = build['prodinj'].drop_duplicates()

    compile = compile.append(build['prodinj'])

    compiler['prog'] = 100*(fetch['idx_api']+1)/getlist['apilist_len']

    progress = compiler['prog']

    if compiler['prog'] > compiler['marker']:

        compiler['size'] = np.round(compile.memory_usage().sum()/1000000,2) + compiler['size_rem']
        compiler['count'] = len(compile['API10'].unique()) + compiler['count_rem']
        compiler['rows'] = len(compile) + compiler['rows_rem']

        print(str(np.round(compiler['prog'],1))+'% complete, '+str(compiler['count'])+' wells with data found, '+str(compiler['rows'])+' rows, '+str(compiler['size'])+'MB')
        compiler['marker'] = compiler['marker']+compiler['intvl']

        if np.round(compile.memory_usage().sum()/1000000,2) > compiler['limit']:

            compile_save(compile)

            compiler['size_rem'] = compiler['size']
            compiler['count_rem'] = compiler['count']
            compiler['rows_rem'] = compiler['rows']

            compile = pd.DataFrame()
            compiler['file'] = compiler['file'] + 1

print(str(np.round(compiler['prog'],1))+'% complete, '+str(compiler['count'])+' wells with data found, '+str(compiler['rows'])+' rows, '+str(compiler['size'])+'MB')
compile_save(compile)

print('compiling pickle files')

filenames = glob.glob('C:/Users/bvjs/Python/python-3.4.3.amd64/bvjs/doggr_scraper/cymmck_doggr_prod_*.pk1')
df_compiles = pd.DataFrame()
compiles = []
for file_ in filenames:
    df_compiles_ind = pd.read_pickle(file_)
    compiles.append(df_compiles_ind)
    os.remove(file_)
df_compiles = pd.concat(compiles)
del df_compiles_ind
df_compiles.to_csv('C:/Users/bvjs/Python/python-3.4.3.amd64/bvjs/doggr_scraper/cymmck_doggr_prod_'+compiler['file_name_appendix']+'.csv', index=False)

print('job complete!')


# "C:\Program Files (x86)\Python_3_5_1\Python.exe" "C:\Users\Xylozi\Documents\Python\Projects\EU4\Colour Generator\colour_generator2.py"

import os
import sys
import random
import time
import colorsys

"""
Generate EU4 province files from definition.csv with names
"""

def getScriptPath():
	""" Returns current path of script """
	return os.path.dirname(os.path.realpath(sys.argv[0]))

def main():
	os.chdir( getScriptPath() )

	targetDir = getScriptPath() + "//provinces"
	nameList = []

	with open( "proper_definition.csv", "rt" ) as sourceFile:
		text = sourceFile.readlines()

	for i in range( len( text ) - 1 ):
		line = text[i].split( ";" )
		print( line )
		name = line[4].strip( "\t" ).strip( "\n" ).strip( "\t" ).strip( " " ).strip( "-" ).strip( "'" )
		nameList.append( name )

	os.chdir( targetDir )

	for i in range( len( nameList ) ):
		output = open( "{0} - {1}.txt".format( i+1, nameList[i] ), "wt" )
		output.write( "# {0} \n".format( nameList[i] ) )
		output.write( "is_city = no\n" )
		output.write( "\n" )
		output.write( "base_tax = 1\n" )
		output.write( "base_manpower = 1\n" )
		output.write( "base_production = 1\n" )
		output.write( "\n" )
		output.write( "culture = noculture\n" )
		output.write( "religion = noreligion\n" )
		output.write( "\n" )
		output.write( "hre = no\n" )
		output.write( "trade_goods = grain\n" )
		output.write( "\n" )
		output.close()

	output.close()

if __name__ == "__main__":
	main()


from itertools import permutations

def operation(num1, num2, op):
    if op == '+':
        return str(int(num1) + int(num2))
    elif op == '-':
        return str(int(num1) - int(num2))
    else:
        return str(int(num1) * int(num2))

def calculation(exp, op):
    arr = []
    tmp = ""
    for i in exp:
        if i.isdigit() == True:
            tmp += i
        else:
            arr.append(tmp)
            arr.append(i)
            tmp = ""
    arr.append(tmp)

    for o in op:
        stack = []
        while len(arr) != 0:
            tmp = arr.pop(0)
            if tmp == o:
                stack.append(operation(stack.pop(),arr.pop(0),o))
            else:
                stack.append(tmp)
        arr = stack

    return abs(int(arr[0]))

def solution(expression):
    answer = 0
    result = []
    for i in permutations(['+','-','*'],3):
        result.append(calculation(expression, i))
    return max(result)


"""empty message

Revision ID: 150fed6b4ab3
Revises: 34d99c094b7a
Create Date: 2020-11-24 23:07:15.786796

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '150fed6b4ab3'
down_revision = '34d99c094b7a'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('rating_factors', schema=None) as batch_op:
        batch_op.add_column(sa.Column('rating_manual_id', sa.Integer(), nullable=True))
        batch_op.create_foreign_key('fk_rating_manual_id', 'rating_manuals', ['rating_manual_id'], ['id'])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('rating_factors', schema=None) as batch_op:
        batch_op.drop_constraint('fk_rating_manual_id', type_='foreignkey')
        batch_op.drop_column('rating_manual_id')
    # ### end Alembic commands ###


from bin.contentctl_project.contentctl_infrastructure.tests.test_constants import SECURITY_CONTENT_ROOT

import os
from bin.contentctl_project.contentctl_infrastructure.builder.security_content_playbook_builder import SecurityContentPlaybookBuilder
from bin.contentctl_project.contentctl_infrastructure.builder.security_content_detection_builder import SecurityContentDetectionBuilder

def test_read_playbook():
    playbook_builder = SecurityContentPlaybookBuilder(input_path = SECURITY_CONTENT_ROOT)
    playbook_builder.setObject(os.path.join(os.path.dirname(__file__),
        'test_data/playbook/example_playbook.yml'))
    playbook = playbook_builder.getObject()

    assert playbook.name == "Ransomware Investigate and Contain"

def test_enrich_detections():

    playbook_builder = SecurityContentPlaybookBuilder(input_path = SECURITY_CONTENT_ROOT)
    playbook_builder.setObject(os.path.join(os.path.dirname(__file__),
        'test_data/playbook/example_playbook.yml'))
    playbook_builder.addDetections()
    playbook = playbook_builder.getObject()

    assert playbook.tags.detection_objects[0]['path'] == "detections/endpoint/attempted_credential_dump_from_registry_via_reg_exe.yml"


import re
import sys
import os

first_content='''%YAML:1.0
---
opencv_lbphfaces:
   threshold: 1.7976931348623157e+308
   radius: 1
   neighbors: 8
   grid_x: 8
   grid_y: 8
   histograms:
      '''
last='''labels: !!opencv-matrix
        rows: {0}
        cols: 1
        dt: i
        data: [{1}]
   labelsInfo:
        []
'''

dir=os.path.join("Database","Trainedfile")

def merge(filename):
    if(not os.path.exists(os.path.join(dir,"main.yml"))):
        os.rename(os.path.join(dir,filename),os.path.join(dir,"main.yml"))
    else:
        file1=open(os.path.join(dir,"main.yml"))
        file2=open(os.path.join(dir,filename))

        file1_content=file1.read()
        file2_content=file2.read()

        file1.close()
        file2.close()

        file1_mid=re.search("(?s)histograms:(?s)(.*)labels:",file1_content).group(1)
        file2_mid=re.search("(?s)histograms:(?s)(.*)labels:",file2_content).group(1)
        mid_content=file1_mid.strip()+"\n"+file2_mid

        file1_data=re.search("data:(?s)(.*)(?s)[[](?s)(.*)(?s)[]](?s)(.*)(?s)labelsInfo:",file1_content).group(2)
        file2_data=re.search("data:(?s)(.*)(?s)[[](?s)(.*)(?s)[]](?s)(.*)(?s)labelsInfo:",file2_content).group(2)
        data_string=file1_data+","+file2_data
        rows=len(data_string.split(","))

        last_content=last.format(rows,data_string)

        final_content=first_content+mid_content+last_content

        newfile=open(os.path.join(dir,"main.yml"),"w")
        newfile.write(final_content)
        newfile.close()
        os.remove(os.path.join(dir,filename))


import cv2 as cv
import numpy as np
import os
import random
import progressbar as progressbar

frame_height = 2160
frame_width = 3840

train_split = 0.7

widgets = [' [',
         progressbar.Timer(format= 'elapsed time: %(elapsed)s'),
         '] ',
           progressbar.Bar('*'),' (',
           progressbar.ETA(), ') ',
          ]

files = list(os.listdir('PDESTRE/Videos'))
files.sort()

train_file = open('train_pdestre.txt', 'w')
val_file = open('val_pdestre.txt', 'w')

for file in files:

    frame_counter = 1
    cap = cv.VideoCapture('PDESTRE/Videos/' + file)

    num_frames = int(cap.get(cv.CAP_PROP_FRAME_COUNT))
    bar = progressbar.ProgressBar(max_value = num_frames,
                              widgets=widgets).start()

    while(1):
        #print(frame_counter)
        # Read current frame
        ret,img = cap.read()
        if img is None:
            break

        bar.update(frame_counter)

        img_name = 'PDESTRE/images/' + file[:-4] + '-{}.jpg'.format(frame_counter)

        label_file = open('PDESTRE/labels/' + file[:-4] + '-{}.txt'.format(frame_counter), 'w')
        label_file.close()

        directory = os.getcwd()

        if(random.random() < train_split):
            train_file.write(directory + '/PDESTRE/images/' + file[:-4] + '-{}.jpg\n'.format(frame_counter))
        else:
            val_file.write(directory + '/PDESTRE/images/' + file[:-4] + '-{}.jpg\n'.format(frame_counter))

        cv.imwrite(img_name, img)
        frame_counter += 1

train_file.close()
val_file.close()

labels = list(os.listdir('PDESTRE/Annotations'))
labels.sort()

for label in labels:
    file_name = label[:-4]
    print(file_name)

    lines = open('PDESTRE/Annotations/' + label).read().strip().split('\n')

    for line in lines:

        line = list(line.split(','))
        frame_number, x, y, h, w = int(line[0]), float(line[2])/frame_width, float(line[3])/frame_height, float(line[4])/frame_height, float(line[5])/frame_width

        label_file = open('PDESTRE/labels/' + file_name + '-{}.txt'.format(frame_number), 'a')
        object_str = "0 {} {} {} {} \n".format(x, y, w, h)
        label_file.write(object_str)
        label_file.close()


#!/usr/bin/env python3

from pathlib import Path

import argparse
import re
import os

def cwd():
    """Return current working directory."""
    return os.path.dirname(os.path.realpath(__file__))

def fatal_error(msg: str) -> None:
    """Prints an error message and exists with error code 1."""
    print(f"[x] {msg}")
    exit(1)

def warn(msg: str) -> None:
    """Prints a warning message."""
    print(f"[!] {msg}")

def info(msg: str) -> None:
    """Prints an info message."""
    print(f"[*] {msg}")

def check_paths(llvm_dir: Path, arch: str) -> None:
    """Checks all relevant directories for errors and if they exist."""
    if not llvm_dir.exists():
        fatal_error(f"{llvm_dir} does not exist.")

    if not llvm_dir.is_dir():
        fatal_error(f"{llvm_dir} is not a directory.")

    out_dir: Path = Path(cwd()).joinpath(arch)
    if not out_dir.exists():
        fatal_error(f"Output directory {out_dir} does not exist.")

    if not out_dir.is_dir():
        fatal_error(f"Output directory {out_dir} is not a directory.")

    arch_dir = llvm_dir.joinpath(arch)
    if not arch_dir.exists():
        fatal_error(f"Test file directory {arch_dir} does not exist.")

    if not arch_dir.is_dir():
        fatal_error(f"Test file directory {arch_dir} is not a directory.")

def get_included_files(
    arch_dir: Path,
    out_path: Path,
    included_files: set[str],
    excluded_files: set[str] = None,
) -> list[tuple[Path, Path]]:
    """
    Generates the file list to update. Only the files listed
    via command line arguments are added.
    """
    files = list()
    file: Path
    for file in arch_dir.iterdir():
        stem = file.stem
        if stem not in included_files:
            continue
        if excluded_files and stem in excluded_files:
            included_files.remove(stem)
            continue

        included_files.remove(stem)
        files.append((file, out_path.joinpath(file.name + ".cs")))

    if len(included_files) != 0:
        warn(
            f"Could not find {', '.join(included_files)} in the LLVM test files."
        )

    return files

def get_all_files(
    arch_dir: Path,
    out_path: Path,
    excluded_files: set[str] = None,
) -> list[tuple[Path, Path]]:
    """
    Generates the file list to update. All files of an
    architecture are added.
    """
    files = list()
    file: Path
    for file in arch_dir.iterdir():
        stem = file.stem
        if excluded_files and stem in excluded_files:
            continue

        files.append((file, out_path.joinpath(file.name + ".cs")))
    return files

def get_file_list(
    llvm_dir: Path,
    arch: str = None,
    excluded_files: set[str] = None,
    included_files: set[str] = None,
) -> list[tuple[Path, Path]]:
    """
    Generates a list of files to update.
    The list contains tuples of the form: (llvm_file_path, cs_file_path)
    """

    out_dir: Path = Path(cwd()).joinpath(arch)
    arch_dir = llvm_dir.joinpath(arch)

    if included_files and len(included_files) != 0:
        return get_included_files(
            arch_dir, out_dir, included_files, excluded_files
        )
    return get_all_files(arch_dir, out_dir, excluded_files)

def create_new_test_file(arch: str, cs_file: Path) -> str:
    """
    Creates a new test files and asks for the tesst parameter for it.
    :return: The test parameter string.
    """
    info(f"Add new test file: {cs_file}")
    info("You need to provide the test parameters for it.")
    test_parameters = f"# CS_ARCH_{arch.upper()}, "
    test_parameters += input(
        "\nAdd architecture mode of tests"
        "(CS_MODE_THUMB, CS_MODE_BIG_ENDIAN, ...)\n"
        "> "
    )
    test_parameters += ", "
    test_parameters += input(
        "\nAdd disassembly options for this test file"
        "(CS_OPT_SYNTAX_NOREGNAME, CS_OPT_SYNTAX_ATT, None, ...)\n"
        "> "
    )
    test_parameters += "\n"
    cs_file.touch()
    return test_parameters

def get_test_parameters(cs_file: Path) -> str:
    """
    Extracts the test parameters string from
    an existing Capstone test file.
    """
    with open(cs_file) as f:
        line = f.readline()

    # Check for "# CS_ARCH_<ARCH>, CS_MODE_<MODE>, ..." lines
    regex = r"#\s*CS_ARCH_.+,\s*CS_MODE_.+,\s*.+"
    if not re.search(regex, line):
        fatal_error(
            f"The first line in {cs_file} is not "
            f"the test parameter line.\nLine: {line}"
        )
    return line

def decimal_to_hex_fix(asm: str) -> str:
    """
    Replaces every immediate number in the asm string with its hex form.
    If it is larger than the hex threshold.
    """
    # Defined in utils.h
    hex_threshold = 9
    matches = re.findall(r"([#\s]-?\d+)", asm)
    if not matches:
        return asm

    for m in matches:
        num = int(m[1:])
        neg_num = num < 0
        sign = ""
        if neg_num:
            num = num * -1
            sign = "-"
        if num < hex_threshold:
            continue
        prefix = m[0]
        asm = re.sub(m, rf"{prefix}{sign}{hex(num)}", asm)
    return asm

def extract_tests(llvm_file: Path) -> str:
    """
    Extracts all compatible test cases in the given llvm_file
    and returns them as string.
    """
    hex_encoding = r"(0x[a-fA-F0-9][a-fA-F0-9],?\s*)+"
    asm_regex = r"(.*)"

    test_case_patterns = [
        rf"#?\s*@?\s*CHECK:\s+{asm_regex}\s+@\s+encoding:\s+\[({hex_encoding})\]",
    ]

    result = ""

    if llvm_file.is_dir():
        return result

    f = open(llvm_file)
    for line in f.readlines():
        match = list()
        for regex in test_case_patterns:
            match: list = re.findall(regex, line)
            if match:
                break
        if not match:
            continue
        match = match[0]
        asm = re.sub(r"\s+", " ", match[0])
        asm = asm.strip(" ")
        asm = decimal_to_hex_fix(asm)
        hexbytes = re.sub(r"\s", "", match[1])
        result += f"{hexbytes} = {asm}\n"
    f.close()
    return result

def update(
    llvm_dir: Path,
    arch: str,
    excluded_files: set[str] = None,
    included_files: set[str] = None,
) -> None:
    """
    Updates all regression test files for Capstone.
    """

    check_paths(llvm_dir, arch)

    files: list[tuple[Path, Path]] = get_file_list(
        llvm_dir, arch, excluded_files, included_files
    )

    for file in files:
        llvm_file = file[0]
        cs_file = file[1]

        cs_tests = extract_tests(llvm_file)
        if cs_tests == "":
            continue

        if not cs_file.exists():
            test_parameters = create_new_test_file(arch, cs_file)
        else:
            test_parameters = get_test_parameters(cs_file)

        with open(cs_file, "w") as f:
            f.write(test_parameters)
            f.write(cs_tests)
    info("Update done")

def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        prog="Test file updater",
        description="Synchronizes test files with LLVM",
    )
    parser.add_argument(
        "-d",
        dest="llvm_dir",
        help="Path to the LLVM MC Disassembler test files.",
        required=True,
        type=Path,
    )
    parser.add_argument(
        "-a",
        dest="arch",
        help="Name of architecture to update.",
        choices=["ARM"],
        required=True,
    )
    parser.add_argument(
        "-e",
        dest="excluded_files",
        metavar="filename",
        nargs="+",
        help="File names to exclude from update (without file extension).",
        type=list,
    )
    parser.add_argument(
        "-f",
        dest="included_files",
        metavar="filename",
        nargs="+",
        help="Specific list of file names to update (without file extension).",
    )
    arguments = parser.parse_args()
    return arguments

if __name__ == "__main__":
    args = parse_args()
    update(args.llvm_dir, args.arch, args.excluded_files, args.included_files)


from shutterstock.endpoint import EndPoint, EndPointParam, ChoicesParam, IntegerParam

class SimpleEndPoint(EndPoint):
    """Endpoint for testing"""
    YES = 'yes'
    NO = 'no'
    MAYBE = 'maybe'
    CHOICES = (YES, NO, MAYBE, )

    id = EndPointParam(required=True,
                       help_text='Required. The ID of the object.')
    choice = ChoicesParam(required=True, default=MAYBE, choices=CHOICES,
                           help_text='Required. Choice field')
    not_required = EndPointParam(help_text='This field is not required.')
    limit = IntegerParam(min=1, max=100)

def test_endpoint_param_setup():
    assert len(SimpleEndPoint.params) == 4
    assert SimpleEndPoint.id in SimpleEndPoint.params
    assert SimpleEndPoint.choice in SimpleEndPoint.params
    assert SimpleEndPoint.not_required in SimpleEndPoint.params

def test_endpoint_prepare():
    endpoint = SimpleEndPoint('/simple/{id}')
    uri, params = endpoint.prepare(id=5, nothing=True)
    assert uri == '/simple/5'

    assert 'nothing' not in params

    assert 'id' in params
    assert params['id'] == 5

    # Test default on require field
    assert 'choice' in params
    assert params['choice'] == SimpleEndPoint.MAYBE

    # Test that not_required is not in params list
    assert 'not_required' not in params

    invalid_choice_raised = False
    try:
        uri, params = endpoint.prepare(id=5, choice='hard no')
    except ValueError:
        invalid_choice_raised = True
    assert invalid_choice_raised

    invalid_value_raised = False
    try:
        uri, params = endpoint.prepare(id=5, choice=SimpleEndPoint.MAYBE, limit=0)
    except ValueError:
        invalid_value_raised = True
    assert invalid_value_raised

    invalid_value_raised = False
    try:
        uri, params = endpoint.prepare(id=5, choice=SimpleEndPoint.MAYBE, limit=101)
    except ValueError:
        invalid_value_raised = True
    assert invalid_value_raised

    invalid_value_raised = False
    try:
        uri, params = endpoint.prepare(id=5, choice=SimpleEndPoint.MAYBE,
                                       limit=100)
    except ValueError:
        invalid_value_raised = True
    assert invalid_value_raised is False

    invalid_value_raised = False
    try:
        uri, params = endpoint.prepare(id=5, choice=SimpleEndPoint.MAYBE,
                                       limit=1)
    except ValueError:
        invalid_value_raised = True
    assert invalid_value_raised is False


import numpy as np

vectors_rem = np.random.rand(2,4)

#https://en.wikipedia.org/wiki/Orthogonal_complement
def get_orthogonal_complement_full_matrix(vectors):

    def zeroOutDimension(vectorZeroed, vectorZeroing, zeroingInd):
        vectorZeroed -= vectorZeroing * vectorZeroed[zeroingInd] / vectorZeroing[zeroingInd]

    def normalizeDimension(vectorNormalized, normalizedInd):
        vectorNormalized /= vectorNormalized[normalizedInd]

    for zeroingInd, vectorZeroing in enumerate(vectors):
        for zeroedInd, vectorZeroed in enumerate(vectors):
            if zeroingInd!=zeroedInd:
                zeroOutDimension(vectorZeroed, vectorZeroing, zeroingInd)

    for normalizedInd, vectorNormalized in enumerate(vectors):
        normalizeDimension(vectorNormalized, normalizedInd)

    matrix = np.identity(np.shape(vectors)[1])
    matrix[:, :np.shape(vectors)[0]] = np.transpose(-vectors)
    matrix[:np.shape(vectors)[0]] = vectors

    return matrix

def transform_vector_space(vector_space, transformation_matrix):
    return np.linalg.inv(transformation_matrix) @ vector_space

print(get_orthogonal_complement_full_matrix(vectors_rem))

x=0


import os
import sys

from qiniu import Auth

QINIU_SECRET_KEY = os.environ.get("QINIU_SECRET_KEY")
QINIU_ACCESS_KEY = os.environ.get("QINIU_ACCESS_KEY")
BUCKET_NAME = os.environ.get("BUCKET_NAME")

def get_upload_token() -> str:
    try:
        q = Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY)
        token = q.upload_token(BUCKET_NAME)
        return token
    except Exception as e:
        sys.stderr.write(e)
        return ""


import requests
import json

HEADERS = {
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15'
    , 'accept': 'text/plain', 'content-type': 'application/json'}

def get_json(user_id):
    response = requests.get("https://soprachev.com/api/timetableBot/get", json={'apiKey':'test', 'userID': str(user_id)})
    if response.status_code != 200:
        print("Problem with API")
        return False
    data = json.loads(response.text)
    print(data)
    return data['url']

def post_json(user_id, url):
    jsons_str = {"apiKey": "test", "userID": str(user_id), "url": url}
    print(json.dumps(jsons_str))
    response = requests.post("https://soprachev.com/api/timetableBot/update", data=json.dumps(jsons_str), headers=HEADERS)
    if response.status_code != 200:
        print("Problem with my mind")
        print(response.text)
        return
    print(response.text)
    return True


#! /usr/bin/python
from list import list_all
from jinja2 import Environment, FileSystemLoader

env = Environment(loader=FileSystemLoader('.'))
TEMPLATEFILE='index_t.html'
template = env.get_template( TEMPLATEFILE )
types = ['epub', 'pdf', 'mobi']
template_var = list_all( types )
OUTPUTFILENAME='index.html'

html=template.render( template_var )
print("Writing HTML to " + OUTPUTFILENAME + "...")
print("====================================================")
# print html

with open(OUTPUTFILENAME, "wb") as handle:
    ## All we needed was .encode('utf-8')
    ## <http://stackoverflow.com/a/18268929/699556>
    handle.write( html.encode('utf-8') )
print("Done.")
print("====================================================")


"""create share notify table

Revision ID: 18202d01ebb0
Revises: 45ef91c289fb
Create Date: 2015-04-19 16:02:53.223668

"""

# revision identifiers, used by Alembic.
revision = '18202d01ebb0'
down_revision = '45ef91c289fb'

from alembic import op
import sqlalchemy as sa

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('share_notify',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user', sa.String(), nullable=False),
    sa.Column('resource_id', sa.String(), nullable=False),
    sa.Column('timeshared', sa.DateTime(), nullable=False),
    sa.Column('timeopened', sa.DateTime(), nullable=True),
    sa.Column('opened', sa.Boolean(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_share_notify_resource_id', 'share_notify', ['resource_id'], unique=False)
    op.create_index('ix_share_notify_user', 'share_notify', ['user'], unique=False)
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('ix_share_notify_user', table_name='share_notify')
    op.drop_index('ix_share_notify_resource_id', table_name='share_notify')
    op.drop_table('share_notify')
    ### end Alembic commands ###


import time
import threading

def change_time(second):
    hours = second // 3600
    mins = second % 3600 // 60
    sec = second % 3600 % 60 // 60
    return hours,mins,sec

def start_time(st, name, stop=True):
    while stop:
        now = time.time()
    second = now - st
    hours, mins, sec = change_time(second)
    print('%s task time is %s:%s:%s'%(name, hours, mins, sec))

def main():
    st = time.time()
    timers = []
    for i in range(10):
        t = threading.Thread(target=start_time, args=(st, 'player%s'%i,True))
        timers.append(t)
    for th in timers:
        th.start()


from django.contrib import admin
from django.urls import path, include

admin.site.site_header = "Blog Admin"
admin.site.site_title = "Blog Admin Panel"
admin.site.index_title = "Welcome to Blog Admin Panel"

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('home.urls')),
    path('blog/', include('blog.urls')),
]


import fresh_tomatoes
import media

toy_story = media.Movie("Toy Story",
                        "A story of a boy and his toys that came to life",
                        "http://upload.wikimedia.org/wikipedia/en/1/13/Toy_Story.jpg",
                        "https://www.youtube.com/watch?v=vwyZH85NQC4")
print(toy_story.storyline)

avatar = media.Movie("Avatar",
                     "A marine on  an alien planet",
                     "https://upload.wikimedia.org/wikipedia/en/b/b0/Avatar-Teaser-Poster.jpg",
                     "http://www.youtube.com/watch?v=-9ceBgWV8io")
print(avatar.storyline)

the_place = media.Movie("The Place Beyond the Pines",
                     " A motorcycle stunt rider turns to robbing banks as a way to provide for his lover and their newborn child",
                     "https://images.genius.com/13e03b1875bd12032c2e873098c36e92.658x658x1.jpg",
                     "https://www.youtube.com/watch?v=jDcGYFYwDnU")

print(the_place.storyline)

ex_machina = media.Movie("Ex Machina",
                     "A young programmer is selected to participate in a ground-breaking experiment in synthetic intelligence ",
                     "http://t3.gstatic.com/images?q=tbn:ANd9GcQe8L-1PTMlUf-si2Oy6BTd9ZtbWH7BSRSF5k5JGNATxOHzyIdg",
                     "https://www.youtube.com/watch?v=3hAlv0xLJJ4")

kendrick = media.Movie("Kendrick Lamar",
                  "DNA",
                  "http://cache.umusic.com/_sites/kendricklamar.com/images/og.jpg",
                  "https://www.youtube.com/watch?v=NLZRYQMLDW4")

movies = [toy_story, avatar, the_place, ex_machina, kendrick]
fresh_tomatoes.open_movies_page(movies)
print(media.Movie.VALID_RATINGS)
print(media.Movie.__doc__)


from paho.mqtt import client as mqtt
import ssl
import logging
import time
import json

# logging.basicConfig(level=logging.DEBUG)

# Get the IoT Connector ID
try:
    with open("connector_id.txt", "r") as connector_file:
        print("File 'connector_id.txt' found...")
        connector_id = connector_file.read().strip()
except FileNotFoundError as exc:
    print("File 'connector_id.txt' not found...")
    exit()

# Get the Device ID
try:
    with open("device_id.txt", "r") as device_file:
        print("File 'device_id.txt' found...")
        device_id = device_file.read().strip()
except FileNotFoundError as exc:
    print("File 'device_id.txt' not found...")
    exit()

print("\nUsing IoT Connector ID: {}".format(connector_id))
print("\nUsing Device ID: {}".format(device_id))
host = "{}.m2.exosite-staging.com".format(connector_id)
cert = "./Murano_Selfsigned_Root_CA.cer"

def on_connect(client, userdata, flags, rc):
    topic = "$resource/config_io"
    value = '{"channels": {"brewing_status": {"display_name": "Brewing Status","description": "","properties": {"data_type": "BOOLEAN","control": true}}}, {"brewing_mode": {"display_name": "Brewing Mode","description": "","properties": {"data_type": "STRING","control": true}}}}'

    print("Publishing value '{}' to topic '{}'".format(value, topic))
    client.publish(topic, value, qos=0)

    print("Done! Disconnecting...")
    client.disconnect()

def on_message(client, userdata, msg):
    print("Cloud-originated message '{}' found for topic '{}'".format(msg.payload.decode(), msg.topic))

def on_disconnect(client, userdata, rc):
    print(rc)
    if rc != 0:
        # Unexpected disconnect
        print("Disconnected with error: {}".format(rc))
        exit()

client = mqtt.Client(client_id="", clean_session=True)
logger = logging.getLogger(__name__)
client.enable_logger(logger)

# Ref: https://github.com/eclipse/paho.mqtt.python/blob/1.1/README.rst#tls_set
client.tls_set(
    ca_certs=cert,
    cert_reqs=ssl.CERT_REQUIRED
)
client.will_set(topic="$resource/config_io", payload="this is from the will", qos=0)

token = open("./token.txt", "r").read()
print("Using Token: {}\n".format(token))
client.username_pw_set("", token)

client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message

client.connect(host, 8883)
client.loop_forever()


class Solution:
    def product_except_self(self, nums: list[int]) -> list[int]:
        """solution1
        暴力破解：
        """
        new_nums = []
        for i in range(len(nums)):
            new_nums.append(self._product_array(nums[:i] + nums[i + 1 :]))

        return new_nums

    def _product_array(self, nums: list[int]) -> int:
        product = 1
        print(nums)
        for n in nums:
            product *= n
            print(product)
        return product

    """
    solution2:
    * calculate the left
    * calculate the right
    def productExceptSelf(self, nums):
        n = len(nums)
        result = [1] * n

        left_product = 1
        for i in range(n):
            result[i] *= left_product
            left_product *= nums[i]

        right_product = 1
        for i in range(n-1, -1, -1):
            result[i] *= right_product
            right_product *= nums[i]

        return result

    """

if __name__ == "__main__":
    solution = Solution()
    nums = [1, 2, 3, 4]
    print(solution.product_except_self(nums))


########################## Q-1
##Function for reverse traingle
def opptrian(num):
    row=num
    while row>0:
        space = num-row
        while space>0:
            print(end=" ")
            space = space-1
        star = num-row-1
        while star<num-1:
            print("*",end=" ")
            star = star+1
        row = row-1
        print()

##Function for traingle
def trian(num):
    row = 0
    while row<num:
        space = num-row-1
        while space>0:
            print(end=" ")
            space = space-1
        star = row+1
        while star>0:
            print("*", end=" ")
            star= star-1
        row=row+1
        print()

a=0
num= int(input("Enter the length of width: "))
turn= int(input("Enter the number of turns(1 for bowtie, 2 for candy,): "))

if turn == 1:
    opptrian(num)
    trian(num)

if turn == 2:
    opptrian(num)
    trian(num)
    opptrian(num)
    trian(num)

######################### Q-2  #A

def is_leapyear(year):

    temp=(year % 4 ==0) and (year % 100 !=0) or (year % 400 ==0)

    print(temp)

year = int(input("Enter year: "))

is_leapyear(year)

# b. Write a function is_vowel() that returns True or False depending on whether its arguments is a vowel or
# consonant

def is_vowel(vorc):
    vorc = vorc.upper()
    temp = vorc == "A" or vorc == "E" or vorc == "I" or vorc == "O" or vorc == "U"
    print(temp)

vorc = input("enter a char: ")
is_vowel(vorc)

#########################  A  Q-3
import random
print (random.sample([i for i in range(1,1001) if i%5==0 and i%7==0], 5)) #print numbers between 1 to 1000 is divide by 5 and 7

#########################  B
def removeEven(arr):
   return [ i for i  in arr if i % 2 != 0]  ##remove even number from mylist

mylist = [22,44,33,55,66,77]
print(removeEven(mylist))

#######################   Q-4
#import random
str = ["the", "string", "should", "be" ,"in" ,"a", "proper", "manner"]
listStr = list(str)
random.shuffle(listStr)
sentence = listStr
print("original string",str)
print("after suffled",sentence)

###################     Q-5

i=int(input("Enter number of rows: "))
a=[]
for n in range(i):
    a.append([])
    a[n].append(1)
    for k in range(1,n):
        a[n].append(a[n-1][k-1]+a[n-1][k])
    if(i!=0):
        a[n].append(1)
for n in range(i):
    print(end=" ",sep=" ")
    for k in range(0,n+1):
        print('{0:6}'.format(a[n][k]),end=" ",sep=" ")
    print()

###################     Q-6

data = [('sima','patel','may',1990),('janvi','patel','december',1995),('rima','patel','april',1991),('mira','rins','feburary',1999),('rims','yinsho','october',1994)]
print(sorted(data))

#################       Q-7
#####################   A.
def wordFrequencies(myList):
    myDictionary = {}
    for x in myList:
        myDictionary[x] = myList.count(x)
    return (myDictionary)

myList = ["Hi","This","is","a","paragraph","Hi","This","is","python"]
print("Original list: ",myList)
myDict = wordFrequencies(myList)
print("Dictionary with word frequencies : ",myDict)

#####################  B.

def invertDict(myDict):
    newDict = {}
    for key,value in myDict.items():
        newDict[value] = key
    return newDict

def sortDict(newDict):
    return[(i,newDict[i]) for i in sorted(newDict.keys())]

invertedDict = invertDict(myDict)
sortedDict = sortDict(invertedDict)

print("Sorted Dictionary: ",sortedDict)


import shapefile
import os
import json
from json import dumps
import datetime
import utm
import geopandas

inputFilename = os.getcwd() + "/E501_Basemap.shp"
outputGeoJSON = "E501_Basemap.js"

def hasShp( name ):
     if (( name.find(".shp") > -1 )):
        positionShp = name.find(".shp")
        lenShp = len(name)
        if (lenShp == positionShp + 4):
                return name

def flatten_gdf_geometry(gdf, geom_type):
    geometry = gdf.geometry
    flattened_geometry = []
    flattened_gdf = geopandas.GeoDataFrame()
    for geom in geometry:
        print (geom.type)
        if geom.type in ['GeometryCollection', 'MultiPoint', 'MultiLineString', 'MultiPolygon']:
            for subgeom in geom:
                #print(subgeom.type)
                #if subgeom.type==geom_type:
                flattened_geometry.append(subgeom)
        else:
            #if geom.type==geom_type:
            flattened_geometry.append(geom)
    flattened_gdf.geometry=flattened_geometry
    return flattened_gdf

def shp2geoJS(inputFilename, outputGeoJSON):
        shp = geopandas.read_file(inputFilename)
        shp = shp.to_crs(epsg=4326)
        print (shp.head())
        #fig, ax = plt.subplots()
        #shp.plot(ax = ax)
        #fig.show()
        flat1 = flatten_gdf_geometry (shp, 'LineString')
        flat1.to_file(outputGeoJSON, driver="GeoJSON")

# lists the files in current directory and retains the *.shp ones.
shpList = list (map(hasShp, os.listdir()))
while (None in shpList):
        shpList.remove(None)

# goes by all the *.shp files one by one and transforms them into js files so they be consumed by MapBox.
for file in shpList:
        print ("processing the file called " + file )
        shp2geoJS(file, file+'.js')


from mysite.celery import app
from celery.schedules import schedule
from datetime import datetime
# from bikes_monitoring.PrestaRequest.mainp.db.db_writer import ReserveBikes
from bikes_monitoring.PrestaRequest.mainp.reserver import Reserve

@app.task
def auto_delete_reserve(comb_id, phone_number, request_url, api_key):
    # rb = ReserveBikes()
    # rb.deactivate_reservation(comb_id=comb_id, phone_number=phone_number)
    rb = Reserve(api_secret_key=api_key, request_url=request_url)
    db_data = {}
    db_data["comb_id"] = comb_id
    db_data["phone_number"]=phone_number

    rb.db_data = db_data
    rb.url_to_delete = request_url

    rb.deactivate()

    return True


from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from ... import models as semiadmin_model
from student import models as student_model
from datetime import date
from utils import help as help_tool

class SchoolSettings(View):
	def get(self, request):
		school_setting = semiadmin_model.SchoolSettings.objects.last()
		return render(request, 'semiadmin/settings/school_settings.html', {'school_setting':school_setting,
		 'sessions':help_tool.sessioner()})

	def post(self, request):
		school_name = request.POST.get("school_name")
		phone = request.POST.get("phone")
		address = request.POST.get("address")
		category = request.POST.getlist("category[]")
		mark_format = request.POST.getlist("mark_format[]")
		session_years = request.POST.getlist("session[]")

		def sesser(ses):
			try:
				left = ses.split('-')[0]
				right = ses.split('-')[1]
			except:
				return False
			else:
				if left.isdigit() and right.isdigit():
					return True
				return False

		session = []
		for ses in session_years:
			if sesser(ses):
				ses_found = student_model.Session.objects.filter(session=ses).first()

				if ses_found:
					session.append(ses_found)
				else:
					ses_create = student_model.Session.objects.create(session=ses)
					session.append(ses_create)
			else:
				session.append(None)

		school_setting = semiadmin_model.SchoolSettings.objects.first()
		if school_setting:
			school_setting.school_name = school_name
			school_setting.phone = phone
			school_setting.address = address
			school_setting.save()
		else:
			school_setting = semiadmin_model.SchoolSettings.objects.create(school_name=school_name, phone=phone, address=address)

		marksheet_format_entrys = school_setting.marksheet_format_entrys.all()
		length = len(marksheet_format_entrys)
		length_update = len(category)

		if marksheet_format_entrys and length <= length_update:
			for i in range(length_update):
				if length > i:
					if not category[i] and not mark_format[i] and not session[i] :
						continue
					marksheet_format_entrys[i].category = category[i]
					marksheet_format_entrys[i].mark_format = mark_format[i]
					if session[i]:
						marksheet_format_entrys[i].session = session[i]
					marksheet_format_entrys[i].save()

				else:
					if not category[i] and not mark_format[i] and not session[i] :
						continue
					if not session[i]:
						school_setting.marksheet_format_entrys.create(category=category[i], mark_format=mark_format[i])
					else:
						school_setting.marksheet_format_entrys.create(category=category[i], mark_format=mark_format[i], session=session[i])

		elif marksheet_format_entrys and length > length_update:
			for i in range(length):
				if length_update > i:
					if not category[i] and not mark_format[i] and not session[i] :
						continue
					marksheet_format_entrys[i].category = category[i]
					marksheet_format_entrys[i].mark_format = mark_format[i]
					if session[i]:
						marksheet_format_entrys[i].session = session[i]
					marksheet_format_entrys[i].save()
				else:
					marksheet_format_entrys[i].delete()

		else:
			for i in range(len(category)):
				if not category[i] and not mark_format[i] and not session[i] :
						continue
				if not session[i]:
					school_setting.marksheet_format_entrys.create(category=category[i], mark_format=mark_format[i])
				else:
					school_setting.marksheet_format_entrys.create(category=category[i], mark_format=mark_format[i], session=session[i])

		return JsonResponse({"status": True, "notification": "Setting updated successfully"})


from tkinter import *
import qrcode
from PIL import Image,ImageTk
from resizeimage import resizeimage

class QRGenerator:
    def __init__(self,root):
        self.root=root
        self.root.geometry("900x500+200+50")
        self.root.title("QR Generator")
        self.root.resizable(False,False)

        title=Label(self.root,text=" QR Generator",font=("times new roman",40),bg='#053246',fg='white',anchor='w').place(x=0,y=0,relwidth=1)

        #frame 1

        #variables
        self.var_id=StringVar()
        self.var_name=StringVar()
        self.var_department=StringVar()
        self.var_desig=StringVar()

        emp_frame=Frame(self.root,bd=2,relief=RIDGE,bg='white')
        emp_frame.place(x=50,y=100,width=500,height=380)

        emptitle=Label(emp_frame,text=" Employee Details",font=("goudy old style",30),bg='#043246',fg='white',anchor='c').place(x=0,y=0,relwidth=1)

        labelEmp=Label(emp_frame,text=" Employee Id",font=("times new roman",15),bg='white',anchor='c').place(x=20,y=60)
        labelName=Label(emp_frame,text=" Name",font=("times new roman",15),bg='white',anchor='c').place(x=20,y=100)
        labelDepart=Label(emp_frame,text=" Department",font=("times new roman",15),bg='white',anchor='c').place(x=20,y=140)
        labelDesig=Label(emp_frame,text=" Designation",font=("times new roman",15),bg='white',anchor='c').place(x=20,y=180)

        txtEmp=Entry(emp_frame,font=("times new roman",15),bg='lightyellow',textvariable=self.var_id).place(x=200,y=60)
        txtlName=Entry(emp_frame,font=("times new roman",15),bg='lightyellow',textvariable=self.var_name).place(x=200,y=100)
        txtlDepart=Entry(emp_frame,font=("times new roman",15),bg='lightyellow',textvariable=self.var_department).place(x=200,y=140)
        txtDesig=Entry(emp_frame,font=("times new roman",15),bg='lightyellow',textvariable=self.var_desig).place(x=200,y=180)

        btn_gen=Button(emp_frame,text="QR generate",command=self.qrgenerate,font=("times new roman",18,'bold'),bg='#2196f3',fg='white').place(x=50,y=250,width=180,height=30)
        btn_clear=Button(emp_frame,text="Clear",command=self.clear,font=("times new roman",18,'bold'),bg='#607d8b',fg='white').place(x=282,y=250,width=120,height=30)

        self.msg=""
        self.msg_label=Label(emp_frame,text=self.msg,font=("goudy old style",20),bg='white',fg='green')
        self.msg_label.place(x=0,y=320,relwidth=1)

        #frame 2
        qr_frame=Frame(self.root,bd=2,relief=RIDGE,bg='white')
        qr_frame.place(x=600,y=100,width=250,height=380)

        self.qrtitle=Label(qr_frame,text=" QR Code",font=("goudy old style",30),bg='#043246',fg='white',anchor='c').place(x=0,y=0,relwidth=1)
        self.qrcode=Label(qr_frame,text="QR Code\nNot Available",font=("times new roman",15),bg='#3f51b5',fg='white',bd=1,relief=RIDGE)
        self.qrcode.place(x=35,y=100,height=180,width=180)

        #function

    def clear(self):
        self.var_id.set('')
        self.var_name.set('')
        self.var_department.set('')
        self.var_desig.set('')
        self.msg=""
        self.msg_label.config(text=self.msg)
        self.qrcode.config(image='')

    def qrgenerate(self):
        if self.var_desig.get()=='' or self.var_id.get()=='' or self.var_name.get()=='' or self.var_department.get()=='':
            self.msg="All fields are required!!"
            self.msg_label.config(text=self.msg,fg='red')
        else:

            qr_data=(f"Employee Id :{self.var_id.get()}\nEmployee Name: {self.var_name.get()}\nDepartment:{self.var_department.get()}\nDesignation:{self.var_desig.get()}")
            qr_code=qrcode.make(qr_data)

            qr_code=resizeimage.resize_cover(qr_code,[180,180])

            self.im=ImageTk.PhotoImage(qr_code)
            self.qrcode.config(image=self.im)

            self.msg="QR Code generated successfully"
            self.msg_label.config(text=self.msg,fg='green')

root=Tk()
obj=QRGenerator(root)

root.mainloop()


import os
from config import *
import numpy as np

from utils_cpu import cpu_window
from utils import *
import matplotlib.pyplot as plt
import matplotlib

plt.rcParams['figure.figsize'] = (8.0, 12.0) # 设置figure_size尺寸
# plt.rcParams['image.interpolation'] = 'nearest' # 设置 interpolation style
# plt.rcParams['image.cmap'] = 'black' # 设置 颜色 style

fig_name = 'exp_2_'+band_str+'.jpg'

# ra = range(len_layers)
ra = [0, 1, 2, 3]
for i in ra:
    layer_result = []
    for j in range(len_methods):
        method_result = []
        for idx in range(start, end):
            cpu_powers, cpu_types = cpu_window(idx, idx + 5)
            if cpu_powers == False:
                # print('Error! The required amount of cpus is larger than that available.')
                break
            # print(band)
            method_result.append(1 / methods[j](layers[i], cpu_powers, band))

        layer_result.append(method_result)
    x=list(range(1, len(layer_result[0])+1))
    plt.subplot(len(ra), 1, list(ra).index(i)+1)
    for j in range(len_methods):
        plt.plot(x, layer_result[j], label=legend[j],
                 marker=markers[j], color=fcs_line[j],
                 markevery=markevery)
        # if legend[j]=='DADS':
        #     print('DADS', layer_result[j])
        # elif legend[j]=='NM':
        #     print('NM', layer_result[j])
        # print(legend[j], layer_result[j])
    plt.legend(fontsize=fs, loc='best')
    plt.grid(color='black')
    # plt.xlabel('DNN', fontsize=fs)
    plt.ylabel('FPS\n(' + name_list[i]+')', fontsize=fs)
    plt.xticks(fontsize=fs)
    plt.yticks(fontsize=fs)

plt.xlabel('Index', fontsize=fs)
plt.tight_layout()
plt.savefig(fig_name, dpi=600)
plt.show()