import unittest
from unittest.mock import MagicMock, patch

from main import len_joke, get_joke

class TestJoke(unittest.TestCase):

    # The decorator "patch" creates a special fake object, a MagicMock object, and passes
    # The reference to it into the decorated function (test_len_joke in this case)
    # Using the patch eliminates the dependency of the len_joke function on the get_joke function
    # Helps the test be independent from the return value of the get_joke function
    # Also eliminates dependency on internet connection or remote API server
    @patch("main.get_joke")
    def test_len_joke(self, mock_get_joke):
        mock_get_joke.return_value = "one"
        self.assertEqual(len_joke(), 3)

    @patch("main.requests")
    def test_get_joke(self, mock_requests):
        mock_response = MagicMock()
        mock_response.status_code = 200
        mock_response.json.return_value = {"value": {"joke": "hello world"}}
        mock_requests.get.return_value = mock_response
        self.assertEqual(get_joke(), "hello world")

    @patch("main.requests")
    def test_fail_get_joke(self, mock_requests):
        mock_response = MagicMock(status_code=403)
        mock_response.json.return_value = {"value": {"joke": "hello world"}}
        mock_requests.get.return_value = mock_response
        self.assertEqual(get_joke(), "No jokes")

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


import pandas as pd
import networkx as nx
import numpy as np
from src.base import BaseSplitter
import json

class LouvainSplitter(BaseSplitter):
    def __init__(self, **kwargs):
        super(LouvainSplitter, self).__init__(**kwargs)

    @property
    def author(self):
        """The authors name of the splitting implementation"""
        return "Ilya"

    @staticmethod
    def get_all():
        return [
            LouvainSplitter(),
        ]

    @property
    def name(self):
        return "Louvain"

    def split_two(self, df, train_frac):
        super(LouvainSplitter, self).split_two(df, train_frac)
        df = self._get_communities(df)
        df = self._split_groups(df, "community", 10, train_frac, 0)
        return df

    def split_three(self, df, train_frac, val_frac):
        super(LouvainSplitter, self).split_three(df, train_frac, val_frac)
        df = self._get_communities(df)
        df = self._split_groups(df, "community", 10, train_frac, val_frac)
        return df

    @staticmethod
    def _split_groups(
        inter: pd.DataFrame,
        col_name: str = "Target_ID",
        bin_size: int = 10,
        train_frac: float = 0.7,
        val_frac: float = 0.2,
    ) -> pd.DataFrame:
        """Split data by protein (cold-target)
        Tries to ensure good size of all sets by sorting the prots by number of interactions
        and performing splits within bins of 10
        Args:
            inter (pd.DataFrame): interaction DataFrame
            col_name (str): Which column to split on (col_name or 'Drug_ID' usually)
            bin_size (int, optional): Size of the bins to perform individual splits in. Defaults to 10.
            train_frac (float, optional): value from 0 to 1, how much of the data goes into train
            val_frac (float, optional): value from 0 to 1, how much of the data goes into validation
        Returns:
            pd.DataFrame: DataFrame with a new 'split' column
        """
        sorted_index = [x for x in inter[col_name].value_counts().index]
        train_prop = int(bin_size * train_frac)
        val_prop = int(bin_size * val_frac)
        train = []
        val = []
        test = []
        for i in range(0, len(sorted_index), bin_size):
            subset = sorted_index[i : i + bin_size]
            train_bin = list(
                np.random.choice(subset, min(len(subset), train_prop), replace=False)
            )
            train += train_bin
            subset = [x for x in subset if x not in train_bin]
            val_bin = list(
                np.random.choice(subset, min(len(subset), val_prop), replace=False)
            )
            val += val_bin
            subset = [x for x in subset if x not in val_bin]
            test += subset
        train_idx = inter[inter[col_name].isin(train)].index
        val_idx = inter[inter[col_name].isin(val)].index
        test_idx = inter[inter[col_name].isin(test)].index
        inter.loc[train_idx, "split"] = "train"
        inter.loc[val_idx, "split"] = "val"
        inter.loc[test_idx, "split"] = "test"
        inter = inter[inter['split'].notna()]
        return inter

    def _get_communities(self, df: pd.DataFrame) -> pd.DataFrame:
        """Assigns each interaction to a community, based on Louvain algorithm."""
        G = nx.from_pandas_edgelist(df, source="Target_ID", target="Drug_ID")
        all_drugs = set(df["Drug_ID"].unique())
        all_prots = set(df["Target_ID"].unique())
        s = list(nx.algorithms.community.louvain_communities(G, resolution=1.5))
        communities = []
        for i in s:
            drugs = i.intersection(all_drugs)
            prots = i.intersection(all_prots)
            subset = df[df["Target_ID"].isin(prots) & df["Drug_ID"].isin(drugs)]
            communities.append(
                {
                    "protn": len(prots),
                    "drugn": len(drugs),
                    "edgen": len(subset),
                    "protids": prots,
                    "drugids": drugs,
                }
            )
        communities = (
            pd.DataFrame(communities).sort_values("edgen").reset_index(drop=True)
        )
        self.params = {"communities": len(communities)}
        for name, row in communities.iterrows():
            idx = df["Target_ID"].isin(row["protids"]) & df["Drug_ID"].isin(
                row["drugids"]
            )
            df.loc[idx, "community"] = "com" + str(int(name))
        return df


import torch
import torch.nn as nn
import numpy as np
import os
from collections import defaultdict
from torch.distributions.normal import Normal
from common.common_utils import get_device, np_to_tensor, tensor_to_np, linear_lr_decay, set_optimizer_lr
from common.log_utils import Logger
from common.env_utils import evaluate_policy
from common.data_memory import RolloutBuffer
from net.network import BaseActor, BaseCritic
from common.env_utils import make_multiprocessing_env, get_space_dim, make_singleprocessing_env

class PPO(object):
    def __init__(self,
        env_name,
        actor : BaseActor,
        critic : BaseCritic,
        actor_lr = 0.0001,
        critic_lr = 0.001,
        gamma = 0.99,
        batch_size = 128,
        n_cpus = 4,
        device = 'auto',
    ):
        # Environment
        self.env = make_multiprocessing_env(env_name, n_cpus)
        self.eval_env = make_singleprocessing_env(env_name)
        self.n_cpus = n_cpus
        self.n_episode_steps = 1000  # 50000 // self.n_cpus

        # Network
        self.device = get_device(device)

        self.actor = actor
        self.actor.to(self.device)

        self.critic = critic
        self.critic.to(self.device)

        # self.actor_optimizer = torch.optim.Adam(self.actor.parameters(), lr = actor_lr)
        # self.critic_optimizer = torch.optim.Adam(self.critic.parameters(), lr = critic_lr)
        # parameter_group = list(self.actor.parameters()) + list(self.critic.parameters())
        # self.optimizer = torch.optim.Adam(parameter_group, lr = lr)
        self.optimizer = torch.optim.Adam([
            {'params': self.actor.parameters(), 'lr': actor_lr},
            {'params': self.critic.parameters(), 'lr': critic_lr}
        ])

        # Rollout buffer
        self.rollout = RolloutBuffer(self.env.single_observation_space, self.env.single_action_space, self.n_cpus, self.n_episode_steps)
        self.rollout.to_device(self.device)

        # Parameters
        self.epsilon = 0.2
        self.clip_loss_coef = 1.0
        self.value_loss_coef = 1.0
        self.action_entropy_loss_coef = 0.1

        self.gamma = gamma
        self.batch_size = batch_size

        # Logging
        self.logging = None

    def predict(self, obs : np.ndarray, deterministic = False):
        with torch.no_grad():
            action, _ = self.actor.act(np_to_tensor(obs, self.device), deterministic)
            return action.detach().cpu().numpy()

    def get_value(self, batch_obs):
        return self.critic(batch_obs).squeeze()

    def update(self, batch_obs, batch_act, batch_old_a_log_prob, batch_V, batch_return):
        # Evaluate actions
        new_a_log_prob, action_entropy = self.actor.evaluate_act(batch_obs, batch_act)
        V_pred = self.critic.get_value(batch_obs)

        # Compute the advantage value function
        batch_A_hat = batch_return - V_pred.detach()
        batch_A_hat = (batch_A_hat - batch_A_hat.mean()) / (batch_A_hat.std() + 1e-8)

        # Compute the surrogate loss
        ratio = (new_a_log_prob - batch_old_a_log_prob).exp()
        surr1 = ratio * batch_A_hat
        surr2 = torch.clamp(ratio, 1 - self.epsilon, 1 + self.epsilon) * batch_A_hat
        clip_loss = -torch.min(surr1, surr2).mean()
        clip_loss *= self.clip_loss_coef

        # Compute the prediction loss for value function
        value_func_loss = 0.5 * (V_pred - batch_return).pow(2).mean()
        value_func_loss *= self.value_loss_coef
        # # Clipped
        # value_pred_clipped = batch_V + (V_pred - batch_V).clamp(-self.epsilon, self.epsilon)
        # value_losses = (V_pred - batch_return).pow(2)
        # value_losses_clipped = (value_pred_clipped - batch_return).pow(2)
        # value_func_loss = 0.5 * torch.max(value_losses, value_losses_clipped).mean()

        # Action entropy loss (for better exploration)
        action_entropy_loss = -action_entropy.mean()
        action_entropy_loss *= self.action_entropy_loss_coef

        # Symmetry loss
        symmetry_loss = 0.0

        # Total loss
        actor_loss = clip_loss + action_entropy_loss + symmetry_loss
        critic_loss = value_func_loss
        total_loss = actor_loss + critic_loss

        training_info = {
            'Training/clip_loss' : clip_loss,
            'Training/action_entropy_loss' : action_entropy_loss.item(),
            # 'Training/symmetry_loss' : symmetry_loss.item(),
            'Training/actor_loss' : actor_loss.item(),
            'Training/value_func_loss' : value_func_loss.item(),
            'Training/critic_loss' : critic_loss.item(),
            'Training/total_loss' : total_loss.item()
        }

        # Update the critic
        self.optimizer.zero_grad()
        total_loss.backward()
        self.optimizer.step()

        return training_info

    def learn(self, epochs, log_path = None, eval_freq = None, best_model_dir = None):
        # self.actor.train()
        # self.critic.train()

        max_episode_rewards_mean = float('-inf')

        if log_path is not None:
            self.logging = Logger(log_path)

        curr_obs = self.env.reset()
        curr_obs_tensor = np_to_tensor(curr_obs, self.device).float()

        curr_epoch = 0
        eval_every_episode = int(eval_freq * self.n_cpus * self.n_episode_steps)

        # lr_decay = torch.optim.lr_scheduler.MultiStepLR(self.optimizer, [40000, 80000], gamma = 0.1)

        while curr_epoch < epochs:
            self.rollout.reset()

            for _ in range(self.n_episode_steps):
                # Sample trajectories
                with torch.no_grad():
                    action_tensor, action_log_prob_tensor = self.actor.act(curr_obs_tensor)
                    cpu_action = tensor_to_np(action_tensor)
                    value_pred_tensor = self.critic.get_value(curr_obs_tensor)

                next_obs, reward, done, _ = self.env.step(cpu_action)

                self.rollout.add(
                    curr_obs_tensor,
                    action_tensor,
                    np_to_tensor(reward, self.device),
                    np_to_tensor(done, self.device),
                    value_pred_tensor,
                    action_log_prob_tensor
                )
                curr_obs_tensor.copy_(np_to_tensor(next_obs, self.device))

                curr_epoch += self.n_cpus

            # Compute returns
            with torch.no_grad():
                final_V = self.critic.get_value(curr_obs_tensor)
                self.rollout.compute_return(final_V, self.gamma)

            # Training
            epoch_training_info = defaultdict(lambda: 0.0)
            batch_sampler = self.rollout.generate(self.batch_size)
            n_batches = 0
            for sample in batch_sampler:
                n_batches += 1
                batch_obs, batch_act, batch_value_pred, batch_old_action_log_prob, batch_return = sample

                loss_info = self.update(
                    batch_obs,
                    batch_act,
                    batch_old_action_log_prob,
                    batch_value_pred,
                    batch_return
                )

                for loss_name, loss_val in loss_info.items():
                    epoch_training_info[loss_name] += loss_val

            # lr_decay.step()

            # Logging
            for loss_name, loss_val in loss_info.items():
                epoch_training_info[loss_name] /= n_batches

            epoch_training_info['actor_lr'] = self.optimizer.param_groups[0]['lr']
            epoch_training_info['critic_lr'] = self.optimizer.param_groups[1]['lr']

            self.logging.add(curr_epoch, epoch_training_info)

            # Evaluation
            # curr_episode += 1
            if eval_freq is not None and curr_epoch % eval_every_episode == 0:
                (epi_rewards_mean, epi_rewards_std), (epi_steps_mean, epi_steps_std) = evaluate_policy(self.eval_env, self, 50)

                eval_info = {
                    'Eval/epi_rewards_mean' : epi_rewards_mean,
                    'Eval/epi_rewards_std' : epi_rewards_std,
                    'Eval/epi_steps_mean' : epi_steps_mean,
                    'Eval/epi_steps_std' : epi_steps_std
                }
                self.logging.add(curr_epoch, eval_info)

                better = True if epi_rewards_mean >= max_episode_rewards_mean else False
                print('Evaluation: Episode Rewards = {:.2f} +- {:.2f} | Episode Steps = {:.2f} +- {:.2f}'.format(epi_rewards_mean, epi_rewards_std, epi_steps_mean, epi_steps_std))
                if better and best_model_dir is not None:
                    print('Get a better model!')
                    max_episode_rewards_mean = epi_rewards_mean
                    self.save(best_model_dir)
                else:
                    print('Don\'t get a better model!')

        if self.logging is not None:
            self.logging.close()

    def save(self, root_dir):
        if not os.path.exists(root_dir):
            os.makedirs(root_dir)

        # Save actor & critic for independent usage
        self.actor.save(os.path.join(root_dir, 'actor.pkl'))
        self.critic.save(os.path.join(root_dir, 'critic.pkl'))

        # Save PPO state_dict for tweaking
        state_dict = {
            'actor' : self.actor.state_dict(),
            'critic' : self.critic.state_dict(),
            'optimizer' : self.optimizer.state_dict()
        }
        torch.save(state_dict, os.path.join(root_dir, 'PPO.pkl'))

    def load(self, PPO_pkl_path):
        state_dict = torch.load(PPO_pkl_path)
        self.actor.load_state_dict(state_dict['actor'])
        self.critic.load_state_dict(state_dict['critic'])
        self.optimizer.load_state_dict(state_dict['optimizer'])

    def load_actor(self, actor_pkl_path):
        self.actor.load(actor_pkl_path, self.device)
        self.actor.to(self.device)

    def load_critic(self, critic_pkl_path):
        self.critic.load(critic_pkl_path, self.device)
        self.critic.to(self.device)


