import cv2
import numpy as np
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
'''Direcciones donde se extraera el video  4 camaras'''
lista_camaras = ['https://192.168.0.104:8080/video','*','*','*']
cap = None
cap2 = None
cap3= None
cap4 = None

'''recibe la direccion y establece la conexion '''
def video_capture(a):
    cap = cv2.VideoCapture(a)
    return cap
''' inicializa las variables donde capturamos el video en caso de no encontrar no inicializa  '''
if lista_camaras[0] !='*':
    cap = video_capture(lista_camaras[0])
if lista_camaras[1] !='*':
    cap2 = video_capture(lista_camaras[1])
if lista_camaras[2] != '*':
    cap3 = video_capture(lista_camaras[2])
if lista_camaras[3] != '*':
    cap4 = video_capture(lista_camaras[3])

'''creacion de ventana principal'''
window = Tk()
window.title("Camaras")
window.geometry('1200x620')
window.resizable(0, 0)

tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)

tab_control.add(tab1, text='Camaras piso 1')

lbl1 = Label(tab1, text='label1')
lbl1.pack()
frame01 = LabelFrame(tab1, bg="red", width=320, height=200)
frame01.place(x=0, y=0)
L1 = Label(frame01,bg="red", width=320, height=200)
L1.pack()

frame02 = LabelFrame(tab1, bg="blue", width=320, height=200)
frame02.place(x=0, y=200)
L2 = Label(frame02,bg="blue", width=320, height=200)
L2.pack()

frame03 = LabelFrame(tab1, bg="white", width=320, height=200)
frame03.place(x=0, y=400)
L3 = Label(frame03, bg="white", width=320, height=200)
L3.pack()

frame04 = LabelFrame(tab1, bg="orange", width=900, height=600)
frame04.place(x=320, y=0)
L4 = Label(frame04,width=900, height=600)
L4.pack()

'''Evento de click en un frame, cambia la transmision del frame pequeño al frame principal '''
def callback(event):
    frame04.focus_set()
    aux = lista_camaras[0]
    lista_camaras[0] = lista_camaras[3]
    lista_camaras[3] = aux
    if lista_camaras[0] !='*':
        cap =video_capture(lista_camaras[0])
    if lista_camaras[3] !='*':
        cap4 =video_capture(lista_camaras[3])
    print('clicked',lista_camaras[0],lista_camaras[3] ,'cap hecho')

'''Evento de click en un frame, cambia la transmision del frame pequeño al frame principal '''
def callback1(event):
    frame04.focus_set()
    aux = lista_camaras[1]
    lista_camaras[1] = lista_camaras[3]
    lista_camaras[3] = aux
    if lista_camaras[1] !='*':
        cap2 =video_capture(lista_camaras[1])
    if lista_camaras[3] !='*':
        cap4 =video_capture(lista_camaras[3])
    print('clicked',lista_camaras[1],lista_camaras[3] ,'cap hecho')

'''Evento de click en un frame, cambia la transmision del frame pequeño al frame principal '''
def callback2(event):
    frame04.focus_set()
    aux = lista_camaras[2]
    lista_camaras[2] = lista_camaras[3]
    lista_camaras[3] = aux
    if lista_camaras[2] !='*':
        cap3 =video_capture(lista_camaras[2])
    if lista_camaras[3] !='*':
        cap4 =video_capture(lista_camaras[3])
    print('clicked',lista_camaras[2],lista_camaras[3] ,'cap hecho')

'''Se le asigna cada accion a cada frame'''
L1.bind("<Button-1>", callback)
L2.bind("<Button-1>", callback1)
L3.bind("<Button-1>", callback2)

tab_control.pack(expand=1, fill='both')

'''En el bucle permite capturar el video'''
''' cap.read() , captura el video'''
'''cv2.resize(img, (320, 200)) , cambia el tamaño del video a el tamaño establecido'''
'''img = ImageTk.PhotoImage(Image.fromarray(img)) ,  covierte el video a imagen'''
'''L1['image'] = img , ingresa la imagen al label seleccionado'''
''' este proceso se repite a las 4 camaras '''
try:
    while cap.isOpened():
        if lista_camaras[0] != '*':
            cap = video_capture(lista_camaras[0])
            img = cap.read()[1]
            img = cv2.resize(img, (320, 200))
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img = ImageTk.PhotoImage(Image.fromarray(img))
            L1['image'] = img
        if lista_camaras[1] != '*':
            cap2 = video_capture(lista_camaras[1])
            img2 = cap2.read()[1]
            img2 = cv2.resize(img2, (320, 200))
            img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
            img2 = ImageTk.PhotoImage(Image.fromarray(img2))
            L2['image'] = img2
        if lista_camaras[2] != '*':
            cap3 = video_capture(lista_camaras[2])
            img3 = cap3.read()[1]
            img3 = cv2.resize(img3, (320, 200))
            img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB)
            img3 = ImageTk.PhotoImage(Image.fromarray(img3))
            L3['image'] = img3
        if lista_camaras[3] != '*':
            cap4 = video_capture(lista_camaras[3])
            img4 = cap4.read()[1]
            img4 = cv2.resize(img4, (900, 600))
            img4 = cv2.cvtColor(img4, cv2.COLOR_BGR2RGB)
            img4 = ImageTk.PhotoImage(Image.fromarray(img4))
            L4['image'] = img4
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        window.update()
except IOError:
    print('Sistema Cerrado')

cap.release()
cap2.release()
cap3.release()
cap4.release()
cv2.destroyAllWindows()


import requests
from pathlib import Path

Path("./images").mkdir(parents=True, exist_ok=True)

url = 'https://imgs.xkcd.com/comics/python.png'
res = requests.get(url)
with open(Path("./images/python.png"), "wb") as f:
    f.write(res.content)


from __future__ import print_function

__author__ = "zachn@google.com (Zach Newell)"

import argparse
import pprint
import sys

from oauth2client import client
from googleapiclient import sample_tools
from googleapiclient.discovery import build
from googleapiclient.discovery import http

# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument("teamId", help="Coordinate Team ID")

def main(argv):
    # Authenticate and construct service.
    service, flags = sample_tools.init(
        argv,
        "coordinate",
        "v1",
        __doc__,
        __file__,
        parents=[argparser],
        scope="https://www.googleapis.com/auth/coordinate",
    )

    service = build("coordinate", "v1", http=http)

    try:
        # List all the jobs for a team
        jobs_result = service.jobs().list(teamId=flags.teamId).execute(http=http)

        print("List of Jobs:")
        pprint.pprint(jobs_result)

        # Multiline note
        note = """
    These are notes...
    on different lines
    """

        # Insert a job and store the results
        insert_result = (
            service.jobs()
            .insert(
                body="",
                title="Google Campus",
                teamId=flags.teamId,
                address="1600 Amphitheatre Parkway Mountain View, CA 94043",
                lat="37.422120",
                lng="122.084429",
                assignee=None,
                note=note,
            )
            .execute()
        )

        pprint.pprint(insert_result)

        # Close the job
        update_result = (
            service.jobs()
            .update(
                body="",
                teamId=flags.teamId,
                jobId=insert_result["id"],
                progress="COMPLETED",
            )
            .execute()
        )

        pprint.pprint(update_result)

    except client.AccessTokenRefreshError as e:
        print(
            "The credentials have been revoked or expired, please re-run"
            "the application to re-authorize"
        )

if __name__ == "__main__":
    main(sys.argv)


import numpy as np
import cv2

"""
Variable setup-
1 - Canny thresholds
2 - Mask polygon co-ords
3 - Hough transformation vars
"""
# 1 - Lower/Upper Canny thresholds
# Depreciated - Variables have to manually be set in the canny function otherwise the code breaks.

#2 - Points 1-4 co-ordinates for mask
x1 = 277 * 2.02
y1 = 351 * 2.12
x2 = 0
y2 = 535 * 2.12
x3 = 680 * 2.02
y3 = 535 * 2.12
x4 = 454 * 2.02
y4 = 306 * 2.12

#3 - Hough transformation variables
rho = 2
theta = 2 * np.pi/180
threshold = 15
minLength = 15
maxGap = 15

# Importing the test video
cap = cv2.VideoCapture("test_videos/Example002.mp4")
if not cap.isOpened():
    raise BrokenPipeError("Video not initializing")

# Defining codecs and videoWriter for creating output video
fourcc = cv2.VideoWriter_fourcc(*"DIVX")
output = cv2.VideoWriter("test_videos_output/2_final.avi", fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))))

while True:
    ret, frame = cap.read()

    if not ret: # If frame is not read, video has ended, exit
        break

    # Image converted to grayscale for processing
    img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Canny edge detection run on image
    canny = cv2.Canny(img, 173, 106)

    # Creating polygon mask
    mask = np.zeros_like(canny)
    imshape = img.shape
    vertices = np.array([[(x1, y1), (x2, y2), (x3, y3), (x4, y4)]], dtype=np.int32)
    cv2.fillPoly(mask, vertices, 255)

    # Applying mask and hough transform
    masked_edges = cv2.bitwise_and(canny, mask)
    linesP = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]), minLength, maxGap)

    # Drawing hough lines onto original frame
    if linesP is not None:
        for i in range(0, len(linesP)):
            l = linesP[i][0]
            cv2.line(frame, (l[0], l[1]), (l[2], l[3]), (0, 0, 255), 2, cv2.LINE_AA)

    # Writing the modified frame to the output video
    output.write(frame)

    cv2.imshow("frame", frame)
    if cv2.waitKey(19) == ord('q'):
        break

cap.release()
output.release()
cv2.destroyAllWindows()


#! /usr/bin/env python
import sys
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
import gzip
import argparse

zippedGFF3 =False
zippedRef =False
if sys.argv[1][-3:] =='.gz':
	zippedGFF3 = True
if sys.argv[2][-3:] =='.gz':
	zippedRef = True

input = ""
allDNA = ""
if zippedGFF3:
	input = gzip.open(sys.argv[1],'r')
else:
	input = open(sys.argv[1],'r')
if zippedRef:
	allDNA = gzip.open(sys.argv[2],'r')
else:
	allDNA = open(sys.argv[2],'r')
output = open(sys.argv[3],'w')

allStartPos = dict()

lastHeader = ""
sequenceLine = ""
for line in allDNA:

	if line[0] =='>':
		if sequenceLine !="":
			allStartPos[lastHeader] = sequenceLine
		lastHeader = line
		sequenceLine = ""
		continue
	sequenceLine +=line.strip()

allDNA.close()
if sequenceLine !="":
	allStartPos[lastHeader] = sequenceLine

currentAccession = ""
currentGene = ""
currentSeq = ""
lastBadAccession = ""
afterFirst=False

for line in input:
	if line[0]=='#':
		continue
	info = line.strip().split("\t")
	if info[2] != "CDS":
		continue
	accession = info[0]
	if accession ==lastBadAccession:
		continue
	gene = info[8]
	if gene !=currentGene:
		currentGene = gene
		for key in allStartPos.keys():
			if accession in key:
				if afterFirst:
					output.write("\n")
				afterFirst = True

				output.write('>'+info[8]+'\n')
				if accession !=currentAccession:
					currentAccession = accession
					currentSeq = allStartPos[key]
					break
		if accession != currentAccession:
			lastBadAccession = accession
			continue

	if info[6] == '-':
		smaller = currentSeq[int(info[3])-1:int(info[4])]
		my_seq = Seq(smaller)
		output.write(str(my_seq.reverse_complement())+'*')
	else:
		output.write(currentSeq[int(info[3]) -1:int(info[4])]+'*')

