from tkinter import *

# Variables globales
pile = []  # Contenu de la pile
entree_en_cours = ""  # Chaine de caractères représentant les chiffres entrés par l'utilisateur (input)
                      # Celle-ci est convertie en float lorsque l'utilisateur presse Entrée
                      # ou un bouton d'opération (e.g., +, *, -, /)

# Constantes définissant des les opérations de base
OP_ADD = 0 # Addition
OP_SOU = 1 # Soustraction
OP_MUL = 2 # Multiplication
OP_DIV = 3 # Division

# Ajoute un chiffre à ceux déjà entrés par l'utilisateur
def entre_chiffre(nb):
    global entree_en_cours
    entree_en_cours += str(nb)
    met_a_jour_affichage()

# Ajoute l'entrée en cours à la pile
def empile():
    global entree_en_cours
    if entree_en_cours != "":
        pile.append(float(entree_en_cours)) # Conversion de l'entrée en cours en nombre et ajout à la pile
    entree_en_cours = "" # Réinitialisation de l'entrée en cours
    met_a_jour_affichage()

# Ajoute un point aux chiffres déjà entrés par l'utilisateur
def point():
    global entree_en_cours
    entree_en_cours += "."
    met_a_jour_affichage()

# Efface l'entrée en cours, ou la pile
def clear():
    global entree_en_cours, pile
    if entree_en_cours == "":
        # Si l'entrée en cours est vide, on suppose que l'utilisateur veut effacer la pile
        pile = []
    else:
        entree_en_cours = ""
    met_a_jour_affichage()

# Mise à jours de l'affichage de la calculatrice (3 premières lignes de la pile)
def met_a_jour_affichage():
    global pile
    message = "" # On construit une chaine de charactère à afficher
    if len(pile) >= 5:
         for i in range(len(pile) -5, len(pile)):
             message += str(pile[i]) + "\n"
    else:
        for i in range(len(pile)):
            message += str(pile[i]) + "\n"
    if len(pile) > 0:
        message += "---\n"
    message += entree_en_cours
    texte_affichage["text"] = message

# Traitement d'une opération arithmétique
def operation(op):
    global pile
    empile() # On ajoute d'abord la pile ce que l'utilisateur pourrait avoir entré
    if len(pile) >= 2:
        x = pile.pop()
        y = pile.pop()
        if op == OP_ADD:
            pile.append(y + x)
        elif op == OP_SOU:
            pile.append(y - x)
        elif op == OP_MUL:
            pile.append(y * x)
        elif op == OP_DIV:
            pile.append(y / x)
        else:
            print("Operation inconnue.")
    met_a_jour_affichage()

# Crée la fenêtre principale
main = Tk()
main.geometry("500x510+553+90")
main.config(background="grey")
main.title("Calculatrice NPI")

# Ajoute un élément pour afficher la pile et l'entrée en cours
frame = Frame(main, borderwidth=9, relief=RIDGE)
frame.place(x=60, y=20)
texte_affichage = Label(frame)
texte_affichage.config(width=30, height=7, justify=RIGHT, anchor=NE, font=("Helvetica", 16))
texte_affichage.pack(padx=5, pady=5)
met_a_jour_affichage()

# Ajoute les boutons
Button(main, text="0", command=lambda: entre_chiffre(0)).place(x=140, y=400, width=50, height=30)
Button(main, text="1", command=lambda: entre_chiffre(1)).place(x=60,  y=250, width=50, height=30)
Button(main, text="2", command=lambda: entre_chiffre(2)).place(x=140, y=250, width=50, height=30)
Button(main, text="3", command=lambda: entre_chiffre(3)).place(x=220, y=250, width=50, height=30)
Button(main, text="4", command=lambda: entre_chiffre(4)).place(x=60,  y=300, width=50, height=30)
Button(main, text="5", command=lambda: entre_chiffre(5)).place(x=140, y=300, width=50, height=30)
Button(main, text="6", command=lambda: entre_chiffre(6)).place(x=220, y=300, width=50, height=30)
Button(main, text="7", command=lambda: entre_chiffre(7)).place(x=60,  y=350, width=50, height=30)
Button(main, text="8", command=lambda: entre_chiffre(8)).place(x=140, y=350, width=50, height=30)
Button(main, text="9", command=lambda: entre_chiffre(9)).place(x=220, y=350, width=50, height=30)

Button(main, text=".", command=point).place(x=60, y=400, width=50, height=30)
Button(main, text="CLS", command=clear).place(x=220, y=400, width=50, height=30)

Button(main, text="+", command=lambda: operation(OP_ADD)).place(x=300, y=400, width=50, height=30)
Button(main, text="-", command=lambda: operation(OP_SOU)).place(x=300, y=350, width=50, height=30)
Button(main, text="x", command=lambda: operation(OP_MUL)).place(x=300, y=250, width=50, height=30)
Button(main, text="/", command=lambda: operation(OP_DIV)).place(x=300, y=300, width=50, height=30)

Button(main, text="ENTRER", command=empile).place(x=380, y=350, width=80, height=30)

#Button(main, text="PILE", command=met_a_jour_affichage).place(x=380, y=425, width=80, height=30)

