from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import optparse

from utils.data.Dataloader import lidar_camera_dataloader
from utils.core.config import config

from utils.core.config import load_config
from utils.helpers.helpers import save_model
from utils.helpers.helpers import display_two_images

from Generator import Generator
from Discriminator import PixelDiscriminator

import torch.optim as optim
import torch.nn as nn
import torch
from torch.autograd import Variable
import numpy as np
import matplotlib.pyplot as plt

parser = optparse.OptionParser()

parser.add_option('-c', '--config', dest="config",
                  help="load this config file", metavar="FILE")

def train(dataloader, config, device):
    lidar_gen_losses = []
    camera_gen_losses = []
    lidar_disc_losses = []
    camera_disc_losses = []
    generator_losses = []
    cycle_losses = []

    # nn.BCEWithLogitsLoss(reduction='mean') # works better with log loss
    # lidar_weights = torch.tensor([
    #     1.0139991, 88.7063538, 410.3404964, 19369.19224, 22692.19007]).to(device=device, dtype=torch.float)
    # lidar generator_note softmax2d brokes either crossentropy or generator itself completely. Do not use it with lidar
    # log 2
    lidar_weights = torch.tensor([
        1.0139991, 6.4594316, 15.68067773024, 30.2414761695, 30.4699082327]).to(device=device, dtype=torch.float)
    lidar_multiplier = torch.ones(config.TRAIN.BATCH_SIZE, 5, 375, 1242).to(device=device, dtype=torch.float)
    for i in range(config.TRAIN.BATCH_SIZE):
        lidar_multiplier[i][0] = torch.ones(375, 1242) * lidar_weights[0]
        lidar_multiplier[i][1] = torch.ones(375, 1242) * lidar_weights[1]
        lidar_multiplier[i][2] = torch.ones(375, 1242) * lidar_weights[2]
        lidar_multiplier[i][3] = torch.ones(375, 1242) * lidar_weights[3]
        lidar_multiplier[i][4] = torch.ones(375, 1242) * lidar_weights[4]

    # camera_weights = torch.tensor([
    #     1.4859513, 3.9798364, 13.7709121, 483.6851552, 926.3148902]).to(device=device, dtype=torch.float)
    # log2
    # camera_weights = torch.tensor([
    #     1.0, 1.9927091, 3.7835522, 8.9179244, 9.8553588]).to(device=device, dtype=torch.float)
    # criterion = nn.CrossEntropyLoss( reduction=config.TRAIN.DISCRIMINATOR_CRITERION_REDUCTION)
    # criterion = nn.BCELoss(reduction=config.TRAIN.DISCRIMINATOR_CRITERION_REDUCTION)
    criterion_cam_to_lidar = nn.MSELoss(reduction=config.TRAIN.DISCRIMINATOR_CRITERION_REDUCTION)
    criterion_pixel = nn.L1Loss(reduction=config.TRAIN.DISCRIMINATOR_CRITERION_REDUCTION)
    criterion_cycle = nn.L1Loss(reduction=config.TRAIN.DISCRIMINATOR_CRITERION_REDUCTION)
    real_sample_label = 1
    fake_sample_label = 0

    # each sensor should have their own Generator and Discriminator because their input size will probably not match
    lidar_gen = Generator(5, 5, config.NUM_GPUS).to(device)
    camera_gen = Generator(5, 5, config.NUM_GPUS).to(device)
    lidar_disc = PixelDiscriminator(5, config.NUM_GPUS).to(device)
    camera_disc = PixelDiscriminator(5, config.NUM_GPUS).to(device)

    if (device.type == 'cuda') and (config.NUM_GPUS > 1):
        camera_gen = nn.DataParallel(camera_gen, list(range(config.NUM_GPUS)))
        lidar_gen  = nn.DataParallel(lidar_gen, list(range(config.NUM_GPUS)))
        lidar_disc = nn.DataParallel(lidar_disc, list(range(config.NUM_GPUS)))
        camera_disc = nn.DataParallel(camera_disc, list(range(config.NUM_GPUS)))

    # Setup Adam optimizers for both G and D
    optimizer_lidar_gen = optim.Adam(lidar_gen.parameters(), lr=config.LIDAR_GENERATOR.BASE_LR,
                                     betas=(config.TRAIN.BETA1, config.TRAIN.BETA2))
    lidar_gen_scheduler = optim.lr_scheduler.StepLR(optimizer_lidar_gen,
                                                    step_size=config.LIDAR_GENERATOR.STEP_SIZE,
                                                    gamma=config.LIDAR_GENERATOR.STEP_GAMMA)
    optimizer_camera_gen = optim.Adam(camera_gen.parameters(), lr=config.CAMERA_GENERATOR.BASE_LR)
    camera_gen_scheduler = optim.lr_scheduler.StepLR(optimizer_camera_gen,
                                                     step_size=config.CAMERA_GENERATOR.STEP_SIZE,
                                                     gamma=config.CAMERA_GENERATOR.STEP_GAMMA)
    optimizer_lidar_disc = optim.Adam(lidar_disc.parameters(), lr=config.LIDAR_DISCRIMINATOR.BASE_LR,
                                      betas=(config.TRAIN.BETA1, config.TRAIN.BETA2))
    lidar_disc_scheduler = optim.lr_scheduler.StepLR(optimizer_lidar_disc,
                                                     step_size=config.LIDAR_DISCRIMINATOR.STEP_SIZE,
                                                     gamma=config.LIDAR_DISCRIMINATOR.STEP_GAMMA)
    optimizer_camera_disc = optim.Adam(camera_disc.parameters(), lr=config.CAMERA_DISCRIMINATOR.BASE_LR)
    camera_disc_scheduler = optim.lr_scheduler.StepLR(optimizer_camera_disc,
                                                      step_size=config.CAMERA_DISCRIMINATOR.STEP_SIZE,
                                                      gamma=config.CAMERA_DISCRIMINATOR.STEP_GAMMA)
    test_lidar_path1 = "/home/fatih/Inputs/test/46cameraView_0000000000.npz"
    test_camera_path1 = "/home/fatih/Inputs/test/46segmented_0000000000.npz"

    test_lidar_path2 = "/home/fatih/Inputs/test/01cameraView_0000000000.npz"
    test_camera_path2 = "/home/fatih/Inputs/test/01segmented_0000000000.npz"

    test_lidar1 = torch.from_numpy(np.load(test_lidar_path1)["data"].reshape(1, 5, 375, 1242)).to(device=device,
                                                                                dtype=torch.float)
    test_camera1 = torch.from_numpy(np.load(test_camera_path1)["data"].reshape(1, 5, 375, 1242)).to(device=device,
                                                                                dtype=torch.float)

    test_lidar2 = torch.from_numpy(np.load(test_lidar_path2)["data"].reshape(1, 5, 375, 1242)).to(device=device,
                                                                                dtype=torch.float)
    test_camera2 = torch.from_numpy(np.load(test_camera_path2)["data"].reshape(1, 5, 375, 1242)).to(device=device,
                                                                                dtype=torch.float)
    # camera_gen_total_params = sum(p.numel() for p in camera_gen.parameters())
    # print("Camera Generator ", camera_gen_total_params)

    # camera_disch_total_params = sum(p.numel() for p in camera_disc.parameters())
    # print("Camera Discriminator ", camera_disch_total_params)

    example_camera_output = []

    # if config.TRAIN.START_EPOCH > 0:
    #     print("loading previous model")
    #     checkpoint = torch.load(config.TRAIN.LOAD_WEIGHTS)
    #     camera_gen.load_state_dict(checkpoint['camera_gen'])
    #     camera_disc.load_state_dict(checkpoint['camera_disc'])
    #     optimizer_camera_gen.load_state_dict(checkpoint['optimizer_camera_gen'])
    #     optimizer_camera_disc.load_state_dict(checkpoint['optimizer_camera_disc'])
    #     camera_gen.train()
    #     camera_disc.train()
    #     print("done")

    for epoch in range(config.TRAIN.START_EPOCH, config.TRAIN.MAX_EPOCH):
        for current_batch, data in enumerate(dataloader, 0):
            if len(dataloader) - current_batch< config.TRAIN.BATCH_SIZE:
                continue

            label_real = Variable(torch.cuda.FloatTensor(np.ones((config.TRAIN.BATCH_SIZE, 1, 23, 77))),
                                  requires_grad=False)
            label_fake = Variable(torch.cuda.FloatTensor(np.zeros((config.TRAIN.BATCH_SIZE, 1, 23, 77))),
                                  requires_grad=False)

            # display_two_images(data["camera_data"][0], data["lidar_data"][0])
            camera_sample = data["camera_data"].to(device=device, dtype=torch.float)
            lidar_sample = data["lidar_data"].to(device=device, dtype=torch.float)

            ################################################################################
            #                               Zero Gradients
            ################################################################################

            optimizer_lidar_gen.zero_grad()
            optimizer_camera_gen.zero_grad()
            optimizer_lidar_disc.zero_grad()
            optimizer_camera_disc.zero_grad()

            ###############################################################################
            #                          Generators
            ###############################################################################

            camera_gen.zero_grad()
            lidar_gen.zero_grad()

            generated_camera_sample = camera_gen(lidar_sample)
            generated_lidar_sample = lidar_gen(camera_sample)

            cycled_camera_sample = camera_gen(generated_lidar_sample)
            cycled_lidar_sample = lidar_gen(generated_camera_sample)

            camera_cycle_error = criterion_cycle(camera_sample, cycled_camera_sample)
            lidar_cycle_error = criterion_cycle(lidar_sample * lidar_multiplier, cycled_lidar_sample * lidar_multiplier)

            total_cycle_error = camera_cycle_error + lidar_cycle_error

            camera_disc_on_generated = camera_disc(generated_camera_sample, lidar_sample)
            lidar_disc_on_generated = lidar_disc(generated_lidar_sample, camera_sample)

            camera_gen_error_disc = criterion_cam_to_lidar(camera_disc_on_generated, label_real)
            lidar_gen_error_disc = criterion_cam_to_lidar(lidar_disc_on_generated, label_real)

            generated_lidar_with_weight = generated_lidar_sample * lidar_multiplier
            real_lidar_with_weight = lidar_sample * lidar_multiplier

            camera_gen_error_pixel = criterion_pixel(generated_camera_sample, camera_sample)
            lidar_gen_error_pixel = criterion_pixel(generated_lidar_with_weight, real_lidar_with_weight)

            camera_gen_error_pixel = config.CAMERA_GENERATOR.PIXEL_LAMBDA * camera_gen_error_pixel
            lidar_gen_error_pixel = config.LIDAR_GENERATOR.PIXEL_LAMBDA * lidar_gen_error_pixel

            camera_gen_error = camera_gen_error_disc + camera_gen_error_pixel
            lidar_gen_error = lidar_gen_error_disc + lidar_gen_error_pixel

            cycle_loss = config.TRAIN.CYCLE_LAMBDA * total_cycle_error

            generator_loss = camera_gen_error + lidar_gen_error + cycle_loss
            generator_loss.backward()

            optimizer_lidar_gen.step()
            optimizer_camera_gen.step()

            ################################################################################
            #                           Camera Discriminator
            ################################################################################

            camera_disc.zero_grad()
            camera_disc_real_output = camera_disc(camera_sample, lidar_sample)
            camera_disc_real_error = criterion_cam_to_lidar(camera_disc_real_output, label_real)

            camera_disc_fake_output = camera_disc(generated_camera_sample.detach(), lidar_sample)
            camera_disc_fake_error = criterion_cam_to_lidar(camera_disc_fake_output, label_fake)

            camera_disc_total_error = camera_disc_fake_error + camera_disc_real_error
            camera_disc_total_error.backward()
            optimizer_camera_disc.step()

            ################################################################################
            #                           Lidar Discriminator
            ################################################################################

            lidar_disc.zero_grad()
            lidar_disc_real_output = lidar_disc(lidar_sample, camera_sample)
            lidar_disc_real_error = criterion_cam_to_lidar(lidar_disc_real_output, label_real)

            lidar_disc_fake_output = lidar_disc(generated_lidar_sample.detach(), camera_sample)
            lidar_disc_fake_error = criterion_cam_to_lidar(lidar_disc_fake_output, label_fake)

            lidar_disc_total_error = lidar_disc_fake_error + lidar_disc_real_error
            lidar_disc_total_error.backward()
            optimizer_lidar_disc.step()

            if current_batch % 5 == 0:
                print(
                    '[%d/%d][%d/%d]'
                    % (epoch, config.TRAIN.MAX_EPOCH, current_batch, len(dataloader)))
                print(
                    'Camera to Lidar GAN Loss_D R/F: %.4f/%.4f \t Tot = %.4f  \t '
                    'Loss_G: %.4f \t PixelError: %.4f\t LidarCycleError: %.4f '
                    % (lidar_disc_real_error.item(), lidar_disc_fake_error.item(),
                       lidar_disc_total_error.item(), lidar_gen_error.item(), lidar_gen_error_pixel.item(),
                       lidar_cycle_error.item()))
                print(
                    'Lidar to Camera GAN Loss_D R/F: %.4f/%.4f \t Tot = %.4f  \t '
                    'Loss_G: %.4f \t PixelError: %.4f\t CameraCycleError: %.4f '
                    % (camera_disc_real_error.item(), camera_disc_fake_error.item(),
                       camera_disc_total_error.item(), camera_gen_error.item(), camera_gen_error_pixel.item(),
                       camera_cycle_error.item()))
                print(
                    'Cycle Error Total = %.4f \t  Generator Loss = %.4f'
                    % (cycle_loss.item(), generator_loss.item())
                )

            lidar_gen_losses.append(lidar_gen_error.item())
            lidar_disc_losses.append(lidar_disc_total_error.item())
            camera_gen_losses.append(camera_gen_error.item())
            camera_disc_losses.append(camera_disc_total_error.item())
            generator_losses.append(generator_loss.item())
            cycle_losses.append(cycle_loss.item())

            if current_batch == 0:
                with torch.no_grad():
                    fake_lidar1 = lidar_gen(test_camera1.detach())
                    fake_camera1 = camera_gen(test_lidar1.detach())

                    reconst_camera1 = camera_gen(fake_lidar1.detach())
                    reconst_lidar1 = lidar_gen(fake_camera1.detach())

                    np.savez_compressed(config.TRAIN.EXAMPLE_SAVE_PATH + str(epoch) + "_generated_lidar_1",
                                        data=fake_lidar1[-1].cpu().numpy())
                    np.savez_compressed(config.TRAIN.EXAMPLE_SAVE_PATH + str(epoch) + "_generated_camera_1",
                                        data=fake_camera1[-1].cpu().numpy())
                    np.savez_compressed(config.TRAIN.EXAMPLE_SAVE_PATH + str(epoch) + "_reconstructed_lidar_1",
                                        data=reconst_lidar1[-1].cpu().numpy())
                    np.savez_compressed(config.TRAIN.EXAMPLE_SAVE_PATH + str(epoch) + "_reconstructed_camera_1",
                                        data=reconst_camera1[-1].cpu().numpy())
                    np.savez_compressed(config.TRAIN.EXAMPLE_SAVE_PATH + str(epoch) + "_lidar_1",
                                        data=test_lidar1[-1].cpu().numpy())
                    np.savez_compressed(config.TRAIN.EXAMPLE_SAVE_PATH + str(epoch) + "_camera_1",
                                        data=test_camera1[-1].cpu().numpy())
                    fake_lidar2 = lidar_gen(test_camera2.detach())
                    fake_camera2 = camera_gen(test_lidar2.detach())

                    reconst_camera2 = camera_gen(fake_lidar2.detach())
                    reconst_lidar2 = lidar_gen(fake_camera2.detach())
                    np.savez_compressed(config.TRAIN.EXAMPLE_SAVE_PATH + str(epoch) + "_generated_lidar_2",
                                        data=fake_lidar2[-1].cpu().numpy())
                    np.savez_compressed(config.TRAIN.EXAMPLE_SAVE_PATH + str(epoch) + "_generated_camera_2",
                                        data=fake_camera2[-1].cpu().numpy())
                    np.savez_compressed(config.TRAIN.EXAMPLE_SAVE_PATH + str(epoch) + "_reconstructed_lidar_2",
                                        data=reconst_lidar2[-1].cpu().numpy())
                    np.savez_compressed(config.TRAIN.EXAMPLE_SAVE_PATH + str(epoch) + "_reconstructed_camera_2",
                                        data=reconst_camera2[-1].cpu().numpy())
                    np.savez_compressed(config.TRAIN.EXAMPLE_SAVE_PATH + str(epoch) + "_lidar_2",
                                        data=test_lidar2[-1].cpu().numpy())
                    np.savez_compressed(config.TRAIN.EXAMPLE_SAVE_PATH + str(epoch) + "_camera_2",
                                        data=test_camera2[-1].cpu().numpy())

            del generated_lidar_sample
            del generated_camera_sample
            del camera_sample, lidar_sample
            del label_real, label_fake

        camera_gen_scheduler.step()
        camera_disc_scheduler.step()
        lidar_gen_scheduler.step()
        lidar_disc_scheduler.step()

        plt.figure(figsize=(20, 14))
        plt.title("Generator Losses  During Training")
        plt.plot(lidar_gen_losses, label="Lidar Generator Loss")
        plt.plot(camera_gen_losses, label="Camera Generator Loss")
        plt.plot(cycle_losses, label="Cycle Loss")
        plt.plot(generator_losses, label="Total Generator Loss")

        plt.xlabel("iterations")
        plt.ylabel("Loss")
        plt.legend()
        plt.savefig(config.TRAIN.GRAPH_SAVE_PATH+str(epoch)+"Generator")
        plt.close()

        plt.figure(figsize=(20, 14))
        plt.title("Discriminator Losses  During Training")
        plt.plot(lidar_disc_losses, label="Lidar Generator Loss")
        plt.plot(camera_disc_losses, label="Camera Generator Loss")

        plt.xlabel("iterations")
        plt.ylabel("Loss")
        plt.legend()
        plt.savefig(config.TRAIN.GRAPH_SAVE_PATH + str(epoch)+"Discriminator")
        plt.close()

        if epoch != 0 and epoch % config.TRAIN.SAVE_AT == 0:
            print("Saving Model at ", epoch)
            save_model(config, lidar_gen, camera_gen, lidar_disc, camera_disc, optimizer_lidar_gen,
                       optimizer_camera_gen, optimizer_lidar_disc, optimizer_camera_disc, epoch)