output.write("\n")
output.close()
input.close()


"""
Vault helpers.
"""
import hvac
import os

def init_vault_client():
    """
    Initializes vault client.

    Returns:
        (hvac.v1.Client, String): Vault client instance and error message
    """
    client = None
    error = None
    vault_address = os.environ.get("VAULT_ADDR")
    custom_ca = os.environ.get("VAULT_CA")
    verify = True
    if vault_address is None:
        error = "Environment variable VAULT_ADDR not set"

    if custom_ca is not None:
        verify = custom_ca
    client = hvac.Client(url=vault_address, verify=verify)

    try:
        if not client.is_authenticated():
            error = "Vault authentication error"
    except ConnectionError:
        error = "Vault connection error"

    return (client, error)


from PIL import Image, ImageDraw

size = (1280, 720)
color = (255,255,255)
mode='RGB'
img = Image.new(mode, size, color)

img.save(r"C:\Change\This\Path\To\stuck_pixels.png")


import sys

from fbs_runtime.application_context import ApplicationContext
from PyQt5 import QtWidgets, QtGui

from app import App

if __name__ == '__main__':
    appctxt = ApplicationContext()       # 1. Instantiate ApplicationContext
    app = QtWidgets.QApplication(sys.argv)
    # load from fbs resources
    splash_image = appctxt.get_resource('signalum.png')
    pixmap = QtGui.QPixmap(splash_image)
    splash = QtWidgets.QSplashScreen(pixmap)
    splash.show()
    app.setOrganizationName("BisonCorps")
    app.setApplicationName("Signalum")
    a = App()
    splash.showMessage("Loading application")
    a.setWindowTitle("Signalum Desktop - (BisonCorps, 2019)")
    a.show()
    splash.showMessage("Finished loading application")
    splash.finish(a)
    # app.exec_()
    exit_code = appctxt.app.exec_()      # 2. Invoke appctxt.app.exec_()
    sys.exit(exit_code)


type_figure = str(input())
pi = 3.14
if type_figure == 'круг':
    radius = int(input())
    print(pi * radius ** 2)
elif type_figure == 'прямоугольник':
    x = int(input())
    z = int(input())
    print(x*z)
elif type_figure == 'треугольник':
    a = int(input())
    b = int(input())
    c = int(input())
    semi_perimeter = (a + b + c) / 2
    print ((semi_perimeter * (semi_perimeter - a) * (semi_perimeter - b) * (semi_perimeter - c)) ** 0.5)


from traceback import format_exc

from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.views.generic.base import View
from django.shortcuts import render_to_response, render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from django.contrib import auth
from django.core.urlresolvers import reverse_lazy
from django.conf import settings

import datetime
import time
from pprint import pprint, pformat

import models

class ActionGroup(object):
    def __init__(self, action, entries):
        self.action = action
        self.entries = entries or []

def index(request):
    # Get statistical items: total-count, first entry, last entry
    num_entries = models.LogEntry.objects.count()

    entry_first = models.LogEntry.objects.order_by("date_entry")[0] if num_entries else None
    entry_most_recent = models.LogEntry.objects.order_by("-date_entry")[0] if num_entries else None

    # Get number of consecutive days with entries
    consecutive_entries = 0
    date_cursor = datetime.date.today()
    for entry in models.LogEntry.objects.order_by("-date_entry"):
        if entry.date_entry > date_cursor:
            continue
        if entry.date_entry == date_cursor:
            consecutive_entries += 1
            date_cursor -= datetime.timedelta(days=1)
        else:
            break

    # Get items for this month
    today = datetime.date.today()
    first_this_month = datetime.date(today.year, today.month, 1)
    entries = list(models.LogEntry.objects.filter(date_entry__gte=first_this_month).order_by("-date_entry"))

    # Sort entries into action groups
    groups = {}
    for entry in entries:
        if entry.action.name in groups:
            groups[entry.action.name].append(entry)
        else:
            groups[entry.action.name] = [entry]

    return render(request, 'index.html', {
        "entry_first": entry_first,
        "entry_most_recent": entry_most_recent,
        "entry_count": num_entries,
        "consecutive_entries": consecutive_entries,
        "entries": entries,
        "num_entries_this_month": len(entries),
        "num_days_without_entries_this_month": today.day - len(entries),
        "groups": groups,
    })


from pprint import pprint

from twisted.internet.defer import inlineCallbacks

from autobahn.twisted.wamp import ApplicationSession
from autobahn.wamp.exception import ApplicationError

import hostchecker

PRINCIPALS_FILE = '../principals.txt'
PRINCIPALS_DB = dict()
with open(PRINCIPALS_FILE, 'r') as fh:
   for line in fh:
      principal, ticket, role = line.strip().split(' ')
      PRINCIPALS_DB[principal] = {
         'ticket': ticket,
         'role': role
      }

class Authenticator(ApplicationSession):

   @inlineCallbacks
   def onJoin(self, details):
      def authenticate(realm, authid, details):
         ticket = details['ticket']

         protocol, host, port = details['transport']['peer'].split(':')
         allowed_host = hostchecker.allowed(host)
         print('[authenticator] Client:', protocol, host, port, 'allowed:', allowed_host)
         if not hostchecker.allowed(host):
            raise ApplicationError('invalid_host', 'host not allowed')

         if authid in PRINCIPALS_DB:
            if ticket == PRINCIPALS_DB[authid]['ticket']:
               return PRINCIPALS_DB[authid]['role']
            else:
               raise ApplicationError('invalid_ticket',
                     'could not authenticate session - invalid '
                     'ticket "{}" for principal {}'.format(ticket, authid))
         else:
            raise ApplicationError('no_such_user',
                  'no such principal {}'.format(authid))
      try:
         yield self.register(authenticate, 'com.forrestli.jukebox.authenticate')
      except Exception as e:
         print('Failed to register dynamic authenticator: {0}'.format(e))


'''二叉树的最大距离：一个结点到另一个结点经过的边的数量，结点只能经过一次'''
'''
    树型DP套路：

    【使用前提】

    如果题目求解目标是S规则，则求解流程可以定为【以每一个节点为头结点的子树在规则S下的每一个答案，向上传递决策结果】

    常见的可能性分类是根据【子树上的头结点是否参与所求结果】
'''

'''

    在本题中可以将情况分为

    1. 最长路径经过head
        左子树上离head最远的结点经过head后到右子树上最远的结点
        最远结点的距离就是子树的height+1
        所以dis = 左h + 1 + 右h + 1

    2. 最长路径不经过head
        那么最大距离可能来自于head的左子树或者右子树，我们需要比较两个子树的两个最大距离的大小（因为不经过head，所以没法联通）
        a. 左子树上的最大距离
        b. 右子树上的最大距离
'''
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Info():
    def __init__(self, max_distance, height):
        self.max_distance = max_distance
        self.height = height

def process(head: TreeNode):
    if head is None:
        return Info(0, 0)

    leftInfo = process(head.left)
    rightInfo = process(head.right)

    p1 = leftInfo.max_distance
    p2 = rightInfo.max_distance
    p3 = leftInfo.height + rightInfo.height + 2
    max_distance = max(p3, (p1,p2))
    height = max(leftInfo.height, rightInfo.height) + 1
    return Info(max_distance, height)

def main(head):
    return process(head).max_distance

树型DP套路【二叉树的最大距离】.py


class Employee():

    procent_na_zgolemuvanje = 1.05

    def __init__(self, name, surname, age):
        self.name = name
        self.surname = surname
        self.__age = age #private property
        #self.email = f"{name}.{surname}@gmail.com"
        self.plata = 18000

    @property
    def age(self):
        return self.__age

    @age.setter
    def age(self, new_age):
        self.__age = new_age

    @property
    def email(self):
        return f"{self.name}.{self.surname}@gmail.com"

    def zgolemi_plata(self):
        self.plata = self.plata * self.procent_na_zgolemuvanje

    def __str__(self):
        return f"employee: name={self.name}, surname={self.surname}, age={self.age}, email={self.email}, plata={self.plata}"

class Developer(Employee):

    procent_na_zgolemuvanje = 1.1

    def __init__(self, name, surname, age, programming_language):
        super().__init__(name, surname, age)
        self.programming_language = programming_language

    # def zgolemi_plata(self):
    #     self.plata = self.plata * self.procent_na_zgolemuvanje

    def change_programming_language(self, language):
        self.programming_language = language

    def __str__(self):
        employee_for_print = super().__str__()
        return f"{employee_for_print} === developer for {self.programming_language}"

class Manager(Employee):

    procent_na_zgolemuvanje = 1.15

    def __init__(self, name, surname, age, developers):
        super().__init__(name, surname, age)
        self.managed_developers = developers

    def print_developers(self):
        for x in self.managed_developers:
            print("developer: " + x.name + " --> " + x.programming_language)

    def add_developer(self, developer):
        self.plata = self.plata * 1.03
        self.managed_developers.append(developer)

    def remove_developer(self, developer):
        self.managed_developers.remove(developer)
        if len(self.managed_developers) == 0:
            self.plata = self.plata/2
        else:
            self.plata = self.plata * 0.99

# employee1 = Employee("nikola", "stojkovski", 24)
# print(employee1)
# employee1.zgolemi_plata()
# print(employee1)
developer1 = Developer("nikola", "stojkovski", 24, "Python")
print(developer1)
developer2 = Developer("david", "stojanovski", 24, "Java")
print(developer2)
developer3 = Developer("marija", "filipovska", 25, "C++")
print(developer3)
# developer1.zgolemi_plata()
# developer1.change_programming_language('JavaScript')
# print(developer1)
list_developers = [developer1, developer2]
manager1 = Manager("zare", "prezime", 40, list_developers)
#manager1.add_developer(developer3)
manager1.remove_developer(developer1)
manager1.print_developers()
print(manager1)

employee5 = Employee("nenad", "jov", 42)
print(employee5)
employee5.name = "goce"
print(employee5.email)

print(employee5.age)
employee5.age = 18
print(employee5.age)


from policyengine_us.model_api import *

class mi_retirement_benefits_deduction_tier_three_ss_exempt_not_retired(
    Variable
):
    value_type = float
    entity = TaxUnit
    label = "Michigan non-retired tier three retirement benefits deduction"
    unit = USD
    definition_period = YEAR
    reference = (
        "http://legislature.mi.gov/doc.aspx?mcl-206-30",  # (9)(d)
        "https://www.michigan.gov/taxes/-/media/Project/Websites/taxes/Forms/2022/2022-IIT-Forms/BOOK_MI-1040.pdf#page=17",
        "https://www.michigan.gov/taxes/iit/retirement-and-pension-benefits",
    )
    defined_for = "mi_retirement_benefits_deduction_tier_three_eligible"

    def formula(tax_unit, period, parameters):
        p = parameters(
            period
        ).gov.states.mi.tax.income.deductions.retirement_benefits.tier_three.ss_exempt.not_retired
        #  Recipients should receive retirement benefits from employment exempt from Social Security
        eligible_people = tax_unit(
            "mi_retirement_benefits_deduction_tier_three_ss_exempt_not_retired_eligible_people",
            period,
        )

        person = tax_unit.members
        uncapped_pension_income = person("taxable_pension_income", period)
        is_head_or_spouse = person("is_tax_unit_head_or_spouse", period)

        # Head and spouse both are eligible to receive an equal deduction amount
        cap = p.amount * eligible_people

        return min_(
            tax_unit.sum(uncapped_pension_income * is_head_or_spouse), cap
        )


from flask import render_template, session, redirect, url_for, current_app, request

