import json
from plotly.graph_objs import Scattergeo, Layout
from plotly import offline

# Read file with Earthquakes in the World: last 24 file form
filename = 'data/quake.json'
with open(filename) as f:
    data_quake = json.load(f)

# Show data in better form
# readfile = 'data/read_quake.json'
# with open(readfile, 'w') as f:
#     json.dump(data_quake, f, indent=5)

# Create list with features which contain information about quakes
all_quake_dict = data_quake['features']

# var for cordinates quakes
cordinates_x, cordinates_y = [], []

for quake_dict in all_quake_dict:
    cordinate_x = quake_dict['geometry']['coordinates'][0]  # long nesting - debug helps
    cordinate_y = quake_dict['geometry']['coordinates'][1]
    cordinates_x.append(cordinate_x)
    cordinates_y.append(cordinate_y)

print(cordinates_x)
print(cordinates_y)

data = [Scattergeo(lon=cordinates_x, lat=cordinates_y)]  # Obj Scattergeo - placing points on the map
layout = Layout(title='Earthquakes in the World')

fig = {'data': data, 'layout': layout}
offline.plot(fig, filename='earthquakes.html')


num = input('Введите искомое число :')
lst = ['Здесь нет','Здесь есть число 123','Здесь число 3214 есть','Здесь опять ничего нету','456 А здесь есть даже два 789']

# Объединяем все строки в один текст, где элемент списка - слово
lst = ' '.join(lst).split(' ')
print(lst)

# Ищем есть ли заданное число среди слов
ans = list(filter(lambda x: x == num, lst))

print('Заданное число', 'присутствует' if ans else 'отсутствует')

# А здесь ищем пристуствуют ли вообще числа в тексте
ans = list(filter(lambda x: x.isdigit(), lst))
print('Число/числа в строках','есть' if ans else 'нет')


#!/usr/bin/env python3

"""
This ROS node simulates a traffic light (with random initial state) and
publishes a "TrafficLightStatus" message. You can also add the topic
"traffic_light_visu" in rviz to display the traffic light.

To view the state machine, you may use "rosrun smach_viewer smach_viewer.py",
but you need to install corresponding libraries first using:

sudo apt install \
    ros-noetic-smach \
    ros-melodic-smach-ros \
    ros-noetic-smach-msgs \
    ros-noetic-smach-viewer \
    python-gtk2
"""

import rospy
import smach
from smach_ros import IntrospectionServer
import tf
import numpy as np
from visualization_msgs.msg import MarkerArray

from car_demo.msg import TrafficLightStatus
from traffic_light_simulator_visu import (
    getTrafficLightVisualization,
    TrafficLightControlReceiver,
)

# global instance of traffic light interaction
tl_control = TrafficLightControlReceiver()

# stop line coordinates: (xRight, yRight, xLeft, yLeft)
STOP_LINE = (35.6, -0.1, 29.7, -4.6)

# max distance for valid stop lines
MAX_STOP_LINE_DIST = 25

def distPoint2LineSegment(veh_x, veh_y, veh_phi, xr, yr, xl, yl):
    """
    Find the closest point from vehicle pose (veh_x, veh_y, veh_phi)
    to a line segment (xr, yr, xl, yl).
    This code is adapted from:
    https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
    """
    px = xl - xr
    py = yl - yr
    norm = px * px + py * py

    u = ((veh_x - xr) * px + (veh_y - yr) * py) / float(norm)
    u = np.clip(u, 0, 1)

    x = xr + u * px
    y = yr + u * py

    dx = x - veh_x
    dy = y - veh_y
    dist = np.hypot(dx, dy)

    # normal in driving direction
    nx, ny = py, -px
    if dx * nx + dy * ny < 0:
        dist = -dist  # we passed the stop line already

    dphi = veh_phi - np.arctan2(ny, nx)
    dphi = (dphi + np.pi) % (2 * np.pi) - np.pi  # normalize to -pi..pi

    return (dist, dphi)

# define general traffic light state handler
class TrafficLightState(smach.State):
    def __init__(
        self,
        tf_listener,
        tl_publisher,
        tlvis_publisher,
        color="GREEN",
        duration_s=10,
        rate_hz=10,
    ):
        smach.State.__init__(self, outcomes=["ellapsed", "aborted"])
        self.rate_hz = rate_hz
        self.duration_s = duration_s
        self.color = color
        self.tf_listener = tf_listener
        self.tl_publisher = tl_publisher
        self.tlvis_publisher = tlvis_publisher
        # NOTE: the order is important here (xRight, yRight, xLeft, yLeft)
        self.stopline = STOP_LINE
        self.dist2bumper = 2.75

    def execute(self, userdata):
        t_start = rospy.get_time()
        r = rospy.Rate(self.rate_hz)
        x1, y1, x2, y2 = self.stopline
        curr_cnt = tl_control.event_counter

        rospy.logout("Entering state %s." % (self.color))

        while (
            (tl_control.automatic) and (rospy.get_time() - t_start < self.duration_s)
        ) or ((not tl_control.automatic) and (tl_control.event_counter == curr_cnt)):

            # check if rospy has been terminated
            if rospy.is_shutdown():
                rospy.logout("Aborting state %s." % (self.color))
                return "aborted"

            # get vehicle position data
            try:
                (trans, rot) = self.tf_listener.lookupTransform(
                    "map", "din70000", rospy.Time(0)
                )  # get latest trafo between world and vehicle
                rpy = tf.transformations.euler_from_quaternion(rot)
                rospy.logdebug("pos: T=%s, rpy=%s" % (str(trans), str(rpy)))
            except (
                tf.LookupException,
                tf.ConnectivityException,
                tf.ExtrapolationException,
            ):
                rospy.logwarn("Could not receive position information!")
                r.sleep()
                continue

            # compute relative pose to stop line
            dx, dy = self.dist2bumper * np.cos(rpy[2]), self.dist2bumper * np.sin(
                rpy[2]
            )
            dist, dphi = distPoint2LineSegment(
                trans[0] + dx, trans[1] + dy, rpy[2], x1, y1, x2, y2
            )
            rospy.loginfo("dist=%.1f, dphi_deg=%.1f" % (dist, dphi * 180.0 / np.pi))

            # publish current traffic light status
            tl_status = TrafficLightStatus()
            state_dict = {
                "GREEN": tl_status.GREEN,
                "YELLOW": tl_status.YELLOW,
                "RED": tl_status.RED,
                "RED_YELLOW": tl_status.RED_YELLOW,
            }
            tl_status.header.stamp = rospy.get_rostime()
            tl_status.dist_m = dist
            tl_status.dphi_rad = dphi

            if (
                np.abs(tl_status.dist_m) > MAX_STOP_LINE_DIST
            ):  # traffic light too far away
                tl_status.dist_m = tl_status.INVALID
                tl_status.dphi_rad = tl_status.INVALID

            tl_status.state = state_dict[self.color]
            self.tl_publisher.publish(tl_status)

            # publish traffic light visualization
            tl_visualization = getTrafficLightVisualization(
                x1, y1, tl_status.header, self.color
            )
            self.tlvis_publisher.publish(tl_visualization)

            try:
                r.sleep()
            except rospy.exceptions.ROSInterruptException as e:
                rospy.logdebug(
                    f"Stopping {rospy.get_name()}, because of interrupt: {e}"
                )

        rospy.logout("Leaving state %s." % (self.color))

        return "ellapsed"

# main
def main():
    rospy.init_node("traffic_light_simulator")

    # For retrieving vehicle pose
    listener = tf.TransformListener()

    # For retrieving traffic light control commands
    tl_control.initialize()

    # For publishing the traffic lilght status and visualization
    status_publisher = rospy.Publisher(
        "traffic_light_status", TrafficLightStatus, queue_size=1
    )
    visu_publisher = rospy.Publisher("traffic_light_visu", MarkerArray, queue_size=1)

    # Create a SMACH state machine
    sm = smach.StateMachine(outcomes=["ellapsed", "aborted"])

    # Open the container
    with sm:
        # Add states to the container
        smach.StateMachine.add(
            "GREEN",
            TrafficLightState(
                color="GREEN",
                duration_s=5,
                tf_listener=listener,
                tl_publisher=status_publisher,
                tlvis_publisher=visu_publisher,
            ),
            transitions={"ellapsed": "YELLOW"},
        )
        smach.StateMachine.add(
            "YELLOW",
            TrafficLightState(
                color="YELLOW",
                duration_s=3,
                tf_listener=listener,
                tl_publisher=status_publisher,
                tlvis_publisher=visu_publisher,
            ),
            transitions={"ellapsed": "RED"},
        )
        smach.StateMachine.add(
            "RED",
            TrafficLightState(
                color="RED",
                duration_s=5,
                tf_listener=listener,
                tl_publisher=status_publisher,
                tlvis_publisher=visu_publisher,
            ),
            transitions={"ellapsed": "RED_YELLOW"},
        )
        smach.StateMachine.add(
            "RED_YELLOW",
            TrafficLightState(
                color="RED_YELLOW",
                duration_s=3,
                tf_listener=listener,
                tl_publisher=status_publisher,
                tlvis_publisher=visu_publisher,
            ),
            transitions={"ellapsed": "GREEN"},
        )

    # Set random initial state
    # state_list = ['GREEN', 'YELLOW', 'RED', 'RED_YELLOW']
    # sm.set_initial_state([random.choice(state_list)])
    sm.set_initial_state(["RED"])

    # For debugging and visualisation of state machine
    sis = IntrospectionServer("server_name", sm, "/SM_ROOT")
    sis.start()

    # Execute SMACH plan
    sm.execute()

    rospy.spin()
    sis.stop()

if __name__ == "__main__":
    main()


from PyQt5.QtWidgets import QDialog, QVBoxLayout, QTabWidget, QWidget, QGridLayout, QLabel, QTextBrowser, QDialogButtonBox
from PyQt5.QtWidgets import QApplication as app
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QUrl

class About(QDialog):
    def __init__(self, parent=None):
        super(QDialog, self).__init__(parent)
        self.setMinimumSize(600, 500)
        self.setMaximumSize(600, 500)
        self.verticalLayout = QVBoxLayout(self)
        self.tabWidget = QTabWidget(self)
        self.tabVersion = QWidget()
        self.gridLayout3 = QGridLayout(self.tabVersion)
        self.labelAbout = QLabel(self.tabVersion)
        self.gridLayout3.addWidget(self.labelAbout, 0, 0, 1, 1)
        self.tabWidget.addTab(self.tabVersion, "")

        self.tabLicense = QWidget()
        self.gridLayout2 = QGridLayout(self.tabLicense)
        self.browserLicense = QTextBrowser(self.tabLicense)
        self.gridLayout2.addWidget(self.browserLicense, 0, 0, 1, 1)
        self.tabWidget.addTab(self.tabLicense, "")

        self.verticalLayout.addWidget(self.tabWidget)
        self.buttonBox = QDialogButtonBox(self)
        self.buttonBox.setStandardButtons(QDialogButtonBox.Close)
        self.buttonBox.button(QDialogButtonBox.Close).setText(self.tr("Close"))
        self.buttonBox.button(QDialogButtonBox.Close).clicked.connect(self.reject)
        self.verticalLayout.addWidget(self.buttonBox)
        self.buttonBox.accepted.connect(self.reject)
        self.setWindowTitle(self.tr("About"))

        import os.path as os
        with open(os.join(os.dirname(os.dirname(__file__)), "LICENSE"),"r") as license:
            self.browserLicense.setText(license.read())

        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabVersion), self.tr("Version"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabLicense), self.tr("License"))
        self.labelAbout.setText(self.tr("""

{0}

Version: {1}

{0}, cross platform, free and free is a RSS/Atom reader.

PyQt5(Qt5), Python3.4, BeautifulSoup4, feedparser and sqlite3 was created with.

License: Gpl v3

Copyright \u00a9 2015 Metehan Özbek -
            metehan.us  -

""").format(app.applicationName(), app.applicationVersion()))

        self.labelAbout.linkActivated.connect(self.openUrl)

    def openUrl(self, url):
        QDesktopServices.openUrl(QUrl(url))


