import json
import os
import subprocess
from collections import OrderedDict
from copy import deepcopy
from sys import platform

from tabulate import tabulate

from openwpm.config import ConfigEncoder

def parse_http_stack_trace_str(trace_str):
    """Parse a stacktrace string and return an array of dict."""
    stack_trace = []
    frames = trace_str.split("\n")
    for frame in frames:
        try:
            func_name, rest = frame.split("@", 1)
            rest, async_cause = rest.rsplit(";", 1)
            filename, line_no, col_no = rest.rsplit(":", 2)
            stack_trace.append(
                {
                    "func_name": func_name,
                    "filename": filename,
                    "line_no": line_no,
                    "col_no": col_no,
                    "async_cause": async_cause,
                }
            )
        except Exception as exc:
            print("Exception parsing the stack frame %s %s" % (frame, exc))
    return stack_trace

def get_firefox_binary_path():
    """
    If ../../firefox-bin/firefox-bin or os.environ["FIREFOX_BINARY"] exists,
    return it. Else, throw a RuntimeError.
    """
    if "FIREFOX_BINARY" in os.environ:
        firefox_binary_path = os.environ["FIREFOX_BINARY"]
        if not os.path.isfile(firefox_binary_path):
            raise RuntimeError(
                "No file found at the path specified in "
                "environment variable `FIREFOX_BINARY`."
                "Current `FIREFOX_BINARY`: %s" % firefox_binary_path
            )
        return firefox_binary_path

    root_dir = os.path.dirname(__file__) + "/../.."
    if platform == "darwin":
        firefox_binary_path = os.path.abspath(
            root_dir + "/Nightly.app/Contents/MacOS/firefox-bin"
        )
    else:
        firefox_binary_path = os.path.abspath(root_dir + "/firefox-bin/firefox-bin")

    if not os.path.isfile(firefox_binary_path):
        raise RuntimeError(
            "The `firefox-bin/firefox-bin` binary is not found in the root "
            "of the  OpenWPM directory (did you run the install script "
            "(`install.sh`)?). Alternatively, you can specify a binary "
            "location using the OS environment variable FIREFOX_BINARY."
        )
    return firefox_binary_path

def get_version():
    """Return OpenWPM version tag/current commit and Firefox version"""
    try:
        openwpm = subprocess.check_output(
            ["git", "describe", "--tags", "--always"]
        ).strip()
    except subprocess.CalledProcessError:
        ver = os.path.join(os.path.dirname(__file__), "../../VERSION")
        with open(ver, "r") as f:
            openwpm = f.readline().strip()

    firefox_binary_path = get_firefox_binary_path()
    try:
        firefox = subprocess.check_output([firefox_binary_path, "--version"])
    except subprocess.CalledProcessError as e:
        raise RuntimeError("Firefox not found. " " Did you run `./install.sh`?") from e

    ff = firefox.split()[-1]
    return openwpm, ff

def get_configuration_string(manager_params, browser_params, versions):
    """Construct a well-formatted string for {manager,browser}params

    Constructs a pretty printed string of all parameters. The config
    dictionaries are split to try to avoid line wrapping for reasonably
    size terminal windows.
    """

    config_str = "\n\nOpenWPM Version: %s\nFirefox Version: %s\n" % versions
    config_str += "\n========== Manager Configuration ==========\n"

    config_str += json.dumps(
        manager_params.to_dict(),
        sort_keys=True,
        indent=2,
        separators=(",", ": "),
        cls=ConfigEncoder,
    )
    config_str += "\n\n========== Browser Configuration ==========\n"

    print_params = [deepcopy(x.to_dict()) for x in browser_params]
    table_input = list()
    profile_dirs = OrderedDict()
    archive_dirs = OrderedDict()
    js_config = OrderedDict()
    profile_all_none = archive_all_none = True
    for item in print_params:
        browser_id = item["browser_id"]

        # Update print flags
        if item["seed_tar"] is not None:
            profile_all_none = False
        if item["profile_archive_dir"] is not None:
            archive_all_none = False

        # Separate out long profile directory strings
        profile_dirs[browser_id] = str(item.pop("seed_tar"))
        archive_dirs[browser_id] = str(item.pop("profile_archive_dir"))
        js_config[browser_id] = item.pop("cleaned_js_instrument_settings")

        # Copy items in sorted order
        dct = OrderedDict()
        dct["browser_id"] = browser_id
        for key in sorted(item.keys()):
            dct[key] = item[key]
        table_input.append(dct)

    key_dict = OrderedDict()
    counter = 0
    for key in table_input[0].keys():
        key_dict[key] = counter
        counter += 1
    config_str += "Keys:\n"
    config_str += json.dumps(key_dict, indent=2, separators=(",", ": "))
    config_str += "\n\n"
    config_str += tabulate(table_input, headers=key_dict)

    config_str += "\n\n========== JS Instrument Settings ==========\n"
    config_str += json.dumps(js_config, indent=None, separators=(",", ":"))

    config_str += "\n\n========== Input profile tar files ==========\n"
    if profile_all_none:
        config_str += "  No profile tar files specified"
    else:
        config_str += json.dumps(profile_dirs, indent=2, separators=(",", ": "))

    config_str += "\n\n========== Output (archive) profile dirs ==========\n"
    if archive_all_none:
        config_str += "  No profile archive directories specified"
    else:
        config_str += json.dumps(archive_dirs, indent=2, separators=(",", ": "))

    config_str += "\n\n"
    return config_str


import json
from dataclasses import dataclass, field
from typing import Union

import requests

@dataclass
class ManyChatAPI:
    api_base_url = 'https://api.manychat.com/fb/'
    api_key: str
    psid: str
    headers: dict = field(init=False)

    def __post_init__(self):
        self.headers = {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Authorization': f'Bearer {self.api_key}',
        }

    def get_user_info(self) -> dict:
        params = {
            'subscriber_id': self.psid,
        }

        try:
            response = requests.get(
                url=f'{self.api_base_url}subscriber/getInfo',
                headers=self.headers,
                params=params,
                timeout=5,
            )
        except Exception as e:
            results = {
                'status': 'error',
                'message': e,
            }
        else:
            results = json.loads(response.text)

        return results

    def send_content(self, messages: list) -> dict:
        params = {
            'subscriber_id': self.psid,
            'data': {
                'version': 'v2',
                'content': {
                    'messages': [
                        {
                            'type': 'text',
                            'text': message,
                        } for message in messages
                    ]
                }
            },
        }

        try:
            response = requests.post(
                url=f'{self.api_base_url}sending/sendContent',
                headers=self.headers,
                data=json.dumps(params),
                timeout=5,
            )
        except Exception as e:
            results = {
                'status': 'error',
                'message': e,
            }
        else:
            results = json.loads(response.text)

        return results

    def send_flow(self, flow_ns: str) -> dict:
        params = {
            'subscriber_id': self.psid,
            'flow_ns': flow_ns,
        }

        try:
            response = requests.post(
                url=f'{self.api_base_url}sending/sendFlow',
                headers=self.headers,
                data=json.dumps(params),
                timeout=5,
            )
        except Exception as e:
            results = {
                'status': 'error',
                'message': e,
            }
        else:
            results = json.loads(response.text)

        return results

    def set_custom_field_by_name(self,
                                 field_name: str,
                                 field_value: Union[str, int, bool]) -> dict:
        params = {
            'subscriber_id': self.psid,
            'field_name': field_name,
            'field_value': field_value,
        }

        try:
            response = requests.post(
                url=f'{self.api_base_url}subscriber/setCustomFieldByName',
                headers=self.headers,
                data=json.dumps(params),
                timeout=5,
            )
        except Exception as e:
            results = {
                'status': 'error',
                'message': e,
            }
        else:
            results = json.loads(response.text)

        return results


#------------------------------------------------
#other < supper_code
#Dylan Friesen
#October 30th, 2019
#------------------------------------------------

#--------------Dictionaries--------------------

supper = {}

#------------------Definitions-----------------

def add_stuff_s(): #used to add stuff to the supper list
    stop = False
    while stop == False:
        stuff = input("What do you want to add to to your shopping list?").lower()
        if stuff == "stop":
            stop = True
        elif stuff != "stop":
            value = input("How many or description?")
            print("adding", value, stuff)
            supper[stuff] = value

def remove_stuff_s(): #used to remove stuff in the supper list
    stop = False
    while stop == False:
        stuff = input("What do you want to take off of your shopping list?").lower()
        if stuff == "stop":
            stop = True
        elif stuff != "stop":
            if stuff in supper:
                print("removing", stuff)
                del supper[stuff]
            else:
                print("You don't have that on your list")

def view_stuff_s():
    for (stuff, value) in supper.items():
        print("you have", value, stuff, "on your shopping list")

#Main

def main_s(): #main code specified for just deciding what it wanted on the supper list
    play = 1
    print("Supper part of list")
    while play == 1:
        print("")
        print("")
        choice = input("Would you like to Add, Remove, View, or Stop?").lower()
        if choice == "add":
            add_stuff_s()
        elif choice == "remove":
            remove_stuff_s()
        elif choice == "view":
            view_stuff_s()
        elif choice == "stop":
            play = 0
            print ("Onto Desserts")
        else:
            ("Sorry, I didn't understand that.")


import pandas as pd
from math import sqrt
import random

import os
import sys
LIB_PATH = os.path.join(os.path.dirname(__file__), '../lib')
sys.path.append(LIB_PATH)

from RandomForest import *

def main():
    if (len(sys.argv) < 5):
        print("Usage: python3 %s <dataset-csv> <separator> <target-attr> <ntree>" % sys.argv[0])
        exit(-1)

    datasetFile = sys.argv[1]
    separator = sys.argv[2]
    targetAttr = sys.argv[3]
    ntree = int(sys.argv[4])

    random.seed(1)

    # Read dataset
    D = pd.read_csv(datasetFile, sep=separator)
    # Get the number of possible values for each attribute in dataset
    attrsNVals = D.nunique()
    # Build Random Forest
    forest = RandomForest(D, targetAttr, attrsNVals, ntree, attrsSampleFn=sqrt, graph=True)

    if ntree <= 6:
        forest.render()

    # Test classification
    instance = D.iloc[200]
    print("> Test instance:")
    print(instance)
    print("> Prediction: %s" % forest.classify(instance))

if __name__ == "__main__":
    main()


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

import datetime
import json
import sys
from threading import Thread

from resources.lib.common import tools
from resources.lib.indexers.trakt import TraktAPI
from resources.lib.modules import database
from resources.lib.modules.trakt_sync.shows import TraktSyncDatabase
from resources.lib.modules.trakt_sync.hidden import TraktSyncDatabase as HiddenDatabase

try:
    from Queue import Queue
except:
    from queue import Queue

sysaddon = sys.argv[0]
try:
    syshandle = int(sys.argv[1])
except:
    syshandle = ''
trakt = TraktAPI()

language_code = tools.get_language_code()

trakt_database = TraktSyncDatabase()
hidden_database = HiddenDatabase()

