from typing import Any, Dict, List, Type, TypeVar, Union

import attr

from ..types import UNSET, Unset

T = TypeVar("T", bound="ScimServiceProviderConfigBulk")

@attr.s(auto_attribs=True)
class ScimServiceProviderConfigBulk:
    """
    Attributes:
        supported (Union[Unset, bool]):
        max_operations (Union[Unset, int]):
        max_payload_size (Union[Unset, int]):
    """

    supported: Union[Unset, bool] = UNSET
    max_operations: Union[Unset, int] = UNSET
    max_payload_size: Union[Unset, int] = UNSET
    additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        supported = self.supported
        max_operations = self.max_operations
        max_payload_size = self.max_payload_size

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if supported is not UNSET:
            field_dict["supported"] = supported
        if max_operations is not UNSET:
            field_dict["maxOperations"] = max_operations
        if max_payload_size is not UNSET:
            field_dict["maxPayloadSize"] = max_payload_size

        return field_dict

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        d = src_dict.copy()
        supported = d.pop("supported", UNSET)

        max_operations = d.pop("maxOperations", UNSET)

        max_payload_size = d.pop("maxPayloadSize", UNSET)

        scim_service_provider_config_bulk = cls(
            supported=supported,
            max_operations=max_operations,
            max_payload_size=max_payload_size,
        )

        scim_service_provider_config_bulk.additional_properties = d
        return scim_service_provider_config_bulk

    @property
    def additional_keys(self) -> List[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties


from skimage.metrics import structural_similarity
import cv2
import numpy as np
import argparse
import os
SSIM_RES_FILE = 'dump/ssim.txt'

def compute_rise_ssim(uncal_img, cal_img):
    """
    """
    # cal = cv2.imread(args.cal)
    # uncal = cv2.imread(args.unc)
    (score, diff) = structural_similarity(cv2.imread(uncal_img), cv2.imread(cal_img), full=True, multichannel=True)
    with open(SSIM_RES_FILE, 'a') as f:
        f.writelines(str(round(score, 3)) + '\n')
    f.close()
    return score

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Training params')
    parser.add_argument('--unc', type=str, default='/u/dw3zn/Repos/saliency_calibration/rise_uncalibrated.png',
                        help='Uncalibrated saliency map')
    parser.add_argument('--cal', type=str, default='/u/dw3zn/Repos/saliency_calibration/rise_calibrated.png',
    help='Calibrated saliency map')
    args = parser.parse_args()
    compute_rise_ssim()


# -*- coding: utf-8
import pandas as pd
import dask.dataframe as dd
from pprint import pprint

pd.set_option('display.expand_frame_repr', False)

def transform_one(series):
    """Takes a Series object representing a grouped dataframe row,
    and returns a string (serialized JSON).

    :return: dict
    """
    data = series.to_dict()
    if not data:
        return pd.Series([], name='data')
    (customer, url, ts, _) = data.pop('index')
    page_views = data.pop('views')
    visitors = data.pop('visitors')
    data.update({
        'customer': customer,
        'url': url,
        'ts': ts.strftime('%Y-%m-%dT%H:%M:%S'),
        'metrics': {'views': page_views, 'visitors': visitors}
    })
    return pd.Series([data], name='data')

if __name__ == '__main__':
    pdf = pd.DataFrame.from_records([
        ("http://a.com/articles/1", "http://google.com/", "xxx", "2017-09-15 00:10:00", "a.com"),
        ("http://a.com/articles/2", "http://bing.com/", "yyy", "2017-09-15 00:20:00", "a.com"),
        ("http://a.com/articles/2", "http://facebook.com/", "yyy", "2017-09-15 00:30:00", "a.com"),
        ("http://a.com/articles/1", "http://google.com/", "xxx", "2017-09-15 01:10:00", "a.com"),
        ("http://a.com/articles/2", "http://bing.com/", "yyy", "2017-09-15 01:20:00", "a.com")
    ],
    columns=['url', 'referrer', 'session_id', 'ts', 'customer'])

    pdf['ts'] = pd.to_datetime(pdf['ts'])

    df = dd.from_pandas(pdf, 2)

    # round datetimes down to an hour
    df['ts'] = df['ts'].dt.floor('1H')

    # group on customer, timestamp (rounded) and url
    gb = df.groupby(['customer', 'url', 'ts'])

    df = gb.apply(lambda d: pd.DataFrame({
            'views': len(d),
            'visitors': d.session_id.count(),
            'referrers': [d.referrer.tolist()]}),
        meta={'views': int, 'visitors': int, 'referrers': int})

    # I want index to be a part of dataframe, so it is passed
    # to the next .apply call.
    df['index'] = df.index

    df = df.apply(transform_one, axis=1, meta={'data': str}).to_bag()

    pprint(df.compute())


from flask import Flask, render_template, request
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.font_manager import FontProperties

# 绘制饼图时使用中文字体

app = Flask(__name__)

# 指定中文字体
font = FontProperties(fname=r"/libraries/font-apex/2.0/fonts/Font-APEX-Large.ttf")

@app.route('/', methods=['GET', 'POST'])
def calculate():
    if request.method == 'POST':
        input_value = float(request.form['input_name'])  # 获取输入值
        # 计算比例
        life_expenses = input_value * 0.55
        savings_investment = input_value * 0.35
        risk_management = input_value * 0.1

        # 绘制饼图
        mpl.rcParams['font.family'] = ['Arial Unicode MS']
        labels = ['生活開支', '儲蓄與投資', '風險管理']
        sizes = [life_expenses, savings_investment, risk_management]
        colors = ['red', 'yellow', 'orange']
        plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%')
        # plt.title('pie')

        # 将图表保存为图片文件
        chart_path = './static/chart.png'  # 图表保存路径，相对于当前工作目录
        plt.savefig(chart_path, format='png')
        plt.close()

        return render_template('index.html', result=[life_expenses, savings_investment, risk_management], chart_path=chart_path)

    return render_template('index.html', result=None, chart_path=None)

if __name__ == '__main__':
    app.run()


from rest_framework import status
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet

from api.models import Stock, UserStock
from api.permissions import IsInvestor
from api.serializers import StockTransactionSerializer

class BuyStockTransactionViewSet(ViewSet):
    serializer_class = StockTransactionSerializer
    permission_classes = [IsInvestor]

    def create(self, request):
        stock_id = request.data.get('stockId')
        quantity = int(request.data.get('quantity'))

        try:
            stock = Stock.objects.get(pk=stock_id)
        except Stock.DoesNotExist:
            return Response({'error': 'Stock not found'}, status=status.HTTP_404_NOT_FOUND)

        if stock.quantity < quantity:
            return Response({'error': 'There is not enough stock to purchase this quantity.'},
                            status=status.HTTP_400_BAD_REQUEST)

        user_stock, created = UserStock.objects.get_or_create(user=request.user, stock=stock)
        user_stock.quantity += quantity
        user_stock.save()

        stock.quantity -= quantity
        stock.save()

        data = {
            'stockId': stock_id,
            'quantity': quantity,
            'totalCost': stock.price * quantity
        }

        return Response(data, status=status.HTTP_201_CREATED)

class SellStockTransactionViewSet(ViewSet):
    serializer_class = StockTransactionSerializer
    permission_classes = [IsInvestor]

    def create(self, request):
        stock_id = request.data.get('stockId')
        quantity = int(request.data.get('quantity'))

        try:
            stock = Stock.objects.get(pk=stock_id)
        except Stock.DoesNotExist:
            return Response({'error': 'Stock not found'}, status=status.HTTP_404_NOT_FOUND)

        try:
            user_stock = UserStock.objects.get(user=request.user, stock=stock)
        except UserStock.DoesNotExist:
            return Response({'error': 'User does not hold this stock'}, status=status.HTTP_404_NOT_FOUND)

        if quantity > user_stock.quantity:
            return Response({'error': 'Cannot sell more stock than the user holds'}, status=status.HTTP_400_BAD_REQUEST)

        user_stock.quantity -= quantity
        user_stock.save()

        stock.quantity += quantity
        stock.save()

        data = {
            'stockId': stock_id,
            'quantity': quantity,
            'totalProfit': stock.price * quantity
        }

        return Response(data, status=status.HTTP_201_CREATED)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('clients', '0020_remove_statusi_status_discount'),
    ]

    operations = [
        migrations.RenameField(
            model_name='statusi',
            old_name='discount',
            new_name='status_discount',
        ),
    ]


import os
import numpy as np
import cv2

from preprocess import prepareTrain, prepareTest

from config import conf

def training_generator(data, angle_bias=1.0, batch_size=128):
    batch_X = np.zeros((batch_size, conf.row, conf.col, 3))
    batch_y = np.zeros(batch_size)
    data_folder = conf.data_folder
    print (angle_bias, len(data))
    cnt = 0
    while 1:
        while (cnt < batch_size):
            # Randomly select an image.
            index = np.random.randint(len(data))
            camera = ['center', 'left', 'right']
            ci = np.random.randint(3)

            file = os.path.join(data_folder, data[camera[ci]].iloc[index].strip())

            #shift angle for center, left, and right camera
            shift = [0.0, conf.left_offset, conf.right_offset]

            image = cv2.imread(file)

            y = data['steering'].iloc[index] + shift[ci]

            # Apply random brightness,
            X, y = prepareTrain(image, y)

            # A higher bias value will exclude smaller (< 0.1 absolute value) angles.
            threshold = np.random.uniform()
            if abs(y) < 0.1:
                if angle_bias > threshold:
                    continue

            batch_X[cnt] = X
            batch_y[cnt] = y

            cnt += 1

        yield (batch_X, batch_y)

def validation_generator(data, batch_size=128):
    batch_X = np.zeros((batch_size, conf.row, conf.col, 3))
    batch_y = np.zeros(batch_size)
    data_folder = conf.data_folder
    cnt = 0
    while 1:
        while (cnt < batch_size):
            # Randomly select an image.
            index = np.random.randint(len(data))

            file = os.path.join(data_folder, data['center'].iloc[index].strip())

            image = cv2.imread(file)

            y = data['steering'].iloc[index]

            X = prepareTest(image)

            batch_X[cnt] = X
            batch_y[cnt] = y

            cnt += 1

        yield (batch_X, batch_y)


import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import torch
from torch.utils.data import Dataset

