import typer
from dotenv import load_dotenv
from halo import Halo
from langchain.text_splitter import TokenTextSplitter

from chatflock.backing_stores import InMemoryChatDataBackingStore
from chatflock.base import Chat
from chatflock.code import LocalCodeExecutor
from chatflock.code.langchain import CodeExecutionTool
from chatflock.conductors.round_robin import RoundRobinChatConductor
from chatflock.participants.langchain import LangChainBasedAIChatParticipant
from chatflock.participants.user import UserChatParticipant
from chatflock.renderers.terminal import TerminalChatRenderer
from chatflock.web_research import WebSearch
from chatflock.web_research.page_analyzer import OpenAIChatPageQueryAnalyzer
from chatflock.web_research.page_retrievers.selenium_retriever import SeleniumPageRetriever
from chatflock.web_research.search import GoogleSerperSearchResultsProvider
from chatflock.web_research.web_research import WebResearchTool
from examples.common import create_chat_model, get_max_context_size

def chatgpt_clone_with_additional_tools(
    model: str = "gpt-4-1106-preview",
    model_for_page_analysis: str = "gpt-3.5-turbo-1106",
    temperature: float = 0.0,
    temperature_for_page_analysis: float = 0.0,
) -> None:
    chat_model = create_chat_model(model=model, temperature=temperature)
    chat_model_for_page_analysis = create_chat_model(
        model=model_for_page_analysis, temperature=temperature_for_page_analysis
    )

    max_context_size_for_page_analysis = get_max_context_size(chat_model_for_page_analysis) or 12_000

    page_retriever = SeleniumPageRetriever()
    web_search = WebSearch(
        chat_model=chat_model,
        search_results_provider=GoogleSerperSearchResultsProvider(),
        page_query_analyzer=OpenAIChatPageQueryAnalyzer(
            chat_model=chat_model_for_page_analysis,
            # Should `pip install selenium webdriver_manager` to use this
            page_retriever=page_retriever,
            text_splitter=TokenTextSplitter(
                chunk_size=max_context_size_for_page_analysis, chunk_overlap=max_context_size_for_page_analysis // 5
            ),
            use_first_split_only=True,
        ),
    )

    spinner = Halo(spinner="dots")
    ai = LangChainBasedAIChatParticipant(
        name="Assistant",
        chat_model=chat_model,
        tools=[
            CodeExecutionTool(executor=LocalCodeExecutor(spinner=spinner), spinner=spinner),
            WebResearchTool(web_search=web_search, n_results=3, spinner=spinner),
        ],
        spinner=spinner,
    )

    user = UserChatParticipant(name="User")
    participants = [user, ai]

    chat = Chat(
        backing_store=InMemoryChatDataBackingStore(), renderer=TerminalChatRenderer(), initial_participants=participants
    )

    chat_conductor = RoundRobinChatConductor()
    chat_conductor.initiate_dialog(chat=chat)

    page_retriever.close()

if __name__ == "__main__":
    load_dotenv()

    typer.run(chatgpt_clone_with_additional_tools)


import os
import shutil
import datetime

found_num = 0
keys = []
is_copyfile = True
copy_path = ''

def dfs(path):
    global found_num
    try:
        files = os.listdir(path)
    except FileNotFoundError:
        return
    for file in files:
        if os.path.isfile(path + '\\' + file):
            for key in keys:
                if key in file:
                    found_num += 1
                    print(found_num, file)
                    if is_copyfile:
                        shutil.copyfile(path + '\\' + file, copy_path + '\\' + file)
                    break
        else:
            try:
                if os.path.isdir(path + '\\' + file):
                    dfs(path + '\\' + file)
            except PermissionError:
                pass

def search_by_extension_name():
    global keys
    global is_copyfile
    global copy_path
    print('Do not add \\ to the end of all paths!')
    path = input('Please input the start path:')
    keys = input('Please input the file\'s extension name(Use , separate):')
    keys = keys.split(',')
    is_copy = input('If you need to copy this found files to a path(y/n):')
    if is_copy == 'y' or is_copy == 'Y':
        is_copyfile = True
        copy_path = input('Please input the copy path:')
        while not os.path.isdir(copy_path):
            print('Wrong path! please input again.')
            copy_path = input('Please input the copy path:')
    starttime = datetime.datetime.now()
    dfs(path)
    endtime = datetime.datetime.now()
    print('>>>Finish! It spent', (endtime - starttime).seconds, 'sec.', found_num, 'files were found.')

if __name__ == '__main__':
    search_by_extension_name()


from tkinter import Canvas

from threading import Thread

from time import sleep

import scr.entity

class Player():
    def __init__(self, master=None, frame=None, instance=None, px=0, py=0):
        self.master = master
        self.frame = frame
        self.instance = instance
        self.px = px
        self.py = py

        self.thread = Thread()

        self.cooldown_start = False

        self.loadplayer()

        self.master.bind("<Up>",
                         self.up)
        self.master.bind("<Down>",
                         self.down)
        self.master.bind("<Right>",
                         self.right)
        self.master.bind("<Left>",
                         self.left)

    def loadplayer(self):
        self.chunk_x = int(self.chunkx())
        self.chunk_y = int(self.chunky())

        self.instance.loadchunk(self.chunk_x,
                                self.chunk_y)

        self.player = self.instance.map.create_rectangle((self.px%16+16)*10,
                                                         (self.py%16+16)*10,
                                                         (self.px%16+16)*10+10,
                                                         (self.py%16+16)*10+10,
                                                         fill="red",
                                                         width=0)

    def cooldown(func):
        def wrapper(*args, **kwargs):
            if args[0].cooldown_start == True:
                return
            else:
                args[0].cooldown_start = True
                args[0].thread._target = func
                args[0].thread._args = args
                args[0].thread._kwargs = kwargs
                args[0].thread.run()
                return args[0].thread
        return wrapper

    @cooldown
    def up(self, event):
        self.py -= 1
        if (self.px, self.py) in self.instance.hidbox:
            self.py += 1
            self.cooldown_start = False
            return
        if (1+self.py)%16 != 0:
            self.instance.map.move(self.player,
                                   0,
                                   -10)
            print (self.py , self.px)
        elif (1+self.py)%16 == 0:
            self.loadplayer()
            print (self.py , self.px)
        self.instance.map.update()
        self.thread._started.wait(0.5)
        self.cooldown_start = False
        return

    @cooldown
    def down(self, event):
        self.py += 1
        if (self.px, self.py) in self.instance.hidbox:
                self.py -= 1
                self.cooldown_start = False
                return
        if (self.py)%16 != 0:
            self.instance.map.move(self.player,
                                   0,
                                     10)
            print (self.py , self.px)
        elif (self.py)%16 == 0:
            self.loadplayer()
            print (self.py , self.px)
        self.instance.map.update()
        self.thread._started.wait(0.5)
        self.cooldown_start = False
        return

    @cooldown
    def right(self, event):
        self.px += 1
        if (self.px, self.py) in self.instance.hidbox:
                self.px -= 1
                self.cooldown_start = False
                return
        if (self.px)%16 != 0:
            self.instance.map.move(self.player,
                                   10,
                                   0)
            print (self.py , self.px)
        elif (self.px)%16 == 0:
            self.loadplayer()
            print (self.py , self.px)
        self.instance.map.update()
        self.thread._started.wait(0.5)
        self.cooldown_start = False
        return

    @cooldown
    def left(self, event):
        self.px -= 1
        if (self.px, self.py) in self.instance.hidbox:
                self.px += 1
                self.cooldown_start = False
                return
        if (1+self.px)%16 != 0:
            self.instance.map.move(self.player,
                                   -10,
                                   0)
            print (self.py , self.px)
        elif (1+self.px)%16 ==0:
            self.loadplayer()
            print (self.py , self.px)
        self.instance.map.update()
        self.thread._started.wait(0.5)
        self.cooldown_start = False
        return

    def chunkx(self):
        return (self.px-(self.px%16))/16

    def chunky(self):
        return (self.py-(self.py%16))/16


import numpy as np
from scipy.spatial.distance import pdist
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
sns.set(color_codes=True)

# Implement required formulae.
def corners(n):
    return 2**n

def diag(n):
    # Diagonal from [0, ..., 0] to [1, ..., 1]
    # with length sqrt(1^2 + ... + 1^2).
    return np.sqrt(n)

def volume_ball(n, radius):
    # Recursive formula from
    # https://en.wikipedia.org/wiki/Volume_of_an_n-ball#Recursions
    if n == 1: return 2 * radius
    if n == 2: return np.pi * radius**2
    return 2*np.pi/n * radius**2 * volume_ball(n-2, radius)

def volume_skin(n, alpha):
    return 1 - (1 - 2*alpha)**n

def pairwise_distances(n, k):
    points = np.random.uniform(0, 1, (k, n))
    return pdist(points).flatten()

# Generate required statistics.
data = [
    (n, corners(n), diag(n), volume_ball(n, 0.5), volume_skin(n, 0.01))
    for n in range(1, 1+100)
]
df = pd.DataFrame(data=data, columns=["n", "Corners", "Diag", "VolumeB", "VolumeS"]).set_index("n")
with open("dimensional_stats.md", "w") as f:
    f.write(df.to_markdown())

# Generate required plot.
fig, ax = plt.subplots()
for n in range(1, 1+10):
    distances = pairwise_distances(2**n, 1000)
    sns.distplot(distances, bins=100, label=str(2**n), ax=ax)
ax.set_xlabel("Distance")
ax.set_ylabel("Kernel density estimate")
plt.title("Pairwise distances of random points in the n-dimensional unit hypercube.")
plt.tight_layout()
plt.legend(loc="upper right", title="n")
plt.savefig("pairwise_dist.pdf")
plt.show()


# 12
import time

def focus_timer(minutes):
    seconds = minutes * 60
    end_time = time.time() + seconds

    print(f"Focus timer set for {minutes} minutes.")

    while time.time() < end_time:
        remaining_time = int(end_time - time.time())
        minutes = remaining_time // 60
        seconds = remaining_time % 60

        print(f"Time remaining: {minutes:02d}:{seconds:02d}", end="\r", flush=True)
        time.sleep(1)

    print("Focus timer completed.")

# 在这里设置你想要的专注时间（单位：分钟）
focus_time = 25

# 启动专注时钟
focus_timer(focus_time)


from django.contrib.auth import authenticate, login, get_user_model
from django.http import HttpResponse,JsonResponse
from django.shortcuts import render, redirect

from .forms import ContactForm
from products.models import Product

def home_page(request):
    context = {
        "title":"HELLO WORLD!",
        "content":" Welcome to the homepage",
    }
    if request.user.is_authenticated:
        context["premium_content"] = "YEAHHHHH"
    return render(request, "products/list.html", context)

def about_page(request):
    context = {
        "title": "ABOUT PAGE",
        "content":" Welcome to the aboutpage"
    }
    return render(request, "home_page.html", context)

def contact_page(request):
    contact_form = ContactForm(request.POST or None)
    context = {
         "title": "Contact",
         "content":" Welcome to the contactpage",
         "form": contact_form,

    }
    if contact_form.is_valid():
        print(contact_form.cleaned_data)
        if request.is_ajax():
            return JsonResponse({"message":"Thank you for your submission"})

    if contact_form.errors:
        errors = contact_form.errors.as_json()
        if request.is_ajax():
             return HttpResponse(errors, status=400, content_type='application/json')

    return render(request, "contact/view.html", context)

def home_page_old(request):
    html_ = """
    <!doctype html>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">

    <title>Hello, world!</title>

Hello, world!

    """
    return HttpResponse(html_)


