from PySiteCapture.modules import wp_login

import asyncio
import argparse
import json
from pyppeteer import launch
from time import strftime

project_descr = """
PySiteCapture is used to take multiple screenshots of a website and its elements,
and then compare them against one another.
"""

"""Argument management"""
# Create a class instance of ArgumentParser
parser = argparse.ArgumentParser(description=project_descr)

# Generate arguments for the argparser
parser.add_argument('-i', type=str, default='test_list.json', help='The input json.')
parser.add_argument('-w', type=int, default=4, help='The number of workers to spawn and use')
parser.add_argument('--width', type=int, default=1920, help='Used to override the width of the viewport.')
parser.add_argument('--height', type=int, default=1080, help='Used to override the height of the viewport.')
parser.add_argument('--max', type=int, default=5, help='Maximum percentage of allowed image difference')
parser.add_argument('-u', type=str, help='User account for logging into sites and retrieving an auth token')
parser.add_argument('-pw', type=str, help='User password for logging into sites and retrieving an auth token')
parser.add_argument('--nologin', type=bool, default=False, help='Used when sites do not require a login for page SS')

# Parse the arguments passed in the command line and assign them to an iterable object named "args".
args = parser.parse_args()

# PySiteCapture vars
viewport = {'width': args.width, 'height': args.height}
user = {'login': args.u, 'pass': args.pw}
max_workers = args.w
suffix = '_orig'
job_list = json.load(open(args.i))
workers = 0
tasks = []

# Worker class. These are spawned and each navigate pages to complete the jobs in job_list
class Worker:
    def __init__(self, w_id, w_container):
        self.id = w_id
        self.page = w_container
        self.name = 'Worker {}: '.format(str(self.id))
        self.job = None
        tasks.append(asyncio.ensure_future(self.get_job()))

    def log(self, message):
        print('[{}]'.format(strftime("%H:%M:%S")) + ' ' + self.name + message)

    async def get_job(self):
        self.job = job_list.pop(0)
        await self.start_job()

    async def start_job(self):
        self.log('Starting job "{}" on {}'.format(self.job['name'], self.job['url']))
        await self.page.goto(self.job['url'])
        element = await self.page.querySelector(self.job['selector'])
        element_bounding = await element.boundingBox()

        # Buffer all lazily loaded content
        viewport_height = self.page.viewport['height']
        scroll = 0
        while scroll + viewport_height < element_bounding['height']:
            await self.page.evaluate('_viewportHeight= > {window.scrollBy(0, _viewportHeight);}', viewport_height)
            await self.page.waitFor(20)
            scroll = scroll + viewport_height
        await self.page.waitFor(100)

        # Take screenshot
        await self.page.screenshot(path='screenshots/{}{}.png'.format(self.job['name'], suffix), clip=element_bounding)
        self.log('Finished job "{}" on {}'.format(self.job['name'], self.job['url']))

        # Check if no more jobs are to be done, else get a new job.
        if len(job_list) == 0:
            print(self.name + 'Shutting down')
            global workers
            workers -= 1
            if workers == 0:
                print('INFO: All jobs complete.')
                loop.stop()
        else:
            await self.get_job()

# Starts Chromium browser, spawns workers, gets auth tokens. General setup
async def setup():
    # user_auth = await wp_login.get_auth_token(job_list[0]['url'], user)
    browser = await launch({
        'args': ['--no-sandbox']
    })
    print('[{}]'.format(strftime("%H:%M:%S")) + ' INFO: Spawning {} workers'.format(max_workers))
    for _ in range(0, max_workers):
        page = await browser.newPage()
        await page.setJavaScriptEnabled(False)
        await page.setViewport(viewport)
        global workers
        workers += 1
        worker = Worker(workers, page)

loop = asyncio.get_event_loop()
loop.run_until_complete(setup())

# Run all screenshot jobs
loop.run_forever()

# Do image comparison using openCV


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from ..exceptions import UnmarshalError
from ..parameters.unmarshalers import unmarshal_parameters
from ..request_body.unmarshalers import unmarshal_request_body

def unmarshal_request(schema_unmarshaler, request, operation):
    parameters, parameter_errors = unmarshal_parameters(
        schema_unmarshaler, request, operation['parameters']
    )
    if parameter_errors:
        for error in parameter_errors:
            error.schema_path.appendleft('parameters')

    if 'requestBody' in operation:
        request_body, request_body_errors = unmarshal_request_body(
            schema_unmarshaler, request, operation['requestBody']
        )
        if request_body_errors:
            for error in request_body_errors:
                error.schema_path.appendleft('requestBody')
    else:
        request_body = None
        request_body_errors = None

    if parameter_errors or request_body_errors:
        raise UnmarshalError(parameter_errors, request_body_errors)

    return parameters, request_body


