import click
from tools.memoization import memoize
from math import factorial

@click.command('53')
@click.option('--limit', '-l', type=int, default=100)
@click.option('--threshold', '-t', type=int, default=1000000)
@click.option('--verbose', '-v', count=True)
def problem_053(limit, threshold, verbose):
    """Combinatoric selections.

    There are exactly ten ways of selecting three from five, 12345:

    123, 124, 125, 134, 135, 145, 234, 235, 245, and 345

    In combinatorics, we use the notation, 5C3 = 10.

    In general,

    nCr =  |

    n!
    r!(n−r)!

    | ,where r ≤ n, n! = n×(n−1)×...×3×2×1, and 0! = 1.
    ---|---|---

    It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066.

    How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are
    greater than one-million?

    """

    numbers = range(1, limit + 1)
    pairs = ((n, r) for n in numbers for r in range(1, n + 1))
    combos = ((n, r, combinations(n, r)) for n, r in pairs)
    over_threshold = (combo for combo in combos if combo[2] > threshold)
    click.echo(len(list(over_threshold)))

factorial = memoize(factorial)

@memoize
def combinations(n, r):
    return factorial(n) // (factorial(r) * factorial(n - r))


# 이하 교재에 있는 코드

from __future__ import annotations
from typing import Any, Type
import hashlib

class Node:
# 해시 구성 노드
    def __init_(self,key : Any, value : Any, next : Node) -> None:
        # 초기화
        self.key = key
        self.value = value
        self.next = next

class ChainedHash:
# 해시 클래스 구현
    def __init__(self,capacity: int) -> None:
        # 초기화
        self.capacity = capacity
        self.table = [None] * self.capacity

    def hash_value(self, key : Any) -> int:
        # 해시값 구하기
        if isinstance(key, int):
            return key % self.capacity
        return(int(hashlib.sha256(str(key).encode()).hexdigest(),16) % self.capacity)

    def search(self, key : Any) -> Any:
        # 해시에서 키 값을 검색한 후 해당 값 반환
        hash = self.hash_value(key)
        p = self.table[hash]

        while p is not None:    # 해시에 어떤 값이 있을 때
            if p.key == key:    # 찾으려는 값과 키 값이 일치하면
                return p.value  # 검색 성공
            p = p.next          # 뒤쪽 노드 주목

    def add(self, key : Any, value : Any) -> bool:
        # 키가 key이고 값이 value인 원소 추가
        hash = self.hash_value(key)
        p = self.table[hash]

        while p is not None:
            if p.key == key:
                return False
            p = p.next

        temp = Node(key, value, self.table[hash])
        self.table[hash] = temp
        return True

    def remove(self, key: Any) -> bool:
        # 해시 값 삭제 함수
        hash = self.hash_value(key)
        p = self.table[hash]
        pp = None

        while p is not None:
            if p.key == key:
                if pp is None:
                    self.table[hash] = p.next
                else:
                    pp.next = p.next
                return True
            pp = p
            p = p.next
        return False

    def dump(self) -> None:
        # 모든 키 값 반환 함수
        for i in range(self.capacity):
            p = self.table[i]
            print(i, end=' ')
            while p is not None:
                print(f' -> {p.key} ({p.value})', end=' ')
                p = p.next
            print()


# https://atcoder.jp/contests/abc197/tasks/abc197_c
N = int(input())
a_arr = list(map(int, input().split()))
ans = 2**30
for bit in range(2**(N - 1)):
    h_or = a_arr[0]
    h_xor = 0
    for i in range(1, N):
        if bit & 1 << (i - 1):
            h_xor ^= h_or
            h_or = 0
        h_or |= a_arr[i]
    h_xor ^= h_or
    ans = min(ans, h_xor)
print(ans)


import time, requests, os, json
import oss2
from dg_ai_platform.utils import get_b64_md5

SERVER_URL = "https://cai.bigwinepot.com"
PULL_TASK =  SERVER_URL + "/Caldron/task/getTask"
UPDATE_TASK = SERVER_URL + "/Caldron/task/updateTask"
COMMIT_TASK = SERVER_URL + "/Caldron/task/finishTask"
GET_CONF = SERVER_URL + "/Caldron/task/signedUrl"

STATE_DOWNLOAD_DONE = 1
STATE_INFERENCE_DONE = 2
STATE_TASK_DONE = 7

class ITaskProcess:
    def inference(self, input_list, output_list, options=None):
        raise RuntimeError('You need overwrite inference function')

class CaldronAI:
    def __init__(self, task_class, pid, public_key, output_dir='output'):
        self.pid = pid
        self.p_key = public_key
        self.output_dir = output_dir
        self.temp_dir = '.dg_ai_temp'
        if not os.path.exists(self.output_dir):
            os.mkdir(self.output_dir)
        if not os.path.exists(self.temp_dir):
            os.mkdir(self.temp_dir)
        # func = getattr(task_obj, 'inference')
        # if func == None:
        #     raise RuntimeError('Class must be have a function with name inference')
        # else:
        #     self.task_obj = task_obj
        if task_class:
            if issubclass(task_class, ITaskProcess):
                self.task_obj = task_class()
            else:
                raise RuntimeError('Class must be implementation ITaskProcess')
        anonymous_auth = oss2.AnonymousAuth()
        self.host = 'https://oss-cn-zhangjiakou.aliyuncs.com'
        self.bucket_name = 'openapi-ai'
        self.bucket = oss2.Bucket(anonymous_auth, self.host, self.bucket_name)
        self.host_url = self.host.replace('https://', f'https://{self.bucket_name}.')

    def run(self):
        while True:
            self.pull_task()
            time.sleep(5)

    def pull_task(self):
        b64, md5_s = get_b64_md5(PULL_TASK, None, self.p_key)
        headers = {"token":self.pid, "md5":md5_s}
        try:
            response = requests.post(PULL_TASK, data=b64, headers=headers)
            task_data = response.json()
            # print(task_data)
            if ('code' in task_data) and (task_data['code'] != -1) and (task_data['code'] != 1):
                input_list = task_data['data']['inputs']
                task_id = task_data['data']['id']
                options = task_data['data']['options']
                self.file_download(task_id, input_list)
                output_types = task_data['data']['output_types']
                output_num = len(output_types)
                output_list = []
                local_input_list = self.get_input_local(task_id, input_list)
                d_outputs = None
                for i in range(output_num):
                    if output_types[i] == '1':
                        output_types[i] = 'jpg'
                    elif output_types[i] == '2':
                        output_types[i] = 'mp4'
                    elif output_types[i] == '3':
                        output_types[i] = 'mp3'
                    elif output_types[i] == '4':
                        output_types[i] = 'txt'
                    output_fn = os.path.join(self.output_dir, f"{task_id}_{i}.{output_types[i]}")
                    output_list.append(output_fn)
                try:
                    d_outputs = self.task_obj.inference(local_input_list, output_list, options)
                except Exception as e:
                    print('inference exception:', e)
                self.update_task_state(task_id, STATE_INFERENCE_DONE)
                try:
                    if d_outputs is not None:
                        output_list.extend(d_outputs)
                    output_urls, size_list = self.file_upload(output_list)
                    self.clean_local_cache(local_input_list, output_list)
                    self.task_done(task_id, output_urls, size_list)
                except Exception as e:
                    print("upload state error:", e)
            else:
                print('no task')
        except ConnectionError as e:
            print('ConnectionError:', e)
        except json.decoder.JSONDecodeError as e:
            print("JSONDecodeError:", e)

    def file_download(self, id, inputs):
        try:
            for fn, i in zip(inputs, range(len(inputs)) ):
                fn = fn["url"]
                ext = os.path.splitext(fn)[1]
                local_fn = os.path.join(self.temp_dir, f"{id}_{i}{ext}")
                if not os.path.exists(local_fn):
                    print('download file:', fn, local_fn)
                    req = requests.get(fn)
                    with open(local_fn, 'wb') as file:
                        file.write(req.content)
                else:
                    print('file was exists')
            self.update_task_state(id, STATE_DOWNLOAD_DONE)
        except Exception as e:
            print(e)

    def file_upload(self, outputs):
        keys = []
        for fn in outputs:
            keys.append(f'{os.path.basename(fn)}')
        # p_data = {"pid": self.pid, "filenames":keys}
        p_data = {"url":keys}
        # b64 = get_b64(json.dumps(p_data))
        b64, md5_s = get_b64_md5(GET_CONF, p_data, self.p_key)
        headers = {"token":self.pid, "md5":md5_s}
        response = requests.post(GET_CONF, data=b64, headers=headers)
        sign_urls = response.json()['data']
        file_urls = []
        file_size_list = []
        for fn, sign_url in zip(outputs, sign_urls):
            f_size = os.path.getsize(fn)
            server_url = sign_url[:sign_url.rfind('?')]
            print('upload to cloud', fn, '-->', server_url)
            try:
                # self.bucket.put_object_from_file(target_path, fn)
                self.bucket.put_object_with_url_from_file(sign_url, fn)
                # if (self.bucket.object_exists(target_path)):
                file_urls.append(server_url)
                file_size_list.append(f_size)
            except Exception as e:
                print('upload error:', e)
        if len(file_urls) != len(outputs):
            raise RuntimeError('Some file upload failed.')
        return file_urls, file_size_list

    def clean_local_cache(self, local_inputs, outputs):
        print('clean local files')
        for fn in local_inputs:
            if os.path.exists(fn):
                os.remove(fn)
        for fn in outputs:
            if os.path.exists(fn):
                os.remove(fn)

    def get_input_local(self, id, inputs):
        local_inputs = []
        try:
            for fn, i in zip(inputs, range(len(inputs))):
                fn = fn["url"]
                ext = os.path.splitext(fn)[1]
                local_inputs.append( os.path.join(self.temp_dir, f"{id}_{i}{ext}") )
        except Exception as e:
            print("get_input_local: ", e)
        return local_inputs

    def update_task_state(self, task_id, state_code):
        p_data = {"id": task_id, "status":state_code}
        b64, md5_s = get_b64_md5(UPDATE_TASK, p_data, self.p_key)
        headers = {"token":self.pid, "md5":md5_s}
        try:
            response = requests.post(UPDATE_TASK, data=b64, headers=headers)
            result_code = response.text
            print("update_task_state:", result_code)
        except Exception as e:
            print(e)

    def task_done(self, task_id, output_urls, size_list):
        print('task_done:', output_urls)
        # p_data = {"pid":self.pid, "taskid": task_id, "phase":STATE_TASK_DONE, "output_url": output_urls }
        p_data = {"id": task_id, "status":STATE_TASK_DONE, "url": output_urls , "TaskOutputParameterSize":size_list}
        # b64 = get_b64(json.dumps(p_data))
        b64, md5_s = get_b64_md5(COMMIT_TASK, p_data, self.p_key)
        headers = {"token": self.pid, "md5": md5_s}
        try:
            response = requests.post(COMMIT_TASK, data=b64, headers=headers)
            result_code = response.text
            print(result_code)
        except Exception as e:
            print(e)


import pytest

import numpy as np
from xarray import DataArray

from analysis.analyse import check_temperature, reactor_survived