list =(10,20,-2,9,-8,4,3,-7,23,8,-6,-11)
positive=0
negative=0
for i in range(len(list)):
 if(list(i)>0):
    positive=positive+1
 else:
    negative=negative+1
print(f"the positive numers are:{positive}")
print(f"the negative numers are:{negative}")
nextlist=()
for k in range(len(list)):
    if(k%2==1):
        if(list(k)>0):
            nextlist+=(k)
    else:
        if(list(k)<0):
            nextlist+=(k)
print(nextlist)


# STEPS FOR AMAZON HELP PAGE

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from behave import *
import time

# Scenario: User can search for solutions of Canceling an order on Amazon from Help page
@then('Locate "Search Help Library" field and search for "Cancel order"')
def locate_search_help_field(context):
    search_cancel_order = context.driver.find_element(By.ID,'helpsearch')
    search_cancel_order.clear()
    search_cancel_order.send_keys('Cancel order')
    search_cancel_order.send_keys(Keys.RETURN)

@then('Verify that "Cancel Items or Orders" text is displayed')
def verify_cancel_items_or_orders(context):
    expected_result = 'Cancel Items or Orders'
    actual_result = context.driver.find_element(By.XPATH,"//h1").text
    assert expected_result == actual_result, f'Test case failed! Actual text {actual_result} does not match expected {expected_result}'

# Scenario: Verify that Amazon Help page UI elements are present on the page
@then('Verify that Some things you can do here text result is displayed')
def verify_some_things(context):
    expected_result = 'Some things you can do here'
    actual_result = context.driver.find_element(By.CSS_SELECTOR,"div.a-section h2").text
    assert expected_result == actual_result, f'Test case failed! Actual text {actual_result} does not match expected {expected_result}'

@then('Verify that "Search the help library" text is displayed')
def search_help_library(context):
    expected_result = 'Search the help library'
    actual_result = context.driver.find_element(By.XPATH, "//label[@id='help-search-label']//span[contains(text(), 'Search the help library')]").text
    assert expected_result == actual_result, f'Test case failed! Actual text {actual_result} does not match expected {expected_result}'

@then('Verify that "Type something like, question about a charge" text result is displayed')
def type_smth_like(context):
    expected_result = 'Type something like, "question about a charge"'
    actual_result = context.driver.find_element(By.XPATH, "//label[@id='help-search-label']//span[contains(text(), 'Type something like')]").text
    assert expected_result == actual_result, f'Test case failed! Actual text {actual_result} does not match expected {expected_result}'

@then('Scroll down to "Browse Help Topics"')
def scroll_to_browse_help(context):
    element = context.driver.find_element(By.XPATH, "//*[contains(@class, 'help-content')]//*[contains(text(), 'Browse Help')]")
    context.driver.execute_script("arguments[0].scrollIntoView();", element)

@then('Verify that Browse Help Topics text is displayed')
def browse_help_topics(context):
    expected_result = 'Browse Help Topics'
    actual_result = context.driver.find_element(By.XPATH, "//*[contains(@class,'help-content')]//*[contains(text(),'Browse Help')]").text
    assert expected_result == actual_result, f'Test case failed! Actual text {actual_result} does not match expected {expected_result}'

@then('Verify that Where is My Stuff text is properly displayed')
def where_is_my_stuff(context):
    expected_result = "Where's My Stuff?"
    actual_result = context.driver.find_element(By.XPATH, "//*[contains(@rel, 'help-gateway-category') and contains(text(),'My Stuff?')]").text
    assert expected_result == actual_result, f'Test case failed! Actual text {actual_result} does not match expected {expected_result}'

@then('Verify that {find_text} text is displayed')
def find_text_1(context, find_text):
    expected_result = find_text
    actual_result = context.driver.find_element(By.XPATH, f"//*[contains(@class , 'a-column')]//*[ contains(text(), '{find_text}')]").text
    assert expected_result == actual_result, f'Test case failed! Actual text {actual_result} does not match expected {expected_result}'

@then('Verifying that "{find_text2}" text is displayed')
def find_text_2(context, find_text2):
    expected_result = find_text2
    actual_result = context.driver.find_element(By.XPATH, f"//*[contains(@rel, 'help-gateway-category') and contains(text(),'{find_text2}')]").text
    assert expected_result == actual_result, f'Test case failed! Actual text {actual_result} does not match expected {expected_result}'

@then('Verify that "Wrenches icon" is displayed')
def wrenches_icon(context):
    wrenches_pic = context.driver.find_element(By.CSS_SELECTOR, "#help-gateway-category-0 img.csg-hb-promo")


import os
import sys
from dataclasses import dataclass
sys.path.append(os.path.realpath('.'))

import pandas as pd

from sklearn.ensemble import (
    AdaBoostRegressor,
    GradientBoostingRegressor,
    RandomForestRegressor
)
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from xgboost  import XGBRegressor

from src.exception import CustomException
from src.logger import logging
from flaml import AutoML
from src.utils import save_object, evaluate_models

@dataclass
class ModelTrainerConfig:
    trained_model_file_path  = os.path.join("artifact", "_{}_model.pkl")

class ModelTrainer:
    def __init__(self):
        self.model_trainer_config = ModelTrainerConfig()
        self.cities = ['Mariposa', 'Ramona','Redding',
                          'Sanel','Santa-Barbara','Union-City']

    def initiate_model_trainer(self):
        try:
            scores = []
            for city in self.cities:

                logging.info(f"{city}:splitting training and val input data")

                train_df = pd.read_csv(f"artifact/{city}_train.csv")
                X_train = train_df.drop(columns = ['time','FWI']).values
                y_train = train_df['FWI'].values

                test_df = pd.read_csv(f"artifact/{city}_test.csv")
                X_test = test_df.drop(columns = ['time','FWI']).values
                y_test = test_df['FWI'].values
                automl = AutoML()
                # Specify automl goal and constraint
                automl_settings = {
                    "time_budget": 120,  # in seconds
                    "metric": 'mae',
                    "task": 'regression',
                }
                # Train with labeled input data
                automl.fit(X_train=X_train, y_train=y_train,
                        **automl_settings)

                # models = {
                #     "Random Forest": RandomForestRegressor(),
                #     "Decision Tree": DecisionTreeRegressor(),
                #     "Gradient Boosting": GradientBoostingRegressor(),
                #     "Linear Regression": LinearRegression(),
                #     "XGBRegressor": XGBRegressor(),
                #     "AdaBoost Regressor": AdaBoostRegressor(),
                # }

                # params={
                #     "Decision Tree": {
                #         'criterion':['squared_error', 'friedman_mse', 'absolute_error', 'poisson'],
                #         # 'splitter':['best','random'],
                #         # 'max_features':['sqrt','log2'],
                #     },
                #     "Random Forest":{
                #         # 'criterion':['squared_error', 'friedman_mse', 'absolute_error', 'poisson'],

                #         # 'max_features':['sqrt','log2',None],
                #         'n_estimators': [8,16,32,64,128,256]
                #     },
                #     "Gradient Boosting":{
                #         # 'loss':['squared_error', 'huber', 'absolute_error', 'quantile'],
                #         'learning_rate':[.1,.01,.05,.001],
                #         'subsample':[0.6,0.7,0.75,0.8,0.85,0.9],
                #         # 'criterion':['squared_error', 'friedman_mse'],
                #         # 'max_features':['auto','sqrt','log2'],
                #         'n_estimators': [8,16,32,64,128,256]
                #     },
                #     "Linear Regression":{},
                #     "XGBRegressor":{
                #         'learning_rate':[.1,.01,.05,.001],
                #         'n_estimators': [8,16,32,64,128,256]
                #     },
                #     "AdaBoost Regressor":{
                #         'learning_rate':[.1,.01,0.5,.001],
                #         # 'loss':['linear','square','exponential'],
                #         'n_estimators': [8,16,32,64,128,256]
                #     }

                # }

                # logging.info(f"{city}:Evaluating models")

                # model_report:dict = evaluate_models(X_train = X_train,
                #                                 y_train = y_train,
                #                                 X_test = X_test,
                #                                 y_test = y_test,
                #                                 models = models,
                #                                 params = params)
                # best_score = 0
                # best_model_name = ''
                # # getting best model from dictionary
                # for model in model_report:
                #     score = model_report[model]
                #     if score > best_score:
                #         best_model_name = model
                #         best_score = score

                best_model = automl.model#models[best_model_name]
                # if best_score < 0.6:
                #     raise CustomException("no best model found", sys)
                # logging.info(f"best model found {best_model_name}")

                save_object(
                    file_path=self.model_trainer_config.trained_model_file_path.format(city),
                    obj=best_model
                )

                predicted = best_model.predict(X_test)
                score = r2_score(y_test, predicted)
                scores.append(score)
            return score
        except Exception as e:
            raise CustomException(e,sys)

if __name__ == '__main__':
    di = ModelTrainer()
    di.initiate_model_trainer()


"""Gestion des "routes" FLASK et des données pour les films.
Fichier : gestion_films_crud.py
Auteur : OM 2022.04.11
"""
from pathlib import Path

from flask import redirect
from flask import request
from flask import session
from flask import url_for

from APP_PHARMACIE_164.database.database_tools import DBconnection
from APP_PHARMACIE_164.erreurs.exceptions import *
from APP_PHARMACIE_164.clients.gestion_clients_wtf_forms import FormWTFUpdateClient, FormWTFAddClient, FormWTFDeleteClient

"""Ajouter un film grâce au formulaire "client_add_wtf.html"
Auteur : OM 2022.04.11
Définition d'une "route" /film_add

Test : exemple: cliquer sur le menu "Films/Genres" puis cliquer sur le bouton "ADD" d'un "film"

Paramètres : sans

Remarque :  Dans le champ "nom_mail_update_wtf" du formulaire "films/films_update_wtf.html",
            le contrôle de la saisie s'effectue ici en Python dans le fichier ""
            On ne doit pas accepter un champ vide.
"""
@app.route("/clients_afficher/<int:id_client_sel>", methods=['GET', 'POST'])
def clients_afficher(id_client_sel):
    print(" clients_afficher id_client_sel ", id_client_sel)
    if request.method == "GET":
        try:
            with DBconnection() as mc_afficher:
                strsql_client_afficher_data = """SELECT id_client, nom, prenom, date_de_nais,nom_genre,nom_assu FROM t_client
                                            INNER JOIN t_genre ON t_genre.id_genre = t_client.fk_genre
                                            INNER JOIN t_assurance on t_assurance.id_assu = t_client.fk_assu"""
                if id_client_sel == 0:
                    # le paramètre 0 permet d'afficher tous les clients
                    # Sinon le paramètre représente la valeur de l'id du film
                    mc_afficher.execute(strsql_client_afficher_data)
                else:
                    # Constitution d'un dictionnaire pour associer l'id du film sélectionné avec un nom de variable
                    valeur_id_client_selected_dictionnaire = {"value_id_client_selected": id_client_sel}
                    # En MySql l'instruction HAVING fonctionne comme un WHERE... mais doit être associée à un GROUP BY
                    # L'opérateur += permet de concaténer une nouvelle valeur à la valeur de gauche préalablement définie.
                    strsql_client_afficher_data += """ HAVING id_client= %(value_id_client_selected)s"""

                    mc_afficher.execute(strsql_client_afficher_data, valeur_id_client_selected_dictionnaire)

                # Récupère les données de la requête.
                data_client_afficher = mc_afficher.fetchall()
                print("data_client ", data_client_afficher, " Type : ", type(data_client_afficher))

                # Différencier les messages.
                if not data_client_afficher and id_client_sel == 0:
                    flash("""La table "t_client" est vide. !""", "warning")
                elif not data_client_afficher and id_client_sel > 0:
                    # Si l'utilisateur change l'id_mail dans l'URL et qu'il ne correspond à aucun film
                    flash(f"Le client {id_client_sel} demandé n'existe pas !!", "warning")
                else:
                    flash(f"Données client affichés !!", "success")

        except Exception as Exception_client_afficher:
            raise ExceptionClientAfficher(f"fichier : {Path(__file__).name} ; "
                                          f"{clients_afficher.__name__} ;"
                                        f"{Exception_client_afficher}")

    print("clients_afficher  ", data_client_afficher)
    # Envoie la page "HTML" au serveur.
    return render_template("clients/clients_afficher.html", data=data_client_afficher)