import configparser
import os
import glob
from azure.storage.blob import ContainerClient
import logging

config = configparser.ConfigParser()
configfile = r'\PycharmProjects\Python\lib\Azure_config.ini'
properties = config.read(configfile)
log_file=r"\PycharmProjects\Python\lib\app.log"

logging.basicConfig(filename=log_file, filemode='w',format='%(asctime)s - %(message)s', level=logging.INFO)
logging.info('Admin logged in')

def load_azure_config():
    credentials = config['AZURE']['credentials']
    container = config['AZURE']['container']
    inbound_folder = config['AZURE']['inbound_folder']
    return credentials,container,inbound_folder

def get_input_files(inbound_folder):
     with os.scandir(inbound_folder) as files:
           for file in files:
               if file.is_file() and not file.name.startswith('.'):
                   yield file

def file_upload(files,connections_string,containter_name):
    container_client = ContainerClient.from_connection_string(connections_string,containter_name)
    print(type(container_client))
    for file in files:
        print(file)
        logging.info('copying the files to Azure cloud inprogress')
        blob_client=container_client.get_blob_client(file.name)
        print(type(blob_client))
        logging.info('files names' + file.name)
        with open(file.path,"rb") as data:
              print(data)
              #blob_client.upload_blob(data)
    return 0

def main():
    credentials, container, inbound_folder = load_azure_config()
    logging.info('credentials' + credentials)
    logging.info('container' + container)
    logging.info('inbound_folder' + inbound_folder)
    list_of_files = get_input_files(inbound_folder)
    file_upload(list_of_files, credentials, container)
    logging.info('copying the files to Azure cloud Completed')
    return True

if __name__ == "__main__":
    print(main())


import random

class Cells(object):

    def __init__(self, box, number, capacity, intRes):
        """Constructeur de notre classe"""
        self.box = box
        self.number = number
        self.capacity = capacity
        self.intRes = intRes

moduleQty = int(input("Nombre de module?"))
cellQty = int(input("Nombre de Cellules par module?"))
packCellQty = moduleQty*cellQty

#generate random value
# need to be replaced by file parsing
cells_list = []
boxQty = 20
boxCellQty = 30
totCellQty = boxQty*boxCellQty
for boxIndex in range (1,boxQty+1):
    for numberIndex in range (1,boxCellQty+1):
        rCap=random.randint(3200,3350)
        rIntRes=random.randint(20,80)
        cells_list.append(Cells(boxIndex,numberIndex,rCap,rIntRes))

#class cells capacity by ascending order
ascended_cells_list = sorted(cells_list, key=lambda c: c.capacity)

#remove cells with the worst capacity
best_cells = []
for i in range (0,packCellQty):
    best_cells.append(ascended_cells_list[i+(totCellQty-packCellQty)])

#split the list in 11 list of moduleQty
capacity_group = []
for i in range(0, len(best_cells), moduleQty):
        capacity_group.append(best_cells[i:i+moduleQty])

#class internal resistance by ascending order for each group
for i in range (0,len(capacity_group)):
    capacity_group[i] = sorted(capacity_group[i], key=lambda c: c.intRes)

#Make module by matching the internal resistance of each capacity group
Module_list = []
module = []
for i in range (0,moduleQty):
    for j in range (0,cellQty):
        module.append(capacity_group[j][i])
    Module_list.append(module)
    module = [] #clear module

# print the result
summ = 0
print("Cell#--capacity--internal resistence")
for i in range (0,len(Module_list)):
    print("---------------------Module #",i," :")
    for cells in Module_list[i]:
        summ = summ+cells.capacity
        print ("#",cells.box,"-",cells.number," ",cells.capacity," ",cells.intRes)
    print("capacité total:",summ)
    summ = 0


import os
import sys
import logging
import urllib.request
import urllib.parse
from pic_info import PicInfo
import logging
import time
import sqlite3
from update_sql import update_sql
from config import conf

IF_INIT  =  conf["IF_INIT"]
IF_CRAW  = conf["IF_CRAW"]
IF_DOWNLOAD  =conf["IF_DOWNLOAD"]
IF_CROP = conf["IF_CROP"]
IF_UPLOAD  =conf["IF_UPLOAD"]

URL_HEADERS = {
        'Content-Type': 'application/json;charset=utf-8'
}
import socket
socket.setdefaulttimeout(5)