class TestMeltingTemperature:
    def test_melted(self):
        melted = check_temperature(3500)
        assert melted

    def test_not_melted(self):
        melted = check_temperature(1500)
        assert not melted

    def test_unphysical_temperature(self):
        with pytest.raises(ValueError):
            check_temperature(-100)

@pytest.fixture()
def example_temp_data():
    temp = DataArray(
        name='T',
        data=np.linspace(250, 3500, 10), dims=['time'],
        coords={'time': ('time', np.linspace(1, 10, 10))}
    )
    return temp

def test_reactor_survival(example_temp_data):
    survived = reactor_survived(example_temp_data)
    assert not survived


import collections
import configparser
import pathlib
from typing import Any

# Support parsing duplicated options in inf
# https://stackoverflow.com/a/15848928/1592410
class KeyCaseInsensitiveValueExtendedDict(collections.OrderedDict):
    def __setitem__(self, key: str, value: Any) -> None:
        if isinstance(value, list) and key in self:
            self[key].extend(value)
            return
        super().__setitem__(key, value)

    def __contains__(self, key: str) -> bool:
        k: str
        for k in self.keys():
            if k.lower() != key.lower():
                continue
            return super().__contains__(k)
        return False

    def __getitem__(self, key: str) -> Any:
        k: str
        for k in self.keys():
            if k.lower() != key.lower():
                continue
            return super().__getitem__(k)

def parse_inf(inf_path: pathlib.Path) -> configparser.ConfigParser:
    config: configparser.ConfigParser = configparser.ConfigParser(
        allow_no_value=True, dict_type=KeyCaseInsensitiveValueExtendedDict, strict=False)
    config.optionxform = str
    config.read(inf_path, encoding='utf-16')
    return config

def get_cat(inf_path: pathlib.Path) -> pathlib.Path:
    inf_content: configparser.ConfigParser = parse_inf(inf_path)
    return inf_path.parent / inf_content['Version'].get('CatalogFile')

def get_bundled_files(inf_path: pathlib.Path) -> set[pathlib.Path]:
    res: set[pathlib.Path] = set()
    inf_content: configparser.ConfigParser = parse_inf(inf_path)

    if 'SourceDisksFiles' not in inf_content:
        return res

    key: str
    for key in inf_content['SourceDisksFiles']:
        src_file: str = key
        res.add(inf_path.parent / src_file)

    return res

def get_version(self) -> str:
    inf_content: configparser.ConfigParser = parse_inf(list(self.driver_dir.rglob('*.inf'))[-1])
    return '.'.join(inf_content['Version'].get('DriverVer').split(',')[1:]).strip()


import datetime
import json
import webbrowser
from shutil import which

from qgis.PyQt.QtWidgets import (QAction,
                             QDialog,
                             QApplication,
                             QMenu,
                             QMessageBox,
                             QDialogButtonBox,
                             QInputDialog,
                             QAbstractButton)
from qgis.PyQt.QtGui import QIcon, QTextDocument
from qgis.PyQt.QtCore import QSizeF, QPointF, Qt

from qgis.core import (QgsProject,
                       QgsVectorLayer,
                       QgsTextAnnotation,
                       QgsMapLayerProxyModel)
from qgis.gui import QgsMapCanvasAnnotationItem
import processing

from . import resources_rc, trace_attributes_gui

from .. import RESOURCE_PREFIX, PLUGIN_NAME, DEFAULT_COLOR, __version__, __email__, __web__, __help__
from ..utils import exceptions, maptools, logger, configmanager, transform
from ..common import client, directions_core, isochrones_core, matrix_core, gravity_core, trace_attributes_core
from ..gui import directions_gui, isochrones_gui, matrix_gui, locate_gui, identify_gui
from ..gui.common_gui import get_locations

from .ValhallaDialogUI_ui import Ui_ValhallaDialogBase
from .ValhallaDialogConfig import ValhallaDialogConfigMain
from .ValhallaDialogLocate import ValhallaDialogLocateMain
from .ValhallaExtraParamsDialog import ValhallaDialogExtraParams

def on_config_click(parent):
    """Pop up provider config window. Outside of classes because it's accessed by multiple dialogs.

    :param parent: Sets parent window for modality.
    :type parent: QDialog
    """
    config_dlg = ValhallaDialogConfigMain(parent=parent)
    config_dlg.exec_()

def on_help_click():
    """Open help URL from button/menu entry."""
    webbrowser.open(__help__)

def on_about_click(parent):
    """Slot for click event of About button/menu entry."""

    info = 'Provides access to Valhalla routing functionalities.

' \
           '' \
           '

' \
           '' \
           'Author: Nils Nolde
' \
           'Email: <a href="mailto:Nils Nolde <{1}>">{1}
' \
           'Web: {2}
' \
           'Repo: github.com/gis-ops/valhalla-qgis-plugin
' \
           'Version: {3}'.format(DEFAULT_COLOR, __email__, __web__, __version__)

    QMessageBox.information(
        parent,
        'About {}'.format(PLUGIN_NAME),
        info
    )

