from collections import OrderedDict
from werkzeug.local import LocalProxy
from ..wrappers import CanellaModule
from .models import *
from ..babel import lazy_gettext
from .defaults import DEFAULT_SETTINGS, DEFAULT_CATEGORIES

settings = CanellaModule(
    'settings', __name__)

class Settings(object):
    def __init__(self, source):
        self.source = source
        self.options = []
        self.categories = OrderedDict()
        for name, kwargs in DEFAULT_CATEGORIES.items():
            self.register_category(name, **kwargs)
        for option in DEFAULT_SETTINGS:
            self.register_setting(option)

    def register_setting(self, option):
        assert option.name not in [option.name for option in self.options], '%s is already registered' %option.name
        if not option.category:
            option.category = self.categories['general']
        elif option.category not in self.categories:
            raise ValueError('category %s does not exists, please register it first' %option.category)
        self.options.append(option)
        if option.name not in self.source:
            self.edit(option.name, option.default, new=True)

    def register_category(self, name, label, icon=''):
        if name in self.categories:
            raise ValueError('%s category already registered' %name)
        self.categories[name] = dict(label=label, icon=icon)

    def __getattr__(self, key):
        if key in self.source:
            return self.source[key]

    def edit(self, key, value, new=False):
        if key not in [option.name for option in self.options]:
            raise ValueError('Setting %s is not registered yet' %key)
        if not new and key not in self.source:
            raise AttributeError('%r has no attribute %r' %(self.source, key))
        self.source[key] = value
        db.session.commit()

def get_active_site():
    from ..core.models import Site
    active_site = Site.query.filter(Site.is_active==True).one()
    return active_site

current_site = LocalProxy(lambda: get_active_site())
current_settings = LocalProxy(lambda: Settings(get_active_site().settings))


from pathlib import Path

import os

# caminho absoluto deste script
PATH_SCRIPT= os.path.abspath(__file__)

# caminhos dos diretórios do projeto
MODELO_DIR = Path(PATH_SCRIPT).parent.parent
CONFIGURACOES_DIR = MODELO_DIR / "configurcoes"
DADOS_DIR = MODELO_DIR / "dados"
MODEL_TREINADO_DIR = MODELO_DIR / "modelo_treinado"
ROOT = MODELO_DIR.parent


import sys

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None
        self.tail = None
        self.size = 0

    def __repr__(self):
        node = self.head
        nodes = []
        while node != None:
            nodes.append(node.value)
            node = node.next
        nodes.append(None)
        return ('->'.join(list(map(str, nodes))))

    def add_first(self, node):
        node.next = self.head
        self.head = node
        self.size += 1

    def add_last(self, node):
        self.tail.next = node
        self.tail = node
        self.size += 1

    def add_after(self, target_value, new_node):
        node = self.head
        while node is not None:
            if node.value == target_value:
                new_node.next = node.next
                node.next = new_node
                self.size += 1
                break
            else:
                node = node.next

    def remove_node(self, target_value):
        if self.head.value == target_value:
            self.head = self.head.next
            return
        previous_node = self.head
        node = self.head
        while node != None:
            node = node.next
            if node.value == target_value:
                if previous_node.next == self.tail:
                    self.tail = previous_node
                previous_node.next = node.next
                self.size -= 1
                return
            previous_node = node

class Stack:

    def __init__(self):
        self.stack = LinkedList()
        self.min = LinkedList()

    def push(self, value):
        if self.stack.head == None:
            self.stack.head = Node(value)
            self.min.head = Node(value)
            self.min.tail = Node(value)
        else:
            self.stack.add_first(Node(value))
            self.insert_min(Node(value))

    def pop(self):
        remove_value = self.stack.head.value
        self.stack.remove_node(remove_value)
        self.min.remove_node(remove_value)
        return remove_value

    def insert_min(self, node_value):
        node = self.min.head
        if node.value > node_value.value:
            self.min.add_first(node_value)
            return
        prev = node
        while node.next != None:
            node = node.next
            if node.value > node_value.value:
                self.min.add_after(prev.value, node_value)
                return
            prev = node
        self.min.add_after(prev.value, node_value)

    def show_min(self):
        sys.stdout.write(str(self.min.head.value) + '\n')

stack = Stack()

n = int(sys.stdin.readline())
for i in range(n):
    inp_list = list(map(int, sys.stdin.readline().split(' ')))
    if inp_list[0] == 1:
        stack.push(inp_list[1])
    elif inp_list[0] == 2:
        stack.pop()
    else:
        stack.show_min()

Part_1/Алгоритмы и структуры данных/Homeworks/base_data_structure/Task_A_old.py


""""“esxi_host_datastore”"

Revision ID: 4451a81a7415
Revises: 875c236094ba
Create Date: 2017-10-20 15:22:26.234385

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = '4451a81a7415'
down_revision = '875c236094ba'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('datastore_host',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('esxi_host_id', sa.String(length=36), nullable=False),
    sa.Column('esxi_datastore_id', sa.String(length=36), nullable=False),
    sa.ForeignKeyConstraint(['esxi_datastore_id'], ['esxi_datastore.id'], ),
    sa.ForeignKeyConstraint(['esxi_host_id'], ['esxi_host.id'], ),
    sa.PrimaryKeyConstraint('id', 'esxi_host_id', 'esxi_datastore_id'),
    sa.UniqueConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('datastore_host')
    # ### end Alembic commands ###


transactions = ()

class Balance_properties:
    def __init__(self):
        self.balance = 0

    def check_balance(balance, self):
        print(f'Your current balance is: ${balance}. ')

    def deposit(amount, balance, self):
        balance = balance + amount
        return balance

    def check_withdrawal(amount, balance, self):
        if balance >= amount:
            return True
        else:
            return False
            # else:
            #     return ''

    def withdraw(amount, balance, self, transactions):
        balance = balance - amount
        return balance, transactions.append(f'User withrew ${amount}. New balance is, ${balance}. ')

    def menu(self):

        print("Welcome to the ATM")
        while True:
            menu_display = """
            1) Check your Balance
            2) Make a Deposit
            3) Make a withdrawal
            4) View Transactions
            5) Exit
            """
            choice = input(menu_display)
        return choice

def main():
    while True:
        choice = menu()
        if choice == 1:
            check_balance(balance)
        elif choice == 2:
            amount = int(input('How much would you like to deposit today?'))
            balance = deposit(balance, amount)
            print(f'This is your new balance: ${balance}')
        elif choice == 3:
            amount = int(input('How much would you like to withdraw today?'))
            if check_withdrawal == True:
                balance = withdraw(amount, balance, transactions)
            else:
                print(f'Transaction would overdraw your account by ({balance} - {amount})')
        elif choice == 4:
            print(f'here is your transaction history \n {transactions}')
        elif choice == 5:
            Break

main()


'''
Source: https://leetcode.com/problems/reverse-integer/
Author: Vishal Prabhachandar
Date: 2021-June-20

Time complexity: O(n)
Space complexity: O(1)
'''

class Solution:
    def reverse(self, x: int) -> int:
        '''
        reverse the integer

        :param  x: integer
        :return x: integer
        '''
        # constraint
        cons = 2**31
        # handling negative values
        if x < 0:
            x = int(str(x*-1)[::-1])
            x *= -1
        else:
            x = int(str(x)[::-1])
        # return based on constraint
        if x > (cons-1) or x < -cons:
            return(0)
        else:
            return(x)


#!/usr/bin/python3
# Program A
def evilGetLength1(ilist):
	length = len(ilist)
	del ilist[0:length] # Muhaha: clear the list
	return length # drawing A here
mylist = [12.8, -14.9, 16.6, -3.0]
l = evilGetLength1(mylist)
print(mylist) # predict the answer
print(l) # predict the answer

# Program B
def evilGetLength2(ilist):
	length = len(ilist)
	ilist = [] # Muhaha: clear the list
	return length # drawing B here
mylist = [12.8, -14.9, 16.6, -3.0]
l = evilGetLength2(mylist)
print(mylist) # predict the answer
print(l) # predict the answer


import cv2 as cv
import time
import os

"""
detect_face method receives input as a colour image and a cascade detector,
then detects human faces from the input image, and returns a list of detected human faces (if any).
"""

path = '/Users/johnathontoh/Desktop/SIT789 - Applications of Computer Vision and Speech Processing/Week 4/Task 4.2P/Resources_4.2/FaceImages/'

def detect_face(image, scale, neighbours, size, flags):
    start_time = time.time()
    image = cv.imread(image)
    #  pre-trained cascade detector
    cascade_detector = cv.CascadeClassifier('/Users/johnathontoh/Desktop/SIT789 - Applications of Computer Vision and Speech Processing/Week 4/Task 4.2P/Resources_4.2/haarcascade_frontalface_default.xml')
    #convert input image to grayscale
    image_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    faces = cascade_detector.detectMultiScale(image_gray,
                                                scaleFactor = scale, #the ratio between two consecutive scales
                                                minNeighbors = neighbours, #minimum number of overlapping windows to be considered
                                                minSize = size, #minimum size of detection window (in pixels)
                                                flags = flags) #scale the image rather than detection window
    #return faces
    print('Face detection is performed in %s seconds ---' % (time.time() - start_time))
    if (faces is not None):
        print('Found ', len(faces), ' faces')
    else:
        print('There is no face found!')

    from matplotlib import pyplot as plt
    for (x, y, w, h) in faces: #(x, y) are the coordinate of the topleft corner,
        # w, h are the width and height of the bounding box
        cv.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

    plt.imshow(image[:,:,::-1]) # RGB-> BGR
    plt.show()

#detect_face(path + 'img_1123.jpg', 1.2, 20, (50, 50), cv.CASCADE_SCALE_IMAGE)

def video_cam():
    def detect_face_function(image, cascade_detector):
        #convert input image to grayscale
        image_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
        faces = cascade_detector.detectMultiScale(image_gray,
        scaleFactor = 1.1, #the ratio between two consecutive scales
        minNeighbors = 5, #minimum number of overlapping windows to be considered
        minSize = (30, 30), #minimum size of detection window (in pixels)
        flags = cv.CASCADE_SCALE_IMAGE) #scale the image rather than detection window
        return faces

    #OPENCV_VIDEOIO_PRIORITY_MSMF = 0
    #initialise webcam
    cam = cv.VideoCapture(0)
    #initialise cascade_detector
    cascade_detector = cv.CascadeClassifier('/Users/johnathontoh/Desktop/SIT789 - Applications of Computer Vision and Speech Processing/Week 4/Task 4.2P/Resources_4.2/haarcascade_frontalface_default.xml')
    while True:
        #read the image from the cam
        _, image = cam.read()
        #detect human faces from the current image using the cascade_detector

        faces = detect_face_function(image, cascade_detector)
        #display detected faces
        for x, y, w, h in faces:
            cv.rectangle(image, (x, y), (x + w, y + h), color = (0, 255, 0))
            cv.imshow('face detection demo', image)
        if cv.waitKey(1) == ord("q"):
            cv.destroyAllWindows()
            break
    cam.release()

#video_cam()

import imutils
from imutils.object_detection import non_max_suppression
import numpy as np

def detect_pedestrian_function (winstride, padding, scale_num):
    def nms(boxes):

        # We first convert boxes from list to array as required by non_max_suppression method
        # #In addition, each box in the array is encoded by the topleft and bottomright corners
        boxes_array = np.array([[x, y, x + w, y + h] for (x, y, w, h) in boxes])
        boxes_array = non_max_suppression(boxes_array, probs=None, overlapThresh=0.65)
        # create a new list of boxes to store results
        boxes_list = []
        for top_x, top_y, bottom_x, bottom_y in boxes_array: boxes_list.append(
            [top_x, top_y, bottom_x - top_x, bottom_y - top_y])
        return boxes_list

    def detect_pedestrian(image):
        #initialise the HOG descriptor and SVM classifier
        hog = cv.HOGDescriptor()
        hog.setSVMDetector(cv.HOGDescriptor_getDefaultPeopleDetector())
        image_resized = imutils.resize(image,
                                        width = min(400, image.shape[1])) #resize the input image so that

        scale = image.shape[1] / image_resized.shape[1]
        #detect pedestrians
        (boxes, _) = hog.detectMultiScale(image_resized,
                                          winStride= winstride,  # horizontal and vertical stride
                                          padding = padding,  #horizontal and vertical padding for each window
                                          scale = scale_num) #scale factor between two consecutive scales

        # non-maximum suppression
        boxes = nms(boxes)
        # resize the bounding boxes
        for box in boxes:
            box[0] = np.int(box[0] * scale)
            box[1] = np.int(box[1] * scale)
            box[2] = np.int(box[2] * scale)
            box[3] = np.int(box[3] * scale)

        return boxes

    image = cv.imread('/Users/johnathontoh/Desktop/SIT789 - Applications of Computer Vision and Speech Processing/Week 4/Task 4.2P/Resources_4.2/PedestrianImages/person_029.png')
    start_time = time.time()
    pedestrians = detect_pedestrian(image)
    print('Pedestrian detection is performed in %s seconds ---' % (time.time() - start_time))

    from matplotlib import pyplot as plt
    if (pedestrians is not None):
        print('Found ', len(pedestrians), ' pedestrians')

        for (x, y, w, h) in pedestrians:
            cv.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 10)
        plt.imshow(image[:,:,::-1]) # RGB-> BGR
        plt.show()
    else:
        print('There is no pedestrian found!')

#detect_pedestrian_function ((2,2), (6,6), 1.05)
#detect_pedestrian_function ((4,4), (6,6), 1.05)
#detect_pedestrian_function ((6,6), (6,6), 1.05)
#detect_pedestrian_function ((8,8), (6,6), 1.05)

#detect_pedestrian_function ((4,4), (4,4), 1.05)
#detect_pedestrian_function ((4,4), (6,6), 1.05)
#detect_pedestrian_function ((4,4), (8,8), 1.05)
#detect_pedestrian_function ((4,4), (10,10), 1.05)

#detect_pedestrian_function ((4,4), (6,6), 1.05)
#detect_pedestrian_function ((4,4), (6,6), 1.1)
#detect_pedestrian_function ((4,4), (6,6), 1.15)
#detect_pedestrian_function ((4,4), (6,6), 1.2)


def check(p1,p2):
    if p1[0]==p2[0]:
        return "None"
    else:
        return str((p1[1]-p2[1])/(p1[0]-p2[0]))
n=int(input())
s=[]
for i in range(n):
    point=list(map(int,input().split(",")))
    s.append(point)
# [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
ans=[]
for i in range(len(s)-1):
    tem=[]
    for j in range(i+1,len(s)):
        k=check(s[i],s[j])
        tem.append(k)
    Set=list(set(tem))
    count=[]
    for e in Set:
        count.append(tem.count(e)+1)
    count.sort()
    ans.append(count[-1])
ans.sort()
print(ans[-1])


"""

