import re
from bs4 import BeautifulSoup
import json
import requests
from datetime import date

def get_wishlist(steam_id):
    """ получаем список игр из вишлиста вводая публичный стим id """
    url = "http://steamcommunity.com/profiles/{}/wishlist".format(steam_id)
    req = requests.get(url).text
    soup = BeautifulSoup(req, 'html.parser')
    print(soup.title.string)
    foo = soup.find(string=re.compile("var g_rgWishlistData = ")).split(';')[0][25:]
    l = eval(foo)
    temp = []
    for item in l:
        temp.append(item['appid'])
    return temp #[250620, 274940, 329430, 448510, 578080]

def get_game_id_time_price(game_list):
    data_for_graf = []
    now = date.today().strftime("%y.%m.%d")
    game_data = {}
    for g in game_list:
        req = requests.get('http://store.steampowered.com/app/{}/'.format(g), cookies=cookie).text
        soup = BeautifulSoup(req, 'html.parser')
        foo = soup.find(itemprop="price") #<meta content="509" itemprop="price"/>
        # print(foo)
        temp = str(foo).split('"') #['<meta content=', '509', ' itemprop=', 'price', '/>']
        # print(temp)
        if temp == ['None']:
            game_data["game id"] = g
            game_data["date"] = 'None'
            game_data["price"] = 'None'
            pass
        else:
            game_data["game id"] = g
            game_data["date"] = now
            game_data["price"] = temp[1]
        data_for_graf.append(game_data)
        # game_data = {}
    return data_for_graf

def get_cookies(item):
    """
     получаем куки
     сейчас не используется файл куки сохранён в корне
     """
    session = requests.session()
    p = session.post("http://store.steampowered.com/agecheck/app/{0}/".format(item))
    # print('headers', p.headers)
    print('cookies', requests.utils.dict_from_cookiejar(session.cookies))
    # print('html',  p.text)
    cook = requests.utils.dict_from_cookiejar(session.cookies)
    return cook

def store_data(value):
    """ сохраняем данные в json """
    file = 'game_data.json'
    with open(file, 'a') as f_obj:
        if len(value[0]) == 0:
            print("пусто")
            pass
        else:
            json.dump(value, f_obj)
            print('ok')

def load_gamedata():
    """ загружаем файл для анализа """
    filename = 'game_data.json'
    with open(filename) as f:
        plot_dicts = json.load(f)
        print(plot_dicts)

def load_cookie():
    filename = 'cookie.json'
    with open(filename, 'r') as f:
        cookie = json.load(f)
        return cookie

id = '76561198129539168'  # id пользователя

# http://steamcommunity.com/profiles/76561198175913657
# http://steamcommunity.com/profiles/76561198129539168

cookie = load_cookie()

print('-------games-------')
games = get_wishlist(id)  # получаю список игр
print(games)

print('------list_for_analiz--------')
# list_for_analiz = get_game_id_time_price(games)
list_for_analiz = get_game_id_time_price([250620, 274940, 329430, 448510, 578080])
print(list_for_analiz)

# store_data(list_for_analiz) # сохраняем данные


import PIL
from PIL import Image,ImageTk
import cv2
from tkinter import *
import tkinter.messagebox
from tkinter import font as tkFont
import time
import threading
import os
import RPi.GPIO as GPIO

import face_recognition
import cv2
import numpy as np

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)

MODEL = 'cnn'
TOLERANCE = 0.10

unlock1=True
unlock2=True
unlock3=True
unlock4=True
unlock5=True

LockerStatus="No State"

global TreadStatus
TreadStatus=True
global Stop_thread
Stop_thread=False
global Stop_thread2
Stop_thread2=False
global TreadState
TreadState=True
global CheckLocker
CheckLocker=True
Deposit_Mode=True
Claim_Mode=True

video_capture = cv2.VideoCapture(0)
width, height = 500, 500

video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

root = Tk()
root.geometry("1280x720")

root.configure(background='#FFFAFA')

root.bind('<Escape>', lambda e: root.quit())

def Stop_showFrame():
     global Stop_thread
     Stop_thread=True
     hide_Show_frame()
     MainFrame()

def Camera_Tread():
     global t1
     global Stop_thread
     t1=threading.Thread(target=show_frame)
     t1.start()
     print("t1-started Main")

def Start_showFrame():
    global Stop_thread
    Stop_thread=False
    global TreadStatus
    Spacer2.grid(row=0,column=1)
    Frame_imageCapture.grid(row=0,column=0)
    lmain.grid(row=0,column=0)
    imageCaptureFrame.grid(row=0,column=0,padx=30,pady=50)

    if TreadStatus:
        Camera_Tread()
        TreadStatus=False
    else:
         if (t1.is_alive):
             print("T1 is alive")
         else:
            print("T1 start")
            Stop_thread=True
    print(LockerStatus)

def Capture_Image(index):
        a=tkinter.messagebox.askyesno("Face Capture ","Camera will take a photo of your face to Lock locker 2, Do you want to Proceed ? ")
        global unlock1,unlock2,unlock3,unlock4,unlock5
        time.sleep(1.5)
        if a==True:
            print("capture picture")
            ret, frame = video_capture.read()

            if ret:
                 cv2.imwrite(index+".jpg",frame)

                 image=face_recognition.load_image_file(index+".jpg")
                 face_locations = face_recognition.face_locations(image)
                 print("I found {} face(s) in this photograph.".format(len(face_locations)))

                 faces=len(face_locations)
                 if faces==1:
                     if index=="locker1":
                          unlock1=True
                          print("locker 1 lock")
                          tkinter.messagebox.showinfo("face Capture","Face Capture Complete Locker 1 will be Locked ")
                          GPIO.setmode(GPIO.BCM)
                          GPIO.setup(17, GPIO.OUT)
                          GPIO.output(17,True)
                          print("Locker on 1")
                          time.sleep(4)
                          GPIO.output(17,False)
                          GPIO.cleanup()

                     elif index=="locker2":
                          unlock2=True
                          print("locker 2 unlock")
                          tkinter.messagebox.showinfo("face Capture","Face Capture Complete Locker 2 will be Locked ")
                          GPIO.setmode(GPIO.BCM)
                          GPIO.setup(27, GPIO.OUT)
                          GPIO.output(27,True)
                          print("Locker on 1")
                          time.sleep(4)
                          GPIO.output(27,False)
                          GPIO.cleanup()
                     elif index=="locker3":
                          unlock3=True
                          print("locker 3 unlock")
                          tkinter.messagebox.showinfo("face Capture","Face Capture Complete Locker 3 will be Locked ")
                          GPIO.setmode(GPIO.BCM)
                          GPIO.setup(22, GPIO.OUT)
                          GPIO.output(22,True)
                          print("Locker on 1")
                          time.sleep(4)
                          GPIO.output(22,False)
                          GPIO.cleanup()
                     elif index=="locker4":
                          unlock4=True
                          print("locker 4 unlock")
                          tkinter.messagebox.showinfo("face Capture","Face Capture Complete Locker 4 will be Locked ")
                          GPIO.setmode(GPIO.BCM)
                          GPIO.setup(23, GPIO.OUT)
                          GPIO.output(23,True)
                          print("Locker on 1")
                          time.sleep(4)
                          GPIO.output(23,False)
                          GPIO.cleanup()
                     elif index=="locker5":
                          unlock5=True
                          print("locker 5 unlock")
                          tkinter.messagebox.showinfo("face Capture","Face Capture Complete Locker 5  will be Locked ")
                          GPIO.setmode(GPIO.BCM)
                          GPIO.setup(24, GPIO.OUT)
                          GPIO.output(24,True)
                          print("Locker on 1")
                          time.sleep(4)
                          GPIO.output(24,False)
                          GPIO.cleanup()

                     Stop_showFrame()
                 else:
                      tkinter.messagebox.showinfo("face Capture","No face Identified")

imageCaptureFrame=Frame(root)
Frame_imageCapture=Frame(imageCaptureFrame, bg="#FFA500",width=1400,height=1900)
lmain = Label(Frame_imageCapture)
button_Cature=Button(Frame_imageCapture,text="Face lock Capture",font=('Helvetica', '20'),command=lambda :Capture_Image(LockerStatus),width=39,height=3,padx=2,pady=2,bg="#F5FFFA").grid(row=1,column=0)
Spacer2=Frame(imageCaptureFrame,width=630,height=630)

MessageLabel=Label(Spacer2,text="Face Locker will Capture your Face for face Recognition key",font="30",bg="#F0F8FF").grid(row=0,column=0)
MessageLabel=Label(Spacer2,text="Look at the camera and Press Capture",font="30",bg="#F0F8FF").grid(row=1,column=0)
button_Back=Button(Spacer2,text="back to Locker 11",font=('Helvetica', '20'),command=Stop_showFrame,width=39,height=3,padx=2,pady=2,bg="#F5FFFA").grid(row=2,column=0)

def show_frame():
   while True:
    while (not Stop_thread):
     ret, frame = video_capture.read()
     if ret:
        print(Stop_thread)
        frame = cv2.flip(frame, 1)
        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        img = PIL.Image.fromarray(cv2image)
        imgtk = ImageTk.PhotoImage(image=img)
        lmain.imgtk = imgtk
        lmain.configure(image=imgtk)
        time.sleep(0.1)

def hide_Show_frame():
    imageCaptureFrame.grid_forget()
    MainFrame_()

def show_frame_Claim():
    imageCaptureFrame.grid(row=0,column=0,padx=30,pady=50)

    ret, frame = video_capture.read()
    if ret:
        frame = cv2.flip(frame, 1)
        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        img = PIL.Image.fromarray(cv2image)
        imgtk = ImageTk.PhotoImage(image=img)
        lmain.imgtk = imgtk
        lmain.configure(image=imgtk)
        lmain.after(10, show_frame_Claim)

    Hide_LockerFrame_Claim()

def Validate_image(index):

    ret, frame=video_capture.read()
    pic=frame

    cv2.imwrite(index+"_verify.jpg",frame)
    import face_recognition

    known_image = face_recognition.load_image_file(index+".jpg")
    unknown_image = face_recognition.load_image_file(index+"_verify.jpg")

    try:
        biden_encoding = face_recognition.face_encodings(known_image)[0]
        unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
    except IndexError:
        print("Invalid Image")

    known_encodings = [
    unknown_encoding,
    biden_encoding
    ]

    image_to_test = face_recognition.load_image_file(index+"_verify.jpg")
    image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]

    # See how far apart the test image is from the known faces
    face_distances = face_recognition.face_distance(known_encodings, image_to_test_encoding)

    print("Face distance "+str(face_distances[1]))
    for i, face_distance in enumerate(face_distances):
        print("The test image has a distance of {:.2} from known image #{}".format(face_distance, i))
        print("- With a normal cutoff of 0.6, would the test image match the known image? {}".format(face_distance < 0.6))
        print("- With a very strict cutoff of 0.5, would the test image match the known image? {}".format(face_distance < 0.5))
        print()

    results = face_recognition.compare_faces([biden_encoding], unknown_encoding)

    print("Validating.....")

    print(results[0])
    global unlock1,unlock2,unlock3,unlock4,unlock5

    if face_distances[1]<0.40:
        print("valid image")
        tkinter.messagebox.showinfo("Valid Image ",index+" Unlocked!")
        if index=="locker1":
            unlock1=True
            print("locker 1 unlock")
            GPIO.setmode(GPIO.BCM)
            GPIO.setup(17, GPIO.OUT)
            GPIO.output(17,True)
            print("led on 1")
            time.sleep(4)
            GPIO.output(17,False)
            GPIO.cleanup()
            os.remove("locker1.jpg")
        elif index=="locker2":
             unlock2=True
             GPIO.setmode(GPIO.BCM)
             GPIO.setup(27, GPIO.OUT)
             GPIO.output(27,True)
             print("led on 2")
             time.sleep(4)
             GPIO.output(27,False)
             GPIO.cleanup()
             print("locker 2 unlock")
             os.remove("locker2.jpg")
        elif index=="locker3":
             unlock3=True
             print("locker 3 unlock")
             GPIO.setmode(GPIO.BCM)
             GPIO.setup(22, GPIO.OUT)
             GPIO.output(22,True)
             print("led on 3")
             time.sleep(4)
             GPIO.output(22,False)
             GPIO.cleanup()
             os.remove("locker3.jpg")
        elif index=="locker4":
             unlock4=True
             GPIO.setmode(GPIO.BCM)
             GPIO.setup(23, GPIO.OUT)
             GPIO.output(23,True)
             print("led on 4")
             time.sleep(4)
             GPIO.output(23,False)
             GPIO.cleanup()
             print("locker 4 unlock")
             os.remove("locker4.jpg")
        elif index=="locker5":
             unlock5=True
             GPIO.setmode(GPIO.BCM)
             GPIO.setup(24, GPIO.OUT)
             GPIO.output(24,True)
             print("led on 5")
             time.sleep(4)
             GPIO.output(24,False)
             GPIO.cleanup()
             print("locker 5 unlock")
             os.remove("locker5.jpg")
        MainFrame_()
    else:
        print("Invalid image")
        tkinter.messagebox.showinfo("Invalid Image","Invalid face unlock, Locker locked!")

