"""Add auto increment id

Revision ID: 52d0db93f8b7
Revises: 54456124a52f
Create Date: 2021-03-10 22:40:42.015991-08:00

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '52d0db93f8b7'
down_revision = '54456124a52f'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('user_ride_tags',
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('ride_id', sa.Integer(), nullable=False),
    sa.Column('tag_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['ride_id'], ['ride.id'], ),
    sa.ForeignKeyConstraint(['tag_id'], ['tag.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
    sa.PrimaryKeyConstraint('user_id', 'ride_id', 'tag_id')
    )
    op.drop_table('user_ride_tag')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('user_ride_tag',
    sa.Column('user_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('ride_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('tag_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['ride_id'], ['ride.id'], name='user_ride_tag_ride_id_fkey'),
    sa.ForeignKeyConstraint(['tag_id'], ['tag.id'], name='user_ride_tag_tag_id_fkey'),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], name='user_ride_tag_user_id_fkey'),
    sa.PrimaryKeyConstraint('user_id', 'ride_id', 'tag_id', name='user_ride_tag_pkey')
    )
    op.drop_table('user_ride_tags')
    # ### end Alembic commands ###


from .BaseNLP import NLPBase

import re
from nltk.stem import WordNetLemmatizer
import nltk
from nltk.corpus import stopwords
from sklearn.metrics import precision_recall_fscore_support
from sklearn.svm import  SVC
from nltk.classify import SklearnClassifier
from sklearn.metrics import classification_report, accuracy_score, precision_recall_fscore_support
from alive_progress import alive_bar
import random
import pickle
from joblib import dump, load

nltk.download('wordnet')
nltk.download('stopwords')
nltk.download('omw-1.4')

class LinearSVC(NLPBase):
    def pre_process(self,text):
        text = text.lower()
        lemma = WordNetLemmatizer()
        words_filter = [""]
        text = re.sub(r"(\w)([.,;:!?'\"”\)\[\]])", r"\1 \2", text)
        text = re.sub(r"([.,;:!?'\"“\(\)\[\]])(\w)", r"\1 \2", text)
        words = text.split(" ")
        final_tokenized_string = []
        for word in words:
            if word not in stopwords.words('english'):
                lemma_word = lemma.lemmatize(word)
                final_tokenized_string.append(lemma_word)

        # remove blank words from final_tokenized_string
        final_tokenized_string = list(filter(lambda val: val not in words_filter, final_tokenized_string))

        # print(final_tokenized_string)
        return final_tokenized_string

    def split_and_preprocess_data(self, data):
        """Split the data between train_data and test_data according to the percentage
        and performs the preprocessing."""
        train_data = []
        test_data = []

        num_samples = len(data)
        num_training_samples = int((self.percentage * num_samples))
        print("Generating training data")
        with alive_bar(len(data[:num_training_samples])) as bar:
            for (text, label) in data[:num_training_samples]:
                train_data.append((self.to_feature_vector(self.pre_process(text)),label))
                bar()

        print("Generating test data")
        with alive_bar(len(data[num_training_samples:])) as bar:
            for (text, label) in data[num_training_samples:]:
                test_data.append((self.to_feature_vector(self.pre_process(text)),label))
                bar()

        return train_data, test_data

    def to_feature_vector(self, tokens):
        # Should return a dictionary containing features as keys, and weights as values

        local_feature_dict = {}
        word_index = 0
        # Loop through every word in token
        for word in tokens:

            # Check to see if the word is not in the global_feature_dict
            if word in self.global_feature_dict:
                # get the index of the word
                word_index = self.global_feature_dict[word]
            else:
                # Add new word with corresponding index to global_feature_dict
                word_index = len(self.global_feature_dict) + 1
                self.global_feature_dict[word] = word_index

            # Check to see if word is in the local_feature_dict
            # Only applies to this set of tokens
            if word_index in local_feature_dict:
                # Increment value in local_feature_dict
                local_feature_dict[word_index] += 1/len(tokens)
            else:
                # Set the key of word to 1/length of tokens in local_feature_dict
                local_feature_dict[word_index] = 1/len(tokens)

        # Return local_feature_dict as global_feature_dict is set globally (as stated in the name)
        return local_feature_dict

    def cross_validate(self, dataset, folds, classifications):
        results = []
        fold_size = int(len(dataset)/folds) + 1
        for i in range(0,len(dataset),int(fold_size)):
            # insert code here that trains and tests on the 10 folds of data in the dataset
            if self.debug:
                print("Fold start on items %d - %d" % (i, i+fold_size))

            # FILL IN THE METHOD HERE
            statements = [x[0] for x in dataset[i:i+fold_size]]
            correct_outputs = [x[1] for x in dataset[i:i+fold_size]]
            classifier = self.train_classifier(dataset[i:i+fold_size])
            predicted_outputs = self.predict_labels(statements,classifier)
            if len(set(predicted_outputs)) != len(classifications):
                # raise Exception("Classifier did not learn all labels")
                print("Case of classifier not learning all classes in fold")
                continue

            if self.debug:
                print(classification_report(correct_outputs,predicted_outputs,target_names=classifications))

            (precision, recall, f1, _) = precision_recall_fscore_support(correct_outputs,predicted_outputs, average = "macro")
            accuracy = accuracy_score(correct_outputs,predicted_outputs)

            results.append(( precision, recall, f1, accuracy))

        cv_results = []
        for result_index in range(4):
            sum_of_values = 0
            for result in results:
                sum_of_values += result[result_index]
            cv_results.append(sum_of_values/len(results))
        if self.debug:
            print(cv_results)
        return cv_results, classifier

    def get_model_stats_against_test_set(self, classifier, test_set, classifications):
        statements = [x[0] for x in test_set]
        correct_outputs = [x[1] for x in test_set]
        predicted_outputs = self.predict_labels(statements,classifier)
        print(classification_report(correct_outputs,predicted_outputs,target_names=classifications))
        (precision, recall, f1, _) = precision_recall_fscore_support(correct_outputs,predicted_outputs, average = "macro")
        accuracy = accuracy_score(correct_outputs,predicted_outputs)
        print("============")
        print("Model Against Test Split")
        print("============")
        print(f"Precision: {precision}")
        print(f"Recall: {recall}")
        print(f"F1: {f1}")
        print(f"Accuracy: {accuracy}")

    def predict_labels(self, samples, classifier):
        """Assuming preprocessed samples, return their predicted labels from the classifier model."""
        return classifier.classify_many(samples)

    def predict_label_from_raw(self, sample, classifier):
        """Assuming raw text, return its predicted label from the classifier model."""
        return classifier.classify(self.to_feature_vector(self.pre_process(sample)))

    def predict_label_list_from_raw(self, sample, classifier):
        prob = classifier.prob_classify(self.to_feature_vector(self.pre_process(sample)))
        non_zero = prob.samples()
        prob_dict = {}
        for i in non_zero:
            prob_dict[i] = prob.prob(i)
        return prob_dict

    def train_classifier(self, data):
        # pipeline =  Pipeline([('svc', LinearSVC())])
        # return SklearnClassifier(pipeline).train(data)
        return SklearnClassifier(SVC(kernel='linear',probability=True)).train(data)

    def save_model(self, x_classifier, y_classifier):
        folder = 'NLPModel/Linear SVC Models/'
        filename = 'x_classifier.joblib'
        dump(x_classifier, folder+filename)

        filename = 'y_classifier.joblib'
        dump(y_classifier, folder+filename)

        filename = 'global_feature_vector.joblib'
        dump(self.global_feature_dict, folder+filename)

    def start(self):
        # load in data
        # raw_data defined here
        self.load_data()

        x_data, y_data = self.define_categories()

        # train_data and test_data defined here
        x_train, x_test = self.split_and_preprocess_data(x_data)
        y_train, y_test = self.split_and_preprocess_data(y_data)

        print("Training X Classifier...")
        x_results, x_classifier = self.cross_validate(x_train, 10, self.x_split_cats)
        print(x_results)
        print("Training Y Classifier...")
        y_results, y_classifier = self.cross_validate(y_train, 10, self.y_split_cats)
        print(y_results)
        # self.save_model(x_classifier, y_classifier)
        print("\n")
        print("X Axis against test set...")
        self.get_model_stats_against_test_set(x_classifier, x_test, self.x_split_cats)
        print("\n")
        print("Y Axis against test set...")
        self.get_model_stats_against_test_set(y_classifier, y_test, self.y_split_cats)

    def load_model(self):
        folder = 'NLPModel/Linear SVC Models/'
        filename = 'x_classifier.joblib'
        x_classifier = load(folder+filename)
        filename = 'y_classifier.joblib'
        y_classifier = load(folder+filename)
        filename = 'global_feature_vector.joblib'
        self.global_feature_dict = load(folder+filename)

        return x_classifier, y_classifier

    def get_manual_prediction(self, x_classifier, y_classifier, statement = None):
        print("Manual Classifier...")
        if statement is None:
            statement = input("Input to Classify:\n")
        print("X Classifier")
        x_confidence = self.predict_label_list_from_raw(statement, x_classifier)
        print(x_confidence)
        print("Y Classifier")
        y_confidence = self.predict_label_list_from_raw(statement, y_classifier)
        print(y_confidence)
        return x_confidence, y_confidence


import pandas as pd

def main(blacklist=[]):
    '''
    Reads excel/csv files into json format
    Excludes worksheets in blacklist
    '''
    import os
    json_ls = list()
    f_walk = os.walk('data_folder')

    for subdir, dir, files in f_walk:
        for f in files:
            # pandas not only provides file reading functionality,
            # but outputs in a single format
            # if performance requires, other packages may be implemented,
            # that may require additional processing after reading in the file
            fp = os.path.join(subdir, f)
            f_ext = f.split('.')[-1]
            table_ls = []
            if f_ext[:3] == 'xls':
                table = pd.read_excel(fp, sheet_name=None)
                if type(table) == dict:
                    for k in table:
                        if k not in blacklist:
                            table_ls.append((k+f, table[k]))
                        else:
                            continue
            elif f_ext == 'csv':
                table_ls.append((f, pd.read_csv(fp)))
            elif f_ext == 'gitkeep':
                # for directory processing
                continue
            else:
                return Exception('File Type Not Supported')

            for t in table_ls:
                # table_ls is list of tuples
                # index 0: unique name
                # index 1: DataFrame
                # breakpoint()
                try:
                    table_json = table_to_json(t[1], t[0], blacklist=blacklist)
                except Exception as e:
                    print(e)
                    print(t)
                    print('-'*10)
                    table_json = []
                json_ls.append(table_json)

    return(json_ls)

def determine_fips(df):
    '''
    Returns df with fips codes
    County name in df must be in the first column or named 'County Name'
    '''
    from census_response import county_fips

    fips = county_fips()
    # Verifies county column
    # Moves properly named column to first column
    first_column_name = df.iloc[:, 0].name
    cols = df.columns
    default_name = 'County Name'
    if default_name in cols and default_name != first_column_name:
        col_ls = cols.tolist()
        col_ls.remove(default_name)
        col_ls.insert(0, default_name)
        df = df[col_ls]
    else:
        df = df.rename(columns={first_column_name: default_name})
    # add fip column, will be last column (-1)
    df['fips'] = None

    # update fip column with fip codes
    for index, row in df.iterrows():
        # print(index, row)
        fip = fips[row[0]]
        df.iloc[index, -1] = fip

    return df

def table_to_json(df,  filename, blacklist=[]):
    '''
    Converts panda df to json format
    Checks for fips column, calls determine_fips if not present
    '''
    import json

    # get county FIPs, if necessary
    columns = [c.lower() for c in df.columns]
    if 'fips' not in columns:
        # determine fips function
        df = determine_fips(df)
        # return merged dataframe

    # TODO nest data
    df = df.set_index('fips')
    df_json_str = df.to_json(orient='index')
    df_json_dict = json.loads(df_json_str)
    # breakpoint()
    df_values = [*df_json_dict.values()][0]

    final_dict = {k: {filename: v} for k, v in df_values.items()}

    return final_dict

if __name__ == '__main__':
    main(blacklist=['Key'])


from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
# Create your views here.
from django.views import View
from django_redis import get_redis_connection

# 购物车的添加的功能
from sm_goods.models import GoodsSKU

# cartadd的路由
from sm_user.public import session_decorator

# 购物车模块
class ShoppingCartViews(View):
    @session_decorator
    def get(self, request):
        """
            1. 取出商品数据 redis sku_id count
            2. 从数据库中 取出完整的商品信息
            3. 计算总金额 和 总共的商品的件数量

            :param request:
            :return:
        """
        # 连接redis
        cnn = get_redis_connection('default')
        # 从redis中将所有的商品及数量取出来
        cart_key = "cart_%s" % request.session.get("ID")
        cart_list = cnn.hgetall(cart_key)  # 字典形式
        # print(cart_list)

        # 总金额 总数量
        total_money = 0
        total_count = 0
        # 存储所有的商品
        goodsList = []
        # 遍历字典
        for sku_id, count in cart_list.items():
            sku_id = int(sku_id)
            count = int(count)
            # 总数量增加
            total_count += count
            # 获取商品的信息
            goods = GoodsSKU.objects.get(pk=sku_id)
            # 对象上添加一个自定义的属性 count 当前商品的数量
            goods.count = count

            goodsList.append(goods)
            # 总金额
            total_money += goods.goodprice * count

        # 渲染数据
        context = {
            'goodsList': goodsList,
            'total_count': total_count,
            'total_money': total_money,
        }

        return render(request, "shoppingcart/shopcart.html", context)

# 购物车添加数据
class CartAddView(View):
    def post(self, request):
        # 1  前端通过ajax提交数据过来（以post的方式），获取数据（商品的sku_id，数量）
        # 2  保存到redis中
        # 3  验证是否有登录，如果没有登录，告诉ajax中的JS代码实现跳转到登录页面，location.href=url
        # 4 验证数据的合法性，必须都是整数
        # 5 验证商品是否存在
        # 6 验证库存是否足够

        sku_id = request.POST.get("sku_id")
        count = request.POST.get("count")

        # 验证是否登录
        if request.session.get("ID") is None:
            return JsonResponse({"error": 1, "msg": "没有登录，请登录"})

        # 重session中获取用户ID
        user_id = request.session.get("ID")

        # 验证数据的合法性
        try:
            sku_id = int(sku_id)
            count = int(count)
        except Exception as e:
            return JsonResponse({"error": 2, "msg": "数据不合法"})

        # 验证商品是否存在,
        try:
            sku_goods = GoodsSKU.objects.get(pk=sku_id)
        except GoodsSKU.DoesNotExist:
            return JsonResponse({"error": 3, "msg": "商品不存在"})

        # 6 验证库存是否足够
        if sku_goods.goodsstock < count:
            return JsonResponse({"error": 4, "msg": "商品库存不够"})

        # 保存到redis数据库,配置:from  django_redis import get_redis_connection
        # 完成后,连接到redis
        cnn = get_redis_connection("default")  # 选着一个缓存配置
        # 将该用户的商品添加到redis中的购物车
        """
        HINCRBY key field increment
        为哈希表 key 中的域 field 的值加上增量 increment 。
        增量也可以为负数，相当于对给定域进行减法操作。
        如果 key 不存在，一个新的哈希表被创建并执行 HINCRBY命令。
        如果域 field 不存在，那么在执行命令前，域的值被初始化为 0
        """
        cart_key = "cart_{}".format(user_id)
        cnn.hincrby(cart_key, sku_id, count)

        cart_total = 0
        vars = cnn.hvals(cart_key)
        if len(vars) > 0:
            for v in vars:
                cart_total += int(v)
        # 添加成功
        return JsonResponse({"error": 0, "msg": "添加购物车成功!", "cart_total": cart_total})

# 购物车减少数据
class CartDecrease(View):
    """
           1. 前端通过ajax post请求方式 将 sku_id (sku商品id) 一次减一个
           3. 验证是否登陆，如果没登陆，告诉ajax中的js代码实现跳转到登陆页面 location.href=url
           4. 验证数据的合法性，必须都为整数
           5. 验证商品是否存在
           2. 保存到redis
               配置是正确
               使用 hash对象 保存
               hset key 属性 值
               hset cart_user_id 商品id 数量
               >1 -1 如果商品数量等于 0 删除该商品
       :return:
       """

    def get(self, request):
        pass

    def post(self, request):
        sku_id = request.POST.get('sku_id')

        # 验证是否登陆
        if request.session.get("ID") is None:
            return JsonResponse({"error": 1, "msg": "没有登录,请先登录"})
        user_id = request.session.get("ID")

        # 验证数据的合法性
        try:
            sku_id = int(sku_id)
        except Exception as e:
            return JsonResponse({"error": 2, "msg": "数据不合法！"})

        # 验证商品是否存在
        try:
            sku_goods = GoodsSKU.objects.get(pk=sku_id)
        except GoodsSKU.DoesNotExist:
            return JsonResponse({"error": 3, "msg": "商品不存在"})

        # 保存到redis 配置成功
        # 连接到redis
        cnn = get_redis_connection("default")  # 选择一个缓存配置
        # 将该用户的商品添加到redis中的购物车
        cart_key = "cart_%s" % user_id
        # 判断购物车中商品的数量是否 大于 1
        count = cnn.hget(cart_key, sku_id)
        # b'1'
        if int(count) > 1:
            cnn.hincrby(cart_key, sku_id, -1)
        else:
            cnn.hdel(cart_key, sku_id)

        # 计算现在购物车中的商品的数量
        cart_total = 0
        vars = cnn.hvals(cart_key)
        if len(vars) > 0:
            for v in vars:
                cart_total += int(v)
        # 添加成功
        return JsonResponse({"error": 0, "msg": "购物车中删除商品数量成功！", "cart_total": cart_total})


# Write a function called two_list_dictionary()
# It accepts two lists of varying lengths
# The 1st list consists of keys
# The 2nd list consists of values
# Returns a dictionary created from the keys and values
# If there are NOT enough values, the remaining keys should have the value of null

def two_list_dictionary(list_keys, list_values):
    """two_list_dictionary(list, list) returns a new dictionary using the passed lists as content."""
    if len(list_keys) != len(list_values):
        temp_list = []
        dictionary = {}

        try:
            for item in range(len(list_keys)):
                temp_list.append(list_values[item])
                dictionary = dict(zip(list_keys, temp_list))
        except IndexError:
            temp_list.append("None")
            dictionary = dict(zip(list_keys, temp_list))
        return dictionary

    return dict(zip(list_keys, list_values))

# Test Code
print(two_list_dictionary(['a', 'b', 'c', 'd'], [1, 2, 3, 4]))
print(two_list_dictionary(['a', 'b', 'c', 'd'], [1, 2, 3]))
print(two_list_dictionary(['x', 'y', 'z'], [1, 2]))


from lamini_index.lamini_index import LaminiIndex
from lamini_index.query_engine import QueryEngine

import argparse

import logging

def main():
    parser = argparse.ArgumentParser("Answer a question using Lamini Index")

    parser.add_argument("--index", type=str, default="models", help="Path to the index")
    parser.add_argument("--question", type=str, required=True, help="Question to answer")

    args = parser.parse_args()

    # Enable logging
    logging.basicConfig(level=logging.INFO)

    index = LaminiIndex.load_index(args.index)

    engine = QueryEngine(index)

    answer = engine.answer_question(args.question)

    print(f"The answer to the question: '{args.question}'")
    print(f"is: '{answer}'")

main()


import sys

def acceptable(genotype):
    return genotype in ('A', 'C', 'G', 'T', 'AA', 'AC', 'AG', 'AT', 'CC', 'CG', 'CT', 'GG', 'GT', 'TT')

def denovo(chrom, father, mother, child):
    if not acceptable(father) or not acceptable(mother) or not acceptable(child):
        return False
    if len(child) == 2:
        for father_allele in father:
            for mother_allele in mother:
                if child == ''.join(sorted([father_allele, mother_allele])):
                    return False
    else:
        if chrom == 'Y':
            for father_allele in father:
                if child == father_allele:
                    return False
        else:
            for mother_allele in mother:
                if child == mother_allele:
                    return False
    return True

def genotypes(file):
    genotypes = {}
    with open(file) as fp:
        for line in fp:
            if not line.startswith('#'):
                rs, chrom, pos, genotype = line.strip().split()
                if acceptable(genotype):
                    genotypes[(rs, chrom, pos)] = genotype
    return genotypes

if __name__ == "__main__":
    father_genotypes = genotypes(sys.argv[1])
    mother_genotypes = genotypes(sys.argv[2])
    with open(sys.argv[3]) as fp:
        print('rsid', 'chromosome', 'position', 'father', 'mother', 'child')
        for line in fp:
            if not line.startswith('#'):
                rs, chrom, pos, child_genotype = line.strip().split()
                key = (rs, chrom, pos)
                if key in father_genotypes and key in mother_genotypes:
                    father_genotype = father_genotypes[key]
                    mother_genotype = mother_genotypes[key]
                    if acceptable(child_genotype) and denovo(chrom, father_genotype, mother_genotype, child_genotype):
                        print(rs, chrom, pos, father_genotypes[key], mother_genotypes[key], child_genotype, sep='\t')


"""Log functions"""
import time
from datetime import datetime, timedelta
from pytz import timezone
import logging

def build_logger(log_file_path: str) -> logging.RootLogger:
    """
    Creates logger file.

    Parameters
    ----------
    log_file_path: str
        Path to save log file

    Returns
    ----------
    logger: logging.RootLogger
        Logger with file path.
    """
    # Reference
    # https://qiita.com/r1wtn/items/d615f19e338cbfbfd4d6
    # https://www.sejuku.net/blog/23149

    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    fh = logging.FileHandler(log_file_path)
    formatter = logging.Formatter(
        fmt="%(asctime)s: %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
    )

    def japanTime(*args):
        return datetime.now(timezone("Asia/Tokyo")).timetuple()

    formatter.converter = japanTime
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    return logger

def get_log_file_name(name: str = "") -> str:
    """
    Creates log file name based on executed time in Japan time.

    Parameters
    ----------
    name: str, optional
        Name of log file

    Returns
    ----------
    filename: str
        Generated log file name.
    """
    now = datetime.now(timezone("Asia/Tokyo"))
    date_time_str = now.strftime("%Y-%m-%d_%H:%M")
    filename = name + date_time_str + ".log"
    return filename

def log_execution_time(logger: logging.RootLogger) -> None:
    """
    Decorator to log function execution time to logger.

    Parameters
    ----------
    logger: logging.RootLogger
        The logger to write execution time for the function.

    Returns
    ----------
    None
    """

    def decorator(function):
        def wrapper(*args, **kwargs):
            start = time.time()
            function(*args, **kwargs)
            elapsed = time.time() - start
            elapsed_time = str(timedelta(seconds=elapsed))
            logger.info(f"Execution time: {elapsed_time}")

        return wrapper

    return decorator


from collections import deque

t = int(input())

for _ in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    arr.sort()
    q = deque(arr)
    k = arr[-1] - arr[0]
    maxendinghere = float('-inf')
    minendinghere = float('inf')
    res = []
    for i in range(n):
        for pos, j in [(q[0], 0), (q[-1], 1)]:
            newmaxendinghere = max(pos, pos + maxendinghere)
            newminendinghere = min(pos, pos + minendinghere)
            if newmaxendinghere < k and newminendinghere > -k:
                res.append(pos)
                if j == 0:
                    q.popleft()
                else:
                    q.pop()
                maxendinghere = newmaxendinghere
                minendinghere = newminendinghere
                break
    if len(res) == n:
        print("Yes")
        print(*res)
    else:
        print("No")


#!/usr/bin/python3

def main():
    number = 600851475143
    print(GreatestPrimeFactor(number))

def GreatestPrimeFactor(number):
    lastFactor = 0
    if number % 2 == 0:
        number = number / 2
        lastFactor = 2
        while number % 2 == 0:
            number /= 2
    else:
        lastFactor = 1

    factor = 3
    while number > 1 and factor * factor <= number:
        if number % factor == 0:
            number /= factor
            lastFactor = factor
            while number % factor == 0:
                number /= factor
        factor += 2

    if number == 1:
        return lastFactor
    else: return number

if __name__ == "__main__":main()


import pygame
from pygame.locals import *

#debug = False
debug = True
numberOfFlowers = 4
flowersSeparation = 210
characterBaseX = 67
characterBaseY = 550
flowersBaseX = 167
flowersBaseY = 530

speedFactor = 1.0

imagedb = { }

def loadImages() :

    # Background
    bg      = pygame.image.load("assets/bg.png")
    imagedb["bg"] = bg

    # Indicators
    indicators = pygame.image.load("assets/indicators.png")
    imagedb["indicator"] = {}
    imagedb["indicator"]["life"] = []
    imagedb["indicator"]["combo"] = []
    for i in range(4) :
        imagedb["indicator"]["life"].append(getSprite(indicators, i, 0, 87, 87))
        imagedb["indicator"]["combo"].append(getSprite(indicators, i, 2, 87, 87))

    # Tuto stuff
    tutostuff = pygame.image.load("assets/tuto.png")
    imagedb["tuto"] = {}
    imagedb["tuto"]["leftkey"]  = []
    imagedb["tuto"]["rightkey"] = []
    imagedb["tuto"]["Dkey"]     = []
    imagedb["tuto"]["Fkey"]     = []
    imagedb["tuto"]["poof"]     = []
    for i in range(2) :
        imagedb["tuto"]["rightkey"].append(getSprite(tutostuff, i, 0, 175, 175))
        imagedb["tuto"]["leftkey"] .append(getSprite(tutostuff, i, 1, 175, 175))
        imagedb["tuto"]["Dkey"]    .append(getSprite(tutostuff, i, 2, 175, 175))
        imagedb["tuto"]["Fkey"]    .append(getSprite(tutostuff, i, 3, 175, 175))
        imagedb["tuto"]["poof"]    .append(getSprite(tutostuff, i, 4, 175, 175))

    # Title
    title = pygame.image.load("assets/title.png")
    imagedb["title"] = []
    imagedb["title"].append(getSprite(title, 0, 0, 610, 250))
    imagedb["title"].append(getSprite(title, 0, 1, 610, 250))

    # Game over stuff
    gameover = pygame.image.load("assets/gameover.png")
    imagedb["gameoverscorebg"] = getSprite(gameover, 0, 0, 400, 250)
    imagedb["again"]           = getSprite(gameover, 1, 0, 400, 250)

    # Stems
    stems =  pygame.image.load("assets/stems.png")
    imagedb["stems"] = [[],[]]
    for i in range(4) :
        imagedb["stems"][0].append(getSprite(stems, i, 0, 140, 54))
        imagedb["stems"][1].append(getSprite(stems, i, 1, 140, 54))

    # Flower sprites
    flowersHead = pygame.image.load("assets/flowersHead.png")
    imagedb["flowersHead"] = {}
    imagedb["flowersHead"]["neutral"] = []
    imagedb["flowersHead"]["good"] = []
    imagedb["flowersHead"]["evil"] = []
    for i in range(3) :
        imagedb["flowersHead"]["good"]   .append(getSprite(flowersHead, i, 0, 175, 175))
        imagedb["flowersHead"]["evil"]   .append(getSprite(flowersHead, i, 1, 175, 175))
        imagedb["flowersHead"]["neutral"].append(getSprite(flowersHead, i, 2, 175, 175))

    imagedb["flowersDead"] = {}
    imagedb["flowersDead"]["good"] = getSprite(flowersHead, 3, 0, 175, 175)
    imagedb["flowersDead"]["evil"] = getSprite(flowersHead, 3, 1, 175, 175)

    flowersPop = pygame.image.load("assets/flowersPop.png")
    imagedb["flowersPop"] = {}
    imagedb["flowersPop"]["good"] = []
    imagedb["flowersPop"]["evil"] = []
    for i in range(4) :
        imagedb["flowersPop"]["good"].append(getSprite(flowersPop, i, 0, 250, 250))
        imagedb["flowersPop"]["evil"].append(getSprite(flowersPop, i, 1, 250, 250))

    # Character sprites
    #char = pygame.image.load("assets/perso.png")
    #imagedb["char"] = {}
    #imagedb["char"]["standing"] = getSprite(char, 0, 0, 48, 64)
    #imagedb["char"]["left"]  = []
    #imagedb["char"]["right"] = []

    #for i in range(4) :
    #    imagedb["char"]["left"].append(getSprite(char, i, 1, 48, 64))
    #    imagedb["char"]["right"].append(getSprite(char, i, 2, 48, 64))

    # Cut animation
    persoActions = pygame.image.load("assets/persoActions.png")
    imagedb["actions"] = {}
    imagedb["actions"]["movingleft"]  = []
    imagedb["actions"]["movingright"] = []
    imagedb["actions"]["idle"]        = []
    imagedb["actions"]["cutting"]     = []
    imagedb["actions"]["watering"]    = []
    imagedb["actions"]["gethit"]      = []

    for i in range(4) :
        imagedb["actions"]["movingleft"] .append(getSprite(persoActions, i, 0, 225, 225))
        imagedb["actions"]["movingright"].append(getSprite(persoActions, i, 1, 225, 225))

    for i in range(2) :
        imagedb["actions"]["idle"]    .append(getSprite(persoActions, i, 2, 225, 225))
        imagedb["actions"]["cutting"] .append(getSprite(persoActions, i, 3, 225, 225))
        imagedb["actions"]["watering"].append(getSprite(persoActions, i, 4, 225, 225))
        imagedb["actions"]["gethit"]  .append(getSprite(persoActions, i, 5, 225, 225))

def getSprite(img, x, y, w=32, h=32) :

    return img.subsurface((x*w, y*h, w, h))


def gorjetaLegislativa():
    """Calcula a gorjeta legislativa de um garçom dado o valor da conta"""
    conta = float(input('Digite o valor da conta: R$ '))
    gorjeta = float(input('Digite o valor da gorjeta em % (Ex: 10%): '))

    gorjetaGarcom = conta * (gorjeta / 100)

    return print(f'Valor da conta R${conta}\n'
                 f'Valor da gorjeta R${gorjetaGarcom}')

gorjetaLegislativa()


from wsgiref.simple_server import make_server

# python fwsgi_3.py
# Заходим в браузер: http://127.0.0.1:8000/
#                    http://127.0.0.1:8000/abc/

def application(environ, start_response):
    """
    00:47
    simple PC: маршрутизатор
    :param environ: словарь данных от сервера
    :param start_response: функция для ответа серверу
    """
    # print(type(environ))  # <class 'dict'>
    # print(environ)
    path = environ['PATH_INFO']  # 'PATH_INFO': '/'
    if path == '/':
        start_response('200 OK', [('Content-Type', 'text/html')])
        return [b'Index']
    elif path == '/abc/':
        start_response('200 OK', [('Content-Type', 'text/html')])
        return [b'ABC']
    else:
        start_response('404 NOT FOUND', [('Content-Type', 'text/html')])
        return [b'404 Not Found']

with make_server('', 8000, application) as httpd:
    """Запуск непосредственно из PyCharm"""
    print("Serving on port 8000...")
    httpd.serve_forever()


def solution(word):
    # 각 알파벳마다 수 부여
    wordN = {'A':0,'E':1,'I':2,'O':3,'U':4}
    # 문자열로 자르기
    word = list(word)
    # maximum = 맥스값(3125)
    maximum = 5**5
    answer = 0
    # 문자열 규칙 = word숫자 * maximum(경우의 수) 나누기 4의 몫
    for i in word:
        answer += wordN[i]*(maximum-1)//4
        # 한자리수 이동마다 나누기5(경우의 수)
        maximum //= 5
    # 각자리수마다 걸리는 시간만큼 더해주기
    answer += len(word)
    return answer


# Download the raw data of dropsondes from https://www.aoml.noaa.gov/ftp/hrd/data/dropsonde/
import libs.banner
import os
import requests
from bs4 import BeautifulSoup
import re
import libs.tools as tools
import sys
from datetime import datetime

url = "https://www.aoml.noaa.gov/ftp/hrd/data/dropsonde/"

if __name__ == "__main__":
    # Record log
    sys.stdout = tools.Logger(
        "logs/download_raw_" + datetime.now().strftime(r"%Y_%m_%d_%H_%M_%S") + ".log")

    # Get download urls
    response = requests.get(url)
    if response.status_code != 200:
        print(f"{response.status_code}: Failed to obtain {url}")
        exit(-1)
    soup = BeautifulSoup(response.text, "html.parser")
    response.close()
    ul_tag = soup.ul
    li_tags = ul_tag.find_all('li')
    li_tags.pop(0)
    li_urls = [li_tag.a["href"] for li_tag in li_tags]
    raw_urls = [url + li_url + "raw/" for li_url in li_urls]
    save_paths = ["./data/sondes/raw" + li_url for li_url in li_urls]

    for i in range(75, len(raw_urls)):
        print("========================>")
        print(f"Downloading folder {i+1}")
        response = requests.get(raw_urls[i])
        if response.status_code != 200:
            print(
                f"{response.status_code}: Failed to obtain urls from {raw_urls[i]}")
            print("<========================")
            continue
        soup = BeautifulSoup(response.text, "html.parser")
        response.close()
        ul_tag = soup.ul
        li_tags = ul_tag.find_all('li')
        li_tags.pop(0)
        li_urls = [li_tag.a["href"] for li_tag in li_tags]
        # Only get *avp.tar.gz or *.avp.tgz package
        pat1 = r'.*avp\.tar\.gz$'
        pat2 = r'.*AVP\.tar\.gz$'
        pat3 = r'.*avp\.tgz$'
        pat4 = r'.*AVP\.tgz$'
        li_urls = [li_url for li_url in li_urls if re.search(
            pat1, li_url) or re.search(pat2, li_url) or re.search(pat3, li_url) or re.search(pat4, li_url)]
        if len(li_urls) == 0:
            print(f"{response.status_code}: Got nothing from {raw_urls[i]}")
            print("<========================")
            continue
        package_urls = [raw_urls[i] + li_url for li_url in li_urls]
        if not os.path.isdir(save_paths[i]):
            os.mkdir(save_paths[i])
        for url in package_urls:
            tools.download_package(url, save_paths[i])
        print("<========================")


import argparse
import os
from string import Template

import torch
from huggingface_hub import snapshot_download
from torch import nn
from transformers import AutoModelForSequenceClassification, AutoTokenizer

parser = argparse.ArgumentParser()

parser.add_argument("--device", type=int, default=0)
args = parser.parse_args()

model_name = "RM_model_onnx"
device = torch.device('cuda:7')

RM_model_path = "/search/ai/kaitongyang/RLHF_DEBUG/RM/summarization_reward_model/checkpoint-58/"
RM_model_path = '/search/ai/pretrain_models/roberta-base-finetuned-jd-binary-chinese'
RM_tokenizer = AutoTokenizer.from_pretrained(RM_model_path)
RM_model = AutoModelForSequenceClassification.from_pretrained(RM_model_path, torchscript=True)
RM_model.to(device)
query_text = '什么人不能喝三七粉'
response_text = '服用三七粉期间,孕妇和儿童不宜使用。 三七粉是处方药,不是药品。 过量服用会引起中毒。'
input = RM_tokenizer(query_text, response_text, truncation=True, max_length=512, padding="max_length", return_tensors="pt").to(device)
print(input)
print(RM_model(input['input_ids'], input['attention_mask'], input['token_type_ids']))
RM_model = RM_model.eval()  # 转换为eval模式
inputs = (input['input_ids'], input['attention_mask'], input['token_type_ids'])  # 模型测试输入数据
os.makedirs(f"model_store/{model_name}/1", exist_ok=True)
torch.onnx.export(
	RM_model,
	inputs,
	f"model_store/{model_name}/1/model.onnx",  # 输出模型文件名
	input_names=['input_ids', 'attention_mask', 'token_type_ids'],  # 输入节点名，每一个名称对应一个输入名
    output_names=['output'],  # 输出节点名，每一个名称对应一个输出名
	opset_version=11,
	dynamic_axes={'input_ids': {0: 'B', 1: 'C'}, 'attention_mask': {0: 'B', 1: 'C'}, 'token_type_ids': {0: 'B', 1: 'C'}}  # 声明动态维度，默认输入维度固定，可以声明为可变维度（用字符串占位符表示）
)

# traced_script_module.save(f"model_store/{model_name}/1/traced-model.pt")


from data import verbal_const

from utils.logic.functions import l_scale

from devices.factories.luxmeters.base_luxmeter import BaseLuxmeter

#region File Attributes

__author__ = "Orlin Dimitrov"
"""Author of the file."""

__copyright__ = "Copyright 2020, POLYGON Team Ltd."
"""Copyrighter
@see http://polygonteam.com/"""

