# -*- coding: utf-8 -*-
"""Common test case for all message based resources.

"""
import ctypes
import gc
import logging
import time
from types import ModuleType
from typing import Optional

import pytest

from pyvisa import constants, errors
from pyvisa.constants import EventType, ResourceAttribute
from pyvisa.resources import Resource

from .resource_utils import (
    EventAwareResourceTestCaseMixin,
    LockableResourceTestCaseMixin,
    ResourceTestCase,
)

np: Optional[ModuleType]
try:
    import numpy

    np = numpy
except ImportError:
    np = None

class EventHandler:
    """Event handler."""

    def __init__(self) -> None:
        self.event_success = False
        self.srq_success = False
        self.io_completed = False
        self.handle = None
        self.session = None

    def handle_event(self, session, event_type, event, handle=None):
        """Event handler

        Ctypes handler are expected to return an interger.

        """
        self.session = session
        self.handle = handle
        if event_type == EventType.service_request:
            self.event_success = True
            self.srq_success = True
            return 0
        if event_type == EventType.io_completion:
            self.event_success = True
            self.io_completed = True
            return 0
        else:
            self.event_success = True
            return 0

    def simplified_handler(self, resource, event, handle=None):
        """Simplified handler that can be wrapped."""
        self.session = resource.session
        self.handle = handle
        event_type = event.event_type
        if event_type == EventType.service_request:
            self.event_success = True
            self.srq_success = True
            return None
        elif event_type == EventType.io_completion:
            self.event_success = True
            self.io_completed = True
            return None
        else:
            self.event_success = True
            return None

class MessagebasedResourceTestCase(ResourceTestCase):
    """Base test case for all message based resources."""

    #: Type of resource being tested in this test case.
    #: See RESOURCE_ADDRESSES in the __init__.py file of this package for
    #: acceptable values
    RESOURCE_TYPE = ""

    # Any test involving communication involve to first write to glider the
    # data then request it to send it back

    def setup_method(self):
        """Create a resource using the address matching the type."""
        super().setup_method()
        self.instr.write_termination = "\n"
        self.instr.read_termination = "\n"
        self.instr.timeout = 100

    def compare_user_handle(self, h1, h2):
        """Function comparing to user handle as passed to a callback.

        We need such an indirection because we cannot safely always return
        a Python object and most ctypes object do not compare equal.

        """
        if isinstance(h1, ctypes.Structure):
            return h1 == h2
        elif hasattr(h1, "value"):
            return h1.value == h2.value
        else:  # assume an array
            return all((i == j for i, j in zip(h1, h2)))

    def test_encoding(self):
        """Tets setting the string encoding."""
        assert self.instr.encoding == "ascii"
        self.instr.encoding = "utf-8"

        with pytest.raises(LookupError):
            self.instr.encoding = "test"

    def test_termchars(self):
        """Test modifying the termchars."""
        # Write termination
        self.instr.write_termination = "\r\n"
        assert self.instr.write_termination == "\r\n"

        self.instr.read_termination = "\r\0"
        assert self.instr.read_termination == "\r\0"
        assert self.instr.get_visa_attribute(ResourceAttribute.termchar) == ord("\0")
        assert self.instr.get_visa_attribute(ResourceAttribute.termchar_enabled)

        # Disable read termination
        self.instr.read_termination = None
        assert self.instr.get_visa_attribute(ResourceAttribute.termchar) == ord("\n")
        assert not self.instr.get_visa_attribute(ResourceAttribute.termchar_enabled)

        # Ban repeated term chars
        with pytest.raises(ValueError):
            self.instr.read_termination = "\n\n"

    def test_write_raw_read_bytes(self):
        """Test writing raw data and reading a specific number of bytes."""
        # Reading all bytes at once
        self.instr.write_raw(b"RECEIVE\n")
        self.instr.write_raw(b"test\n")
        count = self.instr.write_raw(b"SEND\n")
        assert count == 5
        self.instr.flush(constants.VI_READ_BUF)
        msg = self.instr.read_bytes(5, chunk_size=2)
        assert msg == b"test\n"

        # Reading one byte at a time
        self.instr.write_raw(b"RECEIVE\n")
        self.instr.write_raw(b"test\n")
        self.instr.write_raw(b"SEND\n")
        for ch in b"test\n":
            assert self.instr.read_bytes(1) == ch.to_bytes(1, "little")

        # Breaking on termchar
        self.instr.read_termination = "\r"
        self.instr.write_raw(b"RECEIVE\n")
        self.instr.write_raw(b"te\rst\r\n")
        self.instr.write_raw(b"SEND\n")
        assert self.instr.read_bytes(100, break_on_termchar=True) == b"te\r"
        assert self.instr.read_bytes(100, break_on_termchar=True) == b"st\r"
        assert self.instr.read_bytes(1) == b"\n"

        # Breaking on end of message
        self.instr.read_termination = "\n"
        self.instr.write_raw(b"RECEIVE\n")
        self.instr.write_raw(b"test\n")
        self.instr.write_raw(b"SEND\n")
        assert self.instr.read_bytes(100, break_on_termchar=True) == b"test\n"

    def test_handling_exception_in_read_bytes(self, caplog):
        """Test handling exception in read_bytes (monkeypatching)"""

        def false_read(session, size):
            raise errors.VisaIOError(constants.VI_ERROR_ABORT)

        read = self.instr.visalib.read
        self.instr.visalib.read = false_read
        with caplog.at_level(logging.DEBUG):
            try:
                self.instr.read_bytes(1)
            except errors.VisaIOError:
                pass
            finally:
                self.instr.visalib.read = read
        assert "- exception while reading:" in caplog.records[1].message

    def test_write_raw_read_raw(self):
        """Test writing raw data and reading an answer."""
        self.instr.write_raw(b"RECEIVE\n")
        self.instr.write_raw(b"test\n")
        self.instr.write_raw(b"SEND\n")
        assert self.instr.read_raw(size=2) == b"test\n"

    def test_clear(self):
        """Test clearing the incoming buffer."""
        self.instr.write_raw(b"RECEIVE\n")
        self.instr.write_raw(b"test\n")
        self.instr.write_raw(b"SEND\n")
        self.instr.clear()
        self.instr.timeout = 10
        with pytest.raises(errors.VisaIOError):
            self.instr.read_raw()

    def test_write_read(self):
        """Test writing and reading."""
        self.instr.write_termination = "\n"
        self.instr.read_termination = "\r\n"
        self.instr.write("RECEIVE")
        with pytest.warns(UserWarning):
            self.instr.write("test\r\n")
        count = self.instr.write("SEND")
        assert count == 5
        assert self.instr.read() == "test"

        # Missing termination chars
        self.instr.read_termination = "\r\n"
        self.instr.write("RECEIVE")
        self.instr.write("test")
        self.instr.write("SEND")
        with pytest.warns(Warning):
            assert self.instr.read() == "test\n"

        # Dynamic termination
        self.instr.write_termination = "\r"
        self.instr.write("RECEIVE\n", termination=False)
        self.instr.write("test\r", termination="\n")
        self.instr.write("SEND", termination="\n")
        assert self.instr.read(termination="\r") == "test"

        # Test query
        self.instr.write_termination = "\n"
        self.instr.write("RECEIVE")
        self.instr.write("test\r")
        tic = time.time()
        assert self.instr.query("SEND", delay=0.5) == "test"
        assert time.time() - tic > 0.49

        # Test handling repeated term char
        self.instr.read_termination = "\n"
        for char in ("\r", None):
            self.instr.write_termination = "\n" if char else "\r"
            self.instr.write("RECEIVE", termination="\n")
            with pytest.warns(Warning):
                self.instr.write("test\r", termination=char)
            self.instr.write("", termination="\n")
            self.instr.write("SEND", termination="\n")
            assert self.instr.read() == "test\r\r"

        # TODO not sure how to test encoding

    def test_handling_exception_in_read_raw(self, caplog):
        """Test handling exception in read_bytes (monkeypatching)"""

        def false_read(session, size):
            raise errors.VisaIOError(constants.VI_ERROR_ABORT)

        read = self.instr.visalib.read
        self.instr.visalib.read = false_read
        with caplog.at_level(logging.DEBUG):
            try:
                self.instr.read()
            except errors.VisaIOError:
                pass
            finally:
                self.instr.visalib.read = read

        assert caplog.records

    def test_write_ascii_values(self):
        """Test writing ascii values."""
        # Standard separator
        values = [1, 2, 3, 4, 5]
        self.instr.write("RECEIVE")
        count = self.instr.write_ascii_values("", values, "d")
        assert count == 10
        self.instr.write("SEND")
        assert self.instr.read() == "1,2,3,4,5"

        # Non standard separator and termination
        self.instr.write_termination = "\r"
        self.instr.write("RECEIVE", termination="\n")
        self.instr.write_ascii_values("", values, "d", separator=";", termination=False)
        self.instr.write("", termination="\n")
        self.instr.write("SEND", termination="\n")
        assert self.instr.read() == "1;2;3;4;5"

        # Test handling repeated term char
        for char in ("\r", None):
            self.instr.write_termination = "\n" if char else "\r"
            self.instr.write("RECEIVE", termination="\n")
            with pytest.warns(Warning):
                values = [1, 2, 3, 4, 5]
                self.instr.write_ascii_values(
                    "\r", values, "s", separator=";", termination=char
                )
            self.instr.write("", termination="\n")
            self.instr.write("SEND", termination="\n")
            assert self.instr.read() == "\r1;2;3;4;5\r"

    @pytest.mark.parametrize(
        "hfmt, prefix",
        list(zip(("ieee", "hp", "empty"), (b"#212", b"#A\x0c\x00", b""))),
    )
    def test_write_binary_values(self, hfmt, prefix):
        """Test writing binary data."""
        values = [1, 2, 3, 4, 5, 6]
        self.instr.write_termination = "\n"
        self.instr.write("RECEIVE")
        count = self.instr.write_binary_values("", values, "h", header_fmt=hfmt)
        # Each interger encoded as h uses 2 bytes
        assert count == len(prefix) + 12 + 1
        self.instr.write("SEND")
        msg = self.instr.read_bytes(13 + len(prefix))
        assert msg == prefix + b"\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\n"

        if hfmt == "hp":
            fl_prefix = prefix[0:2] + prefix[-2::][::-1]
        else:
            fl_prefix = prefix
        self.instr.write_termination = "\r"
        self.instr.write("RECEIVE", termination="\n")
        self.instr.write_binary_values(
            "", values, "h", is_big_endian=True, termination=False, header_fmt=hfmt
        )
        self.instr.write("", termination="\n")
        self.instr.write("SEND", termination="\n")
        assert (
            self.instr.read_bytes(13 + len(prefix))
            == fl_prefix + b"\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\n"
        )

        # Test handling repeated term char
        for char in ("\r", None):
            self.instr.write_termination = "\n" if char else "\r"
            self.instr.write("RECEIVE", termination="\n")
            with pytest.warns(Warning):
                self.instr.write_binary_values(
                    "\r", values, "h", header_fmt=hfmt, termination=char
                )
            self.instr.write("", termination="\n")
            self.instr.write("SEND", termination="\n")
            msg = self.instr.read()
            assert (
                msg
                == "\r"
                + prefix.decode("ascii")
                + "\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\r"
            )

        # Wrong header format
        with pytest.raises(ValueError):
            self.instr.write_binary_values("", values, "h", header_fmt="zxz")

    # Without and with trailing comma
    @pytest.mark.parametrize("msg", ["1,2,3,4,5", "1,2,3,4,5,"])
    def test_read_ascii_values(self, msg):
        """Test reading ascii values."""
        # Standard separator
        self.instr.write("RECEIVE")
        self.instr.write(msg)
        self.instr.write("SEND")
        values = self.instr.read_ascii_values()
        assert type(values[0]) is float
        assert values == [1.0, 2.0, 3.0, 4.0, 5.0]

        # Non standard separator and termination
        self.instr.write("RECEIVE")
        self.instr.write(msg.replace(",", ";"))
        tic = time.time()
        values = self.instr.query_ascii_values(
            "SEND", converter="d", separator=";", delay=0.5
        )
        assert time.time() - tic > 0.5
        assert type(values[0]) is int
        assert values == [1, 2, 3, 4, 5]

        # Numpy container
        if np:
            self.instr.write("RECEIVE")
            self.instr.write(msg)
            self.instr.write("SEND")
            values = self.instr.read_ascii_values(container=np.array)
            expected = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
            assert values.dtype is expected.dtype
            np.testing.assert_array_equal(values, expected)

    @pytest.mark.parametrize("hfmt", ("ieee", "hp"))
    def test_read_binary_values(self, hfmt):
        """Test reading binary data."""
        # TODO test handling binary decoding issue (troublesome)
        self.instr.read_termination = "\r"
        # 3328 in binary short is \x00\r this way we can interrupt the
        # transmission midway to test some corner cases
        data = [1, 2, 3328, 3, 4, 5, 6, 7]

        self.instr.write("RECEIVE")
        self.instr.write_binary_values(
            "", data, "h", header_fmt=hfmt, termination="\r\n"
        )
        self.instr.write("SEND")
        new = self.instr.read_binary_values(
            datatype="h",
            is_big_endian=False,
            header_fmt=hfmt,
            expect_termination=True,
            chunk_size=8,
        )
        self.instr.read_bytes(1)
        assert data == new

        self.instr.write("RECEIVE")
        self.instr.write_binary_values(
            "", data, "h", header_fmt=hfmt, is_big_endian=True
        )

        new = self.instr.query_binary_values(
            "SEND",
            datatype="h",
            header_fmt=hfmt,
            is_big_endian=True,
            expect_termination=False,
            chunk_size=8,
            container=np.array if np else list,
        )
        self.instr.read_bytes(1)
        if np:
            np.testing.assert_array_equal(new, np.array(data, dtype=np.int16))
        else:
            assert data == new

    def test_read_query_binary_values_invalid_header(self):
        """Test we properly handle an invalid header."""
        data = [1, 2, 3328, 3, 4, 5, 6, 7]
        self.instr.write("RECEIVE")
        self.instr.write_binary_values(
            "", data, "h", header_fmt="ieee", is_big_endian=True
        )
        self.instr.write("SEND")
        with pytest.raises(ValueError):
            self.instr.read_binary_values(
                datatype="h",
                is_big_endian=False,
                header_fmt="invalid",
                expect_termination=True,
                chunk_size=8,
            )

        self.instr.write("RECEIVE")
        self.instr.write_binary_values(
            "", data, "h", header_fmt="ieee", is_big_endian=True
        )
        with pytest.raises(ValueError):
            self.instr.query_binary_values(
                "*IDN",
                datatype="h",
                is_big_endian=False,
                header_fmt="invalid",
                expect_termination=True,
                chunk_size=8,
            )

    # Not sure how to test this
    @pytest.mark.skip
    def test_handling_malformed_binary(self):
        """ """
        pass

    @pytest.mark.parametrize("hfmt, header", list(zip(("ieee", "empty"), ("#0", ""))))
    def test_read_binary_values_unreported_length(self, hfmt, header):
        """Test reading binary data."""
        self.instr.read_termination = "\r"
        # 3328 in binary short is \x00\r this way we can interrupt the
        # transmission midway to test some corner cases
        data = [1, 2, 3328, 3, 4, 5]

        self.instr.write("RECEIVE")
        self.instr.write(
            header + "\x01\x00\x02\x00\x00\r\x03\x00\x04\x00\x05\x00",
            termination="\r\n",
        )
        self.instr.write("SEND")
        new = self.instr.read_binary_values(
            datatype="h",
            is_big_endian=False,
            header_fmt=hfmt,
            expect_termination=True,
            chunk_size=6,
            data_points=6,
        )
        self.instr.read_bytes(1)
        assert data == new

        self.instr.write("RECEIVE")
        self.instr.write(
            header + "\x00\x01\x00\x02\r\x00\x00\x03\x00\x04\x00\x05",
            termination="\r\n",
        )
        new = self.instr.query_binary_values(
            "SEND",
            datatype="h",
            header_fmt=hfmt,
            is_big_endian=True,
            expect_termination=False,
            chunk_size=6,
            container=np.array if np else list,
            data_points=6,
        )
        self.instr.read_bytes(1)
        if np:
            np.testing.assert_array_equal(new, np.array(data, dtype=np.int16))
        else:
            assert data == new

        # Check we do error on unreported/unspecified length
        self.instr.write("RECEIVE")
        self.instr.write(
            header + "\x01\x00\x02\x00\x00\r\x03\x00\x04\x00\x05\x00",
            termination="\r\n",
        )
        self.instr.write("SEND")
        with pytest.raises(ValueError):
            self.instr.read_binary_values(
                datatype="h",
                is_big_endian=False,
                header_fmt=hfmt,
                expect_termination=True,
                chunk_size=6,
            )

    def test_read_binary_values_empty(self):
        """Test reading binary data."""
        self.instr.write("RECEIVE")
        self.instr.write("#10")
        self.instr.write("SEND")
        new = self.instr.read_binary_values(
            datatype="h",
            is_big_endian=False,
            header_fmt="ieee",
            expect_termination=True,
            chunk_size=6,
        )
        assert not new

        if np:
            self.instr.write("RECEIVE")
            self.instr.write(
                "#10",
                termination="\n",
            )
            new = self.instr.query_binary_values(
                "SEND",
                datatype="h",
                header_fmt="ieee",
                is_big_endian=True,
                expect_termination=False,
                chunk_size=6,
                container=np.array if np else list,
            )

            assert not new.size if np else not new

    def test_delay_in_query_ascii(self):
        """Test handling of the delay argument in query_ascii_values."""
        # Test using the instrument wide delay
        self.instr.query_delay = 1.0
        self.instr.write("RECEIVE")
        self.instr.write("1,2,3,4,5")
        tic = time.perf_counter()
        values = self.instr.query_ascii_values("SEND")
        assert time.perf_counter() - tic > 0.99
        assert type(values[0]) is float
        assert values == [1.0, 2.0, 3.0, 4.0, 5.0]

        # Test specifying the delay
        self.instr.query_delay = 0.0
        self.instr.write("RECEIVE")
        self.instr.write("1,2,3,4,5")
        tic = time.perf_counter()
        values = self.instr.query_ascii_values("SEND", delay=1.0)
        assert time.perf_counter() - tic > 0.99
        assert type(values[0]) is float
        assert values == [1.0, 2.0, 3.0, 4.0, 5.0]

        # Test specifying a 0 delay
        self.instr.query_delay = 1.0
        self.instr.write("RECEIVE")
        self.instr.write("1,2,3,4,5")
        tic = time.perf_counter()
        values = self.instr.query_ascii_values("SEND", delay=0.0)
        assert time.perf_counter() - tic < 0.99
        assert type(values[0]) is float
        assert values == [1.0, 2.0, 3.0, 4.0, 5.0]

    def test_instrument_wide_delay_in_query_binary(self):
        """Test handling delay in query_ascii_values."""
        header = "#0"
        data = [1, 2, 3328, 3, 4, 5]
        # Test using the instrument wide delay
        self.instr.query_delay = 1.0
        self.instr.write("RECEIVE")
        self.instr.write(
            header + "\x00\x01\x00\x02\r\x00\x00\x03\x00\x04\x00\x05",
            termination="\r\n",
        )
        tic = time.perf_counter()
        new = self.instr.query_binary_values(
            "SEND",
            datatype="h",
            header_fmt="ieee",
            is_big_endian=True,
            expect_termination=False,
            chunk_size=6,
            data_points=6,
        )

        assert time.perf_counter() - tic > 0.99
        assert data == new

    def test_delay_args_in_query_binary(self):
        """Test handling of the delay argument in query_ascii_values."""
        header = "#0"
        data = [1, 2, 3328, 3, 4, 5]
        self.instr.query_delay = 0.0
        self.instr.write("RECEIVE")
        self.instr.write(
            header + "\x00\x01\x00\x02\r\x00\x00\x03\x00\x04\x00\x05",
            termination="\r\n",
        )
        tic = time.perf_counter()
        new = self.instr.query_binary_values(
            "SEND",
            datatype="h",
            header_fmt="ieee",
            is_big_endian=True,
            expect_termination=False,
            chunk_size=6,
            data_points=6,
            delay=1.0,
        )

        assert time.perf_counter() - tic > 0.99
        assert data == new

    def test_no_delay_args_in_query_binary(self):
        """Test handling of the delay argument in query_ascii_values."""
        header = "#0"
        data = [1, 2, 3328, 3, 4, 5]
        self.instr.query_delay = 1.0
        self.instr.write("RECEIVE")
        self.instr.write(
            header + "\x00\x01\x00\x02\r\x00\x00\x03\x00\x04\x00\x05",
            termination="\r\n",
        )
        tic = time.perf_counter()
        new = self.instr.query_binary_values(
            "SEND",
            datatype="h",
            header_fmt="ieee",
            is_big_endian=True,
            expect_termination=False,
            chunk_size=6,
            data_points=6,
            delay=0.0,
        )

        assert time.perf_counter() - tic < 1.0
        assert data == new

    def test_stb(self):
        """Test reading the status byte."""
        assert 0 <= self.instr.stb <= 256
        assert 0 <= self.instr.read_stb() <= 256

