import telebot
from config import TOKEN, keys
from extensions import ExchangeException, Exchange

bot = telebot.TeleBot(TOKEN)

# Обработка команды /start
@bot.message_handler(commands=['start'])
def start(message):
    start = "Привет! Я бот, который может вернуть цену на определенное количество валюты.\n\n" \
                   "Пример использования: <имя валюты, цену которой вы хотите узнать> " \
                   "<имя валюты, в которой нужно узнать цену первой валюты> <количество первой валюты>\n\n" \
                   "Команды:\n" \
                   "/start - выводит инструкции по применению бота\n" \
                   "/help - выводит список команд бота\n" \
                   "/values - выводит информацию о всех доступных валютах\n\n" \
                   "Пример запроса: Рубль доллар 100"
    bot.reply_to(message, start)

# Обработка команды /help
@bot.message_handler(commands=['help'])
def help(message):
    help = "/start - выводит инструкции по применению бота\n" \
           "/help - выводит список команд бота\n" \
           "/values - выводит информацию о всех доступных валютах\n\n" \
           "Регистр значения не имеет.\n\n" \
           "Пример запроса: Рубль доллар 100"
    bot.reply_to(message,help)

# Обработка команды /values
@bot.message_handler(commands=['values'])
def values(message: telebot.types.Message):
    text = 'Доступные валюты:'
    for key in keys.keys():
        text = '\n'.join((text, key,))
    bot.reply_to(message, text)

# Обработка текстовых сообщений от пользователя
@bot.message_handler(content_types=['text'])
def get_price(message: telebot.types.Message):
    try:
        values = message.text.lower().split(' ')  # преобразование в нижний регистр регистр

        if len(values) != 3:
            raise ExchangeException('Введите команду или 3 параметра')

        quote, base, amount = values
        total_base = Exchange.get_price(quote, base, amount)
    except ExchangeException as e:
        bot.reply_to(message, f'Ошибка пользователя.\n{e}')
    except Exception as e:
        bot.reply_to(message, f'Что-то пошло не так с {e}')
    else:
        text = f'Переводим {quote} в {base}\n{amount} {quote} = {total_base} {base}'
        bot.send_message(message.chat.id, text)

bot.polling()


#%% Imports
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from collections import defaultdict
from helpers import   pairwiseDistCorr,nn_reg,nn_arch,reconstructionError
from matplotlib import cm
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.random_projection import SparseRandomProjection, GaussianRandomProjection
from itertools import product
from helpers import get_data_from_csv

out = './RP/'
cmap = cm.get_cmap('Spectral')

np.random.seed(42)

# import wine quality data
wineX, wineY = get_data_from_csv("./BASE/wine_trg.csv", n_features=11, sep=',', header=None)
digitX, digitY = get_data_from_csv("./BASE/digit_trg.csv", n_features=256, sep=',', header=None)

wineX = StandardScaler().fit_transform(wineX)
digitX = StandardScaler().fit_transform(digitX)

clusters = [2,5,10,15,20,25,30,35,40]
dims = [2,5,10,15,20,25,30,35,40,45,50,55,60]
dims_wine = [i for i in range(2,12)]

# data for 1

tmp = defaultdict(dict)
for i,dim in product(range(10),dims_wine):
    rp = SparseRandomProjection(random_state=i, n_components=dim)
    tmp[dim][i] = pairwiseDistCorr(rp.fit_transform(wineX), wineX)
tmp =pd.DataFrame(tmp).T
tmp.to_csv(out+'wine scree1.csv')

tmp = defaultdict(dict)
for i,dim in product(range(10),dims):
    rp = SparseRandomProjection(random_state=i, n_components=dim)
    tmp[dim][i] = pairwiseDistCorr(rp.fit_transform(digitX), digitX)
tmp =pd.DataFrame(tmp).T
tmp.to_csv(out+'digit scree1.csv')

tmp = defaultdict(dict)
for i,dim in product(range(10),dims_wine):
    rp = SparseRandomProjection(random_state=i, n_components=dim)
    rp.fit(wineX)
    tmp[dim][i] = reconstructionError(rp, wineX)
tmp =pd.DataFrame(tmp).T
tmp.to_csv(out+'wine scree2.csv')

tmp = defaultdict(dict)
for i,dim in product(range(10),dims):
    rp = SparseRandomProjection(random_state=i, n_components=dim)
    rp.fit(digitX)
    tmp[dim][i] = reconstructionError(rp, digitX)
tmp =pd.DataFrame(tmp).T
tmp.to_csv(out+'digit scree2.csv')

# Data for 2

grid ={'rp__n_components':dims_wine,'NN__alpha':nn_reg,'NN__hidden_layer_sizes':nn_arch}
rp = SparseRandomProjection(random_state=5)
mlp = MLPClassifier(activation='relu',max_iter=2000,early_stopping=True,random_state=5)
pipe = Pipeline([('rp',rp),('NN',mlp)])
gs = GridSearchCV(pipe,grid,verbose=10,cv=5)

gs.fit(wineX,wineY)
tmp = pd.DataFrame(gs.cv_results_)
tmp.to_csv(out+'wine dim red.csv')

grid ={'rp__n_components':dims,'NN__alpha':nn_reg,'NN__hidden_layer_sizes':nn_arch}
rp = SparseRandomProjection(random_state=5)
mlp = MLPClassifier(activation='relu',max_iter=2000,early_stopping=True,random_state=5)
pipe = Pipeline([('rp',rp),('NN',mlp)])
gs = GridSearchCV(pipe,grid,verbose=10,cv=5)

gs.fit(digitX,digitY)
tmp = pd.DataFrame(gs.cv_results_)
tmp.to_csv(out+'digit dim red.csv')

# data for 3
# Set this from chart 2 and dump, use clustering script to finish up
dim = 6
rp = SparseRandomProjection(n_components=dim,random_state=5)

wineX2 = rp.fit_transform(wineX)
wine2 = pd.DataFrame(np.hstack((wineX2,np.atleast_2d(wineY))))
cols = list(range(wine2.shape[1]))
cols[-1] = 'Class'
wine2.columns = cols
wine2.to_csv(out+'wine_datasets.csv',index=False,header=False)

dim = 60
rp = SparseRandomProjection(n_components=dim,random_state=5)
digitX2 = rp.fit_transform(digitX)
digit2 = pd.DataFrame(np.hstack((digitX2,np.atleast_2d(digitY))))
cols = list(range(digit2.shape[1]))
cols[-1] = 'Class'
digit2.columns = cols
digit2.to_csv(out+'digit_datasets.csv',index=False,header=False)


from datetime import datetime

import six
from oslo_config import cfg
from oslo_log import log
from oslo_utils import uuidutils, importutils

from delfin import db
from delfin.common.constants import TelemetryCollection, TelemetryJobStatus
from delfin.exception import TaskNotFound
from delfin.i18n import _
from delfin.task_manager import rpcapi as task_rpcapi
from delfin.task_manager.scheduler import schedule_manager
from delfin.task_manager.tasks.telemetry import PerformanceCollectionTask

CONF = cfg.CONF
LOG = log.getLogger(__name__)

class JobHandler(object):
    def __init__(self, ctx, task_id, storage_id, args, interval):
        # create an object of periodic task scheduler
        self.ctx = ctx
        self.task_id = task_id
        self.storage_id = storage_id
        self.args = args
        self.interval = interval
        self.task_rpcapi = task_rpcapi.TaskAPI()
        self.scheduler = schedule_manager.SchedulerManager().get_scheduler()
        self.stopped = False
        self.job_ids = set()

    @staticmethod
    def get_instance(ctx, task_id):
        task = db.task_get(ctx, task_id)
        return JobHandler(ctx, task_id, task['storage_id'],
                          task['args'], task['interval'])

    def perform_history_collection(self, start_time, end_time, last_run_time):
        # Trigger one historic collection to make sure we do not
        # miss any Data points due to reschedule
        LOG.debug('Triggering one historic collection for task %s',
                  self.task_id)
        try:
            telemetry = PerformanceCollectionTask()
            ret = telemetry.collect(self.ctx, self.storage_id, self.args,
                                    start_time, end_time)
            LOG.debug('Historic collection performed for task %s with '
                      'result %s' % (self.task_id, ret))
            db.task_update(self.ctx, self.task_id,
                           {'last_run_time': last_run_time})
        except Exception as e:
            msg = _("Failed to collect performance metrics during history "
                    "collection for storage id:{0}, reason:{1}"
                    .format(self.storage_id, six.text_type(e)))
            LOG.error(msg)

    def schedule_job(self, task_id):

        if self.stopped:
            # If Job is stopped return immediately
            return

        LOG.info("JobHandler received A job %s to schedule" % task_id)
        job = db.task_get(self.ctx, task_id)
        # Check delete status of the task
        deleted = job['deleted']
        if deleted:
            return
        collection_class = importutils.import_class(
            job['method'])
        instance = collection_class.get_instance(self.ctx, self.task_id)
        current_time = int(datetime.now().timestamp())
        last_run_time = current_time
        next_collection_time = last_run_time + job['interval']
        job_id = uuidutils.generate_uuid()
        next_collection_time = datetime \
            .fromtimestamp(next_collection_time) \
            .strftime('%Y-%m-%d %H:%M:%S')

        existing_job_id = job['job_id']

        scheduler_job = self.scheduler.get_job(existing_job_id)

        if not (existing_job_id and scheduler_job):
            LOG.info('JobHandler scheduling a new job')
            self.scheduler.add_job(
                instance, 'interval', seconds=job['interval'],
                next_run_time=next_collection_time, id=job_id,
                misfire_grace_time=int(job['interval'] / 2))

            update_task_dict = {'job_id': job_id}
            db.task_update(self.ctx, self.task_id, update_task_dict)
            self.job_ids.add(job_id)
            LOG.info('Periodic collection tasks scheduled for for job id: '
                     '%s ' % self.task_id)

            # Check if historic collection is needed for this task.
            # If the last run time is already set, adjust start_time based on
            # last run time or history_on_reschedule which is smaller
            # If jod id is created but last run time is not yet set, then
            # adjust start_time based on interval or history_on_reschedule
            # whichever is smaller

            end_time = current_time * 1000
            # Maximum supported history duration on restart
            history_on_reschedule = CONF.telemetry. \
                performance_history_on_reschedule
            if job['last_run_time']:
                start_time = job['last_run_time'] * 1000 \
                    if current_time - job['last_run_time'] < \
                    history_on_reschedule \
                    else (end_time - history_on_reschedule * 1000)
                self.perform_history_collection(start_time, end_time,
                                                last_run_time)
            elif existing_job_id:
                interval_in_sec = job['interval']
                start_time = (end_time - interval_in_sec * 1000) \
                    if interval_in_sec < history_on_reschedule \
                    else (end_time - history_on_reschedule * 1000)
                self.perform_history_collection(start_time, end_time,
                                                last_run_time)
        else:
            LOG.info('Job already exists with this scheduler')

    def stop(self):
        self.stopped = True
        for job_id in self.job_ids.copy():
            self.remove_scheduled_job(job_id)
        LOG.info("Stopping telemetry jobs")

    def remove_scheduled_job(self, job_id):
        if job_id in self.job_ids:
            self.job_ids.remove(job_id)
        if job_id and self.scheduler.get_job(job_id):
            self.scheduler.remove_job(job_id)

    def remove_job(self, task_id):
        try:
            LOG.info("Received job %s to remove", task_id)
            job = db.task_get(self.ctx, task_id)
            job_id = job['job_id']
            self.remove_scheduled_job(job_id)
        except Exception as e:
            LOG.error("Failed to remove periodic scheduling job , reason: %s.",
                      six.text_type(e))

