######## Tensorflow Imaage Classifier #########
#
# Author: Erik Handeland Date: 12/12/2021
# Description: This program uses a TensorFlow Lite object detection model-metadata to
# perform object detection on an image. It creates a dict containing a
# list of detected objects and the count for each object. It also save a copy
# of the image with draws boxes and scores around the objects of interest for each image.
#
# This code is based off the TensorFlow Lite image classification example at:
# https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/examples/python/label_image.py
# Add the following github repo by Evan Juras:
# https://github.com/EdjeElectronics/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi
#

# Import packages
import os
from os.path import exists
import cv2
import numpy as np
import importlib.util
from tflite_support import metadata
from from_root import from_root

# Import TensorFlow libraries
# If tflite_runtime is installed, import interpreter from tflite_runtime, else import from regular tensorflow
# If using Coral Edge TPU, import the load_delegate library
pkg = importlib.util.find_spec('tflite_runtime')
if pkg:
    from tflite_runtime.interpreter import Interpreter
else:
    from tensorflow.lite.python.interpreter import Interpreter

# Extract metadata from the .tflite file
def load_metadata_labels(PATH_TO_MODEL):
    label_list = []

    try:
        displayer = metadata.MetadataDisplayer.with_model_file(PATH_TO_MODEL)
        file_name = displayer.get_packed_associated_file_list()[0]
    except ValueError:
        # The model-metadata does not have metadata.
        return label_list

    if file_name:
        label_map_file = displayer.get_associated_file_buffer(file_name).decode()
        label_list = list(filter(len, label_map_file.splitlines()))
    return label_list

def load_labels(PATH_TO_GRAPH, PATH_TO_LABELS):
    # Load label list from metadata or from labelmap file
    label_list = load_metadata_labels(PATH_TO_GRAPH)

    if not label_list:  # DEPRECATED this is the old way of loading labels, new ML models should have it as metadata
        if not exists(PATH_TO_LABELS):
            print("No labelmap in metadata and no labelmap.txt found! at path: " + PATH_TO_LABELS)
            return {
                "error": "No labelmap found",
                "vehicles": -1,
                "pedestrians": -1,
                "confidence-threshold": 0.50,
                "objects": [],
            }
        # Load the label map
        with open(PATH_TO_LABELS, 'r') as f:
            label_list = [line.strip() for line in f.readlines()]

    return label_list

# MODEL_NAME: should be the name of a directory in the models directory
# IMG_PATH: should be the path full path to your target image
# COORDS: Whether to return coordinates of detected objects
# MIN_CONF_LEVEL: is the minimum confidence level to be considered a detection 0-1
# PATH_TO_GRAPH & LABELMAP_NAME: Name of the .tflite file and the labelmap file. Defaults should work for most cases
# SAVED_IMG_PATH: Directory to save the image with boxes and scores. If not specified, no image will be saved
def objDetection(MODEL_NAME: str, IMG_PATH: str, MIN_CONF_LEVEL=0.50,
                 GRAPH_NAME="detect.tflite", LABELMAP_NAME="labelmap.txt", SAVED_IMG_PATH="", COORDS=False):
    objects = []

    # Get path to project root
    CWD_PATH = str(from_root())
    # Path to .tflite file, which contains the model-metadata that is used for object detection
    try:  # running from pip install - pip install has different path structure that source
        PATH_TO_MODEL = os.path.join(CWD_PATH, "models", MODEL_NAME)
        PATH_TO_GRAPH = os.path.join(PATH_TO_MODEL, GRAPH_NAME)
        PATH_TO_LABELS = os.path.join(PATH_TO_MODEL, LABELMAP_NAME)
        if not exists(PATH_TO_GRAPH):
            raise FileNotFoundError
    except FileNotFoundError:  # running from source
        PATH_TO_MODEL = os.path.join(CWD_PATH, "obj_detection", "models", MODEL_NAME)
        PATH_TO_GRAPH = os.path.join(PATH_TO_MODEL, GRAPH_NAME)
        PATH_TO_LABELS = os.path.join(PATH_TO_MODEL, LABELMAP_NAME)
        if not exists(PATH_TO_GRAPH):
            print("detect.tflite not found! at path: " + PATH_TO_GRAPH)
            return {
                "error": "Invalid model-metadata path",
                "vehicles": -1,
                "pedestrians": -1,
                "confidence-threshold": MIN_CONF_LEVEL,
                "objects": objects,
            }

    # Load label list from metadata or from labelmap file
    labels = load_labels(PATH_TO_GRAPH, PATH_TO_LABELS)

    # Load the Tensorflow Lite model-metadata.
    interpreter = Interpreter(model_path=PATH_TO_GRAPH)
    interpreter.allocate_tensors()

    # Get model-metadata details
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    height = input_details[0]['shape'][1]
    width = input_details[0]['shape'][2]
    floating_model = (input_details[0]['dtype'] == np.float32)
    input_mean = 127.5
    input_std = 127.5

    # Load image and resize to expected shape [1xHxWx3]
    image = cv2.imread(IMG_PATH)
    if image is None:
        print("Image not found, check path ", IMG_PATH)
        return {
            "error": "Image not found, check path",
            "vehicles": -1,
            "pedestrians": -1,
            "confidence-threshold": MIN_CONF_LEVEL,
            "objects": objects,
        }

    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    imH, imW, _ = image.shape
    image_resized = cv2.resize(image_rgb, (width, height))
    input_data = np.expand_dims(image_resized, axis=0)

    # Normalize pixel values if using a floating model-metadata (i.e. if model-metadata is non-quantized)
    if floating_model:
        input_data = (np.float32(input_data) - input_mean) / input_std

    # Perform the actual detection by running the model-metadata with the image as input
    interpreter.set_tensor(input_details[0]['index'], input_data)
    interpreter.invoke()

    # Retrieve detection results
    try:
        boxes = interpreter.get_tensor(output_details[0]['index'])[0]  # Bounding box coordinates of detected objects
        classes = interpreter.get_tensor(output_details[1]['index'])[0]  # Class index of detected objects
        scores = interpreter.get_tensor(output_details[2]['index'])[0]  # Confidence of detected objects
    except:
        return {
            "error": "Invalid model-metadata output details, probably using model-metadata for JS or Dart",
            "vehicles": -1,
            "pedestrians": -1,
            "confidence-threshold": MIN_CONF_LEVEL,
            "objects": objects,
        }

    # Loop over all detections and draw detection box if confidence is above minimum threshold
    for i in range(len(scores)):

        if ((scores[i] > MIN_CONF_LEVEL) and (scores[i] <= 1.0)):
            # Get bounding box coordinates and draw box Interpreter can
            # return coordinates that are outside of image dimensions,
            # need to force them to be within image using max() and min()
            ymin = int(max(1, (boxes[i][0] * imH)))
            xmin = int(max(1, (boxes[i][1] * imW)))
            ymax = int(min(imH, (boxes[i][2] * imH)))
            xmax = int(min(imW, (boxes[i][3] * imW)))

            # Corners of the bounding box
            tr = (xmax, ymax)  # Top right
            bl = (xmin, ymin)  # Bottom left
            br = (xmax, ymin)
            tl = (xmin, ymax)

            # Draw detection box on image
            cv2.rectangle(image, bl, tr, (10, 255, 0), 2)
            # Draw label
            object_name = labels[int(classes[i])]  # Look up object name from "labels" array using class index
            object_score = int(scores[i] * 100)
            label = '%s: %d%%' % (object_name, object_score)  # Example: 'person: 72%'
            labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2)  # Get font size
            label_ymin = max(ymin, labelSize[1] + 10)  # Make sure not to draw label too close to top of window
            cv2.rectangle(image, (xmin, label_ymin - labelSize[1] - 10),
                          (xmin + labelSize[0], label_ymin + baseLine - 10),
                          (255, 255, 255), cv2.FILLED)  # Draw white box to put label text in
            cv2.putText(image, label, (xmin, label_ymin - 7),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)  # Draw label text

            # Add object to objects list
            obj = {
                "name": object_name,
                "confidence": scores[i],
                "coord": {"top-left": tl, "top-right": tr, "bottom-right": br, "bottom-left": bl} if COORDS else {},
            }
            objects.append(obj)

    # count vehicles and pedestrians
    cars = 0
    people = 0
    for obj in objects:
        if obj["name"] == "car" or obj["name"] == "truck":
            cars += 1
        elif obj["name"] == "person":
            people += 1

    if SAVED_IMG_PATH:
        _, tail = os.path.split(IMG_PATH)
        SAVED_IMG_PATH = os.path.join(SAVED_IMG_PATH, tail[:-4] + "_box.jpg")
        cv2.imwrite(SAVED_IMG_PATH, image)

    return {
        "error": "",
        "vehicles": cars,
        "pedestrians": people,
        "confidence-threshold": MIN_CONF_LEVEL,
        "objects": objects,
    }

# Sample function for detecting if object is in a certain area, useful if some parking lots have handicapped or
# oversize parking spaces
# if inArea([tr, tl, br, bl], (100, 400), (800, 600)):
#       print("Object detected in area")
def inArea(points, box_start, box_end):
    for point in points:
        if (box_start[0] < point[0] < box_end[0] and
                box_start[1] < point[1] < box_end[1]):
            return True
    return False


"""
Name : portfolio_optimizer.py
Author : Yinsen Miao
 Contact : yinsenm@gmail.com
Time    : 7/21/2021
Desc: Solve mean-variance optimization
"""

import numpy as np
import pandas as pd
from scipy.optimize import minimize
from gerber import gerber_cov_stat1, gerber_cov_stat2
from ledoit import ledoit

def set_eps_wgt_to_zeros(in_array, eps=1e-4):
    # set small weights to 0 and return a list
    out_array = np.array(in_array)
    out_array[np.abs(in_array) < eps] = 0
    out_array = np.array(out_array) / np.sum(out_array)
    return out_array

