import  json

# number=[2,3,4,5,6,7,8]
# file_name="number_json"
# # with open(file_name,'w') as f:
#     # json.dump(number,f)
#
# with open(file_name) as f:
#     number=json.load(f)
# print(number)

# 保存和读取用户生成的数据

# uesename=input('What is your name？按回车结束')
filename='username.json'
# with open(filename,'a') as f:
#     json.dump(uesename,f)
#     print(f"Well remeber you when you come back,{uesename}\n")

with  open(filename) as f:
    aaa=json.load(f)
    print(aaa)


from django.http import HttpResponse
from django.shortcuts import render
from .forms import UploadFileForm, ShowDataForm
from .models import Grado, Grado_rp30
import csv, io

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            grado = form.cleaned_data['grado']
            curso = form.cleaned_data['curso']
            csv_file1 = request.FILES['f_matriculados']
            csv_file2 = request.FILES['f_abandono']
            if not form.cleaned_data['f_rp30']:
                datos =  read_csv(csv_file1, csv_file2)
                RP30 = None
            else:
                csv_file3 = request.FILES['f_rp30']
                datos =  read_csv(csv_file1, csv_file2, csv_file3)
                RP30 = 'active'
            create_table(grado, curso, datos[0], datos[1], RP30)
            return HttpResponse('
Archivos subidos con éxito.
')
    else:
        form = UploadFileForm()
    return render(request, 'subir_datos.html', {'form': form})

def get_key(val, diccionario):
    for key, value in diccionario.items():
         if val == value:
             return key

def read_csv(f1, f2, f3=None):
    Alumnos = []
    data_matriculados = f1.read().decode('UTF-8')
    data_abandono = f2.read().decode('UTF-8')
    io_matriculados = io.StringIO(data_matriculados)
    io_abandono = io.StringIO(data_abandono)
    cols_alumnos = dict(enumerate(next(csv.reader(io_matriculados))))
    cols_abandono = dict(enumerate(next(csv.reader(io_abandono))))

    if f3 != None:
        data_rp30 = f3.read().decode('UTF-8')
        io_rp30 = io.StringIO(data_rp30)
        cols_rp30 = dict(enumerate(next(csv.reader(io_rp30))))
        lista_rp30 = [row for row in csv.reader(io_rp30)]

    lista_datos = [row for row in csv.reader(io_matriculados)]
    lista_abandono = [row for row in csv.reader(io_abandono)]

    lista_dni = list(dict.fromkeys([str(lista[get_key('Dni', cols_alumnos)]) for lista in lista_datos]))
    lista_cursos = list(dict.fromkeys([lista[get_key('Curso acta', cols_alumnos)] for lista in lista_datos]))
    lista_asignaturas = list(dict.fromkeys([lista[get_key('Asignatura', cols_alumnos)+1] for lista in lista_datos]))

    lista_cursos.sort()
    exp_1 = 'MAT_' + str(int(lista_cursos[0][2:].replace('-','')) + 101) + '_SN'
    exp_2 = 'MAT_' + str(int(lista_cursos[0][2:].replace('-','')) + 202) + '_SN'

    tam = len(lista_datos)
    i = 0
    for dni in lista_dni:
        dict_alu = {'Sexo': lista_datos[i][get_key('Sexo', cols_alumnos)],
                    'Edad': lista_datos[i][get_key('Edad año ingreso al estudio', cols_alumnos)],
                    'Procedencia': lista_datos[i][get_key('Isla procedencia', cols_alumnos)],
                    'Becado': lista_datos[i][get_key('Becario', cols_alumnos)],
                    'Convocatoria': lista_datos[i][get_key('Conv. Preinsc.', cols_alumnos)],
                    'Modalidad': lista_datos[i][get_key('Modalidad', cols_alumnos)],
                    'Especialidad': lista_datos[i][get_key('Especialidad', cols_alumnos)+1]
                    }
        # Trabajo:
        dict_alu['Trabajo'] = 'No' if lista_datos[i][get_key('Ocpación alumno', cols_alumnos)] == 'No aplica' or lista_datos[i][get_key('Ocpación alumno', cols_alumnos)] == 'No consta' else 'Si'
        # Preferencia
        dict_alu['Preferencia'] = int(lista_datos[i][get_key('Preferencia', cols_alumnos)]) if lista_datos[i][get_key('Preferencia', cols_alumnos)] != '' else None
        # Nota Bach/Ciclo
        dict_alu['Nota_Bach'] = float(lista_datos[i][get_key('Nota Bach / Ciclo', cols_alumnos)].replace(',','.')) if lista_datos[i][get_key('Nota Bach / Ciclo', cols_alumnos)] != '' else None
        # Nota Prueba (Ebau)
        dict_alu['Nota_prueba'] = float(lista_datos[i][get_key('Nota prueba', cols_alumnos)].replace(',','.')) if lista_datos[i][get_key('Nota prueba', cols_alumnos)] != '' else None
        # Nota específica (Ebau)
        dict_alu['Nota_esp'] = float(lista_datos[i][get_key('Nota Específica', cols_alumnos)].replace(',','.')) if lista_datos[i][get_key('Nota Específica', cols_alumnos)] != '' else None
        # Nota admisión
        dict_alu['Admision'] = float(lista_datos[i][get_key('Nota admisión', cols_alumnos)].replace(',','.')) if lista_datos[i][get_key('Nota admisión', cols_alumnos)] != '' else None

        for sublist in lista_abandono:
            if sublist[get_key('Dni', cols_abandono)] == dni:
                if sublist[get_key(exp_1, cols_abandono)] == 'N' and sublist[get_key(exp_2, cols_abandono)] == 'N':
                    dict_alu['Abandono'] = 'Si'
                else:
                    dict_alu['Abandono'] = 'No'

        if f3 != None:
            dict_alu['aciertos'] = None
            dict_alu['errores'] = None
            dict_alu['total'] = None
            for sublist in lista_rp30:
                if sublist[get_key('DNI', cols_rp30)] == dni:
                    dict_alu['aciertos'] = int(sublist[get_key('ACIERTOS', cols_rp30)]) if sublist[get_key('ACIERTOS', cols_rp30)] != '' else None
                    dict_alu['errores'] = int(sublist[get_key('ERRORES', cols_rp30)]) if sublist[get_key('ERRORES', cols_rp30)] != '' else None
                    dict_alu['total'] = int(sublist[get_key('TOTAL PRUEBA', cols_rp30)]) if sublist[get_key('TOTAL PRUEBA', cols_rp30)] != '' else None

        contador = 0
        lista_curso1 = []
        lista_curso2 = []
        while dni == lista_datos[i][get_key('Dni', cols_alumnos)]:
            curso = lista_datos[i][get_key('Curso acta', cols_alumnos)]
            asignaturas_matriculadas = 0
            asignaturas_presentadas = 0
            asignaturas_aprobadas = 0

            while curso == lista_datos[i][get_key('Curso acta', cols_alumnos)] and dni == lista_datos[i][get_key('Dni', cols_alumnos)]:
                asignaturas_matriculadas += 1
                n_asignatura = lista_datos[i][get_key('Asignatura', cols_alumnos)+1]
                notas = []
                while lista_datos[i][get_key('Asignatura', cols_alumnos)+1] == n_asignatura:
                    if lista_datos[i][get_key('Calificación', cols_alumnos)] != 'NP':
                        notas.append(float(lista_datos[i][get_key('Calif. Numérica', cols_alumnos)].replace(',','.')))
                    i += 1
                    if i == tam: break
                if notas:
                    asignaturas_presentadas += 1
                    if max(notas) >= 5.0:
                        asignaturas_aprobadas += 1
                    lista_curso1.append([n_asignatura, max(notas)]) if curso == lista_cursos[0] else lista_curso2.append([n_asignatura, max(notas)])
                else:
                    lista_curso1.append([n_asignatura, None]) if curso == lista_cursos[0] else lista_curso2.append([n_asignatura, None])
                if i == tam: break
            contador += 1
            if contador == 1:
                dict_alu['asignaturas_matriculadas_primero'] = asignaturas_matriculadas
                dict_alu['asignaturas_presentadas_primero'] = asignaturas_presentadas
                dict_alu['asignaturas_aprobadas_primero'] = asignaturas_aprobadas
            else:
                dict_alu['asignaturas_matriculadas_segundo'] = asignaturas_matriculadas
                dict_alu['asignaturas_presentadas_segundo'] = asignaturas_presentadas
                dict_alu['asignaturas_aprobadas_segundo'] = asignaturas_aprobadas
            if i == tam: break

        if contador == 1:
            dict_alu['asignaturas_matriculadas_segundo'] = None
            dict_alu['asignaturas_presentadas_segundo'] = None
            dict_alu['asignaturas_aprobadas_segundo'] = None

        dict_alu['notas'] = []
        for asignatura in lista_asignaturas:
            nota_f = []
            for asig_c1 in lista_curso1:
                if asig_c1[0] == asignatura:
                    if asig_c1[1] != None:
                        nota_f.append(asig_c1[1])
            for asig_c2 in lista_curso2:
                if asig_c2[0] == asignatura:
                    if asig_c2[1] != None:
                        nota_f.append(asig_c2[1])
            if nota_f:
                if len(nota_f) == 2:
                    dict_alu['notas'].append(nota_f[-1])
                else:
                    dict_alu['notas'].append(nota_f[0])
            else:
                dict_alu['notas'].append(None)

        media_notas = []
        for nota in lista_curso1:
            if nota[1] != None:
                media_notas.append(nota[1])
        if len(media_notas) != 0:
            media = sum(map(float,media_notas))/float(len(media_notas))
            dict_alu['Nota_media_1'] = round(media, 2)
        else:
            dict_alu['Nota_media_1'] = None

        media_notas.clear()
        for nota in lista_curso2:
            if nota[1] != None:
                media_notas.append(nota[1])
        if len(media_notas) != 0:
            media = sum(map(float,media_notas))/float(len(media_notas))
            dict_alu['Nota_media_2'] = round(media, 2)
        else:
            dict_alu['Nota_media_2'] = None

        Alumnos.append(dict_alu)

    return Alumnos, lista_asignaturas

def create_table(grado, curso, lista, lista_asignaturas, RP30):
    """
    b_grado = Grado.objects.raw('SELECT alumno_id FROM datos_alumnos_grado')
    b_grado_rp30 = Grado.objects.raw('SELECT alumno_id FROM datos_alumnos_grado_rp30')
    alumnos_id_grado =  max(b_grado) if len(b_grado) > 0 else 0
    alumnos_id_grado_rp = max(b_grado_rp30) if len(b_grado_rp30) > 0 else 0
    id_act = max(alumnos_id_grado, alumnos_id_grado_rp)
    """
    if RP30 is None:
        for alumno in lista:
            created = Grado.objects.create(
                Grado = grado,
                Ingreso = curso,
                Sexo = alumno['Sexo'],
                Edad = alumno['Edad'],
                Procedencia = alumno['Procedencia'],
                Trabajo = alumno['Trabajo'],
                Beca = alumno['Becado'],
                Convocatoria = alumno['Convocatoria'],
                Preferencia = alumno['Preferencia'],
                Modalidad = alumno['Modalidad'],
                Especialidad = alumno['Especialidad'],
                Nota_Bach_Ciclo = alumno['Nota_Bach'],
                Nota_Prueba = alumno['Nota_prueba'],
                Nota_Especifica = alumno['Nota_esp'],
                Nota_Admision = alumno['Admision'],
                Abandono = alumno['Abandono'],
                asignaturas_matriculadas_primer_año = alumno['asignaturas_matriculadas_primero'],
                asignaturas_presentadas_primer_año = alumno['asignaturas_presentadas_primero'],
                asignaturas_aprobadas_primer_año = alumno['asignaturas_aprobadas_primero'],
                asignaturas_matriculadas_segundo_año = alumno['asignaturas_matriculadas_segundo'],
                asignaturas_presentadas_segundo_año = alumno['asignaturas_presentadas_segundo'],
                asignaturas_aprobadas_segundo_año = alumno['asignaturas_aprobadas_segundo'],
                media_primer_año = alumno['Nota_media_1'],
                media_segundo_año = alumno['Nota_media_2'],
                asignaturas = lista_asignaturas,
                notas = alumno['notas'],
            )

    else:
        for alumno in lista:
            created = Grado_rp30.objects.create(
                Grado = grado,
                Ingreso = curso,
                Sexo = alumno['Sexo'],
                Edad = alumno['Edad'],
                Procedencia = alumno['Procedencia'],
                Trabajo = alumno['Trabajo'],
                Beca = alumno['Becado'],
                Convocatoria = alumno['Convocatoria'],
                Preferencia = alumno['Preferencia'],
                Modalidad = alumno['Modalidad'],
                Especialidad = alumno['Especialidad'],
                Nota_Bach_Ciclo = alumno['Nota_Bach'],
                Nota_Prueba = alumno['Nota_prueba'],
                Nota_Especifica = alumno['Nota_esp'],
                Nota_Admision = alumno['Admision'],
                Abandono = alumno['Abandono'],
                asignaturas_matriculadas_primer_año = alumno['asignaturas_matriculadas_primero'],
                asignaturas_presentadas_primer_año = alumno['asignaturas_presentadas_primero'],
                asignaturas_aprobadas_primer_año = alumno['asignaturas_aprobadas_primero'],
                asignaturas_matriculadas_segundo_año = alumno['asignaturas_matriculadas_segundo'],
                asignaturas_presentadas_segundo_año = alumno['asignaturas_presentadas_segundo'],
                asignaturas_aprobadas_segundo_año = alumno['asignaturas_aprobadas_segundo'],
                media_primer_año = alumno['Nota_media_1'],
                media_segundo_año = alumno['Nota_media_2'],
                Aciertos = alumno['aciertos'],
                Fallos = alumno['errores'],
                Total_prueba = alumno['total'],
                asignaturas = lista_asignaturas,
                notas = alumno['notas'],
            )

def show_data(request):
    context = {}
    form = ShowDataForm(request.GET)
    context['form'] = form
    if request.method == 'GET':
        if form.is_valid():
            grado = form.cleaned_data['grado']
            curso = form.cleaned_data['curso']
            rp_30 = form.cleaned_data['rp_30']
            if rp_30 == True:
                alumnos = Grado_rp30.objects.raw('SELECT * FROM datos_alumnos_grado_rp30 WHERE "Ingreso" = %s AND "Grado" = %s', [curso, grado])
                context['rp_30'] = 'On'
            else:
                alumnos = Grado.objects.raw('SELECT * FROM datos_alumnos_grado WHERE "Ingreso" = %s AND "Grado" = %s', [curso, grado])
                context['rp_30'] = 'Off'
            context['alumnos'] = alumnos
            context['mensaje'] = 'No se encontraron registros'
    else:
        form = ShowDataForm()
    return render(request, 'mostrar_datos.html', context)


import torch
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader
from tqdm import tqdm

dataset = torchvision.datasets.CIFAR10("./dataset", train=False,
                                       transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)

class Test(nn.Module):
    def __init__(self):
        super(Test, self).__init__()
        self.linear = Linear(196608, 10)

    def forward(self, input):
        return self.linear(input)

test = Test()

for data in tqdm(dataloader):
    imgs, targets = data
    output = test(torch.flatten(imgs))
    print(imgs.shape)
    print(output.shape)


import re
from typing import Final

def _create_simple_pattern() -> re.Pattern[str]:
    NO_CLOSE_PAREN_CHAR = r'[^)]'
    return re.compile(fr"""
        [^a-zA-Z]       # Some non alphabetic character (prevent format! false positives)
        t!\("           # t!("
        ([^"]+)         # the translation key as a captured group
        "               # "
        (?:,
            {NO_CLOSE_PAREN_CHAR}+
        )?               # optional non-capturing group with a comma and several characters more, excepting close parens.
                \)              # )
    """, re.VERBOSE)

simple_pattern: Final = _create_simple_pattern()

def test_match_simple_pattern_1() -> None:
    KEY = "una_key"
    m = simple_pattern.search(f'let s = t!("{KEY}")')
    assert m
    assert len(m.groups()) == 1
    assert m.groups()[0] == KEY

def test_match_simple_pattern_2() -> None:
    KEY = "una_key_con_arg_${name}_"
    m = simple_pattern.search(f'let s = t!("{KEY}", name=name)')
    assert m
    assert len(m.groups()) == 1
    assert m.groups()[0] == KEY


"""CLI interface for reversewebsockify."""

import logging
import json
import os.path
import tornado.escape
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket
import tornado.concurrent
import tornado.gen
from tornado.queues import Queue
from tornado.options import define, options

define("port", default=8888, help="run on the given port", type=int)
q = Queue()

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/", MainHandler),
            (r"/ws", SocketHandler),
        ]
        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), 'templates'),
            static_path=os.path.join(os.path.dirname(__file__), 'static'),
        )
        super(Application, self).__init__(handlers, **settings)

