from telebot.types import ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardRemove

from .db_structer import TeleUsers, University
from .model import get_semester

class Keyboard:
    def __init__(self, bot):
        """
        Handle all telegram keyboard keys
        :param bot: telebot bot API
        attributes:
            back_btn : KeyboardMarkup Button for back
            main_menu: KeyboardMarkup Button for main menu
        """

        self.jilebi = bot
        self.back_btn = KeyboardButton("🔙 Back")
        self.main_menu = KeyboardButton("🔝 Main Menu")

    def send_home(self, message):
        reply_markup = ReplyKeyboardMarkup(True, False)
        reply_markup.row("Get Today Events", "Get Tomorrow events")
        reply_markup.row("Get this week events", "Get next week events")
        reply_markup.row("Get this month events", "Other's Events")
        reply_markup.row("Extra's", "⚙ Settings")
        self.jilebi.send_message(
            message.chat.id, "Select Menu Item", reply_markup=reply_markup
        )
        TeleUsers.objects(pk=message.chat.id).update(unset__selection=1, position=0)

    def send_settings(self, message):
        reply_markup = ReplyKeyboardMarkup(row_width=2)
        user = TeleUsers.objects.only(
            "is_image_result", "calendar", "is_subscriber"
        ).get(pk=message.chat.id)
        buttons = ["🔗 Setup moodle calender link"]
        if user.calendar:
            buttons.append("🔗 Check my moodle calendar link")
        if user.is_subscriber:
            buttons.append("🔕 Unsubscribe Notification")
        else:
            buttons.append("🔔 Subscribe auto Notification")
        if user.is_image_result:
            buttons.append("📝 Get Text based result")
        else:
            buttons.append("🖼 Get Image based result")
        reply_markup.add(*[KeyboardButton(button) for button in buttons])
        reply_markup.row(self.main_menu)
        self.jilebi.send_message(
            message.chat.id, "Select Setting item", reply_markup=reply_markup
        )

    def send_university(self, message):
        reply_markup = ReplyKeyboardMarkup(True, False, row_width=2)
        reply_markup.add(
            *[KeyboardButton(uni) for uni in University.objects.distinct("name")]
        )
        reply_markup.add(self.back_btn)
        self.jilebi.send_message(
            message.chat.id, "Select a university", reply_markup=reply_markup
        )
        TeleUsers.objects(pk=message.chat.id).update(position=1)

    def send_faculty(self, message):
        user = TeleUsers.objects.only("selection").get(pk=message.chat.id)
        reply_markup = ReplyKeyboardMarkup(True, False, row_width=2)
        faculties = (
            University.objects.only("faculty__name")
            .get(name=user.selection.university)
            .faculty
        )
        reply_markup.add(*[KeyboardButton(faculty.name) for faculty in faculties])
        reply_markup.add(self.back_btn, self.main_menu)
        self.jilebi.send_message(
            message.chat.id, "Select the Faculty", reply_markup=reply_markup
        )
        user.update(position=2)

    def send_division(self, message):
        user = TeleUsers.objects.only("selection").get(pk=message.chat.id)
        reply_markup = ReplyKeyboardMarkup(True, False, row_width=2)
        divisions = (
            University.objects.only("faculty__name", "faculty__division__name")
            .get(name=user.selection.university)
            .faculty.get(name=user.selection.faculty)
            .division
        )
        reply_markup.add(*[KeyboardButton(division.name) for division in divisions])
        reply_markup.add(self.back_btn, self.main_menu)
        self.jilebi.send_message(
            message.chat.id, "Select the Division", reply_markup=reply_markup
        )
        user.update(position=3)

    def send_semester(self, message):
        user = TeleUsers.objects.only("selection").get(pk=message.chat.id)
        reply_markup = ReplyKeyboardMarkup(True, False, row_width=2)
        if user.selection.division:
            semesters = (
                University.objects.only(
                    "faculty__name",
                    "faculty__division__name",
                    "faculty__division__semester__name",
                )
                .get(name=user.selection.university)
                .faculty.get(name=user.selection.faculty)
                .division.get(name=user.selection.division)
                .semester
            )
        else:
            semesters = (
                University.objects.only("faculty__name", "faculty__semester__name")
                .get(name=user.selection.university)
                .faculty.get(name=user.selection.faculty)
                .semester
            )

        reply_markup.add(*[KeyboardButton(semester.name) for semester in semesters])
        reply_markup.add(self.back_btn, self.main_menu)
        self.jilebi.send_message(
            message.chat.id, "Select a semester", reply_markup=reply_markup
        )
        user.update(position=4)

    def send_others_menu(self, message):
        semester = get_semester(message.chat.id)
        reply_markup = ReplyKeyboardMarkup()
        buttons = []
        if semester.donor_calendar:
            buttons.extend(["Today Events", "Tomorrow events", "This week events", "Next week events",
                            "This month events"])
            self.jilebi.send_message(
                message.chat.id,
                f"Link Donate by: {semester.donor_name}",
                reply_markup=ReplyKeyboardRemove(),
            )
        else:
            self.jilebi.send_message(
                message.chat.id,
                "If you're a student of this course, please contact me to "
                "donate your link.",
            )
        buttons.append("Modules list")
        reply_markup.add(*[KeyboardButton(button) for button in buttons])
        reply_markup.row(self.back_btn, self.main_menu)
        self.jilebi.send_message(
            message.chat.id, "Select Item: ", reply_markup=reply_markup
        )
        TeleUsers.objects(pk=message.chat.id).update(position=5)

    def send_extras(self, message):
        reply_markup = ReplyKeyboardMarkup()
        reply_markup.row("Submit your module details", "Send Queries/Feedbacks")
        reply_markup.row("🎁 Share Jilebi", "👨🏻💻 Source Code", "Users Count")
        reply_markup.row(self.main_menu)
        self.jilebi.send_message(
            message.chat.id, "Select Item: ", reply_markup=reply_markup
        )

    def ask_division(self, message):
        reply_markup = ReplyKeyboardMarkup(row_width=2, one_time_keyboard=True)
        reply_markup.row("Yes", "No")
        self.jilebi.send_message(
            message.chat.id,
            "Are you have Divisions / Department inside your faculty?",
            reply_markup=reply_markup,
        )


import matplotlib
matplotlib.use('Agg')
import pylab
from pylab import rcParams
import matplotlib.pyplot as plt
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import pandas as pd
from pandas_datareader import data as pdr
import fix_yahoo_finance as yf
from io import BytesIO
import os

yf.pdr_override()

def start(bot, update):
  update.message.reply_text("Hai, yuk cek record stocks pilihanmu. \n Caranya ketik /help dulu ya")

def helps(bot, update):
  update.message.reply_text("Caranya: <Simbol perusahaan> <tgl mulai> <tgl selesai> \n Format tanggalnya YYYY-MM-DD")

def get_stock(bot, update):
  update.message.reply_text("Sedang diproses, mohon ditunggu...")
  idchat = update['message']['chat']['id']
  text = update['message']['text']
  texts = text.split(" ")
  if (len(texts) == 3):
    s = texts[1]
    e = texts[2]
    data = pdr.get_data_yahoo(texts[0], start=s, end=e)
    close = data['Close']
    all_weekdays = pd.date_range(start=s, end=e, freq='B')
    close = close.reindex(all_weekdays)
    fig = plt.figure(figsize=(15,8))
    ax = fig.add_subplot(111)
    plt.ylabel('Close Price')
    plt.xlabel('Date')
    plt.title("Close Price Stocks: "+str(texts[0]))
    # buffer = BytesIO()
    ax.plot(close.index,close)
    buffer = BytesIO()
    fig.savefig(buffer, format='png')
    buffer.seek(0)
    bot.send_photo(idchat, photo=buffer)
  elif (len(texts) == 4):
    get_stock_multi(bot, update)

def get_stock_multi(bot, update):
  idchat = update['message']['chat']['id']
  text = update['message']['text']
  texts = text.split(" ")
  s = texts[2]
  e = texts[3]
  print("get")
  data1 = pdr.get_data_yahoo(texts[0], start=s, end=e)
  data2 = pdr.get_data_yahoo(texts[1], start=s, end=e)
  close1 = data1['Close']
  close2 = data2['Close']
  all_weekdays = pd.date_range(start=s, end=e, freq='B')
  close1 = close1.reindex(all_weekdays)
  close2 = close2.reindex(all_weekdays)
  fig = plt.figure(figsize=(15,8))
  ax = fig.add_subplot(111)
  plt.ylabel('Close Price')
  plt.xlabel('Date')
  plt.title("Close Price Stocks: "+str(texts[0])+" & "+str(texts[1]))
  ax.plot(close1.index,close1,'r')
  ax.plot(close2.index,close2,'b')
  ax.legend([texts[0], texts[1]])
  buffer = BytesIO()
  fig.savefig(buffer, format='png')
  buffer.seek(0)
  print("get")
  bot.send_photo(idchat, photo=buffer)

def main():
  # Create Updater object and attach dispatcher to it
  TOKEN = "781975723:AAEIsAhOsFRnlGOwbVJJY9nTwWGgSw7-Pc8"
  PORT = int(os.environ.get('PORT','5000'))
  updater = Updater(TOKEN)
  updater.start_webhook(listen="0.0.0.0",
                    port = PORT,
                    url_path=TOKEN)
  dispatcher = updater.dispatcher
  print("Bot started")

  # Add command handler to dispatcher
  start_handler = CommandHandler('start',start)
  help_handler = CommandHandler('help',helps)
  stock_case = MessageHandler(Filters.text, get_stock)
  dispatcher.add_handler(start_handler)
  dispatcher.add_handler(stock_case)
  dispatcher.add_handler(help_handler)

  # Start the bot
  updater.bot.set_webhook("https://still-bastion-47471.herokuapp.com/"+TOKEN)
  updater.idle()
  # updater.start_polling()

  # Run the bot until you press Ctrl-C
  # updater.idle()

if __name__ == '__main__':
  main()