class portfolio_optimizer:
    def __init__(self, min_weight: float = 0., max_weight: float = 1.0,
                 cov_function: str = "HC",
                 freq: str = "monthly",
                 gs_threshold: float = 0.5):
        """
        :param min_weight:
        :param max_weight:
        :param cov_function: can be one of the HC (historical covariance matrix), GS1 (Gerber Stat1), GS2 (Gerber Stat2)
        :param freq: frequency of the returns series either daily or monthly
        :param gs_threshold: threshold of Gerber statistics between 0 and 1
        """
        # check arguments
        assert cov_function in ['HC', 'GS1', 'GS2', 'SM'], "The covariance function must be one from HC, SM, GS1, and GS2"
        assert freq in ['daily', 'monthly'], "The return series can only be either daily or monthly"
        assert 1 > min_weight >= 0, "The minimal weight shall be in [0, 1)"
        assert 1 >= max_weight > 0, "The maximum weight shall be in (0, 1]"
        assert 1 >= gs_threshold > 0, "The Gerber shrinkage threshold shall be in (0, 1]"

        self.min_weight = min_weight
        self.max_weight = max_weight

        self.factor = 252 if freq == "daily" else 12  # annual converter
        self.cov_function = cov_function  # covariance function can be one of HC, GS1, GS2
        self.freq = freq  # freq of return series can be either daily or monthly
        self.init_weights = None  # initial portfolio weights
        self.covariance = None
        self.returns_df = None
        self.negative_returns_df = None
        self.covariance_neg = None  # covariance matrix of only negative returns for sortino ratio
        self.obj_function = None
        self.by_risk = None
        self.gs_threshold = gs_threshold

    def set_returns(self, returns_df: pd.DataFrame):
        """
        pass the return series to the class
        :param returns_df: pd.DataFrame of historical daily or monthly returns
        """
        self.returns_df = returns_df.copy(deep=True)
        self.negative_returns_df = returns_df[returns_df < 0].fillna(0)  # keep only the negative returns

    def optimize(self, obj_function: str,
                 target_std: float = None,
                 target_return: float = None,
                 prev_weights: np.array = None,
                 init_weights: np.array = None,
                 cost: float = None) -> np.array:
        """
        Perform portfolio optimization given a series of returns
        :param obj_function:
        :param target_std: targeted annaulized portfolio standard deviation (std)
        :param target_return: targeted annaulized portfolio return deviation
        :param prev_weights: previous weights
        :param prices: current price level when we rebalance our portfolio
        :param cost: cost of transaction fee and slippage in bps or 0.01%
        :return: an array of portfolio weights p x 1
        """
        n, p = self.returns_df.shape  # n is number of observations, p is number of assets

        if init_weights is None:
            self.init_weights = np.array(p * [1. / p])  # initialize weights: equal weighting
        else:
            self.init_weights = init_weights  # otherwise use the nearby weights as hot start for MVO

        self.obj_function = obj_function

        # get covariance matrix
        if self.cov_function == "HC":
            self.covariance = self.returns_df.cov().to_numpy()  # convert to numpy
            self.covariance_neg = self.negative_returns_df.cov().to_numpy()  # convert to numpy
        elif self.cov_function == "SM":
            self.covariance, _ = ledoit(self.returns_df.values)
            self.covariance_neg, _ = ledoit(self.negative_returns_df.values)
        elif self.cov_function == "GS1":
            self.covariance, _ = gerber_cov_stat1(self.returns_df.values, threshold=self.gs_threshold)
            self.covariance_neg, _ = gerber_cov_stat1(self.negative_returns_df.values, threshold=self.gs_threshold)
        elif self.cov_function == "GS2":
            self.covariance, _ = gerber_cov_stat2(self.returns_df.values, threshold=self.gs_threshold)
            self.covariance_neg, _ = gerber_cov_stat2(self.negative_returns_df.values, threshold=self.gs_threshold)

        # set objective function
        if obj_function == "equalWeighting":
            self.init_weights = np.array(p * [1. / p])  # initialize weights: equal weighting
            return self.init_weights

        # set the bounds of each asset holding from 0 to 1
        bounds = tuple((self.min_weight, self.max_weight) for k in range(p))
        constraints = [{'type': 'eq', 'fun': lambda x: np.sum(x) - 1.0}]  # fully invest

        if obj_function == 'meanVariance':
            if target_std is not None:
                self.by_risk = True
                # optimize under risk constraint
                constraints.append({'type': 'eq', 'fun': lambda weights: \
                    self.calc_annualized_portfolio_std(weights) - target_std})
            else:
                # optimize under return constraint
                self.by_risk = False
                constraints.append({'type': 'eq', 'fun': lambda weights: \
                    self.calc_annualized_portfolio_return(weights) - target_return})

        if prev_weights is not None and cost is not None:
            # cost function with transaction fee
            cost_fun = lambda weights: self.object_function(weights) +\
                                       np.abs(weights - prev_weights).sum() * cost / 10000.
        else:
            # cost function without any transaction fee
            cost_fun = lambda weights: self.object_function(weights)

        # trust-constr, SLSQP, L-BFGS-B
        try:
            opt = minimize(cost_fun, x0=self.init_weights, bounds=bounds, constraints=constraints, method="SLSQP")
        except:
            # if SLSQP fails then switch to trust-constr
            opt = minimize(cost_fun, x0=self.init_weights, bounds=bounds, constraints=constraints, method="trust-constr")

        return set_eps_wgt_to_zeros(opt['x'])   # pull small values to zeros

    def object_function(self, weights: np.array) -> float:
        """
        :param weights: current weights to be optimized
        """

        if self.obj_function == "maxReturn":
            f = self.calc_annualized_portfolio_return(weights)
            return -f
        elif self.obj_function == "minVariance":
            f = self.calc_annualized_portfolio_std(weights)
            return f
        elif self.obj_function == "meanVariance" and self.by_risk:
            f = self.calc_annualized_portfolio_return(weights)  # maximize target return level
            return -f
        elif self.obj_function == "meanVariance" and not self.by_risk:
            f = self.calc_annualized_portfolio_std(weights)  # minimize target risk or std level
            return f
        elif self.obj_function == "maxSharpe":
            f = self.calc_annualized_portfolio_sharpe_ratio(weights)
            return -f
        elif self.obj_function == "maxSortino":
            f = self.calc_annualized_sortino_ratio(weights)
            return -f
        elif self.obj_function == 'riskParity':
            f = self.calc_risk_parity_func(weights)
            return f
        else:
            raise ValueError("Object function shall be one of the equalWeighting, maxReturn, minVariance, " +
                             "meanVariance, maxSharpe, maxSortino or riskParity")

    def calc_annualized_portfolio_return(self, weights: np.array) -> float:
        # calculate the annualized standard returns
        annualized_portfolio_return = float(np.sum(self.returns_df.mean() * self.factor * weights))
        #float(np.sum(((1 + self.returns_df.mean()) ** self.factor - 1) * weights))
        return annualized_portfolio_return

    def calc_annualized_portfolio_std(self, weights: np.array) -> float:
        if self.obj_function == "equalWeighting":
            # if equal weight then set the off diagonal of covariance matrix to zero
            annualized_portfolio_std = np.sqrt(np.dot(weights.T, np.dot(np.diag(self.covariance.diagonal()) * self.factor, weights)))
        else:
            temp = np.dot(weights.T, np.dot(self.covariance * self.factor, weights))
            if temp <= 0:
                temp = 1e-20  # set std to a tiny number
            annualized_portfolio_std = np.sqrt(temp)
        if annualized_portfolio_std <= 0:
            raise ValueError('annualized_portfolio_std cannot be zero. Weights: {weights}')
        return annualized_portfolio_std

    def calc_annualized_portfolio_neg_std(self, weights: np.array) -> float:
        if self.obj_function == "equalWeighting":
            # if equal weight then set the off diagonal of covariance matrix to zero
            annualized_portfolio_neg_std = np.sqrt(np.dot(weights.T, np.dot(np.diag(self.covariance_neg.diagonal()) * self.factor, weights)))
        else:
            annualized_portfolio_neg_std = np.sqrt(np.dot(weights.T, np.dot(self.covariance_neg * self.factor, weights)))
        if annualized_portfolio_neg_std == 0:
            raise ValueError('annualized_portfolio_std cannot be zero. Weights: {weights}')
        return annualized_portfolio_neg_std

    def calc_annualized_portfolio_moments(self, weights: np.array) -> tuple:
        # calculate the annualized portfolio returns as well as its standard deviation
        return self.calc_annualized_portfolio_return(weights), self.calc_annualized_portfolio_std(weights)

    def calc_annualized_portfolio_sharpe_ratio(self, weights: np.array) -> float:
        # calculate the annualized Sharpe Ratio
        return self.calc_annualized_portfolio_return(weights) / self.calc_annualized_portfolio_std(weights)

    def calc_annualized_sortino_ratio(self, weights: np.array) -> float:
        # calculate the annualized Sortino Ratio
        return self.calc_annualized_portfolio_return(weights) / self.calc_annualized_portfolio_neg_std(weights)

    def calc_risk_parity_func(self, weights):
        # Spinu formulation of risk parity portfolio
        assets_risk_budget = self.init_weights
        portfolio_volatility = self.calc_annualized_portfolio_std(weights)

        x = weights / portfolio_volatility
        risk_parity = (np.dot(x.T, np.dot(self.covariance * self.factor, x)) / 2.) - np.dot(assets_risk_budget.T, np.log(x + 1e-10))
        return risk_parity

    def calc_relative_risk_contributions(self, weights):
        # calculate the relative risk contributions for each asset given returns and weights
        rrc = weights * np.dot(weights.T, self.covariance) / np.dot(weights.T, np.dot(self.covariance, weights))
        return rrc

# unitest the code
if __name__ == "__main__":
    bgn_date = "2016-01-01"
    end_date = "2020-01-01"
    file_path = "../data/prcs.csv"
    rets_df = pd.read_csv(file_path, parse_dates=['Date'], index_col=["Date"]).pct_change()[bgn_date: end_date]
    rets = rets_df.values

    # test objective function list
    obj_function_list = ['equalWeighting', 'minVariance', 'maxReturn', 'maxSharpe', 'maxSortino', 'riskParity']
    cov_function_list = ["HC", "SM", "GS1", "GS2"]

    for cov_fun in cov_function_list:
        print("MVO based on %s covariance function ..." % cov_fun)
        port_opt = portfolio_optimizer(min_weight=0, max_weight=1, cov_function=cov_fun, freq="monthly")
        port_opt.set_returns(returns_df=rets_df)
        # run MVO under various optimization goals

        for obj_fun_str in obj_function_list:
            weights = port_opt.optimize(obj_fun_str)
            ret, std = port_opt.calc_annualized_portfolio_moments(weights=weights)
            sharpe = port_opt.calc_annualized_portfolio_sharpe_ratio(weights=weights)
            sortino = port_opt.calc_annualized_sortino_ratio(weights=weights)
            print("%20s: ret %.3f, std %.3f, Sharpe %.3f, Sortino %.3f" % (obj_fun_str, ret, std, sharpe, sortino))
        obj_fun_str = "meanVariance"
        # optimize for target std levels
        target_stds = [3, 6, 9, 12, 15]
        for target_std in target_stds:
            weights = port_opt.optimize(obj_fun_str, target_std / 100.)
            # print(weights)
            ret, std = port_opt.calc_annualized_portfolio_moments(weights=weights)
            sharpe = port_opt.calc_annualized_portfolio_sharpe_ratio(weights=weights)
            sortino = port_opt.calc_annualized_sortino_ratio(weights=weights)
            print("%20s (%02d%%): ret %.3f, std %.3f, Sharpe %.3f, Sortino %.3f" % (
                obj_fun_str, target_std, ret, std, sharpe, sortino))


from django.conf.urls import url

from tests import views, exceptions

