import datetime
from datetime import date
import csv
from tkinter import *
from tkinter import messagebox
from tkinter import ttk

#global app,review1,sentiment,polarity,subjectivity

def validate_date(day,month,year):
    isValidDate = True
    try :
        datetime.datetime(int(year),int(month),int(day))
    except ValueError :
        isValidDate = False
    return isValidDate

def is_float(string):
    try:
        float(string)
        return True
    except ValueError:
        return False

def validate_2():
    #global app,review1,sentiment,polarity,subjectivity

    #print(app,review1,sentiment,polarity,subjectivity)
    #validation for app name

    if app.get()=='':
        Label(screen_18_2, text="Please enter app name", font=("Open Sans",13,"bold"),fg="red",bg='white').place(x=0,y=y1+6*space_y)
        #print("Please enter app name")
        return False

    #validation for review
    if review1.get()=='':
        Label(screen_18_2, text="Please enter the review                    ", font=("Open Sans",11,"bold"),fg="red",bg='white').place(x=0,y=y1+6*space_y)
        #print("Please enter the review")
        return False

    #validation for sentiment
    if sentiment.get()=='--sentiment--':
        Label(screen_18_2, text="Please choose the sentiment                         ", font=("Open Sans",11,"bold"),fg="red",bg='white').place(x=0,y=y1+6*space_y)
        #print("Please choose the sentiment")
        return False

    #validation for sentiment polarity
    if str(polarity.get())=='' or is_float(str(polarity.get()))==False :
        Label(screen_18_2, text="Please enter the valid sentiment polarity                       ", font=("Open Sans",11,"bold"),fg="red",bg='white').place(x=0,y=y1+6*space_y)
        #print("Please enter the valid sentient polarity")
        return False
    if float(str(polarity.get()))<-1 or float(str(polarity.get()))>1:
        Label(screen_18_2, text="Please enter the valid sentiment polarity                       ", font=("Open Sans",11,"bold"),fg="red",bg='white').place(x=0,y=y1+6*space_y)
        #print("Please enter the valid sentient polarity")
        return False

    #validation for sentiment subjectivity
    if str(subjectivity.get())=='' or is_float(str(subjectivity.get()))==False :
        Label(screen_18_2, text="Please enter the valid sentient subjectivity", font=("Open Sans",11,"bold"),fg="red",bg='white').place(x=0,y=y1+6*space_y)
       # print("Please enter the valid sentient subjectivity")
        return False
    if float(str(subjectivity.get()))<0 or float(str(subjectivity.get()))>1:
        Label(screen_18_2, text="Please enter the valid sentient subjectivity", font=("Open Sans",11,"bold"),fg="red",bg='white').place(x=0,y=y1+6*space_y)
        #print("Please enter the valid sentient subjectivity")
        return False

    Label(screen_18_2, text="                                                                                ",fg="black",bg='white').place(x=0,y=y1+6*space_y)
    new_data2=[app.get(),review1.get(),sentiment.get(),polarity.get(),subjectivity.get()]
    with open('dataset_2.csv', 'a') as csvFile:
        writer = csv.writer(csvFile)
        writer.writerow(new_data2)
    csvFile.close()

    messagebox.showinfo("","Data Entered Successfully!!")
    screen_18_2.destroy()

