import pandas as pd
from typing import List, Any
from dataclasses import dataclass
from src.data.load import load_wav_file
import numpy as np
import math

def aggregate_groups(grouped_df: Any) -> pd.DataFrame:
    new_df = grouped_df.agg({'Begin Time (s)': list})
    new_df['End Time (s)'] = grouped_df.agg({'End Time (s)': list})['End Time (s)']
    new_df['File Offset (s)'] = grouped_df.agg({'File Offset (s)': list})['File Offset (s)']
    new_df['Duration (s)'] = grouped_df.agg({'Duration (s)': list})['Duration (s)']
    new_df.reset_index(inplace=True, )
    return new_df

def preprocess_training_df(training_df: pd.DataFrame, wav_buckets: List[Any]) -> pd.DataFrame:
    '''
        Preprocesses the dataframe such that we can use it for extracting the rumble clips
    '''

    def _get_keys(wav_keys, file_name):
        for key in wav_keys:
            wav_name = key.split('/')[-1]
            if file_name == wav_name:
                return key
        return None

    wav_keys = [summary.key for summary in wav_buckets]
    training_df['aws key'] = training_df['Begin File'].apply(lambda file_name: _get_keys(wav_keys, file_name))
    # Add the duration
    training_df["Duration (s)"] = training_df['End Time (s)'] - training_df['Begin Time (s)']

    training_df = training_df.groupby(['Begin File', 'aws key'])

    result = aggregate_groups(training_df)

    return result

def merge_rumble_and_gunshots(preprocessed_rumble_df: pd.DataFrame,
                              preprocessed_gunshot_df: pd.DataFrame, ) -> pd.DataFrame:
    '''
        Preprocesses the dataframe such that we can use it for extracting the rumble clips
    '''

    cols_to_rename = [
        'Begin Time (s)', 'End Time (s)',
        'File Offset (s)', 'Duration (s)'

    ]
    rumble_rename = {col: f'Rumble {col}' for col in cols_to_rename}
    gunshot_rename = {col: f'Gunshot {col}' for col in cols_to_rename}

    preprocessed_rumble_df = preprocessed_rumble_df.rename(rumble_rename, axis=1)
    preprocessed_gunshot_df = preprocessed_gunshot_df.rename(gunshot_rename, axis=1)

    merged_result = preprocessed_rumble_df.merge(preprocessed_gunshot_df, how='outer', on=['Begin File', 'aws key'])

    return merged_result

@dataclass
class Clip:
    data: np.array
    sample_rate: int
    sound_class: str

def extract_clips(wav_data: np.array, sample_rate: int, row: any, sound_classes: list[str], clip_lenght=5.0) -> List[
    Clip]:
    '''
        Extract clips from wav data based on the rows and classes.
        Args:
            wav_data: numpy array containing the wav data
            sample_rate: sample rate of the wav file
            row: dataframe row containing the file offsets and durations for the given classes
            sound_classes: the different sound classes e.g. Rumble, Gunshot, Other
            clip_lenght: the lenghts of the resulting clips. Any clip that has a longer duration will not be returned
                clip_length is the number of clips that
        Returns:
            A list of clips
    '''
    clips = [
    ]
    for sound_class in sound_classes:
        start_times = row[f'{sound_class} File Offset (s)']
        durations = row[f'{sound_class} Duration (s)']

        for start_time, duration in zip(start_times, durations):
            start_index = int((start_time) * sample_rate)

            if duration <= clip_lenght:
                end_index = int((start_time + clip_lenght) * sample_rate)

                clips.append(Clip(wav_data[start_index: end_index], sample_rate, sound_class))
    return clips

def get_clips_from_file(s3: Any, row: Any, sound_classes) -> List[Clip]:
    '''
        Get the clips from a given file
    '''
    # Load the datafile
    sample_rate, data = load_wav_file(s3, row['aws key'])

    return extract_clips(data, sample_rate, row, sound_classes)

def add_random_clips(df: pd.DataFrame) -> pd.DataFrame:
    def __add_random_intervals(x: any, clip_length=5):
        n_seconds_day = 60 * 60 * 24

        rumble_lengths = len(x['Rumble File Offset (s)'])
        gunshot_lenghts = len(x['Gunshot File Offset (s)'])

        size = max(rumble_lengths, gunshot_lenghts)
        x["Other File Offset (s)"] = list(np.random.uniform(low=0, high=n_seconds_day, size=size))
        x["Other Duration (s)"] = list([clip_length] * size)

        return x

    df = df.apply(__add_random_intervals, axis=1)

    return df


from typing import List
from app.classes.spec.symboleo_contract import SymboleoContract
from app.classes.spec.declaration import Declaration
from app.classes.spec.norm import Obligation
from app.classes.operations.contract_updater_config import UpdateConfig
from app.classes.operations.op_code import OpCode

class TestCase:
    def __init__(
        self,
        case_id: str,
        init_sym: SymboleoContract,
        op_code: OpCode,
        update_config: UpdateConfig,
        exp_sym: SymboleoContract = None
    ):
        self.case_id = case_id
        self.init_sym = init_sym
        self.op_code = op_code
        self.update_config = update_config
        self.exp_sym = exp_sym

class TestInfo:
    def __init__(
        self,
        full_nl: str,
        refinement: str,
        declarations: List[Declaration],
        obligation: Obligation
    ):
        self.full_nl = full_nl
        self.declarations = declarations
        self.obligation = obligation


from abc import ABCMeta, abstractmethod
from fastsklearnfeature.interactiveAutoML.new_bench.multiobjective.metalearning.openml_data.private_models.randomforest.samfletcherforest import DP_Random_Forest
from sklearn.base import BaseEstimator, ClassifierMixin
import pandas as pd
import copy
import numpy as np

class PrivateRandomForest(ClassifierMixin, BaseEstimator, metaclass=ABCMeta):

    def __init__(self, n_estimators, epsilon, max_depth=None):
        self.n_estimators = n_estimators
        self.epsilon = epsilon
        self.max_depth = max_depth

    def fit(self, X, y, sample_weight=None):

        self.n_outputs_ = 1
        self.n_features_ = X.shape[1]

        cats = []
        for i in range(X.shape[1]):
            if len(np.unique(X[:, i])) <= 2:
                cats.append(i + 1)

        new_y = copy.deepcopy(y)
        if isinstance(y, pd.DataFrame):
            new_y = new_y.values
        new_y = new_y.reshape(-1,1)

        self.classes_ = np.unique(new_y)

        all_data = np.hstack((new_y,X))

        self.model = DP_Random_Forest(all_data, epsilon=self.epsilon, num_trees=self.n_estimators, categs=cats, max_depth=self.max_depth)
        return self

    def predict(self, X):
        empty = np.zeros((len(X), 1))
        all_data = np.hstack((empty, X))
        return np.asarray(self.model.predict(all_data))

# A toy example of how to call the class
if __name__ == '__main__':
    from sklearn.datasets import load_breast_cancer
    from sklearn.metrics import f1_score
    diabetes = load_breast_cancer()

    X = diabetes.data
    y = diabetes.target

    model = PrivateRandomForest(n_estimators=100, epsilon=0.1)
    model.fit(X, y)

    print(f1_score(y,model.predict(X)))

    #print(model.predict(X))

    import numpy as np
    from art.classifiers import SklearnClassifier

    import copy
    from art.attacks.evasion import HopSkipJump

    classifier = SklearnClassifier(model=model)
    attack = HopSkipJump(classifier=classifier, max_iter=1, max_eval=100)

    X_test_adv = attack.generate(X)

    print(model.predict(X_test_adv))


from nose.tools import assert_equal

def add2(x, y):
    return x + y

def check_add2(exp, x, y):
    obs = add2(x, y)
    assert_equal(exp, obs)

def test_add2():
    cases = [
        (4, 2, 2),
        (5, -5, 10),
        (42, 40, 2),
        (16, 3, 13),
        (-128, 0, -128),
        ]
    for exp, x, y in cases:
        yield check_add2, exp, x, y


from flask import Flask, render_template, request, redirect, url_for, flash, jsonify

app = Flask(__name__)

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Restaurant, MenuItem

engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine

DBSession = sessionmaker(bind=engine)
session = DBSession()

@app.route('/restaurants/<int:restaurant_id>/')
def restaurant_menu(restaurant_id):
    restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
    items = session.query(MenuItem).filter_by(restaurant_id=restaurant_id)

    return render_template('menu.html', restaurant=restaurant, items=items)

@app.route('/restaurants/<int:restaurant_id>/JSON')
def restaurant_menu_json(restaurant_id):
    restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
    items = session.query(MenuItem).filter_by(restaurant_id=restaurant_id)
    return jsonify(menu_items=[i.serialize for i in items])

@app.route('/restaurants/<int:restaurant_id>/new/', methods=['GET', 'POST'])
def new_menu_item(restaurant_id):
    if request.method == 'POST':
        new_item = MenuItem(name=request.form['name'],
                            restaurant_id=restaurant_id,
                            description=request.form.get('description', ""),
                            price=request.form.get('price'))
        session.add(new_item)
        session.commit()
        flash("new menu item created!")
        return redirect(url_for('restaurant_menu', restaurant_id=restaurant_id))
    else:
        return render_template('newmenuitem.html', restaurant_id=restaurant_id)

# Task 2: Create route for editMenuItem function here
@app.route('/restaurants/<int:restaurant_id>/menu/<int:menu_id>/edit/', methods=['GET', 'POST'])
def edit_menu_item(restaurant_id, menu_id):
    menu_item = session.query(MenuItem).filter_by(restaurant_id=restaurant_id, id=menu_id).one()
    if request.method == 'POST':
        menu_item.name = request.form['name']
        menu_item.price = request.form.get('price', '$0.0')
        menu_item.description = request.form.get('description', '')
        session.commit()
        flash("menu item edited!")
        return redirect(url_for('restaurant_menu', restaurant_id=restaurant_id))
    else:
        return render_template('editmenuitem.html', restaurant_id=restaurant_id, menu_id=menu_id, menu_item=menu_item)

@app.route('/restaurants/<int:restaurant_id>/menu/<int:menu_id>/JSON')
def restaurant_menu_item_jason(restaurant_id, menu_id):
    menu_item = session.query(MenuItem).filter_by(restaurant_id=restaurant_id, id=menu_id).one()
    return jsonify({"MenuItem": menu_item.serialize})

# Task 3: Create a route for deleteMenuItem function here
@app.route('/restaurants/<int:restaurant_id>/menu/<int:menu_id>/delete/', methods=['GET', 'POST'])
def delete_menu_item(restaurant_id, menu_id):
    menu_item = session.query(MenuItem).filter_by(restaurant_id=restaurant_id, id=menu_id).one()
    if request.method == 'POST':
        session.delete(menu_item)
        session.commit()
        flash("menu item " + menu_item.name + " deleted!")
        return redirect(url_for('restaurant_menu', restaurant_id=restaurant_id))
    else:
        return render_template('deleetemenuitem.html', menu_item=menu_item)

@app.route('/restaurant/<int:restaurant_id>/delete/', methods=['GET', 'POST'])
def delete_restaurant(restaurant_id):
    restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
    if request.method == 'POST':
        session.delete(restaurant)
        session.commit()
        flash("Restaurant " + restaurant.name + " deleted!")
        return redirect(url_for('show_restaurants'))
    else:
        return render_template('deleterestaurant.html', restaurant=restaurant)

@app.route('/restaurant/new/', methods=['GET', 'POST'])
def new_restaurant():
    if request.method == 'POST':
        restaurant = Restaurant(name=request.form['name'])
        session.add(restaurant)
        session.commit()
        flash("new restaurant created!")
        return redirect(url_for('show_restaurants'))
    else:
        return render_template('newrestaurant.html')

