import argparse
from lstm import text_LSTM

#%%

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='This A.I. Rapper Needs Training')
    parser.add_argument('artist_name', metavar='artist', type=str,
                        help='Artist to be mimicked. If you are hesitating, type "박재범" or "염따".')
    parser.add_argument('--epochs', type=int, default=3, required=False,
                        help='Train epochs')
    args = parser.parse_args()

    t1 = text_LSTM(args.artist_name)
    t1.func_make_structure()
    t1.func_train(args.epochs)


# coding: utf-8
from bs4 import BeautifulSoup
import requests

r = requests.get('http://www.caltrain.com/schedules/weekdaytimetable.html')
soup = BeautifulSoup(r.text, 'html.parser')
nbtt = soup.select('.NB_TT')
nbtt = nbtt[0]
rows = nbtt.select('tr')
def get_time_pairs_for_table(table):
    rows = table.select('tr')
    for r in rows:
        if "Mountain View" in str(r):
            mtview = r
        if "Diridon" in str(r):
            diridon = r
    mtview_times = mtview.select("td")
    diridon_times = diridon.select("td")
    assert len(mtview_times) == len(diridon_times)
    mtview_time_strs = [i.contents[0] for i in mtview_times if len(i.contents) > 0]
    diridon_time_strs = [i.contents[0] for i in diridon_times if len(i.contents) > 0]
    assert len(mtview_time_strs) == len(diridon_time_strs)
    time_pairs = list(zip(diridon_time_strs, mtview_time_strs))
    time_pairs_trimmed = [x for x in time_pairs if ':' in x[0] and ':' in x[1]]
    return time_pairs_trimmed

nb_pairs = get_time_pairs_for_table(soup.select('.NB_TT')[0])
sb_pairs = get_time_pairs_for_table(soup.select('.SB_TT')[0])
print("NORTH")
print(nb_pairs)
print("SOUTH")
print(sb_pairs)


import os
import os.path as osp
from copy import deepcopy
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from core.base import Base
from torch.utils.data import DataLoader, Subset
from utils.interact.log import Log, log
from .Defense import Defense
from tqdm import trange
from utils import get_latent_rep
from utils import BeingRobust
from utils import robust_estimation
import torchvision
class Spectre(Base,Defense):
    """Filtering the training samples spectral signatures (Spectral).
    Args:
        task(dict):The defense strategy is used for the task, refrence core.Base.Base.
        schedule=None(dict): Config related to model training,refrence core.Base.Base.
        defense_schedule(dict): Parameters related to defense tasks.
    """

    def __init__(self, task=None, defense_schedule=None):
        if task is not None:
            Base.__init__(self, task=task, schedule=defense_schedule['schedule'])
        else:
            Base.__init__(self)

        Defense.__init__(self)

        self.global_defense_schedule = defense_schedule

    def get_defense_strategy(self):
        return self.global_defense_schedule["defense_strategy"]

    def repair(self, model=None, dataset=None, schedule=None):
        """Perform Spectral defense method based on attacked dataset and retrain with the filtered dataset.
        The repaired model will be stored in self.model

        Args:
            schedule (dict): Schedule for Spectral. Contraining sub-schedule for pre-isolatoin training, clean training, unlearning and test phrase.
            transform (classes in torchvison.transforms): Transform for poisoned trainset in filter phrase
        """
        assert dataset is not None, "dataset is None"

        if model is None:
            model = self.model

        if schedule is not None:
            current_schedule = deepcopy(schedule)
        elif self.global_defense_schedule is not None:
            # print(self.global_defense_schedule)
            current_schedule = deepcopy(self.global_defense_schedule)
        else:
            raise AttributeError("Training schedule is None, please check your schedule setting.")

        # filter before repair
        if current_schedule["repair"]["filter"]:
            msg = "==========Start filtering before repair==========\n"
            log(msg)
            removed_indices, left_indices = self.filter(dataset=dataset, schedule=current_schedule)
            dataset = Subset(dataset, left_indices)
            log(f'The number of Samples with label:{current_schedule["filter"]["y_target"]} is {len(removed_indices) + len(left_indices)}, removed:{len(removed_indices)}, left:{len(left_indices)}\n')

        # Train the model with filtered dataset
        msg = "==========Train the model with filtered dataset==========\n"
        log(msg)
        self.init_model()
        self.train(dataset, current_schedule['schedule'])

    def _get_latents(self, model=None, dataset=None, schedule=None):
        device = schedule["filter"]["device"]
        model = model.to(device)
        y_target = schedule["filter"]["y_target"]

        # dataset_y = []
        # for i in range(len(dataset)):
        #     dataset_y.append(dataset[i][1])

        # cur_indices = [i for i,v in enumerate(dataset_y) if v==y_target]
        # benign_id = [i for j,v in enumerate(dataset_y) if v!=y_target]

        dataset_y = dataset.get_modified_targets()
        cur_indices = np.where(dataset_y == y_target)[0]
        msg = f'Target label {y_target}, Number of samples with  label {y_target} is {len(cur_indices)}\n'
        log(msg)

        model.eval()
        msg = "==========Start filtering poisoned dataset==========\n"
        log(msg)
        sub_dataset = Subset(dataset,cur_indices)
        latents, _ = get_latent_rep(model, schedule["filter"]['layer'], sub_dataset, device=device)
        return cur_indices, latents

    def target_label_identifier(self, latent_feats, class_indices, max_k, alpha, varepsilon):
        """
        The idea is that the mean QUE scores obtained for the target label are clearly larger (for appropriate values of effective dimension k)
        than those obtained for untargeted labels
        """
        num_classes = len(class_indices)
        target_label  = None
        best_k = None
        max_q = 0.0
        best_to_be_removed = None
        for i in range(num_classes):
            if len(class_indices[i]) > 1:
                latents = latent_feats[class_indices[i]]
                k, q, removed = self.k_identifier(latents, max_k, alpha, varepsilon)
                if q > max_q:
                    max_q = q
                    target_label = i
                    best_k = k
                    best_to_be_removed = removed
        return target_label, best_k, best_to_be_removed

    def k_identifier(self, latents, max_k, alpha, varepsilon):
        """
        The idea is that if poisons were correctly identified,  then the mean QUE score will be large as poisons have strong spectral signature.
        """
        # n * m
        feat_deviation = latents - latents.mean(dim=0) # centered data
        # m * m
        U, _, _ = torch.svd(feat_deviation.T)
        # m * max_k
        U = U[:, :max_k]
        max_q = 0.0
        best_n_dim = None
        best_to_be_removed = None
        for n_dim in range(2, max_k):
            S_removed, S_left,_ = self.SPECTRE(latents, n_dim, alpha, varepsilon)
            # len(S_left) * m
            left_feats = latents[S_left]
            # max_k*max_k
            # covariance = torch.cov(torch.matmul(left_feats, U))
            # L, V = torch.linalg.eig(covariance)
            # L, V = L.real, V.real
            # L = (torch.diag(L) ** (1 / 2) + 0.001).inverse()
            # # max_k*max_k
            # normalizer = torch.matmul(V, torch.matmul(L, V.T))

            # # n*m *  m*max_k --> n*max_k
            # projected_feats = torch.matmul(latents, U)
            # # n*max_k *  max_k*max_k --> n*max_k
            # whitened_feats = torch.matmul(projected_feats, normalizer)

            whitened_feats = self.whiten(torch.matmul(latents, U), oracle_clean_feats=torch.matmul(left_feats, U))
            q = self.QUEscore(whitened_feats, alpha=alpha).mean()
            if q > max_q:
                max_q = q
                best_n_dim = n_dim
                best_to_be_removed = S_removed

            return  best_n_dim, max_q, best_to_be_removed

    def SPECTRE(self, latents, n_dim, alpha, varepsilon):
        # n * m
        feat_deviation = latents - latents.mean(dim=0) # centered data
        # m * m
        U, _, _ = torch.svd(feat_deviation.T)
        # m * n_dim
        U = U[:, :n_dim]
        # print(f"latents:{latents.size()}, U:{U.size()}\n")
        # n*m *  m*n_dim --> n*n_dim
        projected_feats = torch.matmul(latents, U)
        # n*n_dim
        whitened_feats = self.whiten(projected_feats, oracle_clean_feats=None)
        taus = self.QUEscore(whitened_feats, alpha = alpha)

        p_score = np.percentile(taus, 1.5 * varepsilon)

        top_scores = np.where(taus > p_score)[0]
        S_removed = np.copy(top_scores)
        S_left = np.delete(range(len(latents)),S_removed)

        return S_removed, S_left, p_score

    # filter criteria
    def QUEscore(sefl, temp_feats, alpha = 4.0):
        n_samples = temp_feats.shape[0]
        n_dim = temp_feats.shape[1]
        temp_feats = temp_feats - temp_feats.mean(dim=0)
        Sigma = torch.matmul(temp_feats, temp_feats.T) / (n_samples - 1)
        I = torch.eye(n_dim).cuda()
        Q = torch.exp((alpha * (Sigma - I)) / (torch.linalg.norm(Sigma, ord=2) - 1))
        trace_Q = torch.trace(Q)

        taus = []
        for i in range(n_samples):
            h_i = temp_feats[i]
            tau_i = torch.matmul(h_i.T, torch.matmul(Q, h_i)) / trace_Q
            tau_i = tau_i.item()
            taus.append(tau_i)
        taus = np.array(taus)
        return taus

    def whiten(self, temp_feats, oracle_clean_feats=None):
        # whiten the data
        if oracle_clean_feats is None:
            estimator = robust_estimation.BeingRobust(random_state=0, keep_filtered=True).fit((temp_feats.T).cpu().numpy())
            clean_mean = torch.FloatTensor(estimator.location_).cuda()
            filtered_feats = (torch.FloatTensor(estimator.filtered_).cuda() - clean_mean).T
            # clean_covariance = torch.cov(filtered_feats)
            clean_covariance = torch.mm(filtered_feats, filtered_feats.t()) / (filtered_feats.size(0) - 1)
        else:
            clean_feats = oracle_clean_feats
            clean_mean = torch.FloatTensor(clean_feats.mean(dim = 1)).cuda()
            filtered_feats = (torch.FloatTensor(clean_feats).cuda() - clean_mean).T
            # clean_covariance = torch.cov(clean_feats)
            clean_covariance = torch.mm(filtered_feats, filtered_feats.t()) / (filtered_feats.size(0) - 1)
        temp_feats = temp_feats.cuda()
        temp_feats = (temp_feats.T - clean_mean).T
        #clean_covariance ^ (-1/2)
        L, V = torch.eig(clean_covariance,eigenvectors = True)
        # L, V = torch.linalg.eig(clean_covariance)
        L, V = L.real, V.real
        # L = L[:, 0]
        # print(f"L:{L},V:{V}\n")
        # print(torch.diag(L)**(1/2)+0.001)
        L = (torch.diag(L)**(1/2)+0.001).inverse()
        normalizer = torch.matmul(V, torch.matmul( L, V.T ) )
        temp_feats = torch.matmul(normalizer, temp_feats)
        return temp_feats

    def filter_via_spectre(self, latents=None, n_dim=512, alpha=4.0, varepsilon=0.01):
        log(f"The shape of the 'latents' matrix is {latents.shape}\n")
        if isinstance(latents, np.ndarray):
            latents = torchvision.transforms.ToTensor()(latents)
        if latents.dim() > 2:
            latents = latents.squeeze()
        S_removed, S_left, p_score = self.SPECTRE(latents, n_dim, alpha, varepsilon)
        return S_removed, S_left, p_score

    def filter(self, model=None, dataset=None, schedule=None):
        """
        filter out poisoned samples from dataset with label y_target.
        Args:
            dataset (torch.utils.data.Dataset): The dataset to filter.
            schedule (dict): defense schedule filteringing the dataset.
        """
        if schedule is not None:
            current_schedule = deepcopy(schedule)
        elif self.global_defense_schedule is not None:
            current_schedule = deepcopy(self.global_defense_schedule)
        else:
            raise AttributeError("Training schedule is None, please check your schedule setting.")

        if current_schedule['filter']['train']:
            # train the model with poisoned dataset
            msg = "==========Start training with poisoned dataset==========\n"
            log(msg)
            log('the length of poisoned dataset:{0}.\n'.format(len(dataset)))
            self.train(dataset, current_schedule["schedule"])
            model = deepcopy(self.get_model())
        else:
            assert model is not None, "model is None"

        cur_indices, latents = self._get_latents(model=model, dataset=dataset, schedule=current_schedule)

        removed_inds, _, p_score = self.filter_via_spectre(latents=latents, n_dim=current_schedule["filter"]["n_dim"], alpha=current_schedule["filter"]["alpha"], varepsilon=current_schedule["filter"]["varepsilon"])
        ### c. detect the backdoor data by the SVD decomposition
        re = [cur_indices[v] for i,v in enumerate(removed_inds)]
        # print(f"cur_indices:{cur_indices}, latents:{len(latents)}")
        left_inds = np.delete(range(len(dataset)), re)

        log(f'The value at {current_schedule["filter"]["percentile"]} of the Scores list is {p_score}\n')
        log(f'The number of Samples with label:{current_schedule["filter"]["y_target"]} is {len(cur_indices)}, removed:{len(re)}, left:{len(left_inds)}\n')

        return re, left_inds

    def filter_with_latents(self, latents_path=None, schedule=None):

        if schedule is not None:
            current_schedule = deepcopy(schedule)
        elif self.global_defense_schedule is not None:
            current_schedule = deepcopy(self.global_defense_schedule)
        else:
            raise AttributeError("Training schedule is None, please check your schedule setting.")

        assert latents_path is not None, "the 'latents_path' must be not None, when dataset is not specified"
        data = np.load(latents_path)
        latents, y_labels = data["latents"], data["y_labels"]
        cur_indices = np.where(y_labels == current_schedule["filter"]["y_target"])[0]
        latents = latents[cur_indices]
        removed_inds, _, p_score = self.filter_via_spectre(latents=latents, n_dim=current_schedule["filter"]["n_dim"], alpha=current_schedule["filter"]["alpha"], varepsilon=current_schedule["filter"]["varepsilon"])
        print(f"removed_inds:{len(removed_inds)}")
        ### c. detect the backdoor data by the SVD decomposition
        re = [cur_indices[v] for i,v in enumerate(removed_inds)]
        left_inds = np.delete(range(len(data["latents"])), re)

        log(f'The value at {0} of the Scores list is {p_score}\n')
        log(f'The number of Samples with label:{current_schedule["filter"]["y_target"]} is {len(cur_indices)}, removed:{len(re)}, left:{len(left_inds)}\n')

        return re, left_inds