import cv2
from cv2 import sqrt
import matplotlib.pyplot as plt
from numpy import zeros_like
import numpy as np
def CIE_lab_nearest_segmentation(image,n_blocks):
    '''
    n_blocks : The category into which the image needs to be divided
    image : BGR image
    return : segmentedFrames,Iplot
    segmentedFrames : (RGB)The set of images after segmentation
    Iplot : (RGB)The distribution of segmented image on axis (a,b)
    '''
    Ilab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
    Ilab = cv2.split(Ilab)
    #Color sample space generation
    sampleAreas = []
    def MouseHandler(event,x,y,flags,param):
        if event != cv2.EVENT_LBUTTONDBLCLK:
            return
        sampleAreas.append((x,y))
    cv2.imshow("image",image)
    cv2.setMouseCallback('image',MouseHandler)
    while len(sampleAreas) < n_blocks:
        cv2.waitKey(20)
    cv2.setMouseCallback('image',lambda*args:None)
    #Calculate average color
    colorMarksLAB = []
    colorMarksBGR = []
    for pix in sampleAreas:
        mask = zeros_like(Ilab[0])
        cv2.circle(mask,pix,10,255,-1)
        a = np.mean(Ilab[1][np.argwhere(mask > 0)])
        b = np.mean(Ilab[2][np.argwhere(mask > 0)])
        colorMarksLAB.append((a,b))
        colorMarksBGR.append(image[mask>0,:].mean(axis=(0)))
    #Calculates the minimum distance between all pixels and all colors
    distance = []
    for color in colorMarksLAB:
        distance.append(sqrt(pow((Ilab[1]-color[0]),2)+pow((Ilab[2]-color[1]),2)))
    distance_min = np.minimum.reduce(distance)
    #Calculates the color label for each pixel
    labels = zeros_like(Ilab[0],dtype= np.uint8)
    for i in range(len(colorMarksLAB)):
        mask = distance_min ==distance[i]
        labels[mask] = i
    #Split and store the original image
    segmentedFrames = []
    for i in range(len(colorMarksLAB)):
        Itmp = zeros_like(image)
        mask = labels == i
        Itmp[mask] = image[mask]
        segmentedFrames.append(Itmp)
    #Displays the color distribution in (a,b) coordinates
    Iplot = np.full((256,256,3),255,dtype=np.uint8)
    for i in range(len(colorMarksLAB)):
        Itmp = zeros_like(image)
        mask = labels == i
        Iplot[Ilab[1][mask],Ilab[2][mask],:] = colorMarksBGR[i]
    for i in range(n_blocks):
        segmentedFrames[i] = cv2.cvtColor(segmentedFrames[i],cv2.COLOR_BGR2RGB)
    Iplot = cv2.cvtColor(Iplot,cv2.COLOR_BGR2RGB)
    return segmentedFrames,Iplot

#input the image
#2 categories
I1 = cv2.imread("pictures/ball2.jpg",cv2.IMREAD_COLOR)
segmentedFrames1,Iplot1 = CIE_lab_nearest_segmentation(I1,2)
I1 = cv2.cvtColor(I1, cv2.COLOR_BGR2RGB)

plt.subplot(221),plt.title('original image'),plt.xticks([]),plt.yticks([])
plt.imshow(I1)
plt.subplot(222),plt.title('segmented image 1'),plt.xticks([]),plt.yticks([])
plt.imshow(segmentedFrames1[0])
plt.subplot(223),plt.title('segmented image 2'),plt.xticks([]),plt.yticks([])
plt.imshow(segmentedFrames1[1])
plt.show()
plt.waitforbuttonpress

#3 categories
I2 = cv2.imread("pictures/ball3.jpg",cv2.IMREAD_COLOR)
segmentedFrames2,Iplot2 = CIE_lab_nearest_segmentation(I2,3)
I2 = cv2.cvtColor(I2, cv2.COLOR_BGR2RGB)

plt.subplot(221),plt.title('original image'),plt.xticks([]),plt.yticks([])
plt.imshow(I2)
plt.subplot(222),plt.title('segmented image 1'),plt.xticks([]),plt.yticks([])
plt.imshow(segmentedFrames2[0])
plt.subplot(223),plt.title('segmented image 2'),plt.xticks([]),plt.yticks([])
plt.imshow(segmentedFrames2[1])
plt.subplot(224),plt.title('segmented image 3'),plt.xticks([]),plt.yticks([])
plt.imshow(segmentedFrames2[2])
plt.show()
plt.waitforbuttonpress


# -*- coding: utf-8 -*-
"""
Created on Fri Oct  5 18:56:30 2018

@author: Andrei ROG
"""
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
from sklearn.decomposition import PCA
from random import seed
from random import random
from random import gauss
import random
import copy
from scipy import stats

def data_rand(N,M,sigma,Groups=2):

    # create the data container
    data_rand = []
    Labels = []
    # seed random number generator

    # generate random numbers between 0-1
    for _ in range(M):
        mean_random  = random.randint(50,150)#create one mean value
        v = []#create the sample points for each variable
        for k in range(N):
            v.append(gauss(mean_random, random.randint(sigma,2*sigma)))
        data_rand.append(v)
    for _ in range(N):
        Labels.append(random.randint(0,Groups-1))
    return data_rand,Labels

def add_signifficance(data,Labels,Groups,averageSig,sigma,sigvars):
    sig = []
    for j in Groups:
        if j>0:
            for v in sigvars:
                k = random.randint(averageSig-2*sigma,averageSig+2*sigma) + gauss(0, random.randint(sigma,2*sigma))
                sig.append(k)
                data[Labels==j,v] = data[Labels==j,v] + k
    return data,sig

def JSDe(X,Y,w,k):
    #project the data to k
    N,M = np.shape(X)
    T = np.repeat([k],N,0)
    xp = np.sort(np.sum(X*T,1)/np.linalg.norm(k)**2)
    j = 0
    JSDe = 0
    C = np.unique(Y)
    for c in C:
        Xc = X[Y==c,:]
        n,m = np.shape(Xc)
        T = np.repeat([k],n,0)
        xc = np.sort(np.sum(Xc*T,1)/np.linalg.norm(k)**2)
        nc = np.shape(xc)[0]
        jsd = 0
        for i in np.arange(1,nc-1,1):
#            sx = np.min([xp[i]-xp[i-1],xp[i+1]-xp[i]])
#            rx = np.min(np.abs(xc[xc!=xp[i]]-xp[i]))
            sx = np.min(np.abs(xp[xp!=xc[i]]-xc[i]))
            rx = np.min(np.abs([xc[i]-xc[i-1],xc[i+1]-xc[i]]))
            if rx == 0 or sx == 0:
                jsd += (0 + np.log2(N/(nc-1)))
            else:
                jsd += (np.log2(sx/rx) + np.log2(N/(nc-1)))
        JSDe += (w[j]/nc)*jsd
        j += 1
    return JSDe
def JSD(X,Y,w,k,nr_pts,hist = False):
    N,M = np.shape(X)
    T = np.repeat([k],N,0)
    xp = np.sort(np.sum(X*T,1)/np.linalg.norm(k)**2)

#    print(np.log2(Slack*nr_pts))
    pts = np.linspace(np.min(xp),np.max(xp),nr_pts)
    C = np.unique(Y)
    j = 0
    Hc = 0
    jsd = 0
    fmix = np.zeros(np.shape(pts)[0])
#    plt.figure()
    if hist == False:
        for c in C:
            Xc = X[Y==c,:]
            n,m = np.shape(Xc)
            T = np.repeat([k],n,0)
            xc = np.sort(np.sum(Xc*T,1)/np.linalg.norm(k)**2)
            KernelD = stats.gaussian_kde(xc,bw_method='scott')
            f = KernelD.evaluate(pts)
            fmix = fmix + w[c]*f
    #        plt.plot(pts,f)
            Fx = (f[0:-1] + 0.5*np.diff(f))
            Fx = Fx/np.sum(Fx)

            Hc = Hc - w[c]*(np.sum(Fx[Fx>0]*np.log2(Fx[Fx>0])))
    #        Hc = Hc - w[c]*np.trapz(f[f>0]*np.log2(f[f>0]),pts[f>0])
            j+=1
    #    plt.plot(pts,fmix)
        Fmix = (fmix[0:-1] + 0.5*np.diff(fmix))
        H = Fmix/np.sum(Fmix)
        Hmix = -(np.sum(H[H>0]*np.log2(H[H>0])))
    else:
        for c in C:
            Xc = X[Y==c,:]
            n,m = np.shape(Xc)
            T = np.repeat([k],n,0)
            xc = np.sort(np.sum(Xc*T,1)/np.linalg.norm(k)**2)
            f,_ = np.histogram(xc,bins = nr_pts,range=[pts[0],pts[-1]])

    #        plt.plot(pts,f)
            f = f/np.sum(f)

            Hc = Hc - w[c]*(np.sum(f[f>0]*np.log2(f[f>0])))
    #        Hc = Hc - w[c]*np.trapz(f[f>0]*np.log2(f[f>0]),pts[f>0])
            j+=1
    #    plt.plot(pts,fmix)
        n,m = np.shape(X)
        T = np.repeat([k],n,0)
        Xc = np.sort(np.sum(X*T,1)/np.linalg.norm(k)**2)

        Fmix,_ = np.histogram(Xc,bins = nr_pts,range=[pts[0],pts[-1]])
        H = Fmix/np.sum(Fmix)
        Hmix = -(np.sum(H[H>0]*np.log2(H[H>0])))
    jsd = Hmix - Hc
    if jsd<0:
        print('negative')
    return jsd

def gradJSDe(X,Y,w,k):
    dG = None
    N,M = np.shape(X)
    T = np.repeat([k],N,0)
    xp = np.sort(np.sum(X*T,1)/np.linalg.norm(k)**2)

    dG = 0
    C = np.unique(Y)
    for j,c in enumerate(C):
        Xc = X[Y==c,:]
        n,m = np.shape(Xc)
        T = np.repeat([k],n,0)
        xc = np.sort(np.sum(Xc*T,1)/np.linalg.norm(k)**2)
        nc = np.shape(xc)[0]
        jsd = 0
        for i in np.arange(1,nc-1,1):
#            sx = np.min([xp[i]-xp[i-1],xp[i+1]-xp[i]])
#            rx = np.min(np.abs(xc[xc!=xp[i]]-xp[i]))

            sx = np.min(np.abs(xp[xp!=xc[i]]-xc[i]))
            dvc = [xc[i]-xc[i-1],xc[i+1]-xc[i]]

            rx = np.min(np.abs(dvc))
            if np.abs(dvc[0])<np.abs(dvc[1]):
                id_xc = i-1
            else:
                id_xc = i+1
            id_xp = np.where(np.abs(xp[xp!=xc[i]]-xc[i]) == np.min(np.abs(xp[xp!=xc[i]]-xc[i])))
            redX = X[xp!=xc[i],:]
            red_xp = xp[xp!=xc[i]]
            aq = np.squeeze(redX[id_xp,:])
            ai = Xc[id_xc,:]
            aj = Xc[i]
            delta_xi = xc[i] - xc[id_xc]
            delta_xp = xc[i] - red_xp[id_xp]
            if rx == 0 or sx == 0:
                jsd += 0
            else:
#                jsd += (xc[i]*ai + red_xp[id_xp]*aq)/sx**2 - (xc[i]*ai + xc[id_xc]*aj)/rx**2
                jsd += ((np.abs(delta_xi)*delta_xp*[ai-aq] - np.abs(delta_xp)*delta_xi*[ai-aj])/(np.abs(delta_xp)*np.abs(delta_xi)))
        dG += (w[j]/nc/np.log(2))*jsd
    dG = np.squeeze(dG)
    dx = dG[0]
    dy = dG[1]

    return dx,dy,dG

if __name__ == '__main__':
    __spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)"
    N = 200
    M = 2
    CLASSES = 2
    random.seed
    data_rand,Labels = data_rand(N,M,20,Groups=CLASSES)
    data_rand = np.array(data_rand, dtype='float32').T
    Labels = np.array(Labels,dtype='int32')
    j = 0
    varSig = []
    while j< M:
        v = random.randint(0,M-1)
        if v not in varSig:
            varSig.append(v)
            j += 1

    averageSig = 350
    sigma = 10
    data_sig,sig = add_signifficance(copy.deepcopy(data_rand),Labels,np.arange(0,CLASSES,1),averageSig,sigma,varSig)
    w = (1/CLASSES)*np.ones(CLASSES)