urlpatterns = [
    url(r'^snippets/$', views.SnippetList.as_view(), name='snippet-list'),
    url(r'^snippets2/$', views.SnippetList.as_view(), name='snippet2-list'),
    url(r'^snippet/(?P<pk>\d+)/$', views.SnippetDetail.as_view(),
        name='snippet-detail'),

    url(r'^server_error/$', exceptions.server_error, name='server-error'),
    url(r'^not_found/$', exceptions.not_found, name='not-found'),
    url(r'^method_not_allowed/$', exceptions.method_not_allowed,
        name='not-allowed'),
    url(r'^not_authenticated/$', exceptions.not_authenticated,
        name='not-authenticated'),
]


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests
from bs4 import BeautifulSoup
import time

BASE_URL = 'https://www.nowcoder.com'

driver = webdriver.Chrome(executable_path="P:/selenium/chromedriver.exe")
driver.get('https://www.nowcoder.com/discuss/experience?tagId=2656')  # 对应的页面

# 等待ajax内容加载
wait = WebDriverWait(driver, 10)
wait.until(
    EC.presence_of_element_located((By.CLASS_NAME, "js-nc-wrap-link"))
)

def scrollPage(timeout, times):
    for i in range(times):
        print('next')
        # 向下刷新一次内容
        driver.execute_script("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));")
        time.sleep(timeout)

# 以3秒的间隔向下刷新5次内容
scrollPage(3, 5)

# 带有 class='js-nc-wrap-link' 属性的标签都是面经的链接
items = driver.find_elements_by_class_name('js-nc-wrap-link')

with open('content.txt', 'w', encoding='utf-8') as f:
    # 逐个读取每一个链接中的文本，并写到文件里面
    for item in items:
        print(item.get_attribute('data-href'))
        response = requests.get(BASE_URL + item.get_attribute('data-href'))
        data = response.text
        soup = BeautifulSoup(data, 'html.parser')
        words = soup.find('div', {'class': 'post-topic-des nc-post-content'})
        f.write(words.get_text())


from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
import sqlite3
from time import sleep

imgchrList = ['n','a','e','m','i','g','h','v']
DiffList = ['nov','adv','exh','mxm','inf','grv','hvn','vvd']

conn = sqlite3.connect("SDVXRanking.db")
cur = conn.cursor()

for tid in range(164,1412):
    print('Loading...' + str(tid))
    sql = "insert into TrackList (TrackID) VALUES (?);"
    cur.execute(sql,(str(tid+1000),))
    for i in range(0,8):
        req = Request('http://anzuinfo.me/trackData.html?trackID='+str(tid).zfill(4)+imgchrList[i])
        res = urlopen(req)
        html = res.read().decode('utf8')

        bs = BeautifulSoup(html, 'html.parser')
        TrackData = bs.findAll('table', attrs={'class': 'trackData'})

        for tracks in TrackData:
            findlv = 'lv '+DiffList[i]
            TrackLevel = tracks.find('div', attrs={'class': findlv})
            if TrackLevel is None:
                continue
            TrackLevel = TrackLevel.text
            TrackDifficulty = DiffList[i].upper()
            TrackTitle = tracks.find('td', attrs={'class': 'title'}).text
            sql = "update TrackList SET TrackTitle = :Title, "+TrackDifficulty+" = :Lv where TrackID = :ID;"
            cur.execute(sql,{'Title': TrackTitle, 'Lv': TrackLevel, 'ID':str(tid+1000)})
    conn.commit()
    sleep(0.02)

conn.close()


# Importing modules
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import joblib
import findspark
findspark.init()
from pyspark.sql import SparkSession
from pyspark.ml import PipelineModel
from pyspark.sql.functions import *

# Configure spark session
spark = SparkSession\
    .builder\
    .master('local[2]')\
    .appName('quake_etl')\
    .config('spark.jars.package', 'org.mongodb.spark:mongo-spark-connector 2.12:2.4.1')\
    .getOrCreate()
from bokeh.io import output_notebook, output_file
from bokeh.plotting import figure, show, ColumnDataSource
from bokeh.models.tools import HoverTool
import math
from math import pi
from bokeh.palettes import Category20c
from bokeh.transform import cumsum
from bokeh.tile_providers import CARTODBPOSITRON, get_provider, Vendors
from bokeh.themes import built_in_themes
from bokeh.io import curdoc
import warnings
warnings.filterwarnings('ignore')
from pyspark.sql.functions import desc

df=joblib.load('./joblibs/df.joblib')
data=spark.createDataFrame(df)
df_quake_freq=joblib.load('./visualization_files/df_quake_freq.joblib')
df_pred=joblib.load('./visualization_files/rffpred.joblib')
def svm(a):
    clf=joblib.load('./joblibs/svmModel.joblib')
    a=np.array([a])
    y_pred_svm=clf.predict(a)
    return y_pred_svm[0]/10
def dt(a):
    a=np.array([a])
    dc=joblib.load('./joblibs/dtModel.joblib')
    y_pred_dc=dc.predict(a)
    return y_pred_dc[0]
def rf(a):
    pipe=PipelineModel.load("./joblibs/rfmodel.model_v0")
    ip = pd.DataFrame(np.array([a]))
    ip.columns=['Latitude','Longitude','Depth']
    dip=spark.createDataFrame(ip)
    pred_results_RF = pipe.transform(dip)
    return pred_results_RF.collect()[0][4]
def knn(a):
    from sklearn.neighbors import KNeighborsRegressor
    #Seperating X and y
    X=df[['Year','Latitude','Longitude','Depth']]
    y=df[['Magnitude']]
    kneigh=KNeighborsRegressor(n_neighbors = 5)
    kneigh.fit(X, y.values.ravel())
    a=np.array([a])
    y_pred_knn=kneigh.predict(a)
    return y_pred_knn[0]
def style(p):

        p.title.align='center'
        p.title.text_font_size = '20pt'
        p.title.text_font = 'serif'

        p.xaxis.axis_label_text_font_size = '14pt'
        p.xaxis.axis_label_text_font_style= 'bold'
        p.yaxis.axis_label_text_font_size = '14pt'
        p.yaxis.axis_label_text_font_style= 'bold'

        p.xaxis.major_label_text_font_size = '12pt'
        p.yaxis.major_label_text_font_size = '12pt'

        p.legend.location = 'top_left'
        return p
def showmap():

        # Earthquakein Map Representation
    df_quakes_2016 = data[data['Year']==2016]

    df_quakes_2016=df_quakes_2016.toPandas()

    def plotMap():
        lat = df_quakes_2016['Latitude'].values.tolist()
        lon = df_quakes_2016['Longitude'].values.tolist()

        pred_lat = df_pred['Latitude'].values.tolist()
        pred_lon = df_pred['Longitude'].values.tolist()

        lst_lat = []
        lst_lon = []
        lst_pred_lat = []
        lst_pred_lon = []

        i=0
        j=0

        for i in range (len(lon)):
            r_major = 6378137.000
            x = r_major * math.radians(lon[i])
            scale = x/lon[i]
            y = 180.0/math.pi * math.log(math.tan(math.pi/4.0 +
            lat[i] * (math.pi/180.0)/2.0)) * scale

            lst_lon.append(x)
            lst_lat.append(y)
            i += 1

        for j in range (len(pred_lon)):
            r_major = 6378137.000
            x = r_major * math.radians(pred_lon[j])
            scale = x/pred_lon[j]
            y = 180.0/math.pi * math.log(math.tan(math.pi/4.0 +
            pred_lat[j] * (math.pi/180.0)/2.0)) * scale

            lst_pred_lon.append(x)
            lst_pred_lat.append(y)
            j += 1

        df_quakes_2016['coords_x'] = lst_lat
        df_quakes_2016['coords_y'] = lst_lon
        df_pred['coords_x'] = lst_pred_lat
        df_pred['coords_y'] = lst_pred_lon

        df_quakes_2016['Mag_Size'] = df_quakes_2016['Magnitude'] * 4
        df_pred['Mag_Size'] = df_pred['Pred_Magnitude'] * 4

        lats = df_quakes_2016['coords_x'].tolist()
        longs = df_quakes_2016['coords_y'].tolist()
        mags = df_quakes_2016['Magnitude'].tolist()
        years = df_quakes_2016['Year'].tolist()
        mag_size = df_quakes_2016['Mag_Size'].tolist()

        pred_lats = df_pred['coords_x'].tolist()
        pred_longs = df_pred['coords_y'].tolist()
        pred_mags = df_pred['Pred_Magnitude'].tolist()
        pred_year = df_pred['Year'].tolist()
        pred_mag_size = df_pred['Mag_Size'].tolist()

        cds = ColumnDataSource(
        data=dict(
        lat=lats,
        lon=longs,
        mag=mags,
        year=years,
        mag_s=mag_size
        )
        )

        pred_cds = ColumnDataSource(
        data=dict(
        pred_lat=pred_lats,
        pred_long=pred_longs,
        pred_mag=pred_mags,
        year=pred_year,
        pred_mag_s=pred_mag_size
        )
        )

        TOOLTIPS = [( "Year", "@year"), ("Magnitude", "@mag"),("Predicted Magnitude", "@pred_mag")
        ]

        p = figure(title = 'Earthquake Map',
        plot_width=2300, plot_height=450,
        x_range=(-2000000, 6000000),
        y_range=(-1000000, 7000000),
        tooltips=TOOLTIPS)

        p.circle(x='lon', y='lat', size='mag_s', fill_color='#cc0000', fill_alpha=0.7,
        source=cds, legend='Quakes 2016')

        p.circle(x='pred_long', y='pred_lat', size='pred_mag_s', fill_color='#ccff33', fill_alpha=7.0,
        source=pred_cds, legend='Predicted Quakes 2017')

        tile_provider = get_provider(Vendors.CARTODBPOSITRON)
        p.add_tile(tile_provider)

        p.title.align='center'
        p.title.text_font_size='20pt'
        p.title.text_font='serif'

        p.legend.location='bottom_right'
        p.legend.background_fill_color='black'
        p.legend.background_fill_alpha=0.8
        p.legend.click_policy='hide'
        p.legend.label_text_color='white'
        p.xaxis.visible=False
        p.yaxis.visible=False
        p.axis.axis_label=None
        p.axis.visible=False
        p.grid.grid_line_color=None
        show(p)
    df_quakes_2016['Magnitude']
    plotMap()

def freqgraph():
    #Frequency of Earthquake By Year
    def plotBar():

        cds = ColumnDataSource(data=dict(
            yrs= df_quake_freq[ 'Year'].values.tolist(),
            numQuakes = df_quake_freq['Counts'].values.tolist()))

        TOOLTIPS =[ ('Number of earthquakes','@numQuakes'),('Year','@yrs')]

        barChart = figure(title='Frequency of Earthquakes by Year',
                    plot_height=400,
                    plot_width=1150,
                    x_axis_label='Years',
                    y_axis_label='Number of Occurances',
                    x_minor_ticks=2,
                    y_range=(0, df_quake_freq['Counts'].max() +100),
                    toolbar_location=None,
                    tooltips=TOOLTIPS)

        print(cds)
        barChart.vbar (x='yrs', bottom=0, top='numQuakes',
                color='#cc0000', width=0.75,
                legend='Year', source=cds)

        barChart = style(barChart)
        show(barChart)
        return barChart
    plotBar()

