import struct

from Crypto.Util.py3compat import byte_string, bchr, bord

from Crypto.Util.number import long_to_bytes, bytes_to_long

__all__ = ['DerObject', 'DerInteger', 'DerBoolean', 'DerOctetString',
           'DerNull', 'DerSequence', 'DerObjectId', 'DerBitString', 'DerSetOf']

# Useful references:
# - https://luca.ntop.org/Teaching/Appunti/asn1.html
# - https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/
# - https://www.zytrax.com/tech/survival/asn1.html
# - https://www.oss.com/asn1/resources/books-whitepapers-pubs/larmouth-asn1-book.pdf
# - https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
# - https://misc.daniel-marschall.de/asn.1/oid-converter/online.php

def _is_number(x, only_non_negative=False):
    test = 0
    try:
        test = x + test
    except TypeError:
        return False
    return not only_non_negative or x >= 0

class BytesIO_EOF(object):
    """This class differs from BytesIO in that a ValueError exception is
    raised whenever EOF is reached."""

    def __init__(self, initial_bytes):
        self._buffer = initial_bytes
        self._index = 0
        self._bookmark = None

    def set_bookmark(self):
        self._bookmark = self._index

    def data_since_bookmark(self):
        assert self._bookmark is not None
        return self._buffer[self._bookmark:self._index]

    def remaining_data(self):
        return len(self._buffer) - self._index

    def read(self, length):
        new_index = self._index + length
        if new_index > len(self._buffer):
            raise ValueError("Not enough data for DER decoding: expected %d bytes and found %d" % (new_index, len(self._buffer)))

        result = self._buffer[self._index:new_index]
        self._index = new_index
        return result

    def read_byte(self):
        return bord(self.read(1)[0])

class DerObject(object):
        """Base class for defining a single DER object.

        This class should never be directly instantiated.
        """

        def __init__(self, asn1Id=None, payload=b'', implicit=None,
                     constructed=False, explicit=None):
                """Initialize the DER object according to a specific ASN.1 type.

                :Parameters:
                  asn1Id : integer or byte
                    The universal DER tag number for this object
                    (e.g. 0x10 for a SEQUENCE).
                    If None, the tag is not known yet.

                  payload : byte string
                    The initial payload of the object (that it,
                    the content octets).
                    If not specified, the payload is empty.

                  implicit : integer or byte
                    The IMPLICIT tag number (< 0x1F) to use for the encoded object.
                    It overrides the universal tag *asn1Id*.
                    It cannot be combined with the ``explicit`` parameter.
                    By default, there is no IMPLICIT tag.

                  constructed : bool
                    True when the ASN.1 type is *constructed*.
                    False when it is *primitive* (default).

                  explicit : integer or byte
                    The EXPLICIT tag number (< 0x1F) to use for the encoded object.
                    It cannot be combined with the ``implicit`` parameter.
                    By default, there is no EXPLICIT tag.
                """

                if asn1Id is None:
                    # The tag octet will be read in with ``decode``
                    self._tag_octet = None
                    return
                asn1Id = self._convertTag(asn1Id)

                self.payload = payload

                # In a BER/DER identifier octet:
                # * bits 4-0 contain the tag value
                # * bit 5 is set if the type is 'constructed'
                #   and unset if 'primitive'
                # * bits 7-6 depend on the encoding class
                #
                # Class        | Bit 7, Bit 6
                # ----------------------------------
                # universal    |   0      0
                # application  |   0      1
                # context-spec |   1      0 (default for IMPLICIT/EXPLICIT)
                # private      |   1      1
                #

                constructed_bit = 0x20 if constructed else 0x00

                if None not in (explicit, implicit):
                    raise ValueError("Explicit and implicit tags are"
                                     " mutually exclusive")

                if implicit is not None:
                    # IMPLICIT tag overrides asn1Id
                    self._tag_octet = 0x80 | constructed_bit | self._convertTag(implicit)
                elif explicit is not None:
                    # 'constructed bit' is always asserted for an EXPLICIT tag
                    self._tag_octet = 0x80 | 0x20 | self._convertTag(explicit)
                    self._inner_tag_octet = constructed_bit | asn1Id
                else:
                    # Neither IMPLICIT nor EXPLICIT
                    self._tag_octet = constructed_bit | asn1Id

        def _convertTag(self, tag):
                """Check if *tag* is a real DER tag (5 bits).
                Convert it from a character to number if necessary.
                """
                if not _is_number(tag):
                    if len(tag) == 1:
                        tag = bord(tag[0])
                # Ensure that tag is a low tag
                if not (_is_number(tag) and 0 <= tag < 0x1F):
                    raise ValueError("Wrong DER tag")
                return tag

        @staticmethod
        def _definite_form(length):
                """Build length octets according to BER/DER
                definite form.
                """
                if length > 127:
                        encoding = long_to_bytes(length)
                        return bchr(len(encoding) + 128) + encoding
                return bchr(length)

        def encode(self):
                """Return this DER element, fully encoded as a binary byte string."""

                # Concatenate identifier octets, length octets,
                # and contents octets

                output_payload = self.payload

                # In case of an EXTERNAL tag, first encode the inner
                # element.
                if hasattr(self, "_inner_tag_octet"):
                    output_payload = (bchr(self._inner_tag_octet) +
                                      self._definite_form(len(self.payload)) +
                                      self.payload)

                return (bchr(self._tag_octet) +
                        self._definite_form(len(output_payload)) +
                        output_payload)

        def _decodeLen(self, s):
                """Decode DER length octets from a file."""

                length = s.read_byte()

                if length > 127:
                    encoded_length = s.read(length & 0x7F)
                    if bord(encoded_length[0]) == 0:
                        raise ValueError("Invalid DER: length has leading zero")
                    length = bytes_to_long(encoded_length)
                    if length <= 127:
                        raise ValueError("Invalid DER: length in long form but smaller than 128")

                return length

        def decode(self, der_encoded, strict=False):
                """Decode a complete DER element, and re-initializes this
                object with it.

                Args:
                  der_encoded (byte string): A complete DER element.

                Raises:
                  ValueError: in case of parsing errors.
                """

                if not byte_string(der_encoded):
                    raise ValueError("Input is not a byte string")

                s = BytesIO_EOF(der_encoded)
                self._decodeFromStream(s, strict)

                # There shouldn't be other bytes left
                if s.remaining_data() > 0:
                    raise ValueError("Unexpected extra data after the DER structure")

                return self

        def _decodeFromStream(self, s, strict):
                """Decode a complete DER element from a file."""

                idOctet = s.read_byte()
                if self._tag_octet is not None:
                    if idOctet != self._tag_octet:
                        raise ValueError("Unexpected DER tag")
                else:
                    self._tag_octet = idOctet
                length = self._decodeLen(s)
                self.payload = s.read(length)

                # In case of an EXTERNAL tag, further decode the inner
                # element.
                if hasattr(self, "_inner_tag_octet"):
                    p = BytesIO_EOF(self.payload)
                    inner_octet = p.read_byte()
                    if inner_octet != self._inner_tag_octet:
                        raise ValueError("Unexpected internal DER tag")
                    length = self._decodeLen(p)
                    self.payload = p.read(length)

                    # There shouldn't be other bytes left
                    if p.remaining_data() > 0:
                        raise ValueError("Unexpected extra data after the DER structure")