"""
Email backends.
"""

from django.core.mail.backends.base import BaseEmailBackend

from django_yubin import queue_email_message

class QueuedEmailBackend(BaseEmailBackend):
    """
    A wrapper that manages a queued SMTP system.
    """

    def send_messages(self, email_messages):
        """
        Add new messages to the email queue.

        The ``email_messages`` argument should be one or more instances
        of Django's core mail ``EmailMessage`` class.
        """
        num_sent = 0
        for email_message in email_messages:
            num_sent += queue_email_message(email_message)
        return num_sent


import random
from typing import Union
from typing import TYPE_CHECKING

from loguru import logger

import config
from modules.base import ModuleBase
from contracts.stark_bridge.main import StarkBridgeContracts
from contracts.base import TokenBase
from src.schemas.action_models import ModuleExecutionResult, TransactionPayloadData

if TYPE_CHECKING:
    from src.schemas.tasks.stark_bridge import StarkBridgeTask
    from src.schemas.wallet_data import WalletData

class StarkBridge(ModuleBase):
    task: 'StarkBridgeTask'

    def __init__(
            self,
            account,
            task: 'StarkBridgeTask',
            wallet_data: 'WalletData'
    ):
        super().__init__(
            account=account,
            task=task,
        )

        self.task = task
        self.wallet_data = wallet_data
        self.coin_x = self.tokens.get_by_name('ETH')

        self.stark_bridge_contracts = StarkBridgeContracts()
        self.router_contract = self.get_contract(
            address=self.stark_bridge_contracts.router_address,
            abi=self.stark_bridge_contracts.router_abi,
            provider=account
        )

        self.initial_balance_x_wei = None
        self.token_x_decimals = 18

    async def calculate_amount_out_from_balance(
            self,
            coin_x: TokenBase
    ) -> Union[int, None]:
        """
        Returns random amount out of token x balance.
        :param coin_x:
        :return:
        """
        balance_x_wei = await self.get_token_balance(
            token_address=self.coin_x.contract_address,
            account=self.account
        )
        token_x_decimals = self.token_x_decimals

        if balance_x_wei == 0:
            logger.error(f"Wallet {coin_x.symbol.upper()} balance = 0")
            return None

        wallet_token_x_balance_decimals = balance_x_wei / 10 ** token_x_decimals

        if self.task.use_all_balance is True:
            amount_out_wei = balance_x_wei

        elif self.task.send_percent_balance is True:
            percent = random.randint(
                int(self.task.min_amount_out), int(self.task.max_amount_out)
            ) / 100
            amount_out_wei = int(balance_x_wei * percent)

        elif wallet_token_x_balance_decimals < self.task.min_amount_out:
            logger.error(
                f"Wallet {coin_x.symbol.upper()} balance less than min amount out, "
                f"balance: {wallet_token_x_balance_decimals}, min amount out: {self.task.min_amount_out}"
            )
            return None

        elif wallet_token_x_balance_decimals < self.task.max_amount_out:
            amount_out_wei = self.get_random_amount_out_of_token(
                min_amount=self.task.min_amount_out,
                max_amount=wallet_token_x_balance_decimals,
                decimals=token_x_decimals
            )

        else:
            amount_out_wei = self.get_random_amount_out_of_token(
                min_amount=self.task.min_amount_out,
                max_amount=self.task.max_amount_out,
                decimals=token_x_decimals
            )

        self.initial_balance_x_wei = balance_x_wei

        return amount_out_wei

    async def build_txn_payload_data(self) -> Union[TransactionPayloadData, None]:
        """
        Builds transaction payload data
        :return:
        """

        if self.wallet_data.pair_address is None:
            logger.error(f"Pair EVM address not set")
            return None

        if len(self.wallet_data.pair_address) != config.EVM_ADDRESS_LENGTH:
            logger.error(f"Pair EVM address is not valid, should be {config.EVM_ADDRESS_LENGTH} chars length")
            return None

        amount_out_wei = await self.calculate_amount_out_from_balance(self.coin_x)
        if not amount_out_wei:
            return None

        bridge_call = self.build_call(
            to_addr=self.router_contract.address,
            func_name='initiate_withdraw',
            call_data=[
                self.i16(self.wallet_data.pair_address),
                amount_out_wei,
                0
            ]
        )

        return TransactionPayloadData(
            calls=[bridge_call],
            amount_x_decimals=amount_out_wei / 10 ** self.token_x_decimals,
            amount_y_decimals=0,
        )

    async def send_txn(self) -> ModuleExecutionResult:
        """
        Sends transaction.
        :return:
        """
        txn_payload_data = await self.build_txn_payload_data()
        if txn_payload_data is None:
            self.module_execution_result.execution_info = f"Failed to build txn payload data"
            return self.module_execution_result

        txn_info_message = (
            f"Bridge - Stark → ETH L1 (StarkGate) of {round(txn_payload_data.amount_x_decimals, 4)} ETH, "
            f"recipient: {self.wallet_data.pair_address}"
        )

        txn_status = await self.simulate_and_send_transfer_type_transaction(
            account=self.account,
            calls=txn_payload_data.calls,
            txn_info_message=txn_info_message
        )

        return txn_status


import os
from pathlib import Path

import yaml

try:
    from yaml import CLoader as Loader
except ImportError:
    from yaml import Loader

class ConfigurationError(Exception):
    pass

class Configs:
    def __init__(self):
        try:
            self.cache_dir = os.environ["COURT_SCRAPER_DIR"]
        except KeyError:
            self.cache_dir = str(
                Path(os.path.expanduser("~")).joinpath(".court-scraper")
            )
        self.config_file_path = str(Path(self.cache_dir).joinpath("config.yaml"))
        self.db_path = str(Path(self.cache_dir).joinpath("cases.db"))

    @property
    def captcha_service_api_key(self):
        with open(self.config_file_path) as fh:
            configs = yaml.load(fh, Loader=Loader)
            try:
                return configs["captcha_service_api_key"]
            except KeyError:
                msg = f"Set captcha_service_api_key in {self.config_file_path}"
                raise ConfigurationError(msg)


"""
    dqm gui start:
        starts the offline DQM GUI on port 8060

    Usage:
        dqm gui start

"""
import logging
import os
import hepshell
from hepshell.interpreter import time_function

LOG = logging.getLogger(__name__)

from l1t_cli.commands.dqm.gui.setup import DQM_GUI_PATH

class Command(hepshell.Command):

    def __init__(self, path=__file__, doc=__doc__):
        super(Command, self).__init__(path, doc)

    @time_function('dqm gui start', LOG)
    def run(self, args, variables):
        self.__prepare(args, variables)

        commands = [
            'cd {DQM_GUI_PATH}',
            'source current/apps/dqmgui/128/etc/profile.d/env.sh',
            '$PWD/current/config/dqmgui/manage -f dev start "I did read documentation"'
        ]

        all_in_one = ' && '.join(commands)
        all_in_one = all_in_one.format(DQM_GUI_PATH = DQM_GUI_PATH)

        from hepshell.interpreter import call
        code, _, stderr = call(all_in_one, logger=LOG, shell = True)
        if not code == 0:
            msg = 'Could not start DQM GUI: {0}'.format(stderr)
            LOG.error(msg)
            return False
        self.__text = 'DQM GUI now available at http://localhost:8060/dqm/dev'

        return True

    def __can_run(self):
        # if DQMOffline exists
        return True


"""
1296. Divide Array in Sets of K Consecutive Numbers
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.

Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].

Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].

Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true

Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.

Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length
Note: This question is the same as 846: https://leetcode.com/problems/hand-of-straights/
"""
# Time complexity: O(N log N)
# Space complexity: O(N)
class Solution:
    def isPossibleDivide(self, nums: List[int], k: int) -> bool:
        if len(nums)%k != 0:
            return False
        count = collections.Counter(nums)
        for n in sorted(count):
            if count[n] > 0:
                to_subtract = count[n]
                for i in range(n, n+k):
                    if count[i] < to_subtract:
                        return False
                    count[i] -= to_subtract
        return True

Leetcode/1296. Divide Array in Sets of K Consecutive Numbers.py


import os
import telebot
from flask import Flask, request
import random

TOKEN = '1668076384:AAF4aDoDF6_KqaADNtgGjUIdgykdVAM4qtI'
APP_NAME = 'https://error-bot-check.herokuapp.com/'

bot = telebot.TeleBot(TOKEN)

server = Flask(__name__)

testGroup_withOlenka = -579324010
test_group = -1001153348142

@bot.message_handler(commands=['start'])
def send_welcome(message):
    # mes = get_messages()
    mes = 'some text'
    bot.send_message(message.from_user.id, mes)
    # bot.send_message(message.from_user.id, mes)

@bot.message_handler(commands=['send'])
def send_to_group(message):
    mes = 'my message'
    bot.send_message(testGroup_withOlenka, mes)

@bot.message_handler(content_types=["text"])
def repeat_all_messages(message):
    for_message = ['пока я туповат и никуя не понимаю', 'я учусь', 'отстань от меня человек', 'ой, всё']
    bot_message_random = random.randrange(0, len(for_message))
    bot_mes = for_message[bot_message_random].capitalize()
    bot.send_message(message.chat.id, bot_mes)

#
# def get_messages():
#     # Функция использует метод getUpdates и возвращает массив объектов
#     result = requests.get(f'https://api.telegram.org/bot{TOKEN}/getUpdates?offset=10').json()
#     return result['result']

