import logging

import dnfdaemon.client
from gi.repository import Gdk

import yumex.common.const as const
from yumex.backend import Backend
from yumex.common import (
    CONFIG,
    exception_handler,
    timer,
    _,
    format_number,
    ngettext,
    pkg_id_to_full_name,
    to_pkg_tuple,
)

logger = logging.getLogger("yumex.yum_backend")

class DnfPackage:
    """package object for a package in the package system."""

    def __init__(self, po_tuple, action, backend):
        self.backend = backend
        (pkg_id, summary, size) = po_tuple
        self.pkg_id = pkg_id
        self.action = action
        (n, e, v, r, a, repo_id) = to_pkg_tuple(self.pkg_id)
        self.name = n
        self.epoch = e
        self.version = v
        self.release = r
        self.arch = a
        self.repository = repo_id
        self.visible = True
        self.selected = False
        self.downgrade_po = None
        self.summary = summary
        self.size = size
        self.sizeM = format_number(size)  # pylint: disable=invalid-name
        # cache
        self._description = None
        self.action = action
        self.queued = False
        self.recent = False
        self.selected = False

    def __str__(self):
        """String representation of the package object."""
        return self.fullname

    @property
    def fullname(self):
        return pkg_id_to_full_name(self.pkg_id)

    @exception_handler
    def get_attribute(self, attr):
        """Get a given attribute for a package."""
        return self.backend.GetAttribute(self.pkg_id, attr)

    @property
    def filename(self):
        """RPM filename of a package."""
        # the full path for at localinstall is stored in repoid
        if self.action == "li":
            return self.repository
        else:
            return f"{self.name}-{self.version}.{self.release}.{self.arch}.rpm"

    @property
    def fullver(self):
        """Package full version-release."""
        return f"{self.version}-{self.release}"

    @property
    def installed(self):
        return self.repository[0] == "@"

    @property
    def url(self):
        return self.get_attribute("url")

    def set_select(self, state):
        """Package is selected in package view."""
        self.selected = state

    def set_visible(self, state):
        """Package is visible in package view"""
        self.visible = state

    @property
    def description(self):
        return self.get_attribute("description")

    @property
    def changelog(self):
        return self.get_attribute("changelog")

    @property
    def filelist(self):
        return self.get_attribute("filelist")

    @property
    def pkgtags(self):
        return self.get_attribute("pkgtags")

    @property
    def color(self):
        """Package color to show in package view."""
        color = CONFIG.session.color_normal
        if self.action == "u":
            color = CONFIG.session.color_update
        elif self.action == "o":
            color = CONFIG.session.color_obsolete
        elif self.action == "do":
            color = CONFIG.session.color_downgrade
        elif self.action == "r":
            color = CONFIG.session.color_install
        rgba = Gdk.RGBA()
        rgba.parse(color)
        return rgba

    @property
    @exception_handler
    def downgrades(self):
        return self.backend.get_downgrades(self.pkg_id)

    @property
    @exception_handler
    def updateinfo(self):
        return self.get_attribute("updateinfo")

    @property
    @exception_handler
    def requirements(self):
        return self.get_attribute("requires")

    @property
    def is_update(self):
        """Package is an update/replacement to another package."""
        return self.action == "o" or self.action == "u"

    def exception_handler(self, e):
        """
        send exceptions to the frontend
        """
        self.backend.frontend.exception_handler(e)

class DnfRootBackend(Backend, dnfdaemon.client.Client):
    """Backend to do all the dnf related actions"""

    def __init__(self, frontend):
        Backend.__init__(self, frontend, filters=True)
        dnfdaemon.client.Client.__init__(self)
        self.gpg_confirm = None
        self.dnl_progress = None
        self._files_to_download = 0
        self._files_downloaded = 0
        self._current_download = None
        self._dnl_packages = None
        if self.running_api_version == const.NEEDED_DAEMON_API:
            logger.debug(f"dnfdaemon api version ({self.running_api_version})")
        else:
            raise dnfdaemon.client.APIVersionError(
                _(
                    f"dnfdaemon api version : {self.running_api_version}"
                    "\ndon't match"
                    "\nneeded api version : {const.NEEDED_DAEMON_API}"
                )
            )

    def on_TransactionEvent(self, event, data):
        if event == "start-run":
            pass
        elif event == "download":
            self.frontend.infobar.message(_("Downloading packages"))
        elif event == "pkg-to-download":
            self._dnl_packages = data
        elif event == "signature-check":
            self.frontend.infobar.set_progress(0.0)
            self.frontend.infobar.message(_("Checking package signatures"))
            self.frontend.infobar.set_progress(1.0)
            self.frontend.infobar.message_sub("")
        elif event == "run-test-transaction":
            # self.frontend.infobar.message(_('Testing Package Transactions')) #
            # User don't care
            pass
        elif event == "run-transaction":
            self.frontend.infobar.message(_("Applying changes to the system"))
            self.frontend.infobar.message_sub("")
        elif event == "verify":
            self.frontend.infobar.message(_("Verify changes on the system"))
            self.frontend.infobar.message_sub("")
        # elif event == '':
        elif event == "fail":
            self.frontend.infobar.hide()
        elif event == "end-run":
            self.frontend.infobar.hide()
        else:
            logger.debug(f"TransactionEvent : {event}")

    def on_RPMProgress(
        self, package, action, te_current, te_total, ts_current, ts_total
    ):
        num = f" ( {ts_current}/{ts_total} )"
        if "," in package:  # this is a pkg_id
            name = pkg_id_to_full_name(package)
        else:  # this is just a pkg name (cleanup)
            name = package
        # logger.debug('on_RPMProgress : [%s]', package)
        action_msg = const.RPM_ACTIONS.get(action, None)
        if action_msg:
            self.frontend.infobar.message_sub(action_msg % name)
        else:
            logger.info(f"RPM Progress: Undefinded action {action}")
        if 0 < ts_current <= ts_total:
            frac = float(ts_current) / float(ts_total)
            self.frontend.infobar.set_progress(frac, label=num)

    def on_GPGImport(self, pkg_id, userid, hexkeyid, keyurl, timestamp):
        values = (pkg_id, userid, hexkeyid, keyurl, timestamp)
        self.gpg_confirm = values
        logger.debug(f"received signal : GPGImport {repr(values)}")

    def on_DownloadStart(self, num_files, num_bytes):
        """Starting a new parallel download batch."""
        self._files_to_download = num_files
        self._files_downloaded = 0
        self.frontend.infobar.set_progress(0.0)
        self.frontend.infobar.message_sub(
            # Translators: %d will be replaced with the number of files
            # to download; %s will be replaced with the preformatted
            # number of bytes to download + the prefix (k, M, etc.)
            # Note that 'B' for 'bytes' is already here, it must be preserved.
            ngettext(
                "Downloading %d file (%sB)...",
                "Downloading %d files (%sB)...",
                num_files,
            )
            % (num_files, format_number(num_bytes))
        )

    def on_DownloadProgress(self, name, frac, total_frac, total_files):
        """Progress for a single element in the batch."""
        num = f"( {self._files_downloaded}/{self._files_to_download} )"
        self.frontend.infobar.set_progress(total_frac, label=num)

    def on_DownloadEnd(self, name, status, msg):
        """Download of af single element ended."""
        if status == -1 or status == 2:  # download OK or already exists
            logger.debug(f"Downloaded : {name}")
            self._files_downloaded += 1
            self.frontend.infobar.message_sub(
                f"{name} - ({self._files_downloaded}/{self._files_to_download})"
            )
        else:
            logger.debug(f"Download Error : {name} - {msg} (status : {status} )")

    def on_RepoMetaDataProgress(self, name, frac):
        """Repository Metadata Download progress."""
        values = (name, frac)
        logger.debug(f"on_RepoMetaDataProgress (root): {repr(values)}")
        if frac == 0.0:
            self.frontend.infobar.message_sub(name)
        else:
            self.frontend.infobar.set_progress(frac)

    def setup(self):
        """Setup the dnf backend daemon."""
        try:
            self.Lock()
            self.SetWatchdogState(False)
            self._update_config_options()
            return True, ""
        except dnfdaemon.client.AccessDeniedError:
            return False, "not-authorized"
        except dnfdaemon.client.LockedError:
            return False, "locked-by-other"

    @exception_handler
    def quit(self):
        """Quit the dnf backend daemon."""
        self.Unlock()
        self.Exit()

    @exception_handler
    def reload(self):
        """Reload the dnf backend daemon."""
        self.Unlock()  # Release the lock
        # time.sleep(5)
        self.Lock()  # Load & Lock the daemon
        self.SetWatchdogState(False)
        # self._update_config_options()
        self.cache.reset()  # Reset the cache

    def _update_config_options(self):
        if CONFIG.session.clean_instonly:
            self.SetConfig("installonly_limit", CONFIG.conf.installonly_limit)
            logger.debug(f"installonly_limit = {CONFIG.conf.installonly_limit}")
        else:
            self.SetConfig("installonly_limit", "<off>")
            logger.debug("installonly_limit = <off>")
        self.SetConfig("clean_requirements_on_remove", CONFIG.session.clean_unused)
        logger.debug(f"clean_requirements_on_remove = {CONFIG.session.clean_unused}")
        if CONFIG.session.enabled_repos:
            logger.debug(f"root: Setting repos : {CONFIG.session.enabled_repos}")
            self.SetEnabledRepos(CONFIG.session.enabled_repos)

    def _make_pkg_object(self, pkgs, flt):
        """Get a list Package objects from a list of pkg_ids & attrs.

        All packages has the same action type.
        Package object are taken from cache if available.

        :param pkgs: list of (pkg_id, summary, size)
        :param flt: pkg_filter (installed, available ....)
        """
        # TODO: should be combined with _make_pkg_object_with_attr
        # No need for 3 almost indentical way to make a list of package objects
        po_list = []
        action = const.FILTER_ACTIONS[flt]
        for pkg_values in pkgs:
            po_list.append(DnfPackage(pkg_values, action, self))
        return self.cache.find_packages(po_list)

    @timer
    def _make_pkg_object_with_attr(self, pkgs):
        """Make list of Packages from a list of pkg_ids & attrs.

        Package have different action type
        Package object are taken from cache if available.

        :param pkgs: list with (pkg_id, summary, size, action)
        """
        po_list = []
        for elem in pkgs:
            (pkg_id, summary, size, action) = elem
            po_tuple = (pkg_id, summary, size)
            po_list.append(DnfPackage(po_tuple, const.BACKEND_ACTIONS[action], self))
        return self.cache.find_packages(po_list)

    def _build_package_list(self, pkg_ids):
        """Make list of Packages from a list of pkg_ids

        Summary, size and action is read from dnf backend

        Package object are taken from cache if available.

        :param pkg_ids:
        """
        # TODO: should be combined with _make_pkg_object_with_attr
        # No need for 3 almost indentical way to make a list of package objects
        po_list = []
        for pkg_id in pkg_ids:
            summary = self.GetAttribute(pkg_id, "summary")
            size = self.GetAttribute(pkg_id, "size")
            pkg_values = (pkg_id, summary, size)
            action = const.BACKEND_ACTIONS[self.GetAttribute(pkg_id, "action")]
            po_list.append(DnfPackage(pkg_values, action, self))
        return self.cache.find_packages(po_list)

    @exception_handler
    @timer
    def get_packages(self, flt):
        """Get packages for a given pkg filter."""
        logger.debug(f"get-packages : {flt} ")
        if flt == "all":
            filters = ["installed", "updates", "available"]
        else:
            filters = [flt]
        result = []
        for pkg_flt in filters:
            # is this type of packages is already cached ?
            if not self.cache.is_populated(pkg_flt):
                fields = ["summary", "size"]  # fields to get
                po_list = self.GetPackages(pkg_flt, fields)
                if pkg_flt == "updates_all":
                    pkg_flt = "updates"
                pkgs = self._make_pkg_object(po_list, pkg_flt)
                self.cache.populate(pkg_flt, pkgs)
            result.extend(Backend.get_packages(self, pkg_flt))
        logger.debug(f" number of packages = {len(result)}")
        return result

    @exception_handler
    def get_downgrades(self, pkg_id):
        """Get downgrades for a given pkg_id"""
        pkgs = self.GetAttribute(pkg_id, "downgrades")
        return self._build_package_list(pkgs)

    @exception_handler
    def get_repo_ids(self, flt):
        """Get repository ids"""
        repos = self.GetRepositories(flt)
        return repos

    @exception_handler
    def get_repositories(self, flt="*"):
        """Get a list of repo attributes to populate repo view."""
        repo_list = []
        repos = self.GetRepositories(flt)
        for repo_id in repos:
            if repo_id.endswith("-source") or repo_id.endswith("-debuginfo"):
                continue
            repo = self.GetRepo(repo_id)
            repo_list.append([repo["enabled"], repo_id, repo["name"], False])
        return sorted(repo_list, key=lambda elem: elem[1])

    @timer
    @exception_handler
    def get_packages_by_name(self, name_key, newest_only):
        """Get packages by a given name wildcard.

        :param name_key: package wildcard
        :param newest_only: get lastest version only
        """
        attrs = ["summary", "size", "action"]
        pkgs = self.GetPackagesByName(name_key, attrs, newest_only)
        return self._make_pkg_object_with_attr(pkgs)

    @exception_handler
    def search(self, search_attrs, keys, match_all, newest_only, tags):
        """Search given pkg attributes for given keys.

        :param search_attrs: package attrs to search in
        :param keys: keys to search for
        :param match_all: match all keys
        :param newest_only:
        :param tags:
        """
        attrs = ["summary", "size", "action"]
        pkgs = self.Search(search_attrs, keys, attrs, match_all, newest_only, tags)
        return self._make_pkg_object_with_attr(pkgs)

    @exception_handler
    def get_groups(self):
        """Get groups/categories from dnf daemon backend"""
        result = self.GetGroups()
        return result

    @timer
    def get_group_packages(self, grp_id, grp_flt):
        """Get a list of packages from a grp_id and a group filter.

        :param grp_id:
        :param grp_flt:
        """
        attrs = ["summary", "size", "action"]
        pkgs = self.GetGroupPackages(grp_id, grp_flt, attrs)
        return self._make_pkg_object_with_attr(pkgs)


