from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from future import standard_library
standard_library.install_aliases()

from builtins import input
from builtins import range
from builtins import object
import itertools
try:
    import MalmoPython
except ImportError:
    import malmo.MalmoPython as MalmoPython

import json
import logging
import math
import os
import random
import sys
import time
import malmoutils
import matplotlib.pyplot as plt
import plotting_1
from collections import deque, namedtuple
import numpy as np
from random import randint
# TODO:调整好maze地图
malmoutils.fix_print()
actionSet = ["movenorth 1", "movesouth 1", "movewest 1", "moveeast 1"]
size = 0
# ===========================================================================================================================
class StateProcessor():
    """
    Process a grid map state.
    """
    def __init__(self):
        pass

    def gridProcess(self, world_state):
        msg = world_state.observations[-1].text
        observations = json.loads(msg)
        grid = observations.get(u'floor13x13', 0)
        # print("observations______", observations)
        # Xpos = observations.get(u'XPos', 0)
        # Zpos = observations.get(u'ZPos', 0)
        obs = np.array(grid)
        # print(grid)
        obs = np.reshape(obs, [13, 13, 1])
        # if int(5+Zpos) > 12 or int(Xpos+5) > 12:
        #     print("Zpos", Zpos)
        #     print("Xpos", Xpos)
        # obs[6][6] = "human"
        # print((int)(5+ Zpos), (int)(Xpos+5))
        # print(obs)
        obs[obs == "air"] = 0
        obs[obs == "beacon"] = 1
        obs[obs == "glass"] = 1
        obs[obs == "carpet"] = 2
        obs[obs == "fire"] = 3
        obs[obs == "netherrack"] = 3
        obs[obs == "sea_lantern"] = 4
        obs[obs == "emerald_block"] = 5
        # obs[obs == "grass"] = 5
        # obs[obs == "human"] = 6

        return obs

    def squeeze_dim(self, state_to_be_squeezed):
        """
        Args:
            state_to_be_squeezed: [size, size, 1]
        Returns:
            state_squeezed: [size, size]
        """
        return np.squeeze(state_to_be_squeezed)

    def change2feature(self, state_to_be_changed):
        """
        Args:
            state_to_be_changed: [size, size]
        Returns:
            state_featured: [N_feature, size, size] # N_feature == 7
        """
        ft1 = np.where(state_to_be_changed=='0', 1, 0)
        ft2 = np.where(state_to_be_changed=='1', 1, 0)
        ft3 = np.where(state_to_be_changed=='2', 1, 0)
        ft4 = np.where(state_to_be_changed=="3", 1, 0)
        ft5 = np.where(state_to_be_changed=="4", 1, 0)
        ft6 = np.where(state_to_be_changed=="5", 1, 0)
        # ft7 = np.where(state_to_be_changed=="6", 1, 0)
        # ft7 = np.zeros(shape=(13, 13))
        # ft7[6][6] = 1   # 表示这里是人
        # ft8 = np.where(state_to_be_changed=="7", 1, 0)
        state_featured = np.array([ft1, ft2, ft3, ft4, ft5, ft6])
        return state_featured

class Estimator(nn.Module): # TODO:之后单独放在一个文件里
    def __init__(self, name, save_dir=None):
        super().__init__()
        self.name = name
        self.save_dir = save_dir
        # define the layers and maybe the conv layers are not so good
        self.conv1 = nn.Conv2d(6, 25, 6, 1)
        self.conv2 = nn.Conv2d(32, 16, 3, 1)
        self.flatten = nn.Flatten()
        self.fc1 = nn.Linear(25*8*8, 200) # (1024, 512)
        self.fc2 = nn.Linear(200, 64)  # (512, 256)
        self.fc3 = nn.Linear(64, len(actionSet)) # (64, len(actionSet))
        # loss
        self.loss = nn.SmoothL1Loss()
        # optimizer
        self.optimizer = torch.optim.Adam(params=self.parameters(), lr=0.01)
        self.fc1.weight.data.normal_(0, 0.1)   # 原来是0, 0.1
        self.fc2.weight.data.normal_(0, 0.1)   # 原来是0, 0.1
        self.fc3.weight.data.normal_(0, 0.1)   # 原来是0, 0.1

    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = self.flatten(x)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.fc2(x)
        x = F.relu(x)
        x = self.fc3(x)
        return x

    def save_model(self, pt_file):
        torch.save(self, f=pt_file)

    def load_model(self, pt_file):
        self = torch.load(pt_file)
        self.eval()

def copy_model_parameters(from_model, to_model):
    """
    Copies the model parameters of one estimator to another.
    Args:
      from_model: Estimator to copy the paramters from
      to_model: Estimator to copy the parameters to
    """
    to_model.load_state_dict(from_model.state_dict())

def make_epsilon_greedy_policy(estimator, nA):
    """
        Creates an epsilon-greedy policy based on a given Q-function approximator and epsilon.
        Args:
            estimator: An estimator that returns q values for a given state
            nA: Number of actions in the environment.
        Returns:
            A function that takes the (sess, state, epsilon) as an argument and returns
            the probabilities for each action in the form of a numpy array of length nA.
    """
    def policy_fn(state, epsilon):
        """
        Args:
            state: shape[4, size, size]
            epsilon: decaying with step_num
        """
        A = [0] * len(actionSet)
        # 第一步，找出最大的q_value预测值的action
        approx_q_values = estimator.forward(torch.unsqueeze(state, 0)).detach().numpy()
        max_a = np.argmax(approx_q_values)
        # 第二步，概率赋值
        ordinary_prob = epsilon / nA
        for i in range(len(actionSet)):
            A[i] = ordinary_prob

        A[max_a] += 1.0 - epsilon
        return A # 数组 1 * 4

    return policy_fn

class Mission_Loader():
    def __init__(self) -> None:
        pass

    def mission_load(self, mission_file):
        with open(mission_file, 'r') as f:
            print("Loading mission from %s" % mission_file)
            mission_xml = f.read()
            my_mission = MalmoPython.MissionSpec(mission_xml, True)
        my_mission.removeAllCommandHandlers()
        my_mission.allowAllDiscreteMovementCommands()
        my_mission.setViewpoint(2)
        return my_mission

    def start_mission(self, my_mission, agent_host, max_retries, my_clients, expID, my_mission_record, agentID):
        for retry in range(max_retries):
            try:
                agent_host.startMission(my_mission, my_clients, my_mission_record, agentID, "%s" % (expID))
                break
            except RuntimeError as e:
                if retry == max_retries - 1:
                    print("Error starting mission:", e)
                    exit(1)
                else:
                    time.sleep(2.5)

    def mission_load_by_num(self, mission_file):
        mazeNum = randint(0, 4)
        mission_file = os.path.join(mission_file, "maze%s.xml" % mazeNum)
        currentMission = mission_file
        return currentMission

    def get_world_state(self, agent_host):
        world_state = agent_host.getWorldState()
        while not world_state.has_mission_begun:
            print(".", end="")
            time.sleep(0.1)
            world_state = agent_host.getWorldState()
        agent_host.sendCommand("look -1")
        agent_host.sendCommand("look -1")
        while world_state.is_mission_running and all(e.text == '{}' for e in world_state.observations):
            world_state = agent_host.peekWorldState()
        return world_state

def show_train_loss(mean_loss, episode_num):
    try:
        train_loss_lines.remove(train_loss_lines[0])
    except Exception:
        pass
    train_loss_lines = plt.plot(episode_num, mean_loss, 'r', lw = 2)
    plt.title('train_loss')
    plt.xlabel('num_episode')
    plt.ylabel('loss')
    plt.legend(['train_loss'])
    if os.path.exists('./train_loss_img/episode_{}.jpg'.format(episode_num[-1]-1)):
        os.remove('./train_loss_img/episode_{}.jpg'.format(episode_num[-1]-1))
    with open(os.path.join('.', 'train_loss_img', 'episode_{}.jpg'.format(episode_num[-1])), 'wb') as f:
        plt.savefig(f) ## 保存图片

def show_success_rate(success_rate, episode_num_20):
    try:
        success_rate_lines.remove(success_rate_lines[0])
    except Exception:
        pass
    success_rate_lines = plt.plot(episode_num_20, success_rate, 'b', lw=2)
    plt.title('success_rate')
    plt.xlabel('num_episode')
    plt.ylabel('success_rate')
    plt.legend(['success_rate'])
    if os.path.exists('./success_rate/episode_{}.jpg'.format(episode_num_20[-1]-20)):
        os.remove('./success_rate/episode_{}.jpg'.format(episode_num_20[-1]-20))
    with open(os.path.join('.', 'success_rate', 'episode_{}.jpg'.format(episode_num_20[-1])), 'wb') as f:
        plt.savefig(f)