def Locker1():
     global unlock1
     global LockerStatus
     if  unlock1==True:
        print(str(unlock1)+" unlock mode 1")
        if Claim_Mode==True:
           Validate_image("locker1")

        else:
          print("Locker 1 capture..")
          LockerStatus="locker1"
          if(os.path.isfile('locker1.jpg')):
             tkinter.messagebox.showinfo("Locker Ouccupied","Locker ouccupied, Please Select anothere locker!")
          else:
               Start_showFrame()

     else:
        print("Validate image start")
        if Deposit_Mode==True:
           tkinter.messagebox.showinfo("Locker Ouccupied","Locker ouccupied, Please Select anothere locker!")
           #Validate_image("locker5")

def Locker2():
    global unlock2
    global LockerStatus
    if  unlock2==True:
        if Claim_Mode==True:
           Validate_image("locker2")

        else:
          print("Locker 2 capture..")
          LockerStatus="locker2"
          if(os.path.isfile('locker2.jpg')):
             tkinter.messagebox.showinfo("Locker Ouccupied","Locker ouccupied, Please Select anothere locker!")
          else:
               Start_showFrame()

    else:
        print("Validate image start")
        if Deposit_Mode==True:
           tkinter.messagebox.showinfo("Locker Ouccupied","Locker ouccupied, Please Select anothere locker!")
           #Validate_image("locker5")

def Locker3():
     global unlock4
     global LockerStatus
     if  unlock4==True:
        if Claim_Mode==True:
           Validate_image("locker3")

        else:
          print("Locker 3 capture..")
          LockerStatus="locker3"
          if(os.path.isfile('locker3.jpg')):
             tkinter.messagebox.showinfo("Locker Ouccupied","Locker ouccupied, Please Select anothere locker!")
          else:
               Start_showFrame()

     else:
        print("Validate image start")
        if Deposit_Mode==True:
           tkinter.messagebox.showinfo("Locker Ouccupied","Locker ouccupied, Please Select anothere locker!")
           #Validate_image("locker5")

def Locker4():
     global unlock4
     global LockerStatus
     if  unlock4==True:
        if Claim_Mode==True:
           Validate_image("locker4")

        else:
          print("Locker 4 capture..")
          LockerStatus="locker4"
          if(os.path.isfile('locker4.jpg')):
             tkinter.messagebox.showinfo("Locker Ouccupied","Locker ouccupied, Please Select anothere locker!")
          else:
               Start_showFrame()

     else:
        print("Validate image start")
        if Deposit_Mode==True:
           tkinter.messagebox.showinfo("Locker Ouccupied","Locker ouccupied, Please Select anothere locker!")
           #Validate_image("locker5")

def Locker5():
    global unlock5
    global LockerStatus

    if  unlock5==True:
        if Claim_Mode==True:
                Validate_image("locker5")

        else:
            print("Locker 5 capture..")
            LockerStatus="locker5"
            if(os.path.isfile('locker5.jpg')):
                tkinter.messagebox.showinfo("Locker Ouccupied","Locker ouccupied, Please Select anothere locker!")
            else:
                Start_showFrame()

    else:
        print("Validate image start")
        if Deposit_Mode==True:
            tkinter.messagebox.showinfo("Locker Ouccupied","Locker ouccupied, Please Select anothere locker!")
            #Validate_image("locker5")

def MainFrame_():
    print(threading.active_count)
    global CheckLocker
    global unlock1,unlock2,unlock3,unlock4,unlock5
    i=1
    while(CheckLocker):

        if(os.path.isfile('locker1.jpg')):
            unlock1=True
            print("Locker 1 locked")

        if(os.path.isfile('locker2.jpg')):
            unlock2=True
            print("Locker 2 locked")

        if(os.path.isfile('locker3.jpg')):
            unlock3=True
            print("Locker 3 locked")

        if(os.path.isfile('locker4.jpg')):
            unlock4=True
            print("Locker 4 locked")

        if(os.path.isfile('locker5.jpg')):
            unlock3=True
            print("Locker 5 locked")

        CheckLocker=False

    global AddFrame1
    AddFrame1=Frame(root, bg="#FFFAFA", padx=15, pady=15,width=140,height=700)
    AddFrame1.grid(row=0,column=0)
    global MainFrame
    MainFrame=Frame(root, bg="#FFFAFA", padx=15, pady=15,)
    MainFrame.grid(row=0,column=1)
    global AddFrame2
    AddFrame2=Frame(root, bg="#FFFAFA", padx=15, pady=15,width=140,height=500)
    AddFrame2.grid(row=0,column=2)
    button_Claim=Button(MainFrame,text="Claim",font=('Helvetica', '20'),command=Hide_MainFrame_claim,width=60,height=10,bg="#F5FFFA").grid(row=2,column=0,sticky=NSEW)
    button_Deposit=Button(MainFrame,text="Deposit",font=('Helvetica', '20'),command=Hide_MainFrame,width=60,height=10,bg="#F5FFFA").grid(row=3,column=0)

def Hide_MainFrame():
    AddFrame1.grid_forget()
    MainFrame.grid_forget()
    AddFrame2.grid_forget()
    LockerFrame()

def Hide_MainFrame_claim():
    AddFrame1.grid_forget()
    MainFrame.grid_forget()
    AddFrame2.grid_forget()
    LockerFrame_Claim()

def LockerFrame():
    global FrameDeposit,Claim_Mode,Deposit_Mode
    Claim_Mode=False
    Deposit_Mode=True
    FrameDeposit=Frame(root,bg="#FFFAFA")
    FrameDeposit.grid(row=0,column=1,padx=5,pady=16)
    global Spacer1
    Spacer1=Frame(root,width=275,height=691,bg="#F0F8FF")
    Spacer1.grid(row=0,column=0)

    button_Deposit1=Button(FrameDeposit,text="Locker 1",font=('Helvetica', '20'),command=Locker1,width=20,height=10,padx=2,pady=2,bg="#F0F8FF")
    button_Deposit1.grid(row=0,column=0,)
    button_Deposit2=Button(FrameDeposit,text="Locker 2",font=('Helvetica', '20'),command=Locker2,width=20,height=10,padx=2,pady=2,bg="#F0F8FF")
    button_Deposit2.grid(row=0,column=1,)
    button_Deposit3=Button(FrameDeposit,text="Locker 3",font=('Helvetica', '20'),command=Locker3,width=20,height=10,padx=2,pady=2,bg="#F0F8FF")
    button_Deposit3.grid(row=0,column=2)
    button_Deposit4=Button(FrameDeposit,text="Locker 4",font=('Helvetica', '20'),command=Locker4,width=20,height=10,padx=2,pady=2,bg="#F0F8FF")
    button_Deposit4.grid(row=1,column=0,)
    button_Deposit5=Button(FrameDeposit,text="Locker 5",font=('Helvetica', '20'),command=Coin_Tread,width=20,height=10,padx=2,pady=2,bg="#F0F8FF")
    button_Deposit5.grid(row=1,column=1,)
    button_DepositBack=Button(FrameDeposit,text="Back",font=('Helvetica', '20'),command=Hide_LockerFrame_back,width=20,height=10,padx=2,pady=2,bg="#F0F8FF").grid(row=1,column=2,)

    if(os.path.isfile('locker1.jpg')):
        button_Deposit1.config(bg='#FA8072')
    if(os.path.isfile('locker2.jpg')):
        button_Deposit2.config(bg='#FA8072')
    if(os.path.isfile('locker3.jpg')):
        button_Deposit3.config(bg='#FA8072')
    if(os.path.isfile('locker4.jpg')):
        button_Deposit4.config(bg='#FA8072')
    if(os.path.isfile('locker5.jpg')):
        button_Deposit5.config(bg='#FA8072')

def InsertCoin():
    Hide_LockerFrame()
    global insertCoin
    global Stop_thread2
    InsertCoin=Frame(root,bg="#F5FFFA")
    Spacer_claim=Frame(root,width=275,height=691,bg="#F5FFFA")
    Spacer_claim.grid(row=0,column=0)
    ClaimMessage=Label(Spacer_claim,text="Insert Coin to Proceed",font="120")
    ClaimMessage.grid(row=1,column=1)
    n=0
    while True:
        while (not Stop_thread2):
            print("waiting for coin to insert")
            time.sleep(1)
            n=n+1
            GPIO.setmode(GPIO.BCM)
            GPIO.setup(18, GPIO.IN) #elisi ang channel na variable sa port na gusto nimo
            #GPIO.output(channel_is_on,LOW)
            channel_is_on = GPIO.input(18)  # Returns 0 if OFF or 1 if ON elisi ang channel na variable sa port na gusto nimo

            if n==22:
                Stop_thread2=True
                Spacer_claim.grid_forget()
                MainFrame_()
            elif channel_is_on==1:
                print("Channel")
                print(channel_is_on)
                Stop_thread2=True
                Locker5()

def Coin_Tread():
     global t2
     global Stop_thread2
     t2=threading.Thread(target=InsertCoin)
     if t2.is_alive==True:
        print("Insert coin called")
        InsertCoin()

     else:
        t2.start()
        print("t2-started Main")

def LockerFrame_Claim():
    global FrameClaim,Claim_Mode,Deposit_Mode,unlock1,unlock3

    Claim_Mode=True
    Deposit_Mode=False
    print(str(Claim_Mode)+ " claim mode")
    print(str(unlock1)+ " unlock 1 mode")
    print(str(unlock3)+ " unlock 3 mode")

    FrameClaim=Frame(root,bg="#F5FFFA")
    FrameClaim.grid(row=0,column=1,padx=5,pady=16)
    global Spacer_claim
    Spacer_claim=Frame(root,width=275,height=691,bg="#F5FFFA")
    Spacer_claim.grid(row=0,column=0)
    ClaimMessage=Label(Spacer_claim,text="Select your Locker",font="50")
    ClaimMessage.grid(row=0,column=0)

    button_Deposit1=Button(FrameClaim,text="Locker 1",font=('Helvetica', '20'),command=Locker1,width=20,height=10,padx=2,pady=2,bg="#F0F8FF").grid(row=0,column=0,)
    button_Deposit2=Button(FrameClaim,text="Locker 2",font=('Helvetica', '20'),command=Locker2,width=20,height=10,padx=2,pady=2,bg="#F0F8FF").grid(row=0,column=1,)
    button_Deposit3=Button(FrameClaim,text="Locker 3",font=('Helvetica', '20'),command=Locker3,width=20,height=10,padx=2,pady=2,bg="#F0F8FF").grid(row=0,column=2)
    button_Deposit4=Button(FrameClaim,text="Locker 4",font=('Helvetica', '20'),command=Locker4,width=20,height=10,padx=2,pady=2,bg="#F0F8FF").grid(row=1,column=0,)
    button_Deposit5=Button(FrameClaim,text="Locker 5",font=('Helvetica', '20'),command=Locker5,width=20,height=10,padx=2,pady=2,bg="#F0F8FF").grid(row=1,column=1,)
    button_DepositBack=Button(FrameClaim,text="Back",font=('Helvetica', '20'),command=Hide_LockerFrame_Claim_back,width=20,height=10,padx=2,pady=2,bg="#F0F8FF").grid(row=1,column=2,)

