import tkinter as tk #Module imported for Graphical user interface
from tkinter import ttk
import turtle as t #Module imported to draw Ghana Flag on tkinter canvas
from datetime import date #Module imported to validate age of users

#Password for Registrar is vtRs_20_regstr!$

V={} #Initialize Voter's List as empty dictionary

Vread=open("Voters_Register.txt","r") #Open's text file to read Voter's information
#Loop to append each line in text file as a list in the Voters dictionary, Voter's ID is the key
for i in Vread:
    d=i.split("\t")
    d.remove('\n')
    if len(d)>0:
        V[int(d[0])]=d
    else:
        V={}
Vread.close()

#Function to delete information from entries
def clear_entries():
    ent_first_name.delete(0,tk.END)
    ent_middle_name.delete(0,tk.END)
    ent_last_name.delete(0,tk.END)
    ent_DOB.delete(0,tk.END)
    gnd.set("Please select your Gender")
    var.set("Please Select your region")
    ent_country.delete(0,tk.END)
    ent_vo_id.delete(0,tk.END)

#Function to recieve data from entries and write unto file
def update_list():
    from datetime import date
    global V
    days = 365.2425
    f_name=ent_first_name.get()
    m_name=ent_middle_name.get()
    l_name=ent_last_name.get()
    DOB=ent_DOB.get()
    gender=gnd.get()
    region=var.get()
    country=ent_country.get()
    card_id=ent_vo_id.get()

    Vs=open("Voters_Register.txt","a+") #Open text file to read and write
    #Check age of Voter
    try:
        birthDate=date(int(DOB.split("/")[2]),int(DOB.split("/")[1]),int(DOB.split("/")[0]))
        age = int((date.today() - birthDate).days / days)
    except IndexError or ValueError:
        tk.messagebox.showwarning("Warning", "Inappropriate date format")

    #Conditional statements to ensure form is complete
    if (len(card_id) and len(f_name) and len (m_name)
        and len (l_name) and len(DOB) and len(gender)
        and len(region) and len(country)!=0
        and region!="Please Select your region"
        and gender!="Please select your Gender"):

        #Conditional statements to ensure user is an Eligible voter
        if age>=18 and country=="Ghanaian":

            if int(card_id)not in V.keys():
                Vs.write(card_id+'\t' )
                Vs.write(f_name+'\t' )
                Vs.write(m_name+'\t' )
                Vs.write(l_name+'\t' )
                Vs.write(DOB+'\t')
                Vs.write(gender+'\t')
                Vs.write(region+'\t')
                Vs.write(country+'\t'+'\n')
                Vs.close()
                Vread=open("Voters_Register.txt","r")
                tk.messagebox.showinfo("Success!","Form has been submitted succesfully")
                for i in Vread:
                    d=i.split("\t")
                    d.remove('\n')
                    V[int(card_id)]=d
            else:
                tk.messagebox.showerror("Error", "Voters ID already in system")
        else:
            tk.messagebox.showerror("Error", "User is not an eligible voter")
    else:
        tk.messagebox.showerror("Error","Incomplete Registration")

#Funfunction to open voters list
def openlist():
    global window2

    #Creation of 2nd Window, for the list of voters
    window2=tk.Tk()
    window2.title("Voters_list")

    vtrs_title = tk.Frame(master=window2,relief=tk.SUNKEN, borderwidth=3, height=20)
    vtrs_title.pack(fill=tk.BOTH, expand=False)

    lbl_title = tk.Label(master=vtrs_title, text="Voters Registration List:")
    lbl_title.pack()

    ttle= ["Voters ID Number",
           "First Name",
           "Middle Name",
           "Last Name",
           "Date of Birth(dd/mm/yyyy)",
           "Gender",
           "Nationality",
           "Region"
           ]

    vtrs_row2=tk.Frame(master=window2,relief=tk.SUNKEN, borderwidth=3,height=150)
    vtrs_row2.pack(fill=tk.BOTH,expand=True)

    a=0
    for i in ttle:
        vtrs_col=tk.Frame(master=vtrs_row2,relief=tk.SUNKEN, borderwidth=3,height=40)
        vtrs_col.grid(column=a,row=0,sticky="new")

        a=a+1

        vtrs_ttle=tk.Label(master=vtrs_col, text=i)
        vtrs_ttle.grid(sticky="nsew")

    Vread=open("Voters_Register.txt","r")
    b=0

    #Creation of frames and Listboxes for Voters list window

    vtrs_list0=tk.Frame(master=vtrs_row2,relief=tk.SUNKEN, borderwidth=0.5)
    vtrs_list0.grid(column=0,row=1,sticky="nsew")
    vtrs_row2.rowconfigure(1,weight=1)
    vtrs_row2.columnconfigure(0,weight=1)
    vtrs_list0.rowconfigure(0,weight=1)
    vtrs_list0.columnconfigure(0,weight=1)
    listbox0 = tk.Listbox(master=vtrs_list0)
    listbox0.grid(sticky="nsew")

    vtrs_list1=tk.Frame(master=vtrs_row2,relief=tk.SUNKEN, borderwidth=0.5)
    vtrs_list1.grid(column=1,row=1,sticky="nsew")
    vtrs_row2.rowconfigure(1,weight=1)
    vtrs_row2.columnconfigure(1,weight=1)
    vtrs_list1.rowconfigure(0,weight=1)
    vtrs_list1.columnconfigure(0,weight=1)
    listbox1 = tk.Listbox(master=vtrs_list1)
    listbox1.grid(sticky="nsew")

    vtrs_list2=tk.Frame(master=vtrs_row2,relief=tk.SUNKEN, borderwidth=0.5)
    vtrs_list2.grid(column=2,row=1,sticky="nsew")
    vtrs_row2.rowconfigure(1,weight=1)
    vtrs_row2.columnconfigure(2,weight=1)
    vtrs_list2.rowconfigure(0,weight=1)
    vtrs_list2.columnconfigure(0,weight=1)
    listbox2 = tk.Listbox(master=vtrs_list2)
    listbox2.grid(sticky="nsew")

    vtrs_list3=tk.Frame(master=vtrs_row2,relief=tk.SUNKEN, borderwidth=0.5)
    vtrs_list3.grid(column=3,row=1,sticky="nsew")
    vtrs_row2.rowconfigure(1,weight=1)
    vtrs_row2.columnconfigure(3,weight=1)
    vtrs_list3.rowconfigure(0,weight=1)
    vtrs_list3.columnconfigure(0,weight=1)
    listbox3 = tk.Listbox(master=vtrs_list3)
    listbox3.grid(sticky="nsew")

    vtrs_list4=tk.Frame(master=vtrs_row2,relief=tk.SUNKEN, borderwidth=0.5)
    vtrs_list4.grid(column=4,row=1,sticky="nsew")
    vtrs_row2.rowconfigure(1,weight=1)
    vtrs_row2.columnconfigure(4,weight=1)
    vtrs_list4.rowconfigure(0,weight=1)
    vtrs_list4.columnconfigure(0,weight=1)
    listbox4 = tk.Listbox(master=vtrs_list4)
    listbox4.grid(sticky="nsew")

    vtrs_list5=tk.Frame(master=vtrs_row2,relief=tk.SUNKEN, borderwidth=0.5)
    vtrs_list5.grid(column=5,row=1,sticky="nsew")
    vtrs_row2.rowconfigure(1,weight=1)
    vtrs_row2.columnconfigure(5,weight=1)
    vtrs_list5.rowconfigure(0,weight=1)
    vtrs_list5.columnconfigure(0,weight=1)
    listbox5= tk.Listbox(master=vtrs_list5)
    listbox5.grid(sticky="nsew")

    vtrs_list6=tk.Frame(master=vtrs_row2,relief=tk.SUNKEN, borderwidth=0.5)
    vtrs_list6.grid(column=6,row=1,sticky="nsew")
    vtrs_row2.rowconfigure(1,weight=1)
    vtrs_row2.columnconfigure(6,weight=1)
    vtrs_list6.rowconfigure(0,weight=1)
    vtrs_list6.columnconfigure(0,weight=1)
    listbox6= tk.Listbox(master=vtrs_list6)
    listbox6.grid(sticky="nsew")

    vtrs_list7=tk.Frame(master=vtrs_row2,relief=tk.SUNKEN, borderwidth=0.5)
    vtrs_list7.grid(column=7,row=1,sticky="nsew")
    vtrs_row2.rowconfigure(1,weight=1)
    vtrs_row2.columnconfigure(7,weight=1)
    vtrs_list7.rowconfigure(0,weight=1)
    vtrs_list7.columnconfigure(0,weight=1)
    listbox7= tk.Listbox(master=vtrs_list7)
    listbox7.grid(sticky="nsew")

    scrollbar = tk.Scrollbar(vtrs_list7, orient=tk.VERTICAL)
    #Function for scrollbar control
    def yview(*args):
        listbox0.yview(*args)
        listbox1.yview(*args)
        listbox2.yview(*args)
        listbox3.yview(*args)
        listbox4.yview(*args)
        listbox5.yview(*args)
        listbox6.yview(*args)
        listbox7.yview(*args)

    scrollbar.config(command=yview)
    scrollbar.grid(row=0,column=len(ttle),sticky="nse")
    lbox=[listbox0,listbox1,listbox2,listbox3,listbox4,listbox5,listbox6,listbox7]

    for i in range(len(lbox)):                                                      #Loop to append data into listboxes
        x=0

        for key in V:
            lbox[i].insert(x,V[key][i])
            x=x+1
                                                                                #Function to delete selected user
    def del_user():
        Vread1=open("Voters_Register.txt","r")                                      #Open file to read
        pos = 0
        for line in Vread1:
                                                                                # For each line, check if line contains the string
            if ent_search.get() in line:
                print(pos)
                break
            pos += 1
        Vread1.close()
        try:
            del V[int(ent_search.get())]                                            #Delete ID number stated from Dictionary

        except KeyError or ValueError:
            tk.messagebox.showinfo("my message","User not found in Voter's List")
        Vread2 = open("Voters_Register.txt", "r")                                   #Open file to read
        lines = Vread2.readlines()
        Vread2.close()
        del lines[pos]                                                              #Delete line that contains ID number stated from text file
        Vwrite = open("Voters_Register.txt", "w+")                                  #Open file to write
        for line in lines:                                                          #Write remaining files to text file
            Vwrite.write(line)

        Vwrite.close()
        window2.withdraw()
        openlist()

    def reset():
        Vread3 = open("Voters_Register.txt", "r")                                   #Open file to read
        backup_lines = Vread3.readlines()
        Vread3.close()
        Vbackup = open("Voters_Register_backup.txt", "w+")                          #Open back up file to write
        for line in backup_lines:                                                   #Write remaining files to text file
            Vbackup.write(line)
        Vbackup.close()
        V.clear()                                                                   #Clear Voters Dictionary
        Vs=open("Voters_Register.txt","w")                                          #Open File to write
        window2.withdraw()                                                          #Temporarily close Window
        openlist()                                                                  #Refresh page to display new list

    #Frame to search for Users
    vtrs_search = tk.Frame(master=window2,relief=tk.SUNKEN, borderwidth=3, height=20)
    vtrs_search.pack(fill=tk.BOTH, expand=False)
    btn_delete = tk.Button(master=vtrs_search, text="Delete",command=del_user)
    btn_delete.pack(side=tk.RIGHT, padx=10,pady=10, ipadx=10)

    btn_reset = tk.Button(master=vtrs_search, text="Reset",command=reset)
    btn_reset.pack(side=tk.LEFT, padx=10,pady=10, ipadx=10)

    ent_search = tk.Entry(master=vtrs_search)
    ent_search.pack(side=tk.RIGHT,padx=10,pady=10)
    lbl_search = tk.Label(master=vtrs_search, text="Search for Voter's ID")
    lbl_search.pack(side=tk.RIGHT,padx=10,pady=10)

    window2.mainloop()

def confirm2():
    if entry2.get()=="vtRs_20_regstr!$":
        check.withdraw()
        window.deiconify()
        entry2.delete(0,tk.END)
    else:
       tk.messagebox.showerror("error", "Incorrect password, check code")

def confirm():

    global response
    global btn_dup
    response=choice.get()
    if response==0:
        login.withdraw()
        window.deiconify()
        btn_dup.pack_forget()

        return response
    else:
        login.withdraw()
        check.deiconify()
        return response

