import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10));
from matplotlib import rcParams
import scipy.ndimage

from skimage.measure import label, regionprops
import numpy as np

def name(obj, callingLocals=locals()):
    for k, v in list(callingLocals.items()):
         if v is obj:
            nm = k
    print(nm)

def resize_(image):
    h, w = image.shape[:2]
    if h<400:
        dif = int((400 - h)/2)
        pad_top = dif
        pad_bot = dif

        if pad_top + dif + h != 399:
            pad_top = dif + 1

        image = cv2.copyMakeBorder(image, pad_top, pad_bot, 0, 0, borderType=cv2.BORDER_CONSTANT, value=0)
    return image

def plot_(n,text_='',plotVisible=True):

    if plotVisible==True:
        print(text_)
        plt.imshow(n, cmap='gray')
        o = plt.show()
    return 0

def plotX(numplots=0, img1=False,label1="",img2=False,label2="",img3=False,label3="",img4=False,label4="", plotVisible=True):
    # Display result
    if plotVisible==True:
        fig, ax_arr = plt.subplots(1, numplots, constrained_layout=False, sharex=False, sharey=False, figsize=(10, 10))
        if numplots==2:
            ax1, ax2 = ax_arr.ravel()

            ax1.imshow(img1, cmap='gray')
            ax1.set_title(label1)

            ax2.imshow(img2, cmap='gray')
            ax2.set_title(label2)

        if numplots==4:
            ax1, ax2, ax3 ,ax4  = ax_arr.ravel()

            ax1.imshow(img1, cmap='gray')
            ax1.set_title(label1)

            ax2.imshow(img2, cmap='gray')
            ax2.set_title(label2)

            ax3.imshow(img3, cmap='gray')
            ax3.set_title(label1)

            ax4.imshow(img4, cmap='gray')
            ax4.set_title(label4)

        for ax in ax_arr.ravel():
            ax.set_axis_off()

        plt.tight_layout()
        plt.show()

def cut_edges(im_in):
    # Mask used to flood filling.
    ret,mask = cv2.threshold(im_in ,220,255,cv2.THRESH_BINARY)
    mask1 = mask.copy()

#     plot_(mask1)
    # Notice the size needs to be 2 pixels than the image.
    h, w = mask1.shape[:2]
    mask_ = np.zeros((h+2, w+2), np.uint8)

    # Floodfill from point (0, 0)
    if mask1[0,0] ==     255:cv2.floodFill(mask1, mask_, (0,0), 0)
    if mask1[h-2, 0] ==  255:cv2.floodFill(mask1, mask_, (0,h-2 ), 0)
    if mask1[0,w-2] ==   255:cv2.floodFill(mask1, mask_, (w-2,0), 0)
    if mask1[h-2,w-2] == 255:cv2.floodFill(mask1, mask_, (w-2,h-2), 0)

    im_out =  (im_in * (~(mask - mask1)/255)).astype('uint8')
#     print(im_out)
#     plot_(im_out)
    return im_out

def remove_holes(im_in):

    im_floodfill = im_in.copy()

    # Mask used to flood filling.
    # Notice the size needs to be 2 pixels than the image.
    h, w = im_in.shape[:2]
    mask_ = np.zeros((h+2, w+2), np.uint8)

    print(type(im_floodfill))
    print(np.min(im_floodfill),'-',np.max(im_floodfill))
    print(im_floodfill.shape)
    plt.imshow(im_floodfill, cmap='gray')
    plt.show()

    # Floodfill from point (0, 0)
    cv2.floodFill(im_floodfill, mask_, (0,0), 255);
    cv2.floodFill(im_floodfill, mask_, (w-1,h-1), 255);

    # Invert floodfilled image
    im_floodfill_inv = cv2.bitwise_not(im_floodfill)

    # Combine the two images to get the foreground.
    im_out = im_in | im_floodfill_inv

    return im_out

def remove_areas(m):
    L = label(m)

    while np.max(L)!=1:
#         print(np.max(L))
        reg_max=0
        reg_max_l = 0
        for region  in (regionprops(L)):
            # take regions with large enough areas
            if region.area > reg_max:
                reg_max= region.area
                reg_max_l = region.label

        L[L!=reg_max_l]=0
        L[L==reg_max_l]=1

    return L

def remove_bottom_areas(m,n):

    return L

def crop_image(mask, image):
    mask_=mask*1
    len_= mask.shape[0]-1
    y, y_ = 0, 0
    for pos,v in enumerate(mask_):
        for j, v_ in enumerate(v):
            if v_==1:
                y= pos
                break

    for i in range(len(mask_)):
        for j, v_ in enumerate( mask_[len_-i]):
            if v_==1:
                y_= len_-i
                break

    len_= mask_.shape[1]-1
    mask_=cv2.rotate(mask_, cv2.ROTATE_90_CLOCKWISE)

    x, x_ = 0, 0
    for pos,v in enumerate(mask_):
        for j, v_ in enumerate(v):
            if v_==1:
                x= pos
                break

    for i in range(len(mask_)):
        for j, v_ in enumerate( mask_[len_-i]):
            if v_==1:
                x_= len_-i
                break

    newim=image[y_:y,x_:x]

    return newim

def get_boundaries(img_bc, boundaries = 0):
    img_bc[img_bc>=1]=1
    y, x = img_bc.shape
    img_fin_bin = np.empty_like (img_bc[:])

    for j in range(5):
        i = j
        img_top_bin = np.empty_like (img_bc[i:])
        img_top_bin[:] = img_bc[i:]

        img_bot_bin = np.empty_like (img_bc[:y-i])
        img_bot_bin[:] = img_bc[:y-i]

        img_fin_bin[i:] = img_fin_bin[i:] + img_bot_bin
        img_fin_bin[:y-i] = img_fin_bin[:y-i] + img_top_bin

    img_bc[img_fin_bin>=1]=1
    return img_bc

def apply_brightness_contrast(input_img, brightness = 0, contrast = 0):

    if brightness != 0:
        if brightness > 0:
            shadow = brightness
            highlight = 255
        else:
            shadow = 0
            highlight = 255 + brightness
        alpha_b = (highlight - shadow)/255
        gamma_b = shadow

        buf = cv2.addWeighted(input_img, alpha_b, input_img, 0, gamma_b)
    else:
        buf = input_img.copy()

    if contrast != 0:
        f = 131*(contrast + 127)/(127*(131-contrast))
        alpha_c = f
        gamma_c = 127*(1-f)

        buf = cv2.addWeighted(buf, alpha_c, buf, 0, gamma_c)

    return buf

def normal_(result,t):
    result[result<t]=0

    return result.astype('uint8')

def buf_(result,t):
    result[result>=t]=255
    result[result<t]=0

    return result.astype('uint8')

def buf2_(result,t):
    result[result<t]=0

    return result.astype('uint8')

def grayscale(rgb):
    gray_ = np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

    return gray_

def cut_borders(gray_):
    y,x = gray_.shape
    margin = 10
    gray_=gray_[margin:y-margin,margin:x-margin]
    y,x = gray_.shape
    midX = int(x/2)
    gray_=gray_[:,midX-200:midX+200]

    return gray_

def main(filename,original):
#     temp= readImg(filename)
#     gray= grayscale(temp)
#     gray= grayscale(filename)
    inverted= invert(filename)
    b = scipy.ndimage.filters.gaussian_filter(inverted,sigma=5)
    return draw(b,filename,10,original)


import socket
import sys
import ast
from threading import *
from socketserver import *
import os
import threading
import time
threads=[]

HOST = '127.0.0.1'
PORT = 5004
SIZE = 2048

lock = threading.Lock()

class ClientThread(Thread):
    def __init__(self,ip,port,connexion):
            Thread.__init__(self)
            self.ip = ip
            self.port = port
            self.connexion=connexion
            print ("*****************New socket thread launched *****************")

    def run(self):

        def SignInParticipant(IP):
            File1=open('participant.txt','r')
            data=File1.readlines()
            File1.close()

            File3=open('participant.txt','w')
            File3.write('       '+str(len(data)+1)+'                      '+str(IP)+'    \n')
            File3.close()

        def ParticipantExists(IP):

                f=open('participant.txt','a')
                f.close()
                f=open('participant.txt','r')
                lines=f.readlines()
                Exists =False
                for participant in lines:
                    List= participant.split()
                    if (IP==List[1]):
                        Exists=True
                f.close()
                return Exists

        def SearchID(ip):
            File1=open('participant.txt','r')
            for participant in File1:
                L= participant.split()
                if (L[1] == ip):
                    return(L[0])

        def EnvoyerBien(self):
            f = open("bien.txt",'r')
            data = f.read()
            f.close()
            self.connexion.sendto(data.encode(),(self.ip,self.port))

        def Changeprix(ref,prix):
            f = open("bien.txt","r")
            lines=f.readlines()
            f.close()
            pos=0
            for report in lines:
                pos+=1
                L=report.split()
                if(L[0]==ref):
                    break
            L[3] = prix
            lines[pos-1]='   '+str(L[0])+'                  '+str(L[1])+'                    '+str(L[2])+'                 '+str(L[3])+'                 '+str(L[4])+'                 \n'
            lock.acquire()
            f = open("bien.txt","w")
            f.writelines(lines)
            lock.release()
            f.close()

        def ChangerEtat(ref,id):
            f = open("bien.txt","r")
            lines=f.readlines()
            f.close()
            pos=0
            for report in lines:
                pos+=1
                L=report.split()
                if(L[0]==ref):
                    break
            L[2] = 'Vendu'
            L[4] = id
            lines[pos-1]='   '+str(L[0])+'                  '+str(L[1])+'                    '+str(L[2])+'                 '+str(L[3])+'                 '+str(L[4])+'                 \n'
            lock.acquire()
            f = open("bien.txt","w")
            f.writelines(lines)
            lock.release()
            f.close()

        def SearchCurrentPrice(ref):
            f = open('bien.txt','r')
            lines=f.readlines()
            f.close()
            for report in lines:
                L=report.split()
                if(L[0]==ref):
                    return int(L[3])

        EnvoyerBien(self)
        if ((ParticipantExists(self.ip)==False)):
                SignInParticipant(self.ip)

        def SearchEtat(ref):
            f = open('bien.txt','r')
            lines=f.readlines()
            f.close()
            for report in lines:
                L=report.split()
                if(L[0]==ref):
                    return str(L[2])

        def Ajoterfacture(id,tot):
            f = open('facture.txt','a')
            f.close()
            f = open('facture.txt','r')
            lines=f.readlines()
            f.close()
            pos=0
            exist = False
            for report in lines:
                pos+=1
                L=report.split()
                if(L[0]==id):
                    exist = True
                    break
            if(exist == False):
                f = open('facture.txt','a')
                f.write('       '+str(id)+'                      '+str(tot)+'    \n')
                f.close()
            else:
                L[1] = tot
                lines[pos-1]='       '+str(L[0])+'                      '+str(L[1])+'    \n'
                f = open("facture.txt","w")
                f.writelines(lines)
                f.close()

        def EnvoyerFacture(self,id,prix):
            s = prix*0.2 + prix
            f = open('bien.txt','r')
            lines=f.readlines()
            f.close()
            tot = 0
            for report in lines:
                L=report.split()
                if(L[4]==id):
                    tot+=int(L[3])*0.2 + int(L[3])
            Ajoterfacture(id,tot)
            data = 'operation effectuée avec succès \n' + "prix de l'objet acheté " +str(s)+'\n'+ "prix total à payer"+str(tot)
            self.connexion.sendto(data.encode(),(self.ip,self.port))

        while(1):
            (data,addr) = self.connexion.recvfrom(SIZE)
            ref = str(data.decode())
            (data,addr) = self.connexion.recvfrom(SIZE)
            prix = int(data.decode())
            if (prix>SearchCurrentPrice(ref) and SearchEtat(ref) == 'Disponible'):
                Changeprix(ref,prix)
                time.sleep(10)
                crprix = SearchCurrentPrice(ref)
                id = SearchID(self.ip)
                if (crprix == prix):
                    ChangerEtat(ref,id)
                    EnvoyerFacture(self,id,prix)
                    f = open("hist"+str(ref)+".txt","a")
                    f.write('       '+str(id)+'                      '+str(prix)+'                    succes'+'\n')
                    f.close()
                else:
                    f = open("hist"+str(ref)+".txt","a")
                    f.write('       '+str(id)+'                      '+str(prix)+'                    echec'+'\n')
                    f.close()
                    EnvoyerBien(self)
            EnvoyerBien(self)