max_queue_len = 100
def download_img(INIT_QUE,DOWNLOAD_QUE,lock,process_number_dict):
    conn = sqlite3.connect("picinfo.db")
    #cursor = conn.cursor()

    while True:
        cur_img = INIT_QUE.get()
        logging.info("INIT_QUE get size:%d" % INIT_QUE.qsize())
        if not cur_img:
            INIT_QUE.put(None)
            if process_number_dict["download"] > 1:
                process_number_dict["download"] -= 1
            else:
                DOWNLOAD_QUE.put(None)
            logging.info("DOWNLOAD_QUE put size:%d None" % INIT_QUE.qsize())
            break
        if cur_img.status != PicInfo.INIT:
            logging.warn("img {} NOT INIT".format(cur_img.id))
            INIT_QUE.task_done()
            continue
        try:
            #cover_request = urllib.request.Request(cur_img.url, method="GET", headers=URL_HEADERS)
            #response = urllib.request.urlopen(cover_request)
            #resp_header = response.info()
            #content_type = resp_header["Content-Type"]
            #image_type = content_type.split('/')
            #if image_type[0] == "image" and len(image_type) == 2:
                download_path = os.path.join("imgs",cur_img.country,cur_img.query,"%s.jpg" % cur_img.id)
                start_time =int(time.time()  * 1000)
                #type = image_type[1]
                logging.info("downloading...%s" % cur_img.url)
                urllib.request.urlretrieve(cur_img.url, download_path)
                end_time =int(time.time()  * 1000)
                fsize = os.path.getsize(download_path)
                logging.info("downloading %d finish " % (end_time - start_time)  + cur_img.url+" -> "+download_path)
                if fsize < 100:
                    logging.warn("the file {} size is less than 100".format(cur_img.id))
                    logging.error("ERROR",cur_img.status)
                    cur_img.status = PicInfo.ERROR
                else:
                    cur_img.status = PicInfo.DOWNLOAD
                    if DOWNLOAD_QUE.qsize() > max_queue_len:
                        DOWNLOAD_QUE.join()
                    if IF_CROP:
                        DOWNLOAD_QUE.put(cur_img)
                    logging.info("DOWNLOAD_QUE put size:%d" % DOWNLOAD_QUE.qsize())
        except Exception as e:
            logging.error(e)
            cur_img.status = PicInfo.ERROR
        print(cur_img)
        update_sql(cur_img,conn,lock["sql"])
        INIT_QUE.task_done()

    #cursor.close()
    conn.close()


import os
import tempfile

from toil.common import Toil
from toil.job import Job

def helloWorld(message):
    return f"Hello, world!, here's a message: {message}"

if __name__ == "__main__":
    jobstore: str = tempfile.mkdtemp("tutorial_quickstart")
    os.rmdir(jobstore)
    options = Job.Runner.getDefaultOptions(jobstore)
    options.logLevel = "OFF"
    options.clean = "always"

    hello_job = Job.wrapFn(helloWorld, "Woot")

    with Toil(options) as toil:
        print(toil.start(hello_job))  # prints "Hello, world!, ..."


# 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 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
        )


#Marlon Aldair Arciniega sanchez

txt = str(open('texto_04.txt', 'r'))

linea=0
for linea in txt:

    if ((linea%2)==0): #las lineas pares(las que no deseamos)
        continue
    if ((linea % 2)!=0): #lineas impares
        print(linea)

txt.close()


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 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)


from Fibot.NLP.nlg import Query_answer_unit

def main():
    qa = Query_answer_unit()
    qa.load()
    message = ""
    while message != "bye":
        message = input("Send me a message:\n")
        print("\n\nbot says: {}".format(qa.get_response(message)))

if __name__ == "__main__":
    main()


#!/usr/bin/python

import os, exceptions
import subprocess
import fcntl
import rpc

def makeItNonBlocking(f):
    fd = f.fileno()
    fl = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

class RLog:
    def __init__(self):
        self.pipes = []

    def shell(self, cmd):
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        makeItNonBlocking(p.stdout)
        makeItNonBlocking(p.stderr)
        self.pipes += [p.stdout, p.stderr]
        return p

    @rpc.methodWrapper
    def get(self):
        def safeRead(f):
            try:
                content = f.read()
            except exceptions.IOError:
                content = ""
            if content == None: content = ""
            return content
        self.pipes = [p for p in self.pipes if not p.closed]
        return ''.join([safeRead(f) for f in self.pipes])


# @Time    : 2019/3/11 9:32
# @Author  : Xu Huipeng
# @Blog    : https://brycexxx.github.io/

