import geopandas as gpd
import os

dir = os.path.dirname(os.path.abspath(__file__))
colors = [
    "#f77779",
    "#b6d86c",
    "#fccd7b",
    "#fb9677",
    "#fd453f",
    "#646e78",
    "#8d98a7",
    "#dcccbb",
    "#eab464",
    "#a7754d",
    "#fa8334",
    "#fffd77",
    "#ffe887",
    "#388697",
    "#776533",
    "#b47eb3",
    "#fdf5bf",
    "#ffd5ff",
    "#97d6c3",
    "#8bb8a8",
    "#4599f7",
    "#f4f4e7",
    "#eb9baf",
    "#f9e8ab",
    "#fbc5ae",
    "#668bc3",
    "#df5d68",
    "#f5cbcb",
    "#5fb6c4",
    "#c7ffe9",
    "#a54657",
    "#587635",
    "#f7ee7f",
    "#f6a66a",
    "#f76657",
    "#fbc7b5",
    "#ffa8a9",
    "#f786aa",
    "#a64a76",
    "#cdb7ab",
]
countries = gpd.read_file(os.path.join(dir, "datasets","ne_50m_admin_0_countries.shp"))
ocean = gpd.read_file(os.path.join(dir, "datasets","ne_50m_ocean.shp"))
gridlines = gpd.read_file(os.path.join(dir, "datasets","ne_50m_geographic_lines.shp"))
rivers = gpd.read_file(os.path.join(dir, "datasets","ne_50m_rivers_lake_centerlines.shp"))
lakes = gpd.read_file(os.path.join(dir, "datasets","ne_50m_lakes.shp"))
us_states = gpd.read_file(os.path.join(dir, "datasets","us_states.geojson"))


from collections import deque

direction = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break

    mymap = []
    for i in range(h):
        mymap.append(list(map(int, input().split())))

    cnt = 0
    for i in range(h):
        for j in range(w):
            if mymap[i][j] == 1:
                queue = deque()
                queue.append((i, j))
                mymap[i][j] = 0

                while len(queue) != 0:
                    q = queue.popleft()
                    for d in direction:
                        newpos = (q[0] + d[0], q[1] + d[1])
                        if not (0 <= newpos[0] < h) or not (0 <= newpos[1] < w):
                            continue
                        if mymap[newpos[0]][newpos[1]] == 1:
                            queue.append(newpos)
                            mymap[newpos[0]][newpos[1]] = 0
                cnt += 1
    print(cnt)


from django.db import migrations, models
from ..helpers.date_time_without_tz_field import DateTimeWithoutTZField

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0004_journeys'),
    ]

    operations = [
        migrations.CreateModel(
            name='JourneyDriver',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),

                ('datetime_start', DateTimeWithoutTZField()),
                ('states', models.PositiveSmallIntegerField()),

                (
                    'journey',
                    models.ForeignKey(
                        on_delete=models.deletion.DO_NOTHING,
                        to='app.Journey',
                    ),
                ),
                (
                    'driver',
                    models.ForeignKey(
                        on_delete=models.deletion.DO_NOTHING,
                        to='app.Driver',
                    ),
                ),

                ('created_at', DateTimeWithoutTZField(auto_now_add=True)),
                ('updated_at', DateTimeWithoutTZField(null=True)),
            ],
        ),
    ]


import pickle
import sys, os
from os import listdir
from os.path import isfile, join
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix, f1_score
from collections import Counter
import pandas as pd

mypath = sys.argv[1]
onlyfiles = [os.path.join(mypath, f )for f in listdir(mypath) if isfile(join(mypath, f))]
max_acc = 0
mean_acc = 0
def transpose(labels):
    labels = np.array(labels)
    labels[np.where(labels==0)] = 2
    labels[np.where(labels==1)] = 0
    labels[np.where(labels==2)] = 1

    return list(labels)

def calculatePrecisionRecallAccuracy(labels, outputs):
    tp = 0
    fp = 0
    tn = 0
    fn = 0
    for label, output in zip(labels, outputs):
        if label==output and label ==0:
            tn = tn+1
        elif label==output and label==1:
            tp = tp+1
        elif label!=output and label == 0:
            fp = fp+1
        else:
            fn = fn +1
    precision = tp*1.0/(tp+fp)
    recall = tp*1.0/(tp+fn)
    accuracy = (tp+tn)*1.0/(tp+fp+fn+tn)
#    print("tp:", tp, "fp:", fp, "fn:", fn)
    return  precision, recall, accuracy

onlyfiles.sort(key=lambda x: (int(os.path.basename(x).split('_')[0]), x) )

for f in onlyfiles:
    basename = os.path.basename(f)
    splits = basename.split('_')
    epoch = splits[0]
    scale = splits[2]
    resize = splits[3]
    accuracy, labels, outputs = pickle.load(open(f,'rb'))
#    accuracy = f1_score(labels, outputs, average='macro')
#    print("accuracy:", accuracy, f)

#    c = Counter(labels)
#    out = []
#    lbl = []
#    for idx, label in enumerate(labels):
#        if c[label] != 1:
#            out.append(outputs[idx])
#            lbl.append(label)

#    outputs = out
#    labels = lbl

    accuracy = f1_score(labels, outputs, average='macro')
    print("accuracy:", accuracy, f)

    y_true = pd.Series(labels, name="Actual")
    y_pred = pd.Series(outputs, name="Predicted")
    df_confusion = pd.crosstab(y_true, y_pred, margins=True)
                #confusion_matrix = ConfusionMatrix(y_true, y_pred)
    df_confusion.to_csv("confusion.csv")

    #outputs = transpose(outputs)
#    precision, recall, accuracy = calculatePrecisionRecallAccuracy(labels, outputs)
#    print(classification_report(labels, outputs))
#    matrix = confusion_matrix(outputs, labels)
#    print(matrix)
    if max_acc < accuracy:
      max_acc = accuracy
    mean_acc += accuracy
#    print("precision:%.3f"%precision, "recall:%.3f"%recall,"accuracy:%.3f"%accuracy, "epoch:", epoch, "scale;", scale, "resize:", resize)

print("maximum accuracy:", max_acc)
print("Mean_accuracy:", mean_acc/len(onlyfiles))


'''
#此部分为利用注意力机制拟合训练的数据，权值是根据x值来确定的，测试数据和训练数据之间的相似度来确定权值，最后用该权值得到拟合的y值
import torch
from torch import nn
import matplotlib.pyplot as plt

n_train = 50  # 训练样本数
x_train, _ = torch.sort(torch.rand(n_train) * 5)   # 排序后的训练样本,返回顺序后的tensor以及对应的索引切片
#print(x_train,_)
#print(torch.normal(0.0, 0.5,(2,2)))
def f(x):
    return 2 * torch.sin(x) + x**0.8
y_train = f(x_train) + torch.normal(0.0, 0.5, (n_train,))  # 训练样本的输出
print(y_train.size())
x_test = torch.arange(0, 5, 0.1)  # 测试样本  生成数字0开始5结束步长0.1
y_truth = f(x_test)  # 测试样本的真实输出
n_test = len(x_test)  # 测试样本数
plt.plot(x_test,y_truth,'g')
plt.plot(x_train,y_train,'o')
X_repeat = x_test.repeat_interleave(n_train).reshape((-1, n_train))
#print(X_repeat.size())
attention_weights = nn.functional.softmax(-(X_repeat - x_train)**2 / 2, dim=1)
y_hat = torch.matmul(attention_weights, y_train)
plt.plot(x_test,y_hat,'y')
plt.show()
'''
import matplotlib.pyplot as plt

'''
#随机噪声拟合曲线（效果非常差）
import torch
from torch import nn
import matplotlib.pyplot as plt

x_test = torch.arange(0, 5, 0.1)
n_train = 50  # 训练样本数
def f(x):
    return 2 * torch.sin(x) + x**0.8
y_test = f(x_test)  # 训练样本的输出
n_test = len(x_test)  # 测试样本数
x_train, _ = torch.sort(torch.rand(n_train) * 5)
y_train=torch.normal(0.0, 1, (n_train,))
X_repeat = x_test.repeat_interleave(n_train).reshape((-1, n_train))
#print(X_repeat.size())
attention_weights = nn.functional.softmax(-(X_repeat - x_test)**2 / 2, dim=1)
y_hat = torch.matmul(attention_weights, y_train)
plt.plot(x_train,y_hat,'y')
plt.plot(x_train,y_train,'o')
plt.plot(x_test,y_test,'r')
#plt.show()
'''
'''
#矩阵相乘
import torch
weights = torch.ones((2, 10)) * 0.1
values = torch.arange(20.0).reshape((10,2))
q=torch.bmm(weights.unsqueeze(0), values.unsqueeze(0))
print(q.size())'''

import matplotlib.pyplot as plt
import torch
from torch import nn
class NWKernelRegression(nn.Module):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.w = nn.Parameter(torch.rand((1,), requires_grad=True)) #除了卷积全连接什么的有训练参数，要加入额外的参数
        #就用这个函数，同模型一起训练跟新

    def forward(self, queries, keys, values):
        # queries和attention_weights的形状为(查询个数，“键－值”对个数)
        queries = queries.repeat_interleave(keys.shape[1]).reshape((-1, keys.shape[1]))  #确保查询个数等于键值对数，列式个数和键值对
        self.attention_weights = nn.functional.softmax(
            -((queries - keys) * self.w)**2 / 2, dim=1)
        # values的形状为(查询个数，“键－值”对个数)
        return torch.bmm(self.attention_weights.unsqueeze(1),
                         values.unsqueeze(-1)).reshape(-1)

x_test = torch.arange(0, 5, 0.1)
n_test = len(x_test)
n_train=50
x_train, _ = torch.sort(torch.rand(n_train) * 5)
def f(x):
    return 2 * torch.sin(x) + x**0.8
y_test = f(x_test)
y_train = f(x_train) + torch.normal(0.0, 0.5, (n_train,))

#train
#这里是设置键值对,是用来训练网络的训练参数
# X_tile的形状:(n_train，n_train)，每一行都包含着相同的训练输入
X_tile = x_train.repeat((n_train, 1)) #维度不同第0维添加1维，将行复制n_train次列复制一次就是不变
# Y_tile的形状:(n_train，n_train)，每一行都包含着相同的训练输出
Y_tile = y_train.repeat((n_train, 1))
# keys的形状:('n_train'，'n_train'-1)
#keys = X_tile[(1 - torch.eye(n_train)).type(torch.bool)].reshape((n_train, -1))
keys = X_tile.reshape((n_train, -1))
print(x_train)
print(X_tile)
print(keys)
# values的形状:('n_train'，'n_train'-1)
#alues = Y_tile[(1 - torch.eye(n_train)).type(torch.bool)].reshape((n_train, -1))
values = Y_tile.reshape((n_train, -1))
#定义网络优化器损失函数
net = NWKernelRegression()
loss = nn.MSELoss(reduction='none')
trainer = torch.optim.SGD(net.parameters(), lr=0.5)
#animator = d2l.Animator(xlabel='epoch', ylabel='loss', xlim=[1, 5])
#开始训练
for epoch in range(5):
    trainer.zero_grad()
    l = loss(net(x_train, keys, values), y_train)
    l.sum().backward()
    trainer.step()
    print(f'epoch {epoch + 1}, loss {float(l.sum()):.6f}')
    #animator.add(epoch + 1, float(l.sum()))