# Affiche la fenêtre principale construite et lance la boucle du programme
main.mainloop()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@File  : Parsing03.py
@Author: Xinzhe.Pang
@Date  : 2019/7/6 22:41
@Desc  :
"""
import requests
from lxml import etree

link = "http://www.santostang.com"
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}

r = requests.get(link, headers=headers)

html = etree.HTML(r.text)
title_list = html.xpath('//h1[@class="post-title"]/a/text()')
print(title_list)


## basic package
import pandas as pd
import numpy as np

## Visualisation packagers
import matplotlib.pyplot as plt
import seaborn as sns
import pylab as pl

## pkg data preparation
from sklearn import model_selection
from sklearn.model_selection import train_test_split, KFold, cross_val_score, GridSearchCV, ShuffleSplit, cross_val_score
from sklearn.preprocessing import StandardScaler, LabelEncoder, PolynomialFeatures

## pkg evaluation metric
from sklearn import metrics
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
from sklearn.metrics import roc_auc_score, plot_roc_curve
from sklearn.metrics import roc_curve, auc

import pickle

# CLASSIFICATION

def model_select_class(features, target, models):
    import numpy as np
    np.random.seed(10)
    X_train, X_test, y_train, y_test = train_test_split(features,target, test_size=0.2)
    train_accuracy=[]
    test_accuracy=[]
    ROC=[]

    algo_names = []
    accs = []
    rocs = []
    precs = []
    recs = []
    f1s = []
    gen_errors =[]
    model_probs = []
    for model in models:

        clf=model[1]
        clf.fit(X_train, y_train)
        train_acc=round((clf.score(X_train, y_train))*100,2)
        train_accuracy.append(train_acc)

        y_pred=clf.predict(X_test)
        #find accuracy
        test_acc=round(accuracy_score(y_test,y_pred)*100,2)
        test_accuracy.append(test_acc)

        #find the ROC_AOC curve
        roc=round(roc_auc_score(y_test,y_pred)*100,2)
        ROC.append(roc)

        y_true = y_test
        tn = confusion_matrix(y_true, y_pred)[0, 0]
        fp = confusion_matrix(y_true, y_pred)[0, 1]
        fn = confusion_matrix(y_true, y_pred)[1, 0]
        tp = confusion_matrix(y_true, y_pred)[1, 1]

        accu = round(((tp+tn)/(tp+tn+fp+fn))*100,2)
        prec = round((tp/(tp+fp))*100,2)
        rec = round((tp/(tp+fn))*100, 2) #TPR
        f1 = round((2*((prec*rec)/(prec+rec))),2)
        FPR = round((tn/(tn+fp))*100,2)
        roc_ = round((rec/FPR)*100)
        #gen_error = (train_acc - test_acc)
        #print("\nAccuracy {0} ROC {1}".format(acc,roc))

        algo_names.append(model[0])
        #train_accuracy.append(train_acc)
        rocs.append(roc)
        precs.append(prec)
        recs.append(rec)
        f1s.append(f1)
       # gen_errors.append(gen_error)
        #  Calibration Curve (Reliability Curves)
        #model_prob = clf.fit(X_train, y_train).predict_proba(X_test)
        #model_probs.append(model_prob)

    #skplt.metrics.plot_calibration_curve(y_test,    model_probs, algo_names, n_bins=15, figsize=(12,6));

    res = pd.DataFrame({"Agorithm":algo_names, "Train-Accuracy":train_accuracy,  "ROC":rocs, "Precision":precs,
                        "Recall":recs, "F1-Score":f1s, "Test-Accuracy":test_accuracy})
    res['"GEN. ERROR"'] = res["Train-Accuracy"] - res["Test-Accuracy"]

    return res

def classify_cv(X, y, algo_name, algorithm, gridsearchParams, cv):
    """
    Classification with cross-validation method
    """
    import numpy as np
    np.random.seed(10)
    X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2)

    grid = GridSearchCV(
    estimator=algorithm,
    param_grid=gridsearchParams,
    cv=cv, scoring='accuracy', verbose=1, n_jobs=-1)

    grid_model = grid.fit(X_train, y_train)
    best_params = grid_model.best_params_
    pred = grid_model.predict(X_test)
    predt = grid_model.predict(X_train)

    clf_report = classification_report(y_test,pred)
    train_acc = accuracy_score(y_train, predt)
    test_acc = accuracy_score(y_test, pred)
    conf_mat = confusion_matrix(y_test, pred)

    # metrcs = grid_result.gr
    ## Save model
    # metrcs = grid_result.gr
    #pickle.dump(grid_result, open(algo_name, 'wb'))
    filename = 'models/'+algo_name+str(round(train_acc*100,2))+'.sav'
    pickle.dump(grid_model, open(filename, 'wb'))
    filename1 = 'models/'+algo_name+str(round(train_acc*100,2))+'.pkl'
    pickle.dump(grid_model, open(filename1,'wb'))
    ## load the model from disk
    #loaded_model = pickle.load(open(filename, 'rb'))

    print('Best Params : ', best_params, '\n', '='*50)
    print('Classification Report :', clf_report, '\n', '='*50)
    print('Accuracy Score train : ' + str(train_acc))
    print('Accuracy Score test  : ' + str(test_acc), '\n', '='*50)
    print('Confusion Matrix : \n', conf_mat, '\n', '='*50)

    print("Prediction :")
    y_test =y_test.tolist()
    df2 = pd.DataFrame({'Actual':y_test,'Predicted':pred})
    return df2

def classify_cv1(features, target, algo_name, algorithm, gridsearchParams, cv):

    import numpy as np
    np.random.seed(10)
    algo_name=algo_name
    X_train, X_test, y_train, y_test = train_test_split(features,target, test_size=0.2)

    grid = GridSearchCV(
    estimator=algorithm,
    param_grid=gridsearchParams,
    cv=cv, scoring='accuracy', verbose=1, n_jobs=-1)

    grid_clf = grid.fit(X_train, y_train)
    best_params = grid_clf.best_params_
    y_pred = grid_clf.predict(X_test)
    train_acc = grid_clf.score(X_train, y_train)
    test_acc = grid_clf.score(X_test, y_test)
    roc=round(roc_auc_score(y_test,y_pred)*100,2)
    #redt = grid_clf.predict(X_train)

    y_true = y_test
    tn = confusion_matrix(y_true, y_pred)[0, 0]
    fp = confusion_matrix(y_true, y_pred)[0, 1]
    fn = confusion_matrix(y_true, y_pred)[1, 0]
    tp = confusion_matrix(y_true, y_pred)[1, 1]

    accu = round(((tp+tn)/(tp+tn+fp+fn))*100,2)
    prec = round((tp/(tp+fp))*100,2)
    rec = round((tp/(tp+fn))*100, 2) #TPR
    f1 = round((2*((prec*rec)/(prec+rec))),2)
    FPR = round((tn/(tn+fp))*100,2)
    roc_ = round((rec/FPR)*100)
    gen_error = (train_acc - test_acc)

    res = pd.DataFrame({"Agorithm":algo_name, "Train-Accuracy":train_acc, "ROC":roc, "Precision":prec,
                        "Recall":rec, "F1-Score":f1, "Test-Accuracy":test_acc, "GEN. ERROR":gen_error}, index=[0])
    return res

def classify_ho(X, y,algo_name,  algorithm):
    """
    Classification with holdout method
    """
    np.random.seed(10)

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

    #algo = algo
    model = algorithm.fit(X_train,y_train)
    #estimator=algorithm,
    pred = model.predict(X_test)
    predt = model.predict(X_train)

    clf_report = classification_report(y_test,pred)
    train_acc = accuracy_score(y_train, predt)
    test_acc = accuracy_score(y_test, pred)
    conf_mat = confusion_matrix(y_test, pred)

    ## Save model
    # metrcs = grid_result.gr
    #pickle.dump(grid_result, open(algo_name, 'wb'))
    filename = 'models/'+algo_name+str(round(train_acc*100,2))+'.sav'
    pickle.dump(model, open(filename, 'wb'))
    filename1 = 'models/'+algo_name+str(round(train_acc*100,2))+'.pkl'
    pickle.dump(model, open(filename1,'wb'))
    ## load the model from disk
    loaded_model = pickle.load(open(filename, 'rb'))

    #print('Best Params : ', best_params, '\n', '='*50)
    print('Classification Report :', clf_report, '\n', '='*50)
    print('Accuracy Score train : ' + str(train_acc))
    print('Accuracy Score test  : ' + str(test_acc), '\n', '='*50)
    print('Confusion Matrix : \n', conf_mat, '\n', '='*50)

    print("Prediction :")
    y_test =y_test.tolist()
    df2 = pd.DataFrame({'Actual':y_test,'Predicted':pred})
    #print(df2[:7].T)
    return loaded_model, df2

def classify_ho1(algo_name, algo_and_hp, features, target):
    import numpy as np
    np.random.seed(10)
    X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2)
    algo_names = algo_name
    clf = algo_and_hp
    clf.fit(X_train, y_train)
    y_pred=clf.predict(X_test)
    train_acc = clf.score(X_train, y_train)
    test_acc = clf.score(X_test, y_test)
    roc=round(roc_auc_score(y_test,y_pred)*100,2)

    y_pred=clf.predict(X_test)

    y_true = y_test
    tn = confusion_matrix(y_true, y_pred)[0, 0]
    fp = confusion_matrix(y_true, y_pred)[0, 1]
    fn = confusion_matrix(y_true, y_pred)[1, 0]
    tp = confusion_matrix(y_true, y_pred)[1, 1]

    accu = round(((tp+tn)/(tp+tn+fp+fn))*100,2)
    prec = round((tp/(tp+fp))*100,2)
    rec = round((tp/(tp+fn))*100, 2) #TPR
    f1 = round((2*((prec*rec)/(prec+rec))),2)
    FPR = round((tn/(tn+fp))*100,2)
    gen_error = round((train_acc - test_acc)*100,2)

    res = pd.DataFrame({"Agorithm":algo_names, "Train-Accuracy":train_acc, "ROC":roc, "Precision":prec,
                        "Recall":rec, "F1-Score":f1, "Test-Accuracy":test_acc, "GEN. ERROR":gen_error}, index=[0])
    return res

# FEATURE IMPORTANCE

def feature_import(features, model):
    importances = model.feature_importances_
    indices = np.argsort(importances)[::-1]

    print("Feature Ranking :" )
    features_name = []
    rankins_score = []
    for f in range(features.shape[1]):
       # print(" %s =>  (%f)  " %(list(X)[f], importances[indices[f]]))
        features_name.append(list(features)[f])
        rankins_score.append(importances[indices[f]])
    fimp = pd.DataFrame({'Features Name' : features_name,'Ranking Score' : rankins_score})
    fimp['%'] = round(fimp['Ranking Score']*100,2)

    plt.rcParams['figure.figsize'] = 5,5
    sns.set_style('whitegrid')
    ax = sns.barplot(x='Ranking Score', y='Features Name', data = fimp)
    ax.set(xlabel='Gini Importance')
    ax.set(title='Feature Importance')
    # plt.show()
    return fimp

# HYPERPARAMETER TUNING

def hyperparam_tuning(X_train, y_train, algo_name, algorithm, hyperparams, cv):
    grid = GridSearchCV(
    estimator=algorithm,
    param_grid=hyperparams,
    cv=cv, scoring='accuracy', verbose=1, n_jobs=-1)

    grid_model = grid.fit(X_train, y_train)
    best_params = grid_model.best_params_
    #print(f"{algo_name} best parameters are : {best_params}")
    return algo_name, best_params, cv

def hyperparam_tuning1(X,y, algos):
    scores = []
    cv = ShuffleSplit(n_splits=5, test_size=0.2, random_state=0)
    for algo_name, config in algos.items():
        gs =  GridSearchCV(config['model'],
                           config['params'],
                           cv=cv, return_train_score=False)
        gs.fit(X,y)
        scores.append({
            'model': algo_name,
            'best_score': gs.best_score_,
            'best_params': gs.best_params_
        })

    return pd.DataFrame(scores,columns=['model','best_score','best_params'])

#-PREDICTION

def prediction(loaded_model):
    '''
        Accept features values from the user and classify on base of those features
        if the iris flower spece (setosa, virginica, versicolor)
    '''
    print("============WELCOME TO=============")
    print("        Diabetes Prediction     ")
    print("===================================")
    print(" ")
    # Load Model
    #model = pickle.load(open(model_path, "rb"))

    print("Please Enter Predictor variables")
    print("--------------------------------")
    l=[]
    l.append(float(input('Enter Pregnancies : ')))
    l.append(float(input('Enter Glucose  : ')))
    l.append(float(input('Enter BloodPressure : ')))
    l.append(float(input('Enter SkinThickness  : ')))
    l.append(float(input('Enter Insulin : ')))
    l.append(float(input('Enter BMI  : ')))
    l.append(float(input('Enter DiabetesPedigreeFunction : ')))
    l.append(float(input('Enter Age  : ')))
    print("--------------------------------")

    print("")
    #arr = pd.DataFrame([l])
    #arr = np.asarray([l])
    model = loaded_model
    pred_result = model.predict([l])
    if pred_result[0] == 0:
        result = "This Patient is Not Prediabetic"
    else:
        result = "This Patient is Prediabetic"
    print("===================================")
    print(f'Result : {result}')
    print("============THANK YOU=============")

def prediction2(loaded_model, features):
    #model = pickle.load(open(model_path, "rb"))
    model = loaded_model
    pred_result = model.predict([features])
    if pred_result[0] == 0:
       print("This Patient is Not Prediabetic")
    else:
        print("This Patient is Prediabetic")


# oddsum2.py
# Sums all the odd numbers up to (and including) a given number
# RGW, 2013

def main():
    print( "Sum all the odd values" )
    start = int( input( "Enter the starting value (must be odd): " ) )
    end = int( input( "Enter the ending value (should be odd): " ) )
    total = 0
    count = 0
    for i in range( start, end+1, 2 ):
        total = total + i
        count = count + 1
    print( "This program summed", count, "values" )
    print( "  and the total is", total )

main()


# Enter hours and pay rate
hrs = input('Enter Hours:')
rate = input('Enter Pay Rate:')

# Non-numeric error message
try:
    h = float(hrs)
    r = float(rate)
except:
    print('Error, please enter numeric input.')
    quit() # Sanity check-point

# Calculating pay
o = r*1.5
pay = 0
if h <= 40.0:
    pay = h * r
    print(pay)
else:
    pay = (40.0*r) + ((h-40.0)*o)
    print(pay)


import requests
from typing import Union
from bs4 import BeautifulSoup

_UPLOADING_URL = "http://picplus.ru/upload.php"
_BOUNDARY = "---------------------------2782508715104495231337274406"
_HEADERS = {
    "Host": "picplus.ru",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate",
    "Content-Type": f'multipart/form-data; boundary={_BOUNDARY}',
    "cache-control": "no-cache"
    }
_FORM_DATA = {
    "urlarea": None,
    "check_compres": "true",
    "new_comp": "80",
    "thumb_size": "400",
    "check_orent": "m",
    "texttype": "nothing",
    "text": "awd"
    }

def _retrieve_urls(page_html):
    soup = BeautifulSoup(page_html, features="html.parser")
    tds = soup.find_all("td")
    urls = []
    for i in range(3, len(tds)-2, 2):
        urls.append(tds[i]
            .find_all("span")[1]
            .find_all("input")[1]
            ["value"]
        )
    return urls

def _upload_images(urls: Union[str, list]):
    if type(urls) is list:
        if len(urls) > 10:
            raise Exception("You can't upload more than 10 images at once")
        urls = " ".join(urls)

    _FORM_DATA["urlarea"] = urls
    res = requests.post(_UPLOADING_URL, _FORM_DATA)
    page_html = res.text
    res.close()

    return _retrieve_urls(page_html)

def upload_images(urls: Union[str, str]):
    print(f'Uploading images to hosting...')

    tens = len(urls) // 10
    reminder = len(urls) % 10

    print(f'Images array size is {len(urls)}')
    print(f'Tens: {tens}')
    print(f'Reminder: {reminder}')

    uploaded_urls = []
    for i in range(tens):
        ten = urls[i*10:i*10+10]
        uploaded_urls.extend(_upload_images(ten))
    if reminder > 0:
        reminded = urls[len(urls)-reminder:]
        uploaded_urls.extend(_upload_images(reminded))

    print("Uploading images is completed!")
    return uploaded_urls


import cv2
import argparse
import json
points = []

def onMouseCallback(event, x, y, flags, param):
    global points
    if event == cv2.EVENT_LBUTTONDOWN:
        points.append((x,y))
    # elif event == cv2.EVENT_LBUTTONDBLCLK:
    #     points = points[:len(points)-1]

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--input', type=str, help='image which need to be calibrate')
    parser.add_argument('--output', type=str, help='output file to save the calibration')
    args = parser.parse_args()
    assert args.input != None
    assert args.output.split('.')[-1] == 'json'
    img = None
    if args.input.split('.')[-1] == 'jpg' or args.input.split('.')[-1] == 'png':
        img = cv2.imread(args.input)
    elif args.input.split('.')[-1] == 'avi' or args.input.split('.')[-1] == 'mp4':
        cap = cv2.VideoCapture(args.input)
        ret, img = cap.read()
        cap.release()
    else:
        exit(-1)
    cv2.namedWindow('calib')
    cv2.setMouseCallback('calib', onMouseCallback)
    while 1:
        cp = img.copy()
        for i in range(len(points)-1):
            cv2.line(cp, points[i], points[i+1], (255,255,255), thickness=1)
        cv2.imshow('calib', cp)
        q = cv2.waitKey(30)
        if q == ord('q'):
            break
        elif q == ord('x'):
            points = points[:len(points) - 1]

    with open(args.output, 'r+') as w:
        root = json.load(w)
        root['area'] = []
        for p in points:
            root['area'].append(p)

    with open(args.output, 'w')  as w:

        json.dump(root, w)
        # root['area'] = points
        # json.dump(root, w)
        # print(root)


# 寻找10000以内的所有素数(质数)
import time

# 返回所有质数
def prime(number):
    prime_list = []
    for i in range(2, number + 1):
        for j in range(2, i):
            if i % j == 0:
                break
        else:
            prime_list.append(i)
    return prime_list

# 返回所有质数(优化)
def prime_optimize(number):
    if number < 2:
        return []
    else:
        prime_list = [2]
    for i in range(3, number + 1, 2):  # 优化2：除2外，偶数不是质数
        for j in range(2, int(i ** 0.5) + 1):  # 优化1：如果n不是质数, 则n有满足1<d<=sqrt(n)的因子d
            if i % j == 0:
                break
        else:
            prime_list.append(i)
    return prime_list

n = 10000
t0 = time.time()
l1 = prime(n)
t1 = time.time()
l2 = prime_optimize(n)
t2 = time.time()
print(l1[:10], len(l1) if l1 == l2 else "Error!")
print("优化前：{}".format(t1 - t0))
print("优化后：{}".format(t2 - t1))
input("运行完毕，请按回车键退出...")

【第09课】枚举算法/Q2求质数(优化版).py


"""Registry where you can register components by target class and classes
that you look up the component for."""

from .mapping import MultiMap, ClassMapKey, ClassMultiMapKey, InverseMap
from .interfaces import IRegistry, IClassLookup
from .lookup import Lookup

SENTINEL = object()

class ClassRegistry(IRegistry, IClassLookup):
    def __init__(self):
        self._map = MultiMap()

    def register(self, target, sources, component):
        key = ClassMultiMapKey(*sources)
        target = ClassMapKey(target)
        im = self._map.exact_get(key)
        if im is None:
            self._map[key] = im = InverseMap()
        im.register(target, component)

    def clear(self):
        self._map = MultiMap()

    def exact_get(self, target, sources):
        key = ClassMultiMapKey(*sources)
        target = ClassMapKey(target)
        im = self._map.exact_get(key)
        if im is None:
            return None
        return im.exact_get(target)

    def get(self, target, sources):
        try:
            return next(self.get_all(target, sources))
        except StopIteration:
            return None

    def get_all(self, target, sources):
        target = ClassMapKey(target)
        key = ClassMultiMapKey(*sources)
        for im in self._map.all(key):
            for found in im.all(target):
                yield found

class Registry(ClassRegistry, Lookup):
    """A registry that is also a lookup.
    """
    def __init__(self):
        ClassRegistry.__init__(self)
        # the class_lookup is this class itself
        Lookup.__init__(self, self)


# coding=utf-8
# __author__ = 'doriswang'
import numpy as np
import matplotlib.pyplot as plt
import random

file_name = '/home/dw20/yw58/CGT/output/10/10000000/5000/0/0001/seq/rf/stDistance.txt'
input_file = open(file_name, 'r')
gfile_name = '/home/dw20/yw58/CGT/output/10/10000000/5000/0/0001/cgt/rf/stDistance.txt'
ginput_file = open(gfile_name, 'r')
##sinput_file = open(sfile_name, 'r')
#output = open('newLL.txt', 'w')
lines = input_file.readlines()
glines = ginput_file.readlines()
#slines = sinput_file.readlines()

LL = 0.6153846153846154
#d = 0.25
cLL = 0
c_count = 0
c = 0
x = [0]
y = [LL]
y1 = [cLL]
data = []

for line in lines:
    x.append(c_count)
    y.append(float(line))
    c_count+=1
    if(c_count>100):
        break

counter = 0

for gline in glines:
    y1.append(float(gline))
    counter+=1
    if(counter>100):
        break

input_file.close()
ginput_file.close()
#output.close()
# -*- coding: utf-8 -*-

fig = plt.figure(figsize=(18,6)) #创建绘图对象
#plt.plot(x,y,"b",linewidth=1)   #在当前绘图对象绘图（X轴，Y轴，蓝色虚线，线宽度）
#plt.plot(x,y1,"r",linewidth=1)   #在当前绘图对象绘图（X轴，Y轴，蓝色虚线，线宽度）

plt.plot(x, y, 'b')
plt.plot(x, y1, 'r')
#ax1.set_ylim([-15280, -14000])
#ax1.set_title("Double Y axis")

#ax2.set_xlabel('')

#plt.xlabel("Iteration(s)") #X轴标签
#plt.ylabel("Log likelihood")  #Y轴标签
plt.title("P(GT|ST) v.s. P(S|GT)P(GT|ST) likelihood") #图标题
plt.show()  #显示图
plt.savefig("line.jpg") #保存图


'''
Created on Aug 20, 2019

@author: latikamehra
'''

from fileHandlers import ReadWriteDictionary
from formatters import AppLogger, PrettyPrinter
import os
import logging
from execs import FindDuplicates, ReviewDuplicates, ManageDuplicates

class FindReviewManageDuplicates():

    def __init__(self, imgDir, manualReview=True, pixThreshold=5, confirmLevelCount=3):

        self.imgDir = imgDir
        self.pixThreshold = pixThreshold
        self.confirmLevelCount = confirmLevelCount
        fileSuffix = "threshold_"+str(pixThreshold)
        self.manualReview  = manualReview

        self.toKeepDir = self.imgDir+"/DuplicatesToKeep/"
        self.secDupesDir = self.imgDir+"/DuplicatesToRemove/"

        curDir = os.path.dirname(os.path.abspath(__file__))
        opDir = curDir+"/logsAndOutput/"
        logDir = opDir+"/logs/"
        debugDir = opDir+"/debug/"
        #debugDir = opDir+"/"+fileSuffix+"/debug/"
        #logDir = opDir+"/"+fileSuffix+"/logs/"
        resDir = opDir+"/"+fileSuffix+"/results/"
        sumDir = opDir+"/"+fileSuffix+"/summary/"

        AppLogger.initiate(logDir, "FindAndHandleDuplicates", consoleFlag=False, lvl=logging.DEBUG)

        self.basicDetsLog = AppLogger.prntr("BasicDetails"+fileSuffix, debugDir, "BasicImageDetails", consoleFlag=False)
        self.potentialDupleListOP = AppLogger.prntr("PotentialDupeList"+fileSuffix, debugDir, "PotentialDuplicates", consoleFlag=False)
        self.op_summary = AppLogger.prntr("Summary"+fileSuffix, sumDir, "Summary", consoleFlag=True)

        opWidth = 150
        self.pp = PrettyPrinter.PrettyPrint(opWidth=opWidth)
        self.sep1 = "-"*opWidth
        self.sep2 = "="*opWidth

        probableDupleListOP = resDir+"ProbableDuplicates"+".json"
        reviewedDupeDictOP = resDir+"ReviewedDuplicates"+".json"

        self.DictBckp = ReadWriteDictionary.ReadWrite(probableDupleListOP)
        self.RvwdBckp = ReadWriteDictionary.ReadWrite(reviewedDupeDictOP)

        prntStr = "\n\n"
        prntStr += self.pp.cat([self.sep2])
        prntStr += self.pp.cat(["Threshold = "+str(threshold)])
        prntStr += self.pp.cat([self.sep2])

        self.op_summary.info(prntStr)

    def exec(self, actionList=['find', 'review', 'manage']):
        if 'find' in actionList :
            fnd = FindDuplicates.Find(self)
            fnd.findDuplicates()

        if 'review' in actionList :
            rvw = ReviewDuplicates.Review(self)
            rvw.review(self.manualReview)

        if 'manage' in actionList :
            mng = ManageDuplicates.Manage(self)
            mng.manage(self.manualReview)

    def getDupeCount(self, dupeDict):
        cntr = 0
        for dupeList in dupeDict.values():
            cntr += len(dupeList)

        return cntr

if __name__ == "__main__" :

    imgDir = "/Users/latikamehra/Pictures/JonnyPics/"
    thresholds = [10]
    confirmLevelCount = 3
    manualReview = False
    dupesRemoved = 0
    rmd = [None]*len(thresholds)

    actionList = ['manage']
    actionList = ['find', 'review', 'manage']

    try :
        for i, threshold in enumerate(thresholds):
            rmd[i] = FindReviewManageDuplicates(imgDir, manualReview, threshold, confirmLevelCount)
            rmd[i].exec(actionList)
    except Exception as e:
        print (str(e))
        quit()


import pygame;
from pygame.sprite import Sprite;
from pygame.rect import Rect;

class Star(Sprite):
    def __init__(self, size, texture, screen, position):
        """конструктор - просто инициализирует"""
        super(Star , self).__init__();
        self.screen = screen;

        #загружаем изобрвжение звезды
        self.image = pygame.image.load(texture);

        # размеры и позиция
        self.rect = Rect(0, 0, 10, 10);
        self.rect.centerx = position[0];
        self.rect.centery = position[1];
        self.image = pygame.transform.scale(self.image, (size, size));

    def blitme(self):
        self.screen.blit(self.image, self.rect);


#!/usr/bin/env python3
#我是一关于进程的测试程序
#进程之间数据共享
import threading
from multiprocessing import  Process
import time

li = []

def foo(i):
    li.append(i)
    print('say hi',li)

for i in range(10):
    p = threading.Thread(target=foo, args=(i,))
    p.start()


import numpy as np

import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func, inspect

from flask import Flask, jsonify

#################################################
# Database Setup
#################################################
engine = create_engine("sqlite:///hawaii.sqlite")

# Create the inspector and connect it to the engine
inspector = inspect(engine)

# reflect an existing database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(engine, reflect=True)

# Save reference to the table
#### Passenger = Base.classes.passenger

Measurement = Base.classes.measurement
Station = Base.classes.station

#################################################
# Flask Setup
#################################################
app = Flask(__name__)

#################################################
# Flask Routes
#################################################

# /
# Home page.
# List all routes that are available.
@app.route("/")
def welcome():
    """List all available api routes."""

    return (
        """Available Routes:
"""
        f"/api/v1.0/precipitation
"
        f"/api/v1.0/stations
"
        f"/api/v1.0/tobs
"
        f"/api/v1.0/start
"
        f"/api/v1.0/start/end

"
        f"for 'start' and 'end' portion of the URL, enter date in the following format:
"
        f"yyyy-mm-dd"
    )

# Create our session (link) from Python to the DB
#session = Session(engine)

# /api/v1.0/precipitation
# Convert the query results to a Dictionary using date as the key and prcp as the value.
# Return the JSON representation of your dictionary.
@app.route("/api/v1.0/precipitation")
def precipitation():
    session = Session(engine)
    ## instruction didn't specify what the query is.  Use start date of 2017-07-07 as the query
    query_result = session.query(Measurement.date, Measurement.prcp).\
    filter(Measurement.date >= '2017-07-07').\
    order_by(Measurement.date).all()
    session.close()

    precipitation_values_list = []
    # go through the query_results, grab the date and prcp, put it into dictionary as key and value
    # append dictionary to the list
    for i in query_result:
        precipiation_dictionary = {"Date":i.date, "Prcp": i.prcp}
        precipitation_values_list.append(precipiation_dictionary)
    # Convert list of tuples into normal list
    result = list(np.ravel(precipitation_values_list))
    return jsonify(result)

# /api/v1.0/stations
# Return a JSON list of stations from the dataset.
@app.route("/api/v1.0/stations")
def stations():
    session = Session(engine)
    query_result = session.query(Station.station).all()
    session.close()
    # Convert list of tuples into normal list
    result = list(np.ravel(query_result))
    # jsonify the list
    return jsonify(result)

# /api/v1.0/tobs
# query for the dates and temperature observations from a year from the last data point.
# Return a JSON list of Temperature Observations (tobs) for the previous year.
@app.route("/api/v1.0/tobs")
def tobs():
    session = Session(engine)
    query_result = session.query(Measurement.tobs).all()
    session.close()
    # Convert list of tuples into normal list
    result = list(np.ravel(query_result))
    # jsonify the list
    return jsonify(result)

# /api/v1.0/<start> and /api/v1.0/<start>/<end>
# Return a JSON list of the minimum temperature, the average temperature, and the max temperature for a given start or start-end range.
# When given the start only, calculate TMIN, TAVG, and TMAX for all dates greater than and equal to the start date.
@app.route("/api/v1.0/<start>")
def query_start_only(start):
    session = Session(engine)
    sel = [func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)]
    query_result = session.query(*sel).\
        filter(Measurement.date >= start).all()
    session.close()

    result = list(np.ravel(query_result))
    return jsonify(result)

# When given the start and the end date, calculate the TMIN, TAVG, and TMAX for dates between the start and end date inclusive.
@app.route("/api/v1.0/<start>/<end>")
def query_start_end(start, end):
    session = Session(engine)
    sel = [func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)]
    query_result = session.query(*sel).\
        filter(Measurement.date >= start).filter(Measurement.date <= end).all()
    session.close()

    result = list(np.ravel(query_result))
    return jsonify(result)

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


# Event Selector "selection_type" options
SINGLE_STREAM = "single_stream"
COMPETING_STREAMS = "competing_streams"

# Event Selector "method" options
GREEDY = "greedy"
EPS_GREEDY = "epsilon greedy"
SOFTMAX = "softmax"
THOMPSON = "thompson"
THOMPSON_SOFTMAX = "thompson softmax"

# ModelUpdater "kind" options
GBM = "gbm"
RF = "rf"
LOGISTIC = "logistic"
SINGLE_TREE = "single_tree"
BETA_TREE = "beta_tree"

# Variance estimator "kind" options
BOOTSTRAP = "bootstrap"


# -*- coding: utf-8 -*-
import os.path

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
# logger stuff
LOGGER_NAME = "AWS_Batch_Processing"
LOG_FILE = os.path.join(ROOT_DIR, "logger{}{}".format(os.path.sep, LOGGER_NAME))

# URL_BASE = "https://swapi.co/api/people"
URL_BASE = "https://swapi.co/api/"

# JSON SAMPLE FILES
JSON_SAMPLES = os.path.join(ROOT_DIR, 'json_airegas')
CALENDARIO_SAMPLE = os.path.join(JSON_SAMPLES, 'CalendarioEjemploV0.1.json')
PRECIO_FORMULA_SAMPLE = os.path.join(JSON_SAMPLES, 'PrecioFormulaEjemploV0.1.json')
TASA_MOLECULA_SAMPLE = os.path.join(JSON_SAMPLES, 'TasaMoleculaEjemploV0.1.json')
NONINATION_SAMPLE = os.path.join(JSON_SAMPLES, 'NominacionEjemploV0.1.json')
PREVISION_SAMPLE = os.path.join(JSON_SAMPLES, 'PrevisionEjemploV0.1.json')
CALENDARIO_B70_SAMPLE = os.path.join(JSON_SAMPLES, 'CalendarioB70EjemploV0.1.json')
PRECIOS_REGULADOS_SAMPLE = os.path.join(JSON_SAMPLES, 'PreciosReguladosEjemploV0.1.json')
CONSUMO_NOCTURNO_SAMPLE = os.path.join(JSON_SAMPLES, 'ConsumoNocturnoEjemploV0.1.json')
CONSUMO_SAMPLE = os.path.join(JSON_SAMPLES, 'ConsumoEjemploV0.1.json')
DATOS_CLI_SAMPLE = os.path.join(JSON_SAMPLES, 'DatosCLIEjemploV0.1.json')
DETALLE_CALIDAD_GAS_SAMPLE = os.path.join(JSON_SAMPLES, 'DetallesCalidadGasEjemploV0.1.json')
IMPORTES_ATR_SAMPLE = os.path.join(JSON_SAMPLES, 'ImportesATRPassthroughEjemploV0.1.json')

# FIELDS to check data integrity
# nombre_clase_FIELDS (se invocan por instrospeccion)

# Calendar
Calendar_FIELDS = ['_id', 'ts', 'calendarCode', 'Periods']
Periods_FIELDS = ['idPeriod', 'from', 'to', 'calendarDates', 'active']

# Precio formula
Formula_Price_FIELDS = ['_id', 'ts', 'formulaCode', 'formulaDes', 'from', 'to', 'compoundIndex', 'prices']
Prices_FIELDS = ["formulaDates", "formulaPrice", "formulaPriceDetail"]

# Tasa_Molecula
Molecule_Tax_FIELDS = ['_id', 'ts', 'taxCode', 'taxDes', 'from', 'to', 'taxVal']

# Nomination
Nomination_FIELDS = ['_id', 'ts', 'CLI', 'nomination']
Nomination_Details_FIELDS = ['nominationDates', 'nominationVal']

# Prevision
Forecast_FIELDS = ['_id', 'ts', 'CLI', 'forecast']
Forecast_Details_FIELDS = ['forecastDates', 'forecastPrice']

# Calendario_B70
B70_Calendar_FIELDS = ['_id', 'ts', 'CLI', 'from', 'to']

# Precios_regulados
Regulated_Price_FIELDS = ['_id', 'ts', 'regulatedPriceName', 'regulatedDates', 'regulatedVal']

# Consumo_Nocturno
Night_Consumption_FIELDS = ['_id', 'ts', 'CLI', 'dates', 'origin', 'qd']

# Consumo
Consumption_FIELDS = ['_id', 'ts', 'CLI', 'dates', 'origin', 'qd', 'qdElec', 'qdTerm', 'qdRed', 'qdSupRed']

# Datos_CLI
CLI_FIELDS = ['_id', 'ts', 'StartDate', 'EndDate']
Invoice_FIELDS = ['email', 'name', 'IBAN', 'swift', 'paymentTerm', 'paymentMethod']
Supply_FIELDS = ['CUPS', 'address', 'locality', 'postalCode', 'position']
Contract_FIELDS = ['CounterPartyName', 'CounterPartyDocNumber', 'clientName', 'connectionPoint', 'startDate', 'active',
                   'portfolio', 'broker', 'commission']

# Detalle calidad de gas
Gas_Quality_Detail_FIELDS = ['_id', 'ts', 'CLI', 'date', 'meter', 'detailPCS', 'detailPCI', 'detailDensity', 'detailN2',
                             'detailPressure', 'detailTemp', 'detailValueZ', 'detailValueK', 'detailLectm3',
                             'detailConsm3',
                             'detailAdjustementskWh', 'detailConskWh', 'detailCO2'
                             ]

# Importes ATR
Atr_Amount_FIELDS = ['_id', 'ts', 'CLI', 'From', 'To', 'ATRPrices']
ATRPrices_FIELDS = ['TPAId', 'TPAConcept', 'TPAConceptAmount']

MONGODB_TEST = {'db_name': "airegas",
                'host': 'localhost',
                'port': "27017",
                'user': '',
                'pwd': ''
                }

# URL API REST
ENDPOINT_URL = 'https://swapi.co/api/'


'''
Create a variable called num.
Check if the variable is divisible by 3 or 5.
If it is log “This number is divisible by 3 or 5”
to the console. Otherwise log “This number is not
divisible by 3 or 5”.
'''

num = 15

def divthreeorfive(numbervalue):
    if numbervalue % 5 == 0 and numbervalue % 3 == 0:
        print("Number divides by three and five.")
    else:
        print("Does not divide by three and five.")

divthreeorfive(num)


import pytest
from datetime import date

from tests.utils import file_response
from city_scrapers.spiders.ward_night import WardNightSpider, Calendar
from textwrap import dedent

test_response = file_response('files/ward_night.json')
spider = WardNightSpider(start_date=date(2017, 11, 1))
parsed_items = [item for item in spider.parse(test_response) if isinstance(item, dict)]

# def test_id():
#    assert parsed_items[0]['id'] == 'ward_night/201711071600/x/ward_night_ward_1'

def test_name():
    assert parsed_items[0]['name'] == 'Ward Night: Ward 1'

def test_description():
    expected = dedent("""\
    Ward Night with Alderman Joe Moreno (Ward 1).
    first come first served, one-on-one meetings usually about 10-20 minutes""")

    assert parsed_items[0]['description'] == expected

def test_start_time():
    assert parsed_items[0]['start_time'].isoformat() == '2017-11-07T16:00:00-06:00'

def test_end_time():
    assert parsed_items[0]['end_time'].isoformat() == '2017-11-07T18:00:00-06:00'

def test_location():
    assert parsed_items[0]['location'] == {
        'url': None,
        'name': '2740 W North Ave, Chicago',
        'coordinates': {
            'latitude': None,
            'longitude': None,
        }
    }

@pytest.mark.parametrize('item', parsed_items)
def test_timezone(item):
    assert item['timezone'] == 'America/Chicago'

@pytest.mark.parametrize('item', parsed_items)
def test_all_day(item):
    assert item['all_day'] is False

@pytest.mark.parametrize('item', parsed_items)
def test_classification(item):
    assert item['classification'] is None

@pytest.mark.parametrize('item', parsed_items)
def test_status(item):
    assert item['status'] == 'tentative'

@pytest.mark.parametrize('item', parsed_items)
def test__type(item):
    assert item['_type'] == 'event'

# Test integration with the Calendar class

def test_weekly_generation():
    row = [
        'Gregory Mitchell',
        '773.731.7777',
        'https://www.gregmitchell7thward.org/',
        '7',
        'Bridget Newsham',
        'Yes',
        '2249 E 95th St. Chicago ',
        'Weekly',
        'Monday',
        '3:00 PM',
        '7:00 PM',
        'No',
        '',
        'First come first served '
    ]

    spider = WardNightSpider()
    spider.start_date = date(2017, 10, 31)
    events = spider._parse_row(row)

    assert events[0]['start_time'].isoformat() == '2017-11-06T15:00:00-06:00'
    assert events[0]['end_time'].isoformat() == '2017-11-06T19:00:00-06:00'
#    assert events[0]['id'] == 'ward_night/201711061500/x/ward_night_ward_7'

    assert events[1]['start_time'].isoformat() == '2017-11-13T15:00:00-06:00'
    assert events[1]['end_time'].isoformat() == '2017-11-13T19:00:00-06:00'
#    assert events[1]['id'] == 'ward_night/201711131500/x/ward_night_ward_7'

    assert events[2]['start_time'].isoformat() == '2017-11-20T15:00:00-06:00'
    assert events[2]['end_time'].isoformat() == '2017-11-20T19:00:00-06:00'
#    assert events[2]['id'] == 'ward_night/201711201500/x/ward_night_ward_7'

def test_monthly_generation():
    row = [
        'Leslie Hairston',
        '773.324.5555',
        'http://leslieahairston.com/',
        '5',
        'Bridget Newsham',
        'Yes',
        'Rotating locations',
        'Monthly (4th occurrence)',
        'Tuesday',
        '6:00 PM',
        '8:00 PM',
        '',
        '',
        'Every fourth Tuesday, no meeting in November or December, group meetings rather than one-on-one'
    ]

    spider = WardNightSpider()
    spider.start_date = date(2017, 10, 31)
    events = spider._parse_row(row)

    assert events[0]['start_time'].isoformat() == '2017-11-28T18:00:00-06:00'
    assert events[0]['end_time'].isoformat() == '2017-11-28T20:00:00-06:00'
#    assert events[0]['id'] == 'ward_night/201711281800/x/ward_night_ward_5'

    assert events[1]['start_time'].isoformat() == '2017-12-26T18:00:00-06:00'
    assert events[1]['end_time'].isoformat() == '2017-12-26T20:00:00-06:00'
#    assert events[1]['id'] == 'ward_night/201712261800/x/ward_night_ward_5'

    assert events[2]['start_time'].isoformat() == '2018-01-23T18:00:00-06:00'
    assert events[2]['end_time'].isoformat() == '2018-01-23T20:00:00-06:00'
#    assert events[2]['id'] == 'ward_night/201801231800/x/ward_night_ward_5'

# Calendar tests

def test_third_thursday():
    cal = Calendar(date(2017, 10, 15))
    days = cal.nth_weekday(3, 'Thursday', 4)

    assert days == [date(2017, 10, 19), date(2017, 11, 16), date(2017, 12, 21),
                    date(2018, 1, 18)]

def test_every_monday():
    cal = Calendar(date(2017, 10, 15))
    days = cal.weekday('Monday', 4)

    assert days == [date(2017, 10, 16), date(2017, 10, 23), date(2017, 10, 30),
                    date(2017, 11, 6)]

def test_last_friday():
    cal = Calendar(date(2017, 10, 15))
    days = cal.last_weekday('Friday', 4)

    assert days == [date(2017, 10, 27), date(2017, 11, 24), date(2017, 12, 29),
                    date(2018, 1, 26)]


# coding=utf-8
from django.test import TestCase
from django.utils.text import slugify

from blog.serializers.tag import TagSerializer
from blog.models.post_category import PostCategory
from blog.models.post_tag import PostTag
from tests.help.method.rest import validate_response_data, PostRequest
from tests.help.method.category import TestCategory
from tests.help.method.tag import TestTag
from tests.help.method.user import create_test_user
from tests.help.validate.response import ValidateResponse
from django.utils.dateparse import parse_datetime
from blog.serializers.category import CategorySerializer

class TestCreatePost(TestCase):

    @validate_response_data
    def validate_response_data(self, *args, **kwargs):
        pass

    @create_test_user
    def test_create_post_missing_category(self, test_user):
        response = PostRequest().post(
            Authorization='Bearer {}'.format(test_user.access_token)
        )
        self.assertEqual(
            response.data.get('category'),
            ['This field is required.']
        )

    @create_test_user
    def test_create_post_missing_tags(self, test_user):
        category = TestCategory().create()
        response = PostRequest().post(
            category=category.id,
            Authorization='Bearer {}'.format(test_user.access_token)
        )
        self.assertEqual(
            response.data.get('tags'),
            ['This field is required.']
        )

    @create_test_user
    def test_create_with_not_existing_category(self, test_user):
        response = PostRequest().post(
            category=123,
            Authorization='Bearer {}'.format(test_user.access_token)
        )
        self.assertEqual(
            response.data.get('category'),
            ['Category matching query does not exist.']
        )

    @create_test_user
    def test_create_with_not_existing_tag(self, test_user):
        category = TestCategory().create()
        response = PostRequest().post(
            category=category.id,
            tags=[123],
            Authorization='Bearer {}'.format(test_user.access_token)
        )

        self.assertEqual(
            response.data.get('tags'),
            ['Tag matching query does not exist.']
        )

    @create_test_user
    def test_create_post_check_relations(self, test_user):
        category = TestCategory().create()
        tags = TestTag().create_multiple(amount=3)

        post_request = PostRequest()
        response = post_request.post(
            category=category.id,
            tags=[tag.id for tag in tags],
            Authorization='Bearer {}'.format(test_user.access_token)
        )

        self.validate_response_data(response=response, expected={
            'title': post_request.json.get('title'),
            'slug': slugify(post_request.json.get('title')),
            'body': post_request.json.get('body'),
            'public': post_request.json.get('public'),
            'date': parse_datetime(post_request.json.get('date')),
            'author': test_user.user.username,
            'category': CategorySerializer(category).data,
            'tags': TagSerializer(tags).data_list ,
            'status_code': 201
        }, exclude_actual=['id'])

        self.assertEqual(len(PostCategory.objects.all()), 1)
        self.assertEqual(len(PostTag.objects.all()), 3)

    def test_create_post_without_auth(self):
        response = PostRequest().post()
        validate = ValidateResponse(response=response, cls=self)
        validate.status_code(expected_status=401)

        validate.response_keys(expected_keys={
            'detail'
        })


from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<item_id>[0-9]+)/$', views.detail, name='detail'),
    url(r'^new/', views.new, name="new"),
    url(r'^newpilot/', views.newpilot, name="newpilot")
]


from check_id import CheckID

raw_data = "../../raw_datas/zillow_illinois.csv"
bucketname = "amfamdatamining1"
output = 'res.txt'
obj = CheckID()
obj.parse_csv_id(raw_data, bucketname, output)

# print obj.exist_s3(bucketname, 'rawfiles/3726897')


import pandas as pd
import numpy as np
from sklearn.base import TransformerMixin
from sklearn.pipeline import Pipeline

class last_week_of_month_log_returns(TransformerMixin):

    def __init__(self, col = 'close', lags = 5):
        self.transformer_name = 'last_weeks_log_returns'
        self.feature_name = 'day_ret_lag_'
        self.col = col
        self.lags = lags

    def fit(self, *_):
        # statless
        return self

    def transform(self, X, *_):

        out = pd.DataFrame()

        for sym in X.symbol.unique():
            result = X.loc[X.symbol == sym]
            result = result.groupby(['month', 'year']).tail(self.lags + 1)
            result.set_index(['month', 'year'], inplace = True)
            result = result[[self.col]].apply(lambda x: np.log(x) - np.log(x.shift(1))) # shift moves dates back by 1.

            reps = np.ceil(float(len(result)) / (self.lags + 1)).astype(int)
            pivot_col = np.tile([str(l + 1) for l in range(self.lags + 1)], reps)
            result['lags'] = pivot_col[0:len(result)]

            result = result.reset_index().pivot_table(
                index = ['year', 'month'], columns = 'lags', values = self.col)

            result.drop('1', inplace = True, axis = 1)
            result.columns = [self.feature_name + str(l) for l in range(self.lags)]
            result['symbol'] = sym

            out = pd.concat([out, result], axis = 0)

        out.set_index(['symbol'], append=True, inplace = True)

        return out

class monthly_log_returns(TransformerMixin):
    # get the price on the last day of the month
    # form returns series

    def __init__(self, col = 'close', lags = 12):
        self.transformer_name = 'monthly_log_returns'
        self.feature_name = 'log_return_month_'
        self.col = col
        self.lags = lags

    def fit(self, *_):
        # statless
        return self

    def transform(self, X, *_):

        out = pd.DataFrame()

        for sym in X.symbol.unique():
            result = X.loc[X.symbol == sym]
            result = result.groupby(['year', 'month']).last()
            result = result[[self.col]].apply(lambda x: np.log(x) - np.log(x.shift(1))) # shift moves dates back by 1.
            result.columns = [self.feature_name + '0']
            # want to lag a weeks (assume 5 days) worth of returns
            for l in range(self.lags - 1):
                lag_ret = result.iloc[:,-1].shift().to_frame(self.feature_name + str(l + 1))
                result = pd.concat([result, lag_ret], axis=1)

#             result.drop(self.feature_name + '0', inplace = True, axis = 1)
            result['symbol'] = sym
            out = pd.concat([out, result], axis = 0)

        out.set_index(['symbol'], append=True, inplace = True)

        return out

class monthly_targets(TransformerMixin):
    # get the price on the last day of the month
    # form returns series

    def __init__(self, col = 'close', log_ret = True):
        self.transformer_name = 'monthly_targets'
        self.feature_name = 'target'
        self.col = col
        self.log_ret = log_ret

    def fit(self, *_):
        # statless
        return self

    def transform(self, X, *_):

        out = pd.DataFrame()

        for sym in X.symbol.unique():
            result = X.loc[X.symbol == sym].groupby(['year', 'month']).last()
            if self.log_ret:
                result = result[[self.col]].apply(lambda x: np.log(x.shift(-1)) - np.log(x)) # shift moves dates back by 1.
            else:
                result = result[[self.col]].apply(lambda x: x.shift(-1) - x) # shift moves dates back by 1.
            result.columns = [self.feature_name]
            result['symbol'] = sym
            out = pd.concat([out, result], axis = 0)

        out.set_index(['symbol'], append=True, inplace = True)

        return out

class DFFeatureUnion(TransformerMixin):
    # FeatureUnion but for pandas DataFrames

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

    def fit(self, X, y=None):
        for (name, t) in self.transformer_list:
            t.fit(X, y)
        return self

    def transform(self, X):
        # assumes X is a DataFrame
        Xts = [t.transform(X) for _, t in self.transformer_list]
        Xunion = reduce(lambda X1, X2: pd.merge(X1, X2, left_index=True, right_index=True), Xts)
        return Xunion


# -*- coding: utf-8 -*-
# @Author: Institute for Infocomm Research
# @Date:   2020-08-14

import torch
import torch.nn as nn
from mahalanobis import MahalanobisLayer
from dataset import unsw
from fastloader import FastTensorDataLoader
from torch import optim
from tqdm import tqdm
import numpy as np

class MAE(nn.Module):
    def __init__(self, layer_dims):
        super(MAE, self).__init__()

        self.layer_dims = layer_dims

        self.encoding_layers = torch.nn.Sequential(
            nn.Linear(layer_dims[0], layer_dims[1]),  # 1st hidden layer
            nn.Tanh(),  # 1st hidden layer
            nn.Linear(layer_dims[1], layer_dims[2]),  # 2nd hidden layer
            nn.Tanh(),  # 2nd hidden layer
            nn.Linear(layer_dims[2], layer_dims[3]),  # 3rd hidden layer
            nn.Tanh(),  # 3rd hidden layer
            nn.Linear(layer_dims[3], layer_dims[4]),  # 4th hidden layer
            nn.Tanh(),  # 4th hidden layer
            nn.Linear(layer_dims[4], layer_dims[5]),  # 5th hidden layer
            nn.Tanh(),  # 5th hidden layer
            nn.Linear(layer_dims[5], layer_dims[6]),   # Compression layer
        )

        self.decoding_layers = torch.nn.Sequential(
            nn.Linear(layer_dims[6], layer_dims[7]),  # 7th hidden layer
            nn.Tanh(),  # 7th hidden layer
            nn.Linear(layer_dims[7], layer_dims[8]),  # 8th hidden layer
            nn.Tanh(),  # 8th hidden layer
            nn.Linear(layer_dims[8], layer_dims[9]),  # 9th hidden layer
            nn.Tanh(),  # 9th hidden layer
            nn.Linear(layer_dims[9], layer_dims[10]),  # 10th hidden layer
            nn.Tanh(),  # 10th hidden layer
            nn.Linear(layer_dims[10], layer_dims[11]),  # 11th hidden layer
            nn.Tanh(),  # 11th hidden layer
            nn.Linear(layer_dims[11], layer_dims[12])  # Output layer
        )

        # self.mahalanobis = mahalanobis

        # if mahalanobis:
        #     self.mahalanobis_layer = MahalanobisLayer(layer_dims[0],
        #                                               mahalanobis_cov_decay)

    def forward(self, x_feature):

        x_feature_enc = self.encoding_layers(x_feature)
        x_feature_fit = self.decoding_layers(x_feature_enc)

        # if self.mahalanobis:
        #     x_feature_fit = self.mahalanobis_layer(x_feature, x_feature_fit)

        return x_feature_fit

    def reconstruct(self, x_feature):

        x_feature_prime = self.encoding_layers(x_feature)
        x_feature_prime = self.decoding_layers(x_feature_prime)

        return x_feature_prime

'''
if __name__ == "__main__":

    layer_dims = 43, 512, 256, 128, 64, 32, 16, 32, 64, 128, 256, 512, 43

    model = MAE(layer_dims, mahalanobis=True, mahalanobis_cov_decay=0.001).cuda()

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    model.to(device)

    batch_size = 1024

    train_x, train_y = unsw("unsw/train.csv")
    test_x, test_y = unsw("unsw/test.csv")
    train_loader = FastTensorDataLoader(train_x, train_y, batch_size=batch_size, shuffle=True)
    test_loader = FastTensorDataLoader(test_x, test_y, batch_size=batch_size, shuffle=True)
    test_size = test_x.size(0)

    optimizer = optim.Adam(model.parameters(), lr=1e-3)
    criterion = nn.MSELoss()

    loss = 0.0
    for idx, (batch_features, _) in enumerate(tqdm(train_loader)):
        batch_features = batch_features.cuda()
        input_features = batch_features.view(-1, 43)
        outputs = model(input_features)
        import pdb; pdb.set_trace()

        train_loss = criterion(outputs, torch.zeros(outputs.size(), device=device))

        optimizer.zero_grad()
        train_loss.backward()
        optimizer.step()

        if model.mahalanobis_layer:
            with torch.no_grad():
                x_fit = model.reconstruct(input_features)
                model.mahalanobis_layer.update(input_features, x_fit)

    model.eval()
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    model.to(device)
    i = 0
    result = np.zeros([test_size, 1])
    for idx, (feature, label) in enumerate(tqdm(test_loader)):
        test_feature = feature.cuda()
        test_input_feature = test_feature.view(-1, 43)
        with torch.no_grad():
            output = model.reconstruct(test_input_feature)

        if i == 0:
            outputs = output
            features = test_input_feature
            labels = label
        else:
            outputs = torch.cat((outputs, output), 0)
            features = torch.cat((features, test_input_feature), 0)
            labels = torch.cat((labels, label), 0)

        i += 1
'''


#!/usr/bin/env python3

import socket, random, codec

sock=socket.socket()
#host = socket.gethostname()
#port = random ( 1025 , 65535 )
port= 12345

sock.connect(('127.0.0.1', port))

while True:
    data = sock.recv(1024)
    text = data.decode('ascii')
    print ("Server: {} ".format(text))
    rep = input('Client: ')
    reply = rep.encode('ascii')
    sock.send(reply)

sock.close()


# -*- coding: utf-8 -*-
#/#############################################################################
#
#    Tech-Receptives Solutions Pvt. Ltd.
#    Copyright (C) 2004-TODAY Tech-Receptives(<http://www.tech-receptives.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#/#############################################################################
from osv import osv
from osv import fields
import datetime
import time
week_number  = {
    'Mon': 1,
    'Tue': 2,
    'Web': 3,
    'Thu': 4,
    'Fri': 5,
    'Sat': 6,
    'Sun': 7,
}

class time_table_report(osv.osv_memory):
    _name = 'time.table.report'
    _description = 'Generate Time Table Report'
    _columns = {
        'standard_id': fields.many2one('op.standard', 'Standard'),
        'division_id': fields.many2one('op.division', 'Division'),
        'faculty_id': fields.many2one('op.faculty', string='Faculty'),
        'start_date':fields.date('Start Date', required=True),
        'end_date':fields.date('End Date', required=True),
        'state': fields.selection([('s','Student'),('t','Teacher')],string='Select',\
                                  required=True),
    }

    _defaults = {
                 'state': 't',
                 'start_date': time.strftime('2012-10-01'),
                 'end_date': time.strftime('2012-10-31')
                 }

    def gen_time_table_report(self, cr, uid, ids, context={}):
        value = {}
        data = self.read(cr, uid, ids, ['start_date', 'end_date','standard_id',\
                            'division_id','state','faculty_id'], context=context)

        if data[0]['state'] == 's':
            time_table_ids = self.pool.get('op.timetable').search(cr, uid, \
                      [('standard_id','=',data[0]['standard_id'][0]),
                        ('division_id','=',data[0]['division_id'][0]),
                        ('start_datetime','>',data[0]['start_date'] + '%H:%M:%S'),
                        ('end_datetime','<',data[0]['end_date'] + '%H:%M:%S'),
                        ],order='start_datetime asc')

            data[0].update({'time_table_ids': time_table_ids})
        else:
            teacher_time_table_ids = self.pool.get('op.timetable').search(cr, uid,\
                      [('start_datetime','>',data[0]['start_date'] + '%H:%M:%S'),
                        ('end_datetime','<',data[0]['end_date'] + '%H:%M:%S'),
                        ('faculty_id','=',data[0]['faculty_id'][1]),
                        ],order='start_datetime asc')

            data[0].update({'teacher_time_table_ids': teacher_time_table_ids})

        if data[0]['state'] == 's' :
            value = {
                    'type': 'ir.actions.report.xml',
                    'report_name': 'time.table.report',
                    'datas': data[0],
                    }
            return value
        elif data[0]['state'] == 't':
            value = {
                    'type': 'ir.actions.report.xml',
                    'report_name': 'time.table.teacher.report',
                    'datas': data[0],
                    }
            return value

time_table_report()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:


"""SAML 2.0 bindings module implements SOAP binding for subject query