@app.route("/client_add_wtf", methods=['GET', 'POST'])
def client_add_wtf():
    # Objet formulaire pour AJOUTER un client
    form = FormWTFAddClient()
    print(" on submit FormWTFAddClient ", form.submit.data, " req ", request.method)
    try:
        if request.method == "POST" and form.submit.data:
            # Récuperer les données du formulaire défini dans APP_PHARMACIE_164/clients/gestion_clients_wtf_forms.py
            nom_client = form.nom_client_wtf.data
            prenom_client = form.prenom_client_wtf.data
            date_nais_client = form.date_nais_client_wtf.data
            genre_selectionne = form.genres_dropdown_wtf.data
            assurance_selectionne = form.assu_dropdown_wtf.data

            valeurs_insertion_dictionnaire = {"value_nom_client": nom_client,
                                              "value_prenom_client": prenom_client,
                                              "value_date_client": date_nais_client,
                                              "value_genre_val_list_dropdown": genre_selectionne,
                                              "value_assu_val_list_dropdown": assurance_selectionne
                                              }
            print("valeurs_insertion_dictionnaire ", valeurs_insertion_dictionnaire)

            strsql_insert_client = """INSERT INTO t_client (id_client,nom,prenom,date_de_nais,fk_genre,fk_assu)
                                    VALUES (NULL,%(value_nom_client)s,%(value_prenom_client)s,%(value_date_client)s,%(value_genre_val_list_dropdown)s,%(value_assu_val_list_dropdown)s) """
            with DBconnection() as mconn_bd:
                mconn_bd.execute(strsql_insert_client, valeurs_insertion_dictionnaire)

            flash(f"Données insérées !!", "success")
            print(f"Données insérées !!")

            # Pour afficher et constater l'insertion du nouveau film (id_client_sel=0 => afficher tous les films)
            return redirect(url_for('clients_afficher', id_client_sel=0))

        if request.method == "GET":
            with DBconnection() as mc_afficher:
                strsql_genres_afficher = """SELECT id_genre, nom_genre FROM t_genre ORDER BY id_genre ASC"""
                mc_afficher.execute(strsql_genres_afficher)

            data_genres = mc_afficher.fetchall()
            print("demo_select_wtf data_genres ", data_genres, " Type : ", type(data_genres))

            """
                Préparer les valeurs pour la liste déroulante de l'objet "FormWTFAddClient"
                la liste déroulante est définie dans le "APP_PHARMACIE_164/clients/gestion_clients_wtf_forms.py"
                le formulaire qui utilise la liste déroulante "APP_PHARMACIE_164/templates/clients/client_add_wtf.html"
            """
            genre_val_list_dropdown = []
            for i in data_genres:
                genre_val_list_dropdown = [(i["id_genre"], i["nom_genre"]) for i in data_genres]

            print("genre_val_list_dropdown ", genre_val_list_dropdown)
            # Les valeurs sont chargées dans la liste déroulante
            form.genres_dropdown_wtf.choices = genre_val_list_dropdown

            with DBconnection() as mc_afficher:
                strsql_assu_afficher = """SELECT id_assu, nom_assu FROM t_assurance ORDER BY id_assu ASC"""
                mc_afficher.execute(strsql_assu_afficher)

            data_assu = mc_afficher.fetchall()
            print("demo_select_wtf data_assu ", data_assu, " Type : ", type(data_assu))

            """
                Préparer les valeurs pour la liste déroulante de l'objet "FormWTFAddClient"
                la liste déroulante est définie dans le "APP_PHARMACIE_164/clients/gestion_clients_wtf_forms.py"
                le formulaire qui utilise la liste déroulante "APP_PHARMACIE_164/templates/clients/client_add_wtf.html"
            """
            assu_val_list_dropdown = []
            for i in data_assu:
                assu_val_list_dropdown = [(i["id_assu"], i["nom_assu"]) for i in data_assu]

            print("assu_dropdown_wtf ", assu_val_list_dropdown)
            # Les valeurs sont chargées dans la liste déroulante
            form.assu_dropdown_wtf.choices = assu_val_list_dropdown

    except Exception as Exception_client_ajouter_wtf:
        raise ExceptionClientsAjouterWtf(f"fichier : {Path(__file__).name}  ;  "
                                        f"{client_add_wtf.__name__} ; "
                                        f"{Exception_client_ajouter_wtf}")

    return render_template("clients/client_add_wtf.html", form=form)

"""Editer(update) un film qui a été sélectionné dans le formulaire "clients_afficher.html"
Auteur : OM 2022.04.11
Définition d'une "route" /film_update

Test : exemple: cliquer sur le menu "Films/Genres" puis cliquer sur le bouton "EDIT" d'un "film"

Paramètres : sans

But : Editer(update) un genre qui a été sélectionné dans le formulaire "genres_afficher.html"

Remarque :  Dans le champ "nom_mail_update_wtf" du formulaire "clients/films_update_wtf.html",
            le contrôle de la saisie s'effectue ici en Python.
            On ne doit pas accepter un champ vide.
"""

@app.route("/client_update_wtf", methods=['GET', 'POST'])
def client_update_wtf():
    # L'utilisateur vient de cliquer sur le bouton "EDIT". Récupère la valeur de "id_client"
    id_client_update = request.values['id_client_btn_edit_html']

    # Objet formulaire pour l'UPDATE
    form_update_client = FormWTFUpdateClient()
    try:

        if request.method == "POST" and form_update_client.submit.data:
            # Récupèrer la valeur du champ depuis "genre_update_wtf.html" après avoir cliqué sur "SUBMIT".
            nom_client_update = form_update_client.nom_client_update_wtf.data
            prenom_client_update = form_update_client.prenom_client_update_wtf.data
            date_nais_client_update = form_update_client.date_nais_client_update_wtf.data
            genre_client_update = form_update_client.genres_dropdown_update_wtf.data
            fk_assu_client_update = form_update_client.assu_dropdown_update_wtf.data

            valeur_update_dictionnaire = {"value_id_client": id_client_update,
                                          "value_nom_client": nom_client_update,
                                          "value_prenom_client": prenom_client_update,
                                          "value_date_client": date_nais_client_update,
                                          "value_genre_update_val_list_dropdown": genre_client_update,
                                          "value_assurance_client": fk_assu_client_update
                                        }
            print("valeur_update_dictionnaire ", valeur_update_dictionnaire)

            str_sql_update_client = """UPDATE t_client SET nom = %(value_nom_client)s,
                                                            prenom = %(value_prenom_client)s,
                                                            date_de_nais = %(value_date_client)s,
                                                            fk_genre = %(value_genre_update_val_list_dropdown)s,
                                                            fk_assu = %(value_assurance_client)s
                                                            WHERE id_client = %(value_id_client)s"""
            with DBconnection() as mconn_bd:
                mconn_bd.execute(str_sql_update_client, valeur_update_dictionnaire)

            flash(f"Donnée mise à jour !!", "success")
            print(f"Donnée mise à jour !!")

            # afficher et constater que la donnée est mise à jour.
            # Afficher seulement le film modifié, "ASC" et l'"id_client_update"
            return redirect(url_for('clients_afficher', id_client_sel=id_client_update))
        elif request.method == "GET":
            # Opération sur la BD pour récupérer "id_mail" et "nom_genre" de la "t_genre"
            str_sql_id_mail = "SELECT * FROM t_client WHERE id_client = %(value_id_client)s"
            valeur_select_dictionnaire = {"value_id_client": id_client_update}
            with DBconnection() as mybd_conn:
                mybd_conn.execute(str_sql_id_mail, valeur_select_dictionnaire)
            # Une seule valeur est suffisante "fetchone()", vu qu'il n'y a qu'un seul champ "nom genre" pour l'UPDATE
            data_client = mybd_conn.fetchone()
            print("data_client ", data_client, " type ", type(data_client), " genre ",
                  data_client["nom"])

            # Afficher la valeur sélectionnée dans le champ du formulaire "client_update_wtf.html"
            form_update_client.nom_client_update_wtf.data = data_client["nom"]
            form_update_client.prenom_client_update_wtf.data = data_client["prenom"]
            form_update_client.date_nais_client_update_wtf.data = data_client["date_de_nais"]
            form_update_client.genres_dropdown_update_wtf.data = data_client["fk_genre"]
            form_update_client.assu_dropdown_update_wtf.data = data_client["fk_assu"]

        if request.method == "GET":
            with DBconnection() as mc_afficher:
                strsql_genres_afficher = """SELECT id_genre, nom_genre FROM t_genre ORDER BY id_genre ASC"""
                mc_afficher.execute(strsql_genres_afficher)

            data_genres = mc_afficher.fetchall()
            print("demo_select_wtf data_genres ", data_genres, " Type : ", type(data_genres))

            """
                Préparer les valeurs pour la liste déroulante de l'objet "FormWTFAddClient"
                la liste déroulante est définie dans le "APP_PHARMACIE_164/clients/gestion_clients_wtf_forms.py"
                le formulaire qui utilise la liste déroulante "APP_PHARMACIE_164/templates/clients/client_add_wtf.html"
            """
            genre_update_val_list_dropdown = []
            for i in data_genres:

                genre_update_val_list_dropdown = [(i["id_genre"], i["nom_genre"]) for i in data_genres]
            print("genre_update_val_list_dropdown ", genre_update_val_list_dropdown)
            # Les valeurs sont chargées dans la liste déroulante
            form_update_client.genres_dropdown_update_wtf.choices = genre_update_val_list_dropdown

            with DBconnection() as mc_afficher:
                strsql_assu_afficher = """SELECT id_assu, nom_assu FROM t_assurance ORDER BY id_assu ASC"""
                mc_afficher.execute(strsql_assu_afficher)

            data_assu = mc_afficher.fetchall()
            print("demo_select_wtf data_assu ", data_assu, " Type : ", type(data_assu))

            """
                Préparer les valeurs pour la liste déroulante de l'objet "FormWTFAddClient"
                la liste déroulante est définie dans le "APP_PHARMACIE_164/clients/gestion_clients_wtf_forms.py"
                le formulaire qui utilise la liste déroulante "APP_PHARMACIE_164/templates/clients/client_add_wtf.html"
            """
            assu_update_val_list_dropdown = []
            for i in data_assu:
                assu_update_val_list_dropdown = [(i["id_assu"], i["nom_assu"]) for i in data_assu]

            print("assu_update_val_list_dropdown ", assu_update_val_list_dropdown)
            # Les valeurs sont chargées dans la liste déroulante
            form_update_client.assu_dropdown_update_wtf.choices = assu_update_val_list_dropdown

    except Exception as ExceptionClient_Update_Wtf:
        raise ExceptionClientUpdateWtf(f"fichier : {Path(__file__).name}  ;  "
                                     f"{client_update_wtf.__name__} ; "
                                     f"{ExceptionClient_Update_Wtf}")

    return render_template("clients/client_update_wtf.html", form_update_client=form_update_client)