#test
#这里是测试参数并且查看测试结果
# keys的形状:(n_test，n_train)，每一行包含着相同的训练输入（例如，相同的键）
keys = x_train.repeat((n_test, 1))
# value的形状:(n_test，n_train)
values = y_train.repeat((n_test, 1))
y_hat = net(x_test, keys, values).unsqueeze(1).detach()
plt.plot(x_test,y_test,'r')
plt.plot(x_train,y_train,'o')
plt.plot(x_train,y_hat,'g')
plt.show()

#print(keys)
#print(keys.shape[1].reshape((-1, keys.shape[1])))
'''keys = X_tile[(1 - torch.eye(n_train)).type(torch.bool)]
print(X_tile)
print(keys)
a=torch.tensor([1,2,3])

c=a[True,True,True]
print("C",c)'''

#print((1 - torch.eye(n_train)).type(torch.bool).reshape((n_train, -1).shape()))
'''print(torch.rand((1,)))
queries=torch.randn(5)
keys=torch.tensor([[1,2]
                   [2,3]])
queries = queries.repeat_interleave(keys.shape[1]).reshape((-1, keys.shape[1]))
print(queries)'''
#看net就知道这里是要将输入的维度转变成和键值对一样的维数也就是说先必须和键一样，因为要拿查询q和所有的键来做相似度计算
#前面得到了每一个的权值然后要和v相乘计算所以维度要和v的维度能做矩阵相乘


from flask import Flask, request, render_template
import pymongo
import pandas as pd

from atlas_mongo.Mongo import ConectColl
import atlas_mongo.folium_maps as fmaps
import atlas_mongo.external_api as exa
import machine.prepare_data as mppd
import machine.sandro_rey as sandro

app = Flask(__name__)

@app.route('/hello')
def hello():
    return 'Hi Ovi!'

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/home')
def home_two():
    return render_template('home.html')

@app.route('/create', methods=['POST'])
def create():
    fecha = request.form.get('fecha')
    year = int(request.form.get('year'))
    horario = request.form.get('horario')
    festividad = request.form.get('festividad')
    tipo_accidente = request.form.get('tipo_accidente')
    lesividad = request.form.get('lesividad')
    meteo = request.form.get('meteo')
    distrito = request.form.get('distrito')
    direccion = request.form.get('direccion')
    lon = round(float(request.form.get('lon')), 6)
    lat = round(float(request.form.get('lat')), 6)
    coll.create(fecha, year, horario, festividad, tipo_accidente,
                lesividad, meteo, distrito, direccion, lon, lat)
    coll.acc.create_index([('localizacion', pymongo.GEOSPHERE)])
    return 'Doc Inserted'

# ----
@app.route('/create/pred', methods=['POST'])
def create_pred():
    lon = round(float(request.form.get('lon')), 6)
    lat = round(float(request.form.get('lat')), 6)
    weight = round(float(request.form.get('weights')), 1)
    coll.create_pred(lon, lat, weight)
    coll.pred.create_index([('localizacion', pymongo.GEOSPHERE)])
    return 'Pred Inserted'
# -----

@app.route('/historical/direction', methods=['GET'])
def insert_dir():
    return render_template('historical.html')

@app.route('/historical/direction', methods=['POST'])
def get_coord_dir():
    pto_a = request.form.get('pto_a')
    pto_b = request.form.get('pto_b')
    lat_a, lon_a = exa.get_coord_dir(pto_a)
    lat_b, lon_b = exa.get_coord_dir(pto_b)
    folium_map = fmaps.print_heat_map_dir(coll, lat_a, lon_a, lat_b, lon_b)
    return folium_map._repr_html_()

# Predicciones y buenas noches

@app.route('/prediction/direction', methods=['GET'])
def insert_dir_pred():
    weather, day = exa.get_weather_day()
    fest = mppd.get_fest(day)
    return render_template('prediction.html', vble_w=weather, vble_d=day, vble_f=fest)

@app.route('/prediction/direction', methods=['POST'])
def get_coord_dir_pred():
    pto_a = request.form.get('pto_a')
    pto_b = request.form.get('pto_b')
    horario_cat = request.form.get('horario')

    lat_a, lon_a = exa.get_coord_dir(pto_a)
    lat_b, lon_b = exa.get_coord_dir(pto_b)
    weather, day = exa.get_weather_day()

    data = mppd.prepare_to_predict(
        horario_cat, day, lon_a, lat_a, lon_b, lat_b, weather)
    # data.to_csv('output/X_to_pred.csv', index=False)
    data_to_map = sandro.pred_y_buenas_noches(data)

    folium_map = fmaps.print_heat_map_pred(
        data_to_map, lat_a, lon_a, lat_b, lon_b)
    return folium_map._repr_html_()

if __name__ == '__main__':
    coll = ConectColl()
    app.run(debug=True)


#!/usr/bin/env python3

import sys
import multiprocessing
from multiprocessing.managers import BaseManager
from Etos import *
import Pause
import ECarModel
import socket

registerModule(ECarModel)
registerModule(Pause)

def taskF(job_q, result_q):
    while(True):
        sim = Simulation()
        request=job_q.get()
        sim.disableLog()
        sim.setParameters(**request)
        sim.start("XML/e-car-inwest2.xml#transaction[@id='starter']")
        ip = sys.argv[2]
        result_q.put( (request, (sim.charged_f.sum / sim.charged_h.sum, sim.batteryOut[1.0]), ip))
        print("request", request)

def mp_simulate(shared_job_q, shared_result_q, nprocs):
    """ Split the work with jobs in shared_job_q and results in
        shared_result_q into several processes. Launch each process with
        factorizer_worker as the worker function, and wait until all are
        finished.
    """
    procs = []
    for i in range(nprocs):
        p = multiprocessing.Process(
                target=taskF,
                args=(shared_job_q, shared_result_q))
        procs.append(p)
        p.start()

    for p in procs:
        p.join()

def make_client_manager(ip, port, authkey):
    """ Create a manager for a client. This manager connects to a server on the
        given address and exposes the get_job_q and get_result_q methods for
        accessing the shared queues from the server.
        Return a manager object.
    """
    class ServerQueueManager(BaseManager):
        pass

    ServerQueueManager.register('get_job_q')
    ServerQueueManager.register('get_result_q')

    manager = ServerQueueManager(address=(ip, port), authkey=authkey)
    manager.connect()

    print ('Client connected to %s:%s' % (ip, port))
    return manager

def runclient():
    manager = make_client_manager(sys.argv[1], 6842, b"heslo")
    job_q = manager.get_job_q()
    result_q = manager.get_result_q()
    mp_simulate(job_q, result_q, 4)

if __name__ == '__main__':
    runclient()


from setuptools import setup

params = {}
params['scripts'] = ['bin/logwork']

setup(name='logwork',
      version='0.05',
      description='simple work logging with active window monitoring',
      url='',
      author='Diogo Silva',
      author_email='',
      license='',
      packages=['logwork'],
      zip_safe=False,
      **params)


from PIL import Image
import os

def convert_to_jpeg(file_path):
    # check if the file exists
    if not os.path.isfile(file_path):
        raise ValueError(f"File {file_path} does not exist.")

    # get the file extension
    file_extension = os.path.splitext(file_path)[1].lower()

    # check if the file extension is supported
    if file_extension not in ['.png', '.jpg', '.jpeg', '.bmp', '.gif']:
        raise ValueError(f"File extension {file_extension} is not supported.")

    # open the image and convert it to JPEG
    with Image.open(file_path) as img:
        img = img.convert('RGB')
        new_file_path = os.path.splitext(file_path)[0] + '.jpeg'
        img.save(new_file_path, 'JPEG', quality=90)

    # return the path of the new JPEG file
    return new_file_path
#TO-pdf
import subprocess
import os

def convert_to_pdf(file_path):
    # check if the file exists
    if not os.path.isfile(file_path):
        raise ValueError(f"File {file_path} does not exist.")

    # get the file extension
    file_extension = os.path.splitext(file_path)[1].lower()

    # check if the file extension is supported
    if file_extension not in ['.doc', '.docx', '.ppt', '.pptx', '.odt', '.ods', '.odp', '.xls', '.xlsx']:
        raise ValueError(f"File extension {file_extension} is not supported.")

    # convert the file to PDF using unoconv
    subprocess.run(['unoconv', '-f', 'pdf', file_path])

    # get the path of the new PDF file
    pdf_file_path = os.path.splitext(file_path)[0] + '.pdf'

    # check if the PDF file was created
    if not os.path.isfile(pdf_file_path):
        raise ValueError("PDF conversion failed.")

    # return the path of the new PDF file
    return pdf_file_path

from fpdf import FPDF
import os

def convert_to_pdf(file_path):
    # Get the name and extension of the file
    file_name, file_extension = os.path.splitext(file_path)

    # Check if the file is already a PDF
    if file_extension.lower() == '.pdf':
        return file_path

    # Create a new PDF object
    pdf = FPDF()

    # Add a page to the PDF
    pdf.add_page()

    # Set the font and font size
    pdf.set_font('Arial', size=12)

    # Read the file contents
    with open(file_path, 'rb') as f:
        file_contents = f.read()

    # Add the file contents to the PDF
    pdf.cell(200, 10, txt=file_contents)


import enum

from ..ui.ui_base import *

from ..utill import image_manager

# origin To end
class ProgressbarDirection(enum.Enum):
    RightToLeft = 0
    DownToTop = 1
    LeftToRight = 2
    TopToDown = 3

class UIProgressBar(BaseUIObject):
    direction_dic = {
        ProgressbarDirection.RightToLeft: 1.0, ProgressbarDirection.DownToTop: 1.0,
        ProgressbarDirection.LeftToRight: -1.0, ProgressbarDirection.TopToDown: -1.0
    }

    def __init__(self, x, y, tag, w=None, h=None, direction: ProgressbarDirection = ProgressbarDirection.LeftToRight):
        super().__init__(x, y)
        self.ratio = 1.0
        self.image_tag = tag
        self.image_controller = None
        self.direction = direction

    def load(self):
        self.image_controller = image_manager.get_image_controller(self.image_tag)

    def update_value(self, value: float, value_max: float = 1.0):
        self.ratio = value / value_max

    def draw(self):
        if self.is_visible:
            src_w = self.image_controller.image.w
            src_h = self.image_controller.image.h
            center_calibration_direction = UIProgressBar.direction_dic[self.direction]
            if self.direction in (ProgressbarDirection.LeftToRight, ProgressbarDirection.RightToLeft):
                cal_w = src_w * self.ratio
                cal_h = src_h
                x = self.position[0] + center_calibration_direction * ((1 - self.ratio) * src_w / 2)
                y = self.position[1]
            else:
                cal_w = src_w
                cal_h = src_h * self.ratio
                x = self.position[0]
                y = self.position[1] + center_calibration_direction * ((1 - self.ratio) * src_h / 2)
            self.image_controller.clip_draw_no_animation(0, 0, int(cal_w), int(cal_h), int(x), int(y))


from typing import Any, Dict
from django.db import models
from django.shortcuts import render, get_object_or_404
from django.views import generic
from django.contrib.auth.models import User
from django.utils import timezone
from django.http import HttpResponse, HttpResponseRedirect
import datetime
from django.core.paginator import Paginator

from .models import Blog, Comment, Reply, BlogForm
from .forms import CommentForm, ReplyForm, SearchForm

# Create your views here.

class IndexView(generic.ListView):
    template_name = "blogs/index.html"
    context_object_name = "latest_blog_list"
    paginate_by = 3
    model = Blog

    def get_queryset(self):
        return Blog.objects.filter(pub_date__lte=timezone.now()).order_by("-pub_date")

    def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
        context = super(IndexView, self).get_context_data(**kwargs)
        context['latest'] = Blog.objects.filter(pub_date__lte=timezone.now()).order_by("-pub_date")[:5]
        return context