class Menus:
    def __init__(self):
        self.itemList = []
        self.threadList = []
        self.direct_episode_threads = []
        self.title_appends = tools.getSetting('general.appendtitles')
        self.task_queue = Queue(40)

    ######################################################
    # MENUS
    ######################################################

    def onDeckShows(self):
        hidden_shows = hidden_database.get_hidden_items('progress_watched', 'shows')
        trakt_list = trakt.json_response('sync/playback/episodes', limit=True)

        if trakt_list is None:
            return
        trakt_list = [i for i in trakt_list if i['show']['ids']['trakt'] not in hidden_shows]
        trakt_list = sorted(trakt_list, key=lambda i: tools.datetime_workaround(i['paused_at'][:19],
                                                                                format="%Y-%m-%dT%H:%M:%S",
                                                                                date_only=False), reverse=True)
        filter_list = []
        showList = []
        sort_list = []
        for i in trakt_list:
            if i['show']['ids']['trakt'] not in filter_list:
                if int(i['progress']) != 0:
                    showList.append(i)
                    filter_list.append(i['show']['ids']['trakt'])
                    sort_list.append(i['show']['ids']['trakt'])

        sort = {'type': 'showInfo', 'id_list': sort_list}
        self.mixedEpisodeBuilder(showList, sort=sort)
        tools.closeDirectory('tvshows')

    def discoverShows(self):

        tools.addDirectoryItem(tools.lang(32007), 'showsPopular&page=1', '', '')
        if tools.getSetting('trakt.auth') is not '':
            tools.addDirectoryItem(tools.lang(32008), 'showsRecommended', '', '')
        # tools.addDirectoryItem('This Years Most Popular', '', '', '')
        tools.addDirectoryItem(tools.lang(32009), 'showsTrending&page=1', '', '')
        tools.addDirectoryItem(tools.lang(32067), 'showsNew', '', '')
        tools.addDirectoryItem(tools.lang(32010), 'showsPlayed&page=1', '', '')
        tools.addDirectoryItem(tools.lang(32011), 'showsWatched&page=1', '', '')
        tools.addDirectoryItem(tools.lang(32012), 'showsCollected&page=1', '', '')
        tools.addDirectoryItem(tools.lang(32013), 'showsAnticipated&page=1', '', '')
        tools.addDirectoryItem(tools.lang(32014), 'showsUpdated&page=1', '', '')
        tools.addDirectoryItem(tools.lang(40121), 'showsNetworks', '', '')
        tools.addDirectoryItem(tools.lang(40123), 'showYears', '', '')
        tools.addDirectoryItem(tools.lang(32062), 'tvGenres', '', '')
        tools.addDirectoryItem(tools.lang(40151), 'showsByActor', '', '')
        # show genres is now labeled as tvGenres to support genre icons in skins
        if tools.getSetting('searchHistory') == 'false':
            tools.addDirectoryItem(tools.lang(32016), 'showsSearch', '', '')
        else:
            tools.addDirectoryItem(tools.lang(32016), 'showsSearchHistory', '', '')
        tools.closeDirectory('addons')

    def myShows(self):
        tools.addDirectoryItem(tools.lang(32063), 'onDeckShows', None, None)
        tools.addDirectoryItem(tools.lang(32017), 'showsMyCollection', '', '')
        tools.addDirectoryItem(tools.lang(32018), 'showsMyWatchlist', '', '')
        tools.addDirectoryItem('Next Up', 'showsNextUp', '', '')
        tools.addDirectoryItem('Upcoming Episodes', 'myUpcomingEpisodes', '', '')
        tools.addDirectoryItem('Unfinished Shows in Collection', 'showsMyProgress', '', '')
        tools.addDirectoryItem('Recent Episodes', 'showsMyRecentEpisodes', '', '')
        tools.addDirectoryItem('My Show Lists', 'myTraktLists&actionArgs=shows', '', '')
        tools.closeDirectory('addons')

    def myShowCollection(self):
        trakt_list = trakt_database.get_collected_episodes()
        trakt_list = [i for i in trakt_list if i is not None]
        trakt_list = list(set([i['show_id'] for i in trakt_list]))
        trakt_list = [{'ids': {'trakt': i}} for i in trakt_list]
        trakt_list = [i for i in trakt_list if i is not None]
        if trakt_list is None:
            return
        self.showListBuilder(trakt_list)
        tools.closeDirectory('tvshows', sort='title')

    def myShowWatchlist(self):
        trakt_list = trakt.json_response('users/me/watchlist/shows', limit=False)
        if trakt_list is None:
            return
        try:
            sort_by = trakt.response_headers['X-Sort-By']
            sort_how = trakt.response_headers['X-Sort-How']
            trakt_list = trakt.sort_list(sort_by, sort_how, trakt_list, 'show')
        except:
            tools.log('Failed to sort trakt list by response headers', 'error')
            pass
        self.showListBuilder(trakt_list)
        tools.closeDirectory('tvshows')

    def myProgress(self):

        collected_episodes = trakt_database.get_collected_episodes()
        collection = list(set([i['show_id'] for i in collected_episodes]))
        if len(collection) == 0:
            return

        show_dicts = []
        for i in collection:
            show_dicts.append({'show': {'ids': {'trakt': i}}})

        show_meta_list = trakt_database.get_show_list(show_dicts)
        unfinished = []

        for show in show_meta_list:
            if show['info']['playcount'] == 0:
                unfinished.append(show)

        self.showListBuilder(unfinished)
        tools.closeDirectory('tvshows', sort='title')

    def newShows(self):

        hidden = hidden_database.get_hidden_items('recommendations', 'shows')
        datestring = datetime.datetime.today() - datetime.timedelta(days=29)
        trakt_list = database.get(trakt.json_response, 12, 'calendars/all/shows/new/%s/30?languages=%s' %
                                  (datestring.strftime('%d-%m-%Y'), language_code))

        if trakt_list is None:
            return
        # For some reason trakt messes up their list and spits out tons of duplicates so we filter it
        duplicate_filter = []
        temp_list = []
        for i in trakt_list:
            if not i['show']['ids']['tvdb'] in duplicate_filter:
                duplicate_filter.append(i['show']['ids']['tvdb'])
                temp_list.append(i)

        trakt_list = temp_list

        trakt_list = [i for i in trakt_list if i['show']['ids']['trakt'] not in hidden]

        if len(trakt_list) > 40:
            trakt_list = trakt_list[:40]
        self.showListBuilder(trakt_list)
        tools.closeDirectory('tvshows')

    def myNextUp(self, ):

        watched_shows = trakt_database.get_watched_shows()
        hidden_shows = hidden_database.get_hidden_items('progress_watched', 'shows')

        watched_shows = [i for i in watched_shows if i['trakt_id'] not in hidden_shows]
        watched_episodes = trakt_database.get_watched_episodes()

        self._start_queue_workers()

        for show in watched_shows:
            self.task_queue.put((self._get_next_episode_to_watch, (show, watched_episodes)), block=True)

        self._finish_queue_workers()

        if tools.getSetting('nextup.sort') == '1':
            watched_list = trakt.json_response('users/me/watched/shows')
            watched_list = sorted(watched_list, key=lambda i: i['last_watched_at'], reverse=True)
            watched_list = [i['show']['ids']['trakt'] for i in watched_list]
            sort = {'type': 'showInfo', 'id_list': watched_list}
        else:
            sort = None

        episodes = self.itemList
        self.itemList = []

        self.mixedEpisodeBuilder(episodes, sort=sort, hide_watched=True)

        tools.closeDirectory('tvshows')

    def _get_next_episode_to_watch(self, show_db_dict, watched_episodes):
        try:
            show_id = show_db_dict['trakt_id']

            if show_db_dict['kodi_meta'] == {}:
                show_db_dict['kodi_meta'] = trakt_database.get_single_show(show_id)

            watched_episodes = [i for i in watched_episodes if i['show_id'] == show_id]
            watched_episodes = sorted(watched_episodes, key=lambda episode: episode['season'], reverse=True)

            season = watched_episodes[0]['season']
            season_meta = trakt_database.get_single_season(show_id, season)

            watched_episodes = [i for i in watched_episodes if i['season'] == season]
            watched_episodes = sorted(watched_episodes, key=lambda episode: episode['number'], reverse=True)
            last_watched_episode = watched_episodes[0]['number']
            next_episode = int(watched_episodes[0]['number']) + 1

            if season_meta is None:
                tools.log('Could not acquire season meta information for %s Season %s' % (show_id, season), 'error')
                return

            if season_meta['info']['episode_count'] == len(watched_episodes) \
                    or season_meta['info']['episode_count'] == last_watched_episode:

                if int(show_db_dict['kodi_meta']['info']['season_count']) > season:
                    season += 1
                    next_episode = 1

            episode_dict = {'show': {'ids': {'trakt': show_id}},
                            'episode': {'season': season, 'number': next_episode}}

            self.itemList.append(episode_dict)
        except KeyError:
            import traceback
            traceback.print_exc()
            pass
        except:
            import traceback
            traceback.print_exc()

    def myRecentEpisodes(self):
        hidden_shows = hidden_database.get_hidden_items('calendar', 'shows')
        datestring = datetime.datetime.today() - datetime.timedelta(days=13)
        trakt_list = database.get(trakt.json_response, 12, 'calendars/my/shows/%s/14' %
                                  datestring.strftime('%d-%m-%Y'))

        if trakt_list is None:
            return
        trakt_list = [i for i in trakt_list if i['show']['ids']['trakt'] not in hidden_shows]
        self.mixedEpisodeBuilder(trakt_list)
        tools.closeDirectory('episodes')

    def myUpcomingEpisodes(self):
        tomorrow = (datetime.date.today() + datetime.timedelta(days=1)).strftime('%Y-%m-%d')
        upcoming_episodes = database.get(trakt.json_response, 24, 'calendars/my/shows/%s/30' % tomorrow)

        sort = sorted(upcoming_episodes, key=lambda i: i['first_aired'])
        sort = [i['episode']['ids']['trakt'] for i in sort]
        sort = {'type': None, 'id_list': sort}
        self.mixedEpisodeBuilder(upcoming_episodes, sort=sort, hide_watched=False, hide_unaired=False,
                                 prepend_date=True)
        tools.closeDirectory('episodes')

    def showsNetworks(self):
        trakt_list = database.get(trakt.json_response, 24, 'networks')

        if trakt_list is None:
            return
        list_items = []
        for i in trakt_list:
            list_items.append(tools.addDirectoryItem(i['name'], 'showsNetworkShows&actionArgs=%s&page=1' % i['name'],
                                                     '', '', bulk_add=True))
        tools.addMenuItems(syshandle, list_items, len(list_items))
        tools.closeDirectory('addons')

    def showsNetworkShows(self, network, page):

        trakt_list = database.get(trakt.json_response, 24, 'shows/popular?networks=%s&page=%s' % (network, page))

        if trakt_list is None:
            return

        self.showListBuilder(trakt_list)

        if len(trakt_list) == int(tools.getSetting('item.limit')):
            tools.addDirectoryItem(tools.lang(32019), 'showsNetworkShows&actionArgs=%s&page=%s' %
                                   (network, int(page) + 1), '', '')

        tools.closeDirectory('tvshows')

    def showsPopular(self, page):
        trakt_list = database.get(trakt.json_response, 12, 'shows/popular?page=%s' % page)

        if trakt_list is None:
            return

        self.showListBuilder(trakt_list)
        tools.addDirectoryItem(tools.lang(32019), 'showsPopular&page=%s' % (int(page) + 1), '', '')
        tools.closeDirectory('tvshows')

    def showsRecommended(self):
        trakt_list = database.get(trakt.json_response, 12, 'recommendations/shows?ignore_collected=true',
                                  limit=True, limitOverride=100)
        if trakt_list is None:
            return
        self.showListBuilder(trakt_list)
        tools.closeDirectory('tvshows')

    def showsTrending(self, page):
        trakt_list = database.get(trakt.json_response, 12, 'shows/trending?page=%s' % page)
        if trakt_list is None:
            return
        self.showListBuilder(trakt_list)
        tools.addDirectoryItem(tools.lang(32019), 'showsTrending&page=%s' % (int(page) + 1), '', '')
        tools.closeDirectory('tvshows')

    def showsPlayed(self, page):
        trakt_list = database.get(trakt.json_response, 12, 'shows/played?page=%s' % page)
        if trakt_list is None:
            return
        self.showListBuilder(trakt_list)
        tools.addDirectoryItem(tools.lang(32019), 'showsPlayed&page=%s' % (int(page) + 1), '', '')
        tools.closeDirectory('tvshows')

    def showsWatched(self, page):
        trakt_list = database.get(trakt.json_response, 12, 'shows/watched?page=%s' % page)
        if trakt_list is None:
            return
        self.showListBuilder(trakt_list)
        tools.addDirectoryItem(tools.lang(32019), 'showsWatched&page=%s' % (int(page) + 1), '', '')
        tools.closeDirectory('tvshows')

    def showsCollected(self, page):
        trakt_list = database.get(trakt.json_response, 12, 'shows/collected?page=%s' % page)
        if trakt_list is None:
            return
        self.showListBuilder(trakt_list)
        tools.addDirectoryItem(tools.lang(32019), 'showsCollected&page=%s' % (int(page) + 1), '', '')
        tools.closeDirectory('tvshows')

    def showsAnticipated(self, page):
        trakt_list = database.get(trakt.json_response, 12, 'shows/anticipated?page=%s&language=%s'
                                  % (page, language_code))
        if trakt_list is None:
            return
        self.showListBuilder(trakt_list)
        tools.addDirectoryItem(tools.lang(32019), 'showsAnticipated&page=%s' % (int(page) + 1), '', '')
        tools.closeDirectory('tvshows')

    def showsUpdated(self, page):
        import datetime
        date = datetime.date.today() - datetime.timedelta(days=31)
        date = date.strftime('%Y-%m-%d')
        trakt_list = database.get(trakt.json_response, 12, 'shows/updates/%s?page=%s' % (date, page))
        if trakt_list is None:
            return
        self.showListBuilder(trakt_list)
        tools.addDirectoryItem(tools.lang(32019), 'showsUpdated&page=%s' % (int(page) + 1), '', '')
        tools.closeDirectory('tvshows')

    def showSearchHistory(self):
        history = database.getSearchHistory('show')
        tools.addDirectoryItem(tools.lang(40142), 'showsSearch', '', '')
        tools.addDirectoryItem(tools.lang(40140), 'clearSearchHistory', '', '', isFolder=False)

        for i in history:
            tools.addDirectoryItem(i, 'showsSearch&actionArgs=%s' % tools.quote(i), '', '')
        tools.closeDirectory('addon')

    def showsSearch(self, actionArgs=None):

        if actionArgs == None:
            k = tools.showKeyboard('', tools.lang(32016))
            k.doModal()
            query = (k.getText() if k.isConfirmed() else None)
            if query == None or query == '':
                return
        else:
            query = actionArgs
        database.addSearchHistory(query, 'show')
        query = tools.deaccentString(tools.display_string(query))
        tools.quote(query)
        tools.closeAllDialogs()
        tools.closeDirectory('tvshows')
        tools.execute("Container.Update(%s?action=showsSearchResults&actionArgs=%s, replace)'" % (sysaddon, query))

    def showsSearchResults(self, query):
        query = tools.quote_plus(tools.unquote(query))
        trakt_list = trakt.json_response('search/show?query=%s&extended=full&type=show&field=title' % query)
        if trakt_list is None:
            return
        self.showListBuilder(trakt_list)
        tools.closeDirectory('tvshows')

    def showsByActor(self, actionArgs):

        if actionArgs == None:
            k = tools.showKeyboard('', tools.lang(32016))
            k.doModal()
            query = (k.getText() if k.isConfirmed() else None)
            if query == None or query == '':
                return
        else:
            query = tools.unquote(actionArgs)

        database.addSearchHistory(query, 'showActor')
        query = tools.deaccentString(query)
        query = query.replace(' ', '-')
        query = tools.quote_plus(query)

        trakt_list = trakt.json_response('people/%s/shows' % query, limit=True)

        try:
            trakt_list = trakt_list['cast']
        except:
            import traceback
            traceback.print_exc()
            trakt_list = []

        trakt_list = [i['show'] for i in trakt_list]

        self.showListBuilder(trakt_list)

        tools.closeDirectory('tvshows')

    def showSeasons(self, args):

        args = tools.get_item_information(args)

        self.seasonListBuilder(args['ids']['trakt'])

        tools.closeDirectory('seasons')

    def seasonEpisodes(self, args):

        args = tools.get_item_information(args)

        show_id = args['showInfo']['ids']['trakt']

        if 'seasonInfo' in args:
            season_number = args['seasonInfo']['info']['season']
        else:
            season_number = args['info']['season']

        self.episodeListBuilder(show_id, season_number)
        tools.closeDirectory('episodes', sort='episode')

    def showGenres(self):
        tools.addDirectoryItem(tools.lang(32065), 'showGenresGet', '', '', isFolder=True)
        genres = database.get(trakt.json_response, 24, 'genres/shows')

        if genres is None:
            return

        for i in genres:
            tools.addDirectoryItem(i['name'], 'showGenresGet&actionArgs=%s' % i['slug'], '', '', isFolder=True)
        tools.closeDirectory('addons')

    def showGenreList(self, args, page):
        if page is None:
            page = 1

        if args is None:
            genre_display_list = []
            genre_string = ''
            genres = database.get(trakt.json_response, 24, 'genres/shows')

            for genre in genres:
                genre_display_list.append(genre['name'])
            genre_multiselect = tools.showDialog.multiselect(tools.addonName + ": Genre Selection", genre_display_list)

            if genre_multiselect is None: return
            for selection in genre_multiselect:
                genre_string += ', %s' % genres[selection]['slug']
            genre_string = genre_string[2:]

        else:
            genre_string = args

        page = int(page)
        trakt_list = database.get(trakt.json_response, 12,
                                  'shows/popular?genres=%s&page=%s' % (genre_string, page))
        if trakt_list is None:
            return

        self.showListBuilder(trakt_list)
        tools.addDirectoryItem(tools.lang(32019),
                               'showGenresGet&actionArgs=%s&page=%s' % (genre_string, page + 1), None, None)
        tools.closeDirectory('tvshows')

    def showsRelated(self, args):
        trakt_list = database.get(trakt.json_response, 12, 'shows/%s/related' % args)
        if trakt_list is None:
            return

        self.showListBuilder(trakt_list)
        tools.closeDirectory('tvshows')

    def showYears(self, year=None, page=None):
        if year is None:
            current_year = int(tools.datetime_workaround(datetime.datetime.today().strftime('%Y-%m-%d')).year)
            all_years = reversed([year for year in range(1900, current_year+1)])
            menu_items = []
            for year in all_years:
                menu_items.append(tools.addDirectoryItem(str(year), 'showYears&actionArgs=%s' % year, '', '',
                                  bulk_add=True))
            tools.addMenuItems(syshandle, menu_items, len(menu_items))
            tools.closeDirectory('tvshows')
        else:
            if page is None:
                page = 1

            trakt_list = trakt.json_response('shows/popular?years=%s&page=%s' % (year, page))
            self.showListBuilder(trakt_list)
            tools.addDirectoryItem(tools.lang(32019),
                                   'showYears&actionArgs=%s&page=%s' % (year, int(page) + 1), None, None)
            tools.closeDirectory('tvshows')

    ######################################################
    # MENU TOOLS
    ######################################################

    def seasonListBuilder(self, show_id, smartPlay=False):

        self.itemList = trakt_database.get_season_list(show_id)

        self.itemList = [x for x in self.itemList if x is not None and 'info' in x]

        self.itemList = sorted(self.itemList, key=lambda k: k['info']['season'])

        if len(self.itemList) == 0:
            tools.log('We received no titles to build a list', 'error')
            return

        hide_specials = False

        if tools.getSetting('general.hideSpecials') == 'true':
            hide_specials = True

        item_list = []

        for item in self.itemList:

            try:
                if hide_specials and int(item['info']['season']) == 0:
                    continue

                action = 'seasonEpisodes'

                args = {'trakt_id': item['showInfo']['ids']['trakt'],
                        'season': item['info']['season'],
                        'item_type': 'season'}

                args = tools.quote(json.dumps(args, sort_keys=True))

                item['trakt_object']['show_id'] = item['showInfo']['ids']['trakt']
                name = item['info']['season_title']

                if not self.is_aired(item['info']) or 'aired' not in item['info']:
                    if tools.getSetting('general.hideUnAired') == 'true':
                        continue
                    name = tools.colorString(name, 'red')
                    name = tools.italic_string(name)
                    item['info']['title'] = name

                item['info'] = tools.clean_air_dates(item['info'])

            except:
                import traceback
                traceback.print_exc()
                continue

            if smartPlay is True:
                return args

            cm = []
            if tools.getSetting('trakt.auth') != '':
                cm.append(('Trakt Manager', 'RunPlugin(%s?action=traktManager&actionArgs=%s)' % (sysaddon, args)))

            if tools.context_addon():
                cm = []

            item_list.append(tools.addDirectoryItem(name, action, item['info'], item['art'], cm=cm, isFolder=True,
                                                    isPlayable=False, actionArgs=args, set_ids=item['ids'],
                                                    bulk_add=True))

        tools.addMenuItems(syshandle, item_list, len(item_list))

    def episodeListBuilder(self, show_id, season_number, smartPlay=False, hide_unaired=False):

        try:
            item_list = []

            self.itemList = trakt_database.get_season_episodes(show_id, season_number)
            self.itemList = [x for x in self.itemList if x is not None and 'info' in x]

            if len(self.itemList) == 0:
                tools.log('We received no titles to build a list', 'error')
                return

            try:
                self.itemList = sorted(self.itemList, key=lambda k: k['info']['episode'])
            except:
                pass

            for item in self.itemList:

                cm = []

                try:

                    if tools.getSetting('smartplay.playlistcreate') == 'true' and smartPlay is False:
                        action = 'smartPlay'
                        playable = False
                    else:
                        playable = True
                        action = 'getSources'

                    args = {'trakt_id': item['showInfo']['ids']['trakt'],
                            'season': item['info']['season'],
                            'episode': item['info']['episode'],
                            'item_type': 'episode'}

                    args = tools.quote(json.dumps(args, sort_keys=True))

                    name = item['info']['title']

                    if not self.is_aired(item['info']):
                        if tools.getSetting('general.hideUnAired') == 'true' or hide_unaired:
                            continue
                        else:
                            name = tools.colorString(name, 'red')
                            name = tools.italic_string(name)
                            item['info']['title'] = name

                    item['info'] = tools.clean_air_dates(item['info'])

                except:
                    import traceback
                    traceback.print_exc()
                    continue
                cm.append((tools.lang(32070),
                           'XBMC.PlayMedia(%s?action=shufflePlay&actionArgs=%s)' % (sysaddon, args)))

                cm.append(('Browse Season',
                           'XBMC.Container.Update(%s?action=seasonEpisodes&actionArgs=%s)' %
                           (sysaddon,
                            tools.quote(json.dumps({'trakt_id': item['showInfo']['ids']['trakt'],
                                                    'season': item['info']['season'],
                                                    'item_type': 'season'})))))

                cm.append((tools.lang(33022),
                           'PlayMedia(%s?action=getSources&seren_reload=true&actionArgs=%s)' % (sysaddon, args)))

                cm.append((tools.lang(32066),
                           'PlayMedia(%s?action=getSources&source_select=true&actionArgs=%s)' % (sysaddon, args)))

                if tools.getSetting('trakt.auth') != '':
                    cm.append(('Trakt Manager', 'RunPlugin(%s?action=traktManager&actionArgs=%s)' % (sysaddon, args)))

                if tools.context_addon():
                    cm = []

                if tools.getSetting('premiumize.enabled') == 'true' and tools.getSetting('premiumize.pin') != '':
                    cm.append((tools.lang(32068),
                               'XBMC.RunPlugin(%s?action=filePicker&actionArgs=%s)' % (sysaddon, args)))

                item_list.append(tools.addDirectoryItem(name, action, item['info'], item['art'], isFolder=False,
                                                        isPlayable=playable, actionArgs=args, bulk_add=True,
                                                        set_ids=item['ids'], cm=cm))

            if smartPlay is True:
                return item_list
            else:
                tools.addMenuItems(syshandle, item_list, len(item_list))

        except:
            import traceback
            traceback.print_exc()

    def mixedEpisodeBuilder(self, trakt_list, sort=None, hide_watched=False, smartPlay=False, hide_unaired=True,
                            prepend_date=False):

        self.threadList = []

        try:
            if len(trakt_list) == 0:
                tools.log('We received no titles to build a list', 'error')
                return

            self.itemList = trakt_database.get_episode_list(trakt_list)

            self.itemList = [x for x in self.itemList if x is not None and 'info' in x]
            self.itemList = [i for i in self.itemList if 'info' in i and i['info'].get('premiered', None) is not None]
            if sort is None:
                self.itemList = sorted(self.itemList,
                                       key=lambda i: tools.datetime_workaround(i['info']['premiered'],
                                                                               tools.trakt_gmt_format, False),
                                       reverse=True)
            elif sort is not False:
                sort_list = []
                for trakt_id in sort['id_list']:
                    try:
                        if not sort['type']:
                            item = [i for i in self.itemList if i['ids']['trakt'] == trakt_id][0]
                        else:
                            item = [i for i in self.itemList if i[sort['type']]['ids']['trakt'] == trakt_id][0]
                        sort_list.append(item)
                    except IndexError:
                        continue
                    except:
                        import traceback
                        traceback.print_exc()
                self.itemList = sort_list

            item_list = []

            for item in self.itemList:
                if item is None:
                    continue

                if item['info'].get('title', '') == '':
                    continue

                if hide_watched and item['info']['playcount'] != 0:
                    continue

                cm = []

                try:
                    name = tools.display_string(item['info']['title'])

                    if not self.is_aired(item['info']) and hide_unaired is True:
                        continue
                    elif not self.is_aired(item['info']):
                        name = tools.colorString(name, 'red')
                        name = tools.italic_string(name)
                        item['info']['title'] = name

                    item['info'] = tools.clean_air_dates(item['info'])

                    args = {'trakt_id': item['showInfo']['ids']['trakt'],
                            'season': item['info']['season'],
                            'episode': item['info']['episode'],
                            'item_type': 'episode'}

                    args = tools.quote(json.dumps(args, sort_keys=True))

                    if tools.getSetting('smartplay.playlistcreate') == 'true' and smartPlay is False:
                        action = 'smartPlay'
                        playable = False
                    else:
                        playable = True
                        action = 'getSources'

                    if self.title_appends == 'true':
                        name = "%s: %sx%s %s" % (tools.colorString(item['showInfo']['info']['tvshowtitle']),
                                                 tools.display_string(item['info']['season']).zfill(2),
                                                 tools.display_string(item['info']['episode']).zfill(2),
                                                 tools.display_string(item['info']['title']))
                    if prepend_date:
                        release_day = tools.datetime_workaround(item['info']['aired'])
                        release_day = release_day.strftime('%d %b')
                        name = '[%s] %s' % (release_day, name)

                    cm.append((tools.lang(32069),
                               'XBMC.Container.Update(%s?action=showSeasons&actionArgs=%s)' %
                               (sysaddon, tools.quote(json.dumps({'trakt_id': item['showInfo']['ids']['trakt'],
                                                     'item_type': 'show'})))))

                    cm.append(('Browse Season',
                               'XBMC.Container.Update(%s?action=seasonEpisodes&actionArgs=%s)' %
                               (sysaddon,
                                tools.quote(json.dumps({'trakt_id': item['showInfo']['ids']['trakt'],
                                                        'season': item['info']['season'],
                                                        'item_type': 'season'})))))

                    cm.append((tools.lang(32070),
                               'XBMC.PlayMedia(%s?action=shufflePlay&actionArgs=%s)' % (sysaddon, args)))

                    cm.append((tools.lang(32066),
                               'PlayMedia(%s?action=getSources&source_select=true&actionArgs=%s)' % (sysaddon, args)))

                    cm.append((tools.lang(33022),
                               'PlayMedia(%s?action=getSources&seren_reload=true&actionArgs=%s)' % (sysaddon, args)))

                    if tools.getSetting('trakt.auth') != '':
                        cm.append(('Trakt Manager', 'RunPlugin(%s?action=traktManager&actionArgs=%s)'
                                   % (sysaddon, tools.quote(json.dumps(item['trakt_object'])))))

                    if tools.context_addon():
                        cm = []

                    if tools.getSetting('premiumize.enabled') == 'true' and tools.getSetting('premiumize.pin') != '':
                        cm.append((tools.lang(32068),
                                   'XBMC.RunPlugin(%s?action=filePicker&actionArgs=%s)' % (sysaddon, args)))

                    item['info']['title'] = item['info']['originaltitle'] = name

                    item_list.append(tools.addDirectoryItem(name, action, item['info'], item['art'], isFolder=False,
                                                            isPlayable=playable, actionArgs=args, bulk_add=True,
                                                            set_ids=item['ids'], cm=cm))

                except:
                    import traceback
                    traceback.print_exc()
                    continue

            if smartPlay is True:
                return item_list
            else:
                tools.addMenuItems(syshandle, item_list, len(item_list))

        except:
            import traceback
            traceback.print_exc()

    def showListBuilder(self, trakt_list, forceResume=False, info_only=False):

        try:
            if len(trakt_list) == 0:
                tools.log('We received no titles to build a list', 'error')
                return
        except:
            import traceback
            traceback.print_exc()
            return

        if 'show' in trakt_list[0]:
            trakt_list = [i['show'] for i in trakt_list]

        show_ids = [i['ids']['trakt'] for i in trakt_list]

        self.itemList = trakt_database.get_show_list(show_ids)
        self.itemList = [x for x in self.itemList if x is not None and 'info' in x]
        self.itemList = tools.sort_list_items(self.itemList, trakt_list)

        item_list = []

        for item in self.itemList:
            try:
                # Add Arguments to pass with items
                args = {'trakt_id': item['ids']['trakt'], 'item_type': 'show'}
                args = tools.quote(json.dumps(args, sort_keys=True))

                cm = []

                name = tools.display_string(item['info']['tvshowtitle'])

                if info_only == True:
                    return args

                if not self.is_aired(item['info']):
                    if tools.getSetting('general.hideUnAired') == 'true':
                        continue
                    name = tools.colorString(name, 'red')
                    name = tools.italic_string(name)

                item['info'] = tools.clean_air_dates(item['info'])

                if 'setCast' in item:
                    set_cast = item['setCast']
                else:
                    set_cast = False

                if tools.getSetting('smartplay.clickresume') == 'true' or forceResume is True:
                    action = 'playbackResume'
                else:
                    action = 'showSeasons'

                # Context Menu Items

                cm.append((tools.lang(32070),
                           'XBMC.PlayMedia(%s?action=shufflePlay&actionArgs=%s)' % (sysaddon, args)))

                cm.append((tools.lang(32020),
                           'Container.Update(%s?action=showsRelated&actionArgs=%s)' % (sysaddon, item['ids']['trakt'])))

                cm.append((tools.lang(32069),
                           'XBMC.Container.Update(%s?action=showSeasons&actionArgs=%s)' % (sysaddon, args)))

                if tools.getSetting('trakt.auth') != '':
                    cm.append(('Trakt Manager', 'RunPlugin(%s?action=traktManager&actionArgs=%s)' % (sysaddon, args)))

                cm.append((tools.lang(40153),
                           'XBMC.PlayMedia(%s?action=playFromRandomPoint&actionArgs=%s' % (sysaddon, args)))

                if tools.context_addon():
                    cm = []

            except:
                import traceback
                traceback.print_exc()
                continue

            item_list.append(tools.addDirectoryItem(name, action, item['info'], item['art'], cm=cm, isFolder=True,
                                                    isPlayable=False, actionArgs=args, bulk_add=True, set_cast=set_cast,
                                                    set_ids=item['ids']))

        tools.addMenuItems(syshandle, item_list, len(item_list))

    def runThreads(self, join=True):
        for thread in self.threadList:
            thread.start()

        if join == True:
            for thread in self.threadList:
                thread.join()

    def _start_queue_workers(self):

        self.queue_finished = False

        for i in range(40):
            self.threadList.append(Thread(target=self._queue_worker))

        for i in self.threadList:
            i.start()

    def _finish_queue_workers(self):

        self.queue_finished = True

        for i in self.threadList:
            i.join()

        self.threadList = []

    def _queue_worker(self):
        while not self.task_queue.empty() or not self.queue_finished:
            try:
                target = self.task_queue.get(timeout=3)
            except:
                continue
            try:
                target[0](*target[1])
            except:
                import traceback
                traceback.print_exc()
                pass

    def is_aired(self, info):
        try:
            try:air_date = info['aired']
            except: air_date = info.get('premiered')
            if air_date == '' or air_date is None:
                return False
            if int(air_date[:4]) < 1970:
                return True

            time_format = tools.trakt_gmt_format
            if len(air_date) == 10:
                time_format = '%Y-%m-%d'

            air_date = tools.gmt_to_local(air_date, format=time_format)

            if tools.getSetting('general.datedelay') == 'true':
                air_date = tools.datetime_workaround(air_date, time_format, False)
                air_date += datetime.timedelta(days=1)
            else:
                air_date = tools.datetime_workaround(air_date, time_format, False)

            if air_date > datetime.datetime.now():
                return False
            else:
                return True
        except:
            import traceback
            traceback.print_exc()
            # Assume an item is not aired if we do not have any information on it or fail to identify
            return False