"""Effacer(delete) un film qui a été sélectionné dans le formulaire "clients_afficher.html"
Auteur : OM 2022.04.11
Définition d'une "route" /film_delete

Test : ex. cliquer sur le menu "film" puis cliquer sur le bouton "DELETE" d'un "film"

Paramètres : sans

Remarque :  Dans le champ "nom_film_delete_wtf" du formulaire "clients/client_delete_wtf.html"
            On doit simplement cliquer sur "DELETE"
"""

@app.route("/client_delete", methods=['GET', 'POST'])
def client_delete_wtf():
    # Pour afficher ou cacher les boutons "EFFACER"
    data_client_delete = None
    btn_submit_del = None
    # L'utilisateur vient de cliquer sur le bouton "DELETE". Récupère la valeur de "id_mail"
    id_client_delete = request.values['id_client_btn_delete_html']

    # Objet formulaire pour effacer le film sélectionné.
    form_delete_client = FormWTFDeleteClient()
    try:
        # Si on clique sur "ANNULER", afficher tous les films.
        if form_delete_client.submit_btn_annuler.data:
            return redirect(url_for("clients_afficher", id_client_sel=0))

        if form_delete_client.submit_btn_conf_del_film.data:
            # Récupère les données afin d'afficher à nouveau
            # le formulaire "clients/client_delete_wtf.html" lorsque le bouton "Etes-vous sur d'effacer ?" est cliqué.
            data_client_delete = session['data_client_delete']
            print("data_client_delete ", data_client_delete)

            flash(f"Effacer le client de façon définitive de la BD !!!", "danger")
            # L'utilisateur vient de cliquer sur le bouton de confirmation pour effacer...
            # On affiche le bouton "Effacer client" qui va irrémédiablement EFFACER le client
            btn_submit_del = True

        # L'utilisateur a vraiment décidé d'effacer.
        if form_delete_client.submit_btn_del_film.data:
            valeur_delete_dictionnaire = {"value_id_client": id_client_delete}
            print("valeur_delete_dictionnaire ", valeur_delete_dictionnaire)

            str_sql_delete_fk_traitement_client = """DELETE FROM t_pers_traitement WHERE fk_client = %(value_id_client)s"""
            str_sql_delete_fk_telephone_client = """DELETE FROM t_pers_telephone WHERE fk_client = %(value_id_client)s"""
            str_sql_delete_fk_mail_client = """DELETE FROM t_pers_mail WHERE fk_client = %(value_id_client)s"""
            str_sql_delete_fk_adresse_client = """DELETE FROM t_pers_adresse WHERE fk_client = %(value_id_client)s"""
            str_sql_delete_fk_achat_client = """DELETE FROM t_pers_achat_client WHERE fk_client = %(value_id_client)s"""
            str_sql_delete_client = """DELETE FROM t_client WHERE id_client = %(value_id_client)s"""
            # Manière brutale d'effacer d'abord la "fk_film", même si elle n'existe pas dans la "t_pers_mail"
            # Ensuite on peut effacer le film vu qu'il n'est plus "lié" (INNODB) dans la "t_pers_mail"
            with DBconnection() as mconn_bd:
                mconn_bd.execute(str_sql_delete_fk_traitement_client, valeur_delete_dictionnaire)
                mconn_bd.execute(str_sql_delete_fk_telephone_client, valeur_delete_dictionnaire)
                mconn_bd.execute(str_sql_delete_fk_mail_client, valeur_delete_dictionnaire)
                mconn_bd.execute(str_sql_delete_fk_achat_client, valeur_delete_dictionnaire)
                mconn_bd.execute(str_sql_delete_fk_adresse_client, valeur_delete_dictionnaire)
                mconn_bd.execute(str_sql_delete_client, valeur_delete_dictionnaire)

            flash(f"Client définitivement effacé !!", "success")
            print(f"Client définitivement effacé !!")

            # afficher les données
            return redirect(url_for('clients_afficher', id_client_sel=0))
        if request.method == "GET":
            valeur_select_dictionnaire = {"value_id_client": id_client_delete}
            print(id_client_delete, type(id_client_delete))

            # Requête qui affiche le film qui doit être efffacé.
            str_sql_client_delete = """SELECT * FROM t_client WHERE id_client = %(value_id_client)s"""

            with DBconnection() as mydb_conn:
                mydb_conn.execute(str_sql_client_delete, valeur_select_dictionnaire)
                data_client_delete = mydb_conn.fetchall()
                print("data_client_delete...", data_client_delete)

                # Nécessaire pour mémoriser les données afin d'afficher à nouveau
                # le formulaire "clients/client_delete_wtf.html" lorsque le bouton "Etes-vous sur d'effacer ?" est cliqué.
                session['data_client_delete'] = data_client_delete

            # Le bouton pour l'action "DELETE" dans le form. "client_delete_wtf.html" est caché.
            btn_submit_del = False

    except Exception as Exception_client_delete_wtf:
        raise ExceptionClientDeleteWtf(f"fichier : {Path(__file__).name}  ;  "
                                     f"{client_delete_wtf.__name__} ; "
                                     f"{Exception_client_delete_wtf}")

    return render_template("clients/client_delete_wtf.html",
                           form_delete_client=form_delete_client,
                           btn_submit_del=btn_submit_del,
                           data_client_del=data_client_delete
                           )


# date : 20/05/20
# There are two sorted arrays nums1 and nums2 of size m and n respectively.

# Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

# You may assume nums1 and nums2 cannot be both empty.

# Example 1:

# nums1 = [1, 3]
# nums2 = [2]

# The median is 2.0
# Example 2:

# nums1 = [1, 2]
# nums2 = [3, 4]

# The median is (2 + 3)/2 = 2.5

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        total = nums1 + nums2
        total.sort()
        k = len(total)
        if(k%2==0):
            return (total[int(k/2)]+total[int(k/2)-1])/2
        else:
            return total[floor(k/2)]

# Runtime: 92 ms, faster than 81.38% of Python3 online submissions for Median of Two Sorted Arrays.
# Memory Usage: 14.1 MB, less than 5.71% of Python3 online submissions for Median of Two Sorted Arrays.


from tkinter import *

janela = Tk()
janela.title("Conversor de Dólar")
janela.geometry("300x500")
janela.resizable(False, False)
janela.config(background='#4ed08d')

def converter():
    dolar = float(text_dolar.get())
    real = dolar * 5
    text_real.insert(0, real)

def limpar():
    text_dolar.delete(0, END)
    text_real.delete(0, END)

frame_dolar = Frame(janela, borderwidth=2, relief='solid')
label_dolar = Label(janela, text="Dólar")
text_dolar = Entry(frame_dolar, width=38)

frame_real = Frame(janela, borderwidth=2, relief='solid')
label_real = Label(janela, text="Real")
text_real = Entry(frame_real, width=38)
botao_converter = Button(janela, text="Converter", command=converter)
apagar = Button(janela, text="Limpar", command=limpar)

frame_dolar.place(x=23, y=165, width=250, height=40)
label_dolar.place(x=23, y=160)
text_dolar.place(x=5, y=10)

frame_real.place(x=23, y=235, width=250, height=40)
label_real.place(x=23, y=225)
text_real.place(x=5, y=10)

botao_converter.place(x=115, y=290)
apagar.place(x=120, y=320)

janela.mainloop()


import json
import logging

import requests

LOGGER = logging.getLogger(__name__)

class RundeckAPI:
    def __init__(
        self,
        api_version,
        auth_token,
        base_url,
        max_executions_for_request=1000,
    ):
        self.api_version = api_version
        self.auth_token = auth_token
        self.base_url = base_url
        self.max_executions_for_request = max_executions_for_request
        self.url = f'{self.base_url}/{self.api_version}/'

        self.headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'X-Rundeck-Auth-Token': self.auth_token,
        }

    def _get(self, url_path, params=None):
        return requests.get(
            f'{self.url}{url_path}', headers=self.headers, params=params,
        )

    def _delete(self, url_path):
        return requests.delete(f'{self.url}{url_path}', headers=self.headers)

    def _post(self, url_path, data):
        return requests.post(
            f'{self.url}{url_path}', data=data, headers=self.headers,
        )

    def get_project_names(self):
        response = self._get(url_path='projects')
        if response.status_code == 200:
            return [project['name'] for project in response.json()]

        LOGGER.error('No project created')
        return []

    def get_project(self, name):
        response = self._get(url_path=f'project/{name}/')
        if response.status_code == 200:
            return response.json()

        LOGGER.error('%s project does not exist', name)
        return ''

    def get_job_ids_for_project(self, project_name):
        params = {'project': project_name}
        response = self._get(url_path='jobs', params=params)

        if response.status_code == 200:
            return [job['id'] for job in response.json()]

        LOGGER.error('No jobs for the %s project found', project_name)
        return []

    def get_executions_for_job(self, job_id):
        response = self._get(url_path=f'job/{job_id}/executions/')

        if response.status_code == 200:
            executions = response.json()['executions']
            return [execution['id'] for execution in executions]

        LOGGER.error('No executions for the %s job found', job_id)
        return []

    def get_executions_for_project(self, project_name, filters=None):
        if filters:
            params = filters

        response = self._get(
            url_path=f'project/{project_name}/executions/', params=params,
        )
        if response.status_code == 200:
            executions = response.json()['executions']
            return [execution['id'] for execution in executions]

        LOGGER.error(
            'No executions for the %s project with the parameters %s found',
            project_name,
            filters,
        )
        return []

    def delete_execution(self, execution_id):
        response = self._delete(url_path=f'execution/{execution_id}')

        return response.status_code

    def bulk_delete_executions(self, execution_ids):
        response = self._post(
            url_path='executions/delete/',
            data=json.dumps({'ids': execution_ids}),
        )

        return response.status_code


# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy
from scrapy.loader.processors import MapCompose, TakeFirst, Join
from scrapy.loader import ItemLoader #拿到takefirst
import re
import datetime

# class ArticalsoiderItem(scrapy.Item):
#     # define the fields for your item here like:
#     # name = scrapy.Field()
#     title = scrapy.Field()
#     create_time = scrapy.Field()
#     front_image_url = scrapy.Field()
#     front_image_path = scrapy.Field()
#     praise_number = scrapy.Field()
#     colloct_number = scrapy.Field()
#     conmment_number = scrapy.Field()
#     content = scrapy.Field()
#     tag = scrapy.Field()
#     url_hashcode = scrapy.Field() #得到URL生成的哈希码

class Item_Takefirst(ItemLoader):#自定义拿到takefirst
    default_output_processor = TakeFirst()

def time_convert(value):
    #value = (re.match(".*(\d+)/(\d+)/(\d+).*?", value).group(1))
    value = value.replace("\r", "").replace("\n", "").replace("·", "").strip()
    try:
        value = datetime.datetime.strptime(value, "%Y/%m/%d").date()
    except Exception as e:
        value = datetime.datetime.now().date()
        #print(e)
    return value

def praise_convert(value):
    try:
        value = int(value)
    except:
        value = 0
    return value

def colloct_convert(value):
    try:
        value = int(re.match(".*(\d+).*?", value).group(1))
    except:
        value = 0
    return value

def comment_convert(value):
    try:
        value = (re.match(".*(\d+).*?", value).group(1))
    except:
        value = 0
    return value

def tag_convert(value):
    try:
        value = [element for element in value if not element.strip().endswith("评论")]  # 去掉评论这个标签 输出结果['职场', ' 7 评论 ', '程序员', '职场']
        # value = dict.fromkeys(value,1)
        # value = list(value.keys())
        # value = ",".join(value)
    except:
        value = "没有标签"
    return value

def return_value(value):#去掉takefirst效果
    return value