def deep_q_learning(agent_host,
                    q_estimator,
                    target_estimator,
                    state_processor,
                    num_episodes,
                    experiment_dir,
                    replay_memory_size=50000,
                    replay_memory_init_size=5000,
                    update_target_estimator_every=1000,
                    discount_factor=0.99,
                    epsilon_start=1.0,
                    epsilon_end=0.1,
                    epsilon_decay_steps=8000,
                    batch_size=32,
                    record_video_every=100):
    """
    Q-Learning algorithm for off-policy TD control using Function Approximation.
    Finds the optimal greedy policy while following an epsilon-greedy policy.
    Args:
        q_estimator: Estimator object used for the q values
        target_estimator: Estimator object used for the targets
        state_processor: A StateProcessor object
        num_episodes: Number of episodes to run for
        experiment_dir: Directory to save Tensorflow summaries in
        replay_memory_size: Size of the replay memory
        replay_memory_init_size: Number of random experiences to sample when initializing
          the reply memory.
        update_target_estimator_every: Copy parameters from the Q estimator to the
          target estimator every N steps
        discount_factor: Gamma discount factor
        epsilon_start: Chance to sample a random action when taking an action.
          Epsilon is decayed over time and this is the start value
        epsilon_end: The final minimum value of epsilon after decaying is done
        epsilon_decay_steps: Number of steps to decay epsilon over
        batch_size: Size of batches to sample from the replay memory
        record_video_every: Record a video every N episodes
    Returns:
        An EpisodeStats object with two numpy arrays for episode_lengths and episode_rewards.
    """
    mission_loader = Mission_Loader()

    mission_file = agent_host.getStringArgument('mission_file')
    mission_file = os.path.join(mission_file, "maze0.xml")
    currentMission = mission_file
    my_mission = mission_loader.mission_load(mission_file=mission_file)

    my_clients = MalmoPython.ClientPool()
    my_clients.add(MalmoPython.ClientInfo('127.0.0.1', 10000))  # add Minecraft machines here as available

    max_retries = 3
    agentID = 0
    total_t = 0     # 在一个episode中的total_t
    expID = 'Deep_q_learning memory'

    Transition = namedtuple("Transition", ["state", "action", "reward", "next_state", "done"])

    # The replay memory
    replay_memory = []

    # Keeps track of useful statistics
    stats = plotting_1.EpisodeStats(
        episode_lengths=np.zeros(num_episodes),
        episode_rewards=np.zeros(num_episodes))

    # Create directories for checkpoints and summaries
    checkpoint_dir = os.path.join(experiment_dir, "checkpoints")
    checkpoint_path = os.path.join(checkpoint_dir, "model")
    monitor_path = os.path.join(experiment_dir, "monitor")
    train_loss_dir = os.path.join('.', 'train_loss_img')
    success_rate_dir = os.path.join('.', 'success_rate')

    if not os.path.exists(checkpoint_dir):
        os.makedirs(checkpoint_dir)
    if not os.path.exists(monitor_path):
        os.makedirs(monitor_path)
    if not os.path.exists(train_loss_dir):
        os.makedirs(train_loss_dir)
    if not os.path.exists(success_rate_dir):
        os.makedirs(success_rate_dir)
    # Create save_dir
    save_dir = os.path.abspath("./save")
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    save_file = os.path.join(save_dir, 'train_pytorch_q_net(2).pt')

    # The epsilon decay schedule
    epsilons = np.linspace(epsilon_start, epsilon_end, epsilon_decay_steps)

    # The policy we're following
    policy = make_epsilon_greedy_policy(
        q_estimator,
        len(actionSet))

    my_mission_record = malmoutils.get_default_recording_object(agent_host,
                                                                "save_%s-rep" % (expID))
    # start mission
    mission_loader.start_mission(my_mission=my_mission, agent_host=agent_host,
                                 max_retries=max_retries, my_clients=my_clients,
                                 expID=expID, my_mission_record=my_mission_record,
                                 agentID=agentID)

    world_state = agent_host.getWorldState()
    while not world_state.has_mission_begun:
        print(".", end="")
        time.sleep(0.1)
        print("Sleeping")
        world_state = agent_host.getWorldState()
        for error in world_state.errors:
            print("Error:", error.text)
    print()
    agent_host.sendCommand("look -1")
    agent_host.sendCommand("look -1")
    print("Populating replay memory...")

    while world_state.is_mission_running and all(e.text == '{}' for e in world_state.observations):
        print("Sleeping....")
        world_state = agent_host.peekWorldState()

    # Populate the replay memory with initial experience

    while world_state.number_of_observations_since_last_state <= 0 and world_state.is_mission_running:
        # print("Sleeping")
        time.sleep(0.1)
        world_state = agent_host.peekWorldState()

    state = state_processor.gridProcess(world_state)  # MALMO ENVIRONMENT Grid world NEEDED HERE/ was env.reset()
    state = state_processor.squeeze_dim(state)
    state = state_processor.change2feature(state)

    ###############################################################################
    # 预先填充replay容器
    ###############################################################################
    for i in range(replay_memory_init_size):
        print("%s th replay memory" % i)
        mission_file = agent_host.getStringArgument('mission_file')
        if i % 20 == 0:
            currentMission = mission_file = mission_loader.mission_load_by_num(mission_file)
        else:
            mission_file = currentMission

        print("Mission File:", mission_file)

        # 生成动作概率、选择动作、执行动作
        action_probs = policy(torch.from_numpy(state.astype('float32')), epsilons[min(total_t, epsilon_decay_steps - 1)])
        action = np.random.choice(np.arange(len(action_probs)), p=action_probs)
        agent_host.sendCommand(actionSet[action])

        # 观察执行后状态
        world_state = agent_host.peekWorldState()
        num_frames_seen = world_state.number_of_video_frames_since_last_state

        # 观察是否完成了状态转移/执行完动作
        while world_state.is_mission_running and world_state.number_of_video_frames_since_last_state == num_frames_seen:
            world_state = agent_host.peekWorldState()
        # done 获取方法：判断是否mission还在执行中
        done = not world_state.is_mission_running

        # 如果mission未结束
        if world_state.is_mission_running:
            # Getting the reward from taking a step, 重新判断world_state
            while world_state.number_of_rewards_since_last_state <= 0:
                time.sleep(0.1)
                world_state = agent_host.peekWorldState()
            # reward~!!
            reward = world_state.rewards[-1].getValue()
            print("1)Just received the reward: %s on action: %s " % (reward, actionSet[action]))

            while world_state.is_mission_running and all(e.text == '{}' for e in world_state.observations):
                world_state = agent_host.peekWorldState()

            # 如果已经done了
            if not world_state.is_mission_running:
                # next_state
                next_state = state
                # done
                done = not world_state.is_mission_running
                print("1)Action: %s, Reward: %s, Done: %s" % (actionSet[action], reward, done))
                # add replay_memory
                replay_memory.append(Transition(state, action, reward, next_state, done))

                # restart mission for next round of memory generation
                my_mission = mission_loader.mission_load(mission_file=mission_file)
                mission_loader.start_mission(my_mission=my_mission, agent_host=agent_host,
                                             max_retries=max_retries, my_clients=my_clients,
                                             expID=expID, my_mission_record=my_mission_record,
                                             agentID=agentID)

                # get new world_state for next new start state
                world_state = mission_loader.get_world_state(agent_host=agent_host)
                state = state_processor.gridProcess(world_state)  # Malmo GetworldState? / env.reset()
                state = state_processor.squeeze_dim(state)
                state = state_processor.change2feature(state)
                # state = np.stack([state] * 4, axis=0)   # shape of [4, size, size]
                # state = np.expand_dims(state, 0)

            # if not done
            else:
                # next_state
                next_state = state_processor.gridProcess(world_state)
                next_state = state_processor.squeeze_dim(next_state)
                next_state = state_processor.change2feature(next_state)
                # next_state = np.append(state[1:, :, :], np.expand_dims(next_state, 0), axis=0)
                # next_state = np.expand_dims(next_state, 0)
                # done
                done = not world_state.is_mission_running
                print("1)Action: %s, Reward: %s, Done: %s" % (actionSet[action], reward, done))
                # add replay_memory
                replay_memory.append(Transition(state, action, reward, next_state, done))

                # update state
                state = next_state

        # 如果mission结束了
        else:
            # reward
            if len(world_state.rewards) > 0:
                reward = world_state.rewards[-1].getValue()
            else:
                reward = 0

            # next_state
            next_state = state

            # done
            done = not world_state.is_mission_running

            print("2)Just received the reward: %s on action: %s " % (reward, actionSet[action]))
            print("2)Action: %s, Reward: %s, Done: %s" % (actionSet[action], reward, done))

            # 加入经验值
            replay_memory.append(Transition(state, action, reward, next_state, done))

            # restart mission for next round of memory generation
            my_mission = mission_loader.mission_load(mission_file=mission_file)
            mission_loader.start_mission(my_mission=my_mission, agent_host=agent_host,
                                            max_retries=max_retries, my_clients=my_clients,
                                            expID=expID, my_mission_record=my_mission_record,
                                            agentID=agentID)

            # get new world_state for next new start state
            world_state = mission_loader.get_world_state(agent_host=agent_host)
            state = state_processor.gridProcess(world_state)  # Malmo GetworldState? / env.reset()
            state = state_processor.squeeze_dim(state)
            state = state_processor.change2feature(state)
        total_t += 1
    print("Finished populating memory")

    #######################################################################################
    # 正式训练
    #######################################################################################
    mean_loss = []
    episode_num = []
    currentMission = mission_file
    num_success = 0
    num_20 = 0
    success_rate = []
    for i_episode in range(num_episodes):
        print("%s-th episode" % i_episode)
        episode_num.append(i_episode+1)

        # Start mission if not the first episode
        if i_episode != 0:
            mission_file = agent_host.getStringArgument('mission_file')
            if i_episode%20 == 0:
                num_success = num_20 = 0
                currentMission = mission_file = mission_loader.mission_load_by_num(mission_file)
            else:
                mission_file = currentMission
            my_mission = mission_loader.mission_load(mission_file=mission_file)
            my_mission.forceWorldReset()
            my_mission.setViewpoint(2)
            my_clients = MalmoPython.ClientPool()
            my_clients.add(MalmoPython.ClientInfo('127.0.0.1', 10000))  # add Minecraft machines here as available

            max_retries = 3
            agentID = 0
            expID = 'Deep_q_learning '
            my_mission_record = malmoutils.get_default_recording_object(agent_host,
                                                                        "save_%s-rep%d" % (expID, i))
            for retry in range(max_retries):
                try:
                    agent_host.startMission(my_mission, my_clients, my_mission_record, agentID, "%s-%d" % (expID, i))
                    break
                except RuntimeError as e:
                    if retry == max_retries - 1:
                        print("Error starting mission:", e)
                        exit(1)
                    else:
                        time.sleep(2.5)
            # restart mission and get world_state
            world_state = agent_host.getWorldState()
            print("Waiting for the mission to start", end=' ')
            while not world_state.has_mission_begun:
                print(".", end="")
                time.sleep(0.1)
                world_state = agent_host.getWorldState()
                for error in world_state.errors:
                    print("Error:", error.text)
        agent_host.sendCommand("look -1")
        agent_host.sendCommand("look -1")
        # 获取初始状态state
        while world_state.is_mission_running and all(e.text == '{}' for e in world_state.observations):
            print("Sleeping....")
            world_state = agent_host.peekWorldState()
        state = state_processor.gridProcess(world_state)  # Malmo GetworldState? / env.reset()
        state = state_processor.squeeze_dim(state)
        state = state_processor.change2feature(state)

        # loss初始化
        loss = None
        total_loss = 0

        # One step in the environment
        for t in itertools.count():
            total_t += 1

            # Epsilon for this time step
            epsilon = epsilons[min(total_t, epsilon_decay_steps - 1)]

            # Maybe update the target estimator
            if total_t % update_target_estimator_every == 0:
                copy_model_parameters(q_estimator, target_estimator)
                print("\nCopied model parameters to target network.")

            # Print out which step we're on, useful for debugging.
            print("\rStep {} ({}) @ Episode {}/{}, loss: {}".format(
                t, total_t, i_episode + 1, num_episodes, loss), end="")
            # sys.stdout.flush()
            if loss != None:
                total_loss += loss
            # 生成动作概率、选择动作、执行动作
            action_probs = policy(torch.from_numpy(state.astype('float32')), epsilon)
            action = np.random.choice(np.arange(len(action_probs)), p=action_probs)
            agent_host.sendCommand(actionSet[action])

            world_state = agent_host.peekWorldState()

            num_frames_seen = world_state.number_of_video_frames_since_last_state
            # 判断是否通过动作变化，成功转移了状态
            while world_state.is_mission_running and world_state.number_of_video_frames_since_last_state == num_frames_seen:
                world_state = agent_host.peekWorldState()

            done = not world_state.is_mission_running
            print(" IS MISSION FINISHED? ", done)

            # 如果还没有停止当前的mission
            if world_state.is_mission_running:
                while world_state.number_of_rewards_since_last_state <= 0:
                    time.sleep(0.1)
                    world_state = agent_host.peekWorldState()
                reward = world_state.rewards[-1].getValue()
                while world_state.is_mission_running and all(e.text == '{}' for e in world_state.observations):
                    world_state = agent_host.peekWorldState()
                # 如果还在运行，则从正在运行的世界环境中读取新状态
                if world_state.is_mission_running:
                    next_state = state_processor.gridProcess(world_state)
                    next_state = state_processor.squeeze_dim(next_state)
                    next_state = state_processor.change2feature(next_state)
                    # next_state = np.append(state[1:, :, :], np.expand_dims(next_state, 0), axis=0)  # TODO:这里为啥是append(state[::1:])
                    # next_state = np.expand_dims(next_state, 0)
                # 如果没有在运行，则当前状态就是下一个状态，同时标记done=True
                else:
                    print("Mission finished prematurely")
                    next_state = state
                    done = not world_state.is_mission_running

                # If our replay memory is full, pop the first element
                if len(replay_memory) == replay_memory_size:
                    replay_memory.pop(0)
                # Save transition to replay memory
                replay_memory.append(Transition(state, action, reward, next_state, done))
                # Update statistics
                stats.episode_rewards[i_episode] += reward
                stats.episode_lengths[i_episode] = t

                ##############
                # 训练
                ##############
                samples = random.sample(replay_memory, batch_size)
                states_batch, action_batch, reward_batch, next_states_batch, done_batch = map(np.array, zip(*samples))
                # all get shape of [batch_size, ?(, ...)]

                q_values_next = q_estimator.forward(torch.from_numpy(next_states_batch.astype('float32'))).detach().numpy()
                # print()
                # print(q_values_next)
                # print()
                best_actions = np.argmax(q_values_next, axis=1)
                # 我们设计target_q值时，是选择的下一个状态的最大value
                # 在DQN中，下一个状态的最大value直接使用target_net来计算并取所有动作对应q值最大者
                # 在DDQN中，为避免估计得到的target值过大，会改为：先用动态更新的q_eval_net来找到下一个状态的最优动作
                #   然后使用target_net来估计当前状态的所有动作对应的q值，然后索引所找到的下一个状态的最优动作对应的target_q值，进而
                # 更新target
                q_values_next_target = target_estimator.forward(torch.from_numpy(next_states_batch.astype('float32')))
                targets_batch = torch.from_numpy(reward_batch.astype('float32')) + torch.from_numpy(np.invert(done_batch).astype(np.float32)) * \
                                discount_factor * q_values_next_target[np.arange(batch_size), best_actions]
                states_batch = np.array(states_batch)
                old_values_batch = q_estimator.forward(torch.from_numpy(states_batch.astype('float32')))[np.arange(batch_size), action_batch]
                # optimizer
                q_estimator.optimizer.zero_grad()
                # loss
                loss = q_estimator.loss(targets_batch, old_values_batch)
                loss.backward()
                q_estimator.optimizer.step()

            if done:
                # get reward
                if len(world_state.rewards)>0:
                    reward = world_state.rewards[-1].getValue()
                    if int(reward) > 200:
                        num_success += 1
                else:
                    print("IDK no reward")
                    reward= 0
                print("Just received the reward: %s on action: %s " % (reward, actionSet[action]))

                # 当前状态即作为下一个状态
                next_state = state

                # 放入memory
                if len(replay_memory) == replay_memory_size:
                    replay_memory.pop(0)
                replay_memory.append(Transition(state, action, reward, next_state, done))

                stats.episode_rewards[i_episode] += reward
                stats.episode_lengths[i_episode] = t

                samples = random.sample(replay_memory, batch_size)
                states_batch, action_batch, reward_batch, next_states_batch, done_batch = map(np.array, zip(*samples))
                # 训练！！
                q_values_next = q_estimator.forward(torch.from_numpy(next_states_batch.astype('float32'))).detach().numpy()
                best_actions = np.argmax(q_values_next, axis=1)
                # 我们设计target_q值时，是选择的下一个状态的最大value
                # 在DQN中，下一个状态的最大value直接使用target_net来计算并取所有动作对应q值最大者
                # 在DDQN中，为避免估计得到的target值过大，会改为：先用动态更新的q_eval_net来找到下一个状态的最优动作
                #   然后使用target_net来估计当前状态的所有动作对应的q值，然后索引所找到的下一个状态的最优动作对应的target_q值，进而
                # 更新target
                q_values_next_target = target_estimator.forward(torch.from_numpy(next_states_batch.astype('float32')))
                targets_batch = torch.from_numpy(reward_batch.astype('float32')) + torch.from_numpy(np.invert(done_batch).astype(np.float32)) * \
                                discount_factor * q_values_next_target[np.arange(batch_size), best_actions]
                states_batch = np.array(states_batch)
                old_values_batch = q_estimator.forward(torch.from_numpy(states_batch.astype('float32')))[np.arange(batch_size), action_batch]
                # optimizer
                q_estimator.optimizer.zero_grad()
                # loss
                loss = q_estimator.loss(targets_batch, old_values_batch)
                loss.backward()
                q_estimator.optimizer.step()
                if t != 0:
                    mean_loss.append(float(total_loss / t))
                show_train_loss(mean_loss=mean_loss, episode_num=episode_num)
                num_20 += 1
                print('----------success_rate in episode {0}/20: {1}%----------'.format(num_20, num_success / num_20 * 100))
                print("End of Episode")
                break

        # 训练结束，保存模型
        if i_episode == num_episodes - 1:
            q_estimator.save_model(save_file)

        yield total_t, plotting_1.EpisodeStats(
            episode_lengths=stats.episode_lengths[:i_episode + 1],
            episode_rewards=stats.episode_rewards[:i_episode + 1])

    return stats