class FailedJobHandler(object):
    def __init__(self, ctx):
        # create an object of periodic failed task scheduler
        self.scheduler = schedule_manager.SchedulerManager().get_scheduler()
        self.ctx = ctx
        self.stopped = False
        self.job_ids = set()

    @staticmethod
    def get_instance(ctx, failed_task_id):
        return FailedJobHandler(ctx)

    def schedule_failed_job(self, failed_task_id):

        if self.stopped:
            return

        try:
            job = db.failed_task_get(self.ctx, failed_task_id)
            retry_count = job['retry_count']
            result = job['result']
            job_id = job['job_id']
            if retry_count >= \
                    TelemetryCollection.MAX_FAILED_JOB_RETRY_COUNT or \
                    result == TelemetryJobStatus.FAILED_JOB_STATUS_SUCCESS:
                LOG.info("Exiting Failure task processing for task [%d] "
                         "with result [%s] and retry count [%d] "
                         % (job['id'], result, retry_count))
                self._teardown_task(self.ctx, job['id'], job_id)
                return
            # If job already scheduled, skip
            if job_id and self.scheduler.get_job(job_id):
                return

            try:
                db.task_get(self.ctx, job['task_id'])
            except TaskNotFound as e:
                LOG.info("Removing failed telemetry job as parent job "
                         "do not exist: %s", six.text_type(e))
                # tear down if original task is not available
                self._teardown_task(self.ctx, job['id'],
                                    job_id)
                return

            if not (job_id and self.scheduler.get_job(job_id)):
                job_id = uuidutils.generate_uuid()
                db.failed_task_update(self.ctx, job['id'],
                                      {'job_id': job_id})

                collection_class = importutils.import_class(
                    job['method'])
                instance = \
                    collection_class.get_instance(self.ctx, job['id'])
                self.scheduler.add_job(
                    instance, 'interval',
                    seconds=job['interval'],
                    next_run_time=datetime.now(), id=job_id,
                    misfire_grace_time=int(job['interval'] / 2))
                self.job_ids.add(job_id)

        except Exception as e:
            LOG.error("Failed to schedule retry tasks for performance "
                      "collection, reason: %s", six.text_type(e))
        else:
            LOG.info("Schedule collection completed")

    def _teardown_task(self, ctx, failed_task_id, job_id):
        db.failed_task_delete(ctx, failed_task_id)
        self.remove_scheduled_job(job_id)

    def remove_scheduled_job(self, job_id):
        if job_id in self.job_ids:
            self.job_ids.remove(job_id)
        if job_id and self.scheduler.get_job(job_id):
            self.scheduler.remove_job(job_id)

    def stop(self):
        self.stopped = True
        for job_id in self.job_ids.copy():
            self.remove_scheduled_job(job_id)

    def remove_failed_job(self, failed_task_id):
        try:
            LOG.info("Received failed job %s to remove", failed_task_id)
            job = db.failed_task_get(self.ctx, failed_task_id)
            job_id = job['job_id']
            self.remove_scheduled_job(job_id)
            db.failed_task_delete(self.ctx, job['id'])
            LOG.info("Removed failed_task entry  %s ", job['id'])
        except Exception as e:
            LOG.error("Failed to remove periodic scheduling job , reason: %s.",
                      six.text_type(e))

    @classmethod
    def job_interval(cls):
        return TelemetryCollection.FAILED_JOB_SCHEDULE_INTERVAL


from trainer import image_classifier, augmentation_pipeline,GCSHelper
import argparse

def str2bool(v):
    if v.lower() in ('yes', 'true', 't', 'y', '1'):
        return True
    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

def run(data_directory, output_directory, project_id,augment_flag,augment_samples,nr_epochs,drop_out,val_split,model,batch_size,check_overfit):
    image_classifier.check_input(project_id=project_id, data_dir=data_directory, output_dir=output_directory,
                                 validation_split=val_split, num_epochs=nr_epochs, dropout=drop_out,
                                 augmentation_samples=augment_samples)

    print('AUGMENTING IMAGES...')
    if augment_flag:
        augmentation_pipeline.augmentImages(project_id=project_id, data_dir=data_directory, sample_size=augment_samples,cloudML=True)
    print('AUGMENTING IMAGES DONE!')
    print('TRAINING MODEL...')
    image_classifier.retrain(project_id, data_directory, batch_size=batch_size, model=model, dropout=drop_out, num_epochs=nr_epochs,
                             validation_split=val_split, output_dir=output_directory, cloud_mode=True,check_overfit=check_overfit)

parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str, help='directory of data')
parser.add_argument('--output_dir', type=str, help='directory of output model')
parser.add_argument('--project_id', type=str, default="trainer-cl", help='Google cloud projectID')
parser.add_argument('--aug_flag', type=str2bool, default=False, help='True if augmentation is done on images')
parser.add_argument('--aug_samples', type=int, default=1, help='extra augmentation samples that are added per category')
parser.add_argument('--nr_epochs', type=int, default=1, help='extra augmentation samples that are added per category')
parser.add_argument('--drop_out', type=float, default=0.1, help='Amount of droppout to prevent overfitting')
parser.add_argument('--val_split', type=float, default=0.1, help='Percentage of data used for validation')
parser.add_argument('--model', type=str, default="MobileNet", help='Used model architecture')
parser.add_argument('--batch_size', type=int, default=16, help='Batch size used for model training')
parser.add_argument('--check_overfit', type=str2bool, default=True, help='Add early stopping check')

args = parser.parse_args()

try:
    run(args.data_dir,args.output_dir,args.project_id,args.aug_flag,args.aug_samples,args.nr_epochs,args.drop_out,args.val_split,args.model,args.batch_size,args.check_overfit)
    GCSHelper.uploadClosingStatusFilesToGCS(args.project_id,[],'done.txt',args.output_dir)
except Exception as e:
    GCSHelper.uploadClosingStatusFilesToGCS(args.project_id,[str(e)], 'wrong.txt', args.output_dir)


from flask import Flask, jsonify, request
from flask_cors import CORS
from flask_jwt_extended import create_access_token, JWTManager
from flask_mysqldb import MySQL
from dotenv import load_dotenv
import os
from datetime import datetime

app = Flask(__name__)

load_dotenv()

app.config['MYSQL_HOST'] = os.environ.get('MYSQL_HOST')
app.config['MYSQL_USER'] = os.environ.get('MYSQL_USER')
app.config['MYSQL_PASSWORD'] = os.environ.get('MYSQL_PASSWORD')
app.config['MYSQL_DB'] = os.environ.get('MYSQL_DB')

mysql = MySQL(app)

CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True)

@app.route('/coords', methods=["GET"])
def get_coords():
    try:
        cursor = mysql.connection.cursor()
        cursor.execute("SELECT latitude, longitude FROM sample")
        results = cursor.fetchall()
        cursor.close()

        coords = [{'lat': row[0], 'lng': row[1]} for row in results]

        return jsonify({'coords': coords})

    except Exception as e:
        return jsonify({'error': str(e)}), 500


from PIL import Image
from os import listdir, mkdir

def PrepareChars5x7(jmeno, mezX, mezY):
    im = Image.open(jmeno)
    Pixels = im.load()
    for x in range(13):
        for y in range(4):
            imnew = Image.new(mode="RGB", size=(5, 7))
            pole = imnew.load()
            print(pole[1, 1], imnew.size)
            for x2 in range(5):
                for y2 in range(7):
                    pole[x2, y2] = Pixels[x2 + (5 + mezX) * x, y2 + (7 + mezY) * y]

            imnew.save("Characters/ch" + str(x + 13 * y) + ".png")