# def set_message(chat_id, text):
#     # Функция использует метод sendMessage для отправки текстовых сообщений
#     requests.get(f'https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={text}')
#
# def check_message(message):
#     # Проверяем текст сообщения отправленное боту
#     if message.lower() in ['привет', 'hello']:
#         return 'Привет :)'
#     else:
#         return 'Я не понимаю('
#
# def run():
#     update_id = get_messages()[-1]['update_id'] # Присваиваем ID последнего отправленного сообщения боту
#     while True:
#         time.sleep(1)
#         messages = get_messages() # Получаем обновления
#         for message in messages:
#             # Если в обновлении есть ID больше чем ID последнего сообщения, значит пришло новое сообщение
#             if update_id < message['update_id']:
#                 update_id = message['update_id'] # Присваиваем ID последнего отправленного сообщения боту
#                 # Отвечаем тому кто прислал сообщение боту
#                 set_message(message['message']['chat']['id'], check_message(message['message']['text']))
#

@server.route('/' + TOKEN, methods=['POST'])
def getMessage():
    json_string = request.get_data().decode('utf-8')
    update = telebot.types.Update.de_json(json_string)
    bot.process_new_updates([update])
    return "it works", 200

@server.route("/")
def webhook():
    bot.remove_webhook()
    bot.set_webhook(url=APP_NAME + TOKEN)
    return "it worksssssssss", 200

if __name__ == "__main__":
    server.run(host="0.0.0.0", port=int(os.environ.get('PORT', 5000)))


import numpy as np
import pandas as pd
import random
import tqdm
import torch
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
import os

# 全局参数，随机种子，图像尺寸
seed = 114514
np.random.seed(seed)
random.seed(seed)
BATCH_SIZE = 512

hidden_dim = 16
epochs = 1
device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
print(device)

df = pd.read_csv('train_dataset.csv')
print('共{}个用户，{}本图书，{}条记录'.format(max(df['user_id']) + 1, max(df['item_id']) + 1, len(df)))

class Goodbooks(Dataset):
    def __init__(self, df, mode='training', negs=99):
        super().__init__()

        self.df = df
        self.mode = mode

        self.book_nums = max(df['item_id']) + 1
        self.user_nums = max(df['user_id']) + 1

        self._init_dataset()

    def _init_dataset(self):
        self.Xs = []

        self.user_book_map = {}
        for i in range(self.user_nums):
            self.user_book_map[i] = []

        for index, row in self.df.iterrows():
            user_id, book_id = row
            self.user_book_map[user_id].append(book_id)

        if self.mode == 'training':
            for user, items in tqdm.tqdm(self.user_book_map.items()):
                for item in items[:-1]:
                    self.Xs.append((user, item, 1))
                    for _ in range(3):
                        while True:
                            neg_sample = random.randint(0, self.book_nums - 1)
                            if neg_sample not in self.user_book_map[user]:
                                self.Xs.append((user, neg_sample, 0))
                                break

        elif self.mode == 'validation':
            for user, items in tqdm.tqdm(self.user_book_map.items()):
                if len(items) == 0:
                    continue
                self.Xs.append((user, items[-1]))

    def __getitem__(self, index):

        if self.mode == 'training':
            user_id, book_id, label = self.Xs[index]
            return user_id, book_id, label
        elif self.mode == 'validation':
            user_id, book_id = self.Xs[index]

            negs = list(random.sample(
                list(set(range(self.book_nums)) - set(self.user_book_map[user_id])),
                k=99
            ))
            return user_id, book_id, torch.LongTensor(negs)

    def __len__(self):
        return len(self.Xs)

# 建立训练和验证dataloader
traindataset = Goodbooks(df, 'training')
validdataset = Goodbooks(df, 'validation')

trainloader = DataLoader(traindataset, batch_size=BATCH_SIZE, shuffle=True, drop_last=False, num_workers=0)
validloader = DataLoader(validdataset, batch_size=BATCH_SIZE, shuffle=True, drop_last=False, num_workers=0)

# 构建模型
class NCFModel(torch.nn.Module):
    def __init__(self, hidden_dim, user_num, item_num, mlp_layer_num=4, weight_decay=1e-5, dropout=0.5):
        super().__init__()
        self.hidden_dim = hidden_dim
        self.user_num = user_num
        self.item_num = item_num
        self.mlp_layer_num = mlp_layer_num
        self.weight_decay = weight_decay
        self.dropout = dropout

        self.mlp_user_embedding = torch.nn.Embedding(user_num, hidden_dim * (2 ** (self.mlp_layer_num - 1)))
        self.mlp_item_embedding = torch.nn.Embedding(item_num, hidden_dim * (2 ** (self.mlp_layer_num - 1)))

        self.gmf_user_embedding = torch.nn.Embedding(user_num, hidden_dim)
        self.gmf_item_embedding = torch.nn.Embedding(item_num, hidden_dim)

        mlp_Layers = []
        input_size = int(hidden_dim * (2 ** (self.mlp_layer_num)))
        for i in range(self.mlp_layer_num):
            mlp_Layers.append(torch.nn.Linear(int(input_size), int(input_size / 2)))
            mlp_Layers.append(torch.nn.Dropout(self.dropout))
            mlp_Layers.append(torch.nn.ReLU())
            input_size /= 2
        self.mlp_layers = torch.nn.Sequential(*mlp_Layers)

        self.output_layer = torch.nn.Linear(2 * self.hidden_dim, 1)

    def forward(self, user, item):
        user_gmf_embedding = self.gmf_user_embedding(user)
        item_gmf_embedding = self.gmf_item_embedding(item)

        user_mlp_embedding = self.mlp_user_embedding(user)
        item_mlp_embedding = self.mlp_item_embedding(item)

        gmf_output = user_gmf_embedding * item_gmf_embedding

        mlp_input = torch.cat([user_mlp_embedding, item_mlp_embedding], dim=-1)
        mlp_output = self.mlp_layers(mlp_input)

        output = torch.sigmoid(self.output_layer(torch.cat([gmf_output, mlp_output], dim=-1))).squeeze(-1)

        # return -r_pos_neg + reg
        return output

    def predict(self, user, item):
        self.eval()
        with torch.no_grad():
            user_gmf_embedding = self.gmf_user_embedding(user)
            item_gmf_embedding = self.gmf_item_embedding(item)

            user_mlp_embedding = self.mlp_user_embedding(user)
            item_mlp_embedding = self.mlp_item_embedding(item)

            gmf_output = user_gmf_embedding.unsqueeze(1) * item_gmf_embedding

            user_mlp_embedding = user_mlp_embedding.unsqueeze(1).expand(-1, item_mlp_embedding.shape[1], -1)
            mlp_input = torch.cat([user_mlp_embedding, item_mlp_embedding], dim=-1)
            mlp_output = self.mlp_layers(mlp_input)

        output = torch.sigmoid(self.output_layer(torch.cat([gmf_output, mlp_output], dim=-1))).squeeze(-1)
        return output

model = NCFModel(hidden_dim, traindataset.user_nums, traindataset.book_nums).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
crit = torch.nn.BCELoss()

loss_for_plot = []
hits_for_plot = []

for epoch in range(epochs):

    losses = []
    for index, data in enumerate(trainloader):
        user, item, label = data
        user, item, label = user.to(device), item.to(device), label.to(device).float()
        y_ = model(user, item).squeeze()

        loss = crit(y_, label)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        losses.append(loss.detach().cpu().item())

    hits = []
    for index, data in enumerate(validloader):
        user, pos, neg = data
        pos = pos.unsqueeze(1)
        all_data = torch.cat([pos, neg], dim=-1)
        output = model.predict(user.to(device), all_data.to(device)).detach().cpu()

        for batch in output:
            if 0 not in (-batch).argsort()[:10]:
                hits.append(0)
            else:
                hits.append(1)
    print('Epoch {} finished, average loss {}, hits@20 {}'.format(epoch, sum(losses) / len(losses),
                                                                  sum(hits) / len(hits)))
    loss_for_plot.append(sum(losses) / len(losses))
    hits_for_plot.append(sum(hits) / len(hits))

# 模型保存
torch.save(model.state_dict(), 'model.h5')

import matplotlib.pyplot as plt

x = list(range(1, len(hits_for_plot) + 1))
plt.subplot(1, 2, 1)
plt.xlabel('epochs')
plt.ylabel('loss')
plt.plot(x, loss_for_plot, 'r')

plt.subplot(1, 2, 2)
plt.xlabel('epochs')
plt.ylabel('acc')
plt.plot(x, hits_for_plot, 'r')

plt.show()

df = pd.read_csv('test_dataset.csv')
user_for_test = df['user_id'].tolist()

predict_item_id = []

def chunks(l, n):
    for i in range(0, len(l), n):
        yield l[i, i + n]

f = open('submission.csv', 'w', encoding='utf-8')

for user in user_for_test:
    # 将用户已经交互过的物品排除
    user_visited_items = traindataset.user_book_map[user]
    items_for_predict = list(set(range(traindataset.book_nums)) - set(user_visited_items))

    results = []
    user = torch.Tensor([user]).to(device)

    for batch in chunks(items_for_predict, 64):
        batch = torch.Tensor(batch).unsqueeze(0).to(device)

        result = model(user, batch).view(-1).detach().cpu()
        results.append(result)

    results = torch.cat(results, dim=-1)
    predict_item_id = (-results).argsort()[:10]
    list(map(lambda x: f.write('{},{}\n'.format(user.cpu().item(), x)), predict_item_id))

f.flush()
f.close()


import asyncio
import websockets
import json
import time

# This is IQRFBB-10 node address in IQRF network
boardAddr = 3

# JSON messages by "https://docs.iqrfsdk.org/iqrf-gateway-daemon/api.html"
LEDR_ON = {
  "mType": "iqrfEmbedLedr_Set",
  "data": {
    "msgId": "testEmbedLedr",
    "req": {
      "nAdr": boardAddr,
      "param": {
        "onOff": True
      }
    },
    "returnVerbose": True
  }
}

LEDR_OFF = {
  "mType": "iqrfEmbedLedr_Set",
  "data": {
    "msgId": "testEmbedLedr",
    "req": {
      "nAdr": boardAddr,
      "param": {
        "onOff": False
      }
    },
    "returnVerbose": True
  }
}

LEDG_ON = {
  "mType": "iqrfEmbedLedg_Set",
  "data": {
    "msgId": "testEmbedLedr",
    "req": {
      "nAdr": boardAddr,
      "param": {
        "onOff": True
      }
    },
    "returnVerbose": True
  }
}

LEDG_OFF = {
  "mType": "iqrfEmbedLedg_Set",
  "data": {
    "msgId": "testEmbedLedr",
    "req": {
      "nAdr": boardAddr,
      "param": {
        "onOff": False
      }
    },
    "returnVerbose": True
  }
}

async def hello():
    # Connect websockets
    async with websockets.connect(
            'ws://localhost:1338') as websocket:

        # RED LED ON
        await websocket.send(json.dumps(LEDR_ON))
        print(f"Sent > {LEDR_ON}")

        response = await websocket.recv()
        print(f"Received < {response}")

        # Wait 2 sec
        time.sleep(2)

        # RED LED OFF
        await websocket.send(json.dumps(LEDR_OFF))
        print(f"Sent > {LEDR_OFF}")

        response = await websocket.recv()
        print(f"Received < {response}")

        # GREEN LED ON
        await websocket.send(json.dumps(LEDG_ON))
        print(f"Sent > {LEDG_ON}")

        response = await websocket.recv()
        print(f"Received < {response}")

        # Wait 2 sec
        time.sleep(2)

        # GREEN LED OFF
        await websocket.send(json.dumps(LEDG_OFF))
        print(f"Sent > {LEDG_OFF}")

        response = await websocket.recv()
        print(f"Received < {response}")

asyncio.get_event_loop().run_until_complete(hello())


import numpy as np
import scipy.sparse.linalg