NERC DataGrid Project
"""
__author__ = "P J Kershaw"
__date__ = "12/02/10"
__copyright__ = "(C) 2010 Science and Technology Facilities Council"
__license__ = "http://www.apache.org/licenses/LICENSE-2.0"
__contact__ = "Philip.Kershaw@stfc.ac.uk"
__revision__ = '$Id$'
import logging
log = logging.getLogger(__name__)

from ndg.saml.saml2.core import SubjectQuery, Subject, NameID
from ndg.saml.saml2.binding.soap.client import SOAPBindingInvalidResponse
from ndg.saml.saml2.binding.soap.client.requestbase import (
    RequestBaseSOAPBinding,)

class SubjectQueryResponseError(SOAPBindingInvalidResponse):
    """SAML Response error from Subject Query"""

class SubjectQuerySOAPBinding(RequestBaseSOAPBinding):
    """SAML Subject Query SOAP Binding
    """
    SUBJECT_ID_OPTNAME = 'subjectID'
    SUBJECT_ID_FORMAT_OPTNAME = 'subjectIdFormat'

    CONFIG_FILE_OPTNAMES = (
        SUBJECT_ID_OPTNAME,
        SUBJECT_ID_FORMAT_OPTNAME
    )

    __PRIVATE_ATTR_PREFIX = "__"
    __slots__ = tuple([__PRIVATE_ATTR_PREFIX + i
                       for i in CONFIG_FILE_OPTNAMES])
    del i

    QUERY_TYPE = SubjectQuery

    def __init__(self, **kw):
        '''Create SOAP Client for a SAML Subject Query'''
        self.__subjectIdFormat = None
        super(SubjectQuerySOAPBinding, self).__init__(**kw)

    def addQueryAttributes(self, query):
        """Adds to a query attributes that are configured for
        SubjectQuerySOAPBinding.
        """
        super(SubjectQuerySOAPBinding, self).addQueryAttributes(query)
        # Initialise a Subject with the configured format.
        query.subject = Subject()
        nameID = NameID()
        nameID.format = self.subjectIdFormat
        query.subject.nameID = nameID

    def _getSubjectIdFormat(self):
        return self.__subjectIdFormat

    def _setSubjectIdFormat(self, value):
        self.__subjectIdFormat = value

    subjectIdFormat = property(_getSubjectIdFormat, _setSubjectIdFormat,
                               doc="Subject Name ID format")

    def setQuerySubjectId(self, query, subjectID):
        """Sets the subject ID for a query created by SubjectQuerySOAPBinding or
        a derived class.
        """
        if not query.subject:
            query.subject = Subject()
            nameID = NameID()
            nameID.format = self.subjectIdFormat
        query.subject.nameID.value = subjectID


import math
from simanim import *

dubina_mora = 9
dno_broda = -1.1 # y koordinata dna slike broda
scena_w, scena_h  = 50, 30
brod_w, brod_h =  12, 9

def setup(m):
    PixelsPerUnit(15)
    ViewBox((0, -dubina_mora), 50, 30)
    FramesPerSecond(60)
    UpdatesPerFrame(10)

    m.f = InputFloat(10_000, (0, 20_000))  # N

    m.f_struje = -10_000
    m.brod_masa = 10_000
    m.a = (m.f_struje + m.f) / m.brod_masa
    m.v = 0 # brzina broda na desno
    m.x = 25 - brod_w/2 # leva tacka broda
                        # (na pocetku tako da je brod u sredini slike)

def update(m):
    dx = m.v * m.dt + m.a * m.dt * m.dt / 2
    dv = m.a * m.dt
    m.x += dx
    m.v += dv

    # ako je brod dotakao levu ili desnu ivicu ekrana,
    # ili se ne krece, zavrsi simulaciju
    if m.a == 0 or m.x <= 0 or m.x + brod_w >= 50:
        Finish()

def crtaj_vodoravan_vektor(x, y, d, boja):
    if d != 0:
        vec = Arrow((x, y), (x + d, y))
        vec.pen_color = boja
        vec.line_width = 0.3
        vec.head_len = 1 if abs(d) > 1 else abs(d)/2
        Draw(vec)

def draw(m):
    nebo = Box((0, 0), scena_w, scena_h - dubina_mora)
    nebo.fill_color = '#c6d3dc'
    more = Box((0, -dubina_mora), scena_w, dubina_mora)
    more.fill_color = '#8b9dc4'
    oblaci = Image("viking_back.png", (0, 0), scena_w, scena_h - dubina_mora)
    brod = Image("viking_ship.png", (m.x, dno_broda), brod_w, brod_h)
    Draw(nebo, more, oblaci, brod)

    ## koordinate i razmera vektora pri crtanju
    # (razmera je ovde odnos intenziteta vektora i njegove duzine)
    kf = 0.0008     # 1N (jedan Njutn) u duznim jedinicama simulacije
    ka = 0.7 * kf   # 1m/s² u duznim jedinicama simulacije
    x_f = m.x + brod_w/2 # x koordinata napadne tacke (poceci svih vektora)
    y_a = 15        # y koordinata vektora ubrzanja na slici
    y_frez = 10
    y_fvesla = dno_broda + 0.5
    y_fstruja = dno_broda

    # vektor sile struje
    crtaj_vodoravan_vektor(x_f, y_fstruja, m.f_struje*kf, '#ff0000')
    str_struja = f'Fs={abs(0.001*m.f_struje):0.0f}KN'
    tekst_struja = Text((x_f + m.f_struje*kf - 1, y_fstruja - 2), str_struja)
    tekst_struja.pen_color = '#ff0000'
    tekst_struja.font_size = 2
    Draw(tekst_struja)

    # vektor sile vesala
    crtaj_vodoravan_vektor(x_f, y_fvesla, m.f*kf, '#ffff00')
    tekst_vesla = Text((x_f, y_fvesla - 2), f'F={(0.001*m.f):0.1f}KN')
    tekst_vesla.pen_color = '#ffff00'
    Draw(tekst_vesla)

    # vektor rezultante
    crtaj_vodoravan_vektor(x_f, y_frez, (m.f_struje + m.f)*kf, '#008000')
    str_frez = f'Frez={(0.001*abs(m.f_struje+m.f)):0.1f}KN'
    if abs(m.a) < 0.3:
        if m.f_struje + m.f > 0:
            str_frez += ' (на десно)'
        elif m.f_struje + m.f < 0:
            str_frez += ' (на лево)'
    tekst_frez = Text((x_f, y_frez + 2), str_frez)
    tekst_frez.pen_color = '#008000'
    Draw(tekst_frez)

    # vektor ubrzanja
    crtaj_vodoravan_vektor(x_f, y_a, (m.f_struje + m.f)*ka, '#ffffff')
    str_a = f'a={abs(m.a):0.2f}m/s²'
    if abs(m.a) < 0.3:
        if m.a > 0:
            str_a += ' (на десно)'
        elif m.a < 0:
            str_a += ' (на лево)'
    tekst_a = Text((x_f, y_a + 2), str_a)
    tekst_a.pen_color = '#ffffff'
    tekst_m = Text((x_f, y_a + 4), f'm={m.brod_masa}Kg')
    Draw(tekst_a, tekst_m)

Run(setup, update, draw)


class Settings():
    """A class to store all settings for Sun Wukong Invasion."""

    def __init__(self):
        """Initialize the game's static settings."""
        # Screen settings
        self.screen_width = 1200
        self.screen_height = 600
        self.bg_color = (230, 230, 230)

        # Sun Wukong settings
        self.lives_limit = 3

        # Bullet settings
        self.bullet_width = 3
        self.bullet_height = 60
        self.bullet_color = 60, 60, 60
        self.bullets_allowed = 3

        # Monster settings
        self.group_move_speed = 10

        # How quickly the game speeds up
        self.speedup_scale = 1.1
        self.initialize_dynamic_settings()

    def initialize_dynamic_settings(self):
        """Initialize settings that change throughout the game."""
        self.sun_wukong_speed_factor = 1.5
        self.bullet_speed_factor = 3

        self.monster_speed_factor = 1

        # group_direction of 1 represents down; -1 represents up.
        self.group_direction = 1

    def increase_speed(self):
        """Increase speed settings."""
        self.sun_wukong_speed_factor *= self.speedup_scale
        self.bullet_speed_factor *= self.speedup_scale
        self.monster_speed_factor *= self.speedup_scale