#    #######PCA####
#    pca = PCA(n_components=1)#create an instance of PCA
#    pca.fit(data_sig)
#    L_pca = pca.components_
#    Xp_train_pca = np.dot(np.dot(data_sig,L_pca.T),np.linalg.pinv(np.dot(L_pca,L_pca.T)))

    Span = np.arange(-1,1,0.01)
    JS_es1 = np.zeros([np.shape(Span)[0],np.shape(Span)[0]])
    JS_es2 = copy.deepcopy(JS_es1)
    JS_es3 = copy.deepcopy(JS_es1)
    dX = np.zeros([np.shape(Span)[0],np.shape(Span)[0]])
    dY = np.zeros([np.shape(Span)[0],np.shape(Span)[0]])
    p =0

    for p,i in enumerate(Span):
        q = 0
        for q,j in enumerate(Span):
            k = np.array([i,j])/np.linalg.norm([i,j])
#            k =  [0.01*random.randint(1,100) for _ in range(M)]
#            k = k-np.mean(k)

#            JS_estim = JSDe(data_rand,Labels,w,k)
#            JS_kdens = JSD(data_rand,Labels,w,k,100)
            dx,dy,_ = gradJSDe(data_sig,Labels,w,k)
            JS1 = JSDe(data_sig,Labels,w,k)
            JS2 = JSD(data_sig,Labels,w,k,100)
            JS3 = JSD(data_sig,Labels,w,k,100,hist=True)
            JS_es1[p,q] = JS1
            JS_es2[p,q] = JS2
            JS_es3[p,q] = JS3
            dX[p,q] = dx
            dY[p,q] = dy
            q = q+1

    X, Y = np.meshgrid(Span, Span)
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    # Plot the surface.
    surf = ax.plot_surface(X, Y, JS_es1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
    plt.title('Eucliden dist JSd')
    fig.colorbar(surf, shrink=0.5, aspect=5)

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    # Plot the surface.
    surf = ax.plot_surface(X, Y, JS_es2, cmap=cm.coolwarm, linewidth=0, antialiased=False)
    plt.title('Gauss kernel JSd')
    fig.colorbar(surf, shrink=0.5, aspect=5)
    plt.show()

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    # Plot the surface.
    surf = ax.plot_surface(X, Y, JS_es3, cmap=cm.coolwarm, linewidth=0, antialiased=False)
    plt.title('Histogram JSd')
    fig.colorbar(surf, shrink=0.5, aspect=5)
    plt.show()
#    np.delete(data_sig)
    # quiver from the smoothed version
    dy, dx = np.gradient(JS_es2, X[0,:], Y[:,0])
#    fig, ax = plt.subplots()
#    ax.quiver(X, Y, dx, dy, JS_es2)
#    ax.set(aspect=1, title='Quiver Plot of the kernel density')
#    plt.show()

    skip = (slice(None, None, 3), slice(None, None, 3))
    fig, ax = plt.subplots()
    ax.quiver(X[skip], Y[skip], dx[skip], dy[skip], JS_es2[skip])
    ax.set(aspect=1, title='Quiver Plot of the kernel')
    plt.show()

    fig, ax = plt.subplots()
    im = ax.imshow(JS_es2, extent=[X.min(), X.max(), Y.min(), Y.max()],origin='lower')
    ax.quiver(X[skip], Y[skip], dx[skip], dy[skip])

    fig.colorbar(im)
    ax.set(aspect=1, title='Quiver Plot of the kernel')
    plt.show()

    fig, ax = plt.subplots()
    ax.streamplot(X, Y, dx, dy, color=JS_es2, density=0.5, cmap='gist_earth')
    cont = ax.contour(X, Y, JS_es2, cmap='gist_earth')
    ax.clabel(cont)
    ax.set(aspect=1, title='Streamplot with contours kernel density')
    plt.show()

    #theoretical gradient from the k-nn
#    fig, ax = plt.subplots()
#    ax.quiver(X, Y, dX, dY, JS_es1)
#    ax.set(aspect=1, title='Quiver Plot from the theoretical k-nn')
#    plt.show()

    skip = (slice(None, None, 3), slice(None, None, 3))
    fig, ax = plt.subplots()
    ax.quiver(X[skip], Y[skip], dX[skip], dY[skip], JS_es1[skip])
    ax.set(aspect=1, title='Quiver Plot from theoretical k-nn')
    plt.show()

    fig, ax = plt.subplots()
    im = ax.imshow(JS_es1, extent=[X.min(), X.max(), Y.min(), Y.max()],origin='lower')
    ax.quiver(X[skip], Y[skip], dX[skip], dY[skip])

    fig.colorbar(im)
    ax.set(aspect=1, title='Quiver Plot from theoretical k-nn')
    plt.show()

    fig, ax = plt.subplots()
    ax.streamplot(X, Y, dX, dY, color=JS_es1, density=0.5, cmap='gist_earth')
    cont = ax.contour(X, Y, JS_es1, cmap='gist_earth')
    ax.clabel(cont)
    ax.set(aspect=1, title='Streamplot with contours from theoretical k-nn')
    plt.show()

    #gradient from the k-nn surface
    dYe, dXe = np.gradient(JS_es1, X[0,:], Y[:,0])
#    fig, ax = plt.subplots()
#    ax.quiver(X, Y, dX, dY, JS_es1)
#    ax.set(aspect=1, title='Quiver Plot from k-nn surface')
#    plt.show()

    skip = (slice(None, None, 3), slice(None, None, 3))
    fig, ax = plt.subplots()
    ax.quiver(X[skip], Y[skip], dXe[skip], dYe[skip], JS_es1[skip])
    ax.set(aspect=1, title='Quiver Plot from k-nn surface')
    plt.show()

    fig, ax = plt.subplots()
    im = ax.imshow(JS_es1, extent=[X.min(), X.max(), Y.min(), Y.max()],origin='lower')
    ax.quiver(X[skip], Y[skip], dXe[skip], dYe[skip])

    fig.colorbar(im)
    ax.set(aspect=1, title='Quiver Plot')
    plt.show()
    ax.set(title='K-nn surface quiver')

    fig, ax = plt.subplots()
    ax.streamplot(X, Y, dXe, dYe, color=JS_es1, density=0.5, cmap='gist_earth')
    cont = ax.contour(X, Y, JS_es1, cmap='gist_earth')
    ax.clabel(cont)
    ax.set(aspect=1, title='Streamplot with contours from k-nn surface')
    plt.show()
    #gradient estimation from histogram entropy
    dYh, dXh = np.gradient(JS_es3, X[0,:], Y[:,0])
#    fig, ax = plt.subplots()
#    ax.quiver(X, Y, dXe, dYe, JS_es3)
#    ax.set(aspect=1, title='Quiver Plot')
#    plt.show()

    skip = (slice(None, None, 3), slice(None, None, 3))
    fig, ax = plt.subplots()
    ax.quiver(X[skip], Y[skip], dXh[skip], dYh[skip], JS_es3[skip])
    ax.set(aspect=1, title='Quiver Plot histogram entropy')
    plt.show()
    fig, ax = plt.subplots()
    im = ax.imshow(JS_es3, extent=[X.min(), X.max(), Y.min(), Y.max()],origin='lower')
    ax.quiver(X[skip], Y[skip], dXh[skip], dYh[skip])

    fig.colorbar(im)
    ax.set(aspect=1, title='Quiver Plot histogram entropy')
    plt.show()

    fig, ax = plt.subplots()
    ax.streamplot(X, Y, dXh, dYh, color=JS_es3, density=0.5, cmap='gist_earth')


from multiprocessing import Pool
from datetime import datetime, timedelta

def duplicate_date(date_tuple):
    pass

if __name__ == '__main__':

    pool = Pool(3)

    init_date_str = '20200531'
    init_date = datetime.strptime(init_date_str, '%Y%m%d').date()
    date_list = []

    number = 1
    while 1:
        next_date = init_date + timedelta(days=number)
        str_next_date = str(next_date).replace('-', '')

        # 该批次 导入的时间
        if str_next_date >= '20210101':
            date_list.append((number, str_next_date))
        number += 1

        # 该批次 导入的截止时间
        if str_next_date == '20210630':
            break

    try:
        pool.map(duplicate_date, date_list)
        pool.close()
        pool.join()
    except Exception as e:
        logger.info(e)


"""Routes for the course resource.
"""

from flask import Flask, jsonify, request
from datetime import datetime
import data
from validate import Validation_Put, Validation_Post

d = data.load_data()
app = Flask(__name__)
current_highest_id = 200

@app.route('/course/<int:id>', methods=['GET'])
def get_course(id):
    """Get a course by id.

        :param int id: The record id.
        :return: A single course (see the challenge notes for examples)
        :rtype: object
        """

    """
    -------------------------------------------------------------------------
    Challenge notes:
    -------------------------------------------------------------------------
    1. Bonus points for not using a linear scan on your data structure.
    """
    # YOUR CODE HERE
    if id in d.keys():
        return jsonify({"data": d[id]}),200
    else:
        return jsonify({"message": f"Course {id} does not exist"}),404

@app.route("/course", methods=['GET'])
def get_courses():
    """Get a page of courses, optionally filtered by title words (a list of
        words separated by commas".

        Query parameters: page-number, page-size, title-words
        If not present, we use defaults of page-number=1, page-size=10

        :return: A page of courses (see the challenge notes for examples)
        :rtype: object
        """

    """
    -------------------------------------------------------------------------
    Challenge notes:
    -------------------------------------------------------------------------
    1. Bonus points for not using a linear scan, on your data structure, if
       title-words is supplied
    2. Bonus points for returning resulted sorted by the number of words which
       matched, if title-words is supplied.
    3. Bonus points for including performance data on the API, in terms of
       requests/second.
    """
    # YOUR CODE HERE

    # Included sorting for title-words match
    # Highest to lowest in terms of number of matching words
    # For each number of similar words, sorted in ascending order

    pages = request.args.get('page-size', 200)
    pages = int(pages)
    page_number = request.args.get('page-size', 1)
    words = request.args.get('title-words', '')
    words = words.split(',')
    count = [[] for i in range(len(words) + 1)]
    final = []
    for i in (sorted(d)):
        c = 0
        for j in words:
            if j.lower() in d[i]['title'].lower():
                c += 1
        if c != 0:
            count[c].append(d[i])
    for i in range(len(count) - 1, -1, -1):
        final += count[i]
    print(final)
    if final != [] and pages <= current_highest_id:
        if len(final) > pages:

            return jsonify({"data": final[0:pages],
                            "metadata": {"page_count": int(pages / int(page_number)), "page_number": 1,
                                         "page_size": pages, "record_count": len(final)}}),200
        else:
            return jsonify({"data": final, "metadata": {"page_count": int(pages / int(page_number)), "page_number": 1,
                                                        "page_size": pages, "record_count": len(final)}}),200
    else :
        return jsonify({"message": "No courses found"}), 404

@app.route("/course", methods=['POST'])
def create_course():
    """Create a course.
        :return: The course object (see the challenge notes for examples)
        :rtype: object
        """

    """
    -------------------------------------------------------------------------
    Challenge notes:
    -------------------------------------------------------------------------
    1. Bonus points for validating the POST body fields
    """
    # YOUR CODE HERE
    global current_highest_id
    current_highest_id += 1
    validating = Validation_Post()
    errors = validating.validate(request.get_json())
    if errors:
        message = ''
        for i in errors:
            message += errors[i][0]
        return jsonify(({"message": message})), 404
    date_created = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
    date_updated = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
    new_entry = request.get_json()
    new_entry["date_created"] = date_created
    new_entry["date_updated"] = date_updated
    new_entry["id"] = current_highest_id
    d[current_highest_id] = new_entry
    return jsonify({"data": d[current_highest_id]}),200

@app.route("/course/<int:id>", methods=['PUT'])
def update_course(id):
    """Update a a course.
        :param int id: The record id.
        :return: The updated course object (see the challenge notes for examples)
        :rtype: object
        """

    """
    -------------------------------------------------------------------------
    Challenge notes:
    -------------------------------------------------------------------------
    1. Bonus points for validating the PUT body fields, including checking
       against the id in the URL

    """
    # YOUR CODE HERE
    validating = Validation_Put()

    errors = validating.validate(request.get_json())
    if errors:
        message = ''
        for i in errors:
            message += errors[i][0]
        return jsonify({"message": message}), 404

    else:
        updated_entry = request.get_json()
        if id != updated_entry["id"]:
            return jsonify({"message": "The id does match the payload"}), 404
        else:
            updated_entry["date_updated"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
            updated_entry["date_created"] = d[id]["date_created"]
            d[id]=updated_entry
            return jsonify({"data": d[id]}), 200

@app.route("/course/<int:id>", methods=['DELETE'])
def delete_course(id):
    """Delete a course
        :return: A confirmation message (see the challenge notes for examples)
        """
    """
    -------------------------------------------------------------------------
    Challenge notes:
    -------------------------------------------------------------------------
    None
    """
    # YOUR CODE HERE
    try:
        del d[id]
    except KeyError:
        return jsonify({"message": f"Course {id} does not exist"}), 404
    return jsonify({"message": "The specified course was deleted"}), 200

if __name__ == '__main__':
    app.run(debug=True)


import json
import pathlib
import sys
import csv
from collections import defaultdict
import argparse

import tabulate

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Evaluate results")

    parser.add_argument("data", help="Results file", type=pathlib.Path)
    parser.add_argument("--only", help="Filter by type (single_scc, one_state_scc)")
    parser.add_argument("--latex", help="LaTeX output", action="store_true")
    parser.add_argument(
        "--scatter", help="Instead, write the scatter plot data", type=str
    )
    parser.add_argument(
        "--scatter-to",
        help="Value to replace timeout with in scatter plot",
        type=int,
        default=600,
    )
    args = parser.parse_args()

    with args.data.open(mode="rt") as f:
        results = json.load(f)

    approaches = set()
    data = defaultdict(dict)
    for family, family_results in results.items():
        for instance, instance_data in family_results.get("results", {}).items():
            d = instance_data["data"]
            size = d.get("states", 0)
            if size == "error":
                size = -1
            components = d.get("components", 0)
            if components == "error":
                components = -1
            largest_component = d.get("largest_component", 0)
            if largest_component == "error":
                largest_component = -1
            if args.only == "one_state_scc":
                if largest_component != 1:
                    continue
            elif args.only == "single_scc":
                if largest_component != size:
                    continue

            for approach, approach_result in instance_data.get("results", {}).items():
                approaches.add(approach)
                data[
                    (
                        family_results["type"].lower(),
                        family,
                        size,
                        components,
                        largest_component,
                        instance,
                    )
                ][approach] = approach_result
    approaches = list(sorted(approaches))

    if args.scatter:
        if len(args.scatter.split(",")) != 2:
            sys.exit("Give scatter as <left>,<right>")
        left_name, right_name = args.scatter.split(",")
        if left_name not in approaches:
            sys.exit(f"{left_name} not known")
        if right_name not in approaches:
            sys.exit(f"{right_name} not known")

        writer = csv.writer(sys.stdout)
        writer.writerow([left_name, right_name])
        for key in data.keys():
            model_type, family, size, components, largest_component, instance = key

            left, right = data[key].get(left_name, None), data[key].get(
                right_name, None
            )
            if (
                left is None
                or left.get("type", None) not in {"success", "timeout", "skipped"}
                or right is None
                or right.get("type", None) not in {"success", "timeout", "skipped"}
            ):
                continue
            if left["type"] != "success" and right["type"] != "success":
                continue

            writer.writerow(
                [
                    round(left["time"], 3)
                    if left["type"] == "success"
                    else args.scatter_to,
                    round(right["time"], 3)
                    if right["type"] == "success"
                    else args.scatter_to,
                ]
            )
        sys.exit()

    header = [
        "type",
        "instance",
        "size",
        "components",
        "largest_component",
    ] + approaches
    rows = []
    for key in sorted(data.keys()):
        model_type, family, size, components, largest_component, instance = key
        row = [model_type, f"{family}/{instance}", size, components, largest_component]
        for approach in approaches:
            result = data[key].get(approach, None)
            if result is None or "type" not in result:
                row.append("?")
            else:
                result_type = result["type"]
                if result_type == "error":
                    error_type = result.get("error", "generic")
                    if error_type == "stackoverflow":
                        row.append("Stack Overflow")
                    elif error_type == "memory" or result["code"] == -9:
                        row.append("M/O")
                    elif error_type == "bounds":
                        row.append("Internal error")
                    elif error_type == "convergence":
                        row.append("Convergence error")
                    else:
                        row.append(f"E({result['code']})")
                elif result_type == "timeout":
                    row.append("T/O")
                elif result_type == "skipped":
                    row.append("-")
                elif result_type == "success":
                    row.append(f"{result['time']:.2f}")
                else:
                    row.append(f"?{result_type}?")
        rows.append(row)
    print(tabulate.tabulate(rows, header, tablefmt="latex" if args.latex else "simple"))


'''
Author: lmio 2091319361@qq.com
Date: 2023-09-10 10:30:34
LastEditors: lmio 2091319361@qq.com
Description: 8029. 与车相交的点
'''

import enum
from typing import List

class Solution:
    def numberOfPoints(self, nums: List[List[int]]) -> int:
        if not nums:
            return 0
        res = 0
        nums.sort()
        start, end = nums[0]
        for u, v in nums:
            if u <= end:
                end = max(end, v)
            else:
                res += end - start + 1
                start, end = u, v
        res += end-start+1
        return res


from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from api_app.models import Junction
from test_util import login

class JunctionTests(APITestCase):
    def test_create_junction(self):
        """
        Test adding a new junction."
        """
        login(self.client)
        url = reverse('junction-list')
        data = {'description': 'Test junction'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Junction.objects.count(), 1)
        self.assertEqual(Junction.objects.get().description, 'Test junction')

    def test_duplicate_junction(self):
        """
        Test that adding a junction with an existing description fails."
        """
        login(self.client)
        url = reverse('junction-list')
        data = {'description': 'Test junction'}
        response = self.client.post(url, data, format='json')
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(Junction.objects.count(), 1)


"""
Given the head of a singly linked list, reverse the list, and return the reversed list.

Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]

"""

from typing import Optional

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def reverseList(head: Optional[ListNode]) -> Optional[ListNode]:
    cur = None
    temp = head
    while temp:
        temp_node = temp.next
        temp.next = cur
        cur = temp
        temp = temp_node
    return cur

"""

Time Complexity: O(N)
Space: O(1)

"""


from enum import Enum
from typing import Tuple, Union, NewType

from quantlib.utils import quantlib_err_header

qrangespec = {"bitwidth": 8 , "signed":True}

# The data type representing quantisation granularity. QuantLib modules will
# instantiate a separate quantiser for each of the specified dimensions. The
# dimensions are specified by means of their integer index. These indices are
# collected in a tuple: this choice implies that the quantisation granularity
# will be static throughout training.
QGranularity = NewType('QGranularity', Tuple[int, ...])

# The data type representing how users can specify quantisation granularity:
#   * either they provide the explicit collection of dimension indices
#   * or they provide string shortcuts.
QGranularitySpecType = Union[QGranularity, Tuple[int, ...], str]

def resolve_qgranularity_qgranularityspec(qgranularityspec: QGranularity) -> QGranularity:
    return qgranularityspec

def resolve_tuple_qgranularityspec(qgranularityspec: Tuple[int, ...]) -> QGranularity:
    """Map an explicit index collection (tuple of integers) to a
    ``QGranularity`` object.
    """
    if any(map(lambda i: i < 0, qgranularityspec)):
        raise ValueError(quantlib_err_header() + f"requires non-negative dimension indices, but found the following values: {tuple(i for i in qgranularityspec if i < 0)}.")

    qgranularityspec = tuple(set(qgranularityspec))  # remove duplicate values
    qgranularityspec = tuple(sorted(qgranularityspec))
    qgranularity = QGranularity(qgranularityspec)
    return qgranularity

# String shortcuts must belong to the following list. We assume the NCHW
# ordering of PyTorch.
QGranularityStrSpecOptions = Enum('QGranularityStrSpecOptions',
                                  [
                                      ('PER-ARRAY',              QGranularity(tuple())),
                                      ('PER-OUTCHANNEL_WEIGHTS', QGranularity((0,))),
                                  ])

def resolve_str_qgranularityspec(qgranularityspec: str) -> QGranularity:
    """Map a (supported) string shortcut to a ``QGranularity`` object."""
    qgranularityspec = qgranularityspec.upper()
    try:
        qgranularity = getattr(QGranularityStrSpecOptions, qgranularityspec).value
        return qgranularity
    except AttributeError:
        raise ValueError(f"unsupported QGranularity string specification: {qgranularityspec}.")

# Redirect the general solver to the type-specific solver method.
QGranularitySpecSolvers = Enum('QGranularitySpecSolvers',
                               [
                                   ('QGRANULARITY', resolve_qgranularity_qgranularityspec),
                                   ('TUPLE',        resolve_tuple_qgranularityspec),
                                   ('STR',          resolve_str_qgranularityspec),
                               ])

def resolve_qgranularityspec(qgranularityspec: QGranularitySpecType) -> QGranularity:
    """Resolve user specifications into ``QGranularity`` objects.

    This function uses a strategy pattern to disambiguate different types of
    specifications.
    """
    qgranularityspec_class = qgranularityspec.__class__.__name__.upper()

    try:
        solver = getattr(QGranularitySpecSolvers, qgranularityspec_class)
        qgranularity = solver(qgranularityspec)
        return qgranularity
    except AttributeError:
        raise TypeError(f"unsupported QGranularity specification type: {qgranularityspec_class}.")


import json
from contracts_api_test.unit.test_base import BaseTestCase
from contracts_api.api.models import Stage, Flow

class FlowsResourceTest(BaseTestCase):
    def setUp(self):
        super(FlowsResourceTest, self).setUp()

        # create fixture stages and flows
        Stage.create(name='stage1').save()
        Stage.create(name='stage2').save()
        self.flow = Flow.create(flow_name='flow1', stage_order=[1,2]).save()

    render_templates = False

    def test_list_get(self):
        flows = json.loads(self.client.get('/api/v1/flows').data)
        self.assertEquals(flows['meta']['count'], 1)

    def test_detail_get(self):
        flow = json.loads(self.client.get('/api/v1/flow/{id}'.format(id=self.flow)).data)
        self.assertEquals(flow['flow_name'], 'flow1')
        self.assertEquals(type(flow), dict)

        bad_flow = self.client.get('/api/v1/flow/10000000')
        self.assertEquals(bad_flow.status_code, 404)

    def test_create(self):
        # test good flow create
        flow = self.client.post('/api/v1/flows', data=json.dumps({
            'flow_name': 'flow2',
            'stage_order': [2,1]
        }))
        self.assertEquals(flow.status_code, 201)

        # test duplicate name
        dupe_flow = self.client.post('/api/v1/flows', data=json.dumps({
            'flow_name': 'flow2',
            'stage_order': []
        }))
        self.assertEquals(dupe_flow.status_code, 400)

        # test bad stages
        bad_stages = self.client.post('/api/v1/flows', data=json.dumps({
            'flow_name': 'flow3',
            'stage_order': [1, 2, 100, 101]
        }))
        self.assertEquals(bad_stages.status_code, 400)
        self.assertTrue('100, 101' in bad_stages.data)
        self.assertTrue('1, 2' not in bad_stages.data)

        # test bad data format
        bad_format = self.client.post('/api/v1/flows', data=json.dumps([]))
        self.assertEquals(bad_format.status_code, 403)

    def test_update(self):
        update_url = '/api/v1/flow/{id}'.format(id=self.flow)

        flow = self.client.put(update_url, data=json.dumps({
            'stage_order': [2,1]
        }))
        self.assertEquals(flow.status_code, 200)

        # test bad stages
        bad_stages = self.client.put(update_url, data=json.dumps({
            'stage_order': [100, 101]
        }))
        self.assertEquals(bad_stages.status_code, 400)

        # test 404 for bad stage id
        no_stage = self.client.put('/api/v1/flow/10000000')
        self.assertEquals(no_stage.status_code, 404)

    def test_delete(self):
        flow = self.client.delete('/api/v1/flow/{id}'.format(id=self.flow))
        self.assertEquals(flow.status_code, 204)

        bad_flow = self.client.delete('/api/v1/flow/10000000')
        self.assertEquals(bad_flow.status_code, 404)


from typing import List

from fastapi import APIRouter, Depends, status
from sqlalchemy.orm import Session

from blog import schemas, oauth2, token
from blog.database import get_db
from blog.repository import blog

router = APIRouter(
    prefix="/blog",
    tags=["Blogs"]
)

@router.get("/", response_model=List[schemas.ShowBlog])
async def all(db: Session = Depends(get_db), current_user: schemas.User = Depends(oauth2.get_current_user)):
    return blog.get_all(db)

@router.post("/", status_code=status.HTTP_201_CREATED)
async def create_blog(request: schemas.Blog, db: Session = Depends(get_db),
                      current_user: schemas.User = Depends(oauth2.get_current_user)):
    return blog.create(request, db, current_user)

@router.get("/{id}", status_code=status.HTTP_200_OK, response_model=schemas.ShowBlog)
async def get_by_id(id: int, db: Session = Depends(get_db),
                    current_user: schemas.User = Depends(oauth2.get_current_user)):
    return blog.get_by_id(id, db)

@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete(id: int, db: Session = Depends(get_db), current_user: schemas.User = Depends(oauth2.get_current_user)):
    return blog.delete(id, db)

@router.put("/{id}", status_code=status.HTTP_202_ACCEPTED)
async def update(id: int, request: schemas.UpdateBlog, db: Session = Depends(get_db),
                 current_user: schemas.User = Depends(oauth2.get_current_user)):
    return blog.update(id, request, db)


"""Binary exponentiation"""
from typing import TypeVar

AnyNum = TypeVar("Num", bool, int, float, complex)

def exp(a: AnyNum, b: int) -> AnyNum:
    res = 1
    while b > 0:
        if b & 1 != 0:
            res = res * a
        a = a * a
        b >>= 1
    return res

def f(x):
    a = exp(x, 2)
    b = exp(2.0, x)

    return a * b

x = exp(1, 2)
y = exp(2.0, 3)

# f := Callable[[int], float]
# x := int
# y := float


import getopt
import os, requests
import sys
from configparser import ConfigParser
from bs4 import BeautifulSoup
from utils.configuration_files import ConfigFilePath

class ConfigReader(object):
    __config_file_path = ConfigFilePath.DEFAULT
    __instance = None
    __max_rows = None
    __region = "america"
    __soup = None
    __url = None

    @staticmethod
    def get_instance():
        if ConfigReader.__instance is None:
            ConfigReader()
        return ConfigReader.__instance

    def __new__(cls, *args, **kwargs):
        if cls.__instance is None:
            cls.__instance = super(ConfigReader, cls).__new__(cls)
            cls.__instance.set_region(cls.__instance.read_sys_args().get("-r"))
            cls.__instance.set_max_rows(cls.__instance.read_sys_args().get("-m"))
        return cls.__instance

    def get_max_rows(self):
        return self.__max_rows

    def get_region(self):
        return self.__region

    def get_soup(self):
        return self.__soup

    def get_url(self):
        return self.__url

    def set_max_rows(self, rows):
        self.__max_rows = rows

    def set_region(self, region):
        self.__region = region
        self.__url = self.parse_config_url()
        self.__soup = self.make_soup()

    def make_soup(self):
        requested_text = requests.get(self.__url).text
        return BeautifulSoup(requested_text, 'html.parser')

    def parse_config_url(self):
        config = ConfigParser()
        file = os.getcwd() + self.__config_file_path.value
        config.read(file)
        return config["urls"].get(self.__region)

    @staticmethod
    def read_sys_args():
        default_values = {"-r": "america", "-m": 15}
        try:
            opts, args = getopt.getopt(sys.argv[1:], "r:m:")
            if len(opts) == 0:
                raise Exception()
            else:
                for o, a in opts:
                    if o == "-r":
                        r_temp = {"-r": a}
                        default_values.pop("-r")
                        default_values.update(r_temp)
                    if o == "-m":
                        m_temp = {"-m": int(a)}
                        default_values.pop("-m")
                        default_values.update(m_temp)
        except Exception as err:
            print(err)
        return default_values


from typing import List

# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution(object):
    root: TreeNode = None
    ptr = root

    def buildTree(self, preorder: List[int]):
        root = TreeNode(preorder[0])

        for node in preorder[1:]:
            if ptr.val > node:
                ptr.left = TreeNode(node)
                ptr = ptr.left
            else:
                ptr.right = TreeNode(node)
                ptr = ptr.right

        return root

    def postorder(node: TreeNode):
        if node is None:
            return
        postorder(node.left)
        postorder(node.right)
        print(node.val)

# 입력
s = Solution()
tree = []
while True:
    try:
        tree.append(int(input()))
    except:
        break
root = s.buildTree(tree)
s.postorder(root)


try:
    import os
    import platform
    import time
    import subprocess
    import requests

    def savedWIFI():
        if platform.system() == "Windows":
                a = subprocess.check_output(['netsh', 'wlan', 'show', 'profile']).decode('utf-8').split('\n')
                a = [i.split(":")[1][1:-1] for i in a if "All User Profile" in i]

                for i in a:
                    results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', i, 'key=clear']).decode('utf-8').split('\n')
                    results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]

                    try:
                        print("{:<30}| {:<}". format(i, results[0]))
                    except IndexError:
                        print("{:<30}| {:<}". format(i, ""))

                time.sleep(1)
                main()

        elif platform.system() == "Linux":
            print("\n[ERROR] This command currently doesnt work on linux, check for new versions")
            time.sleep(1)
            main()

    def WifiBruteForce():
        url = "http://www.python.org"
        timeout = 5
        def createNewConnection(name, SSID, key):
            config = """<?xml version=\"1.0\"?>
        <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
            <name>"""+name+"""</name>
            <SSIDConfig>
                <SSID>
                    <name>"""+SSID+"""</name>
                </SSID>
            </SSIDConfig>
            <connectionType>ESS</connectionType>
            <connectionMode>auto</connectionMode>
            <MSM>
                <security>
                    <authEncryption>
                        <authentication>WPA2PSK</authentication>
                        <encryption>AES</encryption>
                        <useOneX>false</useOneX>
                    </authEncryption>
                    <sharedKey>
                        <keyType>passPhrase</keyType>
                        <protected>false</protected>
                        <keyMaterial>"""+key+"""</keyMaterial>
                    </sharedKey>
                </security>
            </MSM>
        </WLANProfile>"""
            if platform.system() == "Windows":
                command = "netsh wlan add profile filename=\""+name+".xml\""+" interface=Wi-Fi"
                with open(name+".xml", 'w') as file:
                    file.write(config)
            elif platform.system() == "Linux":
                command = "nmcli dev wifi connect '"+SSID+"' password '"+key+"'"
            os.system(command)
            if platform.system() == "Windows":
                os.remove(name+".xml")

        def connect(name, SSID):
            os.system("netsh wlan connect name=\""+name+"\" ssid=\""+SSID+"\" interface=Wi-Fi")

        def displayAvailableNetworks():
            os.system("netsh wlan show networks interface=Wi-Fi")

        print("[LOADING] Searching if connected to any network")

        try:
            request = requests.get(url, timeout=timeout)
            print("[-] Please disconnect your internet for this operation to work, try again later"), main()

        except (requests.ConnectionError, requests.Timeout) as exception:
            print("[LOADING] Loading program..."), time.sleep(1)

        connected = True
        while connected:
            try:
                displayAvailableNetworks()
                WIFI = input("WIFI Name: ")
                with open("Cracker.txt", "r") as f:
                    for line in f:
                        words = line.split()
                        if words:
                            print(f"Password: {words[0]}")

                            createNewConnection(WIFI, WIFI, words[0])
                            connect(WIFI, WIFI)

                            try:
                                request = requests.get(url, timeout=timeout)
                                connected = False
                                choice = input(f"[+] The password might have been cracked, are you connected to {WIFI} (y/N) ? ")
                                if choice == "y":
                                    print("\n[EXITING] Operation canceled")
                                    main()
                                elif choice == "n":
                                    print("\n[-] Operation continues\n")

                            except (requests.ConnectionError, requests.Timeout) as exception:
                                print("[LOADING] Loading program..."), time.sleep(1)

                print("[+] Operation complete")
                choice = input("See ACC Information (y/N) ? ")
                if choice == "y" or "Y":
                    print(f"[LOADING] Searching for {WIFI} Acc")
                    time.sleep(1)
                    os.system(f'netsh wlan show profile name="{WIFI}" key=clear')
                    main()
                elif choice == "n" or "N":
                    print("\n[EXITING] Exiting program...")
                    time.sleep(2)
                    main()

            except KeyboardInterrupt:
                print("\n[[EXITING] Aborting program...")
                exit()
            except:
                print("[ERROR] Fijate bien si esta bien escrito el txt.")
                main()

    def main():
        time.sleep(1)
        print("\n")
        print("Instagram Hack by Deimos = TOOLS =")
        time.sleep(1)
        print("1 - Sacar informacion de cuenta ")
        print("2 - Ataque a fuerza bruta")
        print("3 - Phising")
        print("0 - Exit")
        a = input("-> ")
        if a == "1":
            if platform.system() == "Linux":
                os.system("Hey bienvenido para continuar pon enter!")
                time.sleep(2)
                main()
            elif platform.system() == "Windows":
                print("Hey! losentimos pero esta herramienta no esta disponible para Windows! :C")
                time.sleep(1)
                main()

        elif a == "2":
            print("\n")
            savedWIFI()
        elif a == "3":
            print("\n")
            WifiBruteForce()
        elif a == "0":
            print("\n[EXITING] Exiting Program...")
            exit()
        else:
            os.system("cls")
            print("Unkown command, try again later")
            time.sleep(2)
            os.system("cls")
            main()
except KeyboardInterrupt:
        print("\n[[EXITING] Aborting program...")
        exit()
except ImportError:
        print("\n[ERROR] An error occured getting the modules, try installing requestss")
        exit()

print("""

███╗░░░███╗░█████╗░██████╗░  ██╗███╗░░██╗░██████╗████████╗░█████╗░░██████╗░██████╗░░█████╗░███╗░░░███╗  ██╗░░██╗
████╗░████║██╔═══╝░╚════██╗  ██║████╗░██║██╔════╝╚══██╔══╝██╔══██╗██╔════╝░██╔══██╗██╔══██╗████╗░████║  ╚██╗██╔╝
██╔████╔██║██████╗░░█████╔╝  ██║██╔██╗██║╚█████╗░░░░██║░░░███████║██║░░██╗░██████╔╝███████║██╔████╔██║  ░╚███╔╝░
██║╚██╔╝██║██╔══██╗░╚═══██╗  ██║██║╚████║░╚═══██╗░░░██║░░░██╔══██║██║░░╚██╗██╔══██╗██╔══██║██║╚██╔╝██║  ░██╔██╗░
██║░╚═╝░██║╚█████╔╝██████╔╝  ██║██║░╚███║██████╔╝░░░██║░░░██║░░██║╚██████╔╝██║░░██║██║░░██║██║░╚═╝░██║  ██╔╝╚██╗
╚═╝░░░░░╚═╝░╚════╝░╚═════╝░  ╚═╝╚═╝░░╚══╝╚═════╝░░░░╚═╝░░░╚═╝░░╚═╝░╚═════╝░╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░░░░╚═╝  ╚═╝░░╚═╝

██████╗░██████╗░░█████╗░░██████╗  ████████╗░█████╗░░█████╗░██╗░░░░░  ██╗░░░██╗██████╗░
██╔══██╗██╔══██╗██╔══██╗██╔════╝  ╚══██╔══╝██╔══██╗██╔══██╗██║░░░░░  ██║░░░██║╚════██╗
██║░░██║██║░░██║██║░░██║╚█████╗░  ░░░██║░░░██║░░██║██║░░██║██║░░░░░  ╚██╗░██╔╝░░███╔═╝
██║░░██║██║░░██║██║░░██║░╚═══██╗  ░░░██║░░░██║░░██║██║░░██║██║░░░░░  ░╚████╔╝░██╔══╝░░
██████╔╝██████╔╝╚█████╔╝██████╔╝  ░░░██║░░░╚█████╔╝╚█████╔╝███████╗  ░░╚██╔╝░░███████╗
╚═════╝░╚═════╝░░╚════╝░╚═════╝░  ░░░╚═╝░░░░╚════╝░░╚════╝░╚══════╝  ░░░╚═╝░░░╚══════╝

|------------><-----------------
|BY DEIMOS :))| Hack | V2 - V3 |
|------------><-----------------

""")
main()


import urllib.request
from lxml import etree
import requests
def Schedule(blocknum,blocksize,totalsize):
    '''blocknum:已经下载的数据块
    blocksize:数据块的大小
    totalsize:远程文件的大小'''
    per = 100.0 * blocknum * blocksize / totalsize
    if per > 100:
        per = 100
    print('当前下载进度: %d' % per)

user_agent = 'Mozilla/4.0 (compatible; MISE 5.5; Windows NT)'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Max OS X 10_12_3) AppleWebKit/537.36(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
r = requests.get('http://www.ivsky.com/tupian/huwai_gongyuan_v50710/', headers=headers) #需下载图片的链接
#使用lxml解析网页
html = etree.HTML(r.text)
img_urls = html.xpath('.//img/@src') #先找到所有的img
i = 0
path = 'D:\Python_Test/test_picture/' #指定下载文件路径
for img_url in img_urls:
    urllib.request.urlretrieve(img_url, path + 'img%s.jpg' % i, Schedule)
    i += 1


import json
import requests

def baiduSearch(page):
    retData = requests.post(
        "https://talent.baidu.com/httservice/getPostListNew?recruitType=SOCIAL&pageSize=20&keyWord=&curPage=" + str(
            page) + "&projectType=",
        headers={"Referer": "https://talent.baidu.com/jobs/social-list"})
    return retData.text

ResultJson = json.loads(baiduSearch(1))
# 初次请求
total = int(ResultJson["data"]["total"])
NeedIndex = int((total - total % 20) / 20 + 1)
# 计算翻页数

for PageIndex in range(1, NeedIndex):
    ResultJson = json.loads(baiduSearch(PageIndex))
    PageData = ResultJson["data"]["list"]
    for DataIndex in range(0, 19):
        Name = PageData[DataIndex]["name"]  # 职位名
        ServiceCondition = PageData[DataIndex]["serviceCondition"]  # 职位条件
        WorkContent = PageData[DataIndex]["workContent"]  # 工作内容
        PostType = PageData[DataIndex]["postType"]  # 工作内容
        print(Name, ServiceCondition, WorkContent, PostType)


#!/usr/bin/python3
import sys
import json

class LineByLineFile():
 def __init__(self, filename):
  self.lineno = -1
  self.file = open(filename,"r")

 def readline(self):
  line = self.file.readline()
  self.lineno += 1
  return line

 def close(self):
  self.file.close()

class FromGratextaFile():
 def __init__(self,file):
  if type(file) == str:
   file = LineByLineFile(sys.argv[1])
  self.bookmarks = {}
  self.cells = {}

  #READ BOOKMARKS SECTION
  while True:
   line = file.readline()
   if not line:
    break
   if line.strip() == "CELLS":
    break
   elif line.strip() == "BOOKMARKS":
    pass
   elif not line.strip():
    pass
   elif ':' in line:
    [bookmark_name_string,destination_string] = line.split(':')
    bookmark_name = json.loads(bookmark_name_string.strip())
    self.bookmarks[bookmark_name] = json.loads(destination_string.strip())
   else:
    sys.exit("Error on line %n: Expected a bookmark entry."%file.lineno)

  #READ CELLS SECTION
  while True:
   line = file.readline()
   if not line:
    break
   if not line.strip():
     pass
   else:
    cell_id = line.rstrip('\n')
    cell = {}
    # READ PROPERTIES DICT
    while True:
     line = file.readline()
     if not line:
      break
     if line == " ---\n":
      break
     if line.startswith(' '):
      try:
       [key,value] = line.split(":")
      except ValueError:
       print("Error on line %i: expected dictionary entry."%file.lineno)
       print(line)
       exit()
      key = key.strip()
      value = value.strip()
      if key == "up" or key == "down" or key == "tags":
       try:
        value = json.loads(value)
       except ValueError:
        print("Error on line %i: failed to parse value."%file.lineno)
        print(line)
       cell[key] = value
      elif key == "left" or key == "right":
       link = {}
       try:
        json.loads(value)
       except json.decoder.JSONDecodeError as e:
        try:
         link["service"] = json.loads(value[:e.pos])
        except json.decoder.JSONDecodeError as e2:
         print("Error on line %i: Failed to parse string."%file.lineno)
         print(line)
         print(e)
         exit(1)
        try:
         rest = value[e.pos:]
         json.loads(rest)
        except json.decoder.JSONDecodeError as e1:
         try:
          path_string = rest[:e1.pos]
          link["path"] = json.loads(path_string)
         except ValueError as e3:
          print("Error on line %i: Failed to parse string %s."%(file.lineno,path_string))
          print(e3)
          print(line)
          exit(1)
         link["cell_id"] = json.loads(rest[e1.pos:])
         cell[key] = link
    # read cell text
    text = ""
    while True:
     line = file.readline()
     if not line or line == '\n':
      if text:
       cell["text"] = text[:-1]
      else:
       cell["text"] = ""
      self.cells[cell_id] = cell
      break
     elif line.startswith(' '):
      text += line[1:]
    text = ""

 def get_json(self):
  obj = {"bookmarks":self.bookmarks,"cells":self.cells}
  return json.dumps(obj)

gd = FromGratextaFile(sys.argv[1])
print(gd.get_json())


""" Recognize digits drawn by user on a matplotlib plot

This script loads a model fit to MNIST data, then displays a blank
image in a matplotlib plot. You can draw on the plot with your mouse,
and erase it by pressing any key. As you draw, the title of the plot
will be updated with the digit recognized by the model.
"""
import argparse
import matplotlib.pyplot as plt
import numpy as np
from keras.models import load_model
import interactiveplot as ip
import utilities

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--modelfile', default='mnist_model.h5', type=str, help='Path to trained model stored in a file that keras.models.load_model will understand')

    args = parser.parse_args()

    model = load_model(args.modelfile)

    # plt.ion is required for this kind of interactivity
    plt.ion()

    fig = plt.figure()
    ax = fig.add_subplot(111)

    data = np.zeros((28,28))

    # If I replace np.random.rand in ax.imshow with np.zeros or data, interactivity
    # breaks. Initializing with random data and then immediately changing to zeros
    # works fine though. I have *no idea* why this happens, and I'm definitely going
    # come back to this and tinker with it in the future.
    plot = ax.imshow(np.random.rand(28,28))
    plot.set_data(data)

    model = load_model(args.modelfile)

    def recognition_func(image):
        return utilities.recognize_digit(model, image)

    interactivePlot = ip.InteractiveMnistPlot(ax, plot, data, recognition_func)

    # block=True is necessary for interactivity
    plt.show(block=True)


# coding=utf-8
from flask import Flask, render_template, request, send_from_directory, send_file, redirect, url_for, abort
import os, json, zipfile, datetime, time, io
from cache import Cache
from config import Config as cfg

cache = Cache()

app = Flask(__name__)

@app.route('/')
@app.route('/index/')
def index():
    return render_template(
        'index.html',
        title='Главная',
        cache=cache,
        dir=cfg.DATA_FILE,
    )

@app.route('/scan/')
def scan():
    cache.scan()
    return render_template(
        'scan.html',
        title='Сканирование каталога',
        cache=cache,
    )

@app.route('/problems')
def problems():
    return render_template(
        'problems.html',
        title='Разрешение конфликтов',
        cache=cache,
    )

@app.route('/problems_resolve', methods=['POST'])
def problems_resolve():
    f = request.form.to_dict(flat=False)
    cache.resolve_problems(f)
    return render_template(
        'problems_resolve.html',
        title='Разрешение конфликтов',
        cache=cache,
        form=f,
    )

@app.route('/dups')
def dups():
    cache.find_dups()
    return render_template('dups.html', title='Поиск дубликатов', cache=cache.memory, len=cache.len())

@app.route('/md5_check')
def md5_check():
    cache.md5_check()
    return render_template('md5_check.html', title='Контрольная сумма', cache=cache.memory, len=cache.len())

@app.route('/favicon.ico')
def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico',
                               mimetype='image/vnd.microsoft.icon')

