# -*- coding:utf-8 -*-
from absl import flags
import json
import math
import pickle as cPickle
import random
import sys

import numpy as np
import tensorflow as tf
import modeling, xlnet, model_utils
import sentencepiece as spm
from prepro_utils import preprocess_text, encode_ids
from tensorflow.contrib import rnn, crf
from tensorflow.contrib.crf import crf_log_likelihood
from tensorflow.contrib.layers.python.layers import initializers

from utils import f1_score, format_result, get_tags, format_tags, new_f1_score

# Model
flags.DEFINE_float("dropout", default=0.1,
      help="Dropout rate.")
flags.DEFINE_float("dropatt", default=0.1,
      help="Attention dropout rate.")
flags.DEFINE_integer("clamp_len", default=-1,
      help="Clamp length.")
flags.DEFINE_bool("use_bfloat16", default=False,
      help="Whether to use bfloat16.")

# Parameter initialization
flags.DEFINE_enum("init", default="normal",
                  enum_values=["normal", "uniform"],
                  help="Initialization method.")
flags.DEFINE_float("init_std", default=0.02,
                   help="Initialization std when init is normal.")
flags.DEFINE_float("init_range", default=0.1,
                   help="Initialization std when init is uniform.")

# TPUs and machines
flags.DEFINE_bool("use_tpu", default=False, help="whether to use TPU.")

# Training
flags.DEFINE_integer("train_batch_size", default=1,
                     help="batch size for training")
flags.DEFINE_integer("train_steps", default=8000,
                     help="Number of training steps")
flags.DEFINE_integer("warmup_steps", default=0, help="number of warmup steps")
flags.DEFINE_integer("save_steps", default=None,
                     help="Save the model for every save_steps. "
                     "If None, not to save any model.")
flags.DEFINE_integer("max_save", default=5,
                     help="Max number of checkpoints to save. "
                     "Use 0 to save all.")
flags.DEFINE_integer("shuffle_buffer", default=2048,
                     help="Buffer size used for shuffle.")

# Optimization
flags.DEFINE_float("learning_rate", default=3e-5, help="initial learning rate")
flags.DEFINE_float("min_lr_ratio", default=0.0,
                   help="min lr ratio for cos decay.")
flags.DEFINE_float("clip", default=1.0, help="Gradient clipping")
flags.DEFINE_float("weight_decay", default=0.00, help="Weight decay rate")
flags.DEFINE_float("adam_epsilon", default=1e-6, help="Adam epsilon")
flags.DEFINE_string("decay_method", default="poly", help="poly or cos")
flags.DEFINE_float("lr_layer_decay_rate", default=0.75,
                   help="Top layer: lr[L] = FLAGS.learning_rate."
                   "Lower layers: lr[l-1] = lr[l] * lr_layer_decay_rate.")

flags.DEFINE_string("data_dir", default="data/", help="tran, dev and test data dir", )
flags.DEFINE_string("xlnet_config", default="chinese_xlnet_base_L-12_H-768_A-12/xlnet_config.json", help="xlnet config file dir")
flags.DEFINE_string("init_checkpoint", default="chinese_xlnet_base_L-12_H-768_A-12/xlnet_model.ckpt", help="xlnet model init checkpoint")
flags.DEFINE_string("spm", default="chinese_xlnet_base_L-12_H-768_A-12/spiece.model", help="spiece model file")
flags.DEFINE_string("entry", default="train", help="operation")

FLAGS = flags.FLAGS

class Model():
    def __init__(self):
        self.nums_tags = 4
        self.lstm_dim = 128
        self.embedding_size = 50
        self.max_epoch = 10
        self.global_steps = tf.Variable(0, trainable=False)
        self.best_dev_f1 = tf.Variable(0.0, trainable=False)
        self.checkpoint_dir = "./model/"
        self.checkpoint_path = "./model/ner.ckpt"
        self.initializer = initializers.xavier_initializer()

        self.is_training = True if FLAGS.entry=="train" else False

    def __creat_model(self):

        # xlnet embbeding layer
        self._init_xlnet_placeholder()
        self.xlnet_layer()

        # bi-Lstm layer
        self.biLSTM_layer()

        # logits_layer
        self.logits_layer()

        # loss_layer
        self.loss_layer()

        # crf_layer
        self.crf_layer()

        # optimizer_layer
        self.xlnet_optimizer_layer()

    def _init_xlnet_placeholder(self):
        self.input_ids = tf.placeholder(
            dtype=tf.int32,
            shape=[None, None],
            name="xlnet_input_ids"
        )
        self.input_mask = tf.placeholder(
            dtype=tf.float32,
            shape=[None, None],
            name="xlnet_input_mask"
        )
        self.segment_ids = tf.placeholder(
            dtype=tf.int32,
            shape=[None, None],
            name="xlnet_segment_ids"
        )
        self.targets = tf.placeholder(
            dtype=tf.int32,
            shape=[None, None],
            name="xlnet_targets"
        )

        self.dropout = tf.placeholder(
            dtype=tf.float32,
            shape=None,
            name="xlnet_dropout"
        )
        used = tf.sign(tf.abs(self.input_ids))
        length = tf.reduce_sum(used, reduction_indices=1)
        self.length = tf.cast(length, tf.int32)
        self.nums_steps = tf.shape(self.input_ids)[-1]

    def xlnet_layer(self):
        xlnet_config = xlnet.XLNetConfig(json_path = FLAGS.xlnet_config)
        run_config = xlnet.create_run_config(self.is_training, True, FLAGS)

        xlnet_model = xlnet.XLNetModel(
            xlnet_config = xlnet_config,
            run_config = run_config,
            input_ids = self.input_ids,
            seg_ids = self.segment_ids,
            input_mask = self.input_mask)

        self.embedded = xlnet_model.get_sequence_output()
        self.model_inputs = tf.nn.dropout(
            self.embedded, self.dropout
        )

    def embedding_layer(self):
        with tf.variable_scope("embedding_layer") as scope:
            sqart3 = math.sqrt(3)

            self.embedding_matrix = tf.get_variable(
                name="embedding_matrix",
                shape=[self.input_size, self.embedding_size],
                initializer=self.initializer,
                dtype=tf.float32,
            )

            self.embedded = tf.nn.embedding_lookup(
                self.embedding_matrix, self.inputs
            )

            self.model_inputs = tf.nn.dropout(
                self.embedded, self.dropout
            )

    def biLSTM_layer(self):
        with tf.variable_scope("bi-LSTM") as scope:
            lstm_cell = {}
            for direction in ["forward", "backward"]:
                with tf.variable_scope(direction):
                    lstm_cell[direction] = rnn.LSTMCell(
                        num_units=self.lstm_dim,
                    )

            outputs, final_states = tf.nn.bidirectional_dynamic_rnn(
                cell_fw=lstm_cell['forward'],
                cell_bw=lstm_cell['backward'],
                inputs=self.model_inputs,
                sequence_length=self.length,
                dtype=tf.float32,
            )
            self.lstm_outputs = tf.concat(outputs, axis=2)

    def logits_layer(self):
        with tf.variable_scope("hidden"):
            w = tf.get_variable("W", shape=[self.lstm_dim*2, self.lstm_dim],
                                dtype=tf.float32, initializer=self.initializer
                                )
            b = tf.get_variable("b", shape=[self.lstm_dim], dtype=tf.float32,
                                initializer=self.initializer
                                )

            output = tf.reshape(self.lstm_outputs, shape=[-1, self.lstm_dim*2])
            hidden = tf.tanh(tf.nn.xw_plus_b(output, w, b))
            self.hidden = hidden

        with tf.variable_scope("logits"):
            w = tf.get_variable("W", shape=[self.lstm_dim, self.nums_tags],
                                initializer=self.initializer, dtype=tf.float32
                                )
            self.test_w = w
            b = tf.get_variable("b", shape=[self.nums_tags], dtype=tf.float32)
            self.test_b = b
            pred = tf.nn.xw_plus_b(hidden, w, b)
            self.logits = tf.reshape(
                pred, shape=[-1, self.nums_steps, self.nums_tags])

    def loss_layer(self):
        with tf.variable_scope("loss_layer"):
            logits = self.logits
            targets = self.targets

            self.trans = tf.get_variable(
                "transitions",
                shape=[self.nums_tags, self.nums_tags],
                initializer=self.initializer
            )

            log_likelihood, self.trans = crf_log_likelihood(
                inputs=logits,
                tag_indices=targets,
                transition_params=self.trans,
                sequence_lengths=self.length
            )
            self.loss = tf.reduce_mean(-log_likelihood)

    def crf_layer(self):
        # CRF decode, pred_ids 是一条最大概率的标注路径
        self.pred_ids, _ = crf.crf_decode(potentials=self.logits, transition_params=self.trans, sequence_length=self.length)

    def xlnet_optimizer_layer(self):
        correct_prediction = tf.equal(
            tf.argmax(self.logits, 2), tf.cast(self.targets, tf.int64))
        self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        FLAGS.train_steps = int(
            self.train_length / FLAGS.train_batch_size * self.max_epoch)
        FLAGS.warmup_steps = int(FLAGS.train_steps * 0.1)

        self.train_op, self.learning_rate, _ = model_utils.get_train_op(FLAGS, self.loss)
        self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=5)

    def xlnet_step(self, sess, batch):
        try:
            ntokens, tag_ids, inputs_ids, segment_ids, input_mask = zip(*batch)
        except ValueError:
            print("ValueError: not enough values to unpack")
            return 0, 0, 0, 0, 0

        feed = {
            self.input_ids: inputs_ids,
            self.targets: tag_ids,
            self.segment_ids: segment_ids,
            self.input_mask: input_mask,
            self.dropout: 0.5
        }
        embedding, global_steps, loss, _, logits, acc, length = sess.run([self.embedded, self.global_steps, self.loss, self.train_op, self.logits, self.accuracy, self.length], feed_dict=feed)
        return global_steps, loss, logits, acc, length

    def train(self):
        from xlnet_data_utils import XLNetDataUtils
        sp_model = spm.SentencePieceProcessor()
        sp_model.Load(FLAGS.spm)

        self.train_data = XLNetDataUtils(sp_model, batch_size=8)
        self.dev_data = XLNetDataUtils(sp_model, batch_size=8)
        self.dev_batch = self.dev_data.iteration()

        data = {
            "batch_size": self.train_data.batch_size,
            "input_size": self.train_data.input_size,
            "vocab": self.train_data.vocab,
            "tag_map": self.train_data.tag_map,
        }

        f = open("model/data_map.pkl", "wb")
        cPickle.dump(data, f)
        f.close()
        self.batch_size = self.train_data.batch_size
        self.nums_tags = len(self.train_data.tag_map.keys())
        self.tag_map = self.train_data.tag_map
        self.train_length = len(self.train_data.data)

        # save vocab
        print("-"*50)
        print("train data:\t", self.train_length)
        print("nums of tags:\t", self.nums_tags)

        self.__creat_model()
        with tf.Session() as sess:
            with tf.device("/gpu:0"):
                ckpt = tf.train.get_checkpoint_state(self.checkpoint_dir)
                if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
                    print("restore model")
                    self.saver.restore(sess, ckpt.model_checkpoint_path)
                else:
                    sess.run(tf.global_variables_initializer())

                    tvars = tf.trainable_variables()
                    (assignment_map, initialized_variable_names) = model_utils.get_assignment_map_from_checkpoint(tvars, FLAGS.init_checkpoint)
                    tf.train.init_from_checkpoint(FLAGS.init_checkpoint, assignment_map)
                    for var in tvars:
                        init_string = ""
                        if var.name in initialized_variable_names:
                            init_string = ", *INIT_FROM_CKPT*"
                        print("  name = %s, shape = %s%s", var.name, var.shape,
                                        init_string)

                for i in range(self.max_epoch):
                    print("-"*50)
                    print("epoch {}".format(i))

                    steps = 0
                    for batch in self.train_data.get_batch():
                        steps += 1
                        global_steps, loss, logits, acc, length = self.xlnet_step(sess, batch)
                        if steps % 1 == 0:
                            print("[->] step {}/{}\tloss {:.2f}\tacc {:.2f}".format(
                                steps, len(self.train_data.batch_data), loss, acc))
                        if steps % 100 == 0:
                            self.xlnet_evaluate(sess, "ORG")
                            self.xlnet_evaluate(sess, "PER")
                            self.xlnet_evaluate(sess, "LOC")
                        if steps % 1000 == 0:
                            self.saver.save(sess, self.checkpoint_path)
                    self.saver.save(sess, self.checkpoint_path)

    def xlnet_evaluate(self, sess, tag):
        result = []
        trans = self.trans.eval()
        batch = self.dev_batch.__next__()

        ntokens, tag_ids, inputs_ids, segment_ids, input_mask = zip(*batch)
        feed = {
            self.input_ids: inputs_ids,
            self.segment_ids: segment_ids,
            self.targets: tag_ids,
            self.input_mask: input_mask,
            self.dropout: 1
        }
        pre_paths, acc, lengths = sess.run([self.pred_ids, self.accuracy, self.length], feed_dict=feed)

        tar_paths = tag_ids
        recall, precision, f1 = f1_score(tar_paths, pre_paths, tag, self.tag_map)
        best = self.best_dev_f1.eval()
        if f1 > best:
            print("\tnew best f1:")
            print("\trecall {:.2f}\t precision {:.2f}\t f1 {:.2f}".format(recall, precision, f1))
            tf.assign(self.best_dev_f1, f1).eval()

    def prepare_xlnet_pred_data(self, text):
        text.replace('…', '.')
        text.replace('℃', 'C')
        text_preprocessed = preprocess_text(text)
        input_ids = encode_ids(self.sp_model, text_preprocessed)
        input_mask = [1] * len(input_ids)
        segment_ids = [0] * len(input_ids)

        feed = {
            self.input_ids: [input_ids],
            self.segment_ids: [segment_ids],
            self.input_mask: [input_mask],
            self.dropout: 1
        }
        return feed

    def predict(self):
        f = open("model/data_map.pkl", "rb")
        maps = cPickle.load(f)
        f.close()
        self.batch_size = 1
        self.sp_model = spm.SentencePieceProcessor()
        self.sp_model.Load(FLAGS.spm)
        self.train_length = 10

        self.tag_map = maps.get("tag_map", {})
        self.nums_tags = len(self.tag_map.values())
        self.__creat_model()
        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(self.checkpoint_dir)
            if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
                print("[->] restore model")
                self.saver.restore(sess, ckpt.model_checkpoint_path)
            else:
                print("[->] no model, initializing")
                sess.run(tf.global_variables_initializer())

            trans = self.trans.eval()
            while True:
                text = input(" > ")

                feed = self.prepare_xlnet_pred_data(text)

                paths, length = sess.run([self.pred_ids, self.length], feed_dict=feed)

                print(format_tags(paths[0], self.tag_map))
                org = get_tags(paths[0], "ORG", self.tag_map)
                org_entity = format_result(org, text, "ORG")
                per = get_tags(paths[0], "PER", self.tag_map)
                per_entity = format_result(per, text, "PER")
                loc = get_tags(paths[0], "LOC", self.tag_map)
                loc_entity = format_result(loc, text, "LOC")

                resp = org_entity["entities"] + per_entity["entities"] + loc_entity["entities"]
                print(json.dumps(resp, indent=2, ensure_ascii=False))

