from django.shortcuts import render , redirect
from ..forms import candidateRegForm , EditProfileForm , EditCandidateProfile
from ..models import User , Candidate
from django.views.generic import CreateView , UpdateView

# from django.contrib.auth import authenticate , login , logout

class candidateReg(CreateView):
    model = User
    form_class = candidateRegForm
    template_name = 'candidateReg.html'

    def get_context_data(self , **kwargs):
        kwargs['user_type'] = 'candidate'
        return super().get_context_data(**kwargs)

    def form_valid(self , form):
        user = form.save()
        return redirect('account:login')
        # return redirect('account:home')

def candidateUpdate(request):
    if request.method == 'POST':
        u_form = EditProfileForm(request.POST , instance = request.user)
        c_form = EditCandidateProfile(request.POST, instance = request.user.candidate)
        if u_form.is_valid() and c_form.is_valid():
            u_form.save()
            c_form.save()
            return redirect('account:candidate_dash')
    else:
        u_form = EditProfileForm(instance = request.user)
        c_form = EditCandidateProfile(instance = request.user.candidate)

    context = {
        'u_form' : u_form,
        'c_form' : c_form
    }

    return render(request , 'userUpdateProfile.html' , context)


"""
Multi-Line Highlight
====================
This multi-line chart uses an invisible Voronoi tessellation to handle mouseover to
identify the nearest point and then highlight the line on which the point falls.
It is adapted from the Vega-Lite example found at
https://bl.ocks.org/amitkaps/fe4238e716db53930b2f1a70d3401701
"""
# category: interactive charts
import altair as alt
from vega_datasets import data

source = data.stocks()

highlight = alt.selection_point(on='mouseover',
                          fields=['symbol'], nearest=True)

base = alt.Chart(source).encode(
    x='date:T',
    y='price:Q',
    color='symbol:N'
)

points = base.mark_circle().encode(
    opacity=alt.value(0)
).add_params(
    highlight
).properties(
    width=600
)

lines = base.mark_line().encode(
    size=alt.condition(~highlight, alt.value(1), alt.value(3))
)

points + lines


from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^add$', views.TaskInstanceAddView.as_view()),
    url(r'^(?P<id>[^/]+)?$', views.TaskInstanceListView.as_view()),
    url(r'^deploy/(?P<id>[^/]+)$', views.TaskInstanceDeployView.as_view()),
    url(r'^undeploy/(?P<id>[^/]+)$', views.TaskInstanceUndeployView.as_view()),
    url(r'^get_addresses/(?P<id>[^/]+)$', views.GetTaskInstanceAddressesView.as_view()),
]


import unittest

import gym

import gym_trading
from gym_trading.utils.decorator import print_time

class MarketMakerTestCases(unittest.TestCase):

    @print_time
    def test_time_event_env(self):
        config = dict(
            id=gym_trading.envs.MarketMaker.id,
            symbol='LTC-USD',
            fitting_file='demo_LTC-USD_20190926.csv.xz',
            testing_file='demo_LTC-USD_20190926.csv.xz',
            max_position=10,
            window_size=5,
            seed=1,
            action_repeats=5,
            training=False,
            format_3d=True,
            reward_type='default',
            ema_alpha=None,
        )
        print(f"**********\n{config}\n**********")

        env = gym.make(**config)
        done = gym_trading.envs.test_env_loop(env=env)
        _ = env.reset()
        self.assertEqual(True, done)

if __name__ == '__main__':
    unittest.main()


import json
import logging
import markdown
import os
import threading
import time
import traceback
import random
import urllib
from flask import Flask, redirect, render_template, request, Response, session, url_for, send_from_directory
from flask_caching import Cache
from ccl_scratch_tools import Parser
from ccl_scratch_tools import Scraper
from ccl_scratch_tools import Visualizer
from werkzeug.exceptions import default_exceptions, HTTPException, InternalServerError, NotFound

from lib import common
from lib import errors
from lib import schema
from lib import scrape
from lib import tasks
from lib import authentication
from lib import admin
from lib import display
from lib import certificate
from lib import summary
from lib.authentication import admin_required, login_required
from lib.reports import reporting
from lib.settings import CACHE_DIRECTORY, CLRY, PROJECT_CACHE_LENGTH, PROJECT_DIRECTORY, REDIRECT_PAGES, SITE

app = Flask(__name__)
app.register_blueprint(reporting)

try:
    celery = tasks.make_celery(CLRY["name"],
                               CLRY["result_backend"],
                               CLRY["broker_url"],
                               app)
except:
    logging.warn("Couldn't load celery.")

parser = Parser()

app.jinja_env.filters["twodec"] = common.twodec
app.jinja_env.filters["indexOf"] = common.indexOf
app.jinja_env.filters["pluralize"] = common.pluralize
app.jinja_env.filters["human_block"] = common.human_block
app.jinja_env.filters["get_selected"] = common.get_selected
app.secret_key = os.urandom(24)
app.url_map.strict_slashes = False

app.config["CACHE_TYPE"] = "lib.cache.MongoCache"
app.config["CACHE_DEFAULT_TIMEOUT"] = 1200

cache = Cache(app)

# Pass things to all templates
@app.context_processor
def inject_vars():
    return dict(user=authentication.get_login_info(),
                valid_admin_pages=admin.VALID_ADMIN_PAGES,
                SITE=SITE)

# Helper routes
@app.route("/redirect", methods=["GET"])
def redirect_to():
    if (request.args.get("username") is not None
        and request.args.get("username") != ""):  # yapf: disable
        return redirect("/user/{0}".format(
            urllib.parse.quote(request.args.get("username"))))
    else:
        return render_template("index.html",
                               message="Sorry! I wasn't able to do that.")

# Authentication
@app.route("/login", methods=["GET", "POST"])
def login():
    session.clear()

    if request.method == "GET":
        return render_template("login.html")
    else:
        # yapf: disable
        if (request.form["username"] is None
            or request.form["username"] == ""
            or request.form["password"] is None
            or request.form["password"] == ""):
            return render_template("login.html",
                                   message="All fields are required!")
        # yapf: enable

        res = authentication.login_user(request.form["username"],
                                        request.form["password"])
        if res:
            return redirect("/admin")
        else:
            return render_template(
                "login.html",
                message="Couldn't log in with that username/password combination!"
            )

@app.route("/logout", methods=["GET"])
def logout():
    session.clear()
    return render_template("login.html", message="Successfully logged out.")

@app.route("/register", methods=["POST"])
def register():
    res = authentication.register_user(request.form["username"],
                                       request.form["email"],
                                       request.form["first_name"],
                                       request.form["last_name"],
                                       request.form["password"],
                                       request.form["user_role"])

    if type(res) == bool and res:
        return redirect("/login")
    else:
        return render_template(
            "index.html",
            message="One or several of your inputs were invalid.")

# For when the site is brand new
@app.route("/setup", methods=["GET"])
def setup():
    common.connect_db()
    if len(authentication.User.objects()) == 0:
        session["user"] = {"role": "site_admin"}
        return render_template("setup.html")
    else:
        return redirect("/")

# Admin pages
@app.route("/admin")
@admin_required
def admin_index():
    return render_template("admin/index.html")

@app.route("/admin/<page>", methods=["GET", "POST"])
@admin_required
def admin_page(page):
    if page in admin.VALID_ADMIN_PAGES:
        if request.method == "GET":
            info = admin.get_info(page)
            return render_template("admin/{0}.html".format(page), info=info)
        else:
            if request.is_json:
                form = request.get_json()
            else:
                form = request.form
            result = admin.set_info(page, form)

            if "redirect" in request.form:
                if request.form["redirect"] in admin.VALID_REDIRECTS:
                    return redirect(request.form["redirect"])

            return json.dumps(result)
    else:
        return redirect("/admin")

@app.route("/admin/cache/clear")
@admin_required
def clear_cache():
    cache.clear()
    return redirect("/admin")

@app.route("/admin/error/<eid>")
@admin_required
def error_page(eid):
    error = errors.get_error(eid)
    if not error:
        return redirect("/admin/errors")
    else:
        issue = {
            "title":
                "{} error when loading {}"
                .format(error["error_code"],
                        urllib.parse.urlparse(error["url"]).path),
            "body":
                "**[Replicate here]({})**\n\nWhen accessing `{}`, there's a {} error. The traceback says:\n\n```python\n{}\n```"
                .format(error["url"],
                        urllib.parse.urlparse(error["url"]).path,
                        error["error_code"],
                        error["traceback"])
        }

        return render_template("admin/error.html", error=error, issue=issue)

def schema_editor(id):
    data = {
        "min_instructions_length": 0,
        "min_description_length": 0,
        "min_comments_made": 0,
        "min_blockify": {
            "comments": 0,
            "costumes": 0,
            "sounds": 0,
            "sprites": 0,
            "variables": 0
        },
        "required_text": [],
        "required_block_categories": {},
        "required_blocks": [],
        "stats": [],
        "text": {},
        "comparison_basis": {
            "basis": "__none__",
            "priority": None
        }
    }

    if id != "__new__":
        common.connect_db()
        try:
            data = schema.Challenge.objects(id=id).first().to_mongo()
        except AttributeError:
            raise NotFound()

    blocks = parser.block_data
    block_list = list()
    block_dict = dict()

    for cat in blocks:
        block_list += blocks[cat].keys()

        for block in blocks[cat]:
            block_dict[blocks[cat][block].lower().replace(" ", "")] = block

    return render_template("admin/edit_schema.html",
                           blocks=blocks,
                           block_dict=block_dict,
                           block_list=block_list,
                           categories=list(blocks.keys()),
                           data=data,
                           schema_id=id,
                           stats=scrape.get_default_studio_stats())

@app.route("/admin/schemas/edit", methods=["GET"])
@admin_required
def add_schema():
    return schema_editor("__new__")

@app.route("/admin/schemas/edit/<id>", methods=["GET"])
@admin_required
def edit_schema(id):
    return schema_editor(id)

# Studios, projects, users, challenges
@app.route("/certificate/generate")
@admin_required
def generate_certificate():
    common.connect_db()
    authors = list(set(scrape.Project.objects().values_list("author")))
    certificate.generate_certs.delay(authors)

    return redirect("/admin/utilities")

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

@app.route("/md", methods=["POST"])
def md():
    text = request.form["text"]
    if text is not None:
        ret = {"html": common.md(text), "js": "/static/js/sb.js"}

        return json.dumps(ret)
    return "False"

@app.route("/project/d", methods=["POST"])
def project_download():
    if request.form["sid"] is None or request.form["pid"] is None:
        return "False"
    sid = request.form["sid"]
    pid = request.form["pid"]

    scraper = Scraper()
    try:
        pid = int(pid)
    except:
        return "False"

    if pid in scraper.get_projects_in_studio(sid):
        return str(scrape.add_project(pid, sid, CACHE_DIRECTORY))
    else:
        return "False"

@app.route("/project/f/<pid>", methods=["POST"])
def project_feedback(pid):
    if ("_gu_uid" in request.cookies and "feelings" in request.json
        and "minutes" in request.json):  # yapf: disable
        try:
            common.connect_db()
            reflection = scrape.ProjectReflection(
                project_id=pid,
                gu_uid=request.cookies.get("_gu_uid"),
                minutes=int(request.json["minutes"]),
                feelings=request.json["feelings"])
            reflection.save()
            return "True"
        except:
            return "False"
    else:
        return "False"

