# coding: utf-8
import numpy as np
from . import _kernel
from ..utils import basis_transformer
from ..utils import flush_progress_bar

class LinearRegression():
    def __init__(self, basis="none", **basisargs):
        self.basis = basis_transformer(basis, **basisargs)

    def basis_transform(self, X):
        X   = X.reshape(-1,1) if X.ndim==1 else X
        N,_ = X.shape
        X   = self.basis.transform(X)
        D   = int(np.product(X.shape)/N)
        X   = X.reshape(N,D)
        return X

    def fit(self, train_x, train_y):
        """
        @param train_x: shape=(N,?)
        @param train_y: shape=(N,M)
        @val         w: shape=()
        """
        train_x = self.basis_transform(train_x).astype(float)
        self.w = np.linalg.solve(train_x.T.dot(train_x), train_x.T.dot(train_y))

    def predict(self, X):
        """
        @param X: shape=(N',D)
        """
        X = self.basis_transform(X)
        return X.dot(self.w) # shape=(N',M)

class LinearRegressionRidge(LinearRegression):
    def __init__(self, lamda=1e-3, basis="none", **basisargs):
        self.lamda = lamda
        super().__init__(basis=basis, **basisargs)

    def fit(self, train_x, train_y):
        """
        @param train_x: shape=(N,?)
        @param train_y: shape=(N,M)
        @val         w: shape=()
        """
        train_x = self.basis_transform(train_x)
        N, M = train_x.shape
        self.w = np.linalg.solve(self.lamda*np.identity(M) + train_x.T.dot(train_x), train_x.T.dot(train_y))

class LinearRegressionLASSO(LinearRegression):
    def __init__(self, lamda=1e-3, basis="none", **basisargs):
        self.lamda = lamda
        super().__init__(basis=basis, **basisargs)

    @staticmethod
    def prox(w,alpha,rho,lamda):
        z0 = w + alpha/rho
        c = lamda/rho
        return z0-c if c<z0 else z0+c if z0<-c else 0

    def fit(self, train_x, train_y, rho=1e-3, tol=1e-7, max_iter=100, verbose=1):
        """
        @param train_x: shape=(N,?)
        @param train_y: shape=(N,M)
        @val         w: shape=()
        """
        train_x = self.basis_transform(train_x)
        N, M = train_x.shape
        # Initialization.
        alpha = np.ones(shape=(M))
        z = np.ones(shape=(M))
        for it in range(max_iter):
            w = np.linalg.solve(train_x.T.dot(train_x)+rho*np.identity(M), train_x.T.dot(train_y)-alpha+rho*z)
            z = np.asarray([self.prox(w_,alpha_,rho,self.lamda) for w_,alpha_ in zip(w,alpha)])
            alpha += rho*(w-z)
            diff = np.sqrt(np.sum(np.square(w-z)))
            if diff < tol: break
            flush_progress_bar(it, max_iter, metrics={"diff": diff}, verbose=verbose)
        if verbose: print()
        self.w = w

class BayesianLinearRegression(LinearRegression):
    def __init__(self, alpha=1, beta=25, basis="none", **basisargs):
        self.alpha=alpha
        self.beta=beta
        self.SN=None
        self.mN=None
        super().__init__(basis=basis, **basisargs)

    def fit(self, train_x, train_y):
        train_x = self.basis_transform(train_x)
        N,M = train_x.shape
        self.SN = np.linalg.inv(self.alpha*np.eye(M) + self.beta*train_x.T.dot(train_x))
        self.mN = self.beta*self.SN.dot(train_x.T.dot(train_y))

    def predict(self, X):
        X = self.basis_transform(X)
        mu = np.array([self.mN.T.dot(x) for x in X])
        std = np.sqrt(np.array([1/self.beta + x.T.dot(self.SN).dot(x) for x in X]))
        return (mu, std)

class EvidenceApproxBayesianRegression(BayesianLinearRegression):
    def __init__(self, alpha=1, beta=1, basis="none", **basisargs):
        super().__init__(alpha=alpha, beta=beta, basis=basis, **basisargs)
        self.gamma = None

    def fit(self, train_x, train_y, max_iter=100, verbose=1):
        train_x_ = self.basis_transform(train_x)
        N,M = train_x_.shape
        Phi = train_x_.T.dot(train_x_)
        eigvals = np.linalg.eigvals(Phi)
        for it in range(max_iter):
            params = [self.alpha, self.beta]
            super().fit(train_x, train_y)
            lamda = self.beta * eigvals
            self.gamma = np.sum(lamda / (self.alpha + lamda))
            self.alpha = self.gamma / self.mN.dot(self.mN)
            self.beta  = (N-self.gamma) / np.sum( (train_y-train_x_.dot(self.mN))**2 )
            if np.allclose(params, [self.alpha, self.beta]): break
            flush_progress_bar(it, max_iter, barname="Search for Hyper Parameters (alpha, beta)", verbose=verbose)
        super().fit(train_x, train_y)

    def evidence(self, train_x, train_y):
        """ loglikelihood of marginalization ln p(y|α,β) PRML(3.86) """
        train_x = self.basis_transform(train_x)
        N,M = train_x.shape
        A = self.alpha*np.eye(M) + self.beta*train_x.T.dot(train_x)
        EmN = self.beta/2 * np.sum( (train_y - train_x.dot(self.mN))**2 ) + self.alpha/2 * self.mN.dot(self.mN)
        logmarg = M/2*np.log(self.alpha) + N/2*np.log(self.beta) - EmN - 1/2*np.log(np.linalg.det(A)) - N/2*np.log(2*np.pi)
        return logmarg

class KernelRegression():
    def __init__(self, lamda, kernel="gaussian", **kernelargs):
        self.lamda = lamda
        self.kernel = _kernel.get(kernel, **kernelargs)
        self.x_train = None # shape=(N,D)

    def fit(self, x_train, y_train):
        """
        @param x_train: shape=(N, D)
        @param y_train: shape=(N, M)
        """
        N = x_train.shape[0]
        K = np.asarray([[self.kernel(x, x_prime) for x in x_train] for x_prime in x_train])
        self.theta = np.linalg.solve(K.T.dot(K)+self.lamda*np.identity(N), K.T.dot(y_train))
        self.x_train = x_train

    def predict(self, X):
        """
        @param X: shape=(N', D)
        """
        K = np.asarray([[self.kernel(x, x_prime) for x_prime in self.x_train] for x in X])
        predictions = K.dot(self.theta) # (N',N)@(N,M) = (N',M)
        return predictions


import random,os
from logo import logo

def deal_card():
    cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
    card=random.choice(cards)
    return card

def calculate_score(player_card):
    if sum(player_card) == 21 and len(player_card) == 2:
        return 0
    if 11 in player_card and sum(player_card) > 21:
        player_card.remove(11)
        player_card.append(1)
    return sum(player_card)

def compare(final_user_score, final_computer_score):
    if (final_user_score == final_computer_score):
        return "Its a draw"
    elif (final_user_score == 0):
        return "You Win with a black jack"
    elif (final_computer_score == 0):
        return "You Lose, Computer has black jack"
    elif (final_user_score > 21):
        return (f"You Lose, user score is {final_user_score} > 21")
    elif (final_computer_score > 21):
        return (f"You Win, computer score is {final_computer_score} > 21")
    elif (final_user_score > final_computer_score):
        return (f"You Win, user score is {final_user_score} > computer score {final_user_score}")
    else:
        return (f"You Lose, computer score is {final_computer_score} > user score {final_user_score}")

def play_game():
    print(logo)
    user_cards=[]
    computer_cards=[]
    game_over_flag=False

    for _ in range(2):
            user_cards.append(deal_card())
            computer_cards.append(deal_card())

    while not game_over_flag:
        user_score=calculate_score(player_card=user_cards)
        computer_score=calculate_score(player_card=computer_cards)
        print(f"user cards are {user_cards}, user_score is: {sum(user_cards)}")
        print(f"computer's first card is: {computer_cards[0]}")

        if (user_score == 0 or computer_score == 0 or user_score > 21):
            game_over_flag=True
        else:
            check=input("does the user wants to draw another card? Type 'y' else 'n' ").lower()
            if check=="y":
                user_cards.append(deal_card())
                game_over_flag=False
            else:
                game_over_flag=True

    while computer_score !=0 and computer_score < 17:
        computer_cards.append(deal_card())
        computer_score=calculate_score(player_card=computer_cards)
        #print(f"computer cards are {computer_cards}, computer_score is: {sum(computer_cards)}")

    print(f"user cards are {user_cards}, user_score is: {sum(user_cards)}")
    print(f"computer cards are {computer_cards}, computer_score is: {sum(computer_cards)}")
    print(compare(user_score,computer_score))

while input("Do you want to play blackJack game? Type 'y' else 'n' ").lower():
    os.system('clear')
    play_game()


"""
Workflow for running model optimization.
"""
from dataset import Dataset
from model import Model
from optimizer import Optimizer

def run(epochs=5, init_lr=3e-5, model_name="small_bert/bert_en_uncased_L-4_H-512_A-8"):
    """
    Executes the workflow for model optimization.
    """
    dataset = Dataset()
    print(dataset)
    model = Model(model_name)
    opt = Optimizer(dataset, epochs, init_lr)
    model.compile(opt)
    _history = model.fit(dataset, opt)
    model.save("bert.h5")
    # model.save_to_json("bert-json-dir")


"""
Coding: UTF-8
Author: Randal
Time: 2021/2/20
E-mail: RandalJin@163.com

Description: This is a simple toolkit for data extraction of text.
The most important function in the script is about word frequency statistics.
Using re, I generalized the process in words counting, regardless of  any preset
word segmentation. Besides, many interesting functions, like getting top sentences are built here.

All rights reserved.

"""

import xlwings as xw
import pandas as pd
import numpy as np
import os
import re
from alive_progress import alive_bar
from alive_progress import show_bars, show_spinners
import jieba
import datetime
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
import math

class jieba_vectorizer(CountVectorizer):

    def __init__(self, tf, userdict, stopwords, orient=False):
        """
        :param tf: 输入的样本框，{axis: 1, 0: id, 1: 标题, 2: 正文, 3: 来源, 4: freq}
        :param stopwords: 停用词表的路径
        :param user_dict_link: 关键词清单的路径
        :param orient: {True: 返回的 DTM 只包括关键词清单中的词，False: 返回 DTM 中包含全部词语}
        :return: 可以直接使用的词向量样本
        """
        self.userdict = userdict
        self.orient = orient
        self.stopwords = stopwords
        jieba.load_userdict(self.userdict)  # 载入关键词词典

        tf = tf.copy()  # 防止对函数之外的原样本框造成改动
        print('切词中，请稍候……')
        rule = re.compile(u'[^\u4e00-\u9fa5]')  # 清洗所有样本，只保留汉字
        for i in range(0, tf.shape[0]):
            try:
                tf.iloc[i, 2] = rule.sub('', tf.iloc[i, 2])
            except TypeError:
                print('样本清洗Error: doc_id = ' + str(i))
            continue

        if self.stopwords is not None:
            stopwords = txt_to_list(self.stopwords)  # 载入停用词表
        else:
            stopwords = []

        # 开始切词

        words = []
        items = range(0, len(tf))
        with alive_bar(len(items), force_tty=True, bar='circles') as bar:
            for i, row in tf.iterrows():
                item = row['正文']
                result = jieba.cut(item)
                # 同时过滤停用词
                word = ''
                for element in result:
                    if element not in stopwords:
                        if element != '\t':
                            word += element
                            word += " "
                words.append(word)
                bar()

        # CountVectorizer() 可以自动完成词频统计，通过fit_transform生成文本向量和词袋库
        # 如果需要换成 tfidfVectorizer, 把下面三行修改一下就可以了
        vect = CountVectorizer()
        X = vect.fit_transform(words)
        self.vectorizer = vect

        matrix = X
        X = X.toarray()
        # 二维ndarray可以展示在pycharm里，但是和DataFrame性质完全不同
        # ndarray 没有 index 和 column
        features = vect.get_feature_names()
        XX = pd.DataFrame(X, index=tf['id'], columns=features)

        self.DTM0 = matrix
        self.DTM = XX
        self.features = features

        # # 下面是之前走的弯路，不足一哂
        # words_bag = vect.vocabulary_
        # # 字典的转置（注意只适用于vk一一对应的情况，1v多k请参考setdefault)
        # bag_words = dict((v, k) for k, v in words_bag.items())
        #
        # # 字典元素的排列顺序不等于字典元素值的排列顺序
        # lst = []
        # for i in range(0, len(XX.columns)):
        #     lst.append(bag_words[i])
        # XX.columns = lst

        if orient:
            dict_filter = txt_to_list(self.userdict)
            for word in features:
                if word not in dict_filter:
                    XX.drop([word], axis=1, inplace=True)
            self.DTM_key = XX

    def get_feature_names(self):
        return self.features

    def strip_non_keywords(self, df):
        ff = df.copy()
        dict_filter = txt_to_list(self.userdict)
        for word in self.features:
            if word not in dict_filter:
                ff.drop([word], axis=1, inplace=True)
        return ff

