import os
import sqlite3 as sql
from loacore.conf import DB_PATH
from loacore.utils.status import ProcessState

from pyFreelingApi import freeling_api as freeling

def add_lemmas_to_sentences(sentences, print_lemmas=False, _state_queue=None, _id_process=None, freeling_modules=None):
    """

    Performs a Freeling process to add lemmas to words.\n
    However, the argument is actually a sentence to better fit Freeling usage.\n
    Our sentences will be converted to a Freeling Sentences before processing.

    .. note:: This function should be used only inside the :func:`file_process.add_files()` function.

    :param sentences: Sentences to process
    :type sentences: :obj:`list` of |Sentence|
    :param print_lemmas: If True, print lemmatization results
    :type print_lemmas: boolean
    """
    from loacore.utils.db import safe_commit, safe_execute
    from loacore.conf import DB_TIMEOUT

    freeling_sentences = [sentence.compute_freeling_sentence() for sentence in sentences]

    if freeling_modules is None:
        if _state_queue is not None:
            _state_queue.put(
                ProcessState(_id_process, os.getpid(), "Loading Freeling...", " - "))
        morfo = init_freeling()
    else:
        morfo = freeling_modules

    # Print sentence
    _lemmatization_state(_state_queue, _id_process)

    processed_sentences = morfo.analyze(freeling_sentences)

    # Copy freeling results into our Words
    for s in range(len(sentences)):
        sentence = sentences[s]

        if not len(sentence.words) == len(processed_sentences[s]):
            print("/!\\ Warning, sentence offset error in lemma_process /!\\")
            print(sentence.sentence_str())
            print([w.get_form() for w in processed_sentences[s]])

        for w in range(len(sentence.words)):
            word = sentence.words[w]
            word.lemma = processed_sentences[s][w].get_lemma()
            if print_lemmas:
                print(word.word + " : " + word.lemma)

    # Add lemmas to database
    conn = sql.connect(DB_PATH, timeout=DB_TIMEOUT)
    c = conn.cursor()

    sentence_count = 0
    total_sentence = len(sentences)
    _commit_state(_state_queue, _id_process, " - ", " - ")
    for sentence in sentences:
        # Print state
        sentence_count += 1
        _commit_state(_state_queue, _id_process, sentence_count, total_sentence)

        for word in sentence.words:
            # Add Lemma to Lemma Table
            safe_execute(c,
                         "INSERT INTO Lemma (Lemma, ID_Word) VALUES (?, ?)",
                         0,
                         _state_queue,
                         _id_process,
                         mark_args=(word.lemma, word.id_word))

            # Get back id of last inserted lemma
            safe_execute(c,
                         "SELECT last_insert_rowid()",
                         0,
                         _state_queue,
                         _id_process)
            id_lemma = c.fetchone()[0]

            # Update Word table
            safe_execute(c,
                         "UPDATE Word SET ID_Lemma = " + str(id_lemma) + " WHERE ID_Word = " + str(word.id_word),
                         0,
                         _state_queue,
                         _id_process)

    if _state_queue is None:
        print("")
    safe_commit(conn, 0, _state_queue, _id_process)

    conn.close()

# ********************************************* Freeling Options****************************************************** #

def my_maco_options(lang, lpath):

    # create options holder
    opt = freeling.maco_options(lang)

    # Provide files for morphological submodules. Note that it is not
    # necessary to set file for modules that will not be used.

    opt.DictionaryFile = os.path.join(lpath, "dicc.src")

    opt.PunctuationFile = os.path.join(lpath, "..", "common", "punct.dat")
    return opt

def init_freeling():

    freeling.util_init_locale("default")

    from loacore.conf import lang
    from loacore.conf import LANG_PATH as lpath

    # create the analyzer with the required set of maco_options
    morfo = freeling.maco(my_maco_options(lang, lpath))
    #  then, (de)activate required modules
    morfo.set_active_options(False,  # UserMap
                             False,  # NumbersDetection,
                             True,  # PunctuationDetection,
                             False,  # DatesDetection,
                             True,  # DictionarySearch,
                             False,  # AffixAnalysis,
                             False,  # CompoundAnalysis,
                             False,  # RetokContractions,
                             False,  # MultiwordsDetection,
                             False,  # NERecognition,
                             False,  # QuantitiesDetection,
                             False)  # ProbabilityAssignment

    return morfo

def _lemmatization_state(state_queue, id_process):
    if state_queue is not None:
        state_queue.put(
            ProcessState(id_process, os.getpid(), "Lemmatization", "-"))
    else:
        print("Lemmatization", end="\n")

def _commit_state(state_queue, id_process, sentence_count, total_sentence):
    if state_queue is not None:
        state_queue.put(
            ProcessState(id_process, os.getpid(), "Lemma DB commit...", str(sentence_count) + " / " + str(total_sentence)))
    else:
        print("\r" + str(sentence_count) + " / " + str(total_sentence) + " sentences added.", end="")


from flask import Flask, request, render_template
import random
from expansions import expand

app = Flask(__name__, static_url_path="")

@app.route("/", methods=["POST", "GET"])
def render_name():
    themes = []
    expression = ""
    name = ""

    if len(request.values.get("expression", "").strip()) > 0:
        expression = request.values.get("expression")
        name = expand(expression)

    else:
        themes = request.values.getlist("theme")
        if not isinstance(themes, list):
            themes = ["buzzword"]

        buzz_patterns = []
        dict_patterns = []

        for theme in themes:
            buzz_patterns.append("{prefix}{%s|few}" % theme)
            buzz_patterns.append("{%s|few}{suffix}" % theme)
            buzz_patterns.append("{%s|several}" % theme)
            dict_patterns.append("{dict %s|several}" % theme)

        if random.random() > .7:
            pattern = random.choice(dict_patterns)
        else:
            pattern = random.choice(buzz_patterns)

        name = expand(pattern)

    return render_template("index.html", name=name, themes=themes, expression=expression)

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


import re
import pandas as pd
from datetime import date, timedelta, datetime
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By

urls = [
    "https://apnews.com/article/east-timor-prime-minister-gusmao-9d3f254834da006a71e1597f361a33a5",
    "https://apnews.com/article/deaths-toxic-gas-leak-south-africa-a69a73e5c454fdfd7331be873556eab9",
    "https://apnews.com/article/china-heat-weather-flood-7a2ef4f89ebbf63cd8158f1954c78633"
]

today = date.today()
week_ago = today - timedelta(days=7)

def scrap(link):
    options = Options()
    options.add_argument("--headless")
    options.add_argument("--disable-gpu")
    options.add_argument("--log-level=3")

    driver = webdriver.Firefox(options)

    df = pd.DataFrame(columns=["Title", "Summary", "HTML", "Pictures", "Videos", "Categories", "OG Date", "Update Date"])

    driver.get(link)
    driver.implicitly_wait(3)
    soup = BeautifulSoup(driver.page_source, "html.parser")
    og_date = soup.find("meta", {"property": "article:published_time"})["content"] if soup.find("meta", {"property": "article:published_time"}) else ""
    og_date = re.sub(r"T.*", "", og_date).strip()

    og_date = datetime.strptime(og_date, "%Y-%m-%d").date()

    # if og_date < week_ago:
    #     return

    title = soup.find("h1").text
    summary = soup.find("meta", {"name": "description"})["content"].strip()
    html = soup.find("main", {"class": "Page-main"})

    pictures = []
    videos = []

    figures = driver.find_elements(By.XPATH, '//figure')

    for fig in figures:
        # if there is a slideshow of images
        if fig.find_elements(By.CLASS_NAME, "Carousel") != []:
            for img in fig.find_elements(By.CLASS_NAME, "CarouselSlide"):
                src = img.find_element(By.TAG_NAME, "img").get_attribute("src")
                figcaption = img.find_element(By.CLASS_NAME, "CarouselSlide-infoDescription").text
                pictures.append([src, figcaption])
        # if there is a single image
        else:
            if fig.find_element(By.TAG_NAME, "img").get_attribute("src") == None:
                src = re.search('srcset="([^"]+)"', fig.get_attribute("innerHTML")).group(1)

                #if there is multiple images, get the first one
                if "," in src:
                    src = src.split(",")[0]
            else:
                src = fig.find_element(By.TAG_NAME, "img").get_attribute("src")

            figcaption_element = fig.find_elements(By.TAG_NAME, "figcaption")
            figcaption = figcaption_element[0].text if figcaption_element else fig.find_element(By.CLASS_NAME, "dcr-12evv1c").text
            pictures.append([src, figcaption])

    categories = []
    for category in soup.find_all("meta", {"property": "article:tag"}):
        categories.append(category["content"])

    update_date = soup.find("meta", {"property": "article:modified_time"})["content"]
    update_date = re.sub(r"T.*", "", update_date).strip()
    update_date = datetime.strptime(update_date, "%Y-%m-%d").date()
    # print(update_date)

    df.loc[len(df)] = [title, summary, html, pictures, videos, categories, og_date, update_date]
    df.to_csv("apnews.csv", index=False)
    driver.quit()

"""
for url in urls:
    scrap(driver, url)
print(df)
"""


# Project, Part 1, Uninformed Search

from networkx import nx
# import matplotlib.pyplot as plt
from queue import PriorityQueue
import time

# MAX_CARDS = 1

def load_data():
    G = nx.Graph()
    with open("input.txt", "r") as f:
        for line in f.readlines():
            G.add_edge(*tuple(map(int, line.split())))

    return G

def exposed_graph(G, node):
    H = nx.Graph()
    for i in node:
        H.add_edges_from([(i, n) for n in G.neighbors(i)])

    return H

def path_cost(G, node):
    population = G.number_of_nodes()

    cost = 0
    unexposed_people = G.copy()
    for i, person in enumerate(node):
        n_exposed = G.number_of_nodes() - unexposed_people.number_of_nodes()
        n_given_card = i

        new_exposed_people = unexposed_people.neighbors(person)
        new_exposed = len(new_exposed_people)

        adoption_probability = max(.1, 1 - 1 / (n_exposed + n_given_card)) if n_exposed + n_given_card != 0 else .1
        cost += population - 1 - new_exposed * adoption_probability

        unexposed_people.remove_nodes_from(new_exposed_people)
        unexposed_people.remove_node(person)

    return cost

def card_candidates(G, node):
    H = exposed_graph(G, node)
    I = G.copy()
    I.remove_nodes_from(H)
    return I.nodes()

def goal_test(G, node, max_cards, candidates=None):
    if len(node) == max_cards:
        return True

    if candidates is None:
        candidates = card_candidates(G, node)
    return len(candidates) == 0

# running in about T(n, c) = .009 * n^(c - 1) where n of friendships and c is the number of cards given out
def uniform_cost_search(G, num_cards):
    frontier = PriorityQueue()

    count = 0
    frontier.put((0, []))
    while "pigs" != "fly":
        if frontier.empty():
            raise Exception("Not solution found")

        cost, node = frontier.get()

        candidates = card_candidates(G, node)
        if goal_test(G, node, num_cards, candidates):
            print("Nodes expanded: " + str(count))
            return node

        count += 1
        for candidate in candidates:
            child = node[:] + [candidate]
            frontier.put((path_cost(G, child), child))

def main():
    print("running project.part1.uninformed_search")

    G = load_data()
    for i in range(1, 4):
        start = time.time()

        solution = uniform_cost_search(G, i)

        print(solution)
        print("Time: " + str(time.time() - start))
        print()

if __name__ == '__main__':
    main()


# Libraries
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import plotly.express as px
import folium
import plotly.graph_objects as go
import streamlit as st
from PIL import Image
import folium
from folium.plugins import MarkerCluster
from streamlit_folium import folium_static

#=========================
#Funções
#=========================

#Preenchimento do nome dos países
COUNTRIES = {
1: "India",
14: "Australia",
30: "Brazil",
37: "Canada",
94: "Indonesia",
148: "New Zeland",
162: "Philippines",
166: "Qatar",
184: "Singapure",
189: "South Africa",
191: "Sri Lanka",
208: "Turkey",
214: "United Arab Emirates",
215: "England",
216: "United States of America",
}
def country_name(country_id):
    return COUNTRIES[country_id]