def Hide_LockerFrame():
    FrameDeposit.grid_forget()
    Spacer1.grid_forget()

def Hide_LockerFrame_Claim():
    FrameClaim.grid_forget()
    Spacer_claim.grid_forget()

def Hide_LockerFrame_Claim_back():
    FrameClaim.grid_forget()
    Spacer_claim.grid_forget()
    MainFrame_()

def Hide_LockerFrame_back():
    FrameDeposit.grid_forget()
    Spacer1.grid_forget()
    MainFrame_()

helv36 = tkFont.Font(family='Helvetica', size=15, weight=tkFont.BOLD)

MainFrame_()

#show_frame()
root.mainloop()


import datetime
from docs_support import save_uploaded_doc, doc_url, calc_doc_story
from members_support import calc_grouped_selected_options, calc_all_tags, get_tag_ids, init_query, get_topics_query
from date_utils import date_of_date_str, parse_date, get_all_dates, update_record_dates, fix_record_dates_in, fix_record_dates_out
import stories_manager

@serve_json
def upload_doc(vars):
    user_id = vars.user_id or auth.current_user()
    comment("start handling uploaded doc file")
    user_id = int(vars.user_id) if vars.user_id else auth.current_user()
    fil = vars.file
    result = save_uploaded_doc(fil.name, fil.BINvalue, user_id)
    if result != 'duplicate':
        calc_doc_story(result)
    return dict(upload_result=result)

@serve_json
def get_doc_list(vars):
    params = vars.params
    if params.checked_doc_list:
        q = (db.TblDocs.story_id.belongs(params.checked_doc_list)) & (db.TblStories.id==db.TblDocs.story_id)
        lst0 = db(q).select()
        lst0 = [rec for rec in lst0]
        for rec in lst0:
            rec.TblDocs.checked = True
    else:
        lst0 = []
    selected_topics = params.selected_topics or []
    q = make_docs_query(params)
    lst = db(q).select(orderby=~db.TblDocs.id)
    lst = [rec for rec in lst if rec.TblDocs.story_id not in params.checked_doc_list]
    lst = lst0 + lst
    doc_list = []
    for rec1 in lst:
        rec = rec1.TblDocs
        fix_record_dates_out(rec)
        story = get_story_by_id(rec.story_id)
        rec.story = story
        rec.doc_url = doc_url(rec.story_id)
        rec.doc_jpg_url = rec.doc_url.replace('/docs/', '/docs/pdf_jpgs/').replace('.pdf', '.jpg')
        rec.keywords = rec1.TblStories.keywords
        doc_list.append(rec)
    return dict(doc_list=doc_list, no_results=not doc_list)

@serve_json
def delete_checked_docs(vars):
    checked_doc_list = vars.params.checked_doc_list
    db(db.TblDocs.story_id.belongs(checked_doc_list)).update(deleted=True)
    db(db.TblStories.id.belongs(checked_doc_list)).update(deleted=True)
    return dict()

@serve_json
def apply_to_checked_docs(vars):
    all_tags = calc_all_tags()
    params = vars.params
    sdl = params.checked_doc_list
    if params.docs_date_str:
        dates_info = dict(
            doc_date = (params.docs_date_str, params.docs_date_span_size)
        )
    else:
        dates_info = None

    st = params.selected_topics
    added = []
    deleted = []
    changes = dict()
    new_topic_was_added = False
    for story_id in sdl:
        drec = db(db.TblDocs.story_id==story_id).select().first() #get rid of _term_id_
        curr_tag_ids = set(get_tag_ids(story_id, 'D'))
        for tpc in st:
            topic = tpc.option
            doc_id = drec.id #get rid of _term_id_
            if topic.sign=="plus" and topic.id not in curr_tag_ids:
                new_id = db.TblItemTopics.insert(item_type='D', topic_id=topic.id, story_id=story_id)
                curr_tag_ids |= set([topic.id])
                ###added.append(item)
                topic_rec = db(db.TblTopics.id==topic.id).select().first()
                if topic_rec.topic_kind == 0: #never used
                    new_topic_was_added = True
                if 'D' not in topic_rec.usage:
                    usage = topic_rec.usage + 'D'
                    topic_rec.update_record(usage=usage, topic_kind=2) #topic is simple
            elif topic.sign=="minus" and topic.id in curr_tag_ids:
                q = (db.TblItemTopics.item_type=='D') & (db.TblItemTopics.story_id==story_id) & (db.TblItemTopics.topic_id==topic.id) #got rid of _item_id_
                curr_tag_ids -= set([topic.id])
                ###deleted.append(item)
                db(q).delete()
        curr_tags = [all_tags[tag_id] for tag_id in curr_tag_ids]
        keywords = "; ".join(curr_tags)
        changes[doc_id] = dict(keywords=keywords, doc_id=doc_id)
        rec = db(db.TblDocs.id==doc_id).select().first()
        rec = db(db.TblStories.id==rec.story_id).select().first()
        rec.update_record(keywords=keywords, is_tagged=bool(keywords))
        if dates_info:
            update_record_dates(rec, dates_info)
    ###changes = [changes[doc_id] for doc_id in sdl]
    ###ws_messaging.send_message('DOC-TAGS-CHANGED', group='ALL', changes=changes)
    return dict(new_topic_was_added=new_topic_was_added)

#----------------support functions-----------------

def make_docs_query(params):
    q = init_query(db.TblDocs, params.editing)
    if params.days_since_upload:
        days = params.days_since_upload.value
        if days:
            upload_date = datetime.datetime.today() - datetime.timedelta(days=days)
            q &= (db.TblDocs.upload_date >= upload_date)
    opt = params.selected_uploader
    if opt == 'mine':
        q &= (db.TblDocs.uploader==params.user_id)
    elif opt == 'users':
        q &= (db.TblDocs.uploader!=None)
    opt = params.selected_dates_option
    if opt == 'selected_dates_option':
        pass
    elif opt == 'dated':
        q &= (db.TblDocs.doc_date != NO_DATE)
    elif opt == 'undated':
        q &= (db.TblDocs.doc_date == NO_DATE)
    if params.selected_docs:
        q &= (db.TblDocs.story_id.belongs(params.selected_docs))
    if params.selected_topics:
        q1 = get_topics_query(params.selected_topics)
        q &= q1
    return q

def get_story_by_id(story_id):
    sm = stories_manager.Stories()
    return sm.get_story(story_id)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('templates', '0002_auto_20141127_0059'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='template',
            options={'ordering': ('path',), 'verbose_name': 'Template Link', 'verbose_name_plural': 'Links to Templates for Content Views/Widgets/Pages'},
        ),
        migrations.RemoveField(
            model_name='template',
            name='content',
        ),
        migrations.RemoveField(
            model_name='template',
            name='creation_date',
        ),
        migrations.RemoveField(
            model_name='template',
            name='last_changed',
        ),
        migrations.AddField(
            model_name='template',
            name='path',
            field=models.FilePathField(default='', path=b'templates/', recursive=True),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='template',
            name='_name',
            field=models.CharField(default='', max_length=1024, editable=False),
            preserve_default=False,
        ),
    ]


from os import system, path, remove, chdir, getcwd, listdir, name
import subprocess

P_RSCRIPTS = "../R/"
PR_BIOTRANSFORMER = "C:/Users/Aborrel/research/Silent_Spring/PFAS/BioTransformerJar/biotransformerjar"

R_BIN = "C:\\Program Files\\R\\R-4.0.2\\bin\\Rscript.exe"
P_RQSAR_linux = "/mnt/c/Users/AlexandreBorrel/research/development/QSAR-QSPR"
P_RQSAR_window = "c:/Users/aborr/research/development/QSAR-QSPR/"
######
# Main functions

def runRCMD(cmd, out = 0):

    workdir = getcwd()
    chdir(P_RSCRIPTS)
    if name == "nt":
        l_elem = cmd.split(" ")
        cmd_line = [R_BIN] + l_elem
        print(cmd_line)
        p = subprocess.Popen(cmd_line)
        (output, err) = p.communicate()
        p.wait()
        print(err)
    else:
        print(cmd)
        system(cmd)
    chdir(workdir)

# for the Kaumas dataset

def corHormResponse(p_filin):
    cmd = "./corHormResponse.R %s"%(p_filin)
    runRCMD(cmd)

def barplotHormones(p_filin):
    cmd = "./barplotHormones.R %s"%(p_filin)
    runRCMD(cmd)

def drawResponseCurve(p_filin, hormone, pval):
    cmd = "./draw_RC.R %s %s %s"%(p_filin, hormone, pval)
    runRCMD(cmd)


#!/usr/bin/env python
import re
from setuptools import setup, find_packages

# Taken from https://github.com/kennethreitz/requests/commit/1145fe64a
version = ''
with open('reget/__init__.py', 'r') as fd:
    version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
                        fd.read(), re.MULTILINE).group(1)

if not version:
    raise RuntimeError('Cannot find version information')

setup(
    name='reget',
    version=version,
    description='HTTP downloader that fix wget issues',
    long_description=open('README.rst').read(),
    url='https://github.com/infotim/python-reget',
    download_url='https://github.com/infotim/python-reget/tarball/' + version,
    author='Timofey Stolbov',
    author_email='timofey@stolbov.info',
    license='MIT',
    packages=find_packages(),
    include_package_data=True,
    install_requires=['requests~=2.7.0'],
    zip_safe=False,
    entry_points={
        'console_scripts': ['reget=reget.cli:main'],
    },
    classifiers=(
        'Development Status :: 4 - Beta',
        'Environment :: Console',
        'Intended Audience :: Developers',
        'Intended Audience :: End Users/Desktop',
        'Intended Audience :: System Administrators',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Topic :: Software Development :: Libraries',
        'Topic :: Utilities'
    ),
)


import sys
import logging
import xlsxwriter
from Utility import Common
from Utility import Message
from Entity import DBEntity
from Entity import MariaEntity
from EDI import EDIEntity

def getCustSalesList(strEDIConnection, strEDIDB, strDataDate):
    strSQL = "SELECT WANT_CHAN_NM, WANT_COM_NM " + \
             "     , PROD_H1_NM, PROD_H2_NM, PROD_H3_NM " + \
             "     , (POSTED_AMT_LAST_ALL/1000) AS POSTED_AMT_LAST_ALL " + \
             "     , (POSTED_AMT_LAST/1000) AS POSTED_AMT_LAST " + \
             "     , (POSTED_AMT_THIS/1000) AS POSTED_AMT_THIS " + \
             "     , (POSTING_AMT/1000) AS POSTING_AMT " + \
             "FROM DMMDL.DC0021_CUST_SALES " + \
             "WHERE {DATA_DATE} AND RPT_YM=SUBSTR(DATA_DATE, 1, 6) " + \
             "ORDER BY WANT_CHAN_ID, WANT_COM_ID " + \
             "       , PROD_H1_ID, PROD_H2_ID, PROD_H3_ID; "

    dictMariaParameter = {
        "DATA_DATE": MariaEntity.MariaParameter(strOperator="=", objValue=strDataDate, enumType=MariaEntity.EnumMariaParameterType.Char),
    }
    strSQL = DBEntity.getMariaSQLCommand(strSQL, dictMariaParameter)

    listCustSales = DBEntity.getMariaDataList(strEDIConnection, strEDIDB, strSQL)
    return listCustSales