def main(opts):
    load_config(opts.config)
    dataloader = lidar_camera_dataloader(config)
    device = torch.device("cuda:0" if (torch.cuda.is_available() and config.NUM_GPUS > 0) else "cpu")
    train(dataloader, config, device)

if __name__ == "__main__":
    options, args = parser.parse_args()
    main(options)


# standard imports
import logging

# external imports
from chainlib.chain import ChainSpec
from chainlib.eth.unittest.ethtester import create_tester_signer
from chainlib.eth.unittest.base import TestRPCConnection
from chainlib.eth.tx import (
        receipt,
        Tx,
        )
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.gas import (
        OverrideGasOracle,
        Gas,
        )
from chainlib.eth.address import to_checksum_address
from chainlib.eth.block import (
        block_latest,
        block_by_number,
        block_by_hash,
        )
from funga.eth.keystore.dict import DictKeystore
from funga.eth.signer import EIP155Signer
from hexathon import (
        strip_0x,
        add_0x,
        )

# local imports
from erc20_demurrage_token import DemurrageToken
from erc20_demurrage_token.sim.error import TxLimitException

logg = logging.getLogger(__name__)

class DemurrageTokenSimulation:

    def __init__(self, chain_str, settings, redistribute=True, cap=0, actors=1):
        self.chain_spec = ChainSpec.from_chain_str(chain_str)
        self.accounts = []
        self.redistribute = redistribute
        self.keystore = DictKeystore()
        self.signer = EIP155Signer(self.keystore)
        self.eth_helper = create_tester_signer(self.keystore)
        self.eth_backend = self.eth_helper.backend
        self.gas_oracle = OverrideGasOracle(limit=100000, price=1)
        self.rpc = TestRPCConnection(None, self.eth_helper, self.signer)
        for a in self.keystore.list():
            self.accounts.append(add_0x(to_checksum_address(a)))
        settings.sink_address = self.accounts[0]

        self.actors = []
        for i in range(actors):
            idx = i % 10
            address = self.keystore.new()
            self.actors.append(address)
            self.accounts.append(address)

            nonce_oracle = RPCNonceOracle(self.accounts[idx], conn=self.rpc)
            c = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.gas_oracle)
            (tx_hash, o) = c.create(self.accounts[idx], address, 100000 * 1000000)
            self.rpc.do(o)
            o = receipt(tx_hash)
            r = self.rpc.do(o)
            if r['status'] != 1:
                raise RuntimeError('failed gas transfer to account #{}: {} from {}'.format(i, address, self.accounts[idx]))
            logg.info('added actor account #{}: {} block {}'.format(i, address, r['block_number']))

        c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
        (tx_hash, o) = c.constructor(self.accounts[0], settings, redistribute=redistribute, cap=cap)
        self.rpc.do(o)
        o = receipt(tx_hash)
        r = self.rpc.do(o)
        if (r['status'] != 1):
            raise RuntimeError('contract deployment failed')
        self.address = r['contract_address']
        logg.info('deployed contract to {} block {}'.format(self.address, r['block_number']))

        o = block_latest()
        r = self.rpc.do(o)
        self.last_block = r
        self.start_block = self.last_block

        o = block_by_number(r)
        r = self.rpc.do(o)
        self.last_timestamp = r['timestamp']
        self.start_timestamp = self.last_timestamp

        nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
        o = c.decimals(self.address, sender_address=self.accounts[0])
        r = self.rpc.do(o)
        self.decimals = c.parse_decimals(r)

        self.period_seconds = settings.period_minutes * 60

        self.period = 1
        self.period_txs = []
        self.period_tx_limit = self.period_seconds - 1
        self.sink_address = settings.sink_address

        logg.info('intialized at block {} timestamp {} period {} demurrage level {} sink address {} (first address in keystore)'.format(
                self.last_block,
                self.last_timestamp,
                settings.period_minutes,
                settings.demurrage_level,
                settings.sink_address,
                )
            )

        self.eth_helper.disable_auto_mine_transactions()

        self.caller_contract = DemurrageToken(self.chain_spec)
        self.caller_address = self.accounts[0]

    def __check_limit(self):
        if self.period_tx_limit == len(self.period_txs):
            raise TxLimitException('reached period tx limit {}'.format(self.period_tx_limit))

    def __check_tx(self, tx_hash):
        o = receipt(tx_hash)
        rcpt = self.rpc.do(o)
        if rcpt['status'] == 0:
            raise RuntimeError('tx {} (block {} index {}) failed'.format(tx_hash, self.last_block, rcpt['transaction_index']))
        logg.debug('tx {} block {} index {} verified'.format(tx_hash, self.last_block, rcpt['transaction_index']))

    def get_now(self):
        o = block_latest()
        r = self.rpc.do(o)
        o = block_by_number(r, include_tx=False)
        r = self.rpc.do(o)
        return r['timestamp']

    def get_minutes(self):
        t = self.get_now()
        return int((t - self.start_timestamp) / 60)

    def get_start(self):
        return self.start_timestamp

    def get_period(self):
        o = self.caller_contract.actual_period(self.address, sender_address=self.caller_address)
        r = self.rpc.do(o)
        return self.caller_contract.parse_actual_period(r)

    def get_demurrage(self):
        o = self.caller_contract.demurrage_amount(self.address, sender_address=self.caller_address)
        r = self.rpc.do(o)
        logg.info('demrrage amount {}'.format(r))
        return float(self.caller_contract.parse_demurrage_amount(r) / (10 ** 38))

    def get_supply(self):
        o = self.caller_contract.total_supply(self.address, sender_address=self.caller_address)
        r = self.rpc.do(o)
        supply = self.caller_contract.parse_total_supply(r)
        return supply

    def from_units(self, v):
        return v * (10 ** self.decimals)

    def mint(self, recipient, value):
        self.__check_limit()
        nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
        c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.gas_oracle)
        (tx_hash, o) = c.mint_to(self.address, self.accounts[0], recipient, value)
        self.rpc.do(o)
        self.__next_block()
        self.__check_tx(tx_hash)
        self.period_txs.append(tx_hash)
        logg.info('mint {} tokens to {} - {}'.format(value, recipient, tx_hash))
        return tx_hash

    def transfer(self, sender, recipient, value):
        nonce_oracle = RPCNonceOracle(sender, conn=self.rpc)
        c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.gas_oracle)
        (tx_hash, o) = c.transfer(self.address, sender, recipient, value)
        self.rpc.do(o)
        self.__next_block()
        self.__check_tx(tx_hash)
        self.period_txs.append(tx_hash)
        logg.info('transfer {} tokens from {} to {} - {}'.format(value, sender, recipient, tx_hash))
        return tx_hash

    def balance(self, holder, base=False):
        o = None
        if base:
            o = self.caller_contract.base_balance_of(self.address, holder, sender_address=self.caller_address)
        else:
            o = self.caller_contract.balance_of(self.address, holder, sender_address=self.caller_address)
        r = self.rpc.do(o)
        return self.caller_contract.parse_balance(r)

    def __next_block(self):
        hsh = self.eth_helper.mine_block()
        o = block_by_hash(hsh)
        r = self.rpc.do(o)

        for tx_hash in r['transactions']:
            o = receipt(tx_hash)
            rcpt = self.rpc.do(o)
            if rcpt['status'] == 0:
                raise RuntimeError('tx {} (block {} index {}) failed'.format(tx_hash, self.last_block, rcpt['transaction_index']))
            logg.debug('tx {} (block {} index {}) verified'.format(tx_hash, self.last_block, rcpt['transaction_index']))

        logg.debug('now at block {} timestamp {}'.format(r['number'], r['timestamp']))

    def next(self):
        target_timestamp = self.start_timestamp + (self.period * self.period_seconds)
        logg.info('warping to {}, {} from start {}'.format(target_timestamp, target_timestamp - self.start_timestamp, self.start_timestamp))
        self.last_timestamp = target_timestamp

        o = block_latest()
        r = self.rpc.do(o)
        self.last_block = r
        o = block_by_number(r)
        r = self.rpc.do(o)
        cursor_timestamp = r['timestamp'] + 1

        nonce_oracle = RPCNonceOracle(self.accounts[2], conn=self.rpc)
        c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.gas_oracle)

        i = 0
        while cursor_timestamp < target_timestamp:
            logg.info('mining block on {}'.format(cursor_timestamp))
            (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[2])
            self.rpc.do(o)
            self.eth_helper.time_travel(cursor_timestamp + 60)
            self.__next_block()
            o = receipt(tx_hash)
            r = self.rpc.do(o)
            if r['status'] == 0:
                raise RuntimeError('demurrage fast-forward failed on step {} timestamp {} block timestamp {} target {}'.format(i, cursor_timestamp, target_timestamp))
            cursor_timestamp += 60*60 # 1 hour
            o = block_by_number(r['block_number'])
            b = self.rpc.do(o)
            logg.info('block mined on timestamp {} (delta {}) block number {}'.format(b['timestamp'], b['timestamp'] - self.start_timestamp, b['number']))
            i += 1

        (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[2])
        self.rpc.do(o)

        nonce_oracle = RPCNonceOracle(self.accounts[3], conn=self.rpc)
        c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.gas_oracle)
        (tx_hash, o) = c.change_period(self.address, self.accounts[3])
        self.rpc.do(o)
        self.eth_helper.time_travel(target_timestamp + 1)
        self.__next_block()

        o = block_latest()
        r = self.rpc.do(o)
        o = block_by_number(self.last_block)
        r = self.rpc.do(o)
        self.last_block = r['number']
        block_base = self.last_block
        logg.info('block before demurrage execution {} {}'.format(r['timestamp'], r['number']))

        if self.redistribute:
            for actor in self.actors:
                nonce_oracle = RPCNonceOracle(actor, conn=self.rpc)
                c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.gas_oracle)
                (tx_hash, o) = c.apply_redistribution_on_account(self.address, actor, actor)
                self.rpc.do(o)

            nonce_oracle = RPCNonceOracle(self.sink_address, conn=self.rpc)
            c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.gas_oracle)
            (tx_hash, o) = c.apply_redistribution_on_account(self.address, self.sink_address, self.sink_address)
            self.rpc.do(o)

        self.__next_block()

        o = block_latest()
        self.last_block = self.rpc.do(o)

        o = block_by_number(self.last_block)
        r = self.rpc.do(o)
        for tx_hash in r['transactions']:
            o = receipt(tx_hash)
            rcpt = self.rpc.do(o)
            if rcpt['status'] == 0:
                raise RuntimeError('demurrage step failed on block {}'.format(self.last_block))

        self.last_timestamp = r['timestamp']
        logg.debug('next concludes at block {} timestamp {}, {} after start'.format(self.last_block, self.last_timestamp, self.last_timestamp - self.start_timestamp))
        self.period += 1
        self.period_txs = []

        return (self.last_block, self.last_timestamp)


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="EgyVoc", # Replace with your own username
    version="0.0.11",
    author="Marwan Kilani",
    author_email="kilani.edu@gmail.com",
    description="Vocalizer for Ancient Egyptian",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/MKilani/EgyVoc",
    include_package_data=True,
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    install_requires=["py4j"],
    python_requires='>=3.0',
)


