from tfc import utfc
from tfc.utils import TFCDictRobust, egrad, NllsClass, MakePlot

import numpy as onp
import jax.numpy as np
from jax import vmap, jacfwd, jit, lax

import tqdm
import pickle

from scipy.optimize import fsolve
from time import process_time as timer

## TEST PARAMETERS: ***************************************************
tol = np.finfo(float).eps
maxIter = 50

## CONSTANTS: *********************************************************
# Number of points to use
N = 100

# Number of basis functions to use
ms = 30
mc = 1

# Number of constraints
nCx = 0
nCy = 0

## GET CHEBYSHEV VALUES **********************************************
stfc = utfc(N,nCx,ms,basis='CP',x0 = -1, xf = 1.)
ctfc = utfc(N,nCy,mc,basis='CP',x0 = -1, xf = 1.)

Hs  = stfc.H
Hc  = ctfc.H

## DEFINE THE ASSUMED SOLUTION **************************************
z = stfc.z
z0 = z[0]
zf = z[-1]

## DEFINE CONSTRAINED EXPRESSION *************************************
r = lambda z, xi, IC: np.dot(Hs(z),xi['xis'])
v = egrad(r,0)
a = egrad(v,0)

lam = lambda z, xi: np.dot(Hc(z),xi['xic'])
lamr = egrad(lam,0)

## FORM LOSS AND JACOBIAN ***********************************************************************************
L0  = lambda xi,IC: r(z,xi,IC)[0,:] - IC['R0']
Ld0 = lambda xi,IC: xi['b']**2 * v(z,xi,IC)[0,:] - IC['V0']
Lf  = lambda xi,IC: r(z,xi,IC)[-1,:]
Ldf = lambda xi,IC: xi['b']**2 * v(z,xi,IC)[-1,:]

Ls  = lambda xi,IC: xi['b']**4 * a(z,xi,IC) - IC['ag'] + lam(z,xi)

# Htf = lambda xi,IC: np.dot(lam(z,xi)[-1,:],(-1./2.*lam(z,xi)[-1,:] + IC['ag']))
# Updated because need to at lam_r * v term for spectral method
Htf = lambda xi,IC: np.dot(lam(z,xi)[-1,:],(-1./2.*lam(z,xi)[-1,:] + IC['ag'])) \
                  + np.dot(-xi['b']**2 *lamr(z,xi)[-1,:], xi['b']**2 * v(z,xi,IC)[-1,:])

L = jit(lambda xi,IC: np.hstack([Ls(xi,IC)[1:-1,:].flatten(), \
                                 L0(xi,IC).flatten(), \
                                 Ld0(xi,IC).flatten(), \
                                 Lf(xi,IC).flatten(), \
                                 Ldf(xi,IC).flatten(), \
                                 Htf(xi,IC)] ))

## INITIALIZE VARIABLES *************************************************************************************
xis   = onp.zeros((Hs(z).shape[1],3))
xic  = onp.zeros((Hc(z).shape[1],3))
b    = np.sqrt(2)*onp.ones(1)

xi = TFCDictRobust({'xis':xis,\
                    'xic':xic,\
                    'b':b})

IC = {'R0': np.zeros((3,)), 'V0': np.zeros((3,)), 'ag': np.zeros((3,))}

## NONLINEAR LEAST-SQUARES CLASS *****************************************************************************
nlls = NllsClass(xi,L,IC,tol=tol,maxIter=maxIter,timer=True)

data = pickle.load(open('data/EOL_IC.pickle','rb'))
sol = {'loss': onp.zeros((data['R0'].shape[0])), 'it': onp.zeros((data['R0'].shape[0])), 'time': onp.zeros((data['R0'].shape[0]))}
## RUN TEST *************************************************************************************************
for i in tqdm.trange(data['R0'].shape[0]):
    R0 = data['R0'][i,:]
    V0 = data['V0'][i,:]

    ## scale initial conditons
    pscale = np.max(np.abs(R0))
    tscale = pscale/np.max(np.abs(V0))

    xi = TFCDictRobust({'xis':onp.zeros((Hs(z).shape[1],3)),\
                        'xic':onp.array([0.5 * (V0 - R0), -0.5*(V0 + R0)]),\
                        'b':np.sqrt(10.)*onp.ones(1)})

    IC['R0']    = R0 / pscale
    IC['V0']    = V0 * tscale/pscale
    IC['ag']    = np.array([0., 0., -1.62]) * tscale**2/pscale

    xi,it,time = nlls.run(xi,IC)

    sol['loss'][i] = np.max(np.abs(L(xi,IC)))
    sol['it'][i]   = it
    sol['time'][i] = time

## END: **************************************************************
# with open('data/EOL_Spec.pickle', 'wb') as handle:
#     pickle.dump(sol, handle)


import findspark
from pyspark.sql import SparkSession
import sys

# The entry point into all functionality in Spark is the SparkSession class.
spark = (SparkSession
        .builder
        .appName("my awesome Spark SQL program")
        .master("spark://172.31.93.203:7077")
        .getOrCreate())

# You can read the data from a file into DataFrames
# df = spark.read.json("")
input_path = "hdfs://172.31.93.203:9000/" + sys.argv[1]
df2 = spark.read.option("header","true").option("delimiter", ",").csv(input_path)

# sorting as required
answer = df2.sort(["cca2", "timestamp"])

answer.write.format("csv").mode("overwrite").save("hdfs://172.31.93.203:9000/" + sys.argv[2])


import torch
torch.manual_seed(1741)
import random
random.seed(1741)
import numpy as np
np.random.seed(1741)
from collections import defaultdict
import pickle
import os
import tqdm
from itertools import combinations
from data_loader.reader import mulerx_tsvx_reader, tbd_tml_reader, tdd_tml_reader, tml_reader, tsvx_reader
from utils.tools import create_target, padding, pos_to_id
from sklearn.model_selection import train_test_split
from utils.SentenceEncoder import SentenceEncoder
import gc

class Reader(object):
    def __init__(self, type) -> None:
        super().__init__()
        self.type = type

    def read(self, dir_name, file_name):
        if self.type == 'tsvx':
            return tsvx_reader(dir_name, file_name)
        elif self.type == 'tml':
            return tml_reader(dir_name, file_name)
        elif self.type == 'mulerx':
            return mulerx_tsvx_reader(dir_name, file_name)
        elif self.type == 'tbd_tml':
            return tbd_tml_reader(dir_name, file_name)
        elif self.type == 'tdd_man':
            return tdd_tml_reader(dir_name, file_name, type_doc='man')
        elif self.type == 'tdd_auto':
            return tdd_tml_reader(dir_name, file_name, type_doc='auto')
        else:
            raise ValueError("We have not supported {} type yet!".format(self.type))

class C2V(object):
    def __init__(self, emb_file:str) -> None:
        super().__init__()
        with open(emb_file, 'r', encoding='UTF-8') as f:
            lines = f.readlines()
        self.c2v = {}
        for line in lines:
            tokens = line.split(" ")
            concept = tokens[0]
            emb = [float(tok) for tok in tokens[1:]]
            self.c2v[concept] = emb

    def get_emb(self, concept):
        _concept = "_".join(concept.split(" ")).lower()
        # print(cp)
        try:
            return self.c2v[_concept]
        except:
            return [0.0]*300

def load_dataset(dir_name, type):
    reader = Reader(type)
    onlyfiles = [f for f in os.listdir(dir_name) if os.path.isfile(os.path.join(dir_name, f))]
    corpus = []
    # i = 0
    for file_name in tqdm.tqdm(onlyfiles):
        # if i == 1:
        #     break
        # i = i + 1
        if type == 'i2b2_xml':
            if file_name.endswith('.xml'):
                my_dict = reader.read(dir_name, file_name)
                if my_dict != None:
                    corpus.append(my_dict)
        else:
            my_dict = reader.read(dir_name, file_name)
            if my_dict != None:
                corpus.append(my_dict)
    return corpus

global_sent_encoder = SentenceEncoder('roberta-base')
global_c2v = C2V('./datasets/numberbatch-en-19.08.txt')