from flask import Flask, jsonify, request
from flask_restful import Resource, Api
from lib.postcode_validator import Postcode

app = Flask(__name__)

@app.route('/', methods=['GET'])
def index():
    return jsonify({
        'message': 'UK postcode validator.'
    })

@app.route('/<string:s>', methods=['GET'])
def validate_postcode(s=""):

    s = str(s)
    postcode = Postcode()
    is_valid = postcode.validate(s)
    postcode_fmt = postcode.format_code(s)

    return jsonify({
        'is_valid': is_valid,
        'postcode': postcode_fmt,
    })

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


import numpy as np
from src.edit_distance.edit_distance import EditDistance

class StringKNN():

    def compute_distance(self, x_1_list, x_2_list, edit_matrix=None):
        """
        We compute the edit distance between x_1_list and x_2_list

        Args:
            x_1_list (np.ndarray): The list of points
            x_2_list (np.ndarray): The second list of points

        Returns:
            The Edit distance between the points
        """
        self.__distance = EditDistance(
            self._cost_matrix, self._cost_index, self._k_stripe
        )
        if(x_1_list.ndim == 1 and x_2_list.ndim == 1):
            return self.__distance.compute(
                x_1_list, x_2_list, edit_matrix=edit_matrix
            )
        if(x_1_list.ndim == 2 and x_2_list.ndim == 2):
            return self.__distance.compute_list(x_1_list, x_2_list)
        else:
            raise Exception("x_1_list and x_2_list must have the same length")

    def _get_edit_matrix(self):
        return self.__distance.get_edit_matrix()

    def __compute_distance_matrix(
        self, point_1_list, point_2_list
    ):
        """
        We compute the distance matrix

        Args:
            point_1_list (np.ndarray): The dataset of the first ball
            point_2_list (np.ndarray): The dataset of the second ball

        Returns:
            np.ndarray: The distance matrix
        """
        # We initalize the distance matrix
        distance_matrix = np.zeros(
            (len(point_1_list), len(point_2_list)))

        # for each example of the first dataset,
        for i in range(len(point_1_list)):
            # for each of the second dataset,
            for j in range(len(point_2_list)):
                # we compute the distance between the two examples
                distance_matrix[i, j] = self._compute_distance(
                    point_1_list[i], point_2_list[j])

        return distance_matrix