"""
Binary Search Tree implementation in Python 3.0.

Main difference between a normal tree and a binary search tree is the ordering of elements.
In binary search tree, the parent node is greater than the left child and smaller than the right child.

By using this condition, the runtime of operations like inserting decreases from O(n) to O(logn)
"""

class Node(object):
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

class BST(object):
    def __init__(self, root):
        self.root = Node(root)

    def search(self, val):
        return self.preorder_search(self.root, val)

    def preorder_search(self, start, val):
        if start:
            if start.value == val:
                return True
            else:
                if start.value > val:
                    return preorder_search(start.left, val)
                else:
                    return preorder_search(start.right, val)
        return False

    def insert(self, new_element):
        self.preorder_insert(self.root, new_element)

    def preorder_insert(self, start, new_element):
        if start.value < new_element:
            if start.right:
                self.preorder_insert(start.right, new_element)
            else:
                start.right = Node(new_element)
        else:
            if start.left:
                self.preorder_insert(start.left, new_element)
            else:
                start.left = Node(new_element)

    def print_tree(self):
        return self.preorder_print(self.root, "")

    def preorder_print(self, start, traversal):
        if start:
            traversal += str(start.value)
            traversal = self.preorder_print(start.left, traversal)
            traversal = self.preorder_print(start.right, traversal)
        return traversal

if __name__ == "__main__":
    # test case
    tree = BST(4)

    #         4
    #       /   \
    #      2     5
    #     / \
    #    1   3
    tree.insert(2)
    tree.insert(1)
    tree.insert(3)
    tree.insert(5)

    # output is True
    print(tree.search(4))

    # output is False
    print(tree.search(6))

    # output is 42135
    print(tree.print_tree())


#!/usr/bin/python3
"""
pascal_triangle module.
"""

def pascal_triangle(n):
    """
    Returns an empty list if n <= 0
    """
    if n <= 0:
        return []

    rows = [[1 for j in range(i + 1)] for i in range(n)]
    n_val = 0

    while n_val < n:
        i = 0
        while i < n_val - 1:
            rows[n_val][i + 1] = sum(rows[n_val - 1][i:i + 2])
            i += 1
        n_val += 1

    return rows


import numpy

neuronsInput = 3
neuronsOutput = 4
neuronsHidden = 3
nrHidden = 1

def activate(x, synapses):
    s = numpy.dot(x, synapses)
    return 1/(1+numpy.exp(-s))

def derivate(x):
    return x * (1 - x)

numpy.random.seed(1)

synapses = []
for i in range(neuronsHidden):
    synapses.append(2*numpy.random.random((neuronsInput, neuronsOutput)) -1)

def readData(self, fileName):
    f = open(fileName, 'r')

    data = []
    i = 0
    for line in f:
        data.append(line.strip().split(' '))

        # convert to float
        for j in range(0, len(data[i]) - 1):
            data[i][j] = float(data[i][j])
        i += 1
    return data

input = numpy.array([[0, 0, 1],
                     [1, 1, 1],
                     [1, 0, 1],
                     [0, 1, 1]])
trainingOutput = numpy.array(
    [[0, 1, 1, 0]]
).T

for i in range(10000):
    output = []
    error = []
    adjustments = []
    for j in range(neuronsHidden):
        output.append(activate(input, synapses[j]))
    for j in range(neuronsHidden):
        error.append(trainingOutput - output[j])
    for j in range(len(error)):
        adjustments.append(numpy.dot(input.T, error[j]*derivate(output[j])))
    for j in range(len(error)):
        synapses[j] += adjustments[j]

print(synapses, " POWERPLANT")

for j in synapses:
    print(activate([1, 1, 0], ))


# This problem was asked by Google.
# Find the minimum number of coins required to make n cents.
# You can use standard American denominations, that is, 1¢, 5¢, 10¢, and 25¢.
# For example, given n = 16, return 3 since we can make it with a 10¢, a 5¢, and a 1¢.
from typing import List

# Greedy (Works due to currency)
def get_least_coins(denomination: List[int], amount: int) -> int:
    coins = 0
    for coin in sorted(denomination, reverse=True):
        coins += amount // coin
        amount = amount % coin
    return coins

if __name__ == '__main__':
    denomination = [1, 5, 10, 25]
    amount = 16
    print(get_least_coins(denomination, amount))


class Solution:
    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        cur, stack, res = root, [], []

        while cur or stack:
            if cur:
                stack.append(cur)
                cur = cur.left
            else:
                cur = stack.pop()
                res.append(cur.val)
                cur = cur.right

        return res


import os
import requests, json
from flask import Flask,redirect,url_for,render_template,request
from steam_api import get_csgo3, get_csgo4, get_csgo5
from helper import fill_till

app = Flask(__name__)