def loader(dataset, min_ns, file_type=None, file_path=None, label_type=None):
    sent_encoder = global_sent_encoder
    c2v = global_c2v
    def get_data_point(my_dict, flag):
        data = []
        eids = my_dict['event_dict'].keys()
        pair_events = list(combinations(eids, 2))

        ctx_id_augm = []
        for pair in pair_events:
            x, y = pair

            x_sent_id = my_dict['event_dict'][x]['sent_id']
            y_sent_id = my_dict['event_dict'][y]['sent_id']
            ctx_id = list(range(len( my_dict["sentences"])))
            if  x_sent_id != y_sent_id:
                ctx_id.remove(x_sent_id)
                ctx_id.remove(y_sent_id)
            else:
                ctx_id.remove(x_sent_id)
            id_augm = [tuple(sorted([x_sent_id, y_sent_id, id])) for id in ctx_id]
            ctx_id_augm.extend(id_augm)
        ctx_id_augm = list(set(ctx_id_augm))
        # print(len(ctx_id_augm))

        ctx_augm = []
        ctx_augm_mask = []
        _augm_emb = []
        for ids in ctx_id_augm:
            ids = set(ids)
            sent = []
            for id in ids:
                sent = sent + my_dict["sentences"][id]["roberta_subword_to_ID"][1:]
            sent = [0] + sent
            pad, mask = padding(sent, max_sent_len=512)
            _augm_emb.append(sent_encoder.encode(pad, mask, is_ctx=True))
            ctx_augm.append(pad)
            ctx_augm_mask.append(mask)
        _augm_emb = torch.cat(_augm_emb, dim=0)
        if len(ctx_augm) == 0:
            _augm_emb = []
        _ctx_augm_emb = {}
        for i in range(len(ctx_id_augm)):
            _ctx_augm_emb[ctx_id_augm[i]] = _augm_emb[i]

        doc = []
        doc_mask = []
        doc_emb = []
        for sent_id in list(range(len( my_dict["sentences"]))):
            sent = my_dict["sentences"][sent_id]["roberta_subword_to_ID"]
            pad, mask = padding(sent, max_sent_len=512)
            doc_emb.append(sent_encoder.encode(pad, mask))
            doc.append(pad)
            doc_mask.append(mask)
        doc_emb = torch.cat(doc_emb, dim=0)

        sent_ev = defaultdict(list)
        sent_ev_ids = defaultdict(list)
        ev_kg_emb = {}
        # print(eids)
        for eid in eids:
            sent_id = my_dict['event_dict'][eid]['sent_id']
            e_possition = my_dict["event_dict"][eid]["roberta_subword_id"]
            sent_ev[sent_id].append(e_possition)
            sent_ev_ids[sent_id].append(eid)
            mention = my_dict["event_dict"][eid]["mention"] ######
            # print(mention)
            kg_emb = c2v.get_emb(mention)
            # print(kg_emb)
            ev_kg_emb[eid] = kg_emb
        sent_ev = dict(sent_ev)
        sent_ev_ids = dict(sent_ev_ids)
        # print(ev_kg_emb)

        for pair in pair_events:
            x, y = pair

            x_sent_id = my_dict['event_dict'][x]['sent_id']
            y_sent_id = my_dict['event_dict'][y]['sent_id']

            x_sent = my_dict["sentences"][x_sent_id]["roberta_subword_to_ID"]
            y_sent = my_dict["sentences"][y_sent_id]["roberta_subword_to_ID"]

            x_position = my_dict["event_dict"][x]["roberta_subword_id"]
            y_position = my_dict["event_dict"][y]["roberta_subword_id"]

            x_sent_pos = pos_to_id(my_dict["sentences"][x_sent_id]["roberta_subword_pos"])
            y_sent_pos = pos_to_id(my_dict["sentences"][y_sent_id]["roberta_subword_pos"])

            target, x_position_new, y_position_new = create_target(x_sent, y_sent, x_sent_id, y_sent_id, x_position, y_position)
            target_encode = sent_encoder.encode(target)
            target_emb = target_encode[:, 0].squeeze()
            target_len = len(target)

            x_ev_embs = target_encode[:, x_position_new].squeeze()
            y_ev_embs = target_encode[:, y_position_new].squeeze()

            x_kg_ev_emb = torch.tensor(ev_kg_emb[x])
            y_kg_ev_emb = torch.tensor(ev_kg_emb[y])
            # print(x_kg_ev_emb)

            ctx = []
            _ctx_emb = []
            ctx_pos = []
            ctx_len = []
            _ctx_ev_embs = []
            _ctx_ev_kg_embs = []
            ctx_id = list(range(len( my_dict["sentences"])))
            num_ev_sents = []
            if  x_sent_id != y_sent_id:
                ctx_id.remove(x_sent_id)
                ctx_id.remove(y_sent_id)
            else:
                ctx_id.remove(x_sent_id)
            id_mapping = {}
            if len(ctx_id) != 0:
                i = 0
                for sent_id in ctx_id:
                    id_mapping[i] = sent_id
                    sent = my_dict["sentences"][sent_id]['roberta_subword_to_ID']
                    sent_pos = pos_to_id(my_dict["sentences"][sent_id]['roberta_subword_pos'])
                    ctx.append(sent)
                    ctx_pos.append(sent_pos)
                    ctx_len.append(len(sent))
                    sent_emb = _ctx_augm_emb[tuple(sorted([x_sent_id, y_sent_id, sent_id]))]
                    assert sent_emb != None
                    _ctx_emb.append(sent_emb)
                    e_possitions = sent_ev.get(sent_id)
                    if e_possitions == None:
                        e_possitions = []
                    num_ev_sents.append(len(e_possitions))
                    if len(e_possitions) != 0:
                        ev_embs = torch.max(doc_emb[sent_id, e_possitions, :], dim=0)[0] # 768
                        _ctx_ev_embs.append(ev_embs)

                    eids = sent_ev_ids.get(sent_id)
                    if eids == None:
                        eids = []
                    # print(eids)
                    if len(eids) != 0:
                        sent_ev_kg_emb = [ev_kg_emb[eid] for eid in eids]
                        # print("before: ", torch.tensor(sent_ev_kg_emb))
                        sent_ev_kg_emb = torch.max(torch.tensor(sent_ev_kg_emb), dim=0)[0] # 300
                        # print("max: ", sent_ev_kg_emb)
                        _ctx_ev_kg_embs.append(sent_ev_kg_emb)
                    else:
                        # print("Sent no ev")
                        _ctx_ev_embs.append(torch.ones(768)*-1000.0)
                        _ctx_ev_kg_embs.append(torch.ones(300)*-1000.0)
                    i = i + 1
                # print(ctx_ev_kg_embs)
                ctx_ev_kg_embs =torch.stack(_ctx_ev_kg_embs, dim=0)
                ctx_ev_embs = torch.stack(_ctx_ev_embs, dim=0)
                ctx_emb = torch.stack(_ctx_emb, dim=0) # ns x 768
            # print(ctx_emb.size())
            xy = my_dict["relation_dict"].get((x, y))
            yx = my_dict["relation_dict"].get((y, x))

            candidates = [
                [str(x), str(y), x_sent, y_sent, x_sent_id, y_sent_id, x_sent_pos, y_sent_pos, x_position, y_position, x_ev_embs, y_ev_embs, x_kg_ev_emb,
                y_kg_ev_emb, id_mapping, target, target_emb, target_len, ctx, ctx_emb, ctx_ev_embs, num_ev_sents, ctx_ev_kg_embs, ctx_len, ctx_pos, flag, xy],
                [str(y), str(x), y_sent, x_sent, y_sent_id, x_sent_id, y_sent_pos, x_sent_pos, y_position, x_position, y_ev_embs, x_ev_embs, y_kg_ev_emb,
                x_kg_ev_emb, id_mapping, target, target_emb, target_len, ctx, ctx_emb, ctx_ev_embs, num_ev_sents, ctx_ev_kg_embs, ctx_len, ctx_pos, flag, yx],
            ]
            for item in candidates:
                if item[-1] != None:
                    data.append(item)
        return data

    train_set = []
    train_short = []
    test_set = []
    test_short = []
    validate_set = []
    validate_short = []
    if dataset == "MATRES":
        print("MATRES Loading .......")
        aquaint_dir_name = "./datasets/MATRES/TBAQ-cleaned/AQUAINT/"
        timebank_dir_name = "./datasets/MATRES/TBAQ-cleaned/TimeBank/"
        platinum_dir_name = "./datasets/MATRES/te3-platinum/"
        validate = load_dataset(aquaint_dir_name, 'tml')
        train = load_dataset(timebank_dir_name, 'tml')
        test = load_dataset(platinum_dir_name, 'tml')
        _tt = train + validate
        _tt = list(sorted(_tt, key=lambda x: x["doc_id"]))
        train, validate = train_test_split(_tt, test_size=0.1, train_size=0.9)

        processed_dir = "./datasets/MATRES/docEvR_processed_kg/"
        if not os.path.exists(processed_dir):
            os.mkdir(processed_dir)

        for my_dict in tqdm.tqdm(train):
            file_name = my_dict["doc_id"] + ".pkl"
            if not os.path.exists(processed_dir+file_name):
                data = get_data_point(my_dict, 2)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            else:
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            for item in data:
                if len(item[-4]) >= min_ns:
                    train_set.append(item)
                if len(item[-4]) < min_ns:
                    train_short.append(item)

        for my_dict in tqdm.tqdm(validate):
            file_name = my_dict["doc_id"] + ".pkl"
            if not os.path.exists(processed_dir+file_name):
                data = get_data_point(my_dict, 2)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            else:
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            for item in data:
                if len(item[-4]) >= min_ns:
                    validate_set.append(item)
                if len(item[-4]) < min_ns:
                    validate_short.append(item)

        for my_dict in tqdm.tqdm(test):
            file_name = my_dict["doc_id"] + ".pkl"
            if not os.path.exists(processed_dir+file_name):
                data = get_data_point(my_dict, 2)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            else:
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            for item in data:
                if len(item[-4]) >= min_ns:
                    test_set.append(item)
                if len(item[-4]) < min_ns:
                    test_short.append(item)

        print("Train_size: {}".format(len(train_set) + len(train_short)))
        print("Test_size: {}".format(len(test_set) + len(test_short)))
        print("Validate_size: {}".format(len(validate_set) + len(validate_short)))

    if dataset == "HiEve":
        print("HiEve Loading .....")
        dir_name = "./datasets/hievents_v2/processed/"
        corpus = load_dataset(dir_name, 'tsvx')
        corpus = list(sorted(corpus, key=lambda x: x["doc_id"]))
        train, test = train_test_split(corpus, train_size=0.8, test_size=0.2)
        train, validate = train_test_split(train, train_size=0.75, test_size=0.25)
        sample = 0.4

        processed_dir = "./datasets/hievents_v2/processed/docEvR_processed_kg/"
        if not os.path.exists(processed_dir):
            os.mkdir(processed_dir)

        for my_dict in tqdm.tqdm(train):
            file_name = my_dict["doc_id"] + ".pkl"
            if os.path.exists(processed_dir+file_name):
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            else:
                data = get_data_point(my_dict, 1)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            for item in data:
                if item[-1] == 3:
                    if random.uniform(0, 1) < sample:
                        if len(item[-4]) >= min_ns:
                            train_set.append(item)
                        else:
                            train_short.append(item)
                else:
                    if len(item[-4]) >= min_ns:
                            train_set.append(item)
                    else:
                        train_short.append(item)

        for my_dict in tqdm.tqdm(test):
            file_name = my_dict["doc_id"] + ".pkl"
            if os.path.exists(processed_dir+file_name):
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            else:
                data = get_data_point(my_dict, 1)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            for item in data:
                if item[-1] == 3:
                    if random.uniform(0, 1) < sample:
                        if len(item[-4]) >= min_ns:
                            test_set.append(item)
                        else:
                            test_short.append(item)
                else:
                    if len(item[-4]) >= min_ns:
                            test_set.append(item)
                    else:
                        test_short.append(item)

        for my_dict in tqdm.tqdm(validate):
            file_name = my_dict["doc_id"] + ".pkl"
            if os.path.exists(processed_dir+file_name):
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            else:
                data = get_data_point(my_dict, 1)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            for item in data:
                if item[-1] == 3:
                    if random.uniform(0, 1) < sample:
                        if len(item[-4]) >= min_ns:
                            validate_set.append(item)
                        else:
                            validate_short.append(item)
                else:
                    if len(item[-4]) >= min_ns:
                            validate_set.append(item)
                    else:
                        validate_short.append(item)

        print("Train_size: {}".format(len(train_set) + len(train_short)))
        print("Test_size: {}".format(len(test_set) + len(test_short)))
        print("Validate_size: {}".format(len(validate_set) + len(validate_short)))

    if dataset == 'TBD':
        print("Timebank Dense Loading .....")
        train_dir = "./datasets/TimeBank-dense/train/"
        test_dir = "./datasets/TimeBank-dense/test/"
        validate_dir = "./datasets/TimeBank-dense/dev/"
        train = load_dataset(train_dir, 'tbd_tml')
        test = load_dataset(test_dir, 'tbd_tml')
        validate = load_dataset(validate_dir, 'tbd_tml')
        _tt = train + validate
        _tt = list(sorted(_tt, key=lambda x: x["doc_id"]))
        train, validate = train_test_split(_tt, test_size=0.1, train_size=0.9)

        processed_dir = "./datasets/TimeBank-dense/docEvR_processed_kg/"
        if not os.path.exists(processed_dir):
            os.mkdir(processed_dir)

        for my_dict in tqdm.tqdm(train):
            file_name = my_dict["doc_id"] + ".pkl"
            if not os.path.exists(processed_dir+file_name):
                data = get_data_point(my_dict, 4)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            else:
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            for item in data:
                if len(item[-4]) >= min_ns:
                    train_set.append(item)
                if len(item[-4]) < min_ns:
                    train_short.append(item)

        for my_dict in tqdm.tqdm(test):
            file_name = my_dict["doc_id"] + ".pkl"
            if not os.path.exists(processed_dir+file_name):
                data = get_data_point(my_dict, 4)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            else:
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            for item in data:
                if len(item[-4]) >= min_ns:
                    test_set.append(item)
                if len(item[-4]) < min_ns:
                    test_short.append(item)

        for my_dict in tqdm.tqdm(validate):
            file_name = my_dict["doc_id"] + ".pkl"
            if not os.path.exists(processed_dir+file_name):
                data = get_data_point(my_dict, 4)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            else:
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            for item in data:
                if len(item[-4]) >= min_ns:
                    validate_set.append(item)
                if len(item[-4]) < min_ns:
                    validate_short.append(item)

        print("Train_size: {}".format(len(train_set) + len(train_short)))
        print("Test_size: {}".format(len(test_set) + len(test_short)))
        print("Validate_size: {}".format(len(validate_set) + len(validate_short)))

    if dataset == 'TDD_man':
        print("TDD_man Loading .....")
        train_dir = "./datasets/TimeBank-dense/train/"
        test_dir = "./datasets/TimeBank-dense/test/"
        validate_dir = "./datasets/TimeBank-dense/dev/"
        train = load_dataset(train_dir, 'tdd_man')
        test = load_dataset(test_dir, 'tdd_man')
        validate = load_dataset(validate_dir, 'tdd_man')
        # _tt = train + validate
        # _tt = list(sorted(_tt, key=lambda x: x["doc_id"]))
        # train, validate = train_test_split(_tt, test_size=0.1, train_size=0.9)

        processed_dir = "./datasets/TDDiscourse/TDDMan/docEvR_processed_kg/"
        if not os.path.exists(processed_dir):
            os.mkdir(processed_dir)

        for my_dict in tqdm.tqdm(train):
            file_name = my_dict["doc_id"] + ".pkl"
            if not os.path.exists(processed_dir+file_name):
                data = get_data_point(my_dict, 5)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            else:
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            for item in data:
                if len(item[-4]) >= min_ns:
                    train_set.append(item)
                if len(item[-4]) < min_ns:
                    train_short.append(item)

        for my_dict in tqdm.tqdm(test):
            file_name = my_dict["doc_id"] + ".pkl"
            if not os.path.exists(processed_dir+file_name):
                data = get_data_point(my_dict, 5)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            else:
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            for item in data:
                if len(item[-4]) >= min_ns:
                    test_set.append(item)
                if len(item[-4]) < min_ns:
                    test_short.append(item)

        for my_dict in tqdm.tqdm(validate):
            file_name = my_dict["doc_id"] + ".pkl"
            if not os.path.exists(processed_dir+file_name):
                data = get_data_point(my_dict, 5)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            else:
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            for item in data:
                if len(item[-4]) >= min_ns:
                    validate_set.append(item)
                if len(item[-4]) < min_ns:
                    validate_short.append(item)

        print("Train_size: {}".format(len(train_set) + len(train_short)))
        print("Test_size: {}".format(len(test_set) + len(test_short)))
        print("Validate_size: {}".format(len(validate_set) + len(validate_short)))

    if dataset == 'TDD_auto':
        print("TDD_auto Loading .....")
        train_dir = "./datasets/TimeBank-dense/train/"
        test_dir = "./datasets/TimeBank-dense/test/"
        validate_dir = "./datasets/TimeBank-dense/dev/"
        train = load_dataset(train_dir, 'tdd_auto')
        test = load_dataset(test_dir, 'tdd_auto')
        validate = load_dataset(validate_dir, 'tdd_auto')
        # _tt = train + validate
        # _tt = list(sorted(_tt, key=lambda x: x["doc_id"]))
        # train, validate = train_test_split(_tt, test_size=0.1, train_size=0.9)

        processed_dir = "./datasets/TDDiscourse/TDDAuto/docEvR_processed_kg/"
        if not os.path.exists(processed_dir):
            os.mkdir(processed_dir)

        for my_dict in tqdm.tqdm(train):
            file_name = my_dict["doc_id"] + ".pkl"
            if not os.path.exists(processed_dir+file_name):
                data = get_data_point(my_dict, 5)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            else:
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            for item in data:
                if len(item[-4]) >= min_ns:
                    train_set.append(item)
                if len(item[-4]) < min_ns:
                    train_short.append(item)

        for my_dict in tqdm.tqdm(test):
            file_name = my_dict["doc_id"] + ".pkl"
            if not os.path.exists(processed_dir+file_name):
                data = get_data_point(my_dict, 5)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            else:
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            for item in data:
                if len(item[-4]) >= min_ns:
                    test_set.append(item)
                if len(item[-4]) < min_ns:
                    test_short.append(item)

        for my_dict in tqdm.tqdm(validate):
            file_name = my_dict["doc_id"] + ".pkl"
            if not os.path.exists(processed_dir+file_name):
                data = get_data_point(my_dict, 5)
                with open(processed_dir+file_name, 'wb') as f:
                    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            else:
                with open(processed_dir+file_name, 'rb') as f:
                    data = pickle.load(f)
            for item in data:
                if len(item[-4]) >= min_ns:
                    validate_set.append(item)
                if len(item[-4]) < min_ns:
                    validate_short.append(item)

        print("Train_size: {}".format(len(train_set) + len(train_short)))
        print("Test_size: {}".format(len(test_set) + len(test_short)))
        print("Validate_size: {}".format(len(validate_set) + len(validate_short)))

    if dataset=='infer':
        reader = Reader(file_type)
        print(f'Reading file {file_path} ....')
        my_dict = reader.read('', file_path)
        data = get_data_point(my_dict, label_type)
        for item in data:
            if len(item[-4]) >= min_ns:
                test_set.append(item)
            else:
                test_short.append(item)
        print("Train_size: {}".format(len(train_set)))
        print("Test_size: {}".format(len(test_set)))
        print("Validate_size: {}".format(len(validate_set)))
        print("Train_size: {}".format(len(train_short)))
        print("Test_size: {}".format(len(test_short)))
        print("Validate_size: {}".format(len(validate_short)))

    del sent_encoder
    del c2v
    gc.collect()

    return train_set, test_set, validate_set, train_short, test_short, validate_short