def make_doc_freq(word, doc):
    """
    :param word: 指的是要对其进行词频统计的关键词
    :param doc: 指的是要遍历的文本
    :return: lst: 返回字典，记录关键词在文本当中出现的频次以及上下文
    """
    # 使用正则表达式进行匹配, 拼接成pattern

    # re.S表示会自动换行
    # finditer是findall的迭代器版本，通过遍历可以依次打印出子串所在的位置
    it = re.finditer(word, doc, re.S)
    # match.group()可以返回子串，match.span()可以返回索引
    lst = []
    for match in it:
        lst.append(match.span())
    freq = dict()
    freq['Frequency'] = len(lst)
    # 将上下文结果也整理为一个字典
    context = dict()
    for i in range(0, len(lst)):
        # 将span的范围前后各扩展不多于10个字符，得到上下文
        try:
            # 为了划出适宜的前后文范围，需要设定索引的最大值和最小值
            # 因此要比较span+10和doc极大值，span-10和doc极小值
            # 最大值在两者间取小，最小值在两者间取大
            MAX = min(lst[i][1] + 10, len(doc))
            MIN = max(0, lst[i][0] - 10)
            # 取得上下文
            context[str(i)] = doc[MIN: MAX]
        except IndexError:
            print('IndexError: ' + word)
    freq['Context'] = context
    return freq

def make_info_freq(name, pattern, doc):
    """
    :param name: 指的是对其进行词频统计的形式
    :param pattern: 指的是对其进行词频统计的正则表达式
    :param doc: 指的是要遍历的文本
    :return: lst: 返回字典，记录关键词在文本当中出现的频次以及上下文
    注：该函数返回字典中的context元素为元组：（关键词，上下文）
    """
    # 使用正则表达式进行匹配, 拼接成pattern
    # re.S表示会自动换行
    # finditer是findall的迭代器版本，通过遍历可以依次打印出子串所在的位置
    it = re.finditer(pattern[0], doc, re.S)
    # match.group()可以返回子串，match.span()可以返回索引
    cls = pattern[1]
    lst = []
    for match in it:
        lst.append(match.span())
    freq = dict()
    freq['Frequency'] = len(lst)
    freq['Name'] = name
    # 将上下文结果也整理为一个字典
    context = dict()
    for i in range(0, len(lst)):
        # 将span的范围前后各扩展不多于10个字符，得到上下文
        try:
            # 为了划出适宜的前后文范围，需要设定索引的最大值和最小值
            # 因此要比较span+10和doc极大值，span-10和doc极小值
            # 最大值在两者间取小，最小值在两者间取大
            MAX = min(lst[i][1] + 10, len(doc))
            MIN = max(0, lst[i][0] - 10)
            # 取得匹配到的关键词，并做掐头去尾处理
            word = match_cut(doc[lst[i][0]: lst[i][1]], cls)
            # 将关键词和上下文打包，存储到 context 条目中
            context[str(i)] = (word, doc[MIN: MAX])
        except IndexError:
            print('IndexError: ' + name)
    freq['Context'] = context
    return freq

def make_docs_freq(word, docs):
    """
    :param word: 指的是要对其进行词频统计的关键词
    :param docs: 是要遍历的文本的集合，必须是pandas DataFrame的形式，至少包含id列 (iloc: 0)，正文列 (iloc: 2) 和预留出的频次列 (iloc: 4)
    :return: 返回字典，其中包括“单关键词-单文本”的词频字典集合，以及计数结果汇总
    """
    freq = dict()
    # 因为总频数是通过"+="的方式计算，不是简单赋值，所以要预设为0
    freq['Total Frequency'] = 0
    docs = docs.copy()  # 防止对函数之外的原样本框造成改动

    for i in range(0, len(docs)):
        # 对于每个文档，都形成一个字典，字典包括关键词在该文档出现的频数和上下文
        # id需要在第0列，正文需要在第2列
        freq['Doc' + str(docs.iloc[i, 0])] = make_doc_freq(word, docs.iloc[i, 2])
        # 在给每个文档形成字典的同时，对于总概率进行滚动加总
        freq['Total Frequency'] += freq['Doc' + str(docs.iloc[i, 0])]['Frequency']
        docs.iloc[i, 4] = freq['Doc' + str(docs.iloc[i, 0])]['Frequency']

    # 接下来建立一个DFC(doc-freq-context)统计面板，汇总所有文档对应的词频数和上下文
    # 首先构建(id, freq)的字典映射
    xs = docs['id']
    ys = docs['freq']
    # zip(迭代器)是一个很好用的方法，建议多用
    id_freq = {x: y for x, y in zip(xs, ys)}

    # 新建一个空壳DataFrame，接下来把数据一条一条粘贴进去
    data = pd.DataFrame(columns=['id', 'freq', 'word', 'num', 'context'])
    for item in xs:
        doc = freq['Doc' + str(item)]
        num = doc['Frequency']
        context = doc['Context']
        for i in range(0, num):
            strip = {'id': item, 'freq': id_freq[item], 'word': word, 'num': i, 'context': context[str(i)]}
            # 默认orient参数等于columns
            # 如果字典的值是标量，那就必须传递一个index，这是规定
            strip = pd.DataFrame(strip, index=[None])
            # df的append方法只能通过重新赋值来进行修改
            data = data.append(strip)
    data.set_index(['id', 'freq', 'word'], drop=True, inplace=True)
    freq['DFC'] = data
    return freq

def make_infos_freq(name, pattern, docs):
    """
    :param name: 指的是对其进行词频统计的形式
    :param pattern: 指的是对其进行词频统计的（正则表达式, 裁剪方法）
    :param docs: 是要遍历的文本的集合，必须是pandas DataFrame的形式，至少包含id列(iloc: 0)和正文列(iloc: 2)
    :return: 返回字典，其中包括“单关键词-单文本”的词频字典集合，以及计数结果汇总
    """
    freq = dict()
    # 因为总频数是通过"+="的方式计算，不是简单赋值，所以要预设为0
    freq['Total Frequency'] = 0
    docs = docs.copy()  # 防止对函数之外的原样本框造成改动

    items = range(0, len(docs))
    with alive_bar(len(items), force_tty=True, bar='circles') as bar:
        for i in items:
            # 对于每个文档，都形成一个字典，字典包括关键词在该文档出现的频数和上下文
            # id需要在第0列，正文需要在第2列
            # pattern 要全须全尾地传递进去，因为make_info_freq两个参数都要用
            freq['Doc' + str(docs.iloc[i, 0])] = make_info_freq(name, pattern, docs.iloc[i, 2])
            # 在给每个文档形成字典的同时，对于总概率进行滚动加总
            freq['Total Frequency'] += freq['Doc' + str(docs.iloc[i, 0])]['Frequency']
            docs.iloc[i, 4] = freq['Doc' + str(docs.iloc[i, 0])]['Frequency']
            bar()

    # 接下来建立一个DFC(doc-freq-context)统计面板，汇总所有文档对应的词频数和上下文
    # 首先构建(id, freq)的字典映射
    xs = docs['id']
    ys = docs['freq']
    # zip(迭代器)是一个很好用的方法，建议多用
    id_freq = {x: y for x, y in zip(xs, ys)}

    # 新建一个空壳DataFrame，接下来把数据一条一条粘贴进去
    data = pd.DataFrame(columns=['id', 'freq', 'form', 'word', 'num', 'context'])
    for item in xs:
        doc = freq['Doc' + str(item)]
        num = doc['Frequency']
        # 从（关键词，上下文）中取出两个元素
        context = doc['Context']
        for i in range(0, num):
            # context 中的关键词已经 match_cut 完毕，不需要重复处理
            strip = {'id': item, 'form': name, 'freq': id_freq[item], 'word': context[str(i)][0],
                     'num': i, 'context': context[str(i)][1]}
            # 默认orient参数等于columns
            # 如果字典的值是标量，那就必须传递一个index，这是规定
            strip = pd.DataFrame(strip, index=[None])
            # df的append方法只能通过重新赋值来进行修改
            data = data.append(strip)
    data.set_index(['id', 'freq', 'form', 'word'], drop=True, inplace=True)
    freq['DFC'] = data
    print(name + '  Completed')
    return freq

def words_docs_freq(words, docs):
    """
    :param words: 表示要对其做词频统计的关键词清单
    :param docs: 是要遍历的文本的集合，必须是pandas DataFrame的形式，至少包含id列、正文列、和频率列
    :return: 返回字典，其中包括“单关键词-多文本”的词频字典集合，以及最终的DFC(doc-frequency-context)和DTM(doc-term matrix)
    """
    freqs = dict()
    # 与此同时新建一个空壳DataFrame，用于汇总DFC
    data = pd.DataFrame()
    # 新建一个空壳，用于汇总DTM(Doc-Term-Matrix)
    dtm = pd.DataFrame(None, columns=words, index=docs['id'])
    # 来吧，一个循环搞定所有
    items = range(len(words))
    with alive_bar(len(items), force_tty=True, bar='blocks') as bar:
        for word in words:
            freq = make_docs_freq(word, docs)
            freqs[word] = freq
            data = data.append(freq['DFC'])
            for item in docs['id']:
                dtm.loc[item, word] = freq['Doc' + str(item)]['Frequency']
            bar()
    # 记得要sort一下，不然排序的方式不对（应该按照doc id来排列）
    data.sort_index(inplace=True)

    freqs['DFC'] = data
    freqs['DTM'] = dtm
    return freqs

def infos_docs_freq(infos, docs):
    """
    :param docs: 是要遍历的文本的集合，必须是pandas DataFrame的形式，至少包含id列和正文列
    :param infos: 指的是正则表达式的列表，格式为字典，key是示例，如“（1）”，value 是正则表达式，如“（[0-9]）”
    :return: 返回字典，其中包括“单关键词-多文本”的词频字典集合，以及最终的DFC(doc-frequency-context)和DTM(doc-term matrix)
    """
    freqs = dict()
    # 与此同时新建一个空壳DataFrame，用于汇总DFC
    data = pd.DataFrame()
    # 新建一个空壳，用于汇总DTM(Doc-Term-Matrix)
    dtm = pd.DataFrame(None, columns=list(infos.keys()), index=docs['id'])
    # 来吧，一个循环搞定所有
    items = range(len(infos))
    with alive_bar(len(items), force_tty=True, bar='blocks') as bar:
        for k, v in infos.items():
            freq = make_infos_freq(k, v, docs)
            freqs[k] = freq
            data = data.append(freq['DFC'])
            for item in docs['id']:
                dtm.loc[item, k] = freq['Doc' + str(item)]['Frequency']
            bar()
    # 记得要sort一下，不然排序的方式不对（应该按照doc id来排列）
    data.sort_index(inplace=True)

    freqs['DFC'] = data
    freqs['DTM'] = dtm
    return freqs

def massive_pop(infos, doc):
    """
    :param infos: List，表示被删除内容对应的正则表达式
    :param doc: 表示正文
    :return: 返回一个完成删除的文本
    """
    for info in infos:
        doc = re.sub(info, '', doc)
    return doc

def massive_sub(infos, doc):
    """
    :param infos: Dict, 表示被替换内容对应的正则表达式及替换对象
    :param doc: 表示正文
    :return: 返回一个完成替换的文本
    """
    for v, k in infos:
        doc = re.sub(v, k, doc)
    return doc

