# from autogluon.tabular import TabularDataset, TabularPredictor
# train_data = TabularDataset('https://autogluon.s3.amazonaws.com/datasets/Inc/train.csv')
# f = open('/workfile.txt', 'r+')
# f.write(train_data)
# subsample_size = 500  # subsample subset of data for faster demo, try setting this to much larger values
# train_data = train_data.sample(n=subsample_size, random_state=0)
# train_data.head()
# label = 'class'
# print("Summary of class variable: \n", train_data[label].describe())
# save_path = 'agModels-predictClass'  # specifies folder to store trained models
# predictor = TabularPredictor(label=label, path=save_path).fit(train_data)
# test_data = TabularDataset('https://autogluon.s3.amazonaws.com/datasets/Inc/test.csv')
# y_test = test_data[label]  # values to predict
# test_data_nolab = test_data.drop(columns=[label])  # delete label column to prove we're not cheating
# test_data_nolab.head()
# predictor = TabularPredictor.load(save_path)  # unnecessary, just demonstrates how to load previously-trained predictor from file
# y_pred = predictor.predict(test_data_nolab)
# test_data_nolab[label] = y_pred
# test_data_nolabel.to_csv(filelist[3],encoding='utf-8', index=False)F

# print("Predictions:  \n", y_pred)
# perf = predictor.evaluate_predictions(y_true=y_test, y_pred=y_pred, auxiliary_metrics=True)

# import os
# import os.path
# from os import path
# # import aif360.datasets.compas_dataset

# # filelist = ['workfile.csv', 'workfile_test.csv', 'workfile_train.csv', 'workfile_test_predictions.csv']
# # for file in filelist:
# #     if path.isfile(file):
# #         os.remove(file)
# import pandas as pd
# # protected_attributes = ['age', 'sex', 'race', 'gender', 'ethnicity',
# #                         'marital status', 'religion', 'national origin',
# #                         'public assistance', 'disability', 'pregnancy', 'maternity']
# # df = pd.read_csv('workfile.csv')
# # print(list(df))
# # present_protected_attributes = []
# # for attr in df:
# #     for prot in protected_attributes:
# #         if prot in attr:
# #             present_protected_attributes.append(attr)

# # print(present_protected_attributes)
from aif360.datasets import StandardDataset
from aif360.metrics import BinaryLabelDatasetMetric
from sklearn.preprocessing import OrdinalEncoder
import pandas as pd
from itertools import permutations

label = 'decile_score'
# protected_attr = 'sex'
# priveledged = 'Female'
# unpriveledged = 'Male'
df = pd.read_csv('compas-scores-low-med-high.csv')
# df[label].mask(df[label] <= 4, 0, inplace=True)
# df[label].mask(df[label] >= 5, 1, inplace=True)

# print(df.head())
# df.fillna(0)

# # # now that we have the binary encoding for scores we can calculate bias metrics
oe = OrdinalEncoder()
df[["sex", "age_cat", "race", "c_days_from_compas", "c_charge_degree", "c_charge_desc"]] = oe.fit_transform(
    df[["sex", "age_cat", "race", "c_days_from_compas", "c_charge_degree", "c_charge_desc"]])
df.to_csv("compas-scores-orig-processed.csv", encoding='utf-8', index=False)

df[label].mask(df[label] == 0, 0, inplace=True)
df[label].mask(df[label] >= 2, 1, inplace=True)
df.fillna(0)

# print(df.head())
# dataset = StandardDataset(df,
#                           label_name=label,
#                           favorable_classes=[0],
#                           protected_attribute_names=[protected_attr],
#                           privileged_classes=[[0]])

# privileged_groups = [{protected_attr: 0}]
# unprivileged_groups = [{protected_attr: 1}]
# metric_orig_train = BinaryLabelDatasetMetric(dataset,
#                                              unprivileged_groups=unprivileged_groups,
#                                              privileged_groups=privileged_groups)

# print(metric_orig_train.mean_difference())
# print(metric_orig_train.disparate_impact())

# if not path.isfile('woo.csv'):
#     print('it works')
# from flask import Flask, render_template, request
# from autogluon.tabular import TabularPredictor
import pandas as pd
from sklearn.model_selection import train_test_split
# import json
# import os
# import shutil
import os.path
# from os import path
import itertools

# label = 'decile_score'
# df = pd.read_csv('compas-scores-orig2.csv')
# make all headers lowercase
# df.columns = df.columns.str.lower()
# # make client label input all lowercase
# label = label.lower()
# df[label].mask(df[label] <= 4, 0, inplace=True)
# df[label].mask(df[label].between(5,7), 1, inplace=True)
# df[label].mask(df[label] >= 8, 2, inplace=True)
# df.fillna(0)

# split the csv into train and test data
# train_data, test_data = train_test_split(df, test_size=0.2)
# # save test and train csv's
# df.to_csv('low_medium_high.csv', encoding='utf-8', index=False)

# models_dir = 'agModels-predictClass'
# predictor = TabularPredictor(label=label, path=models_dir).fit(
#     train_data=train_data, presets='best_quality')
# y_test = test_data[label]  # values to predict
# # delete label column
# test_data_nolabel = test_data.drop(columns=[label])
# # predict
# y_pred = predictor.predict(test_data_nolabel)
# save predictions
# test_data = test_data_nolabel
# test_data_nolabel[label] = y_pred
# test_data_nolabel.to_csv('compas-test.csv',
#                             encoding='utf-8', index=False)
# get random row like in the code
# df = test_data.sample()

# get protected attributes permutations
# protected_attributes = ['age_cat', 'sex', 'race']
# need to make a new df with all permutations of 3 columns
# sex_perms = ['Male', 'Female']
# age_cat_perms = ['Greater than 45', '25-45', 'Less than 25']
# race_perms = ['Other', 'African-American',
#               'Hispanic', 'Asian', 'Native American']
# protected_atrributes = [sex_perms, age_cat_perms, race_perms]
# all_permutations = list(itertools.product(*protected_atrributes))
# print(all_permutations)

# new_df = df.reset_index()
# new_dict = new_df.to_dict('records')
# my_dict = new_dict[0]
# del my_dict['index']

# output = pd.DataFrame()
# # create new df with all permutations
# for permutation in all_permutations:
#     my_dict['sex'] = permutation[0]
#     my_dict['age_cat'] = permutation[1]
#     my_dict['race'] = permutation[2]
#     print(my_dict)
#     output = output.append(my_dict, ignore_index=True)

# print(output.head())
# predictor = TabularPredictor.load(models_dir)
# y_pred = predictor.predict(output)
# # append predictions
# output[label] = y_pred
# # save
# output.to_csv('permutations_csv',
#                                  encoding='utf-8', index=False)

# protected attributes who are the most unpriveledged overall
total_dict_unpriveledged_mean = {}
# protected attributes who have the highest benefit overall
total_dict_higher_benefit = {}

protected_attributes = ['age_cat', 'sex', 'race']
processed_data_sex = {'Male': 1, 'Female': 0}
processed_data_age_cat = {'age_cat_greater_than_45': 1,
                          'age_cat_25_to_45': 0, 'age_cat_less_than_25': 2}
processed_data_race = {'African-American': 0, 'Asian': 1,
                       'Caucasion': 2, 'Hispanic': 3, 'Native American': 4, 'Other': 5}

dataset = StandardDataset(df,
                            label_name=label,
                            # 0 represents low recidivism score
                            favorable_classes=[0],
                            protected_attribute_names=['sex'],
                            # for priveleged class we will use our dict
                            privileged_classes=[[0]]) # Female
# Again using our dict
privileged_groups = [{'sex': 0}] # Female
unprivileged_groups = [{'sex': 1}] # Male
metric_orig_train = BinaryLabelDatasetMetric(dataset,
                                                unprivileged_groups=unprivileged_groups,
                                                privileged_groups=privileged_groups)
# A negative value indicates less favorable outcomes for the unprivileged groups.
mean_diff = metric_orig_train.mean_difference()
# The ideal value of this metric is 1.0 A value < 1 implies higher benefit for the privileged group and a
# value >1 implies a higher benefit for the unprivileged group.
disparate_impact = metric_orig_train.disparate_impact()
if mean_diff < 0:
    sex_unpriveledged_mean = (list(processed_data_sex.keys())[list(processed_data_sex.values()).index(1)])
    total_dict_unpriveledged_mean[sex_unpriveledged_mean] = mean_diff
    key = "{}_mean : {}".format(sex_unpriveledged_mean, mean_diff)
    print(key)
if disparate_impact < 1:
    higher_benefit_priveledged = (list(processed_data_sex.keys())[list(processed_data_sex.values()).index(0)])
    total_dict_higher_benefit[higher_benefit_priveledged] = disparate_impact
    key = "{}_disparate_benefit: {}".format(higher_benefit_priveledged, disparate_impact)
    print(key)
# if disparate_impact > 1:
#     higher_benefit_unpriveledged = (list(pocessed_data_sex.keys())[list(pocessed_data_sex.values()).index(1)])
#     total_dict_higher_benefit[higher_benefit_unpriveledged] = disparate_impact
#     key = "{}_disparate_benefit: {}".format(higher_benefit_unpriveledged, disparate_impact)
#     print(key)

permutations_age_cat = list( permutations( range( 3 ), 2 ) )
lowest_mean_diff = 0
lowest_disparate_impact = 1
mean_list_unpriveledged = []
disparate_impact_list_benefit = []

