# encoding=utf-8
import random,os
from PIL import Image

'''
Delete non-jpg files
'''
def DeleteNoJPGFile(dir):
    w = os.walk(dir)
    for path,subpath,files in w:
        for file in files:
            ext = os.path.splitext(file)[1]
            if ext.lower() != '.jpg':
                fullpath = os.path.join(path,file)
                os.remove(fullpath)
                print('delete {0}'.format(fullpath))

'''
Rename file name with prefix and 6 bit number
'''
def RenameFileName(dir,prefix='logo_'):
    w = os.walk(dir)
    num = 1
    for path, subpath, files in w:
        for file in files:
            ext = os.path.splitext(file)[1]
            newfileename = "{0}{1}.jpg".format(prefix,'%06d'%(num))
            num=num+1
            if ext.lower() == '.jpg':
                fullpath = os.path.join(path, file)
                newpath = os.path.join(path,newfileename )
                print(fullpath,newpath)
                os.rename(fullpath,newpath)
                print('rename {0} to {1}'.format(fullpath,newfileename))
'''
Rename directiory name with frefix and 2 bit number
'''
def RenameDir(dir,prefix='sam_'):
    w = os.walk(dir)
    num = 0
    for path, subpath, files in w:
        for sp in subpath:
            newpath = "{0}{1}".format(prefix,'%02d'%(num))
            num=num+1
            print(subpath,newpath)
            os.rename(os.path.join(path,sp),os.path.join(path,newpath))
            print('rename {0} to {1}'.format(subpath,newpath))

'''
Delete the duplicate file, using the file hash method
'''
import hashlib,shutil
def DeleteDuplx(dir):
    backdir = os.path.join(r'D:\img\samples\duplicate')
    if not os.path.exists(backdir):
        os.makedirs(backdir)

    h = hashlib.md5
    picSet = {}

    w = os.walk(dir)
    num = 1
    for path, subpath, files in w:
        for file in files:
            ext = os.path.splitext(file)[1]
            num = num + 1
            if ext.lower() == '.jpg':
                fullpath = os.path.join(path, file)

                picMd5 = ''
                with open(fullpath, 'rb') as pic:
                    picMd5 = hashlib.md5(pic.read()).hexdigest()

                if picMd5 in picSet:
                    shutil.move(fullpath,os.path.join(backdir,file))
                    print('move {0} to {1}!'.format(fullpath,backdir))
                else:
                    picSet[picMd5]=fullpath
                    # print('put {0} to dictionary,and the MD5 is : {1}'.format(fullpath,picMd5))
'''
Resize image size and save as file with prefix and 6 bit number
'''
def Resize(dir,targetdir,width,height,prefix='sam_'):
    if not os.path.exists(targetdir):
        os.makedirs(targetdir)

    num = 0
    w = os.walk(dir)
    for path, subpath, files in w:
        for file in files:
            ext = os.path.splitext(file)[1]
            if ext.lower() == '.jpg':
                fullpath = os.path.join(path, file)
                img = Image.open(fullpath)
                out =img.resize((width,height),Image.ANTIALIAS)
                newfilename = "{0}{1}.jpg".format(prefix,'%06d'%(num))
                num = num+1
                resizedir =os.path.join(targetdir,newfilename)
                out.save(resizedir)

                print('resize {0} to {1}'.format(file,resizedir))

# copy all file to targetdir
def CopyFile(sourdir,targetdir,prefix='m_'):
    if not os.path.exists(targetdir):
        os.makedirs(targetdir)
    num = 0
    w = os.walk(sourdir)
    for path,subpath,files in w:
        for file in files:
            fullpath = os.path.join(path,file)
            newfilename = '{}{}.jpg'.format(prefix,'%06d'%(num))
            tarfullpath = os.path.join(targetdir, newfilename)
            num+=1
            shutil.copy(fullpath, tarfullpath)
            print('move {0} to {1}!'.format(fullpath, tarfullpath))
# copy source file with regurl experssion to target
import re
def CopyFile3(sourdir,targetdir,sourRE = r'\\sam_\d\d',prefix='m_'):
    if not os.path.exists(targetdir):
        os.makedirs(targetdir)
    num = 0
    w = os.walk(sourdir)

    for path,subpath,files in w:
        print(path)
        if re.match(sourRE,path) is  not None:# and re.match('.*sam_\d{2}_\d',path) is None:
            print(path)
            # for file in files:
            #     fullpath = os.path.join(path,file)
            #     newfilename = '{}{}.jpg'.format(prefix,'%06d'%(num))
            #     tarfullpath = os.path.join(targetdir, newfilename)
            #     num+=1
            #     shutil.copy(fullpath, tarfullpath)
            #     print('move {0} to {1}!'.format(fullpath, tarfullpath))

#copy source dir with postfix to targetdir
def CopyFile(sourdir,postfix,targetdir,prefix='m_'):
    if not os.path.exists(targetdir):
        os.makedirs(targetdir)
    num = 0
    w = os.walk(sourdir)
    for path,subpath,files in w:
        print(path)
        if path.endswith(postfix):

            for file in files:
                fullpath = os.path.join(path,file)
                newfilename = '{}{}.jpg'.format(prefix,'%06d'%(num))
                tarfullpath = os.path.join(targetdir, newfilename)
                num+=1
                shutil.copy(fullpath, tarfullpath)
                print('move {0} to {1}!'.format(fullpath, tarfullpath))

if __name__ == '__main__':
    # RenameDir(r'D:\img\samples\resultbyimg')

    dir = r'D:\img\cnnSamples\nor'
    RenameFileName(dir,prefix='nor_')

    # sd = r'D:\img\samples\mil by m\mil sam00-22'
    # td = r'D:\img\samples\mil by m\mil_by_m'
    # CopyFile3(sd,td,sourRE='sam_\d[2][^_]',prefix='m_')

    # dir = r'D:\img\samples\normal'
    # targetdir = r'D:\img\samples\nor_100X100'
    #
    # Resize(dir,targetdir,100,100,prefix='nor_')
    #
    # # DeleteNoJPGFile(path)
    # # RenameFileName(path,'normal_')
    #
    # DeleteDuplx(path)


import datetime
import uuid

from py4web import request, URL
from pydal.validators import *
from .my_validators import IS_ISO_DATETIME

from yatl.helpers import BUTTON, I, A, SPAN, DIV

from .components.grid import Grid

from .constants import *
from .common import db, session, auth, Field
from .util import random_id

from .settings import COLAB_BASE
from .common import url_signer
from .models import is_admin

GRADE_HELP = """Highest valid grade.
If you click on the grade, you can see the list of all grades the student has received
for this assignment, along with the validity of each.  You can then decide which
grades should be considered valid.
"""
LATE_HELP = "Does the student have a late grade that is not valid?"
HOMEWORK_HELP = """
This is the notebook on which the student is working.
Be careful if you modify it to let the student know.
To see the work that was graded, click on the grade, and follow the links to the feedback."""

class ParticipantsGrid(Grid):

    def __init__(self, path, **kwargs):
        super().__init__(path, session, use_id=True,  auth=auth, db=db, signer=url_signer,
                         sort_fields=[
                             db.auth_user.first_name,
                             db.auth_user.last_name,
                             db.auth_user.email,
                             None,
                             db.homework.grade,
                             db.homework.has_invalid_grade,
                         ],
                         default_sort=[0, 0, 1, 0, 0, 0],
                         **kwargs)

    def api(self, id=None):
        """Returns the grid."""
        # Table header.
        header = dict(
            is_header=True,
            cells=[
                dict(text="First Name", sortable=True),
                dict(text="Last Name", sortable=True),
                dict(text="Email", sortable=True),
                dict(text="Notebook", help=HOMEWORK_HELP),
                dict(text="Grade", sortable=True, help=GRADE_HELP),
                dict(text="Late Grades", sortable=True, help=LATE_HELP),
            ],
        )
        # Parses the query.
        req = self._get_request_params(header)
        # Forms the database query.
        query = ((db.homework.assignment_id == id) &
                 (db.homework.student == db.auth_user.email))
        if req.query:
            query &= ((db.auth_user.first_name.startswith(req.query)) |
                      (db.auth_user.last_name.startswith(req.query)) |
                      (db.auth_user.email.startswith(req.query)))
        db_rows = db(query).select(**req.search_args).as_list()
        has_more, result_rows = self._has_more(db_rows)
        # Now creates the results.
        rows = [header]
        for r in result_rows:
            if r["homework"]["has_invalid_grade"]:
                indicator = SPAN(I(_class="fa fa-warning"), _class="icon is-small has-text-danger")
            else:
                indicator = SPAN(I(_class="fa fa-check-square"), _class="icon has-text-success is-small")
            if r["homework"]["drive_id"] is None:
                row_notebook = ""
            else:
                row_notebook = A(I(_class="fa fa-file"), _target="_blank", _href=COLAB_BASE + r["homework"]["drive_id"]).xml()
            rows.append(dict(cells=[
                dict(text=r["auth_user"]["first_name"]),
                dict(text=r["auth_user"]["last_name"]),
                dict(text=r["auth_user"]["email"]),
                dict(html=row_notebook),
                dict(text=r["homework"]["grade"], url=URL("teacher-homework-details", r["homework"]["id"], signer=self.signer)),
                dict(html=A(indicator, _href=URL("teacher-homework-details", r["homework"]["id"], signer=self.signer)).xml()),
            ]))
        return dict(
            page=int(req.page),
            has_search=True,
            has_delete=False,
            has_more=has_more,
            rows=rows
        )


# encoding=utf-8
import os
import re
import math
import time
from nltk.corpus import stopwords
from nltk.stem import SnowballStemmer
import nltk

# the folder of papers
folder_prefix = "./ICML"
# output file
out_file = 'results.txt'
# the dist of categories, whose keys are numbers, e.g., 1,2,3,...,15
categories = {}
# the key of the category to output
category_out = '7'
# the number of all papers
total_papers_num = 0
# the set of words appear in all papers
all_words = set()
# IDF values of all words
words_idf = {}
# the list of words in the lexicographical order
words_index = []

# calculate IDF values of all words
def idf(word, categories):
    num = 0
    for key in categories:
        for paper in categories[key].papers:
            if word in paper.words_tf:
                num += 1
    words_idf[word] = math.log(total_papers_num / num)

# calculate TF values of the word list
def tf(words):
    words_tf = {}
    # the number of words in the paper
    num = len(words)
    for word in words:
        # add the word to global words list
        all_words.add(word)
        if word in words_tf:
            words_tf[word] += 1 / num
        else:
            words_tf[word] = 1 / num
    return words_tf

# calculate TF-IDF values of words of the paper
def tf_idf(paper):
    words_tf_idf = {}
    for word in paper.words_tf:
        words_tf_idf[word] = paper.words_tf[word] * words_idf[word]
    return words_tf_idf

# text pre-processing
def handle_words(content):
    # word segmentation
    tokens = nltk.word_tokenize(content)
    words = []
    stemmer = SnowballStemmer('english')
    for token in tokens:
        # remove stopwords and strings containing non-alphanumeric characters, or whose length is 1
        if re.search(u'^[a-zA-Z]+$', token) and len(token) > 1 and token.lower() not in stopwords.words('english'):
            # translate words into lower case and extract stemmer
            words.append(stemmer.stem(token.lower()))
    return words

def out_category(category_key):
    # in the lexicographical order
    papers_out = sorted(categories[category_key].papers, key=lambda x: x.name)
    for paper in papers_out:
        print("output paper : " + paper.name)
        out_paper(tf_idf(paper))

def out_paper(words_tf_idf):
    f = open(out_file, 'a')
    f.write('[')
    line = ''
    words_index_tf_idf = {}
    for word in words_tf_idf:
        words_index_tf_idf[words_index.index(word) + 1] = words_tf_idf[word]
    words_index_ordered_tf_idf = sorted(words_index_tf_idf.items(), key=lambda w: w[0])
    num = 0
    for words_index_ordered in words_index_ordered_tf_idf:
        if words_index_ordered[1] > 0:
            if num > 0:
                line += ', '
            line += "%d:%.10f" % (words_index_ordered[0], words_index_ordered[1])
            num += 1
    f.write(line)
    f.write(']\n')
    f.close()