@app.route('/restaurant/<int:restaurant_id>/edit/', methods=['GET', 'POST'])
def edit_restaurant(restaurant_id):
    restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
    if request.method == 'POST':
        restaurant.name = request.form.get('name', '')
        session.commit()
        flash("Restaurant" + restaurant.name + " edited!")
        return redirect(url_for('show_restaurants'))
    else:
        return render_template('editrestaurant.html', restaurant=restaurant)

@app.route('/restaurants/JSON')
def show_restaurants_json():
    restaurants = session.query(Restaurant)
    return jsonify(restaurant_items=[i.serialize for i in restaurants])

@app.route('/')
@app.route('/restaurants')
def show_restaurants():
    # return "Hello World"
    restaurants = session.query(Restaurant)
#    items = session.query(MenuItem).filter_by(restaurant_id=restaurant.id)
    return render_template('restaurant.html', restaurants=restaurants, is_home=True)

if __name__ == '__main__':
    app.secret_key = 'moo moo I am a cow'
    app.debug = True
    app.run(host='0.0.0.0', port=5002)


import os
import json
import datetime

from flask import Flask, url_for, redirect, \
    render_template, session, request
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, login_required, login_user, \
    logout_user, current_user
from requests_oauthlib import OAuth2Session
from requests.exceptions import HTTPError

from htmlmin.minify import html_minify

from .forms import InputForm

from .tools import process, users_cards_save, print_user_library, print_user_deck, \
    modify_user_deck, save_user_library, process_by_id
from libs.builder import Builder

import app.shared_stuff

"""App Configuration"""

from .config import FlaskConfig
from .config import GoogleAuthConfig
from .config import DatabaseConfig

"""APP creation and configuration"""
app = Flask(__name__)
app.config.from_object(FlaskConfig)
db = SQLAlchemy(app)
login_manager = LoginManager(app)
login_manager.login_view = "login"
login_manager.session_protection = "strong"

shared_stuff.db = db

from .user import User

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))
""" OAuth Session creation """

def get_google_auth(state=None, token=None):
    if token:
        return OAuth2Session(GoogleAuthConfig.CLIENT_ID, token=token)
    if state:
        return OAuth2Session(
            GoogleAuthConfig.CLIENT_ID,
            state=state,
            redirect_uri=GoogleAuthConfig.REDIRECT_URI)
    oauth = OAuth2Session(
        GoogleAuthConfig.CLIENT_ID,
        redirect_uri=GoogleAuthConfig.REDIRECT_URI,
        scope=GoogleAuthConfig.SCOPE)
    return oauth

@app.route('/')
def index():
    form = InputForm()
    return html_minify(render_template(
        'home.html',
        title='Input',
        fill='',
        form=form))

@app.route('/login')
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    google = get_google_auth()
    auth_url, state = google.authorization_url(
        GoogleAuthConfig.AUTH_URI, access_type='offline')
    session['oauth_state'] = state

    # Since we login only with google, we can skip the "login" page.
    # return html_minify(render_template('login.html', auth_url=auth_url))
    return redirect(auth_url)

@app.route('/gCallback')
def callback():
    if current_user is not None and current_user.is_authenticated:
        return redirect(url_for('index'))
    if 'error' in request.args:
        if request.args.get('error') == 'access_denied':
            return 'You denied access.'
        return 'Error encountered.'
    if 'code' not in request.args and 'state' not in request.args:
        return redirect(url_for('login'))
    else:
        google = get_google_auth(state=session['oauth_state'])
        try:
            token = google.fetch_token(
                GoogleAuthConfig.TOKEN_URI,
                client_secret=GoogleAuthConfig.CLIENT_SECRET,
                authorization_response=request.url)
        except HTTPError:
            return 'HTTPError occurred.'
        google = get_google_auth(token=token)
        resp = google.get(GoogleAuthConfig.USER_INFO)
        if resp.status_code == 200:
            user_data = resp.json()
            email = user_data['email']
            user = User.query.filter_by(email=email).first()
            if user is None:
                user = User()
                user.email = email
            user.name = user_data['name']
            user.tokens = json.dumps(token)
            user.avatar = user_data['picture']
            db.session.add(user)
            db.session.commit()
            login_user(user)
            return redirect(url_for('index'))
        return 'Could not fetch your information.'

@app.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('index'))

@app.route('/searchcard', methods=['POST'])
def searchcard():
    search_string = request.form['search_string']

    output_table, log = process(current_user, search_string)

    # tady html_minify() dělá nějaké problémy ... :/
    return render_template(
        'page_parts/card_search_table.html',
        search_table=output_table)

@app.route('/savecards', methods=['POST'])
@login_required
def savecards():

    card_list = request.form['card_list']

    users_cards_save(current_user, card_list)

    return "success"

@app.route('/savelibrary', methods=['POST'])
@login_required
def savelibrary():

    card_list = request.form['card_list']

    status = save_user_library(current_user, card_list)
    if status:
        return "success"
    else:
        return "failure"

@app.route('/modifydeck', methods=['POST'])
@login_required
def modifydeck():

    card_list = request.form['card_list']
    deck_id = request.form['deck_id']

    status = modify_user_deck(current_user, deck_id, card_list)
    if status:
        return "success"
    else:
        return "failure"

@app.route('/library')
@login_required
def library():

    bu = Builder(DatabaseConfig)

    decks = current_user.get_deck_list()

    for deck in decks:
        if not deck['name']:
            deck['name'] = "Deck {}".format(deck['id'])
        if not deck['card_count']:
            deck['card_count'] = 0

    if decks:
        new_id = decks[-1]['id'] + 1
    else:
        new_id = 1
    newdeck = [{
        'id': new_id,
        'name': 'New Deck',
        'card_count': '',
        'info': ''}]

    library_table, log = print_user_library(current_user)

    return html_minify(render_template(
        'library.html',
        title='Library',
        form="",
        table_of_decks_data=decks + newdeck,
        library_table=library_table,
        fill="",
        log=log,
        db_info=bu.get_db_info()))

@app.route('/deck/<int:deck_id>', methods=['GET', 'POST'])
@login_required
def deck(deck_id, search_table=None):

    form = InputForm()
    if form.validate_on_submit():
        search_table, log_0 = process(current_user, form.text.data)
    else:
        log_0 = []
        search_table = None

    bu = Builder(DatabaseConfig)

    deck_table, log_1 = print_user_deck(current_user, deck_id)
    library_table, log_2 = print_user_library(current_user)

    log = log_0 + log_1 + log_2

    return html_minify(render_template(
        'deck.html',
        title='Deck',
        form="",
        deck_table=deck_table,
        library_table=library_table,
        search_table=search_table,
        fill="",
        log=log,
        db_info=bu.get_db_info()))

@app.route('/card/<card_id>')
@login_required
def card(card_id):

    bu = Builder(DatabaseConfig)

    card_details, output_table, log = process_by_id(card_id)

    return html_minify(render_template(
        'card.html',
        title='Deck',
        form="",
        output_table=output_table,
        card_details_data=card_details,
        fill="",
        log=log,
        db_info=bu.get_db_info()))

@app.route('/input', methods=['GET', 'POST'])
def input():

    form = InputForm()
    bu = Builder(DatabaseConfig)
    if form.validate_on_submit():
        #flash( 'Input containted: %s' % (form.text.data) )
        # return redirect('/')

        output_table, log = process(current_user, form.text.data)

        return html_minify(render_template(
            'table.html',
            title='Output',
            form=form,
            output_table=output_table,
            fill=form.text.data,
            log=log,
            db_info=bu.get_db_info()))

    return html_minify(render_template(
        'home.html',
        title='Input',
        fill='',
        form=form))


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from numpy.polynomial import Polynomial

# Define a polynomial named 'f'
f = Polynomial([2.0, 1.0, -6.0, -2.0, 2.5, 1.0])

# Define the first derivative of polynomial 'f' named 'f_prime'
f_prime = f.deriv(1)

# Draw the X-axis
plt.axhline(y=0, color='k')

# Generate 100 values of x.
x = np.linspace(-2.5, 1.6, 100)

# Calculate y-values of corresponding x-values of f(x).
y = f(x)

# Plot the graph of f(x) using x-values and y-values
plt.plot(x, y)

# Plot the roots of the graph
plt.plot(f.roots(), f(f.roots()), 'ro')

# Print the roots of the function f(x)
print(f.roots())

# Calcuate y-values for corresponding x-values of f'(x).
y = f_prime(x)

# Plot the graph of f'(x)
plt.plot(x, y)

# Plot the roots of f'(x). Notice that, where f'(x) is zero the slop of f(x) is zero.
plt.plot(f_prime.roots(), f_prime(f_prime.roots()), 'bo')

# Print the roots of f'(x).
print(f_prime.roots())

# Start with an initial value
# complete the line below
xk = 5

# Create a list for storing values of x's after each iteration
list_x = [xk, ]

# Create a list for storing values of f(x)'s after each iteration
list_f = [f(xk), ]

while True:
        #+--------------------+
    #| Start of your code |
    #+--------------------+

    # Calculate newer values of xk
    k=xk-f(xk)/f_prime(xk);
    xk=k;
    list_x.append(xk);
    list_f.append(f(xk));
    er=abs(list_x[-1]-list_x[-2])/list_x[-1] ;
    if er<1e-6:
        break;

    # append xk into list_x

    # append f(xk) into list_f

    # Write the breaking condition; answer should be accurate to 6 decimal places

    #+------------------+
    #| End of your code |
    #+------------------+

df = pd.DataFrame({"x": list_x, "f(x)": list_f})

print(df)
plt.show();


import math
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(os.curdir)))
import qs
import sim

from evaluation import plot_colormap
from lists import VerletList
from qs_impl import initial_particles, print_activated_bacteria, apply_diffusion_reaction, print_summed_ahl,\
    load_bacteria, xy_to_index
from numpy import ndarray
from sim_impl import apply_periodic_boundary_conditions, inner_square_outer_boundary_2d, pse_predict_u_2d

def apply_advection(_particles: ndarray):
    for i in range(0, sim.particle_number_per_dim ** 2):
        x = _particles[i][0]
        y = _particles[i][1]

        updated_x = x + sim.dt * 4 * (1 - math.cos(2 * math.pi * y / 50))
        updated_y = y + sim.dt * 3
        _particles[i][0] = updated_x
        _particles[i][1] = updated_y

def a(s: float) -> float:
    _s = abs(s)
    if 0 <= _s < 1:
        return 1 - 0.5 * (5 * _s ** 2 - 3 * _s ** 3)
    elif 1 <= _s < 2:
        return 0.5 * (2 - _s) ** 2 * (1 - _s)
    else:
        return 0

def remesh_range(x: float):
    _x = math.floor(x)
    return filter(lambda y: sim.domain_lower_bound <= y <= sim.domain_upper_bound,
                  [_x - 2, _x - 1, _x, _x + 1, _x + 2, _x + 3])

def apply_remeshing(unmoved_particles: ndarray, moved_particles: ndarray) -> ndarray:
    remeshed_particles = np.zeros(moved_particles.shape)
    remeshed_particles[:, 0] = unmoved_particles[:, 0]  # copy x
    remeshed_particles[:, 1] = unmoved_particles[:, 1]  # copy y
    remeshed_particles[:, 3] = moved_particles[:, 3]  # copy u_c (bacteria stay fixed)

    for i in range(0, sim.particle_number_per_dim ** 2):
        x = moved_particles[i][0]
        y = moved_particles[i][1]
        strength_u_e = moved_particles[i][2]

        for grid_x in remesh_range(x):
            for grid_y in remesh_range(y):
                s_x = (x - grid_x)  # / sim.h = 1
                s_y = (y - grid_y)  # / sim.h = 1
                j = xy_to_index(grid_x, grid_y)
                remeshed_particles[j][2] += a(s_x) * a(s_y) * strength_u_e

    return remeshed_particles

