from core.models import BaseModel
from core.models import Keyword

from google.appengine.ext import ndb

class Variable(BaseModel):
    name = ndb.StringProperty()
    tipo = ndb.StringProperty()
    keywords = ndb.KeyProperty(Keyword, repeated=True)
    description = ndb.StringProperty()

    @classmethod
    def get_all(cls):
        return Variable.query().fetch()

    @classmethod
    def create(cls, name, tipo, keywords, description):
        if name and tipo:
            variable = Variable(
                name=name,
                tipo=tipo,
                keywords=keywords,
                description=description
                )
            variable.put()
            return variable
        else:
            return False

    @classmethod
    def update(cls, variable, name, tipo, keywords, description):
        if variable:
            variable.name = name
            variable.tipo = tipo
            variable.keywords = [Keyword.update(keyword) for keyword in keywords if keyword.name]
            variable.description = description
            variable.put()
            return variable
        else:
            return False


from flask import Blueprint, jsonify
from flask_login import login_required
from app.models import db, User
from flask_login import current_user
from app.forms import UserForm
from app.models.request import FriendRequest

user_routes = Blueprint('users', __name__)

@user_routes.route('/')
@login_required
def users():
    users = User.query.all()
    return {'users': [user.to_dict() for user in users]}

@user_routes.route('/<user_id>/friends')
@login_required
def users_friends(user_id):
    users_friends = User.query.join(FriendRequest,
    (FriendRequest.recipient_id == User.id) | (FriendRequest.sender_id == User.id)).filter(
    (FriendRequest.sender_id == user_id) | (FriendRequest.recipient_id == user_id)
    ).filter(FriendRequest.status == 'accepted').filter(User.id != user_id).all()

    return {'friends': [friend.to_dict() for friend in users_friends]}

@user_routes.route('/mutualfriends/<user_id>')
@login_required
def mutual_friends(user_id):

    current_user_friends = User.query.join(FriendRequest,
    (FriendRequest.recipient_id == User.id) | (FriendRequest.sender_id == User.id)).filter(
    (FriendRequest.sender_id == current_user.id) | (FriendRequest.recipient_id == current_user.id)
    ).filter(FriendRequest.status == 'accepted').filter(User.id != current_user.id).all()

    users_friends = User.query.join(FriendRequest,
    (FriendRequest.recipient_id == User.id) | (FriendRequest.sender_id == User.id)).filter(
    (FriendRequest.sender_id == user_id) | (FriendRequest.recipient_id == user_id)
    ).filter(FriendRequest.status == 'accepted').filter(User.id != user_id).all()

    friend_arr = []
    for friend in current_user_friends:
        for user_friend in users_friends:
            if friend.to_dict() == user_friend.to_dict():
                friend_arr.append(friend)
    return {'users': [user.to_dict() for user in friend_arr]}

@user_routes.route('/<int:id>', methods=['PUT'])
@login_required
def update_user(id):
    user_form = UserForm()

    user = User.query.get(id)
    user.profile_image_url = user_form.data['profile_image_url']
    db.session.commit()

    return user.to_dict()

@user_routes.route('/<int:id>')
@login_required
def user(id):
    user = User.query.get(id)
    return user.to_dict()


from PIL import Image

#opening the reference image
reference_name = "uno_reverse_resized"
reference_path = f"reference_images/{reference_name}.jpg"

reference_image = Image.open(reference_path)
reference_pixels = list(reference_image.getdata())
reference_size = reference_image.size # getting the refence image's size in (x-pixles,y-pixels) format

# making a greyscale version of the reference image
reference = []
for p in reference_pixels:
    avg = (p[0]+p[1]+p[2])/3
    reference.append((avg,avg,avg))

# defining the ascii characters that correspond to certain 'levels of grey' going from black --> white
raw = """$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'. """
ascii_sorted = [x for x in raw]

output = """"""
output = ""
for y in range(reference_size[1]):
    if y % 2 == 0:
        continue
    for x in range(reference_size[0]):
        pixel = reference[reference_size[0]*y + x][0]
        output += ascii_sorted[round(pixel / 3.643) - 1]
    output += "\n"

# writing the output to a text file
with open(f"outputs/output_{reference_name}.txt","w") as f:
    f.write(output)


import numpy as np
from sklearn.metrics import RocCurveDisplay
import matplotlib.pyplot as plt
import os

cert_or_unm = 'snd-cert' if False else 'snd-unm'
path = os.path.join('.', 'negative-selection', 'syscalls', cert_or_unm)
jarfile = os.path.join('.', 'negative-selection', 'negsel2.jar')

filename = rf'{path}\{cert_or_unm}'
trainfile = f'{filename}-proc.train'
v = 1
testfile = f'{filename}.{v}-proc.test'
outfile = f'{filename}.{v}.out'

n = 12
r = 4