def Roztrid():
    seznam = listdir("Characters")

    for polozka in seznam:
        im = Image.open("Characters/" + polozka)
        pixels = im.load()

        hodnota = 0
        for x in range(5):
            for y in range(7):
                if pixels[x,y][0] != 0:
                    hodnota += 1

        if str(hodnota) not in listdir("Characters"):
            mkdir("Characters/" + str(hodnota))

        im.save("Characters/" + str(hodnota) + "//" + polozka)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Picture',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('image', models.ImageField(null=True, upload_to=b'media/product_pictures', blank=True)),
                ('description', models.CharField(max_length=140, null=True, blank=True)),
                ('default_picture', models.BooleanField(default=False)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Product',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=140)),
                ('description', models.TextField(null=True, blank=True)),
                ('total_number_of_tickets', models.IntegerField()),
                ('tickets_sold', models.IntegerField()),
                ('end_time', models.DateTimeField()),
                ('start_time', models.DateTimeField()),
                ('pricing_per_ticket', models.DecimalField(max_digits=8, decimal_places=2)),
                ('winning_ticket_number', models.IntegerField()),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Ticket',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('ticket_number', models.IntegerField()),
                ('product', models.ForeignKey(related_name='tickets', to='ticketing.Product')),
                ('user', models.ForeignKey(related_name='tickets', to=settings.AUTH_USER_MODEL)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.AddField(
            model_name='picture',
            name='product',
            field=models.ForeignKey(related_name='pictures', to='ticketing.Product'),
            preserve_default=True,
        ),
    ]


# 导入所需的库
import jieba
import docx
from docx import Document
from docx.shared import Inches
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS

# 读取文档内容
filter_words = ['', '','','','','','','']
document = Document('221.docx')
text = ''
text= jieba.cut(text)
text = ''.join(str(x) for x in text)
for paragraph in document.paragraphs:
    text += paragraph.text + ' '
for word in filter_words:
    text = text.replace(word, '')

# 创建停用词集合
stopwords = set(STOPWORDS)
stopwords = ['同志们', '二','三','四','五','一','六','七','八','九','十','']
# 创建词云对象，并设置参数
wordcloud = WordCloud(
    font_path="simhei.ttf",
    width=1200, height=800,
    background_color='white',
    stopwords=stopwords,
    min_font_size=10).generate(text)

# 绘制词云图
plt.figure(figsize=(8, 8), facecolor=None)
plt.imshow(wordcloud)
plt.axis("off")
plt.tight_layout(pad=0)
plt.show()
# 创建需要过滤的词汇列表

# 加载需要过滤的文本
text = 'I hate this bad movie, it is so ugly and boring.'

# 使用字符串函数 replace() 进行替换

print(text)


#!/usr/bin/env python3
#_*_ coding: utf8 _*_

#------------------------------------------------------------
#-----            GUILLOTINE                           -----|
# ----            FINDER HTTP SECURITY HEADERS          ----|
# ----            Gohanckz                              ----|
# ----            Contact : igonzalez@pwnsec.cl         ----|
# ----            Version : 2.0                         ----|
#------------------------------------------------------------
try:
    from banner import banner
    from prettytable import PrettyTable
    import requests
    import argparse
    from urllib3.exceptions import InsecureRequestWarning
except ImportError as err:
    print("Some libraries are missing:")
    print(err)

parser = argparse.ArgumentParser(description="Finder Security Headers")
parser.add_argument("-t","--target",help="Show http security headers enabled and missing")
parser.add_argument("-v","--verbose",action="store_true",help="Show full response")
parser = parser.parse_args()

try:
    requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
    url = requests.get(url=parser.target, verify=False)

    security_headers = [
        "Strict-Transport-Security",
        "X-Frame-Options",
        "X-Content-Type-Options",
        "Content-Security-Policy",
        "X-Permitted-Cross-Domain-Policies",
        "Referrer-Policy",
        "Clear-Site-Data",
        "Cross-Origin-Embedder-Policy",
        "Cross-Origin-Opener-Policy",
        "Cross-Origin-Resource-Policy",
        "Cache-Control"
    ]

    info_headers = []
    headers_site = []
    security_headers_site = []
    missing_headers = []

    headers = dict(url.headers)

    for i in headers:
        headers_site.append(i)

    for i in headers:
        info_headers.append(headers[i])

    for i in headers_site:
        if i in security_headers:
            security_headers_site.append(i)

    for j in security_headers:
        if not j in [h for h in headers_site]:
            missing_headers.append(j)

    table = PrettyTable()
    table.add_column("Header",headers_site)
    table.add_column("Information",info_headers)
    table.align="l"

    while len(security_headers_site) < len(missing_headers):
        security_headers_site.append(" ")

    while len(security_headers_site) > len(missing_headers):
        missing_headers.append(" ")

    count = 0
    for i in security_headers_site:
        if i != " ":
            count += 1

    count_m = 0
    for j in missing_headers:
        if j != " ":
            count_m +=1

    s_table = PrettyTable()
    s_table.add_column("Enabled Security Header",security_headers_site)
    s_table.add_column("Missing Security Header",missing_headers)
    s_table.align="l"
except:
    print("[!] time out, unable to connect to site.")

def main():
    banner()
    try:
        print("\n[*] Analyzing target : ",parser.target)
        print("[*] Security headers enabled :", count)
        print("[*] Missing Security Headers :",count_m)
    except:
        print("[!] Syntax Error.")
        print("[+] Usage: python3 guillotine.py -t http://example.site")
def target():
    try:
        print(s_table)
    except:
        pass
def verbose():
    try:
        print(table)
    except:
        pass
if __name__ == '__main__':
    main()
    if parser.verbose:
        verbose()
    elif parser.target:
        target()


from django.db import models

class Vehicles(models.Model):

    class Meta:
        ordering = [ 'year']

    id = models.AutoField(
        primary_key = True
    )

    year_min = 1900
    year_max = 2100
    year = models.IntegerField(
        'Year',
    )

    man_max_len = 50
    manufacturer = models.CharField(
        'Manufacturer',
        max_length = man_max_len,
    )

    model_max_len = 100
    model = models.CharField(
        'model',
        max_length = model_max_len
    )

    sn_max_len = 15
    serial_no = models.CharField(
        'Serial Number',
        unique = True,
        max_length = sn_max_len
    )


import connexion

from openapi_server import orm
from openapi_server.db import db
from openapi_server.models.error import Error  # noqa: E501
from openapi_server.models.qc_result import QcResult  # noqa: E501

def samples_id_qc_result_delete(id):  # noqa: E501
    """samples_id_qc_result_delete

    Delete the QC result associated with a sample with {id}. # noqa: E501

    :param id:
    :type id: str

    :rtype: None
    """

    sample = orm.Sample.query.get(id)
    if not sample or not sample.qc_result:
        return Error(404, 'Not found'), 404

    db.session.delete(sample.qc_result)
    db.session.commit()

    return '', 204

def samples_id_qc_result_get(id):  # noqa: E501
    """samples_id_qc_result_get

    Return the QC result associated with a sample. # noqa: E501

    :param id:
    :type id: str

    :rtype: QcResult
    """

    sample = orm.Sample.query.get(id)
    if not sample or not sample.qc_result:
        return Error(404, 'Not found'), 404

    return sample.qc_result.to_model(), 200

def samples_id_qc_result_put(id, qc_result=None):  # noqa: E501
    """samples_id_qc_result_put

    Add or replace new QC result associated with a sample. # noqa: E501

    :param id:
    :type id: str
    :param qc_result: QC result to be added
    :type qc_result: dict | bytes

    :rtype: QcResult
    """
    if connexion.request.is_json:
        qc_result = QcResult.from_dict(connexion.request.get_json())  # noqa: E501

    sample = orm.Sample.query.get(id)
    if not sample:
        return Error(404, 'Not found'), 404

    inst = orm.QcResult.from_model(qc_result)
    inst.sample_id = sample.id

    if sample.qc_result:
        sample.qc_result = inst
    else:
        db.session.add(inst)
    db.session.commit()

    return inst.to_model(), 200, {'location': ''}


import os
import cv2
import pytesseract
import numpy as np
from tqdm import tqdm

INPUT_PATH: str = "inputs_control/"
OUTPUT_PATH: str = "text_pred_control/"
#CONFIG: str = "--psm 6 --oem 1"
CONFIG: str = "--psm 7 --oem 1"

def pipeline(file) -> str:

    path: str = f"{INPUT_PATH}{file}"
    img: np.ndarray = cv2.imread(path)
    text: str = pytesseract.image_to_string(img, config=CONFIG)

    iterator: str = file.split(".")[0]
    with open(OUTPUT_PATH + f"{iterator}.txt", 'w') as f:
        f.write(text)

    return text

def main() -> int:

    files = os.listdir(INPUT_PATH)

    for file in tqdm(files):
        pipeline(file)

    return 0

if __name__ == "__main__":
    main()


import os
import requests
from app import Processing
import nltk
from moviepy.editor import *
from pexels_api import API
from pathlib import Path
import time
import pyttsx3

# configurations of paths, output URL, file structure
# 16:9 ratios possible for upright smartphone usage
# 1080, 1920 --> FullHD resolution
# 540, 960 --> 1/4 data size compared to FullHD
# 270, 480 --> 1/8 data size compared to FullHD
WIDTH_OUT = 540/2
HEIGHT_OUT = 960/2
screensize = (WIDTH_OUT, HEIGHT_OUT)

FONT = "Helvetica-Bold"
FONTSIZE_MAIN = WIDTH_OUT * 0.1
FONTSIZE_SUB = WIDTH_OUT * 0.03
FONT_COLOUR = "white"
PADDING = WIDTH_OUT * 0.1

readingSpeed = 0.2

audio_dir_emotional = "static/music/emotional.mp3"
audio_dir_promo = "static/music/promo.mp3"
audio_dir_neutral = "static/music/neutral.mp3"

audio_emotional = AudioFileClip(audio_dir_emotional, fps=44100)
audio_neutral = AudioFileClip(audio_dir_neutral, fps=44100)
audio_promo = AudioFileClip(audio_dir_promo, fps=44100)

ABS_PATH = os.path.abspath(__file__)  # "/app.py"
BASE_DIR = os.path.dirname(ABS_PATH)  # "/"

Path(os.path.join(BASE_DIR, "downloads")).mkdir(parents=True, exist_ok=True)
OUTPUT = os.path.join(BASE_DIR, "downloads")

# API setups
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
api = API(PEXELS_API_KEY)

def dl_img(url, filename):
    print(filename)
    r = requests.get(url, allow_redirects=True)
    open(filename, 'wb').write(r.content)
    return filename

def pexels_fetch(to_download):
    downloaded_files = []
    n = 0

    for i in to_download:
        api.search(" ".join(i), page=1, results_per_page=1)
        dl = api.get_entries()
        print(dl)
        img = [
            dl_img(dl[0].large, os.path.join(OUTPUT, str("image_downloaded_" + str(n) + ".jpg"))),
            dl[0].photographer
               ]
        downloaded_files.append(img)
        n += 1
    return downloaded_files

def zoom(file, t):
    f = (ImageClip(file)
         .resize(height=screensize[1])
         .resize(lambda t: 1 + 0.02 * t)
         .set_position(('center', 'center'))
         .set_duration(t)
         )
    f = resize_to_ouput_size(f)
#    cvc = ImageClip(f, t)
    return f

def resize_to_ouput_size(f):
    if f.w < WIDTH_OUT:
        f = f.resize(width=WIDTH_OUT)
    if f.h < HEIGHT_OUT:
        f = f.resize(height=HEIGHT_OUT)
    f = f.crop(x_center=f.w / 2, y_center=f.h / 2, width=WIDTH_OUT, height=HEIGHT_OUT)
    return f

'''
# voiceover functionality deprecated due to non-existent espeak support on heroku

def voiceover(textSnippet, i):
    engine = pyttsx3.init()
    print(f"inside voiceover func, processing: {textSnippet} \nIsBusy is set to {engine.isBusy()}")
    audioFileName = f"voiceover text segment no. {i}.mp3"
    engine.save_to_file(textSnippet, audioFileName)
    engine.runAndWait()
#    engine.stop()
    print(f"text to speech worked correctly? \nisBusy is set to {engine.isBusy()}")

    return audioFileName
'''

def overlay_text(file, i):
    overlay = TextClip(file.text_segmented[i],
                       size=(WIDTH_OUT * 0.9, HEIGHT_OUT),
                       color=FONT_COLOUR,
                       method="caption",
                       align="East",
                       fontsize=FONTSIZE_MAIN,
                       font=FONT
                       )
    combined = CompositeVideoClip([overlay, overlay_attribution(file.downloaded_items[i][1])])

# voiceover functionality deprecated
#    if file.voiceover == True or file.voiceover == "true" or file.voiceover == "True":
#        audio_clip_temp = AudioFileClip(voiceover(file.text_segmented[i], i), fps=44100)
#        combined = combined.set_audio(audio_clip_temp)

    combined = combined.set_duration(file.text_timing[i])

    return combined

def overlay_attribution(text):
    attribution = TextClip(f"Image from www.pexels.com by: {text}",
                           size=(WIDTH_OUT, HEIGHT_OUT * 0.95),
                           color=FONT_COLOUR,
                           fontsize=FONTSIZE_SUB,
                           align="south",
                           method="caption",
                           font=FONT
                           )
    attribution = attribution.set_position((0, 0.97), relative=True)
    return attribution

def create_kopfkino(content):
    file = Processing(user_input=content.get("user_input"), style=content.get("style"), voiceover=content.get("voiceover"))
    print(f"voiceover from content JSON is set to: {file.voiceover}")
    nlp_testing_2(file)
    print(file.downloaded_items)
    print(file.text_searchwords)
    file.downloaded_items = pexels_fetch(file.text_searchwords)

    for i in range(0, len(file.downloaded_items)):
        file.footage.append(zoom(file.downloaded_items[i][0], file.text_timing[i]))
    for i in range(0, len(file.text_segmented)):
        clip = overlay_text(file, i)
        combined = CompositeVideoClip([file.footage[i], clip])
        file.footage_and_text.append(combined)

    file.export_file = concatenate(file.footage_and_text)

    if file.style == "neutral":
        file.export_file = file.export_file.set_audio(audio_neutral.set_duration(file.export_file.duration))
    elif file.style == "emotional":
        file.export_file = file.export_file.set_audio(audio_emotional.set_duration(file.export_file.duration))
    elif file.style == "promo":
        file.export_file = file.export_file.set_audio(audio_promo.set_duration(file.export_file.duration))
    else:
        file.export_file = file.export_file.set_audio(audio_neutral.set_duration(file.export_file.duration))

    file.export_file.write_videofile(os.path.join(OUTPUT, f"Kopfkino_export_in workerinstance.mp4"), codec='libx264',
                                     audio_codec='aac', fps=24)
    with open(os.path.join(OUTPUT, f"Kopfkino_export_in workerinstance.mp4"), "rb") as trans:
        result = trans.read()

    return result

def nlp_testing_2(file):
    text_raw = file.user_input
    print(text_raw)
    file.text_segmented = nltk.sent_tokenize(text_raw)
    for i in range(0, len(file.text_segmented)):
        n = 0
        for c in file.text_segmented[i]:
            n += 1
        n = round(n * readingSpeed, 1)
        if n < 5:
            n = 5
        file.text_timing.append(n)
        text_segmented_to_words = nltk.word_tokenize(file.text_segmented[i])
        file.text_searchwords.append([])
        print(f"POS Tags{nltk.pos_tag(text_segmented_to_words)}")
        for p in nltk.pos_tag(text_segmented_to_words):
            if p[1] in {"JJ", "NN", "NNS", "VB"}:
                print(f"found word {p} and put it to the searchwords")
                file.text_searchwords[i].append(p[0])

    for x in file.text_searchwords:
        if len(x) == 0:
            x.append("error")
            print("-------> ERROR HANDLING NEEDED: No searchword left: appended full sentence OR error")

    return f"\nsegmented: {file.text_segmented}, \ntimings: {file.text_timing} \nsearchwords: {file.text_searchwords}"


import pandas as pd
import numpy as np
import random
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt

x= ""

def calc_color_indxs(centroids):
    #function assigns centroid indexes to each training example i.e. it assigns the
    # nearest cluster centroid to each training example
    # It uses Eucledian Distance to measure the distance between cluster centroids and training example
    global x
    centroid_indx = np.zeros(((x.shape[0]),1))
    for i in range(0,x.shape[0]):
        dist = x[i,:]-centroids
        dist = np.sum(np.power(dist,2),axis = 1)
        centroid_indx[i] = np.argmin(dist)
    return centroid_indx.astype(int)

def calc_cost(centroids,sample_color_indx):
    #calculates the cost value of the calculated centroid.
    #cost = average of the distances between the centroids and the assigned training examples
    sample_centroids = centroids[sample_color_indx.reshape((sample_color_indx.shape[0]))]
    dist = x - sample_centroids
    dist = np.sum(np.power(np.sum(np.power(dist,2),axis = 1),0.5),axis = 0)
    return dist/sample_centroids.shape[0]

def update_centroids(centroids,sample_color_indx,k):
    #updates the centroid for each assigned cluster
    #calculates the centroid by taking mean of all the example assigned to the cluster
    for i in range(0,k):
        indxs = np.where(sample_color_indx == i)
        x_centroid = x[indxs[0]]
        if x_centroid.shape[0] == 0:
            continue
        centroids[i] = np.mean(x_centroid,axis = 0)
    return centroids

if __name__ == '__main__':
    data = load_iris(as_frame = True)
    df = data.data
    num_of_features = df.shape[1]
    x = np.array(df.iloc[1:,0:num_of_features])
    k = int(input("Enter Number of Clusters: "))
    random_init_indx = random.sample(range(0,df.shape[0]),k)
    centroids = np.array(df.iloc[random_init_indx,0:num_of_features])
    plt.subplot(1,2,1)
    i = 0
    #------------------------------------------------------------------
    sample_color_indx = calc_color_indxs(centroids) #step1
    cost0 = calc_cost(centroids,sample_color_indx)
    prev_centroids = centroids
    centroids = update_centroids(centroids,sample_color_indx,k) #step2\
    plt.scatter(i,cost0)
    i = i + 1
    #----------------------------------------------------------------
    sample_color_indx = calc_color_indxs(centroids) #step1
    cost1 = calc_cost(centroids,sample_color_indx) #step2
    #--------------------------------------------------------------------
    while cost0-cost1>=pow(10,-9):
        i = i + 1
        plt.scatter(i,cost1)
        prev_centroids = centroids
        centroids = update_centroids(centroids,sample_color_indx,k)
        cost0 = cost1
        sample_color_indx = calc_color_indxs(centroids)
        cost1 = calc_cost(centroids,sample_color_indx)
    print(cost0)
    #plots two subplots in a figure,
    #1.) Cost funcn vs. no. of iterations
    #2.) Plot Training examples of same clusters with same color.
    plt.subplot(1,2,2)
    sample_color_indx = calc_color_indxs(prev_centroids)
    colors = plt.cm.Spectral(np.linspace(0,1,k))
    for i,col in zip(range(k),colors):
        indxs = np.where(sample_color_indx == i)
        x_centroid = x[indxs[0]]
        plt.scatter(x_centroid[:,0],x_centroid[:,1],color = col)
    plt.show()


import copy
import json
import logging
import os
from threading import Thread

import requests
import six
import yaml
from toscaparser.tosca_template import ToscaTemplate
from yaml import Loader

from configuration_tool.common.tosca_reserved_keys import IMPORTS, DEFAULT_ARTIFACTS_DIRECTORY, \
    EXECUTOR, NAME, TOSCA_ELEMENTS_MAP_FILE, TOSCA_ELEMENTS_DEFINITION_FILE, TOPOLOGY_TEMPLATE, TYPE, \
    TOSCA_ELEMENTS_DEFINITION_DB_CLUSTER_NAME, NODE_TEMPLATES, RELATIONSHIP_TEMPLATES
from configuration_tool.common import utils
from configuration_tool.common.configuration import Configuration
from configuration_tool.configuration_tools.ansible.instance_model.instance_model import update_instance_model
from configuration_tool.configuration_tools.combined.combine_configuration_tools import get_configuration_tool_class
from configuration_tool.providers.common.provider_configuration import ProviderConfiguration
from configuration_tool.providers.common.tosca_template import ProviderToscaTemplate

REQUIRED_CONFIGURATION_PARAMS = (TOSCA_ELEMENTS_DEFINITION_FILE, DEFAULT_ARTIFACTS_DIRECTORY, TOSCA_ELEMENTS_MAP_FILE)

REQUIRED_CONFIGURATION_PARAMS = (TOSCA_ELEMENTS_DEFINITION_FILE, DEFAULT_ARTIFACTS_DIRECTORY, TOSCA_ELEMENTS_MAP_FILE)

def load_to_db(node_templates, relationship_templates, config, database_api_endpoint, template, cluster_name):
    definitions = {}
    all_templates = node_templates
    all_templates = utils.deep_update_dict(all_templates, relationship_templates)
    def_cluster = config.get_section(config.MAIN_SECTION).get(TOSCA_ELEMENTS_DEFINITION_DB_CLUSTER_NAME)
    for key, value in all_templates.items():
        type = value[TYPE]
        r = requests.get(utils.get_url_for_getting_dependencies(def_cluster, database_api_endpoint, type))
        try:
            response = r.json()
        except Exception:
            raise Exception("Failed to parse json response from db")
        if response['status'] != 200:
            raise Exception("Error in db! Status code: %s, msg: %s" % (response['status'], response['message']))
        definitions = utils.deep_update_dict(definitions, response['result'])
    with open(os.path.join(utils.get_tmp_clouni_dir(), 'template.yaml'), "w") as f:
        template = utils.deep_update_dict(template, definitions)
        del template[IMPORTS]
        print(yaml.dump(template, Dumper=utils.NoAliasDumper), file=f)
    with open(os.path.join(utils.get_tmp_clouni_dir(), 'template.yaml'), "r") as f:
        files = {'file': f}
        res = requests.post(utils.get_url_for_loading_to_db(cluster_name, database_api_endpoint), files=files)
        try:
            response = res.json()
        except Exception:
            raise Exception("Failed to parse json response from db on loading template")
        if response['status'] != 200:
            raise Exception("Error in db! Status code: %s, msg: %s" % (response['status'], response['message']))

def translate(provider_template, validate_only, configuration_tool, cluster_name, is_delete=False,
              extra=None, log_level='info', debug=False, host_ip_parameter='public_address',
              database_api_endpoint=None, grpc_cotea_endpoint=None):
    log_map = dict(
        debug=logging.DEBUG,
        info=logging.INFO,
        warning=logging.WARNING,
        error=logging.ERROR,
        critical=logging.ERROR
    )

    logging_format = "%(asctime)s %(levelname)s %(message)s"
    logging.basicConfig(filename='.clouni-configuration-tool.log', filemode='a', level=log_map[log_level],
                        format=logging_format, datefmt='%Y-%m-%d %H:%M:%S')

    config = Configuration()

    template = yaml.load(provider_template, Loader=Loader)
    topology_template = template.get(TOPOLOGY_TEMPLATE)

    # tmp version - provider gets from first node template (can't use different providers in template)
    provider = None
    for key in topology_template.get('node_templates').keys():
        provider_template_name = key
        tosca_type = topology_template.get('node_templates').get(provider_template_name).get('type')
        (provider, _, _) = utils.tosca_type_parse(tosca_type)
        if provider in ['openstack', 'amazon', 'kubernetes']: # TODO: make config prividers file!
            break

    provider_config = ProviderConfiguration(provider)
    for sec in REQUIRED_CONFIGURATION_PARAMS:
        if sec not in config.get_section(config.MAIN_SECTION).keys():
            logging.error('Provider configuration parameter "%s" is missing in configuration file' % sec)
            raise Exception('Provider configuration parameter "%s" is missing in configuration file' % sec)

    def_files = config.get_section(config.MAIN_SECTION).get(TOSCA_ELEMENTS_DEFINITION_FILE)
    if isinstance(def_files, six.string_types):
        def_files = [def_files]
    provider_def_files = provider_config.get_section(config.MAIN_SECTION).get(TOSCA_ELEMENTS_DEFINITION_FILE)
    if isinstance(provider_def_files, six.string_types):
        provider_def_files = [provider_def_files]
    default_import_files = []
    for def_file in def_files:
        default_import_files.append(os.path.join(utils.get_project_root_path(), def_file))
    for def_file in provider_def_files:
        default_import_files.append(os.path.join(utils.get_project_root_path(), 'configuration_tool', 'providers',
                                                 provider, def_file))
    logging.info("Default TOSCA template definition file to be imported \'%s\'" % json.dumps(default_import_files))

    # Add default import of normative TOSCA types to the template
    template[IMPORTS] = template.get(IMPORTS, [])
    for i in range(len(template[IMPORTS])):
        if isinstance(template[IMPORTS][i], dict):
            for import_key, import_value in template[IMPORTS][i].items():
                if isinstance(import_value, six.string_types):
                    template[IMPORTS][i] = import_value
                elif isinstance(import_value, dict):
                    if import_value.get('file', None) is None:
                        logging.error("Imports %s doesn't contain \'file\' key" % import_key)
                        raise Exception("Imports %s doesn't contain \'file\' key" % import_key)
                    else:
                        template[IMPORTS][i] = import_value['file']
                    if import_value.get('repository', None) is not None:
                        logging.warning("Clouni doesn't support imports \'repository\'")
    template[IMPORTS].extend(default_import_files)
    for i in range(len(template[IMPORTS])):
        template[IMPORTS][i] = os.path.abspath(template[IMPORTS][i])
    if template.get(TOPOLOGY_TEMPLATE):
        tmpl = template.get(TOPOLOGY_TEMPLATE)
        if database_api_endpoint:
            if not tmpl.get(NODE_TEMPLATES):
                tmpl[NODE_TEMPLATES] = {}
            if not tmpl.get(RELATIONSHIP_TEMPLATES):
                tmpl[RELATIONSHIP_TEMPLATES] = {}
            load_to_db(tmpl[NODE_TEMPLATES], tmpl[RELATIONSHIP_TEMPLATES], config, database_api_endpoint, template, cluster_name)
        else:
            if tmpl.get(NODE_TEMPLATES):
                node_templates = tmpl.get(NODE_TEMPLATES)
                for elem in node_templates:
                    update_instance_model(cluster_name, node_templates[elem], node_templates[elem][TYPE], elem, [], [], is_delete, init=True)
            if tmpl.get(RELATIONSHIP_TEMPLATES):
                rel_templates = tmpl.get(RELATIONSHIP_TEMPLATES)
                for elem in rel_templates:
                    update_instance_model(cluster_name, rel_templates[elem], rel_templates[elem][TYPE], elem, [], [], is_delete, init=True)

    copy_of_template = copy.deepcopy(template)
    try:
        tosca_parser_template_object = ToscaTemplate(yaml_dict_tpl=copy_of_template)
    except Exception as e:
        logging.exception("Got exception from OpenStack tosca-parser: %s" % e)
        raise Exception("Got exception from OpenStack tosca-parser: %s" % e)

    # After validation, all templates are imported
    if validate_only:
        msg = 'The input "%(template_file)s" successfully passed validation. \n' \
              % {'template_file': 'TOSCA template'}
        return msg

    tosca = ProviderToscaTemplate(template, provider, configuration_tool, cluster_name,
                                  host_ip_parameter, is_delete, grpc_cotea_endpoint)

    tool = get_configuration_tool_class(configuration_tool)(provider)

    default_artifacts_directory = config.get_section(config.MAIN_SECTION).get(DEFAULT_ARTIFACTS_DIRECTORY)

    configuration_content = tool.to_dsl(provider, tosca.provider_operations, tosca.reversed_provider_operations,
                                        tosca.cluster_name, is_delete, target_directory=default_artifacts_directory,
                                        extra=extra, debug=debug,
                                        grpc_cotea_endpoint=grpc_cotea_endpoint)
    return configuration_content


import numpy as np
import tensorflow as tf
from pyTasks.task import Task, Parameter
from pyTasks.task import Optional, containerHash
from pyTasks.target import CachedTarget, LocalTarget
from pyTasks.target import JsonService, FileTarget
from .gram_tasks import PrepareKernelTask
import logging
import math
from time import time
import os
from tensorflow.contrib.tensorboard.plugins import projector
from scipy.spatial.distance import cdist
from .graph_tasks import EdgeType

class WVSkipgram(object):

    def __init__(self, num_words, learning_rate, embedding_size,
                 num_steps, neg_sampling, unigrams, log="./log/"):
        self.num_words = num_words
        self.learning_rate = learning_rate
        self.embedding_size = embedding_size
        self.num_steps = num_steps
        self.neg_sampling = neg_sampling
        self.unigrams = unigrams
        self.log_dir = log
        self.graph, self.batch_inputs, self.batch_labels,self.normalized_embeddings,\
        self.loss, self.optimizer = self.trainer_initial()

    def trainer_initial(self):
        graph = tf.Graph()
        with graph.as_default():

            # logging
            self.logger = tf.summary.FileWriter(self.log_dir)

            with tf.name_scope("embedding"):
                batch_inputs = tf.placeholder(tf.int64, shape=([None, ]))
                batch_labels = tf.placeholder(tf.int64, shape=([None, 1]))

                graph_embeddings = tf.Variable(
                        tf.random_uniform([self.num_words, self.embedding_size], -0.5 / self.embedding_size, 0.5/self.embedding_size),
                        name='word_embedding')

                batch_graph_embeddings = tf.nn.embedding_lookup(graph_embeddings, batch_inputs) #hiddeb layer

                weights = tf.Variable(tf.truncated_normal([self.num_words, self.embedding_size],
                                                              stddev=1.0 / math.sqrt(self.embedding_size))) #output layer wt
                biases = tf.Variable(tf.zeros(self.num_words)) #output layer biases

                #negative sampling part
                loss = tf.reduce_mean(
                    tf.nn.nce_loss(weights=weights,
                                   biases=biases,
                                   labels=batch_labels,
                                   inputs=batch_graph_embeddings,
                                   num_sampled=self.neg_sampling,
                                   num_classes=self.num_words,
                                   sampled_values=tf.nn.fixed_unigram_candidate_sampler(
                                       true_classes=batch_labels,
                                       num_true=1,
                                       num_sampled=self.neg_sampling,
                                       unique=True,
                                       range_max=self.num_words,
                                       distortion=0.75,
                                       unigrams=self.unigrams)#word_id_freq_map_as_list is the
                                   # frequency of each word in vocabulary
                                   ))
                norm = tf.sqrt(tf.reduce_mean(tf.square(graph_embeddings), 1, keep_dims=True))
                normalized_embeddings = graph_embeddings/norm

                # summary
                tf.summary.histogram("weights", weights)
                tf.summary.histogram("biases", biases)
                tf.summary.scalar("loss", loss)

                config = projector.ProjectorConfig()
                emb = config.embeddings.add()
                emb.tensor_name = normalized_embeddings.name
                emb.metadata_path = os.path.join(self.log_dir, 'vocab.tsv')
                projector.visualize_embeddings(self.logger, config)

            with tf.name_scope('descent'):
                global_step = tf.Variable(0, trainable=False)
                learning_rate = tf.train.exponential_decay(self.learning_rate,
                                                           global_step, 100000, 0.96, staircase=True) #linear decay over time

                learning_rate = tf.maximum(learning_rate,0.001) #cannot go below 0.001 to ensure at least a minimal learning

                optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step=global_step)

                self.logger.add_graph(graph)
        return graph, batch_inputs, batch_labels, normalized_embeddings, loss, optimizer

    def train(self, dataset):
        with tf.Session(graph=self.graph,
                        config=tf.ConfigProto(log_device_placement=True,allow_soft_placement=False)) as sess:

            merged_summary = tf.summary.merge_all()
            saver = tf.train.Saver()

            init = tf.global_variables_initializer()
            sess.run(init)

            sess.run(tf.tables_initializer())

            step = 0

            for i in range(self.num_steps):
                t0 = time()

                feed_it = dataset.make_initializable_iterator()
                next_element = feed_it.get_next()

                sess.run(feed_it.initializer)
                while True:
                    try:
                        feed_dict = sess.run([next_element])
                        feed_dict = {self.batch_inputs: feed_dict[0][0],
                                     self.batch_labels:
                                     sess.run(
                                        tf.reshape(feed_dict[0][1], [-1, 1])
                                        )
                                     }
                        loss_val = 0
                        _,  loss_val = sess.run([self.optimizer, self.loss], feed_dict=feed_dict)

                        if step % 10 == 0:
                            s = sess.run(merged_summary, feed_dict=feed_dict)
                            self.logger.add_summary(s, step)

                        if step % 1000 == 0:
                            saver.save(sess, os.path.join(self.log_dir, "model.ckpt"), step)

                        step += 1
                    except tf.errors.OutOfRangeError:
                        break

                epoch_time = time() - t0
                loss = 0

            #done with training
            final_embeddings = self.normalized_embeddings.eval()
        return final_embeddings