@app.route("/project/o/<pid>")
def feedback_owner(pid):
    try:
        common.connect_db()
        reflection = scrape.ProjectReflection.objects(
            project_id=pid).order_by("-timestamp").first()
        return reflection["gu_uid"]
    except:
        return ""

@app.route("/project/r/<pid>")
def reload_project(pid):
    try:
        pid = int(pid)
    except:
        pid = 0

    scrape.set_reload_page(pid)
    return redirect("/project/{}".format(pid))

@app.route("/project/<pid>/view", methods=["GET"])
@cache.cached(timeout=PROJECT_CACHE_LENGTH,
              forced_update=scrape.get_reload_project,
              unless=authentication.session_active)
def project__id(pid):
    return display.get_project_page(pid, CACHE_DIRECTORY)

@app.route("/project/<pid>", methods=["GET"])
def project_id(pid):
    return render_template("project_loader.html")

@app.route("/studio", methods=["GET", "POST"])
@admin_required
def studio():
    if request.method == "GET":
        common.connect_db()
        return render_template("studio.html",
                               schemas=list(schema.Challenge.objects().order_by("-modified")))  # yapf: disable
    else:
        scraper = Scraper()
        sid = scraper.get_id(request.form["studio"])

        s = None
        if request.form["schema"] != "__none__":
            s = request.form["schema"]

        if request.form["studio"] == "__all__":
            scrape.rescrape_all.delay(cache_directory=CACHE_DIRECTORY)
            return "Started"
        elif sid is not None:
            scrape.add_studio.delay(sid,
                                    schema=s,
                                    show=("show" in request.form),
                                    cache_directory=CACHE_DIRECTORY)
            return redirect("/studio/{0}".format(sid))
        else:
            return render_template(
                "studio.html",
                message="Please enter a valid studio ID or URL.")

@app.route("/studio/list/<sid>")
def studio_list(sid):
    if sid == "":
        return "Must include a studio ID.", 400

    common.connect_db()
    studio = scrape.Studio.objects(studio_id=sid).first()

    if studio is None:
        return "Studio does not exist.", 404

    limit = 8
    page = 0
    order = "author"
    try:
        if "page" in request.args:
            page = int(request.args["page"])
        if "order" in request.args:
            if request.args["order"] in {"author", "title", "id", "project_id"}:
                order = request.args["order"]
        if "limit" in request.args:
            if int(request.args["limit"]) <= 100:
                limit = int(request.args["limit"])
    except:
        return "Invalid arguments", 400

    skip = page * limit

    projects = scrape.Project.objects(
        studio_id=sid).order_by(order).skip(skip).limit(limit)
    info = {"projects": list()}
    for i, project in enumerate(projects):
        info["projects"].append({
            "project_id": project["project_id"],
            "title": project["title"],
            "author": project["author"],
            "image": (project["image"] if "image" in project else ""),
            "modified": project["history"]["modified"]
        })

    return Response(json.dumps(info), mimetype="application/json")

@app.route("/studio/<sid>")
def studio_id(sid):
    if sid == "":
        return redirect("/prompts")

    common.connect_db()
    studio = scrape.Studio.objects(studio_id=sid).first()

    if studio is None or (not (studio["public_show"]
                               or authentication.session_active())):
        return redirect("/prompts")

    projects = list(scrape.Project.objects(studio_id=sid).order_by("author"))
    info = {"authors": list(), "project_ids": list(), "titles": list()}

    for project in projects:
        info["authors"].append(project["author"].lower())
        info["project_ids"].append(project["project_id"])
        info["titles"].append(project["title"].lower())

    message = None
    if studio["status"] == "in_progress" or studio["status"] is None:
        message = "This studio is currently in the process of being downloaded and analyzed. Refresh page."

    return render_template("studio_id.html",
                           info=info,
                           projects=projects,
                           studio=studio,
                           message=message)

@app.route("/user/<username>", methods=["GET", "POST"])
def user_id(username):
    if request.method == "POST":
        return send_from_directory(f"{CACHE_DIRECTORY}/certificates",
                                   filename="{}.pdf".format(username.lower()))
    else:
        common.connect_db()
        projects = list(scrape.Project.objects(author=username.lower()))
        studios = dict()

        keep_projects = list()
        for i, project in enumerate(projects):
            if project["studio_id"] not in studios:
                studio = scrape.Studio.objects(
                    studio_id=project["studio_id"]).first()

                if studio is not None:
                    studios[project["studio_id"]] = studio
                    keep_projects.append(project)
            else:
                keep_projects.append(project)

        return render_template("username.html",
                               projects=keep_projects,
                               studios=studios,
                               username=username)

@app.route("/prompts", methods=["GET"])
@cache.cached(timeout=600, unless=authentication.session_active)
def prompts():
    common.connect_db()
    studios = list(scrape.Studio.objects(public_show=True))
    schema_ids = set()
    for studio in studios:
        if "challenge_id" not in studio:
            studios.remove(studio)
            break

        schema_ids.add(studio["challenge_id"])

    schemas = schema.Challenge.objects(id__in=schema_ids).order_by("short_label", "title")  # yapf: disable
    id_order = list(schemas.values_list("id"))

    for i in range(len(id_order)):
        id_order[i] = str(id_order[i])

    schemas = schemas.as_pymongo()

    new_schemas = dict()
    for sc in schemas:
        new_schemas[str(sc["_id"])] = sc

    # Order the studios
    ordered_studios = [None] * len(studios)
    for studio in studios:
        studio["challenge_id"] = str(studio["challenge_id"])

        try:
            ordered_studios[id_order.index(studio["challenge_id"])] = studio
        except ValueError:
            pass

    return render_template("prompts.html",
                           challenges=ordered_studios,
                           schemas=new_schemas)

@app.route("/summary", methods=["GET", "POST"])
def summarize():
    if request.method == "GET":
        with open("{}/lib/data/summary.json".format(PROJECT_DIRECTORY)) as f:
            data = json.load(f)

        for i, item in enumerate(data["content"]):
            data["content"][i] = common.md(item) if isinstance(item, str) else item  # yapf: disable

        return render_template("summary.html", data=data)
    else:
        with open("{}/data/summary.json".format(CACHE_DIRECTORY)) as f:
            return Response(f.read(), mimetype="application/json")

@app.route("/summary/image")
@cache.cached()
def summary_image():
    try:
        with open("{}/cache/data/projects.jpg".format(PROJECT_DIRECTORY), "rb") as f:  # yapf: disable
            return f.read()
    except:
        return "Not found", 404

@app.route("/summary/generate")
@admin_required
def generate_summary():
    summary.generate_summary_page.delay()
    return redirect("/admin/utilities")

# Static pages -- About, Strategies, Signup, Research
@app.route("/")
@cache.cached(unless=authentication.session_active)
def homepage():
    return render_template("home.html", section="home")

@app.route("/about", methods=["GET"])
@cache.cached(unless=authentication.session_active)
def about():
    return render_template("about.html")

@app.route("/strategies", methods=["GET"])
@cache.cached(unless=authentication.session_active)
def strategies():
    return render_template("strategies.html")

@app.route("/signup", methods=["GET", "POST"])
@cache.cached(unless=authentication.session_active)
def signup():
    return render_template("signup.html")

@app.route("/research", methods=["GET"])
@cache.cached(unless=authentication.session_active)
def research():
    return render_template("research.html")

# Error pages
@app.route("/ie")
@cache.cached(unless=authentication.session_active)
def ie():
    return render_template("ie.html")

def error(e):
    """Handle errors."""

    if e.code == 404 and request.path in REDIRECT_PAGES:
        return redirect(REDIRECT_PAGES[request.path], code=301)

    status = "closed" if e.code == 404 else "open"
    saved = errors.add_error(e.code,
                             request.url,
                             traceback.format_exc(),
                             status)

    if not isinstance(e, HTTPException):
        e = InternalServerError()

    scratch = "when i receive [error {} v]\n say [Oh no!]\nswitch costume to (sad :\( v)".format(e.code)  # yapf: disable

    return render_template("error.html", error=e, scratch=scratch, saved=saved)

# Listen for errors
for code in default_exceptions:
    app.errorhandler(code)(error)

if __name__ == "__main__":
    app.run()


from keras.applications.vgg16 import VGG16
from keras.preprocessing.image import load_img, img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.layers import Input, Flatten, Dense
from keras.models import Model
from keras.optimizers import SGD
from keras.callbacks import TensorBoard, ModelCheckpoint
from sklearn.model_selection import train_test_split
import numpy as np
from tqdm import tqdm

imageX = 100
imageY = 100

def loadData(dir):
    lines = open(dir).read().split('\n')[:-1]
    X = []
    Y = []
    for line in tqdm(lines):
        try:
            x, y = line.split('|')
            x = load_img(x, target_size=(imageX, imageY))
            x = img_to_array(x)
            yoh = [0, 0]
            yoh[int(y)]=1
            Y.append(yoh)
            X.append(x)
        except:
            print('missed image')

    X = np.array(X)
    Y = np.array(Y)
    X = preprocess_input(X)
    print(X.shape, Y.shape)

    return X, Y

def train():
    vgg = VGG16(include_top = False, classes=2, input_shape=(imageX, imageY, 3))

    input = Input(shape=(imageX, imageY, 3), name = 'image_input')

    vgg = vgg(input)

    x = Flatten(name='flatten')(vgg)
    x = Dense(2, activation='softmax', name='predictions')(x)

    vgg = Model(input=input, output=x)

    vgg.summary()

    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    vgg.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])
    tb = TensorBoard(log_dir='./Graph', histogram_freq=0, write_graph=True, write_images=True)

    X, Y = loadData("clean")
    X /= 255
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2)

    batch_size = 32
    nb_epoch = 1000

    filepath="weights-improvement-{epoch:02d}.hdf5"

    cp = ModelCheckpoint('model-{epoch:03d}.h5', verbose=1, monitor='val_acc',save_best_only=True, mode='auto')

    print('training ========================================================================================')

    vgg.fit(X_train, y_train,
              batch_size=batch_size,
              epochs=nb_epoch,
              validation_data=[X_test, y_test],
              shuffle=True,
              callbacks=[tb, cp])

train()


#imports
import os

import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager

import numpy as np
import pandas as pd

import seaborn as sns
plt.style.use('seaborn-whitegrid')

import util_graficos as graf

# %matplotlib inline

# ========================================================
# estilos de graficos :
# >>  https://tonysyu.github.io/raw_content/matplotlib-style-gallery/gallery.html
# ========================================================
# plt.style.use('seaborn-whitegrid')
# plt.style.use('seaborn-pastel')
# plt.style.use('seaborn-colorblind')
# plt.style.use('seaborn-bright')
# plt.style.use('grayscale')
# plt.style.use('ggplot')
# plt.style.use('fivethirtyeight')
# plt.style.use('dark_background')
# plt.style.use('classic')
# plt.style.use('bmh')

pasta_origem = '../data/data_grouped'
arq_metadata = open('%s/_metadata.csv' % (pasta_origem), 'w+')

probs = ['0.01','0.02','0.05','0.001','0.0001']
# probs = ['0.01']#,'0.02']#,'0.05','0.001','0.0001']