# Author-Autodesk Inc.
# Description-Creates a constant radius fillet on the selected edge

import adsk.core, adsk.fusion, traceback

def createFillet():
    ui = None
    try:
        # Get the Application object.
        app = adsk.core.Application.get()

        # Get various top-level Fusion objects.
        ui  = app.userInterface
        design = app.activeProduct
        root = design.rootComponent

        # Have the edge selected and add it to an ObjectCollection.
        selection = ui.selectEntity("Select edge to fillet", "Edges")
        edge = selection.entity
        edgeCollection = adsk.core.ObjectCollection.create()
        edgeCollection.add(edge)

        # Create the FilletInput object.
        fillets = root.features.filletFeatures
        filletInput = fillets.createInput()
        filletInput.addConstantRadiusEdgeSet(edgeCollection, adsk.core.ValueInput.createByString('.25 in'), True)

        # Create the fillet.
        fillet = fillets.add(filletInput)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def main():
    createFillet()

main()


from pydriller import Repository
from github import Github
import json
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
from typing import Optional

g = Github("ADD_YOUR_OWN_API_KEY")

# STEP 1: Go to https://github.com/ipfs/go-ipfs

# STEP 2: Decide on granularity of the project
# Files Go files for now as we can identify the modified files easily

# STEP 3:
#  List of all the Entities of the repository:
# repo = g.get_repo("ipfs/go-ipfs")
# contents = repo.get_contents("")
# while contents:
#     file_content = contents.pop(0)
#     if file_content.type == "dir":
#         contents.extend(repo.get_contents(file_content.path))
#     else:
#         print(file_content)