class ValhallaDialogMain:
    """Defines all mandatory QGIS things about dialog."""

    def __init__(self, iface):
        """

        :param iface: the current QGIS interface
        :type iface: Qgis.Interface
        """
        self.iface = iface
        self.project = QgsProject.instance()

        self.first_start = True
        # Dialogs
        self.dlg = None
        self.menu = None
        self.actions = None

    def initGui(self):
        """Called when plugin is activated (on QGIS startup or when activated in Plugin Manager)."""

        def create_icon(f):
            """
            internal function to create action icons

            :param f: file name of icon.
            :type f: str

            :returns: icon object to insert to QAction
            :rtype: QIcon
            """
            return QIcon(RESOURCE_PREFIX + f)

        icon_plugin = create_icon('icon_valhalla.png')

        self.actions = [
            QAction(
                icon_plugin,
                PLUGIN_NAME,  # tr text
                self.iface.mainWindow()  # parent
            ),
            # Config dialog
            QAction(
                create_icon('icon_settings.png'),
                'Provider Settings',
                self.iface.mainWindow()
            ),
            # About dialog
            QAction(
                create_icon('icon_about.png'),
                'About',
                self.iface.mainWindow()
            ),
            # Help page
            QAction(
                create_icon('icon_help.png'),
                'Help',
                self.iface.mainWindow()
            )

        ]

        # Create menu
        self.menu = QMenu(PLUGIN_NAME)
        self.menu.setIcon(icon_plugin)
        self.menu.addActions(self.actions)

        # Add menu to Web menu and make sure it exsists and add icon to toolbar
        self.iface.addPluginToWebMenu("_tmp", self.actions[2])
        self.iface.webMenu().addMenu(self.menu)
        self.iface.removePluginWebMenu("_tmp", self.actions[2])
        self.iface.addWebToolBarIcon(self.actions[0])

        # Connect slots to events
        self.actions[0].triggered.connect(self._init_gui_control)
        self.actions[1].triggered.connect(lambda: on_config_click(parent=self.iface.mainWindow()))
        self.actions[2].triggered.connect(lambda: on_about_click(parent=self.iface.mainWindow()))
        self.actions[3].triggered.connect(on_help_click)

    def unload(self):
        """Called when QGIS closes or plugin is deactivated in Plugin Manager"""

        self.iface.webMenu().removeAction(self.menu.menuAction())
        self.iface.removeWebToolBarIcon(self.actions[0])
        QApplication.restoreOverrideCursor()
        del self.dlg

    def _cleanup_annotations(self):
        """When window is closed without calculating, clean up annotations"""

        if hasattr(self, 'dlg'):
            for a in self.dlg.annotations:
                self.project.annotationManager().removeAnnotation(a)
            self.dlg.annotations = []

    def _init_gui_control(self):
        """Slot for main plugin button. Initializes the GUI and shows it."""

        # Only populate GUI if it's the first start of the plugin within the QGIS session
        # If not checked, GUI would be rebuilt every time!
        if self.first_start:
            self.first_start = False
            self.dlg = ValhallaDialog(self.iface, self.iface.mainWindow())  # setting parent enables modal view
            # Make sure plugin window stays open when OK is clicked by reconnecting the accepted() signal
            self.dlg.global_buttons.accepted.disconnect(self.dlg.accept)
            self.dlg.global_buttons.accepted.connect(self.run_gui_control)
            self.dlg.global_buttons.rejected.connect(self._cleanup_annotations)
            self.dlg.avoidlocation_dropdown.setFilters(QgsMapLayerProxyModel.PointLayer)
            self.dlg.avoidpolygons_dropdown.setFilters(QgsMapLayerProxyModel.PolygonLayer)

            providers = configmanager.read_config()['providers']
            self.dlg.provider_combo.clear()
            for provider in providers:
                self.dlg.provider_combo.addItem(provider['name'], provider)

        # Populate provider box on window startup, since can be changed from multiple menus/buttons

        self.dlg.show()

    def run_gui_control(self):
        """Slot function for OK button of main dialog."""

        self.dlg: ValhallaDialog

        # Associate annotations with map layer, so they get deleted when layer is deleted
        for annotation in self.dlg.annotations:
            # Has the potential to be pretty cool: instead of deleting, associate with mapLayer, you can change order after optimization
            # Then in theory, when the layer is remove, the annotation is removed as well
            # Doesng't work though, the annotations are still there when project is re-opened
            # annotation.setMapLayer(layer_out)
            self.project.annotationManager().removeAnnotation(annotation)
        self.dlg.annotations = []

        provider_id = self.dlg.provider_combo.currentIndex()
        provider = configmanager.read_config()['providers'][provider_id]

        # if there are no coordinates, throw an error message
        if not self.dlg.routing_fromline_list.count():
            QMessageBox.critical(
                self.dlg,
                "No waypoints",
                """
                Did you forget to set routing waypoints?

                Use the 'Add Waypoint' button to add up to 20 waypoints.
                """
            )
            return

        # if no API key is present, when ORS is selected, throw an error message
        if not provider['key'] and provider['base_url'].startswith('https://api.openrouteservice.org'):
            QMessageBox.critical(
                self.dlg,
                "Missing API key",
                """
                Did you forget to set an API key for Mapbox?

                If you don't have an API key, please visit https://account.mapbox.com/auth/signup/?route-to="/" to get one.

                Then enter the API key in Web ► Valhalla ► Provider Settings or the settings symbol in the main Valhalla GUI, next to the provider dropdown.
                """
            )
            return

        clnt = client.Client(provider)
        clnt_msg = ''

        method = self.dlg.routing_method.currentText()
        profile = self.dlg.routing_travel_combo.currentText()
        params = {}
        # Add extra params
        extra_params_text = self.dlg.dlg_params.extra_params_text.toPlainText()
        extra_params = {}
        if extra_params_text:
            extra_params = json.loads(extra_params_text)
        # get the timing info
        time_params = dict()
        if self.dlg.routing_time_options_group.isChecked():
            date_time = datetime.datetime.now().replace(second=0, microsecond=0).isoformat(timespec="minutes")
            py_date = self.dlg.datetime_date.dateTime().toPyDateTime()
            py_time = self.dlg.datetime_time.dateTime().toPyDateTime()
            time_type = 0  # right now, ie realtime
            if self.dlg.datetime_departure.isChecked():
                time_type = 1
                date_time = py_date.strftime("%Y-%m-%d") + "T" + py_time.strftime("%H:%M")
            elif self.dlg.datetime_arrival.isChecked():
                time_type = 2
                date_time = py_date.strftime("%Y-%m-%d") + "T" + py_time.strftime("%H:%M")
            time_params = {"date_time": {
                "type": time_type,
                "value": date_time
            }}
        try:
            if method == 'route':
                layer_out = QgsVectorLayer("LineString?crs=EPSG:4326", f"Route {profile.capitalize()}", "memory")
                layer_out.dataProvider().addAttributes(directions_core.get_fields())
                layer_out.updateFields()

                directions = directions_gui.Directions(self.dlg)
                params = directions.get_parameters()
                params.update(extra_params)
                params.update(time_params)
                response = clnt.request('/route', {}, post_json=params)
                feat = directions_core.get_output_feature_directions(
                    response,
                    profile,
                    directions.costing_options,
                    "{}, {}".format(params['locations'][0]['lon'], params['locations'][0]['lat']),
                    "{}, {}".format(params['locations'][-1]['lon'], params['locations'][-1]['lat'])
                )
                layer_out.dataProvider().addFeature(feat)
                layer_out.updateExtents()
                self.project.addMapLayer(layer_out)

            elif method == 'isochrone':
                geometry_type = self.dlg.polygons.currentText()
                isochrones = isochrones_core.Isochrones()
                isochrones.set_parameters(profile, geometry_type)
                locations = get_locations(self.dlg.routing_fromline_list)

                aggregate = self.dlg.iso_aggregate.isChecked()
                locations = [locations] if aggregate else locations

                no_points = self.dlg.iso_no_points.isChecked()

                metrics = []
                if self.dlg.contours_distance.text():
                    metrics.append('distance')
                if self.dlg.contours.text():
                    metrics.append('time')

                for metric in metrics:
                    isochrones_ui = isochrones_gui.Isochrones(self.dlg)
                    params = isochrones_ui.get_parameters(metric)  # change once isodistances are there too
                    params.update(extra_params)
                    params.update(time_params)

                    name = 'Isodistance' if metric == 'distance' else 'Isochrone'
                    layer_out = QgsVectorLayer(f"{geometry_type}?crs=EPSG:4326", f"{name} {params['costing']}", "memory")
                    layer_out.dataProvider().addAttributes(isochrones.get_fields())
                    layer_out.updateFields()

                    for i, location in enumerate(locations):
                        params['locations'] = location if aggregate else [location]
                        isochrones.set_response(clnt.request('/isochrone', {}, post_json=params))
                        for feat in isochrones.get_features(str(i), isochrones_ui.costing_options):
                            layer_out.dataProvider().addFeature(feat)

                    layer_out.updateExtents()
                    # isochrones.stylePoly(layer_out, metric)
                    self.project.addMapLayer(layer_out)

                if not no_points:
                    multipoint_layer = QgsVectorLayer("MultiPoint?crs=EPSG:4326", f"Snapped Points {params['costing']}", "memory")
                    point_layer = QgsVectorLayer("Point?crs=EPSG:4326", f"Input Points {params['costing']}", "memory")

                    multipoint_layer.dataProvider().addAttributes(isochrones.get_point_fields())
                    multipoint_layer.updateFields()
                    point_layer.dataProvider().addAttributes(isochrones.get_point_fields())
                    point_layer.updateFields()

                    for feat in isochrones.get_multipoint_features('0'):
                        multipoint_layer.dataProvider().addFeature(feat)
                    for feat in isochrones.get_point_features('0'):
                        point_layer.dataProvider().addFeature(feat)
                    multipoint_layer.updateExtents()
                    point_layer.updateExtents()
                    self.project.addMapLayer(multipoint_layer)
                    self.project.addMapLayer(point_layer)

            elif method == 'sources_to_targets':
                layer_out = QgsVectorLayer("None", 'Matrix_Valhalla', "memory")
                layer_out.dataProvider().addAttributes(matrix_core.get_fields())
                layer_out.updateFields()

                matrix = matrix_gui.Matrix(self.dlg)
                params = matrix.get_parameters()
                params.update(extra_params)
                response = clnt.request('/sources_to_targets', post_json=params)
                feats = matrix_core.get_output_features_matrix(
                    response,
                    profile,
                    matrix.costing_options
                )
                for feat in feats:
                    layer_out.dataProvider().addFeature(feat)

                self.project.addMapLayer(layer_out)

            elif method == 'locate':
                locate_dlg = ValhallaDialogLocateMain()
                locate_dlg.setWindowTitle('Locate Response')

                locate = locate_gui.Locate(self.dlg)
                params = locate.get_parameters()
                params.update(extra_params)
                response = clnt.request('/locate', post_json=params)

                locate_dlg.responseArrived.emit(json.dumps(response, indent=4))

                locate_dlg.exec_()

            elif method == 'extract-osm':
                if not which('osmium'):
                    QMessageBox.critical(
                        self.dlg,
                        "ModuleNotFoundError",
                        """osmium wasn\'t found in your PATH.

Please install before trying again."""
                    )
                    return
                if not self.dlg.pbf_file.filePath():
                    QMessageBox.critical(
                        self.dlg,
                        "FileNotFoundError",
                        """Seems like you forgot to set a PBF file path in the configuration for the Identity tool."""
                    )
                    return

                identify = identify_gui.Identify(self.dlg)
                params = identify.get_locate_parameters()
                response = clnt.request('/locate', post_json=params)
                way_dict = identify.get_tags(response)

                for way_id in way_dict:
                    way = way_dict[way_id]

                    layer_out = QgsVectorLayer("LineString?crs=EPSG:4326", "Way " + str(way_id), "memory")
                    layer_out.dataProvider().addAttributes(identify.get_fields(way["tags"]))
                    layer_out.updateFields()

                    feat = identify.get_output_feature(way)
                    layer_out.dataProvider().addFeature(feat)
                    layer_out.updateExtents()

                    self.project.addMapLayer(layer_out)
            elif method == 'centroid [experimental]':
                layer_routes = QgsVectorLayer("LineString?crs=EPSG:4326", f"Centroid Routes {profile}", "memory")
                layer_gravity = QgsVectorLayer("Point?crs=EPSG:4326", f"Centroid Point {profile}", "memory")
                layer_routes.dataProvider().addAttributes(gravity_core.get_fields())
                layer_gravity.dataProvider().addAttributes(gravity_core.get_fields())
                layer_routes.updateFields()
                layer_gravity.updateFields()

                directions = directions_gui.Directions(self.dlg)
                params = directions.get_parameters()
                params.update(extra_params)
                response = clnt.request('/centroid', {}, post_json=params)
                line_feats, point_feat = gravity_core.get_output_feature_gravity(
                    response,
                    profile,
                    directions.costing_options
                )
                layer_routes.dataProvider().addFeatures(line_feats)
                layer_gravity.dataProvider().addFeature(point_feat)

                layer_routes.updateExtents()
                layer_gravity.updateExtents()

                self.project.addMapLayer(layer_routes)
                self.project.addMapLayer(layer_gravity)

            elif method == 'trace_attributes':
                layer_edges = QgsVectorLayer("LineString?crs=EPSG:4326", f"Trace Edges {profile}", "memory")
                layer_points = QgsVectorLayer("Point?crs=EPSG:4326", f"Trace Points {profile}", "memory")
                layer_edges.dataProvider().addAttributes(trace_attributes_core.get_fields('edge'))
                layer_points.dataProvider().addAttributes(trace_attributes_core.get_fields('point'))
                layer_edges.updateFields()
                layer_points.updateFields()

                trace_attributes = trace_attributes_gui.TraceAttributes(self.dlg)
                params = trace_attributes.get_parameters()
                params.update(extra_params)

                response = clnt.request('/trace_attributes', {}, post_json=params)
                edge_feats, point_feats = trace_attributes_core.get_output_features(response)

                layer_edges.dataProvider().addFeatures(edge_feats)
                layer_points.dataProvider().addFeatures(point_feats)
                layer_edges.updateExtents()
                layer_points.updateExtents()

                self.project.addMapLayer(layer_edges)
                self.project.addMapLayer(layer_points)

        except exceptions.Timeout as e:
            msg = "The connection has timed out!"
            logger.log(msg, 2)
            self.dlg.debug_text.setText(msg)
            self._display_error_popup(e)
            return

        except (exceptions.ApiError,
                exceptions.InvalidKey,
                exceptions.GenericServerError) as e:
            msg = (e.__class__.__name__,
                   str(e))

            logger.log("{}: {}".format(*msg), 2)
            clnt_msg += "{}: ({})
".format(*msg)
            self._display_error_popup(e)
            return

        except Exception as e:
            msg = [e.__class__.__name__ ,
                   str(e)]
            logger.log("{}: {}".format(*msg), 2)
            clnt_msg += "{}: {}
".format(*msg)
            self._display_error_popup(e)
            raise

        finally:
            # Set URL in debug window
            clnt_msg += '{0}
Parameters:
{1}
timing: {2:.3f} secs'.format(clnt.url, json.dumps(params, indent=2), clnt.response_time)
            self.dlg.debug_text.setHtml(clnt_msg)

    def _display_error_popup(self, e):
        QMessageBox.critical(
            self.dlg,
            e.__class__.__name__,
            str(e)
        )