def cal_8queens(results: list, row: int):
    """
    results 下标表示所在行，存储的值表示列
    """
    if row == len(results):
        print_8queens(results)
        print('-----------------------')
        return
    for i in range(len(results)):
        if is_ok(results, row - 1, i):
            results[row] = i
            cal_8queens(results, row + 1)

def print_8queens(results):
    for k in range(len(results)):
        s = ''
        for m in range(len(results)):
            if results[k] == m:
                s += ' Q '
            else:
                s += ' * '
        print(s)

def is_ok(results, row, col):
    left_up = col - 1
    right_up = col + 1
    for i in range(row, -1, -1):
        if results[i] == col or results[i] == left_up or \
                results[i] == right_up:
            return False
        left_up -= 1
        right_up += 1
    return True

if __name__ == "__main__":
    results = [None] * 8
    cal_8queens(results, 0)


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 jax.numpy as np
from jax import random
from jax.example_libraries import optimizers
from jax import jit, grad, vmap
import functools
import neural_tangents as nt
from neural_tangents import stax
import matplotlib.pyplot as plt
import torch

import os
import sys
import inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir)

import datasets.gp_reg_dataset.gp_regression as d

def make_nngp(num_layers, h_dim, w_std, b_std):
    if num_layers == 1:
        init_fn, apply_fn, kernel_fn = stax.serial(
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(1, W_std=w_std, b_std=b_std)
        )
    elif num_layers == 2:
        init_fn, apply_fn, kernel_fn = stax.serial(
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(1, W_std=w_std, b_std=b_std)
        )
    elif num_layers == 3:
        init_fn, apply_fn, kernel_fn = stax.serial(
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(1, W_std=w_std, b_std=b_std)
        )
    elif num_layers == 4:
        init_fn, apply_fn, kernel_fn = stax.serial(
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(1, W_std=w_std, b_std=b_std)
        )
    elif num_layers == 5:
        init_fn, apply_fn, kernel_fn = stax.serial(
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(h_dim, W_std=w_std, b_std=b_std), stax.Relu(),
            stax.Dense(1, W_std=w_std, b_std=b_std)
        )
    return init_fn, apply_fn, kernel_fn

def predict(model, x, K=50, key=random.PRNGKey(1)):
    init_fn, apply_fn = model

    if K == 1:
        key, net_key = random.split(key)
        _, params = init_fn(net_key, (0, 1))
        return apply_fn(params, test.x), torch.tensor([0])

    return y_pred.mean(0), y_pred.std(0)

def full_training(exp_name=None, n_epochs=10000,
                  num_layers=2, h_dim=50,
                  w_std=1.0, b_std=1.0, lik_std=0.05):
    torch.manual_seed(1)
    key = random.PRNGKey(1)

    train, test, noise_std = d.import_train_test()
    train_x = train[:,0].reshape(-1,1); train_y = train[:,1].reshape(-1,1)
    test_x = test[:,0].reshape(-1,1); test_y = test[:,1].reshape(-1,1)

    # nngp init
    init_fn, apply_fn, kernel_fn = make_nngp(num_layers, h_dim, w_std, b_std)
    # _, params = init_fn(key, train_x.shape)

    # kwargs = dict(
    #     f=apply_fn,
    #     trace_axes=(),
    #     vmap_axes=0
    # )

    # apply_fn = jit(apply_fn)
    # kernel_fn = jit(kernel_fn, static_argnames='get')

    k_train_train = kernel_fn(train_x, train_x, 'nngp')
    k_test_train = kernel_fn(test_x, train_x, 'nngp')
    k_test_test = kernel_fn(test_x, test_x, 'nngp')
    std_dev = np.sqrt(np.diag(k_test_test))

    # predict_fn = nt.predict.gradient_descent_mse_ensemble(kernel_fn, X, Y, diag_reg=1e-4)
    predict_fn = nt.predict.gp_inference(k_train_train, train.y, diag_reg=1e-4)

    nngp_mean, nngp_covariance = predict_fn('nngp', k_test_train, k_test_test)
    # nngp_mean, nngp_covariance = predict_fn(x_test=test.x, get='nngp', compute_cov=True)

    nngp_mean = np.reshape(nngp_mean, (-1,))
    nngp_std = np.sqrt(np.diag(nngp_covariance))

    plot_posterior(train, test, nngp_mean, nngp_std)

if __name__ == '__main__':
    full_training()


#!/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 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()


############################################################
# -*- 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 pandas as pd
import xlrd as excel
import mysql.connector as sql
import math

NULL = 'NULL'