def Servermenu():
    while(True):
        print('Donner votre choix\n<0> Ajouter bien\n<1> Consulter la liste des biens\n<2> Consulter la facture d’un acheteur\n'+
        '<3> Consulter l’historique des proposions\n')
        x = int(input())
        if(x == 0):
            rep = "oui"
            File1 = open('bien.txt', 'a')
            while (rep != "non"):
                print("donner la reference de l'objet")
                ref = input()
                print("donner la prix de l'objet")
                prix = int(input())
                File1.write('   '+ref+'                  '+str(prix)+'                    '+'Disponible'+'                 '+str(prix)+'                 '+str(0)+'                 \n')
                print("Ajouter un autre bien ?!")
                rep = input()
            File1.close()
        if(x == 1):
            File1 = open('bien.txt', 'r')
            lines = File1.readlines()
            for line in lines:
                print(str(line))
            File1.close()
        if(x == 2):
            File1 = open('facture.txt', 'r')
            lines = File1.readlines()
            for line in lines:
                print(str(line))
            File1.close()
        if(x == 2):
            File1 = open('facture.txt', 'r')
            lines = File1.readlines()
            for line in lines:
                print(str(line))
            File1.close()
        if(x == 3):
            a = int(input("donner reference de l'objet"))
            File1 = open('hist'+str(a)+'.txt', 'r')
            lines = File1.readlines()
            for line in lines:
                print(str(line))
            File1.close()

def Serveur():
    mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    mysocket.bind(('127.0.0.1', 5004))
    mysocket.listen(3)

    while True :
        print('waiting')
        (client, (ip,port)) = mysocket.accept()
        newthread = ClientThread(ip,port,client)
        newthread.start()
        threads.append(newthread)

def main():
    print("Donner votre choix :\n 1_Lancer le serveur\n 2_Consulter les fichiers\n")
    x = int(input())
    if(x == 1):
        Serveur()
    if(x == 2):
        Servermenu()

main()


from colossalai.zero.shard_utils import TensorShardStrategy

zero = dict(model_config=dict(shard_strategy=TensorShardStrategy(),
                              reduce_scatter_bucket_size_mb=25,
                              fp32_reduce_scatter=False,
                              tensor_placement_policy="cuda",
                              gradient_predivide_factor=1.0,
                              reuse_fp16_shard=True),
            optimizer_config=dict(gpu_margin_mem_ratio=0.8,
                                  initial_scale=2**5,
                                  min_scale=1,
                                  growth_factor=2,
                                  backoff_factor=0.5,
                                  growth_interval=1000,
                                  hysteresis=2,
                                  max_scale=2**32))


# CatOS-type package
author = "catwared, https://vk.com/voroninmine"
mode = "parameter"
deps = 'None'
identificator = 'dehex'
command_ru = 'dehex'
description = 'Декодировщик текста из HEX'

try:
    r = ''.join(map(lambda i: chr(int(i, base=16)), parameter.split()))
    message(f"Результат: {r}", reply=True)
except Exception as e:
    message("Произошла ошибка при декодировании - " + str(e).upper().replace(" ", "_"), reply=True)


# -*- coding: UTF-8 -*-
"""
=========
frequency
=========

Script : frequency.py

Author :  Dan_Patterson@carleton.ca

Modified : 2019-02-23

Purpose :
    To supplant the Frequency tool for those that don't have an
    advanced license.

Useage :  Load the toolbox into Pro and run the tool script from there.

References
----------
`<http://desktop.arcgis.com/en/arcmap/latest/tools/analysis-toolbox/
frequency.htm>`_.

Notes
-----

>>> to_array = arcpy.da.TableToNumPyArray(r"C:\folder\sample.dbf", "*")
>>> arcpy.da.NumPyArrayToTable(from_array, r"C:\folder_tbl\test.gdb\out")

Dev Info

  tbx - C:\GIS\Tools_scripts\Table_tools\Table_tools.tbx
  script - C:\GIS\Tools_scripts\Table_tools\Scripts\frequency.py
  arcpy.Tabletools.Frequency("polygon_demo",
             r"C:\GIS\Tools_scripts\Table_tools\Table_tools.gdb\f2",
             "Test;main_part", None)
:---------------------------------------------------------------------:
"""
# ---- imports, formats, constants ----
import sys
import numpy as np
import arcpy

import numpy.lib.recfunctions as rfn

ft = {'bool': lambda x: repr(x.astype('int32')),
      'float': '{: 0.3f}'.format}
np.set_printoptions(edgeitems=10, linewidth=80, precision=2, suppress=True,
                    threshold=100, formatter=ft)
np.ma.masked_print_option.set_display('-')  # change to a single -

script = sys.argv[0]  # print this should you need to locate the script

def freq(a, flds=None):
    """Frequency and crosstabulation

    Parameters
    ----------
    a : array
        A structured array
    flds : field
        Fields to use in the analysis

    Notes
    -----
    : (1) slice the input array by the classification fields
    : (2) sort the sliced array using the flds as sorting keys
    : (3) use unique on the sorted array to return the results
    : (4) a quick histogram to get the counts until numpy 1.12 can be used
    : ... then ship the results back.  only uni and vals is needed. The
    :     rest is for testing and future work.
    >>> np.unique(ar, return_index=False, return_inverse=False,
    ...           return_counts=False, axis=None)
    """
    a = a[flds]  # (1)
    idx = np.argsort(a, axis=0, order=flds)  # (2)
    a_sort = a[idx]
    uni, cnts = np.unique(a_sort, return_counts=True)  # (3)
    out = rfn.append_fields(uni, names="Counts", data=cnts, usemask=False)
    return out

if len(sys.argv) == 1:
    testing = True
    in_tbl = r"C:\Arc_projects\Table_tools\Table_tools.gdb\SamplingGrids"
    all_flds = "*"
    cls_flds = 'Row_class;Col_class'
else:
    testing = False
    in_tbl = sys.argv[1]           # input table
    out_tbl = sys.argv[2]          # results table
    cls_flds = sys.argv[3]         # classification field(s)

# ---- common tasks
cls_flds = cls_flds.split(";")  # tidy up the two field inputs
a = arcpy.da.TableToNumPyArray(in_tbl, "*")  # use the full array's data

out = freq(a, cls_flds)  # do freq analysis

# create the output array and return the table
if not testing:
    arcpy.da.NumPyArrayToTable(out, out_tbl)

# ----------------------------------------------------------------------
# __main__ .... code section
if __name__ == "__main__":
    """Do nothing for now.
    """


import numpy as np

np.random.seed(1)

def get_prediction(layer, weight):
    prediction_value = np.dot(layer, weight)
    return prediction_value

def get_delta(prediction, goal_prediction):
    delta_value = prediction - goal_prediction
    return delta_value

def get_error(delta):
    error_value = delta ** 2
    return error_value

def get_derivative(delta, layer):
    derivative_value = layer.dot(delta)
    return derivative_value

def get_weight(weights, derivative, learning_rate):
    for index in reversed(range(0, len(weights))):
        # print(f'weights[{index}] -= learning_rate ({learning_rate}) * derivative[index] ({derivative[index]})')
        weights[index] -= learning_rate * derivative[index]
        # weights[index] -= learning_rate * derivative[index]
    return weights

def relu(x):
    return (x > 0) * x

def relu2deriv(out):
    return out > 0

def nn(input, weight, goal_prediction, alpha=1.0, to_visual=0):
    print_star_num = 80

    layer_0 = input
    if to_visual == 1:
        print(f'layer_0 [{i}:{i + 1}] = {arr_print(layer_0, 10)}')
        print("*" * print_star_num)
    layer_1 = relu(get_prediction(layer=layer_0, weight=weight[0]))     # weight = weights_0_to_1
    if to_visual == 1:
        print(f'layer_1  = {arr_print(layer_1, 10)}')
        print(f'--> relu(np.dot(layer_0, weights_0_to_1))')
        print(f'  --> np.dot(layer_0, weights_0_to_1)): \n'
              f'      layer_0 = \n{arr_print(layer_0, 10)}\n'
              f'      weights_0_to_1 = {arr_print(weight[0], 10)}')
        print("*" * print_star_num)
    layer_2 = get_prediction(layer=layer_1, weight=weight[1])           # weight = weights_1_to_2
    if to_visual == 1:
        print(f'layer_2  = {arr_print(layer_2, 10)}')
        print(f'  --> np.dot(layer_1, weights_1_to_2)): \n'
              f'      layer_1 = {arr_print(layer_1, 10)}\n'
              f'      weights_1_to_2 = {arr_print(weight[1], 10)}')
        print("*" * print_star_num)

    # goal_prediction = walk_vs_stop[i:i + 1]
    layer_2_delta = get_delta(prediction=layer_2, goal_prediction=goal_prediction)
    if to_visual == 1:
        print(f'layer_2_delta  = {arr_print(layer_2_delta, 10)}')
        print("*" * print_star_num)
    layer_2_derivative = get_derivative(delta=layer_2_delta, layer=layer_1.T)
    error_2_layer = get_error(delta=layer_2_delta)
    if to_visual == 1:
        print(f'error_2_layer  = {error_2_layer}')
        print("*" * print_star_num)

    layer_1_delta = get_derivative(layer=layer_2_delta, delta=weight[1].T) * relu2deriv(layer_1)
    if to_visual == 1:
        print(f'layer_2_delta  = {arr_print(layer_1_delta, 10)}')
        print("*" * print_star_num)
    layer_1_derivative = get_derivative(delta=layer_1_delta, layer=layer_0.T)

    # weight = weights_0_to_1, weights_1_to_2
    derivative = layer_1_derivative, layer_2_derivative
    weight = get_weight(weights=weight, derivative=derivative, learning_rate=alpha)

    prediction = layer_2
    delta = layer_1_delta, layer_2_delta
    error = error_2_layer
    weights = weight
    return prediction, delta, error, derivative, weights

def arr_print(arr, symb_nth=0):
    arr_len = list(arr.shape)
    arr_out = []
    for dim in range(arr_len[0]):
        arr_current_string = []
        for dim_inside in arr[dim]:
            arr_current_string.append(dim_inside)
        arr_out.append(arr_current_string.copy())
    new = '\n'
    max_numbers_per_column = 8
    round_number = 5
    def gen_nth(length):
        return "".join([" " for i in range(length)])
    def check_enough_elements(string, length):
        add_number = 0
        if len(string) < length:
            add_number = length - len(string)
        return add_number
    line_horizontal = gen_nth(symb_nth) + "-" * (arr_len[1]*(max_numbers_per_column + 3) + 1)
    generated_data = f"{new}{gen_nth(symb_nth)}| ".join(["".join([str(iii) + gen_nth(check_enough_elements(str(iii), max_numbers_per_column)) + " | " for iii in np.around(i, decimals=round_number)]) for i in arr_out])
    return f'\n{line_horizontal}\n{gen_nth(symb_nth)}| {generated_data}\n{line_horizontal}'