def collect_ast(G, nodes):
    stack = []
    stack.extend(nodes)

    out = []

    while len(stack) > 0:
        act = stack.pop()
        out.append(act)

        for in_node, _, _, d in G.in_edges(act, keys=True, data='type'):
            if d is EdgeType.se:
                stack.append(in_node)

    return out

def is_ast_node(G, node):
    ast_node = True

    for out_node, _, _, d in G.out_edges(node, keys=True, data='type'):
        ast_node &= d is EdgeType.se

    for out_node, _, _, d in G.in_edges(node, keys=True, data='type'):
        ast_node &= d is EdgeType.se

    return ast_node

class WVGraphSentenceTask(Task):
    out_dir = Parameter('./w2v/sentences/')

    def __init__(self, name, h, D):
        self.name = name
        self.h = h
        self.D = D

    def require(self):
        return PrepareKernelTask(self.name, self.h, self.D)

    def output(self):
        path = self.out_dir.value + self.__taskid__() + '.txt'
        return FileTarget(path)

    def __taskid__(self):
        return 'W2VGraphSentence_%s_%d_%d' % (self.name, self.h, self.D)

    def run(self):
        with self.input()[0] as i:
            G = i.query()

        L = []

        with self.output() as output:
            for node in G:

                in_nodes = []
                ast_nodes = []

                for in_node, _, _, d in G.in_edges(node, keys=True, data='type'):
                    if d is EdgeType.se:
                        ast_nodes.append(in_node)
                    elif d is EdgeType.de:
                        in_nodes.append(in_node)

                in_nodes.extend(collect_ast(G, ast_nodes))

                if len(in_nodes) == 0:
                    continue

                in_nodes = [G.node[n]['label'] for n in in_nodes]

                output.write(
                    str(G.node[node]['label']) + ' ' + ' '.join(in_nodes)+'\n'
                )