@app.route('/',methods=['GET','POST'])
def home():
    output1 = ""
    output2 = ""
    output3 = "TEST"

    id = "76561198800721901"

    try:
        output1 = get_csgo3(id)
    except Exception as E:
        print(E)
        output1 = "E" + str(E)

    try:
        output2 = get_csgo4(id)
    except Exception as E:
        print(E)
        output2 = "E" + str(E)

    try:
        output3 = get_csgo5()
    except Exception as E:
        print(E)
        output3 = "E" + str(E)

    if request.method=='POST':
        id = request.form['player_id']

        try:
            output1 = get_csgo3(id)
        except Exception as E:
            print(E)
            output1 = "E" + str(E)

        try:
            output2 = get_csgo4(id)
        except Exception as E:
            print(E)
            output2 = "E" + str(E)

        try:
            output3 = get_csgo5()
        except Exception as E:
            print(E)
            output3 = "E" + str(E)

        return render_template('index.html', output1=output1, output2=output2, output3=output3)
    return render_template('index.html', output1=output1, output2=output2, output3=output3)

@app.route('/last',methods=['GET','POST'])
def last():
    output1 = get_csgo5()
    print(">>> output1", output1)
    #output1['last_match_wins'] = "99999"
    return render_template('last.html', output1=output1)

@app.route('/api',methods=['GET','POST'])
def api():
    data = get_csgo5()
    error = ""
    print(">>> output1", data)
    output = "```"

    output += "╔══════════════════╦═══════════════════════╗ \n"
    output += "║ LAST ROUND STATS ║   Rounds won: " + fill_till(str(data['alex_last_match_wins']), 7) + " ║ \n"
    output += "╚══════════════════╩═══════════════════════╝ \n"
    output += "\n"
    output += "╔═══════════╦═══════╦════════╦═════════════╗ \n"
    output += "║ Player    ║ Kills ║ Deaths ║ Damage done ║ \n"
    output += "╠═══════════╬═══════╬════════╬═════════════╣ \n"
    if 'kristiaan_last_match_kills' in data:
        output +=  "║ " + fill_till("Kristiaan", 9)
        output += " ║   " + fill_till(str(data['kristiaan_last_match_kills']), 3)
        output += " ║   " + fill_till(str(data['kristiaan_last_match_deaths']), 4)
        output += " ║ " + fill_till("   " + str(data['kristiaan_last_match_damage']), 11)
        output += " ║\n"
    else:
        error +=  "Kristiaan's Steam profile is set to private\n"

    if 'muffin_last_match_kills' in data:
        output +=  "║ " + fill_till("Muffin", 9)
        output += " ║   " + fill_till(str(data['muffin_last_match_kills']), 3)
        output += " ║   " + fill_till(str(data['muffin_last_match_deaths']), 4)
        output += " ║ " + fill_till("   " + str(data['muffin_last_match_damage']), 11)
        output += " ║\n"
    else:
        error +=  "Muffin's Steam profile is set to private\n"

    if 'devlin_last_match_kills' in data:
        output +=  "║ " + fill_till("Devlin", 9)
        output += " ║   " + fill_till(str(data['devlin_last_match_kills']), 3)
        output += " ║   " + fill_till(str(data['devlin_last_match_deaths']), 4)
        output += " ║ " + fill_till("   " + str(data['devlin_last_match_damage']), 11)
        output += " ║\n"
    else:
        error +=  "Devlin's Steam profile is set to private\n"

    if 'alex_last_match_kills' in data:
        output +=  "║ " + fill_till("Alex", 9)
        output += " ║   " + fill_till(str(data['alex_last_match_kills']), 3)
        output += " ║   " + fill_till(str(data['alex_last_match_deaths']), 4)
        output += " ║ " + fill_till("   " + str(data['alex_last_match_damage']), 11)
        output += " ║\n"
    else:
        error +=  "Alex' Steam profile is set to private\n"

    output += "╚═══════════╩═══════╩════════╩═════════════╝ \n"
    output += "\n"
    output += error
    output += "\n"
    output += "```"
    output += "\n"
    return output

@app.route('/api1', methods=['POST', 'GET'])
def api2():
    return '''╔══╦══╦══╦══╦══╗
║  ║  ║  ║  ║  ║
╠══╬══╬══╬══╬══╣
║  ║  ║  ║  ║  ║
╠══╬══╬══╬══╬══╣
║  ║  ║  ║  ║  ║
╠══╬══╬══╬══╬══╣
║  ║  ║  ║  ║  ║
╚══╩══╩══╩══╩══╝'''

if __name__ == '__main__':
    #DEBUG is SET to TRUE. CHANGE FOR PROD
    app.run(port=5000,debug=True)


import os

CHANNELS = 1
SAMPLE_RATE = 16000
BITS_PER_SAMPLE = 16

COMPTYPE = 'NONE'
COMPNAME = 'not compressed'

RES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../resources/pmdl')

DEFAULT_LANG = 'en'
VAD_RESOURCE = os.path.join(RES_DIR, DEFAULT_LANG, 'personal_enroll.res')

def get_enroll_resource(lang=DEFAULT_LANG):
    res = os.path.join(RES_DIR, lang, 'personal_enroll.res')
    if not os.path.isfile(res):
        res = os.path.join(RES_DIR, DEFAULT_LANG, 'personal_enroll.res')

    return str(res)

def get_detect_resource(lang=DEFAULT_LANG):
    res = os.path.join(RES_DIR, lang, 'common.res')
    if not os.path.isfile(res):
        res = os.path.join(RES_DIR, DEFAULT_LANG, 'common.res')

    return str(res)


import numpy as np
import matplotlib.pyplot as plt
import math
from scipy.misc import face
from skimage.measure import label, regionprops
from skimage import color
from os import listdir
from scipy.ndimage import label
from skimage.filters import threshold_otsu, gaussian
from skimage.exposure import adjust_sigmoid
from skimage.transform import resize

def check(image, y, x):
	if not 0 <= x < image.shape[1]:
		return False
	if not 0 <= y < image.shape[0]:
		return False
	if image[y, x] != 0:
		return True
	return False

def neghbrs_arnd(image, y, x):
	left = y, x - 1
	top = y - 1, x
	right = y, x + 1
	down = y + 1, x
	if not check(image, *left):
		left = None, None
	if not check(image, *top):
		top = None, None
	if not check(image, *right):
		right = None, None
	if not check(image, *down):
		down = None, None
	return left, top, right, down

def area(LB, label = 1):
	pxs = np.where(LB == label)
	return len(pxs[0])

def get_boundaries(LB, label = 1):
	pxs = np.where(LB == label)
	boundaries = []
	for y, x in zip(*pxs):
		for yn, xn, in neghbrs_arnd(LB, y, x):
			if yn == None:
				boundaries.append((y,x))
				break
			elif xn == None:
				boundaries.append((y,x))
				break
			elif LB[yn, xn] != label:
				boundaries.append((y,x))
				break
	return boundaries

def perimeter(LB, label = 1):
	return len(get_boundaries(LB, label))

path = "images/"
files = [file for file in listdir(path)]
pencils = 0

for file in files:
    image = plt.imread(path + file)
    bw = color.rgb2gray(image)
    bw = resize(bw, (bw.shape[0]//10, bw.shape[1]//10))
    bw = adjust_sigmoid(bw, cutoff=10, gain=3)
    bw = gaussian(bw, sigma=3)
    binary = bw.copy()
    binary[bw >= threshold_otsu(bw)] = 0
    binary[binary > 0] = 1
    labeled = label(binary)[0]
    for i in range(1, np.max(labeled) + 1):
        figure_area = area(labeled, i)
        figure_perimeter = perimeter(labeled, i)
        area_perimeter_div = figure_area / figure_perimeter
        if 6 < area_perimeter_div < 10 and 3000 < figure_area < 5000:
            pencils += 1

print(pencils)


#Permitirle al usuario ingresar un diccionario de comidas donde las claves son el
#nombre de las comidas y el valor el precio de estas. Las comidas se pueden ingresar
#infinitamente hasta que el precio de una sea 0. Una vez finalizado el ingreso, debemos
#mostrarle al usuario las comidas con sus respectivos precios y permitirle elegir una y la
#cantidad que va a comer. Si la comida que eligió el usuario no existe debemos mostrar
#un error. Si la comida existe, mostrar el precio total que va a pagar.

menu = {}

comida = str(input("Ingrese nombre de la comida: "))
precio = int(input("Ingrese el valor de ese plato: "))

while precio > 0:

    if(precio > 0):
        menu[comida] = precio
        comida = str(input("Ingrese nombre de la comida: "))
        precio = int(input("Ingrese el valor de ese plato: "))

for platillo in menu.keys():
    print(f"{platillo} ......... $ {menu[platillo]}")

pedido = str(input("Que platillo va a pedir?: "))
cantidad = int(input("Que cantidad de platillos va a pedir?: "))

if pedido in menu.keys():
    print(f"Usted encargo {cantidad} {pedido} con un valor total de: $ {menu[pedido]*cantidad} ")
else:
    print("Error el platillo que ingreso no se encuentra en el menu")


import numpy as np
import cv2
from rknn.api import RKNN

if __name__ == '__main__':

    # Create RKNN object
    rknn = RKNN()

    # load rknn
    print('--> Load rknn model')
    ret = rknn.load_rknn("./examples/squeezenet_test/squeezenet.rknn")
    if ret != 0:
        print('Load rknn model failed')
        exit(ret)
    print('done')

    # Set inputs
    input_data = cv2.imread("./examples/squeezenet_test/squeezenet_test.jpg")
    # Init runtime environment
    print('--> Init runtime environment')
    ret = rknn.init_runtime()
    if ret != 0:
        print('Init runtime environment failed')
        exit(ret)
    print('done')

    # Inference
    print('--> Running model')
    outputs = rknn.inference(inputs=[input_data])
    print('done')

    print("**********************")
    pred_output = outputs[0].reshape((1000,)).astype(np.float32)
    top_k=5
    top_k_idx=pred_output.argsort()[::-1][0:top_k]
    print(pred_output[top_k_idx])
    rknn.release()


import spotipy
from spotipy.oauth2 import SpotifyOAuth
import openpyxl

# Configura las credenciales de la API de Spotify
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id='id_cliente', #esto lo consigues en la pagina sporify developer
                                               client_secret='id_secreto', #al igual que este
                                               redirect_uri='http://localhost:8080',
                                               scope='playlist-read-private'))

