def num_check(question, type):

    if type == int:
        error_type = "a number"
    else:
        error_type = "an integer"

    error = "Please enter {} that is more than zero \n".format(error_type, type)

    valid = False
    while not valid:
       try:
           response = type(input(question))
           if response <=0:
               print(error)
           else:
               return response

       except ValueError:
           print(error)


#coding: utf-8
import time

sec = 0
minutes = 0
while True:
	minutes += 1
	while True:
		print(sec)
		time.sleep(1)
		sec += 1
		if sec == 60:
			sec = 0
			minutes = str(minutes)
			print("\n" + minutes + " MIN COMPLITE\n")
			minutes = int(minutes)
			break


# bgp_interface.py
# Author: Thomas MINIER - MIT License 2017-2018
from flask import Blueprint, request, Response, render_template, abort, json
from ldf.tpf_metadata import TPFMetadata
from query_engine.optimizer.plan_builder import build_left_plan
from query_engine.iterators.utils import itimeout
from http_server.bgp.schemas import BGPQuery
from http_server.utils import secure_url, format_marshmallow_errors
from time import time

def bgp_blueprint(datasets):
    """Get a Blueprint that implement a Nested Loop Join interface with deadlines on /nlj/<dataset-name>"""
    bgp_blueprint = Blueprint('bgp-ldf', __name__)

    @bgp_blueprint.route('/bgp/', methods=["GET"])
    def nlj_index():
        mimetype = request.accept_mimetypes.best_match(['application/trig', 'text/html'])
        datasets_infos = datasets._config["datasets"]
        if mimetype is 'text/html':
            return render_template("index_sage.html", datasets=datasets_infos)
        return Response(response="", content_type="application/trig")

    @bgp_blueprint.route('/bgp/<dataset_name>', methods=['GET', 'POST'])
    def get_nlj_fragment(dataset_name):
        dataset = datasets.get_dataset(dataset_name)
        if dataset is None:
            abort(404)
        mimetype = request.accept_mimetypes.best_match(['application/trig', 'text/html'])
        url = secure_url(request.url)
        # process GET request as regular TPF queries
        if request.method == "GET" or (not request.is_json):
            (subject, predicate, obj, page) = (
                request.args.get("subject", ""),
                request.args.get("predicate", ""),
                request.args.get("object", ""),
                request.args.get("page", 1))
            (fragment, cardinality) = dataset.get_fragment(url, subject, predicate, obj, int(page))

            metadata = TPFMetadata(url, cardinality, dataset_name, dataset.page_size())
            fragment.add_metadata(metadata)
            if mimetype is 'text/html':
                return render_template("sage.html", fragment=fragment, metadata=metadata)
            return Response(response=fragment.serialize(), content_type="application/trig")

        # else, process POST requests as NLJ requests
        post_query, errors = BGPQuery().load(request.get_json())
        if len(errors) > 0:
            return Response(status="400", response=format_marshmallow_errors(errors), content_type='text/plain')
        quota = int(request.args.get("quota", dataset.deadline()))
        bgp = post_query['bgp']
        controls = post_query['controls']
        # build physical query plan, then execute it with the given number of tickets
        start = time()
        join = build_left_plan(bgp, dataset._factory, controls=controls)
        loadingTime = (time() - start) * 1000
        bindings = list(itimeout(join, quota))
        # compute controls for the next page
        hasNext = join.has_next()
        start = time()
        controls = join.export() if hasNext else None
        exportTime = (time() - start) * 1000
        stats = {'import': loadingTime, 'export': exportTime}
        # if controls is not None:
        #     controls['hash'] = hash_controls(controls)
        return json.jsonify(bindings=bindings, cardinality=len(bindings), next=hasNext, controls=controls, stats=stats)
    return bgp_blueprint


import numpy as np
from scipy.stats import norm

class BlackScholes():
    def __init__(self, underlying, strike, volatility, interestRate, timeToExpiration):
        self.So = underlying
        self.X = strike
        self.sigma = volatility / (100 * np.sqrt(252))
        self.r = interestRate / 100
        self.t = timeToExpiration
        self.d1 = 0
        self.d2 = 0

        self.callPrice = 0
        self.putPrice = 0

        self.deltaCall = 0
        self.deltaPut = 0
        self.vega = 0

        self._calcD1()
        self._calcD2()
        self._calcPrices()
        self._calcGreeks()

    def _calcD1(self):
        self.d1 = (np.log(self.So/self.X) + self.t * (self.r / 365 + (self.sigma ** 2) / 2)) / (self.sigma * np.sqrt(self.t))

    def _calcD2(self):
        self.d2 = self.d1 - self.sigma * np.sqrt(self.t)

    def _calcPrices(self):
        self.callPrice = self.So * norm.cdf(self.d1) - self.X * np.exp(-self.r * self.t / 365) * norm.cdf(self.d2)
        self.putPrice =  - self.So * norm.cdf( - self.d1) + self.X * np.exp(-self.r * self.t / 365) * norm.cdf(- self.d2)

    def _calcGreeks(self):
        self.deltaCall = norm.cdf(self.d1)
        self.deltaPut = (norm.cdf(self.d1) - 1)
        self.vega = (1/100)  * np.sqrt(self.t) * (1 / (2 * np.pi)) * np.exp(((-self.d1) ** 2) / 2)


from django.conf.urls import url

from .views import (
    IndexView,
    Http404View,
    Http500View,
)

urlpatterns = [
    url(r'^http-404/$', Http404View.as_view(), name='http_404'),
    url(r'^http-500/$', Http500View.as_view(), name='http_500'),
    url(r'', IndexView.as_view(), name='index'),
]


### GT ID: cchen376
### Assignment #1 CS7641 Fall 2019

#!/usr/bin/env python
# coding: utf-8

# In[1]:

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
from sklearn.model_selection import learning_curve
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
from helperfuncs import *
# In[2]:

seed=0
scoring='f1'

df_egrid = pd.read_csv('Data_for_UCI_named.csv')
df_egrid['stabf']=np.where(df_egrid['stabf']=='unstable',0,1)
y_egrid=df_egrid['stabf']
X_egrid=df_egrid.drop(['stabf','stab'],axis=1)

df_def = pd.read_csv('default_data_cleaned.csv')
y_def=df_def['default payment next month']
X_def=df_def.drop(['default payment next month','ID','Unnamed: 0'],axis=1)

X_train_egrid, X_test_egrid, y_train_egrid, y_test_egrid= train_test_split(X_egrid,y_egrid,test_size=.3,random_state=seed,stratify=y_egrid)
X_train_def, X_test_def, y_train_def, y_test_def= train_test_split(X_def,y_def,test_size=.3,random_state=seed,stratify=y_def)

# ## find best Dtree parameters using f1 score

# In[4]:

tree_params={'criterion':['gini','entropy'],'max_depth':[5,10,40,70,100]}

# In[5]:

best_params_egrid,df_cv_egrid,clf_cv_egrid=clfGridSearch(DecisionTreeClassifier(random_state=seed),params=tree_params,
                                                         X=X_train_egrid,y=y_train_egrid,cv=3,scoring=scoring,
                                                         name='electrical',clf_type='Dtree',seed=seed)

best_params_def,df_cv_def,clf_cv_def=clfGridSearch(DecisionTreeClassifier(random_state=seed),params=tree_params,
                                                   X=X_train_def,y=y_train_def,cv=3,scoring=scoring,
                                                   name='default',clf_type='Dtree',seed=seed)

# In[6]:

print(best_params_egrid)

# In[7]:

print(best_params_def)

# ## Plot Grid Search Results

# In[9]:
plt.figure()

colors=['r','b']
for idx,(label, grp) in enumerate(df_cv_egrid.groupby('param_criterion')):
    plt.plot(grp['param_max_depth'],grp['mean_test_score'],c=colors[idx],linestyle='-')
    plt.plot(grp['param_max_depth'],grp['mean_train_score'],c=colors[idx],linestyle='--')
plt.xlabel('max tree depth')
plt.ylabel(scoring+ ' score')
plt.grid()
plt.title('electrical - Dtree grid search results (CV)')
plt.legend(['entropy - test '+scoring,'entropy - train '+scoring,'Gini - test '+scoring,'Gini - train '+scoring])
plt.savefig('parameter_tuning_electrical_dtree.png')

# In[28]:

df_cv_def[['param_criterion','param_max_depth','mean_test_score']]

# In[27]:

plt.figure()

colors=['r','b']
for idx,(label, grp) in enumerate(df_cv_def.groupby('param_criterion')):
    plt.plot(grp['param_max_depth'],grp['mean_test_score'],c=colors[idx],linestyle='-')
    plt.plot(grp['param_max_depth'],grp['mean_train_score'],c=colors[idx],linestyle='--')
plt.xlabel('max tree depth')
plt.ylabel(scoring +' score')
plt.grid()
plt.title('default - Dtree grid search results (CV)')
plt.legend(['entropy - test '+scoring,'entropy - train '+scoring,'Gini - test '+scoring,'Gini - train '+scoring])
plt.savefig('parameter_tuning_default_dtree.png')

# ## Generate Learning curves for best hyperparameters

# In[3]:

clf_egrid = DecisionTreeClassifier(criterion = 'entropy', max_depth=10)
clf_def = DecisionTreeClassifier(criterion = 'entropy', max_depth=40)

# In[12]:

train_sizes=np.linspace(.05,1,8)

plot_learning_curve(clf_egrid,
                    'learning curve Dtree Electrical(5foldCV)',
                    X_egrid_s,y_egrid,cv=5,train_sizes=train_sizes,scoring=scoring,seed=seed)

# In[13]:

plot_learning_curve(clf_def,
                    'learning curve Dtree Default(5foldCV)',
                    X_def_s,y_def,cv=5,train_sizes=train_sizes,seed=seed)

# ## fit best classifier with pruning

# In[4]:

clf_egrid.fit(X_train_egrid,y_train_egrid)
y_pred_egrid=clf_egrid.predict(X_test_egrid)

# In[5]:

prune_duplicate_leaves(clf_egrid)

# In[6]:

y_pred_pruned_egrid=clf_egrid.predict(X_test_egrid)

# In[7]:

print(classification_report(y_test_egrid,y_pred_egrid))
print(confusion_matrix(y_test_egrid,y_pred_egrid))

# In[8]:

plot_roc_curve(y_test_egrid,y_pred_egrid,'ROC Curve for Dtree Electrical')

# ## Default dataset

# In[9]:

clf_def.fit(X_train_def,y_train_def)
y_pred_def=clf_def.predict(X_test_def)
print(classification_report(y_test_def,y_pred_def))
print(confusion_matrix(y_test_def,y_pred_def))

# In[10]:

prune_duplicate_leaves(clf_def)

# In[11]:

y_pred_pruned_def=clf_def.predict(X_test_def)

# In[12]:

print(classification_report(y_test_def,y_pred_pruned_def))
print(confusion_matrix(y_test_def,y_pred_pruned_def))

# In[13]:

plot_roc_curve(y_test_def,y_pred_def,'ROC Curve for Dtree Default')

# In[ ]:


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 13 22:20:38 2021