import os
import tkinter as tk
from  tkinter import messagebox

# 定时关机
def on_click():
	time = xls_text.get().lstrip()
	if time == "0" or time == "":
		os.system("shutdown -s")
		exit()
	else:
		index = 1
		try:
			a = int(time)
			if len(time) > 6:
				tk.messagebox.showinfo("提示", "请输入一个稍小的数字")
				return 1
		except:
			tk.messagebox.showinfo("错误", "请输入一个正确的数字")
			index = 0
			return 0
		if index == 1:
			os.system("shutdown -s -t " + time)
			tk.messagebox.showinfo("完成", "计算机将在 "+time+" 秒后关机！")
# 定时重启
def on_click3():
	time = xls_text.get().lstrip()
	if time == "0" or time == "":
		os.system("shutdown -r")
		exit()
	else:
		index = 1
		try:
			a = int(time)
			if len(time) > 6:
				tk.messagebox.showinfo("提示", "请输入一个稍小的数字")
				return 1
		except:
			tk.messagebox.showinfo("错误", "请输入一个正确的数字")
			index = 0
			return 0
		if index == 1:
			os.system("shutdown -r -t " + time)
			tk.messagebox.showinfo("完成", "计算机将在 "+time+" 秒后重启！")
# 取消关机
def on_click2():
	os.system("shutdown -a")

root = tk.Tk()
root.geometry('400x200+200+200')
root.title("自动关机 power by : 王荣光")
xls_text = tk.StringVar()

l1 = tk.Label(root, text="请输入关机定时(0或空为立即执行)")
l1.pack()
xls = tk.Entry(root, textvariable=xls_text)
xls_text.set(" ")
xls.pack()

tk.Button(root, text="设置关机", command=on_click).pack()
tk.Button(root, text="设置重启", command=on_click3).pack()
tk.Button(root, text="立即取消", command=on_click2).pack()
root.mainloop()


import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
# unfinished 18/02

def gaussian(height, center_x, center_y, width_x, width_y):
    """Returns a gaussian function with the given parameters"""
    width_x = float(width_x)
    width_y = float(width_y)
    return lambda x, y: height*np.exp(
        -(((center_x-x)/width_x)**2+((center_y-y)/width_y)**2)/2)

def moments(data):
    """Returns (height, x, y, width_x, width_y)
    the gaussian parameters of a 2D distribution by calculating its
    moments """
    total = data.sum()
    X, Y = np.indices(data.shape)
    x = (X*data).sum()/total
    y = (Y*data).sum()/total
    col = data[:, int(y)]
    width_x = np.sqrt(np.abs((np.arange(col.size)-y)**2*col).sum()/col.sum())
    row = data[int(x), :]
    width_y = np.sqrt(np.abs((np.arange(row.size)-x)**2*row).sum()/row.sum())
    height = data.max()
    return height, x, y, width_x, width_y

def fitgaussian(data):
    """Returns (height, x, y, width_x, width_y)
    the gaussian parameters of a 2D distribution found by a fit"""
    params = moments(data)
    def errorfunction(p): return np.ravel(gaussian(*p)(*np.indices(data.shape)) -
                                          data)
    p, success = optimize.leastsq(errorfunction, params)
    return p

def gaussian_fitting(galaxies_points, img):
    for counter in range(5):
        galaxy = galaxies_points[counter]
        x_min, x_max = 1e9, 0
        y_min, y_max = 1e9, 0
        for point in galaxy:
            if point[0] < x_min:
                x_min = point[0]
            elif point[0] > x_max:
                x_max = point[0]
            if point[1] < y_min:
                y_min = point[1]
            elif point[1] > y_max:
                y_max = point[1]
        buffer = 0
        x_min = max(int(x_min-buffer), 0)
        # x_max = min(int(x_max+buffer), img.shape[1] - 1)
        y_min = max(int(y_min-buffer), 0)
        # y_max = min(int(y_max+buffer), img.shape[0] - 1)
        # print(x_min, x_max, y_min, y_max)
        if x_min == x_max or y_min == y_max:
            print(f'weird stuff {counter}')
            continue

        x = np.arange(x_min, x_max, 1)
        y = np.arange(y_min, y_max, 1)
        x, y = np.meshgrid(y, x)
        img_slice = img[x_min:x_max, y_min:y_max]
        params = fitgaussian(img_slice)
        fit = gaussian(*params)
        fig, ax = plt.subplots()
        plt.imshow(img_slice, origin='upper')
        plt.contour(fit(*np.indices(img_slice.shape)))
        # ax.contour(x, y, twoD_Gaussian((x, y), *popt).reshape(7, 12), 1, colors='w')

if __name__ == '__main__':
    img = np.load('testData_noisy.npy')
    galaxies_points = np.load('galaxies_points.npy', allow_pickle=True)
    # print(img.shape)
    # print(len(galaxies_points))
    gaussian_fitting(galaxies_points, img)
    plt.show()


# Import the necessary libraries
import gym
import numpy as np

# Create the Frozen Lake environment using Gym
env = gym.make('FrozenLake-v0')

# Let's take a look at the Frozen Lake environment using the render function
env.render()

# FROM HERE WE WILL START COMPUTING THE OPTIMAL VALUE FUNCTION
# Define the value_iteration function, which takes the environment as a parameter
def value_iteration(env):
	# Set the number of iterations
	num_iterations = 1000

	# Set the threshold number for checking the convergence of the value function
	threshold = 1e-20

	# Set the discount factor 𝛾  to 1
	gamma = 1.0

	# We will initialize the value table by setting the value of all states to zero
	value_table = np.zeros(env.observation_space.n)

	# For every iteration
	for i in range(num_iterations):
		# Update the value table
		updated_value_table = np.copy(value_table)

		# for each state, we compute the Q values of all the actions
		# in the state and then we update the value of the state as
		# the one that has the maximum Q value
		for s in range(env.observation_space.n):
			# Compute the Q value of all the actions
			Q_values = [sum([prob*(r + gamma * updated_value_table[s_])
				for prob, s_, r, _ in env.P[s][a]])
					for a in range(env.action_space.n)]

			# Update the value of the state as a maximum Q value
			value_table[s] = max(Q_values)

		# After computing the value table, we check whether
		# the difference between the value table obtained in
		# the current iteration and the previous iteration
		# is less than or equal to a threshold value.
		# If the difference is less than the threshold,
		# then we break the loop and return the value table
		# as our optimal value function as the following code shows:
		if (np.sum(np.fabs(updated_value_table - value_table)) <= threshold):
			break
	return value_table