def maggraph():
    def plotMagnitude():

        cds= ColumnDataSource(data=dict(
        yrs = df_quake_freq[ 'Year'].sort_values().values.tolist(),
        avg_mag = df_quake_freq['avg(Magnitude)'].round(1).values.tolist(),
        max_mag= df_quake_freq [ 'max(Magnitude)'].values.tolist()))

        TOOLTIPS = [('Year', '@yrs'),('avg(Magnitude)', '@avg_mag'),('max(Magnitude)','@max_mag')]

        mp = figure(title='Maximum and Average Magnitude by Year',
                    plot_width=1150,
                    plot_height=400,
        x_axis_label='Years',
        y_axis_label='Magnitude', y_range=(5, df_quake_freq[ 'max(Magnitude)'].max() + 1),
        x_minor_ticks=2,
        toolbar_location=None,
        tooltips= TOOLTIPS)

        mp.line(x='yrs', y='max_mag', color='#cc0000', line_width=2, legend= 'Max Magnitude', source=cds)
        mp.circle(x='yrs', y='max_mag', color='#cc0000', size=8, fill_color='#cc0000', source=cds)

        mp.line(x='yrs', y='avg_mag', color='yellow', line_width=2, legend = 'Avg Magnitude', source=cds)
        mp.circle(x='yrs', y='avg_mag', color='yellow', size=8, fill_color='yellow', source=cds)
        mp =style(mp)
        show(mp)
        return mp

    plotMagnitude()


import PySimpleGUI as sg  # Simple GUI for Python
import core
import guiElements

import constants as const

# Setup the simple window
sg.theme("Black")

layout = [
    [guiElements.frameSelectJobAndZone],
    [guiElements.frameSelectResource],
    [guiElements.frameAssignAKey],
    [guiElements.layoutStatusAndStartStopBtns],
]

# Create the Window
window = sg.Window(
    title="Wakfu FarmBot 0.1",
    layout=layout,
    size=(400, 350),
    element_justification="c",
    element_padding=10,
)

# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:  # if user closes windows
        break
    if event == "button_start":
        core.onClick_Start_Button(event, values, window)

    if event == "button_stop":
        core.onClick_Stop_Button(event, values, window)

    if event == "combo_key":
        core.onChange_Key_Combo(event, values, window)

    if event == "combo_zone":
        core.onChange_Zone_Combo(event, values, window)

    if event == "combo_resource":
        core.onChange_Resource_Combo(event, values, window)

    if event == "combo_job":
        core.onChange_Job_Combo(event, values, window)

window.close()


__all__ = ["save", "load", "load_state_dict", "arange", "cat", "cos", "clamp", "Device", "from_numpy", "flatten",
           "LongTensor", "matmul", "mm", "normal", "ones", "x2ms_pow", "sin", "tanh", "x2ms_tensor", "Tensor",
           "split", 'as_tensor', 'argmax', 'Generator', 'sigmoid', 'rand', 'floor', 'bernoulli', 'equal', 'var_mean',
           'randperm', 'sqrt', 'stack', 'log', 'exp', 'typename', 'is_tensor', 'randn', 'FloatTensor', 'x2ms_max',
           'x2ms_min', 'bmm', 'x2ms_abs', 'square', 'squeeze', 'unsqueeze', 'transpose', 'repeat_interleave', 'div',
           'ones_like', 'where', 'tensordot', 'meshgrid', 'roll', 'linspace', 'full', 'empty', 'x2ms_sum',
           'multinomial', 'gather', 'sort', 'topk', 'x2ms_all', 'cumsum', 'einsum', 'full_like', 'masked_select',
           'x2ms_mean', 'mul', 'isfinite', 'diag', 'acos', 'add', 'argsort', 'asin', 'atan2', 'bincount',
           'broadcast_tensors', 'chunk', 'conj', 'cosh', 'cross', 'cumprod', 'diagflat', 'x2ms_diagonal', 'eq',
           'zeros_like', 'atan', 'unique', 'triu', 'nonzero', 'log2', 'cdist', 'erf', 'softmax', 'eye', 'prod', 'norm',
           'zeros', 'lt', 'ge', 'ne', 'le', 'reshape', 'reminder', 'result_type', 'real', 'reciprocal', 'neg', 'isinf',
           'isnan', 'argmin', 'floor_divide', 'fmod', 'empty_like', 'erfc', 'erfinv', 'expm1', 'flip', 'gt',
           'bitwise_and', 'bitwise_or', 'bitwise_xor', 'bartlett_window', 'blackman_window', 'hamming_window', 'histc',
           'imag', 'ceil', 'lerp', 'log1p', 'logical_and', 'logical_not', 'logical_or', 'logical_xor', 'var', 'unbind',
           'trunc', 'true_divide', 'triu_indices', 'triu', 'tril', 'trapz', 'trapezoid', 'trace', 'tan', 'take', 'lt',
           'ge', 'ne', 'le', 'reshape', 'reminder', 'result_type', 'real', 'reciprocal', 'neg',
           'minimum', 'hann_window', 'dot', 'scatter', 'ger', 'addmm', 'BoolTensor', 'finfo', 'IntTensor',
           'get_rng_state', 'set_rng_state', 'randint', 'randn_like', 'ByteTensor', 'index_select', 'allclose', 't',
           'vstack', 'rsqrt', 'x2ms_round', 'acosh', 'addcmul', 'addcdiv', 'asinh', 'atanh', 'amax', 'amin',
           'cummax', 'cummin', 'logsumexp', 'renorm', 'xlogy', 'sign', 'sinh', 'less', 'narrow', 'tensor_zeros_like',
           'Size', 'DoubleTensor', 'cosine_similarity', 'block_diag', 'cholesky_solve', 'lu_solve', 'x2ms_any',
           'greater', 'greater_equal', 'less_equal', 'not_equal', 'multiply', 'logspace', 'tril_indices', 'vander',
           'atleast_1d', 'atleast_2d', 'atleast_3d', 'column_stack', 'rad2deg', 'outer', 'negative', 'log10',
           'count_nonzero', 'signbit', 'isposinf', 'isin', 'isneginf', 'copysign', 'deg2rad', 'diff', 'gcd',
           'heaviside']

import numbers
import numpy as np
import mindspore
import mindspore.default_config
import mindspore.nn
import mindspore.numpy
import mindspore.dataset.transforms
import mindspore.ops.functional as F
from mindspore.nn import DistributedGradReducer
from mindspore.ops import composite
from mindspore.parallel._utils import (_get_device_num, _get_gradients_mean, _get_parallel_mode)
from mindspore.common.parameter import ParameterTuple
from mindspore.context import ParallelMode
from .core.decorator import x2ms_func_decorator
from .torch_api.save_load import save, load, load_state_dict
from .torch_api import tensor_api
from .torch_api import autograd
from .third_party_adapter import math_api
from .torch_api.nn_api import nn_cell
from .torch_api.nn_api import nn_init
from .torch_api.nn_api import nn_functional
from .core.context import x2ms_context
from .utils.util_api import logger
from .torch_api.torch_base_api import arange, cat, cos, clamp, Device, from_numpy, flatten, \
    LongTensor, matmul, mm, normal, ones, x2ms_pow, sin, tanh, x2ms_tensor, Tensor, zeros, split, as_tensor, dot, \
    x2ms_sum, argmax, Generator, sigmoid, rand, floor, bernoulli, equal, randperm, var_mean, sqrt, stack, log, exp, \
    typename, is_tensor, randn, FloatTensor, x2ms_max, x2ms_min, bmm, x2ms_abs, square, squeeze, unsqueeze, \
    transpose, repeat_interleave, div, ones_like, where, tensordot, meshgrid, roll, linspace, full, empty, \
    multinomial, gather, sort, topk, x2ms_all, cumsum, einsum, full_like, masked_select, x2ms_mean, mul, isfinite, \
    diag, acos, add, argsort, asin, atan2, bincount, broadcast_tensors, chunk, conj, cosh, cross, cumprod, \
    diagflat, x2ms_diagonal, zeros_like, atan, unique, nonzero, log2, cdist, erf, softmax, eye, prod, norm, \
    lt, ge, eq, ne, le, reshape, reminder, result_type, real, reciprocal, neg, isinf, isnan, argmin, floor_divide, \
    fmod, empty_like, erfc, erfinv, expm1, flip, gt, bitwise_and, bitwise_or, bitwise_xor, bartlett_window, \
    blackman_window, hamming_window, histc, imag, ceil, lerp, log1p, logical_and, logical_not, logical_or, \
    logical_xor, var, unbind, trunc, true_divide, triu_indices, triu, tril, trapz, trapezoid, trace, tan, take, \
    minimum, hann_window, scatter, ger, addmm, BoolTensor, finfo, IntTensor, get_rng_state, set_rng_state, randint, \
    randn_like, ByteTensor, index_select, allclose, t, vstack, rsqrt, x2ms_round, acosh, addcmul, addcdiv, asinh, \
    atanh, amax, amin, cummax, cummin, logsumexp, renorm, xlogy, sign, sinh, less, narrow, tensor_zeros_like, Size, \
    DoubleTensor, cosine_similarity, block_diag, cholesky_solve, lu_solve, x2ms_any, greater, greater_equal, \
    less_equal, not_equal, multiply, logspace, tril_indices, vander, atleast_1d, atleast_2d, atleast_3d, \
    column_stack, rad2deg, outer, negative, log10, count_nonzero, signbit, isposinf, isin, isneginf, copysign, \
    deg2rad, diff, gcd, heaviside

# overwrite Magic methods
mindspore.Tensor.__and__ = tensor_api.tensor_and
mindspore.Tensor.__or__ = tensor_api.tensor_or
mindspore.Tensor.__format__ = tensor_api.tensor_format
mindspore.Tensor.__getitem__ = tensor_api.tensor_getitem
mindspore.Tensor.__matmul__ = tensor_api.matmul
mindspore.Tensor.__setitem__ = tensor_api.tensor_setitem
mindspore.Tensor.T = tensor_api.transpose_

mindspore.Tensor.__float__ = lambda t: float(t.asnumpy())
mindspore.Tensor.__int__ = lambda t: int(t.asnumpy())

mindspore.Parameter.__iadd__ = tensor_api.parameter_iadd
mindspore.Parameter.__isub__ = tensor_api.parameter_isub
mindspore.Parameter.__imul__ = tensor_api.parameter_imul
mindspore.Parameter.__idiv__ = tensor_api.parameter_idiv

# overwrite properties
mindspore.Tensor.is_cuda = tensor_api.is_cuda
mindspore.Tensor.data = tensor_api.property_data
mindspore.Tensor.device = tensor_api.property_device
mindspore.Parameter.grad = tensor_api.grad
mindspore.Parameter.grad = tensor_api.set_grad

@property
def parameter_data(self):
    return self

@parameter_data.setter
def set_data(self, new_data):
    self.set_data(new_data)

mindspore.Parameter.data = parameter_data
mindspore.Parameter.data = set_data