def simulate(_particles: ndarray, _verlet: VerletList, n_evolutions: int):
    inner_square, outer_index_pairs = inner_square_outer_boundary_2d()
    _particle_evolution = [(0, _particles)]
    dt_evolution = sim.t_max if n_evolutions < 1 else sim.t_max / (n_evolutions - 1)

    for t in np.arange(sim.dt, sim.t_max + sim.dt, sim.dt):
        print("{:6.2f}%".format(t / (sim.t_max + sim.dt) * 100))

        updated_particles = apply_diffusion_reaction(_particles, _verlet)
        apply_periodic_boundary_conditions(updated_particles, outer_index_pairs)

        apply_advection(updated_particles)
        updated_particles = apply_remeshing(_particle_evolution[0][1], updated_particles)
        apply_periodic_boundary_conditions(updated_particles, outer_index_pairs)

        _particles = updated_particles
        if t % dt_evolution < sim.dt:
            _particle_evolution.append((t, _particles))

    # There are cases where the last evolution step is missed
    if sim.t_max % dt_evolution != 0:
        _particle_evolution.append((sim.t_max, _particles))
    return _particle_evolution

if __name__ == "__main__":
    sim.D = 1
    sim.domain_lower_bound = -3
    sim.domain_upper_bound = 53
    sim.particle_number_per_dim = 57
    sim.h = (sim.domain_upper_bound - sim.domain_lower_bound) / (sim.particle_number_per_dim - 1)
    sim.epsilon = sim.h
    sim.volume_p = sim.h ** 2
    sim.cutoff = 3 * sim.epsilon
    sim.cell_side = sim.cutoff
    sim.t_max = 10
    sim.dt = sim.h ** 2 / (4 * sim.D)

    qs.gamma_e = 0.1
    qs.gamma_c = 0.1

    particles, verlet = initial_particles()
    load_bacteria("bacterialPos.dat", particles)

    particle_evolution = simulate(particles, verlet, 2)
    print_summed_ahl(particle_evolution)
    print_activated_bacteria(particle_evolution)

    x_coords, y_coords, u_e_coords = pse_predict_u_2d(particle_evolution[-1][1], 0)
    fig = plot_colormap(u_e_coords, sim.particle_number_per_dim, sim.particle_number_per_dim,
                        xlabel="x + {}".format(-sim.domain_lower_bound),
                        ylabel="y + {}".format(-sim.domain_lower_bound),
                        title="$u_e$ concentration field at t={}".format(particle_evolution[-1][0]))
    plt.show()


'''This script demonstrates using dropout in a single-layer auto-encoder for
MNIST data

'''

import os
import matplotlib.pylab as plt

from pydeep.neural import *
import pydeep.utils.utils

mnist_path = os.path.join(os.environ['PYDEEP_HOME'], 'demo',  'mnist', 'train-images-idx3-ubyte.npy')
data = np.load(mnist_path).astype('float') / 255
train = data
tp = NetTrainParams()
tp.maxepoch=200
tp.batchsize=100
tp.eta = scalar_schedule.ConstantSchedule(0.01)
tp.mu  = scalar_schedule.ConstantSchedule(0.5)
arch = [784, 100, 784]
lts=['Logistic', 'Linear']
regs = [NetReg() for lt in lts]
for reg in regs:
    reg.dropout=True
    reg.drop_rate = 0.5
    reg.max_constraint = True
    reg.max_unit_weight = 10
regs[0].drop_rate = 0.2
net = Net(arch, lts, regs)
train_sgd(net, train, train, tp)

Wimg = pydeep.utils.utils.tile_raster_images(net.layers[0].W.transpose(), (28,28), (10,10))

plt.figure(1)
plt.imshow(Wimg)
plt.set_cmap('gray')
plt.axis('off')
plt.show()


import os
import multiprocessing

input_data_path = "/data/disk2/private/zhx/scm/data/pretrain"
output_data_path = "/data/disk2/private/zhx/scm/data/pretrain_bin"
os.makedirs(output_data_path,exist_ok=True)

num_files = 100
num_process = 5

q = multiprocessing.Queue()
bq = multiprocessing.Queue()

for a in range(1, num_files + 1):
    q.put(a)

def work():
    while True:
        a = q.get(timeout=5)

        os.system("""
        python3 create_instances_fast.py --input_file_prefix %s/%d --output_file %s/%d --vocab_file ../utils/word2id.txt --dupe_factor 1 --max_seq_length 512 --max_predictions_per_seq 80 --random_seed 0
""" % (input_data_path, a, output_data_path, a))

        bq.put(1)

if __name__ == "__main__":
    process_list = []
    for a in range(0, num_process):
        process = multiprocessing.Process(target=work)
        process_list.append(process)
        process_list[-1].start()

    done = 0

    while done < num_files:
        _ = bq.get()

        done += _

        print("%d/%d\r" % (done, num_files), end="")


from django.test import TestCase
from django.db.utils import IntegrityError
from django.core.exceptions import ValidationError

from django.contrib.auth.models import User
from userprofile.models import Profile

from project.tests.factories.project import ProjectFactory
from userprofile.tests.factories.profile import ProfileFactory
from userprofile.tests.factories.user import UserFactory

from faker import Factory

from utilities import USER_TYPES, MANAGER

faker = Factory.create()

class UserProfileTest(TestCase):
    @classmethod
    def setUpTestData(cls):
        user = UserFactory()
        profile = ProfileFactory(user=user, project=None)

    def setUp(self):
        self.user = User.objects.last()
        self.profile = Profile.objects.last()

    def test_happy_cases(self):
        # Profiles linked with single user
        profile_count = Profile.objects.filter(user=self.user).count()
        self.assertEqual(profile_count, 1)
        # Choices allowed for Profile Types (designation)
        choices = self.profile._meta.get_field("designation").choices
        self.assertEqual(
            choices,
            USER_TYPES + ((MANAGER, "Manager"),),
        )
        # Adding Project to profile and verifying its added
        project = ProjectFactory()
        project.save()
        self.profile.project = project
        self.profile.save()
        self.assertEqual(self.profile.project.name, project.name)
        # Checking if object name is same as set by str method
        expected_object_name = f"{self.profile.user.username}"
        self.assertEqual(str(self.profile), expected_object_name)
        # Verifying url the is got reverse operation is correct
        self.assertEqual(
            self.profile.get_absolute_url(), f"/users/{self.profile.user.id}"
        )

    def test_no_user_profile(self):
        try:
            ProfileFactory(user=None, project=None)
            self.fail("Profile existing without user")
        except IntegrityError as e:
            pass

    def test_link_mutliple_profiles(self):
        try:
            ProfileFactory(user=self.user, project=None, designation=MANAGER)
            self.fail("Duplicate profile created for same user")
        except Exception as e:
            self.assertEqual(Profile.objects.all().count(), User.objects.all().count())

    def test_custom_designation_for_user(self):
        try:
            self.profile.designation = faker.word(['random', 'words', 'for', 'design', 'ation'])
            self.profile.full_clean()
            self.profile.save()
            self.fail("Random Designation Assigned")
        except ValidationError as e:
            pass


import numpy as np
import os
from keras.preprocessing.image import image

def preprocessing(path, X, y):
    img_list = os.listdir(path)
    for img_name in img_list:
        img_path = path + '/' + img_name
        img = image.load_img(img_path, color_mode="grayscale", target_size=(150, 150))
        img_tensor = image.img_to_array(img)
        X.append(img_tensor)
        if path.split("/")[-1] == "NORMAL":
            y.append([0])
        else:
            y.append([1])

def save_data(X_train, X_val, X_test, y_train, y_val, y_test):
    np.save("D:/dataset/train/X_train.npy", X_train)
    np.save("D:/dataset/val/X_val.npy", X_val)
    np.save("D:/dataset/test/X_test.npy", X_test)
    np.save("D:/dataset/train/y_train.npy", y_train)
    np.save("D:/dataset/val/y_val.npy", y_val)
    np.save("D:/dataset/test/y_test.npy", y_test)

train_normal_path = "D:/X-Ray image/train/NORMAL"
train_pneumonia_path = "D:/X-Ray image/train/PNEUMONIA"
val_normal_path = "D:/X-Ray image/val/NORMAL"
val_pneumonia_path = "D:/X-Ray image/val/PNEUMONIA"
test_normal_path = "D:/X-Ray image/test/NORMAL"
test_pneumonia_path = "D:/X-Ray image/test/PNEUMONIA"

X_train, y_train = [], []
X_val, y_val = [], []
X_test, y_test = [], []

preprocessing(train_normal_path, X_train, y_train)
preprocessing(train_pneumonia_path, X_train, y_train)
X_train = np.array(X_train)
y_train = np.array(y_train)

preprocessing(val_normal_path, X_val, y_val)
preprocessing(val_pneumonia_path, X_val, y_val)
X_val = np.array(X_val)
y_val = np.array(y_val)

preprocessing(test_normal_path, X_test, y_test)
preprocessing(test_pneumonia_path, X_test, y_test)
X_test = np.array(X_test)
y_test = np.array(y_test)

X_train_mean = np.mean(X_train)
X_train_std = np.std(X_train)
X_train = (X_train - X_train_mean) / X_train_std
X_val = (X_val - X_train_mean) / X_train_std
X_test = (X_test - X_train_mean) / X_train_std

save_data(X_train, X_val, X_test, y_train, y_val, y_test)


import os
import numpy as np
import pandas as pd
from .initialization_functions import pprint
import matplotlib.pyplot as plt
from simsopt.geo import (CurveLength, CurveCurveDistance, MeanSquaredCurvature,
                        LpCurveCurvature, ArclengthVariation)
from simsopt.objectives import SquaredFlux
from simsopt.objectives import QuadraticPenalty
from simsopt.geo import curves_to_vtk
from scipy.optimize import minimize

def form_stage_2_objective_function(surf, bs, base_curves, curves, inputs):
    # Define the individual terms in the objective function
    Jf = SquaredFlux(surf, bs)
    Jls = [CurveLength(c) for c in base_curves]
    Jccdist = CurveCurveDistance(curves, inputs.CC_THRESHOLD, num_basecurves=inputs.ncoils)
    # Jcsdist = CurveSurfaceDistance(curves, surf, inputs.CS_THRESHOLD)
    class Jcsdist:
        def __init__(self) -> None: pass
        def shortest_distance(self): return 0

    Jmscs = [MeanSquaredCurvature(c) for c in base_curves]

    if inputs.QAorQHorQIorCNT == 'CNT':
        CURVATURE_THRESHOLD_ARRAY = [inputs.CURVATURE_THRESHOLD_CNT_BIG,inputs.CURVATURE_THRESHOLD]
        Jcs = [LpCurveCurvature(c, 2, CURVATURE_THRESHOLD_ARRAY[i]) for i, c in enumerate(base_curves)]
        MSC_THRESHOLD_ARRAY = [inputs.MSC_THRESHOLD_CNT_BIG,inputs.MSC_THRESHOLD]
        J_MSC = inputs.MSC_WEIGHT * sum(QuadraticPenalty(J, MSC_THRESHOLD_ARRAY[i]) for i, J in enumerate(Jmscs))
        LENGTH_THRESHOLD_ARRAY = [inputs.LENGTHBOUND_CNT_BIG,inputs.LENGTHBOUND]
        J_LENGTH_PENALTY = inputs.LENGTH_CON_WEIGHT * sum([QuadraticPenalty(Jls[i], LENGTH_THRESHOLD_ARRAY[i]) for i in range(len(base_curves))])
    else:
        Jcs = [LpCurveCurvature(c, 2, inputs.CURVATURE_THRESHOLD) for c in base_curves]
        J_MSC = inputs.MSC_WEIGHT * sum(QuadraticPenalty(J, inputs.MSC_THRESHOLD) for J in Jmscs)
        J_LENGTH_PENALTY = inputs.LENGTH_CON_WEIGHT * sum(QuadraticPenalty(Jls[i], inputs.LENGTHBOUND) for i in range(len(base_curves)))

    Jals = [ArclengthVariation(c) for c in base_curves]
    # Form the total objective function. To do this, we can exploit the
    # fact that Optimizable objects with J() and dJ() functions can be
    # multiplied by scalars and added
    J_LENGTH = inputs.LENGTH_WEIGHT * sum(Jls)
    J_CC = inputs.CC_WEIGHT * Jccdist
    # J_CS = inputs.CS_WEIGHT * Jcsdist
    class J_CS:
        def __init__(self) -> None: pass
        def J(self): return 0
        def dJ(self): return 0
    J_CURVATURE = inputs.CURVATURE_WEIGHT * sum(Jcs)
    J_ALS = inputs.ARCLENGTH_WEIGHT * sum(Jals)

    JF_simple = Jf + J_LENGTH_PENALTY + J_MSC + J_CC

    JF = Jf + J_ALS + J_CC + J_CURVATURE + J_MSC + J_LENGTH_PENALTY# + J_LENGTH + J_CS

    return JF_simple, JF, Jls, Jmscs, Jccdist, Jcsdist(), Jf, J_LENGTH, J_CC, J_CS(), J_CURVATURE, J_MSC, J_ALS, J_LENGTH_PENALTY