class ValhallaDialog(QDialog, Ui_ValhallaDialogBase):
    """Define the custom behaviour of Dialog"""

    def __init__(self, iface, parent=None):
        """
        :param iface: QGIS interface
        :type iface: QgisInterface

        :param parent: parent window for modality.
        :type parent: QDialog/QApplication
        """
        QDialog.__init__(self, parent)
        self.setupUi(self)

        self._iface = iface
        self._mapCanvas = self._iface.mapCanvas()
        self.project = QgsProject.instance()  # invoke a QgsProject instance

        # Set things around the custom map tool
        self.line_tool = None
        self.last_maptool = None
        self.annotations = []

        # Change OK and Cancel button names
        self.global_buttons.button(QDialogButtonBox.Ok).setText('Apply')
        self.global_buttons.button(QDialogButtonBox.Cancel).setText('Close')

        # Add extra params dialog
        self.dlg_params = ValhallaDialogExtraParams(parent=self)

        #### Set up signals/slots ####

        # Config/Help dialogs
        self.provider_config.clicked.connect(lambda: on_config_click(self))
        self.help_button.clicked.connect(on_help_click)
        self.about_button.clicked.connect(lambda: on_about_click(parent=self._iface.mainWindow()))
        self.provider_refresh.clicked.connect(self._on_prov_refresh_click)
        self.time_button_group.buttonToggled.connect(self._on_time_button_toggle)

        # Routing tab
        self.routing_fromline_map.clicked.connect(self._on_linetool_init)
        self.routing_fromline_clear.clicked.connect(self._on_clear_listwidget_click)
        self.routing_import_locations.clicked.connect(self._on_import_locations_click)

        # Extra params
        self.extra_params_button.clicked.connect(self.dlg_params.exec_)

    def _on_time_button_toggle(self, button: QAbstractButton, is_checked: bool):
        if button.objectName() == "datetime_now" and is_checked:
            self.datetime_date.setEnabled(False)
            self.datetime_time.setEnabled(False)
        elif button.objectName() != "datetime_now" and is_checked:
            self.datetime_date.setEnabled(True)
            self.datetime_time.setEnabled(True)

    def _on_import_locations_click(self):
        """Imports valhalla locations as JSON to location table input"""
        raw_json, ok = QInputDialog.getText(
            self,
            'Import Valhalla locations',
            'Valhalla locations as JSON array',
        )
        if ok:
            j = json.loads(raw_json)
            self.routing_fromline_list.clear()
            for idx, loc in enumerate(j):
                self.routing_fromline_list.addItem("Point {0}: {1:.6f}, {2:.6f}".format(idx, loc['lon'], loc['lat']))

    def _on_prov_refresh_click(self):
        """Populates provider dropdown with fresh list from config.yml"""

        providers = configmanager.read_config()['providers']
        self.provider_combo.clear()
        for provider in providers:
            self.provider_combo.addItem(provider['name'], provider)

    def _on_clear_listwidget_click(self):
        """Clears the contents of the QgsListWidget and the annotations."""
        items = self.routing_fromline_list.selectedItems()
        if items:
            # if items are selected, only clear those
            for item in items:
                row = self.routing_fromline_list.row(item)
                self.routing_fromline_list.takeItem(row)
                if self.annotations:
                    self.project.annotationManager().removeAnnotation(self.annotations.pop(row))
        else:
            # else clear all items and annotations
            self.routing_fromline_list.clear()
            self._clear_annotations()

    def _linetool_annotate_point(self, point, idx):
        annotation = QgsTextAnnotation()

        c = QTextDocument()
        html = "" + str(idx) + ""
        c.setHtml(html)

        annotation.setDocument(c)

        annotation.setFrameSizeMm(QSizeF(5,8))
        annotation.setFrameOffsetFromReferencePointMm(QPointF(5, 5))
        annotation.setMapPosition(point)
        annotation.setMapPositionCrs(self._mapCanvas.mapSettings().destinationCrs())

        return QgsMapCanvasAnnotationItem(annotation, self._iface.mapCanvas()).annotation()

    def _clear_annotations(self):
        """Clears annotations"""
        for annotation in self.annotations:
            if annotation in self.project.annotationManager().annotations():
                self.project.annotationManager().removeAnnotation(annotation)
        self.annotations = []

    def _on_linetool_init(self):
        """Hides GUI dialog, inits line maptool and add items to line list box."""
        self.hide()
        self.routing_fromline_list.clear()
        # Remove all annotations which were added (if any)
        self._clear_annotations()

        self.last_maptool = self._iface.mapCanvas().mapTool()

        self.line_tool = maptools.LineTool(self._iface.mapCanvas())
        self._iface.mapCanvas().setMapTool(self.line_tool)
        self.line_tool.pointDrawn.connect(lambda point, idx: self._on_linetool_map_click(point, idx))
        self.line_tool.doubleClicked.connect(self._on_linetool_map_doubleclick)

    def _on_linetool_map_click(self, point, idx):
        """Adds an item to QgsListWidget and annotates the point in the map canvas"""

        transformer = transform.transformToWGS(self._mapCanvas.mapSettings().destinationCrs())
        point_wgs = transformer.transform(point)
        self.routing_fromline_list.addItem("Point {0}: {1:.6f}, {2:.6f}".format(idx, point_wgs.x(), point_wgs.y()))

        annotation = self._linetool_annotate_point(point, idx)
        self.annotations.append(annotation)
        self.project.annotationManager().addAnnotation(annotation)

    def _on_linetool_map_doubleclick(self):
        """
        Populate line list widget with coordinates, end line drawing and show dialog again.

        :param points_num: number of points drawn so far.
        :type points_num: int
        """

        self.line_tool.pointDrawn.disconnect()
        self.line_tool.doubleClicked.disconnect()
        QApplication.restoreOverrideCursor()
        self.show()
        self._iface.mapCanvas().setMapTool(self.last_maptool)


#!/usr/bin/env python
# coding: utf-8

#Imports
import pandas as pd

#Set the input variable
sample_input1 = '''8
2017-01-03,16:18:50,AAPL,142.64
2017-01-03,16:25:22,AMD,13.86
2017-01-03,16:25:25,AAPL,141.64
2017-01-03,16:25:28,AMZN,845.61
2017-01-03,16:28:50,AAPL,140.64
2017-01-03,16:29:59,FB,140.34
2017-01-04,16:29:32,AAPL,143.64
2017-01-04,16:30:50,AAPL,141.64'''

sample_input2 = '''10
2017-01-03,16:18:50,AAPL,142.64
2017-01-03,16:25:22,AMD,13.80
2017-01-03,16:25:22,AMD,13.88
2017-01-03,16:25:22,AMD,13.86
2017-01-03,16:25:25,AAPL,141.64
2017-01-03,16:25:28,AMZN,845.61
2017-01-03,16:28:50,AAPL,140.64
2017-01-03,16:29:59,FB,140.34
2017-01-04,16:29:32,AAPL,143.64
2017-01-04,16:30:50,AAPL,141.64'''

def pre_process_to_df(input_text):
    #Drop the first line which is not required
    sample_input_trimmed = input_text[input_text.find('\n')+1:]
    df = pd.DataFrame([x.split(',') for x in sample_input_trimmed.split('\n')])
    df.columns = ['Date','Time','Symbol','Price']
    df['DateTime'] = df['Date'] + ' ' + df['Time']
    df['DateTime'] =  pd.to_datetime(df['DateTime'], format='%Y-%m-%d %H:%M:%S')
    df.drop(columns=['Date', 'Time'])
    #Drop the transactions out of 09:30:00 hrs and 16:30:00 trading window
    df = (df.set_index('DateTime')
            .between_time('09:30:00', '16:30:00')
            .reset_index()
            .reindex(columns=df.columns))
    df = df.sort_values(by=['DateTime'])
    return df

def calculate_day_wise_ticker(df):
    day_wise_transactions = [v for k, v in df.groupby(pd.Grouper(key='DateTime',freq='D'))]
    for i in day_wise_transactions:
        i = i.sort_values(by=['Symbol'])
        print('Trading Day = ',str(i.DateTime.max())[:10])
        print('Last Quote Time = ',str(i.DateTime.max())[11::])
        print('Number of valid quotes = ',i.shape[0])
        print('Most active hour = ', i.DateTime.dt.hour.mode()[0])
        print('Most active symbol = ',i.Symbol.mode()[0])
        day_symbol_wise_transactions = [v for k, v in i.groupby(pd.Grouper(key='Symbol'))]
        for j in day_symbol_wise_transactions:
            print(', '.join(str(x) for x in [j.DateTime.max().strftime("%Y-%m-%d %H:%M:%S"),j.Symbol.unique()[0],j.Price.max(),j.Price.min()]))

if __name__ == "__main__":
    df_processed = pre_process_to_df(sample_input1)
    calculate_day_wise_ticker(df_processed)


## This is intended to be run from Azure Devops as a test of succesfull docker deployment
import json
import numpy as np

def test():
# find 30 random samples from test set
    n = 30
    sample_indices = np.random.permutation(X_test.shape[0])[0:n]

    test_samples = json.dumps({"data": X_test[sample_indices].tolist()})
    test_samples = bytes(test_samples, encoding='utf8')

    # predict using the deployed model
    result = service.run(input_data=test_samples)

    # compare actual value vs. the predicted values:
    i = 0
    plt.figure(figsize = (20, 1))

    for s in sample_indices:
        plt.subplot(1, n, i + 1)
        plt.axhline('')
        plt.axvline('')

        # use different color for misclassified sample
        font_color = 'red' if y_test[s] != result[i] else 'black'
        clr_map = plt.cm.gray if y_test[s] != result[i] else plt.cm.Greys

        plt.text(x=10, y =-10, s=result[i], fontsize=18, color=font_color)
        plt.imshow(X_test[s].reshape(28, 28), cmap=clr_map)

        i = i + 1
    plt.show()


#!/usr/bin/env python3

#===============================================================================
# Scrape bbc.com coronoavirus us and canada news articles
#
# Dartmouth College, LING48 / COSC72, Spring 2020
# Anne Bailey, Stephen Crowe, Thanh Nguyen Jr
# Professor Rolando Coto-Solano
#
# This script is based off of the following tutorials:
# https://towardsdatascience.com/web-scraping-news-articles-in-python-9dd605799558
# https://medium.com/ymedialabs-innovation/web-scraping-using-beautiful-soup-and-selenium-for-dynamic-page-2f8ad15efe25
#
# This script uses the BeautifulSoup package. See the documentation here:
# https://www.crummy.com/software/BeautifulSoup/bs4/doc/
#
# This script uses the Selenium package. See the documentation here:
# https://selenium-python.readthedocs.io/
#
# Usage: from terminal run `pipenv run python3 scrape_vox.py`
#===============================================================================

import requests
import time
from bs4 import BeautifulSoup
from selenium import webdriver
import sys

url = "https://www.bbc.com/news/world/us_and_canada"

# Selenium needs to access Chrome browser driver. The options open the browser
# in incognito mode and doesn't actually open a window.
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
options.add_argument('--headless')
# May need to replace chromedriver with path to chromedriver executable
# Download chromedriver executable here: https://chromedriver.chromium.org/downloads
driver = webdriver.Chrome("chromedriver", chrome_options=options)

# Get the webpage
driver.get(url)

# Click the load more button up to number_of_pages_to_load times
number_of_pages_to_load = 30
for i in range(number_of_pages_to_load):
  try:
    load_more_button = driver.find_elements_by_class_name("lx-stream-show-more__button")[0]
    time.sleep(1)
    load_more_button.click()
    time.sleep(1)
    print('Page: ' + str(i))
  except Exception as e:
    print(e)
    break
print("Complete")
page_source = driver.page_source
driver.quit()

soup1 = BeautifulSoup(page_source, 'html5lib')

# News identification
coverpage_news = soup1.find_all(class_='gs-c-promo-heading')
stream_post_news = soup1.find_all('a', class_='lx-stream-post__header-link')

number_of_coverpage_news_articles = len(coverpage_news)
number_of_stream_post_news_articles = len(stream_post_news)
number_of_articles = number_of_coverpage_news_articles + number_of_stream_post_news_articles

print(number_of_coverpage_news_articles)
print(number_of_stream_post_news_articles)

# Empty lists for content, links and titles
news_contents = []
list_links = []
list_titles = []

for n in range(0, number_of_coverpage_news_articles):
    try:
        # We need to ignore "live" pages since they are not articles
        if "live" in coverpage_news[n]['href']:
            continue

        # Getting the link of the article
        link = coverpage_news[n]['href']
        if (link.find("https://") == -1):
            link = "https://www.bbc.com{}".format(link)
        print(link)
        list_links.append(link)

        # Getting the title
        title = coverpage_news[n].get_text()
        list_titles.append(title)

        # Reading the content (it is divided in paragraphs)
        article = requests.get(link)
        article_content = article.content
        soup_article = BeautifulSoup(article_content, 'html5lib')
        body = soup_article.find_all('div', class_='story-body')
        if len(body) == 0: continue
        x = body[0].find_all('p')

        # Unifying the paragraphs
        list_paragraphs = []
        for p in range(0, len(x)):
            paragraph = x[p].get_text()
            list_paragraphs.append(paragraph)
            final_article = " ".join(list_paragraphs)

        news_contents.append(final_article)
    except Exception as e:
        print(e)
        continue