@app.route('/photo')
def photo():
    photo_id = request.args.get('photo_id')
    path = f"{cfg.DATA_FILE}, {photo_id}"
    print(path)
    return send_file(path)

@app.route('/view')
def view():
    folder = request.args.get('folder')
    photo_id = request.args.get('photo_id')
    if not (folder and photo_id):
        folder = "to_view"
        if folder in cache.memory:
            if cache.memory[folder]:
                photo_id = cache.memory[folder][0]
            else:
                abort(404, "Нет фотографий для просмотра")
        else:
            abort(404, "Нет фотографий для просмотра")
        return redirect(url_for('view', photo_id=photo_id, folder=folder))

    return render_template('view.html', title='Просмотр фото', photo=photo_id,
                           folder=folder, cache=cache.memory, len=cache.len())

@app.route('/apply', methods=['POST'])
def apply():
    photo = request.form["photo_id"]
    l = [key for key in request.form if key != 'photo_id']
    cache.active_tags = l
    if l:
        if 'new' in cache.memory:
            if photo in cache.memory['new']:
                cache.memory['old'][photo] = cache.memory['new'][photo]
        cache.memory['old'][photo]['tags'] = l
        if 'tags' not in cache.memory:
            cache.memory['tags'] = {key: 1 for key in l}
        else:
            for tag in l:
                if tag in cache.memory['tags']:
                    cache.memory['tags'][tag] += 1
                else:
                    cache.memory['tags'][tag] = 1
        cache.dump()
        if 'to_view' in cache.memory:
            if photo in cache.memory['to_view']:
                cache.memory['to_view'].remove(photo)
        if 'new' in cache.memory:
            if photo in cache.memory['new']:
                del cache.memory['new'][photo]
    return redirect(url_for('view'))