# ---------------------------------- PROBLEM 8 (HARD) --------------------------------------#
# Min Number Of Jumps

# You are given a non-empty array of integers. Each element represents the maximum number of steps
# you can take forward. For example, if the element at index 1 is 3, you can go from index 1 to
# index 2, 3, or 4. Write a function that returns the minimum number of jumps needed to reach the
# final index. Note that jumping from index i to index i + x always constitutes 1 jump, no matter
# how large x is.

# Sample input: [3, 4, 2, 1, 2, 3, 7, 1, 1, 1, 3]
# Sample output: 4 (3 --> 4 or 2 --> 2 or 3 --> 7 --> 3)

# ----------------METHOD 01---------------------#
# COMPLEXITY = TIME: O(n^2), SPACE: O(n)
def minNumberOfJumps(lst):
	jumps = [float("inf")] * len(lst)
	jumps[0] = 0

	for i in range(1, len(lst)):
		for j in range(i):
			if lst[j] + j >= i:
				jumps[i] = min(jumps[i], jumps[j] + 1)

	return jumps[-1]
# ----------------METHOD 01---------------------#

# ----------------METHOD 02---------------------#
# COMPLEXITY = TIME: O(n), SPACE: O(1)
def minNumberOfJumps(lst):
	if len(lst) == 1:
		return 0
	jumps = 0
	maxReach = lst[0]
	steps = lst[0]
	for i in range(1, len(lst) - 1):
		maxReach = max(maxReach, lst[i] + i)
		steps -= 1
		if steps == 0:
			jumps += 1
			steps = maxReach - i
	return jumps + 1
# ----------------METHOD 02---------------------#


# -*- coding: utf-8 -*-
"""
Created on Fri Sep 10 16:48:16 2021

@author: dript
"""

'''
This file will be triggered to pull API data and Google Sheet data
and check if any new date there or not. Then merge new date if
available.

'''

import pandas as pd
from .api_data_parser import getApiData
from .sheet_data_parser import getSheetData
from datetime import datetime
import logging

class Updater():
    def __init__(self, config_path):
        self.config = config_path

    def apiData(self):
        """This pull the API data from https://data.covid19bharat.org/v4/min/timeseries.min.json

        Returns:
            dict: Gives a Dict in following format
            '{
              "confirmed":DataFrame,
              "recovered":DataFrame,
              "deceased":DataFrame
            }'
        """
        data = getApiData()
        return data()

    def sheetData(self):
        """This pull the Google Sheet data

        Returns:
            dict: Gives a Dict in following format
            '{
              "confirmed":DataFrame,
              "recovered":DataFrame,
              "deceased":DataFrame
            }'
        """
        data, spread_obj = getSheetData().pull_data_sheet(self.config)
        return data, spread_obj

    def logger(self, massage):
        """Helper function to log any massage

        Args:
            massage (str): The massage that has to be logged
        """
        logging.basicConfig(filename="stdOut.log",
                            format='%(asctime)s %(message)s',
                            filemode='a',
                            level=logging.INFO)

        logging.info(massage)

    def Validator(self, new_frame, key):
        """Validate the data whether current date values
            are greater than previous data values or not

        Args:
            new_frame (DataFrame): The new data with new dates
            key (str): A value among ["confirmed","deceased","recovered"]

        Returns:
            bool: If any new date value less than previous data returns False.
        """
        new_frame = new_frame.set_index("CODE")
        new_frame = new_frame.astype(int)

        def getName(d1, d2):
            tally = new_frame[d1] <= new_frame[d2]
            name = tally[tally == False].index[0]
            return name
        haserror = False
        col_list = new_frame.columns
        for idx, val in enumerate(col_list):
            if idx > 0:
                if (new_frame[col_list[idx - 1]] <= new_frame[col_list[idx]]).all() != True:
                    haserror = True
                    print(f"Data integrity error found for {key} check 'stdOut.log' file.")
                    self.logger(
                        f"Data Integrity Error b/w {col_list[idx - 1]} and {col_list[idx]} at {getName(col_list[idx - 1],col_list[idx])} for updating {key}")
        if haserror == True:
            return True    # Toggle this to False if don't want to update when value is less than previous day
        return True

    def prepare_for_merge(self, api_data, sheet_data, key):
        """Prepare the new data for merging with old data.

        Args:
            api_data (DataFrame): The data pull from API
            sheet_data (DataFrame): The data pulled from Google Sheet
            key (str): A value among ["confirmed","deceased","recovered"]

        Raises:
            Exception: If last updated date is not specified in API data then
            it raises an exception.

        Returns:
            DataFrame: If new date is available the new merged data will be returned.
            Otherwise old data is returned.
        """
        sheet_cols_last = sheet_data.columns[-1]
        sheet_cols_last = datetime.strptime(sheet_cols_last, "%m/%d/%Y")
        #print(api_data.columns)
        if sheet_cols_last in api_data.columns:
            idx = list(api_data.columns).index(sheet_cols_last)
            if len(list(api_data.columns)[idx:]) > 1:
                new_col = api_data.iloc[:, idx+1:].astype(int)
                new_col.columns = [i.strftime("%m/%d/%Y")
                                   for i in new_col.columns]
                new_col["CODE"] = api_data["CODE"]
                mer = pd.merge(sheet_data[["CODE",sheet_data.columns[-1]]], new_col, on="CODE", how='left')
                if self.Validator(mer, key):
                    mer = pd.merge(sheet_data, new_col, on="CODE", how='left')
                    return mer.dropna(axis=0)
                else:
                    return sheet_data
            else:
                return sheet_data
        else:
            raise Exception("Something Wrong Happended!!")

    def __call__(self):
        print("Fetching API data....")
        api = self.apiData()
        print("Fetching API data done.")
        print("Fetching Google Sheet data....")
        sheet, objs = self.sheetData()
        print("Fetching Google Sheet data done.")

        print("Performing Update of Google Sheet...")
        res_dict = {}
        for api_data, sheet_data in zip(api, sheet):
            final_data = self.prepare_for_merge(
                api[api_data], sheet[sheet_data], key=api_data)
            if final_data.shape[1] > sheet[sheet_data].shape[1]:
                res_dict[api_data] = final_data
                obj = objs[api_data]
                print(f"Updating {api_data} Google Sheet...")
                obj.df_to_sheet(final_data, index=False, replace=True)
            else:
                print(
                    f"Updating {api_data} Google Sheet is not required(already up-to-date)...")
            res_dict[api_data] = final_data
        print("Google Sheet Updating Done..")
        return res_dict


from fastapi import APIRouter,Depends
from sqlalchemy.orm import Session

from core.config import get_db

router=APIRouter(
    prefix='/items',
    tags=['Items']
)

@router.get('/')
def get_user(db:Session =Depends(get_db)):
    return 'Items'


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

# @author: x.huang
# @date:09/04/18
import logging
import time

import tornado
from tornado import httputil
from tornado import ioloop, gen
from tornado import options
from tornado import web
from tornado.options import define, options, parse_command_line

UPLOAD_DIR = 'public'
define("port", default=8181, help="Run server on a specific port", type=int)

def save_file(file: httputil.HTTPFile, file_path):
    with open(file_path, 'wb+') as f:
        f.write(file.body)

class UploadHandler(web.RequestHandler):
    @gen.coroutine
    def post(self, *args, **kwargs):
        file = self.request.files.get('file')[0]  # type: httputil.HTTPFile
        now = time.time()
        file_path = "%s/%s_%s" % (UPLOAD_DIR, file.filename, now)
        save_file(file, file_path)
        self.write("%s upload success." % file_path)
        self.finish()

class HelloHandler(web.RequestHandler):
    @gen.coroutine
    def get(self):
        self.write({
            'msg': 'success'

        })
        self.finish()

class Application(web.Application):

    def __init__(self):
        handler_list = [
            (r'/upload', UploadHandler),
            (r'/hello', HelloHandler),
        ]
        settings = dict(
            debug=True,

        )
        web.Application.__init__(self, handler_list, **settings)

def start_server():
    options.logging = 'info'
    logging.basicConfig(level=logging.INFO)
    app = Application()
    parse_command_line()
    app.listen(options.port)

    loop = ioloop.IOLoop().current()
    try:
        logging.info('Tornado version {}'.format(tornado.version))
        logging.info('Tornado Server Start, listen on {}'.format(options.port))
        logging.info('Quite the Server with CONTROL-C.')
        loop.start()
    except KeyboardInterrupt:
        loop.close()

if __name__ == '__main__':
    start_server()


from MainWindow import MainWindow
from PyQt5.QtWidgets import QApplication

import os

folder_path = 'Output'
if not os.path.exists(folder_path):
    os.makedirs(folder_path)

app = QApplication([])

win = MainWindow()
win.show()

app.exec()


# -*- coding: utf-8 -*-
import csv
import nltk
import pdb
import pickle
from nltk.stem import WordNetLemmatizer

def addBigram(cntBigram,strBigram):
    if strBigram in cntBigram:
        cntBigram[strBigram]=cntBigram[strBigram]+1
    else:
        cntBigram[strBigram]=1
    return cntBigram

lemmatizer = WordNetLemmatizer()

# WRITE FILE
with open("final.txt","w",encoding='utf_8') as f: #open an output file
    # READ CSV FILE for sentences with wrong collocation (Wci) error tag
    filename = "wci.csv"
    #dictWrgVbNn={}
    with open(filename,'r', newline='') as csvfile:
        csvreader = csv.reader(csvfile,delimiter='\t')
        for line in csvreader:
            p1=int(line[2]) # the start position of the marked error
            p2=int(line[3]) # the end position of the marked error
            sentence=line[5]
            # split the sentence into tokens and add pos tags
            words=sentence.split(' ')
            tagged_words = nltk.pos_tag(words)
            # find Verb-Noun Bigram Wrong collocation
            flagVerb=False
            flagNoun=False
            for word_pos in tagged_words[p1:p2]:
                if word_pos[1].startswith('V'): # if there is a Verb within the marked error
                    lemma0=lemmatizer.lemmatize(word_pos[0].lower(),'v')
                    word0=word_pos[0].lower()
                    if lemma0 == 'be': # exclude 'be'
                        lemma0=[]
                        word0=[]
                        flagVerb=False
                        #pdb.set_trace()
                    else:
                        flagVerb=True
                elif (flagVerb and word_pos[0].isalpha() and word_pos[1].startswith('N')): # find the Noun after the Verb
                    flagNoun=True
                    lemma1=lemmatizer.lemmatize(word_pos[0].lower(),'n')
                    word1=word_pos[0].lower()
                    break

            if flagVerb:
                if flagNoun: # Have found a Noun in the marked error
                    #dictWrgVbNn=addBigram(dictWrgVbNn,lemma0+' '+lemma1)
                    #pdb.set_trace()
                    f.write(word0+' '+word1+'\n')
                else: # find a Noun in the following words until reach the end of sentence
                    for word_pos in tagged_words[p2:]:
                        if (word_pos[0].isalpha() and word_pos[1].startswith('N')):
                            flagNoun=True
                            lemma1=lemmatizer.lemmatize(word_pos[0].lower(),'n')
                            word1=word_pos[0].lower()
                            #dictWrgVbNn=addBigram(dictWrgVbNn,lemma0+' '+lemma1)
                            #pdb.set_trace()
                            f.write(word0+' '+word1+'\n')
                            break

# WRITE FILE
##f = open("final.txt","w",encoding='utf_8') #open an output file
##for ngram in dictWrgVbNn.keys(): #iterate over each ngram
##    words=ngram.split(' ')
##    for word in words:
##       #pdb.set_trace()
##       f.write(word)
##       f.write("\t")
##    f.write(str(dictWrgVbNn[ngram])) #then print the score
##    f.write("\n") #print a new line
##f.close() #close the output file
##
##    # open a file to store the data
##file = open('dictWrgVbNn', 'wb')
##data=dictWrgVbNn
##pickle.dump(data, file)
##file.close()