# 接下来取每个样本的前n句话(或者不多于前n句话的内容)，再做一次进行对比
# 取前十句话的原理是，对！？。等表示语义结束的符号进行计数，满十次为止
def top_n_sent(n, doc, percentile=1):
    """
    :param n: n指句子的数量，这个函数会返回一段文本中前n句话，若文本内容不多于n句，则全文输出
    :param word: 指正文内容
    :param percentile: 按照分位数来取句子时，要输入的分位，比如一共有十句话，取50%分位就是5句
    如果有11句话，向下取整也是输出5句
    :return: 返回字符串：前n句话
    """
    info = '[。？！]'
    # 在这个函数体内，函数主体语句的作用域大于循环体，因此循环内的变量相当于局部变量
    # 因此想在循环外直接返回，就会出现没有定义的错误，因此可以做一个全局声明
    # 但是不建议这样做，因为如果函数外有一个变量恰巧和局部变量重名，那函数外的变量也会被改变
    # 因此还是推荐多使用迭代器，把循环包裹成迭代器，可以解决很多问题
    # 而且已经封装好的迭代器，例如re.findall_iter，就不用另外再去写了，调用起来很方便
    # 如下，第一行代码的作用是用列表包裹迭代器，形成一个生成器的列表
    # 每个生成器都存在自己的 Attribute
    re_iter = list(re.finditer(info, doc))
    # max_iter 是 re 匹配到的最大次数
    max_iter = len(re_iter)

    # 这一句表示，正文过于简短，或者没有标点，此时直接输出全文
    if max_iter == 0:
        return doc

    # 考虑 percentile 的情况，如果总共有11句，就舍弃掉原来的 n，直接改为总句数的 percentile 对应的句子数
    # 注意是向下取整
    if percentile != 1:
        n = math.ceil(percentile * max_iter)
        # 如果匹配到至少一句，循环自然结束，输出结果
        if n > 0:
            return doc[0: re_iter[n - 1].end()]
        # 如果正文过于简短，或设定的百分比过低，一句话都凑不齐，此时直接输出第一句
        elif n == 0:
            return doc[0: re_iter[0].end()]

    # 如果匹配到的句子数大于 n，此时只取前 n 句
    if max_iter >= n:
        return doc[0: re_iter[n - 1].end()]
    # 如果匹配到的句子不足 n 句，直接输出全部内容
    elif 0 < max_iter < n:
        return doc[0: re_iter[-1].end()]

    # 为减少重名的可能，尽量在函数体内减少变量的使用

def dtm_sort_filter(dtm, keymap, name=None):
    """
    :param dtm: 前面生成的词频统计矩阵：Doc-Term-Matrix
    :param keymap: 字典，标明了  类别-关键词列表  两者关系
    :param name: 最终生成 Excel 文件的名称（需要包括后缀）
    :return: 返回一个字典，字典包含两个 pandas.DataFrame: 一个是表示各个种类是否存在的二进制表，另一个是最终的种类数
    """
    dtm = dtm.applymap(lambda x: 1 if x != 0 else 0)
    strips = {}
    for i, row in dtm.iterrows():
        strip = {}
        for k, v in keymap.items():
            strip[k] = 0
            for item in v:
                try:
                    strip[k] += row[item]
                except KeyError:
                    pass
        strips[i] = strip
    dtm_class = pd.DataFrame.from_dict(strips, orient='index')
    dtm_class = dtm_class.applymap(lambda x: 1 if x != 0 else 0)
    dtm_final = dtm_class.agg(np.sum, axis=1)
    result = {'DTM_class': dtm_class, 'DTM_final': dtm_final}
    return result

def dtm_point_giver(dtm, keymap, scoremap, name=None):
    """
    :param dtm: 前面生成的词频统计矩阵：Doc-Term-Matrix
    :param keymap: 字典，{TypeA: [word1, word2, word3, ……], TypeB: ……}
    :param scoremap: 字典，标明了  类别-分值 两者关系
    :param name: 最终生成 Excel 文件的名称（需要包括后缀）
    :return: 返回一个 pandas.DataFrame，表格有两列，一列是文本id，一列是文本的分值（所有关键词的分值取最高）
    """
    dtm = dtm.applymap(lambda x: 1 if x != 0 else 0)

    # 非 keymap 中词会被过滤掉
    strips = {}
    for i, row in dtm.iterrows():
        strip = {}
        for k, v in keymap.items():
            strip[k] = 0
            for item in v:
                try:
                    strip[k] += row[item]
                except KeyError:
                    pass
        strips[i] = strip

    dtm_class = pd.DataFrame.from_dict(strips, orient='index')
    dtm_class = dtm_class.applymap(lambda x: 1 if x != 0 else 0)

    # 找到 columns 对应的分值
    keywords = list(dtm_class.columns)
    multiplier = []
    for keyword in keywords:
        multiplier.append(scoremap[keyword])

    # DataFrame 的乘法运算，不会改变其 index 和 columns
    dtm_score = dtm_class.mul(multiplier, axis=1)
    # 取一个最大值来赋分
    dtm_score = dtm_score.agg(np.max, axis=1)
    return dtm_score

def dfc_sort_filter(dfc, keymap, name=None):
    """
    :param dfc: 前面生成的词频统计明细表：Doc-Frequency-Context
    :param keymap: 字典，标明了  关键词-所属种类  两者关系
    :param name: 最终生成 Excel 文件的名称（需要包括后缀）
    :return: 返回一个 pandas.DataFrame，表格有两列，一列是文本id，一列是文本中所包含的业务种类数
    """
    # 接下来把关键词从 dfc 的 Multi-index 中拿出来（这个index本质上就是一个ndarray)
    # 拿出来关键词就可以用字典进行映射
    # 先新建一列class-id，准备放置映射的结果
    dfc.insert(0, 'cls-id', None)

    # 开始遍历
    for i in range(0, len(dfc.index)):
        dfc.iloc[i, 0] = keymap[dfc.index[i][2]]

    # 理论上就可以直接通过 excel 的分类计数功能来看业务种类数了
    # 失败了，excel不能看种类数，只能给所有值做计数，因此还需要借助python的unique语句
    # dfc.to_excel('被监管业务统计.xlsx')

    # 可以对于每一种index做一个计数，使用loc索引到的对象是一个DataFrame
    # 先拿到一个doc id的列表
    did = []
    for item in dfc.index.unique():
        did.append(item[0])
    did = list(pd.Series(did).unique())

    # 接下来获得每一类的结果，注：多重索引的取值值得关注
    uni = {}
    for item in did:
        uni[item] = len(dfc.loc[item, :, :]['cls-id'].unique())

    # 把生成的字典转换为以键值行索引的 DataFrame
    uni = pd.DataFrame.from_dict(uni, orient='index')
    uni.fillna(0, axis=1, inplace=True)
    # uni.to_excel(name)
    return uni

def dfc_point_giver(dfc, keymap, name=None):
    """
    :param dfc: 前面生成的词频统计明细表：Doc-Frequency-Context
    :param keymap: 字典，标明了  关键词-分值 两者关系
    :param name: 最终生成 Excel 文件的名称（需要包括后缀）
    :return: 返回一个 pandas.DataFrame，表格有两列，一列是文本id，一列是文本的分值（所有关键词的分值取最高）
    """
    dfc.insert(0, 'point', None)

    # 开始遍历
    for i in range(0, len(dfc.index)):
        dfc.iloc[i, 0] = keymap[dfc.index[i][2]]

    # 可以对于每一种index做一个计数，使用loc索引到的对象是一个DataFrame
    # 先拿到一个doc id的列表
    did = []
    for item in dfc.index.unique():
        did.append(item[0])
    did = list(pd.Series(did).unique())

    # 接下来获得每一类的结果，注：多重索引的取值值得关注
    uni = {}
    for item in did:
        uni[item] = max(dfc.loc[item, :, :]['point'].unique())

    # 把生成的字典转换为以键值行索引的 DataFrame
    uni = pd.DataFrame.from_dict(uni, orient='index')
    uni.fillna(0, axis=1, inplace=True)
    # uni.to_excel(name)
    return uni

def dfc_sort_counter(dfc, name=None):
    """
    :param dfc: 前面生成的词频统计明细表：Doc-Frequency-Context
    :param name: 最终生成 Excel 文件的名称（需要包括后缀）
    :return: 返回一个 pandas.DataFrame，表格有两列，一列是文本id，一列是文本中所包含的业务种类数
    """
    # 可以对于每一种index做一个计数，使用loc索引到的对象是一个DataFrame
    dfc.insert(0, 'form', None)
    for i in range(0, dfc.shape[0]):
        dfc.iloc[i, 0] = dfc.index[i][2]

    # 先拿到一个doc id的列表
    did = []
    for item in dfc.index.unique():
        did.append(item[0])
    did = list(pd.Series(did).unique())

    # 接下来获得每一类的结果，注：多重索引的取值值得关注
    uni = {}
    for item in did:
        uni[item] = len(dfc.loc[item, :, :, :]['form'].unique())

    # 把生成的字典转换为以键值行索引的 DataFrame
    uni = pd.DataFrame.from_dict(uni, orient='index')
    uni.fillna(0, axis=1, inplace=True)
    # uni.to_excel(name)
    return uni

# 定义一个大写中文数字转阿拉伯数字的函数
CN_NUM = {
    '〇': 0, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9, '零': 0,
    '壹': 1, '贰': 2, '叁': 3, '肆': 4, '伍': 5, '陆': 6, '柒': 7, '捌': 8, '玖': 9, '貮': 2, '两': 2,
}

CN_UNIT = {
    '十': 10,
    '拾': 10,
    '百': 100,
    '佰': 100,
    '千': 1000,
    '仟': 1000,
    '万': 10000,
    '万': 10000,
    '亿': 100000000,
    '亿': 100000000,
    '兆': 1000000000000,
}

def chinese_to_arabic(cn:str) -> str:
    unit = 0  # current
    ldig = []  # digest
    for cndig in reversed(cn):
        if cndig in CN_UNIT:
            unit = CN_UNIT.get(cndig)
            if unit == 10000 or unit == 100000000:
                ldig.append(unit)
                unit = 1
        else:
            dig = CN_NUM.get(cndig)
            if unit:
                dig *= unit
                unit = 0
            ldig.append(dig)
    if unit == 10:
        ldig.append(10)
    val, tmp = 0, 0
    for x in reversed(ldig):
        if x == 10000 or x == 100000000:
            val += tmp * x
            tmp = 0
        else:
            tmp += x
    val += tmp
    val = str(val)
    return val

def match_cut(matchobj, cls):
    """
    :param matchobj: 输入匹配好的字符串
    :param cls: 这是正则表达式的匹配方法，遵循掐头去尾的原则。
    0：原样奉还，1：掐一去一，2：掐一去二，3：掐二去一，4：掐一去零,  5：掐零去一
    :return: 返回一个处理好的字符串
    """
    if cls == 0:
        return matchobj
    if cls == 1:
        return matchobj[1: -1]
    if cls == 2:
        return matchobj[1: -2]
    if cls == 3:
        return matchobj[2, -1]
    if cls == 4:
        return matchobj[1:]
    if cls == 5:
        return matchobj[: -1]

def re_formatter(file_time):
    """
    这个函数是修改数据格式的一个小工具，会将输入的时间统一由YYYY-DD-MM修改为YYYY/MM/DD
    :param file_time: 输入的时间, 纯字符串格式
    :return: 返回的时间，datetime格式(%YYYY/%MM/%DD)
    """
    # 首先把空格和空行删去
    file_time = str(file_time)
    file_time = file_time.strip('\n\r\t ')
    file_time = file_time[:10]
    # 将字符串转为datetime格式
    file_time = datetime.datetime.strptime(file_time, '%Y-%m-%d')
    # 将datetime转为字符串
    file_time = datetime.datetime.strftime(file_time, '%Y/%m/%d')
    return file_time

def year_month_finder(file_time):
    """
   这个函数是修改数据格式的一个小工具，会将输入的时间统一由YYYY/DD/MM修改为YYYY年M月
    :param file_time: 输入的时间, 纯字符串格式
    :return: 返回的时间，格式为(%YYYY年%M月)
    """
    # 首先把空格和空行删去
    file_time = str(file_time)
    file_time = file_time.strip('\n\r\t ')
    # 将字符串转为datetime格式
    try:
        file_time = datetime.datetime.strptime(file_time, '%Y/%m/%d')
        file_time_year = datetime.datetime.strftime(file_time, '%Y')
        file_time_month = datetime.datetime.strftime(file_time, '%m')
        file_time = file_time_year + 'M' + file_time_month
    except ValueError:
        pass
    return file_time

def month_day_finder(file_time):
    """
   这个函数是修改数据格式的一个小工具，会将输入的时间统一由YYYY/DD/MM修改为M月D日
    :param file_time: 输入的时间, 纯字符串格式
    :return: 返回的时间，格式为(%M%月/%D%日)
    """
    # 首先把空格和空行删去
    file_time = str(file_time)
    file_time = file_time.strip('\n\r\t ')
    # 将字符串转为datetime格式
    try:
        file_time = datetime.datetime.strptime(file_time, '%Y/%m/%d')
        file_time_month = datetime.datetime.strftime(file_time, '%m')
        file_time_day = datetime.datetime.strftime(file_time, '%d')
        file_time = file_time_month + '/' + file_time_day
    except ValueError:
        pass
    return file_time