class MainHandler(tornado.web.RequestHandler):
    @tornado.gen.coroutine
    def get(self):
        self.data_future = q.get()
        SocketHandler.broadcast(self.request.full_url())
        data = yield self.data_future
        self.write(data)
        self.set_status(200)
        self.finish()
    @tornado.gen.coroutine
    def post(self):
        self.data_future = q.get()
        SocketHandler.broadcast(json.loads(self.request.body))
        data = yield self.data_future
        self.write(data)
        self.set_status(200)
        self.finish()
    def on_connection_close(self):
        self.msg_future.set_result(None)

class SocketHandler(tornado.websocket.WebSocketHandler):
    waiters = set()

    def check_origin(self, origin):
        return True

    def open(self):
        SocketHandler.waiters.add(self)
        logging.info(
            'New WebSocket Connection: %d total',
            len(SocketHandler.waiters)
        )

    def select_subprotocol(self, subprotocol):
        if len(subprotocol):
            return subprotocol[0]
        return super().select_subprotocol(subprotocol)

    async def on_message(self, message):
        logging.info('new message: %s', message)
        await q.put(message)

    def on_close(self):
        SocketHandler.waiters.remove(self)
        logging.info(
            'Disconnected WebSocket (%d total)',
            len(SocketHandler.waiters)
        )

    @classmethod
    def broadcast(cls, data):
        for waiter in cls.waiters:
            try:
                waiter.write_message(data, binary=True)
            except tornado.websocket.WebSocketClosedError:
                logging.error("Error sending message", exc_info=True)

def relay():
    tornado.options.parse_command_line()
    app = Application()
    app.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

def cli() -> None:
    relay()


import os
from argparse import ArgumentParser

intro = '''# Use useful_python_programs/scars_thingy.py for ease of usage.

# TODO: (use CTRL + F to jump between these)
# 1) TITLE
# 2) INITIALS
# 3) DESCRIPTION

################
### 1) TITLE ###
################

defined_text = {
	name = Statelore_Get_Name'''
title_block = '''
	text = {
		trigger = {
			check_variable = { selected_lore = 1 }
		}
		localization_key = state_lore_name.1
	}'''
initial = '''
}

###################
### 2) INITIALS ###
###################

defined_text = {
	name = Statelore_Get_Initials'''
initial_block = '''
	text = {
		trigger = {
			check_variable = { lore_entries_onscreen^sl_i = 1 }
		}
		localization_key = state_lore_initials.1
	}'''
description = '''
}

######################
### 3) DESCRIPTION ###
######################
defined_text = {
	name = Statelore_Get_Desc'''
description_block = '''
	text = {
		trigger = {
			check_variable = { selected_lore = 1 }
		}
		localization_key = state_lore_desc.1
	}'''

end = '''
}
'''

if __name__ == "__main__":
	parser = ArgumentParser()
	parser.add_argument('number', metavar='number', type=int, help='''The number of blocks you want to do.''')
	args = parser.parse_args()
	number = args.number
	text = intro
	for i in range(1, number+1):
		text += title_block.replace('1', str(i))
	text += initial
	for i in range(1, number+1):
		text += initial_block.replace('1', str(i))
	text += description
	for i in range(1, number+1):
		text += description_block.replace('1', str(i))
	text += end

	output = open(os.path.join('..', 'common', 'scripted_localisation', 'TNO_state_lore_scripted_localisation.txt'), 'w+')
	output.write(text)


from langchain.vectorstores import Qdrant
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
import qdrant_client
import os

# Initialize a Qdrant client. Returns an object used to interact with the Qdrant server
client = qdrant_client.QdrantClient(
    os.getenv("QDRANT_HOST"),
    api_key= os.getenv("QDRANT_API_KEY")
)

#create a qdrant collection
def create_collection():
    os.environ['QDRANT_COLLECTION_NAME'] = "Tenaris"
    #create vectors configuration object
    vectors_config = qdrant_client.http.models.VectorParams(
        size=1536,
        distance=qdrant_client.http.models.Distance.COSINE
    )
    client.create_collection(
        collection_name=os.getenv("QDRANT_COLLECTION_NAME"),
        vectors_config=vectors_config
    )