X = []
Y = []

for _ in range(3):
    num1, num2 = map(int, input().split())
    if num1 in X:
        X.remove(num1)
    else:
        X.append(num1)
    if num2 in Y:
        Y.remove(num2)
    else:
        Y.append(num2)

print(X[0], Y[0])


def run(after):
    arr = [3, 7]
    i = 0
    j = 1

    while True:
        i = (i + arr[i] + 1) % len(arr)
        j = (j + arr[j] + 1) % len(arr)

        added = arr[i] + arr[j]
        digits = [int(c) for c in str(added)]
        arr.extend(digits)

        if len(arr) >= after + 10:
            return arr[after : after + 10]

def run_before(before_sequence):
    arr = [3, 7]
    i = 0
    j = 1
    last_window_start = 0

    while True:
        i = (i + arr[i] + 1) % len(arr)
        j = (j + arr[j] + 1) % len(arr)

        added = arr[i] + arr[j]
        digits = [int(c) for c in str(added)]
        arr.extend(digits)

        while last_window_start < len(arr) - len(before_sequence):
            # print(arr[k : k + len(before_sequence)])
            if (
                arr[last_window_start : last_window_start + len(before_sequence)]
                == before_sequence
            ):
                return last_window_start
            last_window_start += 1

# next_10 = run(after=170641)
# print("".join(str(c) for c in next_10))

# before_i = run_before(before_sequence=[5, 1, 5, 8, 9])
# before_i = run_before(before_sequence=[5, 9, 4, 1, 4])
before_i = run_before(before_sequence=[1, 7, 0, 6, 4, 1])
print(before_i)


import argparse
import logging
import math
import os

from torch import nn, optim
from torch.nn.utils import clip_grad_norm
from torch.optim import lr_scheduler
from torch.utils.data import DataLoader

from src.data import Vocab, TranslationDataset
from src.models import Seq2SeqModel
from src.utils import *

logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

def train(args):
    src_word_vocab = Vocab.from_file(args.src_vocab_path, add_pad=True, add_unk=True)
    tgt_word_vocab = Vocab.from_file(args.tgt_vocab_path, add_pad=True, add_unk=True)
    train_dataset = TranslationDataset(
        data_path=args.data_prefix + '_' + 'train.json',
        src_word_vocab=src_word_vocab,
        tgt_word_vocab=tgt_word_vocab,
        max_length=args.max_length,
        lower=args.lower
    )
    valid_dataset = TranslationDataset(
        data_path=args.data_prefix + '_' + 'dev.json',
        src_word_vocab=src_word_vocab,
        tgt_word_vocab=tgt_word_vocab,
        max_length=args.max_length,
        lower=args.lower
    )
    train_loader = DataLoader(
        dataset=train_dataset, batch_size=args.batch_size,
        shuffle=True, num_workers=2,
        collate_fn=train_dataset.collate,
        pin_memory=True
    )
    valid_loader = DataLoader(
        dataset=valid_dataset, batch_size=args.batch_size,
        shuffle=False, num_workers=2,
        collate_fn=valid_dataset.collate,
        pin_memory=True
    )

    model = Seq2SeqModel(
        num_src_words=len(src_word_vocab),
        num_tgt_words=len(tgt_word_vocab),
        word_dim=args.word_dim,
        hidden_dim=args.hidden_dim,
        use_leaf_rnn=args.leaf_rnn,
        pooling_method=args.pooling,
        dropout_prob=args.dropout,
        bidirectional=args.bidirectional,
        encoder_type=args.encoder_type
    )
    if args.load_checkpoint is not None:
        model.load_state_dict(torch.load(args.load_checkpoint))
    if args.glove:
        logger.info('Loading GloVe pretrained vectors...')
        glove_weight = load_glove(
            path=args.glove, vocab=src_word_vocab,
            init_weight=model.word_embedding.weight.data.numpy())
        glove_weight[src_word_vocab.pad_id] = 0
        model.word_embedding.weight.data.set_(torch.FloatTensor(glove_weight))
    if args.fix_word_embedding:
        logger.info('Will not update word embeddings')
        model.word_embedding.weight.requires_grad = False
    if torch.cuda.is_available():
        model = model.cuda()
    try:
        optimizer_class = getattr(optim, args.optimizer)
    except AttributeError:
        raise Exception('Optimizer {:s} not supported'.format(args.optimizer))
    params = [p for p in model.parameters() if p.requires_grad]
    optimizer = optimizer_class(lr=args.lr, params=params, weight_decay=args.l2reg)
    scheduler = lr_scheduler.ReduceLROnPlateau(
        optimizer=optimizer, mode='max', factor=0.5, patience=10, verbose=True)
    criterion = nn.CrossEntropyLoss(ignore_index=tgt_word_vocab.pad_id)

    def run_iter(batch, is_training):
        model.train(is_training)
        sources = wrap_with_variable(batch['sources'], volatile=not is_training)
        targets_input = wrap_with_variable(batch['targets_input'])
        targets_output = wrap_with_variable(batch['targets_output'])
        src_lengths = wrap_with_variable(batch['src_lengths'], volatile=not is_training)
        tgt_lengths = wrap_with_variable(batch['tgt_lengths'] - 1, volatile=not is_training)
        tree_strs = tree_masks = None
        if args.encoder_type == 'gumbel':
            preds, sents_info = model(
                sources, src_lengths, targets_input, tgt_lengths, return_select_masks=not is_training)
            tree_masks = sents_info[-1]
        elif args.encoder_type in ['parsing', 'balanced', 'left', 'right']:
            if args.encoder_type == 'parsing':
                tree_masks = batch['masks']
                for i, mask in enumerate(tree_masks):
                    tree_masks[i] = wrap_with_variable(mask, volatile=not is_training)
            elif args.encoder_type == 'balanced':
                tree_masks = generate_balance_masks(src_lengths.data.tolist())
            elif args.encoder_type == 'left':
                tree_masks = generate_left_branch_masks(src_lengths.data.tolist())
            elif args.encoder_type == 'right':
                tree_masks = generate_right_branch_masks(src_lengths.data.tolist())
            else:
                raise Exception('Encoder type {:s} not implemented.'.format(args.encoder_type))
            preds, sents_info = model(sources, src_lengths, targets_input, tgt_lengths, tree_masks)
        elif args.encoder_type == 'lstm':
            preds, sents_info = model(sources, src_lengths, targets_input, tgt_lengths)
        else:
            raise Exception('Encoder type {:s} not implemented.'.format(args.encoder_type))
        preds = preds.view(-1, preds.size(2))
        loss = criterion(input=preds, target=targets_output.view(-1))
        if is_training:
            optimizer.zero_grad()
            loss.backward()
            clip_grad_norm(parameters=params, max_norm=5)
            optimizer.step()
            return loss, tree_strs
        else:
            if tree_masks is not None:
                raw_sentences = batch['src_sentences']
                tree_strs = get_tree_structures(raw_sentences, src_lengths, tree_masks)
            # decode BLEU
            bleu_list = list()
            init_word_ids = [tgt_word_vocab.word_to_id('<S>') for _ in range(tgt_lengths.size(0))]
            decode_results = model.decoder.decode(sents_info[0], init_word_ids, args.max_length)
            for i in range(tgt_lengths.size(0)):
                bleu_list.append(
                    bleu(decode_results[i], targets_output[i], tgt_word_vocab.word_to_id('</S>')))
            return loss, tree_strs, bleu_list

    num_train_batches = len(train_loader)
    validate_every = num_train_batches // 10
    best_valid_loss = 1e10
    iter_count = 0
    logger.info('Validate every {:d} batches'.format(validate_every))
    for epoch_num in range(args.max_epoch):
        logger.info('Epoch {:d}: start'.format(epoch_num))
        avg_train_loss = 0
        for batch_iter, train_batch in enumerate(train_loader):
            train_loss, _ = run_iter(batch=train_batch, is_training=True)
            avg_train_loss += unwrap_scalar_variable(train_loss)
            iter_count += 1
            if (batch_iter + 1) % args.log_period == 0:
                if args.encoder_type == 'gumbel':
                    if iter_count % args.anneal_temperature_every == 0:
                        rate = args.anneal_temperature_rate
                        new_temperature = max([0.5, math.exp(-rate * iter_count)])
                        model.encoder.gumbel_temperature = new_temperature
                        logger.info('Iter #{:d}: '
                                    'Set Gumbel temperature to {:.4f}'.format(iter_count, new_temperature))
                avg_train_loss /= args.log_period
                logger.info('Epoch {:d}: {:d} batches, train loss = {:.4f}'.format(
                    epoch_num, batch_iter + 1, avg_train_loss
                ))
                avg_train_loss = 0
            if (batch_iter + 1) % validate_every == 0:
                valid_loss_sum = bleu_sum = num_valid_instances = 0
                num_valid_batches = len(valid_loader)
                tree_strs = None
                for valid_batch in valid_loader:
                    if not tree_strs:
                        valid_loss, tree_strs, bleus = run_iter(batch=valid_batch, is_training=False)
                    else:
                        valid_loss, _, bleus = run_iter(batch=valid_batch, is_training=False)
                    num_valid_instances += valid_batch['sources'].size(0)
                    valid_loss_sum += unwrap_scalar_variable(valid_loss)
                    bleu_sum += sum(bleus)
                valid_loss = valid_loss_sum / num_valid_batches
                valid_bleu = bleu_sum / num_valid_instances
                scheduler.step(-best_valid_loss)
                progress = epoch_num + batch_iter/num_train_batches
                logger.info('Epoch {:.2f}: '
                            'valid loss = {:.4f}, '
                            'BLEU = {:.3f}'.format(progress, valid_loss, valid_bleu))
                if tree_strs is not None:
                    logger.info('Tree Example: {:s}'.format(tree_strs[0]))
                if valid_loss < best_valid_loss:
                    best_valid_loss = valid_loss
                    model_filename = ('model-{:.2f}-{:.4f}-{:.2f}.pkl'.format(
                        progress, valid_loss, valid_bleu))
                    model_path = os.path.join(args.save_dir, model_filename)
                    torch.save(model.state_dict(), model_path)
                    model_path = os.path.join(args.save_dir, 'best_model.pkl')
                    torch.save(model.state_dict(), model_path)
                    print('Saved the new best model to {:s}'.format(model_path))