# baca excel, return n x 3 array
# excel: kolom 0 = ID, kolom 1 = nama, kolom 7 = bdn
def read_tajnid(path=r"data.xlsx"):

    input_workbook = excel.open_workbook(path)
    input_worksheet = input_workbook.sheet_by_index(0)

    data = []
    badan_dict = {'K': 'Khm', 'L': 'Ljh',
                  'A': 'Ahr', 'T': 'Ahf',
                  'N': 'Nhr', 'G': 'Bnh',
                  'B': 'Aba'}

    for i in range(input_worksheet.nrows - 1):
        if input_worksheet.cell_value(i, 0) == '':
            continue
        ID = str(round(input_worksheet.cell_value(i, 0), None))
        nama = input_worksheet.cell_value(i, 1)
        badan = input_worksheet.cell_value(i, 7)
        badan = badan_dict[badan]
        data2 = (ID, nama, badan)
        data.append(data2)

    return data

# input data ke database, argument: nx3 array
def insert_tajnid(data):

    mydb = sql.connect(
        host="localhost",
        user="root"
    )

    mycursor = mydb.cursor()
    mycursor.execute("use yogyakarta")

    sql_formula = "INSERT INTO data VALUES (%s, %s, %s, 1, NULL, NULL)"

    for item in data:
        try:
            mycursor.execute(sql_formula, item)
        except Exception as why:
            print(why)
            continue

    mydb.commit()

# input data ke database 2
def insert_ch(data):

    mydb = sql.connect(
        host="localhost",
        user="root"
    )

    mycursor = mydb.cursor()
    mycursor.execute("use yogyakarta")

    sql_formula = "INSERT INTO data VALUES (%s, %s, %s, %s, %s, %s, %s)"

    for item in data:
        try:
            mycursor.execute(sql_formula, item)
        except Exception as why:
            print(why, "Error di", item)
            return

    mydb.commit()

# baca excel, return n x 7 array
# 1 kuitansi, 2 ID, 9 tgl transaksi, 15 mjd, 80 jh, 81 Std, 82 RSID
# in excel format tgl transaksi 0000 00 00
def read_ch(path=r"data.xlsx"):

    input_workbook = excel.open_workbook(path)
    input_worksheet = input_workbook.sheet_by_index(0)

    data = []

    for i in range(input_worksheet.nrows):
        if i == 0:
            continue
        kuitansi = str(round(input_worksheet.cell_value(i, 1), None))
        ID = str(round(input_worksheet.cell_value(i, 2), None))
        tgl = input_worksheet.cell_value(i, 9)

        tgl = tgl.replace(' ', '-')

        masjid = input_worksheet.cell_value(i, 15)
        jamiah = input_worksheet.cell_value(i, 80)
        mta = input_worksheet.cell_value(i, 81)
        rs = input_worksheet.cell_value(i, 82)
        data2 = (kuitansi, ID, tgl, masjid, jamiah, mta, rs)
        data.append(data2)

    return data

def read_column(path):

    book = excel.open_workbook(path)
    sheet = book.sheet_by_index(0)

    for i in range(sheet.ncols):
        print(i, sheet.cell_value(0, i))

def read_mysql(table="tjd"):

    mydb = sql.connect(
        host="localhost",
        user="root"
    )

    mycursor = mydb.cursor()
    mycursor.execute("use yogyakarta")

    sql_formula = "SELECT * FROM " + table

    mycursor.execute(sql_formula)

    return mycursor.fetchall()

# membaca janji excel
# excel: 1 ID, 7 periode, 8 perjanjian, 9 nominal
def read_perjanjian(path=r"data.xlsx"):

    book = excel.open_workbook(path)
    sheet = book.sheet_by_index(0)

    nary = {'KomplekMjd': 2, 'Jmh': 3, 'Studio': 4, 'RSID': 5}

    cek = []
    data = []

    for i in range(sheet.nrows):
        if i == 0:
            continue
        if round(sheet.cell_value(i, 1), None) in cek:
            for item in data:
                if str(round(sheet.cell_value(i, 1), None)) in item:
                    if sheet.cell_value(i, 8) in nary.keys():
                        item[nary[sheet.cell_value(i, 8)]+1] += sheet.cell_value(i, 9)
        else:
            cek.append(round(sheet.cell_value(i, 1), None))
            ID = str(round(sheet.cell_value(i, 1), None))
            period = sheet.cell_value(i, 7)
            period = period[:4]
            no = ID+'-'+period[2:4]
            datum = [no, ID, period, 0, 0, 0, 0]
            if sheet.cell_value(i, 8) in nary.keys():
                datum[nary[sheet.cell_value(i, 8)]+1] += sheet.cell_value(i, 9)
            data.append(datum)

    hasil = []

    for item in data:
        hasil.append(tuple(item))

    print("Cek", len(cek), "sama dengan", len(data), "sehingga", len(cek) == len(data))

    return hasil

