import time
import Queue
import socket
import threading
import logging
from wireprotocol import WireProtocol, load_packet

logger = logging.getLogger(__name__)

class Peer(threading.Thread):

    def __init__(self, wire, connection, ip, port=None):
        threading.Thread.__init__(self)
        self.wire = wire
        self._stopped = False
        self.lock = threading.Lock()
        self._connection = connection

        assert ip.count('.') == 3
        self.ip = ip
        # None if peer was created in response to external connect
        self.port = port
        self.node_id = ''
        self.response_queue = Queue.Queue()
        self.hello_received = False
        self.hello_sent = False
        self.last_valid_packet_received = time.time()
        self.last_asked_for_peers = 0
        self.last_pinged = 0

    def __repr__(self):
        return "<Peer(%s:%r)>" % (self.ip, self.port)

    def id(self):
        return hash(repr(self))

    def connection(self):
        if self.stopped():
            raise IOError("Connection was stopped")
        else:
            return self._connection

    def stop(self):
        logger.debug('disconnected: {0}'.format(repr(self)))
        with self.lock:
            if self._stopped:
                return
            self._stopped = True

        # shut down
        try:
            self._connection.shutdown(socket.SHUT_RDWR)
        except IOError as e:
            logger.debug(
                "shutting down failed {0} \"{1}\"".format(repr(self), str(e)))
        self._connection.close()

    def stopped(self):
        with self.lock:
            return self._stopped

    def send_packet(self, response):
        self.response_queue.put(response)

    def _process_send(self):
        '''
        :return: size of processed data
        '''
        # send packet
        try:
            packet = self.response_queue.get(timeout=.1)
        except Queue.Empty:
            packet = ''

        while packet:
            logger.debug('{0}: send packet {1}'.format(
                repr(self), str(load_packet(packet))[:60]))
            try:
                n = self.connection().send(packet)
                packet = packet[n:]
            except IOError as e:
                logger.debug(
                    '{0}: send packet failed, {1}'
                    .format(repr(self), str(e)))
                self.stop()
                break
        if packet:
            return len(packet)
        else:
            return 0

    def _process_recv(self):
        '''
        :return: size of processed data
        '''
        packet = ""
        while True:
            try:
                chunk = self.connection().recv(2048)
            except IOError:
                chunk = ''
            if not chunk:
                break
            packet += chunk

        if packet:
            logger.debug('{0}: received packet {1}'.format(
                repr(self), str(load_packet(packet))[:60]))
            self.wire.recv_packet(self, packet)
        return len(packet)

    def run(self):
        while not self.stopped():
            send_size = self._process_send()
            recv_size = self._process_recv()
            # pause
            if not (send_size or recv_size):
                time.sleep(0.1)

class PeerManager(threading.Thread):

    max_silence = 5  # how long before pinging a peer
    max_ping_wait = 1.  # how long to wait before disconenctiong after ping
    max_ask_for_peers_elapsed = 30  # how long before asking for peers

    def __init__(self, config):
        threading.Thread.__init__(self)
        self.config = config
        self.connected_peers = set()
        self._seen_peers = set()  # (host, port, node_id)
        self._stopped = False
        self.local_address = ()  # host, port
        self.lock = threading.Lock()
        self.wire = WireProtocol(self, config)

    def get_peer_by_id(self, peer_id):
        for peer in self.connected_peers:
            if peer_id == peer.id():
                return peer
        return None

    def add_peer_address(self, ip, port, node_id):
        ipn = (ip, port, node_id)
        with self.lock:
            if ipn not in self._seen_peers:
                self._seen_peers.add(ipn)

    def get_known_peer_addresses(self):
        # fixme add self
        return set(self._seen_peers).union(self.get_connected_peer_addresses())

    def get_connected_peer_addresses(self):
        "get peers, we connected and have a port"
        return set((p.ip, p.port, p.node_id) for p in self.connected_peers
                   if p.port)

    def stop(self):
        with self.lock:
            if not self._stopped:
                for peer in self.connected_peers:
                    peer.stop()
            self._stopped = True

    def stopped(self):
        with self.lock:
            return self._stopped

    def add_peer(self, connection, host):
        # FIXME: should check existance first
        peer = Peer(self.wire, connection, host)
        peer.start()
        with self.lock:
            self.connected_peers.add(peer)
        return peer

    def remove_peer(self, peer):
        peer.stop()
        with self.lock:
            self.connected_peers.remove(peer)
        # connect new peers if there are no candidates

    def connect_peer(self, host, port):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.settimeout(1)
        logger.debug('connecting {0}:{1}'.format(host, port))
        try:
            sock.connect((host, port))
        except Exception as e:
            logger.debug(
                'Conencting {0}:{1} failed, {2}'.format(host, port, str(e)))
            return False
        sock.settimeout(.1)
        ip, port = sock.getpeername()
        logger.debug('connected {0}:{1}'.format(ip, port))
        peer = self.add_peer(self.wire, sock, ip, port)

        # Send Hello
        peer.wire.send_Hello(peer)
        peer.wire.send_GetPeers(peer)
        return True

    def _peer_candidates(self):
        candidates = self.get_known_peer_addresses().difference(
            self.get_connected_peer_addresses())
        candidates = [
            ipn for ipn in candidates if not ipn[:2] == self.local_address]
        return candidates

    def _poll_more_candidates(self):
        for peer in list(self.connected_peers):
            with peer.lock:
                peer.wire.send_GetPeers(peer)

    def _connect_peers(self):
        num_peers = self.config.getint('network', 'num_peers')
        candidates = self._peer_candidates()
        if len(self.connected_peers) < num_peers:
            logger.debug('not enough peers: {0}'.format(
                len(self.connected_peers)))
            logger.debug('num candidates: {0}'.format(len(candidates)))
            if len(candidates):
                ip, port, node_id = candidates.pop()
                self.connect_peer(ip, port)
                # don't use this node again in case of connect error > remove
                self._seen_peers.remove((ip, port, node_id))
            else:
                self._poll_more_candidates()

    def _check_alive(self, peer):
        now = time.time()
        dt_ping = now - peer.last_pinged
        dt_seen = now - peer.last_valid_packet_received
        # if ping was sent and not returned within last second
        if dt_ping < dt_seen and dt_ping > self.max_ping_wait:
            logger.debug(
                '{0} last ping: {1} last seen: {2}'
                .format(peer, dt_ping, dt_seen))
            logger.debug(
                '{0} did not respond to ping, disconnecting {1}:{2}'
                .format(peer, peer.ip, peer.port))
            self.remove_peer(peer)
        elif min(dt_seen, dt_ping) > self.max_silence:
            # ping silent peer
            logger.debug('pinging silent peer {0}'.format(peer))

            with peer.lock:
                peer.wire.send_Ping(peer)
                peer.last_pinged = now

    def manage_connections(self):
        self._connect_peers()

        for peer in list(self.connected_peers):
            if peer.stopped():
                self.remove_peer(peer)
                continue

            self._check_alive(peer)

            now = time.time()

            # ask for peers
            if now - peer.last_asked_for_peers >\
                    self.max_ask_for_peers_elapsed:
                with peer.lock:
                    peer.wire.send_GetPeers(peer)
                    peer.last_asked_for_peers = now

    def run(self):
        while not self.stopped():
            self.manage_connections()
            self.wire.send_chain_out_cmd()
            time.sleep(0.1)

class TcpServer(threading.Thread):

    def __init__(self, peer_manager, host, port):
        self.peer_manager = peer_manager
        threading.Thread.__init__(self)
        self.daemon = True
        self.host = host
        self.port = port
        self.lock = threading.Lock()

        # start server
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((self.host, self.port))
        sock.listen(5)
        self.sock = sock
        self.ip, self.port = sock.getsockname()
        logger.info("TCP server started {0}:{1}".format(self.ip, self.port))

    def run(self):
        import sys
        import traceback
        while not self.peer_manager.stopped():
            logger.debug('in run loop')
            try:
                connection, (host, port) = self.sock.accept()
            except IOError as e:
                traceback.print_exc(file=sys.stdout)
                time.sleep(0.1)
                continue

            connection.settimeout(.1)
            try:
                self.peer_manager.add_peer(connection, host)
                logger.debug(
                    "new TCP connection {0} {1}:{2}"
                    .format(connection, host, port))
            except BaseException as e:
                logger.error(
                    "cannot start TCP session \"{0}\" {1}:{2} "
                    .format(str(e), host, port))
                traceback.print_exc(file=sys.stdout)
                connection.close()
                time.sleep(0.1)


# High scores

scores = []

choice = None

while choice != "0" :
	print(
	"""
	High Scores

	0 - Exit
	1 - Show Scores
	2 - Add a Score
	3 - Delete a Score
	4 - Sort Scores
	"""
	)
	choice = input("Choice:")
	print()
	# Exit
	if choice=="0":
		print("gtfo fgt.")
	# List high-score table
	elif choice == "1":
		print("High Scores")
		for score in scores:
			print(score)
	# Add a score
	elif choice=="2":
		score = int(input("What score did u get, fgt?: "))
		scores.append(score)
	# Remove a score
	elif choice=="3":
		score=int(input("Remove which score, fgt?: "))
		if score in scores:
			scores.remove(score)
		else:
			print(score, "isnt in the high scores list, u fgt.")
	# Sort scores
	elif choice == "4":
		scores.sort(reverse=True)
	# fgt tryin to be smart
	else:
		print("Fgt, ", choice, "isn't a valid choice.")

	input("\n\nPress Enter to exit.")


##############################################################################
#
# Copyright (c) 2004 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Processor for inclusions when building a release.

:Variables:
  - `PACKAGE_CONF`: The name of the file that specifies how the
    package is assembled.