def out_stopwords():
    f = open('stopwords.txt', 'a')
    for stopword in stopwords.words('english'):
        f.write(stopword + '\n')
    f.close()

def out_words_index():
    f = open('words_index.txt', 'a')
    num = 1
    for word in words_index:
        f.write(str(num) + '\t' + word + '\n')
        num += 1
    f.close()

class Paper:
    def __init__(self, file, path_prefix):
        global total_papers_num
        total_papers_num += 1
        self.name = file.replace('.txt', '')
        self.words_tf = tf(handle_words(open(os.path.join(path_prefix, file)).read()))
        print("  process the paper: " + self.name)

class Category:
    def __init__(self, folder_name):
        self.name = folder_name.split('.')[1].strip()
        print("process the category: " + self.name)
        path = os.path.join(folder_prefix, folder_name)
        files = os.listdir(path)
        self.papers = [Paper(file, path) for file in files if file != 'desktop.ini']
        print()

if __name__ == '__main__':
    t1 = time.time()
    icml_papers = os.listdir(folder_prefix)
    # all papers of ICML
    for sub_papers in icml_papers:
        key = sub_papers.split('.')[0].strip()
        categories[key] = Category(sub_papers)
    t2 = time.time()
    print("finished pre-processing %s papers,cost:%f s \n" % (total_papers_num, t2 - t1))
    # calculate idf
    for word in all_words:
        idf(word, categories)
    t3 = time.time()
    print("finished calculating IDF,cost:%f s \n" % (t3 - t2))
    # sort all words in the lexicographical order
    words_index = list(all_words)
    words_index.sort()
    # print vectors of target papers
    out_category(category_out)

    print("\noutput stopwords\n")
    out_stopwords()
    print("output words index")
    out_words_index()


import math
from gym import spaces
from Environment import Environment
import numpy as np
import random
from utils import (
    discretize_distance,
    discretize_angle,
    angle_between_points
)

class BunnyEnvironment(Environment):
    def __init__(self, grid_size=20):
        super(BunnyEnvironment, self).__init__(grid_size)
        self.num_angle_bins = 32
        self.distance_bins = [1, 2, 3, 5, 10, 20, 40]
        self.action_space = spaces.Discrete(9)  # Only bunny actions
        self.observation_space = spaces.Tuple([
            spaces.Discrete(grid_size),  # Bunny X
            spaces.Discrete(grid_size),  # Bunny Y
            spaces.Discrete(4),          # Discretized direction to carrot
            spaces.Discrete(len(self.distance_bins) + 1)  # Discretized distance to carrot
        ])
        self.reset()

    def reset(self):
        self.step_counter = 0
        return super().reset()

    def step(self, bunny_action):
        self.step_counter += 1
        new_bunny_pos = self.update_position(self.bunny_pos, bunny_action)
        self.bunny_pos = new_bunny_pos
        self.wolf_pos = self.update_position(self.wolf_pos, random.randint(0, 8))
        reward, done = self.calculate_reward(new_bunny_pos, self.wolf_pos)
        bunny_state = self.calculate_states(self.wolf_pos, new_bunny_pos)
        return bunny_state, reward, done

    def calculate_reward(self, bunny_pos, wolf_pos):
#         if (wolf_pos == bunny_pos):
#             return -50, True
        if bunny_pos == self.location_pos:
            return 100, True

         # Calculate the distance band to determine proximity
        distance_band = discretize_distance(np.linalg.norm(np.array(bunny_pos) - np.array(self.location_pos)), self.distance_bins)
        # The default reward is negative and becomes more negative the further away the bunny is
        # Assuming the first band is the closest, we reverse the distance band order for penalty calculation
        reward = -(len(self.distance_bins) - distance_band) * 2
        # Add a time penalty that gets worse the longer the wolf takes to catch the bunny
        time_penalty = -0.01 * self.step_counter  # Adjust the 0.01 factor to scale the time penalty
        reward += time_penalty
        return reward, False  # Bunny not caught yet


def NN(cities,dist):
    '''
    greedy algorithm / nearest neighbor algorithm
    copied from google-step-tsp/solver_greedy.py
    '''

    N = len(cities)

    current_city = 0
    unvisited_cities = set(range(1, N))
    tour = [current_city]

    while unvisited_cities:

        next_city = min(unvisited_cities,
                        key=lambda city: dist[current_city][city])
        unvisited_cities.remove(next_city)
        tour.append(next_city)
        current_city = next_city

    return tour

def GS(cities):
    '''
    GrahamScan algorithm
    '''
    def find_min_y(cities):

        '''
        y座標最小の点を見つけ、リストの先頭に移動する関数
        '''

        # y座標最小の点を見つける（同じならx座標が小さいほう）
        min_x = cities[0][1]
        min_y = cities[0][2]
        index = 0

        for i in range(1,len(cities)):
            x = cities[i][1]
            y = cities[i][2]
            if y < min_y or y == min_y and x < min_x:
                min_x = x
                min_y = y
                index = i

        # y座標最小の点をリストの先頭にもってくる
        cities[0], cities[index] = cities[index], cities[0]

        return cities

    def sort_by_angle(cities):

        '''
        リストの先頭に移動した地点を基準として角度順に
        リスト全体を並び替える
        '''

        # cities: もともとの辞書の状態をリストに変えてから扱う
        len_cities = len(cities)
        count = 0
        index = 0

        while True:
            count += 1
            index += 1

            angle_ijk = angle(cities[0][1:],cities[index][1:],cities[index+1][1:])

            if angle_ijk < 0:
                cities[index],cities[index+1] = cities[index+1],cities[index]
                count = 0

            if count == len_cities - 1:
                break

            if index == len_cities - 2:
                index = 0

        return cities

    cities_lst = dct_to_lst(cities)
    cities_lst = find_min_y(cities_lst)
    cities_lst = sort_by_angle(cities_lst)

    # 凸包から削除された都市を追加していくリスト
    left_cities = []

    index = -1
    count = 0
    while True:
        index += 1
        count += 1
        angle_ijk = angle(cities_lst[index][1:],cities_lst[index+1][1:],cities_lst[index+2][1:])
        if angle_ijk < 0:
            left_cities.append(cities_lst[index+1])
            del cities_lst[index+1]
            count = 0
        if count == len(cities_lst):
            break
        if index >= len(cities_lst) - 3:
            index -= 1

    return cities_lst,left_cities

def CHI(cities,dist):
    '''
    Convex Hull Insertion algorithm
    '''
    def eval_cost(dist,i,j,k):
        # cityの追加コストを求める関数
        cost = dist[i][k] + dist[k][j] - dist[i][j]
        return cost

    def additional_cost(dist,i,j,k):
        add_cost = (dist[i][k] + dist[k][j]) / dist[i][j]
        return add_cost

    def find_k(convex_hull,left_cities,dist):
        '''
        凸包上の連続した都市をi,jとし、内部の各都市kに対してコストを計算
        kに対するコスト最小とする都市i,jを決定し辞書にkeyをkとして格納していく
        '''
        k_dct = {}
        for k in range(len(left_cities)):
            index_k = left_cities[k][0]

            for i in range(len(convex_hull)):
                index_i = convex_hull[i][0]
                index_j = convex_hull[(i+1)%len(convex_hull)][0]
                cost = eval_cost(dist,index_i,index_j,index_k)
                if k==0:
                    min_cost = cost
                    min_i = i
                    min_i_in_cities = index_i
                    min_j_in_cities = index_j
                else:
                    if cost < min_cost:
                        min_cost = cost
                        min_i = i
                        min_i_in_cities = index_i
                        min_j_in_cities = index_j

            # index_k : left_citiesのk番目の都市の、cities全体でのindex
            # k : left_citiesのk番目の都市のまさにleft_cities上でのindex(=k)
            # min_i : left_citiesのk番目の都市を直後に挿入すべき、convex_hull上のindex
            # min_i(j)_in_cities : left_citiesのk番目の都市を直後に挿入すべき都市の、cities全体でのindex
            k_dct[index_k] = [k,min_i,min_i_in_cities,min_j_in_cities]

        return k_dct

    def add_best_k(convex_hull,left_cities,k_dct,dist):
        '''
        find_kで見つけたi,j,kの組み合わせの中から、
        追加コストが最小となるkを凸包に追加する
        '''
        min_cost = 10**10

        for index_k in k_dct:
            i = k_dct[index_k][2]
            j = k_dct[index_k][3]
            add_cost = additional_cost(dist,i,j,index_k)
            if add_cost < min_cost:
                best_k = index_k

        k = k_dct[best_k][0]
        i = k_dct[best_k][1]

        convex_hull.insert(i+1,left_cities[k])
        del left_cities[k]

        return convex_hull,left_cities

    convex_hull,left_cities = GS(cities)
    # まずグラハムスキャン法により凸包を初期経路として求める

    while left_cities:
        #残りの都市がなくなるまで追加していく
        k_dct = find_k(convex_hull,left_cities,dist)
        convex_hull,left_cities = add_best_k(convex_hull,left_cities,k_dct,dist)

    tour = []
    for i in range(len(convex_hull)):
        tour.append(convex_hull[i][0])

    return tour

def _2opt(cities,tour,max_iter):
    '''
    ランダムに二つの辺を選んで、入れ替えた方が経路が短くなる場合は入れ替える
    '''
    _iter = 0
    len_tour = len(tour)

    checked = set()

    while _iter < max_iter:

        # ランダムに2つの辺を選ぶ
        i = random.randrange(len_tour)
        j = random.randrange(len_tour)

        cnt = 0
        while i==j or (i,j) in checked:
            # 同じ辺を見たり、もうすでに見た組み合わせはできるだけ避ける
            if cnt > 100 and i!=j:
                # max_iterが組み合わせ数より大きい場合、何回やってもすでに見た組み合わせを見ることになる
                # その場合に無限ループにならないように10回やっても被ってたら抜ける
                break
            cnt += 1
            i = random.randrange(len_tour)
            j = random.randrange(len_tour)

        # i番目の辺を構成する頂点の組み合わせをpair_iに格納
        pair_i = []
        pair_i.append(cities[tour[i]])
        pair_i.append(cities[tour[(i+1)%len_tour]])

        # j番目の辺を構成する頂点の組み合わせをpair_jにか右脳
        pair_j = []
        pair_j.append(cities[tour[j]])
        pair_j.append(cities[tour[(j+1)%len_tour]])

        current_dis = distance(pair_i[0],pair_i[1]) + distance(pair_j[0],pair_j[1])
        new_dis = distance(pair_i[0],pair_j[0]) + distance(pair_i[1],pair_j[1])

        if new_dis <= current_dis:
            # 入れ替えた方が距離が短くなる場合、入れ替えを行う

            new_tour = tour[(i+1)%len_tour:(j+1)%len_tour]
            tour[(i+1)%len_tour:(j+1)%len_tour] = new_tour[::-1]

        # すでに検証した組み合わせとして記録する
        checked.add((i,j))

        _iter += 1

    return tour

if __name__ == '__main__':

    import random
    import argparse
    import csv
    import math

    from util import *

    parser = argparse.ArgumentParser(description='Program for solving TSP problem')
    parser.add_argument('-i','--index',help='index of input_csv',default='0')
    parser.add_argument('-m','--max_iter',help='maximum times of iteration of swap operation',default=10000,type=int)
    parser.add_argument('-o','--option',help='which method to use',default='CHI')
    args = parser.parse_args()

    cities = load_input_csv('./google-step-tsp/input_'+args.index+'.csv')

    dist = cities_to_dist(cities)

    if args.option == 'CHI':
        tour = CHI(cities,dist)
    elif args.option == 'NN':
        tour = NN(cities,dist)
    else:
        print('正しい方法を入力してください')
        exit()

    tour = _2opt(cities,tour,args.max_iter)
    print(tour)
    print('sum_length:',calculate_sum_length(cities,tour))

    key = input()
    if key == 'a':
        # 結果を見て記録するかどうかを判断する
        print('recorded!')
        record_tour(tour,args.index)


from rest_framework import serializers

from literature.models import Literature, LiteratureSubSection