for n in range(0, number_of_stream_post_news_articles):
    try:
        # Getting the link of the article
        link = stream_post_news[n]['href']
        link = "https://www.bbc.com{}".format(link)
        print(link)
        list_links.append(link)

        # Getting the title
        title = stream_post_news[n].get_text()
        list_titles.append(title)

        # Reading the content (it is divided in paragraphs)
        article = requests.get(link)
        article_content = article.content
        soup_article = BeautifulSoup(article_content, 'html5lib')
        body = soup_article.find_all('div', class_='story-body')
        if len(body) == 0: continue
        x = body[0].find_all('p')

        # Unifying the paragraphs
        list_paragraphs = []
        for p in range(0, len(x)):
            paragraph = x[p].get_text()
            list_paragraphs.append(paragraph)
            final_article = " ".join(list_paragraphs)

        news_contents.append(final_article)
    except Exception as e:
        print(e)
        continue

f = open("../central/bbc_" + str(number_of_articles) + ".txt", "w")
f2 = open("../central/central_articles.txt", "a")
for content in news_contents:
    f.write(content)
    f2.write(content)
f.close()
f2.close()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

def solve(N,J):
    numstr = "1" + "0" * (N-1)
    num = int(numstr, 2)

    ans = []
    while True:
        num += 1

        binstr = bin(num)[2:]

        # if binstr.count("1") % 2:
        #     continue
        if binstr[-1] == '0':
            continue

        ok = True
        divisors = []
        for base in range(2,11):
            val = int(binstr, base)
            cands = [2,3,5,7,11]
            for c in cands:
                if val % c == 0:
                    divisors.append(c)
                    break
            else:
                ok = False
                break

        if ok:
            ans.append([binstr] + divisors)
            if len(ans) == J:
                break

            # validation
            for i, base in enumerate(range(2,11)):
                val = int(binstr, base)
                # print("for base {}, {} % {} = {}.".format(base, val, divisors[i], val % divisors[i])
                if val % divisors[i] != 0:
                    print("error!!!")
                    sys.exit(0)

    for a in ans:
        print (' '.join([str(x) for x in a]))

    # for j in jamcoins:
    #     print (' '.join([str(bin(j))[2:], '3', '2', '3', '2', '3', '2', '3', '2', '3']))

T=int(input())
for testcase in range(T):
    N,J = [int(x) for x in input().split()]
    print("Case #{}:".format(testcase+1))
    solve(N,J)


from teacher import Teacher
#create instance
t = Teacher()
#store data into the instance
t.setid(10)
t.setname('Prakash')
t.setaddress('H.No.-29, Neeruganti Street, Anantapur')
t.setsalary(25000.50)
#retrieve data from instance and display
print('id=', t.getid())
print('name=', t.getname())
print('address=', t.getaddress())
print('salary=', t.getsalary())


# https://paiza.jp/works/mondai/a_rank_level_up_problems/a_rank_twopointers_boss
from input3 import _INPUT
import io, sys
sys.stdin = io.StringIO(_INPUT)

N, K = map(int, input().split())
numbers = list(map(int, input().split()))
A = [1] * (N + 1)
for i in range(N):
    if numbers[i] == 0:
        A[i + 1] = 0
        continue
    if A[i] == 0:
        A[i + 1] = numbers[i]
    else:
        A[i + 1] = A[i] * numbers[i]
A = A[1:]

length = float("inf")
start = 0
end = 0

while True:
    if end >= N or start > end:
        break
    if A[end] >= K:
        if end - start + 1 < length:
            length = end - start + 1
        if A[end] // A[start] < K:
            end += 1
        else:
            start += 1
    elif numbers[end] == 0:
        start = end + 1
        end += 1
    else:
        end += 1

print(length)


from specty.vault76.module import Module
from typing import TYPE_CHECKING
from discord import Forbidden, HTTPException, NotFound
from discord.ext.commands import (
    CommandNotFound,
    MissingPermissions,
    NSFWChannelRequired,
    DisabledCommand,
    NotOwner,
    CommandOnCooldown,
    NoPrivateMessage,
    ConversionError,
    BadArgument,
    ChannelNotReadable,
    MessageNotFound,
    RoleNotFound,
    GuildNotFound,
    BadInviteArgument,
    BadColorArgument,
    PartialEmojiConversionFailure,
    EmojiNotFound,
    UserInputError,
    BotMissingPermissions,
    CheckFailure,
    ObjectNotFound,
    BadBoolArgument,
    CommandInvokeError,
    Paginator,
    UserNotFound,
    MemberNotFound,
    ChannelNotFound,
    NotOwner,
)

if TYPE_CHECKING:
    from specty.vault76.bot import SpectralCore

class Events(Module):
    def __init__(self, bot: "SpectralCore"):
        self.bot = bot

    @Module.listener()
    async def on_command_error(self, ctx, error):
        if hasattr(ctx.command, "on_error"):
            return

        elif isinstance(error, CommandNotFound):
            return

        elif isinstance(error, Forbidden):
            return await ctx.error(error)

        elif isinstance(error, NSFWChannelRequired):
            return

        elif isinstance(error, MissingPermissions):
            perms = [f"`{p}`" for p in error.missing_permissions]
            return await ctx.error(
                f"You don't have {', '.join(perms).replace('_', '.')} permission(s)"
            )

        elif isinstance(error, BotMissingPermissions):
            perms = [f"`{p}`" for p in error.missing_permissions]
            return await ctx.error(
                f"I'am missing {', '.join(perms).replace('_', '.')} permissions(s)"
            )

        elif isinstance(error, DisabledCommand):
            return await ctx.error(f"This command is disabled")

        elif isinstance(error, NotOwner):
            return

        elif isinstance(error, CommandOnCooldown):
            await ctx.warn(f"you are on cooldown, try after `{error.retry_after:.2f}s`")

        elif isinstance(error, NoPrivateMessage):
            return

        elif isinstance(error, HTTPException):
            return await ctx.error(str(error))

        elif isinstance(error, CheckFailure):
            return

        elif isinstance(error, NotFound):
            return await ctx.error(
                "provided asset/user was not found, that might be discord skill issue."
            )

        elif isinstance(error, ConversionError):
            return await ctx.error("Provided argument is not valid.")

        if isinstance(error, BadArgument):
            ctx.command.reset_cooldown(ctx)
            if isinstance(error, UserNotFound):
                await ctx.error(f"User not found.")
            elif isinstance(error, MemberNotFound):
                await ctx.error(f"Member not found.")
            elif isinstance(error, ChannelNotFound):
                await ctx.error(f"Channel not found.")
            elif isinstance(error, ChannelNotReadable):
                await ctx.error(
                    f"I don't have permissions to read messages in {error.argument.mention}."
                )
            elif isinstance(error, MessageNotFound):
                await ctx.error(f"Message not found.")
            elif isinstance(error, RoleNotFound):
                await ctx.error(f"Role not found.")
            elif isinstance(error, GuildNotFound):
                await ctx.error(f"Guild not found.")
            elif isinstance(error, BadInviteArgument):
                await ctx.error(f"Invite not found.")
            elif isinstance(
                error,
                (EmojiNotFound, PartialEmojiConversionFailure),
            ):
                await ctx.error(f"Invalid emoji.")
            elif isinstance(error, ObjectNotFound):
                await ctx.error(f"Invalid identificator.")
            elif isinstance(error, BadBoolArgument):
                await ctx.error(f"Not valid boolean option.")
            elif isinstance(error, BadColorArgument):
                await ctx.error(f"Invalid color.")
            else:
                helper = (
                    str(ctx.invoked_subcommand)
                    if ctx.invoked_subcommand
                    else str(ctx.command)
                )
                return await ctx.send_help(helper)

        elif isinstance(error, UserInputError):

            helper = (
                str(ctx.invoked_subcommand)
                if ctx.invoked_subcommand
                else str(ctx.command)
            )
            return await ctx.send_help(helper)

        elif isinstance(error, CheckFailure):
            return
        else:
            if isinstance(error, CommandInvokeError):
                original = error.original
                if "Forbidden" in str(original) or "HTTPException" in str(original):
                    return await ctx.reply(error.original)
                return await ctx.error(original)

async def setup(bot: "SpectralCore") -> None:
    await bot.add_cog(Events(bot))


# -*- coding: utf-8 -*-
"""
Created on Sat Sep 15 18:48:18 2018

@author: Scott
"""

#import packages and data

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import ATUS_Functions as AF
Codes=pd.read_csv("ATUS_Survey/codes.csv")
Activities=pd.read_csv("ATUS_Survey/atusact.csv")
Summary=pd.read_csv("ATUS_Survey/atussum.csv")
Respondent=pd.read_csv("ATUS_Survey/atusresp.csv")
#print(SU.head(10))
N_people=len(Summary["tucaseid"])  #Initialize all dataframes and store number of people as variable
Roster=pd.read_csv("ATUS_Survey/atusrost.csv")

#Split Times by morning sleep (from the previous night), and night sleep

SleepDict1={}
SleepDict2={}
for ind, row in Activities.iterrows():
    if (row["trcodep"]==10101) | (row["trcodep"]==10199):
        if (AF.Hour_Decimal(row["tustarttim"])>=4) & (AF.Hour_Decimal(row["tustarttim"])<=8):
            if row["tucaseid"] in SleepDict1.keys():
                SleepDict1[row["tucaseid"]]+=row["tuactdur24"]
            else:
                SleepDict1[row["tucaseid"]]=row["tuactdur24"]
        else:
            if row["tucaseid"] in SleepDict2.keys():
                SleepDict2[row["tucaseid"]]+=row["tuactdur24"]
            else:
                SleepDict2[row["tucaseid"]]=row["tuactdur24"]

AgeDict={}
for ind,row in Roster.iterrows():
    if (row["tulineno"]==1):
        AgeDict[row["tucaseid"]]=row["teage"]

#Combine day of week and stat weight from Respondent file with Sleep amount from above
#Use morning and night separated sleep

import math

cols=["DayOfWeek","Sleep","Weight","Age"]

SleepList1=[]
SleepList2=[]

for ind, row in Respondent.iterrows():
    if (row["tulineno"]==1 and math.isnan(row["tudiaryday"]) != True) and row["tucaseid"] in SleepDict1.keys():
        SleepList1.append([row["tudiaryday"],SleepDict1[row["tucaseid"]],row["tufnwgtp"],AgeDict[row["tucaseid"]]])
    if (row["tulineno"]==1 and math.isnan(row["tudiaryday"]) != True) and row["tucaseid"] in SleepDict2.keys():
        SleepList2.append([row["tudiaryday"],SleepDict2[row["tucaseid"]],row["tufnwgtp"],AgeDict[row["tucaseid"]]])