from . import main
from .forms import lcd_set_form

import logging

format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO,
                    datefmt="%H:%M:%S")

from .. import lcd_state, task_queue
# from .. import lcd_state

def post_msg_to_queue(msg):
    global task_queue

    logging.info(f"View - posting message: {msg}")
    task_queue.put(msg)

@main.route('/')
def index():
    post_msg_to_queue({'action': "test"})
    return render_template('index.html')

@main.route('/lcd/msg')
def lcd_message():
    return render_template('lcd.html', lines=lcd_state['msg'], local_hardware=current_app.config['LOCAL_HARDWARE'])

@main.route('/lcd/clear')
def lcd_clear_message():

    lcd_state['msg'] = {'line1': '', 'line2': '', 'line3': '', 'line4': ''}

    post_msg_to_queue({'action': "redisplay"})

    return render_template('lcd.html', msg=lcd_state['msg'], local_hardware=current_app.config['LOCAL_HARDWARE'])

@main.route('/lcd/set', methods=['GET', 'POST'])
def lcd_set_message():

    form = lcd_set_form(request.form)

    print(f"Request Methon: {request.method}")
    if request.method == 'POST':
        print(f"  Valid form? {form.validate()}")

        if form.validate():
            print("Processing submitted form data...")
            lcd_state['msg']['line1'] = form.line1.data
            lcd_state['msg']['line2'] = form.line2.data
            lcd_state['msg']['line3'] = form.line3.data
            lcd_state['msg']['line4'] = form.line4.data

            # lcd_string(lcd_state['msg']['line1'], 1)
            # lcd_string(lcd_state['msg']['line2'], 2)
            # lcd_string(lcd_state['msg']['line3'], 3)
            # lcd_string(lcd_state['msg']['line4'], 4)

            post_msg_to_queue({'action': "redisplay"})

            print("redirecting to GET display resource...")
            return redirect(url_for('main.lcd_message'))
        else:
            return render_template('lcd_set.html', form=form, local_hardware=current_app.config['LOCAL_HARDWARE'])

    else:  # was a GET message
        form = lcd_set_form()

        print("Displaying form for user data entry`...")
        form.line1.data = lcd_state['msg']['line1']
        form.line2.data = lcd_state['msg']['line2']
        form.line3.data = lcd_state['msg']['line3']
        form.line4.data = lcd_state['msg']['line4']
        return render_template('lcd_set.html', form=form)


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

import matplotlib.pyplot as plt
import os
import pandas as pd
import numpy as np
import random

def pareto_frontier(df, xname, yname):
    df = df.sort_values(yname)
    df_out = df.iloc[[0]]
    max_rec = df_out[xname]

    for i in range(1,len(df)):
        row = df.iloc[i,:]
        if (row[xname] > max_rec).any():
            df_out = df_out.append(row)
            max_rec = row[xname]

    return df_out

def plot_recalls(df, marker, color, algo):
    print(algo, ':')
    df = pareto_frontier(df, 'recall', 'query_time')
    df = df[df['recall'] <= 0.99]
    del df['var_recall']
    del df['k']
    if 'val_recall' in df.columns:
        del df['val_recall']
        del df['val_var_recall']
        del df['val_query_time']
    if 'r0' in df.columns:
        del df['r0']
    if 'r1' in df.columns:
        del df['r1']
    if 'r2' in df.columns:
        del df['r2']
    if 'r3' in df.columns:
        del df['r3']
    if 'r4' in df.columns:
        del df['r4']
    if 'r5' in df.columns:
        del df['r5']
    if 'r6' in df.columns:
        del df['r6']
    if 'r7' in df.columns:
        del df['r7']
    if 'r8' in df.columns:
        del df['r8']
    if 'r9' in df.columns:
        del df['r9']
    if 'r10' in df.columns:
        del df['r10']
    df.insert(0, 'recall', df.pop('recall'))
    df.insert(1, 'query_time', df.pop('query_time'))
    df.index = [''] * len(df)
    print(df)
    print()
    x = df['recall']
    y = df['query_time']
    plt.plot(x, y, linewidth=2.0, label=algo, linestyle='-', marker=marker, markersize=6, color=color)
    return x, y

def plot_scatter(ax, datapath, colors, data = 2, n_corpus = 1000, n_train = 1000, s_corpus = 10, s_train = 15, alpha = 0.8):
    corpus_file = os.path.join(datapath, 'corpus.csv')
    train_file = os.path.join(datapath, 'train.csv')
    df_corpus = pd.read_csv(corpus_file, header=None)
    df_train = pd.read_csv(train_file, header=None)
    ax.scatter(df_corpus.iloc[0:n_corpus,0], df_corpus.iloc[0:n_corpus,1], s=s_corpus, c=colors[-1], alpha=alpha)
    if data == 2:
        ax.scatter(df_train.iloc[0:n_train,0], df_train.iloc[0:n_train,1], s=s_train, alpha=alpha, c=colors[0])
    else:
        n_clusters = 5
        n = int(n_train / n_clusters)
        nx = int(len(df_train) / n_clusters)

        for i in [4, 0, 1, 2, 3]:
            idx = np.arange(i * nx, i * nx + n)
            ax.scatter(df_train.iloc[idx,0], df_train.iloc[idx,1], s=s_train, c=colors[i], alpha=alpha)

def precision(df, n_trees, depth, k=10, k_build=50):
    if 'k_build' in df.columns:
        df = df[df['k_build'] == k_build]
    df2 = df[df['n_trees'] == n_trees]
    df3 = df2[df2['depth'] == depth]
    df4 = df3[df3['k'] == k]
    df6 = df4[['v', 'k', 'recall', 'cs_size', 'query_time']]

#    df0 = pd.DataFrame({'v' : [0],
#                        'k' : [k],
#                        'recall' : [1],
#                        'cs_size' : [n]})
#    df6 = df0.append(df5)

    df6['k_found'] = df6['recall'] * df6['k']
    df6 = df6[df6['k_found'] > 0]
    df6['precision'] = df6['k_found'] / df6['cs_size']
    return df6

def sample(images, labels, n_sample):
    n = images.shape[0]
    idx = np.arange(n)
    idx_corpus = idx[labels == 0]
    idx_train = idx[labels == 1]
    pos_corpus = random.sample(set(idx_corpus), n_sample)
    pos_train = random.sample(set(idx_train), n_sample)

    out_images = np.concatenate((images[pos_corpus,:], images[pos_train,:]))
    out_labels = np.concatenate((np.zeros(n_sample), np.ones(n_sample)))
    return out_images, out_labels

def scatter_csv(fname_tsne, n_sample=1000, s_corpus=10, s_train=15, alpha=0.8,
                scatter_colors = ['#45a2a8', '#cf7e15']):
    df_tsne = pd.read_csv(fname_tsne, sep = '\t')
    images, labels = sample(df_tsne.iloc[:,0:2].values, df_tsne.iloc[:,2], n_sample)
    labels = np.flip(labels)
    images = np.flip(images, axis=0)
    col = np.where(labels == 1, scatter_colors[-1], scatter_colors[0])
    s = np.where(labels == 1, s_train, s_corpus)
    plt.scatter(images[:,0], images[:,1], c=col, s=s, alpha=alpha)

def generate_lookup_data(filepath, treetype):
    infile = os.path.join(filepath, treetype + '.txt')
    outfile = os.path.join(filepath, treetype + '-lookup.txt')
    df = pd.read_csv(infile, delim_whitespace=True)
    df[df['v'] == 1].to_csv(outfile, sep='\t')

def generate_knn_data(filepath, treetype):
    infile = os.path.join(filepath, 'rf-' + treetype + '.txt')
    outfile = os.path.join(filepath, treetype + '-knn.txt')
    df = pd.read_csv(infile, delim_whitespace=True)
    df[df['v'] == 1].to_csv(outfile, sep='\t')


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

from targets.firefox.fx_testcase import *

class Test(FirefoxTest):

    @pytest.mark.details(
        description='Edit a Bookmark from Bookmarks menu',
        locale=['en-US'],
        test_case_id='163195',
        test_suite_id='2525',
        profile=Profiles.TEN_BOOKMARKS
    )
    def run(self, firefox):
        firefox_menu_bookmarks_pattern = Pattern('firefox_menu_bookmarks.png')
        edit_this_bookmark_option_pattern = Pattern('edit_this_bookmark_option.png')

        navigate(LocalWeb.MOZILLA_TEST_SITE)

        test_site_opened = exists(LocalWeb.MOZILLA_LOGO, FirefoxSettings.SITE_LOAD_TIMEOUT)
        assert test_site_opened is True, 'Previously bookmarked Mozilla website is opened'

        open_firefox_menu()

        firefox_menu_bookmarks_exists = exists(firefox_menu_bookmarks_pattern, FirefoxSettings.FIREFOX_TIMEOUT)
        assert firefox_menu_bookmarks_exists is True, 'Firefox menu > Bookmarks exists'

        click(firefox_menu_bookmarks_pattern)

        edit_this_bookmark_option_exists = exists(edit_this_bookmark_option_pattern, FirefoxSettings.FIREFOX_TIMEOUT)
        assert edit_this_bookmark_option_exists, 'Edit This Bookmark option exists'

        click(edit_this_bookmark_option_pattern)

        edit_stardialog_displayed = exists(Bookmarks.StarDialog.EDIT_THIS_BOOKMARK, FirefoxSettings.FIREFOX_TIMEOUT)
        assert edit_stardialog_displayed is True, 'The Edit This Bookmark popup is displayed under the star-shaped ' \
                                                  'icon.'


import random
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from collections import deque

from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.initializers import RandomUniform

class NumberCube:
    def __init__(self, goal):
        self.goal = goal.flatten()
        self.board = self.goal.copy()
        self.row, self.col = np.shape(goal)
        self.state_size = self.row * self.col

    def left(self, idx):
        if idx % self.col != 0:
            self.board[idx], self.board[idx-1] = self.board[idx-1], self.board[idx]

    def up(self, idx):
        if idx >= self.row:
            self.board[idx], self.board[idx-3] = self.board[idx-3], self.board[idx]

    def right(self, idx):
        if idx % self.col != (self.row - 1):
            self.board[idx], self.board[idx+1] = self.board[idx+1], self.board[idx]

    def down(self, idx):
        if idx <= (self.state_size - self.row - 1) :
            self.board[idx], self.board[idx+self.row] = self.board[idx+self.row], self.board[idx]

    def take_action(self,action):
        idx = np.where(self.board == 0)[0][0]
        if action == 0: self.left(idx)
        elif action == 1: self.up(idx)
        elif action == 2: self.right(idx)
        else: self.down(idx)

    def step(self,action):
        self.take_action(action)
        reward = (np.sum(self.board == self.goal.flatten()) - 8) / 9
        done = np.array_equal(self.board, self.goal.flatten())
        return self.board, reward, done

    def reset(self):
        self.board = self.goal.copy()
        [self.take_action(i) for i in [np.random.randint(4) for i in range(np.random.randint(10)+1)]]
        return self.board

# 상태가 입력, 큐함수가 출력인 인공신경망 생성
def build_net(state_size, action_size):
    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Dense(590, input_dim=state_size, activation='relu',
                        kernel_initializer='he_uniform'))
    model.add(tf.keras.layers.Dense(590, input_dim=state_size, activation='relu',
                        kernel_initializer='he_uniform'))
    model.add(tf.keras.layers.Dense(590, input_dim=state_size, activation='relu',
                        kernel_initializer='he_uniform'))
    model.add(tf.keras.layers.Dense(590, input_dim=state_size, activation='relu',
                        kernel_initializer='he_uniform'))
    model.add(tf.keras.layers.Dense(action_size))
    model.compile(loss='mse', optimizer=tf.keras.optimizers.Adam(lr=0.001), metrics=['acc'])
    print(model.summary())
    return model