def _get_calculate_shape(obj, other):
    if not isinstance(other, mindspore.Tensor):
        return obj.shape
    return np.broadcast_shapes(obj.shape, other.shape)

def _replace_tensor_calculate_func(origin_func_name, output_type=None):
    origin_func = getattr(mindspore.Tensor, origin_func_name)

    def new_func(obj, other):
        if obj.dtype == mindspore.float64:
            obj = obj.astype(mindspore.float32)
        if isinstance(other, np.ndarray):
            other = mindspore.Tensor(other, obj.dtype)
        if obj.size == 0 or (isinstance(other, mindspore.Tensor) and other.size == 0):
            if output_type is None:
                return mindspore.ops.Zeros()(_get_calculate_shape(obj, other), obj.dtype)
            else:
                return mindspore.ops.Zeros()(_get_calculate_shape(obj, other), output_type)
        return origin_func(obj, other)

    setattr(mindspore.Tensor, origin_func_name, new_func)

for func_name in ("__add__", "__sub__", "__mul__", "__truediv__", "__mod__", "__pow__"):
    _replace_tensor_calculate_func(func_name)

for func_name in ("__lt__", "__gt__", "__le__", "__ge__", "__eq__", "__ne__"):
    _replace_tensor_calculate_func(func_name, mindspore.bool_)

class GraphTrainStep(mindspore.nn.TrainOneStepCell):
    def __init__(self, network, optimizer):
        super(GraphTrainStep, self).__init__(network, optimizer)

    def call_construct(self, *inputs):
        new_input = list(mindspore.Tensor(value, dtype=mindspore.float32)
                         if not isinstance(value, mindspore.Tensor)
                         else value
                         for value in inputs)
        return self.__call__(*new_input)

    def construct(self, *inputs):
        output = self.network(*inputs)
        loss = output[0]
        model_output = output[1:]
        sens = (F.fill(loss.dtype, loss.shape, self.sens),)
        for output_value in model_output:
            sens += self._get_sens(output_value)
        grads = self.grad(self.network, self.weights)(*inputs, sens)
        grads = self.grad_reducer(grads)
        F.depend(loss, self.optimizer(grads))
        return output

    @staticmethod
    def _get_sens(value):
        if isinstance(value, mindspore.Tensor):
            return (F.fill(value.dtype, value.shape, 0),)
        if isinstance(value, list):
            sens = []
            for tensor in value:
                sens.append(F.fill(tensor.dtype, tensor.shape, 0))
            return (sens,)
        if isinstance(value, tuple):
            sens = ()
            for tensor in value:
                sens += (F.fill(tensor.dtype, tensor.shape, 0),)
            return (sens,)
        return (0,)

def add_module(obj, name, module):
    setattr(obj, name, module)

classic_cell_init = mindspore.nn.Cell.__init__

def new_cell_init(self, auto_prefix=True, flags=None):
    classic_cell_init(self, auto_prefix, flags)
    self.training = True

# same name and inherit subclass api
mindspore.nn.Cell.add_module = add_module
mindspore.nn.Cell.__init__ = new_cell_init
mindspore.nn.Cell._modules = nn_cell._modules

@property
def is_floating_point(self):
    return self in (mindspore.float16, mindspore.float32, mindspore.float64)

mindspore.dtype.typing.Number.is_floating_point = is_floating_point

def cuda_set_device(device):
    pass

def is_cuda_available():
    """
       Stub function for torch.cuda.is_available.
       get the info from default_config.
    """
    return True

def memory_cached():
    return 0.0

def memory_reserved():
    return 0.0

def max_memory_reserved(device=None):
    return 0.0

def max_memory_allocated(device=None):
    return 0.0

def memory_allocated(device=None):
    return 0.0

def get_device():
    return mindspore.context.get_context('device_target')

@x2ms_func_decorator(mindspore.nn.Cell)
def parameters(obj, *args, **kwargs):
    return get_cell_params(obj, *args, **kwargs)

def get_cell_params(cell, recurse=True):
    return iter(cell.trainable_params(recurse) + cell.untrainable_params(recurse))

@x2ms_func_decorator(mindspore.nn.Cell)
def named_parameters(model, prefix='', recurse=True):
    return list(param for param in model.parameters_and_names(prefix, recurse))

@x2ms_func_decorator(mindspore.nn.Cell)
def named_modules(model, prefix=''):
    return model.cells_and_names(prefix)

@x2ms_func_decorator(mindspore.nn.Cell)
def graph_forward(obj, *args, **kwargs):
    return obj(*args, **kwargs)

@x2ms_func_decorator(mindspore.nn.Cell)
def forward(obj, *args, **kwargs):
    return obj.construct(*args, **kwargs)

@x2ms_func_decorator(mindspore.nn.Cell)
def x2ms_train(obj, *args, **kwargs):
    if len(obj.trainable_params()) > 0:
        if obj not in x2ms_context.amp_model:
            x2ms_context.amp_model.append(obj)
    return obj.set_train(*args, **kwargs)

@x2ms_func_decorator(mindspore.nn.Cell)
def x2ms_eval(obj, *args, **kwargs):
    return obj.set_train(False)

class TrainCellWithoutOptimizer(mindspore.nn.Cell):
    def __init__(self, network, sens=1.0):
        super(TrainCellWithoutOptimizer, self).__init__(auto_prefix=False)
        self.network = network
        self.network.set_grad()
        self.model_weights = ParameterTuple(parameters(network))
        self.grad = composite.GradOperation(get_by_list=True, sens_param=True)
        self.sens = sens
        self.parallel_flag = False
        self.grad_reducer = F.identity
        self.parallel_mode = _get_parallel_mode()
        self.parallel_flag = self.parallel_mode in (ParallelMode.DATA_PARALLEL, ParallelMode.HYBRID_PARALLEL)
        if self.parallel_flag:
            self.grads_mean = _get_gradients_mean()
            self.device_num = _get_device_num()
            self.grad_reducer = DistributedGradReducer(self.model_weights, self.grads_mean, self.device_num)

    def construct(self, *inputs):
        train_loss = self.network(*inputs)
        train_sens = F.fill(train_loss.dtype, train_loss.shape, self.sens)
        grads = self.grad(self.network, self.model_weights)(*inputs, train_sens)
        grads = self.grad_reducer(grads)
        for i, parameter in enumerate(self.model_weights):
            parameter.grad = grads[i]
        return train_loss

_wrapped_model_dict = {}

def train_one_step_cell(model, optimizer=None):
    key = id(model)
    if key in _wrapped_model_dict.keys():
        return _wrapped_model_dict.get(key)
    if x2ms_context.amp_opt_level is None or x2ms_context.amp_model is None:
        if optimizer is None:
            wrapped_model = TrainCellWithoutOptimizer(model)
        else:
            wrapped_model = mindspore.nn.TrainOneStepCell(model, optimizer)
    else:
        if isinstance(x2ms_context.loss_scale, numbers.Number) and x2ms_context.amp_opt_level != "O2":
            wrapped_model = mindspore.amp.build_train_network(model, optimizer, level=x2ms_context.amp_opt_level,
                                                              loss_scale_manager=mindspore.FixedLossScaleManager(
                                                                  x2ms_context.loss_scale))
        else:
            wrapped_model = mindspore.amp.build_train_network(model, optimizer, level=x2ms_context.amp_opt_level)
    _wrapped_model_dict[key] = wrapped_model
    return wrapped_model

def graph_train_one_step_cell(model, optimizer):
    key = id(model)
    if key in _wrapped_model_dict.keys():
        return _wrapped_model_dict.get(key)
    if x2ms_context.amp_opt_level is None or x2ms_context.amp_model is None:
        wrapped_model = GraphTrainStep(model, optimizer)
    else:
        raise NotImplementedError("Graph mode does not currently support Mixed precision")
    _wrapped_model_dict[key] = wrapped_model
    return wrapped_model

def load_state_dict_from_url(url, model_dir=None, map_location=None, progress=True, check_hash=False):
    """
    Current not support 'model_dir', 'map_location', 'progress', 'check_hash' parameter.
    """
    logger.warning("Not support load_state_dict_from_url now")
    return {}

def to(obj, *args, **kwargs):
    if isinstance(obj, mindspore.nn.Cell):
        return _cell_to(obj, *args, **kwargs)
    elif isinstance(obj, mindspore.Tensor):
        return _tensor_to(obj, *args, **kwargs)
    else:
        return obj.to(*args, **kwargs)

def _cell_to(obj, *args, **kwargs):
    if args:
        param = args[0]
        if isinstance(param, mindspore.Type) and param in (mindspore.float16, mindspore.float32):
            return obj.to_float(dst_type=param)
        if isinstance(param, mindspore.Tensor) and param.dtype in (mindspore.float16, mindspore.float32):
            return obj.to_float(dst_type=param.dtype)
    if len(args) > 1:
        param = args[1]
        if param in (mindspore.float16, mindspore.float32):
            return obj.to_float(dst_type=param)
    if 'dtype' in kwargs.keys() and kwargs['dtype'] in (mindspore.float16, mindspore.float32):
        return obj.to_float(dst_type=kwargs['dtype'])
    if 'other' in kwargs.keys() and kwargs['other'].dtype in (mindspore.float16, mindspore.float32):
        return obj.to_float(dst_type=kwargs['other'])
    return obj

def _tensor_to(obj, *args, **kwargs):
    if args:
        param = args[0]
        if isinstance(param, mindspore.common.Type):
            return obj.astype(dtype=param)
        if isinstance(param, mindspore.Tensor):
            return obj.astype(dtype=param.dtype)
    if len(args) > 1:
        return obj.astype(dtype=args[1])
    if 'dtype' in kwargs.keys():
        return obj.astype(dtype=kwargs['dtype'])
    if 'other' in kwargs.keys():
        return obj.astype(dtype=kwargs['other'].dtype)
    return obj

def get_device_properties(device):
    return CUDADeviceProperty()

def convert_sync_batchnorm(module, process_group=None):
    return module

class CUDADeviceProperty:
    def __init__(self):
        device_target = mindspore.context.get_context('device_target')
        device_id = mindspore.context.get_context('device_id')
        self.name = f'{device_target}:{device_id}'
        self.total_memory = 0


from __future__ import annotations
from dataclasses import dataclass
from typing import Optional
import re
import string

@dataclass
class Equipment:
    title: str
    value: str
    unit: Optional[str] = None
    quantity: Optional[int] = None

_equipment = [
    ("Backpack", "2gp"),
    ("Candle", "1cp"),
    ("Chain, 10'", "30gp"),
    ("Chalk, 1 piece", "1cp"),
    ("Chest, empty", "2gp"),
    ("Crowbar", "2gp"),
    ("Flask, empty", "3cp"),
    ("Flint & steel", "15cp"),
    ("Grappling hook", "1gp"),
    ("Hammer, small", "5sp"),
    ("Holy symbol", "25gp"),
    ("Holy water, 1 vial**", "25gp"),
    ("Ironspikes, each", "1sp"),
    ("Lantern", "10gp"),
    ("Mirror, hand-sized", "10gp"),
    ("Oil, 1 flask***", "2sp"),
    ("Pole, 10-foot", "15cp"),
    ("Rations, per day", "5cp"),
    ("Rope, 50'", "25cp"),
    ("Sack, large", "12cp"),
    ("Sack, small", "8cp"),
    ("Thieves' tools", "25gp"),
    ("Torch, each", "1cp"),
    ("Waterskin", "5sp"),
]