import numpy as np
from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler
import pandas as pd
from math import sqrt
import matplotlib.pyplot as plt
import os
from scipy.spatial.distance import pdist

df = pd.read_csv("Final-data.csv")
scaler = MinMaxScaler()
df_scaled = scaler.fit_transform(df)
k = 3
kmeans = KMeans(n_clusters=k)
kmeans.fit(df_scaled)

df['cluster'] = kmeans.predict(df_scaled)

def euclidean_distance(point1, point2):
    distance = 0
    for i in range(len(point1)):
        distance += (point1[i] - point2[i]) ** 2
    return sqrt(distance)

# pdist kullanılmadan yapılmış hali. // Not: pdist daha hızlı optimize ediyor.
"""
def calculate_dunn_index(data, kmeans):
    cluster_distances = pd.DataFrame(columns=range(kmeans.n_clusters), index=range(kmeans.n_clusters))
    for i in range(kmeans.n_clusters):
        for j in range(kmeans.n_clusters):
            if i != j:
                cluster_distances.iloc[i, j] = np.linalg.norm(kmeans.cluster_centers_[i] - kmeans.cluster_centers_[j])

    min_cluster_distances = cluster_distances.min(axis=1)
    max_intra_cluster_distances = data.groupby('cluster').apply(lambda x: x.apply(np.linalg.norm, axis=1).max())

    dunn_index = min_cluster_distances.min() / max_intra_cluster_distances.max()

    return dunn_index
"""

def calculate_dunn_index(data, kmeans):
    cluster_distances = pd.DataFrame(columns=range(kmeans.n_clusters), index=range(kmeans.n_clusters))
    for i in range(kmeans.n_clusters):
        for j in range(kmeans.n_clusters):
            if i != j:
                cluster_distances.iloc[i, j] = np.linalg.norm(kmeans.cluster_centers_[i] - kmeans.cluster_centers_[j])

    min_cluster_distances = cluster_distances.min(axis=1)

    max_intra_cluster_distances = []
    for i in range(kmeans.n_clusters):
        cluster_data = data[data['cluster'] == i]
        if not cluster_data.empty:
            distances = pdist(cluster_data.values, metric='euclidean')
            max_intra_cluster_distances.append(max(distances))

    dunn_index = min_cluster_distances.min() / max(max_intra_cluster_distances)

    return dunn_index

def calculate_bcss(kmeans, data):
    mean = data.mean().values

    bcss = 0
    for i in range(kmeans.n_clusters):
        cluster_mean = kmeans.cluster_centers_[i]
        bcss += len(data[data['cluster'] == i]) * euclidean_distance(mean, cluster_mean) ** 2
    return bcss

normalized_df = (df - df.min()) / (df.max() - df.min())

x_column = 'a3'
y_column = 'a4'

fig, ax = plt.subplots(figsize=(8, 6))
kmeans = KMeans(n_clusters=3).fit(normalized_df)
df['cluster'] = kmeans.labels_

colors = ['r', 'g', 'b']
for cluster in range(kmeans.n_clusters):
    cluster_data = df[df['cluster'] == cluster]
    ax.scatter(cluster_data[x_column], cluster_data[y_column], label=f'Cluster {cluster}', marker='o', c=colors[cluster], s=50)

ax.legend(title='Cluster')
ax.grid(True)
ax.set_xlabel(x_column)
ax.set_ylabel(y_column)
ax.set_title('Clustering Results')

plt.savefig('plot.png')
plt.show()

wcss = kmeans.inertia_
bcss = calculate_bcss(kmeans, normalized_df)
dunn_index = calculate_dunn_index(df, kmeans)

for result_file in ['result_en.txt', 'result_tr.txt']:
    if os.path.exists(result_file):
        os.remove(result_file)

with open('result_en.txt', 'w') as f:
    for i, row in df.iterrows():
        f.write(f'Record {i+1}:\n')
        f.write(f'    Cluster {row["cluster"]}\n')

    for i in range(kmeans.n_clusters):
        cluster_size = len(df[df['cluster'] == i])
        f.write(f'Cluster {i}: {cluster_size} records\n')

    f.write(f'WCSS: {wcss:.2f}\n')
    f.write(f'BCSS: {bcss:.2f}\n')
    f.write(f'Dunn Index: {dunn_index:.2f}\n')

with open('result_tr.txt', 'w') as f:
    for i, row in df.iterrows():
        f.write(f'Kayit {i+1}:\n')
        f.write(f'    Kume {row["cluster"]}\n')

    for i in range(kmeans.n_clusters):
        cluster_size = len(df[df['cluster'] == i])
        f.write(f'Kume {i}: {cluster_size} kayit\n')

    f.write(f'WCSS: {wcss:.2f}\n')
    f.write(f'BCSS: {bcss:.2f}\n')
    f.write(f'Dunn Index: {dunn_index:.2f}\n')


import os
import sys

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets, uic
from PIL import Image, ImageTk
import tkinter as tk
import subprocess
options = ["Web", "Local" , "QnA"]

def on_button(option):
	print(option)
	if option == "Local":
		query = searchBox.get()
		commandList = []
	#if tokens[0] in ['file','document']:

		command = "sudo find /home -type f -name '" + query + "'"
		ans = subprocess.check_output(command, shell=True);
		command = str(ans,'utf-8').rstrip()
		commands = command.split("\n")
		for i in commands:
			commandList.append(i)
		print("in here")
		#os.system(command)
		#if tokens[0] in ['directory','folder']:
		command = "sudo find /home -type d -name '" + query + "'"
		#os.system(command)
		ans = subprocess.check_output(command, shell=True);
		#print(ans)
		command = str(ans,'utf-8').rstrip()
		commands = command.split("\n")
		for i in commands:
			commandList.append(i)
		#print(commands)
		for command in commandList:
			print(command)
			if len(command) > 0:
				os.system("gnome-open " + command)
		pass
		"""GAURAV FUNCTION"""

	elif option == "Web":
		query = searchBox.get().replace(" ", "+")
		app = QtWidgets.QApplication(sys.argv)
		path_ui = os.path.join(os.path.dirname(__file__), "test.ui")
		window = uic.loadUi(path_ui)
		window.widget.load(QtCore.QUrl("https://www.duckduckgo.com/?q="+ query ))
		window.show()
		app.exec_()
		window.destroy()
		app.destroy()

	elif option == "QnA":
		pass
		"""Shrinivas FUNCTION"""

def on_search():
	print(searchBox.get())

master = tk.Tk()
master.title('Sigma Σ')
master.geometry("%dx%d" % (1000, 500))

b= tk.Button(master,command = on_search)
#img = Image.open("sigma1.png")
photo= tk.PhotoImage(file="sigma1.png")
b.config(width="30",height="30")
b.pack()

searchBox = tk.Entry(master, textvariable = "Type here to search! ",width = 60)
searchBox.pack()

for i,option in enumerate(options):
	B= tk.Button(master, text = option, command = lambda option=option:on_button(option))
	B.pack()
master.mainloop()


from torch import nn

leaf_types = (
    nn.Conv1d,
    nn.Conv2d,
    nn.Conv3d,
    nn.Linear,
    nn.Identity,

    nn.PReLU,
    nn.ReLU,
    nn.ReLU6,
    nn.RReLU,
    nn.SELU,
    nn.CELU,
    nn.GELU,
    nn.Sigmoid,
    nn.SiLU,
    nn.Mish,
    nn.Threshold,
    nn.GLU,
    nn.Hardsigmoid,
    nn.Hardtanh,
    nn.Hardswish,
    nn.LeakyReLU,
    nn.LogSigmoid,

    nn.MaxPool1d,
    nn.MaxPool2d,
    nn.MaxPool3d,
    nn.AdaptiveMaxPool1d,
    nn.AdaptiveMaxPool2d,
    nn.AdaptiveMaxPool3d,

    nn.AvgPool1d,
    nn.AvgPool2d,
    nn.AvgPool3d,
    nn.AdaptiveAvgPool1d,
    nn.AdaptiveAvgPool2d,
    nn.AdaptiveAvgPool3d,

    nn.BatchNorm1d,
    nn.BatchNorm2d,
    nn.BatchNorm3d,
    nn.GroupNorm,
    nn.SyncBatchNorm,
    nn.LayerNorm,

    nn.Dropout,
    nn.Embedding,
    nn.Softmax,
    nn.Softmax2d,
    nn.LogSoftmax,
)

def get_other_info(module):
    # If a handler function exists for the module's type, call it
    extra_rep = ''
    if isinstance(module, leaf_types):
        extra_rep = module.extra_repr()
        if extra_rep:
            return '(' + extra_rep + ')'

    # If no specific handler, return an empty string
    return extra_rep


#Manejo Excepciones
import sys

try:
    f = open("miArchivo.txt")
    s = f.readline()
    i = int(s.strip())
except OSError as err:
    print("Error OS: {0}".format(err))
except ValueError:
    print("No puede convertir el dato en un entero")
except:
    print("Error inesperado: ", sys.exc_info() [0])
    raise


##_Pico W weather sation with Mongodb and BMP280 sensor
##_by ArturCr
##_10/09/2022

##_Libraries
import machine
import urequests as requests
import network
import rp2
import time
##_Library required for bme280
import bme280

##_A file called secrets.py is required with the following data...
from secrets import secrets as s
SSID = s['ssid']
PSK = s['pw']
COUNTRY = s['country']
API_KEY = s['api-key']
URL_ENDPOINT = s['url-endpoint']
CLUSTER = s['cluster']
DATABASE = s['database']
COLLECTION = s['collection']
STATION_NAME  = "Pico_Station_0"

##_Led pattern on powerup
led = machine.Pin('LED', machine.Pin.OUT)
led.toggle()
time.sleep(.75)
led.off()

##_Provides time to avoid reset loops but allows for clean network reconnection
def reset_protocol():
    for x in range(50):
        led.toggle()
        time.sleep(.2)
    led.on()
    time.sleep(1)
    machine.reset()

##_Configure the Wifi card to use the frequencies available for that geography
rp2.country(COUNTRY)

##_Connects to wifi using the provided credentials otherwise reboot pico
def connect_to_wifi(ssid, psk):
    ##_Enable Wifi in Client Mode
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    ##_Connect to Wifi, keep trying until failure or success
    wlan.connect(ssid, psk)

    while not wlan.isconnected() and wlan.status() >= 0:
        print("Waiting to Connect")
        time.sleep(5)
    if not wlan.isconnected():
        #raise Exception("Wifi not available")
        print("Wifi not available")
        reset_protocol()
    print("Connected to WiFi")

##_Discconect from wifi to safe on power
def disconnect_to_wifi():
    ##_Stop the Wifi
    try:
        wlan = network.WLAN(network.STA_IF)
        wlan.disconnect()
        wlan.active(False)
    except Exception as e:
        print('try 2: ',e)

##_Get weather data from bme280 sensor
Data_to_send=[]
def get_bme280_readings():
    data_to_send=[]

    ##_Power on sensor
    power = machine.Pin(2, machine.Pin.OUT)
    power.on()
    time.sleep(1)

    ##_Commuticate over I2C
    i2c = machine.I2C(0,scl=machine.Pin(1), sda=machine.Pin(0))
    bme = bme280.BME280(i2c=i2c)

    for x in range (6):
        print(bme.values)
        ##Temperature(C) Pressure(-1000) and Humidity(%)
        data_to_send.append({'Temperature':float(bme.values[0][:-1]),'Pressure':float(bme.values[1][:-3])-1000.0,'Humidity':float(bme.values[2][:-1]),'timestamp':formated_datetime()})
        time.sleep(2)

    print(data_to_send)
    return data_to_send