class EventAwareMessagebasedResourceTestCaseMixin(EventAwareResourceTestCaseMixin):
    """Mixin for message based resources supporting events."""

    def test_manually_called_handlers(self):
        """Test calling manually even handler."""

        class FalseResource(Resource):
            session = None
            visalib = None
            _session = None

            def __init__(self):
                pass

        fres = FalseResource()
        fres2 = FalseResource()
        fres2.session = 1

        handler = EventHandler()
        false_wrapped_handler = fres.wrap_handler(handler.simplified_handler)
        false_wrapped_handler(None, EventType.clear, 1, 1)
        assert handler.event_success

        with pytest.raises(RuntimeError):
            false_wrapped_handler(1, EventType.clear, 1, 1)

    def test_handling_invalid_handler(self):
        """Test handling an error related to a wrong handler type."""
        with pytest.raises(errors.VisaTypeError):
            event_type = EventType.exception
            self.instr.install_handler(event_type, 1, object())

    def test_uninstalling_missing_visa_handler(self):
        """Test uninstalling a visa handler that was not registered."""
        handler1 = EventHandler()
        handler2 = EventHandler()
        event_type = EventType.exception
        self.instr.install_handler(event_type, handler1.handle_event)
        with pytest.raises(errors.UnknownHandler):
            self.instr.uninstall_handler(event_type, handler2.handle_event)

        self.instr.uninstall_handler(event_type, handler1.handle_event)

        with pytest.raises(errors.UnknownHandler):
            self.instr.uninstall_handler(event_type, handler2.handle_event)

    def test_handler_clean_up_on_resource_del(self):
        """Test that handlers are properly cleaned when a resource is deleted."""
        handler = EventHandler()
        event_type = EventType.exception
        self.instr.install_handler(event_type, handler.handle_event)

        self.instr = None
        gc.collect()
        assert not self.rm.visalib.handlers

    def test_uninstall_all_handlers(self):
        """Test uninstall all handlers from all sessions."""
        handler = EventHandler()
        event_type = EventType.exception
        self.instr.install_handler(event_type, handler.handle_event)

        self.rm.visalib.uninstall_all_visa_handlers(None)
        assert not self.rm.visalib.handlers

    def test_manual_async_read(self):
        """Test handling IOCompletion event which has extra attributes."""
        # Prepare message
        self.instr.write_raw(b"RECEIVE\n")
        self.instr.write_raw(b"test\n")
        self.instr.write_raw(b"SEND\n")

        # Enable event handling
        event_type = EventType.io_completion
        event_mech = constants.EventMechanism.queue
        wait_time = 2000  # set time that program waits to receive event
        self.instr.enable_event(event_type, event_mech, None)

        try:
            visalib = self.instr.visalib
            buffer, job_id, status_code = visalib.read_asynchronously(
                self.instr.session, 10
            )
            assert buffer is visalib.get_buffer_from_id(job_id)
            response = self.instr.wait_on_event(event_type, wait_time)
        finally:
            self.instr.disable_event(event_type, event_mech)

        assert response.event.status == constants.StatusCode.success
        assert bytes(buffer) == bytes(response.event.buffer)
        assert bytes(response.event.data) == b"test\n"
        assert response.event.return_count == 5
        assert response.event.operation_name == "viReadAsync"

    def test_getting_unknown_buffer(self):
        """Test getting a buffer with a wrong ID."""
        assert self.instr.visalib.get_buffer_from_id(1) is None

    def test_wait_on_event_timeout(self):
        """Test waiting on a VISA event."""
        event_type = EventType.service_request
        event_mech = constants.EventMechanism.queue
        # Emit a clear to avoid dealing with previous requests
        self.instr.clear()
        self.instr.enable_event(event_type, event_mech, None)
        try:
            response = self.instr.wait_on_event(event_type, 10, capture_timeout=True)
        finally:
            self.instr.disable_event(event_type, event_mech)
        assert response.timed_out
        assert response.event.event_type == event_type

        with pytest.raises(errors.VisaIOError):
            self.instr.enable_event(event_type, event_mech, None)
            try:
                response = self.instr.wait_on_event(event_type, 10)
            finally:
                self.instr.disable_event(event_type, event_mech)

    def test_wait_on_event(self):
        """Test waiting on a VISA event."""
        event_type = EventType.service_request
        event_mech = constants.EventMechanism.queue
        wait_time = 2000  # set time that program waits to receive event
        self.instr.enable_event(event_type, event_mech, None)
        self.instr.write("RCVSLOWSRQ")
        self.instr.write("1")
        self.instr.write("SENDSLOWSRQ")
        try:
            response = self.instr.wait_on_event(event_type, wait_time)
        finally:
            self.instr.disable_event(event_type, event_mech)

        assert not response.timed_out
        assert response.event.event_type == EventType.service_request
        assert self.instr.read() == "1"

    def test_managing_visa_handler(self):
        """Test using visa handlers."""

        def _test(handle):
            handler = EventHandler()
            event_type = EventType.service_request
            event_mech = constants.EventMechanism.handler
            user_handle = self.instr.install_handler(
                event_type, handler.handle_event, user_handle=handle
            )
            self.instr.enable_event(event_type, event_mech, None)
            self.instr.write("RCVSLOWSRQ")
            self.instr.write("1")
            self.instr.write("SENDSLOWSRQ")

            try:
                t1 = time.time()
                while not handler.event_success:
                    if (time.time() - t1) > 2:
                        break
                    time.sleep(0.1)
            finally:
                self.instr.disable_event(event_type, event_mech)
                self.instr.uninstall_handler(
                    event_type, handler.handle_event, user_handle
                )

            assert handler.session == self.instr.session
            assert self.compare_user_handle(handler.handle, user_handle)
            assert handler.srq_success
            assert self.instr.read() == "1"
            self.instr.clear()

        class Point(ctypes.Structure):
            _fields_ = [("x", ctypes.c_int), ("y", ctypes.c_int)]

            def __eq__(self, other):
                if type(self) is not type(other):
                    return False
                return self.x == other.x and self.y == other.y

        for handle in (1, 1.0, "1", [1], [1.0], Point(1, 2)):
            print(handle)
            _test(handle)

    def test_wrapping_handler(self):
        """Test wrapping a handler using a Resource."""

        handler = EventHandler()
        event_type = EventType.service_request
        event_mech = constants.EventMechanism.handler
        wrapped_handler = self.instr.wrap_handler(handler.simplified_handler)
        user_handle = self.instr.install_handler(event_type, wrapped_handler, 1)
        self.instr.enable_event(event_type, event_mech, None)
        self.instr.write("RCVSLOWSRQ")
        self.instr.write("1")
        self.instr.write("SENDSLOWSRQ")

        try:
            t1 = time.time()
            while not handler.event_success:
                if (time.time() - t1) > 2:
                    break
                time.sleep(0.1)
        finally:
            self.instr.disable_event(event_type, event_mech)
            self.instr.uninstall_handler(event_type, wrapped_handler, user_handle)

        assert self.instr.session == handler.session
        assert self.compare_user_handle(handler.handle, user_handle)
        assert handler.srq_success
        assert self.instr.read() == "1"

    def test_bare_handler(self):
        """Test using a bare handler passing raw backend values."""
        from pyvisa import ctwrapper

        if not isinstance(self.instr.visalib, ctwrapper.IVIVisaLibrary):
            return

        ctwrapper.WRAP_HANDLER = False
        try:
            handler = EventHandler()
            event_type = EventType.service_request
            event_mech = constants.EventMechanism.handler
            user_handle = self.instr.install_handler(
                event_type, handler.handle_event, 1
            )
            self.instr.enable_event(event_type, event_mech, None)
            self.instr.write("RCVSLOWSRQ")
            self.instr.write("1")
            self.instr.write("SENDSLOWSRQ")

            try:
                t1 = time.time()
                while not handler.event_success:
                    if (time.time() - t1) > 2:
                        break
                    time.sleep(0.1)
            finally:
                self.instr.disable_event(event_type, event_mech)
                self.instr.uninstall_handler(
                    event_type, handler.handle_event, user_handle
                )

            assert self.instr.session == handler.session.value
            assert self.compare_user_handle(handler.handle.contents, user_handle)
            assert handler.srq_success
            assert self.instr.read() == "1"
        finally:
            ctwrapper.WRAP_HANDLER = True