class DetailView(generic.DetailView):
    model = Blog
    template_name = "blogs/details.html"

    def get_context_data(self, *args, **kwargs):
        context = super(DetailView, self).get_context_data(*args, **kwargs)
        context['blogs'] = Blog.objects.filter(pub_date__lte=timezone.now()).order_by("-pub_date")[:5]
        context['comments'] = Comment.objects.filter(blog_id=self.kwargs['pk'])#blog__pk=9)#context['blogs'].values('id'))
        context['replys'] = []

        all_replies = Reply.objects.all()
        for comment in context['comments']:
            for reply in all_replies:
                if reply.comment.comment_text == comment.comment_text:
                    context['replys'].append(reply)
        context['form_r'] = ReplyForm()
        context['form'] = CommentForm()
        context['search_form'] = SearchForm()
        return context

def createView(request):
    context = {}
    context['form'] = BlogForm()
    return render(request, "blogs/create.html", context)

def like(request, blog_id):
    blog = get_object_or_404(Blog, pk=blog_id)
    blog.likes += 1

def createBlog(request):
    if request.method == 'POST':
        #request.POST['back_img'] = 'images/597_1.jpg'
        form = BlogForm(request.POST, request.FILES)
        if form.is_valid():
            blog = form.save(commit=False)
            user = request.user
            blog.author = user
            blog.save()
        else:
            print(form.errors.as_data())
    return HttpResponseRedirect("/blogs/")

def post(request, blog_id):
    blog = get_object_or_404(Blog, pk=blog_id)
    new_comment = Comment()
    new_comment.comment_text = request.POST['comment_text']
    new_comment.pub_date = datetime.datetime.now()
    new_comment.author = request.user
    new_comment.blog_id = blog_id
    new_comment.save()
    blog.comment_set.add(new_comment)
    return HttpResponseRedirect(f"/blogs/{blog_id}/")

def reply(request, comment_id):
    comment = get_object_or_404(Comment, pk=comment_id)
    new_reply = Reply()
    new_reply.content = request.POST['content']
    new_reply.pub_date = datetime.datetime.now()
    new_reply.author = request.user
    comment.reply_set.add(new_reply)
    new_reply.save()
    comment.reply_set.add(new_reply)
    return HttpResponseRedirect(f"/blogs/")

def search(request):
    form = SearchForm(request.GET)
    results = []
    latest = Blog.objects.filter(pub_date__lte=timezone.now()).order_by("-pub_date")[:5]
    popular = Blog.objects.filter(pub_date__lte=timezone.now()).order_by("likes")[:5]
    trending = Blog.objects.filter(pub_date__lte=timezone.now()).order_by("-pub_date")[:5]

    if form.is_valid():
        query = form.cleaned_data['query']
        results = Blog.objects.filter(title__contains=query)

    return render(request, 'search/search.html', {'form':form, 'results': results, 'latest':latest, 'popular':popular, 'trending':trending})


# coding=utf-8

import json
import os
import sys
from datetime import datetime
import json, time
import requests
from firebase import firebase

from threading import Thread, current_thread
from flask import Flask, request, __version__, render_template, jsonify

application = Flask(__name__, static_folder='web/static', template_folder='web/templates')
PORT = int(os.getenv('PORT', '8087'))

def get_tracks():

    while True:

        db = firebase.FirebaseApplication('https://ledjam-a27cd.firebaseio.com', None)

        print('starting bot.')
        data = request_data()

        for entry in data['tracks']:

            try:
                s_url = entry['spotify']['url']
            except:
                s_url = ''

            try:
                d_url = entry['deezer']['url']
            except:
                d_url = ''

            post_data(db, entry['artist'], entry['title'], s_url, d_url)

        print('tracks saved.')

        time.sleep(60*20)

def request_data():
    url = 'http://www.djamradio.com/actions/infos.php'

    r  = requests.post(url)

    data = r.text

    data = json.loads(data)

    return data

def post_data(firebase, artist, track, s_url, d_url):
    tmp = {}

    tmp['artist'] = artist
    tmp['track'] = track
    tmp['spotify'] = s_url
    tmp['deezer'] = d_url

    result = firebase.post('/tracks', tmp)

p = Thread(target=get_tracks)
p.start()

if __name__ == '__main__':
    application.run(host='0.0.0.0', port=int(PORT), threaded=True, debug=False)


import os
import numpy as np
import logging
from langchain.embeddings import OpenAIEmbeddings
from sklearn.metrics.pairwise import cosine_similarity
from dotenv import load_dotenv

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s [%(levelname)s] - %(message)s')
logger = logging.getLogger(__name__)
load_dotenv()

class EmbeddingComparator:
    def __init__(self):
        """Initializes the comparator with OpenAIEmbeddings."""

        self.embedder = OpenAIEmbeddings()

    def generate_embedding(self, text):
        """
        Generates an embedding for a given text.

        Args:
        - text (str): The input text.

        Returns:
        - ndarray: The generated embedding.
        """

        return self.embedder.embed_query(text)

    def parse_ui_string(self, ui_string):
        """
        Splits the UI string by comma and strips whitespace.

        Args:
        - ui_string (str): The input UI string.

        Returns:
        - list: List of UI elements.
        """
        return [element.strip() for element in ui_string.split(',')]

    def average_embedding(self, elements):
        """
        Computes the average embedding for a list of elements.

        Args:
        - elements (list): The input list of elements.

        Returns:
        - ndarray or None: The average embedding, or None if the list is empty.
        """

        embeddings = [self.generate_embedding(e) for e in elements if e]
        return np.mean(embeddings, axis=0) if embeddings else None

    def validate_test_cases_semantically(self, test_cases, clusters, SOME_THRESHOLD=0.8):
        """
        Validates test cases based on semantic similarity with clusters.

        Args:
        - test_cases (list): List of test case descriptions.
        - clusters (dict): Dictionary with cluster IDs as keys and their
            elements as values.
        - SOME_THRESHOLD (float, optional): Threshold for similarity.
            Defaults to 0.8.

        Returns:
        - dict: Results with test case descriptions as keys and their status
            and best matched cluster (if passed) as values.
        """

        logger.info('Validating test cases semantically.')
        results = {}

        test_case_embeddings = {
            desc: self.generate_embedding(desc) for desc in test_cases}
        cluster_embeddings = {cluster_id: self.average_embedding(self.parse_ui_string(elements))
                              for cluster_id, elements in clusters.items()}

        for desc, test_case_emb in test_case_embeddings.items():
            similarities = {}
            for cluster_id, cluster_emb in cluster_embeddings.items():
                similarity = cosine_similarity([test_case_emb], [cluster_emb])[0][0] \
                    if test_case_emb is not None and cluster_emb is not None else 0
                similarities[cluster_id] = similarity

            best_match_cluster = max(similarities, key=similarities.get)

            if similarities[best_match_cluster] > SOME_THRESHOLD:
                results[desc] = {'status': 'passed',
                                 'best_matched_cluster': best_match_cluster}
            else:
                results[desc] = {'status': 'failed'}

        return results


#
# @lc app=leetcode id=337 lang=python3
#
# [337] House Robber III
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
from collections import deque

class Solution:
    def rob(self, root: TreeNode) -> int:
        memo = dict({None: [0, 0]})
        nodes = deque()
        node = root

        while nodes or node:
            while node:
                if node.right:
                    nodes.append(node.right)
                nodes.append(node)
                node = node.left

            node = nodes.pop()

            if nodes and node.right == nodes[-1]:
                nodes.pop()
                nodes.append(node)
                node = node.right
            else:
                left = memo[node.left]
                right = memo[node.right]

                memo[node] = [max(left) + max(right), node.val + left[0] + right[0]]
                node = None

        return max(memo[root])

# @lc code=end


from typing import Optional
from uuid import UUID

from dependency_injector.wiring import Provide, inject
from fastapi import APIRouter, Depends, HTTPException

from dependencies import Container
from dto.user_dto import CreateUserDto
from models.user_model import User, UserList
from services.user_service import UserService

router = APIRouter()

@router.post("/users/{username}/providers/{provider}/psychologists")
@inject
async def save_psychologist(provider: str,
                            username: str,
                            user: CreateUserDto,
                            user_service: UserService = Depends(Provide[Container.user_service])) -> User:
    return await user_service.save_psychologist(provider, username, user)

@router.post("/users/{username}/providers/{provider}/clients")
@inject
async def save_client(provider: str,
                      username: str,
                      user: CreateUserDto,
                      user_service: UserService = Depends(Provide[Container.user_service])) -> User:
    return await user_service.save_client(provider, username, user)

@router.get("/users/{username}/providers/{provider}")
@inject
async def get(provider: str,
              username: str,
              user_service: UserService = Depends(Provide[Container.user_service])) -> User:
    user = await user_service.get(provider, username)

    if not user:
        raise HTTPException(status_code=404, detail="User not found")

    return user

@router.get("/psychologists/{user_id}/clients")
@inject
async def find_clients(user_id: UUID,
                       query: Optional[str] = None,
                       user_service: UserService = Depends(Provide[Container.user_service])) -> UserList:
    return await user_service.find_clients(user_id, query)

@router.get("/clients/{client_id}/psychologists")
@inject
async def find_clients(client_id: UUID,
                       query: Optional[str] = None,
                       user_service: UserService = Depends(Provide[Container.user_service])) -> UserList:
    return await user_service.find_psychologists(client_id, query)


from django.http import HttpResponse
from django.shortcuts import render
def homepage(request):
    return render( request, "home.html")

def count (request):
    fulltext = request.GET['fulltext']
    wordlist = fulltext.split()
    worddict = {}
    for word in wordlist :
        if word in worddict :
            # increase
            worddict[word] += 1
        else:
            # add to the worddict
            worddict [word]  = 1

    return render(request, 'count.html' , {'fulltext' : fulltext  , 'count':len(wordlist),'worddict' : worddict.items() })

def create_about():
    f = open('.\\templates\\about.html' , 'w')
    data = """
<!DOCTYPE html>

    <meta charset="utf-8">
    <title>  Hello i'm elhoussine</title>

Pythin programming with django and html and the good programmer elhoussine

    <form action="{% url 'count' %}">
  <textarea cols='40' rows='10' name='fulltext'></textarea >

<input type="submit" value="Count The Words !"  />

 About as page from this link :

<form action="{% url 'about'%}">
 about as page
    </form>

    """
    f.write(data)
create_about()

def about(request):
    return render( request , ('about.html'))