def main():
    parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
    parser.add_argument('--data-prefix', type=str, required=True)
    parser.add_argument('--src-vocab-path', type=str, required=True)
    parser.add_argument('--tgt-vocab-path', type=str, required=True)
    parser.add_argument('--encoder-type', type=str, required=True)
    parser.add_argument('--pooling', type=str, default=None)
    parser.add_argument('--lower', default=False, action='store_true')
    parser.add_argument('--max-length', type=int, default=64)
    parser.add_argument('--word-dim', type=int, default=300)
    parser.add_argument('--hidden-dim', type=int, default=300)
    parser.add_argument('--leaf-rnn', default=False, action='store_true')
    parser.add_argument('--bidirectional', default=False, action='store_true')
    parser.add_argument('--dropout', type=float, default=0)
    parser.add_argument('--glove', default=None)
    parser.add_argument('--anneal-temperature-every', type=int, default=1e10)  # for Gumbel
    parser.add_argument('--anneal-temperature-rate', type=float, default=0)  # for Gumbel
    parser.add_argument('--fix-word-embedding', default=False, action='store_true')
    parser.add_argument('--batch-size', type=int, default=32)
    parser.add_argument('--max-epoch', type=int, default=5)
    parser.add_argument('--save-dir', type=str, required=True)
    parser.add_argument('--lr', type=float, default=.0002)
    parser.add_argument('--optimizer', type=str, default='Adam')
    parser.add_argument('--l2reg', type=float, default=0)
    parser.add_argument('--log-period', type=int, default=100)
    parser.add_argument('--load-checkpoint', default=None)
    args = parser.parse_args()

    # config log
    if os.path.exists(args.save_dir):
        os.system('rm -rf ' + args.save_dir)
    os.system('mkdir ' + args.save_dir)

    torch.save(args, args.save_dir + '/args.pt')
    handler = logging.FileHandler(args.save_dir + '/train.log', 'w')
    handler.setLevel(logging.INFO)
    handler.setFormatter(formatter)
    # console log
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    console.setFormatter(formatter)

    logger.addHandler(handler)
    logger.addHandler(console)

    train(args)

if __name__ == '__main__':
    main()


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

class SupremeBot:
    def __init__(self):
        self.bot = webdriver.Firefox()

    def shop(self):
        bot = self.bot
        bot.get('https://www.supremenewyork.com/shop/all/jackets')
        items = bot.find_elements_by_css_selector('a.name-link')
        links = [elem.get_attribute('href') for elem in items]
        print(links)

ed = SupremeBot()
ed.shop()


import sys
l = sys.stdin.read().split('\n\n')
res = 0
for p in l:
    x = p.split()
    c = 0
    for i in x:
        s = i.split(':')[0]
        c += (s != 'cid')
    res += (c == 7)
print(res)


from dataclasses import dataclass
from trystructure_namespace_clusters.org.openadr.oadr_2.pkg_0b.pkg_2012.pkg_07.oadr_query_registration_type import OadrQueryRegistrationType

__NAMESPACE__ = "http://openadr.org/oadr-2.0b/2012/07"

@dataclass
class OadrQueryRegistration(OadrQueryRegistrationType):
    """
    Query VTN for registration information without actually registering.
    """
    class Meta:
        name = "oadrQueryRegistration"
        namespace = "http://openadr.org/oadr-2.0b/2012/07"


from collections import deque
class Solution:
    def tribonacci(self, n: int) -> int:
        arr = deque([0 ,1, 1])
        if n < 3: return arr[n]
        for _ in range(3, n + 1):
            tmp = sum(arr)
            arr.popleft()
            arr.append(tmp)
        return arr[-1]

LeetCode/1137. N-th Tribonacci Number.py


from csv import DictReader, DictWriter
from http import HTTPStatus

def open_csv_reading(file):
    with open(file, "r") as f:
        list_products_dict = DictReader(f)
        # return [product for product in list_products_dict]

        list_teste = []
        for product in list_products_dict:
            product["id"] = int(product["id"])
            product["price"] = float(product["price"])
            list_teste.append(product)

        return list_teste

def open_csv_append(file, data, cont_list):
    with open(file, "a") as f:
        fieldnames = ["id", "name", "price"]
        file_open = DictWriter(f, fieldnames=fieldnames)
        file_open.writerow({"id": int(cont_list[::-1][0]) + 1, "name": data["name"], "price": data["price"]})

def open_csv_writer(file, list_writer):
    with open(file, "w") as f:
        fieldnames = ["id", "name", "price"]
        file_writer = DictWriter(f, fieldnames=fieldnames)
        file_writer.writeheader()
        file_writer.writerows(list_writer)


import subprocess
import time
import socket

def ping(host):
    try:
        subprocess.check_output(["ping", "-c", "1", host])
        return True
    except subprocess.CalledProcessError:
        return False

def is_valid_ip(ip):
    try:
        socket.inet_aton(ip)
        return True
    except socket.error:
        return False

def main():
    devices = []

    while True:
        print("\n1. Ajouter une adresse IP")
        print("2. Démarrer la surveillance")
        print("3. Quitter")
        choice = input("Choix : ")

        if choice == "1":
            ip = input("Adresse IP : ")
            if is_valid_ip(ip):
                devices.append(ip)
                print(f"Adresse IP {ip} ajoutée.")
            else:
                print("Adresse IP non valide.")
        elif choice == "2":
            if not devices:
                print("Aucune adresse IP à surveiller.")
            else:
                while True:
                    for device in devices:
                        status = "en ligne" if ping(device) else "hors ligne"
                        print(f"{device} est {status}.")
                    time.sleep(10)
        elif choice == "3":
            break

main()


import constants
from game.casting.actor import Actor
from game.services.video_service import VideoService
from game.shared.point import Point

class Alien(Actor):
    """
    The aliens are enemies that will move down the screen until they are
    hit by a bullet or reach the bottom making the player lose.

    Attributes:
        self.aliens[list]: When an alien is created it will append to this list
    """

    def __init__(self):
        super().__init__()
        self._aliens = []
        self._generate_aliens()
        self._rows = 1
        self._timer = 0

    def _generate_aliens(self):
        """generates a row of aliens at the top"""
        for n in range(constants.COLUMNS):
            x = (n)*constants.CELL_SIZE
            y = 0
            position = Point(x, y)
            velocity = Point(0, constants.CELL_SIZE)
            text = "H"
            color = constants.GREEN
            alien = Actor()
            alien.set_position(position)
            alien.set_velocity(velocity)
            alien.set_text(text)
            alien.set_color(color)
            self._aliens.append(alien)

    def get_aliens(self):
        """returns the list of aliens"""
        return self._aliens

    def move_next(self):
        """
        Moves the aliens down one row when the timer is ready.
        Generates more aliens if the max rows haven't been filled yet.
        Resets the timer.
        """
        self._timer += 1
        if self._timer >= 10:
            for alien in self._aliens:
                alien.move_next()
            if self._rows < constants.MAX_ALIEN_ROWS:
                self._generate_aliens()
                VideoService().draw_actors(self._aliens)
                self._rows += 1
            self._timer = 0


from telethon.sync import TelegramClient
from pytz import timezone
from datetime import timedelta
from datetime import date
import pytz
import pandas as pd
import streamlit as st
import re

import configparser
config = configparser.ConfigParser()
config.read("telethon.config")

api_id = config["telethon_credentials"]["api_id"]
api_hash = config["telethon_credentials"]["api_hash"]
username = config['telethon_credentials']['username']

chats = ['lootbot','dealsworldchannel', 'TrickXpert', 'dealspoint', 'FRCP_Official', 'deals_battle_extra', 'deals_battle', 'kooltech007', 'Offer_point_Deals']
dates = [date.today(),  date.today()-timedelta(days = 1)]
client =  TelegramClient(username, api_id, api_hash)

df = pd.DataFrame()

def remove_stars(x):
    x = ' '.join(str(x).split('\n'))
    x = ''.join(x.split('*'))
    return x

for chat in chats:
    for crr_date in dates:
        with TelegramClient(username, api_id, api_hash) as client:
            # datetime.datetime.utcnow().date()
            for message in client.iter_messages(chat, offset_date=crr_date, reverse=True):
                # print(message)
                # pattern = r'[^\w\s]'
                # try:
                #     text = re.sub(pattern, '', str(message.text))
                # except:
                text = remove_stars(message.text)
                try:
                    link = re.search("(?P<url>https?://[^\s]+)", text).group("url")
                except:
                    link = None

                data = { "group" : chat,
                        "sender" : message.sender_id,
                        "link" : link,
                        "text" : text,
                        "date" : message.date}

                temp_df = pd.DataFrame(data, index=[1])
                df = pd.concat([temp_df, df], ignore_index=True )

df['date'] = df['date'].dt.tz_convert(pytz.timezone('Asia/Kolkata'))
# df['text'] = df['text'].apply(lambda x: re.sub(pattern, '', x))

df.to_csv("data_{}.csv".format(date.today()), index=False)


# middleware.py

from django.utils import timezone
from django.contrib.sessions.models import Session
from django.conf import settings

class SessionTimeoutMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # Get the session key from the request
        session_key = request.session.session_key

        # Check if the user is authenticated and a session key exists
        if request.user.is_authenticated and session_key:
            # Get the session data from the database
            session_data = Session.objects.get(session_key=session_key).get_decoded()

            # Get the last activity time from the session data
            last_activity_str = session_data.get('last_activity')

            # Convert the last_activity string to a timezone-aware datetime object
            last_activity = timezone.datetime.strptime(last_activity_str, '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=timezone.utc) if last_activity_str else None

            # Check if the session has timed out
            if last_activity and (timezone.now() - last_activity).seconds > settings.SESSION_COOKIE_AGE:
                # Log the user out if the session has timed out
                request.session.flush()

        response = self.get_response(request)

        # Update last activity time in the session data
        request.session['last_activity'] = timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')

        return response


import cv2 as cv
import pickle
import numpy as np

cap = cv.VideoCapture('vids_and_pics/carPark.mp4')

width = cap.get(cv.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv.CAP_PROP_FRAME_HEIGHT)
fps = cap.get(cv.CAP_PROP_FPS)
fourcc = cv.VideoWriter_fourcc(*"mp4v")
save_path = 'countParking-output.mp4'
vw = cv.VideoWriter(save_path, fourcc, int(fps), (int(width), int(height)), True)

with open('CarParkPos', 'rb') as f:
    posList = pickle.load(f)

width, height = 107, 48

def put_text_rect(img, text, pos, scale=3, thickness=1, colorT=(255, 255, 255), colorR=(255, 0, 255),
                  font=cv.FONT_HERSHEY_COMPLEX, offset=10, border=None, colorB=(0, 255, 0)):
    ox, oy = pos
    (w, h), _ = cv.getTextSize(text, font, scale, thickness)

    x1, y1, x2, y2 = ox - offset, oy - offset, ox + w + offset, oy - h - offset
    cv.rectangle(img, (x1, y1), (x2, y2), colorR, cv.FILLED)
    if border is not None:
        cv.rectangle(img, (x1, y1), (x2, y2), colorB, border)
    cv.putText(img, text, (ox, oy), font, scale, colorT, thickness)

    return img, [x1, y2, x2, y1]

def car_park_check(img):
    carpark_count = 0
    for pos in posList:
        x, y = pos
        carpark_img = img[y:y + height, x:x + width]
        whiteness = cv.countNonZero(carpark_img)

        if whiteness < 500:
            carpark_count += 1
            color = (0, 255, 0)
        else:
            color = (0, 0, 255)

        cv.rectangle(frame, pos, (pos[0] + width, pos[1] + height), color, 3)
        put_text_rect(frame, str(whiteness), (x, y + height), scale=0.5, thickness=1, offset=0, colorR=whiteness)

    put_text_rect(frame, f'Free: {carpark_count}/{len(posList)}', (50, 50), scale=1, thickness=1, offset=0,
                  colorR=(0, 200, 0))

while True:
    _, frame = cap.read()
    frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    imgBlur = cv.GaussianBlur(frame_gray, (3, 3), 1)
    imgThreshold = cv.adaptiveThreshold(imgBlur, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 25, 16)

    car_park_check(imgThreshold)
    cv.imshow('image', frame)
    vw.write(frame)
    if cv.waitKey(25) & 0xFF == ord('q'):
        break

cv.destroyAllWindows()


from itertools import combinations

with open("input.txt") as f:
    numbers = f.read().split("\n")

numbers = list(map(lambda x: int(x), numbers))
window = 25
invalid_num = 0

for i in range(len(numbers)):
    pairs = list(combinations(numbers[i : i + window], 2))
    sums = [pair[0] + pair[1] for pair in pairs if pair[0] != pair[1]]
    fail = True
    if numbers[i + window] in sums:
        fail = False

    if fail:
        invalid_num = numbers[i + window]
        break

for i in range(len(numbers)):
    total = numbers[i]
    j = i + 1
    while j <= len(numbers):
        if total == invalid_num and j - i > 1:
            print(numbers[i:j], sum(numbers[i:j]))
            print(
                "Weakness:",
                min(numbers[i:j]) + max(numbers[i:j]),
            )
            break

        if total > invalid_num:
            break
        total = total + numbers[j]
        j += 1


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

"""
朴素贝叶斯分类
"""

import numpy as np
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.naive_bayes import BernoulliNB

# 高斯朴素贝叶斯
def gaussianNB():
    X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
    Y = np.array([1, 1, 1, 2, 2, 2])

    clf = GaussianNB()
    clf.fit(X, Y)
    print(clf.predict([[-0.8, -1]]))

    clf_pf = GaussianNB()
    clf_pf.partial_fit(X, Y, np.unique(Y))
    print(clf_pf.predict([[-0.8, -1]]))

# 多项朴素贝叶斯
def multinomialNB():
    X = np.random.randint(5, size=(6, 100))
    Y = np.array([1, 2, 3, 4, 5, 6])
    clf = MultinomialNB()
    clf.fit(X, Y)
    print(clf.predict(X[2:3]))

# 伯努利朴素贝叶斯
def bernoulliNB():
    X = np.random.randint(2, size=(6, 100))
    Y = np.array([1, 2, 3, 4, 4, 5])
    clf = BernoulliNB()
    clf.fit(X, Y)
    print(clf.predict(X[2:3]))

if __name__ == '__main__':
    gaussianNB()
    multinomialNB()
    bernoulliNB()


import pandas as pd

def _get_x_value_across_columns(df,function, fields, lower_padding, upper_padding):
    if type(df) == pd.core.groupby.generic.DataFrameGroupBy:
        final_df = df.apply(lambda x: x)
    else:
        final_df = df

    values = []

    for field in fields:
        column_values = final_df[field]
        values.append(function(column_values))

    value = function(values)

    if value < 0:
        value = value * lower_padding
    else:
        value = value * upper_padding

    return value

def get_min_value_across_columns(df, fields=[],padding=0):

    lower_padding = 1+padding
    upper_padding = 1-padding

    return _get_x_value_across_columns(df,min,fields=fields,lower_padding=lower_padding, upper_padding=upper_padding)

def get_max_value_across_columns(df, fields=[],padding=0):

    lower_padding = 1-padding
    upper_padding = 1+padding

    return _get_x_value_across_columns(df,max,fields=fields,lower_padding=lower_padding, upper_padding=upper_padding)

def drop_outliers_of_columns(df, columns):
    df_new = df.copy()
    for column in columns:
        if df_new[column].dtype in [float,int]:
            df_new = drop_outliers_of_column(df_new, column, upper_quantile=0.90)
    return df_new

def drop_outliers_of_column(df, column, lower_quantile=0.10, upper_quantile=0.90):
    q_max = df[column].quantile(upper_quantile)
    q_min = df[column].quantile(lower_quantile)
    df_new = df[(df[column] <= q_max) & (df[column] >= q_min)]
    return df_new

def _correlation_matrix_style(v, upper_cut_off_correlation, lower_cut_off_correlation):
    upper_cut_off = (v > upper_cut_off_correlation and v > 0) or (v < -upper_cut_off_correlation and v < 0)
    lower_cut_off = (v < lower_cut_off_correlation and v > 0) or (v > -lower_cut_off_correlation and v < 0)
    if upper_cut_off:
        return "background: red"
    elif lower_cut_off:
        return "background: green"
    else:
        return "background: orange"

def correlation_matrix(df, upper_cut_off_correlation=0.8,lower_cut_off_correlation=0.2):
    correlation_matrix = df.corr()
    return correlation_matrix.style.apply(lambda x: [_correlation_matrix_style(v, upper_cut_off_correlation, lower_cut_off_correlation) for v in x], axis = 1).format(precision=2)

def only_numeric_columns(df,columns = []):
    if len(columns) == 0:
        columns = df.columns
    numeric_columns = []
    for column in columns:
        if df[column].dtype in [float,int]:
            numeric_columns.append(column)
    return numeric_columns

def extract_subject_dataframe(df,age_group,subject_index=-1):
    subject = df[df['age_group']==age_group]['subject'].unique()[subject_index]
    subject_df = df[df['subject'] == subject]
    return subject_df

def extract_subject_dataframes(df,subject_index=3):
    subject_30_df = extract_subject_dataframe(df,30,subject_index)
    subject_50_df = extract_subject_dataframe(df,50,subject_index)
    return subject_30_df, subject_50_df

def extract_sample_sessions(df,subject_index=3,session_index=-1):
    subject_30_df, subject_50_df = extract_subject_dataframes(df, subject_index)
    uuid_30 = subject_30_df[subject_30_df['hand']=='dominant']['uuid'].unique()[session_index]
    uuid_50 = subject_50_df[subject_50_df['hand']=='dominant']['uuid'].unique()[session_index]
    single_session_30_df = df[df['uuid'] == uuid_30]
    single_session_50_df = df[df['uuid'] == uuid_50]
    return single_session_30_df, single_session_50_df

def split_by_column(df, column):
    device_dfs = []
    for device in df[column].unique():
        mask = df[column] == device
        current_df = df[mask]
        device_dfs.append(current_df)
    return device_dfs

def reduce_to_one_session_for_subject(df):
    df_copy = df.copy()
    uuids = []
    for subject in df_copy['subject']:
        uuid = df_copy[df_copy['subject'] == subject]['uuid'].unique().any()
        if uuid not in uuids:
            uuids.append(uuid)
    return df_copy[df_copy['uuid'].isin(uuids)]


#!/usr/bin/env python

"""The setup script."""

from setuptools import setup, find_packages

with open('README.md') as readme_file:
    readme = readme_file.read()

with open('HISTORY.md') as history_file:
    history = history_file.read()

with open('requirements.txt') as f:
    requirements = f.read().splitlines()

setup_requirements = ['pytest-runner', ]

test_requirements = ['pytest>=3', ]

setup(
    author="Aron C",
    author_email='aronwc@gmail.com',
    python_requires='>=3.5',
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
        'Natural Language :: English',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
    ],
    description="utilies for github classroom",
    entry_points={
        'console_scripts': [
            'ghc-utils=ghc_utils.cli:main',
            'clone=ghc_utils.cli:clone',
            'grades=ghc_utils.cli:grades',
            'delete=ghc_utils.cli:delete',
        ],
    },
    install_requires=requirements,
    license="GNU General Public License v3",
    long_description_content_type="text/markdown",
    long_description=readme + '\n\n' + history,
    include_package_data=True,
    keywords='ghc_utils',
    name='ghc_utils',
    packages=find_packages(include=['ghc_utils', 'ghc_utils.*']),
    setup_requires=setup_requirements,
    test_suite='tests',
    tests_require=test_requirements,
    url='https://github.com/aronwc/ghc-utils',
    version='0.1.2',
    zip_safe=False,
)


from django.urls import include, path

from . import views

urlpatterns_v1 = [
    path('invoices/', views.InvoiceListCreateAPI.as_view()),
    path('invoices/<int:pk>/', views.InvoiceDetailPayAPI.as_view()),
    path('cards/', views.CardListCreateAPI.as_view()),
    path('cards/<int:pk>/', views.CardDetailDestroyAPI.as_view()),
    path('auth/users/me/', views.UserDetailAPI.as_view()),
    path('auth/', include('djoser.urls')),
    path('auth/', include('djoser.urls.jwt')),
]

urlpatterns = [
    path('v1/', include(urlpatterns_v1)),
]


#import libraries
from queue import Queue
from threading import Thread
import time
import sys
import os
import subprocess

#modules for requesting
import requests

#custom modules
from general import  *
import eventmanager as evm
import telegramlistener as tl
import interpreter

ppath = getParentPath ()

try:
    os.remove (ppath / 'data' / 'communication' / 'terminate.txt')
    os.remove (ppath / 'data' / 'communication' / 'console.txt')
    os.remove (ppath / 'data' / 'communication' / 'console-ready.txt')
    os.remove (ppath / 'data' / 'communication' / 'evm.txt')
    os.remove (ppath / 'data' / 'communication' / 'evm-ready.txt')
    os.remove (ppath / 'data' / 'communication' / 'main.txt')
    os.remove (ppath / 'data' / 'communication' / 'main-ready.txt')
except Exception as e:
    pass

global listener_callback
listener_callback = True

def sendTelegramMessage (input):
    nowtime = current_time()
    message = input.lstrip()

    if message != "":
        evm.event_log("User issued command <send> with message '" + message +"'. Sending it...", "Sending message '" + message + "' ....", module="Listener", level=2, time=nowtime, mprefix=2)
        result = tl.sendMessage(message)

        if result == "ConnectionError":
            evm.event_log("Connection Error. Failed to send the message.", "Failed to send the message. Pleasy try again later.", module="Listener", level=4, time=nowtime, mprefix=2)
        else:
            evm.event_log("Message send successfully", "Success!", module="Listener", level=2, time=nowtime, mprefix=2)

    else:
        evm.event_log("User issued command <send> without a message, ignoring it.", consolemessage= "Error: No message found behind the command.", level=1, mprefix=2, module="Console", time=nowtime)