# Main body=======================================================
if __name__ == '__main__':
    agent_host = MalmoPython.AgentHost()

    # Find the default mission file by looking next to the schemas folder:
    schema_dir = None
    try:
        # schema_dir = os.environ['mazes']
        schema_dir = "maze"
    except KeyError:
        print("MALMO_XSD_PATH not set? Check environment.")
        exit(1)
    mission_file = os.path.abspath(schema_dir)  # Integration test path
    if not os.path.exists(mission_file):
        print("Could not find Maze.xml under MALMO_XSD_PATH")
        exit(1)

    # add some args
    agent_host.addOptionalStringArgument('mission_file',
                                        'Path/to/file from which to load the mission.', mission_file)
    agent_host.addOptionalFlag('load_model', 'Load initial model from model_file.')
    agent_host.addOptionalStringArgument('model_file', 'Path to the initial model file', '')
    agent_host.addOptionalFlag('debug', 'Turn on debugging.')
    agent_host.setRewardsPolicy(MalmoPython.RewardsPolicy.LATEST_REWARD_ONLY)
    agent_host.setObservationsPolicy(MalmoPython.ObservationsPolicy.LATEST_OBSERVATION_ONLY)
    malmoutils.parse_command_line(agent_host)

    # Where we save our checkpoints and graphs
    experiment_dir = os.path.abspath("./experiments/{}".format("DeepQLearning"))

    # Create estimators
    q_estimator = Estimator(name="q", save_dir=experiment_dir)
    target_estimator = Estimator(name="target_q")

    # State processor
    state_processor = StateProcessor()
    for t, stats in deep_q_learning(agent_host,
                                    q_estimator,
                                    target_estimator,
                                    state_processor,
                                    num_episodes=500,
                                    experiment_dir=experiment_dir,
                                    replay_memory_size=10000,    # 改为2000？
                                    replay_memory_init_size=50,    # 改为500？
                                    update_target_estimator_every=100,
                                    discount_factor=0.99,
                                    epsilon_start=0.2,
                                    epsilon_end=0.1,
                                    epsilon_decay_steps=8000,
                                    batch_size=32,
                                    record_video_every=100):
        # 迭代器返回
        print("Episode {0}'s Reward is: {1}".format(t, stats.episode_rewards[-1]))

    # something to write here
    # ======================================================================================


import math
import torch
from missile import Missile
import random
from Qtorch import QLearning
import numpy as np
MISSILE_STATUS = {0: "Flying", 1: "Locked", 2: "Intercepted", 3: "Escaped"}
max_lock_missiles = 10 # 每轮锁定导弹的上限