##_Real time clock
rtc = machine.RTC()
def set_RTC():
    #connect_to_wifi(SSID, PSK)
    ##_Make GET request to server with time data
    while True:
        try:
            ##Getting time data from "http://date.jsontest.com"
            r = requests.get("http://date.jsontest.com", timeout=10)
            break
        except:
            print('.',end='')
            time.sleep(1)

    date_and_time = r.json()
    r.close()

    print(date_and_time)

    H24_clock_offset=0
    if date_and_time['time'][9:]=='PM':
        H24_clock_offset=12
    hour=int(date_and_time['time'][:2])+H24_clock_offset

    rtc.datetime((int(date_and_time['date'][6:]),
                  int(date_and_time['date'][:2]),
                  int(date_and_time['date'][3:5]),
                  0,
                  int(hour),
                  int(date_and_time['time'][3:5]),
                  int(date_and_time['time'][6:8]),
                  1))
    #disconnect_to_wifi()

##_Reformat data for the database to be able to read it
def formated_datetime():
    a=rtc.datetime()
    return str(a[0])+'-'+str(a[1])+'-'+str(a[2])+'T'+str(a[4])+':'+str(a[5])+':'+str(a[6])

def send_data_out():
    #connect_to_wifi(SSID, PSK)

    url = URL_ENDPOINT + "/action/insertOne"
    headers = { "api-key": API_KEY}

    documentToAdd = {"device": STATION_NAME , "v": Data_to_send}

    insertPayload = {
        "dataSource": CLUSTER,
        "database": DATABASE,
        "collection": COLLECTION,
        "document": documentToAdd,
    }

    print("sending...")

    response = requests.post(url, headers=headers, json=insertPayload, timeout=10)

    print("Response: (" + str(response.status_code) + "), msg = " + str(response.text))

    if response.status_code == 201:
        print("Added Successfully")
        led.toggle()
        time.sleep(.25)
        led.toggle()
    else:
        print("Error")

    ##_Always close response objects so we don't leak memory
    response.close()
    time.sleep(.01)

##_Main Loop
try:
    connect_to_wifi(SSID, PSK)

    set_RTC()

    print(formated_datetime())

    Data_to_send=get_bme280_readings()

    send_data_out()

    disconnect_to_wifi()

except Exception as e:
    print('Main code loop error: ',e)

disconnect_to_wifi()
print("Entering deepsleep 60 sec")
machine.deepsleep(60*1000)#Deepsleep for x secounds


import sys
import logging
from telegram import Update
from future.utils import bytes_to_native_str
from threading import Lock
try:
    import ujson as json
except ImportError:
    import json
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
import tornado.web

logging.getLogger(__name__).addHandler(logging.NullHandler())

class WebhookServer(object):

    def __init__(self, listen, port, webhook_app, ssl_ctx):
        self.http_server = HTTPServer(webhook_app, ssl_options=ssl_ctx)
        self.listen = listen
        self.port = port
        self.loop = None
        self.logger = logging.getLogger(__name__)
        self.is_running = False
        self.server_lock = Lock()
        self.shutdown_lock = Lock()

    def serve_forever(self):
        with self.server_lock:
            IOLoop().make_current()
            self.is_running = True
            self.logger.debug('Webhook Server started.')
            self.http_server.listen(self.port, address=self.listen)
            self.loop = IOLoop.current()
            self.loop.start()
            self.logger.debug('Webhook Server stopped.')
            self.is_running = False

    def shutdown(self):
        with self.shutdown_lock:
            if not self.is_running:
                self.logger.warning('Webhook Server already stopped.')
                return
            else:
                self.loop.add_callback(self.loop.stop)

    def handle_error(self, request, client_address):
        """Handle an error gracefully."""
        self.logger.debug('Exception happened during processing of request from %s',
                          client_address, exc_info=True)

class WebhookAppClass(tornado.web.Application):

    def __init__(self, webhook_path, bot, update_queue, default_quote=None):
        self.shared_objects = {"bot": bot, "update_queue": update_queue,
                               "default_quote": default_quote}
        handlers = [
            (r"{0}/?".format(webhook_path), WebhookHandler,
             self.shared_objects)
            ]  # noqa
        tornado.web.Application.__init__(self, handlers)

    def log_request(self, handler):
        pass

# WebhookHandler, process webhook calls
class WebhookHandler(tornado.web.RequestHandler):
    SUPPORTED_METHODS = ["POST"]

    def __init__(self, application, request, **kwargs):
        super(WebhookHandler, self).__init__(application, request, **kwargs)
        self.logger = logging.getLogger(__name__)
        self._init_asyncio_patch()

    def _init_asyncio_patch(self):
        """set default asyncio policy to be compatible with tornado
        Tornado 6 (at least) is not compatible with the default
        asyncio implementation on Windows
        Pick the older SelectorEventLoopPolicy on Windows
        if the known-incompatible default policy is in use.
        do this as early as possible to make it a low priority and overrideable
        ref: https://github.com/tornadoweb/tornado/issues/2608
        TODO: if/when tornado supports the defaults in asyncio,
                remove and bump tornado requirement for py38
        Copied from https://github.com/ipython/ipykernel/pull/456/
        """
        if sys.platform.startswith("win") and sys.version_info >= (3, 8):
            import asyncio
            try:
                from asyncio import (
                    WindowsProactorEventLoopPolicy,
                    WindowsSelectorEventLoopPolicy,
                )
            except ImportError:
                pass
                # not affected
            else:
                if isinstance(asyncio.get_event_loop_policy(), WindowsProactorEventLoopPolicy):
                    # WindowsProactorEventLoopPolicy is not compatible with tornado 6
                    # fallback to the pre-3.8 default of Selector
                    asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())

    def initialize(self, bot, update_queue, default_quote=None):
        self.bot = bot
        self.update_queue = update_queue
        self._default_quote = default_quote

    def set_default_headers(self):
        self.set_header("Content-Type", 'application/json; charset="utf-8"')

    def post(self):
        self.logger.debug('Webhook triggered')
        self._validate_post()
        json_string = bytes_to_native_str(self.request.body)
        data = json.loads(json_string)
        self.set_status(200)
        self.logger.debug('Webhook received data: ' + json_string)
        data['default_quote'] = self._default_quote
        update = Update.de_json(data, self.bot)
        self.logger.debug('Received Update with ID %d on Webhook' % update.update_id)
        self.update_queue.put(update)

    def _validate_post(self):
        ct_header = self.request.headers.get("Content-Type", None)
        if ct_header != 'application/json':
            raise tornado.web.HTTPError(403)

    def write_error(self, status_code, **kwargs):
        """Log an arbitrary message.

        This is used by all other logging functions.

        It overrides ``BaseHTTPRequestHandler.log_message``, which logs to ``sys.stderr``.

        The first argument, FORMAT, is a format string for the message to be logged.  If the format
        string contains any % escapes requiring parameters, they should be specified as subsequent
        arguments (it's just like printf!).

        The client ip is prefixed to every message.

        """
        super(WebhookHandler, self).write_error(status_code, **kwargs)
        self.logger.debug("%s - - %s" % (self.request.remote_ip, "Exception in WebhookHandler"),
                          exc_info=kwargs['exc_info'])


from django.http import HttpResponse
from django.shortcuts import render
import operator

def home(request):
    return render(request,'home.html',{'hi': '12' })
def count(request):
    fulltext = request.GET['fulltext']
    print(fulltext)
    wordlist = fulltext.split()

    worddic = {}
    for word in wordlist:
        if word in worddic:
            worddic[word] += 1
        else:
            worddic[word] = 1
    maxv = 0
    for word in worddic:
        if maxv < worddic[word]:
            maxv = worddic[word]
    sortedic = sorted(worddic.items(),key=operator.itemgetter(1), reverse = True)
    return render(request,'count.html',{'fulltext':fulltext,'count':len(wordlist),'sortedic':sortedic,'max':maxv})


from flask import Flask, render_template, request, make_response, send_from_directory, redirect, jsonify
import random
import string
import os
import time

import multiprocessing as mp

def uuid(n=20):
  return ''.join(random.sample(string.ascii_letters + string.digits, n))
app = Flask(__name__)

ROOT = os.getcwd()
SHAREROOT = os.path.join(ROOT, "shared")

def add_removal(did, internal = 15*60, dir_ = "remove.txt"):
  with open(os.path.join(ROOT, dir_),"a", encoding="utf-8") as f:
    f.write("{} {}\n".format(did, time.time()+internal))

class Share(object):
  def __init__(self, did, internal = 15*60):
    self.did = did
    self.didRoot = os.path.join(SHAREROOT, did)
    if not os.path.isdir(self.didRoot):
      os.mkdir(self.didRoot)
    self.txtFile = os.path.join(self.didRoot, "temp.txt")

  def writeFiles(self, files):
    with open(self.txtFile,"a", encoding="utf-8") as f:
      for file_ in files:
        file_.save("{}/{}".format(self.didRoot, file_.filename))
        f.write("{} {}\n".format(file_.filename, "/download/{}/{}".format(self.did, file_.filename)))

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html', title="Home", home_status="active")

@app.route('/upload')
def upload_file():
   return render_template('upload.html', title="Upload", configer=True, upload_status = "active")

@app.route('/download')
def download():
   return render_template('download.html', title="Download", download_status = "active")

@app.route('/list_from_code',methods=['GET',"POST"])
def list_from_code():
  if request.method == 'POST':
    did = request.form.get('code')
    return redirect("/listfiles/"+did)
  return render_template('download.html', title="Download",download_status = "active")

@app.route('/listfiles/<did>')
def listfiles(did):
  if not os.path.isfile(SHAREROOT+"/{}/temp.txt".format(did)):
    return render_template('download.html', title="Download", download_status = "active", alertMSG = "Code NOT Valid")
  files=[]
  with open(SHAREROOT+"/{}/temp.txt".format(did),"r", encoding="utf-8") as f:
    for line in f.readlines():
      name, url = line.split(" ")
      files.append((name.strip(),url.strip()))
  return render_template('filelist.html', title="Download", download_status="active", data = {"files":files})

@app.route("/download/<did>/<filename>", methods=['GET'])
def download_file(did,filename):
    response = make_response(send_from_directory(os.path.join(SHAREROOT,did), filename, as_attachment=True))
    response.headers["Content-Disposition"] = "attachment; filename={}".format(filename.encode().decode('latin-1'))
    return response

@app.route("/configer", methods=["POST"])
def configer():
  if request.method == "POST":
    getDid, getTime = request.form.get("did"), request.form.get("time")
    if not getTime:
      getTime = 15
    getTime = int(float(getTime))
    if not getDid:
      getDid = uuid()
    while os.path.isdir(SHAREROOT+"/"+getDid):
        getDid = getDid+uuid(4)
    add_removal(getDid, getTime*60)
    return render_template('upload.html', title="Upload", upload=True, upload_status = "active", didUrl=getDid)
  return render_template('upload.html', title="Upload", configer=True, upload_status = "active")

@app.route('/uploader', methods=['GET', 'POST'])
def uploader():
  if request.method == 'POST':
    try:
      getDid = request.form.get("did")
      share = Share(getDid)
      filenames = request.files.getlist('file')
      share.writeFiles(filenames)
      res = jsonify({"imgUrl": ",".join([x.filename for x in filenames]), "didUrl":getDid, "msg":"success"})
      return res
    except Exception as e:
      return str(e)
  return render_template('upload.html', title="Upload", upload=True, upload_status = "active")

if __name__ == '__main__':
  app.run(host='127.0.0.1', port=8080, debug=True, threaded=True)


from chromadb.errors import NoIndexException

from pilot.scene.base_chat import BaseChat, logger, headers
from pilot.scene.base import ChatScene
from pilot.common.sql_database import Database
from pilot.configs.config import Config

from pilot.common.markdown_text import (
    generate_markdown_table,
    generate_htm_table,
    datas_to_table_html,
)

from pilot.configs.model_config import (
    DATASETS_DIR,
    KNOWLEDGE_UPLOAD_ROOT_PATH,
    LLM_MODEL_CONFIG,
    LOGDIR,
)

