import json
import os
import time
import warnings
import numpy as np
import torch
from collections import defaultdict
from torch.utils.data import DataLoader
from transformers import get_linear_schedule_with_warmup
from typing import List, Tuple, Dict

from pathlib import Path

from error_correction.modelling.base_transformer import BaseTransformer
from error_correction.modelling.dataset.error_correction_dataset import (
    ErrorCorrectionSeq2SeqDataset,
)
from error_correction.modelling.lightning_base import add_generic_args
from error_correction.modelling.reader.mask_based_correction_reader import (
    MaskBasedCorrectionReader,
)
from error_correction.modelling.reader.supervised_correction_reader import (
    SupervisedCorrectionReader,
)
from error_correction.modelling.utils import (
    is_truthy,
    SARI_KEYS,
    use_task_specific_params,
    pickle_save,
    freeze_params,
    lmap,
    flatten_list,
    save_json,
    calculate_sari,
    post_clean,
    assert_all_frozen,
)

class ErrorCorrectionModule(BaseTransformer):
    mode = "error-correction"
    val_metric = SARI_KEYS[2]
    loss_names = ["loss"]
    metric_names = SARI_KEYS

    def __init__(self, hparams, **kwargs):
        super().__init__(hparams, num_labels=None, mode=self.mode, **kwargs)
        use_task_specific_params(self.model, "error-correction")

        self.metrics_save_path = Path(self.output_dir) / (
            "metrics.json"
            if not self.hparams.do_predict
            else "metrics_test_{}.json".format(os.path.basename(self.hparams.test_file))
        )

        self.hparams_save_path = Path(self.output_dir) / "hparams.pkl"
        pickle_save(self.hparams, self.hparams_save_path)
        self.step_count = 0
        self.metrics = defaultdict(list)

        self.dataset_kwargs: dict = dict(
            max_source_length=self.hparams.max_source_length,
            mutation_source=is_truthy(self.hparams.mutation_source),
            mutation_target=is_truthy(self.hparams.mutation_target),
        )

        n_observations_per_split = {
            "train": self.hparams.n_train,
            "val": self.hparams.n_val,
            "test": self.hparams.n_test,
        }
        self.n_obs = {
            k: v if v >= 0 else None for k, v in n_observations_per_split.items()
        }

        self.target_lens = {
            "train": self.hparams.max_target_length,
            "val": self.hparams.val_max_target_length,
            "test": self.hparams.test_max_target_length,
        }

        self.data_paths = {
            "train": self.hparams.train_file,
            "val": self.hparams.val_file,
            "test": self.hparams.test_file,
        }

        assert (
            self.target_lens["train"] <= self.target_lens["val"]
        ), f"target_lens: {self.target_lens}"
        assert (
            self.target_lens["train"] <= self.target_lens["test"]
        ), f"target_lens: {self.target_lens}"

        if self.hparams.freeze_embeds:
            self.freeze_embeds()

        if self.hparams.freeze_encoder:
            freeze_params(self.model.get_encoder())
            assert_all_frozen(self.model.get_encoder())

        self.num_workers = hparams.num_workers
        self.decoder_start_token_id = None
        self.dataset_class = ErrorCorrectionSeq2SeqDataset
        self.wiki_reader = self.get_reader(self.hparams.reader, self.hparams.do_predict)

    def get_reader(self, name, test):
        labels = set()
        if self.hparams.labels == "all":
            labels.add("SUPPORTS")
            labels.add("REFUTES")
        else:
            labels.add(self.hparams.labels.upper())

        print("XXX Reader labels {}".format(self.hparams.labels))
        print("XXX labels", labels)

        if name == "supervised":
            return SupervisedCorrectionReader(labels, test)
        elif name == "mask":
            return MaskBasedCorrectionReader(labels, test)
        else:
            raise RuntimeError(f"Unknown reader {name}")

    def freeze_embeds(self):
        """Freeze token embeddings and positional embeddings for bart, just token embeddings for t5."""
        try:
            freeze_params(self.model.model.shared)
            for d in [self.model.model.encoder, self.model.model.decoder]:
                freeze_params(d.embed_positions)
                freeze_params(d.embed_tokens)
        except AttributeError:
            freeze_params(self.model.shared)
            for d in [self.model.encoder, self.model.decoder]:
                freeze_params(d.embed_tokens)

    def forward(self, input_ids, **kwargs):
        return self.model(input_ids, **kwargs)

    def ids_to_clean_text(self, generated_ids: List[int]):
        gen_text = self.tokenizer.batch_decode(
            generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
        )
        return lmap(str.strip, gen_text)

    def _step(self, batch: dict) -> Tuple:
        pad_token_id = self.tokenizer.pad_token_id
        src_ids, src_mask = batch["input_ids"], batch["attention_mask"]
        tgt_ids = batch["decoder_input_ids"]

        decoder_input_ids = self.model._shift_right(tgt_ids)

        outputs = self(
            src_ids,
            attention_mask=src_mask,
            decoder_input_ids=decoder_input_ids,
            use_cache=False,
        )
        lm_logits = outputs[0]

        # Same behavior as modeling_bart.py, besides ignoring pad_token_id
        ce_loss_fct = torch.nn.CrossEntropyLoss(ignore_index=pad_token_id)

        assert lm_logits.shape[-1] == self.model.config.vocab_size
        loss = ce_loss_fct(lm_logits.view(-1, lm_logits.shape[-1]), tgt_ids.view(-1))

        return (loss,)

    def training_step(self, batch, batch_idx) -> Dict:
        loss_tensors = self._step(batch)
        logs = {name: loss for name, loss in zip(self.loss_names, loss_tensors)}
        return {"loss": loss_tensors[0], "log": logs}

    def validation_step(self, batch, batch_idx) -> Dict:
        return self._generative_step(batch)

    def validation_epoch_end(self, outputs, prefix="val") -> Dict:
        self.step_count += 1
        losses = {
            k: torch.stack([x[k] for x in outputs]).mean() for k in self.loss_names
        }
        loss = losses["loss"]

        rouges = {
            k: np.array(flatten_list([x[k] for x in outputs])).mean().item()
            for k in self.metric_names + ["gen_time", "summ_len"]
        }
        rouge_tensor: torch.FloatTensor = torch.tensor(rouges[self.val_metric]).type_as(
            loss
        )
        rouges.update({k: v.item() for k, v in losses.items()})
        losses.update(rouges)
        metrics = {f"{prefix}_avg_{k}": x for k, x in losses.items()}
        metrics["step_count"] = self.step_count

        self.save_metrics(metrics, prefix)  # writes to self.metrics_save_path

        preds = flatten_list([x["preds"] for x in outputs])
        targets = flatten_list([x["target"] for x in outputs])
        metadata = flatten_list([x["metadata"] for x in outputs])

        self.save_predictions(preds, targets, metadata, prefix)

        return {
            "log": metrics,
            "preds": preds,
            f"{prefix}_loss": loss,
            f"{prefix}_{self.val_metric}": rouge_tensor,
        }

    def save_metrics(self, latest_metrics, type_path) -> None:
        self.metrics[type_path].append(latest_metrics)
        save_json(self.metrics, self.metrics_save_path)

    def save_predictions(self, predictions, actual, metadata, type_path) -> None:
        with open(
            Path(self.output_dir)
            / (
                "predictions_set_{}_epoch_{}_steps_{}_.jsonl".format(
                    type_path, self.trainer.current_epoch, self.step_count
                )
                if not self.hparams.do_predict
                else "final_predictions_set_{}_file_{}".format(
                    type_path, os.path.basename(self.hparams.test_file)
                )
            ),
            "w+",
        ) as f:
            for p, a, m in zip(predictions, actual, metadata):
                f.write(
                    json.dumps({"prediction": p, "actual": a, "metadata": m}) + "\n"
                )

    def _generative_step(self, batch: dict) -> dict:
        pad_token_id = self.tokenizer.pad_token_id
        (
            source_ids,
            source_mask,
            y,
            original_ids,
        ) = ErrorCorrectionSeq2SeqDataset.trim_seq2seq_batch(batch, pad_token_id)
        t0 = time.time()
        generated_ids = self.model.generate(
            input_ids=source_ids,
            attention_mask=source_mask,
            use_cache=True,
            decoder_start_token_id=self.decoder_start_token_id,
            max_length=self.hparams.val_max_target_length,
        )
        gen_time = (time.time() - t0) / source_ids.shape[0]
        preds = self.ids_to_clean_text(generated_ids)
        target = self.ids_to_clean_text(y)
        original = self.ids_to_clean_text(original_ids)
        loss_tensors = self._step(batch)
        base_metrics = {name: loss for name, loss in zip(self.loss_names, loss_tensors)}
        metrics: Dict = self.calc_generative_metrics(original, preds, target)
        summ_len = lmap(len, generated_ids)
        base_metrics.update(
            gen_time=[gen_time],
            summ_len=summ_len,
            preds=preds,
            target=target,
            metadata=batch["metadata"],
            **metrics,
        )
        return base_metrics

    def calc_generative_metrics(self, originals, preds, target) -> Dict:
        # return calculate_rouge([p.replace(": ","") for p in preds], [p.replace("correction: ","") for p in target])

        return calculate_sari(
            originals, [post_clean(p) for p in preds], [post_clean(p) for p in target]
        )

    def test_step(self, batch, batch_idx):
        return self._generative_step(batch)

    def test_epoch_end(self, outputs):
        return self.validation_epoch_end(outputs, prefix="test")

    def get_dataset(self, type_path) -> ErrorCorrectionSeq2SeqDataset:
        n_obs = self.n_obs[type_path]
        max_target_length = self.target_lens[type_path]
        instance_generator = self.wiki_reader.read(self.data_paths[type_path])

        dataset = self.dataset_class(
            self.tokenizer,
            instance_generator=instance_generator,
            n_obs=n_obs,
            max_target_length=max_target_length,
            **self.dataset_kwargs,
        )
        return dataset

    def get_dataloader(
        self, type_path: str, batch_size: int, shuffle: bool = False
    ) -> DataLoader:
        dataset = self.get_dataset(type_path)
        sampler = None
        if self.hparams.sortish_sampler and type_path == "train":
            assert self.hparams.gpus <= 1  # TODO: assert earlier
            sampler = dataset.make_sortish_sampler(batch_size)
            shuffle = False

        dataloader = DataLoader(
            dataset,
            batch_size=batch_size,
            collate_fn=dataset.collate_fn,
            shuffle=shuffle,
            num_workers=self.num_workers,
            sampler=sampler,
        )
        return dataloader

    def train_dataloader(self) -> DataLoader:
        dataloader = self.get_dataloader(
            "train", batch_size=self.hparams.train_batch_size, shuffle=True
        )
        t_total = (
            (
                len(dataloader.dataset)
                // (self.hparams.train_batch_size * max(1, self.hparams.gpus))
            )
            // self.hparams.accumulate_grad_batches
            * float(self.hparams.max_epochs)
        )
        scheduler = get_linear_schedule_with_warmup(
            self.opt,
            num_warmup_steps=self.hparams.warmup_steps,
            num_training_steps=t_total,
        )
        if max(scheduler.get_last_lr()) > 0:
            warnings.warn("All learning rates are 0")
        self.lr_scheduler = scheduler
        return dataloader

    def val_dataloader(self) -> DataLoader:
        return self.get_dataloader("val", batch_size=self.hparams.eval_batch_size)

    def test_dataloader(self) -> DataLoader:
        return self.get_dataloader("test", batch_size=self.hparams.eval_batch_size)

    @staticmethod
    def add_model_specific_args(parser, root_dir):
        BaseTransformer.add_model_specific_args(parser, root_dir)
        add_generic_args(parser, root_dir)

        parser.add_argument("--reader", default="wiki", type=str)
        parser.add_argument("--train_file", required=True, type=str)
        parser.add_argument("--val_file", required=True, type=str)
        parser.add_argument("--test_file", required=False, type=str)
        parser.add_argument(
            "--max_source_length",
            default=512,
            type=int,
            help="The maximum total input sequence length after tokenization. Sequences longer "
            "than this will be truncated, sequences shorter will be padded.",
        )
        parser.add_argument(
            "--max_target_length",
            default=256,
            type=int,
            help="The maximum total input sequence length after tokenization. Sequences longer "
            "than this will be truncated, sequences shorter will be padded.",
        )
        parser.add_argument(
            "--val_max_target_length",
            default=256,  # these defaults are optimized for CNNDM. For xsum, see README.md.
            type=int,
            help="The maximum total input sequence length after tokenization. Sequences longer "
            "than this will be truncated, sequences shorter will be padded.",
        )
        parser.add_argument(
            "--test_max_target_length",
            default=256,
            type=int,
            help="The maximum total input sequence length after tokenization. Sequences longer "
            "than this will be truncated, sequences shorter will be padded.",
        )
        parser.add_argument("--freeze_encoder", action="store_true")
        parser.add_argument("--freeze_embeds", action="store_true")
        parser.add_argument("--sortish_sampler", action="store_true", default=False)
        parser.add_argument(
            "--logger_name",
            type=str,
            choices=["default", "wandb", "wandb_shared"],
            default="default",
        )
        parser.add_argument(
            "--n_train",
            type=int,
            default=-1,
            required=False,
            help="# examples. -1 means use all.",
        )
        parser.add_argument(
            "--n_val",
            type=int,
            default=-1,
            required=False,
            help="# examples. -1 means use all.",
        )
        parser.add_argument(
            "--n_test",
            type=int,
            default=-1,
            required=False,
            help="# examples. -1 means use all.",
        )
        parser.add_argument("--mutation_source", required=True)
        parser.add_argument("--mutation_target", required=True)
        parser.add_argument(
            "--labels", type=str, choices=["supports", "refutes", "all"], required=True
        )

        return parser


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

