import tensorflow as tf
import os
import sys
import data_generation
import networks
import scipy.io as sio
import param
import util
import truncated_vgg
from keras.backend.tensorflow_backend import set_session
from keras.optimizers import Adam
import scipy.misc

def train(model_name, gpu_id):

    with tf.Session() as sess:

        params = param.get_general_params()

        network_dir = params['model_save_dir'] + '/' + model_name

        # Creates models directory if not exist.
        if not os.path.isdir(network_dir):
            os.mkdir(network_dir)

        train_feed = data_generation.create_feed(params, params['data_dir'], 'train')
        test_feed  = data_generation.create_feed(params, params['data_dir'], 'test')

        os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        set_session(tf.Session(config=config))

        vgg_model = truncated_vgg.vgg_norm()
        networks.make_trainable(vgg_model, False)
        response_weights = sio.loadmat('../data/vgg_activation_distribution_train.mat')
        model = networks.network_posewarp(params)
        model.compile(optimizer=Adam(lr=1e-4), loss=[networks.vgg_loss(vgg_model, response_weights, 12)])

        n_iters = params['n_training_iter']

        summary_writer = tf.summary.FileWriter("D:\Proyectos\JEJU2018\Code\posewarp-cvpr2018\code\logs", graph=sess.graph)

        tr_x, tr_y = next(train_feed)
        te_x, te_y = next(test_feed)

        # Prepare output directories if they don't exist.
        output_dir = '../output/' + model_name + '/'

        if not os.path.isdir(output_dir):
            os.mkdir(output_dir)

        scipy.misc.imsave('../output/tr_orig_image.png', tr_x[0][0, :, :, :])
        scipy.misc.imsave('../output/tr_targ_image.png', tr_y[0, :, :, :])
        scipy.misc.imsave('../output/te_orig_image.png', te_x[0][0, :, :, :])
        scipy.misc.imsave('../output/te_targ_image.png', te_y[0, :, :, :])

        for step in range(0, n_iters):
            x, y = next(train_feed)

            train_loss = model.train_on_batch(x, y)

            util.printProgress(step, 0, train_loss)

            # out = sess.run(conv, feed_dict={"input_1:0" : x[0]})
            # plt.matshow(out[0, :, :, 0])
            # plt.show()

            gen = tf.get_default_graph().get_tensor_by_name("loss/add_2_loss/lambda_5/add:0")
            inp = tf.get_default_graph().get_tensor_by_name("in_img0:0")
            out = tf.get_default_graph().get_tensor_by_name("in_img1:0")
            p_s = tf.get_default_graph().get_tensor_by_name("mask_src/truediv:0")
            # p_t = tf.get_default_graph().get_tensor_by_name("in_pose1:0")

            image_summary_1 = tf.summary.image('images', [inp[0, :, :, :], out[0, :, :, :], gen[0, :, :, :]],
                                               max_outputs=100)
            # image_summary_2 = tf.summary.image('pose', [tf.reduce_sum(p_s[0, :, :, :], 2, keepdims=True)], max_outputs=100)

            image_summary_1 = sess.run(image_summary_1,
                                       feed_dict={"in_img0:0": x[0], "in_pose0:0": x[1], "in_pose1:0": x[2],
                                                  "mask_prior:0": x[3], "trans_in:0": x[4], "in_img1:0": y})

            # image_summary_2 = sess.run(image_summary_2, feed_dict={"in_img0:0" : x[0], "in_pose0:0" : x[1], "in_pose1:0" : x[2],
            #                                                     "mask_prior:0" : x[3], "trans_in:0" : x[4], "in_img1:0" : y})

            summary_writer.add_summary(image_summary_1)
            # summary_writer.add_summary(image_summary_2)

            train_image = sess.run(gen, feed_dict={"in_img0:0": tr_x[0], "in_pose0:0": tr_x[1], "in_pose1:0": tr_x[2],
                                                   "mask_prior:0": tr_x[3], "trans_in:0": tr_x[4], "in_img1:0": tr_y})

            test_image = sess.run(gen, feed_dict={"in_img0:0": te_x[0], "in_pose0:0": te_x[1], "in_pose1:0": te_x[2],
                                                  "mask_prior:0": te_x[3], "trans_in:0": te_x[4], "in_img1:0": te_y})
            if step > 0 and step % params['model_save_interval'] == 0:
                model.save_weights(network_dir + '/' + str(step) + '.h5')

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Need model name and gpu id as command line arguments.")
    else:
        train(sys.argv[1], sys.argv[2])


import datetime

from django import forms
from django.core.exceptions import ValidationError

from .models import TimeLog, Subject, Tag

class DateForm(forms.Form):
    def __init__(self, *args, **kwargs):
        self.min_date = kwargs.pop('min_date')
        self.max_date = kwargs.pop('max_date')
        # if user has no record (min_date and max_date is None) then disable the date inputs else set the min and max attrs
        if self.min_date and self.max_date:
            self.base_fields['start'].widget.attrs['min'] = self.min_date.isoformat()
            self.base_fields['start'].widget.attrs['max'] = self.max_date.isoformat()
            self.base_fields['end'].widget.attrs['min'] = self.min_date.isoformat()
            self.base_fields['end'].widget.attrs['max'] = self.max_date.isoformat()
        else:
            self.base_fields['start'].widget.attrs['disabled'] = True
            self.base_fields['end'].widget.attrs['disabled'] = True

        super().__init__(*args, **kwargs)

    start = forms.DateField(label='From')
    end = forms.DateField(label='To')

    def clean(self):
        # if min_date or max_date is none, it means that user has no record yet
        if self.min_date is None:
            raise ValidationError("You don't have any record!")

        cleaned_data = super().clean()
        start = cleaned_data.get('start')
        end = cleaned_data.get('end')
        if start and end:
            if start > end:
                raise ValidationError('Your selected start date is greater then selected end date')
            if not (self.min_date <= start <= self.max_date and self.min_date <= end <= self.max_date):
                raise ValidationError(f'Your records date are between {self.min_date} and {self.max_date}')
        return cleaned_data

class TimeLogForm(forms.ModelForm):
    hours = forms.IntegerField(min_value=0, max_value=24)
    minutes = forms.IntegerField(min_value=0, max_value=59)

    def __init__(self, *args, **kwargs):
        self.registrant_user = kwargs.pop('registrant_user', None)
        super().__init__(*args, **kwargs)
        # add registrant user's subjects and tags to the corresponding field choices
        self.fields['subject'].queryset = self.registrant_user.subject_set.all()
        self.fields['tags'].queryset = self.registrant_user.tag_set.all()
        # add html attribute to the widget of fields
        self.fields['subject'].widget.attrs['class'] = 'form-select'
        self.fields['tags'].widget.attrs['class'] = 'form-select'
        self.fields['tags'].widget.attrs['size'] = '3'
        self.fields['date'].widget.attrs['class'] = 'form-control'
        self.fields['hours'].widget.attrs['class'] = 'form-control'
        self.fields['minutes'].widget.attrs['class'] = 'form-control'
        self.fields['description'].widget.attrs['class'] = 'form-control'

    class Meta:
        model = TimeLog
        exclude = ['user', 'duration']
        widgets = {
            'date': forms.DateInput(attrs={'type': 'date', 'max': datetime.date.today}),
        }

    def clean(self):
        clean_data = super().clean()
        hours = clean_data.get('hours')
        minutes = clean_data.get('minutes')
        date = clean_data.get('date')

        # calculate and check if the duration is valid
        if hours is not None and minutes is not None and date:
            # calculate duration minutes based on hours and minutes
            duration = (hours * 60) + minutes
            if duration == 0:
                raise ValidationError("Both hour and minute fields can not be 0.")

            if duration > 1440:
                raise ValidationError("One day is 24 hours!")

            # check the particular date's durations doesn't exceed 24 hours
            previous_durations_total = 0
            for timelog in self.registrant_user.timelogs.filter(date=date):
                previous_durations_total += timelog.duration

            if (previous_durations_total + duration) > 1440:
                remaind_hours = (1440 - previous_durations_total) // 60
                remaind_miuntes = (1440 - previous_durations_total) % 60

                if remaind_miuntes or remaind_hours:
                    raise ValidationError(f'Your remaind duration for '
                                          f'{date} is {remaind_hours} hours and {remaind_miuntes} minutes.')
                else:
                    raise ValidationError(f'There is no time left for {date}')

            clean_data['duration'] = duration

        return clean_data

class SubjectForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.user_subjects = kwargs.pop('user_subjects')
        super().__init__(*args, **kwargs)
        # add bootstarp class to the fields
        for v in self.visible_fields():
            v.field.widget.attrs['class'] = 'form-control'
    class Meta:
        model = Subject
        fields = ['name', 'description']

    def clean(self):
        clean_data = super().clean()
        name = clean_data.get('name')
        if name:
            if name.lower() in [s.name for s in self.user_subjects]:
                raise ValidationError(f'{name.lower()} already exists.')
            clean_data['name'] = name.lower()

        return clean_data

class TagForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.user_tags = kwargs.pop('user_tags')
        super().__init__(*args, **kwargs)
        # add bootstarp class to the name field
        self.fields['name'].widget.attrs['class'] = 'form-control'

    class Meta:
        model = Tag
        fields = ['name']

    def clean(self):
        clean_data = super().clean()
        name = clean_data.get('name')
        if name:
            if name.lower() in [s.name for s in self.user_tags]:
                raise ValidationError(f'{name.lower()} already exists.')
            clean_data['name'] = name.lower()

        return clean_data


import sys,time,unittest
from selenium.webdriver.common.by import By
from selenium import webdriver
sys.path.append(".//")
sys.path.append(sys.path[0].split("ATQ项目")[0] + 'ATQ项目\\02.方法模块')
import Function_temp  as F