for combination in permutations_age_cat:
    dataset = StandardDataset(df,
                            label_name=label,
                            # 0 represents low recidivism score
                            favorable_classes=[0],
                            protected_attribute_names=['age_cat'],
                            # for priveleged class we will use our dict
                            privileged_classes=[[combination[0]]])
    # Again using our dict
    privileged_groups = [{'age_cat': combination[0]}]
    unprivileged_groups = [{'age_cat': combination[1]}]
    metric_orig_train = BinaryLabelDatasetMetric(dataset,
                                                    unprivileged_groups=unprivileged_groups,
                                                    privileged_groups=privileged_groups)

    mean_diff = metric_orig_train.mean_difference()
    disparate_impact = metric_orig_train.disparate_impact()

    if mean_diff < 0 and mean_diff < lowest_mean_diff:
        least_priveledged = combination[1]
        lowest_mean_diff = mean_diff
        age_unpriveledged = (list(processed_data_age_cat.keys())[list(processed_data_age_cat.values()).index(least_priveledged)])
        mean_list_unpriveledged.append(age_unpriveledged)

    if disparate_impact < 1 and disparate_impact < lowest_disparate_impact:
        highest_priveledged = combination[0]
        lowest_disparate_impact = disparate_impact
        higher_benefit_priveledged = (list(processed_data_age_cat.keys())[list(processed_data_age_cat.values()).index(highest_priveledged)])
        disparate_impact_list_benefit.append(higher_benefit_priveledged)

age_unpriveledged = (list(processed_data_age_cat.keys())[list(processed_data_age_cat.values()).index(least_priveledged)])
total_dict_unpriveledged_mean[age_unpriveledged] = lowest_mean_diff
key = "{}_mean : {}".format(age_unpriveledged, lowest_mean_diff)
print(key)

higher_benefit_priveledged = (list(processed_data_age_cat.keys())[list(processed_data_age_cat.values()).index(highest_priveledged)])
total_dict_higher_benefit[higher_benefit_priveledged] = lowest_disparate_impact

key = "{}_disparate_benefit: {}".format(higher_benefit_priveledged, lowest_disparate_impact)
print(key)

age_list = ['age_cat_greater_than_45', 'age_cat_25_to_45', 'age_cat_less_than_25']

age_mean_least_counts = {}
for val in age_list:
    c = mean_list_unpriveledged.count(val)
    age_mean_least_counts[val] = c

# To return
age_highest_benefit_counts = {}
for val in age_list:
    c = disparate_impact_list_benefit.count(val)
    age_highest_benefit_counts[val] = c
# print((list(processed_data_age_cat.keys())[list(processed_data_age_cat.values()).index(least_priveledged)]))
# print((list(processed_data_age_cat.keys())[list(processed_data_age_cat.values()).index(compared_to)]))
# print(worst)

permutations_race = permutations_age_cat = list( permutations( range( 6 ), 2 ) )

# reset values
lowest_mean_diff = 0
lowest_disparate_impact = 1

mean_list_unpriveledged_race = []
disparate_impact_list_benefit_race = []
for combination in permutations_race:
    dataset = StandardDataset(df,
                            label_name=label,
                            # 0 represents low recidivism score
                            favorable_classes=[0],
                            protected_attribute_names=['race'],
                            # for priveleged class we will use our dict
                            privileged_classes=[[combination[0]]])
    # Again using our dict
    privileged_groups = [{'race': combination[0]}]
    unprivileged_groups = [{'race': combination[1]}]
    metric_orig_train = BinaryLabelDatasetMetric(dataset,
                                                    unprivileged_groups=unprivileged_groups,
                                                    privileged_groups=privileged_groups)

    mean_diff = metric_orig_train.mean_difference()
    disparate_impact = metric_orig_train.disparate_impact()

    if mean_diff < 0:
        if mean_diff < lowest_mean_diff:
            least_priveledged = combination[1]
            lowest_mean_diff = mean_diff
        race_unpriveledged = (list(processed_data_race.keys())[list(processed_data_race.values()).index(combination[1])])
        mean_list_unpriveledged_race.append(race_unpriveledged)

    if disparate_impact < 1:
        if disparate_impact < lowest_disparate_impact:
            highest_priveledged = combination[0]
            lowest_disparate_impact = disparate_impact
        higher_benefit_priveledged = (list(processed_data_race.keys())[list(processed_data_race.values()).index(combination[0])])
        disparate_impact_list_benefit_race.append(higher_benefit_priveledged)

race_unpriveledged = (list(processed_data_race.keys())[list(processed_data_race.values()).index(least_priveledged)])
total_dict_unpriveledged_mean[race_unpriveledged] = lowest_mean_diff

higher_benefit_priveledged = (list(processed_data_race.keys())[list(processed_data_race.values()).index(highest_priveledged)])
total_dict_higher_benefit[higher_benefit_priveledged] = lowest_disparate_impact

race_list = ['African-American', 'Asian', 'Caucasion', 'Hispanic', 'Native American', 'Other']
race_mean_least_counts = {}
for val in race_list:
    c = mean_list_unpriveledged_race.count(val)
    race_mean_least_counts[val] = c

# To return
race_highest_benefit_counts = {}
for val in race_list:
    c = disparate_impact_list_benefit_race.count(val)
    race_highest_benefit_counts[val] = c

print("\nAge Category: unpriveledged occurences - mean diffence")
print(age_mean_least_counts)
print("\nAge Category: higher benefit occurrences - disparate impact")
print(age_highest_benefit_counts)

print("\nRace: unpriveledged occurences - mean diffence")
print(race_mean_least_counts)
print("\nRace: higher benefit occurrences - disparate impact")
print(race_highest_benefit_counts)

print("\nTotal: Highest benefitting attribute - disparate impact")
print(total_dict_higher_benefit)
print("\nTotal: Most Unpriveledged attribute - mean difference")
print(total_dict_unpriveledged_mean)
# print((list(processed_data_race.keys())[list(processed_data_race.values()).index(least_priveledged)]))
# print((list(processed_data_race.keys())[list(processed_data_race.values()).index(compared_to)]))
# # print(worst_mean)
# print(least_priveledged_list)
# print(max(set(least_priveledged_list), key=least_priveledged_list.count))
# print("Most unpriveledged to Least")
# for i in range (5):
#     print(least_priveledged_list.count(i))
# for unpriv in least_priveledged_list:
#     print((list(processed_data_race.keys())[list(processed_data_race.values()).index(unpriv)]))

dataset = StandardDataset(df,
                            label_name=label,
                            # 0 represents low recidivism score
                            favorable_classes=[0],
                            protected_attribute_names=['race'],
                            # for priveleged class we will use our dict
                            privileged_classes=[[0]])
    # Again using our dict
privileged_groups = [{'race': 0}]
unprivileged_groups = [{'race': 4}]
metric_orig_train = BinaryLabelDatasetMetric(dataset,
                                                unprivileged_groups=unprivileged_groups,
                                                privileged_groups=privileged_groups)

mean_diff = metric_orig_train.mean_difference()
disparate_impact = metric_orig_train.disparate_impact()
print(mean_diff)

# from itertools import permutations
# all_combinations = list( permutations( range( 6 ), 2 ) )
# print(all_combinations)


from django.shortcuts import get_object_or_404, render, redirect, reverse
from django.http import HttpResponseRedirect
from .models import UserNotification

# Create your views here.

def notification_visited(request, pk):
    notification = get_object_or_404(UserNotification, pk=pk)
    question_id = notification.related_question.pk
    notification.delete()
    return HttpResponseRedirect(reverse('posts:question_page', args=(question_id,)))

def delete(request, pk):
    get_object_or_404(UserNotification, pk=pk).delete()
    return HttpResponseRedirect('')


#%%
import nltk
import vaderSentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import numpy as np
import json
import glob
import errno
import pandas
import random
import requests
from bs4 import BeautifulSoup
import news_web_scraping as NWS

#%%
# FUNCTIONS

# input: text (string)
# output: average sentiment of the text and a list containing a tuple of each sentence with its corresponding sentiment
# sentiment is rating between -4 (EXTREAMLY negative) and 4 (EXTREAMLY positive)
def calcSentiment(text):
    analyzer = SentimentIntensityAnalyzer()
    sentence_list = nltk.sent_tokenize(text)
    sentiment_list = []
    if not sentence_list:
        return sentence_list, sentiment_list
        #raise ValueError('no sentences in the text given -> 0/0')
    for sentence in sentence_list:
            vs = analyzer.polarity_scores(sentence)
            compound_sentiment = round(vs["compound"]*4, 4)
            sentiment_list = sentiment_list + [compound_sentiment]
    return sentence_list, sentiment_list

def get_json_text(json_file):
    json_string = json_file.read()
    json_dict = json.loads(json_string)
    return json_dict["text"]

def split_toParagraphs(article_text):
    paragraph_list = article_text.split("\n")
    paragraph_list = [paragraph.strip(' ') for paragraph in paragraph_list]
    paragraph_list = list(filter(None, paragraph_list))
    return paragraph_list

def paragraph_analysis(paragraph_list):
    all_para_sentiments = []
    all_para_sentences = []
    for paragraph in paragraph_list:
        para_sentences, para_sentiments = calcSentiment(paragraph)
        if para_sentiments:
            para_sentiment_avg = np.mean(np.asarray(para_sentiments))
            all_para_sentiments = all_para_sentiments + [para_sentiment_avg]
            all_para_sentences = all_para_sentences + [para_sentences]
    return all_para_sentences, all_para_sentiments