'''
Given a list, rotate the list to the right by k places, where k is non-negative.

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.
'''

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if not (head and head.next):
            return head

        length = 1
        node = head
        while node.next:
            node = node.next
            length += 1
        node.next = head

        tail, cur = node, head

        # 右旋k, 其实就是左旋(length - k % length)
        n = length - k % length
        while n > 0:
            cur = cur.next
            tail = tail.next
            n -= 1
        tail.next = None

        return cur


import json
import jieba
import numpy as np
from sklearn.cluster import KMeans
from collections import Counter
from pyecharts import options as opts
from pyecharts.charts import Tree
import os
from pyspark.sql import SparkSession, SQLContext

os.environ['JAVA_HOME'] = "/usr/local/java/jdk1.8"
spark = SparkSession.builder.getOrCreate()
jdbcDF = spark.read.format("jdbc") \
    .option("driver", "com.mysql.cj.jdbc.Driver")\
    .option("url", "jdbc:mysql://plane.mysql.database.azure.com:3306/weibo") \
    .option("user", "mykaneki") \
    .option("dbtable", "weibo") \
    .option("password", "azureLinux668CMY...").load()

stopwords = [line.strip() for line in open("../stopwords/hit_stopwords.txt", 'r', encoding="utf-8").readlines()]
stopwords_other = ['\n', ' ']
stopwords.extend(stopwords_other)

load_dict = jdbcDF.select('topics').take(500)

title_list = []
title_word_list = set()
for dic in load_dict:
    title = dic['topics']
    if title in title_list:
        continue
    word_cut = jieba.lcut(title)
    word_cut = [i for i in word_cut if i not in stopwords]
    title_word_list |= set(word_cut)
    title_list.append(title)

title_list = list(set(title_list))
title_word_list = list(title_word_list)
vsm_list = []
for title in title_list:
    temp_vector = []
    # print(title)
    for word in title_word_list:
        temp_vector.append(title.count(word) * 1.0)
    vsm_list.append(temp_vector)

docs_matrix = np.array(vsm_list)
column_sum = [float(len(np.nonzero(docs_matrix[:, i])[0]))
              for i in range(docs_matrix.shape[1])]
column_sum = np.array(column_sum)
column_sum = docs_matrix.shape[0] / column_sum
idf = np.log(column_sum)
idf = np.diag(idf)
tfidf = np.dot(docs_matrix, idf)

clf = KMeans(n_clusters=20)
clf.fit(tfidf)
centers = clf.cluster_centers_
labels = clf.labels_
# print(labels)
list_divide = {}
for i in range(len(labels)):
    id = str(labels[i])
    if id in list_divide.keys():
        list_divide[id].append(title_list[i])
    else:
        list_divide[id] = []

list_divide_plus = {}
word_used = []
key_word = []
for k, v in list_divide.items():
    word = []
    for i in v:
        word.extend(jieba.lcut(i))
    word = [i for i in word if i not in stopwords]
    word_count = Counter(word)
    word = word_count.most_common(20)
    for i in word:
        if i[0] not in word_used:
            key_word.append(i[0])
            list_divide_plus[i[0]] = v
            word_used.append(i[0])
            break

dic_data = []
dic_tree = {}
dic_tree["name"] = "主题树"
tree_list = []
for k, v in list_divide_plus.items():
    dic = {}
    dic["name"] = k
    list = []
    for v1 in v:
        dic1 = {}
        dic1["name"] = v1
        list.append(dic1)
    dic["children"] = list
    tree_list.append(dic)
dic_tree["children"] = tree_list
dic_data.append(dic_tree)
print(dic_data)
c = (
    Tree()
    .add("", data=dic_data, layout="radial", symbol="emptyCircle", symbol_size=7)
    .set_global_opts(title_opts=opts.TitleOpts(title="主题树展示"))
    .render("tree.html")
)


from django import forms
from pages.models import Page, Menu, Contact
from micro_blog.models import Subscribers, Category
import json

class PageForm(forms.ModelForm):
    contact_info = forms.CharField(max_length=5000, required=False)

    class Meta:
        model = Page
        exclude = ('category', 'contact_info')

    def __init__(self, *args, **kwargs):
        super(PageForm, self).__init__(*args, **kwargs)

    def clean_contact_info(self):
        if self.cleaned_data['contact_info']:
            valid = False
            print (json.loads(str(self.cleaned_data['contact_info'])))
            try:
                if json.loads(str(self.cleaned_data['contact_info'])):
                    valid = True
            except:
                valid = False
            if valid:
                return self.cleaned_data['contact_info']
            else:
                raise forms.ValidationError('Enter A Valid Json')

    def save(self, commit=True):
        instance = super(PageForm, self).save(commit=False)
        instance.country_id = self.cleaned_data['country']
        if commit:
            instance.save()
        return instance

class MenuForm(forms.ModelForm):

    class Meta:
        model = Menu
        exclude = ('lvl', 'url')

class ContactForm(forms.ModelForm):
    email = forms.EmailField()
    full_name = forms.CharField(max_length=200)
    message = forms.CharField(max_length=200)
    country = forms.CharField(max_length=200, required=False)
    enquery_type = forms.CharField(max_length=200, required=False)

    class Meta:
        model = Contact
        exclude = ('full_name', 'message', 'email', 'phone', 'contact_info', 'country', 'enquery_type')

    def __init__(self, *args, **kwargs):
        super(ContactForm, self).__init__(*args, **kwargs)
        # if 'country' in self.data.keys():
        #     self.fields['country'].required = True

        if 'enquery_type' in self.data.keys():
            self.fields['enquery_type'].required = True

class SubscribeForm(forms.ModelForm):
    class Meta:
        model = Subscribers
        fields = ('email',)

    def __init__(self, *args, **kwargs):
        super(SubscribeForm, self).__init__(*args, **kwargs)

    def clean_email(self):

        if str(self.data['is_blog']) == 'True':
            if len(self.data['is_category']) > 0:
                category = Category.objects.get(id=self.data['is_category'])
                existing_subscribers = Subscribers.objects.filter(email=self.data['email'], blog_post=True, category=category)
            else:
                existing_subscribers = Subscribers.objects.filter(email=self.data['email'], blog_post=True, category=None)
        else:
            existing_subscribers = Subscribers.objects.filter(email=self.data['email'], blog_post=False)
        if existing_subscribers:
            raise forms.ValidationError('User with email id is already Subscribed')

        return self.data['email']


import os
from ruamel.yaml import YAML

from tex_builder import TexBuilder

def main():
    os.system('rm -rf lib')
    os.system('cp -r ../lib lib')

    yaml = YAML(typ='safe')

    f = open('content.yml')
    yml_file_content = f.read()
    f.close()
    content = yaml.load(yml_file_content)

    tex = TexBuilder()

    for topic in content.keys():
        tex.section(topic)
        topic_dir = content[topic][0]['dir']
        subtopics = content[topic][1]['subtopics']

        for subtopic in subtopics:
            tex.subsection((subtopic['name']))
            file_path = '{0}/{1}'.format(topic_dir, subtopic['file'])
            tex.code_file(file_path)

    with open('content.tex', 'w') as f:
        f.write(tex.dump())

    os.system('latexmk -pdf -shell-escape main.tex')

if __name__ == "__main__":
    main()


#will be simplistic so that it can directly be fed to the trainloader/other loaders
#will read from text files
#new mode option to read from different text files

from torch.utils.data import Dataset
import numpy as np
from torchvision import transforms
import helper_func
import info

#print(type(self.train))
class OCTdatasetV2(Dataset):

    def __init__(self,transform = transforms.Compose([transforms.ToTensor()]),train = False, valid = False,test = False):
        super().__init__()
        self.octdata = helper_func.read_h5()
        self.transform = transform
        self.train = train
        self.valid = valid
        self.test = test

        if self.train is True:
            print("Train")
            self.train_labels = helper_func.read_files(info.train_file_loc)

        elif self.valid is True:
            print("Valid")
            self.valid_labels = helper_func.read_files(info.valid_file_loc)

        elif self.test is True:
            print("Test")
            self.test_labels = helper_func.read_files(info.test_file_loc)

    def __len__(self):
        if self.train is True:
            return len(self.train_labels)
        elif self.valid is True:
            return len(self.valid_labels)
        elif self.test is True:
            return len(self.test_labels)

    def __getitem__(self, index):

       if self.train is True:
           init_label,label = self.train_labels[index]
           init_data = self.octdata[init_label][0]
           datum = self.octdata[label][0]
           groundtruth = np.subtract(self.octdata[init_label][1],self.octdata[label][1])
           groundtruth = np.transpose(groundtruth)
           if self.transform is not None:
                datum = self.transform(datum)
                datum = datum.reshape(1,-1,64,64)
                init_data = self.transform(init_data)
                init_data = init_data.reshape(1,-1,64,64)
           return init_data,datum,groundtruth

       elif self.valid is True:
           init_label,label = self.valid_labels[index]
           init_data = self.octdata[init_label][0]
           datum = self.octdata[label][0]
           groundtruth = np.subtract(self.octdata[init_label][1],self.octdata[label][1])
           groundtruth = np.transpose(groundtruth)
           if self.transform is not None:
                datum = self.transform(datum)
                datum = datum.reshape(1,-1,64,64)
                init_data = self.transform(init_data)
                init_data = init_data.reshape(1,-1,64,64)
           return init_data,datum,groundtruth

       elif self.test is True:
           init_label,label = self.test_labels[index]
           init_data = self.octdata[init_label][0]
           datum = self.octdata[label][0]
           groundtruth = np.subtract(self.octdata[init_label][1],self.octdata[label][1])
           groundtruth = np.transpose(groundtruth)
           if self.transform is not None:
                datum = self.transform(datum)
                datum = datum.reshape(1,-1,64,64)
                init_data = self.transform(init_data)
                init_data = init_data.reshape(1,-1,64,64)
           return init_data,datum,groundtruth

#octdata = OCTdatasetV2(train=True)


from mpl_toolkits.basemap import shapefile
from collections import defaultdict
from itertools import groupby
import numpy as np