def insert_perjanjian(data):

    mydb = sql.connect(
        host="localhost",
        user="root"
    )

    mycursor = mydb.cursor()
    mycursor.execute("use yogyakarta")

    sql_formula = "INSERT INTO proyek VALUES (%s, %s, %s, %s, %s, %s, %s)"

    for item in data:
        try:
            mycursor.execute(sql_formula, item)
        except Exception as why:
            print(why, "Error di", item)
            return

    mydb.commit()

    print("Inserting to Database Succeed")

# Path of file excel, str filename -> str path
def path_cd(excel_name):
    return r"D:\project\{}".format(excel_name)

# Path of file excel perjanjian lainnya, str filename -> str path
def path_perj_pl(periode='1920'):
    return r"D:\project\{}.xlsx".format(periode)

# return database, cursor
def connect_to_mysql(database_name="yogyakarta"):
    db = sql.connect(
        host="localhost",
        user="root",
        database=database_name
    )

    my_cursor = db.cursor()
    # mycursor.execute("")

    return db, my_cursor

# Deleting space, /, -, (, ), ., '
def deleting_decoration(the_list):
    new_list = []
    for item in the_list:
        var = item
        var = var.replace('/', '')
        var = var.replace('-', '')
        var = var.replace('(', '')
        var = var.replace(')', '')
        var = var.replace('.', '')
        var = var.replace('\'', '')
        var = ''.join(var.split())
        # print(var)
        new_list.append(var)
    return new_list

# dictionary data type
def data_type_dict():
    tipe_tipe = {
        'M1': 'VARCHAR(30)',
        'ID': 'VARCHAR(10)',
        'Nama': 'VARCHAR(50)',
        'NamaC': 'VARCHAR(50)',
        'Bn': 'VARCHAR(20)',
        'NoTelepon': 'VARCHAR(15)',
        'BulanBayar': 'VARCHAR(15)',
        'Keterangan': 'TEXT',
        'Alh': 'TEXT',
        'TglTransaksi': 'DATE',
        'default': 'INT',

        'id': 'VARCHAR(15)',
        'Periode': 'VARCHAR(10)'
    }
    return tipe_tipe

# Creating table in mysql
def create_table_sql(column_list, table_name, cursor, dict_type):

    inside_query = query_creating_table(column_list, dict_type)

    query = "CREATE TABLE {} ({});".format(table_name, inside_query)
    print(query)
    cursor.execute(query)

# What is my type? return string value
def my_type(name, dict_type):
    if name in dict_type.keys():
        return dict_type[name]
    return dict_type['default']

# sql query formula for creating table, return string
def query_creating_table(list_column, dict_type):
    the_string = ''
    for item in list_column:
        the_string += item + ' '
        the_string += my_type(item, dict_type) + ', '
        if item == list_column[-1]:
            the_string += 'PRIMARY KEY ({})'.format(list_column[0])
            break

    return the_string

# Creating table in database mysql
def creating_table(dataframe, table_name='data'):
    dictionary = data_type_dict()
    # print(dictionary.keys())
    db, mycursor = connect_to_mysql()

    # dataframe = pd.read_excel(r"D:\project\data.xlsx")
    list_of_columns = deleting_decoration(list(dataframe.columns))

    create_table_sql(list_of_columns, table_name, mycursor, dictionary)

# to string all, general list -> string list
def tostringall(the_list):
    final_list = []
    for item in the_list:
        if item == NULL:
            final_list.append(NULL)
            continue
        final_list.append('\''+str(item)+'\'')
    return final_list

# replace nan with 'NULL', list->list
def nan_to_null(the_list):
    final_list = []
    for item in the_list:
        try:
            if math.isnan(item):
                final_list.append(NULL)
            else:
                final_list.append(item)
        except Exception as why:
            final_list.append(item)
    return final_list

# Ganti format tgl transaksi, cth 2021 01 01 -> 2021-01-01, pd_df->pd_df
def ganti_format_tgl(pd_df, kolom='Tgl Transaksi'):
    the_list = list(pd_df[kolom].values)
    for i, item in enumerate(the_list):
        the_list[i] = '-'.join(str(item).split())
    pd_df[kolom] = the_list
    return pd_df

# Ganti tipe data kolom ke str
def change_column_type(pd_df, kolom):
    the_list = list(pd_df[kolom].values)
    for i, item in enumerate(the_list):
        the_list[i] = str(item)
    pd_df[kolom] = the_list
    return pd_df

