from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import TimeoutException, NoSuchElementException, ElementNotInteractableException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import pandas as pd
from datetime import datetime
import time

urls = [
    'https://www.tradingview.com/markets/stocks-usa/market-movers-all-stocks/',
    'https://www.tradingview.com/markets/stocks-usa/market-movers-high-dividend/',
    'https://www.tradingview.com/markets/stocks-usa/market-movers-large-cap/',
    'https://www.tradingview.com/markets/stocks-usa/market-movers-largest-employers/',
    'https://www.tradingview.com/markets/stocks-usa/market-movers-highest-net-income/',
    'https://www.tradingview.com/markets/stocks-usa/market-movers-gainers/',
    'https://www.tradingview.com/markets/stocks-usa/market-movers-losers/'
]

options_ = webdriver.ChromeOptions()
options_.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
+"AppleWebKit/537.36 (KHTML, like Gecko)"
+"Chrome/87.0.4280.141 Safari/537.36")

browser = webdriver.Chrome(options=options_)

browser.implicitly_wait(3)
browser.maximize_window()

for url in urls:
    browser.get(url)
    file_base_name = url.split('/')[-2]
    # print(file_base_name)
    xlwriter = pd.ExcelWriter(file_base_name + '.xlsx')

    categories = browser.find_elements(By.CLASS_NAME, 'content-vcCjkHCG')

    for category in categories:
        print(f'Processing Report: {category.text}')
        try:
            try:
                browser.find_element(By.XPATH, f'//span[text()="{category.text}"]').click()
                time.sleep(1)
            except ElementNotInteractableException:
                pass

            # load_more = True
            # counter = 0
            # max_counter = 3

            # while load_more:
            #     try:
            #         browser.find_element(By.CLASS_NAME, 'loadButton-59hnCnPW').click()
            #         time.sleep(1)
            #         if counter > max_counter:
            #             load_more = False
            #         counter += 1
            #     except ElementNotInteractableException:
            #         load_more = False

            df = pd.read_html(browser.page_source)[1]
            df.replace('—', '', inplace=True)
            df.to_excel(xlwriter, sheet_name=category.text, index=False)

        except (NoSuchElementException, TimeoutException):
            print(f'Report: {category.text} is not found.')
            continue

    print('Excel file saved at {}'.format(file_base_name + '.xlsx'))
    xlwriter.save()
    print()

browser.quit()


class Solution:
    def findPosition(self, n , m , k):
        if m <= n - k + 1: return m + k - 1
        m = m - (n - k + 1)
        if m % n == 0: return n
        return m % n

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

        ob = Solution()
        print(ob.findPosition(N,M,K))

Goldman Sachs/7. Find the position of M-th item.py


#!/usr/bin/env python
# This is the bearing calculator code for ITU ROVER TEAM
import rospy
from std_msgs.msg import String
import rosparam

pxCoordinateTopic = rospy.get_param(
    "RoverReachImage/CalculateBearing/sub_pxCoordinates", "/px_coordinates"
)
pxCoordinateTopic1 = rospy.get_param(
    "RoverReachImage/calculatedBearing/sub_pxCoordinates1", "/px_coordinates1"
)
bearingPub = rospy.get_param(
    "RoverReachImage/CalculateBearing/pub_bearing", "/bearing_to_ball"
)
camAngleOfView = rospy.get_param("RoverReachImage/CalculateBearing/camAngleOfView", 110)

pxCoordinates = [None] * 2
pxCoordinates1 = [None] * 2
videoWidth = None
videoHeight = None
calculatedBearing = None
noArtag = True

def pxCallback(data):
    global pxCoordinates
    global videoHeight
    global videoWidth
    global noArtag
    if data.data != "-":
        pxCoordinates[0] = float(data.data.split(",")[0])
        pxCoordinates[1] = float(data.data.split(",")[1])
        videoWidth = float(data.data.split(",")[2])
        videoHeight = float(data.data.split(",")[3])
        noArtag = False
    else:
        noArtag = True

def pxCallback1(data):
    global pxCoordinates1
    global videoHeight
    global videoWidth
    global noArtag

    if data.data != "-":
        pxCoordinates1[0] = float(data.data.split(",")[0])
        pxCoordinates1[1] = float(data.data.split(",")[1])
        videoWidth = float(data.data.split(",")[2])
        videoHeight = float(data.data.split(",")[3])
        noArtag = False
    else:
        noArtag = True

def scCallback(data):
    sc = data.data

def calculateBearing(pxWidth, pxHeight, pxWidth1, pxHeight1, videoWidth, videoHeight):
    global calculatedBearing
    global noArtag

    sc_sub = rospy.Subscriber("/stage_counter_topic", String, scCallback)

    if sc_sub == 3:
        if pxWidth != None and videoWidth != None and noBall == False:
            center = videoWidth / 2
            anglePerPixel = camAngleOfView / videoWidth
            diff = center - pxWidth
            if diff > 5:
                print("+" + str(abs(diff * anglePerPixel)))
                calculatedBearing = "+" + str(abs(diff * anglePerPixel))

            elif diff < -5:
                print("-" + str(abs(diff * anglePerPixel)))
                calculatedBearing = "-" + str(abs(diff * anglePerPixel))

            else:
                print("Duz")
                calculatedBearing = "0"

        else:
            calculatedBearing = "-"

    if sc_sub >= 4:
        if (
            pxWidth != None
            and videoWidth != None
            and pxWidth1 != None
            and noArtag == False
        ):
            center = videoWidth / 2
            anglePerPixel = camAngleOfView / videoWidth
            middle_point = (pxWidth + pxWidth1) / 2
            diff = center - middle_point

            if diff > 5:
                print("+" + str(abs(diff * anglePerPixel)))
                calculatedBearing = "+" + str(abs(diff * anglePerPixel))

            elif diff < -5:
                print("-" + str(abs(diff * anglePerPixel)))
                calculatedBearing = "-" + str(abs(diff * anglePerPixel))

            else:
                print("Duz")
                calculatedBearing = "0"

        else:
            calculatedBearing = "-"

        rospy.sleep(0.04)

def printIt(pxWidth, pxHeight, videoWidth, videoHeight):
    print(
        "pxWidth : "
        + str(pxWidth)
        + " pxHeight : "
        + str(pxHeight)
        + "pxWidth1 : "
        + str(pxWidth1)
        + " pxHeight1 : "
        + str(pxHeight1)
        + " videoWidth : "
        + str(videoWidth)
        + " videoHeight : "
        + str(videoHeight)
    )

def main():
    global pxCoordinates
    global pxCoordinates1
    global videoWidth
    global videoHeight
    global calculatedBearing

    while not rospy.is_shutdown():
        calculateBearing(
            pxCoordinates[0],
            pxCoordinates[1],
            pxCoordinates1[0],
            pxCoordinates1[1],
            videoWidth,
            videoHeight,
        )
        if calculatedBearing != None:
            bearingPublisher.publish(calculatedBearing)

    rospy.spin()

if __name__ == "__main__":

    try:
        rospy.init_node("bearing")
        bearingPublisher = rospy.Publisher(bearingPub, String, queue_size=100)
        rospy.Subscriber(pxCoordinateTopic, String, pxCallback)
        rospy.Subscriber(pxCoordinateTopic, String, pxCallback1)
        while not rospy.is_shutdown():
            main()
    except rospy.ROSInterruptException:
        pass


"""

File: not_equal_pitch_constraint.py

Purpose: Defines a two note constraint where the second note's pitch must equal first note's pitch.

"""
from melody.constraints.abstract_constraint import AbstractConstraint
from structure.note import Note
from misc.ordered_set import OrderedSet

