import pandas as pd
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import mysql.connector as mysql
import numpy as np
from sqlalchemy import create_engine, Table, MetaData
from sqlalchemy import text as sqlalctext #edit st 2023-03-07
import os

#########################################################################################
#function to create load difital demand as dataframe
def load_dd_df():
    '''
    loads digital demand dataframe for all dates
    where country is DE
    and gt_category 13
    returns:
        df_dd_raw (Dataframe)
    '''
    #SQL ALCHEMY
    #creating the engine
    #syntax: dialect+driver://username:password@host:port/database
    engine = create_engine('mysql+pymysql://sandbox_read_only:zhsqehk23Xs8tVmVn3sSkyq5TvZumR5q@mysqldatabase.cmi5f1vp8ktf.us-east-1.rds.amazonaws.com:3306/sandbox')

    #creating a connection object
    connection = engine.connect()

    #creating the metadata object
    # metadata = MetaData()

    #loading the digital_demand table #edit pik 2023-03-07
    # df_dd_raw_table = Table('digital_demand',
    #                        metadata)

    #this is the query to be performed #edit st 2023-03-07
    stmt = "SELECT * FROM digital_demand WHERE (gt_category = 13) AND (country = 'DE') and (date >= '2022-01-01');" #date updated to 2022 jan 1

    df_dd_raw = pd.read_sql(sqlalctext(stmt), connection) #edit st 2023-03-07
    df_dd_raw['date'] = pd.to_datetime(df_dd_raw['date'])

    connection.close()

    #storing df_digital_demand in variable df_raw to maintain code in viz generator
    df_raw_22_onwards = df_dd_raw

    # loading data from a blob container called csv that contains digital demand data from 2010 to 2022
    container_string=os.environ["CONNECTION_STRING"]
    storage_account_name = "storage4slack"
    container_name = "csv"
    blob_service_client = BlobServiceClient.from_connection_string (container_string)
    container_client = blob_service_client.get_container_client(container_name)
    filename = "split_df_raw.csv"
    blob_client = container_client.get_blob_client(filename)
    blob_name= filename

    blob_client = blob_service_client.get_blob_client(container_name, blob_name)
    blob_data = blob_client.download_blob().readall()

    # Open the csv file and read its contents
    # with open(filename, 'rb') as file:
    #     file_data = file.read()

    # Download the CSV file to a local temporary file
    with open(filename, "wb") as my_blob:
        download_stream = blob_client.download_blob()
        my_blob.write(download_stream.readall())

    # file_data is our new csv
    df_raw_10_21 = pd.read_csv(filename)

    # creating a list of dataframes to be merged
    frames = [df_raw_10_21, df_raw_22_onwards]

    # merging the dataframes
    df_raw = pd.concat(frames)

    return df_raw


import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
import os

COUNTY = "snohomish"
fileDir = os.path.dirname(__file__)
filePath = os.path.join(fileDir, "../data/snohomish.txt")
filePath = os.path.abspath(os.path.realpath(filePath))
f = open(filePath, 'w')
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(ChromeDriverManager().install(), options = chrome_options)

def scraping(url):
    print("Scraping from " + url)
    f.write("\n\n\n")
    f.write("Scraping from " + url + "\n\n\n")
    driver.get(url)
    time.sleep(1)
    result = driver.execute_script("return document.documentElement.outerHTML")
    return BeautifulSoup(result, 'html.parser')

soup = scraping("https://www.snohomishcountywa.gov/DocumentCenter/Index/6666")
data = soup.find_all("a", class_="t-link")
numbers = set()
for i in range(len(data)):
    if len(data[i].get_text()) > 2:
        continue
    numbers.add(data[i].get_text())
max = 0
for i in numbers:
    if int(i) > max:
        max = int(i)

soup = scraping("https://snohomishcountywa.gov/Archive.aspx?AMID=124")
data = soup.find(id = "modulecontent")
data = data.find_all("a", href = True)
links = set()
for i in range(len(data)):
    if "ADID" in data[i]['href']:
        links.add("https://snohomishcountywa.gov/" + data[i]['href'])

for i in links:
    soup = scraping(i)
    data1 = soup.find(class_ = "pageStyles")
    data = data1.find_all("p")
    for i in range(len(data)):
        f.write(data[i].get_text(separator = '\n'))
    data = data1.find_all("li")
    for i in range(len(data)):
        f.write(data[i].get_text(separator = '\n'))

PDFS = set()

soup = scraping("https://www.snohomishcountywa.gov/CivicAlerts.aspx?CID=59")
data = soup.find_all("a", class_ = "Hyperlink")
for i in range(len(data)):
    if "DocumentCenter/View" in data[i]['href'] and "snohomishcountywa.gov" not in data[i]['href']:
        PDFS.add("https://snohomishcountywa.gov" + data[i]['href'])

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import webbrowser
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized")
directory = os.getcwd()[:-7] + "data/" + COUNTY + "-PDF"
prefs = {"download.default_directory":
                            directory}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument('--headless')
chrome_options.add_argument("--enable-javascript")
driver = webdriver.Chrome(ChromeDriverManager().install(), options = chrome_options)
driver.get("https://www.snohomishcountywa.gov/DocumentCenter/Index/6666")
time.sleep(3)
download_button = driver.find_element_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "t-arrow-next", " " ))]')
#download_button.click()
content = driver.find_elements_by_class_name("pdf")
for link in content:
    PDFS.add(link.get_attribute("href"))
for i in range(max - 1):
    download_button.click()
    time.sleep(2)
    content = driver.find_elements_by_class_name("pdf")
    for link in content:
        print("LLOOK")
        print(link.get_attribute("href"))
        PDFS.add(link.get_attribute("href"))

import PyPDF2
import io

import requests
from PyPDF2 import PdfFileReader

path = "../data/" + COUNTY + "-PDF"
fileDir = os.path.dirname(__file__)
filePath = os.path.join(fileDir, path)
filePath = os.path.abspath(os.path.realpath(filePath))
os.makedirs(filePath, exist_ok=True)

def getPDFs(file_url, county):
    count = 1
    title = file_url.split('/').pop()
    if len(title) == 0:
        title = "NONAME" + str(count)
    r = requests.get(file_url, stream = True)
    path = "../data/" + COUNTY + "-PDF" + "/" + title + ".pdf"
    if os.path.isfile(path):
        path = "../data/" + COUNTY + "-PDF" + "/" + title + 1
        count = count + 1
    fileDir = os.path.dirname(__file__)
    filePath = os.path.join(fileDir, path)
    filePath = os.path.abspath(os.path.realpath(filePath))
    h = open(filePath, "wb")
    with h as pdf:
        for chunk in r.iter_content(chunk_size=1024):
         if chunk:
             pdf.write(chunk)
    return "data/" + title

for url in PDFS:
    print("Scraping from" + url)
    getPDFs(url, COUNTY)
    r = requests.get(url)
    fi = io.BytesIO(r.content)
    try:
        reader = PdfFileReader(fi)
    except Exception:
        continue
    number_of_pages = reader.getNumPages()
    for page_number in range(number_of_pages):
        page = reader.getPage(page_number)
        page_content = page.extractText()
        f.write(page_content)

f.close()
driver.quit()
print("finished")


import cv2
import numpy as np
import pickle
import os, math
from Gaussian import simple_gaussian, evaluate
from skimage.measure import label, regionprops
import matplotlib.pyplot as plt