def log_out():
    window.withdraw() or check.withdraw()
    login.deiconify()
    entry2.delete(0,tk.END)
    btn_dup.pack(side=tk.RIGHT,padx=10,pady=10, ipadx=10)

#Creation of 1st Window, for data entry
window = tk.Tk()
window.withdraw()
window.title("Voter's Registration")
##login=tk.Tk()
login = tk.Toplevel(window)
login.geometry("700x300")
login_heading=tk.Frame(master=login,relief=tk.SUNKEN,borderwidth=3,height=3)
login_heading.pack(fill=tk.BOTH,side=tk.LEFT, expand=True,pady=30,padx=(20,10))
lbl_login= tk.Label(master=login_heading, text="Welcome to Ghana's \n\nVoters Registeration form")
lbl_login.config(font=("Courier", 20))
lbl_login.pack(expand=False,side=tk.TOP,pady=10)

question = tk.Label(master=login_heading, text="Please indicate your Status:")
question.config(font=("Courier", 14))
question.pack(anchor=tk.CENTER,expand=True)
choice=tk.IntVar()
choice.set(0)
voter=tk.Radiobutton(login, text="Voter",padx = 20,variable=choice,value=0)
voter.config(font=("Times", 14))
voter.pack(anchor=tk.W, expand=True)
registrar=tk.Radiobutton(login, text="Registrar",padx = 20,variable=choice,value=1)
registrar.config(font=("Times", 14))
registrar.pack(anchor=tk.W,expand=True)
ans = tk.Button (login, text = 'Submit', command = confirm,bg="green",fg="white")
ans.pack (pady=10)
check = tk.Toplevel(window)
check.geometry("700x250")

check.withdraw()
heading=tk.Frame(master=check,height=3)
label2 = tk.Label(heading, text="Enter Registrar's passcode")
label2.config(font=("Courier", 18))
heading.pack(pady=(30,20),anchor=tk.CENTER)
entry2 = tk.Entry(check,show="*",width=30)
button2 = tk.Button(check, text="Verify", command=confirm2,bg="green",fg="white",width=7)
button2.config(font=("Times", 14))
btn_logout1 = tk.Button(master=check, text="Logout", command=log_out)

label2.pack(anchor=tk.CENTER)
entry2.pack(anchor=tk.CENTER)

button2.pack(pady=20,ipadx=10,anchor=tk.CENTER)
btn_logout1.pack(padx=10,pady=(30,5), ipadx=10,anchor=tk.CENTER)

vtrs_top=tk.Frame(master=window,borderwidth=1,height=3)
vtrs_top.pack(fill=tk.BOTH,padx=40)

vtrs_heading=tk.Frame(master=vtrs_top,relief=tk.SUNKEN,borderwidth=3,height=3)
vtrs_heading.pack(fill=tk.BOTH,side=tk.LEFT, expand=True,pady=(20,40))
lbl_heading= tk.Label(master=vtrs_heading, text="Welcome to Ghana's Voters Registeration form")
lbl_heading.config(font=("Courier", 14))
lbl_heading.pack(expand=False,side=tk.TOP)
lbl_warning= tk.Label(master=vtrs_heading, text="Please fill this form honestly and to the best of your knowledge ",height=1)
lbl_warning.pack(expand=False)
lbl_disclaimer= tk.Label(master=vtrs_heading, text="AN INACCURATE REPRESENATION OF YOUR TRUE IDENTITY IS A CRIME! ",fg="red")
lbl_disclaimer.pack(expand=False)
canvas = tk.Canvas(master=vtrs_top,width=151,height=121)
canvas.pack(expand=False,side=tk.LEFT,padx=10,pady=10)

#Ghana Flag
Kwad=t.RawTurtle(canvas)
Kwad.speed(speed="fastest")
Kwad.hideturtle()
x=-76
y=61
color=["red","yellow","green"]
for i in color:
    Kwad.penup()
    Kwad.goto(x,y)
    Kwad.pendown()
    Kwad.fillcolor(i)
    Kwad.begin_fill()
    for i in range(2):
        Kwad.forward(150)
        Kwad.right(90)
        Kwad.forward(40)
        Kwad.right(90)
    Kwad.end_fill()
    y=y-40

Kwad.penup()
Kwad.goto(-16,7)
Kwad.pendown()
Kwad.fillcolor('black')
Kwad.begin_fill()
for i in range(5):
    Kwad.forward(30)
    Kwad.right(144)
Kwad.end_fill()

#Creation of the forms frame and entries
vtrs_form = tk.Frame(master=window,relief=tk.SUNKEN, borderwidth=3)
vtrs_form.pack(fill=tk.BOTH, expand=True)

vtrs_form.grid_columnconfigure(0, weight=1)
vtrs_form.grid_columnconfigure(1, weight=1)
for i in range(9):
    vtrs_form.grid_rowconfigure(i, weight=1)

#Function to validate Voters ID entry
def votID(no,P):
    if len(P)<=10:
        if no.isdigit():
            return True
        elif no ==" ":
            return True
        else:
            return False
    else:
        return False

vi=window.register(votID)

lbl_gh_no = tk.Label(master=vtrs_form, text="Voter's ID Number:\n(10 digits)")
ent_vo_id = tk.Entry(master=vtrs_form, width=50)
lbl_gh_no.grid(row=0, column=0, sticky="ew")
ent_vo_id.grid(row=0, column=1,sticky="ew",padx=10,pady=5)
ent_vo_id.config(validate="key",validatecommand=(vi,"%S","%P"))

lbl_first_name = tk.Label(master=vtrs_form, text="First Name:")
ent_first_name = tk.Entry(master=vtrs_form, width=50)
lbl_first_name.grid(row=1, column=0, sticky="ew")
ent_first_name.grid(row=1, column=1,sticky="ew",padx=10,pady=5)

lbl_middle_name = tk.Label(master=vtrs_form, text="Middle Name:")
ent_middle_name = tk.Entry(master=vtrs_form, width=50)
lbl_middle_name.grid(row=2, column=0, sticky="ew")
ent_middle_name.grid(row=2, column=1,sticky="ew",padx=10,pady=5)

lbl_last_name = tk.Label(master=vtrs_form, text="Last Name:")
ent_last_name = tk.Entry(master=vtrs_form, width=50)
lbl_last_name.grid(row=3, column=0, sticky="ew")
ent_last_name.grid(row=3, column=1,sticky="ew",padx=10,pady=5)

#Function to validate Date of Birth entry
def DOB(ent,P):
    if len(P)<=10:
        if ent.isdigit():
            return True
        elif ent == " ":
            return True
        elif "/" in ent:
            return True

        else:
            return False
    else:
        return False
db=window.register(DOB)

lbl_DOB = tk.Label(master=vtrs_form, text="Date of Birth:\n (dd/mm/yyyy)")
ent_DOB = tk.Entry(master=vtrs_form, width=50)
lbl_DOB.grid(row=4, column=0, sticky="ew")
ent_DOB.grid(row=4, column=1,sticky="ew",padx=10,pady=5)
ent_DOB.config(validate="key",validatecommand=(db,"%S","%P"))

lbl_gender = tk.Label(master=vtrs_form, text="Gender:")
lbl_gender.grid(row=5, column=0, sticky="ew")

Options = [
    "Male",
    "Female"
    ]
gnd = tk.StringVar()
gnd.set("Please select your Gender")

opt = tk.OptionMenu(vtrs_form, gnd, *Options)
opt.grid(row=5,column=1,sticky="ew",padx=10,pady=5)

#Function to validate Nationality entry
def cntry(cnt,P):
    if len(P)<=56:
        if cnt.isalpha():
            return True

        elif "-" in cnt:
            return True
        elif cnt == "":
            return True
        else:
            return False
    else:
        return False
ct=window.register(cntry)
lbl_country = tk.Label(master=vtrs_form, text="Nationality:")
ent_country = tk.Entry(master=vtrs_form, width=50)
lbl_country.grid(row=6, column=0, sticky="ew")
ent_country.grid(row=6, column=1,sticky="ew",padx=10,pady=5)
ent_country.config(validate="key",validatecommand=(ct,"%S","%P"))

OptionList = [
"Ahafo",
"Ashanti",
"Brong Ahafo",
"Bono East",
"Central",
"Eastern",
"Greater Accra",
"North East",
"Northern",
"Oti",
"Savannah",
"Upper East",
"Upper West",
"Volta",
"Western",
"Western North"
]

lbl_region = tk.Label(master=vtrs_form, text="Region:")
lbl_region.grid(row=8, column=0, sticky="new")

var = tk.StringVar()

opt = ttk.Combobox(vtrs_form,textvariable = var)
opt['values'] =(
"Ahafo",
"Ashanti",
"Brong Ahafo",
"Bono East",
"Central",
"Eastern",
"Greater Accra",
"North East",
"Northern",
"Oti",
"Savannah",
"Upper East",
"Upper West",
"Volta",
"Western",
"Western North"
)

opt.set("Please Select your region")

opt.grid(row=8,column=1,sticky="new",padx=10,pady=5)

##Buttons
frm_buttons = tk.Frame(master=window)
frm_buttons.pack(fill=tk.BOTH,expand=False)

btn_submit = tk.Button(master=frm_buttons, text="Submit",command=lambda:[update_list(),clear_entries()])
btn_submit.pack(side=tk.RIGHT, padx=10,pady=10, ipadx=10)

btn_clear = tk.Button(master=frm_buttons, text="Clear", command=clear_entries)
btn_clear.pack(side=tk.RIGHT,padx=10,pady=10, ipadx=10)

btn_dup = tk.Button(master=frm_buttons, text="Open List", command=openlist)
btn_dup.pack(side=tk.RIGHT,padx=10,pady=10, ipadx=10)

btn_logout = tk.Button(master=frm_buttons, text="Logout", command=log_out)
btn_logout.pack(side=tk.LEFT,padx=10,pady=10, ipadx=10)

# Start the form

window.mainloop()


import os
import random
import pickle
from icrawler.builtin import BingImageCrawler

def download_images(query , num_images , download_dir):
    bing_crawler = BingImageCrawler(downloader_threads=4 , storage={'root_dir': download_dir})
    bing_crawler.crawl(keyword=query , filters=None , offset=0 , max_num=num_images)

if __name__ == "__main__":
    search_queries = ["dog" , "car" , "food" , "buildings" , "mountains", "cat" , "animal" , "laptop" , "planets" , "sun" , "water" , "sky"]
    download_dir = "./data/neg_samples/"
    for query in search_queries:
        curr_download_dir = os.path.join(download_dir , query)
        if os.path.exists(curr_download_dir):
            os.makedirs(curr_download_dir)

        download_images(query , 500 , curr_download_dir)

    image_names = []
    for folder_name in os.listdir(download_dir):
        folder_path = os.path.join(download_dir , folder_name)
        for img_name in os.listdir(folder_path):
            img_path = os.path.join(folder_path , img_name)
            image_names.append(img_path)

    random.shuffle(image_names)
    train_image_names = random.choices(image_names , k = 2500)
    test_image_names = random.choices(image_names , k = 1000)

    for i in range(len(train_image_names)):
        print(i , train_image_names[i])

    print("****************************************************")
    for i in range(len(test_image_names)):
        print(i , test_image_names[i])

    with open("./data/neg_samples.pickle" , "wb") as content:
        pickle.dump(train_image_names , content , protocol = pickle.HIGHEST_PROTOCOL)

    with open("./data/neg_samples_test.pickle" , "wb") as content:
        pickle.dump(test_image_names , content , protocol = pickle.HIGHEST_PROTOCOL)

    print("Neg_samples pickle file is generated.")


import logging

from django.core.exceptions import PermissionDenied

from wagtail.log_actions import log
from wagtail.signals import page_unpublished

logger = logging.getLogger("wagtail")

class UnpublishPagePermissionError(PermissionDenied):
    """
    Raised when the page unpublish cannot be performed due to insufficient permissions.
    """

    pass