class ATQtest(unittest.TestCase):
    #driver全局变量
    option =webdriver.FirefoxOptions()
    option.set_headless()
    dr1 = webdriver.Firefox(firefox_options=option)#设置对象
    dr = webdriver.Firefox()#设置对象
    F.driver=dr
    def setUp(self,driver = dr):
        self.driver =driver#设置对象
        self.base_url = "http://11.8.127.248:8080/atq/frame.jsp"#网址
        self.username='sunhongbin'#登录用户名

    def test_01_login(self):
        #data
        print("登录ATQ！")
        username = self.username
        #page_element
        用户名输入框=(By.NAME,'loginId')
        密码输入框=(By.NAME,'password')
        登录按钮=(By.LINK_TEXT,'登录')
        #Script
        driver = self.driver
        self.driver.get(self.base_url)
        F.find_element(用户名输入框).clear()
        F.find_element(用户名输入框).send_keys(username)
        F.find_element(密码输入框).send_keys('123456')
        F.find_element(登录按钮).click()
        #等待
        time.sleep(3)

    def test_02_case_import(self):
        #data
        print("进入案例管理页面！")
        driver = self.driver
        #case_path=self.case_path
        #page
        案例管理菜单=(By.XPATH,"//span[contains(text(),'案例管理')]")
        需求管理菜单=(By.XPATH,"//span[contains(text(),'需求管理')]")
        xf=(By.XPATH,"/html/body/div[3]/div/div/div[2]/div[2]/div/iframe")
        加载提示=(By.XPATH,"//div[contains(text(),'Loading')]")
        导入案例按钮=(By.XPATH,"//span[text()='导入案例']")
        #Script
        print("导入案例！")
        F.find_element(案例管理菜单).click()
        time.sleep(2)
        #切换iframe页面
        #driver.switch_to.frame(F.find_element(xf).find_element())
        F.switch_to.frame(xf)
        print("切换成功？")
        time.sleep(5)
        if F.find_element(需求管理菜单):
            F.find_element(需求管理菜单).highlight()
            print(F.find_element(需求管理菜单))
            print("失败")
        else:
            print("成功")
        print("等待【案例管理_案例列表】页面加载...")
        print(F.find_elements(加载提示))
        for i in range(0,30):
            refresh=F.find_elements(加载提示).find_elements()
            print()
            if len(refresh)<2:
                print("【案例管理_案例列表】页面加载完成！")
                F.find_element(导入案例按钮).click()
                break
            else:
                print(i)
                time.sleep(3)
        time.sleep(2)
        #上传按钮
        upload=driver.find_element_by_id("upload")
        upload.send_keys(case_path)
        value=upload.get_attribute('value')
        if value!="":
            print("文件上传成功！")
        driver.find_element_by_xpath("//span[text()='确定']").click()
        time.sleep(2)
        #判断页面的元素检查是否正在导入，默认先等30s
        if len(driver.find_elements_by_xpath("//div[contains(text(),'导入中，请稍候')]"))>0:
            print("导入中，请耐心等候...")
            time.sleep(30)
        #30s后通过判断导入结束的弹出窗口判断是否导入完毕，如果没有找到窗口则等待继续寻找窗口，直至寻找成功
        #回到主frame页
        driver.switch_to.default_content()
        for i in range(0,100):
            try:
                text=driver.find_element_by_xpath("/html/body/div[10]/div[2]").text
                print("案例导入完毕！")
                break
            except:
                time.sleep(0.01)
        time.sleep(5)
        #通过判断Loading元素，目录树页面是否加载成功，如果未加载成功则等待2s，反复循环
        self.driver.switch_to.frame(xf)
        print("等待【案例管理_案例目录】页面加载...")
        for i in range(0,100):
            refresh= driver.find_elements_by_xpath("//div[contains(text(),'Loading')]")
            if len(refresh)>0:
                time.sleep(2)
            else:
                print("【案例管理_案例目录】页面加载完成！")
                break
        #创建excel对象并取excel中的数据去判断目录树对应的目录是否已经存在，以此判断案例是否导入成功
        ex=openpyxl.load_workbook(case_path)
        sh=ex[ex.sheetnames[0]]
        print("案例目录检查...")
        if self.isElementExist("by.xpath","//span[text()='"+sh['C2'].value+"']/../../../following-sibling::tr[1]//span[text()='"+sh['D2'].value+"']"):
            print("案例目录检查完毕，案例目录存在，案例导入成功！")
        else:print("案例目录检查完毕，未发现案例目录，案例导入失败！")
        #回到主frame页
        driver.switch_to.default_content()
        time.sleep(5)

    def tearDown(self):
        pass
        #self.driver.quit()#这里有多个test需要用到driver，所以tearDown中，不要关闭浏览器

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