# preforms sentiment analysis on each article in a list of articles
def article_list_analysis(article_list):
    article_sentence_list = []
    sentence_sentiment_list = []
    avg_article_sentiment_list = []
    for article_text in article_list:
        if article_text:
            article_text = str(article_text)
            sentence_list, sentiment_list = calcSentiment(article_text)
            if sentiment_list:
                article_sentiment = np.mean(np.asarray(sentiment_list))
                article_sentence_list = article_sentence_list + [sentence_list]
                sentence_sentiment_list = sentence_sentiment_list + [sentiment_list]
                avg_article_sentiment_list = avg_article_sentiment_list + [article_sentiment]
    return article_sentence_list, sentence_sentiment_list, avg_article_sentiment_list

def politi_buzz_analysis(path):
    files = glob.glob(path)
    article_list = []
    for file_name in files:
        try:
            with open(file_name, 'r') as json_file:
                article_text = get_json_text(json_file)
                article_list = article_list + [article_text]
        except IOError as exc:
            if exc.errno != errno.EISDIR:
                raise
    return article_list_analysis(article_list)

def BBC_analysis(path):
    files = glob.glob(path)
    article_list = []
    for file_name in files:
        try:
            with open(file_name, 'r') as txt_file:
                article_text = txt_file.read()
                article_list = article_list + [article_text]
        except IOError as exc:
            if exc.errno != errno.EISDIR:
                raise
    return article_list_analysis(article_list)

def kaggle_Fact_Fake_analysis(path):
    df = pandas.read_csv(path)
    fake_df = df.loc[df['label'] == 1]
    real_df = df.loc[df['label'] == 0]
    article_list_fake = fake_df['text'].values.tolist()
    article_list_real = real_df['text'].values.tolist()
    article_list_fake = random.sample(article_list_fake, 100)  # needed as 13000 takes too long
    article_list_real = random.sample(article_list_real, 100)  # needed as 13000 takes too long
    fake_sentence_list, sentence_sentiment_fake_list, article_sentiment_fake_list = article_list_analysis(article_list_fake)
    real_sentence_list, sentence_sentiment_real_list, article_sentiment_real_list = article_list_analysis(article_list_real)
    return article_sentiment_fake_list, fake_sentence_list, sentence_sentiment_fake_list, article_sentiment_real_list, real_sentence_list, sentence_sentiment_real_list

def avg_var_calculation(sentiment_list, factOrFake, news_source):
    sentiment_array = np.asarray(sentiment_list)
    sentiment_avg = np.mean(sentiment_array)
    sentiment_var = np.var(sentiment_array)
    print(factOrFake + " article avg " + news_source + ": " + str(sentiment_avg))
    print(factOrFake + " article var " + news_source + ": " + str(sentiment_var))
    return sentiment_array, sentiment_avg, sentiment_var

def cal_article_abs_sentiment(list_list):
    mean_list = []
    for list in list_list:
        mean = np.mean(np.absolute(np.asarray(list)))
        mean_list = mean_list + [mean]
    return np.asarray(mean_list)

def kaggle_mult_news_analysis(path, publication):
    df = pandas.read_csv(path)
    df_publication = df.loc[df['publication'] == publication]
    article_list = df_publication['content'].values.tolist()
    article_list = random.sample(article_list, 100)  # needed as otherwise it takes too long
    sentence_list, sentence_sentiments, article_sentiments = article_list_analysis(article_list)
    return sentence_list, sentence_sentiments, article_sentiments

#def kaggel_Fake_analysis(path):
#    df = pandas.read_csv(path)
#    article_list = df['text'].values.tolist()
#    art_sub_list = random.sample(article_list, 100)  # needed as 13000 takes too long
#    sentence_list, sentence_sentiment_list, article_sentiment_tot_list = article_list_analysis(art_sub_list)
#    return article_sentiment_tot_list, sentence_list, sentence_sentiment_list

# scrapes NYT articles and preforms sentiment analysis on them
def NYT_scrape_SA(url):
    title_list, article_list = NWS.NYT_page_scrape(url)
    sentence_list, sentence_sentiments, article_sentiments = article_list_analysis(article_list)
    return title_list, sentence_list, sentence_sentiments, article_sentiments


poppler_path=r"C:\Users\ahmed\Downloads\New folder\Release-22.04.0-0\poppler-22.04.0\Library\bin"

pdf_path = r"D:\certifiaction\freelancing.pdf"

from pdf2image import convert_from_path
pages=convert_from_path(pdf_path=pdf_path,poppler_path=poppler_path)

import os

saving_folder=r"D:\certifiaction"
c=1
for page in pages:
   img_name=f"img-{c}.png"
   page.save(os.path.join(saving_folder,img_name),"png")
   c+=1


import os
import sys
import torch
import torchvision.models
from torch import optim
from torch import nn
from tqdm import tqdm
from collections import Counter
from easydict import EasyDict as edict
from tools import CustomDataSet, Averagemeter, Speedometer, print_one_line, model_size_mb, ROCEstimator
from torch.utils.tensorboard import SummaryWriter
from torch.cuda import amp
import resnet

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# --------- CONFIG -----------

os.environ['TORCH_HOME'] = './weights'

cfg = edict()
cfg.train_in_fp16 = True
cfg.crop_size = (256, 256)
cfg.batch_size = 32
cfg.grad_accum_batches = 4
cfg.num_epochs = 32
cfg.num_classes = 2
cfg.augment = True
cfg.model_name = "effnet_v2_s"
cfg.pretrained = True
cfg.labels_smoothing = 0.05
cfg.max_batches_per_train_epoch = -1  # -1 - all
crop_format = '256x60x0.1' if cfg.crop_size[0] == 256 else '224x90x0.2'
local_path = f"/home/alex/Fastdata/deepfakes/sequence/{crop_format}"

for key in cfg:
    print(f" - {key}: {cfg[key]}")

# ---------- DNN --------------

if cfg.model_name == "effnet_v2_s":
    model = torchvision.models.efficientnet_v2_s()
    if cfg.pretrained:
        model.load_state_dict(torchvision.models.efficientnet_v2_s(
            weights=torchvision.models.EfficientNet_V2_S_Weights.IMAGENET1K_V1).state_dict())
    model.classifier[1] = nn.Linear(in_features=1280, out_features=cfg.num_classes, bias=True)
    max_lr = 0.001
elif cfg.model_name == "resnet18-ir":
    model = resnet.ResNetFaceGray(block=resnet.IRBlock, layers=[2, 2, 2, 2], use_se=True, attention=False,
                                  output_features=cfg.num_classes)
    max_lr = 0.01

model = model.to(device)
# model = nn.DataParallel(model)

print(f" - model size: {model_size_mb(model):.3f} MB")

# Loss and optimizer
loss_fn = nn.CrossEntropyLoss(label_smoothing=cfg.labels_smoothing)
optimizer = optim.Adam(model.parameters(), lr=max_lr)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', patience=2, factor=0.2, min_lr=0.0001,
                                                       verbose=True)

# ----------------------------

writer = SummaryWriter(filename_suffix=cfg.model_name)

print("Train dataset:")
train_dataset = CustomDataSet([
    f"{local_path}/roop",
    f"{local_path}/FaceForensics++",
    f"{local_path}/Celeb-DF-v2",
    f"{local_path}/dfdc",
], cfg.crop_size, do_aug=cfg.augment)
print(f"  {train_dataset.labels_names()}")
assert cfg.num_classes == len(train_dataset.labels_names())
lbls_count = dict(Counter(train_dataset.targets))
print(f"  {lbls_count}")
class_weights = list(1 / torch.Tensor(list(lbls_count.values())))
samples_weights = [class_weights[lbl] for lbl in train_dataset.targets]
sampler = torch.utils.data.WeightedRandomSampler(weights=samples_weights, num_samples=len(train_dataset),
                                                 replacement=True)
train_dataloader = torch.utils.data.DataLoader(train_dataset, sampler=sampler, batch_size=cfg.batch_size, num_workers=8)

alive_lbl = None
for key in train_dataset.labels_names():
    if train_dataset.labels_names()[key] == 'live':
        alive_lbl = key
        break

print("Test dataset:")
test_dataset = CustomDataSet([
    f"{local_path}/toloka"
], cfg.crop_size, do_aug=False)
print(f"  {test_dataset.labels_names()}")
print(f"  {dict(Counter(test_dataset.targets))}")
test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=cfg.batch_size, shuffle=True, num_workers=6)

metrics = {
    'train': {'EER': float('inf'), 'loss': float('inf'), 'BPCER@0.01': float('inf')},
    'test': {'EER': float('inf'), 'loss': float('inf'), 'BPCER@0.01': float('inf')}
}

def update_metrics(mode, epoch, running_loss, roc_estimator):
    if not os.path.exists('./weights'):
        os.makedirs('./weights')
    writer.add_scalar(f"Loss/{mode}", running_loss, epoch)
    if running_loss < metrics[mode]['loss']:
        metrics[mode]['loss'] = running_loss
        print(f" - loss:  {running_loss:.5f} - improvement")
    else:
        print(f" - loss:  {running_loss:.5f}")
    eer, err_s = roc_estimator.estimate_eer()
    writer.add_scalar(f"EER/{mode}", eer, epoch)
    if eer < metrics[mode]['EER']:
        metrics[mode]['EER'] = eer
        if mode == 'test':
            torch.save(model, f"./weights/tmp_{cfg.model_name}@{crop_format}.pth")
        print(f" - EER: {eer:.4f} (score: {err_s:.3f}) - improvement")
    else:
        print(f" - EER: {eer:.4f} (score: {err_s:.3f})")
    print(f" - BPCER@0.1: {roc_estimator.estimate_bpcer(target_apcer=0.1):.4f}")
    bpcer01 = roc_estimator.estimate_bpcer(target_apcer=0.01)
    print(f" - BPCER@0.01: {bpcer01:.4f}")
    writer.add_scalar(f"BPCER@0.01/{mode}", bpcer01, epoch)
    print(f" - BPCER@0.001: {roc_estimator.estimate_bpcer(target_apcer=0.001):.4f}")