from pilot.scene.chat_knowledge.v1.prompt import prompt
from pilot.embedding_engine.embedding_engine import EmbeddingEngine
from pilot.server.knowledge.service import KnowledgeService

CFG = Config()

class ChatKnowledge(BaseChat):
    chat_scene: str = ChatScene.ChatKnowledge.value()

    """Number of results to return from the query"""

    def __init__(self, chat_session_id, user_input, knowledge_space):
        """ """
        super().__init__(
            chat_mode=ChatScene.ChatKnowledge,
            chat_session_id=chat_session_id,
            current_user_input=user_input,
        )
        self.space_context = self.get_space_context(knowledge_space)
        self.top_k = (
            CFG.KNOWLEDGE_SEARCH_TOP_SIZE
            if self.space_context is None
            else int(self.space_context["embedding"]["topk"])
        )
        # self.recall_score = CFG.KNOWLEDGE_SEARCH_TOP_SIZE if self.space_context is None else self.space_context["embedding"]["recall_score"]
        self.max_token = (
            CFG.KNOWLEDGE_SEARCH_MAX_TOKEN
            if self.space_context is None
            else int(self.space_context["prompt"]["max_token"])
        )
        vector_store_config = {
            "vector_store_name": knowledge_space,
            "vector_store_type": CFG.VECTOR_STORE_TYPE,
            "chroma_persist_path": KNOWLEDGE_UPLOAD_ROOT_PATH,
        }
        self.knowledge_embedding_client = EmbeddingEngine(
            model_name=LLM_MODEL_CONFIG[CFG.EMBEDDING_MODEL],
            vector_store_config=vector_store_config,
        )

    def generate_input_values(self):
        try:
            if self.space_context:
                self.prompt_template.template_define = self.space_context["prompt"][
                    "scene"
                ]
                self.prompt_template.template = self.space_context["prompt"]["template"]
            docs = self.knowledge_embedding_client.similar_search(
                self.current_user_input, self.top_k
            )
            context = [d.page_content for d in docs]
            context = context[: self.max_token]
            input_values = {"context": context, "question": self.current_user_input}
        except NoIndexException:
            raise ValueError(
                "you have no knowledge space, please add your knowledge space"
            )
        return input_values

    @property
    def chat_type(self) -> str:
        return ChatScene.ChatKnowledge.value()

    def get_space_context(self, space_name):
        service = KnowledgeService()
        return service.get_space_context(space_name)


#!/usr/bin/env python3.7
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 11 11:14:12 2019

@author: ejreidelbach

:DESCRIPTION:

:REQUIRES:

:TODO:
"""

#==============================================================================
# Package Import
#==============================================================================
import json
import os
import pandas as pd
import pathlib
import tqdm
import urllib.request

#==============================================================================
# Reference Variable Declaration
#==============================================================================

#==============================================================================
# Function Definitions
#==============================================================================
def downloadPictures(dict_url, folder_name):
    '''
    Purpose: Download pictures via the provided url for the image

    Inputs
    ------
        dict_url : dictionary of URLs
            dictionary in which the name of the image is the key and the
            associated value is the URL of the image
        folder_name : string
            name of the folder in which downloaded images should be stored

    Outputs
    -------
        files are saved to a folder named after the input dictionary
    '''
    # Check to see if the desired folder exists and create it if it doesn't
    pathlib.Path('pictures/',folder_name).mkdir(parents=True, exist_ok=True)

    # Download all the images at the associated URLs
    for name, url in tqdm.tqdm(dict_url.items()):
        if not pd.isna(url):
            urllib.request.urlretrieve(url, 'pictures/' + folder_name + '/'
                                       + name + '.jpg')

#==============================================================================
# Working Code
#==============================================================================

# Set the project working directory
path_dir = pathlib.Path('/home/ejreidelbach/Projects/draft-gem/src/static')
os.chdir(path_dir)

#------------------------------------------------------------------------------
# Download Pictures of Schools
#------------------------------------------------------------------------------
# Ingest the listing of schools and their associated pictures
df_pics_school = pd.read_csv('positionData/school_pictures.csv')
df_pics_school.set_index('school', drop=True, inplace=True)

# Convert the dataframe to a dictionary
dict_pics_school = df_pics_school.to_dict('index')
for key, value in dict_pics_school.items():
    dict_pics_school[key] = value['urlSchool']

# Download the pictures
downloadPictures(dict_pics_school, 'School')

#------------------------------------------------------------------------------
# Download Pictures of Quarterbacks
#------------------------------------------------------------------------------
# Ingest the Quarterback data
df_pics_qb = pd.read_csv('positionData/QB/pos_QB_final.csv')

# Rename the `pictureSchool` and `picturePlayer` variables to include URL
df_pics_qb.rename(columns = {'picturePlayer':'picturePlayerURL',
                             'pictureSchool':'pictureSchoolURL'}, inplace=True)

# Create the `pictureSchoolPath` and `picturePlayerPath` variables to map
#   the file location of the associated images (i.e. /positionData/pictures/..)
df_pics_qb['picturePlayerPath'] = df_pics_qb.apply(
        lambda row: '/positionData/pictures/QB/' + row['unique_id'] + '.png'
        if not pd.isnull(row['picturePlayerURL']) else '', axis = 1)
df_pics_qb['pictureSchoolPath'] = df_pics_qb['School'].apply(
        lambda x: '/positionData/pictures/School/' + x + '.png')

# Convert the dataframe to a dictionary
dict_pics_qb = df_pics_qb.to_dict('index')

# Write the updated data (with paths to pictures vice URLs) to a .csv file
#   ('pos_POSITION_final_pics.csv')
df_pics_qb.to_csv('positionData/QB/pos_QB_final_pics.csv', index = False)

# Write the updated data (with paths to pictures vice URLs) to a .json file
#   ('pos_POSITION_final_pics.json')
with open('positionData/QB/pos_QB_final_pics.json', 'wt') as out:
    json.dump(dict_pics_qb, out, sort_keys=True)

# Minimize the QB Dictionary for the purposes of picture scraping
dict_pics_qb_small = {}
for key, value in dict_pics_qb.items():
    dict_pics_qb_small[dict_pics_qb[key]['unique_id']] = value['picturePlayerURL']

# Download the pictures
downloadPictures(dict_pics_qb_small, 'QB')


"""
Unit tests for the conversion of input units
"""
import pytest
import numpy as np

from astropy import units as u

from speccado.source import convert_wav_units, convert_flux_units

class TestWavUnits:
    """Tests of function sc.source.convert_wav_units"""

    @pytest.mark.parametrize("inwav, testwav",
                             [(1.234 * u.nm, 0.001234 * u.um),
                              (98765 * u.angstrom, 9.8765 * u.um),
                              (3.24 * u.um, 3.24 * u.um),
                              (3.42e-06 * u.m, 3.42 * u.um)])
    def test_wavelength(self, inwav, testwav):
        """Conversion of various wavelength units"""
        outwav = convert_wav_units(inwav)
        assert np.isclose(outwav.value, testwav.value)

    @pytest.mark.parametrize("inwav, testwav",
                             [(8103727.71474878 * u.cm**(-1), 0.001234 * u.um),
                              (101250.44297069 * u.m**(-1), 9.8765 * u.um),
                              (0.30864198 * u.um**(-1), 3.24 * u.um),
                              (292.39766082 * u.mm**(-1), 3.42 * u.um)])
    def test_wavenumber(self, inwav, testwav):
        """Conversion of wavenumber to wavelength"""
        outwav = convert_wav_units(inwav)
        assert np.isclose(outwav.value, testwav.value)

    @pytest.mark.parametrize("inwav, testwav",
                             [(2.42943645e+05 * u.THz, 0.001234* u.um),
                              (3.03541192e+04 * u.GHz, 9.8765 * u.um),
                              (9.25285364e+07 * u.MHz, 3.24* u.um),
                              (8.76586135e+13 / u.s, 3.42 * u.um)])
    def test_frequency(self, inwav, testwav):
        """Conversion of frequency to wavelength"""
        outwav = convert_wav_units(inwav)
        assert np.isclose(outwav.value, testwav.value)

    @pytest.mark.parametrize("inwav, testwav",
                             [(1.60976161e-09 * u.erg, 0.001234 * u.um),
                              (0.12553455 * u.eV, 9.8765 * u.um),
                              (6.13100563e-20 * u.J, 3.24 * u.um),
                              (0.00036253 * u.keV, 3.42 * u.um)])
    def test_photon_energy(self, inwav, testwav):
        """Conversion of photon energy to wavelength"""
        outwav = convert_wav_units(inwav)
        assert np.isclose(outwav.value, testwav.value)

    def test_incompatible_units(self):
        """Test one case of non-equivalent units"""
        inwav = 2.34 * u.m / u.s
        with pytest.raises(u.UnitConversionError):
            convert_wav_units(inwav)

class TestFluxUnits:
    """Tests of function sc.source,convert_flux_units"""

    def test_src_flam_si(self):
        """
        Conversion of SI input units to internal units, point source
        """
        si_flux = np.array([1, 3.4]) * u.J / u.m**2 / u.m / u.s
        wavelength = np.array([3.1, 4.3]) * u.um

        testflux = np.array([1.56057616e+13,
                             7.35987854e+13]) * u.ph / u.s / u.m**2 / u.um

        outflux = convert_flux_units(si_flux, wavelength)
        assert outflux.unit.is_equivalent(
            si_flux.unit,
            equivalencies=u.spectral_density(wavelength))
        assert np.allclose(outflux.value, testflux.value)

    def test_src_fnu(self):
        """
        Conversion of f_nu to f_lambda
        """
        f_nu = np.array([2.3e+4, 99.4e+4]) * u.erg / u.s / u.m**2 / u.Hz
        wavelength = np.array([3.1, 4.3]) * u.um

        testf_lam = np.array([1.11972176e+30,
                              3.48868619e+31]) * u.ph / u.s / u.m**2 / u.um

        f_lam = convert_flux_units(f_nu, wavelength)

        assert f_lam.unit.is_equivalent(
            f_nu.unit,
            equivalencies=u.spectral_density(wavelength))
        assert np.allclose(f_lam.value, testf_lam.value)

    def test_src_jansky(self):
        """
        Conversion of Jy to internal units, point source

        The test uses a reference wavelength of 3 * u.um.
        """
        jy_flux = np.array([1, 3.4]) * u.Jy
        lam_ref = np.array([3.0, 5.7]) * u.um
        testflux = np.array([5030634.01565452,
                             9002187.18590809]) * u.ph / u.m**2 / u.s / u.um

        outflux = convert_flux_units(jy_flux, wav=lam_ref)

        assert outflux.unit.is_equivalent(
            jy_flux.unit,
            equivalencies=u.spectral_density(wav=lam_ref))
        assert np.allclose(testflux.value, outflux.value)

    def test_bg_flam_per_deg2(self):
        """
        Test a simple conversion of angular scale

        This uses a influx per deg^2
        """
        influx = 2.34 * u.erg / u.s / u.m**2 / u.um / u.deg**2
        lam_ref = 4.2 * u.um

        testflux = 381753.84604285 * u.ph / u.s / u.m**2 / u.um / u.arcsec**2

        outflux = convert_flux_units(influx, lam_ref)
        print(testflux)
        print(outflux)
        assert np.allclose(outflux.value, testflux.value)

    def test_bg_flam_per_rad(self):
        """
        Test a simple conversion of angular scale

        This uses influx per rad^2
        """
        influx = 2.34 * u.erg / u.s / u.m**2 / u.um / u.rad**2
        lam_ref = 4.2 * u.um

        testflux = 116.2888 * u.ph / u.s / u.m**2 / u.um / u.arcsec**2

        outflux = convert_flux_units(influx, lam_ref)
        print(testflux)
        print(outflux)
        assert np.allclose(outflux.value, testflux.value)

    def test_bg_jansky_per_deg2(self):
        """
        Test conversion from Jy/deg^2
        """
        influx = 1.2e-5 * u.Jy / u.deg**2
        lam_ref = 9.2 * u.um

        testflux = 1.51891124e-06 * u.ph / u.s / u.m**2 / u.um / u.arcsec**2

        outflux = convert_flux_units(influx, lam_ref)
        assert np.allclose(outflux.value, testflux.value)


#!/usr/bin/env python3
import pandas as pd
import numpy as np
from math import sqrt
from stats_loader import load_stats
from profile_loader import load_profile
import time as pytime
import argparse
from preprocess import *
import sys

better_than = 0.2  # 1=go fixed if time is less than float, 0.5 if time is half then float, etc.

def load_data_onlystats(path, features=None):
	new_features, d3 = load_data_phase1(path)

	if not features:
		features = new_features

	print(features, file=sys.stderr)
	for f in features:
		if f not in d3.columns.values:
			d3[f] = 0.

	print(d3.loc[:, features].shape, file=sys.stderr)
	return d3.fillna(0), features

def load_data(path, features=None, response=None, boostfail=0):
	d3, features = load_data_onlystats(path, features)

	d3['ratio'] = d3['flo_T'] / d3['fix_T']
	w1 = d3['fix_T'] < (d3['flo_T'] * better_than)
	w2 = d3['fix_T'] < (d3['flo_T'])
	# d3['worth']=w1.astype(int)+w2.astype(int)-1
	# d3['worth']=d3['fix_T'] < (d3['flo_T'])
	d3['worth'] = ((d3['ratio'] - 1.0) / 0.2).astype(int).clip(-1, 1)
	# d3['worth'] = w1

	if boostfail > 0:
		for rowi in d3.index:
			if int(d3.loc[rowi, 'worth']) < 1:
				row = d3.loc[rowi, :]
				for repi in range(0, boostfail):
					cprow = row.copy()
					cprow.name = (cprow.name[0] + '_c' + str(repi), cprow.name[1])
					d3 = d3.append(cprow)

	# print d3['ratio'], d3['worth']
	print('<20%', sum(w1), 'of', len(d3['worth']))
	print('<100%', sum(w2) - sum(w1), 'of', len(d3['worth']))
	print('>100%', len(d3['worth']) - sum(w2), 'of', len(d3['worth']))

	if not response:
		response = ['worth']

	print(d3.loc[:, response].shape, file=sys.stderr)
	return d3.fillna(0), features, response

def split_train_test(d3):
	msk = [n < 0.8 * len(d3) for n in range(0, len(d3))]
	np.random.shuffle(msk)
	train = d3[msk]
	test = d3[[not v for v in msk]]
	return train, test

def do_test_prediction(fitr, test, features, response, oneatatime):
	if not oneatatime:
		benchs = [test.index]
	else:
		benchs = [[x] for x in test.index]
	results, times = [], []
	for bench in benchs:
		res = fitr.score(test.loc[bench, features], np.ravel(test.loc[bench, response]))
		t0 = pytime.time()
		pred = fitr.predict(test.loc[bench, features])
		t1 = pytime.time()
		print('Predict', bench, pred)
		print('Actual', np.ravel(test.loc[bench, response]))
		results += [res]
		times += [t1 - t0]
	return results, times

def train_adaboost(train, features, response):
	from sklearn.ensemble import AdaBoostClassifier
	regr = AdaBoostClassifier(n_estimators=100)
	fitr = regr.fit(train.loc[:, features], np.ravel(train.loc[:, response]))
	return fitr

def train_gradient(train, features, response):
	from sklearn.ensemble import GradientBoostingClassifier
	regr = GradientBoostingClassifier(max_features=int(sqrt(len(features))))
	fitr = regr.fit(train.loc[:, features], np.ravel(train.loc[:, response]))
	return fitr

def train_extree(train, features, response):
	from sklearn.ensemble import ExtraTreesClassifier
	regr = ExtraTreesClassifier(max_features=int(sqrt(len(features))))
	fitr = regr.fit(train.loc[:, features], np.ravel(train.loc[:, response]))
	return fitr

def train_bagging(train, features, response):
	from sklearn.ensemble import BaggingClassifier
	regr = BaggingClassifier(max_features=int(sqrt(len(features))))
	fitr = regr.fit(train.loc[:, features], np.ravel(train.loc[:, response]))
	return fitr

def train_randomforest(train, features, response):
	from sklearn.ensemble import RandomForestClassifier
	regr = RandomForestClassifier(max_features=int(sqrt(len(features))))
	fitr = regr.fit(train.loc[:, features], np.ravel(train.loc[:, response]))
	return fitr

def train_multilayerperceptron(train, features, response):
	from sklearn.neural_network import MLPClassifier
	regr = MLPClassifier(solver='lbfgs', alpha=1e-5,
						 hidden_layer_sizes=(int(len(features) / 2), int(len(features) / 8)), random_state=1)
	fitr = regr.fit(train.loc[:, features], np.ravel(train.loc[:, response]))
	return fitr

def train_svc(train, features, response):
	from sklearn.svm import SVC
	regr = SVC()
	fitr = regr.fit(train.loc[:, features], np.ravel(train.loc[:, response]))
	return fitr


# challenge7.py

from Crypto.Cipher import AES
from Crypto import Random
import cryptotools as ct

with open('7.txt') as fo:
	encrypted = fo.read()

key = b'YELLOW SUBMARINE'

cipher = AES.new(key, AES.MODE_ECB)

encrypted_bytes = ct.base64_to_bytes(encrypted)
msg = cipher.decrypt(encrypted_bytes)

with open('7_decrypted.txt', 'w') as f:
    print(msg, file=f)


from setuptools import find_packages, setup
from glob import glob
package_name = 'hb_task_1b'

setup(
    name=package_name,
    version='0.0.0',
    packages=find_packages(exclude=['test']),
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        ('share/' + package_name + '/launch', glob('launch/*.launch.py')),
        ('share/' + package_name + '/urdf', glob('urdf/*.xacro')),
        ('share/' + package_name + '/meshes', glob('meshes/*')),
        ('share/' + package_name + '/worlds', glob('worlds/*')),
    ],

    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='aniket',
    maintainer_email='aniket@todo.todo',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'controller = hb_task_1b.controller:main',
            'service_node = hb_task_1b.service_node:main',
        ],
    },
)


import os
import shutil
import time
from datetime import datetime

'''
Put this python file in another directorty or result may cause problem
'''

def folder_maker(folder_path):
    """
    Moves the files in the directory of the modified time
    """
    if os.getcwd() == folder_path:
        print("folder path invalid")
    else:
        os.chdir(folder_path)
        for i in os.listdir():
            path = os.path.join(folder_path, i)
            ti = (datetime.fromtimestamp(os.path.getmtime(path)))
            c_ti = str(ti.strftime('%Y-%m-%d'))
            f_path = os.path.join(folder_path, c_ti)
            if not os.path.exists(f_path):
                os.makedirs(f_path)
            source = os.path.join(folder_path, i)
            destination = os.path.join(folder_path, f_path)
            test = shutil.move(source, destination)

folder_maker()


#  Given the root of a binary tree, return the inorder traversal of its nodes' values.

# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution(object):
    values_list = []

    def inorderTraversal_helper(self, root):
        if root is None:
            return None

        self.inorderTraversal_helper(root.left)
        self.values_list.append(root.val)
        self.inorderTraversal_helper(root.right)

    def inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        self.values_list = []
        self.inorderTraversal_helper(root)
        return self.values_list

if __name__ == '__main__':
    solution = Solution()

    # root = TreeNode(1, None, TreeNode(2, TreeNode(3), None))
    # root = TreeNode(1, TreeNode(2), None)
    root = TreeNode(1, None, TreeNode(2))
    # root = TreeNode(None)

    print(solution.inorderTraversal(root))


# R-4.1
# Describe a recursive algorithm for finding the maximum element in a se-
# quence, S, of n elements. What is your running time and space usage?
from itertools import compress, product

def find_maximum(integers):
    """
     running time is n
    """
    if not integers:
        raise ValueError('Parameter numbers should be not empty.')

    def find_maximum_in_collection(numbers, maximum):
        if not numbers:
            return maximum
        if maximum < numbers[0]:
            maximum = numbers[0]
        return find_maximum_in_collection(numbers=numbers[1:], maximum=maximum)

    return find_maximum_in_collection(numbers=integers[1:], maximum=integers[0])

# R-4.2
# Draw the recursion trace for the computation of power(2, 5), using the
# traditional function implemented in Code Fragment 4.11.
def calculate_recursive_power(base, power):
    if base == 0:
        return 0
    if power == 0:
        return 1
    if power == 1:
        return base

    return base * calculate_recursive_power(base=base, power=power - 1)

# R-4.3
# Draw the recursion trace for the computation of power(2, 18), using the
# repeated squaring algorithm, as implemented in Code Fragment 4.12.
def calculate_fast_recursive_power(base, power):
    if base == 0:
        return 0
    if power == 0:
        return 1
    if power == 1:
        return base

    partial = calculate_fast_recursive_power(base=base, power=power // 2)
    result = partial * partial
    if power % 2 == 1:
        result *= base
    return result

# R-4.4
# Draw the recursion trace for the execution of function reverse(S, 0, 5)
# (Code Fragment 4.10) on S = [4, 3, 6, 2, 6].
def reverse(objects):
    def reverse_collection(objects, start, end):
        if start > end:
            return objects
        tmp = objects[start]
        objects[start] = objects[end]
        objects[end] = tmp

        return reverse_collection(objects, start=start + 1, end=end - 1)

    return reverse_collection(objects=objects, start=0, end=len(objects) - 1)

# R-4.5
# Draw the recursion trace for the execution of function PuzzleSolve(3, S,U )
# (Code Fragment 4.14), where S is empty and U = {a, b, c, d}.
class PermutationSolver:
    def __init__(self, elements, deep_level=None):
        self.elements = elements
        if not deep_level:
            self.deep_level = len(self.elements)
        else:
            self.deep_level = deep_level

        self._all_permutations = None

    def calculate_permutations(self):
        _result_of_permutations = []

        def _calculate_permutations(deep: int, result: tuple, elements):
            for index in range(0, len(elements)):
                if deep <= 1:
                    _result_of_permutations.append(result + tuple([elements[index]]))
                else:
                    _calculate_permutations(
                        deep=deep - 1,
                        result=result + tuple(elements[index]),
                        elements=elements[0: max(0, index)] + elements[index + 1: len(elements)]
                    )

        _calculate_permutations(deep=self.deep_level, result=tuple(), elements=self.elements)
        return _result_of_permutations

    def check_existence_in_permutation_set(self, value):
        if not self._all_permutations:
            self._all_permutations = self.calculate_permutations()

        for permutation in self._all_permutations:
            if value == permutation:
                return True
        return False

# R-4.6
# Describe a recursive function for computing the n th Harmonic number
def calculate_harmonic_number(how_many_numbers, sum_of_numbers=0.0):
    if how_many_numbers <= 1:
        return sum_of_numbers + 1.0

    return calculate_harmonic_number(
        how_many_numbers=how_many_numbers - 1,
        sum_of_numbers=sum_of_numbers + (1 / how_many_numbers)
    )

# R-4.7
# Describe a recursive function for converting a string of digits into the in-
# teger it represents. For example, 13531 represents the integer 13, 531.
def string_to_integer(value: str, multiplier=1, sum_of_numbers=0):
    if not value:
        return sum_of_numbers
    return string_to_integer(
        value=value[0:len(value) - 1],
        multiplier=multiplier * 10,
        sum_of_numbers=sum_of_numbers + (int(value[-1]) * multiplier)
    )

# C-4.9
# Write a short recursive Python function that finds the minimum and max-
# imum values in a sequence without using any loops.
def find_max_and_min(collection_of_objs, max_obj=None, min_obj=None):
    if not collection_of_objs:
        raise ValueError('objects can not be None or empty')

    if not max_obj:
        max_obj = collection_of_objs[0]
    if not min_obj:
        min_obj = collection_of_objs[0]

    def _find_max_min(collection, maximum, minimum):
        if len(collection) == 0:
            return maximum, minimum
        else:
            if collection[0] > maximum:
                maximum = collection[0]
            if collection[0] < minimum:
                minimum = collection[0]

            return find_max_and_min(collection_of_objs=collection_of_objs[1:], max_obj=maximum, min_obj=minimum)

    return _find_max_min(collection=collection_of_objs[1:], maximum=max_obj, minimum=min_obj)

# C-4.10
# Describe a recursive algorithm to compute the integer part of the base-two
# logarithm of n using only addition and integer division.
def find_logarithm_integer_part(result, base=2):
    if base <= 0:
        raise ValueError('base should not be less or equal 0')
    if base == result:
        return 1
    if result == 1:
        return 0

    # TODO logic should be improved. dirty hacks
    def _find_logarithms_integer_part(_base, _result, ):
        if result < _base:
            return 0
        return 1 + find_logarithm_integer_part(result / base, base=base)

    return _find_logarithms_integer_part(
        _base=base,
        _result=result
    )

# C-4.11
# Describe an efficient recursive function for solving the element unique-
# ness problem, which runs in time that is at most O(n 2 ) in the worst case
# without using sorting.
# =======
# possible solution, hashing, sorting, iteration, recursion
def solve_uniqueness_problem(data: list):
    def is_unique_value(data: list, value):
        if not data:
            return True

        if data[0] == value:
            return False

        return is_unique_value(data=data[1:], value=value)

    not_unique_elements = set()
    for index in range(len(data)):
        if not is_unique_value(data=data[index + 1:], value=data[index]):
            not_unique_elements.add(data[index])

    return not_unique_elements

# C-4.12
# Give a recursive algorithm to compute the product of two positive integers,
# m and n, using only addition and subtraction.
def calculate_product(number1: int, number2: int):
    if not number1 or not number2:
        return 0

    def _add_base(total_sum, base, how_many_times):
        if how_many_times == 0:
            return total_sum
        return _add_base(total_sum=total_sum + base, base=base, how_many_times=how_many_times - 1)

    return _add_base(total_sum=0, base=number1, how_many_times=number2)

# C-4.13
# In Section 4.2 we prove by induction that the number of lines printed by
# a call to draw interval(c) is 2 c − 1. Another interesting question is how
# many dashes are printed during that process. Prove by induction that the
# number of dashes printed by draw interval(c) is 2 c+1 − c − 2.
# draw_interval(3)
# - draw_interval(2)
# - draw_interval(1)
# - draw_interval(0) = > None
# - draw_line(1) = > -
# - draw_interval(0) = > None
# - draw_line(2) = > --
# - draw_interval(1)
# - draw_interval(0) = > None
# - draw_line(1) = > -
# - draw_interval(0) = > None
# draw_interval(2)
# - draw_interval(1)
# - draw_interval(0) = > None
# - draw_line(1) = > -
# - draw_interval(0) = > None
# - draw_line(2) = > --
# draw_interval(1)
# - draw_interval(0) = > None
# - draw_line(1) = > -
# - draw_interval(0) = > None
#
# draw_interval(c) = > how
# many
# printed
# lines
# 2 ^ ^ (c) - (1)
#
# draw_interval(1) = > 2 - 1 = 1
# draw_interval(2) = > 4 - 1 = 3
# draw_interval(3) = > 8 - 1 = 7
# draw_interval(4) = > 16 - 1 = 16
#
# draw_interbal(c) = > how
# many
# dashes
# printed
# 2 ^ ^ (c + 1) - c - 2
#
# draw_interval(1) = > 2 ^ ^ (1 + 1) - 1 - 2 = 0 == > 2 ^ ^ (c + 1) - c - 2
# draw_interval(2) = > 2 ^ ^ (2 + 1) - 2 - 2 = 4 == > 2 ^ ^ (c + 1) - c - 2
#
# PROOF:
# 1 + 2 * 2 ^ ^ (c) - c - 1 - 2 = 2 ^ ^ (c + 1) + c - 2

# C-4.14
# In the Towers of Hanoi puzzle, we are given a platform with three pegs, a,
# b, and c, sticking out of it. On peg a is a stack of n disks, each larger than
# the next, so that the smallest is on the top and the largest is on the bottom.
# The puzzle is to move all the disks from peg a to peg c, moving one disk
# at a time, so that we never place a larger disk on top of a smaller one.
# See Figure 4.15 for an example of the case n = 4. Describe a recursive
# algorithm for solving the Towers of Hanoi puzzle for arbitrary n. (Hint:
# Consider first the subproblem of moving all but the n th disk from peg a to
# another peg using the third as “temporary storage.”)

def solve_hanoi_towers_problem(source, auxilary, destination):
    def _move_disk(how_many_disks, source, auxilary, destination):
        print("source ", source, " auxilary ", auxilary, " destination ", destination)
        if how_many_disks > 0:
            _move_disk(how_many_disks=how_many_disks - 1, source=source, auxilary=destination, destination=auxilary)
            if source:
                print("move disk ", "source ", source, " auxilary ", auxilary, " destination ", destination)
                destination.append(source.pop())
            _move_disk(how_many_disks=how_many_disks - 1, source=auxilary, auxilary=source, destination=destination)

    return _move_disk(how_many_disks=len(source), source=source, auxilary=auxilary, destination=destination)

# C-4.15 Write a recursive function that will output all the subsets of a set of n
# elements (without repeating any subsets).
def find_unique_subsets(elements: set):
    def _find_unique_subset(items: list):
        if not items:
            return [set()]
        return (set(compress(items, mask)) for mask in product(*[[0, 1]] * len(items)))

    return _find_unique_subset(items=list(elements))

BINARY = [0, 1]

# binary permutations
def produce_binary_reprezentation_from_0_to_n(n):
    if not n:
        return []
    final_result = []

    def produce_binary(n, result: tuple):
        if n == 0:
            final_result.append(result)
            return
        for b in BINARY:
            produce_binary(n=n - 1, result=result + tuple([b]))

        return result

    produce_binary(n, result=tuple())
    return final_result

# def per(n):
#     for i in range(1<<n):
#         s=bin(i)[2:]
#         s='0'*(n-len(s))+s
#         print map(int,list(s))

# C-4.16 Write a short recursive Python function that takes a character string s and
# outputs its reverse. For example, the reverse of pots&pans would be
# snap&stop .
def reverse_separated_elements(text: str, separator: str):
    def _reverse_string(s: str):
        return s[::-1]

    def _reverse_separated_elements(separared_text: list):
        if not separared_text:
            return ''
        result = _reverse_separated_elements(separared_text=separared_text[1:])
        if result:
            result = result + separator
        return result + _reverse_string(separared_text[0])

    separared_text = [sub_string for sub_string in text.split(sep=separator) if sub_string]
    return _reverse_separated_elements(separared_text=separared_text)

# C-4.17 Write a short recursive Python function that determines if a string s is a
# palindrome, that is, it is equal to its reverse. For example, racecar and
# gohangasalamiimalasagnahog are palindromes.
def is_palindrome_recursive(text: str):
    def _is_palindrome_rescursive(s: str, start_index: int, end_index: int):
        if start_index > end_index:
            return True
        if s[start_index] != s[end_index]:
            return False
        return _is_palindrome_rescursive(s=s, start_index=start_index + 1, end_index=end_index - 1)

    if not text:
        return False

    if len(text) == 1:
        return True

    return _is_palindrome_rescursive(s=text, start_index=0, end_index=len(text) - 1)

VOWELS = {'a', 'e', 'i', 'o', 'u', 'y'}

# C-4.18 Use recursion to write a Python function for determining if a string s has
# more vowels than consonants.
def is_more_vowels_in_string(s: str):
    def _is_more_vowels_in_string(s: str, current_index, how_many_vowels: int):
        if current_index >= len(s):
            if how_many_vowels > len(s) / 2:
                return True
            else:
                return False

        if how_many_vowels > len(s) / 2:
            return True

        current_character = s[current_index]
        if current_character in VOWELS:
            how_many_vowels = how_many_vowels + 1
        return _is_more_vowels_in_string(s=s, current_index=current_index+1, how_many_vowels=how_many_vowels)

    if not s:
        return False

    return _is_more_vowels_in_string(s=s, current_index=0, how_many_vowels=0)

# C-4.19 Write a short recursive Python function that rearranges a sequence of in-
# teger values so that all the even values appear before all the odd values.
def swap_even_numbers_before_odd_numbers(numbers: list):
    numbers_length = len(numbers)

    def _swap_even_numbers_before_odd_numbers(numbers: list, even_index: int, odd_index: int):
        if even_index >= numbers_length or odd_index >= numbers_length:
            return numbers
        if numbers[even_index] % 2 != 0:
            return _swap_even_numbers_before_odd_numbers(numbers=numbers, even_index=even_index+1, odd_index=odd_index)
        if numbers[odd_index] % 2 == 0:
            return _swap_even_numbers_before_odd_numbers(numbers=numbers, even_index=even_index, odd_index=odd_index+1)
        if numbers[even_index] % 2 == 0 and numbers[odd_index] % 2 != 0:
            if even_index > odd_index:
                tmp_even = numbers[even_index]
                numbers[even_index] = numbers[odd_index]
                numbers[odd_index] = tmp_even

            return _swap_even_numbers_before_odd_numbers(numbers=numbers, even_index=even_index, odd_index=odd_index+1)
        raise ValueError('UPS')

    if not numbers or len(numbers) == 1:
        return numbers

    return _swap_even_numbers_before_odd_numbers(numbers=list(numbers), even_index=0, odd_index=0)

def find_pair_number_to_get_sum(sorted_numbers: list, sum_result: int):
    def _find_pair_number_to_get_sum(
            sorted_numbers: list,
            sum_result: int,
            start_index: int,
            end_index: int
    ):
        if start_index > end_index:
            return tuple()
        sum_of_two_numbers = sorted_numbers[start_index] + sorted_numbers[end_index]
        if sum_of_two_numbers == sum_result:
            return sorted_numbers[start_index], sorted_numbers[end_index]
        elif sum_of_two_numbers < sum_result:
            return _find_pair_number_to_get_sum(
                sorted_numbers=sorted_numbers,
                sum_result=sum_result,
                start_index=start_index+1,
                end_index=end_index
            )
        else:
            return _find_pair_number_to_get_sum(
                sorted_numbers=sorted_numbers,
                sum_result=sum_result,
                start_index=start_index,
                end_index=end_index-1
            )

    if not sorted_numbers:
        return tuple()

    return _find_pair_number_to_get_sum(
        sorted_numbers=sorted_numbers,
        sum_result=sum_result,
        start_index=0,
        end_index=len(sorted_numbers) - 1
    )


class Data:

    def __init__(self):
        self.classes = []
        self.X_train = []
        self.X_test  = []
        self.Y_train = []
        self.Y_test  = []
        self.loadData()

    def loadData(self):

        files = {
            'X_test'  : 'test/X_test.txt',
            'Y_test'  : 'test/y_test.txt',
            'X_train' : 'train/X_train.txt',
            'Y_train' : 'train/y_train.txt',
            'classes' : './activity_labels.txt'
        }

        with open(files['classes'], 'r') as classes:
            self.classes = [x.split("\n")[0] for x in classes.readlines()]

        with open(files['X_test'], 'r') as xtest:
            for line in xtest:
                linha = []
                for number in line.strip().split():
                    linha.append(float(number))
                self.X_test.append(linha)

        with open(files['X_train'], 'r') as xtrain:
            for line in xtrain:
                linha = []
                for number in line.strip().split():
                    linha.append(float(number))
                self.X_train.append(linha)

        with open(files['Y_test'], 'r') as ytest:
            for line in ytest:
                self.Y_test.append(line.split("\n")[0])

        with open(files['Y_train'], 'r') as ytrain:
            for line in ytrain:
                self.Y_train.append(line.split("\n")[0])


import sys
sys.path.append("/usr/lib/python3.7/")
#import tensorflow as tf

import platform
import cv2
from threading import Thread
from video_stream import VideoStream
from datetime import datetime
import datetime
import time
import numpy as np
import os
from detector_cv2 import Detector
from picamera.array import PiRGBArray
from picamera import PiCamera
from database_manager import DatabaseManager
import pymongo
import ast
import tensorflow as tf
import nms

SSD_MOBILENET_V2_SAVED_MODEL_PATH="/home/pi/Desktop/My_Server/backend/models/"
category_map = {
    1: 'person',
    2: 'bicycle',
    3: 'car',
    4: 'motorcycle',
    5: 'airplane',
    6: 'bus',
    7: 'train',
    8: 'truck',
    9: 'boat',
    10: 'traffic light',
    11: 'fire hydrant',
    13: 'stop sign',
    14: 'parking meter',
    15: 'bench',
    16: 'bird',
    17: 'cat',
    18: 'dog',
    19: 'horse',
    20: 'sheep',
    21: 'cow',
    22: 'elephant',
    23: 'bear',
    24: 'zebra',
    25: 'giraffe',
    27: 'backpack',
    28: 'umbrella',
    31: 'handbag',
    32: 'tie',
    33: 'suitcase',
    34: 'frisbee',
    35: 'skis',
    36: 'snowboard',
    37: 'sports ball',
    38: 'kite',
    39: 'baseball bat',
    40: 'baseball glove',
    41: 'skateboard',
    42: 'surfboard',
    43: 'tennis racket',
    44: 'bottle',
    46: 'wine glass',
    47: 'cup',
    48: 'fork',
    49: 'knife',
    50: 'spoon',
    51: 'bowl',
    52: 'banana',
    53: 'apple',
    54: 'sandwich',
    55: 'orange',
    56: 'broccoli',
    57: 'carrot',
    58: 'hot dog',
    59: 'pizza',
    60: 'donut',
    61: 'cake',
    62: 'chair',
    63: 'couch',
    64: 'potted plant',
    65: 'bed',
    67: 'dining table',
    70: 'toilet',
    72: 'tv',
    73: 'laptop',
    74: 'mouse',
    75: 'remote',
    76: 'keyboard',
    77: 'cell phone',
    78: 'microwave',
    79: 'oven',
    80: 'toaster',
    81: 'sink',
    82: 'refrigerator',
    84: 'book',
    85: 'clock',
    86: 'vase',
    87: 'scissors',
    88: 'teddy bear',
    89: 'hair drier',
    90: 'toothbrush'
}
img_width=300
img_height=300

class DetectionManager:
    def __init__(self):
        self.model_paths = {"SSD_Mobilenet_v2_320x320":"/home/pi/Desktop/My_Server/backend/models/"}
        self.category_maps = {"SSD_Mobilenet_v2_320x320":{1: "person"}}

    def setupDetection(self, detection_period_id, neuralNetworkType="SSD_Mobilenet_v2_320x320", detectionSeconds=60,  obj_threshold=0.3,
                       video_resolution={"width":320, "height":320}, framerate=30):
        #TU SPRAWDZIC
        self.neuralNetworkType = neuralNetworkType
        self.obj_threshold = obj_threshold
        self.detectionSeconds = detectionSeconds
        self.video_resolution = video_resolution
        self.framerate = framerate
        self.detector = Detector("SSD_Mobilenet_v2_320x320")
        self.database_manager = DatabaseManager()
        #print("In setupDetection: " + str(detection_period_id))
        self.detection_period_id = detection_period_id
        #print("Starting detection: " + str(datetime.datetime.now()))
        detections = self.detect()
        #print("Ending detection: " + str(datetime.datetime.now()))

    def detect(self):
        resolution={"width":1200, "height":720}
        framerate=30
        camera = PiCamera()
        camera.rotation = 180
        camera.resolution = (resolution["width"], resolution["height"])
        camera.framerate = framerate
        rawCapture = PiRGBArray(camera, size = (resolution["width"], resolution["height"]))
        print("I am alive")
        detections = self.detect()
        file = open("test_file", "w")
        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        file.write("Active on: " + str(current_time))
        file.close()
        start_time = time.time()
        #video_stream = VideoStream(self.video_resolution, self.framerate)

        #video_stream.start()
        detection_results = None
        '''
        xml_file = '/home/pi/Desktop/My_Server/backend/endpoints/haarcascades/haarcascade_frontalface_default.xml'
        classifier = cv2.CascadeClassifier(xml_file)
        '''
            #frame = video_stream.read()
        for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

            # Otherwise, grab the next frame from the stream
            img = frame.array

            rawCapture.truncate(0)

            #detection_results = self.detector.detect(frame, self.video_resolution["width"], self.video_resolution["height"])
            '''
            selected_indices = tf.image.non_max_suppresion(
                boxes=detection_results['detection_boxes'][0],
                scores=detection_results['detection_scores'][0],
                max_output_size=10,
                iou_threshold=0.5,
                score_threshold=0.2
                )

            selected_boxes = tf.gather(detection_results['detection_boxes'][0], selected_indices)
            '''

            #print("How many detected boxes were created: " + str(len(detection_results['detection_boxes'][0].numpy())))

            #selected_boxes = nms.non_max_suppression_fast(detection_results['detection_boxes'][0].numpy(), 0.4)
            '''
            selected_boxes = detection_results['detection_boxes'][0]

            for box in selected_boxes:
                frame = cv2.rectangle(frame, (int(box[1] * resolution["width"]), int(box[0] * resolution["height"])),
                                          (int(box[3] * resolution["width"]), int(box[2] * resolution["height"])), (0, 255, 0), 2)

            for box, score, det_class in zip(detection_results['detection_boxes'][0], detection_results['detection_scores'][0], detection_results['detection_classes'][0]):
                 if score > 0.2 and int(det_class) == 1:
                    frame = cv2.rectangle(frame, (int(box[1] * resolution["width"]), int(box[0] * resolution["height"])),
                                          (int(box[3] * resolution["width"]), int(box[2] * resolution["height"])), (255, 0, 0), 2)
            '''
            '''
            gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

            faces_rect = classifier.detectMultiScale(
                gray_img, scaleFactor=1.1, minNeighbors=9)

            for (x, y, w, h) in faces_rect:
                frame = cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), thickness=2)
            '''
            number_of_people = self.detector.detect_cv2(img)

            #print(detection_results)

            #cv2.imwrite('\home\pi\Desktop\my_img', frame)
            current_time = time.time()
            elapsed_time = current_time - start_time

            #detection_objects.append({"frame_time":current_time, "detections":predicted_boxes})
            self.database_manager.insertDetection(current_time, number_of_people, self.detection_period_id)

            if elapsed_time > self.detectionSeconds:
                #self.writeDetectionPeriodSummary(detection_objects, start_time, self.detectionSeconds)
                '''
                for det_obj in detection_objects:
                    for fin_box in det_obj:
                        print("detection_object: " + str(fin_box.classes[0]))
                '''
                break

        camera.stop_preview()
        camera.close()
        cv2.destroyAllWindows()
        #videostream.stop()

        #return final_boxes
        '''

    def determineSecondsForDetection(self, detectionTimeString):
        timeValues = []
        timeValues = detectionTimeString.split(':')
        for timeVal in timeValues:
            print("Time value: " + timeVal)
        timeNow = datetime.datetime.now().time()
        hours = int(timeValues[0]) - timeNow.hour
        minutes = int(timeValues[1]) - timeNow.minute
        print("Hours: " + str(hours))
        print("Minutes: " + str(minutes))
        if minutes < 0:
            minutes = 60 - minutes
            hours -= 1

        if hours < 0:
            print("End time must be grater that start time !")
        start_time = time.time()

    def writeDetectionPeriodSummary(self, detections, timestamp, seconds_of_detection):
        f = open(self.PATH_TO_DETECTION_FILES + str(timestamp), "a")
        f.write("START:" + str(timestamp) + ":DURATION:" + str(seconds_of_detection) + "\n")
        for detection in detections:
            f.write("frame_time:" + str(detection["frame_time"]) + ":detections:" + str(detection["detections"]) + "\n")
        f.write("NUMBER_OF_DETECTIONS:" + str(len(detections)))
        f.close()

    def get_detection_data(self):
        detection_objects = []
        # iterate over files in
        # that directory
        for filename in os.listdir(self.PATH_TO_DETECTION_FILES):
            f = os.path.join(self.PATH_TO_DETECTION_FILES, filename)
            # checking if it is a file
            if os.path.isfile(f):
                myfile = open(f, "r")
                mylist = myfile.readlines()
                basic_info_array = mylist[0].split(':')
                print("Timestamp: " + basic_info_array[1])
                print("secondsOfDetection: " + basic_info_array[3])
                detection_object = {
                    "timestamp":basic_info_array[1],
                    "secondsOfDetection":basic_info_array[3],
                    "detections":mylist[1:len(mylist)-1],
                    "numberOfDetections":mylist[-1]
                    }
                myfile.close()
                return detection_object

                detection_objects.append(detection_object)
        return detection_objects
'''


from io import StringIO
import json
from unittest import TestResult
import requests
from http.cookies import SimpleCookie

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:78.0) Gecko/20100101 Firefox/78.0",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5"
}

site_list = []
sites = []
total_cookies = 0
with open('sites.txt', 'r') as f:
    # Loops through each line (site) in file
    # and appends to sites_list
    for site in f:
        # Stripping newline from the string
        site_list.append(site.strip('\n'))

# Context manager to properly open and close file handle
with open('failed_sites.txt', 'w') as f:
    # Loop through each site in sites.txt
    for site in site_list:
         # Just declaring these to be used later. My naming is terrible
        samesite = 0
        lax = 0
        strict = 0
        ssnone = 0
        path_count = 0
        path_nondefault_count = 0
        http_only = 0
        secure = 0
        total_cookies = 0
        try:
            r = requests.head("http://"+site, timeout=60, headers=headers, allow_redirects=True)

            # Writes the HTTP response to the text file for this site
            with open('responses/' + site + '.txt', 'w') as response_file:
                r.history.append(r)
                for response in r.history:

                    # If this reponse does not set any cookies then continue to next response
                    if 'Set-Cookie' in response.headers:
                        cookies = response.headers['Set-Cookie']
                        total_cookies += len(response.raw.headers.getlist('Set-Cookie'))
                        samesite += cookies.count('SameSite')
                        lax += cookies.count('Lax')
                        strict += cookies.count('Strict')
                        ssnone += cookies.count('None')
                        path_count += cookies.count('path')
                        path_default = cookies.count('path=/')
                        path_nondefault_count += (path_count - path_default)
                        http_only += cookies.count('HttpOnly')
                        secure += cookies.count ('Secure')

                    response_file.write("HTTP/" + str(response.raw.version)[0] + "." + str(response.raw.version)[1] + " " + str(response.status_code) + " " + response.reason + '\n')
                    for item in response.headers:
                        if item == 'Set-Cookie':
                            for i in response.raw.headers.getlist('Set-Cookie'):
                                response_file.write('Set-Cookie: ' + i + '\n')
                            continue
                        response_file.write(item + ": " + response.headers[item] + '\n')
                    response_file.write('\n')

        # Catches potential exceptions for when a site takes too long to respond or some other error
        except requests.exceptions.RequestException as ex:
            f.write(site + ": " + str(ex) + '\n')
        sites.append(
                            {
                                'name': site,
                                'status_code': r.status_code,
                                'cookie_count': total_cookies,
                                'samesite_count': samesite,
                                'httponly_count': http_only,
                                'secure': secure,
                                'samesite_strict': strict,
                                'samesite_lax': lax,
                                'samesite_none': ssnone,
                                'path_count': path_count,
                                'path_nondefault_count': path_nondefault_count

                            }
                        )

# Writes the results to a json file to be read by table.py
with open('results.json', 'w') as f:
    json.dump(sites, f)