"""empty message

Revision ID: 9c5fa6db20f1
Revises: ar399258p714
Create Date: 2023-03-06 13:56:47.958406

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '9c5fa6db20f1'
down_revision = 'ar399258p714'
branch_labels = None
depends_on = None

def upgrade():
    op.add_column('finding', sa.Column('column_start', sa.Integer(), nullable=False, server_default=sa.text("0")))
    op.add_column('finding', sa.Column('column_end', sa.Integer(), nullable=False, server_default=sa.text("0")))
    op.drop_constraint('uc_finding_per_branch', 'finding', type_='unique')
    op.create_unique_constraint('uc_finding_per_branch', 'finding',
                                ['commit_id', 'branch_id', 'rule_name', 'file_path', 'line_number',
                                 'column_start', 'column_end'])

def downgrade():
    op.drop_constraint('uc_finding_per_branch', 'finding', type_='unique')
    op.create_unique_constraint('uc_finding_per_branch', 'finding',
                                ['commit_id', 'branch_id', 'rule_name', 'file_path', 'line_number'])
    op.drop_column('finding', 'column_start')
    op.drop_column('finding', 'column_end')


#!/usr/bin/env python3
import queries
import connection_handler

from IPython import embed
import mysql.connector

import grammar_format

from dotenv import load_dotenv
from managers.word_manager import Word_Manager

import re
load_dotenv()

class Phrase_Manager:
    def __init__(self, phrase="None", person="None", person_manager="None"):
        self.person = person
        self.person_manager = person_manager
        lower_phrase = phrase.lower()
        result = lower_phrase.find("?")
        self.new_phrase = self.remove_bad_chars(lower_phrase)
        self.parsed_phrase = self.new_phrase.split()

        if lower_phrase == 'teach':
            self.teach_phrase()
        else:
            if result == -1:
                print("That is a statement")
            else:
                self.get_question_format()

    def get_question_format(self):
        question_format = ''

        for word in self.parsed_phrase:
            word_manager = Word_Manager(word)
            question_format += grammar_format.assign_part_of_speech(
                word_manager.word)

    def check_for_assigning_attribute(self):
        if self.possessive and self.check_for_attribute():
            self.assign_attribute()

    def check_for_attribute(self):
        attribute_reference = self.parsed_phrase.index("is")
        attribute_index = attribute_reference - 1
        self.attribute = self.parsed_phrase[attribute_index]
        if self.attribute == 'name':
            try:
                first_or_last = self.parsed_phrase[attribute_index - 1]
                self.attribute = first_or_last + "_" + self.attribute
            except Exception as e:
                print("Exception has occured 48: " + str(e))
        self.get_new_value()
        if hasattr(self.person, self.attribute):
            return True
        else:
            return False

    def get_new_value(self):
        self.new_value_index = self.parsed_phrase.index("is") + 1
        self.new_value = self.parsed_phrase[self.new_value_index]
        if self.attribute == 'first_name' or self.attribute == 'last_name':
            self.new_value = self.new_value.capitalize()

    def assign_attribute(self):
        self.person_manager.update_person(
            self.person.id, self.attribute, self.new_value)
        print("Updated!")

    def determine_if_possessive(self):
        self.establish_new_connection()
        word = self.parsed_phrase[0]
        try:
            self.cursor.execute(queries.check_possessive(word))
        except Exception as e:
            print("Exception has occured 40: " + str(e))

        result = self.cursor.fetchall()

        self.list_result = [list(i) for i in result]
        if 'is' in self.parsed_phrase:
            if self.check_exists_result(self.list_result):
                self.possessive = True
        else:
            self.possessive = False

    # def handle_question(self):
    #     phrases = self.get_questions()

    def get_questions(self):
        self.establish_new_connection()

        try:
            self.cursor.execute(queries.get_questions())
        except Exception as e:
            print("Exception has occured 40: " + str(e))

        result = self.cursor.fetchall()

        self.list_result = [list(i) for i in result]
        print("Results: " + str(self.list_result))

    def save_new_phrase(self, phrase):
        self.establish_new_connection()
        try:
            self.cursor.execute(queries.save_new_phrase(
                phrase, self.person.id))
            phrase_id = self.cursor.lastrowid
        except Exception as e:
            print("Exception has occured 54: " + str(e))
        self.cnx.commit()

        try:
            self.cursor.execute(queries.save_person_phrase_matrix(
                phrase_id, self.person.id))
        except Exception as e:
            print("Exception has occured 61: " + str(e))
        self.cnx.commit()

        self.cursor.close()
        self.cnx.close()

    def remove_bad_chars(self, phrase):
        bad_chars = [';', ':', '!', "*", "?"]
        for i in bad_chars:
            phrase = phrase.replace(i, '')

        return phrase

    def teach_phrase(self):
        self.phrase = input(
            f"What new phrase would you like to teach me?")

        if self.check_if_known():
            print(f"I already know the phrase {self.phrase}")
        else:
            self.learn_phrase()

    def learn_phrase(self):
        self.definition = input(
            f"What does the phrase {self.phrase} mean? ")
        print("Thanks! I'll remember that.")
        self.save_new_phrase()

    def learn_phrase(self, phrase):
        print(f"I'm now learning the phrase: {phrase}")

    def check_if_known(self):
        if self.check_for_phrase():
            self.phrase_known()
        else:
            self.phrase_not_known()

    def check_for_phrase(self):
        try:
            self.cursor.execute(queries.check_for_phrase(self.phrase))
        except Exception as e:
            print("Exception has occured 102: " + str(e))
        result = self.cursor.fetchall()
        self.check_exists_result(result)

    def check_exists_result(self, result):
        result_list = [list(i) for i in result]
        number_returned = result_list[0][0]
        if int(number_returned) > 0:
            return True
            self.update_phrase()
        else:
            return False

    def update_phrase(self):
        try:
            self.cursor.execute(queries.update_phrase(
                phrase, self.person.person_id))
        except Exception as e:
            print("Exception has occured: 120 " + str(e))
        self.cnx.commit()
        self.cursor.close()
        self.cnx.close()

    def establish_new_connection(self):
        connection = connection_handler.establish_connection()
        self.cnx = connection[0]
        self.cursor = connection[1]

    @staticmethod
    def is_confirmation(word_or_phrase):
        Phrase_Manager.establish_new_connection()

        try:
            cursor.execute(queries.check_for_confirmation(word_or_phrase))
        except Exception as e:
            print("Exception has occured 144: " + str(e))
        result = cursor.fetchall()

        if Phrase_Manager.confirmation_exists(result):
            return True
        else:
            return False

    @staticmethod
    def confirmation_exists(result):
        result_list = [list(i) for i in result]
        number_returned = result_list[0][0]
        if int(number_returned) > 0:
            return True
        else:
            return False


from utils import parseDate, checkDateInTheFuture, checkDateFromNotTooBig, s3Query
from http_response import okResponse, badRequestResponse
from typing import Union
import os

import boto3

BucketName = os.environ.get('BUCKET_NAME')
FileName = os.environ.get('PROV_FILE_NAME')

s3 = boto3.client('s3')
maxMonths = 5

def lambda_handler(event, context):
    try:

        if not(event['queryStringParameters']) is None and 'prov' in event['queryStringParameters']:
            prov = event['queryStringParameters']['prov']
            dateFrom = parseDate(event['queryStringParameters'], 'date-from')

            if not checkDateFromNotTooBig(maxMonths, dateFrom):
                return badRequestResponse(f'date-from should be max {maxMonths} months in the past')

            if checkDateInTheFuture(dateFrom):
                return badRequestResponse(f'date-from should not be in the future')

            message = queryData(prov, dateFrom)

            return okResponse(message)
        else:
            return badRequestResponse('Province is missing')

    except Exception as e:
        print(e)
        return {
            "statusCode": 500,
        }

def queryData(prov: str, dateFrom: str) -> str:

    query = f"""
    SELECT
        denominazione_regione AS region,
        denominazione_provincia AS province,
        sigla_provincia AS province_initials,
        totale_casi AS total,
        data AS reporting_date
    FROM s3object s
    WHERE sigla_provincia ='{prov}' AND data > '{dateFrom}'
    """

    return s3Query(s3, query, BucketName, FileName)


#Importing pyplot submodule
import matplotlib.pyplot as plt
import numpy as np

x = np.array(['Apples','Bananas','Lichi','Pineapple'])
y = np.array([100,45,60,90])

#using bar() to represent data in bar graph
plt.subplot(1,2,2)
plt.bar(x,y, width = 0.5)
plt.title('Vertical')
plt.show()

#for showing the graph horizontally, barh() is used and instead of width, height is used
plt.subplot(1,2,1)
plt.barh(x,y, height = 0.5)
plt.title('Horizontal')

plt.suptitle('Prices of fruits per kg')
plt.show()


import pandas as pd
from django.contrib.auth.decorators import login_required
from django.contrib.sites.shortcuts import get_current_site
from django.http import HttpResponse
from django.urls import reverse
from researcher_UI.models import Administration

@login_required
def download_links(request, study_obj, administrations=None):
    """Download only the associated administration links instead of the whole data spreadsheet"""

    response = HttpResponse(content_type="text/csv")  # Format response as a CSV
    response["Content-Disposition"] = (
        "attachment; filename=" + study_obj.name + "_links.csv" ""
    )  # Name CSV

    if administrations is None:
        administrations = Administration.objects.filter(study=study_obj)

    admin_data = pd.DataFrame.from_records(administrations.values()).rename(
        columns={
            "id": "administration_id",
            "study_id": "study_name",
            "url_hash": "link",
        }
    )  # Grab variables from administration objects
    admin_data = admin_data[
        ["study_name", "subject_id", "repeat_num", "administration_id", "link"]
    ]  # Organize columns

    admin_data[
        "study_name"
    ] = study_obj.name  # Replace study ID number with actual study name

    # Recreate administration links and add them to dataframe
    test_url = "".join(
        [
            "http://",
            get_current_site(request).domain,
            reverse("administer_cdi_form", args=["a" * 64]),
        ]
    ).replace("a" * 64 + "/", "")
    admin_data["link"] = test_url + admin_data["link"]

    if study_obj.instrument.language in ["English"] and study_obj.instrument.form in [
        "WS",
        "WG",
    ]:
        admin_data = admin_data.append(
            {"study_name": "3rd Edition (Marchman et al., 2023)"}, ignore_index=True
        )

    admin_data.to_csv(
        response, encoding="utf-8", index=False
    )  # Convert dataframe into a CSV

    # Return CSV
    return response


import numpy as np
import pandas as pd
#from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import xlwt
import KMeans
import Visualization

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

points = []
center_points = []
K = 4
file="D:\\experiment\\第三次豆瓣\\测试3\\train\\douban_train_zuobiao.csv"
data=pd.read_csv(file)
train_data = np.array(data)#np.ndarray()每个姓名转换为一个list[]
#print(train_data)
all_list=train_data.tolist()#转换list
#print(all_list)

for item in all_list:
    print(item[2])
    print(item[3])
    print("-----------------------")
    point = [item[2], item[3]]
    points.append(point)

#print(type(points))#每个点存入列表
points = np.array(points)#转化为数组形式
#print(points)

center_points=[[18.26227416, -42.2997346], [16.23449381, -36.77185165], [58.35130569, 34.61516792], [-4.43906712, -56.93233191]]

kmeans = KMeans.KMeans(points, center_points, K)#K-means聚类
center_points, kmeans_cluster = kmeans.find_cluster_by_kmeans()#找到K-means聚类的簇
for i in kmeans_cluster:
    print(i)
data1 = np.array(center_points)  # np.ndarray()每个姓名转换为一个list[]
data2 = np.array(kmeans_cluster)  # np.ndarray()每个姓名转换为一个list[]
# print(train_data)
visual = Visualization.Visualization(center_points, kmeans_cluster)
visual.visual()


from datetime import datetime, timedelta
from pytz import timezone
from dateutil.relativedelta import relativedelta

data_1 = datetime(2023, 10, 30, 17, 10, 59)
print(data_1)

data_str = "2023-10-30 17:18:59"
data_str_formatter = "%Y-%m-%d %H:%M:%S"

data_2 = datetime.strptime(data_str, data_str_formatter)
print(data_2)

data_3 = datetime.now()
print(data_3)

data_4 = datetime.now(timezone("Asia/Tokyo"))
print(data_4)

data_5 = datetime(2023, 10, 30, 14, 20, 36, tzinfo=timezone("Asia/Tokyo"))
print(data_5)

fmt_1 = "%d/%m/%Y %H:%M:%S"
data_6 = datetime.strptime("31/10/2023 14:39:30", fmt_1)
data_7 = datetime.strptime("10/10/2023 14:39:30", fmt_1)
delta_1 = data_6 - data_7
delta_2 = timedelta(days=20)
print(data_7 - delta_2)
print(delta_1)
fmt_2 = "%d/%m/%Y %H:%M:%S"

data_8 = datetime.strptime("10/10/2001 15:35:26", fmt_2)
relative_delta_1 = data_8 - relativedelta(days=365 * 20)

print(relative_delta_1)

data_9 = datetime.now()
fmt_3 = "%d/%m/%Y"
print(data_9.strftime(fmt_3))


import glob

import cv2
import numpy as np
from tqdm import tqdm

class Calibration(object):
    def __init__(self, targetfilepath):
        # termination criteria
        self.criteria = (cv2.TERM_CRITERIA_EPS +
                         cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-5)

        # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
        self.worldPoints = np.zeros((9 * 6, 3), np.float32)  # 모두 [0] 체스판에서 찾을 점의 3D좌표 셋 바탕
        self.worldPoints[:, :2] = np.mgrid[0:9, 0:6].T.reshape(-1, 2)
        # 월드 좌표계의 베이스를 잡아줌, 왼쪽 맨 위를 원점으로 함(0,0,0)
        # 체스판이므로 Z-좌표값은 0으로 함

        # Arrays to store object points and image points from all the images.
        self.objectPoints = []  # 3d point in real world space, 켈리브레이션 넣을때 셋 지정하려고
        self.imagePoints = []  # 2d points in image plane.

        self.cameraMatrix = None
        self.distortion = None
        self.img_shape = None
        self.rvecs = None
        self.tvecs = None

# 덤프를 제거한 원하는 본질만 담은 것 & 회전벡터는 3x3으로 처리해놓기도 함
        self.targetRvecs = []
        self.targetTvecs = []

        self.readfile(targetfilepath)

    def readfile(self, targetfilepath):

        targetimagefile = glob.glob(targetfilepath + '\\*.jpg')
        #targetimagefile.sort()

        print("start loading files")
        for i in tqdm(range(len(targetimagefile))):

            # print(targetimagefile[i])

            imgmat = cv2.imread(targetimagefile[i])

            # 그레이 스케일로 변경
            imggray = cv2.cvtColor(imgmat, cv2.COLOR_BGR2GRAY)

            # Find the chess board corners
            # 성공 여부, 코너 포인트 리스트
            # 이미지, 모서리 수, 플래그
            ret, corners = cv2.findChessboardCorners(imggray, (9, 6), None)

            if ret is True:
                # If found, add object points, image points (after refining them)
                cv2.cornerSubPix(imggray, corners, (11, 11), (-1, -1), self.criteria)  # 찾은 코너에 대한 보정
                self.imagePoints.append(corners)
                self.objectPoints.append(self.worldPoints)

                # ret = cv2.drawChessboardCorners(imgmat, (9, 6), corners, ret)
                # cv2.imshow("test", imgmat)
                # cv2.waitKey(1)

        self.img_shape = cv2.cvtColor(cv2.imread(targetimagefile[0]), cv2.COLOR_BGR2GRAY).shape[::-1]
        self.calibrate(len(targetimagefile))

    def calibrate(self, target_lenght):
        print("enter 1d calibration")
        ret, self.cameraMatrix, self.distortion, self.rvecs, self.tvecs = cv2.calibrateCamera(
            objectPoints=self.objectPoints,
            imagePoints=self.imagePoints,
            imageSize=self.img_shape,
            cameraMatrix=self.cameraMatrix,
            distCoeffs=self.distortion,
            rvecs=self.rvecs,
            tvecs=self.tvecs)
        print(ret)
        # 한 카메라에 대한 켈리브레이션:
        # 성공여부, camera matrix, distortion coefficients, rotation and translation vector"s"
        # R|t는 한 뷰에 대한 월드 원점 - 2D 뷰의 영상중점 (cx,cy)간 좌표변환들
        print("exit 1d calibration")

        # for i in tqdm(range(target_lenght)):
        #     dst, _ = cv2.Rodrigues(self.rvecs[i])
        #     self.targetRvecs.append(dst)
        #     self.targetTvecs.append(self.tvecs[i])
        # print("Rodrigues eqs is solved")
        # for i, (r, t) in enumerate(zip(self.rvecs, self.tvecs)):
        #        dst, _ = cv2.Rodrigues(r)  # 회전벡터의 경우 표현방식이 달라서 변환 필요. 로드리게스 변환 참조.
        #        print(i, "번째 회전이동 : \n", dst)
        #        print(i, "번째 평행이동 : \n", t)


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

import vtk

def main():
    cps = vtk.vtkConvexPointSet()
    points = vtk.vtkPoints()
    points.InsertNextPoint(0, 0, 0)
    points.InsertNextPoint(1, 0, 0)
    points.InsertNextPoint(1, 1, 0)
    points.InsertNextPoint(0, 1, 0)
    points.InsertNextPoint(0, 0, 1)
    points.InsertNextPoint(1, 0, 1)
    points.InsertNextPoint(1, 1, 1)
    points.InsertNextPoint(0, 1, 1)
    points.InsertNextPoint(0.5, 0, 0)
    points.InsertNextPoint(1, 0.5, 0)
    points.InsertNextPoint(0.5, 1, 0)
    points.InsertNextPoint(0, 0.5, 0)
    points.InsertNextPoint(0.5, 0.5, 0)

    for i in range(0, 13):
        cps.GetPointIds().InsertId(i, i)

    ug = vtk.vtkUnstructuredGrid()
    ug.Allocate(1, 1)
    ug.InsertNextCell(cps.GetCellType(), cps.GetPointIds())
    ug.SetPoints(points)

    colors = vtk.vtkNamedColors()

    mapper = vtk.vtkDataSetMapper()
    mapper.SetInputData(ug)

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetColor(colors.GetColor3d("Tomato"))
    actor.GetProperty().SetLineWidth(3)
    actor.GetProperty().EdgeVisibilityOn()

    # Glyph the points
    sphere = vtk.vtkSphereSource()
    sphere.SetPhiResolution(21)
    sphere.SetThetaResolution(21)
    sphere.SetRadius(.03)

    # Create a polydata to store everything in
    polyData = vtk.vtkPolyData()
    polyData.SetPoints(points)

    pointMapper = vtk.vtkGlyph3DMapper()
    pointMapper.SetInputData(polyData)
    pointMapper.SetSourceConnection(sphere.GetOutputPort())

    pointActor = vtk.vtkActor()
    pointActor.SetMapper(pointMapper)
    pointActor.GetProperty().SetColor(colors.GetColor3d("Peacock"))

    # Create a renderer, render window, and interactor
    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetWindowName("Convex Point Set")
    renderWindow.AddRenderer(renderer)
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)

    # Add the actors to the scene
    renderer.AddActor(actor)
    renderer.AddActor(pointActor)
    renderer.SetBackground(colors.GetColor3d("Silver"))

    renderer.ResetCamera()
    renderer.GetActiveCamera().Azimuth(210)
    renderer.GetActiveCamera().Elevation(30)
    renderer.ResetCameraClippingRange()

    # Render and interact
    renderWindow.SetSize(640, 480)
    renderWindow.Render()
    renderWindowInteractor.Start()

if __name__ == '__main__':
    main()


__author__ = 'diegopinheiro'

from common.attribute import Attribute
import math
import numpy

class AttributeConverter:

    @staticmethod
    def get_representation(attribute=Attribute(), category=None):
        number_representation = AttributeConverter.get_number_representation(attribute=attribute)
        category_index = attribute.categories.index(category)
        category_position = int(math.pow(2, category_index))
        return [int(bit) for bit in list(numpy.binary_repr(category_position, width=number_representation))]

    @staticmethod
    def get_number_representation(attribute=Attribute()):
        return len(attribute.categories)

    @staticmethod
    def get_attribute_category(attribute=Attribute,
                               representation=None):
        representation = "".join([str(i) for i in representation])
        category_position = int(representation, 2)
        category_index = 0
        if category_position == 1:
            category_position = 0
        if category_position != 0:
            category_index = int(math.log(category_position, 2))
        return attribute.categories[category_index]


from kafka.admin import KafkaAdminClient, ConfigResource, ConfigResourceType

TOPIC_NAME = "kafka.client.tutorial"
BOOTSTRAP_SERVER_HOST = "kafka_tutorial:9092" # 카프카 클러스터 서버의 host와 port를 지정

admin_client = KafkaAdminClient(
    bootstrap_servers=BOOTSTRAP_SERVER_HOST
)

print("== Get broker information")
# return type dict
describe_cluster = admin_client.describe_cluster()
for node in describe_cluster.get('brokers'):
    print(f"node : {node}")
    cr = ConfigResource(ConfigResourceType.BROKER, name=node.get('node_id'), configs=node)
    describe_config = admin_client.describe_configs([cr])
    for config_i in describe_config:
        print(f"\tconfig:\t:{config_i}")

print("== End broker information")

print("== Get topic information")
describe_topic = admin_client.describe_topics([TOPIC_NAME])
for info_i in describe_topic:
    for k, v in info_i.items():
        print(f'{k}\t{v}')
    print('==================================================================')

print("== End topic information")
admin_client.close()


import json

import pandas as pd
import numpy as np

import filenames

myjson = {
    "arm": {
        "malware": [],
        "benign": []
    },
    "mips": {
        "malware": [],
        "bening": []
    }
}

df_arm_malware_forpoison = pd.read_csv(filenames.forpoison_arm_malware, header=None, index_col=False)
myjson["arm"]["malware"] = \
    df_arm_malware_forpoison[df_arm_malware_forpoison.columns[-2]].tolist()
df_arm_benign_forpoison = pd.read_csv(filenames.forpoison_arm_benign, header=None, index_col=False)
myjson["arm"]["benign"] = \
    df_arm_benign_forpoison[df_arm_benign_forpoison.columns[-2]].tolist()

df_mips_malware_forpoison = pd.read_csv(filenames.forpoison_mips_malware, header=None, index_col=False)
myjson["mips"]["malware"] = \
    df_mips_malware_forpoison[df_mips_malware_forpoison.columns[-2]].tolist()
df_mips_benign_forpoison = pd.read_csv(filenames.forpoison_mips_benign, header=None, index_col=False)
myjson["mips"]["benign"] = \
    df_mips_benign_forpoison[df_mips_benign_forpoison.columns[-2]].tolist()

with open(filenames.poisonJSON, "w") as f:
    json.dump(myjson, f)


import numpy as np
import matplotlib.pyplot as plt

with open("../buildxcode/cte.txt") as f:
    data = f.read()

    data = data.split('\n')
    x = [float(i) for i in data]

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.set_title("CTE")
    ax1.set_ylabel('CTE')

    ax1.plot(x, 'b')
    plt.show()


from django import forms
from django.core.exceptions import ValidationError

from manager.models import Accountancy
from manager.wallet_operations import wallet_choice, wallet_data_parse, change_wallet_balance

class AccountancyForm(forms.ModelForm):

    class Meta:
        model = Accountancy
        fields = ()

    def clean(self):
        amount = float(self.data["amount"])
        if amount < 0:
            raise ValidationError("Amount can't be negative.")

        wallet_type, previous_amount = wallet_data_parse(self.data)
        _, self.wallet_obj = wallet_choice(
            wallet_type,
            self.instance.card_id or self.instance.cash_id or self.instance.cryptocurrency_id
        )
        amount = amount - float(previous_amount) if previous_amount else 0

        self.wallet_obj = change_wallet_balance(
            self.instance.IO, self.wallet_obj, amount
        )

        return super().clean()

    def save(self, commit=True):
        accountancy = super(AccountancyForm, self).save(commit=False)
        self.clean()
        if commit:
            accountancy.amount = float(self.data["amount"])
            accountancy.save()
            self.wallet_obj.save()
        return super().save(commit)

class AccountancySearchForm(forms.Form):
    IO_type = forms.CharField(
        max_length=50,
        required=False,
        label="",
        widget=forms.TextInput(attrs={
            "placeholder": "Search by type ...",
            "class": "small_plate _comforta_bold text_shadow"
        })
    )


from django.db import IntegrityError
from django.utils.timezone import make_aware

from datetime import datetime

import logging

from .utils import get_extras

class DatabaseHandler(logging.Handler):
    """
    A log handler to store logs into the database.

    Currently, only log entries that belong to an event are stored in the database.
    All other log entries are available in the log files / via syslog.
    """

    def __init__(self, *args, **kwargs):
        self._logentry_model = None

        super(DatabaseHandler, self).__init__(*args, **kwargs)

    def emit(self, record):
        # the handler is initialized by django before the database setup, so the import would fail
        # therefore, we do it here dynamically when necessary - but only once
        if not self._logentry_model:
            from .models import LogEntry

            self._logentry_model = LogEntry

        # get the event, helper and user if they are stored in the entry
        event = record.event if hasattr(record, "event") else None
        if not event:
            return

        helper = record.helper if hasattr(record, "helper") else None
        user = record.user if hasattr(record, "user") else None

        # create the entry
        entry = self._logentry_model(
            timestamp=make_aware(datetime.fromtimestamp(record.created)),
            level=record.levelname,
            message=record.getMessage(),
            event=event,
            helper=helper,
            user=user,
            extras=get_extras(record),
            module=record.name,
        )

        try:
            entry.save()
        except ValueError:
            # if the event is deleted, we cannot save. we only store logs for existing events,
            # so we can discard this event (deletions are still logged via syslog / in files if container is used)
            pass
        except IntegrityError:
            # if a helper is deleted, the helper object is still there while we prepare the entry.
            # on save, the helper may already be deleted, so we have a foreign key error.
            entry.helper = None
            entry.save()


from pydantic import BaseModel, EmailStr
from datetime import datetime
from typing import Optional

class User(BaseModel):
    user_id : int
    username : str
    forenames: str
    surname : str
    email : EmailStr
    bio : Optional[str]
    display_name : Optional[str]
    created_at:  datetime

class UserRegistrationRequest(BaseModel):
    username : str
    forenames: str
    surname: str
    email: EmailStr
    password: str

class UserUpdateRequest(BaseModel):
    forenames: str
    surname : str
    email : EmailStr
    bio : str
    display_name : str

class UserPartialUpdateRequest(BaseModel):
    forenames: Optional[str]
    surname : Optional[str]
    email : Optional[EmailStr]
    bio : Optional[str]
    display_name : Optional[str]

    class Config:
        extra = "forbid"


#!/usr/bin/python
from Constants import Constants as cnt
from CoinDaemon import CoinDaemon
from bitcoinrpc.authproxy import AuthServiceProxy

class Wallet:
    """Provides a high-level abstraction of a coin wallet and simplifies
    the process of making JSON API RPC calls to the coin wallet
    daemon"""

    walletPath = ""
    walletName = ""
    walletNumber = 0
    walletNumberStr = '%0*d' % (6, walletNumber)
    walletPort = 00000
    walletBalance = 0.0
    walletProxy = None

    def __init__(self, a_str, b_int, c_int, d_str):
        self.walletPath = a_str
        self.walletNumber = b_int
        self.walletPort = c_int
        self.walletName = d_str

    def get_balance(self):
        return self.daemonAPI.getbalance()

    def test_wallet(self):
        """Function that tests the wallet and returns an int depending
        on the result"""

        import os, shutil, time, socket
        from bitcoinrpc.authproxy import JSONRPCException

        copyPath = cnt.HOME_DIR + "." + self.walletName + "/wallet.dat"

        shutil.copy(self.walletPath, copyPath)

        theDaemon = CoinDaemon(self.walletName, self.walletPort, self.walletPath)
        COMMAND = cnt.SCRIPT_DIR + "bin/" + self.walletName + " getbalance"
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        daemonTimer = 0
        RPCTimer = 0

        while(theDaemon.is_running()):
            print("[LOG] Waited " + str(daemonTimer) + " seconds for " + self.walletName + " daemon to stop...")
            daemonTimer = daemonTimer + 1
            time.sleep(1)
        else:
            theDaemon.start_daemon()

        while not(sock.connect_ex(('localhost', int(self.walletPort))) == 0):
            print("[LOG] Waited " + str(RPCTimer) + " seconds for RPC API...")
            RPCTimer = RPCTimer + 10
            time.sleep(10)
        else:
            self.walletProxy = AuthServiceProxy("http://" + cnt.USER + ":" + cnt.PASSWORD + "@127.0.0.1:" + self.walletPort)
            print("[LOG] RPC API Up!")

            self.walletBalance = self.walletProxy.getbalance()

            if(self.walletBalance == 0):
                #~ print("[LOG] Wallet tested - " + self.walletPath + " - EMPTY")
                theDaemon.stop_daemon()
                return 0
            else:
                try:
                    print(self.walletProxy.keypoolrefill())
                except JSONRPCException:
                    #~ print("[LOG] LOCKED Wallet tested - " + self.walletPath + " - balance: " + str(self.walletBalance))
                    theDaemon.stop_daemon()
                    return 2
                else:
                    #~ print("[LOG] Wallet tested - " + self.walletPath + " - balance: " + str(self.walletBalance))
                    theDaemon.stop_daemon()
                    return 1


import requests

class Polyline:
    def __init__(self) -> None:
        self.users = dict()
        self.polylines = []
        self.matches = []

    def add(self, ID: int, name: str, ph_no: int, source: list, destination: list) -> None:
        polyline = Polyline.__get_polyline(source, destination)
        self.polylines.append([ID, polyline])
        self.users[ID] = [name, ph_no]
        self.__match_polylines()

    def check_status(self, ID) -> int:
        try:
            idx = next(i for i, j in enumerate(self.matches) if j[0] == ID)
            match, share = self.matches[idx][1], self.matches[idx][2]
            match = self.users[match] + [share]
            del self.matches[idx]
        except:
            match = [-1, -1]
        return match

    def remove(self, ID: int):
        try:
            idx = next(i for i, j in enumerate(self.polylines) if j[0] == ID)
            del self.polylines[idx]
        except:
            print('No such ID')

    @staticmethod
    def LCSubStr(string1: str, string2: str) -> str:

        m = len(string1)
        n = len(string2)

        result = 0
        end = 0

        length = [[0 for j in range(m+1)] for i in range(2)]

        currRow = 0
        for i in range(0, m + 1):
            for j in range(0, n + 1):
                if (i == 0 or j == 0):
                    length[currRow][j] = 0

                elif (string1[i - 1] == string2[j - 1]):
                    length[currRow][j] = length[1 - currRow][j - 1] + 1

                    if (length[currRow][j] > result):
                        result = length[currRow][j]
                        end = i - 1
                else:
                    length[currRow][j] = 0

            currRow = 1 - currRow

        if (result == 0):
            return "-1"

        return string1[end - result + 1: end + 1]

    def _is_matching(self, polyline1: str, polyline2: str) -> bool:

        if len(polyline1) < len(polyline2):
            polyline1, polyline2 = polyline2, polyline1

        lcs = self.LCSubStr(polyline1, polyline2)
        com_head_len = 2
        n_lcs = len(lcs)
        n_line1 = len(polyline1) - com_head_len
        n_line2 = len(polyline2) - com_head_len

        # Assuming common header is 1st 2 chars
        if lcs == polyline1[:com_head_len] or lcs == "-1":
            return False
        # Fully contained
        elif (lcs == polyline1[com_head_len: ] or lcs == polyline2[com_head_len: ]):
            return True
        # LCS more than 50% of both:
        elif (n_lcs > n_line1/2 and n_lcs > n_line2/2):
            return True
        else:
            return False

    def __match_polylines(self):
        if len(self.polylines) < 2:
            return
        id1, polyline1 = self.polylines[-1]
        for idx, (id2, polyline2) in enumerate(self.polylines[:-1]):
            if self._is_matching(polyline1, polyline2):
                # share of polyline1
                share = self._get_fare_share(polyline1, polyline2)
                del self.polylines[-1]
                del self.polylines[idx]
                self.matches.append([id1, id2, share])
                self.matches.append([id2, id1, 100-share])

    def _get_fare_share(self, polyline1: str, polyline2: str):
        is_poly_swapped = False
        if len(polyline1) < len(polyline2):
            is_poly_swapped = True
            polyline1, polyline2 = polyline2, polyline1

        # lcs = self.LCSubStr(polyline1, polyline2)
        com_head_len = 2
        # n_lcs = len(lcs)
        n_line1 = len(polyline1) - com_head_len
        n_line2 = len(polyline2) - com_head_len

        fare_share = min(1, n_line1/(n_line1+n_line2)) * 100
        if is_poly_swapped:
            fare_share = 100 - fare_share
        return int(fare_share)

    @staticmethod
    def __get_polyline(source: list, destination: list) -> str:
        api = f'https://router.hereapi.com/v8/routes?transportMode=car&origin={source[0]},{source[0]}&destination={destination[0]},{destination[0]}&return=polyline,summary&apikey=DCt7LzSN9sR8IGVpnTjD3CtQWYu55oinzBdFfD9idAE'
        polyline = requests.get(api)
        return polyline.json()['routes'][0]['sections'][0]['polyline']

if __name__ == '__main__':
    polyline = Polyline()
    polyline.add(123, "person1", 9988776655, [52.5308,13.3847], [52.5264,13.3686])
    polyline.add(122, "person2", 9977553311, [52.5308,13.3847], [52.5264,13.3686])
    print(polyline.check_status(122))


# IntesisHome Inegration with  Domoticz
#
# Author: CV8R
#
"""
<plugin key="BasePlug" name="IntesisBox WMP-1 Protocol" author="CV8R" version="0.0.9" >
	<description>