class UnpublishPageAction:
    def __init__(
        self,
        page,
        set_expired=False,
        commit=True,
        user=None,
        log_action=True,
        include_descendants=False,
    ):
        self.page = page
        self.set_expired = set_expired
        self.commit = commit
        self.user = user
        self.log_action = log_action
        self.include_descendants = include_descendants

    def check(self, skip_permission_checks=False):
        if (
            self.user
            and not skip_permission_checks
            and not self.page.permissions_for_user(self.user).can_unpublish()
        ):
            raise UnpublishPagePermissionError(
                "You do not have permission to unpublish this page"
            )

    def _unpublish_page(self, page, set_expired, commit, user, log_action):
        """
        Unpublish the page by setting ``live`` to ``False``. Does nothing if ``live`` is already ``False``
        :param log_action: flag for logging the action. Pass False to skip logging. Can be passed an action string.
            Defaults to 'wagtail.unpublish'
        """
        if page.live:
            page.live = False
            page.has_unpublished_changes = True
            page.live_revision = None

            if set_expired:
                page.expired = True

            if commit:
                # using clean=False to bypass validation
                page.save(clean=False)

            page_unpublished.send(sender=page.specific_class, instance=page.specific)

            if log_action:
                log(
                    instance=page,
                    action=log_action
                    if isinstance(log_action, str)
                    else "wagtail.unpublish",
                    user=user,
                )

            logger.info('Page unpublished: "%s" id=%d', page.title, page.id)

            page.revisions.update(approved_go_live_at=None)

            # Unpublish aliases
            for alias in page.aliases.all():
                alias.unpublish()

    def execute(self, skip_permission_checks=False):
        self.check(skip_permission_checks=skip_permission_checks)

        self._unpublish_page(
            self.page,
            set_expired=self.set_expired,
            commit=self.commit,
            user=self.user,
            log_action=self.log_action,
        )

        if self.include_descendants:
            from wagtail.models import UserPagePermissionsProxy

            user_perms = UserPagePermissionsProxy(self.user)
            for live_descendant_page in (
                self.page.get_descendants().live().defer_streamfields().specific()
            ):
                action = UnpublishPageAction(live_descendant_page)
                if user_perms.for_page(live_descendant_page).can_unpublish():
                    action.execute(skip_permission_checks=True)


# -*- coding: utf-8 -*-
"""
Created on Thu Feb 23 20:25:16 2017

@author: memo

demonstrates inference with frozen graph def
same as sample.py, but:
- instead of loading model + checkpoint, loads frozen graph
- instead of calling model.sample() function, uses own sample() function with named ops
"""

from __future__ import print_function
import numpy as np
import tensorflow as tf
from tensorflow.python.platform import gfile

import argparse
import os
from six.moves import cPickle
from six import text_type

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-n', type=int, default=500,
                       help='number of characters to sample')
    parser.add_argument('--prime', type=text_type, default=u' ',
                       help='prime text')
    parser.add_argument('--sample', type=int, default=1,
                       help='0 to use max at each timestep, 1 to sample at each timestep, 2 to sample on spaces')
    parser.add_argument('--model_dir', type=str, default='./save',
                       help='path to frozen graph pb file')

    args = parser.parse_args()

    with open(os.path.join(args.model_dir, 'chars_vocab.pkl'), 'rb') as f:
        chars, vocab = cPickle.load(f)

    with tf.Session() as sess:
        # load frozen graph
        with gfile.FastGFile(os.path.join(args.model_dir, 'graph_frz.pb'),'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            sess.graph.as_default()
            tf.import_graph_def(graph_def, name='')

        # don't  call model.sample(), instead call sample() function defined below
        print(sample(sess, chars, vocab, args.n, args.prime, args.sample))

def sample(sess, chars, vocab, num=200, prime='The ', sampling_type=1):
    data_in = 'data_in:0'
    data_out = 'data_out:0'
    state_in = 'state_in:0'
    state_out = 'state_out:0'

    state = sess.run(state_in)
    for char in prime[:-1]:
        x = np.zeros((1, 1))
        x[0, 0] = vocab[char]
        feed = {data_in: x, state_in:state}
        [state] = sess.run([state_out], feed)

    def weighted_pick(weights):
        t = np.cumsum(weights)
        s = np.sum(weights)
        return(int(np.searchsorted(t, np.random.rand(1)*s)))

    ret = prime
    char = prime[-1]
    for n in range(num):
        x = np.zeros((1, 1))
        x[0, 0] = vocab[char]
        feed = {data_in: x, state_in:state}
        [probs, state] = sess.run([data_out, state_out], feed)
        p = probs[0]

        if sampling_type == 0:
            sample = np.argmax(p)
        elif sampling_type == 2:
            if char == ' ':
                sample = weighted_pick(p)
            else:
                sample = np.argmax(p)
        else: # sampling_type == 1 default:
            sample = weighted_pick(p)

        pred = chars[sample]
        ret += pred
        char = pred
    return ret

if __name__ == '__main__':
    main()


from django.conf.urls import patterns, include, url
from django.contrib import admin

from django_consultants import views

admin.autodiscover()

urlpatterns = patterns('',

    url(r'^admin/', include(admin.site.urls)),

    # The main page.
    url(r'^$', views.main_page),

    # Login/logout.
    url(r'^login/$', 'django.contrib.auth.views.login'),
    url(r'^logout/$', views.logout_page),

    # Web portal.
    url(r'^portal/', include('portal.urls')),

    # Serve static content.
    url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': 'static'}),
)


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

import json
import os

from absl import logging
import tensorflow as tf

_SUMMARY_TXT = 'training_summary.txt'
_MIN_SUMMARY_STEPS = 10

def _save_checkpoint(checkpoint, model_dir, checkpoint_prefix):
    """Saves model to with provided checkpoint prefix."""

    checkpoint_path = os.path.join(model_dir, checkpoint_prefix)
    saved_path = checkpoint.save(checkpoint_path)
    logging.info('Saving model as TF checkpoint: %s', saved_path)
    return

def _get_input_iterator(input_fn, strategy):
    input_data = input_fn()
    if callable(input_data):
        iterator = iter(
            strategy.experimental_distribute_datasets_from_function(input_data))
    else:
        iterator = iter(strategy.experimental_distribute_dataset(input_data))
    return iterator

def _float_metric_value(metric):
    """Gets the value of a float-value keras metric."""
    return metric.result().numpy().astype(float)

def _steps_to_run(current_step, steps_per_epoch, steps_per_loop):
    """Calculates steps to run on device."""
    if steps_per_loop <= 0:
        raise ValueError('steps_per_loop should be positive integer.')
    if steps_per_loop == 1:
        return steps_per_loop
    remainder_in_epoch = current_step % steps_per_epoch
    if remainder_in_epoch != 0:
        return min(steps_per_epoch - remainder_in_epoch, steps_per_loop)
    else:
        return steps_per_loop

def _write_txt_summary(training_summary, model_dir):
    """Writes a summary text file to record stats."""
    summary_path = os.path.join(model_dir, _SUMMARY_TXT)
    with tf.io.gfile.GFile(summary_path, 'wb') as f:
        logging.info('Training Summary: \n%s', str(training_summary))
        f.write(json.dumps(training_summary, indent=4))

def run_customized_training_loop(
        strategy=None,
        model_fn=None,
        loss_fn=None,
        model_dir=None,
        train_input_fn=None,
        steps_per_epoch=None,
        steps_per_loop=1,
        epochs=1,
        eval_input_fn=None,
        eval_steps=None,
        init_checkpoint=None,
        custom_callbacks=None):

    total_training_steps = steps_per_epoch * epochs

    # To reduce unnecessary send/receive input pipeline operation, we place input
    # pipeline ops in worker task.
    train_iterator = _get_input_iterator(train_input_fn, strategy)
    eval_iterator = _get_input_iterator(eval_input_fn, strategy)

    with strategy.scope():
        # To correctly place the model weights on accelerators,
        # model and optimizer should be created in scope.
        model = model_fn()
        if not hasattr(model, 'optimizer'):
            raise ValueError('User should set optimizer attribute to model '
                             'inside `model_fn`.')
        optimizer = model.optimizer
        # use_float16 = isinstance(
        #     optimizer, tf.keras.mixed_precision.experimental.LossScaleOptimizer)

        if init_checkpoint:
            logging.info(
                'Checkpoint file %s found and restoring from '
                'initial checkpoint for core model.', init_checkpoint)
            checkpoint = tf.train.Checkpoint(model=model)
            checkpoint.restore(init_checkpoint).assert_consumed()
            logging.info('Loading from checkpoint file completed')

        train_loss_metric = tf.keras.metrics.Mean(
            'training_loss', dtype=tf.float32)
        eval_loss_metric = tf.keras.metrics.Mean(name='eval_loss')
        train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
            name='train_accuracy')
        eval_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
            name='eval_accuracy')
        # Create summary writers
        eval_summary_writer = tf.summary.create_file_writer(
            os.path.join(model_dir, 'summaries/eval'))
        if steps_per_loop >= _MIN_SUMMARY_STEPS:
            # Only writes summary when the stats are collected sufficiently over
            # enough steps.
            train_summary_writer = tf.summary.create_file_writer(
                os.path.join(model_dir, 'summaries/train'))
        else:
            train_summary_writer = None

        def train_step(inputs):
            """Replicated training step."""

            input_ids, labels = inputs[0]['input_ids'], inputs[1]
            with tf.GradientTape() as tape:
                predictions = model(input_ids, training=True)
                loss = loss_fn(labels, predictions)
                # if use_float16:
                #     scaled_loss = optimizer.get_scaled_loss(loss)

            # if use_float16:
            #     scaled_grads = tape.gradient(scaled_loss, training_vars)
            #     grads = optimizer.get_unscaled_gradients(scaled_grads)
            # else:
            #     grads = tape.gradient(loss, training_vars)
            gradients = tape.gradient(loss, model.trainable_variables)
            optimizer.apply_gradients(zip(gradients, model.trainable_variables))
            # For reporting, the metric takes the mean of losses.
            train_loss_metric.update_state(loss)
            train_accuracy.update_state(labels, predictions)
            return loss

        def eval_step(inputs):
            input_ids, labels = inputs[0]['input_ids'], inputs[1]

            predictions = model(input_ids, training=False)
            e_loss = loss_fn(labels, predictions)

            eval_loss_metric.update_state(e_loss)
            eval_accuracy.update_state(labels, predictions)

        @tf.function
        def distributed_train_step(dataset_inputs):
            per_replica_losses = strategy.experimental_run_v2(train_step,
                                                              args=(dataset_inputs,))
            return strategy.reduce(tf.distribute.ReduceOp.SUM, per_replica_losses,
                                   axis=None)

        @tf.function
        def distributed_eval_step(dataset_inputs):
            return strategy.experimental_run_v2(eval_step, args=(dataset_inputs,))

        def _run_evaluation(current_training_step, iterator):
            """Runs validation steps and aggregate metrics."""
            distributed_eval_step(next(iterator))
            eval_template = "Eval Step: %d   Test Loss: %f, Test Accuracy: %f" \
                            % (eval_steps,
                               _float_metric_value(eval_loss_metric),
                               _float_metric_value(eval_accuracy) * 100)
            logging.info(eval_template)
            with eval_summary_writer.as_default():
                tf.summary.scalar(
                    eval_loss_metric.name, _float_metric_value(eval_loss_metric), step=current_training_step)
                tf.summary.scalar(
                    eval_accuracy.name, _float_metric_value(eval_accuracy), step=current_training_step)

                eval_summary_writer.flush()

        def _run_callbacks_on_batch_begin(batch):
            """Runs custom callbacks at the start of every step."""
            if not custom_callbacks:
                return
            for callback in custom_callbacks:
                callback.on_batch_begin(batch)

        def _run_callbacks_on_batch_end(batch):
            """Runs custom callbacks at the end of every step."""
            if not custom_callbacks:
                return
            for callback in custom_callbacks:
                callback.on_batch_end(batch)

        # Training loop starts here.
        checkpoint = tf.train.Checkpoint(model=model, optimizer=optimizer)
        latest_checkpoint_file = tf.train.latest_checkpoint(model_dir)
        if latest_checkpoint_file:
            logging.info(
                'Checkpoint file %s found and restoring from '
                'checkpoint', latest_checkpoint_file)
            checkpoint.restore(latest_checkpoint_file)
            logging.info('Loading from checkpoint file completed')

        current_step = optimizer.iterations.numpy()
        checkpoint_name = 'ctl_step_{step}.ckpt'

        while current_step < total_training_steps:
            # Training loss/metric are taking average over steps inside micro
            # training loop. We reset the their values before each round.
            train_loss_metric.reset_states()
            eval_loss_metric.reset_states()
            train_accuracy.reset_states()
            eval_accuracy.reset_states()
            for metric in model.metrics:
                metric.reset_states()

            _run_callbacks_on_batch_begin(current_step)
            # Runs several steps in the host while loop.
            steps = _steps_to_run(current_step, steps_per_epoch, steps_per_loop)
            total_loss = 0.0
            for i in range(steps):
                total_loss += distributed_train_step(next(train_iterator))
            train_loss = total_loss / steps

            _run_callbacks_on_batch_end(current_step)
            current_step += steps

            # train_loss = _float_metric_value(train_loss_metric)
            # Updates training logging.
            template = "Epoch %d, Train Step: %d/%d  Loss: %f, Accuracy: %f" % (
                current_step / steps_per_epoch, current_step, total_training_steps,
                train_loss, _float_metric_value(train_accuracy) * 100)

            if train_summary_writer:
                with train_summary_writer.as_default():
                    tf.summary.scalar(
                        train_loss_metric.name, train_loss, step=current_step)
                    tf.summary.scalar(
                        train_accuracy.name, _float_metric_value(train_accuracy), step=current_step)
                    train_summary_writer.flush()
            logging.info(template)

            # Saves model checkpoints and run validation steps at every epoch end.
            if current_step % steps_per_epoch == 0:
                # To avoid repeated model saving, we do not save after the last
                # step of training.
                if current_step < total_training_steps:
                    _save_checkpoint(checkpoint, model_dir,
                                     checkpoint_name.format(step=current_step))

                if eval_input_fn:
                    logging.info('Running evaluation after step: %s.', current_step)
                    _run_evaluation(current_step, eval_iterator)
                    # Re-initialize evaluation metric.
                    for metric in model.metrics:
                        metric.reset_states()

        _save_checkpoint(checkpoint, model_dir,
                         checkpoint_name.format(step=current_step))

        if eval_input_fn:
            logging.info('Running final evaluation after training is complete.')
            _run_evaluation(current_step, eval_iterator)

        training_summary = {
            'total_training_steps': total_training_steps,
            'train_loss': _float_metric_value(train_loss_metric),
        }

        _write_txt_summary(training_summary, model_dir)

        return model