# Check sameness two lists | list, list -> print(list, list)
def sameness(list_one, list_two):
    final_one = []
    final_two = []
    for item in list_one:
        if item not in list_two:
            final_one.append(item)
    for item in list_two:
        if item not in list_one:
            final_two.append(item)
    print('There are {} problems'.format(len(final_one) + len(final_two)))
    if any(final_one) or any(final_two):
        print('List one:')
        for item in final_one:
            print(item)
        print('\n')
        print('List two:')
        for item in final_two:
            print(item)
    return not (any(final_one) or any(final_two))

# database to Pandas DataFrame
def sql_to_pd(database, table_name='tajnid'):
    return pd.read_sql("select * from {}".format(table_name), database)

# Return unique items in list, list->list
def unique_list(the_list):
    new_list = []
    for item in the_list:
        if item in new_list:
            continue
        new_list.append(item)
    return new_list

# Creating 2D list
def list_2d(rows, columns):
    the_list = []
    for i in range(rows):
        the_list_child = []
        for j in range(columns):
            the_list_child.append(math.nan)
        the_list.append(the_list_child)
    return the_list

# Clear decoration of items in a column, pd df -> pd df
def deleting_decoration_df(dataframe, nama_kolom):
    the_list = list(dataframe[nama_kolom].values)
    the_list = deleting_decoration(the_list)
    dataframe[nama_kolom] = the_list
    return dataframe

# Khusus untuk bagian perjanjian
# Transport values, pd_df pd_df -> pd_df
def transport_perjanjian(df_base, target_df, perj_type='JhIdn'):
    nominals = []
    for nomor_ID in target_df['ID'].values:
        filtered = df_base[[value == nomor_ID for value in df_base['ID']]]
        filtered = filtered[[value == perj_type for value in filtered['PERJANJIAN']]]
        filtered = filtered['NOMINAL PERJANJIAN (Rp)'].values
        if any(filtered):
            nominals.append(filtered[0])
        else:
            nominals.append(0)
    target_df[perj_type] = nominals
    return target_df

# Khusus untuk bagian perjanjian
# Ngisi id, gabungan ID dan PERIODE, pd_df pd_df -> pd_df
def ngisi_id(df_base, df_target):
    # insert id
    ids = []
    for nomor_ID in df_target['ID'].values:
        filtered = df_base[[value == nomor_ID for value in df_base['ID']]]
        the_period = filtered['PERIODE'].values[0]

        ids.append(str(nomor_ID) + str(the_period))

    df_target['id'] = ids
    return df_target

# Insert row in a table mysql, input pd df
def insert_row(dataframe, table_name='data'):
    extract = list(dataframe.values)
    list_of_columns = deleting_decoration(list(dataframe.columns))
    list_of_columns = ', '.join(list_of_columns)

    mydb, mycursor = connect_to_mysql()

    countfull = dataframe.shape[0]
    for i in range(countfull):
        try:
            values = extract[i]
            values = nan_to_null(values)
            values = ', '.join(tostringall(values))
            query_insert = "INSERT INTO {}({}) VALUES ({});".format(table_name, list_of_columns, values)
            mycursor.execute(query_insert)
            print('{} / {}'.format(i + 1, countfull))
        except Exception as why:
            print(why)

    mydb.commit()

# inserting items excel into db
def inserting_row(dataframe, table_name='data'):
    try:
        dataframe = ganti_format_tgl(dataframe)
    except Exception as why:
        print(why)

    insert_row(dataframe, table_name)

# Complete input data excel to database
def input_chid(the_excel='Yogyakarta 20210101-0131.xlsx', nama_table='data'):
    the_database, mycursor = connect_to_mysql()
    dataframe = pd.read_sql('DESCRIBE {};'.format(nama_table), the_database)
    kolom_one = list(dataframe['Field'].values)

    df_2 = pd.read_excel(path_chid(the_excel))
    kolom_two = deleting_decoration(list(df_2.columns))

    if sameness(kolom_one, kolom_two):
        inserting_row(df_2, nama_table)