def Replay_Bellman(main_model, target_model, batch, discnt_rate):

    states = np.vstack([x[0] for x in batch])
    actions = np.array([x[1] for x in batch])
    rewards = np.array([x[2] for x in batch])
    states2 = np.vstack([x[3] for x in batch])
    done = np.array([x[4] for x in batch])

    # 벨만 최적 방정식을 이용한 예측 Q 값
    Q = rewards + discnt_rate * np.max(target_model.predict(states2), axis=1) * ~done

    target = main_model.predict(states)
    target[np.arange(len(states)), actions] = Q

    return states, target

goal = np.array([[1, 2, 3],[4, 0, 6],[7, 8, 9]])
env = NumberCube(goal)
state_size = env.state_size
action_size = 4

Episodes = 10000
discnt_rate = 0.99
batch_size = 64
Re_m = 20000
Target_update_frequency = 5
counters = []
loss,accurancy = [],[]
e_min = 0.01

# 리플레이 메모리, 최대 크기 Re_m = 20000
Re_buffer = deque(maxlen=Re_m)

G = tf.Graph()
with G.as_default():
    # 메인 모델과 타깃 모델 생성
    main_model = build_net(state_size, action_size)
    target_model = build_net(state_size, action_size)

    # 타깃 모델 초기화
    target_model.set_weights(main_model.get_weights())

    for episode in range(Episodes):
        e = 1. / ((episode / 10) + 1)  # 입실론
        if e < e_min:
            e = e_min
        done = False

        count = 0
        # env 초기화
        state = env.reset()
        state = np.reshape(state,[1,state_size]) / 9

        while not done:
            # env.render()  # 행동 모니터링
            count += 1

            # Q-net에서 입실론 탐욕 정책으로 현재 상태에 따른 행동 선택

            if np.random.rand() <= e:
                action = random.randrange(action_size)
            else:
                action = np.argmax(main_model.predict(state))

            # 선택한 행동으로 환경에서 한 타임스텝 진행
            state2, reward, done = env.step(action)
            state2 = np.reshape(state2,[1,state_size]) / 9

            # 리플레이 메모리에 경험 샘플 [s, a, r, s', d] 저장
            Re_buffer.append((state, action, reward, state2, done))
            # 리플레이 메모리가 배치 크기만큼 쌓이면, 무작위로 추출한 배치로 학습
            if len(Re_buffer) > batch_size:
                batch = random.sample(Re_buffer, batch_size)
                states, target = Replay_Bellman(main_model, target_model, batch, discnt_rate)
                hist = main_model.fit(states, target, batch_size=batch_size, epochs=1, verbose=0)

            # 일정 주기마다 타겟 모델 초기화
            if episode % Target_update_frequency == 0:
                target_model.set_weights(main_model.get_weights())

            if count == 15: break

            state = state2
        loss.append(hist.history['loss'][0])
        accurancy.append(hist.history['acc'][0])
        counters.append(count)
        #print("Episode: {:>4}, count: {:>4}, Re_len: {:>4}, e: {:>4.2f}".format(episode, count, len(Re_buffer), e))
        print('episode:',episode,'memory:',len(Re_buffer),'count:',count)

fig, loss_ax = plt.subplots()

acc_ax = loss_ax.twinx()

loss_ax.plot(loss, 'y', label='train loss')

acc_ax.plot(accurancy, 'b', label='train acc')

loss_ax.set_xlabel('epoch')
loss_ax.set_ylabel('loss')
acc_ax.set_ylabel('accuray')

loss_ax.legend(loc='upper left')
acc_ax.legend(loc='lower left')

plt.show()
#plt.plot(counters)


T = int(input())
for t in range(1, T+1):

    N, B = map(int, input().split())
    people = list(map(int, input().split()))
    result = 999999

    for i in range(1<<N):
        part_sum = 0

        for j in range(N):
            if i & (1<<j):
                part_sum += people[j]
                if part_sum >= result:
                    break

        result = part_sum if B <= part_sum <= result else result

    print("#{0} {1}".format(t, result-B))

장훈이의 높은 선반.py

장훈이의 높은 선반.py


import scrapy
import pandas as pd
from arabic_scrapper.helper import load_dataset_lists,selenium_path
from arabic_scrapper.items import GeneralItem
import time
from bs4 import BeautifulSoup
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from lxml import etree
from datetime import datetime

site_list,catagory,main_category,sub_category,platform,media_type,urgency = load_dataset_lists("Diwan crown prince state of kuwait")
now = datetime.now()

class DiwanSpider(scrapy.Spider):
    name = 'diwan'
    def __init__(self):
        self.chrome_options = Options()
        self.chrome_options.add_argument("--headless")
        self.chrome_options.add_argument('--no-sandbox')
        self.chrome_options.add_argument('--disable-dev-shm-usage')
        self.chromedriver=selenium_path()

    def start_requests(self):
        for page,catagori,main_categor,sub_categor,platfor,media_typ,urgenc in zip(site_list,catagory,main_category,sub_category,platform,media_type,urgency):
            url = page
            driver = webdriver.Chrome(self.chromedriver,options=self.chrome_options)
            driver.delete_all_cookies()
            driver.get(url)
            # time.sleep(5)
            html=driver.page_source
            driver.quit()
            soup=BeautifulSoup(html,"lxml") #donwloading the entiring page
            #print("////////////////soup////////////",soup)
            dom = etree.HTML(str(soup))
            # #print("//////////////////dom/////////////////",dom)
            urls=dom.xpath('//*[@class="news-texts"]/a/@href')
            #print("///////////////////all urls/////////////////////////",urls)
            for url in urls:
                page_url="https://cpd.gov.kw/Ar/"+url
                #print("/////////////////////pageurl//////////////////",page_url)
                # yield scrapy.Request(url=page_url,callback=self.link_extractor,meta={"current_url":page_url,"catagory":catagori})
                yield scrapy.Request(url=page_url,callback=self.link_extractor,meta={"current_url":page_url,"catagory":catagori,"main_category":main_categor,"sub_category":sub_categor,"platform":platfor,"media_type":media_typ,"urgency":urgenc})

    def link_extractor(self,response):
        #print("/////////////////////////////News page//////////////////////",response.meta["current_url"])
        #download
        driver = webdriver.Chrome(self.chromedriver,options=self.chrome_options)
        driver.delete_all_cookies()
        driver.get(response.meta["current_url"])
        html=driver.page_source
        driver.quit()
        soup=BeautifulSoup(html,"lxml")
        dom = etree.HTML(str(soup))
        print("################html variable###############",html,type(html))# <class 'str'>
        print("########### Soup variable ##############",soup,type(soup)) #<class 'bs4.BeautifulSoup'>
        print("########### dom variable ##############",dom,type(dom))

        contents=dom.xpath('//*[@class="news-texts"]//p/text()')
        contents="\n\n".join(contents[0:len(contents)])

        diwan_item=GeneralItem()
        diwan_item["news_agency_name"]="Diwan crown prince state of kuwait"
        diwan_item["page_url"]=response.meta["current_url"]
        diwan_item["category"]=response.meta["catagory"]
        diwan_item["title"]=str("".join(dom.xpath('//*[@class="news-texts"]/a/h3/text()')))
        diwan_item["contents"]=contents
        diwan_item["image_url"]="https://cpd.gov.kw"+str("".join(dom.xpath('//*[@class="carousel-item active"]/img/@src')))
        diwan_item["date"]=str("".join(dom.xpath('//*[@class="news-texts"]/p[1]/text()')))
        diwan_item["author_name"]="Diwan crown prince state of kuwait"

        diwan_item["main_category"]=response.meta["main_category"]
        diwan_item["sub_category"]=response.meta["sub_category"]
        diwan_item["platform"]=response.meta["platform"]
        diwan_item["media_type"]=response.meta["media_type"]
        diwan_item["urgency"]=response.meta["urgency"]
        diwan_item["created_at"]=str(now.strftime("%Y:%m:%d %H:%M:%S"))
        diwan_item["updated_at"]=str(now.strftime("%Y:%m:%d %H:%M:%S"))
        diwan_item["deleted_at"]=None
        yield diwan_item


from django.shortcuts import render, redirect, get_object_or_404
from django.contrib import messages
from django.http import JsonResponse
from datetime import datetime
from django.contrib.auth.decorators import login_required
from custadv.models import Custadv, Custqtn
from custadv.forms import CustadvForm, CustqtnForm

@login_required
def custadvList(request, template_name='custadv/custadvList.html'):
    custadvs = Custadv.objects.all()
    ctx = {}
    ctx['custadvs'] = custadvs
    ctx['title'] = 'List'
    return render(request, template_name, ctx)

@login_required
def custadvCreate(request, template_name='custadv/custadvForm.html'):
    form = CustadvForm(request.POST or None)
    if form.is_valid():
        new_cust = form.save(commit=False)
        new_cust.created_by = request.user
        new_cust.last_updated_by = request.user
        new_cust.save()
        messages.success(request, '資料已新增！')
        return redirect('custadv:custadvUpdate', new_cust.id)
    ctx = {}
    ctx['form'] = form
    ctx['title'] = 'New'
    return render(request, template_name, ctx)

@login_required
def custadvView(request, pk, template_name='custadv/custadvView.html'):
    custadv = get_object_or_404(Custadv, pk=pk)
    ctx = {}
    ctx['custadv'] = custadv
    ctx['custqtns'] = Custqtn.objects.filter(custadv=custadv)
    ctx['title'] = 'View'
    return render(request, template_name, ctx)

@login_required
def custadvUpdate(request, pk, template_name='custadv/custadvForm.html'):
    custadv = get_object_or_404(Custadv, pk=pk)
    form = CustadvForm(request.POST or None, instance=custadv)
    if form.is_valid():
        custadv_obj = form.save(commit=False)
        custadv_obj.last_updated_by = request.user
        custadv_obj.save()
        messages.success(request, '資料已更新！')
        return redirect('custadv:custadvUpdate', custadv.id)
    ctx = {}
    ctx['custadv'] = custadv
    ctx['custqtns'] = Custqtn.objects.filter(custadv=custadv)
    ctx['form'] = form
    ctx['title'] = 'Edit'
    return render(request, template_name, ctx)

@login_required
def custadvDelete(request, pk, template_name='custadv/custadvDelete.html'):
    custadv = get_object_or_404(Custadv, pk=pk)
    if request.method == 'POST':
        custadv.delete()
        messages.success(request, '資料已刪除！')
        return redirect('custadv:custadvList')
    ctx = {}
    ctx['custadv'] = custadv
    ctx['custqtns'] = Custqtn.objects.filter(custadv=custadv)
    ctx['title'] = 'Delete'
    return render(request, template_name, ctx)

@login_required
def custadvData(request, pk):
    custadv = get_object_or_404(Custadv, pk=pk)
    ctx = {}
    if request.POST.get('requestData') == 'master':
        ctx['custadv'] = custadv
        template_name = 'custadv/custMaster.html'
    else:
        ctx['custqtns'] = Custqtn.objects.filter(custadv=custadv)
        template_name = 'custadv/custDetail.html'
    return render(request, template_name, ctx)