def main(_):
    model = Model()
    if FLAGS.entry == "train":
        model.train()
    elif FLAGS.entry == "predict":
        model.predict()

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


from collections import defaultdict

def search(start, node, count, nodes):
    global sum_, visited
    if node in visited:
        return
    visited.add(node)
    if node > start and node % start == 0:
        sum_ += count
    for neighbor in nodes[node]:
        search(start, neighbor, count + 1, nodes)

while True:

    try:
        nodes = defaultdict(list)
        n = int(input())
        for _ in range(1, n):
            from_, to_ = map(int, input().strip().split(' '))
            nodes[from_].append(to_)
            nodes[to_].append(from_)

        sum_ = 0
        for i in range(1, n):
            visited = set()
            search(i, i, 1, nodes)

        print(sum_)

    except EOFError:
        break


from __future__ import annotations

import os
import sys

# If python version is <  3.10 skip test
# glob on that version of Python doesnt have root_dir param
if sys.version_info < (3, 10):
    print("Skip test")
    sys.exit(0)

from dynaconf import Dynaconf

settings = Dynaconf(
    environments=True,
    lowercase_read=False,
    load_dotenv=True,
    root_path=os.path.dirname(os.path.abspath(__file__)),
    settings_files=["settings.toml", ".config/*.yaml"],
)

assert settings.LOG_LEVEL == "INFO"
assert settings.DATA_SOURCES == {"NAME": 1}
print(settings.LOG_LEVEL)
print(settings.DATA_SOURCES)


#Question 1
def f1():
    S=input("Enter your string")
    ch=input("Enter your choice\na. Check if its a palindrome\nb. Reverse each word in it\nc. Remove duplicate entries of a word entered")
    if ch=='a':
        if S==S[::-1]:
            print("It is a palindrome")
        else:
            print("It is not a palindrome")
    elif ch=='b':
        T=S.split()
        S2=""
        for i in T:
            S2+=i[::-1]+" "
        print(S2)
    elif ch=='c':
        T=S.split()
        S2=""
        for i in T:
            if i not in S2:
                S2+=i+" "
        print(S2)

#Question 2
def f2():
    L=[]
    while True:
        F=input("Enter student's first name")
        La=input("Enter student's last name")
        P=float(input("Enter percentage"))
        H=float(input("Enter height"))
        L.append([F.upper(),La.upper(),P,H])
        B=input("Do you want to continue entering? 'Y' or 'N'")
        if B=='N'or B=='n':
            break
    L.sort()
    x=1
    for i in L:
        i.insert(0,x)
        x+=1
    for i in L:
        print(i)
    while True:
        ch = int(input("Enter choice:\n1.To sort based on height\n2.To sort based on scores and display top 3"))
        if ch == 1:
            for i in range(len(L)):
                for i in range(len(L) - 1):
                    if L[i][4] > L[i + 1][4]:
                        L[i], L[i + 1] = L[i + 1], L[i]
            print("Roll no.\tFirst Name\tLast name\tPercentage\tHeight")
            for i in L:
                for j in i:
                    print(j, end="\t")
                print()
            break
        elif ch == 2:
            k = 1
            while k < len(L):
                for i in range(k):
                    if L[k][3] > L[i][3]:
                        m = L.pop(k)
                        L.insert(i, m)
                        k = k + 1
                        break
                else:
                    k+=1
            print("Top 3:")
            print("Roll no.\tFirst Name\tLast name\tPercentage\tHeight")
            for i in L[:3]:
                for j in i:
                    print(j, end="\t")
                print()
            break
        else:
            print("Please enter an appropriate option")
#Question 3(Used answer)
def f3():
    ch=int(input("Enter choice:\n1.Input 2 matrices and find their sum\n2.Input a matrix and find its transpose"))
    if ch==1:
        M1=[]

        r=int(input("Enter no. of rows"))
        c=int(input("Enter no. of columns"))
        print("Enter for matrix 1")
        for i in range(r):
            print("Enter for row ",(i+1))
            t=[]
            for j in range(c):
                x=int(input("Enter element "+str(j+1)))
                t.append(x)
            M1.append(t)
        print("m1:")
        for i in M1:
            print(i)
        M2=[]
        print("Enter for matrix 2")
        for i in range(r):
            print("Enter for row ",(i+1))
            t = []
            for j in range(c):
                x = int(input("Enter element " + str(j + 1)))
                t.append(x)
            M2.append(t)
        print("m2:")
        for i in M2:
            print(i)
        M3=[]
        for i in range(r):
            t=[]
            for j in range(c):
                x=M1[i][j]+M2[i][j]
                t.append(x)
            M3.append(t)
        print("Sum of m1 and m2:")
        for i in M3:
            print(i)
    elif ch==2:
        M=[]
        print("Enter matrix")
        r=int(input("Enter no. of rows"))
        c=int(input("Enter no. of columns"))
        for i in range(r):
            print("Enter for row",(i+1))
            t=[]
            for j in range(c):
                x=int(input("Enter element "+str(j+1)))
                t.append(x)
            M.append(t)
        Mt=[]
        for i in range(c):
            t=[]
            for j in range(r):
                t.append(M[j][i])
            Mt.append(t)
        print("Entered matrix:")
        for i in M:
            print(i)
        print("Transpose of the matrix:")
        for i in Mt:
            print(i)