storm_dtype = [('xcent', 'f'), ('ycent', 'f'), ('frame_index', 'i'),
               ('frame_time', 'f'), ('feat_size', 'f'),
               ('feat_id', 'i'), ('track_id', 'i'), ('poly', 'O')]
izip = zip

def storm_loader(filename):
    """
    Return a flat table of data. Each row is a feature.
    Columns are

    xcent, ycent, frame_index, frame_time, feat_size, feat_id, track_id
    and then a Nx2 array of polygon vertices ('poly').

    """
    shp = shapefile.Reader(filename)
    storms = np.array([tuple(record) + (np.array(poly.points),)
                       for record, poly in
                       izip(shp.iterRecords(), shp.iterShapes())],
                      dtype=storm_dtype)
    return storms

def storm_saver(filename, storms):
    """
    Save a numpy structured array (or, at least a list of numpy
    elements with the proper dtypes) where each row is a feature.
    Columns are

    xcent, ycent, frame_index, frame_time, feat_size, feat_id, track_id
    and then a Nx2 array of polygon vertices ('poly').
    """
    shp = shapefile.Writer(shapeType=shapefile.POLYGON)
    shp.field('x', 'N', decimal=6)
    shp.field('y', 'N', decimal=6)
    shp.field('frameindx', 'N')
    shp.field('frametime', 'N', decimal=4)
    shp.field('size', 'N', decimal=2)
    shp.field('cornerid', 'N')
    shp.field('trackid', 'N')
    for storm in storms:
        shp.record(storm['xcent'], storm['ycent'], storm['frame_index'],
                   storm['frame_time'], storm['feat_size'], storm['feat_id'],
                   storm['track_id'])
        if storm['poly'] is not None and len(storm['poly']) > 0:
            shp.poly([storm['poly'].tolist()])
        else:
            shp.null()
    if filename.endswith('.shp'):
        filename = filename[:-4]
    shp.save(filename)

def track_loader(filename, group='track_id'):
    storms = storm_loader(filename)
    tracks = defaultdict(list)
    for key, items in groupby(storms, lambda x: x[group]):
        for storm in items:
            tracks[key].append((storm['xcent'], storm['ycent']))
    return tracks

def polygon_loader(filename, group='frame_index'):
    storms = storm_loader(filename)
    polygons = defaultdict(list)
    for key, items in groupby(storms, lambda x: x[group]):
        for storm in items:
            polygons[key].append(storm['poly'])
    return polygons


"""
460. LFU Cache
Design and implement a data structure for a Least Frequently Used (LFU) cache.

Implement the LFUCache class:
- LFUCache(int capacity) Initializes the object with the capacity of the data structure.
- int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, return -1.
- void put(int key, int value) Update the value of the key if present, or inserts the key if not already present. When the cache reaches its capacity, it should invalidate and remove the least frequently used key before inserting a new item. For this problem, when there is a tie (i.e., two or more keys with the same frequency), the least recently used key would be invalidated.

To determine the least frequently used key, a use counter is maintained for each key in the cache. The key with the smallest use counter is the least frequently used key.

When a key is first inserted into the cache, its use counter is set to 1 (due to the put operation). The use counter for a key in the cache is incremented either a get or put operation is called on it.

Example1:
Input
["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]
Output
[null, null, null, 1, null, -1, 3, null, -1, 3, 4]

Explanation
// cnt(x) = the use counter for key x
// cache=[] will show the last used order for tiebreakers (leftmost element is  most recent)
LFUCache lfu = new LFUCache(2);
lfu.put(1, 1);   // cache=[1,_], cnt(1)=1
lfu.put(2, 2);   // cache=[2,1], cnt(2)=1, cnt(1)=1
lfu.get(1);      // return 1
                 // cache=[1,2], cnt(2)=1, cnt(1)=2
lfu.put(3, 3);   // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2.
                 // cache=[3,1], cnt(3)=1, cnt(1)=2
lfu.get(2);      // return -1 (not found)
lfu.get(3);      // return 3
                 // cache=[3,1], cnt(3)=2, cnt(1)=2
lfu.put(4, 4);   // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1.
                 // cache=[4,3], cnt(4)=1, cnt(3)=2
lfu.get(1);      // return -1 (not found)
lfu.get(3);      // return 3
                 // cache=[3,4], cnt(4)=1, cnt(3)=3
lfu.get(4);      // return 4
                 // cache=[3,4], cnt(4)=2, cnt(3)=3

Constraints:
0 <= capacity <= 10^4
0 <= key <= 10^5
0 <= value <= 10^9
At most 2 * 10^5 calls will be made to get and put.
"""

"""
Note:
1. Two Hash Tables <Integer, DLList> freq: O(1) time | O(1) space
{
    1: head <-> (1,1) <-> (2, 2) <-> tail
    2: head <-> (3,2) <-> tail
}

(1) Each key is mapping to the corresponding node (self._node), where we can retrieve the node in O(1) time
(2) Each frequency freq is mapped to a Doubly Linked List (self._freq), where all nodes in the DLList have the same frequency. Moreover, each node will be always inserted in the head (indicating most recently used).
(3) A minimum frequency self._minfreq is maintained to keep track of the minimum frequency of across all nodes in this cache, such that the DLList with the min frequency can always be retrieved in O(1) time.
"""

import unittest
class ListNode:
    def __init__(self, key: int, val: int) -> None:
        self.val = val
        self.key = key
        self.count = 1
        self.prev = None
        self.next = None

class DLList:
    def __init__(self):
        self.dummyHead = ListNode(None, None)
        self.dummyTail = ListNode(None, None)
        self.dummyHead.next, self.dummyTail.prev = self.dummyTail, self.dummyHead
        self._size = 0

    def __len__(self):
        return self._size
    def addHead(self, node) -> None:
        next = self.dummyHead.next
        node.next, node.prev = next, self.dummyHead
        self.dummyHead.next = next.prev = node
        self._size += 1

    def remove(self, node) -> None:
        node.prev.next, node.next.prev = node.next, node.prev
        self._size -= 1

    def removeTail(self) -> int:
        prev = self.dummyTail.prev
        self.remove(prev)
        return prev.key

from collections import defaultdict

class LFUCache:
    def __init__(self, capacity: int):
        self._size = 0
        self._capacity = capacity

        self._node = dict() # <key, ListNode>
        self._freq = defaultdict(DLList)
        self._minfreq = 0

    def _update(self, node):
        freq = node.count
        self._freq[freq].remove(node)
        if self._minfreq == freq and not self._freq[freq]:
            self._minfreq += 1

        node.count += 1
        freq = node.count
        self._freq[freq].addHead(node)

    def get(self, key):
        if key not in self._node:
            return -1

        node = self._node[key]
        self._update(node)
        return node.val

    def put(self, key, value):
        if self._capacity == 0:
            return
        if key in self._node:
            node = self._node[key]
            self._update(node)
            node.val = value
        else:
            if self._size == self._capacity:
                keyToRemove = self._freq[self._minfreq].removeTail()
                del self._node[keyToRemove]
                self._size -= 1

            node = ListNode(key, value)
            self._node[key] = node
            self._freq[1].addHead(node)
            self._minfreq = 1
            self._size += 1

# Unit Tests
classes = [LFUCache]

class TestLFUCache(unittest.TestCase):
    def testLFUCache1(self):
        for myclass in classes:
            lfu = myclass(2)
            lfu.put(1, 1)
            lfu.put(2, 2)
            self.assertEqual(lfu.get(1), 1)
            lfu.put(3, 3)
            self.assertEqual(lfu.get(2), -1)
            self.assertEqual(lfu.get(3), 3)
            lfu.put(4, 4)
            self.assertEqual(lfu.get(1), -1)
            self.assertEqual(lfu.get(3), 3)
            self.assertEqual(lfu.get(4), 4)

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


def solution(xs):
    ans=0
    arr=sorted(xs)
    pos,neg=[],[]
    for i in arr:
        if i>0:
            pos.append(i)
        elif i<0:
            neg.append(i)
    if len(neg)%2!=0:
        if len(neg)!=1:
             neg=neg[:-1]
        elif len(neg)==1 and len(pos)==0:
            if 0 in xs:
                return str(0)
            return str(neg[0])
        elif len(neg)==1 and len(pos)!=0:
            neg=[]
    pos+=neg
    count=0
    for i in pos:
        if count==0:
            ans=1
        ans*=i
        count+=1
    ans=str(ans)
    return ans

if __name__=="__main__":
    arr=[2,-2,-3,5]
    print(solution(arr))


from flask import request
from flasgger.utils import swag_from
from flask import Blueprint

from usersServiceApp.core.register_logic import validate_and_create_level
from usersServiceApp.errors.usersException import usersException
from flask import jsonify

from usersServiceApp.infra.db_level import get_all_levels

bp_level = Blueprint('level', __name__, url_prefix='/level/')

@bp_level.route("/", methods=['POST'])
@swag_from(methods=['POST'])
def new_level():
    """
    Register a new level
    The form has to be complete.
    ---
    tags:
      - level
    consumes:
      - application/json
    parameters:
      - name: body
        in: body
        required: true
        schema:
            required:
              - level_description
            properties:
              level_description:
                type: string
                description: description of the new level.
    responses:
      200:
        description: A successful profile creation
        schema:
          properties:
              level_description:
                type: string
                description:  description of the new level.
              id:
                type: integer
                description: Unique identifier of the created level
    """
    try:
        post_data = request.get_json()
        level_created = validate_and_create_level(post_data)
    except usersException as e:
        return jsonify({'Error': e.message}), e.error_code
    return jsonify({'id': level_created.id_level, "level_description": level_created.level_description}), 200

@bp_level.route("/", methods=['GET'])
@swag_from(methods=['GET'])
def all_levels():
    """
    Get all levels
    ---
    tags:
      - level
    consumes:
      - application/json
    parameters:
      - name: body
        in: body
        required: false
    responses:
      200:
        description: A list of levels created
    """
    levels = get_all_levels()
    list_levels = []
    for level in levels:
        lang = {
            'id_level': level.id_level,
            "level_description": level.level_description
        }
        list_levels.append(lang)
    return jsonify(list_levels), 200


# Exercício 10:
#Crie uma função que converta um valor em horas para minutos e segundos.
#Solicite ao usuário que digite o valor em horas e, em seguida,
#exiba as conversões para minutos e segundos.
#Fórmula para converter horas em minutos: Minutos = Horas * 60
#Fórmula para converter horas em segundos: Segundos = Horas * 3600

import os
import re

def validar_horario(horario):
    padrao = r'^([01]?[0-9]|2[0-3]):[0-5][0-9]$'
    if re.match(padrao, horario):
        return True
    else:
        return False

def obter_horas():
    while True:
        horario = input('Digite o horario (HH:mm) que deseja converter ou "s" para sair: ')
        if horario.lower() == 's':
            break
        if validar_horario(horario):
            return horario
            #print(horario)
        else:
            print(
                f'\nHorario invalido!! Digite horario no formado HH:mm. **voce digitou: "{horario}"\n'
            )

def converter_horario(horario):
    horas, minutos = horario.split(':')
    horas = float(horas)
    minutos = float(minutos)
    resultado_minutos = (horas * 60) + minutos
    resultado_segundos = (horas * 3600) + (minutos * 60)
    return resultado_minutos, resultado_segundos

def exibir_horarios_convertidos(horario, resultado_minutos, resultado_segundos):
    print(
        f'-' *70 +
        f'\nO horario digitado -> {horario}\n'+
        f'-' *70 +
        f'\nO resultado da conversao do horario informado para minutos -> {resultado_minutos:,.2f} minutos\n'+
        f'-' *85 +
        f'\nO resultado da conversao do horario informado para segundos -> {resultado_segundos:,.2f} segundos\n'+
        f'='*85
    )

