from django.shortcuts import redirect
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from .models import Post
from .models import Skill
from .models import Hobby
from .models import Responsibility
from .models import Experience
from .forms import PostForm
from .forms import SkillForm
from .forms import HobbyForm
from .forms import ExperienceForm
from .forms import ResponsibilityForm
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext

def cv(request):
    skills = Skill.objects.all()
    experience=Experience.objects.all()
    hobbies=Hobby.objects.all()
    responsibilities=Responsibility.objects.all()
    return render(request, 'blog/cv.html', {'skills': skills,'experience': experience,'hobbies': hobbies,'responsibilities': responsibilities})

def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/post_list.html', {'posts': posts})

def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm()
    return render(request, 'blog/post_edit.html', {'form': form})

def skill_new(request):
    if request.method == "POST":
        form = SkillForm(request.POST)
        if form.is_valid():
            skill = form.save(commit=False)
            skill.author = request.user
            skill.save()
            return redirect('cv')
    else:
        form = SkillForm()
    return render(request, 'blog/post_edit.html', {'form': form})
def hobby_new(request):
    if request.method == "POST":
        form = HobbyForm(request.POST)
        if form.is_valid():
            hobby = form.save(commit=False)
            hobby.save()
            return redirect('cv')
    else:
        form = HobbyForm()
    return render(request, 'blog/post_edit.html', {'form': form})

def experience_new(request):
    if request.method == "POST":
        form = ExperienceForm(request.POST)
        if form.is_valid():
            experience = form.save(commit=False)
            experience.save()
            return redirect('cv')
    else:
        form = ExperienceForm()
    return render(request, 'blog/post_edit.html', {'form': form})
def responsibility_new(request):
    if request.method == "POST":
        form = ResponsibilityForm(request.POST)
        if form.is_valid():
            responsibility = form.save(commit=False)
            responsibility.save()
            return redirect('cv')
    else:
        form = ResponsibilityForm()
    return render(request, 'blog/post_edit.html', {'form': form})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm(instance=post)
    return render(request, 'blog/post_edit.html', {'form': form})


import numpy as np
import os
from random import *
import random
from datetime import datetime
random.seed(datetime.now())

def averageDataRG(data):
    output=np.ones((int(data.shape[0]/2),int(data.shape[0]/2)))
    for i in range(0,data.shape[0]-1,2):
        for j in range(0,data.shape[1]-1,2):
            output[int(i/2),int(j/2)]=(data[i,j]+data[i+1,j]+
                data[i,j+1]+data[i+1,j+1])/4
    return output

outFolder="rg_outputs/"
if not os.path.exists(outFolder):
    os.makedirs(outFolder)

Nv=4096

data_file=["input data filepath"]

data=np.load(data_file)

output=[]
for j in range(len(data)):
    output.append(averageDataRG(data[j].reshape(64,64)).reshape((1024)).copy())

np.savetxt(outFolder + "outputL1_Nv" + str(Nv) + ".csv", output, delimiter=',')

data=output
output2=[]
for j in range(len(output)):
    output2.append(averageDataRG(output[j].reshape(32,32)).reshape((256)).copy())

np.savetxt(outFolder + "outputL2_Nv" + str(Nv) + ".csv", output2,delimiter=',')

output3=[]
for j in range(len(output2)):
    output3.append(averageDataRG(output2[j].reshape(16,16)).reshape((64)).copy())

np.savetxt(outFolder + "outputL3_Nv" + str(Nv) + ".csv", output3, delimiter=',')


import numpy as np
import cv2
from sklearn.externals import joblib

contMLP=0
contKNN=0
cap = cv2.VideoCapture(0)

while(1):
    ret,frame = cap.read()
    frame_resize = cv2.resize(frame,(100,100), interpolation = cv2.INTER_AREA)
    cv2.imshow("img",frame_resize)
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    blur = cv2.medianBlur(gray,7)
    ret,edges = cv2.threshold(blur,0,300,cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    #edges = cv2.adaptiveThreshold(blur,230,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
    edges= cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
    #cv2.imshow("edges",edges)
    #cv2.waitKey(0)

    drawing = np.zeros(frame.shape,np.uint8)

    contours, hierarchy = cv2.findContours(edges.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    draw = cv2.drawContours(frame,contours,-1,(0,0,255),3)
    #cv2.imshow("contours",draw)
    #cv2.waitKey(0)
    for component in zip(contours,hierarchy):
        currentContour = component[0]
        x,y,w,h = cv2.boundingRect(currentContour)
        #print(x,y,w,h)
        p = cv2.arcLength(currentContour,True)
        epsilon = 0.015*p
        approx = cv2.approxPolyDP(currentContour,epsilon,True)
        lados = len(approx)
        #print(lados)
        #cv2.imshow("lados", draw)
        #cv2.waitKey(0)

        empty = np.zeros((h,w),np.uint8)
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),1)
        #print(w,h)
        roi= edges[y:y+h,x:x+w]
        empty[0:h,0:w]=roi
        #cv2.imshow("roi",roi)
        cv2.imshow("edges", edges)
        cv2.waitKey(0)
        edges = empty
        #cv2.imshow("empty", edges)
        #cv2.waitKey(0)
        img_resize = cv2.resize(edges,(100,100), interpolation = cv2.INTER_AREA)
        ret, edges_res = cv2.threshold(img_resize,0,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        #cv2.imshow("img_resize",img_resize)
        #cv2.waitKey(0)

        contours_2,hierarchy_2 = cv2.findContours(edges_res.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
        hier = hierarchy[0]
        contornos=hier.shape[0]
cv2.destroyAllWindows()


import os
from pathlib import Path
from typing import Annotated

from fastapi import Form, HTTPException, Request, UploadFile, status
from sqlalchemy.orm import Session

from src.models.post import Post
from src.schemas.post import SelectedPost
from src.utils.currunt_user_id import get_current_user_id

IMGDIR = "images"

SECRET_KEY = os.environ.get("SECRET_KEY")
ALGORITHM = os.environ.get("ALGORITHM")

async def create_post(
    request: Request,
    location: Annotated[str, Form()],
    caption: Annotated[str, Form()],
    image: UploadFile,
    db: Session,
):
    user_id = get_current_user_id(request)
    data = await image.read()
    content_type = image.content_type
    filename = image.filename

    if not content_type == "image/jpeg":
        raise HTTPException(status_code=403, detail="Invalid format")

    # Create the folder if it doesn't exist
    Path(IMGDIR).mkdir(parents=True, exist_ok=True)

    with open(os.path.join(IMGDIR, filename), "wb") as my_file:
        my_file.write(data)
    new_post = Post(location=location, caption=caption, image=filename, user_id=user_id)

    db.add(new_post)
    db.commit()
    db.refresh(new_post)
    return {"Message": "Post Created"}

def get_all_post(request: Request, db: Session):
    all_post = db.query(Post).all()
    return all_post

def post_by_id(id: int, request: Request, db: Session):
    post = db.query(Post).get(id)
    with open(os.path.join(IMGDIR, post.image), "rb") as my_file:
        my_file.read()
    if not post:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Post not found"
        )
    return post

def post_with_user_(request: Request, db: Session):
    posts = db.query(Post).all()
    for post in list(posts):
        post.__dict__.update({"username": post.users.username})
    if not posts:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Post not found"
        )
    return posts

def post_user(request: Request, db: Session):
    posts = db.query(Post).all()
    post_list = []
    for post in posts:
        post_dict = {}
        post_dict["username"] = post.users.username
        post_dict["post"] = post
        post_list.append(post_dict)

    if not posts:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Post not found"
        )
    return post_list

def delete_post(id: int, request: Request, db: Session):
    post = db.query(Post).get(id)
    if not post:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Post not found"
        )
    db.delete(post)
    db.commit()
    return {"message": "Post Deleted"}

def delete_all_post(request: Request, post: SelectedPost, db: Session):
    for id in post.ids:
        post = db.query(Post).get(id)
        if post:
            db.delete(post)
            db.commit()
    return {"message": "Selected Posts Deleted"}


from abc import ABCMeta, abstractmethod
import random
import torch
import cv2
from ..transform.mosaic import merge_bboxes
import numpy as np
from torch.utils.data import Dataset
from ..transform import Pipeline

class BaseDataset(Dataset, metaclass=ABCMeta):
    """
    A base class of detection dataset. Referring from MMDetection.
    A dataset should have images, annotations and preprocessing pipelines
    NanoDet use [xmin, ymin, xmax, ymax] format for box and
     [[x0,y0], [x1,y1] ... [xn,yn]] format for key points.
    instance masks should decode into binary masks for each instance like
    {
        'bbox': [xmin,ymin,xmax,ymax],
        'mask': mask
     }
    segmentation mask should decode into binary masks for each class.

    :param img_path: image data folder
    :param ann_path: annotation file path or folder
    :param use_instance_mask: load instance segmentation data
    :param use_seg_mask: load semantic segmentation data
    :param use_keypoint: load pose keypoint data
    :param load_mosaic: using mosaic data augmentation from yolov4
    :param mode: train or val or test
    """

    def __init__(self,
                 img_path,
                 ann_path,
                 input_size,
                 pipeline,
                 keep_ratio=True,
                 use_instance_mask=False,
                 use_seg_mask=False,
                 use_keypoint=False,
                 load_mosaic=False,
                 mixup=False,
                 mode='train'
                 ):
        self.img_path = img_path
        self.ann_path = ann_path
        self.input_size = input_size
        self.pipeline = Pipeline(pipeline, keep_ratio)
        self.keep_ratio = keep_ratio
        self.use_instance_mask = use_instance_mask
        self.use_seg_mask = use_seg_mask
        self.use_keypoint = use_keypoint
        self.load_mosaic = load_mosaic
        self.mixup = mixup
        self.mode = mode

        self.data_info = self.get_data_info(ann_path)
        self.indices = list(range(len(self.data_info)))
        # print(self.indices)

    def __len__(self):
        return len(self.data_info)

    def get_valid_train_data(self, idx):
        while True:
            data = self.get_train_data(idx)
            if data is None:
                idx = self.get_another_id()
                continue
            else:
                return data

    def get_mosaic(self, idx):
        data = self.get_valid_train_data(idx)
        w, h = self.input_size
        # print(self.input_size)
        min_offset = 0.2
        cut_x = np.random.randint(
            int(w*min_offset), int(w*(1 - min_offset)))
        cut_y = np.random.randint(
            int(h*min_offset), int(h*(1 - min_offset)))
        data0 = data
        random_index = random.sample(self.indices, 3)
        data1 = self.get_valid_train_data(random_index[0])
        data2 = self.get_valid_train_data(random_index[1])
        data3 = self.get_valid_train_data(random_index[2])
        d1 = data0['img'][:, :cut_y, :cut_x]
        d2 = data1['img'][:, cut_y:, :cut_x]
        d3 = data2['img'][:, cut_y:, cut_x:]
        d4 = data3['img'][:, :cut_y, cut_x:]

        tmp1 = torch.cat([d1, d2], 1)
        tmp2 = torch.cat([d4, d3], 1)
        mosaic_img = torch.cat([tmp1, tmp2], 2)

        labels, boxes = merge_bboxes([data0['gt_bboxes'], data1['gt_bboxes'], data2['gt_bboxes'], data3['gt_bboxes']], [
            data0['gt_labels'], data1['gt_labels'], data2['gt_labels'], data3['gt_labels']], cut_x, cut_y, w, h)
        # print(data['gt_labels'])
        # print('before:',data['gt_labels'].dtype)
        data['gt_labels'] = np.array(labels, dtype=np.int64)
        data['gt_bboxes'] = np.array(boxes, dtype=np.float32).reshape(-1, 4)
        data['img'] = mosaic_img
        return data

    def __getitem__(self, idx):
        if self.mode == 'val' or self.mode == 'test':
            return self.get_val_data(idx)
        else:
            if self.load_mosaic:
                data = self.get_mosaic(idx)
            else:
                data = self.get_valid_train_data(idx)

            if self.mixup and random.random() < 0.5:
                if self.load_mosaic:
                    data1 = self.get_mosaic(self.get_another_id())
                else:
                    data1 = self.get_valid_train_data(self.get_another_id())

                r = np.random.beta(8.0, 8.0)  # mixup ratio, alpha=beta=8.0
                data['img'] = data['img'] * r + data1['img'] * (1 - r)
                data['gt_labels'] = np.concatenate([data['gt_labels'],data1['gt_labels']])
                data['gt_bboxes'] = np.concatenate([data['gt_bboxes'],data1['gt_bboxes']])

            return data

    @abstractmethod
    def get_data_info(self, ann_path):
        pass

    @abstractmethod
    def get_train_data(self, idx):
        pass

    @abstractmethod
    def get_val_data(self, idx):
        pass

    def get_another_id(self):
        return np.random.random_integers(0, len(self.data_info)-1)