def preprocess(file):
    chunk = n
    step = 1
    result = ""
    with open(file) as f:
        for line in f.read().split():
            if len(line) < chunk:
                result += line*(chunk//len(line))+line[:chunk % len(line)]+'\n'

            i = 0
            while i+chunk <= len(line):
                result += line[i:i+chunk]+'\n'
                i += step
            result += '\n'

    return result[:-1]

def plot_base():

    plt.plot([0, 1], [0, 1], "k--", label="chance level (AUC = 0.5)")
    plt.axis("square")
    plt.grid(alpha=0.5)
    plt.title(f'natural selection with n={n}')
    plt.legend(loc='lower right')
    plt.tight_layout()

    plt.show()

if __name__ == '__main__':
    # preprocess train and test
    with open(trainfile, 'w') as out:
        out.write(preprocess(f'{filename}.train'))

    with open(testfile, 'w') as out:
        out.write(preprocess(f'{filename}.{v}.test'))

    ax = plt.gca()
    for r in range(3, 9):
        # negative selection
        os.system(
            f'cmd /c "java -jar {jarfile} -self {trainfile} -alphabet file://{filename}.alpha -n {n} -r {r} -c -l < {testfile} > {outfile}')

        # post process output
        with open(outfile) as file:
            lines = file.read().split('NaN')
            pred = []
            for l in lines:
                scores = np.array(l.split(), dtype=np.float)
                pred.append(np.max(scores))

        pred = np.array(pred)

        with open(f'{filename}.{v}.labels') as file:
            label = np.array(file.read().split(), dtype=np.float)

        RocCurveDisplay.from_predictions(
            label, pred, name=f'negative selection (r={r})', ax=ax)

    plot_base()


from typing import List, Tuple, Optional

from collections import defaultdict,deque
from functools import cache
import heapq
from sortedcontainers import SortedDict,SortedList

class Solution:
    def minReverseOperations(self, n: int, p: int, banned: List[int], k: int) -> List[int]:
        ret = [-1]*n
        ret[p] =0
        visit={}
        visit[p] =1
        for a in banned:
            visit[a] =1
        cnt =0
        left, right = p,p
        while left != 0  or right != n-1:
            cnt +=1
            ll,rr = left,right
            #print(ret,visit)
            for i in range(max(ll-k+1,0),ll+1):
                if i + k-1>=n:continue
                nexp= i + k-1- (ll-i)
                #print(nexp,ll,i,"ll")
                if nexp not in visit:
                    ret[nexp] = cnt
                    visit[nexp] =1
                    left = min(left,nexp)
                    right = max(nexp,right)
            for i in range(max(0,rr-k+1),rr+1):
                if i + k-1>=n:continue
                nexp = i + k -1-(rr-i)
                #print(nexp,i,rr,"rr")
                if nexp not in visit :
                    ret[nexp] =cnt
                    visit[nexp] =1
                    right = max(right ,nexp)
                    left = min(left,nexp)
            print(ret,left,right,ll,rr)
            if ll == left and rr ==right:
                break
        return ret

re =Solution().minReverseOperations(n = 5, p = 0, banned = [], k = 4)
print(re)


from typing import List
from . import ImageDisplayOptions, UrlTypeHint
from .AndroidApp import AndroidApp
from .CarouselBrowseItem import CarouselBrowseItem
from .Image import Image
from .OpenUrlAction import OpenUrlAction

class CarouselBrowse(dict):
    """
    {
      "item_list": [
        {
          object(Item)
        }
      ],
      "imageDisplayOptions": enum(ImageDisplayOptions)
    }
    """

    def __init__(self, image_display_options: ImageDisplayOptions, carousel_browse_items: List[CarouselBrowseItem]):
        super().__init__()

        super(CarouselBrowse, self).__init__()

        self.image_display_options = image_display_options

        self['item_list'] = list()
        self.add_items(carousel_browse_items)

    @property
    def image_display_options(self):
        return ImageDisplayOptions(self.get('imageDisplayOptions'))

    @image_display_options.setter
    def image_display_options(self, image_display_options: ImageDisplayOptions):
        self['imageDisplayOptions'] = image_display_options.name

    def add_items(self, items: List[CarouselBrowseItem]) -> List[CarouselBrowseItem]:
        for item in items:
            assert isinstance(item, CarouselBrowseItem)
            self['item_list'].append(item)

        return self['item_list']

    def add_item(self, title: str = '', description: str = '', footer: str = '', image_url: str = '',
                 accessibility_text: str = '', image_height: int = 0, image_width: int = 0,
                 action_url: str = '', android_app_package_name: str = '', android_versions_list: List[int] = None,
                 type_hint: UrlTypeHint = None) -> CarouselBrowseItem:
        item = CarouselBrowseItem(title=title, description=description,
                                  image=Image(url=image_url, accessibility_text=accessibility_text,
                                              height=image_height, width=image_width), footer=footer,
                                  open_url_action=OpenUrlAction(action_url=action_url,
                                                                android_app=AndroidApp(
                                                                    package_name=android_app_package_name,
                                                                    versions_list=android_versions_list),
                                                                type_hint=type_hint))
        self['item_list'].append(item)
        return item


import os
import opts
import json
from PIL import Image
import subprocess
import platform
import shutil

from constants import VIDEO_FNAME, VIDEO_META_FNAME

output_path = os.path.join(os.path.dirname(__file__), opts.global_opts.outpath)
ops = ['txt2img', 'img2img', 'img2vid', 'editing']

op_opt_keys = {
    # "txt2img": [k for k in opts.txt2img_opts_dict.keys() if k not in ['num_batches']],
    "txt2img": list(opts.txt2img_opts_dict.keys()),
    "img2img": list(opts.img2img_opts_dict.keys()),
    "img2vid": list(opts.img2vid_opts_dict.keys()),
    "editing": list(opts.editing_opts_dict.keys()),
}

def is_video(op) -> bool:
    return op == 'img2vid'

def get_start_idx(op) -> int:
    if is_video(op):
        return num_video(get_op_path(op))
    else:
        return num_with_ext(get_op_path(op), "png")

def make_output_dir():
    for op in ops:
        op_path = get_op_path(op)
        os.makedirs(op_path, exist_ok=True)

def num_with_ext(path, ext) -> int:
    return len([f for f in os.listdir(path) if f.endswith(ext)])

def num_video(path) -> int:
    return len([f for f in os.listdir(path) if f.startswith('vid_')])

def get_op_path(op) -> str:
    assert op in ops, f"op {op} not in {ops}"
    return os.path.join(output_path, op)

def get_folder_path(op, folder_id) -> str:
    op_path = get_op_path(op)
    folder_name = idx_video_folder(op, folder_id)
    folder_path = os.path.join(op_path, folder_name)
    # print('found folder path id', folder_id, folder_path)
    return folder_path

def prefix_str(idx) -> str:
    return f"{idx:05d}"

def idx_name(op, idx, ext=None) -> str:
    # open the directory and load list of all .png files
    # sort png files by name
    # if idx is in the list, return the name
    # else, return int(name of last file) + (idx - len(list)) + 1
    files = os.listdir(get_op_path(op))
    files = sorted([f for f in files if f.endswith('.png')])
    if idx < len(files):
        prefix = int(files[idx].split('.')[0])
    elif len(files) == 0:
        prefix = idx
    else:
        prefix = int(files[-1].split('.')[0]) + (idx - len(files)) + 1
    if ext is None:
        return prefix_str(prefix)
    return f"{prefix_str(prefix)}.{ext}"

def idx_video_folder(op, idx, full_name=True) -> str:
    assert is_video(op)
    files = os.listdir(get_op_path(op))
    files = sorted([f for f in files if f.startswith('vid_')])
    if idx < len(files):
        prefix = int(files[idx].split('_')[1])
    elif len(files) == 0:
        prefix = idx
    else:
        prefix = int(files[-1].split('_')[1]) + (idx - len(files)) + 1
    if full_name:
        return f"vid_{prefix_str(prefix)}"
    return prefix_str(prefix)

def job_to_start_idx(op, job_id):
    name_ids = [int(f.split('.')[0])
                for f in os.listdir(get_op_path(op)) if f.endswith('.png')]
    return len([i for i in name_ids if i < job_id])

def save_img(img, opt, **kwargs):
    assert opt.context in ops
    op = opt.context
    op_path = get_op_path(op)
    idx = num_with_ext(op_path, "png")
    prefix = idx_name(op, idx)
    print('saving new image', idx, prefix)
    print(os.path.join(op_path, prefix+'.png'))
    img.save(os.path.join(op_path, prefix+'.png'))
    meta = {k: v for k, v in kwargs.items()}
    for k in op_opt_keys[opt.context]:
        meta[k] = getattr(opt, k)
    meta['job_id'] = int(prefix) - meta['idx_in_job']
    with open(os.path.join(op_path, prefix+'.json'), "w") as f:
        json.dump(meta, f)
    return img, meta, idx

def save_img2vid_data(op, meta):
    assert op == 'img2vid'
    folder_id = len(os.listdir(get_op_path(op)))
    folder_path = get_folder_path(op, folder_id)
    os.makedirs(folder_path, exist_ok=True)
    with open(os.path.join(folder_path, VIDEO_META_FNAME), "w") as f:
        json.dump(meta, f)

def save_img2vid_img(img, opt, folder_id):
    op = opt.context
    assert op == 'img2vid'
    folder_path = get_folder_path(op, folder_id)
    idx = num_with_ext(folder_path, "png")
    fname = f'{prefix_str(idx)}.png'
    print('saving new image', idx, fname)
    img.save(os.path.join(folder_path, fname))
    return img, idx

def create_video(opt, folder_id):
    op = opt.context
    assert op == 'img2vid'
    folder_path = get_folder_path(op, folder_id)
    img_path = f'{folder_path}/%05d.png'
    vid_path = f'{folder_path}/video.mp4'
    if platform.system() != 'Windows':
        img_path = f'"{img_path}"'
        vid_path = f'"{vid_path}"'
    cmd = f'ffmpeg -y -vcodec png -r {opt.frame_rate} -i "IMGPATH" -c:v libx264 -vf fps={opt.frame_rate} -pix_fmt yuv420p -crf 17 -preset veryfast "VIDPATH"'
    cmd = cmd.split()
    cmd[cmd.index('"IMGPATH"')] = img_path
    cmd[cmd.index('"VIDPATH"')] = vid_path
    print('video command:', ' '.join(cmd))
    process = subprocess.Popen(
        cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()
    if process.returncode != 0:
        print(stderr)
        raise RuntimeError(stderr)
    print('video created successfully.')

def load_img(idx, op):
    assert op in ops
    op_path = get_op_path(op)
    name_str = idx_name(op, idx)
    print('loading image', idx, name_str)
    name_id = int(name_str)
    img = Image.open(os.path.join(op_path, name_str+'.png'))
    with open(os.path.join(op_path, name_str+'.json'), "r") as f:
        meta = json.load(f)
    # meta['name_id'] = name_id
    # meta['job_id'] = name_id - meta['idx_in_job']
    return img, meta, idx

def load_vid(idx, op):
    assert op in ops
    folder_path = get_folder_path(op, idx)
    print('loading vid', idx, folder_path)
    with open(os.path.join(folder_path, VIDEO_META_FNAME), "r") as f:
        meta = json.load(f)
    with open(os.path.join(folder_path, VIDEO_FNAME), "rb") as f:
        mov = f.read()
    return mov, meta, idx

def delete_video(idx, op):
    assert is_video(op)
    op_path = get_op_path(op)
    name_str = idx_video_folder(op, idx)
    shutil.rmtree(os.path.join(op_path, name_str))
    return None, None

def delete_image(idx, op):
    assert op in ops
    if is_video(op):
        return delete_video(idx, op)
    op_path = get_op_path(op)
    name_str = idx_name(op, idx)
    with open(os.path.join(get_op_path(op), name_str+'.json'), 'r') as f:
        meta = json.load(f)
        job_id = meta['job_id']
    with open(os.path.join(get_op_path(op), f'batch_{prefix_str(job_id)}.json'), 'r') as f:
        job_meta = json.load(f)
    job_meta['job_size'] -= 1
    with open(os.path.join(get_op_path(op), f'batch_{prefix_str(job_id)}.json'), 'w') as f:
        json.dump(job_meta, f)
    os.remove(os.path.join(op_path, name_str+'.png'))
    os.remove(os.path.join(op_path, name_str+'.json'))
    return job_id, job_meta


import csv
from ...models import Solution
from django.core.management.base import BaseCommand, CommandError

class Command(BaseCommand):

    def handle(self, *args, **options):
        Solution.objects.all().delete()

        with open("drawdown-stats.csv", "r+") as f:
            for index, row in enumerate(csv.reader(f)):
                if index == 0 or index == 83:
                    continue
                ss = Solution()
                ss.rank = row[0]
                ss.solution = row[1]
                ss.sector = row[2]
                ss.co2_reduction = row[3]
                ss.net_cost = row[4]
                ss.savings = row[5]
                ss.save()


"""

strategy to solve the problem:
    #daily temperature problem: find the number of day from the ith day will have warmer temperature

    #using stack strategy:
        res (list): to hold result list
        stack (list): to hold the index, temperature of daly
        #for each iteration we will know the index (date) and it's temperature
            #if
"""

#############################################reference solution
from typing import List
class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        res = [0] * len(temperatures)
        stack = []  # pair: [temp, index]

        for i, t in enumerate(temperatures):
            while stack and t > stack[-1][0]: #find result of all date in stack
                stackT, stackInd = stack.pop()
                res[stackInd] = i - stackInd
            stack.append((t, i)) #build stack
        return res


import os
import sys
import pandas as pd
import numpy as np

os.environ['CUDA_VISIBLE_DEVICES'] = '0'

_cur_dir = os.path.split(os.path.abspath(__file__))[0]
_root_dir = os.path.split(_cur_dir)[0]
sys.path.append(_root_dir)

from server.interfaces.data.data_add_values import data_add_values, DataInput
from server.interfaces.data.data_add_avg_values import data_add_avg_values, DataAvgInput
from config.path import VALUE_DIR
from core.processor import d_column_2_zh_name
from core.encode import encode

if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1].lower() == '--drop_all':
        from lib.milvus import o_milvus

        o_milvus.recreate_collection(o_milvus.VALUE_NAME)
        o_milvus.recreate_collection(o_milvus.AVG_VALUE_NAME)

    file_names = list(filter(lambda x: not x.startswith('.'), os.listdir(VALUE_DIR)))

    # 添加 avg value embeddings 到 milvus
    for file_name in file_names:
        column = file_name.split(".")[0]

        # 过滤王永和端不支持的字段
        if column not in d_column_2_zh_name:
            continue

        file_path = os.path.join(VALUE_DIR, file_name)

        df = pd.read_csv(file_path)

        # 过滤空数据
        tmp_pairs = list(map(lambda x: [f'{x[0]}' if pd.notna(x[0]) else '', x[1]], zip(df['text'], df['freq'])))
        text_freq_pairs = list(filter(lambda x: x[0].strip(), tmp_pairs))
        texts, freqs = list(zip(*text_freq_pairs))

        # encode 成 embedding
        embeddings = encode(texts)
        data = list(map(lambda x: {'emb': x[0], 'freq': x[1]}, zip(embeddings, freqs)))

        # 获取 加权平均向量
        total_freq = np.sum(np.sqrt(np.array(freqs)))
        embeddings = np.array(list(map(lambda x: np.array(x['emb']) * np.sqrt(x['freq']) / total_freq, data)))
        weighted_sum_embeddings = np.sum(embeddings, axis=0)
        weighted_sum_embeddings = list(map(float, weighted_sum_embeddings))

        # 插入 初始加权平均向量
        print(f'inserting avg embeddings for {column}')
        print(data_add_avg_values(DataAvgInput(
            column=column,
            embedding=weighted_sum_embeddings,
            count=total_freq,
        )))

    # 添加 value embeddings 到 milvus
    for file_name in file_names:
        column = file_name.split(".csv")[0]

        # 过滤王永和端不支持的字段
        if column not in d_column_2_zh_name:
            continue

        file_path = os.path.join(VALUE_DIR, file_name)

        df = pd.read_csv(file_path)
        texts = list(map(lambda x: f'{x}' if pd.notna(x) else '', df['text']))
        texts = list(filter(lambda x: x.strip(), texts))

        if texts:
            print(data_add_values(DataInput(
                column=column,
                data=texts
            )))

    print('\ndone')


#required modules for the program
import pyttsx3
import datetime
import speech_recognition as sr
import webbrowser
import wikipedia
import os
import requests
# import googlesearch

#engine
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[0].id)

def speak(audio):
    engine.say(audio)
    #run and wait for the user's command
    engine.runAndWait()

#greeting programme to wish the user
def wishMe():

    #whishing according to time of the day
    hour = int(datetime.datetime.now().hour)
    if hour >= 0 and hour < 12:
        print("Mark : Good Morning!,hey there")
        speak("Good Morning!, hey there")

    elif hour >= 12 and hour < 18:
        print("Mark : Good Afternoon!,hey there")
        speak("Good Afternoon!,hey there")

    else:
        print("Mark : Good Evening!,hey there")
        speak("Good Evening!,hey there")

    print("Mark : I am Mark. your virtual assistant, please tell me how can i help you? ")
    speak("I am Mark. your virtual assistant, please tell me how can i help you? ")

def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Mark : am Listening...")
        speak("am Listening")
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source, duration=1)
        audio = r.listen(source)

    try:
        print("Mark : trying to Recognize")
        speak("trying to Recognize")
        query = r.recognize_google(audio,key= None , language ='en-US')
        print("user said:\n",{query})

    except Exception as e:
        print(e)
        print("Mark : say that again please..")
        speak("say that again please..")
        return "None"
    return query