import sys,argparse
import os,glob
import numpy as np
import pandas as pd
from scipy import stats
import re,bisect

# ==== main

slurm_dir = 'run_get_RPKM_slurms'
outdir='rpkm_csv'
os.makedirs(slurm_dir,exist_ok=True)
os.makedirs(outdir,exist_ok=True)

project_dir='/nv/vol190/zanglab/zw5j/since2019_projects/UTX_HaoJiang'
bam_dir='{}/f0_data_process/chip_seq/data_4th_20201220/process_qc_out/'.format(project_dir)
promoter_file='{}/f3_differential_chip_binding/data/hg38_4k_promoter_geneID.bed'.format(project_dir)
udhs_file='{}/f3_differential_chip_binding/data/hg38_unionDHS_fc4_50merge.bed'.format(project_dir)

celltypes = ['PCDH','WT','DEL3','EIF']
chip_markers= ['MLL4GK','P300','UTX4']

for chip_marker in chip_markers:
    for celltype in celltypes:
        basename='{}{}'.format(celltype,chip_marker)
        bamfile='{}/{}/{}_treat.bam'.format(bam_dir,basename,basename)
        if not os.path.isfile(bamfile):
            continue
        print(basename)
        # ==== write the .slurm file
        slurmfile = slurm_dir+os.sep+'run_{}.slurm'.format(basename)
        with open(slurmfile,'w') as slurmout:
            slurmout.write('''#!/bin/bash
#SBATCH -n 1
#SBATCH --mem=20000
#SBATCH -t 12:00:00
#SBATCH -p standard
#SBATCH -A cphg_cz3d
''')

        #######################
        # REMEMBER TO CHANGE THE MEMORY !!!
        #########################

            slurmout.write('#SBATCH -o {}/slurm_{}.out\n\n'.format(slurm_dir,basename))
            slurmout.write('python get_RPM_RPKM_count_on_regions.py \\\n-i {} \\\n-t {} \\\n-s hg38 -f bam -o {}/{}_on_Promoter.csv\n\n'.format(promoter_file,bamfile,outdir,basename))
            slurmout.write('python get_RPM_RPKM_count_on_regions.py \\\n-i {} \\\n-t {} \\\n-s hg38 -f bam -m -e 10000 -o {}/{}_on_Promoter_es10kb.csv\n\n'.format(promoter_file,bamfile,outdir,basename))
            slurmout.write('python get_RPM_RPKM_count_on_regions.py \\\n-i {} \\\n-t {} \\\n-s hg38 -f bam -o {}/{}_on_UDHS.csv\n'.format(udhs_file,bamfile,outdir,basename))