streetlights = np.array([[1, 0, 1],
                         [0, 1, 1],
                         [0, 0, 1],
                         [1, 1, 1]])

walk_vs_stop = np.array([1, 1, 0, 0]).T

alpha = 0.2
hidden_size = 4

weights_0_to_1 = 2 * np.random.random((3, hidden_size)) - 1
weights_1_to_2 = 2 * np.random.random((hidden_size, 1)) - 1
print(f'size weights_0_to_1 = {weights_0_to_1.shape}  \n'
      f'{arr_print(weights_0_to_1)}')
print(f'size weights_1_to_2 = {weights_1_to_2.shape}  \n'
      f'{arr_print(weights_1_to_2)}')

input = streetlights[0]  # 1, 0, 0
goal_prediction = walk_vs_stop[0]  # 0

weight = [weights_0_to_1, weights_1_to_2]

to_visual = 0
to_visual_part = 0
iter_to_show_detailed = [0, 1]

for ii in range(60):
    error_global = 0
    if (to_visual_part == 1) and (ii in iter_to_show_detailed):
        to_visual = 1
    for i in range(len(streetlights)):
        input = streetlights[i:i+1]            # change inputs
        goal_prediction = walk_vs_stop[i:i+1]  # change goal prediction
        # prediction, delta, error, derivative, weights
        results_my = nn(input=input, weight=weight, goal_prediction=goal_prediction, alpha=alpha, to_visual=to_visual)
        error_global += np.sum(results_my[2])       # compute error for each iteraration during 1 epoch
        weight = results_my[4]              # update weights
    if to_visual == 1:
        print("'"*150)
    if (to_visual_part == 1) and (ii in iter_to_show_detailed):
        to_visual = 0
    print(f'ITER = {ii}   --->   error_global = {error_global}')

# page 138


# Aula 13 - Desafio 54: Grupo da maioridade
# Ler o ano de nascimento de 7 pessoas. Depois informar quantas nao atingiram a maioridade (21) e quantas ja atingiram

from datetime import date

ano = date.today().year
jovem, adulto = 0, 0

for n in range(1, 8):
    nasc = int(input(f'Ano de nascimento da {n}ª pessoa: '))
    idade = ano - nasc
    if idade < 21:
        jovem += 1
    else:
        adulto += 1

print(f'Das 7 pessoas, {jovem} nao atingiram a maioridade e {adulto} ja.')


class City:
    def __init__(self, name, country, region, latitude, longitude, population, timezone):
        self.name = name
        self.country = country
        self.region = region
        self.latitude = latitude
        self.longitude = longitude
        self.population = population
        self.timezone = timezone

    def __str__(self):
        return f"{self.name}, a city located in {self.country}, in the {self.region} region. " \
               f"Its geographical coordinates are {self.latitude}, {self.longitude}. " \
               f"It is inhabited by {self.population} people. It is in the {self.timezone} time zone."


# !/usr/bin/python
# -*- coding:utf-8 -*-
# @Time     : 2021/3/17 16:04
# @Author   : Yuan Chuxuan
# @File     : BM.py

import DM
from BasicModel import *

'''
此代码的注释是由袁楚轩完成的，有任何问题，请联系本人
'''

def get_feature(file_list: list, crypto_list: list, fuc: str, model_name):
    """
    创建特征文件
    用CnPo特征进行分类

    :param file_list: ones文件的地址
    :param crypto_list: 加密算法种类
    :param model_name: 模型名称
    :return: 特征的list[]
    """
    conv_feature = get_conv_feature(file_list, crypto_list, 192, fuc=fuc)
    return conv_feature

def load_and_test(file_list: list, model_name, args):
    """
     use the pre-trained model

    :param file_list: the directory of feature file
    :param model_name: the name of the model used.
    :param args: basic args
    :return: result list
    """
    # 访问特征
    ans = []
    confidence = []
    conv_feature_list = []
    # 利用网络获得ones特征
    ones_file_dir = DM.get_feature(file_list, 'classify')
    # 分类
    if model_name == 'RF':
        # 获得卷积特征
        conv_feature_list = get_feature([ones_file_dir], args.crypto_list, fuc='classify', model_name='RF')
        ans, confidence = RF(args.crypto_list, conv_feature_list)
    elif model_name == 'SVM':
        # 获得卷积特征
        conv_feature_list = get_feature([ones_file_dir], args.crypto_list, fuc='classify', model_name='SVM')
        ans, confidence = SVM(args.crypto_list, conv_feature_list)
    result = []
    ans = ans[0].tolist()
    for i in range(0, len(conv_feature_list)):  # make decision
        for j in range(0, 4):
            if ans[i] == j:
                result.append(args.crypto_list[j] + '加密')

    return result, confidence

def train_and_test(crypto_file_list: dict, model_name, args):
    """
    train and test the model

    :param crypto_file_list: 一个加密算法有一个list全部是文件
    :param model_name: the name of the model used.
    :param args: args used to train the model
    :return: acc, file_path, model_path
    """
    # 得到ones文件
    ones_file_dirs = []
    for tmp in args.crypto_list:
        ones_file_dirs.append(DM.get_feature(crypto_file_list[tmp], tmp))
    # 获得特征列表
    get_feature(ones_file_dirs, args.crypto_list, fuc='train', model_name=model_name)  # back  involve without
    # 训练
    acc, file_path, model_path = train(model_name, args.crypto_list)

    return acc * 100, file_path, model_path

# for routine please see backend.py


import os
import tarfile
from six.moves import urllib
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml/master/"
HOUSING_PATH = "datasets/housing"
HOUSING_URL = DOWNLOAD_ROOT + HOUSING_PATH + "/housing.tgz"

def fetch_housing_data(housing_url=HOUSING_URL, housing_path=HOUSING_PATH):
    if not os.path.isdir(housing_path):
        os.makedirs(housing_path)
    tgz_path = os.path.join(housing_path, "housing.tgz")
    urllib.request.urlretrieve(housing_url, tgz_path)
    housing_tgz = tarfile.open(tgz_path)
    housing_tgz.extractall(path=housing_path)
    housing_tgz.close()

def load_housing_data(housing_path=HOUSING_PATH):
    csv_path = os.path.join(housing_path, "housing.csv")
    return pd.read_csv(csv_path)

housing = load_housing_data()

# print(housing.info())
# print(housing["ocean_proximity"].value_counts())
# print(housing.describe())

# housing.hist(bins=50, figsize=(20, 15))
# plt.show()

def split_train_test(data, test_ratio):
    shuffled_indices = np.random.permutation(len(data))
    test_set_size = int(len(data) * test_ratio)
    test_indices = shuffled_indices[:test_set_size]
    train_indices = shuffled_indices[test_set_size:]
    return data.iloc[train_indices], data.iloc[test_indices]

train_set, test_set = split_train_test(housing, 0.2)
# print(f'{len(train_set)} train + {len(test_set)} test')

import hashlib

def test_set_check(indentifier, test_ratio, hash):
    return hash(np.int64(indentifier)).digest()[-1] < 256 * test_ratio

def split_train_test_by_id(data, test_ratio, id_colum, hash=hashlib.md5):
    ids = data[id_colum]
    in_test_set = ids.apply(
        lambda id_: test_set_check(id_, test_ratio, hash))
    return data.loc[~in_test_set], data.loc[in_test_set]

housing_with_id = housing.reset_index()
train_set, test_set = split_train_test_by_id(
    housing_with_id, 0.2, "index")
# print(f'{len(train_set)} train + {len(test_set)} test')

from sklearn.model_selection import train_test_split, StratifiedShuffleSplit

train_set, test_set = train_test_split(
    housing_with_id, test_size=0.2, random_state=42)
# print(f'{len(train_set)} train + {len(test_set)} test')

housing["income_cat"] = np.ceil(housing["median_income"] / 1.5)
housing["income_cat"].where(housing["income_cat"] < 5, 5.0, inplace=True)

split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)
for train_index, test_index in split.split(housing, housing["income_cat"]):
    strat_train_set = housing.loc[train_index]
    strat_test_set = housing.loc[test_index]
# print(housing["income_cat"].value_counts() / len(housing))

for set in (strat_train_set, strat_test_set):
    set.drop(["income_cat"], axis=1, inplace=True)


import bpy
import random

# DELETE MESHES
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

# COLORS
black = (0, 0, 0, 1)
color1 = bpy.data.materials.new("MondrianBlack")
color1.diffuse_color = black

white = (1, 1, 1, 1)
color2 = bpy.data.materials.new("MondrianWhite")
color2.diffuse_color = white

blue = (0, 0, 1, 0)
color3 = bpy.data.materials.new("MondrianBlue")
color3.diffuse_color = blue

red = (1, 0, 0, 1)
color4 = bpy.data.materials.new("MondrianRed")
color4.diffuse_color = red

yellow = (1, 1, 0, 1)
color5 = bpy.data.materials.new("MondrianYellow")
color5.diffuse_color = yellow

# WHITE PLANE
bpy.ops.mesh.primitive_plane_add()
bpy.ops.transform.translate(value=(0, -3, 0))
bpy.ops.transform.resize(value=(16, 22, 14))
monPlane1 = bpy.context.active_object
monPlane1.data.materials.append(color2)

# NUMBER 3
bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(-10, -1, 0))
myCube1 = bpy.context.active_object
myCube1.data.materials.append(color1)

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(-6, 1, 0))
bpy.ops.transform.resize(value=(3, 1, 1))
myCube2 = bpy.context.active_object
myCube2.data.materials.append(color1)

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(-2, -1, 0))
bpy.ops.transform.resize(value=(1, 2, 1))
myCube3 = bpy.context.active_object
myCube3.data.materials.append(color1)

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(-5, -4, 0))
bpy.ops.transform.resize(value=(3, 1, 1))
myCube4 = bpy.context.active_object
myCube4.data.materials.append(color1)

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(-6, 1, 0))
bpy.ops.transform.resize(value=(3, 1, 1))
myCube5 = bpy.context.active_object
myCube5.data.materials.append(color1)

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(-2, -7, 0))
bpy.ops.transform.resize(value=(1, 2, 1))
myCube6 = bpy.context.active_object
myCube6.data.materials.append(color1)

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(-6, -9, 0))
bpy.ops.transform.resize(value=(3, 1, 1))
myCube7 = bpy.context.active_object
myCube7.data.materials.append(color1)

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(-10, -7, 0))
myCube8 = bpy.context.active_object
myCube8.data.materials.append(color1)

# LETTER D
bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(3, -4, 0))
bpy.ops.transform.resize(value=(1, 6, 1))
myCube8 = bpy.context.active_object
myCube8.data.materials.append(color1)

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(7, 1, 0))
bpy.ops.transform.resize(value=(3, 1, 1))
myCube9 = bpy.context.active_object
myCube9.data.materials.append(color1)

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(10, -4, 0))
bpy.ops.transform.resize(value=(1, 4, 1))
myCube10 = bpy.context.active_object
myCube10.data.materials.append(color1)

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(7, -9, 0))
bpy.ops.transform.resize(value=(3, 1, 1))
myCube11 = bpy.context.active_object
myCube11.data.materials.append(color1)

# COLOR LIST

colorList = [color3, color4, color5]