def validate_1():
    global app2,category,rating,review,size,unit,installs,type_,price,content_rating,genre,last_updated,current_ver,android_ver
    price1='0'
    label_fg='red'
    #validation for app name
    if app2.get()=='':
        Label(screen_18_1, text="Please enter app name", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter app name")
        return False

    #validation for category
    if category.get()=='':
        Label(screen_18_1, text="Please enter the category of the app", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter the category of the app")
        return False

    #validation for rating
    if rating.get()=='' or is_float(str(rating.get()))==False :
        Label(screen_18_1, text="Please enter a valid rating                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter a valid rating")
        return False
    if float(str(rating.get()))<0 or float(str(rating.get()))>5:
        Label(screen_18_1, text="Please enter a valid rating                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter a valid rating")
        return False

    #validation for review
    if review.get()=='' or str(review.get()).isdigit()==False :
        Label(screen_18_1, text="Please enter the valid number of reviews                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter the valid number of reviews")
        return False

    if int(str(review.get()))<0 :
        Label(screen_18_1, text="Please enter a valid number of reviews                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter a valid number of reviews")
        return False

    #validation for size
    if size.get()=='' or is_float(str(size.get()))==False:
        Label(screen_18_1, text="Please enter the valid size                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter the valid size")
        return False
    if float(str(size.get()))<0:
        Label(screen_18_1, text="Please enter the valid size                                                       ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter the valid size")
        return False
    if unit.get()=='---Unit---':
        Label(screen_18_1, text="Please enter the unit of size                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter the unit of size")
        return False
    size1=str(size.get())+str(unit.get())

    #validation for installs
    if installs.get()=='' or str(installs.get()).isdigit()==False :
        Label(screen_18_1, text="Please enter the valid number of installs                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter the valid number of installs")
        return False
    installs1=int(str(installs.get()))
    if installs1<0 :
        Label(screen_18_1, text="Please enter the valid number of installs                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter a valid number of installs")
        return False
    installs1=str(installs1)[::-1]
    installs1=list(installs1)
    length=len(installs1)
    install_main=''
    count=1
    for i in range(0,length-1):
        if count%3==0:
            install_main=install_main+str(installs1[i])+','
            #count=count+1
        else:
            install_main=install_main+str(installs1[i])
        count=count+1
    install_main=install_main+str(installs1.pop())
    install_main=install_main[::-1]+"+"
    #print(install_main)

    #validation for type
    if type_.get()=='---Type---':
        Label(screen_18_1, text="Please choose type of app                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white',).place(x=0,y=y3+8.5*space_y2)
        #print("Please choose type of app")
        return False

    #validation for price
    if price.get()=='' or is_float(str(price.get()))==False :
        Label(screen_18_1, text="Please enter the valid price                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter the valid price")
        return False
    if float(str(price.get()))<0 :
        Label(screen_18_1, text="Please enter the valid price                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter the valid price")
        return False

    if (type_.get()=='Free' and price.get()!='0') or (type_.get()=='Paid' and price.get()=='0'):
        Label(screen_18_1, text="Please enter the valid price                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter the valid price")
        return False

    if price.get()!='0':
        price1='$'+str(price.get())
        #print(price)

    #validation for genre
    if genre.get()=='':
        Label(screen_18_1, text="Please enter genre of the app                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter genre of the app")
        return False

    #validation for content rating
    if content_rating.get()=='':
        Label(screen_18_1, text="Please enter content rating                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter content rating")
        return False

    #validation for last updated
    if last_updated.get()=='':
        Label(screen_18_1, text="Please enter a valid date                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter a valid date")
        return False
    try:
        day,month,year = str(last_updated.get()).split('/')
    except:
        #print("Please enter a valid date")
        Label(screen_18_1, text="Please enter a valid date                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        return False

    if validate_date(day,month,year)==False:
        Label(screen_18_1, text="Please enter a valid date                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter a valid date")
        return False

    last_updated1=date(day=int(day), month=int(month), year=int(year)).strftime('%B %d, %Y')
    #print(last_updated)

    #validation for current version
    if current_ver.get()=='':
        Label(screen_18_1, text="Please enter current version of app                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter current version of app")
        return False

    #validation for android version
    if android_ver.get()=='':
        Label(screen_18_1, text="Please enter android version                                                    ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)
        #print("Please enter android version")
        return False

    Label(screen_18_1, text="                                                                                ", font=("Open Sans",11,"bold"),fg=label_fg,bg='white').place(x=0,y=y3+8.5*space_y2)

    new_data1=[app2.get(),category.get(),rating.get(),review.get(),size1,install_main,type_.get(),price1,content_rating.get(),genre.get(),last_updated1,current_ver.get(),android_ver.get()]
    with open('dataset_1.csv', 'a') as csvFile:
        writer = csv.writer(csvFile)
        writer.writerow(new_data1)
    csvFile.close()

    messagebox.showinfo("","Data Entered Successfully!!")
    screen_18_1.destroy()

def gui_18_2():
    global app,review1,sentiment,polarity,subjectivity,screen_18_2,x1,x2,y1,y2,space_y
    screen_18_2=Tk()
    screen_18_2.configure(bg='white')
    z1=Label(screen_18_2, text=" GOOGLE PLAY STATS", width='35', height="4", font=("Times New Roman", 40,'bold'), fg='white', bg='#00FFAF',anchor=NW,justify=CENTER).place(x=0, y=0)
    app=StringVar()
    review1=StringVar()
    sentiment=StringVar()
    polarity=StringVar()
    subjectivity=StringVar()
    x1=130
    y1=190
    x2=x1+210
    y2=y1
    space_y=65
    box_color='#E0FFFF'
    label_fg='#002F2F'
    label_bg='#B9FFFF'
    Label(screen_18_2, text="",bg='#B9FFFF',width=76,height=30).place(x=0.25*x1,y=0.45*y1)
    Label(screen_18_2, text="  Please Enter Data",bg='#800080',fg='white',width=35,height=2, font=("Times New Roman", 19,'bold'),anchor=W).place(x=0.25*x1+3,y=0.45*y1)
    #screen_18_2.title("My first frame")
    screen_18_2.geometry("600x665+20+40")
    a=Label(screen_18_2, text="App Name : ",bg=label_bg,fg=label_fg).place(x=x1,y=y1)
    b=Entry(screen_18_2, textvar=app,bg=box_color).place(x=x2, y=y2)
    c=Label(screen_18_2, text="Review : ",bg=label_bg,fg=label_fg).place(x=x1,y=y1+space_y)
    d=Entry(screen_18_2, textvar=review1,bg=box_color).place(x=x2, y=y2+space_y)
    e=Label(screen_18_2, text="Sentiment : ",bg=label_bg,fg=label_fg).place(x=x1,y=y1+2*space_y)
    sentiment_list=['Possitive','Neutral','Negative']
    f = OptionMenu(screen_18_2, sentiment, *sentiment_list)
    f.config(width=12)
    sentiment.set('--sentiment--')
    f.place(x=x2, y=y2+2*space_y)
    g=Label(screen_18_2, text="Sentiment Polarity : ",bg=label_bg,fg=label_fg).place(x=x1,y=y1+3*space_y)
    h=Entry(screen_18_2, textvar=polarity,bg=box_color).place(x=x2, y=y2+3*space_y)
    i=Label(screen_18_2, text="Sentiment Subjectivity : ",bg=label_bg,fg=label_fg).place(x=x1,y=y1+4*space_y)
    j=Entry(screen_18_2, textvar=subjectivity,bg=box_color).place(x=x2, y=y2+4*space_y)
    k=Button(screen_18_2, text="Submit", bg="#00CDCD", width=15, height=1, font=("Open Sans",13,"bold"),fg="white",command=validate_2)
    k.place(x=x2-x1,y=y1+5*space_y)

    screen_18_2.mainloop()

def gui_18_1():
    global app2,category,rating,review,size,unit,installs,type_,price,content_rating,genre,last_updated,current_ver,android_ver,screen_18_1,x3,y3,space_y2
    screen_18_1=Tk()
    screen_18_1.configure(bg='white')
    screen_18_1.geometry("600x665+20+40")
    Label(screen_18_1, text=" GOOGLE PLAY STATS", width='35', height="4", font=("Times New Roman", 40,'bold'), fg='white', bg='#00FFAF',anchor=NW,justify=CENTER).place(x=0, y=0)
    box_color='#E0FFFF'
    label_fg='#002F2F'
    label_bg='#B9FFFF'

    app2=StringVar()
    category=StringVar()
    rating=StringVar()
    review=StringVar()
    size=StringVar()
    unit=StringVar()
    installs=StringVar()
    type_=StringVar()
    price=StringVar()
    content_rating=StringVar()
    genre=StringVar()
    last_updated=StringVar()
    current_ver=StringVar()
    android_ver=StringVar()
    x3=60
    y3=170
    x4=x3+90
    y4=y3
    space_y2=55
    space_x=150
    x5=x4+space_x
    y5=y3
    x6=x5+100
    y6=y5
    #screen_18_2.title("My first frame")
    Label(screen_18_1, text="",bg='#B9FFFF',width=76,height=33).place(x=0.5*x3,y=0.45*y3)
    Label(screen_18_1, text="  Please Enter Data",bg='#800080',fg='white',width=35,height=2, font=("Times New Roman", 19,'bold'),anchor=W).place(x=0.5*x3+4,y=0.45*y3)
    #row 1
    a=Label(screen_18_1, text="App Name : ",bg=label_bg,fg=label_fg).place(x=x3,y=y3)
    b=Entry(screen_18_1, textvar=app2, bg=box_color).place(x=x4, y=y4)
    c=Label(screen_18_1, text="Last updated : \n(dd/mm/yyyy)  ",bg=label_bg,fg=label_fg).place(x=x5,y=y5)
    d=Entry(screen_18_1, textvar=last_updated, bg=box_color).place(x=x6, y=y6)
    #row 2
    e=Label(screen_18_1, text="Category : ",bg=label_bg,fg=label_fg).place(x=x3,y=y3+space_y2)
    f=Entry(screen_18_1, textvar=category, bg=box_color).place(x=x4, y=y4+space_y2)
    g=Label(screen_18_1, text="Genre : ",bg=label_bg,fg=label_fg).place(x=x5,y=y5+space_y2)
    h=Entry(screen_18_1, textvar=genre, bg=box_color).place(x=x6, y=y6+space_y2)
    #row 3
    i=Label(screen_18_1, text="Rating : ",bg=label_bg,fg=label_fg).place(x=x3,y=y3+2*space_y2)
    j=Entry(screen_18_1, textvar=rating, bg=box_color).place(x=x4, y=y4+2*space_y2)
    k=Label(screen_18_1, text="Number of : \nReviews  ",bg=label_bg,fg=label_fg).place(x=x5,y=y5+2*space_y2)
    l=Entry(screen_18_1, textvar=review, bg=box_color).place(x=x6, y=y6+2*space_y2)
    #row 4
    m=Label(screen_18_1, text="Size : ",bg=label_bg,fg=label_fg).place(x=x3,y=y3+3*space_y2)
    n=Entry(screen_18_1, textvar=size, bg=box_color).place(x=x4, y=y4+3*space_y2)
    o=Label(screen_18_1, text="Unif of : \nSize  ",bg=label_bg,fg=label_fg).place(x=x5,y=y5+3*space_y2)
    unit_list=["M","k"]
    p = OptionMenu(screen_18_1, unit, *unit_list)
    p.config(width=12)
    unit.set('---Unit---')
    p.place(x=x6, y=y6+3*space_y2)
    #row 5
    q=Label(screen_18_1, text="Number of : \nInstalls ",bg=label_bg,fg=label_fg).place(x=x3,y=y3+4*space_y2)
    r=Entry(screen_18_1, textvar=installs, bg=box_color).place(x=x4, y=y4+4*space_y2)
    s=Label(screen_18_1, text="Content : \nRating  ",bg=label_bg,fg=label_fg).place(x=x5,y=y5+4*space_y2)
    t=Entry(screen_18_1, textvar=content_rating, bg=box_color).place(x=x6, y=y6+4*space_y2)
    #row 6
    u=Label(screen_18_1, text="Type of : \nApp ",bg=label_bg,fg=label_fg).place(x=x3,y=y3+5*space_y2)
    type_list=["Free","Paid"]
    v = OptionMenu(screen_18_1, type_, *type_list)
    v.config(width=12)
    type_.set('---Type---')
    v.place(x=x4, y=y4+5*space_y2)
    w=Label(screen_18_1, text="Price :  ",bg=label_bg,fg=label_fg).place(x=x5,y=y5+5*space_y2)
    x=Entry(screen_18_1, textvar=price, bg=box_color).place(x=x6, y=y6+5*space_y2)
    #row 7
    y=Label(screen_18_1, text="Current App : \nVersion  ",bg=label_bg,fg=label_fg).place(x=x3,y=y3+6*space_y2)
    z=Entry(screen_18_1, textvar=current_ver, bg=box_color).place(x=x4, y=y4+6*space_y2)
    a1=Label(screen_18_1, text="Android : \nVersion   ",bg=label_bg,fg=label_fg).place(x=x5,y=y5+6*space_y2)
    b1=Entry(screen_18_1, textvar=android_ver, bg=box_color).place(x=x6, y=y6+6*space_y2)
    #row 8
    k=Button(screen_18_1, text="Submit", bg="#00CDCD", width=15, height=1, font=("Open Sans",13,"bold"),fg="white",command=validate_1)
    k.place(x=3.7*x3,y=y3+7.05*space_y2)

    screen_18_1.mainloop()


import openai

def main():
    # Set your OpenAI API key here
    openai.api_key = "YOUR OPEN AI API KEY"

    while True:
        # Get user input.
        user_input = input("You: ")

        # Use the ChatGPT API to generate a response.
        response = openai.Completion.create(
            engine="text-davinci-003",  # Choose the engine you want to use
            prompt=user_input,
            max_tokens=50  # You can adjust this value to control response length
        )

        # Print the response from the model.
        print("Chatbot:", response.choices[0].text.strip())

if __name__ == "__main__":
    main()


# standard library
from typing import Any, Tuple, Union

# dependencies
import numpy as np
from pytest import mark
from typing_extensions import Annotated as Ann
from typing_extensions import Literal as L

# submodules
from xarray_dataclasses.typing import (
    Attr,
    Coord,
    Data,
    Name,
    Role,
    get_dims,
    get_dtype,
    get_name,
    get_role,
)

# test datasets
testdata_dims = [
    (Coord[Tuple[()], Any], ()),
    (Coord[L["x"], Any], ("x",)),
    (Coord[Tuple[L["x"]], Any], ("x",)),
    (Coord[Tuple[L["x"], L["y"]], Any], ("x", "y")),
    (Data[Tuple[()], Any], ()),
    (Data[L["x"], Any], ("x",)),
    (Data[Tuple[L["x"]], Any], ("x",)),
    (Data[Tuple[L["x"], L["y"]], Any], ("x", "y")),
    (Ann[Coord[L["x"], Any], "coord"], ("x",)),
    (Ann[Data[L["x"], Any], "data"], ("x",)),
    (Union[Ann[Coord[L["x"], Any], "coord"], Ann[Any, "any"]], ("x",)),
    (Union[Ann[Data[L["x"], Any], "data"], Ann[Any, "any"]], ("x",)),
]

testdata_dtype = [
    (Coord[Any, Any], None),
    (Coord[Any, None], None),
    (Coord[Any, int], np.dtype("i8")),
    (Coord[Any, L["i8"]], np.dtype("i8")),
    (Data[Any, Any], None),
    (Data[Any, None], None),
    (Data[Any, int], np.dtype("i8")),
    (Data[Any, L["i8"]], np.dtype("i8")),
    (Ann[Coord[Any, float], "coord"], np.dtype("f8")),
    (Ann[Data[Any, float], "data"], np.dtype("f8")),
    (Union[Ann[Coord[Any, float], "coord"], Ann[Any, "any"]], np.dtype("f8")),
    (Union[Ann[Data[Any, float], "data"], Ann[Any, "any"]], np.dtype("f8")),
]

testdata_name = [
    (Attr[Any], None),
    (Data[Any, Any], None),
    (Coord[Any, Any], None),
    (Name[Any], None),
    (Any, None),
    (Ann[Attr[Any], "attr"], "attr"),
    (Ann[Data[Any, Any], "data"], "data"),
    (Ann[Coord[Any, Any], "coord"], "coord"),
    (Ann[Name[Any], "name"], "name"),
    (Ann[Any, "other"], None),
    (Union[Ann[Attr[Any], "attr"], Ann[Any, "any"]], "attr"),
    (Union[Ann[Data[Any, Any], "data"], Ann[Any, "any"]], "data"),
    (Union[Ann[Coord[Any, Any], "coord"], Ann[Any, "any"]], "coord"),
    (Union[Ann[Name[Any], "name"], Ann[Any, "any"]], "name"),
    (Union[Ann[Any, "other"], Ann[Any, "any"]], None),
]

testdata_role = [
    (Attr[Any], Role.ATTR),
    (Data[Any, Any], Role.DATA),
    (Coord[Any, Any], Role.COORD),
    (Name[Any], Role.NAME),
    (Any, Role.OTHER),
    (Ann[Attr[Any], "attr"], Role.ATTR),
    (Ann[Data[Any, Any], "data"], Role.DATA),
    (Ann[Coord[Any, Any], "coord"], Role.COORD),
    (Ann[Name[Any], "name"], Role.NAME),
    (Ann[Any, "other"], Role.OTHER),
    (Union[Ann[Attr[Any], "attr"], Ann[Any, "any"]], Role.ATTR),
    (Union[Ann[Data[Any, Any], "data"], Ann[Any, "any"]], Role.DATA),
    (Union[Ann[Coord[Any, Any], "coord"], Ann[Any, "any"]], Role.COORD),
    (Union[Ann[Name[Any], "name"], Ann[Any, "any"]], Role.NAME),
    (Union[Ann[Any, "other"], Ann[Any, "any"]], Role.OTHER),
]

# test functions
@mark.parametrize("tp, dims", testdata_dims)
def test_get_dims(tp: Any, dims: Any) -> None:
    assert get_dims(tp) == dims

@mark.parametrize("tp, dtype", testdata_dtype)
def test_get_dtype(tp: Any, dtype: Any) -> None:
    assert get_dtype(tp) == dtype

@mark.parametrize("tp, name", testdata_name)
def test_get_name(tp: Any, name: Any) -> None:
    assert get_name(tp) == name

@mark.parametrize("tp, role", testdata_role)
def test_get_role(tp: Any, role: Any) -> None:
    assert get_role(tp) == role


import cv2
import numpy as np

# https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_trackbar/py_trackbar.html

def nothing(x):
    pass

# Create a black image, a window
img = np.zeros((300,512,3), np.uint8)
cv2.namedWindow('gambar')

# Create trackbar for color change
cv2.createTrackbar('R','gambar',0,255,nothing)
cv2.createTrackbar('G','gambar',0,255,nothing)
cv2.createTrackbar('B','gambar',0,255,nothing)

# Create switch for ON/OFF functionality
switch = '0 : OFF \n1: ON'
cv2.createTrackbar(switch,'gambar',0,1,nothing)

while(1):
    cv2.imshow('gambar', img)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

    # get current positions of four trackbars
    r = cv2.getTrackbarPos('R','gambar')
    g = cv2.getTrackbarPos('G','gambar')
    b = cv2.getTrackbarPos('B','gambar')
    s = cv2.getTrackbarPos(switch,'gambar')

    if s == 0:
        img[:] = 0
    else:
        img[:] = [b,g,r]

cv2.destroyAllWindows()


from operator import attrgetter
from django.template import loader
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse
from django.db.models import Q
from django.core.paginator import Paginator
from django.shortcuts import get_object_or_404, redirect, render
from form.models import CoopForm, shareCapitalFiles
from .serializers import *

from rest_framework import generics, permissions, response, status, pagination
from rest_framework.decorators import api_view
from rest_framework.renderers import TemplateHTMLRenderer

# Pagination for ListAPIView
class AllCooperativesListPagination(pagination.PageNumberPagination):
    page_size = 4

# All Cooperatives API View
class AllCooperativesList(generics.ListAPIView):
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'api-cooperativeslist.html'
    serializer_class = CooperativesListSerializer
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]
    pagination_class = AllCooperativesListPagination

    def get_queryset(self):
        return CoopForm.objects.all().order_by('id')

    def list(self, *args, **kwargs):
        queryset = self.get_queryset()
        page_size = self.pagination_class.page_size
        paginator = Paginator(queryset, page_size)
        page_number = self.request.GET.get("page")
        page_obj = paginator.get_page(page_number)
        is_paginated = self.paginate_queryset(queryset)

        serializer = self.get_serializer(page_obj, many=True)
        data = serializer.data

        context = {
            'coops': data,
            'paginator': paginator,
            'page_obj': page_obj,
            'is_paginated': is_paginated,
            'total_cooperatives': paginator.count
        }

        return self.get_paginated_response(context)

    def get_paginated_response(self, context):
        return response.Response(context)

# API Search Results View
class APISearchCooperative(generics.ListAPIView):
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'api-searchresult.html'
    serializer_class = CooperativesListSerializer
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]
    pagination_class = AllCooperativesListPagination

    # get search query object
    def get_queryset(self, query=None):
        query = self.request.GET.get(
            "q", None) if self.request.GET.get("q") else ''
        queryset = []
        queries = query.split(" ")
        for q in queries:
            results = CoopForm.objects.filter(
                Q(society_name__icontains=q) |
                Q(society_category__icontains=q)
            ).distinct()

            queryset.extend(iter(results))
        return sorted(list(set(queryset)), key=attrgetter('society_name'), reverse=True)

    def list(self, *args, **kwargs):
        queryset = self.get_queryset()
        page_size = self.pagination_class.page_size
        paginator = Paginator(queryset, page_size)
        page_number = self.request.GET.get("page")
        page_obj = paginator.get_page(page_number)
        is_paginated = self.paginate_queryset(queryset)

        serializer = self.get_serializer(page_obj, many=True)
        searchresult = serializer.data

        context = {
            'paginator': paginator,
            'page_obj': page_obj,
            'is_paginated': is_paginated,
            'searchresult': searchresult,
            'query': str(self.request.GET.get("q")),
            'count_query': paginator.count,
        }

        return response.Response(context)

# Register Cooperative API View
class RegisterCooperative(LoginRequiredMixin, generics.CreateAPIView):
    login_url = 'api-login'
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'api-register.html'
    serializer_class = RegisterCooperativeSerializer
    permission_classes = [permissions.IsAuthenticated]
    http_method_names = ['get', 'post']

    def get(self, request, serializer_errors=None, *args, **kwargs):
        serializer = self.get_serializer()

        context = {
            'tag': serializer,
            'defaultdate': str(datetime.date.today()),
            'errors': serializer_errors,
        }

        return response.Response(context)

    def post(self, request, *args, **kwargs):
        # check if "Limited Liability" is selected and share capital files are attached
        if self.request.data.get('nature_registration') == 'Limited Liability':
            if not self.request.FILES.getlist('attach_share_capital'):
                messages.error(
                    self.request, 'Please attach share capital files or select a different option')
                return response.Response(self.get())

        # check if "Have Bye Laws - Yes" is selected and a copy of bye laws is attached
        if self.request.data.get('have_bye_laws') == 'Yes':
            if not self.request.FILES.get('attach_bye_laws'):
                messages.error(
                    self.request, 'Please attach a copy of your bye laws or select a different option')
                return response.Response(self.get())

        # serialize, create and save the object
        try:
            serializer = self.get_serializer(data=self.request.data)
            serializer.is_valid(raise_exception=True)
        except Exception:
            messages.error(
                self.request, 'This form has errors. Please scroll down to address them')
            return response.Response(self.get(serializer_errors=serializer.errors))
        else:
            return self.create(serializer)

    def create(self, serializer):
        self.perform_create(serializer)

        # upload share capital files
        if self.request.FILES:
            sharefiles = self.request.FILES.getlist('attach_share_capital')
            if sharefiles:
                for file in sharefiles:
                    file_data = {"coopform": serializer.data['id'],
                                 "attach_share_capital": file}
                    file_serializer = ShareCapitalSerializer(data=file_data)
                    file_serializer.is_valid(raise_exception=True)
                    file_serializer.save()
                messages.success(self.request, 'File(s) uploaded successfully')

        messages.success(self.request, 'Profile has been created')
        instance = get_object_or_404(CoopForm, pk=serializer.data['id'])
        return redirect('api-retrievecooperative', pk=instance.pk)

# Update Cooperative API View
class UpdateCooperative(LoginRequiredMixin, generics.UpdateAPIView):
    login_url = 'api-login'
    template_name = 'api-updateform.html'
    renderer_classes = [TemplateHTMLRenderer]
    permission_classes = [permissions.IsAuthenticated]
    serializer_class = UpdateCooperativeSerializer
    http_method_names = ['get', 'post', 'put', 'patch']

    def get_object(self):
        return get_object_or_404(CoopForm, pk=self.kwargs['pk'])

    def get(self, request, pk):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        data = serializer.data
        society_category = serializer['society_category'].choices

        sharefiles = shareCapitalFiles.objects.filter(
            coopform_id=instance.pk)
        sharefiles_serializer = ShareCapitalSerializer(sharefiles, many=True)
        sharefilesdata = sharefiles_serializer.data

        context = {
            'form': data,
            'obj': instance,
            'sharefiles': sharefilesdata,
            'coopform_id': instance.pk,
            'tag': serializer,
            'LGA': data.get('LGA'),
            'BYE_LAWS_CHOICES': data.get('BYE_LAWS_CHOICES'),
            #'categories': data.get('SOCIETY_CATEGORY'),
            'categories': list(society_category.items()),
            'REGISTRATION': data.get('REGISTRATION_TYPE'),
        }

        return response.Response(context)

    def files_to_exclude(self, data, serializer):
        # if these fields are empty in request.POST, but not empty in the database,
        # add to exclusion list
        files = ['society_logo', 'attach_first_minute',
                 'attach_second_minute', 'attach_bye_laws']

        # add to exclude_fields
        for file in files:
            if file in data and not self.request.data.get(file):
                serializer.exclude_fields([file])

    def upload_share_capital_files(self, instance):
        # upload share capital files
        if self.request.FILES:
            sharefiles = self.request.FILES.getlist('attach_share_capital')
            if sharefiles:
                for file in sharefiles:
                    file_data = {"coopform": instance.pk,
                                 "attach_share_capital": file}
                    file_serializer = ShareCapitalSerializer(data=file_data)
                    file_serializer.is_valid(raise_exception=True)
                    file_serializer.save()
                messages.success(self.request, 'File(s) uploaded successfully')

    def checks(self, instance):
        # if "Without Limited Liability" is selected,
        # delete the associated share capital files
        if self.request.data.get('nature_registration') == 'Without Limited Liability':
            sharefiles = shareCapitalFiles.objects.filter(
                coopform_id=instance.pk)
            if sharefiles:
                sharefiles.delete()
                messages.success(
                    self.request, 'Share Capital File(s) have been deleted')

        # if "No" is selected, delete the associated bye laws file
        if self.request.data.get('have_bye_laws') == 'No':
            if instance.attach_bye_laws:
                instance.attach_bye_laws.delete()
                messages.success(
                    self.request, 'Bye Laws file has been deleted')

        # if society's logo checkbox is selected, delete the society logo
        if 'delete_society_logo' in self.request.POST:
            instance.society_logo.delete()
            messages.success(
                self.request, 'Society Logo has been deleted')

    def post(self, *args, **kwargs):
        instance = self.get_object()
        partial = kwargs.pop('partial', False)
        data = self.get_serializer(instance).data

        serializer = self.get_serializer(
            instance,
            data=self.request.data,
            partial=partial
        )

        # check if "Limited Liability" is selected and share capital files are attached
        if self.request.data.get('nature_registration') == 'Limited Liability':
            if not data['attach_share_capital'] and not self.request.FILES.getlist('attach_share_capital'):
                messages.error(
                    self.request, 'Please attach share capital files (5a) or choose a different option')
                return self.get(self.request, pk=instance.pk)

        # check if "Yes" is selected and a copy of bye laws is attached
        if self.request.data.get('have_bye_laws') == 'Yes':
            if not instance.attach_bye_laws and not self.request.FILES.get('attach_bye_laws'):
                messages.error(
                    self.request, 'Please attach a copy of your bye laws (9a) or choose a different option')
                return self.get(self.request, pk=instance.pk)

        # fields to exclude if they are empty in request.POST, but not empty in the database
        self.files_to_exclude(data, serializer)

        # upload share capital files
        self.upload_share_capital_files(instance)

        # perform some checks
        self.checks(instance)

        try:
            serializer.is_valid(raise_exception=True)
        except Exception as e:
            messages.error(self.request, f'{e}')
            return self.get(self.request, pk=instance.pk)
        else:
            self.perform_update(serializer)
            messages.success(self.request, 'Profile has been updated')
            return redirect('api-retrievecooperative', pk=instance.pk)

# Delete Share Capital file
@api_view(('DELETE', 'GET', ))
def deletesharecapitalfile(request, pk, coopform_id):

    # get the template to render
    template = loader.get_template('snippets/api-sharecapitalfiles.html')

    # delete the file object
    shareCapitalFiles.objects.get(id=pk).delete()

    # retrieve the new updated files list
    sharefiles = shareCapitalFiles.objects.filter(coopform_id=coopform_id)

    # serialize the list
    sharefiles_serializer = ShareCapitalSerializer(sharefiles, many=True)
    files = sharefiles_serializer.data

    # pass to context menu
    context = {
        'sharefiles': files,
        'coopform_id': coopform_id,
    }

    return HttpResponse(template.render(context))

# Retrieve Cooperative API View
class RetrieveCooperative(generics.RetrieveAPIView):
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'api-profiledetails.html'
    serializer_class = RetrieveCooperativeSerializer
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]
    http_method_names = ['get',]

    def get_object(self):
        return get_object_or_404(CoopForm, pk=self.kwargs['pk'])

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        data = serializer.data

        sharefiles = shareCapitalFiles.objects.filter(
            coopform_id=self.kwargs.get('pk'))
        sharefiles_serializer = ShareCapitalSerializer(sharefiles, many=True)
        sharefilesdata = sharefiles_serializer.data

        context = {
            'form': data,
            'tag': serializer,
            'sharefiles': sharefilesdata,
        }

        return response.Response(context)

# Delete API View
class DeleteCooperative(generics.DestroyAPIView):
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'api-deletesuccess.html'
    serializer_class = DeleteCooperativeSerializer
    permission_classes = [permissions.IsAuthenticated]
    http_method_names = ['get', 'post',]

    def get_object(self):
        try:
            return CoopForm.objects.get(pk=self.kwargs['pk'])
        except CoopForm.DoesNotExist:
            return response.Response(status=status.HTTP_404_NOT_FOUND)

    def post(self, request, *args, **kwargs):
        instance = self.get_object()
        societyname = self.get_serializer(instance).data.get('society_name')
        context = {'society_name': societyname}
        self.perform_destroy(instance)
        return response.Response(context)

# test
def test(request):
    return HttpResponse(render(request, 'api-finish.html'))


import time
import datetime
import sys
print('输入指定日期即可穿越(只能到未来)'"\n"'请输入今天星期几：')
wed=int(input('nummber'))

print('输入指定日期即可穿越(只能到未来)'"\n"'请输入目标年月日：')
y=int(input('年'))
m=int(input('月'))
d=int(input('日'))
sj=str(y)+' '+str(m)+' '+str(d)
d1=datetime.datetime(y,m,d)
print('启动中，还需要')
while True:
   d2=datetime.datetime.now()
   sec=round((d1-d2).total_seconds())
   op=[int(sec/86400),'天',int((sec-int(sec/86400)*86400)/3600),'小时',int((sec-int(sec/3600)*3600)/60),'分',int((sec-int(sec/60)*60)),'秒']

   nn=(''.join('%s' %id for id in op))
   sys.stdout.write("\r%s"%nn)
   sys.stdout.write('即可启动')
   sys.stdout.flush()
   time.sleep(1)
   if int(sec/86400)%7==1:
     out=wed+1

   if int(sec/86400)%7==2:
     out=wed+1

   if int(sec/86400)%7==3:
     out=wed+1

   if int(sec/86400)%7==4:
     out=wed+1

   if int(sec/86400)%7==5:
     out=wed+1

   if int(sec/86400)%7==6:
     out=wed+1

   if int(sec/86400)%7==0:
     out=wed+1

   if out==1:
      print('周一')
   if out==2:
      print('周二')
   if out==3:
      print('周三')
   if out==4:
      print('周四')
   if out==5:
      print('周五')
   if out==6:
      print('周六')
   if out==7:
      print('周日')
   if out==8:
      print('周一')
   if out==9:
      print('周二')
   if out==10:
      print('周三')
   if out==11:
      print('周四')
   if out==12:
      print('周五')
   if out==13:
      print('周六')
   if out==14:
      print('周日')
   break


import io

import pytest
from django.core.management import call_command

@pytest.mark.django_db
def test_makemigrations():
    out = io.StringIO()
    call_command('makemigrations', dry_run=True, noinput=True, stdout=out)
    output = out.getvalue()
    assert output == 'No changes detected\n', (
        "`makemigrations` thinks there are schema changes without migrations")


import json
import time
import constant
import db_connect

from nba_api.stats.static import players
from nba_api.stats.endpoints import playerawards

ids = constant.PLAYERS_IDS
awardsWrapper = []
file = 'player_awards.json'
players_dict = players.get_players()

time.sleep(3)
awardsWrapper = []
active_player_iteration = 0
for (index, player) in enumerate(players_dict):
    # if active_player_iteration == 1:
    #       break
    if player['is_active'] == False:
        continue
    data = playerawards.PlayerAwards(player['id'])
    awardsData = json.loads(data.get_normalized_json())
    awards = {"id": player['id']}
    awards.update(awardsData)

    awardsWrapper.append(awards)
    print(player['full_name'] + ' awards scraped 🏀 🤖 ✏ 📄')
    active_player_iteration += 1
    time.sleep(3)

with open(file, 'w') as outfile:
    json.dump(awardsWrapper, outfile)

with open(file, 'r') as outfile:
    file_contents = json.load(outfile)


import torch
import random, os
import numpy as np
from minatar import Environment
from utils.Agent_Nets import Q_ConvNet
from tensorboardX import SummaryWriter
import argparse
from tqdm import tqdm

parser = argparse.ArgumentParser(description='MinAtar')
parser.add_argument('--id', type=str, default='DQN', help='Experiment ID')
parser.add_argument('--seed', type=int, default=1, help='Random seed')
parser.add_argument('--game', type=str, default='asterix', help='Game')
parser.add_argument('--use-cuda', type=str, default='True', help='Disable CUDA')
parser.add_argument('--Eval-episode', type=int, default=int(30), metavar='Episodes', help='Number of evaluating episode')
parser.add_argument('--T-max', type=int, default=int(5e6), metavar='STEPS', help='Number of training steps')
parser.add_argument('--dueling', type=str, default='False', help='Dueling Network Architecture')
parser.add_argument('--double', type=str, default='False', help='Double DQN')
parser.add_argument('--n-step', type=int, default=1, help='Multi-step DQN')
parser.add_argument('--distributional', type=str, default='False', help='Distributional DQN')
parser.add_argument('--noisy', type=str, default='False', help='Noisy DQN')

class Eval_Agent():
    def __init__(self, args):
        self.args = args
        self.seed = args.seed
        random.seed(self.seed)
        np.random.seed(self.seed)
        torch.manual_seed(self.seed)
        if torch.backends.cudnn.enabled:
            torch.backends.cudnn.benchmark = False
            torch.backends.cudnn.deterministic = True

        if torch.cuda.is_available() and args.use_cuda:
            args.device = torch.device('cuda')
        else:
            args.device = torch.device('cpu')

        self.save_model_dir = os.path.join('results/alg={}/env={},seed={}/saved_models'.format(args.id, args.game, args.seed))
        if not os.path.exists('results/alg={}/env={},seed={}'.format(args.id, args.game, args.seed)):
            print("cannot find {}".format('results/alg={}/env={},seed={}'.format(args.id, args.game, args.seed)))
            exit()
            os.makedirs('results/alg={}/env={},seed={}/eval_logs')
        self.writer = SummaryWriter('results/alg={}/env={},seed={}/eval_logs'.format(args.id, args.game, args.seed))

        self.eval_env = Environment(args.game)
        self.obs_dim = self.eval_env.state_shape()[2]
        self.act_dim = self.eval_env.num_actions()

        self.QValue_Net = Q_ConvNet(in_channels=self.obs_dim,
                            num_actions=self.act_dim,
                            dueling=args.dueling,
                            noisy=args.noisy,
                            distributional=args.distributional,
                            atom_size=51,
                            v_min=-10.0,
                            v_max=10.0).to(args.device)
        self.QValue_Net.eval()

    def get_state(self, s):
        return (torch.tensor(s, device=args.device).permute(2, 0, 1)).unsqueeze(0).float()

    def Eval(self):

        for step in tqdm(range(int(5e4), self.args.T_max + 1, int(5e4))):
            if not os.path.exists(self.save_model_dir + '/training_step={}_checkpoint.pth'.format(step)):
                print("cannot find model {}".format(self.save_model_dir + '/training_step={}_checkpoint.pth'.format(step)))
                exit()

            self.QValue_Net.load_state_dict(torch.load(self.save_model_dir + '/training_step={}_checkpoint.pth'.format(step)).state_dict())
            print("eval model learning step={}...\n".format(step))
            score = 0.0
            for episode in tqdm(range(self.args.Eval_episode)):
                self.eval_env.reset()
                state = self.get_state(self.eval_env.state())
                done = False
                while (not done):
                    next_state, action, reward, done = self.Eval_Interaction(state, self.eval_env)
                    score += reward.item()
                    state = next_state
            R = score / self.args.Eval_episode
            self.writer.add_scalar("Eval/RETURN/STEP", R, global_step=step)
            print("Return={}".format(R))
        self.writer.close()

    def Eval_Interaction(self, state, eval_env):
        with torch.no_grad():
            action = self.QValue_Net(state).max(1)[1].view(1, 1)
        reward, done = eval_env.act(action)
        next_state = self.get_state(eval_env.state())
        return next_state, action, torch.tensor([[reward]], device=self.args.device).float(), torch.tensor([[done]], device=self.args.device)

def Eval_after_Train(args):
    Agent = Eval_Agent(args)
    Agent.Eval()

if __name__ == '__main__':
    args = parser.parse_args()
    Agent = Eval_Agent(args)
    Agent.Eval()


#!/usr/bin/python3
import os
import sys
import numpy as np

print("!!! Use this program at your own risk.\nIt reads in multiple copies of" \
      + " a corrupt file, and tries to create a non-corrupt version by" \
      + " doing a majority vote for each byte in the file.")

print("This code runs with Python3, not Python")

# You run this program with:
#
# python3 RecoverNEF.py
#
# If you have a memory card which gives you corrupted photos when you download
# them to your computer, but the corruption changes each time you download
# the file, then this program might help you.
#
# What you need to do. First, download multiple copies of the files on your
# memory card, and store them in different directories. For example you might
# download the files seven times, and store them in directories DL1, DL2, ...
#

# Change this to the directory where you have the DL1, DL2, etc directories
basePath = "./"

# Add additional directories here if you downloaded it more times.
dirList = ["DL1","DL2","DL3","DL4","DL5","DL6"]

# This is where the rescued files will be stored
outDir = basePath + "OUT/"

# Reading the filenames in the DL1 directory
fileNames = os.listdir(basePath + dirList[0])

# Creating the OUT directory if it does not exist
if not os.path.exists(outDir):
  os.makedirs(outDir)

# This will tell you some statistics, more about it later.
nFiles = len(dirList)
voteStats = np.zeros((nFiles+1,),dtype=int)

# Change this to True if you want more information about the errors corrected
verbose = False

for fName in fileNames:

  fData = []

  voteStats[:] = 0

  print("Reading files:")
  for dName in dirList:
   fNameFull = basePath + dName + "/" + fName
   print(fNameFull)
   ff = open(fNameFull,'rb')
   data = ff.read()
   ff.close()

   fData.append(data)

  dLen = [len(x) for x in fData]
  assert min(dLen) == max(dLen), "All data not equal length " + fName
  dLen = int(np.median(dLen))

  outData = bytearray()
  totErrors = 0

  bdata = np.zeros((len(fData),),dtype=np.byte)

  print("Processing " + str(fName) + " (this is a bit slow)")

  for idx in range(0,dLen):

    if(idx % 1000000 == 0):
      sys.stdout.write('.')
      sys.stdout.flush()

    bdata[:] = 0

    for fn in range(0,len(fData)):
      import pdb
      pdb.set_trace()
      bdata[fn] = fData[fn][idx]

    if((bdata == bdata[0]).all()):
      outData.append(bdata[0])
      voteStats[nFiles] += 1
    else:

      b = max(set(bdata), key=list(bdata).count)
      outData.append(b)
      totErrors += 1

      voteCnt = np.sum(b == bdata)
      voteStats[voteCnt] += 1

      if(verbose):
        print(fName + ": Corrected byte " + str(b) + " at position " + str(idx)\
              + ", vote count " + str(voteCnt) + "/" + str(len(fData)))

  fOutName = outDir + fName

  if(os.path.isfile(fOutName)):
    print("\n" + fOutName + " already exists," \
       + " press enter to continue, Ctrl+C to abort")
    input()
    print("Overwriting file.")

  with open(fOutName,'wb') as fOut:
    fOut.write(outData)

  print("\n" + fName + " total errors found: " + str(totErrors))
  voteStr = ""
  for i in range(0,len(voteStats)):
    voteStr += str(i) + ":" + str(voteStats[i]) + "  "

  print("Vote statistics: " + str(voteStr))

  if(np.sum(voteStats[0:int(np.ceil(nFiles/2.0)+1)]) > 0):
    print("!!! Warning, voting might be wrong. Download more copies.")

  # import pdb
  # pdb.set_trace()


import numpy as np
import pandas as pd
from sklearn.datasets import load_iris

from auto_corr_feature_selection import AutoCorrFeatureSelection

# download data
iris = load_iris()
iris_df = pd.DataFrame(
    data=np.c_[iris["data"], iris["target"]], columns=iris["feature_names"] + ["target"]
)

# set up auto correlation
auto_corr = AutoCorrFeatureSelection(iris_df.drop(columns=["target"]))

# select low correlated columns
selected_columns = auto_corr.select_columns_above_threshold(threshold=0.85)
iris_filtered_df = iris_df[selected_columns]

# summary
print("Number of original columns: {}".format(len(iris_df.columns)))
print("Number of selected columns: {}\n".format(len(iris_filtered_df.columns)))

print("Selected columns: {}\n".format(selected_columns))

correlation_matrix = auto_corr.correlation_matrix()
print("Correlation matrix:")
print(correlation_matrix)


import logging
import logging.config
import yaml
from pathlib import Path

package_dir = Path(__file__).parent.absolute()
DEFAULT_LOG_PATH = package_dir.joinpath('logger_config.yml')
DEFAULT_LOG_LEVEL = logging.INFO

class LoggerFactory(object):
    """
    Reusable logger.
    """

    _LOG = None

    @staticmethod
    def __create_logger(name: str, cfg_path: Path):
        """
        A private method that interacts with the python logging module.
        """
        if cfg_path.exists():
            with open(cfg_path, 'rt') as cfg_file:
                try:
                    config = yaml.safe_load(cfg_file.read())
                    logging.config.dictConfig(config)
                except yaml.YAMLError as exc:
                    logging.basicConfig(level=DEFAULT_LOG_LEVEL)
                    logging.warning("YAMLError happened: %s", exc)

        LoggerFactory._LOG = logging.getLogger(name)

        return LoggerFactory._LOG

    @staticmethod
    def get_logger(
            name: str,
            cfg_path=DEFAULT_LOG_PATH,
    ):
        """
        A static method called by other modules to
        initialize logger in their own module.

        Args:
            name (str): Name of the logger.
            cfg_path (Path, Optional): :obj:`Path` of the yaml
            config file for logging
        """
        logger = LoggerFactory.__create_logger(name, cfg_path)

        # return the logger object
        return logger


#pip install matplotlib
#pip install scipy
#sudo apt-get install python3-pil python3-pil.imagetk

import sys
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

# label names
title = "Comparison of the Execution Time of some algorithms\nfor solving Knapsack Problems."
xLabel = "Tested Data"
yLabel = "Execution Time (seconds)"
personalAlgo = "'Personal Algorithm'"

# create and show the graph
def graph(time, tests):

    # create a curve for each tested algorithm

    if len(time["brute_force"]) != 0 :
        plt.plot(tests, time["brute_force"], label='Brute force', marker = '.', color = 'royalblue')

    if len(time["branch_and_bound"]) != 0 :
        plt.plot(tests, time["branch_and_bound"], label='Banch and bound', marker = '.', color = 'orange')

    if len(time["greedy"]) != 0 :
        plt.plot(tests, time["greedy"], label = 'Greedy', marker = '.', color = 'limegreen')

    if len(time["greedy_value"]) != 0 :
        plt.plot(tests, time["greedy_value"], label = 'Greedy Value', marker = '.', color = 'yellowgreen')

    if len(time["greedy_weight"]) != 0 :
        plt.plot(tests, time["greedy_weight"], label = 'Greedy Weight', marker = '.', color = 'seagreen')

    if len(time["dynamic_programming"]) != 0 :
        plt.plot(tests, time["dynamic_programming"], label = 'Dynamic', marker = '.', color = 'r')

    if len(time["polynomial"]) != 0 :
        plt.plot(tests, time["polynomial"], label = 'Polynomial', marker = '.', color = 'm')

    if len(time["grasp"]) != 0 :
        plt.plot(tests, time["grasp"], label = 'Grasp', marker = '.', color = 'skyblue')

    if len(time["ant_colony"]) != 0 :
        plt.plot(tests, time["ant_colony"], label = 'Ant colony', marker = '.', color = 'hotpink')

    if len(time["personal"]) != 0 :
        plt.plot(tests, time["personal"], label = personalAlgo, marker = '.', color = 'tan')

    # create labels
    plt.grid()
    plt.ylabel(yLabel)
    plt.xlabel(xLabel)
    plt.title(title, fontname = "Liberation Serif", fontsize = 15, fontweight = "bold", y = 1.01)
    plt.legend()

    # show graph
    plt.show()


#!/usr/bin/env -S pipenv run python
from collections import defaultdict

import bs4
from glob import iglob
import json
from pathlib import Path
import requests

if __name__ == '__main__':
    authorToMaterials = defaultdict(set)

    for path in iglob('./journal/**/short.json', recursive=True):
        content = Path(path).read_text()
        article = json.loads(content, parse_constant=True)

        for a in article['authors']:
            authorToMaterials[a['href']].add(article['url'])

    authors = {}

    for url, setOfArticles in authorToMaterials.items():
        print(url)
        host = 'https://cinematograph.media'
        content = requests.get(host + url).text
        root = bs4.BeautifulSoup(content, 'html.parser')

        authors[url] = {
            'url':       str(url),
            'name':      root.select_one('.author-bio__name').get_text().strip(),
            'status':    root.select_one('.author-bio__status').get_text().strip(),
            'bio':       root.select_one('.author-bio__description').get_text().strip(),
            'links':     [l.attrs['href'] for l in root.select('.author-bio__link')],
            'materials': list(setOfArticles),
        }

    content = json.dumps(authors, ensure_ascii=False, indent=2)
    Path('journal/authors.json').write_text(content)


from collections import deque

N, M = map(int, input().split())
dy, dx = [0, 0, 1, -1], [1, -1, 0, 0]
matrix = [[*input()] for _ in range(N)]

def bfs(i, j):
    visited = [[False] * M for _ in range(N)]
    queue = deque()
    queue.append((i, j, 0))
    visited[i][j] = True
    res = 0
    while queue:
        y, x, cnt = queue.popleft()
        res = max(res, cnt)
        for k in range(4):
            ny, nx = y + dy[k], x + dx[k]
            if 0 <= ny < N and 0 <= nx < M and not visited[ny][nx] and matrix[ny][nx] == 'L':
                visited[ny][nx] = True
                queue.append((ny, nx, cnt + 1))
    return res

answer = 0
for i in range(N):
    for j in range(M):
        if matrix[i][j] == 'L':
            answer = max(answer, bfs(i, j))

print(answer)


import logging
import logging.config
import yaml

def init_dict_config():
    with open("logger_setup.yaml", "r") as file:
        data = yaml.load(file, Loader=yaml.FullLoader)

    logging.config.dictConfig(data)

def init_logs_with_python():
    """Initialize the logger that will be used throughout the application.
    """
    # Create logger
    root_logger = logging.getLogger()
    root_logger.setLevel(logging.NOTSET)

    # Formatter for all levels
    # Ref: https://stackoverflow.com/a/13733863/6323360
    formatter = logging.Formatter(
        fmt="[%(asctime)s] [%(levelname)-8s] %(message)s [%(filename)s] [%(threadName)s]",
        datefmt="%Y-%m-%d %H:%M:%S"
    )

    # Create file handler for all levels
    file_handler_all_levels = logging.FileHandler(
        "{}/{}.log".format("logs", "all_levels"))
    file_handler_all_levels.setLevel(logging.DEBUG)
    file_handler_all_levels.setFormatter(formatter)
    root_logger.addHandler(file_handler_all_levels)

    # Create file handler for errors or higher
    file_handler_errors = logging.FileHandler(
        "{}/{}.log".format("logs", "errors"))
    file_handler_errors.setLevel(logging.ERROR)
    file_handler_errors.setFormatter(formatter)
    root_logger.addHandler(file_handler_errors)

    # Console for DEBUG or higher
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)
    console_handler.setFormatter(formatter)
    root_logger.addHandler(console_handler)


import warnings
from typing import Union

import numpy as np
import pandas as pd

def make_shifted_target(
    y: pd.Series,
    use_diff_of_y: bool = False,
    lags: Union[int, list] = 1,
    newcol_prefix: str = None,
):
    """
    Creates a target dataframe by performing shifting or differencing (once or multiple times)
    on a monospaced series. The dataframe contains columns 'TARGET_lead_x', where `x` are the
    lags and TARGET is the name of the input series.

    Parameters
    ----------
    y: pd.Series
        A series containing the target data. Must be monospaced in time, for the shifting
        to work correctly.
    use_diff_of_y: boolean, optional (default=False)
        If true, the difference between the target now and the shifted target is used,
        else just uses the shifted target itself.
    lags: array-like or int, optional (default=1)
        A list of integers, or a single integer describing what lags should be used to look in the
        future. For example, if this is `[1, 2, 3]`, the output will have three columns, performing
        shifting on 1, 2, and 3 timesteps in the future.
        If this is a list, the output will be a dataframe. If this is a scalar, the output will
        be a pd.Series
    newcol_prefix: str, optional (default=None)
        The prefix that the output columns will have. If None, `y.name` is used instead.

    Returns
    -------
    target: pd.DataFrame or pd.Series
        A target with the same index as `y`, and columns equal to `len(lags)`
        The values will be 'future values' of `y`.
        If we consider the index to be the 'timestamp', then the index will be the moment the
        prediction is made, not the moment the prediction is about. Therefore, the columns
        will be the future values with different lags.
        Any values that cannot be calculated (because there is no available future value) will be
        set to `np.nan`.

    Examples
    --------
    >>> df = pd.DataFrame({
    ...     'X': [18, 19, 20, 21],
    ...     'y': [10, 20, 50, 100]
    ... })
    >>> make_shifted_target(df['y'], use_diff_of_y = False, lags=1)
    0     20.0
    1     50.0
    2    100.0
    3      NaN
    Name: y_lead_1, dtype: float64

    --------
    >>> df = pd.DataFrame({
    ...     'X': [18, 19, 20, 21],
    ...     'y': [10, 20, 50, 100]
    ... })
    >>> make_shifted_target(df['y'], use_diff_of_y = True, lags=1)
    0    10.0
    1    30.0
    2    50.0
    3     NaN
    Name: y_diff_1, dtype: float64
    """

    if newcol_prefix is None:
        newcol_prefix = y.name

    if np.isscalar(lags):
        series_output = True
        lags = [lags]
    else:
        series_output = False

    for lag in lags:
        if lag < 0:
            raise ValueError("All lags must be greater or equal to 0")
        if lag % 1 != 0:
            raise ValueError("All lags must be integers")

    # Lagging to the future means negative lags
    if use_diff_of_y:
        result = pd.concat([-1 * y.diff(-1 * lag) for lag in lags], axis=1)
        names = ["{}_diff_{}".format(newcol_prefix, lag) for lag in lags]
    else:
        result = pd.concat([y.shift(-1 * lag) for lag in lags], axis=1)
        names = ["{}_lead_{}".format(newcol_prefix, lag) for lag in lags]

    result.columns = names
    if series_output:
        result = result.iloc[:, 0]

    return result

def make_differenced_target(
    y: pd.Series,
    lags: Union[int, list] = 1,
    newcol_prefix: str = None,
):
    """
    Creates a target dataframe by performing differencing (once or multiple times)
    on a monospaced series. The dataframe contains columns 'TARGET_lead_x', where `x` are the
    lags and TARGET is the name of the input series.

    Parameters
    ----------
    y: pd.Series
        A series containing the target data. Must be monospaced in time, for the differencing
        to work correctly.
    lags: array-like or int, optional (default=1)
        A list of integers, or a single integer describing what lags should be used to look in the
        future. For example, if this is `[1, 2, 3]`, the output will have three columns, performing
        differencing on 1, 2, and 3 timesteps in the future.
        If this is a list, the output will be a dataframe. If this is a scalar, the output will
        be a pd.Series
    newcol_prefix: str, optional (default=None)
        The prefix that the output columns will have. If None, `y.name` is used instead.

    Returns
    -------
    target: pd.DataFrame or pd.Series
        A target with the same index as `y`, and columns equal to `len(lags)`
        The values will be 'future values' of `y` but differenced.
        If we consider the index to be the 'timestamp', then the index will be the moment the
        prediction is made, not the moment the prediction is about. Therefore, the columns
        will be different future values with different lags.
        Any values that cannot be calculated (because there is no available future value) will be
        set to `np.nan`.

    Examples
    --------
    >>> df = pd.DataFrame({
    ...     'X': [18, 19, 20, 21],
    ...     'y': [10, 20, 50, 100]
    ... })
    >>> make_differenced_target(df['y'], lags=1)
    0    10.0
    1    30.0
    2    50.0
    3     NaN
    Name: y_diff_1, dtype: float64
    """
    warnings.warn(
        "make_differenced_target will be deprecated and replaced by make_shifted_target",
        PendingDeprecationWarning,
    )

    result = make_shifted_target(y=y, use_diff_of_y=True, lags=lags, newcol_prefix=newcol_prefix)

    return result

def inverse_differenced_target(predictions: pd.DataFrame, y: pd.Series):
    """
    Inverses differencing by adding the current values to the prediction.

    This function will take differenced target(s) and the current values, and return the actual
    target(s). Can be used to convert predictions from a differenced model to real predictions.

    predictions and y must be joined on index. Any indexes that only appear in predictions, or
    only appear in y, will also appear in the output, with nans inserted.

    Parameters
    ----------
    predictions: pd.DataFrame
        Dataframe containing differenced values.
    y: pd.Series
        The actual values in the present

    Returns
    -------
    actual: pd.DataFrame
        Dataframe containing un-differenced values, created by adding predictions to `y` on index.
        The index of this output will be the union of the indexes of predictions and `y`.
        The columns refer to the values/predictions made at a single point in time.
        For example, if the index is '18:00', and the predictions are made on differencing 1 hour,
        2 hour and 3 hours, then one row will contain the predictions made at 18:00,
        predicting what the target will be at 19:00, 20:00 and 21:00

    Examples
    --------
    >>> df = pd.DataFrame({
    ...     'X': [18, 19, 20, 21],
    ...     'y': [10, 20, 50, 100]
    ... })
    >>> target = make_differenced_target(df['y'], lags=1)
    >>> inverse_differenced_target(target, df['y'])
    0     20.0
    1     50.0
    2    100.0
    3      NaN
    Name: y_diff_1, dtype: float64

    >>> prediction = pd.DataFrame({
    ...    'pred_diff_1': [15, 25, 34, np.nan],
    ...    'pred_diff_2': [40, 55, np.nan, np.nan]
    ... })
    >>> inverse_differenced_target(prediction, df['y'])
       pred_diff_1  pred_diff_2
    0         25.0         50.0
    1         45.0         75.0
    2         84.0          NaN
    3          NaN          NaN
    >>> # This means that at timestep 0, we predict that the next two values will be 25 and 60
    >>> # At timestep 1, we predict the next two values will be 45 and 105
    >>> # At timestep 2, we predict the next two values will be 84 and unknown, etcetera.
    """
    result = predictions.add(y, axis=0)
    # Series lose their name on pd.Series.add, so put the name back
    if isinstance(result, pd.Series):
        result = result.rename(predictions.name)
    return result


import os, errno, shutil, pwd

while True:
    project = input('Enter the name of project (project.local): ')
    if (project != ""):
        break
docroot = input('Enter root folder (/var/www/html): ')

if (docroot == ""):
    docroot = "/var/www/html"

virtualhost="""
<VirtualHost *:80>
    ServerAdmin admin@example.com
    DocumentRoot """ +docroot+ """/""" +project+ """
    ServerName """ +project+ """
    ErrorLog logs/""" +project+ """-error_log
    CustomLog logs/""" +project+ """-access_log common
</VirtualHost>"""

def existsDirectory(directory):
    response = False
    if os.path.exists(directory):
        response = True
    return response

def createDirectory(directory):
    try:
        os.makedirs(directory)
        os.chmod(directory, 0o775)
        uid =  pwd.getpwnam('root').pw_uid
        gid = pwd.getpwnam('www-data').pw_uid
        os.chown(directory, uid, gid) # set user:group as root:www-data
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

def createVirtualFile(project):
    try:
        f = open('/etc/apache2/sites-available/'+project+'.conf', 'w')
        f.write(virtualhost)
        f.close()
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

def enableVirtualHost(project):
    status = os.system('sudo a2ensite '+project)
    print(status)

def updateHostsFile(project):
    with open("/etc/hosts","a") as f:
        f.write('127.0.0.1 '+ project + '\n')

def restartApache():
    status = os.system('sudo service apache2 restart')
    print(status)

if existsDirectory(docroot+ "/" +project):
    print('El directorio existe')

    while True:
        choice = input('Remove directory and create new [y/n]: ')
        if (choice == 'y' or choice == 'Y') or (choice == 'n' or choice == 'N'):
            break

    if (choice == 'y' or choice == 'Y'):
        shutil.rmtree(docroot+ "/" +project)
        createDirectory(docroot+ "/" +project)
        createVirtualFile(project)
        enableVirtualHost(project)
        updateHostsFile(project)
        restartApache()
    else:
        pass
else:
    print('El directorio NO existe')
    createDirectory(docroot+ "/" +project)
    createVirtualFile(project)
    enableVirtualHost(project)
    updateHostsFile(project)
    restartApache()


from pathlib import Path
import pandas as pd

# Load in the acousticbrainz dataset into the variable 'acousticbrainz'
acousticbrainz = pd.read_hdf(Path.cwd().parents[0] / 'datasets' / 'acousticbrainzV3.h5')

# Metadata to consider
variables = ['analysis_sample_rate', 'bit_rate', 'codec', 'downmix', 'equal_loudness', 'length', 'lossless',
			'replay_gain', 'essentia_high', 'extractor_high', 'gaia_high', 'essentia_low', 'essentia_git_sha_low',
			'essentia_build_sha_low', 'extractor_low']

## Define the spikes
# Spike in acousticness at 0.09 - 0.10
acoust_spike = acousticbrainz[acousticbrainz[('mood_acoustic', 'acoustic')].between(0.09, 0.10, inclusive=True)]

# Spike in mood relaxed at 0.805 - 0.815
relaxed_spike = acousticbrainz[acousticbrainz[('mood_relaxed', 'relaxed')].between(0.805, 0.815, inclusive=True)]

# Spike in mood electronic at 0.972 - 0.982
electronic_spike = acousticbrainz[acousticbrainz[('mood_electronic', 'electronic')].between(0.972, 0.982, inclusive=True)]

# Small spike in sad at 0.346 - 0.362
sad_spike = acousticbrainz[acousticbrainz[('mood_sad', 'sad')].between(0.346, 0.362, inclusive=True)]

# Baseline contains all submission not in either of the three spikes
spike_ids = set(acoust_spike.index).union(relaxed_spike.index, electronic_spike.index, sad_spike.index)
nonspike_ids = set(acousticbrainz.index).difference(spike_ids)
baseline = acousticbrainz.loc[list(nonspike_ids)]

## Write datasets
spike_names = ['acoust_spike', 'relaxed_spike', 'electronic_spike', 'sad_spike']
spikes = [acoust_spike, relaxed_spike, electronic_spike, sad_spike]
baseline['label'] = 'non-anomaly'

for i in range(0, len(spikes)):
	print(f"Labeling data for {spike_names[i]}...")
	spike_data = spikes[i][variables].copy(deep=True)
	spike_data['label'] = 'anomaly'

	out = spike_data.append(baseline[variables + ['label']])
	out = out.reset_index(drop=True)
	out.to_csv(spike_names[i] + '.csv', index=False)


import numpy as np
from scipy import ndimage
import torch
import torch.nn as nn
import torch.nn.functional as F

def threshold_mask(data, threshold_value=0.2, radius=50):
    threshold = (data.max() - data.min()) * threshold_value
    mask = np.ones(data.shape)
    W = len(mask)
    H = len(mask[0])
    mask[mask * data < threshold] = 0
    mask = ndimage.filters.maximum_filter(mask, radius)
    return mask

def threshold_temp(data, threshold_value: int = 800):
    mask = np.ones(data.shape)
    mask[data < threshold_value] = 0
    return mask

def std_GS(data, std=True):
    # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')  # device = cuda会引起多线程的报错
    if std:
        try:
            data = torch.from_numpy(data).to(device='cpu', dtype=torch.float32).reshape(-1, 1, 789, 113)
        except:
            data = data.to(device='cpu', dtype=torch.float32).reshape(-1, 1, 789, 113)
        InstanceNorm = nn.InstanceNorm2d(1)
        data_std = InstanceNorm(data).cpu().numpy()
        return ndimage.filters.gaussian_filter(data_std.reshape([789, 113, 1]), sigma=20)
    else:
        try:
            data = data.cpu().numpy()
        except:
            pass
        return ndimage.filters.gaussian_filter(data.reshape([789, 113, 1]), sigma=20)

class RelativeL2Error(nn.Module):
    """
    input: tensor, output: single tensor value
    if plot=Ture, then output: TENSOR (default plot=Faulse)
    f: regressed value, g: reference value
    Calculate relative l2 error between result and ground truth, see HFM paper
    """

    def __init__(self):
        super().__init__()

    def forward(self, f, g, reduct='mean'):
        return F.mse_loss(f, g, reduction=reduct) / F.mse_loss(g, torch.mean(g))

class NormMSELoss(nn.Module):
    """
    Calculate MSE between normalized result and ground truth
    """

    def __init__(self):
        super().__init__()

    def forward(self, f, g, reduct='mean'):
        InstanceNorm = nn.InstanceNorm2d(1)
        return F.mse_loss(InstanceNorm(f), InstanceNorm(g), reduction=reduct)

def temp_recover(temp_ori, temp_pred):
    """
    Recover temperature(as np array) from standard deviation and mean.
    """
    try:
        temp_ori = temp_ori.cpu().numpy()
        temp_pred = temp_pred.cpu().numpy()
    except:
        pass
    temp_ori = temp_ori.reshape(-1, 1, 789, 113)
    temp_pred = temp_pred.reshape(-1, 1, 789, 113)

    std_ori = np.std(temp_ori, axis=(2, 3))
    mean_ori = np.mean(temp_ori, axis=(2, 3))

    temp_real = temp_pred * std_ori + mean_ori

    return temp_real

def cal_PSNR(mse, img_true):
    if mse == 0:
        return 100
    return 10 * np.log10(img_true.max() ** 2 / mse)

def cal_SSIM(img_pred, img_true):
    assert img_pred.shape == img_true.shape
    mu1 = img_pred.mean()
    mu2 = img_true.mean()
    sigma1 = np.std(img_pred)
    sigma2 = np.std(img_true)
    sigma12 = ((img_pred - mu1) * (img_true - mu2)).mean()
    k1, k2 = 0.01, 0.03
    L = img_true.max()
    C1 = (k1 * L) ** 2
    C2 = (k2 * L) ** 2
    C3 = C2 / 2
    l12 = (2 * mu1 * mu2 + C1) / (mu1 ** 2 + mu2 ** 2 + C1)
    c12 = (2 * sigma1 * sigma2 + C2) / (sigma1 ** 2 + sigma2 ** 2 + C2)
    s12 = (sigma12 + C3) / (sigma1 * sigma2 + C3)
    ssim = l12 * c12 * s12
    return ssim


import datetime
from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.simple_tag
def current_time(format_string):
    return datetime.datetime.now().strftime(format_string)

@register.simple_tag

def store_list(items):
    itarray = []
    for items in itarray:
        if items not in itarray:
            itarray.append(items)
            return itarray

def main():
    global itarray


import json
import pytest
from model_bakery import baker
from django.urls import reverse
from deliveries.models import ProductChoices

pytestmark = pytest.mark.django_db

class TestProductChoicesEndpoints:

    endpoint = reverse('products-list')

    def test_list(self, api_client):
        baker.make(ProductChoices, _quantity=3)

        response = api_client().get(self.endpoint)
        assert response.status_code == 200
        assert len(json.loads(response.content)) == 3

    def test_create(self, api_client):
        product = baker.prepare(ProductChoices)

        input_json = {
            'product': product.product
        }
        expected_json = {
            'product': product.product
        }
        response = api_client().post(
            self.endpoint,
            data=input_json,
            format='json'
        )
        assert response.status_code == 201
        expected_json['id'] = response.data['id']
        assert json.loads(response.content) == expected_json

    def test_invalid_create(self, api_client):
        baker.prepare(ProductChoices)

        input_json = {
            'product': ''
        }
        response = api_client().post(
            self.endpoint,
            data=input_json,
            format='json'
        )
        assert response.status_code == 400

    def test_retrieve(self, api_client):
        product = baker.make(ProductChoices)
        expected_json = {
            'id' : product.id,
            'product' : product.product
        }
        url = f'{self.endpoint}{product.id}/'
        response = api_client().get(url)
        assert response.status_code == 200
        assert json.loads(response.content) == expected_json

    def test_update(self, api_client):
        old_product = baker.make(ProductChoices)
        new_product = baker.prepare(ProductChoices)
        product_dict = {
            'id' : old_product.id,
            'product' : new_product.product
        }

        url = f'{self.endpoint}{old_product.id}/'
        response = api_client().put(url, product_dict, format='json')
        assert response.status_code == 200
        assert json.loads(response.content) == product_dict

    def test_delete(self, api_client):
        product = baker.make(ProductChoices)
        url = f'{self.endpoint}{product.id}/'
        response = api_client().delete(url)
        assert response.status_code == 204
        assert ProductChoices.objects.all().count() == 0


'''
Product views
'''
from datetime import datetime

from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db.models import Avg

from product.forms import ReviewForm, ProductForm

from product.models import Product, Review
from category.models import Category

# Views
def product_detail(request, category_handle, product_handle):
    '''
    Shows product page
    '''
    get_object_or_404(Category, category_handle=category_handle)
    product = get_object_or_404(Product, product_handle=product_handle)
    reviews = Review.objects.filter(product=product)
    rating = Review.objects.filter(product=product).aggregate(Avg('rating'))

    template = 'product/product_detail.html'
    context = {
        'product': product,
        'reviews': reviews,
        'rating': rating['rating__avg'],
    }
    return render(request, template, context)

@login_required
def add_product(request):
    """
    Add a product
    """
    if not request.user.is_staff:
        messages.error(request, 'You are not allowed to do that')
        return redirect('home')

    if request.method == 'POST':
        form = ProductForm(request.POST, request.FILES)
        if form.is_valid():
            product = form.save()
            messages.success(request, 'Product has been added')
            return redirect(reverse(
                'product_detail',
                kwargs={
                    'category_handle': product.category.category_handle,
                    'product_handle': product.product_handle
                }))

        messages.error(
            request,
            'Invalid form data'
        )
    else:
        form = ProductForm()

    template = 'product/add_product.html'
    context = {
        'form': form,
    }

    return render(request, template, context)

@login_required
def edit_product(request, product_id):
    """
    Edit a product
    """
    if not request.user.is_staff:
        messages.error(request, 'You are not allowed to do that')
        return redirect('home')

    product = get_object_or_404(Product, pk=product_id)
    if request.method == 'POST':
        form = ProductForm(request.POST, request.FILES, instance=product)
        if form.is_valid():
            form.save()
            messages.success(
                request,
                'Product has been updated'
            )
            return redirect(reverse(
                'product_detail',
                kwargs={
                    'category_handle': product.category.category_handle,
                    'product_handle': product.product_handle
                }))

        messages.error(
            request,
            'Invalid product information'
        )
    else:
        form = ProductForm(instance=product)
        messages.info(request, f'You are editing {product.product_name}')

    template = 'product/edit_product.html'
    context = {
        'form': form,
        'product': product,
    }

    return render(request, template, context)

@login_required
def delete_product(request, product_id):
    '''
    Delete product
    '''
    # Query for the post
    if request.user.is_staff:
        product = get_object_or_404(Product, pk=product_id)
        category_handle = product.category.category_handle

        product.delete()

        messages.success(request, 'Product has been deleted')
        return redirect(
            'category_detail',
            handle=category_handle
        )

    messages.error(request, 'You are not allowed to do that')
    return redirect('home')

@login_required
def add_review(request, product_id):
    '''
    Adds review to product
    '''
    # Query for the product
    product = get_object_or_404(Product, pk=product_id)

    # Add a new review
    if not Review.objects.filter(user=request.user, product=product).exists():
        if request.method == 'POST':
            form = ReviewForm(request.POST)

            if form.is_valid():

                Review.objects.create(
                    user=request.user,
                    product=product,
                    content=form.cleaned_data['content'],
                    rating=form.cleaned_data['rating'],
                )
                messages.success(request, 'Thank you for your review')
                return redirect(reverse(
                    'product_detail',
                    kwargs={
                        'category_handle': product.category.category_handle,
                        'product_handle': product.product_handle
                    })
                )

            messages.error(request, 'Invalid form')
        form = ReviewForm()
        return render(
                request,
                'product/add_review.html',
                {
                    'product': product,
                    'form': form
                }
            )

    messages.error(request, 'You already reviewed this product')
    return redirect(
        'product_detail',
        category_handle=product.category.category_handle,
        product_handle=product.product_handle
    )

@login_required
def edit_review(request, review_id):
    '''
    Edit review route
    '''
    # Get review
    review = get_object_or_404(Review, pk=review_id)

    if request.user == review.user:
        if request.method == 'POST':
            form = ReviewForm(request.POST)
            if form.is_valid():

                review.rating = form.cleaned_data['rating']
                review.content = form.cleaned_data['content']
                review.modified_at = datetime.now()
                review.save()
                messages.success(request, "Edit successful")
                return redirect(
                    'product_detail',
                    category_handle=review.product.category.category_handle,
                    product_handle=review.product.product_handle
                )

            messages.error(request, "Invalid input")

        else:
            form = ReviewForm(instance=review)
            messages.info(
                request,
                f'You are editing your review for {review.product.product_name}'
            )

        return render(
            request,
            'product/edit_review.html',
            {
                'review': review,
                'form': form
            }
        )

    # User is not logged in and not allowed to edit it
    messages.error(
        request,
        'Really? No you cannot edit this review, this is not your review!'
    )
    return render(
        request,
        'product/product_detail.html',
        {'product_detail': review.product}
    )

@login_required
def delete_review(request, review_id):
    '''
    Delete review
    '''
    # Get review
    review = get_object_or_404(Review, pk=review_id)

    if request.user == review.user:

        # Product for redirect later
        product = review.product

        review.delete()

        messages.success(request, 'Review has been deleted')
        return redirect(
            'product_detail',
            category_handle=product.category.category_handle,
            product_handle=product.product_handle
        )

    messages.error(request, 'This is not your product')
    return redirect('home')


from django.shortcuts import render, redirect

from gamesPlayApp.game.forms import CreateProfileForm, CreateGameForm, EditGameForm, DeleteGameForm, EditProfileForm, \
    DeleteProfileForm
from gamesPlayApp.game.models import Profile, Game

def get_user_profile():
    return Profile.objects.first()

def show_index(request):
    user_profile = get_user_profile()
    context = {'user_profile': user_profile}
    return render(request, 'home-page.html', context)

def create_profile(request):
    form = CreateProfileForm()
    if request.method == 'POST':
        form = CreateProfileForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('home')

    context = {'form': form}
    return render(request, 'create-profile.html', context)

def show_dashboard(request):
    user_profile = get_user_profile()
    games = Game.objects.all()
    context = {'games': games, 'user_profile': user_profile}
    return render(request, 'dashboard.html', context)

def create_game(request):
    user_profile = get_user_profile()
    form = CreateGameForm()
    if request.method == 'POST':
        form = CreateGameForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('show dashboard')
    context = {'form': form, 'user_profile': user_profile}
    return render(request, 'create-game.html', context)

def show_game_details(request, pk):
    user_profile = get_user_profile()
    game = Game.objects.get(pk=pk)

    context = {'game': game, 'user_profile': user_profile}
    return render(request, 'details-game.html', context)

def edit_game(request, pk):
    user_profile = get_user_profile()
    game = Game.objects.get(pk=pk)
    form = EditGameForm(instance=game)
    if request.method == 'POST':
        form = CreateGameForm(request.POST, instance=game)
        if form.is_valid():
            form.save()
            return redirect('show dashboard')
    context = {'form': form, 'user_profile': user_profile}
    return render(request, 'edit-game.html', context)

def delete_game(request, pk):
    user_profile = get_user_profile()
    game = Game.objects.get(pk=pk)
    form = DeleteGameForm(instance=game)
    if request.method == 'POST':
        form = DeleteGameForm(request.POST, instance=game)
        if form.is_valid():
            form.save()
            return redirect('show dashboard')
    context = {'form': form, 'user_profile': user_profile}
    return render(request, 'delete-game.html', context)

def show_profile_details(request):
    user_profile = get_user_profile()
    games = Game.objects.all()
    if user_profile.first_name and user_profile.last_name:
        display_name = f'{user_profile.first_name} {user_profile.last_name}'
    elif user_profile.first_name:
        display_name = user_profile.first_name
    else:
        display_name = user_profile.last_name
    try:
        average_games_rating = sum([g.rating for g in games]) / games.count()
    except ZeroDivisionError:
        average_games_rating = 0

    context = {'user_profile': user_profile, 'display_name': display_name, 'games': games,
               'average_games_rating': average_games_rating}
    return render(request, 'details-profile.html', context)

def edit_profile(request):
    user_profile = get_user_profile()
    form = EditProfileForm(instance=user_profile)
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=user_profile)
        if form.is_valid():
            form.save()
            return redirect('profile details')
    context = {'form': form, 'user_profile': user_profile}
    return render(request, 'edit-profile.html', context)