# FROM HERE WE WILL EXTRACT THE OPTIMAL POLICY FROM
# THE COMPUTED OPTIMAL VALUE FUNCTION

# We define a function called extract_policy
# which takes value_table as a parameter
def extract_policy(value_table):
	# Set the discount factor 𝛾  to 1
	gamma = 1.0
	# Let initialize the policy with zeros,
	# that is, we set the actions for all the states to be zero:
	policy = np.zeros(env.observation_space.n)

	# For each state, we compute the Q values for all the actions
	# in the state and then we extract the policy by selecting
	# the action that has the maximum Q value.
	for s in range(env.observation_space.n):
		# Compute the Q value of all the actions in the state, 𝑄(𝑠,a)
		Q_values = [sum([prob*(r + gamma * value_table[s_])
			for prob, s_, r, _ in env.P[s][a]])
				for a in range(env.action_space.n)]

		# Extract the policy by selecting the action that has the maximum Q value,
		policy[s] = np.argmax(np.array(Q_values))
	return policy

#	We learned that in the Frozen Lake environment,
#	our goal is to find the optimal policy that selects the correct action
#	in each state so that we can reach state G from state A without visiting the hole states.

# Firstly, we compute the optimal value function
# using our value_iteration function by passing our
# Frozen Lake environment as the parameter:
optimal_value_function = value_iteration(env)

# Next, we extract the optimal policy from the optimal value
# function using our extract_policy function:
optimal_policy = extract_policy(optimal_value_function)

# We can print the obtained optimal policy:
print(optimal_policy)

# The preceding code will print the following.
# As we can observe, our optimal policy tells us
# to perform the correct action in each state:

basic_rl_algorithm/2. Solving the Frozen Lake problem with value iteration.py


import pickle
from numpy import dot
from numpy.linalg import norm
from scipy.spatial import distance

with open('./anchor_embeddings.pkl', 'rb') as handler:
    anchor_embeddings = pickle.load(handler)

with open('./positive_embeddings.pkl', 'rb') as handler:
    positive_embeddings = pickle.load(handler)

with open('./negative_embeddings.pkl', 'rb') as handler:
    negative_embeddings = pickle.load(handler)

def cos_sim(a, b):
    return dot(a, b)/(norm(a)*norm(b))

# print(len(anchor_embeddings))
similarity_scores = []
for i in range(len(anchor_embeddings)):
    anchor_vector = anchor_embeddings[i]
    positive_vector = positive_embeddings[i]
    negative_vector = negative_embeddings[i]
    # print(type(anchor_vector), anchor_vector.shape)
    pos_cos_sim = 1 - distance.cosine(anchor_vector, positive_vector)
    # pos_cos_sim = 1 - distance.euclidean(anchor_vector, positive_vector)
    # pos_cos_sim = cos_sim(anchor_vector, positive_vector)

    neg_cos_sim = 1 - distance.cosine(anchor_vector, negative_vector)
    # neg_cos_sim = 1 - distance.euclidean(anchor_vector, negative_vector)
    # neg_cos_sim = cos_sim(anchor_vector, negative_vector)
    print(pos_cos_sim, neg_cos_sim)
    similarity_scores.append( (pos_cos_sim, neg_cos_sim) )
    # break

with open('./similarity_scores.pkl', 'wb') as handler:
    pickle.dump(similarity_scores, handler)


from django.shortcuts import render

from django.core.exceptions import ValidationError
from django.db.models import Q, ProtectedError

from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated, AllowAny

from recruit.models import JobOpening, Company
from recruit.serializers import JobOpeningSerializer, UpdateJobOpeningSerializer, CompanySerializer

class JobOpeningView(APIView):
    permission_classes = [AllowAny, ]

    def get(self, request, jo_pk):
        job_opening = JobOpening.objects.get(pk=jo_pk)
        serializer = JobOpeningSerializer(job_opening)

        other_opening = JobOpening.objects.filter(company=job_opening.company)
        other_opening_pk = [x.pk for x in other_opening]
        other_opening_pk.pop(other_opening_pk.index(jo_pk))

        data = serializer.data
        data["other_opening"] = other_opening_pk

        return Response({"message":"success get!", "data": data}, status=status.HTTP_200_OK)

    def put(self, request, jo_pk):
        data = request.data
        if data.get("company"):
            raise ValidationError("company cannot be changed.")

        serializer = UpdateJobOpeningSerializer(data=data, partial=True)
        job_opening = JobOpening.objects.get(pk=jo_pk)

        if serializer.is_valid():
            # serializer에 의해 company가 제외된다.
            print("serial data:", serializer.validated_data)
            obj = serializer.update(job_opening, serializer.validated_data)
            result = UpdateJobOpeningSerializer(obj)
            return Response({"message": "success update!", "data": result.data}, status=status.HTTP_200_OK)

        return Response({"message": "fail"}, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, jo_pk):
        job_opening = JobOpening.objects.get(pk=jo_pk)
        serializer = JobOpeningSerializer(job_opening)
        job_opening.delete()

        return Response({"message":"success delete!", "data": serializer.data}, status=status.HTTP_200_OK)

class JobOpeningCreateView(APIView):
    permission_classes = [AllowAny, ]

    def post(self, request):
        data = request.data
        serializer = JobOpeningSerializer(data=data)
        print(serializer.is_valid())
        print(serializer.errors)

        if serializer.is_valid():
            # serializer.data 는 Foreinkey의 pk만, serializer.validated_data는 object가 나옴
            obj = serializer.create(serializer.validated_data)
            result = JobOpeningSerializer(obj)
            return Response({"message": "success create!", "data": result.data }, status=status.HTTP_201_CREATED)

        return Response({"message": "fail", "error": serializer.errors }, status=status.HTTP_400_BAD_REQUEST)

class JobOpeningListView(APIView):
    permission_classes = [AllowAny, ]

    def get(self, request):
        search_query = request.GET.get('search', None)

        if search_query:
            print(search_query)

            try:
                results = JobOpening.objects.filter(
                    Q(company__name__icontains=search_query) |
                    Q(position__icontains=search_query) |
                    Q(content__icontains=search_query) |
                    Q(tech__icontains=search_query)
                    )
            except Company.DoesNotExist as e:
                results = JobOpening.objects.filter(
                    Q(position__icontains=search_query) |
                    Q(content__icontains=search_query) |
                    Q(tech__icontains=search_query)
                    )
            finally:
                serializer = JobOpeningSerializer(results, many=True)
                for data in serializer.data:
                    data["company_name"] = Company.objects.get(pk=data.get("id")).name

                print("results:",results)
                return Response({"message": "success get!", "data": serializer.data}, status=status.HTTP_200_OK)
        else:
            result = JobOpening.objects.all()
            serializer = JobOpeningSerializer(result, many=True)

            return Response({"message": "success get!", "data": serializer.data}, status=status.HTTP_200_OK)

class ApplyView(APIView):
    permission_classes = [AllowAny, ]

    def post(self, request, jo_pk):
        user = request.user
        job_opening = JobOpening.objects.get(pk=jo_pk)

        if job_opening in user.applyed.all():
            return Response({"message" : "A Job Opening could be applied only once."}, status=status.HTTP_400_BAD_REQUEST)

        user.applyed.add(job_opening)
        serializer = JobOpeningSerializer(job_opening)

        return Response({"message": "success to apply", "data":serializer.data}, status=status.HTTP_200_OK)


from flask import Flask, jsonify, request

app = Flask(__name__)

pecas = [
    {
        'id': 1,
        'peca' : 'bateria',
        'descricao':'bateria moura'
    },
    {
        'id': 2,
        'peca' : 'bubina',
        'descricao':'bubina generica'
    },
    {
        'id': 3,
        'peca' : 'filtro de oleo',
        'descricao':'filtro de oleo generico'
    }
]

@app.route('/pecas', methods=['GET'])
def obter_pecas():
    return jsonify(pecas)

@app.route('/pecas/<int:id>', methods=['GET'])
def obter_pecas_id(id):
    for peca in pecas:
      if peca.get('id') == id:
            return jsonify(peca)

@app.route('/pecas/<int:id>', methods=['PUT'])
def editar_peca_id(id):
    peca_dif = request.get_json()
    for indice,peca in enumerate(pecas):
        if peca.get('id') == id:
            pecas[indice].update(peca_dif)
            return jsonify(pecas[indice])

@app.route('/pecas', methods=['POST'])
def incluir_nova_peca():
    nova_peca = request.get_json()
    pecas.append(nova_peca)

    return jsonify(pecas)

@app.route('/pecas/<int:id>', methods=['DELETE'])
def excluir_peca(id):
    for indice, peca in enumerate(pecas):
        if peca.get('id') == id:
            del pecas[indice]
        return jsonify(pecas)

app.run(port=5000, host='localhost', debug=True)


vals = input().split()  # standard input and split it into items
ivals = [int(val) for val in vals]  # using list comprehesion to convert items into numbers

def get_type(a, b, c):
  longest = max(a, b, c)
  others = a + b + c - longest
  if others > longest:
    return 2  # can make triangle
  elif others == longest:
    return 1  # cna make segment
  else:
    return 0  # impossible

# a, b, c, d
# a, b, c (missing d)
# a, b, d (missing c)
# a, c, d (missing b)
# b, c, d (missing a)

final_tp = 0  # initialize the final type to be impossible
for i in range(4):
  tp = get_type(ivals[i], ivals[(i+1)%4], ivals[(i+2)%4])  # getting the remainder in order to reset to the start if it's out of index
  # if tp == 2: # if tp is 2, means you already find a triangle, no need to loop
  #   final_tp = tp
  #   break
  # elif tp > final_tp: # otherwise, you have to update the final_tp,
  #   final_tp = tp
  if tp > final_tp:
    final_tp = tp
    if final_tp == 2:
      break

if final_tp == 2:
  print("TRIANGLE")
elif final_tp == 1:
  print("SEGMENT")
else:
  print("IMPOSSIBLE")


'''
Crie um programa que vai ler vários números e colocar em uma lista.
Depois disso, crie duas listas extras que vão conter apenas os valores pares e os valores ímpares digitados,
respectivamente. Ao final, mostre o conteúdo das três listas geradas.
'''
lista = []
par = []
impar = []

while True:
    n = (int(input('Digite um valor: ')))
    lista.append(n)
    if n % 2 == 0:
        par.append(n)
    else:
        impar.append(n)
    resp = ' '
    while resp not in 'SN':
        resp = input('Você quer continuar? [S/N]').strip().upper()[0]
    if resp == 'N':
        break
print(f'A lista completa é: {lista}')
print(f'A lista de pares é: {par}')
print(f'A lista de impares é: {impar}')


from __future__ import unicode_literals

import frappe, os, copy, json, re
from frappe import _

from frappe.modules import get_doc_path
from jinja2 import TemplateNotFound
from frappe.utils import cint, strip_html
from frappe.utils.pdf import get_pdf,cleanup
from PyPDF2 import PdfFileWriter, PdfFileReader

no_cache = 1
no_sitemap = 1

base_template_path = "templates/www/printview.html"
standard_format = "templates/print_formats/standard.html"