#!/usr/bin/env python3
"""Run a batch of simulations reproducibly.

Usage:
  run_batch.py <command>
  run_batch.py [options] [--] <command>
  run_batch.py [--git [--input_branch=<branch>]... [--results_branch=<branch>]]
               [--archive [--full]] [--class=<class>]
               [--sweep=<sweep>]...
               [--post=<command>]
               [--clean]
               [options] [--] <command>
  run_batch.py --help

Options:
  -h --help                 Show this screen.
  -g --git                  Use Git to checkout input files and record results.
  -a --archive              Save the results of each run in an archive folder.
  -f --full                 Save full data files to archive folder.
  -d --clean                Clean up by deleting results files after completion.
  --class=<class>           Specify the simulation class (see below)
  --input_branch=<branch>   Specify an input branch in Git.
                            Can be specified multiple times to run for multiple
                            input branches.
  --results_branch=<branch> Specify a results branch in Git.
                            Does not allow multiple branches.
  --post=<command>          Specify a post-processing command.
  --sweep=<key:v1,v2,v3...> Specify a parametric sweep with multiple values for
                            parameters leading to multiple runs.
                            Multiple parameters can be specified independently,
                            such as --sweep='a:1,2' --sweep='b:2,4'
                            or connected, like --sweep='(a,b):(1,2),(2,4)'.
                            With separate specification, all combinations are
                            simulated (e.g. (1,2), (1,4), (2,2) and (2,4)).
                            With connected specification, only the given
                            combinations are simulated (e.g. (1,2) and (2,4)).
                            See below for more examples.

Options passed to Reproducible:
  --config <configfile>     Overwrite the location of Reproducible config file.
  --logfile <logfile>       Overwrite the name of the Reproducible log file.
  -l --runlog               Save a run log to file for each Reproducible run.
  -d --devel                Run Reproducible in developer mode.

Options passed to Reproducible run:
  --template <template>     the name of the file(s) that should be treated
                            as a template inside the command;
                            for multiple templates separate with commas
  --src <sourcedir>         check the source code at <sourcedir>
  --build                   build the code from source using `make install`
  --hash <oldhash>          pre-load parameters from this run
  --show                    print out rendered template
  --save                    save the rendered template to file
  --list-parameters         list all parameters that need to be set
  -p <key:value>            for several parameters use k1:v1,k2:v2 syntax

Simulation classes:
  impact                    Simulations with Impact-T or Impact-Z.
                            Input files: *.in *.data *.txt *.xlsx
                            Output files: fort.* *.dst *.plt
  bdsim                     Simulations with BDSIM.
                            Input files: *.gmad *.data *.txt *.xlsx
                            Output files: *.root *.png *.eps
  opal                      Simulations with OPAL.
                            Input files: *.in *.data *.txt *.xlsx
                            Output files: *.h5 *.lbal *.stat *.dat data

Examples:

run_batch.py "echo Hello world"

    Run a single command reproducibly.
    This will call `reproduce run -- "echo Hello world"` without other options.

run_batch.py -l "echo Hello world"

    Pass a command to the reproduce script.
    This will call `reproduce --runlog run -- "echo Hello world"`.

run_batch.py --class=impact -- ImpactTexe

    Run a simulation reproducibly in Impact-T.
    This will call `reproduce run -- ImpactTexe` without any other options.

run_batch.py --git --input_branch=develop --results_branch=results \\
             --class=impact -- ImpactTexe

    First checkout input files from the 'develop' branch and 'simulations.log'
    from the 'results' branch, then run the simulation in Impact, then check
    in 'simulations.log' as a new commit on the 'results' branch.
    The simulation will be run as `reproduce run -- ImpactTexe`.

run_batch.py --git --input_branch=input/full --input_branch=input/nospacecharge
             --results_branch=results --class=impact -- ImpactTexe

    Run the same simulation reproducibly using two different input branches.
    This will result in two separate commits in the results branch.

run_batch.py --template=ImpactT.in --sweep=I:0.0,0.2,0.4,0.6 \\
             --class=impact -- ImpactTexe

    Sweep through the parameter options for beam current I.
    The input file 'ImpactT.in' should be set up as a template with {{I}} in
    place of the beam current, and a separate simulation will be run for each
    parameter value:
        reproduce run --template ImpactT -p I:0.0 -- ImpactTexe
        reproduce run --template ImpactT -p I:0.2 -- ImpactTexe
        reproduce run --template ImpactT -p I:0.4 -- ImpactTexe
        reproduce run --template ImpactT -p I:0.6 -- ImpactTexe
    If `--git` is specified, each run result will be in a separate commit.
    If `--archive` is specified, each run will be archived to a separate folder.
    If `--runlog` is specified, each run will produce a separate log.

run_batch.py --template=ImpactT.in --sweep=I:0.0,0.2,0.4,0.6 --sweep=E:1.0,1.5 \\
             --class=impact -- ImpactTexe

    Double parametric sweep for beam current I and energy E.
    The input file 'ImpactT.in' should be set up as a template with {{I}} in
    place of the beam current, and {{E}} in place of the beam energy.
    All combinations of parameter values for the two sweeps will run in separate
    simulations:
        reproduce run --template ImpactT -p I:0.0,E:1.0 -- ImpactTexe
        reproduce run --template ImpactT -p I:0.2,E:1.0 -- ImpactTexe
        reproduce run --template ImpactT -p I:0.4,E:1.0 -- ImpactTexe
        reproduce run --template ImpactT -p I:0.6,E:1.0 -- ImpactTexe
        reproduce run --template ImpactT -p I:0.0,E:1.5 -- ImpactTexe
        reproduce run --template ImpactT -p I:0.2,E:1.5 -- ImpactTexe
        reproduce run --template ImpactT -p I:0.4,E:1.5 -- ImpactTexe
        reproduce run --template ImpactT -p I:0.6,E:1.5 -- ImpactTexe

run_batch.py --template=ImpactT.in --sweep=(I,E):(0.0,1.0),(0.2,1.0),(0.4,1.5) \\
             --class=impact -- ImpactTexe

    Single parametric sweep for multiple parameters I and E.
    The input file 'ImpactT.in' is set up as a template with {{I}} and {{E}}
    as above, but only the given combinations of parameter values will run:
        reproduce run --template ImpactT -p I:0.0,E:1.0 -- ImpactTexe
        reproduce run --template ImpactT -p I:0.2,E:1.0 -- ImpactTexe
        reproduce run --template ImpactT -p I:0.4,E:1.5 -- ImpactTexe

"""

import sys
import os
import pathlib
import git
import subprocess
import shutil
from datetime import datetime
from docopt import docopt
import itertools
import unicodedata
import re

# User settings
REPRODUCIBLE = '~/Code/Reproducible'
PYENV        = '~/.pyenv'
LOGFILE      = 'simulations.log'
ARCHIVE_LOG  = 'simulation.log'
ARCHIVE_ROOT = '~/Simulations/'

# Utility methods
def get_folder(folder_path = '.'):
    """Get an absolute path object from a given path"""
    return pathlib.Path(folder_path).expanduser().absolute()

def get_date_as_folder_name():
    """Get a date string in YYYY-MM-DD format for making an archive folder"""
    return datetime.today().strftime('%Y-%m-%d')

def get_safe_folder_name(value):
    """Get a name for a folder without special characters."""
    value = unicodedata.normalize('NFKD', value)
    value = value.encode('ascii', 'replace').decode('ascii')
    value = re.sub(r'[^\.\w\s-]', '-', re.sub(r'\?', '_', value)).strip()
    value = re.sub(r'([^0-9])\.([^0-9])', r'\1-\2', value)
    value = re.sub(r'-$', '', re.sub(r'[-\s]+', '-', value))
    return value

def get_archive_folder(archive_root):
    """Get an absolute path object for a new archive folder with today's date"""
    if not archive_root.is_dir():
        raise OSError(f'Archive location not found: {archive_root}')
    else:
        return archive_root.joinpath(get_date_as_folder_name())

def get_python_for_reproducible(reproducible_folder, pyenv_folder):
    """Get the path of the correct version of Python to run Reproducible"""
    if not reproducible_folder.is_dir():
        raise OSError('Reproducible script folder not found: '
                      + str(reproducible_folder))
    if (reproducible_folder.joinpath('.python-version').is_file()
        and pyenv_folder.is_dir()):
        with open(reproducible_folder.joinpath('.python-version'), 'r') as f:
            python_version = f.readline().strip()
        return str(pyenv_folder.joinpath('versions')
                   .joinpath(python_version)
                   .joinpath('bin/python3'))
    else:
        return 'python3'

# Communication methods
def announce(message):
    """Announce a given message."""
    print(str(message))

def announce_error(message):
    """Announce a given error message."""
    print(str(message), file=sys.stderr)

def announce_start(this_run):
    """Announce that a particular run is about to start"""
    print('-----------------------------------------------------------------')
    announce(f'Start of run: {this_run["title"]}')
    print()

def announce_end(this_run):
    """Announce that a particular run has finished"""
    print()
    announce(f'End of run: {this_run["title"]}')
    print('-----------------------------------------------------------------')

# Git methods
def get_git_repo(repo_path):
    """Return a repo object for the Git repo at a given path"""
    if not pathlib.Path(repo_path).is_dir():
        raise OSError(f'Repo folder not found: {repo_path}')
    else:
        return git.Repo(repo_path)

def git_checkout(repo, branch_name):
    """Check out the given branch"""
    if not isinstance(repo, git.Repo):
        raise TypeError(f'Not a valid repo: {repo}')
    repo.heads[branch_name].checkout()

def git_switch(repo, branch_name):
    """Switch branches without checking anything out"""
    if not isinstance(repo, git.Repo):
        raise TypeError(f'Not a valid repo: {repo}')
    repo.head.reference = repo.heads[branch_name]
    repo.head.reset(index=True, working_tree=False)

def git_get_file(repo, branch_name, file_name):
    """Checkout a single file from a given branch"""
    if not isinstance(repo, git.Repo):
        raise TypeError(f'Not a valid repo: {repo}')
    repo.git.checkout(branch_name, '--', file_name)

def git_commit(repo, commit_files, commit_message):
    """Add and commit the given files"""
    if not isinstance(repo, git.Repo):
        raise TypeError(f'Not a valid repo: {repo}')
    repo.index.add(commit_files)
    repo.index.commit(commit_message)

def get_input_branch(repo, given_input_branch):
    """Get a list of all input branches, unless given an override"""
    if given_input_branch:
        if not (   isinstance(given_input_branch, str)
                or isinstance(given_input_branch, list)):
            raise ValueError('Invalid input for input branch: '
                             + str(given_input_branch))
        else:
            input_branch = given_input_branch
    else:
        input_branch = [head.name for head in repo.heads
                        if 'input' in head.name]
        if len(input_branch) == 1:
            input_branch = input_branch[0]
    if isinstance(input_branch, str):
        if input_branch not in [head.name for head in repo.heads]:
            raise ValueError(f'Input branch not found: {input_branch}')
    elif isinstance(input_branch, list):
        for branch in input_branch:
            if branch not in [head.name for head in repo.heads]:
                raise ValueError(f'Input branch not found: {branch}')
    else:
        raise ValueError(f'Invalid value for input branch: {input_branch}')
    return input_branch

def get_results_branch(repo, given_results_branch):
    """Get the default result branch, unless given an override"""
    if given_results_branch:
        if not isinstance(given_results_branch, str):
            raise ValueError('Invalid input for results branch: '
                             + str(given_results_branch))
        else:
            results_branch = given_results_branch
    else:
        results_branch = [head.name for head in repo.heads
                          if 'results' in head.name]
        if len(results_branch) > 1:
            raise ValueError('Ambiguous results branches in Git')
        else:
            results_branch = results_branch[0]
    if results_branch not in [head.name for head in repo.heads]:
        raise ValueError(f'Results branch not found: {results_branch}')
    else:
        return results_branch

def get_commit_files(settings, parameters):
    """Get a list of files to commit to the results branch"""
    patterns = list()
    patterns.append(settings['logfile'])
    return patterns

def get_commit_message(this_run):
    """Create a message string to commit results of the current run"""
    return 'Results for ' + this_run['title']

# Template methods
def get_valid_templates(run_folder, templates):
    """Check which files exist and return lists of valid and invalid files."""
    valid_templates = []
    invalid_templates = []
    for template in templates.split(','):
        if run_folder.joinpath(template).is_file():
            valid_templates.append(template)
        else:
            invalid_templates.append(template)
    return (None if len(valid_templates) == 0 else ','.join(valid_templates),
            None if len(invalid_templates) == 0 else ','.join(invalid_templates))