__credits__ = ["Angel Boyarov"]
"""Credits"""

__license__ = "GPLv3"
"""License
@see http://www.gnu.org/licenses/"""

__version__ = "1.0.0"
"""Version of the file."""

__maintainer__ = "Orlin Dimitrov"
"""Name of the maintainer."""

__email__ = "or.dimitrov@polygonteam.com"
"""E-mail of the author.
@see or.dimitrov@polygonteam.com"""

__status__ = "Debug"
"""File status."""

#endregion

class LightSensor(BaseLuxmeter):
    """Light sensor."""

#region Attributes

    __analog_input = verbal_const.OFF
    """Sensor analog input.
    """

    __state = {}
    """Value from the input.
    """

    __min_measured = 0.0
    """Minimum value of the sensor.
    """

    __max_measured = 15000.0
    """Maximum of te sensor.
    """

#endregion

    def __init__(self, **config):

        super().__init__(config)

        self._vendor = "POLYGONTeam"

        self._model = "MI"

        self.__analog_input = self._config["analog_input"]

#region Public Methods

    def update(self):
        """Update sensor data.
        """

        self.__state = self._controller.analog_read(self.__analog_input)

    def get_value(self):
        """Get value.
        """

        raw = 0.0

        if bool(self.__state):
            raw = l_scale(self.__state["value"],
                          [self.__state["min"], self.__state["max"]],
                          [self.__min_measured, self.__max_measured])

        return raw