def execWriteWorkbook(strJobPathRES, listCustSales):
    if len(listCustSales) == 0:
        return False
    else:
        objExcelWorkbook = None
        objExcelWorksheet = None

        objExcelWorkbook = xlsxwriter.Workbook(strJobPathRES)

        objExcelColumnFormat = objExcelWorkbook.add_format({
            "bold": 1, "align": "center", "valign": "vcenter", "font_size": 11, "font_color": "#FFFFFF",
            "fg_color": "#305496",
            "border": 0})

        objExcelRowLeftFormat = objExcelWorkbook.add_format({
            "bold": 1, "align": "left", "valign": "vcenter", "font_size": 11, "font_color": "#000000",
            "border": 0})
        objExcelRowRightFormat = objExcelWorkbook.add_format({
            "bold": 1, "align": "right", "valign": "vcenter", "font_size": 11, "font_color": "#000000",
            "border": 0})
        objExcelRowRightFormat.set_num_format('#,##0.00000')

        objExcelRow = 0
        objExcelWorksheet = objExcelWorkbook.add_worksheet("客户业绩通用报表")

        objExcelWorksheet.set_default_row(20)

        objExcelWorksheet.set_column(0, 0, 18)
        objExcelWorksheet.set_column(1, 1, 10)
        objExcelWorksheet.set_column(2, 2, 18)
        objExcelWorksheet.set_column(3, 3, 14)
        objExcelWorksheet.set_column(4, 4, 20)
        objExcelWorksheet.set_column(5, 5, 15)
        objExcelWorksheet.set_column(6, 6, 15)
        objExcelWorksheet.set_column(7, 7, 15)
        objExcelWorksheet.set_column(8, 8, 15)
        objExcelWorksheet.set_column(9, 9, 15)

        objExcelWorksheet.write(objExcelRow, 0, "旺旺渠道", objExcelColumnFormat)
        objExcelWorksheet.write(objExcelRow, 1, "产品大类", objExcelColumnFormat)
        objExcelWorksheet.write(objExcelRow, 2, "旺旺销售公司", objExcelColumnFormat)
        objExcelWorksheet.write(objExcelRow, 3, "PM线", objExcelColumnFormat)
        objExcelWorksheet.write(objExcelRow, 4, "产品线", objExcelColumnFormat)
        objExcelWorksheet.write(objExcelRow, 5, "去年全月销退金额", objExcelColumnFormat)
        objExcelWorksheet.write(objExcelRow, 6, "月同期销退金额", objExcelColumnFormat)
        objExcelWorksheet.write(objExcelRow, 7, "本月销退金额", objExcelColumnFormat)
        objExcelWorksheet.write(objExcelRow, 8, "未过帐金额", objExcelColumnFormat)
        objExcelWorksheet.write(objExcelRow, 9, "单位：千元")
        objExcelRow += 1

        for dictEMPSales in listCustSales:
            objExcelWorksheet.write(objExcelRow, 0, dictEMPSales["WANT_CHAN_NM"], objExcelRowLeftFormat)
            objExcelWorksheet.write(objExcelRow, 1, dictEMPSales["PROD_H1_NM"], objExcelRowLeftFormat)
            objExcelWorksheet.write(objExcelRow, 2, dictEMPSales["WANT_COM_NM"], objExcelRowLeftFormat)
            objExcelWorksheet.write(objExcelRow, 3, dictEMPSales["PROD_H2_NM"], objExcelRowLeftFormat)
            objExcelWorksheet.write(objExcelRow, 4, dictEMPSales["PROD_H3_NM"], objExcelRowLeftFormat)
            objExcelWorksheet.write(objExcelRow, 5, dictEMPSales["POSTED_AMT_LAST_ALL"], objExcelRowRightFormat)
            objExcelWorksheet.write(objExcelRow, 6, dictEMPSales["POSTED_AMT_LAST"], objExcelRowRightFormat)
            objExcelWorksheet.write(objExcelRow, 7, dictEMPSales["POSTED_AMT_THIS"], objExcelRowRightFormat)
            objExcelWorksheet.write(objExcelRow, 8, dictEMPSales["POSTING_AMT"], objExcelRowRightFormat)
            objExcelWorksheet.write(objExcelRow, 9, "")
            objExcelRow += 1

        objExcelWorkbook.close()
        return True

def execMailCustSales(strJobPathRES, strDataDate):
    strEDIConnection = "3DW-DWuser@tpe-centos7-maria10-dw"
    strEDIDB = "DMMDL"

    listCustSales = getCustSalesList(strEDIConnection, strEDIDB, strDataDate)

    if execWriteWorkbook(strJobPathRES, listCustSales) == True:
        strMessageSMTPServer = "tw-mail02.want-want.com"
        strMessageSMTPFrom = "Data.Center@want-want.com"
        arrayMessageSMTPTo = [
            "Xie_Wei3@want-want.com",
            "Yan_HongMei@want-want.com",
            "Zoe.Hsu@want-want.com"
        ]
        strMessageSubject = "[测试]客户业绩通用报表-%s" % strDataDate
        strAttachFileName = "[测试]客户业绩通用报表-%s.xlsx" % strDataDate

        Message.sendSMTPMail(strMessageSMTPServer, strMessageSMTPFrom, arrayMessageSMTPTo, strMessageSubject, "", strJobPathRES, strAttachFileName)
        return True
    else:
        return False

def main():
    strLoggerName = sys.argv[1]

    strEDIConnection = sys.argv[2]
    strEDIDB = sys.argv[3]
    strEDINo = sys.argv[4]

    strJobPathRES = sys.argv[5]
    # strDependOnJobPathRES = sys.argv[6]

    Common.setLogging(strLoggerName)
    # logger = logging.getLogger(strLoggerName)

    dictFlow = EDIEntity.getFlow(strEDIConnection, strEDIDB, strEDINo)
    strDataDate = dictFlow["DATA_DATE"]

    execMailCustSales(strJobPathRES, strDataDate)

if __name__ == "__main__":
    main()


import os
from pathlib import Path
import errno
import tensorflow as tf
import numpy as np
from tensorflow import keras
import hvd
import unet

tf.logging.set_verbosity(tf.logging.INFO)

def test(images):
    with Path("model.yaml").open("r") as br:
        model = keras.models.model_from_yaml(br.read())
        assert isinstance(model, keras.Model)
    for img in images:
        model.predict(images)