def main():
    horario = obter_horas()
    resultado_minutos, resultado_segundos = converter_horario(horario)
    exibir_horarios_convertidos(horario, resultado_minutos, resultado_segundos)

if __name__ =='__main__':
    os.system('clear')
    main()


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

import numpy as np
import cv2

if __name__ == '__main__':
    import sys
    try:
        fn = sys.argv[1]
    except:
        fn = '../temp/cat.jpg'

    img = cv2.imread(fn)
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    def nothing(x):
        pass

    cv2.namedWindow('HSV', 0)
    cv2.createTrackbar( "H", "HSV", 127, 255, nothing)
    cv2.createTrackbar( "S", "HSV", 127, 255, nothing)
    cv2.createTrackbar( "V", "HSV", 127, 255, nothing)

    while True:
        k = cv2.waitKey(1) & 0xFF
        if k == 27:
            break
        _hsv = hsv.copy()
        _hsv[:,:,0] += cv2.getTrackbarPos("H", "HSV") - 127
        _hsv[:,:,1] += cv2.getTrackbarPos("S", "HSV") - 127
        _hsv[:,:,2] += cv2.getTrackbarPos("V", "HSV") - 127
        cv2.imshow("Image Changing", cv2.cvtColor(_hsv, cv2.COLOR_HSV2BGR))


import hashtable

def test_init():
    '''test initialization of hashtable'''
    minsize = 8
    h = hashtable.Hashtable(minsize,one="one", two="two", three="three", four="four", five="five", six="six", seven="seven", eight="eight", nine="nine")
    assert len(h._buckets)  ==  8 and  h.CHAIN_ELEMENTS == 9

def test_get():
    '''test location of keys'''
    minsize = 8
    h = hashtable.Hashtable(minsize,one="one", two="two", three="three", four="four", five="five", six="six", seven="seven", eight="eight", nine="nine")
    assert h.get('four') == 'four'

def test_delete():
    ''' test deletion of keys'''
    minsize = 8
    # Remove a bunch of the keys
    h = hashtable.Hashtable(minsize,one="one", two="two", three="three", four="four", five="five", six="six", seven="seven", eight="eight", nine="nine")
    del h['one']
    del h['two']
    del h['three']
    del h['four']
    del h['five']
    del h['six']
    del h['seven']
    del h['eight']
    assert h.CHAIN_ELEMENTS == 1

def test_insert_after_del():
    '''test insertion of keys after deletion'''
    minsize = 8
    # Remove some of the keys
    h = hashtable.Hashtable(minsize,one="one", two="two", three="three", four="four", five="five", six="six", seven="seven", eight="eight", nine="nine")
    del h['one']
    del h['two']
    del h['three']
    del h['four']
    del h['five']
    del h['six']
    del h['seven']
    del h['eight']
    h['ten'] = 10
    assert h.get('ten') == 10

def test_resize():
    '''test resizing of hashtable once load factor is reached'''
    minsize = 3
    h = hashtable.Hashtable(minsize)
    h['one'] = "one"
    h['two'] = "two"
    h['three'] = "three"
    h['four'] = "four"
    h['five'] = "five"
    h['six'] = "six"
    h['seven'] = "seven"
    h['eight'] = "eight"
    h['nine'] = "nine"
    h['ten'] = 10
    #After resize, the number of (active) chains in the table should be 4 and total chain elements should be 10
    assert h.CHAIN_ELEMENTS == 10 and h.NUM_CHAINS == 4

def test_add_duplicate():
    '''test if the hashtable handles duplicates'''
    minsize = 8
    h  = hashtable.Hashtable(minsize,one="one", two="two", three="three", four="four", five="five", six="six", seven="seven", eight="eight", nine="nine")
    h["one"] = 17
    assert h.get("one") == 17


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

class PageAddProductPO:

    def handle_category_list(self, category_list):

        '''

        :param category_list:
        :return:
        category_info = {'level_count', 'category_info':{'cate_1','cate_2'...}}
        '''
        category_info = {}

        #计算有多少级
        cate_sperate_list = []
        temp_list = category_list
        root_list = [0]
        count_used = 0
        while True:
            temp_sperate_list = []
            temp_root_list = []
            for item in temp_list:
                for parent_id in root_list:
                    if item['parent_category_id'] == parent_id:
                        temp_sperate_list.append(item)
                        temp_root_list.append(item['category_id'])
                        count_used = count_used+1
            cate_sperate_list.append(temp_sperate_list)
            root_list = temp_root_list
            if count_used == len(temp_list):
                break

        return cate_sperate_list

# test_list = [{'is_delete': 0L, 'category_id': 1L, 'category_content': u'\u52a8\u7269', 'parent_category_id': 0L},
#              {'is_delete': 0L, 'category_id': 2L, 'category_content': u'\u690d\u7269', 'parent_category_id': 0L},
#              {'is_delete': 0L, 'category_id': 3L, 'category_content': u'\u852c\u83dc', 'parent_category_id': 2L},
#              {'is_delete': 0L, 'category_id': 4L, 'category_content': u'\u6c34\u679c', 'parent_category_id': 2L},
#              {'is_delete': 0L, 'category_id': 5L, 'category_content': u'\u5bb6\u79bd', 'parent_category_id': 1L},
#              {'is_delete': 0L, 'category_id': 6L, 'category_content': u'\u5bb6\u755c', 'parent_category_id': 1L}]
# a= PageAddProductPO().handle_category_list(test_list)
# print a[0]
# print a[1]


import io
import sys

_INPUT = """\
4
90 180 45 195
"""
sys.stdin = io.StringIO(_INPUT)

n = int(input())
a = list(map(int, input().split()))

li = []
li.append(360)
cnt = 0

for i in range(n):
    cnt += a[i]
    tmp = cnt // 360
    if tmp:
        ans = cnt - tmp*360
        li.append(ans)
    else:
        li.append(cnt)

li.sort()
num = 0
for i in range(1, len(li)):
    num = max(li[-i] - li[-i-1], num)
print(num)


from dateutil.relativedelta import relativedelta

from odoo import fields, models

class AccountPaymentTermLine(models.Model):
    _inherit = "account.payment.term.line"

    def write(self, vals):
        res = super().write(vals)
        if vals.get("discount_days"):
            for item in self:
                # get all invoice related to this payment term and update
                # validity discount date
                invoice_ids = self.env["account.move"].search(
                    [
                        ("state", "=", "posted"),
                        ("invoice_payment_term_id", "=", item.payment_id.id),
                    ]
                )
                for inv in invoice_ids:
                    # Check payment date discount validation
                    invoice_date = fields.Date.from_string(inv.invoice_date)
                    # Update discount validity days
                    for line in inv.invoice_payment_term_id.line_ids:
                        inv.discount_date = invoice_date + relativedelta(
                            days=line.discount_days
                        )
        return res


"""
* Title: Egg Drop
* Author: Sam Gavis-Hughson
* Date: 11/28/2017
*
* Consider dropping eggs off of a building. If you drop an egg from the Xth floor
* and it breaks, then you know for all eggs dropped from floor X or above, they
* will break. Similarly, if you drop an egg from floor X and it doesn't break, no
* egg will break when dropped from floors 1 through X. Given a building with N
* floors and E eggs, find the minimum number of drops needed to determine the
* maximum floor that you can drop an egg from without it breaking.
*
* eg.
*
* eggs = 1
* floors = 10
* eggDrop(eggs, floors) = 10
* If you only have one egg, you need to try every floor from 1 to floors.
*
* eggs = 2
* floors = 10
* eggDrop(eggs, floors) = 4
* Drop the egg from the 4th floor, then the 7th, then the 10th. Each time, if
* the egg breaks, go to one above the previous test and progress up linearly.
*
* Execution: python egg_drop.py
"""
import unittest
from typing import List

def brute_force_egg_drop(eggs: int, floors: int):
    """
    Brute force recursive solution. Try dropping an egg from every floor,
    then recursively try the case where it breaks and the case where it
    doesn't to see how many drops they take. floors indicates the range of
    floors that need to be tested, not the total floors. Eg. if the egg broke
    at floor 20 and not at floor 5, then floors = 15
    """
    if floors == 0:
        return 0
    if floors == 1:
        return 1
    # If we only have one egg, we have to go up one at a time through each
    # floor to guarantee that we find the exact floor
    if eggs == 1:
        return floors

    max_drops = float("inf")

    # Try dropping the egg from each height. Compute the worst case for if
    # it breaks and if it doesn't break
    for i in range(1, floors):
        # If the egg breaks, then the floor we're looking for could be any
        # floor less than i
        breaks = brute_force_egg_drop(eggs - 1, i - 1)

        # If the egg doesn't break, we can exclude all the floors below and
        # including the current floor (i)
        doesnt_break = brute_force_egg_drop(eggs, floors - i)
        max_drops = min(max_drops, max(breaks, doesnt_break))
    return max_drops + 1

def top_down_egg_drop(eggs: int, floors: int):
    dp = [[0]*(floors+1)]*(eggs+1)
    return top_down_egg_drop_helper(eggs, floors, dp)

def top_down_egg_drop_helper(eggs: int, floors: int, dp: List[List[int]]):
    if floors == 0:
        return 0
    if floors == 1:
        return 1
    if eggs == 1:
        return floors

    if dp[eggs][floors] == 0:
        max_drops = float("inf")
        for i in range(1, floors):
            breaks = top_down_egg_drop_helper(eggs - 1, i - 1, dp)
            doesnt_break = top_down_egg_drop_helper(eggs, floors - i, dp)
            max_drops = min(max_drops, max(breaks, doesnt_break))
        dp[eggs][floors] = max_drops + 1
    return dp[eggs][floors]

def bottom_up_egg_drop(eggs: int, floors: int):
    dp = [[0]*(floors+1)]*(eggs+1)
    for i in range(1, len(dp)):
        for j in range(1, len(dp[0])):
            if j == 1:
                dp[i][j] = 1
            elif i == 1:
                dp[i][j] = j
            else:
                max_drops = float("inf")
                for k in range(1, j+1):
                    breaks = dp[i-1][k-1]
                    doesnt_break = dp[i][j-k]
                    max_drops = min(max_drops, max(breaks, doesnt_break))
                dp[i][j] = max_drops + 1
    return dp[eggs][floors]

class TestEggDrop(unittest.TestCase):
    """Unit test for egg_drop."""

    def test_brute_force(self):
        self.assertEqual(brute_force_egg_drop(1, 1), 1)
        self.assertEqual(brute_force_egg_drop(1, 10), 10)
        self.assertEqual(brute_force_egg_drop(2, 10), 4)
        self.assertEqual(brute_force_egg_drop(2, 20), 6)
        self.assertEqual(brute_force_egg_drop(3, 10), 4)

    def test_top_down(self):
        self.assertEqual(top_down_egg_drop(1, 1), 1)
        self.assertEqual(top_down_egg_drop(1, 10), 10)
        self.assertEqual(top_down_egg_drop(2, 10), 4)
        self.assertEqual(top_down_egg_drop(2, 20), 6)
        self.assertEqual(top_down_egg_drop(3, 10), 4)

    def test_bottom_up(self):
        self.assertEqual(bottom_up_egg_drop(1, 1), 1)
        self.assertEqual(bottom_up_egg_drop(1, 10), 10)
        self.assertEqual(bottom_up_egg_drop(2, 10), 4)
        self.assertEqual(bottom_up_egg_drop(3, 10), 4)

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


import requests
from requests.compat import urljoin
from  ..models import Model_ConfigProduct

def PRODUCT_ID():
    return Model_ConfigProduct.find_one_by_attr({
            "config_product_code": "CI21000"
        }).config_product_id

def DATA_RATE_GROUP(product_id: int):
    return [
    {
        'config_product_id': product_id,
        'config_rate_group_code': 'APU',
        'config_rate_group_label': "Annual Rate per $1000",
        'unit_value': 1000,
        'apply_discretionary_factor': True
    },
    {
        'config_product_id': product_id,
        'config_rate_group_code': 'FLAT',
        'config_rate_group_label': "Flat Rate",
        'unit_value': 1,
        'apply_discretionary_factor': False
    },
]