def inner_coil_loop(mpi, JF_simple, JF, Jls, Jmscs, Jccdist, Jcsdist, Jf, J_LENGTH, J_CC, J_CS, J_CURVATURE, J_MSC, J_ALS, J_LENGTH_PENALTY, vmec, curves, base_curves, surf, coils_results_path, number_vmec_dofs, bs, max_mode, inputs, figures_results_path, surf_full_boundary, write_coils=True):

    def fun_coils_simple(dofss, info, oustr_dict=[]):
        info['Nfeval'] += 1
        if info['Nfeval'] == 2: pprint('Iteration #: ', end='', flush=True)
        pprint(info['Nfeval'], ' ', end='', flush=True)
        JF.x = dofss
        J = JF_simple.J()
        grad = JF_simple.dJ()
        if mpi.proc0_world:
            jf = Jf.J()
            BdotN = np.mean(np.abs(np.sum(bs.B().reshape((inputs.nphi, inputs.ntheta, 3)) * surf.unitnormal(), axis=2)))
            outstr = f"\nfun_coils_simple#{info['Nfeval']} - J={J:.1e}, Jf={jf:.1e}, ⟨B·n⟩={BdotN:.1e}"
            dict1 = {}
            dict1.update({
                'Nfeval': info['Nfeval'], 'J':float(J), 'Jf': float(jf),'J_length':float(J_LENGTH.J()),
                'J_CC':float(J_CC.J()),'J_CURVATURE':float(J_CURVATURE.J()), 'J_LENGTH_PENALTY': float(J_LENGTH_PENALTY.J()),#,'J_CS':float(J_CS.J())
                'J_MSC':float(J_MSC.J()), 'J_ALS':float(J_ALS.J()), 'Lengths':float(sum(j.J() for j in Jls)),
                'curvatures':float(np.sum([np.max(c.kappa()) for c in base_curves])),'msc':float(np.sum([j.J() for j in Jmscs])),
                'B.n':float(BdotN),
                #'gradJcoils':float(np.linalg.norm(JF.dJ())),
                'C-C-Sep':float(Jccdist.shortest_distance())#, 'C-S-Sep':float(Jcsdist.shortest_distance())
            })
            if inputs.debug_coils_outputtxt:
                # outstr += f", ║∇J coils║={np.linalg.norm(JF.dJ()):.1e}"
                outstr += f", C-C-Sep={Jccdist.shortest_distance():.2f}"#, C-S-Sep={Jcsdist.shortest_distance():.2f}
                outstr += f" Jf={jf:.1e}, J_length={J_LENGTH.J():.1e}, J_CC={(J_CC.J()):.1e}, J_CURVATURE={J_CURVATURE.J():.1e}, J_MSC={J_MSC.J():.1e}, J_ALS={J_ALS.J():.1e}, J_LENGTH_PENALTY={J_LENGTH_PENALTY.J():.1e}"#, J_CS={J_CS.J():.1e}
                cl_string = ", ".join([f"{j.J():.1f}" for j in Jls])
                kap_string = ", ".join(f"{np.max(c.kappa()):.1f}" for c in base_curves)
                msc_string = ", ".join(f"{j.J():.1f}" for j in Jmscs)
                outstr += f" Coil lengths=sum([{cl_string}])={sum(j.J() for j in Jls):.1f}, curvature=[{kap_string}], mean squared curvature=[{msc_string}]"
            with open(inputs.debug_output_file, "a") as myfile:
                myfile.write(outstr)
            oustr_dict.append(dict1)
        return J, grad

    def fun_coils(dofss, info, oustr_dict=[]):
        info['Nfeval'] += 1
        if info['Nfeval'] == 2: pprint('Iteration #: ', end='', flush=True)
        pprint(info['Nfeval'], ' ', end='', flush=True)
        JF.x = dofss
        J = JF.J()
        grad = JF.dJ()
        if mpi.proc0_world:
            jf = Jf.J()
            BdotN = np.mean(np.abs(np.sum(bs.B().reshape((inputs.nphi, inputs.ntheta, 3)) * surf.unitnormal(), axis=2)))
            outstr = f"\nfun_coils#{info['Nfeval']} - J={J:.1e}, Jf={jf:.1e}, ⟨B·n⟩={BdotN:.1e}"
            dict1 = {}
            dict1.update({
                'Nfeval': info['Nfeval'], 'J':float(J), 'Jf': float(jf),'J_length':float(J_LENGTH.J()),
                'J_CC':float(J_CC.J()),'J_CURVATURE':float(J_CURVATURE.J()), 'J_LENGTH_PENALTY': float(J_LENGTH_PENALTY.J()),#,'J_CS':float(J_CS.J())
                'J_MSC':float(J_MSC.J()), 'J_ALS':float(J_ALS.J()), 'Lengths':float(sum(j.J() for j in Jls)),
                'curvatures':float(np.sum([np.max(c.kappa()) for c in base_curves])),'msc':float(np.sum([j.J() for j in Jmscs])),
                'B.n':float(BdotN),
                # 'gradJcoils':float(np.linalg.norm(JF.dJ())),
                # 'C-S-Sep':float(Jcsdist.shortest_distance())
                'C-C-Sep':float(Jccdist.shortest_distance())
            })
            if inputs.debug_coils_outputtxt:
                # outstr += f", ║∇J coils║={np.linalg.norm(JF.dJ()):.1e}"
                outstr += f", C-C-Sep={Jccdist.shortest_distance():.2f}"#, C-S-Sep={Jcsdist.shortest_distance():.2f}"
                outstr += f" Jf={jf:.1e}, J_length={J_LENGTH.J():.1e}, J_CC={(J_CC.J()):.1e}, J_CURVATURE={J_CURVATURE.J():.1e}, J_MSC={J_MSC.J():.1e}, J_ALS={J_ALS.J():.1e}, J_LENGTH_PENALTY={J_LENGTH_PENALTY.J():.1e}"#, J_CS={J_CS.J():.1e}
                cl_string = ", ".join([f"{j.J():.1f}" for j in Jls])
                kap_string = ", ".join(f"{np.max(c.kappa()):.1f}" for c in base_curves)
                msc_string = ", ".join(f"{j.J():.1f}" for j in Jmscs)
                outstr += f" Coil lengths=sum([{cl_string}])={sum(j.J() for j in Jls):.1f}, curvature=[{kap_string}], mean squared curvature=[{msc_string}]"
            with open(inputs.debug_output_file, "a") as myfile:
                myfile.write(outstr)
            oustr_dict.append(dict1)
        return J, grad

    dofs = np.concatenate((JF.x, vmec.x))
    oustr_dict=[]
    if write_coils:
        bs.set_points(surf_full_boundary.gamma().reshape((-1, 3)))
        curves_to_vtk(curves, os.path.join(coils_results_path,f"curves_before_inner_loop_max_mode_{max_mode}"))
        pointData = {"B_N": np.sum(bs.B().reshape((inputs.nphi, inputs.ntheta, 3)) * surf_full_boundary.unitnormal(), axis=2)[:, :, None]}
        surf_full_boundary.to_vtk(os.path.join(coils_results_path, f"surf_before_inner_loop_max_mode_{max_mode}"), extra_data=pointData)
    pprint(f'\n  Running simple intermediate coil loop with {inputs.MAXITER_stage_2_simple} iterations:')
    info_simple={'Nfeval':0}
    bs.set_points(surf.gamma().reshape((-1, 3)))
    res = minimize(fun_coils_simple, dofs[:-number_vmec_dofs], jac=True, args=(info_simple,oustr_dict), method='L-BFGS-B', options={'maxiter': inputs.MAXITER_stage_2_simple, 'maxcor': 300}, tol=1e-10)
    dofs[:-number_vmec_dofs] = res.x
    JF.x = dofs[:-number_vmec_dofs]
    pprint(f'\n  Running more complete intermediate coil loop with {inputs.MAXITER_stage_2} iterations:')
    info_not_simple={'Nfeval':0}
    bs.set_points(surf.gamma().reshape((-1, 3)))
    res = minimize(fun_coils, dofs[:-number_vmec_dofs], jac=True, args=(info_not_simple,oustr_dict), method='L-BFGS-B', options={'maxiter': inputs.MAXITER_stage_2, 'maxcor': 300}, tol=1e-10)
    dofs[:-number_vmec_dofs] = res.x
    JF.x = dofs[:-number_vmec_dofs]
    if write_coils:
        curves_to_vtk(curves, os.path.join(coils_results_path,f"curves_after_inner_loop_max_mode_{max_mode}"))
        bs.set_points(surf_full_boundary.gamma().reshape((-1, 3)))
        pointData = {"B_N": np.sum(bs.B().reshape((inputs.nphi, inputs.ntheta, 3)) * surf_full_boundary.unitnormal(), axis=2)[:, :, None]}
        surf_full_boundary.to_vtk(os.path.join(coils_results_path, f"surf_after_inner_loop_max_mode_{max_mode}"), extra_data=pointData)
        bs.set_points(surf.gamma().reshape((-1, 3)))
        bs.save(os.path.join(coils_results_path,f"biot_savart_inner_loop_max_mode_{max_mode}.json"))
        df = pd.DataFrame(oustr_dict)
        df.to_csv(f'output_stage2_max_mode_{max_mode}.csv', index_label='index')
        ax=df.plot(kind='line', logy=True, y=['J','Jf','J_length','J_CC','J_CURVATURE','J_MSC','J_ALS','J_LENGTH_PENALTY','C-C-Sep'], linewidth=0.8)
        ax.set_ylim(bottom=1e-9, top=None)
        ax.set_xlabel('Number of function evaluations')
        ax.set_ylabel('Objective function')
        plt.axvline(x=info_simple['Nfeval'], linestyle='dashed', color='k', label='simple-loop', linewidth=0.8)
        plt.legend(loc=3, prop={'size': 6})
        plt.tight_layout()
        plt.savefig(os.path.join(figures_results_path, f'optimization_stage2_max_mode_{max_mode}.pdf'), bbox_inches = 'tight', pad_inches = 0)

    return dofs, bs, JF


# -*- coding: utf-8 -*-
"""
Created on Mon Jul 25 16:25:28 2022

Example of how to setup and run the Head deformation code

@author: Mark Gardner

Last Edited: 25/07/2022

"""

import json
from pathlib import Path
import sys

def ChangeJsonFile(InfoFile,NewVars):
    '''
    Function for writing data to the example json files
    '''
    with open(InfoFile) as json_file:
        data = json.load(json_file)

    for key in NewVars:
        data[key] = NewVars[key]

    with open(InfoFile,'w') as json_file:
        json.dump(data,json_file)