if  __name__ == "__main__":
    wishMe()
    while True:
        query = takeCommand().lower()

        #searches wikipedia for answer
        if 'wikipedia' in query:
            print('Mark : searching wikipedia...give me some time please')
            speak('searching wikipedia...give me some time please')
            query = query.replace("wikipedia","")
            results = wikipedia.summary(query, sentences = 2)
            print("Mark : According to wikipedia\n")
            speak("According to wikipedia")
            print("Mark : ")
            print(results)
            speak(results)

        #open's youtube in the webbrowser
        elif 'open youtube' in query:
            print('Mark : opening youtube , just for u boss')
            speak('opening youtube , just for u boss')
            webbrowser.open("https://www.youtube.com")

        #open's google in the webbrowser
        elif 'open google' in query:
            print("Mark : opening google , !!searching something usefull !! ")
            speak("opening google , !!searching something usefull !! ")
            webbrowser.open("https://www.google.com")

        #open's instagram in the webbrowser
        elif 'open instagram' in query:
            print("Mark : opening instagram, double tap to like <3 !!")
            speak("opening instagram, double tap to like <3 !!")
            webbrowser.open("https://www.instagram.com")

        #plays songs from local DIR
        elif 'play songs' in query:
            print("Mark : ok playing, your songs, have fun !!")
            speak("ok playing, your songs, have fun !!")
            music_dir = 'C:\\Users\\jayes\\Desktop\\my stuff\\my songs'
            music = os.listdir(music_dir)
            print(music)
            os.startfile(os.path.join(music_dir,music[0]))

        #tells the current local time in 24hr format
        elif 'the time' in query:
            strTime = datetime.datetime.now().strftime("%H:%M:%S")
            print("boss , the time is.. ",{strTime})
            speak(f"boss , the time is.. {strTime}")

        #opens gtav.exe from the source
        elif 'open gta ' in query:
            print("Mark : opening GTA v , dont play for time boss !!")
            speak("opening GTA five , dont play for time boss !!")
            gta_path = 'D:\\Games\\Grand Theft Auto V'
            os.startfile(gta_path)

        #to greet mom
        elif 'mummy' in query:
            print("Mark : namaste mummyji")
            speak("namaste mummyji")

        #to greet dad
        elif 'papa' in query:
            print("Mark : Namaste papaji")
            speak("namaste papaji")

        #to greet sir
        elif 'sir' in query:
            print("Mark : vanakam sir")
            speak("vanakam sir")

        elif 'joke' in query:
            res = requests.get(
                'https://icanhazdadjoke.com/',
                headers={"Accept":"application/json"}
                )
            if res.status_code == requests.codes.ok:
                takeCommand(str(res.json()['jokes']))
            else:
                takeCommand("oops!I ran out of jokes")

        elif  'jayesh' in query:
            print("Mark : jayesh seth is a talented boy ")
            speak("jayesh seth is a talented boy")

        elif 'who are you' in query:
            print("Mark : iam a virtual assistant made by MR.jayesh seth")
            speak("iam a virtual assistant made by MR.jayesh seth")

        #terminates/exits the programme
        elif 'bye' in query:
            print("Mark : good bye, see you soon")
            speak("good bye, see you soon")
            exit()


from pathlib import Path
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.transforms as mtransforms
from scipy.signal import savgol_filter
from mne import read_evokeds
from mne.viz import plot_topomap

root = Path(__file__).parent.parent.parent.absolute()

fig = plt.figure(figsize=(8, 4))
gs = gridspec.GridSpec(4, 8)
ax = {}
ax["A"] = plt.subplot(gs[1:3, :2])
ax["B"] = plt.subplot(gs[:2, 2:4])
ax["E"] = plt.subplot(gs[2:, 2:4])
ax["C"] = plt.subplot(gs[:2, 4:6])
ax["F"] = plt.subplot(gs[2:, 4:6], sharey=ax["E"])
ax["D"] = plt.subplot(gs[:2, 6:])
ax["G"] = plt.subplot(gs[2:, 6:], sharey=ax["E"])
plt.subplots_adjust(hspace=0.3)
csd = read_evokeds(root / "results" / "group_csd-ave.fif")
X = csd[-1].data
X -= X.mean(axis=1, keepdims=True)  # subtract each channels mean
cov = (X @ X.T) / (X.shape[1] - 1)

eig_vals, eig_vecs = np.linalg.eig(cov)
# scale eigenvalues to percent variance explained
eig_vals = (eig_vals / eig_vals.sum()) * 100
ax["A"].plot(eig_vals, color="black", linewidth=1.5)
ax["A"].set(ylabel="Variance explained [%]", xlabel="Component", xlim=(0, 20))

labels = ["B", "C", "D", "E", "F", "G"]
for i in range(3):
    plot_topomap(eig_vecs[:, i], csd[0].info, show=False, axes=ax[labels[i]])
    for j in range(4):
        X = csd[j].data
        stc = X.T @ (eig_vecs[:, i] * eig_vals[i])
        stc = savgol_filter(stc, 50, 8)
        ax[labels[i + 3]].plot(csd[0].times - 1.0, stc, label=csd[j].comment)
        ax[labels[i + 3]].set(yticks=[])
        ax[labels[i + 3]].set_title(
            f"PC{i+1}: {eig_vals[i].round(1)}%", fontsize="medium"
        )
ax["F"].set_xlabel("Time [s]")
ax["F"].legend(loc="lower right", fontsize="xx-small")
ax["G"].set_ylabel("Amplitude [a.u.]")
ax["G"].yaxis.set_label_position("right")

for label in ax.keys():
    trans = mtransforms.ScaledTranslation(10 / 72, -5 / 72, fig.dpi_scale_trans)
    ax[label].text(
        0.0,
        1.0,
        label,
        transform=ax[label].transAxes + trans,
        fontsize=10,
        weight="bold",
        verticalalignment="top",
        fontfamily="serif",
    )

plt.savefig(root / "results" / "plots" / "components.png", dpi=300, bbox_inches="tight")


import os
import uuid
import json
import s3_handler
import processing_requests as pr

import pika
import aio_pika
import asyncio
import logging

from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.contrib.middlewares.logging import LoggingMiddleware

from config import TOKEN, inline_choose_filters, MESSAGES, input_path
from state import BotStates

bot = Bot(token=TOKEN)
dp = Dispatcher(bot, storage=MemoryStorage())
dp.middleware.setup(LoggingMiddleware())
s3handler = s3_handler.s3Handler()
amqp_url = os.environ["AMQP_URL"]

conn_params = pika.URLParameters(amqp_url)

async def consume():

    connection2 = None
    while connection2 == None:
        try:
            connection2 = await aio_pika.connect_robust(amqp_url)
            logging.info('Connected')
        except:
            logging.info('Waiting for connection')
            await asyncio.sleep(50)

    queue_name = "second"
    async with connection2:
        channel2 = await connection2.channel()
        await channel2.set_qos(prefetch_count=10)

        queue = await channel2.declare_queue(queue_name)

        async with queue.iterator() as it:
            async for message in it:
                async with message.process():
                    input = pr.requests_queue.json_deserializer(message.body)
                    print(input)
                    outp_path = input['outp_path']
                    s3handler.download(input['outp_path'])
                    photo = open(outp_path, 'rb')
                    await bot.send_photo(input["chat_id"], photo)
                    await dp.current_state(user=input["chat_id"]).finish()

@dp.message_handler(state='*', commands=['start'])
async def process_start_command(message: types.Message):
    await dp.current_state(user=message.from_user.id).set_state('bot_wait_a_photo')
    await message.reply(MESSAGES['start'])

@dp.message_handler(state=BotStates.BOT_WAIT_A_PHOTO, content_types=['photo'])
async def process_get_image(message: types.Message):
    await dp.current_state(user=message.from_user.id).set_state('bot_wait_a_filter')
    await message.reply(text=MESSAGES['get_image'], reply_markup=inline_choose_filters)
    file_url = message.photo[-1]
    file_info = await bot.get_file(file_url.file_id)
    file_name = str(uuid.uuid4())
    file_path = input_path + file_name + '.' + file_info.file_path.split('photos/')[1].split('.')[-1]
    await file_url.download(file_path)
    s3handler.upload(file_path)
    pr.requests_queue.new_request(message.from_user.id, file_name)
    pr.requests_queue.add_name(message.from_user.id, file_name)
    os.remove(file_path)

@dp.callback_query_handler(state=BotStates.BOT_WAIT_A_FILTER)
async def process_callback_filter(callback_query: types.CallbackQuery):
    await dp.current_state(user=callback_query.from_user.id).set_state('bot_wait_a_process')
    await bot.answer_callback_query(callback_query.id)
    pr.requests_queue.add_filter(callback_query.from_user.id, str(callback_query.data))
    await bot.send_message(callback_query.from_user.id, MESSAGES['filter'])
    logging.info(pr.requests_queue.get_request(callback_query.from_user.id))
    req = pr.requests_queue.json_serializer(pr.requests_queue.get_request(callback_query.from_user.id))
    connection = pika.BlockingConnection(conn_params)
    channel = connection.channel()
    channel.queue_declare(queue='first')
    channel.basic_publish(exchange='', routing_key='first', body=req)
    connection.close()

@dp.callback_query_handler(state=BotStates.BOT_WAIT_A_PROCESS)
async def process_callback_wait(callback_query: types.CallbackQuery):
    await bot.answer_callback_query(callback_query.id)
    await bot.send_message(callback_query.from_user.id, MESSAGES['filter_wait'])

if __name__ == '__main__':
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.create_task(consume())
    executor.start_polling(dp)


#!/usr/bin/python3
#-*-coding:utf-8-*-
# @Time:    2020/8/10 19:41
# @Author:  haiyong
# @File:    removeDuplicates.py
from typing import List

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        i = 0
        j = 0
        while j < len(nums) - 1:
            if nums[j] == nums[j + 1]:
                j += 1
            else:
                i += 1
                nums[i] = nums[j + 1]
                j += 1
        return i + 1

if __name__ == "__main__":
    num = Solution()
    result = num.removeDuplicates([0,0,1])
    print(result)


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Yingli Zhou
# version 0.1.0 2022/1/1

from collections import defaultdict
import requests
import re
# 下载一个网页
url = 'http://rest.kegg.jp/link/ko/M00001'
# 模拟浏览器发送http请求
response = requests.get(url)
# 编码方式
response.encoding='utf-8'
# 目标主页的网页源码
html = response.text

with open(r'F:\kegg\module.txt','w+') as f:
    f.writelines(html)

module_dict = defaultdict(list)
module_description = {}
with open(r'','r') as f:
    for i in f.readlines():
        ipart = i.strip().split('\t')
        module = ipart[0].split(':')[1]
        ko_id = ipart[1].split(':')[1]
        module_dict[module].append(ko_id)

with open(r'','r') as f:
    for i in f.readlines():
        ipart = i.strip().split('\t')
        module = ipart[0].split(':')[1]
        description = ipart[1]
        module_description[module] =description

for module in module_dict.keys():
    ko_list ='\t'.join(module_dict[module])
    print('%s\t%s\t%s' % (module,ko_list,module_description[module]))


import torch
import numpy as np

from tqdm import tqdm

class NN():
  def __init__(self):
    pass

  def fit(self, X, y):
    self.X = X
    self.y = y

  def _predict(self, X, mode, y = None):
    pass

  def q(self, X, y):
    return self._predict(X = X, mode = 'q', y = y)

  def q_mean(self, X, y):
    yhat = self.q(X, y)
    return torch.mean(yhat)

class kNN(NN):
  def __init__(self, k):
    super().__init__()
    self.k = k

  def _predict(self, X, mode, y = None):
    n = X.shape[0]
    yhat = torch.zeros(n)
    delta = torch.cdist(self.X, X)
    for i in tqdm(range(n)):
      vizinhos = torch.argsort(delta[:, i], axis = 0)[:self.k]
      possible, count = torch.unique(self.y[vizinhos], return_counts = True)
      freq = count / len(vizinhos)
      if mode == 'q':
        try:
            yhat[i] = freq[y[i] == possible]
        except:
            pass

    return yhat

class sNN(NN):
  def __init__(self, tau):
    super().__init__()
    self.tau = tau

  def _predict(self, X, mode, y = None):
    n = X.shape[0]
    yhat = torch.zeros(n)
    delta = torch.cdist(self.X, X)
    for i in tqdm(range(n)):
      weights = self._softmin(delta[:, i])
      if mode == 'q':
        yhat[i] = torch.sum(weights[y[i] == self.y])

    return yhat

  def _softmin(self, x):
    return torch.exp(-self.tau * x) / torch.sum(torch.exp(-self.tau * x))