import os
from tkinter import *
import tkinter.messagebox
from tkinter import filedialog
from pygame import mixer  # helps play music
import time
from mutagen.mp3 import MP3

root = Tk()  # creates_a_window

statusBar = Label(root, text="Music Player Running", relief=SUNKEN, anchor=W)  # creating statusBar at bottom of window
statusBar.pack(side=BOTTOM, fill=X)

menuBar = Menu(root)  # creating a menu bar
root.config(menu=menuBar)

# creating items of menu bar

def browse_file():
    global filename
    filename = filedialog.askopenfilename()

subMenu = Menu(menuBar, tearoff=0)  # to get rid of the dotted line
menuBar.add_cascade(label="file", menu=subMenu)
subMenu.add_command(label="Open", command=browse_file)
subMenu.add_command(label="Exit", command=root.destroy)

def about_us():
    tkinter.messagebox.showinfo("About Music Player", "This is a music player built using the Tkinter, Pygame and "
                                                      "Mutagen libraries of python by Epsisri  Potluri, "
                                                      "RA1711003011248.")

subMenu = Menu(menuBar, tearoff=0)
menuBar.add_cascade(label="Help", menu=subMenu)
subMenu.add_command(label="About", command=about_us)

mixer.init()  # initialize mixer

# root.geometry('500x500')  # specify the dimensions of the window
root.title("Music Player")  # change the title from Tk to Music Player
# to change icon we downloaded an ico file and pasted it in this directory
root.iconbitmap(r'music_player.ico')  # to change icon

rightFrame = Frame(root)
rightFrame.pack()

topFrame = Frame(rightFrame)
topFrame.pack()

topLabel = Label(topFrame, text='Play the music of your choice!')
topLabel.pack(pady=10)  # to show in window

lengthLabel = Label(topFrame, text="Total duration: 00:00", relief=GROOVE)  # to show length of entire song
lengthLabel.pack()

def show_details():
    topLabel['text'] = "Now Playing -" + os.path.basename(filename)
    fileData = os.path.splitext(filename)
    if fileData[1] == '.mp3':   # finding total duration of song
        audio = MP3(filename)
        totalLength = audio.info.length
    else:
        a = mixer.Sound(filename)
        totalLength = a.get_length()
    mins,secs = divmod(totalLength,60)
    mins = round(mins)
    secs = round(secs)
    timeFormat = '{:02d}:{:02d}'.format(mins, secs)
    lengthLabel['text'] = "Total duration -" + timeFormat

def play_music():
    try:
        paused
    except NameError:
        try:
            mixer.music.load(filename)  # load the mp3 file we are playing
            mixer.music.play()
            statusBar['text'] = "Now Playing " + os.path.basename(filename)  # os.path.basename() is to remove url
            show_details()
        except:
            tkinter.messagebox.showerror("Song not selected", "Please select a song to continue")
    else:
        mixer.music.unpause()
        statusBar['text'] = "Music Resumed"

def pause_music():
    global paused
    paused = TRUE
    mixer.music.pause()  # pause the music
    statusBar['text'] = "Music has been paused"

def stop_music():
    mixer.music.stop()  # stop the music
    statusBar['text'] = "Music has stopped"

def set_vol(val):
    volume = int(val) / 100  # mixer takes values from 0 to 1
    mixer.music.set_volume(volume)

muted = FALSE

def mute_music():
    global muted
    if muted:  # un-mute music
        mixer.music.set_volume(0.75)
        volumeBut.configure(image=volumePhoto)
        scale.set(75)
        muted = FALSE
        statusBar['text'] = "Music has been un-muted"
    else:  # mute the music
        mixer.music.set_volume(0)
        volumeBut.configure(image=mutePhoto)
        scale.set(0)
        muted = TRUE
        statusBar['text'] = "Music has been muted"

middleFrame = Frame(rightFrame)  # create frame to group buttons
middleFrame.pack(padx=10, pady=10)

pausePhoto = PhotoImage(file='pause-button.png')
pauseBut = Button(middleFrame, image=pausePhoto, command=pause_music)  # creating a pause button
pauseBut.grid(row=0, column=0, padx=10)

playPhoto = PhotoImage(file='play.png')
playBut = Button(middleFrame, image=playPhoto, command=play_music)  # creating a play button
playBut.grid(row=0, column=1, padx=10)

stopPhoto = PhotoImage(file='stop.png')
stopBut = Button(middleFrame, image=stopPhoto, command=stop_music)  # creating a stop button
stopBut.grid(row=0, column=2, padx=10)

bottomFrame = Frame(rightFrame)  # create frame for volume
bottomFrame.pack(padx=10, pady=10)

mutePhoto = PhotoImage(file='mute.png')
volumePhoto = PhotoImage(file='volume.png')
volumeBut = Button(bottomFrame, image=volumePhoto, command=mute_music)  # creating a stop button
volumeBut.grid(row=0, column=0, padx=10)

scale = Scale(bottomFrame, from_=0, to=100, orient=HORIZONTAL, command=set_vol)  # creating a volume modulator
scale.set(75)  # setting a default value in the widget
mixer.music.set_volume(0.75)  # set the default volume to 75
scale.grid(row=0, column=1, padx=10)

root.mainloop()  # engage momentary window in infinite loop


## built-in modules
import os
import time
from pathlib import Path
## pip installed modules
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.edge.service import Service as EdgeService

def setup_driver(browser_name, browser_driver_path, headless=None, url=None):
    """Helper function to setup the webdriver."""
    if browser_name == 'chrome':
        driver_exe = 'chromedriver_win64_v119.exe'
        s = ChromeService(browser_driver_path / driver_exe)
    elif browser_name == 'edge':
        driver_exe = 'msedgedriver_win64_v118.exe'
        s = EdgeService(browser_driver_path / driver_exe)
    else:
        raise ValueError(f"Unsupported browser: {browser_name}")

    options = webdriver.ChromeOptions() if browser_name == 'chrome' else webdriver.EdgeOptions()
    if headless:
        options.add_argument('--headless')
    driver = webdriver.Chrome(service=s, options=options) if browser_name == 'chrome' else webdriver.Edge(service=s, options=options)
    if url:
        driver.get(url)
    return driver

def save_html_from_url(driver, html_file_name, html_path=None):
    html_file_name = Path(html_file_name)
    if not html_path:
        html_path = Path(os.getcwd())
    html_path = Path(html_path)
    html = driver.page_source
    time.sleep(2)
    if str(html_file_name.parent) == '.':
        html_path_file = html_path / "{}.html".format(html_file_name.stem)
    else:
        html_path_file = html_path / html_file_name.parent / "{}.html".format(html_file_name.stem)
    if not html_path_file.perent.is_dir():
        html_path_file.mkdir(parents=True)
    with open(html_path_file, 'w') as f:
        f.write(html)
    # print(html)

def save_complete_screenshot(driver: webdriver.Chrome, path: str = '/tmp/screenshot.png', headless=False) -> None:
    nap_time = .5
    ## Set window size
    if not headless:
        time.sleep(nap_time)
    original_size = driver.get_window_size()
    driver.maximize_window()
    required_width = driver.execute_script('return document.body.parentNode.scrollWidth')
    required_height = driver.execute_script('return document.body.parentNode.scrollHeight')
    driver.set_window_size(required_width, required_height)
    if not headless:
        time.sleep(nap_time)
    driver.find_element(By.TAG_NAME, "body").screenshot(path)  # avoids scrollbar
    driver.set_window_size(original_size['width'], original_size['height'])
    if not headless:
        time.sleep(nap_time)

def save_html_to_png(html_file_name, html_path=None, browser_name=None, browser_driver_path=None, headless=False):
    """Converts an HTML file to a PNG image using Selenium.

    Args:
        html_file_name: The name of the HTML file to convert.
        html_path: The directory of the HTML file. Defaults to the current working directory.
        browser_name: The name of the browser to use. Defaults to "edge".
        browser_driver_path: The path to the browser driver. Defaults to a subdirectory 'browser_drivers' in the current working directory.
        headless: Whether to run the browser in headless mode. Defaults to False.

    Raises:
        FileNotFoundError: If the HTML file or the browser driver is not found.
        Exception: If the browser fails to launch.
    """
    if html_path:
        html_path = Path(html_path)
    else:
        html_path = Path(os.getcwd())
    html_file_name = Path(html_file_name)

    if not browser_name:
        browser_name = "edge"

    if not browser_driver_path:
        browser_driver_path = Path(os.getcwd()) / 'browser_drivers'

    driver = setup_driver(browser_name, browser_driver_path, headless)

    file_url = f'file:///{(html_path / html_file_name.stem)}.html'
    png_path_file = f'{(html_path / html_file_name.stem)}.png'
    driver.get(file_url)
    save_complete_screenshot(driver=driver, path=png_path_file, headless=headless)
    driver.quit()
    return png_path_file


# FOR UNIT TESTS commented line 34
# This file contains all of the classes relating to Tools and placement
# so, the toolbar will recieve a click and say "selected_tool = LineDrawer()" for example

import pygame
from line import Line, LineManager, Orb
from sidebar import Sidebar
from toolbar import Toolbar
from datetime import datetime

# the parent class of all tools. main makes it follow the mouse
class Tool():
    def __init__(self, lineManager: LineManager, sidebar: Sidebar) -> None:
        self.x = 0
        self.y = 0
        self.cursor_image = None # child instance sets after super init
        self.cursor_rect = None

        # when a new tool is initialized, reset some things
        sidebar.hide()
        lineManager.lineEditing = None

    # called by main to update tool position
    def move_to(self, pos: tuple):
        self.x = pos[0]
        self.y = pos[1]

    # performs tool action. overridden by children. returns success or failure
    # tools should only ever need to talk to the LineManager and UI elements
    def clickAction(self, lineManager: LineManager, toolbar: Toolbar, sidebar: Sidebar) -> bool:
        if str(type(lineManager)) != "<class 'line.LineManager'>" or \
            str(type(sidebar)) != "<class 'sidebar.Sidebar'>" or \
                str(type(toolbar)) != "<class 'toolbar.Toolbar'>":
            raise TypeError
        # print("default tool action")
        return True

    # performs tool hover function. overridden by some children (mouse motion triggers this)
    def hoverAction(self, lineManager: LineManager) -> bool:
        return True

    # called by main to draw the cursor image of the tool
    def drawTool(self, window_surface: pygame.Surface):
        # align rect so that the bottom left corner of the image is at the cursor
        r = self.cursor_rect
        if r is not None:
            r.x = self.x - r.width
            r.y = self.y - r.height
            window_surface.blit(self.cursor_image, r)
        return 0

    # called by child to load up and set the cursor image of the tool
    def setCursorImage(self, filePath: str):
        self.cursor_image = pygame.image.load(filePath).convert()
        self.cursor_rect = self.cursor_image.get_rect()