from tech_news.database import find_news

# Requisito 10
def top_5_news():
    """Seu código deve vir aqui"""
    news_list = find_news()
    classified_news = sorted(
        news_list, key=lambda news: news["comments_count"], reverse=True
    )
    return [
        (news["title"], news["url"])
        for news in classified_news
        if classified_news.index(news) < 5
    ]

# Requisito 11
def top_5_categories():
    """Seu código deve vir aqui"""
    news_list = find_news()
    categories = {news["category"]: 0 for news in news_list}
    classified_categories = sorted(categories, key=lambda category: category)
    categories = {key: 0 for key in classified_categories}
    for news in news_list:
        categories[news["category"]] += 1
    ordered_categories = sorted(
        categories.items(), key=lambda category: (category[1]), reverse=True
    )
    return [
        category
        for category, qnt in ordered_categories
        if ordered_categories.index((category, qnt)) < 5
    ]


import pykka
import re
import json

from gpt_connection import GPT_Connection
from tools import ToolRunner
from frontend_utils import initial_request

from bots.Dispatcher import Dispatcher
from bots.Workflow import Workflow

from prompts import DISPATCHER_PROMPT_TEMPLATE, INITIAL_PROMPT_TEMPLATE, ITERATING_ACTION_PROMPT_TEMPLATE, FORCE_END_ITERATION_PROMPT, \
    PLANT_HEALTH_BOT_DESCRIPTION, PRODUCTION_OUTPUT_BOT_DESCRIPTION, DISTRIBUTION_BOT_DESCRIPTION, \
    ONT_PRODUCTION_PLANT, ONT_MACHINES, ONT_WORK_ORDERS, ONT_PRODUCTION_ALLOCATION_PLAN, ONT_DISTRIBUTION_WAREHOUSE, ONT_TRANSIT_ORDER, \
    GET_OBJECTS, MODIFY_OBJECT, CREATE_OBJECT