class DerInteger(DerObject):
        """Class to model a DER INTEGER.

        An example of encoding is::

          >>> from Crypto.Util.asn1 import DerInteger
          >>> from binascii import hexlify, unhexlify
          >>> int_der = DerInteger(9)
          >>> print hexlify(int_der.encode())

        which will show ``020109``, the DER encoding of 9.

        And for decoding::

          >>> s = unhexlify(b'020109')
          >>> try:
          >>>   int_der = DerInteger()
          >>>   int_der.decode(s)
          >>>   print int_der.value
          >>> except ValueError:
          >>>   print "Not a valid DER INTEGER"

        the output will be ``9``.

        :ivar value: The integer value
        :vartype value: integer
        """

        def __init__(self, value=0, implicit=None, explicit=None):
                """Initialize the DER object as an INTEGER.

                :Parameters:
                  value : integer
                    The value of the integer.

                  implicit : integer
                    The IMPLICIT tag to use for the encoded object.
                    It overrides the universal tag for INTEGER (2).
                """

                DerObject.__init__(self, 0x02, b'', implicit,
                                   False, explicit)
                self.value = value  # The integer value

        def encode(self):
                """Return the DER INTEGER, fully encoded as a
                binary string."""

                number = self.value
                self.payload = b''
                while True:
                    self.payload = bchr(int(number & 255)) + self.payload
                    if 128 <= number <= 255:
                        self.payload = bchr(0x00) + self.payload
                    if -128 <= number <= 255:
                        break
                    number >>= 8
                return DerObject.encode(self)

        def decode(self, der_encoded, strict=False):
                """Decode a DER-encoded INTEGER, and re-initializes this
                object with it.

                Args:
                  der_encoded (byte string): A complete INTEGER DER element.

                Raises:
                  ValueError: in case of parsing errors.
                """

                return DerObject.decode(self, der_encoded, strict=strict)

        def _decodeFromStream(self, s, strict):
                """Decode a complete DER INTEGER from a file."""

                # Fill up self.payload
                DerObject._decodeFromStream(self, s, strict)

                if strict:
                    if len(self.payload) == 0:
                        raise ValueError("Invalid encoding for DER INTEGER: empty payload")
                    if len(self.payload) >= 2 and struct.unpack('>H', self.payload[:2])[0] < 0x80:
                        raise ValueError("Invalid encoding for DER INTEGER: leading zero")

                # Derive self.value from self.payload
                self.value = 0
                bits = 1
                for i in self.payload:
                    self.value *= 256
                    self.value += bord(i)
                    bits <<= 8
                if self.payload and bord(self.payload[0]) & 0x80:
                    self.value -= bits

class DerBoolean(DerObject):
    """Class to model a DER-encoded BOOLEAN.

    An example of encoding is::

    >>> from Crypto.Util.asn1 import DerBoolean
    >>> bool_der = DerBoolean(True)
    >>> print(bool_der.encode().hex())

    which will show ``0101ff``, the DER encoding of True.

    And for decoding::

    >>> s = bytes.fromhex('0101ff')
    >>> try:
    >>>   bool_der = DerBoolean()
    >>>   bool_der.decode(s)
    >>>   print(bool_der.value)
    >>> except ValueError:
    >>>   print "Not a valid DER BOOLEAN"

    the output will be ``True``.

    :ivar value: The boolean value
    :vartype value: boolean
    """
    def __init__(self, value=False, implicit=None, explicit=None):
        """Initialize the DER object as a BOOLEAN.

        Args:
          value (boolean):
            The value of the boolean. Default is False.

          implicit (integer or byte):
            The IMPLICIT tag number (< 0x1F) to use for the encoded object.
            It overrides the universal tag for BOOLEAN (1).
            It cannot be combined with the ``explicit`` parameter.
            By default, there is no IMPLICIT tag.

          explicit (integer or byte):
            The EXPLICIT tag number (< 0x1F) to use for the encoded object.
            It cannot be combined with the ``implicit`` parameter.
            By default, there is no EXPLICIT tag.
        """

        DerObject.__init__(self, 0x01, b'', implicit, False, explicit)
        self.value = value  # The boolean value

    def encode(self):
        """Return the DER BOOLEAN, fully encoded as a binary string."""

        self.payload = b'\xFF' if self.value else b'\x00'
        return DerObject.encode(self)

    def decode(self, der_encoded, strict=False):
        """Decode a DER-encoded BOOLEAN, and re-initializes this object with it.

        Args:
            der_encoded (byte string): A DER-encoded BOOLEAN.

        Raises:
            ValueError: in case of parsing errors.
        """

        return DerObject.decode(self, der_encoded, strict)

    def _decodeFromStream(self, s, strict):
        """Decode a DER-encoded BOOLEAN from a file."""

        # Fill up self.payload
        DerObject._decodeFromStream(self, s, strict)

        if len(self.payload) != 1:
            raise ValueError("Invalid encoding for DER BOOLEAN: payload is not 1 byte")

        if bord(self.payload[0]) == 0:
            self.value = False
        elif bord(self.payload[0]) == 0xFF:
            self.value = True
        else:
            raise ValueError("Invalid payload for DER BOOLEAN")