class WVVocabulary(Task):
    out_dir = Parameter('./w2v/')

    def __init__(self, graph_list, length, h, D):
        self.graph_list = graph_list
        self.h = h
        self.D = D
        self.length = length

    def require(self):
        return [
            WVGraphSentenceTask(
                name,
                self.h,
                self.D
            )
            for name in self.graph_list
        ]

    def output(self):
        path = self.out_dir.value + self.__taskid__() + '.json'
        return CachedTarget(
            LocalTarget(path, service=JsonService)
        )

    def __taskid__(self):
        return 'W2VVocabulary_%d_%d_%d' % (self.h, self.D,
                                           containerHash(self.graph_list))

    def run(self):
        vocab = {}
        overall = 0
        for inp in self.input():
            with inp as i:
                for line in i.readlines():
                    for w in line.split():
                        if w not in vocab:
                            vocab[w] = 0
                        vocab[w] += 1
                    overall += 1
        vocab = [x for x in sorted(
                    list(vocab.items()), key=lambda x: x[1], reverse=True
                 )][:self.length]
        vocab = {k[0]: (v, k[1]) for v, k in enumerate(vocab)}

        print('### Parsed %s samples ###' % overall)

        with self.output() as o:
            o.emit(vocab)

class WVEmbeddingTask(Task):
    out_dir = Parameter('./w2v/')
    embedding_size = Parameter(10)
    learning_rate = Parameter(0.001)
    num_steps = Parameter(3)
    neg_sampling = Parameter(15)
    batch_size = Parameter(100)
    log_dir = Parameter('./log/embedded/')

    def __init__(self, graph_list, length, h, D):
        self.graph_list = graph_list
        self.h = h
        self.D = D
        self.length = length

    def require(self):
        out = [WVVocabulary(self.graph_list, self.length, self.h, self.D)]
        out.extend([
            WVGraphSentenceTask(
                name,
                self.h,
                self.D
            )
            for name in self.graph_list
        ])
        return out

    def output(self):
        path = self.out_dir.value + self.__taskid__() + '.json'
        return CachedTarget(
            LocalTarget(path, service=JsonService)
        )

    def __taskid__(self):
        return 'W2VEmbeddingTask_%d_%d_%d' % (self.h, self.D,
                                           containerHash(self.graph_list))

    def _get_vocab(self, vocab):
        vocab = [x[0] for x in
                 sorted(list(vocab.items()),
                        key=lambda v: v[1][0])]

        with open(os.path.join(self.log_dir.value, 'vocab.tsv'), 'w') as o:
            for v in vocab:
                o.write(v+'\n')

        return vocab

    def run(self):
        with self.input()[0] as i:
            vocab = i.query()
        inp = (self.input()[i] for i in range(1, len(self.input())))
        filenames = [f.sandBox + f.path for f in inp]

        unigrams = [x[1][1] for x in
                    sorted(list(vocab.items()),
                    key=lambda v: v[1][0])]

        model_skipgram = WVSkipgram(
            len(vocab),
            self.learning_rate.value,
            self.embedding_size.value,
            self.num_steps.value,
            self.neg_sampling.value,
            unigrams,
            self.log_dir.value
        )

        with tf.Session(graph=model_skipgram.graph,
                        config=tf.ConfigProto(log_device_placement=True,allow_soft_placement=False)) as sess:

            vocab_mapping = tf.constant(self._get_vocab(vocab))
            table = tf.contrib.lookup.index_table_from_tensor(
                                        mapping=vocab_mapping, num_oov_buckets=1,
                                        default_value=-1)

            def parse_mapping(line):
                line = tf.string_split([line], ' ').values
                line = table.lookup(line)
                label = line[0:1]
                features = line[1:]
                return features, tf.tile(label, [tf.shape(features)[0]])

            dataset = tf.data.TextLineDataset(filenames)
            dataset = dataset.map(parse_mapping)
            dataset = dataset.flat_map(lambda features, labels:
                                        tf.data.Dataset().zip((
                                          tf.data.Dataset().from_tensor_slices(features),
                                          tf.data.Dataset().from_tensor_slices(labels))
                                        ))
            dataset = dataset.shuffle(1000).batch(self.batch_size.value)

        embedding = model_skipgram.train(dataset)

        with self.output() as o:
            o.emit(embedding.tolist())

class WVSimilarWords(Task):
    out_dir = Parameter('./w2v/')

    def __init__(self, graph_list, length, h, D):
        self.graph_list = graph_list
        self.h = h
        self.D = D
        self.length = length

    def require(self):
        out = [WVVocabulary(self.graph_list, self.length, self.h, self.D),
               WVEmbeddingTask(self.graph_list, self.length,
                               self.h, self.D)]

        return out

    def output(self):
        path = self.out_dir.value + self.__taskid__() + '.json'
        return CachedTarget(
            LocalTarget(path, service=JsonService)
        )

    def __taskid__(self):
        return 'W2VSimilarWords_%d_%d_%d' % (self.h, self.D,
                                           containerHash(self.graph_list))

    def run(self):
        with self.input()[0] as i:
            vocab = i.query()
        with self.input()[1] as i:
            embedding = np.array(i.query())

        inv_vocab = [None]*len(vocab)
        for k, v in vocab.items():
            inv_vocab[v[0]] = k
        inv_vocab = inv_vocab

        dis = cdist(embedding, embedding, 'cosine')
        arg_sort = np.argsort(dis, axis=1)[:, 1:6]

        near = {}

        for i, k in enumerate(inv_vocab):
            row = arg_sort[i]
            near[k] = []
            for j in range(row.shape[0]):
                near[k].append([inv_vocab[row[j]], 1-dis[i, j]])

        with self.output() as o:
            o.emit(near)