class LockableMessagedBasedResourceTestCaseMixin(LockableResourceTestCaseMixin):
    """Mixing for message based resources supporting locking."""

    def test_shared_locking(self):
        """Test locking/unlocking a resource."""
        instr2 = self.rm.open_resource(str(self.rname))
        instr3 = self.rm.open_resource(str(self.rname))

        key = self.instr.lock()
        instr2.lock(requested_key=key)

        assert self.instr.query("*IDN?")
        assert instr2.query("*IDN?")
        with pytest.raises(errors.VisaIOError):
            instr3.query("*IDN?")

        # Share the lock for a limited time
        with instr3.lock_context(requested_key=key) as key2:
            assert instr3.query("*IDN?")
            assert key == key2

        # Stop sharing the lock
        instr2.unlock()

        with pytest.raises(errors.VisaIOError):
            instr2.query("*IDN?")
        with pytest.raises(errors.VisaIOError):
            instr3.query("*IDN?")

        self.instr.unlock()

        assert instr3.query("*IDN?")

    def test_exclusive_locking(self):
        """Test locking/unlocking a resource."""
        instr2 = self.rm.open_resource(str(self.rname))

        self.instr.lock_excl()
        with pytest.raises(errors.VisaIOError):
            instr2.query("*IDN?")

        self.instr.unlock()

        assert instr2.query("*IDN?")

        # Share the lock for a limited time
        with self.instr.lock_context(requested_key="exclusive") as key:
            assert key is None
            with pytest.raises(errors.VisaIOError):
                instr2.query("*IDN?")


import pandas as pd

if __name__ == '__main__':
    # 读取文件  参数1 文件的位置  可以是绝对或者相对路径   \表示转移
    # 附加,计算这行代码需要的时间
    df = pd.read_excel("./Online Retail.xlsx")
    # 清洗数据   找到需要的数据,移除不需要的数据
    # 清除带空值的行   how的值   any表示表示清除带空值的行, all表示所有是空值的情况下,再清除
    df.dropna(how='any')
    df['InvoiceDate'] = pd.to_datetime(df['InvoiceDate'], format='%Y-%m-%d')
    # 根据单价和数量,求价格
    df["Price"] = df['Quantity'] * df['UnitPrice']
    # 查看前几行数据 默认显示5行,可以指定行数
    # head = df.head(10)
    # print(head)
    #
    # 数据分析
    head10 = df[df['Quantity'] > 0].groupby('Country')['Quantity'].sum().sort_values(ascending=False).head(10)
    print(head10)

    # 求客单价  总价/总数量
    # 总价
    sum_all = df[df['Quantity'] > 0]['Price'].sum()
    # 总数量
    count = df[df['Quantity'] > 0]['InvoiceNo'].count()
    price_invoice = sum_all / count
    print("客单价是" + str(price_invoice))
    # 附加:用户消费行为分析 参考P41


import numpy as np
import pygplates
from scipy import spatial

def marsaglias_method(N):

    ## Marsaglia's method
    dim = 3

    norm = np.random.normal
    normal_deviates = norm(size=(dim, N))

    radius = np.sqrt((normal_deviates**2).sum(axis=0))
    points = normal_deviates/radius

    return points

def random_points_on_sphere(N):
# function to call Marsaglia's method and return Long/
# Lat arrays

    points = marsaglias_method(N)

    Long=[]
    Lat=[]
    for xyz in points.T:
        LL = pygplates.PointOnSphere((xyz))
        Lat.append(LL.to_lat_lon()[0])
        Long.append(LL.to_lat_lon()[1])

    return np.array(Long), np.array(Lat)

def random_points_feature(N,filename=None):
# function to call Marsaglia's method and return
# feature collection or save to file

    points = marsaglias_method(N)

    #multipoint = pygplates.MultiPointOnSphere((points.T))
    multipoint_feature = pygplates.Feature()
    multipoint_feature.set_geometry(pygplates.MultiPointOnSphere((points.T)))
    multipoint_feature.set_name('Random Points from Marsaglia''s method')

    multipoint_feature_collection = pygplates.FeatureCollection(multipoint_feature)

    if filename is not None:
        multipoint_feature_collection.write(filename)
    else:
        return multipoint_feature_collection

def rtp2xyz(r, theta, phi):
    # if only one value, shape will be empty, hence the next if statement
    if r.size==1:
        rdim=1
    else:
        rdim = r.shape[0]
    rst = r * np.sin(theta)
    xout = np.zeros((rdim,3))
    xout[:,0] = rst * np.cos(phi)       # x
    xout[:,1] = rst * np.sin(phi)       # y
    xout[:,2] = r * np.cos(theta)       # z

    return xout

def create_tree_for_spherical_data(inputLats, inputLons, inputVals, n=16):

    ithetas = np.radians(90-inputLats)
    iphis   = np.radians(inputLons)
    irs     = np.ones(np.shape(ithetas))
    nodes = []

    ixyzs=rtp2xyz(irs.ravel(), ithetas.ravel(), iphis.ravel())
    tree = spatial.cKDTree(ixyzs, n)

    return tree

def sampleOnSphere(inputLats, inputLons, inputVals, othetas, ophis, tree=None, n=16):

    if (tree is None):
        tree = create_tree_for_spherical_data(inputLats, inputLons, inputVals)

    othetas = np.radians(90-othetas)
    ophis   = np.radians(ophis)
    oxyzs=rtp2xyz(np.ones(np.shape(othetas)), othetas, ophis)

    d,l = tree.query(oxyzs)

    return d,l


from telemetry.page import page as page_module
from telemetry.page import shared_page_state

class KeyMobileSitesPage(page_module.Page):

  def __init__(self, url, page_set, name='', labels=None):
    super(KeyMobileSitesPage, self).__init__(
        url=url, page_set=page_set, name=name,
        shared_page_state_class=shared_page_state.SharedMobilePageState,
        credentials_path='data/credentials.json', labels=labels)
    self.archive_data_file = 'data/key_mobile_sites.json'

class CapitolVolkswagenPage(KeyMobileSitesPage):

  """ Why: Typical mobile business site """

  def __init__(self, page_set):
    super(CapitolVolkswagenPage, self).__init__(
      url=('http://iphone.capitolvolkswagen.com/index.htm'
           '#new-inventory_p_2Fsb-new_p_2Ehtm_p_3Freset_p_3DInventoryListing'),
      page_set=page_set)

  def RunNavigateSteps(self, action_runner):
    super(CapitolVolkswagenPage, self).RunNavigateSteps(action_runner)
    action_runner.WaitForElement(text='Next 35')
    action_runner.WaitForJavaScriptCondition(
        'document.body.scrollHeight > 2560')