class LiteratureSerializer(serializers.ModelSerializer):
    class Meta:
        model = Literature
        fields = [
            'id',
            'title',
            'subtitle',
            'is_subtitle_active',
            'is_sequence_active',
        ]

class LiteratureSubSectionSerializer(serializers.ModelSerializer):
    is_subtitle_active = serializers.BooleanField(source='literature.is_subtitle_active')
    is_sequence_active = serializers.BooleanField(source='literature.is_sequence_active')

    class Meta:
        model = LiteratureSubSection
        fields = [
            'id',
            'title',
            'subtitle',
            'is_subtitle_active',
            'is_sequence_active',
        ]

class LiteratureSubSectionDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = LiteratureSubSection
        fields = [
            'id',
            'title',
            'subtitle',
            'main_content',
        ]


import pygame

class Block:
    width = 30
    height = 30
    color = (0,0,0)
    thickness = 0
    initial_position = (0,0)
    def __init__(self, initial_position):
        x, y = initial_position
        self.position = initial_position
        self.visual = pygame.Rect(x, y, self.width, self.height)

    def update(self, screen):
        x, y = self.position
        inBounds = pygame.Rect(0, 0, 255, 255).collidepoint(x + 1, y + 1)
        if inBounds:
            self.position = (x + 1, y + 1)
            self.visual = pygame.Rect(*self.position, self.width, self.height)
            pygame.draw.rect(screen, self.color, self.visual, self.thickness)
        else:
            self.position = (0, 0)
            self.visual = pygame.Rect(*self.position, self.width, self.height)
            pygame.draw.rect(screen, self.color, self.visual, self.thickness)

def main():
    pygame.init()
    pygame.display.set_caption("First Demo")
    screen = pygame.display.set_mode((255, 255))
    running = True

    block = Block((0,0))

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        screen.fill((255, 255, 255))
        block.update(screen)

        pygame.display.update()

if __name__ == "__main__":
    main()


from mcpi.minecraft import Minecraft
from tqdm import tqdm
import time
import os
mc = Minecraft.create()

def steinarten(stone):
    steinart = []
    for ste in stone:
        if ste[3] not in steinart:
            steinart.append(ste[3])
        else:
            None
    return steinart

def is_int(s):
    s = s.split(",")
    for n in s:
        #print(n)
        #print(type(n))
        try:
            int(n)
            return True
        except(ValueError):
            return False

def asc_range(a, b):
    if a > b:
        return range(b, a+1)
    else:
        return range(a, b+1)

def ask_for_change(stone_rel_pos, antwort, a, b):
    # antwort will be a list. it searches in stone_rel_pos at the a'th position for the 1. Item in antwort and replaces every b'th position of stone_rel_pos with the 2. Item in antwort
    if is_int(antwort):
        antwort = antwort.split(",")
        # print(antwortc[0])
        for stone in stone_rel_pos:
            # print(stone[a])
            if int(stone[a]) == int(antwort[0]):
                stone[b] = int(antwort[1])
                # print(stone[3])
            else:
                None
    elif antwort == "n":
        print("Na gut, dann nicht!")
    else:
        print("Das waren leider keine Zahlen!:-(")

def save_project(stone_rel_pos):
    with open(projektname + ".txt", "w") as f:
        for item in stone_rel_pos:
            row = ','.join(str(x) for x in item)
            f.write("%s\n" % row)

def rel_pos(stone_pos):
    stone_rel_pos = []
    erster_stein = stone_pos[0]
    # print(erster_stein)
    for stein in stone_pos:
        x = erster_stein[0] - stein[0]
        y = erster_stein[1] - stein[1]
        z = erster_stein[2] - stein[2]
        c = [x, y, z, stein[3], stein[4]]
        stone_rel_pos.append([abs(n) for n in c])
    # print(stone_rel_pos)
    return stone_rel_pos

def abtasten(inx_a, inx_b, iny_a, iny_b, inz_a, inz_b):
    stone_pos = []
    # print(inx_a, inx_b, iny_a, iny_b, inz_a, inz_b)
    for Blockx in asc_range(inx_a, inx_b):
        # print(Blockx)
        for Blocky in asc_range(iny_a, iny_b):
            # print(Blocky)
            for Blockz in asc_range(inz_a, inz_b):
                # print(Blockz)
                blockart1 = mc.getBlockWithData(Blockx, Blocky, Blockz)
                blockart1 = list(blockart1)
                print(blockart1)
                stone_pos.append([Blockx, Blocky, Blockz, blockart1[0], blockart1[1]])
    # print(stone_pos)
    return stone_pos

def antwort_umwandeln(antwort):
    splitted = antwort.split(",")
    inx_a = int(splitted[0])
    iny_a = int(splitted[1])
    inz_a = int(splitted[2])
    inx_b = int(splitted[3])
    iny_b = int(splitted[4])
    inz_b = int(splitted[5])
    return inx_a, inx_b, iny_a, iny_b, inz_a, inz_b

def scannen():
    antwort = input("Bitte geben Sie die äußeren Koordinaten Ihres Kunstwerkes an.")
    # print(type(antwort))
    if is_int(antwort) :
        inx_a, inx_b, iny_a, iny_b, inz_a, inz_b = antwort_umwandeln(antwort)
    # print("Antwort umgewandelt.")
        stone_pos = abtasten(inx_a, inx_b, iny_a, iny_b, inz_a, inz_b)
    # print("Abgetastet.")
        stone_rel_pos = rel_pos(stone_pos)
    # print("relpos ermittelt.")
        save_project(stone_rel_pos)
        print("Scannen war erfolgreich. Was Sie da gebaut haben ist aber echt beachtlich!")
        time.sleep(2)
    else :
        print("Das waren keine Koordinaten! Sie müssen nochmal von Vorn beginnen.")
        time.sleep(3)

def bauen(projektname):
    with open(projektname + ".txt", "r") as f:
        stone_rel_pos = []
        idCount = 0
        datenwertCount = 0
        nocoord = False # we suggest, we have coords
        for line in f:
            position = line[:-1].split(",")
            position = [int(x) for x in position]
            stone_rel_pos.append(position)

            # check if all is like we want it to be
            if len(position) < 3:
                print("missing coords for blocks, can't go ahead")
                nocoord = True
                break
            if len(position) < 4: # es könnte theoretisch auch nur die Id verlorengegangen sein, sodass nun der Datenwert als Id eigelesen wird. Ist dann halt so...
                position.append(152)
                idCount += 1
            if len(position) < 5:
                position.append(0)
                datenwertCount += 1

    # no coords means no building
    if nocoord:
        return
    print("Vermisse BlockIds bei {} Blöcken. I set this block's id to 152 (RedstoneBlock). You can later change the ids of all redstoneBlocks to whatever you want.".format(idCount))
    print("Vermisse Datenwert bei {} Blöcken. ersetze ihn durch null.".format(datenwertCount))

    print("Ihr Bauwerk besteht aus folenden Steinarten: " + str(steinarten(stone_rel_pos)))
    antwortc = input("Sollten Sie bestimmte Steine ersetzen wollen geben Sie bitte erst den zu Ersetzenden und dann den gewünschten Steinindex Kommasepariert ein. Drücken Sie andernfalls einfach ein'n'.")
    ask_for_change(stone_rel_pos, antwortc, 3, 3)

    antwortd = input("Sollten Sie den Datenwert eines bestimmten Steins verändern wollen geben Sie bitte den Stein und den gewünschten Datenwert an. Drücken Sie anderenfalls einfach ein 'n'.")
    ask_for_change(stone_rel_pos, antwortd, 3, 4)

    Steinabstand = stone_rel_pos[-1]
    # print(type(position))
    # print(stone_rel_pos)
    print("Ihr Bauwerk hat eine länge von " + str(Steinabstand[0] + 1) + " Steinen, eine Höhe von " + str(Steinabstand[1] + 1) + " Steinen und eine Breite von " + str(Steinabstand[2] + 1) + " Steinen.")
    input("Positionieren Sie sich bitte so, dass Ihr künstlerisches Meisterwerk vor Ihnen gebaut werden kann. Und drücken Sie Enter, um Ihr Bauwerk entstehen zu lassen.")
    x, y, z = mc.player.getPos()
    mc.saveCheckpoint()

    # main building tqdm is for the progress bar
    for posi in tqdm(stone_rel_pos, desc="Building...", ascii=False):
        mc.setBlock(x + posi[0], y + posi[1], z + posi[2], posi[3], posi[4])
        time.sleep(0.001)

    antwortb1 = input("Theoretisch dürften Sie das Duplikat Ihres Werkes nun vor sich sehen. sollte es Ihnen nicht gefallen und Sie möchten es wieder löschen drüscken Sie 'l' anderenfalls einfach nur Enter.")
    if antwortb1 == "l":
        mc.restoreCheckpoint()
        print("Das hätten wir beginnen Sie einfach von Vorn.\n")
    else:
        print("Viel Spaß damit!\n")

try:
    while True:
        projektname = input("Geben Sie einen Projektnamen ein.")
        if os.path.isfile(projektname + ".txt"):
            antwort1 = input("Ah. Sie schon wieder. Wollen Sie Ihr Kunstwerk irgendwohin bauen, oder ein neues einscannen? Geben sie 'b' für bauen und 's' für einscannen ein.")
            if antwort1 == "b":
                bauen(projektname)
            elif antwort1 == "s":
                scannen()
            else:
                print("Es tut mir leid, aber das war nicht meine Frage.")
                time.sleep(3)
        else:
            print("Anscheinend gibt es dieses Projekt noch nicht.")
            scannen()
        antwort5 = input("Wollen Sie das Projekt '" + projektname + "' behalten oder löschen? Drücken Sie 'b' für behalten und 'l' für löschen.")
        if antwort5 == "l":
            os.remove(projektname + ".txt")
            print("Ihr Projekt wurde gelöscht!\n")
        elif antwort5 == "b":
            print("Ihr Projekt wurde gespeichert unter " + os.path.realpath(projektname + ".txt") + ".\n")
        else:
            print("Das hatte ich nicht gefragt. Naja egal Ihr projekt hab ich jetzt einfach unter" + os.path.realpath(projektname + ".txt") + " gespeichert.\n")
except KeyboardInterrupt:
    print("\nAuf Wiedersehen!")


import time
from contextlib import ContextDecorator
from statistics import mean, median

class Timer(ContextDecorator):
    name_instance_mapping = dict()

    def __new__(cls, *args, **kwargs):
        #  import pdb; pdb.set_trace()
        if len(args) == 1:
            name = args[0]
            if name in cls.name_instance_mapping:
                return cls.name_instance_mapping[name]
            else:
                instance = super().__new__(cls)
                cls.name_instance_mapping[name] = instance
                return instance
        else:
            return super().__new__(cls)

    def __init__(self, name=None, *, func=None, parent=None):
        self.runs = []
        self.func = func
        self.split_timers = []
        self.split_names = []
        self.parent = parent
        self.in_use = False

    def __enter__(self):
        if self.parent and not self.parent.in_use:
            raise RuntimeError("Cannot split because parent timer is not running")
        self.in_use = True
        self.start = time.time()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.elapsed = time.time() - self.start
        self.runs.append(self.elapsed)
        self.in_use = False
        return False

    def __call__(self, *args, **kwargs):
        with self:
            ret = self.func(*args, **kwargs)
        return ret

    @property
    def mean(self):
        return mean(self.runs)

    @property
    def median(self):
        return median(self.runs)

    @property
    def max(self):
        return max(self.runs)

    @property
    def min(self):
        return min(self.runs)

    def __getitem__(self, identifier):
        if isinstance(identifier, int):
            return self.split_timers[identifier]
        else:
            return self.split_timers[self.split_names.index(identifier)]

    def split(self, name=None):
        if name and name in self.split_names:
            return self.__getitem__(name)

        self.split_names.append(name)
        self.split_timers.append(Timer(parent=self))
        return self.split_timers[-1]


import re
import time
from typing import List, Pattern, Optional, Callable

# from sh import tail

__all__ = ["LogEvent"]