class DerSequence(DerObject):
        """Class to model a DER SEQUENCE.

        This object behaves like a dynamic Python sequence.

        Sub-elements that are INTEGERs behave like Python integers.

        Any other sub-element is a binary string encoded as a complete DER
        sub-element (TLV).

        An example of encoding is:

          >>> from Crypto.Util.asn1 import DerSequence, DerInteger
          >>> from binascii import hexlify, unhexlify
          >>> obj_der = unhexlify('070102')
          >>> seq_der = DerSequence([4])
          >>> seq_der.append(9)
          >>> seq_der.append(obj_der.encode())
          >>> print hexlify(seq_der.encode())

        which will show ``3009020104020109070102``, the DER encoding of the
        sequence containing ``4``, ``9``, and the object with payload ``02``.

        For decoding:

          >>> s = unhexlify(b'3009020104020109070102')
          >>> try:
          >>>   seq_der = DerSequence()
          >>>   seq_der.decode(s)
          >>>   print len(seq_der)
          >>>   print seq_der[0]
          >>>   print seq_der[:]
          >>> except ValueError:
          >>>   print "Not a valid DER SEQUENCE"

        the output will be::

          3
          4
          [4, 9, b'\x07\x01\x02']

        """

        def __init__(self, startSeq=None, implicit=None, explicit=None):
                """Initialize the DER object as a SEQUENCE.

                :Parameters:
                  startSeq : Python sequence
                    A sequence whose element are either integers or
                    other DER objects.

                  implicit : integer or byte
                    The IMPLICIT tag number (< 0x1F) to use for the encoded object.
                    It overrides the universal tag for SEQUENCE (16).
                    It cannot be combined with the ``explicit`` parameter.
                    By default, there is no IMPLICIT tag.

                  explicit : integer or byte
                    The EXPLICIT tag number (< 0x1F) to use for the encoded object.
                    It cannot be combined with the ``implicit`` parameter.
                    By default, there is no EXPLICIT tag.
                """

                DerObject.__init__(self, 0x10, b'', implicit, True, explicit)
                if startSeq is None:
                    self._seq = []
                else:
                    self._seq = startSeq

        # A few methods to make it behave like a python sequence

        def __delitem__(self, n):
                del self._seq[n]

        def __getitem__(self, n):
                return self._seq[n]

        def __setitem__(self, key, value):
                self._seq[key] = value

        def __setslice__(self, i, j, sequence):
                self._seq[i:j] = sequence

        def __delslice__(self, i, j):
                del self._seq[i:j]

        def __getslice__(self, i, j):
                return self._seq[max(0, i):max(0, j)]

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

        def __iadd__(self, item):
                self._seq.append(item)
                return self

        def append(self, item):
                self._seq.append(item)
                return self

        def insert(self, index, item):
                self._seq.insert(index, item)
                return self

        def hasInts(self, only_non_negative=True):
                """Return the number of items in this sequence that are
                integers.

                Args:
                  only_non_negative (boolean):
                    If ``True``, negative integers are not counted in.
                """

                items = [x for x in self._seq if _is_number(x, only_non_negative)]
                return len(items)

        def hasOnlyInts(self, only_non_negative=True):
                """Return ``True`` if all items in this sequence are integers
                or non-negative integers.

                This function returns False is the sequence is empty,
                or at least one member is not an integer.

                Args:
                  only_non_negative (boolean):
                    If ``True``, the presence of negative integers
                    causes the method to return ``False``."""
                return self._seq and self.hasInts(only_non_negative) == len(self._seq)

        def encode(self):
                """Return this DER SEQUENCE, fully encoded as a
                binary string.

                Raises:
                  ValueError: if some elements in the sequence are neither integers
                              nor byte strings.
                """
                self.payload = b''
                for item in self._seq:
                    if byte_string(item):
                        self.payload += item
                    elif _is_number(item):
                        self.payload += DerInteger(item).encode()
                    else:
                        self.payload += item.encode()
                return DerObject.encode(self)

        def decode(self, der_encoded, strict=False, nr_elements=None, only_ints_expected=False):
                """Decode a complete DER SEQUENCE, and re-initializes this
                object with it.

                Args:
                  der_encoded (byte string):
                    A complete SEQUENCE DER element.
                  nr_elements (None or integer or list of integers):
                    The number of members the SEQUENCE can have
                  only_ints_expected (boolean):
                    Whether the SEQUENCE is expected to contain only integers.
                  strict (boolean):
                    Whether decoding must check for strict DER compliancy.

                Raises:
                  ValueError: in case of parsing errors.

                DER INTEGERs are decoded into Python integers. Any other DER
                element is not decoded. Its validity is not checked.
                """

                self._nr_elements = nr_elements
                result = DerObject.decode(self, der_encoded, strict=strict)

                if only_ints_expected and not self.hasOnlyInts():
                    raise ValueError("Some members are not INTEGERs")

                return result

        def _decodeFromStream(self, s, strict):
                """Decode a complete DER SEQUENCE from a file."""

                self._seq = []

                # Fill up self.payload
                DerObject._decodeFromStream(self, s, strict)

                # Add one item at a time to self.seq, by scanning self.payload
                p = BytesIO_EOF(self.payload)
                while p.remaining_data() > 0:
                    p.set_bookmark()

                    der = DerObject()
                    der._decodeFromStream(p, strict)

                    # Parse INTEGERs differently
                    if der._tag_octet != 0x02:
                        self._seq.append(p.data_since_bookmark())
                    else:
                        derInt = DerInteger()
                        data = p.data_since_bookmark()
                        derInt.decode(data, strict=strict)
                        self._seq.append(derInt.value)

                ok = True
                if self._nr_elements is not None:
                    try:
                        ok = len(self._seq) in self._nr_elements
                    except TypeError:
                        ok = len(self._seq) == self._nr_elements

                if not ok:
                    raise ValueError("Unexpected number of members (%d)"
                                     " in the sequence" % len(self._seq))

class DerOctetString(DerObject):
    """Class to model a DER OCTET STRING.

    An example of encoding is:

    >>> from Crypto.Util.asn1 import DerOctetString
    >>> from binascii import hexlify, unhexlify
    >>> os_der = DerOctetString(b'\\xaa')
    >>> os_der.payload += b'\\xbb'
    >>> print hexlify(os_der.encode())

    which will show ``0402aabb``, the DER encoding for the byte string
    ``b'\\xAA\\xBB'``.

    For decoding:

    >>> s = unhexlify(b'0402aabb')
    >>> try:
    >>>   os_der = DerOctetString()
    >>>   os_der.decode(s)
    >>>   print hexlify(os_der.payload)
    >>> except ValueError:
    >>>   print "Not a valid DER OCTET STRING"

    the output will be ``aabb``.

    :ivar payload: The content of the string
    :vartype payload: byte string
    """

    def __init__(self, value=b'', implicit=None):
        """Initialize the DER object as an OCTET STRING.

        :Parameters:
          value : byte string
            The initial payload of the object.
            If not specified, the payload is empty.

          implicit : integer
            The IMPLICIT tag to use for the encoded object.
            It overrides the universal tag for OCTET STRING (4).
        """
        DerObject.__init__(self, 0x04, value, implicit, False)

class DerNull(DerObject):
    """Class to model a DER NULL element."""

    def __init__(self):
        """Initialize the DER object as a NULL."""

        DerObject.__init__(self, 0x05, b'', None, False)

class DerObjectId(DerObject):
    """Class to model a DER OBJECT ID.

    An example of encoding is:

    >>> from Crypto.Util.asn1 import DerObjectId
    >>> from binascii import hexlify, unhexlify
    >>> oid_der = DerObjectId("1.2")
    >>> oid_der.value += ".840.113549.1.1.1"
    >>> print hexlify(oid_der.encode())

    which will show ``06092a864886f70d010101``, the DER encoding for the
    RSA Object Identifier ``1.2.840.113549.1.1.1``.

    For decoding:

    >>> s = unhexlify(b'06092a864886f70d010101')
    >>> try:
    >>>   oid_der = DerObjectId()
    >>>   oid_der.decode(s)
    >>>   print oid_der.value
    >>> except ValueError:
    >>>   print "Not a valid DER OBJECT ID"

    the output will be ``1.2.840.113549.1.1.1``.

    :ivar value: The Object ID (OID), a dot separated list of integers
    :vartype value: string
    """

    def __init__(self, value='', implicit=None, explicit=None):
        """Initialize the DER object as an OBJECT ID.

        :Parameters:
          value : string
            The initial Object Identifier (e.g. "1.2.0.0.6.2").
          implicit : integer
            The IMPLICIT tag to use for the encoded object.
            It overrides the universal tag for OBJECT ID (6).
          explicit : integer
            The EXPLICIT tag to use for the encoded object.
        """
        DerObject.__init__(self, 0x06, b'', implicit, False, explicit)
        self.value = value

    def encode(self):
        """Return the DER OBJECT ID, fully encoded as a
        binary string."""

        comps = [int(x) for x in self.value.split(".")]

        if len(comps) < 2:
            raise ValueError("Not a valid Object Identifier string")
        if comps[0] > 2:
            raise ValueError("First component must be 0, 1 or 2")
        if comps[0] < 2 and comps[1] > 39:
            raise ValueError("Second component must be 39 at most")

        subcomps = [40 * comps[0] + comps[1]] + comps[2:]

        encoding = []
        for v in reversed(subcomps):
            encoding.append(v & 0x7F)
            v >>= 7
            while v:
                encoding.append((v & 0x7F) | 0x80)
                v >>= 7

        self.payload = b''.join([bchr(x) for x in reversed(encoding)])
        return DerObject.encode(self)

    def decode(self, der_encoded, strict=False):
        """Decode a complete DER OBJECT ID, and re-initializes this
        object with it.

        Args:
            der_encoded (byte string):
                A complete DER OBJECT ID.
            strict (boolean):
                Whether decoding must check for strict DER compliancy.

        Raises:
            ValueError: in case of parsing errors.
        """

        return DerObject.decode(self, der_encoded, strict)

    def _decodeFromStream(self, s, strict):
        """Decode a complete DER OBJECT ID from a file."""

        # Fill up self.payload
        DerObject._decodeFromStream(self, s, strict)

        # Derive self.value from self.payload
        p = BytesIO_EOF(self.payload)

        subcomps = []
        v = 0
        while p.remaining_data():
            c = p.read_byte()
            v = (v << 7) + (c & 0x7F)
            if not (c & 0x80):
                subcomps.append(v)
                v = 0

        if len(subcomps) == 0:
            raise ValueError("Empty payload")

        if subcomps[0] < 40:
            subcomps[:1] = [0, subcomps[0]]
        elif subcomps[0] < 80:
            subcomps[:1] = [1, subcomps[0] - 40]
        else:
            subcomps[:1] = [2, subcomps[0] - 80]

        self.value = ".".join([str(x) for x in subcomps])