@login_required
def custqtnNew(request, parent_pk, template_name='custadv/custqtnNew.html'):
    custadv = get_object_or_404(Custadv, pk=parent_pk)
    form = CustqtnForm(request.POST or None)
    if form.is_valid():
        custqtn = form.save(commit=False)
        custqtn.custadv = custadv
        custqtn.created_by = request.user
        custqtn.last_updated_by = request.user
        custqtn.save()
        messages.success(request, '明細資料已新增！')
        return redirect('custadv:custadvUpdate', parent_pk)
    ctx = {}
    ctx["form"] = form
    ctx["custadv"] = custadv
    ctx['custqtns'] = Custqtn.objects.filter(custadv=custadv)
    ctx['title'] = ' New Detail'
    return render(request, template_name, ctx)

@login_required
def custqtnTabledit(request):
    if request.method == 'POST':
        custqtn = get_object_or_404(Custqtn, pk=request.POST.get('id'))
        if request.POST.get('action') == 'edit':
            custqtn.org = request.POST.get('org')
            custqtn.qtndd = datetime.strptime(request.POST.get('qtndd'),"%Y-%m-%d").date()
            custqtn.qtns = request.POST.get('qtns')
            custqtn.last_updated_by = request.user
            custqtn.save()
            messages.success(request, '明細資料已更新！')
        if request.POST.get('action') == 'delete':
            custqtn.delete()
            messages.success(request, '明細資料已刪除！')

        django_messages = []
        for message in messages.get_messages(request):
            django_messages.append({
                "level": message.level,
                "message": message.message,
                "tags": message.tags,
            })
        ctx = {}
        ctx['request'] = request.POST
        ctx['messages'] = django_messages

        return JsonResponse(ctx)
        # return JsonResponse(request.POST)
    else:
        return render(request, 'main/main.html')


import numpy as np
import math

def bincoeff_array(k, n):
    # calculates binomial coefficient: n!
    # n and k are numpy arrays
    # assert (n >= k).all(), "n has to be greater or equal to k! "

    assert (n >= 0).all() and (k >= 0).all(), "n and k both have to be positive!"
    assert n.min() >= k.max(), "every element of n has to be greater or equal to k "

    b = np.zeros((n.size, k.size))

    for i in range(n.size):
        for j in range(k.size):
            b[i, j] = math.factorial(n[i]) / math.factorial(k[j]) \
                                     / math.factorial(n[i] - k[j])
    return b

def bincoeff(k, n):
    # calculates binomial coefficient: n!
    # n and k are scalars

    assert n >= k, "n has to be greater or equal to k!"
    assert n >= 0 and k >= 0, "n and k both have to be positive!"

    b = math.factorial(n) / math.factorial(k) / math.factorial(n - k)

    return b

def bernstein(i, n, t):
    # Calculates the i-th bernstein polynomial of degree n at t
    # i, n ,t are numpy arrays
    # Formula: bincoeff(n,i) * t^i * (1-t)^(n-i)

    t_power_i = np.power(t, i)
    t_power_n_i = np.power(np.ones((t.size,)) - t, n - i)
    B = np.multiply(np.multiply(bincoeff(i, n), t_power_i), t_power_n_i)
    return B

def bezier(P, h):
    # Returns x,y values of a bezier-curve with the given
    # control points P , stepwidth h
    # P is a matrix (2 x n),  h is a scalar

    t = np.arange(0, 1 + h, h)
    [u, v] = np.meshgrid(t, t)
    x = np.zeros(u.shape)
    y = np.zeros(u.shape)
    z = np.zeros(u.shape)
    n = P.shape(1)
    m = P.shape(2)

    for i in range(n):
        for j in range(m):
            x = x + np.multiply(bernstein(i, n, u), bernstein(j, m, v)) * P[0, i, j]
            y = y + np.multiply(bernstein(i, n, u), bernstein(j, m, v)) * P[1, i, j]
            z = z + np.multiply(bernstein(i, n, u), bernstein(j, m, v)) * P[2, i, j]


#from PyQt5 import uic
#from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets  import *
#from PyQt5.QtGui  import *
from PyQt5.uic  import  loadUi
from PyQt5.QtCore import QObject, QThread, pyqtSignal , QRunnable, Qt, QThreadPool ,QMutex
from PyQt5 import QtCore, QtGui
#from PyQt5.QtGui import QMovie
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtWidgets import QLineEdit, QMessageBox, QFileDialog
#from matplotlib.backends.backend_qt5agg  import  ( NavigationToolbar2QT  as  NavigationToolbar )
#import  numpy  as  np
#import  random
#import pandas as pd
import sys
import os
from Report_Analysis import*
from datetime import datetime
from SystemFunction_SWC_Report import SWC_SF_Report_Generation_Runnable
from PolarionPlanAPIs import Polarion_Plan_Runnable
import time
#polarion_object = c.Polarion("https://vseapolarion.vnet.valeo.com/polarion/")
from RTE_API import Task , Task_Flag
import os, shutil

###############################################################################
###################### global variables #######################################

Version={
    "Version": "Version : 4.0.0",
}

Input_Data ={
    "Project":"VW_MEB_Inverter" ,
    "User name": " ",
    "Password": " ",
    "Int_Plan_Doc": " ",
    "Polarion_SW_Plan": " ",
    "RB": "" ,
    "L_Repo": "",
    "Directory": ",",
    "Report_Name": "",
    "SWA_Baseline": "",
    "SF_Mapping": "",
}

Warning_Message ={
    "Warning" : "" ,
}

#########################################################################################
######################### initialization ###############################################

# get current directory
current_directory = os.getcwd()

# Generate Outputs folder
final_directory = os.path.join(current_directory, r'Outputs')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)

# Generate SubFiles folder
final_directory2 = os.path.join(current_directory, r'SubFiles')
if not os.path.exists(final_directory2):
   os.makedirs(final_directory2)

Directory = current_directory.replace("\\", "\\\\")

# delete old output and clear output folders
deleted_directory1 = current_directory+'\SubFiles'
deleted_directory2 = current_directory+'\Outputs'

Delete_Directory_File(deleted_directory1)
Delete_Directory_File(deleted_directory2)

#########################################################################################
# 1. Subclass QRunnable
# main runnable
class Runnable(QRunnable):
    def __init__(self, n):
        super().__init__()
        self.n = n

    def run(self):
        # Get polarion Plan
        infoList1 = []
        infoList1.append(Input_Data["User name"])
        infoList1.append(Input_Data["Password"])
        infoList1.append(Input_Data["Project"])
        infoList1.append(Input_Data["Polarion_SW_Plan"])
        infoList1.append(Input_Data["SWA_Baseline"])

        infoList2 = []
        infoList2.append(Input_Data["User name"])
        infoList2.append(Input_Data["Password"])
        infoList2.append(Input_Data["Project"])
        infoList2.append(Input_Data["SWA_Baseline"])
        infoList2.append(Input_Data["Polarion_SW_Plan"])
        infoList2.append(Input_Data["RB"])
        # Main task
        Main_Task(infoList1, infoList2, Input_Data)
        pass

# main GUI

class MatplotlibWidget(QMainWindow):

    def __init__(self):
        ##print("clicked in init")
        QMainWindow.__init__(self)

        loadUi("Main_Window.ui", self)
        self.show()
        self.UiTimer()
        self.setWindowTitle("SWA Release Tool")

        # creat menue bars
        self.About_Menue = About()
        # tool Bar
        toolbar = QToolBar("Menue tools")
        self.addToolBar(toolbar)
        button_action = QAction("About", self)
        button_action.setStatusTip("About Tool")
        button_action.triggered.connect(self.onMyToolBarButtonClick)
        toolbar.addAction(button_action)
        toolbar2 = QToolBar("Menue tools")

        self.addToolBar(toolbar2)
        button_action2 = QAction("Help", self)
        button_action2.setStatusTip("How to Use tool?")
        button_action2.triggered.connect(self.Help_Menue)
        toolbar2.addAction(button_action2)

        #Get local repo Directory
        self.browse.clicked.connect(self.Get_Directory)

        #Init progress bar
        self.P_Pass.setEchoMode(10)
        self.progressBar.setValue(0)
        self.Version.setText(Version["Version"])
        self.P_Project.currentIndexChanged.connect(self.Select_Proj)

        #self.About.clicked.connect(self.onMyToolBarButtonClick)

        # Run button an execution of main runnable
        self.Run.clicked.connect(self.Script_Runnable)

    # Help Menue Method
    def Help_Menue(self):
        Help_path = os.getcwd()
        file_path = Help_path + "\Help.html"
        os.startfile(file_path)

    # About Menue Method
    def onMyToolBarButtonClick(self):
        self.About_Menue.show()

    # Init Cyclic timer Method -  to refresh main GUI during Running of Runabe and refress program status
    def UiTimer(self):
        # variables
        # count variable
        self.count = 0
        # start flag
        self.start = False
        # creating a timer object
        self.timer = QtCore.QTimer(self)
        # adding action to timer
        self.timer.timeout.connect(self.update_graph)
        # update the timer every tenth second
        self.timer.start(3000)

    # Runnable Initialization method
    def runTasks(self):
        self.Update_GUI_Tasks_names()
        threadCount = QThreadPool.globalInstance().maxThreadCount()
        #self.label.setText(f"Running {threadCount} Threads")
        pool = QThreadPool.globalInstance()
        runnable = Runnable(1)
        # 3. Call start()
        pool.start(runnable)

    # Update Graph method which is called with each timer to Update GUI status
    def update_graph(self):
        # Update current running task
        self.label_10.setText(Task["Name"])

        # Update Progress bar
        self.progressBar.setValue(Task["Progress"])

        # Update Task Complete validation
        self.label___1.setText(Task_Flag["Task1"])
        self.label___2.setText(Task_Flag["Task2"])
        self.label___3.setText(Task_Flag["Task3"])
        self.label___4.setText(Task_Flag["Task4"])
        self.label___5.setText(Task_Flag["Task5"])
        self.label___6.setText(Task_Flag["Task6"])

        # Update warning Message
        self.Warning_Message.setText(Task["Warning_Message"])

    # select current project Method
    def Select_Proj(self):
        Current_Proj= " "

        Current_Cam_Section_Name = str(self.P_Project.currentText())

        if Current_Cam_Section_Name == "VW_MEB_Inverter" :
            Current_Proj="VW_MEB_Inverter"
        elif Current_Cam_Section_Name == "BMW" :
            Current_Proj="obc_35up11kw"
        elif Current_Cam_Section_Name == "PSA":
            Current_Proj="phev_psa_erad_gen2"
        elif Current_Cam_Section_Name == "CEVT":
            Current_Proj="mma_cm1e_dcdc"
        elif Current_Cam_Section_Name == "100-KW":
            Current_Proj="optimus"
        elif Current_Cam_Section_Name == "model_kit":
            Current_Proj="model_kit"
        elif Current_Cam_Section_Name == "DAI":
            Current_Proj="S_DAI_SYS"
        elif Current_Cam_Section_Name == "P2 800V":
            Current_Proj="p2_800v_sic_inv_switching_cell"
        elif Current_Cam_Section_Name == "PSA":
            Current_Proj="phev_psa_erad_gen2"
        Input_Data["Project"] = Current_Proj

    # Get Input from GUI textboxs method
    def Get_Input(self):
        self.Select_Proj()
        Input_Data["User name"]=self.P_User.text()
        Input_Data["Password"] =self.P_Pass.text()

        #Input_Data["Project"] = str(self.P_Project.currentText())
        Input_Data["SWA_Baseline"] = self.SWA_Baseline.text()
        Input_Data["Int_Plan_Doc"] =self.Int_Plan_Document.text()
        Input_Data["Polarion_SW_Plan"] =self.Polarion_Plan.text()
        Input_Data["RB"] =self.Start_RB.text()
        Input_Data["L_Repo"] = self.L_Repo.text()
        Input_Data["Directory"] = Directory
        if self.SF_Mapping.isChecked():
            Input_Data["SF_Mapping"] =1
        else:
            Input_Data["SF_Mapping"] =0

    # Update GUI Task names
    def Update_GUI_Tasks_names(self):
        self.label__1.setText("Create Output report in Subfile Folder")
        self.label__2.setText("Prepare shell script and execute it => Output is Log.txt in subfiles filder")
        self.label__3.setText(
            "Parse Log file and generate Diff logs and pares data into Subfiles/IntegrationReport.xlsx")
        self.label__4.setText(
            "Get polarion plan tasks and WPs and all WIs and verify all Gerrit tickes Ids from polarion and get all there data")
        self.label__5.setText("generate report with system function mapping to SWC from polarion")
        self.label__6.setText("Generate final report")

    # Get Local repo directory Method
    def Get_Directory(self):
        self.dir_path = QFileDialog.getExistingDirectory(self, "Choose Directory", "E:\\")

        self.L_Repo.setText(self.dir_path)
        Input_Data["L_Repo"] = self.L_Repo.text()

    # method to triggered when Run button pressed
    # it is init tasks status
    # and Get Input Data
    # and start the Runnable
    def Script_Runnable(self):
        #print("Getting Input")
        self.label___1.setText("")
        self.label___2.setText("")
        self.label___3.setText("")
        self.label___4.setText("")
        self.label___5.setText("")
        self.label___6.setText("")
        self.Get_Input()
        #print("Finish Input")
        #print("Start Tasks")
        self.runTasks()