# COLORED CUBES

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(-10, 12.5, 0))
bpy.ops.transform.resize(value=(1.8, 4, 1))
myCubeColored1 = bpy.context.active_object
myCubeColored1.data.materials.append(random.choice(colorList))

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(-5, 8.5, 0))
bpy.ops.transform.resize(value=(5, 1, 1))
myCubeColored2 = bpy.context.active_object
myCubeColored2.data.materials.append(random.choice(colorList))

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(6, 5, 0))
bpy.ops.transform.resize(value=(2, 2, 2))
myCubeColored3 = bpy.context.active_object
myCubeColored3.data.materials.append(random.choice(colorList))

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(12, 13, 0))
bpy.ops.transform.resize(value=(1.8, 4, 1))
myCubeColored4 = bpy.context.active_object
myCubeColored4.data.materials.append(random.choice(colorList))

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(-12, -15, 0))
bpy.ops.transform.resize(value=(2, 2, 2))
myCubeColored5 = bpy.context.active_object
myCubeColored5.data.materials.append(random.choice(colorList))

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(-3.5, -17, 0))
bpy.ops.transform.resize(value=(1.8, 4, 1))
myCubeColored6 = bpy.context.active_object
myCubeColored6.data.materials.append(random.choice(colorList))

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(3.5, -23, 0))
bpy.ops.transform.resize(value=(5, 1, 1))
myCubeColored7 = bpy.context.active_object
myCubeColored7.data.materials.append(random.choice(colorList))

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(8, -19, 0))
bpy.ops.transform.resize(value=(2, 2, 1))
myCubeColored8 = bpy.context.active_object
myCubeColored8.data.materials.append(random.choice(colorList))


import csv
import logging
from decimal import Decimal

from model.parse import validate_currency

logger = logging.getLogger(f'classinvoices.{__name__}')

def validate_fee_schedule_row(r, row, errors=None):
    if errors is None:
        errors = []
    if len(row) < 3:
        errors.append(f'Too few columns ({len(row)} < 3) in fee schedule on row {r + 1}: {row}')
        row.extend(['', '', ''])  # Make sure we have enough columns to process below
    class_name = row[0]
    fee = validate_currency(row[2])
    if fee is None:
        errors.append(f'Invalid currency in row {r + 1}, column 3 of fee schedule: {row[2]}')
        fee = Decimal(0.0)
    teacher = row[1]
    if not teacher and fee:
        raise RuntimeError(f'Empty teacher in row {r + 1}, column 2 of fee schedule when fee is not $0')
    return [class_name, teacher, fee]

def read_fee_schedule(path, errors=None):
    """Now open the fee schedule CSV and process its rows.
    Return a list of fee schedule rows, which are 3-tuples:
    (Class name (str), Teacher (str), Fee (Decimal, 2 places))"""
    if errors is None:
        errors = []
    fee_schedule = []
    with open(path, 'r') as f:
        reader = csv.reader(f)
        for r, row in enumerate(reader):
            try:
                fee_schedule.append(validate_fee_schedule_row(r, row, errors=errors))
            except KeyError:
                # Ignore validation error on first row, as it may be a header row
                if r != 0:
                    raise
    return fee_schedule


#!/usr/bin/python3
def square_matrix_simple(matrix=[]):
    dim_row = len(matrix)
    dim_col = len(matrix[0])
    matrix1 = []
    for a in range(dim_row):
        row = []
        for b in range(dim_col):
            row.append(matrix[a][b])
        matrix1.append(row)
    for x in range(len(matrix)):
        for y in range(len(matrix[x])):
            matrix1[x][y] = matrix[x][y]
    for i in range(len(matrix1)):
        for j in range(len(matrix1[i])):
            matrix1[i][j] = matrix1[i][j] * matrix1[i][j]
    return matrix1


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

# ## Language model for the Shakespeare dataset

# # Code by Hao, Zhao, July, 10, 2020.

# ### Motivations:
#
# Use the text preprocessing tools and RNN models to build a character-level language model, and then train the model on the works of Shakespeare, and use the network to generate the similar style of own text..
#

# In[1]:

#### PACKAGE IMPORTS ####

# Run this cell first to import all required packages. Do not make any imports elsewhere in the notebook

import tensorflow as tf
import numpy as np
import json
import matplotlib.pyplot as plt
# get_ipython().run_line_magic('matplotlib', 'inline')

# If you would like to make further imports from tensorflow, add them here
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, Flatten, Embedding, GRU
from tensorflow.keras.models import Model,Sequential

# ![Shakespeare image](test_data_folder/shakespeare/data/shakespeare.png)
#
# #### The Shakespeare dataset
#
# In this assignment, you will use a subset of the [Shakespeare dataset](http://shakespeare.mit.edu). It consists of a single text file with several excerpts concatenated together. The data is in raw text form, and so far has not yet had any preprocessing.
#
# Your goal is to construct an unsupervised character-level sequence model that can generate text according to a distribution learned from the dataset.

# #### Load and inspect the dataset

# In[2]:

# Load the text file into a string

with open('test_data_folder/shakespeare/data/Shakespeare.txt', 'r', encoding='utf-8') as file:
    text = file.read()

# In[3]:

# Create a list of chunks of text

text_chunks = text.split('.')

# To give you a feel for what the text looks like, we will print a few chunks from the list.

# In[4]:

# Display some randomly selected text samples

num_samples = 5
inx = np.random.choice(len(text_chunks), num_samples, replace=False)
for chunk in np.array(text_chunks)[inx]:
    print(chunk)

# #### Create a character-level tokenizer

# You should now write a function that returns a `Tokenizer` object. The function takes a list of strings as an argument, and should create a `Tokenizer` according to the following specification:
#
# * The number of tokens should be unlimited (there should be as many as required by the dataset).
# * Tokens should be created at the character level (not at the word level, which is the default behaviour).
# * No characters should be filtered out or ignored.
# * The original capitalization should be retained (do not convert the text to lower case)
#
# The `Tokenizer` should be fit to the `list_of_strings` argument and returned by the function.
#
# **Hint:** you may need to refer to the [documentation](https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/text/Tokenizer) for the `Tokenizer`.

# In[5]:

#### GRADED CELL ####

# Complete the following function.
# Make sure not to change the function name or arguments.

def create_character_tokenizer(list_of_strings):
    """
    This function takes a list of strings as its argument. It should create
    and return a Tokenizer according to the above specifications.
    """
    tokenizer = Tokenizer(num_words=None,
                         char_level=True,
                         filters=None,
                         lower=False,
                         )

    tokenizer.fit_on_texts(list_of_strings)

    return tokenizer

# In[6]:

# Get the tokenizer

tokenizer = create_character_tokenizer(text_chunks)

# #### Tokenize the text
#
# You should now write a function to use the tokenizer to map each string in `text_chunks` to its corresponding encoded sequence. The following function takes a fitted `Tokenizer` object in the first argument (as returned by `create_character_tokenizer`) and a list of strings in the second argument. The function should return a list of lists, where each sublist is a sequence of integer tokens encoding the text sequences according to the mapping stored in the tokenizer.
#
# **Hint:** you may need to refer to the [documentation](https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/text/Tokenizer) for the `Tokenizer`.

# In[7]:

#### GRADED CELL ####

# Complete the following function.
# Make sure not to change the function name or arguments.

def strings_to_sequences(tokenizer, list_of_strings):
    """
    This function takes a tokenizer object and a list of strings as its arguments.
    It should use the tokenizer to map the text chunks to sequences of tokens and
    then return this list of encoded sequences.
    """

    sequences_encoded = tokenizer.texts_to_sequences(list_of_strings)

    return sequences_encoded

# In[8]:

# Encode the text chunks into tokens

seq_chunks = strings_to_sequences(tokenizer, text_chunks)

# #### Pad the encoded sequences and store them in a numpy array

# Since not all of the text chunks are the same length, you will need to pad them in order to train on batches. You should now complete the following function, which takes the list of lists of tokens, and creates a single numpy array with the token sequences in the rows, according to the following specification:
#
# * The longest allowed sequence should be 500 tokens. Any sequence that is longer should be shortened by truncating the beginning of the sequence.
# * Use zeros for padding the sequences. The zero padding should be placed before the sequences as required.
#
# The function should then return the resulting numpy array.
#
# **Hint:** you may want to refer to the [documentation](https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/sequence/pad_sequences) for the `pad_sequences` function.

# In[9]:

#### GRADED CELL ####

# Complete the following function.
# Make sure not to change the function name or arguments.

def make_padded_dataset(sequence_chunks):
    """
    This function takes a list of lists of tokenized sequences, and transforms
    them into a 2D numpy array, padding the sequences as necessary according to
    the above specification. The function should then return the numpy array.
    """
    sequences_padded = tf.keras.preprocessing.sequence.pad_sequences(sequence_chunks,maxlen=500,padding='pre',value=0,truncating='pre')

    return sequences_padded

# In[10]:

# Pad the token sequence chunks and get the numpy array

padded_sequences = make_padded_dataset(seq_chunks)

# #### Create model inputs and targets
#
# Now you are ready to build your RNN model. The model will receive a sequence of characters and predict the next character in the sequence. At training time, the model can be passed an input sequence, with the target sequence is shifted by one.
#
# For example, the expression `To be or not to be` appears in Shakespeare's play 'Hamlet'. Given input `To be or not to b`, the correct prediction is `o be or not to be`. Notice that the prediction is the same length as the input!
#
# ![sequence_prediction_example](test_data_folder/shakespeare/data/rnn_example.png)
#
# You should now write the following function to create an input and target array from the current `padded_sequences` array. The function has a single argument that is a 2D numpy array of shape `(num_examples, max_seq_len)`. It should fulfil the following specification:
#
# * The function should return an input array and an output array, both of size `(num_examples, max_seq_len - 1)`.
# * The input array should contain the first `max_seq_len - 1` tokens of each sequence.
# * The output array should contain the last `max_seq_len - 1` tokens of each sequence.
#
# The function should then return the tuple `(input_array, output_array)`. Note that it is possible to complete this function using numpy indexing alone!

# In[11]:

#### GRADED CELL ####

# Complete the following function.
# Make sure not to change the function name or arguments.

def create_inputs_and_targets(array_of_sequences):
    """
    This function takes a 2D numpy array of token sequences, and returns a tuple of two
    elements: the first element is the input array and the second element is the output
    array, which are defined according to the above specification.
    """
    max_seq_len  = array_of_sequences.shape[1]
    input_array  = array_of_sequences [:, : (max_seq_len - 1)]
    output_array = array_of_sequences [:, -(max_seq_len-1):]
    return input_array,output_array

# In[12]:

# Create the input and output arrays

input_seq, target_seq = create_inputs_and_targets(padded_sequences)

# #### Preprocess sequence array for stateful RNN
#
# We will build our RNN language model to be stateful, so that the internal state of the RNN will be maintained across batches. For this to be effective, we need to make sure that each element of every batch follows on from the corresponding element of the preceding batch (you may want to look back at the "Stateful RNNs" reading notebook earlier in the week).
#
# The following code processes the input and output sequence arrays so that they are ready to be split into batches for training a stateful RNN, by re-ordering the sequence examples (the rows) according to a specified batch size.

# In[13]:

# Fix the batch size for training

batch_size = 32

# In[14]:

# Prepare input and output arrays for training the stateful RNN

num_examples = input_seq.shape[0]

num_processed_examples = num_examples - (num_examples % batch_size)

input_seq = input_seq[:num_processed_examples]
target_seq = target_seq[:num_processed_examples]

steps = int(num_processed_examples / 32)  # steps per epoch

inx = np.empty((0,), dtype=np.int32)
for i in range(steps):
    inx = np.concatenate((inx, i + np.arange(0, num_processed_examples, steps)))

input_seq_stateful = input_seq[inx]
target_seq_stateful = target_seq[inx]

# #### Split the data into training and validation sets
#
# We will set aside approximately 20% of the data for validation.

# In[15]:

# Create the training and validation splits