@app.route('/search', methods=['POST', 'GET'])
def search():
    if request.method == 'POST':
        cache.search = {}
        l = [key for key in request.form]
        for ph in cache.old:
            if search_tags(l, cache.old[ph]):
                if not ph in cache.to_delete:
                    cache.search[ph] = cache.old[ph]
    return render_template('search.html', title='Найденные фото', cache=cache.memory, len=cache.len())

@app.route('/tags')
def tags():
    return render_template('tags.html', title='Тэги', cache=cache.memory, len=cache.len())

@app.route('/export')
def export():
    memory_file = io.BytesIO()
    zf = zipfile.ZipFile(memory_file, 'w')
    for file in cache.search:
        print(file)
        zf.write(file, arcname=file[file.rfind("\\") + 1:])
    print(zf.filelist)
    zf.close()
    memory_file.seek(0)
    return send_file(io.BytesIO(memory_file.read()), attachment_filename=str(datetime.datetime.now()) + ".zip",
                     as_attachment=True)

@app.route('/arch')
def arch():
    return send_file("D:\\Share\\Сканер.zip", as_attachment=True)

app.run(debug=True, host="0.0.0.0")


import warnings
from collections import OrderedDict
from io import BytesIO

import numpy as np
import pandas
from pandas.core.dtypes.cast import find_common_type
from pandas.core.dtypes.concat import union_categoricals
from pandas.io.common import infer_compression