from string import Template

"""
Main Execution Code
"""

gpt_connection = GPT_Connection()
tool_runner = ToolRunner()

plant_health_ref = Workflow.start(
    name="Plant Health Bot",
    id=2,
    dispatcher_id=1,
    bot_description=PLANT_HEALTH_BOT_DESCRIPTION,
    initial_prompt_template=INITIAL_PROMPT_TEMPLATE,
    iteration_prompt_template=ITERATING_ACTION_PROMPT_TEMPLATE,
    force_end_prompt_template=FORCE_END_ITERATION_PROMPT,
    information=[ONT_PRODUCTION_PLANT, ONT_MACHINES, ONT_WORK_ORDERS],
    readtools=[GET_OBJECTS],
    writetools=[MODIFY_OBJECT, CREATE_OBJECT],
    gpt_connection=gpt_connection,
    tool_runner=tool_runner
)
production_output_ref = Workflow.start(
    name="Production Output Bot",
    id=3,
    dispatcher_id=1,
    bot_description=PRODUCTION_OUTPUT_BOT_DESCRIPTION,
    initial_prompt_template=INITIAL_PROMPT_TEMPLATE,
    iteration_prompt_template=ITERATING_ACTION_PROMPT_TEMPLATE,
    force_end_prompt_template=FORCE_END_ITERATION_PROMPT,
    information=[ONT_PRODUCTION_PLANT, ONT_MACHINES, ONT_PRODUCTION_ALLOCATION_PLAN],
    readtools=[GET_OBJECTS],
    writetools=[MODIFY_OBJECT, CREATE_OBJECT],
    gpt_connection=gpt_connection,
    tool_runner=tool_runner
)
distribution_ref = Workflow.start(
    name="Distribution Bot",
    id=4,
    dispatcher_id=1,
    bot_description=DISTRIBUTION_BOT_DESCRIPTION,
    initial_prompt_template=INITIAL_PROMPT_TEMPLATE,
    iteration_prompt_template=ITERATING_ACTION_PROMPT_TEMPLATE,
    force_end_prompt_template=FORCE_END_ITERATION_PROMPT,
    information=[ONT_PRODUCTION_PLANT, ONT_DISTRIBUTION_WAREHOUSE, ONT_PRODUCTION_ALLOCATION_PLAN, ONT_TRANSIT_ORDER],
    readtools=[GET_OBJECTS],
    writetools=[MODIFY_OBJECT, CREATE_OBJECT],
    gpt_connection=gpt_connection,
    tool_runner=tool_runner
)
workflows = {
    "Plant Health Bot": plant_health_ref,
    "Production Output Bot": production_output_ref,
    "Distribution Bot": distribution_ref
}
workflow_ids = {
    "Plant Health Bot": 2,
    "Production Output Bot": 3,
    "Distribution Bot": 4
}
dispatcher_ref = Dispatcher.start(
    id=1,
    prompt_template=DISPATCHER_PROMPT_TEMPLATE,
    workflows=workflows,
    workflow_ids=workflow_ids,
    gpt_connection=gpt_connection)