sizes = ['100','1000','10000']

df_tudo = pd.DataFrame()

#le os arquivos de entrada
for prob in probs:
    for size in sizes:
        filtro_arq = '%s_%s_' % (prob, size)

        arqs_in = os.listdir(pasta_origem)
        for f in arqs_in:
            if (f.startswith(filtro_arq)):
                print(f)
                csv = os.path.join(pasta_origem, f)
                df_temp = pd.read_csv(delimiter = ';', filepath_or_buffer = csv)
                df_tudo = df_tudo.append(df_temp)

#ordena os registros por algoritmo
df_tudo.sort_values(by=['size_of_array','algoritmo'], inplace=True, ascending=[True,True])

# gera os graficos
rows = 5
cols = 2

larg_fig = 32
alt_fig  = 41
font_size = 15

sns_palette = 'Set2'
color_map = sns.color_palette(sns_palette, n_colors=4)

gerar_graficos = True
gerar_csvs_com_dados_dos_graficos = True

df_dados_csv = pd.DataFrame()

for prob in probs:
    data = df_tudo[df_tudo.probabilidade_erro == float(prob)]

    group_by = ['probabilidade_erro', 'algoritmo']
    df_means = data.groupby(group_by).mean()
    df_std = data.groupby(group_by).std()
    df_min = data.groupby(group_by).min()
    df_max = data.groupby(group_by).max()

    # colunas_join = ['largest_sorted_subarray','k_unordered_sequence','percentual_k_unordered','percentual_maior_array']
    colunas_join = ['percentual_k_unordered','percentual_maior_array']
    df_dados_csv = df_means[colunas_join]
    df_dados_csv = df_dados_csv.join(other=df_std[colunas_join], rsuffix='_std')
    df_dados_csv = df_dados_csv.join(other=df_min[colunas_join], rsuffix='_min')
    df_dados_csv = df_dados_csv.join(other=df_max[colunas_join], rsuffix='_max', lsuffix='_mean')

    indx = 1

    #se tiver dados no DF
    if (data.shape[0] > 0):
        # print(data.head())

        file_title = 'Estatisticas_por_Algoritmo_Juntando_os_Tamanhos_Prob_%s' % (prob)

        if (gerar_csvs_com_dados_dos_graficos):
            df_dados_csv.to_csv('graficos/comparacao_entre_algoritmos/csv/%s.csv' % (file_title))

        if (gerar_graficos):
            graf_title = 'Estatísticas por Algoritmo Juntando Todos os Tamanhos - Probabilidade de Erro %s' % (prob)

            fig = plt.figure(figsize=[larg_fig, alt_fig])
            plt.suptitle(graf_title + '\nBarplot / Média / Desvio Padrão / Mín. / Máx.', fontsize=30)
            plt.rcParams.update({'font.size': font_size})

            #grafico de barras para % Desordenados
            ax = plt.subplot(rows, cols, indx)
            graf.gerarBarplot(ax=ax, x='probabilidade_erro',
                              y='percentual_k_unordered',
                              hue='algoritmo',
                              data=data,
                              title='% Desordenados',
                                  palette=color_map)
            graf.inserirValoresNasBarras(ax, df_means['percentual_k_unordered'] )
            indx += 1

            #grafico de barras para % Maior Array
            ax = plt.subplot(rows, cols, indx)
            graf.gerarBarplot(ax=ax, x='probabilidade_erro',
                              y='percentual_maior_array',
                              hue='algoritmo',
                              data=data,
                              title='% Maior Array',
                              palette=color_map)
            graf.inserirValoresNasBarras(ax, df_means['percentual_maior_array'].values)
            indx += 1

            # grafico de barras para a média das % Desordenados
            ax = plt.subplot(rows, cols, indx)
            plt.ylabel('Média - percentual_k_unordered')
            data_graf = df_means['percentual_k_unordered'].unstack()
            data_graf.plot(kind='bar', ax=ax, color=color_map)
            graf.inserirValoresNasBarras(ax, df_means['percentual_k_unordered'].values)
            indx += 1

            # grafico de barras para a média das % Maior Array
            ax = plt.subplot(rows, cols, indx)
            plt.ylabel('Média - percentual_maior_array')
            data_graf = df_means['percentual_maior_array'].unstack()
            data_graf.plot(kind='bar', ax=ax, color=color_map)
            graf.inserirValoresNasBarras(ax, df_means['percentual_maior_array'].values)
            indx += 1

            # grafico de barras para a std_dev das % Desordenados
            ax = plt.subplot(rows, cols, indx)
            plt.ylabel('Desvio P. - percentual_k_unordered')
            data_graf = df_std['percentual_k_unordered'].unstack()
            data_graf.plot(kind='bar', ax=ax, color=color_map)
            graf.inserirValoresNasBarras(ax, df_std['percentual_k_unordered'].values)
            indx += 1

            # grafico de barras para a std_dev das % Maior Array
            ax = plt.subplot(rows, cols, indx)
            plt.ylabel('Desvio P. - percentual_maior_array')
            data_graf = df_std['percentual_maior_array'].unstack()
            data_graf.plot(kind='bar', ax=ax, color=color_map)
            graf.inserirValoresNasBarras(ax, df_std['percentual_maior_array'].values)
            indx += 1

            # grafico de barras para a std_dev das % Desordenados
            ax = plt.subplot(rows, cols, indx)
            plt.ylabel('Mín. - percentual_k_unordered')
            data_graf = df_min['percentual_k_unordered'].unstack()
            data_graf.plot(kind='bar', ax=ax, color=color_map)
            graf.inserirValoresNasBarras(ax, df_min['percentual_k_unordered'].values)
            indx += 1

            # grafico de barras para a std_dev das % Maior Array
            ax = plt.subplot(rows, cols, indx)
            plt.ylabel('Mín. - percentual_maior_array')
            data_graf = df_min['percentual_maior_array'].unstack()
            data_graf.plot(kind='bar', ax=ax, color=color_map)
            graf.inserirValoresNasBarras(ax, df_min['percentual_maior_array'].values)
            indx += 1

            # grafico de barras para a std_dev das % Desordenados
            ax = plt.subplot(rows, cols, indx)
            plt.ylabel('Máx. - percentual_k_unordered')
            data_graf = df_max['percentual_k_unordered'].unstack()
            data_graf.plot(kind='bar', ax=ax, color=color_map)
            graf.inserirValoresNasBarras(ax, df_max['percentual_k_unordered'].values)
            indx += 1

            # grafico de barras para a std_dev das % Maior Array
            ax = plt.subplot(rows, cols, indx)
            plt.ylabel('Máx. - percentual_maior_array')
            data_graf = df_max['percentual_maior_array'].unstack()
            data_graf.plot(kind='bar', ax=ax, color=color_map)
            graf.inserirValoresNasBarras(ax, df_max['percentual_maior_array'].values)
            indx += 1

            #salva o gráfico
            plt.savefig('graficos/comparacao_entre_algoritmos/%s.png' % (file_title), bbox_inches='tight', pad_inches=2)  # , format='png', orientation='landscape', papertype='letter')


import numpy as np
import metrics
import datasets
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import KFold
from hypothesis import TreeHypothesis
from consistency import MCConsistentTree
from hypothesis_search import MCHypothesisSearch
from algorithms import crembo

def compress_tree(n_estimators, max_tree_depth, max_forest_depth, X_train, y_train, c, weight=None, X_val=None,
                  y_val=None, score=None, delta=1):
    # All but MED are trained on x_val as well
    X_train = np.concatenate([X_train, X_val], axis=0)
    y_train = np.concatenate([y_train, y_val], axis=0)

    # train benchmark tree
    b_tree = DecisionTreeClassifier(max_depth=max_tree_depth, class_weight=weight)
    b_tree.fit(X_train, y_train)
    f_b = TreeHypothesis('Tree_bench', b_tree)

    # train random forest to create a collection of hypotheses
    rf = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_forest_depth, class_weight=weight)
    rf.fit(X_train, y_train)

    # get trees from forest
    hypotheses = []
    for i in range(len(rf.estimators_)):
        name = f'Tree_{i}'
        hypotheses.append(TreeHypothesis(name, f=rf.estimators_[i]))

    # define consistency and hypothesis search algorithms
    consistency = MCConsistentTree(depth=max_tree_depth, class_weight=weight)
    a = MCHypothesisSearch(consistency, X_val=X_val, y_val=y_val, score=score)

    f_med, depth, y1 = crembo(s=X_train, t=hypotheses, c=c, a=a, delta=delta)

    # train a tree with all the data labels from MCMA
    tree = DecisionTreeClassifier(max_depth=max_tree_depth, class_weight=weight)
    tree.fit(X_train, y1)
    f_voting = TreeHypothesis('Tree_voting', tree)

    return rf, hypotheses, f_med, f_voting, f_b

def robustness_agreement_exp(dataset, max_tree_depth, forest_depth, num_trees, num_experiments=1,
                      score='accuracy', weight=None, n_splits=10, delta=1):
    scores = []
    means = []
    for i in range(num_experiments):
        agreement, mean_score = robustness_exp(dataset, max_tree_depth, forest_depth, num_trees, num_experiments=1,
                           score=score, weight=weight, n_splits=n_splits, use_agreement=True, delta=delta)
        means.append(mean_score)
        scores.append(agreement)

    print('\nFinal results:')
    scores = np.asarray(scores)
    scores = scores.mean(axis=0)
    print(f'Average Agreement score: RF {scores[0]}, BM {scores[1]}, VT {scores[2]}, MED {scores[3]}')

def robustness_exp(dataset, max_tree_depth, forest_depth, num_trees, num_experiments=1,
                      score='accuracy', weight=None, n_splits=10, use_agreement=False, delta=1):

    kf_scores = []
    kf = KFold(n_splits=n_splits)
    x, y, X_test, y_test = datasets.prepare_data(dataset, return_test=True)
    c = datasets.get_number_of_classes(dataset)
    score_func, score_metric = metrics.get_socre_foncs(score)

    trees = []
    for train, test in kf.split(x):
        X_train, _, y_train, _ = x[train], x[test], y[train], y[test]
        X_train, X_val, y_train, y_val = datasets.prepare_val(X_train, y_train)
        k_scores = []
        for k in range(num_experiments):
            rf, _, f_med, f_all, f_m = compress_tree(num_trees, max_tree_depth, forest_depth, X_train, y_train,
                                                     c, weight=weight, X_val=X_val, y_val=y_val,
                                                     score=score_metric, delta=delta)

            k_scores.append(score_func(rf, None, f_med, f_all, f_m, X_train, y_train, X_test, y_test))

        trees.append((rf, f_m, f_all, f_med))
        kf_scores.append(metrics.average_scores(k_scores, num_experiments))

    means = metrics.mean_and_std(kf_scores, mean_only=True)
    output = metrics.agreement_score(trees, X_test) if use_agreement else None
    return output, means