def load(hostname: str, *args, **kwargs) -> None:
    product_id = PRODUCT_ID()
    url = urljoin(hostname, f'api/config/product/{product_id}/rate-groups')
    res = requests.post(url, json=DATA_RATE_GROUP(product_id))
    if not res.ok:
        raise Exception(res.text)


from controls import Controls
from snake import Snake, Dir
from renderer import Renderer, State
import time

class Game():
    def __init__(self, x_size, y_size, cell_size):
        window_width = cell_size * x_size
        window_height = cell_size * y_size
        self.x_size = x_size
        self.y_size = y_size
        self.board = [
            [
                State.EMPTY
                for y in range(y_size)
            ]
            for x in range(x_size)
        ]
        self.controls = Controls()
        self.snake = Snake(x_size // 2, y_size // 2, 15, Dir.RIGHT)
        self.renderer = Renderer(window_width, window_height)

    def handle_input(self):
        self.controls.handle_input()

    def update(self):
        self.snake.update(self.controls.key_state)
        for x in range(self.x_size):
            for y in range(self.y_size):
                self.board[x][y] = State.EMPTY
        self.board[self.snake.x_head][self.snake.y_head] = State.SNAKE_HEAD
        for x, y in self.snake.get_body():
            self.board[x][y] = State.SNAKE

    def render(self):
        self.renderer.render(self.board)

    def run(self):
        while True:
            self.handle_input()
            self.update()
            self.render()
            self.controls.clear_inputs()
            time.sleep(0.2)


file_name=""
file_name=input("enter file name")
arr=[]
arr=[]
#open('kkk.tsv', "r+") , Here we can put the full path of a tsv file in place of 'kkk.tsv'
with open(file_name,"r+") as file:
    for i in file.readlines():
        #if header is there and header contains # or any other specific symbol we can mention here
        if '#' not in i:
            #split() will convert the line into a list
            i=i.split('\t')
            #Iterating new list
            for j in i:
                #Identifying the last column
                if "\n" in j or j==str(i[len(i)-1]):
                    j=j.replace('\n', "")
                    arr.append(j)

def SelectionSorting(arr,n):

    # One by one move boundary of unsorted subarray
    for i in range(n):
        min_index = i
        min_str = arr[i]

        # Find the minimum element in unsorted subarray
        for j in range(i+1,n):

            # If min_str is greater than arr[j]
            if min_str>arr[j]:

                # Make arr[j] as min_str and update min_index as j
                min_str = arr[j]
                min_index = j

        # Swap the found minimum element with the first element
        if min_index != i:

            # Store the first element in temp
            temp = arr[i]

            # Place the min element at the first position
            arr[i] = arr[min_index]

            # place the element in temp at min_index
            arr[min_index] = temp

    # Return the sorted array
    return arr

result=""
print("\nSorted last Column are")
for i in range(len(SelectionSorting(arr,len(arr)))):
    result+=SelectionSorting(arr,len(arr))[i]
    print("   ",SelectionSorting(arr,len(arr))[i])


import os, sys
import subprocess
import pandas as pd
import multiprocessing as mp
from p_tqdm import p_map
import time

def get_repo_root():
    """Get the root directory of the repo."""
    dir_in_repo = os.path.dirname(os.path.abspath('__file__'))  # os.getcwd()
    return subprocess.check_output('git rev-parse --show-toplevel'.split(),
                                   cwd=dir_in_repo,
                                   universal_newlines=True).rstrip()

ROOT_dir = get_repo_root()
sys.path.append(ROOT_dir)
sys.path.insert(0, ROOT_dir + '/lib')

import lib.routing as routing

if __name__ == "__main__":
    region = 'vg'
    # Parallelize the routing requests
    print('Parallelize the routing requests...')
    df_od = pd.read_csv(ROOT_dir + f'/example/od_pairs_{region}.csv')

    def parallel_process(x):
        folder2save = ROOT_dir + f'/example/output/'
        mode = "BICYCLE,WALK"
        walkdistance = 300
        region = 'vg'
        routing.requesting_origin_batch(data=x, walkdistance=walkdistance,
                                        folder2save=folder2save, region=region, mode=mode)
    p_map(parallel_process, [x for _, x in df_od.groupby("origin")])


import urllib3
import tempfile
import os
import sys
import argparse
import json
import certifi
import ast

api_dict = json.load(open("./api_dict.json", 'r'))

http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED',
        ca_certs=certifi.where())

class Request():
    '''
    This class handles performing a web request for information from a specified government economic database.

    :param type:
    :param method:
    :param parameters: A list of tuples containing parameter and value pairs.
    '''
    def __init__(self, type, method, parameters=None, format='json'):
        #Base parameters
        self.type = type
        self.uri = api_dict[type]['uri']
        self.method = method
        self.handle = api_dict[type]['method'][method]['handle']
        self.key = '&UserID=' + os.environ[api_dict[type]['key']]
        self.format = api_dict[type]['format'][format]

        self.parameters = dict(parameters)
        self.required = api_dict[type]['method'][method]['parameters']['required']
        self.optional = api_dict[type]['method'][method]['parameters']['optional']

        #print('In class type is: {}'.format(self.type))
        #print('In class method is: {}'.format(self.method))
        #print('In class handle is: {}'.format(self.handle))
        #print('In class uri is: {}'.format(self.uri))
        #print('In class format is: {}'.format(self.format))
        #print('In class parameters is: {}'.format(self.parameters))

        #self.url = self.uri + self.key + self.method + self.format
    def execute_method(self):
        print('Running execute_method...')
        if self.method in ["list_datasets", "get_parameter_list", "get_parameter_values", "get_parameter_values_filtered"]:
            print('Running get_metadata method...')
            response = self.get_metadata()
            data = json.loads(response.data)
        if self.method == 'get_data':
            print('Running get_data method...')
            data = self.get_data()
        else: pass

        return data

    def get_metadata(self):
        url = self.uri + self.key + self.format + self.handle
        for param, value in self.parameters.items():
            url += '&{}={}'.format(param, value)

        print('Request URL: {}'.format(url))
        response = http.request('GET', url)
        return response

    def get_data(self):
        url = self.uri + self.key + self.format + self.handle
        for param, value in self.parameters.items():
            url += '&{}={}'.format(param, value)

        response = http.request('GET', url)
        #print(response.data)
        data = json.loads(response.data)
        return data
        #for i in data['BEAAPI']['Results']['Dataset']:
            #print(i['DatasetName'] + '\n' + i['DatasetDescription'] + '\n\n')
        #output = json.loads(data)
        #return output

    def check_parameters(self):
        for req in self.required:
            if req not in self.parameters:
                print('Missing required parameter {}. Please supply this parameter and retry.'.format(req))
            else: continue

    def display_data(self):
        data = self.get_data()
        for i in data['BEAAPI']['Results']['Dataset']:
            print(i['DatasetName'] + '\n' + i['DatasetDescription'] + '\n\n')
        #print(json.dumps(data, indent=4, sort_keys=True))

# Sysarg format: Type, Method, Dataset_Name, Table_Name, Frequency, Year
# Example: DataSet = NIPA, TableName = T10101, Frequency = Q, Year = 2018
# Table Results: BEAAPI > Results > Data/Notes
if __name__ == '__main__':
    # Database/type specified by command line argument.
    type = sys.argv[1]
    print('Type: {}'.format(type))

    # Method is a command line argument.
    method = sys.argv[2]
    print('Method: {}'.format(method))
    print('Example Parameters for Get Data: DataSet = NIPA, TableName = T10101, Frequency = Q, Year = 2018')

    # Parameters defined by prompting for user keyboard input (for now).
    parameters = list()
    dataset = None
    required_parameters = api_dict[type]['method'][method]['parameters']['required']
    optional_parameters = api_dict[type]['method'][method]['parameters']['optional']
    for param in required_parameters:
        # Do not need to input key/UserID, included in configuration. Method is a command line argument.
        if param in ['UserID', 'Method']: continue
        value = input('{} (required):'.format(param))
        # Keep dataset name stored outside for grabbing extra parameters for a get_data method.
        if param == 'DatasetName': dataset = value
        parameters.append( (str(param), value) )
    for param in optional_parameters:
        # Do not need to define result format, already set to json by default.
        if param == 'ResultFormat': continue
        value = input('{} (optional):'.format(param))
        parameters.append( (str(param), value) )

    # If the method is get_data, need to run a second loop to add dataset specific parameters.
    if method == 'get_data':
        required_parameters = api_dict[type]['method'][method]['parameters'][dataset]['required']
        try:
            optional_parameters = api_dict[type]['method'][method]['parameters'][dataset]['optional']
        except:
            optional_parameters = None
        for param in required_parameters:
            value = input('{} (required):'.format(param))
            parameters.append( (param, value) )
        if optional_parameters != None:
            for param in optional_parameters:
                value = input('{} (optional):'.format(param))
                parameters.append( (param, value) )

    print('Complete Parameters list: {}'.format(parameters))

    #print('kwargs: {}'.format(kwargs))
    obj = Request(type, method, parameters)
    data = obj.execute_method()
    print('Data JSON Object: {}'.format(data))
    data_dict = ast.literal_eval(json.dumps(data))
    #print(data_dict['BEAAPI']['Results'])
    print(json.dumps(data, indent=4, sort_keys=True))