initial_ask = "We just got a message that the ocelot making machine has broken at Stuffed Animal Plant 8"
# initial_ask = "Our trucks carrying transit order T0030 got into an accident."
# initial_ask = "Alice has left the company"
initial_request(initial_ask)
dispatcher_ref.ask(initial_ask)

dispatcher_ref.stop()


# # 8/19/2020
# You will work with a binary classification problem on a subsample from Kaggle playground competition. The objective of this competition is to predict whether a famous basketball player Kobe Bryant scored a basket or missed a particular shot.

# Train data is available in your workspace as bryant_shots DataFrame. It contains data on 10,000 shots with its properties and a target variable "shot\_made\_flag" -- whether shot was scored or not.

# One of the features in the data is "game_id" -- a particular game where the shot was made. There are 541 distinct games. So, you deal with a high-cardinality categorical feature. Let's encode it using a target mean!

# Suppose you're using 5-fold cross-validation and want to evaluate a mean target encoded feature on the local validation.

# Create 5-fold cross-validation
kf = KFold(n_splits=5, random_state=123, shuffle=True)

# For each folds split
for train_index, test_index in kf.split(bryant_shots):
    cv_train, cv_test = bryant_shots.iloc[train_index], bryant_shots.iloc[test_index]

    # Create mean target encoded feature
    cv_train['game_id_enc'], cv_test['game_id_enc'] = mean_target_encoding(train=cv_train,
                                                                           test=cv_test,
                                                                           target='shot_made_flag',
                                                                           categorical='game_id',
                                                                           alpha=5)
    # Look at the encoding
    print(cv_train[['game_id', 'shot_made_flag', 'game_id_enc']].sample(n=1))

# <script.py> output:
#            game_id  shot_made_flag  game_id_enc
#     7106  20500532             0.0     0.361914
#            game_id  shot_made_flag  game_id_enc
#     5084  20301100             0.0     0.568395
#            game_id  shot_made_flag  game_id_enc
#     6687  20500228             0.0      0.48131
#            game_id  shot_made_flag  game_id_enc
#     5046  20301075             0.0     0.252103
#            game_id  shot_made_flag  game_id_enc
#     4662  20300515             1.0     0.452637

Winning a Kaggle Competition in Python/3 Feature Engineering/06_KFold_CrossValidation.py


from dotenv import load_dotenv
import discord
from discord.ext import commands
import os
import re

load_dotenv()
token = os.getenv('DISCORD_TOKEN')
commMark = os.getenv('COMMAND_MARKER')

description = 'Discord administration bot'

intents = discord.Intents.default()
intents.message_content = True
intents.members = True

bot = commands.Bot(command_prefix=commMark, description=description, intents=intents)

#posts log in message on log in
@bot.event
async def on_ready():
    print('Logged on as {bot.user} (ID {bot.user.id})')
    print('-----------')
#------------------------------------------------Functions---------------------------------------------------#

def get_rid(ctx, role_input):
    print("get_rid starting") #debugging - prints once function is called

    #cleans input and assigns to role_name
    role_name = role_input.strip()
    print(role_name)

    #first trying regext to get the id from the message itself
    role_id = re.search(r'\d{18}', role_name)
    roles_list = [] #initializing return list
    if role_id != None: #checking if re found something
        role_id = role.id.group(0) # getting readable id
        roles_list.append(int(role_id)) #getting and appending role-id to list
    else:
        #iterating through roles, searching for name match
        for g_role in ctx.guild.roles:
            if role_name in str(g_role.name):
                roles_list.append(int(g_role.id)) #appending to list

    print(roles_list) #debugging - prints roles_list
    roleLen = len(roles_list)
    print('length: ' + str(roleLen)) #debugging - prints length of roles_list
    print('get_rid finishing')
    return roles_list, len(roles_list)

#similar function to get_rid, but for retrieving user ID
def getuid(ctx, user_input):
    print("get_uid starting") #debugging - prints once function is called
    #cleans input and assigns to role_name
    users_list = []
    user_name = user_input.strip()
    print("uid start " + user_name)
    for g_user in ctx.guild.members:
        print( "uid for " + str(g_user))
        if user_name in str(g_user):
            users_list.append(int(g_user.id))
            print("username match")
            print("appended " + str(g_user.id)) #appending to list
            print("get_uid users list " + str(users_list))
        else:
            print("Not a match")
    print("get_uid list" + str(users_list)) #debugging - prints roles_list
    userLen = len(users_list)
    print(userLen)
    print('get_uid finishing')
    return users_list, len(users_list)

#----------------------------------------- Commands below, functions above-----------------------------------------------------------#

#------------------------------------------testing/troubleshooting commands----------------------------------------------------------#

#@bot.command()
#async def hello(ctx):
#    await ctx.send(f'Hello {ctx.author.display_name}.')

#test command, just echoes the argument
#@bot.command()
#async def test(ctx, content):
#    await ctx.send(content)

#-----------------------------------------------administrative commands---------------------------------------------------------------#
#command to get role ID
@bot.command()
async def roleid(ctx, role_name: str):
    try:
        role, le = get_rid(ctx, role_name)
        print(role)
        print(le)
        if le == 1:
            roleAdd = role[0]
            await ctx.send(roleAdd)
    except:
        emby = discord.Embed(title="", color=discord.Color.red())
        emby.add_field(name="Something went wrong", value="Please check your given argument")
        await ctx.send(embed=emby)