quarter_map = {'1': '1', '2': '1', '3': '1', '4': '2', '5': '2', '6': '2',
               '7': '3', '8': '3', '9': '3', '10': '4', '11': '4', '12': '4',
               '01': '1', '02': '1', '03': '1', '04': '2', '05': '2', '06': '2',
               '07': '3', '08': '3', '09': '3'}

def quarter_finder(file_time):
    """
    这个函数是修改数据格式的一个小工具，会将输入的时间统一由YYYY/DD/MM修改为YYYY年Q季度
    :param file_time: 输入的时间, 纯字符串格式
    :return: 返回的时间，格式为(%YYYY年%Q季度)
    """
    # 首先把空格和空行删去
    file_time = str(file_time)
    file_time = file_time.strip('\n\r\t ')
    # 将字符串转为datetime格式
    try:
        file_time = datetime.datetime.strptime(file_time, '%Y/%m/%d')
        file_time_year = datetime.datetime.strftime(file_time, '%Y')
        file_time_month = datetime.datetime.strftime(file_time, '%m')
        file_time_month = quarter_map[file_time_month]
        file_time = file_time_year + 'Q' + file_time_month
    except ValueError:
        pass
    return file_time

def dataframe_filter(df, keywords, axis, status):
    """
    :param df: 要过滤的样本框
    :param keywords: list, 关键词清单: 要保留/剔除的 index 或 columns
    :param axis: integer, 过滤的方向，axis=0 为竖直方向，axis=1 为水平方向
    :param status: integer, 过滤的方式，status=0 为只保留清单中的数据，status=1 为只去除清单中的数据
    :return: 过滤好的样本框
    """
    df = df.copy()
    # 如果是只保留清单中的数据
    if status == 0:
        if axis == 1:
            lst = df.columns
            items = range(len(lst))
            with alive_bar(len(items), force_tty=True, bar='blocks') as bar:
                for item in lst:
                    if item not in keywords:
                        df.drop([item], axis=1, inplace=True)
                    bar()
        elif axis == 0:
            lst = df.index
            items = range(len(lst))
            with alive_bar(len(items), force_tty=True, bar='blocks') as bar:
                for item in lst:
                    if item not in keywords:
                        df.drop(item, axis=0, inplace=True)
                    bar()
    # 如果是只去除清单中的数据
    elif status == 1:
        if axis == 1:
            lst = df.columns
            items = range(len(lst))
            with alive_bar(len(items), force_tty=True, bar='blocks') as bar:
                for item in lst:
                    if item in keywords:
                        df.drop([item], axis=1, inplace=True)
                    bar()
        elif axis == 0:
            lst = df.index
            items = range(len(lst))
            with alive_bar(len(items), force_tty=True, bar='blocks') as bar:
                for item in lst:
                    if item in keywords:
                        df.drop(item, axis=0, inplace=True)
                    bar()
    else:
        raise KeyError('Axis not properly set')
    return df

def main():
    # 主程序运行，开始！
    # 读取文件
    os.chdir('E:/ANo.3/base')
    app1 = xw.App(visible=False, add_book=False)
    wb = app1.books.open('20210116样本分筛.xlsx')
    try:
        sht = wb.sheets('0次')
        df = pd.DataFrame(sht.used_range.value)
        df.columns = list(df.loc[0])
        df.drop(0, axis=0, inplace=True)
        df.reset_index(drop=True, inplace=True)
        # 记得把id变成整数，否则之后索引字符化后会有麻烦
        df['id'] = df['id'].apply(lambda x: int(x))
        # 提前插入一列频率，方便遍历时记录结果
        df.insert(4, 'freq', None)

        # 获取关键词清单
        f = open('add_words_dict.txt', 'r', encoding='UTF-8')
        n = f.read()
        f.close()
        n = n.split('\n')

        freqs = words_docs_freq(n, df)
        wb = app1.books.add()
        sht = wb.sheets('Sheet1')
        sht.name = '词频统计表'
        sht['A1'].value = freqs['DTM']
        sht = wb.sheets.add('词频统计明细')
        sht['A1'].value = freqs['DFC']
        wb.save('20210126简单词频统计.xlsx')
    finally:
        app1.quit()

def list_to_txt(name, content, sep = '\n'):
    """
    :param name: 指要创建的 txt 文件名称， 不存在的话会自动创建
    :param content: 指要写入的内容，此处必须为列表
    :param sep: 指列表的分隔符，默认为换行符
    :return: 无返回值
    这个函数可以把一个列表写入txt, 元素之间自动以换行符形式进行粘连
    """
    # 首先把列表使用分隔符进行粘连
    content = sep.join(content)
    # 使用with open方式打开，可以保证结束操作的同时关闭句柄
    with open(name, 'w', encoding='UTF-8') as f:
        f.write(content)

def txt_to_list(path, sep = '\n'):
    """
    :param path: 指要读取的 txt 文件所在位置， 不存在的话会报错，path 支持列表格式
    :param sep: 指列表的分隔符，默认为换行符
    :return: 返回一个列表
    这个函数可以把一个 txt 转化为列表 t, 导入的元素之间需要以制定换行符形式分隔开
    如果同时传入多个txt，则自动拼接在同一个列表当中
    """
    if path is not list:
        path = [path]
    lst = []
    for link in path:
        # 使用with open方式打开，可以保证结束操作的同时关闭句柄
        with open(link, 'r', encoding='UTF-8') as f:
            content = str(f.read())
        content = content.split(sep)
        try:
            content.remove('\n')
            content.remove('\t')
            content.remove('\r')
            content.remove('')
            content.remove(' ')
            content.remove('  ')
        except ValueError:
            pass
        lst.extend(content)
    return lst

def doc_filter(df, target):
    """
    :param df: 要进行样本筛选的数据框
    :param target: 符合要求的id列表
    :return: 只保留符合要求id对应样本的数据框
    """
    for doc_id in df['id']:
        if doc_id not in target:
            df.drop(df[df['id'] == doc_id].index, inplace=True)
    return df

def vec_cos(arr1, arr2):
    """
    :param arr1:  the first np.array
    :param arr2:  the second np.array
    :return:  cos(θ) (θ: the angle between two vectors)

    This measures the similarity between two vectors: the closer the value is to 1, the more similar between two vectors
    """
    num = np.sum(arr1 * arr2)
    den = np.sqrt(sum(arr1**2) * sum(arr2**2))
    sim = num/den
    return sim

def cos_rank(matrix, keymap=None, threshold=0.9):
    """
    :param matrix: (np.array) it contains all the keywords frequency vectors
    :param keymap: (dictionary) you can pass a preset dictionary if you wanna keep the original vector id
    :param threshold: (float) you can pass a threshold of cosθ above which the id will be recorded
    :return df: (pd.DataFrame) the rank of similarity

    This function splits the vectors into pairs and calculate the cos similarity pair by pair.
    Then it will generate a DataFrame that ranks the pairs from 1 to 0
    """
    pairs = []
    for i in range(0, len(matrix), 1):
        for t in range(i+1, len(matrix), 1):
            pairs.append((i, t))
    sim = []
    if keymap is None:
        for pair in pairs:
            sim.append((pair, vec_cos(matrix[pair[0]], matrix[pair[1]])))
    else:
        for pair in pairs:
            sim.append(((keymap[pair[0]], keymap[pair[1]]), vec_cos(matrix[pair[0]], matrix[pair[1]])))

    df = pd.DataFrame(sim)
    df.columns = ['id', 'Similarity']
    df.sort_values('Similarity', axis=0, ascending=False, inplace=True)

    # You can choose desired filtering condition
    df = df[df['Similarity'] > threshold]

    return df

def quarter2date(date):
    # 将字符串 date 转化为 datetime 格式
    quarter = int(re.findall('Q[0-9]', date)[0][1])
    month = {1: 1, 2: 4, 3: 7, 4: 10}
    rule = re.compile('Q[0-9]')
    year = rule.sub('', date)
    return datetime.date(int(year), month[quarter], 1)

if __name__ == '__main__':
    main()


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

from django.http import Http404
from proj.utils.mixin import JSONResponseMixin
from django.views.generic.base import View
from models import Options

class ChangeKuponatorStatusView(JSONResponseMixin, View):
    """
    Ajax Представления для изменения статуса настройки купонатор
    """

    def get_context_data(self, **kwargs):
        """
        изменение статуса
        """
        self.check_for_admin()
        obj, created = Options.objects.get_or_create(name='kuponator')
        if created:
            status = False
            obj.discription = u'Булевное занчение, определяет режим купонатор или нет'
        else:
            status = obj.status_bool
        if status:
            obj.status_bool = False
            button_text  = u'Включить'
            button_class = 'btn btn-primary'
        else:
            obj.status_bool = True
            button_text = u'Отключить'
            button_class = 'btn btn-inverse'
        obj.save()
        return [button_text, button_class]

    def check_for_admin(self):
        user = self.request.user
        try:
            pk = user.pk
        except AttributeError:
            raise Http404
        if not pk == 1:
            raise Http404

ChangeKuponatorStatusView = ChangeKuponatorStatusView.as_view()


import httplib2
from tkinter import *
import time
import main
import loadingWindow
import sys

class InitialWindow():

    userEnteredValues = []

    def __init__(self):
        self.window = Tk()
        self.root = Frame(self.window, height=300,width=300)
        self.root.pack()
        self.root.pack_propagate(0)

        #self.frameInitial = Frame()
        #self.frameInitial.pack()
        self.frame1 = Frame(self.root)
        self.frame1.pack()
        self.frame2 = Frame(self.root)
        self.frame2.pack()
        self.frame3 = Frame(self.root)
        self.frame3.pack()
        self.frame4 = Frame(self.root)
        self.frame4.pack()
        self.frame5 = Frame(self.root)
        self.frame5.pack()
        self.label = Label(self.root, text="PySpider GUI")
        self.label.pack()

        self.enter1 = Entry(self.frame1)
        self.enter1.pack(side=RIGHT)
        self.enter2 = Entry(self.frame2)
        self.enter2.pack(side=RIGHT)
        self.enter3 = Entry(self.frame3)
        self.enter3.pack(side=RIGHT)

        self.okButton = Button(self.frame5, text="OK",command=self.callback)
        self.okButton.pack(side=RIGHT)

        self.label1 = Label(self.frame1, text="Enter URL:                                  ")
        self.label1.pack(side=LEFT)
        self.label2 = Label(self.frame2, text="Enter type of data to look for:")
        self.label2.pack(side=LEFT)
        self.label3 = Label(self.frame3, text="Enter location to store data:   ")
        self.label3.pack(side=LEFT)

    def OKButtonReturn(self, term, searchTerm, whereToStore):
      global userEnteredValues
      userEnteredValues = [term, searchTerm, whereToStore]
      main.testGetUserValues = userEnteredValues
      print("the test variable ", main.testGetUserValues)

    def callback(self):
      entryBox1 = (self.enter1.get())
      entryBox2 = (self.enter2.get())
      entryBox3 = (self.enter3.get())

      try:
          h=httplib2.Http()
          resp, content=h.request(entryBox1) # this is where the problem is occuring
      except httplib2.RelativeURIError:
          warningBoxRoot2 = Tk()
          warningBoxLabel2 = Label(warningBoxRoot2, text="URL is malformed or website does not exist.")
          warningBoxLabel2.pack()
          warningBoxRoot2.mainloop()

      if entryBox1 is "" or entryBox2 is "" or entryBox3 is "":
          warningBoxRoot = Tk()
          warningBoxLabel = Label(warningBoxRoot, text="One of the fields was left blank. Please check entries.")
          warningBoxLabel.pack()
          warningBoxRoot.mainloop()

      self.OKButtonReturn(entryBox1, entryBox2, entryBox3)
      self.root.destroy()


import unittest

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from pages.login import LoginPage