class YahooDataset(Dataset):
    def prepare_dataset(self, dataset_path):
        dataset = pd.read_csv(dataset_path)
        dataset = dataset[['Close']]
        class_idx = 0
        return dataset, class_idx

    def split_dataset(self, dataset, train, train_rate):
        n = len(dataset)
        if train:
            return dataset[:int(n * train_rate)]
        else:
            return dataset[int(n * train_rate):]

    def __init__(self, dataset_path, window_len, forecast_len=1, train=True, train_rate=0.7, scalertype="none", scaler=None):
        super().__init__()

        if train == True and (scalertype != "none" and scalertype != "minmax"):
            raise Exception("scaler not recognized, use 'none' or 'minmax'")

        dataset, class_idx = self.prepare_dataset(dataset_path)
        self.class_idx = class_idx
        self.forecast_len = forecast_len
        self.dataset = self.split_dataset(dataset, train, train_rate).to_numpy()
        self.window_len = window_len
        self.scaler = scaler
        if train == True and scalertype == "minmax":
            self.scaler = MinMaxScaler()
            self.scaler.fit(self.dataset)

    def __len__(self):
        return len(self.dataset) - self.window_len - 2 * self.forecast_len + 1

    def get_scaler(self):
        return self.scaler

    def __getitem__(self, index):
        input = self.dataset[index:index + self.window_len + 2 * self.forecast_len - 1, :]
        if self.scaler is not None:
            input = self.scaler.transform(input)
        return torch.from_numpy(input).to(torch.float32), self.window_len, self.class_idx


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

import re
#import opencc
import os
import time
import csv
import json
from gensim import corpora
from utils import load_scws, cut
result_path = './result/'

AB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'classify_dict')

cut_str = load_scws()

dictionary_3 = corpora.Dictionary.load(os.path.join(AB_PATH, 'new_classify.dict'))
step3_score = {}
with open(os.path.join(AB_PATH, 'new_classify.txt')) as f:
    for l in f:
        lis = l.rstrip().split()
        step3_score[int(lis[0])] = [float(lis[1]), float(lis[2]), float(lis[3]), float(lis[4]), float(lis[5])]

def triple_classifier(text):

    entries = cut(cut_str, text)
    entry = [e.decode('utf-8', 'ignore') for e in entries]
    keywords_list = entry

    bow = dictionary_3.doc2bow(entry)
    s = [1, 1, 1, 1, 1]
    for pair in bow:
        s[0] = s[0] * (step3_score[pair[0]][0] ** pair[1])
        s[1] = s[1] * (step3_score[pair[0]][1] ** pair[1])
        s[2] = s[2] * (step3_score[pair[0]][2] ** pair[1])
        s[3] = s[3] * (step3_score[pair[0]][3] ** pair[1])
        s[4] = s[4] * (step3_score[pair[0]][4] ** pair[1])

    s_max = max(s)
    s_dis = abs(s[0]-s_max) + abs(s[1]-s_max) + abs(s[2]-s_max) + abs(s[3]-s_max) + abs(s[4]-s_max)
    if (s_dis/5)/s_max <= 0.7:
        label = 'other'
    else:

        if s[0] > s[1] and s[0] > s[2] and s[0] > s[3] and s[0] > s[4]:#政治领域
            label = 'politics'
        elif s[1] > s[0] and s[1] > s[2] and s[1] > s[3] and s[1] > s[4]:#外交领域
            label = 'diplomacy'
        elif s[2] > s[0] and s[2] > s[1] and s[2] > s[3] and s[2] > s[4]:#经济领域
            label = 'economic'
        elif s[3] > s[0] and s[3] > s[1] and s[3] > s[2] and s[3] > s[4]:#暴恐领域
            label = 'violence'
        elif s[4] > s[0] and s[4] > s[1] and s[4] > s[3] and s[4] > s[2]:#政变领域
            label = 'coup'
        else:
            label = 'other'

    return label

def get_file_data(_id):

    f = open(result_path + 'recommend_news_%s.jl' % _id)
    news_data = []
    for line in f:
        news_dict = json.loads(line)
        news_data.append(news_dict)

    return news_data

def write_file_data(_id,result):

    f = open(result_path + 'classify_result_%s.jl' % _id,'a')

    for item in result:
        row = json.dumps(item)
        f.write(row+'\n')

    f.close()

if __name__ == '__main__':

    news_data = get_file_data('1487665506')
    result = []
    for news in news_data:
        if news.has_key('title') and news.has_key('summary'):
            text = news['title'].encode('utf-8','ignore') + news['summary'].encode('utf-8','ignore')
        elif news.has_key('title') and not news.has_key('summary'):
            text = news['title'].encode('utf-8','ignore')
        elif not news.has_key('title') and news.has_key('summary'):
            text = news['summary'].encode('utf-8','ignore')
        else:
            text = ''

        if len(text):#非空字符
            label = triple_classifier(text)
        else:
            label = 'other'
        news['label'] = label
        result.append(news)

    write_file_data('1487665506',result)


import pandas
import matplotlib.pyplot as plt
import os

year = input("insert year you would like to analyse: ")
month = input("insert month you would like to analyse: ")

if month == "1":
    month_str="January"
elif month =="2":
    month_str="February"
elif month =="3":
    month_str="March"
elif month =="4":
    month_str="April"
elif month =="5":
    month_str="May"
elif month =="6":
    month_str="June"
elif month =="7":
    month_str="July"
elif month =="8":
    month_str="August"
elif month =="9":
    month_str="September"
elif month =="10":
    month_str="October"
elif month =="11":
    month_str="November"
else:
    month_str="December"

def to_usd(my_price):
    return "${0:,.2f}".format(my_price)

#NEED TO INCLUDE HERE VALIDATION REQUIERMENT

df = pandas.read_csv(f'data/sales-{year}{month}.csv')

#print(df.head(5))

total_sales = df['sales price'].sum()

df_agg = df.groupby('product')  \
           .agg({'unit price': 'mean',
                 'units sold': 'sum',
                 'sales price': 'sum'})

df_agg.sort_values('units sold', ascending=False, inplace=True)

print("-----------------------")

print("Total Sales in " + month_str + " " + str(year) + ": " + str(to_usd(total_sales)))
print("-----------------------")
print("Top Selling Products:")

top_sellers = []
rank = 1
for i, row in df_agg.iterrows():
    d = {"rank": rank, "name": row.name, "monthly_sales": row["sales price"]}
    top_sellers.append(d)
    rank = rank + 1

for d in top_sellers:
    print("  " + str(d["rank"]) + ") " + d["name"] +
          ": " + str(to_usd(d["monthly_sales"])))

print("-----------------------") #print("VISUALIZING THE DATA...")

chart_title = "Top Selling Products " + month_str + " " + str(year)

sorted_products = []
sorted_sales = []

for d in top_sellers:
    sorted_products.append(d["name"])
    sorted_sales.append(d["monthly_sales"])

plt.bar(sorted_products, sorted_sales)
plt.title(chart_title)
plt.xlabel("Product")
plt.ylabel("Monthly Sales (USD)")
plt.show()


import sys
import socket
import logging
import ssl
import os
from bs4 import BeautifulSoup

import gzip

BUFFER_SIZE = 1024 * 10
SERVER_HOST = "localhost"

def create_socket(destination_address=SERVER_HOST, port=12000):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_address = (destination_address, port)
        logging.warning(f"connecting to {server_address}")
        sock.connect(server_address)
        return sock
    except Exception as ee:
        logging.warning(f"error {str(ee)}")

def create_secure_socket(destination_address=SERVER_HOST, port=10000):
    try:
        # get it from https://curl.se/docs/caextract.html
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_address = (destination_address, port)
        logging.warning(f"connecting to {server_address}")
        sock.connect(server_address)
        secure_socket = ssl.wrap_socket(sock, keyfile=None, certfile=None, server_side=False,
                                        cert_reqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_SSLv23)
        logging.warning(secure_socket.getpeercert())
        return secure_socket
    except Exception as ee:
        logging.warning(f"error {str(ee)}")

def send_command(server, command_str, is_secure=False):
    request_path = command_str.split("\r\n")[0].split("GET")[
        1].split("HTTP")[0].strip()
    print("request_path", request_path)

    headers = ""
    content_encoded = b""
    content = ""
    content_encoding = "utf-8"
    content_length = -1
    content_type = "text/html"

    bytes_received = 0

    server_address = server[0]
    server_port = server[1]
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    if is_secure == True:
        sock = create_secure_socket(server_address, server_port)
    else:
        sock = create_socket(server_address, server_port)

    try:
        logging.warning(f"[+] Sending data ")
        sock.sendall(command_str.encode())
        while True:
            # socket does not receive all data at once, data comes in part, need to be concatenated at the end of process
            data = sock.recv(BUFFER_SIZE)
            if data:
                if "html" not in content_type:
                    bytes_received += len(data)
                    print(
                        f"[!] Bytes Received {bytes_received}/{content_length}")

                    save_file_name = request_path.split("/")[-1]
                    file = open(save_file_name, "ab")
                    file.write(data)
                    file.close()

                    if bytes_received == content_length:
                        content = f""
                        break
                    continue

                decoded_data = data.decode().split("\r\n\r\n")
                headers += decoded_data[0]
                content += decoded_data[1]

                # read content-encoding and content-length
                for head in headers.split("\r\n"):
                    if "Content-Encoding:" in head:
                        content_encoding = head.split(
                            "Content-Encoding: ")[1]
                    if "Content-Length:" in head:
                        content_length = int(
                            head.split("Content-Length: ")[1])
                    if "Content-Type:" in head:
                        content_type = head.split(
                            "Content-Type: ")[1].strip()

                logging.warning(f"Content-Encoding: {content_encoding}")
                logging.warning(f"Content-Length: {content_length}")
                logging.warning(f"Content-Type: {content_type}")

                if "html" not in content_type:
                    try:
                        save_file_name = request_path.split("/")[-1]
                        os.remove(save_file_name)
                    except:
                        None

                if len(content) == content_length:
                    if content_encoding == "gzip":
                        content = gzip.decompress(content_encoded).decode()
                    elif content_encoding == "utf-8":
                        None
                    else:
                        content = "[Downloadable Content!]"
                    break
            else:
                print("[!] No More Data Received")
                break

        logging.warning("[+] All data has been received")
        return headers, content
    except Exception as ee:
        logging.warning(f"[!] Error during data receiving {str(ee)}")
        return False

def create_request_headers(req_str):
    data = ""
    for head in req_str.split("\n"):
        if head != "":
            data += f"{head}\r\n"

    data += "\r\n"
    return data

#
# START
#

try:
    while True:
        print(f"[!] Where to?: http://{SERVER_HOST}/", end="")
        dest = input()
        request_headers = f"""
GET /{dest} HTTP/1.1
Host: {SERVER_HOST}
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36
"""
        headers, content = send_command(
            (SERVER_HOST, 8000), create_request_headers(request_headers), False)
        print("[!] HEADERS:")
        print(headers)
        soup = BeautifulSoup(content, "html.parser")
        html_text = soup.get_text()
        print(html_text)

except KeyboardInterrupt:
    sys.exit(0)


import pytest
from sanic import Sanic

from .utils import get_spec