f3()

CBSE Python/class11/Practical Worksheets(Class 11)/Lists.py


from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences

from keras.models import Sequential,Model
from keras.layers import LSTM, Flatten, Dense, Bidirectional,Input,Dropout

from keras import backend as K
from keras.engine.topology import Layer
from keras import initializers,regularizers, constraints
import math

class ModelC:

    def __init__(self):

        pass

    def create_model(self, SEQ_LEN ):

        print("Creating Model ......................")
        model = Sequential()
        model.add(Bidirectional(LSTM(10, return_sequences = True),input_shape = (SEQ_LEN,300)))
        model.add(Bidirectional(LSTM(4)))
        model.add(Dense(8,activation = 'relu'))
        model.add(Dense(2,activation = 'softmax'))
        model.compile(loss='binary_crossentropy',
                    optimizer='adam',
                    metrics=['accuracy'])

        print("Model Created ")
        print(model.summary())

        return model

    def train(self, model, mg, train_df, batch_size, stopWords, emb, SEQ_LEN, preProcessor):

        print("Starting the model training ........................")
        mg = preProcessor.batch_gen(train_df,batch_size,SEQ_LEN,stopWords,emb)
        model.fit_generator(mg, epochs=5,
                            steps_per_epoch=32,
                            verbose=True)

        print("Model Trained")
        return model

    def predict(self, model, mg, testQ, stopWords, emb, SEQ_LEN, preProcessor):

        test_questions = preProcessor.batch_gen_test(testQ,SEQ_LEN,stopWords,emb)

        for ques in test_questions:
            pred = model.predict(ques)

        i = 0

        for prediction in pred:
            print("Question : ",testQ[i])
            i = i+1
            if(prediction[0] >= 0.5):
                print("Question belongs to Political Category")
                print("Confidence : ",prediction[0]*100,"%")
            else:
                print("Question belongs to Movies Category")
                print("Confidence : ",prediction[1]*100,"%")

    def save_model(self, model, name):

        print("Saving the model")
        model.save('data/'+name+'.h5')
        print("Model saved")


import os
import sys
import json

import numpy as np
import tensorflow as tf

"""Tensorflow distributed training example
This code is retrived from https://www.tensorflow.org/tutorials/distribute/multi_worker_with_keras
"""

def mnist_dataset(batch_size):
  (x_train, y_train), _ = tf.keras.datasets.mnist.load_data()
  # The `x` arrays are in uint8 and have values in the [0, 255] range.
  # You need to convert them to float32 with values in the [0, 1] range.
  x_train = x_train / np.float32(255)
  y_train = y_train.astype(np.int64)
  train_dataset = tf.data.Dataset.from_tensor_slices(
      (x_train, y_train)).shuffle(60000).repeat().batch(batch_size)
  return train_dataset

def build_and_compile_cnn_model():
  model = tf.keras.Sequential([
      tf.keras.layers.InputLayer(input_shape=(28, 28)),
      tf.keras.layers.Reshape(target_shape=(28, 28, 1)),
      tf.keras.layers.Conv2D(32, 3, activation='relu'),
      tf.keras.layers.Flatten(),
      tf.keras.layers.Dense(128, activation='relu'),
      tf.keras.layers.Dense(10)
  ])
  model.compile(
      loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
      optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),
      metrics=['accuracy'])
  return model

def main():
    per_worker_batch_size = 64
    tf_config = json.loads(os.environ['TF_CONFIG'])
    num_workers = len(tf_config['cluster']['worker'])

    strategy = tf.distribute.MultiWorkerMirroredStrategy()

    global_batch_size = per_worker_batch_size * num_workers
    multi_worker_dataset = mnist_dataset(global_batch_size)

    with strategy.scope():
        # Model building/compiling need to be within `strategy.scope()`.
        multi_worker_model = build_and_compile_cnn_model()

    multi_worker_model.fit(multi_worker_dataset, epochs=3, steps_per_epoch=70)

if __name__ == "__main__":
    worker_rank = sys.argv[1]
    workers = [*map(lambda x: f"{x}:12345", sys.argv[2:])]
    os.environ["TF_CONFIG"] = json.dumps({
    'cluster': {
        'worker': workers
    },
    'task': {'type': 'worker', 'index': worker_rank}
    })

    print("Hello from Python")
    print(workers, worker_rank)
    print(os.environ["TF_CONFIG"])
    main()


import os.path
from django.conf import settings
from django.conf.urls.defaults import *
from contributable_games.game_views import *

urlpatterns = patterns('',
    url(r'^(?P<game_name>.*)/login/', game_login, name="game_login"),
    url(r'^(?P<game_name>.*)/logout/', game_logout, name="game_logout"),
    url(r'^(?P<game_name>.*)/new_play/', new_play, name="new_play"),
    url(r'^(?P<game_name>.*)/log_action/', log_action, name="log_action"),
    url(r'^(?P<game_name>.*)/set_score/', set_score, name="set_score"),
    url(r'^(?P<game_name>.*)/get_score/', get_score, name="get_score"),
    url(r'^(?P<game_name>.*)/res/(?P<resource_path>.*)',
        serve_game_resource, name="serve_game_static"),
    url(r'^(?P<game_name>.*)/game_file/(?P<game_file_path>.*)',
        serve_game_file, name="serve_game_file"),
    url(r'^(?P<game_name>.*)/school_end_stage/', school_end_stage, name="school_end_stage"),
    url(r'^(?P<game_name>.*)/$', serve_game_page,
        {'page_path':'index.html'}, name="serve_game_index"),
    url(r'^(?P<game_name>.*)/(?P<page_path>.*)',
        serve_game_page, name="serve_game_page"),

)


import os, datetime, time, sys, pickle
from random import randint
import re
import osascript
import requests
import random

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, ElementNotVisibleException, \
    UnexpectedAlertPresentException, WebDriverException
from pip._vendor.distlib.util import proceed
import constants
from commons import myprint

checkout_video_pattern = re.compile("check out.*")
watch_pattern = re.compile("watch.*|video entry bonus|comment.*youtube.*")

is_not_available_pattern = re.compile("sorry.*is not available.*")
has_ended_pattern = re.compile(".*competition.*has.*ended")

def get_time():
    return datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')

#Type4
def type4_click_continue(driver, element, patt=''):
    continue_buttons = element.find_elements_by_css_selector("div > div > form > div.form-actions.center > button")
    if len(continue_buttons) == 0:
        myprint("Continue button not found")
        return False
    click_button = continue_buttons[0]
    try:
        if len(continue_buttons)>=2:
            if continue_buttons[0].text == 'Continue':
                click_button = continue_buttons[0]
                click_button.get_attribute("class").split(' ').index('disabled')
            else:
                click_button = continue_buttons[1]
                click_button.get_attribute("class").split(' ').index('disabled')
        else:
            click_button = continue_buttons[0]
            click_button.get_attribute("class").split(' ').index('disabled')
        myprint("Still disabled")
        return False
    except:
        action = ActionChains(driver)
        action.move_to_element(click_button)
        action.click().perform()
        myprint(patt, 'type4_click_continue: continue clicked')
        return True

#Type5
def type5_click_continue(driver, element, patt = ''):
    continue_buttons = element.find_elements_by_css_selector("div.body-widget > div > div > div > div > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > div:nth-child(2) > div > form > div > span:nth-child(1) > button")
    if len(continue_buttons) == 0:
        return False
    action = ActionChains(driver)
    action.move_to_element(continue_buttons[0])
    action.click().perform()
    myprint(constants.CGREEN + patt, 'type5_click_continue: continue clicked' + constants.CEND)
    return True

video_url_arr = []
with open(f'data/video_page_links.txt') as infile:
    for line in infile:
        video_url_arr.append(str(line))
random.shuffle(video_url_arr)

def load_cookie(driver, path):
    with open(path, 'rb') as cookies_file:
        cookies = pickle.load(cookies_file)
        for cookie in cookies:
            driver.add_cookie(cookie)

def is_logged_in(driver):
    prelogin_status = driver.find_elements_by_css_selector("body > div.body-widget > div > div > div.popup-blocks-container > div > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > div:nth-child(3) > div > span > div:nth-child(2) > div")
    if len(prelogin_status)>0 and prelogin_status[0].text=="Login with:":
        return False

    loggedin_status = driver.find_elements_by_css_selector("body > div.body-widget > div > div > div.popup-blocks-container > div > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > div:nth-child(3) > div > span > div.small-bar.contestant-logged-in.center.ng-scope > div.small-bar--text.ng-binding")
    if len(loggedin_status)>0 and loggedin_status[0].text.split('\n')[0]=="Entering as":
        return True
    return False

def login(driver):
    time.sleep(2)
    login_options = driver.find_elements_by_css_selector("div > div.popup-blocks-container > div > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > div:nth-child(2) > div > form > fieldset.center > div > ul > li")
    for element in login_options:
        children  = element.find_elements_by_css_selector("*")
        for child in children:
            if('facebook-background' in child.get_attribute("class").split(' ')):
                try:
                    element.click()
                    time.sleep(2)
                    # myprint("login: 1st fb login succeeded")
                except Exception:
                    # myprint("1st fb login method failed, trying 2nd way")
                    try:
                        fbs = driver.find_elements_by_css_selector("body > div.body-widget > div > div > div.popup-blocks-container > div > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > div:nth-child(3) > div > span > div:nth-child(2) > ul > li > a.facebook-background")
                        fbs[0].click()
                        time.sleep(2)
                        # myprint("login: 2nd fb login succeeded")
                    except Exception:
                        myprint("login: Exception:(suppressed)")
                        return False
                if is_logged_in(driver):
                    return True

    for element in login_options:
        children  = element.find_elements_by_css_selector("*")
        for child in children:
            if('twitter-background' in child.get_attribute("class").split(' ')):
                try:
                    element.click()
                    time.sleep(2)
                    # myprint("login: 1st twitter login succeeded")
                except Exception:
                    # myprint("1st twitter login method failed, trying 2nd way")
                    try:
                        twtr = driver.find_elements_by_css_selector("body > div.body-widget > div > div > div.popup-blocks-container > div > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > div:nth-child(3) > div > span > div:nth-child(2) > ul > li:nth > a.twitter-background")
                        twtr[0].click()
                        time.sleep(2)
                        # myprint("login: 2nd twitter login succeeded")
                    except Exception:
                        myprint("login: Exception:")
                        return False
                if is_logged_in(driver):
                    return True

    myprint(constants.CRED + "login: FAILED:" + constants.CEND)
    return False