import os
import sys
import random

from math import exp

from pyneat import pyneat

# CLI Arguments
load_save = False
for arg in sys.argv:
    if arg == '--load':
        load_save = True

# Experiment Params
input_nodes = 2
output_nodes = 2

population_size = 150
num_generations = 100

# Population cutoff percentage
cutoff = 0.45

# Percentage chance to mutate:
structure_mutation = 0.025
weight_mutation = 0.75

# Constants used in speciation
c1 = 1.0
c2 = 1.0
c3 = 2.5

# Speciation threshold
sigma_t = 3.0

# Define a fitness function a genome can use to evaluate itself
# Note: this needs to be in scope of a fork.
def fitness_func(self):
    global input_nodes

    # Remember this isn't backprop, the batch
    # is just for evaluating loss
    batch_size = 32

    # Create a batch of random inputs
    vec_in = []
    for _ in range(batch_size):
        x = []
        for _ in range(input_nodes):
            x.append(float(random.randint(0,1)))
        vec_in.append(x)

    # Calculate network outputs
    vec_out = [self.network.forward(x) for x in vec_in]

    # This network will evolve to detect if all inputs are high
    expected = []
    for x in vec_in:
        a = [1.0,0.0] if sum(x) == input_nodes else [0.0,1.0]
        expected.append(a)

    # Fitness = the inverse loss
    mse = []
    for y, y_ in zip(vec_out, expected):
        for i in range(len(y)):
            error = (y[i] - y_[i]) ** 2
            mse.append(error)
    self.fitness = 1 - (sum(mse) / len(mse))

    # In this experiment, we have a quantifiable optimum,
    # thus we should save this particular genome.
    if self.fitness > 0.95:
        print('\n~ found optimal network: {} ~'.format(self.fitness))
        print(self)

        # Create and run 5 tests
        tests = [[random.uniform(0.0,1.0) for _ in range(input_nodes)] for _ in range(5)]
        for x in tests:

            # In this case we reset the network to ignore recurrent connections,
            # we haven't been using them during evolution, thus we won't here.
            self.network.reset()
            y = self.network.forward(x)

            # Simplify the log information
            input_text = 'all active' if sum(x) == input_nodes else 'not active'
            output_text = str([round(x,3) for x in y])[1:-1]
            print("{}\t{}".format(input_text, output_text))

            # Save the genome information
            self.save('saves/top.save')

        # Signal network termination, clean up threads
        return True, self

    # Default returns a non-Terminating signal
    # with the modified object.
    return False, self

# If we haven't already evolved a network
if load_save == False:

    # Make sure we are fork protected
    if __name__ == '__main__':

        # Create the gene pool
        gene_pool = pyneat.GenePool(population_size,
                            num_generations,
                            cutoff,
                            [c1,c2,c3],
                            sigma_t,
                            logging=2,
                            num_threads=8)

        # Initialise the gene pool
        gene_pool.init(input_nodes, output_nodes, fitness_func)

        # Run an evolutionary period, specifying that we have defined
        # a signal to save in the fitness function
        gene_pool.evolve(weight_mutation, structure_mutation, saver=pyneat.Saver.USER_DEFINED)

# If we have already evolved a network
else:
    # Load the genome, extract the network
    net = pyneat.Genome().load('saves/top.save').build().network

    # Run the same tests shown in the fitness function
    tests = [[random.uniform(0.0,1.0) for _ in range(input_nodes)] for _ in range(5)]
    for x in tests:
        # Propagate input throughout the network
        net.reset()
        y = net.forward(x)

        # Simplify the log information
        input_text = 'all active' if sum(x) == input_nodes else 'not active'
        output_text = str([round(x,3) for x in y])[1:-1]
        print("{}\t{}".format(input_text, output_text))


import unittest

import logging
from preparation_and_cleaning import *
from models_preparation import *

