#!/usr/bin/env python2

import numpy.random as npr
from collections import defaultdict
from collections import deque
from random import sample
from math import ceil
import math_tools
import json

##########################################
#
# COMMUNITIES
#
# This class contains the community information of the network.
# This information can eithe be read from the LFR-output or
# deduced from the affinity output of the c++ algorithm.
#
# Community information can be accessed in two different ways:
# * The dict "vertexToCommunities" maps each vertex to the communities it belongs to.
# * The dict "communityToVertices" maps each community to the vertices which are part of it.

class Communities(object):

    def __init__(self):
        self.vertexToCommunities = None
        self.communityToVertices = None
        self.numberOfCommunities = None

    def reverseMapping(self, inputDict):
        outputDict = defaultdict(list)
        # remap value list as keys, keys as values
        for key, listOfKey in inputDict.iteritems():
            for element in listOfKey:
                outputDict[element].append(key)
        return outputDict

    # input and output
    # ----------------

    # Read community information from the LFR community file.
    # The input is a file which where each line consists
    # of a nodeid and then a list of communities this node belongs to
    # Ids of nodes and communities in the LFR file start at 1,
    # we subtract 1 to let those ids start at 0.
    def readCommunitiesLFR(self, filename):
        self.vertexToCommunities = defaultdict(list)
        with open (filename, "r") as f:
            belongings = [[(int(x) - 1) for x in line.split()] for line in f.readlines()]
            for belonging in belongings:
                self.vertexToCommunities[belonging[0]] = belonging[1:]
        self.communityToVertices = self.reverseMapping(self.vertexToCommunities)
        self.numberOfCommunities = len(self.communityToVertices)

    #the same as readCommunitiesLFR but reads our file format instead
    #TODO: what format are you talking about?
    #TODO: this function seems buggy: vertices are not cast to int
    def readCommunitiesOurFormat(self, filename):
        self.communityToVertices = defaultdict(list)
        with open (filename, "r") as f:
            for community,line in enumerate(f):
                for vertex in line.split():
                    self.communityToVertices[community].append(vertex)
        self.vertexToCommunities = self.reverseMapping(self.communityToVertices)
        self.numberOfCommunities = len(self.communityToVertices)

    # Write output community file.
    # Each line represents one community and lists all the vertices in this community.
    def writeCommunitesOurFormat(self, filename):
        with open (filename, "w") as f:
            for vertices in self.communityToVertices.values():
                f.write(" ".join([str(x) for x in vertices]) + "\n")

    # Write seed-information to file in affinity-fileformat.
    # seeds is a set of nodeids
    def writeSeedsOurFormat(self, seeds, filename):
        if seeds is None:
            raise Exception("seeds need to be generated first")
        with open (filename, "w") as f:

            f.write("{0} {1}".format(len(seeds), self.numberOfCommunities))
            for seed in seeds:
                tmp = [0] * self.numberOfCommunities
                for community in self.vertexToCommunities[seed]:
                    tmp[community] = 1
                f.write("\n" + str(seed) + " " + " ".join([str(x) for x in tmp]))

    # methods for generating seeds
    # ----------------------------

    # Generate seed nodes from a community object.
    # Pick a fraction of "seedFraction" nodes from each community.
    # The number of seeds per community is at least 1 and rounded to the next bigger integer
    def generateSeedsUniformly(self, seedFraction):
        seeds = set()
        for c in self.communityToVertices:
            if seedFraction == 0:
                seedCount = 1
            else:
                seedCount = int(ceil(seedFraction * len(self.communityToVertices[c])))
            seeds = seeds.union(sample(self.communityToVertices[c], seedCount))
        return seeds

    def generateSeedsDegreeBased(self, graph, seedFraction):
        seeds = set()

        for c, vertices in self.communityToVertices.iteritems():
            communityDegree = 0

            probabilities = list()
            for vertex in vertices:
                communityDegree += len(graph.vertexToNeighbours[vertex])

            for vertex in vertices:
                probabilities.append(len(graph.vertexToNeighbours[vertex]) / float(communityDegree))

            if seedFraction == 0:
                seedCount = 1
            else:
                seedCount = int(ceil(seedFraction * len(self.communityToVertices[c])))

            tmp = math_tools.choice(vertices, seedCount, replace=False, p=probabilities)
            seeds = seeds.union(tmp)
        return seeds

    # stuff
    # ----------------------------

    #Find position of biggest gap
    def _getGap(self,affinityVector):
        affinitytuple = list()

        #create new data structure with community id and correspodning affinity
        for i, affinity in enumerate(affinityVector):
            affinitytuple.append((i,affinity))

        affinitytuple = sorted(affinitytuple,key=lambda item: item[1], reverse=True)

        #Find position of maximum gap
        maxDiff = 0
        maxDiffPosition = -1
        for i in xrange(0,len(affinitytuple[:-1])):
            currentDiff = affinitytuple[i][1] - affinitytuple[i+1][1]
            if currentDiff > maxDiff:
                maxDiff = currentDiff
                maxDiffPosition = i

        first_half = affinitytuple[:maxDiffPosition+1]
        communities = list()
        for element in first_half:
            communities.append(element[0])
        return communities, maxDiffPosition, maxDiff

    def _getJSONOfVertex(self,affinities, groundTruth, vertex):
        affinity_vector = affinities.vertexToAffinities[vertex]
        deviation = math_tools.standartDeviation(affinity_vector)
        communities_according_to_gap,gap_position ,gap_size = self._getGap(affinity_vector)

        actual_communities = groundTruth.vertexToCommunities[vertex]
        detected_communities = self.vertexToCommunities[vertex]

        return {
            "nodeid":vertex,
            "actual_communities":actual_communities,
            "actual_number_of_communities":len(actual_communities),
            "detected_communities":detected_communities,
            "detected_number_of_communities":len(detected_communities),

            "gap_position":gap_position,
            "standard_deviation":deviation,
            "affinities":affinity_vector,
            "gap_size":gap_size,
            "communities_according_to_gap":communities_according_to_gap}

    def writeJSONfile(self, filename, affinities, groundTruth):
        output = list()
        for vertex in self.vertexToCommunities:
            output.append(self._getJSONOfVertex(affinities, groundTruth, vertex))
        with open (filename, "w") as f:
            f.write(json.JSONEncoder(indent=4).encode({"body": output}))

    # classification
    # --------------

    # Classify communities from the affinity output of the c++ algorithm.
    # Simply assign the vertex the the community with the maximum affinity-value.
    def classifyCommunities(self, affinities):
        communitiesPerVertex = 1
        self.vertexToCommunities = defaultdict(list)
        for vertex, affinities in affinities.vertexToAffinities.iteritems():
            maxIndex = max([(v,i) for i,v in enumerate(affinities)])[1]
            self.vertexToCommunities[vertex] = [maxIndex]
        self.communityToVertices = self.reverseMapping(self.vertexToCommunities)
        self.numberOfCommunities = len(self.communityToVertices)

    # Classify communities from the affinity output of the c++ algorithm.
    # Assign communities according to "gap-strategy"
    def classifyCommunitiesWithGaps(self, affinities):
        self.vertexToCommunities = defaultdict(list)
        for vertex, affinities in affinities.vertexToAffinities.iteritems():
            communities,gapPosition,gapSize = self._getGap(affinities)
            self.vertexToCommunities[vertex] = communities
        self.communityToVertices = self.reverseMapping(self.vertexToCommunities)
        self.numberOfCommunities = len(self.communityToVertices)

    def classifyCommunitiesGroundTruth(self, affinities, groundTruth):
        self.vertexToCommunities = defaultdict(list)
        for vertex, affinities in affinities.vertexToAffinities.iteritems():
            actual_number_of_communities = len(groundTruth.vertexToCommunities[vertex])
            # Put affinities with their index (the corresponding community) in tuple
            # and sort them by first entry (affinity).
            # Then extract the second entry (the community) from each tuple.
            sortedTuples = sorted([(v,i) for i,v in enumerate(affinities)], reverse=True)
            communities = [i for (v,i) in sortedTuples][:actual_number_of_communities]
            self.vertexToCommunities[vertex] = communities
        self.communityToVertices = self.reverseMapping(self.vertexToCommunities)
        self.numberOfCommunities = len(self.communityToVertices)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import numpy as np
from scipy.io import loadmat
from sklearn import preprocessing  # 0-1编码
from sklearn.model_selection import StratifiedShuffleSplit  # 随机划分，保证每一类比例相同

class preprocess(object):

    def __init__(self):
        self.d_path='/Users/tung/Python/WorkProject/PHMresearch/WDCNN&LR_FaultDiagnosis/data/0HP'
        self.length=2048
        self.number=200
        self.normal=True
        self.rate=[0.5, 0.25, 0.25]
        self.enc = True
        self.enc_step = 28
        self.filenames = []

    def capture(self):
        """读取mat文件，返回字典

        :param original_path: 读取路径
        :return: 数据字典
        """
        files = {}
        for i in self.filenames:
            # 文件路径
            file_path = os.path.join(self.d_path, i)
            file = loadmat(file_path)
            file_keys = file.keys()
            for key in file_keys:
                if 'DE' in key:
                    files[i] = file[key].ravel()
        return files

    def slice_enc(self, data):
        """将数据切分为前面多少比例，后面多少比例.

        :param data: 单挑数据
        :param slice_rate: 验证集以及测试集所占的比例
        :return: 切分好的数据
        """
        slice_rate = self.rate[1] + self.rate[2]
        keys = data.keys()
        Train_Samples = {}
        Test_Samples = {}
        for i in keys:
            slice_data = data[i]
            all_lenght = len(slice_data)
            end_index = int(all_lenght * (1 - slice_rate))
            samp_train = int(self.number * (1 - slice_rate))  # 700
            Train_sample = []
            Test_Sample = []
            if self.enc:
                enc_time = self.length // self.enc_step
                samp_step = 0  # 用来计数Train采样次数
                for j in range(samp_train):
                    random_start = np.random.randint(low=0, high=(end_index - 2 * self.length))
                    label = 0
                    for h in range(enc_time):
                        samp_step += 1
                        random_start += self.enc_step
                        sample = slice_data[random_start: random_start + self.length]
                        Train_sample.append(sample)
                        if samp_step == samp_train:
                            label = 1
                            break
                    if label:
                        break
            else:
                for j in range(samp_train):
                    random_start = np.random.randint(low=0, high=(end_index - self.length))
                    sample = slice_data[random_start:random_start + self.length]
                    Train_sample.append(sample)

            # 抓取测试数据
            for h in range(self.number - samp_train):
                random_start = np.random.randint(low=end_index, high=(all_lenght - self.length))
                sample = slice_data[random_start:random_start + self.length]
                Test_Sample.append(sample)
            Train_Samples[i] = Train_sample
            Test_Samples[i] = Test_Sample
        return Train_Samples, Test_Samples

    # 仅抽样完成，打标签
    def add_labels(self, train_test):
        X = []
        Y = []
        label = 0
        for i in self.filenames:
            x = train_test[i]
            X += x
            lenx = len(x)
            Y += [label] * lenx
            label += 1
        return X, Y

    # one-hot编码
    def one_hot(self, Train_Y, Test_Y):
        Train_Y = np.array(Train_Y).reshape([-1, 1])
        Test_Y = np.array(Test_Y).reshape([-1, 1])
        Encoder = preprocessing.OneHotEncoder()
        Encoder.fit(Train_Y)
        Train_Y = Encoder.transform(Train_Y).toarray()
        Test_Y = Encoder.transform(Test_Y).toarray()
        Train_Y = np.asarray(Train_Y, dtype=np.int32)
        Test_Y = np.asarray(Test_Y, dtype=np.int32)
        return Train_Y, Test_Y

    def scalar_stand(self, Train_X, Test_X):
        # 用训练集标准差标准化训练集以及测试集
        scalar = preprocessing.StandardScaler().fit(Train_X)
        Train_X = scalar.transform(Train_X)
        Test_X = scalar.transform(Test_X)
        return Train_X, Test_X

    def valid_test_slice(self, Test_X, Test_Y):
        test_size = self.rate[2] / (self.rate[1] + self.rate[2])
        ss = StratifiedShuffleSplit(n_splits=1, test_size=test_size)
        for train_index, test_index in ss.split(Test_X, Test_Y):
            X_valid, X_test = Test_X[train_index], Test_X[test_index]
            Y_valid, Y_test = Test_Y[train_index], Test_Y[test_index]
            return X_valid, Y_valid, X_test, Y_test

    def prepro(self, d_path, length, number, normal, rate, enc, enc_step):
        self.d_path=d_path
        self.length=length
        self.number=number
        self.normal=normal
        self.rate=rate
        self.enc=enc
        self.enc_step=enc_step

        # 获得该文件夹下所有.mat文件名
        self.filenames = os.listdir(self.d_path)
        del self.filenames[2]   #删除.DS_Store

        # 从所有.mat文件中读取出数据的字典
        data = self.capture()
        # 将数据切分为训练集、测试集
        train, test = self.slice_enc(data)
        # 为训练集制作标签，返回X，Y
        Train_X, Train_Y = self.add_labels(train)
        # 为测试集制作标签，返回X，Y
        Test_X, Test_Y = self.add_labels(test)
        # 为训练集Y/测试集One-hot标签
        Train_Y, Test_Y = self.one_hot(Train_Y, Test_Y)
        # 训练数据/测试数据 是否标准化.
        if normal:
            Train_X, Test_X = self.scalar_stand(Train_X, Test_X)
        else:
            # 需要做一个数据转换，转换成np格式.
            Train_X = np.asarray(Train_X)
            Test_X = np.asarray(Test_X)
        # 将测试集切分为验证集合和测试集.
        Valid_X, Valid_Y, Test_X, Test_Y = self.valid_test_slice(Test_X, Test_Y)
        return Train_X, Train_Y, Valid_X, Valid_Y, Test_X, Test_Y