@frappe.whitelist()
def download_multi_pdf(doctype, name, format=None):
	"""
	Concatenate multiple docs as PDF .

	Returns a PDF compiled by concatenating multiple documents. The documents
	can be from a single DocType or multiple DocTypes

	Note: The design may seem a little weird, but it exists exists to
		ensure backward compatibility. The correct way to use this function is to
		pass a dict to doctype as described below

	NEW FUNCTIONALITY
	=================
	Parameters:
	doctype (dict):
		key (string): DocType name
		value (list): of strings of doc names which need to be concatenated and printed
	name (string):
		name of the pdf which is generated
	format:
		Print Format to be used

	Returns:
	PDF: A PDF generated by the concatenation of the mentioned input docs

	OLD FUNCTIONALITY - soon to be deprecated
	=========================================
	Parameters:
	doctype (string):
		name of the DocType to which the docs belong which need to be printed
	name (string or list):
		If string the name of the doc which needs to be printed
		If list the list of strings of doc names which needs to be printed
	format:
		Print Format to be used

	Returns:
	PDF: A PDF generated by the concatenation of the mentioned input docs
	"""

	import json
	output = PdfFileWriter()

	if not isinstance(doctype, dict):
		result = json.loads(name)

		# Concatenating pdf files
		for i, ss in enumerate(result):
			output = frappe.get_print(doctype, ss, format, as_pdf = True, output = output)
		frappe.local.response.filename = "{doctype}.pdf".format(doctype=doctype.replace(" ", "-").replace("/", "-"))
	else:
		for doctype_name in doctype:
			for doc_name in doctype[doctype_name]:
				try:
					output = frappe.get_print(doctype_name, doc_name, format, as_pdf = True, output = output)
				except Exception:
					frappe.log_error("Permission Error on doc {} of doctype {}".format(doc_name, doctype_name))
		frappe.local.response.filename = "{}.pdf".format(name)

	frappe.local.response.filecontent = read_multi_pdf(output)
	frappe.local.response.type = "download"

def read_multi_pdf(output):
	# Get the content of the merged pdf files
	fname = os.path.join("/tmp", "frappe-pdf-{0}.pdf".format(frappe.generate_hash()))
	output.write(open(fname,"wb"))

	with open(fname, "rb") as fileobj:
		filedata = fileobj.read()

	return filedata

@frappe.whitelist()
def download_pdf(doctype, name, format=None, doc=None, no_letterhead=0):
	html = frappe.get_print(doctype, name, format, doc=doc, no_letterhead=no_letterhead)
	frappe.local.response.filename = "{name}.pdf".format(name=name.replace(" ", "-").replace("/", "-"))
	frappe.local.response.filecontent = get_pdf(html)
	frappe.local.response.type = "download"

@frappe.whitelist()
def report_to_pdf(html, orientation="Landscape"):
	frappe.local.response.filename = "report.pdf"
	frappe.local.response.filecontent = get_pdf(html, {"orientation": orientation})
	frappe.local.response.type = "download"

@frappe.whitelist()
def print_by_server(doctype, name, print_format=None, doc=None, no_letterhead=0):
	print_settings = frappe.get_doc("Print Settings")
	try:
		import cups
	except ModuleNotFoundError:
		frappe.throw("You need to install pycups to use this feature!")
		return
	try:
		cups.setServer(print_settings.server_ip)
		cups.setPort(print_settings.port)
		conn = cups.Connection()
		output = PdfFileWriter()
		output = frappe.get_print(doctype, name, print_format, doc=doc, no_letterhead=no_letterhead, as_pdf = True, output = output)
		file = os.path.join("/", "tmp", "frappe-pdf-{0}.pdf".format(frappe.generate_hash()))
		output.write(open(file,"wb"))
		conn.printFile(print_settings.printer_name,file , name, {})
	except IOError as e:
		if ("ContentNotFoundError" in e.message
			or "ContentOperationNotPermittedError" in e.message
			or "UnknownContentError" in e.message
			or "RemoteHostClosedError" in e.message):
			frappe.throw(_("PDF generation failed"))
	except cups.IPPError:
		frappe.throw(_("Printing failed"))
	finally:
		cleanup(file,{})


__author__ = 'sameer'
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.internet import reactor
from twisted.python import log
from pprint import pprint
import treq
from decimal import Decimal
from bs4 import BeautifulSoup
from datetime import datetime, timedelta
import json

class Yahoo():
    def __init__(self, **kwargs):
        self.yahoo_uri = "http://finance.yahoo.com/"
        self.oanda_uri = "http://www.oanda.com/currency/"

    @inlineCallbacks
    def getOrderBook(self, ticker):
        payout, denominated = ticker.split('/')
        url =  self.yahoo_uri + "q"
        params = {'s': "%s%s=X" % (payout, denominated)}
        response = yield treq.get(url, params=params)
        content = yield response.content()
        soup = BeautifulSoup(content)
        bid = Decimal(soup.find(id="yfs_b00_%s%s=x" % (payout.lower(), denominated.lower())).text.replace(',', ''))
        ask = Decimal(soup.find(id="yfs_a00_%s%s=x" % (payout.lower(), denominated.lower())).text.replace(',', ''))
        book = {'contract': ticker,
                'bids': [{'price': bid, 'quantity': 0}],
                'asks': [{'price': ask, 'quantity': 0}]}
        returnValue(book)

    @inlineCallbacks
    def getOHLCVHistory(self, ticker, period="day", start_datetime=None, end_datetime=None):
        payout, denominated = ticker.split('/')

        url = self.oanda_uri + "historical-rates/update"
        params = {'date_fmt': 'us',
                  'start_date': start_datetime.strftime('%Y-%m-%d'),
                  'end_date': end_datetime.strftime('%Y-%m-%d'),
                  'period': "daily",
                  'quote_currency': payout,
                  'base_currency_0': denominated,
                  'rate': 0,
                  'view': 'table',
                  'display': 'absolute',
                  'price': 'bid',
                  'data_range': 'd90'}
        headers = {'X-Requested-With': 'XMLHttpRequest',
                   'X-Prototype-Version': '1.7',
                   'Referer': 'http://www.oanda.com/currency/historical-rates/'}
        try:
            response = yield treq.get(url, params=params, headers=headers)
        except Exception as e:
            pass
        content = yield response.content()
        parsed = json.loads(content)
        data = parsed['widget'][0]['data']
        ohlcv_history = {}

        for row in data:
            date = datetime.fromtimestamp(row[0]/1e3)
            price = float(row[1])
            epoch = datetime.utcfromtimestamp(0)
            open_timestamp = int((date - epoch).total_seconds() * 1e6)
            tomorrow_date = date + timedelta(days=1)
            close_timestamp = int((tomorrow_date - epoch).total_seconds() * 1e6) - 1

            ohlcv = {
                'contract': ticker,
                'open': price,
                'high': price,
                'low': price,
                'close': price,
                'volume': 0,
                'vwap': price,
                'open_timestamp': open_timestamp,
                'close_timestamp': close_timestamp,
                'period': period
            }
            ohlcv_history[open_timestamp] = ohlcv

        returnValue(ohlcv_history)

if __name__ == "__main__":
    yahoo = Yahoo()
    #d = yahoo.getOrderBook('USD/MXN')
    #d.addCallback(pprint).addErrback(log.err)
    now = datetime.utcnow()
    d2 = yahoo.getOHLCVHistory('USD/HUF', start_datetime=now-timedelta(days=30), end_datetime=now)
    d2.addCallback(pprint).addErrback(log.err)

    reactor.run()


# DBSCAN(Density-Based Spatial Clustering of Applications with Noise)
# DBSCAN是基于密度的聚类的一种，相比于K-means，DBSCAN可以聚类非凸的样本
# DBSCAN聚出来的类的数量不是手动定的，而是算法自动算出来的
# DBSCAN第一步是计算

import numpy as np
import random

class DBSCAN:
    def __init__(self, eps=0.5, min_samples=5):
        self.eps = eps  # 邻域半径
        self.min_samples = min_samples  # 定义核心对象所需的领域半径内最少的点数
        self.labels = None  # 样本标签
        self.core_sample_indices = None # 核心样本数量

    def fit(self, X, y=None):
        n_samples, n_features = X.shape
        eps_neignbors = []
        indices = np.arange(0, n_samples)
        # 计算每个样本点的eps邻域
        for i in range(n_samples):
            # 针对每一个测试样本计算它到训练集每个样本的距离
            # 这里只用欧式距离
            distances = np.linalg.norm(X - X[i], axis=1)
            eps_neignbors.append(indices[distances<=self.eps])
        # 过滤出核心对象
        n_neighbors = np.array([len(neighbors)
                                for neighbors in eps_neignbors])
        is_core_samples = n_neighbors >= self.min_samples
        self.core_sample_indices = indices[is_core_samples]
        # 随机选取核心对象并从其出发进行扩充聚类，直到没有核心对象；剩下来的样本的为噪声点/离群点
        # 不加self的core_sample_indices只是个临时对象
        core_sample_indices = set(self.core_sample_indices)
        # clusters = [] # 记录聚类簇 # 这个东西好像并不需要
        k = 0   # 表示第几个类
        labels = np.ones(n_samples, dtype=np.int32) * -1 # 所有样本都设置成-1，后面就不用为噪声点再遍历一次了
        is_scanned = np.zeros(n_samples, dtype=bool) # 记录是否已经扫描过了
        while(core_sample_indices):
            core = random.sample(core_sample_indices,1)[0] # 随机选取一个核心对象
            q = [core] # 用核心对象初始化一个空队列(这里就利用列表来做)
            is_scanned[core] = True
            while(q):
                current = q.pop(0)
                labels[current] = k  # 设置标签
                if is_core_samples[current]:
                    core_sample_indices.remove(current)
                    extended = []
                    for x in eps_neignbors[current]:
                        if not is_scanned[x]:
                            extended.append(x)
                            is_scanned[x] = True
                    q.extend(extended) # 如果是核心对象就扩展
            k += 1

        self.labels = labels

        return self

    def fit_predict(self, X, y=None):
        self.fit(X)
        return self.labels


import cv2 as cv
import numpy as np

Part28 = "Face and eye detection"
# all the idea here that we are going to apply haar
# haar is basically a method which applies different things on the picture to detect something

# for eye detection, you won't get a wrong answer if you didn't crop the face, it will gave you the same result

#image = cv.imread('C:/Users/LENOVO/Desktop/Pictures and videos/Me.jpg')

image = cv.imread('C:/Users/LENOVO/Desktop/Me.jpg')
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)

# actually I didn't see any difference when I used haarcascade_frontalface_alt2, it gave me the same result

face_classifier = cv.CascadeClassifier('haarcascade_frontalface_default.xml')

eye_classifier = cv.CascadeClassifier('haarcascade_eye.xml')
eye = eye_classifier.detectMultiScale(gray,1.3,5)

# faces is a list holds four lines which borders the face

faces = face_classifier.detectMultiScale(gray,1.3,5)

for x,y,w,h in faces:
    cv.rectangle(image,(x,y),(x+w,y+h),(0,255,0),3)
    cv.imshow("window",image)

for x,y,w,h in eye:
    cv.rectangle(image,(x,y),(x+w,y+h),(0,255,0),3)
    cv.imshow("window",image)

cv.waitKey()

Learning_Codes_and_Projects/Face and eye detection.py


from   qm.executable import *
import os
import os.path
import qm
import StringIO
import re
import sys
if sys.platform != "win32":
    import resource

########################################################################
# Classes
########################################################################