from modin.core.execution.ray.implementations.cudf_on_ray.partitioning.partition_manager import (
    GPU_MANAGERS,
)
from modin.core.io.file_dispatcher import OpenFile
from modin.core.storage_formats.pandas.utils import split_result_of_axis_func_pandas
from modin.error_message import ErrorMessage

def _split_result_for_readers(axis, num_splits, df):  # pragma: no cover
    """Splits the DataFrame read into smaller DataFrames and handles all edge cases.

    Args:
        axis: Which axis to split over.
        num_splits: The number of splits to create.
        df: The DataFrame after it has been read.

    Returns:
        A list of pandas DataFrames.
    """
    splits = split_result_of_axis_func_pandas(axis, num_splits, df)
    if not isinstance(splits, list):
        splits = [splits]
    return splits

def find_common_type_cat(types):
    if all(isinstance(t, pandas.CategoricalDtype) for t in types):
        if all(t.ordered for t in types):
            return pandas.CategoricalDtype(
                np.sort(np.unique([c for t in types for c in t.categories])[0]),
                ordered=True,
            )
        return union_categoricals(
            [pandas.Categorical([], dtype=t) for t in types],
            sort_categories=all(t.ordered for t in types),
        ).dtype
    else:
        return find_common_type(types)

class cuDFParser(object):
    @classmethod
    def get_dtypes(cls, dtypes_ids):
        return (
            pandas.concat(cls.materialize(dtypes_ids), axis=1)
            .apply(lambda row: find_common_type_cat(row.values), axis=1)
            .squeeze(axis=0)
        )

    @classmethod
    def single_worker_read(cls, fname, *, reason, **kwargs):
        ErrorMessage.default_to_pandas(reason=reason)
        # Use default args for everything
        pandas_frame = cls.parse(fname, **kwargs)
        if isinstance(pandas_frame, pandas.io.parsers.TextFileReader):
            pd_read = pandas_frame.read
            pandas_frame.read = (
                lambda *args, **kwargs: cls.query_compiler_cls.from_pandas(
                    pd_read(*args, **kwargs), cls.frame_cls
                )
            )
            return pandas_frame
        elif isinstance(pandas_frame, (OrderedDict, dict)):
            return {
                i: cls.query_compiler_cls.from_pandas(frame, cls.frame_cls)
                for i, frame in pandas_frame.items()
            }
        return cls.query_compiler_cls.from_pandas(pandas_frame, cls.frame_cls)

    infer_compression = infer_compression