if __name__ == "__main__":

    """对数据进行预处理,返回train_X, train_Y, valid_X, valid_Y, test_X, test_Y样本.

    :param d_path: 源数据地址
    :param length: 信号长度，默认5个信号周期T=400*10S=66.6min～1h，2048
    :param number: 每种信号个数,总共9类,默认每个类别1000个数据
    :param normal: 是否标准化.True,Fales.默认True
    :param rate: 训练集/验证集/测试集比例.默认[0.5,0.25,0.25],相加要等于1
    :param enc: 训练集、验证集是否采用数据增强.Bool,默认True
    :param enc_step: 增强数据集采样顺延间隔
    :return: Train_X, Train_Y, Valid_X, Valid_Y, Test_X, Test_Y

    """
    #传感器研发组 做实验、售后去现场维护、采回来的数据
    #溶液地未连接(100)、流通池接地(100)、电缆线未连接(100)、球泡破裂(35)、支架损坏(31)、
    #电极污染(36）、电解液缺失(50)、水样波动(100)、正常
    #重采样之后 200*9 = 1800

    path = '/Users/tung/Python/WorkProject/PHMresearch/WDCNN&LR_FaultDiagnosis/data/0HP'
    test = preprocess()
    train_X, train_Y, valid_X, valid_Y, test_X, test_Y = test.prepro(d_path=path,
                                                                length=2048,
                                                                number=200,
                                                                normal=False,
                                                                rate=[0.7, 0.2, 0.1],
                                                                enc=False,
                                                                enc_step=28)
    print('训练样本维度:', train_X.shape)
    print(train_X.shape[0], '训练样本个数')
    print('训练标签one-hot维度',train_Y.shape[1])
    print('验证样本的维度', valid_X.shape)
    print(valid_X.shape[0], '验证样本个数')
    print('测试样本的维度', test_X.shape)
    print(test_X.shape[0], '测试样本个数')


from typing import List

class Solution:
    def sortArrayByParity(self, nums: List[int]) -> List[int]:
        e = []
        odd = []

        for n in nums:
            if(n%2 == 0):
                e.append(n)
            else:
                odd.append(n)
        for o in odd:
            e.append(o)

        return e

nums = [3,1,2,4]
#nums = [0]

print(Solution.sortArrayByParity(Solution, nums))


# Este exemplo esta contido no livro
# Introdução à Programação com Python - 3ª Edição
# Prof. Nilo Ney Coutinho Menezes

salário = float(input("Digite o salário para cálculo do imposto: "))
base = salário
imposto = 0

if base >3000:
    imposto = imposto + ((base-300) * 0.35)
    base = 3000

if base > 1000:
    imposto = imposto + ((base - 1000) * 0.20)
    print(f"Salário: R${salário:6.2f} Imposto a pagar: R$ {imposto: 6.2f}")


#Ejercicio 2: Dados dos parámetros enteros A y B, obtener AB (A elevado a la B) mediante multiplicacionessucesivas, utilizando la función del ejercicio 1.

#Funcion multiplicación dada por suma:
def multSuma(a):
    i = 1
    suma = 0
    while i <= a:
        suma = suma + a
        i = i + 1
    return suma

#Programa Principal:
x = int(input("Ingrese un numero: "))

y = int(input("Ingrese la potencia por la que multiplicará: "))

resultado = 1

