import tflearn
import tflearn.datasets.mnist as mnist
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression

## mnist dataset - 55000 samples
X, Y, test_x, test_y = mnist.load_data(one_hot=True)

# conv_2d
# INPUT  4-D Tensor [batch, height, width, in_channels = columns].
# Output 4-D Tensor [batch, new height, new width, nb_filter].

X = X.reshape([55000, 28, 28, 1])

test_x = test_x.reshape([10000, 28, 28, 1])

CNN = input_data(shape=[None, 28, 28, 1], name='input')

#         incoming, #nb_filter, #Size of filters
CNN = conv_2d(CNN, 32, 2, activation='relu')
#            incoming, #nb_filter, #kernel_size
CNN = max_pool_2d(CNN, 2)

CNN = conv_2d(CNN, 64, 2, activation='relu')
CNN = max_pool_2d(CNN, 2)

#                       incoming,  n_units
CNN = fully_connected(CNN, 1024, activation='relu')
CNN = dropout(CNN, 0.80)

CNN = fully_connected(CNN, 10, activation='softmax')
CNN = regression(CNN, optimizer='adam', learning_rate=0.001, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(CNN)
model.fit({'input': X}, {'targets': Y}, n_epoch=10, validation_set=({'input': test_x}, {'labels': test_y}),
	snapshot_step=750, show_metric=True, run_id='mnist')

#model.save('CNN.model')

#model.load('CNN.model')


import csv
import time
# needs "pip install xmltodict"
import xmltodict
import requests
import secret

def find_dict(res, search):
    for key, value in res.items():
        if search == key:
            return {key: value}

def response_maker(res):
    out = {}
    out.update(find_dict(res["SeoulRtd.citydata"]["CITYDATA"], "AREA_NM"))
    out.update(res["SeoulRtd.citydata"]["CITYDATA"]["LIVE_PPLTN_STTS"]["LIVE_PPLTN_STTS"])
    out.pop("AREA_CONGEST_MSG", None)
    # out.update(res["SeoulRtd.citydata"]["CITYDATA"]["ROAD_TRAFFIC_STTS"]["AVG_ROAD_DATA"])

    tmp = "\'" + out["PPLTN_TIME"] + "\'"
    print("tmp:", tmp)
    out.update({"PPLTN_TIME": tmp})

    # try:
    #     accident_status = res["SeoulRtd.citydata"]["CITYDATA"]["ACDNT_CNTRL_STTS"]["ACDNT_CNTRL_STTS"]
    # except TypeError:
    #     out.update({"ACDNT_CNTRL_CNT": 0})
    # else:
    #     # # If you need the accident information, use this code (but this is not good for statistics)
    #     # if type(accident_status) is list:
    #     #     out.update({"ACDNT_CNTRL_CNT": len(accident_status)})
    #     #     for i in range(len(accident_status)):
    #     #         tmp = dict(("ACDNT" + str(i) + "_" + key, value) for (key, value) in accident_status[i].items())
    #     #         out.update(tmp)
    #     # else:
    #     #     out.update(accident_status)
    #     out.update({"ACDNT_CNTRL_CNT": len(accident_status)})

    # weather_status = dict_response["SeoulRtd.citydata"]["CITYDATA"]["WEATHER_STTS"]["WEATHER_STTS"]
    # weather_status.pop("FCST24HOURS", None)
    # out.update(weather_status)
    return out

if __name__ == "__main__":
    print(("-" * 20) + "Program Start" + ("-" * 20))
    start = time.time()

    url_base = "http://openapi.seoul.go.kr:8088/"
    url_datasource = "/xml/citydata"
    url_page = "/1/5/"
    url_places = ["경복궁·서촌마을", "광화문·덕수궁", "창덕궁·종묘",
                  "강남 MICE 관광특구", "동대문 관광특구", "명동 관광특구", "이태원 관광특구", "잠실 관광특구", "종로·청계 관광특구", "홍대 관광특구",
                  "국립중앙박물관·용산가족공원", "남산공원", "뚝섬한강공원", "망원한강공원", "반포한강공원", "북서울꿈의숲", "서울대공원", "서울숲공원", "월드컵공원", "이촌한강공원", "잠실종합운동장", "잠실한강공원",
                  "가로수길", "낙산공원·이화마을", "노량진", "북촌한옥마을", "성수카페거리", "수유리 먹자골목", "쌍문동 맛집거리", "압구정로데오거리", "여의도", "영등포 타임스퀘어", "인사동·익선동", "창동 신경제 중심지", "DMC(디지털미디어시티)",
                  "가산디지털단지역", "강남역", "건대입구역", "고속터미널역", "교대역", "구로디지털단지역", "서울역", "선릉역", "신도림역", "신림역", "신촌·이대역", "왕십리역", "역삼역", "연신내역", "용산역"]

    output_filename = time.strftime("%Y%m%d-%H%M%S") + ".csv"

    check_availability = []

    for places in range(len(url_places)):
        url = url_base + secret.url_authentication_key + url_datasource + url_page + url_places[places]
        print("now connecting...: " + url)

        response = requests.get(url)
        dict_response = xmltodict.parse(response.content)
        # # to debug response file
        # with open("test.json", "w", encoding="UTF-8") as file:
        #     file.write(str(dict_response))

        if dict_response["SeoulRtd.citydata"]["RESULT"]["RESULT.CODE"] == "INFO-000":
            response_output = response_maker(dict_response)
            with open(output_filename, "w" if places == 0 else "a", encoding="UTF-8") as file:
                writer = csv.writer(file)
                if places == 0:
                    writer.writerow(response_output.keys())
                writer.writerow(response_output.values())
            check_availability.append(0)
        else:
            check_availability.append(1)
        time.sleep(2)

    end = time.time()

    print(("-" * 22) + "Program End" + ("-" * 22))
    print("result: ", check_availability)
    print("elapsed time: ", end - start, "sec")


import numpy as np
from collections import Counter
import cv2 as cv
import sys
from math import sqrt

# Affichage de fenetre
def display(title, img):
    cv.imshow(title, img)
    cv.waitKey(0) # Attente d'appui sur une touche
    cv.destroyAllWindows()

# Localise la region
def locate(box, color):
    locImg = cv.imread("tests_A1/img/regions.png")
    hsv = cv.cvtColor(locImg, cv.COLOR_BGR2HSV)
    x = box[0][0]
    y = box[0][1]

    # Couleur par region
    cal = {"hdf" : (hsv[350][500] - np.array([2, 20, 20]), hsv[350][500] + np.array([2, 20, 20])),
           "idf" : (hsv[500][500] - np.array([2, 20, 20]), hsv[500][500] + np.array([2, 20, 20])),
           "bre" : (hsv[500][100] - np.array([2, 20, 20]), hsv[500][100] + np.array([2, 20, 20])),
           "pll" : (hsv[700][200] - np.array([2, 20, 20]), hsv[700][200] + np.array([2, 20, 20])),
           "cvl" : (hsv[700][500] - np.array([2, 20, 20]), hsv[700][500] + np.array([2, 20, 20])),
           "bfc" : (hsv[500][800] - np.array([2, 20, 20]), hsv[500][800] + np.array([2, 20, 20])),
           "pac" : (hsv[800][800] - np.array([2, 20, 20]), hsv[800][800] + np.array([2, 20, 20])),
           "occ" : (hsv[1000][500] - np.array([2, 20, 20]), hsv[1000][500] + np.array([2, 20, 20])),
           "naq" : (hsv[1000][200] - np.array([2, 20, 20]), hsv[1000][200] + np.array([2, 20, 20])),
           "est" : (hsv[300][800] - np.array([2, 20, 20]), hsv[300][800] + np.array([2, 20, 20])),
           "ara" : (hsv[700][700] - np.array([2, 20, 20]), hsv[700][700] + np.array([2, 20, 20])),
           "nor" : (hsv[300][300] - np.array([2, 20, 20]), hsv[300][300] + np.array([2, 20, 20])),
           "cor" : (hsv[1000][800] - np.array([2, 20, 20]), hsv[1000][800] + np.array([2, 20, 20]))}

    for reg in cal:
        if (hsv[y][x] >= cal[reg][0]).all() and (hsv[y][x] <= cal[reg][1]).all():
            print("{} soldier in {}".format(color, reg))

# Renvoie vrai si fait partie de la légende
def inRegions(box):
    return (box[2][1] <= 1154 and box[0][1] >= 207)

# Recupere les 4 coins du plateau
def getBoardCorners(img):
    # Le detecteur de code Aruco
    det = cv.aruco.ArucoDetector()

    pts, ids, reject = det.detectMarkers(img)
    corners = []

    # On garde les coordonnees des Aruco dont l'id est 0
    for i in range(len(ids)):
        if ids[i][0] == 0:
            corners.append(pts[i][0].astype(int))

    # On trie les coordonnees pour avoir les Aruco en partant d'en haut a gauche dans le sens horaire

    # Tri horizontal
    for i in range(len(corners)):
        for j in range(len(corners)-i-1):
            if corners[j][0][0] > corners[j+1][0][0]: # s'il est + a droite, on swap
                aux = corners[j]
                corners[j] = corners[j+1]
                corners[j+1] = aux

    # Tri vertical
    if corners[0][0][1] > corners[1][0][1]: # s'il est + bas, on swap
        aux = corners[0]
        corners[0] = corners[1]
        corners[1] = aux

    if corners[2][0][1] > corners[3][0][1]:
        aux = corners[2]
        corners[2] = corners[3]
        corners[3] = aux

    # Arrangement final
    aux = corners[1]
    corners[1] = corners[2]
    corners[2] = corners[3]
    corners[3] = aux

    # Seuls les coins du plateau nous interessent a present
    corners[0] = corners[0][0]
    corners[1] = corners[1][1]
    corners[2] = corners[2][2]
    corners[3] = corners[3][3]

    return corners

# Detecte les pions grace a leur couleur
def detColor(img):
    display("Image d'origine", img)

    # On recupere les coins
    boardCorners = getBoardCorners(img)

    # Correction de la perspective
    pts1 = np.float32(boardCorners)
    pts2 = np.float32([[5,5], [995,5], [995,1195], [5,1195]])
    M = cv.getPerspectiveTransform(pts1,pts2)
    img = cv.warpPerspective(img,M,(1000,1200))

    # On recupere les nouveaux coins
    boardCorners = getBoardCorners(img)
    display("Perspective corrigee", img)

    # Contours du plateau
    cv.polylines(img, np.array([boardCorners]), True, (0,0,255), 2)
    display("Detection du plateau", img)

    # Etalonnage
    hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
    cal = {"blue" : (hsv[35][83] - np.array([5, 100, 100]), hsv[35][83] + np.array([5, 100, 100])),
           "green" : (hsv[53][170] - np.array([5, 100, 100]), hsv[53][170] + np.array([5, 100, 100])),
           "orange" : (hsv[71][85] - np.array([5, 100, 100]), hsv[71][85] + np.array([5, 100, 100])),
           "yellow" : (hsv[83][136] - np.array([5, 100, 100]), hsv[83][136] + np.array([5, 100, 100])),
           "black" : (hsv[40][128] - np.array([180, 255, 30]), hsv[40][128] + np.array([180, 255, 30]))
            }

    # Pour chaque couleur : application de masque + detection de contours
    for col in cal:
        thresh = cv.inRange(hsv, cal[col][0], cal[col][1])

        # Reduction bruit hors contours puis dans contours
        kernel = cv.getStructuringElement(cv.MORPH_RECT, (5,5))
        open = cv.morphologyEx(thresh, cv.MORPH_OPEN, kernel)
        kernel = cv.getStructuringElement(cv.MORPH_RECT, (5,5))
        close = cv.morphologyEx(open, cv.MORPH_CLOSE, kernel)
        display(col+" mask + noise reduction", close)

        contours = cv.findContours(close, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
        contours = contours[0]

        # Pour chaque contour : trouver le rectangle qui matche le mieux puis tous les dessiner
        sketch = img.copy()
        for c in contours:
            rot_rect = cv.minAreaRect(c)
            box = cv.boxPoints(rot_rect)
            box = np.int0(box)
            if inRegions(box):
                cv.drawContours(sketch,[box],0,(0,255,0),2)
                locate(box, col)

        #cv.imwrite("{}.png".format(col), sketch)
        display("boxes", sketch)

# Pour boucler sur toutes les images
dictImg =  {0: "tests_A1/img/soldiers.jpg"}

for k in dictImg: # Pour chaque image

    print("\n################\n")

    img = cv.imread(dictImg[k]) # Lecture de img
    print(dictImg[k])
    print("")

    detColor(img)

print("")


from django.http import HttpResponse, JsonResponse
from django.template import loader
from ..BL.UserRolesBL import UserRolesBL as urbl
from ..Models import *
import json
class UserRoleController:
	def allUserRoles(self,request):
		auro = urbl.allUserRoles();
		return HttpResponse(Common.Common().ConverttoJson(auro), content_type="application/json")

	def addUserRole(self,request):
		if request.method == 'POST':
			ud = json.loads(request.body)
			uuro = UserRole.UserRole();
			uuro.fullName = ud['fullname']
			uuro.isActive = ud['active']
		return HttpResponse(Common.Common().ConverttoJson(urbl.addUserRole(uuro)), content_type="application/json")

	def selectUserRole(self,request):
		if request.method == 'POST':
			ud = json.loads(request.body)
			uuro = UserRole.UserRole();
			uuro.Id = ud['id']
		return HttpResponse(Common.Common().ConverttoJson(urbl.selectUserRole(uuro)), content_type="application/json")

	def updateUserRole(self,request):
		if request.method == 'POST':
			ud = json.loads(request.body)
			uuro = UserRole.UserRole();
			uuro.Id = ud['id']
			uuro.fullName = ud['fullname']
			uuro.isActive = ud['active']
		return HttpResponse(Common.Common().ConverttoJson(urbl.updateUserRole(uuro)), content_type="application/json")

	def deleteUserRole(self,request):
		if request.method == 'POST':
			ud = json.loads(request.body)
			uuro = UserRole.UserRole();
			uuro.Id = ud['id']
		return HttpResponse(Common.Common().ConverttoJson(urbl.deleteUserRole(uuro)), content_type="application/json")


# -*- coding: utf-8 -*-
#!/bin/python
import sys
sys.path.append("./secret")

import grid_mdp
import random
random.seed(0)
import numpy as np

class SoftmaxPolicy:
    def __init__(self, grid, epsilon):
        self.actions = grid.actions

        grid.start();
        t,hats,r = grid.receive(self.actions[0]);
        self.theta = [ 0.0  for i in xrange(len(hats)*len(self.actions)) ]
        self.theta = np.array(self.theta);
        self.theta = np.transpose(self.theta);

        self.epsilon = epsilon

    def get_fea_vec(self, fea, a):
        f = np.array([0.0 for i in xrange(len(self.theta))]);

        idx = 0
        for i in xrange(len(self.actions)):
            if a == self.actions[i]: idx = i;
        for i in xrange(len(fea)):
            f[i + idx * len(fea)] = fea[i];

        return f

    def pi(self, fea):
        prob = [ 0.0 for i in xrange(len(self.actions))];
        sum1 = 0.0;
        for i in xrange(len(self.actions)):
            f = self.get_fea_vec(fea,self.actions[i])
            prob[i] = np.exp(np.dot(f, self.theta));
            sum1 += prob[i];
            #这个累加是为了softmax策略里面的分母部分需要对全部动作求和得到一个数
            #然后下面的对每个数除以这个和，也就是softmax里面每个动作的概率
        for i in xrange(len(self.actions)):
            prob[i] /= sum1;

        return prob;

    def take_action(self, fea):
        prob = self.pi(fea);

        ##choose
        r = random.random()
        s = 0.0
        for i in xrange(len(self.actions)):
            s += prob[i]
            if s >= r: return self.actions[i];

        return self.actions[len(self.actions)-1];
        #这里的两个return的原因分别是：首先借助pi(fea)我们知道了
        #fea这个代表的状态对应全部动作每个被选取的可能性，然后随机设置非零数r
        #然后对概率累加；
        #这时候我们要注意一点就是上面概率的计算方法，采用的softmax函数的原因：
            #softmax特点就是如果某一个zj大过其他z,那这个映射的分量就逼近于1,其他就逼近于0
        #当我们到最优动作的时候，这时候因为softmax的原因会让这个概率足够大，所以也就会出现其他动作的概率都很小
        #累加半天都没有随机设置的随机数r大，然后出现最优的时候，一下子就超过了r 然后我们就知道 哦 这个是最优动作
        #其实想法和ε-greedy设置最优动作对应概率最大的想法一样；
        #然后同样的，如果不小心随机的r很大，直到最后都没出现超过它的，那只能把最后一个当做最大了（摊手）

class ValuePolicy:
    def __init__(self, grid, epsilon):
        self.actions = grid.actions

        grid.start();
        t,hats,r = grid.receive(self.actions[0]);
        self.theta = [ 0.0  for i in xrange(len(hats)*len(self.actions)) ]
        self.theta = np.array(self.theta);
        self.theta = np.transpose(self.theta);

        self.epsilon = epsilon

    def get_fea_vec(self, fea, a):
        f = np.array([0.0 for i in xrange(len(self.theta))]);

        idx = 0
        for i in xrange(len(self.actions)):
            if a == self.actions[i]: idx = i;
        for i in xrange(len(fea)):
            f[i + idx * len(fea)] = fea[i];

        return f

    def qfunc(self, fea, a):
        f = self.get_fea_vec(fea, a);
        return np.dot(f, self.theta);

    def epsilon_greedy(self, fea):
        ## max q action
        epsilon = self.epsilon;

        amax    = 0
        qmax    = self.qfunc(fea, self.actions[0])
        for i in xrange(len(self.actions)):
            a   = self.actions[i]
            q   = self.qfunc(fea, a)
            if qmax < q:
                qmax  = q;
                amax  = i;

        ##probability
        pro = [0.0 for i in xrange(len(self.actions))]
        pro[amax] += 1- epsilon
        for i in xrange(len(self.actions)):
            pro[i] += epsilon / len(self.actions)

        ##choose
        r = random.random()
        s = 0.0
        for i in xrange(len(self.actions)):
            s += pro[i]
            if s >= r: return self.actions[i]

        return self.actions[len(self.actions)-1]


#User function Template for python3

class Solution:
    def removeStudents(self, arr, n):
        # code here
        count = [1]*n

        ma = 0
        for i in range(n):
            for j in range(0, i):

                if arr[j] < arr[i] and count[j]+1 < count[i]:
                    count[j] = max(count[j], count[i]+1)
                    ma = max(count[j], ma)

        return n-ma

#{
 # Driver Code Starts
#Initial Template for Python 3

if __name__ == '__main__':
    t = int (input ())
    for _ in range (t):
        N=int(input())
        H=list(map(int,input().split()))

        ob = Solution()
        print(ob.removeStudents(H,N))
# } Driver Code Ends


import cv2
import numpy as np

def binary_convert_2_image(imagePath):
    with open(imagePath, 'rb') as file:
        binary_data = file.read()

    binary_data = np.frombuffer(binary_data, dtype=np.uint8)
    decoded_image = cv2.imdecode(binary_data, cv2.IMREAD_COLOR)
    cv2.imwrite("decoded_image.jpg", decoded_image)

if __name__ == "__main__":
    imagePath = "/root/SuperResolution/flask/images/org.jpg"
    binaryImage = binary_convert_2_image(imagePath)


from flask import Flask, render_template, request, flash
from pymongo import MongoClient
import json
import os
from forms import ContactForm
from flask_mail import Message, Mail

mail = Mail()

app = Flask(__name__)

# app.secret_key = 'SECRET_KEY'

'''
MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017
DBS_NAME = 'donorsUSA'
COLLECTION_NAME = 'projects'
'''

app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_SSL"] = True
# app.config["MAIL_USERNAME"] = ''
# app.config["MAIL_PASSWORD"] = ''
app.config["MAIL_USERNAME"] = os.getenv('MAIL_USERNAME')
app.config["MAIL_PASSWORD"] = os.getenv('MAIL_PASSWORD')

mail.init_app(app)

app.secret_key = os.getenv('SECRET_KEY')
MONGO_URI = os.getenv('MONGODB_URI', 'mongodb://localhost:27017')
DBS_NAME = os.getenv('MONGO_DB_NAME', 'donorsUSA')
COLLECTION_NAME = 'projects'
app.config['DEBUG'] = os.getenv('DEBUG', 'True')

@app.route("/", methods=['GET', 'POST'])
def index():
    """
    A Flask view to serve the main home page.
    """
    form = ContactForm()

    if request.method == 'POST':
        if form.validate() == False:
            flash('Allfields are required')
            return render_template('home.html', form=form)
        else:
            msg = Message(form.subject.data, sender='coylec.devwork@gmail.com', recipients=['conradcoyle@gmail.com'])
            msg.body = """
                  From: %s <%s>
                  %s
                  """ % (form.name.data, form.email.data, form.message.data)
            mail.send(msg)

            return render_template('home.html', success=True)

    elif request.method == 'GET':
        return render_template("home.html", form=form)

@app.route('/statistics')
def stats():
    return render_template('statistics.html')

@app.route("/donorsUS/projects")
def donor_projects():
    """
    A Flask view to serve the project data from
    MongoDB in JSON format.
    """

    # A constant that defines the record fields that we wish to retrieve.
    FIELDS = {
        '_id': False, 'funding_status': True, 'school_state': True,
        'resource_type': True, 'poverty_level': True,
        'date_posted': True, 'total_donations': True
    }

    # Open a connection to MongoDB using a with statement such that the
    # connection will be closed as soon as we exit the with statement
    # The MONGO_URI connection is required when hosted using a remote mongo db.
    with MongoClient(MONGO_URI) as conn:
        # Define which collection we wish to access
        collection = conn[DBS_NAME][COLLECTION_NAME]
        # Retrieve a result set only with the fields defined in FIELDS
        # and limit the the results to 55000
        projects = collection.find(projection=FIELDS, limit=20000)
        # Convert projects to a list in a JSON object and return the JSON data
        return json.dumps(list(projects))

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


"""
Humanities Data Analysis: Case studies with Python
--------------------------------------------------
Folgert Karsdorp, Mike Kestemont & Allen Riddell
Chapter 9: A Topic Model of United States Supreme Court Opinions, 1900-2000
"""

# %%
# Undocumented code snippet used in chapter (e.g., for figure generation)
# NOTE: fixed random seed for normal mixture model
import numpy.random; numpy.random.seed(1)
import random; random.seed(1)

# %%
import pandas as pd

df = pd.read_csv("data/tate.csv.gz", index_col='artId')
df[["artist", "acquisitionYear", "accession_number", "medium", "width", "height"]].head(2)

# %%
# Undocumented code snippet used in chapter (e.g., for figure generation)
assert df.shape[0] == 63298, df.shape[0]

# %%
df = df.loc[(df['width'] < 8000) & (df['height'] < 8000)]
df = df.loc[(df['width'] >= 20) & (df['height'] >= 20)]

# %%
# Undocumented code snippet used in chapter (e.g., for figure generation)
assert df.shape[0] == (63298 - 18), df.shape[0]

# %%
import numpy as np

df["width_log10"] = np.log10(df["width"])
df["height_log10"] = np.log10(df["height"])

# %%
import matplotlib.pyplot as plt
import numpy as np

fig, axes = plt.subplots(1, 3, figsize=(10, 6))
df.plot(x='width_log10', y='height_log10', kind='scatter', alpha=0.02, ax=axes[0])
axes[0].set(xlabel=r'Width ($\log_{10}$)', ylabel=r'Height ($\log_{10}$)')

df['width_log10'].plot(kind='density', title=r'Width ($\log_{10}$)', ax=axes[1])
df['height_log10'].plot(kind='density', title=r'Height ($\log_{10}$)', ax=axes[2])
xlim = (1.2, 3.8); axes[1].set_xlim(*xlim); axes[2].set_xlim(*xlim)
plt.tight_layout();

# %%
# Undocumented code snippet used in chapter (e.g., for figure generation)
np.set_printoptions(suppress=True)  # supress scientific notation

# %%
import sklearn.mixture as mixture

gmm = mixture.BayesianGaussianMixture(n_components=3, max_iter=200)
gmm.fit(df[['width_log10', 'height_log10']])

# order of components is arbitrary, sort by mixing proportions (decending)
order = np.argsort(gmm.weights_)[::-1]
means, covariances, weights = gmm.means_[order], gmm.covariances_[order], gmm.weights_[order]

# mu_1, mu_2, mu_3 in the equation above
print("μ's =", means.round(2))
# Sigma_1, Sigma_2, Sigma_3 in the equation above
print("Σ's =", covariances.round(4))
# theta_1, theta_2, theta_3 in the equation above
print("θ's =", weights.round(2))

# %%
# Undocumented code snippet used in chapter (e.g., for figure generation)
assert (means[0].round(2) == [2.45, 2.4]).all()

# %%
# Undocumented code snippet used in chapter (e.g., for figure generation)
# code not for public consumption, just for plot
import scipy.stats
import matplotlib.patches

# reorder responsibilties
responsibilities = gmm.predict_proba(df[['width_log10', 'height_log10']])[:, order]
assert (responsibilities.argmax(axis=1) == 0).mean() > 0.5
color_predicted = [f'C{i}' for i in responsibilities.argmax(axis=1)]
#color_true = [f'C{i}' for i in (df['mf'] == 'F').astype(int)]
x = np.linspace(df['width_log10'].min(), df['width_log10'].max(), 1000)
y = np.linspace(df['height_log10'].min(), df['height_log10'].max(), 1000)
X, Y = np.meshgrid(x, y)
fig, ax = plt.subplots()

for i, (mu, Sigma) in enumerate(zip(means, covariances)):
    pdf = scipy.stats.multivariate_normal(mean=mu, cov=Sigma).pdf
    Z = pdf(np.column_stack([X.ravel(), Y.ravel()])).reshape(X.shape)
    CS = ax.contour(X, Y, Z, 10, alpha=0.8, cmap="Greys")

ax.scatter(df['width_log10'], df['height_log10'],
           color=color_predicted, alpha=0.05)
ax.set(xlabel=r'Width ($\log_{10}$)', ylabel=r'Height ($\log_{10}$)')

patches = [
    matplotlib.patches.Patch(color=f'C{i}', label=rf'latent class {i+1} ($\theta_{i+1} = {theta:.2f}$)')
    for i, theta in enumerate(weights)
]
plt.legend(handles=patches)
plt.plot([0, 1], [0, 1], transform=ax.transAxes, alpha=0.2, linestyle='--', color="black") # diagonal line
plt.tight_layout();

# %%
import os
import gzip
import pandas as pd

with gzip.open('data/supreme-court-opinions-by-author.jsonl.gz', 'rt') as fh:
    df = pd.read_json(fh, lines=True).set_index(['us_reports_citation', 'authors'])

# %%
df.loc['323 US 214']

# %%
print(df.loc['323 US 214'].loc['murphy', 'text'][:500])

# %%
# Undocumented code snippet used in chapter (e.g., for figure generation)
assert len(df) == 34_677

# %%
df['year'].hist(bins=50)

# %%
df['year'].describe()

# %%
import sklearn.feature_extraction.text as text

# min_df: ignore words occurring in fewer than `n` documents
# stop_words: ignore very common words ("the", "and", "or", "to", ...)
vec = text.CountVectorizer(lowercase=True, min_df=100, stop_words='english')
dtm = vec.fit_transform(df['text'])

# %%
print(f'Shape of document-term matrix: {dtm.shape}. '
      f'Number of tokens {dtm.sum()}')

# %%
import sklearn.decomposition as decomposition
model = decomposition.LatentDirichletAllocation(
    n_components=100, learning_method='online', random_state=1)

# %%
document_topic_distributions = model.fit_transform(dtm)

# %%
vocabulary = vec.get_feature_names()
# (# topics, # vocabulary)
assert model.components_.shape == (100, len(vocabulary))
# (# documents, # topics)
assert document_topic_distributions.shape == (dtm.shape[0], 100)

# %%
topic_names = [f'Topic {k}' for k in range(100)]
topic_word_distributions = pd.DataFrame(
    model.components_, columns=vocabulary, index=topic_names)
document_topic_distributions = pd.DataFrame(
    document_topic_distributions, columns=topic_names, index=df.index)

# %%
document_topic_distributions.loc['323 US 214'].loc['murphy'].head(10)

# %%
murphy_dissent = document_topic_distributions.loc['323 US 214'].loc['murphy']
murphy_dissent.sort_values(ascending=False).head(10)

# %%
topic_word_distributions.loc['Topic 8'].sort_values(ascending=False).head(18)

# %%
# Undocumented code snippet used in chapter (e.g., for figure generation)
assert topic_word_distributions.loc['Topic 8'].shape == (13_231,)

# %%
weights_sample = document_topic_distributions.sample(8)
weights_ordered = weights_sample.apply(
    lambda row: row.sort_values(ascending=False).reset_index(drop=True), axis=1)
# transpose DataFrame so pandas plots weights on correct axis
ax = weights_ordered.T.plot()
ax.set(xlabel='Rank of mixing weight', ylabel='Probability', xlim=(0, 25));

# %%
# Undocumented code snippet used in chapter (e.g., for figure generation)
# lauderdale_clark_label, our_topic_number, our_topic_words
lauderdale_clark_figure_3_mapping = (
    ('lands, indian, land', 59, 'indian, territory, indians'),
    ('tax, commerce, interstate', 89, 'commerce, interstate, state'),
    ('federal, immunity, law', 2, 'suit, action, states, ..., immunity'),
    ('military, aliens, aliens', 22, '..., alien,..., aliens, ..., deportation, immigration'),
    ('property, income, tax', 79, 'tax, taxes, property'),
    ('district, habeas, appeal', 43, 'court, federal, district, appeals, review, courts, habeas'),
    ('negligence, maritime, admiralty', 7, 'vessel, ship, admiralty'),
    ('patent, copyright, cable', 86, 'patent, ..., invention, patents'),
    ('search, fourth, warrant', 37, 'search, warrant, fourth'),
    ('jury, death, penalty', 3, 'sentence, death, sentencing, penalty'),
    ('school, religious, schools', 73, 'religious, funds, ... government, ..., establishment'),
    ('trial, counsel, testimony', 13, 'counsel, trial, defendant'),
    ('epa, waste, safety', 95, 'regulations, ..., agency, ..., safety, ..., air, epa' ),
    ('speech, ordinance, public', 58, 'speech, amendment, .., public'),
    ('antitrust, price, securities', 39, 'market, price, competition, act, antitrust'),
    ('child, abortion, children', 14, 'child, children, medical, ..., woman, ... abortion'),
    ('prison, inmates, parole', 67, 'prison, release, custody, parole' ),
    ('political, election, party', 23, 'speech, amendment, ..., political, party'),
    ('title, vii, employment', 55, 'title, discrimination, ..., vii'),
    ('offense, criminal, jeopardy', 78, 'criminal, ..., crime, offense'),
    ('union, labor, board', 24, 'board, union, labor'),
    ('damages, fees, attorneys', 87, 'attorney, fees, ..., costs'),
    ('commission, rates, gas', 97, 'rate, ..., gas, ..., rates'),
    ('congress, act, usc', 41, 'federal, congress, act, law'),
)
import pickle
import hashlib
assert len(lauderdale_clark_figure_3_mapping) == 24, len(lauderdale_clark_figure_3_mapping)
assert hashlib.sha256(pickle.dumps(lauderdale_clark_figure_3_mapping)).hexdigest() == '02e57c243457f5bc7f06f2bffa6d1bac68cc32d537db29d5dc6519ee11726a16'

# %%
# issueArea is coded as int but pandas does not allow us to mix int and
# values indicating NaN, so we represent the issueArea codes as `float`
# as a compromise.
scdb = pd.read_csv('data/SCDB_2016_01_caseCentered_Citation.csv.zip',
                   dtype={'issueArea': float}, encoding='latin1', index_col='caseId')
df_after_1945 = df.loc[df.case_id.isin(scdb.index)]

# %%
df_after_1945 = df_after_1945.join(scdb['issueArea'], on='case_id')

# %%
# for issueArea labels see SCDB documentation
# Exclude label 14 ("Private Action") as none of the opinions are
# assigned this label
spaeth_issue_areas = {
    1: "Criminal Procedure",
    2: "Civil Rights",
    3: "First Amendment",
    4: "Due Process",
    5: "Privacy",
    6: "Attorneys",
    7: "Unions",
    8: "Economic Activity",
    9: "Judicial Power",
    10: "Federalism",
    11: "Interstate Relations",
    12: "Federal Taxation",
    13: "Miscellaneous",
}
df_after_1945["issueArea"] = pd.Categorical(
    df_after_1945["issueArea"].replace(spaeth_issue_areas),
    categories=spaeth_issue_areas.values())

# %%
import collections

[(issue_area, count)] = collections.Counter(df_after_1945['issueArea']).most_common(1)
print(f'Issue area `{issue_area}` associated with {count} opinions, '
      f'{count / len(df_after_1945):.0%} of all opinions.')

# %%
document_word_counts = dtm.toarray().sum(axis=1)
document_topic_word_counts = document_topic_distributions.multiply(
    document_word_counts, axis='index'
)
df_after_1945 = df_after_1945.join(document_topic_word_counts)

# %%
df_after_1945.groupby('issueArea')["Topic 3"].sum()

# %%
topic_word_distributions.loc['Topic 3'].sort_values(ascending=False).head()

# %%
figure_3_topic_names = [f'Topic {t}' for _, t, _ in lauderdale_clark_figure_3_mapping]
df_plot = df_after_1945.groupby('issueArea')[figure_3_topic_names].sum()
df_plot = df_plot.rename(columns={
    f'Topic {t}': f'{t}: {figure_3_words}'
    for figure_3_words, t, _ in lauderdale_clark_figure_3_mapping
})
# heatmap code adapted from matplotlib documentation:
# https://matplotlib.org/gallery/images_contours_and_fields/
# image_annotated_heatmap.html

# `numpy.flipud` flips y-axis (to align with Lauderdale and Clark)
fig, ax = plt.subplots()
im = ax.imshow(np.flipud(df_plot.values), cmap="Greys")

ax.set_xticks(np.arange(len(df_plot.columns)))
ax.set_yticks(np.arange(len(df_plot.index)))
ax.set_xticklabels(df_plot.columns)
ax.set_yticklabels(reversed(df_plot.index))

# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
         rotation_mode="anchor")
ax.set_title('Topic model and expert label alignment')
fig.tight_layout()

# %%
minor_top_topics = topic_word_distributions['minor'].sort_values(ascending=False).head(5)

# %%
minor_top_topics_top_words = topic_word_distributions.loc[minor_top_topics.index].apply(
        lambda row: ', '.join(row.sort_values(ascending=False).head().index),
        axis=1,
)
minor_top_topics_top_words.name = 'topic_top_words'
minor_top_topics.to_frame().join(minor_top_topics_top_words)

# %%
opinion_of_interest = ('483 US 587', 'brennan')
document_topic_distributions.loc[opinion_of_interest, minor_top_topics.index]

# %%
print(df.loc[opinion_of_interest, 'text'].values[0][1000:2000])

# %%
opinion_of_interest = ('479 US 189', 'white_b')
print(f'"minor" count in 479 US 189:', sum('minor' in word.lower()
          for word in df.loc[opinion_of_interest, 'text'].values[0].split()))

# %%
document_topic_distributions.loc[opinion_of_interest, minor_top_topics.index.tolist() + ['Topic 23']]

# %%
topic_oi = 'Topic 23'
topic_oi_words = ', '.join(
    topic_word_distributions.loc[topic_oi].sort_values(ascending=False).head(8).index)
print(f'Topic 23 top words:\n  {topic_oi_words}')

# %%
print(df.loc[opinion_of_interest, 'text'][0][1000:1500])

# %%
import itertools

opinion_text = df.loc[opinion_of_interest, 'text'][0]
window_width, num_words = 3, len(opinion_text.split())
words = iter(opinion_text.split())
windows = [
    ' '.join(itertools.islice(words, 0, window_width))
    for _ in range(num_words // window_width)
]
print([window for window in windows if 'minor' in window])

# %%
labor_topic = 'Topic 24'
topic_word_distributions.loc[labor_topic].sort_values(ascending=False).head(10)

# %%
topic_top_words = topic_word_distributions.loc[labor_topic].sort_values(
    ascending=False).head(10).index
topic_top_words_joined = ', '.join(topic_top_words)
print(topic_top_words_joined)

# %%
# convert `dtm` (matrix) into an array:
opinion_word_counts = np.array(dtm.sum(axis=1)).ravel()
word_counts_by_year = pd.Series(opinion_word_counts).groupby(df.year.values).sum()
topic_word_counts = document_topic_distributions.multiply(opinion_word_counts,
                                                          axis='index')
topic_word_counts_by_year = topic_word_counts.groupby(df.year.values).sum()
topic_proportion_by_year = topic_word_counts_by_year.divide(word_counts_by_year,
                                                            axis='index')

# %%
topic_proportion_by_year.head()

# %%
import matplotlib.pyplot as plt

window = 3
topic_proportion_rolling = topic_proportion_by_year.loc[1900:, labor_topic].rolling(
    window=window).mean()
topic_proportion_rolling.plot()
plt.title(f'Prevalence of {labor_topic} ({window} year rolling average)'
          f'\n{topic_top_words_joined}');

# %%
# each tuple records the following in the order used by Lauderdale and Clark:
# (<Lauderdale and Clark top words>, <our topic number>, <our top words>)
lauderdale_clark_figure_3_mapping = (
    ('lands, indian, land', 59, 'indian, territory, indians'),
    ('tax, commerce, interstate', 89, 'commerce, interstate, state'),
    ('federal, immunity, law', 2, 'suit, action, states, ..., immunity'),
    ('military, aliens, aliens', 22, '..., alien,..., aliens, ..., deportation, immigration'),
    ('property, income, tax', 79, 'tax, taxes, property'),
    ('district, habeas, appeal', 43, 'court, federal, district, appeals, review, courts, habeas'),
    ('negligence, maritime, admiralty', 7, 'vessel, ship, admiralty'),
    ('patent, copyright, cable', 86, 'patent, ..., invention, patents'),
    ('search, fourth, warrant', 37, 'search, warrant, fourth'),
    ('jury, death, penalty', 3, 'sentence, death, sentencing, penalty'),
    ('school, religious, schools', 73, 'religious, funds, ... government, ..., establishment'),
    ('trial, counsel, testimony', 13, 'counsel, trial, defendant'),
    ('epa, waste, safety', 95, 'regulations, ..., agency, ..., safety, ..., air, epa' ),
    ('speech, ordinance, public', 58, 'speech, amendment, .., public'),
    ('antitrust, price, securities', 39, 'market, price, competition, act, antitrust'),
    ('child, abortion, children', 14, 'child, children, medical, ..., woman, ... abortion'),
    ('prison, inmates, parole', 67, 'prison, release, custody, parole' ),
    ('political, election, party', 23, 'speech, amendment, ..., political, party'),
    ('title, vii, employment', 55, 'title, discrimination, ..., vii'),
    ('offense, criminal, jeopardy', 78, 'criminal, ..., crime, offense'),
    ('union, labor, board', 24, 'board, union, labor'),
    ('damages, fees, attorneys', 87, 'attorney, fees, ..., costs'),
    ('commission, rates, gas', 97, 'rate, ..., gas, ..., rates'),
    ('congress, act, usc', 41, 'federal, congress, act, law'),
)

# %%
# Undocumented code snippet used in chapter (e.g., for figure generation)
# integrity check. We define this table in two places. The definitions must match.
import pickle
import hashlib
assert len(lauderdale_clark_figure_3_mapping) == 24, len(lauderdale_clark_figure_3_mapping)
assert hashlib.sha256(pickle.dumps(lauderdale_clark_figure_3_mapping)).hexdigest() == '02e57c243457f5bc7f06f2bffa6d1bac68cc32d537db29d5dc6519ee11726a16'

# %%
# Undocumented code snippet used in chapter (e.g., for figure generation)
import matplotlib.pyplot as plt
discrimination_topic = 'Topic 15'
topic_top_words = topic_word_distributions.loc[discrimination_topic].sort_values(ascending=False).head(12).index
topic_top_words_joined = f"{', '.join(topic_top_words[:6])}\n{', '.join(topic_top_words[6:])}"
window = 3
topic_proportion_by_year.loc[1900:, discrimination_topic].rolling(window=window).mean().plot()
plt.suptitle(f'Prevalence of {discrimination_topic} ({window} year rolling average)', y=1.05, fontsize='x-large')
plt.title(topic_top_words_joined, fontsize='medium', style='italic')
plt.axvline(x=1954, color='orange', label='Brown v. Board')
plt.ylabel("Topic prevalence")
plt.xlabel("Year")
plt.legend()
plt.savefig('figures/discrimination-topic.png')


import logging

logger = logging.getLogger(__name__)

class Process:

    @staticmethod
    def process_data_dictionary(metrics_dict, source_name):
        """
        this function will process dictionary which we passed as param.
        passed dict format is
                    {'16node_master.Analytics.threads': [(1491.0, 1538062033362), (1391.0, 1538062033352) ],
                    '16node_data1.Analytics.threads': [(1275.0, 1538062210848), (1391.0, 1538062033352)]}
            where key is metric name, value is tuple (metric_value, timestamp)
            key in dictionary should be splitted,
                16node_master.Analytics.threads should be "Analytics.threads" as metric name
                and "16node_master" as tagvalue, tagname is "node"
            hint of creating tag, node=<tagvalue>
        :param metrics_dict:
        :param source_name:
        :return:
        """
        logger.info("processing . . .")
        ret = []
        if len(metrics_dict) == 0:
            logger.info("metrics dictionary is empty")
            return ret

        data_format = "%s %s %s source=%s"
        for metric_name, values_timestamps in metrics_dict.items():
            for value_timestamp in values_timestamps:
                value, timestamp = value_timestamp
                ret.append(data_format % (metric_name, str(value), str(timestamp), source_name))
        return ret


import os
import shutil
from tsff.algorithm_module import train,resume_train
import pandas as pd
import numpy as np
import warnings
import argparse

import time
from pathlib import Path

def parse_args():
    parser = argparse.ArgumentParser(description='Train a detector')
    parser.add_argument('--config', help='train config file path')
    parser.add_argument('--work-dir', help='the dir to save logs and models')
    parser.add_argument(
        '--resume-from', help='the checkpoint file to resume from')
    parser.add_argument(
        '--auto-resume',
        action='store_true',
        help='resume from the latest checkpoint automatically')
    parser.add_argument(
        '--no-validate',
        action='store_true',
        help='whether not to evaluate the checkpoint during training')
    group_gpus = parser.add_mutually_exclusive_group()
    group_gpus.add_argument(
        '--gpus',
        type=int,
        help='number of gpus to use '
        '(only applicable to non-distributed training)')
    group_gpus.add_argument(
        '--gpu-ids',
        type=int,
        nargs='+',
        help='ids of gpus to use '
        '(only applicable to non-distributed training)')
    parser.add_argument('--seed', type=int, default=None, help='random seed')
    parser.add_argument(
        '--deterministic',
        action='store_true',
        help='whether to set deterministic options for CUDNN backend.')
    parser.add_argument(
        '--launcher',
        choices=['none', 'pytorch', 'slurm', 'mpi'],
        default='none',
        help='job launcher')
    parser.add_argument(
        '--options',
        help='override some settings in the used config, the key-value pair '
        'in xxx=yyy format will be merged into config file (deprecate), '
        'change to --cfg-options instead.')
    parser.add_argument(
        '--cfg-options',
        help='override some settings in the used config, the key-value pair '
        'in xxx=yyy format will be merged into config file. If the value to '
        'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
        'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
        'Note that the quotation marks are necessary and that no white space '
        'is allowed.')
    parser.add_argument('--local_rank', type=int, default=0)
    args = parser.parse_args()
    if 'LOCAL_RANK' not in os.environ:
        os.environ['LOCAL_RANK'] = str(args.local_rank)

    return args

def experiment_train(exp_path):
    # args from
    args = parse_args()

    base_path = 'work_dirs'

    start_time = time.gmtime()
    start_time = time.strftime('%Y%m%d_%H',start_time)

    exp_name = os.path.join(Path(base_path))
    csv_name = os.path.join(Path(base_path),f"{start_time}_experiment_list.csv")

    os.makedirs(exp_name, exist_ok= True)

    cur_pd = pd.read_csv(exp_path)
    # save current experiment setting in work_dirs/
    cur_pd.to_csv(csv_name, index = False)

    resume_flag = False

    while True:
        # flag for cur loop
        resume_flag = False
        cur_pd = pd.read_csv(exp_path)
        if 1 not in cur_pd['train_flag'].values:
            break
        # train loop
        train_flag = cur_pd.index[cur_pd['train_flag'] == True].tolist()
        inv_train_flag = cur_pd.index[cur_pd['train_flag'] == False].tolist()

        # only for first loop
        no_train_pd = cur_pd.loc[inv_train_flag,:]
        no_train = no_train_pd.index[no_train_pd['train_start_time'].isna()].tolist()
        cur_pd.loc[no_train,"train_start_time"] = "Excluded"

        # general loop
        cur_time = time.gmtime()
        cur_time = time.strftime('%Y%m%d_%H%M',cur_time)

        cur_idx = train_flag.pop(0)
        # save time in training file
        if isinstance(cur_pd.loc[cur_idx,'train_start_time'],str):
            resume_flag = True
        cur_pd.loc[cur_idx,'train_start_time'] = cur_time

        ## set model conf and work_dir based on csv file
        args.config = cur_pd.loc[cur_idx,'model_conf']
        # save file save path
        cur_work_dir = os.path.join(exp_name,args.config.split('/')[-2],args.config.split('/')[-1].split('.')[0])
        os.makedirs(cur_work_dir,exist_ok=True)
        args.work_dir = str(cur_work_dir)

        # model train start
        if resume_flag is not True:
            train(args=args)
        else:
            resume_train(args=args)

        cur_pd.loc[cur_idx,'train_flag'] = 0

        # save in experiment_control
        cur_pd.to_csv(exp_path, index=False)

        # flag for next loop

if __name__ == "__main__":
    #exp_path = "/workspace/tsff/algorithm_module/experiment_control/experiment_list.csv"
    exp_path = "/home/ycc/additional_life/tsff/algorithm_module/experiment_control/experiment_list.csv"
    #exp_path = "/workspace/work_dirs/20220328_03/experiment.csv"
    experiment_train(exp_path)


import logging
import random
import time
from types import ModuleType
from typing import List, Union

from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver

from great_magna_tests_shared import URLs
from great_magna_tests_shared.enums import PageType, Service
from browserpages import ElementType, common_selectors
from great_magna_tests_shared.utils import check_url_path_matches_template
from browserpages.common_actions import (
    Actor,
    Selector,
    assertion_msg,
    check_for_sections,
    check_if_element_is_not_present,
    check_if_element_is_visible,
    check_url,
    find_element,
    find_selector_by_name,
    find_elements,
    go_to_url,
    pick_option,
    is_element_present,
    submit_form,
    check_random_radio,
    take_screenshot,
    wait_for_page_load_after_action,
    fill_out_input_fields,
    fill_out_email_address
)

NAME = "Marketing Approach"
SERVICE = Service.BUILD_AN_EXPORT_PLAN
TYPE = PageType.BUILD_AN_EXPORT_PLAN
URL = URLs.GREAT_MAGNA_EXPORT_PLAN_MARKETING_APPROACH.absolute
PAGE_TITLE = "Marketing Approach Page"

SELECTORS = {
    "marketing approach": {
        "open": Selector(
            By.XPATH, "//span[contains(text(),'open')]"
        ),
        "close": Selector(
            By.XPATH, "//span[contains(text(),'close')]"
        ),
        "check box": Selector(
            By.CSS_SELECTOR, "#target-age-groups > form > ul > li:nth-child(1) > label"
        ),
        "0-14 year olds": Selector(
            By.CSS_SELECTOR, "#target-age-groups > form > ul > li:nth-child(1) > label"
        ),
        "15-19": Selector(
            By.XPATH, "//label[contains(text(),'15-19 year olds')]"
        ),
        "20-24": Selector(
            By.CSS_SELECTOR, "//input[@id='20-24']"
        ),
        "25-34": Selector(
            By.XPATH, "//label[contains(text(),'25-34 year olds')]"
        ),
        "35-44": Selector(
            By.XPATH, "//label[contains(text(),'35-44 year olds')]"
        ),
        "45-54": Selector(
            By.CSS_SELECTOR, "//label[contains(text(),'45-54 year olds')]"
        ),
        "55-64": Selector(
            By.XPATH, "//label[contains(text(),'55-64 year olds')]"
        ),
        "65": Selector(
            By.CSS_SELECTOR, "//label[contains(text(),'65 years old and over')]"
        ),
        "confirm": Selector(
            By.XPATH, "//button[contains(text(),'Confirm')]"
        ),
        "educational moment": Selector(
            By.CSS_SELECTOR,
            "#target-age-groups > div > div:nth-child(2) > div > div:nth-child(3) > div > div > div > button > i"
        ),
        "add route to market": Selector(
            By.CSS_SELECTOR, "#route-to-market > button"
        ),
        "how will we promote to product drop down": Selector(
            By.CSS_SELECTOR, "#route-to-market > div > div:nth-child(2) > div > button"
        ),
        "example": Selector(
            By.CSS_SELECTOR, "#route-to-market > div:nth-child(1) > div.form-group > div > button"
        ),
        "route to market text": Selector(
            By.CSS_SELECTOR, "#target-age-groups > button > span"
        ),
        "delete icon": Selector(
            By.CSS_SELECTOR, "#route-to-market > div:nth-child(1) > div.text-center > button > i"
        ),
        "marketing resources example": Selector(
            By.CSS_SELECTOR,
            "#resources > div > div.learning > div.learning__buttons.m-b-xs > button.button-example.button.button--small.button--tertiary.m-r-xxs"
        ),
        "marketing resources text": Selector(
            By.XPATH, "//textarea[@id='resources']"
        ),
        "open navigation": Selector(
            By.XPATH,
            "//body/main[@id='content']/div[@id='sidebar-content']/nav[@id='collapseNav']/div[1]/button[1]/i[1]"
        ),
        "section complete": Selector(
            By.XPATH, "//label[contains(text(),'Yes')]"
        ),
        "costs and pricing": Selector(
            By.XPATH, "//span[contains(text(),'Costs and pricing')]"
        ),
        "export plan home": Selector(
            By.CSS_SELECTOR,
            "#marketing-approach-content > section.p-v-m.bg-blue-deep-80 > div > div > div.c-2-3-m.c-1-2-xl > div > a"
        ),
        "selling direct to your customer": Selector(
            By.XPATH, "//h4[contains(text(),'Selling direct to your customer')]"
        ),
        "what marketing resources example": Selector(
            By.CSS_SELECTOR,
            "#resources > div > div.learning > div.learning__buttons.m-b-xs > button.button-example.button.button--small.button--tertiary.m-r-xxs"
        ),
        "lesson": Selector(
            By.CSS_SELECTOR,
            "#resources > div > div.learning > div.learning__buttons.m-b-xs > button.button-lesson.button.button--small.button--tertiary.m-r-xxs"
        ),
        "nav costs and pricing": Selector(
            By.XPATH, "//a[contains(text(),'Costs and pricing')]"
        ),
        "back": Selector(
            By.XPATH, "//body/div[10]/div/div/div/div[1]/a"
        ),
        "add a product": Selector(
            By.XPATH, "//button[contains(text(),'Add a product')]"
        ),
        "add a target market": Selector(
            By.XPATH, "//button[contains(text(),'Add a target market')]"
        ),
        "top export plan home": Selector(
            By.XPATH, "//*[@id=\"marketing-approach-content\"]/section[1]/div/div/div[2]/a/span"
        ),
        "yes checkbox": Selector(
            By.CSS_SELECTOR, "#section-complete > div > label"
        ),
        "search next button": Selector(
            By.XPATH, "//body/div[9]/div/div/form/div[2]/div/span/div/section/div/div/button"
        ),
        "dashboard": Selector(
            By.XPATH, "//a[contains(text(),'Dashboard')]"
        ),

    }
}

def visit(driver: WebDriver, *, page_name: str = None):
    go_to_url(driver, URL, page_name or NAME)

def should_be_here(driver: WebDriver):
    check_url(driver, URL, exact_match=False)

def enter_text(driver: WebDriver, element_name: str):
    text_element = find_element(
        driver, find_selector_by_name(SELECTORS, element_name)
    )
    text_element.clear()
    text_element.send_keys("Automated tests")

def validate_entered_text(driver: WebDriver, element_name: str):
    text_element = find_element(
        driver, find_selector_by_name(SELECTORS, element_name)
    )
    if "Automated tests" in text_element.text:
        return True
    return False

def find_and_click(driver: WebDriver, *, element_selector_name: str):
    find_and_click = find_element(
        driver, find_selector_by_name(SELECTORS, element_selector_name)
    )
    find_and_click.click()

def find_and_select_random_item_list(driver: WebDriver, element_selector_name: str):
    #//body/main/div[2]/section[4]/div/div[2]/div/button
    find_and_click(driver, element_selector_name="Add route to market")
    drop_down_btn = driver.find_element_by_xpath(
        "//body/main/div[2]/section[4]/div/div[2]/div/div[1]/div[1]/div/div/div[2]/button"
        )
    drop_down_btn.click()
    # promote_your_product_btn = driver.find_element_by_xpath(
    #     "//body/main/div[2]/section[4]/div/div[2]/div/div/div[2]/div/div/div[3]/div[1]")
    # promote_your_product_btn.click()
    driver.implicitly_wait(5)
    # select__list body-l bg-white radius
    drop_down_element = driver.find_element_by_xpath(
        "//body/main/div[2]/section[4]/div/div[2]/div/div/div[1]/div/div/div[3]/ul")
    # promote_your_product_element = driver.find_element_by_xpath(
    #     "//body/main/div[2]/section[4]/div/div[2]/div/div/div[2]/div/div/div[3]/ul")
    li_elements = drop_down_element.find_elements_by_tag_name("li")
    # li_elements = promote_your_product_element.find_elements_by_tag_name("li")
    # logging.debug("list elements")
    # logging.debug(li_elements)
    random_number = 0
    if len(li_elements) > 2:
        random_number = random.randint(1, len(li_elements) - 1)
    random_li_element = li_elements[random_number]
    # logging.debug(random_number)
    # logging.debug(random_li_element.tag_name)
    # logging.debug(random_li_element)
    time.sleep(2)

def random_select_checkbox(driver: WebDriver, element_name: str):
    find_and_click(driver, element_selector_name="Select")

    random_age_group_element_xpath = "//body/main/div[2]/section[3]/div/div/div[2]/div/div[1]/form/ul/li" \
                                     + "[" + str(random.randint(0, 8)) + "]" + "/label"
    driver.implicitly_wait(1)
    driver.find_element_by_xpath(random_age_group_element_xpath).click()
    find_and_click(driver, element_selector_name="Confirm")

def check_section_complete_yes(driver: WebDriver, element_selector_name: str):
    check_yes_link = find_element(
        driver, find_selector_by_name(SELECTORS, element_selector_name)
    )
    check_yes_link.click()

def fill_out_country(driver: WebDriver, country: str):
    driver.implicitly_wait(1)
    # parent div: //body/div[5]/div/div/div/div/div/div[1]/div[4]/div[2]/div[2]
    # parent ul: //body/div[5]/div/div/div/div/div/div[1]/div[4]/div[2]/div[2]/ul

    # where to export : country search
    # //body/div[8]/div/div/div/div/div/div[1]/div[3]/div[2]/div[2]
    # //body/div[8]/div/div/div/div/div/div[1]/div[3]/div[2]/div[2]/ul

    # country
    country_btn = find_element(
        driver, find_selector_by_name(SELECTORS, "add a target market")  # dashboard add country button
    )
    country_btn.click()

    # try:
    #     if driver.find_element_by_xpath("//button[contains(text(),'Got it')]").is_displayed():
    #         driver.find_element_by_xpath("//button[contains(text(),'Got it')]").click()
    # except:
    #     pass

    if 0 == len(country):
        # if country name is not provided from the test case, then select one of the random 5 countries listed on the browser
        path_random_country_element = "body > div:nth-child(13) > div > div > div > div > div > div.only-desktop > div.suggested-markets > ul > button:nth-child(" + str(
            random.randint(1, 5)) + ")"
        driver.find_element_by_css_selector(path_random_country_element).click()
    else:
        # search using the provide country name from the test case
        driver.find_element_by_css_selector("#search-input").clear()
        driver.find_element_by_css_selector("#search-input").send_keys(country)

        # look out for the list displayed after entering country name and select random/provided country
        ul_list_element = driver.find_element_by_xpath(
            "//body/div[11]/div/div/div/div/div/div[1]/div[4]/div[2]/div[2]/ul")

        section_elements = ul_list_element.find_elements_by_tag_name("section")
        logging.debug("length of section elements " + str(len(section_elements)))
        # select random section element and within that select a country
        index_random_element_to_be_selected = random.randint(0, len(section_elements) - 1)
        logging.debug("Index of section elements " + str(index_random_element_to_be_selected))
        section_element_selected = section_elements[index_random_element_to_be_selected]
        logging.debug(section_element_selected)

        div_elements = section_element_selected.find_elements_by_tag_name("div")  # 2 has to be present
        logging.debug("length of div elements " + str(len(div_elements)))
        level_1_div_element = div_elements[
            1]  # section_element_selected.find_element_by_class_name("p-t-s expand-section open")
        level_2_div_element = level_1_div_element.find_element_by_tag_name("div")
        span_elements = level_2_div_element.find_elements_by_tag_name("span")
        logging.debug("length of span elements " + str(len(span_elements)))
        # select random span element and within that select a country
        index_random_element_to_be_selected = random.randint(0, len(span_elements) - 1)
        span_element_selected = span_elements[index_random_element_to_be_selected]
        li_element = span_element_selected.find_element_by_tag_name("li")
        # finally arrived at country name button(s)
        buttons_elements = li_element.find_elements_by_tag_name("button")
        logging.debug("length of country button elements " + str(len(buttons_elements)))
        country_name_found = False
        for button_element in buttons_elements:
            if str(button_element.text).lower() == country.lower():
                country_name_found = True
                button_element.click()
                time.sleep(2)
                break
        if country_name_found == False:
            raise Exception("Country name could not be found " + str(country))

def fill_out_product(driver: WebDriver, product_name: str):
    product_btn = find_element(
        driver, find_selector_by_name(SELECTORS, "add a product")
    )
    product_btn.click()
    # search_again_top_bottom(driver)
    driver.implicitly_wait(1)
    driver.find_element_by_xpath("//body/div[9]/div/div/form/div[2]/div/div/div[2]/label/div/input").clear()
    driver.find_element_by_xpath("//body/div[9]/div/div/form/div[2]/div/div/div[2]/label/div/input").send_keys(
        product_name)
    driver.find_element_by_xpath("//body/div[9]/div/div/form/div[2]/div/div/div[2]/button/i").click()

    # Inorder to copy this code, 3 elements to be copied
    # as per the element path on the browser
    # save_product_btn, parent_1_div_element, search next button
    # def search_select_save_radio(driver: WebDriver):
    counter = 0
    while True:

        if counter >= 50:
            break
        # logging.debug("Counter: " + str(counter))

        driver.implicitly_wait(1)

        # check for save button
        save_btn_found = False
        try:
            save_product_btn = driver.find_element_by_xpath(
                "//body/div[9]/div/div/form/div[2]/div/span/div/section[1]/div[2]/button")
            save_btn_found = True
        except Exception as ex:
            logging.debug("save button not found.Exception: " + str(ex))

        if save_btn_found == True:
            logging.debug("Save button found")
            save_product_btn.click()
            return
        # look for div's and radio buttons
        parent_1_div_element = driver.find_element_by_xpath(
            "//body/div[9]/div/div/form/div[2]/div/span/div/section[1]/div")  # ("interaction grid m-v-xs")
        child_1_div_element = parent_1_div_element.find_element_by_tag_name("div")  # ("c-fullwidth")
        main_div_element = child_1_div_element.find_element_by_tag_name("div")  # "m-b-xs"
        # radio button labels
        label_elements = main_div_element.find_elements_by_tag_name("label")
        radio_elements = []
        for label_element in label_elements:
            radio_ele = None
            try:
                radio_ele = label_element.find_element_by_tag_name("input")
            except Exception as e:
                continue
            radio_elements.append(radio_ele)
        # logging.debug('number of labels - ' + str(len(radio_elements)))
        random_label_index = random.randint(0, len(radio_elements) - 1)
        # logging.debug('Index of radio buttons to be selected -> ' + str(random_label_index))

        radio_btn_selected = radio_elements[random_label_index]
        radio_btn_selected.click()

        driver.implicitly_wait(1)
        search_next_btn = find_element(
            driver, find_selector_by_name(SELECTORS, "search next button")
        )
        search_next_btn.click()

        counter += 1

def delete_all_route_to_market(driver: WebDriver, del_button_position: str):
    # 1,3,5,7,......
    # /html/body/main/div[2]/section[4]/div/div[2]/div/div/div[3]/textarea
    # /html/body/main/div[2]/section[4]/div/div[2]/div/div[2]/div[3]/textarea

    route_to_market_text_area_element_index = int(del_button_position)
    # //body/main/div[2]/section[4]/div/div[2]/div[2]/div/div[5]/div[1]/div[1]/div/textarea
    route_to_market_text_area_element_x_path = "//body/main/div[2]/section[4]/div/div[2]/div/div" \
                                               + "[" + str(
        route_to_market_text_area_element_index) + "]" + "/div[3]/textarea"
    route_to_market_text_area_text_exists = True
    time.sleep(2)
    # logging.debug(route_to_market_text_area_element_index)
    # logging.debug(route_to_market_text_area_element_x_path)
    try:
        route_to_market_text_area_text = driver.find_element_by_xpath(route_to_market_text_area_element_x_path).text
        if route_to_market_text_area_text == None or len(route_to_market_text_area_text) <= 0:
            route_to_market_text_area_text_exists = False
    except Exception as e:
        logging.error(e)
        route_to_market_text_area_text_exists = False
    # /html/body/main/div[2]/section[4]/div/div[2]/div/div[2]/div[4]/button/i
    # /html/body/main/div[2]/section[4]/div/div[2]/div/div/div[4]/button/i

    # del_button_position: 5,4,3,2,1
    route_to_market_div_element_xpath = "//body/main/div[2]/section[4]/div/div[2]/div/div" + "[" + del_button_position + "]"
    del_btn_ele_xpath = route_to_market_div_element_xpath + "/div[4]/button/i"
    driver.find_element_by_xpath(del_btn_ele_xpath).click()
    # logging.debug("del_button_position " + str(del_button_position))
    # time.sleep(2)
    if route_to_market_text_area_text_exists == True:
        driver.implicitly_wait(1)
        # 12,13,14,15.......
        # 12 + (1 - 1), 12 + (2 - 1), 12 + (3 - 1), 12 + (4 - 1),.........
        # /html/body/div[16]/div/div/div/div[2]/div[2]/button[1]/i
        # /html/body/div[13]/div/div/div/div[2]/div[2]/button[1]
        delete_msg_yes_index = int(12 + (int(del_button_position) - 1))
        delete_message_yes_element_xpath = "//body/div" + "[" + str(
            delete_msg_yes_index) + "]" + "/div/div/div/div[2]/div[2]/button[1]"
        logging.debug(delete_message_yes_element_xpath)
        delete_message_yes_element = driver.find_element_by_xpath(delete_message_yes_element_xpath)
        delete_message_yes_element.click()
        # time.sleep(1)
    else:
        logging.debug(
            "route_to_market_text_area_text_exists is False for del_button_position " + str(del_button_position))


# -*- coding: utf-8 -*-
"""
Created on Sun Jun 12 2020

@author: gari.ciodaro.guerra

open ports of RedShift for TCP connections.
"""

import boto3
import configparser

config = configparser.ConfigParser()
config.read_file(open('./configuration/redshift.cfg'))

# load configuration parameters
KEY                    = config.get('AWS','KEY')
SECRET                 = config.get('AWS','SECRET')
DWH_CLUSTER_IDENTIFIER = config.get("DWH","DWH_CLUSTER_IDENTIFIER")
DWH_PORT               = config.get("DWH","DWH_PORT")

ec2 = boto3.resource('ec2',
    region_name='us-east-2',
    aws_access_key_id=KEY,
    aws_secret_access_key=SECRET)

#initilizied redshift client
redshift = boto3.client('redshift',
                        region_name='us-east-2',
                        aws_access_key_id=KEY,
                        aws_secret_access_key=SECRET)

cluster_props=redshift.describe_clusters(
    ClusterIdentifier=DWH_CLUSTER_IDENTIFIER)['Clusters'][0]

if __name__=="__main__":

    try:
        vpc = ec2.Vpc(id=cluster_props['VpcId'])
        defaultSg = list(vpc.security_groups.all())[0]
        print(defaultSg)
        defaultSg.authorize_ingress(
            GroupName=defaultSg.group_name,
            CidrIp='0.0.0.0/0',
            IpProtocol='TCP',
            FromPort=int(DWH_PORT),
            ToPort=int(DWH_PORT)
        )
    except Exception as e:
        print(e)


totalQueryVectors = []
totalQueryVectors.append(query_vector)
while(1):
    relFedd = str(raw_input("Enter space seprated relevant documents (-1 to exit): "))
    relFedd = relFedd.split()

    if int(relFedd[0])!=-1:

        relVectors = []
        non_relVectors = []

        for i in range(len(relFedd)):
            ind_val = int(relFedd[i])
            t_Vect = tfresults[sorted_sim_results[ind_val][0]]
            relVectors.append(t_Vect)

        for i in range(len(sorted_sim_results)):
            if str(i) not in relFedd:
                t_Vect = tfresults[sorted_sim_results[i][0]]
                non_relVectors.append(t_Vect)

        mean_rel_vector = np.zeros(len(relVectors[0]))

        for i in range(len(relVectors)):
            tv = np.array(relVectors[i])
            mean_rel_vector+=tv
        mean_rel_vector = mean_rel_vector/len(relVectors)

        mean_non_rel_vector = np.zeros(len(non_relVectors[0]))

        for i in range(len(non_relVectors)):
            tv = np.array(non_relVectors[i])
            mean_non_rel_vector+=tv
        mean_non_rel_vector = mean_non_rel_vector/len(non_relVectors)

        query_vector = (0.1 * query_vector) + (0.75 * mean_rel_vector) - (0.25 * mean_non_rel_vector)
        cosine_sim_results = {}

        query_vector = np.array(query_vector)

        for k,v in tfresults.iteritems():
            doc_vector = np.array(v)
            simval = cos_sim(query_vector,doc_vector)
            cosine_sim_results[k] = simval

        k_value = 10

        sorted_sim_results = sorted(cosine_sim_results.items(), key=operator.itemgetter(1),reverse=True)
        sorted_sim_results = sorted_sim_results[:k_value]

        print()
        for i in range(len(sorted_sim_results)):
            print(str(i)+"\t"+str(count_to_name[sorted_sim_results[i][0]])+"\t"+str(sorted_sim_results[i][1]))
        totalQueryVectors.append(query_vector)
    else:
        break


import hashlib
import shutil
from pathlib import Path
from pathlib import PurePath
from urllib.parse import urlsplit
from urllib.request import urlopen

import pandas as pd
import pytest

TEST_DATA_URL = "https://ndownloader.figshare.com/files/25747817"
TEST_DATA_MD5 = "1ec89bde544c3c4bc400d5b75315921e"

def md5(fn):
    m = hashlib.md5()
    with open(fn, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            m.update(chunk)
    return m.hexdigest()

@pytest.fixture(scope="session")
def data_pth(tmp_path_factory) -> Path:
    """download the smallest aperio test image svs or use local"""
    filename = PurePath(urlsplit(TEST_DATA_URL).path).name
    data_dir = Path(__file__).parent / "data"
    data_dir.mkdir(parents=True, exist_ok=True)
    data_fn = data_dir / filename

    if not data_fn.is_file():
        # download svs from openslide test images
        with urlopen(TEST_DATA_URL) as response, open(
            data_fn, "wb"
        ) as out_file:
            shutil.copyfileobj(response, out_file)

        if md5(data_fn) != TEST_DATA_MD5:  # pragma: no cover
            shutil.rmtree(data_fn)
            pytest.fail("incorrect md5")

    yield data_fn.absolute()

@pytest.fixture()
def data(data_pth) -> pd.DataFrame:
    yield pd.read_csv(data_pth)


import datetime, json
from django.http import JsonResponse
from django.shortcuts import redirect, render
from api.models import links, authTokens
from django.core.validators import URLValidator
from django.utils.crypto import get_random_string
import random
# Create your views here.

def regAPI(request):
    if request.method == 'POST':
        api_key = request.POST['api_key']
        email = request.POST['email']

        if isLoggedin(request):
            if authTokens.objects.filter(email=email, api_key=api_key, status='Active').exists():
                r_api_key = get_random_string(10)
                authTokens.objects.filter(email=email, api_key=api_key).update(api_key=r_api_key)
                request.session['user'] = {'api_key' : r_api_key }
                return JsonResponse(json.dumps({'api_key': r_api_key, 'msg' : 'API_KEY Updated'}), content_type="application/json", safe=False)
            else:
                return redirect('/')
        else:
            return redirect('/')

    else:
        return redirect('/')

def isLoggedin(request):
    try:
        if(request.session['user'].get('isLoggedin') == True and request.session['user'].get('email') != None and request.session['user'].get('api_key') != None):
            return True
        else:
            return False
    except:
            return False
def shoot_out(request, lid):
    try:
        if links.objects.filter(lid=lid).exists():
            qs = links.objects.filter(lid=lid).first()
            r_url = qs.link
            validate = URLValidator()
            try:
                validate(qs.link)
                return redirect(r_url)
            except:
                return redirect('http://'+ qs.link)
        else:
            return render(request, 'wrong-url.html')
    except:
         return render(request, 'wrong-url.html')

def index(request):
    return render(request, 'home.html')

def account(request):
    if isLoggedin(request):
        if request.session['user'].get('status') == 'Active':
            return render(request, 'acc.html', {'context':request.session['user']})
        else:
            return render(request, 'login.html', {'context':request.session['user'], 'div': 'otp'})

    elif request.method == 'POST':
        email = request.POST.get('email')
        psw = request.POST.get('psw')
        if authTokens.objects.filter(email=email, psw=psw).exists():
            qs = authTokens.objects.filter(email=email, psw=psw).first()
            request.session['user'] = {
                'isLoggedin' : True,
                'api_key': qs.api_key,
                'email' : qs.email,
                'status': qs.status
            }
            if qs.status == 'Active':
                return render(request, 'acc.html', {'context': request.session['user']})
            else:
                return render(request, 'login.html', {'context':request.session['user'], 'div': 'otp'})
        elif authTokens.objects.filter(email=email).exists():
            return render(request, 'login.html', {'context': 'Wrong password. <a href="/acc/reset-psw/">reset password<a/>'})
        else:
            otp = random.randint(100000,999999)
            api_key = get_random_string(10)
            created_on = datetime.datetime.now()
            su = authTokens(email=email, psw=psw, otp=otp, api_key=api_key, status='Inactive', created_on=created_on)
            su.save()
            request.session['user'] = {
                'isLoggedin' : True,
                'api_key': api_key,
                'email' : email,
                'status': 'Inactive'
            }
            return render(request, 'login.html', {'context':request.session['user'], 'div': 'otp'})
    else:
        return render(request, 'login.html')

def otp(request):
    if request.method == 'POST':
        email = request.POST.get('email')
        otp = request.POST.get('otp')
        if authTokens.objects.filter(email=email, otp=otp).exists():
            qs = authTokens.objects.filter(email=email, otp=otp).first()
            r_otp = random.randint(100000,999999)
            authTokens.objects.filter(email=email, otp=otp).update(otp=r_otp)
            authTokens.objects.filter(email=email, otp=r_otp).update(status='Active')
            request.session['user'] = {
                'isLoggedin' : True,
                'api_key': qs.api_key,
                'email' : qs.email,
                'status': qs.status
            }
            return render(request, 'acc.html', {'context': request.session['user']})
        else:
            return render(request, 'login.html', {'context':request.session['user'], 'div': 'otp', 'msg' : 'Wrong otp'})
    else:
        return redirect('/')

def logout(request):
    try:
        del request.session['user']
        return redirect('/acc/user')
    except:
        return redirect('/acc/user')


import os, time, goal_dirs
from shutil import move
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
move_dir = "/home/finn/Downloads/sort"

os.chdir(move_dir)
def moveall():
    for item in os.listdir(move_dir):
        for extension in goal_dirs:
            if item.endswith(extension):
                file_path = os.path.abspath(item)
                if os.path.isdir(goal_dirs[extension]) == False:
                    os.mkdir(goals_dirs[extensions])
                shutil.move(file_path, goal_dirs[extension])
                os.remove(file_path)

if __name__ == "__main__":
    patterns = "*"
    ignore_patterns = ""
    ignore_directories = True
    case_sensitive = False
    my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)

    def on_created(event):
        print('file was created')
        moveall()
        print('created file moved')

    def on_deleted(event):
        print('file was deleted')

    def on_modified(event):
        print('file was modified')
        moveall()
        print('modified file moved')

    def on_moved(event):
        print('file was moved')
        moveall()

    my_event_handler.on_created = on_created
    my_event_handler.on_deleted = on_deleted
    my_event_handler.on_modified = on_modified
    my_event_handler.on_moved = on_moved

    go_recursively = True
    my_observer = Observer()
    my_observer.schedule(my_event_handler, move_dir, recursive=go_recursively)

    my_observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        my_observer.stop()
        my_observer.join()


import sys
input = sys.stdin.readline

n = int(input())
m = []

for _ in range(n):
    x, y = map(int, input().split())
    m.append([x,y])

num_sort = sorted(m, key = lambda x : (x[0], x[1]))
for i in range(n):
    print(*num_sort[i], end='\n')


import requests
from bs4 import BeautifulSoup
import pprint

def sort_stories_by_votes(list):
    return sorted(list, key=lambda k: k['votes'], reverse = True);

def create_custom_hn():
    hn = []

    for i in range(1, 2):
        response = requests.get('https://news.ycombinator.com/news?p=' + str(i))
        soup = BeautifulSoup(response.text, 'html.parser')
        links = soup.select('.storylink')
        votes = soup.select('.score')
        subtext = soup.select('.subtext')

        for idx, item in enumerate(links):
            title = item.getText()
            href = item.get('href', '')
            vote = subtext[idx].select('.score')

            if len(vote):
                points = int(vote[0].getText().replace(' points', ''))

                if points > 99:
                    hn.append({'title': title, 'link': href, 'votes': points})

    return sort_stories_by_votes(hn)

pprint.pprint(create_custom_hn())


import logging
import asyncio
import time
import matplotlib.pyplot as plt
import numpy as np
import sys

from aiocoap import *

logging.basicConfig(level=logging.INFO)

addr='[fd00::212:4b00:1204:cb03]' #'134.102.218.18' #coap.me #'localhost'
port='5683'
file=['sen/bar/temp', 'sen/hdc/t']
clock=1 #min 5 sec

async def get(uri):
    protocol = await Context.create_client_context()
    request = Message(code=GET, uri=uri)
    response = await protocol.request(request).response
    return response.payload

#######################################

list_values  = [ [] for _ in file]
end = False
print("recuperation des infos sur :")
for f in file: print('coap://'+addr+':'+port+'/'+f)

k=0
while not end:
    print("\rget "+str(k), end='')
    k+=1
    for i,f in enumerate(file):
        try:
            uri = 'coap://'+addr+':'+port+'/'+f
            raw_res = asyncio.get_event_loop().run_until_complete(get(uri)).decode("utf-8")
            #print(raw_res)
            res = float(raw_res)
            list_values[i].append( res )

        except KeyboardInterrupt:
            end = True
    try:
        if not end:
            time.sleep(clock)
    except:
        end = True

print("fin de recuperation")

print(list_values)

means = [np.mean(vs) for vs in list_values]

for i in range(len(list_values)):
    plt.plot(list_values[i], 'o-', label=file[i])
    plt.plot(np.ones(len(list_values[i]))*means[i], 'o', label='mean '+file[i])

plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
           ncol=2, mode="expand", borderaxespad=0.)
plt.show()


from setuptools import setup, find_packages

with open("README.md", "r", encoding="utf-8") as f:
    long_description = f.read()

setup(
    name="smartmuv",
    description="An EVM-compatible Solidity smart contract state analysis and extraction tool.",
    url="https://github.com/waizkhan7/smartmuv",
    author="NextKore",
    version="0.2.0",
    packages=find_packages(),
    python_requires=">=3.8",
    install_requires=[
        "packaging",
        "crytic-compile>=0.3.3,<0.4.0",
        "web3>=6.0.0",
        "eth-abi>=4.0.0",
        "eth-typing>=3.0.0",
        "eth-utils>=2.1.0",
        "slither-analyzer>=0.9.0",
        "solc-select>=1.0.4",
        "py-solc-x>=1.1.1",
        "solidity-parser>=0.1.1",
        "hexbytes>=0.2.2",
    ],
    license="GNU-3.0",
    long_description=long_description,
    long_description_content_type="text/markdown"
    )


import numpy as np
import csv
import json
from chardet import detect

import os
import pickle
import pandas as pd
from gensim.models import KeyedVectors
from gensim.test.utils import get_tmpfile

path = 'C:/users/anjali/environments/acl'
#with open('C:/users/anjali/environments/acl/data/name_to_ind.pkl','rb') as f:
#	name_to_ind = pickle.load(f)

data = open('C:/users/anjali/environments/acl/data/HanDeSeT.csv',encoding='utf-8')
debates = list(csv.reader(data))[1:]
embeddings = []
#names = []
df = pd.read_csv('C:/users/anjali/environments/acl/data/HanDeSeT.csv')
names = np.unique(df['name'].values)
name_to_ind = dict(map(lambda x:(x[1], x[0]), enumerate(names)))
namess = []

vectors = open('C:/Users/ANJALI/environments/acl/data/danmf_embeddings/agres_64-128.csv')
vecs = list(csv.reader(vectors))[1:]
print(type(vecs[10]))

with open('C:/users/anjali/environments/acl/data/danmf_embeddings/agres_64-128.vec','w') as f:
	count = 0
	#f.write('599 128'+'\n')
	for vec in vecs:
		newlist = []
		newlist.append(int(float(vec[0])))
		for i in vec[1:]:
			newlist.append(float(i))
		for i in newlist:
			f.write(str(i)+' ')
		f.write('\n')

'''

fname = get_tmpfile("C:/users/anjali/environments/acl/data/danmf_embeddings/danmf_supres.vec")
word_vectors = KeyedVectors.load_word2vec_format(fname)
for row in debates:
	namess.append(row[14])
	embeddings.append(word_vectors[str(name_to_ind[row[14]])])

print(len(embeddings))

with open('C:/users/anjali/environments/acl/data/danmf_embeddings/danmf_sup.vec','w') as f:
	f.write(str(embeddings))
#print(word_vectors[str(name_to_ind[names[0]])])
'''
'''
es1 = []
es2 = []
ea1 = []
ea2 = []
with open(path+'/data/sup_0.txt','r') as f:
	for line in f:
		es1.append(line.rstrip().split()[0])
		es2.append(line.rstrip().split()[1])

with open(path+'/data/against_0.txt','r') as f:
	for line in f:
		ea1.append(line.rstrip().split()[0])
		ea2.append(line.rstrip().split()[1])

sup = pd.DataFrame()
sup['es1'] = es1
sup['es2'] = es2
ag = pd.DataFrame()
ag['ea1'] = ea1
ag['ea2'] = ea2

sup.to_csv(path+'/data/sup_0.csv')
ag.to_csv(path+'/data/ag_0.csv')
'''


#!/usr/bin/env python
'''
Package providing helper classes and functions for performing graph search operations for planning.
'''
import sys
import numpy as np
import heapq
import matplotlib.pyplot as plotter
from math import hypot, sqrt

_DEBUG = False
_DEBUG_END = True
_ACTIONS = ['u','d','l','r']
_ACTIONS_2 = ['u','d','l','r','ne','nw','sw','se']
# non-holonomic - only turn either direction and step forward
# 0 is pointing up
_ACTIONS_3 = ['forward','0','45','90', '135','180','225','270','315']
_T = 2
_X = 1
_Y = 0
_GOAL_COLOR = 0.75
_INIT_COLOR = 0.25
_PATH_COLOR_RANGE = _GOAL_COLOR-_INIT_COLOR
_VISITED_COLOR = 0.9

class GridMap:
    '''
    Class to hold a grid map for navigation. Reads in a map.txt file of the format
    0 - free cell, x - occupied cell, g - goal location, i - initial location.
    Additionally provides a simple transition model for grid maps and a convience function
    for displaying maps.
    '''
    def __init__(self, map_path=None):
        '''
        Constructor. Makes the necessary class variables. Optionally reads in a provided map
        file given by map_path.

        map_path (optional) - a string of the path to the file on disk
        '''
        self.rows = None
        self.cols = None
        self.goal = None
        self.init_pos = None
        self.occupancy_grid = None
        if map_path is not None:
            self.read_map(map_path)

    def read_map(self, map_path):
        '''
        Read in a specified map file of the format described in the class doc string.

        map_path - a string of the path to the file on disk
        '''
        map_file = open(map_path,'r')
        lines = [l.rstrip().lower() for l in map_file.readlines()]
        map_file.close()
        self.rows = len(lines)
        self.cols = max([len(l) for l in lines])
        if _DEBUG:
            print('rows', self.rows)
            print('cols', self.cols)
            print(lines)
        self.occupancy_grid = np.zeros((self.rows, self.cols), dtype=np.bool)
        for r in range(self.rows):
            for c in range(self.cols):
                if lines[r][c] == 'x':
                    self.occupancy_grid[r][c] = True
                if lines[r][c] == 'g':
                    self.goal = (r,c)
                elif lines[r][c] == 'i':
                    self.init_pos = (r,c,0)

    def is_goal(self,s):
        '''
        Test if a specifid state is the goal state

        s - tuple describing the state as (row, col) position on the grid.

        Returns - True if s is the goal. False otherwise.
        '''
        return (s[_X] == self.goal[_X] and
                s[_Y] == self.goal[_Y])

    def transition(self, s, a):
        '''
        Transition function for the current grid map.

        s - tuple describing the state as (row, col) position on the grid.
        a - the action to be performed from state s

        returns - s_prime, the state transitioned to by taking action a in state s.
        If the action is not valid (e.g. moves off the grid or into an obstacle)
        returns the current state.
        '''
        new_pos = list(s[:])
        # Ensure action stays on the board
        if a == 'u':
            if s[_Y] > 0:
                new_pos[_Y] -= 1
        elif a == 'd':
            if s[_Y] < self.rows - 1:
                new_pos[_Y] += 1
        elif a == 'l':
            if s[_X] > 0:
                new_pos[_X] -= 1
        elif a == 'r':
            if s[_X] < self.cols - 1:
                new_pos[_X] += 1

        elif a == 'ne':
            if s[_Y] > 0 and s[_X] < self.cols - 1:
                new_pos[_X] += 1
                new_pos[_Y] -= 1
        elif a == 'nw':
            if s[_Y] > 0 and  s[_X] > 0:
                new_pos[_X] -= 1
                new_pos[_Y] -= 1
        elif a == 'sw':
            if s[_Y] < self.rows - 1 and s[_X] > 0:
                new_pos[_X] -= 1
                new_pos[_Y] += 1
        elif a == 'se':
            if s[_Y] < self.rows - 1 and s[_X] < self.cols - 1:
                new_pos[_X] += 1
                new_pos[_Y] += 1

        # actions for non-holonomic robot (forward and angle)
        elif a == '0':
            new_pos[_T] = 0
        elif a == '45':
            new_pos[_T] = 45
        elif a == '90':
            new_pos[_T] = 90
        elif a == '135':
            new_pos[_T] = 135
        elif a == '180':
            new_pos[_T] = 180
        elif a == '225':
            new_pos[_T] = 225
        elif a == '270':
            new_pos[_T] = 270
        elif a == '315':
            new_pos[_T] = 315

        elif a == 'forward':
            if s[_T] == 0:
                if s[_Y] > 0:
                    new_pos[_Y] -= 1
            if s[_T] == 45:
                if s[_Y] > 0 and s[_X] < self.cols - 1:
                    new_pos[_X] += 1
                    new_pos[_Y] -= 1
            if s[_T] == 90:
                if s[_X] < self.cols - 1:
                    new_pos[_X] += 1
            if s[_T] == 135:
                if s[_Y] < self.rows - 1 and s[_X] < self.cols - 1:
                    new_pos[_X] += 1
                    new_pos[_Y] += 1
            if s[_T] == 180:
                if s[_Y] < self.rows - 1:
                    new_pos[_Y] += 1
            if s[_T] == 225:
                if s[_Y] < self.rows - 1 and s[_X] > 0:
                    new_pos[_X] -= 1
                    new_pos[_Y] += 1
            if s[_T] == 270:
                if s[_X] > 0:
                    new_pos[_X] -= 1
            if s[_T] == 315:
                if s[_Y] > 0 and  s[_X] > 0:
                    new_pos[_X] -= 1
                    new_pos[_Y] -= 1

        else:
            print('Unknown action:', str(a))

        # Test if new position is clear
        if self.occupancy_grid[new_pos[0], new_pos[1]]:
            s_prime = tuple(s)
        else:
            s_prime = tuple(new_pos)
        return s_prime

    def display_map(self, path=[], visited={}, filename=None):
        '''
        Visualize the map read in. Optionally display the resulting plan and visisted nodes

        path - a list of tuples describing the path take from init to goal
        visited - a set of tuples describing the states visited during a search
        filename - relative path to file where image will be saved
        '''
        display_grid = np.array(self.occupancy_grid, dtype=np.float32)

        # Color all visited nodes if requested
        for v in visited:
            display_grid[v[0],v[1]] = _VISITED_COLOR
        # Color path in increasing color from init to goal
        for i, p in enumerate(path):
            disp_col = _INIT_COLOR + _PATH_COLOR_RANGE*(i+1)/len(path)
            display_grid[p[0],p[1]] = disp_col

        display_grid[self.init_pos[0],self.init_pos[1]] = _INIT_COLOR
        display_grid[self.goal] = _GOAL_COLOR

        # Plot display grid for visualization
        imgplot = plotter.imshow(display_grid)
        # Set interpolation to nearest to create sharp boundaries
        imgplot.set_interpolation('nearest')
        # Set color map to diverging style for contrast
        imgplot.set_cmap('Spectral')
        if filename is not None:
            plotter.savefig(filename)
        plotter.show()

    def uninformed_heuristic(self, s):
        '''
        Example of how a heuristic may be provided. This one is admissable, but dumb.

        s - tuple describing the state as (row, col) position on the grid.

        returns - floating point estimate of the cost to the goal from state s
        '''
        return 0.0

    def euclidean_heuristic(self, s):
        '''
        Euclidian distance heuristic
        '''
        a = abs(s[0]-self.goal[0])
        b = abs(s[1]-self.goal[1])
        dist = sqrt((a**2)+(b**2))
        return dist

    def manhattan_heuristic(self, s):
        '''
        Manhattan distance heuristic
        '''
        a = abs(s[0]-self.goal[0])
        b = abs(s[1]-self.goal[1])
        dist = a + b
        return dist

class SearchNode:
    def __init__(self, s, A, parent=None, parent_action=None, cost=0):
        '''
        s - the state defining the search node
        A - list of actions
        parent - the parent search node
        parent_action - the action taken from parent to get to s
        '''
        self.parent = parent
        self.cost = cost
        self.parent_action = parent_action
        self.state = s[:]
        self.actions = A[:]

    def __str__(self):
        '''
        Return a human readable description of the node
        '''
        return str(self.state) + ' ' + str(self.actions)+' '+str(self.parent)+' '+str(self.parent_action)
    def __lt__(self, other):
        return self.cost < other.cost

class PriorityQ:
    '''
    Priority queue implementation with quick access for membership testing
    Setup currently to only with the SearchNode class
    '''
    def __init__(self):
        '''
        Initialize an empty priority queue
        '''
        self.l = [] # list storing the priority q
        self.s = set() # set for fast membership testing

    def __contains__(self, x):
        '''
        Test if x is in the queue
        '''
        return x in self.s

    def push(self, x, cost):
        '''
        Adds an element to the priority queue.
        If the state already exists, we update the cost
        '''
        # added "cost < self.get_cost(x)"
        if x.state in self.s:
            return self.replace(x, cost)
        heapq.heappush(self.l, (cost, x))
        self.s.add(x.state)

    def pop(self):
        '''
        Get the value and remove the lowest cost element from the queue
        '''
        x = heapq.heappop(self.l)
        self.s.remove(x[1].state)
        return x[1]

    def peak(self):
        '''
        Get the value of the lowest cost element in the priority queue
        '''
        x = self.l[0]
        return x[1]

    def __len__(self):
        '''
        Return the number of elements in the queue
        '''
        return len(self.l)

    def replace(self, x, new_cost):
        '''
        Removes element x from the q and replaces it with x with the new_cost
        '''
        for y in self.l:
            if x.state == y[1].state:
                self.l.remove(y)
                self.s.remove(y[1].state)
                break
        heapq.heapify(self.l)
        self.push(x, new_cost)

    def get_cost(self, x):
        '''
        Return the cost for the search node with state x.state
        '''
        for y in self.l:
            if x.state == y[1].state:
                return y[0]

    def __str__(self):
        '''
        Return a string of the contents of the list
        '''
        return str(self.l)

def dfs(init_state, f, is_goal, actions):
    '''
    Perform depth first search on a grid map.

    init_state - the intial state on the map
    f - transition function of the form s_prime = f(s,a)
    is_goal - function taking as input a state s and returning True if its a goal state
    actions - set of actions which can be taken by the agent

    returns - ((path, action_path), visited) or None if no path can be found
    path - a list of tuples. The first element is the initial state followed by all states
        traversed until the final goal state
    action_path - the actions taken to transition from the initial state to goal state
    '''
    frontier = [] # Search stack
    n0 = SearchNode(init_state, actions)
    visited = []
    frontier.append(n0)
    while len(frontier) > 0:
        # Peak last element
        n_i = frontier.pop()
        if n_i.state not in visited:
            visited.append(n_i.state)
            if is_goal(n_i.state):
                path, action_path = backpath(n_i)
                return (path, visited)
            else:
                for a in actions:
                    s_prime = f(n_i.state, a)
                    n_prime = SearchNode(s_prime, actions, n_i, a)
                    frontier.append(n_prime)
    return None

def idfs(init_state, f, is_goal, actions, m_max = 10000):
    '''
    Iterative Deepening Search
    m_max is the maximum depth IDFS will be allowed to go to
    m is current depth the algorithm is running DFS to
    '''
    m = 1
    # visited set for entire IDFS
    visited = {}
    while m <= m_max:
        frontier = [] # Search stack
        n0 = SearchNode(init_state, actions)
        frontier.append(n0)
        # visited set for iteration
        visited_local = []
        while len(frontier) > 0:
            n_i = frontier.pop()
            x1,y1 = backpath(n_i)
            n_i_depth = len(x1) - 1
            # account for shorter paths
            if n_i.state not in visited_local or n_i_depth < visited[n_i.state]:
                visited_local.append(n_i.state)
                visited[n_i.state] = n_i_depth
                if is_goal(n_i.state):
                    path, action_path = backpath(n_i)
                    return (path, visited)
                else:
                    for a in actions:
                        s_prime = f(n_i.state, a)
                        n_prime = SearchNode(s_prime, actions, n_i, a)
                        x,y = backpath(n_prime)
                        node_depth = len(x) - 1
                        # only DFS to depth m
                        if node_depth <= m:
                            frontier.append(n_prime)
        # increase DFS depth if goal not found
        m = m + 1
    return None

def bfs(init_state, f, is_goal, actions):
    '''
    Perform breadth first search on a grid map.

    init_state - the intial state on the map
    f - transition function of the form s_prime = f(s,a)
    is_goal - function taking as input a state s and returning True if its a goal state
    actions - set of actions which can be taken by the agent

    returns - ((path, action_path), visited) or None if no path can be found
    path - a list of tuples. The first element is the initial state followed by all states
        traversed until the final goal state
    action_path - the actions taken to transition from the initial state to goal state
    '''
    # same as dfs except fifo queue instead of lifo stack

    frontier = [] # Search queue
    n0 = SearchNode(init_state, actions)
    visited = []
    frontier.append(n0)
    while len(frontier) > 0:
        # Peak last element
        n_i = frontier.pop(0)
        if n_i.state not in visited:
            visited.append(n_i.state)
            if is_goal(n_i.state):
                path, action_path = backpath(n_i)
                return (path, visited)
            else:
                for a in actions:
                    s_prime = f(n_i.state, a)
                    n_prime = SearchNode(s_prime, actions, n_i, a)
                    frontier.append(n_prime)
    return None

def uniform_cost_search(init_state, f, is_goal, actions):
    frontier = PriorityQ() # priority queue
    n0 = SearchNode(init_state, actions, cost = 0)
    visited = []
    frontier.push(n0, n0.cost)
    while len(frontier) > 0:
        # Expand lowest cost node
        n_i = frontier.pop()
        if n_i.state not in visited:
            visited.append(n_i.state)
            if is_goal(n_i.state):
                path, action_path = backpath(n_i)
                return (path, visited)
            else:
                for a in actions:
                    s_prime = f(n_i.state, a)
                    n_prime = SearchNode(s_prime, actions, n_i, a)
                    # only add node and its cost if not in p-queue yet
                    n_prime.cost = costpath(n_prime)
                    if frontier.get_cost(n_prime) is None: # if n_prime isn't in the queue
                        frontier.push(n_prime, n_prime.cost)
                    # if new cost to node is less than another path's cost
                    elif n_prime.cost < frontier.get_cost(n_prime):
                        frontier.push(n_prime, n_prime.cost)
    return None

def a_star_search(init_state, f, is_goal, actions, h):
    '''
    init_state - value of the initial state
    f - transition function takes input state (s), action (a), returns s_prime = f(s, a)
        returns s if action is not valid
    is_goal - takes state as input returns true if it is a goal state
        actions - list of actions available
    h - heuristic function, takes input s and returns estimated cost to goal
        (note h will also need access to the map, so should be a member function of GridMap)
    '''
    frontier = PriorityQ() # priority queue
    n0 = SearchNode(init_state, actions, cost = 0)
    visited = []
    frontier.push(n0, n0.cost)
    while len(frontier) > 0:
        # Expand lowest cost node
        n_i = frontier.pop()
        if n_i.state not in visited:
            visited.append(n_i.state)
            if is_goal(n_i.state):
                path, action_path = backpath(n_i)
                return (path, visited)
            else:
                for a in actions:
                    s_prime = f(n_i.state, a)
                    n_prime = SearchNode(s_prime, actions, n_i, a)
                    # only add node and its cost if not in p-queue yet
                    # f = g + h
                    n_prime.cost = costpath(n_prime) + h(n_prime.state)

                    if frontier.get_cost(n_prime) is None: # if n_prime isn't in the queue
                        frontier.push(n_prime, n_prime.cost)
                    elif n_prime.cost < frontier.get_cost(n_prime):
                        frontier.push(n_prime, n_prime.cost)
    return None

def backpath(node):
    '''
    Function to determine the path that lead to the specified search node

    node - the SearchNode that is the end of the path

    returns - a tuple containing (path, action_path) which are lists respectively of the states
    visited from init to goal (inclusive) and the actions taken to make those transitions.
    '''
    path = []
    action_path = []
    # add goal node to path
    path.append(node.state)
    # run until start node is hit
    while node.parent is not None:
        # add parent node to the path
        node = node.parent
        path.append(node.state)
    # put path in order of start to finish
    path.reverse()

    # action path (not complete yet)
    # # run until start node is hit
    # while node.parent is not None:
    #     # add parent node to the path
    #     action_path.append(node.parent_action)
    #     node = node.parent

    return (path, action_path)

def costpath(node):
    '''
    Function to backtrack the cost of the node
    '''
    cost = 0
    # run until start node is hit
    while node.parent is not None:
        #node = node.parent
        if node.parent_action in ['u','d','l','r']:
            cost = cost + 1
        if node.parent_action in ['ne','nw','sw','se']:
            cost = cost + 1.5

        # non-holonomic
        #   diagonal
        if node.parent_action == 'forward' and node.parent.state[_T] in [45,135,225,315]:
            cost = cost + 1.5
        # up down left right
        if node.parent_action == 'forward' and node.parent.state[_T] in [0,90,180,270]:
            cost = cost + 1
        #   turning
        if node.parent_action in ['0','45','90', '135','180','225','270','315']:
            cost = cost + .25
        node = node.parent
    return (cost)


# 서로 다른 L개의 알파벳 소문자들로 구성
# 최소 한 개의 모음(a, e, i, o, u)과 최소 두 개의 자음
# 암호를 이루는 알파벳이 암호에서 증가하는 순서로 배열
# 암호로 사용했을 법한 문자의 종류는 C가지
def dfs(now, r):    # now : 지금 살펴볼 문자의 idx, r : 현재 고른 문자의 개수
    if r + (C - now) < L:   # 고른 문자 + 고를 수 있는 문자의 최대 개수
        return
    if r == L:  # 다 고른 상황
        aeiou = 0
        for i in range(L):
            if chosen[i] in ['a', 'e', 'i', 'o', 'u']:
                aeiou += 1
        if aeiou >= 1 and L-aeiou >= 2:
            print(''.join(chosen))
        return
    for i in range(now, C):
        chosen.append(alpha[i])
        dfs(i+1, r+1)
        chosen.pop()

L, C = map(int, input().split())
alpha = sorted(input().split())
chosen = []
dfs(0, 0)


class Solution:
    # @param A : list of integers
    # @return an integer
    def solve(self, A):
        def isPrime(num):
            root = int(math.sqrt(num))
            for i in range(2,root+1):
                if num%i==0:
                    return False
            return True

        mod = 1000000007
        count = 1
        for elem in A:
            if elem > 1 and isPrime(elem):
                count = (count%mod * 2)%mod

        return count-1

Advanced/08 Advanced DSA : Maths - Prime Numbers /Assignment/Q4. Prime Subsequences/Prime Subsequences.py