playlist_id = '7a802Q3DsRUdBkDNYrUaJ0' #este es el id de la playlist , en este caso yo sacare las canciones de cada playlist, esto lo consigues en la url de la playlist

# Obtiene las canciones de la playlist
tracks = sp.playlist_tracks(playlist_id)

# Crea un nuevo archivo de Excel
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.title = "Canciones de la Playlist"

# Agrega encabezados a las columnas
sheet['A1'] = 'Número'
sheet['B1'] = 'Artista'
sheet['C1'] = 'Canción'

# Llena el archivo de Excel con la información de las canciones
for idx, track in enumerate(tracks['items'], start=2):
    sheet[f'A{idx}'] = idx - 1
    sheet[f'B{idx}'] = track['track']['artists'][0]['name']
    sheet[f'C{idx}'] = track['track']['name']

# Guarda el archivo de Excel
workbook.save("canciones_de_mi_amor.xlsx")

print("Las canciones de la playlist se han guardado en canciones_de_playlist.xlsx")


import os
from typing import Sequence

from pyrogram.types import ChatPermissions

from userge import Message, userge

CHANNEL = userge.getCLogger(__name__)

_types = [
    "msg",
    "media",
    "polls",
    "invite",
    "pin",
    "info",
    "webprev",
    "inlinebots",
    "animations",
    "games",
    "stickers",
]

def _get_chat_lock(
    message: Message, lock_type: str, should_lock: bool
) -> Sequence[str]:
    if should_lock is True:
        lock = False
    else:
        lock = True
    msg = message.chat.permissions.can_send_messages
    media = message.chat.permissions.can_send_media_messages
    stickers = message.chat.permissions.can_send_stickers
    animations = message.chat.permissions.can_send_animations
    games = message.chat.permissions.can_send_games
    inlinebots = message.chat.permissions.can_use_inline_bots
    webprev = message.chat.permissions.can_add_web_page_previews
    polls = message.chat.permissions.can_send_polls
    info = message.chat.permissions.can_change_info
    invite = message.chat.permissions.can_invite_users
    pin = message.chat.permissions.can_pin_messages
    perm = None

    if lock_type == "msg":
        msg = lock
        perm = "messages"
    elif lock_type == "media":
        media = lock
        perm = "audios, documents, photos, videos, video notes, voice notes"
    elif lock_type == "stickers":
        stickers = lock
        perm = "stickers"
    elif lock_type == "animations":
        animations = lock
        perm = "animations"
    elif lock_type == "games":
        games = lock
        perm = "games"
    elif lock_type == "inlinebots":
        inlinebots = lock
        perm = "inline bots"
    elif lock_type == "webprev":
        webprev = lock
        perm = "web page previews"
    elif lock_type == "polls":
        polls = lock
        perm = "polls"
    elif lock_type == "info":
        info = lock
        perm = "info"
    elif lock_type == "invite":
        invite = lock
        perm = "invite"
    elif lock_type == "pin":
        pin = lock
        perm = "pin"
    return (
        msg,
        media,
        stickers,
        animations,
        games,
        inlinebots,
        webprev,
        polls,
        info,
        invite,
        pin,
        perm,
    )

@userge.on_cmd(
    "lock",
    about={
        "header": "use this to lock group permissions",
        "description": "Allows you to lock some common permission types in the chat.\n"
        "[NOTE: Requires proper admin rights in the chat!!!]",
        "types": [
            "all",
            "msg",
            "media",
            "polls",
            "invite",
            "pin",
            "info",
            "webprev",
            "inlinebots",
            "animations",
            "games",
            "stickers",
        ],
        "examples": "{tr}lock [all | type]",
    },
    allow_channels=False,
    check_restrict_perm=True,
)
async def lock_perm(message: Message):
    """lock chat permissions from tg group"""
    lock_type = message.input_str
    chat_id = message.chat.id
    if not lock_type:
        await message.err(r"I Can't Lock Nothing! (－‸ლ)")
        return
    if lock_type == "all":
        try:
            await message.client.set_chat_permissions(chat_id, ChatPermissions())
            await message.edit("**🔒 Locked all permission from this Chat!**", del_in=5)
            await CHANNEL.log(
                f"#LOCK\n\nCHAT: `{message.chat.title}` (`{chat_id}`)\n"
                f"PERMISSIONS: `All Permissions`"
            )
        except Exception as e_f:
            await message.edit(
                r"`i don't have permission to do that ＞︿＜`\n\n" f"**ERROR:** `{e_f}`",
                del_in=5,
            )
        return
    if lock_type in _types:
        (
            msg,
            media,
            stickers,
            animations,
            games,
            inlinebots,
            webprev,
            polls,
            info,
            invite,
            pin,
            perm,
        ) = _get_chat_lock(message, lock_type, True)
    else:
        await message.err(r"Invalid lock type! ¯\_(ツ)_/¯")
        return
    try:
        await message.client.set_chat_permissions(
            chat_id,
            ChatPermissions(
                can_send_messages=msg,
                can_send_media_messages=media,
                can_send_stickers=stickers,
                can_send_animations=animations,
                can_send_games=games,
                can_use_inline_bots=inlinebots,
                can_add_web_page_previews=webprev,
                can_send_polls=polls,
                can_change_info=info,
                can_invite_users=invite,
                can_pin_messages=pin,
            ),
        )
        await message.edit(f"**🔒 Locked {perm} for this chat!**", del_in=5)
        await CHANNEL.log(
            f"#LOCK\n\nCHAT: `{message.chat.title}` (`{chat_id}`)\n"
            f"PERMISSIONS: `{perm} Permission`"
        )
    except Exception as e_f:
        await message.edit(
            r"`i don't have permission to do that ＞︿＜`\n\n" f"**ERROR:** `{e_f}`",
            del_in=5,
        )

@userge.on_cmd(
    "unlock",
    about={
        "header": "use this to unlock group permissions",
        "description": "Allows you to unlock some common permission types in the chat.\n"
        "[NOTE: Requires proper admin rights in the chat!!!]",
        "types": [
            "all",
            "msg",
            "media",
            "polls",
            "invite",
            "pin",
            "info",
            "webprev",
            "inlinebots",
            "animations",
            "games",
            "stickers",
        ],
        "examples": "{tr}unlock [all | type]",
    },
    allow_channels=False,
    check_restrict_perm=True,
)
async def unlock_perm(message: Message):
    """unlock chat permissions from tg group"""
    unlock_type = message.input_str
    chat_id = message.chat.id
    if not unlock_type:
        await message.err(r"I Can't Unlock Nothing! (－‸ლ)")
        return
    if unlock_type == "all":
        try:
            await message.client.set_chat_permissions(
                chat_id,
                ChatPermissions(
                    can_send_messages=True,
                    can_send_media_messages=True,
                    can_send_stickers=True,
                    can_send_animations=True,
                    can_send_games=True,
                    can_use_inline_bots=True,
                    can_send_polls=True,
                    can_change_info=True,
                    can_invite_users=True,
                    can_pin_messages=True,
                    can_add_web_page_previews=True,
                ),
            )
            await message.edit(
                "**🔓 Unlocked all permission from this Chat!**", del_in=5
            )
            await CHANNEL.log(
                f"#UNLOCK\n\nCHAT: `{message.chat.title}` (`{chat_id}`)\n"
                f"PERMISSIONS: `All Permissions`"
            )
        except Exception as e_f:
            await message.edit(
                r"`i don't have permission to do that ＞︿＜`\n\n" f"**ERROR:** `{e_f}`",
                del_in=5,
            )
        return
    if unlock_type in _types:
        (
            umsg,
            umedia,
            ustickers,
            uanimations,
            ugames,
            uinlinebots,
            uwebprev,
            upolls,
            uinfo,
            uinvite,
            upin,
            uperm,
        ) = _get_chat_lock(message, unlock_type, False)
    else:
        await message.err(r"Invalid Unlock Type! ¯\_(ツ)_/¯")
        return
    try:
        await message.client.set_chat_permissions(
            chat_id,
            ChatPermissions(
                can_send_messages=umsg,
                can_send_media_messages=umedia,
                can_send_stickers=ustickers,
                can_send_animations=uanimations,
                can_send_games=ugames,
                can_use_inline_bots=uinlinebots,
                can_add_web_page_previews=uwebprev,
                can_send_polls=upolls,
                can_change_info=uinfo,
                can_invite_users=uinvite,
                can_pin_messages=upin,
            ),
        )
        await message.edit(f"**🔓 Unlocked {uperm} for this chat!**", del_in=5)
        await CHANNEL.log(
            f"#UNLOCK\n\nCHAT: `{message.chat.title}` (`{chat_id}`)\n"
            f"PERMISSIONS: `{uperm} Permission`"
        )
    except Exception as e_f:
        await message.edit(
            r"`i don't have permission to do that ＞︿＜`\n\n" f"**ERROR:** `{e_f}`",
            del_in=5,
        )