for i in range(y):
    resultado = resultado * (multSuma(x)//x)

print("El resultado de la multiplicación hecha por suma es: ", resultado)


'''当用户输入敏感词语时，则打印出 Freedom，否则打印出 Human Rights。'''
def open_file(filepath):
    senstivite_word = []
    with open(filepath,'r',encoding='utf-8') as f:
        for line in f.readlines():
            senstivite_word.append(line.strip())
    return senstivite_word

def user_input():
    input_word = input('请输入你想搜索的内容：')
    return input_word
'''
def filtered_word(senstivite_word,input_word):
    for word in senstivite_word:
        if input_word.find(word) != -1:
           out_string = 'Freedom'
           return out_string
        else:
            out_string = 'Human Rights'
    return out_string

def display(out_string):
    print(out_string)
'''
def filtered_word(senstivite_word,input_word):
    flag = 0
    for word in senstivite_word:
        if input_word.find(word) != -1:
          flag = 1
    return flag

def display(flag):
    if flag == 0:
        print('Human Rights')
    else:
        print('Freedom')

if __name__ == '__main__':
    filepath = "../practice/doc/filtered_words.txt"
    content = open_file(filepath)
    input_word = user_input()
    out_string = filtered_word(content,input_word)
    display(out_string)


import collections
import sys

import pytest

from extendable import context, main, registry

@pytest.fixture
def test_registry() -> registry.ExtendableClassesRegistry:
    reg = registry.ExtendableClassesRegistry()
    initial_class_defs = main._extendable_class_defs_by_module
    try:
        main._extendable_class_defs_by_module = collections.OrderedDict()
        token = context.extendable_registry.set(reg)
        yield reg
    finally:
        main._extendable_class_defs_by_module = initial_class_defs
        context.extendable_registry.reset(token)

@pytest.fixture
def sys_modules_cleanup() -> None:
    keys = [k for k in sys.modules.keys() if k.startswith("tests.mod_")]
    for k in keys:
        del sys.modules[k]
    try:
        yield
    finally:
        keys = [k for k in sys.modules.keys() if k.startswith("tests.mod_")]
        for k in keys:
            del sys.modules[k]


import json
from datetime import datetime

import scrapy

from util import get_dates_between
from bs4 import BeautifulSoup
import csv

class BaseballSpider(scrapy.Spider):
    name = "baseball"
    start_urls = ["http://sports.news.naver.com/"]

    def __init__(self):
        start_datetime = datetime(2022, 1, 1)
        end_datetime = datetime(2022, 12, 31)
        self.target_dates = get_dates_between(start_datetime, end_datetime)
        self.article_list_url = "https://sports.news.naver.com/kbaseball/news/list?isphoto=N&date={date}&page={page}"
        self.article_url = "https://sports.news.naver.com/news?oid={oid}&aid={aid}"
        self.output_file = open("./baseball.csv", "w")
        self.writer = csv.writer(self.output_file)
        self.writer.writerow(["url", "datetime", "title", "content"])

    def parse(self, response):
        for target_date in self.target_dates:
            target_url = self.article_list_url.format(date=target_date, page=1)
            req = scrapy.Request(
                url=target_url,
                callback=self.parse_total_pages
            )
            req.meta["date"] = target_date
            yield req

    def parse_total_pages(self, response):
        date = response.meta["date"]
        resp_json = json.loads(response.text)
        total_pages = resp_json["totalPages"]
        for i in range(total_pages):
            req = scrapy.Request(
                url=self.article_list_url.format(date=date, page=i+1),
                callback=self.parse_article_list
            )
            req.meta["date"] = date
            yield req

    def parse_article_list(self, response):
        date = response.meta["date"]
        resp_json = json.loads(response.text)
        for item in resp_json["list"]:
            oid = item["oid"]
            aid = item["aid"]
            req = scrapy.Request(
                url=self.article_url.format(oid=oid, aid=aid),
                callback=self.parse_article
            )
            req.meta["date"] = date
            yield req

    def parse_article(self, response):
        url = response.request.url
        date = response.meta["date"]

        def _remove_tags(parent_soup, target_tag):
            tags = parent_soup.find_all(target_tag)
            for tag in tags:
                tag.decompose()

        soup = BeautifulSoup(response.text, "lxml")
        title = soup.find("h4", class_="title").get_text()
        content_soup = soup.find("div", id="newsEndContents")
        _remove_tags(content_soup, "p")
        _remove_tags(content_soup, "div")
        _remove_tags(content_soup, "em")
        _remove_tags(content_soup, "span")
        content = content_soup.get_text().strip()
        self.writer.writerow([url, date, title, content])


"""
TASK
Given 2 sets of integers, M and N, print their difference in ascending order. The term symmetric
difference indicates values that exist in eirhter M or N but do not exist in both.
INPUT
The first line of input contains integer M.
The second line containts M space separeted integers.
The third line contains an integer N
The fourth line contains N space separated integers.
CONSTANTS
-----
OUTPUT
Output the symmetric difference integers an ascending order, one per line
SAMPLE INPUT
4
2 4 5 9
4
2 4 11 12
SAMPLE OUTPUT
5
9
11
12
EXPLANATION
-----
"""
M = int(input())
MM = set([int(x) for x in input().split()])
N = int(input())
NN = set([int(x) for x in input().split()])

difference = MM.symmetric_difference(NN)
data = list(difference)
data.sort()

# sorted(list)
for x in data:
    print(x)


import numpy as np
from rl.memory.linear import LinearMemory
from rl.util import log_self
import math

class HighLowMemory(LinearMemory):

    '''
    Memory divided into two: good and bad experiences
    As with RankedMemory experiences are grouped episodically
    Episodes with a total reward > threshold are assigned to good memory
    The threshold is recomputed every n episodes and
    episodes are reassigned accordingly.
    Memories are sampled from good experiences with a self.prob_high
    Memories are sampled from bad experiences with a 1 - self.prob_high
    Experiences are sampled from a maximum of 3 randomly selected episodes,
    per minibatch for each of the high and low memories
    TODO improvement: do a more natural continuous range to sort high low
    by self.epi_memory.sort(key=lambda epi_exp: epi_exp['total_rewards'])
    '''

    # absorb generic param without breaking
    def __init__(self, env_spec, **kwargs):
        super(HighLowMemory, self).__init__(env_spec)
        # use the old self.exp as buffer, remember to clear
        self.last_exp = self.exp
        self.epi_memory_high = []
        self.epi_memory_low = []
        self.max_reward = -math.inf
        self.min_reward = math.inf
        # 1st  5 epis goes into bad half, recompute every 5 epis
        self.threshold = math.inf
        self.threshold_history = []
        self.epi_num = 0
        self.prob_high = 0.66
        self.num_epis_to_sample = 3
        self.max_epis_in_mem = 15
        self.recompute_freq = 10
        log_self(self)

    def reassign_episodes(self):
        new_high, new_low = []

        for mem in (self.epi_memory_high, self.epi_memory_low):
            for epi_exp in mem:
                if (epi_exp['total_rewards'] > self.threshold):
                    new_high.append(epi_exp)
                else:
                    new_low.append(epi_exp)

        self.epi_memory_high = new_high
        self.epi_memory_low = new_low

    def compute_threshold(self):
        self.threshold_history.append([self.threshold,
                                       self.max_reward,
                                       self.min_reward])
        if (len(self.threshold_history) > 1):
            # Scaled because this threshold seems too severe based on trial
            # runs
            self.threshold =  \
                max(self.threshold,
                    (self.max_reward + self.min_reward) / 2.0 * 0.75)
        else:
            self.threshold = (self.max_reward + self.min_reward) / 2.0 * 0.75
        self.reassign_episodes()
        self.max_reward = -math.inf
        self.min_reward = math.inf

    def add_exp(self, action, reward, next_state, terminal):
        super(HighLowMemory, self).add_exp(
            action, reward, next_state, terminal)
        if terminal:
            epi_exp = {
                'exp': self.exp,
                'total_rewards': np.sum(self.exp['rewards']),
                'epi_num': self.epi_num
            }
            if (epi_exp['total_rewards'] <= self.threshold):
                self.epi_memory_low.append(epi_exp)
            else:
                self.epi_memory_high.append(epi_exp)
            if (self.epi_num > 0 and self.epi_num % self.recompute_freq == 0):
                self.compute_threshold()
            if (epi_exp['total_rewards'] > self.max_reward):
                self.max_reward = epi_exp['total_rewards']
            if (epi_exp['total_rewards'] < self.min_reward):
                self.min_reward = epi_exp['total_rewards']
            self.last_exp = self.exp
            self.exp = {k: [] for k in self.exp_keys}
            self.epi_num += 1
            # print("THRESHOLD HISTORY")
            # print(self.threshold_history)
            # print("HIGH MEM")
            # for epi in self.epi_memory_high:
            #     print(str(epi['total_rewards'])+ " ,", end=" ")
            # print()
            # print("LOW MEM")
            # for epi in self.epi_memory_low:
            #     print(str(epi['total_rewards'] )+ " ,", end=" ")
            # print()

    def pop(self):
        '''convenient method to get exp at [last_ind]'''
        buffer_exp = self.exp  # store for restore later
        self.exp = self.last_exp
        res = super(HighLowMemory, self).pop()
        self.exp = buffer_exp
        return res

    def rand_minibatch(self, size):
        # base case, early exit
        high_samples = np.int(np.ceil(size * self.prob_high))
        low_samples = size - high_samples

        if (len(self.epi_memory_high) == 0 and
                len(self.epi_memory_low) == 0):
            return super(HighLowMemory, self).rand_minibatch(size)

        if (len(self.epi_memory_high) == 0):
            high_samples = 0
            low_samples = size

        high_samples_per_epi = np.int(
            np.ceil(high_samples / self.num_epis_to_sample))
        low_samples_per_epi = np.int(
            np.ceil(low_samples / self.num_epis_to_sample))

        buffer_exp = self.exp
        minibatch_as_list = []
        if high_samples > 0:
            for _i in range(4):
                idx = np.random.randint(0, len(self.epi_memory_high))
                epi_exp = self.epi_memory_high[idx]['exp']
                self.exp = epi_exp
                epi_minibatch = super(HighLowMemory, self).rand_minibatch(
                    high_samples_per_epi)
                minibatch_as_list.append(epi_minibatch)

        if low_samples > 0:
            for _i in range(4):
                idx = np.random.randint(0, len(self.epi_memory_low))
                epi_exp = self.epi_memory_low[idx]['exp']
                self.exp = epi_exp
                epi_minibatch = super(HighLowMemory, self).rand_minibatch(
                    low_samples_per_epi)
                minibatch_as_list.append(epi_minibatch)

        # set buffer back to original
        self.exp = buffer_exp

        # merge all minibatches from best_epi_memory into a minibatch
        minibatch = {}
        for k in self.exp_keys:
            k_exp = np.concatenate(
                [epi_exp[k] for epi_exp in minibatch_as_list]
            )[-size:]
            minibatch[k] = k_exp
        assert len(
            minibatch['rewards']) == size, 'minibatch has the wrong size'

        return minibatch

    def update(self, updates):
        pass

class HighLowMemoryWithForgetting(HighLowMemory):

    '''
    Like HighLowMemory but also has forgetting capability
    Controlled by max_epis_in_mem param
    '''

    # absorb generic param without breaking
    def __init__(self, env_spec, **kwargs):
        super(HighLowMemoryWithForgetting, self).__init__(env_spec)
        self.max_epis_in_mem = 250
        log_self(self)

    def reassign_episodes(self):
        new_high, new_low = []

        for mem in (self.epi_memory_high, self.epi_memory_low):
            for epi_exp in mem:
                if (self.epi_num - epi_exp['epi_num'] <= self.max_epis_in_mem):
                    if (epi_exp['total_rewards'] > self.threshold):
                        new_high.append(epi_exp)
                    else:
                        new_low.append(epi_exp)

        self.epi_memory_high = new_high
        self.epi_memory_low = new_low


#Question1.
def evenly_divisible(a, b, c):
    l1 = []
    for i in range(a, b + 1):
        if i % c == 0:
            l1.append(i)
    print("the sum of evenly divisible :", sum(l1))
    if sum(l1) == 0:  # means there is  no number in list .
        return "No number between {} and {} can be evenly divided by {}".format(a, b, c)

print(evenly_divisible(1, 10, 3))
print("------------------------------------------------------------------------------------------------------------")

#Question2.
def expression_check(s):
    check=eval(s)
    return check
"""eval() function: eval() is not much used due to security reasons:
>You may want to use it to allow users to enter their own “scriptlets”:
small expressions can be use with this function to evaluate the expression.
>eval is also sometimes used in applications needing to evaluate math expressions
>** The data pass inside the eval() function must be in string format."""

print(expression_check("3<7<11"))
print(expression_check("13>44>33>1"))
print(expression_check("1<2<6<9>3"))

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

#Question3.
def replace_vowels(string,char):
    vowels="AEIOUaeiou"
    count=0
    for i in vowels:
        if i in string:
            string=string.replace(i,char)
            count=count+1
    if count==0:
        print("No vowels are there in this string")
    return string

s="the aardvark&quot"
print(replace_vowels(s,"#"))

print("------------------------------------------------------------------------------------------------------------")
#Question4.
 ##method 1:
def factorial(num):
    if type(num)==int:
        fact = 1
        for i in range(1, num + 1):
            fact = fact * i
        return "Factorial of {} is --->{}".format(num, fact)

print(factorial(10))

#method2:
import math
def get_factorial(num):
    f=math.factorial(num)
    return "Factorial of {} is --->{}".format(num,f)
print("------------------------------------------------------------------------------------------------------------")

#Question 5
"""You are given two strings of equal length, you have to find the Hamming Distance between these string.
Where the Hamming distance between two strings of equal length is the number of "same positions" at two string
at which the corresponding character is different."""
def Humming(s1,s2):
    l=[]
    for i in range(len(s1)): #or range(len(s2)) bcz both have equal length.
        if s1[i]!=s2[i]:
            l.append(i)
    c=len(l)
    return "The humming distance {},{} is--->{}".format(s1,s2,c)

s1="strong"
s2="strung"
print(Humming(s1,s2))

17) Python Basics Programming Assignment_17.py


from typing import Optional

import click
from pipelines import (
    seq_classifier_train_eval_pipeline,
    token_classifier_train_eval_pipeline,
)
from steps import (
    data_importer,
    load_tokenizer,
    sequence_classifier_tokenization,
    sequence_evaluator,
    sequence_trainer,
    token_classification_tokenization,
    token_evaluator,
    token_trainer,
)
from steps.configuration import HuggingfaceParameters

@click.command()
@click.option(
    "--nlp_task",
    type=click.Choice(["token-classification", "sequence-classification"]),
    default="sequence-classification",
    help="Name NLP task i.e. token-classification, sequence-classification",
)
@click.option(
    "--pretrained_model",
    default="distilbert-base-uncased",
    help="Pretrained model name from huggingface hub",
)
@click.option(
    "--batch_size",
    default=8,
    help="Batch Size for training",
)
@click.option(
    "--full_set",
    is_flag=True,
    help="By default only a very small subset of the datasets is used in order "
    "to have a quick end-to-end run. By running with the full datasets "
    "the runtime increases significantly.",
)
@click.option(
    "--epochs",
    default=1,
    help="Number of epochs for training",
)
@click.option(
    "--max_seq_length",
    default=128,
    help="Max sequence length for training",
)
@click.option(
    "--init_lr",
    default=2e-5,
    help="Initial learning rate for training",
)
@click.option(
    "--weight_decay_rate",
    default=0.01,
    help="Weight decay rate for training",
)
@click.option(
    "--text_column",
    help="Column name for text in the dataset. i.e. For sequence "
    "classification, this will be text and for token classification, "
    "this will be tokens",
)
@click.option(
    "--label_column",
    help="Column name for label in the dataset. i.e For sequence"
    " classification, this will be label and for token classification, "
    "this will be ner_tags",
)
@click.option(
    "--dataset_name",
    help="Name of the dataset to be used. i.e For sequence classification, "
    "this will be imdb and for token classification, this will be "
    "conll2003",
)
def main(
    nlp_task: str,
    pretrained_model: str,
    batch_size: int,
    epochs: int,
    full_set: bool,
    max_seq_length: int,
    init_lr: float,
    weight_decay_rate: float,
    text_column: Optional[str] = None,
    label_column: Optional[str] = None,
    dataset_name: Optional[str] = None,
):
    if nlp_task == "token-classification":
        if not text_column:
            text_column = "tokens"
        if not label_column:
            label_column = "ner_tags"
        if not dataset_name:
            dataset_name = "conll2003"

        token_classification_config = HuggingfaceParameters(
            label_all_tokens=True,
            pretrained_model=pretrained_model,
            epochs=epochs,
            batch_size=batch_size,
            max_seq_length=max_seq_length,
            init_lr=init_lr,
            weight_decay_rate=weight_decay_rate,
            dummy_run=not full_set,
            dataset_name=dataset_name,
            text_column=text_column,
            label_column=label_column,
        )
        pipeline = token_classifier_train_eval_pipeline(
            importer=data_importer(token_classification_config),
            load_tokenizer=load_tokenizer(token_classification_config),
            tokenization=token_classification_tokenization(
                token_classification_config
            ),
            trainer=token_trainer(token_classification_config),
            evaluator=token_evaluator(token_classification_config),
        )
        pipeline.run()

    elif nlp_task == "sequence-classification":
        if not text_column:
            text_column = "text"
        if not label_column:
            label_column = "label"
        if not dataset_name:
            dataset_name = "imdb"

        sequence_classification_config = HuggingfaceParameters(
            pretrained_model=pretrained_model,
            epochs=epochs,
            batch_size=batch_size,
            dummy_run=not full_set,
            max_seq_length=max_seq_length,
            init_lr=init_lr,
            weight_decay_rate=weight_decay_rate,
            dataset_name=dataset_name,
            text_column=text_column,
            label_column=label_column,
        )
        pipeline = seq_classifier_train_eval_pipeline(
            importer=data_importer(sequence_classification_config),
            load_tokenizer=load_tokenizer(sequence_classification_config),
            tokenization=sequence_classifier_tokenization(
                sequence_classification_config
            ),
            trainer=sequence_trainer(sequence_classification_config),
            evaluator=sequence_evaluator(sequence_classification_config),
        )
        pipeline.run()