#command to add role to user
@bot.command()
async def addrole(ctx, role_name: str, user):
    try:
        role, le = get_rid(ctx, role_name)
        print(role)
        print(le)
        if le == 1:
            roleAdd = role[0]
            print(roleAdd)
            getuid(user)
            await ctx.send('Adding role %s to user %s' % (role_name, user))

    except:
        print("except")

#command to feth user id's
@bot.command()
async def userid(ctx, user):
    print('User ID command called by %s Requesting UserID for %s' % (ctx.author, user ))
    try:
        #calls function to get user ID by username, then prints variables for debugging/logging
        userN, leU = getuid(ctx, user)
        #outputs all user IDs to chat
        if leU == 0:
            await ctx.send("No user found with that name")
        else:
            for i in userN:
                await ctx.send(i)
    except:
        emby = discord.Embed(title="", color=discord.Color.red())
        emby.add_field(name="Something went wrong", value="Please check your given argument")
        await ctx.send(embed=emby)

#starts the bot
bot.run(token)


import pandas as pd
import redis
from redisgraph import Graph
from config import redisgraph_config
from pathlib import Path

def redisgraph_import_csv(output: Path) -> None:
    r = redis.Redis(host=redisgraph_config['host'], port=redisgraph_config['port'])
    graph_name = 'movie_graph'
    redis_graph = Graph(graph_name, r)

    import_csv = pd.read_csv(output / 'neo4j_export.csv')
    break_column_name = '_start'

    nodes = pd.DataFrame()
    relations = pd.DataFrame()

    active_df = nodes

    for idx, col in enumerate(import_csv.columns):
        if col == break_column_name:
            active_df = relations
        active_df[col] = import_csv[col]

    nodes.dropna(how='all', subset=None, inplace=True)
    relations.dropna(how='all', subset=None, inplace=True)

    # relations.to_csv('../out/Relations.csv')

    node_values = nodes['_labels'].unique()
    rows_to_drop = [node_value for node_value in node_values if 'UNIQUE IMPORT LABEL' in node_value]
    for row_to_drop in rows_to_drop:
        nodes = nodes[nodes["_labels"].str.contains(row_to_drop) == False]

    node_values = nodes['_labels'].unique()
    for node_type in node_values:
        node_data = nodes[nodes['_labels'] == node_type]
        filename = f'../out/{node_type.replace(":", "")}.csv'
        node_data.dropna(how='all', axis=1, inplace=True)
        # node_data.to_csv(filename)

        for node in node_data.iloc:
            params = ''
            for param in node_data.columns:
                if not param.startswith('_'):
                    val = node[param]
                    if not pd.isna(val):
                        try:
                            val = float(val)
                        except:
                            val = f'"{val}"'
                        params = params + f', {param}: {val}'

            query = f'MERGE ({node["_labels"]} {{id: {node["_id"]} {params} }})'
            redis_graph.query(query)

    for node in relations.iloc:
        query = f"""
        MATCH (a), (b) WHERE a.id = {node["_start"]} AND b.id = {node["_end"]} CREATE (a)-[:{node['_type']}]->(b)
        """
        redis_graph.query(query)


import sys
import os
from os.path import splitext
import glob
from common import TestCase

def get_tests():
    """Get all the tests to run.
    """
    files = glob.glob('test_*.py')
    return files

def run_all(tests):
    """Run the given tests.
    """
    args = ' '.join(sys.argv[1:])
    success = []
    fail = []
    for test in tests:
        cmd = 'python %s %s'%(test, args)
        print(cmd)
        status = os.system(cmd)
        if status == 0:
            print("OK")
            success.append(test)
        else:
            print("FAIL: %s"%test)
            fail.append(test)

    print('-'*70)
    print("%d successful tests, %d failures"%(len(success), len(fail)))
    for test in fail:
        print(test)
    print('-'*70)
    return len(fail) != 0

class RunAllTests(TestCase):
    """Runs all the tests in one go, instead of running each test
    separately.  This speeds up the testing.
    """
    def get_tests(self):
        tests = get_tests()
        tests = [splitext(t)[0] for t in tests]
        klasses = []
        for test in tests:
            # Find test.
            m = __import__(test)
            m.mayavi = self.script
            m.application = self.application
            for name in dir(m):
                klass = getattr(m, name)
                try:
                    if issubclass(klass, TestCase) and klass is not TestCase:
                        mod_name = '%s.%s'%(test, name)
                        klasses.append((mod_name, klass))
                        break
                except TypeError:
                    continue
        return klasses

    def do(self):
        klasses = self.get_tests()
        for name, klass in klasses:
            # Close existing scenes.
            e = self.script.engine
            for scene in e.scenes:
                e.close_scene(scene)
            print('*'*80)
            print(name)
            obj = klass()
            obj.trait_set(script=self.script)
            obj.test()

def main():
    argv = ' '.join(sys.argv)

    if '--one-shot' in argv:
        argv = argv.replace('--one-shot', '')
        sys.argv = argv.split()
        t = RunAllTests()
        t.main()
    else:
        tests = get_tests()
        status = run_all(tests)
        sys.exit(status)

if __name__ == "__main__":
    main()


import os
import numpy as np
import scipy.io.wavfile as wavfile
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import normalize
from utils import read_textgrid
from numpy_operation import get_martix
from python_speech_features import mfcc
from python_speech_features import delta
#from python_speech_features import fbank

def to_one_hot(labels, dimension=5):
    results = np.zeros((len(labels),dimension))
    for i, label in enumerate(labels):
        results[i, label] = 1.
    return results

def read_wav(filename):
    rate, data = wavfile.read(filename)
    #only use the 1st channel if stereo
    if len(data.shape) > 1:
        data =  data[:,0]
    data = data.astype(np.float32)
    data = data / 32768 #convert PCM int16 to float
    return data, rate

def feature_extract(filename, wavpath, tgpath):
    wav_filename = os.path.join(wavpath,filename+'.wav')
    print(wav_filename)
    tg_filename = os.path.join(tgpath,filename+'.textgrid')

    y,sr = read_wav(wav_filename)

    mfccs = mfcc(signal=y,samplerate=sr,winlen=0.02,winfunc=np.hamming)
    delta1 = delta(mfccs,1)
    delta2 = delta(mfccs,2)

    _mfccs = np.concatenate((mfccs,delta1,delta2),1)
    _mfccs = normalize(_mfccs)
    _mfccs = get_martix(_mfccs,30,10)

    _labels = None
    if(os.path.exists(tg_filename)):
        _labels = read_textgrid(tg_filename,len(_mfccs))
        _labels = to_one_hot(_labels)
    return _mfccs,_labels


T = int(input())
for _ in range(T):
    N, K = [int(x) for x in input().split()]
    time = [int(x) for x in input().split()]
    levels = [[0]]
    for __ in range(K):
        f, t = [int(x) - 1 for x in input().split()]
        level = 0
        for i in range(len(levels)):
            if f in levels[i]:
                level = i
                break
        try:
            if t not in levels[level+1]:
                levels[level+1].append(t)
        except:
            levels.append([])
            if t not in levels[level+1]:
                levels[level+1].append(t)

    W = int(input()) - 1

    level = 0
    for i in range(len(levels)):
        if W in levels[i]:
            level = i
            break

    cost = 0
    for l in levels[:level+1]:
        cost += max([time[i] for i in l])

    print(cost)

Backjoon/HTJ/동적 계획법 기초 단계/1005 ACM Craft.py


#! /usr/bin/python3
# id-DNA.py
# http://rosalind.info/problems/dna/

dna_file = open('/home/steve/Dropbox/Rosalind/rosalind_dna.txt', 'r')
dna_string = str(dna_file.readlines())

base_count = {"A": 0, "G": 0, "C": 0, "T": 0}
for base in dna_string:
    base_count[base] += 1

print(base_count)


import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from math import sqrt
from scipy import stats

import warnings
warnings.filterwarnings("ignore")

from statsmodels.formula.api import ols
from sklearn.metrics import mean_squared_error, r2_score, explained_variance_score, mean_absolute_error
from sklearn.feature_selection import f_regression, SelectKBest, RFE
from sklearn.linear_model import LinearRegression, LassoLars, TweedieRegressor
from sklearn.preprocessing import PolynomialFeatures

from numpy import mean
from numpy import std, absolute
from sklearn.datasets import make_blobs
from sklearn.model_selection import LeaveOneOut
from sklearn.model_selection import cross_val_score
from sklearn.svm import SVR
from sklearn.ensemble import RandomForestRegressor

def feature_ranking(X_train_scaled, y_train):
    lm = LinearRegression()
    rfe = RFE(lm, 1)
    rfe.fit(X_train_scaled, y_train)
    ranks = rfe.ranking_
    names = X_train_scaled.columns.tolist()
    rankdf = pd.DataFrame({'features': names, 'rank': ranks}).set_index('rank').sort_values('rank')
    return rankdf

def cvLinearReg(X_train, y_train):
    # create loocv procedure
    cvLR = LeaveOneOut()
    # create model
    modelLR = LinearRegression()
    # evaluate model
    scoresLR = cross_val_score(modelLR, X_train, y_train, scoring='neg_mean_absolute_error', cv=cvLR, n_jobs=-1)
    # force positive
    scoresLR = absolute(scoresLR)
    # report performance
    print('MAE: %.3f (%.3f)' % (mean(scoresLR), std(scoresLR)))
    meanMAE = mean(scoresLR)
    stddevMAE = std(scoresLR)
    return meanMAE

def cvLassoLars(X_train, y_train, x):
    # LassoLars
    # create loocv procedure
    cvLL = LeaveOneOut()
    # create model
    modelLL = LassoLars(alpha=x)
    # evaluate model
    scoresLL = cross_val_score(modelLL, X_train, y_train, scoring='neg_mean_absolute_error', cv=cvLL, n_jobs=-1)
    # force positive
    scoresLL = absolute(scoresLL)
    # report performance
    print('MAE: %.3f (%.3f)' % (mean(scoresLL), std(scoresLL)))
    meanMAE = mean(scoresLL)
    stddevMAE = std(scoresLL)
    return meanMAE

def cvTweedie(X_train, y_train, pwr, alf):
    # Tweedie Regressor
    # create loocv procedure
    cvTW = LeaveOneOut()
    # create model
    modelTW = TweedieRegressor(power=pwr, alpha=alf) # 0 = normal distribution
    # evaluate model
    scoresTW = cross_val_score(modelTW, X_train, y_train, scoring='neg_mean_absolute_error', cv=cvTW, n_jobs=-1)
    # force positive
    scoresTW = absolute(scoresTW)
    # report performance
    print('MAE: %.3f (%.3f)' % (mean(scoresTW), std(scoresTW)))
    meanMAE = mean(scoresTW)
    stddevMAE = std(scoresTW)
    return meanMAE