# Modified files refrence
#  https://pydriller.readthedocs.io/en/latest/modifiedfile.html

# STEP 4:
# Decide on the complexity of the project you want to measure
# Cyclomatic complexity has been implemented in pydriller it relies on Lizard: https://github.com/terryyin/lizard
# How is it used in pydriller: https://pydriller.readthedocs.io/en/latest/deltamaintainability.html
# It measures
#  - the number of functions
#  - the nloc (number of lines of code without comments)
# TODO:
#  - token count of the functions ??
#  - parameter count of the functions ??

# STEP 5:
# Timeframe
start_tag =  'v0.5.0'
end_tag = 'v0.10.0'
# Might tweak the timeframe to be more specific if needed
# Futher explanation will be needed to be added

# STEP 6:
# Measure the complexity of the project and number of changes
# Merge this information to identify the hot spots of the project

class Modified_Files(BaseModel):
    Files: dict = None

class File(BaseModel):
    file_name: str
    commits: Optional[list]

class Commit(BaseModel):
    commit_id: Optional[str]
    nloc: Optional[int]
    complexity: Optional[int]
    changed_methods: Optional[str]

commit_count = 0
number_of_changes = 0
# Empty list to store the changes

all_files = Modified_Files(Files={})

for commit in Repository('../go-ipfs', from_tag=start_tag, to_tag=end_tag).traverse_commits():
    commit_count += 1

    for m in commit.modified_files:
        if m.filename.endswith(".go"):
            number_of_changes += 1

            #  Check there isn't a file with the same name in the dictionary already
            if m.filename in all_files.Files:
                #  If there is, add the commit to the list of commits
                if m.complexity is not None or m.nloc is not None:
                    all_files.Files[m.filename].commits.append(Commit(commit_id=commit.hash, nloc=m.nloc, complexity=m.complexity))
            else:
                #  If not, create a new file with the commit

                all_files.Files[m.filename] = File(file_name=m.filename, commits=[Commit(commit_id=commit.hash, nloc=m.nloc, complexity=m.complexity)])

#  Export Files Dictionary to a json file

print_files = jsonable_encoder(all_files)

with open('files.json', 'w') as outfile:
    json.dump(print_files, outfile)

# Import Dictionary string from the json file

with open('files.json') as json_file:
    Files = json.load(json_file)
    modified_Files =Modified_Files(**Files)

# STEP 7: Visualization of the Hot Spots

# STEP 8: Analysis of 6 of the Hot Spots
# a) Complexity trend analysis
# b) Manaul analysis of the entity names and content


def main():
    # for loop
    print("this is for loop")
    fh = open('text.txt')
    for index, line1 in enumerate(fh.readlines()):
        print(index, line1, end='')

    print()

    c = 'this is a string'
    for i, s in enumerate(c):
        if s == 's' : print('index of s is {}'.format(i))

if __name__ == "__main__": main()


import json
import logging
import os

from uuidworldconverter.utils import logger

class Modify:

    def __init__(self, config, player_map):
        self.__config = config
        self.__uuid_map = player_map

    def modify_json(self, file):
        # if file_name change it's deactivated via config, it exits
        if not file["enable"]:
            return print(f'[{logger.INFO}] [{file["name"]}] Skipped, {file["name"]}: {file["enable"]}')
        file_path = os.path.join(os.getcwd(), self.__config["server_directory"], file["name"])
        # if file doesn't exist, returns with error print
        if not os.path.isfile(file_path):
            return print(f'[{logger.WARNING}] [{file["name"]}] File not found in directory: {file_path}')
        # open, loads & closes file_name in read mode
        read_file = open(file_path, 'r')
        file_json = json.load(read_file)
        read_file.close()
        # for each in file_json
        print(f'[{logger.INFO}] [{file["name"]}] Starting changing file: {file_path}')
        file_change = False
        for player in file_json:
            if player["uuid"] in self.__uuid_map:
                print(
                    f'[{logger.INFO}] [{file["name"]}] {self.__uuid_map[player["uuid"]][1]} : {player["uuid"]} -> {self.__uuid_map[player["uuid"]][0]}')
                player["uuid"] = self.__uuid_map[player["uuid"]][0]
                file_change = True
        # if something was changed
        if file_change:
            try:
                # opens file_name in write mode
                write_file = open(file_path, 'w')
                # save changes into the file in the disk, then closes it
                json.dump(file_json, write_file, indent=4)
                print(f'[{logger.INFO}] [{file["name"]}] Successfully written json to file: {file_path}')
                write_file.close()
            except Exception as e:
                print(f'[{logger.ERROR}] [{file["name"]}] Could not dump json: {file_path}')
                logging.exception(e)
        else:
            print(f'[{logger.WARNING}] [{file["name"]}] Nothing changed so file stays same: {file_path}')

    def modify_folder(self, folder):
        # if folder change it's deactivated via config it exits
        if not folder["enable"]:
            return print(f'[{logger.INFO}] [{folder["name"]}] Skipped, {folder["name"]}: {folder["enable"]}')
        folder_path = os.path.join(os.getcwd(), self.__config["server_directory"], self.__config["world_directory"],
                                   folder["name"])
        # if folder doesn't exist, returns with error print
        if not os.path.isdir(folder_path):
            return print(f'[{logger.ERROR}] [{folder["name"]}] Directory not found: {folder_path}')
        # for each in folder_path
        print(f'[{logger.INFO}] [{folder["name"]}] Starting changing folder files: {folder_path}')
        file_change = False
        for file in os.listdir(folder_path):
            file_split = str(file).split(".")
            file_uuid = file_split[0]
            if file_uuid in self.__uuid_map:
                old_name = os.path.abspath(os.path.join(folder_path, file))
                new_name = os.path.abspath(
                    os.path.join(folder_path, f'{self.__uuid_map[file_uuid][0]}.{file_split[1]}'))
                try:
                    # changes file_name
                    os.rename(old_name, new_name)
                    file_change = True
                    print(f'[{logger.INFO}] [{folder["name"]}] {self.__uuid_map[file_uuid][1]} : {file} -> {self.__uuid_map[file_uuid][0]}.{file_split[1]}')
                    print(f'[{logger.INFO}] [{folder["name"]}] oldpath: {old_name}')
                    print(f'[{logger.INFO}] [{folder["name"]}] newpath: {new_name}')
                except Exception as e:
                    logging.exception(e)
                    print(f'[{logger.ERROR}] [{folder["name"]}] Could not rename file: {file}')
        if not file_change:
            print(f'[{logger.WARNING}] [{folder["name"]}] Nothing changed so folder stays same: {folder_path}')


# for i in range(4):
#     initial_input = input().split(':')
#     identifier = initial_input[1]
#     students_dict[identifier] = {"name": initial_input[0], "course": initial_input[2]}
#
# searched_course = ' '.join(input().split('_'))
#
# for key, value in students_dict.items():
#     if value['course'] == searched_course:
#         print(f"{value['name']} - {key}")
#

students_dict = {}

command = input()
while ":" in command:
    info = command.split(':')
    name, id, course = info[0], info[1], info[2]
    if course not in students_dict:
        students_dict[course] = {}
    students_dict[course][id] = name
    command = input()

searched_course = ' '.join(command.split('_'))

for key, value in students_dict.items():
    if key == searched_course:
        for id, name in value.items():
            print(f"{name} - {id}")

Programming Fundamentals May 2022/Dictionaries/Lab/students.py


# criando um menu de opções
from time import sleep
n1 = float(input('Digite um número qualquer: '))
n2 = float(input('Digite mais um número: '))
print('     [ 1 ] somar\n'
      '     [ 2 ] multiplicar\n'
      '     [ 3 ] maior\n'
      '     [ 4 ] novos números\n'
      '     [ 5 ] sair do programa')
opção = int(input('>>>>> Qual é a sua opção? '))
while opção !=5:
    if opção > 5 or opção == 0:
        print('Opção inválida. Tente novamente!')
    if opção == 1:
        soma = n1+n2
        print('A soma de {:.2f} com {:.2f} resulta em {:.2f}.'.format(n1, n2, soma))
    elif opção == 2:
        multi = n1*n2
        print('A multiplicação de {:.2f} com {:.2f} resulta em {:.2f}.'.format(n1, n2, multi))
    elif opção == 3:
        if n1 > n2:
            maior = n1
        else:
            maior = n2
        print('O maior número entre {:.2f} e {:.2f} é {:.2f}.'.format(n1, n2, maior))
    elif opção == 4:
        print('Informe os números novamente:')
        n1 = float(input('Primeiro valor: '))
        n2 = float(input('Segundo valor: '))
    sleep(1)
    print('-==-'*8)
    print('     [ 1 ] somar\n'
          '     [ 2 ] multiplicar\n'
          '     [ 3 ] maior\n'
          '     [ 4 ] novos números\n'
          '     [ 5 ] sair do programa')
    opção = int(input('>>>>> Qual é a sua opção? '))
if opção == 5:
    print('Finalizando...')
    sleep(1)
    print('-==-'*8)
    print('Fim do programa. Volte sempre!')


import numpy as np
import opfactory
#from cy.sparsemat import matvec
from numba import jit,njit

@njit(fastmath=True)
def matvec(nrows,IA,JA,data,vec,outvec):
    """Sparse matrix-vector multiplication for small(ish) matrices
    """
    d_ind = 0
    for i in range(nrows):
        ncol = IA[i+1]-IA[i]
        for j in range(ncol):
            col_ind = JA[d_ind]
            outvec[i] = outvec[i] + data[d_ind]*vec[col_ind]
            d_ind += 1
    return outvec