looping example :
there are two types of Loop

1) for loop : for loop - sequence contrl loop
2)while loop : while loop : entry controlled loop

        -> condition check at entry level
        if condition goes true loop will execute
"""

"""
while loop :
    syntax :
    intialization

    while condition:
        statement
        updation

"""

for i in range(5): #by default loop start from 0 to 4
    print(i)

for i in range(1,6): #by default loop start from 1 to 5
    print(i)

# accept 5 numbers from user
for i in range (1,6):
    num = int(input("Enter number : "))

# accept 5 numbers from user and sum
sum= 0
for i in range (1,6):
    num = int(input("Enter number : "))
    sum += num

print("ans = ",sum)

status = True
while status:
    num = int(input("Enter number : "))
    choice =int(input("Do you want to continue press 1 for yes and pres 2 for no : "))
    if choice ==1:
        status = True
    else:
        status = False


#!/usr/bin/env python
# -*- coding: utf-8 -*- #
from __future__ import unicode_literals

AUTHOR = u'cnsworder'
SITENAME = u'Linux Developer'
#SITEURL = ''
SITEURL = 'http://docs.cnsworder.com'

TIMEZONE = 'Asia/Shanghai'

DEFAULT_LANG = u'zh'

# Feed generation is usually not desired when developing
FEED_ALL_ATOM = None
CATEGORY_FEED_ATOM = None
TRANSLATION_FEED_ATOM = None

FEED_ATOM = 'feeds/all.xml'
FEED_DOMAIN = SITEURL

# Blogroll
LINKS =  (('Pelican', 'http://getpelican.com/'),
          ('Python.org', 'http://python.org/'),
          (u'问答', 'http://cnsworder.com'),
          (u'缘起', "http://blog.csdn.net/cnsword"))

# Social widget
SOCIAL = (('twitter', 'https://twitter.com/cnsworder'),
          ('github', 'https://github.com/cnsworder'),
          ('google+', 'https://plus.google.com/u/0/106278662052013132197/posts/p/pub'),)

DEFAULT_PAGINATION = 20

STATIC_PATHS = ([u'highlight', u'img', u'publication/image'])

#EXTRA_PATH_METADATA =

#FILES_TO_COPY = (('publication/image/*', 'img/'),);

DISQUS_SITENAME = u"cnsworder"
#DISQUS_NO_ID = True

GITHUB_URL = u'http://github.com/cnsworder/staticdocs'

#THEME = "pelican-bootstrap3"
THEME = "pelican-octopress-theme"
#THEME = "bootstrap2"
# Uncomment following line if you want document-relative URLs when developing
#RELATIVE_URLS = True


from PIL import Image, ImageFilter

print('''---------------------Pillow---------------------
''')

# PIL：Python Imaging Library，已经是Python平台事实上的图像处理标准库了。PIL功能非常强大，但API却非常简单易用。

# *********** 操作图像 ***********

# 来看看最常见的图像缩放操作，只需三四行代码：
#
# 打开一个jpg图像文件，注意是当前路径:
im = Image.open('test.jpg')
# 获得图像尺寸:
w, h = im.size
print('Original image size: %sx%s' % (w, h))
# 缩放到50%:
im.thumbnail((w // 2, h // 2))
print('Resize image to: %sx%s' % (w // 2, h // 2))
# 把缩放后的图像用jpeg格式保存:
im.save('thumbnail.jpg', 'jpeg')

# 应用模糊滤镜:
im2 = Image.open('test.jpg')
im3 = im2.filter(ImageFilter.BLUR)
im3.save('blur.jpg', 'jpeg')


# -*- coding:UTF-8 -*-
from ui_click.models import clickData
from django.http import HttpResponse
def main(request):
	channel = request.GET.get("channel","")
	page = request.GET.get("page","")
	click = request.GET.get("click","")

	p = clickData(channel=channel, page=page, content=click)
	p.save()
	return HttpResponse("ok");


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from telegram.constants import PARSEMODE_HTML

def send_message(update, context, msg, parse_HTML = False) -> None:
    if parse_HTML:
        context.bot.send_message(chat_id=update.effective_chat.id, text = msg, parse_mode = PARSEMODE_HTML)
    else:
        context.bot.send_message(chat_id=update.effective_chat.id, text = msg)

def c_bot(update, context) -> None:
    # /bot
    send_message(update, context, 'Hello there! I\'m JarvisBot, a bot for group management.\n\nFor a list of commands, type /commands')

def c_commands(update, context) -> None:
    # /commands
    send_message(update, context, 'Command list for JarvisBot:\n/bot - Display general info about the bot\n/commands - Display a list of commands\n/lyrics - Display the lyrics of a song')

def c_kick(update, context) -> None:
    chat_id = update.message.chat_id
    prev_msg = update.message.reply_to_message

    if not prev_msg:
        msg = 'Usage: reply to a message of the user you want to kick with /kick'
    else:
        user_id = prev_msg.from_user.id
        bot = context.bot
        code = kick_member(bot, chat_id, user_id)

        if code == 1:
            msg = 'User kicked succesfully. Good bye, bitch!'
        elif code == -1:
            msg = 'Couldn\'t kick the user'
        elif code == -2:
            msg = 'Not enough rights to kick chat member'
        elif code == -3:
            msg = 'User is an administrator of the chat'
        else:
            msg = 'Unknown error'

    send_message(update, context, msg)

def kick_member(bot, chat_id, user_id) -> int:
    return_code = 0
    try:
        user_data = bot.getChatMember(chat_id, user_id)
        if (user_data['status'] != "left") and (user_data['status'] != "kicked"):
            bot.kickChatMember(chat_id, user_id)
            bot.unbanChatMember(chat_id, user_id)
            return_code = 1
        else:
            return_code = -1
    except Exception as e:
        if str(e) == "Not enough rights to restrict/unrestrict chat member":
            return_code = -2
        elif str(e) == "User is an administrator of the chat":
            return_code = -3

    return return_code


# from ..util.inputs import *
import collections
import functools
import itertools
import operator
import os
import regex
import sys
import typing

def parse_line(line: str) -> str:
	"""Parses a line of input. By default, does nothing.
	Arguments:
	line -- the line of input
	"""
	return line

def solve(input_file: typing.IO) -> typing.Generator[str, None, None]:
	"""Generates solutions to the problem.
	Arguments:
	input_file -- the file containing the input
	"""
	data = [parse_line(line.strip()) for line in input_file if line.strip()]
	ran = set()
	ip = 0
	acc = 0

	yield recurse(data, ip, acc, ran, False)

def recurse(data, ip, acc, ran, swap):

	if ip == len(data):
		return acc

	if ip in ran:
		return

	if ip > len(data):
		return acc

	line = data[ip]
	print(ip, line)

	ran.add(ip)
	args = line.split(" ")

	if args[0] == "jmp":
		ans = recurse(data,ip+int(args[1]),acc,ran,swap)
		if ans: return ans

		if not swap:
			ans = recurse(data,ip+1,acc,ran,True)
			if ans: return ans

	elif args[0] == "nop":
		ans = recurse(data,ip+1,acc,ran,swap)
		if ans: return ans

		if not swap:
			ans = recurse(data,ip+int(args[1]),acc,ran,True)
			if ans: return ans
	else:
		return recurse(data,ip+1,acc+int(args[1]),ran,swap)

def main() -> None:
	"""Called when the script is run."""
	with open(f'input.txt', 'r') as input_file:
		for solution in solve(input_file):
			print(solution)

if __name__ == '__main__':
	main()


import asyncio
import json
from concurrent.futures import ThreadPoolExecutor
from logging import INFO, FileHandler, Formatter, StreamHandler, getLogger
from time import time

from aiohttp import web
from aiohttp.web_request import Request
from apscheduler.schedulers.asyncio import AsyncIOScheduler

import status_manage

config = status_manage.load_config()
system_time = ""

msg_queue = []

result = {}

logger = getLogger('scheduler')
logger.setLevel(level=INFO)

formatter = Formatter(
    '%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')

file_handler = FileHandler(filename='scheduler.log', mode='a')
file_handler.setLevel(level=INFO)
file_handler.setFormatter(formatter)

stream_handler = StreamHandler()
stream_handler.setLevel(level=INFO)
stream_handler.setFormatter(formatter)

logger.addHandler(file_handler)
logger.addHandler(stream_handler)

async def msg_handler(request: Request):
    event = json.loads((await request.content.read()).decode('utf-8'))
    event['timestamp'] = int(round(time() * 1000))
    new_msg_flag = True
    i = 0
    for msg in msg_queue:
        if event['server'] == msg['server'] and event['voicechannel'] == msg[
                'voicechannel']:
            new_msg_flag = False
            break
        i += 1
    if new_msg_flag:
        msg_queue.append(event)
    else:
        msg_queue[i]['botid'].append(event['botid'][0])
        msg_queue[i]['able'].append(event['able'][0])
        msg_queue[i]['already_in'].append(event['already_in'][0])
    signatures = f"{event['server']}-{event['voicechannel']}-{event['botid'][0]}"
    result[signatures] = 'WAITING'
    while result[signatures] == 'WAITING':
        await asyncio.sleep(0.1)
    return web.Response(status=200, text=result[signatures])

app = web.Application()
app.add_routes([web.post('/scheduler', msg_handler)])

async def timeout():
    global msg_queue
    global system_time
    global result
    system_time = int(round(time() * 1000))
    timeout_queue = []
    keep_queue = []
    for msg in msg_queue:
        if system_time < msg['timestamp'] + 1500:
            keep_queue.append(msg)
        else:
            timeout_queue.append(msg)
    msg_queue = keep_queue
    for msg in timeout_queue:
        logger.info(msg)
        if True in msg['already_in']:
            for botid in msg['botid']:
                signatures = f"{msg['server']}-{msg['voicechannel']}-{botid}"
                result[signatures] = "REFUSE"
                logger.info("REFUSE")
        else:
            i = 0
            used = False
            botnum = len(msg['botid']) - 1
            for botid in msg['botid']:
                signatures = f"{msg['server']}-{msg['voicechannel']}-{botid}"
                result[signatures] = "REFUSE"
                if msg['able'][i]:
                    if not used:
                        used = True
                        result[signatures] = "ACCEPT"
                        logger.info("ACCEPT")
                        continue
                else:
                    if i == botnum:
                        result[signatures] = "REPORT"
                        logger.info("REPORT")
                        continue
                logger.info("REFUSE")
                i += 1

if __name__ == '__main__':
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    executor = ThreadPoolExecutor(1000)
    sched = AsyncIOScheduler(timezone='Asia/Shanghai')
    sched.add_job(timeout, 'interval', seconds=0.2)
    sched.start()
    web.run_app(app, port=int(config["schedule_server_port"]), loop=loop)


## 老鼠书_第28章
## classtools.py

class AttrDisplay():
    def _gatherAttrs(self):
        attrs = []
        for key in sorted(self.__dict__):  # __dict__ => 一个关于属性名与属性值的字典
            attrs.append('%s=%s' % (key, getattr(self, key)))  # getattr 获取实例的属性值，属性名用字符串传入
        return ', '.join(attrs)
    def __repr__(self):
        return '[%s: %s]' % (self.__class__.__name__, self._gatherAttrs())  # __class__ => 实例的类； __class__.__name__ => 实例的类的名称

if __name__ == '__main__':
    class TopTest(AttrDisplay):
        count = 0
        def __init__(self):
            self.attr1 = TopTest.count
            self.attr2 = TopTest.count + 1
            TopTest.count += 2
    class SubTest(TopTest):
        pass

    X, Y, Z, A = TopTest(), TopTest(), SubTest(), SubTest()
    print(X)
    print(Y)
    print(Z)
    print(A)


import random
from math import sqrt

def  someDoMonteCarlo(n):
    circle = 0

    for i in range(1, n + 1):

        x = random.random()
        y = random.random()

        # Checking to see if the produced number falls into the circle
        if sqrt(x ** 2 + y ** 2) <= 1:
            circle += 1

    Pi = 4 * (circle / n)
    return Pi

n = [10, 100, 1000]
Pi_storage = {}

for j in n:
     Pi_storage[j] = someDoMonteCarlo(j)
for k in n:
     print("The result of pi estimation after " + str(k) + " attempts is equal to: " + str(Pi_storage[k]))


from doctest import IGNORE_EXCEPTION_DETAIL
import time
from os.path import join
import numpy as np
import torch
import torch.nn.functional as f
from torch.utils.data import DataLoader
from loss import loss_fcn
from utils import *
from model import *
from tqdm import tqdm
from config import DEVICE, D_NUM, D_SCALE
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

VISUALIZE = True

def test(epochs:int,
          test_data_loader:DataLoader,
          save_path:str=join('.','checkpoints'),
          checkpoint:str=None,
          model:torch.nn.Module=None,
          start_epoch:int=0):

    if isinstance(test_data_loader, str):
        test_data_loader = torch.load(test_data_loader)

    num_trainloader = len(test_data_loader)

    if model is not None:
        print("Using Passed In Model.")
        epoch_loss        = [np.zeros(num_trainloader) for i in range(epochs)]
        epoch_initial_acc = [np.zeros(num_trainloader) for i in range(epochs)]
        epoch_refined_acc = [np.zeros(num_trainloader) for i in range(epochs)]
    elif checkpoint is not None:
            print('Loading Checkpoint...')
            out = load_from_ckpt(checkpoint, epochs)
            model, start_epoch, \
                epoch_loss, epoch_initial_acc, epoch_refined_acc = out
    else:
        raise Exception("Test requires a model input")

    print('Start Training From Epoch #{:d}'.format(start_epoch))

    # print_gpu_memory()
    print('Model sent to device: ', DEVICE)
    model = model.to(DEVICE)
    # print_gpu_memory()

    id_str = "test_"+str(int(time.time()))

    model.eval()

    total = num_trainloader
    with tqdm(total=total) as pbar:
        for epoch in range(start_epoch, epochs):
            # with torch.no_grad():
            print("----- TESTING EPOCH #{:d} -----".format(epoch+1+start_epoch))

            model.train()
            epoch_start_time = time.time()
            current_time     = time.time()

            batch_loss        = np.zeros(num_trainloader)
            batch_initial_acc = np.zeros(num_trainloader)
            batch_refined_acc = np.zeros(num_trainloader)

            with torch.no_grad():
                for batch_idx, batch in enumerate(test_data_loader):

                    batch_size, n_views, ch, h, w = batch['input_img'].size()
                    _         , _      , _ ,dh,dw = batch['depth_ref'].size()

                    nn_input= torch.reshape(batch['input_img'], (batch_size*n_views, ch, h, w)).to(DEVICE) # b*n, ch, h, w
                    gt_depth= torch.reshape(batch['depth_ref'], (batch_size, 1, dh, dw)).to(DEVICE)       # b*n, ch, h, w
                    K_batch = torch.reshape(batch['K'], (batch_size*n_views, 3, 3)) # b*n, 1, 3, 3
                    R_batch = torch.reshape(batch['R'], (batch_size*n_views, 3, 3))
                    T_batch = torch.reshape(batch['T'], (batch_size*n_views, 3, 1))
                    d_min   = batch['d']
                    d_int   = batch['d_int']

                    # d_min = torch.tensor(0)*d_min # error in dataset, use d_min = 0
                    d_int = d_int.div(d_int) # set this to 1 so that we fully control interval from config

                    # print("NN Input sent to ", DEVICE, "with shape: ", nn_input.size())
                    # print_gpu_memory()

                    initial_depth_map, refined_depth_map = model(nn_input, K_batch,
                                R_batch, T_batch, d_min, d_int, batch_size, n_views)

                    loss, initial_acc, refined_acc = loss_fcn(gt_depth,
                                            initial_depth_map, refined_depth_map)

                    # store statistics of current item in batch
                    batch_loss[batch_idx] = float(loss.mean())
                    batch_initial_acc[batch_idx] = float(initial_acc.mean())
                    batch_refined_acc[batch_idx] = float(refined_acc.mean())

                    current_time = time.time()

                    if True or (batch_idx+1) % 14 == 0:
                        print(
                            "Epoch: #{:d} Batch: {:d}/{:d}  Time: {:g}\t"
                            "Loss(curr/avg) {:.4f}/{:.4f}\t"
                            "Acc 1(curr/avg) {:.4f}/{:.4f}\t"
                            "Acc 2(curr/avg) {:.4f}/{:.4f}\t"
                            .format(epoch,
                                    batch_idx+1,
                                    num_trainloader,
                                    current_time - epoch_start_time,
                                    batch_loss[batch_idx],
                                    np.mean(batch_loss[:(batch_idx+1)]),
                                    batch_initial_acc[batch_idx],
                                    np.mean(batch_initial_acc[:(batch_idx+1)]),
                                    batch_refined_acc[batch_idx],
                                    np.mean(batch_refined_acc[:(batch_idx+1)])
                                    )
                            )

                        if VISUALIZE:
                            for b in range(batch_size):
                                visualize_depth(batch['input_img'][b,0].unsqueeze(0),
                                                gt_depth[b].unsqueeze(0),
                                                initial_depth_map[b].unsqueeze(0),
                                                refined_depth_map[b].unsqueeze(0))
                        pbar.update(1)

            # save stats for each epoch
            epoch_loss[epoch]        = batch_loss
            epoch_initial_acc[epoch] = batch_initial_acc
            epoch_refined_acc[epoch] = batch_refined_acc

            torch.save({'loss': epoch_loss,
                        'acc_1': epoch_initial_acc,
                        'acc_2': epoch_refined_acc,
                        }, join(save_path, 'testresults_'+id_str+"_"+str(epoch)))

    return epoch_loss, epoch_initial_acc, epoch_refined_acc

def visualize_depth(rgb:torch.Tensor, gt:torch.Tensor, initial:torch.Tensor, refined:torch.Tensor):

    import matplotlib.pyplot as plt
    def plot_depth(img:torch.Tensor, fig, idx, label, maxmax):
        img = img.cpu()
        # img = img-img.min()
        # img = img/img.max()
        img = img/maxmax
        fig.add_subplot(2, 2, idx+1)
        plt.title(label)
        plt.axis('off')
        depth = plt.imshow( (img[0] * 255).type(torch.ByteTensor),  cmap='plasma', vmin=0, vmax=255)
        cbar = plt.colorbar(depth)
        cbar.set_label('Depth Value')
        return fig

    # ignore invalid points in the depth map
    for i in range(gt.size()[0]):

        mask = torch.eq(gt, torch.tensor(0.0).to(DEVICE)).float()
        p_valid = torch.neg(mask - torch.tensor(1.0)).sum((1,2,3))

        fig = plt.figure()
        plt.axis('off')
        plt.suptitle("Depth Comparison, {:d} Valid Points".format(int(p_valid)))
        plt.subplots_adjust(hspace=0.1)

        img = rgb[i*3]
        img = img-img.min()
        img = img/img.max()
        fig.add_subplot(2, 2, 1)
        plt.title("Original")
        plt.axis('off')
        plt.imshow( img.permute(1, 2, 0).cpu() )

        maxmax = torch.max(torch.max((gt)[i].max(), initial[i].max()), refined[i].max()).cpu()

        img = (gt)[i]
        fig = plot_depth(img, fig, 1, "Truth", maxmax)

        # print(img.min(), img.max())

        img = initial[i]
        fig = plot_depth(img, fig, 2, "Initial", maxmax)

        # print(torch.abs(initial[i]-gt[i]).min(), torch.abs(initial[i]-gt[i]).max())

        img = refined[i]
        fig = plot_depth(img, fig, 3, "Refined", maxmax)

        # print(torch.abs(refined[i]-gt[i]).min(), torch.abs(refined[i]-gt[i]).max())

        plt.show()

        del fig

def init_test_model(epochs=10):
    model = MVSNet()
    start_epoch = 0

    return model, start_epoch

def load_from_ckpt(ckpt, epochs):

    model, _ = init_test_model(epochs)

    checkpoint = torch.load(ckpt)

    model.load_state_dict(checkpoint["model_state_dict"])
    loss  = checkpoint['loss']
    acc_1 = checkpoint['acc_1']
    acc_2 = checkpoint['acc_2']

    return model,  0, loss, acc_1, acc_2

if __name__ =="__main__":
    # really strange behavior, need to include the dataset class
    # here in order to load the saved dataset properly
    from data import DtuTrainDataset
    model, _ = init_test_model()
    loss, acc_1, acc_2, = test(epochs=1, model=None,
    checkpoint=join('.','checkpoints','train_1647473828_13_1166'), test_data_loader="evaluation_dataloader")


import sys

sys.stdin = open("1210_Ladder1.txt")
# 어느 사다리를 고르면 X표시에 도착하게 될까?

# X 표시에서 거꾸로 올라가는 것으로 전환해보자
# 가로선을 만났을 떄 어떻게 동작해야 할까?
    # 위로 올라가던 중 좌 or 우에 있는 가로선을 만나면, 그 가로선으로 이동한다.
    # 현재 위치에서 좌, 우를 탐색한 뒤
        # 있으면 해당 가로선으로 이동
        # 없으면 위 칸으로 이동

    # 좌 or 우로 이동하던 중 위로 갈 수 있는 길을 만나면, 위로 올라간다.

N = 100

for _ in range(1, 11):
    tc = int(input())
    ladder_map = []
    for _ in range(N):
        row = list(map(int, sys.stdin.readline().strip().split()))
        ladder_map.append(row)

    # X 위치를 찾는다.
    start_col = 0
    for idx in range(N):
        if ladder_map[N - 1][idx] == 2:
            start_col = idx
            break

    # X 위치를 시작지점으로 탐색을 시작한다.
    cr = N - 1
    cc = start_col
    c_direction = 0

    dr = [-1, 0, 1, 0]
    dc = [0, 1, 0, -1]

    while cr != 0:
        # 좌
        if cc > 1 and ladder_map[cr][cc - 1] == 1:
            while cc > 0 and ladder_map[cr][cc - 1] == 1:
                cc -= 1
            else:
                cr -= 1
                continue
        # 우
        if cc < 99 and ladder_map[cr][cc + 1] == 1:
            while cc < 99 and ladder_map[cr][cc + 1] == 1:
                cc += 1
            else:
                cr -= 1
                continue
        # 양쪽 다 없을 때
        cr -= 1
        cc = cc
    print(f"#{tc} {cc}")


from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.views import (
    LoginView, LogoutView
)
from django.contrib.sites.shortcuts import get_current_site
from django.core.signing import BadSignature, SignatureExpired, loads, dumps
from django.http import Http404, HttpResponseBadRequest
from django.shortcuts import redirect
from django.template.loader import render_to_string
from django.views import generic
from .forms import (
    LoginForm, UserRegistrationForm
)
from .models import Store
import random, string

User = get_user_model()

class Login(LoginView):
    """ログインページ"""
    form_class = LoginForm
    template_name = 'accounts/login.html'

class Logout(LogoutView):
    """ログアウトページ"""
    pass

class Registration(generic.CreateView):
    """ユーザー仮登録"""
    template_name = 'accounts/registration.html'
    form_class = UserRegistrationForm

    def form_valid(self, form):
        """仮登録と本登録用メールの発行."""
        # 仮登録と本登録の切り替えは、is_active属性を使う。
        # 退会処理も、is_activeをFalseにするだけ。
        user = form.save(commit=False)
        user.is_active = False
        user.save()

        # アクティベーションURLの送付
        current_site = get_current_site(self.request)
        domain = current_site.domain
        context = {
            'protocol': self.request.scheme,
            'domain': domain,
            'token': dumps(user.pk),
            'user': user,
        }

        subject = render_to_string('accounts/mail_template/create/subject.txt', context)
        message = render_to_string('accounts/mail_template/create/message.txt', context)
        user.email_user(subject, message)
        return redirect('accounts:temporary_registration')

class TemporaryRegistration(generic.TemplateView):
    """ユーザー仮登録"""
    template_name = 'accounts/temporary_registration.html'

class RegistrationComplete(generic.TemplateView):
    """メール内URLアクセス後のユーザー本登録"""
    template_name = 'accounts/complete_registration.html'
    # ここのアクセス時間についてはいつか検証しておく。本当に1hで無効になる？
    timeout_seconds = getattr(settings, 'ACTIVATION_TIMEOUT_SECONDS', 60*60*24)  # デフォルトでは1日以内

    def get(self, request, **kwargs):
        """tokenが正しければ本登録."""
        token = kwargs.get('token')
        try:
            user_pk = loads(token, max_age=self.timeout_seconds)

        # 期限切れ
        except SignatureExpired:
            return HttpResponseBadRequest()

        # tokenが間違っている
        except BadSignature:
            return HttpResponseBadRequest()

        # tokenは問題なし
        else:
            try:
                user = User.objects.get(pk=user_pk)
            except User.DoesNotExist:
                return HttpResponseBadRequest()
            else:
                if not user.is_active:
                    # 問題なければ本登録とする
                    user.is_active = True
                    user.save()
                    store = Store.objects.get(pk=user_pk)
                    store.store_url = MakeStrRandomCode(24) # とりあえず適当に24文字作成
                    store.save()
                    return super().get(request, **kwargs)

        return HttpResponseBadRequest()

'''
    store_url用にランダムの文字列を作成する関数
'''
def MakeStrRandomCode(length):
   randomlist = [random.choice(string.ascii_letters + string.digits) for i in range(length)]
   return ''.join(randomlist)


import pandas as pd
import plotly.express as px

dfs = pd.read_excel("../11/PT100-tx-tn-prec.xlsx", sheet_name=None)

season_color = {'Spring':'green','Summer':'goldenrod','Autumn':'darkred','Winter':'blue'}

seasons = ['Spring','Summer','Autumn','Winter']
yseasons = ['year'] + seasons

# temperaturas mínimas para cada ano e estação
tmins = dfs['tmin']
tmins = tmins[yseasons].copy()
tmins.dropna(inplace=True)
tmins['year'] = tmins['year'].astype('uint16')

# temperaturas máximas para cada ano e estação
tmaxs = dfs['tmax']
tmaxs = tmaxs[yseasons].copy()
tmaxs.dropna(inplace=True)
tmaxs['year'] = tmaxs['year'].astype('uint16')

# colapsar as estações para índices
tmins = tmins.melt(id_vars=["year"],var_name="season",value_name="temp")
tmins.set_index('year',inplace=True)
years = tmins.groupby('season').idxmin().rename(columns={'temp':'year'})
temps = tmins.groupby('season').min()
tmins = years.join(temps)
tmins['type'] = 'min'

# colapsar as estações para índices
tmaxs = tmaxs.melt(id_vars=["year"],var_name="season",value_name="temp")
tmaxs.set_index('year',inplace=True)
years = tmaxs.groupby('season').idxmax().rename(columns={'temp':'year'})
temps = tmaxs.groupby('season').max()
tmaxs = years.join(temps)
tmaxs['type'] = 'max'

df = pd.concat([tmins,tmaxs])
df.reset_index(inplace=True)

fig = px.scatter(df, x="year", y="temp",color='season',symbol='type')
fig.write_html("ipma.html")


import os
import docworks
from forward  import Forward
from inverted import Inverted

forward  = Forward()
inverted = Inverted()

path = os.getcwd() + "/documents"

documents = []

for file in os.listdir(path):
  if file.endswith(".txt"):
    file_path = f"{path}/{file}"
    tokens = docworks.document_to_words(file_path)
    doc_name = file[:-4]

    forward.append_entry(doc_name, tokens)
    inverted.append_entry(doc_name, tokens)

forward.print()
inverted.print()

print(forward.search("existence"))
print(inverted.search("existence"))

print(forward.search("neglected"))
print(inverted.search("neglected"))

##converting "documents" to strings to be fed into class methods

# 0. Build unit-tests for the tasks below
# 1. Build forward index for a set of documents
# 2. Build inverted index  for a set of documents
# 3. Try to search information based on the indexes


import re
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.fx import Graph, GraphModule

import onnx
import onnx_graphsurgeon as gs
import onnxruntime.tools.symbolic_shape_infer as onnxrt_symbolic_shape_inference

import _operator
from .pytorch_layers import *

class OnnxPytorchParser:
    def __init__(self, model, block_info=None):
        super(OnnxPytorchParser, self).__init__()
        self.model = model
        self.block_info = block_info
        self.onnx_model = onnx.load(model)

        self.onnx_model = onnxrt_symbolic_shape_inference.SymbolicShapeInference.infer_shapes(self.onnx_model, auto_merge=True)
        self.graph = gs.import_onnx(self.onnx_model)
        self.graph.fold_constants().cleanup().toposort()
        self.pytorch_graph = Graph()
        self.pytorch_graph_module = GraphModule(torch.nn.Module(), self.pytorch_graph)
        self.env = {}
        self._illegal_char_regex = re.compile("[^0-9a-zA-Z_]+")

    def convert(self):
        self.gen_pytorch_graph_module()
        return self.pytorch_graph_module

    def create_arg(self, a):
        if isinstance(a, torch.nn.Parameter):
            for n, p in self.pytorch_graph_module.named_parameters():
                if a is p:
                    return self.create_node("get_attr", n, (), {})
        elif isinstance(a, torch.Tensor):
            for n_, p_ in self.pytorch_graph_module.named_buffers():
                if a is p_:
                    return self.create_node("get_attr", n_, (), {})
        elif isinstance(a, torch.nn.Module):
            for n_, p_ in self.pytorch_graph_module.named_modules():
                if a is p_:
                    return self.create_node("get_attr", n_, (), {})
        # For NamedTuple instances that appear literally as args, we emit
        # a node to construct the NamedTuple and use that Node as the argument.
        if isinstance(a, tuple) and hasattr(a, "_fields"):
            args = tuple(self.create_arg(elem) for elem in a)
            return self.create_node("call_function", a.__class__, args, {})

        qualname = None
        if isinstance(a, (torch.Tensor)):
            if not qualname:
                i = 0
                while True:
                    qualname = f"_tensor_constant{i}"
                    if not hasattr(self.pytorch_graph_module, qualname):
                        break
                    i += 1
                setattr(self.pytorch_graph_module, qualname, a)

            return self.pytorch_graph.create_node("get_attr", qualname, (), {})

    def process_inputs(self, inputs):
        inputs = list(inputs)
        for idx in range(len(inputs)):
            input = self.create_arg(inputs[idx])
            if input:
                inputs[idx] = input

        inputs = tuple(inputs)

        return inputs

    def get_node_users(self, node):
        users = []
        for output in node.outputs:  # output is a Variable
            for user in output.outputs:  # user is a Node
                users.append(user)
        return users

    def get_node_feeds(self, node):
        feeds = []
        for input in node.inputs:  # input is a Variable
            for feed in input.inputs:  # user is a Node
                feeds.append(feed)
        return feeds

    def find_block_id(self, node_name, block_info):
        if block_info is None:
            return None

        for block_id, block_data in block_info.items():
            if node_name in block_data['nodes']:
                return block_id
        # Return None if the node is not found in any block
        return None

    def gen_pytorch_graph_module(self):
        for input in self.graph.inputs:
            node = self.pytorch_graph.placeholder(
                self._illegal_char_regex.sub("_", input.name)
            )
            self.env[input.name] = node

        for onnx_node in self.graph.nodes:
            node_name = onnx_node.name
            target_name = node_name
            block_id = self.find_block_id(node_name, self.block_info)
            if block_id is not None:
                target_name = f"{block_id}.{node_name}"
            node_feeds = self.get_node_feeds(onnx_node)
            if len(node_feeds) == 0:
                node_feeds = self.graph.inputs[0]
            elif len(node_feeds) == 1:
                node_feeds = node_feeds[0]

            if onnx_node.op == "Conv":
                module = Conv.from_onnx(onnx_node)
                self.pytorch_graph_module.add_submodule(target_name, module)
                node = self.pytorch_graph.create_node(
                    "call_module",
                    target_name,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "LayerNormalization":
                module = LayerNorm.from_onnx(onnx_node)
                self.pytorch_graph_module.add_submodule(target_name, module)
                node = self.pytorch_graph.create_node(
                    "call_module",
                    target_name,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Relu":
                module = nn.ReLU()
                self.pytorch_graph_module.add_submodule(target_name, module)
                node = self.pytorch_graph.create_node(
                    "call_module",
                    target_name,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Clip":
                module = nn.ReLU6()
                self.pytorch_graph_module.add_submodule(target_name, module)
                node = self.pytorch_graph.create_node(
                    "call_module",
                    target_name,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Add":
                inputs = Arithmetic.from_onnx(onnx_node, self.env)
                inputs = self.process_inputs(inputs)
                node = self.pytorch_graph.create_node(
                    "call_function",
                    torch.add,
                    inputs,
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Sub":
                inputs = Arithmetic.from_onnx(onnx_node, self.env)
                node = self.pytorch_graph.create_node(
                    "call_function",
                    torch.sub,
                    inputs,
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Div":
                inputs = Arithmetic.from_onnx(onnx_node, self.env)
                node = self.pytorch_graph.create_node(
                    "call_function",
                    torch.div,
                    inputs,
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Mul":
                inputs = Arithmetic.from_onnx(onnx_node, self.env)
                inputs = self.process_inputs(inputs)
                node = self.pytorch_graph.create_node(
                    "call_function",
                    torch.mul,
                    inputs,
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "MatMul":
                inputs = Arithmetic.from_onnx(onnx_node, self.env)
                inputs = self.process_inputs(inputs)
                node = self.pytorch_graph.create_node(
                    "call_function",
                    torch.matmul,
                    inputs,
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Gelu":
                node = self.pytorch_graph.create_node(
                    "call_function",
                    F.gelu,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "GlobalAveragePool":
                module = Pool.from_onnx(onnx_node)
                self.pytorch_graph_module.add_submodule(target_name, module)
                node = self.pytorch_graph.create_node(
                    "call_module",
                    target_name,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "MaxPool":
                module = Pool.from_onnx(onnx_node)
                self.pytorch_graph_module.add_submodule(target_name, module)
                node = self.pytorch_graph.create_node(
                    "call_module",
                    target_name,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "AveragePool":
                module = Pool.from_onnx(onnx_node)
                self.pytorch_graph_module.add_submodule(target_name, module)
                node = self.pytorch_graph.create_node(
                    "call_module",
                    target_name,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Flatten":
                node = self.pytorch_graph.create_node(
                    "call_function",
                    torch.flatten,
                    (self.env[node_feeds.name],),
                    {"start_dim": onnx_node.attrs["axis"]},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Concat":
                node = self.pytorch_graph.create_node(
                    "call_function",
                    torch.cat,
                    ([self.env[input_node.name] for input_node in onnx_node.inputs],),
                    {"dim": onnx_node.attrs["axis"]},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Reshape":
                node = self.pytorch_graph.create_node(
                    "call_method",
                    "reshape",
                    (
                        self.env[node_feeds.name],
                        onnx_node.inputs[1].values.tolist(),
                    ),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Transpose":
                node = self.pytorch_graph.create_node(
                    "call_function",
                    torch.permute,
                    (
                        self.env[node_feeds.name],
                        onnx_node.attrs["perm"],
                    ),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Split":
                node = self.pytorch_graph.create_node(
                    "call_function",
                    torch.chunk,
                    (
                        self.env[node_feeds.name],
                        len(onnx_node.inputs[1].values.tolist()),
                    ),
                    {"dim": onnx_node.attrs["axis"]},
                    node_name,
                )
                self.env[node_name] = node
                for i, output in enumerate(onnx_node.outputs):
                    node = self.pytorch_graph.create_node(
                        "call_function",
                        _operator.getitem,
                        (
                            self.env[node_name],
                            i,
                        ),
                        {},
                        output.name,
                    )
                    self.env[output.name] = node
            elif onnx_node.op == "Slice":
                inputs = Slice.from_onnx(onnx_node, self.env)
                node = self.pytorch_graph_module.graph.create_node(
                    "call_function",
                    _operator.getitem,
                    (
                        self.env[node_feeds.name],
                        inputs,
                    ),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Gemm":
                module = Linear.from_onnx(onnx_node)
                self.pytorch_graph_module.add_submodule(target_name, module)
                node = self.pytorch_graph.create_node(
                    "call_module",
                    target_name,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "BatchNormalization":
                module = BatchNorm.from_onnx(onnx_node)
                self.pytorch_graph_module.add_submodule(target_name, module)
                node = self.pytorch_graph.create_node(
                    "call_module",
                    target_name,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Softmax":
                node = self.pytorch_graph_module.graph.create_node(
                    "call_function",
                    F.softmax,
                    (self.env[node_feeds.name],),
                    {"dim": -1},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Sigmoid":
                node = self.pytorch_graph_module.graph.create_node(
                    "call_function",
                    F.sigmoid,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "HardSwish":
                node = self.pytorch_graph_module.graph.create_node(
                    "call_function",
                    F.hardswish,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "LeakyRelu":
                node = self.pytorch_graph_module.graph.create_node(
                    "call_function",
                    F.hardswish,
                    (self.env[node_feeds.name], onnx_node.attrs['alpha']),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Resize":
                node = self.pytorch_graph_module.graph.create_node(
                    "call_function",
                    F.interpolate,
                    (self.env[node_feeds.name],),
                    {'scale_factor': onnx_node.inputs[2].values.tolist()[2:], 'mode': onnx_node.attrs['mode']},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "ReduceMean":
                node = self.pytorch_graph_module.graph.create_node(
                    "call_method",
                    "mean",
                    (self.env[node_feeds.name],),
                    {
                        "dim": onnx_node.attrs["axes"],
                        "keepdim": bool(onnx_node.attrs.get("keepdims", 1)),
                    },
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Shape":
                node = self.pytorch_graph_module.graph.create_node(
                    "call_function",
                    getattr,
                    (self.env[node_feeds.name], "shape"),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "Gather":
                node = self.pytorch_graph_module.graph.create_node(
                    "call_function",
                    _operator.getitem,
                    (
                        self.env[node_feeds.name],
                        int(onnx_node.inputs[1].values),
                    ),
                    {},
                    node_name,
                )
                self.env[node_name] = node
            elif onnx_node.op == "QuantizeLinear":
                dequant_node = onnx_node.o(0)
                assert(dequant_node.op == "DequantizeLinear")

                module = Observer(float(onnx_node.inputs[1].values), float(onnx_node.inputs[2].values))
                self.pytorch_graph_module.add_submodule(target_name, module)
                node = self.pytorch_graph.create_node(
                    "call_module",
                    target_name,
                    (self.env[node_feeds.name],),
                    {},
                    node_name,
                )
                self.env[dequant_node.outputs[0].name] = node
            elif onnx_node.op == "DequantizeLinear":
                pass
            else:
                raise NotImplementedError(
                    "Operator {} is not supported.".format(onnx_node.op)
                )

        for output in self.graph.outputs:
            node = self.pytorch_graph.output(self.env[output.inputs[0].name])
            self.env[output.name] = node

        self.pytorch_graph_module.graph.lint()
        self.pytorch_graph_module.recompile()


import neovim
import re
import rpy2
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr

def get_test():
    exec(open('./rplugin/python3/magrittr-chain-run.py').read())
    import neovim
    import os
    nvim = neovim.attach('socket', path=os.environ['NVIM_LISTEN_ADDRESS'])
    return Main(nvim)

def do_test_stuff():
    m = get_test()

@neovim.plugin
class Main(object):
    def __init__(self, vim):
        self.vim = vim
        self.get_last_expression = robjects.r("""
        library(base)
        library(rlang)
        library(stringr)
        library(purrr)
        get_last_expression = function(xx, remove_assignment) {
          parse_success = TRUE
          e = tryCatch(rlang::parse_exprs(xx), error = function(e) {
            parse_success <<- FALSE
            list(0, FALSE, e, str_detect(e$message, 'unexpected'))
          })
          if(!parse_success) return(e)
          n_e = length(e)
          l_e = dplyr::last(e)
          if(remove_assignment && length(l_e) > 1 && as.character(l_e[[1]]) %in% c('=', '<-', '->')) {
            r_e = expr_text(l_e[[3]],width=500L)
          } else {
            r_e = expr_text(l_e, width=500L)
          }
          list(n_e, str_replace_all(str_replace_all(r_e, '\n', ''), '%>%', '%>%\n '), list(), FALSE)
        }
        """)

    def getLineEndMatch(self, line):
        # Return a match in group(2) if line followed by %>%, %T>%, or +
        return re.search('(.*?)((?:%T?>%|\\+|&|\\||,|\\()\\s*(#.*)?$|$)', line)

    def getPrevLineStartMatch(self, line):
        # Return a match in group(1) if line started by a continuation char
        return re.search('(^\\s*)(,|\\+|\\$|\\))', line)

    def isBlankLineOrComment(self, line):
        return (re.search('^\\s*$', line) or re.search('^\\s*#', line)) != None

    def removeAssignmentOperator(self, line):
        return re.search('(?:^.*%?<-%?|^)(.*?)(?:%?->%?.*$|$)', line).group(1)

    @neovim.function('RunFullMagrittrChain')
    def runFullMagrittrChain(self, args):
        self.runMagrittrChain('full')

    def runExpressionChain(self, remove_assignment):
        # Get the current line and add to the chain_to_run buffer
        ln = self.vim.current.window.cursor[0]-1
        current_line=self.vim.current.buffer[ln]

        # Just return if the line being run is whitespace
        if self.isBlankLineOrComment(current_line):
            return

        # Get the current line without the pipe or plus symbol
        match=self.getLineEndMatch(current_line)
        chain_to_run=match.group(1)
        init = self.get_last_expression(chain_to_run, remove_assignment)
        if init[3][0]:
            done = True
            raise Exception('Cannot run line with open brackets')
            #  self.vim.command('echoerr "Cannot run line with open brackets"', async_=True)
            #  self.vim.call('echoerr', "Cannot run line with open brackets")
            #  self.vim.err_write('Cannot run line with open brackets', async_=True)
            return
            #  exp = chain_to_run
        else:
            done = False

        # Iterate upwards until there's more than one expression
        while(ln > 0 and not done):
            ln -= 1
            print('>>> line number %s' % (ln))
            next_item = self.vim.current.buffer[ln]
            chain_plus = next_item + '\n' + chain_to_run
            print('chain_plus:\n%s' % chain_plus)
            e = self.get_last_expression(chain_plus, remove_assignment)
            if e[3][0]:
                print('line unexpected end of input, returning chain_to_run')
                chain_plus
                exp = self.get_last_expression(chain_to_run, remove_assignment)[1][0]
                done=True
            elif e[0][0] > 1:
                print('more than 1 expression, returning it')
                exp = e[1][0]
                done=True
            else:
                print('continuing, making chain_to_run == chain_plus')
                chain_to_run = chain_plus

        if not done:
            try_exp = self.get_last_expression(chain_to_run, remove_assignment)[1][0]
            if not try_exp:
                exp = chain_to_run
            else:
                exp = try_exp

        command = exp.replace("'", "''")
        self.vim.command("call g:MySendCmdToR('%s')"%command)
        return

    @neovim.function('RunNewMagrittrChain')
    def runNewMagrittrChain(self, args):
        self.runExpressionChain(True)

    @neovim.function('RunFullNewMagrittrChain')
    def runFullNewMagrittrChain(self, args):
        self.runExpressionChain(False)

    @neovim.function('RunMagrittrChain')
    def runMagrittrChain(self, args):
        # Get the current line and add to the chain_to_run buffer
        ln = self.vim.current.window.cursor[0]-1
        current_line=self.vim.current.buffer[ln]

        # Just return if the line being run is whitespace
        if self.isBlankLineOrComment(current_line):
            return

        # Get the current line without the pipe or plus symbol
        match=self.getLineEndMatch(current_line)
        chain_to_run=[match.group(1)]

        # Iterate upwards until reaching a line without pipe or plus
        ln -= 1
        prev_line=current_line
        found_start=False
        while(ln>=0 and not found_start):
            current_line = self.vim.current.buffer[ln]
            if self.isBlankLineOrComment(current_line):
                chain_to_run.append(current_line)
            else:
                match = self.getLineEndMatch(current_line)
                prev_match = self.getPrevLineStartMatch(prev_line)
                if(not match.group(2) and prev_match is None):
                    found_start=True
                else:
                    chain_to_run.append(match.group(0))
            prev_line = current_line
            ln -= 1

        # Remove any assignment operator on the last line(->)
        if(args!='full'):
            chain_to_run[0] = self.removeAssignmentOperator(chain_to_run[0])

        # After dealing with last line, reverse the array because we've been appending lines as we move up
        chain_to_run.reverse()

        # Remove all preceding blank lines and comments
        while(self.isBlankLineOrComment(chain_to_run[0])):
            chain_to_run.pop(0)

        # Remove the assignment operator at the start (if exists) and clean up
        if(args!='full'):
            chain_to_run[0] = self.removeAssignmentOperator(chain_to_run[0])

        chain_to_run = map(lambda x: x.strip(), chain_to_run)
        command = '\n'.join(chain_to_run) \
            .replace("'", "''")

        self.vim.command("call g:MySendCmdToR('%s')"%command)
        return


import numpy as np
import subprocess
import soundfile as sf
import os
import pysptk

def convert_to_wav(input_file):
    '''
    Convert input file to a .wav file using subprocess ffmpeg
    '''
    output_file = f'{input_file[:-4]}.wav'

    # Run the ffmpeg command as a subprocess
    subprocess.run(["ffmpeg", "-i", input_file, output_file])

    return output_file

def buffer(X, n, p=0, opt=None):
    '''
    Mimic MATLAB routine to generate buffer array

    MATLAB docs here: https://se.mathworks.com/help/signal/ref/buffer.html

    Parameters
    ----------
    x: ndarray
        Signal array
    n: int
        Number of data segments
    p: int
        Number of values to overlap
    opt: str
        Initial condition options. default sets the first `p` values to zero,
        while 'nodelay' begins filling the buffer immediately.

    Returns
    -------
    result : (n,n) ndarray
        Buffer array created from X
    '''

    if opt not in [None, 'nodelay']:
        raise ValueError('{} not implemented'.format(opt))

    i = 0
    first_iter = True
    while i < len(X):
        if first_iter:
            if opt == 'nodelay':
                # No zeros at array start
                result = X[:n]
                i = n
            else:
                # Start with `p` zeros
                result = np.hstack([np.zeros(p), X[:n - p]])
                i = n - p
            # Make 2D array and pivot
            result = np.expand_dims(result, axis=0).T
            first_iter = False
            continue

        # Create next column, add `p` results from last col if given
        col = X[i:i + (n - p)]
        if p != 0:
            col = np.hstack([result[:, -1][-p:], col])
        i += n - p

        # Append zeros if last row and not length `n`
        if len(col) < n:
            col = np.hstack([col, np.zeros(n - len(col))])

        # Combine result with next row
        result = np.hstack([result, np.expand_dims(col, axis=0).T])

    return result

def audio_feature(waveFile):
    """
    Extract the following audio features:
        volume:
        pause:
        pause pecentage:
        volume change:
    """
    frameSize = 11025
    overlap = 0
    y, fs = sf.read(waveFile)

    frameMat = buffer(y, frameSize, overlap)

    frameNum = frameMat.shape[1]
    volume1 = np.zeros(frameNum)

    ave = 0
    aveF = 0
    aveB = 0
    aveNum = 0
    pauseNum = 0
    aveFNum = 0
    aveBNum = 0
    bond = 35

    for i in range(frameNum):
        frame = frameMat[:, i]
        frame = frame - np.mean(frame)  # zero-justified
        volume1[i] = np.sum(np.abs(frame))  # method 1

    for i in range(1, frameNum - 1):
        if volume1[i] > bond:
            if volume1[i] < 300:
                ave += volume1[i]
                aveNum += 1
        else:
            pauseNum += 1

    if ave / (aveNum) < 60 and ave / (aveNum) > 45:
        bond = 25
        ave = 0
        aveNum = 0
        pauseNum = 0
        for i in range(1, frameNum - 1):
            if volume1[i] > bond:
                ave += volume1[i]
                aveNum += 1
            else:
                pauseNum += 1

    if ave / (aveNum) < 45:
        bond = 20
        ave = 0
        aveNum = 0
        pauseNum = 0
        for i in range(1, frameNum - 1):
            if volume1[i] > bond:
                ave += volume1[i]
                aveNum += 1
            else:
                pauseNum += 1

    pause = pauseNum * 0.25
    pause_percentage = pauseNum * 25 * fs / len(y)
    volume = ave / (aveNum)

    for i in range(3, round(frameNum / 2)):
        if volume1[i] > bond:
            aveF += volume1[i]
            aveFNum += 1

    for i in range(round(frameNum / 2 + 1), frameNum - 1):
        if volume1[i] > bond:
            aveB += volume1[i]
            aveBNum += 1

    volumn_change = (((aveB / aveBNum) - (aveF / aveFNum)) / (ave / aveNum)) * 100

    return volume, pause, pause_percentage, volumn_change

def pitch(x, fs, method='NCF', winLength=400, overlapLength=200):
    frame_length = winLength
    hop_length = frame_length - overlapLength

    # Pre-emphasis
    preemph_coeff = 0.97
    x = np.append(x[0], x[1:] - preemph_coeff * x[:-1])

    # Compute F0 using SPTK library
    if method == 'NCF':
        f0 = pysptk.swipe(x, fs=fs, hopsize=hop_length, min=60, max=400, threshold=0.25, otype="f0")
    elif method == 'ACF':
        f0 = pysptk.rapt(x, fs=fs, hopsize=hop_length, min=60, max=400)
    else:
        raise ValueError('Invalid method')

    return np.array(f0)

def pitch_feature(waveFile):
    x, fs = sf.read(waveFile)
    x = np.where(np.abs(x) > 0.2, 0, x)

    # readtime = len(x)
    winLength = round(fs / 10)
    overlapLength = round(0 * fs)

    f0 = pitch(x, fs, method='NCF', winLength=winLength, overlapLength=overlapLength)

    frameMat = buffer(x, winLength, overlapLength)
    frameNum = frameMat.shape[1]
    volume1 = np.zeros(frameNum)
    ave = 0
    aveNum = 0
    bond = 25

    for i in range(frameNum):
        frame = frameMat[:, i]
        frame = frame - np.mean(frame)  # zero-justified
        volume1[i] = np.sum(np.abs(frame))  # method 1

    for i in range(1, frameNum - 1):
        if volume1[i] > bond:
            ave += volume1[i]
            aveNum += 1

    if (ave / aveNum < 50) and (ave / aveNum > 35):
        bond = 17
        ave = 0
        aveNum = 0

        for i in range(1, frameNum - 1):
            if (volume1[i] > bond) and (volume1[i] < 300):
                ave += volume1[i]
                aveNum += 1

    if ave / aveNum < 35:
        bond = 11
        ave = 0
        aveNum = 0

        for i in range(1, frameNum - 1):
            if volume1[i] > bond:
                ave += volume1[i]
                aveNum += 1

    f0[volume1 < bond] = np.nan
    p = 0
    pitchnum = 0
    change = 0
    changenum = 0

    for i in range(1, len(f0)):
        if (f0[i] >= 60) and (f0[i] <= 270):
            p += f0[i]
            pitchnum += 1
            if (f0[i - 1] >= 60) and (f0[i - 1] <= 270):
                change += np.abs(f0[i] - f0[i - 1])
                changenum += 1
        else:
            f0[i] = np.nan

    if p / pitchnum > 180:
        p = 0
        pitchnum = 0
        change = 0
        changenum = 0
        for i in range(1, len(f0)):
            if 150 <= f0[i] <= 250:
                p += f0[i]
                pitchnum += 1
                if 150 <= f0[i - 1] <= 250:
                    change += abs(f0[i] - f0[i - 1])
                    changenum += 1
            else:
                f0[i] = float('nan')
    elif 140 < p / pitchnum < 180:
        p = 0
        pitchnum = 0
        change = 0
        changenum = 0
        for i in range(1, len(f0)):
            if 100 <= f0[i] <= 230:
                p += f0[i]
                pitchnum += 1
                if 100 <= f0[i - 1] <= 230:
                    change += abs(f0[i] - f0[i - 1])
                    changenum += 1
            else:
                f0[i] = float('nan')
    else:
        p = 0
        pitchnum = 0
        change = 0
        changenum = 0
        for i in range(1, len(f0)):
            if 70 <= f0[i] <= 180:
                p += f0[i]
                pitchnum += 1
                if 70 <= f0[i - 1] <= 180:
                    change += abs(f0[i] - f0[i - 1])
                    changenum += 1
            else:
                f0[i] = float('nan')

    average_vol = ave / aveNum
    pitch_change = change / changenum
    average_pitch = p / pitchnum

    return average_vol, pitch_change, average_pitch

def voice_features_extraction(voice_file):
    """
    Main function to extract voice features. Called in deployModel.

    Input:
        voice_file: voice file to be analyzed.

    Output:
        (a list) [pause percentage, volume change, pitch change, average pitch]
    """
    # wave_file = f"{voice_file[:-4]}.wav"

    # convert voice file to .wav if it doesn't already exist

    # if os.path.isfile(wave_file):
    #     pass
    # else:
    #     wave_file = convert_to_wav(voice_file)

    volume, pause, pause_percentage, volumn_change = audio_feature(voice_file)
    average_vol, pitch_change, average_pitch = pitch_feature(voice_file)

    return [pause_percentage, volumn_change, pitch_change, average_pitch]


import aiohttp
from bs4 import BeautifulSoup
import pandas as pd
import asyncio
import requests
import requests.exceptions
from urllib.parse import urlsplit
from urllib.parse import urlparse
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
import time
import pickle
import json

class WebScraper(object):

    def __init__(self):
        self.url = 'https://sa.ucla.edu/ro/public/soc'
        try:
            self.df = pd.read_csv('/Users/iggy1212 1/Desktop/Research/WageDetermination/outputs/la_scrape.csv')
        except:
            self.df = pd.DataFrame(columns=['Term','Department','Class Code','Title','Section #','Section','Enrollment Status','Waitlist Status','Units','Instructor'])

        self.start_scrape()

    def start_scrape(self):
        def getShadowRoot(host):
            shadowRoot = self.driver.execute_script("return arguments[0].shadowRoot", host)
            return shadowRoot

        self.driver = webdriver.Chrome('/usr/local/bin/chromedriver')
        self.driver.get(self.url)

        host1 = self.driver.find_element_by_tag_name('ucla-sa-soc-app')
        root1 = getShadowRoot(host1)

        term_select = root1.find_element_by_id('optSelectTerm')
        term_select = Select(term_select)
        term_options = term_select.options
        for x in range(0,len(term_options)):
            self.driver.get(self.url)
            host1 = self.driver.find_element_by_tag_name('ucla-sa-soc-app')
            root1 = getShadowRoot(host1)

            term_select = root1.find_element_by_id('optSelectTerm')
            term_select = Select(term_select)
            term_options = term_select.options

            term_select.select_by_index(x)
            time.sleep(2)

            host2 = root1.find_element_by_id('select_filter_subject')
            root2 = getShadowRoot(host2)

            root2.find_element_by_id('IweAutocompleteContainer').click()
            subjects = len(root2.find_element_by_id('dropdownitems').text.split('\n'))

            for y in range(0,subjects):

                host1 = self.driver.find_element_by_tag_name('ucla-sa-soc-app')
                root1 = getShadowRoot(host1)

                term_select = root1.find_element_by_id('optSelectTerm')
                term_select = Select(term_select)
                term_options = term_select.options

                term_select.select_by_index(x)

                time.sleep(2)

                host2 = root1.find_element_by_id('select_filter_subject')
                root2 = getShadowRoot(host2)
                time.sleep(2)
                root2.find_element_by_id('IweAutocompleteContainer').click()
                root2.find_element_by_id(f'option-{str(y)}').click()
                time.sleep(10)
                try:
                    root1.find_element_by_id('div_btn_go').click()
                except:
                    self.driver.get(self.url)
                    continue
                    time.sleep(10)

                WebDriverWait(self.driver, 30).until(EC.presence_of_element_located((By.XPATH, '/html/body/main')))

                try:
                    host1 = self.driver.find_element_by_tag_name('ucla-sa-soc-app')
                    root1 = getShadowRoot(host1)

                    pages = root1.find_element_by_id('divPagination').text.split('\n')
                    pages = [int(e) for e in pages if e.isdigit()][-1]
                except:
                    pages = 1

                try:
                    host1 = self.driver.find_element_by_tag_name('ucla-sa-soc-app')
                    root1 = getShadowRoot(host1)

                    no_results = root1.find_element_by_id('spanNoSearchResults').text
                except:
                    no_results = ''

                if no_results != '':
                    self.driver.get(self.url)
                    continue
                    time.sleep(10)

                counter=0
                while counter < pages:
                    host1 = self.driver.find_element_by_tag_name('ucla-sa-soc-app')
                    root1 = getShadowRoot(host1)

                    term = root1.find_element_by_id('spanSearchResultsHeader').text.replace('Your results for: ','').split(' ')[0:2]
                    term = ('').join(term)
                    dept = root1.find_element_by_id('spanSearchResultsHeader').text.replace('Your results for: ','').split(' ')[2:-1]
                    dept = ('').join(dept)

                    root1.find_element_by_id('expandAll').click()
                    time.sleep(10)

                    pageload = True
                    while pageload:
                        try:
                            results = root1.find_element_by_id('resultsTitle').get_attribute('innerHTML')
                            results = results.split('>')
                            results = [e.replace('\n            <div class="row-fluid class-title" id="','').replace('"','').strip() for e in results if 'row-fluid class-title' in e]

                            for i in results:
                                if 'No results available' not in root1.find_element_by_id(f'{i}-container').text:
                                    code = i
                                    title = root1.find_element_by_id(f'{i}-title').text

                                    time.sleep(3)
                                    i_d = root1.find_element_by_id(f'{code}-children').get_attribute('innerHTML').split('>')
                                    i_d = [e.replace('\n        <input id="','').replace('-checkbox" type="checkbox"','') for e in i_d if f'_{code}-checkbox" type="checkbox"' in e][0]
                                    i_d = i_d.split('_')[0]

                                    section = root1.find_element_by_id(f'{i_d}_{code}-section').text
                                    status = root1.find_element_by_id(f'{i_d}_{code}-status_data').text
                                    waitlist = root1.find_element_by_id(f'{i_d}_{code}-waitlist_data').text
                                    units = root1.find_element_by_id(f'{i_d}_{code}-units_data').text
                                    instructor = root1.find_element_by_id(f'{i_d}_{code}-instructor_data').text

                                    row = [term,dept,code,title,i_d,section,status,waitlist,units,instructor]
                                    row = pd.Series(row, index = self.df.columns)
                                    self.df = self.df.append(row, ignore_index=True)

                                    claim = True
                                    cap = 1
                                    while claim:
                                        try:
                                            s_i_d = str(int(i_d)+cap)

                                            section = root1.find_element_by_id(f'{s_i_d}_{code}-section').text
                                            status = root1.find_element_by_id(f'{s_i_d}_{code}-status_data').text
                                            waitlist = root1.find_element_by_id(f'{s_i_d}_{code}-waitlist_data').text
                                            units = root1.find_element_by_id(f'{s_i_d}_{code}-units_data').text
                                            instructor = root1.find_element_by_id(f'{s_i_d}_{code}-instructor_data').text

                                            row = [term,dept,code,title,i_d,section,status,waitlist,units,instructor]
                                            row = pd.Series(row, index = self.df.columns)
                                            self.df = self.df.append(row, ignore_index=True)
                                            cap +=1

                                        except:
                                            claim = False
                                else:
                                    pass

                            pageload=False
                            counter+=1
                            print(self.df)
                            if counter < pages:
                                root1.find_element_by_link_text(str(counter+1)).click()
                                time.sleep(5)
                            else:
                                break

                        except:
                            print('i')
                            pass

                self.driver.get(self.url)

            self.df.to_csv('/Users/iggy1212 1/Desktop/Research/WageDetermination/outputs/la_scrape.csv', index=False)

if __name__ == '__main__':
    scraper = WebScraper()


from flask import Flask, render_template, request
import numpy as np
from joblib import load
from sklearn.preprocessing import StandardScaler

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def house_price_pred():
    request_type_str = request.method
    if request_type_str == 'GET':
        return render_template('index.html')
    else:
        int_features = np.array([float(x) for x in request.form.values() if is_float(x)])
        if int_features.shape[0] == 15:
            int_features_final = int_features.reshape(1,-1)
            model = load('model.joblib')
            scalery = load(open('scaler_y.pkl', 'rb'))
            scalerX = load(open('scaler_X.pkl', 'rb'))
            results = make_pred(model, scalerX, scalery, int_features_final)
        else:
            results = "Non-float value(s) inserted! Please insert values as per insructions."
        return render_template('index.html', prediction_text=results)

def make_pred(model, scaler_X, scaler_y, new_array):
    scaled_X = scaler_X.transform(new_array)
    y_pred = model.predict(scaled_X)
    y_pred_inv_scaled = scaler_y.inverse_transform(y_pred.reshape(-1,1))
    return y_pred_inv_scaled

def is_float(s):
    try:
        float(s)
        return True
    except:
        return False

if __name__ == "__main__":
    app.run(debug=True)


"""
This script was created at 09-Dec-21
author: eachrist