EQUIPMENT = {}
#: Process the initial list of equipment tuples into a dict of Equipment classes
for name, cost in _equipment:
    kwargs = {}
    title = name.split(", ")[0]
    kwargs["title"] = title
    kwargs["value"] = cost
    if len(name.split(", ")) > 1:
        quantity = name.split(", ")[1]
        try:
            digits = int("".join([c for c in quantity if c.isdigit()]))
        except IndexError:
            digits = None

        if digits:
            kwargs["quantity"] = digits
            kwargs["unit"] = re.sub(r"[\d+\s]", "", quantity)
    EQUIPMENT[title] = Equipment(**kwargs)


from django import forms
from django.utils.translation import ugettext_lazy as _
from .models import Camera

class CameraAddForm(forms.ModelForm):
    class Meta:
        model = Camera
        fields = ['title', 'ip']
        labels = {
            'title': _('Titel'),
            'ip': _('Adresse')
        }


# Definition for a Node.
from collections import deque
from typing import List

class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children

class Solution:
    def preorder(self, root: Node) -> List[int]:
        output = []

        if root is None:
            return output

        def dps(node: Node):
            output.append(node.val)

            if node.children:
                for child in node.children:
                    dps(child)

        dps(root)

        return output

    def preorder_iter(self, root: Node) -> List[int]:
        output = []

        if root is None:
            return output

        stack = deque()
        stack.append(root)

        while stack:
            popped_node = stack.pop()

            output.append(popped_node.val)

            if popped_node.children:
                for child in reversed(popped_node.children):
                    stack.append(child)

        return output


import pyautogui
import webbrowser as web
import time

msg = input('enter message to send: ')
times = int(input('enter the number of times to send the message: '))

# win_chrome_path = 'C:\Program Files\Google\Chrome\Application\chrome.exe %s'
# web.get(win_chrome_path).open('web.whatsapp.com')

web.open('web.whatsapp.com')

time.sleep(30)

for i in range(times):
    for char in msg:
        pyautogui.press('space' if char==' ' else char)
    pyautogui.press('enter')


from django import forms
from django.core.mail import EmailMessage

from .models import Householdaccountbook

# class HouseholdaccountbookForm(forms.ModelForm):
#   class Meta:
#     model = Householdaccountbook
#     fields = ["pref","choice",]
class TableCreateForm(forms.ModelForm):
  class Meta:
    model = Householdaccountbook
    fields = ('title', 'choice', 'date', 'genre', 'quanity', 'money', 'content' )

  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    for field in self.fields.values():
      field.widget.attrs['class'] = 'form-control'
    # self.fields['date'].widget.attrs['class'] = 'form-control'
    # self.fields['date'].widget.attrs['id'] = "inputdate"
    self.fields['date'].widget.attrs['placeholder'] = "例 2000-12-05"


from telegram.ext import Updater, MessageHandler,Filters
from Adafruit_IO import Client
import os

aio = Client('adeebsheriff', os.getenv('adeebsheriff'))

def demo1(bot,update):
  chat_id = bot.message.chat_id
  path = 'https://cdn3.vectorstock.com/i/1000x1000/87/22/i-am-fine-lettering-typography-calligraphy-overlay-vector-15208722.jpg'
  bot.message.reply_text('I am fine')
  update.bot.sendPhoto(chat_id=chat_id,photo=path)
def demo2(bot,update):
  chat_id = bot.message.chat_id
  path = 'https://static.scientificamerican.com/sciam/cache/file/2B38DE31-C1D3-4339-8808D61972976EE4.jpg'
  bot.message.reply_text('Light is turned ON')
  aio.send('bedroom-light', 1)
  data1 = aio.receive('bedroom-light')
  print(f'Received value: {data1.value}')
  update.bot.sendPhoto(chat_id=chat_id,photo=path)
def demo3(bot,update):
  chat_id = bot.message.chat_id
  path = 'https://image.shutterstock.com/image-photo/light-bulb-turned-off-over-260nw-320485652.jpg'
  bot.message.reply_text('Light is turned OFF')
  aio.send('bedroom-light', 0)
  data1 = aio.receive('bedroom-light')
  print(f'Received value: {data1.value}')
  update.bot.sendPhoto(chat_id=chat_id,photo=path)
def demo4(bot,update):
  chat_id = bot.message.chat_id
  path = 'https://cdn.frontdoorhome.com/ahs/blog/prod/static/cs/ahs/image/running-fan.jpg'
  bot.message.reply_text('Fan is turned ON')
  aio.send('bedroom-fan', 1)
  data2 = aio.receive('bedroom-fan')
  print(f'Received value: {data2.value}')
  update.bot.sendPhoto(chat_id=chat_id,photo=path)
def demo5(bot,update):
  chat_id = bot.message.chat_id
  path = 'https://www.destinationlighting.com/fliptheswitch/wp-content/uploads/sites/2/2018/05/zudio-casablanca.jpg'
  bot.message.reply_text('Fan is turned OFF')
  aio.send('bedroom-fan', 0)
  data2 = aio.receive('bedroom-fan')
  print(f'Received value: {data2.value}')
  update.bot.sendPhoto(chat_id=chat_id,photo=path)
def main(bot,update):
  a = bot.message.text.lower()
  print(a)

  if a == "how are you?":
    demo1(bot,update)
  elif a =="light on" or a=="turn on light":
    demo2(bot,update)
  elif a =="light off" or a=="turn off light":
    demo3(bot,update)
  elif a =="switch on fan" or a=="turn on fan":
    demo4(bot,update)
  elif a =="switch off fan" or a=="turn off fan":
    demo5(bot,update)
  else:
    bot.message.reply_text('Invalid Text')
BOT_TOKEN = os.getenv('BOT_TOKEN')
u = Updater(BOT_TOKEN,use_context=True)
dp = u.dispatcher
dp.add_handler(MessageHandler(Filters.text,main))
u.start_polling()
u.idle()


'''
사업자등록번호로 업종 확인
'''
import csv
import json
from urllib.request import urlopen
from urllib import parse
import datetime as dt
import pandas as pd
import numpy as np
import re

# 대신정보통신 4088118945
# 대보정보통신 1358119406
bizNo = '2118108009'
# 조달청_사용자정보서비스
'''
> parameter(조회조건)
- bizno : 사업자등록번호

> numOfRows 는 totalCount 보다 작아야 함
'''
url = 'http://apis.data.go.kr/1230000/UsrInfoService/getPrcrmntCorpIndstrytyInfo'
queryParams = '?' + parse.urlencode({ parse.quote_plus('serviceKey') : 'B1CsUiO26Y56VDOKIParM6z394FXvTQC0rafsREBzSnOl8Cc1PUFY98LOcqKq5OahD5s2AhvszA2AIIYj0KXvg==',
                                     parse.quote_plus('pageNo') : '1',
                                     parse.quote_plus('numOfRows') : 100,
                                     parse.quote_plus('type') : 'json' ,
                                     parse.quote_plus('bizno') : bizNo
                                    })

# set_API & get_data -> openAPI & parameters
response = urlopen(url + queryParams)
data = response.read()
JSON_object = json.loads(data.decode('utf-8'))

'''
"bizno": "1048118820",
          "indstrytyNm": "엔지니어링사업(프로젝트매니지먼트)",
          "indstrytyCd": "7309",
          "rgstDt": "2011-10-31 00:00:00",
          "vldPrdExprtDt": "",
          "systmRgstDt": "2014-06-30 16:12:45",
          "chgDt": "",
          "indstrytyStatsNm": "",
          "rprsntIndstrytyYn": "N",
          "systmChgDt": "2014-06-30 16:12:45"
'''
result = pd.DataFrame(JSON_object["response"]["body"]["items"], columns = ["bizno",
                                         "indstrytyNm",
                                         "rgstDt",
                                         "vldPrdExprtDt"])

#result

# init Series
s = []

for index, item in result.iterrows():
    if len(item['vldPrdExprtDt']) > 0:
        print(item['vldPrdExprtDt'] + ' -> ' + str(len(item['vldPrdExprtDt'])))
        print(item['indstrytyNm'])
        print(re.sub('\(.*?\)', '', item['indstrytyNm']))
        print('\n')
        s.append(re.sub('\(.*?\)', '', item['indstrytyNm']))

#s

# using naive method
# to remove duplicated
# from list
res = []
for i in s:
    if i not in res:
        res.append(i)

#res
#
print('-'.join(res))


import wx

from ..form.general import GeneralDialog
from ..textbox import LayoutDimensions
from ..textbox.textbox import TextInputLayout, TextSmartBox
from ..textbox.floatbox import FloatInputLayout, FloatSmartBox
from ..controller import ChildController

from ..model.bind import BindOjbect

__author__ = 'Joeny'

class FigureSetting(object):
    """
    Figure Setting model.

    """
    def __init__(self, *args, **kwargs):
        """
        Figure Setting Constructor

        :param args:
        :param kwargs:
        :return:
        """
        self.title = kwargs.get('title', 'Title')
        self.x_title = kwargs.get('x_title', 'X Title')
        self.x_subtitle = kwargs.get('x_subtitle', '')
        self.y_title = kwargs.get('y_title', 'Y Title')
        self.y_subtitle = kwargs.get('y_subtitle', '')

        self.x_min = kwargs.get('x_min', None)
        self.x_max = kwargs.get('x_max', None)
        self.y_min = kwargs.get('y_min', None)
        self.y_max = kwargs.get('y_max', None)

        self.linewidth = kwargs.get('linewidth', 2)
        self.legend = kwargs.get('legend', [])