class PBasis:
    """General class that handles creation of operators in the primitive basis
    and the application of operators on the single-particle functions. Each
    instance of this class handles the primitive basis for a single-mode and
    can handle combined modes.
    """

    def __init__(self, args, combined=False, sparse=False):
        """
        Parameters
        ----------
        args - list (of lists), arguments that describes the primitive basis
                (or primitive bases in a combined mode)
        combined - bool, whether or not a mode is a combined mode
        sparse - bool, use sparse matrix techniques or not
        """
        self.combined = combined
        if self.combined:
            self.params = list()
        self.params = {}
        self.params['basis'] = args[0].lower()
        self.sparse = sparse

        # set up parameters for basis
        if self.params['basis'] == 'ho':
            self.params['npbf']  = args[1]
            self.params['mass']  = args[2]
            self.params['omega'] = args[3]
            if len(args) == 5:
                self.combined = args[4]
            else:
                self.combined = False
            if self.combined:
                self.make_ops = opfactory.make_ho_ops_combined
                if not isinstance(self.params['mass'], list):
                    mlist = [args[2] for i in range(len(args[1]))]
                    self.params['mass'] = mlist
                if not isinstance(self.params['omega'], list):
                    omlist = [args[2] for i in range(len(args[1]))]
                    self.params['omega'] = omlist
            else:
                self.make_ops = opfactory.make_ho_ops
                #self.grid = make_ho_grid(self.params['npbf'])
        elif self.params['basis'] == 'sinc':
            self.params['npbf'] = args[1]
            self.params['qmin'] = args[2]
            self.params['qmax'] = args[3]
            self.params['dq']   = args[4]
            self.params['mass'] = args[5]
            if isinstance(self.params['npbf'], list):
                self.make_ops = opfactory.make_sinc_ops_combined
            else:
                self.make_ops = opfactory.make_sinc_ops
            self.grid = np.arange(qmin,qmax+dq,dq)
        elif self.params['basis'] == 'plane wave':
            if args[1]%2 == 0:
                self.params['npbf'] = args[1]+1
            else:
                self.params['npbf'] = args[1]
            self.params['nm']   = int((args[1]-1)/2)
            self.params['mass'] = args[2]
            if len(args) == 4:
                self.combined = args[3]
            else:
                self.combined = False
            if self.combined:
                raise NotImplementedError
            else:
                self.make_ops = opfactory.make_planewave_ops
        elif self.params['basis'] == 'plane wave dvr':
            raise NotImplementedError
            #if args[1]%2 == 0:
            #    self.params['npbf'] = args[1]+1
            #else:
            #    self.params['npbf'] = args[1]
            #self.params['nm']   = int((args[1]-1)/2)
            #self.params['mass'] = args[2]
            #if len(args) == 4:
            #    self.combined = args[3]
            #else:
            #    self.combined = False
            #if self.combined:
            #    raise NotImplementedError
            #else:
            #    self.make_ops = opfactory.make_planewave_ops
            #    #self.grid = np.arange(qmin,qmax+dq,dq)
        elif self.params['basis'] == 'radial':
            raise NotImplementedError
            #self.params['npbf'] = args[1]
            #self.params['dq']   = args[2]
            #self.params['mass'] = args[3]
        else:
            raise ValueError("Not a valid basis.")

    def make_operators(self, ops, matrix=None):
        """Creates matrices for all the relevant operators used in the
        calculation. These matrices are then stored in a dictionary called
        self.ops.

        Input
        -----
        ops - list of strings, all the operators that are used for this pbf
        """
        try:
            self.ops
        except:
            self.ops = {}
        if matrix is None:
            matrix = [None for i in range(len(ops))]
        for i,op in enumerate(ops):
            if not op in self.ops:
                if matrix[i] is None:
                    self.ops[op] = self.make_ops(self.params,op,sparse=self.sparse)
                else:
                    self.ops[op] = matrix[i]
            ## TODO make this for custom operators
            #if isinstance(op,str):
            #    self.ops[op] = self.make_ops(params,op)
            #else:
            #    ind = 'c%d'%(count)
            #    count += 1
            #    self.ops[op] = op.copy()

    def operate(self, spf, term):
        """Operate a single-body term on a single spf.
        """
        #return self.ops[term]@spf
        if self.sparse:
            op = self.ops[term]
            outvec = np.zeros(op.nrows, dtype=complex)
            return matvec(op.nrows,op.IA,op.JA,op.data,spf,outvec)
            #return matvec(self.ops[term], spf)
        else:
            return np.dot(self.ops[term], spf)

if __name__ == "__main__":

    # no mode combination
    pbf = PBasis(['ho',22,1.0,1.0])
    pbf.make_operators(['q','KE','q^2'])
    print(pbf.params['basis'])
    print(pbf.params['npbf'])
    print(pbf.params['mass'])
    print(pbf.params['omega'])
    opkeys = pbf.ops.keys()
    for op in opkeys:
        print(op)
        print(pbf.ops[op].shape)
    print('')
    print('')

    # mode combination
    pbf = PBasis(['ho',[6,6],1.0,1.0,True])
    pbf.make_operators(['(q)*(1)','(1)*(q)'])
    print(pbf.params['basis'])
    print(pbf.params['npbf'])
    print(pbf.params['mass'])
    print(pbf.params['omega'])
    opkeys = pbf.ops.keys()
    for op in opkeys:
        print(op)
        print(pbf.ops[op].shape)
    print('')
    print('')

    # mode combination
    pbf = PBasis(['ho',[6,6],[1.0,2.0],[1.0,2.0],True])
    pbf.make_operators(['(q)*(1)','(1)*(q)'])
    print(pbf.params['basis'])
    print(pbf.params['npbf'])
    print(pbf.params['mass'])
    print(pbf.params['omega'])
    opkeys = pbf.ops.keys()
    for op in opkeys:
        print(op)
        print(pbf.ops[op].shape)
    print('')
    print('')


# +-----------------------------------------------+
# | UltiLib For UltiAssassin                      |
# | filename: ultilib.py                          |
# | Created by Ulti - http://thekks.net           |
# | Status: Internal Uses                         |
# | Version: 0.11                                 |
# | Website: http://thekks.net                    |
# | Requirement: EventScript 2.1 or higher        |
# |              SourcePythonExtension (SPE)      |
# | Generic Module                                |
# +-----------------------------------------------+
# |                    Credits                    |
# | Design: KKSNetwork                            |
# | Coding: Ultimatebuster                        |
# +-----------------------------------------------+

VERSION = 0.11

import es, popuplib, playerlib, weaponlib, gamethread
import spe

import random

def createServerVar(varname, value, notify=False, description="A custom cvar."):
    """ Creates a CVAR
        varname - the name of the variable
        value - the default value
        notify - Notify if changed
    """
    sv = es.ServerVar(varname, value)

    if notify:
        es.flags("add", "notify", varname)
    return sv

class EasyPlayer(playerlib.Player):
    """Custom Player Object"""
    def removePrimary(self):
        """Safely removes the primary weapon"""
        if self.primary:
            weaponindex = spe.getWeaponIndex(self.userid, self.primary)
            if weaponindex:
                spe.removeEntityByIndex(weaponindex)
            if self.secondary:
                es.sexec(self.userid, "use %s" % self.secondary)
            else:
                es.sexec(self.userid, "use weapon_knife")

    def removeSecondary(self):
        """Safely removes the secondary weapon"""
        if self.secondary:
            weaponindex = spe.getWeaponIndex(self.userid, self.secondary)
            if weaponindex:
                spe.removeEntityByIndex(weaponindex)
            if self.primary:
                es.sexec(self.userid, "use %s" % self.primary)
            else:
                es.sexec(self.userid, "use weapon_knife")

    def resetColor(self):
        """Resets the player color"""
        self.setColor(255, 255, 255 ,255)

    def replaceSecondaryWeapon(self, sec_wep):
        """Replace the secondary weapon
        sec_wep - the name of a secondary weapon"""
        validwep = ("weapon_glock", "weapon_usp", "weapon_p228", "weapon_deagle", "weapon_elite", "weapon_fiveseven")
        if sec_wep not in validwep:
            raise TypeError("You must have a valid weapon to switch a secondary weapon")
        else:
            currentsec = self.getSecondary()
            if currentsec:
                if currentsec == sec_wep:
                    return
                self.removeSecondary()
            es.give(self.userid, sec_wep)

    def replacePrimaryWeapon(self, prim_wep):
        """Replace the primary weapon
        prim_wep - the name of a primary weapon"""
        validwep = tuple(weaponlib.getWeaponList("#primary"))
        if prim_wep not in validwep:
            raise TypeError("You must have a valid weapon to switch a secondary weapon")
        else:
            currentprim = self.getPrimary()
            if currentprim:
                if currentprim == prim_wep:
                    return
                self.removePrimary()
            es.give(self.userid, prim_wep)

    def giveSmoke(self, amount):
        """Gives smoke grenade to player
        amount - the amount of smoke grenade in integer"""
        self.sg = amount
        if amount > 0:
            es.give(self.userid, "weapon_smokegrenade")

    def giveFlash(self, amount):
        """Gives flash bang to player
        amount - the amount of flashbangs"""
        self.fb = amount
        if amount > 0:
            es.give(self.userid, "weapon_flashbang")

    def giveHE(self, amount):
        """Gives he grenade to player
        amount - the amount of he grenades"""
        self.he = amount
        if amount > 0:
            es.give(self.userid, "weapon_hegrenade")

    def switchTeam(self, teamnum, tmodel="player/t_arctic.mdl", ctmodel="player/ct_gign.mdl"):
        """switch player's team without killing the player
        It will also change the player's model, and move them to their spawn point.
        teamnum - the team number. 2 - Terrorist; 3 - CT; 1 - Spectator
        tmodel - the model of terrorist to change to. Default: player/t_arctic.mdl
        ctmodel - the model of counter terrorist to change to. Default: player/ct_gign.mdl"""
        spe.switchTeam(self.userid, teamnum)
        if teamnum == 2:
            self.moveToSpawn(2)
            self.model = tmodel
        elif teamnum == 3:
            self.moveToSpawn(3)
            self.model = ctmodel

    def moveToSpawn(self, team):
        """Move a player to a spawn point.
        Will also activate noblock on the player in case where 2 player spawn on the same place.
        Deactivates no block in 5 + mp_freezetime
        team - the team number. 2 - Terrorist; 3 - CT"""
        spawnLocations = getSpawnLocations(team)
        if spawnLocations:
            spawnloc = random.choice(spawnLocations)
            es.setpos(self.userid, spawnloc[0], spawnloc[1], spawnloc[2])
            self.noblock(1)
            delaytime = es.ServerVar("mp_freezetime") + 5
            gamethread.delayed(delaytime, self.noblock, (0, ))

    def suicide(self):
        """Make the player commit suicide"""
        es.sexec(self.userid, "kill")

    def unblind(self):
        """Unblinds a player"""
        self.flash(0, 10)