# Post-processing methods
def post_process(settings, command):
    """Run the given post-processing command in the run folder"""
    folder = settings['current_folder']
    environ = os.environ.copy()
    this_env = str(pathlib.Path.home().joinpath('.pyenv/versions/scripts/bin'))
    environ['PATH'] = environ['PATH'].replace(f'{this_env}:', '')
    environ['PYENV_DIR'] = ''
    environ['PYENV_VERSION'] = ''
    return subprocess.run(command.split(), cwd=folder, env=environ)

# Archive methods
def create_archive_folder(archive_folder):
    """Make a new folder at the given location, or access an existing folder"""
    if not archive_folder.is_dir():
        archive_folder.mkdir(parents=True)

def get_copy_list(simulation_class):
    """Get list of file patterns to copy for a particular simulation type"""
    copy_list = ['*.data', '*.txt', '*.xlsx']
    if simulation_class == 'impact':
        copy_list.append('*.in')
    elif simulation_class == 'bdsim':
        copy_list.append('*.gmad')
    elif simulation_class == 'opal':
        copy_list.append('*.in')
    else:
        copy_list.append('*.in')
    return copy_list

def get_move_list(simulation_class, is_full_archive):
    """Get list of file patterns to move for a particular simulation type"""
    move_list = ['*.png', '*.eps', '*.ps', '*.jpg', '*.log']
    if simulation_class == 'impact':
        move_list.extend(['*.dst', '*.plt', '*.T7out'])
        if is_full_archive:
            move_list.append('fort.*')
    elif simulation_class == 'bdsim':
        if is_full_archive:
            move_list.append('*.root')
    elif simulation_class == 'opal':
        if is_full_archive:
            move_list.extend(['*.h5', '*.lbal', '*.stat', '*.dat', 'data'])
    return move_list

def get_delete_list(simulation_class):
    """Get list of file patterns to delete - same as full archive list."""
    return get_move_list(simulation_class, is_full_archive=True)

def copy_to_archive(run_folder, archive_folder, copy_patterns):
    """Copy files to the given archive folder based on glob patterns"""
    if not run_folder.is_dir():
        raise OSError(f'Cannot access source folder: {run_folder}')
    if not archive_folder.is_dir():
        raise OSError(f'Cannot access archive folder: {archive_folder}')
    if not isinstance(copy_patterns, list):
        raise ValueError(f'Invalid archive copy pattern: {copy_patterns}')
    file_list = set()
    for pattern in copy_patterns:
        file_list.update(set(run_folder.glob(pattern)))
    for this_file in file_list:
        if this_file.name != LOGFILE:
            shutil.copy2(str(this_file), str(archive_folder))

def move_to_archive(run_folder, archive_folder, move_patterns):
    """Move files to the given archive folder based on glob patterns"""
    if not run_folder.is_dir():
        raise OSError(f'Cannot access source folder: {run_folder}')
    if not archive_folder.is_dir():
        raise OSError(f'Cannot access archive folder: {archive_folder}')
    if not isinstance(move_patterns, list):
        raise ValueError(f'Invalid archive move pattern: {move_patterns}')
    file_list = set()
    for pattern in move_patterns:
        file_list.update(set(run_folder.glob(pattern)))
    for this_file in file_list:
        if this_file.name != LOGFILE:
            shutil.move(str(this_file), str(archive_folder))

def move_rendered_templates(run_folder, archive_folder):
    """Save rendered template files to archive folder"""
    if not run_folder.is_dir():
        raise OSError(f'Cannot access source folder: {run_folder}')
    if not archive_folder.is_dir():
        raise OSError(f'Cannot access archive folder: {archive_folder}')
    file_list = run_folder.glob('*.rendered')
    for this_file in file_list:
        new_filename = str(this_file.name).replace('.rendered','')
        shutil.move(str(this_file), str(archive_folder.joinpath(new_filename)))

def archive_log(settings, archive_folder):
    """Get a log of the last run and save it to the archive folder"""
    log_command = [str(settings['python']),
                   str(settings['reproduce']), 'log', '-n1']
    log_output = subprocess.run(log_command,
                                cwd=settings['current_folder'],
                                capture_output=True).stdout
    with open(archive_folder.joinpath(settings['archive_log']), 'wb') as f:
        f.write(log_output)

def archive_output(settings, this_run):
    """Archive the input, output and log files of the latest run"""
    create_archive_folder(this_run['archive'])
    copy_to_archive(settings['current_folder'],
                    this_run['archive'],
                    this_run['archive_copy'])
    move_to_archive(settings['current_folder'],
                    this_run['archive'],
                    this_run['archive_move'])
    move_rendered_templates(settings['current_folder'], this_run['archive'])
    archive_log(settings, this_run['archive'])

def delete_output(settings, this_run):
    """Delete any output files that haven't been archived."""
    delete_list = get_delete_list(this_run['--class'])
    for pattern in delete_list:
        files = settings['current_folder'].glob(pattern)
        for this_file in files:
            if this_file.name != LOGFILE:
                this_file.unlink()

# Sweep methods
def get_sweep_parameters(sweep_definition):
    """Return the parameter name for a given sweep string"""
    if not ':' in sweep_definition:
        raise ValueError('Invalid sweep definition: ' + sweep_definition)
    sweep_parameters = sweep_definition.split(':')[0]
    if ',' in sweep_parameters:
        return sweep_parameters.replace('(', '').replace(')', '').split(',')
    else:
        return [sweep_parameters]

def get_sweep_values(sweep_definition):
    """Return the parameter name for a given sweep string"""
    if not ':' in sweep_definition:
        raise ValueError('Invalid sweep definition: ' + sweep_definition)
    sweep_values = sweep_definition.split(':')[1]
    if '(' in sweep_values:
        return [tuple(this_set.replace('(', '').replace(')', '').split(','))
                for this_set in sweep_values.split('),')]
    else:
        return sweep_values.split(',')

def get_sweep_strings(sweep_definition):
    """Get all single-value parameter strings for a given sweep string"""
    if not ':' in sweep_definition:
        raise ValueError('Invalid sweep definition: ' + sweep_definition)
    sweep_parameters = get_sweep_parameters(sweep_definition)
    if len(sweep_parameters) > 1:
        return [','.join([sweep_parameters[i] + ':' + values[i]
                        for i in range(len(sweep_parameters))])
                for values in get_sweep_values(sweep_definition)]
    else:
        return [sweep_parameters[0] + ':' + value
                for value in get_sweep_values(sweep_definition)]

def get_sweep_combinations(sweeps):
    """Get all combinations of parameter values for multiple sweeps"""
    sweep_strings = [get_sweep_strings(sweep) for sweep in sweeps]
    combinations = itertools.product(*sweep_strings)
    combination_strings = [','.join(item) for item in combinations]
    return combination_strings

# Define run settings and parameters
def get_settings(arguments):
    """Get the required settings as a dictionary"""
    settings = dict()
    settings['current_folder'] = get_folder('.')
    settings['archive_root'] = get_folder(ARCHIVE_ROOT)
    settings['reproducible'] = get_folder(REPRODUCIBLE)
    settings['pyenv'] = get_folder(PYENV)
    settings['python'] = get_python_for_reproducible(settings['reproducible'],
                                                     settings['pyenv'])
    settings['reproduce'] = get_folder(REPRODUCIBLE).joinpath('reproduce')
    settings['logfile'] = LOGFILE
    settings['archive_log'] = ARCHIVE_LOG
    return settings

def get_parameters(settings, arguments):
    """Get the parameters for the batch run based on the given arguments"""
    parameters = arguments.copy()
    if parameters['--archive']:
        parameters['archive'] = (
            get_archive_folder(settings['archive_root']))
        parameters['archive_copy'] = get_copy_list(parameters['--class'])
        parameters['archive_move'] = (
            get_move_list(parameters['--class'], parameters['--full']))
    else:
        parameters['archive'] = None
    if parameters['--git']:
        repo = get_git_repo(settings['current_folder'])
        parameters['--input_branch'] = (
            get_input_branch(repo, arguments['--input_branch']))
        parameters['--results_branch'] = (
            get_results_branch(repo, arguments['--results_branch']))
    return parameters

def get_title(this_run):
    """Create a title string including the run date and main command"""
    if 'title' in this_run:
        if this_run['title']:
            return this_run['title']
    title = ' '.join([datetime.today().strftime('%Y-%m-%d'),
                      this_run['<command>'].split()[0]])
    return title

# Run methods
def reproducible_run(settings, this_run):
    """Run the given command using Reproducible"""
    command = [str(settings['python']), str(settings['reproduce'])]
    if this_run['--config']:
        command.append('--config')
        command.append(this_run['--config'])
    if this_run['--logfile']:
        command.append('--logfile=')
        command.append(this_run['--logfile'])
    if this_run['--runlog']:
        command.append('--runlog')
    if this_run['--devel']:
        command.append('--devel')
    command.append('run')
    if this_run['--template']:
        command.append('--template')
        command.append(this_run['--template'])
    if this_run['--src']:
        command.append('--src')
        command.append(this_run['--src'])
    if this_run['--build']:
        command.append('--build')
    if this_run['--hash']:
        command.append('--hash')
        command.append(this_run['--hash'])
    if this_run['--show']:
        command.append('--show')
    if this_run['--save']:
        command.append('--save')
    if this_run['--list-parameters']:
        command.append('--list-parameters')
    if this_run['-p']:
        command.append('-p')
        command.append(this_run['-p'])
    command.append('--')
    command.append(this_run['<command>'])
    return subprocess.run(command, cwd=settings['current_folder'])

def run_single(settings, this_run):
    """Work through the simulation steps for each individual run"""
    announce_start(this_run)
    if this_run['--git']:
        repo = get_git_repo(settings['current_folder'])
        git_checkout(repo, this_run['--input_branch'])
        git_get_file(repo, this_run['--results_branch'], settings['logfile'])
    if this_run['--template']:
        valid, invalid = get_valid_templates(settings['current_folder'],
                                             this_run['--template'])
        if invalid:
            announce_error(f'Skipping missing templates: {invalid}')
        this_run['--template'] = valid
    reproducible_run(settings, this_run)
    if this_run['--post']:
        post_process(settings, this_run['--post'])
    if this_run['--git']:
        git_switch(repo, this_run['--results_branch'])
        git_commit(repo, this_run['commit_files'], this_run['commit_message'])
        git_switch(repo, this_run['--input_branch'])
    if this_run['--archive']:
        archive_output(settings, this_run)
    if this_run['--clean']:
        delete_output(settings, this_run)
    announce_end(this_run)

def run_with_git(settings, this_run):
    """Run for a single or multiple input branches"""
    this_run['commit_files'] = get_commit_files(settings, this_run)
    this_run['commit_message'] = get_commit_message(this_run)
    if isinstance(this_run['--input_branch'], list):
        for this_branch in this_run['--input_branch']:
            branch_run = this_run.copy()
            branch_run['--input_branch'] = this_branch
            branch_run['title'] = (
                this_run['title'] + ' for branch ' + this_branch)
            branch_run['commit_message'] = (
                this_run['commit_message'] + ' for branch ' + this_branch)
            if branch_run['--archive']:
                branch_run['archive'] = this_run['archive'].joinpath(
                    this_branch.replace('input/', ''))
            run_single(settings, branch_run)
    else:
        run_single(settings, this_run)