class TheVergeArticlePage(KeyMobileSitesPage):

  """ Why: Top tech blog """

  def __init__(self, page_set):
    super(TheVergeArticlePage, self).__init__(
      # pylint: disable=line-too-long
      url='http://www.theverge.com/2012/10/28/3568746/amazon-7-inch-fire-hd-ipad-mini-ad-ballsy',
      page_set=page_set)

  def RunNavigateSteps(self, action_runner):
    super(TheVergeArticlePage, self).RunNavigateSteps(action_runner)
    action_runner.WaitForJavaScriptCondition(
        'window.Chorus !== undefined &&'
        'window.Chorus.Comments !== undefined &&'
        'window.Chorus.Comments.Json !== undefined &&'
        '(window.Chorus.Comments.loaded ||'
        ' window.Chorus.Comments.Json.load_comments())')

class CnnArticlePage(KeyMobileSitesPage):

  """ Why: Top news site """

  def __init__(self, page_set):
    super(CnnArticlePage, self).__init__(
      # pylint: disable=line-too-long
      url='http://www.cnn.com/2012/10/03/politics/michelle-obama-debate/index.html',
      page_set=page_set)

  def RunNavigateSteps(self, action_runner):
    super(CnnArticlePage, self).RunNavigateSteps(action_runner)
    action_runner.Wait(8)

class FacebookPage(KeyMobileSitesPage):

  """ Why: #1 (Alexa global) """

  def __init__(self, page_set):
    super(FacebookPage, self).__init__(
      url='https://facebook.com/barackobama',
      page_set=page_set)

  def RunNavigateSteps(self, action_runner):
    super(FacebookPage, self).RunNavigateSteps(action_runner)
    action_runner.WaitForJavaScriptCondition(
        'document.getElementById("u_0_c") !== null &&'
        'document.body.scrollHeight > window.innerHeight')

class YoutubeMobilePage(KeyMobileSitesPage):

  """ Why: #3 (Alexa global) """

  def __init__(self, page_set):
    super(YoutubeMobilePage, self).__init__(
      url='http://m.youtube.com/watch?v=9hBpF_Zj4OA',
      page_set=page_set)

  def RunNavigateSteps(self, action_runner):
    super(YoutubeMobilePage, self).RunNavigateSteps(action_runner)
    action_runner.WaitForJavaScriptCondition(
        'document.getElementById("paginatortarget") !== null')

class LinkedInPage(KeyMobileSitesPage):

  """ Why: #12 (Alexa global),Public profile """

  def __init__(self, page_set):
    super(LinkedInPage, self).__init__(
      url='https://www.linkedin.com/in/linustorvalds',
      page_set=page_set,
      name='LinkedIn')

  def RunNavigateSteps(self, action_runner):
    super(LinkedInPage, self).RunNavigateSteps(action_runner)
    action_runner.WaitForJavaScriptCondition(
        'document.getElementById("profile-view-scroller") !== null')

class YahooAnswersPage(KeyMobileSitesPage):

  """ Why: #1 Alexa reference """

  def __init__(self, page_set):
    super(YahooAnswersPage, self).__init__(
      # pylint: disable=line-too-long
      url='http://answers.yahoo.com/question/index?qid=20110117024343AAopj8f',
      page_set=page_set)

  def RunNavigateSteps(self, action_runner):
    super(YahooAnswersPage, self).RunNavigateSteps(action_runner)
    action_runner.WaitForElement(text='Other Answers (1 - 20 of 149)')
    action_runner.ClickElement(text='Other Answers (1 - 20 of 149)')

class GmailPage(KeyMobileSitesPage):

  """ Why: productivity, top google properties """

  def __init__(self, page_set):
    super(GmailPage, self).__init__(
      url='https://mail.google.com/mail/',
      page_set=page_set)

    self.credentials = 'google'

  def RunNavigateSteps(self, action_runner):
    super(GmailPage, self).RunNavigateSteps(action_runner)
    action_runner.WaitForJavaScriptCondition(
        'document.getElementById("og_user_warning") !== null')
    action_runner.WaitForJavaScriptCondition(
        'document.getElementById("og_user_warning") === null')

class GroupClonedPage(KeyMobileSitesPage):

  """ Why: crbug.com/172906 """

  def __init__(self, page_set):
    super(GroupClonedPage, self).__init__(
      url='http://groupcloned.com',
      page_set=page_set)

  def RunNavigateSteps(self, action_runner):
    super(GroupClonedPage, self).RunNavigateSteps(action_runner)
    action_runner.Wait(5)
    action_runner.WaitForJavaScriptCondition('''
        document.getElementById("element-19") !== null &&
        document.getElementById("element-19").contentDocument
          .getElementById("element-22") !== null &&
        document.getElementById("element-19").contentDocument
          .getElementsByClassName(
              "container list-item gc-list-item stretched").length !== 0''')

class GroupClonedListImagesPage(KeyMobileSitesPage):

  """ Why: crbug.com/172906 """

  def __init__(self, page_set):
    super(GroupClonedListImagesPage, self).__init__(
      url='http://groupcloned.com/test/list-images-variable/index.html',
      page_set=page_set)

  def RunNavigateSteps(self, action_runner):
    super(GroupClonedListImagesPage, self).RunNavigateSteps(action_runner)
    action_runner.WaitForJavaScriptCondition(
        'document.getElementById("element-5") !== null')

class GoogleNewsMobilePage(KeyMobileSitesPage):

  """ Why: Google News: accelerated scrolling version """

  def __init__(self, page_set):
    super(GoogleNewsMobilePage, self).__init__(
      url='http://mobile-news.sandbox.google.com/news/pt1',
      page_set=page_set)

  def RunNavigateSteps(self, action_runner):
    super(GoogleNewsMobilePage, self).RunNavigateSteps(action_runner)
    action_runner.WaitForJavaScriptCondition(
        'typeof NEWS_telemetryReady !== "undefined" && '
        'NEWS_telemetryReady == true')

class GoogleNewsMobile2Page(KeyMobileSitesPage):

  """
  Why: Google News: this iOS version is slower than accelerated scrolling
  """

  def __init__(self, page_set):
    super(GoogleNewsMobile2Page, self).__init__(
      url='http://mobile-news.sandbox.google.com/news/pt0',
      page_set=page_set)

  def RunNavigateSteps(self, action_runner):
    super(GoogleNewsMobile2Page, self).RunNavigateSteps(action_runner)
    action_runner.WaitForJavaScriptCondition(
        'document.getElementById(":h") != null')
    action_runner.Wait(1)

class AmazonNicolasCagePage(KeyMobileSitesPage):

  """
  Why: #1 world commerce website by visits; #3 commerce in the US by time spent
  """

  def __init__(self, page_set):
    super(AmazonNicolasCagePage, self).__init__(
      url='http://www.amazon.com/gp/aw/s/ref=is_box_?k=nicolas+cage',
      page_set=page_set)


import socket
# Server's address
HOST = "localhost"
PORT = 8081
c_sock = socket.socket()

c_sock.connect((HOST, PORT))

c_sock.sendall(b"Hello, world!")
response = c_sock.recv(1024)

print(response)

c_sock.close()


# coding=utf-8

"""
Time : 2016/7/29 16:18
Author : Jia Jielin
Company: fhhy.co
File : fhConstant.py
Description:

"""

# system module

# third party module

# own module

# 任务状态
STATUS_NOTTRADED = '未成交'
# STATUS_PARTTRADED = '部分成交'
STATUS_ALLTRADED = '已成交'
STATUS_CANCELLED = '已撤销'
# STATUS_PARTCANCELLED = '部分成交并撤销'
# STATUS_TRADEDCANCELLED = '全部成交并撤销'

# 状态操作
OPTION_CANCEL = 'optionCancel'
OPTION_TRADED = 'optionTraded'
# OPTION_PARTTRADED = 'optionPartTraded'
OPTION_NOTTRADED = 'optionNotTraded'
OPTION_REVOKECANCEL = 'optionRevokeCancel'

# 交易所类型
EXCHANGE_SSE = 'SE'        # 上交所
EXCHANGE_SZSE = 'SZ'      # 深交所
EXCHANGE_CFFEX = 'CFFEX'    # 中金所
EXCHANGE_SHFE = 'SHFE'      # 上期所
EXCHANGE_CZCE = 'CZCE'      # 郑商所
EXCHANGE_DCE = 'DCE'        # 大商所
EXCHANGE_SGE = 'SGE'        # 上金所
EXCHANGE_UNKNOWN = 'UNKNOWN'# 未知交易所
EXCHANGE_NONE = ''          # 空交易所
EXCHANGE_HKEX = 'HKEX'      # 港交所

# 中文字段
POSITION_ON = '开仓'
POSITION_OFF = '平仓'
TRADE_BUY = '买入'
TRADE_SELL = '卖出'

# 默认空值
EMPTY_STRING = ''
EMPTY_INT = 0
EMPTY_UNICODE = u''
EMPTY_FLOAT = 0.0

# 数据库插入数值限制
DB_VOLUME_MAX = 2147483647
DB_PRICE_PREC = 4
DB_PRICE_LONGTH = 8

# 用户验证结果/相关人员权限
VERIFY_NOUSER = 999
VERIFY_ADMIN = 0
VERIFY_FUNDMANAGER = 1
VERIFY_TRADER = 2
VERIFY_DATAMANAGER = 3
VERIFY_INVESTMANAGER = 4

VERIFY_COMMENT = {}
VERIFY_COMMENT[VERIFY_ADMIN] = '管理员'
VERIFY_COMMENT[VERIFY_FUNDMANAGER] = '基金经理'
VERIFY_COMMENT[VERIFY_TRADER] = '交易员'
VERIFY_COMMENT[VERIFY_DATAMANAGER] = '数据管理员'
VERIFY_COMMENT[VERIFY_NOUSER] = '游客'
VERIFY_COMMENT[None] = '非标准用户'
VERIFY_COMMENT[VERIFY_INVESTMANAGER] = '投资总监'

# 界面设置选项
SETTING_DEFAUlT = u'默认'
SETTING_MYSELF = u'护眼'

# 数据库连接
DB_DISCONNECT = 1000

# 产品状态及产品操作
HOME_PAGE = '首页'
PRODUCT_ON = '运作中'
PRODUCT_OFF = '已结束'

# 建议
SUGGEST_HOLD = '持有'
SUGGEST_BUY = '买入'
SUGGEST_SELL = '卖出'

PRODUCT_VIEW = 0
PRODUCT_ADD = 1
PRODUCT_MOD = 2
PRODUCT_DELETE = 3

# 布局相关数据
TRADE_LEFT_MAX = 350
TRADE_LEFT_MIN = 280

LOG_RIGHT_MAX = 300
LOG_RIGHT_MIN = 200

# 股票交易成本
SEC_BUY_COST = 0.0003
SEC_SELL_COST = 0.0013


#어른상어 2시
from collections import defaultdict,deque

global n,m,k,g,shark,sdir,smell

# INITIALIZE
shark = defaultdict(list)
dir = [(-1,0), (1,0), (0,-1), (0,1)] # 위, 아래, 왼쪽, 오른쪽
n,m,k = map(int ,input().split())
gg = [[*map(int,input().split())] for _ in range(n)]
g = [[[0,0]  for _ in range(n)] for _ in range(n)]
initial = [*map(int, input().split())]
sdir = defaultdict(list)
smell = deque()
for mm in range(m):
    for i in range(4):
        l = [*map(int, input().split())]
        sdir[mm+1].append( (l[0]-1, l[1]-1 , l[2]-1, l[3]-1) )
for y in range(n):
    for x in range(n):
        if gg[y][x] != 0 :
            shark[ gg[y][x] ] = [y,x,initial[ gg[y][x]-1 ] -1 ]
            g[y][x] = [k, gg[y][x]]
            smell.append((y,x))