@pytest.fixture(autouse=True)
def handlers(app: Sanic):
    @app.route("/test1")
    async def handler1(_):
        ...

    @app.route("/test2/1")
    async def handler2(_):
        ...

    @app.route("/test3/")
    async def handler3(_):
        ...

    @app.route("/test4/2/")
    async def handler4(_):
        ...

def test_paths_with_all_uri_filter(app: Sanic):
    app.config.API_URI_FILTER = "all"

    spec = get_spec(app)
    assert list(spec["paths"].keys()) == [
        "/test1",
        "/test1/",
        "/test2/1",
        "/test2/1/",
        "/test3",
        "/test3/",
        "/test4/2",
        "/test4/2/",
    ]

def test_paths_with_slash_uri_filter(app: Sanic):
    app.config.API_URI_FILTER = "slash"
    spec = get_spec(app)

    assert list(spec["paths"].keys()) == [
        "/test1/",
        "/test2/1/",
        "/test3/",
        "/test4/2/",
    ]

def test_paths_without_uri_filter(app: Sanic):
    spec = get_spec(app)
    assert list(spec["paths"].keys()) == [
        "/test1",
        "/test2/1",
        "/test3",
        "/test4/2",
    ]


'`Loading bar creator module.'
from hurry.filesize import alternative, size
from colorama import init, Fore

class LoadBar:
    '`Loading bar generator class.'
    total:float = 0
    init(autoreset=True)

    def update(sf, remaining: int) -> None:
        '`Updates the bar.'
        finished:float = sf.total - remaining
        percent:float = round(100 * finished / sf.total, 1)
        filledLength:int = int(50 * finished // sf.total)

        filled:str = Fore.GREEN + '█' * filledLength + Fore.RESET
        finished_label:str = size(finished, system=alternative)
        sf.filesize:str = size(sf.total, system=alternative)

        print(" " * 100,end="\r")
        print(f'\r{finished_label} / {sf.filesize} |{filled.ljust(60,"-")}| {percent} %', end="\r")


import torch
from torch.utils.data import Dataset
from torch.nn.utils.rnn import pad_sequence
import pickle, pandas as pd

class IEMOCAPDataset(Dataset):

    def __init__(self, train=True):
        self.videoIDs, self.videoSpeakers, self.videoLabels, self.videoText,\
        self.videoAudio, self.videoVisual, self.videoSentence, self.trainVid,\
        self.testVid = pickle.load(open('./IEMOCAP_features/IEMOCAP_features.pkl', 'rb'), encoding='latin1')
        '''
        label index mapping = {'hap':0, 'sad':1, 'neu':2, 'ang':3, 'exc':4, 'fru':5}
        '''
        self.keys = [x for x in (self.trainVid if train else self.testVid)]

        self.len = len(self.keys)

    def __getitem__(self, index): # 0: utterance textual feature, 1: sentence length, 2: label, 3: speaker information 4: m_list 5: f_list 6: vid
        vid = self.keys[index]
        m, f = self.getSpeakerList(vid)
        return torch.FloatTensor(self.videoText[vid]),\
               torch.FloatTensor([1]*len(self.videoLabels[vid])),\
               torch.LongTensor(self.videoLabels[vid]),\
               torch.LongTensor([0 if x == 'M' else 1 for x in self.videoSpeakers[vid]]),\
               torch.FloatTensor([[1,0] if x == 'M' else [0,1] for x in self.videoSpeakers[vid]]),\
               vid

    def getSpeakerList(self, vid):
        speaker_list = torch.FloatTensor([[1,0] if x=='M' else [0,1] for x in self.videoSpeakers[vid]])
        male_list =[]
        female_list = []

        idx = 0
        for speaker in speaker_list:
            s = torch.argmax(speaker)
            s = s.cpu().detach().numpy()

            if s == 0:
                male_list.append(idx)
            else:
                female_list.append(idx)

            idx+=1
        return male_list, female_list

    def __len__(self):
        return self.len

    def collate_fn(self, data):
        dat = pd.DataFrame(data)

        return [pad_sequence(dat[i]) if i<5 else dat[i].tolist() for i in dat]

if __name__ == '__main__':
    dataset = IEMOCAPDataset()


#!/usr/bin/env python3
#
# Export reports of the folders to spreadsheets
#   mostly to use in Dropbox or similar.
#  Needs to be run with cron to update the files.
#
############################################
# Import modules
############################################
import sys
import locale
import json
import os
import requests
import pandas as pd
from openpyxl import Workbook

# Import settings from settings.py file
import settings

ver = "0.1.0"

# Set locale
locale.setlocale(locale.LC_ALL, 'en_US.utf8')

if os.path.exists(settings.export_to) is False:
    print(" Error, export location ({}) was not found.".format(settings.export_to))
    sys.exit(1)

############################################
def main():
    payload_api = {'api_key': settings.api_key}
    r = requests.post('{}/api/projects/{}'.format(settings.api_url, settings.project_alias), data=payload_api)
    if r.status_code != 200:
        # Something went wrong
        query_results = r.text.encode('utf-8')
        print("API Returned Error: {}".format(query_results))
        sys.exit(1)

    project_info = json.loads(r.text.encode('utf-8'))
    project_checks = project_info['project_checks']
    project_checks = project_checks.split(',')
    filename = "{}/project_status.xlsx".format(settings.export_to)
    workbook = Workbook()
    sheet = workbook.active

    i = 1
    for attrib, value in project_info.items():
        # print("{}: {}".format(attrib, value))
        if attrib == "folders" or attrib == "reports":
            continue
        elif attrib == "project_stats":
            sheet["A{}".format(i)] = "images_taken"
            sheet["B{}".format(i)] = value['images_taken']
            i += 1
            sheet["A{}".format(i)] = "objects_digitized"
            sheet["B{}".format(i)] = value['objects_digitized']
            i += 1
            continue
        if value == "":
            continue
        sheet["A{}".format(i)] = attrib
        sheet["B{}".format(i)] = value
        i += 1

    workbook.save(filename=filename)

    # Folders info
    filename = "{}/project_folders.xlsx".format(settings.export_to)
    workbook = Workbook()
    sheet = workbook.active

    i = 1
    for attrib, value in project_info.items():
        if attrib != "folders":
            continue
        else:
            folder_info = value
        for values in folder_info:
            for att, val in values.items():
                if att == "folder":
                    sheet["A{}".format(i)] = att
                    sheet["B{}".format(i)] = val
                    i += 1
            for att, val in values.items():
                if att == "folder" or att == "delivered_to_dams" or att == "folder_path" or att == "error_info" or att == "file_errors" or att == "no_files" or att == "notes" or att == "project_id":
                    continue
                else:
                    sheet["B{}".format(i)] = att
                    sheet["C{}".format(i)] = val
                    i += 1

    workbook.save(filename=filename)

    folders_storage = "{}/folders".format(settings.export_to)
    if not os.path.exists(folders_storage):
        os.makedirs(folders_storage)

    folders_qc_storage = "{}/folders_qc".format(settings.export_to)
    if not os.path.exists(folders_qc_storage):
        os.makedirs(folders_qc_storage)

    for folder in project_info['folders']:
        folder_id = folder['folder_id']
        r = requests.post('{}/api/folders/{}'.format(settings.api_url, folder_id), data=payload_api)
        if r.status_code != 200:
            # Something went wrong
            query_results = r.text.encode('utf-8')
            print("API Returned Error: {}".format(query_results))
            sys.exit(1)

        folder_info = json.loads(r.text.encode('utf-8'))
        file_df = pd.DataFrame(folder_info['files'])
        # cols = ['file_id', 'file_name', 'file_timestamp', 'preview_image', 'updated_at']
        cols = ['file_id', 'file_name', 'file_timestamp', 'updated_at', 'tif_md5']
        for pcheck in project_checks:
            cols.append(pcheck)
        filename = "{}/{}.xlsx".format(folders_storage, folder_info['folder'])
        file_df.to_excel(filename, columns=cols, header=True, index=False)
        # Save QC, if any
        r = requests.post('{}/api/folders/qc/{}'.format(settings.api_url, folder_id), data=payload_api)
        if r.status_code != 200:
            # Something went wrong
            query_results = r.text.encode('utf-8')
            print("API Returned Error: {}".format(query_results))
            sys.exit(1)

        qc_info = json.loads(r.text.encode('utf-8'))
        file_df = pd.DataFrame(qc_info['qc'])
        # cols = ['file_name', 'file_qc', 'file_timestamp', 'full_name', 'qc_info', 'updated_at']
        filename = "{}/{}.xlsx".format(folders_qc_storage, folder_info['folder'])
        file_df.to_excel(filename, header=True, index=False)

############################################
# Main loop
############################################
if __name__=="__main__":
    main()

sys.exit(0)


import requests
from pyquery import PyQuery as pq#基于CSS选择器的解析库
from selenium import webdriver#自动化爬虫，与chromedriver搭配使用
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import openpyxl#向excel导入数据
import pandas as pd
import time
import json
import os
import gc

code_url = 'http://quotes.money.163.com/old/#query=hy003026&DataType=HS_RANK&sort=PERCENT&order=desc&count=24&page=0'
time_column = ['2019-09-30', '2019-06-30', '2019-03-31', '2018-12-31', '2018-09-30', '2018-06-30']
sheet_column = ['名称', '2019-09-30', '2019-06-30', '2019-03-31', '2018-12-31', '2018-09-30', '2018-06-30']
sheet_name = ['总资产利润率(%)', '销售净利率(%)', '流动比率(%)', '资产负债率(%)', '总资产周转率(次)', '流动资产周转率(次)', '速动比率(%)']

def get_code(url):
    code_list = []
    chromedriver = "/usr/local/bin/chromedriver"#chromedriver存放位置
    browser = webdriver.Chrome(chromedriver)
    wait = WebDriverWait(browser, 10)#最多延迟10秒
    browser.get(url)
    # browser.add_cookie(cookie)
    for i in range(1, 6):#爬取1-5页
        print(f'Crawling Page {i}')
        try:
            if i == 1:#按股票代码排序，防止因为实时数据导致爬取相同数据
                code_sort = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'table.table-header > thead > tr > th:nth-child(2)')))
                code_sort.click()
            time.sleep(0.5)
            wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'table.ID_table.stocks-info-table > tbody > tr')))
            html = browser.page_source
            doc = pq(html)
            table = doc('table.ID_table.stocks-info-table > tbody > tr').items()
            for tr in table:
                code_list.append({
                    'code': tr.find('td:nth-child(2) > a').text(),
                    'name': tr.find('td:nth-child(3) a.symbol').text()
                })
            # print(code_list)
            page_flip = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.ID_pages.mod-pages > a:last-child')))#点击最后一页
            page_flip.click()
        except:
            print(f'Page {i} does not exist.')
            break
    # save to json
    with open('code_list.json', 'w', encoding='utf-8') as f:
        f.write(json.dumps(code_list))
    return code_list