class chartdata:
    def chartdataorder(dic):
        import datetime
        month=datetime.datetime.now().month
        year=datetime.datetime.now().year
        monthnames={1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun',7:'Jul',8:'Aug',9:'Sep',10:'Oct',11:'Nov',12:'Dec'}

        ls=[]
        i=1
        j=12
        newyear=False
        for key, value in monthnames.items():
            if key==month:
                ls.append(month)
                for l in range(1,12):
                    if ls[-1] !=12 and newyear==False:
                        newmonth=month-i
                        ls.insert(0,newmonth)
                        i=i+1
                        if(newmonth==1):
                            newyear=True
                    else:
                        ls.insert(0,j)
                        j=j-1

        #{k: sample_dict[k] for k in desired_order_list}
        monthreorderdict={k: monthnames[k] for k in ls}
        #print(monthreorderdict)
        changeyear=False
        for key, value in monthreorderdict.items():
            if value!='Jan' and changeyear==False:
                monthreorderdict[key]=value+' '+str(year-2001)
                if value=='Dec':
                    changeyear=True
            else:
                monthreorderdict[key]=value+' '+str(year-2000)

        #print(monthreorderdict)
        monthreorderdict = {value:0 for key, value in monthreorderdict.items()}
        for key,values in dic.items():
            if key in monthreorderdict.keys():
               # print(key)
                monthreorderdict[key]=values

        return monthreorderdict


from google.cloud import bigquery
from google.oauth2 import service_account

BYTES_PER_GB = 2**30
QUERY_LIMIT = 1*BYTES_PER_GB

def estimate_bytes_processed(query, bq_client):
    """A function to estimate query size without execution of query.
    Modified from: https://www.kaggle.com/sohier/beyond-queries-exploring-the-bigquery-api/
    """
    # Below we initialize the dry_run attribute to true so that our query
    # is not actually executed and does not use any of our precious quota
    estjob_config = bigquery.job.QueryJobConfig(dry_run=True)

    estjob = bq_client.query(query, job_config=estjob_config)

    return estjob.total_bytes_processed

creds_path = "secrets/ml-for-finance-296211-4c54724037a8.json"
credentials = service_account.Credentials.from_service_account_file(
    creds_path, scopes=["https://www.googleapis.com/auth/cloud-platform"])

#Establishes our connection with the bigQuery API
client = bigquery.Client(credentials=credentials, project=credentials.project_id)

# Queries written in SQL
query_smol = """
        SELECT
          timestamp,
          size,
          version,
          bits,
          transaction_count
        FROM `bigquery-public-data.crypto_bitcoin.blocks`
"""

query = """
        SELECT
          block_timestamp,
          is_coinbase,
          input_value,
          output_value,
          input_count,
          output_count,
          fee
        FROM `bigquery-public-data.crypto_bitcoin.transactions`
"""

estimate = estimate_bytes_processed(query_smol, client)
print(f"This query will process {estimate/BYTES_PER_GB:.3f} GB")

safe_config = bigquery.QueryJobConfig(maximum_bytes_billed=QUERY_LIMIT)

query_job = client.query(query_smol, job_config=safe_config)

df = query_job.result().to_dataframe()
print(df)
print(df.memory_usage(deep=True))

# Save DataFrame to csv file
df.to_csv('data/blocks.csv', index=False)


from ninja_mod import Ninja
from pet_mod import Pet, Cat

# hammie = Pet('hammie', 'cat', 'sit', 100, 50)
hammie = Cat('hammie', 'sit', 100, 50)
karissa = Ninja('karissa', 'wong', 'jerky', 'wet food', hammie)

karissa.feed()
karissa.walk()
karissa.bathe()


import os
import csv
import asyncio
import random
import time

def read_csv(csvfilename):
    with open(csvfilename, encoding='utf-8') as csvfile:
        rows = [row for row in csv.reader(csvfile)]
    return rows

class trivia:
    def __init__(self, lst, ctx):
        self.score = {}
        self.lst = lst
        self.answer = None
        self.fullanswer = None
        self.ctx = ctx
        self.asked = []
        self.ongoing = True

    async def next_question(self):
        time.sleep(3)
        data_list = self.lst
        selection = random.choice(data_list)
        selection = list(filter(lambda x: x != '' ,selection))
        if selection not in self.asked:
            self.asked.append(selection)

            await self.ctx.send(selection[0])
            ans = selection[1:]
            shuffled = random.sample(ans, len(ans))
            print(shuffled)
            helper = {0:'a', 1:'b', 2: 'c', 3:'d'}
            self.answer = helper[shuffled.index(selection[1])]
            self.fullanswer = selection[1]
            print(self.answer)
            string = ''
            for i in range(len(shuffled)):
                string += '  ' + helper[i] + '. '+shuffled[i]
            await self.ctx.send(string)
            self.ongoing = True
        else:
            await self.next_question()

    async def correct_answer(self,message):
        await message.channel.send(f"Correct! {message.author.name} got it right")
        self.add_point(message)

    async def wrong_answer(self,message):
        await message.channel.send('Wrong!')
        await message.channel.send(f"The answer is {self.answer}, {self.fullanswer}")

    async def end_quiz(self, message):
        await message.channel.send(f"That is the last question")
        await message.channel.send(f"End of Quiz")
        await self.get_leaderboard(message.channel)

    def add_player(self, message):
        #username = message.author.username
        self.score[message.author.name] = 0

    def add_point(self, message):
        self.score[message.author.name] += 1

    async def get_score(self, ctx):
        if ctx.author.name not in self.score:
            await ctx.send(f"{ctx.author.name} has not earned any point")
        else:
            await ctx.send(f"{ctx.author.name} has {self.score[ctx.author.name]}")

    async def get_leaderboard(self, ctx):
        helper = sorted(self.score.items(), key = lambda x: x[1], reverse = True)
        await ctx.send("=====Leaderboard=====")
        if len(helper) == 0:
            await ctx.send('No player found!')
        else:
            string = ''
            for key,value in helper:
                string += str(key) +': ' + str(value) + '\n'
            await ctx.send(string)


import openai
import json
from PyQt5.QtCore import QThread,pyqtSignal

class chatCompletion(QThread):
    responseChanged = pyqtSignal(str)
    with open("API_key.txt","r") as key:
        API_key = key.read()
    def __init__(self, context, maxtkns,temp, destination):
        super().__init__()
        self.context = context
        self.tokens = maxtkns
        self.temp = temp
        self.destination = destination
        destination.append("\nBetsy: ")
        self.chunk_message = None
        self.answer = ""
    def run(self):
        with open("API_key.txt","r") as key:
            API_key = key.read()
            print(API_key)
        openai.api_key = API_key
        with open(self.context) as f:
            messages = json.load(f)
        completion = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=messages,
            temperature=self.temp,
            max_tokens=self.tokens,
            stream = True
            )
        for chunk in completion:
            try:
                self.chunk_message = chunk['choices'][0]['delta']['content']
                # move the cursor to the end of the text
                cursor = self.destination.textCursor()
                cursor.movePosition(cursor.End)

                # insert the new word
                cursor.insertText(self.chunk_message)
                self.answer = self.answer + str(self.chunk_message)
            except:
                self.answer = " \n \n ERREUR LORS DE LA COMPLETION ESSAYEZ RENVOYER LE MESSAGE SI LE PROBLÉME PERSISTE VERIFIEZ 'ETAT DE LA CONNECTION ET REDEMARREZ L'APPLICATION "

        self.responseChanged.emit(self.chunk_message)
    @property
    def response(self):
        return self.chunk_message


from rest_framework.authtoken.models import Token
from django.shortcuts import get_object_or_404
from django.http import JsonResponse
from rest_framework.views import APIView
from user.models import User
from rest_framework import generics
from rest_framework.authentication import TokenAuthentication
from user.serializers import UserSerializer
from rest_framework.permissions import (
    AllowAny,
    IsAuthenticated)

class ValidateToken(APIView):
    permission_classes = [AllowAny]

    def get(self, request):
        return JsonResponse({'status': False,
                             'message': "Método get não permitido"})

    def post(self, request):
        data = request.data
        token = data['token']
        obj = get_object_or_404(Token, key=token)
        if obj is not None:
            return JsonResponse({'status': True})

        message = "Token not found !"
        return JsonResponse({'status': False, 'message': message})

class AddNewUser(generics.CreateAPIView):
    permission_classes = [AllowAny]
    queryset = User.objects.all()
    serializer_class = UserSerializer

class DeleteUser(generics.DestroyAPIView):
    authenticantion_classes = [TokenAuthentication]
    permission_classes = [IsAuthenticated]
    serializer_class = UserSerializer
    lookup_field = "username"

    def get_queryset(self):
        username = self.kwargs.get('username', None)
        queryset = User.objects.filter(username=username)
        return queryset

    def perform_destroy(self, instance):
        serializer = UserSerializer(data=self.get_queryset())
        serializer.is_valid()
        return instance.delete()


#!/usr/bin/python3

import random

def merge_sort(lista):
	if len(lista) > 1:
		mid = len(lista) // 2
		left = lista[:mid]
		right = lista[mid:]
		print(left, '*'*10, right)

		# Llamada recursiva en cada mitad
		merge_sort(left)
		merge_sort(right)

		# Iteradores para recorrer las dos sublistas
		i = 0
		j = 0

		# Iterador para lista principal
		k = 0

		while i < len(left) and j < len(right):
			if left[i] < right[j]:
				lista[k] = left[i]
				i += 1
			else:
				lista[k] = right[j]
				j += 1

			k += 1

		while i < len(left):
			lista[k] = left[i]
			i += 1
			k += 1

		while j < len(right):
			lista[k] = right[j]
			j += 1
			k += 1

		print(f'izquierda {left}, derecha {right}')
		print(lista)
		print('*'*20)

	return lista

if __name__ == '__main__':
	tamanio_lista = int(input('Escriba el tamanio de la lista: '))

	lista = [random.randint(0, 100) for i in range(tamanio_lista)]
	print(lista)

	sorted_list = merge_sort(lista)
	print(sorted_list)


from random import randrange
import googletrans
import discord
import sqlite3

data = sqlite3.connect('database.db')
cursor = data.cursor()

img, style = "", ""

client = discord.Client()
token = "put your bot token here" # <-- TOKEN
embed = discord.Embed

Help_Message = ("*kiss {pessoa} => todas as preferencias.\n"
                "*hug {pessoa} => todas as preferencias.\n"
                "*slap {pessoa} => todas as preferencias.\n"
                "-kiss {pessoa} => preferencia lesbica.\n"
                "-hug {pessoa} => preferencia lesbica.\n"
                "+kiss {pessoa} => preferencia homosexual.")

commands = ["*kiss", "*hug", "*slap", "-kiss", "-hug", "+kiss"]
MaxLen = [61, 51, 29, 38, 18, 38]
titles = ["amorzinho", "amorzinho", "briga briga", "Amorzinho", "Amorzinho", "Amorzinho"]
tables = ["kiss", "hugs", "slap", "kiss_yuri", "hugs_yuri", "kiss_yaoi"]
actions = ["beijou", "abraçou", "bateu em", "beijou", "abraçou", "beijou"]

def function(_title, table, max_table_len, author, person, option):
    global img, style
    element_position = randrange(0, max_table_len)
    element = cursor.execute(f"SELECT link FROM {table} WHERE id={element_position}")
    for i in element:
        img = i
        break
    img = f"{img[0]}"
    style = embed(title=f"{_title}", description=f"{author} {option} {person}")
    style.set_image(url=img)
    return style

def translator(message):
    global embed
    translator = googletrans.Translator()
    translation = translator.translate(message, dest="pt")
    translation = translation.text
    embed = discord.Embed(title=f"{message} = {translation}")

@client.event
async def on_ready():
    print("started!")
    return

@client.event
async def on_message(message):
    global embed
    message.content = message.content.lower()
    if message.content.startswith("--help"):
        embed = discord.Embed(title="HELP MESSAGE", description=Help_Message)
        await message.channel.send(embed=embed)

    if message.content.startswith("+translate"):
        msg = message.content[10:]
        translator(msg)
        await message.channel.send(embed=embed)

    for i in range(len(commands)):
        if message.content.startswith(commands[i]):
            function(titles[i], tables[i], MaxLen[i], message.author.mention, message.content[len(commands[i]):28], actions[i])
            await message.channel.send(embed=style)
            break

client.run(token)


import torch
import os
import logging
import shutil
from tqdm import tqdm
logger = logging.getLogger('Attention')

def save_checkpoint(state, is_best, epoch, checkpoint, ins_name=-1):
    '''Saves model and training parameters at checkpoint + 'last.pth.tar'. If is_best==True, also saves
    checkpoint + 'best.pth.tar'
    Args:
        state: (dict) contains model's state_dict, may contain other keys such as epoch, optimizer state_dict
        is_best: (bool) True if it is the best model seen till now
        checkpoint: (string) folder where parameters are to be saved
        ins_name: (int) instance index
    '''
    if ins_name == -1:
        filepath = os.path.join(checkpoint, f'epoch_{epoch}.pth.tar')
    else:
        filepath = os.path.join(checkpoint, f'epoch_{epoch}_ins_{ins_name}.pth.tar')
    if not os.path.exists(checkpoint):
        logger.info(f'Checkpoint Directory does not exist! Making directory {checkpoint}')
        os.mkdir(checkpoint)
    torch.save(state, filepath)
    logger.info(f'Checkpoint saved to {filepath}')
    if is_best:
        shutil.copyfile(filepath, os.path.join(checkpoint, 'best.pth.tar'))
        logger.info('Best checkpoint copied to best.pth.tar')

def load_checkpoint(checkpoint, model, optimizer=None):
    '''Loads model parameters (state_dict) from file_path. If optimizer is provided, loads state_dict of
    optimizer assuming it is present in checkpoint.
    Args:
        checkpoint: (string) filename which needs to be loaded
        model: (torch.nn.Module) model for which the parameters are loaded
        optimizer: (torch.optim) optional: resume optimizer from checkpoint
        gpu: which gpu to use
    '''
    if not os.path.exists(checkpoint):
        raise FileNotFoundError(f"File doesn't exist {checkpoint}")
    if torch.cuda.is_available():
        checkpoint = torch.load(checkpoint, map_location='cuda')
    else:
        checkpoint = torch.load(checkpoint, map_location='cpu')
    model.load_state_dict(checkpoint['state_dict'])

    if optimizer:
        optimizer.load_state_dict(checkpoint['optim_dict'])

    return checkpoint