class FigureSettingPanel(wx.Panel):
    """
    Particular Figure Setting

    """
    def __init__(self, parent, setting, *args, **kwargs):
        """

        :param setting:
        :param args:
        :param kwargs:
        :return:
        """
        wx.Panel.__init__(self, parent, *args, **kwargs)

        self.layouts = {}
        self.bind_objects = {}

        self.setting = setting

        self.SetSizerAndFit(self.do_layout())

    def do_layout(self):
        """
        Layout form

        :return:
        """
        vsizer = wx.BoxSizer(wx.VERTICAL)

        layout = LayoutDimensions(top=2, bottom=2, left=4, right=4, interior=2,
                                  widths=(100, 200),
                                  stretch_factor=(0, 1), height=24)
        layout.calculate()

        self.layouts['title'] = TextInputLayout(self,
                                                name='Title',
                                                layout=layout,
                                                textbox=TextSmartBox(self))
        self.layouts['x_title'] = TextInputLayout(self,
                                                  name='X Title',
                                                  layout=layout,
                                                  textbox=TextSmartBox(self))
        self.layouts['y_title'] = TextInputLayout(self,
                                                  name='Y Title',
                                                  layout=layout,
                                                  textbox=TextSmartBox(self))

        self.layouts['x_min'] = FloatInputLayout(self,
                                                 name='X Min',
                                                 layout=layout,
                                                 textbox=FloatSmartBox(self, signs=True))

        self.layouts['x_max'] = FloatInputLayout(self,
                                                 name='X Max',
                                                 layout=layout,
                                                 textbox=FloatSmartBox(self, signs=True))

        self.layouts['y_min'] = FloatInputLayout(self,
                                                 name='Y Min',
                                                 layout=layout,
                                                 textbox=FloatSmartBox(self, signs=True))

        self.layouts['y_max'] = FloatInputLayout(self,
                                                 name='Y Max',
                                                 layout=layout,
                                                 textbox=FloatSmartBox(self, signs=True))

        vsizer.AddSpacer(5)
        vsizer.Add(self.layouts['title'], 1, wx.EXPAND | wx.ALL, 0)
        vsizer.AddSpacer(5)
        vsizer.Add(self.layouts['x_title'], 1, wx.EXPAND | wx.ALL, 0)
        vsizer.AddSpacer(5)
        vsizer.Add(self.layouts['y_title'], 1, wx.EXPAND | wx.ALL, 0)
        vsizer.AddSpacer(5)
        vsizer.Add(self.layouts['x_min'], 1, wx.EXPAND | wx.ALL, 0)
        vsizer.AddSpacer(5)
        vsizer.Add(self.layouts['x_max'], 1, wx.EXPAND | wx.ALL, 0)
        vsizer.AddSpacer(5)
        vsizer.Add(self.layouts['y_min'], 1, wx.EXPAND | wx.ALL, 0)
        vsizer.AddSpacer(5)
        vsizer.Add(self.layouts['y_max'], 1, wx.EXPAND | wx.ALL, 0)
        vsizer.AddSpacer(5)

        return vsizer

    def sync_data(self):
        """
        Sync textbox data

        """
        self.bind_objects['title'] = BindOjbect(self.setting.__dict__,
                                                self.layouts['title'].textbox,
                                                'title')
        self.bind_objects['x_title'] = BindOjbect(self.setting.__dict__,
                                                  self.layouts['x_title'].textbox,
                                                  'x_title')
        self.bind_objects['y_title'] = BindOjbect(self.setting.__dict__,
                                                  self.layouts['y_title'].textbox,
                                                  'y_title')
        self.bind_objects['x_min'] = BindOjbect(self.setting.__dict__,
                                                self.layouts['x_min'].textbox,
                                                'x_min')
        self.bind_objects['x_max'] = BindOjbect(self.setting.__dict__,
                                                self.layouts['x_max'].textbox,
                                                'x_max')
        self.bind_objects['y_min'] = BindOjbect(self.setting.__dict__,
                                                self.layouts['y_min'].textbox,
                                                'y_min')
        self.bind_objects['y_max'] = BindOjbect(self.setting.__dict__,
                                                self.layouts['y_max'].textbox,
                                                'y_max')

class FigureSettingDialog(GeneralDialog):
    """
    Modify figure setting.

    """
    def __init__(self, parent, controller=None, setting=None, local=None, btn_flags=wx.OK | wx.CANCEL, **kwargs):
        """
        Figure setting dialog.

        :param parent:
        :param controller:
        :param setting:
        :param btn_flags:
        :param kwargs:
        :return:
        """
        self.nb = None
        self.pages = {}

        if local:
            self.local = local
            self.local.view = self
        else:
            self.local = FigureSettingController(parent, self, setting)

        GeneralDialog.__init__(self,
                               parent,
                               title="Figure Setting",
                               style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
                               controller=controller,
                               local=self.local,
                               btn_flags=btn_flags,
                               **kwargs)

        self.btnsizer.AffirmativeButton.Bind(wx.EVT_BUTTON, self.local.button_ok_click)

    def do_layout(self):
        """
        Draw layout

        :return:
        """
        self.nb = wx.Notebook(self)

        for index, setting in enumerate(self.local.settings):
            # Create Panel.
            self.pages[index] = FigureSettingPanel(self.nb, setting)

            # Add to tab page.
            self.nb.AddPage(self.pages[index], "Plot %d" % (index + 1))

            # Sync Data
            self.pages[index].sync_data()

        return self.nb

class FigureSettingController(ChildController):
    """
    Figure Setting Controller

    """
    def __init__(self, parent, view, settings):
        """

        :param parent:
        :param view:
        :return:
        """
        ChildController.__init__(self, parent, view, settings)

        self.settings = settings

    def sync_data(self):
        """
        Sync Data

        :return:
        """
        pass

    def do_layout(self):
        pass

    def refresh(self):
        pass

    def update_layout(self, state):
        pass

    def button_ok_click(self, event):
        """
        Button ok click

        :param event:
        :return:
        """
        error = False

        #TODO: Need to bind the textbox with the data.

        if error is False:
            event.Skip()
        else:
            if not wx.Validator_IsSilent():
                wx.Bell()

    def delete_control(self):
        pass


import mne
import argparse
import numpy as np
from config import fname

# Handle command line arguments
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('subject', metavar='sub###', type=int, help='The subject to process')
args = parser.parse_args()
subject = args.subject
print('Processing subject:', subject)

report = mne.open_report(fname.report(subject=subject))

# Fit ICA to the continuous data
raw_detrended = mne.io.read_raw_fif(fname.raw_detrend(subject=subject))
ica = mne.preprocessing.ICA(n_components=100).fit(raw_detrended)

# Get ICA components that capture eye blinks and heart beats
eog_epochs = mne.preprocessing.create_eog_epochs(raw_detrended)
_, eog_scores = ica.find_bads_eog(eog_epochs)
eog_bads = list(np.flatnonzero(abs(eog_scores) > 0.2))
ecg_epochs = mne.preprocessing.create_ecg_epochs(raw_detrended)
ecg_bads, ecg_scores = ica.find_bads_ecg(ecg_epochs)
ica.exclude = eog_bads + ecg_bads
print(eog_bads)
print(ecg_bads)

if len(eog_bads) > 0:
    report.add_figs_to_section(ica.plot_scores(eog_scores), 'Correlation between ICA components and EOG channel', 'ICA', replace=True)
    report.add_figs_to_section(ica.plot_properties(eog_epochs, picks=eog_bads), ['Properties of EOG component %02d' % e for e in eog_bads], 'ICA', replace=True)
if len(ecg_bads) > 0:
    report.add_figs_to_section(ica.plot_scores(ecg_scores), 'Correlation between ICA components and ECG channel', 'ICA', replace=True)
    report.add_figs_to_section(ica.plot_properties(ecg_epochs, picks=ecg_bads), ['Properties of ECG component %02d' % e for e in ecg_bads], 'ICA', replace=True)
report.add_figs_to_section(ica.plot_overlay(eog_epochs.average()), 'EOG signal removed by ICA', 'ICA', replace=True)
report.add_figs_to_section(ica.plot_overlay(ecg_epochs.average()), 'ECG signal removed by ICA', 'ICA', replace=True)

ica.save(fname.ica(subject=subject))

report.save(fname.report(subject=subject), overwrite=True, open_browser=False)
report.save(fname.report_html(subject=subject), overwrite=True, open_browser=False)


from nltk.tokenize import TweetTokenizer

class LexiconFeatureExtractor:
    def __init__(self, afinn_lexicon_file_path="resources/lexicons/AFINN-en-165.txt",
                 afinn_emoticon_lexicon_file_path="resources/lexicons/AFINN-emoticon-8.txt",
                 bing_liu_lexicon_file_path="resources/lexicons/BingLiu.txt",
                 mpqa_lexicon_file_path="resources/lexicons/mpqa.txt"):
        print("Loading AFINN lexicons...")
        self.afinn_lexicon = LexiconFeatureExtractor._read_standart_lexicon(afinn_lexicon_file_path)
        self.afinn_emoticon_lexicon = LexiconFeatureExtractor._read_standart_lexicon(afinn_emoticon_lexicon_file_path)
        print("Loading BingLiu lexicon...")
        self.bingliu_lexicon = LexiconFeatureExtractor._read_standart_lexicon(bing_liu_lexicon_file_path)
        print("Loading MPQA lexicon...")
        self.mpqa_lexicon = LexiconFeatureExtractor._read_standart_lexicon(mpqa_lexicon_file_path)
        print("Loading NRC - Hashtag - Emotion - Lexicon")
        self.nrc_hash_emo_lexicon = LexiconFeatureExtractor \
            ._read_labeled_lexicon("resources/lexicons/NRC-Hashtag-Emotion-Lexicon-v0.2.txt")
        print("Loading NRC - AffectIntensity - Lexicon")
        self.nrc_affect_intensity_lexicon = LexiconFeatureExtractor \
            ._read_labeled_lexicon("resources/lexicons/NRC-AffectIntensity-Lexicon.txt")
        print("Loading SentiStrength EmoticonLookupTable")
        self.emoticon_lookup_lexicon = LexiconFeatureExtractor \
            ._read_standart_lexicon("resources/lexicons/EmoticonLookupTable.txt")
        print("Loading SentiStrength EmotionLookupTable")
        self.emotion_lookup_lexicon = LexiconFeatureExtractor \
            ._read_standart_lexicon("resources/lexicons/EmotionLookupTable.txt")

    def extract_feature(self, input_txt):
        res = [LexiconFeatureExtractor.calculate_score_word_based(self.afinn_lexicon, input_txt),
               LexiconFeatureExtractor.calculate_score_word_based(self.afinn_emoticon_lexicon, input_txt),
               LexiconFeatureExtractor.calculate_score_word_based(self.bingliu_lexicon, input_txt),
               LexiconFeatureExtractor.calculate_score_word_based(self.mpqa_lexicon, input_txt)]

        # NRC - Hashtag - Emotion - Lexicon
        res += LexiconFeatureExtractor.calculate_score_labeled(self.nrc_hash_emo_lexicon, input_txt)
        # NRC - Affect intensity - Lexicon
        res += LexiconFeatureExtractor.calculate_multiscore(self.nrc_affect_intensity_lexicon, input_txt)
        # SentiStrength - Emoticon - Lexicon
        res.append(LexiconFeatureExtractor.calculate_score_word_based(self.emoticon_lookup_lexicon, input_txt))
        # SentiStrength - Emotion - Lexicon
        res.append(LexiconFeatureExtractor.calculate_score_word_based(self.emotion_lookup_lexicon, input_txt))

        return res

    @staticmethod
    def _read_standart_lexicon(file_path, delimeter="\t"):
        res = {}
        with(open(file_path, "r")) as f:
            for line in f:
                columns = line.strip().split(delimeter)
                if len(columns) > 1:
                    res[" ".join(columns[:-1]).strip(" ")] = float(columns[-1])
        return res

    @staticmethod
    def _read_multi_score_lexicon(file_path, delimeter="\t", ):
        res = {}
        with(open(file_path, "r")) as f:
            for line in f:
                scores = []
                columns = line.strip().split(delimeter)
                for i in range(1, len(columns)):
                    scores.append(float(columns[i]))
                res[columns[0]] = scores
        return res

    @staticmethod
    def _read_labeled_lexicon(file_path, delimeter="\t",
                              label_index=0, feature_index=1, score_index=2):
        res = {}
        with(open(file_path, "r")) as f:
            for line in f:
                columns = line.strip().split(delimeter)
                if len(columns) > 2:
                    if columns[label_index] not in res:
                        res[columns[label_index]] = {}
                    res[columns[label_index]][columns[feature_index]] = float(columns[score_index])
        return res

    @staticmethod
    def calculate_score_word_based(lexicon, input_txt):
        score = 0.0
        input_words = [t.encode("utf-8") for t in TweetTokenizer().tokenize(input_txt)]
        for k, v in lexicon.items():
            if " " not in k and k in input_words:
                score += v
            elif " " in k and LexiconFeatureExtractor.contains_all(k, input_words):
                score += v
        return score

    @staticmethod
    def calculate_multiscore(lexicon, input_txt, score_count=4):
        res = [0.0 for _ in range(score_count)]
        input_words = [t.encode("utf-8") for t in TweetTokenizer().tokenize(input_txt)]
        for label, d in lexicon.items():
            for k, v in d.items():
                scores = []
                if " " not in k and k in input_words:
                    scores.append(v)
                elif " " in k and LexiconFeatureExtractor.contains_all(k, input_words):
                    scores.append(v)
            for i in range(len(scores)):
                res[i] += scores[i]
        return res

    @staticmethod
    def calculate_score_labeled(lexicon, input_txt):
        res = []
        score = 0.0
        input_words = [t.encode("utf-8") for t in TweetTokenizer().tokenize(input_txt)]
        for label, d in lexicon.items():
            for k, v in d.items():
                score = 0.0
                if " " not in k and k in input_words:
                    score += v
                elif " " in k and LexiconFeatureExtractor.contains_all(k, input_words):
                    score += v
            res.append(score)
        return res

    @staticmethod
    def contains_all(words1, words2):
        for w in words1.split():
            if w not in words2:
                return False
        return True