# Main batch method
def run_batch(settings, parameters):
    """Run through the batch for different parameter values and input files"""
    batch_run = parameters.copy()
    batch_run['title'] = get_title(batch_run)
    if batch_run['--sweep']:
        for this_combination in get_sweep_combinations(batch_run['--sweep']):
            this_run = batch_run.copy()
            this_run['title'] = batch_run['title'] + ' for ' + this_combination
            if this_run['-p']:
                this_run['-p'] += ',' + this_combination
            else:
                this_run['-p'] = this_combination
            if this_run['--archive']:
                folder_name = get_safe_folder_name(this_combination)
                this_run['archive'] = batch_run['archive'].joinpath(folder_name)
            if this_run['--git']:
                run_with_git(settings, this_run)
            else:
                run_single(settings, this_run)
    else:
        if batch_run['--git']:
            run_with_git(settings, batch_run)
        else:
            run_single(settings, batch_run)

# What to do when run as a script
if __name__ == '__main__':
    arguments = docopt(__doc__)
    settings = get_settings(arguments)
    parameters = get_parameters(settings, arguments)
    run_batch(settings, parameters)


import inspect
from types import FunctionType
from typing import TypeVar, Type, Optional

from fastapi import FastAPI, Depends, Header, Cookie, Request, Body
from pydantic import BaseModel, Field, create_model
from core.helper import db_helper

from sample.models import sample

DB_HELPER = db_helper.DBHelper
app = FastAPI()

# class Foo:
#     def __init__(self, a: str, b: str):
#         self.a = a
#         self.b = b
#
#     def __call__(self):
#         return self.a + self.b
#
#
# bar = TypeVar("bar", bound=Foo)
#
#
# def delegate(typ: Type[bar], *args, **kwargs):
#     inst = typ(*args, **kwargs)
#     return inst()
#
#
# print(delegate(Foo, 5, 6))

_m = create_model("Foo", a=(str, Field(None)), b=((Optional[str] | None), Field(None)))

@app.get("/my-endpoint", openapi_extra={
    "parameters":
        [
            {"name": "X-My-Header", "in": "header", "schema": {"type": "string", "default": "d"}, "required": True},
            {"name": "X-My-Cookie", "in": "cookie", "schema": {"type": "string", "default": "e"}, "required": True},
        ]})
async def my_endpoint(
        request: Request,
        t: _m = Body(),
        coobase: str = Cookie("drive"),
):
    cookie = request.cookies.get("X-My-Cookie")
    header = request.headers.get("X-My-Header")
    return {"cookie": coobase, "header": header}

from fastapi import APIRouter
from core import chapter, scenario, actor, actor_role, scene

API_SCENARIO = scenario.APIScenario
API_ACTOR = actor.APIActor
JSON_ROLE = actor_role.JsonFieldRole
MODEL_ROLE = actor_role.ModelFieldRole
HEADER_ROLE = actor_role.HeaderFieldRole
Cast = scene.Cast
SummaryScene = scene.SummaryScene
DetailScene = scene.DetailScene
CreateScene = scene.CreateScene
UpdateScene = scene.UpdateScene
DeleteScene = scene.DeleteScene

foo = API_SCENARIO(
    actors={
        "id": API_ACTOR("id", int, JSON_ROLE, MODEL_ROLE, JSON_ROLE),
        "name": API_ACTOR("name", str, JSON_ROLE, MODEL_ROLE, JSON_ROLE),
        "age": API_ACTOR("age", int, JSON_ROLE, MODEL_ROLE, JSON_ROLE),
        "address": API_ACTOR("address", str, JSON_ROLE, MODEL_ROLE, JSON_ROLE),
        "deleted": API_ACTOR("is_deleted", bool, JSON_ROLE, MODEL_ROLE, JSON_ROLE),
    },
    scenes={
        "summary": SummaryScene(Cast({"id", "name"})),
        "create": CreateScene(Cast({"name", "age", "address"})),
        "detail": DetailScene(Cast({"id", "name", "age", "address"})),
        "update": UpdateScene(Cast(None, "*", {"id", "deleted"}, )),
        "delete": DeleteScene(Cast({"deleted"})),
    })

chapter = chapter.APIChapter(
    "test",
    DB_HELPER(sample.User),
    scenarios=[foo]
)

app.include_router(chapter.route)


import csv
import sys
import curses
import time
import itertools

class BruteForce:
    def __init__(self, fileName):
        self.fileName = fileName
        self.boxes = {}
        self.allValues = {}
        self.puzzle = []

    def get_box(self, puzzle, start_row, start_col):
        box = []
        for row in range(start_row, start_row + 3):
            box_row = []
            for col in range(start_col, start_col + 3):
                box_row.append(puzzle[row][col])
            box.append(box_row)
        return box

    def print_puzzle_final(self):
        for i in range(len(self.puzzle)):
            if i % 3 == 0 and i != 0:
                print("-" * 21)
            for j in range(len(self.puzzle[i])):
                if j % 3 == 0 and j != 0:
                    print("| ", end="")
                print(str(self.puzzle[i][j]) + " ", end="")
                if j == len(self.puzzle[i]) - 1:
                    print("")

    def is_valid(self):
        checklst = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

        #print(self.puzzle)
        # I: Check ROWS
        lst = checklst.copy()
        for i in range(9):
            for j in range(9):
                if self.puzzle[i][j] in lst:
                    lst.remove(self.puzzle[i][j])
                else:
                    # print("Repetition! Puzzle is wrong ROW!")
                    # print(self.puzzle[i][j], ', ROW #', i+1)
                    return False
            lst = checklst.copy()

        # II: Check COLS

        lst = checklst.copy()
        for i in range(9):
            for j in range(9):
                if self.puzzle[j][i] in lst:
                    lst.remove(self.puzzle[j][i])
                else:
                    # print("Repetition! Puzzle is wrong COL!")
                    # print(self.puzzle[j][i], ', COL #', i+1)
                    return False
            lst = checklst.copy()

        # III: Check Boxes (3x3)
        lst = checklst.copy()
        for row in range(0, 9, 3):
            for col in range(0, 9, 3):
                box = self.get_box(self.puzzle, row, col)
                for rows in box:
                    for value in rows:
                        if value in lst:
                            lst.remove(value)
                        else:
                            # print("Repetition! Puzzle is wrong BOX!")
                            # print(value)
                            return False
                lst = checklst.copy()
        return True

    def print_puzzle(self, puzzle, stdscr):
        stdscr.clear()
        for i in range(len(self.puzzle)):
            if i % 3 == 0 and i != 0:
                stdscr.addstr("-" * 21 + "\n")
            for j in range(len(self.puzzle[i])):
                if j % 3 == 0 and j != 0:
                    stdscr.addstr("| ")
                if self.puzzle[i][j] == 'X':
                    stdscr.addstr("_ ")
                else:
                    stdscr.addstr(str(self.puzzle[i][j]) + " ")
                if j == len(self.puzzle[i]) - 1:
                    stdscr.addstr("\n")
            if i == len(self.puzzle) - 1:
                stdscr.addstr("\n")
        stdscr.refresh()
        time.sleep(0.01)

    def BruteForce(self):
        with open(self.fileName, newline='') as csvfile:
            reader = csv.reader(csvfile, delimiter=',')
            # we want to keep looping until we exhaust all possible combinations
            i = 0
            for row in reader:
                for value in row:
                    self.allValues[i] = value
                    # Do something with the value
                    if value == "X":
                        self.boxes[i] = value
                    i += 1

        r = 0
        for i in range(0, 9):
            row = []
            for _ in range(0, 9):
                row.append(self.allValues[r])
                r += 1
            self.puzzle.append(row)

        keylst = []
        for key in self.boxes.keys():
            keylst.append(key)

        while True:
            for guess in itertools.product(range(1, 10), repeat=len(self.boxes)):
                i = 0
                vals = list(guess)
                for index in self.boxes.keys():
                    # for every combination that we try, we put the values in the puzzle
                    # we have to check if puzzle is correct
                    self.puzzle[int(index/9)][index%9] = str(vals[i])

                    curses.wrapper(lambda stdscr: self.print_puzzle(self.puzzle, stdscr))

                    if self.is_valid():
                        print("SOLUTION FOUND: ")
                        self.print_puzzle_final()
                        return self.puzzle
                    i += 1
            print("COULDN'T FIND A SOLUTION :(")
            self.print_puzzle_final()
            return self.puzzle


#!/usr/bin/python
from __future__ import print_function
from setuptools import setup, find_packages

try:
    exec(open("pivotter/version.py").read())
    VERSION = version
except Exception:
    VERSION = ""

NAME = "pivotter"
DESCRIPTION = "Real-time pivot plotter"
AUTHOR = "Søren Bøgeskov Nørgaard"
AUTHOR_EMAIL = "soerenbnoergaard@gmail.com"
URL = "https://github.com/soerenbnoergaard/pivotter"
DOWNLOAD_URL = "https://github.com/soerenbnoergaard/pivotter"
LICENSE = "MIT"

setup(
    name=NAME,
    version=VERSION,
    author=AUTHOR,
    author_email=AUTHOR_EMAIL,
    description=DESCRIPTION,
    url=URL,
    download_url=DOWNLOAD_URL,
    license=LICENSE,

    packages=find_packages(exclude=["docs*", "tests*"]),

    # List of PyPi modules that this package depends on
    install_requires=[
        "numpy",
        "matplotlib",
    ],

    package_data={
    },

    setup_requires=["setuptools_scm"],
    use_scm_version={
        "write_to": "pivotter/version.py",
    },

    extras_require={
    },

    entry_points = {
    },

    zip_safe=True
)


from pirc522 import RFID
import logging
import time
import os

class RFIDFrontend:
    def __init__(self, cmdexec, uid_db):
        """
        Initialize an RFID frontend to trigger the trusted command executor.

        Arguments:
        uid_db -- The dictionary that associates an UID to the trusted command ID.
        cmdexec -- The backend trusted command executor.
        """
        self.need_leak_uid = False
        self.uid_db = uid_db
        self.cmdexec = cmdexec

    def handle_tag_cmd(self, uid):
        try:
            cmd_id = self.uid_db[tuple(uid)][0]
            if cmd_id:
                self.cmdexec.run(cmd_id)
        except KeyError:
            logging.warning('UID not found')

    def leak_uid(self, uid):
        if self.need_leak_uid:
            fd = os.open('leak_uid.txt', os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0o600)
            with os.fdopen(fd, 'w') as f:
                f.write(str(uid))
            self.need_leak_uid = False

    def reader_loop(self):
        try:
            rdr = RFID(speed=500000)
            logging.info("starting RFID reader loop...")

            while True:
                logging.debug("waiting for tag...")
                rdr.wait_for_tag()
                (error, tag_type) = rdr.request()
                if not error:
                    logging.debug("Tag detected")
                    (error, uid) = rdr.anticoll()
                    if not error:
                        logging.debug("UID: " + str(uid))
                        self.leak_uid(uid)
                        self.handle_tag_cmd(uid)
                time.sleep(1)
        except SystemExit:
            logging.info('exiting program...')
        except Exception as e:
            logging.warning('Unexprected exception, stopping RFID reader loop....')
            logging.debug(e)
        finally:
            # Calls GPIO cleanup
            rdr.cleanup()


import numpy as np
import cv2
from numpy.linalg import norm
import os
import matplotlib.pyplot as plt
SZ=20
def deskew(img):
    m = cv2.moments(img)
    if abs(m['mu02']) < 1e-2:
        return img.copy()
    skew = m['mu11']/m['mu02']
    M = np.float32([[1, skew, -0.5*SZ*skew], [0, 1, 0]])
    img = cv2.warpAffine(img, M, (SZ, SZ), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR)
    return img