# about menue GUI Class
class About(QMainWindow):
    def __init__(self):
        super(About, self).__init__()
        loadUi("About.ui", self)
        self.label_10.setText(Version["Version"])

        # variable
        # Button

# main
if __name__ == '__main__':
    app = QApplication([])
    window  =  MatplotlibWidget ()
    sys.exit(app.exec())


import string
import re

fh = open('aoc7.txt')
rules = []
list_of_bags = []
sum_of_inner_bags = {}
total_sum = {}

for line in fh:
    line = line.strip()
    rules.append(line)

for rule in rules:
    rule = rule.split(' contain ')
    to_sum = re.findall('[0-9]+', rule[1])
    sum = 0
    for i in range(len(to_sum)):
        sum += int(to_sum[i])
    sum_of_inner_bags[rule[0][:-1]] = sum

def recursion(colour):
    if sum_of_inner_bags[colour] == 0:
        return sum_of_inner_bags[colour]
    else:
        sum = 0
        for rule in rules:
            if rule.startswith(colour):
                rule = rule.split(' contain ')
                to_sum = re.findall('[0-9]+', rule[1])
                for i in range(len(to_sum)):
                    sum += int(to_sum[i])
                rule[1] = rule[1].split(', ')
                to_multiply = []
                for bag in rule[1]:
                    if bag == 'no other bags.':
                        break
                    if bag[-1] == 'g':
                        to_multiply.append(bag[2:])
                    elif bag[-2] == 'g':
                        to_multiply.append(bag[2:-1])
                    elif bag[-2] == 's':
                        to_multiply.append(bag[2:-2])
                for i in range(len(to_sum)):
                    sum += int(to_sum[i]) * recursion(to_multiply[i])

        total = sum
        return total

print(recursion('shiny gold bag'))


import paho.mqtt.client as mqtt
import json

def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("connected OK")
    else:
        print("Bad connection Returned code=", rc)

def on_disconnect(client, userdata, flags, rc=0):
    print(str(rc))

def on_subscribe(client, userdata, mid, granted_qos):
    print("subscribed: " + str(mid) + " " + str(granted_qos))

def on_message(client, userdata, msg):
    parsed_data = json.loads(msg.payload.decode("utf-8"))
    print(parsed_data['data'])

    EQIP_ID = 1
    EQIP_ID_CON = 1234
    Current_TEMP = 36.5
    Contect_TEMP_01=36.5
    Contect_TEMP_02=36.5
    Contect_TEMP_03=36.5
    Contect_TEMP_04=36.5
    Contect_TEMP_05=36.5
    CPU_TEMP = 36.5
    Spark = False
    Smoke = False
    Fire = False
    Fire_ext = False
    Sensor_Alarm = False
    Cpu_Alarm = False
    Comm_Alarm = False
    Fire_ext_Controll = False

# 새로운 클라이언트 생성
client = mqtt.Client()
# 콜백 함수 설정 on_connect(브로커에 접속), on_disconnect(브로커에 접속중료), on_subscribe(topic 구독),
# on_message(발행된 메세지가 들어왔을 때)
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_subscribe = on_subscribe
client.on_message = on_message

client.username_pw_set("modules", "12345678")

# address : localhost, port: 1883 에 연결
client.connect('127.0.0.1', 1883)

client.subscribe('application/1/device/ac1f09fffe0b8a54/rx', 1)
client.loop_forever()


import re
file1 = open('day7.input.txt', 'r')
lines = file1.readlines()

bagRelationship = {}
allColor = []
for line in lines:
	line = line.rstrip().split("contain")
	parentColor = re.search(r'.+(?=\s+bags)', line[0]).group()
	allColor.append(parentColor)
	if parentColor not in bagRelationship:
		bagRelationship[parentColor] = []

	childrenBagLine = line[1][1:-1].split(", ") # remove last character
	for baginfo in childrenBagLine:
		childrenColor = re.search(r'(?<=\s)\w+\s\w+(?=\s+bags?)', baginfo)
		childrenNumber = re.search(r'^\d+(?=\s)', baginfo)
		if childrenColor:
			bagRelationship[parentColor].append((childrenColor.group(), childrenNumber.group()))

def part1():
	visited = {}
	ans = 0
	for color in allColor:
		if DFSHelper(color, visited):
			print(color)
			ans += 1

	return ans-1
def DFSHelper(color, visited):
	if color == "shiny gold":
		return True

	if color in visited:
		return visited[color]

	for neighborColor in bagRelationship[color]:
		if DFSHelper(neighborColor[0], visited):
			visited[color] = True
			return True
	visited[color] = False
	return False

def part2():
	visited = {}
	print(part2helper('shiny gold', visited))
	print(visited)

def part2helper(color, visited):
	if color in visited:
		return visited[color]

	totalBag = 0
	for children in bagRelationship[color]:
		childcolor = children[0]
		childnumber = int(children[1])
		totalBag += (childnumber + childnumber*part2helper(childcolor, visited))
	visited[color] = totalBag
	return totalBag


from django.db.models import Sum
from rest_framework import exceptions, generics, mixins, viewsets
from rest_framework.decorators import action
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response

from .models import Game, Match, MatchStatus, Player, Point
from .serializers import (GameSerializer, MatchSerializer,
                          MatchStatusSerializer, PlayerSerializer,
                          PointSerializer)

class PlayerViewset(viewsets.ModelViewSet):
    queryset = Player.objects.all()
    serializer_class = PlayerSerializer

class MatchViewset(viewsets.ModelViewSet):
    queryset = Match.objects.all()
    serializer_class = MatchSerializer

    @action(detail=True, methods=['get'],)
    def info(self, request, pk=None):
        """
        Returns the current status, points and player games won
        for the match that is given
        """
        match_status = get_object_or_404(MatchStatus.objects.all(), match_id=pk)
        points = Point.objects.filter(match_id=pk)
        games = Game.objects.filter(match_id=pk)

        match_status_serializer = MatchStatusSerializer(match_status)
        points_serializer = PointSerializer(points, many=True)
        games_serializer = GameSerializer(games, many=True)

        return Response({
            **match_status_serializer.data,
            'points': points_serializer.data,
            'games': games_serializer.data
        })

class PointUpdateView(generics.GenericAPIView):
    queryset = Point.objects.all()
    serializer_class = PointSerializer
    lookup_fields = ('match', 'player')

    def get_object(self):
        queryset = self.get_queryset()
        filter = {}
        for field in self.lookup_fields:
            filter[field] = self.kwargs[field]

        obj = get_object_or_404(queryset, **filter)
        self.check_object_permissions(self.request, obj)
        return obj

    def get_opponent_score(self):
        qs = Point.objects.filter(match_id=self.kwargs['match']).exclude(player_id=self.kwargs['player'])
        if (qs):
            return qs.first()
        else:
            raise exceptions.ValidationError('Unable to find opponent points')

    def get_match(self):
        queryset = Match.objects.all()
        filter = {}
        filter['id'] = self.kwargs['match']
        obj = get_object_or_404(queryset, **filter)
        self.check_object_permissions(self.request, obj)
        return obj

    def get_match_status(self):
        queryset = MatchStatus.objects.all()
        filter = {}
        filter['match'] = self.kwargs['match']
        obj = get_object_or_404(queryset, **filter)
        self.check_object_permissions(self.request, obj)
        return obj

    def get_match_players(self, match):
        """
        Finds player and opponent based on serve
        """
        player = match.players.all().filter(pk=self.kwargs['player']).get()
        opponent = match.players.all().exclude(pk=self.kwargs['player']).get()
        return player, opponent

    def update_player_score(self, player, new_score):
        """
        Update player score based last serve outcome
        """
        match = self.match
        point_obj = Point.objects.filter(match=match, player=player).get()
        point_obj.score=new_score
        point_obj.save()

    def get_or_create_game(self, match, player, set):
        """
        Create Game record if not exists
        """
        try:
            Game.objects.get(match=match, player=player, set=set)
        except Game.DoesNotExist:
            Game.objects.create(match=match, player=player, set=set, games_won=0)

    def init_game(self):
        """
        Initialize Game win record for players
        """
        match = self.match
        match_status = self.get_match_status()
        player, opponent = self.get_match_players(match)
        self.get_or_create_game(match, player, match_status.current_set)
        self.get_or_create_game(match, opponent, match_status.current_set)

    def game_won(self, player):
        """
        Update player games won
        """
        match = self.match
        match_status = self.get_match_status()

        try:
            obj = Game.objects.get(match=match, player=player, set=match_status.current_set)
            obj.games_won += 1
            obj.save()

        except Game.DoesNotExist:
            Game.objects.create(match=match, player=player, set=match_status.current_set, games_won=1)

    def reset_score(self):
        """
        Reset score for new game
        """
        Point.objects.filter(match_id=self.kwargs['match']).update(score=0)

    def next_set(self):
        """
        Progress match to the next set
        """
        match_status = self.get_match_status()
        match_status.current_set += 1
        match_status.save()

    def match_won(self, player):
        """
        Add player as winner
        """
        match_status = self.get_match_status()
        match_status.winner = player
        match_status.save()

    def announce_event(self, message):
        """
        Log latest event
        """
        match_status = self.get_match_status()
        match_status.latest_event = message
        match_status.save()

    def post(self, request, *args, **kwargs):
        """
        Process match progress by processing each score made by a player
        The point system assumed is as follows:
            0 points = Love
            1 point = 15
            2 points = 30
            3 points = 40
            Tied score = All
            40-40 = Deuce
            Server wins deuce point = Ad-In
            Receiver wins deuce point = Ad-Out
        """
        self.match = self.get_match()
        match_status = self.get_match_status()
        self.player, opponent = self.get_match_players(self.match)
        self.init_game()

        # Read previous scores of the players
        player_score_previous = self.get_object().score
        opponent_score = self.get_opponent_score().score

        # Look for Ad-Out
        MIN_SCORE_FOR_WIN = 4
        DUECE_SCORE = 3
        AD_IN_SCORE = 4
        ad_out = player_score_previous == DUECE_SCORE and opponent_score == AD_IN_SCORE
        if ad_out:
            # opponent looses 1 point
            self.update_player_score(opponent, opponent_score-1)
            player_score_now = player_score_previous

        else:
            # Add point to prev score
            player_score_now = player_score_previous + 1

        # Draw game winner - Is player ahead four points or wins AdIn score ?
        duece_active = player_score_previous == DUECE_SCORE and opponent_score == DUECE_SCORE
        ahead_four_points = not duece_active and player_score_now == MIN_SCORE_FOR_WIN
        score_2adin_points = opponent_score == DUECE_SCORE and player_score_now > MIN_SCORE_FOR_WIN

        if ahead_four_points or score_2adin_points:
            self.game_won(self.player)  # Player won the game
            self.announce_event(f'{self.player.name} won the game')
            self.reset_score()

            # Find games won in current set for player and opponent
            player_games_won_currentset = Game.objects.get(
                    match=self.match, player=self.player, set=match_status.current_set
                ).games_won

            opponent_games_won_currentset = Game.objects.get(
                    match=self.match, player=opponent, set=match_status.current_set
                ).games_won

            # Advantage set rule

            # In an advantage set, a player or team needs to win six games, by two, to win the set.
            # This means that there is no tiebreak game played at 6-6.
            # The set continues until one player/team wins by two games.

            player_won_set = player_games_won_currentset >= 6 \
                and player_games_won_currentset - opponent_games_won_currentset >= 2
            if player_won_set:
                # player won set, move to next set
                if match_status.current_set < self.match.sets:
                    self.next_set()
                else:
                    # All sets finished, now draw match

                    # Find games won in All sets for the player and opponent
                    player_games_won_allsets = Game.objects.filter(
                            match=self.match, player=self.player
                        ).aggregate(Sum('games_won'))['games_won__sum']

                    opponent_games_won_allsets = Game.objects.filter(
                            match=self.match, player=opponent
                        ).aggregate(Sum('games_won'))['games_won__sum']

                    winner = self.player if player_games_won_allsets > opponent_games_won_allsets else opponent
                    self.match_won(winner)
                    self.announce_event(f'{winner.name} won the match!')

        else:
            # Update player score, game continues
            self.update_player_score(self.player, player_score_now)
            self.announce_event(f'{self.player.name} scored the point')

        return Response({'success': True}, status=200)