class Environment:
    def __init__(self, missile_list, q_learning=None):
        self.missile_list = missile_list
        self.current_round = 0
        self.intercepted_missiles = []
        self.locked_missiles = []
        self.escaped_missiles = []
        self.num_states = len(missile_list)
        self.total_reward = 0  # 用于存储累计奖励
        self.q_learning = q_learning

    def get_missile_list(self):
        return self.missile_list
    def get_current_round(self):
        return self.current_round
    def set_current_round(self, round):
        self.current_round = round

    def launch_missiles(self):
        game_duration = 16000
        rewards = []  # 创建一个奖励列表
        total_reward = 0
        for elapsed_time in range(game_duration):
            self.update_missile_positions()
            self.update_missile_statuses()
            self.lock_missiles()

             # 在每个回合的开始时检查导弹是否被拦截
            for missile in self.missile_list:
                if missile.get_status() == 1 and self.current_round >= missile.get_intercept_round():
                    distance_to_origin = math.sqrt(missile.x ** 2 + missile.y ** 2)
                    if distance_to_origin < 2:
                        missile.set_status(3)
                    else:
                        missile.set_status(2)
            # Q-learning 策略选择动作
            if self.q_learning is not None and elapsed_time % 8 == 7:
                self.lock_missiles()
                state = [self.current_round] + [m.get_status() for m in self.missile_list]
                actions = [str(m.number) for m in self.missile_list if m.get_status() == 0]
                action = self.q_learning.choose_action(state, actions)
                if action is None:  # 检查 action 是否为 None
                    continue  # 如果为 None，则跳过当前迭代
                action_missile = next((m for m in self.missile_list if m.number == int(action) and m.get_status() == 0), None)
                if action_missile is not None:
                    distance_to_origin = math.sqrt(action_missile.x ** 2 + action_missile.y ** 2)
                    if distance_to_origin < 2:
                        action_missile.set_status(3)
                        q_reward = self.calculate_reward(action_missile) # 使用 calculate_reward 方法计算奖励
                    else:
                        if action_missile.height == 1:
                            q_success_rate = 0.7
                        elif action_missile.height == 2:
                            q_success_rate = 0.8
                        else:
                            q_success_rate = 0.9
                        if random.random() < q_success_rate:
                            action_missile.set_status(2)
                            if action_missile not in self.intercepted_missiles:
                                self.intercepted_missiles.append(action_missile)
                        else:
                            action_missile.set_status(0)  # 导弹拦截失败，状态转为0
                        q_reward = self.calculate_reward(action_missile) # 使用 calculate_reward 方法计算奖励
                    rewards.append(q_reward)  # 将单个导弹的奖励添加到列表中
                    total_reward += q_reward  # 更新总奖励
                    print(f"Round {self.current_round}: Total reward = {total_reward}")  # 在每个回合打印总奖励
                    next_state = [self.current_round + 1] + [m.get_status() for m in self.missile_list]
                    next_available_actions = [str(m.number) for m in self.missile_list if m.get_status() == 0]
                    self.q_learning.update_table(state, action, q_reward, next_state, next_available_actions=next_available_actions)
            self.current_round += 1
            # 判断游戏是否结束
            if self.is_end():
                break
        return rewards  # 游戏结束时返回 rewards 列表

    def get_total_reward(self):
        return self.total_reward

    def calculate_reward(self, missile):
        reward = 0
        height = missile.get_height()
        status = missile.get_status()
        if status == 2 and height == 1:
            reward += 3
        elif status == 2 and height == 2:
            reward += 2
        elif status == 2 and height == 3:
            reward += 1
        elif status == 3:
            reward -= 10
        else:  # 拦截失败
            reward -= 1
        return reward

    def update_missile_positions(self):
        active_missiles = [missile for missile in self.missile_list if missile.get_status() in [0, 1] and missile.launch_round <= self.current_round]
        if not active_missiles:
            return
        positions = torch.tensor([[float(m.x), float(m.y)] for m in self.missile_list], dtype=torch.float)
        positions = positions.float()
        distances = torch.norm(positions, dim=1).float()
        directions = - positions / distances.view(-1, 1)
        new_positions = positions + directions * torch.tensor([float(m.speed) for m in self.missile_list]).view(-1, 1)
        for i, missile in enumerate(self.missile_list):
            if missile.get_status() in [0, 1] and missile.launch_round <= self.current_round:
                missile.set_position(new_positions[i][0].item(), new_positions[i][1].item())
                missile.distance = distances[i].item()

    def lock_missiles(self, actions=None):
        active_missiles = [missile for missile in self.missile_list if
                           missile.get_status() == 0 and missile.launch_round <= self.current_round]
        if not active_missiles:
            return
        if self.q_learning is not None and actions is not None:
            if not actions:
                return
            num_missiles_to_lock = min(len(actions), max_lock_missiles)
            chosen_actions = []
            for _ in range(num_missiles_to_lock):
                state = (self.current_round, len(active_missiles), len(actions))
                action = self.q_learning.choose_action(state, actions)
                chosen_actions.append(action)
                actions.remove(action)

            for action in chosen_actions:
                missile_number = int(action)
                missile = next(
                    (m for m in self.missile_list if m.number == missile_number), None)
                if missile is not None and missile.get_status() == 0:
                    distance_to_origin = math.sqrt(
                        missile.x ** 2 + missile.y ** 2)
                    if distance_to_origin < 2:
                        missile.set_status(3)
                    else:
                        missile.set_status(1)
                    intercept_time = distance_to_origin / (2 * missile.speed)
                    intercept_round = int(intercept_time)
                    missile.set_intercept_round(
                        self.current_round + intercept_round)

                    # 更新Q值
                    if missile.get_status() == 1:
                        state = (self.current_round, len(active_missiles), len(actions) + 1)
                        next_state = (self.current_round, len(active_missiles) - 1, len(actions))
                        next_available_actions = actions.copy()
                        next_available_actions.append(action)
                        reward = self.get_intercept_success_rate(missile) - 1
                        self.q_learning.update_table(state, action, reward, next_state, next_available_actions)

    def update_missile_statuses(self):
        for missile in self.missile_list:
            if missile.get_status() == 0:
                distance_to_origin = math.sqrt(missile.x ** 2 + missile.y ** 2)
                if distance_to_origin < 2:
                    missile.set_status(3)
            elif missile.get_status() == 1:
                distance_to_origin = math.sqrt(missile.x ** 2 + missile.y ** 2)
                intercept_round = int(distance_to_origin / (2 * missile.speed))
                missile.set_intercept_round(intercept_round)
                if self.current_round >= missile.launch_round + intercept_round:
                    if random.random() < self.get_intercept_success_rate(missile):
                        missile.set_status(2)
                        if missile not in self.intercepted_missiles:
                            self.intercepted_missiles.append(missile)
                    else:
                        missile.set_status(3)
                elif missile.get_status() == 2:
                    if missile not in self.intercepted_missiles:
                        self.intercepted_missiles.append(missile)

    def get_intercept_success_rate(self, missile):
        if missile.height == 1:
            return 0.7
        elif missile.height == 2:
            return 0.8
        else:
            return 0.9

    def remove_missiles(self, q_learning):
        intercepted_missiles = []
        escaped_missiles = []
        locked_missiles = []
        for missile in self.missile_list:
            if missile.get_status() == 2:  # Intercepted
                intercepted_missiles.append(missile)
            elif missile.get_status() == 3:  # Escaped
                escaped_missiles.append(missile)
            elif missile.get_status() == 1:  # Locked
                locked_missiles.append(missile)
        return self.missile_list, intercepted_missiles, escaped_missiles, locked_missiles

    def is_end(self):
        if all(missile.get_status() in [2, 3] for missile in self.missile_list):
            return True
        if self.get_current_round() > 20:
            return True
        return False

    def reset(self, missile_list):
        self.missile_list = missile_list
        self.score = 0
        self.locked_missiles = []
        self.intercepted_missiles = []
        self.escaped_missiles = []
        self.current_round = 0
        return self.get_state()

    def get_state(self):
        state = []
        for missile in self.missile_list:
            state.append(missile.status)
        return tuple(state)

    def get_intercepted_missiles(self):
        return self.intercepted_missiles

    def get_available_actions(self):
        actions = []
        for missile in self.missile_list:
            if missile.get_status() == 0:
                actions.append(str(missile.number))
        return actions


import pygame
from bullet import *
red = (250,0,0)
class EnemyTank:
    def __init__(self, x=0, y=0, range_base=70, size=100, color="red"):
        self.size = 50
        self.hp = 100
        self.damage = 40
        self.color = (0,125,0)
        self.x = x
        self.y = y
        self.range_base_detection = 100
        self.range_base_explosion = self.range_base_detection*1.5
        self.already_exploded = False
        self.isDead = False
        self.cooldown = 400
        self.cooldown_explosion = 300
        self.sprite_name = "red2.png"
        self.sprite = pygame.image.load(self.sprite_name).convert_alpha()
        self.showExplosion = False

    def Draw(self, gameDisplay):
        #pygame.draw.rect(gameDisplay, self.color, [self.x - self.size/2, self.y-self.size/2,self.size,self.size])
        gameDisplay.blit(pygame.transform.scale(self.sprite, (self.size, self.size)), (self.Pos_x(), self.Pos_y()))
        if self.showExplosion:
            pygame.draw.rect(gameDisplay, red, [self.x - self.range_base_explosion/2, self.y - self.range_base_explosion/2,self.range_base_explosion,self.range_base_explosion])
            self.showExplosion = False

    def Is_Dead(self):
        if self.hp <= 0: # morreu
            return True
        else: # still breathing
            return False

    def TakeDamage(self,x): # Caso o Whiteflag seja atingido, ele perderá hp
        self.hp -=x

    def Pos_x(self): # Queremos que a posicao utilizada seja com respeito ao canto superior esquerdo da unidade do jogador
        return self.x - self.size/2

    def Pos_y(self):
        return self.y - self.size/2

    def Explosion(self, list_soldiers, gameDisplay):
        if self.cooldown_explosion > 0:
            pygame.draw.rect(gameDisplay, red, [self.x - self.range_base_explosion/2, self.y - self.range_base_explosion/2,self.range_base_explosion,self.range_base_explosion])
            self.cooldown_explosion -= 30
            sound = pygame.mixer.Sound("explosion.wav")
            pygame.mixer.Sound.play(sound)
        else:
            self.hp = 0

            n = len(list_soldiers)
            for j in range(n):
                if self.range_base_explosion/2 >= (((list_soldiers[j].posx-self.x)**2 + (list_soldiers[j].posy-self.y)**2)**0.5):
                    list_soldiers[j].hp = list_soldiers[j].hp - self.damage

    def Update(self, list_soldiers, gameDisplay):
        if self.already_exploded:
            self.cooldown -= 30
            if self.cooldown < 0:
                self.Explosion(list_soldiers, gameDisplay)
                self.showExplosion = True
        else:
            #check range detection
            n = len(list_soldiers)
            for i in range(n):
                if self.range_base_detection/2 >= (((list_soldiers[i].posx-self.x)**2 + (list_soldiers[i].posy-self.y)**2)**0.5):
                    print("Enemy tank explosion!")
                    self.already_exploded = True
                    break