# 상어 이동
def sharkmove():
    global n, m, k, g, shark, sdir, smell
    sg = [[0] * n for _ in range(n)]
    keylist = list( shark.keys() )
    neww = deque()

    for snum in keylist:
        y,x,sd = shark[snum]
        yxd = [[-1,-1,-1] for _ in range(2) ]

        for d in sdir[snum][sd]:
            ny,nx = y+dir[d][0], x+dir[d][1]
            if ny<0 or nx<0 or ny>= n or nx>=n: continue
            if g[ny][nx][0] == 0 or (ny, nx) in neww  : yxd[0] = [ny, nx, d]; break
            elif  g[ny][nx][0] >0 and g[ny][nx][1] == snum:
                if yxd[1][0]==-1: yxd[1] = [ny,nx,d]

        for i in range(2):
            yy,xx,dd = yxd[i]
            if yy > -1:
                if sg[yy][xx]>0 and sg[yy][xx] < snum: del shark[snum]
                else:
                    if sg[yy][xx] > 0 and sg[yy][xx] > snum : del shark[sg[yy][xx]] # 다른 상어 잡아먹기
                    g[yy][xx] = [k+1, snum]
                    if not (yy,xx) in smell : smell.append( (yy,xx) )
                    shark[snum] = [yy,xx,dd]
                    sg[yy][xx] = snum
                    if i == 0: neww.append((yy,xx))
                break

for time in range(1000) :
    sharkmove()

    # 냄새 1씩 줄이기
    if smell :
        pre = smell.copy()
        for l in pre:
            y,x = l
            if g[y][x][0] >0 : g[y][x] = [g[y][x][0]-1, g[y][x][1] ]
            if g[y][x][0] == 0: smell.remove((y,x)); g[y][x] = [0 , 0]

    if len(shark) == 1: print(time+1);exit(0)
print(-1)


import pyarrow as pa

from petastorm.local_disk_cache import LocalDiskCache

class LocalDiskArrowTableCache(LocalDiskCache):
    """A disk cache implementation """
    def __init__(self, *args, **kwargs):
        super(LocalDiskArrowTableCache, self).__init__(*args, **kwargs)
        # Workaround for https://issues.apache.org/jira/browse/ARROW-5260
        # unless we try to serialize something before deserialize_components is called, we would crash with a sigsegv
        pa.serialize(0)

    def get(self, key, fill_cache_func):
        value = self._cache.get(key, default=None)
        if value is None:
            value = fill_cache_func()
            table_pandas = value.to_pandas()
            serialized_df = pa.serialize(table_pandas)
            components = serialized_df.to_components()
            self._cache.set(key, components)
        else:
            original_df = pa.deserialize_components(value)
            value = pa.Table.from_pandas(original_df, preserve_index=False)

        return value


from functools import lru_cache

lines = []
best = 0

for line in open('input.txt'):
    line = line.strip()
    lines.append(line)

M = len(lines)
N = max(len(line) for line in lines)

prev = {None}
curr = set()

def get(i, j):
    if i < 0 or i >= len(lines): return '.', 0
    if j < 0 or j >= len(lines[i]): return '.', 0
    return lines[i][j], ((i,j) in curr)

def scan(i, j, dx, dy):
    if i < 0 or i >= len(lines): return 0
    if j < 0 or j >= len(lines[i]): return 0
    if lines[i][j] == '.':
        return scan(i+dx, j+dy, dx, dy)
    else:
        return 1 if (i,j) in curr else 0

while prev != curr:
    prev = set(curr)
    next = set(curr)
    for i in range(M):
        for j in range(N):
            char, cur = get(i,j)
            close = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    if dx == dy == 0: continue
                    #close += get(i+dx, j+dy)[1]
                    close += scan(i+dx, j+dy, dx, dy)

            if char == '.':
                continue
            elif char == 'L':
                if cur == 0 and close==0:
                    next.add((i,j))
                #elif cur == 1 and close >= 4:
                elif cur == 1 and close >= 5:
                    next.discard((i,j))
    curr = next
    print(len(curr))

print(len(curr))


#!/usr/bin/python

import sys, os, argparse
import asyncio
import traceback
import rulebook
from .util import *
from .libnetconf import NetworkState
import rulebook.runtime

import logging
logger = logging.getLogger(__name__)

logging.basicConfig(level=logging.DEBUG)

try:
    from IPython.kernel.zmq.kernelapp import IPKernelApp
    import zmq
except ImportError:
    logger.warn("IPython and/or pyzmq not available. Interactive console will not work.")
    HAVE_IPYTHON = False
else:
    HAVE_IPYTHON = True

if HAVE_IPYTHON:
    # XXX IPython infests this application with threads!! I'm not sure why.
    #     I'm not even sure in which one the interactive commands are executed.
    # TODO Try to get rid of that.
    class IPythonEmbed:
        def __init__(self, ns):
            self.ns = ns
            self.app = IPKernelApp(transport='ipc')
            NOP = lambda *a,**kw: None
            # Don't exit upon parent process exit
            self.app.init_poller = NOP
            # Don't redirect stdio
            self.app.init_io = NOP
            self.app.init_blackhole = NOP
            # Don't catch SIGINT
            self.app.init_signal = NOP
            self.app.init_connection_file = NOP
            self.app.log_connection_info = NOP

            self.app.connection_file = str(RUNDIR / 'ipython.json')

        def start(self):
            # Make sure only root can access the sockets and the connection file
            with umask_ctx(0o077):
                try: os.unlink(self.app.connection_file)
                except FileNotFoundError: pass
                self.app.initialize()

            self.app.kernel.user_module = sys.modules[__name__]
            self.app.kernel.user_ns = self.ns
            self.app.shell.set_completer_frame()

            self.app.kernel.start()

            for stream in self.app.kernel.shell_streams:
                fd = stream.socket.getsockopt(zmq.FD)
                def callback(stream):
                    stream.flush(zmq.POLLIN, 1)
                    stream.flush(zmq.POLLOUT)
                asyncio.get_event_loop().add_reader(fd, callback, stream)
    # That wasn't so hard, right? Why do the IPython developers keep on recommending
    # regular polling as the way of integrating an IPython kernel with a mainloop then?

class Daemon:
    def __init__(self):
        self.rulebooks = {}
        self.loop = asyncio.get_event_loop()
        if HAVE_IPYTHON:
            self.ipython = IPythonEmbed({'daemon': self})

    # arg_parser = argparse.ArgumentParser()
    # arg_parser.add_argument('-c', nargs=1, dest='config_path', help="Specify alternative configuration directory.")
    # arg_parser.add_argument('-C', action='store_false', dest='want_builtin', default=True,
    #                         help="Do not load builtin rules.")
    # def parse_cmdline(self, argv):
    #     args = self.arg_parser.parse_args(argv)

    def _load_rules(self, dirs):
        for dir in dirs:
            for file in Path(dir).glob('*.rbk'):
                self._load_rbk(file)

    def _load_rbk(self, file):
        file = Path(file).resolve()
        logger.info("Loading rulebook %s", file)
        self.rulebooks[file] = rulebook.load(file, self.ctx)[0]
        self.rulebooks[file].set_active(True)

    def _exception_handler(self, loop, context):
        exc = context.get('exception')
        if isinstance(exc, asyncio.InvalidStateError):
            # XXX Sometimes an exception like this appers as a result of a cancelled coroutine:
            #     Traceback (most recent call last):
            #       File "/usr/lib/python3.4/asyncio/events.py", line 39, in _run
            #         self._callback(*self._args)
            #       File "/usr/lib/python3.4/asyncio/subprocess.py", line 44, in connection_made
            #         self.waiter.set_result(None)
            #       File "/usr/lib/python3.4/asyncio/futures.py", line 298, in set_result
            #         raise InvalidStateError('{}: {!r}'.format(self._state, self))
            #     asyncio.futures.InvalidStateError: CANCELLED: Future<CANCELLED>
            # Maybe I'm doing something wrong, but it looks as a bug in `asyncio`.
            return
        if isinstance(exc, asyncio.CancelledError):
            return
        traceback.print_exception(type(exc), exc, exc.__traceback__)
        traceback.print_stack()
        sys.exit(1)

    @asyncio.coroutine
    def initialize(self):
        # Make all exceptions fatal for easier debugging
        self.loop.set_exception_handler(self._exception_handler)
        tasks = []
        logger.info("Loading network state")
        self.ns = NetworkState()
        self.ctx = rulebook.runtime.Context()
        self.ctx.ns.ns = self.ns # Make the NetworkState available under the name 'ns'
                                 # in the namespace of the rulebooks (a bit unfortunate
                                 # clash of acronyms, TODO better naming).
        self.ctx.ns.logger = logging.getLogger('ns_rbk')
        yield from self.ns.start()

        logger.info("Loading configuration")
        self._load_rules([RULES_BUILTIN, RULES_USER])

        ctl_path = str(RUNDIR / 'ctl.sock')
        try: os.unlink(ctl_path)
        except FileNotFoundError: pass
        yield from asyncio.start_unix_server(self._unix_conn, ctl_path)

        if HAVE_IPYTHON:
            self.ipython.ns['ns'] = self.ns
            self.ipython.ns['ctx'] = self.ctx
            self.ipython.start()
            logger.info("IPython ready. Connect with: ``nsctl console`` or ``ipython console --existing %s``",
                    self.ipython.app.connection_file)

    def _unix_conn(self, reader, writer):
        logger.info('New unix connection')
        @asyncio.coroutine
        def conn_coro():
            while True:
                line = yield from reader.readline()
                if not line: break
                line = line.decode('utf-8').strip()
                logger.info('Got ctl command: %s', line)
        run_task(conn_coro())

    def main(self):
        self.loop.run_until_complete(self.initialize())
        logger.info("Entering mainloop")
        self.loop.run_forever()

def main():
    daemon = Daemon()
    daemon.main()

if __name__ == '__main__':
    main()