@author: VGaur
"""

#You are given a string allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed.

#Return the number of consistent strings in the array words.

class Solution:
    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
        sum = 0
        for i in words:
            for l in set(i):
                if l not in allowed:
                    break
                else:
                    sum += 1
        return sum

Count the Numbers of Consistent Strings.py


from urllib.request import Request, urlopen
import json
import sys
from datetime import *

def json_request(url = '', encoding = 'utf-8', success = None,
                 error = lambda e : print('%s %s' % (e, datetime.now()), file=sys.stderr)):
    try:
        request = Request(url)
        resp = urlopen(request)
        json_body = resp.read().decode(encoding)

        json_result = json.loads(json_body)

        print('%s : success for request [%s]' % (datetime.now(), url))

        if callable(success) is False:
            return json_result

        success(json_result)
    except Exception as e:
        if callable(error) is True:
            error(e)


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

from flask.ext.restful import Resource, marshal_with, reqparse
from flask.ext.login import current_user

from common.fields import notification_field
from common.models import Notification
from utils import api_login_required

__author__ = "Philgyu,Seong"
__email__ = "philgyu.seong@gluvi.co"

KEY_CODE = "code"
KEY_MESSAGE = "message"

LOCATION_FORM = "form"

class NotificationApi(Resource):
    def __init__(self):
        self.post_parser = reqparse.RequestParser()
        self.post_parser.add_argument(KEY_MESSAGE, location=LOCATION_FORM)

    @api_login_required
    @marshal_with(notification_field, "notification")
    def get(self):

        notifications = Notification.query.filter(Notification.user_id == current_user.id).\
            filter(Notification.is_read == 0).all()

        return notifications


# n = 5
# seq_text = "5 1 4 2 3"

def longest_subsequence(arr, f):
    dp = [1] * len(arr)
    choice = [None] * len(arr)
    max_index = 0
    max_value = 1
    for i in range(1, len(arr)):
        curr_choice = None
        for j in range(i - 1, -1, -1):
            if f(arr[i], arr[j]) and 1 + dp[j] > dp[i]:
                dp[i] = 1 + dp[j]
                curr_choice = j
        choice[i] = curr_choice
        if dp[i] > max_value:
            max_value = dp[i]
            max_index = i

    answer = []
    while max_index >= 0:
        answer.append(arr[max_index])
        max_index = choice[max_index]
        if max_index is None:
            break
    return " ".join(map(str, answer[::-1]))

f = open("rosalind_lgis.txt", "r")
num_text = f.readline()
n = int(num_text)
seq_text = ''
for x in f:
    seq_text += x

f.close()

seq = [int(x) for x in seq_text.split(' ')]

# print(n)
# print(len(seq))

sol_increment = longest_subsequence(seq, lambda x, y: x > y)
sol_decrement = longest_subsequence(seq, lambda x, y: x < y)

f2 = open("./sol_longest_seq.txt", "w")
f2.write(sol_increment)
f2.write('\n')
f2.write(sol_decrement)
f2.close()


from tkinter import *
from PIL import ImageTk,Image
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import pyttsx3 as pp
import random
import urllib.request
import urllib.parse
import re
switch =0;count1=0;count2=0
engine = pp.init()
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[1].id)
engine.setProperty('rate', 120)

def reset_tabstop(event):
    event.widget.configure(tabs=(event.width-8, "right"))

def hellow(event):
   print('Hi')

def speak(word):
    engine.say(word)
    engine.runAndWait()

bot = ChatBot("Medguru")

def bolo():
    speak(instanswer)

convo1 = []
f = open("C:/Users/ABHISHEK JAISWAL/Documents/Hackathon/conver.txt")
for lines in f:
    lines.strip()
    convo1.append(lines[:-1])
    lines.strip()
#print(convo)
convo2 = []
f = open("C:/Users/ABHISHEK JAISWAL/Documents/Hackathon/conver2.txt")
for lines in f:
    lines.strip()
    convo2.append(lines[:-1])
    lines.strip()
#print(convo)
trainer = ListTrainer(bot)

trainer.train(convo1)
trainer.train(convo2)

def findcategory(disease_n2):
    listofdis2 = disease_n2.split(" ")
    disease_name2 = "+".join(listofdis2)
    print(disease_name2)
    url2 = "https://www.nhsinform.scot/search?q="+disease_name2+"&locpt=&ds=&tab=inform"
    print(url2)

    values2  = {'s':'basics','submit':'search'}
    data2 = urllib.parse.urlencode(values2)
    data2 = data2.encode('utf-8')
    req2 = urllib.request.Request(url2,data2)
    resp2 = urllib.request.urlopen(req2)
    respData2 = resp2.read()

    paragraphs2 = re.findall(r'class="search__title">(.*?)',str(respData2))
    #print(list(paragraphs2))
    paragraphs2.reverse()
    statements2 = {}
    for eachP in list(set(paragraphs2)):
        #print(eachP)
        if "NHS" in eachP:
            continue
        p1=[0];p2=[0]
        p1 = re.findall(r'',str(eachP))
        #print(p1,p2)
        statements2[p2[-1]] = p1[-1]

    return statements2

def findcategory2(dis3):
    url3 = "https://www.nhsinform.scot"+dis3
    print(url3)

    values3  = {'s':'basics','submit':'search'}
    data3 = urllib.parse.urlencode(values3)
    data3 = data3.encode('utf-8')
    req3 = urllib.request.Request(url3,data3)
    resp3 = urllib.request.urlopen(req3)
    respData3 = resp3.read()

    paragraphs3 = list(re.findall(r'")
        if aaa[-1] == "" or aaa[-1] == " ":
            continue

        statements3.append(aaa[-1])

    return statements3

def findcategory3(dis3):
    url3 = "https://www.nhsinform.scot"+dis3
    print(url3)

    values3  = {'s':'basics','submit':'search'}
    data3 = urllib.parse.urlencode(values3)
    data3 = data3.encode('utf-8')
    req3 = urllib.request.Request(url3,data3)
    resp3 = urllib.request.urlopen(req3)
    respData3 = resp3.read()

    paragraphs3 = list(re.findall(r'<h2(.*?)
',str(respData3)))
    paragraphs4 = list(re.findall(r'<h2(.*?)<h2',str(respData3)))
    x = str(respData3).find(paragraphs4[0])
    paragraphs4 += list(re.findall(r'")
            eachP = eachP[y+1:]
        #print(eachP)
        x = []
        p5 = list(re.findall(r'
(.*?)
',str(eachP)))
        for j in p5:
            if "href" in j:
                continue
            else:
                x.append(j)
        statements4.append((eachP[:50],x))
    #print(statements4)
    for i in statements3:
        n = len(i)
        for j,k in statements4:
            if i in j:
                statements5.append((i,k))
                break
    return statements5

def findans(disease_n):
    disease_n = disease_n.lower()
    if disease_n == "hairfall" or disease_n == "hair fall":
        disease_n = "baldness"
    listofdis = disease_n.split(" ")
    disease_name = "%20".join(listofdis)
    print(disease_name)
    url = "https://www.merriam-webster.com/dictionary/" + disease_name
    #print(url)

    #fp = open('C:/Users/ABHISHEK JAISWAL/Documents/Hackathon/diseases.txt','w+')

    #fp.write(soup.select('div p')[0].get_text() + '\n')
    #fp.close()

    values  = {'s':'basics','submit':'search'}
    data = urllib.parse.urlencode(values)
    data = data.encode('utf-8')
    req = urllib.request.Request(url,data)
    resp = urllib.request.urlopen(req)
    respData = resp.read()

    #print(respData)

    paragraphs = re.findall(r': (.*?)',str(respData))
    #print(list(paragraphs))
    statement = []
    for eachP in list(set(paragraphs)):
        if "<" not in eachP:
            statement.append(eachP)

    if len(statement) == 0:
        loc = paragraphs[0].find("<")
        statement.append(paragraphs[0][:loc])
    return statement

root = Tk()
root.geometry("1000x750")
root.configure(background='#0a0a0a')
root.iconbitmap('C:/Users/ABHISHEK JAISWAL/Documents/Hackathon/abcd.ico')
root.title('MedGuru')
my_img =ImageTk.PhotoImage(Image.open("C:/Users/ABHISHEK JAISWAL/Documents/Hackathon/MG4.jpg"))
my_label = Label(image = my_img)
my_label.place(x=430,y=0)
text2  = Label(root,text = "Note: If you want information about any disease then type \"getinfo\"",fg = "#b8b8b8")
text2.place(x=320,y=158)

satisfy = ['ok','okk','okkk','all right','stop','chup','keep quiet','shut up','understand']
exitgreet = ['bye','byee','by by','bye bye','goodbye','good bye','byeee','byeeee','ok bye','okk bye']
unwanted = ['friend','marry','love','kiss','miss','kill','murder','fuck','idiot','sex','sexy','sing','play','dance','joke','climate','weather','favourite']
greet = ['hi','hii','hiii','hiiii','hello','helloo','hola','hiiiii','hlo','hlw','hey','heyy','whats up','helo',':)','yo','namaste']
greetresponse = ['namaste','hey there!','hi','hii','hiii','hiiii','hello','helloo','namaste','namaste','hola','hiiiii','hlo','hlw','hey','heyy',"hello, how may i help you?",]
satisfyresponse = ['you can ask any medical related question','you can type getinfo to know more','i can help you','i am not bad as you think so']

def ask_from_bot_():
    if switch == 0:
        ask_from_bot()
    else:
        findsolution()

def ask_from_bot():
    query = textF.get()
    tquery = query
    query = query.lower()
    global instanswer
    flag= 0
    if flag==0:
        if query == "getinfo":
            flag = 1
            info()
    if flag==0:
        if query in greet:
            flag = 1
            answer = random.choice(greetresponse)
            txt.configure(state = NORMAL)
            txt.insert(END," \t"+str(tquery)+"   \n "+ str(answer)+"\t  \n")
            #txt.insert(END,"MedGuru: " + str(answer)+"\n")
            txt.tag_add('demo', '0.0', '5.7')
            txt.tag_config('demo', background='#2ce6e2',foreground = "#000000")
            txt.tag_bind('demo', '<Double-1>', hellow)
            txt.yview(END)
            txt.configure(state = DISABLED)
            textF.delete(0,END)
            instanswer = answer
    if flag==0:
        if query in unwanted:
            flag =1
            answer = "please ask only medical related questions"
            txt.configure(state = NORMAL)
            txt.insert(END," \t"+str(tquery)+"   \n "+ str(answer)+"\t  \n")
            #txt.insert(END,"MedGuru: " + str(answer)+"\n")
            txt.tag_add('demo', '0.0', '5.7')
            txt.tag_config('demo', background='#2ce6e2',foreground = "#000000")
            txt.tag_bind('demo', '<Double-1>', hellow)
            txt.yview(END)
            txt.configure(state = DISABLED)
            textF.delete(0,END)
            instanswer = answer
    if flag==0:
        if query in satisfy:
            flag =1
            answer = random.choice(satisfyresponse)
            txt.configure(state = NORMAL)
            txt.insert(END," \t"+str(tquery)+"   \n "+ str(answer)+"\t  \n")
            #txt.insert(END,"MedGuru: " + str(answer)+"\n")
            txt.tag_add('demo', '0.0', '5.7')
            txt.tag_config('demo', background='#2ce6e2',foreground = "#000000")
            txt.tag_bind('demo', '<Double-1>', hellow)
            txt.yview(END)
            txt.configure(state = DISABLED)
            textF.delete(0,END)
            instanswer = answer
    if flag==0:
        answer = bot.get_response(query)
        instanswer = answer
        txt.configure(state = NORMAL)
        txt.insert(END," \t"+str(tquery)+"   \n "+ str(answer)+"\t  \n")
        #txt.insert(END,"MedGuru: " + str(answer)+"\n")
        txt.tag_add('demo', '0.0', '5.7')
        txt.tag_config('demo', background='#2ce6e2',foreground = "#000000")
        txt.tag_bind('demo', '<Double-1>', hellow)
        txt.yview(END)
        txt.configure(state = DISABLED)
        textF.delete(0,END)
        if query in exitgreet:
            speak("Good Bye")
            speak("See you soon")
            root.quit()
ignorelist = ["i","am","suffering","suffer","from","help","me","are","problem","tell","about","this","should","would","will","do",
                "disease","condition","conditions","matter","a","is","what","could","please","feeling","may","feel","we","can","my","get","gets","have","how","to",
                "away","from","rid","of","prevent","against","precautions","character","characteristics","feature","features","qualities","us","protect",
                "secure","precautions","precaution","symptom","affect","keep","avoid","treat","remove","reduce","cure","symptoms","away","how",
                "we","masure","major","measures","relief","relax","be","remedies","heal","take","stay","in","on","restore","recover","fall","mention",
                "expcect","reason","conditions","condition","treatment","benefit","benefits","gain","above","upon","below","down","up","the","well",
                "behind","diseases","quality","affected","secured","measured","number","not","for"," ","  ","which"]

def solveproblem():
    global switch
    if switch == 0:
        answer = "Tell me your problem!"
        instanswer = answer
        txt.configure(state = NORMAL)
        txt.insert(END,"\n "+str(answer)+"\t  \n")
        txt.tag_add('demo', '0.0', '5.7')
        txt.tag_config('demo', background='#2ce6e2',foreground = "#000000")
        txt.tag_bind('demo', '<Double-1>', hellow)
        txt.yview(END)
        txt.configure(state = DISABLED)
        textF.delete(0,END)
    if switch ==0:
        labelText.set("ON")
        label4.config(fg = "green")
        switch = 1
    else:
        labelText.set("OFF")
        label4.config(fg = "red")
        switch = 0

def findsolution():
    global count1
    global count2
    global category1keys
    global firstchoice
    global category1
    global category2
    answer = textF.get()
    instanswer = answer
    txt.configure(state = NORMAL)
    txt.insert(END,"\n "+str(answer)+"\t  \n")
    txt.tag_add('demo', '0.0', '5.7')
    txt.tag_config('demo', background='#2ce6e2',foreground = "#000000")
    txt.tag_bind('demo', '<Double-1>', hellow)
    txt.yview(END)
    txt.configure(state = DISABLED)
    count1+=1
    if count1<= 2:
        answer = "Type the correct number according to your match"
        instanswer = answer
        txt.configure(state = NORMAL)
        txt.insert(END,"\n "+str(answer)+"\t  \n")
        txt.tag_add('demo', '0.0', '5.7')
        txt.tag_config('demo', background='#2ce6e2',foreground = "#000000")
        txt.tag_bind('demo', '<Double-1>', hellow)
        txt.yview(END)
        txt.configure(state = DISABLED)
    if count1==1:
        query = textF.get()
        if query[-1] == "?":
            query = query[:-1]
        query= query.lower()
        print(query)
        textF.delete(0,END)
        txt.yview(END)
        ourlist = query.split(" ")
        print(ourlist)
        ourlist2=[]
        for l in ourlist:
            if l not in ignorelist:
                ourlist2.append(l)
        newquery = " ".join(ourlist2)
        category1 = findcategory(newquery)
        print(category1)
        category1keys = list(category1.keys())
        for l in range(len(category1keys)):
            answer = category1keys[l]
            instanswer = answer
            txt.configure(state = NORMAL)
            txt.insert(END,"\n "+str(l+1)+" : "+str(answer)+"\t  \n")
            txt.tag_add('demo', '0.0', '5.7')
            txt.tag_config('demo', background='#2ce6e2',foreground = "#000000")
            txt.tag_bind('demo', '<Double-1>', hellow)
            txt.yview(END)
            txt.configure(state = DISABLED)
    if count1 == 2:
        query = textF.get()
        firstchoice = category1keys[int(query)-1]
        category2 = findcategory2(category1[firstchoice])
        print(category2)
        n=1
        textF.delete(0,END)
        for l in range(len(category2)):
            answer = category2[l]
            if answer == "" or answer == " ":
                continue
            instanswer = answer
            txt.configure(state = NORMAL)
            txt.insert(END,"\n "+str(n)+" : "+str(answer)+"\t  \n")
            txt.tag_add('demo', '0.0', '5.7')
            txt.tag_config('demo', background='#2ce6e2',foreground = "#000000")
            txt.tag_bind('demo', '<Double-1>', hellow)
            txt.yview(END)
            n+=1
            txt.configure(state = DISABLED)

    if count1 == 3:
        count1-=1
        query = textF.get()
        secondchoice = category2[int(query)-1]
        #print(secondchoice)
        category3 = findcategory3(category1[firstchoice])
        textF.delete(0,END)
        for i, j in category3:
            if i == secondchoice:
                for k in j:
                    answer = k
                    instanswer = answer
                    txt.configure(state = NORMAL)
                    txt.insert(END,"\n  " +str(answer)+"\n")
                    txt.tag_add('demo', '0.0', '5.7')
                    txt.tag_config('demo', background='#2ce6e2',foreground = "#000000")
                    txt.tag_bind('demo', '<Double-1>', hellow)
                    txt.yview(END)
                    txt.configure(state = DISABLED)
                break

def info():
    engine = pp.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice',voices[0].id)
    engine.setProperty('rate', 120)
    textF.delete(0,END)

    def speak2(word):
        engine.say(word)
        engine.runAndWait()

    top = Toplevel()
    top.title("Disease Info")
    top.geometry("1000x750")
    b = Label(top,text = "Search for your disease",font = ("Impact",25),bd = 4)
    #b.grid(row = 0,column = 1,padx = 10,pady = 10)
    b.pack()
    a = Entry(top,font = ("Verdana",20),width = 40)
    #a.grid(row=1,column =1,columnspan = 5 )
    a.pack()

    def buttonpress():
        temp = a.get()
        global instanswer2
        temp = temp.lower()
        disanswer = findans(temp)
        txt2.configure(state = NORMAL);temp2 = ""
        txt2.delete('1.0',END)
        if len(disanswer) != 0:
            txt2.insert(END," " + str(temp.upper())+ " :" + "\n\n")
            for k in disanswer:
                temp2+= ", ," + k
                txt2.insert(END," * " + str(k) + "\n\n")
        txt2.configure(state = DISABLED)
        instanswer2 = temp2
        a.delete(0,END)
        txt2.yview(END)

    def call():
        print(instanswer2)
        speak2(instanswer2)

    e = Button(top,text = "Listen",font = ("Verdana",15),fg = "black",bg = "#b8b8b8",command = call)
    e.place(x =900,y = 137 )
    c = Button(top,text = "Search",font = ("Verdana",15),fg = "blue",bg = "#b8b8b8",command = buttonpress)
    #c.grid(row = 1,column = 4,padx = 10)
    c.pack()
    txt2 = Text(top)
    txt2.place(x=2,y=179,width=994,height = 530)
    #sc2 = Scrollbar(txt2)
    txt2.configure(state = DISABLED,yscrollcommand=sc.set,font = ("Helvetica Neue",15))
    #sc2.place(x=980,y=0,height = 500)
    def enter_function2(event):
        c.invoke()
    top.bind('<Return>',enter_function2)

# 12-22 is for frame where msgs are to be displayed
txt = Text(root)
txt.place(x=0,y=179,width=1000,height = 500)
sc = Scrollbar(txt)
txt.configure(state = DISABLED,yscrollcommand=sc.set,font = ("Helvetica Neue",15))
sc.place(x=980,y=0,height = 500)

#Input field
textF = Entry(root,font = ("Verdana",20),width = 58)
textF.place(x=0,y=679)

#for button
labelText = StringVar()
labelText.set('OFF')
label4 = Label(root,textvariable = labelText,bg = "#b8b8b8",fg = "red")
label4.place(x = 230,y=140,height = 30)
btn = Button(root,text = "Ask",font = ("Verdana",20),command = ask_from_bot_)
btn.place(x=928,y=679,height=37)
btn2 = Button(root,text = "listen",font = ("Verdana",20),command = bolo)
btn2.place(x=832,y=679,height=37)
btn3 = Button(root,text = "Solve My Problem",font = ("Verdana",13),command = solveproblem)
btn3.place(x=50,y=140,height = 30)

#footer = Label(root,text = "team khatarnak")
#footer.pack(side = LEFT)

def enter_function(event):
    btn.invoke()

root.bind('<Return>',enter_function)

#bot.storage.drop()
txt.bind("<Configure>", reset_tabstop)

root.mainloop()


def convert(number):
    result = ""
    if number%3 == 0:
        result += "Pling"

    if number%5 == 0:
        result += "Plang"

    if number%7 == 0:
        result += "Plong"

    if len(result) == 0:
        return format(f"{number}")

    return result


#!/usr/bin/python3
"""Square class"""

class Square:
    """ It is the class for define a square: My first class"""
    __size = ''
    __position = ''

    def __init__(self, size=0, position=(0, 0)):
        self.__size = size
        self.__position = position

    @property
    def size(self):
        return self.__size

    @size.setter
    def size(self, value):
        try:
            ("{:d}".format(value))
            if value < 0:
                raise ValueError("size must be >= 0")
            self.__size = value
        except:
            raise TypeError("size must be an integer")

    @property
    def position(self):
        """Position of the square"""
        return self.__position

    @position.setter
    def position(self, position):
        """Set the value"""
        if (type(value) is not tuple) or (len(value) != 2) or\
            type(value[0]) is not int or\
            not isinstance(value[1], int) or\
                (value[0] < 0 or value[1] < 0):
            raise TypeError("position must be a tuple of 2 positive integers")
        else:
            self.__position = position

    def area(self):
        size = self.__size
        c = size * size
        return c

    def my_print(self):
        position = self.__position
        size = self.__size
        if size == 0:
            print()
        else:
            for i in range(position[1]):
                print()
            for i in range(size):
                for i in range(size + position[0]):
                    if i < position[0]:
                        print(' ', end="")
                    else:
                        print("#", end="")
                print()


#!/usr/bin/env python3 -u
# -*- coding: utf-8 -*-
# copyright: sktime developers, BSD-3-Clause License (see LICENSE file)
"""Implement transformers for summarizing a time series."""

__author__ = ["mloning", "RNKuhns"]
__all__ = ["SummaryTransformer", "MeanTransformer"]

import pandas as pd

from sktime.transformations.base import _SeriesToPrimitivesTransformer
from sktime.utils._maint import deprecated
from sktime.utils.validation.series import check_series

ALLOWED_SUM_FUNCS = [
    "mean",
    "min",
    "max",
    "median",
    "sum",
    "skew",
    "kurt",
    "var",
    "std",
    "mad",
    "sem",
    "nunique",
    "count",
]

def _check_summary_function(summary_function):
    """Validate summary_function.

    Parameters
    ----------
    summary_function : str, list or tuple
        Either a string or list/tuple of strings indicating the pandas summary
        functions ("mean", "min", "max", "median", "sum", "skew", "kurtosis",
        "var", "std", "mad", "sem", "nunique", "count") that is used to summarize
        each column of the dataset.

    Returns
    -------
    summary_function : list or tuple
        The summary functions that will be used to summarize the dataset.
    """
    msg = f"""`summary_function` must be str or a list or tuple made up of
          {ALLOWED_SUM_FUNCS}.
          """
    if isinstance(summary_function, str):
        if summary_function not in ALLOWED_SUM_FUNCS:
            raise ValueError(msg)
        summary_function = [summary_function]
    elif isinstance(summary_function, (list, tuple)):
        if not all([func in ALLOWED_SUM_FUNCS for func in summary_function]):
            raise ValueError(msg)
    else:
        raise ValueError(msg)
    return summary_function

def _check_quantiles(quantiles):
    """Validate quantiles.

    Parameters
    ----------
    quantiles : str, list, tuple or None
        Either a string or list/tuple of strings indicating the pandas summary
        functions ("mean", "min", "max", "median", "sum", "skew", "kurtosis",
        "var", "std", "mad", "sem", "nunique", "count") that is used to summarize
        each column of the dataset.

    Returns
    -------
    quantiles : list or tuple
        The validated quantiles that will be used to summarize the dataset.
    """
    msg = """`quantiles` must be int, float or a list or tuple made up of
          int and float values that are between 0 and 1.
          """
    if isinstance(quantiles, (int, float)):
        if not 0.0 <= quantiles <= 1.0:
            raise ValueError(msg)
        quantiles = [quantiles]
    elif isinstance(quantiles, (list, tuple)):
        if len(quantiles) == 0 or not all(
            [isinstance(q, (int, float)) and 0.0 <= q <= 1.0 for q in quantiles]
        ):
            raise ValueError(msg)
    elif quantiles is not None:
        raise ValueError(msg)
    return quantiles

class SummaryTransformer(_SeriesToPrimitivesTransformer):
    """Calculate summary value of a time series.

    For :term:`univariate time series` a combination of summary functions and
    quantiles of the input series are calculated. If the input is a
    :term:`multivariate time series` then the summary functions and quantiles
    are calculated separately for each column.

    Parameters
    ----------
    summary_function : str, list, tuple, default=("mean", "std", "min", "max")
        Either a string, or list or tuple of strings indicating the pandas
        summary functions that are used to summarize each column of the dataset.
        Must be one of ("mean", "min", "max", "median", "sum", "skew", "kurt",
        "var", "std", "mad", "sem", "nunique", "count").
    quantiles : str, list, tuple or None, default=(0.1, 0.25, 0.5, 0.75, 0.9)
        Optional list of series quantiles to calculate. If None, no quantiles
        are calculated.

    See Also
    --------
    MeanTransformer :
        Calculate the mean of a timeseries.

    Notes
    -----
    This provides a wrapper around pandas DataFrame and Series agg and
    quantile methods.

    Examples
    --------
    >>> from sktime.transformations.series.summarize import SummaryTransformer
    >>> from sktime.datasets import load_airline
    >>> y = load_airline()
    >>> transformer = SummaryTransformer()
    >>> y_mean = transformer.fit_transform(y)
    """

    _tags = {
        "fit-in-transform": True,
    }

    def __init__(
        self,
        summary_function=("mean", "std", "min", "max"),
        quantiles=(0.1, 0.25, 0.5, 0.75, 0.9),
    ):
        self.summary_function = summary_function
        self.quantiles = quantiles
        super(SummaryTransformer, self).__init__()

    def _transform(self, Z, X=None):
        """Logic to transform series.

        Parameters
        ----------
        Z : pd.Series or pd.DataFrame
            The series to transform.

        Returns
        -------
        summary_value : scalar or pd.Series
            If `series_or_df` is univariate then a scalar is returned. Otherwise,
            a pd.Series is returned.
        """
        if self.summary_function is None and self.quantiles is None:
            raise ValueError(
                "One of `summary_function` and `quantiles` must not be None."
            )
        summary_function = _check_summary_function(self.summary_function)
        quantiles = _check_quantiles(self.quantiles)

        summary_value = Z.agg(summary_function)
        if quantiles is not None:
            quantile_value = Z.quantile(quantiles)
            summary_value = pd.concat([summary_value, quantile_value])

        if isinstance(Z, pd.Series):
            summary_value.name = Z.name
            summary_value = pd.DataFrame(summary_value)

        return summary_value.T

    def transform(self, Z, X=None):
        """Transform series.

        Parameters
        ----------
        Z : pd.Series or pd.DataFrame
            The series to transform.

        Returns
        -------
        summary_value : pd.DataFrame
            DataFrame where series are instances (rows) and calculated summary
            values are treated as features (columns).
        """
        self.check_is_fitted()
        Z = check_series(Z)
        summary_value = self._transform(Z, X=X)
        return summary_value

class MeanTransformer(SummaryTransformer):
    """Calculate mean value of a time series.

    See Also
    --------
    SummaryTransformer :
        Calculate summary values of a time series.

    Examples
    --------
    >>> from sktime.transformations.series.summarize import MeanTransformer
    >>> from sktime.datasets import load_airline
    >>> y = load_airline()
    >>> transformer = MeanTransformer()
    >>> y_mean = transformer.fit_transform(y)
    """

    @deprecated(
        """Please use `SummaryTransformer` from
        `sktime.transformation.series.summarize` instead.
        MeanTransformer will be removed in release 0.10.
        """
    )
    def __init__(self):
        super().__init__(summary_function="mean", quantiles=None)


def intersection(arrays):

    """
    YOUR CODE HERE
    """

    num_dict = {}
    result = []
    # sort arrays
    arrays = sorted(arrays, key=lambda x: len(x))
    # iterate through lists
    # compare first value to all other values
    # if all list contain this value, append to result
    for element in arrays[0]:
        num_dict[element] = 1
    for array in arrays[1:]:
        for element in array:
            if element in num_dict:
                num_dict[element] += 1
    result = [num for num in num_dict if num_dict[num] == len(arrays)]

    return result

if __name__ == "__main__":
    arrays = []

    arrays.append(list(range(1000000,2000000)) + [1,2,3])
    arrays.append(list(range(2000000,3000000)) + [1,2,3])
    arrays.append(list(range(3000000,4000000)) + [1,2,3])

    print(intersection(arrays))


from django.test import TestCase
from rest_framework import serializers

from robber import expect

from shared.serializer import NoNullSerializer
from shared.tests.utils import create_object

class NoNullSerializerTestCase(TestCase):
    def test_serialization_remove_None_attr(self):

        class ObjectSerializer(NoNullSerializer):
            id = serializers.IntegerField()
            name = serializers.CharField()
        objects = [
            create_object({'id': 1, 'name': 'Alex', 'value': 3}),
            create_object({'id': 2, 'name': None, 'value': 4})
        ]

        data = ObjectSerializer(objects, many=True).data
        expect(data).to.eq([
            {'id': 1, 'name': 'Alex'},
            {'id': 2}
        ])


# -*- coding: utf-8 -*-
# Copyright 2017 GIG Technology NV
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# @@license_version:1.3@@

from google.appengine.ext import ndb

from rogerthat.bizz.payment.providers.payconiq.consts import PAYMENT_PROVIDER_ID
from rogerthat.bizz.payment.utils import get_provider_namespace

class PayconiqTransaction(ndb.Model):
    STATUS_PENDING = u'pending'
    STATUS_SUCCEEDED = u'succeeded'
    STATUS_FAILED = u'failed'
    STATUS_CANCELED = u'canceled'
    STATUS_TIMEDOUT = u'timedout'

    timestamp = ndb.IntegerProperty()
    test_mode = ndb.BooleanProperty()
    target = ndb.StringProperty()
    currency = ndb.StringProperty()
    amount = ndb.IntegerProperty()
    precision = ndb.IntegerProperty(default=2)
    memo = ndb.StringProperty()
    app_user = ndb.UserProperty()
    status = ndb.StringProperty()

    payconic_transaction_id = ndb.StringProperty()

    @property
    def transaction_url(self):
        if not self.payconic_transaction_id:
            return None
        if self.test_mode:
            return 'https://dev.payconiq.com/payconiq/v2/transactions/%s' % self.payconic_transaction_id
        return 'https://api.payconiq.com/payconiq/v2/transactions/%s' % self.payconic_transaction_id

    @classmethod
    def query(cls, *args, **kwargs):
        kwargs['namespace'] = get_provider_namespace(PAYMENT_PROVIDER_ID)
        return super(PayconiqTransaction, cls).query(*args, **kwargs)

    @classmethod
    def create_key(cls, transaction_id):
        return ndb.Key(cls, u"%s" % transaction_id, namespace=get_provider_namespace(PAYMENT_PROVIDER_ID))

    @classmethod
    def list(cls, start_time, end_time, status=STATUS_SUCCEEDED, test_mode=False):
        qry = cls.query()
        qry = qry.filter(PayconiqTransaction.test_mode == test_mode)
        qry = qry.filter(PayconiqTransaction.status == status)
        qry = qry.filter(PayconiqTransaction.timestamp > start_time)
        qry = qry.filter(PayconiqTransaction.timestamp < end_time)
        return qry


# Chapter 1.12 Defining Functions
# Self Check
import random

def stringGenerator(strLength):
    alphabet = 'abcdefghijklmnopqrstuvwxyz '
    resultStr = ''
    for i in range(strLength):
        resultStr += alphabet[random.randrange(len(alphabet))]
    return resultStr

def score(goal, testStr):
    numSame = 0
    for i in range(len(goal)):
        if goal[i] == testStr[i]:
            numSame += 1
    return numSame / len(goal)

def main():
    goalString = 'methinks it is like a weasel'
    newString = stringGenerator(len(goalString))
    best = 0
    newScore = score(goalString, newString)
    while newScore < 1:
        if newScore > best:
            print(newScore, newString)
            best = newScore
        newString = stringGenerator(len(goalString))
        newScore = score(goalString, newString)

main()


"""
숨바꼭질
https://www.acmicpc.net/problem/1697
"""
from collections import deque
import sys

n, k = map(int, sys.stdin.readline().split())
MAX =100000

def bfs():
    q = deque([n])
    check = [0] * MAX
    check[n] = 1
    cnt = 0

    while q:
        now = q.popleft()
        cnt += 1

        if now == k:
            print(check[k] - 1)
            break

        for i in [now + 1, now - 1, now * 2]:
            if 0 <= i <= MAX and not check[i]:
                q.append(i)
                check[i] = check[now] + 1

bfs()


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

# 什么是爬虫
# 模仿人类利用浏览器浏览网页的行为,并且采集相关的数据
# 第三方库安装 请求库
# pip install requetes
# 以面向过程的方式开发的,只是用到了函数.
# 需要向服务器发送请求

# 1. 导入第三方请求库
import requests
# 2. 导入url编码方法
from urllib.parse import urlencode
# # 5. 导入json库,用于解析网页的字典格式,变成python的字典格式
# import json

# 3 定义一个用于编码url,接收2个参数,都是形参(第一个参数,关键字,第二个参数,翻页)
def data_url(keyword, pn):
    # 3.1 打包我们想要编码的数据
    data = {'tn': 'resultjson_com',
            'ipn': 'rj',
            'ct': 201326592,
            'is': '',
            'fp': 'result',
            'queryWord': keyword,
            'cl': 2,
            'lm': -1,
            'ie': 'utf-8',
            'oe': 'utf-8',
            'adpicid': '',
            'st': '',
            'z': '',
            'ic': '',
            'word': keyword,
            's': '',
            'se': '',
            'tab': '',
            'width': '',
            'height': '',
            'face': '',
            'istype': '',
            'qc': '',
            'nc': '',
            'fr': '',
            'pn': pn,
            'rn': 30,
            'gsm': 'b4'}
    # 3.2 返回打包好的数据
    return data

# 4. 用数据拼接我们要访问的url
def data_url_get(url_data):
    # 4.1 用urlcode方法,给我们的数据编码,完成拼接
    url = 'https://image.baidu.com/search/index?' + urlencode(url_data)
    # 4.3 发送请求,并且得到服务器的响应,用一个变量保存
    response = requests.get(url)
    # 4.4 判断一下,我们是否请求成功
    if response.status_code == 200:
        return response.json()
    # 4.5 异常的判断
    else:
        print('返回的状态码是 %s' % response.status_code)

# 5. 遍历提取字典中的所有的url
def pares_data(dict_json):
    # 5.4 用一个集合去保存我们取出来的url
    all_url = set()
    # 5.1判断键名  叫data是否存在
    if 'data' in dict_json.keys():
        # 5.2 从字典当中取出叫data的键的值
        for item in dict_json.get('data'):
            # 5.3 异常处理,为了防止,取到空的字典.导致异常
            try:
                # 5.5 把取出来的url添加到集合中
                all_url.add(item['thumbURL'])
            # 5.3.1 一旦出现异常,我们就执行下面的代码,为了保证程序能够正常运行
            except:
                print('这个字典里的值已经取光了.')
    # 5.6 返回所去重好的url集合
        return all_url

# 6. 依次请求所有的图片地址.直接储存
def down_image(image_urls):
    # 6.1 遍历集合,依次取出url
    for url in image_urls:
        # print(url)
        # 6.6 给文件取一个名字
        name = url.split(',')[-1].split('&')[0]
        # 6.2 发送请求,并且得到服务器返回的数据
        response = requests.get(url)
        # 6.3 判断请求是否成功.
        if response.status_code == 200:
            # 6.4 文本处理,用w的方式创建文件,b代表二进制(以二进制的方法写入),
            with open('./images/%s.jpg' % name, 'wb') as f:
                # 6.5 写入文件
                f.write(response.content)

        # break

# main函数 ,主函数(程序入口)
def main():
    # 接收用户输入指定参数
    keyword = input("请输入,你想下载的图片类型关键字: ")
    # 暂时固定pn变量
    page = input('请输入你们想下载的页数')
    for pn in range(int(page)):
        # 页数翻页是30为阶段的!所以0*30
        pn = 0 * 30
        # 调用打包数据的函数,返回一个完整的数据
        url_data = data_url(keyword, pn)
        # 调用拼接数据为url的函数,返回时json格式的字典
        dict_json = data_url_get(url_data)
        # 取出我们json数据中包含的所有的图片的地址,接收的是一个集合
        image_urls = pares_data(dict_json)
        # 调用第六步.下载到本地
        down_image(image_urls)

# 如果我们的代码从文件本身执行,就执行main函数
# 这样写代码的方式,通常用于调试
if __name__ == '__main__':
    # 执行main函数
    main()


# importando as bibliotecas
import pandas as pd
import pandas.util.testing as tm
from time import sleep
from selenium import webdriver
import plotly.express as px
import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt
#sns.set()
import random
import plotly.express as px
import pandas.util.testing as tm
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State

df_limpo = pd.read_csv('C:/Users/Jessica/Downloads/projetos_Dash/df_ver1.csv')
print(df_limpo.head())
print(df_limpo.columns)
print(df_limpo.info())

app = dash.Dash(__name__)
# ------------------------------------layout-------------------------------------------------------------------------------
app.layout = html.Div([

    dcc.Dropdown(id='my_dropdown',
                 options=[{'label': x, 'value': x} for x in sorted(df_limpo.Setor.unique())],
                 value='Financeiro',

                 ),

    html.Div([
            dcc.Graph(id='pie-graph', figure={}, className='five columns'),

            dcc.Graph(id="the_graph", figure={}, clickData=None, hoverData=None,
                  # I assigned None for tutorial purposes. By defualt, these are None, unless you specify otherwise.
                  config={
                      'staticPlot': False,  # True, False
                      'scrollZoom': True,  # True, False
                      'doubleClick': 'reset',  # 'reset', 'autosize' or 'reset+autosize', False
                      'showTips': False,  # True, False
                      'displayModeBar': True,  # True, False, 'hover'
                      'watermark': True,
                      # 'modeBarButtonsToRemove': ['pan2d','select2d'],
                  },
                  className='five columns'
                  )

    ]),

])

# ------------------------------------------------------------callback-------------------------------------------------
@app.callback(Output(component_id="the_graph", component_property="figure"),
              [Input(component_id="my_dropdown", component_property="value")])
def update_graph(setor):
    print(setor)
    dff = df_limpo[df_limpo.Setor == setor]
    fig = px.line(dff, x='data', y='value', color='Cod_acao',custom_data=['Cod_acao', 'Setor', 'Volume'])
    fig.update_traces(mode='lines+markers')
    return fig
# ------------------------------------------------------------callback------------------------------------------------

@app.callback(
    Output(component_id='pie-graph', component_property='figure'),
    Input(component_id="the_graph", component_property='hoverData'),
    Input(component_id="the_graph", component_property='clickData'),
    Input(component_id="the_graph", component_property='selectedData'),
    Input(component_id="my_dropdown", component_property='value'))

def update_side_graph(hov_data, clk_data, slct_data,setor):
    if hov_data is None:
        dff2 = df_limpo[df_limpo.Setor == setor]
        dff2 = dff2[dff2.data == '2020-04-20']
        print(dff2)
        fig2 = px.pie(data_frame=dff2, values='Volume', names='Cod_acao',
                      title='Volume em 20-04-2020')
        return fig2
    else:
        print(f'hover data: {hov_data}')
        # print(hov_data['points'][0]['customdata'][0])
        # print(f'click data: {clk_data}')
        # print(f'selected data: {slct_data}')
        dff2 = df_limpo[df_limpo.Setor == setor]
        print(hov_data['points'][0]['x'])
        print(dff2)
        hov_year = hov_data['points'][0]['x']
        dff2 = dff2[dff2.data == hov_year]
        fig2 = px.pie(data_frame=dff2, values='Volume', names='Cod_acao', title=f'Volume em: {hov_year}')
        return fig2

if __name__ == '__main__':
    app.run_server(
        port=8050,

    )


#encoding:utf-8
'''
RSU的Python驱动
V0.1.0 Alpha

<编码未完成>

Python封装提供两种方式：
1、使用    RSU.RSU类，已封装为同步方法，成对儿调用rq/rs
2、直接使用Rsu.ReaderDriver里面的透传方法。

入参中的命令串，使用hex符号字符串即可；所有国标出参都以返回值的方式返回。
'''

from ReaderDriver import *

class DeviceException(Exception):
    '''设备异常抛出'''
    def __init__(self,msg):
        self.msg = msg

    def __str__(self):
        return self.msg

class RSU:
    def __init__(self):
        self.fd = None;
        self.TimeOut = 1000

    def setTimeout(self,timeout):
        self.TimeOut = timeout

    def RSU_Open(self,mode,dev,port):
        fd = RSU_Open(mode,dev,port)
        if fd < 0 :
            raise DeviceException(u"设备打开失败")
        self.fd = fd

    def RSU_Close(self):
        if self.fd is None:
            raise DeviceException(u"设备未打开")
        RSU_Close(self.fd)

    def RSU_INIT(self,Time, BSTInterval, RetryInterval,TxPower, PLLChannelID,timeout = None):
        if timeout is None:
            timeout = self.TimeOut
        ret = RSU_INIT_rq(self.fd, Time, BSTInterval, RetryInterval,TxPower, PLLChannelID, timeout)
        if ret != 0:
            raise DeviceException(u"init_rq 指令发送失败，返回值:%d"%ret)
        return RSU_INIT_rs(self.fd,timeout)

    def PSAM_Reset(self,PSAMSlot, baud,timeout = None):
        if timeout is None:
            timeout = self.TimeOut
        ret = PSAM_Reset_rq(self.fd, PSAMSlot, baud, timeout)
        if ret != 0:
            raise DeviceException(u"pasm reset_rq 指令发送失败，返回值:%d"%ret)
        return PSAM_Reset_rs(self.fd,PSAMSlot,timeout)

    def PSAM_CHANNEL(self,PSAMSlot, APDUList, APDU,timeout=None):
        if timeout is None:
            timeout = self.TimeOut
        ret = PSAM_CHANNEL_rq(self.fd, PSAMSlot, APDUList, APDU,timeout)
        if ret != 0:
            raise DeviceException(u"pasm cmd_rq 指令发送失败，返回值:%d"%ret)
        return PSAM_CHANNEL_rs(self.fd, PSAMSlot, timeout)

    def RSUDoLog(self,IsWriteLog):
        RSUDoLog(IsWriteLog)

    def INITIALISATION(self,BeaconID, Time, Profile,MandApplicationlist, MandApplication, Profilelist,timeout=None):
        if timeout is None:
            timeout = self.TimeOut
        ret =INITIALISATION_rq(self.fd, BeaconID, Time, Profile,
            MandApplicationlist, MandApplication, Profilelist,
            timeout)
        if ret != 0:
            raise DeviceException(u"bst_rq 指令发送失败，返回值:%d"%ret)
        return INITIALISATION_rs(self.fd,timeout)

    def GetSecure(self,accessCredentialsOp, mode, DID,
        AccessCredentials, keyIdForEncryptOp, FID, offset,
        length, RandRSU, KeyIdForAuthen, KeyIdForEncrypt,timeout=None):
        if timeout is None:
            timeout = self.TimeOut
        ret = GetSecure_rq(self.fd, accessCredentialsOp, mode, DID,
            AccessCredentials, keyIdForEncryptOp, FID, offset,
            length, RandRSU, KeyIdForAuthen, KeyIdForEncrypt,
            timeout)
        if ret != 0:
            raise DeviceException(u"getSecure_rq 指令发送失败，返回值:%d"%ret)
        return GetSecure_rs(self.fd,timeout)

    def TransferChannel(self,mode, DID, ChannelID, APDULIST,APDU,timeout=None):
        if timeout is None:
            timeout = self.TimeOut
        ret = TransferChannel_rq(self.fd, mode, DID, ChannelID, APDULIST,APDU, timeout)
        if ret != 0:
            raise DeviceException(u"transferChannel_rq 指令发送失败，返回值:%d"%ret)
        return TransferChannel_rs(self.fd, timeout)

    def TransferChannelSetMMI(self,mode, DID, ChannelID, APDULIST,APDU,s_mode,s_DID,setMMIPar,timeout=None):
        if timeout is None:
            timeout = self.TimeOut
        ret = TransferChannelSetMMI_rq(self.fd, mode, DID, ChannelID, APDULIST,APDU, s_mode, s_DID, setMMIPar, timeout)
        if ret != 0:
            raise DeviceException(u"transferChannel_rq 指令发送失败，返回值:%d"%ret)
        return TransferChannelSetMMI_rs(self.fd, timeout)

    def SetMMI(self,mode, DID, SetMMIPara,timeout=None):
        if timeout is None:
            timeout = self.TimeOut
        ret = SetMMI_rq(self.fd, mode, DID, SetMMIPara, timeout)
        if ret != 0:
            raise DeviceException(u"setMMI_rq 指令发送失败，返回值:%d"%ret)
        return SetMMI_rs(self.fd,timeout)

    def EVENT_REPORT(self,mode, DID, EventType,timeout=None):
        if timeout is None:
            timeout = self.TimeOut
        return EVENT_REPORT_rq(self.fd, mode, DID, EventType, timeout)

    def HFActive(self,InenetoryTimeOut, timeout=None):
        if timeout is None:
            timeout = self.TimeOut
        ret = HF_card_inventory_rq(self.fd, InenetoryTimeOut, timeout)
        if ret != 0:
            raise DeviceException(u"HF_card_inventory_rq 指令发送失败，返回值:%d"%ret)
        return HF_card_inventory_rs(self.fd,timeout)

    def HFCmd(self,APDULIST,APDU, timeout=None):
        if timeout is None:
            timeout = self.TimeOut
        ret = HF_card_channel_rq(self.fd, APDULIST,APDU, timeout)
        if ret != 0:
            raise DeviceException(u"HFCmd_rq 指令发送失败，返回值:%d"%ret)
        return HF_card_channel_rs(self.fd,timeout)


show_views_file = sc.textFile("/home/cloudera/assignment-2/input2/join2_gennum?.txt")
show_channel_file = sc.textFile("/home/cloudera/assignment-2/input2/join2_genchan?.txt")

def split_show_views(line):
	key_value = line.strip().split(",")
	key_in = key_value[0]
	value_in = key_value[1]
	return (key_in, int(value_in))

def split_show_channel(line):
	key_value = line.strip().split(",")
	key_in = key_value[0]
	value_in = key_value[1]
	return (key_in, value_in)

show_views = show_views_file.map(split_show_views)
show_channel = show_channel_file.map(split_show_channel)
joined_dataset = show_channel.join(show_views)

def extract_channel_views(show_views_channel):
	return show_views_channel[1]

channel_views = joined_dataset.map(extract_channel_views)

def sum_channel_views(a, b):
	return a+b

channel_views.reduceByKey(sum_channel_views).collect()


import csv

class CSVFileReader:

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

    def parse_csv_file_and_get_data(self):
        """
        Read the CSV data files and removing first row.

        :return:
        """
        data = self.read_csv_file()
        # Removing the column names
        column_names = data[0]
        del data[0]
        return column_names, data

    def read_csv_file(self):
        """
        Opening and reading csv files

        :return:
        """
        data_matrix = []
        with self.csv_file_object as csv_file:
            csv_file_data = csv.reader(csv_file)
            for row in csv_file_data:
                data_matrix.append(row)
        return data_matrix


from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User
from .constants import STATUS_CHOICES, CATEGORY_CHOICES
from rest_framework.authtoken.models import Token
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=User)
def create_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create(user=instance)

def images_folder(instance, filename):
    return 'media/tickets/images/{}'.format(filename)

class Ticket(models.Model):
    title = models.CharField(
        max_length=100
    )
    description = models.TextField()
    image = models.ImageField(
        upload_to=images_folder,
        null=True,
        blank=True
    )
    sender = models.ForeignKey(
        User,
        on_delete=models.CASCADE
    )
    status = models.CharField(
        max_length=20,
        choices=STATUS_CHOICES
    )
    date = models.DateTimeField(
        auto_now_add=True
    )
    category = models.CharField(
        max_length=20,
        choices=CATEGORY_CHOICES
    )


from unittest2 import TestCase
from threading import Thread
from xmlrpclib import ServerProxy

from sedna.sampler import NodeSamplerServer
from sedna.sampler import NodeSampler
from sedna.sampler import SystemdChecker
from sedna.sampler import PacemakerChecker
from sedna.sampler import InitdChecker
from sedna.common import Service

TEST_IP = "127.0.0.1"
TEST_PORT = "8000"
TEST_SERVICE_NAME = "$$test_name$$"
TEST_METHOD = "$$test_method$$"

class NodeSamplerTest(TestCase):
    """
    The test cases to test class NodeSampler
    """
    def setUp(self):
        self.node_sampler = NodeSampler()

    def test_systemd_checker(self):
        self.node_sampler.register_checker("systemd", SystemdChecker)
        self.service = Service(name=TEST_SERVICE_NAME,
                               check_methods="systemd",
                               ip=TEST_IP)
        self.fake_services = [self.service]
        results = self.node_sampler.sample(services=self.fake_services)
        self.assertEquals(len(results), len(self.fake_services))

    def test_pacemaker_checker(self):
        self.node_sampler.register_checker("pacemaker", PacemakerChecker)
        self.service = Service(name=TEST_SERVICE_NAME,
                               check_methods="pacemaker",
                               ip=TEST_IP)
        self.fake_services = [self.service]
        with self.assertRaises(NotImplementedError):
            self.node_sampler.sample(services=self.fake_services)

    def test_initd_checker(self):
        self.node_sampler.register_checker("init.d", InitdChecker)
        self.service = Service(name=TEST_SERVICE_NAME,
                               check_methods="init.d",
                               ip=TEST_IP)
        self.fake_services = [self.service]
        with self.assertRaises(NotImplementedError):
            self.node_sampler.sample(services=self.fake_services)

    def test_unknown_checker(self):
        self.service = Service(name=TEST_SERVICE_NAME,
                               check_methods="unknown_checker",
                               ip=TEST_IP)
        self.fake_services = [self.service]
        with self.assertRaises(Exception):
            self.node_sampler.sample(services=self.fake_services)

class NodeSamplerServerStartTest(TestCase):
    """
    The test cases to test class NodeSamplerServer.
    """
    def setUp(self):
        _mocked_node_sampler = _MockedNodeSampler()
        self.node_sampler_server =\
            NodeSamplerServer(TEST_PORT, _mocked_node_sampler)

    def test_listen_port(self):
        """
        The test case to test the server
        created by NodeSamplerServer class.
        :return:
        """

        def start_server():
            self.node_sampler_server.start()

        server_thread = Thread(target=start_server)
        server_thread.setDaemon(True)
        server_thread.start()

        proxy = ServerProxy("http://localhost:8000")
        service_a = Service(name="$$service_a$$",
                            check_methods=TEST_METHOD,
                            ip=TEST_IP)
        service_b = Service(name="$$service_b$$",
                            check_methods=TEST_METHOD,
                            ip=TEST_IP)
        service_c = Service(name="$$service_c$$",
                            check_methods=TEST_METHOD,
                            ip=TEST_IP)
        fake_services = [service_a, service_b, service_c]
        self.assertEquals(
            proxy.sample(fake_services), 1,
            "NodeSamplerServer cannot create object.")

class NodeSamplerServerTest(TestCase):
    """
    The test cases to test the capacity of Server start on some occassions.
    """
    def setUp(self):
        _mocked_node_sampler = _MockedNodeSampler()
        self.node_sampler_server = NodeSamplerServer(8000,
                                                     _mocked_node_sampler)

        def start_server():
            self.node_sampler_server.start()

        server_thread = Thread(target=start_server)
        server_thread.setDaemon(True)

        server_thread.start()

    def test_sample_server(self):
        """
        This test case to test sampler response.
        :return:
        """
        fake_uri = "http://localhost:8000/"
        proxy = ServerProxy(fake_uri)

        dict_service_a = {"name": "$$test_service_a$$",
                          "ip": TEST_IP,
                          "check_methods": TEST_METHOD}
        dict_service_b = {"name": "$$test_service_b$$",
                          "ip": TEST_IP,
                          "check_methods": TEST_METHOD}

        fake_services = [dict_service_a, dict_service_b]
        self.assertEquals(proxy.sample(fake_services), 1,
                          "Failed to return result from sampler.")

    def test_sampler_server_negative(self):
        """
        This test case to test when transform value
        which cannot be found in config.
        :return:
        """
        pass

    def tearDown(self):
        pass

class _MockedNodeSampler(NodeSampler):
    """
    The mocked NodeSampler inheriting from NodeSampler contains stubs to
    imitate sampler class.
    """
    def sample(self, services, flag=None):
        return 1


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

"""
This module defines the :class:`~pryv.core.metadata.Metadata` class.