from db.igdb_connector import IGDBConnector
import mysql.connector.errors as errors
import requests
import random
from faker import Faker

__author__ = 'jingyu'

def grab_recipe_data(conn):
    with open("db/sample_categories.txt") as categories:
        for line in categories:
            stripped = line.strip()
            # Check for empty line
            if stripped:
                grab_recipe_data_for(stripped, conn, 100)

def grab_recipe_data_for(query, conn, how_many):
    print("Getting data for", query)
    current = 0

    while current < how_many:
        to = grab_recipe_data_from(query, conn, current)

        if to:
            current = to
        else:
            break

def grab_recipe_data_from(query, conn, from_):
    cur = conn.cursor()

    r = requests.get("https://api.edamam.com/search", params={"q": query, "from": from_})
    print(r.url)
    data = r.json()

    for hit in data["hits"]:
        try:
            insert_recipe(hit["recipe"], cur)
            # only commit insertions if the entire transaction goes through
            conn.commit()
        except errors.IntegrityError as e:
            print(e)
            pass

    return data["to"] if data["more"] else None

def insert_recipe(recipe, cur):
    # insert the recipe
    recipe_data = recipe["uri"], recipe["label"], recipe["image"], recipe["source"], \
                  recipe["url"], recipe["yield"], recipe["calories"], recipe["totalWeight"]

    cur.execute(IGDBConnector.recipe_insert, recipe_data)

    # if rowcount is zero then we didn't insert a new recipe
    if cur.rowcount == 0:
        return

    # http://stackoverflow.com/questions/2548493/how-do-i-get-the-id-after-insert-into-mysql-database-with-python
    recipe_id = cur.lastrowid

    # insert the ingredients
    for ingredient in recipe["ingredients"]:
        insert_ingredients(ingredient, recipe_id, cur)

    insert_nutrients(recipe, recipe_id, cur)
    insert_labels(recipe, recipe_id, cur)

def insert_ingredients(ingredient, recipe_id, cur):
    # need to insert food first
    cur.execute(IGDBConnector.food_insert, (ingredient["food"],))

    ingredient = recipe_id, ingredient["text"], ingredient["food"], \
                 ingredient["measure"], ingredient["quantity"], ingredient["weight"]

    cur.execute(IGDBConnector.ingredient_insert, ingredient)

def insert_nutrients(recipe, recipe_id, cur):
    total_nutrients = [(recipe_id, "total", value["label"], value["quantity"], value["unit"])
                       for key, value in recipe["totalNutrients"].items()]

    daily_nutrients = [(recipe_id, "daily", value["label"], value["quantity"], value["unit"])
                       for key, value in recipe["totalDaily"].items()]

    cur.executemany(IGDBConnector.nutrient_insert, total_nutrients + daily_nutrients)

def insert_labels(recipe, recipe_id, cur):
    diet_labels = [(recipe_id, label) for label in recipe["dietLabels"]]

    cur.executemany(IGDBConnector.dietlabel_insert, diet_labels)

    health_labels = [(recipe_id, label) for label in recipe["healthLabels"]]

    cur.executemany(IGDBConnector.healthlabel_insert, health_labels)

def generate_fake_grocery_data(conn):
    """
    Generates fake grocery data from *some* foods.
    Not generating grocery data for every food is intentional
    to test how our database/app handles missing grocery data.
    :return:
    """

    print("Generating fake grocery data...")

    # setups
    random.seed()
    fake = Faker()

    cur = conn.cursor()

    # erase all previously generated grocery data first
    cur.execute("SET FOREIGN_KEY_CHECKS = 0")
    cur.execute("TRUNCATE TABLE groceries")
    cur.execute("SET FOREIGN_KEY_CHECKS = 1")

    cur.execute("SELECT * FROM foods")
    for row in cur.fetchall():
        food = row[0]

        # 30% chance to skip generation
        if random.random() < 0.3:
            continue

        # Generates 1 - 10 groceries for this food with random names and
        # prices between 1 to 6
        groceries = [(fake.name(), food, random.random() * 5 + 1, "Target")
                     for i in range(0, int(random.random() * 9) + 1)]

        cur.executemany(IGDBConnector.grocery_insert, groceries)

    conn.commit()

def generate_recipe_prices(conn):
    cur = conn.cursor()
    cur.callproc('generate_recipe_prices')
    conn.commit()

def import_sample_recipe_and_grocery():
    """
    Made into a function to avoid polluting global scope
    :return:
    """
    connection = IGDBConnector.connect()
    grab_recipe_data(connection)
    generate_fake_grocery_data(connection)
    generate_recipe_prices(connection)
    connection.close()

if __name__ == "__main__":
    import_sample_recipe_and_grocery()


# -*- coding=UTF-8 -*-
# pyright: strict, reportTypeCommentUsage=none

from __future__ import absolute_import, division, print_function, unicode_literals

TYPE_CHECKING = False
if TYPE_CHECKING:
    from typing import Text
    from .._types.aov_spec import AOVLayerCreationMethod, AOVLayerCreationOperation

class AOVLayerCreationMethodImpl(object):
    def __init__(
        self,
        _operation,  # type: AOVLayerCreationOperation
        _inputs,  # type: tuple[Text, ...]
    ):
        # type: (...) -> None
        self.operation = _operation # type: AOVLayerCreationOperation
        self.inputs = _inputs

def _(v):
    # type: (AOVLayerCreationMethodImpl) ->AOVLayerCreationMethod
    return v


# -*- coding: utf-8 -*-
"""
Created on Tue Feb 28 14:15:41 2017

@author: michal

This script combines all the data files from each run of Pythia. It is important
to rename the runs as simply n with no file extension, with n being intergers from 1
to N , the total number of files to combine.
"""
import numpy as np

#lcmbr listcombiner opens a file, reads each line into a list and appends it to oldlist
def lcmbr(filename,oldlist):
    file = open(str(filename),"r")
    dataset=file.readlines()
    file.close()
    dataset=dataset[143:]
    return (oldlist+dataset)

#initializing variables
data =[]
data1=[]
n    =20

#for loop to add all the files to the data list
while n < 31:
    data=lcmbr(n,data)
    n=n+1

#This removes any rows that are not data but artifacts of pythia
for i in data:
    j=np.fromstring(i,sep="\t")
    if len(j) ==21:
        data1.append(j)

#converting into a numpy array and saving
data2=np.array(data1)
np.savetxt("datasets.csv",data2,delimiter=",",fmt="%1.3f")
"""
IMPORTANT

The data is saved in a np.array of dimensions 21xN where N is the amount of data
the columns represent the following
eta pT phi px py pz E
that makes 7 variables for b, bbar and the parent g in order.
"""


# import ctypes
import logging
import math

import torch
import torch.distributed as dist
from torch.nn.utils import clip_grad_norm_

from mlbench_core.utils.pytorch.distributed import (
    AllReduceAggregation,
    AllReduceAggregationHVD,
)

try:
    from apex.optimizers import FusedAdam
    from apex import amp
except ImportError as e:
    pass

logger = logging.getLogger("mlbench")