class ArticalsoiderItem(scrapy.Item):#重置item用于配置item_loder
    # define the fields for your item here like:
    # name = scrapy.Field()
    title = scrapy.Field()  #output_processor = TakeFirst()
    create_time = scrapy.Field(
        input_processor=MapCompose(time_convert)
    )
    front_image_url = scrapy.Field(
        out_processor=MapCompose(return_value)#image_url需要是一个list，所以要去掉str属性
    )
    front_image_path = scrapy.Field()
    praise_number = scrapy.Field(
        input_processor=MapCompose(praise_convert)
    )
    colloct_number = scrapy.Field(
        input_processor=MapCompose(colloct_convert)
    )
    conmment_number = scrapy.Field(
        input_processor=MapCompose(comment_convert)
    )
    content = scrapy.Field()
    tag = scrapy.Field(
        out_processor=MapCompose(tag_convert)#和前面的不一样，因为tag本身就是一个list所以在经过takefirst进行str以后就会出问题，所以现在要对tag进行还原成list，即重写out_processor
    )
    url_hashcode = scrapy.Field() #得到URL生成的哈希码


import unittest
import shutil
import os
import tempfile
import pandas as pd
import numpy as np
import numpy.testing as npt
import pandas.testing as pdt
import bio_test_artifacts.prebuilt as test_prebuilt
from inferelator.workflow import inferelator_workflow
from inferelator.utils import loader

class TestExpressionLoader(unittest.TestCase):

    def setUp(self):

        self.worker = inferelator_workflow()

    def test_tsv(self):

        file, data = test_prebuilt.counts_yeast_single_cell_chr01(filetype='tsv')
        data.to_csv(file, sep="\t")

        self.worker.set_expression_file(tsv=file)
        self.worker.read_expression()

        npt.assert_array_almost_equal(data.values, self.worker.data.expression_data)

    def test_h5ad(self):
        file, data = test_prebuilt.counts_yeast_single_cell_chr01(filetype='h5ad')

        self.worker.set_expression_file(h5ad=file)
        self.worker.read_expression()

        npt.assert_array_almost_equal(data.values, self.worker.data.expression_data)

    def test_hdf5(self):
        file, data = test_prebuilt.counts_yeast_single_cell_chr01(filetype='hdf5')

        self.worker.set_expression_file(hdf5=file)
        self.worker.read_expression()

        npt.assert_array_almost_equal(data.values, self.worker.data.expression_data)

    def test_mtx(self):
        (file1, file2, file3), data = test_prebuilt.counts_yeast_single_cell_chr01(filetype='mtx')

        self.worker.set_expression_file(mtx=file1, mtx_feature=file2, mtx_barcode=file3)
        self.worker.read_expression()

        npt.assert_array_almost_equal(data.values, self.worker.data.expression_data.A)

    def test_10x(self):
        (file1, file2, file3), data = test_prebuilt.counts_yeast_single_cell_chr01(filetype='mtx')

        with tempfile.TemporaryDirectory() as txdir:
            shutil.copy(file1, os.path.join(txdir, "matrix.mtx"))
            shutil.copy(file2, os.path.join(txdir, "genes,tsv"))
            shutil.copy(file3, os.path.join(txdir, "barcodes.tsv"))

            self.worker.set_expression_file(tenx_path=txdir)
            self.worker.read_expression()

            npt.assert_array_almost_equal(data.values, self.worker.data.expression_data.A)

    def test_10x_ranger3(self):
        (file1, file2, file3), data = test_prebuilt.counts_yeast_single_cell_chr01(filetype='mtx', gzip=True)

        with tempfile.TemporaryDirectory() as txdir:
            shutil.copy(file1, os.path.join(txdir, "matrix.mtx.gz"))
            shutil.copy(file2, os.path.join(txdir, "features.tsv.gz"))
            shutil.copy(file3, os.path.join(txdir, "barcodes.tsv.gz"))

            self.worker.set_expression_file(tenx_path=txdir)
            self.worker.read_expression()

            npt.assert_array_almost_equal(data.values, self.worker.data.expression_data.A)

    def test_df_decode(self):
        idx = pd.Index(['str1', b'str2', b'str3', 'str4', 5, 17.4, np.inf, ('str1',)])
        correct = pd.Index(['str1', 'str2', 'str3', 'str4', 5, 17.4, np.inf, ('str1',)])

        df1 = pd.DataFrame(idx.tolist(), index=idx)
        df1_c = pd.DataFrame(correct.tolist(), index=correct)

        loader._safe_dataframe_decoder(df1)
        pdt.assert_frame_equal(df1, df1_c)

        vals = np.random.rand(len(idx), len(idx))
        df2 = pd.DataFrame(vals, index=idx, columns=idx)
        df2_c = pd.DataFrame(vals, index=correct, columns=correct)

        loader._safe_dataframe_decoder(df2)

        pdt.assert_frame_equal(df2, df2_c)

        df3 = pd.DataFrame(pd.Categorical(idx.tolist()), index=idx)
        df3_c = pd.DataFrame(pd.Categorical(correct.tolist()), index=correct)

        loader._safe_dataframe_decoder(df3)

        pdt.assert_frame_equal(df3, df3_c)


a = int(input())
while(a):
    b,c,d,e=map(int,input().split())
    res = abs(b-d)+abs(c-e)
    if res:
        if res%2==0:
            print(2)
        else:
            print(1)
    else:
        print(0)
    a-=1


from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from .forms import PdfUploadForm
from .models import Category
import PyPDF2

# Create your views here.

# FBV
def pdf_extract(request):
    if request.method == 'POST':
        form = PdfUploadForm(request.POST, request.FILES)
        if form.is_valid():
            page_num = int(request.POST.get("page", 0))
            page_index = page_num - 1
            f = request.FILES['file']

            with open('original.pdf',"wb+") as pdfFileObj:
                for chunk in f.chunks():
                    pdfFileObj.write(chunk)
                pdfReader = PyPDF2.PdfFileReader(pdfFileObj)

    else:
        form = PdfUploadForm()
        return render(request, 'pdf/pdf_upload.html', {'form': form})

from rest_framework.response import Response

def category(request):
    if request.method == "GET":
        # root_category = Category.objects.get(name='Root Category')
        # child_categories = root_category.children.all()

        # 根据name获取他的所有父节点
        for item in Category.objects.filter(name="steverocket1.1.1").get_ancestors(include_self=True):
            print(item.name)

    elif request.method == "POST":
        root_category = Category.objects.create(name='Root Category')
        child_category1 = Category.objects.create(name='Child Category 1', parent=root_category)
        child_category2 = Category.objects.create(name='Child Category 2', parent=root_category)
    # return Response(data={})
    # return JsonResponse({"category": root_category})
    return JsonResponse({})

def whitenoise_server(request):
    # whitenoise 不支持template
    # from django.conf import settings
    # settings.STATIC_ROOT
    # return render(request, "index.html")
    pass

    # return render(request, "README.md")
    # https://gitee.com/SteveRocket/staticfiles/blob/master/staticfiles/index.html
    # return render(request, "https://gitee.com/SteveRocket/staticfiles/blob/master/staticfiles/index.html")


from time import sleep

from bs4 import BeautifulSoup

from Base.SmartCSV import smart_csv
from Base.grab import abstract_grab

AOC_MONITOR_HOST = 'http://b2b.aocmonitor.com.cn'

def get_item_info(index):
    uri = '%s/product/%s.html' % (AOC_MONITOR_HOST, index)

    html = abstract_grab(uri, phone_agent=False)

    soup = BeautifulSoup(html, 'html.parser')

    images = soup.find(id='J_Focus').find(class_='carousel-inner').find_all('img')
    image_list = []
    for image in images:
        image_list.append(image['src'])

    table = soup.find(class_='cpBox').find('table')
    items = table.find_all('tr', class_='proParaRow')
    result = dict()
    result['产品ID'] = index

    for item in items:
        key = item.find(class_='proParaName').text
        value = item.find(class_='proParaValue').text
        result[key] = value

    result['图片列表'] = image_list

    return result

def main():
    result_list = []
    for i in range(57):
        try_time = 3
        item_info = None
        while try_time:
            try:
                item_info = get_item_info(i + 1)
                break
            except Exception:
                sleep(3)
                try_time -= 1
                if try_time:
                    print(i + 1, '出错重爬')
                else:
                    print(i + 1, '停止爬取')
        if item_info:
            result_list.append(item_info)
    smart_csv('b2b.aocmonitor.csv', result_list)

main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

###
# © 2018 The Board of Trustees of the Leland Stanford Junior University
# Nathaniel Watson
# nathankw@stanford.edu
###

"""
Backports biosamples from the ENCODE Portal. Biosample identifiers on the ENCODE Portal can be
provided in one of three ways:

    1. An input file with one ID per line.
    2. On the command-line.
    3. Via an ENCODE Portal search URL.
"""

import argparse
import pdb

import encode_utils.connection as euc
import pulsarpy_to_encodedcc.backport_from_encode_portal.backport as backport

ENC_CONN = euc.Connection("prod")
# Note that ex_url below has a double '%', where the second is used to escape the original.
ex_url = "https://www.encodeproject.org/search/?type=Biosample&lab.title=Michael+Snyder%%2C+Stanford&award.rfa=ENCODE4&biosample_type=tissue"

def get_parser():
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument("-u", "--url", help="""
    A search URL (wrapped in quotation marks to avoid shell expansion, to indicate which biosamples to import, i.e. {}.""".format(ex_url))
    group.add_argument("-i", "--infile", help="File containing biosample record IDs, one per line.")
    group.add_argument("-r", "--rec-ids", nargs="+", help="One or more biosample record identifiers separated by a space.")
    return parser

def main():
    parser = get_parser()
    args = parser.parse_args()
    url = args.url
    infile = args.infile
    rec_ids = args.rec_ids
    inputs = []
    if infile:
        with open(infile) as fh:
            for line in fh:
                line = line.strip()
                if not line or line.startswith("#"):
                    continue
                inputs.append(line)
    elif rec_ids:
        inputs = rec_ids
    else:
        res = ENC_CONN.search(url=url)
        for record in res: # i is a JSON object.
            # GET the object. Only part of the object is given in the search results. For example,
            # the property 'genetic_modifications' isn't present.
            inputs.append(record["@id"])

    for i in inputs:
        print("Backporting Biosample {}".format(i))
        backport.biosample(rec_id=i)

if __name__ == "__main__":
    main()


__author__ = 'abhmul'
'''
Evaluate
'''
import numpy as np
from numpy.random import default_rng

import argparse

from commpy.utilities import dec2bitarray

from src.utils import get_test_sigmas, binlen
from src.turbo_benchmarker import TurboBenchmarker
import src.modified_convcode as mcc

parser = argparse.ArgumentParser()

parser.add_argument('--num_block', type=int, default=100)
parser.add_argument('--block_len', type=int, default=100)
parser.add_argument('--num_dec_iteration', type=int, default=6)

parser.add_argument('--g1',  type=int, default=31)     #,7,5,7 is the best
parser.add_argument('--g2',  type=int, default=23)
parser.add_argument('--g3',  type=int, default=30)

parser.add_argument('--b1',  type=int, default=1)     #,7,5,7 is the best
parser.add_argument('--b2',  type=int, default=0)
parser.add_argument('--b3',  type=int, default=1)

parser.add_argument('--rsc', action='store_true', help="Run the code as a recursive systematic code instead")

parser.add_argument('--batch_size', type=int, default=1)
parser.add_argument('--decoder', choices=['modified_hazzys', 'modified_basic', 'tensorflow_basic'], default='modfied_basic')
parser.add_argument('--num_cpu', type=int, default=1)