class DerBitString(DerObject):
    """Class to model a DER BIT STRING.

    An example of encoding is:

    >>> from Crypto.Util.asn1 import DerBitString
    >>> bs_der = DerBitString(b'\\xAA')
    >>> bs_der.value += b'\\xBB'
    >>> print(bs_der.encode().hex())

    which will show ``030300aabb``, the DER encoding for the bit string
    ``b'\\xAA\\xBB'``.

    For decoding:

    >>> s = bytes.fromhex('030300aabb')
    >>> try:
    >>>   bs_der = DerBitString()
    >>>   bs_der.decode(s)
    >>>   print(bs_der.value.hex())
    >>> except ValueError:
    >>>   print "Not a valid DER BIT STRING"

    the output will be ``aabb``.

    :ivar value: The content of the string
    :vartype value: byte string
    """

    def __init__(self, value=b'', implicit=None, explicit=None):
        """Initialize the DER object as a BIT STRING.

        :Parameters:
          value : byte string or DER object
            The initial, packed bit string.
            If not specified, the bit string is empty.
          implicit : integer
            The IMPLICIT tag to use for the encoded object.
            It overrides the universal tag for BIT STRING (3).
          explicit : integer
            The EXPLICIT tag to use for the encoded object.
        """
        DerObject.__init__(self, 0x03, b'', implicit, False, explicit)

        # The bitstring value (packed)
        if isinstance(value, DerObject):
            self.value = value.encode()
        else:
            self.value = value

    def encode(self):
        """Return the DER BIT STRING, fully encoded as a
        byte string."""

        # Add padding count byte
        self.payload = b'\x00' + self.value
        return DerObject.encode(self)

    def decode(self, der_encoded, strict=False):
        """Decode a complete DER BIT STRING, and re-initializes this
        object with it.

        Args:
            der_encoded (byte string): a complete DER BIT STRING.
            strict (boolean):
                Whether decoding must check for strict DER compliancy.

        Raises:
            ValueError: in case of parsing errors.
        """

        return DerObject.decode(self, der_encoded, strict)

    def _decodeFromStream(self, s, strict):
        """Decode a complete DER BIT STRING DER from a file."""

        # Fill-up self.payload
        DerObject._decodeFromStream(self, s, strict)

        if self.payload and bord(self.payload[0]) != 0:
            raise ValueError("Not a valid BIT STRING")

        # Fill-up self.value
        self.value = b''
        # Remove padding count byte
        if self.payload:
            self.value = self.payload[1:]

class DerSetOf(DerObject):
    """Class to model a DER SET OF.

    An example of encoding is:

    >>> from Crypto.Util.asn1 import DerBitString
    >>> from binascii import hexlify, unhexlify
    >>> so_der = DerSetOf([4,5])
    >>> so_der.add(6)
    >>> print hexlify(so_der.encode())

    which will show ``3109020104020105020106``, the DER encoding
    of a SET OF with items 4,5, and 6.

    For decoding:

    >>> s = unhexlify(b'3109020104020105020106')
    >>> try:
    >>>   so_der = DerSetOf()
    >>>   so_der.decode(s)
    >>>   print [x for x in so_der]
    >>> except ValueError:
    >>>   print "Not a valid DER SET OF"

    the output will be ``[4, 5, 6]``.
    """

    def __init__(self, startSet=None, implicit=None):
        """Initialize the DER object as a SET OF.

        :Parameters:
          startSet : container
            The initial set of integers or DER encoded objects.
          implicit : integer
            The IMPLICIT tag to use for the encoded object.
            It overrides the universal tag for SET OF (17).
        """
        DerObject.__init__(self, 0x11, b'', implicit, True)
        self._seq = []

        # All elements must be of the same type (and therefore have the
        # same leading octet)
        self._elemOctet = None

        if startSet:
            for e in startSet:
                self.add(e)

    def __getitem__(self, n):
        return self._seq[n]

    def __iter__(self):
        return iter(self._seq)

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

    def add(self, elem):
        """Add an element to the set.

        Args:
            elem (byte string or integer):
              An element of the same type of objects already in the set.
              It can be an integer or a DER encoded object.
        """

        if _is_number(elem):
            eo = 0x02
        elif isinstance(elem, DerObject):
            eo = self._tag_octet
        else:
            eo = bord(elem[0])

        if self._elemOctet != eo:
            if self._elemOctet is not None:
                raise ValueError("New element does not belong to the set")
            self._elemOctet = eo

        if elem not in self._seq:
            self._seq.append(elem)

    def decode(self, der_encoded, strict=False):
        """Decode a complete SET OF DER element, and re-initializes this
        object with it.

        DER INTEGERs are decoded into Python integers. Any other DER
        element is left undecoded; its validity is not checked.

        Args:
            der_encoded (byte string): a complete DER BIT SET OF.
            strict (boolean):
                Whether decoding must check for strict DER compliancy.

        Raises:
            ValueError: in case of parsing errors.
        """

        return DerObject.decode(self, der_encoded, strict)

    def _decodeFromStream(self, s, strict):
        """Decode a complete DER SET OF from a file."""

        self._seq = []

        # Fill up self.payload
        DerObject._decodeFromStream(self, s, strict)

        # Add one item at a time to self.seq, by scanning self.payload
        p = BytesIO_EOF(self.payload)
        setIdOctet = -1
        while p.remaining_data() > 0:
            p.set_bookmark()

            der = DerObject()
            der._decodeFromStream(p, strict)

            # Verify that all members are of the same type
            if setIdOctet < 0:
                setIdOctet = der._tag_octet
            else:
                if setIdOctet != der._tag_octet:
                    raise ValueError("Not all elements are of the same DER type")

            # Parse INTEGERs differently
            if setIdOctet != 0x02:
                self._seq.append(p.data_since_bookmark())
            else:
                derInt = DerInteger()
                derInt.decode(p.data_since_bookmark(), strict)
                self._seq.append(derInt.value)
        # end

    def encode(self):
        """Return this SET OF DER element, fully encoded as a
        binary string.
        """

        # Elements in the set must be ordered in lexicographic order
        ordered = []
        for item in self._seq:
            if _is_number(item):
                bys = DerInteger(item).encode()
            elif isinstance(item, DerObject):
                bys = item.encode()
            else:
                bys = item
            ordered.append(bys)
        ordered.sort()
        self.payload = b''.join(ordered)
        return DerObject.encode(self)