@userge.on_cmd(
    "vperm",
    about={
        "header": "use this to view group permissions",
        "description": "Allows you to view permission types on/off status in the chat.",
    },
    allow_channels=False,
    allow_bots=False,
    allow_private=False,
)
async def view_perm(message: Message):
    """check chat permissions from tg group"""
    await message.edit("`Checking group permissions... Hang on!! ⏳`")

    def convert_to_emoji(val: bool):
        return "✅" if val else "❌"

    vmsg = convert_to_emoji(message.chat.permissions.can_send_messages)
    vmedia = convert_to_emoji(message.chat.permissions.can_send_media_messages)
    vstickers = convert_to_emoji(message.chat.permissions.can_send_stickers)
    vanimations = convert_to_emoji(message.chat.permissions.can_send_animations)
    vgames = convert_to_emoji(message.chat.permissions.can_send_games)
    vinlinebots = convert_to_emoji(message.chat.permissions.can_use_inline_bots)
    vwebprev = convert_to_emoji(message.chat.permissions.can_add_web_page_previews)
    vpolls = convert_to_emoji(message.chat.permissions.can_send_polls)
    vinfo = convert_to_emoji(message.chat.permissions.can_change_info)
    vinvite = convert_to_emoji(message.chat.permissions.can_invite_users)
    vpin = convert_to_emoji(message.chat.permissions.can_pin_messages)
    permission_view_str = ""
    permission_view_str += "CHAT PERMISSION INFO:\n\n"
    permission_view_str += f"📩 Send Messages: {vmsg}\n"
    permission_view_str += f"🎭 Send Media: {vmedia}\n"
    permission_view_str += f"🎴 Send Stickers: {vstickers}\n"
    permission_view_str += f"🎲 Send Animations: {vanimations}\n"
    permission_view_str += f"🎮 Can Play Games: {vgames}\n"
    permission_view_str += f"🤖 Can Use Inline Bots: {vinlinebots}\n"
    permission_view_str += f"🌐 Webpage Preview: {vwebprev}\n"
    permission_view_str += f"🗳 Send Polls: {vpolls}\n"
    permission_view_str += f"ℹ Change Info: {vinfo}\n"
    permission_view_str += f"👥 Invite Users: {vinvite}\n"
    permission_view_str += f"📌 Pin Messages: {vpin}\n"
    if message.chat.photo and vmedia == "✅":
        local_chat_photo = await message.client.download_media(
            message=message.chat.photo.big_file_id
        )
        await message.client.send_photo(
            chat_id=message.chat.id,
            photo=local_chat_photo,
            caption=permission_view_str,
            parse_mode="html",
        )
        os.remove(local_chat_photo)
        await message.delete()
        await CHANNEL.log("`vperm` command executed")
    else:
        await message.edit(permission_view_str)
        await CHANNEL.log("`vperm` command executed")


import nltk, os
from nltk.corpus import movie_reviews
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from random import randrange, sample
from gensim import utils
from gensim.corpora.dictionary import Dictionary
from gensim.corpora.mmcorpus import MmCorpus
from gensim.corpora.textcorpus import TextCorpus
from gensim.models.ldamodel import LdaModel

class Corpus:

  def __init__(self):
    path_to_nltk_data = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'nltk_data'
    )
    nltk.data.path.append(path_to_nltk_data)
    self.stopwords = self.stopWordDict()
    return

  ####################
  # Helpers
  ####################
  def combineStrings(self, listOfStrings):
    return "\n\n\n\n\n".join(listOfStrings)

  ####################
  # Word Dict
  ####################
  def wordDictHelper(self, path):
    ret = {}
    with open(path, 'r') as f:
      for line in f:
        word = line.rstrip()
        if word not in ret:
          ret[word] = True
    return ret

  def positiveWordDict(self):
    path = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'lib',
      'positive-words.txt'
    )
    return self.wordDictHelper(path)

  def negativeWordDict(self):
    path = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'lib',
      'negative-words.txt'
    )
    return self.wordDictHelper(path)

  def stopWordDict(self):
    # nltk english stop words
    ret = {}
    sws = stopwords.words('english')
    for sw in sws:
      ret[sw] = True
    return ret
    '''
    # stanford nlp small stop words
    path = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'lib',
      'stop-words-small.txt'
    )
    return self.wordDictHelper(path)
    '''

  ####################
  # Movie Reviews
  ####################

  """
  category is 'positive' or 'negative'
  """
  def getRandomMovieReview(self, category):
    ret = ''
    if category == 'positive' or category == 'negative':
      ret = self.movieReviews(category, 1)[0]
    elif category == 'combined':
      positiveList = self.movieReviews('positive', 10)
      negativeList = self.movieReviews('negative', 10)
      ret = self.combineStrings(positiveList) + "\n\n\n\n\n" + self.combineStrings(negativeList)
    return ret

  """
  category is 'positive' or 'negative'
  Returns a list of strings
  """
  def movieReviews(self, category, count):
    ret = []
    if category != 'positive' and category != 'negative':
      return ret
    fileids = []
    if category == 'positive':
      fileids = movie_reviews.fileids('pos')
    elif category == 'negative':
      fileids = movie_reviews.fileids('neg')
    sampleFileIds = sample(fileids, count)
    for sampleFileId in sampleFileIds:
      ret.append(movie_reviews.raw(sampleFileId))
    return ret

  def tokensFromText(self, text):
    ret = []
    sentences = sent_tokenize(text)
    for s in sentences:
      words = word_tokenize(s)
      for w in words:
        ret.append(w)
    return ret

  def saveGensim(self, topic):
    if topic is None:
      # generate all
      self.saveGensim('movie')
      self.saveGensim('celebrity')
      self.saveGensim('syria')
      self.saveGensim('ufo')
      return

    posDocs = []
    negDocs = []

    if topic == 'movie':
      topic = 'movie_reviews'
    elif topic == 'celebrity':
      topic = 'bieber'

    if topic == 'movie_reviews':
      count = 100
      posDocs = self.movieReviews('positive', count)
      negDocs = self.movieReviews('negative', count)
    else:
      posDocs = self.getArticlesHelper('positive', topic)
      negDocs = self.getArticlesHelper('negative', topic)

    listOfTokens = [] # dictionary
    docs = [] # corpus

    for posDoc in posDocs:
      processed = self.processDocForGensim(posDoc)
      tokens = self.tokensFromText(processed)
      listOfTokens.append(tokens)
      docs.append(processed)
    for negDoc in negDocs:
      processed = self.processDocForGensim(negDoc)
      tokens = self.tokensFromText(processed)
      listOfTokens.append(tokens)
      docs.append(processed)

    dictionaryFilename = 'gensim_dictionary.txt'
    corpusFilename = 'gensim_corpus.mm'

    # make destination files if they don't exist
    dictionaryPath = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'james_data',
      topic,
      dictionaryFilename
    )

    corpusPath = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'james_data',
      topic,
      corpusFilename
    )

    corpusTempPath = corpusPath + '.tmp'

    if os.path.exists(dictionaryPath):
      os.remove(dictionaryPath)

    if os.path.exists(corpusPath):
      os.remove(corpusPath)

    if os.path.exists(corpusTempPath):
      os.remove(corpusTempPath)

    with open(dictionaryPath, 'w') as f:
      f.write(' ')

    with open(corpusPath, 'w') as f:
      f.write(' ')

    # save dictionary and corpus
    d = Dictionary(listOfTokens)
    d.save(dictionaryPath)

    with open(corpusTempPath, 'w') as f:
      f.write('\n'.join(docs))

    corpus = TextCorpus(corpusTempPath)
    MmCorpus.save_corpus(corpusPath, corpus)

    return

  def isAlphanumeric(self, c):
    return ('0' <= c and c <= '9') or ('a' <= c and c <= 'z') or ('A' <= c and c <= 'Z')

  def processDocForGensim(self, doc):
    withoutNewlines = doc.replace('\n', ' ')
    lowered = withoutNewlines.lower()
    ret = ''
    # replace non-ascii letters with ' '
    for c in lowered:
      if (ord(c) < 128):
        ret += c
      else:
        ret += ' '
    tokens = self.tokensFromText(ret)
    filtered = []
    for token in tokens:
      # remove single letter tokens
      l = len(token)
      if l == 1:
        continue
      # remove non alphanumeric tokens
      isAlphanumeric = True
      for c in token:
        if not self.isAlphanumeric(c):
          isAlphanumeric = False
          break
      if not isAlphanumeric:
        continue
      # remove stop words
      if token in self.stopwords:
        continue
      filtered.append(token)

    return ' '.join(filtered)

  ######################
  # James Data Helpers
  ######################
  """
  category is 'positive' or 'negative'
  topic is one of ['bieber', 'syria', 'ufo']
  Returns a string
  """
  def getArticleHelper(self, category, topic):
    ret = ''
    if category == 'combined':
      dirPath = os.path.join(
        os.path.dirname(os.path.abspath(__file__)),
        'james_data',
        topic
      )
      combinedFilePath = os.path.join(dirPath, 'all')

      if os.path.exists(combinedFilePath):
        # use existing file
        ret = ''
        with open(combinedFilePath, 'r') as f:
          ret = f.read()
      else:
        positiveList = self.getArticlesHelper('positive', topic)
        negativeList = self.getArticlesHelper('negative', topic)
        ret = self.combineStrings(positiveList) + "\n\n\n\n\n" + self.combineStrings(negativeList)
        with open(combinedFilePath, 'w') as f:
          # save as 'all' in the topic directory
          f.write(ret)
    else:
      articles = self.getArticlesHelper(category, topic)
      l = len(articles)
      index = randrange(l)
      ret = articles[index]

    return ret

  """
  category is 'positive' or 'negative'
  Returns a list of strings
  """
  def getArticlesHelper(self, category, topic):
    ret = []
    dirPath = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'james_data',
      topic
    )

    if category == 'positive':
      dirPath = os.path.join(dirPath, 'pos')
    elif category == 'negative':
      dirPath = os.path.join(dirPath, 'neg')
    filenames = os.listdir(dirPath)
    for filename in filenames:
      path = os.path.join(dirPath, filename)
      with open(path, 'r') as f:
        fileString = f.read()
        ret.append(fileString)
    return ret

  ######################
  # James Data Getters
  ######################
  def celebrityArticle(self, category):
    return self.getArticleHelper(category, 'bieber')

  def celebrityArticles(self, category):
    return self.getArticlesHelper(category, 'bieber')

  def ufoArticle(self, category):
    return self.getArticleHelper(category, 'ufo')

  def ufoArticles(self, category):
    return self.getArticlesHelper(category, 'ufo')

  def syriaArticle(self, category):
    return self.getArticleHelper(category, 'syria')

  def syriaArticles(self, category):
    return self.getArticlesHelper(category, 'syria')

  ########################
  ## LDA using SNAP data
  ########################

  """
  Command order:
  from corpus import Corpus
  c = Corpus()
  c.SNAP_ldaTopicsForTopic('bieber')

  Once:
  c.SNAP_generateGensimDictionaryForSNAP()
  c.SNAP_generateMmCorpus('all')
  c.SNAP_generateLDAForTopic('all')
  """

  """
  1. (LDA) To run LDA, we need 1) id2word mapping (gensim Dictionary), 2) MmCorpus.
  2. (id2word) For all SNAP data, id2word mapping is shared. This can be generated by going through RepresentativeTokens, and just feeding in all the words to gensim.
  3. (corpus) Each document is a list of (word_id, word_weight) tuples.

  To summarize,
  1) (id2word) Generate a gensim dictionary by going through RepresentativeTokens.txt, and feeding in [[word1, word2, ...]]. Save it as gensim_snap_dict.txt
  2) (corpus) For each topic (e.g., bieber), generate a corpus of [[(word_id, tfidf score), ... ], ...], and save it as MmCorpus named gensim_snap_mmcorpus_bieber.mm
  3) (lda) For each topic (e.g., bieber), generate the lda result as,
  lda = gensim.models.ldamodel.LdaModel(corpus=mm, id2word=id2word, num_topics=10, update_every=1, chunksize=10000, passes=1)
  """

  """
  Creates gensim_snap_dict.txt in snap_data.
  TODO: handle document frequency. Currently all set to 1
  """
  def SNAP_generateGensimDictionaryForSNAP(self):
    inputFilePath = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'snap_data',
      'RepresentativeTokens.txt'
    )
    outputFilepath = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'snap_data',
      'gensim_snap_dict.txt'
    )
    # format: id[TAB]word_utf8[TAB]document frequency[NEWLINE].
    linesToWrite = []
    with open(inputFilePath, 'r') as f:
      for line in f:
        line = line.rstrip()
        tokens = line.split()
        wordID = tokens[0]
        word = tokens[1]
        linesToWrite.append("%s\t%s\t%d" % (wordID, word, 1)) # TODO handle doc freq better
    textToWrite = "\n".join(linesToWrite) + "\n"
    with open(outputFilepath, 'w') as f:
      f.write(textToWrite)
    return

  """
  topic is one of "bieber", "cyrus", "syria", "ufo"
  Generates gensim_snap_mmcorpus_<topic>.mm in snap_data
  """
  def SNAP_generateMmCorpus(self, topic):
    if topic == 'all':
      topics = ['bieber', 'cyrus', 'syria', 'ufo']
      for t in topics:
        self.SNAP_generateMmCorpus(t)
      return
    corpus = self.SNAP_corpusForTopic(topic)
    outputPath = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'snap_data',
      "gensim_snap_mmcorpus_%s.mm" % topic
    )
    id2word = self.SNAP_id2word()
    MmCorpus.save_corpus(outputPath, corpus, id2word)
    return

  """
  HELPER
  topic is one of "bieber", "cyrus", "syria", "ufo"
  Returns [
    [
      (word_id, tfidf), ...
    ],
    ...
  ]
  """
  def SNAP_corpusForTopic(self, topic):
    dirPath = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'snap_data',
      topic
    )
    filenames = os.listdir(dirPath)
    ret = []
    for filename in filenames:
      if len(filename.split('-')) == 2:
        filepath = os.path.join(
          os.path.dirname(os.path.abspath(__file__)),
          'snap_data',
          topic,
          filename
        )
        with open(filepath, 'r') as f:
          flagInDoc = False
          docData = []
          for line in f:
            line = line.rstrip()
            tokens = line.split()
            numTokens = len(tokens)

            flagNewDoc = (numTokens > 2)
            flagEndDoc = (numTokens == 0)

            if (flagInDoc and flagEndDoc):
              # finished reading the current doc
              ret.append(docData)
              docData = []
              flagInDoc = False
            elif (flagInDoc and (not flagEndDoc)):
              # need to read (word_id, tfidf)
              wordID = (int)(tokens[0])
              tfidf = (float)(tokens[1])
              docData.append((wordID, tfidf))
            elif ((not flagInDoc) and flagNewDoc):
              # just read the doc header
              flagInDoc = True
    return ret

  def SNAP_id2word(self):
    path = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'snap_data',
      'gensim_snap_dict.txt'
    )
    # self.myLoadFromText(path)
    ret = Dictionary.load_from_text(path)
    return ret

  """
  Saves the file to,
  "snap_data/gensim_snap_lda_<topic>_<numTopics>"
  """
  def SNAP_generateLDAForTopic(self, topic, numTopics = 5):
    if (topic == 'all'):
      topics = ['syria', 'ufo', 'movie', 'celebrity', 'russia'] # bieber, cyrus
      for t in topics:
        for nt in [5, 10]:
          self.SNAP_generateLDAForTopic(t, nt)
      return
    id2word = self.SNAP_id2word()
    mmPath = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'snap_data',
      "gensim_snap_mmcorpus_%s.mm" % topic
    )
    outPath = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'snap_data',
      "gensim_snap_lda_%s_%d" % (topic, numTopics)
    )
    mm = MmCorpus(mmPath)
    lda = LdaModel(corpus=mm, id2word=id2word, num_topics=numTopics, update_every=1, chunksize=10000, passes=1)
    lda.save(outPath)
    return

  """
  Returns a list of strings
  """
  def SNAP_ldaTopicsForTopic(self, topic, numTopics = 10):
    if numTopics not in [5, 10, 20, 30]:
      print("[ERROR] Invalid numTopics")
      return
    inPath = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'snap_data',
      "gensim_snap_lda_%s_%d" % (topic, numTopics)
    )
    lda = LdaModel.load(inPath)
    return lda.print_topics(numTopics)

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