class LogEvent(object):
    FAILED = -1
    WAITING = 0
    STARTED = 1
    SUCCESS = 2
    _subber = re.compile(r"`(\d+)`")

    def __init__(
        self,
        start_regex: Pattern,
        end_regex: Optional[Pattern] = None,
        title: str = "",
        timeout: float = 5,
        autoreset: bool = False,
    ):
        """
        Defines a regular expression (or optionally 2) that will be
        searched in a log file.

        Args:
            start_regex(re.Pattern):
                If the current log line matches this, a timer is started and
                we attempt to match for the end_regex expression.
            end_regex(re.Pattern): [Optional]
                After the start_regex is found,
                we will attempt to match with this pattern(if provided).
                If not provided, the status is immediately set to SUCCESS.
                If provided and this pattern is found within the timeout,
                the event is a success, else fail.
            timeout(float): Timeout period in seconds
            autoreset(float): On completion of event, this will immediately
                reset the instance and begin looking for the start_regex again.
                Useful if the callbacks are set and are used to trigger
                external activity.
        """
        self.sr = start_regex
        self.er = end_regex
        self.timeout = timeout
        self.loglines: List[str] = []
        self._matches = None

        self._state = self.WAITING
        self._must_reset = autoreset
        self._starttime = time.time()
        if len(title) == 0:
            self.title = self.sr.pattern
        else:
            self.title = title

    def _reinit(self):
        self._state = self.WAITING
        self.loglines = []
        self._matches = None

    def process(self, line: str):
        if (time.time() - self._starttime) > self.timeout:
            self._reason = "Timed out"
            self.set_state(self.FAILED)
        if line == "":
            # not a new line
            return
        if self._state == self.WAITING:
            # Look for first pattern
            matches = self.sr.search(line)
            if matches:
                # TODO: Use _matches to find previously found
                # strings in end_regex
                self._matches = matches.groups()
                self.set_state(self.STARTED)
                self.loglines.append(line)
                if self.er is None:
                    self.set_state(self.SUCCESS)
        elif self._state == self.STARTED:
            # Look for second pattern
            self.loglines.append(line)
            if self.er.search(line):
                # Found
                self.set_state(self.SUCCESS)

    @property
    def reason(self) -> str:
        if self._state == self.SUCCESS:
            return ""
        if hasattr(self, "_reason"):
            return self._reason
        elif self._state == self.STARTED:
            return "End pattern not found"
        elif self._state == self.WAITING:
            return "Start pattern not found"

    @property
    def on_success(self) -> Callable[..., None]:
        if hasattr(self, "_on_success"):
            return self._on_success
        else:
            return lambda x: None

    @on_success.setter
    def on_success(self, callback: Callable[..., None]):
        self._on_success = callback

    @property
    def on_failed(self) -> Callable[..., None]:
        if hasattr(self, "_on_failed"):
            return self._on_failed
        else:
            return lambda x: None

    @on_failed.setter
    def on_failed(self, callback: Callable[..., None]):
        self._on_failed = callback

    def is_complete(self) -> bool:
        return self._state in [self.SUCCESS, self.FAILED]

    def set_state(self, state):
        if state == self.SUCCESS:
            self._state = self.SUCCESS
            self.on_success(self)
            if self._must_reset:
                self._reinit()
        elif state == self.FAILED:
            self._state = self.FAILED
            self.on_failed(self)
            if self._must_reset:
                self._reinit()
        elif state == self.STARTED:
            self._starttime = time.time()
            self._state = self.STARTED

    def success(self) -> bool:
        return self._state == self.SUCCESS

    def failed(self) -> bool:
        return self._state == self.FAILED

    def __str__(self) -> str:
        SUCCESS = "SUCCESS\x1b[0m"
        FAIL = "FAIL"
        TITLE = "\x1b[1m" + self.title + "\x1b[0m"

        result = []

        if self.success():
            result.append(SUCCESS + ": " + TITLE)

        elif self.failed():
            result.append(FAIL + ": " + TITLE)
        else:
            state_str = ""
            if self._state == LogEvent.WAITING:
                state_str = "WAITING"
            elif self._state == LogEvent.STARTED:
                state_str = "IN PROGRESS"
            result.append(state_str + ": " + TITLE)
        if self.er:
            end_pat = f"-> ({self.er.pattern})"
        else:
            end_pat = ""
        result.append(f"Patterns: ({self.sr.pattern}) {end_pat}")
        if self.reason:
            result.append("Reason: " + self.reason)
        if self._matches:
            result.append(f"Groups: {self._matches}")
        return "\n".join(result)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Combines the maxelev.gr3 files into a single netcdf with corresponding experiment
information.

It produces a netCDF file, similar to the structure of SHCISM output.
The mesh structure is derived from the input hgrid.gr3 file.