import threading

import traitlets

import pyrosetta
import pyrosetta.rosetta.basic.options
import pyrosetta.rosetta.protocols.rosetta_scripts as rosetta_scripts
import pyrosetta.rosetta.protocols.moves as moves

import pyrosetta.distributed
import pyrosetta.distributed.tasks.taskbase as taskbase
import pyrosetta.distributed.packed_pose as packed_pose

def validate(protocol_xml):
    """Perform schema and parse validation for the given protocol xml."""
    try:
        test_task = BaseRosettaScriptsTask(protocol_xml)
        test_task.maybe_setup()
    except RuntimeError as error:
        raise error

class BaseRosettaScriptsTask(taskbase.TaskBase):
    @property
    @pyrosetta.distributed.requires_init
    @pyrosetta.distributed.with_lock
    def parser(self):
        if not getattr(self, "_parser", None):
            BaseRosettaScriptsTask._parser = \
                    rosetta_scripts.RosettaScriptsParser()

        return self._parser

    protocol_xml = traitlets.CUnicode()

    def __init__(self, protocol_xml):
        super().__init__(protocol_xml=protocol_xml)

    @pyrosetta.distributed.requires_init
    @pyrosetta.distributed.with_lock
    def setup(self):
        self.default_options = pyrosetta.rosetta.basic.options.process()
        self.tag = self.parser.create_tag_from_xml_string(
                self.protocol_xml, self.default_options)

        # Validate by parsing
        self.parser.parse_protocol_tag(self.tag, self.default_options)
        self.protocol_lock = threading.Lock()

    @property
    @pyrosetta.distributed.requires_init
    @pyrosetta.distributed.with_lock
    def parsed_protocol(self):
        return self.parser.parse_protocol_tag(self.tag, self.default_options)

    def execute(self, pack_or_pose):
        return packed_pose.to_packed(self.apply(pack_or_pose))

class MultioutputRosettaScriptsTask(BaseRosettaScriptsTask):
    @pyrosetta.distributed.requires_init
    def apply(self, pack_or_pose):
        """Apply task generating pose objects."""
        protocol = self.parsed_protocol

        wpose = packed_pose.to_pose(pack_or_pose)

        with self.protocol_lock:
            protocol.apply(wpose)

            if protocol.get_last_move_status() != moves.MoverStatus.MS_SUCCESS:
                return

            while wpose:
                yield wpose
                wpose = protocol.get_additional_output()

class SingleoutputRosettaScriptsTask(BaseRosettaScriptsTask):
    @pyrosetta.distributed.requires_init
    def apply(self, pack_or_pose):
        """Apply task returning a pose object."""
        protocol = self.parsed_protocol

        wpose = packed_pose.to_pose(pack_or_pose)

        with self.protocol_lock:
            protocol.apply(wpose)

            if protocol.get_last_move_status() != moves.MoverStatus.MS_SUCCESS:
                return
            else:
                return wpose


import PySimpleGUI as sg
import numpy as np
import cv2
import matplotlib.pyplot as plt
from Baysian_Mat import Bayesian_Matte
from PIL import Image, ImageOps
import time  # Execution TIme imports
import psutil

from laplac import Laplacianmatting
from compositing import compositing
from QualityTest import mse2d
from QualityTest import sad2d
from QualityTest import psnr2d
from smooth import smooth

# Import your Bayesian_Matte, Laplacianmatting, compositing, mse2d, sad2d, and psnr2d functions here

# Define the PySimpleGUI layout
layout = [
    [sg.Text("Select image file")],
    [sg.Input(key="-IMAGE_FILE-"), sg.FileBrowse()],
    [sg.Text("Select trimap file")],
    [sg.Input(key="-TRIMAP_FILE-"), sg.FileBrowse()],
    [sg.Text("Select GT file")],
    [sg.Input(key="-GT_FILE-"), sg.FileBrowse()],
    [sg.Button("Submit")],
    [sg.Output(size=(60, 2))]
]

# Create the PySimpleGUI window
window = sg.Window("Alpha Matte Calculation", layout)

# Start time for computing the execution time
st = time.time()

# Get initial memory usage
Memstart = psutil.Process().memory_info().rss / (1024 ** 2)

# Event loop
while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    if event == "Submit":
        # Get the file paths from the input fields
        image_path = values["-IMAGE_FILE-"]
        trimap_path = values["-TRIMAP_FILE-"]
        gt_path = values["-GT_FILE-"]

        # Read the image, trimap, and GT files
        image = np.array(Image.open(image_path))
        image_trimap = np.array(Image.open(trimap_path))
        GT = np.array(Image.open(gt_path))

        # Step 2 : Calculating Bayesian Matte for the given trimap
        alpha, pixel_count = Bayesian_Matte(image, image_trimap)

        # Step 3 : Making it back to range (0-255) for display purpose
        alpha_disp = alpha * 255
        alpha_int8 = np.array(alpha, dtype=int)

        et = time.time()
        elapsed_time = et - st

        # Step 4 : End to End testing - 1 : Calculating the Laplacian Matting
        Lalpha = Laplacianmatting(image, image_trimap)

        # Step 5 : Compositing Function Display
        background = np.array(Image.open(
            'C:/Users/aduttagu/Desktop/Main/Bayesian-Matting-Implementation/bayesian-Matting-Python/background.png'))
        comp_Bay = compositing(image, alpha_disp, background)

        # Step 6 : Smoothening ALpha Methods
        smooth_alpha = smooth(alpha_disp)

        # Step 7 : Displaying THe Bayesian, Laplacian and GT.
        fig, axes = plt.subplots(nrows=2, ncols=2)
        axes[0, 0].imshow(alpha_disp, cmap='gray')
        axes[0, 0].set_title('Bayesian - Alpha Matte')
        axes[0, 1].imshow(Lalpha, cmap='gray')
        axes[0, 1].set_title('Laplacian - Alpha Matte')
        axes[1, 0].imshow(GT, cmap='gray')
        axes[1, 0].set_title('Ground Truth')
        axes[1, 1].imshow(smooth_alpha, cmap='gray')
        axes[1, 1].set_title('Smoothed Alpha')
        plt.show()

        plt.imshow(comp_Bay)
        plt.show()

# Close the PySimpleGUI window
window.close()

# Part of End to End testing - 1 : Performance Comparision between Laplacian and Bayesian.
Bay_MSE = mse2d(alpha_disp, GT)
Lap_MSE = mse2d(Lalpha, GT)
print("The MSE between the Ground Truth and Bayesian Alpha Matte is :", Bay_MSE)
print("The MSE between the Ground Truth and Laplacian Alpha Matte is :", Lap_MSE)

Bay_SAD = sad2d(alpha_disp, GT)
Lap_SAD = sad2d(Lalpha, GT)
print("The SAD between the Ground Truth and Bayesian Alpha Matte is :", Bay_SAD)
print("The SAD between the Ground Truth and Laplacian Alpha Matte is :", Lap_SAD)

Bay_PSNR = psnr2d(alpha_disp, GT)
Lap_PSNR = psnr2d(Lalpha, GT)
print("The PSNR between the Ground Truth and Bayesian Alpha Matte is :", Bay_PSNR)
print("The PSNR between the Ground Truth and Laplacian Alpha Matte is :", Lap_PSNR)

print('Execution time for Bayesian Matting: {:.3f} seconds'.format(
elapsed_time))

# get usage after completion of code
Memend = psutil.Process().memory_info().rss / (1024 ** 2)
Memuse = Memend - Memstart
print("Total memory consumed in execution of this program : ", Memuse, "MB's")


from Sentence import Sentence
import nltk

class Text:

    def __init__(self, rawText, name):
        self.rawText = rawText#self.formatText(rawText)
        self.name = name

        splitAtNewlines = [s.strip() for s in rawText.splitlines()]
        rawSentences = []
        for line in splitAtNewlines:
            sentencesInLine = nltk.sent_tokenize(line)
            rawSentences.extend(sentencesInLine)

        self.sentences = []
        for rawSentence in rawSentences:
            sentence = Sentence(self, rawSentence)
            self.sentences.append(sentence)

    def formatText(self, rawText):
        return  rawText.replace(u"\u2018", "'").replace(u"\u2019", "'").replace(u"\xa9", "e").replace(u"\u2014","-").decode("utf8")


from matplotlib import pyplot as plt
font = {'family':'sans-serif', 'sans-serif':'Arial'}
plt.rc('font', **font)

plt.title('', fontsize='x-large', pad=None)
plt.xlabel('', fontsize='x-large')
plt.ylabel('', fontsize='x-large')
# plt.xscale('log')
plt.tick_params(axis="both",direction="in", labelsize='x-large')
plt.subplots_adjust(left=0.30, bottom=0.30, right=0.70, top=0.70, wspace=0.20, hspace=0.20)
plt.legend(fontsize='large').set_draggable(True)
plt.grid(alpha=0.5)


import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import service

#driver = webdriver.Chrome(executable_path="G:\driver\chromedriver_win32\chromedriver.exe") #my computer
driver=webdriver.Chrome()
driver.implicitly_wait(40)
driver.get("https://www.facebook.com/")
driver.find_element(By.XPATH,'//a[@role="button"and@data-testid="open-registration-form-button"]').click()
time.sleep(2)
driver.find_element(By.XPATH,'//input[@name="firstname"and@aria-label="First name"]').send_keys('pavithran')
time.sleep(2)
driver.find_element(By.XPATH,'//input[@name="lastname"and@aria-label="Surname"]').send_keys('sethu')
time.sleep(2)
driver.find_element(By.XPATH,'//input[@aria-label="Mobile number or email address"]').send_keys('9784561524')
driver.find_element(By.XPATH,'//input[@id="password_step_input"and@type="password"]').send_keys('Passcode')
time.sleep(2)
print("--------days-------")
days_elements=driver.find_element(By.ID,"day")#assign the id
days=Select(days_elements)#selecting the all elements
#giving the values manually to the dropdownlist
days.select_by_visible_text("17")#text method
time.sleep(2)
days.select_by_index(2)#index method
time.sleep(2)
days.select_by_value("6")#value method
time.sleep(2)
days_elements.send_keys("25")#send my value to days dropdown box NORMAL METHOD
print("get attribute method the value sent to the dropbox:",days_elements.get_attribute('value')) #get my value from dropbox
time.sleep(2)
totaloptions=len(days.options)#to find total options available in days
print("Total options in day dropdownlist:",totaloptions)#31 options are there
opsd=days.options#to get all options
print("total options")#just for heading
for option in opsd:#for loop
    print("option text is-{}-option value is={}".format(option.text,option.get_attribute("value")))
print("--using range--")
for x in range(0,30):
    print(opsd[x].text)
print("--days after 20th\n--")
for x in opsd:
    y=x.get_attribute("value")
    z=int(y)
    if z>=20:
        print(x.text)

print("--days between 10 to 25\n--")
for x in opsd:
    y=x.get_attribute("value")
    z=int(y)
    if z>=10 and z<=25:
         print(x.text)

print('-----month-----')
#month

month_element=driver.find_element(By.ID,'month')
months=Select(month_element)
months.select_by_value("2")#feb
time.sleep(2)
months.select_by_index(4)
time.sleep(2)
months.select_by_visible_text("Aug")
month_length=len(months.options)
print("total months options are available in facebook\n:",month_length)
ops=months.options
for option in ops:
   print("option text is-{}-option value is={}".format(option.text, option.get_attribute("value")))
#using range printing text
print("--using range--")
for x in range(0,12):
    print(ops[x].text)