import pymysql
import threading
b = 0
flag = 0
db = pymysql.connect("localhost","root","","databasename" )
cursor = db.cursor()
def thread_1():
    global flag
    global b
    while(True):
        if (flag == 0):
            a = input("nhap x:")
            if (a == "exit"):
                flag = 3
                break
            else:
                b = int(a)
                flag = 1;
        if (flag == 4):
            sql = """INSERT INTO ngay3(x)
                        VALUES ('%d')""" %(b)
            try:
                # Execute the SQL command
                cursor.execute(sql)
                # Commit your changes in the database
                db.commit()
            except:
                # Rollback in case there is any error
                db.rollback()
            print("da them x = " + a + " vao bang")
            flag = 0

def thread_2():
    global b
    global flag
    while (True):
        if (flag == 3):
            break
        if (flag == 1):
            sql = """ select x from ngay3
            where x = '%d' """ %(b)
            try:
                # Execute the SQL command
                cursor.execute(sql)
                # Fetch all the rows in a list of lists.
                results = cursor.fetchall()
                #x = None
                #for row in results:
                #    x = row[0]
                if (len(results) != 0):
                    print("da ton tai trong bang")
                else:
                    print("x chua ton tai trong bang")
            except:
                print ("Error: unable to fetch data")
            flag = 4