import boto3
import json

def get_security_hub_info():
    """
    A fucntion that gives security hub products info
    """

    conn = boto3.client('ec2')
    regions = [region['RegionName'] for region in conn.describe_regions()['Regions']]
    product_info = []

    for region in regions:
        # not availble in this region
        if region == 'ap-east-1':
            continue

        client = boto3.client('securityhub', region_name=region)
        # get product information
        response = client.describe_products()['Products']

        for res in response:
            req_info = []
            req_info.append(res)
            product_info.append(req_info)

    # convert products list into dictionary
    product_dict = {"Products": product_info}

    # convert dictionary into json
    products_json = json.dumps(product_dict, indent=4, default=str)

    print(products_json)

get_security_hub_info()

Aws Discovery/security and identity/security hub.py


# Define your item pipelines here
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface

from itemadapter import ItemAdapter
from scrapy.exceptions import DropItem

import os
from urllib.parse import urlparse
from scrapy.pipelines.images import ImagesPipeline

DETAILS_STORE = '/home/tales/Documents/bti/pesquisa/PVC16102-2019/PVC16102-2019/scraper/olx/details'

class OlxPipeline:
    # Attributes
    def __init__(self):
        self.id = 0

    def process_item(self, item, spider):
        # Drop the area if has error or if is less than 25m2
        adapter = ItemAdapter(item)
        if adapter.get('area') == 'AREA-ERR' or adapter.get('area') < '25' or adapter.get('area') == '0':
                raise DropItem(f'THE AREA IS MISSING OR IS LESS THAN 25 M2 IN {item}')
        if adapter.get('categoria') == 'T':
                raise DropItem(f'DROP TERRAIN {item}')
        if adapter.get('tipo') == '--' or adapter.get('tipo') == 'A':
                raise DropItem(f'DROP: NO TYPE OR RENT {item}')
        else:
            newItem = {"id" : self.id} # Creates a id for each house
            newItem.update(item)
            item = newItem
            self.id+=1
            with open(os.path.join(DETAILS_STORE, str(item.get('id'))), "w") as f:
                f.write('TÍTULO\n' + item.get('titulo') + '\n' + 'DESCRIÇÃO\n' + item.get('description'))
            return item

class MyImagesPipeline(ImagesPipeline):
    def file_path(self, request, response=None, info=None, *, item=None):
        return str(item.get('id')) + '/' + os.path.basename(urlparse(request.url).path)


import random
import numpy as np
import cv2
import networkx as nx
import matplotlib.pyplot as plt

#Competidores
Grupo_Papaya=["P1","P2","P3","P4","P5"]
Grupo_Coche=["C1","C2","C3","C4","C5"]
Grupo_Amor=["A1","A2","A3","A4","A5"]
Grupo_Netflix=["N1","N2","N3","N4","N5"]
Grupo_Madagascar=["M1","M2","M3","M4","M5"]
print('Carrera 1:',Grupo_Papaya)
print('Carrera 2:',Grupo_Coche)
print('Carrera 3:',Grupo_Amor)
print('Carrera 4:',Grupo_Netflix)
print('Carrera 5:',Grupo_Madagascar)

#Arreglos de Velocidad

Velocidad=np.zeros((5,5))
Dimension=Velocidad.shape
for x in range(Dimension[0]):
    for y in range(Dimension[0]):
        Velocidad[x,y]=random.randint(1,200)
print('\nResultados')

for x in range(5):
    Velocidad[x].sort()

Velocidad=Velocidad[np.argsort(Velocidad[:,-1])]
for x in range(5):
    print("Carrera",5-x,":",Velocidad[x])
#Arreglo Para las primeras 5 carreras
for x in range(5):
    print('ganador grupo',5-x,Velocidad[x,4])
#Arreglo de carrera 6
Carrera_6 =Velocidad[np.argsort(Velocidad[:, -1])]
Carrera_6.sort()
for x in range(5):
  print(Carrera_6[x])
print("Carrera 6:", Carrera_6, "\n")
print("Caballo más rapido: ", int(Carrera_6[4,4]), "Velocidad tope\n")
#Arreglo de carrera 7
Carrera_7= [Carrera_6[2, 4], Carrera_6[3, 4], Carrera_6[3, 3], Carrera_6[4, 3], Carrera_6[4, 2]]
Carrera_7.sort()
print("Carrera 7:", Carrera_7, "\n")
print ("Caballo en segundo lugar: ", int(Carrera_7[4]), "Velocidad tope")
print(" Podio:\n primer lugar: ",int(Carrera_6[4,4]),"en velocidad tope ","\n En segundo lugar: ",int(Carrera_7[4]),"en velocidad tope")

#Grafo
Primer_Lugar= Carrera_6[4,4]
Segundo_Lugar= Carrera_7[4]
Podio = nx.Graph()
Podio.add_edge('Podio ',Primer_Lugar)
Podio.add_edge('Podio ',Segundo_Lugar)
fig, ax = plt.subplots()
nx.draw(Podio, ax=ax, with_labels=True, )
plt.show()

Carrera de CAballos 19310462/Carrera_de_CAballos_19310462.py


from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
import numpy
import os

# If you have the Intel MKL installed,
# you can use the Intel Link Line advisor
# and replace the fftw3 library with MKL
# once you have compiled the FFTW3 interface
# called fftw3xc

libs = ['m', 'fftw3f']
args = ['-std=c99', '-O3','-DuseOMP', '-fopenmp']
sources = ['src/ccfj.pyx', 'src/CrossCorr.cpp','src/FJcpu.cpp']
include = ['include',numpy.get_include()]
linkerargs = ['-Wl,-rpath,lib','-fopenmp']
libdirs = ['lib']

extensions = [
    Extension("ccfj",
              sources=sources,
              include_dirs=include,
              libraries=libs,
              library_dirs=libdirs,
              extra_compile_args=args,
              language='c++',

              extra_link_args=linkerargs)
]

setup(name='ccfj',
      version='0.1',
      author='Zhengbo Li et al.',
      packages=['ccfj'],
      ext_modules=cythonize(extensions),
      )


from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls import include
from django.contrib import admin
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='SoccerHome'),  # home page main app
    path('Teams', views.team_home, name='SoccerTeam'),
    #path('viewTeams', views.create_home, name='SoccerTeamCreate'),
    path('register', views.register_page, name='registerPage'),
    path('Player', views.player_register, name='registerForPlayer'),  # Registering page path for a player
    path('team', views.team_register, name='registerForTeam'),  # Registering page Path for a team
    path('coach', views.coach_register, name='registerForCoach'),  # Registering page Path for a Coaches
    path('referee', views.referee_register, name='registerForReferee'),  # Registering page Path for a Referee
    path('tournament', views.tournament_register, name='registerForTournament'),  # Registering page Path for Tournament
    path('details', views.details_page, name='DetailsPage'),
    path('<int:pk>/PlayerDetail/', views.player_details, name="PlayerDetails"),
    path('<int:pk>/TeamDetail/', views.team_details, name="TeamDetails"),
    path('<int:pk>/CoachDetail/', views.coach_details, name="CoachDetails"),
    path('<int:pk>/RefereeDetail/', views.referee_details, name="RefereeDetails"),
    path('<int:pk>/TournamentDetail/', views.tournament_details, name="TournamentDetails"),
    path('<int:pk>/PlayerDelete/', views.player_delete, name="PlayerDelete"),
    path('<int:pk>/CoachDelete/', views.coach_delete, name="CoachDelete"),
    path('<int:pk>/RefereeDelete/', views.referee_delete, name="RefereeDelete"),
    path('<int:pk>/TournamentDelete/', views.tournament_delete, name="TournamentDelete"),
    path('<int:pk>/TeamDelete/', views.team_delete, name="TeamDelete")
]


"""
Style: main and subroutines
Components: main program and subroutines
Connectors: procedure calls
Constraints: components can't communicate using shared memory (e.g. global
variables)
Gains: change the implementation of a subroutine without affecting the main
program.
"""

from random import randint

import pygame
from pygame.locals import KEYDOWN, QUIT, K_ESCAPE, K_UP, K_DOWN, K_LEFT, K_RIGHT

############################################################

def process_input(prev_direction):
    game_status, direction = 1, prev_direction
    for event in pygame.event.get():
        if event.type == QUIT:
            game_status = 0 # 0 for game over, 1 for play
        if event.type == KEYDOWN:
            key = event.key
            if key == K_ESCAPE:
                game_status = 0
            elif key == K_UP:
                direction = (0, -1)
            elif key == K_DOWN:
                direction = (0, 1)
            elif key == K_LEFT:
                direction = (-1, 0)
            elif key == K_RIGHT:
                direction = (1, 0)
    return game_status, direction

############################################################

def draw_everything(screen, mybox, pellets, borders):
    screen.fill((0, 0, 64))  # dark blue
    [pygame.draw.rect(screen, (0, 191, 255), b) for b in borders]  # # Deep Sky Blue
    [pygame.draw.rect(screen, (255, 192, 203), p) for p in pellets]  # pink
    pygame.draw.rect(screen, (0, 191, 255), mybox)  # Deep Sky Blue
    pygame.display.update()

############################################################

def create_box(dims):
    w, h = dims
    box = pygame.Rect(w / 2, h / 2, 10, 10)  # start in middle of the screen
    direction = 0, 1  # start direction: down
    return box, direction

############################################################

def collide(box, boxes):
    # return True if box collides with any entity in boxes
    return box.collidelist(boxes) != -1

############################################################

def move(box, direction):
    return box.move(direction[0], direction[1])

############################################################

def create_borders(dims, thickness=2):
    w, h = dims
    return [pygame.Rect(0, 0, thickness, h),
            pygame.Rect(0, 0, w, thickness),
            pygame.Rect(w - thickness, 0, thickness, h),
            pygame.Rect(0, h - thickness, w, thickness)]

############################################################