def is_youtube(element):
    potential_checks  = element.find_elements_by_css_selector("span i")
    for child in potential_checks:
        if('fa-youtube' in child.get_attribute("class").split(' ')):
            return True
    return False

def is_done(element):
    potential_checks  = element.find_elements_by_css_selector("span i")
    for child in potential_checks:
        if('fa-check' in child.get_attribute("class").split(' ')):
            return True
    return False

def fill_global_form(driver):
    global_forms = driver.find_elements_by_css_selector("body > div.body-widget > div > div > div > div > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > div:nth-child(2) > div > form")

    if len(global_forms) > 0:
        try:
            name_boxes = global_forms[0].find_elements_by_css_selector("div.body-widget > div > div > div > div > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > div:nth-child(2) > div > form > fieldset.inputs > div.form-horizontal > div > div > div:nth-child(1) > div > input")
            if len(name_boxes) > 0:
                name_boxes[0].clear()
                name_boxes[0].send_keys(constants.FULL_NAME)

            email_boxes = global_forms[0].find_elements_by_css_selector("div.body-widget > div > div > div > div > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > div:nth-child(2) > div > form > fieldset.inputs > div.form-horizontal > div > div > div:nth-child(2) > div > input")
            if len(email_boxes)  > 0:
                email_boxes[0].clear()
                email_boxes[0].send_keys(constants.GOOG_ID + "@gmail.com")

            dob_boxes = global_forms[0].find_elements_by_css_selector("div.body-widget > div > div > div > div > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > div:nth-child(2) > div > form > fieldset.inputs > div.form-horizontal > div > div > div:nth-child(3) > div > div > input")
            dob_formats = global_forms[0].find_elements_by_css_selector("div.body-widget > div > div > div > div > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > div:nth-child(2) > div > form > fieldset.inputs > div.form-horizontal > div > div > div:nth-child(3) > div > div > div")
            if len(dob_boxes) > 0:
                dob_boxes[0].clear()
                if dob_formats[0].text.strip() == 'MM/DD/YYYY':
                    dob_boxes[0].send_keys(constants.DOB_MMDDYY)
                else:
                    dob_boxes[0].send_keys(constants.DOB)

            mandatory_checkboxes = global_forms[0].find_elements_by_css_selector("div > form > fieldset.inputs > div.form-horizontal > div > div > div:nth-child(3) > div > div > label > span.icon")
            if len(mandatory_checkboxes) > 0:
                mandatory_checkboxes[0].click()

            if type5_click_continue(driver, global_forms[0], "global_forms") == False:
                myprint("Form can not be saved")

            time.sleep(5)

            fb_account_selection = driver.find_elements_by_css_selector("div.body-widget > div > div > div > div > div:nth-child(1) > div:nth-child(2) > div > ul > li:nth-child(1) > a.facebook-border")
            if len(fb_account_selection) == 0:
                fb_account_selection = driver.find_elements_by_css_selector("div.body-widget > div > div > div.popup-blocks-container > div > div:nth-child(1) > div > div > ul > li > a.facebook-border")

            if len(fb_account_selection) > 0:
                fb_account_selection[0].click()
                time.sleep(5)
            else:
                myprint("FB account not found")
        except Exception:
            pass

def is_not_abailable(driver):
    try:
        is_not_available =  driver.find_element_by_css_selector("div.body-widget > div > div > div.popup-blocks-container > div > div:nth-child(1) > div:nth-child(6) > div.center.massive-message.light-grey-bg.ng-scope > h2")
        if is_not_available_pattern.match(is_not_available.text.lower().strip()) != None:
            return True
    except Exception:
        pass
    return False

def has_ended(driver):
    try:
        has_ended =  driver.find_element_by_css_selector("div.body-widget > div > div > div > div > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > div:nth-child(1) > div > h2")
        if has_ended_pattern.match(has_ended.text.lower().strip()) != None:
            return True
    except Exception:
        pass
    return False

def watch(driver, element):
    element.click()
    time.sleep(2)

    sharelinks = element.find_elements_by_css_selector("div.cover-video-container > iframe")
    if len(sharelinks) == 0:
        sharelinks = element.find_elements_by_css_selector("divdiv > div > form > div.form-compact__content > div.form-compact__part.user-fragment.center.ng-binding.ng-scope > p > a")
    sharelinks[0].click()# play it
    osascript.osascript("set volume output volume 0")
    time.sleep(constants.RETRY_VIDEO_CONTINUE_WAIT)
    osascript.osascript("set volume output volume 20")

    #In case video has comments box
    input_boxes = element.find_elements_by_css_selector("div > div > form > div.form-compact__content > div:nth-child(2) > div > div > div > input")
    if len(input_boxes) == 0:
        input_boxes = element.find_elements_by_css_selector("div > div > form > div.form-compact__content > div:nth-child(2) > div > div > textarea")
    if len(input_boxes) > 0:
        input_boxes[0].clear()
        input_boxes[0].send_keys(constants.GOOG_ID + " . I'm loving it")
        time.sleep(2)

    has_clicked = False
    while has_clicked == False:
        has_clicked = type4_click_continue(driver, element,'watch_pattern')
        time.sleep(constants.RETRY_VIDEO_CONTINUE_WAIT)

    element.click()

def loop_video_elements_only(driver):
    elements = driver.find_elements_by_css_selector("div.entry-method")
    cnt = 0
    for ctr, element in enumerate(elements):
        action = ActionChains(driver)
        action.move_to_element(element).perform()
        children  = element.find_elements_by_css_selector("a > span.text")
        txt = children[0].text.lower().strip()
        if is_done(element) == False:
            try:
                if (watch_pattern.match(txt) != None or checkout_video_pattern.match(txt) != None) and is_youtube(element):
                    watch(driver, element)
            except Exception as e:
                myprint('Must be due to inner form case somwhere', e)
            try:
                driver.execute_script("document.getElementsByClassName('expandable')[" + str(ctr) + "].style.visibility='hidden';")
            except Exception as e:
                myprint('Seems there was no expandable here', e)
        if is_done(element) == True:
            cnt = cnt + 1
    if cnt == len(elements):
        return True
    return False

def open_url(driver, d, url, cnt):
    if 'sleep' in d:
        time.sleep(random.randint(10, constants.ANTIFRAUD_FUZZY_SLEEP_LIMIT))
    else:
        time.sleep(2)
    driver.get(url)
    myprint(driver.current_url)
    if is_not_abailable(driver):
        myprint("Is NOT AVAILABLE in your region")
        return
    if has_ended(driver):
        myprint("This Competition has ended")
        return
    if login(driver) == False:
        fill_global_form(driver)
    if loop_video_elements_only(driver) == True:
        myprint("DONE:", url)
        with open("data/video_page_links_done.txt", "a") as myfile:
            myfile.write(url + '\n')

def crawl(d):
    if 'sleep' in d:
        time.sleep(random.randint(100, constants.ANTIFRAUD_FUZZY_SLEEP_LIMIT*10))
    else:
        time.sleep(2)
    cnt = 0
    driver = webdriver.Chrome(constants.CHROME_DRIVER_PATH)
    driver.get("https://google.com")

    load_cookie(driver, '/tmp/gleam_fb_cookie')
    load_cookie(driver, '/tmp/gleam_google_cookie')
    load_cookie(driver, '/tmp/gleam_twitter_cookie')
    load_cookie(driver, '/tmp/gleam_instagram_cookie')
    load_cookie(driver, '/tmp/gleam_soundcloud_cookie')
    load_cookie(driver, '/tmp/gleam_steampowered_cookie')

    for href in video_url_arr:
        try:
            myprint("==", href.replace('\n',''), "==")
            try:
                open_url(driver, d, href, cnt)
            except Exception as e:
                myprint(constants.CRED + "do:" + constants.CEND, e)
                pass
        except Exception:
            pass

    driver.quit()

if __name__ == "__main__":
    argv = sys.argv[1:]
    d = {}
    for i in range(0, len(argv), 2):
        d[argv[i].replace('-', '')] = argv[i + 1]
    myprint(d)
    crawl(d)
    myprint(constants.CGREEN + 'Main PEACEFULLY terminated' + constants.CEND)


print("\n")

import numpy as np
np.random.seed(0)    # For repeatability

class Dense_Layer:
    def __init__(self, n_inputs, n_neurons):
        self.weights = 0.01 * np.random.randn(n_inputs, n_neurons)
        self.biases = np.zeros([1, n_neurons])

    def forward(self, inputs):    # inputs is X
        self.outputs = np.dot(inputs, self.weights) + self.biases

class MAE_Cost:
    def forward(self, y_pred, y_true):    # y_pred is prediction from model, y_true is the answer
        return np.mean(np.abs(y_pred - y_true))

class MSE_Cost:
    def forward(self, y_pred, y_true):
        return np.mean(np.square(y_pred - y_true))

# hours studied
X = np.array([[0], [1], [2], [3], [4], [5], [6], [7], [8]])    # one input feature for each example
# percentage
y = np.array([[20], [30], [40], [50], [60], [70], [80], [90], [100]])    # one output feature for each example

mse = MSE_Cost()    # define cost function
dumb_model1 = Dense_Layer(1, 1)    # 1 input feature, 1 neuron (output feature)
dumb_model1.forward(X)

# set to best because this is the only model we have at the moment
best_model = dumb_model1
lowest_cost = mse.forward(dumb_model1.outputs, y)
print("Initial Cost:", lowest_cost)

for trial in range(1000):
    model = Dense_Layer(1, 1)    # initialize new model
    model.forward(X)    # get new model's predictions
    cost = mse.forward(model.outputs, y)    # get new model's cost

    # save model if the cost is better than our past trials
    if cost < lowest_cost:
        lowest_cost, best_model = cost, model
        print("On trial", str(trial), "we have a lower cost of", cost)

# Summary
print("\nSummary of Best Model\n")
print("Weights:", best_model.weights)
print("Biases:", best_model.biases)
print("Lowest Cost:", lowest_cost)
print("Prediction:", best_model.outputs)

print("\n")


"""The API that serves my model"""

# imports
from fastapi import FastAPI
import uvicorn
from pydantic import BaseModel, conlist
from typing import List
import joblib
from brock import CATEGORIES
import numpy as np