from . import tensor

def apply_kronecker(ops, x):
    """Apply the Kronecker product of a sequence of square matrices or linear operators."""
    if all(isinstance(A, np.ndarray) for A in ops):
        return _apply_kronecker_dense(ops, x)
    else:
        ops = [scipy.sparse.linalg.aslinearoperator(B) for B in ops]
        return _apply_kronecker_linops(ops, x)

def _apply_kronecker_linops(ops, x):
    """Apply the Kronecker product of a sequence of square linear operators."""
    assert len(ops) >= 1, "Empty Kronecker product"

    if len(ops) == 1:
        return ops[0].dot(x)

    # assumption: all operators are square
    sz = np.prod([A.shape[0] for A in ops])
    assert sz == x.shape[0], "Wrong size for input matrix"

    orig_shape = x.shape
    # make sure input x is 2D ndarray
    if (len(orig_shape) == 1):
        x = x.reshape((orig_shape[0], 1))

    n = x.shape[1] # number of input vectors

    # algorithm relies on column-major matrices
    q0 = np.empty(x.shape, order='F')
    q0[:] = x
    q1 = np.empty(x.shape, order='F')

    for i in reversed(range(len(ops))):
        sz_i = ops[i].shape[1]
        r_i = sz // sz_i  # result is always integer

        q0 = q0.reshape((sz_i, n * r_i), order='F')
        q1.resize((r_i, n * sz_i))

        if n == 1:
            q1[:] = ops[i].dot(q0).T
        else:
            for k in range(n):
                # apply op to coefficients for k-th rhs vector
                temp = ops[i].dot(q0[:, k*r_i : (k+1)*r_i]) # sz_i x r_i
                q1[:, k*sz_i : (k+1)*sz_i] = temp.T

        q0, q1 = q1, q0   # swap input and output

    return q0.reshape(orig_shape, order='F')

def _apply_kronecker_dense(ops, x):
    shape_in  = tuple(op.shape[1] for op in ops)
    shape_out = (np.prod([op.shape[0] for op in ops]),) + x.shape[1:]
    assert x.ndim == 1 or x.ndim == 2, \
        'Only vectors or matrices allowed as right-hand sides'
    if x.ndim == 2 and x.shape[1] > 1:
        m = x.shape[1]
        shape_in  = shape_in  + (m,)
    X = x.reshape(shape_in)
    Y = tensor.apply_tprod(ops, X)
    return Y.reshape(shape_out)


import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import pandas as pd

class MyData(Dataset):
    '''
    Pytorch implementation of torch.utils.data.Dataset
    '''
    def __init__(self, filepath:str):
        self.df = pd.read_csv(filepath, header=None)

    def __len__(self):
        return len(self.df)

    def __getitem__(self, idx):
        return self.df.iloc[idx]

class MyModule(nn.Module):
    '''
    Class_Discription
    '''

    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1,20,5),
        self.relu1 = nn.ReLU(),
        self.conv2 = nn.Conv2d(20,64,5),
        self.rely2 = nn.ReLU()

    def forward(self, x):
        x = self.relu1(self.conv1(x))
        x = self.rely2(self.conv2(x))
        return x

def main():
    dataset = MyData('path/to/my/dataset.csv')
    dataloader = DataLoader(dataset, batch_size=1, shuffle=False)

    model = MyModule()

    optimizer = torch.optim.Adam(model.parameters())
    loss_fn = torch.nn.MSELoss()

    for epoch in range(2):  # loop over the dataset multiple times
        for i, data in enumerate(dataloader):
            # get the inputs; data is a list of [inputs, labels]
            inputs, labels = data

            # zero the parameter gradients
            optimizer.zero_grad()

            # forward + backward + optimize
            outputs = model(inputs)
            loss = loss_fn(outputs, labels)
            loss.backward()
            optimizer.step()

if __name__ == '__main__':
    main()


#!/usr/bin/env python

import rospy
import math
import tf
import geometry_msgs.msg

def sendTF(inv_trans, inv_rot, fromName, toName):
	br = tf.TransformBroadcaster()

	## traslation, rotation, time of publish, to-frame, from-frame
	br.sendTransform( (inv_trans[0], inv_trans[1], inv_trans[2]),
		(inv_rot[0], inv_rot[1], inv_rot[2], inv_rot[3]),
		rospy.Time.now(),
		toName,
		fromName)

def Main():

	while not rospy.is_shutdown():
		"""
		try:
			## lookup - from-frame, to-frame, time of transform
			(trans,rot) = listener.lookupTransform('/usb_cam2/ceiling_grid', '/usb_cam2', rospy.Time(0))
		except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
			continue

		## translation, rotation, from-frame, to-frame
		sendTF(trans, rot, '/ceiling_grid', '/usb_cam2_new')
		"""
		try:
			(trans,rot) = listener.lookupTransform('hd_cam/GrBILS_grid', '/hd_cam', rospy.Time(0))
		except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
			continue

		sendTF(trans, rot, '/GrBILS_grid', '/hd_cam_new')

if __name__ == '__main__':
	rospy.init_node('tf_inverter')
	listener = tf.TransformListener()
	rate = rospy.Rate(10.0)
	Main()


import sqlite3

from os.path import isfile

class SqlWriter(object):

    def __init__(self, options):
        self.db_file = options.db
        self.eval_no = str(options.evaluations)
        self.is_split = str(options.is_split)
        self.mul_init = str(options.mul_init)
        self.prop_no = str(options.prop_no)
        self.symm = str(options.symm)
        self.decision = None
        self.create_tables()

    def create_tables(self):
        CREATE_PRISM_TABLE = """
		CREATE TABLE IF NOT EXISTS prismdata (
		"trial_num" INTEGER NOT NULL PRIMARY KEY,
		"result"	TEXT,
		"profile1"	TEXT,
		"profile2"	TEXT,
		"profile3"	TEXT,
		"clientcount"	TEXT,
		"connectrates"	TEXT,
		"clientinit"	TEXT,
		"serverinit"	TEXT
		)
		"""

        CREATE_INFO_TABLE = """
		CREATE TABLE IF NOT EXISTS experiment (
		"evaluations" TEXT,
		"splitworld" TEXT,
		"multiplestates" TEXT,
		"propertyno" TEXT,
		"symmreduction" TEXT
		)
		"""

        INSERT_EXPERIMENT_DATA = """
		INSERT INTO experiment (
		evaluations,
		splitworld,
		multiplestates,
		propertyno,
		symmreduction)
		VALUES (?, ?, ?, ?, ?);
		"""

        if not isfile(self.db_file):
            f = open(self.db_file, 'w')
            f.close()
        else:
            while True:
                self.decision = input('Existing database already exists. Overwrite (y/n)? ')
                if self.decision == 'y' or self.decision == 'n':
                    break
        try:
            conn = sqlite3.connect(self.db_file)
            c = conn.cursor()
            if self.decision == 'y':
                c.execute("DELETE FROM experiment;")
                c.execute("DELETE FROM prismdata;")
            c.execute(CREATE_PRISM_TABLE)
            c.execute(CREATE_INFO_TABLE)
            values = (self.eval_no, self.is_split, self.mul_init,
                      self.prop_no, self.symm, )
            c.execute(INSERT_EXPERIMENT_DATA, values)
        except sqlite3.Error as e:
            print(e)
        finally:
            conn.commit()
            conn.close()

    def write_data(self, values):
        QUERY = """
		INSERT INTO prismdata (
		trial_num,
		result,
		profile1,
		profile2,
		profile3,
		clientcount,
		connectrates,
		clientinit,
		serverinit)
		VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
		"""

        try:
            conn = sqlite3.connect(self.db_file)
            c = conn.cursor()
            c.execute(QUERY, values)
        except sqlite3.Error as e:
            print(e)
        finally:
            # commit changes and close
            conn.commit()
            conn.close()


import random

player_points = 0
computer_points = 0

while True:

    list = ["rock","paper","scissors"]
    computer_move = random.choice(list)

    print()
    player_move = input("Choose between [r]ock, [p]aper and [s]cissors or [l]eave game: ")

    if player_move.lower() == "r":
        player_move = list[0]
    elif player_move.lower() == "p":
        player_move = list[1]
    elif player_move.lower() == "s":
        player_move = list[2]
    elif player_move.lower() == "l":
        break
    else:
        raise SystemExit("Input is invalid! Try again...")

    print()
    print(f"You chose {player_move}")
    print(f"The computer chose {computer_move}")

    if player_move == computer_move:
        print("Game ended with a draw")

    elif player_move == "rock":
        if computer_move == "scissors":
            print("You won!")
            player_points+=1
        else:
            print("You lost! Try again...")
            computer_points+=1

    elif player_move == "paper":
        if computer_move == "rock":
            print("You won!")
            player_points+=1
        else:
            print("You lost! Try again...")
            computer_points+=1
    elif player_move == "scissors":
        if computer_move == "paper":
            print("You won!")
            player_points+=1
        else:
            print("You lost! Try again...")
            computer_points+=1

print()
print(f"Your points: {player_points}")
print(f"Computer points: {computer_points}")

if player_points>computer_points:
    print("You won! Congratulations!")
elif player_points==computer_points:
    print("Overall game ended with a draw!")
elif player_points<computer_points:
    print("You lost!")

input("Press [Enter] to exit")


class MusicTracker():
    def __init__(self):
        self.playList = {}

    def add_track(self, track, artist):
        if isinstance(track, int) and isinstance(artist, int):
        # if track is int and artist is int:
            raise Exception("Please enter text. Track name and artist name.")

        elif track.strip() == "" or artist.strip() == "":
            raise Exception("Please enter a track along with artist name.")

        else:
            self.playList[track] = {'artist' : artist}
            return "Track has been added"

    def display_tracks(self):
        answer = []
        for track in self.playList:
            answer.append(f"{track} by {self.playList[track]['artist']}")

        return answer


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

import tarfile
import json

from django.core.management.base import BaseCommand
from django.core.exceptions import ObjectDoesNotExist

from corpora import models
from base import models as base_models

# fcar must be a bz2 compressed tar, containing a file index.json:
# {
#    "name": "Novelle di Verga",
#    "language": "italiano",
#    "description": "Tutte le novelle di Verga, preso da LiberLiber"
# }
# and one json file for each text:
# {
#    "title": "Novella 1",
#    "author": "Giovanni Verga",
#    "text": "Testo del racconto..."
# }

class Command(BaseCommand):
    requires_migrations_checks = True
    help = 'Import corpus from file <fcar>'

    def add_arguments(self, parser):
        parser.add_argument(
            'fcar',
            help='file car (Corpus ARchive)',
        )

    def handle(self, *args, **options):
        fcar = options["fcar"]

        archive=tarfile.open(name=fcar, mode='r:bz2')
        m_index=archive.getmember("./index.json")

        fd=archive.extractfile(m_index)
        index=json.loads(fd.read().decode())

        language=base_models.Language.objects.get(name=index["language"])

        corpus,created=models.Corpus.objects.get_or_create(name=index["name"],
                                                           defaults={
                                                               "language": language,
                                                               "description": index["description"]
                                                           })

        for m in archive.getmembers():
            if m==m_index: continue
            if not m.isfile(): continue
            fd=archive.extractfile(m)
            print(m)
            obj=json.loads(fd.read().decode())
            author,created=models.Author.objects.get_or_create(name=obj["author"])
            models.Text.objects.get_or_create(corpus=corpus,title=obj["title"],author=author,
                                              defaults={"text": obj["text"]})

            #print(m)

        archive.close()

        # case_set,created=models.CaseSet.objects.get_or_create(name=name)

        # #fname=args[0]
        # with open(fname,'r') as fd:
        #     for r in fd.readlines():
        #         r=r.strip()
        #         if not r: continue
        #         if r.startswith('#'): continue
        #         t=r.split(":")
        #         if len(t)<2: continue
        #         case_pair,created=models.CasePair.objects.get_or_create(lower=t[0],upper=t[1])
        #         case_set.pairs.add(case_pair)