loss_avgm = Averagemeter()
ae_avgm = Averagemeter()
speedometer = Speedometer()
scaler = amp.grad_scaler.GradScaler()
train_roc_est = ROCEstimator()
test_roc_est = ROCEstimator()

def train(epoch, dataloader):
    print("TRAIN:")
    train_roc_est.reset()
    loss_avgm.reset()
    ae_avgm.reset()
    speedometer.reset()
    model.train()
    running_loss = 0
    true_positive_live = 0
    false_positive_live = 0
    true_negative_live = 0
    false_negative_live = 0
    samples_enrolled = 0
    for batch_idx, (inputs, labels) in enumerate(dataloader):
        if batch_idx == cfg.max_batches_per_train_epoch:
            break
        inputs = inputs.to(device)
        labels = labels.to(device)
        #u,c = torch.unique(labels, return_counts=True)
        #print(u,c)
        if cfg.train_in_fp16:
            with amp.autocast():
                outputs = model(inputs)
                loss = loss_fn(outputs, labels)
                loss = loss / cfg.grad_accum_batches
            scaler.scale(loss).backward()
            if (batch_idx + 1) % cfg.grad_accum_batches == 0 or batch_idx == (len(dataloader) - 1):
                scaler.unscale_(optimizer)
                torch.nn.utils.clip_grad_norm_(model.parameters(), 5)
                scaler.step(optimizer)
                scaler.update()
                optimizer.zero_grad()
        else:
            outputs = model(inputs)
            loss = loss_fn(outputs, labels)
            loss = loss / cfg.grad_accum_batches
            loss.backward()
            if (batch_idx + 1) % cfg.grad_accum_batches == 0 or batch_idx == (len(dataloader) - 1):
                torch.nn.utils.clip_grad_norm_(model.parameters(), 5)
                optimizer.step()
                optimizer.zero_grad()
        with torch.no_grad():
            scores = torch.nn.functional.softmax(outputs, dim=1)
            train_roc_est.update(live_scores=scores[labels == alive_lbl, alive_lbl].tolist(),
                                 attack_scores=scores[labels != alive_lbl, alive_lbl].tolist())
            running_loss += loss.item()
            _, predicted = outputs.max(1)
            _tp_live = (predicted[labels == alive_lbl] == alive_lbl).sum().item()
            true_positive_live += _tp_live
            _fp_live = (predicted[labels != alive_lbl] == alive_lbl).sum().item()
            false_positive_live += _fp_live
            _tn_live = (predicted[labels != alive_lbl] != alive_lbl).sum().item()
            true_negative_live += _tn_live
            _fn_live = (predicted[labels == alive_lbl] != alive_lbl).sum().item()
            false_negative_live += _fn_live
            loss_avgm.update(loss.item())
            ae_avgm.update((_fp_live / (_fp_live + _tn_live + 1E-6) + _fn_live / (_fn_live + _tp_live + 1E-6)) / 2)
            samples_enrolled += labels.size(0)
            speedometer.update(labels.size(0))
        print_one_line(
            f'Epoch {epoch} >> loss {loss_avgm.val:.3f}, AE {ae_avgm.val * 100:.2f}%, '
            f'{samples_enrolled}/{len(train_dataset)} ~ '
            f'{100 * samples_enrolled / len(train_dataset):.1f} % | '
            f'{speedometer.speed():.0f} samples / s '
        )
    update_metrics('train', epoch, running_loss / batch_idx, train_roc_est)

def test(epoch, dataloader):
    print("TEST:")
    test_roc_est.reset()
    model.eval()
    running_loss = 0
    with torch.no_grad():
        for batch_idx, (inputs, labels) in enumerate(tqdm(dataloader, file=sys.stdout)):
            inputs = inputs.to(device)
            labels = labels.to(device)
            outputs = model(inputs)
            loss = loss_fn(outputs, labels)
            running_loss += loss.item()
            _, predicted = outputs.max(1)
            scores = torch.nn.functional.softmax(outputs, dim=1)
            test_roc_est.update(live_scores=scores[labels == alive_lbl, alive_lbl].tolist(),
                                attack_scores=scores[labels != alive_lbl, alive_lbl].tolist())
    update_metrics('test', epoch, running_loss / len(dataloader), test_roc_est)
    scheduler.step(metrics['test']['EER'])
    print("\n")

for epoch in range(cfg.num_epochs):
    train(epoch, train_dataloader)
    test(epoch, test_dataloader)


# -*- coding: utf-8 -*-
import sys
import re
import pdb
import sqlite3

def to_numbers(ch):
    n_dict = {"一":1,\
              "二":2,\
              "三":3,\
              "四":4,\
              "五":5,\
              "六":6,\
              "七":7,\
              "八":8,\
              "九":9,\
              "十":10}
    return n_dict[ch]

def main(args):
    #pdb.set_trace()
    #rid = "testrid"
    rid = args[1].decode("gb18030");
    #lines = "|||                 小校场    三清殿    小校场    |||                        ＼   ｜   ／|||                 小校场---武当广场---小校场    |||                      ／   ↑   ＼|||                 小校场    凌霄宫    小校场    ||||||武当广场 - [门派]||||||    这是一个由大石板铺成的广场，是武当弟子学习武功和互|||相切磋的地点。周围种满了梧桐树，一到秋天就是满地的落叶。|||一个年纪轻轻的道童正在打扫。北边是灵霄宫三清殿。|||  「深秋」: 东方的天空渐渐的发白了，又一个万物初醒的早上！。||||||    这里明显的出口是 north、east、southeast、northeast、west、southdown、southwest|||、northwest 和 eastdown。||||||    二位武当派小道士 道童(Daotong)|||    武当派真人 冲虚道长(Chongxu daozhang)|||>".split("|||")

    lines = args[2].decode("gb18030").encode("utf8").split("|||")
    p = re.compile(r"^\s*((一|二|三|四|五|六|七|八|九|十)(位|只))?(.*?)\((.*?)\)( <(.*?)>)?$")
    conn = sqlite3.connect('./tintin/myscripts/pkuxkx/gps/xkxmap.sqlite')
    cursor = conn.cursor()
    cursor.execute('delete from room_npc where rid=?',(rid,))
    for line in lines:
        if p.match(line):
            s = p.match(line)
            npc_num = 1
            if s.group(2) is not None:
                npc_num = to_numbers(s.group(2))
            ps = re.compile(r" |「|」")
            mylist = ps.split(s.group(4).strip(" "))
            npc_title1=""
            npc_title2=""
            npc_title3=""
            npc_cname=""
            ts = []
            for t in mylist:
                if t=="":
                    continue
                ts.append(t)
            if len(ts) == 4:
                npc_title1 = ts[0]
                npc_title2 = ts[1]
                npc_title3 = ts[2]
                npc_cname = ts[3]
            if len(ts) == 3:
                npc_title1 = ts[0]
                npc_title2 = ts[1]
                npc_cname = ts[2]
            if len(ts) == 2:
                npc_title1 = ts[0]
                npc_cname = ts[1]
            if len(ts) == 1:
                npc_cname = ts[0]

            npc_ename = s.group(5)
            npc_status = s.group(7)
            if npc_status is None:
                npc_status = "normal"
#            print(npc_num)
#            print(npc_title1.decode("utf8").encode("gb18030"))
#            print(npc_title2.decode("utf8").encode("gb18030"))
#            print(npc_title3.decode("utf8").encode("gb18030"))
#            print(npc_cname.decode("utf8").encode("gb18030"))
#            print(npc_ename.decode("utf8").encode("gb18030"))
#            print(npc_status.decode("utf8").encode("gb18030"))
            cursor.execute('insert into room_npc (rid,npc_num,npc_title1,npc_title2,npc_title3,npc_cname,npc_ename) values (?,?,?,?,?,?,?)',(rid,npc_num,npc_title1.decode("utf8"),npc_title2.decode("utf8"),npc_title3.decode("utf8"),npc_cname.decode("utf8"),npc_ename.decode("utf8")));
            conn.commit()
    conn.close()

if __name__=="__main__":
    main(sys.argv)


import os
import tempfile
from functools import partial

from jinja2 import Environment, FunctionLoader
from babel.dates import format_date, format_datetime
from babel.numbers import format_currency

try:
    import weasyprint
except ImportError:
    pass

from genshi.template import MarkupTemplate
from trytond.tools import file_open
from trytond.pool import Pool
from trytond.transaction import Transaction
from trytond.report import Report, TranslateFactory, Translator
from executor import execute