# load the model
model = joblib.load("model/model-final.joblib")

APP_DESC = """
##  Goals

- POC to highlight how a relatively flexible pipeline to train and serve ML models
- Flag a model that can associate one or more tags to a question with a score

### Longer term:

- all sorts of ML models can be leveraged with the tensorflow deep learning engine included
- think about real time vs batch scoring of data

### Use cases
 - Real time could include the removal of toxic questions submitted to PQCast, or suggest a set of answers to common questions.
 - In batch, tag questions and use for thought leadership and internal strategic planning (with clients) based on trends in "needs or intents"

"""

# create the app
app = FastAPI(title="POC ML API with Scikit-learn for Text Classification",
              description=APP_DESC,
              version="0.1")

# create the class for the post param
class Message(BaseModel):
    text: str
    class Config:
        schema_extra = {
        "example": {
            "text": "I like computers and internet things"
        }
    }

@app.post('/predict', tags=["predictions"])
async def get_prediction(data: Message):
    """
    For a given question text, predict if the text belongs to a target category.

     - Each question can be tagged with 1 or more categories in the taxonomy.  For example, some may have two tags.
     - Each <b>category</b> is the score of being tagged as that category in our taxonomy.  Scores go from 0 to 1, with 1 representing absolute confidence.
     - Practical usage: only associate a tag based on probability of having that tag (e.g. > .9, or some other score level)

    __Simply, ideal tag scores for a question tagged with `facts` and `admission` would be 1.0 for both of those labels.__
    """
    data = data.dict()
    question = data['text']
    pred = model.predict([question])
    int_p = int(np.round(pred, 4))
    confidence = list(model.predict_proba([question]))
    maxc = int(np.argmax(confidence[0]))
    float_c = float(np.round(confidence[0][maxc], 3))

    # return the values.  Code above removed numpy return values, which will choke FastAPI.
    resp = {'pred': int_p,
            'label': str(CATEGORIES[int_p]),
            'score': float_c}

    return resp


#!/usr/bin/python

import collections
import random
import heapq

class PriorityQueue:
    def __init__(self):
        self.elements = []

    def is_empty(self):
        return len(self.elements) == 0

    def put(self, item, priority):
        heapq.heappush(self.elements, (priority, item))

    def get(self):
        return heapq.heappop(self.elements)[1]

    def contains(self, item):
        return not self.is_empty() and item in list(zip(*self.elements))[1]

def dfs(grid):

    # do the search
    result = dfs_iterative(grid)
    result2 = bfs_iterative(grid)
    result3 = ucs_iterative(grid)

    print ("Depth First Search")
    # print expanded nodes
    print (len(result['visited']))
    for pos in result['visited']:
        print (str(pos[0]) + ' ' + str(pos[1]))

    print ("Breath First Search")
    print (len(result2['visited']))
    for pos in result2['visited']:
        print (str(pos[0]) + ' ' + str(pos[1]))

    print ("Uniform Cost Search")
    path = reconstruct_path(finish_state, result3)
    print (len(path)-1)  # distance is -1
    for pos in path:
        print (str(pos[0]) + ' ' + str(pos[1]))

    # print path
    #path = reconstruct_path(finish_state, result['came_from'])
    #print (len(path)-1)  # distance is -1
    #for pos in path:
        #print (str(pos[0]) + ' ' + str(pos[1]))

# ---------------------------------------------------------------------------
# Depth First Search
#
# * take element from the fringe
# * if it's the goal -> success
# * otherwise add all of it's neighbours that have not yet been
#     looked at to the fringe
#
# Store the path by recording where we came from.
#   See: http://www.redblobgames.com/pathfinding/a-star/introduction.html
#   Alternative: store not only nodes but also their parents in visited
# ---------------------------------------------------------------------------

def dfs_iterative(grid):
    global finish_state
    fringe = collections.deque()  # nodes under consideration
    came_from = {}  # our closed set & node parent information
    visited = []  # only needed for hackerrank - print expanded nodes at end

    # start state
    start = (11, 11)
    fringe.append(start)
    came_from[start] = None

    while len(fringe) > 0:
        current = fringe.pop()  # depth first
        visited.append(current)

        if is_goal(current, grid):  # done?
            finish_state = current
            print (finish_state)
            return {'visited': visited, 'came_from': came_from}

        # expand all possible moves from current
        neighbours = get_neighbours(current, grid)
        for next in neighbours:

            # don't expand (or explore) nodes twice
            if next not in came_from and next not in fringe:
                came_from[next] = current
                fringe.append(next)

def bfs_iterative(grid):
    global finish_state
    fringe = collections.deque()  # nodes under consideration
    came_from = {}  # our closed set & node parent information
    visited = []  # only needed for hackerrank - print expanded nodes at end

    # start state
    start = (11, 11)
    fringe.append(start)
    came_from[start] = None

    while len(fringe) > 0:
        current = fringe.popleft()
        visited.append(current)

        if is_goal(current, grid):  # done?
            finish_state = current
            print (finish_state)
            return {'visited': visited, 'came_from': came_from}

        # expand all possible moves from current
        neighbours = get_neighbours(current, grid)
        for next in neighbours:

            # don't expand (or explore) nodes twice
            if next not in came_from and next not in fringe:
                came_from[next] = current
                fringe.append(next)

def ucs_iterative(grid):
    global finish_state
    fringe = PriorityQueue()   # nodes under consideration
    came_from = {}  # our closed set & node parent information
    cost_so_far = {} # only needed for hackerrank - print expanded nodes at end

    # start state
    start = (11, 11)
    fringe.put(start, 0)
    cost_so_far[start] = 0
    came_from[start] = None

    while not fringe.is_empty() > 0:
        current = fringe.get()

        if is_goal(current, grid):  # done?
            finish_state = current
            return came_from

        # expand all possible moves from current
        neighbours = get_neighbours(current, grid)
        for next in neighbours:

            new_cost = cost_so_far[current] + cost_of_move(next, grid)

            # don't expand (or explore) nodes twice
            if (next not in came_from and not fringe.contains(next)) or (new_cost < cost_so_far[next]):
                came_from[next] = current
                cost_so_far[next] = new_cost
                fringe.put(next, new_cost)

def cost_of_move(pos, grid):
    return 0 if is_goal(pos, grid) else 1

# gets all neighbours for a position on the grid, only returns valid moves
def get_neighbours(pos, grid):
    (x, y) = pos
    step = grid[pos[0]][pos[1]]
    neighbours = [(x-step, y  ),  # up
                  (x,   y-step),  # left
                  (x,   y+step),  # right
                  (x+step, y  )]  # down
    neighbours = filter(lambda x: is_passable(x, grid), neighbours)
    return neighbours

def is_passable(pos, grid):
    x,y = pos[0],pos[1]
    return (x >= 0 and x < 21) and (y >= 0 and y < 21)

def is_goal(pos, grid):
    x,y = pos[0],pos[1]
    return (x == 0 and y == 0) or (x == 0 and y == 20) or (x == 20 and y == 0) or ( x == 20 and y == 20)

def is_start(pos, grid):
    x,y = pos[0],pos[1]
    return x == 11 and y == 11

# build path from start to goal
def reconstruct_path(goal, came_from):
    current = goal
    path = [current]
    while not is_start(current, grid):
        print (current)
        current = came_from[current]
        path.append(current)
    return path[::-1]  # reverse

# PREDEFINED TEMPLATE CODE -----------------------------------------
start_state = (11,11)
finish_state = (0,0)
grid = [[random.randint(1,9) for x in range(21)] for x in range(21)]

dfs(grid)


import unittest

from trainer import main, save_model, ComponentArguments, OutputDestinations
import os
import yaml
import shutil

class TestTrainer(unittest.TestCase):
    def setUp(self) -> None:
        contents_folder = os.path.join(
            os.path.dirname(__file__), "test_contents"
        )
        output_folder = os.path.join(os.path.dirname(__file__), "output")

        def load_yaml(path: str) -> str:
            with open(path, "r") as f:
                d = yaml.load(f)
                return yaml.dump(d)

        parameters = load_yaml(
            os.path.join(contents_folder, "test_parameters.yml")
        )
        self.args = ComponentArguments(
            train_data_path=os.path.join(contents_folder, "train.csv"),
            val_data_path=os.path.join(contents_folder, "val.csv"),
            test_data_path=os.path.join(contents_folder, "test.csv"),
            suffix="_test",
            parameters=parameters,
        )
        self.out_dest = OutputDestinations(output_folder)

    def tearDown(self) -> None:
        shutil.rmtree(self.out_dest.trained_model)

    def test_main(self):
        model = main(args=self.args)
        save_model(model=model, out_dest=self.out_dest)


import sqlite3, time, os
from datetime import datetime as dt
from datetime import timedelta

class Query:
    def __init__(self):
        self.conn = sqlite3.connect('C:/Users/wbmar/OneDrive/Documents/python_projects/options/options.db')
        self.c = self.conn.cursor()

    def get_data(self, metric, start, end):
        start_unix = time.mktime(dt.strptime(start, "%Y-%m-%d").timetuple())
        end_unix = time.mktime(dt.strptime(end, "%Y-%m-%d").timetuple())

        self.c.execute(f"SELECT date,{metric} FROM gme WHERE date BETWEEN {start_unix} AND {end_unix}")
        data = self.c.fetchall()
        self.conn.close()

        unix_list = [item[0] for item in data]
        date_list = [dt.fromtimestamp(int(ts)) for ts in unix_list] # Converts Unix timestamp to local datetime
        last_list = [item[1] for item in data]

        return(date_list, last_list)

    def suggest_dates(self):
        self.c.execute("SELECT MAX(date) FROM gme")
        ts = self.c.fetchone()[0] + 86400 # Adds 1 day to unix time
        self.conn.close()

        start = dt.utcfromtimestamp(ts).strftime('%Y-%m-%d')
        end = (dt.today()-timedelta(days=1)).strftime('%Y-%m-%d')

        return start, end


from gzip import READ
from operator import truediv
from optparse import AmbiguousOptionError
import uuid
from utils.helper import *

REWARD_VALUE = 25.0
NORMAL = 0
REWARD = 1

from blockchainActions.Signature import *