SleepData1=pd.DataFrame(SleepList1,columns=cols)
SleepData1["WeightXSleep"]=SleepData1["Weight"]*SleepData1["Sleep"]
print(SleepData1.head())
SleepData2=pd.DataFrame(SleepList2,columns=cols)
SleepData2["WeightXSleep"]=SleepData2["Weight"]*SleepData2["Sleep"]
print(SleepData2.head())

#Pivot all data into bins by age

grouped1A = SleepData1.groupby('Age').sum()
grouped1A['wtdavg'] = grouped1A['WeightXSleep'] / grouped1A['Weight']
print(grouped1A.head())
grouped2A = SleepData2.groupby('Age').sum()
grouped2A['wtdavg'] = grouped2A['WeightXSleep'] / grouped2A['Weight']
print(grouped2A.head())

#Combine morning and night sleep
GroupedSplitA=[]
Remap=[2,3,4,5,6,7,1]
for i in grouped1A["wtdavg"].keys():
    GroupedSplitA.append((grouped1A["wtdavg"][i]+grouped2A["wtdavg"][i])/60)

Window=2
Edges=np.arange(np.ceil(65/Window))*Window+15
Sums=np.zeros((len(Edges)-1,1))
Counts=np.zeros((len(Edges)-1,1))
for ind,i in enumerate(Edges[0:len(Edges)-1]):
    for ind2,age in enumerate(grouped1A["wtdavg"].keys()):
        if (age>=i) & (age<i+Window):
            Sums[ind]+=GroupedSplitA[ind2]
            Counts[ind]+=1
Avgs=Sums/Counts

plt.plot(Edges[0:len(Edges)-1],Avgs)
plt.xlabel('Age')
plt.ylabel('Average Sleep per Night (hrs)')
#plt.savefig("SleepVsAge_W2.png")


import random

from drop_rate import DropRate, DropRates

class Base:
    def __init__(self, time_to_kill: float, drop_rates: DropRates) -> None:
        super().__init__()
        self.kills = 0
        self.time_spent = 0
        self._time_to_kill = time_to_kill
        self._drop_rates = drop_rates
        self.drop_history = []

    def roll(self):
        rand_val = random.uniform(0, 1)

        if rand_val <= self._drop_rates.unique_chance:
            drop = self._drop_rates.roll_unique()
        else:
            drop = "Nothing"

        self.drop_history.append(drop)

class Leviathan(Base):
    def __init__(self) -> None:
        time_to_kill = 120
        drop_rates = self._get_drop_rates()
        super().__init__(time_to_kill, drop_rates)

    @staticmethod
    def _get_drop_rates() -> DropRates:
        drops=  [
            DropRate("Awakener's orb", 1 / 53),
            DropRate("Smoke quartz", 1 / 180),
            DropRate("Chromium ingot", 1 / 320),
            DropRate("Venator vestige (roll)", 1 / 960 * 3),
            DropRate("Leviathan's lure", 1 / 960),
            DropRate("Virtus mask", 1 / 2880),
            DropRate("Virtus robe top", 1 / 2880),
            DropRate("Virtus robe bottom", 1 / 2880)
            ]
        return DropRates(drops)

def boss_factory(boss_name: str) -> Base:
    bosses = {
        "Leviathan": Leviathan
        }

    if boss_name not in bosses:
        raise ValueError(f"Invalid boss name: {boss_name}")

    return bosses[boss_name]()

if __name__ == "__main__":
    boss = boss_factory("Leviathan")

    for _ in range(100):
        boss.roll()
    print(boss.drop_history)


import os
import cv2

import uuid

import xml.etree.ElementTree as ET

s_path = '/mnt/HardDisk_01/Datasets/cleaning_data/detection_lbl/upd20201013'
out_img_path = '/mnt/HardDisk_01/Datasets/cleaning_data/detection_lbl/output'

img_ext = '.jpg'
desc_ext = '.xml'

extend_perc = 10

fnames = []

out_names = {}

# for f in os.listdir(s_path):
#     if f.endswith(desc_ext):
#         fnames.append(os.path.join(s_path, f))

for (dirpath, dirnames, filenames) in os.walk(s_path):
    for f in filenames:
        if f.endswith(desc_ext):
            fnames.append((dirpath, f))

for dirpath, filename in fnames:
    fn = os.path.join(dirpath, filename)
    root = ET.parse(fn).getroot()
    objects = []

    for type_tag in root.findall('object'):
        obj = {}
        for ch in type_tag:
            if ch.tag == 'name':
                obj['name'] = ch.text.strip().lower()
            elif ch.tag == 'bndbox':
                for el in ch:
                    obj[el.tag] = int(el.text)

        objects.append((fn, obj))

    img = cv2.imread(fn.replace(desc_ext, img_ext))

    for fn, obj in objects:
        try:
            width = img.shape[1]
            height = img.shape[0]

            ol_x = int((obj['xmax'] - obj['xmin']) * extend_perc/100)
            ol_y = int((obj['ymax'] - obj['ymin']) * extend_perc/100)

            xmin = max(0, obj['xmin'] - ol_x)
            xmax = min(width, obj['xmax'] + ol_x)
            ymin = max(0, obj['ymin'] - ol_y)
            ymax = min(height, obj['ymax'] + ol_y)

            obj_img = img[ymin:ymax, xmin:xmax]

            dn = os.path.join(out_img_path, obj['name'])
            if not os.path.exists(dn):
                os.makedirs(dn)

            newname = filename[0: filename.rfind('.')]
            out_names[newname] = out_names.get(newname, -1) + 1
            newname = newname + '_' + str(out_names[newname])
            cv2.imwrite(os.path.join(dn, newname + img_ext), obj_img)
        except:
            print(f'error in {filename}')

print('done')


import logging as log

from virttest import virt_vm
from virttest import virsh
from virttest.libvirt_xml import vm_xml
from virttest.libvirt_xml import LibvirtXMLError
from virttest.libvirt_xml.devices.video import Video
from virttest.utils_test import libvirt

# Using as lower capital is not the best way to do, but this is just a
# workaround to avoid changing the entire file.
logging = log.getLogger('avocado.' + __name__)

def run(test, params, env):
    """
    Test defining/undefining domain by dumping xml, changing it, and re-adding.

    (1) Get name and uuid of existing vm
    (2) Rename domain and verify domain start
    (3) Get uuid of new vm
    (4) Change name&uuid back to original
    (5) Verify domain start
    """

    def do_rename(vm, new_name, uuid=None, fail_info=[]):
        # Change name in XML
        logging.info("Rename %s to %s.", vm.name, new_name)
        try:
            vm = vm_xml.VMXML.vm_rename(vm, new_name, uuid)  # give it a new uuid
        except LibvirtXMLError as detail:
            test.fail("Rename %s to %s failed:\n%s"
                      % (vm.name, new_name, detail))

        # Exercise the defined XML
        try:
            vm.start()
        except virt_vm.VMStartError as detail:
            # Do not raise TestFail because vm should be restored
            fail_info.append("Start guest %s failed:%s" % (vm.name, detail))
        vm.destroy()
        return fail_info

    def prepare_xml_file():
        """
        prepare an invalid xml with video device containing
        invalid attribute "foot", for example,
        <model type='qxl' ram='65536' vram='65536' vgamem='65536' foot='3'/>
        """
        new_xml = xml_backup.copy()

        # Add video device if guest doesn't have
        if not new_xml.get_devices(device_type="video"):
            logging.debug("Guest doesn't have video device, add one")
            new_video = Video()
            new_video.model_type = "virtio"
            new_xml.add_device(new_video)
            logging.debug("The xml with video is %s", new_xml)

        # Set invalid attribute for video device
        video_device_xml = new_xml.xmltreefile.find('/devices/video/model')
        if video_device_xml is None:
            test.error("Failed to get guest video device")
        video_device_xml.set("foot", "3")
        logging.debug("The new xml used for defining a new domain is %s", new_xml)
        xml_file = new_xml.xmltreefile.name

        # undefine the original vm
        virsh.undefine(vm_name, options='--nvram')
        return xml_file

    # Run test
    vm_name = params.get("main_vm")
    vm = env.get_vm(params["main_vm"])
    new_name = params.get("new_name", "test")
    status_error = params.get("status_error") == "yes"
    readonly = params.get("readonly") == "yes"
    option_ref = params.get("option_ref")
    is_defined_from_xml = params.get("is_defined_from_xml") == "yes"
    error_msg = params.get("error_msg")
    uuid = vm.get_uuid()
    logging.info("Original uuid: %s", vm.get_uuid())

    xml_backup = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)
    xml_backup_file = xml_backup.xmltreefile.name

    virsh_dargs = {"debug": True}
    if readonly:
        virsh_dargs["readonly"] = True

    expected_fails_msg = []
    if error_msg:
        expected_fails_msg.append(error_msg)

    if is_defined_from_xml:
        xml_file = prepare_xml_file()
        try:
            ret = virsh.define(xml_file, option_ref, **virsh_dargs)
            libvirt.check_result(ret, expected_fails=expected_fails_msg)
        finally:
            # for positive test, undefine the defined vm firstly
            if not ret.exit_status:
                virsh.undefine(vm_name, options='--nvram')
            # restore the original vm
            ret = virsh.define(xml_backup_file)
            if ret.duration > 3:
                test.fail("Define timeout: expected no more than 3s; actually spent %.2fs" % ret.duration)
        return

    assert uuid is not None
    # Rename to a new name
    fail_info = do_rename(vm, new_name)
    logging.info("Generated uuid: %s", vm.get_uuid())

    # Rename back to original to maintain known-state
    fail_info = do_rename(vm, vm_name, uuid, fail_info)
    logging.info("Final uuid: %s", vm.get_uuid())

    if len(fail_info):
        test.fail(fail_info)


## example code for retrieve an HTML table and write it as a csv file
import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup
# load a wiki page that has a complicated tabel
html=urlopen('http://en.wikipedia.org/wiki/Comparison_of_text_editors')
soup=BeautifulSoup(html,features='lxml')
#get the table
table=soup.find_all('table',{'class':'wikitable sortable'})[0]
#print(table)
#get all the rows
rows=table.find_all('tr')
#print(rows)
#test get first row of table
#first_row=table.find_all('tr')[0]
#print(first_row)
#test get first cell of the first row
#first_row_first_cell=first_row.find_all(['td','th'])[0]
#print(first_row_first_cell)
#get the text of first_row_first_cell
#first_row_first_cell.get_text()
#strip the /n
#first_row_first_cell.get_text().rstrip('\n')

# open a csvfile,make sure to specify newline=''

with open('scrape_table.csv','wt',newline='') as csvfile:
    #create a writer object
    writer=csv.writer(csvfile)
    try:
        for row in rows:
            #for each row of table,create an empty row call csvrow
            csvrow=[]
            #for each cell within each row (either head cell or standard cell)
            for cell in row.find_all(['td','th']):
                #get the cell text, and strip the \n behind it,then append them
                #into one row untill all cell texts are exhausted
                csvrow.append(cell.get_text().rstrip('\n'))
            #when each row is done,append a row at the bottom of the csv file
            writer.writerow(csvrow)
    finally:
        #close the file when done
        csvfile.close()


from collections import defaultdict