thread1 = threading.Thread(target=thread_1)
thread2 = threading.Thread(target=thread_2)
thread1.start()
thread2.start()


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.select import Select
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class assessment:
    element_assessment_classname="//a[normalize-space()='Assessments']"
    label_your_assessment_id="courseName"
    button_create_assessment_xpath="//i[@class='fas fa-laptop-code']"
    link_objective_xpath="//a[@class='dropdown-item'][1]"
    link_subjective_xpath="//a[normalize-space()='Subjective']"
    textbox_add_title_id="title-objective"
    textbox_description_box_xpath="(//div[@role='textbox'])[1]"
    dropdown_subject_xpath="(//select[@id='subject'])[1]"
    textbox_section_name_id="section_name1"
    textbox_section_name_id2 = "section_name2"
    dropdown_marks_per_question_xpath="(//select[@id='section_marks1'])[1]"
    dropdown_marks_per_question_xpath2="//div[@class='sections']//div[@class='section mt-4']//select[@id='section_marks2']"
    textbox_instruction_xpath="//div[@class='section']//div[@role='textbox']"
    textbox_instruction_xpath2="//div[@id='section2']//div[@role='textbox']"
    button_add_new_section_id="btnAdd"
    button_delete_above_section_id="btnDeleteSection"
    button_continue_xpath="//button[@id='createAssessmentBtn']"
    label_create_assessment_xpath="//h5[normalize-space()='Create Questions']"
    element_choose_question_id="chooseques"
    dynamic_units_list_xpath="//li[@class='getUnitsLi'][1]//input[@class='getUnitsInput']"  #change no
    dynamic_checkbox_list_xpath="//li[@class='chooseQuestionsLi pl-3 pr-2 py-2'][1]//input[@type='checkbox']" #change no
    element_topic_id="subject-modal-div"
    button_save_to_section_xpath="//button[normalize-space()='Save to Section']"
    dynamic_question_panel_xpath="//div[@class='addingQues p-1']//div[@class='row'][1]"  #change no
    button_save_and_exit_id="btnSaveExit"
    button_save_and_send_id="//button[@id='btnSaveSend']"
    textbox_start_time_xpath="//input[@id='starttime']"
    textbox_end_time_xpath="//input[@id='endtime']"
    toggle_auto_release_results_id="releaseSwitch"
    textbox_hours_classname="durationHours pl-1"
    textbox_minutes_classname="durationMinutes pl-1"
    toggle_nba_analysis_xpath="//input[@class='custom-control-input' and @id='nbaSwitch']"
    toggle_collect_feedback_xpath="//input[@class='custom-control-input' and @id='feedbackSwitch']"
    radio_class_select_xpath="//input[@class='sectionsListInput' and @data-department='CSE' and @data-id='708']"
    checkbox_rmkcs5_xpath="//input[@data-roll_number='RMKCS5']"
    checkbox_rmkcs04_xpath="//input[@data-roll_number='RMKCS04']"
    checkbox_rmkcs6_xpath="//input[@data-roll_number='RMKCS6']"
    button_send_id="sendQuestionsBtn"
    button_scheduled_xpath="//button[normalize-space()='Scheduled']"
    textbox_add_question_xpath = "//div[@class='col-sm-9']//div[@role='textbox']"
    input_add_image_id="quesImage"
    checkbox_topics_list_dynamic="//li[1]//input[@class='topicTagsInput']" #change no
    dropdown_blooms_id="//select[@id='selectBloomLevel']"
    dropdown_difficulty_id="//select[@id='selectLevel']"
    textbox_optionA_xpath="//div[@class='row pt-4']//div[@class='col-6 pl-0 pr-1']//div[@role='textbox']"
    textbox_optionB_xpath="//div[@class='col-6 pr-1 pl-0']//div[@role='textbox']"
    textbox_optionC_xpath="//div[@class='row no-gutters w-100']//div[@role='textbox']"
    textbox_optionD_xpath="//div[@class='col-6 pl-1 pr-0']//div[@class='row no-gutters']//div[@role='textbox']"
    checkbox_option_xpath="//i[@class='far fa-square fa-2x untick untick1']"
    element_add_source_id="sourceBtn"
    textbox_source_id="sourceInput"
    button_save_section_id= "saveSection"
    button_add_question_id="addques"
    button_topics_close_xpath="//div[@class='modal-dialog modal-lg']//button[@aria-label='Close']"

    def __init__(self,driver):
        self.driver=driver

    def clickAssessment(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.element_assessment_classname).click()

    def clickCreateNewAssessment(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.button_create_assessment_xpath).click()

    def clickObjectiveAssessment(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.link_objective_xpath).click()

    def clickSubjectiveAssessment(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.LINK_TEXT,self.link_subjective_xpath).click()

    def addTitle(self,title):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.ID,self.textbox_add_title_id).clear()
        self.driver.find_element(By.ID,self.textbox_add_title_id).send_keys(title)

    def addDescription(self,descrip):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.textbox_description_box_xpath).clear()
        self.driver.find_element(By.XPATH,self.textbox_description_box_xpath).send_keys(descrip)

    def selectSubject(self,sub):
        self.driver.implicitly_wait(10)
        subject=Select(self.driver.find_element(By.XPATH,self.dropdown_subject_xpath))
        subject.select_by_visible_text(sub)

    def sectionName(self,sec):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.ID,self.textbox_section_name_id).clear()
        self.driver.find_element(By.ID, self.textbox_section_name_id).send_keys(sec)

    def marksPerQuestion(self):
        self.driver.implicitly_wait(10)
        mar=Select(self.driver.find_element(By.XPATH,self.dropdown_marks_per_question_xpath))
        mar.select_by_value('2.0')

    def secInstruction(self,instruction):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.textbox_instruction_xpath).clear()
        self.driver.find_element(By.XPATH,self.textbox_instruction_xpath).send_keys(instruction)

    def addNewSection(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.ID,self.button_add_new_section_id).click()

    def sectionName02(self,sec):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.ID,self.textbox_section_name_id2).clear()
        self.driver.find_element(By.ID, self.textbox_section_name_id2).send_keys(sec)

    def marksPerQuestion02(self):
        self.driver.implicitly_wait(10)
        mar=Select(self.driver.find_element(By.XPATH,self.dropdown_marks_per_question_xpath2))
        mar.select_by_value('3')

    def secInstruction_02(self,instruction):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.textbox_instruction_xpath2).clear()
        self.driver.find_element(By.XPATH,self.textbox_instruction_xpath2).send_keys(instruction)

    def deleteSection(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.ID,self.button_delete_above_section_id).click()

    def scrollTillBodyTop(self):
        self.driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")

    def scrollElementViewByXpath(self,xpath):
        self.element = self.driver.find_element(By.XPATH,xpath)
        self.driver.execute_script("arguments[0].scrollIntoView();", self.element)

    def clickContinue(self):
        self.driver.implicitly_wait(10)
        self.driver.execute_script("window.scrollBy(0,-2000)")
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.button_continue_xpath).click()

    def clickAddQuestion(self):
        time.sleep(1)
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.ID,self.button_add_question_id).click()

    def addquestion(self,ques):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.textbox_add_question_xpath).clear()
        self.driver.find_element(By.XPATH, self.textbox_add_question_xpath).send_keys(ques)

    def selectTopics(self):
        self.driver.implicitly_wait(10)
        self.driver.execute_script("window.scrollBy(0,-1000)")
        self.driver.find_element(By.ID,self.element_topic_id).click()

    def inputOption(self,opta,optb,optc,optd):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.textbox_optionA_xpath).clear()
        self.driver.find_element(By.XPATH, self.textbox_optionA_xpath).send_keys(opta)
        self.driver.find_element(By.XPATH, self.textbox_optionB_xpath).clear()
        self.driver.implicitly_wait(5)
        self.driver.find_element(By.XPATH, self.textbox_optionB_xpath).send_keys(optb)
        self.driver.find_element(By.XPATH, self.textbox_optionC_xpath).clear()
        self.driver.find_element(By.XPATH, self.textbox_optionC_xpath).send_keys(optc)
        self.driver.find_element(By.XPATH, self.textbox_optionD_xpath).clear()
        self.driver.find_element(By.XPATH, self.textbox_optionD_xpath).send_keys(optd)

    def selectOption(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.checkbox_option_xpath).click()

    def selectBloom(self):
        time.sleep(2)
        self.driver.implicitly_wait(10)
        blm=Select(self.driver.find_element(By.XPATH,self.dropdown_blooms_id))
        blm.select_by_value('3')

    def selectDifficulty(self):
        self.driver.implicitly_wait(10)
        dif=Select(self.driver.find_element(By.XPATH,self.dropdown_difficulty_id))
        dif.select_by_value('2')

    def addSource(self,source):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.ID,self.element_add_source_id).click()
        self.driver.find_element(By.ID,self.textbox_source_id).clear()
        self.driver.find_element(By.ID,self.textbox_source_id).send_keys(source)

    def clickSaveToSection(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.button_save_to_section_xpath).click()

    def clickSaveAndSend(self):
        time.sleep(2)
        self.driver.execute_script("window.scrollBy(0,-3000)")
        time.sleep(2)
        #WebDriverWait(self.driver, 20).until(EC.invisibility_of_element_located((By.XPATH,self.button_save_and_send_id)))
        WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH,self.button_save_and_send_id))).click()
        #self.driver.implicitly_wait(10)
        #self.driver.find_element(By.XPATH,self.button_save_and_send_id).click()

    def clickChooseQuestion(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.ID,self.element_choose_question_id).click()

    def chooseTopics(self):
        self.driver.implicitly_wait(10)
        self.topicsCount=self.driver.find_elements(By.XPATH,'//label[@class="topicTagsLabel show1"]')
        for x in range(len(self.topicsCount)):
            print(x)
            self.topicsCount[x].click()

    def choosetopicClose(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.button_topics_close_xpath).click()

    def startTime(self,st):
        self.driver.implicitly_wait(10)
        print(st)
        time.sleep(2)
        self.driver.find_element(By.XPATH,self.textbox_start_time_xpath).clear()
        self.driver.find_element(By.XPATH,self.textbox_start_time_xpath).send_keys(st)

    def endTime(self,et):
        self.driver.implicitly_wait(10)
        print(et)
        time.sleep(2)
        self.driver.find_element(By.XPATH,self.textbox_end_time_xpath).clear()
        self.driver.find_element(By.XPATH,self.textbox_end_time_xpath).send_keys(et)

    def setDuration(self):
        self.driver.implicitly_wait(10)
        self.driver.find_elemement(By.CLASS_NAME,self.textbox_minutes_classname).send_keys('2')

    def classSelect(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.radio_class_select_xpath).click()

    def selectStudents(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.XPATH,self.checkbox_rmkcs5_xpath).click()
        self.driver.find_element(By.XPATH, self.checkbox_rmkcs04_xpath).click()
        self.driver.find_element(By.XPATH, self.checkbox_rmkcs6_xpath).click()

    def sendTest(self):
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.xpath,self.button_send_id).click()


# Converts the PCAP Files to CSV Files for next steps

import sys
import os
import argparse
import csv
sys.path.append(os.getcwd())