"""

import glob
import os
import posixpath
import shutil
import urllib

from zpkgsetup import cfgparser
from zpkgsetup import loggingapi as logging
from zpkgsetup import package
from zpkgsetup import publication
from zpkgsetup import setup
from zpkgsetup import urlutils

from zpkgtools import Error
from zpkgtools import loader

logger = logging.getLogger(__name__)

# Name of the configuration file:
PACKAGE_CONF = "PACKAGE.cfg"

get_schema = cfgparser.cachedSchemaLoader("include.xml")

def collection_path_ref(value):
    if value == "-":
        return value
    if value:
        return normalize_path(value, "destination")
    else:
        return None

def distribution_path_ref(value):
    if value == "-":
        raise ValueError("exclusion not allowed in <distribution>")
    if value:
        return normalize_path(value, "destination")
    else:
        return None

def load_path_ref(value):
    if value == "-":
        raise ValueError("exclusion not allowed in <load>")
    if not value:
        raise ValueError("source must be specified in <load>")
    return normalize_path_or_url(value, "source")

def workspace_path_ref(value):
    return normalize_path(value, "workspace file")

def collection_section(section):
    excludes = []
    includes = {}
    for (k, vs) in section.mapping.iteritems():
        for v in vs:
            if v == "-":
                if len(vs) != 1:
                    raise ValueError("too many exclusions specified for %s"
                                     % k)
                excludes.append(k)
                continue
            if v:
                includes[v] = k
            else:
                L = includes.setdefault(None, [])
                L.append(k)
    if excludes and includes:
        raise ValueError(
            "includes and excludes cannot be mixed in <collection>")
    section.excludes = excludes
    section.includes = includes
    return section

def distribution_section(section):
    includes = {}
    for (k, v) in section.mapping.iteritems():
        if v:
            includes[v] = k
        else:
            L = includes.setdefault(None, [])
            L.append(k)
    section.excludes = {}
    section.includes = includes
    return section

def load_section(section):
    includes = {}
    for (k, v) in section.mapping.iteritems():
        if v:
            includes[v] = k
        else:
            L = includes.setdefault(None, [])
            L.append(k)
    section.excludes = {}
    section.includes = includes
    return section

class InclusionError(Error):
    """Raised to indicate errors processing inclusions."""

class InclusionSpecificationError(cfgparser.ConfigurationError,
                                  InclusionError):
    """Raised to indicate errors in an inclusion specification."""

    def __init__(self, message, filename=None, lineno=None):
        InclusionError.__init__(self, message)
        cfgparser.ConfigurationError.__init__(self, message)
        self.filename = filename
        self.lineno = lineno

    def __str__(self):
        s = InclusionError.__str__(self)
        if self.filename:
            s = "%s\n(in %s" % (s, self.filename)
            if self.lineno is not None:
                s = "%s, line %d" % (s, self.lineno)
            s += ")"
        return s

def load(sourcedir):
    """Return the specifications for populating the distribution and
    collection directories.

    :param sourcedir: Directory we're loading the specifications for.

    If there is not specification file, return empty specifications.
    """
    package_conf = os.path.join(sourcedir, PACKAGE_CONF)
    url = urlutils.file_url(os.path.abspath(package_conf))
    if os.path.isfile(package_conf):
        cf, _ = cfgparser.loadConfig(get_schema(), package_conf)
        config = PackageConstruction(sourcedir, package_conf)
        if cf.collection is not None:
            config.collection.excludes = cf.collection.excludes
            config.collection.includes = cf.collection.includes
        if cf.distribution is not None:
            config.distribution.includes = cf.distribution.includes
        if cf.loads is not None:
            config.loads.includes = cf.loads.includes

        # Now that the configuration object is populated and we know
        # that the PACKAGE_CONF file exists, make sure it isn't copied
        # if there's nothing specified in the <collection> section.
        #
        if config.collection.excludes:
            # XXX should make sure PACKAGE_CONF isn't already excluded
            config.collection.excludes.append(PACKAGE_CONF)
        elif not config.collection.includes:
            # Nothing included or excluded; simply exclude PACKAGE_CONF:
            config.collection.excludes.append(PACKAGE_CONF)
    else:
        config = PackageConstruction(sourcedir, None)
    return config

class PackageConstruction(object):

    def __init__(self, source, filename):
        self.loads = Specification(source, filename, "load")
        self.collection = Specification(source, filename, "collection")
        self.distribution = Specification(source, filename, "distribution")

def normalize_path(path, type):
    if ":" in path:
        scheme, rest = urllib.splittype(path)
        if len(scheme) == 1:
            # looks like a drive letter for Windows; scream,
            # 'cause that's not allowable:
            raise InclusionSpecificationError(
                "drive letters are not allowed in inclusions: %r" % path)
        else:
            raise InclusionSpecificationError(
                "URLs are not allowed in inclusions")
    np = posixpath.normpath(path)
    if posixpath.isabs(np) or np.split("/", 1)[0] == "..":
        raise InclusionSpecificationError(
            "%s path must not be absolute or refer to a location"
            " not contained in the source directory"
            % path)
    if np == ".":
        return os.curdir
    else:
        return np.replace("/", os.sep)

def normalize_path_or_url(path, type):
    if ":" in path:
        scheme, rest = urllib.splittype(path)
        if len(scheme) != 1:
            # should normalize the URL, but skip that for now
            return path
    return normalize_path(path, type)

class Specification:
    """Specification for files to include.

    :Ivariables:
      - `includes`: Mapping from relative path (relative to the
        source) to either the destination path (relative) or an empty
        string.

      - `excludes`: List of relative paths (relative to the source)
        which should *not* be copied along with the included files.

      - `source`: Source directory which will be used to expand glob
        patterns.

      - `filename`: Path of the file from which this specification was
        loaded.  This is used when reporting errors.

      - `group`: String indicating which section of the specification
        file was used to generate this specification.

    """

    def __init__(self, source, filename, group):
        """Initialize the Specification object.

        :Parameters:
          - `source`: Directory that will serve as the primary source
            directory; this is needed to support filename globbing.

          - `filename`: Path of the file from which this specification
            was loaded.  This is used when reporting errors.

          - `group`: String indicating which section of the
            specification file was used to generate this
            specification.

        """
        # The source directory is needed since globbing is performed
        # to locate files if the spec includes wildcards.
        self.includes = {}
        self.excludes = []
        self.source = source
        self.filename = filename
        self.group = group

    def __nonzero__(self):
        return bool(self.includes or self.excludes)

    def cook(self):
        patterns = self.includes.pop(None, [])
        source = os.path.normpath(self.source)
        prefix = os.path.join(source, "")
        for pat in patterns:
            path = os.path.join(source, pat)
            expansions = setup.filter_names(glob.glob(path))
            if not expansions:
                raise InclusionSpecificationError(
                    "%r doesn't match any files in <%s>" % (pat, self.group),
                    self.filename)
            for fn in expansions:
                suffix = fn[len(prefix):].replace(os.sep, "/") # POSIX form
                self.includes[suffix] = suffix
        excludes = []
        for pat in self.excludes:
            path = os.path.join(source, pat)
            expansions = setup.filter_names(glob.glob(path))
            if not expansions:
                raise InclusionSpecificationError(
                    "%r doesn't match any files in <%s>" % (pat, self.group),
                    self.filename)
            for fn in expansions:
                suffix = fn[len(prefix):].replace(os.sep, "/") # POSIX form
                if suffix not in excludes:
                    excludes.append(suffix)
        self.excludes[:] = excludes

class InclusionProcessor:
    """Handler for processing inclusion specifications.

    Methods are provided for managing manifest lists and creating
    the output tree.

    """
    def __init__(self, loader):
        """Initialize the processor.

        :param loader: Resource loader which should be used to load
          external resources.

        """
        self.manifests = []
        self.loader = loader

    def createDistributionTree(self, destination, spec):
        """Create the output tree according to `spec`.

        :Parameters:
          - `destination`: Path of the top-level output directory.
            This directory will be created if it doesn't exist.

          - `spec`: ``Specification`` object that describes what to
            include.  If omitted, an empty specification is used.

        """
        destination = os.path.abspath(destination)
        if spec.includes:
            self.create_directory(spec.source, destination)
            self.addIncludes(destination, spec)
        else:
            self.copyTree(spec.source, destination, spec.excludes,
                          nested=False)

    def copyTree(self, source, destination, excludes=(), nested=True):
        """Populate the destination tree from the source tree.

        :Parameters:
          - `source`: Absolute path to a directory to copy into the
            destination tree.

          - `destination`: Absolute path to a directory that
            corresponds to the `source` tree.  It will be created if
            it doesn't exist.

          - `excludes`: Paths relative to source which should be
            excluded from the copy operation.

        Files and directories will be created with the same permission
        bits and stat info as the source tree.
        """
        # The `nested` flag is only for internal use; it should be
        # False only when calling copyTree based on a specification
        # object represented by a PACKAGE_CONF file located in the
        # directory identified by `source`.  For any other call,
        # `nested` should be True since PACKAGE_CONF files need to be
        # processed.

        self.create_directory(source, destination)
        prefix = os.path.join(source, "")

        # The directory walk *must* be performed in top-down fashion
        # since the handling of nested PACKAGE_CONF files must be able
        # to modify the list of directories which are considered.
        #
        for dirname, dirs, files in os.walk(source, topdown=True):
            dirs[:] = setup.filter_names(dirs)
            files = setup.filter_names(files)

            # reldir is the name of the directory to write to,
            # relative to destination.  It will be '' at the top
            # level.
            reldir = dirname[len(prefix):]
            if reldir:
                destdir = os.path.join(destination, reldir)
            else:
                destdir = destination

            if PACKAGE_CONF in files and ((dirname != source) or nested):
                # a nested PACKAGE.cfg needs to be handled as well
                specs = load(dirname)
                if specs.loads:
                    raise InclusionSpecificationError(
                        "<load> disallowed in a nested %s file"
                        % PACKAGE_CONF)
                if specs.collection:
                    specs.collection.cook()
                    self.createDistributionTree(destdir, specs.collection)
                    # Don't recurse into any directories here; those
                    # were handled by the inner call to
                    # self.createDistributionTree().
                    del dirs[:]
                    continue

            if excludes:
                # excludes are in POSIX path notation
                preldir = reldir.replace(os.sep, "/")
                for name in dirs[:]:
                    prelpath = posixpath.join(preldir, name)
                    if prelpath in excludes:
                        dirs.remove(name)
                for name in files[:]:
                    prelpath = posixpath.join(preldir, name)
                    if prelpath in excludes:
                        files.remove(name)

            for file in files:
                srcname = os.path.join(dirname, file)
                destname = os.path.join(destdir, file)
                # Copy file data, permission bits, and stat info;
                # owner/group are not copied.
                self.copy_file(srcname, destname)

            for dir in dirs[:]:
                srcname = os.path.join(dirname, dir)
                destname = os.path.join(destdir, dir)
                if publication.PUBLICATION_CONF in os.listdir(srcname):
                    dirs.remove(dir)
                    continue
                self.create_directory(srcname, destname)

    def create_directory(self, source, destination):
        if not os.path.exists(destination):
            # Create the directory, copying over the permission
            # bits and stat info.
            os.mkdir(destination)
            shutil.copymode(source, destination)

    def copy_file(self, source, destination):
        """Copy a single file into the output tree."""
        shutil.copy2(source, destination)
        self.add_output(destination)

    def add_output(self, path):
        """Add `path` to each of the relevant manifests."""
        for prefix, manifest in self.manifests:
            if path.startswith(prefix):
                relpath = path[len(prefix):]
                parts = relpath.split(os.sep)
                if len(parts) == 1:
                    manifest.append(parts[0])
                else:
                    manifest.append(posixpath.join(*parts))

    # This pair of methods handles the creation and removal of
    # manifest lists.  We use this approach since we need to support
    # multiple manifests for collection distributions (each component
    # will have a manifest of it's own, as well as the package as a
    # whole).  This makes managing manifests a function of the client
    # rather than being implicit.

    def add_manifest(self, destination):
        prefix = os.path.join(destination, "")
        self.manifests.append((prefix, []))

    def drop_manifest(self, destination):
        prefix = os.path.join(destination, "")
        for i in range(len(self.manifests)):
            if self.manifests[i][0] == prefix:
                return self.manifests.pop(i)[1]
        raise ValueError("no manifest for %s" % destination)

    def addIncludes(self, destination, spec):
        """Process all the inclusion from a specification."""
        logger.debug("processing <%s> from %s (dest = %s)",
                     spec.group, spec.filename, destination)
        for source, relpath in spec.includes.iteritems():
            self.addSingleInclude(relpath, source, destination, spec.source)

    def addSingleInclude(self, relpath, source, destination, dir):
        """Process a single include specification line.

        :Parameters:
          - `relpath`: Path relative to the destination directory, as
            taken from the specification.  This should use the path
            notation of the host operating system.

          - `source`: Path or URL to the input file or directory.

          - `destination`: Top-level destination directory; this is
            used as a base directory for `relpath`.

        """
        logger.debug("adding include %s from %s (dest = %s)",
                     relpath, source, destination)
        dirname, basename = os.path.split(relpath)
        if dirname:
            destdir = os.path.join(destination, dirname)
            if not os.path.exists(destdir):
                os.makedirs(destdir)
        else:
            # Known to exist, so no need to create it.
            destdir = destination

        # This is what we want to create:
        destdir = os.path.join(destdir, basename)

        type = urllib.splittype(source)[0] or ''
        if len(type) in (0, 1):
            # figure it's a path ref, possibly w/ a Windows drive letter
            source = urlutils.file_url(os.path.join(dir, source))
            type = "file"
        try:
            path = self.loader.load(source)
        except ValueError:
            # not a supported URL type
            raise InclusionError("cannot load from a %r URL" % type)
        self.includeFromLocalTree(path, destdir)

    def includeFromLocalTree(self, source, destination):
        # Check for file-ness here since copyTree() doesn't handle
        # individual files at all.
        if os.path.isfile(source):
            self.copy_file(source, destination)
        else:
            self.copyTree(source, destination)


from cloudshell.api.cloudshell_api import InputNameValue
from cloudshell.api.common_cloudshell_api import CloudShellAPIError
from remap_child_resources_constants import MISSING_COMMAND_ERROR
import cPickle
import traceback

TARGET_TYPE_RESOURCE = 'Resource'
PRE_AUTOLOAD_CONFIGURATION_STEPS = 'pre_autoload_configuration_command'
PYTHON_DRIVER_CONFIGURE = 'configure_device_command'
RESOURCE_COMMAND_CONFIGURE_FAMILIES = ['Virtual Traffic Generator Chassis']
NO_DRIVER_ERR = "129"
DRIVER_FUNCTION_ERROR = "151"

def pre_autoload_configuration_step(api, deployed_app, sandbox_id):
        # for devices that have a pre-autoload configuration step run it
        # example: add vnics to a vm before running autoload
        _run_configuration_step(api, deployed_app, sandbox_id, PRE_AUTOLOAD_CONFIGURATION_STEPS)

def driver_command_configure_step(api, deployed_app, sandbox_id, param_args):
    # for devices that need to run configuration after all other steps have finished
    _run_configuration_step(api, deployed_app, sandbox_id, PYTHON_DRIVER_CONFIGURE, param_args)

def _run_configuration_step(api, deployed_app, sandbox_id, configuration_step_command_name, param_args=[]):
        deployed_app_name = deployed_app.AppDeploymentyInfo.LogicalResourceName
        api.ExecuteCommand(sandbox_id, deployed_app_name,
                           TARGET_TYPE_RESOURCE,
                           configuration_step_command_name, param_args)

def call_resource_command_configure(api, deploy_result, resource_details_cache, logger, sandbox_id):
        message_written = False

        resource_cache_pickle_string = cPickle.dumps(resource_details_cache)
        # api.WriteMessageToReservationOutput(sandbox_id, resource_cache_pickle_string)
        configure_params = [InputNameValue('resource_cache', resource_cache_pickle_string)]

        for deployed_app in deploy_result.ResultItems:
            if not deployed_app.Success:
                continue
            deployed_app_name = deployed_app.AppDeploymentyInfo.LogicalResourceName
            app_resource_details = resource_details_cache[deployed_app_name]

            if app_resource_details.ResourceFamilyName not in RESOURCE_COMMAND_CONFIGURE_FAMILIES:
                continue

            try:
                logger.info("Called configure resource command on deployed app {0}".format(deployed_app_name))
                if not message_written:
                    api.WriteMessageToReservationOutput(sandbox_id, 'Calling configure command on device...')
                    message_written = True

                driver_command_configure_step(api, deployed_app, sandbox_id, configure_params)

            except CloudShellAPIError as exc:
                if exc.code not in (NO_DRIVER_ERR,
                                    DRIVER_FUNCTION_ERROR,
                                    MISSING_COMMAND_ERROR):
                    logger.error(
                        "Error executing Configure Resource command on deployed app {0}. Error: {1}".format(
                            deployed_app_name,
                            exc.rawxml))
                    api.WriteMessageToReservationOutput(sandbox_id, 'Configuration failed on "{0}": {1}'
                                                        .format(deployed_app_name, exc.message))

            except Exception as exc:
                logger.error("Error executing Configure Resource command on deployed app {0}. Error: {1}"
                             .format(deployed_app_name, str(exc)))
                api.WriteMessageToReservationOutput(sandbox_id, 'Discovery failed on "{0}": {1}\n{2}'
                                                    .format(deployed_app_name, exc.message, traceback.print_exc()))


""" Python class to represent the sources within an ingest layer """

class Source(object):
    def __init__(self, uri, extent, band_maps, cell_size,
                  extent_crs, source_crs=None):

        """
            Create a new ingest Source

            Args:
                uri (str): location of image
                extent (List[float]): extent of the source
                starting_target_band (int): index of first target band
                band_maps (List[dict]): list of mappings for each band within the image
                extent_crs (str): CRS of the extent
                crs (str): Optional CRS of the Source
        """
        self.uri = uri
        self.cell_size = cell_size
        self.extent = extent
        self.band_maps = band_maps
        self.source_crs = source_crs
        self.extent_crs = extent_crs

    @classmethod
    def from_dict(cls, d):
        return cls(
            d.get('image'),
            d.get('extent'),
            d.get('starting_target_band'),
            d.get('band_maps'),
            d.get('source_crs'),
            d.get('extent_crs')
        )

    def to_dict(self):
        d = {
            'crs': self.source_crs,
            'extentCrs': self.extent_crs,
            'cellSize': self.cell_size,
            'uri': self.uri,
            'extent': self.extent,
            'bandMaps': self.band_maps
        }

        if self.source_crs:
            d['crs'] = self.source_crs

        return d


import random, time

#definitions:
deck = {"Ace of Spades": 11, "Two of Spades": 2, "Three of Spades": 3, "Four of Spades": 4, "Five of Spades": 5, "Six of Spades": 6, "Seven of Spades": 7, "Eight of Spades": 8, "Nine of Spades": 9, "Ten of Spades":10, "Jack of Spades": 10, "Queen of Spades": 10, "King of Spades": 10, "Ace of Clubs": 11, "Two of Clubs": 2, "Three of Clubs": 3, "Four of Clubs": 4, "Five of Clubs": 5, "Six of Clubs": 6, "Seven of Clubs": 7, "Eight of Clubs": 8,
"Nine of Clubs": 9, "Ten of Clubs": 10, "Jack of Clubs": 10, "Queen of Clubs": 10, "King of Clubs": 10, "Ace of Diamonds": 11, "Two of Diamonds": 2, "Three of Diamonds": 3, "Four of Diamonds": 4, "Five of Diamonds": 5, "Six of Diamonds": 6, "Seven of Diamonds": 7, "Eight of Diamonds": 8, "Nine of Diamonds": 9, "Ten of Diamonds": 10, "Jack of Diamonds": 10, "Queen of Diamonds": 10, "King of Diamods": 10, "Ace of Hearts": 11, "Two of Hearts": 2, "Three of Hearts": 3,
"Four of Hearts": 4, "Five of Hearts": 5, "Six of Hearts": 6, "Seven of Hearts": 7, "Eight of Hearts": 8, "Nine of Hearts": 9, "Ten of Hearts": 10, "Jack of Hearts": 10, "Queen of Hearts": 10, "King of Hearts": 10}

while True:

    croupier = []
    croupier_value = 0
    player = []
    player_value = 0
    cards = deck
    aces = ["Ace of Spades", "Ace of Clubs", "Ace of Diamonds", "Ace of Hearts"]

    def first_deal():
        global croupier, croupier_value, player, player_value, cards, croupier_old_value
        for i in (0, 1):
            card, card_value = random.choice(list(cards.items()))
            croupier.append(card)
            croupier_value += card_value
            del cards[card]

            card, card_value = random.choice(list(cards.items()))
            player.append(card)
            player_value += card_value
            del cards[card]

        if player_value > 21:
            test = [card for card in player if card in aces]
            if len(test) > 0: #check for aces in hand
                str_ace = "".join(test[0])
                player[player.index(str_ace)] = str_ace + "(1)"
                player_value -= 10

        if croupier_value > 21:
            test = [card for card in croupier if card in aces]
            if len(test) > 0: #check for aces in hand
                str_ace = "".join(test[0])
                croupier[croupier.index(str_ace)] = str_ace + "(1)"
                croupier_value -= 10 #ace changes value to 1

        croupier_old_value = croupier_value
        print(f"Croupier has: {croupier}. {croupier_value} points.\n")
        print(f"You have: {player}. {player_value} points.\n")

    def check_player_for_blackjack():
        if (player_value == 21) and (len(player) == 2):
            return True
        else: return False

    def check_player(player_val):
        global player, player_value
        if player_val > 21:
            test = [card for card in player if card in aces]
            if len(test) > 0: #check for aces in hand
                str_ace = "".join(test[0])
                player[player.index(str_ace)] = str_ace + "(1)"
                player_value -= 10
                return True
            else:
                return False
        else:
            return True

    def check_croupier(croupier_val):
        global croupier, croupier_value
        if croupier_val > 21:
            test = [card for card in croupier if card in aces]
            if len(test) > 0: #check for aces in hand
                str_ace = "".join(test[0])
                croupier[croupier.index(str_ace)] = str_ace + "(1)"
                croupier_value -= 10 #ace changes value to 1
                return True
            else:
                return False
        else:
            return True

    def check_game(player_val, croupier_val):
        print(f"\nYou have {player_value} points.")
        print(f"Croupier has {croupier_value} points.\n")
        time.sleep(1)
        if ((player_val > croupier_val and player_val <= 21) or (player_val <= 21 and croupier_val > 21)) and blackjack == True:
            print("Congratulation! You got a BLACKJACK! You won!")
        elif (player_val > croupier_val and player_val <= 21) or (player_val <= 21 and croupier_val > 21):
            print("Congratulation! You won!")
        elif (croupier_val < player_val and croupier_val <= 21) or (croupier_val <= 21 and player_val > 21):
            print("Croupier won. You lost.")
        else:
            print("It's a tie!")

    def player_choose():
        print(f"Your cards are: {player}. You have {player_value} points.")
        decision = input("If you want to DRAW, enter 'd' \nIf you want to STAND, enter 's'\n")
        while decision != "d" and decision != "s":
            print("If you want to DRAW, enter 'd' \nIf you want to STAND, enter 's'\n")
            decision = input()
        if decision == "d":
            print("\nYou draw.")
            return True
        else:
            print("\nYou stand.")
            return False

    def croupier_choose(croupier_val):
        global croupier_value, card, cards
        if croupier_val < 17:
            print("\nCroupier draws.\n")
            card, card_value = random.choice(list(cards.items()))
            croupier.append(card)
            croupier_value += card_value
            if croupier_value > 21:
                test = [card for card in croupier if card in aces]
                if len(test) > 0: #check for aces in hand
                    str_ace = "".join(test[0])
                    croupier[croupier.index(str_ace)] = str_ace + "(1)"
                    croupier_value -= 10 #ace changes value to 1
            print(f"Croupier has {len(croupier)} cards with value of at least {croupier_old_value} points.")
            #print(f"Croupier drew: {card} with value of {card_value} points")
            #print(f"Croupier has: {croupier}. {croupier_value} points.")
            del cards[card]
        else:
            print("\nCroupier stands.\n")
            return False

    def player_draws():
        global player_value, cards, card
        card, card_value = random.choice(list(cards.items()))
        player.append(card)
        player_value += card_value
        print(f"You drew: {card}")

        if player_value > 21:
            test = [card for card in player if card in aces]
            if len(test) > 0: #check for aces in hand
                str_ace = "".join(test[0])
                player[player.index(str_ace)] = str_ace + "(1)"
                player_value -= 10

        print(f"You have: {player}. {player_value} points.")
        del cards[card]

    def game():
        global cards, player_value, blackjack, player

        print("\nLet's play blackjack!\n")

        time.sleep(1)

        first_deal() #begin game

        time.sleep(2)

        player_result = True
        croupier_result = True
        while player_result == True and croupier_result == True:
            blackjack = check_player_for_blackjack()
            player_choice = player_choose()
            if player_choice == True: #if player chose to hit
                player_draws()
                player_result = check_player(player_value)
                if player_result == False:
                    break

            time.sleep(2)
            croupier_choice = croupier_choose(croupier_value)
            croupier_result = check_croupier(croupier_value)
            if croupier_result == False:
                break

            if croupier_choice == False and player_choice == False:
                break

        time.sleep(2)
        check_game(player_value, croupier_value)

    game()

    again = input("\nDo you want to play again? Enter 'Y'\nEnter any other key to exit.\n")
    if again == "Y" or "y":
        pass
    if again != "Y" and again != "y":
        break


"""Contains the DOMWidget class"""

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from traitlets import Unicode, Dict, Instance, Bool, List, \
    CaselessStrEnum, Tuple, CUnicode, Int, Set
from .widget import Widget, widget_serialization
from .trait_types import Color
from .style import Style

class DOMWidget(Widget):
    """Widget that can be inserted into the DOM"""

    _model_name = Unicode('DOMWidgetModel', help="""Name of the backbone model
        registered in the front-end to create and sync this widget with.""", sync=True)
    visible = Bool(True, allow_none=True, help="Whether the widget is visible.  False collapses the empty space, while None preserves the empty space.", sync=True)
    _css = Tuple(sync=True, help="CSS property list: (selector, key, value)")
    _dom_classes = Tuple(sync=True, help="DOM classes applied to widget.$el.")

    style = Instance(Style, allow_none=True, sync=True, **widget_serialization)
    def _style_default(self):
        return Style()

    width = CUnicode(sync=True)
    height = CUnicode(sync=True)
    padding = CUnicode(sync=True)
    margin = CUnicode(sync=True)

    color = Color(None, allow_none=True, sync=True)
    background_color = Color(None, allow_none=True, sync=True)
    border_color = Color(None, allow_none=True, sync=True)

    border_width = CUnicode(sync=True)
    border_radius = CUnicode(sync=True)
    border_style = CaselessStrEnum(values=[ # http://www.w3schools.com/cssref/pr_border-style.asp
        'none',
        'hidden',
        'dotted',
        'dashed',
        'solid',
        'double',
        'groove',
        'ridge',
        'inset',
        'outset',
        'initial',
        'inherit', ''],
        default_value='', sync=True)

    font_style = CaselessStrEnum(values=[ # http://www.w3schools.com/cssref/pr_font_font-style.asp
        'normal',
        'italic',
        'oblique',
        'initial',
        'inherit', ''],
        default_value='', sync=True)
    font_weight = CaselessStrEnum(values=[ # http://www.w3schools.com/cssref/pr_font_weight.asp
        'normal',
        'bold',
        'bolder',
        'lighter',
        'initial',
        'inherit', ''] + list(map(str, range(100,1000,100))),
        default_value='', sync=True)
    font_size = CUnicode(sync=True)
    font_family = Unicode(sync=True)

    def __init__(self, *pargs, **kwargs):
        super(DOMWidget, self).__init__(*pargs, **kwargs)

        def _validate_border(name, old, new):
            if new is not None and new != '':
                if name != 'border_width' and not self.border_width:
                    self.border_width = 1
                if name != 'border_style' and self.border_style == '':
                    self.border_style = 'solid'
        self.on_trait_change(_validate_border, ['border_width', 'border_style', 'border_color'])


import pika
import sys

QUEUE_NAME = 'requests'
HOST = 'localhost'
PORT = '5672'

connection = pika.BlockingConnection(pika.ConnectionParameters(
    host=HOST,
    port=PORT
))
channel = connection.channel()
channel.queue_declare(queue=QUEUE_NAME)

range_from, range_to = sys.argv[1], sys.argv[2]

body = f"{range_from} {range_to}"

channel.basic_publish(exchange='',
                      routing_key=QUEUE_NAME,
                      body=body)
print("Sent:", body)
connection.close()


#!/usr/bin/python3
"""
Python script to fetch list of CNS registrants from Memberclicks Oasis based on
the RegistrationYear attribute.