class Tx:
    def __init__(self, type=NORMAL):

        self.type = type
        self.inputs = []
        self.outputs = []
        self.sigs = []
        self.reqd = []
        self.status = []
        self.userId = []
        self.txid = uuid.uuid1()

    def add_input(self, from_addr, amount):
        self.inputs.append((from_addr, amount))

    def add_output(self, to_addr, amount):
        self.outputs.append((to_addr, amount))

    def add_userId(self, id):
        self.userId.append(id)

    def add_reqd(self, addr):
        self.reqd.append(addr)

    def add_status(self, st):
        self.status.append(st)

    def sign(self, private):
        message = self.__gather()
        newsig = sign(message, private)
        self.sigs.append(newsig)

    def is_valid(self):
        if self.type == REWARD:
            if len(self.inputs) != 0 and len(self.outputs) != 1:
                return False
            return True

        else:
            total_in = 0
            total_out = 0
            message = self.__gather()
            for addr, amount in self.inputs:
                found = False
                for s in self.sigs:
                    if verify(message, s, addr):
                        found = True
                if not found:
                    # print ("No good sig found for " + str(message))
                    return False
                if amount < 0:
                    return False
                total_in = total_in + amount
            for addr in self.reqd:
                found = False
                for s in self.sigs:
                    if verify(message, s, addr):
                        found = True
                if not found:
                    return False
            for addr, amount in self.outputs:
                if amount < 0:
                    return False
                total_out = total_out + amount

            if total_out > total_in:
                # print("Outputs exceed inputs")
                return False
            return True

    def __gather(self):
        data = []
        data.append(self.inputs)
        data.append(self.outputs)
        data.append(self.reqd)
        data.append(self.status)
        return data

    def __repr__(self):
        repr_str = "INPUTS:\n"
        for addr, amt in self.inputs:
            repr_str = repr_str + str(amt) + " from " + get_user_name_by_pub_key(pbcKey=addr.decode('utf-8')) + "\n"
            # repr_str = repr_str + str(amt) + "from" + str(addr) + "\n"

        repr_str += "OUTPUTS:\n"
        for addr, amt in self.outputs:
            repr_str = repr_str + str(amt) + " to " + get_user_name_by_pub_key(pbcKey=addr.decode('utf-8')) + "\n"
            # repr_str = repr_str + str(amt) + "to" + str(addr) + "\n"

        repr_str += "EXTRA REQUIRED SIGNATURES:\n"
        for req_sig in self.reqd:
            repr_str = repr_str + str(req_sig) + "\n"

        repr_str += "SIGNATURES:\n"
        for sig in self.sigs:
            repr_str = repr_str + str(sig) + "\n"

        repr_str += "Created by userID:\n"
        for id in self.userId:
            repr_str = repr_str + str(id) + "\n"

        repr_str += "END\n"

        return repr_str


from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
import streamlit as st
import numpy as np
import pandas as pd

class Swg(object):

    def __init__(self,data):

        self.data = data

    def scale(self):
        df = self.data
        #Find Lastrow
        lastrow = len(df.index)
        SWG_Data = pd.read_csv("Dataset/SWG_Dataset.csv")
        SWG_Data = SWG_Data.drop(columns=['Start time','End time','Severity'])

        # SWG_Data.set_axis([read_columns], axis='columns', inplace=True)

        #concat data and dataset for Scaler
        df = pd.concat([df, SWG_Data],axis=0, ignore_index=True)
        # st.table(df.head())
        scl = StandardScaler()

        read_columns = ["UPPER_BUS_PHASE_A","UPPER_BUS_PHASE_B","UPPER_BUS_PHASE_C",
                "LOWER_BUS_PHASE_A","LOWER_BUS_PHASE_B","LOWER_BUS_PHASE_C",
                "OUTGOING_PHASE_A","OUTGOING_PHASE_B","OUTGOING_PHASE_C",
                "UPPER_BUS_PD", "LOWER_BUS_PD", "SPOUT_PD", "OUTGOING_PD"]

        X = scl.fit_transform(df[read_columns].values)

        if df.shape[1] == 9:

            y = df.iloc[:,8].values
            return X,y
        else:
            X = pd.DataFrame(X).iloc[:lastrow]
            return X

    def acc(self,pred,true): return accuracy_score(true,pred)


# 读写二进制文件

# 写文件

def write_bi(path, mode='wb'):
    '''写入二进制位文件,和写text文件差不多'''
    fout = open(path, mode)
    bb = bytes('lier\u2603'.encode('utf-8'))
    fout.write(bb)

def write_range():
    bdata = bytes(range(0, 256))
    fout = open('resources/bfile', 'wb')
    fout.write(bdata)
# 读文件

def read_bi(path, mode='rb'):
    '''读取二进制文件，几乎可读取text一样'''
    fin = open(path, mode)
    bdata = fin.read()
    s = bdata.decode('utf-8')
    print('[read_bi]:\n\ts:{0}'.format(s))

# seek() 改变位置

def seek_bfile():
    '''改变读取头位置'''
    fin = open('resources/bfile', 'rb')
    # 从头部移动开始，移动到240 offset
    fin.seek(240, 0)  # 第二个参数：origin: 0-文件头部 1-当前位置 2：文件尾部
    print('bfile position tell():', fin.tell())
    bdata = fin.read()
    print('bdata length:', len(bdata))
    print(bdata)

# 调用函数部分
write_bi('resources/bi.data')
read_bi('resources/bi.data')
write_range()
seek_bfile()


from sys import stdin
import heapq
from itertools import chain

dy = (-1,0,1,0)
dx = (0,-1,0,1)

n = int(stdin.readline())

board = [list(map(int, stdin.readline().split())) for _ in range(n)]

heap = []
dp = [[1]*n for _ in range(n)]

for i in range(n):
    for j in range(n):
        heapq.heappush(heap, (board[i][j] * -1, i, j))

while heap:
    num, y, x = heapq.heappop(heap)

    for i in range(4):
        ny, nx = y + dy[i], x + dx[i]
        if 0 <= ny < n and 0 <= nx < n and board[ny][nx] > board[y][x]:
            dp[y][x] = max(dp[y][x], dp[ny][nx] + 1)

print(max(chain(*dp)))


def baby(arr):
    arr.sort()
    for i in range(len(arr)):
        if arr.count(arr[i]) == 3:
            return 1
    cnt = 0
    for i in range(len(arr)-1):
        if arr[i] == arr[i+1]:
            continue
        if arr[i] + 1 == arr[i+1]:
            cnt += 1
        else:
            cnt = 0
        if cnt == 2:
            return 1
    return 0

T = int(input())
for tc in range(1, T + 1):
    arr = list(map(int, input().split()))
    first = []
    second = []
    for i in range(len(arr)):
        if i % 2 == 0:
            first.append(arr[i])
        else:
            second.append(arr[i])
    a = 0
    b = 0
    for i in range(3, len(first)+1):
        a = baby(first[0:i])
        b = baby(second[0:i])
        if a or b:
            break
    if a:
        print('#{} 1'.format(tc))
    elif b:
        print('#{} 2'.format(tc))
    else:
        print('#{} 0'.format(tc))

# 쌤풀이
def check(p, idx):
    if p[idx] >= 3:
        return True
    # run
    for i in range(1, 9):
        if p[i - 1] and p[i] and p[i + 1]:
            return True
    return False

for tc in range(1, int(input()) + 1):
    card = list(map(int, input().split()))  # 카드입력
    player1 = [0] * 10
    player2 = [0] * 10
    win = 0
    for i in range(0, len(card), 2):
        player1[card[i]] += 1
        player2[card[i + 1]] += 1
        flag1 = check(player1, card[i])
        flag2 = check(player2, card[i + 1])
        if flag1:
            win = 1
            break
        elif flag2:
            win = 2
            break
    print("#{} {}".format(tc, win))


from userbot.utils import lightning_cmd
from var import Var

@borg.on(lightning_cmd(pattern="stat$"))
async def stats(event):
    if event.fwd_from:
        return
    botusername = Var.TG_BOT_USER_NAME_BF_HER
    noob = "stats"
    if event.reply_to_msg_id:
        await event.get_reply_message()
    tap = await bot.inline_query(botusername, noob)
    await tap[0].click(event.chat_id)
    await event.delete()

@borg.on(lightning_cmd(pattern="xogame$"))
async def gamez(event):
    if event.fwd_from:
        return
    botusername = "@xobot"
    noob = "play"
    if event.reply_to_msg_id:
        await event.get_reply_message()
    tap = await bot.inline_query(botusername, noob)
    await tap[0].click(event.chat_id)
    await event.delete()

@borg.on(lightning_cmd(pattern="mod ?(.*)"))
async def mod(event):
    if event.fwd_from:
        return
    modr = event.pattern_match.group(1)
    botusername = "@PremiumAppBot"
    if event.reply_to_msg_id:
        await event.get_reply_message()
    tap = await bot.inline_query(botusername, modr)
    await tap[0].click(event.chat_id)
    await event.delete()


#!/usr/bin/env python3

'''A command-line implementation of the Tower of Hanoi game.

Rates solutions based on number of moves.'''
import random
import os

from pyfiglet import figlet_format

board = [[],[],[]]
moves = 0
number_of_disks = 3
disk_character = "#"

####################
# Set Up Interface #
####################

def clear_screen():
    '''Clears the screen.'''
    os.system('cls' if os.name == 'nt' else 'clear')

def logo():
    '''Displays the game logo.'''
    clear_screen()
    print(figlet_format('Hanoi', font='whimsy'))

def press_enter():
    '''Pauses the game until the player presses enter.'''
    input("\nPress ENTER to continue. ")

#################
# Game Specific #
#################

def finished():
    '''Checks if the game has been solved.'''
    if len(board[2]) == number_of_disks:
        return True
    return False

def new_game():
    '''Sets up a new game.'''
    global moves, board
    moves = 0
    board = [list(range(number_of_disks,0,-1)),[],[]]
    return True

def game_loop():
    '''Play the game.'''
    while not finished():
        logo()
        draw_board()
        action = input("> ").lower()
        if action == "q" or action == "quit":
            clear_screen()
            break
        elif action == "help" or action == "h":
            logo()
            print("HELP")
            print("Input the moves you'd like to make in the format below:\n\nx > y\n \nX is the column you want to take a stone from and\ny is the column you want to place it onto.\n\n2 > 1 moves the top stone from column 2 to column 1.")
            print("\nQuit exits the game and help brings up this menu.")
            press_enter()
        else:
            try:
                x, y = action.split(",")
            except ValueError:
                continue
            try:
                x = int(x.strip())
                y = int(y.strip())
            except ValueError:
                continue
            if x in range(1,4) and y in range(1,4):
                move(x, y)
    if finished():
        logo()
        draw_board()
        draw_stars(score_moves(moves,number_of_disks))
        print("Congratulations! You solved the puzzle!")
        press_enter()
    return True