from ...asset import Asset
from ...call_builder.base import BaseOrderbookCallBuilder
from ...call_builder.call_builder_sync.base_call_builder import BaseCallBuilder
from ...client.base_sync_client import BaseSyncClient

__all__ = ["OrderbookCallBuilder"]

class OrderbookCallBuilder(BaseCallBuilder, BaseOrderbookCallBuilder):
    """Creates a new :class:`OrderbookCallBuilder` pointed to server defined by horizon_url.
    Do not create this object directly, use :func:`stellar_sdk.Server.orderbook`.

    See `Orderbook <https://developers.stellar.org/api/aggregations/order-books/>`__ for more information.

    :param horizon_url: Horizon server URL.
    :param client: The client instance used to send request.
    :param selling: Asset being sold
    :param buying: Asset being bought
    """

    def __init__(
        self,
        horizon_url: str,
        client: BaseSyncClient,
        selling: Asset,
        buying: Asset,
    ) -> None:
        super().__init__(
            horizon_url=horizon_url, client=client, selling=selling, buying=buying
        )


#!/usr/bin/python

##############################################################
# module: find_post_with_kmeans.py
# description: finding related posts with k-means;
# based on code fragments from Ch 3, Section 'Clustering'
# that in 'Building Machine Learning Systems with Python'
# by Richert and Coelho.
#
# bugs to vladimir dot kulyukin at usu dot edu
###############################################################

import os
import sys
import sklearn.datasets
import scipy as sp

## 1. choose 6 training groups
groups = ['comp.graphics', 'comp.os.ms-windows.misc',
          'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware',
          'comp.windows.x', 'sci.space']

## 2. create train data
train_data = sklearn.datasets.fetch_20newsgroups(subset='train',
                                                 categories=groups)
## 3. this prints ('Number of training posts in tech groups:', 3529)
print('Number of training posts in tech groups:', len(train_data.filenames))

## 4. get the labels of train_data.target
labels = train_data.target
num_clusters = 50  # sp.unique(labels).shape[0]

#print(labels)

## ============ This is where K-Means begins ================
## this is the number of clusters needed by k-means. we
## can experiment with different numbers of clusters
num_clusters = 50  # sp.unique(labels).shape[0]

import nltk.stem
english_stemmer = nltk.stem.SnowballStemmer('english')

from sklearn.feature_extraction.text import TfidfVectorizer

class StemmedTfidfVectorizer(TfidfVectorizer):

    def build_analyzer(self):
        analyzer = super(TfidfVectorizer, self).build_analyzer()
        return lambda doc: (english_stemmer.stem(w) for w in analyzer(doc))

vectorizer = StemmedTfidfVectorizer(min_df=10,
                                    stop_words='english',
                                    decode_error='ignore')

## build feature matrix from train_data
train_data_feat_mat = vectorizer.fit_transform(train_data.data)
num_samples, num_features = train_data_feat_mat.shape
print('#samples: %d, #features: %d' % (num_samples, num_features))
# samples: 3529, #features: 4712

from sklearn.cluster import KMeans

## create k-means clustering algorithm
km = KMeans(n_clusters=num_clusters, n_init=1, verbose=1, random_state=3)
## km and clustered data can be persisted
clustered_data = km.fit(train_data_feat_mat)

print('km.labels_=%s' % km.labels_)
# after the clustering is done, each data item
# has its own cluster label ranging from 0 to 49.
print('km.labels_=%s' % km.labels_)
print('len(km.labels_)=%d' % len(km.labels_))
# there are 3529 samples
# the number of samples is the same as the number of labels
# each label is an integer 0 <= i <= 49. or 0 <= i <= n-1
# where n is the number of clusters.
print('max label = %d' % max(km.labels_))
print('min label = %d' % min(km.labels_))

from sklearn import metrics
print('Homogeneity: %0.3f' % metrics.homogeneity_score(labels, km.labels_))
# Homogeneity: 0.400
print('Completeness: %0.3f' % metrics.completeness_score(labels, km.labels_))
# Completeness: 0.206
print('V-measure: %0.3f' % metrics.v_measure_score(labels, km.labels_))
# V-measure: 0.272
print('Adjusted Rand Index: %0.3f' %
      metrics.adjusted_rand_score(labels, km.labels_))
# Adjusted Rand Index: 0.064
print('Adjusted Mutual Information: %0.3f' %
      metrics.adjusted_mutual_info_score(labels, km.labels_))
# Adjusted Mutual Information: 0.197
print(('Silhouette Coefficient: %0.3f' %
       metrics.silhouette_score(train_data_feat_mat, labels, sample_size=1000)))
# Silhouette Coefficient: 0.006

new_post = \
    """Disk drive problems. Hi, I have a problem with my hard disk.
After 1 year it is working only sporadically now.
I tried to format it, but now it doesn't boot any more.
Any ideas? Thanks.
"""

new_post_vec = vectorizer.transform([new_post]).getrow(0).toarray()
km_predicted_labels = km.predict(new_post_vec)
print('len(km_predicted_labels)=%d' % len(km_predicted_labels))

## 1) predict the top cluster label for the new post
top_new_post_label = km.predict(new_post_vec)[0]

#print(top_new_post_label)

## here is an example of nozero() method.
## >>> np.array([True, False, True, False])
## array([ True, False,  True, False], dtype=bool)
##>>> np.array([True, False, True, False]).nonzero()
##(array([0, 2]),)
## >>> np.array([True, False, True, False]).nonzero()[0]
## array([0, 2])

## 2) find the labels of the posts in the same cluster.
posts_in_same_cluster = (km.labels_ == top_new_post_label).nonzero()[0]
## match the new post to the posts in the same cluster
## what is the reduction of the search space? let's find out:
## >>> len(train_data.filenames)
## 3529
## >>> len(posts_in_same_cluster)
## 122
## so, instead of matching aginst 3529 feature vectors, we are
## matching only against 122, i.e., about 3% of the available
## feature vectors.

## 3) go through the labels in the same cluster and
## save the distances.
similar_posts = []
for i in posts_in_same_cluster:
    dist = sp.linalg.norm(new_post_vec - train_data_feat_mat[i])
    similar_posts.append((dist, train_data.data[i]))

## similar_posts is a list of 2-tuples (dist, post_text).
## 4) sort the posts by their score from smallest to largest
similar_posts.sort(key=lambda post: post[0])

## similar_posts[0][0] to access the distance
## similar_posts[0][1] to access the post's text


from tkinter import font
from PIL import Image
import requests
import streamlit as st
from streamlit_lottie import st_lottie
st.set_page_config(page_title="My Webpage", page_icon=":smile:", layout="wide")

def load_lottieurl(url):
    r = requests.get(url)
    if r.status_code !=200:
        return None
    return r.json()

lottie_coding = load_lottieurl("https://assets7.lottiefiles.com/packages/lf20_p4q9ra7d.json")
img_contact_form = Image.open("image/IMG-20211212-WA0005.jpg")
img_lottie_animation = Image.open("image/IMG-20211212-WA0002.jpg")
# Header Section
with st.container():
    st.subheader("Hi, I am Puji :smirk:")
    st.title("A newbie in programing")
    st.write("I am learning web developer for develop my skills")
    st.write("[My Social>](https://www.instagram.com/pujiprastyo24/)")

# What I Do
with st.container():
    st.write("---")
    left_column, right_column = st.columns(2)
    with left_column:
        st.header("What I Do")
        st.write("##")
        st.write(
        """
        On my life i am like a lot things like:
        - learn proggraming
        - learn robotic
        - learn to service electronic tools
        - learn photography.
        """
        )
        st.write("[My Project>](https://www.youtube.com/playlist?list=PLwvmhtXiU1SXjRpc7dfMD5i5Y9388KP1a)")
    with right_column:
        st_lottie(lottie_coding, height=300, key="coding")

#Project
with st.container():
    st.write("---")
    st.header("My Project")
    st.write("##")
    image_column, text_column = st.columns((1, 2))
    with image_column:
        st.image(img_contact_form)

    with text_column:
        st.subheader("Simple web project")
        st.write(
            """
            I am learning web developmet with python programing lenguage,
            because python lenguage is very simple and easy to understand.
            I am learn python lenguage in
            """
        )
        st.markdown("[youtube...](https://www.youtube.com/c/KelasTerbuka)")

    image_column, text_column = st.columns((1, 2))
    with image_column:
        st.image(img_lottie_animation)

    with text_column:
        st.subheader("Simple web project")
        st.write(
            """
            I am learning web developmet with python programing lenguage,
            because python lenguage is very simple and easy to understand.
            I am learn python lenguage in
            """
        )
        st.markdown("[youtube...](https://www.youtube.com/c/KelasTerbuka)")


import numpy as np
from numpy.core.numeric import roll
import torch
import scipy
from scipy.signal import lfilter

def soft_update_network(source_network, target_network, tau):
    for target_param, local_param in zip(target_network.parameters(),
                                        source_network.parameters()):
        target_param.data.copy_(tau*local_param.data + (1-tau)*target_param.data)

def dict_batch_generator(data, batch_size, keys=None):
    if keys is None:
        keys = list(data.keys())
    num_data = len(data[keys[0]])
    num_batches = int(np.ceil(num_data / batch_size))
    indices = np.arange(num_data)
    np.random.shuffle(indices)
    for batch_id in range(num_batches):
        batch_start = batch_id * batch_size
        batch_end = min(num_data, (batch_id + 1) * batch_size)
        batch_data = {}
        for key in keys:
            batch_data[key] = data[key][indices[batch_start:batch_end]]
        yield batch_data

def minibatch_inference(args, rollout_fn, batch_size = 1000, cat_dim=0):
    data_size = len(args[0])
    num_batches = int(np.ceil(data_size/batch_size))
    inference_results = []
    for i in range(num_batches):
        batch_start = i * batch_size
        batch_end = min(data_size, (i + 1) * batch_size)
        input_batch = [ip[batch_start:batch_end] for ip in args]
        outputs = rollout_fn(*input_batch)
        if i == 0:
            if isinstance(outputs, tuple):
                multi_op = True
            else:
                multi_op = False
            inference_results = outputs
        else:
            if multi_op:
                inference_results = (torch.cat([prev_re, op], dim=cat_dim) for prev_re, op in zip(inference_results, outputs))
            else:
                inference_results = torch.cat([inference_results, outputs])
    return inference_results

def merge_data_batch(data1_dict, data2_dict):
    for key in data1_dict:
        if isinstance(data1_dict[key], np.ndarray):
            data1_dict[key]  = np.concatenate([data1_dict[key], data2_dict[key]], axis=0)
        elif isinstance(data1_dict[key], torch.Tensor):
            data1_dict[key]  = torch.cat([data1_dict[key], data2_dict[key]], dim=0)
    return data1_dict

def discount_cum_sum(x, discount):
    return lfilter([1], [1, float(-discount)], x[::-1], axis=0)[::-1]

def get_grad_parameters(model):
    for p in model.parameters():
        if p.requires_grad == True:
            yield p

def get_flattened_params(model):
    params = []
    for param in model.parameters():
        params.append(param.data.view(-1))

    flat_params = torch.cat(params)
    return flat_params

def set_flattened_params(model, params):
    prev_ind = 0
    for param in model.parameters():
        flat_size = int(np.prod(list(param.size())))
        param.data.copy_(
            params[prev_ind:prev_ind + flat_size].view(param.size()))
        prev_ind += flat_size

def get_flat_grads(model, grad_grad=False):
    grads = []
    for param in model.parameters():
        if grad_grad:
            grads.append(param.grad.grad.view(-1))
        else:
            grads.append(param.grad.view(-1))

    flat_grad = torch.cat(grads)
    return flat_grad


num = int(input('Informe um número: '))
t1 = 0
t2 = 1
print(f'{t1} > {t2}', end=' > ')
c = 3
while c <= num:
    t3 = t1 + t2
    print(f'{t3}', end=' > ')
    t1 = t2
    t2 = t3
    c += 1
print('FIM')