class FP16Optimizer:
    """
    Mixed precision optimizer with dynamic loss scaling and backoff.
    https://docs.nvidia.com/deeplearning/sdk/mixed-precision-training/index.html#scalefactor

    Args:
        fp16_model (`obj`:torch.nn.Module): model (previously casted to half)
        world_size (int): Distributed world size
        use_cuda (bool): Use cuda tensors for aggregation
        use_horovod (bool): Use Horovod for aggregation
        by_layer (bool): Aggregate by layer
        grad_clip (float): coefficient for gradient clipping, max L2 norm of the gradients
        loss_scale (int):  initial loss scale
        dls_downscale (int): loss downscale factor, loss scale is divided by this factor when NaN/INF occurs in the gradients
        dls_upscale (int): loss upscale factor, loss scale is multiplied by this factor if previous dls_upscale_interval batches finished successfully
        dls_upscale_interval (int): interval for loss scale upscaling
        average_models (bool): Average the models
    """

    def __init__(
        self,
        fp16_model,
        world_size,
        use_cuda=False,
        use_horovod=False,
        by_layer=False,
        grad_clip=float("inf"),
        loss_scale=1024,
        dls_downscale=2,
        dls_upscale=2,
        dls_upscale_interval=128,
        average_models=True,
    ):
        self.use_cuda = use_cuda

        self.fp16_model = fp16_model
        self.fp16_params, self.fp32_params = self.initialize_flat_fp32_weight()
        self.since_last_invalid = 0
        self.loss_scale = loss_scale
        self.dls_downscale = dls_downscale
        self.dls_upscale = dls_upscale
        self.dls_upscale_interval = dls_upscale_interval
        self.grad_clip = grad_clip
        self.world_size = dist.get_world_size()

        self.optimizer = None

        if use_horovod:
            self.agg = AllReduceAggregationHVD(
                world_size=world_size, use_cuda=use_cuda
            ).agg_grad(by_layer=by_layer)
        else:
            self.agg = AllReduceAggregation(
                world_size=world_size, use_cuda=use_cuda
            ).agg_grad(by_layer=by_layer)

        if average_models:
            self.agg_mode = "avg"
        else:
            raise NotImplementedError("Only average model is supported right now.")

    def set_optimizer(self, optimizer):
        self.optimizer = optimizer

    # Flattening master weight
    def initialize_flat_fp32_weight(self):
        """ Initializes the model's parameters in fp32 and fp16

        Returns:
            (torch.Tensor, torch.Tensor): The Parametrs in fp16 and fp32
        """
        # Set all gradients to None
        for p in self.fp16_model.parameters():
            p.grad = None

        # Count number of parameters per layer
        nelem = 0
        for p in self.fp16_model.parameters():
            nelem += p.numel()
        fp32_params = torch.empty(
            nelem,
            dtype=torch.float32,
            device=torch.device("cuda" if self.use_cuda else "cpu"),
        )
        fp16_params = torch.empty(
            nelem,
            dtype=torch.float16,
            device=torch.device("cuda" if self.use_cuda else "cpu"),
        )

        pointer = 0
        for p in self.fp16_model.parameters():
            nelem = p.numel()
            fp32_params[pointer : pointer + nelem].copy_(p.data.view(-1))
            fp16_params[pointer : pointer + nelem].copy_(p.data.view(-1))
            pointer += nelem

        fp32_params = torch.nn.Parameter(fp32_params, requires_grad=True)
        fp32_params.grad = torch.autograd.Variable(
            fp32_params.data.new(*fp32_params.size())
        )

        fp16_params = torch.nn.Parameter(fp16_params, requires_grad=True)
        fp16_params.grad = torch.autograd.Variable(
            fp16_params.data.new(*fp16_params.size())
        )

        return fp16_params, fp32_params

    @staticmethod
    def fp16_to_fp32_flat_grad(fp32_params, fp16_model):
        """ Copies the parameters in `fp16_model` into `fp32_params` in-place

        Args:
            fp32_params (torch.Tensor): Parameters in fp32
            fp16_model (torch.nn.Module): Model in fp16

        """
        pointer = 0
        for p in fp16_model.parameters():
            nelem = p.numel()
            fp32_params.grad.data[pointer : pointer + nelem].copy_(p.grad.data.view(-1))
            pointer += nelem

    @staticmethod
    def fp32_to_fp16_grads(fp16_model, fp32_params):
        """ Copies the parameters in `fp32_params` into `fp16_model` in-place

         Args:
             fp16_model (torch.nn.Module): Model in fp16
             fp32_params (torch.Tensor): Parameters in fp32

         """
        pointer = 0
        for p in fp16_model.parameters():
            nelem = p.numel()
            p.data.view(-1).copy_(fp32_params.data[pointer : pointer + nelem])
            pointer += nelem

    def backward_loss(self, loss):
        """ Scales and performs backward on the given loss

        Args:
            loss (torch.nn.Module): The loss

        """
        loss *= self.loss_scale
        loss.backward()

    def step(self, closure=None):
        """
        Performs one step of the optimizer.
        Applies loss scaling, computes gradients in fp16, converts gradients to
        fp32, inverts scaling and applies optional gradient norm clipping.
        If gradients are finite, it applies update to fp32 master weights and
        copies updated parameters to fp16 model for the next iteration. If
        gradients are not finite, it skips the batch and adjusts scaling factor
        for the next iteration.

        Args:
            closure (callable, optional): A closure that reevaluates the model and returns the loss.
        """

        scaling_factor = self.loss_scale

        # Aggregate gradients
        self.agg(self.fp16_model, self.agg_mode)
        # Cast fp16 params to fp32 for optimizer
        self.fp16_to_fp32_flat_grad(self.fp32_params, self.fp16_model)

        if scaling_factor != 1.0:
            self.fp32_params.grad.data /= scaling_factor
        norm = clip_grad_norm_([self.fp32_params], self.grad_clip)

        updated = False
        if math.isfinite(norm):
            self.optimizer.step(closure=closure)
            self.fp32_to_fp16_grads(self.fp16_model, self.fp32_params)
            self.since_last_invalid += 1
            updated = True
        else:
            self.loss_scale /= self.dls_downscale
            self.since_last_invalid = 0
            logger.info(f"Skipped batch, new scale: {self.loss_scale}")

        if self.since_last_invalid >= self.dls_upscale_interval:
            self.loss_scale *= self.dls_upscale
            self.loss_scale = min(self.loss_scale, 8192.0)
            self.since_last_invalid = 0

        for p in self.fp16_model.parameters():
            p.grad = None

        return updated

    def zero_grad(self):
        self.optimizer.zero_grad()

class FP32Optimizer:
    """
    Standard optimizer, computes backward and applies weight update.

    Args:
        model (`obj`:torch.nn.Module): model
        world_size (int): Distributed world size
        use_cuda (bool): Use cuda tensors for aggregation
        by_layer (bool): Aggregate by layer
        grad_clip (float): coefficient for gradient clipping, max L2 norm of the gradients
        average_models (bool): Average the models
    """

    def __init__(
        self,
        model,
        world_size,
        use_cuda=False,
        by_layer=False,
        grad_clip=None,
        average_models=True,
    ):
        self.model = model
        self.grad_clip = grad_clip
        self.optimizer = None
        self.agg = AllReduceAggregation(
            world_size=world_size, use_cuda=use_cuda
        ).agg_grad(by_layer=by_layer)
        if average_models:
            self.agg_mode = "avg"
        else:
            raise NotImplementedError("Only average model is supported right now.")

    def set_optimizer(self, optimizer):
        self.optimizer = optimizer

    def step(self, closure=None):
        """
        Performs one step of the optimizer.
        """
        if self.grad_clip != float("inf"):
            clip_grad_norm_(self.model.parameters(), self.grad_clip)

        self.agg(self.model, self.agg_mode)
        self.optimizer.step(closure=closure)
        return True

    def backward_loss(self, loss):
        loss.backward()

    def zero_grad(self):
        self.optimizer.zero_grad()

class AMPOptimizer:
    """
    Optimizer compatible with AMP.
    Uses AMP to apply loss scaling, computes backward and applies weight
    update.

    Args:
        model (`obj`:torch.nn.Module): model
        grad_clip (float): coefficient for gradient clipping, max L2 norm of the gradients
        loss_scale (int):  initial loss scale
        dls_upscale_interval (int): interval for loss scale upscaling
        average_models (bool): Average the models
        world_size (int): Distributed world size
        use_cuda (bool): Use cuda tensors for aggregation
        by_layer (bool): Aggregate by layer
        use_horovod (bool): Use Horovod for aggregation
    """

    def __init__(
        self,
        model,
        grad_clip=None,
        loss_scale=8192,
        dls_upscale_interval=128,
        average_models=True,
        world_size=1,
        use_cuda=False,
        by_layer=False,
        use_horovod=False,
    ):
        self.model = model
        self.grad_clip = grad_clip
        self.optimizer = None
        loss_scaler = amp._amp_state.loss_scalers[0]
        loss_scaler._loss_scale = loss_scale
        loss_scaler._scale_seq_len = dls_upscale_interval

        if average_models:
            self.agg_mode = "avg"
        else:
            raise NotImplementedError("Only average model is supported right now.")

        if use_horovod:
            self.agg = AllReduceAggregationHVD(
                world_size=world_size, use_cuda=use_cuda
            ).agg_grad(by_layer=by_layer)
        else:
            self.agg = AllReduceAggregation(
                world_size=world_size, use_cuda=use_cuda
            ).agg_grad(by_layer=by_layer)

    def set_optimizer(self, optimizer):
        self.optimizer = optimizer

    def backward_loss(self, loss):
        with amp.scale_loss(loss, self.optimizer) as scaled_loss:
            scaled_loss.backward()

    def step(self, closure=None):
        """
        Performs one step of the optimizer.
        """
        if self.grad_clip != float("inf"):
            clip_grad_norm_(amp.master_params(self.optimizer), self.grad_clip)

        self.agg(self.model, self.agg_mode)
        self.optimizer.step(closure=closure)
        return True

    def zero_grad(self):
        self.optimizer.zero_grad()


#lista = [5,4,6,8]

#matriz = [lista,lista,lista]

#print(matriz)

#for i in matriz:
#    print(max(i))
#    print(min(i))
#    print(sum(i)/len(i))

# print(matriz[0])
# print(matriz[0][0])
# print(matriz[1][0])
# print(matriz[-1][-1])
# matriz[-1][-1] = 5
# print(matriz[-1][-1])
# print(matriz)

matriz = [[5, 7, 6, 9], [3, 4, 2, 8], [7, 9, 6, 1]]

print(len(matriz))
print(len(matriz[0]))

print('--------------------------')
print(matriz)
print('--------------------------')
i = 0
while i < len(matriz):
    j = 0
    while j < len(matriz[0]):
        if matriz[i][j]%2 != 0:
            matriz[i][j] = matriz[i][j]*2
        else:
            matriz[i][j] = matriz[i][j]/2
        j = j + 1
    i = i + 1
print('--------------------------')

print(matriz)
print('usando ciclo for')

for i in range(len(matriz)):
    for j in range(len(matriz[i])):
        if matriz[i][j] <= 4:
            matriz[i][j] = matriz[i][j]*2
        else:
            matriz[i][j] = matriz[i][j]/2

print(matriz)


import sys; sys.stdin = open('1238.txt')

def DFS(v):
    for w in G[v]:
        if D[w] > D[v] + 1:
            D[w] = D[v] + 1
            DFS(w)

INF = 0xffffff
for test_case in range(1, 11):
    N, start = map(int, input().split())
    V, E = 100, N // 2

    G = [[] for i in range(V + 1)]
    arr = list(map(int, input().split()))

    for i in range(0, N, 2):
        G[arr[i]].append(arr[i + 1])  # 유향 그래프

    D = [INF] * (V + 1)
    D[start] = 0
    DFS(start)
    ans = start
    for i in range(1, V + 1):
        if D[i] < INF and D[ans] <= D[i]:
            ans = i

    print("#%d %d" % (test_case, ans))


from rest_framework import serializers
from logistic.models import Logistics
from settlement.serializers import SettlementSerializer
from address.serializers import AddressSerializer

class LogisticsSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(
        view_name='api:v1:logistics-detail',
        lookup_field='uuid'
    )

    class Meta:
        model = Logistics
        fields = (
            'url',
            'postage_type',
            'tracking_number',
            'date_sent'
        )

class LogisticsDetailSerializer(serializers.HyperlinkedModelSerializer):
    settlement = SettlementSerializer(many=True)
    sender_address = AddressSerializer()
    receiver_address = AddressSerializer()

    class Meta:
        model = Logistics
        fields = (
            'settlement',
            'in_stock',
            'ships_in_days',
            'postage_type',
            'tracking_number',
            'is_sent',
            'date_sent',
            'sender_address',
            'receiver_address',
            'is_received',
            'date_received',
        )


import pandas as pd
#gtf file source: ftp://ftp.ebi.ac.uk/pub/databases/gencode/Gencode_mouse/release_M25/gencode.vM25.basic.annotation.gtf.gz
data=pd.read_csv("gencode.vM25.basic.annotation.gtf",header=None,sep='\t')
gene_names=[]
for index,row in data.iterrows():
    cur_gene_name=None
    gene_info=row[8].split(';')
    for entry in gene_info:
        entry=entry.strip()
        if entry.startswith('gene_name'):
            cur_gene_name=entry.split('"')[1]
            break
    gene_names.append(cur_gene_name)
data['gene']=gene_names
data=data[data[2]=="gene"]
plus_strand=data[data[6]=="+"]
minus_strand=data[data[6]=='-']
plus_strand['chrom']=plus_strand[0]
plus_strand['start']=plus_strand[3]
plus_strand['end']=plus_strand[3]+1
minus_strand['chrom']=minus_strand[0]
minus_strand['end']=minus_strand[4]
minus_strand['start']=minus_strand[4]-1
plus_subset=plus_strand[['chrom','start','end','gene']]
minus_subset=minus_strand[['chrom','start','end','gene']]
merged=pd.concat((plus_subset,minus_subset),axis=0)
merged.to_csv("tss.gencode.vM25.basic.annotation.bed",sep='\t',header=False,index=False)


def rno(n):
	if n in range(l,h+1):
		return True
	else:
		return False
l,h,m=int(input('Enter lowest no: ')),int(input('Enter highest no: ')),int(input('Enter no: '))
if (l>h):
	print('Entered lowest number is greater than highest')
elif(rno(m)):
	print(m,'is within range (',l,',',h,')')
else:
	print(m,'is not within range (',l,',',h,')')


import sqlite3
from tkinter import messagebox
try:
    from Tkinter import *
except ImportError:
    from tkinter import *