def cvRandomForest(X_train, y_train, x):
    # Random Forest Regressor
    # create loocv procedure
    cvRF = LeaveOneOut()
    # create model
    modelRF = RandomForestRegressor(n_estimators=x, random_state = 123)
    # evaluate model
    scoresRF = cross_val_score(modelRF, X_train, y_train, scoring='neg_mean_absolute_error', cv=cvRF, n_jobs=-1)
    # force positive
    scoresRF = absolute(scoresRF)
    # report performance
    print('MAE: %.3f (%.3f)' % (mean(scoresRF), std(scoresRF)))
    meanMAE = mean(scoresRF)
    stddevMAE = std(scoresRF)
    return meanMAE

def cvSVR(X_train, y_train, x):
    # Support Vector Regressor
    # create loocv procedure
    cvSVR = LeaveOneOut()
    # create model
    modelSVR = SVR(kernel = x)
    # evaluate model
    scoresSVR = cross_val_score(modelSVR, X_train, y_train, scoring='neg_mean_absolute_error', cv=cvSVR, n_jobs=-1)
    # force positive
    scoresSVR = absolute(scoresSVR)
    # report performance
    print('MAE: %.3f (%.3f)' % (mean(scoresSVR), std(scoresSVR)))
    meanMAE = mean(scoresSVR)
    stddevMAE = std(scoresSVR)
    return meanMAE

def get_baseline_mean(y_train):
    '''
    Using mean gets baseline for y dataframe
    '''
    # determine Baseline to beat
    rows_needed = y_train.shape[0]
    # create array of predictions of same size as y_train.logerror based on the mean
    y_hat = np.full(rows_needed, np.mean(y_train))
    # calculate the MSE for these predictions, this is our baseline to beat
    baseline = mean_absolute_error(y_train, y_hat)
    print("Baseline MAE:", baseline)
    return baseline, y_hat

def get_baseline_median(y_train):
    '''
    Using median gets baseline for y dataframe
    '''
    # determine Baseline to beat
    rows_needed = y_train.shape[0]
    # create array of predictions of same size as y_train.logerror based on the median
    y_hat = np.full(rows_needed, np.median(y_train))
    # calculate the MSE for these predictions, this is our baseline to beat
    baseline = mean_absolute_error(y_train, y_hat)
    print("Baseline MAE:", baseline)
    return baseline, y_hat

def linear_reg_train(x_scaleddf, target):
    '''
    runs linear regression algorithm
    '''
    lm = LinearRegression()
    lm.fit(x_scaleddf, target)
    y_hat = lm.predict(x_scaleddf)

    LM_MAE = mean_absolute_error(target, y_hat)
    return LM_MAE

def lasso_lars(x_scaleddf, target):
    '''
    runs Lasso Lars algorithm
    '''
    # Make a model
    lars = LassoLars(alpha=1)
    # Fit a model
    lars.fit(x_scaleddf, target)
    # Make Predictions
    lars_pred = lars.predict(x_scaleddf)
    # Computer root mean squared error
    lars_MAE = mean_absolute_error(target, lars_pred)
    return lars_MAE

def polynomial2(X_trainsdf, target):
    '''
    runs polynomial algorithm
    '''
    # Make a model
    pf = PolynomialFeatures(degree=2)
    # Fit and Transform model to get a new set of features...which are the original features squared
    X_train_squared = pf.fit_transform(X_trainsdf)

    # Feed new features in to linear model.
    lm_squared = LinearRegression(normalize=True)
    lm_squared.fit(X_train_squared, target)
    # Make predictions
    lm_squared_pred = lm_squared.predict(X_train_squared)
    # Compute root mean squared error
    pf2_MAE = mean_absolute_error(target, lm_squared_pred)
    return pf2_MAE

def tweedie05(X_train_scaled, y_train):
    '''
    runs tweedie algorithm
    '''
    # Make Model
    tw = TweedieRegressor(power=0, alpha=.5) # 0 = normal distribution
    # Fit Model
    tw.fit(X_train_scaled, y_train)
    # Make Predictions
    tw_pred = tw.predict(X_train_scaled)
    # Compute root mean squared error
    tw_MAE = mean_absolute_error(y_train, tw_pred)
    return tw_MAE

def randomforest_test(x_scaleddf, target, X_test, y_test, est):
    '''
    runs random forest regressor
    '''
    # make model
    regressor = RandomForestRegressor(n_estimators = est, random_state = 123)
    # fit the model
    regressor.fit(x_scaleddf, target)
    # make predictions
    y_pred = regressor.predict(X_test)
    # calculate MAE
    randMAE = mean_absolute_error(y_test, y_pred)
    return randMAE, regressor

def lasso_lars_test(x_scaleddf, target, X_test, y_test):
    '''
    runs Lasso Lars algorithm
    '''
    # Make a model
    lars = LassoLars(alpha=1)
    # Fit a model
    lars.fit(x_scaleddf, target)
    # Make Predictions
    lars_pred = lars.predict(X_test)
    # calculate MAE
    lars_MAE = mean_absolute_error(y_test, lars_pred)
    return lars_MAE, lars, lars_pred

def linear_test(x_scaleddf, target, X_test, y_test):
    '''
    runs Lasso Lars algorithm
    '''
    # Make a model
    lm = LinearRegression()
    # Fit model on train dataset
    lm.fit(x_scaleddf, target)
    # Make Predictions on test dataset
    y_hat = lm.predict(X_test)
    # calculate MAE
    LM_MAE = mean_absolute_error(y_test, y_hat)
    return LM_MAE, lm, y_hat

def SVR_test(x_scaleddf, target, X_test, y_test, kern):
    '''
    runs Support Vector Regressor algorithm
    '''
    # Make a model
    regressor = SVR(kernel = kern)
    # Fit model on train dataset
    regressor.fit(x_scaleddf, target)
    # Make Predictions on test dataset
    y_hat = sc_y.inverse_transform(regressor.predict(sc_X.transform(X_test)))
    # calculate MAE
    svr_MAE = mean_absolute_error(y_test, y_hat)
    return svr_MAE, regressor

def tweedie_test(X_train, y_train, X_test, y_test, pwr, alf):
    '''
    runs tweedie algorithm
    '''
    # Make Model
    tw = TweedieRegressor(power=pwr, alpha=alf) # 0 = normal distribution
    # Fit Model
    tw.fit(X_train, y_train)
    # Make Predictions
    tw_pred = tw.predict(X_test)
    # Compute root mean squared error
    tw_MAE = mean_absolute_error(y_test, tw_pred)
    return tw_MAE, tw, tw_pred

def create_visualdf(y_test, y_train, y_test_predLL, y_test_predLR, y_test_predTW, y_test_predrTW):
    '''
    creates dataframe for making visualizations
    '''
    visualdf = pd.DataFrame()
    visualdf['actual'] = y_test.tract_cases_per_100k
    visualdf['baseline'] = y_train.tract_cases_per_100k.mean()
    visualdf['TWpred'] = y_test_predTW
    visualdf['LRpred'] = y_test_predLR
    visualdf['LLpred'] = y_test_predLL
    visualdf['SVI_only'] = y_test_predrTW
    return visualdf

def plot_actual_vs_predicted(visualdf):
    '''
    Produces subplots of actual VS predicted for the top models
    '''
    plt.figure(figsize=(16,8))
    #plt.suptitle('Plotting Actual Cases per 100K vs Predicted Cases per 100K')
    plt.plot(visualdf.actual, visualdf.baseline, alpha=.5, color="gray", label='_nolegend_')
    #plt.annotate("Baseline: Predict Using Mean", (15, 8))
    plt.plot(visualdf.actual, visualdf.actual, alpha=.5, color="blue", label='_nolegend_')
    #plt.annotate("The Ideal Line: Predicted = Actual", (.5, 1), rotation=15.5)
    #plt.subplot(1,3,1,)
    plt.scatter(visualdf.actual, visualdf.SVI_only,
               alpha=.5, color="blue", s=50, label="Model: TW SVI only")
    #plt.subplot(1,3,2)
    plt.scatter(visualdf.actual, visualdf.TWpred,
                alpha=.5, color="green", s=100, label="Model: TW Top 4 Features")
    #plt.subplot(1,3,3)
    # plt.scatter(visualdf.actual, visualdf.LLpred,
    #           alpha=.5, color="orange", s=75, label="Model: LassoLars")
    plt.legend()
    plt.xlabel("Actual Cases per 100K")
    plt.ylabel("Predicted Cases per 100K")

    # plt.annotate("The polynomial model appears to overreact to noise", (2.0, -10))
    # plt.annotate("The OLS model (LinearRegression)\n appears to be most consistent", (15.5, 3))
    plt.show()

def plotting_errors(visualdf):
    '''
    Plots the errors of the top models with zero error line
    '''
    # plotting Errors in Predictions
    plt.figure(figsize=(16,8))
    plt.axhline(label="No Error")
    plt.scatter(visualdf.actual, visualdf.SVI_only-visualdf.actual,
                alpha=.5, color="blue", s=50, label="Model: TW SVI only")
    plt.scatter(visualdf.actual, visualdf.TWpred-visualdf.actual,
                alpha=.5, color="green", s=100, label="Model: TW Top 4 Features")
    # plt.scatter(visualdf.actual, visualdf.LLpred-visualdf.actual,
    #             alpha=.5, color="orange", s=75, label="Model: LassoLars")
    plt.legend()
    plt.xlabel("Actual Cases per 100K")
    plt.ylabel("Residual/Error: Predicted Cases per 100K - Actual Cases per 100K")
    plt.title("Do the size of errors change as the actual value changes?")
    # plt.annotate("The polynomial model appears to overreact to noise", (2.0, -10))
    # plt.annotate("The OLS model (LinearRegression)\n appears to be most consistent", (15.5, 3))
    plt.show()

def plotting_histograms(visualdf):
    '''
    Plots Histograms of top models, currently only shows actual vs best performing
    '''
    plt.figure(figsize=(16,8))
    plt.hist(visualdf.actual, color='blue', alpha=.5, label="Actual Cases per 100K", histtype=u'step', linewidth=5)
    plt.hist(visualdf.LRpred, color='orange', alpha=.5, label="Model: TW SVI only")
    plt.hist(visualdf.TWpred, color='green', alpha=.5, label="Model: TW Top 4 Features")
    #plt.hist(visualdf.LLpred, color='red', alpha=.5, label="Model: LassoLars")
    plt.xlabel("Actual Cases per 100K")
    plt.ylabel("predictions")
    plt.title("SVI alone is not enough")
    plt.legend()
    plt.show()


from odoo import api, fields, models