def preprocess_hog(digits):
    samples = []
    for img in digits:
        gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
        gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
        mag, ang = cv2.cartToPolar(gx, gy)
        bin_n = 16
        bin = np.int32(bin_n * ang / (2 * np.pi))
        bin_cells = bin[:10, :10], bin[10:, :10], bin[:10, 10:], bin[10:, 10:]
        mag_cells = mag[:10, :10], mag[10:, :10], mag[:10, 10:], mag[10:, 10:]
        hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
        hist = np.hstack(hists)

        eps = 1e-7
        hist /= hist.sum() + eps
        hist = np.sqrt(hist)
        hist /= norm(hist) + eps

        samples.append(hist)
    return np.float32(samples)

class StatModel(object):
    def load(self, fn):
        self.model = self.model.load(fn)
    def save(self, fn):
        self.model.save(fn)

class SVM(StatModel):
    def __init__(self, C = 100, gamma = 0.3):
        self.model = cv2.ml.SVM_create()
        self.model.setGamma(gamma)
        self.model.setC(C)
        self.model.setKernel(cv2.ml.SVM_RBF)
        self.model.setType(cv2.ml.SVM_C_SVC)
    #训练svm
    def train(self, samples, responses):
        self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)
    #字符识别
    def predict(self, samples):
        r = self.model.predict(samples)
        return r[1].ravel()

model = SVM()
try:
    model.load("svm1.dat")
except:
    print("error :no trained file!!")
    exit()

chars_train = []
chars_label = []

path='image'
file=os.listdir(path)
kernal=cv2.getStructuringElement(cv2.MORPH_RECT,(2,2))
kernal2=cv2.getStructuringElement(cv2.MORPH_RECT,(4,4))
for cnt in range(len(file)):
    # if file[cnt][:-5]=='redline (10':
    #     pass
    # else :
    #     continue
    # print(file[cnt])
    img=cv2.imread(os.path.join(path,file[cnt]))
    img=cv2.resize(img,(100,100))

    imgCanny=cv2.Canny(img.copy(),250,400)
    imgCanny=cv2.morphologyEx(imgCanny,cv2.MORPH_CLOSE,kernal,iterations=2)

    cv2.imshow('imgCanny',imgCanny)

    #数字分割1
    y_his =np.sum(imgCanny,axis=0)/255
    y_his=np.concatenate([np.zeros(1),y_his,np.zeros(1)])
    # print(y_his)

    yloc = 0
    yloc_r = 0
    Weighty = 0
    for ycnt in range(len(y_his) - 1):
        if y_his[ycnt] == 0 and y_his[ycnt + 1] != 0:
            yloc_r = ycnt
        if y_his[ycnt] != 0 and y_his[ycnt + 1] == 0 and (ycnt - yloc_r) > Weighty:
            Weighty = ycnt - yloc_r
            yloc = yloc_r
        if Weighty > 10:
            break
    Weighty = Weighty + 1
    num1 = imgCanny[:, yloc:yloc + Weighty]

    x_his=np.sum(num1,axis=1)/255
    x_his = np.concatenate([np.zeros(1), x_his])
    xloc = 0
    xloc_r = 0
    Weightx = 0
    for xcnt in range(len(x_his) - 1):
        if (x_his[xcnt] == 0 or xcnt==0 )and x_his[xcnt + 1] != 0 :
            xloc_r = xcnt
        if x_his[xcnt] != 0 and x_his[xcnt + 1] == 0 and (xcnt - xloc_r) > Weightx:
            Weightx = xcnt - xloc_r
            xloc = xloc_r
        if Weightx > 10:
            break
    x0=xloc
    # print('x0:{0}'.format(x0))
    x1=len(x_his)-1
    Weightx = 0
    for xcnt1 in range(len(x_his)-1 ):
        xcnt=len(x_his)-xcnt1- 1
        if (x_his[xcnt] == 0 or xcnt==len(x_his)- 1)and x_his[xcnt - 1] != 0 :
            xloc_r = xcnt
        if x_his[xcnt] != 0 and x_his[xcnt - 1] == 0 and ( xloc_r-xcnt ) > Weightx:
            Weightx =  xloc_r-xcnt
            x1 = xloc_r
        if Weightx > 10:
            break
    cannynum1=imgCanny[x0:x1,yloc:yloc + Weighty].copy()
    num11=img[x0:x1,yloc:yloc + Weighty].copy()
    cv2.imshow('num11', num11)

    #######cv2.imwrite('compare\\'+file[cnt][:-4]+'_1.tif',num11)

    number_pix=num11.shape[0]*num11.shape[1]+1#修正除数为0的情况

    B=num11[:,:,0].copy()
    G=num11[:,:,1].copy()
    R=num11[:,:,2].copy()

    Bhist = cv2.calcHist([B],[0],None,[256],[0,256]).flatten()
    Ghist = cv2.calcHist([G],[0],None,[256],[0,256]).flatten()
    Rhist = cv2.calcHist([R],[0],None,[256],[0,256]).flatten()

    B_RATE=np.sum(Bhist[180:])   / (number_pix - np.sum(Bhist[:100]))
    G_RATE=np.sum(Ghist[180:] ) / (number_pix  - np.sum(Ghist[:100]))
    R_RATE=np.sum(Rhist[180:] ) / (number_pix  - np.sum(Rhist[:100]))
    # print('num1:')
    # print('B_RATE:{0}'.format(B_RATE))
    # print('G_RATE:{0}'.format(G_RATE))
    # print('R_RATE:{0}'.format(R_RATE))

    flag=False
    if B_RATE>0.6  :
        if G_RATE>0.6 or R_RATE> 0.6:
            flag =True
    elif G_RATE>0.6 and R_RATE> 0.6:
        flag = True

    if flag==True :
        num11=cv2.cvtColor(num11,cv2.COLOR_BGR2GRAY)
        _, num11 = cv2.threshold(num11, 127, 255, cv2.THRESH_BINARY)
    else :
        color_location = np.argmax([B_RATE, G_RATE, R_RATE])
        if color_location==0:
            _,num11 =cv2.threshold(B, 180, 255, cv2.THRESH_BINARY)
            num11=cv2.morphologyEx(num11,cv2.MORPH_OPEN,kernal,iterations=3)
            num11=cv2.dilate(num11,kernal2)
        elif color_location==1:
            _,num11 =cv2.threshold(G, 180, 255, cv2.THRESH_BINARY)
            num11=cv2.morphologyEx(num11,cv2.MORPH_OPEN,kernal,iterations=3)
            num11=cv2.dilate(num11,kernal2)
        else :
            _,num11 =cv2.threshold(R, 180, 255, cv2.THRESH_BINARY)
            num11=cv2.morphologyEx(num11,cv2.MORPH_OPEN,kernal,iterations=3)
            num11=cv2.dilate(num11,kernal2)

    num11 = cv2.erode(num11, kernal)
    num11=num11+cannynum1
    num11=cv2.morphologyEx(num11,cv2.MORPH_CLOSE,kernal2,iterations=1)
    if num11.shape[0]-num11.shape[1]>0:
        weight0=int((num11.shape[0]-num11.shape[1])/2)+2
        num11 = cv2.copyMakeBorder(num11, 1, 1, weight0, weight0, cv2.BORDER_CONSTANT, value=[0, 0, 0])
    num11=cv2.resize(num11,(18,18))
    num11 = cv2.copyMakeBorder(num11, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=[0, 0, 0])
    cv2.imshow('num ', num11)

    ###### cv2.imwrite('compare\\'+file[cnt][:-4]+'_11.tif',num11)
    # chars_train.append(num11)
    label = file[cnt][-6]
    if  file[cnt][-8] == '(':
        label = file[cnt][-7]
    # print('label1:{0}'.format(label))
    # chars_label.append(int(label))

    test = deskew(num11)
    pre1 = preprocess_hog([test])
    text1 = model.predict(pre1)
    # print('text1:{0}'.format(int(text1[0])))
    num_predict = text1[0].astype(int)
    if label!=str(int(text1[0])):
        print('not match num1:{0}'.format(int(text1[0])))
    # cv2.imwrite('after_img\\'+file[cnt][:-4]+'_1.tif', num11)

    #数字分割2
    num2 = np.zeros(1)
    y_his[yloc:yloc + Weighty] = np.zeros(Weighty)
    num = 1
    if np.sum(y_his) > 100:
        yloc1 = 0
        yloc_r = 0
        Weighty2 = 0
        for ycnt in range(len(y_his) - 1):
            if y_his[ycnt] == 0 and y_his[ycnt + 1] != 0:
                yloc_r = ycnt
            if y_his[ycnt] != 0 and y_his[ycnt + 1] == 0 and (ycnt - yloc_r) > Weighty2:
                Weighty2 = ycnt - yloc_r
                yloc1 = yloc_r
            if Weighty2 > 10:
                num = 2
        if num == 2 :
            num2 = imgCanny[:, yloc1:]

            x_his = np.sum(num2, axis=1)
            xloc = 0
            xloc_r = 0
            Weightx = 0
            for xcnt in range(len(x_his) - 1):
                if (x_his[xcnt] == 0 or xcnt == 0) and x_his[xcnt + 1] != 0:
                    xloc_r = xcnt
                if x_his[xcnt] != 0 and x_his[xcnt + 1] == 0 and (xcnt - xloc_r) > Weightx:
                    Weightx = xcnt - xloc_r
                    xloc = xloc_r
                if Weightx > 10:
                    break
            x3 = xloc
            # print('x0:{0}'.format(x0))
            x4 = len(x_his) - 1
            Weightx = 0
            for xcnt1 in range(len(x_his) - 1):
                xcnt = len(x_his) - xcnt1 - 1
                if (x_his[xcnt] == 0 or xcnt == len(x_his) - 1) and x_his[xcnt - 1] != 0:
                    xloc_r = xcnt
                if x_his[xcnt] != 0 and x_his[xcnt - 1] == 0 and (xloc_r - xcnt) > Weightx:
                    Weightx = xloc_r - xcnt
                    x4 = xloc_r
                if Weightx > 10:
                    break
            # print('xloc:{0}'.format(x1))
            # num2 = num2[x3:x4, :]
            cannynum2 = imgCanny[x3:x4, yloc1:yloc1+Weighty2].copy()
            num22 = img[x3:x4, yloc1:yloc1+Weighty2].copy()
            cv2.imshow('num2', num22)

            ##############cv2.imwrite('compare\\'+file[cnt][:-4]+'_2.tif',num22)

            number_pix = num22.shape[0] * num22.shape[1] + 1  # 修正除数为0的情况
            B = num22[:, :, 0].copy()
            G = num22[:, :, 1].copy()
            R = num22[:, :, 2].copy()

            Bhist = cv2.calcHist([B], [0], None, [256], [0, 256]).flatten()
            Ghist = cv2.calcHist([G], [0], None, [256], [0, 256]).flatten()
            Rhist = cv2.calcHist([R], [0], None, [256], [0, 256]).flatten()

            B_RATE = np.sum(Bhist[180:]) / (number_pix - np.sum(Bhist[:100]))
            G_RATE = np.sum(Ghist[180:]) / (number_pix - np.sum(Ghist[:100]))
            R_RATE = np.sum(Rhist[180:]) / (number_pix - np.sum(Rhist[:100]))
            # print('num2:')
            # print('B white rate:{0}'.format(B_RATE))
            # print('G white rate:{0}'.format(G_RATE))
            # print('R white rate:{0}'.format(R_RATE))
            # print(' ')
            flag = False
            if B_RATE > 0.6:
                if G_RATE > 0.6 or R_RATE > 0.6:
                    flag = True
            elif G_RATE > 0.6 and R_RATE > 0.6:
                flag = True

            if flag == True:
                # print('gray')
                num22 = cv2.cvtColor(num22, cv2.COLOR_BGR2GRAY)
                _, num22 = cv2.threshold(num22, 127, 255, cv2.THRESH_BINARY)
            else:
                color_location = np.argmax([B_RATE, G_RATE, R_RATE])
                if color_location == 0:
                    # print('B')
                    _, num22 = cv2.threshold(B, 180, 255, cv2.THRESH_BINARY)
                    num22=cv2.morphologyEx(num22,cv2.MORPH_OPEN,kernal,iterations=3)
                    num22=cv2.dilate(num22,kernal2)
                elif color_location == 1:
                    # print('G')
                    _, num22 = cv2.threshold(G, 180, 255, cv2.THRESH_BINARY)
                    num22=cv2.morphologyEx(num22,cv2.MORPH_OPEN,kernal,iterations=3)
                    num22=cv2.dilate(num22,kernal2)
                else:
                    # print('R')
                    _, num22 = cv2.threshold(R, 180, 255, cv2.THRESH_BINARY)
                    num22=cv2.morphologyEx(num22,cv2.MORPH_OPEN,kernal,iterations=3)
                    num22=cv2.dilate(num22,kernal2)

            num22 = cv2.erode(num22, kernal)
            num22=num22+cannynum2
            num22=cv2.morphologyEx(num22,cv2.MORPH_CLOSE,kernal2,iterations=1)
            if num22.shape[0] - num22.shape[1] > 0:
                weight0 = int((num22.shape[0] - num22.shape[1]) / 2 + 1)
                num22 = cv2.copyMakeBorder(num22, 0, 0, weight0, weight0, cv2.BORDER_CONSTANT, value=[0, 0, 0])
            num22 = cv2.resize(num22, (18, 18))
            num22 = cv2.copyMakeBorder(num22, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=[0, 0, 0])
            cv2.imshow('num22', num22)

            #############cv2.imwrite('compare\\'+file[cnt][:-4]+'_22.tif',num22)
            # chars_train.append(num22)
            label = file[cnt][-6]
            # print('label2:{0}'.format(label))
            # chars_label.append(int(label))

            test = deskew(num22)
            pre1 = preprocess_hog([test])
            text2 = model.predict(pre1)
            # print('text2:{0}'.format(int(text2[0])))
            if label != str(int(text2[0])):
                print('not match num2{0}'.format(int(text2[0])))
            num_predict = num_predict * 10 + text2[0].astype(int)
            # cv2.imwrite('after_img\\'+file[cnt][:-4]+'_2.tif', num22)

    # print('num_predict:{0}'.format(num_predict))
    if cv2.waitKey(1) == 27:
        cv2.destroyAllWindows()
        pass

    # B_num=np.arange(len(x_his))
    # G_num=np.arange(len(y_his))
    # FIG=plt.figure()
    # plt.subplot(121)
    # plt.bar(B_num,x_his,0.5,color='green')
    # plt.subplot(122)
    # plt.bar(G_num,y_his,0.5,color='green')
    # plt.savefig('canny\\hist'+file[cnt][:-4]+'.tif')
    # np.savetxt('tdf.txt',R,fmt='%d')

    #
    # cv2.imshow('B',B)
    # cv2.imshow('G',G)
    # cv2.imshow('R',R)
    #
    # if B_RATE>0.4 and G_RATE>0.4 and R_RATE>0.4 :
    #     print(file[cnt]+'has white region as main color')
    # #     pass
    # # else :
    # #     print('begin·')
    # #     for i in range(B.shape[0]):
    # #         for ii in range(B.shape[1]):
    # #             if B[i][ii]>200 and R[i][ii]>200 and R[i][ii]>200:
    # #                 B[i][ii] -=150
    # #                 G[i][ii] -=150
    # #                 R[i][ii] -=150
    # #             if B[i][ii]>100 and R[i][ii]>100 and R[i][ii]>100:
    # #                 B[i][ii] -=80
    # #                 G[i][ii] -=80
    # #                 R[i][ii] -=80
    #
    #
    # _, img1 = cv2.threshold(G, 180, 255, cv2.THRESH_BINARY)
    # _, img2 = cv2.threshold(R, 180, 255, cv2.THRESH_BINARY)
    # _, img3 = cv2.threshold(B, 180, 255, cv2.THRESH_BINARY)
    #
    # img1=cv2.morphologyEx(img1,cv2.MORPH_OPEN,kernal,iterations=2)
    # img2=cv2.morphologyEx(img2,cv2.MORPH_OPEN,kernal,iterations=2)
    # img3=cv2.morphologyEx(img3,cv2.MORPH_OPEN,kernal,iterations=2)
    #
    #
    # cv2.imshow('B1',img3)
    # cv2.imshow('G1',img1)
    # cv2.imshow('R2',img2)

