import numpy as np
import matplotlib.pyplot as plt

x1 = np.array([0.5, 0.1, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.35, 0.25])[:,None]
x2 = np.array([0.9, 0.8, 0.75, 1.0])[:,None]
N = len(x1) + len(x2)
prior1 = len(x1) / N
prior2 = len(x2) / N

def gaussian(mean, var, xx):
    return np.exp(-0.5 * (xx - mean)**2 / var) * 1/np.sqrt(2*np.pi*var)

def plot_tutorial_3a():
    plt.clf()

    """Code for plotting the fitted Gaussians"""
    # X_grid = np.arange(-.1, 1.2, 0.01)[:,None]
    # gaussian_1 = gaussian(x1.mean(), x1.var(), X_grid)
    # gaussian_2 = gaussian(x2.mean(), x2.var(), X_grid)
    # plt.plot(X_grid, gaussian_1, label='Gaussian 1')
    # plt.plot(X_grid, gaussian_2, label='Gaussian 2')

    """ Code to plot the scores p(x,y) = P(y)p(x|y) of the given datapoints """
    xx = np.concatenate((x1,x2))
    p_x_given_class_1 = prior1 * gaussian(x1.mean(), x1.var(), xx)
    plt.scatter(xx, p_x_given_class_1, label='score for data given class 1')
    p_x_given_class_2 = prior2 * gaussian(x2.mean(), x2.var(), xx)
    plt.scatter(xx, p_x_given_class_2, label='score for data given class 2')

    """ Code to plot the scores p(x,y) = P(y)p(x|y) as a function of x"""
    xx = np.linspace(0, 1, num=100)
    p_x_given_class_1 = prior1 * gaussian(x1.mean(), x1.var(), xx)
    plt.plot(xx, p_x_given_class_1, label='score for class 1')
    p_x_given_class_2 = prior2 * gaussian(x2.mean(), x2.var(), xx)
    plt.plot(xx, p_x_given_class_2, label='score for class 2')

    # change labels and add legend
    plt.xlabel('location x')
    plt.ylabel('p(x,y)')
    plt.legend()

    # Show the plot
    plt.show()

def plot_gaussians_only():
    """Code for plotting only the Gaussians"""
    X_grid = np.arange(-.1, 1.2, 0.01)[:,None]
    gaussian_1 = gaussian(x1.mean(), x1.var(), X_grid)
    gaussian_2 = gaussian(x2.mean(), x2.var(), X_grid)

    plt.plot(X_grid, gaussian_1, label='Gaussian 1')
    plt.scatter(x1, np.zeros_like(x1), label='points x1')
    plt.plot(X_grid, gaussian_2, label='Gaussian 2')
    plt.scatter(x2, np.zeros_like(x2), label='points x2')
    plt.legend()
    plt.show()

def plot_probabilities_only(x):
    """Code for plotting only the scores p(x|y)"""
    p_x_given_class_1 = prior1 * gaussian(x1.mean(), x1.var(), x)
    plt.scatter(x, p_x_given_class_1, label='class 1')
    p_x_given_class_2 = prior2 * gaussian(x2.mean(), x2.var(), x)
    plt.scatter(x, p_x_given_class_2, label='class 2')
    plt.legend()
    plt.show()

def probability_of_class_1_given_x(x: float) -> float:
    # P(x=0.6) = P(x=0.6 | y=1)P(y=1) + P(x=0.6 | y=2)P(y=2)
    probability_of_x = gaussian(x1.mean(), x1.var(), x) * prior1 + gaussian(x2.mean(), x2.var(), x) * prior2

    # P(y=1 | x=0.6) = P(x=0.6 | y=1)P(y=1) / P(x=0.6)
    probability_asked = gaussian(x1.mean(), x1.var(), x) * prior1 / probability_of_x
    print(f'P(y=1|x={x}) = {probability_asked}')

probability_of_class_1_given_x(0.6)
plot_tutorial_3a()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Ringtail multiprocessing manager
#

import platform
from time import sleep
import logging
import queue
import fnmatch
import os
import glob
from .storagemanager import StorageManager, StorageManagerSQLite
from .mpreaderwriter import DockingFileReader
from .mpreaderwriter import Writer
from .exceptions import MultiprocessingError
import traceback
from datetime import datetime

os_string = platform.system()
if os_string == "Darwin":  # mac
    import multiprocess as multiprocessing
else:
    import multiprocessing

class MPManager:
    def __init__(
        self,
        storageman,
        storageman_class=StorageManagerSQLite,
        mode="dlg",
        chunk_size=1,
        max_poses=3,
        interaction_tolerance=None,
        store_all_poses=False,
        target=None,
        add_interactions=False,
        interaction_cutoffs=[3.7, 4.0],
        receptor_file=None,
        file_sources={
            "file": [[]],
            "file_path": {"path": [[]], "pattern": "*.dlg*", "recursive": None},
            "file_list": [[]],
        },
        file_pattern="*.dlg*",
        max_proc=None,
    ):

        # confirm that requested parser mode is implemented
        self.implemented_modes = ["dlg", "vina"]
        if mode not in self.implemented_modes:
            raise NotImplementedError(
                f"Requested file parsing mode {mode} not yet implemented"
            )
        self.mode = mode
        self.chunk_size = chunk_size
        self.max_poses = max_poses
        self.store_all_poses = store_all_poses
        self.interaction_tolerance = interaction_tolerance
        self.target = target
        self.add_interactions = add_interactions
        self.interaction_cutoffs = interaction_cutoffs
        self.receptor_file = receptor_file
        self.file_sources = file_sources
        self.file_pattern = file_pattern

        self.storageman = storageman
        self.storageman_class = storageman_class
        self.num_files = 0
        self.max_proc = max_proc

    def process_files(self):
        if self.max_proc is None:
            self.max_proc = multiprocessing.cpu_count()
        self.num_readers = self.max_proc - 1
        self.queueIn = multiprocessing.Queue(maxsize=2 * self.max_proc)
        self.queueOut = multiprocessing.Queue(maxsize=2 * self.max_proc)
        # start the workers in background
        self.workers = []
        self.p_conn, self.c_conn = multiprocessing.Pipe(True)
        logging.info("Starting {0} file readers".format(self.num_readers))
        for i in range(self.num_readers):
            # one worker is started for each processor to be used
            s = DockingFileReader(
                self.queueIn,
                self.queueOut,
                self.c_conn,
                self.storageman,
                self.storageman_class,
                self.mode,
                self.max_poses,
                self.interaction_tolerance,
                self.store_all_poses,
                self.target,
                self.add_interactions,
                self.interaction_cutoffs,
                self.receptor_file,
            )
            # this method calls .run() internally
            s.start()
            self.workers.append(s)

        # start the writer to process the data from the workers
        w = Writer(
            self.queueOut,
            self.num_readers,
            self.c_conn,
            self.chunk_size,
            self.storageman,
            self.mode,
        )

        w.start()
        self.workers.append(w)

        # process items in the queue
        try:
            self._process_sources()
        except Exception as e:
            tb = traceback.format_exc()
            self._kill_all_workers(e, "file sources", tb)
        # put as many poison pills in the queue as there are workers
        for i in range(self.num_readers):
            self.queueIn.put(None)

        # check for exceptions
        while w.is_alive():
            sleep(0.5)
            self._check_for_worker_exceptions()

        w.join()

        logging.info("Wrote {0} files to database".format(self.num_files))

    def _process_sources(self):
        # add individual file(s)
        if self.file_sources["file"] != [[]]:
            for file_list in self.file_sources["file"]:
                for file in file_list:
                    if (
                        fnmatch.fnmatch(file, self.file_pattern)
                        and file != self.receptor_file
                    ):
                        self._add_to_queue(file)
        # add files from file path(s)
        if self.file_sources["file_path"]["path"] != [[]]:
            for path_list in self.file_sources["file_path"]["path"]:
                for path in path_list:
                    # scan for ligand dlgs
                    for files in self._scan_dir(
                        path, self.file_pattern, recursive=True
                    ):
                        for f in files:
                            self._add_to_queue(f)
        # add files from file list(s)
        if self.file_sources["file_list"] != [[]]:
            for filelist_list in self.file_sources["file_list"]:
                for filelist in filelist_list:
                    self._scan_file_list(filelist, self.file_pattern.replace("*", ""))

    def _add_to_queue(self, file):
        max_attempts = 750
        timeout = 0.5  # seconds
        if self.receptor_file is not None:
            if (
                os.path.split(file)[-1] == os.path.split(self.receptor_file)[-1]
            ):  # check that we don't try to add the receptor
                return
        attempts = 0
        while True:
            if attempts >= max_attempts:
                raise MultiprocessingError(
                    "Something is blocking the progressing of file reading. Exiting program."
                ) from queue.Full
            try:
                self.queueIn.put(file, block=True, timeout=timeout)
                self.num_files += 1
                self._check_for_worker_exceptions()
                break
            except queue.Full:
                # logging.debug(f"Queue full: queueIn.put attempt {attempts} timed out. {max_attempts - attempts} put attempts remaining.")
                attempts += 1
                self._check_for_worker_exceptions()

    def _check_for_worker_exceptions(self):
        if self.p_conn.poll():
            error, tb, filename = self.p_conn.recv()
            logging.error(f"Caught error in multiprocessing from {filename}")
            # don't kill parser errors, only database error
            if filename == "Database":
                self._kill_all_workers(error, filename, tb)
            else:
                with open("ringtail_failed_files.log", 'a') as f:
                    f.write(str(datetime.now()) + f"\tRingtail failed to parse {filename}\n")
                    logging.debug(tb)

    def _kill_all_workers(self, error, filename, tb):
        for s in self.workers:
            s.kill()
        logging.debug(f"Error encountered while handling {filename}")
        logging.debug(tb)
        raise error

    def _scan_dir(self, path, pattern, recursive=False):
        """scan for valid output files in a directory
        the pattern is used to glob files
        optionally, a recursive search is performed
        """
        logging.info(
            "-Scanning directory [%s] for files (pattern:|%s|)" % (path, pattern)
        )
        if recursive:
            path = os.path.normpath(path)
            path = os.path.expanduser(path)
            for dirpath, dirnames, filenames in os.walk(path):
                yield (  # <----
                    os.path.join(dirpath, f)
                    for f in fnmatch.filter(filenames, "*" + pattern)
                )
        else:
            yield glob(os.path.join(path, pattern))  # <----

    def _scan_file_list(self, filename, pattern=".dlg"):
        """read file names from file list"""
        lig_accepted = []
        c = 0
        with open(filename, "r") as fp:
            for line in fp.readlines():
                line = line.strip()
                c += 1
                if os.path.isfile(line):
                    if line.endswith(pattern) or line.endswith(pattern + ".gz"):
                        lig_accepted.append(line)
                else:
                    logging.warning("Warning! file |%s| does not exist" % line)
        if len(lig_accepted) == 0:
            raise MultiprocessingError(
                "*ERROR* No valid files were found when reading from |%s|" % filename
            )
        logging.info(
            "# [ %5.3f%% files in list accepted (%d) ]"
            % ((len(lig_accepted) / c * 100, c))
        )

        for file in lig_accepted:
            if file != self.receptor_file:
                self._add_to_queue(file)