File: oasis-registration-list.py

Copyright 2021 Ankur Sinha
Author: Ankur Sinha <sanjay DOT ankur AT gmail DOT com>
"""

from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
from requests.auth import HTTPBasicAuth
from datetime import datetime

baseurl = 'https://ocns.memberclicks.net/'
# from the get_groups function
# these are the ones we care for in abstract submission
# All members can submit 2 abstracts
# Non members (prospect) can submit 1 abstract
member_types = [
    "Faculty Member/For-profit Employee", "OCNS Board",
    "Postdoc Member/Non-profit Employee", "Student Member"
]

def get_token(client_id, client_secret):
    """
    Get authorization token from server.

    :param client ID: client ID to use
    :param client secret: client secret
    :returns: authorization token

    """
    # End point for authorisation
    URL_auth = baseurl + 'oauth/v1/token'
    auth = HTTPBasicAuth(client_id, client_secret)
    client = BackendApplicationClient(client_id)
    oauth = OAuth2Session(client=client)

    api_token = oauth.fetch_token(token_url=URL_auth, auth=auth)

    #  print("API Token received: {}".format(api_token))
    return api_token

def get_groups(client_id, api_token):
    """Get list of groups

    :param client_id: client ID
    :param api_token: API Token
    :returns: TODO
    """
    group_endpoint = baseurl + "api/v1/group"
    headers = {
        "Accept": "application/json",
        "Authorization": "Bearer {}".format(api_token)
    }
    client = OAuth2Session(client_id, token=token)
    r = client.get(group_endpoint, headers=headers)
    if r.status_code == 200:
        results = r.json()
        print("Total groups: ", results["totalCount"])
        print("Groups are:")
        for group in results["groups"]:
            print("{}".format(group["name"]))

def get_registered_users(api_token, year):
    """
    Get list of users registered for a particular conference year.

    :param api_token: api_token
    :param year: value of RegistrationYear field to test
    :returns: Nothing

    """
    headers = {
        "Accept": "application/json",
        "Authorization": "Bearer {}".format(api_token),
    }
    params = {
        "pageSize": "100"
    }
    data = {
        'RegistrationYear': year,
    }
    # No slash at the start here
    URL_profiles = baseurl + "api/v1/profile/search"
    client = OAuth2Session(client_id, token=token)
    r = client.post(URL_profiles, headers=headers, json=data)
    # https://help.memberclicks.com/hc/en-us/articles/230536427-API-Resources-Profile-Search
    if r.status_code == 201:
        # https://help.memberclicks.com/hc/en-us/articles/230536367#get-a-list-of-profiles-by-search-id
        search_url = r.json()["profilesUrl"]
        print("Received search URL: {}".format(search_url))
        print("Getting profiles")
        URL_profiles = search_url
        client = OAuth2Session(client_id, token=token)
        r = client.get(URL_profiles, headers=headers, params=params)
        if r.status_code == 200:
            results = r.json()
            print("Total profiles found: {}".format(results["totalCount"]))
            print("Iterating pages")
            next_page_url = results["firstPageUrl"]
            now = datetime.now().strftime("%Y%m%d%H%M")
            csv_filename = "{}-cns-{}-registrants.csv".format(now, year)
            print("Writing to file: {}".format(csv_filename))
            with open(csv_filename, 'w') as f:
                print(
                    "{}, {}, {}, {}, {}, {}, {}, {}".format(
                        "First Name",
                        "Last Name",
                        "E-mail",
                        "Member type",
                        "Job Title",
                        "Organization",
                        "Registration Year",
                        "Number of abstracts permitted"
                    ),
                    file=f
                )
                while True:
                    client = OAuth2Session(client_id, token=token)
                    r = client.get(next_page_url, headers=headers)
                    results = r.json()
                    print("Page: {}/{}".format(results["pageNumber"],
                                               results["totalPageCount"]))

                    for profile in results["profiles"]:
                        # Number of abstracts
                        num_abstracts = 0
                        if profile["[Member Type]"] in member_types:
                            num_abstracts = 2
                        elif profile["[Member Type]"] == "Prospect":
                            num_abstracts = 1

                        # Mark Board members: for printed list
                        if "OCNS Board" in profile["[Group]"]:
                            member_type = "OCNS Board"
                        else:
                            member_type = profile["[Member Type]"]

                        # Mark non-members for printed list
                        if profile["[Member Type]"] == "Prospect":
                            member_type = "Non-member"

                        print(
                            "{}, {}, {}, {}, {}, {}, {}, {}".format(
                                profile["[Name | First]"],
                                profile["[Name | Last]"],
                                profile["[Email | Primary]"],
                                member_type,
                                profile["Job Title"],
                                profile["[Organization]"],
                                profile["RegistrationYear"],
                                num_abstracts
                            ), file=f)

                    next_page_url = results["nextPageUrl"]
                    if not next_page_url:
                        print("Reached last page. Done.")
                        break

    else:
        print("Received status code {}".format(r.status_code))
        print("Response: {}".format(r.text))

if __name__ == "__main__":
    # credentials
    client_id = "client-id"
    client_secret = "client-secret"

    # Must be a string
    year = "2021"

    # Do the work
    token = get_token(client_id, client_secret)
    get_registered_users(token, year)

    #  get_groups(client_id, token)


'''
Convert the data from story.json into something that can be used by the shopping cart version of the
snap sim.
'''
import json

if __name__=='__main__':
    # Some constants.
    IN_FILENAME = "story.json"
    OUT_FILENAME = "story_cart.json"

    # Load the data.
    data = json.load(open(IN_FILENAME))

    # Initialize the resultant JSON.
    result_json = {}
    result_json["bio"] = data["bio"]
    result_json["items"] = []

    # Iterate through each section of the story:
    for section in data["story"]:
        # Iterate through each item in the section.
        for item in section["items"]:
            # Add the item to the result set.
            result_json["items"].append({
                "name": item["name"],
                "quantity": item["quantity"],
                "description": item["description"],
                "price": item["price"],
                "notes": item["notes"]
            })

    # Write the JSON to the output file.
    json.dump(result_json, open(OUT_FILENAME, "w"))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/5/30 16:44
# @Author  : Yajun Yin
# @Note    :

import os
import logging
from multiprocessing import Process, Queue, JoinableQueue

"""
task_queue: 类似生产者
res_queue: 类似消费者
"""

process_num = 3

def init_logger(name):
    logger = logging.getLogger(name)
    logger.setLevel(level=logging.INFO)
    handler = logging.FileHandler("{name}.log".format(name=name))
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter(fmt='%(levelname)s %(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger

def get_tasks():
    return [1, 2, 3]

class JobProcess(Process):
    def __init__(self, task_queue, res_queue):
        Process.__init__(self)  # 父类必须第一个init
        self.task_queue = task_queue
        self.res_queue = res_queue
        self.logger = None

    def run(self):
        self.logger = init_logger(str(os.getpid()))  # 每个子进程建立日志
        while True:
            task = self.task_queue.get()  # 阻塞程序，时刻监听队列是否有消息
            ret = self.do(task)
            self.res_queue.put(ret)
            self.task_queue.task_done()  # 标志队列中的任务完成，配合queue.join

    def do(self, task):
        # should be over-write
        return 1

task_queue = JoinableQueue()  # 实现了join和task_done的Queue
res_queue = Queue()

# set process pool
for _ in range(process_num):
    p = JobProcess(task_queue, res_queue)
    p.daemon = True
    p.start()

    all_tasks = get_tasks()
    for task in all_tasks:
        task_queue.put(task)

    task_queue.join()  # 只有队列中的所有任务完成才结束,task_queue.qsize==0


n = int(input())
li = []

for i in range(n):
    temp = list(map(int, input().split()))
    li.append(temp)

li.sort(key=lambda x: x[0])
li.sort(key=lambda x: x[1])
cnt = 1
flag = li[0][1]

for i in range(1, len(li)):
    if li[i][0] >= flag:
        cnt += 1
        flag = li[i][1]

print(cnt)


import random

class Segment:
    def __init__(self, x, y, direction):
        self.x = x
        self.y = y
        self.direction = direction
        self.next = None
        self.prev = None

class Snake:
    def __init__(self, x, y):
        '''Initialize a one segment snake at position (x,y)'''
        '''initial direction is 'up'''
        s = Segment(x, y, "up")
        self.head = s
        self.tail = s
        self.radius = 8
        self.length = 1

    def changeDirection(self, key):
        '''Change direction of snake head'''
        # cannot move in opposite of current direction
        current_direction = self.head.direction
        if key is "left" and current_direction is "right":
            return
        elif key is "right" and current_direction is "left":
            return
        elif key is "up" and current_direction is "down":
            return
        elif key is "down" and current_direction is "up":
            return

        self.head.direction = key

    def popTail(self):
        '''removes tail from snake'''
        if self.head is self.tail:
            return
        else:
            new_tail = self.tail.prev
            new_tail.next = None
            self.tail = new_tail
        self.length -= 1

    def addHead(self):
        '''Add segment to head of snake'''
        head = self.head
        delta = self.radius * 2

        #make new head Segment
        if head.direction == "left":
            new_head = Segment(head.x - delta, head.y, head.direction)
        elif head.direction == "right":
            new_head = Segment(head.x + delta, head.y, head.direction)
        elif head.direction == "up":
            new_head = Segment(head.x, head.y - delta, head.direction)
        elif head.direction == "down":
            new_head = Segment(head.x, head.y + delta, head.direction)

        #add new head
        if self.head is None:
            self.head = new_head
            self.tail = new_head
        else:
            new_head.next = self.head
            self.head.prev = new_head
            self.head = new_head

        self.length += 1

    def addTail(self):
        '''Add segment to tail of snake'''
        tail = self.tail
        delta = self.radius * 2

        #make new tail Segment
        if tail.direction == "left":
            new_tail = Segment(tail.x + delta, tail.y, tail.direction)
        elif tail.direction == "right":
            new_tail = Segment(tail.x - delta, tail.y, tail.direction)
        elif tail.direction == "up":
            new_tail = Segment(tail.x, tail.y + delta, tail.direction)
        elif tail.direction == "down":
            new_tail = Segment(tail.x, tail.y - delta, tail.direction)

        #insert new tail
        if self.head is None:
            self.head = new_tail
            self.tail = new_tail
        else:
            new_tail.prev = self.tail
            self.tail.next = new_tail
            self.tail = new_tail

        self.length += 1

    def move(self):
        '''Moves snake. Pops tail off and adds to head'''
        self.addHead()
        self.popTail()

    def eat(self, food):
        '''Increases length of snake and resets food if head
            overlaps with food'''
        s = self.head
        r = self.radius
        if (s.x - r) <= food.x <= (s.x + r) and (s.y - r) <= food.y <= (s.y + r):
            food.setPosition(self)
            self.addTail()

    def collide(self, width, height):
        '''Detects whether game ends'''
        # out of bounds
        if self.head.x < self.radius or self.head.x > (width - self.radius):
            return True
        elif self.head.y < self.radius or self.head.y > (height - self.radius):
            return True

        # hit self
        s = self.head.next
        while s is not None:
            if (s.x -3)<= self.head.x <= (s.x + 3) and (s.y -3) <= self.head.y <= (s.y + 3):
                return True
            s = s.next


'''
Created on 17.04.2018

@author: malte
'''
import math

from helper import inout
import numpy as np
import pandas as pd

class MetaRerank:

    def __init__(self, base, only_hidden=False, threshold=0.15, meta_folder='data/metadata_combined/', return_num_preds=500 ):
        self.base = base
        self.return_num_preds = return_num_preds
        self.only_hidden = only_hidden
        self.meta_folder = meta_folder
        self.threshold = threshold

        #self.fields = ['acousticness','danceability','energy','instrumentalness','loudness','mode','speechiness','tempo','valence']
        self.fields = ['energy','loudness','tempo']

    def train(self, data, test=None):
        self.base.train(data, test=test)

        track_meta = inout.load_meta_track( self.meta_folder )
        track_meta['spotify_id'] = track_meta['track_id']
        del track_meta['track_id']

        tracks = data['tracks']
        tracks['spotify_id'] = tracks['track_uri'].apply( lambda x: x.split(':')[2] )

        self.tracks = tracks.merge( track_meta, on='spotify_id', how='left' )
        self.tracks = self.tracks[ self.fields + ['track_id'] ]
        self.tracks['tempo'] = ( self.tracks['tempo'] - self.tracks['tempo'].min() ) / ( self.tracks['tempo'].max() - self.tracks['tempo'].min() )
        self.tracks['loudness'] = ( self.tracks['loudness'] - self.tracks['loudness'].min() ) / ( self.tracks['loudness'].max() - self.tracks['loudness'].min() )
        self.tracks = self.tracks.fillna(0)
        self.tracks.reset_index(inplace=True,drop=True)
        self.trackmap = pd.Series( index=self.tracks['track_id'], data=self.tracks.index )
        self.tracks = self.tracks[self.fields].values

    def predict(self, name=None, tracks=None, playlist_id=None, artists=None, num_hidden=None):

        baseRes = self.base.predict( name, tracks, playlist_id=playlist_id, artists=artists, num_hidden=num_hidden ).copy()
        tracks = tracks if tracks is not None else []

        baseResShort = baseRes
        num = len(baseRes)
        if self.only_hidden:
            num = num_hidden
            if num < len(baseRes):
                baseResShort = baseRes.head(num).copy()

        if len(tracks) > 0:

            seedidx = self.trackmap[ tracks ].values
            residx = self.trackmap[ baseResShort.track_id.values ].values.astype('int')

            seedmeta = self.tracks[seedidx]
            sstd = seedmeta.std(axis=0) + 1e-7
            smean = seedmeta.mean(axis=0)

            if sstd.mean() <= self.threshold:

                resmeta = self.tracks[residx]

                sim = np.dot( resmeta, smean )
                seednorm = np.sum( np.square( smean ) )
                resnorm = np.sum( np.square( resmeta ),axis=1 )
                sim = sim / ( seednorm * resnorm )

    #             if len(tracks)  > 1:
    #                 resmeta = resmeta.T
    #                 for i in range(len(self.fields)):
    #                     resmeta[i] = self.normpdf( resmeta[i], smean[i], sstd[i] )
    #
    #                 sim = resmeta.sum(axis=0)
    #             else:
    #                 resmeta = np.abs( resmeta - seedmeta )
    #                 sim = (1 - resmeta).sum(axis=1)
    #
    #             sim = (sim - sim.min()) / (sim.max() - sim.min())

                baseResShort['confidence'] += baseResShort['confidence'] * sim
                #res = res[['track_id','confidence','metasim']]

                baseResShort.sort_values( ['confidence','track_id'], ascending=[False,True], inplace=True )
                #baseRes['confidence'] = baseRes['confidence'] * sim.min()

        baseRes = pd.concat([baseResShort,baseRes.tail( len(baseRes)-num )] )

        return baseRes.head(self.return_num_preds)

    def normpdf(self, x, mean, sd):
        var = float(sd)**2
        pi = 3.1415926
        denom = (2*pi*var)**.5
        num = np.exp(-(x-mean)**2/(2*var))
        return num/denom


# File:        2048.py
# Description: opens up the firefox browser to the 2048 game and executes Up, Right, Down, Left pattern on the game.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# Function:    play20480()
# Description: launches a firefox browser to the 2048 game url and constantly performs an Up, Right, Down, Left pattern
#              on the game
# Parameters:  N/A
# Return:      N/A
def play2048():
    # setup firefox to open to the 2048 game
    url = "https://play2048.co/"
    browser = webdriver.Firefox()
    browser.get(url)

    gameArea = browser.find_element_by_tag_name('html')

    # loop that constantly plays the Up, Right, Down, Left pattern on the 2048 game
    while True:
        gameArea.send_keys(Keys.UP)
        gameArea.send_keys(Keys.RIGHT)
        gameArea.send_keys(Keys.DOWN)
        gameArea.send_keys(Keys.LEFT)

play2048()


from netCDF4 import Dataset, date2num, num2date
import os, time
import subprocess
import ctypes
from matplotlib import pyplot as plt
import numpy as np
import datetime
import warnings
from decimal import Decimal

def files_string2list(files_string):
    '''
    灏嗗瓧绗︿覆鍙樹负strptime鍒嗗壊涓烘枃浠跺悕
    :param files_string:
    :return:鏂囦欢鍚嶅垪琛ㄨ繑鍥?    '''
    files_list = []
    temp = ''
    for i in range(len(files_string)):
        if files_string[i] != '\n':
            temp += files_string[i]
        else:
            files_list.append(temp)
            temp = ''
    return files_list

def oco2_weighting_calculation(xco2_day,  xco2_uncert_day, lat_day, lon_day, co2_prior_day,  xco2_prior_day, xco2_avk_day, pres_day, \
            surfp_day, pressure_weight_day, time_day, start_index, last_index, is_10s):
    '''
    浜屾哀鍖栫⒊寤撶嚎娴撳害, 鏂瑰樊1s璁＄畻
    :param oco2:
    :param first_index:
    :param last_index:
    :return:
    '''

    N = last_index-start_index

    #delte nan data
    for i in range(start_index, last_index):
        if(np.isnan(xco2_uncert_day[i])):
            print('nan true')

            if i>start_index:
                xco2_uncert_day[i] = xco2_uncert_day[i-1]
            else:
                xco2_uncert_day[i] = xco2_uncert_day[i+1]

    xco2_inverse_sigma_square = 1/np.square(xco2_uncert_day[start_index:last_index])

    xco2bar = np.sum(xco2_day[start_index:last_index]*xco2_inverse_sigma_square/np.sum(xco2_inverse_sigma_square))
    latbar = np.sum(lat_day[start_index:last_index]*xco2_inverse_sigma_square/np.sum(xco2_inverse_sigma_square))
    co2_priorbar = np.dot(co2_prior_day[start_index:last_index].transpose(), xco2_inverse_sigma_square.transpose())/np.sum(xco2_inverse_sigma_square)
    xco2_priorbar = np.sum(xco2_prior_day[start_index:last_index] * xco2_inverse_sigma_square) / np.sum(xco2_inverse_sigma_square)
    xco2_avkbar = np.dot(xco2_avk_day[start_index:last_index].transpose(), xco2_inverse_sigma_square.transpose()) / np.sum(xco2_inverse_sigma_square)
    presbar = np.dot(pres_day[start_index:last_index].transpose(), xco2_inverse_sigma_square.transpose()) / np.sum(xco2_inverse_sigma_square)
    surfpbar = np.sum(surfp_day[start_index:last_index] * xco2_inverse_sigma_square) / np.sum(xco2_inverse_sigma_square)
    pressure_weightbar = np.dot(pressure_weight_day[start_index:last_index].transpose(), xco2_inverse_sigma_square.transpose()) / np.sum(xco2_inverse_sigma_square)
    timebar = np.sum((time_day[start_index:last_index]-time_day[start_index]) * xco2_inverse_sigma_square)\
              / np.sum(xco2_inverse_sigma_square)+time_day[start_index]

    # #debug
    # debug_unit = 'seconds since 1970-01-01 00:00:00'
    # if timebar-time_day[start_index]<-1:
    #     print(time_day[last_index-1]-time_day[start_index], num2date(time_day[start_index], debug_unit), num2date(time_day[last_index-1], debug_unit))
    # #print(num2date(timebar, debug_unit), num2date(time_day[start_index], debug_unit), num2date(time_day[last_index-1], debug_unit), timebar-time_day[start_index])
    # #end debug

    #base longitude 178 deg
    if(np.max(lon_day[start_index:last_index])>178):
        tmp_lon = np.zeros((last_index-start_index))
        for cur_index in range(start_index, last_index):
            if lon_day[cur_index] < -178:
                tmp_lon[cur_index-start_index] = lon_day[cur_index]+180+2
            else:
                tmp_lon[cur_index - start_index] = lon_day[cur_index] - 178
        tmp_lonbar = np.sum(tmp_lon* xco2_inverse_sigma_square)/np.sum(xco2_inverse_sigma_square)
        if(tmp_lonbar>2):
            lonbar = -180+(tmp_lonbar-2)
        else:
            lonbar = 178+tmp_lonbar

    #base longitude  -178 deg
    elif(np.min(lon_day[start_index])<-178):
        tmp_lon = np.zeros((last_index - start_index))
        for cur_index in range(start_index, last_index):
            if lon_day[cur_index] > 178:
                tmp_lon[cur_index-start_index] = lon_day[cur_index]-180-2
            else:
                tmp_lon[cur_index-start_index] = -180-lon_day[cur_index]

        tmp_lonbar = np.sum(tmp_lon * xco2_inverse_sigma_square) / np.sum(xco2_inverse_sigma_square)

        if(tmp_lonbar<-2):
            lonbar = 180-(-2-tmp_lonbar)
        else:
            lonbar = -178+tmp_lonbar
    else:
        lonbar = np.sum(lon_day[start_index:last_index]*xco2_inverse_sigma_square) / np.sum(xco2_inverse_sigma_square)

    xco2_sigma_IVE = 1/np.sqrt(np.sum(xco2_inverse_sigma_square)/N)
    xco2_floor = np.min(xco2_uncert_day[start_index:last_index])
    xco2_sigma_spread = 0
    if(N!=1):
        xco2_sigma_spread = np.std(xco2_day[start_index:last_index] , ddof=1)

    #variance judgement
    if is_10s:
        xco2_uncertbar = xco2_sigma_IVE
    else:
        xco2_uncertbar = max(xco2_sigma_IVE, xco2_floor, xco2_sigma_spread)

    #reset array
    co2_priorbar = co2_priorbar.transpose()
    xco2_avkbar = xco2_avkbar.transpose()
    presbar = presbar.transpose()
    pressure_weightbar = pressure_weightbar.transpose()

    return xco2bar, xco2_uncertbar, latbar, lonbar, co2_priorbar, xco2_priorbar, xco2_avkbar, presbar, \
           surfpbar, pressure_weightbar, timebar

def oco2_10s_all(xco2_1s, xco2_uncert_1s, lat_1s, lon_1s, co2_prior_1s, xco2_prior_1s,xco2_avk_1s, pres_1s, \
                 surfp_1s, pressure_weight_1s, time_1s, length):
    num = 0
    start_index = 0
    tmp_10s_xco2 = np.zeros(length)
    tmp_10s_xco2_var = np.zeros(length)
    tmp_10s_lat = np.zeros(length)
    tmp_10s_lon = np.zeros(length)
    tmp_10s_co2_apri_profile = np.zeros((length, 20))
    tmp_10s_xco2_apri = np.zeros(length)
    tmp_10s_xco2_avk = np.zeros((length, 20))
    tmp_10s_pres = np.zeros((length, 20))
    tmp_10s_surf = np.zeros(length)
    tmp_10s_xco2_pwf = np.zeros((length, 20))
    tmp_10s_time = np.zeros(length)

    while (True):
        begin_time_num = time_1s[start_index]

        # find time interval index in 10s
        I_flag = True #i exit normally
        for i in range(start_index, length):
            time_num = time_1s[i]
            if (time_num - begin_time_num) < 10:
                continue
            else:
                if i== length-1:
                    I_flag = False
                break

        last_index = i

        if i == length - 1 and I_flag:
            last_index = i+1

        tmp_10s_xco2[num], tmp_10s_xco2_var[num], tmp_10s_lat[num], tmp_10s_lon[num], tmp_10s_co2_apri_profile[num, :], tmp_10s_xco2_apri[num], \
        tmp_10s_xco2_avk[num, :], tmp_10s_pres[num, :], tmp_10s_surf[num], tmp_10s_xco2_pwf[num, :], tmp_10s_time[num] = oco2_weighting_calculation(xco2_1s, \
                xco2_uncert_1s, lat_1s, lon_1s, co2_prior_1s, xco2_prior_1s,xco2_avk_1s, pres_1s, surfp_1s, pressure_weight_1s, \
                time_1s, start_index, last_index, True)

        num += 1
        if i == length - 1 and I_flag:
            break
        # #debug
        # if tmp_10s_lon[num-1]>-50 and tmp_10s_lon[num-1]<-30 and tmp_10s_lat[num-1]>10:
        #     print(time_1s[start_index:last_index], time_1s[last_index-1]-time_1s[start_index], )
        #     print('lon, lat', lon_1s[start_index:last_index], lat_1s[start_index:last_index])
        # #end debug
        start_index = i

    return tmp_10s_xco2, tmp_10s_xco2_var, tmp_10s_lat, tmp_10s_lon, tmp_10s_co2_apri_profile, tmp_10s_xco2_apri, tmp_10s_xco2_avk, tmp_10s_pres, \
           tmp_10s_surf, tmp_10s_xco2_pwf, tmp_10s_time, num

in_oco2_dir = '/share/nas1_share2/zhangqw_share/observation_data/OCO2_DATA/OCO2_L2_Lite_FP.9r/'
out_oco2_dir = '/share/nas1_share1/huoxiao_share/CO2_data/Observation_data/satellite_10s_average/OCO2_Litev9_ND_plus_Glintland/'

oco2_begin_time_str = "2018-12-31 00:00:00"
oco2_end_time_str = "2019-01-01 00:00:00"
oco2_begin_time_date = datetime.datetime.strptime(oco2_begin_time_str, '%Y-%m-%d %H:%M:%S')
oco2_end_time_date = datetime.datetime.strptime(oco2_end_time_str, '%Y-%m-%d %H:%M:%S')

oco2_cur_time_date = oco2_begin_time_date

pro_begin_time = time.time()

#debug
huoxiao_debug_num = 0
warnings.filterwarnings('error')
while(oco2_cur_time_date!=oco2_end_time_date):

    #Determine if the file exists,
    if subprocess.call('test -f ' + in_oco2_dir + '{:0>4d}/oco2_LtCO2_{:0>2d}{:0>2d}{:0>2d}'.format(oco2_cur_time_date.year,oco2_cur_time_date.year%2000, \
        oco2_cur_time_date.month,oco2_cur_time_date.day) + '_B9003r*',shell=True):
        oco2_cur_time_date += datetime.timedelta(days=1)
        continue

    os.chdir(in_oco2_dir)
    command_output = subprocess.check_output("ls  "+ '{:0>4d}/oco2_LtCO2_{:0>2d}{:0>2d}{:0>2d}'.format(oco2_cur_time_date.year,oco2_cur_time_date.year%2000, \
        oco2_cur_time_date.month,oco2_cur_time_date.day) + '_B9003r*',shell=True)

    oco2_filename = files_string2list(command_output.decode())[0]
    oco2_file_path = in_oco2_dir+oco2_filename

    print(oco2_file_path+' is reading')

    in_oco2_nc_data = Dataset(oco2_file_path, 'r')

    # #file is empty
    # if in_oco2.groups['RetrievalResults']['outcome_flag'][:].shape[0] == 0:
    #     continue

    #operation mode: Nadir
    tmp_xco2_day_omeq0 = np.array(in_oco2_nc_data.variables['xco2'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:]==0)]
    tmp_lon_day_omeq0 = np.array(in_oco2_nc_data.variables['longitude'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 0)]
    tmp_lat_day_omeq0 = np.array(in_oco2_nc_data.variables['latitude'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 0)]
    tmp_co2_prior_day_omeq0 = np.array(in_oco2_nc_data.variables['co2_profile_apriori'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 0), :][0]
    tmp_xco2_prior_day_omeq0 = np.array(in_oco2_nc_data.variables['xco2_apriori'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 0)]
    tmp_pres_day_omeq0 = np.array(in_oco2_nc_data.variables['pressure_levels'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 0), :][0]
    tmp_surfp_day_omeq0 = np.array(in_oco2_nc_data.groups['Retrieval']['psurf'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 0)]
    tmp_xco2_uncert_day_omeq0 = np.array(in_oco2_nc_data.variables['xco2_uncertainty'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 0)]
    tmp_pressure_weight_day_omeq0 = np.array(in_oco2_nc_data.variables['pressure_weight'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 0), :][0]
    tmp_xco2_avk_uncert_day_omeq0 = np.array(in_oco2_nc_data.variables['xco2_averaging_kernel'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 0), :][0]
    tmp_time_day_omeq0 = np.array(in_oco2_nc_data.variables['time'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 0)]
    tmp_flag_day_omeq0 = np.array(in_oco2_nc_data.variables['xco2_quality_flag'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 0)]

    tmp_xco2_day_omeq0_gd = tmp_xco2_day_omeq0[np.where(tmp_flag_day_omeq0==0)]
    tmp_lon_day_omeq0_gd = tmp_lon_day_omeq0[np.where(tmp_flag_day_omeq0 == 0)]
    tmp_lat_day_omeq0_gd = tmp_lat_day_omeq0[np.where(tmp_flag_day_omeq0 == 0)]
    tmp_co2_prior_day_omeq0_gd = tmp_co2_prior_day_omeq0[np.where(tmp_flag_day_omeq0 == 0), :][0]
    tmp_xco2_prior_day_omeq0_gd = tmp_xco2_prior_day_omeq0[np.where(tmp_flag_day_omeq0 == 0)]
    tmp_pres_day_omeq0_gd = tmp_pres_day_omeq0[np.where(tmp_flag_day_omeq0 == 0), :][0]
    tmp_surfp_day_omeq0_gd = tmp_surfp_day_omeq0[np.where(tmp_flag_day_omeq0 == 0)]
    tmp_xco2_uncert_day_omeq0_gd = tmp_xco2_uncert_day_omeq0[np.where(tmp_flag_day_omeq0 == 0)]
    tmp_pressure_weight_day_omeq0_gd = tmp_pressure_weight_day_omeq0[np.where(tmp_flag_day_omeq0 == 0), :][0]
    tmp_xco2_avk_uncert_day_omeq0_gd = tmp_xco2_avk_uncert_day_omeq0[np.where(tmp_flag_day_omeq0 == 0), :][0]
    tmp_time_day_omeq0_gd = tmp_time_day_omeq0[np.where(tmp_flag_day_omeq0 == 0)]

    #operation mode: Ocean Glint
    tmp_xco2_day_omeq1 = np.array(in_oco2_nc_data.variables['xco2'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:]==1)]
    tmp_lon_day_omeq1 = np.array(in_oco2_nc_data.variables['longitude'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 1)]
    tmp_lat_day_omeq1 = np.array(in_oco2_nc_data.variables['latitude'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 1)]
    tmp_co2_prior_day_omeq1 = np.array(in_oco2_nc_data.variables['co2_profile_apriori'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 1), :][0]
    tmp_xco2_prior_day_omeq1 = np.array(in_oco2_nc_data.variables['xco2_apriori'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 1)]
    tmp_pres_day_omeq1 = np.array(in_oco2_nc_data.variables['pressure_levels'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 1), :][0]
    tmp_surfp_day_omeq1 = np.array(in_oco2_nc_data.groups['Retrieval']['psurf'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 1)]
    tmp_xco2_uncert_day_omeq1 = np.array(in_oco2_nc_data.variables['xco2_uncertainty'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 1)]
    tmp_pressure_weight_day_omeq1 = np.array(in_oco2_nc_data.variables['pressure_weight'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 1), :][0]
    tmp_xco2_avk_uncert_day_omeq1 = np.array(in_oco2_nc_data.variables['xco2_averaging_kernel'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 1), :][0]
    tmp_time_day_omeq1 = np.array(in_oco2_nc_data.variables['time'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 1)]
    tmp_flag_day_omeq1 = np.array(in_oco2_nc_data.variables['xco2_quality_flag'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 1)]
    tmp_lo_flag_omeq1 = np.array(in_oco2_nc_data.groups['Sounding']['land_water_indicator'][:])[np.where(in_oco2_nc_data.groups['Sounding']['operation_mode'][:] == 1)]

    tmp_xco2_day_omeq1_lo_gd = tmp_xco2_day_omeq1[np.where(tmp_flag_day_omeq1==0)]
    tmp_lon_day_omeq1_lo_gd = tmp_lon_day_omeq1[np.where(tmp_flag_day_omeq1 == 0)]
    tmp_lat_day_omeq1_lo_gd = tmp_lat_day_omeq1[np.where(tmp_flag_day_omeq1 == 0)]
    tmp_co2_prior_day_omeq1_lo_gd = tmp_co2_prior_day_omeq1[np.where(tmp_flag_day_omeq1 == 0), :][0]
    tmp_xco2_prior_day_omeq1_lo_gd = tmp_xco2_prior_day_omeq1[np.where(tmp_flag_day_omeq1 == 0)]
    tmp_pres_day_omeq1_lo_gd = tmp_pres_day_omeq1[np.where(tmp_flag_day_omeq1 == 0), :][0]
    tmp_surfp_day_omeq1_lo_gd = tmp_surfp_day_omeq1[np.where(tmp_flag_day_omeq1 == 0)]
    tmp_xco2_uncert_day_omeq1_lo_gd = tmp_xco2_uncert_day_omeq1[np.where(tmp_flag_day_omeq1 == 0)]
    tmp_pressure_weight_day_omeq1_lo_gd = tmp_pressure_weight_day_omeq1[np.where(tmp_flag_day_omeq1 == 0), :][0]
    tmp_xco2_avk_uncert_day_omeq1_lo_gd = tmp_xco2_avk_uncert_day_omeq1[np.where(tmp_flag_day_omeq1 == 0), :][0]
    tmp_time_day_omeq1_lo_gd = tmp_time_day_omeq1[np.where(tmp_flag_day_omeq1 == 0)]
    tmp_lo_flag_omeq1_lo_gd = tmp_lo_flag_omeq1[np.where(tmp_flag_day_omeq1 == 0)]

    #land
    tmp_xco2_day_omeq1_gd = tmp_xco2_day_omeq1_lo_gd[np.where(tmp_lo_flag_omeq1_lo_gd==0)]
    tmp_lon_day_omeq1_gd = tmp_lon_day_omeq1_lo_gd[np.where(tmp_lo_flag_omeq1_lo_gd == 0)]
    tmp_lat_day_omeq1_gd = tmp_lat_day_omeq1_lo_gd[np.where(tmp_lo_flag_omeq1_lo_gd == 0)]
    tmp_co2_prior_day_omeq1_gd = tmp_co2_prior_day_omeq1_lo_gd[np.where(tmp_lo_flag_omeq1_lo_gd == 0), :][0]
    tmp_xco2_prior_day_omeq1_gd = tmp_xco2_prior_day_omeq1_lo_gd[np.where(tmp_lo_flag_omeq1_lo_gd == 0)]
    tmp_pres_day_omeq1_gd = tmp_pres_day_omeq1_lo_gd[np.where(tmp_lo_flag_omeq1_lo_gd == 0), :][0]
    tmp_surfp_day_omeq1_gd = tmp_surfp_day_omeq1_lo_gd[np.where(tmp_lo_flag_omeq1_lo_gd == 0)]
    tmp_xco2_uncert_day_omeq1_gd = tmp_xco2_uncert_day_omeq1_lo_gd[np.where(tmp_lo_flag_omeq1_lo_gd == 0)]
    tmp_pressure_weight_day_omeq1_gd = tmp_pressure_weight_day_omeq1_lo_gd[np.where(tmp_lo_flag_omeq1_lo_gd == 0), :][0]
    tmp_xco2_avk_uncert_day_omeq1_gd = tmp_xco2_avk_uncert_day_omeq1_lo_gd[np.where(tmp_lo_flag_omeq1_lo_gd == 0), :][0]
    tmp_time_day_omeq1_gd = tmp_time_day_omeq1_lo_gd[np.where(tmp_lo_flag_omeq1_lo_gd == 0)]

    xco2_day = np.concatenate((tmp_xco2_day_omeq0_gd, tmp_xco2_day_omeq1_gd), axis=0)
    lon_day = np.concatenate((tmp_lon_day_omeq0_gd, tmp_lon_day_omeq1_gd), axis=0)
    lat_day = np.concatenate((tmp_lat_day_omeq0_gd, tmp_lat_day_omeq1_gd), axis=0)
    co2_prior_day = np.concatenate((tmp_co2_prior_day_omeq0_gd, tmp_co2_prior_day_omeq1_gd), axis=0)
    xco2_prior_day = np.concatenate((tmp_xco2_prior_day_omeq0_gd, tmp_xco2_prior_day_omeq1_gd), axis=0)
    pres_day = np.concatenate((tmp_pres_day_omeq0_gd, tmp_pres_day_omeq1_gd), axis=0)
    surfp_day = np.concatenate((tmp_surfp_day_omeq0_gd, tmp_surfp_day_omeq1_gd), axis=0)
    xco2_uncert_day = np.concatenate((tmp_xco2_uncert_day_omeq0_gd, tmp_xco2_uncert_day_omeq1_gd), axis=0)
    pressure_weight_day = np.concatenate((tmp_pressure_weight_day_omeq0_gd, tmp_pressure_weight_day_omeq1_gd), axis=0)
    xco2_avk_day = np.concatenate((tmp_xco2_avk_uncert_day_omeq0_gd, tmp_xco2_avk_uncert_day_omeq1_gd), axis=0)
    time_day = np.concatenate((tmp_time_day_omeq0_gd, tmp_time_day_omeq1_gd), axis=0)

    #sort from small to large
    xco2_day = xco2_day[np.argsort(time_day)]
    lon_day = lon_day[np.argsort(time_day)]
    lat_day = lat_day[np.argsort(time_day)]
    co2_prior_day = co2_prior_day[np.argsort(time_day), :]
    xco2_prior_day = xco2_prior_day[np.argsort(time_day)]
    pres_day = pres_day[np.argsort(time_day)]
    surfp_day = surfp_day[np.argsort(time_day)]
    xco2_uncert_day = xco2_uncert_day[np.argsort(time_day)]
    pressure_weight_day = pressure_weight_day[np.argsort(time_day)]
    xco2_avk_day = xco2_avk_day[np.argsort(time_day)]
    time_day = time_day[np.argsort(time_day)]

    #1s average
    index_length = time_day.shape[0]

    # 1s average from good quality data
    tmp_1s_xco2 = np.zeros(index_length)
    tmp_1s_xco2_var = np.zeros(index_length)
    tmp_1s_lat = np.zeros(index_length)
    tmp_1s_lon = np.zeros(index_length)
    tmp_1s_co2_apri_profile = np.zeros((index_length, 20))
    tmp_1s_xco2_apri = np.zeros(index_length)
    tmp_1s_xco2_avk = np.zeros((index_length, 20))
    tmp_1s_pres = np.zeros((index_length, 20))
    tmp_1s_surf = np.zeros(index_length)
    tmp_1s_xco2_pwf = np.zeros((index_length, 20))
    tmp_1s_time = np.zeros(index_length)

    num = 0
    start_index = 0
    if index_length == 0:
        oco2_cur_time_date += datetime.timedelta(days=1)
        continue

    while (True):

        begin_time_num = time_day[start_index]
        # find time interval index in 1s
        I_flag = True  # i exit normally
        for i in range(start_index, index_length):

            time_num = time_day[i]

            if (time_num - begin_time_num) <= 1:
                continue
            else:
                if i == index_length - 1:
                    I_flag = False
                break

        last_index = i
        if i == index_length - 1 and I_flag:
            last_index = i + 1

        tmp_1s_xco2[num], tmp_1s_xco2_var[num], tmp_1s_lat[num], tmp_1s_lon[num], tmp_1s_co2_apri_profile[num, :], tmp_1s_xco2_apri[num], \
        tmp_1s_xco2_avk[num, :], tmp_1s_pres[num, :], tmp_1s_surf[num], tmp_1s_xco2_pwf[num, :], tmp_1s_time[num] \
            = oco2_weighting_calculation(xco2_day, xco2_uncert_day, lat_day, lon_day, co2_prior_day, xco2_prior_day,xco2_avk_day, pres_day, surfp_day, \
                      pressure_weight_day, time_day, start_index, last_index, is_10s=False)

        num += 1

        if i == index_length - 1 and I_flag:
            break

        start_index = i

    # lon_ht_w50 = np.array(tmp_1s_lon)[np.where(np.array(tmp_1s_lon)[:] > -50)]
    # lon_lt_w30 = lon_ht_w50[np.where(lon_ht_w50 < -30)]
    # lat_ht_w50 = np.array(tmp_1s_lat)[np.where(np.array(tmp_1s_lon) > -50)]
    # lat_lt_w30 = lat_ht_w50[np.where(lon_ht_w50 < -30)]
    # lat_ht_n10 = lon_ht_w50[np.where(lat_lt_w30 > 10)]
    #
    # print(lat_ht_n10)
    # exit()
    # 10s calculation
    tmp_10s_xco2, tmp_10s_xco2_var, tmp_10s_lat, tmp_10s_lon, tmp_10s_co2_apri_profile, tmp_10s_xco2_apri, \
    tmp_10s_xco2_avk, tmp_10s_pres, tmp_10s_surf, tmp_10s_xco2_pwf, tmp_10s_time, num_10s \
        = oco2_10s_all(tmp_1s_xco2, tmp_1s_xco2_var, tmp_1s_lat, tmp_1s_lon, \
                       tmp_1s_co2_apri_profile, tmp_1s_xco2_apri, tmp_1s_xco2_avk, tmp_1s_pres, tmp_1s_surf, \
                       tmp_1s_xco2_pwf, tmp_1s_time, num)
    # output

    out_file_path = out_oco2_dir+oco2_filename
    print(out_file_path+' is writing')

    out_oco2 = Dataset(out_file_path, 'w')
    Dataset.createDimension(out_oco2, dimname='Time', size=None)
    Dataset.createDimension(out_oco2, dimname='level', size=20)

    Dataset.createDimension(out_oco2, dimname='DateStrLen', size=23)
    Times_out = Dataset.createVariable(out_oco2, 'Times', datatype=np.float64, dimensions=('Time'))
    Times_out.units = "seconds since 1970-01-01 00:00:00"
    Times_out[:] = tmp_10s_time[:num_10s]

    level_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
    level_out = Dataset.createVariable(out_oco2, 'level', datatype=np.float32, dimensions=('level'))
    level_out[:] = np.arange(1, 21, 1)

    xco2_out = Dataset.createVariable(out_oco2, 'xco2', datatype=np.float32, dimensions=('Time'))
    xco2_out[:] = tmp_10s_xco2[:num_10s]*10**-6

    # xco2 prior
    xco2_apri_out = Dataset.createVariable(out_oco2, 'xco2_apriori', datatype=np.float32, dimensions=('Time'))
    xco2_apri_out[:] = tmp_10s_xco2_apri[:num_10s]*10**-6

    xco2_uncert_out = Dataset.createVariable(out_oco2, 'xco2_uncert', datatype=np.float32, dimensions=('Time'))
    xco2_uncert_out[:] = tmp_10s_xco2_var[:num_10s]*10**-6

    # lat
    lat_out = Dataset.createVariable(out_oco2, 'latitude', datatype=np.float32, dimensions=('Time'))
    lat_out[:] = tmp_10s_lat[:num_10s]

    lon_out = Dataset.createVariable(out_oco2, 'longitude', datatype=np.float32, dimensions=('Time'))
    lon_out[:] = tmp_10s_lon[:num_10s]

    # pressure
    pressure_out = Dataset.createVariable(out_oco2, 'pressure', datatype=np.float32, dimensions=('Time', 'level'))
    pressure_out[:] = tmp_10s_pres[:num_10s, :]*100

    sup_pressure_out = Dataset.createVariable(out_oco2, 'surface_pressure', datatype=np.float32,dimensions=('Time'))
    sup_pressure_out[:] = tmp_10s_surf[:num_10s]*100

    co2_apri_out = Dataset.createVariable(out_oco2, 'co2_profile_apriori', datatype=np.float32,dimensions=('Time', 'level'))
    co2_apri_out[:] = tmp_10s_co2_apri_profile[:num_10s, :]*10**-6

    xco2_avk_out = Dataset.createVariable(out_oco2, 'xco2_averaging_kernel_matrix', datatype=np.float32,dimensions=('Time', 'level'))
    xco2_avk_out[:] = tmp_10s_xco2_avk[:num_10s, :]

    xco2_pwf_out = Dataset.createVariable(out_oco2, 'xco2_pressure_weighting_function', datatype=np.float32,dimensions=('Time', 'level'))
    xco2_pwf_out[:] = tmp_10s_xco2_pwf[:num_10s, :]

    oco2_cur_time_date += datetime.timedelta(days=1)

pro_end_time = time.time()

print('total running time is:', pro_end_time - pro_begin_time)


num = int(input("Enter a number: "))
if num == 0:
    print("The root of number is 0")

def number_root(n):

    root = 0
    str_num = str(n)

    while root < 1 or root >= 10:

        for char in str_num:
            root += int(char)

        str_num = str(root)

    return root

print("The root of number is {0}".format(number_root(num)))


try:
    from StringIO import StringIO # Can't pickle cStringIO
except ImportError:
    from io import StringIO

def iteritems(d):
    if hasattr(d, 'iteritems'):
        return d.iteritems()
    else:
        return d.items()

def tobytes(s):
    if isinstance(s, type(u'')):
        s = bytes(s, 'latin-1')
    return s


import math
def ispalendrome(n):
    n = str(n)
    return n == n[::-1]

limit = 10**8

squares = []
s = 0
for i in range(int(math.sqrt(limit))+1):
    s += i ** 2
    squares += [s]

s = 0
n = 0
found = []
for i in range(len(squares)):
    for j in range(i-1):
        n = squares[i] - squares[j]
        if ispalendrome(n) and n < limit and n not in found:
            s += n
            found += [n]
print(s, len(found))


#
# @lc app=leetcode id=70 lang=python3
#
# [70] Climbing Stairs
#
class Solution:
    def climbStairs(self, n: int) -> int:
        import sys

        if n == 1:
            return 1

        dp = [sys.maxsize] * (n + 1)
        dp[1], dp[2] = 1, 2

        for i in range(3, n + 1):
            dp[i] = dp[i - 1] + dp[i - 2]

        return dp[n]


from setuptools import setup, find_packages
import versioneer

requirements = [
    'cartopy',
    'pandas',
    'xarray',
    'seaborn',
    'shapely',
    'dask',
    'sentinelsat',
    'toolz'
]

long_description = """
A Python package to download, preprocess and plot data from [Copernicus Open Access Hub](https://scihub.copernicus.eu). This implementation is based on `sentinelsat` [package](https://github.com/sentinelsat/sentinelsat) and the [API Hub Access](https://scihub.copernicus.eu/twiki/do/view/SciHubWebPortal/APIHubDescription) to query the database. The preprocess is made with [HARP tools](https://cdn.rawgit.com/stcorp/harp/master/doc/html/harpconvert.html).
"""

setup(
    name='s5p_tools',
    version=versioneer.get_version(),
    cmdclass=versioneer.get_cmdclass(),
    description="A Python package and set of scripts to download, preprocess and plot data from Sentinel-5P",
    author="Bilel Omrani",
    author_email='bilelomrani1@gmail.com',
    url='https://github.com/bilelomrani1/s5p-tools',
    packages=find_packages(),
    install_requires=requirements,
    keywords='s5p-tools',
    classifiers=[
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8'
    ],
    long_description=long_description,
    long_description_content_type='text/markdown',
    scripts=['s5p_tools/bin/s5p-request'],
    license="MIT"
)


#reading from a csv(comma seperated variable) spreadsheet
#Here the comma is the delimiter, we can have anything
#as a delimiter like space, variable x dots etc

import csv

with open('example.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    '''for row in readCSV:
        print(row)
        print(row[0])
        print(row[0],row[1])'''

#Here row actually means columns

#Data will bear in as string so for doing math
#operations on this data, convert it to int or float

#Say we want to put the dates and color in
#seperate columns.

    dates = []
    colors = []

    for row in readCSV:
        color = row[3]
        date = row[0]

        dates.append(date)
        colors.append(color)

    print(dates)
    print(colors)
#Use this when a user enters a different color
#Try and except is like the last effort to save the program,
#we must try something before it for error handeling

    try:
        whatcolor = input('What color do you wish to know the date of? ')
#Right after this,we must think that if this could
#create a problem?
        if whatcolor in colors:
           coldex =colors.index(whatcolor.lower())
##The .lower makes the upprcase letters to be read as lowercase
           theDate = dates[coldex]
           print('The  date of',whatcolor,'is', theDate)
        else:
           print('Color not found, or is not a color.')

    except Exception as e:
        print(e)

    print('continuing')


import json
from google.cloud import bigquery

def voice_average_score():
    client = bigquery.Client("speech-similarity")

    query_job = client.query(
        """
            select
                OriginalVoiceId, AVG(Score) as avgg
            from
                statistics.recorded_voices
            group by
                OriginalVoiceId
        """
    )

    average_scores = query_job.result()

    result = {}

    for row in average_scores:
        result[row.OriginalVoiceId] = {"avgScore": row.avgg}
    # print("yeeee")
    # print(result)
    return result

def voice_maximum_scorers():
    # TOP = 3
    client = bigquery.Client("speech-similarity")

    query_job = client.query(
        """
            with
              cte
            as
            (
              select
                OriginalVoiceId, UserId, Score, ROW_NUMBER()
              over
              (
                partition by
                  OriginalVoiceId
                order by
                  Score
                desc
              )
              as
                rn
              from
                statistics.recorded_voices
            )
            select
              OriginalVoiceId, UserId, Score, rn
            from
              cte
            where
              rn <= 3
            order by
              OriginalVoiceId, rn
        """
    )

    max_scorers = query_job.result()
    result = {}

    for row in max_scorers:
        orig = row.OriginalVoiceId
        if orig not in result:
            result[orig] = {"maxScorers": []}
        result[orig]["maxScorers"].append(
            {"UserId": row.UserId, "Score": row.Score,}
        )

    return result

def voice_users_tried():
    client = bigquery.Client("speech-similarity")

    query_job = client.query(
        """
            select
              OriginalVoiceId,
              count
              (
                distinct(UserId)
              ) as tried
            from
              statistics.recorded_voices
            group by
              OriginalVoiceId
        """
    )

    users_tried = query_job.result()
    results = {}

    for row in users_tried:
        results[row.OriginalVoiceId] = {"numberTried": row.tried}

    return results

def get_all_voice_statistics(request):
    users_tried = voice_users_tried()
    average_scores = voice_average_score()
    max_scorers = voice_maximum_scorers()

    print(users_tried)
    print(average_scores)
    print(max_scorers)

    result = {}
    for voice_id in users_tried.keys():
        statistics = {
            **users_tried[voice_id],
            **average_scores[voice_id],
            **max_scorers[voice_id],
        }
        result[voice_id] = statistics

    return json.dumps(result)

def get_one_voice_statistics(request):
    request_json = request.get_json(silent=True)
    request_args = request.args
    if request_json and "originalVoiceId" in request_json:
        voiceId = request_json["originalVoiceId"]
    elif request_args and "originalVoiceId" in request_args:
        voiceId = request_args["originalVoiceId"]
    else:
        return (json.dumps({"error": "Missing parameter: originalVoiceId"}), 422, {})

    result = {}
    result.update(voice_users_tried()[str(voiceId)])
    result.update(voice_average_score()[str(voiceId)])
    result.update(voice_maximum_scorers()[str(voiceId)])

    return json.dumps(result)

# if __name__ == '__main__':
#     print(voice_average_score())


import random
import numpy as np
from collections import namedtuple
from game2048.expectimax import board_to_move
from grid_ohe import grid_ohe

Guide = namedtuple('Guide', ('state', 'action'))

class Guides:
    def __init__(self, capacity):
        self.capacity = capacity
        self.memory = []
        self.position = 0

    def push(self, *args):
        if len(self.memory) < self.capacity:
            self.memory.append(None)
        self.memory[self.position] = Guide(*args)
        self.position = (self.position + 1) % self.capacity

    def sample(self, batch_size):
        return random.sample(self.memory, batch_size)

    def ready(self, batch_size):
        return len(self.memory) >= batch_size

    def __len__(self):
        return len(self.memory)

class ModelWrapper:
    def __init__(self, model, capacity):
        self.model = model
        self.memory = Guides(capacity)
        self.training_step = 0

    def predict(self, board):
        return self.model.predict(np.expand_dims(board, axis=0))

    def move(self, game):
        ohe_board = grid_ohe(game.board)
        suggest = board_to_move(game.board)
        direction = self.predict(ohe_board).argmax()
        game.move(direction)
        self.memory.push(ohe_board, suggest)

    def train(self, batch):
        if self.memory.ready(batch):
            guides = self.memory.sample(batch)
            X = []
            Y = []
            for guide in guides:
                X.append(guide.state)
                ohe_action = [0] * 4
                ohe_action[guide.action] = 1
                Y.append(ohe_action)
            loss, acc = self.model.train_on_batch(np.array(X), np.array(Y))
            self.training_step += 1


import argparse
import torch
from tqdm import tqdm
from torch.utils.data import Dataset, DataLoader
import torch.nn.functional as F
import numpy as np
from torch.autograd import Variable

# use gpu if cuda can be detected
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

def evaluate(model, val_dataset, criterion):
    val_loader = DataLoader(val_dataset, batch_size=1, shuffle=False, num_workers=4)
    model.to(device)
    model.eval()
    correct = 0.0
    total = 0.0
    val_loss = 0.0
    for i, (images, label) in enumerate(tqdm(val_loader)):
        images = Variable(images.view(1,1,28,28)).to(device)
        labels = Variable(label).to(device)
        # print(images.shape, labels.shape)
        output = model(images)

        # print(label.size(), output.size())
        loss = criterion(output, labels)
        val_loss += loss.item()

        ## -----------Finding accuracy
        # print(output.shape)
        out = torch.max(output)
        outi = torch.argmax(output)
        # print(out)
        if(out > 0.5):
            pred = 1
            if(labels==outi):
                correct += 1
        else:
            pred = 0

        total += 1

    return correct/total, val_loss/(len(val_loader))


import random

def game():
    los = random.randint(1, 9)
    odp = input('Zgadnij liczbę z przedziału od 1 do 9 lub wpisz "exit", żeby wyjść: ')

    try:
        if odp == 'exit':
            return -1

        if int(odp) == los:
            print('Dokładnie.')
            return 1
        elif int(odp) < los:
            print('Za mało.')
        else:
            print('Za dużo.')

        return 0
    except ValueError:
        game()

def main():
    ileZgad = 0

    while(True):
        odp = int(game())

        if odp == -1:
            print('Zakończono grę. Liczba poprawnych typowań: ', ileZgad)
            break
        else:
            ileZgad = ileZgad + odp

main()