##############################
#### Standalone Functions ####
##############################

def removeMapObjective(bomb=True, hostage=True, buyzone=True, userid=None):
    """ Removes Map Objective.
    bomb - Defines whether to remove bomb objective or not. Default: True
    hostage - Defines whether to remove hostage objective or not. Default: True
    buyzone - Defines whether to remove buying objective or not. Default: True
    userid - A userid for es.fire to use. If not provided (None), the first lowest userid in the game will be used.
    """
    hostageEntity = ('func_hostage_rescue', 'hostage_entity')
    bombEntity = ('func_bomb_target', 'weapon_c4')
    if userid == None:
        userid = es.getUseridList()[0]
    if bomb:
        for entity in bombEntity:
            es.fire(userid, entity, "Kill")

    if hostage:
        for entity in hostageEntity:
            es.fire(userid, entity, "Kill")

    if buyzone:
        es.fire(userid, "func_buyzone", "Kill")

def getSpawnLocations(team):
    """Gets the spawn location using entities. Put them in a tuple and return them.
    team - the team number. 2 - Terrorist; 3 - CT"""
    if team == 2:
        tSpawnEntities = es.createentitylist("info_player_terrorist")
        locations = []
        for key in tSpawnEntities:
            xyz = tuple(tSpawnEntities[key]["CBaseEntity.m_vecOrigin"].split(","))
            locations.append(xyz)
        return tuple(locations)
    elif team == 3:
        ctSpawnEntities = es.createentitylist("info_player_counterterrorist")
        locations = []
        for key in ctSpawnEntities:
            xyz = tuple(ctSpawnEntities[key]["CBaseEntity.m_vecOrigin"].split(","))
            locations.append(xyz)
        return tuple(locations)
    else:
        return False

class WeaponSelectionMenu(object):
    """Weapon selection menu"""
    def __init__(self, userid, weaponslist):
        if playerlib.getPlayer(userid).isbot:
            es.give(userid, "weapon_" + weaponslist[0])
            return None

        if len(weaponslist) == 1:
            es.give(userid, "weapon_" + weaponslist[0])
            return None

        self.userid = userid
        self.weaponslist = weaponslist
        menuid = random.randint(0, 1000)
        if es.exists("popup", "ultilib_weaponmenu%d" % menuid):
            popuplib.delete("ultilib_weaponmenu%d" % menuid)

        weaponMenu = popuplib.create("ultilib_weaponmenu%d" % menuid)
        weaponMenu.addline("Choose a weapon:")
        weaponMenu.addline(" ")
        if len(self.weaponslist) < 9:
            for i in range(0, len(self.weaponslist)):
                weaponMenu.addline("->%d. %s" % (i+1, self.weaponslist[i]))
        else:
            for i in range(0, 9):
                weaponMenu.addline("->%d. %s" % (i+1, self.weaponslist[i]))

        weaponMenu.addline(" ")
        weaponMenu.addline("0. Cancel")
        weaponMenu.menuselect = self.select

        popuplib.send("ultilib_weaponmenu%d" % menuid, userid)

    def select(self, userid, choice, popupid):
        if choice < 11:
            es.give(userid, "weapon_" + self.weaponslist[choice-1])


import argparse
from .outliner import Outliner

def parser_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--file_path",
        "-fp",
        help="Path to the file you want to trace",
        required=True,
        nargs=1,
    )
    parser.add_argument(
        "--object_invoke",
        "-o",
        help="The invoking statement of the object",
        type=str,
        required=True,
        nargs=1,
    )
    parser.add_argument(
        "--mode",
        "-m",
        help="Type of display you want (detailed_data or tree)",
        choices=["tree", "detailed_data"],
        default="tree",
        nargs=1,
    )

    return parser.parse_args()

def main():
    args = parser_arguments()

    instance_class = Outliner(
        args.file_path,
        args.object_invoke,
        args.mode,
    )

    instance_class.run()


import functools
import pandas as pd
from torch.utils.data import DataLoader
import numpy as np
import pandas as pd
from sklearn.metrics import roc_auc_score
from sklearn.linear_model import LinearRegression
from tqdm.auto import tqdm

from context_generator.utils.misc import inf_iterator, BlackHole
from context_generator.utils.data import PaddingCollate
from context_generator.utils.transforms import get_transform
from context_generator.datasets import SkempiDataset

def per_complex_corr(df, pred_attr='ddG_pred', limit=10):
    corr_table = []
    for cplx in df['complex'].unique():
        df_cplx = df.query(f'complex == "{cplx}"')
        if len(df_cplx) < limit:
            continue
        corr_table.append({
            'complex': cplx,
            'pearson': df_cplx[['ddG', pred_attr]].corr('pearson').iloc[0,1],
            'spearman': df_cplx[['ddG', pred_attr]].corr('spearman').iloc[0,1],
        })
    corr_table = pd.DataFrame(corr_table)
    avg = corr_table[['pearson', 'spearman']].mean()
    return avg['pearson'] , avg['spearman']

class SkempiDatasetManager(object):

    def __init__(self, config, num_cvfolds, num_workers=4, logger=BlackHole()):
        super().__init__()
        self.config = config
        self.num_cvfolds = num_cvfolds
        self.train_iterators = []
        self.val_loaders = []
        self.logger = logger
        self.num_workers = num_workers
        for fold in range(num_cvfolds):
            train_iterator, val_loader = self.init_loaders(fold)
            self.train_iterators.append(train_iterator)
            self.val_loaders.append(val_loader)

    def init_loaders(self, fold):
        config = self.config
        dataset_ = functools.partial(
            SkempiDataset,
            csv_path = config.data.csv_path,
            pdb_dir = config.data.pdb_dir,
            cache_dir = config.data.cache_dir,
            num_cvfolds = self.num_cvfolds,
            cvfold_index = fold,
            transform = get_transform(config.data.transform)
        )
        train_dataset = dataset_(split='train')
        val_dataset = dataset_(split='val')

        train_cplx = set([e['complex'] for e in train_dataset.entries])
        val_cplx = set([e['complex'] for e in val_dataset.entries])
        leakage = train_cplx.intersection(val_cplx)
        assert len(leakage) == 0, f'data leakage {leakage}'

        train_loader = DataLoader(
            train_dataset,
            batch_size=config.train.batch_size,
            shuffle=True,
            collate_fn=PaddingCollate(),
            num_workers=self.num_workers
        )
        train_iterator = inf_iterator(train_loader)
        val_loader = DataLoader(
            val_dataset,
            batch_size=config.train.batch_size,
            shuffle=False,
            collate_fn=PaddingCollate(),
            num_workers=self.num_workers
        )
        self.logger.info('Fold %d: Train %d, Val %d' % (fold, len(train_dataset), len(val_dataset)))
        return train_iterator, val_loader

    def get_train_iterator(self, fold):
        return self.train_iterators[fold]

    def get_val_loader(self, fold):
        return self.val_loaders[fold]

def overall_correlations(df):
    pearson = df[['ddG', 'ddG_pred']].corr('pearson').iloc[0,1]
    spearman = df[['ddG', 'ddG_pred']].corr('spearman').iloc[0,1]
    return {
        'overall_pearson': pearson,
        'overall_spearman': spearman,
    }

def percomplex_correlations(df, return_details=False):
    corr_table = []
    for cplx in df['complex'].unique():
        df_cplx = df.query(f'complex == "{cplx}"')
        if len(df_cplx) < 10:
            continue
        corr_table.append({
            'complex': cplx,
            'pearson': df_cplx[['ddG', 'ddG_pred']].corr('pearson').iloc[0,1],
            'spearman': df_cplx[['ddG', 'ddG_pred']].corr('spearman').iloc[0,1],
        })
    corr_table = pd.DataFrame(corr_table)
    average = corr_table[['pearson', 'spearman']].mean()
    out = {
        'percomplex_pearson': average['pearson'],
        'percomplex_spearman': average['spearman'],
    }
    if return_details:
        return out, corr_table
    else:
        return out

def overall_auroc(df):
    score = roc_auc_score(
        (df['ddG'] > 0).to_numpy(),
        df['ddG_pred'].to_numpy()
    )
    return {
        'auroc': score,
    }

def overall_rmse_mae(df):
    true = df['ddG'].to_numpy()
    pred = df['ddG_pred'].to_numpy()[:, None]
    reg = LinearRegression().fit(pred, true)
    pred_corrected = reg.predict(pred)
    rmse = np.sqrt( ((true - pred_corrected) ** 2).mean() )
    mae = np.abs(true - pred_corrected).mean()
    return {
        'rmse': rmse,
        'mae': mae,
    }

def analyze_all_results(df):
    methods = df['method'].unique()
    funcs = [
        overall_correlations,
        overall_rmse_mae,
        overall_auroc,
        percomplex_correlations,
    ]
    analysis = []
    for method in tqdm(methods):
        df_this = df[df['method'] == method]
        result = {
            'method': method,
        }
        for f in funcs:
            result.update(f(df_this))
        analysis.append(result)
    analysis = pd.DataFrame(analysis)
    return analysis

def analyze_all_percomplex_correlations(df):
    methods = df['method'].unique()
    df_corr = []
    for method in tqdm(methods):
        df_this = df[df['method'] == method]
        _, df_corr_this = percomplex_correlations(df_this, return_details=True)
        df_corr_this['method'] = method
        df_corr.append(df_corr_this)
    df_corr = pd.concat(df_corr).reset_index()
    return df_corr