def generalization_exp(dataset, max_tree_depth, forest_depth, num_trees, num_experiments=1,
                      score='accuracy', weight=None, n_splits=10, delta=1):

    kf_scores = []
    kf = KFold(n_splits=n_splits)
    x, y, _, _, = datasets.prepare_data(dataset, return_test=False)
    c = datasets.get_number_of_classes(dataset)
    score_func, score_metric = metrics.get_socre_foncs(score)

    for k in range(num_experiments):
        f_scores = []
        for train, test in kf.split(x):
            X_train, X_test, y_train, y_test = x[train], x[test], y[train], y[test]
            X_train, X_val, y_train, y_val = datasets.prepare_val(X_train, y_train)
            rf, _, f_med, f_all, f_m = compress_tree(num_trees, max_tree_depth, forest_depth, X_train, y_train,
                                                     c, weight=weight, X_val=X_val, y_val=y_val,
                                                     score=score_metric, delta=delta)

            f_scores.append(score_func(rf, None, f_med, f_all, f_m, X_train, y_train, X_test, y_test))

        mean_var_win = metrics.mean_and_std(f_scores, mean_only=False)
        kf_scores.append(mean_var_win)

    print('\nFinal results:')
    print(f'Average RF mean {sum([score[0] for score in kf_scores]) / num_experiments}, var {sum([score[1] for score in kf_scores]) / num_experiments}')
    idx = 2
    for t in ('BM', 'VT', 'MED'):
        t_mean = sum([score[idx] for score in kf_scores]) / num_experiments
        t_wins = sum([score[idx + 2] for score in kf_scores]) / num_experiments
        idx += 3
        print(f'Average {t} mean {t_mean}, wins {t_wins}')

    return


"""Collect and save experiencies in IndustrialBenchmark with a behaviour policy."""
import click
import numpy as np
from ib_behavior_policy import IBBehaviorPolicy
from ray.rllib.evaluation.sample_batch_builder import SampleBatchBuilder
from ray.rllib.offline.json_writer import JsonWriter
from tqdm import trange

from raylab.envs.registry import _industrial_benchmark_maker

@click.command()
@click.option(
    "--local-dir",
    "-l",
    type=click.Path(exists=False, file_okay=False, dir_okay=True, resolve_path=True),
    default="data/",
    show_default=True,
    help="",
)
def main(local_dir):
    """Main loop based on `rllib.examples.saving_experiences`."""
    # pylint: disable=too-many-locals
    batch_builder = SampleBatchBuilder()
    writer = JsonWriter(local_dir)
    env = _industrial_benchmark_maker({"max_episode_steps": 1000})
    policy = IBBehaviorPolicy(env.observation_space, env.action_space, {})

    for eps_id in trange(100):
        obs = env.reset()
        prev_action = np.zeros_like(env.action_space.sample())
        prev_reward = 0
        done = False
        time = 0
        while not done:
            action, _, _ = policy.compute_single_action(obs, [])
            new_obs, rew, done, info = env.step(action)
            batch_builder.add_values(
                t=time,
                eps_id=eps_id,
                agent_index=0,
                obs=obs,
                actions=action,
                action_prob=1.0,  # put the true action probability here
                rewards=rew,
                prev_actions=prev_action,
                prev_rewards=prev_reward,
                dones=done,
                infos=info,
                new_obs=new_obs,
            )
            obs = new_obs
            prev_action = action
            prev_reward = rew
            time += 1
        writer.write(batch_builder.build_and_reset())

if __name__ == "__main__":
    main()  # pylint: disable=no-value-for-parameter


import pysony
import board, neopixel
import time
import urllib.request as req
import RPi.GPIO as GPIO
from pathlib import Path

class FixtureInspectorController:
  QX_ADDR = "http://192.168.122.1:8080"

  search = None
  cameras = None
  camera = None

  halfstep = 0
  control_pins = [22, 24, 23, 25]
  halfstep_seq = [
    [1,0,0,0],
    [1,1,0,0],
    [0,1,0,0],
    [0,1,1,0],
    [0,0,1,0],
    [0,0,1,1],
    [0,0,0,1],
    [1,0,0,1]
  ]

  def __init__(self):
    self.setupCam()
    self.setupStepmotor()

  def setupCam(self):
    # print("Searching for camera...")

    # self.search = pysony.ControlPoint(interface="wlan0")
    # self.cameras = self.search.discover()

    # if len(self.cameras):
    #   print("Found: %s" % self.cameras[0])
    #   print("")
    #   self.camera = pysony.SonyAPI(QX_ADDR=self.cameras[0])
    # else:
    #   print("No camera found, aborting")
    #   quit()
    self.camera = pysony.SonyAPI(QX_ADDR=self.QX_ADDR)

    self.camera.startRecMode()
    time.sleep(5)

    self.camera.setFocusMode('AF-S')
    self.camera.setPostviewImageSize('Original')
    self.camera.setFNumber('22')
    self.camera.setIsoSpeedRate('100')
    self.camera.setShutterSpeed('1/20')
    self.camera.setTouchAFPosition([50.0, 50.0])

  def setupStepmotor(self):
    for pin in self.control_pins:
      GPIO.setup(pin, GPIO.OUT)
      GPIO.output(pin, 0)

  # 1 halfstep is 0.9 degree
  # 100 halfstep is 90 degree
  def Stepmotor(self, step):
    for i in range(abs(step)):
      for pin in range(4):
        GPIO.output(self.control_pins[pin], self.halfstep_seq[self.halfstep][pin])

      if step > 0:
        self.halfstep += 1
        if self.halfstep == 8:
          self.halfstep = 0
      else:
        self.halfstep -= 1
        if self.halfstep == -1:
          self.halfstep = 7

      time.sleep(0.001)

  def Stepmotor_idle(self):
    for pin in range(4):
      GPIO.output(self.control_pins[pin], GPIO.LOW)

  def resetStep(self):
    self.halfstep = 0

  def TakePicture(self, filename="image.jpg"):
    res = self.camera.actTakePicture()
    res = req.urlretrieve(res['result'][0][0].replace('\/', '/'), filename)

if __name__ == '__main__':
  filename = ".jpg"

  fictrl = FixtureInspectorController()

  fictrl.pixelOn()

  st0 = time.time()
  for i in range(100):
    st1 = time.time()
    fictrl.camera.setTouchAFPosition([50.0, 50.0])
    for j in range(4):
      print('i : ' + str(i) + ' j : ' + str(j))
      st2 = time.time()
      res = fictrl.camera.actTakePicture()
      print('picture taken : ' + str(time.time()-st2))
      res = req.urlretrieve(res['result'][0][0].replace('\/', '/'),
      str(Path.cwd()) + "/temp_" + str(100).zfill(4) + "/" + str(j).zfill(3) + '_' + str(i).zfill(3) + filename)
      print('image download : ' + str(time.time()-st2))
      fictrl.Stepmotor(-100)
      print('motor rotated : ' + str(time.time()-st2))
    print('a cycle done : ' + str(time.time()-st1))
  fictrl.pixelOff()


#!/usr/bin/python

import commands
import curses
import time
import threading

class CursesUI(threading.Thread):
    """A curses UI or talking to a driver via client"""
    def __init__(self, allow_input):
        """Initializes ncurses"""
        threading.Thread.__init__(self, name='curses-ui')

        # used to stop the UI loop
        self._stop = threading.Event()

        # if False, ignore all comnmands, just print status
        self.allow_input = allow_input

        # keep track of the last keystroke
        # it is cleared and returned by get_command
        self._last_key = None

    def init(self):
        """Sets up ncurses and creates all the windows"""
        #initialize ncurses
        self.stdscr = curses.initscr()
        self.windows = {'stdscr':self.stdscr}

        curses.start_color()
        curses.noecho()
        curses.cbreak()
        self.stdscr.keypad(1)

        curses.curs_set(0)
        curses.halfdelay(1)

        #initialize the status and result windows
        self.windows['time'] = self.create_window(' Time ', 3, 82, 0, 0)

        self.windows['driver'] = self.create_window(' Driver Status ', 12, 40, 3, 0)
        self.windows['monitor'] = self.create_window(' Monitor Status ', 12, 40, 3, 41)
        self.windows['arduino'] = self.create_window(' Arduino Status ', 10, 40, 14, 0)
        self.windows['sensors'] = self.create_window(' Sensor Status ', 10, 40, 14, 41)
        self.windows['result'] = self.create_window(' Last Result ', 3, 82, 25, 0)

    def cleanup(self):
        """Clean up ncurses"""
        self.stdscr.keypad(0)
        curses.nocbreak()
        curses.echo()

        curses.endwin()

    def run(self):
        """The main loop"""
        while not self._stop.is_set():
            self.write_line(self.windows['time'], 1, "%.2f" % time.time(), align = 'center')
            for window in self.windows.values():
                window.refresh()

            if self.allow_input:
                try:
                    self._last_key = self.stdscr.getkey()
                except:
                    pass

    def stop(self):
        """Stops the UI loop"""
        self._stop.set()
        self.join()
        self.cleanup()

    def create_window(self, title, height, width, top, left, border = True):
        """Creates a window"""
        window = curses.newwin(height, width, top, left)

        if border:
            window.box()
        if title:
            self.write_line(window, 0, title, left_margin = 3, nopad = True)

        self.windows['stdscr'].refresh()
        return window

    def write_key_value(self, window, linenum, key, value):
        """Properly formats and writes a key-value pair"""
        key = str(key)
        value = str(value)

        max_width = window.getmaxyx()[1] - 2 #subtract 2 for borders
        key_len = max_width - len(value) - 1 #subtract 1 for the space between k and v

        line = "%s %s" % (key[:key_len].ljust(key_len), value)
        self.write_line(window, linenum, line)

    def write_line(self, window, linenum, line, align = 'left', left_margin = 1, right_margin = 1, nopad = False):
        """Writes a line in the specified window"""
        h, w = window.getmaxyx()
        if linenum > h:
            return

        #truncate the string to fit perfectly inside the window
        if not nopad:
            length = w - left_margin - right_margin
            # first trim too-long lines
            line = line[:w-2]

            # next justify the line
            align_fun = {
                    'left':line.ljust,
                    'right':line.rjust,
                    'center':line.center,
                    }
            line = align_fun[align](length)

        window.addstr(linenum, left_margin, line)

    def write_result(self, result):
        self.write_line(self.windows['result'], 1, str(result))

    def update_status(self, status):
        """puts the current status into the status windows"""
        for cat, s in status.items():
            if cat == 'sensors':
                continue

            window = self.windows[cat]
            linenum = 1
            for key, val in s.items():
                if key == 'alerts':
                    # print out monitor alerts
                    self.write_line(window, linenum, 'Alerts:')
                    linenum += 1
                    for k, v in val.items():
                        self.write_key_value(window, linenum, '  '+k, v)
                        linenum += 1
                else:
                    self.write_key_value(window, linenum, key, val)
                    linenum += 1

        sen_line = 1
        for sen in status['sensors']:
            val = "%s%s" % (sen['value'], sen['units'])
            self.write_key_value(self.windows['sensors'], sen_line, sen['name'], val)
            sen_line += 1

    def get_command(self):
        """Returns the user's last command"""
        try:
            if self._last_key == 'q':
                return commands.Quit()
            elif self._last_key == 'g':
                return commands.Go()
            elif self._last_key == 'r':
                return commands.Reset()
            elif self._last_key == 's':
                return commands.Stop()
            elif self._last_key == 't':
                return commands.Shutdown()
            elif self._last_key == 'b':
                return commands.Brake(1)
            elif self._last_key == 'h':
                return commands.Hold()
            elif self._last_key == 'KEY_LEFT':
                return commands.Steer(direction=-1)
            elif self._last_key == 'KEY_RIGHT':
                return commands.Steer(direction=1)
            elif self._last_key == 'KEY_DOWN':
                return commands.Drive(speed=-1)
            elif self._last_key == 'KEY_UP':
                return commands.Drive(speed=1)

            else:
                return None

        finally:
            self._last_key = None
        self.cleanup()

    def error_notify(self, error):
        """Displays the error in the results window"""
        self.write_result("Error at %.2f: %s" % (time.time(), str(error)))