def sendTelegramMessageSilent (input):
    nowtime = current_time()
    message = input.lstrip()

    if message != "":
        result = tl.sendMessage(message)

#Function to recieve and read messages from eventmanager
def communicationFunc():
    global listener_callback
    listener_callback = True

    while 1:
        fileexists1 = os.path.isfile (ppath / 'data' / 'communication' / 'main-ready.txt')

        if fileexists1 == True:

            f = open (ppath / 'data' / 'communication' / 'main-ready.txt', "r", encoding='utf8')
            lines = f.read()
            f.close ()
            os.remove(ppath / 'data' / 'communication' / 'main-ready.txt')

            messageData1 = lines.splitlines()

            if messageData1[0] == "send": #Send telegram message
                sendTelegramMessage (messageData1[1])

            elif messageData1[0] == "m" or messageData1[0] == "i": #analyse manual command
                evm.event_log("Manual command: <" + messageData1[1] + ">. Sending to Interpreter.",
                "Manual commmand: <" + messageData1[1] + ">. Analysing ...",
                module="MAIN", level=2, mprefix=1, time=current_time())

                interpreter_return = interpreter.interpreter(messageData1[1])
                evm.event_log(interpreter_return, module="INTERPRETER", level=2)

                file = open(ppath / 'data' / 'communication' / 'interpreter.txt', "a", encoding='utf8')
                file.write (interpreter_return)
                file.close()
                os.rename(ppath / 'data' / 'communication' / 'interpreter.txt', ppath / 'data' / 'communication' / 'interpreter-ready.txt')

            elif messageData1[0] == "online" or messageData1[0] == "on" or messageData1[0] == "om": #decide if program should receive new messages
                if len(messageData1)> 1:

                    messageData1[1] = messageData1[1].lower()
                    if messageData1[1] == "true":
                        if listener_callback != True:
                            listener_callback = True
                            evm.event_log("Set onlinemode back to 'true'.", "Set onlinemode back to 'true'. Commands via Telegram can be recieved again.", module="LISTENER", mprefix=2)
                        else:
                            evm.event_log("Nothing has changed. Onlinemode was already 'true'.", "Nothing has changed. Onlinemode was already 'true'.", module="LISTENER", mprefix=2)

                    elif messageData1[1] == "false":
                        if listener_callback != False:
                            listener_callback = False
                            evm.event_log("Set onlinemode to 'false'.", "Set onlinemode to 'false'. Commands via Telegram are ignored.", module="LISTENER", mprefix=2)
                        else:
                            evm.event_log("Nothing has changed. Onlinemode was already 'false'.", "Nothing has changed. Onlinemode was already 'false'.", module="LISTENER", mprefix=2)
                else:
                    evm.event_log("Error, command called with wrong number of arguments.", consolemessage="Error. Please define the state of the program by using 'true' or 'false' as argument.", module="INTERPRETER", level=2, mprefix=2)

        fileexists2 = os.path.isfile (ppath / 'data' / 'communication' / 'evm-ready.txt')

        if fileexists2 == True:

            f = open (ppath / 'data' / 'communication' / 'evm-ready.txt', "r")
            lines = f.read()
            f.close()
            os.remove(ppath / 'data' / 'communication' / 'evm-ready.txt')

            messageData2 = lines.splitlines()

            evm.event_log(messageData2[0], module="CONSOLE", level=int(messageData2[1]), time=messageData2[2])

        fileexists3 = os.path.isfile(ppath / 'data' / 'communication' / 'terminate.txt')

        if fileexists3 == True:
            evm.event_log ("Shutting down communicationthread.", module = "MAIN", level=2)
            return

        time.sleep (0.05)

def telegram_listener ():
    global listener_callback
    listener_callback = True

    #Prepare Listener Loop
    botToken = tl.get_botToken()
    chatID = tl.get_chatID()

    evm.event_log("Prepared Listener with "+ botToken + " as token for Telegram API.", module="MAIN", level=2)

    #start while-loop and main program
    while True:

        result = tl.Polling(botToken)

        while listener_callback == False:
            result_reconnect = tl.Polling(botToken)
            if result_reconnect == False:
                evm.event_log ("Shutting down listenerthread", module = "MAIN", level=2)
                sys.exit()
            else:
                time.sleep(3)

        #Handle exceptions before mainloop
        if result == "ConnectionError":
            print ("Main:Error")
            evm.event_log("Connection Error. Reconnecting...",
            "An error occured (Connection Error). Program will automatically reconnect",
            module="LISTENER", time=current_time(), level=3, mprefix=1)

            while 1:
                result_reconnect = tl.Polling(botToken)
                if result_reconnect == False:
                    evm.event_log ("Shutting down listenerthread", module = "MAIN", level=2)
                    sys.exit()
                elif result_reconnect != "ConnectionError":
                    evm.event_log("Connection reestablished.", "Connection reestablished.", module="LISTENER", time=current_time(), level=2, mprefix=1)

                    result = result_reconnect

                    try:

                        if result['channel_post']['chat']['id'] == int(chatID):

                            result_text = result['channel_post']['text']

                            evm.event_log("New incoming command: <" + result_text + ">. Sending to Interpreter.",
                            "New incoming commmand: <" + result_text + ">. Analysing ...",
                            module="LISTENER", level=2, mprefix=1, time=current_time())

                            interpreter_return = interpreter.interpreter(result_text)
                            evm.event_log(interpreter_return, module="INTERPRETER", level=2)

                            sendTelegramMessageSilent ('Analysis | "' + result_text + '"\n' + interpreter_return)

                            file = open(ppath / 'data' / 'communication' / 'interpreter.txt', "a")
                            file.write (interpreter_return)
                            file.close()
                            os.rename(ppath / 'data' / 'communication' / 'interpreter.txt', ppath / 'data' / 'communication' / 'interpreter-ready.txt')

                    except Exception as c:
                        #print (c)
                        pass

                    break
                else:
                    time.sleep(3)

        elif result == False:
            evm.event_log ("Shutting down listenerthread", module = "MAIN", level=2)
            return
        else:
            try:

                if result['channel_post']['chat']['id'] == int(chatID):

                    result_text = result['channel_post']['text']

                    evm.event_log("New incoming command: <" + result_text + ">. Sending to Interpreter.",
                    "New incoming commmand: <" + result_text + ">. Analysing ...",
                    module="LISTENER", level=2, mprefix=1, time=current_time())

                    interpreter_return = interpreter.interpreter(result_text)
                    evm.event_log(interpreter_return, module="INTERPRETER", level=2)

                    sendTelegramMessageSilent ('Analysis | "' + result_text + '"\n' + interpreter_return)

                    file = open(ppath / 'data' / 'communication' / 'interpreter.txt', "a")
                    file.write (interpreter_return)
                    file.close()
                    os.rename(ppath / 'data' / 'communication' / 'interpreter.txt', ppath / 'data' / 'communication' / 'interpreter-ready.txt')

            except Exception as c:
                #print (c)
                pass

def main():

    #Start the program
    evm.configure_logger()

    #Start Communication-Thread
    communication_thread = Thread (target=communicationFunc)
    communication_thread.start()
    evm.event_log("Started communicationthread.", module="MAIN", level=2)

    #Start Communication-Thread
    listener_thread = Thread (target=telegram_listener)
    listener_thread.start()
    evm.event_log("Started listenerthread.", module="MAIN", level=2)

    #Start console
    subprocess.Popen(str(ppath / 'ui' / 'GA2W Console.exe'))
    evm.event_log("Started console UI.", module = "MAIN", level=2)

    #Starting mainloop
    while 1:
        fileexists4 = os.path.isfile(ppath / 'data' / 'communication' / 'terminate.txt')

        if fileexists4 == True:
            communication_thread.join()
            listener_thread.join()
            break
        else:
            time.sleep(3)

    #Finishing and clearning up

    os.remove (ppath / 'data' / 'communication' / 'terminate.txt')
    evm.event_log("Program terminated.", module="MAIN", level=2)
    sys.exit()

#Start mainloop
#if __name__ == '__main__':
main()


import logging
from typing import Any, Optional, Text, List
from rasa.core.domain import Domain
from rasa.core.featurizers import TrackerFeaturizer
from rasa.core.policies.policy import Policy
from rasa.core.trackers import DialogueStateTracker
from rasa.core.events import UserUttered, SlotSet, BotUttered
import os

from starbot.policy.gpt2_extractor.gpt2_extractor import Gpt2Extractor
from starbot.utils.colors import reset, Fg

logger = logging.getLogger(__name__)

class DirectAction(Policy):
    """The policy that invoke action according to intent directly
    """
    gpt2_extractor: Gpt2Extractor

    def __init__(
            self,
            featurizer: Optional[TrackerFeaturizer] = None,
            priority: int = 4,
    ) -> None:
        super(DirectAction, self).__init__(featurizer, priority)

    def train(
            self,
            training_trackers: List[DialogueStateTracker],
            domain: Domain,
            **kwargs: Any
    ) -> None:
        """Trains the policy on given training trackers."""
        pass

    def continue_training(
            self,
            training_trackers: List[DialogueStateTracker],
            domain: Domain,
            **kwargs: Any
    ) -> None:
        pass

    def predict_action_probabilities(
            self, tracker: DialogueStateTracker, domain: Domain
    ) -> List[float]:
        """Predicts the next action the bot should take
            after seeing the tracker.

            Returns the list of probabilities for the next actions.
            If memorized action was found returns 1.1 for its index,
            else returns 0.0 for all actions."""
        result = [0.0] * domain.num_actions
        message: UserUttered = tracker.events[-1]
        if tracker.events and isinstance(message, UserUttered):
            # 如果bot最后说的话里面有提示备选项，用gpt2辅助提取ner
            index = domain.index_for_action('action_process_intent')
            result[index] = 1.0
            prompt = self.get_bot_prompt(tracker)

            logger.info(f'user uttered:{Fg.red}{message.text}{reset}')
            logger.info(f'prompt is {Fg.red}{prompt}{reset}')

            if prompt and prompt[0] not in ['used']:
                gpt2out = self.gpt2_extractor.process(prompt, message.text)
                logger.info(f'gpt2out:{Fg.red}{gpt2out}{reset}')
                gpt2out = gpt2out.splitlines(keepends=False)[0]
                if len(self.get_common_substr(gpt2out, message.text)) >= 2:
                    message.parse_data['gpt2out'] = gpt2out
        return result

    @staticmethod
    def get_bot_prompt(tracker: DialogueStateTracker):
        count = 0
        for event in reversed(tracker.events):
            if isinstance(event, BotUttered):
                if event.text.startswith('/'):
                    continue
                prompt = event.metadata.get('prompt')
                if prompt:
                    return prompt
                count += 1
                if count >= 3:
                    return None

    def persist(self, path: Text) -> None:
        logger.debug(f"path is {path}")
        os.system(f"mkdir -p '{path}'")
        os.system(f"cp -r /codes/starbot/run/huggpt2/* '{path}'")
        Gpt2Extractor.persist(path)
        return None

    @classmethod
    def load(cls,
             path: Text) -> "DirectAction":
        slf = cls()
        slf.gpt2_extractor = Gpt2Extractor.load(path)
        return slf

    '''
    求两个字符串的最长公共子串
    思想：建立一个二维数组，保存连续位相同与否的状态
    '''
    @staticmethod
    def get_common_substr(str1, str2):
        lstr1 = len(str1)
        lstr2 = len(str2)
        record = [[0 for i in range(lstr2 + 1)] for j in range(lstr1 + 1)]  # 多一位
        max_num = 0  # 最长匹配长度
        p = 0  # 匹配的起始位

        for i in range(lstr1):
            for j in range(lstr2):
                if str1[i] == str2[j]:
                    # 相同则累加
                    record[i + 1][j + 1] = record[i][j] + 1
                    if record[i + 1][j + 1] > max_num:
                        # 获取最大匹配长度
                        max_num = record[i + 1][j + 1]
                        # 记录最大匹配长度的终止位置
                        p = i + 1
        return str1[p - max_num:p]