"""empty message

Revision ID: b2a4b30fdece
Revises: 0a6d3d1508a5
Create Date: 2021-03-04 18:52:11.795274

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'b2a4b30fdece'
down_revision = '0a6d3d1508a5'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('complaint',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('userID', sa.Integer(), nullable=True),
    sa.Column('subject', sa.String(length=60), nullable=True),
    sa.Column('complaint', sa.String(length=500), nullable=True),
    sa.Column('status', sa.Boolean(), nullable=True),
    sa.Column('registeredAdmin', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['registeredAdmin'], ['admin.id'], ),
    sa.ForeignKeyConstraint(['userID'], ['user.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('complaintLocation',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('complaintID', sa.Integer(), nullable=True),
    sa.Column('lng', sa.String(length=60), nullable=True),
    sa.Column('lat', sa.String(length=60), nullable=True),
    sa.ForeignKeyConstraint(['complaintID'], ['complaint.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('complaintLocation')
    op.drop_table('complaint')
    # ### end Alembic commands ###


import time

from metadrive.component.map.pg_map import PGMap
from metadrive.engine.engine_utils import initialize_engine, close_engine
from metadrive.envs import MetaDriveEnv

try:
    from reprlib import repr
except ImportError:
    pass

from metadrive.tests.test_functionality.test_memory_leak_engine import process_memory, total_size

def test_pg_map_memory_leak():

    try:
        default_config = MetaDriveEnv.default_config()

        # default_config["map_config"]["config"] = "SSS"
        # default_config["map_config"]["type"] = "block_sequence"

        default_config["map_config"]["config"] = 1

        # default_config["debug"] = True
        # default_config["debug_physics_world"] = True

        close_engine()
        engine = initialize_engine(default_config)

        ct = time.time()
        last_lm = cm = process_memory()
        last_mem = 0.0
        for t in range(50):
            lt = time.time()

            our_map = PGMap(default_config["map_config"])

            # our_map.blocks.clear()
            # our_map.blocks = []

            size = total_size(our_map)
            # print("map size: {:,}".format(size))
            del our_map

            # print(engine.physics_world.report_bodies())

            nlt = time.time()
            lm = process_memory()
            # print(
            #     "After {} Iters, Time {:.3f} Total Time {:.3f}, Memory Usage {:,} Memory Change {:,}".format(
            # t + 1, nlt - lt, nlt - ct, lm - cm, lm - last_lm
            #     )
            # )
            # last_lm = lm
            # if t > 100:
            #     assert abs((lm - cm) - last_mem) < 1024  # Memory should not have change > 1KB
            # last_mem = lm - cm

    finally:
        close_engine()

    assert lm - cm < 1024 * 1024 * 120, "We expect will cause 100M memory leak with 50 iterations."

if __name__ == "__main__":
    test_pg_map_memory_leak()


#link - https://www.hackerrank.com/challenges/iterables-and-iterators/problem?isFullScreen=true

from itertools import combinations

n = int(input())
arr = [x for x in input().split()]
k = int(input())

combi = list(combinations(arr, k))

total = len(combi)

count = [1 for each in combi if 'a' in each]
print(len(count)/total)


import csv
import string
import chess
import chess.pgn
import random
import json
from random import randint
import pandas as pd

# Flask and WSGI
import flask
from flask import Flask, Blueprint, jsonify
from flask import request
from flask_cors import CORS, cross_origin

app = Flask(__name__)
CORS(app)

# Reading database
db = pd.read_csv('datadifficulty.csv')

# divided by difficulty
db_easy = db.loc[db['Level'] == 'Easy']
db_easy.reset_index(inplace=True, drop=True)
db_medium = db.loc[db['Level'] == 'Medium']
db_medium.reset_index(inplace=True, drop=True)
db_hard = db.loc[db['Level'] == 'Hard']
db_hard.reset_index(inplace=True, drop=True)

# Globals that track current answers
side_move = ''
curr_fen = ''
white_elo = 0; black_elo= 0
white_cp = 0; black_cp = 0
white_wdl = 0.00; black_wdl = 0.00
ans_cpadv = ''; ans_wdladv = ''

def player_move(stm):
    if stm == 'W':
        return 'White'
    else:
        return 'Black'

@app.route('/',methods=['GET', 'POST'])
@cross_origin()
def blank_text():
    return "Blank"

# Extracts a random fen from database and sets globals accordingly
@app.route('/boardupdate', methods=['GET','POST'])
@cross_origin()
def board_update():
    global curr_fen, white_elo, black_elo, white_cp, black_cp, white_wdl, black_wdl, ans_cpadv, ans_wdladv, side_move
    #for testing
    diff_v = request.args.get('difficulty')
    dbr = pd.DataFrame()
    # matching database
    if diff_v == 'E':
        dbr = db_easy
    elif diff_v == 'M':
        dbr = db_medium
    elif diff_v == 'H':
        dbr = db_hard
    else:
        dbr = db
    fen_row = random.randint(0,len(dbr))
    curr_fen = dbr.loc[fen_row,'FEN']
    white_elo = dbr.loc[fen_row,'White ELO']; black_elo = dbr.loc[fen_row,'Black ELO']
    white_cp = dbr.loc[fen_row,'White cp']; black_cp = dbr.loc[fen_row,'Black cp']
    white_wdl = dbr.loc[fen_row,'White wdl']; black_wdl = dbr.loc[fen_row, 'Black wdl']
    ans_cpadv = dbr.loc[fen_row,'CpAdv']; ans_wdladv = dbr.loc[fen_row,'WdlAdv']
    side_move = player_move(str(dbr.loc[fen_row,'STM']))
    print(ans_cpadv, ans_wdladv)

    dict_send = {'fen': curr_fen,
                'WhiteELO': str(white_elo),
                'BlackELO': str(black_elo),
                'blackWDL': str(black_wdl),
                'whiteWDL': str(white_wdl),
                'blackCP': str(black_cp),
                'whiteCP': str(white_cp),
                'side': str(side_move)}
    return jsonify(dict_send)

@app.route('/updatechoice', methods=['GET','POST'])
@cross_origin()
def update_choice():
    choice = request.args.get('UserChoice')
    print('this is the choice', choice)
    comments = ''
    print(ans_cpadv,ans_wdladv)
    if (choice == ans_cpadv) or (choice == ans_wdladv):
        comments = 'User has guessed correctly!'
    else:
        comments = 'User has guessed wrong.' + ' Correct Answer is ' + ans_wdladv
    print(comments)
    return comments

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8000, debug=True)


from db.base import Base, engine, session
from db.tables import Sport, Championship, Match, Duplicate

class Receive:
    """Класс получение данных из бд"""

    def __init__(self):
        self.session = session()

    def get_all_ids_match(self, external_match_id):
        """получение всех id матча (с дубликатами)"""
        match = self.session.query(Match) \
            .filter_by(external_id=external_match_id).first()

        all_match_ids = [match.external_id]
        all_match_ids.extend([duplicate_id.external_match_id for
                              duplicate_id in match.duplicate])

        return all_match_ids

    def get_match_id_by_external_id(self, external_match_id):
        """получить id матча по внешнему id"""
        match_id = self.session.query(Match.id).filter_by(external_id=external_match_id).first()
        if not match_id:
            match_id = self.session.query(Duplicate.parent_match_id).\
                filter_by(external_match_id=external_match_id).first()

        if match_id:
            match_id = match_id[0]

        return match_id


#!/usr/bin/env python

# +=========================================+
# | @author Cole Dapprich, 2018-2020 (AMDG) |
# +=========================================+

from sys import argv, exit
import csv
import re

# 0 = wr, 1 = rb, 2 = te, 3 = qb, 4 = dst/def, 5 = k TODO: make order dynamic
positionalRankings = [ [], [], [], [], [], [] ]

def getRankings(filename):
    data = []
    with open(filename, 'r') as f:
        reader = csv.DictReader(f)
        for row in reader:
            data.append(row)

    return data

def getPositionalRankings(filename, posIdx):
    with open(filename, 'r') as f:
        reader = csv.DictReader(f)
        for row in reader:
            positionalRankings[posIdx].append(row)

def space(num):
    spaces = ''
    for i in range(num):
        spaces += ' '

    return spaces

def printPlayer(player, inPositionMode, posStr):
    print(player['RK'], '\t', player['TIERS'], '\t', player['PLAYER NAME'], space(25 - len(player['PLAYER NAME'])), player['POS'] if not inPositionMode else posStr + player['RK'], '\t', player['TEAM'], '\t', player['BYE'] if not inPositionMode else '\t', '\t', player['BEST'], '\t', player['WORST'], '\t', player['AVG.'], '\t', player['STD.DEV'])

def printNext5(ranks):
    print('')
    print('Rank\tTier\t Player\t\t\t    Pos\t\t Team\t Bye\t Best\t Worst\t Avg\t Std Dev')
    for i in range(5):
        printPlayer(ranks[i], False, "")
    print('')

def printPos(ranks, pos, inPositionMode):
    if inPositionMode:
        count = 1
        posIdx = 0

        if pos == 'RB':
            posIdx = 1
        elif pos == 'TE':
            posIdx = 2
        elif pos == 'QB':
            posIdx = 3
        elif pos == 'DST':
            posIdx = 4
        elif pos == 'K':
            posIdx = 5

        for rank in positionalRankings[posIdx]:
                printPlayer(rank, inPositionMode, pos)
                count += 1
                if count > 5:
                    break
        print('')
    else:
        count = 1
        for rank in ranks:
            if bool(re.match(pos + '\d{1,3}', rank['POS'])):
                printPlayer(rank, inPositionMode, pos)
                count += 1
            if count > 5:
                break
        print('')

def main(argv):
    # check for correct number of arguments; exit if check fails
    if len(argv) < 2:
        exit('USAGE: python draft.py draftRankingsCSVfilePath [positionRankingsCSVfilePath1, positionRankingsCSVfilePath2...] (order expected is wr, rb, te, qb, dst/def, k')

    overallMode = False
    hasPositionFiles = len(argv) == 8
    if hasPositionFiles:
        for i in range(2, len(argv)):
            getPositionalRankings(argv[i], i - 2)

    rankings = getRankings(argv[1])
    printNext5(rankings)
    print('Commands:\n\t[Player Name] - pick player\n\t[QB/RB/WR/TE/DST/DEF] - show next 5 players in that position\n\tall - print(names of next 5 players in rankings\n\tquit/done - exit\n')
    pick = input('Enter Pick #1, or a command (\'help\' for list of commands): ').lower()
    pickNum = 1

    while 'done' != pick and 'quit' != pick and 'q' != pick:
        if pick == 'wr':
            printPos(rankings, 'WR', hasPositionFiles ^ overallMode)
            printNext5(rankings)

        elif pick == 'rb':
            printPos(rankings, 'RB', hasPositionFiles ^ overallMode)
            printNext5(rankings)

        elif pick == 'te':
            printPos(rankings, 'TE', hasPositionFiles ^ overallMode)
            printNext5(rankings)

        elif pick == 'qb':
            printPos(rankings, 'QB', hasPositionFiles ^ overallMode)
            printNext5(rankings)

        elif pick == 'dst' or pick == 'def':
            printPos(rankings, 'DST', hasPositionFiles ^ overallMode)
            printNext5(rankings)

        elif pick == 'kicker' or pick == 'k':
            printPos(rankings, 'K', hasPositionFiles ^ overallMode)
            printNext5(rankings)

        elif pick == 'all':
            printNext5(rankings)

        elif pick == 'help':
            print('Commands:\n\t[Player Name] - pick player\n\t[QB/RB/WR/TE/DST/DEF] - show next 5 players in that position\n\tall - print(names of next 5 players in rankings\n\tquit/done - exit')

        elif pick == 'overall':
            overallMode = not overallMode

        elif pick: # player picked (not empty string)
            names = [rank['PLAYER NAME'] for rank in rankings]
            for name in names:
                if name and pick in name.lower():
                    choice = input('Pick ' + name + '? (y/n/cancel): ').lower()
                    if 'y' in choice:
                        del rankings[names.index(name)]
                        pickNum += 1
                        printNext5(rankings)
                    elif 'cancel' in choice:
                        break

        pick = input('Enter Pick #' + str(pickNum) + ', or a command (\'help\' for list of commands): ').lower()

if __name__ == "__main__":
    main(argv)


import unittest
import matrix
import random

import numpy as np

class TestMatrix(unittest.TestCase):

    def test_initialize(self):
        mat_a = matrix.random(2, 3)
        mat_b = matrix.ones(2, 3)
        mat_c = matrix.zeros(2, 3)

        assert mat_a.colunm == mat_b.colunm == mat_c.colunm == 3
        assert mat_a.row == mat_b.row == mat_c.row == 2

    def test_matmul(self):
        m = random.randint(2, 100)
        k = random.randint(2, 100)
        n = random.randint(2, 100)

        mat_a = matrix.random(m, k)
        mat_b = matrix.random(k, n)
        mat_c = mat_a * mat_b

        self.assertEqual(
            [mat_c.colunm, mat_c.row],
            [n, m]
        )

    def test_add(self):
        m = random.randint(2, 100)
        n = random.randint(2, 100)

        mat_a = matrix.random(m, n)
        mat_b = matrix.random(m, n)
        mat_c = mat_a + mat_b

        self.assertEqual(
            [mat_c.colunm, mat_c.row],
            [n, m]
        )

    def test_minus(self):
        m = random.randint(2, 100)
        n = random.randint(2, 100)

        mat_a = matrix.random(m, n)
        mat_b = matrix.random(m, n)
        mat_c = mat_a - mat_b

        self.assertEqual(
            [mat_c.colunm, mat_c.row],
            [n, m]
        )

    def test_attr(self):
        m = np.ones((10, 10))
        n = matrix.ones(10, 10)

        assert n.colunm == 10
        assert n.row == 10
        self.assertEqual(n.data, m.tolist())

if __name__ == "__main__":
    unittest.main()


import os
import pandas as pd
import numpy as np
from numpy.linalg import inv
import matplotlib.pyplot as plt

# root mean square error metric between predictions and the ground truth
def rmse(pred, true):
    assert pred.shape == true.shape
    n, = true.shape
    return np.sqrt(np.sum((pred - true) ** 2) / n)

def prepo(X):
    """
    For easier updatig, preposessing
    Create a full rating matrix M: N1*N2
    Create two dictionaries mapping users to movies and the reverse
    """
    N1=max(X[0]) # N1 users
    N2=max(X[1]) # N2 movies
    M=np.zeros((N1, N2))
    uv={} # the user i to all movies rated
    vu={} # the movie j to all users rating
    for i in range(len(X[0])):
        # rating matrix M
        M[X[0][i]-1][X[1][i]-1]=X[2][i]
        # a dictionary mapping users ui to movies
        if X[0][i]-1 in uv:
            uv[X[0][i]-1].append(X[1][i]-1)
        else:
            uv[X[0][i]-1]=[X[1][i]-1]
        # another dictionary mapping movies vj to users
        if X[1][i]-1 in vu:
            vu[X[1][i]-1].append(X[0][i]-1)
        else:
            vu[X[1][i]-1]=[X[0][i]-1]
    return M,uv,vu

# update the user i location ui or the object j location vj: d*1 dimension
def update(a,r,list,const,d):
    """
    update a vector b using some vectors a[i] in matrix a and a index list containing {i}.
    r: a rating array
    d: dimension
    const: constant
    :return: updated b
    """
    s1=np.zeros((d,d))
    s2=np.zeros(d)
    for i in list:
        s1=s1+(a[i][np.newaxis, :].T)*a[i]
        s2=s2+r[i]*a[i]
    return sum((inv(const+s1)*s2).T)

# the objective function
def objfunc(X,u,v,M_pred,var,numbda):
    s1=0
    for i in range(len(X[0])):
        s1=s1+(X[2][i]-M_pred[X[0][i]-1,X[1][i]-1])**2
    return -(s1/2/var+sum(sum(v**2))*numbda/2+sum(sum(u**2))*numbda/2)

def MatrixfFactorization(X,M,uv,vu,var,d,numbda,T):
    """
    To recomender user ui for object vj given Gaussian Model Assumption by MatrixfFactorization
    T: Iteration
    :return: predicted missing rates
    """
    # Constants
    N1=max(X[0]) # N1 users
    N2=max(X[1]) # N2 movies

    # Initialze vj from a Guassian distribution
    mean=np.zeros((d,), dtype=np.int)
    var0=1/numbda*np.eye(d)
    v = np.random.multivariate_normal(mean, var0, N2)
    u = np.random.multivariate_normal(mean, var0, N1)

    # Updating
    L=[]
    const=numbda*var*np.eye(d)
    for t in range(T):
        # update the user i location ui
        for i in range(N1):
            if i in vu:
                u[i]=update(v,M[i],uv[i],const,d)

        # update the object j location vj
        for j in range(N2):
            if j in vu:
                v[j]=update(u,M.T[j],vu[j],const,d)

        # predict ratings
        u1=np.asmatrix(u)
        v1=np.asmatrix(v)
        M_pred=u1*(v1.T)

        # the objective function
        Lt=objfunc(X,u,v,var,numbda)
        # make a list for every value of the objective function
        L.append(Lt)
    return L, M_pred, v

#############################################################
# for question 2
# loading the set indexes an incomplete ratings matrix M
X = pd.read_csv(os.path.join("/Users/cengjianhuan/Documents/ML/HW4", 'ratings.csv'), header=None)
# loading the testing set
M_ture = pd.read_csv(os.path.join("/Users/cengjianhuan/Documents/ML/HW4", 'ratings_test.csv'), header=None)
# constants
var = 0.25
d = 10
numbda = 1.0
T=100
# create a full rating matrix M: N1*N2 and two dictionaries mapping users to movies and the reverse
M,uv,vu=prepo(X)
# create lists to store results
L=[]
rmse_list=[]
Lmax=-5000
# run 10 times
for k in range(10):
    # get the objective value and prediction at the k time
    Lk, M_pred, vk=MatrixfFactorization(X,M,uv,vu,var,d,numbda,100)
    L.append(Lk[-1])
    # pick the relative predicted value M_test for rmse
    M_test=np.zeros(len(M_ture[2]))
    for i in range(len(M_ture[2])):
        M_test[i]=M_pred[M_ture[0][i]-1,M_ture[1][i]-1]
    # find the rmse at k time
    rmse_list.append(rmse(M_test, M_ture[2]))

    # find the optimal L
    if L[-1]>Lmax:
        Lmax=L
        vmax=vk

# for question a
for i in range(10):
    plt.plot(L[i][1:],label="L"+str(i))
plt.legend(loc='best')
plt.show()

# for question b
N2,_=vmax.shape
xt=[vmax[49],vmax[484],vmax[181]]
d=[]
minv=[]
idxv=[]
for k in range(len(xt)):
    minv.append([])
    idxv.append([])
    for i in range(1682):
        d.append(np.linalg.norm(xt[k]-vmax[i]))
    M_test=np.zeros(len(M_ture[2]))
    for i in range(len(M_ture[2])):
        M_test[i]=M_pred[M_ture[0][i]-1,M_ture[1][i]-1]
    for t in range(11):
        mn,idx = min((d[i],i) for i in range(len(d)))
        minv[k].append(mn)
        idxv[k].append(idx)
        d[idx]=10


# -*- coding: utf-8 -*-
"""
Created on Wed Feb 13 20:23:59 2019