import asyncio
import random
import pymysql
from kutana import Plugin

plugin = Plugin(name="Jokes", description="Send random jokes in random time to subscribers")

subscribers = []
conn = pymysql.connect(host='localhost', port=3307, user='root', password='*', db='novichok')

async def bg_loop(vk):
    while True:
        with conn:
            cursor = conn.cursor()
            cursor.execute("SELECT anek FROM anekdots ORDER BY rand() LIMIT 1")
            joke = cursor.fetchone()

        for sub in subscribers:
            await vk.send_message(sub, joke[0])

        await asyncio.sleep(random.randrange(15 * 60, 2 * 60 * 60))

@plugin.on_start()
async def _(app):
    backend = app.get_backends()[0]

    # Run only if first backend is Vkontakte
    if backend.get_identity() == "vkontakte":
        asyncio.ensure_future(bg_loop(backend))

@plugin.on_commands(["jokes sub"])
async def _(msg, ctx):
    subscribers.append([msg.receiver_id, ctx.body])
    await ctx.reply("OK")

@plugin.on_commands(["jokes unsub"])
async def _(msg, ctx):
    subscribers.remove(msg.receiver_id)
    await ctx.reply("OK")


"""
* 페어의 노드스왑
연결 리스트를 입력받아 페어 단위로 스왑하라.
- 입력
1->2->3->4
- 출력
2->1->4->3
"""

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

    def print_list(self):
        cur = self
        while cur:
            print(cur.val, end='->')
            cur = cur.next

class Solution:
    '''
    root : 리턴 값
    head : 바꿀 첫 번째 노드
    b : 바꿀 두 번째 노드
    prev : 바꿀 첫 번째 노드의 이전 노드
    ex) prev -> head -> b
    '''
    # 반복 구조로 스왑
    def swapPairs(self, head: ListNode) -> ListNode:
        root = prev = ListNode(None)
        prev.next = head
        while head and head.next:
            # b가 a(head)를 가리키도록
            b = head.next
            head.next = b.next
            b.next = head

            # prev가 b를 가리키도록
            prev.next = b

            # 다음 번 비교를 위해 이동
            head = head.next
            prev = prev.next.next
        return root.next

if __name__ == '__main__':
    solution = Solution()
    param = ListNode(1, ListNode(2, ListNode(3, ListNode(4))))
    result = solution.swapPairs(param)
    result.print_list()


#!/usr/bin/env python

#TCP Scanner
#Author: Chandika Lasiru
#Blog: https://clasiru.blogspot.com

import socket
import sys
import urllib.request
import time
import argparse

banner = """
 ████████╗ ██████╗██████╗ ███████╗
 ╚══██╔══╝██╔════╝██╔══██╗██╔════╝
    ██║   ██║     ██████╔╝███████╗
    ██║   ██║     ██╔═══╝ ╚════██║
    ██║   ╚██████╗██║     ███████║
    ╚═╝    ╚═════╝╚═╝     ╚══════╝
                       TCP Scanner
                    by Area Master
"""

print (banner, "\n");

def google_ok():
    try:
        urllib.request.urlopen('https://www.google.com', timeout=10);
        return True
    except:
        return False
    return True

def yahoo_ok():
    try:
        urllib.request.urlopen('https://www.yahoo.com', timeout=10);
        return True
    except:
        return False
    return True

def target_ok():
    try:
        urllib.request.urlopen('http://'+ip, timeout=10);
        return True
    except:
        return False
    return True

def is_valid_host():
    try:
        socket.gethostbyname(arghost)
        return True
    except:
        return False

def is_valid_minport():
    try:
        int(argsport)
        return True
    except ValueError:
        return False

def is_valid_maxport():
    try:
        int(argeport)
        return True
    except ValueError:
        return False

def scan_tcp_range():
    print ()
    print ('[*] Scanning range: ', int(argsport), 'to', int(argeport))
    print ()
    print ('[*]  PORT     STATE    SERVICE')
    for tcprange in range(int(argsport), int(argeport)+1):
        stcprange = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        contcprange = stcprange.connect_ex((ip, tcprange))
        try:
            service = socket.getservbyport(tcprange)
        except socket.error:
            service = ''
            pass
        if(contcprange == 0) :
            print ('[+] %d /TCP  OPEN' % (tcprange),'   ', service)
        else:
            print ('[ ] %d /TCP  CLOSE' % (tcprange))
        stcprange.close()

def scan_tcp_list():
    print ()
    print ('[*] Scanning Port List: ', argportlist)
    print ()
    print ('[*]  PORT     STATE    SERVICE')
    for tcplist in list(map(int,argportlist)):
        stcplist = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        contcplist = stcplist.connect_ex((ip, tcplist))
        try:
            service = socket.getservbyport(tcplist)
        except socket.error:
            service = ''
            pass
        if(contcplist == 0) :
            print ('[+] %d /TCP  OPEN' % (tcplist),'   ', service)
        else:
            print ('[ ] %d /TCP  CLOSE' % (tcplist))
        stcplist.close()

def scan_tcp_default():
    print ()
    print ('[*] Scanning Default Port List: ', argdefault)
    print ()
    print ('[*]  PORT     STATE    SERVICE')
    for tcpdef in list(map(int,argdefault)):
        stcpdef = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        contcpdef = stcpdef.connect_ex((ip, tcpdef))
        try:
            service = socket.getservbyport(tcpdef)
        except socket.error:
            service = ''
            pass
        if(contcpdef == 0) :
            print ('[+] %d /TCP  OPEN' % (tcpdef),'   ', service)
        else:
            print ('[ ] %d /TCP  CLOSE' % (tcpdef))
        stcpdef.close()

parser = argparse.ArgumentParser('python3 tcps.py', description='Simple Port Scanner for scanning TCP ports in target hosts')

parser.add_argument('--host', default='127.0.0.1', help="Enter IP address or Hostname", type=str)
parser.add_argument('--default', help="Use default port range (21, 22, 23, 80, 443)", action="store_true")
parser.add_argument('--list', help="Enable port(s) list scanning", action="store_true")
parser.add_argument('--pL', default=[21, 22, 23, 80, 443],  help='Ports for port list scanning (Eg: --pL 21 22 80)', nargs='+')
parser.add_argument('--range', help="Enable port range scanning", action="store_true")
parser.add_argument('--sP', default='1', help='Starting port number for port range scanning (between 1 and 65534)', type=int)
parser.add_argument('--eP', default='65534', help='Ending port number for port range scanning (between 1 and 65534)', type=int)

args = parser.parse_args()