import os
import torch
from torchvision import transforms
from torch.utils.data import DataLoader, Dataset
# from dataloader.TransformerDataset import TransformerDataset
import random
import glob
import json
import numpy as np

from dataloader.CNNDataset import CNNDataset

def DataLoaders(params):
    norm_factor = params['data_normalize_factor']
    ds_dir = params['dataset_dir']
    rgb2gray = params['rgb2gray'] if 'rgb2gray' in params else False
    dataset = params['dataset']
    num_worker = 8
    resize = params['resize'] if 'resize' in params else False

    image_datasets = {}
    dataloaders = {}
    dataset_sizes = {}

    ###### Training Set ######

    image_datasets['train'] = CNNDataset(ds_dir, setname='train',
                                         transform=None, norm_factor=norm_factor,
                                         rgb2gray=rgb2gray, resize=resize)

    dataloaders['train'] = DataLoader(image_datasets['train'], shuffle=True, batch_size=params['train_batch_sz'],
                                      num_workers=num_worker)
    dataset_sizes['train'] = {len(image_datasets['train'])}

    ##### Validation Set ######

    image_datasets['val'] = CNNDataset(ds_dir, setname='val', transform=None,
                                       norm_factor=norm_factor,
                                       rgb2gray=rgb2gray, resize=resize)
    dataloaders['val'] = DataLoader(image_datasets['val'], shuffle=False, batch_size=params['val_batch_sz'],
                                    num_workers=num_worker)
    dataset_sizes['val'] = {len(image_datasets['val'])}

    print(dataset_sizes)

    return dataloaders, dataset_sizes


"""
Given 2 args. 1-int k repping the number of workers. 2-array of positive ints repping the duration of work for a task. Each worker must complete 2 unique tasks and can only work on 1 at a time. The number of tasks will always be equal to 2k such that each worker has exactly 2 to complete. Workers will complete tasks in parallel and the time taken to complete ALL tasks will be equal to the time taken to complete the longest pair of tasks.

Your function should the tasks to workers pairing that optimizing this algorithm.

Input:
    k = 3
    tasks = [1, 3, 5, 3, 1, 4]
Output:
    [
        [0, 2], # 1 + 5 = 6
        [4, 5], # 1 + 4 = 5
        [1, 3]  # 3 + 3 = 6
    ]

** note there are multiple solutions
    [2, 4], [0, 5], [1, 3]
** will always be len(tasks)/k = 2
** sum(tasks) / (k) == optimal time (tho this is true, I don't think it's gonna help me)
** maybe using a binary search with above # swapping less than and greater than btw sides
** tasks always even # (divisible by 2)
"""
import time

def taskAssignment(k, tasks):
    # Write your code here.
    matching_pairs = []
    sorted_tasks = [(idx, i) for idx, i in enumerate(tasks)]
    sorted_tasks.sort(key=lambda x: x[1])
    for i in range(int(len(sorted_tasks) / 2)):
        match = (len(sorted_tasks) - i) - 1
        matching_pairs.append([sorted_tasks[i][0], sorted_tasks[match][0]])

    return matching_pairs

tasks = [1, 3, 5, 3, 1, 4]
# tasks = [5, 6, 2, 3, 15, 15, 16, 19, 2, 10, 10, 3, 3, 32, 12, 1, 23, 32, 9, 2]

k = 3
print(taskAssignment(k, tasks))


'''
 @Author: JoeyforJoy & ylheng
 @Date: 2022-03-24 17:49:12
 @LastEditTime: 2022-03-29 11:22:06
 @LastEditors: JoeyforJoy
 @Description: Transfer rosbag to images
 @Example: python bag2img.py ${bag_file} ${img_topic}
'''

import rosbag
import os
import argparse

from src.b2x.utils import *

def parse_args():
    parser = argparse.ArgumentParser(description="Transfer rosbag to images.")
    parser.add_argument("bag_file", type=str, help = "the path of bag file")
    parser.add_argument("topic", type=str, help = "topic name")
    parser.add_argument("--output_dir", type=str, default="./data/imgs", help = "directory of output images")
    parser.add_argument("--format", type=str, default="png", help = "format of output images (png, jpg, jpeg...)")
    return parser.parse_args()

if __name__ == "__main__":
    args = parse_args()

    os.makedirs(args.output_dir, exist_ok=True)

    bag = rosbag.Bag(args.bag_file, "r")
    bag_data = bag.read_messages(args.topic)

    for topic, msg, t in bag_data:
        dumpImageMsg(msg, args.output_dir, str(t), compressed = isCompressedImage(topic), format = args.format)


from datetime import timedelta

from flask import redirect, session
from flask.helpers import flash
from marshmallow_enum import EnumField

from indico.core.db import db
from indico.modules.events import EventLogRealm
from indico.modules.events.registration.controllers.display import RHRegistrationFormRegistrationBase
from indico.modules.events.registration.controllers.management import RHManageRegFormBase
from indico.modules.events.registration.forms import RegistrationPrivacyForm
from indico.modules.events.registration.models.registrations import PublishRegistrationsMode, RegistrationVisibility
from indico.modules.events.registration.util import update_registration_consent_to_publish
from indico.modules.events.registration.views import WPManageRegistration
from indico.modules.logs.models.entries import LogKind
from indico.modules.logs.util import make_diff_log
from indico.util.i18n import _
from indico.web.args import use_kwargs
from indico.web.flask.util import url_for

class RHRegistrationPrivacy(RHManageRegFormBase):
    """Change privacy settings of a registration form."""

    def _get_changes(self, participant_visibility, public_visibility, visibility_duration, retention_period,
                     require_privacy_policy_agreement):
        log_fields = {
            'participant_visibility': {'title': 'Visibility to participants'},
            'public_visibility': {'title': 'Visibility to everyone'},
            'visibility_duration': {'title': 'Visibility duration', 'default': 'Indefinite'},
            'retention_period': {'title': 'Retention period', 'default': 'Indefinite'},
            'require_privacy_policy_agreement': {'title': 'Privacy policy'},
        }

        changes = {
            'participant_visibility': (self.regform.publish_registrations_participants,
                                       PublishRegistrationsMode[participant_visibility]),
            'public_visibility': (self.regform.publish_registrations_public,
                                  PublishRegistrationsMode[public_visibility]),
            'visibility_duration': (self.regform.publish_registrations_duration
                                    if self.regform.publish_registrations_duration is not None else None,
                                    visibility_duration if visibility_duration is not None else None),
            'retention_period': (self.regform.retention_period
                                 if self.regform.retention_period is not None else None,
                                 retention_period if retention_period is not None else None),
            'require_privacy_policy_agreement': (self.regform.require_privacy_policy_agreement,
                                                 require_privacy_policy_agreement)
        }
        changes = {key: (old, new) for key, (old, new) in changes.items() if old != new}
        return make_diff_log(changes, log_fields)

    def _process(self):
        form = RegistrationPrivacyForm(
            event=self.event,
            regform=self.regform,
            retention_period=self.regform.retention_period,
            require_privacy_policy_agreement=self.regform.require_privacy_policy_agreement,
            visibility=[
                self.regform.publish_registrations_participants.name,
                self.regform.publish_registrations_public.name,
                (self.regform.publish_registrations_duration.days // 7
                 if self.regform.publish_registrations_duration is not None else None)
            ]
        )
        if form.validate_on_submit():
            participant_visibility, public_visibility, visibility_duration = form.visibility.data
            visibility_duration = timedelta(weeks=visibility_duration) if visibility_duration is not None else None
            changes = self._get_changes(participant_visibility, public_visibility,
                                        visibility_duration, form.retention_period.data,
                                        form.require_privacy_policy_agreement.data)
            self.regform.retention_period = form.retention_period.data
            self.regform.publish_registrations_participants = PublishRegistrationsMode[participant_visibility]
            self.regform.publish_registrations_public = PublishRegistrationsMode[public_visibility]
            self.regform.publish_registrations_duration = visibility_duration
            self.regform.require_privacy_policy_agreement = form.require_privacy_policy_agreement.data
            db.session.flush()
            self.event.log(EventLogRealm.management, LogKind.change, 'Privacy',
                           f'Privacy settings for "{self.regform.title}" modified', session.user,
                           data={'Changes': changes})
            flash(_('Settings saved'), 'success')
            return redirect(url_for('.manage_registration_privacy_settings', self.regform))

        return WPManageRegistration.render_template('management/regform_privacy.html', self.event,
                                                    regform=self.regform, form=form)

class RHAPIRegistrationChangeConsent(RHRegistrationFormRegistrationBase):
    """Internal API to change registration consent to publish."""

    @use_kwargs({'consent_to_publish': EnumField(RegistrationVisibility)})
    def _process_POST(self, consent_to_publish):
        update_registration_consent_to_publish(self.registration, consent_to_publish)
        return '', 204


from wisdem.aeroelasticse.runFAST_pywrapper import runFAST_pywrapper, runFAST_pywrapper_batch
from wisdem.aeroelasticse.CaseGen_IEC       import CaseGen_IEC

iec = CaseGen_IEC()
iec.Turbine_Class = 'III' # I, II, III, IV
iec.Turbulence_Class = 'A'
iec.D = 206.
iec.z_hub = 140.

# Turbine Data
iec.init_cond = {} # can leave as {} if data not available
iec.init_cond[("ElastoDyn","RotSpeed")] = {'U':[3., 5., 7., 8.2, 25.]}
iec.init_cond[("ElastoDyn","RotSpeed")]['val'] = [2.92, 4.88, 6.81, 7.835, 7.88]
iec.init_cond[("ElastoDyn","BlPitch1")] = {'U':[3., 8.2,  9., 11.,  13.,  15.,  17., 19., 21., 23., 25]}
iec.init_cond[("ElastoDyn","BlPitch1")]['val'] = [0., 0., 2.93, 8.76, 12.01, 14.82, 17.37, 19.73, 21.96, 24.08, 26.10]
iec.init_cond[("ElastoDyn","BlPitch2")] = iec.init_cond[("ElastoDyn","BlPitch1")]
iec.init_cond[("ElastoDyn","BlPitch3")] = iec.init_cond[("ElastoDyn","BlPitch1")]

# DLC inputs
iec.dlc_inputs = {}
iec.dlc_inputs['DLC']   = [1.4]
iec.dlc_inputs['U']     = [[ 6.2]]
iec.dlc_inputs['Seeds'] = [[1]]
iec.dlc_inputs['Yaw']   = [[]]

iec.transient_dir_change        = 'both'  # '+','-','both': sign for transient events in EDC, EWS
iec.transient_shear_orientation = 'both'  # 'v','h','both': vertical or horizontal shear for EWS

# Naming, file management, etc
iec.wind_dir = 'outputs/wind'
iec.case_name_base = 'Hitachi'
iec.Turbsim_exe = '/Users/pbortolo/work/2_openfast/TurbSim/bin/TurbSim_glin64'
iec.cores = 1

iec.debug_level = 2
iec.parallel_windfile_gen = True
iec.run_dir = 'outputs/Hitachi'

# Run case generator / wind file writing
case_inputs = {}
case_inputs[('Fst','OutFileFmt')] = {'vals':[1], 'group':0}
case_inputs[("Fst","CompHydro")]         = {'vals':[0], 'group':0}
case_inputs[("Fst","CompSub")]           = {'vals':[0], 'group':0}
case_inputs[("ElastoDyn","PtfmSgDOF")]   = {'vals':["False"], 'group':0}
case_inputs[("ElastoDyn","PtfmSwDOF")]   = {'vals':["False"], 'group':0}
case_inputs[("ElastoDyn","PtfmHvDOF")]   = {'vals':["False"], 'group':0}
case_inputs[("ElastoDyn","PtfmRDOF")]    = {'vals':["False"], 'group':0}
case_inputs[("ElastoDyn","PtfmPDOF")]    = {'vals':["False"], 'group':0}
case_inputs[("ElastoDyn","PtfmYDOF")]    = {'vals':["False"], 'group':0}
case_inputs[("ElastoDyn","TwFADOF1")]    = {'vals':["True"], 'group':0}
case_inputs[("ElastoDyn","TwFADOF2")]    = {'vals':["True"], 'group':0}
case_inputs[("ElastoDyn","TwSSDOF1")]    = {'vals':["True"], 'group':0}
case_inputs[("ElastoDyn","TwSSDOF2")]    = {'vals':["True"], 'group':0}
case_inputs[("ElastoDyn","FlapDOF1")]    = {'vals':["True"], 'group':0}
case_inputs[("ElastoDyn","FlapDOF2")]    = {'vals':["True"], 'group':0}
case_inputs[("ElastoDyn","EdgeDOF")]     = {'vals':["True"], 'group':0}
case_inputs[("ElastoDyn","DrTrDOF")]     = {'vals':["False"], 'group':0}
case_inputs[("ElastoDyn","GenDOF")]      = {'vals':["True"], 'group':0}
case_inputs[("ElastoDyn","YawDOF")]      = {'vals':["False"], 'group':0}