def create_pellet(dims, offset):
    w, h = dims
    return pygame.Rect(randint(offset, w - offset),
                       randint(offset, h - offset), 5, 5)

def create_pellets(dims, qty, offset=10):
    # this is the only subroutine independent of Pygame
    return [create_pellet(dims, offset) for _ in range(qty)]

def eat_and_replace_colliding_pellet(box, pellets, dims, offset=10):
    p_index = box.collidelist(pellets)
    if p_index != -1:  # ate a pellet: grow, and replace a pellet
        pellets[p_index] = create_pellet(dims, offset)
        box.size = box.width * 1.2, box.height * 1.2
    return box, pellets

############################################################

pygame.init()
clock = pygame.time.Clock()

# display
dims = 400, 300
screen = pygame.display.set_mode(dims)

# game objects
borders = create_borders(dims)
pellets = create_pellets(dims, 4)
mybox, direction = create_box(dims)

# game loop
game_status = 1  # 0 for game over, 1 for play
while game_status:

    game_status, direction = process_input(direction)

    mybox = move(mybox, direction)
    if collide(mybox, borders):
        mybox, direction = create_box(dims)
    mybox, pellets = eat_and_replace_colliding_pellet(mybox, pellets, dims)

    draw_everything(screen, mybox, pellets, borders)

    clock.tick(50)  # or sleep(.02) to have the loop Pygame-independent


import torch
import random
import torch.optim
import pandas as pd
import torch.nn as nn

# Load data and remove the junks
df = pd.read_csv('soil_weather_data.csv')
df = df.drop(['state_name','county_name', 'Value_y', 'lon_y','lat_y'], axis=1)

#exclude labels
# exit()

labels = df['Value_x'].tolist()
# labels = (labels - labels.min()) / (labels.max() - labels.min())
# labels = labels.tolist()
df_no_labels = df.drop(['Value_x'], axis=1)

#Normalize the data:
df_no_labels = (df_no_labels - df_no_labels.min()) / (df_no_labels.max() - df_no_labels.min())
aez = df_no_labels['AEZ_classes'].tolist()
mapping = {}
for i, e in enumerate(set(aez)):
	mapping[e] = i
#splitting train and test sets
random.seed(0)
indices = list(range(15408))
random.shuffle(indices)
test_indices = indices[:5000]
train_indices = indices[5000:]

#Loss function
def RRMSE(output, target):
	nom = (output - target)**2
	denom = target ** 2
	return torch.sqrt(torch.mean(nom / denom))

#ML model 1: simple regressor
simple_reg = nn.Linear(274, 1)

#ML model 2: MLP
MLP = nn.Sequential(nn.Linear(274, 256), nn.ReLU(), nn.Linear(256,1))

selected_model = MLP
optimizer = torch.optim.Adam(selected_model.parameters(), lr=0.0001)
epochs = 300
batch_size = 256

def one_hot(v):
	a = []
	for i in range(19):
		a.append(0)
	a[mapping[v]] = 1.
	return a

def retrieve_data(ind):
	inputs = []
	lbl = []
	for i in ind:
		inputs.append(df_no_labels.iloc[i].tolist())
		lbl.append(labels[i])
	for i in range(len(inputs)):
		inputs[i] = inputs[i][:-5] + one_hot(inputs[i][-5]) + inputs[i][-4:]
	inputs = torch.tensor(inputs)
	lbl = torch.tensor(lbl)
	return inputs, lbl.reshape(-1, 1)

best = 100

all_losses = []

def test():
	global best, all_losses
	with torch.no_grad():
		inputs, lbl = retrieve_data(test_indices)
		out = selected_model(inputs)
		loss = RRMSE(out, lbl)
		all_losses.append(loss)
		if loss < best:
			best = loss
			torch.save(selected_model.state_dict(),'model_best_mlp.pt')
		print("RRMSE: ", loss.item(), "Best:", best)

for e in range(epochs):
	start_ind = 0
	end_ind = batch_size
	batch_cnt = 0
	while(start_ind < len(train_indices)):
		batch_ind = train_indices[start_ind:end_ind]
		start_ind = end_ind
		end_ind = min(len(train_indices), end_ind + batch_size)
		inputs, lbl = retrieve_data(batch_ind)

		out = selected_model(inputs)
		loss = RRMSE(out, lbl)
		if batch_cnt % 10 == 0:
			print(e, batch_cnt, loss.item())
		batch_cnt += 1
		loss.backward()
		optimizer.step()

	test()

torch.save(torch.tensor(all_losses), 'mlp-losses.pt')


import json
import os
from typing import Any, Dict, Optional
import zipfile

from leptonai.config import CACHE_DIR
from leptonai._internal.db import DB
from leptonai.util import create_cached_dir_if_needed
from leptonai.registry import Registry

schema_registry = Registry()
type_str_registry = Registry()

class BasePhoton:
    """Base class for all Photons."""

    photon_type = "base"
    """photon_type defines the type of the photon.
    It is used to identify the photon at building and deployment time. Should not be
    changed by user code.
    """

    extra_files = {}
    """Extra files that should be included in the photon.
    Extra files are files that are not part of the model source file, but are
    required to run the model. It takes two forms:
        - a dictionary of {remote_path: local_path} pairs, where the remote_path
          is relative to the cwd of the photon at runtime, and local_path is
          the path pointing to the file in the local file system. If local_path
          is relative, it is relative to the current working directory of the
          local environment.
        - a list of paths (relative to the cwd of the local directory) to be included.
          The remote path will be the same as the local path.
    """

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        for base in cls.__bases__:
            if issubclass(base, BasePhoton) and base.photon_type == cls.photon_type:
                # do not override load if its photon_type the same as parent class
                break
        else:
            type_str_registry.register(cls.photon_type, cls.load)

    def __init__(self, name: str, model: str):
        self._photon_name = name
        self._photon_model = model

    def save(self, path: Optional[str] = None):
        """
        Saves the photon to a local zip file.
        """
        if path is None:
            # If the user has not explicitly specified a path, we will be using the
            # cache directory to store the photon.
            create_cached_dir_if_needed()
            # assuming maximum 1000 versions for now
            for version in range(1000):
                if version == 0:
                    path = str(CACHE_DIR / f"{self._photon_name}.photon")
                else:
                    path = str(CACHE_DIR / f"{self._photon_name}.{version}.photon")
                if not os.path.exists(path):
                    break
            else:
                raise ValueError(
                    f"Can not find a valid path for creating photon {self._photon_name}"
                )
        with zipfile.ZipFile(path, "w") as f:
            metadata = self.metadata
            checked_extra_files = self._extra_files
            metadata["extra_files"] = list(checked_extra_files.keys())
            f.writestr(
                "metadata.json",
                json.dumps(metadata),
            )
            for name, path_or_content in checked_extra_files.items():
                if isinstance(path_or_content, bytes):
                    f.writestr(name, path_or_content)
                    continue
                elif isinstance(path_or_content, str):
                    if os.path.exists(path_or_content):
                        with open(path_or_content, "rb") as extra_file:
                            f.writestr(name, extra_file.read())
                    else:
                        f.writestr(name, path_or_content)
                else:
                    raise ValueError(
                        "extra_files value should be str or bytes, got"
                        f" {path_or_content}"
                    )

        # use path as local id for now
        add_photon(str(path), self._photon_name, self._photon_model, str(path))
        return path

    @staticmethod
    def load(path: str):
        """
        Loads a photon from a local zip file.
        """
        metadata = BasePhoton.load_metadata(path, unpack_extra_files=True)
        photon_type = metadata["type"]
        photon_cls = type_str_registry.get(photon_type)
        if photon_cls is None:
            raise ValueError(
                f"Can not find Photon class for type '{photon_type}', please"
                " make sure the corresponding module is imported."
            )
        with zipfile.ZipFile(path, "r") as photon_file:
            photon = photon_cls(photon_file, metadata)
        return photon

    @staticmethod
    def load_metadata(path: str, unpack_extra_files: bool = False):
        with zipfile.ZipFile(path, "r") as photon_file:
            with photon_file.open("metadata.json") as config:
                metadata = json.load(config)
            if unpack_extra_files:
                for path in metadata["extra_files"]:
                    dirname = os.path.dirname(path)
                    if dirname:
                        os.makedirs(dirname, exist_ok=True)
                    with open(path, "wb") as out, photon_file.open(path) as extra_file:
                        out.write(extra_file.read())
        return metadata

    @property
    def metadata(self) -> Dict[str, Any]:
        return {
            "name": self._photon_name,
            "model": self._photon_model,
            "type": self.photon_type,
        }

    @property
    def _extra_files(self):
        """
        Returns a dict of extra files to be included in the photon.

        If extra_files is a dict, it will be returned directly, with the keys being
        the file path in the photon, and the values being the file path in the buidling
        environment. If extra_files is a list, each list item should be a relative file
        path in the building environment, and the file will be included in the photon with the
        same name and path.
        """

        def recursive_include(dirpath):
            res = {}
            for root, _, files in os.walk(dirpath):
                for file in files:
                    path = os.path.join(root, file)
                    res[path] = path
            return res

        res = {}
        if isinstance(self.extra_files, list):
            # Verify if the file exists too.
            for path in self.extra_files:
                if not os.path.exists(path):
                    raise ValueError(
                        f"Can not find extra file {path} in the building environment"
                    )
                if os.path.isdir(path):
                    res.update(recursive_include(path))
                else:
                    res[path] = path
        elif isinstance(self.extra_files, dict):
            for from_, to_ in self.extra_files.items():
                if not os.path.exists(to_):
                    raise ValueError(
                        f"Can not find extra file {to_} in the building environment"
                    )
                if os.path.isdir(to_):
                    res.update(recursive_include(to_))
                else:
                    res[from_] = to_
        else:
            raise ValueError(
                f"extra_files should be either a dict or a list, got {self.extra_files}"
            )
        return res

    def __str__(self):
        return f"Photon(name={self._photon_name}, model={self._photon_model})"

def add_photon(id: str, name: str, model: str, path: str):
    DB().cursor().execute(
        """INSERT INTO photon (id, name, model, path, creation_time) VALUES (?, ?, ?, ?, strftime('%s','now'))""",
        (id, name, model, path),
    )
    DB().commit()