class ReportWebkit(Report):
    render_method = "webkit"

    @classmethod
    def render(cls, report, report_context):
        pool = Pool()
        Translation = pool.get('ir.translation')
        Company = pool.get('company.company')

        # Convert to str as buffer from DB is not supported by StringIO
        report_content = (str(report.report_content) if report.report_content
                          else False)
        if not report_content:
            raise Exception('Error', 'Missing report file!')

        translate = TranslateFactory(cls.__name__, Transaction().language,
                                     Translation)
        report_context['setLang'] = lambda language: translate.set_language(
            language)

        company_id = Transaction().context.get('company')
        report_context['company'] = Company(company_id)

        return cls.render_template(report_content, report_context, translate)

    @classmethod
    def convert(cls, report, data):
        # Convert the report to PDF if the output format is PDF
        # Do not convert when report is generated in tests, as it takes
        # time to convert to PDF due to which tests run longer.
        # Pool.test is True when running tests.
        output_format = report.extension or report.template_extension

        if output_format == "html" or Pool.test:
            return output_format, data
        elif cls.render_method == "webkit":
            return output_format, cls.wkhtml_to_pdf(data)
        elif cls.render_method == "weasyprint":
            return output_format, cls.weasyprint(data)

    @classmethod
    def render_template_genshi(cls, template_string, localcontext, translator):
        """
        Legacy genshi rendered for backward compatibility. If your report is
        still dependent on genshi, implement the method render_template in
        your custom report and call this method with the same arguments and
        return the value instead.
        """
        report_template = MarkupTemplate(template_string)

        # Since Genshi >= 0.6, Translator requires a function type
        report_template.filters.insert(
            0, Translator(lambda text: translator(text))
        )

        stream = report_template.generate(**localcontext)

        return stream.render('xhtml').encode('utf-8')

    @classmethod
    def jinja_loader_func(cls, name):
        """
        Return the template from the module directories using the logic
        below:

        The name is expected to be in the format:

            <module_name>/path/to/template

        for example, if the account_reports module had a base template in
        its reports folder, then you should be able to use:

            {% extends 'account_reports/report/base.html' %}
        """
        module, path = name.split('/', 1)
        try:
            with file_open(os.path.join(module, path)) as f:
                return f.read()
        except IOError:
            return None

    @classmethod
    def get_jinja_filters(cls):
        """
        Returns filters that are made available in the template context.
        By default, the following filters are available:

        * dateformat: Formats a date using babel
        * datetimeformat: Formats a datetime using babel
        * currencyformat: Formats the given number as currency
        * modulepath: Returns the absolute path of a file inside a
            tryton-module (e.g. sale/sale.css)

        For additional arguments that can be passed to these filters,
        refer to the Babel `Documentation
        <http://babel.edgewall.org/wiki/Documentation>`_.
        """

        def module_path(name):
            module, path = name.split('/', 1)
            with file_open(os.path.join(module, path)) as f:
                return 'file://' + f.name

        return {
            'dateformat': partial(format_date, locale=Transaction().language),
            'datetimeformat': partial(
                format_datetime, locale=Transaction().language
            ),
            'currencyformat': partial(
                format_currency, locale=Transaction().language
            ),
            'modulepath': module_path
        }

    @classmethod
    def get_environment(cls):
        """
        Create and return a jinja environment to render templates

        Downstream modules can override this method to easily make changes
        to environment
        """
        env = Environment(loader=FunctionLoader(cls.jinja_loader_func))
        env.filters.update(cls.get_jinja_filters())
        return env

    @classmethod
    def render_template(cls, template_string, localcontext, translator):
        """
        Render the template using Jinja2
        """
        env = cls.get_environment()

        # Update header and footer in context
        company = localcontext['company']
        localcontext.update({
            'header': env.from_string(company.header_html or ''),
            'footer': env.from_string(company.footer_html or ''),
        })
        report_template = env.from_string(template_string.decode('utf-8'))
        return report_template.render(**localcontext).encode('utf-8')

    @classmethod
    def weasyprint(cls, data, options=None):
        return weasyprint.HTML(string=data).write_pdf()

    @classmethod
    def wkhtml_to_pdf(cls, data, options=None):
        """
        Call wkhtmltopdf to convert the html to pdf
        """
        with tempfile.NamedTemporaryFile(
                suffix='.html', prefix='trytond_', delete=False
        ) as source_file:
            file_name = source_file.name
            source_file.write(data)
            source_file.close()

            # Evaluate argument to run with subprocess
            args = 'wkhtmltopdf'
            # Add Global Options
            if options:
                for option, value in options.items():
                    args += ' --%s' % option
                    if value:
                        args += ' "%s"' % value

            # Add source file name and output file name
            args += ' %s %s.pdf' % (file_name, file_name)
            # Execute the command using executor
            execute(args)
            return open(file_name + '.pdf').read()


def quick_sort(alist, start, end):
    if start >= end:
        return
    low = start
    high = end
    mid = alist[low]

    while low < high:
        while low < high and mid < alist[high]:
            # 从右边开始找，如果元素小于基准，则把这个元素放到左边
            high -= 1
        alist[low] = alist[high]

        while low < high and mid > alist[low]:
            # 从左边开始找，如果元素大于基准，则把元素放到右边
            low += 1
        alist[high] = alist[low]

    # 循环退出，low==high，把基准元素放到这个位置
    alist[low] = mid

    # 递归调用，重新排列左边的和右边的序列
    quick_sort(alist, start, low - 1)
    quick_sort(alist, low + 1, end)

l = [3,2,56,3,41,5,6,1]
quick_sort(l, 8, 8)
print(l)

def bubble_sort(alist):
    length = len(alist)
    for i in range(length - 1):
        # i表示比较多少轮
        for j in range(length - i - 1):
            # j表示每轮比较的元素的范围，因为每比较一轮就会排序好一个元素的位置，
            # 所以在下一轮比较的时候就少比较了一个元素，所以要减去i
            if alist[j] > alist[j + 1]:
                alist[j], alist[j + 1] = alist[j + 1], alist[j]


import os
import glob
import PIL.Image as I

def load_cityscapes_sem_seg_dict(image_dir, gt_dir):
    """
    Args:
        image_dir (str): path to the raw dataset. e.g., "~/cityscapes/leftImg8bit/train".
        gt_dir (str): path to the raw annotations. e.g., "~/cityscapes/gtFine/train".
    Returns:
        list[dict]: a list of dict, each has "file_name" and
            "sem_seg_file_name".
    """
    ret = []
    for image_file in glob.glob(os.path.join(image_dir, "**/*.png")):
        suffix = "leftImg8bit.png"
        assert image_file.endswith(suffix)
        prefix = image_dir

        label_file = gt_dir + image_file[len(prefix): -len(suffix)] + "gtFine_labelTrainIds.png"
        assert os.path.isfile(
            label_file
        ), "Please generate labelTrainIds.png with cityscapesscripts/preparation/createTrainIdLabelImgs.py"

        w, h = I.open(label_file).size
        ret.append(
            {
                'file_name': image_file,
                'sem_seg_file_name': label_file,
                'height': h,
                'width': w
            }
        )
    return ret


import threading
import socket
import pickle
import time
import math

class SimpleStreamer:

    def __init__(self, port=5202, autostart=True):
        self.port = port
        self.clients = {}
        self.subscriptions = {}
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.subscription_update_thread = threading.Thread(target=self._subscription_update_thread)
        self.data_receive_thread = threading.Thread(target=self._data_receive_thread)
        self._stop = False
        self.data = {}
        self.data_lock = threading.Lock()
        self.last_chunked_packet_id = 0
        if autostart:
            self.start()

    def start(self):
        self.data_receive_thread.start()
        self.subscription_update_thread.start()

    def _data_receive_thread(self):
        self.socket.bind(("0.0.0.0", self.port))
        buffer_pools = {}
        while not self._stop:
            str_dat, client_id = self.socket.recvfrom(4096)
            try:
                dict_data = pickle.loads(str_dat)
            except EOFError as e:
                print(e)
                continue

            # Chunked packet received
            if dict_data['type'] == "chunk":
                if client_id not in buffer_pools:
                    buffer_pools[client_id] = {"id": -1}
                if dict_data["id"] > buffer_pools[client_id]["id"]:
                    buffer_pools[client_id]["id"] = dict_data["id"]
                    buffer_pools[client_id]["chunks"] = {}
                    buffer_pools[client_id]["count"] = dict_data["count"]
                buffer_pools[client_id]["chunks"][dict_data["cid"]] = dict_data["data"]
                if len(buffer_pools[client_id]["chunks"]) >= buffer_pools[client_id]["count"]:
                    str_dat = b"".join(buffer_pools[client_id]["chunks"][i] for i in range(buffer_pools[client_id]["count"]))
                    try:
                        dict_data = pickle.loads(str_dat)
                    except EOFError as e:
                        print(e)
                        continue

            # Subscription update received
            if dict_data['type'] == "sub":
                dict_data = dict_data['data']

                if client_id not in self.clients:
                    self.clients[client_id] = {"last_send": 0}
                    print("new client: {}".format(client_id))
                dict_data['last_beat'] = time.time()
                self.clients[client_id].update(dict_data)

            # Data update received
            elif dict_data["type"] == "data":
                with self.data_lock:
                    self.data[dict_data["id"]] = dict_data["data"]

    def _subscription_update_thread(self):
        while not self._stop:
            for sub_id in self.subscriptions:
                data = {"type": "sub", "data": self.subscriptions[sub_id]}
                self.socket.sendto(pickle.dumps(data), sub_id)
            time.sleep(1.5)

    def subscribe(self, address, port, id, updates_per_sec=10):
        self.subscriptions[(address, port)] = {'id': id, 'ups': updates_per_sec}
        self.data[id] = {}

    def send_data(self, data):
        current_time = time.time()
        dead_clients = []
        for client_id in self.clients:
            if (current_time - self.clients[client_id]['last_beat']) > 2:
                dead_clients.append(client_id)
                continue
            if (current_time - self.clients[client_id]['last_send']) > 1/self.clients[client_id]['ups']:
                client_data = {"type": "data", "id": self.clients[client_id]['id'], 'data': data}
                self._send_dict(client_data, client_id)
                self.clients[client_id]['last_send'] = current_time
        for client_id in dead_clients:
            print("client lost: {}".format(client_id))
            del self.clients[client_id]

    def _send_dict(self, dict, addr_info):
        dict_pkl = pickle.dumps(dict)
        if len(dict_pkl) > 500:
            self.last_chunked_packet_id += 1
            pid = self.last_chunked_packet_id
            count = math.ceil(len(dict_pkl)/300)
            for i in range(count):
                chunk = {"type": "chunk", "id": pid, "cid": i, "count": count, "data": dict_pkl[i*300:(i+1)*300]}
                self._send_dict(chunk, addr_info)
        else:
            self.socket.sendto(dict_pkl, addr_info)

    def get_data(self, id=None):
        with self.data_lock:
            if id is None:
                return self.data
            else:
                return self.data[id]

    def stop(self):
        self._stop = True
        self.data_receive_thread.join()
        self.subscription_update_thread.join()