def parse_page(code, name):
    url = f'http://quotes.money.163.com/f10/zycwzb_{code}.html#01c01'
    response = requests.get(url)
    if response.status_code == 200:#200表示请求成功
        doc = pq(response.text)
        indicators = {
            '代码': code,#函数参数
            '名称': name,#函数参数
            '总资产利润率(%)': doc('td:contains("总资产利润率(%)")').siblings().text().split(),
            '销售净利率(%)': doc('td:contains("销售净利率(%)")').siblings().text().split(),
            '流动比率(%)': doc('td:contains("流动比率(%)")').siblings().text().split(),
            '资产负债率(%)': doc('td:contains("资产负债率(%)")').siblings().text().split(),
            '总资产周转率(次)': doc('td:contains("总资产周转率(次)")').siblings().text().split(),
            '流动资产周转率(次)': doc('td:contains("流动资产周转率(次)")').siblings().text().split(),
            '速动比率(%)': doc('td:contains("速动比率(%)")').siblings().text().split(),
        }
        print(f'success: {code}')
        print(indicators)
        return indicators
    else:
        print(f'error: {code}')
        return 0

def get_indicator(code_list):
    res = []
    for i in code_list:
        indicators = parse_page(i['code'], i['name'])#爬取单页
        if indicators:
            res.append(indicators)
    with open('indicators.json', 'w', encoding='utf-8') as f:
        f.write(json.dumps(res))

def ensure_data():
    if not os.path.exists('code_list.json'):#创建出股票代码和股票名称的.json
        get_code(code_url)
    if not os.path.exists('indicators.json'):#创建出该股票中所要的信息.json
        with open('code_list.json', 'r', encoding='utf-8') as f:
            code_list = json.load(f)#将json格式字符串转化为dict
        get_indicator(code_list)

def to_excel(data):
    file_name = 'indicators.xlsx'
    print('Writing to excel...')
    # create new sheets
    wb = openpyxl.Workbook(file_name)
    for s in sheet_name:
        ws = wb.create_sheet(s)
    wb.save(file_name)
    # preserve created sheets
    book = openpyxl.load_workbook(file_name)#读取
    writer = pd.ExcelWriter(file_name, engine='openpyxl')
    writer.book = book
    writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
    # restructure and write data
    for sheet in sheet_name:
        print(f'Processing: {sheet}')
        result = {}
        for val in data:
            code = val["代码"]
            result[code] = {}#创建空字典
            result[code]["名称"] = val["名称"]
            for i in range(len(time_column)):
                try:
                    result[code][time_column[i]] = float(val[sheet][i])
                except:
                    result[code][time_column[i]] = None

        df = pd.DataFrame(result, index=pd.Series(sheet_column)).T
        df.to_excel(writer, sheet_name=sheet)
        writer.save()
    # release memory
    print('Finished.')
    del wb, ws
    gc.collect()

def main():
    ensure_data()
    with open('indicators.json', 'r', encoding='utf-8') as f:#不必调用f.close()方法
        data = json.load(f)#将json格式字符串转化为dict
        to_excel(data)#写入excel

if __name__ == '__main__':
    main()
    #parse_page(688418, '震有科技')
    # with open('code_list.json', 'r', encoding='utf-8') as f:
    #     code_list = json.load(f)
    # get_indicator(code_list)


import json
import os

class Paths:
    paths = dict()

    @classmethod
    def initialize(cls, filename):
        """
        Initialize the class data with the file

        Args:
            filename (str): path to the file data
        """

        cls.paths.clear()
        with open(filename, encoding="utf-8") as f:
            cls.paths.update(json.load(f))

    @classmethod
    def folder(cls, key):
        """
        Return the absolute path of the folder specified by its ``key``.
        The ``key`` should be in the path.json file.

        Args:
            key (str): key associated to a folder

        Returns:
            str: absolute path of a folder
        """

        p = cls.paths["folders"][key]
        return os.path.abspath(os.path.join(*p))

    @classmethod
    def file(cls, key):
        """
        Return the absolute path of the file specified by its ``key``.
        The ``key`` should be in the path.json file.

        Args:
            key (str): key associated to a file

        Returns:
            str: path to the file
        """

        folder, *p = cls.paths["files"][key]
        folder = cls.folder(folder)
        return os.path.abspath(os.path.join(folder, *p))

    @classmethod
    def image(cls, key):
        """
        Return the absolute path of the image specified by its ``key``.
        The ``key`` shoulf be in the path.json file.

        Args:
            key (str): key associated to the image

        Returns:
            str: path to the image
        """

        p = cls.paths["images"][key]
        folder = cls.folder("r_images")
        return os.path.abspath(os.path.join(folder, p))


from pyrogram import Client, filters
import asyncio
import time
from pyrogram.types import ChatPermissions, Message
from config import (bot, HNDLR, SUDO_USERS, LOGS_CHANNEL )
from pyrogram import Client, filters

@Client.on_message(filters.user(SUDO_USERS) & filters.command(["purge"], prefixes=HNDLR))
@Client.on_message(filters.me & filters.command(["purge"], prefixes=HNDLR))
async def purge(client: Client, message: Message):
    start_time = time.time()
    message_ids = []
    purge_len = 0
    event = await message.edit_text("`Starting To Purge Messages!`")
    me_m =await client.get_me()
    if message.chat.type in ["supergroup", "channel"]:
        me_ = await message.chat.get_member(int(me_m.id))
        if not me_.can_delete_messages:
            await event.edit("`I Need Delete Permission To Do This!`")
            return
    if not message.reply_to_message:
        await event.edit("`Reply To Message To Purge!`")
        return
    async for msg in client.iter_history(
        chat_id=message.chat.id,
        offset_id=message.reply_to_message.message_id,
        reverse=True,
    ):
        if msg.message_id != message.message_id:
            purge_len += 1
            message_ids.append(msg.message_id)
            if len(message_ids) >= 100:
                await client.delete_messages(
                    chat_id=message.chat.id, message_ids=message_ids, revoke=True
                )
                message_ids.clear()
    if message_ids:
        await client.delete_messages(
            chat_id=message.chat.id, message_ids=message_ids, revoke=True
        )
    end_time = time.time()
    u_time = round(end_time - start_time)
    await event.edit(
        f"**>> Fast Purge Done!** \n**>> Total Message Purged :** `{purge_len}` \n**>> Time Taken :** `{u_time}`",
    )
    await asyncio.sleep(3)
    await event.delete()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from os import devnull, system, sync
import timeit
from crc32c import crc32c # crc32c for intel
from zlib import crc32  # standard crc32
import xxhash
import re  # regex
import itertools
from time import sleep, mktime  # For timeouts and time output
from datetime import datetime
from pathlib import Path

# ##################################################################################################
# ##############################################################################
#    Setting functions:
# ##############################################################################
# ##################################################################################################

def search_files(where):
    """ DEF:
        [0] full source path
        [1] file name
        [2] basename
        [3] extension
        [4] size
        [5] mod-date
        [6] hash
        [7] full target path(s) (list)
    """
    found_files = []
    recurse = '**/*'

    for i in where:
        for j in Path(i).glob(recurse):
            if j.is_file():
                j = Path(j).resolve()
                j_stat = j.stat()
                found_files += [[str(j),
                                j.name,
                                j.stem,
                                j.suffix,
                                j_stat.st_size,
                                j_stat.st_mtime,
                                "XYZ",
                                []]]
    found_files = sorted(found_files, key=lambda attris: attris[0])  # For %c in param['naming_file'].
    # print(len(found_files))
    return found_files

def get_source_hashes_CRC32C(what):
    blocksize = 128*256
    for i in what:
        with Path(i[0]).open("rb") as file:
            crcvalue = 0
            while True:
                buf = file.read(blocksize)
                if not buf:
                    break
                crcvalue = (crc32c(buf, crcvalue) & 0xffffffff)
            hashstring = f'{crcvalue:x}'
        i[6] = hashstring
    # return what

def get_source_hashes_CRC32(what):
    blocksize = 128*256
    for i in what:
        with Path(i[0]).open("rb") as file:
            crcvalue = 0
            while True:
                buf = file.read(blocksize)
                if not buf:
                    break
                crcvalue = (crc32(buf, crcvalue) & 0xffffffff)
            hashstring = f'{crcvalue:x}'
        i[6] = hashstring
    # return what

def get_source_hashes_xxhash_256(what):
    blocksize = 128*256
    xxh = xxhash.xxh3_64()
    for i in what:
        xxh.reset()
        with Path(i[0]).open("rb") as file:
            while True:
                buf = file.read(blocksize)
                if not buf:
                    break
                xxh.update(buf)
        crcvalue = xxh.hexdigest()
        i[6] = crcvalue
    # return what

def get_source_hashes_xxhash_4096(what):
    blocksize = 1024*4096
    xxh = xxhash.xxh3_64()
    for i in what:
        xxh.reset()
        with Path(i[0]).open("rb") as file:
            while True:
                buf = file.read(blocksize)
                if not buf:
                    break
                xxh.update(buf)
        crcvalue = xxh.hexdigest()
        i[6] = crcvalue
    # return what

# ##################################################################################################
# ##############################################################################
#    Chronology / Workflow:
# ##############################################################################
# ##################################################################################################

# NOTE: It is best to test this on your machine with your most used sources (e.g. SD-cards).
#       Results vary greatly: With RAM, 128x256bit are faster than 1024x4096, but this tends to get
#       reversed more the slower the cards are.
source_files = search_files(["/tmp/usb1/DCIM", "/tmp/usb2/DCIM"])
print("Files:\t" + str(len(source_files)))
# Just to give the computer some rest between iterations:
sleep(15)

time_crc32c = timeit.timeit('get_source_hashes_CRC32C(source_files)',
                            'from __main__ import source_files, get_source_hashes_CRC32C',
                            number=1)
print("CRC32C:\t" + str(time_crc32c))
# Just to give the computer some rest between iterations:
sleep(15)

time_crc32 = timeit.timeit('get_source_hashes_CRC32(source_files)',
                           'from __main__ import source_files, get_source_hashes_CRC32',
                           number=1)
print("CRC32:\t" + str(time_crc32))
# Just to give the computer some rest between iterations:
sleep(15)

time_xxhash256 = timeit.timeit('get_source_hashes_xxhash_256(source_files)',
                               'from __main__ import source_files, get_source_hashes_xxhash_256',
                               number=1)
print("xxHash (256):\t" + str(time_xxhash256))
# Just to give the computer some rest between iterations:
sleep(15)

time_xxhash4096 = timeit.timeit('get_source_hashes_xxhash_4096(source_files)',
                                'from __main__ import source_files, get_source_hashes_xxhash_4096',
                                number=1)
print("xxHash (4096):\t" + str(time_xxhash4096))