class NotEqualPitchConstraint(AbstractConstraint):
    """

    """

    def __init__(self, not_equal_notes):
        """

        :param not_equal_notes:
        """
        AbstractConstraint.__init__(self, not_equal_notes)
        if len(not_equal_notes) <= 1:
            raise Exception('NotEqualNotePolicy must have two or more v-notes.')

    def clone(self, new_actors=None):
        return NotEqualPitchConstraint(new_actors if new_actors is not None else self.actors)

    def verify(self, p_map):
        not_equal_notes = self.actors
        for v_note in not_equal_notes:
            if v_note not in p_map:
                raise Exception('Improper parameter map in equal note constraints.')
            if p_map[v_note].note is None:
                return False

        t_note_list = [p_map[cn].note for cn in not_equal_notes]
        return NotEqualPitchConstraint.validity_check(t_note_list)[0]

    def values(self, p_map, v_note):
        """
        Find value candidates for v_note target, given p_map.
        In this constraints, we are more interested in the set of values that v_note target cannot take.

        :param p_map:
        :param v_note:
        :return:
        """
        assigned = p_map.assigned_actors(self)
        unassigned = p_map.unassigned_actors(self)
        if len(assigned) == 0:
            pitches = p_map.all_tonal_pitches(v_note)
            return {Note(p, v_note.note.base_duration, v_note.note.num_dots) for p in pitches}
        if v_note in assigned:
            return {p_map[v_note].note}
        if v_note not in unassigned:
            raise Exception('{0} is not in actor list of not equal pitch constraints.'.format(v_note.note))

        e_set = OrderedSet()
        for v in assigned:
            e_set.add(p_map[v].note)

        return NotEqualPitchConstraint.compute_full_result(p_map, v_note, e_set)

    @staticmethod
    def compute_full_result(p_map, v_note, e_set):
        pitches = p_map.all_tonal_pitches(v_note)
        e_chrm_list = [n.diatonic_pitch.chromatic_distance for n in e_set]
        del_set = OrderedSet()
        for p in pitches:
            if p.chromatic_distance in e_chrm_list:
                del_set.add(p)

        pitches = [p for p in pitches if p not in del_set]

        return OrderedSet([Note(p, v_note.base_duration, v_note.num_dots) for p in pitches])

    @staticmethod
    def validity_check(note_list):

        # We compare diatonic distances, as the notes may be enharmonic due to differing tonalities.
        for i in range(0, len(note_list)):
            for j in range(0, len(note_list)):
                if i != j:
                    if note_list[i].diatonic_pitch.diatonic_distance() == \
                            note_list[j].diatonic_pitch.diatonic_distance():
                        return False, note_list[i]
        return True, None


import random
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
num_rolls = 100000
rolls = []
averages = []
for i in range(1, num_rolls + 1):
    roll = random.randint(1, 6)
    rolls.append(roll)
    average = np.mean(rolls)
    averages.append(average)
    if i % 100000 == 0:
        plt.figure()
        sns.lineplot(x=range(i), y=rolls, color='blue', label='Roll')
        sns.lineplot(x=range(i), y=averages, color='red', label='Average')
        plt.xlabel('Number of Rolls')
        plt.ylabel('Value')
        plt.title('Roll Results and Average')
        plt.legend()
        plt.show()


import tensorflow as tf
import numpy as np

# Data for the XOR model
X = np.array([[0,0], [0,1], [1,0], [1,1]])
Y = np.array([  [0],   [1],   [1],   [0]])

# Placeholders
phX = tf.placeholder(tf.float32, shape=[4,2])
phY = tf.placeholder(tf.float32, shape=[4,1])

# Weights
w1 = tf.Variable([[1.0, 0.0], [1.0, 0.0]], shape=[2,2])
w2 = tf.Variable([[0.0], [1.0]], shape=[2,1])

# Biasses
b1 = tf.Variable([0.0, 0.0], shape=[2])
b2 = tf.Variable([0.0], shape=1)

# Layers
hidden = tf.sigmoid(tf.matmul(phX, w1) + b1)
#hidden = tf.math.tanh(tf.matmul(phX, w1) + b1)
output = tf.sigmoid(tf.matmul(hidden, w2) + b2)

# Error
lr = 0.1
err = tf.reduce_mean(tf.squared_difference(phY, output))
train = tf.train.GradientDescentOptimizer(lr).minimize(err)

# Prepare session and run the model
epochs = 100000
eprate = epochs/10
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

print("\n" * 1000)
print("XOR Model, using Tensorflow ", tf.__version__)
for i in range(epochs+1):
	error = sess.run(train, feed_dict={phX:X, phY:Y})
	if (i%eprate) == 0:
		print("Epoch: ", i)
		print("Error: ", sess.run(err, feed_dict={phX:X, phY:Y}))
print("Training Completed!\n")

# Test the NN
print("Test NN:")
Results = sess.run(output, feed_dict={phX:X, phY:Y})

for res in range(4):
	print(X[res], ' = ', Results[res])

sess.close()