#create vector store object
def add_texts_to_collection():
    embeddings = OpenAIEmbeddings()

    vector_store = Qdrant(
            client=client,
            collection_name=os.getenv("QDRANT_COLLECTION_NAME"),
            embeddings=embeddings
        )

    # Add documents to the cloud collection
    # get_chunks function takes a text and returns a list of chunks
    def get_chunks(text):
        splitter = CharacterTextSplitter(
            separator="\n",
            chunk_size=1000,
            chunk_overlap=150,
            length_function=len
            )
        chunks = splitter.split_text(text)
        return chunks

    with open("data/paper.txt", "r") as f:
        raw_text = f.read()

    texts = get_chunks(raw_text)
    return vector_store.add_texts(texts)

# plug vector store into retrieval qa chain

qa = RetrievalQA.from_chain_type(
    llm=OpenAI(),
    chain_type="stuff",
    retriever=Qdrant(
            client=client,
            collection_name=os.getenv("QDRANT_COLLECTION_NAME"),
            embeddings=OpenAIEmbeddings()
        ).as_retriever()

)
query = "What is the text about?"
print(qa.run(query))


# coding:utf-8

friuts = ['apple','banana','orange']

#result是一个可迭代的对象
result = filter(lambda x: 'e' in x,friuts)
print(list(result))
# 上面使用过了，在内存中就会销毁，所以下面的不会执行
for i in result:
    print(i)

print('-----------------------')

def filter_func(item):
    if 'e' in item:
        return True

filter_result = filter(filter_func,friuts)

for i in filter_result:
    print(i)

map_result = map(filter_func,friuts)
print(list(map_result))
# [True, None, True]
# 由于第二个不含e

from functools import reduce
# 累加
add_result = reduce(lambda x,y:x+y,[0,1,2,3,4,5])
# 累乘
multip_result = reduce(lambda x,y:x*y,[1,2,3,4,5])
print(add_result) # 15
print(multip_result)

# 字符串累加
add_result_str = reduce(lambda x,y:x+y,friuts)
# 字符串累乘 报错
# multip_result_str = reduce(lambda x,y:x*y,friuts)
# print(multip_result_str)

11.高阶函数练习.py


from collections import Counter

n, c = [int(d) for d in input().split()]
num = [int(d) for d in input().split()]

freq = Counter(num)
print(
    " ".join(
        [
            str(d)
            for d in sorted(
                num, key=lambda k: (freq[k], n - num.index(k)), reverse=True
            )
        ]
    )
)


import argparse

import urllib3

def main(receiving_service_url):

    # Set up metadata server request
    # See https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature
    metadata_server_token_url = 'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience='

    token_request_url = metadata_server_token_url + receiving_service_url
    token_request_headers = {'Metadata-Flavor': 'Google'}

    http = urllib3.PoolManager()
    token_response = http.request('GET', token_request_url, headers=token_request_headers)

    # Fetch the token
    if token_response.status == 200:
        jwt = token_response.data.decode("utf-8")
        return jwt
    return None

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("audience")
    args = parser.parse_args()
    print(main(args.audience))


### TASK ###
# Write a program that queries Covalent's API ticker endpoint.
# Collect null contract address results for 60 samples at a rate of 1 sample per minute.
# After 60 samples, display a histogram of null results. Only include currencies that have >0 null address responses.

### AUTHOR ###
# Kristopher Buote
# September 26, 2021

### Use python's requests library to query API
import requests
import time
import matplotlib.pyplot as plt
import numpy as np
import csv
import sys

### SET UP ###
# Test connection to the Covalent API with error handling.
# Organize the response data into dictionaries.
# Create a dictionary to store the contract addresses from the API response.
# This program uses the Ticker to associate with sample responses.

### Receive API key as user input
mykey = input("Enter Covalent API Key:")

### Try an initial connection to the API
try:
    response = requests.get('https://api.covalenthq.com/v1/pricing/tickers/',
                            auth=(mykey, ''))

# Exit program if initial connection failed.
except:
    print(f"Connection error. Program exited. Error:{sys.exc_info()[0]}")
    exit()

### Collect the API's response error and message
json_response = response.json()
error = json_response["error"]
error_message = json_response["error_message"]

### Check if response was good (i.e. 200 response code and error is false).
# If good, continue. Else, print error code and message.
if response.status_code == 200 and error is False:
    print(f"API Response: {response.status_code}. No error. Program continuing.")

    ### Organize API response JSON data
    data = json_response["data"]
    timestamp = data["updated_at"]
    items = data["items"]

    ### Create a dictionary using the currency Ticker symbols to store results
    # *** This program assumes there will be no errors returned by the contract_ticker_symbol. It also assumes the same ticker symbols will be returned every sample***
    results = {}

    # Collect the ticker symbols and store them in results
    for item in items:
        ticker_symbol = item["contract_ticker_symbol"]
        results[ticker_symbol] = {}

else:
    print(f"API Response Code: {response.status_code} \nAPI Error Message: {error_message}\nProgram failed and exited.")
    exit()

### DATA COLLECTION ###
# Collect 60 samples, approximately 1 sample per minute
# The results are stored in a nested dictionary using the Ticker and sample count as keys.
# e.g. {'Ticker1': {1: 'abcd', 2: null, ..., 60: 'abcd'}, 'Ticker2': {1: 'efg', 2: 'efg', ..., 60: null}}

for sample in range(1, 61):

    # Query API
    response = requests.get('https://api.covalenthq.com/v1/pricing/tickers/',
                            auth=(mykey, ''))

    json_response = response.json()
    error = json_response["error"]
    error_message = json_response["error_message"]

    # Organize API response JSON data
    data = json_response["data"]
    timestamp = data["updated_at"]
    all_items = data["items"]

    ### Check if an error was thrown by the API response
    # If error message is false, continue with program. Else, skip sample.
    if error is False:

        # Collect results in a nested dictionary using the Ticker symbol as key
        # A new entry will be added for each sample. If an address is returned, it will be stored. If null is returned, it will be stored as null.
        for item in all_items:
            item_ticker = item["contract_ticker_symbol"]
            item_address = item["contract_address"]
            results[item_ticker][sample] = item_address

        print(f"Sample {sample} collected.")

    else:
        print(f"Sample {sample} failed and skipped. API error message: {error_message}")

    # Wait 1 minute for another sample. This is usually commented out for developing.
    time.sleep(60)

### DATA ANALYSIS ###
# Count the amount of null responses for each ticker across the 60 samples
# Note that None is the variable for null in python

# Create a new dictionary to store the null count
ticker_null_count = {}

# Count the number of null address responses for each ticker
for ticker, samples in results.items():
    null_count = sum(value == None for value in samples.values())
    ticker_null_count[ticker] = null_count

# Exclude the tickers with a zero null count
ticker_null_count_non_zero = {key: value for key, value in ticker_null_count.items() if value != 0}

# Sort the dictionary in ascending order
ticker_null_count_ordered = {key: value for key, value in sorted(ticker_null_count_non_zero.items(), key=lambda item: item[1])}

### DATA PRESENTATION AND SAVING ###
### Save the null count data to a csv file
with open('null_count.csv', 'w') as csv_file:
    writer = csv.writer(csv_file)
    for key, value in ticker_null_count_non_zero.items():
       writer.writerow([key, value])

### Create a histogram for the null counts in ascending order. Save the figure as a png file.
plt.bar(ticker_null_count_ordered.keys(), ticker_null_count_ordered.values(), width=1.0, color='g')
plt.title("Null Address Count over 60 samples")
plt.xlabel("Currency Ticker")
plt.ylabel("Null Address Count")

# Save the figure
plt.savefig('null_count_histogram.png')
plt.show()

print("Program complete.")


from unittest import mock

import pytest
from shipyard_airflow.plugins import concurrency_check_operator as operator
from shipyard_airflow.plugins.concurrency_check_operator import (
    ConcurrencyCheckOperator
)
from airflow.exceptions import AirflowException

COLOR_SETS = [
    set(['blue', 'green']),
    set(['blue', 'purple']),
    set(['red', 'purple']),
    set(['red', 'orange']),
    set(['yellow', 'green']),
    set(['yellow', 'orange']),
]

CONFLICT_SET = set(['cow', 'monkey', 'chicken'])

def test_find_conflicting_dag_set():
    """
    Ensure that the right values are determined by find_conflicting_dag_set
    """
    # Should not be found in the default set - no conflicts
    assert operator.DAG_RUN_SELECT_RUNNING_SQL is not None
    assert not operator.find_conflicting_dag_set("this_is_completely_cheese")

    # Check for contents vs the COLOR_SETS
    not_in_green_response_set = set(['purple', 'red', 'orange'])
    response_set = operator.find_conflicting_dag_set(
        dag_name='green', conflicting_dag_sets=COLOR_SETS)
    assert 'blue' in response_set
    assert 'yellow' in response_set
    assert not_in_green_response_set.isdisjoint(response_set)
    assert len(response_set) == 2

def get_executing_dags_stub_running_twice():
    return [
        ('buffalo', 'now'),
        ('buffalo', 'earlier'),
        ('squirrel', 'ages ago'),
    ]

def get_executing_dags_stub():
    return [
        ('buffalo', 'now'),
        ('chicken', 'earlier'),
        ('monkey', 'ages ago'),
    ]

def get_executing_dags_stub_no_conflicts():
    return [
        ('buffalo', 'now'),
        ('hedgehog', 'earlier'),
        ('panda', 'ages ago'),
    ]

def test_find_conflicting_dag():
    """
    Ensure that:
    1) responds with a found conflict
    2) responds None if there is no conflict
    3) responds with the dag_id_to_check being searched for if it is running
       more than once.
    """
    cco = ConcurrencyCheckOperator(
        conflicting_dag_set=CONFLICT_SET,
        task_id='bogus')

    # no conflicts
    cco.get_executing_dags = get_executing_dags_stub_no_conflicts
    assert cco.find_conflicting_dag('buffalo') is None

    # self is running twice
    cco.get_executing_dags = get_executing_dags_stub_running_twice
    assert cco.find_conflicting_dag('buffalo') != 'squirrel'
    assert cco.find_conflicting_dag('buffalo') == 'buffalo'

    # a conflict from the list
    cco.get_executing_dags = get_executing_dags_stub
    assert cco.find_conflicting_dag('buffalo') != 'monkey'
    assert cco.find_conflicting_dag('buffalo') == 'chicken'

@mock.patch('shipyard_airflow.plugins.concurrency_check_operator.XcomPusher')
def test_execute_exception(xcom_pusher):
    """
    Run the whole execute function for testing
    """
    cco = ConcurrencyCheckOperator(
        conflicting_dag_set=CONFLICT_SET,
        task_id='bogus')
    # dag_id of cow should cause monkey to conflict.
    cco.check_dag_id = 'cow'
    cco.get_executing_dags = get_executing_dags_stub
    try:
        context = {'task_instance': None}
        cco.execute(context)
        pytest.fail('AirflowException should have been raised')
    except AirflowException as airflow_exception:
        assert 'Aborting run' in airflow_exception.args[0]