class TestDjangoAdmin(unittest.TestCase):

    def setUp(self):
        options = Options()
        options.binary_location = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
        options.add_argument('--headless')
        options.add_argument('--disable-gpu')
        self.driver = webdriver.Chrome(chrome_options=options)
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(5)
        self.driver.set_page_load_timeout(30)
        self.driver.set_window_size(1920, 1080)

    def test_add_user(self):
        self.driver.get('http://127.0.0.1:8000/admin')
        login_page = LoginPage(self.driver)
        admin_page = login_page.login('admin', 'p@ssword')
        add_user_page = admin_page.go_to_add_user_page()
        user_page = add_user_page.add_user('testuser', 'dummy_p@ssword')
        self.assertIn('testuser" を追加しました。続けて編集できます。
', user_page.driver.page_source)

    def tearDown(self):
        self.driver.close()


import torch
import torch.nn as nn
import tools
from tools.data_utils import get_same_padding
from models.generator import MaskGen_CNN
from models.load_model import load_classifier,load_generator
import torch
import torchaudio
from tools.audio_utils import make_noise,SNR_cal
import os
import numpy as np

class impAug1(nn.Module):
	def __init__(self,args):
		super(impAug1, self).__init__()
		self.gen,_ = load_generator(args)
		self.classifier,_ = load_classifier(args)
		self.mask_option = args.mask_option
		self.wave_shape = args.wave_shape
		self.amptodB_transform = torchaudio.transforms.AmplitudeToDB(stype='magnitude')
		self.noise_1_weight = args.noise_1_weight
		self.noise_2_weight = args.noise_2_weight
		self.noise_3_weight = args.noise_3_weight
		self.gen_augment = args.gen_augment
		self.device = args.device
		self.args=args
		self.mask_shift = args.mask_shift
		self.gen_aug_type = args.gen_aug_type

	def frozen(self,m):
		m.eval()
		for p in m.parameters():
			p.requires_grad = False

	def unfrozen(self,m):
		m.train()
		for p in m.parameters():
			p.requires_grad = True

	def load_best_classifier(self,path):
		cpkt = torch.load(path)
		self.classifier.load_state_dict(cpkt['model_state_dict'])

	def forward(self,x,stft_transform,mask_forward,mask_add_aug=False):
		#figures_path ='./Figures/'

		if mask_forward:
			#torch.save(x.detach().cpu(),os.path.join(figures_path,'data.pt'))
			x= torch.view_as_complex(x)
			if not self.gen_augment:
				xdB = self.amptodB_transform(x.abs())
				mask = self.gen(xdB)
			else:
				if self.gen_aug_type == 1:
					noise3 = self.noise_3_weight * make_noise(self.wave_shape,x.shape[0],stft_transform,self.args).to(self.device)
					x = x +  noise3
					xdB = self.amptodB_transform(x.abs())
					mask = self.gen(xdB)
				elif self.gen_aug_type == 3:
					xdB = self.amptodB_transform(x.abs())
					mask = self.gen(xdB)
					#torch.save(mask.detach().cpu(),os.path.join(figures_path,'mask.pt'))
					ax = np.random.choice([2,3])
					shift = np.random.randint(-self.mask_shift,self.mask_shift)
					mask = torch.roll(mask, shift,ax)
					#torch.save(mask.detach().cpu(),os.path.join(figures_path,'mask_roll.pt'))
				elif self.gen_aug_type == 4:
					mask = torch.zeros(x.shape).to(self.device)
				elif self.gen_aug_type == 5:
					xdB = self.amptodB_transform(x.abs())
					mask = self.gen(xdB)

					ax = np.random.choice([2,3])
					shift = np.random.randint(-self.mask_shift,self.mask_shift)
					strategy = np.random.choice([0,1])
					if strategy ==0:
						#torch.save(mask.detach().cpu(),os.path.join(figures_path,'mask.pt'))
						mask = torch.roll(mask, shift,ax)
						#torch.save(mask.detach().cpu(),os.path.join(figures_path,'mask_roll.pt'))
					else:
						mask = torch.ones(x.shape).to(self.device)
			if self.mask_option ==1:
				if mask_add_aug:
					mask = mask * torch.rand(mask.shape).to(self.device)
				noise1 = self.noise_1_weight * make_noise(self.wave_shape,x.shape[0],stft_transform,self.args).to(self.device)
				x1 = x +  noise1
				noise2 = self.noise_2_weight * make_noise(self.wave_shape,x.shape[0],stft_transform,self.args).to(self.device)
				x2 = x1 +  mask * noise2

			elif self.mask_option == 2:
				if mask_add_aug:
					mask = mask * torch.rand(mask.shape)
				x1 = x
				x2 = x * mask + 1e-5
			elif self.mask_option == 3:
				noise = make_noise(self.wave_shape,x.shape[0],None,self.args).to(self.device)
				#torch.save(noise.detach().cpu(),os.path.join(figures_path,'noise.pt'))
				scale = (torch.mean(x.abs()**2)/(torch.mean(noise.abs()**2) * 10.0**(self.args.target_SNR_dB/10.0)))**0.5
				noise = noise * mask
				x1 = x
				x2 = x +  scale * noise
				#torch.save(x2.detach().cpu(),os.path.join(figures_path,'x2.pt'))
			elif self.mask_option == 4:
				#***********************************************************************************************
				# # last
				# # 1, 10, 20
				q = self.args.q
				num_q = np.percentile(mask.cpu().detach().numpy(), q, axis=[1, 2, 3])
				num_q = torch.from_numpy(np.array(num_q)).to(device=x.device, dtype=torch.float32)
				for i, mask_i in enumerate(mask):
					mask[i] = torch.where(mask_i>num_q[i], torch.ones_like(mask_i), torch.zeros_like(mask_i))
				# #************************************************************************************************
				noise = make_noise(self.wave_shape,x.shape[0],None,self.args).to(self.device)
				scale = (torch.mean(x.abs()**2)/(torch.mean(noise.abs()**2) * 10.0**(self.args.target_SNR_dB/10.0)))**0.5
				x1 = x
				x2 = x +  scale * noise
			else:
				x1 = x
				x2 = x *(1 + mask)
			out = self.classifier(x2)

			return out,mask,x1,x2
		else:
			out = self.classifier(x)
			return out


import pygame
from pygame.locals import*
import time
import math
from math import *
from OpenGL.GL import *
from OpenGL.GLU import *
import random

x = 0.01
y = 0
z = 0

angle = 0.01
display = (600,600)

counter = 0
def lorenz_attractor(x,y,z,counter):

    #parameters of lorenz attractor
    # let a be sigma
    # let b be rho
    # let c be beta
    dt = 0.01
    a = 10.0
    b = 28
    c = (8/3)
    dx = 0
    dy = 0
    dz = 0
    while(counter !=10000):
        glColor3d(1, 1, 1)
        #formula for the three differential equations
        dx = (a*(y - x)) * dt
        dy = (x*(b - z) - y) * dt
        dz = (x*y - c*z) * dt

        glVertex3d(x,y,z)

        x = x + dx
        y = y + dy
        z = z + dz
        counter+=1

def main(angle):
    #initialize pygame
    pygame.init()

    #set the display for the OpenGL
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    gluPerspective(45,(display[0]/display[1]), 0.1, 1500)

    glVertex3d(0, 0, -1)
    glRotatef(0,0,0,0)

    glTranslatef(0,0,0)

    clock = pygame.time.Clock()

    while (True) :
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        clock.tick(69)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

        #time.sleep(0.1)
        #pygame.time.wait(10)

        #zoom out, translate points and rotate
        if (angle<1):
            glRotatef(angle,-0.5,-angle,-5)
            glTranslatef(0,0,-0.5)

        #zoom in, translate points and rotate
        if (angle>1):
            glRotatef(0.9,5,1,-1)
            glTranslatef(0,0,0.01)

        glEnable(GL_POINT_SMOOTH)
        glPointSize(1)

        glBegin(GL_POINTS)

        lorenz_attractor(x, y, z, counter)

        glEnd()

        pygame.display.flip()
        angle+=0.005

main(angle)


import os
import json
import shutil
from pathlib import Path

for i in range(3):
    if i == 0:
        name = 'data_owner'
    elif i == 1:
        name = 'model_owner'
    else:
        name = 'ttp'

    new_dir_path = Path('role_{}_{}'.format(i, name))

    # clean the files
    if os.path.exists(new_dir_path):
        shutil.rmtree(new_dir_path)


# -*- coding:utf-8 -*-
from vcf.parser import _Info as VcfInfo, field_counts as vcf_field_counts
from multiprocessing import Queue, Process, Pool, cpu_count
from maxentpy.maxent import load_matrix5, load_matrix3
from maxentpy.maxent_fast import score5, score3
from functools import partial, reduce
from argparse import ArgumentParser
from collections import namedtuple
from itertools import count
import pandas as pd
import pyfaidx
import queue
import vcf
import os

ROOT = os.path.abspath(os.path.dirname(__file__))
ANNOTATION = os.path.join(ROOT, 'annotation')
VariantRecord = namedtuple('VariantRecord', ['chromosome', 'pos', 'ref', 'alt'])
VariantAnnotation = namedtuple('VariantAnnotation', ['location', 'exon_nearby', 'exon_start', 'exon_end', 'strand', 'gene'])

class MaxEntScan:
    def __init__(self, annotation, reference):
        self.annotation = pd.read_csv(os.path.join(ANNOTATION, annotation + '.exon.gff.tsv'), sep='\t', header=None, dtype={0: str})
        self.matrix5 = load_matrix5()
        self.matrix3 = load_matrix3()
        self.reference = pyfaidx.Fasta(reference)

    def mes_score(self, record):
        if not (record.ref in ['A', 'T', 'C', 'G', 'a', 't', 'c', 'g'] and record.alt in ['A', 'T', 'C', 'G', 'a', 't', 'c', 'g']):
            return record.alt + '|.|.|.|.'
        record_anno = MaxEntScan.variant_annotation(self.annotation, record, 0)
        seq_ss, seq_ss_mut, ss = '', '', ''
        if record_anno.location == 'exon':
            if record_anno.strand == '+':
                if record.pos + 2 >= record_anno.exon_end:
                    seq_ss, seq_ss_mut, ss = MaxEntScan.forward_exon_ss5(self.reference, record, record_anno.exon_end)
                elif record.pos - 2 <= record_anno.exon_start:
                    seq_ss, seq_ss_mut, ss = MaxEntScan.forward_exon_ss3(self.reference, record, record_anno.exon_start)
            else:
                if record.pos - 2 <= record_anno.exon_start:
                    seq_ss, seq_ss_mut, ss = MaxEntScan.reverse_exon_ss5(self.reference, record, record_anno.exon_start)
                elif record.pos + 2 >= record_anno.exon_end:
                    seq_ss, seq_ss_mut, ss = MaxEntScan.reverse_exon_ss3(self.reference, record, record_anno.exon_end)
            return '|'.join([record.alt, record_anno.gene, *MaxEntScan.score_ss_seq(self.matrix5, self.matrix3, seq_ss, seq_ss_mut, ss)])
        else:
            record_anno = MaxEntScan.variant_annotation(self.annotation, record, -6)
            if record_anno.exon_nearby:
                if record_anno.strand == '+':
                    seq_ss, seq_ss_mut, ss = MaxEntScan.forward_intron_ss5(self.reference, record, record_anno.exon_end)
                    return '|'.join([record.alt, record_anno.gene, *MaxEntScan.score_ss_seq(self.matrix5, self.matrix3, seq_ss, seq_ss_mut, ss)])
            record_anno = MaxEntScan.variant_annotation(self.annotation, record, 20)
            if record_anno.exon_nearby:
                if record_anno.strand == '+':
                    seq_ss, seq_ss_mut, ss = MaxEntScan.forward_intron_ss3(self.reference, record, record_anno.exon_start)
                    return '|'.join([record.alt, record_anno.gene, *MaxEntScan.score_ss_seq(self.matrix5, self.matrix3, seq_ss, seq_ss_mut, ss)])
            record_anno = MaxEntScan.variant_annotation(self.annotation, record, 6)
            if record_anno.exon_nearby:
                if record_anno.strand == '-':
                    seq_ss, seq_ss_mut, ss = MaxEntScan.reverse_intron_ss5(self.reference, record, record_anno.exon_start)
                    return '|'.join([record.alt, record_anno.gene, *MaxEntScan.score_ss_seq(self.matrix5, self.matrix3, seq_ss, seq_ss_mut, ss)])
            record_anno = MaxEntScan.variant_annotation(self.annotation, record, -20)
            if record_anno.exon_nearby:
                if record_anno.strand == '-':
                    seq_ss, seq_ss_mut, ss = MaxEntScan.reverse_intron_ss3(self.reference, record, record_anno.exon_end)
                    return '|'.join([record.alt, record_anno.gene, *MaxEntScan.score_ss_seq(self.matrix5, self.matrix3, seq_ss, seq_ss_mut, ss)])
            return '|'.join([record.alt, '.', *MaxEntScan.score_ss_seq(self.matrix5, self.matrix3, seq_ss, seq_ss_mut, ss)])

    @staticmethod
    def variant_annotation(annotation, record, offset):
        exon = annotation[(annotation[0] == record.chromosome) & (annotation[1] <= record.pos + offset) & (record.pos + offset <= annotation[2])]
        if exon.empty:
            location = 'intron'
            exon_nearby = False
            exon_start = None
            exon_end = None
            strand = None
            gene = None
        else:
            location = 'exon'
            exon_nearby = True
            exon_start = exon[1].values[0]
            exon_end = exon[2].values[0]
            strand = exon[3].values[0]
            gene = exon[4].values[0]
        return VariantAnnotation(location, exon_nearby, exon_start, exon_end, strand, gene)

    @staticmethod
    def forward_exon_ss5(reference, record, exon_end):
        seq_ss5 = str(reference.get_seq(record.chromosome, exon_end - 2, exon_end + 6))
        seq_ss5 = seq_ss5[0:3].lower() + seq_ss5[3:5] + seq_ss5[5:].lower()
        seq_ss5_mut = seq_ss5[0:record.pos - exon_end + 2] + record.alt + seq_ss5[record.pos - exon_end + 3:]
        return seq_ss5, seq_ss5_mut, 'ss5'

    @staticmethod
    def reverse_exon_ss5(reference, record, exon_start):
        seq_ss5 = str(reference.get_seq(record.chromosome, exon_start - 6, exon_start + 2).reverse.complement)
        seq_ss5 = seq_ss5[0:3].lower() + seq_ss5[3:5] + seq_ss5[5:].lower()
        seq_ss5_mut = seq_ss5[0:exon_start - record.pos + 2] + MaxEntScan.base_complement(record.alt) + seq_ss5[exon_start - record.pos + 3:]
        return seq_ss5, seq_ss5_mut, 'ss5'

    @staticmethod
    def forward_exon_ss3(reference, record, exon_start):
        seq_ss3 = str(reference.get_seq(record.chromosome, exon_start - 20, exon_start + 2))
        seq_ss3 = seq_ss3[0:18].lower() + seq_ss3[18:20] + seq_ss3[20:].lower()
        seq_ss3_mut = seq_ss3[0:20] + seq_ss3[20:record.pos - exon_start + 20] + record.alt + seq_ss3[record.pos - exon_start + 21:]
        return seq_ss3, seq_ss3_mut, 'ss3'

    @staticmethod
    def reverse_exon_ss3(reference, record, exon_end):
        seq_ss3 = str(reference.get_seq(record.chromosome, exon_end - 2, exon_end + 20).reverse.complement)
        seq_ss3 = seq_ss3[0:18].lower() + seq_ss3[18:20] + seq_ss3[20:].lower()
        seq_ss3_mut = seq_ss3[0:20] + seq_ss3[20:exon_end - record.pos + 20] + MaxEntScan.base_complement(record.alt) + seq_ss3[exon_end - record.pos + 21:]
        return seq_ss3, seq_ss3_mut, 'ss3'

    @staticmethod
    def forward_intron_ss5(reference, record, exon_end):
        seq_ss5 = str(reference.get_seq(record.chromosome, exon_end - 2, exon_end + 6))
        seq_ss5 = seq_ss5[0:3].lower() + seq_ss5[3:5] + seq_ss5[5:].lower()
        seq_ss5_mut = seq_ss5[0:3] + seq_ss5[3:record.pos - exon_end + 2] + record.alt + seq_ss5[record.pos - exon_end + 3:]
        return seq_ss5, seq_ss5_mut, 'ss5'

    @staticmethod
    def reverse_intron_ss5(reference, record, exon_start):
        seq_ss5 = str(reference.get_seq(record.chromosome, exon_start - 6, exon_start + 2).reverse.complement)
        seq_ss5 = seq_ss5[0:3].lower() + seq_ss5[3:5] + seq_ss5[5:].lower()
        seq_ss5_mut = seq_ss5[0:3] + seq_ss5[3:exon_start - record.pos + 2] + MaxEntScan.base_complement(record.alt) + seq_ss5[exon_start - record.pos + 3:]
        return seq_ss5, seq_ss5_mut, 'ss5'

    @staticmethod
    def forward_intron_ss3(reference, record, exon_start):
        seq_ss3 = str(reference.get_seq(record.chromosome, exon_start - 20, exon_start + 2))
        seq_ss3 = seq_ss3[0:18].lower() + seq_ss3[18:20] + seq_ss3[20:].lower()
        seq_ss3_mut = seq_ss3[0:record.pos - exon_start + 20] + record.alt + seq_ss3[record.pos - exon_start + 21:]
        return seq_ss3, seq_ss3_mut, 'ss3'

    @staticmethod
    def reverse_intron_ss3(reference, record, exon_end):
        seq_ss3 = str(reference.get_seq(record.chromosome, exon_end - 2, exon_end + 20).reverse.complement)
        seq_ss3 = seq_ss3[0:18].lower() + seq_ss3[18:20] + seq_ss3[20:].lower()
        seq_ss3_mut = seq_ss3[0:exon_end - record.pos + 20] + MaxEntScan.base_complement(record.alt) + seq_ss3[exon_end - record.pos + 21:]
        return seq_ss3, seq_ss3_mut, 'ss3'

    @staticmethod
    def score_ss_seq(matrix5, matrix3, seq_ss, seq_ss_mut, ss):
        if 'N' in seq_ss or 'N' in seq_ss_mut or 'n' in seq_ss or 'n' in seq_ss_mut:
            return '.', '.', '.'
        if ss == 'ss5':
            score_wt = score5(seq_ss, matrix=matrix5)
            score_mut = score5(seq_ss_mut, matrix=matrix5)
            return str(round(score_wt, 3)), str(round(score_mut, 3)), str(round((score_mut - score_wt) / score_wt, 3))
        elif ss == 'ss3':
            score_wt = score3(seq_ss, matrix=matrix3)
            score_mut = score3(seq_ss_mut, matrix=matrix3)
            return str(round(score_wt, 3)), str(round(score_mut, 3)), str(round((score_mut - score_wt) / score_wt, 3))
        else:
            return '.', '.', '.'

    @staticmethod
    def base_complement(dna_base):
        complement_rule = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C', 'a': 'T', 't': 'A', 'c': 'G', 'g': 'C'}
        return complement_rule[dna_base]

def split_df(df, split_num):
    df.reset_index(drop=True, inplace=True)
    df_list = list()
    step = round(df.shape[0]/split_num)
    for i in range(split_num):
        if i == 0:
            df_list.append(df.loc[0: step-1])
        elif i == split_num-1:
            df_list.append(df.loc[step*i:])
        else:
            df_list.append(df.loc[step*i:step*(i+1)-1])
    return df_list

def score_pseudo_vcf(annotation, reference, format_in, df):
    mes = MaxEntScan(annotation, reference)
    if format_in == 'vcf-4cols':
        df['MaxEntScan'] = df.apply(lambda x: mes.mes_score(VariantRecord(x['#CHROM'], x['POS'], x['REF'], x['ALT'])), axis=1)
    else:
        df['MaxEntScan'] = df.apply(lambda x: mes.mes_score(VariantRecord(x['#Chr'], x['Stop'], x['Ref'], x['Call'])), axis=1)
    return df

def annotation_pseudo_vcf(parsed_args, process_num):
    if parsed_args.format_in == 'vcf-4cols':
        df = pd.read_csv(parsed_args.file_in, sep='\t', dtype={'#CHROM': str})
    else:
        df = pd.read_csv(parsed_args.file_in, sep='\t', dtype={'#Chr': str})
    df_list = split_df(df, process_num)
    score_partial = partial(score_pseudo_vcf, parsed_args.annotation, parsed_args.reference, parsed_args.format_in)
    with Pool(process_num) as pool:
        df_list_score = pool.map(score_partial, df_list)
    df_score = reduce(lambda x, y: x.append(y), df_list_score)
    df_score.to_csv(parsed_args.file_out, sep='\t', index=False)

def score_vcf(records, results, annotation, reference):
    mes = MaxEntScan(annotation, reference)
    while True:
        try:
            record = records.get(False)
        except queue.Empty:
            continue
        if record != 'END':
            record_id, record_infos, record_score_list = record[0], record[1], list()
            for record_info in record_infos:
                record_score_list.append(mes.mes_score(record_info))
            results.put((record_id, ','.join(record_score_list)))
        else:
            records.put('END')
            break

def annotation_vcf(parsed_args, process_num):
    records, results = Queue(100 * process_num), Queue()
    input_finished = False
    output_finished = False
    wait_records = dict()
    processes = list()
    records_id = count()
    for i in range(process_num):
        p = Process(target=score_vcf, args=(records, results, parsed_args.annotation, parsed_args.reference))
        processes.append(p)
        p.start()
    vcf_reader = vcf.Reader(filename=parsed_args.file_in)
    vcf_reader.infos['MaxEntScan'] = VcfInfo('MaxEntScan', vcf_field_counts['A'], 'String',
                                             'MaxEntScan Score for VCF record alleles, related_score = (mut_score-wild_score)/wild_score Format: ALLELE|SYMBOL|wild_score|mut_score|related_score',
                                             version=None, source=None)
    vcf_writer = vcf.Writer(open(parsed_args.file_out, 'w'), vcf_reader)
    while True:
        while not records.full() and not input_finished:
            try:
                record = next(vcf_reader)
                record_id = next(records_id)
                wait_records[record_id] = record
                record_infos = list()
                chromosome = str(record.CHROM)
                pos = record.POS
                ref = record.REF
                for alt in record.ALT:
                    record_infos.append(VariantRecord(chromosome, pos, ref, str(alt)))
                records.put((record_id, record_infos))
            except StopIteration:
                input_finished = True
                records.put('END')
                break
        processes_status = list()
        for p in processes:
            processes_status.append(p.is_alive())
        if True not in processes_status:
            results.put('END')
        while True:
            try:
                result = results.get(False)
            except queue.Empty:
                break
            if result != 'END':
                record_id, record_score = result[0], result[1]
                record_write = wait_records.pop(record_id)
                record_write.add_info('MaxEntScan', record_score)
                vcf_writer.write_record(record_write)
            else:
                output_finished = True
                break
        if output_finished:
            break
    vcf_writer.close()

def main():
    parser = ArgumentParser()
    parser.add_argument('-r', help='Reference genome fasta file', required=True, dest='reference')
    parser.add_argument('-a', help='Genome annotation: GRCh37 or GRCh38', default='GRCh37', dest='annotation')
    parser.add_argument('-i', help='Input file', required=True, dest='file_in')
    parser.add_argument('-o', help='Output file', required=True, dest='file_out')
    parser.add_argument('--format_in', help='Input file format: vcf,vcf-4cols,bgianno', default='vcf-4cols', dest='format_in')
    parser.add_argument('-p', help='Process number', type=int, default=1, dest='processes')
    parsed_args = parser.parse_args()
    if parsed_args.annotation not in ['GRCh37', 'GRCh38']:
        raise Exception('Genome not recognized! Must be GRCh37 or GRCh38')
    if parsed_args.format_in not in ['vcf', 'vcf-4cols', 'bgianno']:
        raise Exception('Input file format not recognized! Must be vcf, vcf-4cols or bgianno')
    process_num = min(cpu_count(), parsed_args.processes)
    if parsed_args.format_in != 'vcf':
        annotation_pseudo_vcf(parsed_args, process_num)
    else:
        annotation_vcf(parsed_args, process_num)

if __name__ == '__main__':
    main()


import sys, functools, math
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
print = sys.stdout.write

def main():
    n, a, b, c, d = map(int, input().split())
    print(f"{min((n // a + [0, 1][n % a != 0]) * b, (n // c + [0, 1][n % c != 0]) * d)}\n")
    return

if __name__ == "__main__":
    main()


import re

file = "day-4-input.txt"

keys = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
eye_colors = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
valid = 0

def check_height(height):
    if height[-2:] == "cm":
        h = int(height[:-2])
        if h >= 150 and h <= 193:
            return True
    if height[-2:] == "in":
        h = int(height[:-2])
        if h >= 59 and h <= 76:
            return True
    return False

def check_hair(hair):
    if (
        hair[0] == "#" and
        len(hair[1:]) == 6 and
        re.match("[0-9|a-f]{6}", hair[1:])
    ):
        return True
    else:
        return False

with open(file, "r") as f:
    text = f.read()
    passports = text.split("\n\n")

for p in passports:
    fields = re.split(" |\n", p)
    data = {f.split(":")[0]: f.split(":")[1] for f in fields}
    if (
        all(key in data for key in keys) and
        (int(data["byr"]) >= 1920 and int(data["byr"]) <= 2002) and
        (int(data["iyr"]) >= 2010 and int(data["iyr"]) <= 2020) and
        (int(data["eyr"]) >= 2020 and int(data["eyr"]) <= 2030) and
        check_height(data["hgt"]) and
        check_hair(data["hcl"]) and
        data["ecl"] in eye_colors and
        re.match("[0-9]{9}", data["pid"]) and len(data["pid"]) == 9
    ):
        valid += 1

print(valid)


import json
import os

import toml

c_config_path = "./bin/develop/config.toml"
if os.environ.get('CONFIG'):
    c_config_path = os.environ['CONFIG']

with open(c_config_path, "r") as f:
    conf = toml.load(f)

a = json.dumps(conf, indent=4)
print(f"config = {a}")

aws_id_rsa = "./bin/develop/id_rsa"
aws_ip_node_list = conf["aws_ip_node_list"]
aws_ip_sync_list = conf["aws_ip_sync_list"]
aws_ip_send_list = conf["aws_ip_send_list"]

telegram_token = conf["telegram_token"]
telegram_chat_id = conf["telegram_chat_id"]

chain_param_interval = conf["chain_param_interval"]
chain_param_poolsize = conf["chain_param_poolsize"]
chain_param_timeout_gap = conf["chain_param_timeout_gap"]
chain_param_cycles_limit = conf["chain_param_cycles_limit"]
chain_param_tx_num_limit = conf["chain_param_tx_num_limit"]
chain_param_api_port = conf["chain_param_api_port"]
chain_param_p2p_port = conf["chain_param_p2p_port"]
chain_param_data_path = conf["chain_param_data_path"]
chain_param_rocksdb_max_openfile = conf["chain_param_rocksdb_max_openfile"]
chain_param_apm_service_name = conf["chain_param_apm_service_name"]
chain_param_apm_tracing_address = conf["chain_param_apm_tracing_address"]
chain_param_apm_tracing_batch_size = conf["chain_param_apm_tracing_batch_size"]
chain_param_logs_path = os.path.join(chain_param_data_path, "logs")

dockerhub_username = "mutadev"

muta_path = conf["muta_repo_path"]
remote_path = conf["remote_path"]

muta_genesis_template_path = os.path.join(muta_path, "devtools", "chain", "genesis.toml")
muta_config_template_path = os.path.join(muta_path, "devtools", "chain", "config.toml")

huobi_path = conf["huobi_repo_path"]
huobi_genesis_template_path = os.path.join(huobi_path, "config", "genesis.toml")
huobi_config_template_path = os.path.join(huobi_path, "config", "chain.toml")


from fastapi import FastAPI
from ray import serve
from transformers import LlamaTokenizer, LlamaForCausalLM, pipeline

app = FastAPI()

@serve.deployment(num_replicas=2, route_prefix="/biogpt")
class BioGPT():
    def __init__(self, max_new_tokens=200):
        self.pipeline = pipeline(
            model="microsoft/BioGPT-Large-PubMedQA",
            max_new_tokens=max_new_tokens,
            device_map="auto",
        )

    @app.get("infer")
    def infer(self, prompt: str) -> str:
        return self.pipeline(prompt)

@serve.deployment(num_replicas=2, route_prefix="/openllamamed")
class OpenLLAMAMed():
    def __init__(self, max_new_tokens=200):
        model_path = 'yhyhy3/open_llama_7b_v2_med_dolphin_qlora_merged'

        self.tokenizer = LlamaTokenizer.from_pretrained(model_path)
        self.model = LlamaForCausalLM.from_pretrained(
            model_path, torch_dtype=torch.float16, device_map='auto',
        )

    @app.get("infer")
    def infer(self, question: str) -> str:
        prompt = f'''### Instruction: Answer the following question.

### Input: {question}

### Response:'''
        return self.pipeline(prompt)

biogpt = BioGPT.bind()
models = OpenLLAMAMed.bind(biogpt)


import numpy as np
from typing import List

class Node:
    def __init__(self,value):
        self.value = value
        self.left = None
        self.right = None

class BinaryTree:
    def __init__(self,head):
        self.head = head
        self.left= None
        self.right= None

    def insert(self,key):
        self.current_node = self.head

        while True:
            if key < self.current_node.value:
                if self.current_node.left != None:
                    self.current_node = self.current_node.left
                else :
                    self.current_node.left = Node(key)
                    break
            else :
                if self.current_node.right !=None:
                    self.current_node = self.current_node.right
                else :
                    self.current_node.right = Node(key)
                    break

    def path(self,key):
        self.current_node = self.head
        path_list = []
        way_list = []
        while key>1:
            if key%2 ==0:
                path_list.append(int(key/2))
                way_list.append(1)
            else :
                path_list.append(int((key-1)/2))
                way_list.append(-1)
            key = int(key/2)
        return np.flip(path_list), np.flip(way_list)

def index_to_key(num):
    return num+34

# def key_to_index(num):
#     return num-34

class DeepWalkHier:
    def __init__(self,
                 adj_matrix,
                 embedding_dim=2,
                 walks_per_vertex=5,
                 walk_len=10,
                 window_size=3,
                 learning_rate=0.02):

        self.adj_matrix = adj_matrix
        self.embedding_dim = embedding_dim
        self.walks_per_vertex = walks_per_vertex
        self.walk_len = walk_len
        self.window_size = window_size
        self.learning_rate = learning_rate

        self.w1 = np.random.rand(len(adj_matrix),embedding_dim)

        self.loss = 0.0
        self.epoch_loss = []
        self.epoch_loss_de = len(self.adj_matrix)

        # randomly initialize
        # make binary tree
        self.vec = []
        no_use = np.random.rand(embedding_dim)
        start = np.random.rand(embedding_dim)
        self.vec.append(no_use)
        self.vec.append(start)
        head = Node(1)
        self.h_softmax_tree = BinaryTree(head)

        V = len(adj_matrix)
        for key in range(2,2*V):
            tree_node = np.random.rand(embedding_dim)
            self.vec.append(tree_node)
            self.h_softmax_tree.insert(key)

    def _sigmoid(self, x):
        return 1/(1+np.exp(-x))

    def _random_walk(self, start_node: int)-> List:
        walk = [0] * self.walk_len
        walk[0] = start_node
        node = start_node
        for i in range(1, self.walk_len):
            next_node = np.random.choice(np.where(self.adj_matrix[node]==1)[0])
            walk[i] = next_node
            node = next_node
        return walk

    def _skip_gram_train(self, walk: List)-> None:
        for idx, input_node in enumerate(walk):
            # make dataset
            left_idx = idx - self.window_size
            right_idx = idx + self.window_size
            if left_idx < 0: left_idx = 0
            if right_idx > self.walk_len-1: right_idx = self.walk_len
            left_node = walk[left_idx:idx]
            right_node = walk[idx+1:right_idx+1]
            output_nodes = left_node + right_node

            # train
            hidden = self.w1[input_node]
            for output_node in output_nodes:
                # get path
                path, left_right = self.h_softmax_tree.path(index_to_key(output_node))
                tmp = [self.vec[i] for i in path] @ hidden
                # calcuate epoch loss
                self.loss = - np.sum(np.log(self._sigmoid(tmp * left_right))) / self.epoch_loss_de
                # backprop, optimization
                left_right = [1 if i==1 else 0 for i in left_right]
                EH = 0
                for i, path_val in enumerate(path):
                    tmp = self._sigmoid(self.vec[path_val] @ hidden) - left_right[i]
                    EH += self.vec[path_val] * tmp
                    self.vec[path_val] = self.vec[path_val] - self.learning_rate * tmp * hidden
                self.w1[input_node] = self.w1[input_node] - self.learning_rate * EH

    def train(self)-> float:
        V = np.arange(0, len(self.adj_matrix))
        for _ in range(self.walks_per_vertex):
            # shuffle vertex
            np.random.shuffle(V)
            for start_node in V:
                # random walk
                W = self._random_walk(start_node)
                # skip-gram
                self._skip_gram_train(W)
            # consider epoch as if all node be start_node
            # = consider epoch as walks_per_vertex
            self.epoch_loss.append(self.loss)
            self.loss = 0.0
        return self.epoch_loss

    def show_embedding(self):
        return self.w1

Comparison in DeepWalk, MF/deepWalkHier.py


import tkinter as tk

def Addition():
    no1 = E1.get()
    no1=int(no1)
    no2 = E2.get()
    no2=int(no2)
    ans = int(no1 + no2)
    ans=int(ans)
    ans1=tk.Label(Main,text=ans)
    ans1.grid(row=5,column=1)

def Subtraction():
    no1 = int(E1.get())
    no2 = int(E2.get())
    ans = int(no1) - int(no2)
    L3.config('Subtraction - ',ans)

def Multiplication():
    no1 = int(E1.get())
    no2 = int(E2.get())
    ans = int(no1) * int(no2)
    L3.config('Multiplication - ',ans)

def Division():
    no1 = int(E1.get())
    no2 = int(E2.get())
    ans = int(no1) / int(no2)
    L3.config('Division - ',ans)

Main = tk.Tk()
Main.title("Calculation")

L1 = tk.Label(Main, text = 'Enter First Number - ')
L1.grid(row = 0, column = 0)

E1 = tk.Entry(Main)
E1.grid(row = 0, column = 1)

L2 = tk.Label(Main, text = 'Enter Second Number - ')
L2.grid(row = 1, column = 0)

E2 = tk.Entry(Main)
E2.grid(row = 1, column = 1)

B1 = tk.Button(Main, text = 'ADD', command = Addition)
B1.grid(row = 2, column = 0)

B2 = tk.Button(Main, text = 'SUB', command = Subtraction)
B2.grid(row = 2, column = 1)

B3 = tk.Button(Main, text = 'MULTI', command = Multiplication)
B3.grid(row = 3, column = 0)

B4 = tk.Button(Main, text = 'DIV', command = Division)
B4.grid(row = 3, column = 1)

L3 = tk.Label(Main, text = ' ')
L3.grid(row = 4, column = 0)

L4=tk.Label(Main,text="Ans is :")
L4.grid(row=5,column=0)

Main.mainloop()


# -*- coding: utf-8 -*-
"""
Created on Tue Dec 17 22:42:13 2019

@author: ricar
"""
from __future__ import unicode_literals
import numpy as np
import os
import matplotlib.pyplot as plt
from os import listdir
from os.path import isfile, join
import librosa
import librosa.display
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import scale
from keras.models import load_model
import matplotlib.pyplot as plt
import librosa.display
import pickle
import youtube_dl

def predict_musical_genre(url):
    #Ruta muestras de audio. 100 audios .wav por género para 10 géneros musicales
    audio_path = "C:/Users/ricar/OneDrive/Documentos/Bootcamp_Data_Science/Machile_Learning/Proyecto/Datasets/gtzan-genres/audios/"

    os.chdir(audio_path)
    os.listdir()

    #Lista de las 10 clases
    clases=[]
    for i in os.listdir():
        clases.append(i)

    dir_proyecto = "C:/Users/ricar/OneDrive/Documentos/Bootcamp_Data_Science/Machile_Learning/Proyecto/"
    os.chdir(dir_proyecto)
    os.listdir()
    #Download Audio Files from Youtube

    #url_youtube_song=input("Introduce la dirección url de Youtube de la canción cuyo género deseas saber: ")

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'wav',
            'preferredquality': '192',
        }],
    }

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
        result = ydl.extract_info(url, download=False)
        video = result['entries'][0] if 'entries' in result else result
        titulo=video['title']
        idv=video['id']
        song_dir=dir_proyecto+titulo+"-"+idv+".wav"

    song=song_dir
    y, sr = librosa.load(song, mono=True)
    stft = librosa.stft(y)
    stft_db = librosa.amplitude_to_db(abs(stft))
    spectogram=np.abs(librosa.stft(y))
    tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
    chroma_stft = librosa.feature.chroma_stft(y=y, sr=sr)
    chroma_cqt = librosa.feature.chroma_cqt(y=y, sr=sr)
    rms = librosa.feature.rms(S=spectogram)
    spectral_centroid = librosa.feature.spectral_centroid(y=y, sr=sr)
    spectral_bandwidth = librosa.feature.spectral_bandwidth(y=y, sr=sr)
    spectral_contrast = librosa.feature.spectral_contrast(y=y, sr=sr)
    tonnetz = librosa.feature.tonnetz(y=y, sr=sr)
    zero_crossing_rate = librosa.feature.zero_crossing_rate(y=y)
    tempogram = librosa.feature.tempogram(y=y, sr=sr)
    fourier_tempogram = librosa.feature.fourier_tempogram(y=y, sr=sr)

    header_list=['tempo', 'chroma_stft_mean', 'chroma_cqt_mean', 'rms_mean',
           'spectral_centroid_mean', 'spectral_bandwidth_mean',
           'spectral_contrast_mean', 'tonnetz_mean', 'zero_crossing_rate_mean',
           'tempogram_mean', 'fourier_tempogram_mean', 'mfcc_mean', 'mfcc1_mean',
           'mfcc2_mean', 'mfcc3_mean', 'mfcc4_mean', 'mfcc5_mean', 'mfcc6_mean',
           'mfcc7_mean', 'mfcc8_mean', 'mfcc9_mean', 'mfcc10_mean', 'mfcc11_mean',
           'mfcc12_mean', 'mfcc13_mean', 'mfcc14_mean', 'mfcc15_mean',
           'mfcc16_mean', 'mfcc17_mean', 'mfcc18_mean', 'mfcc19_mean',
           'mfcc20_mean']

    predict_set = pd.DataFrame(np.nan, index=range(1), columns=header_list)
    id=0
    predict_set.loc[id, predict_set.columns[0]]=tempo
    predict_set.loc[id, predict_set.columns[1]]=np.mean(chroma_stft)
    predict_set.loc[id, predict_set.columns[2]]=np.mean(chroma_cqt)
    predict_set.loc[id, predict_set.columns[3]]=np.mean(rms)
    predict_set.loc[id, predict_set.columns[4]]=np.mean(spectral_centroid)
    predict_set.loc[id, predict_set.columns[5]]=np.mean(spectral_bandwidth)
    predict_set.loc[id, predict_set.columns[6]]=np.mean(spectral_contrast)
    predict_set.loc[id, predict_set.columns[7]]=np.mean(tonnetz)
    predict_set.loc[id, predict_set.columns[8]]=np.mean(zero_crossing_rate)
    predict_set.loc[id, predict_set.columns[9]]=np.mean(tempogram)
    predict_set.loc[id, predict_set.columns[10]]=np.mean(np.abs(fourier_tempogram))

    mfcc = librosa.feature.mfcc(y=y, sr=sr)
    predict_set.loc[id, predict_set.columns[11]]=np.mean(mfcc)

    col=12
    for number, mfccs in enumerate(mfcc):
        predict_set.loc[id, predict_set.columns[col+number]]=np.mean(mfccs)

    X_test= np.array(predict_set)

    modelf=load_model("C:/Users/ricar/OneDrive/Documentos/Bootcamp_Data_Science/Machile_Learning/Proyecto/best_model_feat_10classes.h5")
    scaler_train_10 = pickle.load( open( "scaler_train_10.p", "rb" ) )
    X_test=scaler_train_10.transform(X_test)
    prediccion=modelf.predict_classes(X_test)[0]
    dic_clases={}
    for i , j in enumerate(clases):
        dic_clases[i]=j
    genero=dic_clases[prediccion]
    print("El género de la cancion ", titulo +" es: ", genero)
    return genero, titulo