from django.contrib.auth.backends import RemoteUserBackend
from django.core.exceptions import ImproperlyConfigured
from django.conf import settings
from django.contrib.auth import get_user_model

class GroupEnabledRemoteUserBackend(RemoteUserBackend):
    def authenticate(self, user, remote_groups):
        user.remote_groups = remote_groups
        user.is_superuser = self._is_superuser(user)
        user.is_staff = self._is_staff(user)
        return user

    def _is_superuser(self, user):
        superuser_groups = getattr(settings, 'CUMULUS_SUPERUSER_GROUPS', [])
        superusers = getattr(settings, 'CUMULUS_SUPERUSERS', [])
        result = user.username in superusers or \
            set(user.remote_groups).intersection(set(superuser_groups))
        return bool(result)

    def _is_staff(self, user):
        staff_groups = getattr(settings, 'CUMULUS_STAFF_GROUPS', [])
        staff_users = getattr(settings, 'CUMULUS_STAFF_USERS', [])
        result = user.username in staff_users or \
            set(user.remote_groups).intersection(set(staff_groups))
        return bool(result)


#!/usr/bin/env python3
import cv2
import numpy as np
import dlib
import Facerecognize as fr

font=cv2.FONT_ITALIC
cap=cv2.VideoCapture(0)
detector=dlib.get_frontal_face_detector()
face_data,face_id,dic=fr.training_dataset_and_labels()
recognizer=cv2.face.LBPHFaceRecognizer_create()
recognizer.train(face_data,np.array(face_id))
while cap.isOpened():
    status,frame=cap.read()
    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces=detector(gray)
    for face in faces:
        x1=face.left()
        y1=face.top()
        x2=face.right()
        y2=face.bottom()
        cv2.rectangle(frame,(x1,y1),(x2,y2),(0,255,0),2)
        value,confidence=recognizer.predict(gray[y1:y2,x1:x2])
        text=dic[str(value)]
        if confidence < 100:
            cv2.putText(frame,text,(x1,y1),font,2,(255,0,0),4)
        #print(value)
        #print(confidence)
    cv2.imshow("live",frame)
    if cv2.waitKey(30) & 0xff==ord('q'):
        break
cv2.destroyAllWindows()
cap.release()


# Import required library
import xml.etree.ElementTree as xml
x = "TestID"
y = "TestZugang"
z = "TestDatum"

def createXML(filename):
    # Start with the root element
    root = xml.Element("groot")
    users = xml.Element("users")
    root.append(users)
    id = xml.SubElement(users, "id")
    id.text = x
    zugang = xml.SubElement(id, "zugang")
    zugang.text = y
    am = xml.SubElement(id, "am")
    am.text = z

    tree = xml.ElementTree(root)
    with open(filename, "wb") as fh:
        tree.write(fh)

if __name__ == "__main__":
    createXML("/home/pi/config_dateien/univers.xml")

trash or not used/testfiles_xml/univers.py


# -*- coding: utf-8 -*-
__all__ = ('random_policy', 'stdin_policy', 'imitation_policy', 'inference_policy')

from utils.distribution import sample_by_mask
from utils.match_data import MatchData
from utils.tile_traits import str2tile, tile2str
from torch import nn
import numpy as np

class random_policy:
    def __init__(self):
        pass

    def __call__(self, obs):
        return sample_by_mask(obs[1])

class stdin_policy:
    def __init__(self, env):
        self.env = env

    def __call__(self, obs):
        assert self.env.verbose, "To use stdin policy, you must set env to be verbose."
        self.env.render()
        if obs[1].astype(np.uint8).sum() == 1:
            return obs[1].nonzero()[0]
        return input('> ')

class inference_policy:
    def __init__(self, match_lines):
        self.inferred = [[], [], [], []]
        self.match_lines = match_lines[6:-2]
        self.idx = 0
        self.match_id = match_lines[0].split()[-1]
        self.quan = int(match_lines[1].split()[-1])
        self.tile_wall = [[], [], [], []]
        self.fan_info = match_lines[-2]
        self.scores = list(map(int, match_lines[-1].split()[1:]))
        self.translate_fn = None
        self.last = None
        self.count = 0
        for i in range(4):
            self.tile_wall[i] = match_lines[2+i].split()[3:]
        no_ignored = []
        for idx, line in enumerate(self.match_lines):
            lst = line.split()
            if lst[2] == "Draw":
                self.tile_wall[int(lst[1])].append(lst[3])
            elif lst[2] == "Chi":
                lst[2] = "rChi"
            if 'Ignore' in lst:
                subset = [' '.join(lst[:lst.index('Ignore')])]
                while 'Ignore' in lst:
                    j = lst.index('Ignore')
                    subset.append(' '.join(lst[j+1:j+4] + [lst[3]]))
                    lst = lst[:j] + lst[j+4:]
                subset.sort(key=lambda x:int(x[len('Player x') - 1]))
                no_ignored += subset
            else:
                no_ignored.append(' '.join(lst))
        self.match_lines = no_ignored + ['PLAY 4 PASS PASS']
        for i in range(4):
            self.tile_wall[i] = self.tile_wall[i] + ['??'] * (34 - len(self.tile_wall[i]))
        assert sum([len(self.tile_wall[i]) == 34 for i in range(4)]) == 4

    def __call__(self, obs):
        players = obs[2].reshape(-1)
        mask = obs[1].reshape(players.shape[0], -1)
        self.count += (mask.sum(1) > 1).sum() * 4 * 9 * 145 / 1024 / 1024
        lst = self.match_lines[self.idx].split()
        while lst[2] == 'Draw':
            self.last = None
            self.idx += 1
            lst = self.match_lines[self.idx].split()
        next_player = int(lst[1])
        next_last_tile = lst[3] if lst[2] == 'Play' else None
        for idx, player in enumerate(players):
            if player == next_player:
                action = self.valid_to_do(mask[idx], lst[2], lst[3])
                if action is not None:
                    self.inferred[player].append(action)
                    self.idx += 1
                    lst = self.match_lines[self.idx].split()
                    next_player = int(lst[1])
                    continue
            self.inferred[player].append(self.translate_fn('PASS'))
        if next_last_tile:
            self.last = next_last_tile
        if players.shape[0] == 1:
            return self.inferred[players[0]][-1]
        return np.asarray([self.inferred[player][-1] for player in players])

    def valid_to_do(self, mask, action, tile):
        action = action.upper()
        if action in ['DRAW', 'PASS']:
            return None
        if action == 'CHI':
            if tile[1] <= '7':
                try_action = self.translate_fn(action + ' ' + tile[0] + chr(ord(tile[1]) + 1), self.last)
                if mask[try_action]:
                    return try_action
            if '2' <= tile[1] <= '8':
                try_action = self.translate_fn(action + ' ' + tile, self.last)
                if mask[try_action]:
                    return try_action
            if '3' <= tile[1]:
                try_action = self.translate_fn(action + ' ' + tile[0] + chr(ord(tile[1]) - 1), self.last)
                if mask[try_action]:
                    return try_action
            return None
        if action == 'RCHI':
            action = 'CHI'
        if action in ['CHI', 'BUGANG', 'PLAY']:
            action += ' ' + tile
        elif action == 'GANG' and self.last is None:
            action += ' ' + tile

        try_action = self.translate_fn(action, self.last)
        if not mask[try_action]:
            return None
        return try_action

    def as_match_data(self, id=0):
        tile_wall = sum(map(lambda x:list(reversed(x)), self.tile_wall), [])
        tile_wall = np.asarray([34 if tile == '??' else str2tile[tile] for tile in tile_wall], dtype=np.uint8)
        return MatchData(
            match_id=self.match_id,
            id=id,
            quan=self.quan,
            tile_wall=tile_wall,
            actions=list(map(lambda x:np.asarray(x, dtype=np.uint8), self.inferred)),
            final_info=self.fan_info,
            scores=np.asarray(self.scores, dtype=np.int32)
        )

class imitation_policy:
    def __init__(self, match):
        self.actions = np.zeros((4, max([match.actions[i].shape[0] for i in range(4)])), dtype=np.uint8)
        for i in range(4):
            self.actions[i, :match.actions[i].shape[0]] = match.actions[i]
        self.idxs = np.asarray([0] * 4)

    def __call__(self, obs):
        action = self.actions[obs[2], self.idxs[obs[2]]]
        self.idxs[obs[2]] += 1
        return action


#18. Write a Python program to calculate the sum of three given numbers, if the values are equal then return three times of their sum.

#Python Program

def number(a,b,c):
    x = a + b + c

    if a == b == c :
        return x*3