@mock.patch('shipyard_airflow.plugins.concurrency_check_operator.XcomPusher')
def test_execute_success(xcom_pusher):
    """
    Run the whole execute function for testing - successfully!
    """
    cco = ConcurrencyCheckOperator(
        conflicting_dag_set=set(['car', 'truck']),
        task_id='bogus')

    # dag_id of airplane should have no conflicts
    cco.check_dag_id = 'airplane'
    cco.get_executing_dags = get_executing_dags_stub
    try:
        context = {'task_instance': None}
        cco.execute(context)
        assert True
    except AirflowException:
        pytest.fail('AirflowException should not have been raised')


"""
The following module receives user's coordinates, a year and returns a map
with points of the locations of filming on that year that are the nearest to
the user's location.

To do that, the module uses data.csv file that is generated by gen_csv module.
"""

import pandas as pd
import folium as fl
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderUnavailable
import haversine

def gen_address(coodrdinates: tuple):
    '''
    The function generates an address by the entered by user coordinates.
    '''
    address = ''

    for elem in coodrdinates:
        address += str(elem) + ', '

    geolocator = Nominatim(user_agent="tito")
    location = geolocator.reverse(address[:-2], language='en')

    location = str(location).split(', ')
    return location[-1]

def gen_location(location: str):
    '''
    The function returns latitude and longitude of the entered location.
    '''
    data = ''
    check = False
    counter = 0
    try:
        while check != True:
            if counter == 6:
                return ''

            geolocator = Nominatim(user_agent='tito').geocode(location)

            try:
                data = (geolocator.latitude, geolocator.longitude)
                check = True

            except AttributeError:

                location = location.split(', ')[1:]
                new_location = ''

                for elem in location:
                    new_location += elem + ', '

                location = new_location
                counter += 1

    except GeocoderUnavailable:
        return data
    return data

def gen_distance(coord1: tuple, coord2: tuple):
    '''
    The function returns the great-circle distance between two points on a sphere.
    >>> gen_distance((45.7597, 4.8422), (48.8567, 2.3508))
    392.2172595594006
    '''
    distance = haversine.haversine(coord1, coord2)

    return distance

def calculate_distances(year: int, point: tuple):
    '''
    The function filters a DataFrame by the year entered by user,
    calculates the distances from the entered by the user point to all the
    coordinates and returns the nearest spots.
    '''

    data = pd.read_csv("data.csv")
    address = gen_address(point)  # used to narrow down the number of options

    data = data[data['Location'].str.contains(address) == True]
    data = data[data['Year'] == year]

    data.to_csv('beta.csv', index=False)

    print('\nSearching the nearest points...')

    data['Coordinates'] = data['Location'].apply(gen_location)
    data = data[data['Coordinates'] != '']

    data.to_csv('beta.csv', index=False)

    data['Lat'] = data['Coordinates'].apply(lambda x: x[0])
    data['Lon'] = data['Coordinates'].apply(lambda x: x[1])

    data['Distance'] = data['Coordinates'].apply(
        lambda x: gen_distance(point, x))

    data = data.sort_values(by=['Distance'])

    print('\nGenerating the map...')

    data = data.head(10)
    data.to_csv('beta.csv', index=False)

    return data[['Title', 'Year', 'Location', 'Lat', 'Lon', 'Distance']]

def create_map(data, address: tuple):
    '''
    The following function generates a map with the filming points based
    on a DataFrame.
    '''
    lat = data['Lat'].tolist()
    lon = data['Lon'].tolist()
    title = data['Title'].tolist()
    point_address = data['Location'].tolist()
    year = data['Year'].tolist()
    dist = data['Distance'].tolist()
    length = data.shape[0]

    location = [address[0], address[1]]

    film_map = fl.Map(location=location,
                      zoom_start=5)

    film_points = fl.FeatureGroup(name='Film map')

    html = """

{}

Year: {}
Address: {}

Distance: {} km

        """

    for _ in range(length):

        coordinates = [location, [float(lat[_]), float(lon[_])]]

        film_points.add_child(fl.Marker(location=[lat[_], lon[_]],
                                        popup=html.format(
            title[_], year[_], point_address[_], round(dist[_], 2)),
            icon=fl.Icon()))

        connector = fl.PolyLine(locations=coordinates, weight=3)
        film_map.add_child(connector)

    fl.Marker(
        location=location,
        popup="You are here!",
        icon=fl.Icon(icon="home", color="red"),
    ).add_to(film_map)

    film_map.add_child(film_points)
    film_map.save('FilmsAroundYou.html')

    text = '\nOpen your browser to view the generated map!'

    return text

def main():
    '''
    The function executes aa process of creating a map based on the info,
    entered by user.
    '''
    example = '2002 -37.9064862 144.7533059'
    user_info = input(
        '\nPlease, enter the year and your coordinates (latitude & longitude) \
like this:\n{}\nEXAMPLE: {}\n{}\n'
        .format('='*37, example, '='*37))

    user_year, lat, lon = user_info.split(' ')
    user_coord = (float(lat), float(lon))
    user_year = int(user_year)

    ten_points = calculate_distances(user_year, user_coord)

    if ten_points.shape[0] == 0:
        print('\n\n\nOops, looks like there are no near films points in that year!')
        print('\nTry again with another coordinates/year or regenerate the file!')
        main()

    result = create_map(ten_points, user_coord)
    print(result)

    return result

if __name__ == "__main__":

    main()


import pyranges
import logging
import pathlib
import sys

def id_to_name(params):
    format_map = {
        "gtf": pyranges.read_gtf,
        "gff": pyranges.read_gff3,
        "gff3": pyranges.read_gff3,
    }
    annot_reader = None
    if params.format is None:
        p = pathlib.Path(params.gtf_file)
        suffs = [z.lower().strip('.') for z in p.suffixes]

        z = None
        if len(suffs) >= 1:
            # look at the final suffix
            z = suffs[-1]
            # if the final suffix is gz and there are
            # suffixes preceding it, check the penultimate
            # one and use that
            if z == "gz" and len(suffs) > 1:
                z = suffs[-2]

        if z is None or z not in format_map.keys():
            logging.error(
                "Could not determine format of annotation file. Please provide it explicitly."
            )
            sys.exit(1)
        else:
            annot_reader = format_map[z]
    else:
        fmt = params.format.lower()
        if fmt not in format_map.keys():
            logging.error(
                f'Format must be either "gtf" or "gff3", but {fmt} was provided.'
            )
            sys.exit(1)
        annot_reader = format_map[fmt]

    a = annot_reader(params.gtf_file)
    # only bother looking at `gene` features
    a_genes = a[(a.Feature == "gene")]
    id_name = {}
    for k, df in a_genes:
        cdf = df[["gene_id", "gene_name"]].to_dict(orient="records")
        id_name.update({d["gene_id"]: d["gene_name"] for d in cdf})

    logging.info(f"generated mappings for {len(id_name)} gene ids.")
    logging.info(f"writing output to {params.output}")

    with open(params.output, "w") as ofile:
        for k, v in id_name.items():
            ofile.write(f"{k}\t{v}\n")


from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import date, datetime, timedelta
import glob
import hashlib
import pandas as pd
import re
import time
import os

# set up input and output folders
input_folder = '/Users/yanqingfu/airflow/in'
successful_folder = '/Users/yanqingfu/airflow/successful'
unsuccessful_folder = '/Users/yanqingfu/airflow/unsuccessful'

# get today's date at 12:00 AM
today = datetime.now()
start_date = today.replace(hour=0, minute=0, second=0, microsecond=0)

# define DAG arguments
default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=3),
    'start_date': start_date,
}

# define DAG
dag = DAG(
    'membership_data_pipeline',
    default_args=default_args,
    description='Pipeline to process membership applications',
    schedule=timedelta(hours=1),
    catchup=False,
)

# get first name and last name from full name
def extract_name_parts(name):
    regex = r'([A-Z][a-z]+) ([A-Z][a-z]+)'
    match = re.search(regex, name)
    if match:
        return match.group(1), match.group(2)

# format brithday as YYYYMMDD for different date formats
def format_dob(dob):
    formats = ['%d-%m-%Y', '%Y-%m-%d', '%m/%d/%Y', '%Y/%m/%d']
    for date_format in formats:
        try:
            return datetime.strptime(dob, date_format).strftime('%Y%m%d')
        except ValueError:
            pass

# create memebership id based on last name and birthday hash
def generate_membership_id(row):
    hash_dob = hashlib.sha256(row['date_of_birth'].encode()).hexdigest()[:5]
    return f"{row['last_name']}_{hash_dob}"

# check for new csv files and load them into one dataframe
def get_new_csv_files():
    df = pd.DataFrame()
    # buffer time of 10 minutes
    minutes_ago = time.time() - 600
    all_files = glob.glob(input_folder + "/*.csv")
    # get all files that were added within the last 10 minutes
    new_files = [file for file in all_files if os.path.getmtime(file) >= minutes_ago]
    if new_files:
        df_list = [pd.read_csv(file) for file in new_files]
        df = pd.concat(df_list, ignore_index=True)
        return df

# clean and transform data
def format_data(df):
    if df is not None and not df.empty:
        # drop rows with missing values
        df.dropna(subset=['name', 'date_of_birth'], inplace=True)
        # remove duplicate rows based on all columns
        df.drop_duplicates(inplace=True)
        # get first name and last name
        df[['first_name', 'last_name']] = df['name'].apply(
            extract_name_parts).apply(pd.Series)
        # format date of birth
        df['date_of_birth'] = df['date_of_birth'].apply(format_dob)
        # remove spaces in mobile number
        df['mobile_no'] = df['mobile_no'].str.replace(' ', '')
        # check if age is above 18 years old as of 1 Jan 2022
        df['above_18'] = (pd.to_datetime('20220101',
                                        format='%Y%m%d') - pd.to_datetime(df['date_of_birth'],
                                                                        format='%Y%m%d')).dt.days > 18 * 365
        return df
    else:
        return pd.DataFrame()

# perform validity checks and create membership IDs for successful applications
def check_validity(df):
    result_dict = {}
    if df is not None and not df.empty:
        # check if mobile number is 8 digits, age is above 18 and email ends with .com or .net
        valid_mobile = df['mobile_no'].str.len() == 8
        valid_age = df['above_18']
        valid_email = df['email'].str.endswith(('.com', '.net'))
        # create membership id for successful applications
        successful_applications = df[valid_mobile & valid_age & valid_email].copy()
        successful_applications['membership_id'] = successful_applications.apply(
            generate_membership_id, axis=1)
        unsuccessful_applications = df[~(valid_mobile & valid_age & valid_email)]
        result_dict = {
            'successful_applications': successful_applications,
            'unsuccessful_applications': unsuccessful_applications
        }
        return result_dict
    else:
        return result_dict