#endregion


'''
Methods for working with WordNet triple dataset
'''

import codecs

Hypernym = 0
Hyponym = 1
Meronym = 2
Holonym = 3

Classes = [
    Hypernym,
    Hyponym,
    Meronym,
    Holonym
]

def load(f):
    dataset = []
    with codecs.open(f, 'r', 'utf-8') as stream:
        for line in stream:
            (_id, src, snk, lbl) = [s.strip() for s in line.split('\t')]
            _id, lbl = int(_id), int(lbl)
            dataset.append( (_id, src, snk, lbl) )
    return dataset

def write(data, f):
    with codecs.open(f, 'w', 'utf-8') as stream:
        for (_id, src, snk, lbl) in data:
            stream.write('%s\t%s\t%s\t%d\n' % (
                _id,
                src,
                snk,
                lbl
            ))


# As for one-dimensional signals, images also can be filtered with various low-pass filters (LPF), high-pass filters (HPF), etc.
# A LPF helps in removing noise, or blurring the image.
# A HPF filters helps in finding edges in an image.

# OpenCV provides a function, cv2.filter2D(), to convolve a kernel with an image.
# As an example, we will try an averaging filter on an image. A 5x5 averaging filter kernel can be defined.
# Filtering with the above kernel results in the following being performed:
#   for each pixel, a 5x5 window is centered on this pixel,
#   all pixels falling within this window are summed up, and the result is then divided by 25.
#   This equates to computing the average of the pixel values inside that window.
#   This operation is performed for all the pixels in the image to produce the output filtered image.

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('Messi.jpg')

kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)

plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()


import cv2
import numpy as np

def callback(x):
    pass

cv2.namedWindow('Image')

ref_point = [(0, 800)]
idx = 0

# Mouse Events
def shape_selection(event, x, y, flags, param):
    global ref_point, idx, drawing, point1, point2
    if event == cv2.EVENT_LBUTTONDOWN:
        ref_point = [(x, y)]
        print (ref_point)
        print (image[ref_point[0][1], ref_point[0][0]])

        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

        dlow = 0
        dup = 179

        lower_color = np.array([dlow, 60, 120])
        upper_color = np.array([dup, 255, 255])

        mask = cv2.inRange(hsv, lower_color, upper_color)
        # cv2.imshow('Mask', mask)

        contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

        for contour in contours:
            if (cv2.pointPolygonTest(contour, ref_point[0], False)) == 1:
                print("point is inside contour")
                cv2.drawContours(image, contour, -1, (0, 255, 0), 3)

                # Centre Calculation
                M = cv2.moments(contour)
                cx = int(M['m10'] / M['m00'])
                cy = int(M['m01'] / M['m00'])
                centre = (cx, cy)
                print(centre)

                # Saving Image
                a, b, w, h = cv2.boundingRect(contour)
                print("abc", a,b, w, h)
                if w > 50 and h > 50:
                    idx += 1
                    new_img = image_1[b:b + h, a:a + w]
                    cv2.imwrite(str(idx) + '.png', new_img)

        cv2.imshow("Image", image)

cv2.setMouseCallback("Image", shape_selection)

# Image Reading
image = cv2.imread("Pics/leaves2.jpg")
#blurred_image = cv2.GaussianBlur(image, (5, 5), 1)
image_1= cv2.imread("Pics/leaves2.jpg")

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

print(image.shape)

cv2.imshow("Image", image)

cv2.waitKey(0)
cv2.destroyAllWindows()


# !/usr/bin/python

import pandas as pd
import numpy as np

RSS = pd.read_csv('Fit_1B_2species_2D_rawFitData_FixedAmp.csv')

df=pd.DataFrame(RSS)

#df['RSS']=((df[df.columns[1]]-df[df.columns[2]])**2)

#Create empty data frame
pd.DataFrame()
results=pd.DataFrame()

#Calculate RSS for each fit in the raw data file
for i in range (1,210,2):			#Adjust to number of files
	df['%s%s' % ('RSS', i)]=((df[df.columns[i]]-df[df.columns[i+1]])**2)
	#sum_row=df['%s%s' % ('RSS', i)].sum()
	#results.append(df['%s%s' % ('RSS', i)].sum())

#df.to_csv('RSS_results.csv')
df.to_csv('RSS_output.csv')

quit()


import time
from datetime import datetime as dt
temp_path='hosts'
hosts_path='\/etc\/hosts'
redirect='127.0.0.1'
websites=['www.reddit.com','www.facebook.com','www.pornhub.com','www.xhamster.com','www.myfreecams.com']
cy = dt.now().year
cm = dt.now().month
cd = dt.now().day

while True:
    if dt(cy,cm,cd,8) < dt.now() < dt(cy,cm,cd,12):
        print('test')
        with open(temp_path,'r+') as f:
            content = f.read()
            for website in websites:
                if website in content:
                    pass
                else:
                    f.write(redirect + ' ' + website + '\n')
    else:
        with open(temp_path, 'r+') as f:
            content = f.readlines()
            f.seek(0)
            for ln in content:
                if not any(website in ln for website in websites):
                    f.write(ln)
            f.truncate()

    time.sleep(5)


import asyncio
import functools

import discord
from discord.ext import commands

class PaginationError(Exception):
    pass

class Paginator:
    __slots__ = (
        "ctx",
        "bot",
        "user",
        "channel",
        "msg",
        "execute",
        "embed",
        "max_pages",
        "entries",
        "paginating",
        "current",
        "reactions",
    )

    def __init__(self, ctx):
        self.ctx = ctx
        self.bot = ctx.bot
        self.user = ctx.author
        self.channel = ctx.channel
        self.msg = ctx.message

        self.execute = None
        self.entries = []
        self.embed = None
        self.max_pages = None
        self.paginating = True
        self.current = 0

        part = functools.partial(self.stop, delete=True)
        part.__doc__ = self.stop.__doc__

        self.reactions = [
            ("\N{BLACK LEFT-POINTING TRIANGLE}", self.backward),
            ("\N{BLACK RIGHT-POINTING TRIANGLE}", self.forward),
            ("\N{BLACK SQUARE FOR STOP}", part),
            ("\N{INFORMATION SOURCE}", self.info),
        ]

    def add_entry(self, entry):
        self.entries.append(entry)

    PAGINATION_PERMS = frozenset({"add_reaction", "embed_links", "read_message_history"})

    async def setup(self):
        perms = self.channel.permissions_for(self.ctx.me)
        missing = [perm for perm, value in perms
                   if perm in self.PAGINATION_PERMS and not value]
        if missing:
            raise commands.BotMissingPermissions(missing)

        if not self.entries:
            e = PaginationError("No pagination entries.")
            raise commands.CommandInvokeError(e) from e

        if not self.embed:
            self.entries = [entry + f"\n\nPage {page} of {len(self.entries)}"
                            for page, entry in enumerate(self.entries, 1)]
            try:
                self.msg = await self.channel.send(self.entries[0])
            except AttributeError:
                await self.channel.send(self.entries)
        else:
            for page, embed in enumerate(self.entries, 1):
                embed.set_author(name=f"Page {page} of {len(self.entries)}")
            try:
                self.msg = await self.channel.send(embed=self.entries[0])
            except (AttributeError, TypeError):
                await self.channel.send(embed=self.entries)

        if len(self.entries) == 1:
            return

        self.max_pages = len(self.entries) - 1

        for (r, _) in self.reactions:
            await self.msg.add_reaction(r)

    async def alter(self, page: int):
        try:
            await self.msg.edit(embed=self.entries[page])
        except (AttributeError, TypeError):
            await self.msg.edit(content=self.entries[page])

    async def backward(self):
        """takes you to the previous page or the last if used on the first one."""
        if self.current == 0:
            self.current = self.max_pages
            await self.alter(self.current)
        else:
            self.current -= 1
            await self.alter(self.current)

    async def forward(self):
        """takes you to the next page or the first if used on the last one."""
        if self.current == self.max_pages:
            self.current = 0
            await self.alter(self.current)
        else:
            self.current += 1
            await self.alter(self.current)

    async def stop(self, *, delete=False):
        """stops the paginator session."""
        try:
            if delete:
                await self.msg.delete()
            else:
                await self.msg.clear_reactions()
        except discord.HTTPException:
            pass
        finally:
            self.paginating = False

    async def info(self):
        """shows this page."""
        fmt = []

        for emoji, func in self.reactions:
            fmt.append(f"{emoji} {func.__doc__}")

        if not self.embed:
            fmt.insert(0, "**Instructions**\n"
                          "This is a reaction paginator, when you react to one of the buttons below the message gets"
                          " edited. Below you will find what each does.\n\n"
                          "**Reactions**")
            return await self.msg.edit(content="\n".join(fmt))

        embed = discord.Embed(color=discord.Color(0x008CFF))

        embed.set_author(name="Instructions")

        embed.description = (
            "This is a reaction paginator, when you react to one of the buttons below "
            "the message gets edited. Below you will find what each does."
        )

        embed.add_field(name="Reactions", value="\n".join(fmt))

        await self.msg.edit(embed=embed)

    def check(self, reaction, user):
        if not user.id == self.user.id:
            return False

        if not reaction.message.id == self.msg.id:
            return False

        for (emoji, func) in self.reactions:
            if reaction.emoji == emoji:
                self.execute = func
                return True
        return False

    async def paginate(self, *, embed: bool = True):
        self.embed = embed

        await self.setup()

        while self.paginating:
            done, pending = await asyncio.wait(
                [
                    self.bot.wait_for("reaction_add", check=self.check),
                    self.bot.wait_for("reaction_remove", check=self.check),
                ],
                return_when=asyncio.FIRST_COMPLETED, timeout=120
            )

            try:
                done.pop().result()
            except (discord.HTTPException, asyncio.TimeoutError, KeyError):
                return await self.stop()

            for future in pending:
                future.cancel()

            await self.execute()