# -*- coding: utf-8 -*-
"""
                         +----------------+
                         | BasePackageRef |
                         +-------+--------+
                                 |
              +------------+     |     +-----------------+
              | PackageRef <-----+-----> IndexJsonRecord |
              +------+-----+           +-------+---------+
                     |                         |
                     +-----------+-------------+
                                 |
                         +-------v-------+
                         | PackageRecord |
                         +--+---------+--+
+--------------------+      |         |      +--------------+
| PackageCacheRecord <------+         +------> PrefixRecord |
+--------------------+                       +--------------+

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

from .channel import Channel
from .enums import FileMode, LinkType, NoarchType, PackageType, PathType, Platform
from .._vendor.auxlib.entity import (BooleanField, ComposableField, DictSafeMixin, Entity,
                                     EnumField, IntegerField, ListField, NumberField,
                                     StringField)
from ..base.context import context
from ..common.compat import isiterable, itervalues, string_types, text_type

class LinkTypeField(EnumField):
    def box(self, instance, instance_type, val):
        if isinstance(val, string_types):
            val = val.replace('-', '').replace('_', '').lower()
            if val == 'hard':
                val = LinkType.hardlink
            elif val == 'soft':
                val = LinkType.softlink
        return super(LinkTypeField, self).box(instance, instance_type, val)

class NoarchField(EnumField):
    def box(self, instance, instance_type, val):
        return super(NoarchField, self).box(instance, instance_type, NoarchType.coerce(val))

class TimestampField(NumberField):

    # @staticmethod
    # def _make_seconds(val):
    #     if val:
    #         val = int(val)
    #         if val > 253402300799:  # 9999-12-31
    #             val //= 1000  # convert milliseconds to seconds; see conda/conda-build#1988
    #     return val

    @staticmethod
    def _make_milliseconds(val):
        if val:
            if val < 253402300799:  # 9999-12-31
                val *= 1000  # convert seconds to milliseconds
            val = int(val)
        return val

    def box(self, instance, instance_type, val):
        return self._make_milliseconds(
            super(TimestampField, self).box(instance, instance_type, val)
        )

    def unbox(self, instance, instance_type, val):
        return self._make_milliseconds(
            super(TimestampField, self).unbox(instance, instance_type, val)
        )

    def dump(self, instance, instance_type, val):
        return self._make_milliseconds(
            super(TimestampField, self).dump(instance, instance_type, val)
        )

class Link(DictSafeMixin, Entity):
    source = StringField()
    type = LinkTypeField(LinkType, required=False)

EMPTY_LINK = Link(source='')

class _FeaturesField(ListField):

    def __init__(self, **kwargs):
        super(_FeaturesField, self).__init__(string_types, **kwargs)

    def box(self, instance, instance_type, val):
        if isinstance(val, string_types):
            val = val.replace(' ', ',').split(',')
        return super(_FeaturesField, self).box(instance, instance_type, val)

    def dump(self, instance, instance_type, val):
        if isiterable(val):
            return ' '.join(val)
        else:
            return val or ()  # default value is (), and default_in_dump=False

class ChannelField(ComposableField):

    def __init__(self, aliases=()):
        self._type = Channel
        super(ComposableField, self).__init__(required=False, aliases=aliases)

    def dump(self, instance, instance_type, val):
        if val:
            return text_type(val)
        else:
            val = instance.channel  # call __get__
            return text_type(val)

    def __get__(self, instance, instance_type):
        try:
            return super(ChannelField, self).__get__(instance, instance_type)
        except AttributeError:
            url = instance.url
            return self.unbox(instance, instance_type, Channel(url))

class SubdirField(StringField):

    def __init__(self):
        super(SubdirField, self).__init__(required=False)

    def __get__(self, instance, instance_type):
        try:
            return super(SubdirField, self).__get__(instance, instance_type)
        except AttributeError:
            try:
                url = instance.url
            except AttributeError:
                url = None
            if url:
                return self.unbox(instance, instance_type, Channel(url).subdir)

            try:
                platform, arch = instance.platform.name, instance.arch
            except AttributeError:
                platform, arch = None, None
            if platform and not arch:
                return self.unbox(instance, instance_type, 'noarch')
            elif platform:
                if 'x86' in arch:
                    arch = '64' if '64' in arch else '32'
                return self.unbox(instance, instance_type, '%s-%s' % (platform, arch))
            else:
                return self.unbox(instance, instance_type, context.subdir)

class FilenameField(StringField):

    def __init__(self, aliases=()):
        super(FilenameField, self).__init__(required=False, aliases=aliases)

    def __get__(self, instance, instance_type):
        try:
            return super(FilenameField, self).__get__(instance, instance_type)
        except AttributeError:
            try:
                url = instance.url
                fn = Channel(url).package_filename
                if not fn:
                    raise AttributeError()
            except AttributeError:
                fn = '%s-%s-%s' % (instance.name, instance.version, instance.build)
            assert fn
            return self.unbox(instance, instance_type, fn)

class PathData(Entity):
    _path = StringField()
    prefix_placeholder = StringField(required=False, nullable=True, default=None,
                                     default_in_dump=False)
    file_mode = EnumField(FileMode, required=False, nullable=True)
    no_link = BooleanField(required=False, nullable=True, default=None, default_in_dump=False)
    path_type = EnumField(PathType)

    @property
    def path(self):
        # because I don't have aliases as an option for entity fields yet
        return self._path

class PathDataV1(PathData):
    # TODO: sha256 and size_in_bytes should be required for all PathType.hardlink, but not for softlink and directory  # NOQA
    sha256 = StringField(required=False, nullable=True)
    size_in_bytes = IntegerField(required=False, nullable=True)
    inode_paths = ListField(string_types, required=False, nullable=True)

    sha256_in_prefix = StringField(required=False, nullable=True)

class PathsData(Entity):
    # from info/paths.json
    paths_version = IntegerField()
    paths = ListField(PathData)

class BasePackageRef(DictSafeMixin, Entity):
    name = StringField()
    version = StringField()
    build = StringField(aliases=('build_string',))
    build_number = IntegerField()

class PackageRef(BasePackageRef):
    # the canonical code abbreviation for PackageRef is `pref`
    # fields required to uniquely identifying a package

    channel = ChannelField(aliases=('schannel',))
    subdir = SubdirField()
    fn = FilenameField(aliases=('filename',))

    md5 = StringField(default=None, required=False, nullable=True, default_in_dump=False)
    url = StringField(default=None, required=False, nullable=True, default_in_dump=False)

    @property
    def schannel(self):
        return self.channel.canonical_name

    @property
    def _pkey(self):
        return self.channel.canonical_name, self.subdir, self.name, self.version, self.build

    def __hash__(self):
        return hash(self._pkey)

    def __eq__(self, other):
        return self._pkey == other._pkey

    def dist_str(self):
        return "%s::%s-%s-%s" % (self.channel.canonical_name, self.name, self.version, self.build)

class IndexJsonRecord(BasePackageRef):

    arch = StringField(required=False, nullable=True)  # so legacy
    platform = EnumField(Platform, required=False, nullable=True)  # so legacy

    depends = ListField(string_types, default=())
    constrains = ListField(string_types, default=())

    track_features = _FeaturesField(required=False, default=(), default_in_dump=False)
    features = _FeaturesField(required=False, default=(), default_in_dump=False)

    subdir = SubdirField()
    # package_type = EnumField(NoarchType, required=False)  # previously noarch
    noarch = NoarchField(NoarchType, required=False, nullable=True, default=None,
                         default_in_dump=False)  # TODO: rename to package_type
    preferred_env = StringField(required=False, nullable=True, default=None, default_in_dump=False)

    license = StringField(required=False)
    license_family = StringField(required=False)

    timestamp = TimestampField(required=False)

    @property
    def combined_depends(self):
        from .match_spec import MatchSpec
        result = {ms.name: ms for ms in (MatchSpec(spec) for spec in self.depends or ())}
        result.update({ms.name: ms for ms in (MatchSpec(spec, optional=True)
                                              for spec in self.constrains or ())})
        return tuple(itervalues(result))

class PackageRecord(IndexJsonRecord, PackageRef):
    # the canonical code abbreviation for PackageRecord is `prec`, not to be confused with
    # PackageCacheRecord (`pcrec`) or PrefixRecord (`prefix_rec`)
    #
    # important for "choosing" a package (i.e. the solver), listing packages
    # (like search), and for verifying downloads
    #
    # this is the highest level of the record inheritance model that MatchSpec is designed to
    # work with

    date = StringField(required=False)
    size = IntegerField(required=False)

    package_type = EnumField(PackageType, required=False, nullable=True)

    def __str__(self):
        return "%s/%s::%s==%s=%s" % (self.channel.canonical_name, self.subdir, self.name,
                                     self.version, self.build)

IndexRecord = PackageRecord


from typing import (
    NamedTuple,
    Tuple,
)

from wasm.datatypes import (
    ValType,
)

class ControlFrame(NamedTuple):
    """
    Represents the equivalent of a label during expression validation.
    """
    label_types: Tuple[ValType, ...]
    end_types: Tuple[ValType, ...]
    height: int
    is_unreachable: bool

    def mark_unreachable(self) -> 'ControlFrame':
        """
        Recurn a copy of `self` with the `is_unreachable` flag set.
        """
        return type(self)(
            self.label_types,
            self.end_types,
            self.height,
            True,
        )


import copy
import numpy as np
from sklearn.utils import shuffle
from tensorflow import keras

from shapley.loader import Loader

class MNIST(Loader):
    def __init__(self, num_train, one_hot=True, shuffle=True, by_label=False):
        self.name = 'mnist'
        self.num_train = num_train
        self.num_test = num_train // 10
        self.x_train, self.y_train, self.x_test, self.y_test = self.load_data(one_hot, by_label)
        if shuffle: self.shuffle_data()
        self.x_train = self.x_train[:self.num_train]
        self.y_train = self.y_train[:self.num_train]
        self.x_test = self.x_test[:self.num_test]
        self.y_test = self.y_test[:self.num_test]

    def load_data(self, one_hot, by_label):
        mnist = keras.datasets.mnist
        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        x_train = np.reshape(x_train, [-1, 28 * 28])
        x_train = x_train.astype(np.float32) / 255
        x_test = np.reshape(x_test, [-1, 28 * 28])
        x_test = x_test.astype(np.float32) / 255

        if by_label:
            ind_train = np.argsort(y_train)
            ind_test = np.argsort(y_test)
            x_train, y_train = x_train[ind_train], y_train[ind_train]
            x_test, y_test = x_test[ind_test], y_test[ind_test]

        if one_hot:
            # convert to one-hot labels
            y_train = keras.utils.to_categorical(y_train)
            y_test = keras.utils.to_categorical(y_test)

        return x_train, y_train, x_test, y_test

    def shuffle_data(self):
        ind = np.random.permutation(len(self.x_train))
        self.x_train, self.y_train = self.x_train[ind], self.y_train[ind]
        ind = np.random.permutation(len(self.x_test))
        self.x_test, self.y_test = self.x_test[ind], self.y_test[ind]

    def prepare_data(self):
        return self.x_train, self.y_train, self.x_test, self.y_test


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

"""SNMP Experiments."""

import asyncio
import warnings

from pysnmp import hlapi
from pysnmp.entity import engine, config
from pysnmp.carrier.asyncio.dgram import udp
from pysnmp.entity.rfc3413 import ntfrcv
from pysnmp.smi import builder, compiler, view, rfc1902

DEFAULT_ADDRESSS = "0.0.0.0"
DEFAULT_PORT = 162
DEFAULT_COMMUNITY = "public"
DEFAULT_MIBS = ("SNMPv2-MIB", "IF-MIB", "XYTRONIX-MIB")  # Must be a tuple not a list.

_view_controller = None

def _make_object(*id_parts):
    """Construct a pySNMP ObjectType from the given identity."""
    return hlapi.ObjectType(hlapi.ObjectIdentity(*id_parts))

def _make_get(address, community, *objects, port=161, mp_model=1):
    """Construct a pySNMP get command.

    Defaults to SNMPv2c and port 161.

    """
    engine = hlapi.SnmpEngine()
    community = hlapi.CommunityData(community, mpModel=mp_model)
    target = hlapi.UdpTransportTarget((address, port))
    context = hlapi.ContextData()
    return hlapi.getCmd(engine, community, target, context, *objects)

def _run_command(command):
    """Runs the command and returns a list of all results."""
    # Implicitly calls next() repeatdly on the command iterator until there is nothing left.
    # Supports both single GETs as well as GETBULKs.
    return [result for result in command]

def _extract_errors(results):
    """Returns a list of error tuples if any errors exist in the results.

    Each error tuple consists of (error_indication, error_status, error_index).

    """
    errors = []
    for error_indication, error_status, error_index, var_binds in results:
        if error_indication or error_status:
            error_text = error_status.prettyPrint() if error_status else error_status
            object_id = var_binds[int(error_index) - 1][0] if error_index else "?"
            errors.append((error_indication, error_text, object_id))
    return errors

def _extract_values(results):
    """Returns a dict of data values from the results."""
    values = {}
    for result in results:
        var_binds = result[-1]
        for var_bind in var_binds:
            values[var_bind[0].prettyPrint()] = var_bind[1]
    return values

def _print_errors(errors):
    """Prints errors to the screen."""
    for error_indication, error_text, object_id in errors:
        if error_indication:
            print(error_indication)
        elif error_text:
            print(f"{error_text} at {object_id}")

def _print_values(values):
    """Prints object ids and their data values to the screen."""
    for object_id, value in values.items():
        print(object_id, "=", value)

def _print_results(results):
    """Prints the results of snmp commands and/or any related errors."""
    _print_errors(_extract_errors(results))
    _print_values(_extract_values(results))

def _old_print_results(command):
    """Prints the results of snmp commands and/or any related errors. (Deprecated)"""
    warnings.warn("Deprecated.", DeprecationWarning)
    errorIndication, errorStatus, errorIndex, varBinds = next(command)
    if errorIndication:
        print(errorIndication)
    elif errorStatus:
        print(
            "%s at %s"
            % (
                errorStatus.prettyPrint(),
                errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
            )
        )
    else:
        for varBind in varBinds:
            print(" = ".join([x.prettyPrint() for x in varBind]))

def quickstart():
    """PySNMP Quick Start Example"""
    iterator = hlapi.getCmd(
        hlapi.SnmpEngine(),
        hlapi.CommunityData("public", mpModel=0),
        hlapi.UdpTransportTarget(("192.168.0.59", 161)),
        hlapi.ContextData(),
        hlapi.ObjectType(hlapi.ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
    )
    errorIndication, errorStatus, errorIndex, varBinds = next(iterator)
    if errorIndication:
        print(errorIndication)
    elif errorStatus:
        print(
            "%s at %s"
            % (
                errorStatus.prettyPrint(),
                errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
            )
        )
    else:
        for varBind in varBinds:
            print(" = ".join([x.prettyPrint() for x in varBind]))

def common():
    """PySNMP Tutorial Common Operations Example"""
    engine = hlapi.SnmpEngine()
    community = hlapi.CommunityData("public", mpModel=1)  # SNMPv2c
    target = hlapi.UdpTransportTarget(("192.168.0.59", 161))
    context = hlapi.ContextData()

    # object_id = hlapi.ObjectIdentity(
    #     "iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0"
    # )
    sysDescr = hlapi.ObjectType(hlapi.ObjectIdentity("SNMPv2-MIB", "sysDescr", 0))
    sysUpTime = hlapi.ObjectType(hlapi.ObjectIdentity("SNMPv2-MIB", "sysUpTime", 0))
    ifInOctets = hlapi.ObjectType(hlapi.ObjectIdentity("IF-MIB", "ifInOctets", 1))
    command = hlapi.getCmd(
        engine, community, target, context, sysDescr, sysUpTime, ifInOctets
    )
    _old_print_results(command)

def temperature():
    """One-Wire Temperature sensor on ControlByWeb X-410 module."""
    temp = _make_object("XYTRONIX-MIB", "temp", 0)
    command = _make_get("192.168.0.132", "webrelay", temp)
    results = _run_command(command)
    _print_results(results)

def rewrite():
    """PySNMP Tutorial Common Operations Example, rewritten"""
    sysDescr = _make_object("SNMPv2-MIB", "sysDescr", 0)
    sysUpTime = _make_object("SNMPv2-MIB", "sysUpTime", 0)
    ifInOctets = _make_object("IF-MIB", "ifInOctets", 1)
    command = _make_get("192.168.0.59", "public", sysDescr, sysUpTime, ifInOctets)
    results = _run_command(command)
    _print_results(results)

def _listen_callback(
    snmp_engine,
    state_reference,
    context_engine_id,
    context_name,
    var_binds,
    callback_context,
):
    transport_domain, transport_address = snmp_engine.msgAndPduDsp.getTransportInfo(
        state_reference
    )
    print(
        f"\nNotification from {transport_address}, "
        f"SNMP Engine {context_engine_id.prettyPrint()}, "
        f"Context {context_name.prettyPrint()}"
    )
    for oid, value in var_binds:
        name = rfc1902.ObjectIdentity(oid.prettyPrint())
        name.resolveWithMib(_view_controller)
        print(f"    {name.prettyPrint()} ({oid.prettyPrint()}) = {value.prettyPrint()}")

def listen(
    address=DEFAULT_ADDRESSS,
    port=DEFAULT_PORT,
    community=DEFAULT_COMMUNITY,
    mibs=DEFAULT_MIBS,
):
    """Listen to and SNMP trap and print events."""
    # Based on pySNMP example code.
    mib_builder = builder.MibBuilder()
    compiler.addMibCompiler(mib_builder)
    mib_builder.loadModules(*mibs)
    global _view_controller
    _view_controller = view.MibViewController(mib_builder)
    loop = asyncio.get_event_loop()
    snmp_engine = engine.SnmpEngine()
    print(f"Agent is listening SNMP Trap on {address}, Port: {port}")
    if port < 1024:
        print(
            "WARNING: Port < 1024. Root priviledges or authbind required on *nix systems."
        )
    print("-" * 79)
    config.addTransport(
        snmp_engine,
        udp.domainName + (1,),
        udp.UdpTransport().openServerMode((address, port)),
    )
    config.addV1System(snmp_engine, community, community)
    ntfrcv.NotificationReceiver(snmp_engine, _listen_callback)
    print("Press CTRL-C to quit.")
    loop.run_forever()


#!/bin/env python
#
# 基本边缘检测
#

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

def sobel_x(gray_im, plot: plt.Axes = None):
    sx_kernel = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
    gx_im = cv.filter2D(gray_im, -1, sx_kernel)

    abs_gx_im = np.abs(gx_im)

    if plot is not None:
        plot.set_title('Sobel-X abs gradient')
        plot.set_xticks([])
        plot.set_yticks([])
        plot.imshow(abs_gx_im, cmap='gray')
    return abs_gx_im

def sobel_y(gray_im, plot: plt.Axes = None):
    sy_kernel = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
    gy_im = cv.filter2D(gray_im, -1, sy_kernel)

    abs_gy_im = np.abs(gy_im)

    if plot is not None:
        plot.set_title('Sobel-Y abs gradient')
        plot.set_xticks([])
        plot.set_yticks([])
        plot.imshow(abs_gy_im, cmap='gray')
    return abs_gy_im

if __name__ == '__main__':
    figure = plt.figure()

    gray_im = cv.imread('CH10_Images/Fig1016(a)(building_original).tif', cv.IMREAD_GRAYSCALE)
    gray_im = gray_im.astype(np.float32)
    # cv.normalize(gray_im, gray_im, 0, 1, cv.NORM_MINMAX)

    oplot = figure.add_subplot(221)
    oplot.set_title('origin')
    oplot.set_xticks([])
    oplot.set_yticks([])
    oplot.imshow(gray_im, cmap='gray')

    # x和y方向上的梯度
    g_sx = sobel_x(gray_im)
    g_sy = sobel_y(gray_im)
    # 梯度图
    gradient_im = g_sx + g_sy
    thresh_g = np.max(gradient_im) * 0.33
    thresh_im = np.zeros_like(gradient_im)
    thresh_im[gradient_im>thresh_g] = 1

    gplot = figure.add_subplot(223)
    gplot.set_title('thresh by 33%')
    gplot.set_xticks([])
    gplot.set_yticks([])
    gplot.imshow(thresh_im, cmap='gray')

    blur_im = cv.medianBlur(gray_im, 5)

    bplot = figure.add_subplot(222)
    bplot.set_title('blurred origin')
    bplot.set_xticks([])
    bplot.set_yticks([])
    bplot.imshow(blur_im, cmap='gray')

    # x和y方向上的梯度
    g_sx_blur = sobel_x(blur_im)
    g_sy_blur = sobel_y(blur_im)
    # 梯度图
    blur_gradient_im = g_sx_blur + g_sy_blur
    thresh_bg = np.max(blur_gradient_im) * 0.33
    blur_thresh_im = np.zeros_like(blur_gradient_im)
    blur_thresh_im[blur_gradient_im>thresh_bg] = 1

    bgplot = figure.add_subplot(224)
    bgplot.set_title('blurred - thresh by 33%')
    bgplot.set_xticks([])
    bgplot.set_yticks([])
    bgplot.imshow(blur_thresh_im, cmap='gray')

    plt.show()


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

import bink
import pytest
from unittest.mock import patch #, mock_open

def test_args_happy_path():
    '''
    Pytest for the happy path args
    '''
    args = ['--file' , 'bink/poles.csv']
    parser = bink.parse_args()
    nargs = parser.parse_args(args)
    assert(nargs.file == 'bink/poles.csv')

def test_args_years():
    args = ['-y']
    parser = bink.parse_args()
    nargs = parser.parse_args(args)
    assert(nargs.years == True)

def test_args_invalid(capsys):
    args = ['-a', 'what is this']
    with pytest.raises(SystemExit):
        parser = bink.parse_args()
        parser.parse_args(args)
        out, err = capsys.readouterr()
        assert out == 2

data="test,a,new,file"
# mock_open_func = mock_open(read_data=data)
def mock_csv(f):
    print('I was called with {}'.format(f))
    return [ {'this':123, 'is':'boo', 'a':123,'dict':'really?'} ]

@patch('bink.Pole')
def test_file_open(mockPole):
    '''
    Will have to mock the filesystem for reading.
    '''
    test_data = [
        {'a':12, 'b':23, 'c':'elephant'},
        {'a':12, 'b':23, 'c':'elephant'},
        {'a':12, 'b':23, 'c':'elephant'},
        {'a':12, 'b':23, 'c':'elephant'},
    ]
    pole = mockPole()
    pole.read_data.return_value = True
    pole.csv_data = test_data

    res = pole.read_data('test')
    assert res == True

@patch('bink.csv.DictReader', side_effect=mock_csv)
def test_csv_reader(mock_csv):
    pole=bink.Pole('f')
    assert pole.csv_data == [ {'this':123, 'is':'boo', 'a':123,'dict':'really?'} ]


from django.views import View
from django.shortcuts import render, redirect, get_object_or_404

from iplapp.models import *

class SeasonView(View):

    def get(self, request, *args, **kwargs):
        seasons = Season.objects.all().order_by('-season')
        if  request.user.is_anonymous:
            current_user = None
        else:
            current_user = request.user

        if kwargs:
            season = Season.objects.get(season=kwargs.get('season'))
            page = int(kwargs.get('page'))

            start_page = 8 * (page - 1) + 1
            end_page = 8 * page

            match = Match.objects.filter(season=season)[start_page: end_page + 1]
            count = Match.objects.filter(season=season).count()

            next = page + 1
            prev = page - 1

            if start_page == 1:
                prev = None

            if start_page == (count // 8) * 8 + 1:
                next = None

        return render(
            request,
            template_name='seasons.html',
            context={
                'user': current_user,
                'year': season,
                'seasons': seasons,
                'next': next,
                'prev': prev,
                'matches': enumerate(match, start=start_page),
                'title': 'IPL Clone App',
            }
        )


# Goal 2

# Create a single iterable that combines all the columns from all the iterators.

# The iterable should yield named tuples containing all the columns.
# Make sure that the SSN's across the files match!
# All the files are guaranteed to be in SSN sort order, and every SSN is unique, and every SSN appears in every file.
# Make sure the SSN is not repeated 4 times - one time per row is enough!

from operator import add
from itertools import chain, islice
from functools import reduce
from collections import namedtuple
from goal1_iterators import file_iterator
from constants import data_types, files, tuple_names

def files_iterator(files, data_types):
    """My solution to goal2.  It is not efficient since it does not use the fact that
    namedtuples should be created once and reused.  It would thus be better to return the
    joined dictionary"""
    with open(files[0]) as f0, open(files[1]) as f1, open(files[2]) as f2, open(files[3]) as f3:
        file0_iterator = file_iterator(files[0], data_types[0], tuple_names[0])
        file1_iterator = file_iterator(files[1], data_types[1], tuple_names[1])
        file2_iterator = file_iterator(files[2], data_types[2], tuple_names[2])
        file3_iterator = file_iterator(files[3], data_types[3], tuple_names[3])

        iterators = list(zip(file0_iterator, file1_iterator,
                         file2_iterator, file3_iterator))

        # RESET THE ITERATORS if use get_joined_nt_names in improved post-study code
        f0.seek(0), f1.seek(0), f2.seek(0), f3.seek(0)
        return iterators

def print_n_rows_joint(num_rows_to_display=5):
    """Print n rows of the joint namedtuple"""
    iterators = files_iterator(files, data_types,)
    for row in islice(iterators, num_rows_to_display):
        dict0, dict1, dict2, dict3 = row[0]._asdict(
        ), row[1]._asdict(), row[2]._asdict(), row[3]._asdict()
        if dict0['ssn'] == dict1['ssn'] == dict2['ssn'] == dict3['ssn']:
            print("Next joined nametuple: ")
            joined_dict = {**dict0, **dict1, **dict2, **dict3}
            joined_nt = namedtuple('JoinedNT', joined_dict.keys())(
                *joined_dict.values())
            print("Joined NT: ", joined_nt)

if __name__ == "__main__":
    print_n_rows_joint(10)
    # OUTPUT
    # Next joined nametuple:
    # Joined NT:  JoinedNT(ssn='100-53-9824', first_name='Sebastiano', last_name='Tester', gender='Male', language='Icelandic', vehicle_make='Oldsmobile', vehicle_model='Bravada', model_year=1993, employer='Stiedemann-Bailey', department='Research and Development', employee_id='29-0890771', last_updated='2017-10-07T00:14:42Z', created='2016-01-24T21:19:30Z')
    # Next joined nametuple:
    # Joined NT:  JoinedNT(ssn='101-71-4702', first_name='Cayla', last_name='MacDonagh', gender='Female', language='Lao', vehicle_make='Ford', vehicle_model='Mustang', model_year=1997, employer='Nicolas and Sons', department='Sales', employee_id='41-6841359', last_updated='2017-01-23T11:23:17Z', created='2016-01-27T04:32:57Z')
    # Next joined nametuple:
    # Joined NT:  JoinedNT(ssn='101-84-0356', first_name='Nomi', last_name='Lipprose', gender='Female', language='Yiddish', vehicle_make='GMC', vehicle_model='Yukon', model_year=2005, employer='Connelly Group', department='Research and Development', employee_id='98-7952860', last_updated='2017-10-04T11:21:30Z', created='2016-09-21T23:04:07Z')
    # Next joined nametuple:
    # Joined NT:  JoinedNT(ssn='104-22-0928', first_name='Justinian', last_name='Kunzelmann', gender='Male', language='Dhivehi', vehicle_make='Oldsmobile', vehicle_model='Intrigue', model_year=2000, employer='Upton LLC', department='Marketing', employee_id='56-9817552', last_updated='2017-03-28T12:38:29Z', created='2016-04-15T11:37:17Z')
    # Next joined nametuple:
    # Joined NT:  JoinedNT(ssn='104-84-7144', first_name='Claudianus', last_name='Brixey', gender='Male', language='Afrikaans', vehicle_make='Ford', vehicle_model='Crown Victoria', model_year=2008, employer='Zemlak-Olson', department='Business Development', employee_id='46-2886707', last_updated='2018-02-19T01:34:33Z', created='2016-03-15T14:07:57Z')
    # Next joined nametuple:
    # Joined NT:  JoinedNT(ssn='105-27-5541', first_name='Federico', last_name='Aggett', gender='Male', language='Chinese', vehicle_make='Ford', vehicle_model='Mustang', model_year=2001, employer='Kohler, Bradtke and Davis', department='Support', employee_id='80-0975518', last_updated='2017-07-24T08:58:52Z', created='2016-07-23T17:58:35Z')
    # Next joined nametuple:
    # Joined NT:  JoinedNT(ssn='105-85-7486', first_name='Angelina', last_name='McAvey', gender='Female', language='Punjabi', vehicle_make='Chrysler', vehicle_model='300', model_year=2008, employer='Roberts, Torphy and Dach', department='Human Resources', employee_id='77-4895332', last_updated='2018-02-14T11:32:39Z', created='2016-12-15T05:46:43Z')
    # Next joined nametuple:
    # Joined NT:  JoinedNT(ssn='105-91-5022', first_name='Moselle', last_name='Apfel', gender='Female', language='Latvian', vehicle_make='Isuzu', vehicle_model='Hombre Space', model_year=2000, employer='Lind-Jast', department='Marketing', employee_id='79-6418731', last_updated='2018-03-24T14:29:33Z', created='2016-03-24T03:43:03Z')
    # Next joined nametuple:
    # Joined NT:  JoinedNT(ssn='105-91-7777', first_name='Audi', last_name='Roach', gender='Female', language='Estonian', vehicle_make='Chevrolet', vehicle_model='Silverado 3500', model_year=2004, employer='Bashirian-Lueilwitz', department='Engineering', employee_id='44-3328799', last_updated='2017-05-11T01:48:32Z', created='2016-05-31T00:38:13Z')
    # Next joined nametuple:
    # Joined NT:  JoinedNT(ssn='106-35-1938', first_name='Mackenzie', last_name='Nussey', gender='Male', language='Swedish', vehicle_make='GMC', vehicle_model='Sonoma Club', model_year=1992, employer='Windler, Marks and Haley', department='Services', employee_id='54-6271885', last_updated='2017-10-21T01:07:28Z', created='2016-09-08T04:02:12Z')

# OTHER SCRATCHWORK AND EXPERIMENTS

# SNIFF OUT THE NAME: MAKE THIS INTO A FUNCTION
# for row in islice(iterators, 1):
#     print("ROW 0 FIELDS: ", row[0]._fields)
#     print("ROW 1 FIELDS: ", row[1]._fields)
#     all_fields = row[0]._fields + row[1]._fields + \
#         row[2]._fields+row[3]._fields
#     all_fields = ('ssn',) + \
#         tuple(elt for elt in all_fields if elt != 'ssn')
#     joined_nt = namedtuple('JoinedNT', all_fields)
#     print(all_fields)

# You've pretty much nailed it as far as vanilla Python is concerned, but there's an extra simplification you can do if you're using Python 3.5+.

A = namedtuple("A", "a b c")
B = namedtuple("B", "d e")
a = A(10, 20, 30)
b = B(40, 50)
C = namedtuple("C", A._fields + B._fields)
# print(C(*(a + b)))
# C(a=10, b=20, c=30, d=40, e=50)
# Available in Python 3.5+
#print(C(*a, *b))
# C(a=10, b=20, c=30, d=40, e=50)
# print(A._fields)

def namedtuplemerge(*args):
    cls = namedtuple('_'.join(arg.__class__.__name__ for arg in args), reduce(
        add, (arg._fields for arg in args)))
    return cls(*chain(*args))

contents = [[] for _ in range(4)]
for i in range(4):
    #print(f"File {files[i]}")
    # contents[i] = list(chain(file_iterator(
    #     files[i], data_types=data_types[i], tuple_name=tuple_names[i])))
    contents[i] = file_iterator(
        files[i], data_types=data_types[i], tuple_name=tuple_names[i])
# print(contents)
#args = files
#all_names = list(nt[0]._fields for nt in contents)
# reduced = reduce(add, list(nt[0]._fields for nt in contents))
# # make 'ssn' appear as the first field
# fields_ = ('ssn',) + tuple(set(reduced)-{'ssn'})

# fields_ = ('ssn',)+tuple(set(contents[0][0]._fields + contents[1]
# [0]._fields+contents[2][0]._fields+contents[3][0]._fields)-{'ssn'})
# print(fields_)

# C = namedtuple("C", contents[0][0]._fields + contents[1][0]._fields)
# print(C(*a, *b))

# print(namedtuplemerge(contents[0], contents[1]))

# Loop through rows
# for each row, chain across row (ie, apply merge to each row) if ssn == for each arg

# print(len(contents))
#print(list(nt._fields for nt in contents))
#print(list((nt[0] for nt in contents)))
#print(list((nt[0] for nt in contents if not nt.ssn)))
def joined_fieldnames(contents):
    # To make this function self-contained, consider placing the import inside the function
    #from functools import reduce
    reduced = reduce(
        add, (nt[0]._fields for nt in contents))
    #print('REDUCED', reduced)
    reduced_ = ('ssn',)+tuple(elt for elt in reduced if elt != 'ssn')
    # print(reduced_)
    nt_joint = namedtuple("Joined", reduced_)
    #print("FIELDS: ", nt_joint._fields)
    return nt_joint

# joined_nt = joined_fieldnames(contents)
# count = 0
# for row in joined_nt(*chain(*files)):
#     print(joined_nt(row))
# # print(count)

# print(joined_nt(*chain(*files)))

# def files_iterator(files, data_types):
#     #FILES NOT OPENING CORRECTLY
#     with open(files[0]) as f0, open(files[1]) as f1, open(files[2]) as f2, open(files[3]) as f3:
#         reader0, reader1, reader2, reader3 = csv.reader(
#             f0), csv.reader(f1), csv.reader(f2), csv.reader(f3)
#         readers = reader0, reader1, reader2, reader3
#         nts = get_names(next(f0)), get_names(
#             next(f1)), get_names(next(f2)), get_names(next(f3))
#         yield from reader0

# print(files_iterator(files, data_types))

# def display_first_rows(num_rows=5):
#     contents = [[] for _ in range(len(files))]
#     for i in range(len(files)):
#         print(f"File {files[i]}")
#         contents[i] = files_iterator(
#             files[i], data_types=data_types[i])  # , tuple_name=tuple_names[i])
#         for ticket in islice(contents[i], num_rows):
#             print(ticket)

# display_first_rows(5)

def get_joined_nt_names():
    with open(files[0]) as f0, open(files[1]) as f1, open(files[2]) as f2, open(files[3]) as f3:
        file0_iterator = file_iterator(files[0], data_types[0], tuple_names[0])
        file1_iterator = file_iterator(files[1], data_types[1], tuple_names[1])
        file2_iterator = file_iterator(files[2], data_types[2], tuple_names[2])
        file3_iterator = file_iterator(files[3], data_types[3], tuple_names[3])

        iterators = list(zip(file0_iterator, file1_iterator,
                         file2_iterator, file3_iterator))

        # SNIFF OUT THE NAME: MAKE THIS INTO A FUNCTION
        for row in islice(iterators, 1):
            #print("ROW 0 FIELDS: ", row[0]._fields)
            #print("ROW 1 FIELDS: ", row[1]._fields)
            all_fields = row[0]._fields + row[1]._fields + \
                row[2]._fields+row[3]._fields
            all_fields = ('ssn',) + \
                tuple(elt for elt in all_fields if elt != 'ssn')
            joined_nt = namedtuple('JoinedNT', all_fields)
            # print(all_fields)
            return joined_nt

# def files_iterator(files, data_types):
#     with open(files[0]) as f0, open(files[1]) as f1, open(files[2]) as f2, open(files[3]) as f3:
#         reader0, reader1, reader2, reader3 = csv.reader(
#             f0), csv.reader(f1), csv.reader(f2), csv.reader(f3)
#         readers = zip(reader0, reader1, reader2, reader3)
#         #print("READERS: ", list(readers))
#         nt0, nt1, nt2, nt3 = get_names(next(f0), tuple_names[0]), get_names(
#             next(f1), tuple_names[1]), get_names(next(f2), tuple_names[2]), get_names(next(f3), tuple_names[3])
#         # print(nt0)
#         # print(nt1)
#         #print("LEN READERS: ", len(readers))

#         # for r0 in readers:
#         #     r0 = (type_caster(elt[0], elt[1])
#         #           for elt in zip(r0, data_types[0]))

#         #     print("NEXT", nt0(*r0).ssn)

#         for (r0, r1, r2, r3) in readers:
#             r0 = (type_caster(elt[0], elt[1])
#                   for elt in zip(r0, data_types[0]))
#             r1 = (type_caster(elt[0], elt[1])
#                   for elt in zip(r1, data_types[1]))
#             r2 = (type_caster(elt[0], elt[1])
#                   for elt in zip(r2, data_types[2]))
#             r3 = (type_caster(elt[0], elt[1])
#                   for elt in zip(r3, data_types[3]))
#             # yield list(chain(nt0(*r0), nt1(*r1), nt2(*r2), nt3(*r3)))
#             # yield list(chain(nt0(*r0), nt1(*r1), nt2(*r2), nt3(*r3)))
#             # print(nt0(*r0).ssn)
#             # print(nt1(*r1).ssn)
#             if nt0(*r0).ssn == nt1(*r1).ssn == nt2(*r2).ssn == nt3(*r3).ssn:
#                 print(list(zip(nt0(*r0), nt1(*r1), nt2(*r2), nt3(*r3))))

#             # print("R0", nt0(*r0))
#             # yield r0
#             # yield r1
#             # yield r2
#             # yield r3
# print("FILES ITERATOR: ", files_iterator(files, data_types=data_types))

# BACKUP
# def files_iterator(files, data_types):
#     with open(files[0]) as f0, open(files[1]) as f1, open(files[2]) as f2, open(files[3]) as f3:
#         reader0, reader1, reader2, reader3 = csv.reader(
#             f0), csv.reader(f1), csv.reader(f2), csv.reader(f3)
#         readers = reader0, reader1, reader2, reader3
#         nt0, nt1, nt2, nt3 = get_names(next(f0)), get_names(
#             next(f1)), get_names(next(f2)), get_names(next(f3))
#         print("LEN READERS: ", len(readers))
#         for r0, r1, r2, r3 in readers:
#             r0 = (type_caster(elt[0], elt[1])
#                   for elt in zip(r0, data_types[0]))
#             r1 = (type_caster(elt[0], elt[1])
#                   for elt in zip(r0, data_types[0]))
#             r2 = (type_caster(elt[0], elt[1])
#                   for elt in zip(r0, data_types[0]))
#             r3 = (type_caster(elt[0], elt[1])
#                   for elt in zip(r0, data_types[0]))
#             # yield list(chain(nt0(*r0), nt1(*r1), nt2(*r2), nt3(*r3)))
#             # yield list(chain(nt0(*r0), nt1(*r1), nt2(*r2), nt3(*r3)))
#             print("R0", nt0(*r0))
#             yield r0
#             yield r1
#             yield r2
#             yield r3

# print("FILES ITERATOR: ", files_iterator(files, data_types=data_types))

# for i in range(len(contents[0])):
#     print("NEXT: ")
#     if contents[0][i].ssn == contents[1][i].ssn == contents[2][i].ssn == contents[3][i].ssn:
#         print(joined_nt(contents[0][i], contents[1]
#               [i], contents[2][i], contents[3][i]))

# for j in range(len(files)):
#     # for k in range(len(contents[0][]))
#     # if contents[0]
#     #print(i, j)
#     elt = contents[j][i]
#     print(elt)
#     if
#     print(elt.ssn)
#print(f"File {files[i]}")
#contents[i] = file_iterator(files[i], data_types=filetypes[i])
# print(contents)


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

# In[1]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# In[2]:

data = pd.read_csv("C:/Users/umrahzargar/Downloads/vgsales.csv")

# In[4]:

data.head()

# In[5]:

unique_values = data.nunique()
print(unique_values)

# In[6]:

unique_values_genre = data['Genre'].unique()
print(unique_values_genre)

# In[7]:

data.dropna(how="any", inplace=True)

# In[8]:

data.sort_values(by="Global_Sales", ascending=False, inplace=True)
data.reset_index(drop=True, inplace=True)
data["Rank"] = np.arange(1, len(data) + 1)

# In[9]:

data["Year"] = data["Year"].astype(int)
data["Global_Sales"]= data["Global_Sales"].astype(float)

# In[10]:

data.info()

# # VISUALIZATION

# In[11]:

plt.pie(data.Genre.value_counts(), labels=data.Genre.value_counts().index, autopct='%1.0f%%')
plt.title('Genre')
plt.ylabel(' ')
plt.show()

# In[13]:

plt.barh(data.Genre,data.Global_Sales,color="maroon")
plt.xlabel('Global_Sales')
plt.ylabel('Genre')
plt.title('Global_Sales as per Genre')

# In[14]:

data.Genre.value_counts().head(5).plot(kind = 'bar', color = 'pink')
plt.xlabel('Genre')
plt.ylabel(' ')
plt.title('Top 5 game genre')
plt.show()

# In[15]:

print(data["Year"].unique())

# In[23]:

data_year= data.groupby(['Year'])
data_year = data_year['NA_Sales','EU_Sales','JP_Sales'].aggregate(np.mean)
data_year.plot( linestyle='dashed')
plt.title('Average Sales of the Game')

# In[25]:

data=data[data.Year==1990]
data=data.sort_values('Global_Sales',ascending=False).head(5)
data.Name.value_counts().head(5).plot(kind = 'bar', color = 'yellow')
plt.xlabel('Name')
plt.ylabel('Global_Sales')
plt.title('Top 5 game Names')
plt.show()

# In[27]:

data.Genre.value_counts().head(5).plot(kind = 'bar', color = 'purple')
plt.xlabel('Genre')
plt.ylabel(' ')
plt.title('Top 5 game genre')
plt.show()

# In[31]:

data=data.groupby('Publisher').agg({'JP_Sales':'sum','EU_Sales':'sum','NA_Sales':'sum','Other_Sales':'sum'})
data=data.sort_values('NA_Sales',ascending=False).head(3)
data.plot(kind='bar',figsize=(8,8))
plt.xlabel('Publisher')
plt.ylabel('Sales')
plt.title('Game Sales Per Region from Publishers')


import sys
input = sys.stdin.readline
from collections import deque

def bfs(x, y):
    q = deque()
    q.append(x)
    while q:
        x = q.popleft()
        if x == y:
            return visited[x]
        for n_x, cost in tree[x]:
            if not visited[n_x]:
                visited[n_x] = visited[x] + cost
                q.append(n_x)

N, M = map(int, input().split())
tree = [[] for _ in range(1001)]
nodes = []
answer = []

for _ in range(N-1):
    a, b, c = map(int, input().split())
    tree[a].append([b, c])
    tree[b].append([a, c])

for _ in range(M):
    a, b = map(int, input().split())
    nodes.append([a, b])

for i in range(len(nodes)):
    visited = [0] * 1001
    answer.append(bfs(nodes[i][0], nodes[i][1]))

for a in answer:
    print(a)

백준/Gold/1240. 노드사이의 거리/노드사이의 거리.py


import torch

def reorder_desc(desc, batch_sz):
    """Reorders Descriptors"""

    b, c, h, w = desc.shape
    desc = desc.view(-1, batch_sz, c, h, w)
    desc = desc.transpose(1, 0)
    return desc

def pose_square(pose):
    """Converts pose matrix of size 3x4 to a square matrix of size 4x4"""

    pose_sh = pose.shape
    if pose_sh[2] == 3:
        pose_row = torch.tensor([0., 0., 0., 1.])
        if pose.is_cuda:
            pose_row = pose_row.to(pose.device)
        pose_row = pose_row.repeat(pose_sh[0], pose_sh[1], 1, 1)
        pose = torch.cat((pose, pose_row), 2)

    return pose

def make_symmetric(anc, ref):
    """Makes anchor and reference tensors symmetric"""

    if (anc is None) or (ref is None):
        return None
    ancs = anc.shape
    views = torch.stack(ref, 0)
    if len(ancs) == 3:
        views = views.view(-1, ancs[1], ancs[2])
    else:
        views = views.view(-1, anc.shape[1], ancs[2], ancs[3])
    anc_ref = torch.cat((anc, views), 0)
    return anc_ref


import importlib
import logging

import pygame

from noname import constants
from noname import units
from noname import hud

logger = logging.getLogger(__name__)

if not pygame.font:
    logger.warning("Warning, fonts disabled")
if not pygame.mixer:
    logger.warning("Warning, sound disabled")

class GameClient(object):
    """ The GameClient, drawing on the screen """

    def __init__(self, server_address, width=constants.DEFAULT_WIDTH,
                 height=constants.DEFAULT_HEIGHT, fps=constants.DEFAULT_FPS,):
        pygame.init()
        self.server_address = server_address
        self.width = width
        self.height = height
        self.fps = fps
        self.screen = pygame.display.set_mode((self.width, self.height))
        self.level = None
        self.players = []
        self.player = None
        self.clock = pygame.time.Clock()
        self.hud = hud.Hud(self)

    def update(self, event):
        self.clock.tick(self.fps)
        self.screen.fill(0)
        self.level.draw(self.screen)
        self.player.render(self.screen)
        self.hud.render()
        self.hud.handle(event)
        pygame.display.flip()

    def run(self):
        # FIXME
        # DEV SHIT REMOVE
        import time
        start = time.time()
        # DEV SHIT REMOVE

        running = True
        self.load_level(constants.DEFAULT_LEVEL)
        while running:
            for event in pygame.event.get():
                # pygame.time.wait(500)  # dev quit
                # return  # dev quit
                if event.type == pygame.QUIT:
                    return

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        return

                self.update(event)

                # IMAGE FLIP ONLY?
                # elif event.type == pygame.KEYDOWN:
                #    if self.is_controls_moving(event):
                #         self.player.move(event.key)
            keys = pygame.key.get_pressed()
            if self.is_controls_moving(keys):
                self.player.move(keys)
            if (time.time() - start) >= 20:
                running = False

    def is_controls_moving(self, keys):
        if (keys[pygame.K_UP] or keys[pygame.K_DOWN] or
           keys[pygame.K_LEFT] or keys[pygame.K_RIGHT]):
            logger.debug("key pressed")
            return True
        else:
            return False

        # if ((event.key == pygame.K_RIGHT) or (event.key == pygame.K_LEFT) or
        #   (event.key == pygame.K_UP) or (event.key == pygame.K_DOWN)):
        #    return True
        # else:
        #    return False

    def load_level(self, name):
        module_name = "noname.levels.%s" % name
        logger.debug("Loading level: %s" % module_name)
        level_module = importlib.import_module(module_name)
        self.level = level_module.Level()
        self.level.load_sprites()

    def add_player(self, x=0, y=0):
        single_player_metadata = {
            "name": "gary",
            "file": "Gary.png",
            "width": 32,
            "height": 32,
            "position": {
                "x": x,
                "y": y,
            },
        }
        player = units.Player(single_player_metadata["name"],
                              single_player_metadata)
        # FIXME
        if self.player is None:
            self.player = player
        self.players.append(player)


import numpy as np
import matplotlib.pyplot as plt
import os, shutil, subprocess
from mpl_toolkits.mplot3d import Axes3D

from mpl_toolkits.mplot3d.art3d import Line3DCollection
from matplotlib.colors import ListedColormap, BoundaryNorm

### Using trick from https://stackoverflow.com/questions/37327308/add-alpha-to-an-existing-matplotlib-colormap
### to add alpha gradient in color map
cmap = plt.cm.Blues
my_cmap = cmap(np.arange(cmap.N))
my_cmap[:,-1] = np.linspace(0, 1, cmap.N)
my_cmap = ListedColormap(my_cmap)

### Using the colorline recipe to draw gradient lines : https://nbviewer.jupyter.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb
### Code adapted for Line3DCollection
def make_segments(x, y, z):
    '''
    Create list of line segments from x and y coordinates, in the correct format for LineCollection:
    an array of the form   numlines x (points per line) x 3 (x, y, z) array
    '''
    points = np.array([x, y, z]).T.reshape(-1, 1, 3)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)

    return segments

def colorline(x, y, z, k=None, cmap=plt.get_cmap('copper'), norm=plt.Normalize(0.0, 1.0), linewidth=3, alpha=1.0):
    '''
    Plot a colored line with coordinates x and y
    Optionally specify colors in the array z
    Optionally specify a colormap, a norm function and a line width
    '''

    # Default colors equally spaced on [0,1]:
    if k is None:
        k = np.linspace(0.0, 1.0, len(x))

    # Special case if a single number:
    if not hasattr(k, "__iter__"):  # to check for numerical input -- this is a hack
        k = np.array([k])

    k = np.asarray(k)

    segments = make_segments(x, y, z)
    lc = Line3DCollection(segments, array=k, cmap=cmap, norm=norm, linewidth=linewidth)

    ax = plt.gca()
    ax.add_collection(lc)

    return lc

# Simulation part
sigma = 10.0
beta  = 8.0/3.0
rho   = 28.0

x = 2.01
y = 5.0
z = 20.0

# dx/dt, dy/dt, dz/dt
def ddt(x, y, z):
    dxdt = sigma*(y-x)
    dydt = x*(rho-z)-y
    dzdt = x*y-beta*z

    return dxdt, dydt, dzdt

# Euler method. We build the whole array so we can plot nice trails
# could clearly be optimised into calculating and rendering at the same time !
print('Simulating ...')
max_t = 25.0
t     = 0.0
dt    = 0.001

pos = []
while t < max_t:
    dxdt, dydt, dzdt = ddt(x, y, z)
    x += dxdt * dt
    y += dydt * dt
    z += dzdt * dt

    pos.append((x, y, z))

    t += dt

# Rendering part
print('Rendering ...')
pos = np.asarray(pos)
L = 5000 # Length of the trail
Np = pos.shape[0] - L
cur = L

# Limits of the graph
min_x = pos[:,0].min()
max_x = pos[:,0].max()
min_y = pos[:,1].min()
max_y = pos[:,1].max()
min_z = pos[:,2].min()
max_z = pos[:,2].max()

steps = 100 # Frame skipping

# Violently removing everything from the render directory
if os.path.exists('render'):
    shutil.rmtree('render')
os.mkdir('render')
Nf = Np//steps
print('Preparing to render {} images'.format(Nf))
for i in range(Nf):
    filename = 'img_{:05}.png'.format(i)
    print('Rendering', filename)
    plt.close('all')

    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111, projection='3d')
    colorline(pos[cur-L:cur,0], pos[cur-L:cur,1], pos[cur-L:cur,2], cmap=my_cmap, linewidth=2)
    ax.set_xlim((min_x, max_x))
    ax.set_ylim((min_y, max_y))
    ax.set_zlim((min_z, max_z))
    plt.savefig('render/'+filename)
    cur+=steps

# Building ffmpeg command and running
cmd = 'ffmpeg -framerate 60 -i render/img_%05d.png -c:v libx264 -preset slow -crf 18 -pix_fmt yuv420p render/movie.mp4'
print('Converting to video :', cmd)
subprocess.call(cmd.split())
print('Generated movie should be in render/movie.mp4')


# Packages
import argparse
from classes.llm_agent import agent
from utils.gpt.chat import load_api_key

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--all",
        action = "store_true",
        help = "to load all the environments if given",
    )
    parser.add_argument(
        "--API-KEY",
        default = "utils/api/API_KEY",
        type = str,
        help = "the location to load your OpenAI API Key"
    )
    parser.add_argument(
        "--bound",
        type = int,
        help = "the bound of memory both in forget and remember",
        default = 2
    )
    parser.add_argument(
        "--cross",
        action = "store_true",
        help = "whether will agent bring experience from the past environment or they will refresh their experiences once enter new environment during training"
    )
    parser.add_argument(
        "--delay",
        type = int,
        default = 5,
        help = "the number of seconds to delay when in OpenAI API Calling"
    )
    parser.add_argument(
        "--envs",
        nargs = "+",
        help = "list of environment names, see the data/input/env/env_ids.json for mapping between index and env",
        default = [0],
        type = int
    )
    parser.add_argument(
        "--eval",
        action = "store_true",
        help = "evaluation mode boolean, otherwise it is train mode"
    )
    parser.add_argument(
        "--exp-src",
        nargs = "+",
        type = str,
        help = "list of the starting experience read path, if it is less than the --envs, then we will use default one"
    )
    parser.add_argument(
        "--gpt",
        type = str,
        nargs = "+",
        choices = ["0", "1", "2", "3", "4", "5", "6", "7"],
        help = r'the version of gpt, type version number like 3 or 4, the correspondance relationship is gpt_map = {"0": "gpt-3.5-turbo", "1": "gpt-3.5-turbo-0301", "2": "gpt-3.5-turbo-0613", "3": "gpt-3.5-turbo-16k", "4": "gpt-3.5-turbo-16k-0613", "5": "gpt-4", "6": "gpt-4-0314", "7": "gpt-4-0613"} it should be the same length as env if we want to have different gpt for different envs',
        default = ["0"]
    )
    parser.add_argument(
        "--input",
        action = "store_true",
        help = "if the action and experience will be given by user instead of generating from GPT"
    )
    parser.add_argument(
        "--log",
        action = "store_true",
        help = "if to print the logging informations by print()"
    )
    parser.add_argument(
        "--neg-memo",
        action = "store_true",
        help = "will the agent have negative memory or only positive memory storage"
    )
    parser.add_argument(
        "--prj-name",
        type = str,
        help = "the project name for your wandb",
        default = "LLM As Agent"
    )
    parser.add_argument(
        "--retrain",
        action = "store_true",
        help = "retrain from the directory given in the retrain-src for number of steps defined in --steps"
    )
    parser.add_argument(
        "--retrain-src",
        type = str,
        help = "the retraining directory folder in string",
        default = r"output/seed_23/train/GPT/ENV 0 steps 200 gpt 0 temp 0.7 view 7/2023-08-20 23-23-41"
    )
    parser.add_argument(
        "--screen",
        type = int,
        default = 640,
        help = "set the resolution for pygame rendering (width and height)",
    )
    parser.add_argument(
        "--seed",
        help = "random seed for reproducing results, so far only supports 20, 21, 22, 23, 24",
        default = 23,
        type = int
    )
    parser.add_argument(
        "--steps",
        nargs = "+",
        help = "list of steps taken in the args.env, it should have the same length as the environment, used both in trainng and evaluation",
        default = [100],
        type = int
    )
    parser.add_argument(
        "--temp",
        nargs = "+",
        type = float,
        default = [0.7],
        help = "the list of temprature used by the OpenAI API, if it is less than the --envs, then we will use the first one throughout"
    )
    parser.add_argument(
        "--view",
        nargs = "+",
        type = int,
        default = [7],
        help = "set the number of grid spaces visible in agent-view, if it is less than the --envs, then we will use the first one throughout"
    )
    parser.add_argument(
        "--wandb",
        action = "store_true",
        help = "whether to use wandb to record experiments"
    )

args = parser.parse_args()
llm_agent = agent(args)

try:
    load_api_key(args.API_KEY)
    # Create the agent
    if args.eval:
        llm_agent.eval()
    else:
        if args.retrain:
            llm_agent.retrain()
        else:
            llm_agent.train()

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    for env_id in args.envs:
        llm_agent.save_table(env_id)  # This will always be executed


from random import *
import struct
import rsa
import inspect
from rsa import PublicKey, PrivateKey
import queue
import os

LOG_LEVEL = 4
PKEY_SERIALIZED_SIZE = 256

def bigint_to_bytes(i):
    """
    Serialize a large integer as network serialized bytes
    :param i:
    :return:
    """

    parts = []

    while i:
        parts.append(i & (2 ** 32 - 1))
        i >>= 32

    return struct.pack('>' + 32 * 'L', *parts)

def key_to_bytes(rsakey):
    """
    Convert rsa.PublicKey to its serialized bytes form
    :param rsakey: rsa.PublicKey
    :return: bytes
    """

    i = rsakey.n

    p1 = i & (2 ** 1024 - 1)
    p2 = i >> 1024

    return bigint_to_bytes(p1) + bigint_to_bytes(p2)

def bytes_to_key(mbytes):
    """
    Create rsa.PublicKey object from its serialized bytes form
    :param mbytes: bytes
    :return: rsa.PublicKey
    """

    parts = struct.unpack('>' + 64 * 'L', mbytes)

    res = 0
    for p in reversed(parts):
        res *= 2 ** 32
        res += p

    return PublicKey(res, 2 ** 16 + 1)

def log(*args, **kwargs):
    """
    Default log tool, whose use is highly recommended
    instead of the standard python "print" function

    :param args: any - print args
    :param kwargs: dict - options
    """
    curframe = inspect.currentframe()
    calframe = inspect.getouterframes(curframe, 2)
    callername = calframe[1][3]
    if kwargs.get("level", 1) <= LOG_LEVEL:
        if not kwargs.get("omitcallername", False):
            print(callername + ":", *args)
        else:
            print(*args)

def generate_random():
    # res = bytearray(256)
    # res[0] = 1
    # for i in range(1, PKEY_SERIALIZED_SIZE):
    #     res[i] = randint(0, 1)
    # return bytes(res)
    return b"".join([b"1", os.urandom(255)])

def generate_graph(nodes, edges):
    adj_list = {}
    for node in nodes:
        adj_list[node] = []
    for edge in edges:
        adj_list[edge[0]] += [edge[1]]
    return adj_list

def find_min_hops(nodes, graph, s, t):
    visited = {}
    for node in nodes:
        visited[node] = (-1, [])
    boundery = queue.Queue()
    boundery.put(s)
    visited[s] = (0, [])
    while not boundery.empty():
        node = boundery.get()
        for adj_node in graph[node]:
            if visited[adj_node][0] == -1:
                visited[adj_node] = (visited[node][0] + 1, [node])
                boundery.put(adj_node)
            elif visited[adj_node][0] == visited[node][0] + 1:
                visited[adj_node] = (visited[adj_node][0], visited[adj_node][1] + [node])

    # print("find min hops", s, t)
    # print(visited)
    if visited[t][0] == -1:
        return []
    return calc(visited, s, t, [t])

def calc(visited, s, t, path):
    if len(visited[t][1]) == 0:
        return [path]
    paths = []
    for parent in visited[t][1]:
        paths += calc(visited, s, parent, [parent] + path)
    return paths

def min_weight(paths, config):
    if len(paths) == 0:
        return []
    min_path = paths[0]
    weight = calc_path_weight(paths[0], config)
    for path in paths:
        if calc_path_weight(path, config) < weight:
            weight = calc_path_weight(path, config)
            min_path = path
    return min_path

def calc_path_weight(path, config):
    weight = 0
    for i in range(len(path)-1):
        weight += config.latency(path[i], path[i+1])
    return weight

def has_conflict3(p1, p2, p3):
    return has_conflict2(p1, p2) or has_conflict2(p1, p3) or has_conflict2(p2, p3)

def has_conflict2(p1, p2):
    for n1 in p1:
        for n2 in p2:
            if n1 == n2:
                return True
    return False

def dfs_find_paths(graph, c_node, t, visited, path):
    visited[c_node] = True
    path += [c_node]

    if c_node == t:
        print(c_node, t)
        return [path]

    paths = []
    for n1 in graph[c_node]:
        if visited[n1] is False:
            d = dfs_find_paths(graph, n1, t, visited, path)
            paths += d
            print(paths)

    path.pop()
    visited[c_node] = False
    return paths

if __name__ == "__main__":
    a, b = rsa.newkeys(2048)
    print(a)
    s = key_to_bytes(a)
    r = bytes_to_key(s)
    print(r)
    print(generate_random())


import os
from queue import Empty

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "typemaster.settings")

import django
django.setup()

import pandas as pd
from api.models import Quote

filename = 'quotes.csv'
df = pd.read_csv(filename)

for index, row in df.head(10000).iterrows():
    text=row.quote
    quotee=row.quotee
    source=row.source if isinstance(row.source, str) else ""
    tags=row.tags
    quote = Quote(text=text, quotee=quotee, source=source, tags=tags)
    quote.save()
    print(index+1)

# print(df.columns)


# Youtube Audio Downloader
import os, re, time, requests, ffmpeg, colorama
from urllib.error import HTTPError, URLError

from moviepy.editor import *
from pytube import *

colorama.init()

total_errors = [0]
titles = []
replace_type = 0

def clear():
    global total_errors, titles
    total_errors = 0
    titles = []
    if os.name == 'nt':
        os.system('cls')
    else:
        os.system('clear')

def downloader(video, vtype):
    global total_errors, titles, replace_type

    print("\033[1;34;40m")

    try:
        # Get video and title
        yt = YouTube(video)
        vidtitle = yt.title
        print("\n" + str(len(titles) + 1) + ". Found Video: " + vidtitle)

        # Get file name
        fname = re.sub(yt.author + " ", "", vidtitle)

        if '-' in re.sub("^(?:\w+\s+){3}([^\n\r]+)$", "", fname) and vtype == 'a':
            fname = re.sub('^[^-]*- ', "", fname)

        fname = re.sub('[\\/:"\'*?<>|.,%#$+!`&{}@=]+', "", fname)

        rep = ' '

        if replace_type == 1:
            fname = re.sub(' ', "_", fname)
            rep = '_'

        fname = re.sub('\([^)]*\)', "", fname)

        if 'ft' in fname:
            fname = re.sub('ft[^|]*', "", fname)

        if fname[-1] == '_':
            fname = fname[:-1]
        if fname[0] == '_':
            fname = fname[0:]

        fname = fname.strip()
        fname = fname.strip('_')

        print('File name will be: ' + fname)

        titles.append(fname)

        if vtype == 'a':
            # Choose Stream
            chosenDownload = yt.streams.get_audio_only()
            print("Located highest bitrate audio stream for video: '" + vidtitle + "'. Downloading now...")

            # Download Vid
            chosenDownload.download(filename=fname + '.mp4', max_retries=5)
            print("Finished Download, converting to mp3 file")

            # Convert from mp4 to mp3
            video = AudioFileClip(fname + '.mp4')

            print("\033[1;31;40m")
            video.write_audiofile(fname + '.mp3')
            print("\033[1;34;40m")

            print("Finished Conversion")

            try:
                os.remove(fname + '.mp4')
            except PermissionError:
                print('MP4 file is being used by another process, retrying delete')
                time.sleep(10)
                os.remove(fname + '.mp4')

            print("Removed mp4")
        elif vtype == 'v':
            # Choose Stream
            chosenDownload = yt.streams.get_highest_resolution()
            print("Located highest resolution video stream for video: '" + vidtitle + "'. Downloading now...")

            # Download Vid
            chosenDownload.download(filename=fname + ".mp4", max_retries=5)
            print("Finished Download")
        elif vtype == 'h':
            fname = fname.strip('-')
            # Choose Stream - video
            chosenDownload = yt.streams.filter(only_video=True).first()
            print("Located highest resolution video stream for video: '" + vidtitle + "'. Downloading now...")
            print('Stream is: ' + str(chosenDownload))

            file_types = {
                'video/webm': '.webm',
                'video/mp4': '.mp4',
            }
            fending = file_types[chosenDownload.mime_type]

            # Download Vid
            chosenDownload.download(filename=fname+'v'+fending, max_retries=5)
            print("Finished Download")

            print("Downloading audio")

            # Choose Stream - audio
            chosenDownload = yt.streams.get_audio_only('webm')
            print("Located highest bitrate audio stream for video: '" + vidtitle + "'. Downloading now...")

            # Download audio
            chosenDownload.download(filename=fname + 'a.webm', max_retries=5)
            print("Finished Download, Combining files")

            input_video = ffmpeg.input(fname + 'v' + fending)
            merged_audio = ffmpeg.input(fname + 'a.webm')

            # Combine Files
            print("\033[1;37;40m")
            time.sleep(2)
            (
                ffmpeg
                .concat(input_video, merged_audio, v=1, a=1)
                .output(fname + ".mp4")
                .run(overwrite_output=True, cmd='ffmpeg.exe')
            )

            # Remove Extras
            os.remove(fname + 'v' + fending)
            os.remove(fname + 'a.webm')

        print("Link to thumbnail: \n" + yt.thumbnail_url)
    except (HTTPError, SyntaxError, OSError) as err:
        print("Error downloading: {}".format(err))
        total_errors[0] += 1
        total_errors.append(video)
    except URLError:
        print("Old connection was forcibly closed, to fix this, restart the program...")
        time.sleep(10)
        quit()

    print("\n")

def downPlaylist(vtype):

    print("\033[1;35;40m")

    playlist = Playlist(input("Link to Playlist: \n"))
    listName = playlist.title
    print("Accessing playlist: " + listName)

    listName = re.sub('[\\/:"\'*?<>|.,%#$+!`&{}@=]+', "", listName)

    try:
        os.mkdir(listName)
    except:
        print("Playlist already downloaded (folder already exists). Carrying on...")

    try:
        url = playlist.sidebar_info[0]['playlistSidebarPrimaryInfoRenderer']['thumbnailRenderer']['playlistCustomThumbnailRenderer']['thumbnail']['thumbnails'][2]['url']
    except KeyError:
        try:
            url = playlist.sidebar_info[0]['playlistSidebarPrimaryInfoRenderer']['thumbnailRenderer']['playlistVideoThumbnailRenderer']['thumbnail']['thumbnails'][2]['url']
        except KeyError:
            print("Cant find playlist thumbnail, are you sure you entered a link to a playlist?")
    print('Thumbnail url is:', url)

    get_thumb = (input("Would you like to download the playlist thumbnail?\n") or 'y')

    try:
        if get_thumb[0].lower() == 'y':
            lname = (listName + '.png')
            data = requests.get(url)
            with open(lname, 'wb')as file:
                file.write(data.content)
            os.rename(lname, listName + "/" + lname)
            print("Downloaded thumbnail")
    except:
        print("Thumbnail already downloaded")

    print("proceeding to download", str(playlist.length) + " files\n")
    for video in playlist:
        downloader(video, vtype)

    global titles, total_errors
    print("\n"*3 + "Finished Downloading. Total errors: ")
    print(total_errors)
    print("Moving to playlist folder now...")

    # Move files
    # Find file ending
    f_end = {
        'a': '.mp3',
        'v': '.mp4',
    }

    mov_errors = []
    for n in titles:
        try:
            os.rename(n + f_end[vtype], listName + "/" + n + ".mp3")
        except:
            print("Error Moving a File")
            mov_errors.append(n)

    print('All moving errors:')
    print(mov_errors)

    print("Finished")

def main():
    global replace_type

    print("\033[1;36;40m")

    # Find if user would like to download 1 file or many files using playlist
    dtype = (input("Would you like to download video's through YouTube playlists? [yes for playlist, no for single video, leave empty for spotify download]\n") or 's')

    replace_type_string = 'n'

    if dtype[0].lower() in ['y','n']:
        vidtype = (input("Would you like to download as a video or audio or high res video? [v or a or h]\n") or 'a')
        replace_type_string = (input('Lastly, would you like to replace spaces in the file name with "_" (underscores)?\n') or 'n')

    if replace_type_string[0].lower() == 'y':
        replace_type = 1

    if dtype[0].lower() == 'y':
        downPlaylist(vidtype[0].lower())
    elif dtype[0].lower() == 'n':
        vidurl = input("Enter link to youtube video\nlink: ")
        downloader(vidurl, vidtype[0].lower())
    else:
        link = input("Link: \n")
        try:
            command = ("spotdl " + link + " --ffmpeg ffmpeg.exe")
            os.system('cmd /c "' + command + '"')
            os.remove('.cache')
        except:
            print("Error getting spotify track")
            print("To use this feature you must have spotify downloader installed:\nhttps://github.com/spotDL/spotify-downloader")
            time.sleep(10)

print("\033[1;32;40m")
print("====================")
print("    By Rohan S.")
print("====================")
main()

x = 1
while x == 1:
    print("\033[1;31;40m")
    exit = (input("Type 'y' to exit, or just click enter to repeat...\n") or '  n')

    if exit[0].lower() == 'y':
        x = 0
    else:
        clear()
        time.sleep(1)
        main()


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

import sys
import time
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch_xla
import torch_xla.core.xla_model as xm
import torch_xla.debug.metrics as met
import torch_xla.distributed.parallel_loader as pl
import torch_xla.distributed.xla_multiprocessing as xmp
from torchvision import datasets, transforms, utils

class Cnn(nn.Module):
    def __init__(self):
        super(Cnn, self).__init__()
        self.relu = nn.ReLU()
        self.conv1 = nn.Conv2d(1, 32, kernel_size=5, padding=2)
        self.conv2 = nn.Conv2d(32, 64, kernel_size=5, padding=2)
        self.linear1 = nn.Linear(7 * 7 * 64, 256)
        self.linear2 = nn.Linear(256, 10)

    def forward(self, x):
        x = self.relu(self.conv1(x))
        x = F.max_pool2d(x, kernel_size=2, stride=2)
        x = self.relu(self.conv2(x))
        x = F.max_pool2d(x, kernel_size=2, stride=2)
        x = x.view([-1, 7 * 7 * 64])
        x = self.linear1(x)
        x = F.dropout(x, p=0.4, training=self.training)
        x = self.linear2(x)
        return x

class Trainer():
    def __init__(self, config):
        self.config = config

    def get_dataset(self):
        transform = transforms.Compose([
            transforms.Grayscale(),
            transforms.ToTensor(),
            transforms.Normalize((0.1307,), (0.3081,))
        ])

        train_dataset = datasets.MNIST(self.config.dataroot, train=True, download=True, transform=transform)
        test_dataset = datasets.MNIST(self.config.dataroot, train=False, download=True, transform=transform)

        return train_dataset, test_dataset

    def train(self):
        train_dataset, test_dataset = SERIAL_EXECUTOR.run(self.get_dataset)

        train_sampler = torch.utils.data.distributed.DistributedSampler(
            train_dataset,
            num_replicas=xm.xrt_world_size(),
            rank=xm.get_ordinal(),
            shuffle=True)

        train_loader = torch.utils.data.DataLoader(
            train_dataset,
            batch_size=self.config.batch_size,
            sampler=train_sampler,
            num_workers=self.config.n_workers,
            drop_last=True)

        test_loader = torch.utils.data.DataLoader(
            test_dataset,
            batch_size=self.config.batch_size,
            shuffle=False,
            num_workers=self.config.n_workers,
            drop_last=True)

        lr = self.config.lr * xm.xrt_world_size()

        device = xm.xla_device()
        model = WRAPPED_MODEL.to(device)
        optimizer = optim.SGD(model.parameters(), lr=lr)
        loss_fn = nn.CrossEntropyLoss()

        # if args.model != None:
        #     model.load_state_dict(torch.load(args.model, map_location=device_name), strict=False)

        start_time = time.time()

        def train_loop_fn(loader, config):
            tracker = xm.RateTracker()
            model.train()

            for batch_index, (data, target) in enumerate(loader):
                optimizer.zero_grad()
                output = model(data)
                loss = loss_fn(output, target)
                loss.backward()
                xm.optimizer_step(optimizer)

                tracker.add(config.batch_size)
                if batch_index % 100 == 0:
                    elapsed_time = time.time() - start_time
                    print("[xla:{}]({}) Elapsed: {:0.2f} sec, Loss: {:0.3f}, Rate: {:.2f}, GlobalRate: {:.2f}, AscTime: {}".format(
                        xm.get_ordinal(), batch_index, elapsed_time, loss.item(),
                        tracker.rate(), tracker.global_rate(), time.asctime()), flush=True)

        def test_loop_fn(loader):
            total_samples = 0
            correct = 0
            model.eval()
            data, pred, target = None, None, None
            for data, target in loader:
                output = model(data)
                pred = output.max(1, keepdim=True)[1]
                correct += pred.eq(target.view_as(pred)).sum().item()
                total_samples += data.size()[0]

            accuracy = 100.0 * correct / total_samples
            print('[xla:{}] Accuracy={:.2f}%'.format(
                xm.get_ordinal(), accuracy), flush=True)
            return accuracy, data, pred, target

        for epoch in range(1, args.epochs + 1):
            para_loader = pl.ParallelLoader(train_loader, [device])
            train_loop_fn(para_loader.per_device_loader(device), self.config)
            xm.master_print('Finished training epoch {}'.format(epoch))

            para_loader = pl.ParallelLoader(test_loader, [device])
            accuracy, data, pred, target = test_loop_fn(para_loader.per_device_loader(device))

            if self.config.metrics_debug:
                xm.master_print(met.metrics_report(), flush=True)

        return accuracy, data, pred, target

def mp_fn(rank, config):
    torch.set_default_tensor_type('torch.FloatTensor')
    trainer = Trainer(config)
    trainer.train()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(add_help=True)
    parser.add_argument('--epochs', type=int, default=10, help='epoch count')
    parser.add_argument('--cpu', action='store_true', help='use cpu')
    parser.add_argument('--dataroot', default='data')
    parser.add_argument('--model', help='model to load')
    parser.add_argument('--estimate', help='image file path to estimate class')
    parser.add_argument('--batch_size', type=int, default=32)
    parser.add_argument('--n_workers', type=int, default=4)
    parser.add_argument('--n_cores', type=int, default=8)
    parser.add_argument('--lr', type=float, default=0.01)
    parser.add_argument('--log_steps', type=int, default=20)
    parser.add_argument('--metrics_debug', action='store_true')
    args = parser.parse_args()
    print(args)

    SERIAL_EXECUTOR = xmp.MpSerialExecutor()
    WRAPPED_MODEL = xmp.MpModelWrapper(Cnn())

    xmp.spawn(mp_fn, args=(args,), nprocs=args.n_cores, start_method='fork')


import sys
from PyQt5.QtCore import QUrl, QEventLoop
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
from PyQt5.QtWebEngineWidgets import QWebEngineView

APP = QApplication(['', '--no-sandbox'])

class Widget(QWidget):
    def __init__(self, url):
        super().__init__()
        self.url = url
        self.initUI()

    def initUI(self):

        # set title
        self.setWindowTitle('Query')

        # create web view
        web_view = QWebEngineView()
        web_view.setUrl(QUrl(self.url))

        # create button
        button = QPushButton('Get Query', self)
        button.resize(button.sizeHint())
        button.clicked.connect(lambda: self.set_url(web_view))

        # create layout
        lay = QVBoxLayout(self)
        lay.addWidget(button)
        lay.addWidget(web_view)

    def set_url(self, web_view):
        self.url = web_view.url().toString()
        self.close()

    def get_url(self):
        return self.url

def webView(url):

    app = QApplication.instance()
    app.processEvents(QEventLoop.ExcludeUserInputEvents | QEventLoop.ExcludeSocketNotifiers | QEventLoop.WaitForMoreEvents)

    widget = Widget(url)
    widget.showMaximized()

    return_code = app.exec_()
    if return_code:
        print(f"Return code: {return_code}... exiting")
        exit(return_code)

    app.quit()

    return widget.get_url()


#!/usr/bin/python
# -*- coding: utf-8 -*-
# utf-8 中文编码
import sys
from collections import OrderedDict

class ExitWithError(Exception):
    pass

class MultipleWith:
    def __init__(self, *args, **kwargs):
        u"""
        可以接受的参数格式：

        # 可以保证顺序，前面的处于外侧，后面的处于内测。
        with multiplewith(open('/path1','wb'),open('/path2','wb')) as file_list:
            file_list[1].write(file_list[0].read())

        # 由于 python dict 限制，无法保证顺序。
        with multiplewith(file1=open('/path1','wb'),file2=open('/path2','wb')) as file_dict:
            file_dict['file1'].write(file_dict.file2.read())

        ordered_dict = OrderedDict((
                                    ('A', 1),
                                    ('B', 2),
                                    ('C', 3)
                                    ))
        # OrderedDict 的用法，可保证顺序。
        with multiplewith(ordered_dict.items()):
            file_dict['file1'].write(file_dict.file2.read())

        # 效果同上，可以保证顺序
        with multiplewith(('file1',open('/path1','wb')),('file2',open('/path2','wb'))):
            file_dict['file1'].write(file_dict.file2.read())
        """
        if args and not kwargs:
            if isinstance(args[0], (list, tuple)):
                kwargs = OrderedDict(args)
                args = None
            else:
                self._contexts = []
                for c in args:
                    self._contexts.append(c)
                return
        if not args and kwargs:
            if isinstance(kwargs, OrderedDict):
                self._contexts = kwargs
            else:
                self._contexts = OrderedDict(kwargs)
        else:
            raise ValueError()

    def __enter__(self):
        u""" 进入 with 块 """
        self.__exit_list = []
        exc_type, exc_val, exc_tb = None, None, None

        if isinstance(self._contexts, (list, tuple)):
            # list 类型
            res = []
            for c in self._contexts:
                try:
                    t = c.__enter__()
                except:
                    # 如果出现异常，那么反向调用 __exit__
                    exc_type, exc_value, exc_traceback = sys.exc_info()
                    self.__exit(exc_type, exc_value, exc_traceback, True)
                    # TODO: 正常 with嵌套这里应该是直接退出 with 块，并且异常被捕获并抛弃了。
                    # TODO: 但是目前没有想到什么办法可以不引发异常退出 with 块。
                    raise ExitWithError()

                self.__exit_list.append(c)
                res.append(t)
            return res

        elif isinstance(self._contexts, (dict, OrderedDict)):
            # dict
            res = {}
            for k, v in self._contexts.items():
                try:
                    t = v.__enter__()
                except:
                    exc_type, exc_value, exc_traceback = sys.exc_info()
                    self.__exit(exc_type, exc_value, exc_traceback, True)
                    # TODO: 正常 with嵌套这里应该是直接退出 with 块，并且异常被捕获并抛弃了。
                    # TODO: 但是目前没有想到什么办法可以不引发异常退出 with 块。
                    raise ExitWithError()

                self.__exit_list.append(v)
                res[k] = t
            return res

    def __exit__(self, exc_type, exc_val, exc_tb):
        return self.__exit(exc_type, exc_val, exc_tb, False)

    def __exit(self, exc_type, exc_val, exc_tb, is_raise):
        u""" 反向 exit

    is_raise 是否抛出异常(如果是 __exit__ 引发的异常，会强制抛出)
          """
        #  python 2 exit 抛出异常会被丢弃，python 3 会正常抛出with块。
        self.__exit_list.reverse()

        for _exit in self.__exit_list:
            res = False
            try:
                res = _exit.__exit__(exc_type, exc_val, exc_tb)
            except:
                is_raise = True
                _exc_type, _exc_val, _exc_traceback = sys.exc_info()
                _exc_val.__context__ = exc_val

                exc_type, exc_val, exc_tb = _exc_type, _exc_val, _exc_traceback
            if res:
                exc_type, exc_val, exc_tb = None, None, None

        if exc_val:
            if is_raise:
                raise exc_val
            else:
                return False
        else:
            return True


import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

datos= np.loadtxt("datos_CAMINATA.txt")

plt.hist(datos[0], bins=10, normed = True)
plt.savefig("binomial.png")
plt.close()

pasos=[]
i=0
while True:
	if i > (len(datos)-1):
		break
	else:
		pasos.append(np.sum(np.array(datos[i])))
		i+=1

#loc=media scale=desviacion
loc, scale = norm.fit(pasos)

x=np.linspace(3300, 3700, 100)
y=norm.pdf(x, loc, scale)
plt.hist(pasos, bins=100, normed= True)
plt.plot(x,y, color = "r")
plt.savefig("normal.png")
plt.close()
#De acuerdo con el teorema central miu_o*M = miu miu_0= miu / M

N= 10
M=1000
miu0= loc/M

#m miu= N*p
p=miu0/(N)

print("La probabilidad de sacar una cara con esta moneda es: %s " % p)


from data import puzzle

testInput = """$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k"""

def findDirUnder100k(log: str) -> int:
    splitLog = log.split("\n")
    directories = {}
    Pwd = []
    for line in splitLog:
        splitLine = line.split(" ")
        if line.startswith("$ cd"):
            directory = splitLine[2]
            if directory == "..":
                Pwd.pop() # remove the last thing appended to the current dir
            else:
                Pwd.append(directory)
                if str(Pwd) not in directories:
                    directories[str(Pwd)] = 0
        if splitLine[0].isdigit():
            # create new list, append one directory at a time,
            # update sizes of directories in dictionary
            tempList = []
            for directory in Pwd:
                tempList.append(directory)
                directories[str(tempList)] += int(splitLine[0])
    directoryTotals = list(directories.values())
    sumUnder100k = 0
    for value in directoryTotals:
        if value <= 100000:
            sumUnder100k += value
    return sumUnder100k

def findDirToDelete(log: str) -> int:
    splitLog = log.split("\n")
    directories = {}
    Pwd = []
    for line in splitLog:
        splitLine = line.split(" ")
        if line.startswith("$ cd"):
            directory = splitLine[2]
            if directory == "..":
                Pwd.pop() # remove the last thing appended to the current dir
            else:
                Pwd.append(directory)
                if str(Pwd) not in directories:
                    directories[str(Pwd)] = 0
        if splitLine[0].isdigit():
            # create new list, append one directory at a time,
            # update sizes of directories in dictionary
            tempList = []
            for directory in Pwd:
                tempList.append(directory)
                directories[str(tempList)] += int(splitLine[0])

    directoryTotals = list(directories.values())

    TOTAL_STORAGE_AVAILABLE = 70000000
    SPACE_REQUIRED_FOR_UPDATE = 30000000
    totalSpaceUsed = directories["['/']"]
    unusedSpace = TOTAL_STORAGE_AVAILABLE - totalSpaceUsed
    spaceNeeded = SPACE_REQUIRED_FOR_UPDATE - unusedSpace
    print(f'total space used: {totalSpaceUsed}; space available {unusedSpace}\nspace needed for update: {spaceNeeded}')
    currentDelDirSize = TOTAL_STORAGE_AVAILABLE
    for value in directoryTotals:
        if value >= spaceNeeded and value < currentDelDirSize:
            currentDelDirSize = value
    return currentDelDirSize

# print(findDirUnder100k(testInput))
print(findDirToDelete(puzzle))