@author: de-sinnett
"""
# Import random, operator and plotting modules
import random
import operator
import matplotlib.pyplot

# Create a new empty list
agents = []

# Set up variables to start at random coordinates in a 100x100 grid (agent 1)
agents.append([random.randint(0,99),random.randint(0,99)])

# Random walk one step
if random.random() < 0.5:
    agents[0][0] = agents[0][0] + 1
else:
    agents[0][0] = agents[0][0] - 1

if random.random() < 0.5:
    agents[0][1] = agents[0][1] + 1
else:
    agents[0][1] = agents[0][1] - 1

# Set up variables to start at random coordinates in a 100x100 grid (agent 2)
agents.append([random.randint(0,99),random.randint(0,99)])

# Random walk one step
if random.random() < 0.5:
    agents[1][0] = agents[1][0] + 1
else:
    agents[1][0] = agents[1][0] - 1

if random.random() < 0.5:
    agents[1][1] = agents[1][1] + 1
else:
    agents[1][1] = agents[1][1] - 1

print (agents)

# Print agent with maximum value in first co-ordinate
print (max(agents))

# Print agent with maximum value in second co-ordinate
print (max(agents, key=operator.itemgetter(1)))

# Plot the agents
matplotlib.pyplot.ylim(0, 99)
matplotlib.pyplot.xlim(0, 99)
matplotlib.pyplot.scatter(agents[0][1],agents[0][0])
matplotlib.pyplot.scatter(agents[1][1],agents[1][0])
matplotlib.pyplot.scatter(m[1], m[0], color='red')
matplotlib.pyplot.show()


'''
A simple python calculation question generator
usg: python calculator.py -d "+,-" -c 3 -l 200 -t 30
usg: python calculator.py --op_type="+,-" --op_count=3 --limit=20 --total=30
'''
import sys, getopt, random

def auto_cal_generator(limit=100, op_count=1, op_type=["+"], total=100):
    print("Here are today's %d works, good luck!" % total)
    l = len(op_type)-1
    for j in range(0, total):
        up = limit
        question = ""
        for i in range(0, op_count+1):
            num = 0
            if i == 0:
                num = random.randint(1,max(1,min(limit,up)))
                question = "%s%d" % (question, num)
                up -= num
                continue
            op = "+"
            if limit - up > 0:
                op_i = random.randint(0,l)
                op = op_type[op_i]
            question = "%s%s" % (question, op)
            if op =="+":
                num = random.randint(1,max(1,min(limit,up)))
                up -= num
            elif op == "-":
                num = random.randint(1,max(limit-up, 1))
                up += num
            else:
                print("operator error: %s" % op)
                sys.exit(1)
            question = "%s%d" % (question, num)
        print("%d: %s=" % (j+1, question))

def main(argv=None):
    if argv is None:
        argv = sys.argv
    opts, _dummy = getopt.getopt( sys.argv[1:], "l:c:d:t:", ["limit=","op_count=","op_type=","total="])
    limit = 100
    op_count = 1
    op_type = ["+"]
    total = 100
    for opt,arg in opts:
        if opt in ["-l", "--limit"]:
            limit = int(arg)
        elif opt in ["-c", "--op_count"]:
            op_count = int(arg)
        elif opt in ["-d", "--op_type"]:
            op_type = arg.strip().split(",")
        elif opt in ["-t", "--total"]:
            total = int(arg)

    auto_cal_generator(limit, op_count, op_type, total)

if __name__ == '__main__':
    main()


number_of_soldiers = int(input())
a = list(map(int, input().split()))  # number of weapons
x1 = 0
x2 = 0
for i in range(0, len(a)):
    if a[i] % 2 == 0:
        x1 += 1
    else:
        x2 += 1

if x1 > x2:
    print("READY FOR BATTLE")
else:
    print("NOT READY")


import boto3
import json
import logging
import os
import cv2
import ast
from botocore.exceptions import ClientError

s3 = boto3.client('s3')
s3inputbucket = 'inputvideobucket2022'
sourcefile = 'source.mp4'
sourceoutputfile = 'source.mp4'
labelidentifier = 'Human'
labelconfidence = 80
jsonsource = '0data.json'

def S3Exist(InputVideoBucket, InputVideoKey):
    s3 = boto3.client('s3')
    try:
        response = s3.head_object(Bucket=InputVideoBucket, Key=InputVideoKey)
        return response
    except ClientError:
        #print('key not found ...')
        return ('False')

def CvFrameProcessor(SourceVideoFile, FrameTimeStamp, FrameRate, OutputFrameName):
    VidCapture = cv2.VideoCapture(SourceVideoFile)
    length = int(VidCapture.get(cv2.CAP_PROP_FRAME_COUNT))
    print("length of the input video is:", length, "seconds")
    framenumber = FrameTimeStamp*0.0001*FrameRate
    VidCapture.set(1, framenumber) #Based on total length calculated above in seconds and Framerate, calculate which frame to extract
    ret, frame = VidCapture.read()
    cv2.imwrite(OutputFrameName, frame)

def RekognitionOutputParser (JsonInput, ConfidenceScore, LabelIdentifier):
    NewLabelData = []
    with open(jsonsource) as f:
        data = json.load(f)
        #print(type(data))
    VideoMetadata = ast.literal_eval(data)
    #print(type(VideoMetadata))
    #print(VideoMetadata['VideoMetadata'])
    #VideoMetadata = data['VideoMetadata']
    #print(type(VideoMetadata))
    for LabelData in VideoMetadata['Labels']:
        if (LabelData['Label']['Name'] == LabelIdentifier) and (LabelData['Label']['Confidence'] > ConfidenceScore):
            NewLabelData.append(LabelData)
    #print(NewLabelData)
    return NewLabelData, VideoMetadata

response = S3Exist(s3inputbucket, sourcefile)
if response != 'False':
  print('Key exists, continue ...\n')
  try:
        s3.download_file(s3inputbucket, sourcefile, sourceoutputfile) #Download Video From S3
        if not os.path.exists('OutputFrames'):
            os.makedirs('OutputFrames')

    #Get specific LabelData which is data of interest basd on constraints provided
        LabelData = RekognitionOutputParser(jsonsource, labelconfidence, labelidentifier)
    #Set critical variables to run video frame extracter
        #print(LabelData[1]['VideoMetadata'])
        framerate = LabelData[1]['VideoMetadata']['FrameRate']

        for frame in LabelData[0]:
            outputframename = ( "OutputFrames/" + str(frame["Timestamp"]) + ".jpeg")
            CvFrameProcessor(sourceoutputfile, frame["Timestamp"], framerate, outputframename)
            print (frame["Timestamp"])

    #Run Frame Processor using OpenCV to pull the specific Frames based on Label criteria
            #CvFrameProcessor(sourceoutputfile, timestamp, framerate, outputframename)

  except OSError:
        print('File already exists ... Removing exisitng file \n')
        os.remove('~/environment/RekognitionSid/HumanVideoDetect/source.mp4')

else:
  print('Failed to locate input key in S3 ... ')


class node:
    key = ""
    value = 0

    def __init__(self, key, value):
        self.key = key
        self.value = value

    def set(self, value):
        self.value = value

    def output(self):
        print("'" + self.key + "' : '" + self.value + "'")

class hashTable:
    # bucket is the container for all nodes
    bucket = []
    # we maintain a set of all keys to check for unique vals.
    # this way, we don't have to handle checking
    # at insert during hash function. Spatial tradeoff for speed & convenience.
    keys = set([])
    # cap size of hashTable. Fixed, set at instantiation
    size = 0
    __loadSum = 0
    # load value
    __loadFactor = 0.0

    # takes in size
    def __init__(self, size):
        self.keys = set([])
        self.size = size
        self.bucket = [None] * size
        self.__loadCalc();

    #we recalculate load on every operation
    #this function gets called at set operations and delete operations
    def __loadCalc(self):
        if (self.size == 0):
            self.__loadFactor = 0
        else:
            self.__loadFactor = self.__loadSum/self.size

    # returns value associated with key
    def get(self, k):
        k = str(k);
        if (k in self.keys):
            i = 0
            hashed = (hash(k) + i) % self.size

            while(self.bucket[hashed] != None):
                if(self.bucket[hashed].key != k):
                    hashed = (hash(k) + i) % self.size
                    i += 1
                else:
                    return self.bucket[hashed].value
            return None
        else:
            return None

    # sets value at given key. returns boolean indicating success or failure
    # we use linear probing for collision resolution
    def set(self, k, v):
        k = str(k);
        print("INSIDE SET")
        print(k)
        if (k in self.keys):
            i = 0
            hashed = (hash(k) + i) % self.size
            print("already exists")
            print(hashed)
            while(self.bucket[hashed] != None):

                if(self.bucket[hashed].key != k):
                    hashed = (hash(k) + i) % self.size
                    print(hashed)
                    i += 1
                else:
                    self.bucket[hashed].value = v
                    return True
        elif (self.__loadFactor == 1.0):
            return False
        else:
            newNode = node(k, v)
            i = 0;
            hashed = (hash(k) + i) % self.size;
            print(hashed)
            while(self.bucket[hashed] != None):
                hashed = (hash(k) + i) % self.size
                i += 1
            self.bucket[hashed] = newNode
            self.keys.add(k)
            self.__loadSum += 1
            self.__loadCalc()
            return True

    # deletes value at given key. returns value.
    def delete(self, k):
        k = str(k);
        if (k in self.keys):
            i = 0
            hashed = (hash(k) + i) % self.size
            while(self.bucket[hashed] != None):
                if(self.bucket[hashed].key != k):
                    hashed = (hash(k) + i) % self.size
                    i += 1
                else:
                    tmp = self.bucket[hashed].value
                    self.bucket[hashed] = None
                    self.keys.remove(k)
                    self.__loadSum -= 1
                    self.__loadCalc()
                    return tmp;
            return None
        else:
            return None

    #returns load
    def load(self):
        return self.__loadFactor
    def __str__(self):
        return self.keys.__repr__()


import importlib.resources
from pathlib import Path

import numpy as np
import scipy.linalg as sla

from hivpy.sex_behaviour_data import SexualBehaviourData

def test_probability_loading():
    # Load example file
    data_path = Path(__file__).parent / "test_data" / "sbd_testing.yaml"
    SBD = SexualBehaviourData(data_path)
    dists = SBD._get_discrete_dist_list("Test_Example_List")

    # explicit distribution
    d0 = dists[0]
    N = 10000
    pop0 = d0.sample(size=N)
    for i in range(5):
        v = i + 1
        p = 0.1*i
        count = sum(pop0 == v)
        E_count = p * N
        var = N * p * (1-p)
        assert E_count - 2*var <= count <= E_count + 2*var

    # uniform over range
    d1 = dists[1]
    pop1 = d1.sample(size=N)
    for i in range(1, 11):
        count = sum(pop1 == i)
        E_count = 0.1 * N
        var = N * 0.09
        assert E_count - 2*var <= count <= E_count + 2*var

    # uniform over specific values
    d2 = dists[2]
    pop2 = d2.sample(size=N)
    for i in range(5):
        v = i + 1
        p = 0.2
        count = sum(pop2 == v)
        E_count = N * p
        var = N * p * (1-p)
        assert E_count - 2*var <= count <= E_count + 2*var

def test_sex_behaviour_matrices_diagonalisable():
    """Checks that all of the sexual behaviour transition matrices are diagonalisable
    and therefore can be used to calculate transition matrices for variable time-steps.
    This only needs to be run if the matrices change, and doesn't need to be run every time."""
    with importlib.resources.path("hivpy.data", "sex_behaviour.yaml") as data_path:
        SBD = SexualBehaviourData(data_path)
    count_bad = 0
    bad_matrices = []
    all_matrices = [SBD.sex_behaviour_transition_options["Male"],
                    SBD.sex_behaviour_transition_options["Female"]]
    for matList in all_matrices:
        for m in matList:
            print("m = ", m)
            T = np.array(m).transpose()
            evals, evecs = sla.eig(T)
            print(evals)
            print(evecs)
            # Check that eigenvectors are linearly independent
            # i.e. that no eigenvector is a multiple of any other
            for i in range(0, evals.size):
                for j in range(i + 1, evals.size):
                    ratio = evecs[i] / evecs[j]
                    print((i, j), ratio)
                    if np.allclose(ratio, ratio[0]):
                        count_bad += 1
                        bad_matrices.append(T)
            print("***************************************************")
    print(count_bad)
    print(bad_matrices)
    assert (count_bad == 0)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/2/28 3:56 PM