import unittest
from typing import List
from unittest.mock import MagicMock, call, patch

from charms.operator_libs_linux.v1 import systemd

def with_mock_subp(func):
    """Decorator that sets up a Popen mock.

    Any function that uses this decorator should take a function as an argument. When
    called wtih a series of return codes, that function will mock out subprocess.Popen,
    and set it to return those error codes, in order.

    The function returns the mock Popen object, so that routines such as
    assert_called_with can be called upon it, along with the return from
    systemd._popen_kwargs, for convenience when composing call objects.

    """

    @patch("charms.operator_libs_linux.v1.systemd.subprocess")
    def make_mocks_and_run(cls, mock_subp):
        def make_mock_popen(returncodes: List[int], lines: List[str] = None, stdout: str = None):
            lines = lines if lines is not None else ("", "")

            mock_subp.PIPE = mock_subp.STDOUT = stdout or ""

            side_effects = []

            for code in returncodes:
                mock_proc = MagicMock()
                mock_proc.wait.return_value = None
                mock_proc.stdout.readline.side_effect = lines
                mock_proc.returncode = code
                side_effects.append(mock_proc)

            mock_popen = mock_subp.Popen
            mock_popen.side_effect = tuple(side_effects)

            return mock_popen, systemd._popen_kwargs()

        func(cls, make_mock_popen)

    return make_mocks_and_run

class TestSystemD(unittest.TestCase):
    @with_mock_subp
    def test_service(self, make_mock):
        mockp, kw = make_mock([0, 1])

        success = systemd._systemctl("is-active", "mysql")
        mockp.assert_called_with(["systemctl", "is-active", "mysql"], **kw)
        self.assertTrue(success)

        self.assertRaises(systemd.SystemdError, systemd._systemctl, "is-active", "mysql")
        mockp.assert_called_with(["systemctl", "is-active", "mysql"], **kw)

    @with_mock_subp
    def test_service_running(self, make_mock):
        mockp, kw = make_mock([0, 3])

        is_running = systemd.service_running("mysql")
        mockp.assert_called_with(["systemctl", "is-active", "mysql", "--quiet"], **kw)
        self.assertTrue(is_running)

        is_running = systemd.service_running("mysql")
        mockp.assert_called_with(["systemctl", "is-active", "mysql", "--quiet"], **kw)
        self.assertFalse(is_running)

    @with_mock_subp
    def test_service_start(self, make_mock):
        mockp, kw = make_mock([0, 1])

        started = systemd.service_start("mysql")
        mockp.assert_called_with(["systemctl", "start", "mysql"], **kw)
        self.assertTrue(started)

        self.assertRaises(systemd.SystemdError, systemd.service_start, "mysql")
        mockp.assert_called_with(["systemctl", "start", "mysql"], **kw)

    @with_mock_subp
    def test_service_stop(self, make_mock):
        mockp, kw = make_mock([0, 1])

        stopped = systemd.service_stop("mysql")
        mockp.assert_called_with(["systemctl", "stop", "mysql"], **kw)
        self.assertTrue(stopped)

        self.assertRaises(systemd.SystemdError, systemd.service_stop, "mysql")
        mockp.assert_called_with(["systemctl", "stop", "mysql"], **kw)

    @with_mock_subp
    def test_service_restart(self, make_mock):
        mockp, kw = make_mock([0, 1])

        restarted = systemd.service_restart("mysql")
        mockp.assert_called_with(["systemctl", "restart", "mysql"], **kw)
        self.assertTrue(restarted)

        self.assertRaises(systemd.SystemdError, systemd.service_restart, "mysql")
        mockp.assert_called_with(["systemctl", "restart", "mysql"], **kw)

    @with_mock_subp
    def test_service_reload(self, make_mock):
        # We reload succesfully.
        mockp, kw = make_mock([0])
        reloaded = systemd.service_reload("mysql")
        mockp.assert_called_with(["systemctl", "reload", "mysql"], **kw)
        self.assertTrue(reloaded)

        # We can't reload, so we restart
        mockp, kw = make_mock([1, 0])
        reloaded = systemd.service_reload("mysql", restart_on_failure=True)
        mockp.assert_has_calls(
            [
                call(["systemctl", "reload", "mysql"], **kw),
                call(["systemctl", "restart", "mysql"], **kw),
            ]
        )
        self.assertTrue(reloaded)

        # We should only restart if requested.
        mockp, kw = make_mock([1, 0])
        self.assertRaises(systemd.SystemdError, systemd.service_reload, "mysql")
        mockp.assert_called_with(["systemctl", "reload", "mysql"], **kw)

        # ... and if we fail at both, we should fail.
        mockp, kw = make_mock([1, 1])
        self.assertRaises(
            systemd.SystemdError, systemd.service_reload, "mysql", restart_on_failure=True
        )
        mockp.assert_has_calls(
            [
                call(["systemctl", "reload", "mysql"], **kw),
                call(["systemctl", "restart", "mysql"], **kw),
            ]
        )

    @with_mock_subp
    def test_service_pause(self, make_mock):
        # Test pause
        mockp, kw = make_mock([0, 0, 3])

        paused = systemd.service_pause("mysql")
        mockp.assert_has_calls(
            [
                call(["systemctl", "disable", "mysql", "--now"], **kw),
                call(["systemctl", "mask", "mysql"], **kw),
                call(["systemctl", "is-active", "mysql", "--quiet"], **kw),
            ]
        )
        self.assertTrue(paused)

        # Could not stop service!
        mockp, kw = make_mock([0, 0, 0])
        self.assertRaises(systemd.SystemdError, systemd.service_pause, "mysql")
        mockp.assert_has_calls(
            [
                call(["systemctl", "disable", "mysql", "--now"], **kw),
                call(["systemctl", "mask", "mysql"], **kw),
                call(["systemctl", "is-active", "mysql", "--quiet"], **kw),
            ]
        )

    @with_mock_subp
    def test_service_resume(self, make_mock):

        # Service is already running
        mockp, kw = make_mock([0, 0, 0])
        resumed = systemd.service_resume("mysql")
        mockp.assert_has_calls(
            [
                call(["systemctl", "unmask", "mysql"], **kw),
                call(["systemctl", "enable", "mysql", "--now"], **kw),
                call(["systemctl", "is-active", "mysql", "--quiet"], **kw),
            ]
        )
        self.assertTrue(resumed)

        # Service was stopped
        mockp, kw = make_mock([0, 0, 0])
        resumed = systemd.service_resume("mysql")
        mockp.assert_has_calls(
            [
                call(["systemctl", "unmask", "mysql"], **kw),
                call(["systemctl", "enable", "mysql", "--now"], **kw),
                call(["systemctl", "is-active", "mysql", "--quiet"], **kw),
            ]
        )
        self.assertTrue(resumed)

        # Could not start service!
        mockp, kw = make_mock([0, 0, 3])
        self.assertRaises(systemd.SystemdError, systemd.service_resume, "mysql")
        mockp.assert_has_calls(
            [
                call(["systemctl", "unmask", "mysql"], **kw),
                call(["systemctl", "enable", "mysql", "--now"], **kw),
                call(["systemctl", "is-active", "mysql", "--quiet"], **kw),
            ]
        )

    @with_mock_subp
    def test_daemon_reload(self, make_mock):
        mockp, kw = make_mock([0, 1])

        reloaded = systemd.daemon_reload()
        mockp.assert_called_with(["systemctl", "daemon-reload"], **kw)
        self.assertTrue(reloaded)

        self.assertRaises(systemd.SystemdError, systemd.daemon_reload)
        mockp.assert_called_with(["systemctl", "daemon-reload"], **kw)


from utils.eval_utils import model_fn, model_fn_eval, eval, eval_with_training_dataset
import utils.train_utils as tu
from utils.dataset import *
from utils.log_utils import create_tb_logger
from utils.utils import *

if __name__ == "__main__":
    #TODO: Simplifiy and automate the process
    #Create directory for storing results
    output_dirs = {}
    output_dirs["boston"] = os.path.join("./", "output_mc", "boston")
    output_dirs["concrete"] = os.path.join("./", "output_mc", "concrete")
    output_dirs["energy"] = os.path.join("./", "output_mc", "energy")
    output_dirs["kin8nm"] = os.path.join("./", "output_mc", "kin8nm")
    output_dirs["naval"] = os.path.join("./", "output_mc", "naval")
    output_dirs["power_plant"] = os.path.join("./", "output_mc", "power_plant")
    output_dirs["protein"] = os.path.join("./", "output_mc", "protein")
    output_dirs["wine"] = os.path.join("./", "output_mc", "wine")
    output_dirs["yacht"] = os.path.join("./", "output_mc", "yacht")
    # output_dirs["year"] = os.path.join("./", "output_mc", "year")

    ckpt_dirs = {}
    for key, val in output_dirs.items():
        os.makedirs(val, exist_ok=True)
        ckpt_dirs[key] = os.path.join(val, 'ckpts')
        os.makedirs(ckpt_dirs[key], exist_ok=True)

    # some cfgs, some cfg will be used in the future
    # TODO::put all kinds of cfgs and hyperparameter into a config file. e.g. yaml
    cfg = {}
    cfg["ckpt"] = None
    cfg["num_epochs"] = 40
    cfg["ckpt_save_interval"] = 20
    cfg["batch_size"] = 100
    cfg["pdrop"] = 0.1
    cfg["grad_norm_clip"] = None
    cfg["num_networks"] = 50

    data_dirs = {}
    for key, val in output_dirs.items():
        data_dirs[key] = os.path.join("./data", key)

    data_files = {}
    for key, _ in data_dirs.items():
        data_files[key] = ["{}_train.csv".format(key), "{}_eval.csv".format(key), "{}_test.csv".format(key)]

    train_datasets = {}
    train_loaders = {}
    eval_datasets = {}
    eval_loaders = {}

    for key, fname in data_files.items():
        train_datasets[key] = UCIDataset(os.path.join(data_dirs[key], fname[0]), testing=True)
        train_loaders[key] = torch.utils.data.DataLoader(train_datasets[key],
                                                         batch_size=cfg["batch_size"],
                                                         num_workers=0,
                                                         collate_fn=train_datasets[key].collate_batch)

    # Prepare model
    print("Prepare model")
    from model.fc import FC, FC2

    models = {}
    for key, dataset in train_datasets.items():
        if key in ["protein", "year"]:
            models[key] = FC2(dataset.input_dim, cfg["pdrop"])
        else:
            models[key] = FC(dataset.input_dim, cfg["pdrop"])
        models[key].cuda()

    # Logging
    tb_loggers = {}
    for key, val in output_dirs.items():
        tb_loggers[key] = create_tb_logger(val)

    #Testing
    print("Start testing")
    # Currently the test dataset is the same as training set. TODO: K-Flod cross validation
    test_datasets = {}
    test_loaders = {}
    for key, fname in data_files.items():
        test_datasets[key] = UCIDataset(os.path.join(data_dirs[key], fname[2]), testing=True)
        test_loaders[key] = torch.utils.data.DataLoader(test_datasets[key],
                                                        batch_size=cfg["batch_size"],
                                                        num_workers=0,
                                                        collate_fn=test_datasets[key].collate_batch)

    cur_ckpts = {}
    for key, ckpt_dir in ckpt_dirs.items():
        cur_ckpts[key] = '{}.pth'.format(os.path.join(ckpt_dir, "ckpt_e{}".format(40)))
        print("loading checkpoint ckpt_e{}".format(40))
        models[key].load_state_dict(torch.load(cur_ckpts[key])["model_state"])
        models[key].train()

    # Summarize the result into table and save it
    results = {}
    for key, model in models.items():
        print("==================================Evaluating {}==========================================".format(key))
        result = eval(model, test_loader=test_loaders[key], cfg=cfg, output_dir=output_dirs[key], tb_logger=tb_loggers[key], title='test-'+key)
        results[key] = result

        #TODO below function is to generate figures for training dataset as requested by Joachim
        eval_with_training_dataset(model, train_loaders[key], cfg=cfg, output_dir=output_dirs[key], tb_logger=tb_loggers[key], title='train-'+key)
        print("Finished\n")

    dataset_list = []
    NLL_list = []
    NLL_without_v_Noise_list = []
    RMSE_list = []
    NLL_over_cap_cnt = []
    cap = 0

    for key, val in results.items():
        dataset_list.append(key)
        NLL_list.append(val[0][0])
        RMSE_list.append(val[0][1])
        NLL_over_cap_cnt.append(val[2][0])
        cap = val[2][1]
        NLL_without_v_Noise_list.append(val[3][0])

    err_df = pd.DataFrame(index=range(len(dataset_list)), columns=["Datasets", "RMSE", "NLL", "NLL_no_v_noise"])
    err_df["Datasets"] = pd.DataFrame(dataset_list)
    err_df["RMSE"] = pd.DataFrame(RMSE_list)
    err_df["NLL"] = pd.DataFrame(NLL_list)
    err_df["NLL_no_v_noise"] = pd.DataFrame(NLL_without_v_Noise_list)

    err_sum_dir = "./output_mc/err_summary"
    os.makedirs(err_sum_dir, exist_ok=True)
    err_df.to_csv(os.path.join(err_sum_dir, "err_summary.csv"))

    plot_NLL_cap_cnt(dataset_list, NLL_over_cap_cnt, cap, err_sum_dir)

    #Finalizing
    print("Analysis finished\n")
    #TODO: integrate logging, visualiztion, GPU data parallel etc in the future