'''
For testing/ example purposes it's safest to manually append the path variable to ensure our
package will always be found. This isn't necessary once we actually install the package
because installation in python essentially means "copying the package to a place where it can be found"
'''
this_file_loc = Path(__file__)
sys.path.insert(0, str(this_file_loc.parent.parent))
'''
Now we can guarantee that the package will be found we can import it:
'''

from DeformHeadCT.DeformVolume import DeformationScript
'''
 Do a simple one axis head rotation
'''

'''
Define the location of the json file
'''
InfoFile = 'examples/OneAxisRotation.json'
elastixParamFile = 'examples/Elastix_BSpline_OpenCL_RigidPenalty.txt'

'''
Change the template json file variables to input the location of the dicom files
'''
#NewVars = {}

#NewVars['name'] = ''
#NewVars['InputDirectory'] = ""
#NewVars['OutputDirectory'] = ''

#ChangeJsonFile(InfoFile,NewVars)

'''
Run the function
'''
DeformationScript(InfoFile,RegParamFile=elastixParamFile)


from bs4 import BeautifulSoup
import requests

# Set the base URL for the website
base_url = "https://ec.europa.eu"

# Set the initial URL
url = "https://ec.europa.eu/clima/ets/transaction.do?languageCode=fr&startDate=&endDate=&transactionStatus=4&fromCompletionDate=&toCompletionDate=&transactionID=&transactionType=-1&suppTransactionType=-1&originatingRegistry=-1&destinationRegistry=-1&originatingAccountType=-1&destinationAccountType=-1&originatingAccountIdentifier=&destinationAccountIdentifier=&originatingAccountHolder=&destinationAccountHolder=&search=Search&currentSortSettings="

# Set a flag to control the loop
done = False

# Loop until the flag is set to True
while not done:
    # Make a request to the website
    response = requests.get(url)

    # Parse the HTML content
    soup = BeautifulSoup(response.content, "html.parser")

    # Find all tables on the page
    tables = soup.find_all("table")

    # Iterate through the tables and print their data
    for table in tables:
        rows = table.find_all("tr")
        for row in rows:
            cells = row.find_all("td")
            for cell in cells:
                print(cell.text)

    # Find the "nextList" button
    next_button = soup.find("a", {"title": "Go to the next page"})
    if next_button:
        # If the "nextList" button is found, get its href attribute
        next_url = next_button["href"]
        # Concatenate the base URL and the href attribute to get the full URL
        url = base_url + next_url
    else:
        # If the "nextList" button is not found, set the flag to True to exit the loop
        done = True


from flask import Flask, render_template, request
import openai
import os
from dotenv import load_dotenv
load_dotenv()
import regex as re

app = Flask(__name__)

# Set up OpenAI API credentials
openai.api_key = os.environ["OPENAI_API_KEY"]

# Initialize conversation list
conversation = [{'role':'system', 'content':"""
You are a Language tutor for teaching human speaking languages.
Your first task is to greet and ask the my name.
You ask which language do I want to learn.

Give me options like [Spanish , French , German , Russian  or ask their personal choises of languages]

...wait for my response
You then ask my proficiecy level.

... wait for my response
Based on my language prefernce prepare a course for that language

start the course by describing how you are going to teach

Strictly follow the below steps
give examples at every step and then provide few exercise for practise in each conversation
give two line space before staring the exercise for clearner paragraph
Generate google translate link for the words used for prefered language by me
strictly print those links in form of JSON format at the last of every conversation

"""}]

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

@app.route('/get_response', methods=['POST'])
def get_response():
    # Get user input from the form
    user_input = request.form['user_input']

    # Append user input to conversation
    conversation.append({'role':'user', 'content':f"{user_input}"})

    # Get response from OpenAI API
    response = openai.ChatCompletion.create(
        model = 'gpt-3.5-turbo',
        temperature=0.0,
        messages = conversation,
    )

    # Append OpenAI's response to conversation
    assistant_response = response.choices[0].message["content"]

    #extracting link using regex
    deli = str(assistant_response)
    print(deli)
    stri = re.findall('\{.*\}' , deli)
    print(stri)
    if len(stri) == 0:
        print("None")
    else:
        pattern = r'"([^"]*)"'
        links = re.findall(pattern, stri[0])
        print(links)

    # senti = openai.ChatCompletion.create(
    #     model = 'gpt-3.5-turbo',
    #     temperature=0.0,
    #     messages = [{'role':'system', 'content':f"""If the paragraph given in triple quotes '''{assistant_response}''' is of type that it has no acces to the data then give output "NO" """}],
    # )
    # sentiment = senti.choices[0].message["content"]
    # print(sentiment)

    if len(conversation) == 7:
        conversation.pop(1)
        conversation.pop(2)

    conversation.append({'role':'assistant', 'content':f"{assistant_response}"})

    return {'response': assistant_response}

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


from os.path import join
import time
from kivy.lang import Builder
from kivy.properties import ListProperty, StringProperty
from application.screens.sign_detection.base_sign_detection_screen import BaseSignDetectorScreen
from application.database.json_manager import JsonManager
from constants import kv_files_path, light_green_color

class SignDetectionWithSelectorScreen(BaseSignDetectorScreen):

    spinner_values = ListProperty([])
    window_background = ListProperty(light_green_color)
    reference_image_source = StringProperty('')

    Builder.load_file(join(kv_files_path, 'sign_detection_selection_screen.kv'))

    def __init__(self, screen_name, screen_manager, lesson_name, sign_visual_dict, sign_type, **kwargs):
        super().__init__(screen_name, screen_manager, sign_type, **kwargs)
        self.sign_visual_dict = sign_visual_dict
        self.lesson_name = lesson_name
        self.spinner_values = list(self.sign_visual_dict.keys())
        self.num_errors = 0
        self.start_screen_time = time.time()
        self.__bind_buttons_to_stop_timer()
        self.__disable_start_button()

    def initialize_instructions(self):
        self.update_instructions('Select a sign to recognize and press "Start Detection"')

    def spinner_selection(self, selected_sign):
        self.ids.signs_spinner.label_text = selected_sign
        self.expected_sign = selected_sign
        self.reference_image_source = self.sign_visual_dict[selected_sign]
        self.__enable_start_button()

    def __bind_buttons_to_stop_timer(self):
        button_previous = self.screen_manager.action_bar.ids.previous
        button_main = self.screen_manager.action_bar.ids.main
        button_previous.bind(on_release=self.stop_time)
        button_main.bind(on_release=self.stop_time)

    def stop_time(self, *args):
        json_manager = JsonManager()
        practise_time = time.time() - self.start_screen_time
        json_manager.add_practise_time(self.lesson_name, practise_time)

        button_previous = self.screen_manager.action_bar.ids.previous
        button_main = self.screen_manager.action_bar.ids.main
        button_previous.unbind(on_release=self.stop_time)
        button_main.unbind(on_release=self.stop_time)

    def __disable_start_button(self):
        button = self.ids.start_detection_button
        button.disabled = True

    def __enable_start_button(self):
        button = self.ids.start_detection_button
        if button.disabled:
            button.disabled = False


from auth.constants import FAILED, PROCESSING, SUCCEEDED
from auth.core.ticket import TicketFactory
from auth.core.ticket_notification import TicketNotice
from auth.core.ticket_objects import BaseTicketSerializer
from auth.core.ticket_serializer import TicketStateSerializer
from auth.models import Ticket
from auth.models.ticket_models import TicketState
from auth.utils.serializer import DataPageNumberPagination
from auth.views.ticket_serializer import (
    CommonTicketCreateSerializer,
    TicketQuerySerializer,
    TicketStateApproveSerializer,
    TicketStateQuerySerializer,
    TicketStateRetrieveSerializer,
    TicketWithdrawSerializer,
    WXRecallTicketStateApproveSerializer,
)
from common.decorators import detail_route, list_route, params_valid
from common.views import APIModelViewSet
from django.db.models import F
from rest_framework.response import Response

from common import local