# contains functionality for drawing lines
class LineDrawer(Tool):
    def __init__(self, lineManager: LineManager, sidebar: Sidebar) -> None:
        super().__init__(lineManager, sidebar)

        # load image for LineDrawer
        super().setCursorImage("resources/line_drawer_cursor.png")

    # line click override
    def clickAction(self, lineManager: LineManager, toolbar: Toolbar, sidebar: Sidebar) -> bool:
        return lineManager.addLine((self.x, self.y))

# contains functionality for drawing orbs on lines
class OrbDrawer(Tool):
    def __init__(self, lineManager: LineManager, sidebar: Sidebar) -> None:
        super().__init__(lineManager, sidebar)

        # load image for OrbDrawer
        super().setCursorImage("resources/orb_drawer_cursor.png")

    # orb click override
    def clickAction(self, lineManager: LineManager, toolbar: Toolbar, sidebar: Sidebar) -> bool:
        l = lineManager.getLineAtOrOver((self.x, self.y))
        if l is not None:
            l.addOrb((self.x, self.y), lineManager.gridOffset)
        else:
            print("can't add orb: not over line")

# contains functionality for editing orbs and lines
class Edit(Tool):
    def __init__(self, lineManager: LineManager, sidebar: Sidebar) -> None:
        super().__init__(lineManager, sidebar)

        # load image for Edit tool
        super().setCursorImage("resources/edit_cursor.png")

    # edit click override
    def clickAction(self, lineManager: LineManager, toolbar: Toolbar, sidebar: Sidebar) -> bool:

        # get the line that the cursor is over
        targetLine = lineManager.getLineAtOrOver((self.x, self.y))

        # calc the side to show the sidebar on
        side = 1
        if self.x > (lineManager.screenDimensions[0] / 2):
            side = 0

        if targetLine:
            sidebar.showLineEdit(targetLine, side)

        return True

# contains fucntionality for erasing lines and orbs
class Eraser(Tool):
    def __init__(self, lineManager: LineManager, sidebar: Sidebar) -> None:
        super().__init__(lineManager, sidebar)

        # load image for Eraser
        super().setCursorImage("resources/eraser_cursor.png")

    # TODO: Allow users to drag on the grid holding the eraser and delete parts of lines
    def dragAction(self, lineManager: LineManager, toolbar: Toolbar, sidebar: Sidebar) -> bool:
        # get cursor location
        # if cursor location is close to a dot (less than half the distance between dots on the grid):
        #   get line at cursor position
        #
        pass

# contains functionality for duplicating lines and their orbs
class Duplicator(Tool):
    def __init__(self, lineManager: LineManager, sidebar: Sidebar) -> None:
        super().__init__(lineManager, sidebar)

        # load image for Duplicator
        super().setCursorImage("resources/duplicator_cursor.png")

# contains functionality for eye dropper
class EyeDropper(Tool):
    def __init__(self, lineManager: LineManager, sidebar: Sidebar) -> None:
        super().__init__(lineManager, sidebar)

        # load image for Eye dropper
        super().setCursorImage("resources/eye_dropper_cursor.png")

# contains functionality for export board. writes to a file with all lines, points, and orbs
class ExportBoard(Tool):
    def __init__(self, lineManager: LineManager, sidebar: Sidebar) -> None:
        super().__init__(lineManager, sidebar)
        now = datetime.now()
        dt_str = now.strftime("%d-%m-%Y_%H-%M-%S")
        f = open("ExportedBoard_" + dt_str + ".txt", "w")
        for line in lineManager.lines:
            f.write("Line: \n")
            for point in line.path:
                f.write("\tPoint: \n")
                f.write("\t\tPos: "+str(point.position[0])+", "+str(point.position[1])+"\n")
            for orb in line.orbs:
                f.write("\torb: \n")
                f.write("\t\tPos: "+str(orb.position[0])+", "+str(orb.position[1])+"\n")
                f.write("\t\tSpeed: "+str(orb.speed)+"\n")
        f.close()


import uuid
import datetime

def get_file_path(instance, filename):

    ext = filename.split('.')[-1]
    filename = "%s.%s" % (uuid.uuid4(), ext)
    now = datetime.datetime.now().strftime('%y-%m')
    if instance.Author.id is not None:
        return 'media/{0}/{1}/{2}'.format(instance.Author.username, now, filename)


from sqlalchemy.orm import Session
from app.db.models import Category as CategoryModel
from app.schemas.category import (
    Category as CategorySchema,
)

class CategoryUseCases:
    def __init__(self, db_session: Session) -> None:
        self.db_session = db_session

    def add_category(self, category: CategorySchema):
        category_model = CategoryModel(**category.dict())
        self.db_session.add(category_model)
        self.db_session.commit()
        self.db_session.refresh(category_model)
        return category_model

    def list_categories(self):
        categories_on_db = self.db_session.query(CategoryModel).all()
        categories = [
            CategorySchema.from_orm(category) for category in categories_on_db
        ]
        return categories


import os
import cv2

def RGBtoGray(file, file_url):
    path = os.getcwd() + file_url
    gray_image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)

    filename, extension = os.path.splitext(file)
    gray_filename = filename + '_grayscale' + extension
    gray_file_url = os.path.dirname(path) + '/' + gray_filename

    cv2.imwrite(gray_file_url, gray_image)

    return gray_filename

def face_detection(file, file_url):
    path = os.getcwd() + file_url
    img = cv2.imread(path)
    gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    face_classifier = cv2.CascadeClassifier(
            cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
    )

    face = face_classifier.detectMultiScale(
            gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(40, 40)
    )

    for (x, y, w, h) in face:
        face_img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 4)

    filename, extension = os.path.splitext(file)
    face_filename = filename + '_face' + extension
    face_file_url = os.path.dirname(path) + '/' + face_filename

    cv2.imwrite(face_file_url, face_img)

    return face_filename


#!/usr/bin/python3
import string

import cv2
import argparse
import time

def main():

    # Process arguments
    ap = argparse.ArgumentParser()
    ap.add_argument('-path1', '--file_location1', type=str, required=True,
                    help='File directory1')
    ap.add_argument('-path2', '--file_location2', type=str, required=True,
                    help='File directory2')
    args = vars(ap.parse_args())

    # Load image
    image_filename1 = args['file_location1']
    image1 = cv2.imread(image_filename1, cv2.IMREAD_COLOR) # Load an image

    image_filename2 = args['file_location2']
    image2 = cv2.imread(image_filename2, cv2.IMREAD_COLOR) # Load an image
    while(1):
        cv2.imshow('window', image1)  # Display the image
        cv2.waitKey(3000) # wait for a key press before proceeding
        cv2.imshow('window', image2)  # Display the image
        cv2.waitKey(3000)

if __name__ == '__main__':
    main()