def print_paths_util(graph, start, end, visited, paths, path):

    if start.islower():
        visited[start] = True
    path.append(start)

    if start == end:
        paths.append(path)
        print(path)
    else:
        for i in graph[start]:
            if visited[i] == False:
                print_paths_util(graph, i,end,visited, paths, path)

    path.pop()
    visited[start] = False

def print_paths(graph, start, end):
    visited = defaultdict(bool)
    paths = []
    path = []

    print_paths_util(graph, start, end, visited, paths, path)
    return paths

with open('../inputs/day_12_test.txt') as f:
    lines = f.readlines()

    graph = defaultdict(list)

    for l in lines:
        res = l.strip().split('-')
        graph[res[0]].append(res[1])
        graph[res[1]].append(res[0])

    paths = print_paths(graph, 'start', 'end')
    print("Answer {}".format(len(paths)))


from collections import defaultdict
import setuptools
import sys

PYTHON_REQUIRED = (3, 4)
PYTHON_INSTALLED = sys.version_info[:2]

if PYTHON_INSTALLED < PYTHON_REQUIRED:
    sys.stderr.write('''

`pyignite` is not compatible with Python {}.{}!
Use Python {}.{} or above.

'''.format(
            PYTHON_INSTALLED[0],
            PYTHON_INSTALLED[1],
            PYTHON_REQUIRED[0],
            PYTHON_REQUIRED[1],
        )
    )
    sys.exit(1)

def is_a_requirement(line):
    return not any([
        line.startswith('#'),
        line.startswith('-r'),
        len(line) == 0,
    ])

requirement_sections = [
    'install',
    'setup',
    'tests',
    'docs',
]
requirements = defaultdict(list)

for section in requirement_sections:
    with open(
        'requirements/{}.txt'.format(section),
        'r',
        encoding='utf-8',
    ) as requirements_file:
        for line in requirements_file.readlines():
            line = line.strip('\n')
            if is_a_requirement(line):
                requirements[section].append(line)

with open('README.md', 'r', encoding='utf-8') as readme_file:
    long_description = readme_file.read()

setuptools.setup(
    name='pyignite',
    version='0.3.4',
    python_requires='>={}.{}'.format(*PYTHON_REQUIRED),
    author='Dmitry Melnichuk',
    author_email='dmitry.melnichuk@nobitlost.com',
    description='Apache Ignite binary client Python API',
    long_description=long_description,
    long_description_content_type='text/markdown',
    url=(
        'https://github.com/apache/ignite/tree/master'
        '/modules/platforms/python'
    ),
    packages=setuptools.find_packages(),
    install_requires=requirements['install'],
    tests_require=requirements['tests'],
    setup_requires=requirements['setup'],
    extras_require={
        'docs': requirements['docs'],
    },
    classifiers=[
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3 :: Only',
        'Intended Audience :: Developers',
        'Topic :: Database :: Front-Ends',
        'Topic :: Software Development :: Libraries :: Python Modules',
        'License :: OSI Approved :: Apache Software License',
        'Operating System :: OS Independent',
    ],
)


from sklearn.datasets import load_boston
import matplotlib.pyplot as plt
import numpy as np
dataset = load_boston()
x_data = dataset.data # 特征变量
name_data = dataset.feature_names
y_data = dataset.target # 预测目标房价
'''
#先观察数据分布情况
for i in range(13):
    plt.subplot(4,4,i+1)
    plt.scatter(x_data[:,i],y_data)
    plt.xlabel(name_data[i])
    plt.ylabel('House Price')
plt.show()
'''
#去除异常数据
abnlist = np.array([])
for i in range(len(y_data)):
    if y_data[i] == 50.0:
        abnlist = np.append(abnlist, i) #存储异常值下标

x_data = np.delete(x_data, abnlist, axis=0)
y_data = np.delete(y_data, abnlist)
print("x_data.shape = {}".format(x_data.shape))
print("y_data.shape = {}".format(y_data.shape))
print("__________")

#数据分割
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x_data,y_data,random_state = 33,test_size = 0.2)
print("x_train.shape = {}".format(x_train.shape))
print("y_train.shape = {}".format(y_train.shape))
print("__________")

#Task - a 简单线性回归
from sklearn.linear_model import LinearRegression
lr = [0, 0, 0, 0, 0,   0, 0, 0, 0, 0,   0, 0, 0, 0, 0]
for i in range(13):
    # # 检查x和y的形状
    print(x_train[:, i].reshape(-1, 1).shape)
    print(y_train.reshape(-1, 1).shape)

    # 拟合
    lr[i] = LinearRegression(fit_intercept=True).fit(x_train[:, i].reshape(-1, 1), y_train.reshape(-1, 1))  #参数估计
    print(lr[i].predict( x_test[:, i].reshape(-1, 1) ).shape)

    # 把每个特征对应的预测结果存起来
    if i == 0:
        lr_y_predict = np.expand_dims(lr[i].predict( x_test[:, i].reshape(-1, 1) ), axis=1)
    else:
        pred_temp = np.expand_dims(lr[i].predict( x_test[:, i].reshape(-1, 1) ), axis=1)
        lr_y_predict = np.concatenate((lr_y_predict, pred_temp), axis=1)

    print(lr_y_predict.shape)
    print("+++++++")

lr_y_predict = np.squeeze(lr_y_predict, axis=-1)
print(lr_y_predict.shape)

#Task - b 绘制变量关系图和最小二乘回归线
# x_test = np.sort(x_test, axis=0)
# y_test = np.sort(y_test, axis=0)
for i in range(13):
    plt.subplot(4, 4, i+1)
    plt.scatter(x_train[:, i], y_train)
    plt.scatter(x_test[:, i], y_test, color='red')
    plt.plot(x_test[:, i], lr[i].predict( x_test[:, i].reshape(-1, 1) ), color='black')
    plt.xlabel(name_data[i])
    plt.ylabel('House Price')
plt.show()

# #Task - c 评估回归结果
from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error
for i in range(13):
    print("Feature_name",name_data[i])
    print("r2_score:",r2_score(y_test,lr[i].predict( x_test[:, i].reshape(-1, 1))))
    print("MSE:",mean_squared_error(y_test,lr[i].predict( x_test[:, i].reshape(-1, 1))))
    print("MAE:",mean_absolute_error(y_test,lr[i].predict( x_test[:, i].reshape(-1, 1))))
    print("__________")

#Task - d 分别使用LinearRegression和SGDRegressor训练和预测，评估结果
#数据标准化处理
from sklearn.preprocessing import StandardScaler
#分别初始化对特征和目标值的标准化器
ss_X=StandardScaler()
ss_y=StandardScaler()

#分别对训练和测试数据的特征以及目标值进行标准化处理
x_train=ss_X.fit_transform(x_train)
x_test=ss_X.transform(x_test)
y_train=ss_y.fit_transform(y_train.reshape(-1,1))
y_test=ss_y.transform(y_test.reshape(-1,1))

from sklearn.linear_model import SGDRegressor
from sklearn.linear_model import LinearRegression
sgdr = SGDRegressor()
sgdr.fit(x_train,y_train)
sgdr_y_predict = sgdr.predict(x_test)
lr = LinearRegression()
lr.fit(x_train,y_train)
lr_y_predict = lr.predict(x_test)
plt.plot(y_test,color = 'blue')
plt.plot(lr_y_predict,color = 'red')
plt.plot(sgdr_y_predict,color = 'black')
plt.legend(['House Price','Linear Regression','SGD Regressor'],loc='upper right')
plt.show()

#Linear Regression模型评估
#r2_score
print("Linear Regression")
print("r2_score:",r2_score(y_test,lr_y_predict))
#MSE
print("MSE:",mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(lr_y_predict)))
#MAE
print("MAE:",mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(lr_y_predict)))

#SGDRegressor模型评估
print("SGDRegressor")
#r2_score
print("r2_score:",r2_score(y_test,sgdr_y_predict))
#MSE
print("MSE:",mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(sgdr_y_predict)))
#MAE
print("MAE:",mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(sgdr_y_predict)))


from core.plugin import *
from core.bot import Bot

from plugins.pixiv.plugin_config import *
from plugins.pixiv.plugin_doc import PLUGIN_DOC

from core.image import *

import aiohttp

class Pixiv_Plugin(Plugin):

    def __init__(self):
        super().__init__(
                name = "Pixiv",
                requirements = [],
                info = "P站图片搬运",
                doc = PLUGIN_DOC
            )

    @check_setup
    async def handle_message(self, message: Message) -> Union[Message, List[Message], Error]:
        if message.text is not None:
            cmd_args = message.text.split(" ")

            if len(cmd_args) > 2:
                return Error("参数个数错误")

            if cmd_args[0] in FETCH_COMMAND:
                if len(cmd_args) == 1:
                    url = API_URL + "/?san={}".format(SAN)
                else:
                    url = API_URL + "/get/tags/{}?san={}".format(cmd_args[1], SAN)

                timeout = aiohttp.ClientTimeout(total=TIMEOUT)
                async with aiohttp.ClientSession(timeout=timeout) as session:
                    try:
                        res = await session.get(url)
                        content = eval(await res.text())

                        if "code" in content and content["code"] == 200:
                            pic_id = content["data"]['imgs'][0]["id"]
                            author = [content["data"]['imgs'][0]["userid"], content["data"]['imgs'][0]["username"]]
                            pic_url = content["data"]['imgs'][0]["url"]
                            tags = content["data"]['imgs'][0]["tags"]

                            res = await session.get(pic_url)
                            img = await res.read()
                            if isinstance(img, bytes):
                                pic_path = save_image(img, file_name=TMP_FILENAME)
                                reply = Message()
                                reply.text = "[Moment Pixiv 图片服务]\nid: {}\n作者: {}({})\nurl: {}\ntags: {}\n\n".format(pic_id, author[1], author[0], pic_url, tags)
                                reply.pic = Picture(pic_url, pic_path=pic_path)
                                return reply
                            else:
                               return Error("pixiv 服务获取图片时失败...", urge=self.get_name())
                        else:
                            return Error("pixiv 服务连接失败...", urge=self.get_name())
                    except Exception as e:
                        return Error("唔 发生奇怪错误: {}".format(e.args), urge=self.get_name())

        return Error("命令不满足该插件")


from pandas import DataFrame, to_datetime
from typing import Union
from sqlite3 import connect, Connection
from unicodedata import normalize
from zeitsprung.base import Base