class CompilerExecutable(RedirectedExecutable):
    """A 'CompilerExecutable' is a 'Compiler' that is being run."""

    def _InitializeChild(self):
        """Initialize the child process.

        After 'fork' is called this method is invoked to give the
        child a chance to initialize itself.  '_InitializeParent' will
        already have been called in the parent process."""

        # Disable compiler core dumps.
        if sys.platform != "win32":
            resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
        # Do whatever the base class version would otherwise do.
        RedirectedExecutable._InitializeChild(self)

    def _StdinPipe(self):
        """Return a pipe to which to redirect the standard input.

        returns -- A pipe, or 'None' if the standard input should be
        closed in the child."""

        # The compiler should not need the standard input.
        return None

    def _StderrPipe(self):
        """Return a pipe to which to redirect the standard input.

        returns -- A pipe, or 'None'.  If 'None' is returned, but
        '_StdoutPipe' returns a pipe, then the standard error and
        standard output will both be redirected to that pipe.  However,
        if '_StdoutPipe' also returns 'None', then the standard error
        will be closed in the child."""

        # The standard output and standard error are combined.
        return None

class Compiler:
    """A 'Compiler' compiles and links source files."""

    MODE_PREPROCESS = 'preprocess'
    """Preprocess the source files, but do not compile them."""

    MODE_COMPILE = 'compile'
    """Compile the source files, but do not assemble them."""

    MODE_ASSEMBLE = 'assemble'
    """Compile the source files, but do not link them."""

    MODE_LINK = 'link'
    """Compile and link the source files."""

    modes = [ MODE_COMPILE, MODE_ASSEMBLE, MODE_LINK, MODE_PREPROCESS ]
    """The available compilation modes."""

    def __init__(self, path, options=None, ldflags=None):
        """Construct a new 'Compiler'.

        'path' -- A string giving the location of the compiler
        executable.

        'options' -- A list of strings indicating options to the
        compiler, or 'None' if there are no options.

        'ldflags' -- A list of strings indicating ld flags to the
        compiler, or 'None' if there are no flags."""

        self._path = path
        self.SetOptions(options or [])
        self.SetLDFlags(ldflags or [])

    def Compile(self, mode, files, dir, options = [], ldflags = [],
                output = None, timeout = -1):
        """Compile the 'files'.

        'mode' -- The compilation mode (one of the 'Compiler.modes')
        that should be used to compile the 'files'.

        'files' -- A sequence of strings giving the names of source
        files (including, in general, assembly files, object files,
        and libraries) that should be compiled.

        'dir' -- The directory in which to run the compiler.

        'options' -- A sequence of strings indicating additional
        options that should be provided to the compiler.

        'ldflags' -- A sequence of strings indicating additional
        linker flags that should be provided to the compiler, if
        linking is done.

        'output' -- The name of the file should be created by the
        compilation.  If 'None', the compiler will use a default
        value.

        'timeout' -- The maximum number of seconds the compiler is
        permitted to run.  If 'timeout' is -1, the compiler is
        permitted to run forever.

        returns -- A tuple '(status, output)'.  The 'status' is the
        exit status returned by the compiler, as indicated by
        'waitpid'.  The 'output' is a string containing the standard
        outpt and standard errror generated by the compiler."""

        # Get the command to use.
        command = self.GetCompilationCommand(mode, files, options,
                                             ldflags, output)
        # Invoke the compiler.
        return self.ExecuteCommand(dir, command, timeout)

    def ExecuteCommand(self, dir, command, timeout = -1):
        """Execute 'command' in 'dir'.

        'dir' -- The directory in which to execute the command.

        'command' --  A sequence of strings, as returned by
        'GetCompilationCommand'.

        'timeout' -- The maximum number of seconds the compiler is
        permitted to run.  If 'timeout' is -1, the compiler is
        permitted to run forever.

        returns -- A tuple '(status, output)'.  The 'status' is the
        exit status returned by the compiler, as indicated by
        'waitpid'.  The 'output' is a string containing the standard
        output and standard errror generated by the compiler."""

        # Invoke the compiler.
        executable = CompilerExecutable(timeout)
        status = executable.Run(command, dir = dir)
        # Return all of the information.
        return (status, executable.stdout)

    def GetCompilationCommand(self, mode, files, options=[],
                              ldflags = [], output=None):
        """Return the appropriate command for compiling 'files'.

        'mode' -- The compilation mode (one of the 'Compiler.modes')
        that should be used to compile the 'files'.

        'files' -- A sequence of strings giving the names of source
        files (including, in general, assembly files, object files,
        and libraries) that should be compiled.

        'options' -- A sequence of strings indicating additional
        options that should be provided to the compiler.

        'ldflags' -- A sequence of strings indicating additional
        linker flags that should be provided to the compiler, if
        linking is done.

        'output' -- The name of the file should be created by the
        compilation.  If 'None', the compiler will use a default
        value.  (In some cases there may be multiple outputs.  For
        example, when generating multiple object files from multiple
        source files, the compiler will create a variety of objects.)

        returns -- A sequence of strings indicating the arguments,
        including 'argv[0]', for the compilation command."""

        # Start with the path to the compiler.
        command = [self.GetPath()]
        # Add switches indicating the compilation mode, if appropriate.
        command += self._GetModeSwitches(mode)
        # Add the options that should be used with every compilation.
        command += self._options
        # Add the options that apply to this compilation.
        command += options
        # Set the output file.
        if output:
            command += ["-o", output]
        # Add the input files.
        command += files
        if mode == Compiler.MODE_LINK:
            command += ldflags
            command += self.GetLDFlags()

        return command

    def ParseOutput(self, output, ignore_regexps = ()):
        """Turn the 'output' into a sqeuence of 'Diagnostic's.

        'output' -- A string containing the compiler's output.

        'ignore_regexps' -- A sequence of regular expressions.  If a
        diagnostic message matches one of these regular expressions,
        it will be ignored.

        returns -- A list of 'Diagnostic's corresponding to the
        messages indicated in 'output', in the order that they were
        emitted."""

        raise NotImplementedError

    def GetPath(self):
        """Return the location of the executable.

        returns -- A string giving the location of the executable.
        This location is the one that was specified as the 'path'
        argument to '__init__'."""

        return self._path

    def GetOptions(self):
        """Return the list of compilation options.

        returns -- A list of strings giving the compilation options
        specified when the 'Compiler' was constructed."""

        return self._options

    def SetOptions(self, options):
        """Reset the list of compiler options.

        'options' -- A list of strings indicating options to the
        compiler, or 'None' if there are no options."""

        self._options = options

    def GetLDFlags(self):
        """Return the list of link options.

        returns -- A list of strings giving the link options
        specified when the 'Compiler' was constructed."""

        return self._ldflags

    def SetLDFlags(self, ldflags):
        """Reset the list of link options.

        'ldflags' -- A list of strings indicating options to the
        linker, or 'None' if there are no flags."""

        self._ldflags = ldflags

    def GetExecutableExtension(self):
        """Return the extension for executables.

        returns -- The extension (including leading '.', if
        applicable) for executable files created by this compiler."""

        if sys.platform == "win32":
            return ".exe"
        else:
            return ""

    def GetObjectExtension(self):
        """Return the extension for object files.

        returns -- The extension (including leading '.', if
        applicable) for object files created by this compiler."""

        if sys.platform == "win32":
            return ".obj"
        else:
            return ".o"

    def _GetModeSwitches(self, mode):
        """Return the compilation switches for the compilation 'mode'.

        'mode' -- The compilation mode (one of 'Compiler.modes').

        returns -- A sequence of strings indicating the switches that
        are used to indicate the compilation mode."""

        if mode == self.MODE_PREPROCESS:
            return ["-E"]
        elif mode == self.MODE_COMPILE:
            return ["-S"]
        elif mode == self.MODE_ASSEMBLE:
            return ["-c"]

        # Other modes require no special option.
        return []

class SourcePosition:
    """A 'SourcePosition' indicates a location in source code.

    A 'SourcePosition' consists of:

    - A file name.  The file name is a string.  It may be an absolute
      or relative path.  If no file name is available, the file name
      is the empty string.

    - A line number, indexed from one.  If no line number is
      available, the line number is zero.

    - A column number, indexed from one.  If no column number is
      available, the column nubmer is zero."""

    def __init__(self, file, line, column):
        """Construct a new 'SourcePosition'.

        'file' -- The file name.

        'line' -- The line number, indexed from one.  If no line numer
        is availble, use zero for this parameter.

        'column' -- The column number, indexed from one.  If no column
        number is available, use zero for this parameter."""

        self.file = file
        self.line = line
        self.column = column

    def __str__(self):
        """Return a textual representation of this 'SourcePosition'.

        returns -- A string representing this 'SourcePosition'"""

        result = ''
        if self.file:
            result = result + '"%s"' % os.path.split(self.file)[0]
        if self.line:
            if self.file:
                result = result + ', '
            result = result + 'line %d' % self.line
        if self.column:
            result = result + ': %d' % self.column

        return result

class Diagnostic:
    """A 'Diagnostic' is a message issued by a compiler.

    Each 'Diagnostic' has the following attributes:

    - The source position that the compiler associates with the
      diagnostic.

    - The severity of the diagnostic.

    - The message issued by the compiler.

    A 'Diagnostic' may either be an actual diagnostic emitted by a
    compiler, or it may be the pattern for a diagnostic that might be
    emitted.  In the latter case, the message is a regular expression
    indicating the message that should be emitted."""

    def __init__(self, source_position, severity, message):
        """Construct a new 'Diagnostic'.

        'source_position' -- A 'SourcePosition' indicating where the
        diagnostic was issued.  For an expected diagnostic, 'None'
        indicates that the position does not matter.

        'severity' -- A string indicating the severity of the
        diagnostic.  For an expected diagnostic, 'None' indicates
        that the severity does not matter.

        'message' -- For an emitted diagnostic, a string indicating
        the message produced by the compiler.  For an expected
        diagnostic, a string giving a regular expression indicating
        the message that might be emitted.  For an expected
        diagnostic, 'None' indicates that the message does not
        matter."""

        self.source_position = source_position
        self.severity = severity
        self.message = message

    def __str__(self):
        """Return an informal representation of this 'Diagnostic'.

        returns -- A string representing this 'Diagnostic'."""

        if self.source_position:
            source_position_string = str(self.source_position)
        else:
            source_position_string = "<no source position>"

        if self.severity:
            severity_string = self.severity
        else:
            severity_string = "<no severity>"

        if self.message:
            message_string = self.message
        else:
            message_string = "<no message>"

        return '%s: %s: %s' % (source_position_string,
                               severity_string,
                               message_string)

########################################################################
# Compilers
########################################################################