class ScoreCorrectionView(generics.GenericAPIView, mixins.UpdateModelMixin):
    """
    Provides manual update of the score in case of corrections
    """

    queryset = Point.objects.all()
    serializer_class = PointSerializer
    lookup_fields = ('match', 'player')

    def get_object(self):
        queryset = self.get_queryset()
        filter = {}
        for field in self.lookup_fields:
            filter[field] = self.kwargs[field]

        obj = get_object_or_404(queryset, **filter)
        self.check_object_permissions(self.request, obj)
        return obj

    def put(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)


print(os.getcwd())
#home/user
os.mkdir("new_dir")
os.chdir("new_dir")
os.getcwd()
#'/home/user/new_dir'

#-------------------------------------------------------------------

import os
os.listdir("website")
#['images', 'index.html', 'favicon.ico']
dir = "website"
for name in os.lisdir(dir):
    fullname = os.path.join(dir, name)
    if os.path.isdir(fullname):   # By using os.path.join we can concatenate directories in a way that can be used with other os.path() functions.
        print("{} is a directory".format(fullname))
    else:
        print("{} is a file".format(fullname))

# result would be:
# website/images is a directory
# website/index.html is a file
# website/favicon.ico is a file

# protip - In Linux and MacOS, the portions of a file are split using a forward slash(/). On Windows, they're split using a backslash(\).

Google IT Automation with Python Professional Certificate/Using Python to Interact with the Operating System/directories.py


# Python 3
# Uses PCS exports to produce a traveling salesman solution to ordering the papers for discussion
# Created by Michael Bernstein in June 2019. MIT License.

import csv
from collections import defaultdict
import pprint
from numpy import median
import pandas as pd
import sys
import math
import tsp
import numpy as np

# Group papers into batches of this score range (e.g., .25 = 3.25-3.50) for discussion
# This defines the minimum and maximum score range to be discussed
SCORE_STEPS = [3.01, 3.25, 3.5, 3.75, 4.0, 4.25, 4.5, 4.75]

# Where the output should be written to.
OUTPUT_CSV = 'uist19a_travelingsalesman_order.csv'
# Bidding CSVs from PCS: this is the only CSV that contains conflict information in PCS, so we use it.
# This may be a list of CSVs if needed, since one of the ACs may be conflicted with a paper
# and that one won't show up in their CSV. This just merges all the CSVs in the list.
BIDDING_CSVS = ['uist19a_committee_bidding.csv']
# Submission CSV: we use this as an index of all the papers and their current overall scores
SUBMISSION_CSV = 'uist19a_submission.csv'

# get the set of papers that will be discussed
def load_eligible_papers(filename, min_score, max_score):
    with open(filename) as reviewfile:
        df = pd.read_csv(reviewfile)
        df['Overall Score'] = pd.to_numeric(df['Overall Score'])
        df = df[pd.notnull(df['Overall Score'])]
        df = df[df['Overall Score'] >= min_score]
        df = df[df['Overall Score'] < max_score]
        df = df.sort_values(by=['Overall Score'], ascending=False)
        papers = df['Paper ID'].tolist()
        print(papers)
        return papers

# load the conflicts from the PCS file
def load_conflicts(file_list, eligible_papers):
    conflicts = dict()
    ac_columns = dict()

    for filename in file_list: #will do some redundant work, but that's ok
        with open(filename) as conflictfile:
            reader = csv.reader(conflictfile)
            row_num = 0
            for row in reader:
                row_num += 1
                if row_num == 1 or row_num == 2 or row_num == 4 or row_num == 5: # blank or not useful  lines
                    continue
                elif row_num == 3:
                    for i in range(2, len(row)): # skip the first couple of columns because they just give the paper number and title
                        ac_columns[i] = row[i].split('\n')[0]
                else:
                    sub_id = row[0]
                    if sub_id in eligible_papers:
                        conflicts[sub_id] = list()
                        for i in range(2, len(row)): # skip the first couple of columns because they just give the paper number and title
                            if (i-2) % 3 == 0: # bid column
                                if row[i] == "C": # conflict
                                    conflicts[sub_id].append(ac_columns[i])
    return ac_columns, conflicts

# load the ACs from a PCS file
def load_ACs(filename, eligible_papers):
    acs = defaultdict(list)
    with open(filename) as reviewfile:
        reader = csv.DictReader(reviewfile)
        for row in reader:
            role = row['Role']
            submission = row['\ufeffSub ID']#row['\xef\xbb\xbfSub ID']
            if submission in eligible_papers:
                if role is not None and 'AC' in role:
                    acs[submission].append(row['Reviewer'])
    return acs

# Return pairs of papers that cannot be in the same group: an AC from one paper is conflicted the other paper
def create_costs(conflicts):
    costs = list()
    for paper1 in conflicts.keys():
        paper1_distances = list()
        for paper2 in conflicts.keys():
            paper1_conflicts = set(conflicts[paper1])
            paper2_conflicts = set(conflicts[paper2])
            difference = paper1_conflicts.symmetric_difference(paper2_conflicts)
            cost = 0
            if len(difference) > 0:
                cost = len(difference)#1

            paper1_distances.append(cost)
        costs.append(paper1_distances)
    return costs

# Create a graph coloring, to identify sets of nodes (papers) that are safe to discuss together
def construct_tsp(costs):
    print("Solving traveling salesman problem. Did you know this is NP-hard? COME ON, GIVE ME A SEC!")
    r = range(len(costs))
    dist = {(i, j): costs[i][j] for i in r for j in r}
    tsp_result = tsp.tsp(r, dist)
    return tsp_result[1]

# Split the papers into groups with additional metadata
def split_into_groups(tsp_result, conflicts):
    groups = list()
    step = 0
    papers_per_group = 1 # increase this if you want to batch papers out and send conflicts out for multiple papers at once (e.g., "next two papers, get out") to reduce cross traffic. We ultimately chose not to do this.
    while step < len(tsp_result):
        indices = tsp_result[step:step+1]
        subgroup = [list(conflicts.keys())[paper] for paper in indices]
        groups.append(create_subgroup(subgroup, conflicts))
        step += len(subgroup)

    return groups

# Returns a structured subgroup for a set of papers
def create_subgroup(subgroup, conflicts):
    group_conflicts = set()
    for subgroup_paper in subgroup:
        if subgroup_paper in conflicts:
            group_conflicts.update(conflicts[subgroup_paper])
    return {
        'papers': subgroup,
        'conflicts': group_conflicts
    }

def print_moves(title, groups, ac_columns):
    num_papers = 0
    is_in_room = defaultdict(lambda: True)
    total_movements = 0
    at_least_one_movements = 0
    for i, group in enumerate(groups):
        num_papers += len(group['papers'])

        # calculate symmetric difference
        paper1_conflicts = group['conflicts']
        if i > 0:
            paper2_conflicts = groups[i-1]['conflicts']
        else:
            paper2_conflicts = set()
        difference = paper1_conflicts.symmetric_difference(paper2_conflicts)

        print("%s: %d movements, now out of room: %s" % (group['papers'], len(difference), group['conflicts']))
        has_at_least_one_move = False
        for ac in set(ac_columns.values()):
            is_conflicted = ac in group['conflicts']

            if is_in_room[ac] and is_conflicted:
                total_movements += 1
                is_in_room[ac] = False
                has_at_least_one_move = True
            elif not is_in_room[ac] and not is_conflicted:
                total_movements += 1
                is_in_room[ac] = True
                has_at_least_one_move = True
        if has_at_least_one_move:
            at_least_one_movements += 1
    print("%s: total person movements in and out of the room: %d" % (title, total_movements))
    print("%s: total papers requiring at least one move in or out: %d" % (title, at_least_one_movements))
    print("%s: %d total papers" % (title, num_papers))

# how much time do ACs spend out of the room?
def print_AC_stats(ac_columns, all_conflicts, groups, all_eligible_papers):

    print_moves("If we go by score descending order", [create_subgroup([paper], all_conflicts) for paper in all_eligible_papers], ac_columns)
    print("\n\n\n-----\n\n\n")
    print_moves("If we go by TSP order", groups, ac_columns)

# output the CSV with the papers, the order to run them in, and the decision label to give them
def append_CSV(writer, groups, cur_min_score):
    for i, group in enumerate(groups):
        for paper in group['papers']:
            # in order for the descending sort to work in PCS, we need to actually apply a label that is the reverse of the intended traversal order. In other words, TS3.00-1 will be the last one in the sort, and TS3.00-9 (assuming nine entries) will be the first one. So, we reverse the order of the labels, which keeps us with the original order. Technically this isn't necessary because the traveling salesman solution is reversible, but this way the output from the program in the terminal matches the traversal order in PCS.
            d = {
                'paper': paper,
                'group': cur_min_score,
                'order': i+1,
                'label': 'TS%.2f-%d' % (cur_min_score, len(groups) - i),
                'conflicts': group['conflicts'] if len(group['conflicts']) > 0 else '{}'
            }
            writer.writerow(d)