Author: Jamal Uddin Khan
Email: jamal.khan@legos.obs-mip.fr
"""

import numpy as np
import netCDF4
import os
from glob import glob

# Gr3 Object to read and write the gr3 formatted files
class Gr3(object):
    def __init__(self, grname=None, nelem=0, nnode=0, nodes=[], elems=[]):
        self.grname = grname
        self.nelem = nelem
        self.nnode = nnode
        self.nodes = nodes
        self.elems = elems

    def read(self, fname, readnodes=True, readelements=True):
        __file = fname
        with open(__file) as f:
            ds = f.readlines()
            __line = 0

            # Reading the gr3 name
            self.grname = ds[__line].strip()
            __line = __line + 1

            # Reading the number of nodes and elements
            __nelem, __nnode = np.fromstring(string=ds[__line].split('\n')[0], count=2, sep=' ')
            self.nelem = int(__nelem)
            self.nnode = int(__nnode)
            __line = __line + 1

            if readnodes:
                # Reading the nodes
                __nodes = np.genfromtxt(fname=ds[__line:__line+self.nnode])
                __line = __line + self.nnode
                self.nodes = np.array(__nodes)

            if readelements:
                # Reading the elements
                __elems = np.genfromtxt(fname=ds[__line:__line+self.nelem], dtype=int)
                __line = __line + self.nelem
                self.elems = np.array(__elems, dtype=int)
                self.elems = self.elems[:, 2:]

if __name__=='__main__':
    # Inputs
    in_folder = './maxelev'
    in_files = glob(os.path.join(in_folder, 'Track_*.gr3'))

    mesh_file = './hgrid.gr3'
    mesh_data = Gr3()
    mesh_data.read(fname=mesh_file, readnodes=True, readelements=True)

    n_experiment = len(in_files)
    print(f'No of experiment to merge: {n_experiment:04d}')
    print(f'Depth file : {mesh_file:s}')

    # netCDF file
    try:
        nc = netCDF4.Dataset('maxelev.nc', 'w', format='NETCDF4', clobber=True)

        nc.createDimension(dimname='nSCHISM_hgrid_node', size=mesh_data.nnode)
        nc.createDimension(dimname='nSCHISM_hgrid_face', size=mesh_data.nelem)
        nc.createDimension(dimname='nMaxSCHISM_hgrid_face_nodes', size=4)
        nc.createDimension(dimname='n_experiment', size=n_experiment)

        var_node_x = nc.createVariable(varname='SCHISM_hgrid_nodex', datatype=float, dimensions=('nSCHISM_hgrid_node'))
        var_node_x.long_name = 'Node x-coordinate'
        var_node_x.standard_name = 'Longitude'
        var_node_x.units = 'degrees east'
        var_node_x[:] = mesh_data.nodes[:, 1]

        var_node_y = nc.createVariable(varname='SCHISM_hgrid_nodey', datatype=float, dimensions=('nSCHISM_hgrid_node'))
        var_node_y.long_name = 'Node y-coordinate'
        var_node_y.standard_name = 'Latitude'
        var_node_y.units = 'degrees north'
        var_node_y[:] = mesh_data.nodes[:, 2]

        var_elem = nc.createVariable(varname='SCHISM_hgrid_face_nodes', datatype=int, dimensions=('nSCHISM_hgrid_face', 'nMaxSCHISM_hgrid_face_nodes'))
        var_elem.long_name = 'Element Connectivity Table'
        var_elem[:, 0:3] = mesh_data.elems[:, 0:3]

        var_depth = nc.createVariable(varname='depth', datatype=float, dimensions=('nSCHISM_hgrid_node'))
        var_depth.long_name = 'Bathymetry at node'
        var_depth.standard_name = 'Depth'
        var_depth.convention = 'Positive downard'
        var_depth[:] = mesh_data.nodes[:, 3]

        var_exps = nc.createVariable(varname='experiment', datatype=int, dimensions=('n_experiment'))
        var_exps.long_name = 'Track no of the experiment'
        var_exps.ensemble = 'Kerry Emmanuel'
        var_exps[:] = np.arange(start=1, stop=n_experiment+1, step=1)

        var_maxelev = nc.createVariable(varname='maxelev', datatype=float, dimensions=('n_experiment', 'nSCHISM_hgrid_node'))
        var_maxelev.long_name = 'Maximum elevation'
        var_maxelev.standard_name = 'Maximum elevation'
        var_maxelev.units = 'm'
        var_maxelev.datum = 'MSL'

        nc.experiment_setup = 'Hydro + 35cm Sa'
        nc.contact = 'Jamal Khan'
        nc.email = 'jamal.khan@legos.obs-mip.fr'
    except:
        print('netCDF Error!')
    else:
        track_data = Gr3()
        for i, in_file in enumerate(in_files):
            print(in_file)
            track_no = int(os.path.basename(in_file).split('.')[0].split('_')[1])
            track_data.read(in_file, readnodes=True, readelements=False)
            var_maxelev[track_no-1, :] = track_data.nodes[:, 3]

            if i%100 == 0:
                nc.sync()
    finally:
        nc.close()


import os

from absl.testing import parameterized

from tensorflow.python.distribute import combinations
from tensorflow.python.distribute import strategy_combinations
from tensorflow.python.framework import ops
from tensorflow.python.ops import variable_scope
from tensorflow.python.ops import variables
from tensorflow.python.platform import test
from tensorflow.python.training import checkpoint_utils
from tensorflow.python.training import saver as saver_lib

def _create_checkpoints(sess, checkpoint_dir):
  checkpoint_prefix = os.path.join(checkpoint_dir, "model")
  checkpoint_state_name = "checkpoint"
  v1 = variable_scope.get_variable("var1", [1, 10])
  v2 = variable_scope.get_variable("var2", [10, 10])
  sess.run(variables.global_variables_initializer())
  v1_value, v2_value = sess.run([v1, v2])
  saver = saver_lib.Saver()
  saver.save(
      sess,
      checkpoint_prefix,
      global_step=0,
      latest_filename=checkpoint_state_name)
  return v1_value, v2_value

class CheckpointUtilsWithDistributionStrategyTest(
    test.TestCase, parameterized.TestCase):

  def _get_test_object(self):
    checkpoint_dir = self.get_temp_dir()
    with self.cached_session() as session:
      v1, v2 = _create_checkpoints(session, checkpoint_dir)
    return checkpoint_dir, v1, v2

  @combinations.generate(
      combinations.combine(
          distribution=[
              strategy_combinations.default_strategy,
              strategy_combinations.one_device_strategy,
              strategy_combinations.mirrored_strategy_with_gpu_and_cpu,
              strategy_combinations.mirrored_strategy_with_two_gpus,
              strategy_combinations
              .mirrored_strategy_with_two_gpus_no_merge_call,
          ],
          in_replica_mode=[True, False],
          mode=["graph"]))
  def testInitFromCheckpoint(self, distribution, in_replica_mode):
    checkpoint_dir, v1_value, v2_value = self._get_test_object()

    def init_and_verify(g):
      v1 = variable_scope.get_variable("new_var1", [1, 10])
      v2 = variable_scope.get_variable(
          "new_var2", [10, 10],
          synchronization=variable_scope.VariableSynchronization.ON_READ,
          aggregation=variable_scope.VariableAggregation.MEAN)
      checkpoint_utils.init_from_checkpoint(checkpoint_dir, {
          "var1": "new_var1",
          "var2": "new_var2"
      })
      with self.session(graph=g) as session:
        session.run(variables.global_variables_initializer())
        self.assertAllEqual(v1_value, self.evaluate(v1))
        self.assertAllEqual(v2_value, self.evaluate(v2))

    with ops.Graph().as_default() as g, distribution.scope():
      if in_replica_mode:
        distribution.extended.call_for_each_replica(init_and_verify, args=[g])
      else:
        init_and_verify(g)

  @combinations.generate(
      combinations.combine(
          distribution=[
              strategy_combinations.default_strategy,
              strategy_combinations.one_device_strategy,
              strategy_combinations.mirrored_strategy_with_gpu_and_cpu,
              strategy_combinations.mirrored_strategy_with_two_gpus,
              strategy_combinations
              .mirrored_strategy_with_two_gpus_no_merge_call,
          ],
          in_replica_mode=[True, False],
          mode=["graph"]))
  def testInitFromDifferentNameObject(self, distribution, in_replica_mode):
    checkpoint_dir, v1_value, _ = self._get_test_object()

    def init_and_verify(g):
      v1 = variable_scope.get_variable("new_var1", [1, 10])
      # Use string add to create new object in each replica
      prefix = "new_"
      suffix = "var1"
      new_var1 = prefix + suffix
      checkpoint_utils.init_from_checkpoint(checkpoint_dir, {
          "var1": new_var1,
      })
      with self.test_session(graph=g) as session:
        session.run(variables.global_variables_initializer())
        self.assertAllEqual(v1_value, self.evaluate(v1))

    with ops.Graph().as_default() as g, distribution.scope():
      if in_replica_mode:
        distribution.extended.call_for_each_replica(init_and_verify, [g])
      else:
        init_and_verify(g)

if __name__ == "__main__":
  test.main()


import torch
import torch.nn as nn

class Add_Norm(nn.Module):
    def __init__(self):
        super(Add_Norm, self).__init__()
        self.dropout = nn.Dropout(0.1)#parameters to be added

    def forward(self,x,sub_layer,**kwargs):
        "sub_layer can be self-attention or feed_forward"
        sub_output = sub_layer(x,**kwargs)
        x = self.dropout(x + sub_output)

        layer_norm = nn.LayerNorm(x.size()[1:])
        layer_norm.to(device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'))
        out = layer_norm(x)
        return out


from django.urls import path
from . import views

app_name = 'movies'
urlpatterns = [
    path('', views.index, name="index"),
    path('<int:id>/', views.detail, name="detail"),
    path('<int:id>/review_create', views.review_create, name="review_create"),
    path('<int:review_id>/review_delete/<int:movie_id>/', views.review_delete, name="review_delete"),
    path('<int:id>/like/', views.like, name='like')
]


from django.shortcuts import render
from django.http import HttpResponse
from mainpage.models import Query,Domain
# Create your views here.
def indexpage(request):
    alldomains=Domain.objects.all()
    return render(request,"index.html",{'alldomains':alldomains})
def submitdata(request):
    if request.method=="POST":
        name=request.POST["nameofclient"]
        email=request.POST["emailofclient"]
        cocode=request.POST["ccode"]
        phoneno=request.POST["phonenoofclient"]
        message=request.POST["messagedata"]
        user=Query(name=name,email=email,countrycode=cocode,phone=phoneno,message=message)
        user.save()
        return render(request,"querysubmit.html")
    return HttpResponse("not found")


from collections import defaultdict, Counter

import numpy

from Bio import pairwise2

from variation import (GT_FIELD, CHROM_FIELD, POS_FIELD, REF_FIELD, ALT_FIELD,
                       MISSING_INT, MISSING_STR)
from variation.variations.filters import IndelFilter, FLT_VARS, SELECTED_VARS
from variation.matrix.stats import counts_and_allels_by_row
from variation.gt_writers.vcf import _join_str_array_along_axis0

INDEL_CHAR = b'-'

class AlignmentTooDifficultError(ValueError):
    pass

def _do_easy_multiple_alignment(alleles, lengths):
    longest_allele_idx = numpy.argmax(numpy.array(lengths))
    longest_allele = alleles[longest_allele_idx]

    aligned_alleles = []
    for allele in alleles:
        if allele == longest_allele:
            aligned_allele = allele
        elif not allele.strip():
            aligned_allele == INDEL_CHAR * len(allele)
        else:
            alignment = pairwise2.align.globalxx(longest_allele.decode(),
                                                 allele.decode())[0]
            aligned_allele = alignment[1].encode()
        aligned_alleles.append(aligned_allele)

    # check that this simple multiple alignment is fine
    for idx in range(len(longest_allele)):
        nucleotides = {allele[idx] for allele in aligned_alleles}.difference([45])
        if len(nucleotides) > 1:
            raise AlignmentTooDifficultError('Alignment too difficult')
    return aligned_alleles

def _fix_allele_lengths(alleles, try_to_align_easy_indels,
                        put_hyphens_in_indels):
    if not put_hyphens_in_indels:
        return alleles

    lengths = [len(allele) for allele in alleles]
    one_length = len(alleles[0])
    if all(length == one_length or not length for length in lengths):
        return alleles

    if max(lengths) == 2:
        alleles = [allele + INDEL_CHAR if len(allele) == 1 else allele for allele in alleles]
        return alleles

    if try_to_align_easy_indels:
        alleles = _do_easy_multiple_alignment(alleles, lengths)
    else:
        raise RuntimeError('We should not be here')

    one_length = len(alleles[0])
    if not all([len(allele) == one_length for allele in alleles]):
        raise AlignmentTooDifficultError('Alignment too difficult')

    return alleles

def write_fasta(variations, out_fhand, remove_indels=True,
                write_one_seq_per_sample_setting_hets_to_missing=False,
                remove_invariant_snps=False, remove_sites_all_N=True,
                try_to_align_easy_indels=False, put_hyphens_in_indels=True):

    if not remove_indels:
        if try_to_align_easy_indels and not put_hyphens_in_indels:
            msg = 'try_to_align and not hyphens_in_indels are incompatible options'
            raise ValueError(msg)
        if try_to_align_easy_indels and put_hyphens_in_indels:
            pass
        if not try_to_align_easy_indels and not put_hyphens_in_indels:
            pass
        if not try_to_align_easy_indels and put_hyphens_in_indels:
            msg = 'not try_to_align and put hyphens_in_indels are incompatible options'
            raise ValueError(msg)

    stats = {}
    stats['snps_tried'] = 0
    stats['complex_skipped'] = 0
    stats['snps_written'] = 0

    samples = variations.samples

    chroms = variations[CHROM_FIELD] if CHROM_FIELD in variations else None
    poss = variations[POS_FIELD] if POS_FIELD in variations else None

    if remove_indels:
        filter_indels = IndelFilter(report_selection=True)
        result = filter_indels(variations)
        stats['indels_removed'] = numpy.sum(numpy.logical_not(result[SELECTED_VARS]))
        variations = result[FLT_VARS]
        if chroms is not None:
            chroms = chroms[result['selected_vars']]
        if poss is not None:
            poss = poss[result['selected_vars']]

    N = b'N'
    desc = b''

    if chroms is not None and poss is not None:
        chrom0 = chroms[0].astype('S')
        pos0 = poss[0]
        chrom1 = chroms[-1].astype('S')
        pos1 = poss[-1]
        desc = b' From %s:%i to %s:%i' % (chrom0, pos0, chrom1, pos1)
        if stats is not None:
            stats['start_chrom'] = chrom0
            stats['start_pos'] = chrom0
            stats['end_chrom'] = chrom1
            stats['end_pos'] = chrom1
        if chrom0 == chrom1:
            desc += b' length covered:%i' % (pos1 - pos0)
            if stats is not None:
                stats['length_covered'] = pos1 - pos0

    refs = variations[REF_FIELD]
    alts = variations[ALT_FIELD]
    gts = variations[GT_FIELD][...]

    if write_one_seq_per_sample_setting_hets_to_missing:
        if gts.shape[2] != 2:
            raise NotImplementedError('Not implemented yet for non diploids')

        # remove hets
        haps1 = gts[:, :, 0]
        haps2 = gts[:, :, 1]
        haps1[haps1 != haps2] = MISSING_INT
        shape = haps1.shape
        haplotypes = haps1.reshape((shape[0], shape[1], 1))
        # haps = haps1
    else:
        haplotypes = gts

    if remove_invariant_snps or remove_sites_all_N:
        all_counts, alleles = counts_and_allels_by_row(haplotypes)
        try:
            missing_allele_idx = alleles.index(MISSING_INT)
        except ValueError:
            missing_allele_idx = None
        if missing_allele_idx is None:
            counts = all_counts
            snps_not_all_missing = None
        elif missing_allele_idx == 0:
            counts = all_counts[:, 1:]
            snps_not_all_missing = all_counts[:, missing_allele_idx] < haplotypes.shape[1]
        else:
            raise NotImplementedError('Should be an easy fix')

    haps_to_keep = numpy.ones((haplotypes.shape[0],)) == 1
    if remove_invariant_snps:
        this_haps_to_keep = numpy.sum(counts, axis=1) - numpy.max(counts, axis=1) > 0
        haps_to_keep = numpy.logical_and(haps_to_keep, this_haps_to_keep)

    if remove_sites_all_N and snps_not_all_missing is not None:
        this_haps_to_keep = snps_not_all_missing
        haps_to_keep = numpy.logical_and(haps_to_keep, this_haps_to_keep)

    if haps_to_keep is not None:
        haplotypes = haplotypes[haps_to_keep, ...]
        alts = alts[haps_to_keep]
        refs = refs[haps_to_keep]

    if alts.dtype.itemsize > refs.dtype.itemsize:
        str_dtype = alts.dtype
    else:
        str_dtype = refs.dtype

    letter_haps = numpy.full_like(haplotypes, dtype=str_dtype, fill_value=b'')

    for snp_idx in range(haplotypes.shape[0]):
        stats['snps_tried'] += 1
        alleles = [refs[snp_idx]] + list(alts[snp_idx, :])

        lengths = [len(allele) for allele in alleles]
        len_longest_allele = max(lengths)
        empty_allele = N * len_longest_allele
        letter_haps[snp_idx, :, :] = empty_allele

        try:
            alleles = _fix_allele_lengths(alleles,
                                          try_to_align_easy_indels=try_to_align_easy_indels,
                                          put_hyphens_in_indels=put_hyphens_in_indels)
        except AlignmentTooDifficultError:
            # we don't know how to align this complex, so we skip it
            stats['complex_skipped'] += 1
            continue

        stats['snps_written'] += 1

        ref_allele = alleles[0]
        alt_alleles = alleles[1:]

        letter_haps[snp_idx, :, :][haplotypes[snp_idx, :, :] == 0] = ref_allele
        for alt_allele_idx in range(len(alt_alleles)):
            alt_allele = alt_alleles[alt_allele_idx]
            if alt_allele == MISSING_STR:
                break
            letter_haps[snp_idx, :, :][haplotypes[snp_idx, :, :] == alt_allele_idx + 1] = alt_allele

    joined_letter_haps = []
    for idx in range(letter_haps.shape[2]):
        joined_letter_haps.append(_join_str_array_along_axis0(letter_haps[:, :, idx].T,
                                                              the_str_array_has_newlines=False))

    lengths = []
    for smpl_idx, sample in enumerate(samples):
        for haploid_idx, haploid_joined_letter_haps in enumerate(joined_letter_haps):
            if write_one_seq_per_sample_setting_hets_to_missing:
                this_desc = b'>%s' % sample.encode() + desc
            else:
                this_desc = b'>%s_hap%d' % (sample.encode(), haploid_idx + 1) + desc
            out_fhand.write(this_desc)
            out_fhand.write(b'\n')
            sample_hap = haploid_joined_letter_haps[smpl_idx]
            lengths.append(len(sample_hap))
            out_fhand.write(sample_hap)
            out_fhand.write(b'\n')

    if put_hyphens_in_indels:
        assert all(length == lengths[0] for length in lengths)

    return {'stats': stats}


import datetime
import sqlite3

from model import Task

conn = sqlite3.connect('tasktracker.db')
cur = conn.cursor()

def create_table():
    cur.execute(
        """CREATE TABLE IF NOT EXISTS tasktracker(id integer, task text, tag text, date_added text, date_completed text, status integer)""")

create_table()

def insert(task: Task):
    cur.execute('SELECT COUNT(*) FROM tasktracker')
    count = cur.fetchone()[0]
    task.id = count if count else 0
    with conn:
        cur.execute('INSERT INTO tasktracker VALUES(:id, :task, :tag, :date_added, :date_completed, :status)',
                    {'id': task.id, 'task': task.task, 'tag': task.tag, 'date_added': task.date_added,
                     'date_completed': task.date_completed, 'status': task.status})

def select_all() -> list[Task]:
    cur.execute('SELECT * FROM tasktracker')
    res_all = cur.fetchall()
    tasks = []
    for res in res_all:
        tasks.append(Task(*res))
    return tasks

def delete(id):
    cur.execute('SELECT COUNT(*) FROM tasktracker')
    count = cur.fetchone()[0]

    with conn:
        cur.execute('DELETE FROM tasktracker WHERE id = :id', {'id': id})

    for pos in range(id + 1, count):
        update_id(pos, pos - 1, False)

def update_id(old_id: int, new_id: int, commit=True):
    with conn:
        cur.execute('UPDATE tasktracker SET id = :new_id WHERE id = :old_id',
                    {'new_id': new_id, 'old_id': old_id})
        if commit:
            conn.commit()

def update(id: int, task: str, tag: str):
    with conn:
        if task is not None and tag is not None:
            cur.execute('UPDATE tasktracker SET task = :task, tag = :tag WHERE id = :id',
                        {'task': task, 'tag': tag, 'id': id})
        elif task is not None:
            cur.execute('UPDATE tasktracker SET task = :task WHERE id = :id',
                        {'tag': tag, 'id': id})
        elif tag is not None:
            cur.execute('UPDATE tasktracker SET tag = :tag WHERE id = :id',
                        {'tag': tag, 'id': id})

def complete(id: int):
    with conn:
        cur.execute('UPDATE tasktracker SET status = "1", date_completed = :date_completed WHERE id = :id',
                    {'id': id, 'date_completed': datetime.datetime.now().isoformat()})


from help_function import *
import matplotlib.pyplot as plt
from loss_function import *
from  activation_function import *
from network_layer import *
from  Performance import *
from optimizer import *

# train_x: 训练数据  train_y：训练标签 test_x：测试数据  test_y：测试标签
# layers：定义的网络 loss：损失函数  训练批大小：batch_size 学习率：eta
# 训练次数：epoch 误差计算样本数：n_sample interval 显示间隔：interval
# optimizer: 优化器   alpha：alpha
def train(train_x, train_y, test_x, test_y, layers, loss, batch_size=100, eta=0.001, epoch=50, n_sample=200,
          interval=10, optimizer=Adagrad, alpha=0.9):
    n_train = train_x.shape[0]  # 训练数据的采样数
    n_test = test_x.shape[0]  # 测试数据的采样数
    n_batch = n_train // batch_size
    train_error = []
    test_error = []
    for i in range(epoch):
        # -- 误差的测算 --
        # 在训练过程中选取部分样本进行误差计算
        x, y, a = forward_sample(layers, train_x, train_y, n_sample)
        error_train = get_error(a, y, n_sample, loss)

        x, y, a = forward_sample(layers, test_x, test_y, n_sample)
        error_test = get_error(a, y, n_sample, loss)

        train_error.append(error_train)
        test_error.append(error_test)

        # -- 处理进度的显示 --
        if interval != 0 and i % interval == 0:
            print("Epoch:" + str(i) + "/" + str(epoch),
                  "  Error_train:" + str(error_train),
                  "  Error_test:" + str(error_test))

        # -- 学习 --
        # 随机选取样本进行学习
        index_rand = np.arange(n_train)
        np.random.shuffle(index_rand)
        for j in range(n_batch):
            mb_index = index_rand[j * batch_size: (j + 1) * batch_size]
            x = train_x[mb_index, :]
            t = train_y[mb_index, :]

            a, z = forward_propagation(layers, x, True)

            grad_w, grad_b = backpropagation(layers, t, a, z, loss)
            uppdate_wb(layers, grad_w, eta, optimizer, grad_b, alpha)

    # -- 显示记录误差的表格 --

    plt.plot(train_error, label="Train")
    plt.plot(test_error, label="Test")
    plt.legend()
    plt.xlabel("Epochs")
    plt.ylabel("Error")
    plt.show()

    # -- 正确率的测定 --
    x, y, a = forward_sample(layers, train_x, train_y, n_train)
    count_train = np.sum(np.argmax(a, axis=1) == np.argmax(y, axis=1))

    x, y, a = forward_sample(layers, test_x, test_y, n_test)
    count_test = np.sum(np.argmax(a, axis=1) == np.argmax(y, axis=1))

    if interval != 0:
        print("Accuracy Train:", str(count_train / n_train * 100) + "%",
              "Accuracy Test:", str(count_test / n_test * 100) + "%")
    return a, y


# -*- coding: utf-8 -*-
"""
    Zenesque Colorscheme
    ~~~~~~~~~~~~~~~~~~~~

    Converted by Vim Colorscheme Converter