def eval_skempi(df_items, mode, ddg_cutoff=None):
    assert mode in ('all', 'single', 'multiple')
    if mode == 'single':
        df_items = df_items.query('num_muts == 1')
    elif mode == 'multiple':
        df_items = df_items.query('num_muts > 1')

    if ddg_cutoff is not None:
        df_items = df_items.query(f"ddG >= {-ddg_cutoff} and ddG <= {ddg_cutoff}")

    df_metrics = analyze_all_results(df_items)
    df_corr = analyze_all_percomplex_correlations(df_items)
    df_metrics['mode'] = mode
    return df_metrics

def eval_skempi_three_modes(results, ddg_cutoff=None):
    df_all = eval_skempi(results, mode='all', ddg_cutoff=ddg_cutoff)
    df_single = eval_skempi(results, mode='single', ddg_cutoff=ddg_cutoff)
    df_multiple = eval_skempi(results, mode='multiple', ddg_cutoff=ddg_cutoff)
    df_metrics = pd.concat([df_all, df_single, df_multiple], axis=0)
    df_metrics.reset_index(inplace=True, drop=True)
    return df_metrics


from turtle import color
import torch
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import seaborn as sns
import pandas as pd
from torch.utils.data import Dataset, DataLoader

from data import ArielDataset
from data import DATA_DIR

def load_all_data(dataset):
  dataset_values = {}
  for key in dataset[0]['infos'].keys():
    dataset_values[key] = []

  dataset_values['data'] = []
  dataset_values['labels'] = []

  for data in tqdm(dataset):
    for key in data['infos'].keys():
      dataset_values[key].append(data['infos'][key])
    dataset_values['data'].append(data['data'].cpu().detach().numpy())
    dataset_values['labels'].append(data['labels'].cpu().detach().numpy())
  for key, values in dataset_values.items():
    dataset_values[key] = np.array(values)

  return dataset_values

def plot_dataset_infos(data):

  fig, axs = plt.subplots(1, 8)
  fig.suptitle('Ariel Dataset Analysis : Test set')

  keys = list(data.keys())
  keys = ['star_temp', 'star_logg', 'star_rad', 'star_mass', 'star_k_mag', 'period', 'sma', 'incl']
  # keys.remove('file_name')

  for i, key in enumerate(keys):
    bar = axs[i].bar(
      key,
      data[key].mean(),
      yerr=data[key].std(),
      align='center', alpha=0.5, ecolor='black', capsize=10
  )
    for idx, rect in enumerate(bar):
      height = rect.get_height()
      axs[i].text(rect.get_x() + rect.get_width()/2., 0.25*height,
              "Mean: {:.1f}\nMin: {:.1f}\nMax: {:.1f}\nStd: {:.1f}".format(data[key].mean(), data[key].min(), data[key].max(), data[key].std()),
              ha='center', va='bottom')

  # fig.tight_layout(pad=0.01)
  plt.show()

def plot_labels_distrib(data):
  mean = np.mean(data['labels'], axis=0)
  std = np.std(data['labels'], axis=0)
  arr = np.arange(55)
  print(mean.shape, std.shape)
  fig, ax = plt.subplots()
  bar = plt.bar(
    arr,
    mean,
    yerr=std,
    align='center', alpha=0.5, ecolor='black', capsize=10
  )
  for idx, rect in enumerate(bar):
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width()/2., 0.1*height,
            "Min: {:.2f} Max: {:.2f}".format(data['labels'][:,idx].min(), data['labels'][:,idx].max()),
            ha='center', va='bottom', rotation=90)

  plt.xlabel('wavelength')
  plt.ylabel('mean value')
  plt.show()

def plot_light_curves(data, y):
  T, W = data.shape
  data_df = pd.DataFrame(data=data)
  y_df = pd.DataFrame(data=np.tile(y, (T, 1)))
  mean_data_df = data_df.mean(axis=1)
  std_data_df = data_df.std(axis=1)
  low = mean_data_df - std_data_df
  high = mean_data_df + std_data_df
  plt.errorbar(np.arange(T), mean_data_df, yerr=std_data_df, alpha=.5, fmt='-', linewidth=2, elinewidth=0.5, capsize=2, capthick=0.5)
  plt.fill_between(x=np.arange(T), y1=low, y2=high, alpha=.25)
  plt.ylim(low.min(), high.max())
  plt.title('Mean lightcurve. Mean Target : {:.4f}'.format(np.mean(y)))
  plt.show()

if __name__ == '__main__':
  sns.set_theme(style="whitegrid")

  print('Loading data ...')
  dataset = ArielDataset(DATA_DIR, 'train')
  dataloader = DataLoader(dataset, batch_size=1, shuffle=False, num_workers=0)
  print('Done.')
  for i_batch, sample_batched in enumerate(dataloader):
      X = sample_batched['data'].squeeze(0).permute(1, 0).cpu().detach().numpy()
      y = sample_batched['labels'].squeeze(0).cpu().detach().numpy()
      plot_light_curves(X[25:-25], y)


import pandas as pd
from sklearn.metrics import confusion_matrix
from sklearn.cross_validation import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

# Function to perform training with giniIndex.
def train_using_gini(X_train, X_test, y_train):

    # Creating the classifier object
    clf_gini = DecisionTreeClassifier(criterion = "gini",
            random_state = 100,max_depth=3, min_samples_leaf=5)

    # Performing training
    clf_gini.fit(X_train, y_train)
    return clf_gini

# Function to perform training with entropy.
def tarin_using_entropy(X_train, X_test, y_train):

    # Decision tree with entropy
    clf_entropy = DecisionTreeClassifier(
            criterion = "entropy", random_state = 100,
            max_depth = 3, min_samples_leaf = 5)

    # Performing training
    clf_entropy.fit(X_train, y_train)
    return clf_entropy

# Function to make predictions
def prediction(X_test, clf_object):

    # Predicton on test with giniIndex
    y_pred = clf_object.predict(X_test)
    #print("Predicted values:")
    #print(y_pred)
    return y_pred

# Function to calculate accuracy
def cal_accuracy(y_test, y_pred):

    print("Confusion Matrix: ",
        confusion_matrix(y_test, y_pred))

    print ("Accuracy : ",
    accuracy_score(y_test,y_pred)*100)

    #print("Report : ",
    #classification_report(y_test, y_pred))

# Driver code
def main():

    # Building Phase
    names=['sepal.length','sepal.width','petal.length','petal.width']
    dataset=pd.read_csv("iris.csv").values
    x=dataset[:,:4]
    y=dataset[:,4]
    x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3)
    clf_gini = train_using_gini(x_train, x_test, y_train)
    clf_entropy = tarin_using_entropy(x_train, x_test, y_train)

    # Operational Phase
    print("Results Using Gini Index:")

    # Prediction using gini
    y_pred_gini = prediction(x_test, clf_gini)
    cal_accuracy(y_test, y_pred_gini)

    print("Results Using Entropy:")
    # Prediction using entropy
    y_pred_entropy = prediction(x_test, clf_entropy)
    cal_accuracy(y_test, y_pred_entropy)

    from sklearn.externals.six import StringIO
    from sklearn.tree import export_graphviz
    import pydotplus

    dot_data = StringIO()

    export_graphviz(clf_entropy, out_file=dot_data,feature_names=names,filled=True, rounded=True,special_characters=True)

    graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
    graph.write_png('analysis_result.png')

# Calling main function
if __name__=="__main__":
    main()


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

from flask import Flask, render_template, request
from dpjobsearch import app
from config import cell_info, DataListDir, SchedulesDir
from funcoes import FindHostInJob, CountLineOfFile, CountFileInDir
import codecs
import re

from datetime import datetime
now = datetime.now()

@app.route('/', methods=['GET'])
def index():
    NUM_DATALIST = CountFileInDir(DataListDir)
    NUM_SCHEDULE = CountFileInDir(SchedulesDir)

    LISTA_DIARIA = []
    try:
        PARAM = request.args.get('backup')
    except:
        PARAM = ''

    LISTA_HOSTS = []
    VAR = []
    try:
        PARAM = request.args.get('host')
    except:
        PARAM = ''

    file = codecs.open(cell_info, 'r', encoding='utf-16')
    ''' STRING SPLIT '''
    '''-host "mggenesaplp1.energisa.corp" -os "microsoft i386 wNT-6.0-S" -da A.09.00 -ma A.09.00 -ts_core A.09.00'''
    for line in file.readlines():
        HOST = line.split('"')[1]
        OS = line.split('"')[3]
        VERSION = line.split()[7]
        linha = str(HOST+':'+OS)
        LISTA_HOSTS.append(linha)
        VAR.append(HOST)

    file.close()

    JOBS = []
    for host in VAR:
        ''' pega os jobs do host '''
        jobs = FindHostInJob(host)

        ''' verifica se tem que fazer o job hoje '''
        AUX = []
        for job in jobs:
            SchedulesFile = SchedulesDir+'/'+job
            fileSC = codecs.open(SchedulesFile, 'r', encoding='utf-16')
            a = False
            b = False
            c = False
            dia = None
            hora = None
            exclude = None

            for linesc in fileSC.readlines():
                ''' Verifica quando vai ser executado '''
                if '-every' in linesc:
                    a = True
                    continue

                if a == True:
                    dia = linesc.replace("\t", "").replace("\n", "").replace("\r", "").replace("-day ", "")[:-1]

                    a = False
                    b = True
                    continue

                if b:
                    hora = linesc.replace("\t", "").replace("\n", "").replace("\r", "").replace("-at ", "")
                    b = False

                ''' Verifica quando não vai ser executado '''
                if '-exclude' in linesc:
                    c = True
                    continue

                if c:
                    exclude = linesc.replace("\t", "").replace("\n", "").replace("\r", "").replace("-day ", "").replace(" ", "")
                    if exclude.isdigit() and int(exclude) < 10:
                        exclude = '0' + exclude
                    c = False

            ''' Popula o array com as informações do job '''
            AUX.append({"name": job, "dia": dia, "hora": hora, "exclude": exclude})
        JOBS.append({"host": host, "jobs": AUX})

    A = []

    for host in JOBS:
        AUX = []
        for job in host['jobs']:
            ''' Verificação dos jobs diários '''
            if job['dia'] == "":
                if job['exclude'] is not None:
                    if job['exclude'] != now.strftime('%a') and job['exclude'] != now.strftime('%d'):
                        AUX.append(job)
                else:
                    AUX.append(job)

            ''' Verificação dos jobs semanais '''
            if now.strftime('%a') in str(job['dia']):
                if job['exclude'] is not None:
                   if job['exclude'] != now.strftime('%d'):
                       AUX.append(job)
                else:
                    AUX.append(job)

            ''' Verificação dos jobs mensais '''
            if "-month" in str(job['dia']):
                d = job['dia'].replace(' -month', '')
                ''' formata o dia '''
                if d.isdigit() and int(d) < 10:
                    d = '0' + d

                if d == now.strftime('%d'):
                    AUX.append(job)

        A.append({'host': host, 'jobs': AUX})

    return render_template('dash.html', dados = [
    CountLineOfFile(cell_info),
    NUM_DATALIST,
    NUM_SCHEDULE,
    now.day,
    now.month,
    VAR,
    JOBS,
    A])