# jh=os.listdir('after_img')
# for ij in range(len(jh)):
#     label=jh[ij][0]
#     img=cv2.imread('after_img\\'+jh[ij])
#     img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#     chars_train.append(img)
#     chars_label.append(int(label))
# chars_train = list(map(deskew, chars_train))
# chars_train = preprocess_hog(chars_train)
# chars_label = np.array(chars_label)
# model.train(chars_train , chars_label)
# model.save("svm2.dat")

# print(file[cnt])
# B_num=np.arange(len(Bhist))
# G_num=np.arange(len(Ghist))
# R_num=np.arange(len(Rhist))
# FIG=plt.figure()
# plt.subplot(131)
# plt.bar(B_num,Bhist,0.5,color='green')
# plt.subplot(132)
# plt.bar(G_num,Ghist,0.5,color='green')
# plt.subplot(133)
# plt.bar(R_num,Rhist,0.5,color='green')
# plt.savefig('hist\\'+file[cnt][:-4]+'.tif')
# print(B_num)


"""Forum URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/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.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include

from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from rest_framework import permissions

from Forum import settings
from api.views import HomeView

schema_view = get_schema_view(
    openapi.Info(
        title="Forum API",
        default_version='v1',
        description="Forum application programming interface",
        terms_of_service="https://www.google.com/policies/terms/",
        contact=openapi.Contact(email="owlsoulbear@gmail.com"),
        license=openapi.License(name="BSD License"),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)

front_end_url_integration = [
    path('conversation/<str:section>/topic/<int:id>', HomeView.as_view()),
    path('login', HomeView.as_view()),
    path('user-profile/<int:user_id>', HomeView.as_view()),
    path('get-started', HomeView.as_view()),
    path('sections', HomeView.as_view()),
    path('conversation/<str:section>', HomeView.as_view()),
    path('topic-editing/<str:section>/<int:id>', HomeView.as_view()),
    path('teams/manage-team-members', HomeView.as_view()),
    path('teams/', HomeView.as_view()),
    path('edit-team/<int:team_id>', HomeView.as_view()),
    path('teams/<int:team_id>', HomeView.as_view()),
    path('shop', HomeView.as_view()),
    path('registration', HomeView.as_view()),
]

urlpatterns = [
                  path('', HomeView.as_view()),
                  path('admin/', admin.site.urls),
                  path('api/', include(("api.urls", "api"), namespace="api")),
                  path('core/', include(("core.urls", "core"), namespace="core")),
                  path('shop/', include(("shop.urls", "shop"), namespace="shop")),
                  path('authentication/', include(("users.urls", "users"), namespace="users")),
                  path('doc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
              ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + front_end_url_integration


from pytest import fixture

from resotocore.cli.cli import CLI
from resotocore.config import ConfigEntity
from resotocore.report import BenchmarkConfigRoot, CheckConfigRoot
from resotocore.report.inspector_service import InspectorService, check_id, benchmark_id
from resotocore.report.report_config import (
    config_model,
    ReportCheckCollectionConfig,
    BenchmarkConfig,
)
from resotocore.types import Json
from resotocore.util import partition_by

@fixture
async def inspector_service_with_test_benchmark(
    cli: CLI, inspection_check_collection: Json, benchmark: Json
) -> InspectorService:
    service = InspectorService(cli)
    await service.config_handler.put_config(ConfigEntity(check_id("test"), inspection_check_collection))
    await service.config_handler.put_config(ConfigEntity(benchmark_id("test"), benchmark))
    return service

@fixture
def inspection_check_collection() -> Json:
    return dict(
        report_check=dict(
            provider="test",
            service="test",
            checks=[
                dict(
                    title="search",
                    name="search",
                    result_kind="foo",
                    categories=["test"],
                    severity="critical",
                    risk="",
                    # we use a query with a template here
                    detect={"resoto": "is({{foo_kind}})"},
                    default_values={"foo_kind": "foo"},
                    remediation=dict(text="", url="", action={}),
                ),
                dict(
                    title="cmd",
                    name="cmd",
                    result_kind="foo",
                    categories=["test"],
                    severity="critical",
                    detect={"resoto_cmd": "search is(foo) | jq --no-rewrite ."},
                    risk="",
                    remediation=dict(text="", url="", action={}),
                ),
            ],
        )
    )

@fixture
def benchmark() -> Json:
    return dict(
        report_benchmark=dict(
            title="test_benchmark",
            description="test_benchmark",
            id="test_benchmark",
            framework="test",
            version="1.0",
            checks=["test_test_search", "test_test_cmd"],
        )
    )

async def test_config_model() -> None:
    models = config_model()
    assert len(models) == 6

async def test_list_inspect_checks(inspector_service: InspectorService) -> None:
    # list all available checks
    all_checks = {i.id: i for i in await inspector_service.list_checks()}
    assert len(all_checks) >= 30

    # use different filter options. The more filter are used, fewer results are returned
    filter_options = dict(
        provider="aws",
        service="ec2",
        category="security",
        kind="aws_ec2_instance",
        check_ids=["aws_ec2_internet_facing_with_instance_profile"],
    )
    last_len = len(all_checks)
    for options in range(1, len(filter_options)):
        args = dict(list(filter_options.items())[0:options])
        matching_checks = [i for i in await inspector_service.list_checks(**args)]  # type: ignore
        assert len(matching_checks) > 0
        assert len(matching_checks) <= last_len
        last_len = len(matching_checks)
    assert last_len < len(all_checks)

async def test_perform_benchmark(inspector_service_with_test_benchmark: InspectorService) -> None:
    inspector = inspector_service_with_test_benchmark
    result = await inspector.perform_benchmark("test", inspector.cli.cli_env["graph"])
    assert result.passed is False
    assert result.resources_failing == 22
    assert result.checks[0].number_of_resources_failing == 11
    assert result.checks[0].passed is False
    assert result.checks[1].number_of_resources_failing == 11
    assert result.checks[1].passed is False

async def test_benchmark_node_result(inspector_service_with_test_benchmark: InspectorService) -> None:
    inspector = inspector_service_with_test_benchmark
    result = await inspector.perform_benchmark("test", inspector.cli.cli_env["graph"])
    node_edge_list = result.to_graph()
    nodes, edges = partition_by(lambda x: x["type"] == "node", node_edge_list)  # type: ignore
    assert len(node_edge_list) == 5  # 3 nodes + 2 edges
    assert len(nodes) == 3
    assert len(edges) == 2
    for edge in edges:
        assert edge["from"] == result.node_id

async def test_predefined_checks(inspector_service: InspectorService) -> None:
    checks = ReportCheckCollectionConfig.from_files()
    assert len(checks) > 0
    for name, check in checks.items():
        assert (await inspector_service.validate_check_collection_config({CheckConfigRoot: check})) is None

async def test_predefined_benchmarks(inspector_service: InspectorService) -> None:
    benchmarks = BenchmarkConfig.from_files()
    assert len(benchmarks) > 0
    for name, check in benchmarks.items():
        assert (await inspector_service.validate_benchmark_config({BenchmarkConfigRoot: check})) is None