def get_ui(allow_input, **rest):
    return CursesUI(allow_input)

if __name__ == "__main__":
    ui = get_ui()
    ui.init()

    ui.start()
    try:
        while True:
            time.sleep(0.05)

            command = ui.get_command()
            if not command:
                continue
            if type(command) == commands.Quit:
                break
            else:
                ui.write_result("command was %s" % str(command))
    finally:
        ui.stop()


from django.http import HttpResponse

articles_list = [
    [1, "Айви Яптанго", 2020, "Самые шикарные парочки знаменитостей 2019 года", ["красота", "гороскопы"]],
    [2, "Лео Месси", 2014, "Un Abrazo a Todos", ["лайфстайл", "недвижимость"]],
    [3, "Гэри Паска", 2016, "Продаётся дом в Южной Флориде за $2,695", ["недвижимость", "коучинг", "howto"]],
    [4, "Роби Тобинсон", 1967, "7 лет я применял этот трюк и назад пути нет", ["лайфхак", "коучинг", "howto"]],
    [5, "Металлий Вутко", 2017, "Let Me Speak From My Heart", ["футбол", "допинг"]],
    [6, "Роби Тобинсон", 1977, "Беспроигрышная древнеримская техника обольщения", ["отношения", "история", "howto"]],
    [7, "Роби Тобинсон", 2022, "3 способа установить девайс от храпа", ["здоровье", "коучинг", "howto"]],
    [8, "Роби Тобинсон", 1975, "Интимная проблема, которой втайне озабочены все ваши друзья", ["отношения", "здоровье", "howto"]],
    [9, "Elina Shake", 2008, "Представления, основанные на классах", ["python", "howto", "лайфхак"]],
    [10, "Бен Франклин", 1753, "Электрические стодолларовые купюры", ["фондовая биржа", "рынки", "электричество"]],
    [11, "Роби Тобинсон", 2012, "5 забавных Django Apps, о которых говорят все", ["django", "IT", "howto"]],
    [12, "Металлий Вутко", 2017, "No Problems, No Criminality", ["допинг", "недвижимость"]],
    [13, "Роби Тобинсон", 1987, "7 способов до смерти напугать своего босса в пятницу 13-го", ["работа", "мистика", "howto"]],
    [14, "Твентин Карантино", 2007, "Четыре сервера", ["кино", "django", "мистика"]],
]

# в функции generate_html()
# оформим перечень статей в виде html-списка
def generate_html(articles):
    if len(articles) == 0:
        return '
По вашему запросу не найдено ни одной статьи!
'
    else:
        base_html = '
Статьи по вашему запросу:

'
        for article in articles:
            list_item = f'

' \
                        f'
{article[3]}
' \
                        f'
автор: {article[1]}
' \
                        f'
год: {article[2]}
' \
                        f'
теги: {", ".join(article[4])}
' \
                        f'

'
            base_html += list_item
        base_html += '
'
        return base_html

# на случай, если в адресе не указан год - установим значение year=-1
def dashboard(request, year=-1):
    found_articles = []
    if year == -1:
        # если в адресе не указан год,
        # записываем в found_articles все статьи нашего блога
        found_articles = articles_list
    else:
        for article in articles_list:
            if year == article[2]:
                found_articles.append(article)
    beautiful_html = generate_html(found_articles)
    return HttpResponse(beautiful_html)

def article_by_id(request, id):
    found_articles = []
    for article in articles_list:
        if id == article[0]:
            found_articles.append(article)
    beautiful_html = generate_html(found_articles)
    return HttpResponse(beautiful_html)

def articles_by_tag(request, tag):
    found_articles = []
    for article in articles_list:
        if tag in article[4]:
            found_articles.append(article)
    beautiful_html = generate_html(found_articles)
    return HttpResponse(beautiful_html)


#
# License: BSD
#   https://raw.github.com/robotics-in-concert/rocon_tools/license/LICENSE
#
##############################################################################
# Imports
##############################################################################

import threading
import time
import rospy
import uuid
import unique_id
import functools

# Local imports
from .exceptions import ServicePairException

##############################################################################
# Request Handlers
##############################################################################

class RequestHandlerBase(object):

    def __init__(self, key):
        self.key = key  # uuid hexstring key (the request_handlers key)
        self.response = None

class BlockingRequestHandler(RequestHandlerBase):

    def __init__(self, key):
        super(BlockingRequestHandler, self).__init__(key)
        self.event = threading.Event()

class NonBlockingRequestHandler(RequestHandlerBase):

    def __init__(self, key, callback, error_callback):
        super(NonBlockingRequestHandler, self).__init__(key)
        self.timer = None
        self.callback = callback
        self.error_callback = error_callback

    def copy(self):
        '''
          The deepcopy function has some issues (related to threads),
          so using this independant copy method here. Note that this only
          ever gets used for non-blocking calls to help handle the
          race conditions between timeout handling and normal callback
          handling
        '''
        new_copy = NonBlockingRequestHandler(self.key, self.callback, self.error_callback)
        new_copy.timer = self.timer
        return new_copy

##############################################################################
# Client Class
##############################################################################

class ServicePairClient(object):
    '''
      The client side of a pubsub service pair.
    '''
    __slots__ = [
            '_publisher',
            '_subscriber',
            '_request_handlers',  # initiate, track and execute requests with these { hex string ids : dic of RequestHandler objects (Blocking/NonBlocking) }
            'ServicePairSpec',
            'ServicePairRequest',
            'ServicePairResponse',
            '_lock'               # prevent race conditions in handling of non-blocking callbacks and timeouts.
        ]

    ##########################################################################
    # Initialisation
    ##########################################################################

    def __init__(self, name, ServicePairSpec):
        '''
          @param name : resource name of service pair (e.g. testies for pair topics testies/request, testies/response)
          @type str
          @param ServicePairSpec : the pair type (e.g. rocon_service_pair_msgs.msg.TestiesPair)
          @type str
        '''
        try:
            p = ServicePairSpec()
            self.ServicePairSpec = ServicePairSpec
            self.ServicePairRequest = type(p.pair_request)
            self.ServicePairResponse = type(p.pair_response)
        except AttributeError:
            raise ServicePairException("Type is not an pair spec: %s" % str(ServicePairSpec))
        self._lock = threading.Lock()
        self._subscriber = rospy.Subscriber(name + "/response", self.ServicePairResponse, self._internal_callback)
        self._publisher = rospy.Publisher(name + "/request", self.ServicePairRequest)
        self._request_handlers = {}  # [uuid_msgs/UniqueId]

    def wait_for_service(self, timeout):
        '''
          Waits for the service pair server to appear.

          @param timeout : time to wait for data
          @type rospy.Duration

          @raise ROSException: if specified timeout is exceeded
          @raise ROSInterruptException: if shutdown interrupts wait
        '''
        timeout_time = time.time() + timeout.to_sec()
        while not rospy.is_shutdown() and time.time() < timeout_time:
            if self._subscriber.get_num_connections() > 0 and self._publisher.get_num_connections() > 0:
                return
            rospy.rostime.wallsleep(0.1)
        if rospy.is_shutdown():
            raise rospy.ROSInterruptException("rospy shutdown")
        else:
            raise rospy.ROSException("timeout exceeded while waiting for service pair server %s" % self._subscriber.resolved_name[:-len('/response')])

    ##########################################################################
    # Execute Blocking/NonBlocking
    ##########################################################################

    def __call__(self, msg, timeout=None, callback=None, error_callback=None):
        '''
          Initiates and executes the client request to the server. The type of arguments
          supplied determines whether to apply blocking or non-blocking behaviour.

          @param msg : the request message
          @type <name>Request

          @param timeout : time to wait for data
          @type rospy.Duration

          @param callback : user callback invoked for responses of non-blocking calls
          @type method with arguments (uuid_msgs.UniqueID, <name>Response)

          @return msg/id : for blocking calls it is the response message, for non-blocking it is the unique id
          @rtype <name>Response/uuid_msgs.UniqueID
        '''
        pair_request_msg = self.ServicePairRequest()
        pair_request_msg.id = unique_id.toMsg(unique_id.fromRandom())
        pair_request_msg.request = msg
        key = unique_id.toHexString(pair_request_msg.id)
        if callback == None and error_callback == None:
            self._request_handlers[key] = BlockingRequestHandler(key)
            return self._make_blocking_call(self._request_handlers[key], pair_request_msg, timeout)
        else:
            request_handler = NonBlockingRequestHandler(key, callback, error_callback)
            self._request_handlers[key] = request_handler.copy()
            self._make_non_blocking_call(request_handler, pair_request_msg, timeout)
            return pair_request_msg.id

    ##########################################################################
    # Private Support Methods
    ##########################################################################

    def _make_blocking_call(self, request_handler, msg, timeout):
        '''
          @param request_handler : information and event handler for the request
          @type RequestHandler

          @param msg : the request pair message structure
          @type self.ServicePairRequest
        '''
        self._publisher.publish(msg)
        if timeout is None:
            request_handler.event.wait()
        else:
            request_handler.event.wait(timeout.to_sec())
        if request_handler.response is not None:
            response = request_handler.response
        else:
            response = None
        del self._request_handlers[request_handler.key]
        return response

    def _make_non_blocking_call(self, request_handler, msg, timeout):
        '''
          @param request_handler : a copy of information and event handler for the request (used for the timer)
          @type RequestHandler

          @param msg : the request pair message structure
          @type self.ServicePairRequest
        '''
        self._publisher.publish(msg)
        if timeout is not None:
            # bind the key so the timer callback knows which request to handle.
            delete_request_handler = functools.partial(self._timer_callback, request_handler=request_handler)
            request_handler.timer = rospy.Timer(timeout, delete_request_handler, oneshot=True)

    def _timer_callback(self, unused_event, request_handler):
        '''
          Handle a timeout for non-blocking requests. This will call the user's defined error callback function
          (with args: (uuid_msgs.UniqueID, str)).

          @param event : regular rospy timer event object (not used)

          @param request_handler : a copy of the handler that gets bound when this callback is passed into the timer
          @type NonBlockingRequestHandler

          @todo respond on the error callback.
        '''
        already_handled = False
        self._lock.acquire()
        try:
            del self._request_handlers[request_handler.key]
        except KeyError:
            already_handled = True
        self._lock.release()
        if not already_handled:
            if request_handler.error_callback is not None:
                request_handler.error_callback(unique_id.toMsg(uuid.UUID(request_handler.key)), "timeout")

    def _internal_callback(self, msg):
        '''
          @param msg : message returned from the server (with pair id etc)
          @type self.ServicePairResponse
        '''
        # Check if it is a blocking call that has requested it.
        key = unique_id.toHexString(msg.id)
        already_handled = False
        non_blocking_request_handler = None
        self._lock.acquire()
        try:
            request_handler = self._request_handlers[key]
            request_handler.response = msg.response
            if isinstance(request_handler, BlockingRequestHandler):
                request_handler.event.set()
                already_handled = True
            else:  # NonBlocking
                # make a copy and delete so we can release the lock. Process after.
                non_blocking_request_handler = request_handler.copy()
                del self._request_handlers[key]
        except KeyError:
            already_handled = True  # it's either a blocking, or a non-blocking call handled by the timeout
        self._lock.release()
        if not already_handled:
            # Could use EAFP approach here since they will almost never be None, but this is more readable
            if non_blocking_request_handler.callback is not None:
                request_handler.callback(msg.id, msg.response)