def delete_profile(request):
    user_profile = get_user_profile()
    games = Game.objects.all()
    form = DeleteProfileForm(instance=user_profile)

    if request.method == 'POST':
        form = DeleteProfileForm(request.POST, instance=user_profile)
        if form.is_valid():
            games.delete()
            form.save()
            return redirect('home')

    context = {'form': form, 'user_profile': user_profile}
    return render(request, 'delete-profile.html', context)


import numpy as np
import ast
import re
import pickle
import json
from nltk.stem import PorterStemmer
from pymongo import MongoClient
client = MongoClient("localhost", 27017)
products = client["fashion_products"]["products"]
print(products.count())

with open('vocab_types.json') as f:
    vocab = f.readlines()
    vocab = ast.literal_eval(vocab[0])

def extract_keywords(editor_note):
    keyword_pairs = []
    has_type = False
    for i, c in enumerate(editor_note.split()):
        if c.lower() in vocab.keys():
            for cat in vocab[c.lower()]:
                if cat == 'type':
                    has_type = True
                keyword_pairs.append((cat, c.lower()))
    if has_type:
        return keyword_pairs
    else:
        return None

res = []
for p in products.find():
    des = p['descriptions'][0].lower().replace("\n", "")
    for sent in des.split("."):
        sent = sent.strip()
        if "this" in sent or "these" in sent:
            if 15 < len(sent.split()) < 20:
                res.append(sent)