class SQLiteEngine(Base):
    """Class to set up and access a SQLite database to store the data from the 'zeitsprung.fm' website."""

    def __init__(self, db_file: str, verbose: bool = True) -> None:
        """
        Class constructor for the Scraper class.

        Parameters
        ----------
        db_file : str
            Path to the 'zeitsprung.db' file.
            Hint: The database is created inside the data folder, specified in the constructor of 'Scrape'.
        verbose : bool, default True
            Print messages about the activities conducted by a class instance.

        Returns
        -------
        None

        """
        super().__init__(verbose)
        self.db_file = db_file
        self.verbose = verbose

    def __str__(self) -> str:
        """
        Print function of the class.

        Returns
        -------
        str
            A string, which describes the class instance.

        """
        return f"Database engine connecting to '{self.db_file}'."

    def create_connection(self) -> Connection:
        """
        Creates a connection object using the provided credentials in instance creation.

        Returns
        -------
        Connection
            A object for connecting to the database.

        """
        conn = None
        try:
            conn = connect(self.db_file)
        except BaseException as e:
            print(e)
        return conn

    def setup_schema(self) -> None:
        """
        Creates the schema with 'meta' and 'audio' relations in the 'zeitsprung.db' database.

        Returns
        -------
        None

        """
        self._print(f"Setting up SQLite database at '{self.db_file}'.")
        conn = self.create_connection()
        cur = conn.cursor()
        cur.execute('DROP TABLE IF EXISTS meta;')
        cur.execute('''
        CREATE TABLE meta (
            uid INTEGER PRIMARY KEY,
            published_at DATETIME NOT NULL,
            modified_at DATETIME NOT NULL,
            abbreviation TEXT NOT NULL,
            title TEXT NOT NULL,
            description TEXT NOT NULL,
            url_episode TEXT NOT NULL,
            url_audio TEXT NOT NULL
        );
        ''')
        cur.execute('DROP TABLE IF EXISTS audio;')
        cur.execute('''
        CREATE TABLE audio (
            uid INTEGER PRIMARY KEY,
            file_path TEXT NOT NULL,
            duration INTEGER NOT NULL,
            frame_rate INTEGER NOT NULL,
            frame_width INTEGER NOT NULL
        );
        ''')
        conn.commit()
        conn.close()

    def insert_meta_row(self, row: list) -> None:
        """
        Inserts a record row into the 'meta' relation.

        Parameters
        ----------
        row : list
            Values to insert.

        Returns
        -------
        None

        """
        self._print(f"Writing row for '{row[0]}' to table 'meta'.")
        conn = self.create_connection()
        cur = conn.cursor()
        cur.execute(f"""
        INSERT INTO meta (uid, published_at, modified_at, abbreviation, title, description, url_episode, url_audio)
        VALUES('{row[0]}', '{row[1]}', '{row[2]}', '{row[3]}', '{normalize("NFKD", row[4]).replace("'", '')}',
            '{normalize("NFKD", row[5]).replace("'", '')}', '{row[6]}', '{row[7]}');
        """)
        conn.commit()
        conn.close()

    def insert_audio_row(self, row: list) -> None:
        """
        Inserts a record row into the 'audio' relation.

        Parameters
        ----------
        row : list
            Values to insert.

        Returns
        -------
        None

        """
        self._print(f"Writing row for '{row[0]}' to table 'audio'.")
        conn = self.create_connection()
        cur = conn.cursor()
        cur.execute(f"""
        INSERT INTO audio (uid, file_path, duration, frame_rate, frame_width)
        VALUES('{row[0]}', '{row[1]}', '{row[2]}', '{row[3]}', '{row[4]}');
        """)
        conn.commit()
        conn.close()

    def query_last_episode_id(self) -> Union[int, None]:
        """
        Queries the last episode from the 'meta' relation.

        Returns
        -------
        int or None
            The id of the last episode.

        """
        conn = self.create_connection()
        cur = conn.cursor()
        cur.execute('SELECT max(uid) FROM meta')
        uid = cur.fetchall()[0][0]
        return 0 if uid is None else uid

    def query_all_meta(self) -> DataFrame:
        """
        Queries the complete record from the 'meta' relation and parses it into a DataFrame object.

        Returns
        -------
        DataFrame
            The meta table.

        """
        self._print("Querying all rows from table 'meta'.")
        conn = self.create_connection()
        cur = conn.cursor()
        cur.execute('SELECT * FROM meta')
        rows = cur.fetchall()
        conn.close()
        df = DataFrame([list(row) for row in rows],
                       columns=['uid', 'published_at', 'modified_at', 'abbreviation',
                                'title', 'description', 'url_episode', 'url_audio'])
        df['published_at'] = to_datetime(df['published_at'])
        df['modified_at'] = to_datetime(df['modified_at'])
        return df

    def query_all_audio(self) -> DataFrame:
        """
        Queries the complete record from the 'audio' relation and parses it into a DataFrame object.

        Returns
        -------
        DataFrame
            The audio table.

        """
        self._print("Querying all rows from table 'audio'.")
        conn = self.create_connection()
        cur = conn.cursor()
        cur.execute('SELECT * FROM audio')
        rows = cur.fetchall()
        conn.close()
        df = DataFrame([list(row) for row in rows],
                       columns=['uid', 'file_path', 'duration', 'frame_rate', 'frame_width'])
        return df


# This file must use Python 1.5 syntax.
import glob
import os
import re
import sys

class base_check_python_version:

    def __init__(self):
        version = None
        try:
            version = sys.version_info[0:2]
        except AttributeError:
            pass  # pre 2.0, no neat way to get the exact number

        # The change to prefer 2.4 really messes up any systems which have both
        # the new and old version of Python, but where the newer is default.
        # This is because packages, libraries, etc are all installed into the
        # new one by default. Some things (like running under mod_python) just
        # plain don't handle python restarting properly. I know that I do some
        # development under ipython and whenever I run (or do anything that
        # runs) 'import common  # pylin: disable=W0611' it restarts my shell. Overall, the change was
        # fairly annoying for me (and I can't get around having 2.4 and 2.5
        # installed with 2.5 being default).
        if not version or version < (2, 4) or version >= (3, 0):
            try:
                # We can't restart when running under mod_python.
                from mod_python import apache
            except ImportError:
                self.restart()

    def extract_version(self, path):
        match = re.search(r'/python(\d+)\.(\d+)$', path)
        if match:
            return (int(match.group(1)), int(match.group(2)))
        else:
            return None

    PYTHON_BIN_GLOB_STRINGS = ['/usr/bin/python2*', '/usr/local/bin/python2*']

    def find_desired_python(self):
        """Returns the path of the desired python interpreter."""
        pythons = []
        for glob_str in self.PYTHON_BIN_GLOB_STRINGS:
            pythons.extend(glob.glob(glob_str))

        possible_versions = []
        best_python = (0, 0), ''
        for python in pythons:
            version = self.extract_version(python)
            if version and version >= (2, 4):
                possible_versions.append((version, python))

        possible_versions.sort()

        if not possible_versions:
            raise ValueError('Python 2.x version 2.4 or better is required')
        # Return the lowest possible version so that we use 2.4 if available
        # rather than more recent versions.
        return possible_versions[0][1]

    def restart(self):
        python = self.find_desired_python()
        sys.stderr.write('NOTE: %s switching to %s\n' %
                         (os.path.basename(sys.argv[0]), python))
        sys.argv.insert(0, '-u')
        sys.argv.insert(0, python)
        os.execv(sys.argv[0], sys.argv)


import os
import requests
import poyo
from chalicelib import settings
from functools import lru_cache

def _request(path, method="get", data=None):
    url = f"https://api.statuspage.io/v1/pages/{settings.statuspage_page}/{path}"
    headers = {"Authorization": f"OAuth {settings.statuspage_key}"}
    r = requests.request(method, url, headers=headers, data=data)
    if r.status_code != 200 and r.status_code != 201:
        print(f"Received error {r.status_code} for {url} with body:\n{r.content}")
        r.raise_for_status()
    return r.json()

def _get_incidents():
    return _request("incidents/unresolved.json")

def _create_incident(name, body, incident_status, component_id, component_status):
    data = {
        "incident[name]": name,
        "incident[body]": body,
        "incident[status]": incident_status,
        "incident[component_ids][]": component_id,
        f"incident[components][{component_id}]": component_status,
    }
    _request("incidents.json", method="post", data=data)

def _update_incident(incident_id, incident_status, component_id, component_status):
    data = {
        "incident[status]": incident_status,
        "incident[component_ids]": component_id,
        f"incident[components][{component_id}]": component_status,
    }
    _request(f"incidents/{incident_id}.json", method="patch", data=data)

# TODO log error and exclude if multiple components or no components
def _we_created_incident(incident):
    # filter out incidents that were not opened by this tool
    if settings.watermark in incident["incident_updates"][-1]["body"]:
        print("We opened it")
        return True
    else:
        print("We did not open it")
        return False

def _component_from_incident(incident):
    return incident["incident_updates"][-1]["affected_components"][-1]["code"]

# memoize so only hit statuspage api once even if creating multiple incidents
@lru_cache(maxsize=1)
def _component_ids_to_names():
    components = _request("components.json")
    ids_to_names = {c["id"]: c["name"] for c in components}
    return ids_to_names

def _render_incident_text(component_id, incident_name, incident_body):
    # replace placeholder with name of the component we're opening incident for
    if "{{component_name}}" in incident_name or "{{component_name}}" in incident_body:
        component_name = _component_ids_to_names()[component_id]
        incident_name = incident_name.replace("{{component_name}}", component_name)
        incident_body = incident_body.replace("{{component_name}}", component_name)
    # add our watermark so later we can identiy this incident as being created by this tool
    incident_body = f"{incident_body}\n{settings.watermark}"
    return (incident_name, incident_body)

def components_and_incidents():
    components = set()
    components_to_incidents = dict()
    incidents = _get_incidents()
    print(f"Found {len(incidents)} statuspage incidents")
    for incident in incidents:
        print(f"For statuspage incident {incident['id']}")
        if _we_created_incident(incident):
            affected_component = _component_from_incident(incident)
            print(f"Affected component is {affected_component}")
            components.add(affected_component)
            # TODO log error if there was already an incident that affected this component
            components_to_incidents[affected_component] = incident["id"]
    return (components, components_to_incidents)

def close_incident(component_id, incident_id):
    print(
        f"Resolving statuspage incident {incident_id} and marking component {component_id} operational"
    )
    _update_incident(incident_id, "resolved", component_id, "operational")

# TODO allow choosing template based on a tag
def open_incident(component_id):
    template_path = f"{os.path.dirname(__file__)}/incident_templates/default.yml"
    template = poyo.parse_string(open(template_path, "r").read())
    name, body = _render_incident_text(component_id, template["name"], template["body"])

    print(
        f"Opening statuspage incident for component {component_id} and marking it {template['component_status']}"
    )
    _create_incident(
        name,
        body,
        template["incident_status"],
        component_id,
        template["component_status"],
    )


from core.plugin import *

from plugins.word.plugin_config import *
from plugins.word.plugin_doc import PLUGIN_DOC

import random
import re
import json

class Word_Plugin(Plugin):

    def __init__(self):
        super().__init__(
                name = "Word",
                requirements = [],
                info = "背单词插件",
                doc = PLUGIN_DOC
            )

    @check_setup
    async def handle_message(self, message: Message) -> Union[Message, List[Message], Error]:
        if message.text is not None:
            if re.match(COMMAND_PATTERN, message.text) is not None:
                ge_pos = message.text.find("个")
                word_num = int(message.text[1:ge_pos])
                if word_num > WORD_THRESHOLD:
                    return Error("太多词了!", urge=self.get_name())

                reply = Message()
                # reply.at = message.sender
                # without at, you can translate the word directly by Translate Plugin
                reply.text = "给你词: \n"

                with open(WORD_PATH) as fp:
                    word_list = list(json.load(fp))

                    for i in range(word_num):
                        reply.text += random.choice(word_list)
                        if i != word_num-1:
                            reply.text += "\n"

                return reply

        return Error("命令不满足该插件")