#!/usr/bin/env python

import sys, os, shlex
import multiprocessing as mp
import subprocess as sp
from pli.lib.util import log

def find_files(root):
    outdir = 'tmpout'
    for curdir, dirs, files in os.walk(root):
        protein_fname = None
        ligand_fname = None
        for f in files:
            if f.endswith('_protein.pdb'):
                sym = f[1:3]
                protein_fname = f'{curdir}/{f}'
            elif f.endswith('_ligand.mol2'):
                ligand_fname = f'{curdir}/{f}'
        if protein_fname and ligand_fname:
            bname = os.path.basename(protein_fname).split('_')[0]
            sym = bname[1:3]
            if not os.path.exists(f'{outdir}/{sym}/{bname}_ALP.pkl.gz'):
                yield protein_fname, ligand_fname, sym

def worker(args):
    protein_iname, ligand_iname, sym = args
    cmd = f'python pli/bin/plifinder.py {protein_iname} {ligand_iname} -o tmpout/{sym}'
    log(cmd)
    proc = sp.Popen(shlex.split(cmd), universal_newlines=True, stdout=sp.PIPE, stderr=sp.STDOUT)
    ret = proc.wait()
    return ret, protein_iname, ligand_iname, sym

def main():
    root = 'v2015'
    pool = mp.Pool(mp.cpu_count())
    count = 0
    for ret, protein_iname, ligand_iname, sym in pool.imap(worker, find_files(root)):
        if ret != 0:
            log(f'!! Error {protein_iname} {ligand_iname}')
            continue
        count += 1
        log(f'{count} {protein_iname} {ligand_iname} -> {sym}')

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Mon Jun 22 11:31:56 2020

@author: admin
"""

from __future__ import print_function
import os, sys, time, argparse
from sensapex import SensapexDevice, UMP, UMPError

parser = argparse.ArgumentParser(
    description="Test for sensapex devices; prints position and status updates continuously.")
parser.add_argument('--group', type=int, default=0, help="Device group number")
args = parser.parse_args()

ump = UMP.get_ump(group=args.group)
devids = ump.list_devices()
devs = {i:SensapexDevice(i) for i in devids}

print("SDK version:", ump.sdk_version())
print("Found device IDs:", devids)

def print_pos(timeout=None):
    line = ""
    for i in devids:
        dev = devs[i]
        try:
            pos = str(dev.get_pos(timeout=timeout))
        except Exception as err:
            pos = str(err.args[0])
        pos = pos + " " * (30 - len(pos))
        line += "%d:  %s" % (i, pos)
    print(line)

t = time.time()
while True:
    t1 = time.time()
    dt = t1 - t
    t = t1
    line = "%3f" % dt
    for id in sorted(list(devs.keys())):
        line += "   %d: %s busy: %s" % (id, devs[id].get_pos(timeout=0), devs[id].is_busy())
    line += "                           \r"
    print(line, end=" ")
    sys.stdout.flush()
    time.sleep(0.01)


"""system URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from clubs import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),

    path('user_dashboard/', views.user_dashboard, name='user_dashboard'),
    path('user_profile/', views.user_profile, name='user_profile'),
    path('user_profile/edit', views.edit_user_profile, name='edit_user_profile'),
    path('user_profile/change_password', views.change_password, name='change_password'),
    path('user_profile/<int:user_id>/<int:membership_id>', views.user_profile, name='user_profile'),

    path('member_profile/<int:membership_id>', views.member_profile, name='member_profile'),

    path('log_in/', views.log_in, name='log_in'),
    path('log_out/', views.log_out, name='log_out'),
    path('sign_up/', views.sign_up, name='sign_up'),

    path('membership_application/', views.membership_application, name='membership_application'),

    path('new_club/', views.club_creation, name='new_club'),
    path('available_clubs/', views.available_clubs, name='available_clubs'),
    path('club/<int:club_id>', views.club_dashboard, name='club_dashboard'),

    path('club_memberships/', views.club_memberships, name='club_memberships'),
    path('my_applications/', views.my_applications, name='my_applications'),

    path('club/<int:club_id>/<int:user_id>/promote', views.promote_member, name='promote_member'),
    path('club/<int:club_id>/<int:user_id>/demote', views.demote_member, name='demote_member'),
    path('club/<int:club_id>/<int:user_id>/kick', views.kick_member, name='kick_member'),

    path('club/<int:club_id>/edit', views.edit_club, name='edit_club'),
    path('club/<int:club_id>/leave', views.leave_club, name='leave_club'),

    path('tournament/<int:tournament_id>', views.tournament_dashboard, name='tournament_dashboard'),

    path('club/<int:club_id>/transfer_ownership/<int:user_id>', views.transfer_ownership, name='transfer_ownership'),

    path('membership/<int:membership_id>/approve', views.accept_membership, name='accept_membership'),
    path('membership/<int:membership_id>/deny', views.reject_membership, name='reject_membership'),

    path('new_tournament/<int:club_id>', views.tournament_creation, name='new_tournament'),

    path('tournament/<int:tournament_id>/join', views.join_tournament, name='join_tournament'),
    path('tournament/<int:tournament_id>/leave', views.leave_tournament, name='leave_tournament'),
    path('tournament/<int:tournament_id>/cancel', views.cancel_tournament, name='cancel_tournament'),
    path('tournament/<int:tournament_id>/generate_matches', views.generate_matches, name='generate_matches')
]


import sys
from itertools import combinations
from collections import deque

def move(stage):
    for i in range(M):
        for j in range(N-1, 0, -1):
            enemy[j][i] = enemy[j-1][i]
    if stage == 0:
        for i in range(M):
            enemy[0][i] = 0

dr = [0, -1, 0]
dc = [-1, 0, 1]
N, M, D = map(int, sys.stdin.readline().split())
enemy = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
comb = list(combinations(range(N), 3))
visited = [[0] * M for _ in ' '*N]
shot = [[0] * M for _ in ' '*N]
q = deque()
shot_clear = deque()
result = 0
for stage in range(N):
    max_kill = 0
    for archers in comb:
        kill = 0
        for archer in archers:
            q.append((N, archer))
            dist = 0
            len_q = len(q)
            cnt = 0
            while q:
                if len_q == cnt:
                    len_q = len(q)
                    cnt = 0
                    dist += 1
                if dist == D:
                    q.clear()
                    break
                cnt += 1
                r, c = q.popleft()
                for d in range(3):
                    nr = r + dr[d]
                    nc = c + dc[d]
                    if 0 <= nr < N and 0 <= nc < M:
                        if enemy[nr][nc]:
                            if not shot[nr][nc]:
                                kill += 1
                                shot[nr][nc] = 1
                                shot_clear.append((nr, nc))
                            q.clear()
                            break
                        q.append((nr, nc))
            while shot_clear:
                r, c = shot_clear.popleft()
                enemy[r][c] = 0
                shot[r][c] = 0
        if kill > max_kill:
            max_kill = kill
    result += max_kill
    move(stage)
print(result)


"""DuerOS entity class."""
from __future__ import annotations

from dueros_smarthome.client import DeviceActionResponse
from dueros_smarthome.const import STATUS_OK, STATUS_NOT_LOGIN
from dueros_smarthome.models import Appliance, Connectivity

from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryAuthFailed, IntegrationError
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN, NAME, VERSION, LOGGER
from .coordinator import DuerOSDataUpdateCoordinator, get_unique_id

class DuerOSEntity(CoordinatorEntity):
    """DuerOSEntity class."""

    def _set_unique_id(self) -> None:
        self._attr_unique_id = get_unique_id(self._appliance)

    def __init__(
        self, coordinator: DuerOSDataUpdateCoordinator, appliance: Appliance
    ) -> None:
        """Initialize."""
        super().__init__(coordinator)
        self._attr_available = False
        self._attr_has_entity_name = True
        self._update(appliance)
        self._set_unique_id()
        self._attr_device_info = DeviceInfo(
            identifiers={(DOMAIN, self.unique_id)},
            name=NAME,
            model=VERSION,
            manufacturer=self._appliance.bot_name,
        )

    def _update(self, appliance: Appliance):
        self._appliance = appliance
        self._attr_available = (
            self._appliance.state_settings.connectivity.value == Connectivity.REACHABLE
        )
        self._attr_name = self._appliance.friendly_name

    @callback
    def _handle_coordinator_update(self) -> None:
        self._update(self.coordinator.data[self.unique_id])
        self.async_write_ha_state()

    @property
    def available(self) -> bool:
        return self._attr_available

    @staticmethod
    def _check_response(rsp: DeviceActionResponse) -> None:
        if STATUS_NOT_LOGIN == rsp.status:
            LOGGER.error(rsp.msg)
            raise ConfigEntryAuthFailed(rsp.msg)
        if STATUS_OK != rsp.status:
            LOGGER.error(rsp.msg)
            raise IntegrationError(rsp.msg)