arghost = args.host
argportlist = args.pL
argsport = args.sP
argeport = args.eP

ip = socket.gethostbyname(arghost)

localtime = time.asctime(time.localtime(time.time()))

if len(sys.argv)==1:
    parser.print_help();
    sys.exit(0)

if is_valid_host():
    print ('[*] Scanning started at', localtime)
    if google_ok() or yahoo_ok():
        print ('[*] Internet Status: Online')
    else:
        print ('[*] Internet: Offline')
    print ('[*] Target Host: ', ip)
    if target_ok():
        print ('[*] Target Status: Appears Online')
    else:
        print ('[*] Target Status: Appears Offline')
    if args.list:
        scan_tcp_list()
    elif args.range:
        scan_tcp_range()
    elif args.default:
        argdefault = [21, 22, 23, 80, 443]
        scan_tcp_default()
    else:
        print ()
        print ('[*] Nothing to scan. Options are wrong !')
    print ()
    print ('[*] Scanning finished at', localtime)
    print ('[*] Have a nice day !')

else:
    print ('[*] Host: ', arghost, 'is invalid')

sys.exit(0)


import sys

data = [line.rstrip() for line in sys.stdin.readlines()]
data = [line.split(' -> ') for line in data]
data = [[line[0].split(','),line[1].split(',')] for line in data]
data = [[list(map(int,line[0])), list(map(int,line[1]))] for line in data]

field_size = 1000
field = [ [0 for x in range(field_size)] for y in range(field_size) ]
for p1,p2 in data:
    if p1[0]==p2[0]:
        x = p1[0]
        y1 = min(p1[1],p2[1])
        y2 = max(p1[1],p2[1])
        for y in range(y1,y2+1):
            field[x][y] += 1
    elif p1[1]==p2[1]:
        y = p1[1]
        x1 = min(p1[0],p2[0])
        x2 = max(p1[0],p2[0])
        for x in range(x1,x2+1):
            field[x][y] += 1

num_overlaps = 0
for row in field:
    for value in row:
        if value>1:
            num_overlaps += 1
print(num_overlaps)


# src/api/users/views.py

from flask import request  # removed Blueprint and Api, replaced with Namespace
from flask_restx import Namespace, Resource, fields  # Api removed

from src.api.users.crud import (  # isort:skip
    get_all_users,
    get_user_by_email,
    add_user,
    get_user_by_id,
    update_user,
    delete_user,
)

users_namespace = Namespace("users")

# link a Flask-RESTX API to a Flask Blueprint.
# users_blueprint = Blueprint("users", __name__)
# api = Api(users_blueprint)

# create an api model to define the shape of Userslist
user = users_namespace.model(
    "User",
    {
        "id": fields.Integer(readOnly=True),
        "username": fields.String(required=True),
        "email": fields.String(required=True),
        "created_date": fields.DateTime,
    },
)

# define the route handler.
class UsersList(Resource):
    @users_namespace.expect(user, validate=True)
    @users_namespace.response(201, "<user_email> was added!")
    @users_namespace.response(400, "Sorry. That email already exists.")
    def post(self):
        """Creates a new user."""
        post_data = request.get_json()
        username = post_data.get("username")
        email = post_data.get("email")
        response_object = {}

        user = get_user_by_email(email)
        if user:
            response_object["message"] = "Sorry. That email already exists."
            return response_object, 400

        add_user(username, email)

        response_object["message"] = f"{email} was added!"
        return response_object, 201

    @users_namespace.marshal_with(user, as_list=True)
    def get(self):
        """Returns all users."""
        return get_all_users(), 200

class Users(Resource):
    @users_namespace.marshal_with(
        user
    )  # user model used as a serialiser to generate a JSON object from the model's fields.
    @users_namespace.response(200, "Success")
    @users_namespace.response(404, "User <user_id> does not exist.")
    def get(self, user_id):
        """Returns a single user."""
        user = get_user_by_id(user_id)
        if not user:
            users_namespace.abort(404, f"User {user_id} does not exist")
        return user, 200

    @users_namespace.expect(user, validate=True)
    @users_namespace.response(200, "<user_id> was updated!")
    @users_namespace.response(400, "Sorry. That email already exists.")
    @users_namespace.response(404, "User <user_id> does not exist")
    def put(self, user_id):
        """Updates a user."""
        post_data = request.get_json()
        username = post_data.get("username")
        email = post_data.get("email")
        response_object = {}

        user = get_user_by_id(user_id)
        if not user:
            users_namespace.abort(404, f"User {user_id} does not exist")

        if get_user_by_email(email):
            response_object["message"] = "Sorry. That email already exists."
            return response_object, 400

        update_user(user, username, email)

        response_object["message"] = f"{user.id} was updated!"
        return response_object, 200

    @users_namespace.response(200, "<user_id> was removed!")
    @users_namespace.response(404, "User <user_id> does not exist")
    def delete(self, user_id):
        """Deletes a user."""
        response_object = {}
        user = get_user_by_id(user_id)

        if not user:
            users_namespace.abort(404, f"User {user_id} does not exist")

        delete_user(user)

        response_object["message"] = f"{user.email} was removed!"
        return response_object, 200

# api.add_resource(UsersList, "/users")
# api.add_resource(Users, "/users/<int:user_id>")

users_namespace.add_resource(UsersList, "")
users_namespace.add_resource(Users, "/<int:user_id>")


from classes import Env, HumanSnake, AISnake, Game
import numpy as np
import json
import sys

N = int(sys.argv[1])
mem = int(sys.argv[2]) # 0 1 2 3 supported
num = int(sys.argv[3])
num_iters_play = 1000
alphas = [0.1, 0.3, 0.5, 0.7, 0.9, 1.1]
eps = 0.05

best_q =dict()
best_mean = 0
best_sc = dict()
best_alpha = 0

#snake = HumanSnake()
for alpha in alphas: # broad sweep
    alpha = round(alpha,3)
    snake = AISnake(N=N,mem=mem, alpha=alpha)
    game = Game(snake, N)
    print(f'\n{N},{mem} a: ',alpha)
    game.train(num=int(num), print_every=int(num/5))
    sc = game.play(num=num_iters_play, print_score=False)
    mean = np.sum([k*v for k,v in sc.items()])/num_iters_play
    print(f'Mean({alpha}): ', mean)
    if best_mean < mean:
        best_alpha = alpha
        best_mean = mean
        best_sc = sc
        best_q = snake.Q

with open(f'final_weights/{N}x{N}-{mem}.json','w') as f:
    print(f'{N},{mem} Best distr: ',best_sc)
    print(f'Mean({best_alpha}): ', best_mean)
    json.dump({
                'N': N,
                'distribution': best_sc,
                'mean': best_mean,
                'weights': {k:[v[0].tolist(),v[1]] for k,v in best_q.items()},
                },
                f,
                indent=2,
            )


from django.template import RequestContext
from django.shortcuts import render_to_response
from django.views.generic import list_detail
from crystalscreener.screens.models import Screen
from django.db.models import Q

def search(request, what):
	#what = what.split()[-1]
	if what and len(what) >= 3:
		clause = Q(salt1__icontains=what)\
			   | Q(salt2__icontains=what)\
			   | Q(salt3__icontains=what)\
			   | Q(salt4__icontains=what)\
			   | Q(prec1__icontains=what)\
			   | Q(prec2__icontains=what)\
			   | Q(prec3__icontains=what)
	screen = Screen.objects.filter(clause)
	return render_to_response('screens/screen_list.html', {'object_list':screen})