"""
#  ============ #
#    Imports    #
# ============= #
import os
import pandas as pd
from s0_cases_dictionaries import dict_cases
from s__Helpers_Functions import check_paths
from s1_ExploreData_Functions import explore_sns_data, explore_ges_data, select_users
from s2_CreateDataframes_Functions import create_df_sns, create_df_ges
from s3_ExtractFeatures_Functions import extract_features_sns, extract_features_ges
from s4_GetResults_Functions import get_results

# =============== #
#    Functions    #
# =============== #
def main_thread(case: str, screen: str):

    print('---', screen, '---\n')
    screen_path = check_paths(case_path, screen)

    # Explore data
    print('-> ExploreData\n')
    dict_acc = explore_sns_data(case, screen, screen_path, 'accelerometer')
    dict_gyr = explore_sns_data(case, screen, screen_path, 'gyroscope')
    dict_ges = explore_ges_data(case, screen, screen_path)
    dict_acc_fnl, dict_gyr_fnl, dict_ges_fnl = select_users(case, screen_path, dict_acc, dict_gyr, dict_ges)
    print('  ---\n')

    # Create dataframes
    print('-> CreateDataframes\n')
    df_acc = create_df_sns(case, screen_path, dict_acc_fnl, 'accelerometer')
    df_gyr = create_df_sns(case, screen_path, dict_gyr_fnl, 'gyroscope')
    df_ges = create_df_ges(case, screen_path, dict_ges_fnl)
    print('  ---\n')

    # Extract Features
    print('-> ExtractingFeatures\n')
    ftr_acc = extract_features_sns(case, screen_path, df_acc, 'accelerometer')
    ftr_gyr = extract_features_sns(case, screen_path, df_gyr, 'gyroscope')
    ftr_ges = extract_features_ges(case, screen_path, df_ges)
    print('  ---\n')

    # Get Results
    print('-> GettingResults\n')
    results = get_results(case, screen, screen_path, ftr_acc, ftr_gyr, ftr_ges)
    print('  ---\n')

    return

# ========== #
#    main    #
# ========== #
if __name__ == "__main__":

    # Select case
    for case in ['case23.2']:

        print('=====')
        print(case)
        print('=====')
        case_path = check_paths(os.path.dirname(__file__), os.path.join('cases', case))

        # Select screen
        for screen in dict_cases[case]['screens']:
            main_thread(case, screen)
            # input('Press Enter to continue...\n')


import os
import yaml
from util import dump_configs, dump_scores
import logging

from model.detection import Detector

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", type=str, default="aiops18")
parser.add_argument("--csv_name", type=str, required=True)
parser.add_argument("--pre_trained_detector", type=str, default=None)
params = vars(parser.parse_args())

import pickle
def get_Xy(dataset, data_type, csv_name):
    with open(f"../data/{dataset}/{data_type}/{csv_name}_X.pkl",'rb') as rf:
        X = pickle.load(rf)
    with open(f"../data/{dataset}/{data_type}/{csv_name}_y.pkl",'rb') as rf:
        y = pickle.load(rf)
    return X, y

def main(dataset, csv_name, pre_trained_detector=None):
    train_X, _ = get_Xy(dataset, "train", csv_name)
    valid_X, valid_y = get_Xy(dataset, "valid", csv_name)
    test_X, test_y = get_Xy(dataset, "test", csv_name)

    with open(os.path.join("../data", dataset, "config.yaml"), 'r') as f:
        configs = yaml.safe_load(f.read())

    hash_id, _, result_dir = dump_configs(configs, dataset)
    logging.info("Start Experiment {}...".format(hash_id))

    detector = Detector(**configs)
    train_X = detector.scale(train_X, "train-test")
    test_X = detector.scale(test_X, "test")
    valid_X = detector.scale(valid_X, "test")

    if pre_trained_detector is None:
        top_k = configs["top_k"]
        indices = detector.feat_filter(valid_X, valid_y)
        cur_indices = indices[: top_k]
        detector.fit(train_X[:, cur_indices])
        eval_res = detector.evaluate(test_X[:, cur_indices], test_y)
    else:
        NotImplementedError

    dump_scores(result_dir, eval_res)

if __name__ == "__main__":
    if not os.path.exists("./result/detect"):
        os.makedirs("./result/detect", exist_ok=True)
    if not os.path.exists("./model_save/detect"):
        os.makedirs("./model_save/detect", exist_ok=True)
    main(**params)


from sklearn.feature_extraction.text import TfidfVectorizer
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
import numpy as np
import os
import pandas as pd

def text_feature_extraction(corpus):
    '''
    Corpus : A list of Questions in the dataset
    '''
    if os.path.exists("../data/text_features.npy"):
        ans=input("Feature already exists,do you want to re run it?(y/n)\n")
        if ans=="n":
            return
        else:
            pass
    vectorizer=TfidfVectorizer()
    text_features=np.array(vectorizer.fit_transform(corpus).todense())
    print(text_features.shape)
    np.save("../data/text_features.npy",text_features)

def image_feature_extraction(df):
    '''
    images: A numpy 4D array of shape (no. of examples,299,299,3)
    '''
    if os.path.exists("../data/image_features.npy"):
        ans=input("Feature already exists,do you want to re run it?(y/n)\n")
        if ans=="n":
            return
        else:
            pass
    file_list = os.listdir('../data/train_images')
    print(len(file_list))
    test_datagen = ImageDataGenerator(rescale=1./255)
    test_generator = test_datagen.flow_from_dataframe(
        dataframe=df,
        directory="../data/train_images/",
        x_col="image",
        y_col=None,
        batch_size=8,
        shuffle=False,
        seed=123,
        class_mode=None,
        target_size=(229,229))
    filenames = test_generator.filenames
    nb_samples = len(filenames)
    print(nb_samples)
    model=keras.applications.inception_v3.InceptionV3(include_top=True, weights='imagenet', pooling='avg')
    model.layers.pop()
    model = Model(model.input, model.layers[-1].output)
    image_features = model.predict_generator(test_generator,steps = np.ceil(nb_samples/8),use_multiprocessing=False, verbose=1)
    print(image_features.shape)
    np.save("../data/image_features.npy",image_features)

data=pd.read_csv("../data/train.csv")
image_feature_extraction(data)
text_feature_extraction(data['question'].tolist())


def tree9():
    line1=input().split(' ')
    M,D=int(line1[0]),int(line1[1])
    Q=[0]
    arr=[]
    for i in range(M):
        order=input().split(' ')
        if(order[0]=='A'):
            arr.append((Q[-1]+int(order[1]))%D)
        else:
            tmp=arr[len(arr)-int(order[1]):]
            tmp.sort()
            print(tmp[-1])
            Q.append(tmp[-1])

    return

tree9()


import time
def fill(d, e):

    toDo = [(d, e)]
    while toDo:
        x, y = toDo.pop()
        if y < 0 or y >= rowCount or x < 0 or x >= columnCounts[y]:
            continue
        if grid[y][x] != '%':
            continue

        grid[y][x] = '.'

        for a, b in (x + 1, y), (x, y + 1), (x, y - 1), (x - 1, y), \
                    (x + 1, y + 1), (x - 1, y + 1), (x - 1, y - 1), (x + 1, y - 1):
            toDo.append((a, b))
h = time.clock()
grid = [list(line.strip()) for line in open('patches.txt', 'r')]
rowCount = len(grid)

columnCounts = []
for row in grid:
    columnCounts.append(len(row))

count = 0

for y, row in enumerate(grid):
    for x, cell in enumerate(row):
        if cell == '%':
            fill(x, y)
            count += 1

print(count, "patches" if count != 1 else "patch")
print(time.clock() - h)


import numpy as numpy
from astropy import units
import re
import warnings
from . import Tools

from .ClassicProblem import ClassicProblem
from .Dynamic import Dynamic
from .Modifiable import Modifiable

__author__ = "Do Kester"
__year__ = 2021
__license__ = "GPL3"
__version__ = "2.7.0"
__url__ = "https://www.bayesicfitting.nl"
__status__ = "Perpetual Beta"

#  * This file is part of the BayesicFitting package.
#  *
#  * BayesicFitting is free software: you can redistribute it and/or modify
#  * it under the terms of the GNU Lesser General Public License as
#  * published by the Free Software Foundation, either version 3 of
#  * the License, or ( at your option ) any later version.
#  *
#  * BayesicFitting is distributed in the hope that it will be useful,
#  * but WITHOUT ANY WARRANTY; without even the implied warranty of
#  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  * GNU Lesser General Public License for more details.
#  *
#  * The GPL3 license can be found at <http://www.gnu.org/licenses/>.
#  *
#  *    2018 - 2021 Do Kester

class EvidenceProblem( ClassicProblem ):
    """
    An EvidenceProblem is a ClassicProblem containing a Dynamic and/or Modifiable
    model, where the (Gauss-approximated) evidence is used as likelihood

    Problems can be solved by NestedSampler, with appropriate Engines and
    ErrorDistributions.

    The result of the function for certain x and p is given by
    problem.result( x, p )
    The parameters, p, are to be optimized while the x provide additional
    information.

    Attributes from Problem
    -----------------------
    model, xdata, ydata, weights

    Author :         Do Kester

    """

    #  *************************************************************************
    def __init__( self, model=None, xdata=None, ydata=None, weights=None, copy=None ):
        """
        Constructor for classic problems.

        Parameters
        ----------
        model : Model
            the model to be solved
        xdata : array_like or None
            independent variable
        ydata : array_like or None
            dependent variable
        weights : array_like or None
            weights associated with ydata
        copy : Problem
            to be copied

        """
        if not ( model.isDynamic() or model.isModifiable() ) :
            raise ValueError( "Model needs to be dynamic or modifiable" )

        super( ).__init__( model=model, xdata=xdata, ydata=ydata, weights=weights,
                           copy=copy )

    def copy( self ):
        """
        Copy.

        The copy points to the same instance of model.
        """
        return EvidenceProblem( model=self.model, xdata=self.xdata, ydata=self.ydata,
                weights=self.weights, copy=self )

    def myEngines( self ) :
        """
        Return a default list of preferred engines
        """
        engs = []

        if self.model.isDynamic() :
            engs += ["birth", "death"]

        if self.model.isModifiable() :
            engs += ["struct"]

        return engs

    def myDistribution( self ) :
        """
        Return the default preferred ModelDistribution: "model"
        """
        return "model"

    #  *****TOSTRING***********************************************************
    def baseName( self ):
        """ Returns a string representation of the model.  """
        return "EvidenceProblem of " + self.model.__str__()