print("----last 3 months---\n")
for x in ops:
    y=(x.get_attribute('value'))
    z=int(y)
    if z>=10:
        print(x.text)
print("----between months:----\n")
for x in ops:
    y=(x.get_attribute('value'))
    z=int(y)
    if z>=2 and z<=10:
        print(x.text)
print("---1st 3 months\n---")
for x in ops:
    y=(x.get_attribute('value'))
    z=int(y)
    if z<=3:
        print(x.text)

print("-------year--------")
year_elements=driver.find_element(By.ID,"year")
years=Select(year_elements)
years.select_by_visible_text("1997")
time.sleep(3)
years.select_by_value("1996")
time.sleep(3)
years.select_by_index(1)#2021
totalyears=len(years.options)
print("total no of options in year:",totalyears)#118
opsy=years.options
for x in opsy:
   print("year is={} year value is={}".format(x.text,x.get_attribute("value")))
print("--using range--")
for x in range(0,30):
    print(opsy[x].text)
print("--years above 1997\n--")
for x in opsy:
    y=x.get_attribute("value")
    z=int(y)
    if z>=1997:
        print(x.text)
print("--years between 2000 to 1990\n--")
for x in opsy:
    y=x.get_attribute("value")
    z=int(y)
    if z<=2000 and z>=1990:
        print(x.text)

print(type(y))
print(type(z))

#gender selection
gender_f=driver.find_element(By.XPATH,'(//input[@type="radio"and@name="sex"])[1]').click()
status=driver.find_element(By.XPATH,'(//input[@type="radio"and@name="sex"])[1]').is_selected()
print(status)
time.sleep(3)
gender_m=driver.find_element(By.XPATH,'(//input[@type="radio"and@name="sex"])[2]').click()
status=driver.find_element(By.XPATH,'(//input[@type="radio"and@name="sex"])[2]').is_selected()
print(status)
time.sleep(3)
gender_c=driver.find_element(By.XPATH,'(//input[@type="radio"and@name="sex"])[3]').click()
status=driver.find_element(By.XPATH,'(//input[@type="radio"and@name="sex"])[3]').is_selected()
print(status)
custom=driver.find_element(By.XPATH,'//select[@aria-label="Select your pronoun"]')
custom_s=Select(custom)
custom_s.select_by_value("1")
time.sleep(2)
custom_s.select_by_value("2")
time.sleep(2)
customs=custom_s.options
for x in customs:
    print(x.text)
driver.find_element(By.XPATH,'//input[@name="custom_gender"]').send_keys("they")

driver.find_element(By.XPATH,'//button[text()="Sign Up"]').click()
time.sleep(5)
driver.close()


from django.test import TestCase
from car import models

class ModelTest(TestCase):

    def test_create_user_with_email_successful(self):
        """Test creating a new car is successful"""
        category = 'CO'
        model = "TT RS 2020"
        name = 'Audi TT RS TURBO'
        number_of_doors = 3
        description = 'This car is a beast'
        car = models.Car.objects.create(
            category=category,
            model=model,
            name=name,
            number_of_doors=number_of_doors,
            description=description
        )

        self.assertEqual(car.category, category)
        self.assertEqual(car.model, model)
        self.assertEqual(car.name, name)
        self.assertEqual(car.number_of_doors, number_of_doors)
        self.assertEqual(car.description, description)


import os

import pytest

from dvclive.data.scalar import Scalar
from dvclive.keras import DvcLiveCallback
from tests.test_main import read_logs

# pylint: disable=unused-argument, no-name-in-module, redefined-outer-name

@pytest.fixture
def xor_model():
    import numpy as np
    from tensorflow.python.keras import Sequential
    from tensorflow.python.keras.layers import Activation, Dense

    def make():
        x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
        y = np.array([[0], [1], [1], [0]])

        model = Sequential()
        model.add(Dense(8, input_dim=2))
        model.add(Activation("relu"))
        model.add(Dense(1))
        model.add(Activation("sigmoid"))

        model.compile(
            loss="binary_crossentropy", optimizer="sgd", metrics=["accuracy"]
        )

        return model, x, y

    yield make

def test_keras_callback(tmp_dir, xor_model, capture_wrap):
    model, x, y = xor_model()

    model.fit(
        x,
        y,
        epochs=1,
        batch_size=1,
        validation_split=0.2,
        callbacks=[DvcLiveCallback()],
    )

    assert os.path.exists("dvclive")
    logs, _ = read_logs(tmp_dir / "dvclive" / Scalar.subfolder)

    assert os.path.join("train", "accuracy") in logs
    assert os.path.join("eval", "accuracy") in logs

@pytest.mark.parametrize("save_weights_only", (True, False))
def test_keras_model_file(
    tmp_dir, xor_model, mocker, save_weights_only, capture_wrap
):
    model, x, y = xor_model()
    save = mocker.spy(model, "save")
    save_weights = mocker.spy(model, "save_weights")

    model.fit(
        x,
        y,
        epochs=1,
        batch_size=1,
        callbacks=[
            DvcLiveCallback(
                model_file="model.h5", save_weights_only=save_weights_only
            )
        ],
    )
    assert save.call_count != save_weights_only
    assert save_weights.call_count == save_weights_only

@pytest.mark.parametrize("save_weights_only", (True, False))
def test_keras_load_model_on_resume(
    tmp_dir, xor_model, mocker, save_weights_only, capture_wrap
):
    import dvclive.keras

    model, x, y = xor_model()

    if save_weights_only:
        model.save_weights("model.h5")
    else:
        model.save("model.h5")

    load_weights = mocker.spy(model, "load_weights")
    load_model = mocker.spy(dvclive.keras, "load_model")

    model.fit(
        x,
        y,
        epochs=1,
        batch_size=1,
        callbacks=[
            DvcLiveCallback(
                model_file="model.h5",
                save_weights_only=save_weights_only,
                resume=True,
            )
        ],
    )

    assert load_model.call_count != save_weights_only
    assert load_weights.call_count == save_weights_only

def test_keras_no_resume_skip_load(tmp_dir, xor_model, mocker, capture_wrap):
    model, x, y = xor_model()

    model.save_weights("model.h5")

    load_weights = mocker.spy(model, "load_weights")

    model.fit(
        x,
        y,
        epochs=1,
        batch_size=1,
        callbacks=[
            DvcLiveCallback(
                model_file="model.h5",
                save_weights_only=True,
                resume=False,
            )
        ],
    )

    assert load_weights.call_count == 0

def test_keras_no_existing_model_file_skip_load(
    tmp_dir, xor_model, mocker, capture_wrap
):
    model, x, y = xor_model()

    load_weights = mocker.spy(model, "load_weights")

    model.fit(
        x,
        y,
        epochs=1,
        batch_size=1,
        callbacks=[
            DvcLiveCallback(
                model_file="model.h5",
                save_weights_only=True,
                resume=True,
            )
        ],
    )

    assert load_weights.call_count == 0

def test_keras_None_model_file_skip_load(
    tmp_dir, xor_model, mocker, capture_wrap
):
    model, x, y = xor_model()

    model.save_weights("model.h5")

    load_weights = mocker.spy(model, "load_weights")

    model.fit(
        x,
        y,
        epochs=1,
        batch_size=1,
        callbacks=[
            DvcLiveCallback(
                save_weights_only=True,
                resume=True,
            )
        ],
    )

    assert load_weights.call_count == 0


from django.test import TestCase

from costcenter.forms import FundForm

class FundFormTest(TestCase):
    def test_empty_form(self):
        form = FundForm()
        self.assertIn("fund", form.fields)
        self.assertIn("name", form.fields)
        self.assertIn("vote", form.fields)
        self.assertIn("download", form.fields)

        # test just one rendered field
        self.assertInHTML(
            '<input type="text" name="fund" maxlength="4" required id="id_fund">',
            str(form),
        )

    def test_filled_form(self):
        data = {"fund": "C119", "name": "National Procurement", "vote": 1, "download": True}
        f = FundForm(data=data)
        self.assertTrue(f.is_valid())

    def test_vote_not_1_or_5(self):
        data = {"fund": "C113", "name": "NP", "vote": "6", "download": 1}
        form = FundForm(data=data)

        self.assertEqual(form.errors["vote"], ["Vote must be 1 or 5"])

    def test_fund_starts_with_non_letter(self):
        data = {"fund": "3113"}
        form = FundForm(data=data)

        self.assertEqual(form.errors["fund"], ["Fund must begin with a letter"])

    def test_fund_is_not_4_characters_long(self):
        data = {"fund": "c3456"}
        form = FundForm(data=data)
        msg = f"Ensure this value has at most 4 characters (it has {len(data['fund'])})."
        self.assertEqual(form.errors["fund"], [msg])


def solution(s):
    from collections import deque
    answer = ''
    s = deque(s)
    while s:
        a = s.popleft()
        if answer:
            if answer[-1] == ' ':
                answer += a.upper()
            else:
                answer += a.lower()
        else:
            answer += a.upper()
    return answer

프로그래머스/lv2/12951. JadenCase 문자열 만들기/JadenCase 문자열 만들기.py


import json
import random

while True:
    inp = input("> ")
    ints = {}
    with open('intents.json', 'r') as f:
        json.dump(f, ints)
    try:
        if ints[inp].type() == list:
            val = random.choice(ints[inp])
        else:
            val = ints[inp]
        print(val)
    except:
        print("I don't understand.")


from django.urls import path

from Zoo import views
import templates

urlpatterns = [
    path('login/', views.user_login, name='login'),
    path('logout/',views.logout, name='logout'),
    path('user_create/', views.user_create, name='user_create'),
    path('index/', views.index, name='index'),
    path('detail/<int:id>', views.animal_detail, name='animal_detail'),
    path('animal_delete/<int:id>/', views.animal_delete, name='animal_delete'),
    path('check/<int:id>', views.check, name='check'),
    path('search/', views.search, name='search'),
    path('search_filter/', views.search_filter, name='search_filter'),
    path('write_log/<int:id>/', views.write_log, name='write_log'),
    path('edit_log/<int:id>/', views.edit_log, name='edit_log'),
    path('log_delete/<int:id>/', views.log_delete, name='log_delete'),
    path('zone/<int:id>', views.zone, name='zone'),
]


import time
from selenium.webdriver.support.ui import Select
from selenium import webdriver

class InventoryPage():
    def __init__(self,driver) :
        self.driver = driver

    def navigate(self, urlLogin):
        self.driver.get(urlLogin)

    def changeSorting(self, locatorClass, option):
        self.sel = Select (self.driver.find_element_by_class_name (locatorClass))
        self.sel.select_by_value (option)

    def check_A_to_Z_sort(self):
        items_names = self.driver.find_elements_by_class_name("inventory_item_name")
        for name in items_names:
            name_text=name.text
            print(name_text)
            names_list=[]
            names_list.append(name_text)
        sorted_names = sorted(names_list)
        if(names_list == sorted_names):
            print("'A_to_Z' sorting working ")
        else:
            print("'A_to_Z' sorting not working")

    def check_Z_to_A_sort(self):
        items_names = self.driver.find_elements_by_class_name("inventory_item_name")
        for name in items_names:
            name_text=name.text
            print(name_text)
            names_list=[]
            names_list.append(name_text)
        sorted_names = sorted(names_list)
        reversed_names = sorted_names.reverse()
        if(names_list == reversed_names):
            print("'Z_to_A' sorting working ")
        else:
            print("'Z_to_A' sorting not working")

    def check_low_to_high_sort(self):
        items_prices = self.driver.find_elements_by_class_name ("inventory_item_price")
        for price in items_prices:
            price_text=price.text
            price_text = price_text.replace('$','')
            value = float(price_text)
            prices_values=[]
            prices_values.append(value)
        sorted_prices = sorted(prices_values)
        if(prices_values == sorted_prices):
            print("'low_to_high' sorting working ")
        else:
            print("'low_to_high' sorting not working")

    def check_high_to_low_sort(self):
        items_prices = self.driver.find_elements_by_class_name ("inventory_item_price")
        for price in items_prices:
            price_text=price.text
            price_text = price_text.replace('$','')
            value = float(price_text)
            prices_values=[]
            prices_values.append(value)
        sorted_prices = sorted(prices_values)
        reversed_prices = sorted_prices.reverse()
        if(prices_values == reversed_prices):
            print("'high_to_low' sorting working ")
        else:
            print("'high_to_low' sorting not working")

    def click_item_page_and_verify(self,item_full_id):
        self.driver.find_element_by_id(item_full_id).click()
        item_id = item_full_id[5]
        currentURL = self.driver.current_url
        assert currentURL == "https://www.saucedemo.com/inventory-item.html?id=" + str(item_id)
        print("item page " + str(item_id)+" opened")

    def click_item_to_cart_and_verify(self,item_id):
        self.driver.find_element_by_id(item_id).click()
        item_shopped = self.driver.find_element_by_class_name("shopping_cart_badge")
        assert int(item_shopped.text) == 1
        self.driver.find_element_by_class_name("shopping_cart_badge").click()
        time.sleep(2)
        self.driver.find_element_by_id("checkout").click()
        time.sleep(2)
        currentURL = self.driver.current_url
        assert currentURL == "https://www.saucedemo.com/checkout-step-one.html"
        print("check out page opened")