def score_moves(moves, n):
    '''Calculate scores depending on the number of moves.'''
    best_moves = 2**n - 1
    stars = 1
    proportion = (moves - best_moves) / best_moves
    if proportion <= 0.2:
        stars = 3
    elif proportion <= 0.4:
        stars = 2
    return stars

def move(from_column, to_column):
    '''Moves a disk from one column to another.'''
    global board, moves
    to_column -=1
    from_column -=1
    valid = True
    if len(board[from_column]) == 0:
        valid = False
    if valid and (len(board[to_column]) == 0 or board[from_column][-1] < board[to_column][-1]):
        print("valid)")
        moves += 1
        board[to_column].append(board[from_column].pop())
    else:
        print("That is not a valid move!")
        press_enter()
        return False
    return True

########
# Menu #
########

def show_menu():
    '''Shows the game menu.'''
    while True:
        logo()
        print("Play\nHelp\nQuit")
        action = input("> ").lower()
        if action == "q" or action == "quit":
            clear_screen()
            break
        elif action == "play" or action == "p":
            new_game()
            game_loop()
        elif action == "help" or action == "h":
            draw_help()

def draw_help():
    '''Shows the help page.'''
    logo()
    print("HELP")
    print("Input the moves you'd like to make in the format below:\n\nx,y\n \nX is the column you want to take a stone from and\ny is the column you want to place it onto.\n\nExample: 2,1 moves the top stone from column 2 to column 1.")
    print("\nQUIT exits the game.\nHELP brings up this menu.")
    press_enter()

###########
# Drawing #
###########

def draw_board():
    '''Draws the game display.'''
    #print(board)
    whole = number_of_disks * 2 + 2
    mid = whole//2
    rest = whole - mid+1
    display = "=="
    for x in range(3):
        display += "="*mid + str(x+1) + "="*rest
    display += "="
    for row in range(number_of_disks+1):
        newstr = ""
        for column in range(3):
            try:
                drawing = disk_character*board[column][row]
            except IndexError:
                drawing = "|"
            else:
                drawing += "|" + drawing
            newstr += "   " + drawing.center(number_of_disks*2+1)
        display = newstr + "\n" + display
    print("Move all of the disks onto the last peg.")
    print("\n"+display)
    if not finished():
        print("\nEnter your move.\nExample:\n1, 2 moves the top disk from column 1 to column 2.\n")
        print("QUIT to quit. HELP for help")

def draw_stars(stars):
    star = [
        "    .    ",
        " __.'.__ ",
        "'-.   .-'",
        "  /.'.\  ",
        "  '   '  "
    ]
    print("")
    for line in star:
        linetext = line;
        count = stars
        while count >= 2:
            linetext += " " + line;
            count -= 1
        linetext = linetext.center(40)
        print(linetext)

###################
# Run the Program #
###################

show_menu()


import argparse
import os
import sys
import time
sys.path.append(os.path.abspath('../..'))
import pandas
import json
import modules.io as io
import modules.vascular_data as sv
import vtk
import numpy as np

parser = argparse.ArgumentParser()
parser.add_argument('-config')
parser.add_argument('-tube_file')
parser.add_argument('-output_fn')
args = parser.parse_args()

cfg = io.load_json(args.config)

DIR          = cfg['dir']
MESH_LABELS  = cfg['mesh_labels']
GENS         = cfg['generations']
NUM_MODELS   = cfg['num_models']
SIM_NAME     = cfg['sim_name']
RESULTS_FILE = cfg['results_file']

LABS = cfg['labels']
LABELS = []
for i in range(cfg['start'], cfg['end'], cfg['incr']):
    for l in LABS:
        new_l = str(i)
        if len(new_l) == 1:
            new_l = "0000"+new_l
        if len(new_l) == 2:
            new_l = "000"+new_l
        if len(new_l) == 3:
            new_l = "00"+new_l
        if len(new_l) == 4:
            new_l = "0"+new_l
        LABELS.append(l+'_'+new_l)

TUBE_FILE = io.load_json(args.tube_file)
POINTS    = TUBE_FILE['points']
NORMALS   = TUBE_FILE['normals']
RADIUSES  = TUBE_FILE['radiuses']
N         = range(len(POINTS))

data = []

for gen in GENS:
    for mesh in MESH_LABELS:
        for nm in range(NUM_MODELS):
            vtu_fn = DIR+'/'+str(gen)+'/'+mesh+'/'+str(nm)+'/'+SIM_NAME+'/'+RESULTS_FILE
            print(vtu_fn)
            if not os.path.exists(vtu_fn): continue

            pd = sv.read_vtu(vtu_fn)

            for j,p,n,r in zip(N, POINTS, NORMALS, RADIUSES):
                surf = sv.clip_plane_rad(pd,p,n,r)

                #try:
                area = sv.vtk_integrate_pd_area(surf)
                length = sv.vtk_integrate_pd_boundary_length(surf)

                for l in LABELS:
                    print(l)
                    ints    = sv.vtk_integrate_pd_volume(surf, l)
                    ints_bd = sv.vtk_integrate_pd_boundary(surf, l)

                    for k in range(len(ints)):
                        q = l.split('_')[0]
                        t = int(l.split('_')[1])
                        c = k

                        d = {"generation":gen,
                            "mesh":mesh,
                            "model":nm,
                            "point":j,
                            "time":t,
                            "component":c,
                             "x":p[0], "y":p[1], "z":p[2],
                             "nx":n[0],"ny":n[1],"nz":n[2],
                             "radius_actual":np.sqrt(area/np.pi),
                             "radius_supplied":r,
                             "area":area,
                             "length":length
                             }

                        d[q] = ints[k]*1.0/area
                        d[q+'_boundary'] = ints_bd[k]*1.0/length

                        data.append(d)
                #except:
                    #print("failed vtu {} point {}".format(i,j))

df = pandas.DataFrame(data)
df.to_csv(args.output_fn)


from clean_data import class_normalize
import pandas as pd

#dummy dataframe
df = pd.DataFrame([['toddlers',2,4],['adults',0,-1],['elders',0,0]])
df = df.rename(columns={0:'agegroup',1:'score1',2:'score2'})
df.head()

#normalize column 'agegroup' in indicated order
df = class_normalize(df=df,column='agegroup', \
                     mapping_order=['toddlers','adults','elders'])
df.head()


# import vbx.sim
# import openvino.inference_engine as ie
# import argparse
# import os
import numpy as np
import cv2
import json
# from  vbx.postprocess.blazeface import blazeface
from math import ceil
from itertools import product as product

def calcIouLTRB(A,B):
    # A and B are of the format [left, top, right, bottom]
    left = max(A[0],B[0])
    top = max(A[1],B[1])
    right = min(A[2],B[2])
    bottom = min(A[3],B[3])
    intersect = max(0,right-left) * max(0,bottom-top)
    union = (A[2]-A[0])*(A[3]-A[1]) + (B[2]-B[0])*(B[3]-B[1]) - intersect
    if union>0:
        return intersect/union
    else:
        return 0