class cuDFCSVParser(cuDFParser):
    @classmethod
    def parse(cls, fname, **kwargs):
        warnings.filterwarnings("ignore")
        num_splits = kwargs.pop("num_splits", None)
        start = kwargs.pop("start", None)
        end = kwargs.pop("end", None)
        index_col = kwargs.get("index_col", None)
        gpu_selected = kwargs.pop("gpu", 0)

        if start is not None and end is not None:
            put_func = cls.frame_partition_cls.put

            # pop "compression" from kwargs because bio is uncompressed
            with OpenFile(fname, "rb", kwargs.pop("compression", "infer")) as bio:
                if kwargs.get("encoding", None) is not None:
                    header = b"" + bio.readline()
                else:
                    header = b""
                bio.seek(start)
                to_read = header + bio.read(end - start)
            pandas_df = pandas.read_csv(BytesIO(to_read), **kwargs)
        else:
            # This only happens when we are reading with only one worker (Default)
            pandas_df = pandas.read_csv(fname, **kwargs)
            num_splits = (
                1  # force num_splits to be 1 here because we don't want it partitioning
            )
        if index_col is not None:
            index = pandas_df.index
        else:
            index = len(pandas_df)
        partition_dfs = _split_result_for_readers(1, num_splits, pandas_df)
        key = [
            put_func(GPU_MANAGERS[gpu_selected], partition_df)
            for partition_df in partition_dfs
        ]
        return key + [index, pandas_df.dtypes]


import yt_dlp
import logging
import argparse
import requests
import pandas as pd
from youtubesearchpython import *

def get_episodes(channel_id,last_episode,logging):

    # Get playlist
    playlist = Playlist(playlist_from_channel_id(channel_id))
    while playlist.hasMoreVideos:
        print('Getting more videos...')
        playlist.getNextVideos()
        print(f'Videos Retrieved: {len(playlist.videos)}')

    # Store
    stor_metadata=pd.DataFrame()
    for v in playlist.videos:
        try:
            ep_number = int(v['title'].split("|")[-1].split("#")[-1])
            stor_metadata.loc[v['title'],'number']=ep_number
            stor_metadata.loc[v['title'],'link']=v['link']
            stor_metadata.loc[v['title'],'title']=v['title']
            stor_metadata.loc[v['title'],'img']=v['thumbnails'][3]['url']
        except:
            logging.info("Metadata extraction failed on %s", v['title'])

    stor_metadata = stor_metadata[stor_metadata.number > last_episode]
    return stor_metadata

def dump_audio(stor_metadata,logging):
    # Iterate through episodes
    for ix in stor_metadata.index:
        ep_number=int(stor_metadata.loc[ix,'number'])
        logging.info("Dumping audio for: %s"%ep_number)
        img_url=stor_metadata.loc[ix,'img']
        ep_link=stor_metadata.loc[ix,'link']
        # Write img
        with open("../public/0%s.jpg"%str(ep_number), 'wb') as f:
            response = requests.get(img_url)
            f.write(response.content)
        # Write audio
        ydl_opts = {
        'format': 'm4a/bestaudio/best',
        'outtmpl': 'audio/%s.m4a'%str(ep_number),
        'noplaylist': True,
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'm4a',
        }]}
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            error_code = ydl.download(ep_link)
    # Save
    stor_metadata.reset_index().to_csv("audio_transcription/episodes.csv")

def get_last_episode():
    parser = argparse.ArgumentParser(description='Process last_episode')
    parser.add_argument('last_episode', type=int, help='the last episode number')
    args = parser.parse_args()
    return args.last_episode

def main():
    # Set up logger
    logging.basicConfig(filename='audio_dump.log',
                        filemode='w',
                        level=logging.DEBUG)

    # Lex Fridman
    last_episode = get_last_episode()
    channel_id = "UCSHZKyawb77ixDdsGog4iWA"
    stor_metadata = get_episodes(channel_id,last_episode,logging)
    dump_audio(stor_metadata,logging)

if __name__ == '__main__':
    main()


import wikipedia
import nltk.data
import re

sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')

#print wikipedia.search("Barack")
#print wikipedia.suggest("Barak Obama")
#print wikipedia.search("Ford", results=3)
#print wikipedia.summary("Apple III", sentences=1)

#a = re.compile("jpg")

def searchWikiPage(keyword):
	output={}
	content = wikipedia.page(keyword)
	senteces=sent_detector.tokenize(content.summary.strip())
	output['title']=content.title.encode('utf-8')
	output['summary']=senteces[0]
	i=0
	photos=[]
	for item in content.images:
		if '.jpg' in item.lower():
			photos.append(item)
		i+=1
	output['image']=photos[0].encode('utf-8')
	output['url']=content.url.encode('utf-8')
	return output

#print searchWikiPage("Cristiano Ronaldo")


import logging
import os
from typing import Dict

from monai.transforms import Invertd, SaveImaged