import math
import numpy as np
from scipy import linalg
from channel import channel
from I_beam import I_beam
from RHS import RHS
from stress_from_strain import stress_from_strain
from get_data import get_data
import time
import matplotlib.pyplot as plt
import pandas as pd
from finite_strip_moment import finitestrip_shape
import os
import xlrd
from xlutils.copy import copy
from xlwt import Workbook

#cd c:/Users/saulg/Documents/year\ 4/IIB_Project/code/mycode

def find_nearest(array, value):
    array = np.asarray(array)
    idx = (np.abs(array - value)).argmin()
    return idx

factor = 5

list_M =[]
list_C =[]
list_stress = []
list_stress_c = []
list_Et= []
list_Et_c= []
list_type = []

for data_point in range(100,196):
     Material, Forming_process, Bending_type, Actual_Length, shape, Material_flat, Material_corner = get_data(data_point)
     if Bending_type == 4:
          """print(Material)
          print(Forming_process)
          print(Bending_type)
          print(shape)
          print(Actual_Length)
          print(Material_flat)
          print(Material_corner)"""

          number = 100
          max_L = Actual_Length*5
          min_L = shape[2]/5
          r = (max_L/min_L) ** (1/(number-1))
          Moment = []
          stress_list = []
          Moment_1 = []
          Curvature_1 = []
          Curvature = []
          Length = []
          Length_factor= True
          tic = time.perf_counter()

          for i in range(number):
               L = min_L * r**i
               Length.append(L)
               if i == 0:
                    A_initial = 0.00001
               elif i == 1:
                    A_initial = A
                    A_2 = A
               elif i == 2:
                    A_initial = abs(2 * A - A_2)
                    A_3 = A_2
                    A_2 = A
               else:
                    A_initial = abs(3 * A - 3 * A_2 + A_3)
                    A_3 = A_2
                    A_2 = A

               M, A, max_stress = finitestrip_shape(L, shape, Material_flat, Material_corner, A_initial)
               C=A
               if M =="Fail":
                    Length.pop()
                    break
               if Length_factor:
                    Moment_1.append(M)
                    Curvature_1.append(M)

                    for num in range(2,round(factor*L/(shape[2]))):
                         M_2 = Moment[find_nearest(Length, L/num)]
                         C_2 = Curvature[find_nearest(Length, L/num)]
                         if M > M_2:
                              M = M_2
                              max_stress = stress_list[find_nearest(Length, L/num)]
                         if C > C_2:
                              C = C_2

               Moment.append(M)#
               Curvature.append(C)
               stress_list.append(max_stress)
          toc = time.perf_counter()
          print(f"Done in {toc - tic:0.4f} seconds")
          typ = "Flexural"
          M, A, max_stress= finitestrip_shape(Actual_Length, shape, Material_flat, Material_corner, A_initial)
          C=A
          if Length_factor and M !="Fail":
               for num in range(2,round(factor*Actual_Length/(shape[2]))):

                    M_2 = Moment[find_nearest(Length, Actual_Length/num)]
                    C_2 = Curvature[find_nearest(Length, Actual_Length/num)]
                    if M_2<M:
                         M=M_2
                         typ = "Local"
                         max_stress = stress_list[find_nearest(Length, Actual_Length/num)]
                    if C > C_2:
                         C = C_2
          print(data_point, Actual_Length,M,C, max_stress)
          list_M.append(M)
          list_C.append(C)
          list_stress.append(max_stress[0][0])
          list_stress_c.append(max_stress[1][0])
          list_Et.append(max_stress[0][1])
          list_Et_c.append(max_stress[1][1])
          list_type.append(typ)

          plt.semilogx([Actual_Length], [M],'rx')
          if Length_factor:
               plt.semilogx(Length, Moment_1, linewidth = 0.4, color = "black", label='Singe Half Wavelength')
               plt.semilogx(Length, Moment, linewidth = 1.2, color = "black", label='Multiple Half Wavelengths')
          else:
               plt.semilogx(Length, Moment, linewidth = 1.4, color = "black")

          #plt.semilogx([1000], [151], 'rx',  label='Buckling')
          #plt.semilogx([1000], [169], 'bx',  label='Failure')

          plt.xlabel('L / mm')
          plt.ylabel('Buckling Moment / KNm')
          plt.grid(True,'both')
          plt.title(data_point)
          plt.legend()
          #plt.show()
     else:
          list_M.append("")
          list_C.append("")
          list_stress.append("")
          list_stress_c.append("")
          list_Et.append("")
          list_Et_c.append("")
          list_type.append("")
print("M")
for n in list_M:
     print(n)
print("list_C")
for n in list_C:
     print(n)
print("list_stress")
for n in list_stress:
     print(n)
print("list_stress_c")
for n in list_stress_c:
     print(n)
print("list_Et")
for n in list_Et:
     print(n)
print("list_Et_c")
for n in list_Et_c:
     print(n)
print("type")
for n in list_type:
     print(n)

"""
print(finitestrip_shape(L = 1000, shape = "channel", b = 138.60, d = 202.05, r = 5, t_flange = 6.11, t_web = 6.01, c = 23, Eel = 195000, spr = 520, n = 7.5, v = 0.3, k = -0.46))

x1=[]
y1=[]

for f in file:
     x1.append(f[0])
     y1.append(f[1])
plt.semilogx(x1, y1, linewidth = 0.4, color = "red", label='CUFSM')"""


from datetime import datetime

import pendulum
from airflow import DAG
from airflow.operators.bash_operator import BashOperator

default_args = {
    'owner': 'hungvv1',
    'email': ['vuviethung.98.hust@gmail.com'],
    'email_on_failure': True,
    'email_on_retry': True
}

dag = DAG('project_analytics_monthly',
            default_args=default_args,
            description='Các jobs chạy phân tích dự án theo tháng',
            schedule_interval='@once',
            start_date=datetime(2018, 1, 1, tzinfo=pendulum.timezone('Asia/Ho_Chi_Minh')),
            catchup=False
        )

t1 = BashOperator(
    task_id="project_statistics_monthly",
    bash_command='/home/vuviethung/code/code/cen_jobs/project_statistics_monthly/run-project-filtering.sh',
    dag=dag,
    queue='worker_2'
)


import discord
import asyncio
import random
from discord.ext import commands
import os
import datetime
import pymongo
import json
import math
m=None
db_client=pymongo.MongoClient(os.getenv("DB_URL"))
db1_client=pymongo.MongoClient(os.getenv("DB2_URL"))

class guide(commands.Cog):
    def __init__(self,bot):
        self.bot=bot
    @commands.command()
    @commands.guild_only()
    async def guide(self,ctx,number=None):
        if number==None:
            number=1
        number=int(number)-1
        #registration
        list_of_pages=[]
        embed=discord.Embed(title="Guide",description="**Registration**\n\nIn order to use some features of the bot or challenge others, you need to have an account on your name. You can create an account by using `c!register` command.\nAfter creating an account you can check your profile using `c!profile` command.")
        embed.set_image(url="https://raw.githubusercontent.com/Shrikar-Kota/Personalcrickgame/master/images/register-pic.png")
        embed.set_footer(text="Page : 1/8")
        list_of_pages.append(embed)
        #challenge
        embed=discord.Embed(title="Guide",description="**Challenge**\n\nAfter you are done with creating your account you can now use all the bot's commands and play with it.\nTo play with the other person, either you need to challenge them or they need to challenge you which you can do by using `c!challenge` command tagging the opponent.\nAfter you throw a challenge, the opponent can accept it using `c!accept` or decline it using `c!decline` command.")
        embed.set_footer(text="Page : 2/8")
        embed.set_image(url="https://raw.githubusercontent.com/Shrikar-Kota/Personalcrickgame/master/images/Challenge-pic.png")
        list_of_pages.append(embed)
        #select_team
        embed=discord.Embed(title="Guide",description="**Selecting team**\n\nAfter the opponent accepts the challenge, the bot will prompt you to select a team from default teams.\nYou can check the available leagues by typing `c!leagues`. After checking the available leagues, you can select a league from it by using `c!st league_id`, here league id is the id given by the respective league.\nAfter selecting the league, the bot will prompt you to select a team which you can do by using `c!select_team <league_id> <team_id>`.")
        embed.set_footer(text="Page : 3/8")
        embed.set_image(url="https://raw.githubusercontent.com/Shrikar-Kota/Personalcrickgame/master/images/select-team-pic.png")
        list_of_pages.append(embed)
        #set_overs
        embed=discord.Embed(title="Guide",description="**Setting overs**\n\nAfter you are done with choosing your team, you need to set the overs of the match. This can be done using `c!so` command.")
        embed.set_footer(text="Page : 4/8")
        embed.set_image(url="https://raw.githubusercontent.com/Shrikar-Kota/Personalcrickgame/master/images/set-overs-pic.png")
        list_of_pages.append(embed)
        #toss
        embed=discord.Embed(title="Guide",description="**Toss**\n\nSimilar to the real life cricket game, the teams have to choose what they gonna do first. This can be done by doing toss. Toss can be done using `c!toss` command.\nAfter one of the players type the command, the opponent needs to send his choice in the chat i.e., heads/tails.")
        embed.set_footer(text="Page : 5/8")
        embed.set_image(url="https://raw.githubusercontent.com/Shrikar-Kota/Personalcrickgame/master/images/toss-pic.png")
        list_of_pages.append(embed)
        #choose
        embed=discord.Embed(title="Guide",description="**Choosing**\n\nAfter the toss, the toss-winner should choose either bowling or batting by using `c!choose` command.")
        embed.set_footer(text="Page : 6/8")
        embed.set_image(url="https://raw.githubusercontent.com/Shrikar-Kota/Personalcrickgame/master/images/choose-pic.png")
        list_of_pages.append(embed)
        #select_player
        embed=discord.Embed(title="Guide",description="**Selecting players**\n\nAfter choosing either batting or bowling, the teams can choose their players by using `c!sp` command.")
        embed.set_footer(text="Page : 7/8")
        embed.set_image(url="https://raw.githubusercontent.com/Shrikar-Kota/Personalcrickgame/master/images/select-player-pic.png")
        list_of_pages.append(embed)
        #bowl
        embed=discord.Embed(title="Guide",description="**Selecting players**\n\nAfter choosing your players, now you are all set to play the match. The bowling team needs to use the `c!bowl` command and the batting team needs to wait patiently for their turn to come :wink:.")
        embed.set_footer(text="Page : 8/8")
        embed.set_image(url="https://raw.githubusercontent.com/Shrikar-Kota/Personalcrickgame/master/images/bowl-pic.png")
        list_of_pages.append(embed)
        global m
        m=await ctx.send(embed=list_of_pages[number])
        await m.add_reaction("⬅")
        await m.add_reaction("➡")
        def check(reaction, user):
            return (user.id==ctx.message.author.id and m.id==reaction.message.id)
        while 1:
            try:
                reaction, user = await self.bot.wait_for('reaction_add', timeout=30.0, check=check)
            except asyncio.TimeoutError:
                return
            else:
                m = await ctx.channel.fetch_message(m.id)
                if str(reaction.emoji)=="➡":
                    e=m.embeds
                    e=str(e[0].footer.text)
                    e=e.split(":")
                    e=e[1].split("/")
                    count=int(e[0])
                    if count>len(list_of_pages)-1:
                        count=0
                    await m.edit(embed=list_of_pages[count])
                    await m.remove_reaction("➡",user)
                elif str(reaction.emoji)=="⬅":
                    e=m.embeds
                    e=str(e[0].footer.text)
                    e=e.split(":")
                    e=e[1].split("/")
                    count=int(e[0])-2
                    if count==-1:
                        count=len(list_of_pages)-1
                    await m.edit(embed=list_of_pages[count])
                    await m.remove_reaction("⬅",user)
                else:
                    await m.remove_reaction(str(reaction.emoji),user)