print(number(11,12,13))
print(number(10,10,10))


from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from mlxtend.data import loadlocal_mnist
from keras.models import Sequential
from keras.layers import Conv2D,Activation,MaxPooling2D
from keras.layers import Flatten,Dense
from keras.utils import np_utils
from keras.optimizers import SGD
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np

style.use("ggplot")

classes = ["0","1","2","3","4","5","6","7","8","9"]

X, y = loadlocal_mnist(
        images_path='/home/yash/Desktop/PyImageSearch/DNN_DigitRecognition/train-images.idx3-ubyte',
        labels_path='/home/yash/Desktop/PyImageSearch/DNN_DigitRecognition/train-labels.idx1-ubyte')

X = X.astype("float32")
X = X.reshape(-1,28,28,1)
X/=255.0 #normalizing
y = np_utils.to_categorical(y,10) #onehot encode

X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)

#shallow net

model = Sequential()
model.add(Conv2D(32,kernel_size=(3,3),padding="same",input_shape=X_train[0].shape))
model.add(Activation("relu"))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation("relu"))
model.add(Dense(10))
model.add(Activation("softmax"))

sgd = SGD(lr=0.01,momentum=0.9,decay=1e-6,nesterov=True)

model.compile(optimizer=sgd,loss="categorical_crossentropy",metrics=["accuracy"])
print("[INFO] Model Training...")

hist = model.fit(X_train,y_train,batch_size=32, epochs=6,validation_data=(X_test,y_test),verbose=1)
model.save("shallowNET_Digits.h5")

predictions = model.predict(X_test,batch_size=32)
print(classification_report(y_test.argmax(axis=1),predictions.argmax(axis=1),target_names=classes))

train_loss = hist.history["loss"]
train_acc = hist.history["accuracy"]
val_loss = hist.history["val_loss"]
val_acc = hist.history["val_accuracy"]

plt.figure()
plt.plot(np.arange(0,6),train_loss,label="Train_Loss")
plt.plot(np.arange(0,6),val_loss,label="Val_Loss")
plt.plot(np.arange(0,6),train_acc,label="Train_Acc")
plt.plot(np.arange(0,6),val_acc,label="Val_Acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch#")
plt.ylabel("Loss/Accuracy")
plt.legend()
plt.show()


"""
МОДУЛЬ 3
Программа "Личный счет"
Описание работы программы:
Пользователь запускает программу у него на счету 0
Программа предлагает следующие варианты действий
1. пополнить счет
2. покупка
3. история покупок
4. выход

1. пополнение счета
при выборе этого пункта пользователю предлагается ввести сумму на сколько пополнить счет
после того как пользователь вводит сумму она добавляется к счету
снова попадаем в основное меню

2. покупка
при выборе этого пункта пользователю предлагается ввести сумму покупки
если она больше количества денег на счете, то сообщаем что денег не хватает и переходим в основное меню
если денег достаточно предлагаем пользователю ввести название покупки, например (еда)
снимаем деньги со счета
сохраняем покупку в историю
выходим в основное меню

3. история покупок
выводим историю покупок пользователя (название и сумму)
возвращаемся в основное меню

4. выход
выход из программы

При выполнении задания можно пользоваться любыми средствами

Для реализации основного меню можно использовать пример ниже или написать свой
"""

# функция добавления денег на счет
def add_money():
    add_sum = float(input('На сколько пополним счет?\n'))
    return add_sum

# функция покупки. Проверяем остаток на счете и добавляем покупку в словарь
# на вход принимаются остаток на счете и переменная для сохранения истории покупок
def check_purchase(account_sum, purchases={}):
    sum_purchase = float(input('Сколько потратим?\n'))
    if account_sum < sum_purchase:
        print('Недостаточно денег на счете!')
    else:
        # уменьшаем сумму на счете, сохраняем покупку в словарь
        account_sum -= sum_purchase
        purchase_name = input('Введите категорию покупки:\n')
        # добавляем покупку в словарь в виде покупка-сумма
        purchases[purchase_name] = sum_purchase

    return purchases

# вывод истории покупок - пары ключ-значение из словаря
def purchase_history(purchases):
    print('Покупок:')
    for key, value in purchases.items():
        print(f'{key} на сумму {value}')

# баланс счета на старте программы
account_sum = 0
while True:
    print('1. пополнение счета')
    print('2. покупка')
    print('3. история покупок')
    print('4. выход')

    choice = input('Выберите пункт меню: ')
    if choice == '1':
        add_summ = add_money()
        account_sum += add_summ # пополняем общую сумму нашего счета в границах программы
    elif choice == '2':
        purchases = check_purchase(account_sum)
    elif choice == '3':
        purchase_history(purchases)
    elif choice == '4':
        break
    else:
        print('Неверный пункт меню')


from flask import Flask, flash, redirect,\
    url_for, session, g

from functools import wraps
from database import connect_db, query_db

from tigergrader.config import cfg

app = Flask(__name__)
app.config.update(cfg)

class GraderConfiguration():

    def __setitem__(self, k, v):
        g.db.execute('replace into configuration values (?, ?)', (k, v))
        g.db.commit()

    def __getitem__(self, k):
        v = query_db('select value from configuration where key == ?', [k])
        if v and "value" in v[0]:
            return v[0]["value"]

@app.before_request
def before_request():
    g.db = connect_db()

@app.teardown_request
def teardown_request(exception):
    if hasattr(g, 'db'):
        g.db.close()

def login_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if "username" not in session:
            flash('You need to log in')
            return redirect(url_for('login'))

        elif session["username"] == app.config["ADMIN_USERNAME"]:
            return redirect(url_for('admin'))

        return f(*args, **kwargs)
    return decorated_function

def get_active_modules():
    conf = GraderConfiguration()
    active_modules = conf["active_modules"]
    if active_modules:
        active_modules = active_modules.split(",")
    else:
        active_modules = []
    return active_modules

import tigergrader.admin
import tigergrader.student


import uuid

from fastapi import status
from fastapi.testclient import TestClient
from httpx import Response

from internal.types import ScopedSession
from models.chat import Room

def test_POST_create_new_room_api_success_should_return_200(client: TestClient) -> None:
    response: Response = client.post("/rooms", json={"name": "New room", "creator_id": None})
    assert response.status_code == status.HTTP_200_OK

def test_GET_get_all_rooms_api_success_should_return_200(client: TestClient, db_session) -> None:
    for i in range(10):
        enc_key: str = uuid.uuid4().hex
        room = Room(name=f"Room {i}", slug=f"room-{i}", encryption_key=enc_key)
        db_session.add(room)
    db_session.commit()

    response: Response = client.get("/rooms")
    assert response.status_code == status.HTTP_200_OK
    assert len(response.json()) == 10

def test_POST_join_existing_room_api_success_should_return_200(
    client: TestClient, db_session: ScopedSession
) -> None:
    room = Room(name="Room", slug="room", encryption_key=uuid.uuid4().hex)
    db_session.add(room)
    db_session.commit()

    response: Response = client.post("/rooms/join", json={"name": room.name})
    assert response.status_code == status.HTTP_200_OK

def test_POST_join_existing_room_api_failed_should_return_404(
    client: TestClient, db_session: ScopedSession
) -> None:
    response: Response = client.post("/rooms/join", json={"name": "Some name"})
    assert response.status_code == status.HTTP_404_NOT_FOUND


# chat/consumers.py
import base64
import random

from asgiref.sync import async_to_sync
from channels.generic.websocket import AsyncWebsocketConsumer
from channels.db import database_sync_to_async
import json
import time
from face_machine_client.models import PassInfo, ClientInfo, SyncInfo
from face_irobot_main.models import FaceStore

import string
import random

def create_client_id(randomlength=15):
    """
    生成client_id
    """
    random_str = ''
    base_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789'
    length = len(base_str) - 1
    for i in range(randomlength):
        random_str += base_str[random.randint(0, length)]
    return random_str