"""ohca URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from ohca import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('case/id/', views.case_by_id.as_view(), name='case'),
    path('case/id/multi/', views.case_by_id_multi.as_view(), name='case_multi'),
    path('case/dispatch/', views.case_by_disp.as_view(), name='dispatch'),
    path('case/dispatch/multi/', views.case_by_disp_multi.as_view(), name='dispatch_multi'),
    path('system/', views.system_view.as_view(), name='system'),
    path('locale/', views.locale_view.as_view(), name='locale')
]


'''Python | Frequency of each character in String'''

def solve(s):
    dict={}
    for i in s:
        if i in dict:
            dict[i]+=1
        else:
            dict[i]=1
    return dict

def solve1(s):
    from collections import Counter
    return Counter(s)

#Using dict.get()
'''
   get() method is used to check the previously occurring
   character in string, if its new, it assigns 0 as initial
   and appends 1 to it, else appends 1 to previously holded
   value of that element in dictionary.
'''
def solve2(s):
    res = {}
    for key in s:
        res[key]=res.get(key,0)+1
    return res

#Driver Code
if __name__ == "__main__":
    print(solve('GeeksforGeeks'))
    print(solve1('GeeksforGeeks'))
    print(solve2('GeeksforGeeks'))


from collections.abc import Mapping, Sequence
from typing import Any, Optional
import tensorflow as tf
from tensorflow_privacy.privacy.fast_gradient_clipping import type_aliases
from tensorflow_privacy.privacy.fast_gradient_clipping.registry_functions import registry_function_utils

def nlp_on_device_embedding_layer_computation(
    layer_instance: tf.keras.layers.Layer,
    input_args: Sequence[Any],
    input_kwargs: Mapping[str, Any],
    tape: tf.GradientTape,
    num_microbatches: Optional[tf.Tensor] = None,
) -> type_aliases.RegistryFunctionOutput:
  """Registry function for `tfm.nlp.layers.OnDeviceEmbedding`.

  Args:
    layer_instance: A `tfm.nlp.layers.OnDeviceEmbedding` instance.
    input_args: See `dense_layer_computation()` in `dense.py`.
    input_kwargs: See `dense_layer_computation()` in `dense.py`.
    tape: See `dense_layer_computation()` in `dense.py`.
    num_microbatches: See `dense_layer_computation()` in `dense.py`.

  Returns:
    See `dense_layer_computation()` in `dense.py`.
  """
  if input_kwargs:
    raise ValueError("Embedding layer calls should not receive kwargs.")
  del input_kwargs
  if len(input_args) != 1:
    raise ValueError("Only layer inputs of length 1 are permitted.")
  if hasattr(layer_instance, "_use_one_hot"):
    if layer_instance._use_one_hot:  # pylint: disable=protected-access
      raise NotImplementedError(
          "The embedding feature '_use_one_hot' is not supported."
      )
  # NOTE: Since the implementation of `tfm.nlp.layers.OnDeviceEmbedding` uses
  # `.set_shape()`, we can assume that inputs are not ragged.
  input_ids = tf.cast(*input_args, tf.int32)
  if len(layer_instance.trainable_variables) != 1:
    raise ValueError(
        "Only layer instances with only one set of trainable variables"
        "are permitted."
    )
  base_vars = layer_instance.trainable_variables[0]
  tape.watch(base_vars)
  outputs = layer_instance(input_ids)

  def sqr_norm_fn(base_vars_grads: tf.IndexedSlices):
    return registry_function_utils.embedding_sqr_norm_fn(
        base_vars_grads.values,
        input_ids,
        num_microbatches,
    )

  return base_vars, outputs, sqr_norm_fn


from machinable import Component
from neuroh5.io import read_population_names
from typing import Dict
from miv_simulator.utils.io import H5FileManager

class NeuroH5Graph(Component):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._graph = None

    @property
    def graph(self) -> None:
        if self._graph is None:
            self._graph = H5FileManager(self.local_directory())
        return self._graph

    def __call__(self) -> None:
        print("Merging H5 data")
        self.network = None
        self.synapses = {}
        self.synapse_forest = {}
        self.connections = {}
        populations = []
        for u in self.uses:
            name = u.module.replace("miv_simulator.interface.", "")
            if name == "network_architecture":
                self.network = u
            elif name == "connections":
                populations = read_population_names(u.config.forest_filepath)
                for p in populations:
                    if p in self.connections:
                        raise ValueError(
                            f"Redundant distance connection specification for population {p}"
                            f"Found duplicate in {u.config.forest_filepath}, while already "
                            f"defined in {self.connections[p].config.forest_filepath}"
                        )
                    self.connections[p] = u
            elif name == "synapse_forest":
                if u.config.population in self.synapse_forest:
                    raise ValueError(
                        f"Redundant distance connection specification for population {u.config.population}"
                        f"Found duplicate in {u}, while already "
                        f"defined in {self.synapse_forest[u.config.population]}"
                    )
                self.synapse_forest[u.config.population] = u
            elif name == "synapses":
                if u.config.population in self.synapses:
                    raise ValueError(
                        f"Redundant specification for population {u.config.population}"
                        f"Found duplicate in {u}, while already "
                        f"defined in {self.synapses[u.config.population]}"
                    )
                self.synapses[u.config.population] = u

        self.graph.import_h5types(self.network.config.filepath)
        self.graph.import_soma_coordinates(
            self.network.config.filepath,
            populations=list(populations) + ["STIM"],
        )
        for p in self.synapse_forest.keys():
            self.graph.import_synapse_attributes(
                p,
                self.synapse_forest[p].output_filepath,
                self.synapses[p].output_filepath,
            )
        self.graph.copy_stim_coordinates()

        for p, c in self.connections.items():
            self.graph.import_projections(p, c.output_filepath)

    def on_compute_predicate(self):
        def generate_uid(use):
            if getattr(use, "refreshed_at", None) is not None:
                return f"{use.uuid}-{use.refreshed_at}"
            return use.uuid

        return {
            "uses": sorted(
                map(
                    generate_uid,
                    self.uses,
                )
            )
        }

    def files(self) -> Dict[str, str]:
        return {
            "cells": self.graph.cells_filepath,
            "connections": self.graph.connections_filepath,
        }


import logging
from operator import itemgetter
from ..lib import text

class AnswerEvaluator(object):
    def __init__(self):
        self.threshold = .01

    def evaluate(self, played_question, proposal):
        answers = filter(lambda a: text.word_count(proposal, clean=True) >=
                            min(text.word_count(a.phrase, clean=True), 2),
                            played_question.remaining_answers)
        if not answers:
            return
        scores = [self._score(proposal, a) for a in answers]
        best_score, best_answer = max(zip(scores, answers), key=itemgetter(0))
        print("Scores = %s" % zip(scores, answers))
        if best_score >= self.threshold:
            played_question.add_answer_found(best_answer)
            return best_answer
        else:
            return

    def _score(self, proposal, answer):
        given_answer_tok = text.tokenise(proposal, clean=True)
        if not given_answer_tok:
            return 0.
        weights = [answer.keywords.get(w, 0.) for w in given_answer_tok]
        score = sum(weights)/float(len(given_answer_tok))
        return all(w > 0 for w in weights)*score


import datetime
from pprint import pprint
import requests
from nonebot import on_command, CommandSession  # 命令的对话
from nonebot import permission as perm

from .. import sql_tables as sql_

# def 前要加async
# send 前加await
# （二者搭配使用）试图处理多个消息（来自不同人消息）
# 用于python引进异步IO的两个关键字

@on_command('get_menber_count', aliases=['总人数'], permission=perm.GROUP_MEMBER)
async def _(session: CommandSession):
    group_id = session.ctx['group_id']
    member_list = await session.bot.get_group_member_list(
        group_id=group_id
    )
    await session.send(f'群里一共有{len(member_list)}个人')

@on_command('我要打卡', aliases=['我要参加打卡'], permission=perm.GROUP_MEMBER)
async def _(session: CommandSession):
    msg = session.ctx
    pprint(msg)

    u_id = msg['user_id']
    u_name = msg['sender']['nickname']
    u_sex = msg['sender']['sex']
    u_age = msg['sender']['age']
    print(u_id, u_name, u_sex, u_age)
    cre_a, na_a = sql_.select_user_credit(u_id)

    if cre_a != -1:
        await session.send('您已经参加打卡了哦~~~，希望你这个小马猴可以坚持下去呀')
        await session.send('其他想要参加打卡的马猴可以@我发送‘我要参加打卡’哦~~')
        await session.send('打卡开放时间为6:00~8:20和18：00~21:00')
    else:
        sql_.add_uesr(u_id, u_name, u_sex, u_age)
        await session.send('成功参加打卡，希望马猴可以按时打卡哟~')
        await session.send('打卡开放时间为6:00~8:20和18：00——21:00')
        await session.send('打卡的规则：一天仅能打两次卡，高数一次，英语一次。')
        await session.send('若要打卡，请艾特我发送需要打卡的科目')
        await session.send('格式：英语打卡、英语、高数打卡、高数')
        await session.send('其他想要参加打卡的马猴可以@我发送‘我要参加打卡’哦~~')

# @on_command('打卡', aliases=['今日打卡'], permission=perm.GROUP_MEMBER)
# async def _(session: CommandSession):
#     msg = session.ctx
#     pprint(msg)
#
#     u_id = msg['user_id']
#     u_name = msg['sender']['nickname']
#     u_sex = msg['sender']['sex']
#     u_age = msg['sender']['age']
#     print(u_id, u_name, u_sex, u_age)
#     sql_.add_uesr(u_id, u_name, u_sex, u_age)
#     # group_id = msg['group_id']
#     await session.send('成功参加打卡')

@on_command('英语打卡', aliases=['英语'], permission=perm.GROUP_MEMBER)
async def _(session: CommandSession):
    msg = session.ctx
    pprint(msg)
    today = datetime.date.today()
    u_id = msg['user_id']
    c_type = 'English'
    c_credit = 1
    flag = sql_.add_record(u_id, c_type, c_credit, today)
    cre_a, na_a = sql_.select_user_credit(u_id)
    if flag == -1:
        await session.send(na_a + "，今日已打过英语，不能重复打卡")
    else:

        await session.send(na_a + '打卡成功')
        await session.send('英语积分+1')
        await session.send('当前总积分为' + str(cre_a))

@on_command('高数打卡', aliases=['高数', '数学'], permission=perm.GROUP_MEMBER)
async def _(session: CommandSession):
    msg = session.ctx
    pprint(msg)
    today = datetime.date.today()
    u_id = msg['user_id']
    c_type = 'Math'
    c_credit = 1
    flag = sql_.add_record(u_id, c_type, c_credit, today)
    cre_a, na_a = sql_.select_user_credit(u_id)
    if flag == -1:
        await session.send(na_a + "，今日已打过高数，不能重复打卡")
    else:
        await session.send(na_a + '打卡成功')
        await session.send('高数积分+1')
        await session.send('当前总积分为' + str(cre_a))

@on_command('积分', aliases=['查询积分', '查询'], permission=perm.GROUP_MEMBER)
async def _(session: CommandSession):
    msg = session.ctx
    pprint(msg)
    u_id = msg['user_id']
    cre_a, na_a = sql_.select_user_credit(u_id)
    if cre_a != -1:
        await session.send(na_a + '当前总积分为' + str(cre_a))
    else:
        await session.send('暂时没有您的数据呢')

@on_command('每日一句', aliases=['每日', '美句'], permission=perm.GROUP_MEMBER)
async def _(session: CommandSession):
    resp = requests.get(
        'http://api.youngam.cn/api/one.php',
        headers={
            'User-Agent': 'Mozilla/5.0(Windows NT 10.0;Win64;x64) AppleWebKit/537.36 (KHTML, like Gercko) Chrome/71.0.3578.98 Safari/537.36'
        }
    )
    data = resp.json()
    pprint(data)
    readings = data.get('data')

    if not readings:
        await session.send('诶呀，暂时没有数据呢~~')

    reply = '每日一句：'
    reading = readings[0]
    title = reading.get('text', '未知内容')
    reply += f'\n{title}\n'
    # reply += '\n' + title + '\n' + url + '\n'
    # reply += '\n{}\n{}\n'.format(title, url)
    # reply += '\n%s\n%s\n' % (title, url)
    await session.send(reply)

@on_command('英文每日一句', aliases=['英文美句', '英语美句'], permission=perm.GROUP_MEMBER)
async def _(session: CommandSession):
    STORY_URL_FORMAT = 'http://daily.zhihu.com/story/{}'  # {}表示可以新嵌入一个值

    resp = requests.get(
        'http://open.iciba.com/dsapi/',
        headers={
            'User-Agent': 'Mozilla/5.0(Windows NT 10.0;Win64;x64) AppleWebKit/537.36 (KHTML, like Gercko) Chrome/71.0.3578.98 Safari/537.36'
        }
    )
    data = resp.json()
    pprint(data)
    # stories = data['stories']
    content = data.get('content')
    note = data.get('note')

    if not content:
        await session.send('诶呀，暂时没有数据呢~~')

    reply = '英文每日一句：'
    # url = STORY_URL_FORMAT.format(reading['src'])
    reply += f'\n{content}\n{note}\n'
    # reply += '\n' + title + '\n' + url + '\n'
    # reply += '\n{}\n{}\n'.format(title, url)
    # reply += '\n%s\n%s\n' % (title, url)
    await session.send(reply)


from collections import OrderedDict
from glob import glob
from os.path import basename, exists, join, splitext

import numpy as np
from nnmnkwii.datasets import FileDataSource

# List of available speakers.
available_speakers = [
    "225",
    "226",
    "227",
    "228",
    "229",
    "230",
    "231",
    "232",
    "233",
    "234",
    "236",
    "237",
    "238",
    "239",
    "240",
    "241",
    "243",
    "244",
    "245",
    "246",
    "247",
    "248",
    "249",
    "250",
    "251",
    "252",
    "253",
    "254",
    "255",
    "256",
    "257",
    "258",
    "259",
    "260",
    "261",
    "262",
    "263",
    "264",
    "265",
    "266",
    "267",
    "268",
    "269",
    "270",
    "271",
    "272",
    "273",
    "274",
    "275",
    "276",
    "277",
    "278",
    "279",
    "280",
    "281",
    "282",
    "283",
    "284",
    "285",
    "286",
    "287",
    "288",
    "292",
    "293",
    "294",
    "295",
    "297",
    "298",
    "299",
    "300",
    "301",
    "302",
    "303",
    "304",
    "305",
    "306",
    "307",
    "308",
    "310",
    "311",
    "312",
    "313",
    "314",
    # "315", transcriptions are missing, so excludes it here
    "316",
    "317",
    "318",
    "323",
    "326",
    "329",
    "330",
    "333",
    "334",
    "335",
    "336",
    "339",
    "340",
    "341",
    "343",
    "345",
    "347",
    "351",
    "360",
    "361",
    "362",
    "363",
    "364",
    "374",
    "376",
]
assert len(available_speakers) == 108

def _parse_speaker_info(data_root):
    speaker_info_path = join(data_root, "speaker-info.txt")
    if not exists(speaker_info_path):
        raise RuntimeError(
            'speaker-info.txt doesn\'t exist at "{}"'.format(speaker_info_path)
        )

    speaker_info = OrderedDict()
    # filed_names = ["ID", "AGE", "GENDER", "ACCENTS", "REGION"]
    with open(speaker_info_path, "rb") as f:
        for line in f:
            line = line.decode("utf-8")
            fields = line.split()
            if fields[0] == "ID":
                continue
            assert len(fields) == 4 or len(fields) == 5 or len(fields) == 6
            ID = fields[0]
            speaker_info[ID] = {}
            speaker_info[ID]["AGE"] = int(fields[1])
            speaker_info[ID]["GENDER"] = fields[2]
            speaker_info[ID]["ACCENTS"] = fields[3]
            if len(fields) > 4:
                speaker_info[ID]["REGION"] = " ".join(fields[4:])
            else:
                speaker_info[ID]["REGION"] = ""
    return speaker_info

class _VCTKBaseDataSource(FileDataSource):
    def __init__(self, data_root, speakers, labelmap, max_files):
        self.data_root = data_root
        # accept both e.g., "225" and "p225"
        for idx in range(len(speakers)):
            if speakers[idx][0] == "p":
                speakers[idx] = speakers[idx][1:]
        if speakers == "all":
            speakers = available_speakers
        for speaker in speakers:
            if speaker not in available_speakers:
                raise ValueError(
                    "Unknown speaker '{}'. It should be one of {}".format(
                        speaker, available_speakers
                    )
                )
        self.speakers = speakers
        if labelmap is None:
            labelmap = {}
            for idx, speaker in enumerate(speakers):
                labelmap[speaker] = idx
        self.labelmap = labelmap
        self.labels = None
        self.max_files = max_files

        self.speaker_info = _parse_speaker_info(data_root)
        self._validate()

    def _validate(self):
        # should have pair of transcription and wav files
        for _, speaker in enumerate(self.speakers):
            txt_files = sorted(
                glob(
                    join(
                        self.data_root,
                        "txt",
                        "p" + speaker,
                        "p{}_*.txt".format(speaker),
                    )
                )
            )
            wav_files = sorted(
                glob(
                    join(
                        self.data_root,
                        "wav48",
                        "p" + speaker,
                        "p{}_*.wav".format(speaker),
                    )
                )
            )
            assert len(txt_files) > 0
            for txt_path, wav_path in zip(txt_files, wav_files):
                assert (
                    splitext(basename(txt_path))[0] == splitext(basename(wav_path))[0]
                )

    def collect_files(self, is_wav):
        if is_wav:
            root = join(self.data_root, "wav48")
            ext = ".wav"
        else:
            root = join(self.data_root, "txt")
            ext = ".txt"

        paths = []
        labels = []

        if self.max_files is None:
            max_files_per_speaker = None
        else:
            max_files_per_speaker = self.max_files // len(self.speakers)
        for idx, speaker in enumerate(self.speakers):
            speaker_dir = join(root, "p" + speaker)
            files = sorted(glob(join(speaker_dir, "p{}_*{}".format(speaker, ext))))
            files = files[:max_files_per_speaker]
            if not is_wav:
                files = list(
                    map(lambda s: open(s, "rb").read().decode("utf-8")[:-1], files)
                )
            for f in files:
                paths.append(f)
                labels.append(self.labelmap[self.speakers[idx]])
        self.labels = np.array(labels, dtype=np.int16)

        return paths

class TranscriptionDataSource(_VCTKBaseDataSource):
    """Transcription data source for VCTK dataset.

    The data source collects text transcriptions from VCTK.
    Users are expected to inherit the class and implement ``collect_features``
    method, which defines how features are computed given a transcription.

    Args:
        data_root (str): Data root.
        speakers (list): List of speakers to find. Speaker id must be ``str``.
          For supported names of speaker, please refer to ``available_speakers``
          defined in the module.
        labelmap (dict[optional]): Dict of speaker labels. If None,
          it's assigned as incrementally (i.e., 0, 1, 2) for specified
          speakers.
        max_files (int): Total number of files to be collected.

    Attributes:
        speaker_info (dict): Dict of speaker information dict. Keyes are speaker
          ids (str) and each value is speaker information consists of ``AGE``,
          ``GENDER`` and ``REGION``.
        labels (numpy.ndarray): Speaker labels paired with collected files.
          Stored in ``collect_files``. This is useful to build multi-speaker
          models.
    """

    def __init__(
        self, data_root, speakers=available_speakers, labelmap=None, max_files=None
    ):
        super(TranscriptionDataSource, self).__init__(
            data_root, speakers, labelmap, max_files
        )

    def collect_files(self):
        return super(TranscriptionDataSource, self).collect_files(False)

class WavFileDataSource(_VCTKBaseDataSource):
    """Transcription data source for VCTK dataset.

    The data source collects text transcriptions from VCTK.
    Users are expected to inherit the class and implement ``collect_features``
    method, which defines how features are computed given a transcription.

    Args:
        data_root (str): Data root.
        speakers (list): List of speakers to find. Speaker id must be ``str``.
          For supported names of speaker, please refer to ``available_speakers``
          defined in the module.
        labelmap (dict[optional]): Dict of speaker labels. If None,
          it's assigned as incrementally (i.e., 0, 1, 2) for specified
          speakers.
        max_files (int): Total number of files to be collected.

    Attributes:
        speaker_info (dict): Dict of speaker information dict. Keyes are speaker
          ids (str) and each value is speaker information consists of ``AGE``,
          ``GENDER`` and ``REGION``.
        labels (numpy.ndarray): Speaker labels paired with collected files.
          Stored in ``collect_files``. This is useful to build multi-speaker
          models.
    """

    def __init__(
        self, data_root, speakers=available_speakers, labelmap=None, max_files=None
    ):
        super(WavFileDataSource, self).__init__(
            data_root, speakers, labelmap, max_files
        )

    def collect_files(self):
        return super(WavFileDataSource, self).collect_files(True)


import traceback
from flask import render_template, redirect, request, url_for, session, flash
from app import db
from app.views.base_views import BaseViews
from app.blueprints.user_management.models.role import Role
from app.blueprints.user_management.models.audit import Audit
from app.blueprints.user_management.models.user_role import UserRole
from app.blueprints.user_management.views.access_control import only_admin

from dont_touch.core_views import render

class RolesViews(BaseViews):

    @only_admin
    def index(self):
        self.roles = Role.query.all()
        # return render_template('user_management/roles/index.html', roles=self.roles)
        return self.render('index')

    @only_admin
    def create(self):
        name = request.form['name']
        role = Role(name=name)
        db.session.add(role)
        db.session.commit()
        self.id = role.id
        return self.redirect('show')
        # return redirect(url_for('user_management.roles.show', id=role.id))

    @only_admin
    def new(self):
        return self.render('new')

    @only_admin
    def show(self, id):
        session['now_viewed'] = id
        role = Role.query.filter_by(id=id).first()

        return render_template('user_management/roles/show.html', role=role)

    @only_admin
    def edit(self, id):
        session['now_edited'] = id
        role = Role.query.get(id)
        return render_template('user_management/roles/edit.html', role=role)

    @only_admin
    def update(self, id):
        if session.get('now_edited', None) == id:
            role = Role.query.get(id)
            role.name = request.form['name']
            db.session.commit()
            flash(f'Role {role.name} edited successfully', 'success')
            return redirect(url_for('user_management.roles.show', id=id))
        else:
            flash('Irregular editing try refused', 'error')
            return redirect(url_for('user_management.roles.index')), 400

    @only_admin
    def destroy(self, id):
        try:
            if session.get('now_viewed', None) == id:
                role = Role.query.filter_by(id=id)
                role.users = []
                user_roles = UserRole.query.filter_by(role_id=role.first().id)
                user_roles.delete()
                name = role.first().name
                role.delete()
                db.session.commit()
                flash(f'Role {name} deleted successfully', 'success')
                return {
                    'message': f'Role {name} has been deleted',
                    'redirect': url_for('user_management.roles.index')
                }, 200

            else:
                return {
                    'message': f'Irregular deleting try',
                    'redirect': url_for('user_management.roles.index')
                }, 400

        except Exception as e:
            traceback.print_exc()
            flash(f'An error occurred!', 'danger')
            return {
                'message': 'Nothing happened',
                'redirect': url_for('user_management.roles.show', id=id)
            }, 500

roles_views = RolesViews()


import io
import os
import threading
from datetime import datetime as dt
from pathlib import Path
import logging

from utils.validators import (
    file_validate,
    number_validate,
)
from utils.constants import (
    russianAlphabet,
    englishAlphbet,
    digits,
    log_file,
    result_folder,
)
from utils.exceptions import (
    Info_exception,
)
from utils.compare_docx import compare_docx
from utils.compare_txt import compare_txt
from utils.dataObjects import Subtext

from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd

from babel.dates import format_datetime

eng_rus_alphabet = set.union(russianAlphabet, englishAlphbet, digits)

os.makedirs(result_folder, exist_ok=True)

logging.basicConfig(filename=log_file, format='%(asctime)s :: %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)

def select_file(path_file: Entry):
    filepath = fd.askopenfilename(
        filetypes=[
            ("All supported", ".txt .docx .pdf"),
            ("Text files", ".txt"),
            ("Word files", ".docx"),
            ("PDF files", ".pdf"),
        ]
    )
    if filepath != "":
        path_file.delete(0, END)
        path_file.insert(0, filepath)

def save_to_file(field: tk.Text):
    filepath = fd.asksaveasfilename(defaultextension='.txt', filetypes=[("Text files", ".txt")])
    if filepath != "":
        with io.open(filepath, mode='w+', encoding='utf-8') as f:
            f.write(f'{field.get(1.0, END)}')

def clear_field(field: tk.Text, label_result: tk.Label, open_folder_btn: tk.Button):
    field.delete(1.0, END)
    label_result.grid_remove()
    open_folder_btn.grid_remove()

def progress_start(pb: ttk.Progressbar, label_result: tk.Label, open_folder_btn: tk.Button):
    label_result.grid_remove()
    open_folder_btn.grid_remove()
    pb.grid()
    pb.start(25)

def progress_stop(pb: ttk.Progressbar):
    pb.stop()
    pb.grid_remove()

def open_folder(label_result: tk.Label):
    path_folder = Path(label_result.cget('text').split('\n')[1]).resolve().parent
    os.system(f'explorer {path_folder}')

def make_output(path_file1: str, path_file2: str, subs: list[Subtext]) -> str:
    file_name1 = path_file1.split('\\')[-1]
    file_name2 = path_file2.split('\\')[-1]
    output = f'Сравнение содержимого файлов на цитирование.\n\
{format_datetime(dt.now(), format="<dd MMMM yyyy> <HH:mm:ss>", locale="ru")}\n\
Файл 1: <{file_name1}>\n\
Файл 2: <{file_name2}>\n\
Предоставление найденных  совпадений:\n'
    for sub in subs:
        output += f'[{sub.linenum_file_1}|{sub.linenum_file_2}]\n{sub.quote}\n\
*******************************************************************************\n'
    return output

def find_subtext(
    path_file1: str,
    path_file2: str,
    min_words: str,
    field: tk.Text,
    info_label: tk.Label,
    pb: ttk.Progressbar,
    label_result: tk.Label,
    open_folder_btn: tk.Button,
):
    info_label.config(text='')
    if not (path_file1 and path_file2):
        info_label.config(text='Выберите файл')
        return
    path_file1 = os.path.abspath(path_file1)
    path_file2 = os.path.abspath(path_file2)
    try:
        f1_extension = file_validate(path_file1, ['txt', 'pdf', 'docx'])
        f2_extension = file_validate(path_file2, ['txt', 'pdf', 'docx'])
        min_words = number_validate(min_words)
    except Info_exception as e:
        info_label.config(text=f'{e}')
        return
    pb.grid()
    threading.Thread(target=lambda: progress_start(pb, label_result, open_folder_btn)).start()
    created_files = ()
    if f1_extension == f2_extension == 'docx':
        try:
            created_files = compare_docx(path_file1, path_file2, min_words)
            subs = compare_txt(path_file1, path_file2, min_words)
        except Info_exception as e:
            pb.grid_remove()
            progress_stop(pb)
            info_label.config(text=f'{e}')
            return
    else:
        try:
            subs = compare_txt(path_file1, path_file2, min_words)
        except Info_exception as e:
            pb.grid_remove()
            progress_stop(pb)
            info_label.config(text=f'{e}')
            return
    output = make_output(path_file1, path_file2, subs)
    field.delete(1.0, END)
    field.insert(INSERT, output)
    progress_stop(pb)
    if created_files:
        label_result.config(
            text=f'Результаты сохранены в файлах:\n\
{created_files[0]}\n{created_files[1]}'
        )
        label_result.grid()
        open_folder_btn.grid()

def main():
    app_version = 'v.1.3 Release'
    main_window = tk.Tk()
    main_window.title(f"Поиск совпадающих подтекстов в двух текстах {app_version}")
    main_window.geometry('1150x460')
    main_window.minsize(width=500, height=460)
    main_window.columnconfigure(0, weight=0)
    main_window.columnconfigure(1, weight=2)
    main_window.rowconfigure(0, weight=1)
    main_window.rowconfigure(1, weight=0)
    frame1 = tk.LabelFrame(main_window, text='Входные данные', width=400)
    frame1.grid(column=0, row=0, padx=5, pady=5, sticky=NSEW)
    frame2 = tk.LabelFrame(main_window, text='Выходные данные')
    frame2.grid(column=1, row=0, padx=5, pady=5, sticky=NSEW)
    frame3 = tk.Frame(main_window)
    frame3.grid(column=0, row=1, padx=5, pady=5, sticky=NSEW, )
    frame4 = tk.Frame(main_window)
    frame4.grid(column=1, row=1, padx=5, pady=5, sticky=NSEW)

    label_f1 = Label(frame1, text='Выберите файл 1', anchor=W)
    label_f1.grid(column=0, row=0, padx=5, pady=5, sticky=EW)
    path_file1 = Entry(frame1)
    path_file1.grid(column=0, row=1, padx=5, pady=5, sticky=EW)
    choose_file1 = Button(frame1, text='Выбрать...', command=lambda: select_file(path_file1))
    choose_file1.grid(column=1, row=1, padx=5, pady=5)

    label_f2 = Label(frame1, text='Выберите файл 2', anchor=W)
    label_f2.grid(column=0, row=2, padx=5, pady=5, sticky=EW)
    path_file2 = Entry(frame1)
    path_file2.grid(column=0, row=3, padx=5, pady=5, sticky=EW)
    choose_file2 = Button(frame1, text='Выбрать...', command=lambda: select_file(path_file2))
    choose_file2.grid(column=1, row=3, padx=5, pady=5)

    label_f2 = Label(frame1, text='Минимальное количество слов совпадений', anchor=W)
    label_f2.grid(column=0, row=4, padx=5, pady=5, sticky=EW)
    min_words_field = Entry(frame1, width=5)
    min_words_field.insert(0, '10')
    min_words_field.grid(column=0, row=5, padx=5, pady=5, sticky=W)

    info_label = Label(frame1, text='', anchor=W, fg='red', wraplength=200)
    info_label.grid(column=0, row=6, padx=5, pady=5, sticky=EW)

    pb = ttk.Progressbar(
        frame4,
        orient='horizontal',
        mode='indeterminate',
        length=200,
    )
    pb.grid(column=0, row=0, padx=5, pady=5, sticky=W)
    pb.grid_remove()

    label_result = Label(frame4, text='', anchor=E, justify=LEFT)
    label_result.grid(column=0, row=0, padx=5, pady=5, sticky=NSEW)
    label_result.grid_remove()

    open_folder_btn = Button(frame4, text='Открыть папку...', command=lambda: open_folder(label_result))
    open_folder_btn.grid(column=0, row=1, padx=5, pady=5, sticky=W)
    open_folder_btn.grid_remove()

    SVBar = tk.Scrollbar(frame2)
    SVBar.pack(side=tk.RIGHT, fill="y")
    TBox = tk.Text(
        frame2,
        yscrollcommand=SVBar.set,
        wrap="word",
    )
    TBox.pack(expand=1, fill=tk.BOTH)
    SVBar.config(command=TBox.yview)
    start_btn = Button(
        frame3,
        text='Старт',
        command=lambda: threading.Thread(
            target=lambda: find_subtext(
                path_file1.get(),
                path_file2.get(),
                min_words_field.get(),
                TBox,
                info_label,
                pb,
                label_result,
                open_folder_btn,
            )
        ).start()
    )
    save_to_file_btn = Button(
        frame3,
        text='Сохранить',
        command=lambda: save_to_file(TBox)
    )
    clear_btn = Button(
        frame3,
        text='Очистить',
        command=lambda: clear_field(TBox, label_result, open_folder_btn)
    )
    quit_app_btn = Button(
        frame3,
        text='Выход',
        command=main_window.destroy
    )
    empty_label = Label(frame3, text='\n\n')
    empty_label.grid(column=0, row=1, padx=5, pady=5, sticky=NSEW)
    start_btn.grid(column=0, row=2, padx=5, pady=5)
    save_to_file_btn.grid(column=1, row=2, padx=5, pady=5)
    clear_btn.grid(column=2, row=2, padx=5, pady=5)
    quit_app_btn.grid(column=3, row=2, padx=5, pady=5)

    main_window.mainloop()

if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        logger.exception(msg=e, exc_info=True)


import sqlite3
from sqlite3 import Error
from columnar import columnar
from click import style
from termcolor import cprint

def create_connection(db_file):

    conn = None
    try:
        conn = sqlite3.connect(db_file)
    except Error as e:
        print(e)

    return conn

def create_tables(conn):

    sql_create_columns_table = """ CREATE TABLE IF NOT EXISTS columns_db (
                                        rsid TEXT PRIMARY KEY,
                                        gene TEXT,
                                        chr TEXT NOT NULL,
                                        position INTEGER NOT NULL,
                                        orientation TEXT NOT NULL,
                                        reference TEXT
                                    ); """

    sql_create_genotypes_table = """ CREATE TABLE IF NOT EXISTS genotypes_db (
                                            id integer PRIMARY KEY AUTOINCREMENT,
                                            rsid TEXT,
                                            genotype TEXT NOT NULL,
                                            magnitude REAL,
                                            color TEXT,
                                            summary TEXT
                                        ); """

    try:
        c = conn.cursor()
        c.execute(sql_create_columns_table)
        c.execute(sql_create_genotypes_table)
    except Error as e:
        print(e)

def insert_in_tables(conn, rsid, rsid_columns, rsid_genotypes):

    rsid = rsid.capitalize()

    columns_values = [rsid_columns[column] for column in rsid_columns]

    sql_insert_columns = ''' INSERT INTO columns_db(rsid,gene,chr,position,orientation,reference)
                                VALUES(?,?,?,?,?,?) '''

    with conn:
        cur = conn.cursor()
        cur.execute(sql_insert_columns, columns_values)

    # insert multiple genotypes
    for genotype in rsid_genotypes:
        genotype_values = [rsid_genotypes[genotype][feature] for feature in rsid_genotypes[genotype]]
        genotype_values.insert(0, genotype)
        genotype_values.insert(0, rsid)

        sql_insert_genotypes = ''' INSERT INTO genotypes_db(rsid,genotype,magnitude,color,summary)
                                        VALUES(?,?,?,?,?) '''

        with conn:
            cur.execute(sql_insert_genotypes, genotype_values)

    # insert multiple genotypes
    for genotype in rsid_genotypes:
        genotype_values = [rsid_genotypes[genotype][feature] for feature in rsid_genotypes[genotype]]
        genotype_values.insert(0, genotype)
        genotype_values.insert(0, rsid)

        sql_insert_genotypes = ''' INSERT INTO genotypes_db(rsid,genotype,magnitude,color,summary)
                                        VALUES(?,?,?,?,?) '''

        with conn:
            cur.execute(sql_insert_genotypes, genotype_values)

def check_isin_table(conn, rsid):
    rsid = rsid.capitalize()

    sql_isin_table = ''' SELECT count(*) FROM columns_db WHERE rsid == ?'''

    cur = conn.cursor()
    cur.execute(sql_isin_table, (rsid,))
    count = cur.fetchall() # returns [(0,)] or [(1,)]

    return False if count[0][0] == 0 else True

def get_rsid_from_table(conn, rsid):
    to_print = {"genotypes":dict()}

    sql_rsid_columns = """SELECT * FROM columns_db WHERE rsid == ? """
    cur = conn.cursor()
    cur.execute(sql_rsid_columns, (rsid,))
    columns = cur.fetchall()[0]

    features = ["rsid", "gene", "chr", "position", "orientation", "reference"]
    for feature, column in zip(features, columns):
        to_print[feature] = column

    sql_rsid_genotypes = """SELECT * FROM genotypes_db WHERE rsid == ? """
    cur = conn.cursor()
    cur.execute(sql_rsid_genotypes, (rsid,))
    genotypes = cur.fetchall()

    features = ["magnitude", "color", "summary"]
    for genotype in genotypes:
        genotype_alleles = genotype[2]
        to_print["genotypes"][genotype_alleles] = dict()
        for feature, value in zip(features, genotype[3:]):
            to_print["genotypes"][genotype_alleles][feature] = value

    return to_print

def print_rsid(to_print):

    keys_genotypes = list(to_print["genotypes"])

    patterns = [
        ("- Gene:", lambda text: style(text, bold=True)),
        ("- Chr:", lambda text: style(text, bold=True)),
        ("- Orient:", lambda text: style(text, bold=True)),
        ("- Pos:", lambda text: style(text, bold=True)),
        ("- Ref:", lambda text: style(text, bold=True))
    ]

    lines = list()

    # line_1
    print()
    cprint(f"    {to_print['rsid']}    ", attrs=["bold", "underline"])

    ## TABLE ##
    # line_2
    line_2 = ["- Gene:", to_print['gene'] ]
    if len(keys_genotypes) >= 1:
        line_2 += ["\t\t\t\t", keys_genotypes[0], "", str(to_print["genotypes"][keys_genotypes[0]]["magnitude"]), "",
                   to_print["genotypes"][keys_genotypes[0]]["summary"]]
        re_pattern = keys_genotypes[0].replace("(", "\(").replace(")", "\)")
        geno_pattern = (re_pattern,
                         lambda text: style(text, bold=True, fg=to_print["genotypes"][keys_genotypes[0]]["color"]))

        patterns.append(geno_pattern)
        lines.append(line_2)

    # line_3
    line_3 = ["- Chr:", to_print['chr']]
    if len(keys_genotypes) >= 2:
        line_3 += ["", keys_genotypes[1], "", str(to_print["genotypes"][keys_genotypes[0]]["magnitude"]), "",
                   to_print["genotypes"][keys_genotypes[1]]["summary"]]
        re_pattern = keys_genotypes[1].replace("(", "\(").replace(")", "\)")
        geno_pattern = (re_pattern,
                         lambda text: style(text, bold=True, fg=to_print["genotypes"][keys_genotypes[1]]["color"]))

        patterns.append(geno_pattern)
        lines.append(line_3)

    # line_4
    line_4 = ["- Orient:", to_print['orientation']]
    if len(keys_genotypes) >= 3:
        line_4 += ["", keys_genotypes[2], "", str(to_print["genotypes"][keys_genotypes[2]]["magnitude"]), "",
                   to_print["genotypes"][keys_genotypes[2]]["summary"]]
        re_pattern = keys_genotypes[2].replace("(", "\(").replace(")", "\)")
        geno_pattern = (re_pattern,
                        lambda text: style(text, bold=True,fg=to_print["genotypes"][keys_genotypes[2]]["color"]))

        patterns.append(geno_pattern)
        lines.append(line_4)

    # line_5
    line_5 = ["- Pos:", to_print['position']] + [""]*(len(line_2)-2)
    lines.append(line_5)

    # line_6
    line_6 = ["- Ref:", to_print['reference']] + [""]*(len(line_2)-2)
    lines.append(line_6)

    # justify
    if len(line_2) > 2:
        justify_text=["c", "l", "c", "c", "c", "c", "c", "l"]
    else:
        justify_text = ["c", "l"]

    table = columnar(lines,
                     patterns=patterns,
                     min_column_width=2,
                     headers=None,
                     justify=justify_text,
                     no_borders=True
                     )
    print(table)


import re
import sqlite3 as sq
import datetime
import requests
#from django.utils.dateparse import parse_datetime
URL = 'https://auto.ria.com/api/search/auto?indexName=auto%2Corder_auto%2Cnewauto_search&price_ot=1&currency=1' \
      '&abroad=2&custom=1&page=0&countpage=20&with_feedback_form=1&withOrderAutoInformer=1&with_last_id=1'
URL_CURRENT_CAR = 'https://auto.ria.com/uk/bu/blocks/json/2999/299858/29985840?langId=4&lang_id=4'
COUNT_PAGES = int(input('Enter count pages:...'))
re_ex = r'\d{8}'
data_cars = []

def check_correct_data(lst_dict):
	if lst_dict['autoData'].get('categoryId', 'Undefined value') == 1:
		lst_dict['autoData']['categoryId'] = 'Легкові'
	if lst_dict.get("addDate", 'Undefined value') == 'Undefined value':
		lst_dict["addDate"] = str(datetime.datetime.now())
	if lst_dict['autoData'].get('fuelName', 'Undefined value').strip().isalpha():
		lst_dict['autoData']['fuelName'] = [lst_dict['autoData']['fuelName'], 'Undefined value']
	else:
		lst_dict['autoData']['fuelName'] = lst_dict['autoData']['fuelName'].split(',')

def get_data_all_cars(ids, url):
	for id_ in ids:
		our_url = url.replace(re.search(re_ex, url).group(0), id_)
		json_data = requests.get(our_url).json()
		check_correct_data(json_data)
		print(f'Parse data car with id:{id_}')
		data_cars.append({
			"brand": json_data.get("markName", 'Undefined value').strip(),
			"model": json_data.get("modelName", 'Undefined value').strip(),
			"version": json_data['autoData'].get("version", 'Undefined value').strip(),
			"year": json_data['autoData'].get("year", 'Undefined value'),
			"prise_usd": str(json_data.get("USD", 'Undefined value')).replace(' ', ''),
			"prise_uah": str(json_data.get("UAH", 'Undefined value')).replace(' ', ''),
			"race": json_data['autoData'].get("race", 'Undefined value').strip(),
			"transmission": json_data['autoData'].get("gearboxName", 'Undefined value').strip(),
			"region": json_data['stateData'].get('name', 'Undefined value').strip(),
			"city": json_data.get("cityLocative", 'Undefined value').strip(),
			"engine_type": json_data['autoData'].get("fuelName", 'Undefined value')[0].strip(),
			"engine_capacity": json_data['autoData'].get("fuelName", 'Undefined value')[-1].strip(),
			"plate_number": json_data.get("plateNumber", 'Undefined value').strip(),
			"vin_code": json_data.get("VIN", 'Undefined value').strip(),
			"type_of_transport": json_data['autoData'].get("categoryId", 'Undefined value').strip(),
			"body_type": json_data.get("subCategoryName", 'Undefined value').strip(),
			"drive_type": json_data["autoData"].get("driveName", 'Undefined value').strip(),
			"description": json_data["autoData"].get("description", 'Undefined value'),
			"date_created": json_data.get("addDate", 'Undefined value'),
			"id": id_
		})
	for i in data_cars:
		for j in i:
			print(f'{j}:{i[j]}')
	print('Result:...')

def get_ids_and_pages(url, params=None):
	print('Send get request to url...')
	json_var = requests.get(url, params=params).json()
	ids = []
	if (json_var["result"]["search_result"]["count"]) % 20 == 0:
		num_of_pages = ((json_var["result"]["search_result"]["count"]) // 20) - 1
	else:
		num_of_pages = ((json_var["result"]["search_result"]["count"]) // 20)
	if num_of_pages > 0:
		if COUNT_PAGES < num_of_pages:
			for page in range(COUNT_PAGES):
				print(f'Parse ids on page {page + 1}/{COUNT_PAGES}...')
				json_var = requests.get(url).json()
				ids.extend(json_var["result"]["search_result"]["ids"])
				url = url.replace(f'page={page}', f'page={page + 1}')
		else:
			for page in range(num_of_pages + 1):
				print(f'Parse ids on page {page + 1}/{COUNT_PAGES}...')
				json_var = requests.get(url).json()
				ids.extend(json_var["result"]["search_result"]["ids"])
				url = url.replace(f'page={page}', f'page={page + 1}')
	else:
		print(f'Parse ids on page 1/{COUNT_PAGES}...')
		ids.extend(json_var["result"]["search_result"]["ids"])
	return ids

get_data_all_cars(get_ids_and_pages(URL), URL_CURRENT_CAR)

with sq.connect("cars.db") as con:
	cur = con.cursor()

	cur.execute("""
	CREATE TABLE IF NOT EXISTS cars (
	marka TEXT,
	model TEXT,
	year INTEGER,
	race TEXT,
	city TEXT,
	USD INTEGER,
	EUR INTEGER,
	UAH INTEGER,
	ID INTEGER PRIMARY KEY
	)""")
	param = """INSERT INTO cars VALUES (?,?,?,?,?,?,?,?,?)"""
	for car in data_cars:
		data_tuple = (car['marka'], car['model'], car['year'], car['race'], car['city'], car['USD'], car['EUR'],
		              car['UAH'], int(car['id']))
		try:

			cur.execute(param, data_tuple)
		except sq.IntegrityError:
			query = """UPDATE cars SET marka = ?, model = ?, year = ?, race = ?, city = ?, USD = ?, EUR = ?,
			UAH = ?, id = ? WHERE ID = ?"""
			data_update = (*data_tuple, car['id'])
			cur.execute(query, data_update)
			print(f'Car with id {car["id"]} was update in DB')
	print('The file was create and full')
	con.commit()


import tensorflow as tf
import tensorflow_probability as tfp
from tensorflow import keras
from tensorflow.keras import Input, Model

tfd = tfp.distributions
tfpl = tfp.layers
tfkl = tf.keras.layers

class MLPNet(keras.Model):
    """Create a Multilayer Perceptron (MLP) model with Normal distribution output."""

    def __init__(self, num_of_layers=3, nodes=1024, seed=2345, name="MLPNet", **kwargs):
        """
        Initialize the MLPNet model.

        Args:
            num_of_layers (int): Number of layers in the MLP.
            nodes (int): Number of nodes in each layer.
            seed: Random seed.
            name (str): Name of the model (default is 'MLPNet').
        """
        super(MLPNet, self).__init__(name=name, **kwargs)

        self.mlpnet_layers = []  # List to hold layers
        self.num_of_layers = num_of_layers  # Number of layers in the network
        self.nodes = nodes  # Number of nodes in each layer
        activation = tfkl.PReLU(alpha_initializer=tf.initializers.constant(0.3))

        # Create layers for the MLP network
        for li in range(num_of_layers):
            self.mlpnet_layers.append(tfkl.Dense(nodes*2, name='dense' + str(li + 1)))
            self.mlpnet_layers.append(tfkl.BatchNormalization(name='bn' + str(li + 1)))
            self.mlpnet_layers.append(tfkl.Activation(activation, name='act' + str(li + 1)))

        # Define the probability distribution layer (Normal distribution with mean and scale)
        self.prob = tfpl.DistributionLambda(lambda t: tfd.Normal(
            loc=t[..., :nodes], scale=1e-5 + tf.math.softplus(t[..., nodes:])), name='dist')

    def summary(self, pixels):

        """
        Generate a summary of the model architecture.

        Args:
            pixels (tuple): Shape of the input layer.

        Returns:
            Summary of the model architecture.
        """

        x = Input(shape=pixels)
        model = Model(inputs=x, outputs=self.call(x))
        return model.summary()

    def call(self, inputs):
        """
        Forward pass through the MLPNet model.

        Args:
            inputs: Input tensor.

        Returns:
            Probability distribution output.
        """
        x = inputs

        # Pass input through the defined layers
        for li, layer in enumerate(self.mlpnet_layers.layers):
            layer._name = 'MLPLayer' + str(li + 1)
            x = layer(x)

        x = self.prob(x)  # Apply probability distribution to the output x
        return x

#model = MLPNet(3, 1024,12345)
#model.summary(1024)


from flask import Flask
from Models.User import User
from config import session_key, app_config, jwt_secret
from Controllers import AuthRoutes, ErrorRoutes, ReviewRoutes, BookRoutes
from Services.Database.database_monoengine import db
from flask_jwt_extended import JWTManager
from flask_login import LoginManager
from flask_mongoengine import MongoEngine

'''
Basic start up of application
Database gets imported then configured and injected in the lifetime of the application
Jwt configuration for authentication and authorization
'''

app = Flask(__name__)

# app settings
app.secret_key = session_key
app.static_folder = app_config['ROOT_PATH'] + '/Views/static'
app.template_folder = app_config['ROOT_PATH'].split('Controllers')[0] + '/Views/templates'

# blueprints init
blueprints = [
    ErrorRoutes.mod,
    AuthRoutes.mod,
    ReviewRoutes.mod,
    BookRoutes.mod

]

for bp in blueprints:
    app.register_blueprint(bp)

# db stuff
app.config['MONGODB_SETTINGS'] = {
    'db': 'bookreview',
    'host': 'mongodb://localhost/mybookreview'
}
app.url_map.strict_slashes = False

login_manager = LoginManager()
db.init_app(app)
login_manager.init_app(app)
login_manager.login_view = 'auth_routes.login'
@login_manager.user_loader
def load_user(user_id):
    return User.objects(id=user_id).first()

# jwt stuff
app.config['JWT_SECRET_KEY'] = jwt_secret
app.config['JWT_TOKEN_LOCATION'] = ['cookies']
jwt = JWTManager(app)

if __name__ == '__main__':
    app.run(host="localhost", port=5010, debug=True)


'''
Created on 5 Apr 2017

@author: jdrumgoole
'''

import pymongo

if __name__ == '__main__':

    client = pymongo.MongoClient()
    database = client[ "b2b" ]
    zips_collection = database[ "zips" ]

    print(
'''
We can use query operators to produce more fine grained queries.
For instance to find zip codes with populations greater than 90,000 we use
the $gt operator.

    cursor = zips_collection.find( { "pop" : { "$gt" : 90000 }})
    count = 0
    for i in cursor:
        count = count + 1
        print( i )
    print( "%i records" % count )

''')

    cursor = zips_collection.find( { "pop" : { "$gt" : 90000 }})
    count = 0
    for i in cursor:
        count = count + 1
        print( i )
    print( "%i records" % count )

    print(
'''
We can do the inverse with $lt.
For instance to find zip codes with populations less than 1000 we use
the $lt operator.

    cursor = zips_collection.find( { "pop" : { "$lt" : 10 }})
    count = 0
    for i in cursor:
        count = count + 1
        print( i )
    print( "%i records" % count )

''')

    cursor = zips_collection.find( { "pop" : { "$lt" : 10 }})
    count = 0
    for i in cursor:
        count = count + 1
        print( i )
    print( "%i records" % count )

    print(
'''
We can use the "in" operator to see if a value is a member of a set (aka an array).
Lets see the populations of the zip codes in three states. Rhode Island, District of
Columbia and Delaware.

    cursor = zips_collection.find( { "state" : { "$in" : [ "DC", "RI", "DE"] }},
                                   { "pop" : 1, "state" : 1})
    count = 0
    for i in cursor:
        count = count + 1
        print( i )
    print( "%i records" % count )

''')

    cursor = zips_collection.find( { "state" : { "$in" : [ "DC", "RI", "DE"] }},
                                   { "pop" : 1, "state" : 1})
    count = 0
    for i in cursor:
        count = count + 1
        print( i )
    print( "%i records" % count )


import click
import dateparser
from typing import Dict

import constants
from flask import Flask
from menu import get_menu
from menu.recipes import get_recipe
from utils import cache

app = Flask(__name__)
cache.init_app(app)

@click.command()
@click.argument("when", default="today")
def menu(when: str) -> None:
    date = dateparser.parse(when)
    lunch_menu, dinner_menu = get_menu(date)
    for i, day in enumerate(constants.DAYS):
        print(f"* {day}")
        print(f"  Lunch: {lunch_menu[i]}")
        print(f"  Dinner: {dinner_menu[i]}")
        if i < 6:
            print()

@click.command()
@click.argument("when", default="today")
def groceries(when: str) -> None:
    date = dateparser.parse(when)
    lunch_menu, dinner_menu = get_menu(date)
    all_recipes = {
        name: get_recipe(name)
        for name in (list(lunch_menu) + list(dinner_menu))
    }
    shopping_list: Dict[str, int] = {}
    for recipe_name, recipe in all_recipes.items():
        if len(recipe.ingredients) == 0:
            print(
                f"WARNING: recipe {recipe_name} does not have any ingredients"
            )
        for ingredient in recipe.ingredients:
            name = ingredient.name.lower()
            if name not in shopping_list.keys():
                shopping_list[name] = 0
            shopping_list[name] += 1
    for ingredient in sorted(shopping_list.keys()):
        print(f"{ingredient}: {shopping_list[ingredient]}")


#!/usr/bin/python

import argparse

from HashInfo import *
from SignatureForger import *

def printOutput(signature):
  print("****************************************************")
  print("*         Signature successfully generated!        *")
  print("****************************************************")
  print("")
  print("Signature:")
  print(hex(signature))
  print("Plaintext:")
  print(hex(toInt(signature) ** 3))

def getArguments():
  parser = argparse.ArgumentParser(description="Signature forger for RSA PKCS1v1.5 given that the exponent 3 is used and the verification algorithm is not implemented properly")

  parser.add_argument("-k", "--keysize", type=int)
  parser.add_argument("-ha", "--hashalg", action="store", type=str, choices=list(HASH_ASN1.keys()))
  parser.add_argument("-va", "--variant", type=int, choices=[1,2])
  parser.add_argument("-of", "--outputformat", type=str, choices=["hex", "base64", "raw"])
  parser.add_argument("-m", "--message", type=str)

  args = parser.parse_args()
  return args

if __name__ == "__main__":

  args = getArguments()

  keySize = args.keysize
  hashAlg = args.hashalg

  signatureForger = SignatureForger(keySize, hashAlg)

  signature = ""

  if args.variant == 1:
    signature = signatureForger.forgeSignature_method1(args.message)
  elif args.variant == 2:
    signature = signatureForger.forgeSignature_method2(args.message)
  else:
    raise Exception("Unsupported signature method")

  printOutput(signature)


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import gym  # 包含CartPole的平台环境

import matplotlib.pyplot as plt # 绘图

# Hyperparameters
BATCH_SIZE = 32  # 样本数量
LR = 0.01  # 学习率
EPSILON = 0.9  # greedy policy
GAMMA = 0.9  # reward discount
TARGET_REPLACE_ITER = 100  # 目标网络更新频率
MEMORY_CAPACITY = 2000  # 记忆库容量

# 使用gym库中的环境：CartPole，且打开封装
# 在pycharm中可通过两次shift打开全局搜索，找到源码
env = gym.make('CartPole-v0').unwrapped

N_ACTIONS = env.action_space.n  # 杆子动作个数 (2个)
N_STATES = env.observation_space.shape[0]  # 杆子状态个数 (4个)

# 定义Net类 (定义网络)
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        self.fc1 = nn.Linear(N_STATES, 30)  # 设置第一个全连接层
        self.fc1.weight.data.normal_(0, 0.1)
        self.out = nn.Linear(30, N_ACTIONS)  # 设置第二个全连接层
        self.out.weight.data.normal_(0, 0.1)

    def forward(self, x):
        x = F.relu(self.fc1(x))  # 使用ReLU
        actions_value = self.out(x)
        return actions_value

class DQN(object):
    def __init__(self):  # 定义DQN的一系列属性
        self.eval_net, self.target_net = Net(), Net()  # 利用Net创建评估网络和目标网络
        self.learn_step_counter = 0  # for target updating
        self.memory_counter = 0  # for storing memory
        self.memory = np.zeros((MEMORY_CAPACITY, N_STATES * 2 + 2))  # 初始化记忆库，一行代表一个transition
        self.optimizer = torch.optim.Adam(self.eval_net.parameters(), lr=LR)  # Adam优化器
        self.loss_func = nn.MSELoss()  # loss(xi, yi)=(xi-yi)^2

    def choose_action(self, x):  # 根据x状态选择动作
        x = torch.unsqueeze(torch.FloatTensor(x), 0)  # 将x转换成32-bit floating point形式，并在dim=0增加维数为1的维度
        if np.random.uniform() < EPSILON:  # 生成随机数，如果小于EPSILON，选择最优动作
            actions_value = self.eval_net.forward(x)  # 通过对评估网络输入状态x，前向传播获得动作值
            action = torch.max(actions_value, 1)[1].data.numpy()  # 输出每一行最大值的索引，并转化为numpy ndarray形式
            np.random.shuffle(action) # 打乱顺序，避免总是选第0个
            action = action[0]  # 输出第0个
        else:  # 随机选择
            action = np.random.randint(0, N_ACTIONS)
        return action  # 返回选择的动作 (0或1)

    def store_transition(self, s, a, r, s_):  # 定义记忆存储函数 (这里输入为一个transition)
        transition = np.hstack((s, [a, r], s_))  # 在水平方向上拼接数组
        # 如果记忆库满了，便覆盖旧的数据
        index = self.memory_counter % MEMORY_CAPACITY  # 获取transition要置入的行数
        self.memory[index, :] = transition  # 置入transition
        self.memory_counter += 1  # memory_counter自加1

    def learn(self):  # 定义学习函数(记忆库已满后开始学习)
        # 目标网络参数更新
        if self.learn_step_counter % TARGET_REPLACE_ITER == 0:  # 每100步触发，实现Fixed Q-targets
            self.target_net.load_state_dict(self.eval_net.state_dict())  # 将评估网络的参数赋给目标网络
        self.learn_step_counter += 1  # 学习步数自加1

        # Experience Replay
        # 在[0, 2000)内随机抽取32个数，可能会重复
        sample_index = np.random.choice(MEMORY_CAPACITY, BATCH_SIZE)
        b_memory = self.memory[sample_index, :]
        b_s = torch.FloatTensor(b_memory[:, :N_STATES])
        b_a = torch.LongTensor(b_memory[:, N_STATES:N_STATES + 1].astype(int))
        b_r = torch.FloatTensor(b_memory[:, N_STATES + 1:N_STATES + 2])
        b_s_ = torch.FloatTensor(b_memory[:, -N_STATES:])

        # 获取32个transition的评估值和目标值，并利用损失函数和优化器进行评估网络参数更新
        q_eval = self.eval_net(b_s).gather(1, b_a)
        # q_next不进行反向传递误差，所以detach
        q_next = self.target_net(b_s_).detach()
        # q_next.max(1)[0]表示只返回每一行的最大值，不返回索引(长度为32的一维张量)；
        # .view()表示把前面所得到的一维张量变成(BATCH_SIZE, 1)的形状；最终通过公式得到目标值
        q_target = b_r + GAMMA * q_next.max(1)[0].view(BATCH_SIZE, 1)

        # 输入32个评估值和32个目标值，使用均方损失函数
        loss = self.loss_func(q_eval, q_target)
        self.optimizer.zero_grad()  # 清空上一步的残余更新参数值
        loss.backward()  # 误差反向传播, 计算参数更新值
        self.optimizer.step()  # 更新评估网络的所有参数

dqn = DQN()

y = np.array([]) # 记录score

for i in range(300):  # 300个episode循环
    print('Episode: %s' % i)
    s = env.reset()  # 重置环境，先reset才能render

    episode_reward_sum = 0  # 初始化该循环对应的episode的总奖励（加速训练）
    score = 0 # 初始化得分

    while True:  # 开始一个episode (每一个循环代表一步)
        env.render()  # 显示实验动画

        a = dqn.choose_action(s)  # 输入该步对应的状态s，选择动作
        s_, r, done, info = env.step(a)  # 执行动作，获得反馈

        # 修改奖励 根据x的偏离和theta的角度差打分
        x, x_dot, theta, theta_dot = s_
        r1 = (env.x_threshold - abs(x)) / env.x_threshold - 0.8
        r2 = (env.theta_threshold_radians - abs(theta)) / env.theta_threshold_radians - 0.5
        new_r = r1 + r2

        score += 1

        dqn.store_transition(s, a, new_r, s_)  # 存储

        episode_reward_sum += new_r

        s = s_  # 更新状态

        if dqn.memory_counter > MEMORY_CAPACITY:  # 如果累计的transition数量超过了记忆库的固定容量2000
            # 开始学习 (抽取32个transition，并对评估网络参数进行更新
            # 并在开始学习后每隔100次将评估网络的参数赋给目标网络)
            dqn.learn()

        if done:  # 如果done为True
            print('episode%s -- reward_sum: %s' % (i, score))
            break  # 该episode结束

    y = np.append(y, episode_reward_sum)

x = np.arange(0, 300) # x坐标

plt.title("Result")
plt.plot(x, y, color="red") # 绘图
plt.show()


from karim.secrets import secrets
import telegram.bot
from telegram.ext import messagequeue as mq

class MQBot(telegram.bot.Bot):
    def __init__(self, *args, is_queued_def=True, mqueue=None, **kwargs):
        super(MQBot, self).__init__(*args, **kwargs)
        # below 2 attributes should be provided for decorator usage
        self._is_messages_queued_default = is_queued_def
        self._msg_queue = mqueue or mq.MessageQueue()

    def __del__(self):
        try:
            self._msg_queue.stop()
        except:
            pass

    @mq.queuedmessage
    def send_queued_message(self, *args, **kwargs):
        '''Wrapped method would accept new `queued` and `isgroup`
        OPTIONAL arguments'''
        return super(MQBot, self).send_message(*args, **kwargs)

    def report_error(self, error=None, send_screenshot=False, screenshot_name=''):
        string = str(secrets.get_var('DEVS')).replace('[', '')
        string = string.replace(']', '')
        string = string.replace(' ', '')
        devs = list(string.split(','))
        for dev in devs:
            if send_screenshot:
                self.send_photo(chat_id=int(dev), photo=open('{}.png'.format(screenshot_name), 'rb'), caption='There was an error with the Karim Luman bot: \n{}'.format(error))
            else:
                self.send_message(chat_id=int(dev), text='There was an error with the Karim Luman bot: \n{}'.format(error))


# -- coding: utf-8 --
import torch.nn.init as init
import math

def weights_init(init_type='gaussian'):
    def init_fun(model):
        classname = model.__class__.__name__
        if (classname.find('Conv') == 0 or classname.find('Linear') == 0) and hasattr(m, 'weight'):
            if init_type == 'gaussian':
                init.normal_(model.weight.data, 0.0, 0.02)
            elif init_type == 'xavier':
                init.xavier_normal_(model.weight.data, gain=math.sqrt(2))
            elif init_type == 'kaiming':
                init.kaiming_normal_(model.weight.data, a=0, mode='fan_in')
            elif init_type == 'orthogonal':
                init.orthogonal_(model.weight.data, gain=math.sqrt(2))
            elif init_type == 'default':
                pass
            else:
                assert 0, "Unsupported initialization: {}".format(init_type)

            if hasattr(model, 'bias') and model.bias is not None:
                init.constant_(model.bias.data, 0.0)

    return init_fun