class Tabulator:
    __slots__ = ("_widths", "_columns", "_rows")

    def __init__(self):
        self._widths = []
        self._columns = []
        self._rows = []

    def set_columns(self, columns):
        self._columns = columns
        self._widths = [len(c) + 2 for c in columns]

    def add_row(self, row):
        rows = [str(r) for r in row]
        self._rows.append(rows)
        for index, element in enumerate(rows):
            width = len(element) + 2
            if width > self._widths[index]:
                self._widths[index] = width

    def add_rows(self, rows):
        for row in rows:
            self.add_row(row)

    def render(self):
        sep = "╬".join("═" * w for w in self._widths)
        sep = f"╬{sep}╬"

        to_draw = [sep]

        def get_entry(d):
            elem = "║".join(f"{e:^{self._widths[i]}}" for i, e in enumerate(d))
            return f"║{elem}║"

        to_draw.append(get_entry(self._columns))
        to_draw.append(sep)

        for row in self._rows:
            to_draw.append(get_entry(row))

        to_draw.append(sep)
        return "\n".join(to_draw)

class Plural:
    def __init__(self, value):
        self.value = value

    def __format__(self, format_spec):
        v = self.value
        singular, _, pl = format_spec.partition('|')
        pl = pl or f"{singular}s"
        if not abs(v) == 1:
            return f"{v} {pl}"
        return f"{v} {singular}"


from pyspark.sql import SparkSession, SQLContext
from pyecharts import options as opts
from pyecharts.charts import Pie
import os

os.environ['JAVA_HOME'] = "/usr/local/java/jdk1.8"

spark = SparkSession.builder.getOrCreate()
df = spark.read.format("jdbc") \
    .option("driver", "com.mysql.cj.jdbc.Driver") \
    .option("url", "jdbc:mysql://plane.mysql.database.azure.com:3306/weibo") \
    .option("user", "mykaneki") \
    .option("dbtable", "weibo_new") \
    .option("password", "azureLinux668CMY...").load()

load_dict = df.select('text', 'attitudes_count', 'comments_count').take(1000)

dic_attitudes = {}
dic_comments = {}
for dic in load_dict:
    title = dic['text']
    attitudes_count = dic['attitudes_count']
    comments_count = dic['comments_count']
    dic_attitudes[title] = attitudes_count
    dic_comments[title] = comments_count

d_attitudes = sorted(dic_attitudes.items(), key=lambda d: int(d[1]), reverse=True)
d_comments = sorted(dic_comments.items(), key=lambda d: int(d[1]), reverse=True)

columns_attitudes = [i[0] for i in d_attitudes[0:5]]
columns_comments = [i[0] for i in d_comments[0:5]]
data_attitudes = [i[1] for i in d_attitudes[0:5]]
data_comments = [i[1] for i in d_comments[0:5]]

c = (
    Pie(init_opts=opts.InitOpts(width="1440px", height="300px"))
    .add(
        "",
        [
            list(z)
            for z in zip(
            columns_attitudes,
            data_attitudes,
        )
        ],
        center=["40%", "50%"],

    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="top5点赞帖子饼图"),
        legend_opts=opts.LegendOpts(type_="scroll", pos_left="60%", orient="vertical"),
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    .render("top5_attitudes.html")
)

d = (
    Pie(init_opts=opts.InitOpts(width="1440px", height="300px"))
    .add(
        "",
        [
            list(z)
            for z in zip(
            columns_comments,
            data_comments,
        )
        ],
        center=["40%", "50%"],

    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="top5评论帖子饼图"),
        legend_opts=opts.LegendOpts(type_="scroll", pos_left="60%", orient="vertical"),
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    .render("top5_comments.html")
)


import sys
# sys.path.append() # we could add extra places for python to look

entry = input('Enter name and age ') # assume a comma
fn, age = entry.split(', ')
if not fn.isalpha():
    print(f'{fn} needs to be alphabetic')
    sys.exit(-1)
if not age.isdigit():
    print(f'{age} needs to be numeric')
    sys.exit(-1)
print(fn, age)


"""
URL configuration for investments project - portfolio app.
"""
from django.urls import path, include, re_path
from django.views.static import serve
from django.conf import settings
from portfolio import views

urlpatterns = [
    path('', views.customer_list),
    path('api/customers/', views.customer_list),
    path('api/mycustomers/', views.my_customer_list),
    path('api/customers/<int:pk>', views.getCustomer),
    path('api/investments/', views.investment_list),
    path('api/investments/<int:pk>', views.getInvestment),
    path('api/stocks/', views.stock_list),
    path('api/stocks/<int:pk>', views.getStock),
    re_path(r'^media/(?P<path>.*)$', serve, {'document_root':
settings.MEDIA_ROOT}), #serve media files when deployed
    re_path(r'^static/(?P<path>.*)$', serve, {'document_root':
settings.STATIC_ROOT}), #serve static files when deployed
]


import asyncio
import collections
import logging
import os
import sys
from functools import wraps
from importlib import import_module
from pathlib import Path
from typing import Callable

from watchfiles import Change, awatch

logger = logging.getLogger('cogwatch')
logger.addHandler(logging.NullHandler())
# prevents log events bubbling up to the parent and duplicating output
logger.propagate = False

# We try to import the first available library that is supported. Note: the
# library name does not neccessarily match the name of the module. For example:
#
# - 'discord.py' is imported as 'discord'.
# - 'nextcord' is imported as 'nextcord'.
# - 'disnake' is imported as 'disnake'.
# - 'py-cord' is imported as 'discord'.
# - 'discord.py-message-components' (discord4py) is imported as 'discord'.
#
# If you are adding support for a library, please ensure that the library name
# works for both the commands extension and the base library.
supported_libraries = ['discord', 'nextcord', 'disnake']

for library_name in supported_libraries:
    try:
        library = import_module(library_name)
        commands = import_module(f'{library_name}.ext.commands')
    except ImportError:
        logger.debug(f'Could not find {library_name} library, passing...')
        pass
    except Exception as e:
        logger.error(
            f'Failed to import {library_name} library. Please report this error here: https://github.com/robertwayne/cogwatch/issues'
        )
        raise e
    else:
        logger.info(f'Found {library_name}.')
        globals()[library_name] = library
        globals()[f'{library_name}.ext.commands'] = commands

        break
else:
    raise ImportError(
        'Could not find discord.py or another supported library, please install one of the following:\n'
        + '\n'.join(supported_libraries)
    )

# We need to alias cog exceptions, as 'pycord' moves them to the base library
# module whereas they are a part of the commands.ext module in other libraries.
#
# I'm not sure if there is a cleaner way to structure this, as it must be set
# from a dynamically loaded library. It works for now, though.
ExtensionNotLoaded = None
NoEntryPointError = None
ExtensionFailed = None
ExtensionNotFound = None
ExtensionAlreadyLoaded = None
ExtensionError = None

try:
    ExtensionNotLoaded = commands.ExtensionNotLoaded
    NoEntryPointError = commands.NoEntryPointError
    ExtensionFailed = commands.ExtensionFailed
    ExtensionNotFound = commands.ExtensionNotFound
    ExtensionAlreadyLoaded = commands.ExtensionAlreadyLoaded
    ExtensionError = commands.ExtensionError
except AttributeError:
    ExtensionNotLoaded = library.ExtensionNotLoaded
    NoEntryPointError = library.NoEntryPointError
    ExtensionFailed = library.ExtensionFailed
    ExtensionNotFound = library.ExtensionNotFound
    ExtensionAlreadyLoaded = library.ExtensionAlreadyLoaded
    ExtensionError = library.ExtensionError