def set_logger(log_path):
    '''Set the logger to log info in terminal and file `log_path`.
    In general, it is useful to have a logger so that every output to the terminal is saved
    in a permanent file. Here we save it to `model_dir/train.log`.
    Example:
    logging.info('Starting training...')
    Args:
        log_path: (string) where to log
    '''
    _logger = logging.getLogger('Attention')
    _logger.setLevel(logging.INFO)

    fmt = logging.Formatter('[%(asctime)s] %(name)s: %(message)s', '%H:%M:%S')

    class TqdmHandler(logging.StreamHandler):
        def __init__(self, formatter):
            logging.StreamHandler.__init__(self)
            self.setFormatter(formatter)

        def emit(self, record):
            msg = self.format(record)
            tqdm.write(msg)

    file_handler = logging.FileHandler(log_path)
    file_handler.setFormatter(fmt)
    _logger.addHandler(file_handler)
    _logger.addHandler(TqdmHandler(fmt))


import fastf1 as ff1
import pandas as pd
import numpy as np
import math
from fastf1 import plotting
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
from matplotlib.collections import LineCollection

# Enable the cache
ff1.Cache.enable_cache('cache')

# Enable plotting settings
ff1.plotting.setup_mpl()

# Choosing an f1 session
count = 0

# -------------- YEAR SESSION --------------
while count == 0:
    year = int(input("Choose a year between 2015 and 2022 for the session: "))
    if 2015 <= year <= 2022:
        grand_prix_list = ff1.get_event_schedule(year)
        print(grand_prix_list['Location'])
        count = 1
    else:
        print("\nYear not valid. Try again.")

# -------------- GRAND PRIX --------------
while count == 1:
    grand_prix = input("\nChoose a Grand Prix from the list:")
    for i in grand_prix_list['Location']:
        if i == grand_prix:
            count = 2

    if count == 1:
        print("\nGrand Prix not valid. Try Again")

# -------------- SESSION TYPE --------------
while count == 2:
    session_type = input("\nChoose session type: (FP1, FP2, FP3, Q, R): ")
    if session_type == "FP1" or session_type == "FP2" or session_type == "FP3" or session_type == "Q" or session_type == "R":
        session = ff1.get_session(year, grand_prix, session_type)
        count = 3
    else:
        print("\nSession not valid. Try again")

if count == 3:
    count = 4
    session.load()  # Load the session

# -------------- DRIVER 1 --------------
print("\nChoose one of the following driver.")
print(session.results['Abbreviation'])
while count == 4:
    driver_1 = input("\nWrite the driver 1:")
    for i in session.results['Abbreviation']:
        if i == driver_1:
            laps_driver_1 = session.laps.pick_driver(driver_1)
            fastest_driver_1 = laps_driver_1.pick_fastest()
            telemetry_driver_1 = fastest_driver_1.get_telemetry()

            count = 5

    if count == 4:
        print("\nDriver's abbreviation incorrect. Try again.")

# -------------- DRIVER 2 --------------
print("\nChoose one of the following driver.")
print(session.results['Abbreviation'])
while count == 5:
    driver_2 = input("\nWrite the driver 2:")
    for i in session.results['Abbreviation']:
        if i == driver_2:
            laps_driver_2 = session.laps.pick_driver(driver_2)
            fastest_driver_2 = laps_driver_2.pick_fastest()
            telemetry_driver_2 = fastest_driver_2.get_telemetry()

            count = -1

    if count == 5:
        print("\nDriver's abbreviation incorrect. Try again.")

# Variables for the plots
team_driver_1 = fastest_driver_1['Team']
team_driver_2 = fastest_driver_2['Team']

driver_speed_1 = telemetry_driver_1['Speed']
driver_speed_2 = telemetry_driver_2['Speed']

driver_throttle_1 = telemetry_driver_1['Throttle']
driver_throttle_2 = telemetry_driver_2['Throttle']

driver_brake_1 = telemetry_driver_1['Brake']
driver_brake_2 = telemetry_driver_2['Brake']

driver_gear_1 = telemetry_driver_1['nGear']
driver_gear_2 = telemetry_driver_2['nGear']

driver_distance_1 = telemetry_driver_1['Distance']
driver_distance_2 = telemetry_driver_2['Distance']

# Plot configuration
plot_size = [15,15]
plot_title = f"{session.event.year} {session.event.EventName} - {session.name} - {driver_1}"
plot_ratios = [1, 3, 2, 1, 1, 2, 1]
plot_filename = plot_title.replace(" ", " ") + ".png"

plt.rcParams['figure.figsize'] = [20, 15]
fig, ax = plt.subplots(7, gridspec_kw={'height_ratios': [5, 5, 5, 5, 5, 7, 7]})

# Subplot 1: Speed
ax[0].plot(telemetry_driver_1['Distance'], telemetry_driver_1['Speed'], label=driver_1, color = ff1.plotting.team_color(team_driver_1))
ax[0].plot(telemetry_driver_2['Distance'], telemetry_driver_2['Speed'], label=driver_2, color = ff1.plotting.team_color(team_driver_2))
ax[0].set(ylabel='Speed')
ax[0].legend(loc="lower right")

# Subplot 2: Throttle
ax[1].plot(telemetry_driver_1['Distance'], telemetry_driver_1['Throttle'], label=driver_1, color=ff1.plotting.team_color(team_driver_1))
ax[1].plot(telemetry_driver_2['Distance'], telemetry_driver_2['Throttle'], label=driver_2, color=ff1.plotting.team_color(team_driver_2))
ax[1].set(ylabel='Throttle')

# Subplot 3: Brake
ax[2].plot(telemetry_driver_1['Distance'], telemetry_driver_1['Brake'], label=driver_1, color=ff1.plotting.team_color(team_driver_1))
ax[2].plot(telemetry_driver_2['Distance'], telemetry_driver_2['Brake'], label=driver_2, color=ff1.plotting.team_color(team_driver_2))
ax[2].set(ylabel='Brake')

# Subplot 4: Gear
ax[3].plot(telemetry_driver_1['Distance'], telemetry_driver_1['nGear'], label=driver_1, color=ff1.plotting.team_color(team_driver_1))
ax[3].plot(telemetry_driver_2['Distance'], telemetry_driver_2['nGear'], label=driver_2, color=ff1.plotting.team_color(team_driver_2))
ax[3].set(ylabel='Gear')

# Subplot 5: RPM
ax[4].plot(telemetry_driver_1['Distance'], telemetry_driver_1['RPM'], label=driver_1, color=ff1.plotting.team_color(team_driver_1))
ax[4].plot(telemetry_driver_2['Distance'], telemetry_driver_2['RPM'], label=driver_2, color=ff1.plotting.team_color(team_driver_2))
ax[4].set(ylabel='RPM')

def longAcceleration():
    driver_time_1 = (telemetry_driver_1['Time'] / np.timedelta64(1, 's')).astype(float)
    driver_time_2 = (telemetry_driver_2['Time'] / np.timedelta64(1, 's')).astype(float)

    driver_speed_1 = telemetry_driver_1['Speed']
    driver_speed_2 = telemetry_driver_2['Speed']

    raw_acceleration_data_1 = []
    raw_acceleration_data_2 = []

    for i in driver_time_1.index:
        if i > 0 and i < driver_time_1.size:
            s_p = driver_speed_1.iloc[i-1]            # Previous speed
            s_f = driver_speed_1.iloc[i]              # Current speed
            t_p = driver_time_1.iloc[i-1]             # Previous time
            t_f = driver_time_1.iloc[i]               # Current time

            with np.errstate(divide='ignore', invalid = 'ignore'):
                acc = ((s_f - s_p) / 3.6) / (t_f - t_p)
                g_force = acc / 9.81
                raw_acceleration_data_1.append(g_force)

            acceleration_data_1 = [v for v in raw_acceleration_data_1 if not (math.isinf(v) or math.isnan(v) or v < -6.0 or v > 6)]

    for i in driver_time_2.index:
            if i > 0 and i < driver_time_2.size:
                s_p = driver_speed_2.iloc[i-1]          # Previous speed
                s_f = driver_speed_2.iloc[i]            # Current speed
                t_p = driver_time_2.iloc[i-1]           # Previous time
                t_f = driver_time_2.iloc[i]             # Current time

                with np.errstate(divide='ignore', invalid = 'ignore'):
                    acc = ((s_f - s_p) / 3.6) / (t_f - t_p)
                    g_force = acc / 9.81
                    raw_acceleration_data_2.append(g_force)

                acceleration_data_2 = [v for v in raw_acceleration_data_2 if not (math.isinf(v) or math.isnan(v) or v < -6.0 or v > 6)]

    # Subplot 5: Longitudinal Acceleration
    ax[5].plot(acceleration_data_1, label = driver_1, color = ff1.plotting.team_color(team_driver_1))
    ax[5].plot(acceleration_data_2, label = driver_2, color = ff1.plotting.team_color(team_driver_2))
    ax[5].set_ylabel("Long Acc")

def latAcceleration():
    if grand_prix == "Imola":
        if driver_1 == "VER" or driver_2 == "VER":
            driver = "VER"
            laps_driver = session.laps.pick_driver(driver)
            fastest_driver = laps_driver.pick_fastest()
            telemetry_driver = fastest_driver.get_telemetry()
            team_driver = fastest_driver['Team']

            driver_time = (telemetry_driver['Time'] / np.timedelta64(1, 's')).astype(float)
            driver_speed = telemetry_driver['Speed']

            excel_data = pd.read_excel('Dataset/tempi_curve.xlsx')                  # Load xlsx file
            df = pd.DataFrame(excel_data)                                           # Read the values of the file in the dataframe

            raw_acceleration_data = []

            for i in df.index:
                for j in driver_time.index:
                    if df['Start time'][i] <= driver_time[j] <= df['End time'][i]:
                        radius = df['Radius'][i]
                        speed = driver_speed[j]

                        with np.errstate(divide='ignore', invalid = 'ignore'):
                                speed_ms = speed / 3.6                               # Speed in meters/seconds
                                acceleration = (math.pow(speed_ms, 2)) / radius
                                g_force = acceleration / 9.81
                                raw_acceleration_data.append(g_force)

                        acceleration_data = [v for v in raw_acceleration_data if not (math.isinf(v) or math.isnan(v) or v > 6.0 or v == 0)]

            # Subplot 6: Lateral Acceleration
            ax[6].plot(acceleration_data, label = driver, color = ff1.plotting.team_color(team_driver))
            ax[6].set(ylabel='Lat Acc')

longAcceleration()
latAcceleration()

# using padding
fig.tight_layout(pad=2.5)
plt.show()