import time

def primes(n):
    P = []
    f = []
    for i in range(n + 1):
        if i > 2 and i % 2 == 0:
            f.append(1)
        else:
            f.append(0)

    i = 3
    while i * i <= n:
        if f[i] == 0:
            j = i * i
            while j <= n:
                f[j] = 1
                j += i + i
        i += 2

    P.append(2)
    for i in range(3, n, 2):
        if f[i] == 0:
            P.append(i)

    return P

def isPrime(n):
    if n > 2 and n % 2 == 0:
        return 0

    i = 3
    while i * i <= n:
        if n % i == 0:
            return 0
        i += 2

    return 1

def primeCnt(n):
    cnt = 0
    for i in range(2, n):
        if isPrime(i):
            cnt += 1
    return cnt

if __name__ == '__main__':
    start = time.process_time()
    n = 10000000
    P = primes(n)
    print("There are %d primes less than %d" % (len(P), n))
    # for i in range(10):
    #  print(P[i])
    print("Time: %f" % (time.process_time() - start))
    # for n in range(2,100000):
    #  if isPrime(n):
    #    print("%d is prime"%n)
    # print("%d is "%n + ("prime" if isPrime(n) else "not prime"))

    start = time.process_time()
    n = 1000000
    print("There are %d primes less than %d" % (primeCnt(n), n))
    print("Time: %f" % (time.process_time() - start))


# ========================================================================
# Figure 2 - (Prisma) + citation netowork
# ========================================================================

# %%
pp = '/Users/llotter/MAsync/project'
wd = '/Users/llotter/MAsync/project/data'
sdir = '/Users/llotter/MAsync/project/fig'

import sys
from os.path import join
sys.path.append(join(pp, 'src'))
from utils_io import csv_to_nimare_ds, fnirs_to_nimare_dataset
from citenet import citeNet
import pandas as pd
import numpy as np
import json
import networkx
from pyvis.network import Network
import matplotlib.pyplot as plt
from nilearn.plotting import plot_markers, plot_connectome
from matplotlib import cm
from matplotlib.colors import to_hex

# colors
cmap = list(cm.get_cmap('tab10').colors)
col_fMRI = [to_hex(cmap[3]), to_hex(cmap[3])+'cc']
col_fNIRS = [to_hex(cmap[0]), to_hex(cmap[0])+'cc']

# %% get data

# nimare ds
nim_fMRI = csv_to_nimare_ds(join(wd, 'datasets', 'fMRI_coordinates.csv'), # spreadsheed with study info
                      exp_var='publication', # column indicating experiment names
                      n_var  ='n', # column indicating sample sizes
                      con_var='contrasts', # column indicating contrast names
                      spa_var='space', # column indicating coordinate space
                      x_var  ='x', y_var='y', z_var='z', # columns indicating coordinates
                      single_contrasts=False) # concatenate contrasts per experiment
nim_fNIRS = fnirs_to_nimare_dataset(join(wd, 'datasets', 'fNIRS_coordinates.csv'), False)
nim_ds = nim_fMRI.merge(nim_fNIRS)

# metadata
ds_fMRI = pd.read_csv(join(wd, 'datasets', 'fMRI_coordinates.csv'))
ds_fMRI['type'] = 'fMRI'
ds_fMRI.reset_index(drop=True, inplace=True)
ds_fNIRS = pd.read_csv(join(wd, 'datasets', 'fNIRS_coordinates.csv'))
ds_fNIRS.rename(columns={'contrast': 'contrasts'}, inplace=True)
ds_fNIRS['type'] = 'fNIRS'
ds_fNIRS.reset_index(drop=True, inplace=True)
ds_merged = pd.concat([ds_fMRI, ds_fNIRS], )

# get dois as identifier
dois = ds_merged.doi.unique().tolist()
dois = [d for d in dois if str(d)!='nan']

# general data
study_info = pd.read_csv(join(sdir, 'citenet', 'included_studies_data.csv'))

# create nw data dict
nw_data = {}
for n_doi, doi in enumerate(dois):
    print(doi)
    # get doi
    ds_doi = ds_merged[ds_merged.doi==doi]
    # references
    references = citeNet.get_references_from_doi(doi, which_list="references"),
    citations = citeNet.get_references_from_doi(doi, which_list="citations")
    # coordinates

    # save data
    nw_data[doi] = {
        'title': study_info.Title[study_info.DOI==doi].values[0],
        'author': study_info.Author[study_info.DOI==doi].values[0],
        'year': study_info.Year[study_info.DOI==doi].values[0],
        'type': ds_doi.type.unique()[0],
        'reference': [r for r in references[0]],
        'citation': citations,
        'citation_count': len(citations),
        'exps': list(),
        'foci': list()
    }
    # get & save experiment(s)
    exps_doi = ds_doi.publication.unique()
    for exp in exps_doi:
        ds_exp = ds_doi[ds_doi.publication==exp]
        fem = ds_exp.propFem.unique()[0] if ds_exp.type.unique()[0]=='fMRI' else ds_exp.nFem.unique()[0]/ds_exp.n.unique()[0]
        nw_data[doi]['exps'].append({
            'exp': exp,
            'area': ds_exp.area.unique()[0],
            'n': ds_exp.n.unique()[0],
            'nDyads': ds_exp.nDyads.unique()[0],
            'propFem': fem,
            'ageMean': ds_exp.ageMean.unique()[0],
            'task': ds_exp.task.unique()[0],
            'contrast': ds_exp.contrasts.unique().tolist()
        })
        nw_data[doi]['foci'].append(
            nim_ds.coordinates[nim_ds.coordinates.study_id==exp][['x','y','z']].values)
    # create glassbrain
    # coords
    foci = nw_data[doi]['foci']
    foci = np.concatenate(foci)
    marker_color = col_fMRI[1] if nw_data[doi]['type']=='fMRI' else col_fNIRS[1]
    fig = plt.figure(figsize=(2,2))
    gb = plot_connectome(np.full([len(foci),len(foci),], np.nan), foci, figure=fig, colorbar=None,
                         display_mode='z', node_size=130, node_color=marker_color, annotate=False) #output_file=brain_path,
    # fNIRS: all channel coords
    if nw_data[doi]['type']=='fNIRS':
        all_foci = ds_fNIRS.query("doi==@doi")[["x","y","z"]].values
        gb.add_markers(all_foci, alpha=0.4, marker_color="black")
    # save
    plt.savefig(join(sdir, 'citenet_brains', f'doi_{n_doi}.svg'), transparent=True, bbox_inches='tight')
    plt.close()
    nw_data[doi]['brain'] = f'citenet_brains/doi_{n_doi}.svg'

# save
#with open(join(sdir, 'citenet', 'included_studies_fMRI.json'), 'w') as f:
#    json.dump(nw_data, f)

# %% citation network ========================================================================

# load oc data
#with open(join(sdir, 'citenet', 'included_studies_fMRI.json')) as f:
#    nw_data = json.load(f)

# get list of dois
dois = list(nw_data.keys())

# get areas for coloring
#areas = [nw_data[record]['exps'][0]['area'] for record in nw_data]
#communities = list()
#for area in list(set(areas)):
#    communities.append([dois[i] for i,a in enumerate(areas) if a==area])

## create Networkx network -----------------------------------------------
nx = networkx.DiGraph() # initialize network
nx.add_nodes_from(dois) # add nodes, nodes are referred to by doi

## draw edges
for record in nw_data:
    ## get dois of references within our network and add edges
    refs = [r for r in dois if r in nw_data[record]["reference"]]
    [nx.add_edge(record, ref) for ref in refs]
    ## do the same but for citations - redundant, necessary?
    cits = [c for c in dois if c in nw_data[record]["citation"]]
    [nx.add_edge(cit, record) for cit in cits]

## create pyvis network --------------------------------------------------

## layout
# network layout is passed to visjs as a string in json format
nw_layout = """
    var options = {
        "nodes": {
            "scaling": {
                "min": 30,
                "max": 50
            },
            "borderWidth": 1.5,
            "borderWidthSelected": 3.5,
            "font": {
                "size": 20,
                "background": "rgba(255,255,255,0.5)"
                },
            "shadow": {
                "enabled": true
                }
            },
        "edges": {
            "font": {
                "color": "transparent",
                "strokeColor": "transparent",
                "background": "transparent"
                },
            "color": {
                "inherit": true,
                "opacity": 0.6
                },
            "smooth": {
                "type": "continuous",
                "forceDirection": "none"
                }
            },
        "interaction": {
            "keyboard": {
                "enabled": true
                },
            "navigationButtons": true,
            "zoomView": true
            },
        "layout": {
            "improvedLayout": false,
            "randomSeed": 4
        },
        "physics": {
            "enabled": true,
            "forceAtlas2Based": {
                "gravitationalConstant": -100,
                "springLength": 220,
                "springConstant": 0.01,
                "avoidOverlap": 0.5
                },
            "maxVelocity": 50,
            "solver": "forceAtlas2Based",
            "timestep": 0.5,
            "stabilization": {
                "enabled": true,
                "fit": true
                }
            }
        }
    """

    ## initialize pyvis network and populate with nx data
nw = Network(directed=True,
             height='100%', width='100%', bgcolor='#ffffff', font_color=False,
             heading="")
nw.from_nx(nx) # load nx data
nw.set_options(nw_layout)

## iterate over records, find respective node, add metadata & set details
for i, record in enumerate(nw_data):
    # author & year
    authoryear = nw_data[record]['author']+', '+nw_data[record]['year']
    # experiments
    exp_info = ''
    for i, exp in enumerate(nw_data[record]['exps']):
        exp_info += """
            Experiment {i}: {name}

            Area: {area}

            Task: {task}

            Contrast(s): {contrast}

            Sample: {nsub:.0f} subj; {ndy:.0f} dyads; {age:.2f} y; {fem:.0%} ♀

            """.format(i=i+1, name=exp['exp'], area=exp['area'], task=exp['task'],
                       contrast='; '.join(exp['contrast']), nsub=exp['n'], ndy=exp['nDyads'],
                       age=exp['ageMean'], fem=exp['propFem'])
    ## add metadata to respective node
    nw.get_node(record)["label"] = authoryear
    nw.get_node(record)["title"] = """

                {a} ({type}):

                {t}

 {exps}

                Citation-count: NW: {c_nw}, OC: {c_all}

                Reference-count: NW: {r_nw}, OC: {r_all}

        """.format(a=authoryear, type=nw_data[record]["type"], t=nw_data[record]["title"], d=record, exps=exp_info,
                    c_nw=nx.in_degree[record], r_nw=nx.out_degree[record],
                    c_all=nw_data[record]["citation_count"], r_all=len(nw_data[record]["reference"]),
                    img=nw_data[record]['brain'])

    ## set node size
    nw.get_node(record)["value"] = nx.in_degree[record]
    ## node shape
    nw.get_node(record)["shape"] = "circularImage"
    nw.get_node(record)["image"] = nw_data[record]['brain']
    #nw.get_node(record)["shape"] = "dot" if nw_data[record]["type"] == 'fMRI' else 'diamond'
    ## node color
    nw.get_node(record)["color"] = {
        "border": col_fMRI[0] if nw_data[record]["type"]=='fMRI' else col_fNIRS[0],
        "background": 'white',
        "highlight": {
            "border": col_fMRI[0] if nw_data[record]["type"]=='fMRI' else col_fNIRS[0],
            "background": 'white'
        }
    }

    ## color-code communities
    # iterate over nodes in communites and set colors
    #for i, c in enumerate(communities):
    #    for node in c:
    #        nw.get_node(node)["color"] = {
    #            "border": colors[i],
    #            "background": 'white',
    #            "highlight": {
    #                "border": colors[i],
    #                "background": 'white'
    #            }
    #        }