@app.route('/search', methods=['GET'])
def search():
    LISTA_HOSTS = []
    try:
        PARAM = request.args.get('host')
    except:
        PARAM = ''

    file = codecs.open(cell_info, 'r', encoding='utf-16')
    ''' STRING SPLIT '''
    '''-host "mggenesaplp1.energisa.corp" -os "microsoft i386 wNT-6.0-S" -da A.09.00 -ma A.09.00 -ts_core A.09.00'''
    for line in file.readlines():
        HOST = line.split('"')[1]
        OS = line.split('"')[3]
        VERSION = line.split()[7]
        linha = str(HOST+':'+OS)
        LISTA_HOSTS.append(linha)

    file.close()
    return render_template('search.html', hosts = [PARAM, LISTA_HOSTS])

@app.route('/job', methods=['GET'])
def jobsearch():
    JOBDATA = []
    JOBSC = []
    ''' DATALIST '''
    DATA = ''
    DEV = ''
    POOL = ''
    DIRETORIO = ''

    ''' SCHEDULE '''
    DAY = 0
    RETENTION = 0
    STATUS = True

    try:
        host = request.args.get('host')
    except:
        host = None
        return str('sem host')

    try:
        JOBNAME = request.args.get('detail')
        DataListFile = DataListDir+'/'+JOBNAME
        SchedulesFile = SchedulesDir+'/'+JOBNAME
    except:
        JOBNAME = ''
        DataListFile = None
        SchedulesFile = None

    if host is not None:
        ''' retorna nome de job que tem o host '''
        retorno = FindHostInJob(host)
    else:
        retorno = None

    if retorno is not None and DataListFile is not None and SchedulesFile is not None:
        fileDL = codecs.open(DataListFile, 'r', encoding='utf-16')
        fileSC = codecs.open(SchedulesFile, 'r', encoding='utf-16')

        for line in fileDL.readlines():
            if 'GROUP' in line:
                JOBDATA.append(line)
            if host in line:
                JOBDATA.append(line)
            if 'DEVICE' in line:
                DEV = DEV + ' ' + line.split('_')[1]
            if '-pool' in line:
                if POOL == '':
                    POOL=line.split('"')[1]
            if '"/' in line and \
                    'RECYCLE' not in line and \
                    'System Volume Information' not in line:
                DIRETORIO = DIRETORIO + ' ' + line

        JOBDATA.append(DEV)
        JOBDATA.append(POOL)
        JOBDATA.append(DIRETORIO)
        fileDL.close()

        for linesc in fileSC.readlines():
            if '-days' in linesc and DAY == 0 and RETENTION != 0:
                DAY = linesc.split(' ')[3]
            if '-disable' in linesc:
                STATUS = False
            if 'protection' in linesc and RETENTION == 0:
                RETENTION = linesc.split(' ')[3]

        JOBSC.append(STATUS)
        JOBSC.append(RETENTION)
        JOBSC.append(DAY)
        fileSC.close()

    return render_template('job.html', jobs = [retorno, host, JOBDATA, JOBSC])


# -*- coding: utf-8 -*-
"""
Created on Fri Apr  8 16:54:36 2011

@author: ProfMobius
@version: v1.0
"""
import sys, time
from optparse import OptionParser
from commands import Commands

def main(conffile=None, force=False):
    commands = Commands(conffile)

    commands.logger.info ('== Updating MCP ==')
    commands.downloadupdates(force)

if __name__ == '__main__':
    parser = OptionParser(version='MCP %s' % Commands.MCPVersion)
    parser.add_option('-f', '--force', action='store_true', dest='force', help='force update', default=False)
    parser.add_option('-c', '--config', dest='config', help='additional configuration file')
    (options, args) = parser.parse_args()
    main(options.config, options.force)


# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
	def closestValue(self, root, target):
		"""
		:type root: TreeNode
		:type target: float
		:rtype: int
		"""
		node = root
		result = root.val
		while True:
			if target==node.val: return node.val
			elif target > node.val:
				if target-node.val < abs(result - target):
					result = node.val
				if node.right:
					node = node.right
				else:
					return result
			else:
				if node.val-target < abs(result - target):
					result = node.val
				if node.left:
					node = node.left
				else:
					return result


import pandas as pd
import pandas_profiling
import streamlit as st
from st_on_hover_tabs import on_hover_tabs
import os
from streamlit_pandas_profiling import st_profile_report

st.set_page_config(layout="wide")
st.markdown('<style>' + open('./style/style.css').read() + '</style>', unsafe_allow_html=True)
if os.path.exists('./data/dataset.csv'):
    df = pd.read_csv('./data/dataset.csv', index_col=None)
st.title("Tuto analyse automatisÃ©e de vos donnÃ©es ")
with st.sidebar:
    tabs = on_hover_tabs(tabName=['Charger les donnÃ©es', 'Analyser', 'Exporter'],
                         iconName=['upload file', 'analytics', 'download'], default_choice=0)
    st.image("./style/iiidata.png")

if tabs == 'Charger les donnÃ©es':
    file = st.file_uploader("Chargez vos donnÃ©es")
    separator = st.radio("Si votre dataset ne s'affiche pas correctement,sÃ©lectionner le bon sÃ©parateur",
                         [",", ";"])
    if file:
        df = pd.read_csv(file, index_col=None, sep = separator)
        df.to_csv('./data/dataset.csv', index=None)
        if len(df.columns) >= 2 :
            st.success("DonnÃ©es chargÃ©es correctement, vous pouvez passer Ã  l'analyse")
        else :
            st.error('Il semblerait que vous avez sÃ©lectionnÃ© le mauvais sÃ©parateur')
        st.dataframe(df)

elif tabs == 'Analyser':
    st.header("Rapport d'analyse exploratoire des donnÃ©es")
    profile_df = df.profile_report()
    st_profile_report(profile_df)
    profile_df.to_file("output.html")

elif tabs == 'Exporter':
    with open("output.html", "rb") as f:
        dl = st.download_button("TÃ©lÃ©charger le rapport ðŸ’¾ ", f, "rapport_analyse_data.html")
        st.balloons()
        # if dl :


# coding=utf-8
import sec

LOG_LEVEL = sec.load("log_level", fallback="INFO").upper()

# Flask
DEBUG = False

# Flask-Caching
CACHE_TYPE = "redis"
CACHE_REDIS_URL = sec.load("redis_url", fallback="redis://localhost")
CACHE_DEFAULT_TIMEOUT = 600

# Flack
FLACK_DEFAULT_NAME = "TzBot"
FLACK_TOKEN = sec.load("slack_token")
FLACK_URL_PREFIX = sec.load("flack_prefix", fallback="/")

# App
RESOURCES_DIR = "./resources"


from flask import Flask, request, jsonify
from http import HTTPStatus
from flask_cors import CORS
from sentence_transformers import SentenceTransformer, uti
import json
import numpy as np
import pymysql

def get_db_connection():
    return pymysql.connect(host='', user='', password='', db='', charset='utf8')

app = Flask(__name__)
CORS(app)

# model = SentenceTransformer()
# corpus_embeddings = np.load()

@app.route('/model', methods=['POST', 'OPTIONS'])
def model():
    if request.method == 'POST':
        data = request.get_json()
        query = data['query']
        hits = search_bi_encoder(query)

        db = get_db_connection()
        cursor = db.cursor()

        try:
            sql = "select * from coordinates where docid=" + str(hits[0]) + ";"
            cursor.execute(sql)
            rows = cursor.fetchall()

            for row in rows:
                docid = row[0]
                x = row[1]
                y = row[2]

                if docid == hits[0]:
                    result = {"x" : x, "y" : y, "status":HTTPStatus.OK}
        finally:
            cursor.close()
            db.close()
            return jsonify(result)
    return '', 204

def search_bi_encoder(query):
    model = SentenceTransformer()
    corpus_embeddings = np.load()
    question_embedding = model.encode(query, convert_to_tensor=True)
#    question_embedding = question_embedding.cuda()
    hits = util.semantic_search(question_embedding, corpus_embeddings, top_k=5)
    hits = hits[0]
    hits = sorted(hits, key=lambda x: x['score'], reverse=True)
    hits = [hit['corpus_id'] for hit in hits[:5]]

    return hits

if __name__ == '__main__':
    app.run('0.0.0.0', port=5000, debug=True)


import os
ABSPATH = os.path.abspath('.')

from flask import Flask, redirect, url_for, request, render_template
app = Flask(__name__, template_folder=ABSPATH+'/html')

@app.route('/success/<name>')
def success(name):
    return 'Welcome %s' % name.title()

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        user = request.form['nm']
        return redirect(url_for('success', name=user))
    else:
        user = request.args.get('nm')
        return redirect(url_for('success',name=user))

#@app.route('/loginbypass/<user>')
#def loginbypass(user):
#    return redirect(url_for('success',name=user))

@app.route('/')
def user_page():
    return render_template('flask_3.html')

if __name__ == '__main__':
    app.run(debug = True)