class FaceWebsocket(AsyncWebsocketConsumer):
    """
    人脸后台websocket服务
    """

    def __init__(self, *args, **kwargs):
        # 初始化参数
        super().__init__(args, kwargs)
        self.client_user = None
        self.client_id = None
        self.company_id = None

    async def connect(self):
        # 初始化连接
        await self.accept()
        # 生成本次连接的client_id
        self.client_id = create_client_id()
        await self.send(text_data=json.dumps(
            {
                'type': 'init',
                'message': 'connected',
                'error_code': 0,
                'client_id': self.client_id  # 返还client_id,用于判断是否是本次连接
            }))

    async def disconnect(self, close_code):
        await self.send(text_data=json.dumps(
            {
                'message': 'disconnect'
            }))

    async def receive(self, text_data=None, bytes_data=None):
        # 处理websocket
        text_data_json = json.loads(text_data)
        message_type = text_data_json.get('type')
        # 登录进程
        if message_type == "authenticate":
            # 设备认证
            client_user = text_data_json.get('client_user')
            client_key = text_data_json.get('client_key')
            # 校验连接是否有效
            if text_data_json.get('client_id') == self.client_id:
                # 异步database
                results = await database_sync_to_async(auth_client_token)(client_user, client_key)
                # 登录成功
                if results[0] == 'ok':
                    self.company_id = results[2]
                    self.client_user = client_user
                    await self.send(text_data=json.dumps(
                        {
                            # 返还登录状态与设备参数
                            'message': 'auth_success',
                            'client_token': results[1],
                            'company_id': results[2]
                        }))
                    # 加入到相同企业通信通道中，用于主动下发参数
                    await self.channel_layer.group_add(
                        str(self.company_id),
                        self.channel_name
                    )
                    # 加入设备单独通信通道，用于点对点下发参数
                    await self.channel_layer.group_add(
                        str(client_user),
                        self.channel_name
                    )
                # 登录失败
                else:
                    await self.send(text_data=json.dumps(
                        {
                            'message': 'auth_fail'
                        }))
                    await self.close()
            else:
                await self.send(text_data=json.dumps(
                    {
                        'message': 'client_id_error'
                    }))
                await self.close()
        # 通行日志
        elif message_type == "pass_info":
            if self.company_id:
                info_detail = text_data_json.get("info_detail")
                task_id = text_data_json.get("task_id")
                update_results = await database_sync_to_async(update_pass_info)(info_detail_list=info_detail,
                                                                                task_id=task_id,
                                                                                client_user=self.client_user
                                                                                )
                await self.send(text_data=json.dumps(update_results))
            else:
                await self.send(text_data=json.dumps(
                    {
                        'message': 'need_auth'
                    }))
        # 同步人脸数据库
        elif message_type == "sync_database":
            if self.company_id:
                data_sync_type = text_data_json.get('sync_type')

                # 全同步模式
                if data_sync_type == "full_sync":
                    resp_data = await database_sync_to_async(get_face_store)(client_user=self.client_user,
                                                                             data_sync_type=data_sync_type,
                                                                             company_id=self.company_id)
                    await self.send(text_data=json.dumps(resp_data))

                # 检验同步模式,设备发送本地数据库人员列表进行数据量比对
                elif data_sync_type == "check_sync":
                    check_sync_list = text_data_json.get("check_sync_list")
                    resp_data = await database_sync_to_async(get_face_store)(client_user=self.client_user,
                                                                             data_sync_type=data_sync_type,
                                                                             company_id=self.company_id,
                                                                             check_sync_list=check_sync_list)
                    await self.send(text_data=json.dumps(resp_data))

                # 确认比对状态
                elif data_sync_type == "check_finished":
                    check_finished_list = text_data_json.get("check_finished_list")
                    resp_data = await database_sync_to_async(get_face_store)(client_user=self.client_user,
                                                                             data_sync_type=data_sync_type,
                                                                             company_id=self.company_id,
                                                                             check_sync_list=check_finished_list)
                    await self.send(text_data=json.dumps(resp_data))

                # 指定人员同步
                elif data_sync_type == "single_sync":
                    staff_id_list = text_data_json.get('staff_id_list')
                    resp_data = await database_sync_to_async(get_face_store)(data_sync_type=data_sync_type,
                                                                             staff_id_list=staff_id_list)
                    await self.send(text_data=json.dumps(resp_data))

            else:
                await self.send(text_data=json.dumps(
                    {
                        'message': 'need_auth'
                    }))
        # 同步状态
        elif message_type == "sync_info":
            if self.company_id:
                sync_state = text_data_json.get('sync_state')
                state = sync_state.get("state")
                face_data_len = sync_state.get("face_data_len")
                success_sync = sync_state.get("success_sync")
                # 判断同步类型
                if sync_state['type'] == "full_sync":
                    sync_type = "full_sync"
                elif sync_state['type'] == 'single_sync':
                    sync_type = "single"
                elif sync_state['type'] == 'check_sync':
                    sync_type = 'check_sync'
                    face_data_len = 0
                    success_sync = 0
                else:
                    sync_type = ''
                # 进行数据库同步
                await database_sync_to_async(update_sync_state)(sync_type=sync_type,
                                                                sync_state=state,
                                                                client_user=self.client_user,
                                                                face_data_len=face_data_len,
                                                                success_sync=success_sync)

            else:
                await self.send(text_data=json.dumps(
                    {
                        'message': 'need_auth'
                    }))

    # 向设备下发通知
    async def get_notification(self, event):
        message = event['message']
        # 异步下发
        await self.send(text_data=json.dumps({
            'message': message
        }))

def auth_client_token(client_user, client_key):
    """
    设备认证
    """
    # 在数据库中检索设备号与认证码
    auth = ClientInfo.objects.filter(client_user=client_user, client_key=client_key)
    # print('auth', auth)
    auth_state = auth.count()
    # print('auth_state', auth_state)
    if auth_state >= 1:
        # 生成设备唯一token
        auth.update(client_token=create_token())
        return 'ok', auth.get().client_token, auth.get().client_info.company_id
    else:
        return 'error', None

def create_token():
    """
    生成Token
    """
    token = 'time=' + str(int(time.time())) + 'num=' + str(random.randint(1000, 9999))
    token = base64.b64encode(token.encode('utf-8')).decode('utf-8')
    print('token', token)
    return token

def get_face_store(client_user, data_sync_type, company_id=None, staff_id_list=None, check_sync_list=None):
    """
    获取人脸数据库
    """
    if check_sync_list is None:
        check_sync_list = []
    resp_data = {
        'type': data_sync_type,
        'face_store': []
    }
    # 全同步方式
    if data_sync_type == "full_sync":
        full_store = FaceStore.objects.filter(company_id=company_id)
        for single_face in full_store:
            single_data = {'name': single_face.name,
                           'phone': single_face.phone,
                           'staff_id': single_face.staff_id,
                           'company_id': single_face.company_id,
                           'face_code': str(single_face.face_code)}
            resp_data['face_store'].append(single_data)
        print(len(resp_data['face_store']))
        return resp_data
    # 指定人员同步
    elif data_sync_type == "single_sync":
        for single_staff in staff_id_list:
            single_face = FaceStore.objects.filter(staff_id=single_staff).get()
            print('single_face', single_face)
            single_data = {'name': single_face.name,
                           'phone': single_face.phone,
                           'staff_id': single_face.staff_id,
                           'company_id': single_face.company_id,
                           'face_code': str(single_face.face_code)}
            resp_data['face_store'].append(single_data)
        print(len(resp_data['face_store']))
        return resp_data
    # 人员列表检查同步
    elif data_sync_type == "check_sync":
        local_staff_list = []
        for x in FaceStore.objects.filter(company_id=company_id):
            local_staff_list.append(x.staff_id)
        print('local_staff_list', local_staff_list)
        if check_sync_list:
            check_sync_list = check_sync_list
        else:
            check_sync_list = []
        update_list = []
        for check_staff in local_staff_list:
            if check_staff not in check_sync_list:
                update_list.append(check_staff)
        # 检测数据库改动(人脸更新,人脸删除)
        for k in FaceStore.objects.filter(company_id=company_id):
            local_client_list = eval(k.client_sync_list)
            print('local_client_list', local_client_list)
            if client_user in local_client_list:
                pass
            else:
                update_list.append(k.staff_id)
        print('update_list', update_list)
        resp_data['update_list'] = update_list
        return resp_data
    # 确认比对状态
    elif data_sync_type == "check_finished":
        try:
            for single_staff in check_sync_list:
                local_client_list = FaceStore.objects.filter(staff_id=single_staff).get().client_sync_list
                new_local_client_list = eval(local_client_list)
                # print('new_local_client_list', new_local_client_list)
                new_local_client_list.append(client_user)
                # print('new_local_client_list_return', new_local_client_list)
                # 设备号去重
                new_local_client_list = list(set(new_local_client_list))
                FaceStore.objects.filter(staff_id=single_staff).update(client_sync_list=str(new_local_client_list))
        except Exception as e:
            print(e)
        else:
            resp_data['update_state'] = "cool"
            return resp_data

def update_sync_state(sync_type, sync_state, client_user, face_data_len=0, success_sync=0):
    """
    更新同步状态
    """
    try:
        # 全同步
        if sync_type == "full_sync":
            SyncInfo.objects.create(sync_type=sync_type,
                                    sync_state=sync_state,
                                    client_user_id=client_user,
                                    face_data_len=face_data_len,
                                    success_sync=success_sync)
        # 人员列表检查同步
        elif sync_type == "check_sync":
            SyncInfo.objects.create(sync_type=sync_type,
                                    sync_state=sync_state,
                                    client_user_id=client_user)
    except Exception as e:
        print(e)
        return "sync_info_error"
    else:
        return 'ok'

def update_pass_info(info_detail_list, task_id, client_user):
    """
    更新通行日志
    """
    resp_data = {
        'task_id': task_id,
        'pass_update_state': ''
    }
    try:
        # 分条插入数据库中
        for single_info in info_detail_list:
            name = single_info.get('name')
            staff_id = single_info.get('staff_id')
            company_id = single_info.get('company_id')
            pass_time = single_info.get('pass_time')
            PassInfo.objects.create(name=name,
                                    staff_id=staff_id,
                                    company_id=company_id,
                                    pass_time=pass_time,
                                    client_user=client_user)
    except Exception as e:
        print(e)
        resp_data['pass_update_state'] = 'error'
        return resp_data
    else:
        resp_data['pass_update_state'] = 'success'
        return resp_data


# It take sorted list and assumes that they are strictly linear to run perfectly
# It druns really really fast if you took care of the division in the line equation and the data is fairly linear
# I guess this is the first algorithm which deserves file in the chat section

def interpolation_search(L, X):
    low = 0
    high = len(L)-1
    while low<=high:
        i = ((((X-L[low])*(high-low))//(L[high]-L[low]))+low)          #line equation of the "linear" data ... pay attention of that integer division
        if X == L[i]:
            return i
        elif X < L[i]:
            high = i-1
        elif X > L[i]:
            low = i+1
    return -1

L=[1,2,3,4,5,6,7,8,9,10,11,13,15,18]
print(interpolation_search(L,10))