#Criação do nome das Cores
COLORS = {
"3F7E00": "darkgreen",
"5BA829": "green",
"9ACD32": "lightgreen",
"CDD614": "orange",
"FFBA00": "red",
"CBCBC8": "darkred",
"FF7800": "darkred",
}
def color_name(color_code):
    return COLORS[color_code]

#Criação do Tipo de Categoria de Comida
def create_price_tye(price_range):
    if price_range == 1:
        return "cheap"
    elif price_range == 2:
        return "normal"
    elif price_range == 3:
        return "expensive"
    else:
        return "gourmet"

    #Renomear as colunas do DataFrame
def rename_columns(dataframe):
    df = dataframe.copy()
    title = lambda x: inflection.titleize(x)
    snakecase = lambda x: inflection.underscore(x)
    spaces = lambda x: x.replace(" ", "")
    cols_old = list(df.columns)
    cols_old = list(map(title, cols_old))
    cols_old = list(map(spaces, cols_old))
    cols_new = list(map(snakecase, cols_old))
    df.columns = cols_new
    return df

#------------------------Início da estrutura lógica do código---------------------
#---------------------------------------------------------------------------------
#Import dataset
#---------------------------------------------------------------------------------
df=df = pd.read_csv('dataset/zomato.csv')
#---------------------------------------------------------------------------------

#Limpeza de Dados

#Removendo as vírgulas da culinária
df['Cuisines'] = df['Cuisines'].astype( str )

df["Cuisines"] = df.loc[:, "Cuisines"].apply(lambda x: x.split(",")[0])

#Não remova 'Has Online delivery', 'Is delivering now',

#removendo colunas
df = df.drop(['Switch to order menu'], axis = 1)

#Associando os códigos
df['Country'] = df['Country Code'].apply(lambda x: country_name(x), [0])

#Removendo valores vazios

df['Cuisines'] = df['Cuisines'].astype( str )

linha_vazia = df['Cuisines'] != 'nan'

df = df.loc[linha_vazia, :]

#Removendo duplicatas

df = df.drop_duplicates()

#Removendo Outlier
linha = df[df['Average Cost for two'] == 25000017]
df = df.drop(385)

#----------------Copy------------------------
df1 = df.copy()

#================================================
#Barra Lateral
#================================================

st.header('Fome Zero! ')

image_path = 'logo.png'
image = Image.open(image_path)
st.sidebar.image(image, width=120)

st.sidebar.markdown('# Fome Zero')
st.sidebar.markdown('''---''')

st.sidebar.markdown('### Filtros')

country_options = st.sidebar.multiselect(
    'Escolha os Paises que Deseja visualizar os Restaurantes',
    ["India", "Australia", "Brazil", "Canada", "Indonesia", "New Zeland", "Philippines", "Qatar", "Singapure", "South Africa", "Sri Lanka", "Turkey", "United Arab Emirates", "England", "United States of America"],
    default = ["India", "Australia", "Brazil", "Canada", "Indonesia", "New Zeland", "Philippines", "Qatar", "Singapure", "South Africa", "Sri Lanka", "Turkey", "United Arab Emirates", "England", "United States of America"])

#filtro da visão países
linha_selecionada = df1['Country'].isin(country_options)
df1 = df1.loc[linha_selecionada, :]

st.sidebar.markdown('''---''')

st.sidebar.markdown('###### Powered by Mateus')
st.sidebar.markdown('###### Data Scientist @ Comunidade DS')

#====================================================
#Layout do streamlit
#====================================================

st.subheader('O Melhor lugar para encontrar seu mais novo restaurante favorito!')

st.markdown('Temos as seguintes marcas dentro da nossa plataforma:')

with st.container():
    col1, col2, col3, col4, col5 = st.columns(5)
    with col1:
        #st.subheader('Restaurantes Cadastrados')
        registro = df1['Restaurant ID'].nunique()
        col1.metric('Restaurantes Cadastrados', registro)
    with col2:
        cadastro = df1['Country Code'].nunique()
        col2.metric('Países Cadastrados', cadastro)
    with col3:
        cities = df1['City'].nunique()
        col3.metric('Cidades Cadastradas', cities)
    with col4:
        votos = df1['Votes'].sum()
        col4.metric('Avaliações feitas na Plataforma', votos)
    with col5:
        culinaria = df["Cuisines"].nunique()
        col5.metric('Tipos de Culinária', culinaria)

with st.container():
    #df_aux = df1.loc[:, ['City', 'Latitude', 'Longitude']]
    #map = folium.Map()
    #for index, location_info in df_aux.iterrows():
     #   folium.Marker([location_info['Latitude'], location_info['Longitude']], popup=location_info[['City']], icon=folium.Icon(color='green', icon = 'ok-sign'),
      #             ).add_to(map)

    #folium_static(map, width = 1024, height = 600)
    locais = df1[['Latitude', 'Longitude']].values.tolist()

    mapa = folium.Map(location=[121.009787, 14.447615], zoom_start = 5)

    MarkerCluster(locations=locais).add_to(mapa)
    mapa.save('mapa.html')

    folium_static(mapa, width = 1024, height = 600)


from flask import Flask, request, jsonify, json
from flask_api import status
from jinja2._compat import izip
from datetime import datetime
from flask_cors import CORS, cross_origin
from flaskext.mysql import MySQL
from flask import Blueprint
from flask_restplus import Api, Resource, fields
import requests

app = Flask(__name__)
cors = CORS(app)
mysql = MySQL()
product_rating = Blueprint('product_rating_api', __name__)
api = Api(product_rating, version='1.0', title='MyElsa API',
    description='MyElsa API')
name_space = api.namespace('ProductRating', description='Product Rating')

'''app.config['MYSQL_DATABASE_USER'] = 'creamson_langlab'
app.config['MYSQL_DATABASE_PASSWORD'] = 'Langlab@123'
app.config['MYSQL_DATABASE_DB'] = 'creamson_logindb'
app.config['MYSQL_DATABASE_HOST'] = 'creamsonservices.com'
'''

app.config['MYSQL_DATABASE_USER'] = 'admin'
app.config['MYSQL_DATABASE_PASSWORD'] = 'cbdHoRPQPRfTdC0uSPLt'
app.config['MYSQL_DATABASE_DB'] = 'creamson_logindb'
app.config['MYSQL_DATABASE_HOST'] = 'myelsa.cdcuaa7mp0jm.us-east-2.rds.amazonaws.com'

mysql.init_app(app)

app.config['CORS_HEADERS'] = 'Content-Type'

rating_post = api.model('rating_post', {
	"user_id":fields.Integer(required=True),
	"status":fields.String(),
	"rating":fields.Integer(),
	"product_code":fields.String(required=True)})

@name_space.route("/rateProductPost")
class rateProductPost(Resource):
	@name_space.expect(rating_post)
	def post(self):
		details = request.get_json()
		connection = mysql.connect()
		cursor = connection.cursor()
		user_id = details['user_id']
		fav_status = details['status']
		product_code = details['product_code']
		rating = details['rating']

		cursor.execute("""SELECT `id` FROM `product_rating` WHERE
			`user_id` = %s and `product_code` = %s""",(user_id,product_code))

		idx = cursor.fetchone()
		# print(idx)
		if idx:
			idx = idx[0]
		else:
			idx = None

		if idx == None:
			product_rating_insert_query = ("""INSERT INTO `product_rating`(`user_id`, `product_code`,
				`rating`, `status`) VALUES(%s,%s,%s,%s)""")
			product_rating_data = (user_id,product_code,rating,fav_status)
			cursor.execute(product_rating_insert_query,product_rating_data)
			connection.commit()
			details['rating_id'] = cursor.lastrowid

			cursor.execute("""SELECT avg(`rating`),COUNT(`product_code`) FROM product_rating
					WHERE product_code = %s GROUP BY product_code""",(product_code))

			rating_data = cursor.fetchall()
			if rating_data:
				avg_rating = rating_data[0][0]
				noofrating = rating_data[0][1]
				avg_rating = 4.5
				noofrating = 12 + noofrating
				if noofrating < 20:
					avg_rating = 4.5
				if noofrating > 20:
					avg_rating = round((rating_data[0][0]),2)

			details['noofrating'] = noofrating
			details['avg_rating'] = avg_rating

			return ({"attributes": {
		    				"status_desc": "Product Rating Details.",
		    				"status": "success"
		    				},
		    				"responseList":{"RatingDtls":details}}), status.HTTP_200_OK

		else:
			# update_url = 'http://127.0.0.1:5000/product_rating/ProductRating/rateProductUpdate'
			update_url = 'http://ec2-18-191-221-235.us-east-2.compute.amazonaws.com/product_rating/ProductRating/rateProductUpdate'
			update_data = details
			headers = {'Content-type':'application/json', 'Accept':'application/json'}
			rating_put_res = requests.put(update_url, data=json.dumps(update_data),headers=headers)
			res = rating_put_res.json()
			print(res)
			return res

		connection.commit()
		cursor.close()

@name_space.route("/rateProductUpdate")
class rateProductUpdate(Resource):
	@name_space.expect(rating_post)
	def put(self):
		details = request.get_json()
		connection = mysql.connect()
		cursor = connection.cursor()
		user_id = details['user_id']
		fav_status = details['status']
		product_code = details['product_code']
		rating = details['rating']
		print(user_id,product_code)
		cursor.execute("""SELECT `id`,`rating`,`status` FROM `product_rating` WHERE
			`user_id` = %s and `product_code` = %s""",(user_id,product_code))

		idx = cursor.fetchall()
		# print(idx)
		if idx:
			rating_id = idx[0][0]
			cursor.execute("""SELECT avg(`rating`),COUNT(`product_code`) FROM product_rating
					WHERE product_code = %s GROUP BY product_code""",(product_code))

			rating_data = cursor.fetchall()
			if rating_data:
				avg_rating = rating_data[0][0]
				noofrating = rating_data[0][1]
				avg_rating = 4.5
				noofrating = 12 + noofrating
				if noofrating < 20:
					avg_rating = 4.5
				if noofrating > 20:
					avg_rating = round((rating_data[0][0]),2)

			details['noofrating'] = noofrating
			details['avg_rating'] = avg_rating
		if rating and not fav_status:
			update_rating_query = ("""UPDATE `product_rating` SET `rating`= %s
				WHERE id = %s""")
			update_rating_data = (rating,rating_id)
			cursor.execute(update_rating_query,update_rating_data)
		elif fav_status and not rating:
			update_rating_query = ("""UPDATE `product_rating` SET `status`= %s
				WHERE id = %s""")
			update_rating_data = (fav_status,rating_id)
			cursor.execute(update_rating_query,update_rating_data)

		elif fav_status and rating:
			update_rating_query = ("""UPDATE `product_rating` SET `rating`= %s,`status`= %s
				WHERE id = %s""")
			update_rating_data = (rating,fav_status,rating_id)
			cursor.execute(update_rating_query,update_rating_data)
		else:
			user_rating = idx[0][1]
			user_fav_status = idx[0][2]
			update_rating_query = ("""UPDATE `product_rating` SET `rating`= %s ,`status`= %s
				WHERE id = %s""")
			update_rating_data = (user_rating,user_fav_status,rating_id)
			cursor.execute(update_rating_query,update_rating_data)

		connection.commit()
		details['rating_id'] = rating_id
		cursor.close()
		return ({"attributes": {
		    				"status_desc": "Product Rating Details.",
		    				"status": "success"
		    				},
		    				"responseList":{"RatingDtls":details}}), status.HTTP_200_OK