class GCC(Compiler):
    """A 'GCC' is a GNU Compiler Collection compiler."""

    _severities = [ 'warning', 'error' ]
    """The diagnostic severities generated by the compiler.  Order
    matters; the order given here is the order that the
    '_severity_regexps' will be tried."""

    _severity_regexps = {
        'warning' :
          re.compile('^(?P<file>[^:]*):((?P<line>[^:]*):)?'
                     '(\s*(?P<column>[0-9]+):)? '
                     'warning: (?P<message>.*)$'),
        'error':
          re.compile('^(?P<file>[^:]*):((?P<line>[^:]*):)?'
                     '(\s*(?P<column>[0-9]+):)? '
                     '(?P<message>.*)$')
        }
    """A map from severities to compiled regular expressions.  If the
    regular expression matches a line in the compiler output, then that
    line indicates a diagnostic with the indicated severity."""

    _internal_error_regexp = re.compile('Internal (compiler )?error')
    """A compiled regular expression.  When an error message is matched
    by this regular expression, the error message indicates an
    internal error in the compiler."""

    MODE_PRECOMPILE = "precompile"
    """Precompile a header file."""

    modes = Compiler.modes + [MODE_PRECOMPILE]

    def ParseOutput(self, output, ignore_regexps = ()):
        """Return the 'Diagnostic's indicated in the 'output'.

        'output' -- A string giving the output from the compiler.

        'ignore_regexps' -- A sequence of regular expressions.  If a
        diagnostic message matches one of these regular expressions,
        it will be ignored.

        returns -- A list of 'Diagnostic's corresponding to the
        messages indicated in 'output', in the order that they were
        emitted."""

        # Assume there were no diagnostics.
        diagnostics = []
        # Create a file object containing the 'output'.
        f = StringIO.StringIO(output)
        # Reall all of the output, line by line.
        for line in f.readlines():
            for severity in self._severities:
                match = self._severity_regexps[severity].match(line)
                # If it does not look like an error message, skip it.
                if not match:
                    continue

                # Some error messages are ignored.
                ignore = 0
                for ignore_regexp in ignore_regexps:
                    if ignore_regexp.match(match.group()):
                        ignore = 1
                        break
                if ignore:
                    continue

                # An internal error is an error that indicates that
                # the compiler crashed.
                message = match.group('message')
                if (severity == 'error'
                    and self._internal_error_regexp.search(message)):
                    severity = 'internal_error'

                # If there is no line number, then we will not be
                # able to convert it to an integer.
                try:
                    line_number = int(match.group('line'))
                except:
                    line_number = 0

                # See if there is a column number.
                try:
                    column_number = int(match.group('column'))
                except:
                    column_number = 0

                source_position = SourcePosition(match.group('file'),
                                                 line_number,
                                                 column_number)
                diagnostic = Diagnostic(source_position,
                                        severity,
                                        message)
                diagnostics.append(diagnostic)
                break

        return diagnostics

class EDG(Compiler):
    """An 'EDG' is an Edison Design Group compiler."""

    __diagnostic_regexp = re.compile('^"(?P<file>.*)", line (?P<line>.*): '
                                     '(?P<severity>.*): (?P<message>.*)$')

    def ParseOutput(self, output, ignore_regexps = ()):
        """Return the 'Diagnostic's indicated in the 'output'.

        'output' -- A string giving the output from the compiler.

        'ignore_regexps' -- A sequence of regular expressions.  If a
        diagnostic message matches one of these regular expressions,
        it will be ignored.

        returns -- A list of 'Diagnostic's corresponding to the
        messages indicated in 'output', in the order that they were
        emitted."""

        # Assume there were no diagnostics.
        diagnostics = []
        # Create a file object containing the 'output'.
        f = StringIO.StringIO(output)
        # Reall all of the output, line by line.
        for line in f.readlines():
            match = self.__diagnostic_regexp.match(line)
            if match:
                source_position = SourcePosition(match.group('file'),
                                                 int(match.group('line')),
                                                 0)
                diagnostic = Diagnostic(source_position,
                                        match.group('severity'),
                                        match.group('message'))
                diagnostics.append(diagnostic)

        return diagnostics


user_input = input("Give me Text......\n")
print("Yup!")
replace_word = input("Tell me which Word You want to Replace\n")
count_word = user_input.count(replace_word)
print(
  f"OK I have scanned your text your text has {count_word} time {replace_word}"
)
new_word=input(f"Tell me new word for {replace_word}\n")
print("I am going to replace that ☺\n")
final_text=user_input.replace(replace_word,new_word)
print(final_text)


import sys
_module = sys.modules[__name__]
del sys
models = _module
models = _module
snres_discriminator = _module
snres_generator = _module
src = _module
functions = _module
max_sv = _module
snlayers = _module
snconv2d = _module
snlinear = _module
test = _module
train = _module

from _paritybench_helpers import _mock_config, patch_functional
from unittest.mock import mock_open, MagicMock
from torch.autograd import Function
from torch.nn import Module
import abc, collections, copy, enum, functools, inspect, itertools, logging, math, matplotlib, numbers, numpy, pandas, queue, random, re, scipy, sklearn, string, tensorflow, time, torch, torchaudio, torchtext, torchvision, types, typing, uuid, warnings
import numpy as np
from torch import Tensor
patch_functional()
open = mock_open()
yaml = logging = sys = argparse = MagicMock()
ArgumentParser = argparse.ArgumentParser
_global_config = args = argv = cfg = config = params = _mock_config()
argparse.ArgumentParser.return_value.parse_args.return_value = _global_config
yaml.load.return_value = _global_config
sys.argv = _global_config
__version__ = '1.0.0'
xrange = range
wraps = functools.wraps

import torch.nn as nn

import torch

from torch.nn.modules import conv

from torch.nn.modules import Linear

import torch.nn.functional as F

from torch.nn.modules.utils import _pair

import torch.optim as optim

from torchvision import datasets

from torchvision import transforms

import torchvision.utils as vutils

from torch.autograd import Variable

import torch.utils.data

from torch.nn.modules.utils import _triple

import torch.backends.cudnn as cudnn

import random

import numpy as np

import matplotlib.pyplot as plt

class _netG(nn.Module):

    def __init__(self, nz, nc, ngf):
        super(_netG, self).__init__()
        self.convT1 = nn.Sequential(nn.ConvTranspose2d(nz, ngf * 4, 4, 1, 0, bias=False), nn.BatchNorm2d(ngf * 4), nn.ReLU(True))
        self.convT2 = nn.Sequential(nn.ConvTranspose2d(10, ngf * 4, 4, 1, 0, bias=False), nn.BatchNorm2d(ngf * 4), nn.ReLU(True))
        self.main = nn.Sequential(nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False), nn.BatchNorm2d(ngf * 4), nn.ReLU(True), nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False), nn.BatchNorm2d(ngf * 2), nn.ReLU(True), nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False), nn.BatchNorm2d(ngf), nn.ReLU(True), nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False), nn.Tanh())

    def forward(self, input, input_c):
        out1 = self.convT1(input)
        out2 = self.convT2(input_c)
        output = torch.cat([out1, out2], 1)
        output = self.main(output)
        return output

def _l2normalize(v, eps=1e-12):
    return v / ((v ** 2).sum() ** 0.5 + eps)

def max_singular_value(W, u=None, Ip=1):
    """
    power iteration for weight parameter
    """
    if u is None:
        u = torch.FloatTensor(1, W.size(0)).normal_(0, 1)
    _u = u
    for _ in range(Ip):
        _v = _l2normalize(torch.matmul(_u, W.data), eps=1e-12)
        _u = _l2normalize(torch.matmul(_v, torch.transpose(W.data, 0, 1)), eps=1e-12)
    sigma = torch.matmul(torch.matmul(_v, torch.transpose(W.data, 0, 1)), torch.transpose(_u, 0, 1))
    return sigma, _v

class SNConv2d(conv._ConvNd):

    def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True):
        kernel_size = _pair(kernel_size)
        stride = _pair(stride)
        padding = _pair(padding)
        dilation = _pair(dilation)
        super(SNConv2d, self).__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, False, _pair(0), groups, bias)

    def forward(self, input):
        w_mat = self.weight.view(self.weight.size(0), -1)
        sigma, _ = max_singular_value(w_mat)
        self.weight.data = self.weight.data / sigma
        return F.conv2d(input, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)

class _netD(nn.Module):

    def __init__(self, nc, ndf):
        super(_netD, self).__init__()
        self.conv1_1 = SNConv2d(nc, ndf / 2, 3, 1, 1, bias=False)
        self.conv1_2 = SNConv2d(10, ndf / 2, 3, 1, 1, bias=False)
        self.lrelu = nn.LeakyReLU(0.2, inplace=True)
        self.main = nn.Sequential(SNConv2d(ndf, ndf, 4, 2, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), SNConv2d(ndf, ndf * 2, 3, 1, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), SNConv2d(ndf * 2, ndf * 2, 4, 2, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), SNConv2d(ndf * 2, ndf * 4, 3, 1, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), SNConv2d(ndf * 4, ndf * 4, 4, 2, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), SNConv2d(ndf * 4, ndf * 8, 3, 1, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), SNConv2d(ndf * 8, ndf * 8, 4, 2, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), SNConv2d(ndf * 8, 1, 4, 1, 0, bias=False))

    def forward(self, input, input_c):
        out1 = self.lrelu(self.conv1_1(input))
        out2 = self.lrelu(self.conv1_2(input_c))
        output = torch.cat([out1, out2], 1)
        output = self.main(output)
        return output.view(-1, 1).squeeze(1)

class ResBlock(nn.Module):

    def __init__(self, in_channels, out_channels, hidden_channels=None, upsample=False):
        super(ResBlock, self).__init__()
        hidden_channels = in_channels
        self.upsample = upsample
        self.conv1 = nn.Conv2d(in_channels, hidden_channels, kernel_size=3, padding=1)
        self.conv2 = nn.Conv2d(hidden_channels, out_channels, kernel_size=3, padding=1)
        self.conv_sc = nn.Conv2d(in_channels, out_channels, kernel_size=1, padding=0)
        self.upsampling = nn.UpsamplingBilinear2d(scale_factor=2)
        self.bn1 = nn.BatchNorm2d(in_channels)
        self.bn2 = nn.BatchNorm2d(hidden_channels)
        self.relu = nn.ReLU()

    def forward_residual_connect(self, input):
        out = self.conv_sc(input)
        if self.upsample:
            out = self.upsampling(out)
        return out

    def forward(self, input):
        out = self.relu(self.bn1(input))
        out = self.conv1(out)
        if self.upsample:
            out = self.upsampling(out)
        out = self.relu(self.bn2(out))
        out = self.conv2(out)
        out_res = self.forward_residual_connect(input)
        return out + out_res

class OptimizedBlock(nn.Module):

    def __init__(self, in_channels, out_channels):
        super(OptimizedBlock, self).__init__()
        self.res_block = self.make_res_block(in_channels, out_channels)
        self.residual_connect = self.make_residual_connect(in_channels, out_channels)

    def make_res_block(self, in_channels, out_channels):
        model = []
        model += [SNConv2d(in_channels, out_channels, kernel_size=3, padding=1)]
        model += [nn.ReLU()]
        model += [SNConv2d(out_channels, out_channels, kernel_size=3, padding=1)]
        model += [nn.AvgPool2d(2)]
        return nn.Sequential(*model)

    def make_residual_connect(self, in_channels, out_channels):
        model = []
        model += [SNConv2d(in_channels, out_channels, kernel_size=1, padding=0)]
        model += [nn.AvgPool2d(2)]
        return nn.Sequential(*model)

    def forward(self, input):
        return self.res_block(input) + self.residual_connect(input)

class SNLinear(Linear):
    """Applies a linear transformation to the incoming data: :math:`y = Ax + b`
       Args:
           in_features: size of each input sample
           out_features: size of each output sample
           bias: If set to False, the layer will not learn an additive bias.
               Default: ``True``
       Shape:
           - Input: :math:`(N, *, in\\_features)` where :math:`*` means any number of
             additional dimensions
           - Output: :math:`(N, *, out\\_features)` where all but the last dimension
             are the same shape as the input.
       Attributes:
           weight: the learnable weights of the module of shape
               `(out_features x in_features)`
           bias:   the learnable bias of the module of shape `(out_features)`

           W(Tensor): Spectrally normalized weight

           u (Tensor): the right largest singular value of W.
       """

    def __init__(self, in_features, out_features, bias=True):
        super(SNLinear, self).__init__(in_features, out_features, bias)
        self.register_buffer('u', torch.Tensor(1, out_features).normal_())

    @property
    def W_(self):
        w_mat = self.weight.view(self.weight.size(0), -1)
        sigma, _u = max_singular_value(w_mat, self.u)
        self.u.copy_(_u)
        return self.weight / sigma

    def forward(self, input):
        return F.linear(input, self.W_, self.bias)