def screen_list(request, name):
	#screen = Screen.objects.filter(screen_name__icontains=name)
	screen = Screen.objects.values()
	out = []
	for s in screen:
		if s['screen_name'] == name:
			out.append(s)
	out.sort()
	screen = out
	return render_to_response('screen_list.html', {'screen':screen})

def list_all_screens(request):
	val = Screen.objects.values()
	screen_names = []
	for v in val:
		if v['screen_name'] not in screen_names:
			screen_names.append(v['screen_name'])
	screen_names.sort()
	return render_to_response('list_all_screens.html', {'screen_names':screen_names})


# Import Packages
# import numpy as np
import pandas as pd
from utils.loss_functions import mean_squared_error
from models.model_based.SVDpp import SurSVDpp
from models.cross_validation.parameter_test import parameter_test
from models.cross_validation.ModelTester import ModelTester

ratings_dict = {"itemID": [1, 1, 1, 2, 2, 2, 2],
                "userID": [9, 32, 2, 45, 20, 9, 32],
                "ratings": [0, 0, 1, 0, 1, 1, 0]}

df = pd.DataFrame(ratings_dict)
df = df[["userID", "itemID", "ratings"]]

print(df)

ratings_matrix = df.pivot(index="userID",
                          columns="itemID",
                          values="ratings")
print("\n")
print(ratings_matrix.head())

a = SurSVDpp()
a.fit(ratings_matrix)

print("\nOutput")
print(a.predict(user=2, item=2))

k_values = [1, 2, 3]
cv_times = 3

model_tester = ModelTester(ratios=(0.5, 0.2, 0.3),
                           model_based=True,
                           seed=42)
d_test, d_train = parameter_test(k_val=k_values,
                                 cv_times=cv_times,
                                 model=SurSVDpp,
                                 loss_function=mean_squared_error,
                                 model_tester=model_tester,
                                 data=ratings_matrix)


import datetime
import os
import pickle
import random

import h5py
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch import sigmoid
from torch.utils.data import Dataset
import torch.nn.init as init
from tqdm import tqdm

from synet.evaluation.evl import *
from synet.models.synet import Block, SYnet
from synet.config import b_size, num_labels, freq_dim, epoch_size,\
    audio_data_path

date = datetime.datetime.now()

def get_row_avg_std(data):
    print('Get std and average')
    common_sum = 0
    square_sum = 0

    # remove zero padding
    tfle = 0
    for i in range(len(data)):
        tfle += (data[i].sum(0) != 0).astype('int').sum()
        common_sum += data[i].sum(-1)
        square_sum += (data[i] ** 2).sum(-1)

    common_avg = common_sum / tfle
    square_avg = square_sum / tfle
    std = np.sqrt(square_avg - common_avg ** 2)

    return common_avg, std

def get_weighted_loss(data, beta):
    class_prior = data[:].sum(0) / data[:].sum()
    mean_prior = class_prior.mean()

    loss_weight = (
        (mean_prior / class_prior) * (1 - mean_prior) / (1 - class_prior)
        ) ** beta

    return loss_weight

def model_init(m):
    classname = m.__class__.__name__

    if classname.find('Conv') != -1:
        init.xavier_uniform_(m.weight, gain=np.sqrt(2))
        init.constant_(m.bias, 0)

    elif classname.find('BatchNorm') != -1:
        init.constant_(m.weight, 1)
        init.constant_(m.bias, 0)

    elif classname.find('Linear') != -1:
        init.xavier_uniform_(m.weight, gain=np.sqrt(2))

def load_audio(test_index_save_path):
    tr_data = h5py.File(audio_data_path, 'r')

    data_length = len(tr_data['x'])
    np.random.seed(19941115)
    index = np.random.permutation(data_length)
    train_index = list(np.sort(index[1000:]))
    test_index = list(np.sort(index[:1000]))

    Xtr = tr_data['x'][train_index]
    Ytr = tr_data['y'][train_index]
    Xva = tr_data['x'][test_index]
    Yva = tr_data['y'][test_index]

    avg, std = get_row_avg_std(Xtr)
    loss_weight = get_weighted_loss(Ytr, beta=1)

    test_index_save_path = os.path.join(test_index_save_path, 'test_index.pkl')

    with open(test_index_save_path, 'wb') as f:
        pickle.dump(test_index, f)

    return Xtr, Ytr, Xva, Yva, avg, std, loss_weight

class Data2Torch(Dataset):
    def __init__(self, data):
        self.X = data[0]
        self.Y = data[1]

    def __getitem__(self, index):
        mX = torch.from_numpy(self.X[index]).float()
        mY = torch.from_numpy(self.Y[index]).float()

        return mX, mY

    def __len__(self):

        return len(self.X)

class Net(nn.Module):
    def __init__(
            self, freq_dim, num_labels, avg=None, std=None):
        super(Net, self).__init__()

        self.model = SYnet(freq_dim, num_labels)

        if avg is not None:
            self.avg = nn.Parameter(torch.from_numpy(avg).float(),
                                    requires_grad=False
                                    )

        else:
            self.avg = nn.Parameter(torch.zeros(freq_dim).float(),
                                    requires_grad=False
                                    )

        if std is not None:
            self.std = nn.Parameter(torch.from_numpy(std).float(),
                                    requires_grad=False
                                    )

        else:
            self.std = nn.Parameter(torch.ones(freq_dim).float(),
                                    requires_grad=False
                                    )

    def forward(self, x):
        batch, freq_dim, length = x.size()
        avg = self.avg.view(1, freq_dim, 1).expand(batch, -1, length)
        std = self.std.view(1, freq_dim, 1).expand(batch, -1, length)
        x = (x - avg) / std

        macro_out, micro_out = self.model(x)

        return macro_out, micro_out

def mm_loss(target, macro_out, micro_out, loss_weight):
    loss_weight = torch.from_numpy(loss_weight).float().to(device)
    loss = 0
    loss_weight = loss_weight.view(1, -1).repeat(target.size(0), 1)

    loss_fn = torch.nn.BCEWithLogitsLoss(weight=loss_weight)
    loss += loss_fn(macro_out, target)

    for att_sc, det in micro_out:
        det_size = det.size()

        fl_target = target.view(
            det_size[0], det_size[1], 1).repeat(1, 1, det_size[2])

        loss_weight = loss_weight.view(
            det_size[0], det_size[1], 1
            ).repeat(1, 1, det_size[2])

        loss_weight = att_sc * loss_weight
        loss_weight = loss_weight.detach()

        loss_fn = torch.nn.BCEWithLogitsLoss(weight=loss_weight)
        loss += loss_fn(det, fl_target)

    return loss