@name_space.route("/getProductRatingAndLike/<int:user_id>/<string:product_code>")
class rateProductUpdate(Resource):
	def get(self,user_id,product_code):
		connection = mysql.connect()
		cursor = connection.cursor()
		details = {"avg_rating":4.5,
					"noofrating":12,
					"likes_count":0,
					"dislike_count":0,
					"user_rating":"",
					"user_vote":""}
		try:

			cursor.execute("""SELECT id FROM product_rating
				WHERE product_code = %s""",(product_code))

			idx = cursor.fetchone()
			if idx:
				idx = idx[0]

			if idx:
				cursor.execute("""SELECT avg(`rating`),COUNT(`product_code`) FROM product_rating
					WHERE product_code = %s GROUP BY product_code""",(product_code))

				rating_data = cursor.fetchall()
				if rating_data:
					avg_rating = rating_data[0][0]
					noofrating = rating_data[0][1]
					avg_rating = 4.5
					noofrating = 12 + noofrating
					if noofrating < 20:
						avg_rating = 4.5
					if noofrating > 20:
						avg_rating = round((rating_data[0][0]),2)
				else:
					avg_rating = 0
					noofrating = 0

				cursor.execute("""SELECT COUNT(status) FROM product_rating WHERE
					product_code = %s and status = 'l' GROUP BY product_code""",(product_code))

				like_data = cursor.fetchone()
				if like_data:
					likes_count = like_data[0]
				else:
					likes_count = 0

				cursor.execute("""SELECT COUNT(status) FROM product_rating WHERE
					product_code = %s and status = 'd' GROUP BY product_code""",(product_code))

				dislike_data = cursor.fetchone()
				if dislike_data:
					dislike_count = dislike_data[0]
				else:
					dislike_count = 0

				cursor.execute("""SELECT `rating`,`status` FROM product_rating WHERE
					product_code = %s and `user_id` = %s""",(product_code,user_id))

				user_data = cursor.fetchall()
				if user_data:
					user_rating = user_data[0][0]
					user_vote = user_data[0][1]
				else:
					user_rating = 0
					user_vote = ""
				# print(user_data)
				details = {"avg_rating":avg_rating,
					"noofrating":noofrating,
					"likes_count":likes_count,
					"dislike_count":dislike_count,
					"user_rating":user_rating,
					"user_vote":user_vote}

		except:
			pass

		return ({"attributes": {
		    				"status_desc": "Product Rating Details.",
		    				"status": "success"
		    				},
		    				"responseList":details}), status.HTTP_200_OK

if __name__ == '__main__':
	app.run(host='0.0.0.0',debug=True)


# coding: utf-8

# In[1]:

import numpy as np
import matplotlib.pyplot as plt
from Random import RandomDist

# experiment parameters
Nmeas = 1
Nexp = 1000
sigma = 2

true = []
best = []

# instantiate Random class
random = RandomDist()

# Loop through different mu values
# use function in random.py returns a random pesudo integer using normal distribution (Box-muller method)
for i in range(-100,100):
    mu_true = i/10.0
    for e in range(0, Nexp):
        mu_best = 0
        for m in range(0, Nmeas):
            # random pesudo number
            x = float(random.normal(mu_true,sigma))
            mu_best += x

        mu_best = mu_best/Nmeas

        # append into array of mu values
        true.append(mu_true)
        best.append(mu_best)

# plotting
plt.figure(figsize=(10,8))
plt.hist2d(true, best, bins=[50,150])
plt.colorbar()
plt.ylim(-10,10)
plt.xlim(-10,10)
plt.xlabel('$\\mu$ True', fontsize = 15)
plt.ylabel('$\\mu$ measured',fontsize = 15)
plt.title('Neyman Construction of Gaussian',fontsize = 15)
plt.savefig('Neyman_Gaussian.png')
plt.show()


from typing import List, Dict
from data.utils.emojis import EMOJIS
import requests
import random
import sys
import logging

def get_json_data_from_url(url: str) -> List[Dict]:
    response = requests.get(url)
    if response.status_code == 200:
        return response.json()
    else:
        sys.exit(-1)

def get_shuffled_emojis() -> List:
    emojis = list(EMOJIS)
    random.shuffle(emojis)
    return emojis

def log_command_pressed(update):
    user = update.message.from_user
    username = user.username
    user_id = user.id
    first_name = user.first_name

    if update.message.location is not None:
        lat = update.message.location.latitude
        long = update.message.location.longitude
        logging.info(f"{username}/{first_name} ({user_id}) sent a location: {lat}, {long}.")
    else:
        command = update.message.text[:255]
        logging.info(f"{username}/{first_name} ({user_id}) sent {command}.")


import os
import configparser

from .singleton import Singleton

class Settings(metaclass=Singleton):
    _instance = None

    ROOT_PATH = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))

    """
    On which IP address do you want to run Dwata backend
    Default is localhost or 127.0.0.1
    """
    SERVER_HOST = "127.0.0.1"

    """
    On which port do you want the Dwata backend to listen to
    Lower port numbers may need root permissions on Linux systems
    Default is 45454
    """
    SERVER_PORT = 45454

    """
    The INI file to conifgure databases can be store in any path that Dwata backend can read
    Usually the same INI stores the configuration for SERVER_, INI_FILE_PATH and databases
    But you may separate them
    Please use an absolute path
    """
    DB_SETTINGS_FILE_PATH = os.path.join(
        os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
        "settings.ini"
    )

    def __init__(self, settings_str=None):
        p = configparser.ConfigParser()
        if settings_str is None:
            path = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
            settings_str = open(os.path.join(path, "settings.ini")).read()
        p.read_string(settings_str)

        if "settings" in p:
            self.SERVER_HOST = p["settings"].get("server_host", self.SERVER_HOST)
            self.SERVER_PORT = p["settings"].get("server_host", self.SERVER_PORT)
            self.DB_SETTINGS_FILE_PATH = p["settings"].get("server_host", self.DB_SETTINGS_FILE_PATH)

    def read_db_settings(self, settings_str=None):
        """
        This method reads the DB connection settings from the settings.ini file
        The sample file which may be used for this is settings.ini.template
        :param settings_str: Pass a string of settings in INI format (mainly for tests)
        :return: DB settings dict
        """
        databases = {}
        p = configparser.ConfigParser()
        if settings_str is None:
            path = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
            settings_str = open(self.DB_SETTINGS_FILE_PATH).read()

        p.read_string(settings_str)
        if "databases" in p:
            databases = dict((key[3:], value) for key, value in p["databases"].items()
                             if key[0:3] == "db_")

        return databases

    @property
    def databases(self):
        return self.read_db_settings()

settings = Settings()


import sys

import numpy as np
import pygame
from pygame.locals import (K_ESCAPE, K_RETURN, K_SPACE, KEYDOWN,
                           MOUSEBUTTONDOWN, MOUSEMOTION, QUIT)

pygame.init()
font = pygame.font.SysFont('Verdana', 30)
bg = (40, 40, 40)
lines = (70, 70, 70)
white = (255, 255, 255)

in_play_button = lambda x, y: 120 >= x >= 9 and 645 >= y >= 605
in_clear_button = lambda x, y: 257 >= x >= 135 and 645 >= y >= 605
in_step_button = lambda x, y: 388 >= x >= 275 and 645 >= y >= 605

class Button:
    __slots__ = ([
        'main_text', 'second_text', 'main_color', 'select_color', 'pos'
    ])

    def __init__(self,
                 main_text: str = '',
                 second_text: str = '',
                 main_color: tuple = (0, 100, 0),
                 select_color: tuple = (0, 255, 0),
                 pos: tuple = (0, 0)):
        self.main_text = main_text
        self.second_text = second_text
        self.main_color = main_color
        self.select_color = select_color
        self.pos = pos

    def __call__(self, text: bool = True, color: bool = True):
        if text:
            _text = self.main_text
        else:
            _text = self.second_text
        if color:
            _color = self.main_color
        else:
            _color = self.select_color

        return font.render(_text, True, white, _color)

class GameOfLife():

    def __init__(self):

        self.width = 1200
        self.height = 600
        self.square_size = 10

        self.rc = int(self.height / self.square_size)
        self.cc = int(self.width / self.square_size)

        self.game_board = np.zeros((self.rc, self.cc))

        # Buttons
        self.play_button = Button(' Start  ', ' Pause ', (0, 100, 0),
                                  (0, 255, 0), (10, 605))
        self.clear_button = Button('  Clear  ', '', (100, 0, 0), (255, 0, 0),
                                   (135, 605))
        self.step_button = Button('  Step  ', '', (0, 100, 0), (0, 255, 0),
                                  (275, 605))

        pygame.init()
        pygame.display.set_caption('Game of Life!!')
        self.screen = pygame.display.set_mode((self.width, self.height + 60))
        self.clock = pygame.time.Clock()
        self.update_game = False

    def __call__(self, update_game: bool = False):

        state = np.copy(self.game_board)

        for row in range(self.rc):
            for col in range(self.cc):
                if update_game:
                    life_cells = \
                        state[(row - 1) % self.rc, (col - 1) % self.cc] + \
                        state[(row - 1) % self.rc, col % self.cc] + \
                        state[(row - 1) % self.rc, (col + 1) % self.cc] + \
                        state[ row % self.rc, (col - 1) % self.cc] + \
                        state[ row % self.rc, (col + 1) % self.cc] + \
                        state[(row + 1) % self.rc, (col - 1) % self.cc] + \
                        state[(row + 1) % self.rc, col % self.cc] + \
                        state[(row + 1) % self.rc, (col + 1) % self.cc]

                    # Rules
                    if self.game_board[row, col] == 0 and life_cells == 3:
                        self.game_board[row, col] = 1

                    elif self.game_board[row, col] == 1 and (life_cells > 3 or
                                                             life_cells < 2):
                        self.game_board[row, col] = 0

                # Draw cells
                if self.game_board[row, col] == 0:
                    pygame.draw.rect(
                        self.screen, lines,
                        (col * self.square_size, row * self.square_size,
                         self.square_size, self.square_size), 1)
                else:
                    pygame.draw.rect(
                        self.screen, white,
                        (col * self.square_size, row * self.square_size,
                         self.square_size, self.square_size), 0)

    def quit_game(self):
        pygame.quit()
        sys.exit()

    def switch_update(self):
        self.update_game = not self.update_game
        self.pb = self.play_button(not self.update_game, True)

    def clear_screen(self):
        self.game_board = np.zeros((self.rc, self.cc))
        if self.update_game:
            self.switch_update()

    def events(self):
        for event in pygame.event.get():

            # Close game
            if event.type == QUIT:
                self.quit_game()

            elif event.type == KEYDOWN:
                keys = pygame.key.get_pressed()

                if keys[K_ESCAPE]:
                    self.quit_game()
                elif keys[K_SPACE]:
                    self.switch_update()
                elif keys[K_RETURN]:
                    self(True)

            elif event.type == MOUSEMOTION:
                x_pos, y_pos = pygame.mouse.get_pos()

                self.pb = self.play_button(not self.update_game,
                                           not in_play_button(x_pos, y_pos))
                self.cb = self.clear_button(True,
                                            not in_clear_button(x_pos, y_pos))
                self.sb = self.step_button(True,
                                           not in_step_button(x_pos, y_pos))

            clicks = pygame.mouse.get_pressed()
            if sum(clicks) == 0:
                continue
            x_pos, y_pos = pygame.mouse.get_pos()

            x_cell = int(np.floor(x_pos / self.square_size))
            y_cell = int(np.floor(y_pos / self.square_size))

            if clicks == (1, 0, 0):

                if in_play_button(x_pos, y_pos):
                    self.switch_update()
                elif in_clear_button(x_pos, y_pos):
                    self.clear_screen()
                elif in_step_button(x_pos, y_pos):
                    self(True)
                elif y_cell < self.rc:
                    self.game_board[y_cell, x_cell] = 1

            elif clicks == (0, 0, 1):

                if y_cell < self.rc:
                    self.game_board[y_cell, x_cell] = 0

        self.screen.blit(self.pb, self.play_button.pos)
        self.screen.blit(self.cb, self.clear_button.pos)
        self.screen.blit(self.sb, self.step_button.pos)

if __name__ == '__main__':
    game = GameOfLife()
    while True:
        game.clock.tick(15)
        game.screen.fill(bg)
        game.events()
        game(game.update_game)
        pygame.display.flip()