"""Epidemic_personnel_management_system URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app01.views import index, admin, account, nurse, resident, area, notice, face_register, face_updater
from app01.views import face, record

urlpatterns = [
    path('index/', index.index),

    path('admin/list/', admin.admin_list),
    path('admin/add/', admin.admin_add),
    path('admin/<int:nid>/edit/', admin.admin_edit),
    path('admin/<int:nid>/delete/', admin.admin_delete),
    path('admin/<int:nid>/reset/', admin.admin_reset),

    path('login/', account.login),
    path('logout/', account.logout),
    path('image/code/', account.image_code),

    path('nurse/list/', nurse.nurse_list),
    path('nurse/add/', nurse.nurse_add),
    path('nurse/<int:nid>/edit/', nurse.nurse_edit),
    path('nurse/<int:nid>/delete/', nurse.nurse_delete),

    path('resident/list/', resident.resident_list),
    path('resident/add/', resident.resident_add),
    path('resident/<int:nid>/edit/', resident.resident_edit),
    path('resident/<int:nid>/delete/', resident.resident_delete),

    path('area/list/', area.area_list),
    path('area/add/', area.area_add),
    path('area/delete/', area.area_delete),
    path('area/detail/', area.area_detail),
    path('area/edit/', area.area_edit),

    path('notice/list/', notice.notice_list),
    path('notice/add/', notice.notice_add),
    path('notice/<int:nid>/detail/', notice.notice_detail),
    path('notice/<int:nid>/edit/', notice.notice_edit),
    path('notice/<int:nid>/delete/', notice.notice_delete),

    path('chart/list/', resident.chart_list),
    path('chart/bar/', resident.chart_bar),
    path('chart/pie/', resident.chart_pie),
    path('chart/line/', resident.chart_line),

    path('face/regist/', face.regist),
    path('face/list/', face.face_list),
    path('face/record/', face.face_record),
    path('face/<int:nid>/edit/', face.face_edit),

    path('record/list/', record.record_list)

]


import time
from src.data import data
from src.error import InputError, AccessError
from src.auth import decode_token
from src.channel import react_info

def dm_create_v1(token, u_id):
    dm_id = len(data['dm']) + 1
    #Finding owner handle
    decode = decode_token(token)
    dm_id = len(data['dm']) + 1
    for i in range(len(data['users'])):
        if decode == data['users'][i]['auth_user_id']:
            fname1 = data['users'][i]['name_first']
            lname1 = data['users'][i]['name_last']
            name1 = fname1 + lname1

    #handle for users + owner
    user_handles = [name1]
    name2 = ""
    for auth_user_id in u_id:
        name2 = find_user_handles(auth_user_id)
        user_handles.append(name2)

    #sorting user handles
    user_handles = sorted(user_handles)

    dm_name = ",".join(user_handles)
    u_id.append(decode)
    dms = {
        'dm_id': dm_id,
        'dm_name': dm_name,
        'u_ids': u_id,
        'owner': [decode]
        }
    data['dm'].append(dms)

    return {'dm_id': dm_id, 'dm_name': dm_name}

def find_user_handles(u_id):
    for i in range(len(data['users'])):
        if u_id == data['users'][i]['auth_user_id']:
            fname2 = data['users'][i]['name_first']
            lname2 = data['users'][i]['name_last']
            return fname2 + lname2

    else:
        raise AccessError("user not found")

def dm_messages_v1(token, dm_id, start):
    '''
    Function returns a dictionary with the 50 most recent messages starting from the start index
    Arguments:
        <token> (<string>)      - <uniqiue jwt string for each user>
        <dm_id> (<int>)    - <unique integer for each dm>
        <start> (<int>)         - <starting index of messages to be returned>

    Exceptions:
        InputError  - Occurs when dm_id is not valid or start is greater than total messages in dm
        AccessError - Occurs when the user is not a member of the dm

    Return Value:
        Returns a dictionary with a list of messages, starting integer and ending integer
        Ending integer will be start + 50 or -1 if there are no more messages
    '''
    # may have to change start to int from string (not sure yet)
    new_dict = {
        'messages': [],
        'start': start,
        'end': start + 50
        }
    dm_index = next((index for (index, d) in enumerate(data['dm']) if d["dm_id"] == dm_id), None)
    # Check if dm id exists
    if dm_index is None:
        raise InputError("dm ID is not a valid dm")
    # check if user is in the dm
    u_id = decode_token(token)
    if u_id not in data['dm'][dm_index]['u_ids']:
        raise AccessError("Authorised user is not a member of dm with dm_id")
    # Create list with all of dm's messages
    message_list = []
    for message_info in data['message']['message_info']:
        if message_info['dm_id'] == dm_id:
            output = {
                'message_id': message_info['message_id'],
                'u_id': message_info['auth_user_id'],
                'message': message_info['message'],
                'time_created': message_info['time_created'],
                'reacts': [react_info(u_id, message_info)],
                'is_pinned': message_info['is_pinned']
            }
            message_list.append(output)

    # check if start index is greater than the amount of messages
    if start > len(message_list):
        raise InputError("Start is greater than the total number of messages in the dm")
    # if start index is equal to total messages, return no messages
    elif start == len(message_list):
        new_dict['end'] = -1
        return new_dict
    message_list.reverse()
    message_index = start
    # Most recent messages will start at the beginning of the list
    for messages in message_list[start:start+50]:
        new_dict['messages'].append(messages)
        message_index += 1
        # Check if we are going outside the range of the list
        if message_index >= len(message_list):
            message_index = -1
            break
    new_dict['end'] = message_index
    return new_dict

def dm_details_v1(token, dm_id):
    '''
    This function takes gives us the details of a particular user who is a member of a dm
    Arguments:
        <token> (string) ----token is an encoded version of auth user id
        <dm id> (integer) ---dm_id is the unique id for each dm
    Exceptions:
        InputError -- occurs when your dm id is invalid
        AccessError -- occurs when you are not a member of this dm
    Return value:
        returns the nane of the dm
        returns list of members' auth id
    '''
    decoded = decode_token(token)
    decoded = int(decoded)
    dms_aid = data['dm']
    dm_ids = []

    details = {}
    for i in range(len(dms_aid)):
        dm_ids.append(dms_aid[i]['dm_id'])
    if int(dm_id) not in dm_ids:
        raise InputError("Invalid dm_id")
    for users in dms_aid:
        if dm_id == users['dm_id'] and decoded not in users['u_ids']:
            raise AccessError("not a member")

    for i in range(len(data['dm'])):
        if dm_id == data['dm'][i]['dm_id']:
            details['name'] = data['dm'][i]['dm_name']
            out = []
            for m in data['dm'][i]['u_ids']:
                auth_index = next((index for (index, d) in enumerate(data['users']) if d["auth_user_id"] == m), None)
                member_info = {
                    'u_id': data['users'][auth_index]['auth_user_id'],
                    'email': data['users'][auth_index]['email'],
                    'name_first': data['users'][auth_index]['name_first'],
                    'name_last': data['users'][auth_index]['name_last'],
                    'handle_str': data['users'][auth_index]['handle_str'],
                }
                out.append(member_info)
            details['members'] = out
    return details

def dm_list_v1(token):
    '''
    This function gives out the list of dms the user is part. The function takes an encoded token decodes it
    and gives us the list of dms after checking the data structure.

    Arguments:
        <token> (string) --- token is an encoded version of auth user id

    Exceptions:
        AccessError -- occurs when the token is invalid
    Return Value:
        return list of dm ids that the user is member of
    '''
    decoded = decode_token(token)
    dms_id = data.get('dm')
    dm_ids = []
    dm_info = []
    for i in range(len(dms_id)):
        dm_ids.append(dms_id[i]['u_ids'])
    # if next((True for token_list in data['users'] if decoded in token_list['token']), False) is False:
    #     raise InputError("Invalid token")
    for i in range(len(data['dm'])):
        if decoded in data['dm'][i]['u_ids']:
            out = {
                "dm_id": data['dm'][i]['dm_id'],
                "name": data['dm'][i]['dm_name']
            }
            dm_info.append(out)
    return {'dms': dm_info}

def dm_remove_v1(token, dm_id):
    '''
    This function removes the entire dm or deletes it. but only the user who created it can delete a particular
    dm.
    Arguments:
        <token> (string) -----token is an encoded version of auth user id
        <dm_id> (integer) ----dm_id is the unique id for each dm
    Exceptions:
        InputError -- occurs when your dm id is invalid
        AccessError -- occurs when you are not a creator but want to delete this dm
    Return Value:
        This function does not return anything
    '''
    decode = decode_token(token)
    vdm = []
    ids = data.get('dm')
    for i in range(len(data['dm'])):
        vdm.append(ids[i]['dm_id'])
    if dm_id not in vdm:
        raise InputError("not a valid dm id")
    for i in range(len(data['dm'])):
        if dm_id == data['dm'][i]['dm_id']:
            if decode in data['dm'][i]['owner']:
                del data['dm'][i]
            else:
                raise AccessError("not owner")
    return {}

def dm_leave_v1(token, dm_id):
    '''
    This function provides you with the service of leaving a dm.
    Arguments:
        <token> (string) -----token is an encoded version of auth user id
        <dm_id> (integer) ----dm_id is the unique id for each dm

    Exceptions:
        InputError -- occurs when your dm id is invalid
        AccessError -- occurs when you are not a member of a particular dm

    Return Value:
        This function does not return anything

    '''
    decode = decode_token(token)
    vdm = []
    udm = []
    ids = data.get('dm')
    for i in range(len(data['dm'])):
        vdm.append(ids[i]['dm_id'])
        uid = udm+(ids[i]['u_ids'])
    if dm_id not in vdm:
        raise InputError("not a valid dm id")
    if decode not in uid:
        raise AccessError("You are not a member")
    for i in range(len(data['dm'])):
        if decode in data['dm'][i]['u_ids'] and decode != data['dm'][i]['owner']:
            data['dm'][i]['u_ids'].remove(decode)
    return {}

def dm_invite_v1(token, dm_id, u_id):
    auth_user_id = decode_token(token)
    dm_index = next((index for (index, d) in enumerate(data['dm']) if d["dm_id"] == dm_id), None)
    # Check if channel id exists
    if dm_index is None:
        raise InputError("dm_id does not refer to an existing dm.")
    if auth_user_id == u_id:
        return {}
    user_index = next((index for (index, d) in enumerate(data['users']) if d["auth_user_id"] == u_id), None)
    if user_index is None:
        raise InputError("u_id does not refer to a valid user")
    if auth_user_id not in data['dm'][dm_index]['u_ids']:
        raise AccessError('the authorised user is not already a member of the channel')

    data['dm'][dm_index]['u_ids'].append(u_id)
    if data['users'][user_index]['permission'] == 1:
        data['dm'][dm_index]['owner'].append(u_id)
    return {}


# multiAgents.py
# --------------
# Licensing Information:  You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).

from util import manhattanDistance
from game import Directions
import random, util

from game import Agent

class ReflexAgent(Agent):
    """
    A reflex agent chooses an action at each choice point by examining
    its alternatives via a state evaluation function.

    The code below is provided as a guide.  You are welcome to change
    it in any way you see fit, so long as you don't touch our method
    headers.
    """

    def getAction(self, gameState):
        """
        You do not need to change this method, but you're welcome to.

        getAction chooses among the best options according to the evaluation function.

        Just like in the previous project, getAction takes a GameState and returns
        some Directions.X for some X in the set {NORTH, SOUTH, WEST, EAST, STOP}
        """
        # Collect legal moves and successor states
        legalMoves = gameState.getLegalActions()

        # Choose one of the best actions
        scores = [self.evaluationFunction(gameState, action) for action in legalMoves]
        bestScore = max(scores)
        bestIndices = [index for index in range(len(scores)) if scores[index] == bestScore]
        chosenIndex = random.choice(bestIndices)  # Pick randomly among the best

        "Add more of your code here if you want to"

        return legalMoves[chosenIndex]

    def evaluationFunction(self, currentGameState, action):
        """
        Design a better evaluation function here.

        The evaluation function takes in the current and proposed successor
        GameStates (pacman.py) and returns a number, where higher numbers are better.

        The code below extracts some useful information from the state, like the
        remaining food (newFood) and Pacman position after moving (newPos).
        newScaredTimes holds the number of moves that each ghost will remain
        scared because of Pacman having eaten a power pellet.

        Print out these variables to see what you're getting, then combine them
        to create a masterful evaluation function.
        """
        # Useful information you can extract from a GameState (pacman.py)
        successorGameState = currentGameState.generatePacmanSuccessor(action)
        newPos = successorGameState.getPacmanPosition()
        newFood = successorGameState.getFood()
        newGhostStates = successorGameState.getGhostStates()
        newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]

        "*** YOUR CODE HERE ***"
        def getGostDistance():
            minimumDistance = 1e10
            for state in newGhostStates:
                i = manhattanDistance(newPos, state.getPosition())
                if (minimumDistance > i):
                    minimumDistance = i
            return minimumDistance

        def isFoodNearer():
            position = currentGameState.getPacmanPosition()
            nearestFoodDistance = 1e10
            for food in currentGameState.getFood().asList():
                i = manhattanDistance(position, food)
                if nearestFoodDistance > i:
                    nearestFoodDistance = i
            newFoodDistances = 1e10
            if not newFood.asList() :
                newFoodDistances = 0
            else:
                for food in newFood.asList():
                    i = manhattanDistance(newPos, food)
                    if newFoodDistances > i:
                        newFoodDistances = i
            return nearestFoodDistance - newFoodDistances

        if getGostDistance() <= 1 or action == Directions.STOP:
            return 0
        if successorGameState.getScore() - currentGameState.getScore() > 0:
            return 8
        elif isFoodNearer() > 0:
            return 4
        elif action == currentGameState.getPacmanState().getDirection():
            return 2
        else:
            return 1

def scoreEvaluationFunction(currentGameState):
    """
    This default evaluation function just returns the score of the state.
    The score is the same one displayed in the Pacman GUI.

    This evaluation function is meant for use with adversarial search agents
    (not reflex agents).
    """
    return currentGameState.getScore()

class MultiAgentSearchAgent(Agent):
    """
    This class provides some common elements to all of your
    multi-agent searchers.  Any methods defined here will be available
    to the MinimaxPacmanAgent, AlphaBetaPacmanAgent & ExpectimaxPacmanAgent.

    You *do not* need to make any changes here, but you can if you want to
    add functionality to all your adversarial search agents.  Please do not
    remove anything, however.

    Note: this is an abstract class: one that should not be instantiated.  It's
    only partially specified, and designed to be extended.  Agent (game.py)
    is another abstract class.
    """

    def __init__(self, evalFn='scoreEvaluationFunction', depth='2'):
        self.index = 0  # Pacman is always agent index 0
        self.evaluationFunction = util.lookup(evalFn, globals())
        self.depth = int(depth)

class MinimaxAgent(MultiAgentSearchAgent):
    """
    Your minimax agent (question 2)
    """

    def getAction(self, gameState):
        """
        Returns the minimax action from the current gameState using self.depth
        and self.evaluationFunction.

        Here are some method calls that might be useful when implementing minimax.

        gameState.getLegalActions(agentIndex):
        Returns a list of legal actions for an agent
        agentIndex=0 means Pacman, ghosts are >= 1

        gameState.generateSuccessor(agentIndex, action):
        Returns the successor game state after an agent takes an action

        gameState.getNumAgents():
        Returns the total number of agents in the game

        gameState.isWin():
        Returns whether or not the game state is a winning state

        gameState.isLose():
        Returns whether or not the game state is a losing state
        """
        "*** YOUR CODE HERE ***"
        def maxValue(depth, currentState):
            pacmanLegalActions = currentState.getLegalActions(0)
            if (depth > self.depth) or currentState.isWin() or currentState.isLose():
                return self.evaluationFunction(currentState)

            muximumValue = -1e10

            for pacmanAction in pacmanLegalActions:
                pacmanSuccessor = currentState.generateSuccessor(0, pacmanAction)
                muximumValue = max(muximumValue, minValue(depth, pacmanSuccessor, 1))
            return muximumValue

        def minValue(depth, currentState, gostIndex):
            gostLegalActions = currentState.getLegalActions(gostIndex)
            if currentState.isWin() or currentState.isLose():
                return self.evaluationFunction(currentState)

            minimumValue = 1e10

            agentCount = currentState.getNumAgents()
            if gostIndex <= agentCount - 2:
                for gostAction in gostLegalActions:
                    gostSuccessor = currentState.generateSuccessor(gostIndex, gostAction)
                    minimumValue = min(minimumValue, minValue(depth, gostSuccessor, gostIndex + 1))
            else:
                for gostAction in gostLegalActions:
                    gostSuccessor = currentState.generateSuccessor(gostIndex, gostAction)
                    minimumValue = min(minimumValue, maxValue(depth + 1, gostSuccessor))

            return minimumValue

        bestValue = -1e10
        bestAction = None
        for pacmanAction in gameState.getLegalActions(0):
            pacmanSuccessorState = gameState.generateSuccessor(0, pacmanAction)
            gostValue = minValue(1, pacmanSuccessorState, 1)
            if gostValue > bestValue:
                bestValue = gostValue
                bestAction = pacmanAction
        return bestAction

class AlphaBetaAgent(MultiAgentSearchAgent):
    """
    Your minimax agent with alpha-beta pruning (question 3)
    """

    def getAction(self, gameState):
        """
        Returns the minimax action using self.depth and self.evaluationFunction
        """
        "*** YOUR CODE HERE ***"
        def maxValue(depth, currentState, alfa, beta):
            pacmanLegalActions = currentState.getLegalActions(0)
            if (depth > self.depth) or currentState.isWin() or currentState.isLose():
                return self.evaluationFunction(currentState)

            maximumValue = -1e10
            for pacmanAction in pacmanLegalActions:
                pacmanSuccessor = currentState.generateSuccessor(0,pacmanAction)
                maximumValue = max(maximumValue, minValue(depth, pacmanSuccessor, 1, alfa, beta))
                if maximumValue > beta : return maximumValue
                alfa = max(alfa, maximumValue)
            return maximumValue

        def minValue(depth, currentState, agentIndex, alfa, beta):
            gostLegalActions = currentState.getLegalActions(agentIndex)
            if currentState.isWin() or currentState.isLose():
                return self.evaluationFunction(currentState)

            minimumValue = 1e10
            agentCount = currentState.getNumAgents()
            if agentIndex <= agentCount-2:
                for gostAction in gostLegalActions:
                    gostSuccessor = currentState.generateSuccessor(agentIndex,gostAction)
                    minimumValue = min(minimumValue, minValue(depth, gostSuccessor ,agentIndex + 1, alfa, beta))
                    if minimumValue < alfa : return minimumValue
                    beta = min(beta, minimumValue)
            else:
                for gostAction in gostLegalActions:
                    gostSuccessor = currentState.generateSuccessor(agentIndex,gostAction)
                    minimumValue = min(minimumValue, maxValue(depth + 1, gostSuccessor, alfa, beta))
                    if minimumValue < alfa : return minimumValue
                    beta = min(beta, minimumValue)

            return minimumValue

        bestValue = -1e10
        bestAction = None
        alfa = -1e10
        beta = 1e10
        for pacmanAction in gameState.getLegalActions(0):
            successorState = gameState.generateSuccessor(0, pacmanAction)
            gostValue = minValue(1, successorState, 1, alfa, beta)
            if gostValue > bestValue:
                bestValue = gostValue
                bestAction = pacmanAction
            if bestValue > beta : return bestAction
            alfa = max(alfa, bestValue)
        return bestAction

class ExpectimaxAgent(MultiAgentSearchAgent):
    """
    Your expectimax agent (question 4)
    """

    def getAction(self, gameState):
        """
        Returns the expectimax action using self.depth and self.evaluationFunction
        All ghosts should be modeled as choosing uniformly at random from their
        legal moves.
        """
        "*** YOUR CODE HERE ***"

        def maxValue(depth, currentState):
            pacmanLegalActions = currentState.getLegalActions(0)
            if (depth > self.depth) or currentState.isWin() or currentState.isLose():
                return self.evaluationFunction(currentState)

            muximumValue = -1e10

            for pacmanAction in pacmanLegalActions:
                pacmanSuccessor = currentState.generateSuccessor(0, pacmanAction)
                muximumValue = max(muximumValue, chanceValue(depth, pacmanSuccessor, 1))
            return muximumValue

        def chanceValue(depth,currentState,agentIndex):
            gostLegalActions = currentState.getLegalActions(agentIndex)
            if currentState.isWin() or currentState.isLose():
                return self.evaluationFunction(currentState)

            gostChanceValue = 0
            agentCount = currentState.getNumAgents()
            if agentIndex <= agentCount - 2:
                for action in gostLegalActions:
                    gostSuccessor = currentState.generateSuccessor(agentIndex,action)
                    gostChanceValue += chanceValue(depth, gostSuccessor, agentIndex + 1)

            else:
                for action in gostLegalActions:
                    gostSuccessor = currentState.generateSuccessor(agentIndex,action)
                    gostChanceValue += maxValue(depth + 1, gostSuccessor) / len(gostLegalActions)

            return gostChanceValue

        bestValue = -1e10
        bestAction = None
        for pacmanAction in gameState.getLegalActions(0):
            pacmanSuccessorState = gameState.generateSuccessor(0, pacmanAction)
            gostValue = chanceValue(1, pacmanSuccessorState, 1)
            if gostValue > bestValue:
                bestValue = gostValue
                bestAction = pacmanAction
        return bestAction

def betterEvaluationFunction(currentGameState):
    """
    Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
    evaluation function (question 5).

    DESCRIPTION: <write something here so we know what you did>
    """
    "*** YOUR CODE HERE ***"
    score = currentGameState.getScore()

    pacmanPos = currentGameState.getPacmanPosition()
    foodLeft = currentGameState.getFood().asList()
    ghostStates = currentGameState.getGhostStates()
    capsulesLeft = currentGameState.getCapsules()

    if len(capsulesLeft) > 0:
        capsuleDistances = []
        for capsule in capsulesLeft:
            capsuleDistances.append(manhattanDistance(capsule,pacmanPos))
        score = score - min(capsuleDistances)

    if len(foodLeft) > 0:
        foodDistances = []
        for food in foodLeft:
            foodDistances.append(manhattanDistance(food,pacmanPos))
        score = score - min(foodDistances)

    if currentGameState.hasFood(pacmanPos[0],pacmanPos[1]): score = score + 50
    if currentGameState.isLose(): score = score - 9999
    if currentGameState.isWin(): score = score + 9999
    for capsule in capsulesLeft:
        if pacmanPos == capsule : score = score + 100

    for ghost in ghostStates:
        if ghost.scaredTimer > 0:
            score = score + manhattanDistance(ghost.getPosition(),pacmanPos)
        else:
            score = score - manhattanDistance(ghost.getPosition(),pacmanPos)

    score = score - len(foodLeft)

    return score

# Abbreviation
better = betterEvaluationFunction


#!/usr/bin/env python

# 怎样实现一个按优先级排序的队列？ 并且在这个队列上面每次pop操作总是返回优先级最高的那个元素
# 利用元组比较大小
import heapq

class PriorityQueue:
    def __init__(self):
        self._queue = []
        self._index = 0

    def push(self, item, priority):
        heapq.heappush(self._queue, (-priority, self._index, item))
        self._index += 1

    def pop(self):
        return heapq.heappop(self._queue)[-1]

    def iterator(self):
        length = len(self._queue)
        counter = 0
        while True:
            yield self.pop()
            counter += 1
            if counter >= length:
                return

class Item:
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return 'Item({!r})'.format(self.name)

q = PriorityQueue()
q.push(Item('foo'), 1)
q.push(Item('bar'), 5)
q.push(Item('spam'), 4)
q.push(Item('grok'), 1)

for x in q.iterator():
    print(x)

# 利用元祖比较大小
a = (1, 0, Item('foo'))
b = (5, 1, Item('bar'))
c = (1, 2, Item('grok'))
d = (4, 3, Item('spam'))

print(a > b)
print(d>a)


#!/usr/bin/env python3
import psycopg2
import psycopg2.extras
from functools import partial
from subprocess import check_output
from configparser import ConfigParser

def main():
    # parse application configurations
    cfg_parser = ConfigParser()
    cfg_parser.read('../config.ini')
    conf = dict(cfg_parser.items('db'))

    # get IP address of the database container
    host = check_output("docker ps | grep %s | awk '{print $NF;}' | \
                        xargs docker inspect --format \
                        '{{ .NetworkSettings.IPAddress }}'" % conf['host'],
                        shell=True).decode('utf-8').strip("\n")

    try:
        db = None
        db = psycopg2.connect(
            "dbname='{}' user='{}' host='{}' password='{}'".format(
                conf['name'], conf['user'], host, conf['pass']))

        cursor = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)

        cursor.execute("SELECT value FROM settings WHERE key='languages'")
        languages = cursor.fetchone().value

        languages = {v: k for k, v in languages.items() if len(v) != 0}

        # partial replace langs
        prl = partial(replace_langs, cursor, languages)

        prl('accounts', ['data'])
        prl('actions', ['title'])
        prl('address_book', ['name', 'descr', 'address'])
        prl('calendar', ['comment'])
        prl('groups', ['title'])
        prl('libraries', ['title', 'fields'])
        prl('roles', ['title', 'division'])
        prl('sequences', ['descr'])
        prl('service_users', ['descr'])
        prl('settings', ['value'])
        prl('snippets', ['descr'])
        prl('statuses', ['title'])
        prl('tabs', ['title'])
        prl('task_states', ['new_data'])
        prl('tasks', ['data'])
        prl('templates', ['descr', 'body'])
        prl('workflows', ['title', 'descr'])

        cursor.execute("SELECT * FROM libraries")
        libs = cursor.fetchall()
        for lib in libs:
            fields = [f['name'] for k, f in lib.fields.items()
                      if f['type'] == 'jsonb']

            if len(fields) > 0:
                prl(lib.name, fields)

        db.commit()
        cursor.close()

    except Exception as e:
        if db is not None:
            db.rollback()
        print("Database Error [{}]".format(str(e)))
    finally:
        if db is not None:
            db.close()

def replace_langs(cur, languages, table, fields):
    rpl = '{0}=replace({0}::text, \'"{1}":\', \'"{2}":\')::jsonb'

    for k, v in languages.items():
        rpls = [rpl.format(f, k, v) for f in fields]

        cur.execute("UPDATE {0} SET {1}".format(table, ",".join(rpls)))

if __name__ == '__main__':
    main()


# !_*_ coding:utf8 _*_
# @function 百度图片爬虫
# @Author alexchung
# @Date 2019/7/29 17:21 PM

import os
import re
import requests
import hashlib
import time

import concurrent.futures

from urllib import error
from bs4 import BeautifulSoup

class Crawler(object):
    def __init__ (self, fig_type='', fig_num=0, save_path=None):
        self.type = fig_type
        self.page = fig_num / 30
        # self.route = file_route
        self.save_path = os.path.join(save_path, str(fig_type))

        self.url_list = []
        self.number = len(self.url_list)

        if os.path.exists(self.save_path) is False:
            os.makedirs(self.save_path)

    def run(self):
        """
        运行方法，更新属性
        :return:
        """
        self.get_url()
        # self.getSearchURL()
        # self.getFileURL()
        self.download_picture_threading()

    def get_url(self):

        params = []
        for i in range(30, 30 * int(self.page) + 30, 30):
            params.append({
                'tn': 'resultjson_com',
                'ipn': 'rj',
                'ct': 201326592,
                'is': '',
                'fp': 'result',
                'queryWord': self.type,
                'cl': 2,
                'lm': -1,
                'ie': 'utf-8',
                'oe': 'utf-8',
                'adpicid': '',
                'st': -1,
                'z': '',
                'ic': 0,
                'word': self.type,
                's': '',
                'se': '',
                'tab': '',
                'width': '',
                'height': '',
                'face': 0,
                'istype': 2,
                'qc': '',
                'nc': 1,
                'fr': '',
                'pn': i,
                'rn': 30,
                'gsm': '5a',
                '1564468338576': ''
            })
        url = 'https://image.baidu.com/search/index'
        for p in params:
            page_list = requests.get(url, params=p).json().get('data')
            for ls in page_list:
                if ls.get('thumbURL') == None:
                    continue
                else:
                    self.url_list.append(ls.get('thumbURL'))
                    self.number += 1

    # @ property
    def get_search_url(self):
        """
        获取搜索url
        :return:
        """
        self.url = r'http://image.baidu.com/search/index?tn=baiduimage&ps=1&lm=-1&cl=2&nc=1&ie=utf-8&word=' + self.type

    def get_file_url(self):
        """
        获取图片文件url
        :return:
        """
        # time
        t = 0
        # url size
        fig_size = 0
        while t < 1000:
            Url = self.url + str(t)
            try:
                Result = requests.get(Url, timeout=7)
            except BaseException:
                t += 60
                continue
            else:
                result = Result.text
                # html = urlopen(self.url)
                # htmls = html.read().decode()
                pic_url = re.findall(r'"objURL":"(.*?)",', result, re.S)
                self.number = len(pic_url)
                if len(pic_url) == 0:
                    break
                else:
                    self.url_list = pic_url
                    t += 60

    def download_picture(self):
        """
        执行图片下载
        :return:
        """

        # num_index = 0
        print('图片保存路径为:{0}'.format(self.save_path))
        print('当前页面检索到{0}张照片'.format(str(self.number)))
        for pic_source in self.url_list:
            # print('正在下载第{0}/{1}张照片，照片源：{2}'.format(str(num_index+1), str(self.number), str(pic_source)))
            try:
                self.save_image(pic_source)
            except BaseException:
                print('未知原因导致当前图片无法下载')
                continue

    def download_picture_threading(self):
        """
        执行图片下载
        :return:
        """

        # num_index = 0
        print('图片保存路径为:{0}'.format(self.save_path))
        print('当前页面检索到{0}张照片'.format(str(self.number)))

        with concurrent.futures.ThreadPoolExecutor() as execute:
            execute.map(self.save_image, self.url_list)

    def save_image(self, pic_url):
        """
        save unique image
        :param pic_url:
        :param save_path:
        :return:
        """
        print('正在下载照片源：{0}'.format(str(pic_url)))
        if pic_url is None:
            raise KeyError("picture url is None")
        else:
            pic = requests.get(pic_url, timeout=7)

        md5 = hashlib.md5()
        md5.update(pic_url.encode('utf-8'))
        string = os.path.join(self.save_path, str(md5.hexdigest()) + '.jpg')

        fp = open(string, 'wb')
        fp.write(pic.content)
        fp.close()

if __name__  == "__main__":
    save_path = os.getcwd()
    # 获取类实例
    start_time = time.perf_counter()
    crawler_fig = Crawler('湖泊', 300, save_path=save_path)
    crawler_fig.run()
    finish_time = time.perf_counter()
    print(f"Spend {finish_time-start_time} second")


#!/usr/bin/env python
"""icon2vec model implemented in TensorFlow.