class Trainer:
    def __init__(self, model, lr, epoch, save_fn, device, loss_weight):
        self.epoch = epoch
        self.lr = lr
        self.save_fn = save_fn
        self.loss_weight = loss_weight
        self.model = model.to(device)

    def tester(self, loader, size, th, generate_th):
        all_pred = np.zeros((size, num_labels))
        all_tar = np.zeros((size, num_labels))

        self.model.eval()

        with torch.no_grad():
            ds = 0
            for idx, _input in enumerate(loader):

                data = _input[0].to(device)
                target = _input[1].to(device)

                pred, frame_pred = self.model(data)

                all_tar[ds: ds + len(target)] = target.cpu().numpy()
                all_pred[ds: ds + len(target)] = sigmoid(pred).cpu().numpy()
                ds += len(target)

            if generate_th:
                va_th, evl_metrics, va_out = evl_for_th(all_tar, all_pred, th)

                return va_th, evl_metrics, va_out

            else:
                evl_metrics, va_out = evl(all_tar, all_pred, th)

        return evl_metrics, va_out

# random.shuffle not return value, that a problem!!
    def downsampling(self, data, target):
        happys = []
        aggressives = []
        sads = []
        calms = []

        for i, y in enumerate(target):
            if y[0] == 1:
                happys.append(i)

            if y[1] == 1:
                aggressives.append(i)

            if y[2] == 1:
                sads.append(i)

            if y[3] == 1:
                calms.append(i)

        label_lens = [len(x) for x in [happys, aggressives, sads, calms]]
        sample_size = min(label_lens)
        random.shuffle(happys)
        random.shuffle(aggressives)
        random.shuffle(sads)
        random.shuffle(calms)

        sample_indexs = happys[:sample_size] + aggressives[:sample_size] +\
            sads[:sample_size] + calms[:sample_size]
        random.shuffle(sample_indexs)

        data = data[sample_indexs]
        target = target[sample_indexs]

        return data, target

    def fit(self, tr_loader, va_loader):
        save_dict = {}

        def lambda_for_optim(epoch):
            return self.lr ** ((epoch / 50) + 1) / self.lr

        optimizer = optim.SGD(
            self.model.parameters(),
            lr=self.lr, momentum=0.9,
            weight_decay=1e-4
            )

        scheduler = torch.optim.lr_scheduler.LambdaLR(
            optimizer, lr_lambda=lambda_for_optim)

        for e in range(1, self.epoch + 1):
            loss_total = 0

            with tqdm(total=len(tr_loader)) as t:
                t.set_description('Epoch %2d/%2d' % (e, self.epoch))

                for batch_idx, _input in enumerate(tr_loader):
                    data = _input[0]
                    target = _input[1]

                    # data, target = self.downsampling(data, target)
                    data = data.to(device)
                    target = target.to(device)
                    macro_out, micro_out = self.model(data)

                    loss = mm_loss(
                        target,
                        macro_out,
                        micro_out,
                        self.loss_weight
                        )

                    loss_total += loss.data
                    optimizer.zero_grad()
                    loss.backward()
                    optimizer.step()

                    t.update(1)
                    t.set_postfix(loss='%.4f' % loss.data)

            scheduler.step()

            va_th, evl_metrics, va_out = self.tester(
                tr_loader, len(tr_loader.dataset), [], True)

            evl_metrics, va_out = self.tester(
                va_loader, len(va_loader.dataset), va_th, False)

            print(np.around(evl_metrics[:, 0], decimals=3))
            save_dict['state_dict'] = self.model.state_dict()
            save_dict['va_out'] = va_out
            save_dict['va_th'] = va_th
            save_dict['evl_metrics'] = evl_metrics
            torch.save(save_dict, os.path.join(self.save_fn, '_e_%d' % (e)))

def main():
    # load data
    t_kwargs = {'batch_size': b_size, 'num_workers': 0,
                'pin_memory': False, 'drop_last': True}

    v_kwargs = {'batch_size': b_size, 'num_workers': 0, 'pin_memory': False}

    out_model_fn = os.path.join(
        './trained_model',
        '%d%d%d' % (date.year, date.month, date.day)
        )

    if not os.path.exists(out_model_fn):
        os.makedirs(out_model_fn)

    Xtr, Ytr, Xva, Yva, avg, std, loss_weight = load_audio(
        test_index_save_path=out_model_fn
        )

    trdata = [Xtr, Ytr]
    vadata = [Xva, Yva]

    tr_loader = torch.utils.data.DataLoader(
        Data2Torch(trdata),
        shuffle=True,
        **t_kwargs
        )

    va_loader = torch.utils.data.DataLoader(Data2Torch(vadata), **v_kwargs)
    print('finishing data building...')

    # load model
    model = Net(
        freq_dim=freq_dim,
        num_labels=num_labels,
        avg=avg,
        std=std
        )

    model.apply(model_init)
    print(model)
    print('finishing model loading...')

    # start training
    trer = Trainer(
        model, 0.01, epoch_size, out_model_fn, device, loss_weight
        )

    trer.fit(tr_loader, va_loader)

    print(out_model_fn)

if __name__ == '__main__':
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    main()


class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        i,j=0,len(matrix[0])-1
        while i<len(matrix) and j>-1:
            curr=matrix[i][j]
            if curr==target:
                return True
            elif target>curr:
                i+=1
            elif target<curr:
                j-=1
        return False

codes/accepted_codes/Search a 2D Matrix II_460992473.py


from django.contrib.auth import get_user_model
from rest_framework import serializers
from rest_framework.validators import UniqueValidator
from .models import RobotInfo

User = get_user_model()

class RobotCreateSerializer(serializers.ModelSerializer):
    name = serializers.CharField(required=True,
                                 label='robot_name',
                                 allow_blank=False,
                                 validators=[UniqueValidator(
                                     queryset=RobotInfo.objects.all(),
                                     message="robot is already exist."
                                 )])

    class Meta:
        model = RobotInfo
        fields = ['name', 'type', 'user']

class RobotDetailSerializer(serializers.ModelSerializer):
    """
    Robot Detail Serializer
    """

    class Meta:
        model = RobotInfo
        fields = "__all__"


import threading

class SyncList:
    lock = threading.Lock()

    def __init__(self):
        self.__list = []

    def append(self, element):
        SyncList.lock.acquire()
        self.__list.append(element)
        SyncList.lock.release()

    def insert(self, index, element):
        SyncList.lock.acquire()
        self.__list.insert(index, element)
        SyncList.lock.release()

    def pop(self, index=None):
        SyncList.lock.acquire()
        if index is None:
            self.__list.pop()
        else:
            self.__list.pop(index)
        SyncList.lock.release()

    def __getitem__(self, item):
        with SyncList.lock:
            if isinstance(item, int):
                if 0 <= item < len(self.__list):
                    return self.__list[item]
            return None

    def clear(self):
        SyncList.lock.acquire()
        self.__list.clear()
        SyncList.lock.release()

    def extend(self, sync_list):
        SyncList.lock.acquire()
        self.__list.extend(sync_list.__list)
        SyncList.lock.release()

    def __str__(self):
        return str(self.__list)

def f1(sync_list):
    sync_list.append(0)
    sync_list.append(1)
    sync_list.append(2)
    sync_list.insert(0, 3)
    sync_list.pop()

def f2(sync_list):
    sync_list.append(4)
    sync_list.pop(1)
    sync_list.append(9)

sync = SyncList()
t1 = threading.Thread(target=f1, args=(sync,))
t2 = threading.Thread(target=f2, args=(sync,))
t1.start()
t2.start()
t1.join()
t2.join()
print("Both threads are finished")
print(sync)