parser.add_argument('--snr_test_start', type=float, default=-1.5)
parser.add_argument('--snr_test_end', type=float, default=2.0)
parser.add_argument('--snr_points', type=int, default=8)

parser.add_argument('--save', action='store_true', help="Save results to file when complete")
parser.add_argument('--plot', action='store_true', help="Create a plot when complete")
parser.add_argument('--show_plot', action='store_true', help="Show plot when complete")

SEED = 2021

if __name__ == '__main__':
    args = parser.parse_args()
    print(args)

    memory = max(binlen(g) for g in [args.g1, args.g2, args.g3]) - 1
    gen1 = dec2bitarray(args.g1, memory + 1)
    bias1 = args.b1
    gen2 = dec2bitarray(args.g2, memory + 1)
    bias2 = args.b2
    gen3 = dec2bitarray(args.g3, memory + 1)
    bias3 = args.b3

    model_title = f'modified-turbo-{"rsc-" if args.rsc else ""}{args.g1}{args.g2}{args.g3}-{args.b1}{args.b2}{args.b3}'

    trellis1 = mcc.ModifiedTrellis(np.stack([gen1, gen2], axis=0), np.stack([bias1, bias2], axis=0), delay=0)
    trellis2 = mcc.ModifiedTrellis(np.stack([gen1, gen3], axis=0), np.stack([bias1, bias3], axis=0), delay=0)
    if args.rsc:
        trellis1 = trellis1.to_rsc()
        trellis2 = trellis2.to_rsc()

    print("trellis1")
    print(f"Trellis is code type {trellis1.code_type} and rate {trellis1.k}/{trellis1.n}")
    print(f"Trelis has generator {trellis1.generator} and bias {trellis1.bias}")
    print(f"Code has delay {trellis1.delay}")
    print("Output table")
    print(trellis1.output_table)
    print("Next State table")
    print(trellis1.next_state_table)

    print("trellis2")
    print(f"Trellis is code type {trellis2.code_type} and rate {trellis2.k}/{trellis2.n}")
    print(f"Trelis has generator {trellis2.generator} and bias {trellis2.bias}")
    print(f"Code has delay {trellis2.delay}")
    print("Output table")
    print(trellis2.output_table)
    print("Next State table")
    print(trellis2.next_state_table)

    snrs, test_sigmas = get_test_sigmas(
        args.snr_test_start, args.snr_test_end, args.snr_points)

    benchmarker = TurboBenchmarker(
        trellis1,
        trellis2,
        systematic=args.rsc,
        model_title=model_title,
        save_to_file=args.save,
        save_plot=args.plot,
        show_plot=args.show_plot,
        seed_generator=default_rng(SEED))
    benchmarker.benchmark(
        encoder_name="modified_basic",
        decoder_name=args.decoder,
        test_sigmas=test_sigmas,
        num_block=args.num_block,
        block_len=args.block_len,
        num_decoder_iterations=args.num_dec_iteration,
        num_cpu=args.num_cpu,
        batch_size=args.batch_size
    )


from pwn import p8, p16, p32, p64
import random

random.seed(116)

flag = "grey{r_y0u_d1zzy?_9bfad}"

OP_IDXS = [i for i in range(64)]
OP_IDXS_R = [i for i in range(64)]
OOUT_IDXS = [i for i in range(64)]
OOUT_IDXS_R = [i for i in range(64)]
RIN = [i for i in range(64)]
RIN_R = [i for i in range(64)]
ROUT = [i for i in range(64)]
ROUT_R = [i for i in range(64)]

S = [i for i in range(256)]
RCI = 0
RCJ = 0

def get_swap():
    global RCI, RCJ
    RCI = (RCI + 1) % 256
    RCJ = (RCJ + S[RCI]) % 256
    S[RCI], S[RCJ] = S[RCJ], S[RCI]
    return RCI % 64, RCJ % 64

def scramble_o():
    for _ in range(312):
        i, j = get_swap()
        OP_IDXS[OP_IDXS_R[i]] = j
        OP_IDXS[OP_IDXS_R[j]] = i
        OP_IDXS_R[i], OP_IDXS_R[j] = OP_IDXS_R[j], OP_IDXS_R[i]

def scramble_oout():
    for _ in range(456):
        i, j = get_swap()
        OOUT_IDXS[OOUT_IDXS_R[i]] = j
        OOUT_IDXS[OOUT_IDXS_R[j]] = i
        OOUT_IDXS_R[i], OOUT_IDXS_R[j] = OOUT_IDXS_R[j], OOUT_IDXS_R[i]

def scramble_rin():
    for _ in range(331):
        i, j = get_swap()
        RIN[RIN_R[i]] = j
        RIN[RIN_R[j]] = i
        RIN_R[i], RIN_R[j] = RIN_R[j], RIN_R[i]

def scramble_rout():
    for _ in range(599):
        i, j = get_swap()
        ROUT[ROUT_R[i]] = j
        ROUT[ROUT_R[j]] = i
        ROUT_R[i], ROUT_R[j] = ROUT_R[j], ROUT_R[i]

ADDS = [45124, 27787, 5443, 57762, 26426, 35976, 795, 56870, 6573, 53292, 35658, 9236, 29135, 15515, 10430, 16765, 1976, 47730, 27666, 52187, 46620, 59509, 25031]

def get_noise():
    seq = [i for i in range(len(ADDS))]
    i = random.choice(seq)
    return 9 + i, ADDS[i]

OPI_INPUT = 0
OPI_OUTPUT = 1
OPI_XOR = 4
OPI_JMP = 7
OPI_ADD = 8
OPI_ADD_R = 57
OPI_OR = 6
OPI_AND = 5
OPI_NOISE = 12
OPI_UNLOCK = 56

START_SCRAMBLE = False

def make_op(c0, c1, c2, c3, opi, r1, r2, imm):
    global START_SCRAMBLE

    if START_SCRAMBLE:
        c0 = random.randint(0, 1)
        c1 = random.randint(0, 1)
        c2 = random.randint(0, 1)
        c3 = random.randint(0, 1)

    op = [0] * 32

    op[0] = str(c0)
    op[1] = str(c1)
    op[2] = str(c2)
    op[3] = str(c3)

    op[4:10] = list(bin(opi)[2:].rjust(6, '0'))[::-1]
    op[10:16] = list(bin(r1)[2:].rjust(6, '0'))[::-1]
    op[16:22] = list(bin(r2)[2:].rjust(6, '0'))[::-1]
    op[22:32] = list(bin(imm)[2:].rjust(10, '0'))[::-1]

    if len(op) > 32:
        print("???", hex(imm))

    bop = int(''.join(op)[::-1], 2)

    if c0 == 1:
        scramble_o()
    if c1 == 1:
        scramble_oout()
    if c2 == 1:
        scramble_rin()
    if c3 == 1:
        scramble_rout()

    return bop

OPS = []
# OPS.append(make_op(1, 1, 1, 1, OPI_UNLOCK, 0, 0, 0))
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[1], RIN[0], 16))

# make message
msg = "Enter flag (exactly 24 chars): "
for i in range(len(msg)):
    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[8+i], RIN[0], ord(msg[i])))

# print message
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[1], RIN[0], 8))
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_OUTPUT]], ROUT[1], RIN[1], 0x0200+31))

# read input
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[1], RIN[0], 32))
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_INPUT]], ROUT[1], RIN[1], 0x0100+24))

# echo input
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[1], RIN[0], 32))
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_OUTPUT]], ROUT[1], RIN[1], 0x0200+24))

# scramble input
seq = [i for i in range(24)]
seq2 = [i for i in range(24)]
scrambled = [-1] * 24
random.shuffle(seq)
random.shuffle(seq2)
for i1, i2 in zip(seq, seq2):
    if i1 == 9:
        # unlock the nasties
        START_SCRAMBLE = True
        OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_UNLOCK]], ROUT[1], RIN[0], 0xcc))

    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[8+i1], RIN[32+i2], 0))
    scrambled[i2] = i1

# check scrambled
# OPS.append(make_op(0, 0, 0, 0, OP_IDXS[OPI_ADD], ROUT[1], RIN[0], 8))
# OPS.append(make_op(0, 0, 0, 0, OP_IDXS[OPI_OUTPUT], ROUT[1], RIN[1], 0x0200+24))

# accumulate verdict in R2
# ensure first char is g

LEFT = len(flag) - 1

OPS.append(make_op(1, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[3], RIN[8 + scrambled[0]], 0))
OPS.append(make_op(0, 1, 0, 0, OOUT_IDXS[OP_IDXS[OPI_XOR]], ROUT[2], RIN[3], ord('g')))
OPS.append(make_op(0, 0, 1, 0, OOUT_IDXS[OP_IDXS[OPI_JMP]], ROUT[1], RIN[2], 0x300 + 3))

# if first char is not g
OPS.append(make_op(0, 0, 0, 1, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[2], RIN[0], 1))
OPS.append(make_op(0, 0, 1, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[3], RIN[0], 0))
OPS.append(make_op(0, 1, 0, 0, OOUT_IDXS[OP_IDXS[OPI_JMP]], ROUT[1], RIN[3], 0x300 + 1))

OPS.append(make_op(0, 0, 0, 1, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[2], RIN[0], 0))

## make pairs
idxs = [i for i in range(1, len(flag))]
random.shuffle(idxs)

# add 2 random chars
# mix up with some random operations
curr_i = 0
for _ in range(len(idxs)):
    next_i = idxs.pop()

    rs = [3, 4, 5, 6, 7]
    random.shuffle(rs)
    r1 = rs.pop()
    r2 = rs.pop()
    r3 = rs.pop()

    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[r2], RIN[8 + scrambled[curr_i]], 0))
    for _ in range(random.randint(0, 2)):
        OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[random.randint(0, 63)]], ROUT[random.choice(rs)], RIN[random.randint(0, 63)], random.randint(0, 63)))
        LEFT -= 1
    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[r3], RIN[8 + scrambled[next_i]], 0))
    for _ in range(random.randint(0, 2)):
        OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[random.randint(0, 63)]], ROUT[random.choice(rs)], RIN[random.randint(0, 63)], random.randint(0, 63)))
        LEFT -= 1

    noise1 = random.randint(0, 0xff)
    opi_noise, noise2 = get_noise()

    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD_R]], ROUT[r1], RIN[r2], RIN[r3]))
    for _ in range(random.randint(0, 2)):
        OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[random.randint(0, 63)]], ROUT[random.choice(rs)], RIN[random.randint(0, 63)], random.randint(0, 63)))
        LEFT -= 1

    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[opi_noise]], ROUT[r1], RIN[r1], noise1))
    for _ in range(random.randint(0, 2)):
        OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[random.randint(0, 63)]], ROUT[random.choice(rs)], RIN[random.randint(0, 63)], random.randint(0, 63)))
        LEFT -= 1

    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_AND]], ROUT[r1], RIN[r1], 0xff))
    for _ in range(random.randint(0, 2)):
        OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[random.randint(0, 63)]], ROUT[random.choice(rs)], RIN[random.randint(0, 63)], random.randint(0, 63)))
        LEFT -= 1

    # checking and branching if flag is correct
    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_XOR]], ROUT[3], RIN[r1], ((ord(flag[curr_i]) + ord(flag[next_i]) + noise1) ^ noise2) & 0xff))
    for _ in range(random.randint(0, 2)):
        OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[random.randint(0, 63)]], ROUT[random.randint(4, 7)], RIN[random.randint(0, 63)], random.randint(0, 63)))
        LEFT -= 1
    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_JMP]], ROUT[1], RIN[3], 0x300 + 3))

    # this means false
    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[2], RIN[0], 1))
    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[3], RIN[0], 0))
    OPS.append(make_op(0, 1, 0, 0, OOUT_IDXS[OP_IDXS[OPI_JMP]], ROUT[1], RIN[3], 0x300 + 1))

    curr_i = next_i