# legend
nw.add_node('fMRI', label='fMRI', shape="circularImage", value=40, borderWidth=2,
            image='citenet_brains/legend.svg', physics=False)
nw.get_node('fMRI')["color"] = {"border": col_fMRI[0], "background": 'white',
                                "highlight": {"border": col_fMRI[0], "background": 'white'}}
nw.get_node('fMRI')["x"] = -750
nw.get_node('fMRI')["y"] = 800
nw.get_node('fMRI')['font'] = {"size": 30}
nw.add_node('fNIRS', label='fNIRS', shape="circularImage", value=40, borderWidth=2,
            image='citenet_brains/legend.svg', physics=False)
nw.get_node('fNIRS')["color"] = {"border": col_fNIRS[0], "background": 'white',
                                "highlight": {"border": col_fNIRS[0], "background": 'white'}}
nw.get_node('fNIRS')["x"] = -640
nw.get_node('fNIRS')["y"] = 800
nw.get_node('fNIRS')['font'] = {"size": 30}
## save
nw.show('fig2B.html')

# %%


# Import Dependencies
import os
import csv

# Set path for file
budget_data = os.path.join("Resources", "budget_data.csv")
outputfile = os.path.join("Analysis" , "pybank.txt")

#read the data from the file
with open(budget_data, newline="", encoding="utf-8") as csvfile:
    csv_reader = csv.reader(csvfile, delimiter=",")
    csv_header = next(csvfile)
    #print(f" {csv_header}")

# find net amount of profit and loss
    Profit = []
    months = []

# read each row of data after header
    for rows in csv_reader:
            Profit.append(int(rows[1]))
            months.append(rows[0])

    # find revenue change
    average_change =[]

    for row in range(1, len(Profit)):
        average_change.append((int(Profit[row]) - int(Profit[row-1])))

   # calculate total length of months
    total_months = len(months)

    # calculate average revenue change
    revenue_average_change = sum(average_change) / len(average_change)
    revenue_average = round(revenue_average_change, 2)

    # greatest increase in revenue
    greatest_increase = max(average_change)
    #greatest decrease in revenue
    greatest_decrease = min(average_change)

    #greatest increse in month
    greatest_increase_month = months[average_change.index(max(average_change))+1]
    greatest_decrease_month = months[average_change.index(min(average_change))+1]

#output requireed
print("Financial Analysis")
print("-----------------------------------")
print("Total months: " + str(total_months))
print("Total: $" + str(sum(Profit)))
print("Average change: $" + str(revenue_average))
print(f"Greatest Increase in Profits: {str(greatest_increase_month)} $({str(greatest_increase)})")
print(f"Greatest Decrease in Profits: {str(greatest_decrease_month)} $({str(greatest_decrease)})")

with open (outputfile, "w") as file:
    file.write ("Financial Analysis \n")
    file.write("----------------------------------- \n")
    file.write("Total months: " + str(total_months) )
    file.write("\n Total: $" + str(sum(Profit)))
    file.write("\n Average change: $" + str(revenue_average))
    file.write(f"\nGreatest Increase in Profits: {str(greatest_increase_month)} $({str(greatest_increase)})")
    file.write(f"\nGreatest Decrease in Profits: {str(greatest_decrease_month)} $({str(greatest_decrease)})")


import sys
read=sys.stdin.readline

dp=[None]*101
def bottom_up(N):
    dp[1]=1
    dp[2]=1
    dp[3]=1
    dp[4]=2
    dp[5]=2

    if N>5:
        for n in range(6,N+1):
            dp[n]=dp[n-1]+dp[n-5]

    return dp[N]

case_num=int(read())

for _ in range(case_num):
    print(bottom_up(int(read())))


#finding a word from a text in file
def check(f_name, word):
    with open(f_name) as file:
        return any(word in line for line in file)

if check('Sample.txt', 'Python'):
    print("true")
else:
    print("false")


import os
import shutil
import tarfile
import urllib.request as request
from contextlib import closing
from glob import glob
from io import StringIO
from os.path import join

import numpy as np
import pandas as pd
from tqdm import tqdm

DST = "../pgs_data"
LIST = join(DST, "pgs_scores_list.txt")

# urls:
LIST_FILE = "http://ftp.ebi.ac.uk/pub/databases/spot/pgs/pgs_scores_list.txt"
BASE_URL = "http://ftp.ebi.ac.uk/pub/databases/spot/pgs/scores/"

# templates
SCORE_TEMPLATE = "{id}/ScoringFiles/{id}.txt.gz"
META_TEMPLATE = "{id}/Metadata/{id}_metadata.tar.gz"

def download_multiple_pgs(pgs_ids="all", dst=DST):
    download_ftp(LIST_FILE, LIST)
    all_ids = pd.read_csv(LIST, header=None).values.flatten()
    if pgs_ids == "all":
        dl_ids = all_ids
    else:
        dl_ids = np.intersect1d(all_ids, pgs_ids)
    for pgs_id in dl_ids:
        load_pgs(pgs_id, dst=dst)

def load_pgs(pgs_id, dst=DST):
    score_url = join(BASE_URL, SCORE_TEMPLATE.format(id=pgs_id))
    score_fn = join(dst, SCORE_TEMPLATE.format(id=pgs_id))
    download_ftp(score_url, score_fn)

    meta_url = join(BASE_URL, META_TEMPLATE.format(id=pgs_id))
    meta_fn = join(dst, META_TEMPLATE.format(id=pgs_id))
    download_ftp(meta_url, meta_fn)

def download_ftp(url, fn):
    dir = join(*fn.split("/")[:-1])
    os.makedirs(dir, exist_ok=True)
    print(f"downloading: {url} to {fn}")
    with closing(request.urlopen(url)) as r:
        with open(fn, "wb") as f:
            shutil.copyfileobj(r, f)

def list_traits(pgs_dir):
    files = sorted(glob(join(pgs_dir, "*.sscore")))
    pgs = [fn.split("/")[-1].split(".")[0] for fn in files]
    traits = []
    for i, p in tqdm(enumerate(pgs)):
        fn = join(DST, p, "Metadata", p + "_metadata.tar.gz")
        t = tarfile.open(fn, "r:gz")
        df = pd.read_csv(
            StringIO(t.extractfile(f"{p}_metadata_efo_traits.csv").read().decode())
        )
        trait = ", ".join(df["Ontology Trait Label"])

        traits.append({"pgs_id": p, "trait": trait, "index": i})

    return pd.DataFrame(traits)


import os
from gtts import gTTS
import tkinter as tk
from tkinter import Text, Button, Label, ttk
import threading

def criar_audio():
    texto = texto_entry.get("1.0", "end-1c")  # "1.0" indica o início do texto e "end-1c" é o final sem o caractere final de quebra de linha
    lingua = "pt"  # Defina o idioma desejado

    criar_audio_button.config(state="disabled")  # Desativa o botão enquanto o áudio está sendo criado
    progress_bar.start()  # Inicia a barra de progresso

    def criar_audio_thread():
        tts = gTTS(texto, lang=lingua)
        tts.save("audio.mp3")

        # Toca o áudio sem precisar abrir o arquivo
        os.system("ffplay -autoexit -nodisp audio.mp3")

        progress_bar.stop()  # Para a barra de progresso
        criar_audio_button.config(state="normal")  # Reativa o botão

    audio_thread = threading.Thread(target=criar_audio_thread)
    audio_thread.start()

# Cria uma janela principal
janela = tk.Tk()
janela.title("Texto Para Falar")

# Cria um rótulo explicativo
label = Label(janela, text="Digite o texto que você deseja transformar em áudio:")
label.pack()

# Cria um campo de texto para o usuário digitar o texto
texto_entry = Text(janela, height=5, width=40)
texto_entry.pack()

# Cria um botão para criar o áudio
criar_audio_button = Button(janela, text="Criar Áudio", command=criar_audio)
criar_audio_button.pack()

# Cria uma barra de progresso
progress_bar = ttk.Progressbar(janela, mode="indeterminate")
progress_bar.pack()

# Inicia a interface gráfica
janela.mainloop()


import os

import torch
from torch import nn
from torch.autograd import Variable
from torch.utils.data import DataLoader, random_split

import torch.nn.functional as F
import torch.optim as optim
import torchvision.utils as utils
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import torch.utils.data as data_utils
import numpy as np
import matplotlib.pyplot as plt

from model.vgg16 import VGG16

if __name__ == '__main__':

    # gpu 설정
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    # 모델 생성
    model = VGG16(_input_channel=1, num_class=10)
    model.to(device)

    criterion = nn.CrossEntropyLoss()

    # 테스트 할 모델 체크포인트 경로
    test_checkpoint = './checkpoints/robust5_checkpoint.pth'

    #------------------------------------------
    # 이전 체크포인트로부터 모델 로드
    print("start model load...")
    # 체크포인트 로드
    checkpoint = torch.load(test_checkpoint, map_location=device)

    # 각종 파라미터 로드
    model.load_state_dict(checkpoint['model'])
    batch_size = checkpoint['batch_size']

    print("model load end.")
    #------------------------------------------

    #------------------------------------------
    # MNIST dataset

    # 테스트 데이터셋 로드
    transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    #transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
    transforms.Normalize((0.5), (0.5))
    ])
    test_data = dsets.MNIST(root='data/', train=False, transform=transform, download=True)
    #test_data  = dsets.MNIST(root='data/', train=False, transform=transforms.ToTensor(), download=True)
    #test_data = data_utils.Subset(test_data, torch.arange(3000))
    # 데이터로더 생성
    test_loader = torch.utils.data.DataLoader(
                    dataset=test_data,
                    batch_size=batch_size,
                    shuffle=False)
    print(len(test_loader))
    #------------------------------------------

    test_loss = 0.0
    test_accuracy = 0.0
    adv_examples = []
    model.eval()
    with torch.no_grad():
        for x_b, target_b in test_loader:
            for index in range(batch_size):
                if index >= len(target_b):
                    break
                x = x_b[index].unsqueeze(dim = 0)
                target = target_b[index].unsqueeze(dim = 0)
                x = x.to(device)
                target = target.to(device)

                x.requires_grad = True

                out = model(x)

                # loss 계산
                loss = criterion(out, target)
                test_loss = test_loss + loss.item()

                # accuracy 계산
                output_label = torch.argmax(out, 1).to('cpu').numpy()
                target_label = target.to('cpu').numpy()

                correct_num = 0.0

                answer = target_label
                predict = output_label

                if answer == predict:
                    correct_num = correct_num + 1.0

                accuracy = correct_num / batch_size
                test_accuracy = test_accuracy + accuracy

        avg_test_loss = test_loss / len(test_loader)
        avg_test_accuracy = test_accuracy / len(test_loader)

        print('test loss : %f' % (avg_test_loss))
        print('test accuracy : %f' % (avg_test_accuracy))


"""
Basic string functional utilities.
"""

import re

def asciify(string):
	"""
	Removes non-ascii characters from the string.
	"""
	new_string = ''
	for c in string:
		if ((c == '\n') or (32 <= ord(c)) and (ord(c) <= 126)):
			new_string = new_string + c
		else:
			new_string = new_string + ' '
	return new_string