if __name__ == "__main__":
    main()


class Coche:
    def __init__(self, marca, modelo):
        self.marca = marca
        self.modelo = modelo
        self.arrancado = False

    def arrancar(self):
        self.arrancado = True
        print("El", self.marca, self.modelo, "se ha arrancado")

    def arrancar(self):
        self.arrancado = False
        print("El", self.marca, self.modelo, "se ha parado")

laguna = Coche("Ranalt", "Laguna")
tesla = Coche("Tesla", "Model 3")

print(type(laguna))
print(type(tesla))

print(laguna.marca, laguna.modelo)
print(tesla.marca, tesla.modelo)


from flask import Flask, jsonify, Request
from flask_cors import CORS
import os
import current_patient_service as cps
import json

app = Flask(__name__)
CORS(app)

@app.route("/api/get_shap_interpretation_by_session_id/<session_id>",
            methods=["GET"])
def get_cardiac_cpet_intepretation_by_id(session_id):
    """API that returns a base64 of the force plot and custom shap summary

    This method validates the input and calls a method that will process a
    list of values that represents the time and voltage of an ecg signal.
    It plots the information and enconde it as a base64 string and without
    storing it into the database.

    Args:
        in_data (str): session id

    Returns
        str,str, int: SHAP and force plot image as a base64 str, 200
        str, int: Error Message, 400
    """
    try:
        shap_custom_result = cps.get_interpretation_images_by_id(session_id)
        #res, code = hr.get_record_by_patient_id(patient_id)
        return json.dumps(vars(shap_custom_result[0])), 200
    except Exception as e:
        print(e)
        return "Unexpected error", 400
    pass

@app.route("/api/get_dynamic_record_by_session_id/<session_id>",
            methods=["GET"])
def get_dynamic_cpet_by_session_id(session_id):
    """API that retrieves all the cpet reocrds of a session

    This method validates the input and calls a method that will retrieve
    a list of all the health records of a patient, if there is one exists.
    The result will be a list of dictionaries containing the information

    Args:
        session_id (str): The id of the health record

    Returns
        `list` of `dict`, int: All patient's medical records, 200
        str, int: Error Message, 400
    """
    try:
        result = cps.get_dynamic_cpet_record_by_session_id(session_id)
        #res, code = hr.get_record_by_patient_id(patient_id)
        return json.dumps(vars(result[0])), 200
    except Exception as e:
        print(e)
        return "Unexpected error", 400
    pass

@app.route("/api/get_record_by_patient_id/<session_id>",
           methods=["GET"])
def get_cpet_record_by_session_id(session_id):
    """API that retrieves all the cpet reocrds of a session

    This method validates the input and calls a method that will retrieve
    a list of all the health records of a patient, if there is one exists.
    The result will be a list of dictionaries containing the information

    Args:
        session_id (str): The id of the health record

    Returns
        `list` of `dict`, int: All patient's medical records, 200
        str, int: Error Message, 400
    """
    try:
        result = cps.get_cpet_record_by_session_id(session_id)
        return json.dumps(vars(result[0])), 200
    except Exception as e:
        print(e)
        return "Unexpected error", 400

@app.route('/')
def root():
    return "Hello World!"

if __name__ == "__main__":
        # This is used when running locally only. When deploying to Google App
    # Engine, a webserver process such as Gunicorn will serve the app. This
    # can be configured by adding an `entrypoint` to app.yaml.
    # Flask's development server will automatically serve static files in
    # the "static" directory. See:
    # http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed,
    # App Engine itself will serve those files as configured in app.yaml.
    app.run(host='127.0.0.1', port=8080, debug=True)
    #app.run(host="0.0.0.0", port=5000)


from django.urls import path
from . import views

app_name = 'cinema'
urlpatterns = [
    path('', views.movies, name='movies'),
    path('persons/', views.persons, name='persons'),
    path('person_detail/<str:imdb_id>/', views.person_detail, name='person_detail'),
    path('<str:imdb_id>/', views.movie_detail, name='movie_detail'),
    path('<str:imdb_id>/edit_save=<int:edit_save>/', views.movie_detail, name='movie_detail'),
    path('<str:imdb_id>/edit_save=<int:edit_save>/b_page_number=<int:b_page_number>', views.movie_detail, name='movie_detail'),
]


from numpy import *
import numpy as np
from matplotlib import *
from scipy import *
from pylab import figure, show, setp
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import scipy.io as sio
from scipy.signal import find_peaks

def step_rossler(x1,y1,z1,x2,y2,z2, dt, a, b, c, K,w1,w2):
    dx1 = dt*(-w1*y1-z1 + K*(x2-x1))
    dy1 = dt*(w1*x1 + a*y1)
    dz1 = dt*(b + z1*(x1 - c))

    dx2 = dt*(-w2*y2-z2 + K*(x1-x2))
    dy2 = dt*(w2*x2 + a*y2)
    dz2 = dt*(b + z2*(x2 - c))

    x1,y1,z1 = x1+dx1, y1+dy1, z1+dz1
    x2,y2,z2 = x2+dx2, y2+dy2, z2+dz2

    return (x1,y1,z1, x2,y2,z2)
def orbit_rossler(x1,y1,z1,x2,y2,z2, dt, a, b, c, K, w1,w2, numsteps):
    XYZ = np.zeros((numsteps+1)*6)
    XYZ = np.reshape(XYZ, (numsteps+1, 6))
    XYZ[0,:] = x1,y1,z1,x2,y2,z2
    for i in range(1, numsteps+1):
        x1,y1,z1,x2,y2,z2 = step_rossler(x1,y1,z1,x2,y2,z2, dt, a,b,c,K, w1, w2)

        XYZ[i,:] = x1,y1,z1,x2,y2,z2
    return XYZ
def simFn(x1,x2, skew):
    if skew == 0:
        diff_skew = x1 - x2
    else:
        diff_skew = x1[skew:] - x2[:-skew]

    diff_skew_avg = np.average(diff_skew*diff_skew)

    x1_sq_avg = np.average(x1*x1)
    x2_sq_avg = np.average(x2*x2)
    factor = np.sqrt(x1_sq_avg*x2_sq_avg)

    return diff_skew_avg/factor
x1_0, y1_0, z1_0 = [0.001]*3
x2_0, y2_0, z2_0 = [0.002]*3

a=0.165
b=0.2
c=10
w1=0.99
w2=0.95

dt = 0.01
t = np.arange(0,1000, dt)
numsteps = len(t) - 1
K = 0.2
XYZ_0_2 = orbit_rossler(x1_0, y1_0, z1_0, x2_0, y2_0, z2_0,dt,a,b,c, K, w1,w2, numsteps)
x1,y1,z1,x2,y2,z2 = [ XYZ_0_2[:,i] for i in range(6) ]
x1 = XYZ_0_2[:,0]
x2 = XYZ_0_2[:,3]

x1 = x1[10000:]
x2 = x2[10000:]

tau = np.arange(0,30,dt)
S = np.array([ simFn(x2,x1,int(_tau/dt)) for _tau in tau ])
minskew = np.argmin(S[:1000])
print(minskew)
#plt.plot(x1,x2)
#ax = plt.gca()
#ax.set_aspect(1.0)
#ax.set_xlabel('$x1(t)$')
#ax.set_ylabel('$x2(t)$')

#plt.plot(tau, S)
#ax = plt.gca()
#ax.set_xlabel('$\Delta t$')
#ax.set_ylabel('$S(\Delta t)$')

#plt.plot(x1,x2)
#ax = plt.gca()
#ax.set_aspect(1.0)
#ax.set_xlabel('$x1(t)$')
#ax.set_ylabel('$x2(t)$')

#plt.plot(tau, S)
#ax = plt.gca()
#ax.set_xlabel('$\Delta t$')
#ax.set_ylabel('$S(\Delta t)$')

plt.plot(x1[:-minskew], x2[minskew:])
ax = plt.gca()
ax.set_aspect(1.0)
ax.set_xlabel('$x1(t + \Delta t)$')
ax.set_ylabel('$x2(t)$')

show()

Картинки 17.py

Картинки 17.py


import os
import string
import tempfile

from tensorflow.python.data.benchmarks import benchmark_base
from tensorflow.python.data.experimental.ops import readers
from tensorflow.python.data.ops import readers as core_readers
from tensorflow.python.ops import parsing_ops
from tensorflow.python.platform import gfile
from tensorflow.python.platform import googletest

class CsvDatasetBenchmark(benchmark_base.DatasetBenchmarkBase):
  """Benchmarks for `tf.data.experimental.CsvDataset`."""

  FLOAT_VAL = '1.23456E12'
  STR_VAL = string.ascii_letters * 10

  def _set_up(self, str_val):
    # Since this isn't test.TestCase, have to manually create a test dir
    gfile.MakeDirs(googletest.GetTempDir())
    self._temp_dir = tempfile.mkdtemp(dir=googletest.GetTempDir())

    self._num_cols = [4, 64, 256]
    self._num_per_iter = 5000
    self._filenames = []
    for n in self._num_cols:
      fn = os.path.join(self._temp_dir, 'file%d.csv' % n)
      with open(fn, 'w') as f:
        # Just write 100 rows and use `repeat`... Assumes the cost
        # of creating an iterator is not significant
        row = ','.join(str_val for _ in range(n))
        f.write('\n'.join(row for _ in range(100)))
      self._filenames.append(fn)

  def _tear_down(self):
    gfile.DeleteRecursively(self._temp_dir)

  def _run_benchmark(self, dataset, num_cols, prefix, benchmark_id):

    self.run_and_report_benchmark(
        dataset=dataset,
        num_elements=self._num_per_iter,
        name='%s_with_cols_%d' % (prefix, num_cols),
        iters=10,
        extras={
            'model_name': 'csv.benchmark.%d' % benchmark_id,
            'parameters': '%d' % num_cols,
        },
        warmup=True)

  def benchmark_map_with_floats(self):
    self._set_up(self.FLOAT_VAL)
    for i in range(len(self._filenames)):
      num_cols = self._num_cols[i]
      kwargs = {'record_defaults': [[0.0]] * num_cols}
      dataset = core_readers.TextLineDataset(self._filenames[i]).repeat()
      dataset = dataset.map(lambda l: parsing_ops.decode_csv(l, **kwargs))  # pylint: disable=cell-var-from-loop
      self._run_benchmark(
          dataset=dataset,
          num_cols=num_cols,
          prefix='csv_float_map_decode_csv',
          benchmark_id=1)
    self._tear_down()

  def benchmark_map_with_strings(self):
    self._set_up(self.STR_VAL)
    for i in range(len(self._filenames)):
      num_cols = self._num_cols[i]
      kwargs = {'record_defaults': [['']] * num_cols}
      dataset = core_readers.TextLineDataset(self._filenames[i]).repeat()
      dataset = dataset.map(lambda l: parsing_ops.decode_csv(l, **kwargs))  # pylint: disable=cell-var-from-loop
      self._run_benchmark(
          dataset=dataset,
          num_cols=num_cols,
          prefix='csv_strings_map_decode_csv',
          benchmark_id=2)
    self._tear_down()

  def benchmark_csv_dataset_with_floats(self):
    self._set_up(self.FLOAT_VAL)
    for i in range(len(self._filenames)):
      num_cols = self._num_cols[i]
      kwargs = {'record_defaults': [[0.0]] * num_cols}
      dataset = core_readers.TextLineDataset(self._filenames[i]).repeat()
      dataset = readers.CsvDataset(self._filenames[i], **kwargs).repeat()  # pylint: disable=cell-var-from-loop
      self._run_benchmark(
          dataset=dataset,
          num_cols=num_cols,
          prefix='csv_float_fused_dataset',
          benchmark_id=3)
    self._tear_down()

  def benchmark_csv_dataset_with_strings(self):
    self._set_up(self.STR_VAL)
    for i in range(len(self._filenames)):
      num_cols = self._num_cols[i]
      kwargs = {'record_defaults': [['']] * num_cols}
      dataset = core_readers.TextLineDataset(self._filenames[i]).repeat()
      dataset = readers.CsvDataset(self._filenames[i], **kwargs).repeat()  # pylint: disable=cell-var-from-loop
      self._run_benchmark(
          dataset=dataset,
          num_cols=num_cols,
          prefix='csv_strings_fused_dataset',
          benchmark_id=4)
    self._tear_down()