class ProjectTask(models.Model):
    _inherit = "project.task"

    @api.depends(
        "stage_id",
        "timesheet_ids.unit_amount",
        "estimate_adjustment",
        "planned_hours",
        "child_ids.timesheet_ids.unit_amount",
        "child_ids.planned_hours",
        "child_ids.effective_hours",
        "child_ids.subtask_effective_hours",
        "child_ids.stage_id",
        "product_backlog_id.task_hours",
    )
    def _compute_hours_get(self):
        """ This method is used to calculate weightage based on task stage,
            timesheet amount, estimate adjustment, planned hours,
            child task planned hours, backlog task hours,
            child task effective hours etc """
        for task in self:
            weightage = children_hours = 0
            for child_task in task.child_ids:
                if child_task.stage_id and child_task.stage_id.fold:
                    children_hours += (
                        child_task.effective_hours + child_task.subtask_effective_hours
                    )
                else:
                    children_hours += max(
                        child_task.planned_hours,
                        child_task.effective_hours + child_task.subtask_effective_hours,
                    )

            task.subtask_effective_hours = children_hours
            task.effective_hours = sum(task.sudo().timesheet_ids.mapped("unit_amount"))
            task.remaining_hours = (
                task.planned_hours - task.effective_hours - task.subtask_effective_hours
            )
            # Commented this line as total hours replaced as total hours spent in v15
            # here both line added total hours and total hours spent
            #             task.total_hours = max(task.planned_hours, task.effective_hours)
            task.total_hours_spent = task.effective_hours + task.subtask_effective_hours
            #             task.delay_hours = max(-task.remaining_hours, 0.0)

            story_estimated_hours = task.product_backlog_id.expected_hours
            planned_hours = task.planned_hours
            estimate_adjustment = task.estimate_adjustment

            if story_estimated_hours > 0.0:
                weightage = planned_hours / story_estimated_hours
            hours = planned_hours + estimate_adjustment
            # hours = hours if hours > 0 else 1
            task.weightage = weightage

            if task.product_backlog_id.task_hours > 0:
                # New weightage calculation in Version 2
                task.weightage = hours / task.product_backlog_id.task_hours

            # New progress calculation in Version 2
            if task.effective_hours and hours > 0:
                task.progress = (task.effective_hours / hours) * 100

    name = fields.Char("Homework", size=256, translate=True)
    email = fields.Char(
        "Send mail",
        size=256,
        help="An email will be sent upon completion and upon validation of the"
        "Task to the following recipients. Separate with comma (,)"
        "each recipient ex: example@email.com, test@email.com",
    )
    task_number = fields.Char(
        "Task Number",
        readonly=True,
        copy=False,
        size=64,
        help="Sequence of the task number",
    )
    estimate_adjustment = fields.Float()
    weightage = fields.Float(compute="_compute_hours_get")
    schedule_date = fields.Datetime(help="Date scheduled for task")

    def _valid_field_parameter(self, field, name):
        return name == 'size' or super()._valid_field_parameter(field, name)

    @api.model
    def create(self, vals):
        result = super(ProjectTask, self).create(vals)
        if result.manager_id:
            result.message_unsubscribe(partner_ids=[result.manager_id.id])
        return result


import sys
import math

def list_mean(L):
    '''Compute the mean of an array. Expects a non-empty array.

    Parameters
    ----------
    L : list of int
        array containing numbers whose mean is desired.

    Returns
    -------
    m
        Arithmetic mean of the values in V

    '''
    if L is None:
        return None
    if len(L) == 0:
        return None

    s = 0

    for line in L:
        s += line

    m = sum(L)/len(L)
    return m

def list_stdev(L):
    '''Compute the standard deviation of an array. Expects a non-empty array.

    Parameters
    ----------
    L : list of int
        Non-empty array containing numbers whose standard deviation is desired.

    Returns
    -------
    sd
        Standard deviation of the values in V

'''
    if L is None:
        return None
    if len(L) == 0:
        return None
    else:
        mean = list_mean(L)

    sd = math.sqrt(sum([(mean-x)**2 for x in L]) / len(L))
    return sd


from angle_controller import *
from x_controller import *
from z_controller import *
from multirotore import *

class Autopilot:
	def __init__(self, x_target, z_target):
		self.drone = Multirotore(1.0, 0.25, 7.0*(10.0**(-5)))
		self.angle_controller = Angle_Controller(4.0, 1.57, 2.0, 0.2, 15.0)
		self.x_controller = X_Controller(0.4, 2.0, 0.5, 0.2, 0.52)
		self.z_controller = Z_Controller(4.0, 2.0, 20.0, 40.0, 15.0)

		self.x_target = x_target
		self.z_target = z_target
	def evaluate(self, delta_t):
		theta_target = (-1)*self.x_controller.evaluate(delta_t, self.x_target, self.drone.x, self.drone.vx)
		out_1 = self.angle_controller.evaluate(delta_t, theta_target, self.drone.theta, self.drone.omega)

		out_2 = self.z_controller.evaluate(delta_t, self.z_target, self.drone.z, self.drone.vz)

		f1 = out_2 - out_1
		f2 = out_2 + out_1

		self.drone.evaluate(delta_t, f1, f2)


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

"""
CS224N 2018-19: Homework 5
model_embeddings.py: Embeddings for the NMT model
Pencheng Yin <pcyin@cs.cmu.edu>
Sahil Chopra <schopra8@stanford.edu>
Anand Dhoot <anandd@stanford.edu>
Michael Hahn <mhahn2@stanford.edu>
"""

import torch.nn as nn

# Do not change these imports; your module names should be
#   `CNN` in the file `cnn.py`
#   `Highway` in the file `highway.py`
# Uncomment the following two imports once you're ready to run part 1(j)

from cnn import CNN
from highway import Highway

# End "do not change"

class ModelEmbeddings(nn.Module):
    """
    Class that converts input words to their CNN-based embeddings.
    """
    def __init__(self, embed_size, vocab):
        """
        Init the Embedding layer for one language
        @param embed_size (int): Embedding size (dimensionality) for the output
        @param vocab (VocabEntry): VocabEntry object. See vocab.py for documentation.
        """
        super(ModelEmbeddings, self).__init__()

        ## A4 code
        # pad_token_idx = vocab.src['<pad>']
        # self.embeddings = nn.Embedding(len(vocab.src), embed_size, padding_idx=pad_token_idx)
        ## End A4 code

        ### YOUR CODE HERE for part 1j

        self.e_char = 50
        self.w_word = 21  # same as max_word_length. same value (21) used in function pad_sents_char in utils.py
        self.embed_size = embed_size  # same as e_word

        self.char_embedding = nn.Embedding(len(vocab.char2id), self.e_char, vocab.char2id['<pad>'])
        self.cnn = CNN(self.e_char, self.embed_size, self.w_word)
        self.highway = Highway(self.embed_size)

        ### END YOUR CODE

    def forward(self, input):
        """
        Looks up character-based CNN embeddings for the words in a batch of sentences.
        @param input: Tensor of integers of shape (sentence_length, batch_size, max_word_length) where
            each integer is an index into the character vocabulary

        @param output: Tensor of shape (sentence_length, batch_size, embed_size), containing the
            CNN-based embeddings for each word of the sentences in the batch
        """
        ## A4 code
        # output = self.embeddings(input)
        # return output
        ## End A4 code

        ### YOUR CODE HERE for part 1j

        # x_padded has shape : (sentence_length, batch_size, max_word_length)
        x_padded = input

        # x_emb has shape : (sentence_length, batch_size, max_word_length, e_char)
        x_emb = self.char_embedding(x_padded)

        # x_reshape_4D has shape : (sentence_length, batch_size, e_char, max_word_length)
        x_reshape_4D = x_emb.permute(0, 1, 3, 2)

        sentence_length, batch_size, e_char, max_word_length = x_reshape_4D.shape

        # x_reshape has shape : (-1, e_char, max_word_length)
        x_reshape = x_reshape_4D.view(-1, e_char, max_word_length)

        # x_conv_out has shape : (-1, e_word)
        x_conv_out = self.cnn(x_reshape)

        # x_word_embed has shape : (-1, e_word)
        x_word_embed = self.highway(x_conv_out)

        output = x_word_embed.view(sentence_length, batch_size, self.embed_size)

        return output

        ### END YOUR CODE


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

from   _GTW                         import GTW
from   _TFL                         import TFL

from   _TFL.defaultdict             import defaultdict

import _GTW._OMP._PAP._E164.Country

class Country_33 (GTW.OMP.PAP.E164.Country_M) :
    """Provide phone number mapping for France."""

    generated_from     = \
        "https://en.wikipedia.org/wiki/Telephone_numbers_in_France"
    generation_date    = "2015-07-27 10:05"

    formatted_sn       = GTW.OMP.PAP.E164.Country_M.formatted_sn_4x2

    ndc_info_map       = \
        { "1" : "Île-de-France"
        , "2" : "Northwest France"
        , "3" : "Northeast France"
        , "4" : "Southeast France"
        , "5" : "Southwest France"
        , "6" : "Mobile phone services"
        , "7" : "Mobile phone services"
        , "8" : "Freephone (numéro vert) and shared-cost services."
        , "9" : "Non-geographic number (used by VoIP services)"
        }

    ndc_max_length     = 1

    ndc_types_normal   = {"geographic", "mobile", "voip"}

    ndc_usage_map      = \
        { "1" : "geographic"
        , "2" : "geographic"
        , "3" : "geographic"
        , "4" : "geographic"
        , "5" : "geographic"
        , "6" : "mobile"
        , "7" : "mobile"
        , "8" : "service"
        , "9" : "voip"
        }

    sn_max_length_map  = defaultdict (lambda : 8)
    sn_mix_length_map  = defaultdict (lambda : 8)

Country = Country_33 # end class

### __END__ GTW.OMP.PAP.E164._Country__33


from socket import *

class sending():
    def __init__(self, ip):
        # Opens the socket(connection)
        self.host = ip
        self.port = 13000
        self.addr = (self.host, self.port)
        self.UDPSock = socket(AF_INET, SOCK_DGRAM)

    def send(self, data):
        # Sends parameter data to other user
        self.UDPSock.sendto(data.encode(), self.addr)
    def const_send(self, data):
        # Sends parameter data to other user
        global stop
        while stop == False:
            self.UDPSock.sendto(data.encode(), self.addr)

    def close():
        #Closes the socket
        self.UDPSock.close()
        os._exit()

class recieving():
    def __init__(self):
        # Opens your socket the recieving data
        self.host = ""
        self.port = 13000
        self.buf = 320
        self.addr = (self.host, self.port)

        self.UDPSock = socket(AF_INET, SOCK_DGRAM)
        self.UDPSock.bind(self.addr)

    def recieve(self, select, attack, player, other, active):
        # Function to recieve data and call function

        recieved = "None"
        while True:
            while len(recieved) != 2:
                (data,addr) = self.UDPSock.recvfrom(self.buf)

                recieved = data.decode()
                recieved = recieved.split()

            return recieved

    def connect(self):
        # Function to recieve data and call function
        recieved = None

        while recieved == None:
            (data,addr) = self.UDPSock.recvfrom(self.buf)
            recieved = data.decode()
            return(recieved)

    def exit(self):
        # Closes Socket
        self.UDPSock.close()
        os._exit()
other_ip = " " #Put ip address of other person here

Sender = sending(other_ip)
Recieve = recieving()