# @Author  : huxiaoman
# @File    : 21_MergeTwoSortedLists.py
# @Package : LeetCode
# @E-mail  : charlotte77_hu@sina.com

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if not l1 or not l2:
            return l1 or l2
        head = cur = ListNode(0)
        while l1 and l2:
            if l1.val <l2.val:
                cur.next = l1
                l1 = l1.next
            else:
                cur.next = l2
                l2 = l2.next
            cur = cur.next
        cur.next = l1 or l2
        return head.next


from django.db import models

class AudioTourModel(models.Model):
    title = models.CharField(max_length=200, blank=False, help_text='Title for audio tour area')
    soundcloud_id = models.IntegerField(
        blank=False,
        help_text='Specific Id from SoundCloud service to embed')
    index = models.DecimalField(
        blank=False,
        unique=True,
        decimal_places=2,
        max_digits=4,
        help_text='Locations will be sorted by this index.  \
                    You can use up to 2 decimal places up to 99.99')


import datetime
from typing import Any, Dict, List, Optional, Type, TypeVar, Union, cast

import attr
from dateutil.parser import isoparse

from ..types import UNSET, Unset

T = TypeVar("T", bound="PatchedApplication")

@attr.s(auto_attribs=True)
class PatchedApplication:
    """Dynamically removes fields from serializer.
    https://stackoverflow.com/questions/27935558/dynamically-exclude-or-include-a-field-in-django-rest-framework-serializer"""

    url: Union[Unset, str] = UNSET
    id: Union[Unset, int] = UNSET
    ecosystem: Union[Unset, str] = UNSET
    primary_software: Union[Unset, str] = UNSET
    dependent_software: Union[Unset, List[str]] = UNSET
    machine_name: Union[Unset, str] = UNSET
    name: Union[Unset, str] = UNSET
    description: Union[Unset, str] = UNSET
    trello_board_url: Union[Unset, Optional[str]] = UNSET
    documentation_url: Union[Unset, Optional[str]] = UNSET
    pipelines: Union[Unset, List[str]] = UNSET
    owning_team: Union[Unset, str] = UNSET
    created: Union[Unset, datetime.datetime] = UNSET
    additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        url = self.url
        id = self.id
        ecosystem = self.ecosystem
        primary_software = self.primary_software
        dependent_software: Union[Unset, List[Any]] = UNSET
        if not isinstance(self.dependent_software, Unset):
            dependent_software = self.dependent_software

        machine_name = self.machine_name
        name = self.name
        description = self.description
        trello_board_url = self.trello_board_url
        documentation_url = self.documentation_url
        pipelines: Union[Unset, List[Any]] = UNSET
        if not isinstance(self.pipelines, Unset):
            pipelines = self.pipelines

        owning_team = self.owning_team
        created: Union[Unset, str] = UNSET
        if not isinstance(self.created, Unset):
            created = self.created.isoformat()

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if url is not UNSET:
            field_dict["url"] = url
        if id is not UNSET:
            field_dict["id"] = id
        if ecosystem is not UNSET:
            field_dict["ecosystem"] = ecosystem
        if primary_software is not UNSET:
            field_dict["primary_software"] = primary_software
        if dependent_software is not UNSET:
            field_dict["dependent_software"] = dependent_software
        if machine_name is not UNSET:
            field_dict["machine_name"] = machine_name
        if name is not UNSET:
            field_dict["name"] = name
        if description is not UNSET:
            field_dict["description"] = description
        if trello_board_url is not UNSET:
            field_dict["trello_board_url"] = trello_board_url
        if documentation_url is not UNSET:
            field_dict["documentation_url"] = documentation_url
        if pipelines is not UNSET:
            field_dict["pipelines"] = pipelines
        if owning_team is not UNSET:
            field_dict["owning_team"] = owning_team
        if created is not UNSET:
            field_dict["created"] = created

        return field_dict

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        d = src_dict.copy()
        url = d.pop("url", UNSET)

        id = d.pop("id", UNSET)

        ecosystem = d.pop("ecosystem", UNSET)

        primary_software = d.pop("primary_software", UNSET)

        dependent_software = cast(List[str], d.pop("dependent_software", UNSET))

        machine_name = d.pop("machine_name", UNSET)

        name = d.pop("name", UNSET)

        description = d.pop("description", UNSET)

        trello_board_url = d.pop("trello_board_url", UNSET)

        documentation_url = d.pop("documentation_url", UNSET)

        pipelines = cast(List[str], d.pop("pipelines", UNSET))

        owning_team = d.pop("owning_team", UNSET)

        created: Union[Unset, datetime.datetime] = UNSET
        _created = d.pop("created", UNSET)
        if not isinstance(_created, Unset):
            created = isoparse(_created)

        patched_application = cls(
            url=url,
            id=id,
            ecosystem=ecosystem,
            primary_software=primary_software,
            dependent_software=dependent_software,
            machine_name=machine_name,
            name=name,
            description=description,
            trello_board_url=trello_board_url,
            documentation_url=documentation_url,
            pipelines=pipelines,
            owning_team=owning_team,
            created=created,
        )

        patched_application.additional_properties = d
        return patched_application

    @property
    def additional_keys(self) -> List[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties


#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from distutils.core import setup
import py2exe #@UnusedImport
import sys, time

sys.path.append(r"d:\work\cm2\src")

reload(sys)
if hasattr(sys, "setdefaultencoding"): sys.setdefaultencoding("utf-8")

if len(sys.argv) == 1:
    sys.argv.append("py2exe")
    sys.argv.append("-q")

datafiles = [("imageformats", [ r"c:\Python26\Lib\site-packages\PyQt4\plugins\imageformats\qgif4.dll",
                                r"c:\Python26\Lib\site-packages\PyQt4\plugins\imageformats\qico4.dll",
                                r"c:\Python26\Lib\site-packages\PyQt4\plugins\imageformats\qjpeg4.dll" ]),
             ("", [r"yadd.ico"])
            ]
includes = ["sip", "xml.etree.ElementTree", "dbhash"]
excludes = ["doctest", "pdb", "difflib",
            '_pybsddb', '_scproxy', 'bsddb3.dbutils', 'cjkcodecs.aliases', 'iconv_codec' ]

setup(options={"py2exe": {
    "includes": includes,
    "excludes": excludes,
    "bundle_files": 3,
    "optimize": 2,
    "compressed": True,
    "ascii": True,
    "dist_dir": "build\\release",
    "dll_excludes": ["w9xpopen.exe"],
    "packages": ["encodings"]}},
    data_files=datafiles,
    zipfile=None,
    windows=[{
        "script": "yadd.py",
        "icon_resources": [(1, "yadd.ico")]
    }],
    name="Yadd",
    version="1.2.0.%s" % time.strftime("%m%d")
)