"""
from pygments.style import Style
from pygments.token import Token, String, Keyword, Number, Operator, Comment, Name, Generic

class ZenesqueStyle(Style):

    background_color = '#0f1216'
    styles = {
        Token:              'noinherit #adadad bg:#0f1216',
        String:             'noinherit #5d7a64 italic',
        Name.Function:      'noinherit #9784a2 bold',
        Generic.Output:     '#7e7e7e bold',
        Comment.Preproc:    'noinherit #5567a1 italic',
        Generic.Error:      '#727272',
        Generic.Traceback:  'noinherit #6f6f6f',
        Generic.Inserted:   'noinherit bg:#5a7e5d',
        Name.Constant:      'noinherit #8d5c57 bold',
        Keyword.Type:       '#518991 bold italic',
        Name.Tag:           '#5a89a4 italic',
        Name.Variable:      'noinherit #a16f51 bold',
        Name.Entity:        'noinherit #a7a863',
        Generic.Deleted:    'noinherit #bf6a6a bg:#2d1212',
        Comment:            'noinherit #777777 italic',
        Generic.Subheading: '#6d6d6d bold',
        Generic.Heading:    '#6d6d6d bold',
        Keyword:            '#5a89a4 italic',
        Name.Attribute:     'noinherit #9784a2 bold',
    }


from ctypes import *
from threading import Thread
from time import sleep

import matplotlib.pyplot as plt
import numpy

# import matplotlib
# matplotlib.rcParams['interactive'] == True

# print(plt.get_backend())

ret_spi_transfer = 0

MAX = 512

so_file = "./main.so"
c_lib = CDLL(so_file)

# https://stackoverflow.com/questions/54888242/passing-nested-ctypes-structure-with-array-to-c-library-function-not-as-expected

class SPI_CONFIG(Structure):
    _fields_ = [("freq", c_uint32),
                ("time_delay", c_float),
                ("mode", c_uint8),
                ("is_msb_First", c_bool),
                ("bits_per_word", c_uint8)]

class SPI_PINS(Structure):
    _fields_ = [("cs", c_int),
                ("clk", c_int),
                ("mosi", c_int),
                ("miso", c_int),
                ("time_ns", c_uint64)]

SPI_PINS_MAX = SPI_PINS * MAX

class CBFIFO(Structure):
    _fields_ = [("spi_pins_status", SPI_PINS_MAX),
                ("head", c_int),
                ("tail", c_int),
                ("count", c_int)]

    def __init__(self, data):
        self.spi_pins_status = SPI_PINS_MAX()
        self.head = 0
        self.tail = 0
        self.count = 0
        for i in range(len(data)):
            self.spi_pins_status[i].cs = data[i][0]
            self.spi_pins_status[i].clk = data[i][1]
            self.spi_pins_status[i].mosi = data[i][2]
            self.spi_pins_status[i].miso = data[i][3]
            self.spi_pins_status[i].time_ns = data[i][4]
            self.count = 0

# def circular_buffer():
#     class CBFIFO(Structure):
#         _fields_ = [("spi_pins_status", SPI_PINS * MAX),
#                     ("head", c_int),
#                     ("tail", c_int),
#                     ("count", c_int)]
#     return CBFIFO
# reference: https://www.digitalocean.com/community/tutorials/calling-c-functions-from-python
sleep(1)

cs_list = []
clk_list = []
mosi_list = []
miso_list = []
time_stamps_list = []

# function to create threads

# cbfifo_gen_c = c_uint64 * 8
# cbfifo_gen = CBFIFO(SPI_PINS(0, 0, 0, 0, 0), 0, 0, 0)

spi_pins_list = [[-1 for i in range(5)] for j in range(MAX)]
cbfifo_gen_c = CBFIFO(spi_pins_list)

c_lib.cbfifo_init(cbfifo_gen_c)

def get_spi_from_c():
    global ret_spi_transfer

    print("Getting parameters")
    c_lib.get_spi.restype = POINTER(SPI_PINS)
    while True:
        ret_spi_transfer = c_lib.get_spi(cbfifo_gen_c)
        if ret_spi_transfer:
            cs_list.append(ret_spi_transfer.contents.cs)
            clk_list.append(ret_spi_transfer.contents.clk)
            mosi_list.append(ret_spi_transfer.contents.mosi)
            miso_list.append(ret_spi_transfer.contents.miso)
            time_stamps_list.append(ret_spi_transfer.contents.time_ns)
            sleep(0.1)
        else:
            print("No more SPI transfer")
            break

spi_data_c = c_uint64 * 8
spi_data = spi_data_c(0xA5, 0xB0, 3, 4, 5, 6, 7, 8)

if __name__ == "__main__":
    mode = input("Enter required SPI mode (i.e, 0,1,2,3): ")
    print(mode)
    msb_first = input("MSB first? (enter True/False): ")
    print(msb_first)
    bits_per_word = input("Enter required bits per word (i.e., 8,16,32,64): ")
    print(bits_per_word)
    spi_config = SPI_CONFIG(500, 2000000, int(
        mode), msb_first, int(bits_per_word))
    c_lib.config_set(spi_config)
    sleep(1)
    c_lib.init_spi()

    print("Starting SPI generator")

# get number of elements in cbfifo from c_lib
    c_lib.cbfifo_count.restype = c_int
    cbfifo_count = c_lib.cbfifo_count(cbfifo_gen_c)
    print("cbfifo_count: ", cbfifo_count)

    ret_spi_transfer = c_lib.spi_transfer(spi_data, 1)
    print("SPI transfer finished...exiting")

    # get number of elements in cbfifo from c_lib
    c_lib.cbfifo_count.restype = c_int
    cbfifo_count = c_lib.cbfifo_count(cbfifo_gen_c)
    print("cbfifo_count: ", cbfifo_count)

    get_spi_from_c()

    # print("cs_list: ", cs_list)
    # print("clk_list: ", clk_list)
    # print("mosi_list: ", mosi_list)
    # print("miso_list: ", miso_list)
    # print("time_stamps_list: ", time_stamps_list)

    # modified timestamp starts from 0
    time_stamps_list = [x - time_stamps_list[0] for x in time_stamps_list]

    cs = numpy.array(cs_list)
    clk = numpy.array(clk_list)
    mosi = numpy.array(mosi_list)
    miso = numpy.array(miso_list)
    timestamp = numpy.array(time_stamps_list)

    # print cs, clk, mosi, miso, timestamp
    print("cs: ", cs)
    print("clk: ", clk)
    print("mosi: ", mosi)
    print("miso: ", miso)
    print("timestamp: ", timestamp)

    plt.plot([1, 2, 3])

    plt.show()
    # plt.show(block=True)

    # # plot the data in dfferent colors
    # plt.plot(timestamp, cs, color='r', label='cs')
    # plt.plot(timestamp, clk, color='g', label='clk')
    # plt.plot(timestamp, mosi, color='b', label='mosi')
    # plt.plot(timestamp, miso, color='y', label='miso')
    # plt.legend(loc='upper left')
    # plt.xlabel('Time (ns)')
    # plt.ylabel('SPI pins status')
    # plt.title('SPI pins status vs Time')
    # plt.show()

    print("Exiting")

    print("done")


# -*- coding: utf-8 -*-
import datetime, pytz
from django.conf import settings as django_settings
from django.utils.translation import ugettext_lazy as _
from magi.default_settings import DEFAULT_ENABLED_PAGES
from majilove import models

############################################################
# General settings

SITE_NAME = 'Maji Love'
SITE_URL = 'http://maji.love/'
SITE_IMAGE = 'majilove_collection.png'
SITE_LOGO = 'majilove.png'
EMAIL_IMAGE = 'majilove_collection_full.png'
EMPTY_IMAGE = 'emptyicon.png'
DONATE_IMAGE = 'donate.png'
SITE_NAV_LOGO = 'majilove_title_white.png'
SITE_STATIC_URL = '//localhost:{}/'.format(django_settings.DEBUG_PORT) if django_settings.DEBUG else '//i.maji.love/'
ACCOUNT_MODEL = models.Account
COLOR = '#5acccd'

############################################################
# Game

GAME_NAME = u'Utano☆Princesama Shining Live'
GAME_URL = 'https://www.utapri-shining-live.com/en/'

############################################################
# Enabled pages

ENABLED_PAGES = DEFAULT_ENABLED_PAGES
ENABLED_PAGES['index']['enabled'] = True

############################################################
# Social

TWITTER_HANDLE = "MajiLoveCollect"
HASHTAGS = ['シャニライ', 'ShiningLive']
GITHUB_REPOSITORY = ('MagiCircles', 'MajiLove')

############################################################
# Donations

DONATORS_STATUS_CHOICES = (
    ('THANKS', 'Thanks'),
    ('SUPPORTER', _('Shining Student')),
    ('LOVER', _('Shining Master Course')),
    ('AMBASSADOR', _('Shining Idol')),
    ('PRODUCER', _('Shining Super Star')),
    ('DEVOTEE', _('Shining Saotome')),
)

############################################################
# Prelaunch details

LAUNCH_DATE = True

############################################################
# Technical details

GOOGLE_ANALYTICS = 'UA-118452679-1'
DISQUS_SHORTNAME = 'maji-love'

############################################################
# Generated settings

TOTAL_DONATORS = django_settings.TOTAL_DONATORS
LATEST_NEWS = django_settings.LATEST_NEWS
STAFF_CONFIGURATIONS = django_settings.STAFF_CONFIGURATIONS


############################################################
# -*- coding: utf-8 -*-
#
#       #   #  #   #   #  ####
#      ##  ##  #  ##  #     #
#     # # # #  # # # #     ###
#    #  ##  #  ##  ##        #
#   #   #   #  #   #     ####
#
# Python-based Tool for interaction with the 10micron mounts
# GUI with PyQT5 for python
# Python  v3.6.5
#
# Michael Würtenberger
# (c) 2016, 2017, 2018
#
# Licence APL2.0
#
###########################################################
import logging
import math
import datetime
import PyQt5
from astropy import _erfa

class Transform:
    logger = logging.getLogger(__name__)

    def __init__(self, app):
        self.app = app
        self.ERFA = _erfa
        self.mutexERFA = PyQt5.QtCore.QMutex()
        self.mutexTopocentric = PyQt5.QtCore.QMutex()
        # if nothing is present, use the coordinates of greenwich
        self.siteLat = 51.476852
        self.siteLon = 0
        self.siteHeight = 46
        # date of 01.05.2018
        self.julianDate = 2458240
        # connect data transfer
        self.app.signalMountSiteData.connect(self.setSiteData)
        self.app.signalJulianDate.connect(self.setJulianDate)

    def setSiteData(self, lat, lon, height):
        self.siteLat = self.degStringToDecimal(lat)
        self.siteLon = self.degStringToDecimal(lon)
        self.siteHeight = float(height)

    def setJulianDate(self, jd):
        self.julianDate = jd

    def topocentricToAzAlt(self, ra, dec):
        self.mutexTopocentric.lock()
        ra = (ra * 360 / 24 + 360.0) % 360.0
        dec = math.radians(dec)
        ra = math.radians(ra)
        lat = math.radians(self.siteLat)
        alt = math.asin(math.sin(dec) * math.sin(lat) + math.cos(dec) * math.cos(lat) * math.cos(ra))
        value = (math.sin(dec) - math.sin(alt) * math.sin(lat)) / (math.cos(alt) * math.cos(lat))
        # we have to check for rounding error, which could happen
        if value > 1:
            value = 1
        elif value < -1:
            value = -1
        A = math.acos(value)
        A = math.degrees(A)
        alt = math.degrees(alt)
        if math.sin(ra) >= 0.0:
            az = 360.0 - A
        else:
            az = A
        self.mutexTopocentric.unlock()
        return az, alt

    def degStringToDecimal(self, value, splitter=':'):
        returnValue = 0
        sign = 1
        if '-' in value:
            value = value.replace('-', '')
            sign = -1
        elif '+' in value:
            value = value.replace('+', '')
        else:
            # just for formal understanding
            pass
        try:
            if len(value.split(splitter)) == 3:
                hour, minute, second = value.split(splitter)
                returnValue = (float(hour) + float(minute) / 60 + float(second) / 3600) * sign
            elif len(value.split(splitter)) == 2:
                hour, minute = value.split(splitter)
                returnValue = (float(hour) + float(minute) / 60) * sign
        except Exception as e:
            self.logger.error('Error in conversion of:{0} with splitter:{1}, e:{2}'.format(value, splitter, e))
            returnValue = 0
        finally:
            pass
        return returnValue

    @staticmethod
    def decimalToDegree(value, with_sign=True, with_decimal=False, spl=':'):
        if value >= 0:
            sign = '+'
        else:
            sign = '-'
        value = abs(value)
        hour = int(value)
        minute = int((value - hour) * 60)
        second = int(((value - hour) * 60 - minute) * 60)
        if with_decimal:
            second_dec = '.{0:1d}'.format(int((((value - hour) * 60 - minute) * 60 - second) * 10))
        else:
            second_dec = ''
        if with_sign:
            returnValue = '{0}{1:02d}{5}{2:02d}{5}{3:02d}{4}'.format(sign, hour, minute, second, second_dec, spl)
        else:
            returnValue = '{0:02d}{4}{1:02d}{4}{2:02d}{3}'.format(hour, minute, second, second_dec, spl)
        return returnValue

    @staticmethod
    def decimalToDegreeMountSr(value):
        degree = int(value)
        minute = int((value - degree) * 60)
        second = int(((value - degree) * 60 - minute) * 60)
        second_dec = int((((value - degree) * 60 - minute) * 60 - second) * 100)
        returnValue = ':Sr{0:02d}:{1:02d}:{2:02d}.{3:02d}#'.format(degree, minute, second, second_dec)
        return returnValue

    @staticmethod
    def decimalToDegreeMountSd(value):
        if value >= 0:
            sign = '+'
        else:
            sign = '-'
        value = abs(value)
        degree = int(value)
        minute = int((value - degree) * 60)
        second = int(((value - degree) * 60 - minute) * 60)
        second_dec = int((((value - degree) * 60 - minute) * 60 - second) * 10)
        returnValue = ':Sd{0}{1:02d}*{2:02d}:{3:02d}.{4:01d}#'.format(sign, degree, minute, second, second_dec)
        return returnValue

    def transformERFA(self, ra, dec, transform=1):
        self.mutexERFA.lock()
        ts = datetime.datetime.utcnow()
        dut1_prev = self.ERFA.dat(ts.year, ts.month, ts.day, 0)
        dut1 = 37 + 4023.0 / 125.0 - dut1_prev
        # suc, tai1, tai2 = self.ERFA.eraUtctai(self.julianDate, 0)
        tai1, tai2 = self.ERFA.utctai(self.julianDate, 0)
        # tt1, tt2 = self.ERFA.eraTaitt(tai1, tai2)
        tt1, tt2 = self.ERFA.taitt(tai1, tai2)
        jdtt = tt1 + tt2
        date1 = self.julianDate
        date2 = 0

        if transform == 1:  # J2000 to Topo Az /Alt
            ra = ra % 24
            aob, zob, hob, dob, rob, eo = self.ERFA.atco13(ra * self.ERFA.D2PI / 24,
                                                           dec * self.ERFA.D2PI / 360,
                                                           0.0,
                                                           0.0,
                                                           0.0,
                                                           0.0,
                                                           date1 + date2,
                                                           0.0,
                                                           dut1,
                                                           self.siteLon * self.ERFA.DD2R,
                                                           self.siteLat * self.ERFA.DD2R,
                                                           self.siteHeight,
                                                           0.0,
                                                           0.0,
                                                           0.0,
                                                           0.0,
                                                           0.0,
                                                           0.0)
            val1 = aob * 360 / self.ERFA.D2PI
            val2 = 90.0 - zob * 360 / self.ERFA.D2PI

        elif transform == 2:                                                                                # Topo to J2000
            rc, dc, eo = self.ERFA.atic13(self.ERFA.anp(ra * self.ERFA.D2PI / 24 + self.ERFA.eo06a(jdtt, 0.0)),
                                          dec * self.ERFA.D2PI / 360,
                                          date1 + date2,
                                          0.0)
            val1 = rc * 24.0 / self.ERFA.D2PI
            val2 = dc * self.ERFA.DR2D

        elif transform == 3:                                                                                # J2000 to Topo
            ri, di, eo = self.ERFA.atci13(ra * self.ERFA.D2PI / 24,
                                          dec * self.ERFA.D2PI / 360,
                                          0,
                                          0,
                                          0,
                                          0,
                                          date1 + date2,
                                          0)
            val1 = self.ERFA.anp(ri - eo) * 24 / self.ERFA.D2PI
            val2 = di * 360 / self.ERFA.D2PI
        else:
            val1 = ra
            val2 = dec
        self.mutexERFA.unlock()
        return val1, val2


import time
from can import Message
from can.interfaces.ixxat import IXXATBus, exceptions

PFC1 = "PFC1"  # 1
PFC2 = "PFC2"  # 2
PFC3 = "PFC3"  # 4
PFC4 = "PFC4"  # 8
PFC5 = "PFC5"  # 16
PFC6 = "PFC6"  # 32
PFC7 = "PFC7"  # 64
PFC8 = "PFC8"  # 128
PFC9 = "PFC9"  # 1
PFC10 = "PFC10"  # 2
PFC11 = "PFC11"  # 4
PFC12 = "PFC12"  # 8
PFC13 = "PFC13"  # 16
PFC14 = "PFC14"  # 32
PFC15 = "PFC15"  # 64
PFC16 = "PFC16"  # 128
STOP = "STOP"  # 0

class pfc_control:
    stop_all = 00
    pfc_list_upper = {"STOP": 0, "PFC1": 1, "PFC2": 2, "PFC3": 4, "PFC4": 8, "PFC5": 16, "PFC6": 32, "PFC7": 64,
                      "PFC8": 128}
    pfc_list_lower = {"STOP": 0, "PFC9": 1, "PFC10": 2, "PFC11": 4, "PFC12": 8, "PFC13": 16, "PFC14": 32, "PFC15": 64,
                      "PFC16": 128}
    CARD1 = 0x60A
    CARD2 = 0x60B

    def __init__(self, channel, card_id):
        self.can_channel = channel
        self.card_id = card_id

    @staticmethod
    def pfc_set(cont, state):
        cont1 = 0
        cont2 = 0
        try:
            # bus = can.interface.Bus(interface="seeedstudio", channel=0,)
            bus = IXXATBus(channel=0, can_filters=[{"can_id": 0x00, "can_mask": 0x000}], bitrate=250000)

        except exceptions.VCIDeviceNotFoundError:
            bus = None

        try:
            # for x in cont:
            #     if x in pfc_control.pfc_list_upper:
            #         cont1 += pfc_control.pfc_list_upper[x]
            #
            # for x in cont:
            #     if x in pfc_control.pfc_list_lower:
            #         cont2 += pfc_control.pfc_list_lower[x]

            op_pfc_read = Message(arbitration_id=card_id, is_extended_id=False, data=[64, 00, 32, 00, 00, 00, 00, 00])
            bus.send(op_pfc_read)
            # time.sleep(0.2)

            msg = bus.recv()
            print(msg)

            if msg.arbitration_id == 1418:
                var = msg.data.hex()

            print(var)
            for i in range(0, len(var) + 1, 2):
                data_packet.append(var[i:i + 2])
            print(data_packet)
            print(data_packet[5], data_packet[4])
            print(data_packet[5] + data_packet[4])
            d = int(data_packet[5] + data_packet[4], 16)
            bit_calculation = pow(2, cont - 1)

            if bit_calculation & d == 0:
                if state == 1:
                    d += bit_calculation
            else:
                if state == 0:
                    d -= bit_calculation

            cont2 = int(d / 256)
            cont1 = int(d % 256)

            op_enable_msg = Message(arbitration_id=card_id, is_extended_id=False,
                                    data=[0x2B, 0x00, 0x20, 0x00, cont1, cont2, 0, 0])

            bus.send(op_enable_msg)

            time.sleep(0.5)

            print(op_enable_msg)

            bus.shutdown()

        except AttributeError:
            print(cont)

    @staticmethod
    def pfc_stop():
        try:
            bus = IXXATBus(channel=0, can_filters=[{"can_id": 0x00, "can_mask": 0x000}], bitrate=250000)

        except exceptions.VCIDeviceNotFoundError:
            bus = None

        try:
            op_enable_msg = Message(arbitration_id=1546, is_extended_id=False,
                                    data=[0x2B, 0x00, 0x20, 0x00, 0, 0, 0, 0])

            bus.send(op_enable_msg)

            time.sleep(0.5)

            print(op_enable_msg)

            bus.shutdown()

        except AttributeError:
            print("Attribute Error")

        print("Stopped!!!!")

    @staticmethod
    def read_pfc():
        global var, data_packet
        data_packet = ["0", "0", "0", "0", "0", "0", "0", "0"]
        var = ''
        try:
            bus = IXXATBus(channel=0, can_filters=[{"can_id": 0x00, "can_mask": 0x000}], bitrate=250000)

        except exceptions.VCIDeviceNotFoundError:
            bus = None

        try:
            packet = [64, 1, 64, 0, 0, 0, 0, 0]
            op_enable_msg = Message(arbitration_id=1546, is_extended_id=False,
                                    data=packet)

            bus.send(op_enable_msg)

            msg = bus.recv()
            print(msg)
            if msg.arbitration_id == 1418:
                data_packet.clear()
                var = msg.data.hex()
                # print(msg)

            for i in range(0, len(var) + 1, 2):
                data_packet.append(var[i:i + 2])
            # print(data_packet)
            d = int(data_packet[5] + data_packet[4], 16)
            # bit_location = pow(2, pfc_number-1)
            cont1 = int(d % 256)
            cont2 = int(d / 256)

            # print(cont1)

            bus.shutdown()

            if cont1 == 1:
                return 1
            elif cont1 == 2:
                return 2
            else:
                return 0

        except exceptions as err:
            print(err)

print(pfc_control.read_pfc())
# print(pfc_control.pfc_set([PFC2,PFC15]))


# -*- coding: utf8 -*-

import random
import redis

REDIS = redis.StrictRedis()

class User:

    def __init__(self, jnum, username, hpw):
        self.jnum = jnum
        self.username = username
        self.hpw = hpw

    def save(self):
        key = 'Us:'+self.username
        REDIS.set(key, self.jnum)
        REDIS.set(key+':hpw', self.hpw)
        REDIS.set('J'+str(self.jnum)+':username',self.username)

    @staticmethod
    def getUser(username):
        key = 'Us:'+username
        if REDIS.exists(key):
            jnum = int(REDIS.get(key))
            hpw = REDIS.get(key+':hpw')
            return User(jnum, username, hpw)
        return 0

    @staticmethod
    def getUsersAndColors():
        ''' Renvoie les utilisateurs associés à leur couleur de tous les joueurs sous forme d'un tableau [username,jnum,color] où chaque couleur est un triplet (r,b,g). Attention les éélments des triplets sont des chaines de caractères, pas des chiffres'''
        import joueurs
        nb = joueurs.JoueurPossible.getNbJoueurs()
        pipe = REDIS.pipeline()
        for i in xrange(nb):
            key = 'J'+str(i+1)
            pipe.get(key+':username')
            pipe.get(key+':red')
            pipe.get(key+':blue')
            pipe.get(key+':green')
        res = pipe.execute()

        users = []
        for i in xrange(nb):
            username = res[i*4]
            color = []
            color.append(res[i*4+1])
            color.append(res[i*4+2])
            color.append(res[i*4+3])
            users.append([username,i+1,color])
        return users

    def setColor(self,red,blue,green):
        from arbre_action import Joueur
        Joueur(self.jnum).setColor(red,blue,green)

    def getColor(self):
        from arbre_action import Joueur
        j = Joueur(self.jnum)
        return [j.getRed(),j.getBlue(),j.getGreen()]


from calendar import day_abbr
import os
import pandas as pd
from transformers import pipeline
from src.config import output_path, output_filetype, labels

class Classifier:
    def __init__(self, data_filetype="json", labels=labels, output_filetype=output_filetype):
        self.data_filetype = data_filetype

        self.data_points = None
        self.labels = labels
        self.output_filetype = output_filetype
        self.hypothesis_template = "This text is about {}."

        self.predictions = []
        self.predictions_as_df = None

        self._init_model()
        self._read_data()

    def _init_model(self):
        self.model = pipeline('zero-shot-classification', model='facebook/bart-large-mnli')

    def _read_data(self):
        data_file = output_path + 'out.' + self.data_filetype

        if self.data_filetype == "json":
            data_points = pd.read_json(data_file)
        elif self.data_filetype == "csv":
            data_points = pd.read_csv(data_file)
        elif self.data_filetype == "excel":
            data_points = pd.read_excel(data_file)
        else:
            raise ValueError("Data file type unsupported")

        self.data_points = data_points.iloc[: , :3]

    def _tokenizer(self, text):
        return self.tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=500)

    def _classify_data_point(self, data_point):
        prediction = self.model(data_point, self.labels, hypothesis_template=self.hypothesis_template, multi_label=True)
        return prediction['labels'][0]

    def _save_predictions(self):
        os.makedirs(output_path, exist_ok=True)
        file_path = output_path + "predictions." + self.output_filetype

        try:
            if self.output_filetype == "csv":
                self.predictions_as_df.to_csv(file_path)
            elif self.output_filetype == "json":
                self.predictions_as_df.to_json(file_path)
            elif self.output_filetype == "excel":
                self.predictions_as_df.to_excel(file_path)
            else:
                print(f"Save unsuccesful: {self.output_filetype} is unsupported")
        except:
            print("Save unsuccesful: something went wrong. View predictions at classifier_instance.predictions")

    def classify(self):
        print(f"Classifying {len(self.data_points)} data points")

        for index, data_point in self.data_points.iterrows():
            id = data_point[0]
            description = data_point[1]
            amount = data_point[2]
            label = self._classify_data_point(data_point=description)

            self.predictions.append((id, description, amount, label))

        print(f"Saving predictions")
        self.predictions_as_df = pd.DataFrame(self.predictions)
        self.save_predictions()


import aiohttp
import async_timeout

__all__ = ('http',)

async def http(url: str, json: dict=None, headers: dict=None, timeout=15, method='POST'):
    """Async HTTP client."""
    if headers is None:
        headers = {}

    with async_timeout.timeout(timeout):
        async with aiohttp.ClientSession(headers=headers) as session:
            async with session.request(method, url, json=json) as response:
                data = await response.json()
                return response, data


'''
    Llenmaos el diccionario con los datos de persona, desde el teclado.
    En esta ocasión lo hacemos de forma recursiva, en plan didáctico. No se pedia.
'''

from typing import Dict
import pprint
#----------------------------------------------
def func_llena_diccionario(hemos_terminado, dic :Dict):
    #caso recursivo
    if not hemos_terminado:
        entrada_usuario_k = input("Para finalizar pulse ENTER. \nSi desea continuar, por favor, introduzca clave a guardar: ")
        if entrada_usuario_k == '':
            hemos_terminado = True
        else:
            entrada_usuario_v = input("Y ahora, el valor para esa clave: ")
            dic[entrada_usuario_k] = entrada_usuario_v

        func_llena_diccionario(hemos_terminado, dic)

    #caso base
    else:
        print('El usuario ha cancelado la entrada de datos. Los datos actualizados del usuario son:')
#-----------------------------------------------
datos_usuario = {}
hemos_terminado_las_entradas = False
func_llena_diccionario(hemos_terminado_las_entradas, datos_usuario)

pprint.pprint(datos_usuario)
for k,v in datos_usuario.items():
    pprint.pprint(f'{k}: {v}')


import xlrd
import os
exc_path=os.path.abspath('pythonLearn/other/testcases.xlsx')
print(exc_path,type(exc_path))
# 打开excle读取数据
data=xlrd.open_workbook(exc_path)
# sheet
sheet=data.sheet_by_index(0)
print(sheet.name) # 第一个sheet的name
sheet=data.sheets()[1]
print(sheet.name,type(sheet))
sheets=data.sheet_names()
print(sheets,type(sheets)) # ['login', 'lines'] <class 'list'> 返回一个list
for s in sheets:
    print(data.sheet_by_name(s))  # <xlrd.sheet.Sheet object at 0x000000000297CA58>
# row,colocum
rows=data.sheet_by_name('login').nrows
cols=data.sheet_by_index(0).ncols
print(rows,cols)  # 返回总的行数和列数
r=data.sheet_by_name('login').row_values(1)
print(r,type(r))# 第2行的值,返回list
c=data.sheet_by_index(0).col_values(2)
print(c,type(c))# 第3列的值,返回list
cell_value=data.sheet_by_index(0).cell(0,0).value
print(cell_value) # 第一行第一列的值

# 读取excel，返回格式为 {{sheet1:[{key:value1},{key:value2}]},{sheet2:[{key1:value1},{key2:value2}]}}
class oExcel(object):
    def __init__(self):
        # exc_path 可以是固定路径的excel
        self.path=exc_path
        self.data=xlrd.open_workbook(self.path)
    def rdExcel(self):
        # 获取所有sheet的名称
        sheets=self.data.sheet_names()
        sheetDict={}
        # 遍历sheet
        for sheet in sheets:
            sheetList=[]
            # 根据sheet名称确定table
            table=self.data.sheet_by_name(sheet)
            # 总行数
            nrows=table.nrows
            # 每个sheet的第一行，即表头行作为dict的key，key是list
            if nrows>1:
                key=table.row_values(0)
                # 从第一行开始
                for i in range(1,nrows):
                    row_value=table.row_values(i) # 获取每一行的值,list形式
                    # dict(zip)将key和每一行zip，得到一个dict,依次添加进sheetlist中
                    sheetList.append(dict(zip(key,row_value)))
                    i+=1
                #  {sheetname:[每个sheet内容(包含key和每一行值组成的字典)]}
                sheetDict[sheet]=sheetList
            else:
                sheetDict[sheet]=[]
        return sheetDict
    def getData(self,sheetname):
        # 通过sheet名称获取sheetlist（pytest框架读取的是list)
        return self.rdExcel()[sheetname]


from dal import autocomplete
from django import forms

from velafrica.bikes.models import Bike, BikeCategory
from velafrica.stock.models import Warehouse
from velafrica.velafrica_sud.models import Container

class BikeForm(forms.ModelForm):
    warehouse = forms.ModelChoiceField(
        queryset=Warehouse.objects.all(),
        widget=autocomplete.ModelSelect2(url='autocomplete:warehouse'),
        required=False,
        label='Warehouse'
    )
    container = forms.ModelChoiceField(
        queryset=Container.objects.all(),
        widget=autocomplete.ModelSelect2(url='autocomplete:container'),
        required=False,
        label='Container'
    )
    category = forms.ModelChoiceField(
        queryset=BikeCategory.objects.all(),
        widget=autocomplete.ModelSelect2(url='autocomplete:bike_category'),
        label='Category'
    )

    class Meta:
        model = Bike
        fields = ('__all__')