if __name__ == "__main__":
    all_groups = list()
    all_conflicts = dict()
    all_ac_columns = dict()
    all_eligible_papers = set()

    with open(OUTPUT_CSV, 'w') as output_csv:
        writer = csv.DictWriter(output_csv, fieldnames=['paper', 'group', 'order', 'label', 'conflicts'])
        writer.writeheader()

        # We will iterate by score range (e.g., 3.5-3.25, then 3.25-3.0, then...)
        for index in range(len(SCORE_STEPS)-1):
            cur_min_score = SCORE_STEPS[index]
            cur_max_score = SCORE_STEPS[index+1]
            # Submissions CSV from PCS
            eligible_papers = load_eligible_papers(SUBMISSION_CSV, cur_min_score, cur_max_score)
            print("Range [%.2f, %.2f): %d papers" % (cur_min_score, cur_max_score, len(eligible_papers)))
            all_eligible_papers.update(eligible_papers)

            ac_columns, conflicts = load_conflicts(BIDDING_CSVS, eligible_papers)
            print("Conflicts loaded for %d ACs on %d papers." % (len(ac_columns.keys()), len(conflicts.keys())))
            all_conflicts.update(conflicts)

            # Now, create the distances of traversing from each paper to each other paper
            costs = create_costs(conflicts)
            print("Cost structure:")
            print(costs)

            # Solve the traveling salesman problem
            tsp_result = construct_tsp(costs)
            print('Recommended path: %s' % tsp_result)

            # Slim the coloring down into groups of reasonable size
            #inverted_coloring = descriptive_stats(coloring, conflicts)
            groups = split_into_groups(tsp_result, conflicts)
            all_groups.extend(groups)
            all_ac_columns = ac_columns

            append_CSV(writer, groups, cur_min_score)

    # Print some checking information about how much the ACs are out of the room
    print_AC_stats(all_ac_columns, all_conflicts, all_groups, all_eligible_papers)


from requests import get
from requests.exceptions import RequestException
from contextlib import closing
from bs4 import BeautifulSoup
import time
import argparse
import re

def simple_get(url):
    """
    Attempts to get the content at `url` by making an HTTP GET request.
    If the content-type of response is some kind of HTML/XML, return the
    text content, otherwise return None.
    """
    time.sleep(.3)
    try:
        with closing(get(url, stream=True)) as resp:
            if is_good_response(resp):
                return resp.content
            else:
                return None

    except RequestException as e:
        log_error('Error during requests to {0} : {1}'.format(url, str(e)))
        return None

def is_good_response(resp):
    """
    Returns True if the response seems to be HTML, False otherwise.
    """
    content_type = resp.headers['Content-Type'].lower()
    return (resp.status_code == 200
            and content_type is not None
            and content_type.find('html') > -1)

def log_error(e):
    """
    It is always a good idea to log errors.
    This function just prints them, but you can
    make it do anything.
    """
    print(e)

parser = argparse.ArgumentParser(description='Download Latin names from the web.')
parser.add_argument('output', help='the output file')

args = parser.parse_args()
# print(args.output)

f = open(args.output, "w")
conversion = {}

for code in range(ord('a'), ord('z') + 1):
    letter = chr(code)
    if letter in ("x", "y"):
        continue
    print("[INFO] Letter " + letter)
    addr = 'http://rbms.info/lpn/' + letter + '/'
    # print(addr)
    raw_html = simple_get(addr)
    html = BeautifulSoup(raw_html, 'html.parser')
    for a in html.select(".entry-content td a"):
        try:
            link = a['href']
            if "#" in link:
                continue
            if not "http://" in link:
                continue
            # print(link)
            name_td = ""
            try:
                name_td = a.parent.parent.select("td:nth-of-type(2)")[0]
            except IndexError:
                continue
            name_links = name_td.select("a")
            modern_name = a.parent.parent.select("td:nth-of-type(2)")[0].getText()
            if len(name_links) > 0:
                modern_name = name_links[0].getText()
            modern_name = modern_name.replace("\n", " ");
            modern_name = re.sub("\\[.*\\]", "", modern_name)

            # print(modern_name)

            if not modern_name:
                print("[ERR] Missing vernacular name for " + link)
                continue

            page_raw_html = simple_get(link)
            page_html = BeautifulSoup(page_raw_html, 'html.parser')
            for p in page_html.select(".entry-content p"):
                text = p.text
                text = text.replace("\n", " ")
                m = re.search("\\((.*?)=", text)
                if m is None:
                    continue
                parts = m.group(1).split(",")
                parts = [part.strip() for part in parts]
                for part in parts:
                    m = re.search("[a-zA-Z]", part)
                    if m is None:
                        print("[ERR] No letters in " + part)
                        continue
                    part = re.sub("\\[.*\\]", "", part)

                    f.write(part)
                    v_parts = modern_name.split("=")
                    for v_part in v_parts:
                        v_part = v_part.strip()
                        f.write("\t")
                        f.write(v_part)
                    f.write("\n")
                # print(parts)
        except KeyError:
            pass
    # for td in html.select(".entry-content tr td:first-child"):
    #     text = str(td)
    #     text = text.replace(">see also", ">")
    #     text = text.replace("> see also", ">")
    #     text = text.replace(">see ", ">")
    #     text = text.replace("> see ", ">")
    #     text = text.replace("*", "")
    #     parts = text.split("<br/>")
    #     for part in parts:
    #         clean = BeautifulSoup(part, "lxml").text.strip()
    #         f.write(clean)
    #         f.write("\n")

f.close()


# Python driver script that loads an input image from disk and then
# applies FFT blur detection to it.
# USAGE:
# python blur_detector_image.py --image images

# import the necessary packages
from fourier_blur import detect_blur_fft
import numpy as np
import argparse
import imutils
import cv2
import os

# construct the argument parser and parse four command line arguments
    # --image: the path to the input image for blur detection
    # --thresh: the threshold for blur detector calculation
    # --vis: flag indicating whether to visualize the input image
    # and the magnitude spectrum image
    # --test: test results by progressively blurring input image and
    # conduct FFT-based blur detection on each image
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", type=str, required=True,
	help="path input image used to detect blur")
ap.add_argument("-t", "--thresh", type=int, default=20,
	help="threshold for blur detector to fire")
ap.add_argument("-v", "--vis", type=int, default=-1,
	help="whether or not intermediary steps will bev visualized")
ap.add_argument("-d", "--test", type=int, default=-1,
	help="whether or not image should be progressively blurred")
args = vars(ap.parse_args())

# loop over images
for imagePath in os.listdir(args["image"]):
    image = args["image"] + "\\" + imagePath
    print(image)
# load the input image from disk, resize it, and convert it to
# grayscale
    orig = cv2.imread(image)
    orig = imutils.resize(orig, width=500)
    gray = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY)

    # apply blur detector using the FFT function
    (mean, blurry) = detect_blur_fft(gray, size=60,
        thresh=args["thresh"], vis=args["vis"] > 0)

    # draw on the image, indicating whether or not it is blurry
    # add two more channels to single-channel gray image, storing
    # the result in image
    image = np.dstack([gray] * 3)
    color = (0, 0, 255) if blurry else (0, 255, 0)
    # set color as red(if blurry) and green(if not blurry)
    text = "Blurry ({:.4f})" if blurry else "Not Blurry ({:.4f})"
    # draw blurry text indication and mean value in top-left corner
    # of image, print out same info in terminal
    text = text.format(mean)
    cv2.putText(image, text, (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
        color, 2)
    print("[INFO] {}".format(text))

    # show the output image
    # cv2.imshow("Output", image)
    # cv2.waitKey(0)

    # check to see if are going FFT blurriness detector is tested
    # using various sizes of a Gaussian kernel
    if args["test"] > 0:
        # loop over various blur radii
        for radius in range(1, 30, 2):
            # clone the original grayscale image
            image = gray.copy()

            #check to see if the kernel radius is greater than zero
            if radius > 0:
                # blur the input image by the supplied radius using a
                # Gaussian kernel
                image = cv2.GaussianBlur(image, (radius, radius), 0)

                # apply our blur detector using the FFT
                (mean, blurry) = detect_blur_fft(image, size=60,
                    thresh=args["thresh"], vis=args["vis"] > 0)

                # draw on the image, indicating whether or not it is
                # blurry
                image = np.dstack([image] * 3)
                color = (0, 0, 255) if blurry else (0, 255, 0)
                text = "Blurry ({:.4f})" if blurry else "Not Blurry ({:.4f})"
                text = text.format(mean)
                cv2.putText(image, text, (10, 25), cv2.FONT_HERSHEY_SIMPLEX,
                    0.7, color, 2)
                print("[INFO] Kernel: {}, Result: {}".format(radius, text))

            # show the image
            cv2.imshow("Test Image", image)
            cv2.waitKey(0)


import math

class XpThresholds:

    EASY   = 0
    MEDIUM = 1
    HARD   = 2
    DEADLY = 3

    challenge = [ EASY, MEDIUM, HARD, DEADLY ]

    def __init__(self, tables):
        self.tables = tables

    def get(self, level, challenge):
        if level < 1 or level > 20:
            raise ValueError()

        if challenge not in self.challenge:
            raise ValueError()

        return self.tables[level-1][challenge]

class Multipliers:

    def __init__(self, table):
        self.parseTable(table)

    def parseTable(self, table):
        self.table = {}
        self.categories = []
        for label, multiplier in table:
            multiplier = float(multiplier.replace("x ", "").replace("× ", ""))

            category = self.extractList(label)
            self.categories.append(category)

            for number in category:
                self.table[number] = multiplier

        self.categories = list(sorted(self.categories))

    def extractList(self, label):
        def extractRangeList(label, separator):
            values = label.split(separator)
            values = list(map(int, values))
            values = values[0], values[1] +1 # ranges class is non-inclusive
            values = list(range(*values))
            return values

        if "or more" in label:
            value = label.replace(" or more", "")
            value = int(value)
            self.max = value
            return [value]
        elif "–" in label:
            return extractRangeList(label, "–")
            return values
        elif "-" in label:
            return extractRangeList(label, "-")
        else:
            return [int(label)]

    def get(self, numberOfEnemies):
        if numberOfEnemies < 1:
            raise ValueError()
        elif numberOfEnemies > self.max:
            return self.table[self.max]
        else:
            return self.table[numberOfEnemies]

    def getCategories(self):
        return self.categories

class ChallengeTable:
    def __init__(self, thresholds, multipliers):
        self.thresholds  = thresholds
        self.multipliers = multipliers

        self.createRows()

    def createRows(self):
        def convertLabel(label):
            if len(label) > 1:
                return "{} - {}".format(label[0], label[-1])
            else:
                return str(label[0])

        labels = self.multipliers.getCategories()
        rows     = list(map(convertLabel, labels))
        rows[-1] = rows[-1] + "+"

        self.rows = list(zip(rows, map(lambda x: x[0], labels)))

    def compute(self, partyLevels):
        def computeBaseline(challenge):
            return sum([ self.thresholds.get(level, challenge) for level in partyLevels ])

        def computeValue(base, multi):
            return math.ceil(base / multi)

        easy   = computeBaseline(XpThresholds.EASY)
        medium = computeBaseline(XpThresholds.MEDIUM)
        hard   = computeBaseline(XpThresholds.HARD)
        deadly = computeBaseline(XpThresholds.DEADLY)

        baseline = [easy, medium, hard, deadly]

        result = []

        for label, minFoeThreshold in self.rows:
            multi = self.multipliers.get(minFoeThreshold)
            result.append([label] + list(map(lambda x: computeValue(x, multi), baseline)))

        return result