class PCAP2CSVConverter:
    pcapDir = None
    csvDir = None
    CMD = '''tshark -r {} -T fields -e frame.number -e frame.time -e ip.src -e ip.dst -e ip.proto -e tcp.srcport -e tcp.dstport -e udp.srcport -e udp.dstport -e frame.len -e tcp.flags.ack -e tcp.flags.syn -e tcp.flags.fin -e tcp.flags.reset -e tcp.flags.push -e tcp.flags.urg -e _ws.col.Protocol -e eth.src -e eth.dst -E separator=,  -E quote=d > {}.csv'''
    def convertToCSV(self, pcapWithPath):
        print(pcapWithPath)
        filename, ext = os.path.splitext(pcapWithPath)
        filename = filename.replace(self.pcapDir, '')
        directory, _ = os.path.split(filename)
        if (directory[0]=='/' or directory[0] == '\\'):
            directory = directory[1:]
        complete_dir = os.path.join(self.csvDir, directory)
        if not os.path.isdir(os.path.join(self.csvDir, directory)):
            os.makedirs(os.path.join(self.csvDir, directory))
        if filename[0] == '/' or filename[0] == '\\':
            filename = filename[1:]
        destPath = os.path.join(self.csvDir, filename)
        cmd = self.CMD.format(pcapWithPath, destPath)
        os.system(cmd)
    def __init__(self, pcap_dir, csv_dir):
        self.pcapDir = pcap_dir
        self.csvDir = csv_dir
        if not os.path.isdir(self.csvDir):
            os.makedirs(self.csvDir)
    def start(self):
        fileList = []
        for root, dirs, files in os.walk(self.pcapDir):
            for name in files:
                if os.path.splitext(name)[1] == '.pcap':
                    fileList.append(os.path.join(root, name))
        count = 0
        print('Total Files:', len(fileList))
        for pcap in fileList:
            self.convertToCSV(pcap)
            count += 1
            print('.', end='')

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-i", dest="input_dir", help="Input Directory or the PCAP Directory")
    parser.add_argument("-o", dest="output_dir", help="Output Directory or the CSV Directory")
    args = parser.parse_args()
    if args.input_dir is None or args.output_dir is None:
        parser.print_help()
        return
    if not os.path.isdir(args.input_dir):
        print(f"No such directory: {args.input_dir}", file=sys.stderr)
        return
    if not os.path.isdir(args.output_dir):
        os.makedirs(args.output_dir)

    p2c = PCAP2CSVConverter(args.input_dir, args.output_dir)
    p2c.start()

if __name__ == "__main__":
    main()


import numpy as np

class ReplayBuffer:
    def __init__(self, buffer_shapes, size_in_transitions, T, sampler_function):
        """
        Replay buffer.

        :param buffer_shapes dict: dictionary specifying the shapes of replay buffer;
        size (number of timesteps collected) and dimensionality (keys: 'o', 'g', 'u', 'ag')
        :param size_in_transitions int: maximum size of the buffer in terms of transitions
        :param T int: length of each episode in terms of timesteps (num transitions + 1)
        :param sampler_function function: function to sample batches of transitions from the replay buffer
        """

        # Compute size of buffer in terms of episodes
        self.size_episodes = size_in_transitions // T

        # Set sampler function
        self.sampler_function = sampler_function

        # Initialize buffers with appropiate shapes
        self.buffers = {key: np.empty([self.size_episodes, *shape])
                        for key, shape in buffer_shapes.items()}

        # Initialize current buffer size in terms of episodes to zero
        self.current_size = 0

    def sample(self, batch_size):
        """
        Samples a batch of transitions from replay buffer.

        :param batch_size int: number of transitions to sample into the batch

        :return: a batch of transitions sampled from replay buffer
        :rtype: dict
        """
        assert self.current_size > 0

        # Get buffer with currently stored episodes
        buffer = {key: self.buffers[key][:self.current_size, :, :] for key in self.buffers.keys()}

        # Store next observation and next achieved goal in the buffer
        buffer['o_'] = buffer['o'][:, 1:, :]
        buffer['ag_'] = buffer['ag'][:, 1:, :]

        return self.sampler_function(buffer, batch_size)

    def store_episode(self, episode_batch, n_envs):
        """
        Stores a batch of episodes into the replay buffer.

        :param episode_batch dict: dictionary containing a batch of episodes with the following keys: 'o', 'g', 'u', 'ag'
        :param n_envs int: number of environments used to collect the episode batch (equal to the number of single episodes in the batch)
        """

        batch_size = n_envs # batch size in episodes

        # Get indexes in the replay buffer to store the batch
        idxs = self._get_storage_idx(batch_size)

        # Store episode_batch in the replay buffer
        for key in self.buffers.keys():
            self.buffers[key][idxs] = episode_batch[key]

    def _get_storage_idx(self, inc): # code from openAI baselines, comments added
        """
        Get the indexes of the replay buffer where episodes will be stored.

        :param inc int: number of episodes to store (1 or number of environments in vectorized environment)

        :return: indexes in the replay buffer to store episodes
        :rtype: numpy.ndarray
        """

        if self.current_size + inc <= self.size_episodes:
            # If the current size plus the number of episodes to store is less than or equal
            # to the total buffer size, store the episodes consecutively
            idx = np.arange(self.current_size, self.current_size+inc)
        elif self.current_size < self.size_episodes:
            # If the current size is less than the total buffer size, but the number of episodes to store
            # plus the current size is greater than the buffer size,
            # store the first n episodes consecutively and the rest randomly
            overflow = inc - (self.size_episodes - self.current_size)
            idx_a = np.arange(self.current_size, self.size_episodes)
            idx_b = np.random.randint(0, self.current_size, overflow)
            idx = np.concatenate([idx_a, idx_b])
        else:
            # If the current size equals the total buffer size, store the episodes randomly
            idx = np.random.randint(0, self.size_episodes, inc)

        # Update the current size of the replay buffer
        self.current_size = min(self.size_episodes, self.current_size+inc)

        return idx


from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = [], excludes = [])

executables = [
    Executable('main.py', 'Win32GUI')
]

setup(name='UniViewSigGrpMakerQt',
      version = '0.1',
      description = 'Qt application for making Signal groups for Uniview',
      options = dict(build_exe = buildOptions),
      executables = executables)


from argparse import ArgumentParser
from pathlib import Path
from os import listdir
from os.path import isfile, join
from difflib import unified_diff
from dataclasses import dataclass

import yaml
import subprocess
import termcolor

OK_TAG = f"[{termcolor.colored('OK', 'green')}]"
KO_TAG = f"[{termcolor.colored('KO', 'red')}]"

@dataclass
class TestCase:
    file: str
    stdout: str
    stderr: str
    exit_code: int

def diff(expected : str, actual : str) -> str:
    expected_lines = expected.splitlines(keepends=True)
    actual_lines = actual.splitlines(keepends=True)
    return ''.join(unified_diff(expected_lines, actual_lines, fromfile="expected", tofile="actual"))

def run_shell(shell : str, test : str) -> subprocess.CompletedProcess:
    return subprocess.run(["env", "-i", shell, test], capture_output=True, text=True)

def performc_checks(test : TestCase, actual : subprocess.CompletedProcess):
    assert (not test.stderr) or actual.stderr != "", \
        "something was expected on stderr"

    assert test.stdout == actual.stdout, \
        f"sdout differs \n{diff(test.stdout, actual.stdout)}"

    assert test.exit_code == actual.returncode, \
        f"Exited with {actual.returncode} expected {test.exit_code}"

if __name__ == "__main__":
    parser = ArgumentParser("TestSuite")
    parser.add_argument("--binary", required=True, type=Path)
    args = parser.parse_args()

    binary_path = "build/42sh"

    with open("./tests/data.yml", "r") as file:
        testsuite = [TestCase(**test) for test in yaml.safe_load(file)]

    for testcase in testsuite:
        file = testcase.file

        sh_proc = run_shell(binary_path, file)

        try:
            performc_checks(testcase, sh_proc)
        except AssertionError as err:
            print(f"{KO_TAG} {file}\n{err}")
        else:
            print(f"{OK_TAG} {file}")
            pass

        print("\t===================================\t")


from matplotlib import pyplot as plt
import numpy as np
import csv
from collections import Counter
import ast
import pandas as pd

with open('twitterData_public_tweets.csv') as file:
    csv_reader =  csv.DictReader(file)

    user_id_counter = Counter()
    followers = {}
    count = 0
    highest_follower = ''

    for row in csv_reader:
        user_id_counter.update({row['user_id']})
        followers[row['user_id']] = [row['user_follower_count'], row['user_verified']]
        if int(row['user_follower_count']) > count:
            highest_follower = row['user_id']
            count = int(row['user_follower_count'])

user = []
tally = []

follower_num = []
user = []
verified = Counter()

i = 1

data = []
data.append(["Most followed user",count])
for tweet in user_id_counter.most_common(10):
    temp = []
    temp.append("user {}".format(i))
    print(tweet[0])
    temp.append(int(followers[tweet[0]][0]))
    data.append(temp)

    follower_num.append(float(followers[tweet[0]][0]))
    user.append(tweet[0])
    verified.update({followers[tweet[0]][1]})
    i+=1

verified_name = []
verified_tally = []

for tweet in verified.most_common(3):
    verified_name.append(tweet[0])
    verified_tally.append(tweet[1])

fig, ax = plt.subplots(1,1)
column_labels=["user", "follower count"]
ax.axis('tight')
ax.axis('off')
the_table = ax.table(cellText=data,colLabels=column_labels,colColours =["yellow"] * 2,loc="center")
the_table[(1, 0)].set_facecolor("#1ac3f5")
the_table[(1, 1)].set_facecolor("#1ac3f5")
plt.show()

plt.bar(user, tally)
plt.xlabel('Top users')
plt.ylabel('number of tweets')
plt.title('Who sent the most tweets out?')
plt.show()

plt.pie(verified_tally, labels = verified_name, shadow=True)
plt.title('How many users are verified?')
plt.show()