# -*- coding: utf-8 -*-
"""
Created on Tue Apr 13 18:41:38 2021

@author: divyoj
"""# -*- coding: utf-8 -*-
"""
Created on Tue Apr 13 18:41:38 2021

@author: divyoj
"""
## importing libraries:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation
import os
# # note that this must be executed before 'import numba'
# os.environ['NUMBA_DISABLE_INTEL_SVML'] = '1'
from numba import njit
import time as process_time
import plotting
from scipy.integrate import solve_ivp
@njit
def do_timestep(t,z,aT,bT,alpha, beta, gamma, zeta):
    ''' function to give dxdt at a time step '''

    aL = z[0*(nx*ny):1*(nx*ny)].reshape((ny,nx))
    bL = z[1*(nx*ny):2*(nx*ny)].reshape((ny,nx))
    aR = z[2*(nx*ny):3*(nx*ny)].reshape((ny,nx))
    bR = z[3*(nx*ny):4*(nx*ny)].reshape((ny,nx))

    # total membrane protein concentration:
    a0 = aL + aR
    b0 = bL + bR
    # intitating dxdt to arrays of zeros:
    daL=np.zeros((ny,nx));daR=np.zeros((ny,nx));dbL=np.zeros((ny,nx));dbR=np.zeros((ny,nx));
    ## Equations for al,aR,bl and bR:
    # aL
    daL[0,1:nx-1]=alpha*(aT[0,1:nx-1]-a0[0,1:nx-1])-beta*aL[0,1:nx-1]+beta*gamma*( aL[0,1:nx-1]*bR[0,1-1:nx-1-1] ) -zeta*(aL[0,1:nx-1]-aR[0,1:nx-1])**3;
    # aR
    daR[0,1:nx-1]=alpha*(aT[0,1:nx-1]-a0[0,1:nx-1])-beta*aR[0,1:nx-1]+beta*gamma*( aR[0,1:nx-1]*bL[0,1+1:nx-1+1] ) -zeta*(aR[0,1:nx-1]-aL[0,1:nx-1])**3;
    # bL
    dbL[0,1:nx-1]=alpha*(bT[0,1:nx-1]-b0[0,1:nx-1])-beta*bL[0,1:nx-1]+beta*gamma*( bL[0,1:nx-1]*aR[0,1-1:nx-1-1] ) -zeta*(bL[0,1:nx-1]-bR[0,1:nx-1])**3;
    # bR
    dbR[0,1:nx-1]=alpha*(bT[0,1:nx-1]-b0[0,1:nx-1])-beta*bR[0,1:nx-1]+beta*gamma*( bR[0,1:nx-1]*aL[0,1+1:nx-1+1] ) -zeta*(bR[0,1:nx-1]-bL[0,1:nx-1])**3;

    # Boundary Conditions:
    #aL
    daL[0,0]=daL[0,1];
    daL[0,nx-1]=alpha*(aT[0,nx-1]-a0[0,nx-1])-beta*aL[0,nx-1]+beta*gamma*(aL[0,nx-1]*bR[0,nx-1-1])-zeta*(aL[0,nx-1]-aR[0,nx-1])**3;

    #aR
    daR[0,0]=alpha*(aT[0,0]-a0[0,0])-beta*aR[0,0]+beta*gamma*( aR[0,0]*bL[0,1] ) -zeta*(aR[0,0]-aL[0,0])**3;
    daR[0,nx-1]=daR[0,nx-2];

    #bL
    dbL[0,0]=dbL[0,1];
    dbL[0,nx-1]=alpha*(bT[0,nx-1]-b0[0,nx-1])-beta*bL[0,nx-1]+beta*gamma*(bL[0,nx-1]*aR[0,nx-1-1])-zeta*(bL[0,nx-1]-bR[0,nx-1])**3;

    #bR
    dbR[0,0]=alpha*(bT[0,0]-b0[0,0])-beta*bR[0,0]+beta*gamma*( bR[0,0]*aL[0,1] ) -zeta*(bR[0,0]-bL[0,0])**3;
    dbR[0,nx-1]=dbR[0,nx-2];

    # checking if membrane concentration went above total concetration:
    for i in range(ny):
        for j in range(nx):
            if a0[i,j]>aT[i,j]:

                if daL[i,j]>0:
                    daL[i,j]=0
                if daR[i,j]>0:
                    daR[i,j]=0

            if b0[i,j]>bT[i,j]:
                if dbL[i,j]>0:
                    dbL[i,j]=0
                if dbR[i,j]>0:
                    dbR[i,j]=0

            if aL[i,j]<=0:
                daL[i,j]=0
            if bL[i,j]<=0:
                dbL[i,j]=0

            if aR[i,j]<=0:
                daR[i,j]=0
            if bR[i,j]<=0:
                dbR[i,j]=0

    return np.concatenate((daL.flatten(),dbL.flatten(),daR.flatten(),dbR.flatten()))

#@njit
def simulate(rho,alpha, beta, gamma, zeta):
    ''' function to iterate over time and return arrays with the result '''
    ## initilizing the arrays to store the values over time:
    aL_t = np.zeros((T_max+1,ny,nx)); aR_t = np.zeros((T_max+1,ny,nx));
    bL_t = np.zeros((T_max+1,ny,nx)); bR_t = np.zeros((T_max+1,ny,nx));

    # total proteins in the cells
    aT = rho + np.zeros((ny,nx))
    bT = rho + np.zeros((ny,nx))

    ## initializing aL,bR,bL,aR
    aL = np.zeros((ny,nx)) + 0.6*rho; aR = np.zeros((ny,nx)) + 0.1*rho
    bL = np.zeros((ny,nx)) + 0.1*rho; bR = np.zeros((ny,nx)) + 0.6*rho

    ## Deletion in one range:
    aL[0,:int(nx/2)]=aR[0,:int(nx/2)]=aT[0,:int(nx/2)]=0

    ## Collecting the initial conditions into a single array:
    ic = np.array(aL.flatten().tolist()+bL.flatten().tolist()+aR.flatten().tolist()+bR.flatten().tolist())

    ## Solving the initial value problem:
    sol = solve_ivp(lambda t,y: do_timestep(t,y,aT,bT,alpha, beta, gamma, zeta),t_span=[0,T_max],y0=ic,t_eval=list(np.linspace(0,T_max,T_max+1)))

    t = sol.t
    aball = sol.y

    for t_index, ts in enumerate(t):

        aball_at_ts = aball[:,t_index]

        aL_t[t_index]= aball_at_ts[0*(nx*ny):1*(nx*ny)].reshape((ny,nx));
        bL_t[t_index]= aball_at_ts[1*(nx*ny):2*(nx*ny)].reshape((ny,nx));
        aR_t[t_index]= aball_at_ts[2*(nx*ny):3*(nx*ny)].reshape((ny,nx));
        bR_t[t_index]= aball_at_ts[3*(nx*ny):4*(nx*ny)].reshape((ny,nx));

    #return (aL_t[:,:,10:nx-10],aR_t[:,:,10:nx-10],bL_t[:,:,10:nx-10],bR_t[:,:,10:nx-10])
    return (aL_t,bL_t,aR_t,bR_t)

if __name__ == "__main__":
    # Lattice:
    w,h = 10,2;
    dx,dy=0.01,0.1;
    nx=int(w/dx)
    ny=1;#int(h/dx);
    # time:
    T_max=100;

    # parameters:
    alpha=10;
    gamma=1; beta=1;
    zeta=0.0;

#%% Characterisation over rho:
    # folder for storing the data:
    folder="./"+"zeta="+str(zeta)+"_alpha="+str(alpha)+"/"
    if not os.path.exists(folder):
        os.makedirs(folder)

    rhos = np.around(np.linspace(0.8,1.2,21),2);rho0_array=rhos.copy();
    p_array=rhos.copy();delta_p_array=rhos.copy()

    for ri, rho in enumerate(rhos):
        print (ri, rho)

        aL_t, bL_t, aR_t, bR_t = simulate(rho,alpha, beta, gamma, zeta)

         # Plotting at each rho
        #%% Plotting at each rho:
        rho0_array[ri],p_array[ri],delta_p_array[ri]=plotting.plots_at_rho(aL_t,bL_t,aR_t,bR_t,rho,folder)

    # Plotting final graphs after iterating over rhos
    plotting.plots_over_rho(rhos,rho0_array,p_array,delta_p_array,folder)


from operator import itemgetter
def solve():
    #n = int(input())
    n, m, x = map(int, input().split())
    a = list(map(int, input().split()))
    #a = list(map(str, input()))

    towers = [0] * m
    pairs = []
    for i in range(n):
        pairs.append([a[i], i])
    pairs.sort(key=itemgetter(0), reverse=True)

    i = 0
    while i < n:
        if towers[-1] >= towers[0]:
            for k in range(0, m):
                towers[k] += pairs[i][0]
                pairs[i][0] = k + 1
                i += 1
                if i == n:
                    break
        else:
            for k in range(m - 1, -1, -1):
                towers[k] += pairs[i][0]
                pairs[i][0] = k + 1
                i += 1
                if i == n:
                    break
    if abs(towers[0] - towers[-1]) > x:
        print('NO')
        return
    pairs.sort(key=itemgetter(1))
    print('YES')
    for i in range(n):
        print(pairs[i][0], end = ' ')
    print()

for _ in range(int(input())):
    solve()


import unittest
from core import configuration
import time
import logging

class TestFB(unittest.TestCase):

    def setUp(self):
        # Configure the logging output
        logging.basicConfig(level=logging.INFO,
                            format='[%(asctime)s][%(levelname)s][%(threadName)s] %(message)s')

        self.conf = configuration.Configuration('config_1', 'EMB_RES')

    def test_fb_creation(self):
        self.conf.create_fb('E_EXAMPLE_1', 'TEST_FB')

        self.assertEqual(2, len(self.conf.fb_dictionary))

        fb = self.conf.fb_dictionary['E_EXAMPLE_1']

        self.assertEqual(1, len(fb.input_events))
        self.assertEqual(2, len(fb.output_events))
        self.assertEqual(1, len(fb.input_vars))
        self.assertEqual(0, len(fb.output_vars))
        self.assertEqual(1, len(fb.input_events))

    def test_fb_connection(self):
        self.conf.create_fb('E_EXAMPLE_1', 'TEST_FB')
        self.conf.create_fb('E_EXAMPLE_2', 'TEST_FB')

        self.assertEqual(3, len(self.conf.fb_dictionary))

        self.conf.create_connection('E_EXAMPLE_1.EO0', 'E_EXAMPLE_2.EI')

        fb_1 = self.conf.get_fb('E_EXAMPLE_1')
        self.assertEqual(1, len(fb_1.output_connections))

        fb_2 = self.conf.get_fb('E_EXAMPLE_2')
        self.assertEqual(0, len(fb_2.output_connections))

        connection = fb_1.output_connections['EO0'][0]
        self.assertEqual('EI', connection.value_name)
        self.assertEqual(fb_2, connection.destination_fb)

    def test_fb_running(self):
        self.conf.create_fb('E_EXAMPLE_1', 'TEST_FB')
        self.conf.start_work()

        fb = self.conf.get_fb('E_EXAMPLE_1')
        fb.push_event('EI', 1)

        time.sleep(0.01)

        # Validate the inputs
        event_type, value, is_watch = fb.input_events['EI']
        self.assertEqual(1, value)
        self.assertEqual('Event', event_type)
        var_type, value, is_watch = fb.input_vars['G']
        self.assertEqual(None, value)
        self.assertEqual('BOOL', var_type)

        # Validate the outputs
        event_type, value, is_watch = fb.output_events['EO0']
        self.assertEqual(2, value)
        self.assertEqual('Event', event_type)
        event_type, value, is_watch = fb.output_events['EO1']
        self.assertEqual(None, value)
        self.assertEqual('Event', event_type)

        self.conf.stop_work()

    def test_fb_flow(self):
        self.conf.create_fb('E_EXAMPLE_1', 'TEST_FB')
        self.conf.create_fb('E_EXAMPLE_2', 'TEST_FB')
        self.conf.create_connection('E_EXAMPLE_1.EO0', 'E_EXAMPLE_2.EI')
        self.conf.start_work()

        fb = self.conf.get_fb('E_EXAMPLE_1')
        fb.push_event('EI', 1)

        time.sleep(0.01)

        fb_2 = self.conf.get_fb('E_EXAMPLE_2')

        # Validate the inputs
        event_type, value, is_watch = fb_2.input_events['EI']
        self.assertEqual(2, value)
        self.assertEqual('Event', event_type)
        var_type, value, is_watch = fb_2.input_vars['G']
        self.assertEqual(None, value)
        self.assertEqual('BOOL', var_type)

        # Validate the outputs
        event_type, value, is_watch = fb_2.output_events['EO0']
        self.assertEqual(3, value)
        self.assertEqual('Event', event_type)
        event_type, value, is_watch = fb_2.output_events['EO1']
        self.assertEqual(None, value)
        self.assertEqual('Event', event_type)

        self.conf.stop_work()