case_list, case_name_list = iec.execute(case_inputs=case_inputs)

# Run FAST cases
fastBatch = runFAST_pywrapper_batch(FAST_ver='OpenFAST',dev_branch = True)
fastBatch.FAST_exe = '/Users/pbortolo/work/2_openfast/openfast/build/glue-codes/openfast/openfast'   # Path to executable
fastBatch.FAST_InputFile = 'DW0.fst'   # FAST input file (ext=.fst)
fastBatch.FAST_directory = '/Users/pbortolo/work/3_projects/1_Hitachi/files2Hitachi/OpenFAST/DW0'   # Path to fst directory files
fastBatch.FAST_runDirectory = iec.run_dir
fastBatch.case_list = case_list
fastBatch.case_name_list = case_name_list
fastBatch.debug_level = 2
fastBatch.run_serial()


import os
import openai
import pandas as pd
from concurrent.futures import ThreadPoolExecutor
from google.cloud import bigquery
# from dotenv import load_dotenv
from datetime import datetime
from fetch_query import prepare_notam_row, check_existing_notams_keys, fetch_notam_by_ids

# load_dotenv()
GPT_MODEL = "gpt-4-0613"
GPT_MODEL_BRIEFING = "gpt-4"
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY

function_descriptions = [
    {
        "name": "extract_info_from_notam",
        "description": "extract and interprate key info from an NOTAM (Notice to Airman), such as short description, category, impacted role (ATC,Flight Dispatcher, etc).",
        "parameters": {
            "type": "object",
            "properties": {
                "notamShortDescription": {
                    "type": "string",
                    "description": "The most important informations of the NOTAM summarized to one or two setences"
                },
                "notamDescription": {
                    "type": "string",
                    "description": "Interpretation of the NOTAM"
                },
                "category": {
                    "type": "string",
                    "description": "Try to categorise this NOTAM into categories like those: 1. Opening Hours; 2. Airport maintenance; 3. Airspace closure; 4. Obstacle; etc."
                },
                "impactedRole":{
                    "type": "string",
                    "description": "Try to categorise which role should be informed about this NOTAM. Return value as string for roles after comma. (Pilot, Flight Dispatcher, ATC)."
                },
            },
            "required": ["notamShortDescription", "notamDescription", "category", "impactedRole"]
        }
    }
]

def interpret_notam_with_gpt(notam_message):
    # Call the OpenAI Chat Completions API to interpret the NOTAM
    prompt = f"Please extract key information from this NOTAM: {notam_message} "
    message = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=GPT_MODEL,
        api_key=OPENAI_API_KEY,
        messages=message,
        functions = function_descriptions,
        function_call="auto"
    )
    arguments = response.choices[0]["message"]["function_call"]["arguments"]
    short_interpretation = eval(arguments).get("notamShortDescription")
    interpretation= eval(arguments).get("notamDescription")
    category= eval(arguments).get("category")
    roles= eval(arguments).get("impactedRole")

    return short_interpretation, interpretation, category, roles

def prepare_gpt_interpretation_row(notam, short_interpretation, interpretation, category, roles):
    return {
        'notam_id': notam['notam_id'],
        'notam_content': notam['message'] if notam['message'] else notam['all'],
        'gpt_model': GPT_MODEL,
        'gpt_short_interpretation': short_interpretation,
        'gpt_interpretation': interpretation,
        'gpt_category': category,
        'gpt_interpretation_role': roles,
        'processed_at': datetime.now().isoformat()
    }

def insert_gpt_interpretation_into_bigquery(rows_to_insert):
    client = bigquery.Client()
    table_ref = client.dataset('model').table('notam_gpt_interpretation')
    dataframe = pd.DataFrame(rows_to_insert)
    job = client.load_table_from_dataframe(dataframe, table_ref)
    job.result()

def check_interpretation_exists(notam_id, model=GPT_MODEL):
    client = bigquery.Client()
    query = f"""
    SELECT notam_id FROM UNNEST({notam_id}) notam_id
    EXCEPT DISTINCT
    SELECT notam_id FROM notamify.model.notam_gpt_interpretation WHERE notam_id IN UNNEST({notam_id}) AND gpt_model = '{model}'
    """
    query_job = client.query(query)
    notam_ids = [row['notam_id'] for row in query_job.result()]
    return notam_ids

def fetch_interpret_and_insert_notams(notams, notam_ids):
    if notams is None:
        print("NOTAMs not found. Skipping interpretation.")
        return

    notam_ids_to_interpret = check_interpretation_exists(notam_ids)

    if not notam_ids_to_interpret:
        return

    interpretations_to_insert = []

    def process_notam(notam):
        if notam['notam_id'] in notam_ids_to_interpret:
            message = notam['message'] if notam['message'] else notam['all']
            short_interpretation, interpretation, category, roles = interpret_notam_with_gpt(message)
            interpretation_row = prepare_gpt_interpretation_row(notam, short_interpretation, interpretation, category, roles)
            return interpretation_row

    # Parallelize NOTAM processing
    with ThreadPoolExecutor() as executor:
        interpretations_to_insert = list(executor.map(process_notam, notams))

    # Remove None values (if any)
    interpretations_to_insert = [x for x in interpretations_to_insert if x is not None]

    # Insert into BigQuery in one go
    if interpretations_to_insert:
        insert_gpt_interpretation_into_bigquery(interpretations_to_insert)

    return None

# Briefing

def fetch_interpretations_from_bigquery(notam_ids):
    client = bigquery.Client()
    query = f"""
    SELECT icao, gpt_short_interpretation, gpt_category, gpt_interpretation_role
    FROM notamify.model.notam_gpt_interpretation
    INNER JOIN
    (SELECT notam_id, icao FROM notamify.raw.notams_icao_api QUALIFY ROW_NUMBER() OVER(PARTITION BY notam_id ORDER BY processed_at DESC) = 1) USING (notam_id)
    WHERE notam_id IN UNNEST ({notam_ids})
    ORDER BY icao, gpt_interpretation_role
    """
    query_job = client.query(query)
    return list(query_job.result())

def generate_briefing(notam_ids, role):
    # Fetch the interpretations from BigQuery
    interpretations = fetch_interpretations_from_bigquery(notam_ids)

    # Format the interpretations into a briefing
    notams = ";\n".join([f"Airport: {interp['icao']}. NOTAM: {interp['gpt_short_interpretation']}\n" for interp in interpretations])

    # Use the GPT model to generate a summary of the briefing
    prompt = f"You are tasked with providing a briefing for a {role} based solely on the provided NOTAMs. Your goal is to extract and present only the information that is directly relevant to the responsibilities of a {role}. Please prioritize the most critical information and keep the briefing concise and to the point. Do not include any information that is not directly related to the role of a {role}. Begin the briefing with the phrase 'Here is your briefing' and format the briefing in Markdown. Use the following NOTAMs as your source of information:\n{notams}."
    message = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=GPT_MODEL_BRIEFING,
        api_key=OPENAI_API_KEY,
        messages=message
    )

    # Extract the summary from the response
    summary = response.choices[0].message.content.strip()

    return summary


#ecoding: utf-8
import kivy
kivy.require('1.9.1')

# Testing github with pycharm

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.uix.behaviors.button import ButtonBehavior
from kivy.graphics import Color, Ellipse, Rectangle
from kivy.properties import ListProperty
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.animation import Animation

class Gerenciador(ScreenManager):
    pass

class Menu(Screen):

    def on_pre_enter(self): #Éssa função é para mostrar o Popup quando apertardo o botão ESQ
        Window.bind(on_request_close=self.confirmar)

    def confirmar(self,*args,**kwargs):

        box = BoxLayout(orientation='vertical', padding=10, spacing=10)
        botoes = BoxLayout(spacing=10)
        pop = Popup(title='Deseja realmente sair?', content=box, size_hint=(None,None),
                    size=(180,180))
        imag = Image(source='icones/atencao.png')
        bt_sim = Botao(text='Sim',on_release=App.get_running_app().stop)
        bt_nao = Botao(text='Não', on_release=pop.dismiss)

        box.add_widget(imag)
        botoes.add_widget(bt_sim)
        botoes.add_widget(bt_nao)

        box.add_widget(botoes)

        anim_pop = Animation(size=(300,180), duration=.2, t="out_back")
        anim_pop.start(pop)

        anim_bt_sim = Animation(color=(0,0,0,1)) + Animation(color=(1,1,1,1))
        anim_bt_sim.start(bt_sim)
        anim_bt_sim.repeat = True

        pop.open()
        return True

class Competitivo(Screen):
    pass

class Botao(ButtonBehavior,Label):
    cor = ListProperty([.3,.3,.3,1])
    cor2 = ListProperty([0,0,0,1])

    def __init__(self,**kwargs):
        super(Botao,self).__init__(**kwargs)
        self.atualizar()

    def on_size(self,*kwargs):
        self.atualizar()
    def on_pos(self,*kwargs):
        self.atualizar()

    def on_cor(self,*args):
        self.atualizar()

    def on_press(self,*args):
        self.cor,self.cor2 = self.cor2,self.cor

    def on_release(self,*args):
        self.cor,self.cor2 = self.cor2,self.cor

    def atualizar(self):
        self.canvas.before.clear()
        with self.canvas.before:
            # Color(rgba=(.3,.3,.3,1))
            Color(rgba=self.cor)
            Ellipse(size=(self.height,self.height),
                    pos=(self.x,self.y))
            Ellipse(size=(self.height,self.height),
                    pos=(self.x+self.width-self.height,self.y))
            Rectangle(size=(self.width-self.height,self.height),
                      pos=(self.x+self.height/2.0, self.y))