class TicketViewSet(APIModelViewSet):
    queryset = Ticket.objects.none()
    serializer_class = BaseTicketSerializer
    pagination_class = DataPageNumberPagination

    # 单据内容暂时不需要加上权限控制
    def get_queryset(self):
        return Ticket.objects.all()

    def serialize_queryset(self, query_params):
        """
        序列化query_set
        @param query_params:
        @return:
        """
        queryset = self.get_queryset().filter(**query_params).all()

        page = self.paginate_queryset(queryset)
        if page is not None:
            data = TicketFactory.serialize_ticket_queryset(page, many=True)
            return self.get_paginated_response(data)

        data = TicketFactory.serialize_ticket_queryset(queryset, many=True)
        return Response(data)

    @params_valid(TicketQuerySerializer, add_params=False)
    def list(self, request, *args, **kwargs):
        """
        @api {get} /v3/auth/tickets/ 获取单据列表
        @apiName ticket_list
        @apiGroup Ticket
        @apiParam {boolean} [is_creator] 是否为创建人
        @apiParam {boolean} [is_processor] 是否为处理候选人
        @apiParam {boolean} [is_processed_by] 是否为处理人
        @apiParam {string} [process_id] 流程ID，仅对通用单据生效，比如 verify_tdm_data、batch_recalc
        @apiSuccessExample {Json} 成功返回
        [
            {
                "id": 41,
                "process_length": 1,
                "ticket_type": "project_biz",
                "created_by": "admin1234",
                "created_at": "2018-12-18 11:59:22",
                "reason": "hello world",
                "status": "succeeded",
                "end_time": "2018-12-18 12:06:25",
                "extra": "{}",
                "process_step": 1,
                "permissions": [
                    {
                        "id": 54,
                        "ticket": "Ticket object",
                        "key": "result_table_id",
                        "value": "591_test_rt",
                        "scope": {
                            "result_table_name": "test_rt",
                            "result_table_info": {
                                "id": "591_test_rt",
                                "name": "test_rt"
                            },
                            "result_table_id": "591_test_rt"
                        },
                        "subject_id": "1",
                        "subject_name": "数据平台对内版",
                        "subject_class": "project",
                        "action": "result_table.query_data",
                        "object_class": "result_table"
                    }
                ],
                "ticket_type_display": "项目申请业务数据",
                "status_display": "成功"
            }
        ]
        """
        username = local.get_request_username()
        status = request.cleaned_params.get("status")
        ticket_type = request.cleaned_params.get("ticket_type")
        process_id = request.query_params.get("process_id")

        query_params = {}
        if request.cleaned_params.get("is_creator"):
            query_params["created_by"] = username
        if request.query_params.get("is_processor"):
            query_params["states__processors__contains"] = Ticket.generate_sub_member(username)
        if request.query_params.get("is_processed_by"):
            query_params["states__processed_by"] = username
        if request.query_params.get("present"):
            query_params["states__process_step__lte"] = F("process_step")
        if status:
            query_params["status"] = status
        if ticket_type:
            query_params["ticket_type"] = ticket_type
        if process_id:
            query_params["process_id"] = process_id

        return self.serialize_queryset(query_params)

    def create(self, request, *args, **kwargs):
        """
        @api {post} /v3/auth/tickets/ 创建单据
        @apiName create_ticket
        @apiGroup Ticket
        @apiParam {String} reason 创建原因
        @apiParam {String} ticket_type 单据类型
        @apiParam {Json} permissions 申请的权限列表
        @apiParamExample {Json} 项目申请结果数据样例
            {
                "reason": "hello world",
                "ticket_type": "project_biz",
                "permissions": [
                    {
                        "subject_id": "1",
                        "subject_class": "project",
                        "action": "result_table.query_data",
                        "object_class": "result_table",
                        "scope": {
                            "result_table_id": "591_test_rt"
                        }
                    }
                ]
            }
        @apiParamExample {Json} 项目申请资源组样例
            {
                "reason": "hello world",
                "ticket_type": "use_resource_group",
                "permissions": [
                    {
                        "subject_id": "1",
                        "subject_class": "project_info",
                        "action": "resource_group.use",
                        "object_class": "resource_group",
                        "scope": {
                            "resource_group_id": "tgqa"
                        }
                    }
                ]
            }
        @apiSuccessExample {Json} 项目申请结果数据返回样例结果
            {
                "id": 45,
                "process_length": 1,
                "ticket_type": "project_biz",
                "created_by": "admin1234",
                "created_at": "2018-12-21 11:55:39",
                "reason": "hello world",
                "status": "processing",
                "end_time": null,
                "extra": null,
                "process_step": 0,
                "permissions": [
                    {
                        "id": 58,
                        "ticket": "Ticket object",
                        "key": "result_table_id",
                        "value": "591_test_rt",
                        "scope": {
                            "result_table_name": "test_rt",
                            "result_table_info": {
                                "id": "591_test_rt",
                                "name": "test_rt"
                            },
                            "result_table_id": "591_test_rt"
                        },
                        "subject_id": "1",
                        "subject_name": "数据平台对内版",
                        "subject_class": "project",
                        "action": "result_table.query_data",
                        "object_class": "result_table"
                    }
                ],
                "ticket_type_display": "项目申请业务数据",
                "status_display": "处理中"
            }
        """
        data = request.data.copy()
        data["created_by"] = local.get_request_username()
        return Response(TicketFactory(data["ticket_type"]).generate_ticket_data(data))

    def retrieve(self, request, *args, **kwargs):
        """
        @api {get} /v3/auth/tickets/:ticket_id/ 查询单据
        @apiName ticket_query
        @apiGroup Ticket
        @apiSuccessExample {Json} 成功返回 Response.data
            {
                "id": 45,
                "process_length": 1,
                "ticket_type": "project_biz",
                "created_by": "admin1234",
                "created_at": "2018-12-21 11:55:39",
                "reason": "hello world",
                "status": "processing",
                "end_time": null,
                "extra": "{}",
                "process_step": 0,
                "permissions": [
                    {
                        "id": 58,
                        "ticket": "Ticket object",
                        "key": "result_table_id",
                        "value": "591_test_rt",
                        "scope": {
                            "result_table_name": "test_rt",
                            "result_table_info": {
                                "id": "591_test_rt",
                                "name": "test_rt"
                            },
                            "result_table_id": "591_test_rt"
                        },
                        "subject_id": "1",
                        "subject_name": "数据平台对内版",
                        "subject_class": "project",
                        "action": "result_table.query_data",
                        "object_class": "result_table"
                    }
                ],
                "ticket_type_display": "项目申请业务数据",
                "status_display": "处理中"
            }
        """
        ticket = self.get_object()
        data = TicketFactory.serialize_ticket_queryset(ticket)
        return Response(data)

    @list_route(methods=["get"])
    def ticket_types(self, request):
        """
        @api {get} /v3/auth/tickets/ticket_types/ 获取单据类型
        @apiName ticket_types
        @apiGroup Ticket
        @apiSuccessExample {Json} 成功返回
        [
            {id: 'APP', name: '业务数据'},
        ]
        """
        return Response(TicketFactory.list_ticket_types())

    @list_route(methods=["get"])
    def ticket_status(self, request):
        """
        @api {get} /v3/auth/tickets/ticket_status/ 获取单据状态列表
        @apiName ticket_types
        @apiGroup Ticket
        @apiSuccessExample {Json} 成功返回
        [
            {id: 'processing', name: '处理中'},
        ]
        """
        return Response(TicketFactory.list_ticket_status())

    @list_route(methods=["post"])
    @params_valid(CommonTicketCreateSerializer, add_params=False)
    def create_common(self, request):
        """
        @api {post} /v3/auth/tickets/create_common/ 创建单据
        @apiName create_common_ticket
        @apiGroup Ticket
        @apiParam {String} reason 创建原因
        @apiParam {String} ticket_type 单据类型
        @apiParam {String} process_id 任务ID
        @apiParam {String} content 任务描述
        @apiParam {String} callback_url 回调 URL，审批结果会通过 callback_url 进行回调
        @apiParam {Dict} [ticket_step_params] 单据步骤参数
        @apiParam {String} [auto_approve] 是否自动审批
        @apiParamExample {Json} verify_tdm_data 请求示例
            {
                "reason": "hello world",
                "ticket_type": "verify_tdm_data",
                "process_id": "111",
                "content": "用户要接入 TDM XX 表数据，是否允许",
                "callback_url": "http://xxx.com/xxx"
            }
        @apiParamExample {Json} create_resource_group 请求示例
            {
                "reason": "hello world",
                "ticket_type": "create_resource_group",
                "ticket_step_params": {
                    "0&process_scope_id": "591"   # 表示第一步的 process_scope_id 参数赋值为 591
                },
                "process_id": "111",
                "content": "现在要创建 XXX 资源组",
                "callback_url": "http://xxx.com/xxx"
            }
        @apiSuccessExample {Json} 成功返回
            {
                "id": 420,
                "ticket_type_display": "TDM原始数据接入申请",
                "status_display": "处理中",
                "process_length": 1,
                "ticket_type": "verify_tdw_data",
                "created_by": "request_user",
                "created_at": "2019-06-24 15:57:42",
                "reason": "test",
                "status": "processing",
                "end_time": null,
                "process_step": 0,
                "processors": ["admin"],
                "state_id": 358,
                "extra_info": {
                    "content": "用户要接入 TDM XX 表数据，是否允许",
                    "process_id": "111",
                    "callback_url": "http://xxx.com/xxx"
                },
                "permission": []
            }
        @apiSuccessExample {Json} 回调示例
            curl -XPOST http://xxx.com/xxx -d
            {
                "operator": "user01",
                "message": "ok",
                "process_id": "111",
                "status": "succeeded"  # succeeded=通过 | failed=拒绝 | stopped=中止
            }
        """
        data = request.cleaned_params
        data_restr = {}
        data_restr["created_by"] = local.get_request_username()
        data_restr["ticket_type"] = data["ticket_type"]
        data_restr["reason"] = data["reason"]
        data_restr["ticket_step_params"] = data.get("ticket_step_params", {})
        data_restr["auto_approve"] = data.get("auto_approve", True)
        extra_info = {
            "process_id": data["process_id"],
            "ticket_step_params": data_restr["ticket_step_params"],
            "content": data["content"],
            "callback_url": data.get("callback_url"),
        }
        data_restr["extra"] = extra_info
        data_restr["permission"] = []
        res = TicketFactory(data_restr["ticket_type"]).generate_ticket_data(data_restr)[0]
        return Response(res)

    @detail_route(methods=["post"], url_path="withdraw")
    @params_valid(TicketWithdrawSerializer, add_params=False)
    def withdraw(self, request, pk):
        """
        @api {post} /v3/auth/tickets/:id/withdraw/ 撤销单据
        @apiName withdraw_ticket
        @apiGroup Ticket
        @apiParam {String} process_message 处理信息
        @apiSuccessExample {json}
        {
            "id": 74,
            "extra": {
                "content": "对《项目1》->《任务1》的离线结果表 1_xx, 2_xx 进行重算",
                "process_id": "111_xx"
            },
            "ticket_type_display": "离线补录申请",
            "status_display": "已终止",
            "process_length": 1,
            "ticket_type": "batch_recalc",
            "created_by": "request_user",
            "created_at": "2019-06-28 18:10:50",
            "reason": "test",
            "status": "stopped",
            "end_time": "2019-06-28 18:10:50",
            "process_step": 0,
            "processors": ["admin"],
            "state_id": 74,
            "extra_info": {
                "content": "对《项目1》->《任务1》的离线结果表 1_xx, 2_xx 进行重算",
                "process_id": "111_xx"
            }
        }
        """
        ticket = self.get_object()
        username = local.get_request_username()
        result = TicketFactory.withdraw(
            ticket,
            request.cleaned_params["process_message"],
            username,
        )
        return Response(result)