def setup(bot):
	bot.add_cog(guide(bot))


import contextlib
from functools import wraps

class cached_property(object):
    """
    Property which caches the result of the given `getter`.

    :param doc: Optional docstring which is used instead of the `getter`\s
                docstring.
    """
    def __init__(self, getter, doc=None):
        self.getter = getter
        self.__module__ = getter.__module__
        self.__name__ = getter.__name__
        self.__doc__ = doc or getter.__doc__

    def __get__(self, obj, type=None):
        if type is None:
            return self
        value = obj.__dict__[self.__name__] = self.getter(obj)
        return value

def service(func):
    func.__transactional__ = True
    return func

def _transactional(session_provider):
    def wrap_function(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            session = session_provider()
            try:
                ret = func(*args, **kwargs)
                session.commit()
                return ret
            except:
                session.rollback()
                raise
        return wrapper
    return wrap_function

def transactional(instance, session_provider):
    for key in dir(instance):
        func = getattr(instance, key)

        if callable(func) and getattr(func, '__transactional__', False):
            setattr(instance, key, wraps(func)(_transactional(session_provider)(func)))
    return instance


# This file is part of Indico.
# Copyright (C) 2002 - 2023 CERN
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see the
# LICENSE file for more details.

import pytest

from indico.modules.events.contributions.lists import ContributionListGenerator
from indico.modules.events.contributions.models.persons import AuthorType, ContributionPersonLink
from indico.modules.events.models.persons import EventPerson
from indico.modules.events.registration.models.forms import RegistrationForm
from indico.modules.events.registration.models.registrations import Registration, RegistrationState

@pytest.fixture
def create_registration(dummy_event):
    """Return a callable that lets you create a contribution."""

    def _create_registration(user, regform, **kwargs):
        return Registration(
            first_name='Guinea',
            last_name='Pig',
            checked_in=True,
            state=RegistrationState.complete,
            currency='USD',
            email=user.email,
            user=user,
            registration_form=regform,
            **kwargs
        )

    return _create_registration

def test_filter_contrib_entries(app, db, dummy_event, create_user, create_contribution, create_registration):
    registered_user = create_user(1)
    registered_speaker = create_user(2)
    unregistered_user = create_user(3)
    dummy_regform = RegistrationForm(event=dummy_event, title='Registration Form', currency='USD')
    dummy_event.registrations.append(create_registration(registered_user, dummy_regform))
    dummy_event.registrations.append(create_registration(registered_speaker, dummy_regform))
    registered_speaker_contribution = create_contribution(dummy_event, 'Registered Speaker', person_links=[
        ContributionPersonLink(person=EventPerson.create_from_user(registered_speaker, dummy_event),
                               is_speaker=True)
    ])
    registered_speaker_author_contribution = create_contribution(
        dummy_event, 'Registered Speaker Author', person_links=[
            ContributionPersonLink(person=EventPerson.for_user(registered_speaker, dummy_event),
                                   is_speaker=True, author_type=AuthorType.primary)
        ])
    unregistered_speaker_registered_author_contribution = create_contribution(
        dummy_event, 'Unregistered Speaker, Registered Author', person_links=[
            ContributionPersonLink(person=EventPerson.for_user(unregistered_user, dummy_event),
                                   is_speaker=True),
            ContributionPersonLink(person=EventPerson.for_user(registered_user, dummy_event),
                                   author_type=AuthorType.primary)
        ])
    registered_speaker_unregistered_author_contribution = create_contribution(
        dummy_event, 'Registered Speaker, Unregistered Author', person_links=[
            ContributionPersonLink(person=EventPerson.for_user(registered_user, dummy_event), is_speaker=True),
            ContributionPersonLink(person=EventPerson.for_user(unregistered_user, dummy_event),
                                   author_type=AuthorType.primary)
        ])
    # Filter contributions with registered users
    with app.test_request_context():
        list_gen = ContributionListGenerator(dummy_event)
        list_gen.list_config['filters'] = {'items': {'people': {'registered'}}}
        result = list_gen.get_list_kwargs()
    assert result['contribs'] == [
        registered_speaker_contribution,
        registered_speaker_author_contribution,
        unregistered_speaker_registered_author_contribution,
        registered_speaker_unregistered_author_contribution
    ]

    # Filter contributions with registered speakers
    list_gen.list_config['filters'] = {'items': {'speakers': {'registered'}}}
    with app.test_request_context():
        result = list_gen.get_list_kwargs()
    assert result['contribs'] == [
        registered_speaker_contribution,
        registered_speaker_author_contribution,
        registered_speaker_unregistered_author_contribution
    ]

    # Filter contributions with unregistered speakers and registered users
    list_gen.list_config['filters'] = {'items': {'speakers': {'not_registered'}, 'people': {'registered'}}}
    with app.test_request_context():
        result = list_gen.get_list_kwargs()
    assert result['contribs'] == [
        unregistered_speaker_registered_author_contribution
    ]


'''
Created on 2017年12月28日

@author: Administrator
'''

import os
import grpc
import sys
import getopt
import time
from concurrent import futures
import cProfile

root_path = os.path.dirname(__file__)
sys.path.append('./')
sys.path.append('../')
sys.path.append('../proto')

import chat_pb2
import chat_pb2_grpc

import route_database_pb2
import route_database_pb2_grpc

import route_db

_ONE_DAY_IN_SECONDS = 60 * 1

class Calc(chat_pb2_grpc.CalcServicer):
    def add(self, request, context):
        return chat_pb2.Result(c = request.a + request.b);

    def sub(self, request, context):
        return chat_pb2.Result(c = request.a - request.b);

    def mul(self, request, context):
        return chat_pb2.Result(c = request.a * request.b);

class Greeter(chat_pb2_grpc.GreeterServicer):

    def SayHello(self, request, context):
        return chat_pb2.HelloReply(message='Hello, %s!' % request.name)

class Route(route_database_pb2_grpc.RouteGreeterServicer):

    def __init__(self):
        #     db = cProfile.run('route_db.read_route_database()')
        self.db = route_db.read_route_database()
        print(self.db)

    def GetFeature(self, request, context):
        for feature in self.db:
            if feature.location == request:
                return feature
        return None

    def ListFeatures(self, request, context):
        pass

    def RecordRoute(self, request, context):
        pass

    def RouteChat(self, request, context):
        pass

def client():
    channel = grpc.insecure_channel('localhost:50052')
    stub = chat_pb2_grpc.GreeterStub(channel)
#     routestub = route_database_pb2_grpc.RouteGreeterStub(channel)
    calcstub = chat_pb2_grpc.CalcStub(channel)
    tm1 = time.time()

#     for i in range(3000):
#         t = time.time()
#         reponse = stub.SayHello(chat_pb2.HelloRequest(name='liwei'))
#         print(i, ' >>>>>>>>', reponse, time.time() - t)

#     for _ in range(3000):
#         t = time.time()
#         reponse = routestub.GetFeature(route_database_pb2.FeatureRequest(route_database_pb2.Point(latitude=408122808, longitude=743999179)))
#         print(i, ' >>>>>>>>', reponse, time.time() - t)

    for i in range(3000):
#         t = time.time()
        reponse = calcstub.add(chat_pb2.Value(a=10, b=20))
#         print(i, ' >>>>>>>>', reponse, time.time() - t)

#         t = time.time()
#         reponse = calcstub.sub(chat_pb2.Value(a=10, b=20))
#         print(i, ' >>>>>>>>', reponse, time.time() - t)
#
#         t = time.time()
#         reponse = calcstub.mul(chat_pb2.Value(a=10, b=20))
#         print(i, ' >>>>>>>>', reponse, time.time() - t)

    tm2 = time.time()

    print('all time: ', tm2-tm1)

def server():
    srv = grpc.server(futures.ThreadPoolExecutor(max_workers=(8)))
    chat_pb2_grpc.add_GreeterServicer_to_server(Greeter(), srv)
    chat_pb2_grpc.add_CalcServicer_to_server(Calc(), srv)
#     route_database_pb2_grpc.add_RouteGreeterServicer_to_server(Route(), srv)
    srv.add_insecure_port('[::]:50052')
    srv.start()

    try:
        while True:
            print('looping')
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        srv.stop(0)

def main():
    opts, _ = getopt.getopt(sys.argv[1:], 'ht:')
    t = ''
    for op, v in opts:
        if op == '-t':
            t = v

    if t == 's':
        server()
    elif t == 'c':
        client()

if __name__ == '__main__':
    main()


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

import csv
import cvxpy as cp
import numpy as np
import matplotlib.pyplot as plt

train_data =[]
with open("hw04_sample_vectors.csv", "r") as csv_file:
    reader = csv.reader(csv_file, delimiter=',')
    for data in reader:
        train_data.append(data)
csv_file.close()

label =[]
with open("hw04_labels.csv", "r") as csv_file:
    reader = csv.reader(csv_file, delimiter=',')
    for data in reader:
        label.append(data)
csv_file.close()

for data in label:
    if np.linalg.norm(data) == 0:
        data[0] = -1

x2 = np.arange(-0.6, 0.4, 0.01)

def plot_graph(name, omega, omega_0):
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.scatter(train_data[:1000, 0], train_data[:1000, 1], c='b', s=3)
    ax1.scatter(train_data[1000:, 0], train_data[1000:, 1], c='r', s=3)
    x1 = (-1*omega[1]*x2 - omega_0) / omega[0]
    ax1.plot(x1, x2, 'g-')
    ax1.set_xlabel('x1')
    ax1.set_ylabel('x2')
    fig.savefig('hw4_2_c_{}.pdf'.format(name))

label = np.asarray(label, dtype=np.float32)
train_data = np.asarray(train_data, dtype=np.float32)
#one_arr = np.ones(train_data.shape[0])
#one_arr = np.resize(one_arr, (train_data.shape[0], 1))
#train_data = np.concatenate((train_data, one_arr), axis=1)

omega = cp.Variable(2)
omega_0 = cp.Variable(1)

objective = cp.Minimize(cp.norm(omega))
constraints = [label[i] * (omega.T * train_data[i] + omega_0) >= 1
               for i in range(len(label))]
prob = cp.Problem(objective, constraints)

result = prob.solve()

print(omega.value)
print(omega_0.value)

plot_graph('1', omega.value, omega_0.value)


import json, os
from bson import ObjectId, Regex

class Utils:

    def __init__(self, db):
        self.db = db

    def store_ids(self, admin_email, id_list):

        self.db.files.update(
            {'admin_email': admin_email},
            {'admin_email': admin_email, 'id_list': id_list},
            upsert=True
        )

    def remove_ids(self, email):

        print('--remove_ids--')

        obj = self.db.files.find_one({"admin_email": email})

        print(obj)
        if obj:

            col_names = self.db.collection_names()

            print(obj)

            if col_names:

                for name in col_names:

                    for id in obj['id_list']:

                        try:
                            self.db[name].delete_one({
                                '_id': ObjectId(id)
                            })

                            print('Deleted %s : %s' % (name, id))

                        except:
                           pass

            # self.db.files.remove({'_id': ObjectId(obj['_id'])})
            # x = self.db.users.remove({'email': obj['admin_email']})
            # print(x)


from __future__ import absolute_import
from django.conf import settings
from celery.utils.log import get_task_logger

from biostar import const
from datetime import timedelta

logger = get_task_logger(__name__)

from celery import Celery

app = Celery('biostar')

# Read the configuration from the config file.
app.config_from_object(settings.CELERY_CONFIG)

# Discover tasks in applications.
app.autodiscover_tasks(
    lambda: ["biostar.mailer"]
)

@app.task
def data_cleanup(days=1, weeks=20):
    "Reduces messages and post views"

    from biostar.apps.posts.models import PostView
    from biostar.apps.messages.models import Message

    # Reduce post views.
    past = const.now() - timedelta(days=days)
    query = PostView.objects.filter(date__lt=past)
    msg = "Deleting %s PostViews" % query.count()
    logger.info(msg)
    query.delete()

    # Reduce messages.
    since = const.now() - timedelta(weeks=weeks)
    query = Message.objects.filter(sent_at__lt=since)
    msg = "Deleting %s messages" % query.count()
    logger.info(msg)
    query.delete()

@app.task
def test(*args, **kwds):
    logger.info("*** executing task %s %s, %s" % (__name__, args, kwds))
    return 1000


import os
import sys
import time
from asyncio import sleep, ensure_future

import discord
import pandas as pd
import jaconv

class Pokemon(discord.Client):
    def __init__(self):
        super(Pokemon, self).__init__()

        self.table = pd.read_csv(
            './pokemon.tsv', sep='\t', dtype=str
        ).fillna("")
        self.table['No.str'] = \
            self.table['No.'].apply(lambda x: f'No.{x}')

        self.table_moves = pd.read_csv(
            './pokemon_moves.tsv', sep='\t', dtype=str
        ).fillna("")
        self.table_moves_katakana_lower = \
            self.table_moves.applymap(
                lambda name: jaconv.hira2kata(name.lower())
            )

    async def on_message(self, message):
        if len(message.content) == 0:
            return

        content = message.content.lower()
        content_normalized = jaconv.hira2kata(content[0].upper() + content[1:])

        if content_normalized == 'No.???':
            return

        # filter pokemon list
        match = self.table[
            self.table.drop(columns='No.').applymap(
                lambda name: name == content_normalized
            ).any(axis=1)
        ]
        if len(match) != 0:
            # display 1st pokemon of filtered list (expected that only 1 item has been extracted)
            match = match.drop(columns='No.str')
            matchseq = match.iloc[0]
            embed = format_pokeinfo(zip(matchseq.index, matchseq))
            await message.channel.send(embed=embed)

        # filter move(waza) list
        content_normalized_lower = content_normalized.lower()
        match_moves = self.table_moves[
            self.table_moves_katakana_lower.applymap(
                lambda name: name == content_normalized_lower
            ).any(axis=1)
        ]
        if len(match_moves) != 0:
            # display 1st move of filtered list (expected that only 1 item has been extracted)
            matchseq_moves = match_moves.iloc[0]
            embed = format_pokeinfo(zip(matchseq_moves.index, matchseq_moves))
            await message.channel.send(embed=embed)

def format_pokeinfo(index_value_seq):
    name_jp = 'NO DATA'
    description = ''
    for index, value in index_value_seq:
        if index in ['日', '🇯🇵']:
            if value == '':
                continue
            name_jp = value
            continue
        description += (
            f'{index} [NO DATA]\n'
            if value == '' else
            f'{index} `{value}`\n'
        )
    title = name_jp
    description = description[:-1]
    return discord.Embed(title=title, description=description)

if __name__ == '__main__':
    print("sudawoodo has started")
    poke = Pokemon()
    poke.run(os.getenv('DISCORD_BOT_TOKEN'))


import sys

from flask import Flask, Response, request
import requests
import json

from settings import *

LOAN_XML = """<?xml version='1.0' encoding='UTF-8'?>
  <item_loan>
    <circ_desk>{}</circ_desk>
    <library>{}</library>
  </item_loan>
""".format(CIRC_DESK, LIB_NAME)

app = Flask(__name__)

@app.route('/')
def root():
    return app.send_static_file('self-check.html')

@app.route('/login/<userid>/<lastname>')
def login(userid, lastname):
    url = "{}/users/{}".format(API_URL, userid)
    params = {}
    params['apiKey'] = API_KEY
    params['expand'] = "loans,requests,fees"
    params['format'] = "json"
    response = requests.get(url, params=params)
    if response.json().get("last_name").lower() == lastname.lower():
        if response.status_code == 200:
            return Response(response, mimetype="application/json")
        else:
            return Response('Incorrect Login
 Try Again', 500)
    else:
        return Response('Incorrect Login
Try Again', 401)

@app.route('/checkout/<userid>/<barcode>')
def loan(userid, barcode):
    # test to see if book is already checked out
    barcodeurl = "{}/items".format(API_URL)
    params = {'apiKey': API_KEY,
              'item_barcode': barcode,
              'format': 'json'}
    redirect = requests.get(barcodeurl, params=params, allow_redirects=True)
    url = redirect.url
    url, _ = url.split('?')
    url = '{}/loans'.format(url)

    # del params['item_barcode']
    loans_response = requests.get(url, params=params)
    already_checked_out = loans_response.json().get('item_loan', False)

    # error handling
    if already_checked_out:
        return Response('This item is already checked out', 409)
    if loans_response.status_code == 404:
        return Response('Error: Invalid Barcode', 404)

    # Checkout the item
    url = "{}/users/{}/loans".format(API_URL, userid)
    headers = {'Content-Type': 'application/xml', 'dataType': "xml"}
    response = requests.post(url, params=params, headers=headers, data=LOAN_XML)
    if response.status_code == 400 and "reference" in redirect.text.lower():
        return Response('Cannot Checkout: Reference Materials', 403)
    if response.status_code == 400 and "non-circulating" in redirect.text.lower():
        return Response('Cannot Checkout: Reserve Materials', 403)
    if response.status_code == 400 and "loan limit" in response.text.lower():
        return Response('Item cannot be loaned due to loan limit being reached', 411)
    return Response(response, mimetype="application/json")

if __name__ == "__main__":
    app.run()


__all__ = [
    "DeviceTreeWidget"
]

from .var_widgets import (
    VarTreeview,
    VarButton,
    VarLabelFrame
)
from qemu import (
    qvd_get
)
from six.moves.tkinter_ttk import (
    Scrollbar
)
from six.moves.tkinter import (
    Radiobutton,
    StringVar
)
from common import (
    mlget as _
)
from .gui_dialog import (
    GUIDialog
)

class DeviceTreeWidget(GUIDialog):
    def __init__(self, root, *args, **kw):
        GUIDialog.__init__(self, master = root, *args, **kw)
        self.qom_type_var = root.qom_type_var

        self.title(_("Device Tree"))
        self.grid()

        self.columnconfigure(0, weight = 1)
        self.rowconfigure(0, weight = 1)

        self.columnconfigure(2, minsize = 200)

        self.attributes("-topmost", 1)
        geom = "+" + str(int(root.winfo_rootx())) \
             + "+" + str(int(root.winfo_rooty()))
        self.geometry(geom)

        self.focus()

        self.device_tree = VarTreeview(self)
        self.device_tree["columns"] = ("Macros")

        self.device_tree.heading("#0", text = _("Devices"))
        self.device_tree.heading("Macros", text = _("Macros"))

        self.device_tree.bind("<ButtonPress-1>", self.on_b1_press_dt)

        self.device_tree.grid(
            row = 0,
            column = 0,
            sticky = "NEWS"
        )

        #Add Scrollbar
        ysb = Scrollbar(self,
            orient = "vertical",
            command = self.device_tree.yview
        )
        xsb = Scrollbar(self,
            orient = "horizontal",
            command = self.device_tree.xview
        )
        self.device_tree['yscroll'] = ysb.set
        self.device_tree['xscroll'] = xsb.set
        ysb.grid(row = 0, column = 1, sticky = "NS")
        xsb.grid(row = 1, column = 0, sticky = "EW")

        self.add_button = VarButton(
            self,
            text = _("Select"),
            command = self.on_select_qom_type
        )
        self.add_button.grid(row = 1, column = 2, sticky = "WE")
        self.add_button.config(state = "disabled")

        self.fr = VarLabelFrame(self, text = _("Select QOM type"))
        self.fr.grid(row = 0, column = 2, sticky = "SEWN")

        # Check exception before __init__ call.
        bp = root.mach.project.build_path
        qvd = qvd_get(bp, version = root.mach.project.target_version)
        # the QOM type of roots[0] is "device"
        roots = qvd.qvc.device_tree[0]["children"]
        self.qom_create_tree("", roots)

    def qom_create_tree(self, parent_id, dt_list):
        dt_list.sort(key = lambda x: x["type"])
        for dict_dt in dt_list:
            if "macro" in dict_dt:
                value = ""
                for macro in dict_dt["macro"]:
                    value = value + " " + macro
            else:
                value= "None"
            tr_id = self.device_tree.insert(parent_id, "end",
                text = dict_dt["type"],
                values = value
            )
            if "children" in dict_dt:
                self.qom_create_tree(tr_id, dict_dt["children"])

    def on_select_qom_type(self):
        self.qom_type_var.set(self.v.get())
        self.destroy()

    # write selected qom type in qom_type_var
    def on_b1_press_dt(self, event):
        item = self.device_tree.identify('item', event.x, event.y)
        if item:
            self.add_button.config(state = "active")
            for widget in self.fr.winfo_children():
                widget.destroy()

            dt_type = self.device_tree.item(item, "text")
            self.v = StringVar()
            self.v.set(dt_type) # initialize

            b = Radiobutton(self.fr,
                text = dt_type,
                variable = self.v,
                value = dt_type
            )
            b.pack(anchor = "w")

            macros = self.device_tree.item(item, "values")[0]
            if not macros == "None":
                l = macros.split(" ")
                for mstr in l:
                    b = Radiobutton(
                        self.fr,
                        text = mstr,
                        variable = self.v,
                        value = mstr
                    )
                b.pack(anchor = "w")

            b.select()