class SNResDiscriminator(nn.Module):

    def __init__(self, ndf=64, ndlayers=4):
        super(SNResDiscriminator, self).__init__()
        self.res_d = self.make_model(ndf, ndlayers)
        self.fc = nn.Sequential(SNLinear(ndf * 16, 1), nn.Sigmoid())

    def make_model(self, ndf, ndlayers):
        model = []
        model += [OptimizedBlock(3, ndf)]
        tndf = ndf
        for i in range(ndlayers):
            model += [ResBlock(tndf, tndf * 2, downsample=True)]
            tndf *= 2
        model += [nn.ReLU()]
        return nn.Sequential(*model)

    def forward(self, input):
        out = self.res_d(input)
        out = F.avg_pool2d(out, out.size(3), stride=1)
        out = out.view(-1, 1024)
        return self.fc(out)

class SNResGenerator(nn.Module):

    def __init__(self, ngf, z=128, nlayers=4):
        super(SNResGenerator, self).__init__()
        self.input_layer = nn.Linear(z, 4 ** 2 * ngf * 16)
        self.generator = self.make_model(ngf, nlayers)

    def make_model(self, ngf, nlayers):
        model = []
        tngf = ngf * 16
        for i in range(nlayers):
            model += [ResBlock(tngf, tngf / 2, upsample=True)]
            tngf /= 2
        model += [nn.BatchNorm2d(ngf)]
        model += [nn.ReLU()]
        model += [nn.Conv2d(ngf, 3, kernel_size=3, stride=1, padding=1)]
        model += [nn.Tanh()]
        return nn.Sequential(*model)

    def forward(self, z):
        out = self.input_layer(z)
        out = out.view(z.size(0), -1, 4, 4)
        out = self.generator(out)
        return out

import torch
from torch.nn import MSELoss, ReLU
from _paritybench_helpers import _mock_config, _mock_layer, _paritybench_base, _fails_compile

TESTCASES = [
    # (nn.Module, init_args, forward_args, jit_compiles)
    (ResBlock,
     lambda: ([], {'in_channels': 4, 'out_channels': 4}),
     lambda: ([torch.rand([4, 4, 4, 4])], {}),
     True),
    (SNLinear,
     lambda: ([], {'in_features': 4, 'out_features': 4}),
     lambda: ([torch.rand([4, 4, 4, 4])], {}),
     False),
    (_netG,
     lambda: ([], {'nz': 4, 'nc': 4, 'ngf': 4}),
     lambda: ([torch.rand([4, 4, 64, 64]), torch.rand([4, 10, 64, 64])], {}),
     True),
]

class Test_godisboy_SN_GAN(_paritybench_base):
    def test_000(self):
        self._check(*TESTCASES[0])

    def test_001(self):
        self._check(*TESTCASES[1])

    def test_002(self):
        self._check(*TESTCASES[2])


import matplotlib.pyplot as plt
import numpy as np
from math import sqrt
import sys

def fpga_plot(casename):
    filename_arch = '../benchmarks/' + casename + '.arch'
    filename_fp = '../outputs/' + casename + '.floorplan'
    fa = open(filename_arch, 'r')
    line = fa.readline()
    line = line.split()
    r, c, s, d = int(line[0]), int(line[1]), int(line[2]), int(line[3])
    fig = plt.figure(figsize=(20, 20))
    ax = fig.add_subplot(111)

    for i in range(s, c, d):
        for j in range(0, r, 3):
            mux = plt.Rectangle((i, j), 1, 3, edgecolor='white',
                                facecolor='lightblue', fill=True, linewidth=2)
            ax.add_patch(mux)

    fp = open(filename_fp, 'r')
    for line in fp.readlines():
        line = line.split()
        if len(line) == 1:
            break
        name, x, y, w, h = int(line[0]), int(line[1]), int(
            line[2]), int(line[3]), int(line[4])
        module = plt.Rectangle((x, y), w, h, edgecolor='grey',
                               facecolor='pink', fill=True, linewidth=2, alpha=0.8)
        ax.add_patch(module)
        ax.text(x, y+1, "M"+line[0], fontsize=10)
    fp.close()

    fixedline = plt.Rectangle((0, 0), c, r, edgecolor='red',
                              facecolor='green', fill=False, linewidth=2, linestyle='dashed')
    ax.add_patch(fixedline)
    ax.text(0, -5, "Fixed-outline(W, H): (" +
            str(c)+", "+str(r)+")", fontsize=20)

    plt.plot(range(r), alpha=0)
    plt.axis('off')
    plt.savefig('../outputs/'+casename + '.png')
    print(casename + '.png '+'is saved.')
    plt.show()

if __name__ == '__main__':
    casename = sys.argv[1]
    fpga_plot(casename)


def shifrovka(original_text):
    #Функція приймає текст шифрування
    text=list(original_text.lower())
    number=[]
    #Кожна буква перетворюється на номер у таблиці символів
    for element in text:
        number.append(ord(element))

    #До номеру букви додається +2, щоб виконати переміщення до іншої букві у абетці
    for i in range(0,len(number)):
        if number[i]!=32:
            number[i]=number[i]+2
    #Номера перетворюється на значення букв у таблиці за цими номерами
    for j in range(0,len(number)):
        number[j]=chr(number[j])
    #Вивід результату
    result=''
    for k in range (0, len(number)):

        result+=number[k]
    print(result)

shifrovka("Лузанов Евгений Олександрович")


import os
import six
import imageio
from PIL import Image
import numpy as np

from .images import ImagesDataset
from . import datasets_root
from . import util
from .utils import nputil
from .utils import imutil

try:
    # Python 2
    from itertools import izip
except ImportError:
    # Python 3
    izip = zip

from tensorflow.contrib.slim.nets import inception, vgg

class coco_animals(ImagesDataset):
    def __init__(self):
        ImagesDataset.__init__(self)
        self.dataset_name='coco-animals'
        self.source_url='http://cs231n.stanford.edu/'
        self.source_files=['coco-animals.zip']
        self.dataset_home=os.path.join(datasets_root,self.dataset_name)
        self.height=None
        self.width=None
        self.depth=None

        self.x_layout =  imutil.LAYOUT_NCHW
        self.x_layout_file = imutil.LAYOUT_NCHW

        #self.x_shape = 'NCHW' ## other alternates are NHW and NHCW
        self.n_classes = 8

    def load_data(self,force=False, shuffle=True, x_is_images=False):
        self.dataset_home=os.path.join(datasets_root,self.dataset_name)

        self.downloaded_files=util.download_dataset(source_url=self.source_url,
                                                    source_files=self.source_files,
                                                    dest_dir = self.dataset_home,
                                                    force=force,
                                                    extract=True)

        # the archive contains the name of the coco-animals folder, hence this temporary fix for now
        self.dataset_home=os.path.join(self.dataset_home,self.dataset_name)
#        print('Extracting ',self.downloaded_files[0])
        ilabels = os.listdir(os.path.join(self.dataset_home,'train'))

        label2id = dict(zip(ilabels, range(len(ilabels))))
        #print(label2id)

        self.label2id=label2id
        id2label = dict(zip(label2id.values(), label2id.keys()))
        self.id2label = id2label

        n_train = 800
        n_valid = 200

        x_train_files=[]
        y_train = np.zeros((n_train,), dtype=np.uint8)
        x_valid_files=[]
        y_valid=np.zeros((n_valid,), dtype=np.uint8)
        #print(ilabels)

        for i in range(self.n_classes):
            label = id2label[i]
            ifolder = os.path.join(self.dataset_home,'train',label)
            #print(os.listdir(ifolder))
            files = [name for name in os.listdir(ifolder) if name.endswith('.jpg')]
            for f in files:
                x_train_files.append(os.path.join(ifolder,f))
                #y_train.append(labels2id[label])
            y_train[i * (n_train // self.n_classes): (i+1) * (n_train // self.n_classes)] = i

            ifolder = os.path.join(self.dataset_home,'val',label)
            files = [name for name in os.listdir(ifolder) if name.endswith('.jpg')]
            for f in files:
                x_valid_files.append(os.path.join(ifolder,f))
                #y_val.append(labels2id[label])
            y_valid[i * (n_valid // self.n_classes): (i+1) * (n_valid // self.n_classes)] = i

        if shuffle:
            x_train_files, y_train = self.shuffle_xy(x_train_files,y_train)

        if x_is_images:
            x_train = self.load_images(x_train_files)
            x_valid = self.load_images(x_valid_files)
        else:
            x_train = x_train_files
            x_valid = x_train_files
        self.x_is_images=x_is_images

        self.part['X_train']=x_train
        self.part['Y_train']=y_train

        self.part['X_valid']=x_valid
        self.part['Y_valid']=y_valid

        return x_train, y_train, x_valid, y_valid

    def preprocess_for_vgg(self,incoming):
        if isinstance(incoming, six.string_types):
            img = self.load_image(incoming)
        elif isinstance(incoming, imageio.core.util.Image ):
            img = Image.fromarray(incoming)
        else: #
            img=incoming

        #print(type(img))

        img_size = vgg.vgg_16.default_image_size

        height = img_size
        width = img_size

        img = self.resize_image(img,height,width)
        img = self.pil_to_nparray(img)
        if len(img.shape)==2:   # greyscale or no channels then add three channels
            h=img.shape[0]
            w=img.shape[1]
            img = np.dstack([img]*3)

        means = np.array([[[123.68, 116.78, 103.94]]]) #shape=[1, 1, 3]
        try:
            img = img - means
        except Exception as ex:
            print('Error preprocessing ',incoming)
            print(ex)

        return img

    def preprocess_for_inception(self,incoming):

        img_size = inception.inception_v3.default_image_size

        height = img_size
        width = img_size

        if isinstance(incoming, six.string_types):
            img = self.load_image(incoming)
        elif isinstance(incoming, imageio.core.util.Image ):
            img = Image.fromarray(incoming)
        else: #
            img=incoming

        #print(type(img))

        img = self.resize_image(img,height,width)
        img = self.pil_to_nparray(img)
        if len(img.shape)==2:   # greyscale or no channels then add three channels
            h=img.shape[0]
            w=img.shape[1]
            img = np.dstack([img]*3)

        img = ((img/255.0) - 0.5) * 2.0

        return img


import requests
import logging
import jq
from deckster.common.core import update_key_image, update_label_display

def main(deck, key, pressed):
    logger = logging.getLogger("deckster")
    args = key.args
    url = args["url"]
    if "json_data" in args:
        json_data = args["json_data"]
    else:
        json_data = None
    if "headers" in args:
        headers = args["headers"]
    else:
        headers = None

    try:
        logger.debug(f"Trying GET request for {url} with parameters: {json_data} and headers: {headers}.")
        res = requests.get(url, params = json_data, headers = headers)
    except Exception as e:
        logger.error(f"Request to {url} failed: {e}")

    if not res.status_code in args["status_codes"]:
        logger.error(f"Status code returned {res.status_code} not in expected codes.")
        return

    d = jq.compile(args["json_parse"]).input(res.json()).first()
    logger.info(f"Parsed result: '{d}' from {url}")
    if "send_to_display" in key.args or "send_to_label" in key.args:
            to_label = "send_to_label" in key.args
            logger.info(f"Sending GET result to {'label' if to_label else 'display'} for key {key.key}.")
            if to_label:
                key.label = str(d)
            else:
                key.display = str(d)
            update_label_display(key, True if "send_to_label" in key.args else False)
            update_key_image(deck, key, pressed)