def scrfd(modelOut, inputWidth, inputHeight, detectThresh=0.75, maxIou=0.35):
    mapStrides = [8,16,32]
    maxPreDetects = 64

    assert(inputHeight % 32 == 0)
    assert(inputWidth % 32 == 0)
    h32 = (inputHeight//32) # image height at stride=32
    w32 = (inputWidth//32)  # image width at stride=32
    h8,h16 = 4*h32, 2*h32
    w8,w16 = 4*w32, 2*w32
    mapSizes = [[h8,w8],[h16,w16],[h32,w32]]
    mapPixels = np.array([h8*w8, h16*w16, h32*w32])

    # each map shape is [anchor][channel][y][x]
    #   there are 2 anchors per pixel
    #   location map has 4 channels; confidence has 2 channels; landmarks has 10 channels
    #   the range of x and y pixel are given by mapSizes[...]
    confMaps = [modelOut[0], modelOut[1], modelOut[2]]
    locMaps = [modelOut[3], modelOut[4], modelOut[5]]
    landMaps = [modelOut[6], modelOut[7], modelOut[8]]

    scores = np.concatenate(confMaps)

    # alternatively, this argsort could be moved inside the scores loop above
    #   this would reduce memory, but complicate the code and make optimization/vectorization more difficult
    order = scores.argsort()[::-1][:maxPreDetects]
    order = order[scores[order]>detectThresh]

    faces = []
    for n,ind in enumerate(order):
        detectScore = scores[ind]
        # scores index to other indices
        mapNum = 0
        if ind>=2*mapPixels[0]:
            ind -= 2*mapPixels[0]
            mapNum += 1
            if ind>2*mapPixels[1]:
                ind -= 2*mapPixels[1]
                mapNum += 1
        # get anchor per pixel
        pixels = mapPixels[mapNum]
        anchNum = 0
        if ind >= pixels:
            anchNum = 1
            ind = ind - pixels
        # get pixel
        mapSize = mapSizes[mapNum]
        y = ind//mapSize[1]
        x = ind - y*mapSize[1]
        # get prior
        stride = mapStrides[mapNum]
        anchor = np.array([x, y]) * stride # anchor center, units of pixels

        # get location data
        location = locMaps[mapNum][anchNum*4*pixels+ind::pixels][:4] * stride
        box = np.concatenate((anchor - location[:2], anchor + location[2:]))
        # [left, top, right, bottom]

        # NMS
        passNms = True
        for face in faces:
            if calcIouLTRB(face['box'],box)>maxIou:
                passNms = False
                break
        if not passNms:
            continue

        # get landmarks
        landmark = landMaps[mapNum][anchNum*10*pixels+ind::pixels][:10] * stride
        keypoints = np.array([anchor + landmark[:2],
                              anchor + landmark[2:4],
                              anchor + landmark[4:6],
                              anchor + landmark[6:8],
                              anchor + landmark[8:10]])

        # add detection to list of faces
        faces.append({'box':box,'landmarks':keypoints,'score':detectScore})

    return faces

if __name__ == "__main__":
    main()


import pytest
import parse_data

MOCK_DATA_FILENAME = 'mock_data/mock_basket.csv'

LOAD_DATA_RESULT = """             fase            team_1         team_2  score_1  score_2
0  1_fase_grupo_a     CD José Regio  Juvemaia ACDC       72       35
1  1_fase_grupo_a        Guifões SC        FC Gaia       68       54
3  1_fase_grupo_b          FC Porto   Club 5Basket       31       71
4  1_fase_grupo_b  FidesGondobasket     CLIP Teams       24       73"""

PARSE_GAMES_RESULT = [{'fase': '1_fase_grupo_a', 'CD José Regio': 72, 'Juvemaia ACDC': 35}, {'fase': '1_fase_grupo_a', 'Guifões SC': 68, 'FC Gaia': 54}, {'fase': '1_fase_grupo_b', 'FC Porto': 31, 'Club 5Basket': 71}, {'fase': '1_fase_grupo_b', 'FidesGondobasket': 24, 'CLIP Teams': 73}]

GET_TEAMS_NAMES_RESULT = ['CD José Regio', 'CLIP Teams', 'Club 5Basket', 'FC Gaia', 'FC Porto', 'FidesGondobasket', 'Guifões SC', 'Juvemaia ACDC']

def test_load_data():
    result = repr(parse_data.load_data(MOCK_DATA_FILENAME, ';'))
    assert result == LOAD_DATA_RESULT

def test_parse_games():
    data = parse_data.load_data(MOCK_DATA_FILENAME, ';')
    result = parse_data.parse_games(data)
    assert result == PARSE_GAMES_RESULT

def test_get_team_names():
    data = parse_data.load_data(MOCK_DATA_FILENAME, ';')
    result = parse_data.get_team_names(data)
    assert result == GET_TEAMS_NAMES_RESULT


"""Exports the Problem classes.

The various Problem classes have common structure but do not inherit from each other.
"""

import random
from random import randint

class MultProblem(object):
	"""An object representing a random multiplication problem	"""

	type = "multiplication"

	def __init__(self):
		self.a = randint(2, 10)
		self.b = randint(2, 10)
		self.answer = self.a * self.b
		self.statement = "What is {0} * {1}?  ".format(self.a, self.b)

class HardMultProblem(object):
	"""An object representing a random hard multiplication problem."""

	def __init__(self):
		self.type = "hard mult"
		self.a = randint(10, 100)
		self.b = randint(1, 10)
		self.answer = self.a * self.b
		self.statement = "What is {0} * {1}?  ".format(self.a, self.b)

class DivProblem(object):
	"""An object representing a random division problem with no remainder."""

	def __init__(self):
		self.type = "division"
		self.answer = randint(1, 10)
		self.divisor = randint(1, 10)
		self.dividend = self.answer * self.divisor
		self.statement = "What is {0} / {1}? ".format(self.dividend, self.divisor)

class SpanishProblem(object):
	"""An object representing a random Spanish vocabulary flashcard

	the flashcard is picked from the given set of words
	"""

	def __init__(self, words):
		self.type = "spanish"
		self.words = words
		self.english = self.words.keys()[randint(0, len(self.words)-1)]
		#print "the word for this new question is: ", repr(self.english)
		#print type(self.words[self.english])
		self.spanish = self.words[self.english].encode('utf-8')
		self.statement = "What is \"{0}\" in Spanish?".format(self.english)
		self.answer = self.spanish

class CakeProblem(object):

	def __init__(self, ingredient, answer, ratio):
		self.type = "cake"
		self.ingredient = ingredient
		self.answer = answer
		self.ratio = ratio
		self.recipe = answer * ratio
		self.statement = """The recipe calls for {0} cups of {1}.
How many will you need?""".format(self.recipe, self.ingredient)
		if ingredient == "eggs":
			self.statement = """The recipe calls for {0} eggs.
How many will you need?""".format(self.recipe)


class Solution:
    def swimInWater(self, grid: List[List[int]]) -> int:
        n = len(grid)
        if len(grid) == 1:
            return grid[0][0]

        minheap = [(grid[0][0],0, 0)]
        time = 0
        visited = set()
        direction = [(1,0), (-1,0), (0,1),(0,-1)]
        while minheap:
            cost, r,c = heapq.heappop(minheap)
            time = max(cost,time)
            if r == n-1 and c == n-1:
                return time
            if (r,c) in visited:
                continue
            visited.add((r,c))

            for dr, dc in direction:
                newrow = dr + r
                newcol = dc + c
                if newrow < 0 or newrow == n or newcol < 0  or newcol == n or (newrow,newcol) in visited:
                    continue
                heapq.heappush(minheap, (max(cost, grid[newrow][newcol]), newrow,newcol))


from pyspark.sql import SparkSession
from pyspark.ml.feature import VectorAssembler
from pyspark.ml.classification import RandomForestClassifier,LinearSVC
import ast
from pyspark.ml import Pipeline
from pyspark.sql import functions as F, types
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
import pandas as pd

spark = SparkSession \
    .builder \
    .appName("toxicity_classification") \
    .getOrCreate()
sc = spark.sparkContext

dfTrain = spark.read.csv('Data/FeaturizedDataTraining.csv',header=True)
column_names = dfTrain.schema.names[1:]
dfTrain = dfTrain.select(*(F.col(c).cast("float").alias(c) for c in column_names))
dfTrain = dfTrain.withColumn("label", dfTrain["label"].cast(types.IntegerType()))
dfVal = spark.read.csv('Data/FeaturizedDataValidation.csv',header=True)
column_names = dfVal.schema.names[1:]
dfVal = dfVal.select(*(F.col(c).cast("float").alias(c) for c in column_names))
dfVal = dfVal.withColumn("label", dfVal["label"].cast(types.IntegerType()))

dfTrain = dfTrain.withColumn('label',(dfTrain['label']>0).cast('integer'))
dfVal = dfVal.withColumn('label',(dfVal['label']>0).cast('integer'))

#Creating the data frame containing the training data
#pandas_df = pd.read_csv('Data/FeaturizedDataTraining.csv')
#pandas_df = pd.read_csv('Data/FeaturizedDataTrainingSample.csv')
#dfTrain = spark.createDataFrame(pandas_df)

#Creating the data frame containing the testing data
#pandas_df = pd.read_csv('Data/FeaturizedDataValidation.csv')
#pandas_df = pd.read_csv('Data/FeaturizedDataValidationSample.csv')
#dfVal = spark.createDataFrame(pandas_df)

#Creating the data frame containing the examples
pandas_df = pd.read_csv('Data/Examples.csv')
dfExample = spark.createDataFrame(pandas_df)

column_names = dfExample.schema.names[1:501] #column_names is the same for al three dfs

vec_assemb = VectorAssembler(inputCols=column_names, outputCol="Vecfeatures")
vec_assemb.transform(dfTrain)

rf = RandomForestClassifier(labelCol="label", featuresCol="Vecfeatures", numTrees=100)
lsvc = LinearSVC(maxIter=10, regParam=0.1, featuresCol="Vecfeatures")
pipeline = Pipeline(stages=[vec_assemb,rf ])
ML_model = pipeline.fit(dfTrain)
predictions = ML_model.transform(dfVal)

#evaluator = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="weightedRecall")
#recall = evaluator.evaluate(predictions)

evaluator = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="accuracy")
accuracy = evaluator.evaluate(predictions)

TP = predictions.where(predictions.label==1).where(predictions.prediction==1).count()
TN = predictions.where(predictions.label==0).where(predictions.prediction==0).count()
FP = predictions.where(predictions.label==0).where(predictions.prediction==1).count()
FN = predictions.where(predictions.label==1).where(predictions.prediction==0).count()
precision = TP/(TP+FP)
recall = TP/(TP+FN)

print('accuracy',accuracy)
print('recall:',recall)
print('precision',precision)

sample_pred = ML_model.transform(dfExample)

sample_pred.select('sentence', 'prediction').show()
ML_model.save("SavedSVMModel_rf_nt_100")

# dfTrain = spark.read.csv('Data/FeaturizedDataTraining.csv',header=True)
# column_names = dfTrain.schema.names[1:]
# dfTrain = dfTrain.select(*(F.col(c).cast("float").alias(c) for c in column_names))
# dfTrain = dfTain.withColumn("label", dfTrain["label"].cast(types.IntegerType()))

# dfTain = spark.read.csv('Data/FeaturizedDataTraining.csv',header=True)
# column_names = dfTain.schema.names[1:]
# dfTain = dfTain.select(*(F.col(c).cast("float").alias(c) for c in column_names))
# dfTain = dfTain.withColumn("label", dfTain["label"].cast(types.IntegerType()))


from django.db import models

# Create your models here.
class Team(models.Model):
    """ Model class for Teams details   """

    team_id = models.IntegerField (primary_key=True)
    name = models.CharField (max_length=200)
    logo_uri = models.ImageField (upload_to='pics')
    club_state = models.CharField(max_length=200)

    def __str__(self):
        return self.name

class Player(models.Model):
    """ Model class for Player details   """

    player_id = models.IntegerField (primary_key=True)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    image_uri = models.ImageField(upload_to='pics')
    jersey_number = models.PositiveIntegerField()
    country = models.CharField(max_length=100)
    team_id = models.ForeignKey(Team,on_delete = models.CASCADE)

class Matches(models.Model):

    """ Model class for Match Details """

    match_id = models.IntegerField(primary_key=True)
    teams = models.ManyToManyField(Team)
    stadium = models.CharField(max_length=200)
    location = models.CharField(max_length=200)
    winner = models.CharField(max_length=200)
    date = models.DateTimeField(auto_now=True)

class PlayerHistory(models.Model):

    """ Model class for Player performance record  """

    id = models.IntegerField (primary_key=True)
    player_id = models.ForeignKey(Player, on_delete=models.CASCADE)
    no_of_matches = models.PositiveIntegerField()
    total_run = models.PositiveIntegerField()
    highest_score = models.PositiveIntegerField()
    no_of_fifties = models.PositiveIntegerField()
    no_of_hundreds = models.PositiveIntegerField()

class Points(models.Model):

    """Model class for Team Points"""

    id = models.IntegerField(primary_key=True)
    team = models.OneToOneField(Team, on_delete=models.CASCADE)
    points = models.PositiveIntegerField()