class TestAIFunction(unittest.TestCase):
    runtime_uid = None
    deployment_uid = None
    function_uid = None
    scoring_url = None
    logger = logging.getLogger(__name__)

    @classmethod
    def setUpClass(self):
        TestAIFunction.logger.info("Service Instance: setting up credentials")

        self.wml_credentials = get_wml_credentials()
        self.client = get_client()

        self.function_name = 'simplest AI function'
        self.deployment_name = "Test deployment"

    def test_01_service_instance_details(self):
        TestAIFunction.logger.info("Check client ...")
        self.assertTrue(self.client.__class__.__name__ == 'APIClient')

        TestAIFunction.logger.info("Getting instance details ...")
        details = self.client.service_instance.get_details()
        TestAIFunction.logger.debug(details)

        self.assertTrue("published_models" in str(details))
        self.assertEqual(type(details), dict)

    def test_02_create_ai_function(self):

        self.client.repository.FunctionMetaNames.show()

        function_props = {
            self.client.repository.FunctionMetaNames.NAME: 'simplest AI function',
            self.client.repository.FunctionMetaNames.RUNTIME_UID: 'ai-function_0.1-py3.6'
        }

        # def score(payload):
        #     values = [[row[0]*row[1]] for row in payload['values']]
        #     return {'fields': ['multiplication'], 'values': values}

        def score(payload):
            payload = payload['input_data'][0]
            values = [[row[0] * row[1]] for row in payload['values']]
            return {'predictions': [{'fields': ['multiplication'], 'values': values}]}

        ai_function_details = self.client.repository.store_function(score, function_props)

        TestAIFunction.function_uid = self.client.repository.get_function_uid(ai_function_details)
        function_url = self.client.repository.get_function_href(ai_function_details)
        TestAIFunction.logger.info("AI function ID:" + str(TestAIFunction.function_uid))
        TestAIFunction.logger.info("AI function URL:" + str(function_url))
        self.assertIsNotNone(TestAIFunction.function_uid)
        self.assertIsNotNone(function_url)
        TestAIFunction.runtime_uid = self.client.runtimes.get_uid(ai_function_details)

    def test_03_update_function(self):
        function_props = {
            self.client.repository.FunctionMetaNames.NAME: 'simplest AI function'
        }

        details = self.client.repository.update_function(TestAIFunction.function_uid, function_props)
        self.assertFalse('xxxx' in json.dumps(details))

    def test_04_download_ai_function_content(self):
        try:
            import os
            os.remove('test_ai_function_v41.gz')
        except:
            pass
        self.client.repository.download(TestAIFunction.function_uid, filename='test_ai_function_v41.gz')
        try:
            os.remove('test_ai_function_v41.gz')
        except:
            pass

    def test_05_get_details(self):

        details = self.client.repository.get_function_details(self.function_uid)
        self.assertTrue(self.function_name in str(details))

    def test_06_list(self):
        self.client.repository.list()

        self.client.repository.list_functions()

    def test_07_create_deployment(self):
        deploy_meta = {
            self.client.deployments.ConfigurationMetaNames.NAME: "deployment_Function",
            self.client.deployments.ConfigurationMetaNames.DESCRIPTION: "deployment_description",
            self.client.deployments.ConfigurationMetaNames.ONLINE: {}
        }

        TestAIFunction.logger.info("Create deployment")
        deployment = self.client.deployments.create(artifact_uid=TestAIFunction.function_uid, meta_props=deploy_meta, asynchronous=False)
        TestAIFunction.logger.debug("deployment: " + str(deployment))
        TestAIFunction.scoring_url = self.client.deployments.get_scoring_href(deployment)
        TestAIFunction.logger.debug("Scoring href: {}".format(TestAIFunction.scoring_url))
        TestAIFunction.deployment_uid = self.client.deployments.get_uid(deployment)
        TestAIFunction.logger.debug("Deployment uid: {}".format(TestAIFunction.deployment_uid))
        self.client.deployments.list()
        self.assertTrue("online" in str(deployment))

    def test_08_update_deployment(self):
        patch_meta = {
            self.client.deployments.ConfigurationMetaNames.DESCRIPTION: "deployment_Updated_Function_Description",
            self.client.deployments.ConfigurationMetaNames.NAME : "deployment_Function"
        }
        self.client.deployments.update(TestAIFunction.deployment_uid, patch_meta)

    def test_09_get_deployment_details(self):
        TestAIFunction.logger.info("Get deployment details")
        deployment_details = self.client.deployments.get_details()
        TestAIFunction.logger.debug("Deployment details: {}".format(deployment_details))
        self.assertTrue('deployment_Function' in str(deployment_details))

    def test_10_score(self):
            scoring_payload = {
                self.client.deployments.ScoringMetaNames.INPUT_DATA: [{
                    "fields": ["multiplication"],
                    "values": [[2.0, 2.0], [99.0, 99.0]]
                }
                ]
            }
            predictions = self.client.deployments.score(TestAIFunction.deployment_uid, scoring_payload)
            print("Predictions: {}".format(predictions))
            self.assertTrue("values" in str(predictions))

    def test_11_delete_deployment(self):
        TestAIFunction.logger.info("Delete deployment")
        self.client.deployments.delete(TestAIFunction.deployment_uid)

    def test_12_delete_function(self):
        TestAIFunction.logger.info("Delete function")
        self.client.repository.delete(TestAIFunction.function_uid)

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


from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
import qgis.utils
import xml.etree.ElementTree as ET
from ...tools import Tools

class LoadShapefile():

    #####################
    def __init__(self):
        self.title = ""
        self.directory = ""
        self.shapefile = ""
        self.layerfile = ""
        self.caveat = ""

####################
    def isValid(self):
        """ Validates the LoadShapefile. """
        if (self.title is None or self.shapefile is None or self.directory is None):
            return False
        if (len(self.title) > 0 and len(self.shapefile) > 0 and len(self.directory) > 0):
            return True
        else:
            return False

#####################
    def doLoad(self):
        # Loads shapefile and attaches resultant layer to QgsMapLayerRegistry.
        directory = Tools.endWithSlash(self.directory)
        shpLocation = directory + self.shapefile
        title = Tools.substituteRegion(self.title, " ")
        layer = LoadShapefile.loadShapefile(shpLocation, title)
        if (layer is not None):
            QgsMapLayerRegistry.instance().addMapLayer(layer)
            if self.caveat != "" and self.caveat is not None:
                Tools.alert(self.caveat, "Caveat")
            return True
        else:
            return False

#########################################
    @staticmethod
    def loadShapefile(filePath, title=""):
        """ Loads a shapefile from disk and returns a QgsVectorLayer containing it. """
        assert isinstance(filePath, str), "Bad Parameter"
        assert isinstance(title, str), "Bad Parameter"

        if title == "":
            title = Tools.getFilenameFromPath(filePath)

        shpLocation = Tools.findData(filePath)
        if (shpLocation != ""):
            title = Tools.substituteRegion(title, " ")
            layer = QgsVectorLayer(shpLocation, title, "ogr")
            if layer.isValid():
                return layer
            else:
                Tools.logError("Invalid data found at " + shpLocation)
                return None
        else:
            Tools.logError("Unable to locate " + title)
            return None

##############################################
    @staticmethod
    def parseXML(XMLMenuElement):
        """ Create a LoadShapefile from data in the XMLMenuElement. """
        assert isinstance(XMLMenuElement, ET.Element), "Bad Parameter"

        loadShapefile = LoadShapefile()
        loadShapefile.title = Tools.getAttributeFromElement(XMLMenuElement, "title")
        loadShapefile.directory = Tools.getAttributeFromElement(XMLMenuElement, "directory")
        loadShapefile.shapefile = Tools.getAttributeFromElement(XMLMenuElement, "shapefile")
        loadShapefile.layerfile = Tools.getAttributeFromElement(XMLMenuElement, "layerfile")
        loadShapefile.caveat = Tools.getAttributeFromElement(XMLMenuElement, "caveat")
        if loadShapefile.isValid():
            return loadShapefile
        else:
            return None


# -*- coding:utf-8 -*-
# @Time  : 2019/4/22 3:17
# @Author: xiaoxiao
# @File  : regulax.py

import re
from class_0419.common.my_config import config
def Regulax(data,p=None):
    if not p:
        p="#(.*?)#"
    while re.search(p,data):
        result=re.search(p,data).group(1)
        params=config.get("member",result)
        data=re.sub(p,params,data,count=1)
    return data


import pytest

from xalbrain import (
    CardiacModel,
    NoCellModel,
    BasicSplittingSolver,
)

from dolfin import (
    Constant,
    UnitSquareMesh,
    Function,
    Expression,
    errornorm,
)

from xalbrain.utils import (
    convergence_rate,
)

from typing import (
    Tuple,
)

def main(N: int, dt: float, T: float, theta: float) -> \
         Tuple[float, float, float, float]:

    mesh = UnitSquareMesh(N, N)
    time = Constant(0.0)
    lam = Constant(1.0)
    cell_model = NoCellModel()

    ac_str = "(8*pi*pi*lam*sin(t) + (lam + 1)*cos(t))*cos(2*pi*x[0])*cos(2*pi*x[1])/(lam + 1)"
    stimulus = Expression(ac_str, t=time, lam=lam, degree=3)
    brain = CardiacModel(mesh, time, 1.0, 1.0, cell_model, stimulus=stimulus)

    # Define solver solver

    ps = BasicSplittingSolver.default_parameters()
    ps["theta"] = theta
    ps["pde_solver"] = "monodomain"
    ps["BasicMonodomainSolver"]["linear_variational_solver"]["linear_solver"] = "direct"
    solver = BasicSplittingSolver(brain, params=ps)

    vs0 = Function(solver.VS)
    vs_, vs, vur = solver.solution_fields()
    vs_.assign(vs0)

    solutions = solver.solve((0, T), dt)
    for timestep, (vs_, vs, vur) in solutions:
        continue

    v_exact = Expression(
        "sin(t)*cos(2*pi*x[0])*cos(2*pi*x[1])",
        t=T,
        degree=3
    )

    # compute errors
    v, s = vs.split(deepcopy=True)
    v_error = errornorm(v_exact, v, "L2", degree_rise=2)

    return (v_error, mesh.hmin(), dt, T)

def test_analytic_mondomain() -> None:
    """Test errors vs reference"""
    N = 20
    dt = 1e-6
    T = 10*dt

    v_error, h, dt, T = main(N, dt, T, 0.5)
    v_reference = 4.203505851866419e-08
    v_diff = abs(v_error - v_reference)
    tol = 1e-9

    print("v_diff: {}".format(v_diff))

    assert v_diff < tol, "Failed error for u, diff: {}".format(v_diff)

def test_spatial_and_temporal_convergence() -> None:
    """Test convergence rates for bidomain solver."""
    v_errors = []
    dts = []
    hs = []

    dt = 1e-3
    theta = 0.5
    N = 5

    for level in range(3):
        a = dt/(2**level)
        T = 10*a
        v_error, h, a, T = main(N*(2**level), a, T, theta)
        v_errors.append(v_error)
        dts.append(a)
        hs.append(h)

    v_rates = convergence_rate(hs, v_errors)
    print("v_errors = {}".format(v_errors))
    print("v_rates = {}".format(v_rates))

    assert all(v > 1.9 for v in v_rates), "Failed convergence for v"

if __name__ == "__main__":
    args = {
        "N": 10,
        "dt": 5e-3,
        "T": 1,
        "theta": 1.0,
    }

    # main(**args)
    # test_analytic_mondomain()
    test_spatial_and_temporal_convergence()


#In mathematics, the factorial of integer 'n' is written as 'n!'. It is equal to
# the product of n and every integer preceding it. For example: 5! = 1 x 2 x 3 x
# 4 x 5 = 120

#Your mission is simple: write a function that takes an integer 'n' and returns
 #'n!'.

#You are guaranteed an integer argument. For any values outside the positive
# range, return null, nil or None .
# This function should return n!

def factorial(n):
    result = 1
    if n < 0:
        return None
    for x in range(n+1):
        if x !=0:
            result *= x
    return result

#Finish the solution so that it sorts the passed in array of numbers. If the
#function passes in an empty array or null/nil value then it should return an
#empty array

#simple bubblw sort
def solution(nums):
    if nums == [] or nums == None:
        return []
    for i in range(len(nums)):
        for j in range(len(nums)-1-i):
            if nums[j] > nums[j+1]:
                nums[j], nums[j+1] = nums[j+1], nums[j]
    return nums


import sys

def MergerResult(num):
	resultMap = {}
	for i in range(num):
		fr = open("../output/result" + str(i), "r")
		lines = fr.readlines()
		fr.close()
		for line in lines:
			ps = line.split(" | ")
			if len(ps) == 2:
				resultMap[ps[0].strip()] = float(ps[1].strip())

	sortedIP = sorted(resultMap, key = resultMap.get)
	fw = open("../output/ip.result", "w")
	for ip in sortedIP:
		fw.write("{0}\n".format(ip))
	fw.close()

if (__name__ == '__main__'):
	MergerResult(4)


import re

# worse performance but to show power of function
def find_item(hero) :
    with open('data/one_piece_file.txt', encoding='utf-8') as f:
        text = f.read().replace('\n', '')
        return len(re.findall(hero, text))

# read charactor name
name_dict = {}
with open('data/one_piece_charactor_list.txt') as f:
    for line in f:
        names = line.split('|')
        for n in names:
            name_num = find_item(n)
            name_dict[n] = name_num

name_sorted = sorted(name_dict.items(), key=lambda item: item[1], reverse=True)

print(name_sorted)