def display(img):
    cv2.imshow('image',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows

image_path = os.listdir('C:/Users/TonyY/AppData/Local/Programs/Python/Python37-32/code/276HW1/validset')
for f in os.listdir('./validset/'):
    img_test = cv2.imread('./validset/'+f)
    img_test_ycrcb = cv2.cvtColor(img_test, cv2.COLOR_RGB2YCR_CB)
    image_array, class_array = evaluate(img_test_ycrcb, f)
    #plt.imshow(image_array)
    #plt.show()
    #cv2.waitKey(0)
    image_array = np.array(image_array, dtype='uint8')

    contours, hierarchy = cv2.findContours(image_array, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    if len(contours) == 0:
        continue

    Max = np.max([cv2.contourArea(c) for c in contours])
    allaxis = []
    center = []
    for c in contours:
        area = cv2.contourArea(c)
        if area < 300:
            continue
        x, y, w, h = cv2.boundingRect(c)
        if h/w < 1.0 or h/w > 3:
            continue
        #print(h/w)
        allaxis.append([x,y,w,h])
        center.append([int((2*x+w)/2),int((2*y+h)/2)])
    '''
    #caluate the distence between center
    left = 0
    right = left + 1
    maxD = math.sqrt((center[left][0]-center[right][0])**2 + (center[left][1]-center[right][1])**2)
    for i in range(1,len(center)-1):
        left = i
        right = left + 1
        tmp = math.sqrt((center[left][0]-center[right][0])**2 + (center[left][1]-center[right][1])**2)
        if tmp > maxD:
            maxD = tmp
    '''
    for i in range(len(center)-1):
        a = cv2.line(img_test, (center[i][0],center[i][1]), (center[i+1][0],center[i+1][1]),(0, 255, 0), 2)
    for i in allaxis:
        a = cv2.rectangle(img_test, (i[0],i[1]),(i[0]+i[2], i[1]+i[3]), (0, 255, 0), 2)
    print(f)
    print(center)
    #cv2.imshow('title',a)
    #cv2.waitKey(0)

    cv2.imwrite('C:/Users/TonyY/Desktop/All stuff/sensing & estimation in robotics/HW/testfigure/' +f, img_test)
    '''
    rect = cv2.minAreaRect(c)
    bbox = np.int0(cv2.boxPoints(rect))
    cv2.drawContours(img, [box], 0, (0, 255, 0), 2)
    '''

    '''
    #regionprops
    label_img = label(image_array)

    regions = regionprops(label_img)
    regions.pop(0)
    wantRegion = regions[1]
    for i in regions:
        if i.area > wantRegion.area:
            wantRegion = i

    #get bounding box
    img_test = cv2.cvtColor(img_test, cv2.COLOR_BGR2RGB)
    plt.imshow(img_test)

    minr, minc, maxr, maxc = wantRegion.bbox

    bx = (minc, maxc, maxc, minc, minc)
    by = (minr, minr, maxr, maxr, minr)

    plt.plot(bx, by, '-r')
    plt.title(f)
    plt.show()

    cv2.imwrite('C:/Users/TonyY/Desktop/All stuff/sensing & estimation in robotics/HW/testfigure/' +f, plt.figure())
    '''


from collections import deque

n=int(input())
gl=[[] for i in range(n)]
el=[]
for _ in range(n-1):
    a,b=map(int, input().split())
    a-=1
    b-=1
    gl[a].append(b)
    gl[b].append(a)
    el.append((a,b))

dists=[-1]*n
dists[0]=0
q=deque([0])
while q:
    poped=q.popleft()
    for neib in gl[poped]:
        if dists[neib]!=-1:continue
        dists[neib]=dists[poped]+1
        q.append(neib)

# print(dists)
allsum=0
vl=[0]*n
q=int(input())
for i in range(q):
    t,e,x=map(int, input().split())
    a,b=el[e-1]
    if t==1:
        if dists[a]<dists[b]:
            vl[b]-=x
            allsum+=x
        else:
            vl[a]+=x
    else:
        if dists[b]<dists[a]:
            vl[a]-=x
            allsum+=x
        else:
            vl[b]+=x

dists=[-1]*n
dists[0]=0
q=deque([0])
while q:
    poped=q.popleft()
    for neib in gl[poped]:
        if dists[neib]!=-1:continue
        dists[neib]=dists[poped]+1
        vl[neib]+=vl[poped]
        q.append(neib)

for a in vl:
    print(a+allsum)


#!/usr/bin/env python
import roslib
roslib.load_manifest('robomuse_drivers')
import rospy
import math
import tf
import geometry_msgs
import aruco_mapping.msg
from visualization_msgs.msg import Marker
from std_msgs.msg import Int32
import numpy as np

class converter():
    def __init__(self):
        self.listener = tf.TransformListener()
        self.br = tf.TransformBroadcaster()
        self.arucsubpose = rospy.Subscriber('/robomuse/camwrtmark0'+str(j),Pose,self.callback)
        self.arucsubid = rospy.Subscriber('/robomuse/arucoidmarker0'+str(j),aruco_mapping.msg.ArucoMarker,self.arucbk)
        self.idn = -1
        self.n = 0
        self.flag = 0
        self.happened = 0
        self.pose = geometry_msgs.msg.Pose()
        self.pos = [0,0,0]
        self.ori = [0,0,0,1]
        self.visible = [0,0,0,0,0,0]
        self.idlist = []

    def repeater(self):
        while not rospy.is_shutdown():
            j = 0
            if self.happened == 1:
                for i in self.idlist:
                    if i == self.idn:
                        self.index = j
                        break
                    else:
                        j = j+1
                try:
                    if self.visible[j]==1:
                        (self.pos,self.ori) = self.listener.lookupTransform('markerframe_'+str(j),'camera_'+str(j), rospy.Time(0))
                        self.br.sendTransform((self.pos[0], self.pos[1], self.pos[2]),(self.ori[0],self.ori[1],self.ori[2],self.ori[3]),rospy.Time.now(),"globalmarker_"+str(j),'camera_rgb_optical_frame')
                except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
                    continue
            rate.sleep()
        return 0

if __name__ == '__main__':
    rospy.init_node('aruco_rel_to_global')
    rate = rospy.Rate(30)
    a = converter()
    a.repeater()


# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymongo
#pip install pymongo
from douban.settings import mongo_host,mongo_port,mongo_db_name,mongo_db_collection

#保存爬取得到的items数据
class DoubanPipeline(object):
    def __init__(self):
        host = mongo_host
        port = mongo_port
        dbname = mongo_db_name
        sheet_name=mongo_db_collection
        #连接
        client = pymongo.MongoClient(host=host,port=port)
        #数据库
        mydb = client[dbname]
        self.post = mydb[sheet_name]

    # 插入数据库
    def process_item(self, item, spider):#item是spider传过来的yield
        data = dict(item)
        self.post.insert(data)
        return item

    def save_data_to_txt(self, data):
        with open('data.txt', 'w', encoding='utf-8') as f: #写入本地文件名csdn 类型汉字(W) 编码方式utf-8
            f.write(data)
            f.write('\n')   #换行


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

__author__ = "esemi"

import db_operations
import sys
import logging
import time
import config
import os

def filter_bot(x):

    bots = (
        'Googlebot',
        'Ezooms',
        'MJ12bot',
        'EmbeddedWB',
        'bingbot',
        'Crawler',
        'statdom.ru/Bot',
        'AhrefsBot',
        'crawler@nigma.ru',
        'Mail.RU/',
        'yandex.com/bots',
        'urllib',
        'FunWebProducts',
        'SeznamBot',
    )

    for bot in bots:
        if x.find(bot) != -1:
            return False

    if len(x) < 20 or len(x) > 250:
        return False

    return True

if __name__ == "__main__":
    config.initLog("agents_update", True)
    logging.info("Started %s" % time.strftime("%H:%M:%S %d.%m.%Y"))

    try:
        f = open(os.path.join(os.path.dirname(__file__), '..', 'docs', 'agents'), 'r')
    except IOError:
        logging.error('file not found')
        sys.exit()

    agents = set([i.strip() for i in f if filter_bot(i.strip())])
    logging.info("found %d unique agents" % len(agents))

    db = db_operations.DB()
    for agent in agents:
        try:
            db.addAgent(agent)
        except db_operations.mdb.IntegrityError:
            continue
        logging.info("agent added %s" % agent)
    db.close()


import os
import json
import cv2
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
from time import sleep
from datetime import date, datetime

root = "/home/matt/"
# AWS IoT certificate based connection
myMQTTClient = AWSIoTMQTTClient("dackrambo")
myMQTTClient.configureEndpoint("a1xrgfi7tw2r2a.iot.ap-southeast-2.amazonaws.com", 8883)
myMQTTClient.configureCredentials(root + ".aws/rootCA.crt",
    root + ".aws/2d4288b294-private.pem.key",
    root + ".aws/2d4288b294-certificate.pem.crt")
myMQTTClient.configureOfflinePublishQueueing(-1)  # Infinite offline Publish queueing
myMQTTClient.configureDrainingFrequency(2)  # Draining: 2 Hz
myMQTTClient.configureConnectDisconnectTimeout(10)  # 10 sec
myMQTTClient.configureMQTTOperationTimeout(5)  # 5 sec

#connect and publish
myMQTTClient.connect()
myMQTTClient.publish("skullcave/info", "connected", 0)

# Set up webcam
webcam = cv2.VideoCapture(0)

# Define a function to handle the callback from the subsciption
def thingdoer(client, userdata, message):
    # Decode message from bytes > str > Json, then get 'message'
    msg = json.loads(message.payload.decode("utf-8"))
    print (msg["message"])
    #publish_temp()
    publish_temp(custom = True, payload = stream_video())
    return True

def publish_temp(custom = False ,payload = 0):
    now = datetime.utcnow()
    now_str = now.strftime('%Y-%m-%dT%H:%M:%SZ') #e.g. 2016-04-18T06:12:25.877Z
    result = (10,20)
    if custom == False:
        payload = '{ "timestamp": "' + now_str + '","temperature": ' + str(result[0]) + ',"humidity": '+ str(result[1]) + ' }'
    myMQTTClient.publish("thing01/img", payload, 0)

def stream_video():
    grabbed, frame = webcam.read()
    if grabbed:
        print(frame.shape)
        frame = cv2.resize(frame, (48,64))
        cv2.imshow("Preview", frame)
        cv2.waitKey(200)
        cv2.destroyAllWindows()
        img = cv2.imencode('.jpg', frame)[1].tostring()
        return str(img)
        # & 0xFF
        # # If the `q` key is pressed, break from the loop
        # if key == ord('q'):
        #     cv2.destroyAllWindows()

myMQTTClient.subscribe("test01", 0, thingdoer)

while True:
    sleep(5)

#myMQTTClient.onMessage = thingdoer

#loop and publish sensor reading
# while 1:
#     now = datetime.utcnow()
#     now_str = now.strftime('%Y-%m-%dT%H:%M:%SZ') #e.g. 2016-04-18T06:12:25.877Z
#     result = (1,2)
#     if True:
#         payload = '{ "timestamp": "' + now_str + '","temperature": ' + str(result[0]) + ',"humidity": '+ str(result[1]) + ' }'
#         print (payload)
#         myMQTTClient.publish("thing01/data", payload, 0)
#         sleep(4)
#     else:
#         print (".")
#         sleep(1)


#!/usr/bin/env python
from typing import Tuple, Dict, Union, NoReturn
import numpy as np

from PlaxisRectangleGrid import utils
from PlaxisRectangleGrid.calc_func import distance_euc

class Point(object):
    def __init__(self, pt):
        self._coords = pt

    @property
    def x(self):
        return self._coords[0]

    @x.setter
    def x(self, x):
        self._coords[0] = x

    @property
    def z(self):
        return self._coords[1]

    @z.setter
    def z(self, z):
        self._coords[0] = z

    @property
    def u_y(self) -> float:
        return self._coords[2]

    @u_y.setter
    def u_y(self, u_y: Union[int, float]) -> NoReturn:
        self._coords[2] = u_y

    @property
    def coords(self):
        return self._coords

    @coords.setter
    def coords(self, new_coords):
        self._coords = np.ndarray(new_coords, dtype='float')

    @property
    def xz(self):
        return self._coords[:2]

    def nearby_points(self,
                      points: np.ndarray,
                      eps_x=1,
                      eps_z=1
                      ) -> Tuple[np.ndarray, list]:
        """
        Функция поиска ближайщих точек массива для данной точки. Ищет в
        заданной окрестности (eps_x, eps_z). Поиск происходит делением
        точек на 4 части (верх-право, верх-лево, низ-право, низ-лево)
        относительно самой точки(центр) и последующим нахождением минимального
        расстояния(евклидова метрика) из расстояний каждой точки до центра в
        каждой четверти.

        :param points: массив точек [[x1, z1, u_y1], [x2, z2, u_y2] ...]
        :param eps_x: окрестность поиска по горизонтальной оси
        (имеет смысл задавать как шаг сетки)
        :param eps_z: окрестность поиска по вертикальной оси
        (имеет смысл задавать как шаг сетки)
        :return: возвращает массив ближайщих точек(максимум 4) и
        массив расстояний от этих точек до данной точки
        """

        right_limit = self.x + eps_x  # правая граница поиска
        left_limit = self.x - eps_x  # левая граница поиска
        upper_limit = self.z + eps_z  # верхняя граница поиска
        lower_limit = self.z - eps_z  # нижняя граница поиска

        # Ищем точки в заданной окрестности (eps_x, eps_z)
        eps_area = points[np.where(
            (points[:, 0] <= right_limit) &
            (points[:, 0] >= left_limit) &
            (points[:, 1] <= upper_limit) &
            (points[:, 1] >= lower_limit))]

        # создаем маску для точек лежащие левее(и на вертикальной линии
        # проходящей через данную точку) и правее от данной точки
        mask_vertical = eps_area[:, 0] <= self.x

        # в случае если все точки окажутся левее пробуем более строгое условие,
        # т.к. некоторые точки могут оказаться на вертикальной линии проходящей
        # через данную точку
        if np.sum(mask_vertical == True) == len(eps_area):
            mask_vertical = eps_area[:, 0] < self.x

        # по маске создаем массивы точек лежащих слева и справа
        right = eps_area[mask_vertical]
        left = eps_area[~mask_vertical]

        # для левых и правых точек создаем маски для точек лежащие выше(и на
        # горизонтальной линии проходящей через данную точку) и ниже от
        # данной точки
        right_mask = right[:, 1] >= self.z
        left_mask = left[:, 1] >= self.z

        # в случае если все точки окажутся выше пробуем более строгое условие,
        # т.к. некоторые точки могут оказаться на горизонтальной
        # линии проходящей через данную точку
        if np.sum(right_mask == True) == len(right):
            right_mask = right[:, 1] > self.z

        if np.sum(left_mask == True) == len(left):
            left_mask = left[:, 1] > self.z

        # по маскам создаем массивы точек лежащих в четвертях
        nearby_points = [right[right_mask],
                         right[~right_mask],
                         left[left_mask],
                         left[~left_mask]]

        nearby = []
        nearby_dist = []

        # для каждой четверти выбираем точку с наименьшим расстоянием
        for pts in nearby_points:
            if len(pts):
                dist = distance_euc(self.xz, pts[:, :-1])
                min_idx = dist.argmin()
                nearby.append(pts[min_idx])
                nearby_dist.append(dist[min_idx])
        return np.array(nearby), nearby_dist

    def point4vectors2d(self, points: np.ndarray) -> np.ndarray:
        """
        Определяет положение точки относительно векторов образованных из
        массива точек на плоскости. с помощью векторного произведения векторов.
        0 - принадлежит вектору,
        1 - лежит правее,
        -1 - лежит левее.

        :param points: массив точек
        :return: результат векторных произведений.
        """

        start_point = points[0, :-1]  # точка начала векторов
        end_points = points[1:, :-1]  # точки конца векторов

        vec_1 = self.xz - start_point  # вектор до данной точки
        vec_2 = end_points - start_point  # векторы до остальных точек

        # массив знаков векторных произведений
        return np.sign(np.cross(vec_2, vec_1))

    def __repr__(self):
        x, z, u_z = self._coords
        return f"x: {x}, z: {z} u_z: {u_z}"

def write_log(method):
    """
    Декоратор для записи лога операции над точками
    :param method: метод возвращающий команду и массив точек
    :return: возвращающая команду и массив точек
    """
    def wrapper(self, *args, **kwargs):
        command, points = method(self, *args, **kwargs)
        self.logs.append([command, points])
        return command, points
    return wrapper

class GridPoint(Point):
    """
    Класс узла сетки.
    """
    def __init__(self, pt):
        super().__init__(pt)
        self.logs = []

    @write_log
    def point4point2d(self,
                      points: np.ndarray,
                      dist: np.ndarray,
                      eps_match=0
                      ) -> Tuple[str, np.ndarray]:
        """
        Определяет взаимное положение точки относительно точки на плоскости.
        Возвращает кортеж из: командa ('point' - совпадают,
        'no_point' - не совпадают), ближайшая точка [x1, y1, z1].

        :param points: массив точек ([[x1, y1, z1], [x2, y2, z2], ...])
        :param dist: массив расстояний ([dist1, dist2, ....])
        :param eps_match: окрестность в которой точки можно считать совпадающим
        :return: (команда: 'point'/'no_point', ближайщая точка: [[x1, y1, z1]])
        """
        idx = np.argmin(dist)
        command = 'point' if dist[idx] <= eps_match else 'no_point'
        return command, points[idx].reshape(1, 3)

    @write_log
    def point4lines2d(self, points: np.ndarray) -> Tuple[str, np.ndarray]:
        """
        Определяет взаимное положение точки относительно прямой, заданной двумя
        точками на плоскости.
        Возвращает кортеж из: командa ('line' - принадлежит,
        'outside' - не принадлежит), points.
        :param points: массив из двух точек ([[x1, y1, z1], [x2, y2, z2]])
        :return: (команда: 'line'/'outside', [[x1, y1, z1], [x2, y2, z2]])
        """
        sign = self.point4vectors2d(points)

        if sign == 0:
            return 'line', points
        else:
            return 'outside', points

    @write_log
    def point4triangle2d(self, points: np.ndarray) -> Tuple[str, np.ndarray]:
        """
        Определяет положение точки относительно треугольника образованного
        тремя точками. Из 1ой точки проводим вектора V1,V2 до остальных двух и
        V3 до данной точки.
        Берем знаки векторных произведений (V1, V3), (V2, V3). Если знаки
        различны - данная точка лежит между этими векторами(внутри) и повторяем
        данные операции для следующей точки. Если знаки также различны - точка
        точка лежит внутри треугольника.
        Если знаки одинаковы - вне данных векторов и следовательно вне
        треугольника.
        Если произведение равно 0, то точка принадлежит данному вектору,
        следовательно можно применить методы и функции для точки и линии.

        Возвращает кортеж из: командa ('line' - принадлежит линии,
        'inside' - принадлежит области, 'outside' - не принадлежит области),
        points.

        :param points: массив из двух точек ([[x1, y1, z1], [x2, y2, z2], ,
        [x3, y3, z3]])
        :return: ('line', [[x, y, z], [x*, y*, z*]]) \\ ('outside'/'inside',
        [[x1, y1, z1], [x2, y2, z2], [x3, y3, z3]])
        """
        # создаем 2 набора точек.
        # Для второго набора делаем смещение элементов вправо на 1 шаг.
        sets_points = np.array([points, np.roll(points, 1, axis=0)])
        for set_ in sets_points:
            sign = self.point4vectors2d(set_)  # знаки векторного произведения

            if np.any(sign == 0):
                # возвращаем пару точек при которых векторное произведение
                # равно 0
                end_point = set_[1:][sign == 0].reshape(-1)
                return 'line', np.array([set_[0], end_point])

            elif np.all(sign == sign[0]):
                return 'outside', np.array(points)

        return 'inside', np.array(points)

class RectangleGrid(object):
    """
    Класс создает массив точек(узлов) прямоугольной сетки.
    """

    def __init__(self,
                 length: Union[int, float],
                 depth: Union[int, float],
                 step_z: Dict[int, Union[int, float]]):
        self.length = length
        self.depth = depth
        self._step_x = self.set_step_x()
        self._step_z = self.set_step_z(step_z)
        self._grid_x = None
        self._grid_z = None
        self.journal_pts = self.create_grid()
        self.main_info = {'Points': len(self.journal_pts),
                          'Success': 0,
                          'Errors': 0
                          }

    def set_step_z(self, step: Dict[int, Union[int, float]]) -> float:
        """
        Устанавливает шаг сетки по Z.
        """
        return float(step[self.depth])

    def set_step_x(self) -> float:
        """
        Устанавливает шаг сетки по X.
        """
        return 1. if self.length < 60 else 5.

    @property
    def grid_x(self) -> np.ndarray:
        return self._grid_x

    @grid_x.setter
    def grid_x(self, array: np.ndarray) -> NoReturn:
        self._grid_x = array

    @property
    def grid_z(self) -> np.ndarray:
        return self._grid_z

    @grid_z.setter
    def grid_z(self, array: np.ndarray) -> NoReturn:
        self._grid_z = array

    @property
    def step_x(self) -> float:
        return self._step_x

    @property
    def step_z(self) -> float:
        return self._step_z

    def create_grid(self) -> Dict[Tuple[int, int], GridPoint]:
        """
        Создает узлы сетки как класс GridPoint для каждого узла,
        на длине и глубине равной self.length, self.depth и шагом
        self._step_x, self._step_z. Узел имеет координаты [x, z, 0.].
        Связывает эти узлы с координатами ячейки на листе в excel.
        :return: Возвращает словарь, где ключ - координатами ячейки на
        листе в excel; значение - объект GridPoint([x, z, 0.])
        """

        length = self.length
        depth = self.depth
        step_x = self._step_x
        step_z = self._step_z

        self._grid_x = np.arange(-length / 2, length / 2 + 1, step_x)
        grid_z = np.arange(0, depth + step_z, step_z)
        grid_z[-1] = self.depth
        self._grid_z = grid_z

        journal_pts = {(row, col): GridPoint([x, z, 0.])
                       for col, x in enumerate(self._grid_x, 2)
                       for row, z in enumerate(self._grid_z, 2)}
        return journal_pts

class SheetInfo:
    """
    Класс для создания журнала операций на листе excel.
    Производит оформление листа.
    Производит запись операции по узлу GridPoint(Point).
    Вид операции для записи имеет вид:
    GridPoint[x, z, u_y] [('команда', массив_точек), ........ ]
    Использует стили оформления таблицы из класса SheetInfoStyles.
    """

    TITLE_HEADER = {'grid_X': 1,
                    'grid_Z': 2,
                    'grid_u_Y': 3,
                    'command': 4,
                    'X': 5,
                    'Z': 6,
                    'u_Y': 7
                    }

    def __init__(self, sheet, main_info: dict) -> None:
        """
        Инициализация атрибутов экземпляра класса. Создает строки с основной
        информацией содержащейся в переданном словаре main_info.
        Создает атрибут с экземпляром класса SheetInfoStyles являющийся набором
        шаблонов стилей оформления ячеек.

        :param sheet: лист в excel файле
        :param main_info: {'Points': 12345,'Success': 12340, 'Errors': 5}
        """

        self.sheet = sheet
        self.row = 1  # используем как указатель для строки
        self.col = 1
        self.styles = utils.SheetInfoStyles()  # стили оформления
        self.__main_info(main_info)
        self.__header()

    def __main_info(self, main_info: dict) -> NoReturn:
        """
        Создает строки с основной информацией по узлам сетки.
        :param main_info: {'Points': 12345,'Success': 12340, 'Errors': 5}
        :return: NoReturn
        """
        field_col = self.col  # номер столбца полей
        value_col = self.col + 1  # номер столбца для значений полей

        for row, (field, value) in enumerate(main_info.items(), self.row):
            # устанавливаем высоту строк
            self.sheet.row_dimensions[row].height = 20

            # Записываем название поля и оформляем ячейку
            cell = self.sheet.cell(row=row, column=field_col, value=field)
            cell.style = self.styles.main_cell
            cell.number_format = '@'

            # Записываем значение поля и оформляем ячейку
            cell = self.sheet.cell(row=row, column=value_col, value=value)
            cell.style = self.styles.main_cell
            cell.number_format = '0'

            self.row += 1  # перемещаем на следующую строку
        self.row += 2  # пустые строки до таблицы

    def __header(self) -> NoReturn:
        """
        Оформление шапки таблицы операций. Записываем названия столбцов из
        атрибута класса TITLE_HEADER.
        :return: NoReturn
        """
        # устанавливаем высоту строки шапки
        self.sheet.row_dimensions[self.row].height = 25

        # заполняем шапку таблицы
        for field, col in self.TITLE_HEADER.items():
            cell = self.sheet.cell(row=self.row, column=col, value=field)
            cell.style = self.styles.header
            cell.number_format = '@'

            # устанавливаем ширину столбца
            col_letter = cell.column_letter  # получаем букву столбца
            self.sheet.column_dimensions[col_letter].width = 13
        self.row += 1  # перемещаем указатель на новую строку

    def write_journal(self, point: GridPoint) -> NoReturn:
        """
        Запись журнала операций для узла GridPoint.
        Вид операции для записи имеет вид:
        GridPoint[x, z, u_y] [('команда', [массив_точек]), ........ ]
        Использует стили оформления таблицы из класса SheetInfoStyles.
        :param point: экземпляр класса GridPoint
        :return: NoReturn
        """

        # устанавливаем высоту первой стоки строки
        self.sheet.row_dimensions[self.row].height = 20
        column = self.col  # локальный счетчик для столбцов

        """
        используем 2 стиля оформления для визуальной индикации успеха/не_успеха
        найденного перемещения в узле:
              self.styles.success - если значение перемещения не равно None;
              self.styles.errors - если значение перемещения равно None;
        """
        style = self.styles.errors if point.u_y is None else self.styles.success

        # записываем координаты узла
        for col, value in enumerate(point.coords, column):
            cell = self.sheet.cell(row=self.row, column=col, value=value)
            cell.style = style
            cell.font = self.styles.grid_pt_font

            # это условие требуется тк. для координаты u_y требуется повышенное
            # отображение десятичных знаков
            if col == 3:
                cell.number_format = '0.000000'
            else:
                cell.number_format = '0.00'
            column += 1

        # записываем произведенные операции над ближайшими точками узла
        for logs in point.logs:
            # лог имеет вид ('команда', [массив_точек])
            command, points = logs

            # записываем команду
            cell = self.sheet.cell(row=self.row, column=column, value=command)
            cell.style = style

            # в цикле записываем все точки над которыми производилась операция
            for point in points:
                # покоординатно записываем все координаты точки в свой столбец
                for col, value in enumerate(point, column + 1):

                    cell = self.sheet.cell(row=self.row,
                                           column=col,
                                           value=value)
                    cell.style = style
                    cell.number_format = '0.000000'
                self.row += 1  # перемещаем указатель на новую строку
            # перемещаем указатель на новую строку
            # (для визуального разделения операций и узлов)
            self.row += 1


import hashlib
import os
import threading

from dvc.utils.compat import HTTPServer, SimpleHTTPRequestHandler

class ETagHandler(SimpleHTTPRequestHandler):
    def end_headers(self):
        file = self.translate_path(self.path)

        if not os.path.isdir(file) and os.path.exists(file):
            with open(file, "r") as fd:
                etag = hashlib.md5(fd.read().encode("utf8")).hexdigest()
                self.send_header("ETag", etag)

        SimpleHTTPRequestHandler.end_headers(self)

class ContentMD5Handler(SimpleHTTPRequestHandler):
    def end_headers(self):
        file = self.translate_path(self.path)

        if not os.path.isdir(file) and os.path.exists(file):
            with open(file, "r") as fd:
                md5 = hashlib.md5(fd.read().encode("utf8")).hexdigest()
                self.send_header("Content-MD5", md5)

        SimpleHTTPRequestHandler.end_headers(self)

class StaticFileServer:
    _lock = threading.Lock()

    def __init__(self, handler="etag"):
        self._lock.acquire()
        handler_class = ETagHandler if handler == "etag" else ContentMD5Handler
        self._httpd = HTTPServer(("localhost", 0), handler_class)
        self._thread = None

    def __enter__(self):
        self._thread = threading.Thread(target=self._httpd.serve_forever)
        self._thread.daemon = True
        self._thread.start()
        return self._httpd

    def __exit__(self, *args):
        self._httpd.socket.close()
        self._httpd.shutdown()
        self._httpd.server_close()
        self._lock.release()


"""Vemax URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from vemax.views import *

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    url(r'^init$', init_view),
    url(r'^login$', login_view),
    url(r'^logout$', logout_view),
    url(r'^register$', register_view),
    url(r'^homepage$', homepage_view),

    url(r'^information$', information_view),
    url(r'^information_data$', information_data),
    url(r'^relationship$', relationship_view),
    url(r'^relationship_data$', relationship_data),

    url(r'^classify$', classify_view),
    url(r'^classify_data$', classify_data),
    url(r'^classify_detail$', classify_detail_view),

    url(r'^friend_analyse$', friend_analyse),

    url(r'^search$', search_view)
    ]


from jinja2 import StrictUndefined
import requests

from flask import Flask, render_template, redirect, request, flash, session
from flask import jsonify
from flask_debugtoolbar import DebugToolbarExtension

# import pdb; pdb.set_trace()

app = Flask(__name__)

app.secret_key = ["SERVER_APP_SECRET_KEY"]
app.jinja_env.undefined = StrictUndefined

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

@app.route('/job_list')
def job_list():
    """Job List Page"""

    job_1 = {
             "name": "Job 1",
             "car_address": "683 Sutter St, San Francisco, CA 94109",
             "dealership_address": "999 Van Ness Ave, San Francisco, CA 94109",
             "pick_up_time":"never"
            }
    job_2 = {
             "name": "Job 2",
             "car_address": "15215 N. Kierland Blvd, Scottsdale, AZ 85254",
             "dealership_address": "7014 E. Camelback Road, Suite #1210, Scottsdale, AZ 85251",
             "pick_up_time":"never"
            }
    job_3 = {
             "name": "Job 3",
             "car_address": "45500 Fremont Blvd Fremont, CA 94538",
             "dealership_address": "6701 Amador Plaza Road Dublin, CA 94568",
             "pick_up_time": "never"
            }

    car_info = requests.get("https://192.168.2.45:8000/getinfo", verify=False)
    print(car_info)

    return render_template('job_list.html',
                           job_1=job_1,
                           job_2=job_2,
                           job_3=job_3)

@app.route('/job_details')
def job_details():
    """Job Details Page"""

    return render_template('job_details.html')

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

if __name__ == "__main__":
    from server import app
    app.debug = True
    DebugToolbarExtension(app)

    app.run(host="0.0.0.0")


import sys
import os
from pathlib import Path
from aux_func import *
import pandas as pd
import math
import evaluation as eva

path = os.path.dirname(__file__) + '/'

def evaluation_step(sum):

    #Evaluation phase
    #BLEU precision score and ROGUE score

    text = '\n'.join(document)
    gold_list = eva.get_keywords(text)
    text_sum = '\n'.join(sum)
    summ_list = eva.get_keywords(text_sum)
    blue = eva.precision(summ_list,gold_list)
    rogue = eva.recall(summ_list,gold_list)

    print("\nBLEU score of [{0}] = {1:.2f}".format(file.name,blue*100))
    print("ROUGE score of [{0}] = {1:.2f}\n".format(file.name,rogue*100))

    return blue, rogue

def parse_dd_small_nasari():

    #Parse the "dd-small-nasari-15.txt" file, and it converts into a Python dictionary.

    nasari = {}
    with open(path + "input/dd-small-nasari-15.txt", 'r', encoding="utf8") as file:
        for line in file.readlines():
            splits = line.split(";")
            vector_dict = {}
            for term in splits[2:]:
                t_score = term.split("_")
                if len(t_score) > 1: vector_dict[t_score[0]] = t_score[1]     # vector[term] = score

            nasari[splits[1].lower()] = vector_dict #split 1 = wikipage title
    #{word: {term:score}}
    return nasari

def parse_document(file):

    #parse the document and return a list of all paragraphs.

    document = []
    data = file.read_text(encoding='utf-8')
    lines = data.split('\n')

    for line in lines:
        #if the # is not present, we have a normal paragraph to append to the list.
        if line != '' and '#' not in line:  #if the "#" character is present, the line contains the original link of the doc.
            line = line[:-1]  # deletes the final "\n" character.
            document.append(line)

    return document

def summarization_step(document, nasari, percentage):

    #Applies summarization to the document with the input percentage (10/20/30)

    # getting the topics based on the document's title.
    topics = title_topic(document, nasari)
    paragraphs = []
    i = 0
    # for each paragraph, except the title (document[0])
    for paragraph in document[1:]:
        context = create_context(paragraph, nasari)
        paragraph_wo = 0  # Weighted Overlap average inside the paragraph.
        # Computing WO
        for word_vector in context:
            topic_wo = 0
            for topic_vector in topics:
                topic_wo += weighted_overlap(topic_vector, word_vector)
            if topic_wo != 0:
                topic_wo = topic_wo / len(topics)

            # Sum all words WO in the paragraph's WO
            paragraph_wo += topic_wo

        if len(context) > 0:
            paragraph_wo = paragraph_wo / len(context)
            # append in paragraphs a tuple with the index of the paragraph (to
            # preserve order), the WO of the paragraph and the paragraph's text.
            paragraphs.append((i, paragraph_wo, paragraph))
        i += 1

    to_keep = len(paragraphs) - int(round((percentage / 100) * len(paragraphs), 0))
    # Sort by highest score (WO) and keeps all the important entries. From first to "to_keep"
    keep_best_paragraph = sorted(paragraphs, key=lambda x: x[1], reverse=True)[:to_keep]
    # Restore the original order.
    sort_paragraphs = sorted(keep_best_paragraph, key=lambda x: x[0], reverse=False)

    final_summary = []
    for tuple in sort_paragraphs:
        final_summary.append(tuple[2])

    final_summary = [document[0]] + final_summary
    return final_summary

if __name__ == "__main__":

    path_to_input = Path(path + "/documents_to_summarize")
    documents = list(path_to_input.glob('./*.txt'))

    PERCENTAGE = 30
    total_blue_score = 0
    total_rouge_score = 0
    print("------------")
    print(f"Start Summarization task.  Percentage: {PERCENTAGE}")
    nasari = parse_dd_small_nasari()
    n_documents = 0 # n of documents

    for file in documents:
        n_documents += 1
        document = parse_document(file)
        # For each document do summarization.
        summary = summarization_step(document, nasari, PERCENTAGE)

        with open(path + "output/" + str(PERCENTAGE) + "_"+ file.name  ,'w', encoding='utf-8') as out:
            for paragraph in summary:
                out.write(paragraph + '\n')

        #start the evaluation for each document
        blue, rogue = evaluation_step(summary)
        total_blue_score += blue
        total_rouge_score += rogue
    print("Average BLUE score : {0:.2f}\nAverage ROUGE score : {1:.2f}".format(total_blue_score/n_documents,total_rouge_score/n_documents*100))

    print("------------")


"""
Get attention maps, bounding box for a batch
result including: attention maps for all images of
all classes, top5 predictions
All numerical inputs should be in numpy form
"""
import numpy as np
import cv2
import skimage
import skimage.io as imio
import skimage.transform as imtrans
import os
from .get_attention_map import get_attention_map
from .gen_bbox import gen_bbox
import warnings

def get_batch_rst(batch_names, img_szs, cls_scores, conv_ft, proposals, lr_weight,
                    att_map_dir, top5_save_file):
    img_list_file = top5_save_file.split('.txt')[0] + '_img_list.txt'
    batch_size, cls_number = cls_scores.shape
    atten_maps = get_attention_map(conv_ft, lr_weight, proposals)
    for bs in range(batch_size):
        img_name = batch_names[bs]
        with open(img_list_file, 'a+') as i_list:
            i_list.write(img_name + '\n')
        print('img {}/{} {}'.format(bs, batch_size-1, img_name))
        img_sz = img_szs[bs, :]
        map_save_dir = os.path.join(att_map_dir, img_name.split('.')[0])
        img_box_file = os.path.join(map_save_dir,
                                    '{}.txt'.format(img_name.split('.')[0]))
        # if ~os.path.isdir(map_save_dir):
        os.makedirs(map_save_dir, exist_ok=True)

        cls_score = cls_scores[bs, :]
        indexes = (-cls_score).argsort()[0:5]
        img_boxes = None
        maps = atten_maps[bs, :, :, :]
        for cls_idx in range(cls_number):
            cls_map = maps[:, :, cls_idx]
            cls_map = cv2.normalize(cls_map, None, 0.0, 0.99, cv2.NORM_MINMAX)
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                cls_map = skimage.img_as_float(cls_map)
                cls_map = imtrans.resize(cls_map, img_sz, mode='reflect')
                cls_map_file = map_save_dir + '/' + img_name.split('.')[0] + \
                            '_{}.jpg'.format(cls_idx)
                imio.imsave(cls_map_file, cls_map)
            box = gen_bbox(cls_map)
            img_boxes = box if cls_idx==0 else np.vstack((img_boxes, box))
            tmp_str = '{} {} {} {} {}'.format(cls_idx, box[0], box[1], box[2], box[3])
            with open(img_box_file, 'a+') as im_file:
                im_file.write(tmp_str + '\n')
        # record predict to top5 file
        top5_str = ''
        for idx in range(5):
            top5_str += '{} {} {} {} {} '.format(indexes[idx],
                        img_boxes[indexes[idx]][0], img_boxes[indexes[idx]][1],
                        img_boxes[indexes[idx]][2], img_boxes[indexes[idx]][3])
        top5_str += '\n'
        with open(top5_save_file, 'a+') as top5_f:
            top5_f.write(top5_str)


"""Notas URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.contrib.auth import views as auth_views
from Usuarios import views as VistaUsuarios
from ManejoNotas import views as VistaManejoNotas

urlpatterns = [
    # Aquí se pueden ver las tablas de las bases de datos
    path('admin/', admin.site.urls),

    # Muestra el formulario para registrarse o si es post registra un usuario
    path('registrarse/', VistaUsuarios.registrarse, name='registrarse'),
    # Muestra el formulario para iniciar sesión o si es post inicia sesión
    path('iniciar-sesion/', auth_views.LoginView.as_view(
        template_name='usuarios/iniciar-sesion.html',
        next_page='/mis-notas/'), name='iniciar-sesion'),
    # Cierra sesión y lo devuelve a la ventana de iniciar sesión
    path('cerrar-sesion/', auth_views.LogoutView.as_view(next_page='/iniciar-sesion/'), name='cerrar-sesion'),
    # Si se está autenticado muestra la ventana de mis notas, sino lo manda
    # a iniciar sesión
    path('mis-notas/', VistaManejoNotas.mis_notas, name='mis-notas'),
    # Muestra una lista con las notas del usuario
    path('ver-mis-notas/', VistaManejoNotas.ver_mis_notas, name='ver-mis-notas'),
    # Guarda la nota ya sea que la esté editando o creando, si la está editando
    # se debe enviar el id de la nota
    path('guardar-nota/', VistaManejoNotas.guardar_nota, name='guardar-nota'),
    # Borra una nota según el id
    path('borrar-nota/', VistaManejoNotas.borrar_nota, name='borrar-nota'),
    # vista de editar nota
    path('editar-nota/', VistaManejoNotas.editar_nota, name='editar-nota'),
    # vista de crear nota
    path('crear-nota/', VistaManejoNotas.crear_nota, name='crear-nota'),
]


from query_machine.chatgpt import send_to_chatgpt
import query_machine.db as dbs
import json
import yaml
import openai

class QueryMachine:
    def __init__(self, config):
        self.name = config["name"]
        self.type = config["type"]
        self.connection = config["connection"]
        self.tables = config["tables"]
        # database
        db_class = getattr(dbs, self.type)
        self.db_instance = db_class(**self.connection)
        self.get_table_schema()

    def get_table_schema(self):
        if self.tables["table_schema_type"] == "yaml":
            with open(self.tables["table_schema_path"]) as f:
                self.table_schema = json.dumps(
                    yaml.load(f, Loader=yaml.FullLoader))
        elif self.tables["table_schema_type"] == "ddl":
            with open(self.tables["table_schema_path"]) as f:
                self.table_schema = f.read()
        else:
            raise Exception(
                f"no such schema type {self.tables['table_schema_type']}")

    def make_prompt(self, natural_language_query):
        return f"""We defined tables : {self.table_schema},
        make sql in {self.type} db language for next quote
        '{natural_language_query}'
        """

    # if you want to get sql only and no request to database required then use this
    def get_sql(self, natural_language_query):
        prompt = self.make_prompt(natural_language_query)
        try:
            response = send_to_chatgpt(prompt)
        except openai.error.APIConnectionError:
            response = send_to_chatgpt(prompt)

        try:
            ret = response.split("[")[1].split("]")[0]
        except:
            ret = response
        return ret

    def get_data(self, sql):
        return self.db_instance.send_query(sql)

    def ask(self, natural_language_query):
        sql_str = self.get_sql(natural_language_query)
        result = self.get_data(sql_str)
        return sql_str, result

    def info(self):
        return {
            "name": self.name,
            "type": self.type,
            "tables": self.table_schema,
        }


import matplotlib.pyplot as plt
import numpy as np

from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtWidgets import QApplication, QWidget, QStyle
from PyQt5.uic import loadUi
from enum import IntEnum

from SCTimeUtility.log.Log import getLog

class GraphType(IntEnum):
    LAP_TIME = 0,
    AVG_TIME = 1,
    MIN_TIME = 2,
    MAX_TIME = 3

class Graph(QWidget):
    maxGraphNumber = 100

    def __init__(self, uipath):
        super().__init__()
        self.UIPath = uipath
        self.GraphDict = ["Lap vs Time", "Average Lap vs Time", "Minimum Time", "Maximum Time"]
        self.currentGraphType = self.GraphDict[GraphType.LAP_TIME]
        self.graphedTeamList = []
        self.currGraphNum = 1
        self.inMinutes = False

        self.teamList = []

        self.initUI()
        self.addGraphs()
        self.handleUpdate(self.teamList)
        self.bindListeners()

    def initUI(self):
        self.ui = loadUi(self.UIPath, self)
        self.setGeometry(QStyle.alignedRect(Qt.LeftToRight, Qt.AlignRight,
                                            self.size(), QApplication.desktop().availableGeometry()))

    # add graph types
    def addGraphs(self):
        for graph in self.GraphDict:
            self.GraphTypes.addItem(graph)

    def bindListeners(self):
        self.ApplyGraphBtn.clicked.connect(self.drawGraph)
        self.GraphTypes.activated[str].connect(self.typeChosen)
        self.ChosenTeamList.itemDoubleClicked.connect(self.chosenTeamClick)
        self.MinuteButton.toggled.connect(self.timeToggle)
        self.TeamChoiceBox.activated.connect(self.teamChosen)

    def updateTeamList(self, newTeamList):
        self.teamList = newTeamList

    def populateTeamChoiceBox(self):
        self.graphedTeamList = []
        self.TeamChoiceBox.clear()
        for x in range(0, len(self.teamList)):
            self.TeamChoiceBox.addItem(str(self.teamList[x].getTeam()), x)

    def timeToggle(self):
        self.inMinutes = self.MinuteButton.isChecked()

    def handleUpdate(self, newTeamList):
        self.updateTeamList(newTeamList)
        self.populateTeamChoiceBox()

    def drawGraph(self):
        if self.currentGraphType == self.GraphDict[GraphType.LAP_TIME]:
            self.lapVsTimeGraph()
        elif self.currentGraphType == self.GraphDict[GraphType.AVG_TIME]:
            self.avgLapVsTimeGraph()
        elif self.currentGraphType == self.GraphDict[GraphType.MIN_TIME]:
            self.minTimeGraph()
        elif self.currentGraphType == self.GraphDict[GraphType.MAX_TIME]:
            self.maxTimeGraph()

    def teamChosen(self, index):
        self.addTeamToGraphList(self.TeamChoiceBox.currentIndex())

    def addTeamToGraphList(self, index):
        # check for space in list
        if len(self.graphedTeamList) >= self.maxGraphNumber:
            return False

        # if not in list add it
        if not (self.teamList[index] in self.graphedTeamList):
            self.ChosenTeamList.addItem(self.teamList[index].getTeam())
            self.graphedTeamList.append(self.teamList[index])
            return True
        return False

    def removeTeamFromGraphList(self, teamName):
        # search graph list and remove found element
        for i in range(0, len(self.graphedTeamList) - 1):
            team = self.graphedTeamList[i]
            if team.getTeam() == teamName:
                self.graphedTeamList.pop(i)
                return True
        return False

    def chosenTeamClick(self):
        # if the team is double clicked then remove it
        teamName = self.ChosenTeamList.currentItem().text()
        self.removeTeamFromGraphList(teamName)
        # remove the team from the list
        self.ChosenTeamList.takeItem(self.ChosenTeamList.currentRow())

    def typeChosen(self, text):
        # checks that type is valid (in types list)
        if text in self.GraphDict:
            self.currentGraphType = text

    def getElapsed(self, lapList):
        elapsed = []
        if self.inMinutes:
            for lap in lapList:
                elapsed.append(lap / 60)
        else:
            for lap in lapList:
                elapsed.append(lap)
        return elapsed

    def lapVsTimeGraph(self):

        plt.figure(self.currGraphNum)
        # set labels
        plt.title('Lap vs Time')
        plt.xlabel('Lap')
        if self.inMinutes:
            plt.ylabel('Time (minutes)')
        else:
            plt.ylabel('Time (seconds)')

        # plot data
        index = 0
        for team in self.graphedTeamList:
            durationList = []
            graphRange = np.arange(0, len(self.graphedTeamList[index].LapList), 1.0)
            plt.xticks(np.arange(1.0, len(self.graphedTeamList[index].LapList) + 1, 1.0))
            for lap in team.LapList:
                durationList.append(lap.getElapsed())
            durationList = self.getElapsed(durationList)
            # plot curent Team
            plt.plot(graphRange, durationList, label=team.getTeam())
            index += 1

        if len(self.graphedTeamList) > 0:
            plt.legend()
        plt.tight_layout()
        plt.grid(True)
        plt.show()

        # increments the figure number to guarantee new window for next graph
        self.currGraphNum += 1

    def avgLapVsTimeGraph(self):

        plt.figure(self.currGraphNum)

        # set labels
        plt.title('Lap vs Average Time')
        plt.xlabel('Lap')
        if self.inMinutes:
            plt.ylabel('Average Time (minutes)')
        else:
            plt.ylabel('Average Time (seconds)')

        # plot team lap averages
        index = 0
        for team in self.graphedTeamList:
            lapAverages = []
            graphRange = np.arange(0, len(team.LapList), 1.0)
            plt.xticks(np.arange(1.0, len(team.LapList) + 1, 1.0))

            # for every lap for current team calculate the average time
            currLap = 0
            for lap in team.LapList:
                if self.inMinutes:
                    lapAverages.append((lap.getElapsed() / ((currLap + 1) * 60)))
                else:
                    lapAverages.append(lap.getElapsed() / (currLap + 1))
                currLap += 1

            plt.plot(graphRange, lapAverages, label=team.getTeam())
        if len(self.graphedTeamList) > 0:
            plt.legend()

        plt.tight_layout()
        plt.grid(True)
        plt.show()

        # increments the figure number to guarantee new window
        self.currGraphNum += 1

    def minTimeGraph(self):
        labels = []
        data = []

        # calculate minimum times for teams
        for team in self.graphedTeamList:
            lapList = []
            for lap in team.LapList:
                if lap.getElapsed() != 0:
                    lapList.append(lap.getElapsed())

            data.append(min(lapList))
            labels.append(team.getTeam())

        # send data to bar graph
        if self.inMinutes:
            self.barGraph(data, labels, 'Minimum Times', 'Teams', 'Time (minutes)')
        else:
            self.barGraph(data, labels, 'Minimum Times', 'Teams', 'Time (seconds)')

    def maxTimeGraph(self):
        labels = []
        data = []

        # calculate minimum times for teams
        for team in self.graphedTeamList:
            lapList = []
            for lap in team.LapList:
                if lap.getElapsed() != 0:
                    lapList.append(lap.getElapsed())

            data.append(max(lapList))
            labels.append(team.getTeam())

        # send data to bar graph
        if self.inMinutes:
            self.barGraph(data, labels, 'Maximum Times', 'Teams', 'Time (minutes)')
        else:
            self.barGraph(data, labels, 'Maximum Times', 'Teams', 'Time (seconds)')

    def barGraph(self, data, labels, title, x_axis, y_axis):
        # increments the figure number to guarantee new window
        plt.figure(self.currGraphNum)
        self.currGraphNum += 1

        # graph settings
        # range is 1 because only 1 set of bars
        index = np.arange(1)
        bar_width = .5
        opacity = .8

        # add team to legend and plot their data
        for i in range(len(data)):
            plt.bar(index + bar_width * i, data[i],
                    width=bar_width,
                    label=labels[i],
                    alpha=opacity)

        # set labels
        plt.title(title)
        plt.xlabel(x_axis)
        plt.ylabel(y_axis)
        # remove x ticks
        plt.xticks(index, ' ')
        # add legend
        if len(data) > 0:
            plt.legend()

        plt.tight_layout()
        plt.show()


import torchvision.transforms as transforms
import torch

# Hyperparameters

IMG_SIZE = 224
BATCH_SIZE = 32
LR = 3e-04
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
NUM_EPOCHS = 50
NUM_CLASSES = 2
print(f"Selected Device: {DEVICE}")

# Dataset

AUGMENTATION = transforms.Compose(
    [
        transforms.RandomHorizontalFlip(),
        transforms.RandomHorizontalFlip(),
        transforms.RandomRotation(15),
        transforms.Resize(224),
        transforms.ToTensor(),
        transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
    ]
)
TRANSFORM = transforms.Compose(
    [
        transforms.Resize(224),
        transforms.ToTensor(),
        transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
    ]
)

ROOT_DATA_DIR = 'Datasets/FNAC_Total_Dataset_'

# Computation Related
PRECISION = '32-true'
ACCELERATOR = 'gpu'
DEVICES = [0]

# Log Related
MODEL_CKPT_DIR = 'ckpt/full/'
MODEL_CKPT_FILENAME = 'model-{epoch:02d}-{val_loss:.2f}'
LOG_DIR = 'log_dir/'
LOG_NAME = 'Resnet18_Pretrained_Full'
VERSION = '0_1(32-true)'


from utils.common import *
from scipy import ndimage
import numpy as np
from torchvision import transforms as T
import torch, os
from torch.utils.data import Dataset, DataLoader
import matplotlib.pyplot as plt

class Verse_DataSet(Dataset):
    def __init__(self, crop_size, resize_scale, dataset_path, mode=None):
        self.crop_size = crop_size
        self.resize_scale = resize_scale
        self.dataset_path = dataset_path
        self.n_labels = 25

        if mode == 'train':
            self.filename_list = load_file_name_list(os.path.join(dataset_path, 'train_name_list.txt'))
        elif mode == 'val':
            self.filename_list = load_file_name_list(os.path.join(dataset_path, 'val_name_list.txt'))
        else:
            raise TypeError('Dataset mode error!!! ')

    def __getitem__(self, index):
        data, target = self.get_train_batch_by_index(crop_size=self.crop_size, index=index,
                                                     resize_scale=self.resize_scale)
        return torch.from_numpy(data), torch.from_numpy(target)

    def __len__(self):
        return len(self.filename_list)

    def get_train_batch_by_index(self, crop_size, index, resize_scale=1):
        img, label = self.get_np_data_3d(self.filename_list[index], resize_scale=resize_scale)
        img, label = random_crop_3d(img, label, crop_size)
        return np.expand_dims(img, axis=0), label

    def get_np_data_3d(self, filename, resize_scale=1):
        data_np = sitk_read_raw(self.dataset_path + '/data/' + filename,
                                resize_scale=resize_scale)
        data_np = norm_img(data_np)
        label_np = sitk_read_raw(self.dataset_path + '/label/' + filename.replace('image', 'mask'),
                                 resize_scale=resize_scale)
        return data_np, label_np

def main():
    fixd_path = r'F:\Verse_Data\fixed_path'
    dataset = Verse_DataSet([16, 128, 128], 0.5, fixd_path, mode='train')  # batch size
    data_loader = DataLoader(dataset=dataset, batch_size=2, num_workers=0, shuffle=True)
    for batch_idx, (data, target) in enumerate(data_loader):
        target = to_one_hot_3d(target.long())
        print(data.shape, target.shape)
        plt.subplot(121)
        plt.imshow(data[0, 0, 0])
        plt.subplot(122)
        plt.imshow(target[0, 1, 0])
        plt.show()

if __name__ == '__main__':
    main()


# Импорт библиотеки flask
from flask import Flask

# Импорт модуля sqlite3
import sqlite3

# Импорт json
import json

app = Flask(__name__)

def get_data_sql(sql):
    """Создание подключения к БД с помощью метода sqlite3.connect"""
    with sqlite3.connect("netflix.db") as connection:
        connection.row_factory = sqlite3.Row
        result = connection.execute(sql).fetchall()
    return result

@app.route('/movie/<title>', methods=['GET'])
def get_by_title(title):
    """ Представление страницы с выборкой фильма по названию в формате JSON"""
    result = {}
    for item in get_data_sql(sql=f"""
        SELECT title, country, release_year, listed_in as genre, description
        FROM netflix
        WHERE title LIKE '%{title.title()}%'
        ORDER BY release_year desc
        """):
        result = dict(item)

    return app.response_class(
        json.dumps(result, ensure_ascii=False, indent=4),
        mimetype="application/json",
        status=200
    )

@app.route('/movie/<int:year_one>/to/<int:year_two>', methods=['GET'])
def get_by_period(year_one, year_two):
    """ Представление страницы с выборкой фильма по диапазону лет выпуска в формате JSON"""
    sql = f"""
        select * from netflix
        WHERE release_year between '{year_one}' AND '{year_two}'
        LIMIT 100
        """

    result = []
    for item in get_data_sql(sql):
        result.append(dict(item))

    return app.response_class(
        json.dumps(result[:100], ensure_ascii=False, indent=4),
        mimetype="application/json",
        status=200
    )

@app.route('/rating/<rating>', methods=['GET'])
def get_rating(rating):
    """ Представление страницы поиска по рейтингу."""
    rating_dict = {
        "children": ("G", "G"),
        "family": ('G', 'PG', 'PG-13'),
        "adult": ('R', 'NC-17')
    }

    sql = f"""
        select * from netflix
        WHERE rating in {rating_dict.get(rating, ('PG', 'NC-17'))}
        """

    result = []
    for item in get_data_sql(sql):
        result.append(dict(item))

    return app.response_class(
        json.dumps(result, ensure_ascii=False, indent=4),
        mimetype="application/json",
        status=200
    )

@app.route('/genre/<genre>', methods=['GET'])
def get_genre(genre):
    """ Представление страницы с выборкой 10 самых свежих фильмов в формате json по жанру"""
    sql = f"""
            select title, description from netflix
            WHERE listed_in LIKE '%{str(genre).title()}%'
            ORDER by release_year DESC
            LIMIT 10
        """

    result = []
    for item in get_data_sql(sql):
        result.append(dict(item))

    return app.response_class(
        json.dumps(result, ensure_ascii=False, indent=4),
        mimetype="application/json",
        status=200
    )

def get_names(name1='Rose McIver', name2='Ben Lamb'):
    """ Функция получает в качестве аргумента имена двух актеров,
           и возвращает список тех, кто играет с ними в паре больше 2 раз"""
    global result
    sql = f"""
            select "cast" from netflix
            WHERE "cast" LIKE '%{name1}%'
            AND "cast" LIKE '%{name2}%'
        """
    names_dict = {}
    for item in get_data_sql(sql):
        result = dict(item)

        names = set(result.get('cast').split(", ")) - {name1, name2}

        for name in names:
            names_dict[name.strip()] = names_dict.get(name.strip(), 0) + 1

    print(names_dict)

    for key, value in names_dict.items():
        if value > 2:
            print(key)

    return app.response_class(
        json.dumps(result, ensure_ascii=False, indent=4),
        mimetype="application/json",
        status=200
    )
#print(get_names())

def get_film(types='Movie', year=2020, genre='Horror'):
    """Функция передает тип картины (фильм или сериал),
        год выпуска и ее жанр и получать на выходе список
        названий картин с их описаниями в JSON."""
    sql = f"""
            select * from netflix
            WHERE type = '{types.title()}'
            AND release_year = '{year}'
            AND listed_in LIKE  '%{genre.title()}%'
        """

    result = []
    for item in get_data_sql(sql):
        result.append(dict(item))

    return json.dumps(result, ensure_ascii=False, indent=4)

#print(get_film())

if __name__ == '__main__':
    app.run(port=5000, debug=True)


from torch.utils.data import Dataset
from PIL import Image
import os
from pathlib import Path
from utilities.utils import standardize
from torchvision import transforms
import os.path

class Isic:
    def __init__(self, dataroot, dim=64):
        transform = transforms.Compose([transforms.Resize([dim, dim]), transforms.ToTensor(), standardize])
        path = Path(dataroot) / "data" / "ISIC"
        self.train_dataset = ISICDataset(path, transform=transform, test=False, malignant=False)
        self.test_dataset = ISICDataset(path, transform=transform, test=True, malignant=False)
        self.out_dataset = ISICDataset(path, transform=transform, malignant=True)
        self.imDim = (3, dim, dim)
        self.num_classes = 2

class ISICDataset(Dataset):
    def __init__(self, img_path, transform, test=False, malignant=False):
        if malignant:
            self.img_path = os.path.join(img_path, "malignant")
            self.ims = os.listdir(self.img_path)
            self.target = 1
        else:
            if test:
                self.img_path = os.path.join(img_path, "benign_test")
            else:
                self.img_path = os.path.join(img_path, "benign")
            self.ims = os.listdir(self.img_path)
            self.target = 0
        self.transform = transform

    def __getitem__(self, index):
        img_name = os.path.join(self.img_path, self.ims[index])
        img = Image.open(img_name)
        img = self.transform(img)
        return img, self.target

    def __len__(self):
        return len(self.ims)


#!/usr/bin/python3

import requests
import time
from bs4 import BeautifulSoup as bs
from datetime import datetime
import json
import sqlite3
import os

# esta funcion es la encargada de crear la base de datos
def crear_db():
    conex = sqlite3.connect('crypto.db')
    c = conex.cursor()

    c.execute('''CREATE TABLE crypto
         (ID text,
          CRYPTO text,
          COMPRA float,
          VENTA float,
          MONTO float,
          BENEFICIO float,
          FECHA_COMPRA text,
          FECHA_VENTA text)''')
    conex.close()

# esta funcion inserta los datos dentro de la base de datos

def insertar_data(data):
    conex = sqlite3.connect('crypto.db')
    c = conex.cursor()
    # manipula la variable data para tener un formato que acepta
    # el comando execute de la base de datos
    string = 'INSERT INTO crypto VALUES('
    number = len(data) - 1
    j = 0
    for k in data:
        if j < number:
            string += k + ', '
        else:
            string += k
        j += 1
    string += ')'
    print(string)
    ### inserta valores en la base de datos
    c.execute(string)
    ### hace commit a la base de datos para registrar los valores.
    conex.commit()
    conex.close()

# obtiene la fecha actual de la computadora, es importante porque
# sera una de las entradas de la base de datos.
def obtener_fecha():
    # datetime object containing current date and time
    actual = datetime.now()

    # dd/mm/YY H:M:S
    fecha = actual.strftime("%Y/%m/%d %H:%M:%S")
    return fecha

# fabrica una id de localizacion, una especie de numero de factura
# para las transacciones, simplemente usa la fecha de venta.
def obtener_id(fecha_venta):
    id = 'RE'
    for k in fecha_venta:
        if k == '/' or k == ':':
            id += ''
        elif k == ' ':
            id += '_'
        else:
            id += k
    return id

# funcion que hace scraping, trae el valor en dolares de la cryptomoneda
# en el momento que es invocada, recibe el nombre de la cryptomoneda
# si no recibe nada usara bitcoin como moneda por defecto.
def scrape(crypto = 'bitcoin'):
    url = "https://coinmarketcap.com/currencies/" + crypto + '/'
    respuesta = requests.get(url)
    datos = bs(respuesta.content,'html.parser')
    # busca la clase priceValue en el codigo html de la pagina
    # para poder extraer el precio correcto.
    contenido = datos.find('div',{'class':'priceValue'})
    precio = contenido.select_one('span')
    precio = precio.text

    # quita los adornos de la moneda para poder convertirla en un
    # valor float para su uso posterior.
    value = ''
    for k in precio:
        cond = (k == "$" or k == ",")
        if not cond:
            value += k

    value = float(value)
    return value

# se encarga de borrar una entrada del json que ya este completa, en otras
# palabras que ya haya realizado su operacion financiera, es necesario
# borrar la entrada para que la base de datos no tome mas de una vez el
# valor de la transaccion.
def borrar_entrada_json():
    with open('file.json', 'r') as file:
        json_data = json.load(file)
        num = 0
        for k in json_data:
            if k["id"] != "0":
                num += 1

        for k in range(num):
            for i in range(len(json_data)):
                if json_data[i]["id"] != "0":
                    json_data.pop(i)
                    open("file.json", "w").write(json.dumps(json_data,
                                                            indent = 4))
                    break

# revisa el archivo json para poder hacer las operaciones que en el se indican
def json_update(archivo_json):
    with open(archivo_json, 'r') as file:
        json_data = json.load(file)
        size = len(json_data)
        num = 0
        for item in json_data:
            if item['id'] != '0':
                # crear data para la base de datos
                data = []
                for k in item:
                    if type(item[k]) == str:
                        val = "'" + item[k] + "'"
                    else:
                        val = str(item[k])
                    data.append(val)
                # enviar data a la base de datos
                insertar_data(data)
    #borrar data de json
    borrar_entrada_json()

    with open(archivo_json, 'r') as file:
        json_data = json.load(file)
        for item in json_data:
            if item['fecha_venta'] == '0':
                #obtener el precio
                precio = scrape(item['crypto'])
                print("precio crypto: ", precio)
                if item['fecha_compra'] == '0':
                    if precio <= float(item['compra']):
                        item['compra'] = precio
                        item['fecha_compra'] = obtener_fecha()
                else:
                    if precio >= float(item['venta']):
                        item['venta'] = precio
                        item['fecha_venta'] = obtener_fecha()
                        item['beneficio'] = item['monto'] * (item['venta'] / item['compra'] - 1)
                        item['id'] = obtener_id(item['fecha_venta'])
            else:
                num += 1

        if num == size:
            print("TODAS LAS OPERACIONES SE COMPLETARON")

    with open(archivo_json, 'w') as file:
        json.dump(json_data, file, indent=4)

# verifica si existe la base de datos
file_name = r"crypto.db"
bool = os.path.exists(file_name)

# si no existe la base de datos la crea
if not bool:
    crear_db()

# loop de peticiones de precios
while 1:
    json_update('file.json')
    time.sleep(60)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.metrics import r2_score
import pickle
warnings.filterwarnings("ignore")

dataframe = pd.read_csv("C:\\Users\\paulo\\PycharmProjects\\feature-selection\\hour.csv")

def showHist(dataframe):
    plt.rcParams["figure.figsize"] = 15, 18
    dataframe.hist()

showHist(dataframe)

def showPairPlot(dataframe):
    sns.pairplot(dataframe)

showPairPlot(dataframe)

arra = dataframe["dteday"].str.split("-")[0]

def extractColumnsYearMonthDateFromDteday(dataframe):
    dataframe['Year'] = dataframe['dteday'].str.split('-').str[0]
    dataframe['Year'] = dataframe['Year'].astype(int)
    dataframe['Month'] = dataframe['dteday'].str.split('-').str[1]
    dataframe['Month'] = dataframe['Month'].astype(int)
    dataframe['Date'] = dataframe['dteday'].str.split('-').str[2]
    dataframe['Date'] = dataframe['Date'].astype(int)

    dataframe = dataframe.drop(['dteday'], axis = 1)
    dataframe = dataframe.drop(['yr'], axis = 1)
    dataframe = dataframe.drop(['mnth'], axis = 1)
    return(dataframe)

print(dataframe.isna().sum())

dataframe = extractColumnsYearMonthDateFromDteday(dataframe)
DISTANCE_MID_TO_TAIL_IN_BOX_PLOT = 1.5

def outlier(x):
    high=0
    q1 = x.quantile(.25)
    q3 = x.quantile(.75)
    iqr = q3 - q1
    #***************************************************
    #          _________________
    #          |       |        | DISTANCE BETWEEN MID TO LOWER TAIL IS 1.5
    #----------|      mid       |------------
    #          |       |        |
    #          |________________|
    #
    #***************************************************
    low = q1 - DISTANCE_MID_TO_TAIL_IN_BOX_PLOT * iqr
    high += q3 + DISTANCE_MID_TO_TAIL_IN_BOX_PLOT * iqr
    outlier = (x.loc[(x < low) | (x > high)])
    return(outlier)

cont = outlier(dataframe['cnt']).count()

print(cont)

q1 = dataframe['cnt'].quantile(.25)
q3 = dataframe['cnt'].quantile(.75)
iqr = q3-q1
df_new = dataframe[~((dataframe['cnt'] < (q1 - 1.5 * iqr)) | (dataframe['cnt'] > (q3 + 1.5 * iqr)))]

x = df_new.drop(['cnt'], axis = 1)
y = df_new['cnt']
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.30, random_state=40)

lr = LinearRegression(copy_X = True, fit_intercept = True, n_jobs = 0, normalize = False)
LR = lr.fit(x_train, y_train)
LR_Pred = lr.predict(x_test)

print('ROOT EAN ERROR SQUARE:', np.sqrt(metrics.mean_squared_error(y_test, LR_Pred)))

print('R SQUARE:', r2_score(y_test, LR_Pred))

pickle.dump(lr, open('LinearRegression.pkl', 'wb'))


import pygame
from math import *

class Planet:
    def __init__(self, x_expr, y_expr, angle_expr, radius, mass, filename):
        self.x_expr = x_expr
        self.y_expr = y_expr
        self.angle_expr = angle_expr
        self.radius = radius
        self.mass = mass
        self.sprite = pygame.image.load(filename).convert_alpha()

        self.update(0)

    def update(self, time):
        self.pos = (eval(self.x_expr.replace("t", str(time))),
                    eval(self.y_expr.replace("t", str(time))))
        self.angle = eval(self.angle_expr.replace("t", str(time)))

    def draw(self, canvas):
        rotated = pygame.transform.rotozoom(self.sprite, self.angle, 1)
        width, height = rotated.get_size()
        x = self.pos[0] - width // 2
        y = self.pos[1] - height // 2
        canvas.blit(rotated, [x, y])

        # pygame.draw.rect(canvas,[0,255,0],
        #                  [self.pos[0]-self.radius,self.pos[1]-self.radius,
        #                   2*self.radius, 2*self.radius],1)


import requests

from bs4 import BeautifulSoup

import csv

url = 'https://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp'

headers = {'User-Agent' : 'Mozilla-5.0'}

resp = requests.get(url,headers = headers)

# print (resp.status_code)

soup = BeautifulSoup(resp.text,'html.parser')

def nifty_value(soup):
	return (soup.find('table').span.text)

table_find = soup.find('table', id = 'octable')

table_body = table_find.find('tr')

# print (table_find.prettify())

# rws = table_body.find_all('th')
# print (rws[0].text)

# print (rws)

final_list = []

# print (table_body)

for rowes in table_find.find_all('tr'):
	inter_list = []

	# print (rowes)

	for colum in rowes.find_all('th'):
		# print (colum)
		inter_list.append(colum.text)

	if len(inter_list) != 0:

		inter_list = [i.upper() for i in inter_list]

		final_list.append(inter_list)

# print (final_list)

# def extract_data(ref_text_row,ref_text_col,list_name):
# 	table_body = table_find.find(ref_text_row)

# print (final_list)

final_list1 = []

for rowes in table_find.find_all('tr'):
	inter_list = []

	# print (rowes)

	for colum in rowes.find_all('td'):
		# print (colum)
		inter_list.append(colum.text)

	if len(inter_list) != 0:

		inter_list = [i.strip() for i in inter_list]

		final_list1.append(inter_list)

# print (final_list1)

input_csv_data = final_list[1:] + final_list1

# print (input_csv_data)

with open('option_data.csv','w') as file1:

	file1_writer = csv.writer(file1)

	for i in input_csv_data:
		# print (i)
		file1_writer.writerow(i)

print (nifty_value(soup))


"""
Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42. These divisors squared are: 1, 4, 9, 36, 49, 196, 441, 1764. The sum of the squared divisors is 2500 which is 50 * 50, a square!

Given two integers m, n (1 <= m <= n) we want to find all integers between m and n whose sum of squared divisors is itself a square. 42 is such a number.

The result will be an array of arrays or of tuples (in C an array of Pair) or a string, each subarray having two elements, first the number whose squared divisors is a square and then the sum of the squared divisors.

#Examples:

list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
list_squared(42, 250) --> [[42, 2500], [246, 84100]]
The form of the examples may change according to the language, see Example Tests: for more details.

Note

In Fortran - as in any other language - the returned string is not permitted to contain any redundant trailing whitespace: you can use dynamically allocated character strings.
"""

from functools import reduce
from math import sqrt

def factors(n):
    return set(reduce(list.__add__,
                ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))

def list_squared(m, n):
    nums = []
    for i in range(m,n+1):
        fc = factors(i)
        sm = sum(map(lambda x:x**2, fc))
        if sqrt(sm).is_integer():
            nums.append([i, sm])
    return nums

Codewars/5 kyu/Integers Recreation One.py