import jittor as jt
import jittor.nn as nn

from jdet.utils.registry import HEADS, LOSSES, build_from_cfg
from jdet.utils.general import multi_apply
from jdet.ops.bbox_transfomrs import bbox2delta, mask2poly, get_best_begin_point, polygonToRotRectangle_batch, hbb2obb_v2, dbbox2delta_v3, best_match_dbbox2delta, delta2dbbox_v3
from jdet.ops.nms_rotated import multiclass_nms_rotated
from jdet.models.boxes.box_ops import rotated_box_to_poly

def bbox_target_rbbox(pos_bboxes_list,
                neg_bboxes_list,
                pos_assigned_gt_inds_list,
                gt_masks_list,
                pos_gt_labels_list,
                cfg,
                reg_classes=1,
                target_means=[.0, .0, .0, .0, .0],
                target_stds=[1.0, 1.0, 1.0, 1.0, 1.0],
                concat=True,
                with_module=True,
                hbb_trans='hbb2obb_v2'):
    labels, label_weights, bbox_targets, bbox_weights = multi_apply(
        bbox_target_rbbox_single,
        pos_bboxes_list,
        neg_bboxes_list,
        pos_assigned_gt_inds_list,
        gt_masks_list,
        pos_gt_labels_list,
        cfg=cfg,
        reg_classes=reg_classes,
        target_means=target_means,
        target_stds=target_stds,
        with_module=with_module,
        hbb_trans=hbb_trans)

    if concat:
        labels = jt.contrib.concat(labels, 0)
        label_weights = jt.contrib.concat(label_weights, 0)
        bbox_targets = jt.contrib.concat(bbox_targets, 0)
        bbox_weights = jt.contrib.concat(bbox_weights, 0)
    return labels, label_weights, bbox_targets, bbox_weights

def bbox_target_rbbox_single(pos_bboxes,
                       neg_bboxes,
                       pos_assigned_gt_inds,
                       gt_masks,
                       pos_gt_labels,
                       cfg,
                       reg_classes=1,
                       target_means=[.0, .0, .0, .0, .0],
                       target_stds=[1.0, 1.0, 1.0, 1.0, 1.0],
                       with_module=True,
                       hbb_trans='hbb2obb_v2'):
    num_pos = pos_bboxes.size(0)
    num_neg = neg_bboxes.size(0)
    num_samples = num_pos + num_neg
    labels = jt.zeros(num_samples, dtype=jt.int)        #origin: torch.long
    label_weights = jt.zeros(num_samples)
    bbox_targets = jt.zeros((num_samples, 5))
    bbox_weights = jt.zeros((num_samples, 5))
    pos_gt_masks = gt_masks[pos_assigned_gt_inds]
    pos_gt_polys = mask2poly(pos_gt_masks)
    pos_gt_bp_polys = get_best_begin_point(pos_gt_polys)
    pos_gt_obbs = jt.array(polygonToRotRectangle_batch(pos_gt_bp_polys, with_module))

    if pos_bboxes.size(1) == 4:
        pos_ext_bboxes = hbb2obb_v2(pos_bboxes)
    else:
        pos_ext_bboxes = pos_bboxes

    if num_pos > 0:
        labels[:num_pos] = pos_gt_labels
        pos_weight = 1.0 if cfg['pos_weight'] <= 0 else cfg['pos_weight']
        label_weights[:num_pos] = pos_weight
        if with_module:
            pos_bbox_targets = dbbox2delta(pos_ext_bboxes, pos_gt_obbs, target_means,
                                          target_stds)
        else:
            pos_bbox_targets = dbbox2delta_v3(pos_ext_bboxes, pos_gt_obbs, target_means,
                                              target_stds)
        bbox_targets[:num_pos, :] = pos_bbox_targets
        bbox_weights[:num_pos, :] = 1
    if num_neg > 0:
        label_weights[-num_neg:] = 1.0

    return labels, label_weights, bbox_targets, bbox_weights

def rbbox_target_rbbox(pos_rbboxes_list,
                         neg_rbboxes_list,
                         pos_gt_rbboxes_list,
                         pos_gt_labels_list,
                         cfg,
                         reg_classes=1,
                         target_means=[.0, .0, .0, .0, 0],
                         target_stds=[1.0, 1.0, 1.0, 1.0, 1.0],
                         concat=True):
    labels, label_weights, bbox_targets, bbox_weights = multi_apply(
        rbbox_target_rbbox_single,
        pos_rbboxes_list,
        neg_rbboxes_list,
        pos_gt_rbboxes_list,
        pos_gt_labels_list,
        cfg=cfg,
        reg_classes=reg_classes,
        target_means=target_means,
        target_stds=target_stds)

    if concat:
        labels = jt.contrib.concat(labels, 0)
        label_weights = jt.contrib.concat(label_weights, 0)
        bbox_targets = jt.contrib.concat(bbox_targets, 0)
        bbox_weights = jt.contrib.concat(bbox_weights, 0)
    return labels, label_weights, bbox_targets, bbox_weights

def rbbox_target_rbbox_single(pos_rbboxes,
                       neg_rbboxes,
                       pos_gt_rbboxes,
                       pos_gt_labels,
                       cfg,
                       reg_classes=1,
                       target_means=[.0, .0, .0, .0, .0],
                       target_stds=[1.0, 1.0, 1.0, 1.0, 1.0]):
    assert pos_rbboxes.size(1) == 5
    num_pos = pos_rbboxes.size(0)
    num_neg = neg_rbboxes.size(0)
    num_samples = num_pos + num_neg
    labels = jt.zeros(num_samples, dtype=jt.int)        #origin: torch.long
    label_weights = jt.zeros(num_samples)
    bbox_targets = jt.zeros((num_samples, 5))
    bbox_weights = jt.zeros((num_samples, 5))

    if num_pos > 0:
        labels[:num_pos] = pos_gt_labels
        pos_weight = 1.0 if cfg.pos_weight <= 0 else cfg.pos_weight
        label_weights[:num_pos] = pos_weight
        pos_bbox_targets = best_match_dbbox2delta(pos_rbboxes, pos_gt_rbboxes, target_means, target_stds)
        bbox_targets[:num_pos, :] = pos_bbox_targets
        bbox_weights[:num_pos, :] = 1
    if num_neg > 0:
        label_weights[-num_neg:] = 1.0

    return labels, label_weights, bbox_targets, bbox_weights

def accuracy(pred, target, topk=1):
    if isinstance(topk, int):
        topk = (topk, )
        return_single = True
    else:
        return_single = False

    maxk = max(topk)
    _, pred_label = pred.topk(maxk, 1, True, True)
    pred_label = pred_label.t()
    correct = pred_label.equal(target.view(1, -1).expand_as(pred_label))

    res = []
    for k in topk:
        correct_k = correct[:k].view(-1).float().sum(0, keepdims=True)
#        res.append(correct_k.mul_(100.0 / pred.size(0)))
        correct_k *= 100.0 / pred.shape[0]
        res.append(correct_k)
    return res[0] if return_single else res