class Game(Screen):
    lista_nome1 = list()
    lista_nome2 = list()
    lb_posicao = 1
    step = list()
    valida = 0
    pos2 = 0
    cont = 0
    retroceder_uma_vez = 0

    def __init__(self, **kwargs):
        super(Game,self).__init__(**kwargs)
        self.l_nome1 = Game.lista_nome1
        self.l_nome2 = Game.lista_nome2
        self.l_step = Game.step
        self.valida = Game.valida   # Para validar o botão "VS"
        self.pos2 = Game.pos2
        self.cont = Game.cont
        self.voltar_uma = Game.retroceder_uma_vez
        self.lb_posicao  = Game.lb_posicao
        self.step_cont = Game.cont

    def add_lista(self):
        """
        Adicionando os nomes na lista e apagando o nomes que foram escritos caixa de entrada "write_nome"
        :return:
        """
        if self.ids.entrada.text.capitalize().strip() in self.l_nome2 or self.ids.entrada.text.capitalize().strip() in self.l_nome1:
            self.ids.lb_entrada.text = str(self.ids.entrada.text.capitalize().strip() + ' já extende a lista de oponentes')
        elif self.ids.entrada.text != '':
            self.l_nome2.append(self.ids.entrada.text.capitalize().strip())
            self.ids.lb_entrada.text = 'Digite os nome dos jogadores'
            if len(self.l_nome1) > 0:
                self.ids.lb_entrada.text = self.ids.entrada.text.capitalize().strip() + ' Adicionado a fila'
        if len(self.ids.entrada.text) <= 0:
            self.ids.lb_entrada.text = 'Não foi digitado nenhum nome'
        if self.valida > 0:
            self.ids.quant_jogador.text = str(len(self.l_nome2) + 1)
            # self.lb_nome3["text"] = self.l_nome2[self.lb_posicao]
        else:
            self.ids.quant_jogador.text = str(len(self.l_nome2))
        self.ids.entrada.text = ''

    def destribui_nome(self):
        """
        Colocando os nomes nos botões de celeção
        :return:
        """
        if self.valida < 1:  # Aqui valida o botão "VS" para ser usado somente no começo da destribuição dos jogadore
            if len(self.l_nome2) > 2:
                self.l_nome1 = self.l_nome2[0]      #lista_nome1 recebendo a primeira posição da lista_nome2
                del(self.l_nome2[0])                #deletando o nome da lista_nome2 que foi passado pára para a lista_nome1
                self.ids.nome1.text = str(self.l_nome1)
                self.ids.nome2.text = str(self.l_nome2[0])
                self.ids.lb_nome3.text = self.l_nome2[1] # Mostrando o proximo jogador
                self.ids.quant_jogador.text = str(len(self.l_nome2)+1)
                self.valida +=1
            else:
                self.ids.lb_entrada.text = 'Digite pelomenos 3 nomes'

    # Logicá do programa de entradas de nomes
    def passando_nome1(self):
        # if self.step_cont == 1:
        #     self.cont = self.step_cont
        # else:
        #     self.step_cont = self.cont  # STEP_CONT RECEBE O CONTADOR DE VITÓRIA PARA REPOSICIONAR O ULTIMO RESULTADO DA VITORIA
        if self.ids.nome1.text != "Primeiro nome":
            self.pos2 += 1
            self.ids.lb_cont.text = str(self.cont)
            if self.ids.check_vitoria.active:  # celetor para ativar ou desativar o contador de vitórias
                self.cont += 1
                self.ids.lb_vitoria.text = 'Vitoria Ligada'
            else:
                self.ids.lb_vitoria.text = 'Vitoria pausada'
            if self.pos2 >= len(self.l_nome2):
                self.pos2 = 0
            self.lb_posicao = self.pos2
            self.ids.lb_cont.text = str(self.cont)
            self.lb_posicao += 1
            if self.lb_posicao >= len(self.l_nome2):
                self.lb_posicao = 0
            self.ids.nome2.text = str(self.l_nome2[self.pos2])
            self.ids.lb_nome3.text = self.l_nome2[self.lb_posicao]
        else:
            self.ids.lb_entrada.text = 'Caixa primeiro nome vázio!'
        self.step = ''
        self.voltar_uma = 0
        self.step_cont = self.cont  # STEP_CONT RECEBE O CONTADOR DE VITÓRIA PARA REPOSICIONAR O ULTIMO RESULTADO DA VITORIA

    def passando_nome2(self):
        if self.ids.nome2.text != "Segundo nome":
            self.step = self.l_nome2[self.pos2]
            self.l_nome2[self.pos2] = self.l_nome1
            self.l_nome1 = self.step
            self.pos2 += 1
            self.lb_posicao +=1

            self.cont = 1
            if self.ids.check_vitoria.active:
                self.cont = 1
                self.ids.lb_vitoria.text = 'Vitoria Ligada'
            else:
                self.ids.lb_vitoria.text = 'Vitoria Pausada'
            if self.pos2 >= len(self.l_nome2):
                self.pos2 = 0
            if self.lb_posicao >= len(self.l_nome2):
                self.lb_posicao = 0
            self.ids.lb_nome3.text = str(self.l_nome2[self.lb_posicao])
            self.ids.lb_cont.text = str(self.cont)
            self.ids.nome1.text = str(self.l_nome1)
            self.ids.nome2.text = str(self.l_nome2[self.pos2])
        else:
            self.ids.lb_entrada.text = 'Caixa segundo nome vázio!'
        self.voltar_uma = 0

    def retroceder(self):
        if len(self.l_nome2) > 0:
            if self.voltar_uma == 0:
                self.voltar_uma += 1
                if self.pos2 == 0:  # se a pos2 voltar para o zero ele ira receber o ultimo numero da lista que era oque esta
                    self.pos2 = len(self.l_nome2)-1
                else:
                    self.pos2 -= 1
                if self.lb_posicao == 0:
                    self.lb_posicao = len(self.l_nome2)-1
                else:
                    self.lb_posicao -= 1

                if len(self.step) > 0:
                    self.l_nome1 = self.l_nome2[self.pos2]
                    self.l_nome2[self.pos2] = self.step
                    self.step = ''
                    self.cont = self.step_cont
                    self.cont += 1
                    self.ids.nome1.text = str(self.l_nome1)
                    self.ids.nome2.text = str(self.l_nome2[self.pos2])
                    self.ids.lb_nome3.text = str(self.l_nome2[self.lb_posicao])
                else:
                    self.ids.nome2.text = str(self.l_nome2[self.pos2])
                    self.ids.lb_nome3.text = str(self.l_nome2[self.lb_posicao])
                self.cont -= 1
                self.ids.lb_cont.text = str(self.cont)
        else:
            self.ids.lb_entrada.text = 'Sem nome na lista'

    def deleta(self):
        if self.ids.entrada.text != '':
            if self.ids.entrada.text.capitalize() not in self.l_nome2:
                self.ids.lb_entrada.text = str(self.ids.entrada.text.capitalize() +' nome não estende a lista de jogadore')
            posicao = self.l_nome2.index(self.ids.entrada.text.capitalize())
            del (self.l_nome2[posicao])
            self.ids.lb_entrada.text = str(self.ids.entrada.text.capitalize() + ' Removido da lista')
            self.ids.entrada.text = ''
            self.ids.quant_jogador.text = str(len(self.l_nome2) + 1)
            if self.lb_posicao >= len(self.l_nome2):
                self.lb_posicao = 0
            self.ids.lb_nome3.text = self.l_nome2[self.lb_posicao]

    def lb_check(self):
        if self.ids.check_vitoria.active:
            self.ids.lb_vitoria.text = 'Vitoria Ligada'
        else:
            self.ids.lb_vitoria.text = 'Vitoria Pausada'
    #=========== Eventos de teclado ===========================
    def on_pre_enter(self):
        Window.bind(on_keyboard=self.salvar)

    def salvar(self,window,key,*args):
        if key == 13:
            self.add_lista()
        if key == 27:
            App.get_running_app().root.current = 'Menu'
            return True

    def on_pre_leave(self):
        Window.unbind(on_keyboard=self.salvar)

    def sair(self):
        Menu().confirmar()

class MkApp(App):
    def build(self):
        return Gerenciador()

MkApp().run()


"""Test parsing GO-CAMs inputs."""

import io
import sys
import tempfile
import unittest
from unittest import mock

from kg_covid_19.transform_utils.gocam_transform import GocamTransform

class TestGOCams(unittest.TestCase):
    """Tests for parsing GO-CAM data."""

    def setUp(self) -> None:
        """Set up for GO-CAM transform tests."""
        self.gc_nt_file = "tests/resources/gocams/lifted-go-cams-20200619_SNIPPET.nt"
        self.input_dir = "tests/resources/gocams/"
        self.output_dir = tempfile.mkdtemp()
        self.gocams_t = GocamTransform(
            input_dir=self.input_dir, output_dir=self.output_dir
        )

    def test_run(self):
        """Test running the GO-CAM transformation."""
        # Suppress chatter
        with mock.patch("sys.stdout", new=io.StringIO()):
            self.gocams_t.run(data_file=self.gc_nt_file)
            sys.stdout = sys.__stdout__

    def test_run_exists(self):
        """Test if the transform exists."""
        self.assertTrue(isinstance(self.gocams_t.run, object))


# Exercício 5.14
# Escreva um programa que leia números inteiros do teclado.
# O programa deve ler os números até que o usuário digite
# 0. No final da execução, exiba a quantidade de números digitados,
# assim como a soma e a média aritmética.

soma = 0
qtde = 0
while True:
    n = int(input("Digite um número inteiro: "))
    if n == 0:
        break
    soma += n
    qtde += 1
media = soma / qtde
print(f"\nNúmeros digitados: {qtde}.")
print(f"Soma: {soma}.")
print(f"Média: {media}.")


'''
  2.	Program to read a CSV (CSV with n number of columns) and store it in DICT of list.
'''

import csv

#inialise empty dictonary to store csv file rows
dict = dict()
count = 0

#opens csv file Iris.csv in read mode
try:
    with open("Iris.csv","r") as file:
        reader = csv.reader(file)  #reads csv file rows
        for row in reader:
            dict[count] = row  #store rows in dictonary
            count += 1

    print(dict)
except (FileNotFoundError, IOError):
    print("Wrong file or file path")


from appconfig import AppConfig
from requestsevice import LkRequest
import requests
class Remains:
    @staticmethod
    def upload(file_path):
        files = {
            'file': (
                'остатки.xlsx', open(file_path, 'rb'),
                'application/vnd.ms-excel', {'Expires': '0'}
            )
        }
        appconfig = AppConfig.getInstance()
        url = f"https://seller.wildberries.ru/ns/api/suppliers-portal-marketplace/api/v2/portal/stocks/uploads?key={appconfig.get('key')}"
        requests.models.Request()
        req = LkRequest().request(url, files=files, data={'warehouseId': appconfig.get('warehouseId')})
        return req.getData()


t = int(input())
l0 = list(map(float,input().split()))
min_ = min(l0)
max_ = max(l0)
while min_ in l0:
	l0.remove(min_)
while max_ in l0:
	l0.remove(max_)
# print(l0)
# l1 = [value for value in l0 if value != min(l0)]
# l = [value for value in l1 if value != max(l1)]
if len(l0) != 0:
	sum_ = 0
	for i in l0:
		sum_ += i
	avg = sum_ / len(l0)
	print("%.2f"% avg)


# 완주하지 못한 선수 (hash)

# 문제설명 : https://programmers.co.kr/learn/courses/30/lessons/42576

# 예상풀이
# Hash를 사용하지 않은 풀이다. (성공하지 못한풀이(결과x))

# 문제풀이(1)
# def solution(participant, completion):

#     participant.sorted()
#     completion.sorted()

#     for i in range(len(participant)):
#         for j in range(len(completion)):
#             if participant[i] == completion[j]:
#                 participant[i].remove()

#     return participant

#Hash를 사용하지 않은 풀이다. (성공하지 못한풀이(결과x))

# 문제풀이(2)
# def solution(participant, completion):
#     answer = ''
#     participant.sort()
#     completion.sort()

#     for i in range(len(completion)):
#         if participant[i] != completion[i]:
#             answer = participant[i]
#         else:
#             answer = participant[-1]

#     return answer

# 테스트 케이스는 통과했지만, 제출은 성공하지 못했다

# 문제풀이(3)
def solution(participant, completion):

    participant.sort()
    completion.sort()

    for i, j in zip(participant, completion):
        if i != j :
            return i
    return participant[-1]

# 처음에 participant와 completion 배열을 정렬을 해준다.
# zip 함수를 사용하여 participant와 completion 배열을 묶어준 뒤
# i와 j의 값이 같지 않다면 i를 return해주고
# 아니라면 participant배열의 마지막 값을 return 해준다.

# 다른 사람의 풀이(1)
import collections

def solution(participant, completion):
    answer = collections.Counter(participant) - collections.Counter(completion)
    return list(answer.keys())[0]

# collections 모듈을 사용한 풀이다.

print(solution(["leo", "kiki", "eden"], ["eden", "kiki"]))      # "leo"
print(solution(["marina", "josipa", "nikola", "vinko", "filipa"], ["josipa", "filipa", "marina", "nikola"]))        # "leo"
print(solution(["mislav", "stanko", "mislav", "ana"], ["stanko", "ana", "mislav"]))     # "mislav"