from setuptools import setup, find_packages
from os import path

here = path.join(path.abspath(path.dirname(__file__)), 'garpix_notify')

with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='garpix_notify',
    version='5.16.2',
    description='',
    long_description=long_description,
    long_description_content_type='text/markdown',
    url='https://github.com/garpixcms/garpix_notify',
    author='Garpix LTD',
    author_email='info@garpix.com',
    license='MIT',
    packages=find_packages(exclude=['testproject', 'testproject.*']),
    classifiers=[
        'Development Status :: 4 - Beta',
        'Environment :: Web Environment',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Framework :: Django',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
    ],
    include_package_data=True,
    zip_safe=False,
    install_requires=[
        'Django >= 1.11',
        'django-solo >= 1.1.3',
        'fcm-django == 0.3.11',
        'requests >= 2.18.4',
        'django-ckeditor >= 5.6.1',
        'python-telegram-bot >= 12.6.1',
        'viberbot >= 1.0.11',
        'django-uuslug >= 1.2.0',
        'Pillow >= 8.2.0',
        'celery>=4.4.2',
        'redis >= 3.5.3',
        'channels >= 3.0.3, <= 3.0.4',
        'channels-redis == 3.4.0',
        'asgiref >= 3.2.10, <= 3.3.4',
        'twilio == 7.10.0',
        'typing-extensions >= 4.3.0',
        'djangorestframework >= 3.12.4',
        'garpix_utils >= 1.8.0',
        'rarfile >= 4.0',
        'unrar >= 0.4'
    ],
)