import discord
import botconfig
from discord.ext import commands

class Invitation(commands.Cog):
  def __init__(self, bot):
    self.bot = bot

  @commands.command(name='invite')
  async def hello(self, ctx, member: discord.Member = None):
    """Send the invitation's link in a DM"""
    member = member or ctx.author
    try:
      await member.send (botconfig.config['invite_bot'])
    except:
      await ctx.send ('Oups je ne peux pas envoyer de DM !')

  @commands.Cog.listener('on_message')
  async def invitation(self, message):
    """ Send the invitation's link in a DM
        if the word 'invitation' is found
    """
    if "invitation" in message.content.lower():
      member = message.author
      try:
        await member.send (botconfig.config['invite_bot'])
      except:
        await message.channel.send ('Oups je ne peux pas envoyer de DM !')


from pcooja import *
import random
import os
import datetime
import glob
#from custommotes import *

CoojaSimulation.set_contiki_path("/home/user/contiki-ng")

folder = CoojaSimulation.get_contiki_path()+"/examples"

import logging
logger.setLevel(logging.INFO)

def simulation_example1():
    csc_filepath=f"{folder}/rpl-udp/rpl-udp-cooja.csc"

    # Create a simulation object from an existing .csc file.
    simulation = CoojaSimulation.from_csc(csc_filepath)
    simulation.timeout = 10
    # Edit some settings (optional)
    simulation.success_ratio_rx = 1.0

    # Run the simulation and store the log and radio data in files
    simulation.run()

def simulation_example2():
    # Defining cooja mote types and firmware
    server = Z1MoteType(f"{folder}/rpl-udp/udp-server.c")
    client = Z1MoteType(f"{folder}/rpl-udp/udp-client.c")

    # Use a predefined topology that creates 1 server mote and 24 client motes
    topology = Topology.get_tree25_topology(Z1Mote, server, client)

    # Create a simulation with default RX/TX success ratio, seed and timeout values
    simulation = CoojaSimulation(topology, timeout=30)

    # Run the simulation and store the log and radio data in specific files
    simulation.run()

def simulation_example3():
    csc_filepath=f"{folder}/rpl-udp/rpl-udp-sky.csc"

    # Defining cooja mote types and firmware
    server = SkyMoteType(f"{folder}/rpl-udp/udp-server.c")
    client = SkyMoteType(f"{folder}/rpl-udp/udp-client.c")

    # Use topology found in .csc file, mote types are not preserved
    topology = Topology.from_csc(csc_filepath)

    # Set mote types
    topology.set_mote_type(1,server)
    topology.set_mote_type([2,3,4,5,6,7,8], client)

    # Create a simulation with default RX/TX success ratio, seed and timeout values
    simulation = CoojaSimulation(topology, timeout=120)

    # Just for information, it is possible to compile a firmware
    # Compile Server firmware, if it does not exist
    server.compile_firmware()

    logger.setLevel(logging.DEBUG)

    # Compile (and override) Client firmware
    client.compile_firmware(clean=True)

    # Run the simulation and store the log in specific file
    simulation.run(log_file="output.log")

def simulation_example4():
    # Defining cooja mote types and firmware
    server = SkyMoteType(f"{folder}/rpl-udp/udp-server.c")
    client = SkyMoteType(f"{folder}/rpl-udp/udp-client.c")

    # Create a random topology
    n = 10
    width=200
    height=200

    motes=[]
    current_mote_id=1
    for i in range(n):
        x = random.random()*width-width/2
        y = random.random()*height-height/2

        if current_mote_id == 1:
            mote_type = server
        else:
            mote_type = client

        new_mote = SkyMote(current_mote_id,x,y, mote_type)
        motes.append(new_mote)
        current_mote_id += 1

    medium = radio.UDGM(transmitting_range=50, interference_range=70)
    topology = Topology(motes, radiomedium=medium)

    # Create a simulation
    simulation = CoojaSimulation(topology, seed=15468, timeout=100)

    # Run the simulation
    simulation.run(with_gui=True)

def simulation_example5():
    """
    Export simulation.
    """
    csc_filepath=f"{folder}/rpl-udp/rpl-udp-sky.csc"

    # Create a simulation object from an existing .csc file.
    simulation = CoojaSimulation.from_csc(csc_filepath)

    # Run simulation in Cooja with GUI.
    simulation.export()

def simulation_example6():
    # Defining/overriding project configuration in 'project-conf.h'
    project_conf = ProjectConf(RPL_CONF_MAX_INSTANCES=1,
                                NBR_TABLE_CONF_MAX_NEIGHBORS=8,
                                IEEE802154_CONF_PANID=0x42,
                                LOG_CONF_LEVEL_IPV6="LOG_LEVEL_DBG")

    makefile_variables = {"MAKE_ROUTING":"MAKE_ROUTING_RPL_LITE",
                          "MAKE_MAC":"MAKE_MAC_CSMA"}

    # Firmwares
    server = Z1MoteType(f"{folder}/rpl-udp/udp-server.c",
                           project_conf=project_conf,
                           makefile_variables=makefile_variables)
    client = Z1MoteType(f"{folder}/rpl-udp/udp-client.c",
                           project_conf=project_conf,
                           makefile_variables=makefile_variables)

    # Use a predefined topology that creates 1 server mote and 24 client motes
    topology = Topology.get_tree25_topology(Z1Mote, server, client)

    # Create a simulation with default RX/TX success ratio, seed and timeout values
    simulation = CoojaSimulation(topology, timeout=30)

    # Run the simulation and store the log and radio data in specific files
    simulation.run(with_gui=True)

def simulation_example7():
    # Defining/overriding project configuration in 'project-conf.h'
    project_conf = ProjectConf(RPL_CONF_MAX_INSTANCES=1,
                                NBR_TABLE_CONF_MAX_NEIGHBORS=8,
                                IEEE802154_CONF_PANID=0x42,
                                LOG_CONF_LEVEL_RPL="LOG_LEVEL_DBG")

    # Firmwares
    server = CoojaMoteType(f"{folder}/rpl-udp/udp-server.c",
                           project_conf=project_conf)
    client = CoojaMoteType(f"{folder}/rpl-udp/udp-client.c",
                           project_conf=project_conf)

    topology = Topology.get_tree25_topology(CoojaMote, server, client)
    simulation = CoojaSimulation(topology, timeout=100)

    simulation.run()

    log = Log(simulation.get_log_filepath())
    messages = log.get_messages()

    send_requests = log.get_messages(contain="Sending request")
    received_requests = log.get_messages(contain="Received request")
    send_responses = log.get_messages(contain="Sending response")
    received_responses = log.get_messages(contain="Received response")

    print(f"requests sent: {len(send_requests)}")
    print(f"requests received: {len(received_requests)}")
    print(f"responses sent: {len(send_responses)}")
    print(f"responses received: {len(received_responses)}")

    print("")

    app_log = log.get_messages(log_module="App", node_id=4)
    for message in app_log[:5]:
        Log.print_message(message)

    print("")

    errors_log = log.get_messages(log_level=Log.LEVEL_ERR, node_id=5)
    for message in errors_log:
        Log.print_message(message)