class Watcher:
    """The core cogwatch class -- responsible for starting up watchers and managing cogs.

    Attributes
        :client: A Bot client.
        :path: Root name of the cogs directory; cogwatch will only watch within
               this directory -- recursively.
        :debug: Whether to run the bot only when the debug flag is True.
                Defaults to True.
        :loop: Custom event loop. If not specified, will use the current running
               event loop.
        :default_logger: Whether to use the default logger (to sys.stdout) or
                         not. Defaults to True.
        :preload: Whether to detect and load all found cogs on startup. Defaults
                  to False.
        :colors: Whether to use colorized terminal outputs or not. Defaults to
                 True.
    """

    def __init__(
        self,
        client: commands.Bot,
        path: str = 'commands',
        debug: bool = True,
        loop: asyncio.BaseEventLoop = None,
        default_logger: bool = True,
        preload: bool = False,
        colors: bool = True,
    ):
        self.client = client
        self.path = path
        self.debug = debug
        self.loop = loop
        self.default_logger = default_logger
        self.preload = preload
        self.colors = colors

        if self.colors:
            self.CEND = '\33[0m'
            self.CBOLD = '\33[1m'
            self.CGREEN = '\33[32m'
            self.CRED = '\33[31m'
        else:
            self.CEND, self.CBOLD, self.CGREEN, self.CRED = '', '', '', ''

        if default_logger:
            watch_log = logging.getLogger('cogwatch')
            watch_log.setLevel(logging.INFO)
            watch_handler = logging.StreamHandler(sys.stdout)
            watch_handler.setFormatter(logging.Formatter('[%(name)s] %(message)s'))
            watch_log.addHandler(watch_handler)

    @staticmethod
    def get_cog_name(path: str) -> str:
        """Returns the cog file name without .py appended to it."""
        _path = os.path.normpath(path)
        return _path.split(os.sep)[-1:][0][:-3]

    def get_dotted_cog_path(self, path: str) -> str:
        """Returns the full dotted path that discord.py uses to load cog files."""
        _path = os.path.normpath(path)
        tokens = _path.split(os.sep)
        reversed_tokens = list(reversed(tokens))

        # iterate over the list backwards in order to get the first occurrence in cases where a duplicate
        # name exists in the path (ie. example_proj/example_proj/commands)
        try:
            root_index = reversed_tokens.index(self.path.split('/')[0]) + 1
        except ValueError:
            raise ValueError('Use forward-slash delimiter in your `path` parameter.')

        return '.'.join([token for token in tokens[-root_index:-1]])

    async def _start(self):
        """Starts a watcher, monitoring for any file changes and dispatching event-related methods appropriately."""
        while self.dir_exists():
            try:
                async for changes in awatch(Path.cwd() / self.path):
                    self.validate_dir()

                    reverse_ordered_changes = sorted(changes, reverse=True)

                    for change in reverse_ordered_changes:
                        change_type = change[0]
                        change_path = change[1]

                        filename = self.get_cog_name(change_path)

                        new_dir = self.get_dotted_cog_path(change_path)
                        cog_dir = f'{new_dir}.{filename}' if new_dir else f'{self.path}.{filename}'

                        if change_type == Change.deleted:
                            if cog_dir in self.client.extensions:
                                await self.unload(cog_dir)
                        elif change_type == Change.added:
                            if cog_dir not in self.client.extensions:
                                await self.load(cog_dir)
                        elif change_type == Change.modified and change_type != (Change.added or Change.deleted):
                            if cog_dir in self.client.extensions:
                                await self.reload(cog_dir)
                            else:
                                await self.load(cog_dir)

            except FileNotFoundError:
                continue

            else:
                await asyncio.sleep(1)

        else:
            await self.start()

    def check_debug(self):
        """Determines if the watcher should be added to the event loop based on debug flags."""
        return any([(self.debug and __debug__), not self.debug])

    def dir_exists(self):
        """Predicate method for checking whether the specified dir exists."""
        return Path(Path.cwd() / self.path).exists()

    def validate_dir(self):
        """Method for raising a FileNotFound error when the specified directory does not exist."""
        if not self.dir_exists():
            raise FileNotFoundError
        return True

    async def start(self):
        """Checks for a user-specified event loop to start on, otherwise uses current running loop."""
        _check = False
        while not self.dir_exists():
            if not _check:
                logger.error(f'The path {self.CBOLD}{Path.cwd() / self.path}{self.CEND} does not exist.')
                _check = True

        else:
            logger.info(f'Found {self.CBOLD}{Path.cwd() / self.path}{self.CEND}!')
            if self.preload:
                await self._preload()

            if self.check_debug():
                if self.loop is None:
                    self.loop = asyncio.get_event_loop()

                logger.info(f'Watching for file changes in {self.CBOLD}{Path.cwd() / self.path}{self.CEND}...')
                self.loop.create_task(self._start())

    async def handle_extension(self, func: Callable, cog_dir: str):
        """Handles the underlying logic for loading, unloading, and reloading
        cogs. Specifically, we need to handle the case where the underlying
        library uses async or sync setup functions.

        discord.py, for example, is async, but (most) of the other libraries are
        sync.
        """
        future = func(cog_dir)

        # We want to explicitly check if the future is an awaitable, as
        # some of the libraries also return a list | dict type instead of
        # None.
        if future and isinstance(future, collections.abc.Awaitable):
            await future

    async def load(self, cog_dir: str):
        """Loads a cog file into the client."""
        try:
            await self.handle_extension(self.client.load_extension, cog_dir)

        except ExtensionAlreadyLoaded:
            logger.info(f'Cannot reload {cog_dir} because it is not loaded.')
        except NoEntryPointError:
            logger.info(
                f'{self.CBOLD}{self.CRED}[Error]{self.CEND} Failed to load {self.CBOLD}{cog_dir}{self.CEND}; no entry point found.'
            )
        except ExtensionNotFound:
            logger.debug(f'Cannot load {cog_dir} because it does not exist or is a folder.')
            pass
        else:
            logger.info(f'{self.CBOLD}{self.CGREEN}[Cog Loaded]{self.CEND} {cog_dir}')

    async def unload(self, cog_dir: str):
        """Unloads a cog file into the client."""
        try:
            await self.handle_extension(self.client.unload_extension, cog_dir)

        except ExtensionNotLoaded:
            logger.info(f'Cannot reload {cog_dir} because it is not loaded.')
        except Exception as exc:
            self.cog_error(exc)
        else:
            logger.info(f'{self.CBOLD}{self.CRED}[Cog Unloaded]{self.CEND} {cog_dir}')

    async def reload(self, cog_dir: str):
        """Attempts to atomically reload the file into the client."""
        try:
            await self.handle_extension(self.client.reload_extension, cog_dir)

        except ExtensionNotLoaded:
            logger.info(
                f'{self.CBOLD}{self.CRED}[Error]{self.CEND} Failed to reload {self.CBOLD}{cog_dir}{self.CEND}; no entry point found.'
            )
        except ExtensionNotLoaded:
            logger.info(f'Cannot reload {cog_dir} because it is not loaded.')
        except Exception as exc:
            self.cog_error(exc)
        else:
            logger.info(f'{self.CBOLD}{self.CGREEN}[Cog Reloaded]{self.CEND} {cog_dir}')

    @staticmethod
    def cog_error(exc: Exception):
        """Logs exceptions. TODO: Need thorough exception handling."""
        if isinstance(exc, (ExtensionError, SyntaxError)):
            logging.exception(exc)

    async def _preload(self):
        logger.info('Preloading cogs...')
        for cog in {(file.stem, file) for file in Path(Path.cwd() / self.path).rglob('*.py')}:
            new_dir = self.get_dotted_cog_path(cog[1])
            await self.load('.'.join([new_dir, cog[0]]))

def watch(**kwargs):
    """Instantiates a watcher by hooking into a Bot client methods' `self` attribute."""

    def decorator(function):
        @wraps(function)
        async def wrapper(client):
            cw = Watcher(client, **kwargs)
            await cw.start()
            ret_val = await function(client)
            return ret_val

        return wrapper

    return decorator


import nltk
import random
import pickle
from nltk.classify import ClassifierI
from statistics import mode
from nltk.tokenize import word_tokenize

documents_f = open("pickle/documents.pickle", "rb")
documents = pickle.load(documents_f)
documents_f.close()

word_features_f = open("pickle/word_features.pickle", "rb")
word_features = pickle.load(word_features_f)
word_features_f.close()

def find_features(document):
    words = word_tokenize(document)
    features = {}
    for w in word_features:
        features[w] = (w in words)

    return features

featuresets_f = open("pickle/featuresets.pickle", "rb")
featuresets = pickle.load(featuresets_f)
featuresets_f.close()

random.shuffle(featuresets)

testing_set = featuresets[10000:]
training_set = featuresets[:10000]

open_file = open("pickle/naivebayes.pickle", "rb")
classifier = pickle.load(open_file)
open_file.close()

def sentiment(text):
	feats=find_features(text)
	return classifier.classify(feats)


# Binary Tree Zigzag Level Order Traversal

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

# BFS
# runtime: faster than 97.93%
class Solution:
    def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return []

        res = []
        cur_level = [root]
        zigzag = True
        while cur_level:
            res.append([node.val for node in cur_level])
            next_level = []

            if zigzag:
                cur_level = cur_level[::-1]
                for node in cur_level:
                    if node.right:
                        next_level.append(node.right)
                    if node.left:
                        next_level.append(node.left)
            else:
                cur_level = cur_level[::-1]
                for node in cur_level:
                    if node.left:
                        next_level.append(node.left)
                    if node.right:
                        next_level.append(node.right)

            zigzag = not(zigzag)
            cur_level = next_level

        return res

# DFS
class Solution2:
    def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return []

        res = []
        self.dfs(root, res, 0)
        for i,a in enumerate(res):
            if i % 2:
                res[i] = res[i][::-1]
        return res

    def dfs(self, node, res, d):
        if not node:
            return
        if d >= len(res):
            res.append([])
        res[d].append(node.val)
        self.dfs(node.left, res, d+1)
        self.dfs(node.right, res, d+1)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    Dummy conftest.py for pub_sub_interface.

    If you don't know what this is for, just leave it empty.
    Read more about conftest.py under:
    https://pytest.org/latest/plugins.html