# write results as csv files to different status folders
def output_csv_files(result_dict):
    if result_dict:
        successful_applications = result_dict['successful_applications']
        unsuccessful_applications = result_dict['unsuccessful_applications']
        # get the current datatime as YYYYMMDDHH
        now = datetime.now()
        formatted_datetime = now.strftime('%Y%m%d%H')
        # create folders if they don't exist
        for folder in [successful_folder, unsuccessful_folder]:
            if not os.path.exists(folder):
                os.makedirs(folder)
        # write csv files
        successful_applications.to_csv(
            successful_folder +
            '/successful_applications' +
            '_' +
            formatted_datetime +
            '.csv',
            index=False)
        unsuccessful_applications.to_csv(
            unsuccessful_folder +
            '/unsuccessful_applications' +
            '_' +
            formatted_datetime +
            '.csv',
            index=False)

# define task to get new csv files and load them into one dataframe
get_new_csv_files_task = PythonOperator(
    task_id='get_new_csv_files',
    python_callable=get_new_csv_files,
    dag=dag
)

# define task to format data and clean it
format_data_task = PythonOperator(
    task_id='format_data',
    python_callable=format_data,
    op_kwargs={'df': get_new_csv_files_task.output},
    dag=dag
)

# define task to check validity and create membership IDs
check_validity_task = PythonOperator(
    task_id='check_validity',
    python_callable=check_validity,
    op_kwargs={'df': format_data_task.output},
    dag=dag
)

# define task to write csv files to different status folders
output_csv_files_task = PythonOperator(
    task_id='output_csv_files',
    python_callable=output_csv_files,
    op_kwargs={'result_dict': check_validity_task.output},
    dag=dag
)


from scipy.ndimage import distance_transform_edt as distance
from skimage import segmentation as skimage_seg
import torch
import torch.nn as nn
import numpy as np

"""
original code: https://github.com/LIVIAETS/boundary-loss
refarence from: https://github.com/JunMa11/SegWithDistMap
"""

class BoundaryLoss(nn.Module):
    """
    compute boundary loss for binary segmentation
    input: outputs_soft: sigmoid results,  shape=(b,c,x,y)
           gt_sdf: sdf of ground truth (can be original or normalized sdf); shape=(b,c,x,y)
    output: boundary_loss; sclar
    """

    def __init__(self, class_idx=1):
        super(BoundaryLoss, self).__init__()
        # Self.idc is used to filter out some classes of the target mask. Use fancy indexing
        self.class_idx = class_idx

    def __call__(self, outputs_soft, label_batch):
        # print(outputs_soft.shape)
        gt_sdf_npy = compute_sdf1_1(label_batch.cpu().numpy(), outputs_soft.shape)
        gt_sdf = torch.from_numpy(gt_sdf_npy).float().cuda(outputs_soft.device.index)
        pc = outputs_soft.type(torch.float32)
        dc = gt_sdf.type(torch.float32)

        multipled = torch.einsum("bcwh, bcwh->bcwh", pc, dc)
        loss = multipled.mean(dim=(1,2,3))

        return loss

def compute_sdf1_1(img_gt, out_shape):
    """
    compute the normalized signed distance map of binary mask
    input: segmentation, shape = (batch_size, c, x, y)
    output: the Signed Distance Map (SDM)
    sdf(x) = 0; x in segmentation boundary
             -inf|x-y|; x in segmentation
             +inf|x-y|; x out of segmentation
    normalize sdf to [-1, 1]
    """
    img_gt = img_gt.astype(np.uint8)
    normalized_sdf = np.zeros(out_shape)

    for b in range(out_shape[0]): # batch size
            # ignore background
        posmask = img_gt[b].astype(np.bool)
        for c in range(out_shape[1]):
            if posmask.any():
                negmask = ~posmask
                posdis = distance(posmask)
                negdis = distance(negmask)
                boundary = skimage_seg.find_boundaries(posmask, mode='inner').astype(np.uint8)
                sdf = (negdis-np.min(negdis))/(np.max(negdis)-np.min(negdis)) - (posdis-np.min(posdis))/(np.max(posdis)-np.min(posdis))
                sdf[boundary==1] = 0
                normalized_sdf[b][c] = sdf

    return normalized_sdf

def compute_sdf(img_gt, out_shape):
    """
    compute the signed distance map of binary mask
    input: segmentation, shape = (batch_size, x, y, z)
    output: the Signed Distance Map (SDM)
    sdf(x) = 0; x in segmentation boundary
             -inf|x-y|; x in segmentation
             +inf|x-y|; x out of segmentation
    """
    img_gt = img_gt.astype(np.uint8)
    gt_sdf = np.zeros(out_shape)

    for b in range(out_shape[0]): # batch size
        posmask = img_gt[b].astype(np.bool)
        for c in range(out_shape[1]):
            if posmask.any():
                negmask = ~posmask
                posdis = distance(posmask)
                negdis = distance(negmask)
                boundary = skimage_seg.find_boundaries(posmask, mode='inner').astype(np.uint8)
                sdf = negdis - posdis
                sdf[boundary==1] = 0
                gt_sdf[b][c] = sdf

    return gt_sdf


import numpy as np
import pandas as pd
import sys, json

class NaiveBayes:
	def __init__(self):
		self.pos_class = 0
		self.neg_class = 0
		self.class_prob = []
		self.cond_prob = {} # Dictionary to store the conditional probabilities of each attribute given the class 0 or 1

	#Computes the number of positive and negative class and calculate its corresponding probabilities and store in class_prob
	def count_y(self, y):
		for val in y:
			if val==1:
				self.pos_class = self.pos_class+1
		self.neg_class = len(y)-self.pos_class

		self.class_prob.append(self.neg_class/len(y))
		self.class_prob.append(self.pos_class/len(y))

	#Computes the conditional probability for the given attribute given the class
	def compute_freq(self, X, feature, y):
		freq = {}
		for i in range(len(X)):
			val = X[i]
			if val in freq.keys():
				if y[i] == 0:
					freq[val][0] = freq[val][0]+1
				else:
					freq[val][1] = freq[val][1]+1
			else:
				freq[val] = [0,0]
				if y[i] == 0:
					freq[val][0] = freq[val][0]+1
				else:
					freq[val][1] = freq[val][1]+1
		return freq

	#Function to train the training data by computing the conditional probabilities of all attribute given each class output
	def train(self, X_train, y_train):
		self.count_y(y_train)

		for col in X_train.columns:
			#print(col)
			freq = self.compute_freq(X_train[col], col, y_train)
			#print(freq)
			prob = {}
			for key in freq:
				prob[key] = [0,0]
				if freq[key][0] == 0:
					freq[key][0] = 2
				if freq[key][1] == 0:
					freq[key][1] = 2

				prob[key][0] = freq[key][0]/self.neg_class
				prob[key][1] = freq[key][1]/self.pos_class
			#print(prob)
			self.cond_prob[col] = prob

	#returns predicted class y for the given test data
	def test(self, test_data):
		pred_y = []
		for index, row in test_data.iterrows():
			pos_prob = 1
			neg_prob = 1
			for col in test_data.columns:
				neg_prob = neg_prob*self.cond_prob[col][row[col]][0]
				pos_prob = pos_prob*self.cond_prob[col][row[col]][1]
			if pos_prob > neg_prob:
				pred_y.append(1)
			else:
				pred_y.append(0)
		return pred_y

if __name__ == "__main__":
	args = sys.argv
	train_file = str(args[1])
	test_file = str(args[2])
	MFile_name = str(args[3])
	RFile_name = str(args[4])

	train_data = pd.read_csv(train_file)
	NB = NaiveBayes()
	y = train_data['class']
	#NB.count_y(y)

	X_train = train_data.drop(['class'], axis=1)

	NB.train(X_train, y)

	MFile = open(MFile_name, "a")
	MFile.write("\nClass probabilities\n")
	MFile.write("\nP(Class 0) = "+str(NB.class_prob[0]))
	MFile.write("\nP(Class 1) = "+str(NB.class_prob[1]))

	MFile.write("\n\nConditional probabilities for each attribute")
	for col in X_train.columns:
		MFile.write("\n\nAttribute : "+col)
		MFile.write("\n\t Class 0 \t\t Class 1")
		for key in NB.cond_prob[col]:
			MFile.write("\n"+str(key))
			MFile.write("\t"+str(NB.cond_prob[col][key][0]))
			MFile.write("\t"+str(NB.cond_prob[col][key][1]))
		#df = pd.DataFrame(NB.cond_prob[col])
		#df.to_json(MFile)
		#df.to_csv(MFile)

	test_data  = pd.read_csv(test_file)
	test_y = test_data['class'].values
	test_X = test_data.drop(['class'], axis=1)
	pred_y = NB.test(test_X)

	RFile = open(RFile_name, "a")
	RFile.write("Predicted class\n")
	json.dump(pred_y, RFile)

	#Computing confusion matrix
	TP = 0
	TN = 0
	FP = 0
	FN = 0

	for i in range(len(test_y)):
		if test_y[i] == 1 and pred_y[i] == 1:
			TP = TP + 1
		if test_y[i] == 0 and pred_y[i] == 0:
			TN = TN + 1
		if test_y[i] == 0 and pred_y[i] == 1:
			FP = FP + 1
		if test_y[i] == 1 and pred_y[i] == 0:
			FN = FN + 1

	RFile.write("\n\nConfusion Matrix: \n")
	RFile.write("\t True \t False \n")
	RFile.write("True \t "+str(TP)+"\t "+str(FP)+"\n")
	RFile.write("False \t "+str(FN)+"\t "+str(TN)+"\n")


import logging
import json
import os
import math

logger = logging.getLogger()
logger.setLevel(logging.INFO)

class FileStorageClient:

    def __init__(self, client, bucket_name):
        self.client = client
        self.bucket_name = bucket_name

    def upload_local_file(self, file_name):
        s3_file_key = file_name.replace('_', '/')

        logging.info(f"Opening file {file_name}.")
        file_content = open(file_name, 'rb').read()

        logging.info(f"Saving file in s3 bucket with file_key s3://{self.bucket_name}/{s3_file_key}.")
        self._upload(file_key=s3_file_key, file_content=file_content)

    def get(self, file_key):
        logger.info(f'Getting file content for the file: {file_key} from the bucket: {self.bucket_name}')
        response = self.client.get_object(
            Bucket=self.bucket_name,
            Key=file_key
        )
        return response['Body']

    def _upload(self, file_key, file_content):
        logger.info(f'Uploading file {file_key} into bucket {self.bucket_name}...')
        self.client.put_object(
            Bucket=self.bucket_name,
            Key=file_key,
            Body=file_content
        )

    def download_directory(self, directory_prefix, local_directory):
        os.system(f"mkdir {local_directory}")
        os.system(f"aws s3 sync s3://{self.bucket_name}/{directory_prefix} {local_directory}")