File also contains a ModelParams class, which is a convenience wrapper for all the parameters to the model.
Details of the model can be found below.
Based on Ben Eisner, Tim Rocktaschel's good work.
"""

# External dependencies
import tensorflow as tf
from tensorflow.python.framework import ops
import numpy as np
import sys
from os import environ
from random import shuffle
import sklearn.metrics as metrics
from warnings import filterwarnings
filterwarnings(action='ignore', category=UserWarning, module='gensim')
import gensim.models as gs
import pickle as pk
environ['TF_CPP_MIN_LOG_LEVEL'] = '3' #disable all debugging logs
tf.logging.set_verbosity(tf.logging.FATAL)

# Authorship
__author__ = "Ji Li"
__email__ = "jili5@microsoft.com"

class Text2Vec:
    """Class for representing the model in TensorFlow."""

    # define the model
    def __init__(self, model_params, num_icons, trainset, devset, testset):
        """Constructor for the Text2Vec model

        Args:
            model_params: Parameters for the model
            num_icons: Number of icons we will ultimately train
        """
        self.aa = False
        self.model_params = model_params
        self.num_cols = num_icons
        self.savedModels = set()

        self.initializeDataset(trainset, devset, testset)
        self.initializeDatasetWithBenchmarkTraining()
        self.__open_benchmark()

        # row - phrase input to the graph
        self.phrase_vec = tf.placeholder(tf.float32, shape=[None, model_params.in_dim], name='phrase_vec')

        # Correlation between an icon and a phrase
        self.y = tf.placeholder(tf.float32, shape=[None], name='y')

        # Icon indices in current batch
        self.col = tf.placeholder(tf.int32, shape=[None], name='col')

        # 1st pass: init V embeddings for icons - multi layers
        # Column embeddings (here icon representations)
        self.V = []
        for idx, num in enumerate(model_params.nn_params):
            V = tf.get_variable("V"+str(idx), shape=[num_icons, num], initializer=tf.contrib.layers.xavier_initializer())
#             V = tf.Variable(tf.random_uniform([num_icons, num], -0.1, 0.1), name="V"+str(idx))
            self.V.append(V)
#         print(self.V)

        # 2nd pass: calculate score, build projection matrix P between V layers
        prev = model_params.in_dim
        score = self.phrase_vec
        layers = len(model_params.nn_params)
        for idx in range(layers):
            if model_params.in_dim != model_params.nn_params[0] or idx > 0:
#                 P = tf.Variable(tf.random_uniform([prev, model_params.nn_params[idx]]), name="P"+str(idx))
                P = tf.get_variable("P"+str(idx), shape=[prev, model_params.nn_params[idx]], initializer=tf.contrib.layers.xavier_initializer())
                prev = model_params.nn_params[idx]
                score = tf.matmul(score, P)
                score = tf.nn.relu(score)
#                 score = tf.tanh(score)
#                 print("\n")
#                 print("P",P)
#                 print("score", score)

            V = tf.nn.embedding_lookup(self.V[idx], self.col)
#             print("V",V)
            if idx != layers - 1:
                score = tf.multiply(score, V)
            else:
                score = tf.multiply(score, tf.nn.dropout(V, (1-model_params.dropout)))
#             print("score", score)
#             print("\n")
#         print(score)

    	# custom loss
        self.score = tf.reduce_sum(score, 1)

        # Probability of match
        self.prob = tf.sigmoid(self.score)
        # Calculate the cross-entropy loss
#         self.loss = tf.nn.weighted_cross_entropy_with_logits(targets=self.score, logits=self.y, pos_weight=1)
        self.loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=self.score, labels=self.y)

    def initializeDataset(self, trainset, devset, testset):
        icon_idx, phrase_embedding, labels = [], [], []
        ph_idx = []
        for item in trainset:
            icon_idx.append(item[0])
            phrase_embedding.append(item[1])
            labels.append(item[2])
        self.trainset = [np.array(icon_idx), np.array(phrase_embedding), np.array(labels)]

        icon_idx, phrase_embedding, labels = [], [], []
        for item in devset:
            icon_idx.append(item[0])
            phrase_embedding.append(item[1])
            labels.append(item[2])
        self.devset = [np.array(icon_idx), np.array(phrase_embedding), np.array(labels)]

        icon_idx, phrase_embedding, labels = [], [], []
        for item in testset:
            icon_idx.append(item[0])
            phrase_embedding.append(item[1])
            labels.append(item[2])
        self.testset = [np.array(icon_idx), np.array(phrase_embedding), np.array(labels)]

    def initializeDatasetWithBenchmarkTraining(self):
        """initialize benchmark """
        fileObject = open("data/benchmarks/trainset_12-2017_9-1-2018_025Unk.ss.csv.fasttext.p", 'rb')
        benchmarktrainraw = pk.load(fileObject)
        fileObject.close()
        # parse the positive
        icon_idx, phrase_embedding, labels = [], [], []
        ph_idx = []
        for item in benchmarktrainraw:
            icon_idx.append(item[1])
            phrase_embedding.append(item[0])
            labels.append(1)
        # generate negative
        half = len(labels)
        for i in range(half):
            icon_idx.append((icon_idx[i]+np.random.randint(0,1000))%self.num_cols)
            phrase_embedding.append(phrase_embedding[i])
            labels.append(0)
        # combine
        self.trainset = [np.array(icon_idx), np.array(phrase_embedding), np.array(labels)]

    def __open_benchmark(self):
        fileObject = open("data/benchmarks/testset_SingleIcon_9-1_10-22-2018_025Unk.ss.csv.fasttext.p", 'rb')
        self.benchmark = pk.load(fileObject)
        fileObject = open("data/benchmarks/testset_SingleIcon_9-18_10-18-2018_025Unk_MinWord3_Kept24Hrs.ss.csv.fasttext.p", 'rb')
        self.benchmarkMin = pk.load(fileObject)
#         print(len(self.benchmark), self.benchmark[0])
        fileObject.close()

        icon_idx, phrase_embedding, labels, icon, phrase = [], [], [], [], []
        for item in self.benchmark:
            icon_idx.append(item[1])
            phrase_embedding.append(item[0])
            labels.append(1)
            icon.append(item[2])
            phrase.append(item[3])
        self.benchmarkDataset = [np.array(icon_idx), np.array(phrase_embedding), np.array(labels), np.array(icon), np.array(phrase)]

        icon_idx, phrase_embedding, labels = [], [], []
        for item in self.benchmarkMin:
            icon_idx.append(item[1])
            phrase_embedding.append(item[0])
            labels.append(1)
        self.benchmarkDatasetMin = [np.array(icon_idx), np.array(phrase_embedding), np.array(labels)]

    def initializeSession(self):
        self.session = tf.Session()
        init = tf.global_variables_initializer()
        self.session.run(init)

    # train the model using the appropriate parameters
    def train1(self):
        """Train the model on a given knowledge base"""
        self.optimizer1 = tf.train.AdamOptimizer(self.model_params.learning_rate)
        self.minimization_op1 = self.optimizer1.minimize(self.loss)

        self.optimizer = tf.train.AdamOptimizer(0.0001)
        self.minimization_op = self.optimizer.minimize(self.loss)
        self.initializeSession()
        epoch = 0
        total_data_entry = self.trainset[0].shape[0]
        half_data_entry = self.trainset[0].shape[0]//2
        max_res = {"dev":[0,0], "test":[0,0]}
        while epoch < self.model_params.max_epochs:
#             print(total_data_entry, half_data_entry)
            training_idx_pos = np.random.randint(half_data_entry, size=self.model_params.batch_size//2)
            training_idx_neg = np.random.randint(half_data_entry, size=self.model_params.batch_size//2)
            training_idx_neg = [i+ half_data_entry for i in training_idx_neg]
            training_idx = np.concatenate((training_idx_pos, training_idx_neg),axis =0)
            shuffle(training_idx)
#             y = [1 if i < half_data_entry else 0 for i in training_idx]
            y = [self.trainset[2][i] for i in training_idx]

#             print(training_idx_neg, training_idx_pos)
#             print("===",training_idx)
#             print(y)

            _, current_loss = self.session.run([self.minimization_op1, self.loss], feed_dict={
                self.col:self.trainset[0][training_idx],
                self.phrase_vec: self.trainset[1][training_idx],
                self.y:np.array(y)
            })
            current_loss = sum(current_loss)

            if epoch % 10 == 0:
                print("Epoch=%d loss=%3.1f" %(epoch, current_loss))
                devres = self.cal_top_n(self.devset, "dev ", N=2)
                if not devres:
                    epoch += 1
                    continue
                if devres[1] <= max_res["dev"][1]:
                    epoch += 1
                    continue
                testres = self.cal_top_n(self.testset, "test", N=2)
                max_res["dev"] = devres
                max_res["test"] = testres
#                 if devres[1] >= 0.4:# and testres[1] >=0.4:
#                     print(max_res)
#                     return

            epoch += 1

        print("results when max dev accu:")
        print(max_res)
#         self.print_top_accuracy(max_accuracy_top2[0],"dev")
#         self.print_top_accuracy(max_accuracy_top2[1],"test")

        return max_res

    # train the model using the appropriate parameters
    def train(self):
        """Train the model on a given knowledge base"""
        self.optimizer = tf.train.AdamOptimizer(self.model_params.learning_rate)
        self.minimization_op = self.optimizer.minimize(self.loss)

        self.initializeSession()
        self.saver = tf.train.Saver()
        epoch = 0
        total_data_entry = self.trainset[0].shape[0]
        half_data_entry = self.trainset[0].shape[0]//2
        max_res = {"min1000":[0,0], "min5000":[0,0], "minWordAll":[0,0], "notMinAll":[0,0]}
        while epoch < self.model_params.max_epochs:
#             print(total_data_entry, half_data_entry)
            training_idx_pos = np.random.randint(half_data_entry, size=self.model_params.batch_size//2)
            training_idx_neg = np.random.randint(half_data_entry, size=self.model_params.batch_size//2)
            training_idx_neg = [i+ half_data_entry for i in training_idx_neg]
            training_idx = np.concatenate((training_idx_pos, training_idx_neg),axis =0)
            shuffle(training_idx)
#             y = [1 if i < half_data_entry else 0 for i in training_idx]
            y = [self.trainset[2][i] for i in training_idx]

#             print(training_idx_neg, training_idx_pos)
#             print("===",training_idx)
#             print(y)

            _, current_loss = self.session.run([self.minimization_op, self.loss], feed_dict={
                self.col:self.trainset[0][training_idx],
                self.phrase_vec: self.trainset[1][training_idx],
                self.y:np.array(y)
            })
            current_loss = sum(current_loss)
            if epoch < 30000:
                spe = 1000
            else:
                spe = 100

            if epoch % spe == 0:
                print("Epoch=%d loss=%3.1f" %(epoch, current_loss))
                epoch += 1
#                 devres = self.cal_top_n(self.devset, "dev      ", N=2)
                devres = self.cal_top_n(self.benchmarkDatasetMin, "devMin1000 ", N=2,stop=1000)
                if not devres:
                    continue
                if devres[1] < max_res["min1000"][1]:
                    continue
                if devres[1] > 0.21 and devres[1]>=max_res["min1000"][1]:
                    max_res["min1000"] = devres
                    testres = self.cal_top_n(self.benchmarkDatasetMin, "devMin5000  ", N=2, stop=5000)
                    V = self.session.run(self.V[0])

#                     self.saveModel(self.model_params.model_folder("minworddev", devres[0], devres[1]),V)

                    if testres[1] > max_res["min5000"][1]:
                        max_res["min5000"] = testres
                        testminallres = self.cal_top_n(self.benchmarkDatasetMin, "testMinALL  ", N=2)
                        if testminallres[1] > max_res["minWordAll"][1]:
#                             testallres = self.cal_top_n(self.benchmarkDataset, "testALL  ", N=2)
#                             if testminallres[1] > max_res["minWordAll"][1]:
                            max_res["minWordAll"] = testminallres
#                                 max_res["notMinAll"] = testallres
                            self.saveModel(self.model_params.model_folder("minword", testminallres[0], testminallres[1]), V)
#                 if devres[1] > 0.15:
#                     benchmarkres = self.cal_top_n(self.benchmarkDataset, "bench   ", N=2, stop=1000)
#                     benchmarkminires = self.cal_top_n(self.benchmarkDatasetMin, "benchmin", N=2)
#                     max_accuracy_top2 = [devres, testres, benchmarkres]#, benchmarkminres]
            epoch += 1

        print("results when max dev accu:")
        print(max_res)
#         self.print_top_accuracy(max_accuracy_top2[0],"dev")
#         self.print_top_accuracy(max_accuracy_top2[1],"test")

        return max_res

    # find top N icon indices and return P,R,F1,TP,TN,FP,FN
    def cal_top_n(self, dataset, str, N=2, stop = sys.maxsize):
        # quick assessment for early termination:
        if len(dataset) > 20000:
            res = self.cal_top_n(dataset, str+"fast", N=2, stop = stop)
            return res

        results = [] # for phrase - top N icons
        indices_arr = range(0, self.num_cols)
        for ph_idx in range(min(stop, len(dataset[1]))):
#             print(dataset[1][ph_idx])
#             print(np.tile(np.array(dataset[1][ph_idx]), (self.num_cols, 1)).shape)

            res = self.session.run(self.prob, feed_dict={
                    self.col:indices_arr,
                    self.phrase_vec: np.tile(np.array(dataset[1][ph_idx]), (self.num_cols, 1))
            })
            results.append(sorted(indices_arr, key=lambda i:res[i], reverse=True)[:N])

        if str == "bench   ":
            for i in range(10):
                print("phrase:", dataset[4][i])
                print("correct icon:",self.model_params.mp_idx2name[dataset[0][i]], \
                      "suggested icons(Top2)", self.model_params.mp_idx2name[results[i][0]],self.model_params.mp_idx2name[ results[i][1]])
                print("\n")
        return self.cal_metrics(results, dataset[2], dataset[0], str, N=N)

    # calculate details
    def cal_metrics(self, results, labels, icons, str, N=2):
        # results: top N icon indices returned by Text2Vec for each phrase
        # label for each phrase-icon pair
        # icon idx for each phrase-icon pair
#         print(results)
#         print(labels)
#         print(icons)

#         if len(results) != len(labels) or len(results) != len(icons):
#             print("error: len of inputs not equal")
#             raise
        P, T, F = [-404]*N, [0]*N, [0]*N
        for i in range(len(results)):
            if labels[i] == 1.0:
                for n in range(N):
                    if icons[i] in results[i][:n+1]:
                        T[n] += 1
                    else:
                        F[n] += 1
        for n in range(N):
            P[n] = T[n]/(T[n]+F[n])

        self.print_top_accuracy_TP(P, T, F, str)
        return P

    def trainSeq(self):
        """train with keywords pair dataset,then move to the next"""
        print(len(self.trainset[0]))
        self.train1()

        self.model_params.learning_rate = 0.0003
        self.model_params.batch_size = 4000
        self.model_params.max_epochs = 100000
        self.initializeDatasetWithBenchmarkTraining()
        print(len(self.trainset[0]))
        print("\n\nstart training using Eriks trainset\n\n")
        self.train()
        raise

    def print_top_accuracy_TP(self, P, T, F, st):
        if len(P) != len(T) or len(T) != len(F):
            raise
        s = "\t"+st + "\t"
        for i in range(len(P)):
            s += "P" +str(i+1)+"="
            s += "%3.3f," %(P[i])
        s = s[:-1] + "; "
        for i in range(len(T)):
            s += "T"+str(i+1)+"="+str(T[i]) + ",F" + str(i+1)+"="+str(F[i])+","
        s = s[:-1]
        print(s)

    def print_top_accuracy(self,P,st):
        s = "\t"+st+"\t"
        for i in range(len(P)):
            s+= "P" +str(i+1)+"="
            s+= "%3.3f," %(P[i])
        s = s[:-1]
        print(s)

    def close(self):
        self.session.close()
        ops.reset_default_graph()
        tf.reset_default_graph()

    def saveModel(self, pathName, V):
        """save the model with Pat1 Pat2 value"""

        if not pathName in self.savedModels:
            print("saving to ", pathName)
#         # save the tf meta model
#             self.savedModels.add(pathName)
#             self.saver.save(self.session, pathName)
            # save the matrix only
            fileObject = open(pathName+".p", "wb")
            pk.dump(V, fileObject)
            fileObject.close()


a = True
b = False
c = True
d = 30
e = False
f = True
g = -20
h = 14
i = 17
j = -2
k = True
l = -6
m = 12
n = False
o = False
p = 21
q = 29
r = False
s = -5
t = True
u = True
v = True
w = 9
x = True
y = -20
z = 25
if 13 <= a > (h >= p) :
    r = w
    w = False
    f = u
else:
    z = -26
    m = g
    q = True
if -5 > e :
    u = True
    z = d
elif False - 2 :
    n = s
    z = f
else:
    f = True
    e = False
if j :
    w = 19
else:
    r = f
q = k
print( l )
print( c )
print( g )
print( w )
print( s )
print( m )
print( e )
print( q )
print( o )
print( u )
print( k )
print( d )
print( y )
print( t )
print( a )
print( j )
print( p )
print( b )
print( r )
print( f )
print( v )
print( h )
print( n )
print( x )
print( z )
print( i )


"""Reports on the azure spend to slack"""

import csv
import os
from decimal import Decimal
from slack_sdk.webhook import WebhookClient
from datetime import date

with open("report.csv", "r", encoding="utf-8") as csv_file:
    csv_reader = csv.DictReader(csv_file)
    daily_price = [Decimal(row["CostInBillingCurrency"]) for row in csv_reader]
    cost_to_date = sum(daily_price)

URL = os.getenv("SLACK_WEBHOOK_URL")

wh = WebhookClient(URL)
total = round(cost_to_date,2)

today = date.today()
resp = wh.send(
    blocks=[
        {
            "type": "header",
            "text": {
                "type": "plain_text",
                "text": f"Current Azure spend for {today.strftime('%m-%Y')}",
                "emoji": True
            }
        },{
            "type": "section",
            "fields": [{
                "type": "mrkdwn",
                "text": "*Sentinel*",
            },
            {
                "type": "plain_text",
                "text": f"${total} CAD",
            }]
        },{
            "type": "divider",
        },
        {
            "type": "section",
            "fields": [{
                "type": "mrkdwn",
                "text": "*Total*",
            },
            {
                "type": "plain_text",
                "text": f"${total} CAD",
            }]
        }
    ]
)

print(f"Response: {resp.status_code}")
print(f"Response: {resp.body}")


from .return_class import AbstractApiClass

class IndexingConfig(AbstractApiClass):
    """
        The indexing config for a Feature Group

        Args:
            client (ApiClient): An authenticated API Client instance
            primaryKey (str): A single key index
            updateTimestampKey (str): The primary timestamp feature
            lookupKeys (list[str]): A multi-key index. Cannot be used in conjuction with primary key.
    """

    def __init__(self, client, primaryKey=None, updateTimestampKey=None, lookupKeys=None):
        super().__init__(client, None)
        self.primary_key = primaryKey
        self.update_timestamp_key = updateTimestampKey
        self.lookup_keys = lookupKeys

    def __repr__(self):
        repr_dict = {f'primary_key': repr(self.primary_key), f'update_timestamp_key': repr(
            self.update_timestamp_key), f'lookup_keys': repr(self.lookup_keys)}
        class_name = "IndexingConfig"
        repr_str = ',\n  '.join([f'{key}={value}' for key, value in repr_dict.items(
        ) if getattr(self, key, None) is not None])
        return f"{class_name}({repr_str})"

    def to_dict(self):
        """
        Get a dict representation of the parameters in this class

        Returns:
            dict: The dict value representation of the class parameters
        """
        resp = {'primary_key': self.primary_key, 'update_timestamp_key':
                self.update_timestamp_key, 'lookup_keys': self.lookup_keys}
        return {key: value for key, value in resp.items() if value is not None}


def solution(t, p):
    answer = 0
    len_p = len(p)

    total = []
    for i in range(len(t)-len(p)+1):
        total.append(t[i:i+len(p)])

    for t in total:
        if int(t) <= int(p):
            answer += 1

    return answer


from django.urls import include, path
from allauth import urls as auth_urls
from drf_spectacular.views import (
    SpectacularAPIView,
    SpectacularRedocView,
    SpectacularSwaggerView,
)
from .team_urls import buildTeamRouterUrls
from .user_urls import buildUserRouterUrls
from api import views
from api.settings import BASE_URL
from ..admin import sluggo_admin

urlpatterns = [
    # Optional UI:
    path(BASE_URL + "api/schema/", SpectacularAPIView.as_view(), name="schema"),
    path(
        BASE_URL + "api/schema/swagger-ui/",
        SpectacularSwaggerView.as_view(url_name="schema"),
        name="swagger-ui",
    ),
    path(
        BASE_URL + "api/schema/redoc/",
        SpectacularRedocView.as_view(url_name="schema"),
        name="redoc",
    ),
    path(BASE_URL + "api/", include(buildTeamRouterUrls())),
    path(BASE_URL + "api/user/", include(buildUserRouterUrls())),
    path(BASE_URL + "auth/", include("dj_rest_auth.urls")),
    path(BASE_URL + "auth/registration/", include("dj_rest_auth.registration.urls")),
    path(BASE_URL + "auth/slack/", views.SlackLogin.as_view(), name="slack_login"),
    path(BASE_URL + "auth/accounts/", include("allauth.urls")),
    path(BASE_URL + "admin/", sluggo_admin.urls),
    path(BASE_URL + "accounts/", include(auth_urls)),
]