num_train_examples = int(batch_size * ((0.8 * num_processed_examples) // batch_size))

input_train = input_seq_stateful[:num_train_examples]
target_train = target_seq_stateful[:num_train_examples]

input_valid = input_seq_stateful[num_train_examples:]
target_valid = target_seq_stateful[num_train_examples:]

# #### Create training and validation Dataset objects
#
# You should now write a function to take the training and validation input and target arrays, and create training and validation `tf.data.Dataset` objects. The function takes an input array and target array in the first two arguments, and the batch size in the third argument. Your function should do the following:
#
# * Create a `Dataset` using the `from_tensor_slices` static method, passing in a tuple of the input and output numpy arrays.
# * Batch the `Dataset` using the `batch_size` argument, setting `drop_remainder` to `True`.
#
# The function should then return the `Dataset` object.

# In[16]:

#### GRADED CELL ####

# Complete the following function.
# Make sure not to change the function name or arguments.

def make_Dataset(input_array, target_array, batch_size):
    """
    This function takes two 2D numpy arrays in the first two arguments, and an integer
    batch_size in the third argument. It should create and return a Dataset object
    using the two numpy arrays and batch size according to the above specification.
    """
    dataset = tf.data.Dataset.from_tensor_slices((input_array,target_array))
    dataset = dataset.batch(batch_size,drop_remainder=True)

    return dataset

# In[17]:

# Create the training and validation Datasets

train_data = make_Dataset(input_train, target_train, batch_size)
valid_data = make_Dataset(input_valid, target_valid, batch_size)

# #### Build the recurrent neural network model

# You are now ready to build your RNN character-level language model. You should write the following function to build the model; the function takes arguments for the batch size and vocabulary size (number of tokens). Using the Sequential API, your function should build your model according to the following specifications:
#
# * The first layer should be an Embedding layer with an embedding dimension of 256 and set the vocabulary size to `vocab_size` from the function argument.
# * The Embedding layer should also mask the zero padding in the input sequences.
# * The Embedding layer should also set the `batch_input_shape` to `(batch_size, None)` (a fixed batch size is required for stateful RNNs).
# * The next layer should be a (uni-directional) GRU layer with 1024 units, set to be a stateful RNN layer.
# * The GRU layer should return the full sequence, instead of just the output state at the final time step.
# * The final layer should be a Dense layer with `vocab_size` units and no activation function.
#
# In total, the network should have 3 layers.

# In[18]:

#### GRADED CELL ####

# Complete the following function.
# Make sure not to change the function name or arguments.

def get_model(vocab_size, batch_size):
    """
    This function takes a vocabulary size and batch size, and builds and returns a
    Sequential model according to the above specification.
    """
    model = Sequential([Embedding(input_dim=vocab_size, output_dim=256, mask_zero=True, batch_input_shape = (batch_size,None)),
                        GRU(1024,stateful=True,return_sequences=True),
                        Dense(vocab_size)
                       ])
    return model

# In[19]:

# Build the model and print the model summary

model = get_model(len(tokenizer.word_index) + 1, batch_size)
model.summary()

# #### Compile and train the model
#
# You are now ready to compile and train the model. For this model and dataset, the training time is very long. Therefore for this assignment it is not a requirement to train the model. We have pre-trained a model for you (using the code below) and saved the model weights, which can be loaded to get the model predictions.
#
# It is recommended to use accelerator hardware (e.g. using Colab) when training this model. It would also be beneficial to increase the size of the model, e.g. by stacking extra recurrent layers.

# In[20]:

# Choose whether to train a new model or load the pre-trained model

skip_training = True

# In[21]:

# Compile and train the model, or load pre-trained weights

if not skip_training:
    checkpoint_callback=tf.keras.callbacks.ModelCheckpoint(filepath='./test_data_folder/shakespeare/models/ckpt',
                                                           save_weights_only=True,
                                                           save_best_only=True)
    model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['sparse_categorical_accuracy'])
    history = model.fit(train_data, epochs=15, validation_data=valid_data,
                        validation_steps=50, callbacks=[checkpoint_callback])

# In[22]:

# Save model history as a json file, or load it if using pre-trained weights

if not skip_training:
    history_dict = dict()
    for k, v in history.history.items():
        history_dict[k] = [float(val) for val in history.history[k]]
    with open('test_data_folder/shakespeare/models/history.json', 'w+') as json_file:
        json.dump(history_dict, json_file, sort_keys=True, indent=4)
else:
    with open('test_data_folder/shakespeare/models/history.json', 'r') as json_file:
        history_dict = json.load(json_file)

# #### Plot the learning curves

# In[23]:

# Run this cell to plot accuracy vs epoch and loss vs epoch

plt.figure(figsize=(15,5))
plt.subplot(121)
plt.plot(history_dict['sparse_categorical_accuracy'])
plt.plot(history_dict['val_sparse_categorical_accuracy'])
plt.title('Accuracy vs. epochs')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.xticks(np.arange(len(history_dict['sparse_categorical_accuracy'])))
ax = plt.gca()
ax.set_xticklabels(1 + np.arange(len(history_dict['sparse_categorical_accuracy'])))
plt.legend(['Training', 'Validation'], loc='lower right')

plt.subplot(122)
plt.plot(history_dict['loss'])
plt.plot(history_dict['val_loss'])
plt.title('Loss vs. epochs')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.xticks(np.arange(len(history_dict['sparse_categorical_accuracy'])))
ax = plt.gca()
ax.set_xticklabels(1 + np.arange(len(history_dict['sparse_categorical_accuracy'])))
plt.legend(['Training', 'Validation'], loc='upper right')
plt.show()

# #### Write a text generation algorithm
#
# You can now use the model to generate text! In order to generate a single text sequence, the model needs to be rebuilt with a batch size of 1.

# In[24]:

# Re-build the model and load the saved weights

model = get_model(len(tokenizer.word_index) + 1, batch_size=1)
model.load_weights(tf.train.latest_checkpoint('./test_data_folder/shakespeare/models/'))

# An algorithm to generate text is as follows:
#
# 1. Specify a seed string (e.g. `'ROMEO:'`) to get the network started, and a define number of characters for the model to generate, `num_generation_steps`.
# 2. Tokenize this sentence to obtain a list containing one list of the integer tokens.
# 3. Reset the initial state of the network.
# 4. Convert the token list into a Tensor (or numpy array) and pass it to your model as a batch of size one.
# 5. Get the model prediction (logits) for the last time step and extract the state of the recurrent layer.
# 6. Use the logits to construct a categorical distribution and sample a token from it.
# 7. Repeat the following for `num_generation_steps - 1` steps:
#
#     1. Use the saved state of the recurrent layer and the last sampled token to get new logit predictions
#     2. Use the logits to construct a new categorical distribution and sample a token from it.
#     3. Save the updated state of the recurrent layer.
#
# 8. Take the final list of tokens and convert to text using the Tokenizer.
#
# Note that the internal state of the recurrent layer can be accessed using the `states` property. For the GRU layer, it is a list of one variable:

# In[25]:

# Inspect the model's current recurrent state

model.layers[1].states

# We will break the algorithm down into two steps. First, you should now complete the following function that takes a sequence of tokens of any length and returns the model's prediction (the logits) for the last time step. The specification is as follows:
#
# * The token sequence will be a python list, containing one list of integer tokens, e.g. `[[1, 2, 3, 4]]`
# * The function should convert the list into a 2D Tensor or numpy array
# * If the function argument `initial_state` is `None`, then the function should reset the state of the recurrent layer to zeros.
# * Otherwise, if the function argument `initial_state` is a 2D Tensor or numpy array, assign the value of the internal state of the GRU layer to this argument.
# * Get the model's prediction (logits) for the last time step only.
#
# The function should then return the logits as a 2D numpy array, where the first dimension is equal to 1 (batch size).
#
# **Hint:** the internal state of the recurrent can be reset to zeros using the `reset_states` method.

# In[26]:

#### GRADED CELL ####

# Complete the following function.
# Make sure not to change the function name or arguments.

def get_logits(model, token_sequence, initial_state=None):
    """
    This function takes a model object, a token sequence and an optional initial
    state for the recurrent layer. The function should return the logits prediction
    for the final time step as a 2D numpy array.
    """
    token_sequence_input = tf.convert_to_tensor(token_sequence, dtype=tf.float32)

    if initial_state is None:
        model.layers[1].reset_states()
    else:
        model.layers[1].reset_states(initial_state)

    predict = model.predict(token_sequence_input)

    output = predict[:,-1,:]

    return output

# In[27]:

# Test the get_logits function by passing a dummy token sequence

dummy_initial_state = tf.random.normal(model.layers[1].states[0].shape)
get_logits(model, [[1,2,3,4]], initial_state=dummy_initial_state)

# You should now write a function that takes a logits prediction similar to the above, uses it to create a categorical distribution, and samples a token from this distribution. The following function takes a 2D numpy array `logits` as an argument, and should return a single integer prediction that is sampled from the categorical distribution.
#
# **Hint:** you might find the `tf.random.categorical` function useful for this; see the documentation [here](https://www.tensorflow.org/api_docs/python/tf/random/categorical).

# In[28]:

#### GRADED CELL ####

# Complete the following function.
# Make sure not to change the function name or arguments.

def sample_token(logits):
    """
    This function takes a 2D numpy array as an input, and constructs a
    categorical distribution using it. It should then sample from this
    distribution and return the sample as a single integer.
    """
    sample = tf.random.categorical(logits, num_samples=1)[0,0].numpy()
    return sample

# In[29]:

# Test the sample_token function by passing dummy logits

dummy_initial_state = tf.random.normal(model.layers[1].states[0].shape)
dummy_logits = get_logits(model, [[1, 2, 3, 4]], initial_state=dummy_initial_state)
sample_token(dummy_logits)

# In[30]:

logits_size = dummy_logits.shape[1]
dummy_logits = -np.inf*np.ones((1, logits_size))
dummy_logits[0, 20] = 0
sample_token(dummy_logits)
random_inx = np.random.choice(logits_size, 2, replace=False)
random_inx1, random_inx2 = random_inx[0], random_inx[1]
print(random_inx1, random_inx2)
dummy_logits = -np.inf*np.ones((1, logits_size))
dummy_logits[0, random_inx1] = 0
dummy_logits[0, random_inx2] = 0
sampled_token = []
for _ in range(100):
    sampled_token.append(sample_token(dummy_logits))

l_tokens, l_counts = np.unique(np.array(sampled_token), return_counts=True)
len(l_tokens) == 2

# #### Generate text from the model
#
# You are now ready to generate text from the model!

# In[31]:

# Create a seed string and number of generation steps

init_string = 'ROMEO:'
num_generation_steps = 1000

# In[32]:

# Use the model to generate a token sequence

token_sequence = tokenizer.texts_to_sequences([init_string])
initial_state = None
input_sequence = token_sequence

for _ in range(num_generation_steps):
    logits = get_logits(model, input_sequence, initial_state=initial_state)
    sampled_token = sample_token(logits)
    token_sequence[0].append(sampled_token)
    input_sequence = [[sampled_token]]
    initial_state = model.layers[1].states[0].numpy()

print(tokenizer.sequences_to_texts(token_sequence)[0][::2])

# Congratulations for completing this programming assignment! In the next week of the course we will see how to build customised models and layers, and make custom training loops.

# In[ ]:

04_Recurrent_neural_nets/Project_RNN_Language model for the Shakespeare dataset.py


# Advent of code 2021
# Day 2

def get_data():
    with open("day_2_data.txt") as f:
        instructions = [line.split() for line in f]

    return instructions

def part1(instructions):
    horizonatal = 0
    depth = 0

    for ins in instructions:
        cmd = ins[0]
        amount = int(ins[1])

        if cmd == "forward":
            horizonatal += amount
        elif cmd == "up":
            depth -= amount
        elif cmd == "down":
            depth += amount

    print("Part1:", horizonatal*depth)

def part2(instructions):
    horizonatal = 0
    depth = 0
    aim = 0

    for ins in instructions:
        cmd = ins[0]
        amount = int(ins[1])

        if cmd == "forward":
            horizonatal += amount
            depth += (aim * amount)
        elif cmd == "up":
            aim -= amount
        elif cmd == "down":
            aim += amount

    print("Part2:", horizonatal*depth)

instructions = get_data()
part1(instructions)
part2(instructions)


# 069 Análise de dados do grupo
# Crie um programa que leia a idade e o sexo de várias pessoas. A cada pessoa cadastrada, o programa deverá perguntar se o usuário
# quer ou não continuar. No final, mostre:
# a) Quantas pessoas tem mais de 18 anos
# b) Quantos homens foram cadastrados
# c) Quantas mulheres tem menos de 20 anos

maior = 0
homens = 0
mulher = 0
while True:
    idade = int(input('Digite a idade: '))
    sexo = ' '
    while sexo not in 'MF':
        sexo = str(input('Digite seu sexo com F ou M: ')).strip().upper()[0]
    cont = ' '
    while cont not in 'SN':
        cont = str(input('Você deseja continuar? [S/N] ')).strip().upper()[0]
    if idade >= 18:
        maior += 1
    if sexo == 'M':
        homens += 1
    if sexo == 'F' and idade >= 20:
        mulher += 1
    if cont == 'N':
        break
print('Cadastro finalizado!')
print(f'O número de pessoas maiores de 18 anos é {maior}.')
print(f'Foram cadastrados {homens} homens.')
print(f'Há {mulher} mulheres maiores de 18 anos.')


"""
Notifier handlers.
Some send emails, some insert 'notification' objects in TN, etc.

We use classes instead of functions to allow inheritance between handlers.

Don't forget to connect to your signals with 'weak = False' !!
"""
import logging
import traceback
import re

from django.conf import settings
from django.template import Context
from django.core.mail import EmailMessage, EmailMultiAlternatives
from django.core.cache import cache
from django.template.loader import get_template
from django.contrib.sites.models import Site
from django.contrib.auth.models import User
from django.db.models.query import QuerySet

from twistranet.twistapp.lib.log import log
from twistranet.twistapp.lib import utils

SUBJECT_REGEX = re.compile(r"^[\s]*Subject:[ \t]?([^\n$]*)\n", re.IGNORECASE | re.DOTALL)
EMPTY_LINE_REGEX = re.compile(r"\n\n+", re.DOTALL)

class NotifierHandler(object):
    def __call__(self, sender, **kwargs):
        """
        This is where our handlers get called.
        Always call your parent first to populate your class instance
        and check various stuff on it.s
        """

class LogHandler(NotifierHandler):
    '''
    Just prints a log message for what it's been called for.
    '''
    def __init__(self, level = logging.DEBUG):
        self.level = level

    def __call__(self, sender, **kwargs):
        log.log(self.level, "%s %s" % (sender, kwargs))

class NotificationHandler(NotifierHandler):
    """
    This adds a Notification object just right into your twistranet site.
    It is somewhat internationalized.

    You can customize the displayed description upon class creation: the message
    will be _'ed with %(xxx)s values filled from the parameters dictionnary.
    """
    def __init__(self, owner_arg, publisher_arg, message, permissions = "public"):
        """
        Store the message for future use.
        owner_arg and publisher_arg will be used to create the underlying content.
        owner_arg defaults to SystemAccount
        publisher_arg defaults to owner arg's publisher.
        """
        self.owner_arg = owner_arg
        self.publisher_arg = publisher_arg
        self.message = message
        self.permissions = permissions

    def __call__(self, sender, **kwargs):
        """
        We add the Notification object on behalf of SystemAccount.
        """
        from twistranet.twistapp.models import Twistable
        from twistranet.twistapp.models import SystemAccount
        from twistranet.notifier.models import Notification

        # Prepare the message dict.
        # We use title_or_description on each Twistable argument to display it.
        message_dict = {}
        for param, value in kwargs.items():
            if isinstance(value, Twistable):
                message_dict[param] = value.id

        # We force __account__ to fake user login.
        system = SystemAccount.get()
        __account__ = system
        owner = kwargs.get(self.owner_arg, system)
        publisher = kwargs.get(self.publisher_arg, owner.publisher)
        n = Notification(
            publisher = publisher,
            owner = owner,
            title = "",
            description = self.message,
            parameters = message_dict,
            permissions = self.permissions,
        )
        n.save()

class MailHandler(NotifierHandler):
    """
    This is a notifier which sends an email on behalf of TwistraNet.
    So you can define recipient (but not sender, it's set system-wide),
    subject and message.

    Nota: recipient_arg must be an Account.
    If it's a community, it will send a message to all members,
    except if the managers_only bool is True (then it's sent to managers only).

    A text_template and html_template can be provided. They are paths to templates
    (eg. 'email/welcome.txt' and 'email/welcome.html').
    Text template is mandatory, BTW.

    The TEXT (and only text) template _can_ provide a Subject: xxx line AS ITS VERY FIRST LINE line.
    But the 'subject' parameter always superseeds this.
    This way, you can use 'Subject:' in your templates to have your subject use variables and translation.

    One last thing: a signal can overload the templates. Just pass 'text_template' and/or 'html_template' in
    the signal kw arguments to have the templates overloaded for that particular signal instance.
    """
    def __init__(self, recipient_arg, text_template, subject = None, html_template = None, managers_only = False):
        self.recipient_arg = recipient_arg
        self.subject = subject
        self.text_template = text_template
        self.html_template = html_template
        self.managers_only = managers_only

    def __call__(self, sender, **kwargs):
        """
        Generate the message itself.
        XXX TODO: Handle translation correctly (not from the request only)
        """
        # Fake-Login with SystemAccount so that everybody can be notified,
        # even users this current user can't list.
        from twistranet.twistapp.models import SystemAccount, Account, UserAccount, Community, Twistable
        __account__ = SystemAccount.get()
        from_email = settings.SERVER_EMAIL
        host = settings.EMAIL_HOST
        if not host:
            # If host is disabled (EMAIL_HOST is None), skip that
            return

        # Handle recipients emails
        recipients = kwargs.get(self.recipient_arg, None)
        if not recipients:
            raise ValueError("Recipient must be provided as a '%s' parameter" % self.recipient_arg)
        to_list = []
        if not isinstance(recipients, (list, tuple, QuerySet, )):
            recipients = (recipients, )
        for recipient in recipients:
            if isinstance(recipient, Twistable):
                recipient = recipient.object
            if isinstance(recipient, UserAccount):
                to = recipient.email
                if not to:
                    log.warning("Can't send email for '%s': %s doesn't have an email registered." % (sender, recipient, ))
                    return
                to_list.append(to)
            elif isinstance(recipient, Community):
                if self.managers_only:
                    members = recipient.managers
                else:
                    members = recipient.members
                # XXX Suboptimal for very large communities
                to_list.extend([ member.email for member in members if member.email ])
            elif type(recipient) in (str, unicode, ):
                to_list.append(recipient)        # XXX Todo: check the '@'
            else:
                raise ValueError("Invalid recipient: %s (%s)" % (recipient, type(recipient), ))

        # Fetch templates
        text_template = kwargs.get('text_template', self.text_template)
        html_template = kwargs.get('html_template', self.html_template)

        # Now generate template and send mail for each recipient
        # XXX TODO: See http://docs.djangoproject.com/en/1.2/topics/email/#sending-multiple-e-mails
        # for the good approach to use.
        for to in to_list:
            # Append domain (and site info) to kwargs
            d = kwargs.copy()
            domain = cache.get("twistranet_site_domain")
            d.update({
                "domain":       domain,
                "site_name":    utils.get_site_name(),
                "baseline":     utils.get_baseline(),
                "recipient":    to,     # A string
            })

            # Load both templates and render them with kwargs context
            text_tpl = get_template(text_template)
            c = Context(d)
            text_content = text_tpl.render(c).strip()
            if html_template:
                html_tpl = get_template(html_template)
                html_content = html_tpl.render(c)
            else:
                html_content = None

            # Fetch back subject from text template
            subject = self.subject
            if not subject:
                match = SUBJECT_REGEX.search(text_content)
                if match:
                    subject = match.groups()[0]
            if not subject:
                raise ValueError("No subject provided nor 'Subject:' first line in your text template")

            # Remove empty lines and "Subject:" line from text templates
            text_content = SUBJECT_REGEX.sub('', text_content)
            text_content = EMPTY_LINE_REGEX.sub('\n', text_content)

            # Prepare messages
            msg = EmailMultiAlternatives(subject, text_content, from_email, [ to ], )
            if html_content:
                msg.attach_alternative(html_content, "text/html")

            # Send safely
            try:
                log.debug("Sending mail: '%s' from '%s' to '%s'" % (subject, from_email, to))
                msg.send()
            except:
                log.warning("Unable to send message to %s" % to)
                log.exception("Here's what we've got as an error.")


import pandas as pd
from pathlib import Path
from drctrl.plugins.base import BaseOutput
import inspect

class OutputFile(BaseOutput):
    def __init__(self, path, filename,
            delimiter=',', index=False, escapechar='\\', **kwargs):
        self.path = Path(path)
        self.filename       = Path(filename)

        params = {}
        params['sep']       = delimiter
        params['index']     = index
        args = inspect.getargspec(pd.DataFrame.to_csv).args
        for k, v in kwargs.items():
            if k in args:
                params[k] = v

        self.params = params

    def preprocess(self):
        pass

    def output(self, df):
        df.to_csv(self.path.joinpath(self.filename), **self.params)


"""empty message

Revision ID: a20f10061159
Revises: c201a831dd65
Create Date: 2019-03-23 17:42:22.862139

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = 'a20f10061159'
down_revision = 'c201a831dd65'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('comment', sa.Column('content', sa.Text(), nullable=False))
    op.drop_column('comment', 'contnet')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('comment', sa.Column('contnet', mysql.TEXT(), nullable=False))
    op.drop_column('comment', 'content')
    # ### end Alembic commands ###


from collections import deque
import sys
input = sys.stdin.readline

ans =0
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
n,m = map(int, input().split())
arr = list(list(map(int, input().split())) for _ in range(n))
visit = [[0] * m for  _ in range(n)]

locate = []
for i in range(m):
    a, b = map(int ,input().split())
    locate.append((a-1,b-1))
    if i == 0:
        p = (a-1, b-1)
        visit[a-1][b-1] = 1

def dfs(x,y,t):
    global ans
    if (x,y) == locate[t]:
        if t == m-1:
            ans += 1
            return
        t += 1
    for k in range(4):
        nx = x + dx[k]
        ny = y + dy[k]
        if 0<= nx < n and 0 <= ny < n and arr[nx][ny] == 0 and visit[nx][ny] == 0:
            visit[nx][ny] = 1
            dfs(nx, ny, t)
            visit[nx][ny] = 0

dfs(locate[0][0], locate[0][1], 0)
print(ans)


import datetime

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

def parse_log_file(log_file):
    with open(log_file_name, 'r') as f:
        log = f.read()

    lines = log.splitlines()

    if lines[0].startswith('END'):
        lines = lines[2:]

    start_lines = [line for line in lines if line.startswith('START')]
    end_lines = [line for line in lines if line.startswith('END')]
    starts = [datetime.datetime.fromisoformat(line[7:]) for line in start_lines]
    ends = [datetime.datetime.fromisoformat(line[5:]) for line in end_lines]

    start_intervals = []
    for i in range(1, len(starts)):
        start_intervals.append((starts[i] - starts[i - 1]).seconds / 60)

    durations = []
    for i in range(len(starts)):
        durations.append((ends[i] - starts[i]).seconds)

    return np.array(starts), np.array(start_intervals), np.array(durations)

log_file_name = 'log.txt'
starts, start_intervals, durations = parse_log_file(log_file_name)

fig, ax = plt.subplots(constrained_layout=True)
ax.plot(starts[1:], start_intervals, color='blue')
ax.set_ylabel('intervals [minutes]', color='blue')
locator = mdates.AutoDateLocator(minticks=3, maxticks=10)
formatter = mdates.ConciseDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
ax.set_ylim(bottom=0)
ax2 = ax.twinx()
ax2.plot(starts, durations, color='red')
ax2.set_ylabel('durations [seconds]', color='red')
ax2.set_ylim(bottom=0)

plt.savefig('plot.png')
plt.show()


class Solution:
    ### 20. Valid Parentheses ###
    # @param {string} s
    # @return {boolean}
    def isValid(self, s):
        if not s: return True

        left = ''
        for _ in s:
            if left and left[-1]+_  in ('()', '[]', '{}'): left=left[:-1]
            elif _ in '([{': left += _
            else: return False

        return not left

    ### 21. Merge Two Sorted Lists ###
    # @param {ListNode} l1
    # @param {ListNode} l2
    # @return {ListNode}
    def mergeTwoLists(self, l1, l2):
        if l1 and l2:
            if l1.val > l2.val:
                l1, l2 = l2, l1
            l1.next = self.mergeTwoLists(l1.next, l2)

        return l1 or l2

    ### 22. Generate Parentheses ###
    # @param {integer} n
    # @return {string[]}
    def generateParenthesis(self, n):
        if n == 0: return []

        parentheses = []
        def dfs(left, right, cur):
            if not left:
                parentheses.append(cur+')'*right)
                return
            if right: dfs(left,right-1,cur+')')
            dfs(left-1, right+1, cur+'(')
            return

        dfs(n,0,'')
        return parentheses

    ### 23. Merge k Sorted Lists ###
    # @param @param {ListNode[]} lists
    # @return {ListNode}
    def mergeKLists(self, lists):
        if not lists: return None

        nodes = []
        for cur in lists:
            while cur:
                nodes.append(cur)
                cur = cur.next
        if not nodes: return None
        nodes.sort(key=lambda x: x.val)
        tail = None
        while nodes:
            head = nodes.pop()
            head.next = tail
            tail = head

        return head

    ### 24. Swap Nodes in Pairs ###
    # @param {ListNode} head
    # @return {ListNode}
    def swapPairs(self, head):
        if not head or not head.next: return head

        dummy, nxt = head.next, head.next.next
        dummy.next = head
        head.next = self.swapPairs(nxt)

        return dummy

    ### 25. Reverse Nodes in k-Group ###
    # @param {ListNode} head
    # @param {integer} k
    # @return {ListNode}
    def reverseKGroup(self, head, k):
        if not head or k == 0 or k == 1: return head
        cnt, tail = 1, head
        while cnt < k:
            if not tail.next: return head
            tail = tail.next
            cnt += 1

        cnt, dummy, tail = 1, None, head
        while cnt <= k:
            tmp = head.next
            head.next = dummy
            dummy = head
            head = tmp
            cnt += 1

        tail.next = self.reverseKGroup(head, k)
        return dummy

    ### 26. Remove Duplicates from Sorted Array ###
    # @param {integer[]} nums
    # @return {integer}
    def removeDuplicates(self, nums):
        if not nums: return 0

        tail = 0
        for i in xrange(1,len(nums)):
            if nums[i] != nums[tail]:
                tail += 1
                nums[tail] = nums[i]
        nums = nums[:tail+1]
        return tail+1

    ### 27. Remove Element ###
    # @param {integer[]} nums
    # @param {integer} val
    # @return {integer}
    def removeElement(self, nums, val):
        if not nums: return 0

        l, r = 0, len(nums)-1
        while True:
            while l <= r and nums[l] != val: l += 1
            while l <= r and nums[r] == val: r -= 1
            if l > r: break
            nums[l], nums[r] = nums[r], nums[l]

        return r+1

    ### 28. Implement strStr() ###
    # @param {string} haystack
    # @param {string} needle
    # @return {inetger}
    def strStr(self, haystack, needle):
        return haystack.find(needle)

    ### 29. Divide Two Integers ###
    # @param {integer} dividend
    # @param {integer} divisor
    # @return {integer}
    def divide(self, dividend, divisor):
        INT_MAX = 2147483647
        INT_MIN = -2147483648
        if divisor == 0 or (dividend == INT_MIN and divisor == -1): return INT_MAX
        if dividend == 0: return 0

        sign = 1 if (dividend > 0) is (divisor > 0) else -1
        dividend, divisor, res = abs(dividend), abs(divisor), 0
        for i in xrange(31,-1,-1):
            if (dividend >> i) >= divisor:
                res |= 1 << i
                dividend -= divisor << i

        return res*sign


from multiprocessing import Process
import pyaudio
import wave
import numpy as np
import threading

# 음악 파일 경로
# music_file = "/home/codingbotpark/singing_supporter/musics/들리나요..._UC_pwIXKXNm5KGhdEVzmY60A/accompaniment.wav"
# vocal_file = "/home/codingbotpark/singing_supporter/musics/들리나요..._UC_pwIXKXNm5KGhdEVzmY60A/vocals.wav"

# 오디오 설정
chunk = 1024  # 버퍼 크기
format = pyaudio.paInt16  # 샘플 형식
channels = 2  # 채널 수
rate = 44100  # 샘플링 레이트

class PlayMusic(threading.Thread):
    # chunk = 1024  # 버퍼 크기
    # format = pyaudio.paInt16  # 샘플 형식
    # channels = 2  # 채널 수
    # rate = 44100  # 샘플링 레이트
    def __init__(self,musciFilePath):
        super().__init__()
        self.musciFilePath = musciFilePath
    def run(self):
        wf = wave.open(self.musciFilePath, 'rb')

        p = pyaudio.PyAudio()
        stream = p.open(format=format,
                        channels=channels,
                        rate=rate,
                        output=True)

        data = wf.readframes(chunk)

        while data:
            stream.write(data)
            data = wf.readframes(chunk)

        stream.stop_stream()
        stream.close()
        p.terminate()

class MicInput(threading.Thread):
    # chunk = 1024  # 버퍼 크기
    # format = pyaudio.paInt16  # 샘플 형식
    # channels = 2  # 채널 수
    # rate = 44100  # 샘플링 레이트
    def __init__(self,vocalFile):
        super().__init__()
        self.vocalFile = vocalFile
    def run(self):
        wf = wave.open(self.vocalFile, 'rb')

        p = pyaudio.PyAudio()
        vocal_stream = p.open(format=format,
                        channels=channels,
                        rate=rate,
                        output=True,
                        frames_per_buffer=chunk)

        mic_stream = p.open(format=format,
                        channels=channels,
                        rate=rate,
                        input=True)

        vocalData = wf.readframes(chunk)
        while vocalData:
            micData = mic_stream.read(chunk)
            audio = np.frombuffer(micData,dtype=np.int16)
            amplitude = np.max(np.max(np.abs(audio)))
            print("마이크 진폭:", amplitude)

            # 3000 이상일 땐 말하는것
            if (amplitude > 3000):
                vocal_stream.write(micData)
            else:
                vocal_stream.write(vocalData)

            vocalData = wf.readframes(chunk)

        vocal_stream.stop_stream()
        vocal_stream.close()
        mic_stream.stop_stream()
        mic_stream.close()
        p.terminate()

# def execute(musicFile,vocalFile):
#     # print(musicFile,vocalFile)

#     playMusicThread = PlayMusic(musicFile)
#     micInputThread = MicInput(vocalFile)

#     micInputThread.start();
#     playMusicThread.start();

#     playMusicThread.join()
#     micInputThread.join()

# execute(music_file,vocal_file)
# execute("/home/codingbotpark/singing_supporter/musics/들리나요..._UC_pwIXKXNm5KGhdEVzmY60A/accompaniment.wav","/home/codingbotpark/singing_supporter/musics/들리나요..._UC_pwIXKXNm5KGhdEVzmY60A/vocals.wav")


import math
from operator import add, mul

from titus.fcn import Fcn
from titus.fcn import LibFcn
from titus.signature import Sig
from titus.signature import Sigs
from titus.datatype import *
from titus.errors import *
from titus.util import callfcn, div
import titus.P as P

provides = {}
def provide(fcn):
    provides[fcn.name] = fcn

prefix = "model.neural."

####################################################################
def matrix_vector_mult(a, b):
   return [[sum(x * b[i] for i, x in enumerate(row))][0] for row in a]

class SimpleLayers(LibFcn):
    name = prefix + "simpleLayers"
    sig = Sig([
           {"datum" : P.Array(P.Double())},
           {"model":  P.Array(P.WildRecord("M",
                      {"weights": P.Array(P.Array(P.Double())),
                       "bias"   : P.Array(P.Double())}))},
           {"activation": P.Fcn([P.Double()], P.Double())}],
               P.Array(P.Double()))
    errcodeBase = 11000
    def __call__(self, state, scope, pos, paramTypes, datum, model, activation):
        if len(model) == 0:
            raise PFARuntimeException("no layers", self.errcodeBase + 0, self.name, pos)
        # pass datum through first N - 1 layers, apply activation
        for layer in model[:-1]:
            bias = layer["bias"]
            weights = layer["weights"]
            if (len(bias) != len(weights)) or any(len(x) != len(datum) for x in weights):
                raise PFARuntimeException("weights, bias, or datum misaligned", self.errcodeBase + 1, self.name, pos)
            tmp = [sum(y) for y in zip(matrix_vector_mult(weights, datum), bias)]
            datum = [callfcn(state, scope, activation, [i]) for i in tmp]
        # pass datum through final layer, don't apply activation
        bias = model[-1]["bias"]
        weights = model[-1]["weights"]
        if (len(bias) != len(weights)) or any(len(x) != len(datum) for x in weights):
            raise PFARuntimeException("weights, bias, or datum misaligned", self.errcodeBase + 1, self.name, pos)
        return [sum(y) for y in zip(matrix_vector_mult(weights, datum), bias)]
provide(SimpleLayers())


import game
import player

px = player.Player('x')
po = player.Player('o')

game_ends = False

for i in range(100000):
    env = game.Game(3)
    px.state_history = []
    po.state_history = []
    while not game_ends:

        px.take_action(env, 0.05, True)
        if env.check_winner(px.symbol) or len(env.get_allowed_moves()[0]) == 0:
            game_ends = True
            break

        env.draw_board()

        po.take_action(env, 0.05, True)
        if env.check_winner(po.symbol) or len(env.get_allowed_moves()[0]) == 0:
            game_ends = True
            break

        env.draw_board()

    env.draw_board()

    if env.check_winner(px.symbol):
        print("px won!")

    if env.check_winner(po.symbol):
        print("po won!")

    game_ends = False

    px.update(env)
    po.update(env)
    env = None
    '''
    px_symbol = px.symbol
    po_symbol = po.symbol

    px.symbol = po_symbol
    po.symbol = px_symbol
    '''

while True:
    env = game.Game(3)
    px.state_history = []
    px.symbol= 'x'
    while not game_ends or not len(env.get_allowed_moves()) > 0:

        px.take_action(env, 0.0, True)
        if env.check_winner(px.symbol) or len(env.get_allowed_moves()) == 0:
            game_ends = True
            break

        env.draw_board()

        num1, num2 = map(int, input().split())

        env.make_move('o', (num1, num2))

        if env.check_winner('o') or len(env.get_allowed_moves()) == 0:
            game_ends = True
            break

        env.draw_board()

    env.draw_board()
    game_ends = False


from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from datetime import datetime, timedelta
import pandas as pd
import gspread
import yfinance as yf
from gspread_dataframe import set_with_dataframe
from airflow.operators.email_operator import EmailOperator
from forex_python.converter import CurrencyRates
import matplotlib.pyplot as plt
import pytz

Sheet_Name = 'My Investment'
Market_sheet = 'Market Value'
Investment_sheet = 'Investment'

gc = gspread.service_account(filename='creds/myinvestment-403711-f60632e19249.json')
sh = gc.open(Sheet_Name)

def Extract(**kw):
    Market_value_Worksheet = sh.worksheet(Market_sheet)
    Market_df = pd.DataFrame(Market_value_Worksheet.get_all_records())
    Investment_Worksheet = sh.worksheet(Investment_sheet)
    Investment_df = pd.DataFrame(Investment_Worksheet.get_all_records())

    return Market_df, Investment_df

def Transformation(**kw):
    ti = kw['ti']
    Market_df = ti.xcom_pull(task_ids='Extract', key=None)[0]
    Investment_df = ti.xcom_pull(task_ids='Extract', key=None)[1]

    Market_df = pd.DataFrame(Market_df)
    Investment_df = pd.DataFrame(Investment_df)

    for i, row in Market_df.iterrows():
        if row['Fund_ID'] == 'Exchange':
            Dollar = row['MKT Price']

    Investment_df['Total Qty'] = 0.0
    for fund_id, group in Investment_df.groupby('Fund_ID'):
        for i, row in group.iterrows():
            if row['Category'] == 'Invested':
                if i == group.index[0]:
                   Investment_df.loc[i, 'Total Qty'] = row['Qty']
                else:
                    Investment_df.loc[i, 'Total Qty'] = round(Investment_df.loc[i-1, 'Total Qty'], 3) + row['Qty']
            else:
                Investment_df.loc[i, 'Total Qty'] = round(Investment_df.loc[i-1, 'Total Qty'], 3) - row['Qty']

    Investment_df['Net Amount'] = 0
    Investment_df['Status'] = 1
    for fund_id, group in Investment_df.groupby('Fund_ID'):
        for i, row in group.iterrows():
            if row['Category'] == 'Invested':
                if i == group.index[0]:
                    Investment_df.loc[i,'Net Amount'] = row['Amount']
                else:
                    Investment_df.loc[i,'Net Amount'] = Investment_df.loc[i-1,'Net Amount'] + row['Amount']
                    Investment_df.loc[i-1,'Status'] = 0

            else:
                if row['Category'] == 'Redeemed' or row['Total Qty'] > 0:
                    Investment_df.loc[i,'Net Amount'] = (Investment_df.loc[i-1,'Net Amount']/Investment_df.loc[i-1,'Total Qty'])*row['Total Qty']
                    Investment_df.loc[i-1,'Status'] = 0
                else:
                    Investment_df.loc[i,'Net Amount'] = 0
                    Investment_df.loc[i,'Status'] = 0

    Active_Investment = Investment_df.loc[Investment_df['Status']==1]

    Merge_df = pd.merge(Market_df, Active_Investment, on=['Fund_ID', 'Fund Name'], how='inner')
    Merge_df['Current Value'] = Merge_df['MKT Price'] * Merge_df['Total Qty']
    Merge_df['Profit'] = Merge_df['Current Value'] - Merge_df['Net Amount']
    Merge_df['Profit %'] = ((Merge_df['Current Value'] / Merge_df['Net Amount']) * 100) - 100

    Active_Investment.to_csv('./active_investment.csv', index=False)
    Investment_df.to_csv('./investment.csv', index=False)
    Market_df.to_csv('./market.csv', index=False)
    Merge_df.to_csv('./merged.csv', index=False)

    return Merge_df, Dollar

def Load(**kw):
    ti = kw['ti']
    merge_df = ti.xcom_pull(task_ids='Transformation', key=None)[0]
    merge_df = pd.DataFrame(merge_df)

    ist = pytz.timezone('Asia/Kolkata')
    current_time_ist = datetime.now(ist)

    current_date = current_time_ist.strftime('%Y-%m-%d')

    for index, row in merge_df.iterrows():
        Fund_ID = row['Fund_ID']
        US_IND = row['US/IND']
        Date = current_date
        InvestedAmount = row['Net Amount']
        TotalValue = row['Current Value']
        Exchange = row['Exchange Rate']
        Tax = row['Tax']
        Profit = row['Profit']
        Profit_pec = row['Profit %']

        append_list = [Fund_ID, US_IND, Date, InvestedAmount, TotalValue,  Exchange, Tax, Profit, Profit_pec]

        Investment_Growth = sh.worksheet('Finance DWH')
        Investment_Growth.append_row(append_list)

def Report(**kw):
    ti = kw['ti']
    FinanceDWH = sh.worksheet('Finance DWH')
    FinanceDWH = pd.DataFrame(FinanceDWH.get_all_records())
    Dollar = ti.xcom_pull(task_ids='Transformation', key=None)[1]

    ist = pytz.timezone('Asia/Kolkata')
    current_time_ist = datetime.now(ist)

    Today = current_time_ist.strftime('%Y-%m-%d')

    Daily_data = FinanceDWH.loc[FinanceDWH['Date'] == Today]
    Net_Invested = 0
    US_Investment = 0
    US_Net_Investment = 0
    Net_Total_Value = 0
    US_Total_Value = 0
    US_Net_Total_Value = 0
    IND_Net_Total_Value = 0
    IND_Net_Investment = 0

    for i, row in Daily_data.iterrows():
        if row['US_IND'] == 'IND':
            Net_Invested = row['Invested Amount'] + Net_Invested
            IND_Net_Investment = row['Invested Amount'] + IND_Net_Investment
            Net_Total_Value = row['Total Value'] + Net_Total_Value
            IND_Net_Total_Value = row['Total Value'] + IND_Net_Total_Value

        else:
            US_Investment = row['Invested Amount']*row['Exchange']
            Net_Invested = US_Investment + Net_Invested
            US_Net_Investment = US_Investment + US_Net_Investment
            US_Total_Value = row['Total Value']*Dollar
            Net_Total_Value = US_Total_Value + Net_Total_Value
            US_Net_Total_Value = US_Total_Value + US_Net_Total_Value

    Net_Profit = Net_Total_Value-Net_Invested
    US_Profit = US_Net_Total_Value-US_Net_Investment
    IND_Profit = IND_Net_Total_Value - IND_Net_Investment

    Ind_append_list = ['IND',Today,IND_Net_Investment, IND_Net_Total_Value, IND_Profit]
    us_append_list = ['US',Today,US_Net_Investment, US_Net_Total_Value, US_Profit]

    IND_US_Growth = sh.worksheet('IND&US Growth')
    IND_US_Growth.append_row(Ind_append_list)
    IND_US_Growth.append_row(us_append_list)

    def US_IND_Investment_Plot():
        labels = ['US', 'IND']
        value = [US_Net_Investment, IND_Net_Investment]

        plt.figure(figsize=(6, 6))
        # Custom colors for the pie chart
        colors = ['#ff9999', '#66b3ff']
        # Create a pie chart with custom colors
        fig, ax = plt.subplots()
        ax.pie(value, labels=labels, autopct='%1.1f%%', startangle=90, colors=colors)
        # Equal aspect ratio ensures that the pie is drawn as a circle
        ax.axis('equal')
        centre_circle = plt.Circle((0, 0), 0.50, fc='white')
        fig = plt.gcf()
        # Adding Circle in Pie chart
        fig.gca().add_artist(centre_circle)
        # Add a title
        plt.title('Investment Distribution')
        plt.savefig('./donet.png')

    US_IND_Investment_Plot()

    return Net_Invested, Net_Total_Value, Net_Profit, US_Net_Investment, US_Net_Total_Value, US_Profit, IND_Net_Investment, IND_Net_Total_Value, IND_Profit

def send_email(**kw):
    ist = pytz.timezone('Asia/Kolkata')
    current_time_ist = datetime.now(ist)
    current_date = current_time_ist.strftime('%Y-%m-%d')

    ti = kw['ti']

    Net_Invested = ti.xcom_pull(task_ids='Report', key=None)[0]
    Net_Total_Value = ti.xcom_pull(task_ids='Report', key=None)[1]
    Net_Profit = ti.xcom_pull(task_ids='Report', key=None)[2]
    US_Net_Investment = ti.xcom_pull(task_ids='Report', key=None)[3]
    US_Net_Total_Value = ti.xcom_pull(task_ids='Report', key=None)[4]
    US_Profit = ti.xcom_pull(task_ids='Report', key=None)[5]
    IND_Net_Investment = ti.xcom_pull(task_ids='Report', key=None)[6]
    IND_Net_Total_Value = ti.xcom_pull(task_ids='Report', key=None)[7]
    IND_Profit = ti.xcom_pull(task_ids='Report', key=None)[8]

    Investment_Growth = sh.worksheet('Finance DWH')
    data = Investment_Growth.get_all_values()
    dates = [row[2] for row in data]

    template_file = "email_template.html"
    with open(template_file) as file:
        email_content = file.read()

    if current_date in dates:
        # Populate the HTML content with appropriate values
        email_content = email_content.format(
            Date = current_date,
            invested_value= round(Net_Invested),
            total_value=round(Net_Total_Value),
            net_profit=round(Net_Profit),
            us_net_investment=round(US_Net_Investment),
            us_net_total_value=round(US_Net_Total_Value),
            us_profit=round(US_Profit),
            ind_net_investment=round(IND_Net_Investment),
            ind_net_total_value=round(IND_Net_Total_Value),
            ind_profit=round(IND_Profit)
        )

        email_task = EmailOperator(
            task_id='send_email',
            to='boobathya1412@gmail.com',
            subject="Today's Report",
            html_content=email_content
        )
        email_task.execute(context=kw)
    else:
        print('Airflow job failed')
        email_content = """
        Hello,
        Your Airflow job failed as today's data is not available.

        Regards,
        Airflow
        """

default_args = {
    "owner": "Boobathy A",
    'start_date': datetime(2023, 10, 31)
}

dag = DAG('My_Investment',
          default_args=default_args,
         schedule_interval=None # You can adjust this based on your requirements
          )

Extract_Task = PythonOperator(
    task_id='Extract',
    python_callable=Extract,
    provide_context=True,
    dag=dag
)

Transformation_Task = PythonOperator(
    task_id='Transformation',
    python_callable=Transformation,
    provide_context=True,
    dag=dag
)

Load_Task = PythonOperator(
    task_id='Load',
    python_callable=Load,
    provide_context=True,
    dag=dag
)

Report_Task = PythonOperator(
    task_id = 'Report',
    python_callable = Report,
    dag=dag
)

Mail_Task = PythonOperator(
    task_id = 'mail',
    python_callable = send_email,
    dag=dag
)

Extract_Task >> Transformation_Task >> Load_Task >> Report_Task >>Mail_Task


import requests
import json
import jsonpath
import openpyxl
from DataDriven import Library

def test_add_mulitple_data():
    #API
    API_URL = "http://thetestingworldapi.com/api/studentsDetails"
    file = open("/Users/farhanmohammed/PycharmProjects/pythonPyTest/TestCases1/RequestJson.json",'r')
    json_request = json.loads(file.read())

    obj = Library.Common('/Users/farhanmohammed/PycharmProjects/pythonPyTest/TestCases1/TestData.xlsx', 'Sheet1') # Need to pass 2 arguments FileNamePath & SheetName
    col = obj.fetch_coulumn_count()
    row = obj.fetch_row_count()
    keylist = obj.fetch_key_name()

    for i in range(2,row+1):
        updated_json_request = obj.update_json_request(i,json_request,keylist)
        response = requests.post(API_URL,updated_json_request)
        print(response)


from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index),
    url(r'^goodbye$', views.goodbye),
    url(r'^signup$', views.signup),
    url(r'^login$', views.login),
    url(r'^new$', views.new),
    url(r'^(?P<my_var>\w+)$', views.greet),

]