def detag(string):
	"""
	Remove words of type '&xxx;' from the string.
	"""
	lines = string.split('\n')
	new_lines = []
	for line in lines:
		words = line.split()
		new_words = []
		for word in words:
			word.strip()
			if ('&' in word and ';' in word):
				word1 = word.split('&')[0]
				if (len(word1) > 0):
					new_words.append(word1)
				word2 = word.split(';')[1]
				if (len(word2) > 0):
					new_words.append(word2)
			else:
				new_words.append(word)
		if (len(new_words) > 0):
			new_lines.append(' '.join(new_words))
	return '\n'.join(new_lines)

def replace_abbrv(string):
    """
    Replace abbrv like & -> and in the string.
    """
    return string.replace('&', 'and').replace('/', '-').replace('.', '. ').replace(',', ', ').replace('!', '! ').replace('?', '? ')

def depunctuate(string):
	"""
	Extract alphanumeric words from the string.
	"""
	lines = string.split('\n')
	new_lines = []
	for line in lines:
		words = re.findall("[\w',.!?-]+", line)
		if (len(words) > 0):
			new_lines.append(' '.join(words))
	return '\n'.join(new_lines)

def dedigit(string):
	"""
	Remove digits from the string.
	"""
	lines = string.split('\n')
	new_lines = []
	for line in lines:
		words = line.split()
		new_words = []
		for word in words:
			if (word.isdigit() or (re.search('[a-zA-Z]+', word) is None)):
				continue
			if (len(word) > 0):
				new_words.append(word)
		new_lines.append(' '.join(new_words))
	return '\n'.join(new_lines)

def clean(string):
	"""
	Cleans the string to have ascii characters and somewhat valid words.
	"""
	if (string in ['', None]):
		return ''
	string = string.strip()
	string = asciify(string)
	string = detag(string)
	string = replace_abbrv(string)
	string = depunctuate(string)
	string = dedigit(string)
	return string

def paragraphify(string):
	"""
	Forms appropriate paragraphs.
	"""
	lines = string.split('\n')
	new_lines = []
	new_line = ''
	for line in lines:
		new_line = new_line + ' ' + line
		new_line = new_line.strip()
		if (new_line.endswith('.') or new_line.endswith('!') or new_line.endswith('?')):
			# If it is proper end of sentence.
			new_lines.append(new_line)
			new_line = ''
	return '\n'.join(new_lines)


__author__ = 'Viveris Technologies'
__credits__ = '''Contributors:
 * Adrien THIBAUD <adrien.thibaud@toulouse.viveris.com>
 * David PRADAS <david.pradas@toulouse.viveris.com>
 * Mathias ETTINGER <mathias.ettinger@toulouse.viveris.com>
 * Joaquin MUGUERZA <joaquin.muguerza@toulouse.viveris.com>
'''

import os
import re
import sys
import syslog
import argparse
import traceback
import contextlib
import subprocess
from statistics import mean

import collect_agent

def command_line_flag_for_argument(argument, flag):
    if argument is not None:
        yield flag
        yield str(argument)

def main(destination_ip, count, interval, interface, packetsize, ttl, n_mean):
    cmd = ['fping', destination_ip, '-e', '-D']
    if count == 0:
        cmd += ['-l']
    else:
        cmd += ['-c', str(count)]
    cmd.extend(command_line_flag_for_argument(interval, '-p'))
    cmd.extend(command_line_flag_for_argument(interface, '-I'))
    cmd.extend(command_line_flag_for_argument(packetsize, '-b'))
    cmd.extend(command_line_flag_for_argument(ttl, '-t'))

    pattern = re.compile(r'\[(\d+\.\d+)\] {} : \[\d+\], \d+ bytes, (\d+\.?\d*) '.format(destination_ip))
    measurements = []

    # launch command
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    while True:
        # read output
        output = p.stdout.readline().decode().rstrip()
        if not output:
            # this should be the blank line before summary
            if p.poll() is not None:
                # Process ended gracefully
                break
            continue

        match = re.match(pattern, output)

        if match is None:
            message = 'Unrecognised fping output: {}'.format(output)
            collect_agent.send_log(syslog.LOG_WARNING, message)
            continue

        try:
            timestamp, rtt_data = map(float, match.groups())
        except ValueError as exception:
            message = 'ERROR on line \'{}\': {}'.format(output, exception)
            collect_agent.send_log(syslog.LOG_ERR, message)
        else:
            measurements.append(rtt_data)
            if len(measurements) == n_mean:
                collect_agent.send_stat(int(timestamp * 1000), rtt=mean(measurements))
                measurements.clear()

if __name__ == "__main__":
    with collect_agent.use_configuration('/opt/openbach/agent/jobs/fping/fping_rstats_filter.conf'):
        # Define Usage
        parser = argparse.ArgumentParser(
                description=__doc__,
                formatter_class=argparse.ArgumentDefaultsHelpFormatter)
        parser.add_argument('destination_ip', help='')
        parser.add_argument('-c', '--count', type=int, default=0, help='')
        parser.add_argument('-i', '--interval', type=int, help='')
        parser.add_argument('-I', '--interface', type=str, help='')
        parser.add_argument('-s', '--packetsize', type=int, help='')
        parser.add_argument('-t', '--ttl', type=int, help='')
        parser.add_argument('-m', '--mean', type=int, default=1, help='')

        # get args
        args = parser.parse_args()
        destination_ip = args.destination_ip
        count = args.count
        interval = args.interval
        interface = args.interface
        packetsize = args.packetsize
        ttl = args.ttl
        n_mean = args.mean

        main(destination_ip, count, interval, interface, packetsize, ttl, n_mean)


from BdayApp.serializers import ReminderSerializer
from BdayApp.models import UserProfile, BdayAppUser, Reminder
from BdayApp.UserProfileView import UserProfileView
from rest_framework import generics
from rest_framework import response
from rest_framework import mixins, status, exceptions, filters
import logging
import json

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

class ReminderListView(generics.ListAPIView,generics.RetrieveUpdateDestroyAPIView):

    model = Reminder
    serializer_class = ReminderSerializer
    filter_backends = (filters.SearchFilter,)
    search_fields = ('id', 'name', 'creation_date', 'reminder_date')

    @staticmethod
    def handle_error(e):
        content = dict()
        logger.error(e)
        raise exceptions.APIException(e)

    def get_queryset(self):
        access_token = self.kwargs['access_token']
        try:
            user = UserProfileView.get_profile(access_token).user
            reminder_list = Reminder.objects.filter(user=user)
            return reminder_list
        except Reminder.DoesNotExist:
            raise exceptions.NotFound("reminder list not found")
        except Exception as e:
            ReminderListView.handle_error(e)


"""
124.二叉树中的最大路径和
给定一个非空二叉树，返回其最大路径和。
本题中，路径被定义为一条从树中任意节点出发，达到任意节点的序列。该路径至少包含一个节点，且不一定经过根节点。
示例 1:
输入: [1,2,3]

       1
      / \
     2   3

输出: 6

示例 2:
输入: [-10,9,20,null,null,15,7]

   -10
   / \
  9  20
    /  \
   15   7

输出: 42
"""
# 这题晃着一看，觉得好难，实际它对我来说也确实好难，后来思考了一下，是否可以考虑把这个二叉树考虑成图来做呢
# 一个连通图，然后还没有环，目的就是找到图中最大路径和，找最大路径和又是用的什么方法呢？
# BFS？ DFS？ 拓扑序列？ 最后都想不出来，我哭了。。。

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def __init__(self):
        self.ans = float("-inf")

    def maxGain(self, root: TreeNode) -> int:

        if not root:
            return 0
        leftsum = self.maxGain(root.left)
        rightsum = self.maxGain(root.right)
        cur_sum = root.val + leftsum + rightsum
        self.ans = max(self.ans, cur_sum)
        return max(root.val, root.val+max(leftsum, rightsum))

    def maxPathSum(self, root: TreeNode) -> int:
        cur_ans = self.maxGain(root)
        return max(cur_ans, self.ans)

solu = Solution()
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)

ans = solu.maxPathSum(root)
print(ans)

"""
看了题解之后，记录一下思路点吧：
先不讨论题解哪里用到了DFS，直接分析题意。
这道题主要解题点在于以节点 i 为根的路径和是怎么来的，假设以节点 i 为根的路径（注意路径的概念，只能是 i 的左边加 i 或者 i 的右边加 i ）
向其父节点能贡献的值可以由函数 f(i) 得到，可以得到 i 为根的路径和最大值为：
root.val + Math.max(f(root.left), 0) + Math.max(f(root.right), 0)
毕竟由 i 的左右节点贡献的值有可能是负数，所以要排除负数的影响。
再来讨论 f(i) 本身，这个函数的返回值应该是：
f(i) = Math.max(Math.max(f(i.left), f(i.right)), 0) + i.val;
同样的，先排除i的左右子节点贡献负值的可能性，然后选其中的较大值，加上本节点的值就是本节点能向其父节点贡献的值。
上面这两段话其实就是官方题解的最核心部分，以后看到类似这种路径和最大、路径和为某值的题往这种思路上套就奥利给了。

这种题的思路就是两条线：一条线是以该节点为根节点来构造一个路径和，将它作为最终路径来计算路径和，也就是根节点加上左右节点的贡献
还有一条线就是该节点并不是最高层的节点，而是要贡献给该节点的父节点去，那么这个时候，就不能都选择该节点的左右子节点了。
"""


import dash_bootstrap_components as dbc
from dash import Input, Output, State, html
import dash
from dash import html, dcc

from templates import *

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
# app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
PLOTLY_LOGO = "https://images.plot.ly/logo/new-branding/plotly-logomark.png"

def dec(func):
    def wrap(*args, **kwargs):
        print(args, kwargs)
        return func(*args, **kwargs)
    return wrap

# ____________________________________________________________________________________________________

app.layout = main

@app.callback(
    Output("offcanvas", "is_open"),
    Input("button-item-1", "n_clicks"),
    Input("open-offcanvas", "n_clicks"),
    [State("offcanvas", "is_open")],
)
@dec
def toggle_offcanvas1(n1, n2, is_open):
    ctx = dash.callback_context
    dropdown_id = ctx.triggered[0]['prop_id'].split('.')[0]
    print(dropdown_id)
    if dropdown_id == "button-item-1":
        return False
    return True

@app.callback(Output('main-table', 'data'),
              [Input('button-item-2', 'n_clicks'), Input('button-item-3', 'n_clicks')])
@dec
def update_graphs(n2, n3):
    ctx = dash.callback_context
    dropdown_id = ctx.triggered[0]['prop_id'].split('.')[0]

    d2 = [{"name": "sdf", "id": "sdfsf"},
         {"name": "sdf", "id": "sdfsf"},
         {"name": "sdf", "id": "sdfsf"},
         {"name": "sdf", "id": "sdfsf"}, ]

    d3 = [{"name": "sdf", "id": "sdfsf"}, ]

    if dropdown_id == "button-item-2":
        return d2
    elif dropdown_id == "button-item-3":
        return d3
    return [{"name": "", "id": ""}, ]

# @app.callback(
#     [Output('spent-money', 'children'),
#     Output('have-money', 'children'),
#     Output('can-spend', 'children')],
#     [Input('button-item-1', 'n_clicks')]
# )
# def calculate_spending(n):
#     ctx = dash.callback_context
#     dropdown_id = ctx.triggered[0]['prop_id'].split('.')[0]
#     if dropdown_id == "button-item-1":
#         return '1', '2', '3'

@app.callback(
    [Output('content', 'children')],
    [Input('button-item-1', 'n_clicks'),
    Input('button-item-2', 'n_clicks')
     ],
)
@dec
def change_page(n1, n2):
    ctx = dash.callback_context
    button_id = ctx.triggered[0]['prop_id'].split('.')[0]

    if button_id == "button-item-1":
        return (table2,)
    elif button_id == "button-item-2":
        return ('TEST BUTT 2',)
    return ('sdf',)

# ____________________________________________________________________________________________________

if __name__ == '__main__':
    app.run_server(debug=False)