# sub/xor to update verdict

for _ in range(random.randint(12, 16)):
    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[random.randint(0, 63)]], ROUT[random.choice([3, 4, 5, 6, 7])], RIN[random.randint(0, 63)], random.randint(0, 63)))

# control flow
fail_msg = "\nWrong password!\n"
success_msg = "\nCorrect password!\n"

# OPS.append(make_op(0, 0, 0, 0, OPI_ADD, 2, 0, 10))
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_JMP]], ROUT[1], RIN[2], 0x300 + len(fail_msg) + 4))

# default flow: print fail, then skip following
# jmp: print success

## make fail message
msg = fail_msg
for i in range(len(msg)):
    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[8+i], RIN[0], ord(msg[i])))

## print fail message
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[1], RIN[0], 8))
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_OUTPUT]], ROUT[1], RIN[1], 0x0200+len(msg)))

OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[2], RIN[0], 0))
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_JMP]], ROUT[1], RIN[2], 0x300 + len(success_msg) + 2))

## make success message
msg = success_msg
for i in range(len(msg)):
    OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[8+i], RIN[0], ord(msg[i])))

## print success message
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_ADD]], ROUT[1], RIN[0], 8))
OPS.append(make_op(0, 0, 0, 0, OOUT_IDXS[OP_IDXS[OPI_OUTPUT]], ROUT[1], RIN[1], 0x0200+len(msg)))

print(OPS)


# Frontend/Databank specific settings
try:
    from utils.namespaces import NAMESPACES, PREFIXES
except ImportError:
    pass

REDIS_HOST = 'localhost'

GRANARY_STORE = u'/tmp/silos'
GRANARY_URI_ROOT = u'http://databank/'

DOI_CONFIG = '/var/lib/databank/rdfdatabank/config/doi_config.py'
DOI_COUNT = '/var/lib/databank/rdfdatabank/config/doi_count'

BROADCAST_TO = u'redis'
BROADCAST_QUEUE = u'silochanges'

METADATA_EMBARGOED = False

SOLR_HOST = u'http://localhost:8080/solr'
NAMING_RULE = r'[^0-9a-zA-Z_\-\:]'
NAMING_RULE_HUMANIZED = u"numbers, letters, '-' and ':', must be more than one character long and must not contain any spaces."
FORMATS_SERVED = ('text/html',
                  'text/xhtml',
                  'text/plain',
                  'application/json',
                  'application/rdf+xml',
                  'text/xml',
                  'text/rdf+n3',
                  'application/x-turtle',
                  'text/rdf+ntriples',
                  'text/rdf+nt',
                  )
PUBLISHER = u'Bodleian Libraries, University of Oxford'
RIGHTS = 'http://ora.ouls.ox.ac.uk/objects/uuid%3A1d00eebb-8fed-46ad-8e38-45dbdb4b224c'
LICENSE = u'CC0 1.0 Universal (CC0 1.0). See http://creativecommons.org/publicdomain/zero/1.0/legalcode'
#license = Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-nc-sa/3.0/

API_VERSION = 0.3


import ast
import random
import time
import pika
import requests
from flask import Flask
from flask_restful import Api

app = Flask(__name__)
api = Api(app)
app.config['DEBUG'] = True

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='wotnot', durable=True)

def callback(ch, method, properties, body):
    data = body.decode("utf-8")
    data = ast.literal_eval(data)
    if data['random'] % 10 == 0:
        r = random.randint(0, 60)
        time.sleep(4)
        if r % 10 == 0:
            data['category'] = "Failed"
        else:
            data['category'] = "Retried"
    payload = {
        "data": [
            {
                "id": data['id'],
                "message": data['message'],
                "count": data['count'],
                "category": data['category']
            }
        ]
    }
    response = requests.post("http://127.0.0.1:3000/tracker/insert", json=payload)
    print(data)
    print(response.json())
    print(response.status_code)

    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_consume(queue='wotnot', on_message_callback=callback)
channel.start_consuming()


import torch
import torch.utils.data as Data
import torchvision
from torch import nn
from torch.autograd import Variable
from torchvision import transforms

from network.Transformer import Transformer
from network.Discriminator2 import Discriminator
from network.vgg import Vgg16
from network.image_utils import generate_img_batch, calc_acc

import network.image_utils
import network.netutils

import config as cfg
import os