# Converting excel perjanjian ke pd df ready upload, return pd_df
def converting_perjanjian(the_period='2021'):
    df = pd.read_excel(path_perj_pl(the_period))
    # print(df.head())
    df = deleting_decoration_df(df, 'PERJANJIAN')

    jenis_perjanjian = list(df['PERJANJIAN'].values)
    jenis_perjanjian = unique_list(jenis_perjanjian)

    kolom_kolom = ['id', 'ID', 'Nama', 'Periode'] + jenis_perjanjian

    nama_pejanji = unique_list(list(df['NAMA'].values))
    kumpulan_ID = unique_list(list(df['ID'].values))
    tahun_periode = unique_list(list(df['PERIODE']))

    new_df = pd.DataFrame(list_2d(len(nama_pejanji), len(kolom_kolom)), columns=kolom_kolom)

    new_df['ID'] = kumpulan_ID
    new_df['Nama'] = nama_pejanji
    new_df['Periode'] = [tahun_periode[0] for item in new_df['Periode']]

    # insert id
    new_df = ngisi_id(df, new_df)

    # insert perjanjian-perjanjian
    for jenis in jenis_perjanjian:
        new_df = transport_perjanjian(df, new_df, jenis)

    new_df = change_column_type(new_df, 'ID')

    return new_df

# input data perjanjian pd df to perjanjianlainnya database
def input_perjanjianlainnya(the_period='2021', nama_table='chid'):
    the_database, mycursor = connect_to_mysql()
    dataframe = pd.read_sql('DESCRIBE {};'.format(nama_table), the_database)
    kolom_one = list(dataframe['Field'].values)

    df_2 = converting_perjanjian(the_period)
    kolom_two = deleting_decoration(list(df_2.columns))

    if sameness(kolom_one, kolom_two):
        inserting_row(df_2, nama_table)

if __name__ == '__main__':

    pass


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]


class Game:
    graph = {1: [2, 3], 2: [1, 3], 3: [1, 2]}

    players = ['a', 'b']

    def __init__(self):
        self.player_territories = dict()
        self.pieces = dict()

    # player index
    cur_player = 0

    def get_state(self):
        return {
            'current': self.cur_player,
            'graph': self.graph,
            'players': self.players,
            'player_territories': self.player_territories,
            'pieces': self.pieces
        }

    def _check_connected(self, from_index, to_index):
        if to_index not in self.graph[from_index]:
            raise Exception("Territories are not connected!")

    def move(self, num, node_index_from, node_index_to):
        player_index = self.cur_player
        self._check_valid_node(node_index_from)
        self._check_valid_node(node_index_to)
        self._check_valid_unit_strength(num)
        self._check_connected(node_index_from, node_index_to)
        self._check_player_owns(player_index, node_index_from)

        # must leave at least 1 behind
        if self.pieces[node_index_from]["strength"] <= num:
            raise Exception("not enough pieces to move")

        to_owner = self.pieces[node_index_to]["player_index"]
        self.pieces[node_index_from]["strength"] -= num
        if to_owner != player_index:
            # attack
            self._resolve_attack(num, node_index_to)
        else:
            self.reinforce(num, node_index_to)

    def reinforce(self, num, node_index):
        player_index = self.cur_player
        self._check_valid_node(node_index)
        # for initially adding units to unclaimed territories
        if node_index in self.pieces:
            self._check_player_owns(player_index, node_index)
        self._add_node_strength(node_index, num)

    def next_turn(self):
        self.cur_player = (self.cur_player+ 1) % len(self.players)
        print(f"turn {self.cur_player}")

    def _check_valid_node(self, node_index):
        if node_index not in self.graph:
            raise Exception("bad node")

    def _check_player_owns(self, player_index, node_index):
        if self.pieces[node_index]["player_index"] != player_index:
            raise Exception("does not belong to this player.")

    def _resolve_attack(self, attacking_num, target_node_index) -> bool:
        '''
        returns whether attacker won.
        '''
        a = attacking_num
        b = self.pieces[target_node_index]["strength"]
        self._substract_node_strength(target_node_index, min(a, b))
        attacking_num -= min(a, b)

        won = attacking_num > 0
        if won:
            self.pieces[target_node_index]['player_index'] = self.cur_player
            self._add_node_strength(target_node_index, attacking_num)
        return won

    def _check_valid_unit_strength(self, num: int):
        if not isinstance(num, int):
            raise Exception("invalid unit strength type")
        if num < 0:
            raise Exception("negative units")

    def _add_node_strength(self, node_index, num: int):
        self.pieces[node_index]["strength"] += num

    def _substract_node_strength(self, node_index, num: int):
        self.pieces[node_index]["strength"] -= num


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',
        ]


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


class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:

        cost.append(0)

        for i in range(len(cost)-3,-1,-1): # starting on n-1 term
            cost[i] = min(cost[i]+cost[i+1],cost[i]+cost[i+2])

        return min(cost[0],cost[1])

        '''
        DP; start at subproblem (add a 0 to the input list)
        - we can consider the final step to be 0
        - work our way from n -> start
        '''


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()})


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]))


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 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')
]


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