IntesisBox WMP-1

IntesisBox WMP-1 interface for air conditioners into IP based control systems

Configuration

IP Address and Port number default 3310

	</description>
	<params>
		<param field="Address" label="IP Address" width="200px" required="true" default=""/>
		<param field="Port" label="Port" width="30px" required="true" default="3310"/>
		<param field="Mode1" label="Debug" width="75px">
			<options>
				<option label="True" value="Debug"/>
				<option label="False" value="Normal"  default="true" />
			</options>
		</param>
	</params>
</plugin>
"""
from typing import List

# Global var definitions
InitHeartbeatCount = 0
unitmode = "N/A"
oustandingPings = -1
lastHeartbeat = 0

# Limits as Global vars
minTempLimit = 180
maxTempLimit = 280

import Domoticz
import base64
import datetime
import re

class BasePlugin:
	enabled = True
	powerOn = 0
	runCounter = 0

	WMPConn = None
	oustandingPings = 0
	lastHeartbeat = datetime.datetime.now()

	def __init__(self):
		#self.var = 123
		return

	def onStart(self):
		Domoticz.Log("onStart called")

		Domoticz.Heartbeat(20) # Set heartbeat interval slower than default

		if Parameters["Mode1"] == "Debug":
			Domoticz.Debugging(1)

		if (len(Devices) == 0):
			Domoticz.Device(Name="Power", Unit=1, Image=16, TypeName="Switch", Used=1).Create()
			Domoticz.Device(Name="Ambient Temp", Unit=2, TypeName="Temperature", Used=1).Create()

			Options = {"LevelActions" : "|||||",
						"LevelNames" : "|Auto|Heat|Dry|Cool|Fan",
						"LevelOffHidden" : "true",
						"SelectorStyle" : "0"}

			Domoticz.Device(Name="Mode", Unit=3, TypeName="Selector Switch", Image=16, Options=Options, Used=1).Create()

			Options = {"LevelActions" : "||||",
						"LevelNames" : "|Auto|L1|L2|L3",
						"LevelOffHidden" : "true",
						"SelectorStyle" : "0"}

			Domoticz.Device(Name="Fan Speed", Unit=4, TypeName="Selector Switch", Image=7, Options=Options, Used=1).Create()

			Domoticz.Device(Name="Set Temp", Unit=5, Type=242, Subtype=1, Image=16, Used=1).Create()

			Domoticz.Device(Name="Error LED", Unit=6,  Image=13, TypeName="Switch", Used=1).Create()
			Domoticz.Device(Name="Error Text", Unit=7, TypeName="Text", Used=1).Create()

			Domoticz.Log("Device created.")

		DumpConfigToLog()

	def onStop(self):
		Domoticz.Log("onStop called")

	def onConnect(self, Connection, Status, Description):
		Domoticz.Log("onConnect called")
		global ConnectState
		Domoticz.Log("Connecting")
		if (Connection == self.WMPConn):
			if (Status == 0):
				Domoticz.Log("Connected successfully to: " + Connection.Address + ":" + Connection.Port)
				self.WMPConn.Send('ID\n') # Get ID at startup
		else:
			if (Description.find("Only one usage of each socket address") > 0):
				Domoticz.Log(Connection.Address + ":" + Connection.Port + " is busy, waiting.")
			else:
				Domoticz.Log("Failed to connect (" + str(Status) + ") to: " + Connection.Address + ":" + Connection.Port + " with error: " + Description)
			self.WMPConn = None

	def onMessage(self, Connection, Data):
		Domoticz.Debug("onMessage called")
		global unitmode
		global oustandingPings
		global lastHeartbeat
		global minTempLimit
		global maxTempLimit

		strData = Data.decode("utf-8", "ignore")
		Domoticz.Debug("onMessage called with Data: '" + str(strData) + "'")
		#msgDataListRaw = re.split(r':+|,', strData)  # type: List[str]
		msgDataListRaw = re.split(r':+|,+|\[+|\]', strData)  # split string to list of strings
		msgDataList = list(filter(None, msgDataListRaw)) # Remove consecutive delimiters note: filter does not return a list, use list to turn into list
		# Dump stripped messages in to Domoticz Log
		count = 0
		for msgData in msgDataList:
			Domoticz.Debug("Stripped Message[" + str(count) + "] = " + msgData ) # Log the messages incoming and their stripped count
			count = count + 1

		Domoticz.Debug("Resetting Ping to 0")
		oustandingPings = 0  # Reset ping counter onmessage for making sure connection is up in Heartbeat

		# Is it a status update

		if (msgDataList[0] == 'ACK'):
			Domoticz.Debug("Message Acknowledged with response: " + msgDataList[0])
		elif (msgDataList[0] == 'ERR'):
			Domoticz.Error("WMP Message ########## SENDING MESSAGE ERROR ########## with response: " + msgDataList[0])
			Devices[6].Update(nValue=1, sValue="100") # Set the Error LED switch to ON to flag for a send error
		elif (msgDataList[0] == 'LIMITS'): #Get the limits from the AC unit
			DataValues = '|'.join(msgDataList[2:])
			if (msgDataList[1] == 'ONOFF'): #Get the ONOFF limits from the AC unit
				Domoticz.Log("ONOFF Limits from unit: " + DataValues)
			elif (msgDataList[1] == 'MODE'): #Get the MODE limits from the AC unit
				Domoticz.Log("MODE Limits from unit: " + DataValues)
			elif (msgDataList[1] == 'FANSP'): #Get the FANSP limits from the AC unit
				Domoticz.Log("FANSP Limits from unit: " + DataValues)
			elif (msgDataList[1] == 'VANEUD'): #Get the VANEUD limits from the AC unit
				Domoticz.Log("VANEUD Limits from unit: " + DataValues)
			elif (msgDataList[1] == 'VANELR'): #Get the VANELR limits from the AC unit
				Domoticz.Log("VANELR Limits from unit: " + DataValues)
			elif (msgDataList[1] == 'SETPTEMP'): #Get the SETPTEMP temp limits from the AC unit
				Domoticz.Debug("SETPTEMP Temp limit values from unit: " + DataValues)
				minTempLimit = int(msgDataList[2])
				maxTempLimit = int(msgDataList[3])
				Domoticz.Status("Min Temp Limit: " + str(minTempLimit) + " Max Temp Limit: " + str(maxTempLimit))
		if (msgDataList[0] == 'CHN'):
			Domoticz.Debug("Status Update - Unit: " + msgDataList[1] + " Function: " + msgDataList[2] + " Value = " + msgDataList[3])
			# Update the status to Domoticz
			if (msgDataList[2] == 'ONOFF'):
				if (msgDataList[3] == 'ON'):
					Domoticz.Status("Update status to On")
					Devices[1].Update(nValue=1, sValue="100") # AC Power
				elif (msgDataList[3] == 'OFF'):
					Domoticz.Status("Update status to Off")
					Devices[1].Update(nValue=0, sValue="0")
			elif (msgDataList[2] == 'AMBTEMP'):
				ambtemp = str(float(msgDataList[3])/10)
				Domoticz.Log("Ambient temp")
				Domoticz.Debug("Current ambient temp: " + ambtemp + " Degrees")
				Devices[2].Update(nValue=0, sValue=ambtemp)
				#Domoticz.Debug("Resetting Ping to 0") # using AMBTEMP
				#oustandingPings = 0 # Reset ping counter for making sure connection is up in Heartbeat
			elif (msgDataList[2] == 'SETPTEMP'):
				settemp = str(int(msgDataList[3])/10)
				if (unitmode != 'FAN'):
					Domoticz.Status("Set temp is set to: " + settemp + " Degrees")
					Devices[5].Update(nValue=1, sValue=settemp) # Update the temp display in the set temp device
				else:
					Domoticz.Debug("FAN MODE setting temp to not display")
					Devices[5].Update(nValue=1, sValue="22")  # N/A to have a temp displayed
			elif (msgDataList[2] == 'MODE'):
				unitmode = msgDataList[3]
				if (unitmode == "AUTO"):
					Domoticz.Status("Mode to: " + unitmode)
					Devices[3].Update(nValue=1, sValue="10") # Auto
				elif (unitmode == "HEAT"):
					Domoticz.Status("Mode to: " + unitmode)
					Devices[3].Update(nValue=1, sValue="20") # Heat
				elif (unitmode == "DRY"):
					Domoticz.Status("Mode to: " + unitmode)
					Devices[3].Update(nValue=1, sValue="30") # Dry
				elif (unitmode == "COOL"):
					Domoticz.Status("Mode to: " + unitmode)
					Devices[3].Update(nValue=1, sValue="40") # Cool
				elif (unitmode == "FAN"):
					Domoticz.Status("Mode to: " + unitmode)
					Devices[3].Update(nValue=1, sValue="50") # Fan
				Devices[3].Refresh()
			elif (msgDataList[2] == 'FANSP'):
				fspeed = msgDataList[3]
				if (fspeed == "AUTO"):
					Domoticz.Status("Fan Speed to: " + fspeed)
					Devices[4].Update(nValue=1, sValue="10") # Fan Auto
				elif (fspeed == "1"):
					Domoticz.Status("Fan Speed to: " + fspeed)
					Devices[4].Update(nValue=1, sValue="20") # Fan Level 1
				elif (fspeed == "2"):
					Domoticz.Status("Fan Speed to: " + fspeed)
					Devices[4].Update(nValue=1, sValue="30") # Fan Level 2
				elif (fspeed == "3"):
					Domoticz.Status("Fan Speed to: " + fspeed)
					Devices[4].Update(nValue=1, sValue="40") # Fan Level 3
				Devices[4].Refresh()
			elif (msgDataList[2] == 'VANEUD'):
				vaneud = msgDataList[3]
				Domoticz.Status("Vane Up/Down: " + vaneud)
			elif (msgDataList[2] == 'VANELR'):
				vanelr = msgDataList[3]
				Domoticz.Status("Vane Left/Right: " + vanelr)
			elif (msgDataList[2] == 'ERRSTATUS'):
				errorstatus = msgDataList[3]
				if (errorstatus != "OK"):
					Domoticz.Status("Error Status: " + errorstatus)
					Devices[6].Update(nValue=1, sValue="100")  # Set the Error LED switch to ON to flag for an ERROR
				elif (errorstatus == "OK"):
					Domoticz.Status("Error Status: " + errorstatus)
					Devices[6].Update(nValue=0, sValue="0")  # Set the Error LED switch to OFF to clear ERROR
			elif (msgDataList[2] == 'ERRCODE'):
				errorcode = msgDataList[3]
				Domoticz.Status("Error Code: " + errorcode)
				Devices[7].Update(nValue=1, sValue=errorcode)  # Set error text
			else:
				Domoticz.Error("Unrecognised status command")

	def onCommand(self, Unit, Command, Level, Hue):
		Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))

		if (Unit == 1):
			if (Command == "On"):
				Domoticz.Status("Sending Power ON")
				self.powerOn = 1
				self.WMPConn.Send('SET,1:ONOFF,ON\n')
			elif(Command == "Off"):
				Domoticz.Status("Sending Power OFF")
				self.powerOn = 0
				self.WMPConn.Send('SET,1:ONOFF,OFF\n')
		elif (Unit == 3):
			if (Command == "Set Level"):
				Domoticz.Debug("Sending Mode")
				if (str(Level) == '10'):
					Domoticz.Status("Sending Mode Auto")
					self.WMPConn.Send('SET,1:MODE,auto\n')
				elif (str(Level) == '20'):
					Domoticz.Status("Sending Mode Heat")
					self.WMPConn.Send('SET,1:MODE,heat\n')
				elif (str(Level) == '30'):
					Domoticz.Status("Sending Mode Dry")
					self.WMPConn.Send('SET,1:MODE,dry\n')
				elif (str(Level) == '40'):
					Domoticz.Status("Sending Mode Cool")
					self.WMPConn.Send('SET,1:MODE,cool\n')
				elif (str(Level) == '50'):
					Domoticz.Status("Sending Mode Fan")
					self.WMPConn.Send('SET,1:MODE,fan\n')
			self.WMPConn.Send('LIMITS:SETPTEMP\n') # Check temp limits again when changing modes
		elif (Unit == 4):
			if (Command == "Set Level"):
				Domoticz.Debug("Sending Fan Speed")
				if (str(Level) == '10'):
					Domoticz.Status("Sending Fan Speed Auto")
					self.WMPConn.Send('SET,1:FANSP,AUTO\n')
				elif (str(Level) == '20'):
					Domoticz.Status("Sending Fan Speed Level 1")
					self.WMPConn.Send('SET,1:FANSP,1\n')
				elif (str(Level) == '30'):
					Domoticz.Status("Sending Fan Speed Level 2")
					self.WMPConn.Send('SET,1:FANSP,2\n')
				elif (str(Level) == '40'):
					Domoticz.Status("Sending Fan Speed Level 3")
					self.WMPConn.Send('SET,1:FANSP,3\n')
		elif (Unit == 5):
			if (Command == "Set Level"):
				settemp = Level
				Domoticz.Debug("String of Set Temp raw value = " + str(Level))
				settemp = round((int((float(settemp) * 10)))/5)*5 #includes complex rounding to nearest 5
				Domoticz.Debug("Set Temp converted value = " + str(settemp))
				if settemp < minTempLimit: #Adjusting for minLimit of unit
					Domoticz.Status("Set temp point less than min limit setting to min value = " + str(minTempLimit / 10) + " Degrees")
					settemp = minTempLimit #Send the minimum of unit
				if settemp > maxTempLimit: #Adjusting for minLimit of unit
					Domoticz.Status("Set temp point greater than max limit setting to max value = " + str(maxTempLimit / 10) + " Degrees")
					settemp = maxTempLimit
			Domoticz.Status("Setting Temp to: " + str(settemp / 10) + " Degrees")
			Domoticz.Debug("Sending Set Temp to: " + str(settemp))
			self.WMPConn.Send('SET,1:SETPTEMP,' + str(settemp) + '\n')
		elif (Unit == 6):
			if (Command == "Off"):
					Domoticz.Log("User cleared the ERROR Status LED")
					Devices[6].Update(nValue=0, sValue="0")  # Set the Error LED switch to Off
		else:
			Domoticz.Error("No command available to send")

	def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
		Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)

	def onDisconnect(self, Connection):
		Domoticz.Log("onDisconnect called")
		self.WMPConn = None

	def onHeartbeat(self):
		global InitHeartbeatCount  # Counter for first heartbeats
		global oustandingPings # Counter for the Pings for check alive using AMBTEMP
		global lastHeartbeat
		Domoticz.Debug("onHeartbeat called")
		Domoticz.Debug("onHeartbeat called, last response seen " + str(oustandingPings) + " heartbeats ago.")
		Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount))
		lastHeartbeat = datetime.datetime.now()
		if (self.WMPConn == None):
			Domoticz.Log("Connect to WMP")
			InitHeartbeatCount = 0  # reset heartbeat count
			oustandingPings = -1 # reset ping count
			self.handleConnect()
		else:
			if (self.WMPConn.Name == "WMP_Connection") and (self.WMPConn.Connected()):
				oustandingPings = oustandingPings + 1  # Increment Ping Counter, reset at AMPTEMP Status
				if InitHeartbeatCount <= 6:
					InitHeartbeatCount = InitHeartbeatCount + 1
					Domoticz.Debug("Heartbeat Init Count Incremented now = " + str(InitHeartbeatCount))
					if InitHeartbeatCount == 1: #Need to delay these inital messages or some are missed
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting ONOFF")
						self.WMPConn.Send('GET,1:ONOFF\n')
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting MODE")
						self.WMPConn.Send('GET,1:MODE\n')
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting SETPTEMP")
						self.WMPConn.Send('GET,1:SETPTEMP\n')
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting FANSP")
						self.WMPConn.Send('GET,1:FANSP\n')
					if InitHeartbeatCount == 3:
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting VANEUD")
						self.WMPConn.Send('GET,1:VANEUD\n')
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting VANELR")
						self.WMPConn.Send('GET,1:VANELR\n')
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting ERRSTATUS")
						self.WMPConn.Send('GET,1:ERRSTATUS\n')
					if InitHeartbeatCount == 4:
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting ERRCODE")
						self.WMPConn.Send('GET,1:ERRCODE\n')
					if InitHeartbeatCount == 5:
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting LIMITS ONOFF")
						self.WMPConn.Send('LIMITS:ONOFF\n')
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting LIMITS MODE")
						self.WMPConn.Send('LIMITS:MODE\n')
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting LIMITS FANSP")
						self.WMPConn.Send('LIMITS:FANSP\n')
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting LIMITS VANEUD")
						self.WMPConn.Send('LIMITS:VANEUD\n')
					if InitHeartbeatCount == 6:
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting LIMITS VANELR")
						self.WMPConn.Send('LIMITS:VANELR\n')
						Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount) + " Getting LIMITS SETPTEMP")
						self.WMPConn.Send('LIMITS:SETPTEMP\n')
						Domoticz.Heartbeat(20)  # Extending heartbeat at last Limit
				if InitHeartbeatCount == 7:  # when count gets to this number and is connected, it will not increment and commence AMBTEMP Heartbeats
					Domoticz.Debug("Getting Ambient Temp")
					self.WMPConn.Send('GET,1:AMBTEMP\n')  # Get AMBTEMP at Heartbeat to confirm connected
			if (oustandingPings == 3):
				Domoticz.Log(self.WMPConn.Name + " has not responded to 3 heartbeats terminating connection.")
				if (self.WMPConn.Connected()):
					self.WMPConn.Disconnect()
					Domoticz.Debug("Heartbeat Init Count = " + str(InitHeartbeatCount))
				self.WMPConn = None

	def handleConnect(self):
		self.WMPConn = None
		Domoticz.Debug("Settings shorter heartbeat to speed up initialisation")
		Domoticz.Heartbeat(5)  # Setting the inital hearbeat timeout used for delaying startup messages - extended in onHeartbeat after counter reached
		self.WMPConn = Domoticz.Connection(Name="WMP_Connection", Transport="TCP/IP", Protocol="Line", Address=Parameters["Address"], Port=Parameters["Port"])
		self.WMPConn.Connect()

global _plugin
_plugin = BasePlugin()

def onStart():
	global _plugin
	_plugin.onStart()

def onStop():
	global _plugin
	_plugin.onStop()

def onConnect(Connection, Status, Description):
	global _plugin
	_plugin.onConnect(Connection, Status, Description)

def onMessage(Connection, Data):
	global _plugin
	_plugin.onMessage(Connection, Data)

def onCommand(Unit, Command, Level, Hue):
	global _plugin
	_plugin.onCommand(Unit, Command, Level, Hue)

def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
	global _plugin
	_plugin.onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile)

def onDisconnect(Connection):
	global _plugin
	_plugin.onDisconnect(Connection)

def onHeartbeat():
	global _plugin
	_plugin.onHeartbeat()

	# Generic helper functions
def DumpConfigToLog():
	for x in Parameters:
		if Parameters[x] != "":
			Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
	Domoticz.Debug("Device count: " + str(len(Devices)))
	for x in Devices:
		Domoticz.Debug("Device:           " + str(x) + " - " + str(Devices[x]))
		Domoticz.Debug("Device ID:       '" + str(Devices[x].ID) + "'")
		Domoticz.Debug("Device Name:     '" + Devices[x].Name + "'")
		Domoticz.Debug("Device nValue:    " + str(Devices[x].nValue))
		Domoticz.Debug("Device sValue:   '" + Devices[x].sValue + "'")
		Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
	return


from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = input('Enter URL: ')
count2 = input('Enter count: ')
position = input('Enter position: ')
count2 = int(count2)
position = int(position)
html = urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
tags = soup('a')

count = 0
names_list = []

while (count < count2):
	names_list.append(soup.find_all('a')[17].get_text())
	url = soup.find_all('a')[position-1]["href"]
	print ("Retrieving: ", url)
	html = urlopen(url, context=ctx).read()
	soup = BeautifulSoup(html, "html.parser")
	tags = soup('a')
	count +=1

print (names_list)
length = int(len(names_list))
print ("The answer to the assignment for this execution is",(names_list[length-1]))


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

from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QDoubleValidator
from PyQt5.QtCore import pyqtSignal, pyqtSlot
from selfdefineformat.views.editcheckbox import Ui_Form

class EditCheckBoxModule(QWidget, Ui_Form):
    edited = pyqtSignal()

    def __init__(self, element, parent=None):
        super(EditCheckBoxModule, self).__init__(parent)
        self.setupUi(self)
        self.element = element
        self.set_detail()
        self.set_validator()

    def set_validator(self):
        doubleValitor = QDoubleValidator()
        doubleValitor.setBottom(0)
        doubleValitor.setDecimals(2)
        doubleValitor.setNotation(QDoubleValidator.StandardNotation)
        self.lineEdit_width.setValidator(doubleValitor)
        self.lineEdit_height.setValidator(doubleValitor)
        self.lineEdit_fontsize.setValidator(doubleValitor)

    def set_detail(self):
        if self.element.tagName() != 'CheckBox':
            self.close()
            return
        self.lineEdit_text.setText(self.element.attribute("name"))
        self.lineEdit_fontsize.setText(self.element.attribute("size"))
        self.lineEdit_width.setText(self.element.attribute("width"))
        self.lineEdit_height.setText(self.element.attribute("height"))
        self.lineEdit_id.setText(self.element.attribute("ID"))

        try:
            self.style = int(self.element.attribute("style"))
        except ValueError:
            self.style = 0
        format = '{:03b}'.format(self.style)
        self.checkBox_bold.setCheckState(2 if format[0] != '0' else 0)
        self.checkBox_italic.setCheckState(2 if format[1] != '0' else 0)
        self.checkBox_underline.setCheckState(2 if format[2] != '0' else 0)

    @pyqtSlot(str)
    def on_lineEdit_id_textEdited(self, p_str):
        self.element.setAttribute("ID", p_str)
        self.edited.emit()

    @pyqtSlot(str)
    def on_lineEdit_fontsize_textEdited(self, p_str):
        self.element.setAttribute("size", p_str)
        self.edited.emit()

    @pyqtSlot(str)
    def on_lineEdit_width_textEdited(self, p_str):
        self.element.setAttribute("width", p_str)
        self.edited.emit()

    @pyqtSlot(str)
    def on_lineEdit_height_textEdited(self, p_str):
        self.element.setAttribute("height", p_str)
        self.edited.emit()

    @pyqtSlot(str)
    def on_lineEdit_text_textEdited(self, p_str):
        self.element.setAttribute("name", p_str)
        self.edited.emit()

    @pyqtSlot(bool)
    def on_checkBox_bold_toggled(self, p_bool):
        if p_bool:
            self.element.setAttribute("style", self.style + 4)
            self.style += 4
        else:
            self.element.setAttribute("style", self.style - 4)
            self.style -= 4
        self.edited.emit()

    @pyqtSlot(bool)
    def on_checkBox_italic_toggled(self, p_bool):
        if p_bool:
            self.element.setAttribute("style", self.style + 2)
            self.style += 2
        else:
            self.element.setAttribute("style", self.style - 2)
            self.style -= 2
        self.edited.emit()

    @pyqtSlot(bool)
    def on_checkBox_underline_toggled(self, p_bool):
        if p_bool:
            self.element.setAttribute("style", self.style + 1)
            self.style += 1
        else:
            self.element.setAttribute("style", self.style - 1)
            self.style -= 1
        self.edited.emit()

    @pyqtSlot(bool)
    def on_checkBox_default_toggled(self, p_bool):
        if p_bool:
            self.element.setAttribute("default", 2)
        else:
            self.element.setAttribute("style", 0)
        self.edited.emit()


import requests
import json
import ntpath

class Jira:
    """Common JIRA API methods.

    JIRA's REST APIs provide access to resources (data entities) via URI paths. To use a REST API, your application will
    make an HTTP request and parse the response. The JIRA REST API uses JSON as its communication format, and the
    standard HTTP methods like GET, PUT, POST and DELETE (see API descriptions below for which methods are available for
    each resource). URIs for JIRA's REST API resource have the following structure:

    http://host:port/context/rest/api-name/api-version/resource-name

    Currently there are two API names available:

    auth - for authentication-related operations, and
    api - for everything else.

    The current API version is 2. However, there is also a symbolic version, called latest, which resolves to the latest
    version supported by the given JIRA instance. As an example, if you wanted to retrieve the JSON representation of
    issue JRA-9 from Atlassian's public issue tracker, you would access:

    https://jira.atlassian.com/rest/api/latest/issue/JRA-9
    """

    def __init__(self, base_http_url, project_key, auth):
        """Initialize JIRA object with base_http_url, project_key, and auth.

        :param base_http_url: URL for JIRA site - http://host:port/ (i.e. http://localhost:8080/)
        :type base_http_url: str
        :param project_key: The project matching the projectKey supplied in the resource path as shown in URL.
        :type project_key: str
        :param auth: Tuple of username and password for authentication.
        :type auth: tuple[str, str]
        """
        self.base_http_url = base_http_url
        self.project_key = project_key
        self.auth = auth

    def create_issue(self, summary, description='', issue_type='Task'):
        """Create Issue in Jira

        Create issue with summary, description, and issue type.

        :param summary: A brief summary of the issue. This will be the "title" shown next to the issue id on the boards.
        :type summary: str
        :param description: More details about the issue.
        :type description: str
        :param issue_type: Choose one of the predefined issue types for your project ('Bug', 'Task', 'Story', and
            'Epic' by default.)
        :type issue_type: str
        :return: Response from the POST request.
            STATUS 201: Success - application/json Returns a link to the created issue. \n
            STATUS 400: Error - STATUS 400Returned if the input is invalid (e.g. missing required fields, invalid
                        field values, and so forth).
        :rtype: list[requests.Response, str]
        """

        # create Jira issue
        url = self.base_http_url + 'rest/api/2/issue'
        headers = {'Content-Type': 'application/json'}
        data = {
            "fields": {
                "project": {
                    "key": self.project_key
                },
                "summary": summary,
                "description": description,
                "issuetype": {
                    "name": issue_type
                }
            }
        }

        r = requests.post(url, auth=self.auth, headers=headers, data=json.dumps(data))

        return r

    def get_issues(self, issue_id=None, max_results=10, start_at=0):
        """Get specific or list of Jira issue(s).

        Get specific issue by setting the issue_id. Get a list of issues by leaving the issue_id blank and setting the
        limit for the pagination size (default=10).

        :param issue_id: JIRA will attempt to identify the issue by the issueIdOrKey path parameter. This can be an
            issue id, or an issue key.
        :type issue_id: str
        :param max_results: The "maxResults" parameter indicates how many results to return per page.
        :type max_results: int
        :param start_at: Item that should be used as the first item in the page of results.
        :type start_at: int
        :return:
            STATUS 200: Success - application/jsonReturns a full representation of a JIRA issue in JSON format.
            STATUS 404: Error - Returned if the requested issue is not found, or the user does not have permission to
                        view it.
        :rtype: list[requests.Response, str]
        """

        if issue_id is None:
            url = self.base_http_url + 'rest/api/2/issue?maxResults=' + str(max_results) + '&startAt' + str(start_at)
        else:
            url = self.base_http_url + 'rest/api/2/issue/' + str(issue_id)
        headers = {'Content-Type': 'application/json'}

        r = requests.get(url, auth=self.auth, headers=headers)

        return r

    def add_attachment(self, issue_id, attachments):
        """Add attachments to Jira issue

        :param issue_id: JIRA will attempt to identify the issue by the issueIdOrKey path parameter. This can be an
            issue id, or an issue key.
        :type issue_id: str
        :param attachments: List of string paths to attachments to be uploaded and added to an issue.
        :type attachments: list[str]
        :return:
            STATUS 200: Success - application/json
            STATUS 403: Error - Returned if attachments is disabled or if you don't have permission to add attachments
                        to this issue.
            STATUS 404: Error - Returned if the requested issue is not found, the user does not have permission to
                        view it, or if the attachments exceeds the maximum configured attachment size.
        :rtype: list[requests.Response, str]
        """

        # add attachments to Jira issue
        url = self.base_http_url + 'rest/api/2/issue/' + issue_id + '/attachments'
        headers = {'X-Atlassian-Token': 'no-check'}

        r = []
        filenames = []

        # POST request for attachments
        if attachments:
            for file in attachments:
                upload = open(file, 'rb')
                filenames.append(ntpath.basename(file))
                r.append(requests.post(url, auth=self.auth, headers=headers, files={'file': upload}))
                upload.close()
        else:
            r.append('ERROR: No attachments to add.')

        # verify attachments were attached
        if attachments:
            jira_attachments = self.get_issues(issue_id).json()['fields']['attachment']

            for filename in filenames:
                if not any(d['filename'] == filename for d in jira_attachments):
                    # does not exist
                    r.append('ERROR: File ' + filename + ' was not attached.')

        return r


import elasticsearch
import json
import luigi

from elasticsearch.helpers import bulk
from luigi.contrib import esindex
from nfl import scraper

class IngestData(luigi.Task):

    category = luigi.Parameter()
    year = luigi.Parameter()

    def output(self):
        target = luigi.LocalTarget("output/{0}/{1}.json".format(self.year, self.category))
        target.category = self.category
        target.year = self.year
        return target

    def run(self):
        with self.output().open('w') as f:
            f.write(json.dumps(scraper.scrape_year_category(self.year, self.category), indent=2))

class ExportToES(luigi.Task):

    def __init__(self):
        self.host = "localhost"
        self.port = "9200"
        self.index = "demo5"
        super(ExportToES, self).__init__()

    def _init_connection(self):
        return elasticsearch.Elasticsearch(
            host=self.host,
            port=self.port
        )

    def requires(self):
        for year in range(2000, 2015):
            for c in ['KICK_RETURNS', 'KICKING', 'PASSING', 'PUNTING', 'RECEIVING', 'RUSHING', 'SACKS', 'SCORING', 'TACKLES', 'TOUCHDOWNS']:
                yield IngestData(c, year)

    def output(self):
        return esindex.ElasticsearchTarget(host=self.host, port=self.port, index=self.index, doc_type="report", update_id="_id")

    def docs(self):
        for inputFile in self.input():
            with inputFile.open('r') as f:
                for element in json.loads(f.read()):
                    element["_type"] = inputFile.category
                    element["_index"] = self.index
                    yield element

    def run(self):
        es = self._init_connection()
        bulk(es, self.docs())
        self.output().touch()


#reverses the process of the spritesheetCreator script
from PIL import Image
import sys

#sets all the variables we need to accurately crop the images
imageAmount = 0
i = 1
width = 0
height = 0
maxWidth = 0
maxHeight = 0
counter = 0
row = 0
column = 0
#searches for the file saved by the spritesheet script
try:
	with open("imageAtlas.txt", "r") as imageSearch:
		data = imageSearch.read()
except IOError:
    sys.exit()
imageSearch.close()
#changes the data of the atlas to something more readable
newData = data.replace('[', '').replace('(', '').replace(',', '').replace(')', '').replace(']', '')
#takes the new data and finds out how many images were in the spritesheet
imageAmount = (newData.count(" "))/2+1
#saves the data into a list
listNew = newData.split(' ')
imageList = zip(listNew[::2],listNew[1::2])
#uses the same math in the spritesheet script to figure out how many images per row there will be
while(((len(imageList)/i) != i)) :
	i = i + 1
	if(((len(imageList)/i) < i)) :
		break
maxFramesPerRow = i
#opens the spritesheet and finds the size of the largest image
spritesheet = Image.open("spritesheet.png")
maxWidth, maxHeight = max(imageList)
#loops through the amount of images sets the size of the current image using the atlas
#the counter then checks if the row has ended if it had it moves onto the next row
#each image will be its original size the space between them being what the largest one was
#this is figued out because the space is constant
#saves each image into the current script folder
for x in range(imageAmount):
	width, height = imageList[x]
	if(counter == maxFramesPerRow):
		row = row + 1
		column = 0
		counter = 0
		area = (0, int(maxHeight)*row, int(width), int(height)+(int(maxHeight)*row))
		cropped_img = spritesheet.crop(area)
		cropped_img.save("image" + str(x+1) + ".png", "PNG")
	else:
		if(x is not 0):
			column = column+1
		area = (int(maxWidth)*column, int(maxHeight)*row, int(width)+(int(maxWidth)*column), int(height)+(int(maxHeight)*row))
		cropped_img = spritesheet.crop(area)
		cropped_img.save("image" + str(x+1) + ".png", "PNG")
	counter = counter+1


"""
Normally step 1 to align trimmed reads
e.g.
"""
import os
import sys
from tqdm import trange
from joblib import Parallel, delayed
import re

input_root = sys.argv[1]
output_root = sys.argv[2]
genome_dir = sys.argv[3]

subdirs = []
for subdir, dirs, files in os.walk(input_root):
    for file in files:
        subdirs.append(subdir)

subdirs = sorted(set(subdirs))
if not os.path.isdir(output_root):
    os.system(f'mkdir -p {output_root}')

def process(pair_dir):
    name = re.search(".*/(.*$)", pair_dir).group(1)
    if os.path.exists(f"{output_root}/{name}_Aligned.out.bam"):
        return

    os.system(
        (f'STAR --runThreadN 4 --genomeDir {genome_dir} --readFilesIn {pair_dir}/*gz '
         f'--readFilesCommand zcat --outFileNamePrefix {output_root}/{name}_ --quantMode TranscriptomeSAM '
         '--outSAMtype BAM Unsorted'))

Parallel(n_jobs=2)(delayed(process)(subdirs[i]) for i in trange(len(subdirs)))


import pygame
from battle import time, MOVE_DOWN_FREQ, BOARD_HEIGHT, BOARD_WIDTH, BLANK, POISON, SHAPES, BOX_SIZE, \
    TEMPLATE_HEIGHT, TEMPLATE_WIDTH, MOVE_SIDE_WAYS_FREQ
from utils import get_new_piece, get_blank_board, calculate_level_and_fall_frequency, is_valid_position
from pygame.locals import *

BOARD_OFFSET = [int(((BOX_SIZE * BOARD_WIDTH) / 2) + 60) * -1, int(((BOX_SIZE * BOARD_WIDTH) / 2) - 47)]
LEFT_CONTROLS = (K_q, K_w, K_a, K_s, K_d, K_SPACE)
RIGHT_CONTROLS = (K_UP, K_DOWN, K_LEFT, K_RIGHT, K_INSERT, K_HOME)
CONTROLS = (LEFT_CONTROLS, RIGHT_CONTROLS)

class Player(object):
    board = None
    border_color = None
    board_offset = 0
    bangs = 3.0
    now = None
    last_move_down_time = now
    last_move_sideways_time = now
    last_fall_time = now
    moving_down = False  # Note: there is no moving_up variable
    moving_left = False
    moving_right = False
    score = 0
    level = 0
    turn = 0
    fall_frequency = 0
    falling_piece = None
    next_piece = None
    controls = tuple()
    game_over = False

    def __init__(self, now=None, player_num=0, single_player=True):
        if not now:
            self.now = time.time()
        else:
            self.now = now
        self.board = get_blank_board()
        self.last_move_down_time = self.last_fall_time = self.last_move_sideways_time = self.now
        self.update_level()
        self.turn = 1
        if single_player:
            self.controls = LEFT_CONTROLS + RIGHT_CONTROLS
        else:
            self.controls = CONTROLS[player_num]
            self.board_offset = BOARD_OFFSET[player_num]

    def update_level(self):
        self.level, self.fall_frequency = calculate_level_and_fall_frequency(self.score)

    def update_falling_piece(self, now):
        if self.game_over:
            return
        self.falling_piece = self.next_piece
        self.turn += 1
        self.next_piece = get_new_piece(self.turn)
        self.last_fall_time = now

    def remove_completed_line(self):
        """
        Remove any completed lines on the board, move everything above them
        down, and return the number of complete lines.
        """
        if self.game_over:
            return
        num_lines_removes = 0
        y = BOARD_HEIGHT - 1  # Start y at the bottom of the board
        while y >= 0:
            complete, bonus = self.is_completed_line_with_bonus(y)
            if complete:
                # Remove the line and pull boxes down by one line.
                for pull_down_y in range(y, 0, -1):
                    for x in range(BOARD_WIDTH):
                        self.board[x][pull_down_y] = self.board[x][pull_down_y - 1]
                # Set very top line to blank
                for x in range(BOARD_WIDTH):
                    self.board[x][0] = BLANK
                num_lines_removes += 1
                if bonus:
                    num_lines_removes += 4
                # Note on the next iteration of the loop, y is the same.
                # This is so that is the line that was pulled down is also
                # complete, it will be removed.
            else:
                y -= 1
        if num_lines_removes:
            self.score += num_lines_removes
            self.update_level()
            self.bangs += num_lines_removes * .25  # One new bang every four lines

    def is_completed_line_with_bonus(self, y):
        """
        Return True is the line filled with boxes with no gaps.
        """
        bonus = True
        block_color = None
        for x in range(BOARD_WIDTH):
            if self.board[x][y] in (BLANK, POISON):
                return False, False
            if block_color is None:
                block_color = self.board[x][y]
            if bonus:
                bonus = block_color == self.board[x][y]
        return True, bonus

    def handle_event(self, event_type, key):
        if key not in self.controls or self.game_over:
            return
        if event_type == KEYUP:
            if key in (K_LEFT, K_a):
                self.moving_left = False
            elif key in (K_RIGHT, K_d):
                self.moving_right = False
            elif key in (K_DOWN, K_s):
                self.moving_down = False

        elif event_type == KEYDOWN:
            # moving the block sideways
            if key in (K_LEFT, K_a) and is_valid_position(self.board, self.falling_piece, adj_x=-1):
                self.falling_piece['x'] -= 1
                self.moving_left = True
                self.moving_right = False
                self.last_move_sideways_time = self.now
            elif key in (K_RIGHT, K_d) and is_valid_position(self.board, self.falling_piece, adj_x=1):
                self.falling_piece['x'] += 1
                self.moving_left = False
                self.moving_right = True
                self.last_move_sideways_time = self.now
            # Rotating the block (if there is room to rotate)
            elif key in (K_UP, K_w):
                self.falling_piece['rotation'] = (self.falling_piece['rotation'] + 1) % len(SHAPES[self.falling_piece['shape']])
                if not is_valid_position(self.board, self.falling_piece):
                    self.falling_piece['rotation'] = (self.falling_piece['rotation'] - 1) % len(SHAPES[self.falling_piece['shape']])
            elif key == K_q:
                self.falling_piece['rotation'] = (self.falling_piece['rotation'] - 1) % len(SHAPES[self.falling_piece['shape']])
                if not is_valid_position(self.board, self.falling_piece):
                    self.falling_piece['rotation'] = (self.falling_piece['rotation'] + 1) % len(SHAPES[self.falling_piece['shape']])

            # Make the block fall faster with the down key
            elif key in (K_DOWN, K_s):
                self.moving_down = True
                if is_valid_position(self.board, self.falling_piece, adj_y=1):
                    self.falling_piece['y'] += 1
                self.last_move_down_time = self.now

            # Move the current block all the way down
            elif key == K_SPACE:
                self.moving_down = False
                self.moving_left = False
                self.moving_right = False
                for i in range(1, BOARD_HEIGHT):
                    if not is_valid_position(self.board, self.falling_piece, adj_y=i):
                        break
                self.falling_piece['y'] += i - 1

    def calculate_moves(self, now):
        if self.game_over:
            return
        # Handling moving the block because of user input
        if (self.moving_left or self.moving_right) and now - self.last_move_sideways_time > MOVE_SIDE_WAYS_FREQ:
            if self.moving_left and is_valid_position(self.board, self.falling_piece, adj_x=-1):
                self.falling_piece['x'] -= 1
            elif self.moving_right and is_valid_position(self.board, self.falling_piece, adj_x=1):
                self.falling_piece['x'] += 1
            self.last_move_sideways_time = now
        if self.moving_down and now - self.last_move_down_time > MOVE_DOWN_FREQ and is_valid_position(self.board, self.falling_piece, adj_y=1):
            self.falling_piece['y'] += 1
            self.last_move_down_time = now

        # Let the piece fall if it is time to fall
        if now - self.last_fall_time > self.fall_frequency:
            # See if the piece has landed.
            if not is_valid_position(self.board, self.falling_piece, adj_y=1):
                # falling piece has landed, set it on the self.board
                self.add_to_board(self.falling_piece)
                self.remove_completed_line()
                self.falling_piece = None
            else:
                # piece did not land just move it down one block
                self.falling_piece['y'] += 1
                self.last_fall_time = now

    def add_to_board(self, piece):
        """
        Fill in the board based on piece's location, shape, and rotation
        """
        for x in range(TEMPLATE_WIDTH):
            for y in range(TEMPLATE_HEIGHT):
                if SHAPES[piece['shape']][piece['rotation']][x][y] != BLANK:
                    self.board[x + piece['x']][y + piece['y']] = piece['color']


import mock
import testtools

from stackalytics.dashboard import helpers

class TestHelpers(testtools.TestCase):

    @mock.patch('time.time')
    def test_get_current_company(self, mock_time_time):
        current_timestamp = 1234567890
        mock_time_time.return_value = current_timestamp

        user = {
            'user_id': 'smith',
            'user_name': 'John Smith',
            'companies': [{
                'company_name': 'Current',
                'end_date': current_timestamp + 1
            }, {
                'company_name': 'TheCompany',
                'end_date': 0
            }]
        }

        self.assertEqual('Current', helpers.get_current_company(user))

    @mock.patch('stackalytics.dashboard.helpers.make_link')
    def test_extend_user(self, mock_make_link):
        company_link = mock.Mock()
        mock_make_link.return_value = company_link

        user = {
            'user_id': 'smith',
            'user_name': 'John Smith',
            'companies': [{
                'company_name': 'TheCompany',
                'end_date': 0
            }]
        }

        expected = {
            'user_id': 'smith',
            'user_name': 'John Smith',
            'companies': [{
                'company_name': 'TheCompany',
                'end_date': 0
            }],
            'id': 'smith',
            'company_link': company_link,
            'text': 'John Smith',
        }

        observed = helpers.extend_user(user)
        self.assertEqual(expected, observed)
        mock_make_link.assert_called_once_with('TheCompany', '/', mock.ANY)

    @mock.patch('time.time')
    @mock.patch('stackalytics.dashboard.helpers.make_link')
    def test_extend_user_current_company(self, mock_make_link, mock_time_time):
        company_link = mock.Mock()
        mock_make_link.return_value = company_link
        current_timestamp = 1234567890
        mock_time_time.return_value = current_timestamp

        user = {
            'user_id': 'smith',
            'user_name': 'John Smith',
            'companies': [{
                'company_name': 'Current',
                'end_date': current_timestamp + 1
            }, {
                'company_name': 'TheCompany',
                'end_date': 0
            }]
        }

        helpers.extend_user(user)

        mock_make_link.assert_called_once_with('Current', '/', mock.ANY)