res = list(set(res))

#replace frequency < 10 words with UNK
word_map = {}
for r in res:
    for w in r.split():
        if w not in word_map:
            word_map[w] = 0
        word_map[w] += 1

for i in range(len(res)):
    orig = res[i].split()
    new = ['UNK' if word_map[word] <= 10 else word for word in orig]
    curr_i = 1
    while True:
        if curr_i < len(new) and new[curr_i] == new[curr_i-1] and new[curr_i] == 'UNK':
            del new[curr_i]
            continue
        curr_i += 1
        if curr_i >= len(new):
            break
    res[i] = ' '.join(new)

final_res = []
for r in res:
    keyword_pairs = extract_keywords(r)
    if keyword_pairs and len(keyword_pairs) > 2:
        final_res.append((keyword_pairs, r))
print("total:", len(final_res))

train_threshold = int(len(final_res) * 0.95)

with open("train_dataset.p", "wb") as pickle_d:
    pickle.dump(final_res[:train_threshold], pickle_d)

with open('train_dataset.txt', 'w+') as f:
    for r in final_res[:train_threshold]:
        f.write(str(r[1]) + '\n')

with open("test_dataset.p", "wb") as pickle_d:
    pickle.dump(final_res[train_threshold:], pickle_d)

with open('test_dataset.txt', 'w+') as f:
    for r in final_res[train_threshold:]:
        f.write(str(r[1]) + '\n')

with open("total_dataset.p", "wb") as pickle_d:
    pickle.dump(final_res, pickle_d)