class QueueClient:

    BATCH_REQUEST_LIMIT = 10

    def __init__(self, resource, queue_name):
        self.resource = resource
        self.queue_name = queue_name
        self.queue = self.resource.get_queue_by_name(QueueName=self.queue_name)

    def receive_message(self):
        logger.info(f"Receiving messages from SQS queue")
        messages = self.queue.receive_messages(
            AttributeNames=['SentTimestamp'],
            MaxNumberOfMessages=1,
            MessageAttributeNames=['All'],
            VisibilityTimeout=900,
            WaitTimeSeconds=3
        )
        if messages:
            return messages[0].body, messages[0].receipt_handle
        else:
            return None, None

    def delete_message(self, receipt_handle):
        logger.info(f"Deleting consumed message from SQS queue: {self.queue_name}")
        self.resource.meta.client.delete_message(
            QueueUrl=self.queue.url,
            ReceiptHandle=receipt_handle
        )

    def send_messages(self, messages):
        logger.info(f"Number of messages to send to SQS queue {self.queue_name} - {len(messages)}")

        request_count = math.ceil(len(messages) / self.BATCH_REQUEST_LIMIT)

        logger.info(f"Messages will be sent in following number of requests: {request_count}")
        for i in range(request_count):
            start = i * self.BATCH_REQUEST_LIMIT
            end = (i + 1) * self.BATCH_REQUEST_LIMIT

            messages_page = messages[start:end]

            logger.info(f"Sending {i+1}. batch of messages[{start}:{end}] to SQS...")
            self._send_messages_batch(messages_page)
            logger.info("Successfully delivered.")

    def _send_messages_batch(self, messages):
        response = self.resource.meta.client.send_message_batch(
            QueueUrl=self.queue.url,
            Entries=[dict(
                Id=f"{item['country']}-{item['competitor']}-{item['category']}-{item['page_number']}",
                MessageBody=json.dumps(item)
            ) for item in messages]
        )
        successful = response.get("Successful", [])
        failed = response.get("Failed", [])

        logger.info(f"Number of successfully sent messages in batch: {len(successful)}")

        if len(failed) > 0:
            logger.error(f"Number of failed messages in batch: {len(failed)}")
            for f in failed:
                logger.error(f)


import datetime
import threading

import cv2
import mediapipe as mp
import pika
import screeninfo

class NewsConsumer:
    """
    This class implements a RabbitMQ consumer.
    """
    articles = []
    titles = 'There are no news to display...'

    def __init__(self, host):
        """
        This method initializes the RabbitMQ consumer.
        """
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(host))
        self.channel = self.connection.channel()
        self.channel.queue_declare(queue='news_stream')
        self.channel.queue_declare(queue='news_titles')

    def consume(self):
        """
        This method starts consuming messages from the RabbitMQ queue.
        """
        self.channel.basic_consume(queue='news_stream', on_message_callback=self.add_new_article)
        self.channel.basic_consume(queue='news_titles', on_message_callback=self.update_titles)
        self.channel.start_consuming()

    def add_new_article(self, ch, method, properties, body):
        """
        This method adds a new article to the list of articles.
        """
        new_article = body.decode('utf-8')
        # concatenate the new article to the text
        NewsConsumer.articles.append(new_article)

    def update_titles(self, ch, method, properties, body):
        """
        This method updates the titles of the news.
        """
        global titles
        titles = body.decode('utf-8')
        print(
            f"Received titles: {titles} at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
        )
        self.titles += titles
        ch.basic_ack(delivery_tag=method.delivery_tag)

    def close(self):
        """
        This method closes the connection to the RabbitMQ server.
        """
        if self.connection.is_open:
            self.connection.close()

def start_consumer():
    """
    This method starts the RabbitMQ consumer.
    """
    consumer = NewsConsumer('localhost')
    try:
        consumer.consume()
    except KeyboardInterrupt:
        consumer.close()

thread = threading.Thread(target=start_consumer)
thread.start()

camera_id = 0