if __name__ == '__main__':
  benchmark_base.test.main()


# conda install -n py36 python=3.6 openpyxl pandas selenium requests pyinstaller
# pyinstaller -F credamo_download_gui20230224.py --hidden-import "openpyxl.cell._writer"

import time
import openpyxl
import pandas as pd
import tkinter as tk
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import requests
import json

def open_credamo(browser):
    browser.get("https://www.credamo.com")
    # 检测登录
    login = False
    for i in range(5):
        try:
            # show_execute_info("请登录Credamo账号。")
            browser.find_element_by_class_name("login").click()
            time.sleep(1)
            for cookie in browser.get_cookies():
                if cookie['name']=='credamo-dms-auth':
                    login=True
                    break
            # print("未检测到登录，请登录Credamo账号。")
        except:
            time.sleep(1)
            pass
            # print("已登陆。")
    if login==True:
        show_execute_info("已登陆，请在浏览器中手动打开您需要拒绝被试的项目。")
    else:
        show_execute_info("请登录账号，然后打开一个项目。")
    show_execute_info("准备好excel后点击自动拒绝被试")

def get_surveyId(browser):
    url = browser.current_url
    try:
        # 获得surveyId
        startloc = url.index("surveyId")+len("surveyId=")
        endloc = url.index("#")
        surveyId = url[url.index("surveyId")+len("surveyId="):url.index("#")]
    except:
        # print("还没有打开项目,读取不到surveyId,请打开一个项目")
        show_execute_info("还没有打开项目,读取不到surveyId,请打开一个项目")
        return 0

    #检测是否打开数据清理页面，如果没有则自动点击清理
    try:
        browser.find_element_by_class_name("iconfont icon-shaixuan")
        # 如果找到，表明已经打开数据清理页面
        # print("已打开数据清理页面")
        show_execute_info("已打开数据清理页面")
    except:
    # 如果报错，没有打开数据清理页面，自动点击“数据清理”
        try:
            browser.find_element_by_class_name("iconfont icon-shaixuan")
        except:
            browser.find_element_by_link_text("数据清理").click()
            time.sleep(3)

    return int(surveyId)

def set_page_size(browser,page_size):
    page_sizes = [5,10,30,50,100]
    try:
        size_choise = page_sizes.index(page_size)
    except:
        # print("没有该条数/页的选项，请检查")
        show_execute_info("没有该条数/页的选项，请检查")
        return -2
    # 设置100页
    set_size = False
    retry_time = 0
    while (not set_size) and retry_time <= 5:
        try:
            browser.find_element_by_class_name('el-input__inner').click()
            time.sleep(1)
            pageSizes = browser.find_elements_by_class_name('el-select-dropdown__item')
            pageSizes[size_choise].click()
            page_size = page_sizes[size_choise]
            set_size = True
            return page_size
        except:
            # print("设置条数/页失败，正在重试")
            show_execute_info("设置条数/页失败，正在重试")
            retry_time += 1
    # print("设置页码失败，请检查网页。")
    show_execute_info("设置页码失败，请检查网页。")

def go_to_page(browser,page):
    try:
        loc = browser.find_element_by_class_name('el-pagination__jump').find_element_by_class_name('el-input__inner')
        loc.send_keys(Keys.CONTROL+'a')     #全选
        loc.send_keys(Keys.DELETE)		# 删除，清空
        loc.send_keys(page)	# 写入新的值
        loc.send_keys(Keys.ENTER)
        return page
    except:
        # print("无法跳转页面")
        show_execute_info("无法跳转页面")
        return 0

def set_encoding(browser):
    time.sleep(1)
    browser.find_element_by_link_text("数据清理").click()
    time.sleep(1)
    browser.find_element_by_xpath("/html/body/div[@id='survey_bg']/div[4]/div[1]/div[@class='route']/div[@class='button2']").click()
    time.sleep(1)
    browser.find_element_by_xpath("/html/body/div[@id='survey_bg']/div[4]/div[@class='resultView']/div[@class='search-box']/div[@class='popover'][4]").click()
    time.sleep(1)
    browser.find_element_by_xpath("/html/body/div[@id='survey_bg']/div[4]/div[1]/div[@class='route']/div[@class='button1']").click()
    time.sleep(1)

def get_page_df(browser,surveyId,page_size,page_num,download=False):
    try:
        time.sleep(1)
        cookies = browser.get_cookies()
        cookie_str = ""
        for cookie in cookies:
            cookie_str = "{}={}; ".format(cookie['name'],cookie['value']) + cookie_str
        cookie_str = cookie_str[:-2]

        headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
                    "referer": "https://www.credamo.com/survey.html?surveyId=%d}" % surveyId,
                    "cookie":cookie_str}
        url = "https://www.credamo.com/v1/cleanVar/qstOverviewBySurId/%d?currPageSize=%d&currPageIndex=%d"  % (surveyId,page_size,page_num)
    except:
        # print("获取cookie失败，请重试。")
        show_execute_info("获取cookie失败，请重试。")
        return []
    try:
        resp = requests.get(url=url,headers=headers)
        if resp.status_code == 200:
            output = json.loads(resp.content.decode('utf8'))
            data = output['data']
            ques_headers = data['header']
            total_cnt = output['total']
            ques_headers_kv = {}
            for header in ques_headers:
                ques_headers_kv[header["id"]] = header['qNum']+" "+header['questionName']
            df = pd.DataFrame(output['data']['rowList'])
            #如果下载，则不替换列名称，最后再替换
            if not download:
                df.columns = [ques_headers_kv[id] if id not in ['status', 'answerId', 'userId'] else id for id in df.columns]
                return df,total_cnt
            else:
                return df,total_cnt,ques_headers_kv
        else:
            # print("无法登录到问卷页面")
            show_execute_info("无法登录到问卷页面")
            return pd.DataFrame([]),0
    except:
        # print("无法get问卷页面")
        show_execute_info("无法get问卷页面")
        return pd.DataFrame([]),0

def download_data(browser,download_name):
    surveyId = get_surveyId(browser)
    set_encoding(browser)
    page_size = set_page_size(browser,page_size=100)
    page_num = 1
    page_num = go_to_page(browser,page_num)
    page_df,total_cnt,ques_headers_kv = get_page_df(browser,surveyId,page_size,page_num,download=True)
    total_page_num = total_cnt//page_size+1
    page_dfs = pd.DataFrame([])
    for page_num in range(total_page_num):
        page_num += 1
        # print(page_num)
        # go_to_page(browser,page_num)
        # time.sleep(5)
        page_df,total_cnt,ques_headers_kv = get_page_df(browser,surveyId,page_size,page_num,download=True)
        page_df.set_index('userId')
        if str(page_dfs.shape)=="(0, 0)":
            page_dfs = page_df
        else:
            page_dfs = pd.concat([page_dfs,page_df],ignore_index=True)
    page_dfs.columns = [ques_headers_kv[id] if id not in ['status', 'answerId', 'userId'] else id for id in page_df.columns]
    page_dfs = page_dfs[(page_dfs=="*").sum(axis=1)==0] #剔除拒绝的
    download_name_str = download_name.get('0.0','end').strip()+"_"+time.strftime('%Y%m%d_%H%M')+".xlsx"
    page_dfs.to_excel(download_name_str)
    show_execute_info("{}下载完成".format(download_name_str))

def get_userId_loc(excel_name,page_df,page_num,autobatchReject):
    # excel_name_str = excel_name.get("1.0","end").strip()
    # file = pd.read_excel(excel_name_str)

    # print(excel_name_str)

    # try:
        # file = pd.read_excel(excel_name_str)
        # print(file)
    # except:
    #     # print('没有excel文件,请检查exe所在文件夹下是否有"待拒绝被试.xlsx"文件。\n\n')
    #     show_execute_info('没有excel文件,请检查exe所在文件夹下是否有"{}"文件。\n\n'.format(excel_name_str))
    #     return []
    # chosen_ids = file['用户ID'].tolist()

    chosen_ids = excel_name.get("1.0","end").strip().split('\n')
    print(chosen_ids)

    page_userIds = page_df['userId'].tolist()
    page_chosen_ids = list(set(chosen_ids) & set(page_userIds))
    chosen_rows = []
    for chosen_id in page_chosen_ids:
        chosen_row = page_userIds.index(chosen_id)+1
        chosen_rows.append(chosen_row)
        browser.find_element_by_xpath("/html/body/div[1]/div[4]/div[2]/div[3]/div/div[3]/div/div[2]/table/tbody/tr[%d]" % chosen_row)\
            .find_element_by_class_name('vxe-cell--checkbox').click()
    # 批量拒绝
    browser.find_element_by_class_name("el-dropdown").click()
    time.sleep(1)
    if autobatchReject == True:
        browser.find_elements_by_class_name("el-dropdown-menu__item")[1].click()
        time.sleep(1)
        msgbox = browser.find_element_by_class_name("el-message-box")
        msgbox.click()
        # print("请根据网页提示进行操作")
        time.sleep(3)
        msgbox.find_element_by_class_name("el-button").click()
        show_execute_info("第%d页已拒绝%d个被试" % (page_num,len(chosen_rows)))
        time.sleep(3)
    return chosen_rows