def simulation_example8():
    """
    Run simulations on multiple threads
    """
    filepath=folder+"/rpl-udp/rpl-udp-cooja.csc"

    logger.setLevel(logging.ERROR)

    # Thread dispatcher
    simulation_worker = CoojaSimulationWorker(4)

    rx_list = [0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
    for rx in rx_list:
        # Create a simulation object from an existing .csc file.
        simulation = CoojaSimulation.from_csc(filepath)
        simulation.topology.radiomedium.success_ratio_rx = rx
        simulation.timeout = 100
        simulation.title = f"Simulation RX={rx}"

        # arguments that would normally be passed to simulation.run()
        args={'pcap_file':'simulation_'+str(rx)+".pcap", 'log_file':'simulation_'+str(rx)+".log"}

        # Add simulation to worker that dispatchs to a thread
        simulation_worker.add_simulation(simulation, run_args=args)

    # Run simulations on multiple threads
    simulation_worker.run()

def simulation_example9():
    """
    Setting startup delay for each mote
    """
    filepath=folder+"/rpl-udp/rpl-udp.csc"

    # Create a simulation object from an existing .csc file.
    simulation = CoojaSimulation.from_csc(filepath)

    for mote in simulation.topology:
        # Generate random delay between 5000 and 10000 milliseconds
        delay = 5000 + random.random()*(10000-5000)
        mote.startup_delay = delay

    # Run the simulation and store the log and radio data in files
    simulation.run(with_gui=True)

if __name__ == "__main__":
    simulation_example7()


import random
import math
import collections

import numpy as np
from PIL import Image
import torch
from torchvision.transforms.functional import _get_inverse_affine_matrix
import torch.nn.functional as F

def get_params(degree, translate, scale_ranges, shears):
    """Get parameters for affine transformation
    Returns:
        sequence: params to be passed to the affine transformation
    """
    angle = random.uniform(-degree, degree)

    if translate is not None:
        translations = (random.uniform(-translate, translate),
                        random.uniform(-translate, translate))
    else:
        translations = (0, 0)

    if scale_ranges is not None:
        scale = random.uniform(scale_ranges[0], scale_ranges[1])
    else:
        scale = 1.0

    if shears is not None:
        shear = (random.uniform(shears[0], shears[1]), 0.0)
    else:
        shear = (0.0, 0.0)

    return angle, translations, scale, shear

def get_transformed_imgs(img, degrees, translate, scale, shear, resample, fillcolor):

    # affine transformation on image1
    ret1 = get_params(degrees, (translate[0]*0.6, translate[1]*0.6), scale, (shear[0]*0.5, shear[1]*0.5), img.size)
    output_size = img.size
    center1 = (img.size[0] * 0.5 + 0.5, img.size[1] * 0.5 + 0.5)
    matrix1 = _get_inverse_affine_matrix(center1, *ret1)
    img1 = img.transform(output_size, Image.AFFINE, matrix1, resample, fillcolor=fillcolor)

    # affine transformation on image2
    ret2 = get_params(degrees, translate, scale, shear, img.size)
    center2 = (img.size[0] * 0.5 + 0.5 + ret1[1][0], img.size[1] * 0.5 + 0.5 + ret1[1][1])
    matrix2 = _get_inverse_affine_matrix(center2, *ret2)
    img2 = img1.transform(output_size, Image.AFFINE, matrix2, resample, fillcolor=fillcolor)

    aff_para = [math.cos(math.radians(ret2[0])),
                math.sin(math.radians(ret2[0])),
                ret2[1][0]/translate[0]/output_size[0],
                ret2[1][1]/translate[1]/output_size[1],
                ret2[2]*2./(scale[1]-scale[0])-(scale[0]+scale[1])/(scale[1]-scale[0]),
                ret2[3][0]*2./(shear[1]-shear[0])-(shear[0]+shear[1])/(shear[1]-shear[0])]

    return img1, img2, aff_para

def get_ex_aff_para(aff_para, translate, scale, shear, img_size):
    degree_cos = math.degrees(math.acos(aff_para[0]))
    degree_sin = math.degrees(math.asin(aff_para[1]))
    if degree_sin < 0:
        degree_cos *= -1

    ex_aff_para = [(degree_cos + degree_sin) / 2,
                (round(aff_para[2]*img_size[0]*translate[0]), round(aff_para[3]*img_size[1]*translate[1])),
                (aff_para[4]*(scale[1]-scale[0])+(scale[0]+scale[1]))/2.,
                ((aff_para[5]*(shear[1]-shear[0])+(shear[0]+shear[1]))/2., 0.0)]

    return ex_aff_para

def get_paired_imgs(img_p, img_n, degrees, translate, scale, shear, resample, fillcolor):
    img_p1, img_p2, aff_para = get_transformed_imgs(img_p, degrees, translate, scale, shear, resample, fillcolor)
    img_n1, _, _ = get_transformed_imgs(img_n, degrees, translate, scale, shear, resample, fillcolor)
    if random.random() > 0.5:
        target = [1.0,] + aff_para
        return img_p1, img_p2, target
    else:
        target = [0.0,] * 7
        return img_n1, img_p2, target

def get_labels_to_indices(labels):
    """
    Creates labels_to_indices, which is a dictionary mapping each label
    to a numpy array of indices that will be used to index into self.dataset
    """
    if torch.is_tensor(labels):
        labels = labels.cpu().numpy()
    labels_to_indices = collections.defaultdict(list)
    for i, label in enumerate(labels):
        labels_to_indices[label].append(i)
    for k, v in labels_to_indices.items():
        labels_to_indices[k] = np.array(v, dtype=np.int)
    return labels_to_indices

def get_transformed_imgs_2(batch_size, degrees, imgs_t):
    matrix = torch.zeros(batch_size, 2, 3)

    for i, degree in enumerate(degrees):
        c = math.cos(math.radians(degree.item()))
        s = math.sin(math.radians(degree.item()))
        matrix[i,:,:2] = torch.Tensor([[c, -s],[s, c]])

    affine_t = F.affine_grid(matrix, imgs_t.size(), align_corners=False)
    imgs_1 = F.grid_sample(imgs_t, affine_t, mode='bilinear', align_corners=False)

    return imgs_1

def get_mask(img_size):
    x = np.arange(0, img_size[0], 1, np.float32)
    y = np.arange(0, img_size[1], 1, np.float32)
    y = y[:, np.newaxis]
    mask = (((x - img_size[0]/2 + 0.5) ** 2 + (y - img_size[1]/2 + 0.5) ** 2) < (img_size[0]/2) ** 2).astype(np.uint8)
    return torch.from_numpy(mask)


"""
dynamics_tests.py
Description:
    Tests all of the functions related to the pusher slider's dynamics.
"""

import sys, unittest

import jax.numpy as jnp

sys.path.append('../../../')
from src.python.pusher_slider import PusherSliderSystem

class PusherSliderTest2(unittest.TestCase):
    """
    test_get_motion_cone1
    Description:
        Tests that the motion cone computation works as I expect it to.
    """
    def test_get_motion_cone1(self):

        # Constants
        ps = PusherSliderSystem()
        ps.st_cof = 1.0
        ps.s_width = 1.0 # => c = 2
        ps.p_x = 3.0
        ps.p_y = 5.0

        # Algorithm
        gamma_t_expected = (4.0-15.0+1.0*jnp.power(3,2))/(4.0+jnp.power(5.,2)-1*3*5)
        gamma_b_expected = (-4.0-15.0-1*jnp.power(3,2))/(4.0+jnp.power(5.0,2)+1*3*5)

        gt_2, gb_2 = ps.get_motion_cone_vectors()

        self.assertEqual(gamma_t_expected,gt_2)
        self.assertEqual(gamma_b_expected,gb_2)

    """
    test_identify_mode1
    Description:
        Testing the identification of modes from a good input and
        the motion cone computation.
        The input u should be leading to STICKING.
    """
    def test_identify_mode1(self):

        # Constants
        ps = PusherSliderSystem()
        ps.st_cof = 1.0
        ps.s_width = 0.4 # => c = 5
        ps.p_x = 2.0
        ps.p_y = 2.0

        # Algorithm
        mode_out1 = ps.identify_mode(jnp.array([[2.0],[0.5]]))

        self.assertEqual(mode_out1,'Sticking')

    """
    test_identify_mode2
    Description:
        Testing the identification of modes from a good input and
        the motion cone computation.
        The input u should be leading to SLIDING UP.
    """
    def test_identify_mode2(self):

        # Constants
        ps = PusherSliderSystem()
        ps.st_cof = 1.0
        ps.s_width = 0.4 # => c = 5
        ps.p_x = 2.0
        ps.p_y = 2.0

        # Algorithm
        mode_out1 = ps.identify_mode(jnp.array([[2.0],[3.0]]))

        self.assertEqual(mode_out1,'SlidingUp')

    """
    test_identify_mode3
    Description:
        Making sure that this sketch of a plotting algorithm works.
    """
    def test_identify_mode3(self):

        # Constants
        ps = PusherSliderSystem()
        ps.st_cof = 1.0
        ps.s_width = 0.4 # => c = 5
        ps.p_x = 2.0
        ps.p_y = 2.0

        # Algorithm
        mode_out1 = ps.identify_mode(jnp.array([[2.0],[-2.5]]))

        self.assertEqual(mode_out1,'SlidingDown')

    """
    test_C1
    Description:
        Tests that the C matrix is constructed correctly.
    """
    def test_C1(self):
        # constants
        ps = PusherSliderSystem()
        ps.s_theta = jnp.pi/6

        # Algorithm
        C = ps.C()

        self.assertEqual(C[0][0],jnp.cos(jnp.pi/6))
        self.assertEqual(C[0][1],jnp.sin(jnp.pi/6))
        self.assertEqual(C[1][0],-jnp.sin(jnp.pi/6))
        self.assertEqual(C[1][1],jnp.cos(jnp.pi/6))

    """
    test_Q1
    Description:
        Tests that the Q matrix is constructed correctly.
    """
    def test_Q1(self):
        # constants
        ps = PusherSliderSystem()
        ps.ps_cof = 1
        ps.s_width = 0.4 # c = 5
        ps.p_x = 2.0
        ps.p_y = 2.0

        # Algorithm
        Q = ps.Q()

        self.assertEqual(Q[0][0],(1/(25.0+8))*(25+4))
        self.assertEqual(Q[0][1],(1/(25.0+8))*(4))
        self.assertEqual(Q[1][0],(1/(25.0+8))*(4))
        self.assertEqual(Q[1][1],(1/(25.0+8))*(25+4))

    """
    test_f1
    Description:
    	Tests the f() function so that we know that the state of the pusher slider is not modified
		after evaluating f at a given point.
    """
    def test_f1(self):
        # Constants
        ps = PusherSliderSystem()

        x0 = ps.x()
        u_prime = jnp.array([[1.0], [3.0]])
        x1 = x0 + jnp.array([[0.1], [0.0], [0.0], [0.0]])

        x_dot = ps.f(x1,u_prime)
        x_dot2 = ps.f2(x1,u_prime) # This u should call

        x_dim = x_dot.shape[1]
        for x_dim_index in range(x_dim):
            # Constraints
            self.assertEqual(x_dot[0,x_dim_index],x_dot[0,x_dim_index])

if __name__ == '__main__':
    unittest.main()


from datetime import datetime
from ..models import Comment, Bug, ProjectMember
from .schemas import CommentIn, CommentUpdate

async def read_comment(comment_id: str) -> Comment:
    """A function to read a comment from the database"""
    try:
        comment = await Comment.get(comment_id, fetch_links=True)
        if not comment:
            raise Exception("Comment not found")
        return comment
    except Exception as e:
        raise e

async def write_comment(comment: CommentIn, bug: Bug, author: ProjectMember) -> Comment:
    """A function to write a comment to the database"""
    try:
        comment = Comment(
            **comment.dict(),
            bug=bug,
            author=author,
        )
        await comment.save()
        return await read_comment(comment.id)
    except Exception as e:
        raise e

async def read_comments(bug: Bug) -> list[Comment]:
    """A function to read comments from the database"""
    try:
        cmnts = [
            cmnt for cmnt in await Comment.find().to_list() if cmnt.bug.ref.id == bug.id
        ]
        for cmnt in cmnts:
            await cmnt.fetch_all_links()
        return cmnts

    except Exception as e:
        raise e

async def edit_comment(comment: Comment, comment_update: CommentUpdate) -> Comment:
    """A function to edit a comment"""
    try:
        comment.content = comment_update.content
        comment.updated_at = datetime.utcnow()
        await comment.save()
        return await read_comment(comment.id)
    except Exception as e:
        raise e

async def remove_comment(comment: Comment) -> None:
    """A function to remove a comment"""
    try:
        await comment.delete()
    except Exception as e:
        raise e


# Generic
import unittest

# Libs
from flask_testing import TestCase

# Own
from baking.manage import app
#from flask import current_app as app

class TestSongs(TestCase):
    def create_app(self):
        app.config.from_object('config.testing')
        return app

class TestSongEndPoints(TestSongs):
    def test_song_create(self):
        import json

        headers = {'content-type': 'application/json'}
        data = {"title": "gloria",
                "lyrics": "gloria patri et filio et spiritui sancto, sicut erat in principio et nunc et semper et in saecula saeculorum amen",
                "pub_date": "1987-12-05T00:00:00", "artist_id": 1, "country": "US"}

        response = self.client.post('api/v1/songs/', data=json.dumps(data), headers=headers)
        print('\n\nResponse: ' + str(response.status_code))
        self.assertEqual(response.status_code, 201)

    def test_song_get(self):
        from baking.main.database.models import Song
        from baking.main.v1.endpoints.songs import song
        from flask_restplus import marshal

        id_testing = 1
        g = marshal(Song.query.filter(Song.id == id_testing).one(), song)

        response = self.client.get('/api/v1/songs/{}'.format(id_testing))
        print('\n\nResponse: ' + str(response.json))
        self.assertEqual(response.json, g)

    def test_song_update(self):
        from baking.main.database.models import Song
        from baking.main import db

        import json

        data = {"artist_id": 1, "title": "Amen", "lyrics": "I heard from a friend of a friend"}
        headers = {'content-type': 'application/json'}
        id_testing = db.session.query(db.func.max(Song.id)).scalar()
        response = self.client.put('api/v1/songs/{}'.format(id_testing), data=json.dumps(data), headers=headers)
        print('\n\nResponse: ' + str(response.status_code))
        self.assertEqual(response.status_code, 204)

    def test_song_delete(self):
        from baking.main.database.models import Song
        from baking.main import db

        id_testing = db.session.query(db.func.max(Song.id)).scalar()
        response = self.client.delete('api/v1/songs/{}'.format(id_testing))
        print('\n\nResponse: ' + str(response.status_code))
        self.assertEqual(response.status_code, 204)

if __name__ == '__main__':
    unittest.main()


import cv2
import time
import os
import RPi.GPIO as GPIO
import numpy as np

def cropping(frame):
    ## ROI should ideally only be of black background

    ## select roi and close window
    cv2.namedWindow("SELECT ROI", cv2.WINDOW_NORMAL)
    ROI = cv2.selectROI("SELECT ROI", frame, showCrosshair=True)

    x, y, w, h = ROI[0], ROI[1], ROI[2], ROI[3]
    croped_img = frame[y:y+h,x:x+w]
    cv2.destroyWindow("SELECT ROI")
    return croped_img, ROI

def save_init_ROIs(path, camera_name, manual_path = None):
    if manual_path == None:
        curent_path = path + "/" + camera_name
        img_list = os.listdir(curent_path)
        img_list.sort()
        full_path = curent_path + "/" + img_list[-1]
        print(full_path)
    else:
        full_path = manual_path
    img = cv2.imread(full_path)

    half_width = int(img.shape[1]/2)

    img_1 = img[:: , :half_width]
    img_2 = img[:: , half_width:]
    img_halfes = [img_1, img_2]

    ROIs_lst = []
    for i in range(2):
        croped_img, ROI = cropping(img_halfes[i])
        out_path = path + f"/ROI_{camera_name}_{i+1}.jpg"
        cv2.imwrite(out_path, croped_img)
        cv2.destroyAllWindows()
        ROIs_lst.append(ROI)

    return ROIs_lst

def compare_2(croped_img,bckgound_img):
    """compare 2 imgs and return the number of very different pixls"""

    gray_bkgr = cv2.cvtColor(bckgound_img, cv2.COLOR_BGR2GRAY)
    gray_curr = cv2.cvtColor(croped_img, cv2.COLOR_BGR2GRAY)

    diff = cv2.absdiff(gray_curr, gray_bkgr)
    ret,thresh1 = cv2.threshold(diff,27,255,cv2.THRESH_BINARY)

    pix_num = len(np.where(thresh1>0)[0])
    return pix_num

def compare_to_prev(path, camera_name, ROIs, light_dct):
    curent_path = path + "/" + camera_name
    img_list = os.listdir(curent_path)
    img_list.sort()
    full_path = curent_path + "/" + img_list[-1]
    img = cv2.imread(full_path)
    half_width = int(img.shape[1]/2)

    img_1 = img[:: , :half_width]
    img_2 = img[:: , half_width:]
    img_halfes = [img_1, img_2]

    for i in range(2):
        curr_img = img_halfes[i]
        ROI = ROIs[i]
        x, y, w, h = ROI[0], ROI[1], ROI[2], ROI[3]
        croped_img = curr_img[y:y+h,x:x+w]
        bckgrnd_path = path + f"/ROI_{camera_name}_{i+1}.jpg"
        bckgound_img = cv2.imread(bckgrnd_path)

        diff_pix_num = compare_2(croped_img, bckgound_img)

        if diff_pix_num>200:
            GPIO.output(light_dct[f"{camera_name}_{i+1}"], 1) ## turn off trhe light
            print(f"light off{camera_name}_{i+1}")
        else:
            GPIO.output(light_dct[f"{camera_name}_{i+1}"], 0)

            ## overwrite the old background_img with the updated one
            ## (we do this to take care of noise)
#             cv2.imwrite(bckgrnd_path, croped_img)
            ## since the plant will also move slowly it might not make sense to compare to prev img - we need to comp to first img
            ## lets hope the nois does not get too bad

def get_ROIs_for_all_cams(cam_lst = ["A","B","C","D"]):
    path = "/home/pi/Desktop/agueda_imgs/__new_imgs__"
    ROI_dct = {}
    for camera_name in cam_lst:
        ROIs = save_init_ROIs(path, camera_name)
        ROI_dct[camera_name] = ROIs

    return ROI_dct

def loop_through_cams(ROI_dct):

    path = "/home/pi/Desktop/agueda_imgs/__new_imgs__"

    ## update this dct when actually using for then one box!
    light_dct = {"A_1":L1, "A_2":L2, "B_1":L3, "B_2":L3, "C_1":L3, "C_2":L3,"D_1":L3,"D_2":L3}
    for camera_name in list(ROI_dct.keys()):
        ROIs = ROI_dct[camera_name]
        compare_to_prev(path, camera_name, ROIs, light_dct)

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

L1 = 33
L2 = 40
L3 = 38 #...

GPIO.setup(L1, GPIO.OUT)
GPIO.setup(L2, GPIO.OUT)
GPIO.setup(L3, GPIO.OUT) #(dont forget to add the rest!)

# ROIs = save_init_ROIs(path, camera_name)
# ROI_dct = get_ROIs_for_all_cams()
# ROI_dct = {'A': [(217, 263, 147, 226), (239, 276, 55, 138)], 'B': [(365, 448, 86, 79), (220, 231, 63, 43)], 'C': [(180, 149, 56, 43), (152, 131, 64, 52)], 'D': [(128, 103, 62, 58), (80, 53, 101, 86)]}
# loop_through_cams(ROI_dct)


import pytest
from dbt.tests.adapter.utils.test_last_day import BaseLastDay

class TestLastDay(BaseLastDay):
    @pytest.fixture(scope="class")
    def project_config_update(self):
        return {
            "name": "test",
            "seeds": {
                "test": {
                    "data_last_day": {
                        "+column_types": {
                            "date_day": "Nullable(Date)",
                            "result": "Nullable(Date)",
                        },
                    },
                },
            },
        }


import streamlit as st

# Establece el estilo de la barra lateral
st.markdown(
    """
<style>
.sidebar .sidebar-content {
    background-image: linear-gradient(#2e7bcf,#2e7bcf);
    color: white;
}
</style>
""",
    unsafe_allow_html=True,
)

from predict_page import show_predict_page
from explore_page import show_explore_page

options = ("Predecir", "Explorar")
page = st.sidebar.selectbox("Elija una opción", options)

if page == "Predecir":
    show_predict_page()
else:
    show_explore_page()


import inspect
import weakref
from functools import partial
from typing import Callable, Generic, TypeVar

from ._key import bundle_key
from ..exceptions import RequirementNotFound, AmbiguousDependency
from ..extras import clsname
from ..extras.generics import is_generic_type, get_origin, resolve_type
from ..extras.logging import logger
from ..extras.undef import Undefined, default

class _RebindContext(object):
    def __init__(self, bundle):
        self._bundle = bundle

    @property
    def parent(self):
        return Undefined

    def __getattr__(self, item):
        return object.__getattribute__(self, "_bundle").__getattribute__(item)

class BindMeta(type):

    def __getitem__(cls, item) -> 'Binding':
        c_local = cls.caller_local()
        bundle = c_local.get(bundle_key, Undefined)
        if bundle is Undefined:
            from compose import FactoryBundle
            c_local[bundle_key] = FactoryBundle()
            bundle = c_local[bundle_key]
        binding = None
        if cls == ReBind:
            binding = list(bundle.find(item, _RebindContext(bundle)))
            if len(binding):
                binding = binding[0]
            else:
                binding = None
                logger.warning("Rebind could not find previous binding. Binding instead")

        if binding is None:
            binding = Binding(item)
            bundle.visit(binding)

        return binding

    def caller_local(cls):
        stack = inspect.stack()
        for frame_info in stack:
            if frame_info.frame.f_locals.get("cls", None) != cls:
                return frame_info.frame.f_locals
        return {}

class Bind(object, metaclass=BindMeta):
    """Binding creates a whole new binding This is generally what should be used  we setting up a configuration
    with Compose

    """
    pass

class ReBind(object, metaclass=BindMeta):
    """Rebind will attempt to find previously configured bindings and update the factory.

     This can be useful in scenarios where a higher level framework wants to configure scopingbut allow
     the lower level application to define the binding itself. It can also be used to allow for a default binding
     to be overridden later but the more acceptable way to do that is to have mmultiple bundles since bundles cascade
     only when a bundle does not provide and instance.

    """
    pass

T = TypeVar("T", bound=type)

class Binding(Generic[T]):

    def __init__(self, bind: T):
        self.is_singleton = False
        self._as_singleton = False
        self._config_for = bind
        self._factory = None
        self._additional_check = lambda *a: True
        self.provide_type = None

    def to_multiple(self, *factories):
        for factory in factories:
            self._bundle.add_binding(Binding(self._config_for).to(factory))
        self._bundle.remove_binding(self)
        self._bundle = None
        return None

    def to(self, factory: Callable):
        if isinstance(factory, list):
            raise ValueError("Each factory binding must be its own binding, use to_multiple for an easier syntax")
        if not callable(factory) and not isinstance(factory, str):
            raise RuntimeError("NOT CALLABLE!!!")
        self._factory = factory
        if isinstance(factory, (type, str)):
            self.provide_type = factory
        else:
            self.provide_type = inspect.signature(factory).return_annotation
        if self.provide_type is inspect.Signature.empty:
            self.provide_type = Undefined
            msg = f"Provided factory does not specify a return type in the annotation, some providers rely on this" \
                  f" to detirmine if they provide a class or not. "
            logger.warning(msg)
        return self

    def on_target(self, target):
        def check(kls, context: 'InstantiationContext'):
            for node in list(context.instantiation_chain()):
                if node.provider.provide_type == target:
                    return True
            return False

        return check

    def on(self, predicate) -> 'Binding':
        self._additional_check = predicate
        if isinstance(predicate, type):
            self._additional_check = self.on_target(predicate)
        return self

    def to_self(self) -> 'Binding':
        return self.to(self._config_for)

    def as_singleton(self):
        self._as_singleton = True
        return self

    def accept(self, bundle: 'ComposeBundle'):
        self._bundle = weakref.proxy(bundle)
        bundle.add_binding(self)

    def with_args(self, *args, **kwargs):
        self._factory = partial(self._factory, *args, **kwargs)
        return self

    @property
    def factory(self):
        if isinstance(self._factory, str):
            for sub in self._config_for.__subclasses__():
                if clsname(sub, full=True) == self._factory:
                    return sub
            raise RequirementNotFound(f"No Subclass discovered by name {self._factory}", self._factory, Undefined)
        else:
            return self._factory

    def provides(self, kls, templates=None, context=Undefined):
        provides = False
        if is_generic_type(kls) and get_origin(kls) is type:
            if self._config_for == kls:
                provides = True
        if not provides:
            try:
                if isinstance(kls, type) and issubclass(kls, self._config_for):
                    if not templates or all(issubclass(ot, resolve_type(self._config_for, t)) for t, ot in templates):
                        provides = True
            except TypeError as ex:
                if isinstance(kls, TypeVar):
                    msg = f"A generic type was found and can not be resolved. This usually happens when a property on " \
                          f"a dataclass is typed with a generic class without specifying the types of the generics. For " \
                          f"example my_prop:Mapping = field(default=None). Compose can not resolve Mapping, mapping of what?"
                    raise AmbiguousDependency(msg, context)
                raise ex
        if provides:
            if isinstance(self.provide_type, str):
                self.provide_type = self.factory
            return self._additional_check(kls, default(context.parent, context))
        return False

    @property
    def bound_to(self):
        return self._config_for


#!/usr/bin/env python

import os, argparse, logging
from ts3observer.cli import CommandLineInterface as Cli
from ts3observer.gui import GraphicalUserInterface as Gui
from ts3observer.utils import path
from ts3observer.exc import CriticalException, ShutDownException, print_traceback, print_buginfo

class Dispatcher(object):
    ''' Dispatch the task to the right module '''

    def __init__(self):
        self._parse_arguments()

    def _parse_arguments(self):
        ''' Parse the arguments from commandline '''
        parser = argparse.ArgumentParser()
        sub_parser = parser.add_subparsers(dest='task')

        parser.add_argument('-v', '--verbose', action='store_true', help='Increase verbosity for debugging purpose')
        parser.add_argument('-q', '--quiet', action='store_true', help='Only show messaged if there is an critical Exception')
        parser.add_argument('-g', '--graphical', action='store_true', help='Run the ts3observer as Gui')
        parser.add_argument('-d', '--dev', action='store_true', help='Run in developer modus')

        utils_parser = sub_parser.add_parser('utils', help='Need some help?')
        utils_sub_parser = utils_parser.add_subparsers(dest='utils')

        utils_grouplist = utils_sub_parser.add_parser('servergrouplist', help='List all servergroups')
        utils_channellist = utils_sub_parser.add_parser('channellist', help='List all channels')
        utils_clientlist = utils_sub_parser.add_parser('clientlist', help='List all connected clients')
        utils_clientlist.add_argument('-a', '--advanced', action='store_true', help='Get more information about the connected clients')

        run_parser = sub_parser.add_parser('run', help='Run the ts3observer')
        version_parser = sub_parser.add_parser('version', help='Shows the ts3observer version')

        ts3o.args = parser.parse_args()

    def dispatch(self):
        ''' Dispatch the task to the right module '''
        if ts3o.args.graphical:
            getattr(Gui(), ts3o.args.task)()
        else:
            getattr(Cli(), ts3o.args.task)()

class Ts3o(object):
    ''' Define a holder class '''
    pass

def _setup():
    ''' Define some globals for ts3observer '''
    __builtins__.ts3o = Ts3o()
    ts3o.base_path = os.path.abspath(os.path.dirname(__file__))

def _run():
    try:
        _setup()
        Dispatcher().dispatch()

    except ShutDownException as e:
        logging.info('Good Bye!')

    except CriticalException as e:
        if ts3o.args.verbose:
            print_traceback()
        logging.critical('{}: {}'.format(e.__class__.__name__, str(e)))

    except Exception as e:
        print_traceback()
        logging.critical('{}: {}'.format(e.__class__.__name__, str(e)))
        print_buginfo()

if __name__ == '__main__':
    _run()
else:
    raise Exception('Please, run this script directly!')


# created at 2018-09-28

# Author:   coneypo
# Blog:     http://www.cnblogs.com/AdaminXie
# GitHub:   https://github.com/coneypo/Vericode_decoder

# 从网站爬取验证码

import cv2
import numpy as np

from urllib import request
from PIL import Image
from reco_vericode import tools

########## web spider to get vericode ########

path_save = "../data/images/for_test/"

# url_vericode = "https://portal1.ecnu.edu.cn/cas/code"
url_vericode = "http://lxs.ecnu.edu.cn/EN/book.php?act=chkcode"

# 请求响应获取图片
request.urlretrieve(url_vericode, path_save + "test.jpg")

# 存储为 jpg
img_get = Image.open(path_save + "test.jpg")
print(img_get.size)
img_PIL = Image.new("RGB", img_get.size, (255, 255, 255))
img_PIL.paste(img_get)

# convert PIL to OpenCv
img_PIL = img_PIL.convert('RGB')
img_cv2 = np.array(img_PIL)
img_cv2 = img_cv2[:, :, ::-1].copy()

############# decode vericode  #############
decoded_num = tools.decode_vericode(img_cv2)

# print(img_cv2.shape)
cv2.putText(img_cv2, str(decoded_num), (20, 52), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)

cv2.imshow("img_show", img_cv2)
cv2.namedWindow("img_show")
cv2.waitKey(0)


from django import forms

from forms_demos.web.models import Person

class NameForm(forms.Form):
    name = forms.CharField(
        max_length=30,
        label='Your name:',
        help_text='Please enter your name',
        widget=forms.TextInput(
            attrs={
                'placeholder': 'Вашето име...',
            },
        )
    )
    age = forms.IntegerField(
        label='Age',
        help_text='Please enter your age',
    )

    married = forms.BooleanField(required=False)

    email = forms.CharField(
        widget=forms.EmailInput(),
    )

class NameForm2(forms.ModelForm):
    class Meta:
        model = Person
        fields = '__all__'


import discord
from discord.ext import commands
from utils.utils import con
import random

class UnloadCMD(commands.Cog):

    def __init__(self, bot):
        self.bot = bot
    @commands.command(hidden=True)
    @commands.is_owner()
    async def unload(self, ctx, *, cog: str):
        con.log(f'{ctx.author.name} ({ctx.author.id}) is attempting to unload extension {cog}...')
        try:
            self.bot.unload_extension(cog)
            con.log(f'Successfully unloaded extension {cog}.')
        except Exception as e:
            embed = discord.Embed(title='Error', description=str(e), color=0xff0000)
            await ctx.send(embed=embed)
            con.log(f"Couldn't unload extension {cog}: {e}")
        else:
            color=random.randint(1, 16777215)
            embed = discord.Embed(title='Success', description=f'Successfully unloaded extension {cog}', color=color)
            await ctx.send(embed=embed)

def setup(bot):
    bot.add_cog(UnloadCMD(bot))


#import matplotlib.pyplot as plt
import numpy as np
from astropy import units as u
from astropy.io import ascii
from astropy.table import QTable

def unit_check_convert(var, myunit):
    # Check variable units.
    # Add unit if none. If mismatch: convert if possible; error if incompatible.
    #
    # Inputs:
    # var = variable to check. Single value or array.
    # myunit = desired unit in astropy unit format (eg: u.degree)
    #
    # Output:
    # astropy quantity with specified unit. If any quantity in an input array can't be converted,
    #   will error even if other entries are OK.

    try:
        myunit.decompose()
    except:
        raise Exception('to_unit input must be an astropy unit, such as u.degree. You input: "%s" of %s'\
            % (str(myunit), str(type(myunit))))

    # tack on unit if var doesn't have units yet
    try:
        var.unit
    except:
        return var * myunit

    # check whether var units match the requested ones.
    if var.unit == myunit:
        return var
    else:
        print("...Converting [%s] to [%s]" % (str(var.unit), str(myunit)))
        return var.to(myunit)

def calc_alpha(i_dgr, orb_ang_dgr, eccen=0, arg_peri_dgr=0, out_dgr=True):
    # Calculate the phase angle (star/planet/observer angle) for a
    #  given system inclination and orbit angle.
    # Assumes a *circular* orbit
    #
    # Inputs:
    # i_dgr = inclination of orbit, in degrees [single value]
    # orb_ang = angle in the orbit, relative to the ascending node [single value or vector]
    #            = true anomaly + argument of periastron
    # out_dgr = [True]/False -- output alpha in degrees?
    #
    # Future work:
    # eccen = orbit eccentricity. NOT implemented yet.
    # arg_peri_dgr = argument of periastron in degrees. NOT implemented yet.
    #
    # References
    # http://iopscience.iop.org/article/10.1088/0004-637X/747/1/25; Madhusudhan & Burrows 2012

    if eccen !=0 or arg_peri_dgr != 0:
        raise Exception('Non-circular orbits not yet implemented')

    i_dgr = unit_check_convert(i_dgr, u.degree)
    orb_ang_dgr = unit_check_convert(orb_ang_dgr, u.degree)

    alpha = np.arccos( np.sin(orb_ang_dgr) * np.sin(i_dgr) )
    if out_dgr:
        return alpha.to(u.degree)
    else:
        return alpha

def calc_lambert_phase_law(alpha):
    # Evaluate the phase law of a Lambertian planet at a given phase angle
    #
    # Inputs:
    # Phase angle in DEGREES
    #
    # Outputs:
    # Phi - a value between 0 and 1, where 1 is the value at alpha=0, and 0 is no flux
    #
    # References:
    # Traub & Oppenheimer: Direct Imaging of Exoplanets, equation 15.
    #  (pg 116 of Seager Exoplanets textbook)

    alpha = unit_check_convert(alpha, u.degree)

    return ( np.sin(alpha) + (np.pi - alpha.to(u.radian).value) * np.cos(alpha) ) / np.pi

def calc_lambert_flux_ratio(sma=3*u.au, rp=1.*u.jupiterRad, albedo=0.5, \
        inclin=90*u.degree, orb_ang=0*u.degree):
    # Calculate flux ratio of a Lambertian planet on a CIRCULAR, inclined orbit
    #
    # Inputs [defaults in brackets]:
    # rho     = orbital separation in AU (physical, NOT projected); [3au]
    # rp      = planet radius, in Jupiter radii; [1Rj]
    # albedo  = geometric albedo; [0.5]
    # incl    = inclination in degrees; [90dgr]
    # orb_ang = angle in the orbit, relative to the ascending node (single value or vector)
    #            == true anomaly + argument of periastron;  [0dgr = quadrature]
    # Outputs:
    # flux ratio

    sma = unit_check_convert(sma, u.au)
    rp = unit_check_convert(rp, u.jupiterRad)
    inclin = unit_check_convert(inclin, u.degree)
    orb_ang = unit_check_convert(orb_ang, u.degree)

    phi = calc_lambert_phase_law(calc_alpha(inclin, orb_ang))
    rad_ratio = ((rp / sma).decompose())**2 # pl_orbsmax already has associated unit
    f_ratio = albedo * phi * rad_ratio
    return f_ratio

def calc_xy_r_int(inclin):
    # Calculate the xy positions, projected separation, & lambertian brightness
    #  of a planet on a CIRCULAR, inclined orbit
    #
    # Inputs:
    # incl = inclination in degrees
    #
    # Outputs:
    # list of vectors (x,y,r,inten)

    inclin = unit_check_convert(inclin, u.degree)

    ang = np.linspace(0,360,70) * u.degree

    alpha = calc_alpha(inclin,ang,out_dgr=True)
    alpha = unit_check_convert(alpha, u.degree)

    inten = calc_lambert_phase_law(alpha)
    x = np.cos(ang)
    y = np.sin(ang) * np.cos(inclin)
    r = np.sqrt(x**2 + y**2)
    return (x,y,r,inten)

def read_and_filter_exo_archive(fname='data/exo_archive_query.txt', \
        rho_min=0.1*u.arcsec, rho_max=1.5*u.arcsec,\
        st_v_min=8.*u.mag, mp_min=0.25*u.jupiterMass):

    # make sure there are no conflicting units
    rho_min = unit_check_convert(rho_min, u.arcsec)
    rho_max = unit_check_convert(rho_max, u.arcsec)
    st_v_min = unit_check_convert(st_v_min, u.mag)
    mp_min = unit_check_convert(mp_min, u.jupiterMass)

    dat = ascii.read(fname)

    # remove rows with missing important values
    filter_cols = ['st_dist','st_optmag', 'pl_orbsmax','pl_bmassj']
    for f in filter_cols:
        dat = dat[~dat[f].mask]

    # manually assign b/c table units were (non-standard) "mags" not "mag"
    # Must be done before converting to QTable
    dat['st_optmag'].unit = u.mag

    dat = QTable(dat)

    # remove the metadata that had column descriptions, since it makes it a pain
    #  to write out data later if we change columns. Better to add column descriptions
    #  later by hand if needed...
    dat.meta = {}

    dat.rename_column('pl_orbsmax','sma_au')
    dat.rename_column('pl_orbincl','orb_incl')
    dat.rename_column('pl_orbeccen','eccen')
    dat.rename_column('pl_bmassj','pl_massj')

    # replace spaces with _ in star names
    dat['pl_hostname'] = [x.replace(' ','_') for x in dat['pl_hostname']]

    #keep only stars brighter than st_v_min in V
    dat = dat[dat['st_optmag'] < st_v_min]

    #keep only planets larger than mp_min*Mj
    dat = dat[dat['pl_massj'] > mp_min]

    # keep only planets with a maximum elongation between X1 and X2
    tmp = (dat['sma_au'] / dat['st_dist']).decompose() * u.radian
    dat['sma_arcsec'] = tmp.to(u.arcsec)
    dat['sma_arcsec'].info.format = '%.2f'
    dat = dat[dat['sma_arcsec'] >= rho_min]
    dat = dat[dat['sma_arcsec'] < rho_max]
    dat.sort('sma_arcsec')

    return dat

def create_wfirst_reflected_light_table(fname_in='data/exo_archive_query.txt', \
        fname_out = 'test.txt', \
        rho_min=0.12*u.arcsec, rho_max=1.45*u.arcsec,\
        st_v_min=7.*u.mag, mp_min=0.25*u.jupiterMass, rp=1.0*u.jupiterRad,\
        albedo=0.5, orb_ang=0*u.degree, inclin=90*u.degree):

    rho_min = unit_check_convert(rho_min, u.arcsec)
    rho_max = unit_check_convert(rho_max, u.arcsec)
    st_v_min = unit_check_convert(st_v_min, u.mag)
    mp_min = unit_check_convert(mp_min, u.jupiterMass)
    rp = unit_check_convert(rp, u.jupiterRad)
    inclin = unit_check_convert(inclin, u.degree)
    orb_ang = unit_check_convert(orb_ang, u.degree)

    # read the NASA exoplanet archive table & select appropriate systems
    dat = read_and_filter_exo_archive(fname=fname_in,rho_min=rho_min, rho_max=rho_max,\
            st_v_min=st_v_min, mp_min=mp_min)

    # Calculate the Lambertian flux ratio
    dat['Fp/F*_quad'] = calc_lambert_flux_ratio(sma=dat['sma_au'], \
        rp=rp,orb_ang=orb_ang,albedo=albedo, inclin=inclin)
    dat['Fp/F*_quad'].info.format = '%.1e'

    # create header comments / description
    comments = ('#Short caption: All planets from NASA exoplanet archive with a '+ \
        'semi-major axis of %.2f-%.2g arcsec, mass > %.2f Mjup, and host star V mag < %.2f. '+\
        'Lambertian flux ratio assumes: radius = %.1g Rjup, geometric albedo = %.2g, '+\
        'circular orbit, inclination = %.1f, and angle of %.1f degrees from the ascending node.\n' )% \
        (rho_min.value, rho_max.value, mp_min.value, st_v_min.value, rp.value, albedo, \
        inclin.to(u.degree).value, orb_ang.to(u.degree).value)
    comments += '#References:\n'
    comments += '#Lambertian phase curve from Traub & Oppenheimer: ' + \
    'Direct Imaging of Exoplanets, equation 15, pg 116 of Seager Exoplanets textbook.\n'
    #print comments

    #write out with astropy tables to format the body correctly
    t2 = dat['pl_hostname','pl_letter','sma_arcsec','Fp/F*_quad','st_optmag','st_spstr','pl_discmethod']
    ascii.write(t2,fname_out,\
            format='fixed_width', comment='#',delimiter=',',bookend=False,overwrite=True)

    # Then redo write out to get the header formatted the same as the other datafiles
    f = open(fname_out,'r')
    lines = f.readlines()
    f.close()
    f = open(fname_out,'w')
    f.write('#'+lines[0])
    f.write(comments)
    f.write(('').join(lines[1:]))
    f.close()

    print('File written to '+fname_out)

    return

def demo_phase():
    import matplotlib.pyplot as plt

    plt.figure(figsize=[6,6])
    ax1 = plt.subplot(221)
    ax2 = plt.subplot(222)
    ax3 = plt.subplot(223)
    ax4 = plt.subplot(224)

    ang = np.linspace(90,270,50)*u.degree
    for inc in np.linspace(0,90,5)*u.degree:
        alpha = calc_alpha(inc,ang)
        ax1.plot(ang, alpha)
        ax2.plot(ang, calc_lambert_phase_law(alpha), label=inc)

    ax1.set_xlabel('angle from ascending node [dgr]')
    ax1.set_ylabel('alpha [dgr]')
    ax1.legend()

    ax2.set_xlabel('angle from ascending node [dgr]')
    ax2.set_ylabel('flux relative to full')
    ax2.legend(title='inclination', fontsize=6)

    (x,y,r,inten) = calc_xy_r_int(60)
    ax3.plot(r,inten,label='i=60')
    plt.legend()

    ax4.scatter(x,y,c=inten, vmin=0, vmax=1,label='i=60')
    ax4.axis('equal')
    plt.legend()

    plt.tight_layout()
    plt.savefig('demo_phase_curve.pdf')
    print('Saved plot called demo_phase_curve.pdf')


"""Add application settings

Revision ID: 43034be2bc98
Revises: 401d2fcbe9e4
Create Date: 2022-09-04 19:50:51.555555

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '43034be2bc98'
down_revision = '401d2fcbe9e4'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('system_settings', sa.Column('minimum_length', sa.Integer(), nullable=True))
    op.add_column('system_settings', sa.Column('maximum_length', sa.Integer(), nullable=True))
    # Set min to 20 and max to 10000
    op.execute("UPDATE system_settings SET minimum_length = 20, maximum_length = 10000")
    # Set nullable to false
    op.alter_column('system_settings', 'minimum_length', nullable=False)
    op.alter_column('system_settings', 'maximum_length', nullable=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('system_settings', 'maximum_length')
    op.drop_column('system_settings', 'minimum_length')
    # ### end Alembic commands ###


from __future__ import annotations

import logging
import threading
from collections.abc import Iterable
from contextlib import suppress
from functools import partial
from queue import Empty, Queue
from threading import RLock
from typing import Generic, TypeVar

logger = logging.getLogger(__name__)

QueueType = TypeVar("QueueType")

_empty = object()

class QueueIsClosed(Exception):
    pass

def _raise_queue_is_closed(item: object, *, queue: ClosableQueue, **kwargs):
    if item is not ClosableQueue.SENTINEL:
        raise QueueIsClosed
    Queue.put(queue, item, **kwargs)

class ClosableQueue(Queue[QueueType], Generic[QueueType]):
    SENTINEL = object()
    __QUEUES: list[ClosableQueue] = []

    def __init__(self, maxsize: int = 0):  # 0 == infinite
        super().__init__(maxsize=maxsize)
        self.__QUEUES.append(self)
        # ensure close doesn't block and can set raise error
        self.mutex = RLock()  # type: ignore

        self.not_empty = threading.Condition(self.mutex)
        self.not_full = threading.Condition(self.mutex)
        self.all_tasks_done = threading.Condition(self.mutex)

    def close(self):
        with self.mutex:
            self.put(self.SENTINEL)
            self.put = partial(_raise_queue_is_closed, queue=self)
        with suppress(Exception):
            self.__QUEUES.remove(self)

    def close_safely(self):
        with suppress(QueueIsClosed):
            self.close()

    def __iter__(self) -> Iterable[QueueType]:
        try:
            while True:
                item = super().get(block=True)
                if item is self.SENTINEL:
                    with self.mutex:
                        # ensure next iterator will finish immediately
                        self.queue.append(item)
                        self.not_empty.notify()
                    return  # Cause the thread to exit
                yield item
        except BaseException as e:
            logger.exception(e)
        finally:
            with suppress(ValueError):
                self.task_done()

    def pop(self, default=_empty):
        try:
            next = self.get_nowait()
        except Empty:
            return default
        else:
            if next is self.SENTINEL:
                self.put(self.SENTINEL)  # ensure next iterator will finish immediately
            return next

    def iter_non_blocking(self) -> Iterable[QueueType]:
        next_or_sentinel = partial(self.pop, self.SENTINEL)
        return iter(next_or_sentinel, self.SENTINEL)

    @classmethod
    def close_all(cls):
        if cls.__QUEUES:
            logger.info("closing all queues")
            for q in list(cls.__QUEUES):  # avoid modification during iteration
                q.close()

    def get_nowait(self) -> QueueType:
        item = super().get_nowait()
        if item is self.SENTINEL:
            self.queue.append(item)
            raise QueueIsClosed
        return item

    def get(self, block: bool = True, timeout: float | None = None) -> QueueType:
        item = super().get(block, timeout)
        if item is self.SENTINEL:
            self.queue.append(self.SENTINEL)
            raise QueueIsClosed
        return item


import requests
from urllib.parse import urlencode

class BugcrowdAPI:
    """
    A simple client for the bugcrowd.io API
    """

    bugcrowd_api_url = "https://api.bugcrowd.com"
    default_headers = {
        "Accept": "application/vnd.bugcrowd+json",
        "User-Agent": "DefectDojo",
        "Bugcrowd-Version": "2021-10-28",
    }

    def __init__(self, tool_config):
        self.session = requests.Session()
        if tool_config.authentication_type == "API":
            self.api_token = tool_config.api_key
            self.session.headers.update(
                {"Authorization": "Token {}".format(self.api_token)}
            )
            self.session.headers.update(self.default_headers)
        else:
            raise Exception(
                "bugcrowd Authentication type {} not supported".format(
                    tool_config.authentication_type
                )
            )

    def get_findings(self, program, target):
        """
        Returns the findings in a paginated iterator for a given bugcrowd program and target, if target is *,
        everything is returned
        :param program:
        :param target:
        :return:
        """
        params_default = {
            "filter[duplicate]": "false",
            "filter[program]": program,
            "page[limit]": 100,
            "page[offset]": 0,
            "include": "monetary_rewards,target,program,external_issues",
            "sort": "submitted-desc",
        }

        if target:
            params = params_default
            params["filter[target]"] = target
            params_encoded = urlencode(params)
        else:
            params_encoded = urlencode(params_default)

        next = "{}/submissions?{}".format(
            self.bugcrowd_api_url, params_encoded
        )
        while next != "":
            response = self.session.get(url=next)
            response.raise_for_status()
            if response.ok:
                data = response.json()
                if len(data["data"]) != 0:
                    yield data["data"]

                # When we hit the end of the submissions, break out
                if len(data["data"]) == 0:
                    next = ""
                    break

                # Otherwise, keep updating next link
                next = "{}{}".format(
                    self.bugcrowd_api_url, data["links"]["next"]
                )
            else:
                next = "over"

    def test_connection(self):
        # Request programs
        response_programs = self.session.get(
            url="{}/programs".format(self.bugcrowd_api_url)
        )
        response_programs.raise_for_status()

        # Request submissions to validate the org token
        response_subs = self.session.get(
            url="{}/submissions".format(self.bugcrowd_api_url)
        )
        response_subs.raise_for_status()
        if response_programs.ok and response_subs.ok:
            data = response_programs.json().get("data")
            data_subs = response_subs.json().get("meta")
            total_subs = str(data_subs["total_hits"])

            progs = list(filter(lambda prog: prog["type"] == "program", data))
            program_names = ", ".join(
                list(map(lambda p: p["attributes"]["code"], progs))
            )
            # Request targets to validate the org token
            response_targets = self.session.get(
                url="{}/targets".format(self.bugcrowd_api_url)
            )
            response_targets.raise_for_status()
            if response_targets.ok:
                data_targets = response_targets.json().get("data")
                targets = list(
                    filter(lambda prog: prog["type"] == "target", data_targets)
                )
                target_names = ", ".join(
                    list(map(lambda p: p["attributes"]["name"], targets))
                )
                return (
                    f'With {total_subs} submissions, you have access to the "{ program_names }" '
                    f"programs, "
                    f"you can use these as Service key 1 for filtering submissions "
                    f'You also have targets "{ target_names }" that can be used in Service key 2'
                )
            else:
                raise Exception(
                    "Bugcrowd API test not successful, no targets were defined in Bugcrowd which is used for "
                    "filtering, check your configuration, HTTP response was: {}".format(
                        response_targets.text
                    )
                )
        else:
            raise Exception(
                "Bugcrowd API test not successful, could not retrieve the programs or submissions, check your "
                "configuration, HTTP response for programs was: {}, HTTP response for submissions was: {}".format(
                    response_programs.text, response_subs.text
                )
            )

    def test_product_connection(self, api_scan_configuration):
        submissions = []
        submission_gen = self.get_findings(
            api_scan_configuration.service_key_1,
            api_scan_configuration.service_key_2,
        )
        for page in submission_gen:
            submissions = submissions + page
        submission_number = len(submissions)
        return (
            f'You have access to "{submission_number}" submissions (no duplicates)'
            f'in Bugcrowd in the Program code "{api_scan_configuration.service_key_1}"'
            f'and Target "{api_scan_configuration.service_key_2}" '
            f"(leave service key 2 empty to get all submissions in "
            f"program)"
        )


import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-5, 5, 0.02)

loss01 = [(1-np.sign(xi))/2 for xi in x]
hinge = np.maximum(0, 1-x)
square_hinge = np.maximum(0, 1-x)**2
exp = np.exp(-x)
logistic = np.log2(1 + np.exp(-x))

with plt.style.context('Solarize_Light2'):

    plt.axis([-5.1, 5.1, -1.1, 5.1])

    ax = plt.gca()

    ax.xaxis.set_ticks_position('bottom')
    ax.spines['bottom'].set_position(('data', 0))
    #ax.spines['bottom'].set_color('#586e75')

    ax.yaxis.set_ticks_position('left')
    ax.spines['left'].set_position(('data', 0))
    #ax.spines['left'].set_color('#586e75')

    ax.xaxis.set_ticks_position('top')
    ax.spines['top'].set_position(('data', 0))
    ax.spines['top'].set_color('#586e75')

    ax.yaxis.set_ticks_position('right')
    ax.spines['right'].set_position(('data', 0))
    ax.spines['right'].set_color('#586e75')

    ax.tick_params(axis='x')
    ax.tick_params(axis='y')

    plt.plot(x, loss01, linestyle="solid", linewidth=2, label="0-1 loss")
    plt.plot(x, hinge, linestyle="-", linewidth=2, label="hinge")
    plt.plot(x, square_hinge, linestyle="dashed", linewidth=2, label="square hinge")
    plt.plot(x, exp, linestyle=":", linewidth=2, label="exp")
    plt.plot(x, logistic, linestyle="-.", linewidth=2, label="logistic")

    legend = plt.legend(loc='upper right', prop={'family': 'EB Garamond', 'size': 15})
    plt.setp(legend.get_texts(), color='#586e75')

    plt.xlabel(r'$y f(x)$', loc='right')

plt.savefig('surrogate-loss.svg', transparent=True)
plt.show()