def main(_):
    hvd.init()

    cache_dir = os.path.join(os.path.expanduser('~'), '.keras', 'datasets')
    if not os.path.exists(cache_dir):
        try:
            os.mkdir(cache_dir)
        except OSError as e:
            if e.errno == errno.EEXIST and os.path.isdir(cache_dir):
                pass
            else:
                raise

    (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data('MNIST-data-%d' % hvd.rank())
    x_train = (np.reshape(x_train, (-1, 28//2, 28*2, 1)) / 255.0).astype(np.float32)
    y_train = y_train.astype(np.int64)
    dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1000).batch(32)
    dataset = dataset.map(lambda x, y: (x, tf.one_hot(tf.cast(y, tf.int32), 10, 1, 0)))
    image, label = dataset.make_one_shot_iterator().get_next()

    model = unet.create_network(image)
    with Path("model.yaml").open("w") as pw:
        pw.write(model.to_yaml())
    print(model.layers)
    model.summary()
    print(keras.utils.plot_model(model, to_file="model.png", show_shapes=True))
    logits, = model.outputs
    loss = tf.losses.softmax_cross_entropy(tf.concat([image > 0.5, image < 0.5], axis=-1), logits)
    # loss = losses.SoftDice()(logits, tf.cast(tf.concat([image > 0.5, image < 0.5], axis=-1), dtype=tf.float32))
    predict = tf.argmax(logits, 1)

    opt = tf.train.RMSPropOptimizer(0.001 * hvd.size())
    opt = hvd.DistributedOptimizer(opt)
    global_step = tf.train.get_or_create_global_step()
    train_op = opt.minimize(loss, global_step=global_step)
    hooks = [
        hvd.BroadcastGlobalVariablesHook(0),
        tf.train.StopAtStepHook(last_step=20000 // hvd.size()),
        tf.train.LoggingTensorHook(tensors={'step': global_step, 'loss': loss}, every_n_iter=10),
    ]
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.gpu_options.visible_device_list = str(hvd.local_rank())

    checkpoint_dir = './checkpoints' if hvd.rank() == 0 else None

    with tf.train.MonitoredTrainingSession(checkpoint_dir=checkpoint_dir,
                                           hooks=hooks,
                                           config=config) as mon_sess:
        while not mon_sess.should_stop():
            mon_sess.run(train_op)

if __name__ == "__main__":
    tf.app.run()


# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------

# pylint: disable=protected-access

import logging
from os import PathLike
from pathlib import Path
from typing import AnyStr, Dict, IO, Optional, Union

from azure.ai.ml.constants._common import BASE_PATH_CONTEXT_KEY, PARAMS_OVERRIDE_KEY, ScheduleType
from azure.ai.ml.entities._mixins import RestTranslatableMixin
from azure.ai.ml.entities._monitoring.definition import MonitorDefinition
from azure.ai.ml.entities._system_data import SystemData
from azure.ai.ml.entities._schedule.schedule import Schedule
from azure.ai.ml.entities._schedule.trigger import CronTrigger, RecurrenceTrigger, TriggerBase
from azure.ai.ml.entities._util import load_from_dict
from azure.ai.ml._restclient.v2023_04_01_preview.models import CreateMonitorAction
from azure.ai.ml._restclient.v2023_04_01_preview.models import Schedule as RestSchedule
from azure.ai.ml._restclient.v2023_04_01_preview.models import ScheduleProperties
from azure.ai.ml._schema.monitoring.schedule import MonitorScheduleSchema
from azure.ai.ml._utils._experimental import experimental
from azure.ai.ml._utils.utils import dump_yaml_to_file

module_logger = logging.getLogger(__name__)

@experimental
class MonitorSchedule(Schedule, RestTranslatableMixin):
    def __init__(
        self,
        *,
        name: str,
        trigger: Union[CronTrigger, RecurrenceTrigger],
        create_monitor: MonitorDefinition,
        display_name: Optional[str] = None,
        description: Optional[str] = None,
        tags: Optional[Dict] = None,
        properties: Optional[Dict] = None,
        **kwargs,
    ):
        super().__init__(
            name=name,
            trigger=trigger,
            display_name=display_name,
            description=description,
            tags=tags,
            properties=properties,
            **kwargs,
        )
        self.create_monitor = create_monitor
        self._type = ScheduleType.MONITOR

    @classmethod
    def _load(
        cls,
        data: Optional[Dict] = None,
        yaml_path: Optional[Union[PathLike, str]] = None,
        params_override: Optional[list] = None,
        **kwargs,
    ) -> "MonitorSchedule":
        data = data or {}
        params_override = params_override or []
        context = {
            BASE_PATH_CONTEXT_KEY: Path(yaml_path).parent if yaml_path else Path("./"),
            PARAMS_OVERRIDE_KEY: params_override,
        }
        return cls(
            base_path=context[BASE_PATH_CONTEXT_KEY],
            **load_from_dict(MonitorScheduleSchema, data, context, **kwargs),
        )

    def _to_rest_object(self) -> RestSchedule:
        return RestSchedule(
            properties=ScheduleProperties(
                description=self.description,
                properties=self.properties,
                tags=self.tags,
                action=CreateMonitorAction(monitor_definition=self.create_monitor._to_rest_object()),
                display_name=self.display_name,
                is_enabled=self._is_enabled,
                trigger=self.trigger._to_rest_object(),
            )
        )

    def dump(self, dest: Union[str, PathLike, IO[AnyStr]], **kwargs) -> None:
        """Dump the schedule content into a file in yaml format.

        :param dest: The destination to receive this schedule's content.
            Must be either a path to a local file, or an already-open file stream.
            If dest is a file path, a new file will be created,
            and an exception is raised if the file exists.
            If dest is an open file, the file will be written to directly,
            and an exception will be raised if the file is not writable.
        :type dest: Union[str, PathLike, IO[AnyStr]]
        """
        path = kwargs.pop("path", None)
        yaml_serialized = self._to_dict()
        dump_yaml_to_file(dest, yaml_serialized, default_flow_style=False, path=path, **kwargs)

    def _to_dict(self) -> Dict:
        return MonitorScheduleSchema(context={BASE_PATH_CONTEXT_KEY: "./"}).dump(self)  # pylint: disable=no-member

    @classmethod
    def _from_rest_object(cls, obj: RestSchedule) -> "MonitorSchedule":
        properties = obj.properties
        return cls(
            trigger=TriggerBase._from_rest_object(properties.trigger),
            create_monitor=MonitorDefinition._from_rest_object(properties.action.monitor_definition),
            name=obj.name,
            id=obj.id,
            display_name=properties.display_name,
            description=properties.description,
            tags=properties.tags,
            properties=properties.properties,
            provisioning_state=properties.provisioning_state,
            is_enabled=properties.is_enabled,
            creation_context=SystemData._from_rest_object(obj.system_data) if obj.system_data else None,
        )


from schools3.ml.base.hyperparameters import *
import schools3.config.ml.hyperparameters.mlp_hyperparameters_config as config

class MLPHyperparameters(Hyperparameters):
    def __init__(self, **kwargs):
    #hidden_sizes=None, loss=None, optimizer=None, epochs=None, batch_size=None):
        super(MLPHyperparameters, self).__init__()
        self.hidden_sizes = config.get_hidden_sizes_hp(kwargs.get('hidden_sizes', None))
        self.loss = config.get_loss_hp(kwargs.get('loss', None))
        self.optimizer = config.get_optimizer_hp(kwargs.get('optimizer', None))
        self.epochs = config.get_epochs_hp(kwargs.get('epochs', None))
        self.batch_size = config.get_batch_size_hp(kwargs.get('batch_size', None))
        self.reg_const = config.get_reg_const_hp(kwargs.get('reg_const', None))


import os
import json
import time

from unittest import skip

from django.core.urlresolvers import reverse
from django.core.files.uploadedfile import SimpleUploadedFile
from django.contrib.auth import get_user_model
from django.test import Client, TestCase

from geonode.geoserver.helpers import ogc_server_settings
from geonode.layers.models import Layer
from geoserver.catalog import Catalog

from ...MapStoryTestMixin import MapStoryTestMixin
from ...AdminClient import AdminClient
from ...utils import create_admin_user, generate_testname

User = get_user_model()
test_layer_file_path = os.path.realpath('mapstory/tests/sampledata/lewisandclarktrail.csv')

def getLayerCatalog():
    """
    Convenience method for getting the Layer catalog
    """
    #@TODO: Use test server settings
    rest_url = ogc_server_settings.rest
    rest_user = ogc_server_settings.credentials.username
    rest_pass = ogc_server_settings.credentials.password
    return Catalog(rest_url, rest_user, rest_pass)

class TestLayerViews(MapStoryTestMixin):
    """
    Test Layer Views
    """
    def create_layer(self):
        if self.admin_client == None:
            self.admin_client = AdminClient()

        self.login_admin()
        filename = generate_testname(prefix="layer", size=6)
        payload = {
            "featureType":{
                "attributes":{
                    "attribute":[
                        {"name":"geometry","binding":"com.vividsolutions.jts.geom.Point","minOccurs":0,"nillable":True},
                        {"name":"time","binding":"org.geotools.data.postgis.BigDate","nillable":True,"minOccurs":0}
                    ]
                },
                "nativeCRS":"EPSG:4326",
                "srs":"EPSG:4326",
                "store":{"name":"mapstory_geogig"},
                "namespace":{"name":"geonode"},
                "configureTime":True,
                "editable":True,
                "name":filename,
                "start_date":"time",
                "permissions":{
                    "users":{
                        "AnonymousUser":["change_layer_data","download_resourcebase","view_resourcebase"]
                    },
                    "groups":{
                        "registered":["change_layer_data","download_resourcebase","view_resourcebase"]
                    }
                },
                "storeCreateGeogig":True
            }
        }
        response = self.admin_client.post(
            reverse('layer_create'),
            data=json.dumps(payload),
            content_type='application/json',
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        # Should OK
        self.assertEquals(201, response.status_code)
        json_response = json.loads(response.content)
        self.assertTrue(u'layers' in json_response.keys())

        # Layers array should not be empty
        self.assertTrue(len(json_response[u'layers']) > 0)
        # for l in json_response[u'layers']:
            # print(l)

        # Search in the catalog and return the correct layer
        for layer in getLayerCatalog().get_layers():
            if filename in layer.name:
                return layer

        return None

    def get_layer(self):
        self.login_admin()

        all_layers = getLayerCatalog().get_layers()

        # Special case: No layers exist
        if (len(all_layers) < 1):

            # Make a new layer, refresh and break if nothing happened
            test_layer = self.create_test_layer()
            if (test_layer == None):
                raise RuntimeError

            all_layers = getLayerCatalog().get_layers()

        return all_layers[0]

    def login_admin(self):
        """
        Convenience method for loging in as a superuser.
        """
        try:
            user = User.objects.get(username='admin')
            self.assertIsNotNone(user)
        except User.DoesNotExist:
            create_admin_user("admin", "admin")

        self.admin_client.login_as_admin("admin", "admin")

    def setUp(self):
        self.admin_client = AdminClient()

    def test_layers_start_out_clean(self):
        self.assertEquals(0, len(Layer.objects.all()))

    def test_layer_needs_login(self):
        # Should not be authorized to upload things
        with open(test_layer_file_path) as fp:
            response = self.client.post('/uploads/new/json', {'name': 'file', 'attachment': fp}, follow=True)
            self.assertEquals(response.status_code, 200)
            self.assertTemplateUsed(response, 'account/login.html')

    def test_csv_layer_upload(self):
        self.login_admin()

        # Upload the file
        with open(test_layer_file_path) as fp:
            response = self.admin_client.post('/uploads/new/json', {'name': 'file', 'attachment': fp}, follow=True)
            self.assertEquals(response.status_code, 200)
            self.assertTemplateNotUsed(response, 'account/login.html')

    def test_layer_create_wizard(self):
        #@FIXME: Creates a broken layer and causes things to crash
        self.login_admin()

        # Should be emtpty
        catalog = getLayerCatalog()
        current_layer_count = len(catalog.get_layers())

        self.create_layer()

        # Should update the catalog
        self.assertEquals(current_layer_count + 1, len(getLayerCatalog().get_layers()))

    def test_layer_import_wizard_views(self):
        # Should have 0 Layers stored
        response = self.admin_client.get('/maps/new', follow=True)
        self.assertEquals(response.status_code, 200)
        self.assertTemplateNotUsed(response, 'account/login.html')
        self.assertEquals(0, len(Layer.objects.all()))

        self.login_admin()
        with open(test_layer_file_path) as fp:
            # ---------------
            # 1. Upload file
            # ---------------
            response = self.admin_client.post('/uploads/new/json', {'name': 'testlayer', 'file': fp}, follow=True)
            self.assertEquals(response.status_code, 200)

            # Should Respond with JSON
            response_json = json.loads(response.content)
            self.assertIsNotNone(response_json)

            # Should not have any errors
            self.assertFalse(u'errors' in response_json.keys())

            # State should be 'UPLOADED;
            self.assertTrue(u'state' in response_json.keys())
            self.assertEquals(u'UPLOADED', response_json[u'state'])

            # Should have an upload ID
            self.assertIsNotNone(response_json[u'id'])

            # --------------------------
            # 2. Get Upload detail info
            # --------------------------
            url = "/importer-api/data/%s" % (response_json[u'id'],)
            detail_response = self.admin_client.get(url, follow=True)
            self.assertEquals(200, detail_response.status_code)

            # Should respond with JSON
            detail_json = json.loads(detail_response.content)
            self.assertIsNotNone(detail_json)

            # Should have upload detail information
            self.assertTrue(u'name' in detail_json.keys())
            self.assertEquals(True, detail_json[u'complete'])
            self.assertIsNotNone(detail_json[u'date'])
            self.assertEquals('CSV', detail_json[u'file_type'])
            self.assertIsNotNone(detail_json[u'resource_uri'])
            self.assertIsNotNone(detail_json[u'name'])

            # Get the uploaded details
            upload_name = detail_json[u'name']

            # ----------------------
            # 3. POST configuration
            # ----------------------
            json_str = json.dumps({
                "always_geogig": True,
                "configureTime": True,
                "editable": True,
                "convert_to_date": ["e_date", "fdate"],
                "index": 0,
                "name": upload_name,
                "permissions": {
                    "users": {
                        "AnonymousUser": [
                            "change_layer_data",
                            "download_resourcebase",
                            "view_resourcebase"]
                    },
                    "groups": {
                        "registered": [
                            "change_layer_data",
                            "download_resourcebase",
                            "view_resourcebase"
                        ]
                    }
                },
                "start_date": "e_date",
                "end_date": "fdate",
                "geoserver_store": {
                    "type": "geogig"
                }
            })
            url = "/importer-api/data-layers/%s/configure/" % (response_json[u'id'],)
            config_response = self.admin_client.post(url, json_str, content_type="application/json")
            self.assertEquals(config_response.status_code, 200)

            # Should receive a JSON task
            json_task = json.loads(config_response.content)
            self.assertIsNotNone(json_task)
            self.assertTrue(u'task' in json_task.keys())
            taskid = json_task[u'task']
            self.assertIsNotNone(taskid)

            # -------------------
            # 4. Get data layers
            url = "/importer-api/data-layers/%s/" % (response_json[u'id'],)
            response = self.admin_client.get(url, follow=True)
            self.assertEquals(response.status_code, 200)
            self.assertTemplateNotUsed(response, 'account/login.html')

            # Should get JSON back
            json_response = json.loads(response.content)
            self.assertIsNotNone(json_response)

            retries = 0
            while retries < 5:
                time.sleep(2)

                # Try again
                retry_response = self.admin_client.get(url, follow=True)
                self.assertEquals(retry_response.status_code, 200)
                json_response = json.loads(retry_response.content)
                self.assertIsNotNone(json_response)

                if(json_response[u'status'] == u'SUCCESS'):
                    # print("SUCCESS!!")
                    break
                else:
                    # print("retrying")
                    retries += 1

            # @FIXME: Success is not happening
            # self.assertEquals(json_response[u'status'], u'SUCCESS')
            # self.assertEquals(1, len(Layer.objects.all()))

    def test_layer_detail_view(self):
        self.login_admin()
        layer = self.get_layer()
        self.assertIsNotNone(layer)
        # TODO: (Zunware) Layer needs to implement get_absolute_url() for best practices.
        response = self.admin_client.get(reverse('layer_detail', kwargs={'layername': 'geonode:%s' % (layer.name,)}))
        # TODO:FIX: (Zunware): This is giving 404
        # self.assertEquals(200, response.status_code)
        # self.assertTemplateUsed(response, 'layers/layer_detail.html')


import tensorflow as tf
import tensorflow.contrib.slim as slim
import os
from keras.utils import to_categorical
import numpy as np
import random
from scipy import misc
import pickle
import sys
platform = sys.platform

if(platform.lower()[:len("linux")] == "linux"):
    ROOT_FOLDER = "/media/ixtiyor/New Volume/Data_acquisition/Test_Data"
else:
    ROOT_FOLDER = 'H:/Data_acquisition/Test_Data/'

# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# global functions ------------------------------------------------------------

def show_all_variables():
    model_vars = tf.trainable_variables()
    slim.model_analyzer.analyze_vars(model_vars, print_info=True)
def get_number_of_trainable_parameters():
    total_parameters = 0
    for variable in tf.trainable_variables():
        # shape is an array of tf.Dimension
        shape = variable.get_shape()
        print(shape)
        print(len(shape))
        variable_parameters = 1
        for dim in shape:
            print(dim)
            variable_parameters *= dim.value
        print(variable_parameters)
        total_parameters += variable_parameters
#    print(total_parameters)
    return total_parameters

def get_flops(sess, meta):
    """
        meta is metadata shoud be set before building the model:
        run_meta = tf.RunMetadata()

    """
    opts = tf.profiler.ProfileOptionBuilder.float_operation()
    flops = tf.profiler.profile(sess.graph, run_meta=meta, cmd='op', options=opts)

    if flops is not None:
        return flops.total_float_ops
    else:
        return 0;

def one_hot_encode(labels, n_classes):
    """
    this one hot encoding ensures that you have incremental
    classes starts from zero (like 0, 1, 2, 3 ...)
    """
    newlabels = []
    if(n_classes == 0 or n_classes == None):
        n_classes = len(np.unique(labels))

    for l in labels:
        tmp = np.zeros(n_classes)
        tmp[l] = 1
        newlabels.append(list(tmp))
    return newlabels

def check_balance(labels):
    assert len(list(np.shape(labels))) == 1 # change it for one hotted labels
    num_classes = len(np.unique(labels))
    classes = np.unique(labels)
    num_all = len(labels)
    labels = np.array(labels)
    nums = []
    for i in range(num_classes):
        n_i_class = len(np.where(labels==classes[i])[0])
        print("class with value " + str(classes[i]) + " % is " +
              str(round(n_i_class/num_all,2))  + " is " +
              str(n_i_class) + "/" + str(num_all) )
        nums.append(n_i_class)
    print(nums)
    nums = np.array(nums)
    nums =1./ (nums/ np.max(nums))
    return np.array(nums, np.int32)

def get_balanced_data(Z, V, labels, classes=[0,1], proportions=[0, 9]):
    """
    here we get balanced data using just copying
    """
    Z = np.array(Z)
    V = np.array(V)
    labels = np.array(labels)
    classes = np.array(classes)
    Z_balanced, V_balanced, labels_balanced = [], [], []
    for i in range(len(labels)):
        do_the_balancing = False
        index_cls = np.where(classes == labels[i])[0][0]
        if(proportions[index_cls] > 1): do_the_balancing= True
        if(do_the_balancing):
            for j in range(proportions[index_cls] + 1):

                Z_balanced.append(Z[i])

                V_balanced.append(V[i])
                labels_balanced.append(labels[i])
            #print(np.shape(Z_balanced))
        else:
            Z_balanced.append(Z[i])
            V_balanced.append(V[i])
            labels_balanced.append(labels[i])
    return Z_balanced, V_balanced, labels_balanced
def get_balanced_data_crop(Z, V, labels, classes=[0,1], proportions=[1, 9],
                           seed = 777):
    """
    here we get balanced data using removing random data
    """

    classes = np.array(classes)
    Z = np.array(Z)
    V = np.array(V)
    Z_balanced, V_balanced, labels_balanced = [], [], []
    whers = []
    labels= np.array(labels)
    for i in range(len(classes)):
        whers.append(np.where(labels == classes[i])[0])
#    max_prop = np.max(proportions)
    max_prop_id = np.argmax(proportions)
    for i in range(len(classes)):
        if(not i == max_prop_id):
            np.random.seed(seed)
            ids = np.random.choice(len(whers[i]), len(whers[max_prop_id]))
            Z_balanced.extend(Z[whers[i][ids]])
            V_balanced.extend(V[whers[i][ids]])
            labels_balanced.extend(labels[whers[i][ids]])
        else:
            Z_balanced.extend(Z[whers[i]])
            V_balanced.extend(V[whers[i]])
            labels_balanced.extend(labels[whers[i]])
        #print(np.shape(Z_balanced))
    return Z_balanced, V_balanced, labels_balanced
def normalize_test(x, mean, std):
    x = (x -mean)/std
    return x

def normalize(X_train, X_test):
    X_train = np.array(X_train)
    X_test = np.array(X_test)
    _shape = np.shape(X_train)
    print(_shape)
    """normalizing the data """
    if(len(_shape) == 4):
        mean = np.mean(X_train, axis=(0, 1, 2, 3))
        std = np.std(X_train, axis=(0, 1, 2, 3))
    if(len(_shape) == 3):
        mean = np.mean(X_train, axis=(0, 1, 2))
        std = np.std(X_train, axis=(0, 1, 2))
    elif(len(_shape) == 2):
        mean = np.mean(X_train, axis=(0, 1))
        std = np.std(X_train, axis=(0, 1))
    else:
        raise BaseException("bunday datani normalize qila olmayman " +
                            str(len(_shape)))
    X_train = (X_train - mean) / std
    X_test = (X_test - mean) / std
    obj = {"mean":mean, "std":std}
    with open("normalizer.pkl",'wb+') as f:
        pickle.dump(obj, f)

    return X_train, X_test

# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# end of global functions -----------------------------------------------------


from core.TaxonExpression import TaxonArrayIndex, TaxonArrayValue, TaxonBinOp, TaxonConst, TaxonIdExpr, TaxonFieldExpr, TaxonTernaryOp, TaxonThis, TaxonSuper, TaxonUnOp, TaxonTrue, TaxonFalse, TaxonNull, TaxonVoid
from core.TaxonCall import TaxonCall, TaxonNew
from Wpp.expr.parseExpr import slash
from Wpp.WppExpression import WppExpression

revSlash = {value:'\\'+key for key, value in slash.items()}

class WppConst(TaxonConst, WppExpression):
	__slots__ = ()
	def exportString(self):
		if self.constType != 'string':
			return self.value
		s = ''
		for c in self.value:
			s += revSlash.get(c) or c
		return '"'+s+'"'

class WppTrue(TaxonTrue, WppExpression):
	__slots__ = ()
	def exportString(self):
		return 'true'

class WppFalse(TaxonFalse, WppExpression):
	__slots__ = ()
	def exportString(self):
		return 'false'

class WppNull(TaxonNull, WppExpression):
	__slots__ = ()
	def exportString(self):
		return 'null'

class WppVoid(TaxonVoid, WppExpression):
	__slots__ = ()
	def exportString(self):
		return 'void'

class WppIdExpr(TaxonIdExpr, WppExpression):
	__slots__ = ()
	def exportString(self):
		return self.id

class WppFieldExpr(TaxonFieldExpr, WppExpression):
	__slots__ = ()
	def exportString(self):
		return self.id

class WppThis(TaxonThis, WppExpression):
	__slots__ = ()
	def exportString(self):
		return 'this'

class WppSuper(TaxonSuper, WppExpression):
	__slots__ = ()
	def exportString(self):
		return 'super'

class WppCall(TaxonCall, WppExpression):
	__slots__ = ()
	def _setDecl(self, decl):
		# Возможная замена на new
		if decl.type == 'Class':
			self.replaceByNew(decl)
		else:
			super()._setDecl(decl)

	def replaceByNew(self, decl):
		taxonNew = WppNew()
		for i in self.items:
			taxonNew.addItem(i)
		taxonNew.declTaxon = decl

		self.replace(taxonNew)
		self.trace('WppCall: Replaced %s' % (taxonNew.getDebugStr()))

		# Требуется проверить наличие соответствующих конструкторов
		constructorOver = taxonNew.declTaxon.findConstructor()
		if not constructorOver:
			# Если нет конструктора в классе, значит можно вызывать только без параметров - Atom()
			if len(taxonNew.getArguments()) != 0:
				self.throwError('Class %s have no constructor' % (decl.name))
		else:
			constructorOver.find(taxonNew)

class WppNew(TaxonNew, WppExpression):
	__slots__ = ()
	pass

class WppUnOp(TaxonUnOp, WppExpression):
	__slots__ = ()
	def exportString(self):
		return self.opCode + self.priorExportString(self.getArgument())

nearBinOps = {'.'}
class WppBinOp(TaxonBinOp, WppExpression):
	__slots__ = ()
	def __init__(self):
		super().__init__()
		self.quasiType = None

	def exportString(self):
		op = self.opCode
		if op not in nearBinOps:
			op = ' ' + op + ' '
		return self.priorExportString(self.getLeft()) + op + self.priorExportString(self.getRight())
	def onUpdate(self):
		class CheckStatic:
			""" Проверка использования статических полей с классом """
			def check(self):
				binOp = self.taxon
				return binOp.getLeft().isReady() and binOp.getRight().isReady()
			def exec(self):
				taxon = self.taxon
				left = taxon.getLeft()
				isLeftClass =  left.refDecl.target.isClass() if hasattr(left, 'refDecl') else False
				rightDecl = taxon.getRight().refDecl.target
				isRightStatic = 'static' in rightDecl.attrs
				if isLeftClass and not isRightStatic:
					taxon.throwError('Non-static member "%s" cannot be applied to class' % (rightDecl.name))
				if not isLeftClass and isRightStatic:
					taxon.throwError('Static member "%s" can be applied to class only' % (rightDecl.name))
			def __str__(self):
				return 'CheckStatic %s' % (self.taxon.getDebugStr())
		res = super().onUpdate()
		if self.opCode == '.':
			self.addTask(CheckStatic())
		return res

class WppTernaryOp(TaxonTernaryOp, WppExpression):
	__slots__ = ()
	def exportString(self):
		div = [' ? ', ' : ', '']
		res = ''
		for j, item in enumerate(self.items):
			res += self.priorExportString(item)
			res += div[j]
		return res

class WppArrayIndex(TaxonArrayIndex, WppExpression):
	__slots__ = ()
	def exportString(self):
		return '%s[%s]' % (self.getArrayInstance().exportString(), self.getIndexTaxon().exportString())
	def _checkIndexType(self):
		""" Индекс таксона должен каститься в unsigned long """
		# Индексы строгие, как в C++, Java, JS.
		# В PHP отрицательный индекс используется, как ключ ассоциативного массива. В Python - отсчитывает элемент от конца
		# Поэтому для совместимости все индексы беззнаковые целые
		from core.TaxonScalar import TaxonScalar
		from core.QuasiType import QuasiType
		requiredType = TaxonScalar.createByName('long')
		requiredType.attrs.add('unsigned')
		code, errMsg = QuasiType.matchTaxons(requiredType, self.getIndexTaxon())
		if errMsg:
			self.throwError(errMsg)
	def onUpdate(self):
		result = super().onUpdate()
		# Проконтролировать тип индекса. Используется только для Wpp
		class TestIndex:
			def check(self):
				return self.taxon.getIndexTaxon().isReadyFull()
			def exec(self):
				self.taxon._checkIndexType()
			def __str__(self):
				return 'TestArrayIndex'
		self.addTask(TestIndex())
		return result

class WppArrayValue(TaxonArrayValue, WppExpression):
	__slots__ = ()
	pass


import tensorflow as tf
import numpy as np

#cifar10 import
from tensorflow.keras.datasets.cifar10 import load_data

#다음 배치를 읽어오기 위한 next_batch 유틸리티 함수를 정의한다
def next_batch(num, data, labels):
	#num 개수 만큼 랜덤한 샘플과 레이블을 리턴함
	idx = np.arange(0, len(data))
	np.random.shuffle(idx)
	idx = idx[:num] #slicing
	data_shuffle = [data[i] for i in idx]
	labels_shuffle = [labels[i] for i in idx]

	return np.asarray(data_shuffle), np.asarray(labels_shuffle)

#CNN model definition
def build_CNN_classifier(x):
	x_image = x #input image

	#first convolution layer : 하나의 grayscale 이미지를 64개의 feature로 mapping함
	W1_conv = tf.Variable(tf.truncated_normal(shape=[5,5,3,64], stddev=5e-2))
	b1_conv = tf.Variable(tf.constant(0.1, shape=[64]))
	h1_conv = tf.nn.relu(tf.nn.conv2d(x_image, W1_conv, strides=[1,1,1,1], padding='SAME') + b1_conv)

	#first pooling layer
	h1_pool = tf.nn.max_pool(h1_conv, ksize=[1, 3, 3, 1], strides=[1,2,2,1], padding='SAME')

	#second convolution layer : 32개의 feature를 64개의 feature로 mapping함.
	W2_conv = tf.Variable(tf.truncated_normal(shape=[5,5,64,64], stddev=5e-2))
	b2_conv = tf.Variable(tf.constant(0.1, shape=[64]))
	h2_conv = tf.nn.relu(tf.nn.conv2d(h1_pool, W2_conv, strides=[1,1,1,1], padding='SAME')+b2_conv)

	#second pooling layer
	h2_pool = tf.nn.max_pool(h2_conv, ksize=[1,3,3,1], strides=[1,2,2,1], padding='SAME')

	#third convolution layer : 64->128로
	W3_conv = tf.Variable(tf.truncated_normal(shape=[3, 3, 64, 128], stddev=5e-2))
	b3_conv = tf.Variable(tf.constant(0.1, shape=[128]))
	h3_conv = tf.nn.relu(tf.nn.conv2d(h2_pool, W3_conv, strides=[1, 1, 1, 1], padding='SAME') + b3_conv)

	#4th convolution layer : 128개의 feature로 mapping함
	W4_conv = tf.Variable(tf.truncated_normal(shape=[3, 3, 128, 128], stddev=5e-2))
	b4_conv = tf.Variable(tf.constant(0.1, shape=[128]))
	h4_conv = tf.nn.relu(tf.nn.conv2d(h3_conv, W4_conv, strides=[1, 1, 1, 1], padding='SAME') + b4_conv)

	#5th convolution layer : 128개의 feature로 mapping함
	W5_conv = tf.Variable(tf.truncated_normal(shape=[3, 3, 128, 128], stddev=5e-2))
	b5_conv = tf.Variable(tf.constant(0.1, shape=[128]))
	h5_conv = tf.nn.relu(tf.nn.conv2d(h4_conv, W5_conv, strides=[1, 1, 1, 1], padding='SAME') + b5_conv)

	#Fully Connected Layer 1 : 2번의 dwonsapling 후에 32x32이미지를 8x8x128 feature map으로 바꿈
	#이거를 384개의 feature로 mapiing함
	W1_fc = tf.Variable(tf.truncated_normal(shape=[8 * 8 * 128, 384], stddev=5e-2))
	b1_fc = tf.Variable(tf.constant(0.1, shape=[384]))

	h5_conv_flat = tf.reshape(h5_conv, [-1, 8*8*128])
	h1_fc = tf.nn.relu(tf.matmul(h5_conv_flat, W1_fc) + b1_fc)

	# Dropout : 모델의 복잡도를 컨트롤함. feature의 co-adaptation을 방지
	h1_fc_drop = tf.nn.dropout(h1_fc, keep_prob)

	# Fully Connected Layer 2 : 384개의 feature를 10개의 클래스-airplane, automobile, bird...로 maping함
	W2_fc = tf.Variable(tf.truncated_normal(shape=[384, 10], stddev=5e-2))
	b2_fc = tf.Variable(tf.constant(0.1, shape=[10]))
	logits = tf.matmul(h1_fc_drop, W2_fc) + b2_fc
	y_pred = tf.nn.softmax(logits)

	return y_pred, logits

#input, output data, dropout 확률을 입력 받기 위한 placeholder를 정의함
x = tf.placeholder(tf.float32, shape=[None, 32, 32, 3])
y = tf.placeholder(tf.float32, shape=[None, 10])
keep_prob = tf.placeholder(tf.float32)

#cifar-10 data를 다운로드하고 불러옴
(x_train, y_train), (x_test, y_test) = load_data()
#scalar 형태의 레이블(0~9)을 one-hot encoding 형태로 변환함
y_train_one_hot = tf.squeeze(tf.one_hot(y_train, 10), axis=1)
y_test_one_hot = tf.squeeze(tf.one_hot(y_test, 10), axis=1)

#convolutional neural networks(CNN) 그래프를 생성함
y_pred, logits = build_CNN_classifier(x)

#Cross Entropy를 loss function으로 정의, RMSPropOptimizer를 이용해서 loss를 최소화
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=logits))
train_step = tf.train.RMSPropOptimizer(1e-3).minimize(loss)

#정확도 계산 연산을 추가함
correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

#session open, trainning
with tf.Session() as sess:
	#reset
	sess.run(tf.global_variables_initializer())

	#10000 step optimizer
	for i in range(10000):
		batch = next_batch(128, x_train, y_train_one_hot.eval())
		#100 step마다 정확도와 loss를 출력함
		if i%100 == 0:
			train_accuracy = accuracy.eval(feed_dict={x: batch[0], y: batch[1], keep_prob:1.0})
			loss_print = loss.eval(feed_dict={x: batch[0], y: batch[1], keep_prob: 1.0})

			print("Epoch: %d, accuracy(정확도): %f, loss(손실): %f" % (i, train_accuracy, loss_print))

		#20% 확률의 dropout을 이용해서 학습 1-0.2 = 0.8
		sess.run(train_step, feed_dict={x: batch[0], y: batch[1], keep_prob: 0.8})

	#학습이 끝나면 테스트 데이터에 대한 정확도를 출력함
	test_accuracy = 0.0
	for i in range(10):
		test_batch = next_batch(1000, x_test, y_test_one_hot.eval())
		test_accuracy = test_accuracy + accuracy.eval(feed_dict={x: test_batch[0], y: test_batch[1], keep_prob: 1.0})
	test_accuracy = test_accuracy / 10;
	print("테스트 데이터 정확도: %f" % test_accuracy)


#coding=utf-8
import numpy as np
import numpy.linalg as LA
import random
import MySQLdb
import json

def getDocDistance(a, b):
    if LA.norm(a)==0 or LA.norm(b)==0:
        return -1
    return round(np.inner(a, b) / (LA.norm(a) * LA.norm(b)), 4)

#test sample
my_randoms = []
for each in range(26):
    my_randoms.append(random.random())

def getRecommendCluster(a): #輸入向量
    db = MySQLdb.connect("10.120.26.46","yang","iiizb104","project" )
    cursor = db.cursor()
    sql = "SELECT clusterVector FROM project.cluster;"

    cursor.execute(sql)
    results = cursor.fetchall()

    tmpCom = []
    for row in results:
        vec = json.loads(row[0])
        tmpCom.append(getDocDistance(vec, a))

    # print ('max value:', max(tmpCom))
    return [i for i, j in enumerate(tmpCom) if j == max(tmpCom)][0]

def getRecommendSKill(vec, cluster): #分別輸入向量和群
    db = MySQLdb.connect("10.120.26.46","yang","iiizb104","project" )
    cursor = db.cursor()
    sql = "SELECT clusterVector FROM project.cluster where cID = {};".format(cluster)
    sql2 = "SELECT dict FROM project.dict where no = 1;"

    cursor.execute(sql)
    Vector = cursor.fetchone()
    cVector = json.loads(Vector[0])

    cursor.execute(sql2)
    skills = cursor.fetchone()
    skill_list = json.loads(skills[0])

    # vec = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0]

    ind = [i for i, j in enumerate(vec) if j == 0]

    skill_list_new = []
    cVector_new = []

    for each in ind:
        skill_list_new.append(skill_list[each])
        cVector_new.append(cVector[each])

    recSkill = skill_list_new[cVector_new.index(max(cVector_new))]

    return recSkill


import sys
sys.setrecursionlimit(10 ** 6)

def input():
    return sys.stdin.readline()[:-1]

def dfs(now, prev=-1):
    for next in tree[now]:
        # 1個前のノードを参照した時はcontinue
        if next == prev:
            continue

        # 現在のポイントを次のノードへ加算して累積和をとる
        point[next] += point[now]

        # 次のノードへ
        dfs(next, now)

N, Q = map(int, input().split())
tree = [[] for _ in range(N)]
point = [0]*N

# 木構造を2次元配列で表現。要素Xのノードからつながっているノード番号が格納される。
for _ in range(N-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)

# 各ノードのポイントを求める
for _ in range(Q):
    p, x = map(int, input().split())
    point[p-1] += x

dfs(0)
print(*point)


from unittest import TestCase
from s3.network import TrackNetwork
from s3.interlocking import Interlocking
from s3.lever import Lever
from s3.signal import Signal

class InterlockingTest(TestCase):

	@classmethod
	def setUpClass(cls):
		cls.t = TrackNetwork("test")

	def test_alpha_id(self):
		i = Interlocking("Test", self.t)
		self.assertEqual(i.id, "Test")

	def test_digit_id(self):
		i = Interlocking(10, self.t)
		self.assertEqual(i.id, "10")

	def test_levers(self):
		i = Interlocking("Test2", self.t)
		self.assertEqual(len(i.levers), 0)

	def test_network(self):
		i = Interlocking("Test3", self.t)
		self.assertIs(i.network, self.t)
		self.assertIn(i, self.t.interlockings)

	def test_one_signal_levers(self):
		i = Interlocking("Test4", self.t)

		l1 = Lever(1, i)
		s1 = Signal(lever=l1)

		l2 = Lever(2, i)
		s2 = Signal(lever=l2, label="A")
		s3 = Signal(lever=l2, label="B")

		levers = i.one_signal_levers
		self.assertIn(l1, levers)
		self.assertNotIn(l2, levers)


import os
import pandas as pd
import numpy as np

def SQL_Insert(SOURCE, TARGET):
    sql_texts = []
    for index, row in SOURCE.iterrows():
        sql_texts.append(
            'INSERT INTO ' + TARGET + ' (' + str(', '.join(SOURCE.columns)) + ')   VALUES ' + str(tuple(row.values))+";")

    return ('\n'.join(sql_texts))

def users_adapter_to_db(df):
        users_df = df.filter(['first_name', 'last_name', 'phone', 'email', 'start_date'])
        users_df['id'] = df.index+1
        users_df['club_id'] = club_id
        users_df.rename(columns={"start_date": "joined_at"}, inplace = True)
        users_df_reformatted = users_df.reindex(columns=['id', 'first_name', 'last_name', 'phone', 'email', 'joined_at', 'club_id'])
        return (users_df_reformatted)

def memberships_adapter_to_db(users_df_reformatted, df):
        memberships_df = df.filter(['user_id', 'start_date', 'end_date', 'membership_name'])
        memberships_df['id'] = df.index+1
        memberships_df['user_id'] = users_df_reformatted.id
        memberships_df_reformatted = memberships_df.reindex(columns=['id', 'user_id', 'start_date', 'end_date', 'membership_name'])
        return (memberships_df_reformatted)

# check if db file exists
if os.path.isfile("jimalaya.xlsx"):
    # read db file
    df = pd.read_excel('jimalaya.xlsx')
    # check for duplicated email in the file
    sr = df["email"]
    # make sure primary field is unique:
    if (sr.is_unique):
        club_id = '2400'

        # casting date
        df["start_date"] = df.membershp_start_date.dt.strftime('%m/%d/%Y')
        df["end_date"] = df.membership_end_date.dt.strftime('%m/%d/%Y')

        # replacin nan values with None
        df = df.astype('object').where(pd.notnull(df),None)

        # manipulating data to match db dataframe
        users_df_reformatted = users_adapter_to_db(df)
        memberships_df_reformatted = memberships_adapter_to_db(users_df_reformatted, df)

        # generating create table statements
        print(pd.io.sql.get_schema(users_df_reformatted.reset_index(), 'users'))
        print(pd.io.sql.get_schema(memberships_df_reformatted.reset_index(), 'memberships'))

        # calling insert functions
        usersq = SQL_Insert(users_df_reformatted, 'users')
        membershipsq = SQL_Insert(memberships_df_reformatted, 'memberships')

        #print output
        print(usersq)
        print(membershipsq)
    else:
        print("Email is duplicated")
else:
    print("File not found")


import requests
import sys

def getURL(url, session, logger, abortOnFail=False):
    '''
    :param url: url to get
    :param session: requests.Session() instance
    :param logger: logging instance
    :return:
    '''

    try:
        page = session.get(url)
        logger("Downloaded url: %s" % url)
    except:
        msg = "ERROR: failed to get url: " + url
        logger(msg)
        if abortOnFail:
            sys.exit(msg)
        else:
            return None
    return page

def writeHTMLPageToFile(fname_output, page, logger):
    '''
    :param fname_output: path + filename for output html
    :param page: a Session.get(url) object (see getURL())
    :return:
    '''
    try:
        open(fname_output, 'wb').write(page.content)
        logger("Wrote webpage to file: %s" % fname_output)
    except:
        logger("ERROR: Failed to write webpage to file: %s" % fname_output)


#!/usr/bin/python3

# The MIT License (MIT)
#
# Copyright (c) 2020 Susam Pal
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""Scrape https://www.mohfw.gov.in/ to retrieve COVID-19 statistics.

To run this scraper and print a summary of the scraped data, enter this
command at the top-level directory of this project:

    python3 -m py.mohfw
"""

import datetime
import json
import re
import sys
import urllib
import urllib.request

from py import log

class Data:
    def __init__(self):
        """Container for all data read and derived from MoHFW."""
        self.total = -1
        self.active = -1
        self.cured = -1
        self.death = -1
        self.migrated = -1
        self.ref_datetime = None
        self.ref_date = ''
        self.ref_time = ''
        self.foreign = -1
        self.regions = {}
        self.regions_total = -1
        self.regions_cured = -1
        self.regions_death = -1
        self.regions_active = -1

def load_home_data():
    """Return data retrieved from MoHFW website."""
    data = Data()

    # Save the response from MoHFW as a list of lines.
    url = 'https://www.mohfw.gov.in/'
    log.log('Connecting to {} ...', url)
    response = urllib.request.urlopen(url).read().decode('utf-8')
    lines = [l.strip() for l in response.splitlines()]
    lines = [l for l in lines if l != '']

    # Parsers.
    strong_re = re.compile(r'.*(.*)')
    time_re = re.compile(r'.*as on\s*:\s*(\d.*) IST')
    foreign_re = re.compile(r'.*[Ii]ncluding (\d+)? ?[Ff]oreign')
    td_re = re.compile(r'.*([^#]*).*	')
    parser_state = 'DEFAULT'

    # Parse the response.
    for i, line in enumerate(lines):
        if data.active == -1 and 'Active Cases' in line:
            data.active = int(strong_re.match(lines[i - 1]).group(1))
        elif data.cured == -1 and 'Cured' in line:
            data.cured = int(strong_re.match(lines[i - 1]).group(1))
        elif data.death == -1 and 'Deaths' in line:
            data.death = int(strong_re.match(lines[i - 1]).group(1))
        elif data.migrated == -1 and 'Migrated' in line:
            data.migrated = int(strong_re.match(lines[i - 1]).group(1))
        elif data.ref_datetime == None and 'as on' in line:
            t = time_re.match(line).group(1)
            data.ref_datetime = datetime.datetime.strptime(t, '%d %B %Y, %H:%M')
            data.ref_date = data.ref_datetime.strftime('%Y-%m-%d')
            data.ref_time = data.ref_datetime.strftime('%H:%M')
        elif data.foreign == -1 and 'foreign' in line:
            n = foreign_re.match(line).group(1)
            if n is not None:
                data.foreign = int(n)
        elif '
' in line:
            parser_state = 'REGION'
        elif parser_state == 'REGION' and '
' in line:
            if 'Total' in lines[i + 2]:
                parser_state = 'REGION_TOTAL'
                continue
            # Parse
            region_name = td_re.match(lines[i + 2]).group(1)
            active = td_re.match(lines[i + 3]).group(1)
            cured = td_re.match(lines[i + 4]).group(1)
            death = td_re.match(lines[i + 5]).group(1)
            total = td_re.match(lines[i + 6]).group(1)
            # Normalize
            if region_name.startswith('Cases being reassigned'):
                region_name = 'reassigned'
            total = int(total) if total else -1
            cured = int(cured) if cured else -1
            death = int(death) if death else -1
            active = int(active) if active else -1
            # Save
            data.regions[region_name] = (total, active, cured, death)
        elif parser_state == 'REGION_TOTAL' and 'Total' in line:
            parser_state = 'DEFAULT'
            data.regions_active = int(strong_re.match(lines[i + 1]).group(1))
            data.regions_cured = int(strong_re.match(lines[i + 3]).group(1))
            data.regions_death = int(strong_re.match(lines[i + 6]).group(1))
            data.regions_total = int(strong_re.match(lines[i + 9]).group(1))

    data.total = data.active + data.cured + data.death + data.migrated

    # Validations.
    if data.total != data.regions_total:
        log.log('home page: Mismatch in total and regions_total')
    if data.active != data.regions_active:
        log.log('home page: Mismatch in active and regions_active')
    if data.cured + data.migrated != data.regions_cured:
        log.log('home page: Mismatch in cured + migrated and regions_cured')
    if data.death != data.regions_death:
        log.log('home page: Mismatch in death and regions_death')
    return data

def load_dash_data():
    """Return data retrieved from MoHFW dashboard page."""
    data = Data()

    # Retrieve MoHFW dashboard HTML.
    url = 'https://www.mohfw.gov.in/index.php'
    log.log('Connecting to {} ...', url)
    response = urllib.request.urlopen(url).read().decode('utf-8')
    lines = [l.strip() for l in response.splitlines()]
    lines = [l for l in lines if l != '']

    # Parsers.
    strong_re = re.compile(r'.*(.*)')
    time_re = re.compile(r'.*as on\s*:\s*(\d.*) IST')
    js_re = re.compile(r"\['(.*)', (.*), (.*), (.*)\],")
    parser_state = 'DEFAULT'

    # Parse the response.
    for i, line in enumerate(lines):
        if data.active == -1 and 'Active Cases' in line:
            data.active = int(strong_re.match(lines[i - 1]).group(1))
        elif data.cured == -1 and 'Cured' in line:
            data.cured = int(strong_re.match(lines[i - 1]).group(1))
        elif data.death == -1 and 'Deaths' in line:
            data.death = int(strong_re.match(lines[i - 1]).group(1))
        elif data.migrated == -1 and 'Migrated' in line:
            data.migrated = int(strong_re.match(lines[i - 1]).group(1))
        elif data.ref_datetime == None and 'as on' in line:
            t = time_re.match(line).group(1)
            data.ref_datetime = datetime.datetime.strptime(t, '%d %B %Y, %H:%M')
            data.ref_date = data.ref_datetime.strftime('%Y-%m-%d')
            data.ref_time = data.ref_datetime.strftime('%H:%M')
        elif 'Hover' in line:
            break

    data.total = data.active + data.cured + data.death + data.migrated

    # Retrieve MoHFW JSON data.
    url = 'https://www.mohfw.gov.in/data/data.json'
    log.log('Connecting to {} ...', url)
    items = json.load(urllib.request.urlopen(url))

    # Parse the response.
    for item in items:
        region_name = item['state_name']
        total = int(item['positive'])
        cured = int(item['cured'])
        death = int(item['death'])
        active = total - cured - death
        data.regions[region_name] = (total, active, cured, death)

    # Region totals.
    data.regions_total = sum(v[0] for v in data.regions.values())
    data.regions_active = sum(v[1] for v in data.regions.values())
    data.regions_cured = sum(v[2] for v in data.regions.values())
    data.regions_death = sum(v[3] for v in data.regions.values())

    # Validations.
    if data.total != data.regions_total:
        log.log('dashboard: Mismatch in total and regions_total')
    if data.active != data.regions_active:
        log.log('dashboard: Mismatch in active and regions_active')
    if data.cured + data.migrated != data.regions_cured:
        log.log('dashboard: Mismatch in cured + migrated and regions_cured')
    if data.death != data.regions_death:
        log.log('dashboard: Mismatch in death and regions_death')

    return data

def make_summary(data):
    """Print summary of data on the terminal."""
    out = []
    out.append('ref_datetime: {}'.format(data.ref_datetime))
    out.append('overall: total: {}; active: {}; cured: {}; death: {}; '
               'migrated: {}'.format(data.total, data.active,
                                     data.cured, data.death, data.migrated))
    out.append('regions: total: {}; active: {}; cured: {}; death: {}; '
               'foreign: {}'.format(data.regions_total, data.regions_active,
                                    data.regions_cured, data.regions_death,
                                    data.foreign))
    out.append('')
    out.append('regions: {}'.format(sorted(data.regions.items())))
    out.append('')
    return '\n'.join(out)

def make_json_entry(data):
    """Return JSON entry to be added to indiacovid19.json."""
    return ('  [ "{}",  {:5},  {:5},  {:5},  {:5},  "{} {}",  '
            '"https://indiacovid19.github.io/webarchive/mohfw/{}_{}/",'
            '      "" ]'
            .format(data.ref_date, data.active, data.cured, data.death,
                    data.migrated, data.ref_date, data.ref_time,
                    data.ref_date, data.ref_time.replace(':', '')))

def update_json(json_entry):
    """Update indiacovid19.json with the specified JSON entry."""
    with open('indiacovid19.json') as f:
        j = f.read()
    if json_entry in j:
        print('JSON archive is up-to-date')
        return
    print('Updating JSON archive ...')
    lines = j.splitlines()
    for i, line in enumerate(lines):
        if 1 < i < len(lines) - 1 and line.endswith(']'):
            lines[i] = line + ','
    lines[len(lines) - 1] = json_entry
    lines.append(']')
    output = '\n'.join(lines) + '\n'
    with open('indiacovid19.json', 'w') as f:
        f.write(output)
    print('Done')

def print_summary(data, heading):
    """Print a summary of the retrieved data on console."""
    print(heading)
    print('-' * len(heading))
    print(make_summary(data))
    json_entry = make_json_entry(data)
    print('JSON octuple for indiacovid19.json:')
    print()
    print(json_entry)
    print()
    update_json(json_entry)
    print()

def main():
    home_data = load_home_data()
    #dash_data = load_dash_data()
    print()
    print_summary(home_data, 'HOME PAGE DATA')
    #print_summary(dash_data, 'DASHBOARD DATA')

if __name__ == '__main__':
    main()


from __future__ import print_function
import dropbox
import os
import time
import contextlib
from datetime import datetime

def main():
    key = 'secret'  # change this key with yours
    dbx = dropbox.Dropbox(key)

    now = datetime.now()

    try:
        overwrite = True
        dbxpath = '/'+str(now.day)+'-'+str(now.month)+'-db.sql'
        mode = (dropbox.files.WriteMode.overwrite
                if overwrite
                else dropbox.files.WriteMode.add)
        with open('db.sql', 'rb') as f:
            data = f.read()
        with stopwatch('upload %d bytes' % len(data)):
            res = dbx.files_upload(data, dbxpath, mode)
    except dropbox.exceptions.ApiError as err:
        print('*** API error', err)
        return None
    print('uploaded as', res.name.encode('utf8'))
    return res

@contextlib.contextmanager
def stopwatch(message):
    """Context manager to print how long a block of code took."""
    time0 = time.time()

    try:
        yield
    finally:
        time1 = time.time()
        print('total elapsed time for %s: %.3f' % (message, time1 - time0))

if __name__ == '__main__':
    main()


import matplotlib.pyplot as plt
import statsmodels.tsa.api as smt

import mxnet as mx

import seaborn as sns

def tsplot(y, lags=None, figsize=(10, 8)):
    fig = plt.figure(figsize=figsize)
    layout = (2, 2)
    ts_ax = plt.subplot2grid(layout, (0, 0), colspan=2)
    acf_ax = plt.subplot2grid(layout, (1, 0))
    pacf_ax = plt.subplot2grid(layout, (1, 1))

    y.plot(ax=ts_ax)
    smt.graphics.plot_acf(y, lags=lags, ax=acf_ax)
    smt.graphics.plot_pacf(y, lags=lags, ax=pacf_ax)
    [ax.set_xlim(0.5) for ax in [acf_ax, pacf_ax]]
    sns.despine()
    plt.tight_layout()
    return ts_ax, acf_ax, pacf_ax

def predict_dynamic(model, seq_input, other_input, predict_seq_len):

    pred_batch_size, input_seq_len, feature_dim = seq_input.shape

    # this buffer holds the input and output as we fill things in
    inp_buffer = mx.nd.zeros((pred_batch_size, input_seq_len + predict_seq_len, feature_dim),
                             ctx=seq_input.context)
    inp_buffer[:, :input_seq_len, :] = seq_input
    t_buffer = mx.nd.zeros((pred_batch_size, predict_seq_len, feature_dim),
                             ctx=seq_input.context)

    num_predictions = 1
    inp = inp_buffer[:, :input_seq_len, :]
    for j, other_inp in enumerate(other_input):
        t_output = model.tcn.forward(inp)
        output = model.forward((inp, other_inp))
        # for train_sequences, we need to grab every num_predictionth prediction
        # output_idx = mx.nd.arange(0, output.shape[0], num_predictions,
        #                           ctx=seq_input.context) + num_predictions - 1
        # last_outputs = output[output_idx, :]
        inp_buffer[:, input_seq_len + j:input_seq_len + j + 1, :] = \
            output.reshape((pred_batch_size, 1, -1))
        t_buffer[:, j:j + 1, :] = t_output.reshape((pred_batch_size, 1, -1))
        inp = inp_buffer[:, j + 1:input_seq_len + j + 1, :]
    outputs = inp_buffer[:, input_seq_len:, :]
    return outputs, t_buffer