class Main(object):
    def __init__(self):
        # network
        self.T = None
        self.D = None
        self.opt_T = None
        self.opt_D = None
        self.self_regularization_loss = None
        self.local_adversarial_loss = None
        self.delta = None

        # data
        self.anime_train_loader = None
        self.animeblur_train_loader = None
        self.real_loader = None

        self.anime_train_loader = None
        self.animeblur_train_loader = None

    def build_network(self):
        print('=' * 50)
        print('Building network...')
        #self.T = Transformer(4, cfg.img_channels, nb_features=64)

        self.T = Transformer()
        self.D = Discriminator(input_features=cfg.img_channels)

        if cfg.cuda_use:
            self.T.cuda()
            self.D.cuda()

        self.opt_T = torch.optim.Adam(self.T.parameters(), lr=cfg.t_lr)
        self.opt_D = torch.optim.SGD(self.D.parameters(), lr=cfg.d_lr)
        self.self_regularization_loss = nn.L1Loss(size_average=False)
        self.local_adversarial_loss = nn.CrossEntropyLoss(size_average=True)
        self.delta = cfg.delta

        self.vgg = Vgg16(requires_grad=False)
        network.netutils.init_vgg16("./models/")

        self.vgg.load_state_dict(torch.load(os.path.join("./models/", "vgg16.weight")))

        self.vgg.cuda()

    def load_data(self):
        print('=' * 50)
        print('Loading data...')

        transform = transforms.Compose([
#            transforms.Grayscale,
            transforms.Scale((cfg.img_width, cfg.img_height)),
            transforms.ToTensor(),
            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

        anime_train_folder = torchvision.datasets.ImageFolder(root=cfg.anime_path, transform=transform)
        animeblur_train_folder = torchvision.datasets.ImageFolder(root=cfg.animeblur_path, transform=transform)

        real_train_folder = torchvision.datasets.ImageFolder(root=cfg.real_path, transform=transform)

        self.anime_train_loader = Data.DataLoader(anime_train_folder, batch_size=cfg.batch_size, shuffle=True,
                                                pin_memory=True)
        self.animeblur_train_loader = Data.DataLoader(animeblur_train_folder, batch_size=cfg.batch_size, shuffle=True,
                                                pin_memory=True)

        self.real_train_loader = Data.DataLoader(real_train_folder, batch_size=cfg.batch_size, shuffle=True,
                                                pin_memory=True)

        print('anime_train_batch %d' % len(self.anime_train_loader))
        print('animeblur_train_batch %d' % len(self.animeblur_train_loader))
        real_folder = torchvision.datasets.ImageFolder(root=cfg.real_path, transform=transform)
        # real_folder.imgs = real_folder.imgs[:2000]
        self.real_loader = Data.DataLoader(real_folder, batch_size=cfg.batch_size, shuffle=True,
                                           pin_memory=True)
        print('real_batch %d' % len(self.real_loader))

    def pre_train_t(self):
        print('=' * 50)

        #device = torch.device("cuda" if args.cuda else "cpu")

        if cfg.ref_pre_path:
            print('Loading t_pre from %s' % cfg.ref_pre_path)
            self.T.load_state_dict(torch.load(cfg.ref_pre_path))
            return

        print('pre-training the refiner network %d times...' % cfg.t_pretrain)

        mse_loss = torch.nn.MSELoss()

        for index in range(cfg.t_pretrain):

            #print("aaaaaaaa")
            anime_image_batch, _ = iter(self.anime_train_loader).next()
            anime_image_batch = Variable(anime_image_batch).cuda()

            animeblur_image_batch, _ = iter(self.animeblur_train_loader).next()

            animeblur_image_batch = Variable(animeblur_image_batch).cuda()

            real_image_batch, _ = iter(self.real_train_loader).next()
            real_image_batch = Variable(real_image_batch).cuda()

            #print(real_image_batch.size())

            self.T.train()
            transreal_image_batch = self.T(real_image_batch)

            #################################
            real_features = self.vgg(real_image_batch).relu4_3
            transreal_features = self.vgg(transreal_image_batch).relu4_3
            #t_loss = self.self_regularization_loss( real_image_batch, transreal_image_batch )
            #t_loss = mse_loss(real_features, transreal_features)
            loss = torch.abs(transreal_features - real_features)

            #t_loss = loss.sum() / (cfg.batch_size * loss.mean())
            t_loss = loss.mean()
            print(t_loss.size())
            print(t_loss)
            #################################
            # t_loss = torch.div(t_loss, cfg.batch_size)
            t_loss = torch.mul(t_loss, self.delta)

            self.opt_T.zero_grad()
            t_loss.backward()
            self.opt_T.step()

            # log every `log_interval` steps
            if (index % cfg.t_pre_per == 0) or (index == cfg.t_pretrain - 1):
                # figure_name = 'refined_image_batch_pre_train_step_{}.png'.format(index)
                print('[%d/%d] (R)reg_loss: %.4f' % (index, cfg.t_pretrain, t_loss.data[0]))

                anime_image_batch, _ = iter(self.anime_train_loader).next()
                anime_image_batch = Variable(anime_image_batch, volatile=True).cuda()

                animeblur_image_batch, _ = iter(self.animeblur_train_loader).next()
                animeblur_image_batch = Variable(animeblur_image_batch, volatile=True).cuda()

                real_image_batch, _ = iter(self.real_loader).next()
                real_image_batch = Variable(real_image_batch, volatile=True).cuda()

                self.T.eval()
                ref_image_batch = self.T(real_image_batch)

                figure_path = os.path.join(cfg.train_res_path, 'refined_image_batch_pre_train_%d.png' % index)

                generate_img_batch(anime_image_batch.data.cpu(), ref_image_batch.data.cpu(),
                                   real_image_batch.data.cpu(), figure_path)

                self.T.train()

                print('Save t_pre to models/t_pre.pkl')
                torch.save(self.T.state_dict(), 'models/t_pre.pkl')

    def pre_train_d(self):
        print('=' * 50)
        if cfg.disc_pre_path:
            print('Loading D_pre from %s' % cfg.disc_pre_path)
            self.D.load_state_dict(torch.load(cfg.disc_pre_path))
            return

        print('pre-training the discriminator network %d times...' % cfg.t_pretrain)

        self.D.train()
        self.T.eval()
        for index in range(cfg.d_pretrain):
            real_image_batch, _ = iter(self.real_loader).next()
            real_image_batch = Variable(real_image_batch).cuda()

            anime_image_batch, _ = iter(self.anime_train_loader).next()
            anime_image_batch = Variable(anime_image_batch).cuda()

            animeblur_image_batch, _ = iter(self.anime_train_loader).next()
            animeblur_image_batch = Variable(animeblur_image_batch).cuda()

            assert real_image_batch.size(0) == anime_image_batch.size(0)
            assert real_image_batch.size(0) == anime_image_batch.size(0)

            d_real_pred = self.D(real_image_batch).view(-1, 2)

            d_anime_y = Variable(torch.zeros(d_real_pred.size(0)).type(torch.LongTensor)).cuda()
            # real to fake
            d_real_y = Variable(torch.ones(d_real_pred.size(0)).type(torch.LongTensor)).cuda()
            # blur is fake
            d_blur_y = Variable(torch.ones(d_real_pred.size(0)).type(torch.LongTensor)).cuda()

            # ============ real image D ====================================================
            # self.D.train()
            d_anime_pred = self.D(anime_image_batch).view(-1, 2)
            acc_anime = calc_acc(d_anime_pred, 'real')
            d_loss_anime = self.local_adversarial_loss(d_anime_pred, d_anime_y)
            # d_loss_real = torch.div(d_loss_real, cfg.batch_size)

            # ============ anime image D ====================================================
            # self.T.eval()
            real_image_batch = self.T(real_image_batch)

            # self.D.train()
            d_real_pred = self.D(real_image_batch).view(-1, 2)
            acc_real = calc_acc(d_real_pred, 'refine')
            d_loss_real = self.local_adversarial_loss(d_real_pred, d_real_y)
            # d_loss_ref = torch.div(d_loss_ref, cfg.batch_size)

            # =========== blue image D =============

            d_animeblur_pred = self.D(animeblur_image_batch).view(-1, 2)
            acc_blur = calc_acc(d_animeblur_pred, 'refine')
            d_loss_animeblur = self.local_adversarial_loss(d_animeblur_pred, d_blur_y)

            d_loss = d_loss_anime + d_loss_animeblur + d_loss_real

            self.opt_D.zero_grad()
            d_loss.backward()
            self.opt_D.step()

            if (index % cfg.d_pre_per == 0) or (index == cfg.d_pretrain - 1):
                print('[%d/%d] (D)d_loss:%f  acc_anime:%.2f%% acc_real:%.2f%% acc_blur:%.2f%%'
                      % (index, cfg.d_pretrain, d_loss.data[0], acc_anime, acc_real, acc_blur))

        print('Save D_pre to models/D_pre.pkl')
        torch.save(self.D.state_dict(), 'models/D_pre.pkl')

    def train(self):
        print('=' * 50)
        print('Training...')

        #self.D.load_state_dict(torch.load("models/D_620.pkl"))
        #self.T.load_state_dict(torch.load("models/T_620.pkl"))

        '''
        image_history_buffer = ImageHistoryBuffer((0, cfg.img_channels, cfg.img_height, cfg.img_width),
                                                  cfg.buffet_size * 10, cfg.batch_size)
        '''

        for step in range(cfg.train_steps):
            print('Step[%d/%d]' % (step, cfg.train_steps))

            # ========= train the T =========
            self.D.eval()
            self.T.train()

            for p in self.D.parameters():
                p.requires_grad = False

            total_t_loss = 0.0
            total_t_loss_reg_scale = 0.0
            total_t_loss_adv = 0.0
            total_acc_adv = 0.0

            for index in range(cfg.k_t):

                real_image_batch, _ = iter(self.real_loader).next()
                real_image_batch = Variable(real_image_batch).cuda()

                #real_image_batch, _ = iter(self.real_loader).next()
                #real_image_batch = Variable(real_image_batch).cuda()

                d_real_pred = self.D(real_image_batch).view(-1, 2)

                d_real_y = Variable(torch.zeros(d_real_pred.size(0)).type(torch.LongTensor)).cuda()

                transreal_image_batch = self.T(real_image_batch)
                d_transreal_pred = self.D(transreal_image_batch).view(-1, 2)

                acc_adv = calc_acc(d_transreal_pred, 'real')
                t_loss_adv = self.local_adversarial_loss(d_transreal_pred, d_real_y)

		#--------================================================================

                real_features = self.vgg(real_image_batch).relu4_3
                transreal_features = self.vgg(transreal_image_batch).relu4_3
                #t_loss = self.self_regularization_loss( real_image_batch, transreal_image_batch )
                #t_loss = mse_loss(real_features, transreal_features)
                loss = torch.abs(transreal_features - real_features)

                #t_loss_reg = loss.sum() / cfg.batch_size

                t_loss_reg = loss.mean()

                #--------================================================================

                #t_loss_reg = self.self_regularization_loss(   self.vgg(real_image_batch).relu4_3 ,  self.vgg(transreal_image_batch).relu4_3  )
                t_loss_reg_scale = torch.mul(t_loss_reg, self.delta)

                t_loss = t_loss_adv + t_loss_reg_scale

                self.opt_T.zero_grad()
                self.opt_D.zero_grad()
                t_loss.backward()
                self.opt_T.step()

                total_t_loss += t_loss
                total_t_loss_reg_scale += t_loss_reg_scale
                total_t_loss_adv += t_loss_adv
                total_acc_adv += acc_adv

            mean_t_loss = total_t_loss / cfg.k_t
            mean_t_loss_reg_scale = total_t_loss_reg_scale / cfg.k_t
            mean_t_loss_adv = total_t_loss_adv / cfg.k_t
            mean_acc_adv = total_acc_adv / cfg.k_t

            print('(R)t_loss:%.4f t_loss_reg:%.4f, t_loss_adv:%f(%.2f%%)'
                  % (mean_t_loss.data[0], mean_t_loss_reg_scale.data[0], mean_t_loss_adv.data[0], mean_acc_adv))

            # ========= train the D =========
            self.T.eval()
            self.D.train()
            for p in self.D.parameters():
                p.requires_grad = True

            for index in range(cfg.k_d):
                real_image_batch, _ = iter(self.real_loader).next()
                anime_image_batch, _ = iter(self.anime_train_loader).next()
                animeblur_image_batch, _ = iter(self.animeblur_train_loader).next()
                assert real_image_batch.size(0) == anime_image_batch.size(0)

                real_image_batch = Variable(real_image_batch).cuda()
                anime_image_batch = Variable(anime_image_batch).cuda()
                animeblur_image_batch = Variable(anime_image_batch).cuda()

                d_anime_y = Variable(torch.zeros(d_real_pred.size(0)).type(torch.LongTensor)).cuda()
                d_real_y = Variable(torch.ones(d_real_pred.size(0)).type(torch.LongTensor)).cuda()
                d_blur_y = Variable(torch.ones(d_real_pred.size(0)).type(torch.LongTensor)).cuda()

                d_anime_pred = self.D(anime_image_batch).view(-1, 2)
                acc_anime = calc_acc(d_anime_pred, 'real')
                d_loss_anime = self.local_adversarial_loss(d_anime_pred, d_anime_y)

                real_image_batch = self.T(real_image_batch)
                d_real_pred = self.D(real_image_batch).view(-1, 2)
                acc_real = calc_acc(d_real_pred, 'refine')
                d_loss_real = self.local_adversarial_loss(d_real_pred, d_real_y)

                d_animeblur_pred = self.D(animeblur_image_batch).view(-1, 2)
                acc_blur = calc_acc(d_animeblur_pred, 'refine')
                d_loss_animeblur = self.local_adversarial_loss(d_animeblur_pred, d_blur_y)

                d_loss = d_loss_real + d_loss_anime + d_loss_animeblur

                self.D.zero_grad()
                d_loss.backward()
                self.opt_D.step()

                print('(D)d_loss:%.4f anime_loss:%.4f(%.2f%%) real_loss:%.4f(%.2f%%) blur_loss:%.4f(%.2f%%)'
                      % (d_loss.data[0] / 2, d_loss_anime.data[0], acc_anime, d_loss_real.data[0], acc_real, d_loss_animeblur.data[0], acc_blur))

            if step % cfg.save_per == 0:
                print('Save two model dict.')
                torch.save(self.D.state_dict(), cfg.D_path % step)
                torch.save(self.T.state_dict(), cfg.T_path % step)

                real_image_batch, _ = iter(self.real_loader).next()
                real_image_batch = Variable(real_image_batch, volatile=True).cuda()

                anime_image_batch, _ = iter(self.anime_train_loader).next()
                anime_image_batch = Variable(anime_image_batch, volatile=True).cuda()

                animeblur_image_batch, _ = iter(self.animeblur_train_loader).next()
                animeblur_image_batch = Variable(animeblur_image_batch, volatile=True).cuda()

                self.T.eval()
                realtrans_image_batch = self.T(real_image_batch)
                self.generate_batch_train_image(real_image_batch, realtrans_image_batch, animeblur_image_batch, step_index=step)

    def generate_batch_train_image(self, anime_image_batch, ref_image_batch, real_image_batch, step_index=-1):
        print('=' * 50)
        print('Generating a batch of training images...')
        self.T.eval()

        pic_path = os.path.join(cfg.train_res_path, 'step_%d.png' % step_index)
        generate_img_batch(anime_image_batch.cpu().data, ref_image_batch.cpu().data, real_image_batch.cpu().data, pic_path)
        print('=' * 50)

    # def generate_all_train_image(self):
    #     print('=' * 50)
    #     print('Generating all training images...')
    #     self.T.eval()
    #
    #     for index, (anime_image_batch, _) in enumerate(self.anime_train_loader):
    #         pic_path = os.path.join(cfg.final_res_path, 'batch_%d.png' % index)
    #
    #         anime_image_batch = Variable(anime_image_batch, volatile=True).cuda()
    #         ref_image_batch = self.R(anime_image_batch)
    #         generate_img_batch(anime_image_batch.cpu().data, ref_image_batch.cpu().data, pic_path)
    #     print('=' * 50)

if __name__ == '__main__':
    obj = Main()

    if not torch.cuda.is_available():
        raise ValueError("ERROR: cuda is not available, try running on CPU")

    obj.build_network()

    obj.load_data()

    obj.pre_train_t()
#    obj.pre_train_d()
    obj.train()

    obj.generate_all_train_image()


# Task 23 - Preparing for interviews
# Compulsory Task 2 - Little sister’s vocab

# 1. Add a prefix to a word
# Here I declared the  function 'add_prefix_un(word)' that add the string "un" to the word input by the user.

word = input("Insert a word to add a prefix 'un': ")

def add_prefix_un(word):
    return "un" + str(word)

print(add_prefix_un(word))

# 2. Add prefixes to word groups
# Here I declared the function 'make_word_groups(vocab_words)' that takes the first element of the list.
# Then crate another list with the rest of elements
# And finally adds the first prefix (first element) to each of the other elements of the group

vocab_words = ['en', 'close', 'joy', 'lighten']

def make_word_groups(vocab_words):
    prefix = vocab_words[0]
    words = vocab_words[1:]
    prefixed_words = [prefix + word for word in words]
    word_groups = [prefix] + prefixed_words
    return ' :: '.join(word_groups)

print("\nThe string with the prefix applied: ")
print(make_word_groups(vocab_words))

# 3. Remove a suffix from a word
# Here I declared the function 'remove_suffix_ness(word2)' that takes the root of the word in case finishes with 'ness'
# Then if the root of the word doesn't finishes with 'i', return the root.
# However, if the root of the word finishes with 'i', adds a 'y' at the end

word2 = input("\nInsert a word to delete the suffix 'ness': ")

def remove_suffix_ness(word2):
    if word2.endswith("ness"):
        root_word = word2[:-4]
        if root_word[-1] == 'i':
            return root_word[:-1] + 'y'
        else:
            return root_word
    else:
        return word2

print(remove_suffix_ness(word2))

# 4. Extract and transform a word.
# Here I declared the function 'adjective_to_verb(sentence, index):' that spilts the sentence by each word.
# Then following the index, takes the word that is going to be the verb
# The function then ad 'en' to the adjective and converts it in a verb

sentence = "It got dark as the sun set."
index = 2

def adjective_to_verb(sentence, index):
    words = sentence.split()
    adjective = words[index]
    verb = adjective + 'en'
    return verb

print("\nThe extracted adjective as a verb: ")
print("The original sentence is: "+ sentence)
print(adjective_to_verb(sentence, index))