class TicketStateViewSet(APIModelViewSet):
    queryset = TicketState.objects.none()
    serializer_class = TicketStateSerializer
    pagination_class = DataPageNumberPagination

    # 单据内容暂时不需要做权限控制
    def get_queryset(self):
        """
        返回所有审批步骤，需要考虑下是否加上用户过滤环节
        """
        return TicketState.objects.all()

    def serialize_queryset(self, query_params, show_display=False):
        """
        序列化query_set
        @param query_params: 查询参数
        @param show_display: 是否返回显示字段
        @return:
        """
        queryset = self.get_queryset().filter(**query_params).all()

        paged_queryset = self.paginate_queryset(queryset)
        if paged_queryset is not None:
            data = TicketFactory.serialize_state_queryset(paged_queryset, many=True, show_display=show_display)
            return self.get_paginated_response(data)

        data = TicketFactory.serialize_state_queryset(queryset, many=True, show_display=show_display)
        return Response(data)

    @params_valid(TicketStateQuerySerializer, add_params=False)
    def list(self, request, *args, **kwargs):
        """
        @api {get} /v3/auth/ticket_states/ 获取单据列表
        @apiName ticket_state_list
        @apiGroup TicketState
        @apiParam {int} ticket_id 单据标识
        @apiParam {boolean} is_processor 是否为处理候选人
        @apiParam {boolean} is_processed_by 是否为处理人
        @apiSuccessExample {json} 查询结果样例
        [
            {
                "id": 1,
                "processed_at": "2018-12-18 12:06:26",
                "processors": [
                    "processor666"
                ],
                "processed_by": "processor666",
                "process_step": 0,
                "process_message": "hello you are succeeded",
                "status": "succeeded",
                "ticket": {
                    "id": 41,
                    "process_length": 1,
                    "ticket_type": "project_biz",
                    "created_by": "admin1234",
                    "created_at": "2018-12-18 11:59:22",
                    "reason": "hello world",
                    "status": "succeeded",
                    "end_time": "2018-12-18 12:06:25",
                    "extra": "{}",
                    "process_step": 1,
                    "permissions": [
                        {
                            "id": 54,
                            "ticket": "Ticket object",
                            "key": "result_table_id",
                            "value": "591_test_rt",
                            "scope": {
                                "result_table_name": "test_rt",
                                "result_table_info": {
                                    "id": "591_test_rt",
                                    "name": "test_rt"
                                },
                                "result_table_id": "591_test_rt"
                            },
                            "subject_id": "1",
                            "subject_name": "数据平台对内版",
                            "subject_class": "project",
                            "action": "result_table.query_data",
                            "object_class": "result_table"
                        }
                    ],
                    "ticket_type_display": "项目申请业务数据",
                    "status_display": "成功"
                },
                "status_display": "成功"
            }
        ]
        """
        query_params = {}
        username = local.get_request_username()
        status = request.cleaned_params.get("status")
        ticket__ticket_type = request.cleaned_params.get("ticket__ticket_type")
        is_processor = request.cleaned_params.get("is_processor")
        is_processed_by = request.cleaned_params.get("is_processed_by")
        ticket_id = request.cleaned_params.get("ticket_id")
        if ticket_id:
            query_params["ticket_id"] = ticket_id
        if is_processor:
            query_params["processors__contains"] = Ticket.generate_sub_member(username)
            query_params["status__in"] = [PROCESSING, SUCCEEDED, FAILED]
        if status:
            query_params["status"] = status
        if ticket__ticket_type:
            query_params["ticket__ticket_type"] = ticket__ticket_type
        if is_processed_by:
            query_params["processed_by"] = username

        return self.serialize_queryset(query_params)

    @params_valid(TicketStateRetrieveSerializer, add_params=False)
    def retrieve(self, request, pk):
        """
        @api {get} /v3/auth/ticket_states/:ticket_state_id/ 查询单据及关联状态
        @apiName approve_state
        @apiGroup TicketState
        @apiParam {String} status 审批状态
        @apiParam {String} process_message 处理信息
        @apiSuccessExample {json} 审批结果样例
        {
            states: [
                {
                    id: 1702,
                    status_display: "已同意",
                    processed_at: "2019-09-17 15:19:35",
                    processors: [
                        "user01"
                    ],
                    processed_by: "user01",
                    process_step: 1,
                    process_message: "同意",
                    status: "succeeded"
                }
            ],
            "ticket": {
                "id": 42,
                "process_length": 1,
                "ticket_type": "project_biz",
                "created_by": "admin1234",
                "created_at": "2018-12-18 19:47:00",
                "reason": "adf",
                "status": "processing",
                "end_time": null,
                "extra": "{}",
                "process_step": 0,
                "permissions": [
                    {
                        "id": 55,
                        "ticket": "Ticket object",
                        "key": "bk_biz_id",
                        "value": "591",
                        "scope": {
                            "bk_biz_id": "591",
                            "bk_biz_info": {
                                "id": "591",
                                "name": "unknown biz"
                            },
                            "bk_biz_name": "unknown biz"
                        },
                        "subject_id": "sdf",
                        "subject_name": "",
                        "subject_class": "project",
                        "action": "result_table.query_data",
                        "object_class": "biz"
                    }
                ],
                "ticket_type_display": "项目申请业务数据",
                "status_display": "处理中"
            },
            "status_display": "处理中"
        }
        """
        instance = self.get_object()
        instance_data = TicketFactory.serialize_state_queryset(
            instance, ticket_type=instance.ticket.ticket_type, many=False
        )
        data = self.serialize_queryset({"ticket_id": instance_data["ticket"]["id"]}, show_display=True).data
        ticket = {}
        for _data in data:
            ticket = _data.pop("ticket")
        return Response({"ticket": ticket, "states": data})

    @detail_route(methods=["post"], url_path="approve")
    @params_valid(TicketStateApproveSerializer, add_params=False)
    def approve(self, request, pk):
        """
        @api {post} /v3/auth/ticket_states/:id/approve/ 审批单据
        @apiName approve_state
        @apiGroup TicketState
        @apiParam {String} status 审批状态
        @apiParam {String} process_message 处理信息
        @apiSuccessExample {json} 审批结果样例
        {
            "id": 3,
            "processed_at": "2018-12-21 12:10:02",
            "processors": [
                "processor666"
            ],
            "processed_by": "processor666",
            "process_step": 0,
            "process_message": "hello you are succeeded",
            "status": "succeeded",
            "ticket": {
                "id": 43,
                "process_length": 1,
                "ticket_type": "project_biz",
                "created_by": "admin1234",
                "created_at": "2018-12-18 20:02:52",
                "reason": "asdf",
                "status": "succeeded",
                "end_time": "2018-12-21 12:10:02",
                "extra": "{}",
                "process_step": 1,
                "permissions": [
                    {
                        "id": 56,
                        "ticket": "Ticket object",
                        "key": "bk_biz_id",
                        "value": "591",
                        "scope": {
                            "bk_biz_id": "591",
                            "bk_biz_info": {
                                "id": "591",
                                "name": "unknown biz"
                            },
                            "bk_biz_name": "unknown biz"
                        },
                        "subject_id": "213",
                        "subject_name": "goodjob",
                        "subject_class": "project",
                        "action": "result_table.query_data",
                        "object_class": "biz"
                    }
                ],
                "ticket_type_display": "项目申请业务数据",
                "status_display": "成功"
            },
            "status_display": "成功"
        }
        """
        instance = self.get_object()
        username = local.get_request_username()
        result = TicketFactory.approve(
            instance,
            request.cleaned_params["status"],
            request.cleaned_params["process_message"],
            username,
            add_biz_list=True,
        )
        return Response(result)

    @list_route(methods=["post"])
    @params_valid(WXRecallTicketStateApproveSerializer, add_params=False)
    def wx_recall(self, request):
        """
        @api {post} /v3/auth/ticket_states/wx_recall/ 审批单据
        @apiName approve_state
        @apiGroup TicketState
        @apiParam {String} status 审批状态
        @apiParam {String} taskid bkdata:ticket_id:md5(ticket_id+APP_TOKEN)
        @apiSuccessExample {json} 审批结果样例
        {
            "id": 3,
            "processed_at": "2018-12-21 12:10:02",
            "processors": [
                "processor666"
            ],
            "processed_by": "processor666",
            "process_step": 0,
            "process_message": "hello you are succeeded",
            "status": "succeeded",
            "ticket": {
                "id": 43,
                "process_length": 1,
                "ticket_type": "project_biz",
                "created_by": "admin1234",
                "created_at": "2018-12-18 20:02:52",
                "reason": "asdf",
                "status": "succeeded",
                "end_time": "2018-12-21 12:10:02",
                "extra": "{}",
                "process_step": 1,
                "permissions": [
                    {
                        "id": 56,
                        "ticket": "Ticket object",
                        "key": "bk_biz_id",
                        "value": "591",
                        "scope": {
                            "bk_biz_id": "591",
                            "bk_biz_info": {
                                "id": "591",
                                "name": "unknown biz"
                            },
                            "bk_biz_name": "unknown biz"
                        },
                        "subject_id": "213",
                        "subject_name": "goodjob",
                        "subject_class": "project",
                        "action": "result_table.query_data",
                        "object_class": "biz"
                    }
                ],
                "ticket_type_display": "项目申请业务数据",
                "status_display": "成功"
            },
            "status_display": "成功"
        }
        """
        taskid = request.cleaned_params["taskid"]
        ticket_id = int(taskid.split(":")[1])
        TicketNotice().check_taskid(ticket_id, taskid)
        instance = TicketState.objects.filter(ticket=ticket_id, status=PROCESSING).first()
        username = request.cleaned_params["verifier"]
        status = request.cleaned_params["result"]
        result_status = FAILED
        if status == "1":
            result_status = SUCCEEDED
        result = TicketFactory.approve(
            instance, result_status, request.cleaned_params["message"], username, add_biz_list=True
        )
        return Response(result)

    @list_route(methods=["get"])
    def todo_count(self, request):
        return Response(TicketState.todo_count(local.get_request_username()))


import os, sys
import pytest
import re

# Set up script
app_path = os.path.abspath(os.pardir)
print(app_path)
sys.path.append(app_path)

# import the tested and the errors
from ex03.financial import main, TickerNotFound

# Test cases
ARGS_OK = ["", 'msft', 'Total Revenue']
ARGS_BAD_TICKER = ["", 'asldfjhals', "Total Revenue"]

# Test functions
def test_total_revenue():
    assert main(ARGS_OK)[0].lower() == 'Total Revenue'.lower()

def test_is_tuple():
    assert isinstance(main(ARGS_OK), tuple)

def test_invalid_ticker_error():
    # connection error check failed; url comparison ok
    with pytest.raises(TickerNotFound, match='TICKER NOT FOUND', ):
        main(ARGS_BAD_TICKER)

"""
•   If I ask for Total Revenue, do I get the total revenue for the given ticker?
•   Is the type of the return a tuple?
•   If I give an invalid ticker name, do I get an exception?
"""


from Crypto.Util import number
import gmpy2
import random

class Signature:
    def __init__(self, security_parameter):
        self.security_parameter = security_parameter
        ((n,e),(p,q,d)) = self.generateKeyPair()
        self.pubKey = {"n": n, "e": e}
        self.privKey = {"p": p, "q": q, "d": d}

    def gcd(a, b):
        while b != 0:
            (a, b) = (b, a%b)
        return a

    def generateKeyPair(self):
        p = number.getPrime(self.security_parameter)
        q = number.getPrime(self.security_parameter)

        while p == q:
            q = number.getPrime(self.security_parameter)

        n = p * q

        phi_n = (p-1) * (q-1)

        e = random.randint(2, phi_n-1)

        while Signature.gcd(e, phi_n) != 1:
            e = random.randint(2, phi_n-1)

        d = gmpy2.invert(e, phi_n)

        return ((n,e),(p,q,d))

    def signMessage(self, m):
        if type(m) != int:
            raise ValueError("[ERROR] Message to be signed must be an integer type!")

        signature = pow(m, self.privKey["d"], self.privKey["p"] * self.privKey["q"])
        return signature

    def validateSignature(self, m, sign):
        if type(m) != int :
            raise ValueError("[ERROR] Message to be signed must be an integer type!")

        if type(sign) != int:
            raise ValueError("[ERROR] Signature to be verified must be an integer type!")

        m_prime = pow(sign, self.pubKey["e"], self.pubKey["n"])
        return m == m_prime

    def getMessage(self, sign):
        m = pow(sign, self.pubKey["e"], self.pubKey["n"])
        return m

class Predicate_R:
    def __init__(self):
        self.dump = []

    def setRedundancy(self, x):
        self.dump.append(x)

    def checkRedundancy(self, x):
        return x in self.dump


#!/usr/bin/python

""" Transaction Fraud Detection Using Social-network Information
Author: Shifan Mao
Date: 11-06-16 """

import sys
import networkx as nx
import numpy as np
# from graph import Graph

"""
example of executing this script
python ./src/antifraud.py ./paymo_input/batch_payment.txt ./paymo_input/stream_payment.txt ./paymo_output/output1.txt ./paymo_output/output2.txt ./paymo_output/output3.txt
"""

def train(input_batch, feat=None):
    """ Make Network of Users based on Transaction History """
    G = nx.Graph()

    with open(input_batch) as f:
        next(f)  # skip header
        for i, l in enumerate(f):
            row = l.split(',')
            id1,id2 = row[1:3]
            G.add_edge(id1,id2)

            if (feat==4):
                pass # new feature, to be implemented
    return G

def test(input_stream,output_out,G,feat=1):
    """
    Feature 1. Display Warning if Not Friends (degree 1 connection)
    Feature 2. Display Warning if Not Friends of Friends (degree 2 connection)
    Feature 3. Display Warning if Outside of Fourth-degree Friends (degree> 4 connection)
    """
    if (feat not in [1,2,3]):
        raise ValueError('Feature not specified, need to choose feature no. from 1, 2, 3, or 4')

    fout = open(output_out, 'w')
    with open(input_stream) as f:
        next(f)  #skip header
        for i, l in enumerate(f):
            row = l.split(',')
            id1,id2 = row[1:3]

            result = 'unverified\n'
            if (feat==1):
                if G.has_edge(id1,id2):
                    result = 'trusted\n'
            else:
                has_nodes = ((id1 in G) and (id2 in G))
                if (has_nodes):
                    has_path = nx.has_path(G,id1,id2)
                    if (has_path):
                        path_length = nx.shortest_path_length(G,source=id1,target=id2)

                        if (feat==2):
                            if (path_length<=2):
                                result = 'trusted\n'
                        elif (feat==3):
                            if (path_length<=4):
                                result = 'trusted\n'
            fout.write(result)
    fout.close()

def main(input_batch, input_stream, output_out1, output_out2, output_out3):
    """ specify train and test files
    training data comes from transaction history and
    test data are new transactions, which need classification trusted/untrusted """

    outputs = [output_out1,output_out2,output_out3]
    for feature in [1,2,3]:
        output = outputs[feature-1]

        """ load transaction history """
        G = train(input_batch, feat=feature)

        """ test with new transactions """
        test(input_stream,output,G,feat=feature)

if __name__ == "__main__":
    try:
        input_batch, input_stream, output_out1, output_out2, output_out3 = sys.argv[1:]
    except:
        input_batch = '../paymo_input/batch_payment.txt'
        input_stream = '../paymo_input/stream_payment.txt'

        output_out1 = '../paymo_output/output1.txt'
        output_out2 = '../paymo_output/output2.txt'
        output_out3 = '../paymo_output/output3.txt'
    main(input_batch, input_stream, \
         output_out1, output_out2, output_out3)