"""

from algorithms import flood

def pytest_generate_tests(metafunc):
    """
    Customize test functions however needed
    """
    if "algorithm" in metafunc.fixturenames:
        # parameterized algorithms
        algorithms = [
                (flood.Publisher, flood.Subscriber, flood.Router)
        ]
        metafunc.parametrize("algorithm", algorithms)


#-*- coding:utf-8 -*-
from django.shortcuts import render_to_response
from django.http.response import HttpResponse, Http404, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
import hashlib
from xml.etree import ElementTree as etree
from django.utils.encoding import smart_str
import urllib2
import json
from blog.models import Article
from django.db.models.expressions import F
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.views.generic.list import ListView
from django.views.generic.edit import FormView
from forms import ArticlePublishForm
from django.views.generic.detail import DetailView
from django.core.urlresolvers import reverse
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.template.context import RequestContext
from django.db import connection
import urllib
import os

# Create your views here.

def blog(request, classes='all', tags='all'):
#     使用传统方法获取的是一个list?
#     cursor = connection.cursor()
#     cursor.execute("SELECT tags, count(*) AS ct FROM blog_article GROUP BY tags")
#     tags_list = cursor.fetchall()
# 下面这句无法获取到各个tag的数量
    tags_list = Article.objects.values('tags').distinct()
    tags_list_ok = []
    # 使用空格分割tag
    for o in tags_list:
        for p in o['tags'].split():
            tags_list_ok.append(p)
    # 使用set函数去除重复tag
    tags_list_ok = list(set(tags_list_ok))

# 使用一个list传给模板，使其随机选择标签的风格，显示不同的颜色
    tags_class = ["label label-default", "label label-primary", "label label-success", "label label-info", "label label-warning", "label label-danger"]
# 以下这句使用raw的方法来执行传统sql语句
    classes_list = Article.objects.raw("SELECT *, count(*) AS ct FROM blog_article GROUP BY classes")
    if classes == 'all':
        if tags == 'all':
            object_list = Article.objects.all().order_by(F('created').desc())[:100]
        else:
            if 'SERVER_SOFTWARE' in os.environ:
                tags = urllib.unquote(str(tags)).decode('utf8')
            object_list = Article.objects.filter(tags__contains=tags).order_by(F('created').desc())[:100]
    else:
#         将url中的那种码(类似:%E8%A7%86%E5%9B%BE)转回中文
#         判断是BAE环境还是本地开发环境,本地传给视图的还是中文，没有被转成乱码
        if 'SERVER_SOFTWARE' in os.environ:
            classes = urllib.unquote(str(classes)).decode('utf8')
        object_list = Article.objects.filter(classes=classes).order_by(F('created').desc())[:100]
    paginator = Paginator(object_list, 8)
    page = request.GET.get('page')
    try:
        object_list = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        object_list = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        object_list = paginator.page(paginator.num_pages)
#   这里使用context_instance=RequestContext(request)是因为模板中使用了{% if user.is_authenticated %}来判断用户是否登录，需要用到request？
#     return render_to_response('blog_index.html', {'object_list': object_list, 'tags_list': tags_list}, context_instance=RequestContext(request))
    return render_to_response('blog_index.html', locals(), context_instance=RequestContext(request))

#使用tags检索文章的函数，现在修改为直接使用blog函数
# def blog_tags(request, tags):
#     object_list = Article.objects.filter(tags=tags).order_by(F('created').desc())[:100]
#     tags_list = Article.objects.values('tags').distinct()
#     paginator = Paginator(object_list, 3)
#     page = request.GET.get('page')
#     try:
#         object_list = paginator.page(page)
#     except PageNotAnInteger:
#         # If page is not an integer, deliver first page.
#         object_list = paginator.page(1)
#     except EmptyPage:
#         # If page is out of range (e.g. 9999), deliver last page of results.
#         object_list = paginator.page(paginator.num_pages)
#     return render_to_response('blog_index.html', {'object_list': object_list, 'tags_list': tags_list})

# 由于无法解决返回object_list的同时返回一个tagslist，改用普通视图函数blog展示主页
# class ArticleListView(ListView):
#     template_name = 'blog_index.html'
# #     tags_list = Article.objects.values('tags').distinct()
#     context_object_name = 'tags_list'
#
#     def get_queryset(self, **kwargs):
#         object_list = Article.objects.all().order_by(F('created').desc())[:100]
#         paginator = Paginator(object_list, 2)
#         page = self.request.GET.get('page')
#         try:
#             object_list = paginator.page(page)
#         except PageNotAnInteger:
#             # If page is not an integer, deliver first page.
#             object_list = paginator.page(1)
#         except EmptyPage:
#             # If page is out of range (e.g. 9999), deliver last page of results.
#             object_list = paginator.page(paginator.num_pages)
#         return object_list

# class ArticlePublishView(FormView):
class ArticlePublishView(FormView):
    template_name = 'article_publish.html'
    form_class = ArticlePublishForm
    success_url = '/'

    def form_valid(self, form):
        form.save(self.request.user.username)
        return super(ArticlePublishView, self).form_valid(form)

class ArticleDetailView(DetailView):
    template_name = 'article_detail.html'

    def get_object(self, **kwargs):
        id = self.kwargs.get('id')
        try:
            article = Article.objects.get(id=id)
            article.views += 1
            article.save()
            article.tags = article.tags.split()
        except Article.DoesNotExist:
            raise Http404("Article does not exist")
        return article

class ArticleEditView(FormView):
    template_name = 'article_publish.html'
    form_class = ArticlePublishForm
    article = None

    def get_initial(self, **kwargs):
        id = self.kwargs.get('id')
        try:
            self.article = Article.objects.get(id=id)
            initial = {
                'title': self.article.title,
                'content': self.article.content_md,
                'tags': self.article.tags,
                'classes': self.article.classes,
            }
            return initial
        except Article.DoesNotExist:
            raise Http404("Article does not exist")

    def form_valid(self, form):
        form.save(self.request, self.article)
        return super(ArticleEditView, self).form_valid(form)

    def get_success_url(self):
#         id = self.request.POST.get('id')
        id = self.kwargs.get('id')
        success_url = reverse('article_detail', args=(id,))
        return success_url

def articledel(request, id):
    p = Article.objects.get(id=id)
    p.delete()
    return HttpResponseRedirect('/')

def logout(request):
    auth.logout(request)
    return HttpResponseRedirect('/')


import numpy as np
from functools import partial
from menpo.feature import no_op
from menpofit.result import euclidean_bb_normalised_error

from .base import (BaseSupervisedDescentAlgorithm,
                   compute_parametric_delta_x, features_per_patch,
                   update_parametric_estimates, print_parametric_info,
                   build_appearance_model, fit_parametric_shape)
from menpo.model import PCAVectorModel
from menpofit.math import IIRLRegression, IRLRegression, OPPRegression
from menpofit.modelinstance import OrthoPDM
from menpofit.visualize import print_progress

class FullyParametricSDAlgorithm(BaseSupervisedDescentAlgorithm):
    r"""
    """

    def __init__(self, shape_model_cls=OrthoPDM,
                 appearance_model_cls=PCAVectorModel):
        super(FullyParametricSDAlgorithm, self).__init__()
        self.regressors = []
        self.shape_model_cls = shape_model_cls
        self.appearance_model_cls = appearance_model_cls
        self.appearance_model = None
        self.shape_model = None

    def _compute_delta_x(self, gt_shapes, current_shapes):
        # This is called first - so train shape model here
        if self.shape_model is None:
            self.shape_model = self.shape_model_cls(gt_shapes)

        return compute_parametric_delta_x(gt_shapes, current_shapes,
                                          self.shape_model)

    def _update_estimates(self, estimated_delta_x, delta_x, gt_x,
                          current_shapes):
        update_parametric_estimates(estimated_delta_x, delta_x, gt_x,
                                    current_shapes, self.shape_model)

    def _compute_training_features(self, images, gt_shapes, current_shapes,
                                   prefix='', verbose=False):
        if self.appearance_model is None:
            self.appearance_model = build_appearance_model(
                images, gt_shapes, self.patch_shape, self.patch_features,
                self.appearance_model_cls, verbose=verbose, prefix=prefix)

        wrap = partial(print_progress,
                       prefix='{}Extracting patches'.format(prefix),
                       end_with_newline=not prefix, verbose=verbose)

        features = []
        for im, shapes in wrap(zip(images, current_shapes)):
            for s in shapes:
                param_feature = self._compute_test_features(im, s)
                features.append(param_feature)

        return np.vstack(features)

    def _compute_parametric_features(self, patch):
        raise NotImplementedError()

    def _compute_test_features(self, image, current_shape):
        patch_feature = features_per_patch(
            image, current_shape, self.patch_shape, self.patch_features)
        return self._compute_parametric_features(patch_feature)

    def _print_regression_info(self, _, gt_shapes, n_perturbations,
                               delta_x, estimated_delta_x, level_index,
                               prefix=''):
        print_parametric_info(self.shape_model, gt_shapes, n_perturbations,
                              delta_x, estimated_delta_x, level_index,
                              self._compute_error, prefix=prefix)

    def run(self, image, initial_shape, gt_shape=None, **kwargs):
        return fit_parametric_shape(image, initial_shape, self,
                                    gt_shape=gt_shape)

class ParametricAppearanceProjectOut(FullyParametricSDAlgorithm):

    def _compute_parametric_features(self, patch):
        return self.appearance_model.project_out(patch.ravel())

class ParametricAppearanceWeights(FullyParametricSDAlgorithm):

    def _compute_parametric_features(self, patch):
        return self.appearance_model.project(patch.ravel())

class ParametricAppearanceMeanTemplate(FullyParametricSDAlgorithm):

    def _compute_parametric_features(self, patch):
        return patch.ravel() - self.appearance_model.mean().ravel()

class FullyParametricWeightsNewton(ParametricAppearanceWeights):
    r"""
    """

    def __init__(self, patch_features=no_op, patch_shape=(17, 17),
                 n_iterations=3, shape_model_cls=OrthoPDM,
                 appearance_model_cls=PCAVectorModel,
                 compute_error=euclidean_bb_normalised_error,
                 eps=10 ** -5, alpha=0, bias=True):
        super(FullyParametricWeightsNewton, self).__init__(
            shape_model_cls=shape_model_cls,
            appearance_model_cls=appearance_model_cls)

        self._regressor_cls = partial(IRLRegression, alpha=alpha, bias=bias)
        self.patch_shape = patch_shape
        self.patch_features = patch_features
        self.n_iterations = n_iterations
        self._compute_error = compute_error
        self.eps = eps

class FullyParametricMeanTemplateNewton(ParametricAppearanceMeanTemplate):
    r"""
    """

    def __init__(self, patch_features=no_op, patch_shape=(17, 17),
                 n_iterations=3, shape_model_cls=OrthoPDM,
                 appearance_model_cls=PCAVectorModel,
                 compute_error=euclidean_bb_normalised_error,
                 eps=10 ** -5, alpha=0, bias=True):
        super(FullyParametricMeanTemplateNewton, self).__init__(
            shape_model_cls=shape_model_cls,
            appearance_model_cls=appearance_model_cls)

        self._regressor_cls = partial(IRLRegression, alpha=alpha, bias=bias)
        self.patch_shape = patch_shape
        self.patch_features = patch_features
        self.n_iterations = n_iterations
        self._compute_error = compute_error
        self.eps = eps

class FullyParametricProjectOutNewton(ParametricAppearanceProjectOut):
    r"""
    """

    def __init__(self, patch_features=no_op, patch_shape=(17, 17),
                 n_iterations=3, shape_model_cls=OrthoPDM,
                 appearance_model_cls=PCAVectorModel,
                 compute_error=euclidean_bb_normalised_error,
                 eps=10 ** -5, alpha=0, bias=True):
        super(FullyParametricProjectOutNewton, self).__init__(
            shape_model_cls=shape_model_cls,
            appearance_model_cls=appearance_model_cls)

        self._regressor_cls = partial(IRLRegression, alpha=alpha, bias=bias)
        self.patch_shape = patch_shape
        self.patch_features = patch_features
        self.n_iterations = n_iterations
        self._compute_error = compute_error
        self.eps = eps

# TODO: document me!
class FullyParametricProjectOutGaussNewton(ParametricAppearanceProjectOut):
    r"""
    """

    def __init__(self, patch_features=no_op, patch_shape=(17, 17),
                 n_iterations=3, shape_model_cls=OrthoPDM,
                 appearance_model_cls=PCAVectorModel,
                 compute_error=euclidean_bb_normalised_error,
                 eps=10 ** -5, alpha=0, bias=True, alpha2=0):
        super(FullyParametricProjectOutGaussNewton, self).__init__(
            shape_model_cls=shape_model_cls,
            appearance_model_cls=appearance_model_cls)

        self._regressor_cls = partial(IIRLRegression, alpha=alpha, bias=bias,
                                      alpha2=alpha2)
        self.patch_shape = patch_shape
        self.patch_features = patch_features
        self.n_iterations = n_iterations
        self._compute_error = compute_error
        self.eps = eps

class FullyParametricProjectOutOPP(ParametricAppearanceProjectOut):
    r"""
    """

    def __init__(self, patch_features=no_op, patch_shape=(17, 17),
                 n_iterations=3, shape_model_cls=OrthoPDM,
                 appearance_model_cls=PCAVectorModel,
                 compute_error=euclidean_bb_normalised_error,
                 eps=10 ** -5, bias=True):
        super(FullyParametricProjectOutOPP, self).__init__(
            shape_model_cls=shape_model_cls,
            appearance_model_cls=appearance_model_cls)

        self._regressor_cls = partial(OPPRegression, bias=bias)
        self.patch_shape = patch_shape
        self.patch_features = patch_features
        self.n_iterations = n_iterations
        self._compute_error = compute_error
        self.eps = eps


import sys
import os
import numpy as np
import pandas as pd

DEVICE_INFO = pd.read_csv(os.path.join(os.path.dirname(__file__), "device_info.csv"))
DEVICE_INFO.set_index("device_name", inplace = True)

# DEVICE_INFO = {
#         "A100-SXM4-40GB_1g_5gb":{
#             "BW_gbps": 3 * 1600 / 8
#         },
#         "A100-SXM4-40GB_2g_10gb": {
#             "BW_gbps": 2 * 1600 / 8
#         },
#         "A100-SXM4-40GB_3g_20gb": {
#             "BW_gbps": 4 * 1600 / 8
#         },
#         "A100-SXM4-40GB_4g_20gb": {
#             "BW_gbps": 8 * 1600 / 8
#         },
#         ### TODO (huhanpeng) MIG devices
#     }

ALIAS_DICT = {
    "A100": ["A100-SXM4-40GB", "A100_Graphics_Device", "A100-SXM-80GB"],
    "V100": ["Tesla_V100-SXM2-32GB", "Tesla_V100-PCIE-32GB"],
    "T4": ["Tesla_T4"],
    "A100-SXM4-40GB_1g_5gb": [],
    "A100-SXM4-40GB_2g_10gb": [],
    "A100-SXM4-40GB_3g_20gb": [],
    "A100-SXM4-40GB_4g_20gb": [],
    "A30": [],
    "A10": ["NVIDIA_A10"],
    "K80": [],
    "P100": ["Tesla_P100-PCIE-12GB"],
    "HL-100": [],
    "E5-2673": [],
    "EPYC-7452": [],
    "GRAVITON2": [],
    "PLATINUM-8272": []
}

ALIAS2SHORT = {}

for gpu_model, alias in ALIAS_DICT.items():
    ALIAS2SHORT[gpu_model] = gpu_model
    for alia in alias:
        ALIAS2SHORT[alia] = gpu_model

### **NOTE**: ALL_GPU_MODEL must be modified manually, instead of being generated from ALIAS2SHORT
# Be careful to change the order of gpu names, because changing the order of a gpu model
# name may affect the current cached profiled data
ALL_GPU_MODEL = [
    "A100-SXM4-40GB_1g_5gb",
    "A100-SXM4-40GB_2g_10gb",
    "A100-SXM4-40GB_3g_20gb",
    "A100_Graphics_Device",
    "A100-SXM4-40GB_4g_20gb",
    "A100-SXM4-40GB",
    "Tesla_T4",
    "A30",
    "Tesla_V100-SXM2-32GB",
    "A100",
    "T4",
    "V100",
    "A10",
    "K80",
    "P100",
    "HL-100",
    "E5-2673",
    "EPYC-7452",
    "GRAVITON2",
    "PLATINUM-8272"
]

def gpu_model2int(gpu_model):
    return ALL_GPU_MODEL.index(gpu_model)

def short_device_name(gpu_model):
    return ALIAS2SHORT[gpu_model]

def query_cc(gpu_model):
    ''' Query the compute capability '''
    return int(DEVICE_INFO.loc[short_device_name(gpu_model.upper())].capability)

def query_core_num(gpu_model, dtype, tensor_core_or_not):
    ''' Return the number of arithmetic units corresponding to `dtype` '''
    if dtype == "fp16":
        if tensor_core_or_not:
            return int(DEVICE_INFO.loc[short_device_name(gpu_model)].tensor_core_au_per_sm)
        elif "fp16_au_per_sm" in DEVICE_INFO[short_device_name(gpu_model)]:
            return int(DEVICE_INFO.loc[short_device_name(gpu_model)].fp16_au_per_sm)
        else:
            return int(DEVICE_INFO.loc[short_device_name(gpu_model)].fp32_au_per_sm)
    elif dtype == "fp32":
        return int(DEVICE_INFO.loc[short_device_name(gpu_model)].fp32_au_per_sm)

DEVICE_FEATURE_HEAD = [
    "device_id",
    "clock_MHz", "memory_gb", "shm_L3_kb", "L2cache_mb", "L1cache_kb", "BW_gbps", "fp32_tflops"]
DEVICE_FEATURE_LEN = len(DEVICE_FEATURE_HEAD)

def get_device_feature(device: str):
    ''' Return device-dependent features'''
    device = short_device_name(device.upper())
    _device_info = DEVICE_INFO.loc[short_device_name(device)]
    MAX = DEVICE_INFO.loc["MAX"]
    device_feature = np.array([
        # gpu_model2int(device),
        1.,
        float(_device_info.clock_MHz) / float(MAX.clock_MHz),
        # int(_device_info.SM_count),
        np.log2(int(_device_info.memory_gb)) / np.log2(int(MAX.memory_gb)),
        np.log2(int(_device_info.shm_L3_kb)) / np.log2(int(MAX.shm_L3_kb)),
        np.log2(int(_device_info.L2cache_mb)) / np.log2(int(MAX.L2cache_mb)),
        np.log2(int(_device_info.L1cache_kb)) / np.log2(int(MAX.L1cache_kb)),
        np.log2(float(_device_info.BW_gbps)) / np.log2(float(MAX.BW_gbps)),
        float(_device_info.fp32_tflops) / float(MAX.fp32_tflops)
    ])
    assert len(device_feature) == DEVICE_FEATURE_LEN
    return device_feature

if __name__ == "__main__":
    cc = query_cc(sys.argv[1])
    print(cc)


import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams

config = {
    "font.family": 'serif',
    "font.size": 10,
    "mathtext.fontset": 'stix',
    "font.serif": ['Times New Roman'],
}
rcParams.update(config)

table = np.loadtxt('result2.csv', delimiter=',')

epoch = table[:, 0]
DCT = table[:, 1]
DCT_E = table[:, 2]
DCT_N = table[:, 3]

plt.figure(figsize=(12, 10))
plt.axis('off')
plt.subplot(2, 1, 1)
plt.title('Global', fontsize=15)
plt.xlabel('Epoch', fontsize=10)
plt.ylabel('Top1 Accuracy [%]', fontsize=20)
plt.plot(epoch, DCT, label='ResNet50', linewidth=2)
plt.plot(epoch, DCT_E, label='ResNet50-E', linewidth=2)
plt.plot(epoch, DCT_N, label='ResNet50-N', linewidth=2)
plt.legend(fontsize=20, loc='lower right')

plt.subplot(2, 1, 2)
plt.title('Partial', fontsize=15)
plt.xlabel('Epoch', fontsize=10)
plt.ylabel('Top1 Accuracy [%]', fontsize=20)
plt.plot(epoch[-3:], DCT[-3:], label='ResNet50', linewidth=2)
plt.plot(epoch[-3:], DCT_E[-3:], label='ResNet50-E', linewidth=2)
plt.plot(epoch[-3:], DCT_N[-3:], label='ResNet50-N', linewidth=2)
plt.legend(fontsize=20, loc='lower right')
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)

plt.suptitle('ResNet Group', fontsize=20)
plt.savefig('out2.png', dpi=100)
plt.show()


from codeapi.languages import Interpreted
from codeapi.utils import FileManager

class Python(Interpreted):
    name = 'python'
    extension = '.py'

    def __init__(self):
        super().__init__()

        self.file_manager = FileManager(self.languages_path)
        if not self.file_manager.is_directory_exists(self.name):
            self.file_manager.create_directory(self.name)

        self.path = self.file_manager.join(self.languages_path, self.name)

    def run(self, filename: str) -> str:
        return f'python {filename}'


"""
Repository for student
"""
from Iterable.iterable import Iterable
from domain.student import *
from validator.validators import RepositoryException

class StudentRepository:
    def __init__(self, students_list=None):
        """
        Creates a list of students
        :param students_list: the list of students
        """
        self._students_list = Iterable()
        if students_list is not None:
            self._students_list = students_list

    @property
    def students_list(self):
        """
        :return: the list of students
        """
        return self._students_list

    def add_student(self, new_student):
        """
        Adds a new student to the list of students
        :param new_student: the student who will be added to the list
        :return: -
        Raise RepositoryException if the id of the new student is the id of a student from the list of students
        """
        for student in self._students_list:
            if student.entity_id == new_student.entity_id:
                raise RepositoryException('This id already exists')
        self._students_list.append(new_student)

    def remove_student(self, student_id):
        """
        Removes a student from the list of students by its id
        :param student_id: the id of the student who will be removed from the list
        :return: -
        """
        for student in self._students_list[:]:
            if student.entity_id == student_id:
                self._students_list.remove(student)
                return student

    def update_student(self, student, new_name, new_group):
        """
        Change attributes of a student
        :param student:
        :param new_name:
        :param new_group:
        :return:
        """
        old_name, old_group = student.name, student.group
        student.name = new_name
        student.group = new_group
        return student, old_name, old_group


class MyClass:
    def __init__(self):
        self.__private_var = 10  # Private variable using name mangling

    def get_private_var(self):
        return self.__private_var

    def set_private_var(self, value):
        self.__private_var = value

# Creating an instance of MyClass
obj = MyClass()

# Trying to access the private variable directly
# This will result in an AttributeError as the name is mangled
try:
    print(obj.__private_var)
except AttributeError as e:
    print("AttributeError:", e)

# Accessing the private variable using a getter method
print(obj.get_private_var())  # Output: 10

# Trying to modify the private variable directly
# This will not affect the actual variable as it's name-mangled
obj.__private_var = 20

# Accessing the private variable again using the getter method
# It remains unchanged despite the attempted modification
print(obj.get_private_var())  # Output: 10

# Modifying the private variable using a setter method
obj.set_private_var(30)

# Accessing the private variable again using the getter method
# It reflects the change made using the setter method
print(obj.get_private_var())  # Output: 30


class Book:
    def __init__(self,bookname,author,price,published):
        self.book_name = bookname
        self.book_author = author
        self.book_price = price
        self.book_published = published

    def add_citizen(self):
        print("Book Name: "+self.book_name)
        print("Author: "+self.book_author)
        print("Price: "+str(self.book_price))
        print("Publish Date: "+self.book_published)
        print("Book Added")

book1 = Book("Harry Potter","J. K. Kennedy",198, "1997")
book1.add_citizen()


#!!from configobj import ConfigObj
import logging

token = 0
proxy_server = 0
proxy_type = 0

def init_cfg(config_fname):
    try:
        cfglines = open(config_fname).read().splitlines() # removes trailing \n
        cfg = dict(l.split(' = ') for l in cfglines)
    except IOError:
        logging.error('Unable to open %s; disabling HipChat' % config_fname)
        return
    global token, proxy_type, proxy_server
    token = cfg['token']
    proxy_server = cfg.get('proxy_server', 0)
    proxy_type = cfg.get('proxy_type', 0)


import psycopg2
import database_service

conn = database_service.connect()

def create_tables():
    """ create tables in the PostgreSQL database"""
    commands = (
        """
        CREATE TABLE IF NOT EXISTS athlete (
            athlete_id SERIAL PRIMARY KEY,
            name VARCHAR(255) NOT NULL,
            email VARCHAR(255) NOT NULL,
            phone VARCHAR(255) NOT NULL,
            gender VARCHAR(255) NOT NULL,
            birthday DATE NOT NULL
        )
        """,
        """ CREATE TABLE IF NOT EXISTS plan (
                   plan_id SERIAL PRIMARY KEY,
                   athlete_id INTEGER NOT NULL,
                   name VARCHAR(255) NOT NULL,
                   description TEXT NOT NULL,
                   difficulty VARCHAR(255) NOT NULL,
                        FOREIGN KEY (athlete_id)
                            REFERENCES athlete (athlete_id)
                            ON UPDATE CASCADE ON DELETE CASCADE
               )
        """,
        """
                CREATE TABLE IF NOT EXISTS exercise (
                    exercise_id SERIAL PRIMARY KEY,
                    name VARCHAR(255) NOT NULL,
                    description TEXT NOT NULL
                )
                """
    )

    try:
        cur = conn.cursor()
        for c in commands:
            cur.execute(c)
            print("Table was created successfully!")
        cur.close()
        conn.commit()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()

if __name__ == '__main__':
    create_tables()


import re
# import re
# import .txt file ofr exercise
# setup with open statement
# print title of list for clarity
# looking for the last name
# only print if name is capped correctly
# if not not configured correctly return 'NONE'
# separate solution into groups
# using match to find the correct setup for the last name
#
with open('day1/regex.txt') as file:
    names = file.readlines()

print('==============\nLast Name\n==============')

for person in names:
    test = re.match(r'([\w\W])* ([A-Z][A-Za-z]+)$', person)
    if test:
        print(f'{test.groups()[1]}\n')
    else:
        print('NONE')


from flask import Flask,request,render_template
import numpy as np
import pandas as pd

from sklearn.preprocessing import StandardScaler
from src.pipeline.predict_pipeline import CustomData,PredictPipeline

application=Flask(__name__)

app=application

## Route for a home page

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/predictdata',methods=['GET','POST'])
def predict_datapoint():
    if request.method=='GET':
        return render_template('home.html')
    else:
        data=CustomData(
            radius_mean=request.form.get('radius_mean'),
            concave_points_mean=request.form.get('concave_points_mean'),
            compactness_mean=request.form.get('compactness_mean'),
            concave_points_worst=request.form.get('concave_points_worst'),
            concavity_worst=request.form.get('concavity_worst'),
            compactness_worst=float(request.form.get('compactness_worst')),

        )
        pred_df=data.get_data_as_data_frame()
        print(pred_df)
        print("Before Prediction")

        predict_pipeline=PredictPipeline()
        print("Mid Prediction")
        results=predict_pipeline.predict(pred_df)
        print("after Prediction")
        if results[0] == 'B':
            result = 'Mammogram Results indicate Benign tumor'
        else:
            result = 'Mammogram Results indicate Malignant tumor'
        return render_template('home.html',results=result)

if __name__=="__main__":
    app.run(debug=True,host="0.0.0.0")