@HEADS.register_module()
class BBoxHeadRbbox(nn.Module):
    """Simplest RoI head, with only two fc layers for classification and
    regression respectively"""

    def __init__(self,
                 with_avg_pool=False,
                 with_cls=True,
                 with_reg=True,
                 roi_feat_size=7,
                 in_channels=256,
                 num_classes=19,
                 target_means=[0., 0., 0., 0., 0.],
                 target_stds=[0.1, 0.1, 0.2, 0.2, 0.1],
                 reg_class_agnostic=False,
                 with_module=True,
                 hbb_trans='hbb2obb_v2',
                 loss_cls=dict(
                     type='CrossEntropyLoss',
                     use_sigmoid=False,
                     loss_weight=1.0),
                 loss_bbox=dict(
                     type='SmoothL1Loss', beta=1.0, loss_weight=1.0)):
        super(BBoxHeadRbbox, self).__init__()
        assert with_cls or with_reg
        self.with_avg_pool = with_avg_pool
        self.with_cls = with_cls
        self.with_reg = with_reg
        self.roi_feat_size = roi_feat_size
        self.in_channels = in_channels
        self.num_classes = num_classes
        self.target_means = target_means
        self.target_stds = target_stds
        self.reg_class_agnostic = reg_class_agnostic

        self.loss_cls = build_from_cfg(loss_cls, LOSSES)
        self.loss_bbox = build_from_cfg(loss_bbox, LOSSES)

        in_channels = self.in_channels
        if self.with_avg_pool:
            self.avg_pool = nn.AvgPool2d(roi_feat_size)
        else:
            if isinstance(self.roi_feat_size, int):
                in_channels *= (self.roi_feat_size * self.roi_feat_size)
            elif isinstance(self.roi_feat_size, tuple):
                assert len(self.roi_feat_size) == 2
                assert isinstance(self.roi_feat_size[0], int)
                assert isinstance(self.roi_feat_size[1], int)
                in_channels *= (self.roi_feat_size[0] * self.roi_feat_size[1])
        if self.with_cls:
            self.fc_cls = nn.Linear(in_channels, num_classes)
        if self.with_reg:
            out_dim_reg = 5 if reg_class_agnostic else 5 * num_classes
            self.fc_reg = nn.Linear(in_channels, out_dim_reg)
        self.debug_imgs = None
        self.with_module = with_module
        self.hbb_trans = hbb_trans

    def init_weights(self):
        if self.with_cls:
#            nn.init.normal_(self.fc_cls.weight, 0, 0.01)
            nn.init.gauss_(self.fc_cls,0, 0.01)
            nn.init.constant_(self.fc_cls.bias, 0)
        if self.with_reg:
#            nn.init.normal_(self.fc_reg.weight, 0, 0.001)
            nn.init.gauss_(self.fc_reg.weight, 0, 0.001)
            nn.init.constant_(self.fc_reg.bias, 0)

    def excute(self, x):
        if self.with_avg_pool:
            x = self.avg_pool(x)
        x = x.reshape(x.shape[0], -1)
        cls_score = self.fc_cls(x) if self.with_cls else None
        bbox_pred = self.fc_reg(x) if self.with_reg else None
        return cls_score, bbox_pred

    def get_target(self, sampling_results, gt_masks, gt_labels, rcnn_train_cfg):
        pos_proposals = [res.pos_bboxes for res in sampling_results]
        neg_proposals = [res.neg_bboxes for res in sampling_results]
        pos_assigned_gt_inds = [res.pos_assigned_gt_inds  for res in sampling_results]
        pos_gt_labels = [res.pos_gt_labels for res in sampling_results]
        reg_classes = 1 if self.reg_class_agnostic else self.num_classes
        cls_reg_targets = bbox_target_rbbox(
            pos_proposals,
            neg_proposals,
            pos_assigned_gt_inds,
            gt_masks,
            pos_gt_labels,
            rcnn_train_cfg,
            reg_classes,
            target_means=self.target_means,
            target_stds=self.target_stds,
            with_module=self.with_module,
            hbb_trans=self.hbb_trans)
        return cls_reg_targets

    def get_target_rbbox(self, sampling_results, gt_bboxes, gt_labels,
                   rcnn_train_cfg):
        pos_proposals = [res.pos_bboxes for res in sampling_results]
        neg_proposals = [res.neg_bboxes for res in sampling_results]
        pos_gt_bboxes = [res.pos_gt_bboxes for res in sampling_results]
        pos_gt_labels = [res.pos_gt_labels for res in sampling_results]
        reg_classes = 1 if self.reg_class_agnostic else self.num_classes
        cls_reg_targets = rbbox_target_rbbox(
            pos_proposals,
            neg_proposals,
            pos_gt_bboxes,
            pos_gt_labels,
            rcnn_train_cfg,
            reg_classes,
            target_means=self.target_means,
            target_stds=self.target_stds)
        return cls_reg_targets

    def get_det_bboxes(self,
                       rois,
                       cls_score,
                       bbox_pred,
                       img_shape,
                       scale_factor,
                       rescale=False,
                       cfg=None):
        if isinstance(cls_score, list):
            cls_score = sum(cls_score) / float(len(cls_score))
        scores = nn.softmax(cls_score, dim=1) if cls_score is not None else None

        if rois.size(1) == 5:
            obbs = hbb2obb_v2(rois[:, 1:])
        elif rois.size(1) == 6:
            obbs = rois[:, 1:]
        else:
            print('strange size')
            import pdb
            pdb.set_trace()
        hook.record('obbs', obbs)
        hook.record('bbox_pred', bbox_pred)
        hook.record('scores', scores)
        if bbox_pred is not None:
            dbboxes = delta2dbbox_v3(obbs, bbox_pred, self.target_means,
                                    self.target_stds, img_shape)
        else:
            dbboxes = obbs

        if rescale:
            dbboxes[:, 0::5] /= scale_factor
            dbboxes[:, 1::5] /= scale_factor
            dbboxes[:, 2::5] /= scale_factor
            dbboxes[:, 3::5] /= scale_factor
        hook.record('dbboxes', dbboxes)
        hook.record('scores', scores)
        det_bboxes, det_labels = multiclass_nms_rotated(dbboxes, scores,
                                                cfg.score_thr, cfg.nms,
                                                cfg.max_per_img)
        det_bboxes = jt.contrib.concat([rotated_box_to_poly(det_bboxes), det_bboxes[:,-1:]], -1)
        hook.record('det_bboxes', det_bboxes)
        hook.record('det_labels', det_labels)
        return det_bboxes, det_labels

    def loss(self,
             cls_score,
             bbox_pred,
             labels,
             label_weights,
             bbox_targets,
             bbox_weights,
             reduce=True):
        losses = dict()
        if cls_score is not None:
            losses['rbbox_loss_cls'] = self.loss_cls(
                cls_score, labels, label_weights, reduce=reduce)
            losses['rbbox_acc'] = accuracy(cls_score, labels)
        if bbox_pred is not None:
            pos_inds = labels > 0
            if self.reg_class_agnostic:
                pos_bbox_pred = bbox_pred.view(bbox_pred.size(0), 5)[pos_inds]
            else:
                pos_bbox_pred = bbox_pred.view(bbox_pred.size(0), -1,
                                               5)[pos_inds, labels[pos_inds]]
            losses['rbbox_loss_bbox'] = self.loss_bbox(
                pos_bbox_pred,
                bbox_targets[pos_inds],
                bbox_weights[pos_inds],
                avg_factor=bbox_targets.size(0))
        return losses


from flask import Blueprint,render_template, request,session
from app.models.base import db
from app.models.course import Course
from app.models.teacher import Teacher

courseBP = Blueprint('course', __name__)

@courseBP.route('', methods=['GET','POST'])
def course_add():
    teacher = Teacher.query.filter(Teacher.email == session.get("email")).first()
    if request.method == 'GET':
        return render_template("course_add.html", teacher=teacher)
    else:
        id = request.form.get('id')
        name = request.form.get('name')
        teacher.course_id = teacher.course_id + ',' + id
        print(teacher.course_id)
        db.session.commit()
        with db.auto_commit():
                course = Course(name,id)
                db.session.add(course)
        return render_template("course_add.html", teacher=teacher)


from pathlib import Path
import discord
from discord.ext import commands

class MusicBot(commands.Bot):
    def __init__(self):
        self._cogs = [p.stem for p in Path(".").glob("./bot/cogs/*.py")] # Accessing all the cogs
        super().__init__(command_prefix= self.prefix, case_insensitive= True, intents=discord.Intents.all())

    def setup(self):
        print("Running Setup...")

        # Loading the accessed cogs
        for cog in self._cogs:
            self.load_extension(f"bot.cogs.{cog}")
            print(f" Loaded `{cog}` cog")

        print("Setup Complete!")

    def run(self):
        self.setup()

        # Loading the token
        with open("data/token.0", "r", encoding="utf-8") as f:
            TOKEN = f.read()

        print("Running the BOT...")
        super().run(TOKEN, reconnect=True) # Bot of this token will run

# Some auxiliary functions
    async def shutdown(self):
        print("Closing connection to Discord...")
        await super().close()

    async def close(self):
        print("Closing on keyboard interrupt...")
        await self.shutdown()

    async def on_connect(self):
        print(f" Connected to Discord (latency: {self.latency*1000:,.0f} ms)")

    async def on_resumed(self):
        print("Bot resumed.")

    async def on_disconnect(self):
        print("Bot disconnected.")

    async def on_error(self, err, *args, **kwargs):
        raise

    async def on_command_error(self, ctx, exc):
        raise getattr(exc, "original", exc)

    async def on_ready(self):
        self.client_id = (await self.application_info()).id
        print("Bot is powered up & ready to do some COOL STUFFS!!!")

        # Discord RPC & activity status
        await self.change_presence(status=discord.Status.do_not_disturb, activity=discord.Game(name="A Cool Chicken"))

    # Command prefix
    async def prefix(self, bot, msg):
        return commands.when_mentioned_or(">>")(bot, msg)

    async def process_commands(self, message):
        ctx = await self.get_context(message, cls=commands.Context)

        if ctx.command is not None:
            await self.invoke(ctx)

    # Will only read the messages which starts with the command prefix
    async def on_message(self, message):
        if not message.author.bot:
            await self.process_commands(message)


import sys
from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtWidgets import (
    QApplication,
    QCheckBox,
    QDialog,
    QGridLayout,
    QGroupBox,
    QVBoxLayout,
)
databases = ["db{}".format(i) for i in range(10)]
class App(QDialog):
    def __init__(self):
        super().__init__()
        self.title = "MySQL Timing Discrepancies"
        self.left = 10
        self.top = 10
        self.width = 320
        self.height = 100
        self.initUI()
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self._databases_checked = []
        self.createGridLayout()
        windowLayout = QVBoxLayout(self)
        windowLayout.addWidget(self.horizontalGroupBox)
        self.show()
    @property
    def databases_checked(self):
        return self._databases_checked
    def createGridLayout(self):
        self.horizontalGroupBox = QGroupBox("Databases")
        layout = QGridLayout()
        self.horizontalGroupBox.setLayout(layout)
        for i in range(3):
            layout.setColumnStretch(i, 0)
        row_num = 0
        # Generating checkboxes here
        for i, database in enumerate(databases):
            col_num = i % 3
            row_num = i if col_num == 0 else row_num
            cb = QCheckBox(database)
            cb.setChecked(False)
            cb.stateChanged.connect(self.on_stateChanged)
            layout.addWidget(cb, row_num, col_num)
    @pyqtSlot(int)
    def on_stateChanged(self, state):
        checkbox = self.sender()
        text = checkbox.text()
        if state == Qt.Checked:
            self._databases_checked.append(text)
        else:
            self._databases_checked.remove(text)
        print(self.databases_checked)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())


from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
import time