from sklearn.cluster import KMeans
import numpy as np
import matplotlib.pyplot as plt

# X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
X = np.array(np.random.random((100, 2)))
kmeans = KMeans(n_clusters=2).fit(X)
print('Labels')
print(kmeans.labels_)
result = kmeans.predict([[0, 0], [12, 3]])
print('result')
print(result)
print('clusters')
print(kmeans.cluster_centers_)

plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_)
plt.xlabel("X")
plt.ylabel("Y")
plt.show()


from collections import Counter
from itertools import product
from operator import add

def solve(lines, cycles, dimensions):
    board = set()
    for row, line in enumerate(lines):
        for col, elem in enumerate(line):
            if elem == '#':
                cell = dimensions * [0,]
                cell[0], cell[1] = col, row
                board.add(tuple(cell))

    for _ in range(cycles):
        new_board = set()

        neighbour_counts = Counter()
        for cell in board:
            for delta in product(range(-1, 2), repeat=dimensions):
                if delta != dimensions * (0,):
                    neighbour_counts[tuple(map(add, cell, delta))] += 1

        for cell, count in neighbour_counts.items():
            if count == 3 or (cell in board and count == 2):
                new_board.add(cell)
        board = new_board

    return len(board)

with open('input.txt') as file:
    lines = file.read().splitlines()

print(solve(lines, 6, 3))
print(solve(lines, 6, 4))


import collections

from .pybeesgrid import TAG_SIZE, NUM_CONFIGS, NUM_MIDDLE_CELLS
from .pybeesgrid import GridGenerator, BadGridArtist, BlackWhiteArtist, \
    MaskGridArtist, DepthMapArtist
from .pybeesgrid import drawGrids

from .pybeesgrid import INNER_BLACK_SEMICIRCLE, CELL_0_BLACK, CELL_1_BLACK, \
    CELL_2_BLACK, CELL_3_BLACK, CELL_4_BLACK, CELL_5_BLACK, CELL_6_BLACK, \
    CELL_7_BLACK, CELL_8_BLACK, CELL_9_BLACK, CELL_10_BLACK, CELL_11_BLACK, \
    IGNORE, CELL_0_WHITE, CELL_1_WHITE, CELL_2_WHITE, CELL_3_WHITE,  \
    CELL_4_WHITE, CELL_5_WHITE, CELL_6_WHITE, CELL_7_WHITE, CELL_8_WHITE, \
    CELL_9_WHITE, CELL_10_WHITE, CELL_11_WHITE, OUTER_WHITE_RING, \
    INNER_WHITE_SEMICIRCLE

import numpy as np
import warnings

TAG_ID = ['bits']
TAG_CONFIG = ['z_rotation', 'y_rotation', 'x_rotation', 'center', 'radius']
TAG_STRUCTURE = ['inner_ring_radius', 'middle_ring_radius', 'outer_ring_radius', 'bulge_factor',
                 'focal_length']

TAG_LABEL_NAMES = TAG_ID + TAG_CONFIG + TAG_STRUCTURE

CONFIG_LABELS = ('z_rotation', 'y_rotation', 'x_rotation',
                 'center_x', 'center_y', 'radius')

CONFIG_ROTS = (
    CONFIG_LABELS.index('z_rotation'),
    CONFIG_LABELS.index('y_rotation'),
    CONFIG_LABELS.index('x_rotation'),
)

CONFIG_CENTER = (
    CONFIG_LABELS.index('center_x'),
    CONFIG_LABELS.index('center_y'),
)

CONFIG_RADIUS = CONFIG_LABELS.index('radius')

MASK = collections.OrderedDict([
    ("INNER_BLACK_SEMICIRCLE", INNER_BLACK_SEMICIRCLE),
    ("CELL_0_BLACK",           CELL_0_BLACK),
    ("CELL_1_BLACK",           CELL_1_BLACK),
    ("CELL_2_BLACK",           CELL_2_BLACK),
    ("CELL_3_BLACK",           CELL_3_BLACK),
    ("CELL_4_BLACK",           CELL_4_BLACK),
    ("CELL_5_BLACK",           CELL_5_BLACK),
    ("CELL_6_BLACK",           CELL_6_BLACK),
    ("CELL_7_BLACK",           CELL_7_BLACK),
    ("CELL_8_BLACK",           CELL_8_BLACK),
    ("CELL_9_BLACK",           CELL_9_BLACK),
    ("CELL_10_BLACK",          CELL_10_BLACK),
    ("CELL_11_BLACK",          CELL_11_BLACK),
    ("IGNORE",                 IGNORE),
    ("CELL_0_WHITE",           CELL_0_WHITE),
    ("CELL_1_WHITE",           CELL_1_WHITE),
    ("CELL_2_WHITE",           CELL_2_WHITE),
    ("CELL_3_WHITE",           CELL_3_WHITE),
    ("CELL_4_WHITE",           CELL_4_WHITE),
    ("CELL_5_WHITE",           CELL_5_WHITE),
    ("CELL_6_WHITE",           CELL_6_WHITE),
    ("CELL_7_WHITE",           CELL_7_WHITE),
    ("CELL_8_WHITE",           CELL_8_WHITE),
    ("CELL_9_WHITE",           CELL_9_WHITE),
    ("CELL_10_WHITE",          CELL_10_WHITE),
    ("CELL_11_WHITE",          CELL_11_WHITE),
    ("OUTER_WHITE_RING",       OUTER_WHITE_RING),
    ("INNER_WHITE_SEMICIRCLE", INNER_WHITE_SEMICIRCLE)
])

MASK_KEYS = list(MASK.keys())
CELLS_BLACK = MASK_KEYS[MASK_KEYS.index("CELL_0_BLACK"):MASK_KEYS.index("CELL_11_BLACK")+1]
MASK_BLACK = ["INNER_BLACK_SEMICIRCLE"] + CELLS_BLACK
CELLS_WHITE = MASK_KEYS[
              MASK_KEYS.index("CELL_0_WHITE"):
              MASK_KEYS.index("CELL_11_WHITE")+1]

MASK_WHITE = CELLS_WHITE + ["OUTER_WHITE_RING", "INNER_WHITE_SEMICIRCLE"]

def dtype_tag_params(nb_bits=12, with_structure=False):
    keys = TAG_ID + TAG_CONFIG
    if with_structure:
        keys += TAG_STRUCTURE
    reps = {key: 1 for key in keys}
    reps['bits'] = nb_bits
    reps['center'] = 2
    return [(key, "({},)float32".format(n)) for key, n in reps.items()]

def draw_grids(params, with_structure='auto', scales=[1.], artist=None):
    def get_positions(keys):
        positions = {}
        i = 0
        for name in keys:
            positions[name] = i
            i += len(params[name][0])
        return positions, i

    def array_fill_by_keys(struct_arr, keys, positions, arr):
        for name in keys:
            b = positions[name]
            e = b + len(struct_arr[name][0])
            arr[:, b:e] = struct_arr[name]

    if artist is None:
        artist = BlackWhiteArtist(0, 255, 0, 1)

    batch_size = len(params['bits'])
    positions, size = get_positions(TAG_ID + TAG_CONFIG)
    bits_and_config = np.zeros((batch_size, size), dtype=np.float32)
    array_fill_by_keys(params, TAG_ID + TAG_CONFIG, positions, bits_and_config)

    if with_structure == 'auto':
        with_structure = all([struct_key in params.dtype.names for struct_key in TAG_STRUCTURE])
    if with_structure:
        struct_positions, struct_size = get_positions(TAG_STRUCTURE)
        structure = np.zeros((batch_size, struct_size), dtype=np.float32)
        array_fill_by_keys(params, TAG_STRUCTURE, struct_positions, structure)
        structure = np.ascontiguousarray(structure)
    else:
        structure = None
    bits_and_config = np.ascontiguousarray(bits_and_config)
    if structure is not None and (structure == 0).all():
        warnings.warn(
            "draw_grids got a structure that is all zero. Did you use "
            "`dtype_tag_params(with_structure=True)`"
            " and forgot to set the structure?")

    assert bits_and_config.dtype == np.float32
    assert bits_and_config.flags['C_CONTIGUOUS']
    return drawGrids(bits_and_config, structure, artist, scales)

def _normalize_angle(x):
    x %= 2*np.pi
    x = (x + 2*np.pi) % (2*np.pi)
    x[x > np.pi] -= 2*np.pi
    assert ((-np.pi <= x) & (x <= np.pi)).all()
    return x


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

from django_webtest import DjangoTestApp, WebTestMixin
import pytest

from testapp.articles.factories import AuthorFactory, ArticleFactory, TeamFactory

@pytest.fixture(scope='function')
def app(request):
    wtm = WebTestMixin()
    wtm._patch_settings()
    wtm._disable_csrf_checks()
    request.addfinalizer(wtm._unpatch_settings)
    return DjangoTestApp()

@pytest.fixture(scope='function')
def data(request):
    teams = [
        TeamFactory()
        for x in range(0, 2)
    ]
    authors = [
        AuthorFactory(team=team)
        for team in teams
        for x in range(0, 5)
    ]
    articles = [
        ArticleFactory(author=author)
        for author in authors
        for x in range(0, 10)
    ]
    return {
        'teams': teams,
        'authors': authors,
        'articles': articles,
    }


from django.http import  JsonResponse

from base.views import chekctoken

WHITE_URLS = ( '/apis/login/')

class RequestMideleware(object):
    def process_request(self, request):
        if request.path_info in WHITE_URLS:
            return
        try:
            ret = chekctoken(request)
            if not ret:
                response =JsonResponse({'result': 'Unauthorized'})
                response.status_code = 401
                return response
        except:
            return


import os
import pandas as pd
from scipy.io import loadmat

def load_data():
    list_of_files = os.listdir("data\\Identification\\MFCC\\")
    cumulative_df = pd.DataFrame()
    for file in list_of_files:
        data_set = loadmat("data\\Identification\\MFCC\\" + file)
        features = data_set['feat']
        labels = data_set['Y']
        features_df = pd.DataFrame(features)
        labels_df = pd.DataFrame(labels, columns=["Subject", "Session"])
        combined_df = pd.concat([features_df, labels_df], axis=1)
        cumulative_df = pd.concat(
            [cumulative_df, combined_df]).sort_values(by="Subject")
    return cumulative_df

def load_file(filename):
    data_set = loadmat("data\\Identification\\MFCC\\" + str(filename))
    features = data_set['feat']
    labels = data_set['Y']
    features_df = pd.DataFrame(features)
    labels_df = pd.DataFrame(labels, columns=["Subject", "Session"])
    combined_df = pd.concat([features_df, labels_df], axis=1)
    return combined_df