class BANK_MANAGEMENT:
    def __init__(self):
        def info():
            self.accinfo = str(self.getaccount.get())
            self.amtinfo = str(self.getamount.get())

            if self.accinfo.isdigit() == True and len(self.accinfo) != 0 and self.amtinfo.isdigit() == True and len(self.amtinfo) != 0 and int(self.amtinfo)!=0:

                connt = sqlite3.connect('database.db')
                c = connt.cursor()

                c.execute("SELECT * from customer WHERE account = " + self.accinfo)
                row=c.fetchone()
                if row is None:
                    messagebox.showinfo("SORRY!","Account Doesn't Exist")
                else:
                    c.execute("SELECT * from customer WHERE account = " + self.accinfo)
                    r=c.fetchone()

                    if ( r[7]-int(self.amtinfo) >= 0):

                        c.execute("UPDATE customer SET amount = amount-? WHERE account = ?" ,(self.amtinfo,self.accinfo))
                        connt.commit()
                        connt.close()
                        messagebox.showinfo("SUCCESS!", " Account UPDATED!")
                    else:

                        messagebox.showinfo("SORRY!", "ENTER LESS AMOUNT")

            else:
                messagebox.showinfo("ERROR!", "INVALID ENTRY!")

        root = Tk()
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#d9d9d9' # X11 color: 'gray85'
        font10 = "-family {Segoe UI} -size 17 -weight bold -slant "  \
            "roman -underline 0 -overstrike 0"
        font11 = "-family {Segoe UI} -size 28 -weight bold -slant "  \
            "roman -underline 0 -overstrike 0"
        font9 = "-family {Segoe UI} -size 23 -weight bold -slant roman"  \
            " -underline 0 -overstrike 0"

        root.geometry("1063x749+0+0")
        root.title("WITHDRAW AMOUNT")
        root.configure(background="black")

        self.Frame1 = Frame(root)
        self.Frame1.place(relx=0.02, rely=0.03, relheight=0.87, relwidth=0.94)
        self.Frame1.configure(relief=GROOVE)
        self.Frame1.configure(borderwidth="2")
        self.Frame1.configure(relief=GROOVE)
        self.Frame1.configure(background="yellow")
        self.Frame1.configure(width=825)

        self.Label1 = Label(self.Frame1)
        self.Label1.place(relx=0.12, rely=0.21, height=28, width=377)
        self.Label1.configure(background="yellow")
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(font=font9)
        self.Label1.configure(foreground="blue")
        self.Label1.configure(text='''ENTER THE A/C NO.   ''')

        self.Entry1 = Entry(self.Frame1)
        self.getaccount=StringVar()
        self.Entry1.place(relx=0.65, rely=0.21,height=40, relwidth=0.3)
        self.Entry1.configure(background="white")
        self.Entry1.configure(disabledforeground="#a3a3a3")
        self.Entry1.configure(font="TkFixedFont")
        self.Entry1.configure(foreground="#000000")
        self.Entry1.configure(insertbackground="black")
        self.Entry1.configure(width=84)
        self.Entry1.configure(textvariable=self.getaccount)

        self.Label2 = Label(self.Frame1)
        self.Label2.place(relx=0.12, rely=0.34, height=48, width=377)
        self.Label2.configure(background="yellow")
        self.Label2.configure(disabledforeground="#a3a3a3")
        self.Label2.configure(font=font9)
        self.Label2.configure(foreground="blue")
        self.Label2.configure(text='''ENTER THE AMOUNT   ''')

        self.Entry2 = Entry(self.Frame1)
        self.getamount =StringVar()
        self.Entry2.place(relx=0.65, rely=0.34, height=40, relwidth=0.3)
        self.Entry2.configure(background="white")
        self.Entry2.configure(disabledforeground="#a3a3a3")
        self.Entry2.configure(font="TkFixedFont")
        self.Entry2.configure(foreground="#000000")
        self.Entry2.configure(insertbackground="black")
        self.Entry2.configure(width=84)
        self.Entry2.configure(textvariable=self.getamount)

        self.Button1 = Button(self.Frame1)
        self.Button1.place(relx=0.39, rely=0.51, height=74, width=197)
        self.Button1.configure(activebackground="#d9d9d9")
        self.Button1.configure(activeforeground="#000000")
        self.Button1.configure(background="blue")
        self.Button1.configure(disabledforeground="#a3a3a3")
        self.Button1.configure(font=font10)
        self.Button1.configure(foreground="white")
        self.Button1.configure(highlightbackground="#d9d9d9")
        self.Button1.configure(highlightcolor="black")
        self.Button1.configure(pady="0")
        self.Button1.configure(text='''SUBMIT''')
        self.Button1.configure(width=197)
        self.Button1.configure(command=info)

        self.Message1 = Message(self.Frame1)
        self.Message1.place(relx=0, rely=0.02, relheight=0.12, relwidth=1)
        self.Message1.configure(background="blue")
        self.Message1.configure(font=font11)
        self.Message1.configure(foreground="white")
        self.Message1.configure(highlightbackground="#d9d9d9")
        self.Message1.configure(highlightcolor="black")
        self.Message1.configure(text='''WITHDRAW AMOUNT...!!!''')
        self.Message1.configure(width=460)
        root.mainloop()

if __name__ == '__main__':
    GETINFO=BANK_MANAGEMENT()


import re
import socket
from queue import Queue

import docker
import requests
from docker import errors

import job

class Worker:
    """
    This module handles communication between the job queue and docker
    """

    def __init__(self, config):
        self.client = docker.from_env()
        self.max_concurrent = config['docker']['max_concurrent_containers']
        self.hub = None
        self.jobs = []
        self.container_options = {'links': {'anna-worker_selenium_hub': 'hub'}, 'shm_size': '2G', 'detach': True}
        self.last_job_request = 0
        self.queue = Queue()
        self.config = config

    def __del__(self):
        self.prune()
        self.client.close()

    def update(self):
        self.keep_hub_alive()  # Make sure the hub is running
        self.update_jobs()  # Retrieve logs & handle containers
        self.prune()

    def keep_hub_alive(self):
        """
        Makes sure the selenium hub is running & removes any stopped hub containers
        :return:
        """
        try:
            try:
                self.hub = self.client.containers.get('anna-worker_selenium_hub')
                if self.hub.status != 'running':
                    self.hub.remove()
                    self.keep_hub_alive()
            except docker.errors.NotFound:
                try:
                    self.hub = self.client.containers.run('selenium/hub', name='anna-worker_selenium_hub', ports={'4444/tcp': 4444},
                                                          detach=True)
                except docker.errors.APIError:
                    self.hub.stop()
                    self.keep_hub_alive()
        except docker.errors.APIError:
            pass

    def update_jobs(self):
        for job in self.jobs:
            self.update_job(job)

    def is_running(self, job):
        """
        Check if the job's container is running
        :param job:
        :return:
        """
        if job.container is not None:
            container = self.get_container(job)
            if container is not False:
                return container.status in ('starting', 'running')
        return False

    def get_running(self):
        return [job for job in self.jobs if self.is_running(job)]

    def update_job(self, job):
        if not self.is_running(job) and job.container is not None:
            self.remove(job)

    def stop_container(self, job):
        container = self.get_container(job)
        if container is not False:
            job.log = self.get_logs(job)
            if container.status == 'running':
                container.stop()
            container.remove()

    def get_container(self, job):
        if job.container is None:
            return False
        try:
            return self.client.containers.get(job.container)
        except docker.errors.NotFound:
            return False
        except docker.errors.NullResource:
            return False
        except docker.errors.APIError:
            return False
        except requests.exceptions.ReadTimeout:
            return False

    def prune(self):
        try:
            for job in self.jobs:
                if job.container is not None and not self.is_running(job):
                    self.stop_container(job)
        except docker.errors.APIError as e:
            return False
        self.client.containers.prune()

    def get_logs(self, job):
        container = self.get_container(job)
        if container is not False:
            return re.sub("\\x1b\[0m|\\x1b\[92m|\\x1b\[91m|\\x1b\[93m", '',
                          container.logs().decode('utf-8'))  # colorless
        else:
            return 'unable to get logs from container'

    def can_run_more(self):
        """
        Just a simple check against max_concurrent
        """
        queue_length = len(self.jobs)
        if queue_length == 0:
            return False
        running = len(self.get_running())
        return queue_length - running > 0 and running < self.max_concurrent

    @staticmethod
    def before_start(job):
        """
        Make sure we can run the job, set the status & report to slack
        :param job:
        :return:
        """
        if job.driver not in ('chrome', 'firefox'):
            raise TypeError('desired driver(s) not supported: ' + job.driver)

    def __start__(self, job):
        job.container = str(self.run_container(job).short_id)
        return job.container

    @staticmethod
    def after_start(job):
        """
        Set the status & report to slack
        :param job:
        :return:
        """
        pass

    def start_job(self, job):
        """
        Starts the next pending job in the queue
        """
        try:
            self.before_start(job)
            container = self.__start__(job)
            self.after_start(job)
            return container
        except TypeError:
            return ''

    def run_container(self, job):
        """
        Attempt to start the container
        :param job:
        :return:
        """
        image, volumes, command = job.get_image_volumes_and_command()
        return self.client.containers.run(
            image=image,
            links=self.container_options['links'],
            shm_size=self.container_options['shm_size'],
            detach=self.container_options['detach'],
            command=command)

    def available(self):
        return len(self.jobs) < self.max_concurrent

    def append(self, new_job):
        if not isinstance(new_job, dict) or any(attribute not in new_job for attribute in job.attributes):
            raise TypeError
        self.jobs.append(
            job.Job(id=new_job['id'], container=new_job['container'], driver=new_job['driver'], site=new_job['site'],
                    worker=socket.gethostname(), host=self.config['api']['host'], token=self.config['api']['token']))
        return self.start_job(self.jobs[len(self.jobs) - 1])

    def remove(self, job):
        self.stop_container(job=job)
        self.jobs.remove(tuple(j for j in self.jobs if j.id == job.id)[0])


from django.shortcuts import render

# Create your views here.
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework import status
from .models import FoodProducts
from rest_framework import generics
from .serializers import *
from rest_framework.response import Response

from django.core.mail import send_mail
from datetime import datetime

class CreateProducts(generics.CreateAPIView):

    serializer_class = ProductSerializer
    def post(self, request, format=None):
        serializer = ProductSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response({"message": "Success"}, status=status.HTTP_201_CREATED)

        return Response({'message': list(serializer.errors.keys())[0]+' - '+list(serializer.errors.values())[0][0]}, status=status.HTTP_200_OK)