def batchReject(browser,excel_name,page_num_assign_text,autoReject=True):
    surveyId = get_surveyId(browser)
    page_size = set_page_size(browser,page_size=100)
    page_num = 1
    page_num = go_to_page(browser,page_num)
    page_df,total_cnt = get_page_df(browser,surveyId,page_size,page_num)
    total_page_num = total_cnt//page_size+1
    if autoReject:
        for page_num in range(total_page_num):
            page_num += 1
            # print(page_num)
            go_to_page(browser,page_num)
            time.sleep(5)
            page_df,total_cnt = get_page_df(browser,surveyId,page_size,page_num)
            # print(total_cnt)
            userId_loc = get_userId_loc(excel_name,page_df,page_num,autoReject)
            # print(userId_loc)
        show_execute_info('所有页面已拒绝。\n你可以打开另一个项目，清空原用户ID后粘贴新项目待拒绝用户ID，重新点击拒绝被试。\n或者关闭浏览器，关闭本程序')
    else:
        #手动拒绝
        try:
            page_num_assign = int(page_num_assign_text.get('0.0','end').strip())
        except:
            show_execute_info("无法读取页面数，请在上方小方框中输入正确的页面数字")
            return 0
        if page_num_assign <= 0 or page_num_assign > total_cnt//page_size+1:
            show_execute_info("输入的页面数字超过答卷范围，请检查页面数")
        go_to_page(browser,page_num_assign)
        time.sleep(5)
        page_df,total_cnt = get_page_df(browser,surveyId,page_size,page_num_assign)
        # print(total_cnt)
        userId_loc = get_userId_loc(excel_name,page_df,page_num_assign,autoReject)
        # print(userId_loc)
        show_execute_info('页面%d已选中待拒绝被试，可以手动在浏览器中点击拒绝。\n'%page_num_assign)

def show_execute_info(var,cmd=False):
    if cmd:
        print(var)
    else:
        execute_info.insert("0.0", '\n' + time.strftime('%Y-%m-%d %H:%M:%S ') + var + '\n')

window = tk.Tk()
window.title("批量拒绝被试")
window.geometry("600x450")

login_Frame = tk.Frame(window)
login_Frame.pack()

open_button = tk.Button(login_Frame, text='打开Credamo网站', width=15, height=1,
                        command=lambda:open_credamo(browser))
open_button.pack(side="left")

blank1 = tk.Label(login_Frame, text="  ",
                                  width=3)
blank1.pack(side="left")

download_button = tk.Button(login_Frame, text='下载数据', width=15, height=1,
                        command=lambda:download_data(browser,download_name))
download_button.pack(side="left")

download_name = tk.Text(login_Frame, height=1, width=20)
download_name.insert("0.0", 'credamo_data')
download_name.pack(side="left")

download_format = tk.Label(login_Frame, text="_YYYYmmdd_HHMMSS.xlsx",
                                  width=23)
download_format.pack(side="left")

#拒绝被试list
excel_instruc = tk.Label(window, text="请在下面粘贴待拒绝被试的用户ID",
                                width=30, height=2)
excel_instruc.pack()

excel_name = tk.Text(window, height=10)
excel_name.pack()

reject_Frame = tk.Frame(window)
reject_Frame.pack()

reject_button = tk.Button(reject_Frame, text='自动拒绝被试', width=10, height=1,
                        command=lambda:batchReject(browser,excel_name,page_num_assign_text,True))
reject_button.pack(side="left")

blank1 = tk.Label(reject_Frame, text="  ",
                                  width=3)
blank1.pack(side="left")

choose_button = tk.Button(reject_Frame, text='手动拒绝被试', width=10, height=1,
                            command=lambda:batchReject(browser,excel_name,page_num_assign_text,False))
choose_button.pack(side="left")

page_instruc1 = tk.Label(reject_Frame, text="第",
                                width=2)
page_instruc1.pack(side="left")

page_num_assign_text = tk.Text(reject_Frame, height=1, width=4)
page_num_assign_text.insert("0.0", '1')
page_num_assign_text.pack(side="left")

page_instruc2 = tk.Label(reject_Frame, text="页",
                                width=2)
page_instruc2.pack(side="left")

blank_instruc = tk.Label(window, text="----下面是提示----",
                                # bg="green", font=('Arial',12),
                                width=30, height=2)
blank_instruc.pack()

execute_info = tk.Text(window, height=10)
first_info = '一共三个步骤，登录Credamo后打开一个项目，粘贴待拒绝用户ID，最后拒绝被试。\n请在点击第一个按钮登录Credamo，打开您的项目'
show_execute_info(first_info)
execute_info.pack()

browser = webdriver.Chrome()

window.mainloop()


# -*- coding: utf-8 -*-
"""
Created on Sat Jun 20 00:10:04 2020

@author: kcsof
"""
import statistics as stat
from math import sqrt

f = open("C:/Users/kcsof/input03.txt", "r")
f2 = (f.read())
f3 = ' '.join(f2.split())
numyes2 = [int(s) for s in f3.split() if s.isdigit()]

'''
listed = list(numyes2)
popped = listed.pop(0)

#n = int(input())
#X = [int(i) for i in input().split()]

def stddev(lst):
    mean = float(sum(lst)) / len(lst)
    return sqrt(sum((x - mean)**2 for x in lst) / len(lst))

def truncate(n, decimals=1):
    multiplier = 10 ** decimals
    return int(n * multiplier) / multiplier

print(truncate(stddev(listed)))
'''
#it was key to use the population stdev
n,a=input(),list(map(int,input().split()))
print(round(pstdev(a),1))


# python3
# queue_via_stacks.py - Queue implementation that uses two stacks to achieve queue functionality.

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

    def __str__(self):
        return self.data

class Stack:
    """
    Basic Stack implementation.
    """
    def __init__(self):
        self.size = 0
        self.top = None

    def is_empty(self):
        return self.size == 0

    def peek(self):
        if self.size == 0:
            raise Exception("Peeking at an empty stack.")
        return self.top.data

    def push(self, item):
        node = Node(item)
        node.next = self.top
        self.top = node
        self.size += 1

    def pop(self):
        if self.size == 0:
            raise Exception("Popping off of an empty stack.")
        item = self.top.data
        self.top = self.top.next
        self.size -= 1
        return item

    def __str__(self):
        cur = self.head.next
        out = ""
        while cur:
            out += str(cur.data) + " -> "
            cur = cur.next
        return out[:-3]

class MyQueue:
    """
    Queue implementation that uses two stacks to achieve queue functionality.
    One stack is used as a "push_stack" to add new items,
    and another is used as "pop_stack" to remove items.
    """

    def __init__(self):
        self.push_stack = Stack()
        self.pop_stack = Stack()

    def is_empty(self):
        # Check to make sure that both stacks are empty
        if (self.push_stack.is_empty()) and (self.pop_stack.is_empty()):
            return True
        return False

    def add(self, item):
        """
        Add item goes into push_stack,
        but pop_stack has to be empty first so that the new item can be on the top.
        """
        if self.push_stack.is_empty():
            if self.pop_stack.is_empty():
                self.push_stack.push(item)
            else:
                while self.pop_stack.is_empty() == False:
                    self.push_stack.push(self.pop_stack.pop())
                self.push_stack.push(item)
        else:
            self.push_stack.push(item)

    def peek(self):
        """
        Item is peeked at from the top of pop_stack,
        but push_stack has to be empty first to be sure that the item peeked is the first inputted.
        """
        if self.pop_stack.is_empty():
            if self.push_stack.is_empty():
                raise Exception("Peeking at an empty Queue.")
            else:
                while self.push_stack.is_empty() == False:
                    self.pop_stack.push(self.push_stack.pop())
                return self.pop_stack.peek()
        else:
            return self.pop_stack.peek()

    def remove(self):
        """
        Item is removed from the top of pop_stack,
        but push_stack has to be empty first so that the first-in can be first-out.
        """
        if self.pop_stack.is_empty():
            if self.push_stack.is_empty():
                raise Exception("Can't remove from an empty Queue.")
            else:
                while self.push_stack.is_empty() == False:
                    self.pop_stack.push(self.push_stack.pop())
                return self.pop_stack.pop()
        else:
            return self.pop_stack.pop()


### various utilities for plotting data
### for general analysis purposes
### mostly working with final data, some matlab arrays
import matplotlib
matplotlib.use('Agg') # to avoid auto-presenting images, breaking when no X server
import matplotlib.pyplot as plt

from scipy.ndimage import gaussian_filter
from .data_utils import trial_average

def plot_psths(data, fname_root, labels=None, sigma=20):
    """
    Plot PSTHs for each neuron in a final data list.
    fname_root is the root path the figures will be saved to
    labels are the unique label for each image (at the end of the
    fname_root, and in the title of the plots)
        - defaulting to the neuron indices
    """
    data = trial_average(data).mean(0) # across conditions too
    n_neurons = data.shape[0]
    if labels is None:
        labels = list(range(n_neurons))

    for cell in range(n_neurons):
        psth = gaussian_filter(data[cell], sigma=sigma) * 1000

        plt.figure()
        plt.plot(psth, linewidth=3)
        plt.xlabel('time (ms)')
        plt.ylabel('firing rate (Hz)')
        plt.title(f'PSTH for {labels[cell]}')
        plt.savefig(fname_root + labels[cell] + '.png')
        plt.close()


from modules.creat_config import *

@bot.callback_query_handler(func=lambda call: "Pause" in call.data)
def add_pause(call):
    try:
        print(call)
        caption = str(call.message.text)
        print(caption)
        print(call.data)
        key_data=str(call.data).replace("Pause ","")
        print(key_data)
        text=file_pause(key_data)
        bot.answer_callback_query(callback_query_id=call.id,text=text,cache_time=3)
    except Exception as e:
        print(f"Pause :{e}")

def file_pause(gid):
    print("暂停任务")
    try:
        the_pause = aria2.get_download(gid=str(gid))
        torrent_name=the_pause.name
        resume_result=the_pause.pause()
        if resume_result==True:
            print(f"{torrent_name}\n暂停成功")
            return f"暂停成功"
        else:
            print(f"{torrent_name}\n暂停失败")
            return f"暂停失败"
    except Exception as e:
        print (e)
        return f"\n暂停失败：{e}"


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun May 31 20:06:55 2020

@author: eleden
"""

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun May 31 13:46:19 2020

@author: eleden
"""
import os
import fnmatch
import geopandas as gpd
import pandas as pd
import rasterio
from shapely.geometry import mapping
from rasterio.mask import mask
import numpy as np
from sklearn import preprocessing

shapefile_paths = "./Shapefiles/North coast states/North-coast-states.shp"
shapefile = gpd.read_file(shapefile_paths)

col1 = ['AOI', 'Date']
col2 = ['no2Mean', 'no2Std', 'no2Min', 'no2Max', 'no2Median']
col3 = ['ozoneMean', 'ozoneStd', 'ozoneMin', 'ozoneMax', 'ozoneMedian']
col4 = ['aerosolMean', 'aerosolStd', 'aerosolMin', 'aerosolMax', 'aerosolMedian']
col5 = ['Gt']

col = col1 + col2 + col3 + col4 + col5

data = pd.DataFrame(columns=col)

df_covid = pd.read_csv("covid19-cases-us-states-east.csv")
df_covid.date = df_covid.date.astype(str)
for no2_tif_file in os.listdir("./no2_tif/"):
  date = no2_tif_file.split('_')[1][:-4]
  with rasterio.open(f'./no2_tif/{no2_tif_file}') as src:
    for i in range(len(shapefile)):
      geometry = shapefile.iloc[i]['geometry']
      Name = shapefile.iloc[i]['STATE_NAME']
    # transform to GeJSON format
      geoms = [mapping(geometry)]
    # extract the raster values values within the polygon
      out_image, out_transform = mask(src, geoms, crop=True, nodata=np.nan)
      out_meta = src.meta
      mean = np.nanmean(out_image[0])
      min_val = np.nanmin(out_image[0])
      max_val = np.nanmax(out_image[0])
      median = np.nanmedian((out_image[0]))
      std = np.nanstd(out_image[0])
      gt = df_covid.loc[(df_covid.date == date)&(df_covid.state == Name)].positive
      if gt.empty:
        gt = 0
      else:
        gt = gt.values[0]
      val = [Name, date]
      val += [mean, std, min_val, max_val, median]
      val += [0,0,0,0,0]
      val += [0,0,0,0,0]
      val += [gt]
      data.loc[len(data)] = val