class ggNN(NN):
  def __init__(self):
    super().__init__()
    self.DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

  def fit(self, X, y):
    self.X = X
    self.y = y
    self.N = self.X.shape[0]

  def _gg(self, delta, btsz):
    val_min = torch.ones(self.N).to(self.DEVICE) * 1e6
    for b in range(0, self.N, btsz):
      X_batch = self.X[b:b+btsz, :].to(self.DEVICE)
      F_batch = torch.cdist(X_batch, self.X.to(self.DEVICE))**2
      A_batch = delta[:btsz].T + F_batch.T
      val_min_batch, _ = torch.min(A_batch, axis = 1)
      val_min, _ = torch.min(torch.stack((val_min, val_min_batch), dim = 1), dim = 1)
      del X_batch, F_batch, A_batch, val_min_batch
    a = val_min - delta
    return torch.where(a > 0, True, False).cpu()

  def _predict(self, X, mode, y = None):
    n = X.shape[0]
    yhat = torch.zeros(n)
    delta = torch.cdist(self.X, X).to(self.DEVICE)
    for i in tqdm(range(n)):
      vizinhos = self._gg(delta[:, i], btsz = self.N // 4)
      possible, count = torch.unique(self.y[vizinhos], return_counts = True)
      freq = count / torch.sum(vizinhos)
      if mode == 'q':
        try:
            yhat[i] = freq[y[i] == possible]
        except:
            pass

    return yhat

class sggNN(ggNN):
  def __init__(self, tau):
    super().__init__()
    self.tau = tau

  def _predict(self, X, mode, y = None):
    n = X.shape[0]
    yhat = torch.zeros(n)
    delta = torch.cdist(self.X, X).to(self.DEVICE)
    for i in tqdm(range(n)):
      vizinhos = self._gg(delta[:, i], btsz = self.N // 4)
      weights = self._softmin(delta[vizinhos, i])
      if mode == 'q':
        yhat[i] = torch.sum(weights[y[i] == self.y[vizinhos]])

    return yhat

  def _softmin(self, x):
    return torch.exp(-self.tau * x) / torch.sum(torch.exp(-self.tau * x))


import datetime
import csv
import os

def unix_to_utc(ts):
    # 예를들어 1688391782.799519를 변환하게 되면 나중에 역변환 할때 .0799519 부분이 누락됨.
    utc_time = datetime.datetime.fromtimestamp(float(ts))
    return utc_time.strftime("%Y-%m-%d %H:%M:%S")

def utc_to_unix(ts):
    utc_time = datetime.datetime.strptime(ts, "%Y-%m-%d %H:%M:%S")
    unix_time = (utc_time - datetime.datetime(1970, 1, 1)).total_seconds()
    return unix_time

def messages_to_csv(channel_id, channel_name, messages, user_id = "youbin"):
    # 디버깅
    print("messages_to_csv 실행 ---------------------------------------")

    file_path = f"./{user_id}/{channel_id}/{channel_name} 채널 메시지 기록.csv"
    if not os.path.exists(f"./{user_id}/{channel_id}"):
        os.makedirs(f"./{user_id}/{channel_id}")

    datas = []
    for message in messages:
        data = {
            "메시지 ID": message["client_msg_id"],
            "채널 ID": channel_id,
            "채널 이름": channel_name,
            "작성자 ID": message["user_id"],
            "작성자 이름": message["user_name"],
            "메시지 내용": message["text"],
            "작성 시간": message["ts"]
        }
        datas.append(data)

    try:
        with open(file_path, "w", newline = "", encoding = "utf-8") as file:
            field_names = ["메시지 ID", "채널 ID", "채널 이름", "작성자 ID", "작성자 이름", "메시지 내용", "작성 시간"]

            writer = csv.DictWriter(file, fieldnames = field_names)
            writer.writeheader()

            for data in datas:
                writer.writerow(data)

        return file_path

    except Exception as e:
        print(f"Error: {str(e)}")

def delete_channel_csv(channel_id, channel_name, user_id = "youbin"):
    # 디버깅
    print("delete_channel_csv 실행 ---------------------------------------")

    file_path = f"./{user_id}/{channel_id}/{channel_name} 채널 메시지 기록.csv"

    try:
        os.remove(file_path)
        os.rmdir(f"./{user_id}/{channel_id}")
        print(f"File has been deleted successfully.")

        return True

    except FileNotFoundError:
        print(f"File '{file_path}' not found.")

        return False

    except Exception as e:
        print(f"Error occurred while deleting the file: {str(e)}")

        return False

# def update_channel_csv(channel_id, channel_name, message):
#     exist_file = f"./{SOURCE_DIRECTORY}/{channel_name} 채널 메시지 내역.csv"

#     new_data = {
#         "메시지 ID": message["client_msg_id"],
#         "채널 ID": channel_id,
#         "채널 이름": channel_name,
#         "작성자 ID": message["user_id"],
#         "작성자 이름": message["user_name"],
#         "메시지 내용": message["text"],
#         "작성 시간": message["ts"]
#     }

#     with open(exist_file, "a", newline = "", encoding = "utf-8") as file:
#         fieldnames = ["메시지 ID", "채널 ID", "채널 이름", "작성자 ID", "작성자 이름", "메시지 내용", "작성 시간"]

#         writer = csv.DictWriter(file, fieldnames = fieldnames)
#         writer.writerow(new_data)

#     ingest_one_document(channel_id, exist_file)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 11 12:23:17 2019

@author: samas
"""

from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
from keras.layers import Dense, Activation, Dropout, Flatten
from keras import optimizers
from keras.models import Sequential,load_model
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
import cv2
import os
import time
dp_start=time.time()

# step 1: load data

img_width = 150
img_height = 450
train_data_dir = '/Users/samas/Desktop/Model/data/train'
valid_data_dir = '/Users/samas/Desktop/Model/data/validation'

datagen = ImageDataGenerator(rescale = 1./255)

train_generator = datagen.flow_from_directory(directory=train_data_dir,
											   target_size=(img_width,img_height),
											   classes=['EM-QRCRI','Others','ZZ-QRCRI','ZZ-VDLAY','ZZ-VDSHP','ZZ-VDZZZ','ZZ-VRZZZ'],
											   class_mode='categorical',
											   batch_size=16)

validation_generator = datagen.flow_from_directory(directory=valid_data_dir,
											   target_size=(img_width,img_height),
											   classes=['EM-QRCRI','Others','ZZ-QRCRI','ZZ-VDLAY','ZZ-VDSHP','ZZ-VDZZZ','ZZ-VRZZZ'],
											   class_mode='categorical',
											   batch_size=32)

# step-2 : build model

model =Sequential()

model.add(Conv2D(32,(3,3), input_shape=(img_width, img_height, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(32,(3,3), input_shape=(img_width, img_height, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(64,(3,3), input_shape=(img_width, img_height, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(7))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])

print('model complied!!')

print('starting training....')
training = model.fit_generator(generator=train_generator, steps_per_epoch=2048 // 16,epochs=1,validation_data=validation_generator,validation_steps=832//16)

print('training finished!!')

print('saving weights to docclassifier_CNN.h5')

model.save_weights('/Users/samas/Desktop/Model/models/docclassifier_CNN.h5')


# set the matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import argparse
import cv2
import os

import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
from torch.utils.data import DataLoader
from scnet import scnet50_v1d
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.neighbors import KNeighborsClassifier

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument('--train_dataset', default='home/dataset/train/', help='path to train dataset')
ap.add_argument('--test_intra_sensor_dataset', default='home/dataset/test_intra_sensor/', help='path to intra sensor test dataset')
ap.add_argument('--test_inter_sensor_dataset', default='home/dataset/test_inter_sensor/', help='path to inter sensor test dataset')
ap.add_argument('--load', default='home/output/model/model_300.pth', help='path of trained model')
ap.add_argument('--k', type=int, default=5, help='number of nearest neighbour')
ap.add_argument('--image_width', type=int, default=256, help='width of image')
ap.add_argument('--image_height', type=int, default=192, help='height of image')
ap.add_argument('--num_class', type=int, default=2, help='number of class')
ap.add_argument('--latent_feature_dim', type=int, default=128)
ap.add_argument('--batch_size', type=int, default=32)
args = ap.parse_args()

#Finetune model for claassification
class FineTuneModel(nn.Module):
    def __init__(self, original_model, latent_dim, num_classes):
        super(FineTuneModel, self).__init__()
        # Everything except the last linear layer
        self.conv_features = nn.Sequential(*list(original_model.children())[:-1])
        self.fc_features =  nn.Sequential(
                            nn.Linear(2048, 512),
                            nn.ReLU(),
                            nn.Dropout(0.4),
                            nn.Linear(512,latent_dim),
                            nn.ReLU()
                            #nn.LogSoftmax(dim=1) # For using NLLLoss()
                        )
        self.classifier = nn.Sequential(
                            nn.Dropout(0.4),
                            nn.Linear(latent_dim, num_classes)
                        )

    def forward(self, x):
        f = self.conv_features(x)
        f = f.view(f.size(0), -1)
        fc_feat = self.fc_features(f)
        y = self.classifier(fc_feat)
        return fc_feat, y

#Transforming and loading training images
train_trans = transforms.Compose([
    transforms.Resize((args.image_height, args.image_width)),
    # This makes it into [0,1]
    transforms.ToTensor(),
    # This makes it into [-1,1]
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
])
train_data = datasets.ImageFolder(root=args.train_dataset, transform=train_trans)
train_loader = DataLoader(train_data, batch_size=args.batch_size, shuffle=True, num_workers=4)

#Transforming and loading intra sensor testing images
test_intra_sensor_trans = transforms.Compose([
	transforms.Resize((args.image_height, args.image_width)),
	# This makes it into [0,1]
	transforms.ToTensor(),
	# This makes it into [-1,1]
	transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
])

test_intra_sensor_data = datasets.ImageFolder(root=args.test_intra_sensor_dataset, transform=test_intra_sensor_trans)
test_intra_sensor_loader = DataLoader(test_intra_sensor_data, batch_size=args.batch_size, shuffle=True, num_workers=4)

#Transforming and loading inter sensor testing images
test_inter_sensor_trans = transforms.Compose([
    transforms.Resize((args.image_height, args.image_width)),
    # This makes it into [0,1]
    transforms.ToTensor(),
    # This makes it into [-1,1]
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
])

test_inter_sensor_data = datasets.ImageFolder(root=args.test_inter_sensor_dataset, transform=test_inter_sensor_trans)
test_inter_sensor_loader = DataLoader(test_inter_sensor_data, batch_size=args.batch_size, shuffle=True, num_workers=4)

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = scnet50_v1d(pretrained=False)
model= FineTuneModel(model,args.latent_feature_dim, args.num_class)
model = model.to(device)
print("Loading pretrained model......")
model.load_state_dict(torch.load(args.load))
print("pretrained model loaded")

#######################################  Feature Extraction and Training KNN and Testing #####################################

#for storing feature and labels
x_train=[]
y_train=[]
x_intra_sensor_test=[]
y_intra_sensor_test=[]
x_inter_sensor_test=[]
y_inter_sensor_test=[]

def append_to_list(l,a):
    #print("hello ")
    for i in range(a.shape[0]):
        #print("a:i",a[i])
        l.append(a[i].tolist())
    return l

print("extracting feature ..............")
model.eval()
with torch.no_grad():
    for i, data in enumerate(train_loader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data
        inputs=inputs.to(device)
        labels=labels.to(device)

        feat,outputs = model(inputs)
        x_train=append_to_list(x_train,feat.cpu().detach().numpy())
        y_train=append_to_list(y_train,labels.cpu().detach().numpy())

model.eval()
with torch.no_grad():
    for i, data in enumerate(test_intra_sensor_loader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data
        inputs=inputs.to(device)
        labels=labels.to(device)

        feat,outputs = model(inputs)
        x_intra_sensor_test=append_to_list(x_intra_sensor_test,feat.cpu().detach().numpy())
        y_intra_sensor_test=append_to_list(y_intra_sensor_test,labels.cpu().detach().numpy())

model.eval()
with torch.no_grad():
    for i, data in enumerate(test_inter_sensor_loader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data
        inputs=inputs.to(device)
        labels=labels.to(device)

        feat,outputs = model(inputs)
        x_inter_sensor_test=append_to_list(x_inter_sensor_test,feat.cpu().detach().numpy())
        y_inter_sensor_test=append_to_list(y_inter_sensor_test,labels.cpu().detach().numpy())

x_train=np.asarray(x_train)
y_train=np.asarray(y_train)
x_intra_sensor_test=np.asarray(x_intra_sensor_test)
y_intra_sensor_test=np.asarray(y_intra_sensor_test)
x_inter_sensor_test=np.asarray(x_inter_sensor_test)
y_inter_sensor_test=np.asarray(y_inter_sensor_test)

print("feature extraction completed")

print("KNN training started ......")
neigh = KNeighborsClassifier(n_neighbors=args.k)
neigh.fit(x_train,y_train)
print("KNN traing completed")

target_names = ['colored', 'Normal', 'transparent']

print("testing on intra sensor image......")
y_pred_intra_sensor=neigh.predict(x_intra_sensor_test)
#print(classification_report(y_intra_sensor_test, y_pred_intra_sensor, target_names=target_names))
print(confusion_matrix(y_intra_sensor_test, y_pred_intra_sensor))

print("testing on intra sensor images completed")

print("testing on inter sensor image......")
y_pred_inter_sensor=neigh.predict(x_inter_sensor_test)
#print(classification_report(y_inter_sensor_test, y_pred_inter_sensor, target_names=target_names))
print(confusion_matrix(y_inter_sensor_test, y_pred_inter_sensor))
print("testing on inter sensor image completed")


# -*- coding: utf-8 -*-
from selenium import webdriver
from.htmltestrunner import HTMLTestRunner

class Common(object):

    @classmethod
    def get_chrome_driver(cls):
        return webdriver.Chrome()

    @classmethod
    def create_report(cls, fileName, revT, revD, revTest):
        with open('../TestReport/' + fileName + '.html', 'wb') as HtmlFile:
            HTMLTestRunner(
                stream=HtmlFile,  # 文本流
                verbosity=2,  # 报告详情级别,一般都用2
                title=revT,  # 标题
                description=revD  # 描述
            ).run(revTest)


# -*- coding: utf-8 -*-
"""
Created on Tue Sep 21 19:06:54 2021

@author: Bishop
"""

class mongoProcess:
    def get_database():
        from pymongo import MongoClient
        import pymongo

        # Provide the mongodb atlas url to connect python to mongodb using pymongo
        #CONNECTION_STRING = "mongodb+srv://<username>:<password>@<cluster-name>.mongodb.net/myFirstDatabase"
        CONNECTION_STRING = "mongodb://localhost:27017/blogusers"
        # mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false
        # Create a connection using MongoClient. You can import MongoClient or use pymongo.MongoClient
        from pymongo import MongoClient
        client = MongoClient(CONNECTION_STRING)

        # Create the database for our example (we will use the same database throughout the tutorial
        return client['blogusers']

if __name__ == "__main__":

    item_1 = {
    "_id" : "U1IT00001",
    "item_name" : "Blender",
    "max_discount" : "10%",
    "batch_number" : "RR450020FRG",
    "price" : 340,
    "category" : "kitchen appliance"
    }

    item_2 = {
    "_id" : "U1IT00002",
    "item_name" : "Egg",
    "category" : "food",
    "quantity" : 12,
    "price" : 36,
    "item_description" : "brown country eggs"
    }

    dbname =  mongoProcess.get_database()
    print(dbname)
    collection_name = dbname["blogusers"] # get the collection
    #collection_name = dbname["blogusers"]
    collection_name.insert_many([item_1,item_2])

{'MongoClient': 'pymongo.MongoClient', 'pymongo': 'pymongo'}.get_database


"""
before doing this, need to do the following:
1. clone https://github.com/mcordts/cityscapesScripts.git
2. run cityscapesScripts/cityscapesscripts/preparation/createTrainIdLabelImgs.py
```
CITYSCAPES_DATASET=xxxxxx python cityscapesscripts/preparation/createTrainIdLabelImgs.py
```
"""

from pathlib import Path
from shutil import copy
from tqdm import tqdm
import json

src_dir = Path('./data/cityscapes')
dst_dir = Path('./data/cityscapes_ss')

for phase in ['train', 'val']:
    images = {}
    labels = {}
    for src_p in tqdm((src_dir/f'leftImg8bit/{phase}').glob('**/*.png')):
        images[src_p.name[:-16]] = src_p.relative_to(src_dir).as_posix()
        dst_p = dst_dir/src_p.relative_to(src_dir)
        dst_p.parent.mkdir(exist_ok=True, parents=True)
        copy(src_p, dst_p)
    for src_p in tqdm((src_dir/f'gtFine/{phase}').glob('**/*_labelTrainIds.png')):
        labels[src_p.name[:-25]] = src_p.relative_to(src_dir).as_posix()
        dst_p = dst_dir/src_p.relative_to(src_dir)
        dst_p.parent.mkdir(exist_ok=True, parents=True)
        copy(src_p, dst_p)
    assert images.keys() == labels.keys()
    path_list = [f'{images[key]} {labels[key]}\n' for key in sorted(images.keys())]
    with open(dst_dir/f'{phase}.txt', 'w') as f:
        f.writelines(path_list)
with open(dst_dir/'test.txt', 'w') as f:
    f.writelines(path_list)

with open(dst_dir/'labelmap.json', 'w') as f:
    json.dump({
        '0': 'road',
        '1': 'sidewalk',
        '2': 'building',
        '3': 'wall',
        '4': 'fence',
        '5': 'pole',
        '6': 'trafficlight',
        '7': 'trafficsign',
        '8': 'vegetation',
        '9': 'terrain',
        '10': 'sky',
        '11': 'person',
        '12': 'rider',
        '13': 'car',
        '14': 'truck',
        '15': 'bus',
        '16': 'train',
        '17': 'motorcycle',
        '18': 'bicycle'
    }, f, indent=4)


# -*- coding: PYTHON3 -*-
# @Author: Santosh
# @Last Modified by:   Santosh

""" Fourier transform of musical instruments"""

import matplotlib.pyplot as plt
import numpy as np
import math

# READING DATA FROM PIANO.TXT AND STORING IT IN ARRAY
f1 = open('piano.txt', 'r')
lines = f1.readlines()
y_piano = []
for line in lines:
    p = line.split()
    y_piano.append(float(p[0]))
y_piano = np.array(y_piano)
t_piano = np.linspace(0, 2*math.pi, len(y_piano))

#REAL FOURIER TRANSFORM
fk_piano = np.fft.rfft(y_piano)
freq_piano = np.fft.rfftfreq(t_piano.shape[-1])
fk_piano_abs = []

for i in range(len(freq_piano)):
	fk_piano_abs.append(math.hypot(fk_piano.real[i],fk_piano.imag[i]))

#PLOTTING THE PIANO.TXT FILE AND ITS FOURIER TRANSFORM
plt.suptitle("Piano file and its fourier transform")
plt.subplot(211)
plt.plot(t_piano,y_piano)
plt.xlabel('Time')
plt.ylabel(r'$f(t)$')

plt.subplot(212)
plt.xlabel(r'$k$')
plt.ylabel(r'$F(k)$')
plt.plot(freq_piano[0:10000],fk_piano_abs[0:10000])
plt.savefig('piano.png',bbox_inches = 'tight')
plt.show()
plt.clf()

# READING DATA FROM TRUMPET.TXT AND STORING IT IN ARRAY
f2 = open('trumpet.txt', 'r')
lines = f2.readlines()
y_trumpet = []
for line in lines:
    p = line.split()
    y_trumpet.append(float(p[0]))
y_trumpet = np.array(y_trumpet)
t_trumpet = np.linspace(0, 2*math.pi, len(y_trumpet))

#REAL FOURIER TRANSFORM
fk_trumpet = np.fft.rfft(y_trumpet)
freq_trumpet = np.fft.rfftfreq(t_trumpet.shape[-1])
fk_trumpet_abs = []

for i in range(len(freq_trumpet)):
	fk_trumpet_abs.append(math.hypot(fk_trumpet.real[i],fk_trumpet.imag[i]))

#PLOTTING THE TRUMPET.TXT FILE AND ITS FOURIER TRANSFORM
plt.suptitle("Trumpet file and its fourier transform")
plt.subplot(211)
plt.plot(t_trumpet,y_trumpet)
plt.xlabel('Time')
plt.ylabel(r'$f(t)$')

plt.subplot(212)
plt.xlabel(r'$k$')
plt.ylabel(r'$F(k)$')
plt.plot(freq_trumpet[0:10000],fk_trumpet_abs[0:10000])
plt.savefig('trumpet.png',bbox_inches = 'tight')
plt.show()
plt.clf()


import pint
import numpy as np

def buck5v8a():
    """
    Cálculo de fuente switching tipo buck de 5v x 8A.

    Basada en LTC1624 de Analog Devices, usando ecuaciones y modelos
    de fuente buck de:
       Christophe Basso. 2008. Switch-Mode Power Supplies Spice
       Simulations and Practical Designs (1st. ed.). McGraw-Hill, Inc.,
       USA
    """
    units = pint.UnitRegistry()

    # Rango de tensión de entrada (2 baterias AGM o alimentación externa)
    Vi_min = 9 * units.V  # Tensión nominal externa
    Vi = 24 * units.V  # Tensión nominal de batería
    Vi_max = 29.4 * units.V  # Tensión máxima segura de carga

    Vo = 5 * units.V  # Tensión de salida
    Io_min = 2 * units.A  # Corriente de salida mínima
    Io = 4 * units.A  # Corriente de salida nominal (Jetson Nano)
    Io_max = 8 * units.A  # Corriente de salida máxima

    # Rango de frecuencia de conmutación
    fsw_min = 175 * units.kHz
    fsw_max = 225 * units.kHz
    fsw = 200 * units.kHz

    # Rango de tensión de ripple de salida
    Vo_rpp_max = 10 * units.mV
    Vi_rpp_max = 250 * units.mV
    # Caídas de tensión máxima ante escalón de carga
    Vi_tran = 400 * units.mV
    Vo_tran = 200 * units.mV

    # Tensión máxima sobre resistencia de sensado de corriente
    Vsense_thres = 100 * units.mV
    # Tensión máxima de salida del gate driver
    Vdriver_max = 5.5 * units.V
    # Temperatura máxima de operación (rango comercial)
    Ta_max = 70 * units.delta_degC
    # Caída de tensión sobre diodo Schottky de conmutación
    Vd = 0.45 * units.V
    # Eficiencia deseada
    neff = 0.85

    # Cálculo de resistencia de sensado
    Rsense_budget = (Vsense_thres / Io_max).to(units.mΩ)
    Rsense = 12 * units.mΩ

    # Rango de ciclo de trabajo
    M_max = (Vo + Vd)/(Vi_min + Vd)
    M_min = (Vo + Vd)/(Vi_max + Vd)
    D_max = M_max / neff
    D_min = M_min / neff

    # Cálculo de inductor
    L_budget = (D_min * (Vi_max - Vo) / (2 * fsw_min * Io_min)).to(units.uH)

    ## Se utiliza SRP1770C-470M
    L = 47 * units.uH
    L_tol = 0.2
    L_min = L * (1 - L_tol)
    L_max = L * (1 + L_tol)
    assert L_min > L_budget

    L_I_rms_max = 8.7 * units.A
    dI_max = (D_min * (Vi_max - Vo) / (L_min * fsw_min)).to(units.A)
    assert L_I_rms_max > np.sqrt(Io_max**2 + dI_max**2 / 12)

    # Cálculo de capacitores de salida (desacople, reserva)
    ESR_CoutR_max = (Vo_rpp_max / dI_max).to(units.mΩ)

    Cout_budget = max(
        D_max / (2 * fsw_min * ESR_CoutR_max),
        (1 - D_min) / (2 * fsw_min * ESR_CoutR_max),
        (L_max * (Io_max**2 - Io_min**2) / ((Vo + Vo_tran)**2 - Vo**2)),
        (L_max * (Io_min**2 - Io_max**2) / ((Vo - Vo_tran)**2 - Vo**2))
    ).to(units.uF)

    I_CoutR_rms_max = (dI_max / np.sqrt(12)).to(units.mA)

    ## Se utiliza 16SVPE180M como capacitor de desacople
    CoutR = 180 * units.uF
    CoutR_tol = 0.2  # 20%
    CoutR_min = CoutR * (1 - CoutR_tol)
    CoutR_max = CoutR * (1 + CoutR_tol)
    ESR_CoutR = 11 * units.mΩ
    I_CoutR_rms = 4.46 * units.A
    V_CoutR_max = 16 * units.V
    assert I_CoutR_rms > I_CoutR_rms_max
    assert V_CoutR_max > Vo

    ## Se utilizan 2 x EEE-FT1C102AP como capacitores de reserva
    CoutB = 2 * 1000 * units.uF
    CoutB_tol = 0.20  # 20%
    CoutB_min = CoutB * (1 - CoutB_tol)
    CoutB_max = CoutB * (1 + CoutB_tol)
    ESR_CoutB = 60 * units.mΩ / 2
    V_CoutB_max = 16 * units.V
    I_CoutB_rms = 2 * 1.19 * units.A
    assert ESR_CoutB < Vo_tran / (Io_max - Io_min)
    assert V_CoutB_max > Vo

    Cout = CoutB + CoutR
    Cout_min = CoutB_min + CoutR_min
    Cout_max = CoutB_max + CoutR_max
    ESR_Cout = 1/(1/ESR_CoutR + 1/ESR_CoutB)
    assert Cout_min > Cout_budget

    ## Verificación de tensión de ripple de salida
    Vo_rpp = (ESR_Cout * dI_max + dI_max / (8 * fsw_min * Cout_min)).to(units.mV)
    assert Vo_rpp < Vo_rpp_max
    assert Vo_rpp / ESR_CoutB < I_CoutB_rms

    # Cálculo de capacitores de entrada (desacople, reserva)
    ## Rango de frecuencia de resonancia de salida
    fo_min = (1 / (2 * np.pi * np.sqrt(L_max * Cout_max))).to(units.Hz)
    fo_max = (1 / (2 * np.pi * np.sqrt(L_min * Cout_min))).to(units.Hz)
    assert np.pi**2/2 * (fo_max/fsw_min)**2 * (1 - D_min) * Vo < Vo_rpp_max

    ## Frecuencia de crossover máxima
    fcross_max = (1 / (2 * np.pi * ESR_Cout * Cout_min)).to(units.Hz)

    CinR_budget = ((D_max * (1 - D_max) * Io_max) / (Vi_rpp_max * fsw_min)).to(units.uF)
    ESR_CinR_budget = (Vi_rpp_max / Io_max).to(units.mΩ)
    I_CinR_rms_max = Io_max * np.sqrt(
        D_max * (1 - D_max) + 1/12 * (Vo / (L_min * fsw_min * Io_max))**2 * D_max * (1 - D_max)**2
    )

    ts_in = 100 * units.us
    CinB_budget = (0.5 * ts_in * (Io_max - Io_min) * D_max / Vi_tran).to(units.uF)
    ESR_CinB_max = (Vi_tran / ((Io_max - Io_min) * D_max)).to(units.mΩ)

    ## Se utilizan 2 x EEH-ZK1V331P como capacitores de entrada
    Cin = 2 * 330 * units.uF
    Cin_tol = 0.2
    Cin_min = Cin * (1 - Cin_tol)
    Cin_max = Cin * (1 + Cin_tol)
    I_Cin_rms = 2.8 * units.A * 2
    ESR_Cin = 20 * units.mΩ / 2
    V_Cin_max = 35 * units.V
    assert Cin_min > max(CinR_budget, CinB_budget)
    assert ESR_Cin * Io_max  + D_max * (1 - D_max) * Io_max / (Cin_min * fsw_min) < Vi_rpp_max
    assert ESR_Cin < min(Vi_tran / ((Io_max - Io_min) * D_min), ESR_CinB_max)
    assert V_Cin_max > Vi_max
    assert I_Cin_rms > I_CinR_rms_max

    # Cálculo de conmutadores (MOSFET + diodo Schottky)
    Vdr_max = Vswr_max = Vi_max  # Tensión máxima sobre MOSFET y diodo Schottky
    Id_max = Isw_max = Io_max + dI_max  # Corriente máxima de MOSFET y diodo Schottky

    ## Corriente media de MOSFET y diodo Schottky
    Isw_avg_max = Io_max * D_max
    Id_avg_max = Io_max * (1 - D_min)

    ## Corriente RMS máxima de MOSFET y diodo Schottky
    tau = L_min * fsw_min * Io_max / Vo
    Isw_rms_max = Io_max * np.sqrt(D_max * (1 + 1/12 * ((1 - D_max)/tau)**2))
    Id_rms_max = Io_max * np.sqrt(1 - D_max * (1 + 1/12 * ((1 - D_max)/tau)**2))

    ## Se utiliza IRF7470 como MOSFET de conmutacion
    Rds_on = 15 * units.mΩ
    Coss = 690 * units.pF
    Ciss = 3430 * units.pF
    Qg = 44 * units.nC
    Tjsw_max = 150 * units.delta_degC
    Zswja_th = 25 * units.delta_degC / units.W  # Impedancia térmica @ D = 0.5 fsw ≃ 200 kHz

    ## Verificación de potencia y temperatura máxima de conmutador
    Psw = (D_max * Io_max**2 * Rds_on + 1/2 * (Coss * fsw_max * Vswr_max**2)).to(units.W)
    Tjsw = Ta_max + Zswja_th * Psw
    assert Tjsw < Tjsw_max

    ## Verificación de potencia y temperatura máxima de driver
    Pg = (fsw_max * Qg * Vdriver_max).to(units.mW)
    Tjic_max = 125 * units.delta_degC
    Ricja_th = 110 * units.delta_degC / units.W
    Tjic = Ta_max + Pg * Ricja_th
    assert Tjic < Tjic_max

    ## Se utiliza MBRB1645G como diodo
    ### Resistencia térmica para un pad de disipación de 1in²
    Rdja_th = 23.3 * units.delta_degC / units.W
    Tjd_max = 175 * units.delta_degC
    Pd = (Id_avg_max * Vd).to(units.W)
    Tjd = Ta_max + Pd * Rdja_th
    assert Tjd < Tjd_max

    # Cálculo de capacitor de bomba de carga
    Cboost_budget = (50 * Ciss).to(units.uF)
    ## Se utilizan 3 x GCD21BR71H104KA01
    Cboost = 3 * 0.1 * units.uF
    Cboost_tol = 0.1  # 10 %
    Cboost_dc_bias = 0.8
    Cboost_min = Cboost * (1 - Cboost_tol) * Cboost_dc_bias
    V_Cboost_max = 50 * units.V
    assert Cboost_min > Cboost_budget

    # Cálculo de resistencias para realimentación de tensión
    R1 = 5.6 * units.kΩ
    R2 = np.round(R1 * (Vo / (1.19 * units.V) - 1.))

    print('\n'.join('{} = {}'.format(name, value)
          for name, value in locals().items()
          if isinstance(value, units.Quantity)))

if __name__ == '__main__':
    buck5v8a()


from torch.nn.modules.loss import L1Loss
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import numpy as np
import torch.nn.functional as F
import matplotlib.pyplot as plt
from torchvision import transforms
import pandas as pd
from torchsummary import summary
from torch.utils.data import Dataset , DataLoader
from torch.nn import Conv2d , Linear , Dropout2d , BatchNorm2d , MaxPool2d , L1Loss , MSELoss , SiLU , Dropout , init , ReLU , CrossEntropyLoss ,Softmax , Upsample , LeakyReLU
import time
from torch.nn import Sequential
from .common import PredictionNeurons , NeuronBlock , BConvBlock, ResidualBlock,ScalePrediction,ConvBlock
from .config import *

class MultiLayerCreator(nn.Module):

  def __init__ (self,config,resulution,num_classes,in_channel=3,information=False):

    super(MultiLayerCreator,self).__init__()
    self.information=information
    self.resulotion = resulution
    self.num_classes = num_classes
    self.config = config
    self.in_channel = in_channel
    self.layers = self.layer_creator()

  def layer_creator(self):
    layers = nn.ModuleList()

    for conf in self.config:
      if isinstance(conf,list):
        out_c , kernel , sp  = conf
        layers.append(

                ConvBlock(
                    self.in_channel,
                    out_c,
                    kernel_size=kernel,
                    padding=1 if kernel == 3 else 0,
                    stride=sp
                    ).to(device)

        )
        self.in_channel = out_c

      elif isinstance(conf,tuple):
        types , time = conf
        if types == 'B':
          layers +=[
            ResidualBlock( self.in_channel,num_repeat=time)
          ]

        elif types == 'M':
          for i in range(time):
            layers.append(
              ResidualBlock( self.in_channel,time)
            )
        elif types == "S":

          layers += [
            ResidualBlock(self.in_channel,use_residual=False,num_repeat=1),
            ConvBlock(self.in_channel,self.in_channel//2,kernel_size=1),
            ScalePrediction(self.in_channel//2,num_classes=self.num_classes,stride=time)
          ]
          self.in_channel //=2

      elif isinstance(conf , str):

        if conf == "O":
          layers.append(
              NeuronBlock(self.in_channel*84.5, self.num_classes,act=False).to(device)
          )
        elif conf == "P":
          layers.append(
              PredictionNeurons(self.in_channel*3, self.num_classes,act=False).to(device)
          )
        elif conf == "S":

          layers += [
                ResidualBlock(self.in_channel,use_residual=False,num_repeat=1),
                ConvBlock(self.in_channel,self.in_channel//2,kernel_size=1),
                ScalePrediction(self.in_channel//2,num_classes=self.num_classes)
          ]
          self.in_channel //=2

        elif conf == "U":
          layers.append(nn.Upsample(scale_factor=1))
          self.in_channel*=9

    return layers

  def forward(self,x):

    output = []

    route_connections = []

    i=0
    for layer in self.layers:

      if isinstance(layer , ScalePrediction):

        if  self.information:
          print('[INFO] Running On ScalePrediction Layer')
          stp = time.time()

        output.append((layer.forward(x).to(device)))

        if  self.information:
          # print(f'[INFO] Output From Scale Prediction : {x.shape}')
          ttp = (time.time()-stp)
          print('[INFO] Finished On ScalePrediction Layer in {} Sec'.format(ttp) )

        continue
      if  self.information:
        print(f'[INFO] befor the predict : {x.shape}')
      x = layer.forward(x)
      if  self.information:
        print(f"[INFO] Layer is runing on {i} index")
        if isinstance(layer,nn.Upsample) != True:
          print(f"[INFO] Layer Information : in channels : {layer.c_in} ")
        else:
          print("[INFO] Upsample Layer Is Running ")
        print(f'[INFO] after the predict : {x.shape}\n-------------')

      i+=1
      if isinstance(layer,ResidualBlock) and layer.num_repeat==2:

        if  self.information:

          print('[INFO] Repeats ON this Layer : {}'.format(layer.num_repeat))
          strb = time.time()
          print('[INFO] Running On ResidualBlock Layer')

        route_connections.append(x)

        # if  self.information:
        #   routecopy = torch.stack(route_connections).to('cpu').detach().numpy()
        #   vrb = np.array(routecopy)
        #   print('[INFO] Shape  On route connections : {} '.format(vrb.shape))

        if  self.information:
          ttrb = (time.time()-strb)
          print('[INFO] Finished On ResidualBlock Layer in {} Sec'.format(ttrb))

      elif isinstance(layer,nn.Upsample):

        if  self.information:
          print('[INFO] Running On Upsample Layer')
          stu = time.time()

        x = torch.cat((x,route_connections[-1]),dim=1)
        route_connections.pop()

        if  self.information:
          ttu = (time.time()-stu)
          print('[INFO] Finished On Upsample Layer in {} Sec'.format(ttu))

    return output


from fastapi import FastAPI
import datetime
from pydantic import BaseModel
from pydantic import BaseModel, Field
from typing import Optional

tags_metadata = [
    {
        "name": "users",
        "description": "Operations with users. The **login** logic is also here.",
    },
    {
        "name": "items",
        "description": "Manage items. So _fancy_ they have their own docs.",
        "externalDocs": {
            "description": "Items external docs",
            "url": "https://fastapi.tiangolo.com/",
        },
    },
]

app = FastAPI(openapi_tags=tags_metadata)

class Item(BaseModel):
    """ Item class description
    - You can itemize
    - You can write `code`
    """
    item_id: int
    name: str
    description: Optional[str] = Field(
        None, title="The description of the item", max_length=300
    )
    price: float = Field(..., gt=0, description="The price must be greater than zero")
    tax: Optional[float] = None

@app.get("/")
def read_items():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
    return {"item_name": item.name, "item_id": item_id}


from relationalai import LocalConnection

import os
import tempfile
import unittest

class TestInstallSource(unittest.TestCase):

    def testInstallSource(self):
        conn = LocalConnection(dbname="python-sdk")
        conn.debug_level = 1
        self.assertTrue(conn.create_database(overwrite=True))
        self.assertTrue(len(conn.install_source(src_name="src_1", src_str="def foo = 1").get("problems")) == 0)

        # create a tempfile
        fd, path = tempfile.mkstemp()
        with os.fdopen(fd, "w") as tmp:
            tmp.write("def bar = 3")
        self.assertTrue(len(conn.install_source(src_name="src_3", src_path=path).get("problems")) == 0)
        self.assertTrue(len(conn.install_source(src_name="src_2", src_str="def foo = ").get("problems")) == 2)

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


#   Explanation and format of stats files:
#
# stored in %PROGRAMDATA%\Locktime\NetLimiter\4\Stats\*
# contains 4 files:
#   * nlstatsV6.db - transfer stats per app, for IPv6 addresses
#   * nlstatsV4.db - transfer stats per app, for IPv4 addresses
#   * nlstats-apps.lst - known app list
#   * nlstats-users.lst - known user list
# all files are byte streams of fixed-size structures (except the app list) dumped directly to a file
# one stat "row" contains information about a transfer, per app, per connection, grouped by the update time (set in NetLimiter GUI -> Tools -> Stats manager)
#
#
#   IP2LOC files:
#
# maps IP's (specifically, IP ranges) to their geographical locations
# stored in %PROGRAMDATA%\Locktime\NetLimiter\4\Ip2Loc\*
# for each *.dat file, there is a *.xml file that describes the format (and order of fields!) of structures
# 3 important files:
#   * IpRangesV4.dat - ipv4 range to location id
#   * IpRangesV6.dat - ipv6 range to location id
#   * Locations.dat - location id's to actual names
# IpRangesV4.dat is a simple byte stream of a fixed size structure dumped directly
# IpRangesV6.dat and Locations.dat are a bit more complicated, but are still mostly a byte stream of dumped structures
# IpRangesV6.dat and Locations.dat format:
#   * files are broken up into chunks of fixed size (1024 bytes in both cases)
#   * each chunk contains a UINT16 at offset 0 that specifies how many structures it contains
#   * starting at offset 2, variable-sized structures start:
#       * in structure order, dump all fields and:
#           * if the field is a fixed size type (eg. UINT32), dump it directly to the byte stream
#           * if the field is a variable sized type (eg. byte[]), first dump a UINT16 size of the data, then the data
#   * at the end of the file, if there is no space left for another structure, 0x00 padding until the end of the chunk
# IpRangesV6.dat specific: comparing "sparse" IP's:
#   *
# Locations.dat specific: strings are ASCII encoded

import collections
import ctypes
import datetime
import io
import ipaddress
import os
import struct
import typing
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor

# region Utils

EPOCH = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
def epoch_time_to_datetime(epoch_time: int) -> datetime.datetime:
    return EPOCH + datetime.timedelta(seconds=epoch_time)

def ipraw_to_str(raw: typing.Union[int, bytes]) -> str:
    if isinstance(raw, int):
        return str(ipaddress.IPv4Address(raw))
    else:
        return str(ipaddress.IPv6Address(raw))

def str_to_ipraw(string: str) -> typing.Union[int, bytes]:
    if "." in string:
        return int(ipaddress.IPv4Address(string))
    else:
        return int(ipaddress.IPv6Address(string)).to_bytes(6, byteorder="little")

def _chunkify(f: typing.BinaryIO, chunk_size: int) -> typing.Generator[io.BytesIO, None, None]:
    buffer = io.BytesIO()

    while True:
        data = f.read(chunk_size)
        if not data or len(data) < chunk_size:
            break

        buffer.seek(0)
        buffer.write(data)
        buffer.seek(0)

        yield buffer

class BinaryFDAdapter():
    def __init__(self, pathOrFd: typing.Union[str, typing.BinaryIO], writable: bool = False):
        self.pathOrFd = pathOrFd
        self.writable = writable
        self._toClose = False
        self.fd: typing.BinaryIO = None

    def __enter__(self) -> typing.BinaryIO:
        if isinstance(self.pathOrFd, str):
            self._toClose = True
            self.fd = open(self.pathOrFd, "wb" if self.writable else "rb")
        elif isinstance(self.pathOrFd, io.IOBase):
            self.fd = self.pathOrFd
        else:
            raise ValueError()

        return self.fd

    def __exit__(self, _, __, ___):
        if self._toClose:
            self.fd.close()

# endregion

# region Structures

class RawStatsTransferData(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
        ("data_out_payload", ctypes.c_uint32),
        ("zero1", ctypes.c_uint16), # seems to USUALLY be (WORD)0
        ("app_id", ctypes.c_uint16),
        ("data_in_payload", ctypes.c_uint32),
        ("zero2", ctypes.c_uint16), # seems to USUALLY be (WORD)0
        ("user_id", ctypes.c_uint16), # possibly user_id
        ("data_out_overhead", ctypes.c_uint32),
        ("zero3", ctypes.c_uint16), # seems to always be (WORD)0
        ("u3_flags", ctypes.c_uint16), # unknown WORD
        ("data_in_overhead", ctypes.c_uint32),
        ("zero4", ctypes.c_uint16), # seems to always be (WORD)0
        ("network_id", ctypes.c_uint16) # possibly network_id
    ]

    def __str__(self):
        return "\n".join(field + ": " + str(getattr(self, field)) for (field, _) in self._fields_)

assert (ctypes.sizeof(RawStatsTransferData) == 32)

class RawStatsRowV4(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
        ("timestamp", ctypes.c_uint32),

        ("local_ip", ctypes.c_uint32),
        ("remote_ip", ctypes.c_uint32),

        ("local_port", ctypes.c_uint16),
        ("remote_port", ctypes.c_uint16),

        ("transfer_data", RawStatsTransferData)
    ]

    def __str__(self):
        return "\n".join(field + ": " + str(getattr(self, field)) for (field, _) in self._fields_)

class RawStatsRowV6(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
        ("timestamp", ctypes.c_uint32),

        ("local_ip", ctypes.c_byte * 16),
        ("remote_ip", ctypes.c_byte * 16),

        ("local_port", ctypes.c_uint16),
        ("remote_port", ctypes.c_uint16),

        ("transfer_data", RawStatsTransferData)
    ]

    def __str__(self):
        return "\n".join(field + ": " + str(getattr(self, field)) for (field, _) in self._fields_)

class RawAppsRow(ctypes.Structure):
    # unused, only for reference
    _pack_ = 1
    _fields_ = [
        ("app_id", ctypes.c_uint16),

        ("path_length", ctypes.c_uint16),
        ("path", ctypes.c_wchar * 0)
    ]

    def __str__(self):
        return "\n".join(field + ": " + str(getattr(self, field)) for (field, _) in self._fields_)

class RawUsersRow(ctypes.Structure):
    # unused, only for reference
    _pack_ = 1
    _fields_ = [
        ("user_id", ctypes.c_uint16),

        ("user_sid_length", ctypes.c_uint16),
        ("sid", ctypes.c_byte * 0)
    ]

    def __str__(self):
        return "\n".join(field + ": " + str(getattr(self, field)) for (field, _) in self._fields_)

class RawIPv4LocationRangeRow(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
        ("location_id", ctypes.c_uint32),
        ("start_ip", ctypes.c_uint32),
        ("end_ip", ctypes.c_uint32),
    ]

    def __str__(self):
        return "\n".join(field + ": " + str(getattr(self, field)) for (field, _) in self._fields_)

class RawIPv6LocationRangeRow(ctypes.Structure):
    # unused, only for reference
    _pack_ = 1
    _fields_ = [
        ("location_id", ctypes.c_uint32),
        ("start_ip_length", ctypes.c_uint16),
        ("start_ip", ctypes.c_byte * 0),
        ("end_ip_length", ctypes.c_uint16),
        ("end_ip", ctypes.c_byte * 0),
    ]

    def __str__(self):
        return "\n".join(field + ": " + str(getattr(self, field)) for (field, _) in self._fields_)
class RawIPv6LocationRangeChunk(ctypes.Structure):
    # unused, only for reference
    _pack_ = 1024
    _fields_ = [
        ("item_count", ctypes.c_uint16),
        ("items_bytes", ctypes.POINTER(RawIPv6LocationRangeRow)),
        ("padding", ctypes.c_byte * 0),
    ]

class RawLocationRow(ctypes.Structure):
    # unused, only for reference
    _pack_ = 1
    _fields_ = [
        ("location_id", ctypes.c_uint32),
        ("latitude", ctypes.c_double),
        ("longitude", ctypes.c_double),
        ("town_length", ctypes.c_uint16),
        ("town", ctypes.c_byte * 0),
        ("province_length", ctypes.c_uint16),
        ("province", ctypes.c_byte * 0),
        ("country_length", ctypes.c_uint16),
        ("country", ctypes.c_byte * 0),
        ("country_code_length", ctypes.c_uint16),
        ("country_code", ctypes.c_byte * 0),
    ]

    def __str__(self):
        return "\n".join(field + ": " + str(getattr(self, field)) for (field, _) in self._fields_)
class RawLocationChunk(ctypes.Structure):
    # unused, only for reference
    _pack_ = 1024
    _fields_ = [
        ("item_count", ctypes.c_uint16),
        ("items_bytes", ctypes.POINTER(RawLocationRow)),
        ("padding", ctypes.c_byte * 0),
    ]

# endregion

# region Stats

class StatsRow:
    def __init__(self, raw_row: typing.Union[RawStatsRowV4, RawStatsRowV6]):
        self.raw_row = raw_row
        self.ip_version = 4 if isinstance(raw_row, RawStatsRowV4) else 6

        self._timestamp = None
        self.raw_timestamp = raw_row.timestamp

        self._local_ip = None
        self._remote_ip = None

        self.local_port = raw_row.local_port
        self.remote_port = raw_row.remote_port

        self.data_out_payload = raw_row.transfer_data.data_out_payload
        self.data_in_payload = raw_row.transfer_data.data_in_payload
        self.data_out_overhead = raw_row.transfer_data.data_out_overhead
        self.data_in_overhead = raw_row.transfer_data.data_in_overhead
        self.data_out_total = self.data_out_payload + self.data_out_overhead
        self.data_in_total = self.data_in_payload + self.data_in_overhead

        self.app_id = raw_row.transfer_data.app_id
        self.network_id = raw_row.transfer_data.network_id

    @property
    def timestamp(self):
        if self._timestamp is None:
            self._timestamp = epoch_time_to_datetime(self.raw_row.timestamp).astimezone(tz=None).replace(tzinfo=None)
        return self._timestamp

    @property
    def local_ip(self):
        if self._local_ip is None:
            self._local_ip = ipraw_to_str(self.raw_row.local_ip)
        return self._local_ip

    @property
    def remote_ip(self):
        if self._remote_ip is None:
            self._remote_ip = ipraw_to_str(self.raw_row.remote_ip)
        return self._remote_ip

    def process(self):
        _ = self.timestamp
        _ = self.local_ip
        _ = self.remote_ip

class AppRow:
    def __init__(self, app_id: int, path: str):
        self.app_id = app_id
        self.path = path

    @staticmethod
    def from_stream(stream: typing.BinaryIO) -> typing.Optional["AppRow"]:
        header = stream.read(4)
        if not header:
            return None
        if len(header) != 4:
            return None

        app_id = int.from_bytes(header[0:2], byteorder="little", signed=False)
        path_length = int.from_bytes(header[2:4], byteorder="little", signed=False)

        raw = stream.read(path_length * 2)
        if not raw:
            return None
        if len(raw) != path_length * 2:
            return None

        return AppRow(app_id, raw.decode("utf-16le"))

    def to_bytes(self) -> bytes:
        b = self.app_id.to_bytes(2, byteorder="little", signed=False)
        b += (len(self.path) * 2).to_bytes(2, byteorder="little", signed=False)
        b += self.path.encode("utf-16le")
        return b

class UserRow:
    def __init__(self, user_id: int, sid: bytes):
        self.user_id = user_id
        self.sid = sid

    @staticmethod
    def from_stream(stream: typing.BinaryIO) -> typing.Optional["UserRow"]:
        header = stream.read(4)
        if not header:
            return None
        if len(header) != 4:
            return None

        user_id = int.from_bytes(header[0:2], byteorder="little", signed=False)
        user_sid_length = int.from_bytes(header[2:4], byteorder="little", signed=False)

        raw = stream.read(user_sid_length)
        if not raw:
            return None
        if len(raw) != user_sid_length:
            return None

        return UserRow(user_id, raw)

    def to_bytes(self) -> bytes:
        b = self.user_id.to_bytes(2, byteorder="little", signed=False)
        b += (len(self.sid)).to_bytes(2, byteorder="little", signed=False)
        b += self.sid
        return b

def get_apps(pathOrFd: typing.Union[str, typing.BinaryIO]) -> typing.Generator[AppRow, None, None]:
    with BinaryFDAdapter(pathOrFd) as f:
        while True:
            row = AppRow.from_stream(f)
            if not row:
                break
            yield row

def get_users(pathOrFd: typing.Union[str, typing.BinaryIO]) -> typing.Generator[UserRow, None, None]:
    with BinaryFDAdapter(pathOrFd) as f:
        while True:
            row = UserRow.from_stream(f)
            if not row:
                break
            yield row

def __get_rows_commons(pathOrFd: typing.Union[str, typing.BinaryIO], ipv6: bool = None):
    bfda = BinaryFDAdapter(pathOrFd)
    if isinstance(pathOrFd, str):
        if ipv6 is None:
            ipv6 = "6" in os.path.split(pathOrFd)[1]
    elif isinstance(pathOrFd, io.IOBase):
        if ipv6 is None:
            raise ValueError()
    else:
        raise ValueError()
    classType = RawStatsRowV6 if ipv6 else RawStatsRowV4
    sizeof = ctypes.sizeof(classType)

    return (bfda, classType, sizeof)

def get_rows(pathOrFd: typing.Union[str, typing.BinaryIO], ipv6: bool = None) -> typing.Generator[StatsRow, None, None]:
    (bfda, classType, sizeof) = __get_rows_commons(pathOrFd, ipv6)

    with bfda as f:
        while True:
            buffer = f.read(sizeof)
            if not buffer:
                break
            yield StatsRow(classType.from_buffer_copy(buffer))

def __process_batch(classType, sizeof, buffer):
    data_rows = []
    offset = 0

    while offset < len(buffer):
        data_rows.append(StatsRow(classType.from_buffer_copy(buffer, offset)))
        offset += sizeof

    return data_rows
def get_batched_rows(pathOrFd: typing.Union[str, typing.BinaryIO], ipv6: bool = None, batch_size: int = 1000) -> typing.Generator[typing.List[StatsRow], None, None]:
    (bfda, classType, sizeof) = __get_rows_commons(pathOrFd, ipv6)
    batch_byte_size = sizeof * batch_size

    futures = collections.deque()
    with ThreadPoolExecutor() as pool, bfda as f:
        while True:
            buffer = f.read(batch_byte_size)
            if not buffer:
                break

            if len(futures) > 0:
                if futures[0].done():
                    yield futures.popleft().result()
            futures.append(pool.submit(__process_batch, classType, sizeof, buffer))

    for future in futures:
        yield future.result()

# endregion

# region Locations

class IpRange:
    def __init__(self, raw_row: typing.Optional[RawIPv4LocationRangeRow]):
        self.raw_row = raw_row
        self.ip_version = 4

        self.location_id = raw_row.location_id if raw_row else None
        self.start_ip = raw_row.start_ip if raw_row else None
        self.end_ip = raw_row.end_ip if raw_row else None

    def contains_ip(self, ip: typing.Union[int, bytes]) -> bool:
        if (self.ip_version == 4 and isinstance(ip, int)) or \
                (self.ip_version == 6 and isinstance(ip, bytes)):
            return self.start_ip <= ip <= self.end_ip

        return False

def _get_ranges_v6(f: typing.BinaryIO) -> typing.Generator[IpRange, None, None]:
    for chunk_stream in _chunkify(f, 1024):
        [item_count] = struct.unpack("H", chunk_stream.read(2))
        if item_count == 0:
            break

        for i in range(item_count):
            [location_id] = struct.unpack("I", chunk_stream.read(4))
            [length] = struct.unpack("H", chunk_stream.read(2))
            start_ip = chunk_stream.read(length)
            [length] = struct.unpack("H", chunk_stream.read(2))
            end_ip = chunk_stream.read(length)

            iprange = IpRange(None)
            iprange.ip_version = 6
            iprange.location_id = location_id
            iprange.start_ip = start_ip
            iprange.end_ip = end_ip
            yield iprange

def get_ranges(path: str) -> typing.Generator[IpRange, None, None]:
    version = 4 if "4" in os.path.split(path)[1] else 6

    with open(path, "rb") as f:
        if version == 4:
            sizeof = ctypes.sizeof(RawIPv4LocationRangeRow)
            while True:
                buffer = f.read(sizeof)
                if not buffer:
                    break
                yield IpRange(RawIPv4LocationRangeRow.from_buffer_copy(buffer))
        else:
            yield from _get_ranges_v6(f)

class LocationRow:
    def __init__(self, id: int, latitude: float, longitude: float, town: str, province: str, country: str, country_code: str):
        self.id = id
        self.latitude = latitude
        self.longitude = longitude
        self.town = town
        self.province = province
        self.country = country
        self.country_code = country_code

    @staticmethod
    def from_stream(stream: typing.BinaryIO) -> typing.Optional["LocationRow"]:
        [id] = struct.unpack("I", stream.read(4))
        (latitude, longitude) = struct.unpack("dd", stream.read(8 * 2))
        strings = []
        for i in range(4):
            [length] = struct.unpack("H", stream.read(2))
            strings.append(stream.read(length).decode("ASCII"))

        return LocationRow(id, latitude, longitude, *strings)

def get_locations(path: str) -> typing.Generator[LocationRow, None, None]:
    with open(path, "rb") as f:
        for chunk_stream in _chunkify(f, 1024):
            [item_count] = struct.unpack("H", chunk_stream.read(2))
            if item_count == 0:
                break

            for i in range(item_count):
                row = LocationRow.from_stream(chunk_stream)
                if not row:
                    break
                yield row

# endregion


from itertools import combinations_with_replacement

def solution(n, info):
    score = -1
    answer = [-1]
    i = list(combinations_with_replacement(range(0,11),n))
    for a in i:
        tmp = calculate(a, info, n)
        if tmp[0] > score:
            score = tmp[0]
            answer = tmp[1]
    return answer

def calculate(a, info, n):
    tmp = [0] * 11
    rion = 0
    peach = 0
    for i in a:
        tmp[10 - i] += 1
    for i in range(len(tmp)):
        if not (tmp[i] == 0 and info[i] == 0):
            if tmp[i] > info[i]:
                rion += 10 - i
            else:
                peach += 10 - i
    if rion > peach:
        return [rion - peach, tmp]
    else:
        return [-1, [-1]]


from __future__ import annotations

import argparse
from pathlib import Path

import vortex.manage.cli as cli

from tornado.tasks.all import AllGroup

self_path = Path(__file__).parent.parent.parent

if __name__ == "__main__":
    tasks = AllGroup(self_path / "source")

    parser = argparse.ArgumentParser(
        description="Tornado power supply controller development automation tool",
        usage="python -m tornado.manage <component>.<task> [options...]",
    )
    cli.add_parser_args(parser, tasks)

    args = parser.parse_args()

    try:
        params = cli.read_run_params(args, tasks, self_path / "target")
    except cli.ReadRunParamsError as e:
        print(e)
        exit(1)

    cli.setup_logging(params, ["vortex", "tornado"])
    cli.run_with_params(params)


from os import makedirs, path, umask
from time import time
from moviepy.video.io.ImageSequenceClip import ImageSequenceClip
from moviepy.editor import AudioFileClip

OUTPUT_DIR = "videos"

class VideoMaker:
    def __init__(self, audio_path: str, images_path: str, duration: float, number_of_images: int, user_name: str):
        self.audio_path = audio_path
        self.images_path = images_path
        self.duration = duration
        self.number_of_images = number_of_images
        self.user_name = user_name
        self.create_videos_folder()
        self.create_video()

    def create_videos_folder(self):
        if not path.exists(OUTPUT_DIR):
            umask(0)
            makedirs(OUTPUT_DIR, exist_ok=True)

    def create_video(self):
        fps =  self.number_of_images / self.duration

        audio_clip = AudioFileClip(self.audio_path)
        audio_clip = audio_clip.set_end(self.duration)

        video_clip = ImageSequenceClip(self.images_path, fps=fps)
        video_clip.audio = audio_clip

        video_file_path = "{0}/{1}_{2}.mp4".format(OUTPUT_DIR, self.user_name, int(time()))
        video_clip.write_videofile(video_file_path)


#!/bin/python

# Importing Sqlite3 module
import sqlite3

# Creating Connection to movies database file with sqlite3
conn = sqlite3.connect('Find-All-Tarantino-Movies.db')

# Creating Cursor
c = conn.cursor()

# Creating Table
# c.execute("CREATE TABLE movies( title text, year integer, director text)")

# Creating List and putting values in list and tuples
movie_list = [
		('Rushmore', 1998, 'Wes Anderson'),
		('Isle of Dogs', 2018, 'Wes Anderson'),
		('Pulp Fiction', 1994, 'Tarantino'),
		('The Shining', 1980, 'Stanley Kubric'),
		('Django', 2012, 'Tarantino')
				]

# Inserting values in Table
# c.executemany("INSERT INTO movies VALUES(?,?,?)", movie_list)

# Query
c.execute("SELECT title, year FROM movies WHERE director = 'Tarantino'")

# Creating Variable to prints Query
movieshow = c.fetchall()
for movie in movieshow:
  print (movie)

# Displaying All Values
# print (" Show Table: All ")
# for movie in movie_list:
#   print (movie)

# Commiting to database
conn.commit()

# Closing Connection
conn.close()

SQL/Find All Tarantino Movies/Find-All-Tarantino-Movies.py