contact_name="name"
driver = webdriver.Chrome(executable_path=r'C:\Users\HP\Desktop\chromedriver.exe')
driver.get("https://web.whatsapp.com/")
a = input("Press enter after scaning QR code")
inp_xpath_search = " //div[@class='_3FRCZ copyable-text selectable-text']"
input_box_search = WebDriverWait(driver,50).until(lambda driver: driver.find_element_by_xpath(inp_xpath_search))
input_box_search.click()
time.sleep(2)
input_box_search.send_keys(contact_name)
time.sleep(2)
user = driver.find_element_by_xpath("//span[@title='{}']".format(contact_name))
user.click()
chat = driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
for i in range(500):
    chat.send_keys("Guess who learned to automate whatsapp using Python")
    chat.send_keys(Keys.RETURN)


import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib import colors
import copy
import time

def fire(L,p):
    X  = np.zeros((L, L))
    X[1:L-1, 1:L-1] = np.random.random(size=(L-2, L-2)) < p

    fire=0
    for x in range(1,L):
        if X[x, 1] == 1:
            X[x, 1]=2
            fire+=1

    i=2
    while fire>0:
        Y = copy.deepcopy(X)
        for x in range(1,L-1):
            for y in range(1,L-1):
                if Y[x,y] == 2:
                    X[x,y] = 3
                    fire-=1
                elif Y[x,y] == 1:
                    if 2 in [Y[x-1,y-1],Y[x-1,y],Y[x,y-1],Y[x+1,y+1],Y[x+1,y],Y[x,y+1],Y[x+1,y-1],Y[x-1,y+1]]:
                        X[x,y] = 2
                        fire+=1
    return X

def percolation(L,N):
    p1=list(np.linspace(0.1,0.9,15))
    end=[]
    for i in p1:
        koniec=0
        for j in range(N):
            X = fire(L,i)
            if 3 in X[:,L-2]:
                koniec+=1
        print(i)
        end.append(koniec)

    wynik = [x/N for x in end]
    plt.plot(p1,wynik)
    plt.grid()
    plt.xlabel('p')
    plt.title('L={}, MC={}'.format(L-2, N))
    plt.show()

def main():
    L=22
    N=100
    start = time.clock()
    percolation(L,N)
    end = time.clock()
    total = end - start
    print(total)

if __name__=="__main__":
    main()


import pandas as pd
import os
import pandas as pd
from matplotlib.pyplot import *
from parse import parse
from scipy.stats import *

def Maxima(raw_data, Emin, Emax):
    data = raw_data.query(f'd< {Emax}')
    data = data.query(
        f'd > {Emin} & a<b<c<d & d>e')  # filtraggio dei dati in modo da selezionare solo gli eventi che soddisfano la condizione del query

    # parametri della parabola generica di eq ax^2 + bx + c
    popt = pd.DataFrame({'a': 0.5 * data['c'] - data['d'] + 0.5 * data['e'],
                         'b': -0.5 * data['c'] + 0.5 * data['e'],
                         'c': data['d']})

    data.loc[:, 'tmax'] = 4 + np.divide(-popt['b'], 2. * popt['a'])
    data.loc[:, 'Emax'] = np.divide(-np.power(popt['b'], 2.), 4. * popt['a']) + popt['c']

    data['t_s'] = 0

    # calcolo delle interpolazioni in base a dove capita Emax/2
    mask = (((data.a) <= (data.Emax * 0.5)) & ((data.Emax * 0.5) <= (data.b)))  # a<Emax/2<b
    data.loc[mask, 't_s'] = (data['Emax'] * 0.5 - data['a']) * 1 / (data['b'] - data['a']) + 1

    mask = (((data.b) <= (data.Emax * 0.5)) & ((data.Emax * 0.5) <= (data.c)))  # b<Emax/2<c
    data.loc[mask, 't_s'] = (data['Emax'] * 0.5 - data['b']) * 1 / (data['c'] - data['b']) + 2

    mask = (((data.c) <= (data.Emax * 0.5)) & ((data.Emax * 0.5) <= (data.d)))  # c<Emax/2<d
    data.loc[mask, 't_s'] = (data['Emax'] * 0.5 - data['c']) * 1 / (data['d'] - data['c']) + 3

    # calcolo delle interpolazioni in base a dove capita Emax/2
    data['finetime'] = 0
    mask = (((data.a) <= (data.Emax * 0.5)) & ((data.Emax * 0.5) <= (data.b)))  # a<Emax/2<b
    data.loc[mask, 'finetime'] = (data['Emax'] * 0.5 - data['a']) * 255 / (data['b'] - data['a'])

    mask = (((data.b) <= (data.Emax * 0.5)) & ((data.Emax * 0.5) <= (data.c)))  # b<Emax/2<c
    data.loc[mask, 'finetime'] = (data['Emax'] * 0.5 - data['b']) * 255 / (data['c'] - data['b'])

    mask = (((data.c) <= (data.Emax * 0.5)) & ((data.Emax * 0.5) <= (data.d)))  # c<Emax/2<d
    data.loc[mask, 'finetime'] = (data['Emax'] * 0.5 - data['c']) * 255 / (data['d'] - data['c'])

    data['delta t'] = data['tmax'] - data['t_s']

    return data

def lettura_file(path):
    files = os.listdir(path)
    df_from_each_file = (pd.read_csv(path + f, sep=' ') for f in files)
    data = pd.concat(df_from_each_file, ignore_index=True)
    data = data.rename(columns={'1st_sample': 'a',
                                '2nd_sample': 'b',
                                '3rd_sample': 'c',
                                '4th_sample': 'd',
                                '5th_sample': 'e'})  # ho dovuto rinominare le colonne per problemi con il query
    return data

def picchi(path):
    for i, f in enumerate(os.listdir(path)):
        data = pd.read_csv(path + f, sep=' ')
        data = data.rename(columns={'1st_sample': 'a',
                                    '2nd_sample': 'b',
                                    '3rd_sample': 'c',
                                    '4th_sample': 'd',
                                    '5th_sample': 'e'})
        data = data.query('d< 256')
        data = data.query('d > 64 & a<b<c<d & d>e')
        pattern = 'peak_finder_dump_fifo_1ch_lkrl0-fe-1{code}_Thu__08_Sep_2016_15-28-46.csv'
        c = parse(pattern, f)
        os.makedirs(os.getcwd() + '/Picchi/', exist_ok=True)
        if 'a' in c['code']:
            data.to_csv(os.getcwd() + '/Picchi/' + '%.2d%s.csv' % (i + 14, c['code']), sep=' ')
        elif 'b' in c['code']:
            data.to_csv(os.getcwd() + '/Picchi/' + '%.2d%s.csv' % (27 - i, c['code']), sep=' ')
        else:
            pass
    return None

def analisi(raw_data, E_min, E_max):
    folder_path = os.getcwd()
    immagini = folder_path + '/Immagini'
    o = 0

    for i, j in zip(E_min, E_max):
        data = Maxima(raw_data, i, j)
        tempi = np.asarray(abs(data['delta t'])) * 25
        PATH = immagini + "/E_min" + str(i)
        os.makedirs(PATH, exist_ok=True)
        o = o + 1

        title('Istogramma Finetime\n ($%2.1f<E<%2.1f$ GeV , Conteggi totali = %d / %d) ' % (
        i * 0.056, j * 0.056, len(data), len(raw_data)))
        ylabel('Conteggi')
        xlabel('Finetime')
        hist(data['finetime'], bins=32, alpha=0.70)
        savefig(PATH + '/hist_finetime%d.png' % o)
        close()

        n, bin_edges, patches = hist(tempi, bins='auto', density=True, alpha=0.75)
        (mu, sigma) = norm.fit(tempi)
        x = np.arange(min(bin_edges) - 0.5, max(bin_edges) + 0.5, 1 / len(data))
        y = norm.pdf(x, mu, sigma)
        plot(x, y, color='red', label='Gaussiana')

        title('$\mathrm{Distribuzione\ Tempi}$ \n ($%2.1f<E<%2.1f$ GeV, Conteggi totali = %d / %d) ' % (
        i * 0.056, j * 0.056, len(data), len(raw_data)))
        ylabel('Probabilità')
        xlabel('$\Delta$t (ns)')
        legend()
        # p = legend().get_window_extent()  # per grafici migliori
        draw()

        # legenda per Gaussiana
        textstr = '\n'.join((
            r'$\mu=%.2f$' % (mu,),
            r'$\sigma=%.2f$' % (sigma,)))
        props = dict(boxstyle='round', facecolor='red', alpha=0.4)
        # text(p.p0[0]+5,p.p0[1]-10, textstr, fontsize=9, verticalalignment='top', bbox=props, transform=None)

        savefig(PATH + '/hist_tempi%d.png' % o)
        close()
    return None

def energie(raw_data):
    data = raw_data.query('d > 0 & a<b<c<d & d>e')

    popt = pd.DataFrame({'a': 0.5 * data['c'] - data['d'] + 0.5 * data['e'],
                         'b': -0.5 * data['c'] + 0.5 * data['e'],
                         'c': data['d']})

    data.loc[:, 'tmax'] = 4 + np.divide(-popt['b'], 2. * popt['a'])
    data.loc[:, 'Emax'] = np.divide(-np.power(popt['b'], 2.), 4. * popt['a']) + popt['c']

    data['t_s'] = 0

    # calcolo delle interpolazioni in base a dove capita Emax/2
    mask = (((data.a) <= (data.Emax * 0.5)) & ((data.Emax * 0.5) <= (data.b)))  # a<Emax/2<b
    data.loc[mask, 't_s'] = (data['Emax'] * 0.5 - data['a']) * 1 / (data['b'] - data['a']) + 1

    mask = (((data.b) <= (data.Emax * 0.5)) & ((data.Emax * 0.5) <= (data.c)))  # b<Emax/2<c
    data.loc[mask, 't_s'] = (data['Emax'] * 0.5 - data['b']) * 1 / (data['c'] - data['b']) + 2

    mask = (((data.c) <= (data.Emax * 0.5)) & ((data.Emax * 0.5) <= (data.d)))  # c<Emax/2<d
    data.loc[mask, 't_s'] = (data['Emax'] * 0.5 - data['c']) * 1 / (data['d'] - data['c']) + 3

    data['delta t'] = data['tmax'] - data['t_s']

    folder_path = os.getcwd()
    immagini = folder_path + '/Immagini'

    hist(data['Emax'] * 0.056, bins='auto', alpha=0.75)
    title('$\mathrm{Istogramma\ Energie}$ \n (E>%2.1f GeV, Conteggi totali = %d / %d) ' % (
    20 * 0.056, len(data), len(raw_data)))
    yscale('log')
    ylabel('Conteggi')
    xlabel('Energia (GeV)')
    savefig(immagini + '/hist_energie.png')
    close()
    return None