def find_all_local_photons():
    res = DB().cursor().execute("SELECT * FROM photon ORDER BY creation_time DESC")
    records = res.fetchall()
    return records

def find_local_photon(name: str) -> Optional[str]:
    res = (
        DB()
        .cursor()
        .execute(
            "SELECT * FROM photon WHERE name = ? ORDER BY creation_time DESC", (name,)
        )
    )
    record_or_none = res.fetchone()
    if record_or_none is None:
        return None
    else:
        return record_or_none[0]

def remove_local_photon(name: str, remove_all: bool = False):
    res = (
        DB()
        .cursor()
        .execute(
            "SELECT path FROM photon WHERE name = ? ORDER BY creation_time DESC",
            (name,),
        )
    )
    if remove_all:
        path_or_none = res.fetchone()
        while path_or_none:
            path = path_or_none[0]
            if os.path.exists(path):
                os.remove(path)
            path_or_none = res.fetchone()
        DB().cursor().execute("DELETE FROM photon WHERE name = ?", (name,))
        DB().commit()
    else:
        path_or_none = res.fetchone()
        if path_or_none is None:
            return
        path = path_or_none[0]
        if os.path.exists(path):
            os.remove(path)
        DB().cursor().execute(
            "DELETE FROM photon WHERE name = ? AND path = ?", (name, path)
        )
        DB().commit()


# coding: utf-8

import requests

def file_get_contents(filename, use_include_path=False, context=None, offset=None, maxlen=None):
    if filename.lower().startswith("http://") or filename.lower().startswith("https://"):
        resp = requests.get(filename)
        content = resp.content
        if offset is not None:
            content = content[offset:]
        if maxlen is not None:
            content = content[:maxlen]
        return content.decode("utf-8", errors="ignore")
    else:
        with open(filename) as f:
            return f.read()

if __name__ == '__main__':
    print(file_get_contents("https://www.onevgo.com"))


from django.urls import path
from .views import SignUpView, IndexView, sort, add_receipt, delete_receipt, update_receipt, include_receipts

urlpatterns = [
    path('signup/', SignUpView.as_view(), name='signup'),
    path('', IndexView.as_view(), name='index'),
]

htmx_urlpatterns = [
    path('sort/', sort, name='sort'),
    path('add-receipt/', add_receipt, name='add-receipt'),
    path('delete-receipt/<int:pk>/', delete_receipt, name='delete-receipt'),
    path('update-receipt/<int:pk>/', update_receipt, name='update-receipt'),
    path('include-receipts/', include_receipts, name='include-receipts')
]

urlpatterns += htmx_urlpatterns


import pandas as pd
import json

df = pd.read_excel('public_use-industry-skills-needs.xlsx', 'Industry Skills Needs')

# print(df.isic_section_name.unique())
def data_collection():
    industry_names = df.industry_name.unique()

    sections = {'Mining and quarrying' : 'Mining',
                'Manufacturing' : 'Manufacturing',
                'Information and communication' : 'Info',
                'Financial and insurance activities' : 'Finacial',
                'Professional scientific and technical activities':'Scientific-Technical',
                'Arts, entertainment and recreation':'Arts'}

    # industry_name_dict = {}
    # count = 0
    # for item in industry_names:
    #     industry_name_dict[item] = count
    #     count += 1

    # print(industry_name_dict)
    skill_group_names = df['skill_group_name']
    skill_group_names_dict = {}
    for skill in skill_group_names:
        if skill not in skill_group_names_dict:
            skill_group_names_dict[skill] = 1
        else:
            skill_group_names_dict[skill] += 1

    # print(skill_group_names_dict)

    skill_rank = {}
    for ind in df.index:
        if df['skill_group_name'][ind] not in skill_rank:
            skill_rank[df['skill_group_name'][ind]] = df['skill_group_rank'][ind]
        else:
            skill_rank[df['skill_group_name'][ind]] += df['skill_group_rank'][ind]

    for skill in skill_rank:
        skill_rank[skill] /= skill_group_names_dict[skill]

    # print(skill_rank)

    # with open('skill_name_ranks.json', 'w') as f:
    #     json.dump(skill_rank, f, indent=2)

    # with open('skill_name_frequencies.json', 'w') as f:
    #     json.dump(skill_group_names_dict,f, indent=2)
    with open('skill_name_frequencies.json','r') as f:
        freq = json.load(f)
    with open('skill_name_ranks.json','r') as f:
        ranks = json.load(f)

    print(freq)

def industry_names():
    names = df.industry_name.unique()
    industry_name_options = {}
    for name in names:
        industry_name_options[name] = name #creating this dictionary for the dropdown options

    options = []
    for k in industry_name_options:
        options.append({'label':k,'value':k})

    with open('dropdown_options.json', 'w') as f:
        json.dump(options,f,indent=2)

industry_names()


from pydub import AudioSegment
from pydub.playback import play
import os
import tkinter.messagebox
from tkinter import *
from tkinter import ttk
from tkinter import PhotoImage
from tkinter import filedialog
from pygame import mixer
import pygame
import threading
import pydub
from os.path import isdir, join, basename, splitext

#CONSTANTES
FRAMES_PADY = 30
FRAMES_PADX = 30
CHANNELS = 24
SONGEND = pygame.constants.USEREVENT

class CellPad:
    def __init__(self):
        pass
    busy = False
    file_name = None
    channel = None
    sound = None
    volume = 0.7
    paused = False
    played = False
    stopped = True
    loop = False

    def drawPad(self, master=None):
        pad = ttk.Frame(master, width=115, height=160, relief=GROOVE)
        pad.pack_propagate(0)
        pad.grid_propagate(0)
        pad.pack(side=LEFT, padx=5, pady=10)

        self.topframe = Frame(pad, width=100, height=25, relief=FLAT)
        self.topframe.pack(side=TOP, pady=2)
        self.topframe.pack_propagate(0)

        self.labelframe = Frame(self.topframe, relief=FLAT, width=80, height=25)
        self.labelframe.pack(side=LEFT, pady=2)
        self.labelframe.pack_propagate(0)

        self.closeframe = Frame(self.topframe, relief=FLAT)
        self.closeframe.pack(side=RIGHT, pady=2)

        self.label_name = ttk.Label(self.labelframe, text=os.path.basename(self.file_name if self.busy else "..."), relief=FLAT, font='Times 10 italic')
        self.label_name.pack(pady=2, side=LEFT)

        '''self.closeframe = Frame(pad, relief=FLAT, width=20, height=160)
        self.closeframe.pack(side=RIGHT, pady=5)
        self.closeframe.pack_propagate(0)'''

        self.close = ttk.Button(self.closeframe, text="X", command=self.onClose, width=0)
        self.close.pack(side=RIGHT, anchor=W, fill=X)

        self.playphoto = PhotoImage(file='icons/play-button.png')
        self.playbtn = Button(pad, image=self.playphoto, command=self.onPause)
        self.playbtn.pack()

        self.continuephoto = PhotoImage(file='icons/next.png')

        self.buttons = ttk.Frame(pad, padding='5 5')
        self.buttons.pack()

        self.stopphoto = PhotoImage(file='icons/stop16.png')
        self.stopbtn = ttk.Button(self.buttons, image=self.stopphoto, command=self.onStop)
        self.stopbtn.grid(row=0, column=0)

        self.pausephoto = PhotoImage(file='icons/pause.png')
        '''self.pausebtn = ttk.Button(self.buttons, image=self.pausephoto, command=None)
        self.pausebtn.grid(row=0, column=1)'''

        self.loopphoto = PhotoImage(file='icons/circular-arrow16.png')
        self.loopbtn = ttk.Checkbutton(self.buttons, image=self.loopphoto, style='Toolbutton', command=self.onLoop)
        self.loopbtn.grid(row=0, column=1)
        #self.loopImg = Label(self.buttons, image=self.loopphoto)

        #self.loopbtn["onvalue"] = True
        #self.loopbtn["offvalue"] = False
        #self.loopbtn["variable"] = self.loop

        self.sclBtns = ttk.Frame(pad, padding='5 5')
        self.sclBtns.pack()

        self.scale = ttk.Scale(self.sclBtns, from_=0, to=100, orient=HORIZONTAL, command=self.setVol)
        self.scale.set(70)  # implement the default value of self.scale when music player starts
        self.scale.pack()

    def onLoop(self):
        self.loop = not self.loop
        if self.loop:
            self.stopbtn["state"] = DISABLED
            self.close["state"] = DISABLED
            self.loopbtn.select()
        else:
            self.stopbtn["state"] = ACTIVE
            self.close["state"] = ACTIVE
            self.loopbtn.deselect()
        statusbar["text"] = self.loop

    def getChannel(self):
        for i in range(CHANNELS):
            if not mixer.Channel(i).get_busy():
                return i
            else:
                if i== CHANNELS-1:
                    tkinter.messagebox.showerror('Channels full', 'Number maximum of channels reached')
                    return None

    def onPause(self):
        statusbar["text"] = self.file_name
        if self.stopped:
            self.played = True
            self.stopped = False
            self.paused = False
            self.channel = mixer.Channel(self.getChannel())
            self.sound = mixer.Sound(self.file_name)
            self.channel.play(self.sound)
            playinglist.append(self)
        else:
            if self.paused:
                self.channel.unpause()
                self.paused = False
            else:
                self.channel.pause()
                self.paused = True

        if self.played:
            if self.paused:
                self.playbtn.configure(image=self.continuephoto)
            else:
                self.playbtn.configure(image=self.pausephoto)
        else:
            self.playbtn.configure(image=self.playphoto)

    def onStop(self):
        if not self.stopped:
            self.channel.stop()
            self.stopped = True
            self.playbtn.configure(image=self.playphoto)
        else:
            pass

    def setVol(self, val):
        self.volume = float(val) / 100
        self.channel.set_volume(self.volume)

    def onClose(self):
        if self.stopped:
            statusbar["text"] = "close"
            self.label_name["text"] = "..."
            self.file_name = None
            self.busy = False
        else:
            statusbar["text"] = "close"
            self.loopbtn.grid_forget()
            self.loop = False
            self.loopbtn.grid(row=0, column=1)
            self.channel.stop()
            self.file_name = None
            self.channel = None
            self.sound = None
            self.busy = False
            self.paused = False
            self.played = False
            self.stopped = True
            self.label_name["text"] = "..."
            self.playbtn.configure(image=self.playphoto)