from flask import render_template, redirect,url_for,request, flash
from .. import routes
from database import mysql

@routes.route('/')
#Añadiendo id_factura id_servicio a los detalles de la factura
@routes.route('/admin/<username>/mascota/add_color', methods=['POST'])
def add_detalle_factura(username):
    try:
        if request.method == 'POST':
            id_factura = request.form['id_factura']
            servicios = request.form['id_servicios']
            estado = True
            for servicio in servicios:
                cur = mysql.connection.cursor()
                cur.execute("INSERT INTO detalle_factura(id_factura,id_servicio,estado) VALUES (%s,%s,%s)",(id_factura,servicio,estado))
                mysql.connection.commit()
            flash('Servicio agregado a la factura','success')
            return redirect('/')
    except Exception as e:
        flash('Error al agregar el servicio','danger')
        print("No funciono ",e)
        return redirect('/')


#!/usr/bin/env python
# -*- coding: utf8 -*-
# *****************************************************************
# **       PTS -- Python Toolkit for working with SKIRT          **
# **       © Astronomical Observatory, Ghent University          **
# *****************************************************************

# Import the relevant PTS classes and modules
from pts.core.basics.configuration import ConfigurationDefinition
from pts.modeling.tests.NGC4013.test import fitting_filter_names, free_parameters, free_parameter_labels