for ozone_tif_file in os.listdir("./ozone_tif/"):
  date = ozone_tif_file.split('_')[1][:-4]
  with rasterio.open(f'./ozone_tif/{ozone_tif_file}') as src:
    for i in range(len(shapefile)):
      geometry = shapefile.iloc[i]['geometry']
      Name = shapefile.iloc[i]['STATE_NAME']
    # transform to GeJSON format
      geoms = [mapping(geometry)]
    # extract the raster values values within the polygon
      out_image, out_transform = mask(src, geoms, crop=True, nodata=np.nan)
      out_meta = src.meta
      mean = np.nanmean(out_image[0])
      min_val = np.nanmin(out_image[0])
      max_val = np.nanmax(out_image[0])
      median = np.nanmedian((out_image[0]))
      std = np.nanstd(out_image[0])
      gt = df_covid.loc[(df_covid.date == date)&(df_covid.state == Name)].positive
      if gt.empty:
        gt = 0
      else:
        gt = gt.values[0]
      data.loc[(data.AOI == Name) & (data.Date == date), col3] = [mean, std, min_val, max_val, median]

for aerosol_tif_file in os.listdir("./aerosol_tif/"):
  date = aerosol_tif_file.split('_')[1][:-4]
  with rasterio.open(f'./aerosol_tif/{aerosol_tif_file}') as src:
    for i in range(len(shapefile)):
      geometry = shapefile.iloc[i]['geometry']
      Name = shapefile.iloc[i]['STATE_NAME']
    # transform to GeJSON format
      geoms = [mapping(geometry)]
    # extract the raster values values within the polygon
      out_image, out_transform = mask(src, geoms, crop=True, nodata=np.nan)
      out_meta = src.meta
      mean = np.nanmean(out_image[0])
      min_val = np.nanmin(out_image[0])
      max_val = np.nanmax(out_image[0])
      median = np.nanmedian((out_image[0]))
      std = np.nanstd(out_image[0])
      gt = df_covid.loc[(df_covid.date == date)&(df_covid.state == Name)].positive
      if gt.empty:
        gt = 0
      else:
        gt = gt.values[0]
      data.loc[(data.AOI == Name) & (data.Date == date), col4] = [mean, std, min_val, max_val, median]

data.to_csv("dataset.csv",index=False, sep=';')


from django.conf.urls.defaults import patterns

urlpatterns = patterns('channelguide.user_profile.views',
    (r'^register/?$', 'register_view'),
    (r'^search$', 'search'),
    (r'^moderators$', 'moderators'),
    (r'^confirm/(\d+)/([a-z0-9]+)', 'confirm'),
    (r'^set_language_view$', 'set_language_view'),
    (r'^profile/?$', 'user'),
    (r'^profile/(\d+)/$', 'user'),
)

urlpatterns += patterns('django.contrib.auth.views',
    (r'logout/?$', 'logout', {'next_page': '/'}))


# -*- coding:utf-8 -*-
"""滑动窗口的最大值
    题目描述
    给定一个数组和滑动窗口的大小，找出所有滑动窗口里数值的最大值。
    例如，如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3，那么一共存在6个滑动窗口，他们的最大值分别为{4,4,6,6,6,5}；
    针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个：
    {[2,3,4],2,6,2,5,1}， {2,[3,4,2],6,2,5,1}， {2,3,[4,2,6],2,5,1}， {2,3,4,[2,6,2],5,1}， {2,3,4,2,[6,2,5],1}， {2,3,4,2,6,[2,5,1]}。

    解题思路：
    对N个数字的数组，窗口大小为n，则共有N+1-n个滑动窗口。

"""
class Solution:
    def maxInWindows(self, num, size):
        if not num or not size:
            return []
        elif len(num) < size:
            return []
        else:
            left = 0
            right = size
            res = []
            while right < len(num)+1:
                tmp = max(num[left:right])
                res.append(tmp)
                left += 1
                right += 1
            return res

def main():
    num = [10,14,12,11]
    size = 4
    res = Solution().maxInWindows(num, size)
    print(res)

main()


from fragview.models import User, Library, UserProject, PendingProject
from django.test import TestCase
from tests.utils import ProjectTestCase
from tests.project_setup import Project

class TestUserProject(TestCase):
    ERR_MSG = "dummy error msg"

    def setUp(self):
        self.project = UserProject.create_new("MID2", "20201122")
        self.assertTrue(self.project.is_pending())

    def test_set_ready(self):
        """
        test setting pending project into 'ready' state
        """
        self.project.set_ready()
        self.assertFalse(self.project.is_pending())

    def test_set_failed(self):
        """
        test setting error on a pending project
        """

        # set error on the project
        self.project.set_failed(self.ERR_MSG)

        # should still be pending
        self.assertTrue(self.project.is_pending())

        # check that error message was set in the database
        pend_proj = PendingProject.objects.get(project=self.project.id)
        self.assertIsNotNone(pend_proj)
        self.assertEqual(pend_proj.error_message, self.ERR_MSG)

class TestLibrary(ProjectTestCase):
    PROJECTS = [
        Project(
            protein="Nsp5",
            proposal="20180453",
            datasets=[],
            crystals=[],
            results=[],
        ),
        Project(
            protein="MID2",
            proposal="20180453",
            datasets=[],
            crystals=[],
            results=[],
        ),
    ]

    def setUp(self):
        super().setUp()

        lib = Library(name="public")
        lib.save()

        lib = Library(name="private", project_id=self.project.id)
        lib.save()

    def _assert_libs(self, libs, expected_names: set[str]):
        got_names = {lib.name for lib in libs}
        self.assertSetEqual(got_names, expected_names)

    def test_get_all(self):
        # for first project, we should get all libraries
        libs = Library.get_all(self.project)
        self._assert_libs(libs, {"public", "private"})

        # for second project, we should only get 'public' library
        libs = Library.get_all(self.projects[1])
        self._assert_libs(libs, {"public"})

    def test_get_by_name(self):
        #
        # for first project we should be able to get both libs
        #
        lib = Library.get_by_name(self.project, "public")
        self.assertEqual(lib.name, "public")

        lib = Library.get_by_name(self.project, "private")
        self.assertEqual(lib.name, "private")

        #
        # for second project, we should only be able to get
        # the 'public' library
        #
        lib = Library.get_by_name(self.projects[1], "public")
        self.assertEqual(lib.name, "public")

        with self.assertRaises(Library.DoesNotExist):
            Library.get_by_name(self.projects[1], "private")

class TestUser(ProjectTestCase):
    USERNAME = "muser"

    def setUp(self):
        super().setUp()

        self.user_project = UserProject.get(self.projects[0].id)
        self.user = User(username=self.USERNAME)
        self.user.save()

    def test_set_current_project(self):
        """
        test User.set_current_project
        """
        # new user should not have a current_project set initially
        self.assertIsNone(self.user.current_project)

        # set the current project for the user
        self.user.set_current_project(self.user_project)

        #
        # reload user from the database and check it's current project
        #
        rl_usr = User.objects.get(username=self.USERNAME)
        self.assertEqual(rl_usr.current_project.id, self.user_project.id)

    def test_get_current_project_none(self):
        """
        no current project set for the user, and user does not have
        access to any other projects
        """
        self.assertIsNone(self.user.get_current_project(["20160101"]))

    def test_get_current_project_explicit(self):
        """
        get explicitly set current project
        :return:
        """
        self.user.set_current_project(self.user_project)

        proj = self.user.get_current_project(self.proposals)
        self.assertEqual(proj.id, self.user_project.id)

    def test_get_current_project_implicitly(self):
        """
        user have not set current_project, but have access to one of the projects
        """

        # make sure no explicit current project is set
        self.assertIsNone(self.user.current_project)

        proj = self.user.get_current_project(self.proposals)
        self.assertEqual(proj.id, self.user_project.id)


from KPAPI import sendMessage, typingoff
from config import config, load_json, save_json
from Action.All_Menu import teacher, student
import requests, random, json

url = "https://gist.githubusercontent.com/Nguyen3006-IT/85ef6863ddbaaecb3c39d490bf7802b3/raw/f0553d0f2880e245479fa58083098ed0faf79d8d/text2len.json"
json_text = requests.get(url).json()

def conjunction(sender_id, message_text):
  collect_text(message_text.lower())
  try:
    dic_text = load_json("accounts.json") or {}
    texts_collect = load_json("textNoiTu.json") or {}

    if dic_text[sender_id] == "noitu_empty":
      text_user = message_text.split()[-1]
      text_true = [text for text in json_text if text_user == text.split()[0]]
      if text_true == []:
        dic_text[sender_id] = "noitu_empty"
        sendMessage(sender_id, "Bot hết từ... Mong bạn cung cấp thêm giúp Bot.")
      else:
        dic_text[sender_id] = random.choice(text_true)
        sendMessage(sender_id, dic_text[sender_id])

    elif message_text.lower().split()[0] == dic_text[sender_id].lower().split()[-1] and len(message_text.split()) == 2:
      text_user = message_text.split()[-1]
      text_true = [text for text in json_text+texts_collect if text_user == text.split()[0]]
      if text_true == []:
        dic_text[sender_id] = "noitu_empty"
        sendMessage(sender_id, "Bot hết từ. Mời bạn đi trước.")
      else:
        dic_text[sender_id] = random.choice(text_true)
        sendMessage(sender_id, dic_text[sender_id])

    else:
      sendMessage(sender_id, "Bạn đã thua. Bot đi trước")
      dic_text[sender_id] = random.choice(json_text)
      sendMessage(sender_id, dic_text[sender_id])

    typingoff(sender_id)
    save_json("accounts.json", dic_text)

  except Exception as e:
        print(e)

def ongame(sender_id):
  dic_text = load_json("accounts.json")
  dic_text[sender_id] = json_text[random.randint(1,500)]
  save_json("accounts.json", dic_text)
  sendMessage(sender_id, dic_text[sender_id])

def offgame(sender_id):
  tool = load_json("tools.json")
  tool["online_noitu"].remove(sender_id)
  teacher(sender_id) if sender_id in tool["teacher"] else student(sender_id)
  save_json("tools.json", tool)
  sendMessage(sender_id, "Bạn đã rời trò chơi nối từ thành công.")

def collect_text(text):
  texts_collect = load_json("textNoiTu.json")
  texts_collect.append(text)
  with open("textNoiTu.json", 'w') as save:
    json.dump(texts_collect, save, ensure_ascii=False)

def rep_start_conjunction(sender_id):
  params = {"access_token": config["Token"]}
  headers = {"Content-Type": "application/json"}
  data = json.dumps({
      "recipient":{"id": sender_id},
      "message":{
        "attachment":{
        "type":"template",
        "payload":{
            "template_type":"button",
            "text": "Chọn người đi trước",
            "buttons":[{
              "type":"postback",
              "title": "SSBot",
              "payload":"bot_start"
            },
            {
              "type":"postback",
              "title": "Tôi",
              "payload": "user_start"
            }
            ]}}}})
  requests.post("https://graph.facebook.com/v2.6/me/messages", params=params,headers=headers, data=data)


import itertools

from django import forms
from django.utils.translation import gettext

from .conf import get_available_languages, get_default_language
from .fields import get_instance_field_value
from .translator import get_i18n_field
from .utils import build_localized_fieldname, get_language

LANGUAGE_OPTIONS = ["browser", "fallback"]