Metadata describe a release of a project.

.. seealso::

   `PEP 566 -- Metadata for Python Software Packages 2.1 | Python.org \
   <https://www.python.org/dev/peps/pep-0566/>`_
      Official python's documentation for a packages metadata.

"""

from packaging.version import parse as parse_version

#: The fields of metadata.
FIELDS = {
    'metadata_version': '2.1',
    'name': None,
    'version': None,
    'summary': None,
    'description': None,
    'description_content_type': 'text/x-rst; charset=UTF-8',
    'keywords': '',
    'home_page': '',
    'download_url': '',
    'author': '',
    'author_email': '',
    'maintainer': '',
    'maintainer_email': '',
    'license': '',
    'classifiers': None,
    'requires_dist': '',
    'requires_python': '',
    'requires_external': '',
    'project_url': '',
    'provides_extra': '',
}

def _parse_raw(raw):
    """
    Parses the given raw dictionary into a field dictionary. If a metadata field is not found, its
    default value is injected in the returned value.

    :param raw: The raw dictionary.
    :type raw: ``dict``

    :return: The parsed dictionary.
    :rtype: ``dict``
    """
    return {
        field: raw.get(field, default) or default
        for field, default in FIELDS.items()
    }

def parse(raw):
    """
    Parses the given raw object into a metadata instance.

    :param raw: The raw object to parse.

    :return: An instance of metadata.
    :rtype: :class:`~pryv.core.metadata.Metadata`
    """
    fields = _parse_raw(raw)
    fields['version'] = parse_version(raw.get('version'))
    fields['classifiers'] = raw.getlist('classifiers')

    return Metadata(fields)

class Metadata(object):
    """
    This class defines the metadata of a project.
    """

    def __init__(self, fields):
        """
        Creates a new instance of a metadata object.

        :param fields: The fields of the metadata.
        :type fields: ``dict``
        """
        self._fields = fields

    def __getattr__(self, attr_name):
        """
        Overrides python's ``getattr`` method.

        This method acts as a proxy to the metadata's fields which can be accessed directly as
        attributes.

        :param attr_name: The name of the attribute.
        :type attr_name: ``str``

        :return: The value of the field.
        :rtype: ``str``
        """
        if attr_name in self._fields:
            return self._fields.get(attr_name)

        raise ValueError('Unknown field `{}`'.format(attr_name))

    def serialize(self):
        """
        Serializes the metadata to a dictionary.

        :return: A serialized package.
        :rtype: ``dict``
        """
        serialized = self._fields.copy()
        serialized['version'] = self._fields.get('version').public

        return serialized

    @staticmethod
    def deserialize(raw):
        """
        Deserializes a raw dictionary into an instance of metadata.

        :param raw: The raw dictionary to deserialize.
        :type raw: ``dict``

        :return: An instance of metadata.
        :rtype: :class:`~pryv.core.metadata.Metadata`
        """
        fields = _parse_raw(raw)
        fields['version'] = parse_version(raw.get('version'))

        return Metadata(fields)


from flask import Flask, request, jsonify
from flask_restful import Resource, Api
from apps.client.ConfigClient import ConfigClient
from apps.client.PlayerStatisticsClient import PlayerStatisticsClient
from apps.data.PlayerGameStatistics import PlayerGameStatistics
from apps.data.NBASeasonStatistics import NBASeasonStatistics
from apps.data.ComplexEncoder import ComplexEncoder
import json
from datetime import date

# To run server do the following in the terminal
# virtualenv flask
# cd flask
# source bin/activate
# FLASK_APP=/Users/kevinsetayesh/Repos/DFS_2/apps/api/DFSApi.py flask run
#
# To hit an endpoint do a curl command in a NEW terminal (the other terminal is running the server):
#
# ex GET command:
# curl http://127.0.0.1:5000/multi/22
#
# ex POST command:
# curl -H "Content-Type: application/json" -X POST -d '{"name": "kevin", "address":"myAddress"}' http://127.0.0.1:5000/
#
# You can also use postman extension to hit api
#
# To Kill flask server, check out this --> https://stackoverflow.com/questions/19071512/socket-error-errno-48-address-already-in-use

app = Flask(__name__)
api = Api(app)

config_client = ConfigClient()
player_statistics_client = PlayerStatisticsClient()

class DFSApi(Resource):
    def get(self):
        return jsonify([{
                'id': 1,
                'title': 'Kevin Setayesh',
                'completed': False
            },
            {
                'id': 2,
                'title': 'Shannon Setayesh',
                'completed': True
            }
        ])
        # return {'about': 'Hello World!'}

    def post(self):
        some_json = request.get_json()
        return {'you sent': some_json}, 201

class Multi(Resource):
    def get(self, num):
        return {'result': num * 10}

class Config(Resource):
    def post(self):
        print("In Config post()")
        config_json = request.get_json()
        config = config_client.submit_config(config_json)
        print("post: ", config)
        json.loads(json.dumps(config.__dict__))
        # return {'result': result}

    def get(self):
        print("In Config get()")
        config = config_client.get_latest_config()
        print("get: ", config)
        return json.loads(json.dumps(config.__dict__))

class PlayerStatistics(Resource):
    def get(self):
        print("In PlayerStatistics get()")

        nba_year = request.args.get('nba_year')
        start_date = request.args.get('start_date')
        end_date = request.args.get('end_date')

        # request_params = request.get_json()
        # print('request_params: ', request_params)
        # nba_year = request_params['nba_year']
        # start_date = request_params['start_date']
        # end_date = request_params['end_date']
        print('nba_year: ', nba_year)
        print('start_date: ', start_date)
        print('end_date: ', end_date)

        nba_season_statistics: NBASeasonStatistics = \
            player_statistics_client.fetch_all_player_statistics_by_year(nba_year, start_date, end_date)

        return json.loads(json.dumps(nba_season_statistics.__dict__, cls=ComplexEncoder))

@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
    return response

api.add_resource(DFSApi, '/')
api.add_resource(Config, '/config')
api.add_resource(PlayerStatistics, '/playerstatistics') #<nbayear>')
api.add_resource(Multi, '/multi/<int:num>')

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


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

from theano import tensor

from thdl.model.network.abstract import AbstractNetwork
from thdl.model.tensors import get_tensor
from thdl.utils import is_iterable
from thdl.utils.random import get_dtype
from thdl.utils.random import set_seed
from thdl.model import metrics
from thdl.model.layers import Dropout
from thdl.model.objective import CategoricalCrossEntropy
from thdl.model.optimizer import SGD

_TRAIN_TEST_SPLIT_LAYERS = [Dropout, ]

class BaseNetwork(AbstractNetwork):
    def __init__(self, seed=None):
        # seed
        self.seed = seed

        # other parameters
        self.train_test_split = False

        # function
        self.train_func_for_eval = None
        self.predict_func_for_eval = None
        self.train_func_for_res = None
        self.predict_func_for_eval = None

        # components
        self.comp_layers = []
        self.comp_objective = None
        self.comp_optimizer = None

        # metric
        self.train_metrics = None
        self.predict_metrics = None

        # in and out
        self.output_tensor = None

    def set_objective(self, loss_func):
        self.comp_objective = loss_func

    def set_optimizer(self, optimizer):
        self.comp_optimizer = optimizer

    def set_metrics(self, metrics=None, train_metrics=None, predict_metrics=None):
        self.train_metrics = []
        self.predict_metrics = []

        if metrics:
            if type(metrics) in [tuple, list]:
                self.train_metrics.extend(metrics)
                self.predict_metrics.extend(metrics)
            else:
                self.train_metrics.append(metrics)
                self.predict_metrics.append(metrics)

        if train_metrics:
            if type(train_metrics) in [list, tuple]:
                self.train_metrics.extend(train_metrics)
            else:
                self.train_metrics.append(train_metrics)

        if predict_metrics:
            if type(predict_metrics) in [tuple, list]:
                self.predict_metrics.extend(predict_metrics)
            else:
                self.predict_metrics.append(predict_metrics)

    def _check_train_test_split(self, layer):
        if layer.__class__ in _TRAIN_TEST_SPLIT_LAYERS:
            self.train_test_split = True

    def set_output_tensor(self, output_tensor=None, out_dim=None, out_tensor_type=None):
        if output_tensor:
            if isinstance(output_tensor, tensor.TensorVariable):
                self.output_tensor = output_tensor
            else:
                self.output_tensor = get_tensor(output_tensor)
        else:
            assert out_dim and out_tensor_type
            self.output_tensor = tensor.TensorType(out_tensor_type, [False] * out_dim)()


#!/usr/bin/env python

# -*- *************** -*-
# @File  : start_with_cfg.py
# @Description :
# @Author: mql
# @Time  : 2020-09-04 11:24
# -*- *************** -*-

import codecs
from mycolorlog import logger

# f = codecs.open("plot.cfg", 'r', 'utf-8', buffering=True)
# # 读取所有行
# lines = f.readlines()
# f.close()

with open("plot.cfg", mode='r', encoding='utf-8') as f:
    lines = f.readlines()

logger.info("读取到的配置信息：" + str(lines))

collect_frequency = 3
post_rt_url = "http://127.0.0.1/sightApi/other/saveHistoryDataFromNodeJs"
post_his_url = "http://127.0.0.1/sightApi/redis/setAcqdataRData"
ip_tagPrefix = {}

isBase = False
isTagPrefix = False
for line in lines:

    if(not line or line == "" or line == '\n' or line.isspace()):
        continue

    print(line)

    if(line.startswith("# base")):
        isBase = True
        isTagPrefix = False
        continue
    if(line.startswith("# collect_ip_tagPrefix")):
        isBase = False
        isTagPrefix = True
        continue
    if(isBase):
        if(line.startswith("collect_frequency")):
            collect_frequency = int(line.split("=")[1])
        if(line.startswith("post_rt_url")):
            post_rt_url = str(line.split("=")[1])
        if(line.startswith("post_his_url")):
            post_his_url = str(line.split("=")[1])

    if(isTagPrefix):
        if(line.index(",") > 0):
            ddd = line.split(",")
            ip_tagPrefix[ddd[0]] = str(ddd[1].split()[0])

logger.info("collect_frequency= %d" % collect_frequency)
logger.info("post_rt_url= %s" % post_rt_url)
logger.info("post_his_url= %s" % post_rt_url)
logger.info("ip_tagPrefix: %s" % str(ip_tagPrefix))
print(len(ip_tagPrefix))


import pandas as pd
import matplotlib
matplotlib.rcParams.update({
    "pgf.texsystem": "pdflatex",
    'font.family': 'serif',
    'text.usetex': True,
    'pgf.rcfonts': False,
    'figure.dpi': 200,
})
import matplotlib.pyplot as plt
from pathlib import Path

from scripts.fitting import OptimizationProblem, FitParameter
from scripts.models import vectorborne
from scripts.simulator import Simulator

DATA_PATH = Path(__file__).parent.parent / "data"
RESULTS_PATH = Path(__file__).parent.parent / "results"

## Create Dataset ##
# Read data
filename = "00_PfPR_table_Global_admin0_2000-2017.csv"
filepath = DATA_PATH / filename
df = pd.read_csv(filepath, sep=',')

# filter data for 'Congo'
df = df[df["Name_0"] == "Congo"]

# Create data columns
df["n_cases"] = df["PAR"] * df["PfPR_rmean"]
df["time"] = (df["Year"] - df["Year"].min())

## Create simulator ##
# set model and initial values and parameters
model = vectorborne
Nh  = 5.2E6
i_h = 1E6
i_v = 100000

# not important, fitting will be done later!
biting_rate = 0.3
infection_rate_h = 0.4
recovery_rate_h = 0.2
infection_rate_v = 0.2
birth_rate_v = 0.3
mortality_rate_v = 0.4

y0 = [i_h, i_v]
p = [biting_rate,
     infection_rate_h,
     recovery_rate_h,
     infection_rate_v,
     birth_rate_v,
     mortality_rate_v
     ]

# Create simulator
sim = Simulator(model=model, y0=y0, args=(Nh,), parameters=p, integrator="LSODA")

fit_parameters = [
    FitParameter(
        pid="biting_rate", initial_value=0.1,
        lower_bound=1E-6, upper_bound=1
    ),
    FitParameter(
        pid="infection_rate_h", initial_value=0.003,
        lower_bound=1E-4, upper_bound=1
    ),
    FitParameter(
        pid="recovery_rate_h", initial_value=0.1,
        lower_bound=1E-3, upper_bound=1
    ),
    FitParameter(
        pid="infection_rate_v", initial_value=0.001,
        lower_bound=1E-5, upper_bound=1
    ),
    FitParameter(
        pid="birth_rate_v", initial_value=0.1,
        lower_bound=1E-1, upper_bound=1E4
    ),
    FitParameter(
        pid="mortality_rate_v", initial_value=0.001,
        lower_bound=1E-4, upper_bound=1
    ),
]

# run fit
op = OptimizationProblem(opid="congo_fit", sim=sim, parameters=fit_parameters, data=df)
p = op.fitting(size=1, results_path=RESULTS_PATH)

# solve one last time with the optimal values
t, y = sim.integrate(t=20, parameters=p)

## plot fit
fig, ax1 = plt.subplots()

# the humans
ax1.set_xlim(0,17)
ax1.set_xlabel('Year')
ax1.set_xticks([x for x in range(18)])
ax1.set_xticklabels(['\''+str(2000+x)[-2:] for x in range(18)])
ax1.set_ylabel('Number of humans')
ax1.ticklabel_format(axis='y',style='sci',scilimits=(6,6))
ax1.plot(t, y[0], label=r'Prediction of $I_h$', color='black')
ax1.plot(df.time, df.n_cases, label=r'Reference of $I_h$', color='black', linestyle='--')
ax1.tick_params(axis='y',labelcolor='black')

# the vectors
ax2 = ax1.twinx()
ax2.set_ylabel('Number of vectors',color='blue')
ax2.ticklabel_format(axis='y',style='sci',scilimits=(3,3))
ax2.plot(t, y[1], label=r'Prediction of $I_v$', color='blue')
ax2.tick_params(axis='y',labelcolor='blue')

# show
fig.legend(fancybox=True, framealpha=1, bbox_to_anchor=(0.9,0.938))
fig.tight_layout()
plt.show()


'''
The Jira class in this module is the primary abstraction around the Jira API.
'''
import collections.abc
import logging
import os
from typing import Dict, Optional, Set

import jsonlines
import pandas as pd

from jira_offline.config import get_cache_filepath, load_config
from jira_offline.exceptions import (EpicNotFound, EstimateFieldUnavailable, JiraApiError,
                                     JiraNotConfigured, MissingFieldsForNewIssue, ProjectDoesntExist)
from jira_offline.models import AppConfig, CustomFields, IssueFilter, Issue, IssueType, ProjectMeta
from jira_offline.utils.api import get as api_get, post as api_post, put as api_put
from jira_offline.utils.convert import jiraapi_object_to_issue
from jira_offline.utils.decorators import auth_retry

logger = logging.getLogger('jira')

class Jira(collections.abc.MutableMapping):
    _df: Optional[pd.DataFrame] = None

    filter: IssueFilter

    def __init__(self, *args, **kwargs):
        self.store = dict()
        self.update(dict(*args, **kwargs))

        # load application config without prompting
        self.config: AppConfig = load_config()

        # initialise an empty filter
        self.filter = IssueFilter()

    def __getitem__(self, key):
        return self.store[key]

    def __setitem__(self, key, value):
        self.store[key] = value

    def __delitem__(self, key):
        del self.store[key]

    def __iter__(self):
        return iter(self.store)

    def __len__(self):
        return len(self.store)

    class KeysView(collections.abc.KeysView):
        '''Override KeysView to enable filtering via __iter__'''
        def __init__(self, mapping, filter_):
            self._filter = filter_
            super().__init__(mapping)

        def __iter__(self):
            for key in self._mapping:
                if self._filter.compare(self._mapping[key]):
                    yield key

    class ItemsView(collections.abc.ItemsView):
        '''Override ItemsView to enable filtering via __iter__'''
        def __init__(self, mapping, filter_):
            self._filter = filter_
            super().__init__(mapping)

        def __iter__(self):
            for key in self._mapping:
                if self._filter.compare(self._mapping[key]):
                    yield (key, self._mapping[key])

    class ValuesView(collections.abc.ValuesView):
        '''Override ValuesView to enable filtering via __iter__'''
        def __init__(self, mapping, filter_):
            self._filter = filter_
            super().__init__(mapping)

        def __iter__(self):
            for key in self._mapping:
                if self._filter.compare(self._mapping[key]):
                    yield self._mapping[key]

    def keys(self):
        return Jira.KeysView(self, self.filter)

    def items(self):
        return Jira.ItemsView(self, self.filter)

    def values(self):
        return Jira.ValuesView(self, self.filter)

    def load_issues(self) -> None:
        '''
        Load issues from JSON cache file, and store in self (as class implements dict interface)
        '''
        cache_filepath = get_cache_filepath()
        if os.path.exists(cache_filepath) and os.stat(cache_filepath).st_size > 0:
            try:
                with open(cache_filepath) as f:
                    for obj in jsonlines.Reader(f.readlines()).iter(type=dict):
                        self[obj['key']] = Issue.deserialize(
                            obj, project_ref=self.config.projects[obj['project_id']]
                        )

            except (KeyError, TypeError, jsonlines.Error):
                logger.exception('Cannot read issues cache! Please report this bug.')
                return

    def write_issues(self):
        '''
        Dump issues to JSON cache file
        '''
        try:
            issues_json = []
            for issue in self.values():
                data = issue.serialize()

                # calculate the diff to the original Issue on Jira for existing Issues
                if issue.exists:
                    data['diff_to_original'] = issue.diff(data)

                issues_json.append(data)
        except TypeError:
            # an error here means the DataclassSerializer output is incompatible with JSON
            logger.exception('Cannot write issues cache! Please report this bug.')
            return

        with open(get_cache_filepath(), 'w') as f:
            writer = jsonlines.Writer(f)
            writer.write_all(issues_json)

    @auth_retry()
    def get_project_meta(self, project: ProjectMeta):  # pylint: disable=no-self-use
        '''
        Load additional Jira project meta data from Jira's createmeta API

        Params:
            project:  Jira project object into which we should load additional metadata
        '''
        try:
            params = {'projectKeys': project.key, 'expand': 'projects.issuetypes.fields'}
            data = api_get(project, 'issue/createmeta', params=params)
            if not data.get('projects'):
                raise ProjectDoesntExist(project.key)

            # project friendly name
            project.name = data['projects'][0]['name']

            issuetypes_: Dict[str, IssueType] = dict()
            priorities_: Set[str] = set()

            # extract set of issuetypes, and their priority values returned from the createmeta API
            for x in data['projects'][0]['issuetypes']:
                issuetypes_[x['name']] = IssueType(name=x['name'])

                # priority is a project-level setting, which can be extracted from any issue with the
                # "priority" field
                if not priorities_ and x['fields'].get('priority'):
                    priorities_ = {y['name'] for y in x['fields']['priority']['allowedValues']}

            # update project issuetypes & priorities to latest defined on Jira
            project.issuetypes = issuetypes_
            project.priorities = priorities_

            custom_fields = CustomFields()

            # extract custom fields from the API
            for issuetype in data['projects'][0]['issuetypes']:
                if custom_fields:
                    # exit loop when all custom field mappings have been extracted
                    break

                for field_props in issuetype['fields'].values():
                    if not custom_fields.epic_name and field_props['name'] == 'Epic Name':
                        custom_fields.epic_name = str(field_props['schema']['customId'])
                    elif not custom_fields.epic_ref and field_props['name'] == 'Epic Link':
                        custom_fields.epic_ref = str(field_props['schema']['customId'])
                    elif not custom_fields.estimate and field_props['name'] == 'Story Points':
                        custom_fields.estimate = str(field_props['schema']['customId'])

            project.custom_fields = custom_fields

            # pull project statuses for issue types
            self._get_project_issue_statuses(project)

            # pull project components
            self._get_project_components(project)

        except (IndexError, KeyError) as e:
            raise JiraApiError(f'Missing or bad project meta returned for {project.key} with error "{e.__class__.__name__}({e})"')

    def _get_project_issue_statuses(self, project: ProjectMeta):  # pylint: disable=no-self-use
        '''
        Pull valid statuses for each issuetype in this project

        Params:
            project:  Jira project to query
        '''
        data = api_get(project, f'project/{project.key}/statuses')

        for obj in data:
            try:
                issuetype = project.issuetypes[obj['name']]
                issuetype.statuses = {x['name'] for x in obj['statuses']}
            except KeyError:
                logger.debug('Unknown issuetype "%s" returned from /project/{project.key}/statuses', obj['name'])

    def _get_project_components(self, project: ProjectMeta):  # pylint: disable=no-self-use
        '''
        Pull set of components for this project

        Params:
            project:  Jira project to query
        '''
        data = api_get(project, f'project/{project.key}/components')
        project.components = {x['name'] for x in data}

    def new_issue(self, project: ProjectMeta, fields: dict) -> Issue:
        '''
        Create a new issue on a Jira project via the API

        Params:
            project:  Properties of the Jira project on which to create new Issue
            fields:   JSON-compatible key-value pairs for new Issue
        Returns:
            The new Issue, including the Jira-generated key field
        '''
        if 'key' not in fields or 'issuetype' not in fields or 'summary' not in fields:
            raise MissingFieldsForNewIssue(
                '{} is missing a mandatory field {}'.format(fields['key'], ','.join(fields))
            )

        try:
            # key is set by Jira server; remove it
            temp_key = fields['key']
            del fields['key']
            # project_id is application data; remove it
            del fields['project_id']

            # create new issue in Jira
            data = api_post(project, 'issue', data={'fields': fields})

            # retrieve the freshly minted Jira issue
            new_issue: Issue = self.fetch_issue(project, data['key'])

        except JiraApiError as e:
            err: str = 'Failed creating new {} "{}" with error "{}"'.format(
                fields['issuetype']['name'],
                fields['summary'],
                e.message
            )
            if e.message == 'gh.epic.error.not.found':
                raise EpicNotFound(err)
            if "Field 'estimate' cannot be set" in e.message:
                raise EstimateFieldUnavailable(project.key, project.jira_server)
            if 'cannot be set. It is not on the appropriate screen, or unknown.' in e.message:
                raise JiraNotConfigured(project.key, project.jira_server, err)

        # add to self under the new key
        self[new_issue.key] = new_issue

        if new_issue.issuetype == 'Epic':
            # relink any issue linked to this epic to the new Jira-generated key
            for linked_issue in [i for i in self.values() if i.epic_ref == temp_key]:
                linked_issue.epic_ref = new_issue.key

        # remove the placeholder Issue
        del self[temp_key]

        # write changes to disk
        self.write_issues()

        return new_issue

    def update_issue(self, project: ProjectMeta, issue: Issue, fields: dict) -> Optional[Issue]:
        '''
        Update an issue on Jira via the API

        Params:
            project:  Properties of the Jira project to update
            issue:    Issue object to update
            fields:   JSON-compatible key-value pairs to write
        '''
        try:
            api_put(project, f'issue/{issue.key}', data={'fields': fields})

            # Jira is now updated to match local; synchronize our local reference to the Jira object
            issue.original = issue.serialize()
            self[issue.key] = issue
            return issue

        except JiraApiError as e:
            logger.error('Failed updating %s with error "%s"', issue.key, e)

        return None

    def fetch_issue(self, project: ProjectMeta, key: str) -> Issue:  # pylint: disable=no-self-use
        '''
        Return a single Issue object from the Jira API by key

        Params:
            project:  Properties of the project pushing issues to
            key:      Issue key to lookup on Jira API
        Returns:
            Issue dataclass instance
        '''
        data = api_get(project, f'issue/{key}')
        return jiraapi_object_to_issue(project, data)

    def invalidate_df(self):
        '''Invalidate internal dataframe, so it's recreated on next access'''
        self._df = None

    @property
    def df(self) -> pd.DataFrame:
        '''Convert self (aka a dict) into a pandas DataFrame, and cache'''
        if self._df is None:
            items = {}
            for key, issue in self.items():
                items[key] = {
                    k:v for k,v in issue.__dict__.items()
                    if k not in ('original', 'diff_to_original')
                }
            self._df = pd.DataFrame.from_dict(items, orient='index')
            # rename the columns sourced from @property on Issue
            self._df.rename(columns={'_status': 'status', '_priority': 'priority'}, inplace=True)
        return self._df


import argparse
import os
import shutil
import time

import torch
import torchvision
from azureml.core import Run
from torch import nn
from torch.nn import functional as F
from torch.utils.data import DataLoader
from torchvision import transforms as T
from tqdm import tqdm

LEARNING_RATE = 1e-2
MODEL_CHECKPOINT_PATH = "model_checkpoints/checkpoint.pt"

def get_device():
    if torch.cuda.is_available():
        return torch.device(0)
    return torch.device("cpu")

device = get_device()

def init_optimizer(model):
    return torch.optim.Adadelta(model.parameters(), lr=LEARNING_RATE)

# The following neural net, as well as some other components of this script, are from the
# PyTorch example tutorial for training on MNIST:
# https://github.com/pytorch/examples/blob/0352380e6c066ed212e570d6fe74e3674f496258/mnist/main.py
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, 3, 1)
        self.conv2 = nn.Conv2d(32, 64, 3, 1)
        self.dropout1 = nn.Dropout(0.25)
        self.dropout2 = nn.Dropout(0.5)
        self.fc1 = nn.Linear(9216, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = torch.flatten(x, 1)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout2(x)
        x = self.fc2(x)
        output = F.log_softmax(x, dim=1)
        return output

def init_model():
    model = Net().to(device)
    return model

def train_epoch(model, train_data_loader, optimizer):
    for images, targets in tqdm(train_data_loader):

        images = images.to(device)
        targets = targets.to(device)

        optimizer.zero_grad()

        outputs = model(images)
        loss = F.nll_loss(outputs, targets)

        loss.backward()
        optimizer.step()

def save_checkpoint(model, optimizer, epoch):
    torch.save(
        {
            "epoch": epoch,
            "model_state_dict": model.state_dict(),
            "optimizer_state_dict": optimizer.state_dict(),
        },
        MODEL_CHECKPOINT_PATH,
    )
    shutil.copy(MODEL_CHECKPOINT_PATH, "outputs/model.pt")

def score_validation_set(model, data_loader):
    print("\nEvaluating validation set accuracy...\n")

    with torch.no_grad():

        num_correct = 0
        num_total_images = 0

        for images, targets in tqdm(data_loader):

            images = images.to(device)
            targets = targets.to(device)

            outputs = model(images)

            correct = torch.argmax(outputs, dim=1) == targets
            num_correct += torch.sum(correct).item()
            num_total_images += len(images)

        return num_correct, num_total_images

def load_checkpoint():
    checkpoint = torch.load(MODEL_CHECKPOINT_PATH)
    model = init_model()
    optimizer = init_optimizer(model)
    model.load_state_dict(checkpoint["model_state_dict"])
    optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
    return model, optimizer, checkpoint["epoch"]

if __name__ == "__main__":

    torch.manual_seed(0)

    # Parse input command-line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--num_epochs", type=int, help="total # of epochs to train the model"
    )
    args, _ = parser.parse_known_args()

    if torch.cuda.is_available():
        torch.cuda.set_device(device)

    run = Run.get_context()

    transform = T.Compose([T.ToTensor(), T.Normalize((0.1307,), (0.3081,))])
    train_dataset = torchvision.datasets.MNIST(
        ".", train=True, download=True, transform=transform
    )
    valid_dataset = torchvision.datasets.MNIST(
        ".", train=False, download=True, transform=transform
    )

    checkpoint_file_exists = os.path.exists(MODEL_CHECKPOINT_PATH)
    print(f"Checkpoint file exists: {checkpoint_file_exists}")

    # Load state from saved checkpoints if saved checkpoints exist.
    # Otherwise, initialize a model from scratch.
    if checkpoint_file_exists:
        print("Loading saved checkpoints...")
        model, optimizer, starting_epoch = load_checkpoint()
        starting_epoch += 1
    else:
        model = init_model()
        optimizer = init_optimizer(model)
        starting_epoch = 0

    # Set number of workers to # of of CPUs
    num_workers = os.cpu_count()

    # Initialize data loaders
    train_data_loader = DataLoader(
        train_dataset,
        batch_size=32,
        num_workers=num_workers,
        pin_memory=True,
        shuffle=True,
    )
    valid_data_loader = DataLoader(
        valid_dataset, batch_size=32, num_workers=num_workers, pin_memory=True
    )

    for epoch in range(starting_epoch, args.num_epochs):
        run.log("training_epoch", epoch)
        print(f"Starting epoch {epoch}")

        model.train()

        start = time.time()
        train_epoch(model, train_data_loader, optimizer)
        run.log("epoch_train_time", time.time() - start)

        run.flush()
        save_checkpoint(model, optimizer, epoch)

        model.eval()
        num_correct, num_total_images = score_validation_set(model, valid_data_loader)

        print(
            f"Scored validation set: {num_correct} correct, {num_total_images} total images"
        )
        validation_accuracy = num_correct / num_total_images * 100
        run.log("validation_accuracy", validation_accuracy)
        print(f"Accuracy: {validation_accuracy}%")

    print("Done")