'''class FancyListbox(tkinter.Listbox):

    def __init__(self, parent, mscSongs, evrSongs, efxSongs, *args, **kwargs):
        Listbox.__init__(self, parent, *args, **kwargs)

        self.mscsgs = mscSongs
        self.evrsgs = evrSongs
        self.efxsgs = efxSongs

        self.addmenu = Menu(self)

        self.popup_menu = Menu(self, tearoff=0)
        self.popup_menu.add_cascade(label="Add", menu=self.addmenu)

        self.addmenu.add_command(label="to Music")
        self.addmenu.add_command(label="to Environment")
        self.addmenu.add_command(label="to Effect")

        self.bind("<Button-3>", self.popup) # Button-2 on Aqua

    def popup(self, event):
        try:
            self.popup_menu.tk_popup(event.x_root, event.y_root, 0)
        finally:
            self.popup_menu.grab_release()

    def toMusic(self):
        selection = Listbox.curselection(0)
        selection = int(selection[0])
        for p in self.mscsgs:
            if not p.busy:
                p.file_name = songslist[selection]
                p.labelname.config(text=p.file_name)
                statusbar.config(text=p.file_name)
        pass

    def toEnviro(self):
        pass

    def toEffect(self):
        pass'''

root = Tk()
root.attributes("-fullscreen", True)

#create a statusbar
statusbar = ttk.Label(root, text="Welcome to Melody", relief=SUNKEN, anchor=W, font='Times 10 italic')
statusbar.pack(side=BOTTOM, fill=X)

# Create the menubar
menubar = Menu(root)
root.config(menu=menubar)

# Create the submenu

subMenu = Menu(menubar, tearoff=0)

pygame.init()
mixer.init()
mixer.set_num_channels(CHANNELS)

playinglist = []

songslist = []

# songslist - contains the full path + filename
# songsListbox - contains just the filename
# Fullpath + filename is required to play the music inside play_music load function

def browse_file():
    global filename_path
    filename_path = filedialog.askopenfilename(multiple=True)
    [add_to_songslist(f) for f in filename_path]

def add_to_songslist(filename):
    full_filename = filename
    filename = os.path.basename(full_filename)
    songsListbox.insert(END, filename)
    songslist.append(full_filename)

menubar.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="Open", command=browse_file)
subMenu.add_command(label="Exit", command=root.destroy)

def about_us():
    tkinter.messagebox.showinfo('About SoundPadPy', 'This is a music player build using Python Tkinter by '
                                                    '@mrlactobacilo\nThe code base to this project is '
                                                    '\nhttps://github.com/attreyabhatt/Python-Music-Player')

subMenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Help", menu=subMenu)
subMenu.add_command(label="About Us", command=about_us)

def convertMP3toOGG(songpath, path):
    statusbar["text"] = "Converting " + songpath
    soundname = basename(songpath)
    sound = pydub.AudioSegment.from_mp3(songpath)
    sound.export(join(path, splitext(soundname)[0] + ".ogg"), format="ogg")

def file_conversor():
    path = join(os.getcwd(), "converted_songs")
    try:
        if not isdir(path):
            os.mkdir(path)
    except OSError:
        tkinter.messagebox.showinfo('Error', 'Creation of directory %s failed' % path)
    filespath = filedialog.askopenfilename(multiple=True, title="Files to converter")

    try:
        statusbar["text"] = "Converting files..."
        [convertMP3toOGG(f, path) for f in filespath]
    except OSError:
        tkinter.messagebox.showinfo('Error', 'Conversion of file(s) failed!')
    else:
        tkinter.messagebox.showinfo('Successful', 'Conversion of file(s) done!')

subMenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Converter", menu=subMenu)
subMenu.add_command(label="MP3 to OGG", command=file_conversor)

mixer.init()

# Root Window - StatusBar, LeftFrame, RightFrame
# LeftFrame - The listbox (songslist)
# RightFrame - TopFrame,MiddleFrame and the BottomFrame

musicpads = []
environmentpads = []
effectpads = []

def del_song():
    selected_song = songsListbox.curselection()
    selected_song = int(selected_song[0])
    songsListbox.delete(selected_song)
    songslist.pop(selected_song)

def toMusic():
    selected_song = songsListbox.curselection()
    selected_song = int(selected_song[0])
    idx = 0
    for p in musicpads:
        if not p.busy:
            p.file_name = songslist[selected_song]
            p.label_name["text"] = os.path.basename(p.file_name)
            p.busy = True
            statusbar["text"] = p.file_name
            break
        else:
            if idx == len(musicpads)-1:
                tkinter.messagebox.showerror('Pads full', 'This audio pad is full, please delete a item to add another')
                break
        idx += 1

def toEnviro():
    selected_song = songsListbox.curselection()
    selected_song = int(selected_song[0])
    idx = 0
    for p in environmentpads:
        if not p.busy:
            p.file_name = songslist[selected_song]
            p.label_name["text"] = os.path.basename(p.file_name)
            p.busy = True
            statusbar["text"] = p.file_name
            break
        else:
            if idx == len(environmentpads) - 1:
                tkinter.messagebox.showerror('Pads full', 'This audio pad is full, please delete a item to add another')
                break
        idx += 1

def toEffect():
    selected_song = songsListbox.curselection()
    selected_song = int(selected_song[0])
    idx = 0
    for p in effectpads:
        if not p.busy:
            p.file_name = songslist[selected_song]
            p.label_name["text"] = os.path.basename(p.file_name)
            p.busy = True
            statusbar["text"] = p.file_name
            break
        else:
            if idx == len(effectpads) - 1:
                tkinter.messagebox.showerror('Pads full', 'This audio pad is full, please delete a item to add another')
                break
        idx += 1

def busyMonitory():
    while True:
        for cell in playinglist:
            if not cell.channel.get_busy():
                if cell.loop:
                    cell.channel.play(cell.sound)
                else:
                    cell.stopped = True
                    cell.playbtn.configure(image=cell.playphoto)
                    playinglist.remove(cell)

        #statusbar["text"] = len(playinglist)

# ---LEFT FRAME---
leftframe = Frame(root)
leftframe.pack(side=LEFT, padx=30, pady=30)

addButtons = Frame(leftframe)
addButtons.pack(side=TOP)

addMscBtn = ttk.Button(addButtons, text="+ Musics", command=toMusic)
addMscBtn.pack(side=LEFT)

addEvrBtn = ttk.Button(addButtons, text="+ Environments", command=toEnviro)
addEvrBtn.pack(side=LEFT)

addEfxBtn = ttk.Button(addButtons, text="+ Efects", command=toEffect)
addEfxBtn.pack(side=LEFT)

songsListbox = Listbox(leftframe, width=30, height=30)
songsListbox.pack(pady=10)

addBtn = ttk.Button(leftframe, text="+ Add", command=browse_file)
addBtn.pack(side=LEFT)

delBtn = ttk.Button(leftframe, text="- Del", command=del_song)
delBtn.pack(side=RIGHT)

# ---RIGHT FRAME---
rightframe = Frame(root)
rightframe.pack(pady=10)

# TOP FRAME -> Pads for background musics
topframe = ttk.Frame(rightframe, relief=SUNKEN)
topframe.pack(pady=5)

musiclabel = Label(topframe, relief=FLAT, text="Music Pads")
musiclabel.pack(pady=5)

for i in range(8):
    pad = CellPad()
    musicpads.append(pad)
    musicpads[i].drawPad(topframe)

# MIDDLE FRAME -> Pads for background and environment songs
middleframe = ttk.Frame(rightframe, relief=SUNKEN)
middleframe.pack(pady=5)

environmentlabel = Label(middleframe, relief=FLAT, text="Environment Pads")
environmentlabel.pack(pady=5)

for i in range(8):
    pad = CellPad()
    environmentpads.append(pad)
    environmentpads[i].drawPad(middleframe)

# BOTTOM FRAME -> Pads for effect songs
bottomframe = ttk.Frame(rightframe, relief=SUNKEN)
bottomframe.pack(pady=5)

effectlabel = Label(bottomframe, relief=FLAT, text="Effect Pads")
effectlabel.pack(pady=5)

for i in range(8):
    pad = CellPad()
    effectpads.append(pad)
    effectpads[i].drawPad(bottomframe)

t1 = threading.Thread(target=busyMonitory)
t1.start()

tkinter.messagebox.showinfo('Wellcome to SoundPadPy', 'This sound pad just works with WAV and OGG audio files, '
                             'for another file format please use the converter tool.')

root.mainloop()


from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include
from .views import (
    Overview,
    MultipleOrdersView,
    OrderView,
    OrderItemView,
    SupportView,
    Errand,
    ProfileView,
    InfoView,
    Users,
    SpecificUser,
    EditUser,
    NewAddress,
    EditAdress,
    EditAdresses,
    SettingsView,
    EditCompany,
)

from member.views import CookieSettingsView, changePassword

app_name = 'support'

urlpatterns = [
    path('overview/', Overview.as_view(), name='overview'),
    path('orders/', MultipleOrdersView.as_view(), name='orders'),
    path('vieworder/<slug>', OrderView.as_view(), name='vieworder'),
    path('orderItem/<slug>', OrderItemView.as_view(), name='orderItem'),
    path('support/', SupportView.as_view(), name='support'),
    path('errand/<slug>', Errand.as_view(), name='errand'),
    path('search_users/', Users.as_view(), name='search_users'),
    path('specific_user/', SpecificUser.as_view(), name='specific_user'),
    path('my_profile/', ProfileView.as_view(), name='my_profile'),
    path('my_info/', InfoView.as_view(), name='my_info'),
    path('edit_user/', EditUser.as_view(), name='edit_user'),
    path('edit_company/', EditCompany.as_view(), name='edit_company'),
    path('edit_address/', EditAdress.as_view(), name='edit_address'),
    path('edit_addresses/', EditAdresses.as_view(), name='edit_addresses'),
    path('new_address/', NewAddress.as_view(), name='new_address'),
    path('settings/', CookieSettingsView.as_view(), name='settings'),
    path('user_settings/', SettingsView.as_view(), name='user_settings'),

]

# move cookies for general use to base