class TranslationModelFormOptions(forms.models.ModelFormOptions):
    """Add the translation form options to the Meta options."""

    def __init__(self, options=None):
        super().__init__(options)
        self.languages = getattr(options, "languages", ["browser", "fallback"])
        self.fallback_language = getattr(options, "fallback_language", None)

class TranslationModelFormMetaClass(forms.models.ModelFormMetaclass):
    def __new__(mcs, name, bases, attrs):
        """
        Include all translation fields for translatable fields declared in the form.

        We use the standard ModelForm field declaration procedure of the ModelFormMetaClass,
        so that all fields are declared during creation of the new object.

        Different actions are required for different Meta class options:
        1) fields - if fields are declared explicitly with the fields option, the related i18n fields will not be
        included, and these have to be added to base_fields.
        2) exclude - if fields are declared implicitly with the exclude option, the i18n translation fields, as well as
        the i18n field itself, will be included. We will remove the i18n field.

        Here all languages are included, so that in the form we can curate which fields are available according to
        settings of the form instance (e.g. kwarg overrides, or model instance fallbacks).
        """
        new_class = super().__new__(mcs, name, bases, attrs)

        # from ModelFormMetaClass, needed for calling fields for model
        base_formfield_callback = None
        for b in bases:
            if hasattr(b, "Meta") and hasattr(b.Meta, "formfield_callback"):
                base_formfield_callback = b.Meta.formfield_callback
                break
        formfield_callback = attrs.pop("formfield_callback", base_formfield_callback)

        # get options, with extra translation form options
        opts = new_class._meta = TranslationModelFormOptions(getattr(new_class, "Meta", None))
        model_class = opts.model

        languages = get_available_languages()

        # Note that base_fields generated by ModelForm do not yet include translation fields if generated with "fields"
        # Meta option, but includes all translation fields if generated with "excludes" Meta option.
        base_fields = list(new_class.base_fields.keys())

        # convert to list in case it is a tuple
        opts_fields = list(opts.fields) if opts.fields else None
        opts_exclude = list(opts.exclude) if opts.exclude else None

        if model_class:
            i18n_field = get_i18n_field(model_class)
            if i18n_field:
                for original_field_name in i18n_field.fields:  # for all translated fields
                    # for all possible system languages
                    for language in languages:
                        field_name = build_localized_fieldname(
                            original_field_name, language, ignore_default=True
                        )

                        # add i18n field if an explicitly chosen field
                        if (
                            opts.fields
                            and original_field_name in base_fields
                            and field_name not in base_fields
                        ):
                            base_fields.append(field_name)
                            opts_fields.append(field_name)

                        # remove field if an explicitly excluded field
                        if (
                            opts.exclude
                            and original_field_name in opts.exclude
                            and field_name in base_fields
                        ):
                            base_fields.remove(field_name)
                            opts_exclude.append(field_name)

                    # Remove the i18n field if present (e.g. because of using the exclude option)
                    name = f"{original_field_name}_i18n"
                    if name in base_fields:
                        base_fields.remove(name)
                    if opts.exclude:
                        opts_exclude.append(name)

                    # Remove the i18n field for the system default language, because that already exists as the default
                    name = f"{original_field_name}_{get_default_language()}"
                    if name in base_fields:
                        base_fields.remove(name)
                    if opts.fields and name in opts.fields:
                        opts_fields.remove(name)
                    if opts.exclude and name not in opts.exclude:
                        opts_exclude.append(name)

                opts.fields = opts_fields
                opts.exclude = opts_exclude

                base_fields = forms.fields_for_model(
                    opts.model,
                    opts.fields,
                    opts.exclude,
                    opts.widgets,
                    formfield_callback,
                    opts.localized_fields,
                    opts.labels,
                    opts.help_texts,
                    opts.error_messages,
                    opts.field_classes,
                    # limit_choices_to will be applied during ModelForm.__init__().
                    apply_limit_choices_to=False,
                )

                # Override default model fields with any custom declared ones
                # (plus, include all the other declared fields).
                base_fields.update(new_class.declared_fields)
            else:
                base_fields = new_class.declared_fields

            # Override base_fields with properly determined set of all translation fields based on ModelForm
            new_class.base_fields = base_fields

        return new_class

class TranslationModelForm(forms.ModelForm, metaclass=TranslationModelFormMetaClass):
    """
    Model form that only adds translation fields for specified languages.

    Meta options and form parameters include:
    - languages: a list defining languages for which translation fields are added.
        - Options are:
            - "browser": current browser language
            - "fallback": the current fallback language, which is the system fallback,
                          or a customized fallback of the translation field.
            - "fr": a language code
        - Overlap is removed, e.g. ["browser", "fr", "fallback"], becomes ["fr"] if all are equal.
        - The list order determines the order of fields in the form.
        - included languages can also be passed via form kwargs to customize on the fly, useful for
          generating translation forms for a specific language.
    - fallback_language: for adapting the fallback language specification on the fly and override the Meta option and/or
    model translation field custom fallback.

    Form parameters take priority of Meta options.

    For the fallback language the following priority holds:
    1) fallback_language passed as form parameter: Form(fallback_language="fr")
    2) the Meta option "fallback_language":
        e.g. Meta:
                fallback_language = "fr"
    3) a custom fallback of a model instance set via "fallback_language_field":
        e.g. i18n = TranslationField(fields=("title", "header"), fallback_language_field="language_code")
    4) The default language of the system. If not Meta option is given fallback reverts to get_default_language()

    Code example:
        class ChallengeModel(models.Model):
            language_code = CharField()
            title = CharField()
            description = CharField()
            i18n = TranslationField(fields=("title", "description"), fallback_language_field="language_code")

        class ChallengeModelForm(RefactoredModelTransForm):

            class Meta:
                fields = ["title", "description"] or exclude = ["language_code"]
                languages = ["browser", "es", "fallback"]

        class ChallengeCreateUpdateView(GenericCreateUpdateView):
            '''An update view with a browser language, spanish and browser field for translations.'''
            model = Challenge
            form_class = ChallengeModelForm()

        class ChallengeTranslationView(GenericCreateUpdateView):
            '''
            A translation view with a specific translation language field and non-editable fallback field as reference.
            '''
            model = Challenge
            form_class = ChallengeModelForm(
                languages=[translation_language_code, "fallback"],
            )
    """

    def __init__(self, *args, languages=None, fallback_language=None, **kwargs):
        """Prune the translation fields based on included languages and fallback_language."""

        self.model_i18n_field = get_i18n_field(self._meta.model)
        self.languages = languages or self._meta.languages
        self.i18n_fields = [
            field for field in self.model_i18n_field.fields if field in self.base_fields.keys()
        ]

        # Given that we set opt.fields and opts.exclude in Meta,
        # the setting of initial values that occurs in ModelForm includes
        # any additional translation fields that have been added in Meta.
        super().__init__(*args, **kwargs)

        # the following require the instance generated in the super call
        self.fallback_language = self.get_fallback_language(fallback_language)
        self.language_codes = self.get_language_codes()
        self.included_fields = self.get_included_fields()

        self.remove_excess_fields()
        self.set_translation_field_attributes()
        self.order_translation_fields()

    def get_included_fields(self):
        """
        Return a dictionary mapping original field names to a list of included field names,
        which may or may not include the original field name.
        """

        fields = {}
        for original_field in self.i18n_fields:
            fields[original_field] = [
                build_localized_fieldname(original_field, language_code, ignore_default=True)
                for language_code in self.language_codes
            ]
        fields["__all__"] = list(itertools.chain.from_iterable(fields.values()))
        return fields

    def set_translation_field_attributes(self):
        """Apply settings of all original field to relevant translation fields."""

        for original_field_name in self.i18n_fields:
            original_field = self.base_fields[original_field_name]
            for field_name in self.included_fields[original_field_name]:
                language = get_default_language()
                if field_name != original_field_name:
                    language = field_name.replace(f"{original_field_name}_", "")
                is_translation = language != self.fallback_language
                label_text = (
                    gettext("translation language")
                    if is_translation
                    else gettext("default language")
                )
                label = f"{original_field.label} ({language.upper()}, {label_text})"
                self.fields[field_name].label = label
                self.fields[field_name].required = (
                    False if is_translation else original_field.required
                )
                self.fields[field_name].widget = original_field.widget

    def order_translation_fields(self):
        """
        Set the order of the fields, ideally replacing the original field with the set of fields in included fields.

        For the Meta.excludes use a cruder ordering where translated fields types are grouped,
        but per type the languages are in order of: browser, other, fallback
        """

        # Form parameter field_order takes priority, otherwise adopt order of fields in Meta fields option, if available
        field_order = self.field_order or None
        if not field_order and self._meta.fields and self._meta.fields != "all":
            field_order = self._meta.fields

        # in case of an explicit field order replace original field with set of included fields
        if field_order:
            new_field_order = list(field_order)
            for original_field in self.i18n_fields:
                if original_field in new_field_order:
                    index = new_field_order.index(original_field) + 1
                    new_field_order[index:index] = self.included_fields[original_field]
                    new_field_order.pop(index - 1)
            field_order = new_field_order
        else:
            # if no explicit field order adopt the order of i18n form fields used to generate the included fields
            field_order = self.included_fields["__all__"]

        self.order_fields(field_order)

    def remove_excess_fields(self):
        """Remove translations fields that are not included in languages."""
        fields = self.fields.copy()
        for original_field_name in self.i18n_fields:
            for field in fields:
                if original_field_name in field and field not in self.included_fields["__all__"]:
                    self.fields.pop(field)

    def get_fallback_language(self, fallback_language=None):
        """
        Get the fallback language.

        Priority is defined as:
        1) key word argument
        2) form Meta option
        3) model instance custom fallback
        4) system default fallback
        """
        if fallback_language:
            return fallback_language

        if self._meta.fallback_language:
            return self._meta.fallback_language

        if self.model_i18n_field.fallback_language_field and self.instance.pk:
            return get_instance_field_value(
                self.instance, self.model_i18n_field.fallback_language_field
            )

        return get_default_language()

    def get_language_codes(self):
        """Get the languages based on options included in include languages, in the order they are submitted."""

        valid_languages = get_available_languages()
        languages = []
        if self.languages:
            for value in self.languages:
                if value == "browser":
                    value = get_language()
                elif value == "fallback":
                    value = self.fallback_language
                if value not in valid_languages:
                    raise ValueError(f"languages: value {value} is not permitted")
                languages.append(value)

        if not languages:
            raise ValueError("languages: No languages have been defined.")

        # remove duplicates while preserving the order
        no_repeats = set()
        return [x for x in languages if x not in no_repeats and not no_repeats.add(x)]


"""

# Write a function that takes in a non-empty array of integers that are
# sorted in ascending order and returns a new array of the dsame length
# with the squares of the original integers also sorted in ascending order

Example:

        sample input = [1,2,3,5,6,8,9]
        output: [1,4,9,25,36,64,81]

"""

def sortedSquaredArray(array):
    # Write your code here.
    sortedSquares = [0 for _ in array]

    smallerValueIndex = 0
    largerValueIndex = len(array) - 1
    current = largerValueIndex
    while smallerValueIndex <= largerValueIndex:
        if abs(array[smallerValueIndex]) < abs(array[largerValueIndex]):
            sortedSquares[current] = array[largerValueIndex] * array[largerValueIndex]
            largerValueIndex -= 1
        else:
            sortedSquares[current] = array[smallerValueIndex] * array[smallerValueIndex]
            smallerValueIndex += 1
        current -= 1
    return sortedSquares

sortedSquaredArray([1, 2, 3, 5, 6, 8, 9])