# -----------------------------------------------------------------

# Create definition
definition = ConfigurationDefinition(write_config=False)

# Simulation
definition.add_optional("npackages", "positive_integer", "number of photon packages", int(1e4))

# Free parameters
definition.add_optional("free_parameters", "string_list", "list of free parameters", free_parameter_labels, choices=free_parameters)

# Fitting filters
definition.add_optional("fitting_filters", "filter_list", "filters for the fitting", fitting_filter_names, convert_default=True)

# Genetic algorithm settings
definition.add_optional("ngenerations", "positive_integer", "number of generations", 5)
definition.add_optional("nmodels", "positive_integer", "number of models per generation", 10)
definition.add_optional("mutation_rate", "positive_real", "mutation rate", 0.03)
definition.add_optional("crossover_rate", "positive_real", "crossover rate", 0.65)

# Other
definition.add_optional("kernel_type", "string", "convolution kernel type", "Gaussian")
definition.add_optional("kernel_dimension", "positive_integer", "kernel dimension", 6)

# -----------------------------------------------------------------


from typing import Any, Callable, Dict, List

from jsonschema import validate

from pykechain.enums import PropertyType, _AllRepresentations
from pykechain.exceptions import IllegalArgumentError
from pykechain.models.representations.representation_base import BaseRepresentation
from pykechain.models.validators.validator_schemas import representation_jsonschema_stub

class RepresentationsComponent:
    """
    Aggregate class to use representations on an object.

    To add representations to a Pykechain class, create an instance of this class in its __init__() method.

    .. versionadded:: 3.7
    """

    def __init__(
        self, parent_object, representation_options: Dict, update_method: Callable
    ):
        """
        Extract the json with the representation options.

        :param parent_object: Object to which this representation component is attached
        :param representation_options: json with list of representations
        :param update_method: method of the parent_object that is used to update the representations
        """
        self._parent_object = parent_object
        self._repr_options = representation_options
        self._update_method: Callable = update_method

        # Construct representation objects
        self._representations: List["AnyRepresentation"] = []
        representations_json = self._repr_options
        for representation_json in representations_json:
            self._representations.append(
                BaseRepresentation.parse(
                    obj=self._parent_object, json=representation_json
                )
            )

    def get_representations(self) -> List["AnyRepresentation"]:
        """
        Get list of representation objects.

        :return: list of Representations
        :raises IllegalArgumentError if representations are set with incorrect options
        """
        return self._representations

    def set_representations(self, representations: List["AnyRepresentation"]) -> None:
        """Set the representations."""
        self._validate_representations(representations)

        # set the internal representation list
        self._representations = list(set(representations))

        # dump to _json options
        self._dump_representations()

        # update the options to KE-chain backend
        self._update_method(self._repr_options)

    def _validate_representations(self, representations: Any):
        """Check provided representation inputs."""
        if not isinstance(representations, (tuple, list)):
            raise IllegalArgumentError(
                "Should be a list or tuple with Representation objects, got {}".format(
                    type(representations)
                )
            )

        for r in representations:
            if not isinstance(r, BaseRepresentation):
                raise IllegalArgumentError(
                    f"Representation '{r}' should be a Representation object"
                )
            if not _valid_object_type(r, self._parent_object):
                raise IllegalArgumentError(
                    f"Representation '{r}' can not be added to "
                    f"'{self._parent_object}' as the representation is not "
                    "allowed on that propertyType. Please check relation between allowed"
                    "representation and property type accordingly."
                )
            r.validate_json()

    def _dump_representations(self):
        """Dump the representations as json inside the _repr_options dictionary."""
        representations_json = []
        for r in self._representations:
            json_format = r.as_json()
            validate(json_format, representation_jsonschema_stub)
            representations_json.append(json_format)

        self._repr_options = representations_json

def _valid_object_type(representation: BaseRepresentation, obj: "Base") -> bool:
    """
    Check whether the representation can be used on the provided object.

    :param representation: representation to check
    :type representation: BaseRepresentation
    :param obj: object to attach the representation to
    :type obj: Base
    :return: True if feasible, False if not.
    :rtype bool
    """
    rtype = representation.rtype
    if rtype == _AllRepresentations.CUSTOM_ICON:
        from pykechain.models import Activity, Scope

        return isinstance(obj, (Activity, Scope))
    else:
        from pykechain.models import Property

        if not isinstance(obj, Property):
            return False
        else:
            if rtype == _AllRepresentations.BUTTON:
                return obj.type in (
                    PropertyType.SINGLE_SELECT_VALUE,
                    PropertyType.MULTI_SELECT_VALUE,
                )
            elif rtype == _AllRepresentations.DECIMAL_PLACES:
                return obj.type == PropertyType.FLOAT_VALUE
            elif rtype == _AllRepresentations.SIGNIFICANT_DIGITS:
                return obj.type == PropertyType.FLOAT_VALUE
            elif rtype == _AllRepresentations.THOUSANDS_SEPARATOR:
                return obj.type in (PropertyType.INT_VALUE, PropertyType.FLOAT_VALUE)
            elif rtype == _AllRepresentations.LINK_TARGET:
                return obj.type == PropertyType.LINK_VALUE
            elif rtype == _AllRepresentations.AUTOFILL:
                return obj.type in (
                    PropertyType.DATETIME_VALUE,
                    PropertyType.DATE_VALUE,
                    PropertyType.TIME_VALUE,
                    PropertyType.USER_REFERENCES_VALUE,
                )
            elif rtype == _AllRepresentations.GEOCOORDINATE:
                return obj.type == PropertyType.GEOJSON_VALUE
            elif rtype == _AllRepresentations.USE_PROPERTY_NAME:
                return obj.type in (
                    PropertyType.REFERENCES_VALUE,
                    PropertyType.ACTIVITY_REFERENCES_VALUE,
                    PropertyType.USER_REFERENCES_VALUE,
                    PropertyType.SCOPE_REFERENCES_VALUE,
                    PropertyType.SERVICE_REFERENCES_VALUE,
                )
            elif rtype == _AllRepresentations.CAMERA_SCANNER_INPUT:
                return obj.type in (
                    PropertyType.INT_VALUE,
                    PropertyType.FLOAT_VALUE,
                    PropertyType.TEXT_VALUE,
                    PropertyType.CHAR_VALUE,
                )
            elif rtype == _AllRepresentations.SIGNATURE:
                return obj.type == PropertyType.ATTACHMENT_VALUE
            elif rtype == _AllRepresentations.FILE_DISPLAY:
                return obj.type == PropertyType.STOREDFILE_REFERENCES_VALUE
            else:
                return False


from django.shortcuts import render, redirect
import requests
import json

# Create your views here.
def index(request):

    ip = requests.get('https://api.ipify.org?format=json')
    ip_data= json.loads(ip.text)
    response = requests.get('http://ip-api.com/json/'+ ip_data["ip"])
    response_data= response.text
    location_data= json.loads(response_data)
    return render(request, 'base.html', {'data': location_data})