import argparse
import contextlib
import json
import os
import shutil
import subprocess
import sys
import tempfile

import tensorflow as tf
from tensorflow.python.platform import resource_loader

parser = argparse.ArgumentParser(
    description="Script to move TFLite models from pre-release schema to "
    "new schema.")
parser.add_argument(
    "input",
    type=str,
    help="Input TensorFlow lite file in `.json`, `.bin` or `.tflite` format.")
parser.add_argument(
    "output",
    type=str,
    help="Output json or bin TensorFlow lite model compliant with "
    "the new schema. Extension must be `.json`, `.bin` or `.tflite`.")

# RAII Temporary Directory, because flatc doesn't allow direct use of tempfiles.
@contextlib.contextmanager
def TemporaryDirectoryResource():
  temporary = tempfile.mkdtemp()
  try:
    yield temporary
  finally:
    shutil.rmtree(temporary)

class Converter:
  """Converts TensorFlow flatbuffer models from old to new version of schema.

  This can convert between any version to the latest version. It uses
  an incremental upgrade strategy to go from version to version.

  Usage:
    converter = Converter()
    converter.Convert("a.tflite", "a.json")
    converter.Convert("b.json", "b.tflite")
  """

  def __init__(self):
    # TODO(aselle): make this work in the open source version with better
    # path.
    paths_to_try = [
        "../../../../flatbuffers/flatc",  # not bazel
        "../../../../external/flatbuffers/flatc"  # bazel
    ]
    for p in paths_to_try:
      self._flatc_path = resource_loader.get_path_to_datafile(p)
      if os.path.exists(self._flatc_path): break

    def FindSchema(base_name):
      return resource_loader.get_path_to_datafile("%s" % base_name)

    # Supported schemas for upgrade.
    self._schemas = [
        (0, FindSchema("schema_v0.fbs"), True, self._Upgrade0To1),
        (1, FindSchema("schema_v1.fbs"), True, self._Upgrade1To2),
        (2, FindSchema("schema_v2.fbs"), True, self._Upgrade2To3),
        (3, FindSchema("schema_v3.fbs"), False, None)  # Non-callable by design.
    ]
    # Ensure schemas are sorted, and extract latest version and upgrade
    # dispatch function table.
    self._schemas.sort()
    self._new_version, self._new_schema = self._schemas[-1][:2]
    self._upgrade_dispatch = {
        version: dispatch
        for version, unused1, unused2, dispatch in self._schemas}

  def _Read(self, input_file, schema, raw_binary=False):
    """Read a tflite model assuming the given flatbuffer schema.

    If `input_file` is in bin, then we must use flatc to convert the schema
    from binary to json.

    Args:
      input_file: a binary (flatbuffer) or json file to read from. Extension
        must  be `.tflite`, `.bin`, or `.json` for FlatBuffer Binary or
        FlatBuffer JSON.
      schema: which schema to use for reading
      raw_binary: whether to assume raw_binary (versions previous to v3)
        that lacked file_identifier require this.

    Raises:
      RuntimeError: 1. When flatc cannot be invoked.
                    2. When json file does not exists.
      ValueError: When the extension is not json or bin.

    Returns:
      A dictionary representing the read tflite model.
    """
    raw_binary = ["--raw-binary"] if raw_binary else []
    with TemporaryDirectoryResource() as tempdir:
      basename = os.path.basename(input_file)
      basename_no_extension, extension = os.path.splitext(basename)
      if extension in [".bin", ".tflite"]:
        # Convert to json using flatc
        returncode = subprocess.call([
            self._flatc_path,
            "-t",
            "--strict-json",
            "--defaults-json",
        ] + raw_binary + ["-o", tempdir, schema, "--", input_file])
        if returncode != 0:
          raise RuntimeError("flatc failed to convert from binary to json.")
        json_file = os.path.join(tempdir, basename_no_extension + ".json")
        if not os.path.exists(json_file):
          raise RuntimeError("Could not find %r" % json_file)
      elif extension == ".json":
        json_file = input_file
      else:
        raise ValueError("Invalid extension on input file %r" % input_file)
      return json.load(open(json_file))

  def _Write(self, data, output_file):
    """Output a json or bin version of the flatbuffer model.

    Args:
      data: Dict representing the TensorFlow Lite model to write.
      output_file: filename to write the converted flatbuffer to. (json,
        tflite, or bin extension is required).
    Raises:
      ValueError: When the extension is not json or bin
      RuntimeError: When flatc fails to convert json data to binary.
    """
    _, extension = os.path.splitext(output_file)
    with TemporaryDirectoryResource() as tempdir:
      if extension == ".json":
        json.dump(data, open(output_file, "w"), sort_keys=True, indent=2)
      elif extension in [".tflite", ".bin"]:
        input_json = os.path.join(tempdir, "temp.json")
        with open(input_json, "w") as fp:
          json.dump(data, fp, sort_keys=True, indent=2)
        returncode = subprocess.call([
            self._flatc_path, "-b", "--defaults-json", "--strict-json", "-o",
            tempdir, self._new_schema, input_json
        ])
        if returncode != 0:
          raise RuntimeError("flatc failed to convert upgraded json to binary.")

        shutil.copy(os.path.join(tempdir, "temp.tflite"), output_file)
      else:
        raise ValueError("Invalid extension on output file %r" % output_file)

  def _Upgrade0To1(self, data):
    """Upgrade data from Version 0 to Version 1.

    Changes: Added subgraphs (which contains a subset of formally global
    entries).

    Args:
      data: Dictionary representing the TensorFlow lite data to be upgraded.
        This will be modified in-place to be an upgraded version.
    """
    subgraph = {}
    for key_to_promote in ["tensors", "operators", "inputs", "outputs"]:
      subgraph[key_to_promote] = data[key_to_promote]
      del data[key_to_promote]
    data["subgraphs"] = [subgraph]

  def _Upgrade1To2(self, data):
    """Upgrade data from Version 1 to Version 2.

    Changes: Rename operators to Conform to NN API.

    Args:
      data: Dictionary representing the TensorFlow lite data to be upgraded.
        This will be modified in-place to be an upgraded version.
    Raises:
      ValueError: Throws when model builtins are numeric rather than symbols.
    """

    def RemapOperator(opcode_name):
      """Go from old schema op name to new schema op name.

      Args:
        opcode_name: String representing the ops (see :schema.fbs).
      Returns:
        Converted opcode_name from V1 to V2.
      """
      old_name_to_new_name = {
          "CONVOLUTION": "CONV_2D",
          "DEPTHWISE_CONVOLUTION": "DEPTHWISE_CONV_2D",
          "AVERAGE_POOL": "AVERAGE_POOL_2D",
          "MAX_POOL": "MAX_POOL_2D",
          "L2_POOL": "L2_POOL_2D",
          "SIGMOID": "LOGISTIC",
          "L2NORM": "L2_NORMALIZATION",
          "LOCAL_RESPONSE_NORM": "LOCAL_RESPONSE_NORMALIZATION",
          "Basic_RNN": "RNN",
      }

      return (old_name_to_new_name[opcode_name]
              if opcode_name in old_name_to_new_name else opcode_name)

    def RemapOperatorType(operator_type):
      """Remap operator structs from old names to new names.

      Args:
        operator_type: String representing the builtin operator data type
          string. (see :schema.fbs).
      Raises:
        ValueError: When the model has consistency problems.
      Returns:
        Upgraded builtin operator data type as a string.
      """
      old_to_new = {
          "PoolOptions": "Pool2DOptions",
          "DepthwiseConvolutionOptions": "DepthwiseConv2DOptions",
          "ConvolutionOptions": "Conv2DOptions",
          "LocalResponseNormOptions": "LocalResponseNormalizationOptions",
          "BasicRNNOptions": "RNNOptions",
      }
      return (old_to_new[operator_type]
              if operator_type in old_to_new else operator_type)

    for subgraph in data["subgraphs"]:
      for ops in subgraph["operators"]:
        ops["builtin_options_type"] = RemapOperatorType(
            ops["builtin_options_type"])

    # Upgrade the operator codes
    for operator_code in data["operator_codes"]:
      # Check if builtin_code is the appropriate string type
      # use type("") instead of str or unicode. for py2and3
      if not isinstance(operator_code["builtin_code"], type(u"")):
        raise ValueError("builtin_code %r is non-string. this usually means "
                         "your model has consistency problems." %
                         (operator_code["builtin_code"]))
      operator_code["builtin_code"] = (RemapOperator(
          operator_code["builtin_code"]))

  def _Upgrade2To3(self, data):
    """Upgrade data from Version 2 to Version 3.

    Changed actual read-only tensor data to be in a buffers table instead
    of inline with the tensor.

    Args:
      data: Dictionary representing the TensorFlow lite data to be upgraded.
        This will be modified in-place to be an upgraded version.
    """
    buffers = [{"data": []}]  # Start with 1 empty buffer
    for subgraph in data["subgraphs"]:
      if "tensors" not in subgraph:
        continue
      for tensor in subgraph["tensors"]:
        if "data_buffer" not in tensor:
          tensor["buffer"] = 0
        else:
          if tensor["data_buffer"]:
            tensor[u"buffer"] = len(buffers)
            buffers.append({"data": tensor["data_buffer"]})
          else:
            tensor["buffer"] = 0
          del tensor["data_buffer"]
    data["buffers"] = buffers

  def _PerformUpgrade(self, data):
    """Manipulate the `data` (parsed JSON) based on changes in format.

    This incrementally will upgrade from version to version within data.

    Args:
      data: Dictionary representing the TensorFlow data. This will be upgraded
        in place.
    """
    while data["version"] < self._new_version:
      self._upgrade_dispatch[data["version"]](data)
      data["version"] += 1

  def Convert(self, input_file, output_file):
    """Perform schema conversion from input_file to output_file.

    Args:
      input_file: Filename of TensorFlow Lite data to convert from. Must
        be `.json` or `.bin` extension files for JSON or Binary forms of
        the TensorFlow FlatBuffer schema.
      output_file: Filename to write to. Extension also must be `.json`
        or `.bin`.

    Raises:
      RuntimeError: Generated when none of the upgrader supported schemas
        matche the `input_file` data.
    """
    # Read data in each schema (since they are incompatible). Version is
    # always present. Use the read data that matches the version of the
    # schema.
    for version, schema, raw_binary, _ in self._schemas:
      try:
        data_candidate = self._Read(input_file, schema, raw_binary)
      except RuntimeError:
        continue  # Skip and hope another schema works
      if "version" not in data_candidate:  # Assume version 1 if not present.
        data_candidate["version"] = 1
      elif data_candidate["version"] == 0:  # Version 0 doesn't exist in wild.
        data_candidate["version"] = 1

      if data_candidate["version"] == version:
        self._PerformUpgrade(data_candidate)
        self._Write(data_candidate, output_file)
        return
    raise RuntimeError("No schema that the converter understands worked with "
                       "the data file you provided.")

def main(argv):
  del argv
  Converter().Convert(FLAGS.input, FLAGS.output)

if __name__ == "__main__":
  FLAGS, unparsed = parser.parse_known_args()
  tf.compat.v1.app.run(main=main, argv=[sys.argv[0]] + unparsed)


import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from router.route import router
from config.env import *

app = FastAPI(
    redoc_url=False
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=os.getenv('CORS_ALLOW_ORIGINS'),
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
async def root():
    return {
        "Hello": "Welcome to the Spending API",
        "Github": "https://github.com/Paulo-Lopes-Estevao/expensecontrol",
        "Documentation Swegger": "https://fast-hamlet-77449.herokuapp.com/docs"
        }
app.include_router(router)

if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=os.getenv("PORT"))