class UpdateProducts(APIView):
    serializer_class =ProductSerializer

    def get(self,request,pk):
        queryset = FoodProducts.objects.all().filter(pk = pk).values()
        return Response(queryset)

    def patch(self, request,pk):
        update_object = FoodProducts.objects.get(pk= pk)
        serializer = ProductSerializer(update_object, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response( data=serializer.data)
        return Response( data="wrong parameters")

class get_items(APIView):
    serializer_class = ProductSerializer
    def get(self,request,format = None):
        queryset = FoodProducts.objects.all()

        if request.query_params['item_name']:
            queryset = queryset.filter(item_name__icontains = request.query_params['item_name'])

        # if request.query_params['item_type']:
        #     queryset = queryset.filter(item_type__icontains = request.query_params['item_type'])

        if request.query_params['made_of_category']:
            queryset = queryset.filter(made_of_category__icontains = request.query_params['made_of_category'])

        if request.query_params['material_used']:
            queryset = queryset.filter(material_used__icontains = request.query_params['material_used'])

        if request.query_params['item_price']:
            queryset = queryset.filter(item_price__lte = request.query_params['item_price'])

        return Response({"data":queryset.values()})


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 12 20:30:49 2021

@author: carolineskalla
"""
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d

#plotting2 imports
# from plotting2 import log
# from plotting2 import Agent
# from plotting2 import Task
# from plotting2 import Blackboard
# from plotting2 import Initialization
# from plotting2 import MoveTask
# from plotting2 import MoveAgent
# from plotting2 import Assign
#from plotting2 import AssignUnclaimed
#from plotting2 import AgentsWork
#from plotting2 import accessSkillTypes
from plotting2 import simulation
#from plotting2 import Test3D

def Test3D():
    #3D plot of the number of agents and the prortion of generalists
    numRuns = 3
    masterScoreList3DMean = []
    masterScoreList3DSD = []
    #vary proportion of generalists
    foxhedgeArray = ([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
    foxhedgeArraySize = len(foxhedgeArray)
    #vary number of agents
    #agentNumbers = np.array([1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 500, 1000])
    #agentNumbers = [1]
    #list = range(10, 100, 10)
    #agentNumbers.append(list)
    agentNumbers = [element for element in range(10, 1000, 50)]
    #numTasks = 10*numAgents
    i = 0
    while(i < foxhedgeArraySize):
        temp = []
        temp2 = []
        masterScoreList3DMean.append(temp)
        masterScoreList3DSD.append(temp2)
        i += 1

    #print(numAgents)
    #print(numTasks)
    index = 0
    for ratio in foxhedgeArray:
        for agentNum in agentNumbers:
            trials = []
            for runs in range(numRuns):
                #trials = []
                timeFactor = 2
                numAgents = agentNum
                #numTasks = agentNum*10
                #numTasks = int(agentNum*1.5)
                numTasks = agentNum*10
                foxhedge = ratio
                penalty = 0.1
                scorecoeff = 0.1
                score = simulation(timeFactor, numAgents, numTasks, foxhedge, penalty, scorecoeff)
                trials.append(score)
            #stdDev = statistics.stdev(trials)
            print(trials)
            avg = statistics.median(trials)
           # avg = statistics.mean(trials)
           # sd = statistics.stdev(trials)
            masterScoreList3DMean[index].append(avg)
           # masterScoreList3DSD[index].append(sd)
        index += 1

    #plotting 3D Mean
    x = []
    y = []
    z = []
    count = 0

    for r in masterScoreList3DMean:
        count2 = 0
        for na in r:
            x.append(foxhedgeArray[count])
            y.append(agentNumbers[count2])
            z.append(na)
            count2 += 1
        count += 1
    np.array(x)
    np.array(y)
    np.array(z)

    #plotting 3D Standard Dev
    xSD = []
    ySD = []
    zSD = []
    count = 0

    for r in masterScoreList3DSD:
        count2 = 0
        for na in r:
            xSD.append(foxhedgeArray[count])
            ySD.append(agentNumbers[count2])
            zSD.append(na)
            count2 += 1
        count += 1

    """

    # Creating figure for Mean
    fig = plt.figure(figsize = (10, 7))
    ax = plt.axes(projection ="3d")

    # Creating plot
    #ax.surface_plot(x, y, z, cmap=cm.coolwarm,linewidth=0, antialiased=False)
    ax.scatter3D(x, y, z, color = "blue")
    plt.title("Mean Score vs Number of Agents and Proportion of Generalists")
    plt.xlabel('Proportion of Generalists')
    plt.ylabel('Number of Agents')
    ax.set_zlabel('Mean Score')
    plt.figtext(.5, 0.07, "timeFactor = " + str(timeFactor) + ", penalty = " + str(penalty) +  ", scorecoeff = " + str(scorecoeff) + ", numRuns = " + str(numRuns), ha="center", fontsize=10)
    plt.show()
    # show plot
    #plt.show()

    # Creating figure for Standard Dev
    fig2 = plt.figure(figsize = (10, 7))
    ax = plt.axes(projection ="3d")

    # Creating plot
    #ax.surface_plot(x, y, z, cmap=cm.coolwarm,linewidth=0, antialiased=False)
    ax.scatter3D(xSD, ySD, zSD, color = "blue")
    plt.title("Standard Deviation of Score vs Number of Agents and Proportion of Generalists")
    plt.xlabel('Proportion of Generalists')
    plt.ylabel('Number of Agents')
    ax.set_zlabel('Mean Score')
    plt.figtext(.5, 0.07, "timeFactor = " + str(timeFactor) + ", penalty = " + str(penalty) +  ", scorecoeff = " + str(scorecoeff) + ", numRuns = " + str(numRuns), ha="center", fontsize=10)

    # show plot
    plt.show()

    """

    #surface plot (of mean)
    # target grid to interpolate to
    xi = np.arange(0,1.01,0.001)
    yi = np.arange(10,1000.01,0.001)
    xi,yi = np.meshgrid(xi,yi)

    # interpolate
    zi = griddata((x,y),z,(xi,yi),method='linear')
    print(zi)
    """
    #plot
    fig2 = plt.figure()
    axes = fig2.gca(projection ='3d')
    axes.plot_surface(xi, yi, zi)
    #plt.plot(x,y,'k.')
    plt.xlabel('Proportion of Generalist',fontsize=10)
    plt.ylabel('Number of AGents',fontsize=10)
    axes.set_zlabel('Mean Score', fontsize=10)
    plt.title("Mean Score vs Proportion of Generalists and Number of Agents")
    plt.figtext(.5, 0.0, "timeFactor = " + str(timeFactor) + ", penalty = " + str(penalty) +  ", scorecoeff = " + str(scorecoeff) + ", numRuns = " + str(numRuns), ha="center", fontsize=10)
    plt.show()

    #surface plot (of Std Dev)
    # target grid to interpolate to
    xiSD = np.arange(0,1.01,0.001)
    yiSD = np.arange(10,1000.01,0.001)
    xiSD,yiSD = np.meshgrid(xi,yi)

    # interpolate
    ziSD = griddata((xSD,ySD),zSD,(xiSD,yiSD),method='linear')

    #plot
    fig3 = plt.figure()
    axes = fig3.gca(projection ='3d')
    axes.plot_surface(xiSD, yiSD, ziSD)
    #plt.plot(x,y,'k.')
    plt.xlabel('Proportion of Generalist',fontsize=10)
    plt.ylabel('Number of AGents',fontsize=10)
    axes.set_zlabel('Standard Deviation of  Score', fontsize=10)
    plt.title("Mean Score vs Proportion of Generalists and Number of Agents")
    plt.figtext(.5, 0.0, "timeFactor = " + str(timeFactor) + ", penalty = " + str(penalty) +  ", scorecoeff = " + str(scorecoeff) + ", numRuns = " + str(numRuns), ha="center", fontsize=10)
    plt.show()

    """
####Functions to run
#main()
#testTimeFactor()
#Test3D()


from sqlalchemy import inspect

class ModelHistoryProxy(object):
    """ Proxy object to gain access to historical model attributes.

    This leverages SqlAlchemy attribute history to provide access to the previous value of an attribute.
    """

    def __init__(self, instance):
        self.__instance = instance
        self.__inspect = inspect(instance)

    def __getattr__(self, key):
        # Get the attr
        attr = getattr(self.__inspect.attrs, key)

        # Examine attribute history
        # If a value was deleted (e.g. replaced) -- we return it as the previous version.
        # Otherwise it's unchanged, and we return the current value
        history = attr.history
        if not history.deleted:
            # No previous value, return the current value instead
            return attr.value
        else:
            # Return the previous value
            # It's a tuple, since History supports collections, but we do not support these,
            # so just get the first element
            return history.deleted[0]


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

import ConfigParser
import sys
import os
import time
import string
from IPy import IP
from pydota.format.conf_settings import ServerConf
from pydota.commonlib.pydotalog import pydotalog

GEOIP_SORT = []
GEOIP = {}
file_list = {}

def LoadLiveMeizi(start_time):
    """
    :summery: 加载直播媒资信息
    :param start_time: 加载媒资信息的时刻，12为字符串，201511031700
    :return: 媒资信息dict
    """
    Meizi_info = {}

    output_path = ServerConf["meizi"]["output_path"]

    file = output_path + '/' + start_time + '_live.csv'
    if not os.path.exists(file):
        pydotalog.error("live_meizi info file is not exists!")
        sys.exit(-1)

    with open(file) as fp:
        for i, line in enumerate(fp):
            try:
                record = line.strip().split(',')
                sourceid = record[0]
                activityid = record[2]
                cameraid = record[5]
                timeid = record[7]
                startTime = record[8]
                endTime = record[9]
                try:
                    Meizi_info[sourceid].append([activityid, cameraid, timeid, startTime, endTime])
                except KeyError:
                    Meizi_info[sourceid] = []
                    Meizi_info[sourceid].append([activityid, cameraid, timeid, startTime, endTime])
            except KeyError:
                pydotalog.error("line[%d]: live meizi index err!", i)
                continue
    return Meizi_info

def CheckLiveTime(log_time, live_info=[]):
    """
    :summery 校验日志时间是否有效的活动时间期间,并返回活动id和活动摄像机id
    :param log_time: 需要校验的日志时间时间戳
    :param live_info: 日志对应活动的活动信息，list数组
    :return: errnum,str,str 0-有效 －1 时间格式错误 －2 时间不在有效期内
    """
    for live_info_one in live_info:
        try:
            startTime = time.strptime(live_info_one[3], "%Y-%m-%d %H:%M:%S")
            endTime = time.strptime(live_info_one[4], "%Y-%m-%d %H:%M:%S")

            startTime = int(time.mktime(startTime))
            endTime = int(time.mktime(endTime))

            if startTime <= int(log_time) <= endTime:
                return 0, live_info_one[0], live_info_one[1]
            else:
                continue

        except (KeyError, ValueError) as e:
            pydotalog.error("check time failed: %s", str(e))
            return -1, 0, 0

    pydotalog.error("%s is not a valid time !", str(log_time))
    return -2, 0, 0

def loadGeoIp(filename):
    """
    :summery: 加载GEOIP库
    :param filename: GEOIP文件路径
    :return: 生成GEOIP相关dict
    """
    fp = open(filename)
    for i, line in enumerate(fp):
        try:
            record = string.split(line, "\t")
            rangmin  = record[0]
            rangmax  = record[1]
            country  = record[2]
            province = record[3]
            city     = record[4]
            operator = record[6]
            rangmin = IP(rangmin).int()
            rangmax = IP(rangmax).int()
            GEOIP[rangmin] = [rangmax, country, province, city, operator]
            GEOIP_SORT.append(rangmin)
        except ValueError:
            sys.stderr.write(("value error,%s") % line)
    GEOIP_SORT.sort()
    fp.close()

def _getRangeKey(userip):
    low = 0
    height = len(GEOIP_SORT)-1
    while low < height:
        mid = (low+height)/2
        if GEOIP_SORT[mid] < userip and GEOIP_SORT[mid + 1] < userip:
            low = mid + 1
        elif GEOIP_SORT[mid] > userip and GEOIP_SORT[mid - 1] > userip:
            height = mid - 1
        elif GEOIP_SORT[mid] <= userip and GEOIP_SORT[mid +1] > userip:
            return GEOIP_SORT[mid]
        elif GEOIP_SORT[mid -1] <= userip and GEOIP_SORT[mid] > userip:
            return GEOIP_SORT[mid -1]
        elif GEOIP_SORT[mid + 1] == userip:
            return GEOIP_SORT[mid +1]
        else:
            return None
    return None

def formatLocation(userip):
    """
    :summery: 根据userip获取地域信息
    :param userip: 用户ip 127.0.0.1
    :return: 地域信息list
    """
    userip = userip.strip('""')
    userip = IP(userip).int()
    location = _getRangeKey(userip)
    if location and GEOIP[location]:
        if location <= userip <= GEOIP[location][0]:
            return GEOIP[location]
    else:
        return None

def formatTime(timedata):
    """
    :summery time结构体格式化时间输出，异常时,抛出ValueError
    :param timedata: time结构体结构
    :return:年月日字符串,时分秒字符串,时间戳
    """
    try:
        timetmp_date = time.strftime('%Y%m%d', timedata)
        timetmp_time = time.strftime('%H%M%S', timedata)
        timeStamp = int(time.mktime(timedata))
        return timetmp_date, timetmp_time, timeStamp
    except (ValueError, TypeError):
        raise ValueError("timeerr")

def __genOutputFileName(log_time, topic, start_time, data_type):
    """
    根据日志时间，topic，后缀，数据类型拼接写入文件
    :param log_time: 需要写入的日志时间,精确到小时
    :type log_time: basestring 201511012200 12位
    :param topic: kafka－topic名称
    :type topic: basestring
    :param start_time: 文件后缀名称, 为recv文件的时刻
    :type start_time: basestring 201511012300 12位
    :param data_type: 写入数据的类型，分为 orig,des,err
    :type data_type: basestring
    :return:
    """
    output_path = ServerConf["output_path"][data_type]
    if output_path == "":
        pydotalog.error("initConfig fail: no key[output_path or %s]", data_type)
        return ""

    if "err" in data_type:
        file_str = '{0}/{1}/{2}/err_{3}_{4}_{5}_{6}'.format(str(output_path), log_time[0:4],
                                                log_time[4:6], log_time, topic, start_time, data_type)
    elif "des" == data_type:
        if "_pv" in topic:
            file_str = '{0}/{1}/{2}/{3}_pvrawdata_{4}_{5}'.format(str(output_path), log_time[0:4],
                                                    log_time[4:6], log_time, topic, start_time)
        elif "mglive" in topic:
            file_str = '{0}/{1}/{2}/{3}_mgliverawdata_{4}_{5}'.format(str(output_path), log_time[0:4],
                                                    log_time[4:6], log_time, topic, start_time)
        elif "dau_" in topic:
            file_str = '{0}/{1}/{2}/{3}_daurawdata_{4}_{5}'.format(str(output_path), log_time[0:4],
                                                    log_time[4:6], log_time, topic, start_time)
        else:
            file_str = '{0}/{1}/{2}/{3}_playrawdata_{4}_{5}'.format(str(output_path), log_time[0:4],
                                                    log_time[4:6], log_time, topic, start_time)
    else:
        file_str = '{0}/{1}/{2}/{3}_{4}_{5}'.format(str(output_path), log_time[0:4],
                                                log_time[4:6], log_time, topic, start_time)
    file_str = os.path.abspath(file_str)
    dir_name = os.path.dirname(file_str)
    if not os.path.exists(dir_name):
        try:
            os.makedirs(dir_name)
        except OSError as e:
            pydotalog.error("make dir failed:[%s]", str(e))
            return ""
    return file_str

def __output_to_files(log_time, line, topic, start_time, data_type):
    str_log_file = __genOutputFileName(log_time, topic, start_time, data_type)

    if str_log_file == "":
        return False

    if str_log_file in file_list:
        log_file = file_list[str_log_file]
    else:
        try:
            log_file = open(str_log_file, 'w')
            file_list[str_log_file] = log_file
        except IOError as e:
            pydotalog.error("IOError: %s", str(e))
            return False

    log_file.write(line.strip('\n') + '\n')
    return True

def __check_time(log_time):
    """
    :summary: 校验log_time是否合理
    :param log_time: 201510101000
    :return: boolean
    """
    if len(log_time) != 12:
        return False
    try:
        return 201000000000 < int(log_time) < 205000000000
    except ValueError:
        pydotalog.error("log_time[%s] is overtime", str(log_time))
        return False

def write_to_file(line, topic, log_time, start_time, data_type):
    """
    :summary: 根据传入时间以及日志时间，写入对应文件
    :param line: 需要写入的数据,分为原始日志数据和清洗后的des
    :type line: basestring
    :param topic: kafka－topic名称
    :type topic: basestring
    :param log_time: 需要写入的日志时间,精确到小时
    :type log_time: basestring 201511012200 12位
    :param start_time: 文件后缀名称, 为recv文件的时刻
    :type start_time: basestring 201511012300 12位
    :param data_type: 写入数据的类型，分为 orig,des,des_err,orig_err
    :type data_type: basestring
    :return: boolean 是否成功写入
    """
    if __check_time(log_time):
        return __output_to_files(log_time, line, topic, start_time, data_type)
    else:
        return False

def close_files():
    for (_k, _v) in file_list.items():
        _v.close()

def check_act_field(line, *args):
    if len(line.strip('\n')) == 0 or len(args) == 0:
        return False
    for arg in args:
        act_str = '"act":"' + str(arg) + '"'
        if line.find(act_str) != -1:
            return True

    return False

def check_pt_field(line, *args):
    if len(line.strip('\n')) == 0 or len(args) == 0:
        return False
    for arg in args:
        act_str = '"pt":"' + str(arg) + '"'
        act_str1 = '"pt":' + str(arg)
        if line.find(act_str) != -1 or line.find(act_str1) != -1:
            return True

    return False

def ip_into_int(ip):
    # 先把 192.168.1.13 变成16进制的 c0.a8.01.0d ，再去了“.”后转成10进制的 3232235789 即可。
    # (((((192 * 256) + 168) * 256) + 1) * 256) + 13
    try:
        ip_int = reduce(lambda x, y: (x << 8)+y, map(int, ip.split('.')))
    except ValueError:
        return 0
    return ip_int

def is_internal_ip(ip):
    ip = ip_into_int(ip)

    # 无效ip统一当内网ip透传
    if ip == 0:
        return True
    net_a = ip_into_int('10.255.255.255') >> 24
    net_b = ip_into_int('172.31.255.255') >> 20
    net_c = ip_into_int('192.168.255.255') >> 16
    return ip >> 24 == net_a or ip >> 20 == net_b or ip >> 16 == net_c

if __name__ == "__main__":
    pass


from tkinter import  Label, Button, Frame
from CustomType.View import View
from Views.Ventilator_Button import VentilatorButton

class ViewVentilator(Frame):
    class Constants:
        label_size = 10
        toggle_size = 20
        text_button_back = "Back to Home"
        manual_mode = False

    def __init__(self, parent, change_view_handler = None, tap_handler = None, text_view = None):
        super().__init__(parent)

        self.__change_view_handler = change_view_handler
        label = Label(self, text = text_view)
        label.pack(pady = self.Constants.label_size, padx = self.Constants.label_size)

        self.__toogle = VentilatorButton(self, tap_toggle_handler = tap_handler)
        self.__toogle.pack(pady = self.Constants.toggle_size, padx = self.Constants.toggle_size)

        self.back_button = Button(self, text = self.Constants.text_button_back,
                            command = lambda: self.__did_tap_change_button(View.Main_View))
        self.back_button.pack()

    def __did_tap_change_button(self, view):
        if self.__change_view_handler is None:
            return
        self.__change_view_handler(view)
        self.Constants.manual_mode = False


# coding=utf-8

from django.conf.urls import patterns, url

from alfalombard.apps.pawnshop.views import *

urlpatterns = patterns('alfalombard.apps.pawnshop.views',

    url(r'^reports/ticket/$', some_view2, name='report-ticket'),
    url(r'^reports/ticket/(?P<pk>\d+)/$', TicketPdfView.as_view(), name='report-ticket'),

    url(r'^', IndexView.as_view() , name='index'),

    # url(r'^search/$', SearchView.as_view() , name='search'),
    # url(r'^search/live/$', SearchView.as_view(live_clients=True) , name='search_live'),

    # url(r'^client(?P<pk>\d+)/$', ClientView.as_view(), name='client'),
    # url(r'^client(?P<pk>\d+)/edit/$', ClientEdit.as_view(), name='client_edit'),
    # url(r'^new-client/$', ClientNew.as_view(), name='client_new'),
    #
    # url(r'^client(?P<client_id>\d+)/ticket(?P<pk>\d+)/$', TicketView.as_view(), name='ticket'),
    # url(r'^client(?P<client_id>\d+)/ticket(?P<pk>\d+)/re/$', TicketNewView.as_view(), name='ticket_re'),
    # url(r'^client(?P<client_id>\d+)/new-ticket/$', TicketNewView.as_view(), name='ticket_new'),
    # url(r'^new-ticket/$', TicketNewView.as_view(), name='ticket_new_no_client'),
    #
    # url(r'^ajax/$', Ajax.as_view(), name='ajax'),

)


#!/home/xlin/miniconda3/bin/python
# -*- coding: utf-8 -*-

import logging
import os.path
import sys
import multiprocessing

import numpy as np
from gensim.corpora import WikiCorpus
from gensim.models import Word2Vec
from gensim.models.word2vec import LineSentence
from gensim.models.keyedvectors import KeyedVectors
from scipy import spatial

from numpy import dot
from numpy.linalg import norm

from sklearn.manifold import TSNE
import matplotlib.pyplot as plt

if __name__ == '__main__':

    # check and process input arguments
    if len(sys.argv) < 3:
        print(globals()['__doc__'] % locals())
        sys.exit(1)

    wordPairs = []
    with open("word_pair.100.csv") as inFile:
        for aLine in inFile:
            aLine = aLine.strip('\n')
            wordPairs.append(aLine)
        inFile.close()

    wv1 = KeyedVectors.load_word2vec_format(sys.argv[1], binary=True)
    wv2 = KeyedVectors.load_word2vec_format(sys.argv[2], binary=True)

    sumS = 0
    diffS = []
    Smin = 1.0e100
    Smax =-1.0e100
    N = 0
    for aLine in wordPairs:
        tokens = aLine.split(',')
        S1 = wv1.similarity(tokens[0].strip(), tokens[1].strip())
        S2 = wv2.similarity(tokens[0].strip(), tokens[1].strip())
        S0 = (S1 - S2) / 2.0
        #print("%s %s => %.2f" % (tokens[0].strip(), tokens[1].strip(), S1) )
        #print("%s %s => %.2f" % (tokens[0].strip(), tokens[1].strip(), S2) )
        diffS.append(S0)
        if (Smin > S0):
            Smin = S0
        if (Smax < S0):
            Smax = S0
        sumS = sumS + S0
        N = N + 1

    if (len(sys.argv) == 4) :
        outFile = open(sys.argv[3], 'w')
        for ii in range(N):
            aLine = "%.5f\n" %  diffS[ii]
            outFile.write(aLine)
        outFile.close()

    Smean = sumS / N
    sigma = 0
    for ii in range(N):
        diffS[ii] = diffS[ii] - Smean
        sigma = sigma + diffS[ii] * diffS[ii]
    sigma = np.sqrt(sigma/(N-1))
    print("%d, %.2f, %.2f, %.2f, %.2f" % (N, Smin*100, Smax*100, Smean*100, sigma*100))


class KlimaKumanda():
    def __init__(self,Durum="açık",Derece=20,Modlistesi=["sicak","soğuk","fan","nem"],Mod="sıcak",SW=0):
        print("Klima Kumandası aktif!")
        self.Durum=Durum
        self.Derece=Derece
        self.Modlistesi=Modlistesi
        self.Mod=Mod
        self.SW=SW

    def Derece_arttir_azalt(self):
        while 1:
            derece=input("+ ile dereceyi yükseltebilir, - ile düşürebilirsiniz.İşlemi bitirmek için q'ye basabilirsiniz.")
            if derece=="+":
                if self.Derece!=30:
                    self.Derece+=1
                    print("Sıcaklık değeri: ",self.Derece)
            elif derece=="-":
                if self.Derece!=0:
                    self.Derece-=1
                    print("Sıcaklık değeri",self.Derece)
            elif derece=="q":
                print("Derece güncellendi")
                break

    def Klima_ac_kapa(self):
        if self.Durum=="kapalı":
            self.Durum="açık"
            print("klima açılıyor")
        else:
            self.Durum="kapalı"
            print("klima kapanıyor")

    def __str__(self):
        return "Klima Durumu : {} \n Derece : {} \n Mod : {} \n SW : {} ".format(self.Durum,self.Derece,self.Mod,self.SW)
    def Modsec(self):
        print("Modunuz : ",self.Mod)
        if self.Mod=="sıcak":
            self.Mod=self.Modlistesi[1]
        elif self.Mod=="soğuk":
            self.Mod=self.Modlistesi[2]
        elif self.Mod=="fan":
            self.Mod=self.Modlistesi[3]
        else:
            self.Mod=self.Modlistesi[0]
        print("modunuz :",self.Mod)

    def SW_durum(self):
        self.SW+=1
        if(self.SW>4):
            self.SW=0
        print("Sw Durumu",self.SW)

kumanda=KlimaKumanda()
print("""
        ****KLİMA KUMANDASI*****

1-Aç-Kapa
2-Bilgileri Goster
3-Derece Değişikliği
4-Mod Değişikliği
5-SW Ayarı

""")
while 1:
    secim=int(input("Seçim Yapınız"))
    if secim==1:
        kumanda.Klima_ac_kapa()
    elif secim==2:
        print(kumanda)
    elif secim==3:
        if kumanda.Durum=="açık":
            kumanda.Derece_arttir_azalt()
        else:
            print("Kumanda kapalı")
    elif secim==4:
        if kumanda.Durum=="açık":
            kumanda.Modsec()
        else:
            print("Kumanda kapalı")

    elif secim==5:
        if kumanda.Durum=="açık":
            kumanda.SW_durum()
        else:
            print("Kumanda kapalı")
    else:
        print("Geçersiz Seçim")
        break