import monailabel
from monailabel.interfaces.app import MONAILabelApp
from monailabel.interfaces.tasks.infer_v2 import InferTask
from monailabel.interfaces.tasks.scoring import ScoringMethod
from monailabel.interfaces.tasks.strategy import Strategy
from monailabel.interfaces.tasks.train import TrainTask
from monailabel.tasks.activelearning.first import First
from monailabel.tasks.activelearning.random import Random
from monailabel.tasks.infer.bundle import BundleInferTask
from monailabel.tasks.scoring.epistemic_v2 import EpistemicScoring
from monailabel.tasks.train.bundle import BundleTrainTask
from monailabel.utils.others.generic import get_bundle_models, strtobool

logger = logging.getLogger(__name__)

class MyApp(MONAILabelApp):
    def __init__(self, app_dir, studies, conf):
        self.models = get_bundle_models(app_dir, conf)
        # Add Epistemic model for scoring
        self.epistemic_models = (
            get_bundle_models(app_dir, conf, conf_key="epistemic_model") if conf.get("epistemic_model") else None
        )
        if self.epistemic_models:
            # Get epistemic parameters
            self.epistemic_max_samples = int(conf.get("epistemic_max_samples", "0"))
            self.epistemic_simulation_size = int(conf.get("epistemic_simulation_size", "5"))
            self.epistemic_dropout = float(conf.get("epistemic_dropout", "0.2"))

        super().__init__(
            app_dir=app_dir,
            studies=studies,
            conf=conf,
            name=f"MONAILabel - Zoo/Bundle ({monailabel.__version__})",
            description="DeepLearning models provided via MONAI Zoo/Bundle",
            version=monailabel.__version__,
        )

    def init_infers(self) -> Dict[str, InferTask]:
        infers: Dict[str, InferTask] = {}
        #################################################
        # Models
        #################################################
        for n, b in self.models.items():
            i = BundleInferTask(b, self.conf)
            logger.info(f"+++ Adding Inferer:: {n} => {i}")
            infers[n] = i
        return infers

    def init_trainers(self) -> Dict[str, TrainTask]:
        trainers: Dict[str, TrainTask] = {}
        if strtobool(self.conf.get("skip_trainers", "false")):
            return trainers

        for n, b in self.models.items():
            t = BundleTrainTask(b, self.conf)
            if not t or not t.is_valid():
                continue

            logger.info(f"+++ Adding Trainer:: {n} => {t}")
            trainers[n] = t
        return trainers

    def init_strategies(self) -> Dict[str, Strategy]:
        strategies: Dict[str, Strategy] = {
            "random": Random(),
            "first": First(),
        }

        logger.info(f"Active Learning Strategies:: {list(strategies.keys())}")
        return strategies

    def init_scoring_methods(self) -> Dict[str, ScoringMethod]:
        methods: Dict[str, ScoringMethod] = {}
        if not self.conf.get("epistemic_model"):
            return methods

        for n, b in self.epistemic_models.items():
            # Create BundleInferTask task with dropout instantiation for scoring inference
            i = BundleInferTask(
                b,
                self.conf,
                train_mode=True,
                skip_writer=True,
                dropout=self.epistemic_dropout,
                post_filter=[SaveImaged, Invertd],
            )
            methods[n] = EpistemicScoring(
                i, max_samples=self.epistemic_max_samples, simulation_size=self.epistemic_simulation_size
            )
            if not methods:
                continue
            methods = methods if isinstance(methods, dict) else {n: methods[n]}
            logger.info(f"+++ Adding Scoring Method:: {n} => {b}")

        logger.info(f"Active Learning Scoring Methods:: {list(methods.keys())}")
        return methods

"""
Example to run train/infer/scoring task(s) locally without actually running MONAI Label Server
"""

def main():
    import argparse
    from pathlib import Path

    from monailabel.config import settings

    settings.MONAI_LABEL_DATASTORE_AUTO_RELOAD = False
    settings.MONAI_LABEL_DATASTORE_FILE_EXT = ["*.png", "*.jpg", "*.jpeg", ".nii", ".nii.gz"]
    os.putenv("MASTER_ADDR", "127.0.0.1")
    os.putenv("MASTER_PORT", "1234")

    logging.basicConfig(
        level=logging.INFO,
        format="[%(asctime)s] [%(process)s] [%(threadName)s] [%(levelname)s] (%(name)s:%(lineno)d) - %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
        force=True,
    )

    home = str(Path.home())
    studies = f"{home}/Datasets/Radiology"

    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--studies", default=studies)
    args = parser.parse_args()

    app_dir = os.path.dirname(__file__)
    studies = args.studies

    app = MyApp(app_dir, studies, {"preload": "true", "models": "spleen_ct_segmentation_v0.1.0"})
    train(app)

def infer(app):
    import json
    import shutil

    res = app.infer(
        request={
            "model": "spleen_ct_segmentation_v0.1.0",
            "image": "image",
        }
    )

    print(json.dumps(res, indent=2))
    shutil.move(res["label"], os.path.join(app.studies, "test"))
    logger.info("All Done!")

def train(app):
    app.train(
        request={
            "model": "spleen_ct_segmentation_v0.1.0",
            "max_epochs": 2,
            "multi_gpu": False,
            "val_split": 0.1,
            "val_interval": 1,
        },
    )

if __name__ == "__main__":
    main()


from flask import Blueprint, Flask, render_template, redirect, request, url_for

import repositories.employee_repository as employee_repository
import repositories.branch_repository as branch_repository
import repositories.sales_repository as sales_repository
import repositories.product_repository as product_repository
from models.sale import Sale
from models.employee import Employee

logger_blueprint = Blueprint("logger", __name__)

@logger_blueprint.route("/logger")
def index():
    employees = employee_repository.select_all()
    return render_template("logger/index.html", employees = employees)

@logger_blueprint.route("/logger/<id>")
def new(id):
    branch_id = int(id)
    inventory = branch_repository.branch_inventory(branch_id)
    employees = employee_repository.select_all()
    return render_template("logger/new.html", branch_id = branch_id, inventory = inventory, employees = employees)

@logger_blueprint.route("/logger/<id>", methods = ["POST"])
def add(id):
    employee_id = request.form["employee_id"]
    product_id = request.form["product_id"]
    quantity = request.form["quantity"]
    product = product_repository.select(product_id)
    stock_check = product.quantity - int(quantity)
    check = False
    branch_sales = sales_repository.sales_by_branch(id)
    for sale in branch_sales:
        if sale.product_id == int(product_id):
            check = True
    if stock_check >= 0:
        # employee = employee_repository.select()
        branch_id = int(id)
        # sale = sales_repository.sale_by_product(product_id)
        if check:
            sale = sales_repository.sale_by_product(product_id)
            sale_update = Sale(product_id, employee_id, branch_id, quantity, sale.id)
            sales_repository.update(sale_update)
            product.quantity -= int(quantity)
            product_repository.update(product)
            return redirect(url_for("logger.index"))
        else:
            new_sale = Sale(product_id, employee_id, id, quantity)
            sales_repository.save(new_sale)
            return redirect(url_for("logger.index"))
    else:
        difference = stock_check * -1
        return f"Not enough stock for product, you need to order {difference} more"

@logger_blueprint.route("/logger/<id>/view")
def view(id):
    employee_id = int(id)
    employee = employee_repository.select(employee_id)
    employee_sales = employee.total_sales(employee_id)
    branches = branch_repository.select_all()
    return render_template("logger/view.html", employee = employee, sales = employee_sales, branches = branches)


from aria2p import downloads
from flask import Flask, request, render_template
from flask_restful import Api, Resource, reqparse
import aria2p
from time import sleep
import os
import werkzeug
from flask_cors import CORS
os.system('sh aria2.sh&')

app = Flask(__name__)
CORS(app)
api = Api(app)

global aria2
aria2 = aria2p.API(
    aria2p.Client(
        host="http://localhost",
        port=6800,
        secret=""
    )
)

@app.route("/", methods=["GET"])
def f():
    return render_template("index.html")

@app.route("/add", methods=["POST", "GET"])
def addMag():
    if request.method == "POST":
        magnet = request.files['file']

        f = request.files['file']
        f.save(f.filename)
        download = aria2.add_torrent(f.filename)

    return render_template("index.html")

@app.route("/status", methods=["GET"])
def OngoingStats():

    downloads = aria2.get_downloads()

    responses = []

    for download in downloads:

        name_of_torrent = download.name
        stat = download.status
        etaa = download.eta_string(2)
        speed = download.download_speed_string(True)
        pro = download.progress_string(2)
        ulspeed = download.upload_speed_string(True)
        seeder = download.num_seeders
        progress = pro[0:1]

        responses.append({"name": name_of_torrent,
                          "status": stat,
                          "speed": speed,
                          "progress": progress,
                          "eta": etaa,
                          "uplo": ulspeed,
                          "seeds": seeder
                          }
                         )
    return {"resp": responses}

@app.route("/pause", methods=["GET"])
def pause():

    downloads = aria2.get_downloads()
    aria2.get_options(downloads)
    aria2.pause(downloads)
    return render_template("index.html")

@app.route("/resume", methods=["GET"])
def resume():

    downloads = aria2.get_downloads()
    print(downloads)
    aria2.resume(downloads)
    return render_template("index.html")

@app.route("/remove", methods=["POST", "GET"])
def remove():

    downloads = aria2.get_downloads
    print(downloads)
    aria2.remove_all(downloads)
    return render_template("index.html")

if __name__ == "__main__":
    app.run(debug=True)


from django.shortcuts import render
from django.http import HttpResponse
import pickle

# Create your views here.

def home(request):
    """
    TO render home page
    :param request:
    :return:
    """
    return render(request, 'base/index.html')

def profiling(request):
    """
    To render Pandas profiling report
    :param request:
    :return:
    """
    return render(request, "base/AI4I_Profiling.html")

def predictions(request):
    try:
        processtemp = float(request.POST['processtemp'])
        rotationalspeed = float(request.POST['rotationalspeed'])
        torque = float(request.POST["torquenm"])
        toolwear = float(request.POST["toolwearmin"])
        twf = int(request.POST['toolwearf'])
        hdf = int(request.POST['heatfail'])
        pwf = int(request.POST['powerfail'])
        osf = int(request.POST['Verstrainfailure'])
        rnf = int(request.POST['randomFail'])

        # print(processtemp, rotationalspeed, torque, toolwear, twf, hdf, pwf, osf, rnf)

        # Making all the above values into StanderScaler()
        std_scaler = pickle.load(open("base//Pickle files//standard_scaler.pickle", 'rb'))

        x = [[processtemp, rotationalspeed, torque, toolwear, twf, hdf, pwf, osf, rnf]]
        x_standard = std_scaler.transform(x)

        # print("x_standard : ", x_standard)

        # Now doing prediction on scaled data
        lasso = pickle.load(open("base//Pickle files//Lasso(alpha=0.00014660150309437934).pickle", 'rb'))
        predicted_x = lasso.predict(x_standard)
        predicted_air_temp = round(predicted_x[0], 2)
        # print("predicted_air_temp : ", predicted_x)
        return render(request, "base/result.html", {'predicted_air_temp': predicted_air_temp})

    except Exception as e:
        print(f""" ERROR IN : predictions(views) : {str(e)}\n""")