def start_streaming():

    """
    This method starts the video streaming.
    """

    # Using OpenCV to display the image
    cap = cv2.VideoCapture(camera_id)

    w = screeninfo.get_monitors()[0].width
    h = screeninfo.get_monitors()[0].height

    cap.set(cv2.CAP_PROP_FRAME_WIDTH, w)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, h)
    cap.set(cv2.CAP_PROP_FPS, 60)

    # mp_drawing = mp.solutions.drawing_utils
    # mp_drawing_styles = mp.solutions.drawing_styles
    mp_hands = mp.solutions.hands

    zoomed_text_color = (0, 255, 0)
    standard_text_color = (255, 255, 255)
    color = standard_text_color

    direction = 0
    font_size = 12
    box_height = 50
    # box width is the same as the image width
    box_width = 1280

    # is_queue_requested = False

    with mp_hands.Hands(
            model_complexity=0,
            min_detection_confidence=0.6,
            min_tracking_confidence=0.5) as hands:

        while cap.isOpened():

            success, image = cap.read()

            if not success:
                print("Ignoring empty camera frame.")
                # If loading a video, use 'break' instead of 'continue'.
                continue

            image = cv2.flip(image, 1)

            # To improve performance, optionally mark the image as not writeable to
            # pass by reference.
            image.flags.writeable = False
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            results = hands.process(image)

            # # Draw the hand annotations on the image.
            image.flags.writeable = True
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

            # create a background for the text with a border
            cv2.rectangle(image, (0, image.shape[0] - box_height), (image.shape[1], image.shape[0]), (0, 0, 0), -1)
            # border
            cv2.rectangle(image, (0, image.shape[0] - box_height), (image.shape[1], image.shape[0]), (255, 255, 255), 2)

            if results.multi_hand_landmarks:
                for hand_landmarks in results.multi_hand_landmarks:

                    # if left hand is raised then move the text to the left
                    if hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].x < 0.2 and len(results.multi_hand_landmarks) == 1:
                        NewsConsumer.titles = NewsConsumer.titles[1:] + NewsConsumer.titles[0]
                        direction = 0
                        font_size = 12
                        color = standard_text_color
                        box_height = 50

                    # if right hand is raised then move the text to the right
                    elif hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].x > 0.8 and len(results.multi_hand_landmarks) == 1:
                        direction = 1
                        font_size = 12
                        color = standard_text_color
                        box_height = 50

                    # if both hands are raised then increase the font size to 36pt and change the color
                    elif 0.2 < hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].x < 0.8 and len(results.multi_hand_landmarks) == 2:
                        font_size = 36
                        color = zoomed_text_color
                        box_height = 100

            else:
                # if no hands are detected then move the text to the left
                font_size = 12
                color = standard_text_color
                box_height = 50

            if direction == 0:
                NewsConsumer.titles = NewsConsumer.titles[1:] + NewsConsumer.titles[0]
            elif direction == 1:
                NewsConsumer.titles = NewsConsumer.titles[-1] + NewsConsumer.titles[:-1]

            # draw the text in the bottom left corner with a 2px border in black color. Get the font size from the variable font_size
            cv2.putText(image, NewsConsumer.titles, (10, image.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, font_size / 12, color, 2, cv2.LINE_AA)

            # draw another text at the right top corner to show the current time of the day in 24h format (HH:MM:SS) and in white color with a 1px border in black color. Change to font-size to 14pt with bold style
            cv2.putText(image, datetime.datetime.now().strftime("%H:%M:%S"), (image.shape[1] - 100, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)

            # put a logo in the top left corner
            logo = cv2.imread('news/logo.png')

            # resize the logo
            # match the size of the logo and time in the top right corner
            logo = cv2.resize(logo, (50, 50))

            # add the logo to the image
            image[0:logo.shape[0], 0:logo.shape[1]] = logo

            # Flip the image horizontally for a selfie-view display.
            cv2.imshow('AI News', image)

            # wait for the 'q' key to be pressed
            if cv2.waitKey(1) & 0xFF == ord('q') or cv2.waitKey(1) & 0xFF == ord('Q'):
                break

            # if 's' is pressed, save the image
            if cv2.waitKey(1) & 0xFF == ord('s') or cv2.waitKey(1) & 0xFF == ord('S'):
                cv2.imwrite('news.jpg', image)

    cap.release()

    # stop the video
    cv2.destroyAllWindows()

    # stop the consumer thread
    thread.join()

def main():
    """
    Main function
    """
    start_streaming()

if __name__ == '__main__':
    main()


import pytest
import torch
from mmcv import Config

from mmdp.models import build_model

def test_psnr():
    opt = Config()
    opt.model = dict(
        type="Pix2Pix",
        generator=dict(
            type="SIGGRAPHGenerator", input_nc=4, output_nc=2, classification=True
        ),
        gan_loss=dict(type="GANLoss", gan_type="lsgan"),
        l1_loss=dict(type="L1Loss"),
        l2_loss=dict(type="L2Loss"),
        lambda_A=1,
    )
    ab_quant = 10.0  # quantization factor
    ab_max = 110.0  # maximimum ab value
    A = 2 * ab_max / ab_quant + 1
    B = A
    opt.train_cfg = dict(
        ab_quant=10.0, ab_max=110.0, ab_norm=110.0, A=A, B=B, sample_p=1.0
    )
    opt.test_cfg = dict(
        A=1.0,
        l_norm=100.0,
        l_cent=50,
        ab_norm=110,
    )
    model = build_model(opt.model, train_cfg=opt.train_cfg, test_cfg=opt.test_cfg)
    with pytest.raises(AttributeError):
        test_data = torch.randn(1, 4, 176, 176)  # wrong size of input
        result = model.forward(test_data, psnr=True)
        print(result)
    with pytest.raises(AttributeError):
        visuals = model.get_current_visuals()
    model.real_a = torch.randn(1, 2, 16, 16)
    model.real_B = torch.randn(1, 2, 16, 16)
    model.fake_B_reg = torch.randn(1, 2, 16, 16)
    visuals = model.get_current_visuals()
    assert isinstance(visuals, dict)
    assert visuals["fake_reg"].size() == (1, 3, 16, 16)


import sqlite3
from sqlite3 import Error

# Функція для створення з'єднання до БД
def create_connection(db_file):
    conn = None
    try:
        conn = sqlite3.connect(db_file)
    except Error as e:
        print(e)
    return conn

# Вибір всіх значень в таблиці tasks
def select_all_dohodu(conn):
    sql = 'SELECT * FROM oblik_dohodiv'
    cur = conn.cursor()
    cur.execute(sql)
    rows = cur.fetchall()
    for row in rows:
        print(row)

# Вибрати доходи з сумою більше 1000
def select_high_sum_dohodu(conn):
    sql = 'SELECT * FROM oblik_dohodiv WHERE summa > 1000'
    cur = conn.cursor()
    cur.execute(sql)
    rows = cur.fetchall()
    for row in rows:
        print(row)

# Створення нового доходу
def create_dohid(conn, dohid):
    sql = ''' INSERT INTO oblik_dohodiv(summa, dzherelo, data)
              VALUES(?,?,?) '''
    cur = conn.cursor()
    cur.execute(sql, dohid)

# Оновлення рядка
def update_data_dohodu(conn, data):
    sql = ''' UPDATE oblik_dohodiv
              SET summa = ?, dzherelo = ?, data = ?
              WHERE id = ?'''
    cur = conn.cursor()
    cur.execute(sql, data)
    conn.commit()

# Видалення доходу за його номером
def remove_dohid(conn, remove_dohid):
    sql = ''' DELETE FROM oblik_dohodiv WHERE id = ? '''
    cur = conn.cursor()
    cur.execute(sql, remove_dohid)
    conn.commit()

# Головна функція, яка виконується під час запуску скрипта
def main():

    # Шлях до БД
    database = r"db1.db"

    # Встановлення з'єднання
    conn = create_connection(database)

    # Використовууючи встановлене з'єднання виконуються операції над БД
    with conn:
        print("\nВсі доходи (Номер, сума, джерело, дата)")
        select_all_dohodu(conn)
        print("\nДоходи з сумою більше 1000 (завдання, дата, приорітет)")
        select_high_sum_dohodu(conn)
        print("\nВставка нового рядка...")
        create_dohid(conn, ('1200','Біржа','15-12-2019'))
        print("\nВсі доходи (Номер, сума, джерело, дата)")
        select_all_dohodu(conn)
        print("\nЗміна рядка...")
        update_data_dohodu(conn, ('100','bonus','01-12-2019',1))
        print("\nВсі доходи (Номер, сума, джерело, дата)")
        select_all_dohodu(conn)
        print("\nВставка нового рядка...")
        create_dohid(conn, ('200','olx','12-12-2019'))
        print("\nВсі доходи (Номер, сума, джерело, дата)")
        select_all_dohodu(conn)
        print("\nВидалення рядка")
        remove_dohid(conn, (2,))
        print("\nВсі доходи (Номер, сума, джерело, дата)")
        select_all_dohodu(conn)
        input('Press ENTER to exit')

if __name__ == '__main__':
    main()


from django.urls import path

from .views import listCliente, CreateCliente, UpdateCliente, ExcluirCliente

urlpatterns = [
    path('clientes/', listCliente.as_view(), name='cliente'),
    path('clientes/adicionar/', CreateCliente.as_view(), name='criar_cliente'),
    path('clientes/atualizar/<pk>/', UpdateCliente.as_view(), name='atualizar_cliente' ),
    path('clientes/excluir/<pk>/', ExcluirCliente.as_view(), name='excluir_cliente' )
]


from django import template
from pycones.contentchunk.models import Chunk

"""
Usage:

On your template add:

{% load chunk_extras %}

Then you can reference the chunk content as:

{% chunk name="test" as my_content %}

and use it whenever you want

<div>
{{my_content}}
</div>

"""

register = template.Library()

@register.simple_tag(takes_context=True)
def chunk(context, name):
    try:
        o = Chunk.objects.get(name=name)
    except Chunk.DoesNotExist:
        return ""
    return o.text


import random
import os
import time
import speech_recognition as sr
from fuzzywuzzy import fuzz
import pyttsx3
import datetime
import webbrowser

opts = {
    "alias": ('морти','марте','мультик','мартин','мартина','марти','морки','marty','марки',"новости","martin","forte",'morty'),
    "tbr": ('скажи','расскажи','покажи','сколько','произнеси','запусти'),
    "cmds": {
        "ctime": ('сколько время','сейчас времени','который час'),
        "dela" : ('Как деля?','как дела','дела,''какое настроение','как у тебя дела'),
        "vk" : ('вк'),
        "yt" : ('Ютуб','утуб','youtube'),
        "ha" : ('Что-нибудь','мне скучно'),
        "git" : ('гит хаб','Git Hub','гит'),
        "c1" : ("спасибо"),
        "vastart" : ('валорант','valorant')

    }
}

# Функции
def speak(what):
    print( what )
    speak_engine.say( what )
    speak_engine.runAndWait()
    speak_engine.stop()

def callback(recognizer, audio):
    try:
        voice = recognizer.recognize_google(audio, language = "ru-RU").lower()

        print("[log] Распознано: " + voice)

        if voice.startswith(opts["alias"]):
            # Обращаются к Морти
            cmd = voice

            for x in opts['alias']:
                cmd = cmd.replace(x, "").strip()

            for x in opts['tbr']:
                cmd = cmd.replace(x, "").strip()

            # Распознаём и выполняем команду
            cmd = recognize_cmd(cmd)
            execute_cmd(cmd['cmd'])

    except sr.UnknownValueError:
        print("[log] Голос не распознан!")
    except sr.RequestError as e:
        print("[log] Неизвестная ошибка, проверь интернет!")

def recognize_cmd(cmd):
    RC = {'cmd': '', 'percent': 0}
    for c,v in opts['cmds'].items():

        for x in v:
            vrt = fuzz.ratio(cmd, x)
            if vrt > RC['percent']:
                RC['cmd'] = c
                RC['percent'] = vrt

    return RC

def execute_cmd(cmd):
    if cmd == 'ctime':
        # сказать текущее время
        now = datetime.datetime.now()
        speak("Сейчас " + str(now.hour) + ":" + str(now.minute))

    elif cmd == 'dela':
        # рассказать как дела
        speak("хорошо)")

    elif cmd == 'vk':

        speak("Включаю)")
        webbrowser.open('https://vk.com', new=2)

    elif cmd == 'git':

        speak("Решили по пограммировать?)")
        webbrowser.open('https://github.com', new=2)

    elif cmd == 'yt':
        speak("Включаю)")
        webbrowser.open('https://www.youtube.com/', new=2)
    elif cmd == 'ha':
        strings = ['меня заскамли? заскамили???', '-_-']
        speak(random.choice(strings))
    elif cmd == 'c1':
        speak("незачто)")
    elif cmd == 'vastart':
        file_path = r'E:\Riot Games\VALORANT\live\VALORANT.exe'
        os.system("start " + file_path)

    else:
        print('Команда не распознана, повторите!')

# Запуск
r = sr.Recognizer()
m = sr.Microphone(device_index = 1)

with m as source:
    r.adjust_for_ambient_noise(source)

speak_engine = pyttsx3.init()

# Только если установлены голоса для синтеза речи!
voices = speak_engine.getProperty('voices')
speak_engine.setProperty('voice', "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\TokenEnums\RHVoice\Evgeniy-Rus")

speak("Добрый день, повелитель Питона")
speak("Морти слушает")

while True:
    with m as source:
        audio = r.listen(source)
    callback(r, audio)
    time.sleep(0.1)


from datetime import datetime, timedelta
from .models import Allocation, AllocationDetail, Employee, Project
from django.db.models import Q

class TimeLineCalendar:
    class Booking:
        allocation_id = 0
        project = ''
        allocation_type = ''
        day_of_week = 0
        start_date = datetime.now()
        end_date = datetime.now()
        no_of_hours = 0
        color_code = ''

    def __init__(self, start_date=None, end_date=None):
        self.start_date = start_date
        self.end_date = end_date

    @staticmethod
    def get_month_header(start_date, end_date):
        delta = timedelta(days=1)
        month_td = ''
        current_month = start_date.strftime("%b")
        month_col_span = 0

        while start_date <= end_date:
            if current_month == start_date.strftime("%b"):
                month_col_span += 1
            else:
                month_td += '{}	'\
                    .format(month_col_span, current_month + "/" + start_date.strftime('%y'))
                current_month = start_date.strftime("%b")
                month_col_span = 1

            start_date += delta

        # loop ends once look reaches end_date hence not getting opportunity to add last month name. Due to that
        # added this manual entry in tail end of the loop
        month_td += '{}	'.format(
            month_col_span, current_month + "/" + start_date.strftime('%y'))

        return month_td

    @staticmethod
    def get_week_header(start_date, end_date):
        delta = timedelta(days=1)
        week_td = ''
        current_week = start_date.strftime("%W")
        week_col_span = 0

        while start_date <= end_date:
            if current_week == start_date.strftime("%W"):
                week_col_span += 1
            else:
                wc_date = start_date - delta * 2
                week_commencing = wc_date - timedelta(days=wc_date.weekday() % 7)

                week_td += '{}	'.format(
                    week_col_span, week_commencing.strftime("%d-%m"))
                current_week = start_date.strftime("%W")
                week_col_span = 1

            start_date += delta

        wc_date = start_date - delta * 2
        week_commencing = wc_date - timedelta(days=wc_date.weekday() % 7)
        # loop ends once look reaches end_date hence not getting opportunity to add last month name. Due to that
        # added this manual entry in tail end of the loop
        week_td += '{}	'.format(
            week_col_span, week_commencing.strftime("%d-%m"))

        return week_td

    @staticmethod
    def get_week_header_v2(start_date, end_date):
        delta = timedelta(days=1)
        week_td = ''
        current_week = start_date.strftime("%W")

        while start_date < end_date:
            if current_week != start_date.strftime("%W"):
                wc_date = start_date - delta * 2
                week_commencing = wc_date - timedelta(days=wc_date.weekday() % 7)

                week_td += '{}	'.format(
                    week_commencing.strftime("%d-%m"))
                current_week = start_date.strftime("%W")
            start_date += delta

        wc_date = start_date - delta * 2
        week_commencing = wc_date - timedelta(days=wc_date.weekday() % 7)
        # loop ends once look reaches end_date hence not getting opportunity to add last month name. Due to that
        # added this manual entry in tail end of the loop
        week_td += '{}	'.format(
            week_commencing.strftime("%d-%m"))

        return week_td

    @staticmethod
    def get_date_range_header(start_date, end_date):
        delta = timedelta(days=1)
        date_td = ''

        while start_date <= end_date:
            date_td += '{}	'.format(
                start_date.strftime("%a")[0])
            start_date += delta

        return date_td

    @staticmethod
    def is_weekend(date_to_check):
        if date_to_check.strftime("%a") == "Sat" or \
                date_to_check.strftime("%a") == "Sun":
            return True
        else:
            return False

    @staticmethod
    def get_week_commencing_date(date_to_check):
        delta = timedelta(days=1)
        wc_date = date_to_check - delta * 2
        return wc_date - timedelta(days=wc_date.weekday() % 7)

    def is_bank_holiday(self, date_to_check):
        bank_holidays = self.get_bank_holidays()

        if date_to_check.strftime("%d-%m-%Y") in bank_holidays:
            return True
        else:
            return False

    @staticmethod
    def get_bank_holidays():
        # TODO: Replace constant values with database
        bank_holidays = {}

        bank_holidays['21-04-2023'] = 'Eid-ul-Fitr'
        bank_holidays['15-08-2023'] = 'Independence Day'
        bank_holidays['30-08-2023'] = 'Rakshabandhan'
        bank_holidays['24-10-2023'] = 'Dusshera'
        bank_holidays['13-11-2023'] = 'New Year'
        bank_holidays['14-11-2023'] = 'Bhai Duj'
        bank_holidays['25-12-2023'] = 'Christmas'
        bank_holidays['15-01-2024'] = 'Vasi Uttrayan'
        bank_holidays['26-01-2024'] = 'Republic Day'
        bank_holidays['25-03-2024'] = 'Dhuleti'

        return bank_holidays

    def get_employee_allocation_detail(self, start_date, end_date, employee):

        query = Q(Q(employee=employee) & Q(end_date__gte=start_date) & Q(start_date__lte=end_date))

        allocations = Allocation.objects.filter(query)

        bookings = []
        for allocation in allocations:
            for detail in AllocationDetail.objects.filter(allocation=allocation):
                b = self.Booking()
                b.allocation_id = allocation.id
                b.project = allocation.project.name
                b.allocation_type = allocation.allocation_type.name
                b.day_of_week = detail.day_of_week
                b.start_date = allocation.start_date
                b.end_date = allocation.end_date
                b.no_of_hours = allocation.no_of_hours
                b.color_code = allocation.allocation_type.color_code
                bookings.append(b)

        return bookings

    def get_blank_tds(self, start_date, end_date):
        delta = timedelta(days=1)
        tr = ''
        while start_date <= end_date:
            if self.is_weekend(start_date):
                tr += '	'
            elif self.is_bank_holiday(start_date):
                tr += '	'
            else:
                tr += '	'

            start_date += delta
        return tr

    def get_employee_allocation_tr(self, start_date, end_date, allocation):
        delta = timedelta(days=1)
        timeline_tr = ''
        total_allocation_hours = 0
        total_hours_between_dates = 0

        allocation_detail = AllocationDetail.objects.filter(allocation=allocation)

        while start_date <= end_date:
            allocation_data = ''

            for detail in allocation_detail:
                if start_date.weekday() == detail.day_of_week and \
                        start_date >= allocation.start_date and \
                        start_date <= allocation.end_date:

                    class_name = ''
                    if self.is_bank_holiday(start_date):
                        class_name = 'bank-holiday'

                    allocation_data = '	'\
                        .format(allocation.allocation_type.color_code, class_name, allocation.id)

                    total_allocation_hours += allocation.no_of_hours
                    break
                else:
                    if self.is_weekend(start_date):
                        allocation_data = '	'
                    elif self.is_bank_holiday(start_date):
                        allocation_data = '	'
                    else:
                        allocation_data = '	'

            if not self.is_weekend(start_date):
                total_hours_between_dates += 8

            timeline_tr += allocation_data
            start_date += delta

        return timeline_tr

    def gettimelinecalendar(self):

        empty_tds = self.get_blank_tds(self.start_date, self.end_date)

        cal = '
'

        cal += '
'

        cal += '
'
        # Creating space for Name, Job Title, Date Of Joining, Skills, Date, etc... columns
        # cal += '		'
        cal += '{}	'\
            .format('Name')
        cal += '{}	'\
            .format('Job Title')
        cal += self.get_week_header(self.start_date, self.end_date)
        cal += '
'

        cal += '
'
        cal += self.get_date_range_header(self.start_date, self.end_date)
        cal += '
'

        cal += '
'

        cal += '
'

        timeline_tr = ''
        row_span = 0

        days_difference = (self.end_date-self.start_date)
        row_span = days_difference.days + 3

        flex_squad = '
'
        flex_squad += '{}	'.format(row_span, 'Flex Suad')
        flex_squad += '
'
        # Fetch employees who are not allocated in selected time frame and add them in the Flex Squad project
        emps = [allocation.employee.id for allocation in Allocation.objects.filter(start_date__lte=self.end_date,
                                                                                end_date__gte=self.start_date)]

        for emp in Employee.objects.all().exclude(id__in=emps):
            flex_squad += '
'
            flex_squad += '{}	'.format(emp.first_name)
            flex_squad += '{}	'.format(emp.job_title.name)
            flex_squad += empty_tds
            flex_squad += '
'

        cal += flex_squad

        for project in Project.objects.filter(start_date__lte=self.end_date, end_date__gte=self.start_date)\
                .order_by('name'):
            timeline_tr = '
'
            timeline_tr += '{}- Project Type:{}, Start date:{}, ' \
                           'End date:{}, Commercial Status: {}	'.format(
                            row_span, project.name, project.project_type.name,
                            project.start_date, project.end_date, project.commercial_status)

            timeline_tr += '
'

            for allocation in Allocation.objects.filter(project=project, start_date__lte=self.end_date,
                                                        end_date__gte=self.start_date).order_by('employee__first_name'):
                timeline_tr += '
'
                timeline_tr += "{}	".format(allocation.employee.first_name)
                timeline_tr += "{}	".format(allocation.employee.job_title.name)
                timeline_tr += self.get_employee_allocation_tr(self.start_date, self.end_date, allocation)
                timeline_tr += '
'

            cal += timeline_tr

        cal += '
'
        cal += '
'

        return cal

    def capacitycalendar(self):

        cal = '
'
        cal += '
'

        cal += '
'
        cal += '{}	' \
            .format('Name')
        cal += '{}	' \
            .format('Job Title')
        cal += '{}	' \
            .format('Skills')
        cal += self.get_week_header_v2(self.get_week_commencing_date(self.start_date), self.get_week_commencing_date(self.end_date))
        cal += '
'
        cal += '
'

        cal += '
'

        delta = timedelta(days=1)
        start_date = self.get_week_commencing_date(self.start_date)
        end_date = self.get_week_commencing_date(self.end_date)

        for employee in Employee.objects.all():

            allocation_detail = self.get_employee_allocation_detail(self.get_week_commencing_date(self.start_date),
                                                                    self.get_week_commencing_date(self.end_date),
                                                                    employee)
            total_allocation_hours = 0
            employee_tr = '
'
            employee_tr += '{}	'.format(employee.first_name)
            employee_tr += '{}	'.format(employee.job_title.name)
            employee_tr += '{}	'.format(','.join([s.name for s in employee.skills.all()]))

            start_date = self.get_week_commencing_date(self.start_date)
            end_date = self.get_week_commencing_date(self.end_date)
            current_week = self.start_date.strftime("%W")
            while start_date <= end_date:

                for detail in allocation_detail:
                    if start_date.weekday() == detail.day_of_week and \
                            start_date >= detail.start_date and \
                            start_date <= detail.end_date:

                        total_allocation_hours += detail.no_of_hours

                if start_date.strftime("%a") == 'Sun':
                    allocation_percentage = (total_allocation_hours / 40)

                    # progressbar = '
'
                    # progressbar += '
'.format(allocation_percentage)
                    # progressbar += '{:.0%}
'.format(allocation_percentage)
                    # progressbar += '
'

                    current_week = start_date.strftime("%W")

                    # employee_tr += "{}	".format(progressbar)
                    employee_tr += "{} %	".format(self.get_heatmap_color(allocation_percentage*100), allocation_percentage*100)
                    total_allocation_hours = 0

                start_date += delta

            employee_tr += "
"
            cal += employee_tr

        cal += '
'
        cal += '
'

        return cal

    @staticmethod
    def get_heatmap_color(value):
        heatmap_color = ''

        if value>=0 and value<=10:
            heatmap_color='#cc0000'
        elif value>=11 and value<=20:
            heatmap_color='#ff0000'
        elif value>=21 and value<=30:
            heatmap_color = '#ff6600'
        elif value>=31 and value<=40:
            heatmap_color = '#ff9900'
        elif value>=41 and value<=50:
            heatmap_color = '#ffc000'
        elif value>=51 and value<=60:
            heatmap_color = '#ffd966'
        elif value>=61 and value<=70:
            heatmap_color = '#ffe699'
        elif value>=71 and value<=80:
            heatmap_color = '#c6e0b4'
        elif value>=81 and value<=90:
            heatmap_color = '#a9d08e'
        elif value>=91 and value<=100:
            heatmap_color = '#339966'
        elif value>100:
            heatmap_color = '#1654b0'

        return heatmap_color


"""
Scenario 3:
1. Open a browser of your choice say Mozilla Firefox
2. Navigate to http://jqueryui.com/droppable/ webpage.
3. Consider “Drag me to my target” as a source and “Drop here” as a target.
4. Write a code to perform drag and drop operation from source to target.
5. After drag and drop verify the operation is successfully by checking the color property of CSS and
also verify text change. (Use assert statement to verify that color and text are as expected.)
"""

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.color import Color
from test_selScenarios.test_scenario3.DragNDropPage import DragNDropPage

class Test_DragNDrop:
    def dragNdrop(self):
        driver=webdriver.Chrome(executable_path="D:\\TestYantra\\python\\TY\\chromedriver.exe")
        driver.get("http://jqueryui.com/droppable/")
        driver.maximize_window()
        time.sleep(5)
        frame=driver.find_element_by_tag_name("iframe")
        driver.switch_to.frame(frame)
        source=driver.find_element_by_id("draggable")
        target=driver.find_element_by_id("droppable")
        action=ActionChains(driver)
        action.drag_and_drop(source,target).perform()
        time.sleep(5)
        msg=driver.find_element_by_xpath("//div[@id='droppable']/p").text
        assert 'Dropped' in msg, "The text is not changed"
        print("The text is changed")
        bgcolor=target.value_of_css_property("background")
        rgb=bgcolor.split('n')
        hexbgcolor=Color.from_string(rgb[0]).hex
        assert '#fffa90' == hexbgcolor,'The color is not changed'
        print("The color is changed")

ob=Test_DragNDrop()
ob.dragNdrop()