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 numpy as np
from matplotlib import pyplot as plt
from astropy.io import fits as p
from sklearn.mixture import GaussianMixture as GMM
from sklearn.neighbors import KernelDensity as KDE
from itertools import combinations as comb

catalog_name="../Catalogues/Coma_matched_compact_test.fits"
#catalog_name="../Catalogues/Coma_matched_compact.fits"
c_hdu=p.open(catalog_name)
cdata=c_hdu[1].data

def gauss_function(x, amp, x0, sigma):
    return amp * np.exp(-(x - x0) ** 2. / (2. * sigma ** 2.))

plt.clf()
filters="ugiH"

combs=[cc for cc in comb(filters,2)]
aics=[]
bics=[]
ncompsmax=2
for nc in range(2,ncompsmax+1):
	gmm = GMM(n_components=nc, covariance_type="full", tol=0.001)
	ax1=plt.subplot2grid((3,3),(0,0))
	ax2=plt.subplot2grid((3,3),(0,1))
	ax3=plt.subplot2grid((3,3),(0,2))
	ax4=plt.subplot2grid((3,3),(1,1))
	ax5=plt.subplot2grid((3,3),(1,2))
	ax6=plt.subplot2grid((3,3),(2,2))
	axes=[ax1,ax2,ax3,ax4,ax5,ax6]
	for i,c in enumerate(combs):
		colour=cdata[c[0]]-cdata[c[1]]
		gmm=gmm.fit(np.expand_dims(colour,1))
		gmm_x=np.sort(colour)
		gmm_y=np.exp(gmm.score_samples(gmm_x.reshape(-1,1)))
		kde=KDE(bandwidth=(gmm_x[-1]-gmm_x[0])/30,kernel="epanechnikov")
		kde.fit(colour[:,np.newaxis])
		#axes[i].hist(colour, bins=np.linspace(gmm_x[0],gmm_x[-1],20), normed=True, alpha=0.5, color="blue")
		axes[i].fill(gmm_x,np.exp(kde.score_samples(gmm_x[:,np.newaxis])),ec="blue",fc="blue",alpha=.5)
		axes[i].plot(gmm_x, gmm_y, color="black", lw=2, label="GMM")
		axes[i].set_xlabel(c[0]+" - "+c[1],weight="heavy")
		for m, cc, w in zip(gmm.means_.ravel(), gmm.covariances_.ravel(), gmm.weights_.ravel()):
			gauss = gauss_function(x=gmm_x, amp=1, x0=m, sigma=np.sqrt(cc))
			gauss = gauss / np.trapz(gauss, gmm_x) * w
			axes[i].plot(gmm_x,gauss,color="grey")
		bic=gmm.bic(np.expand_dims(colour,1))
		aic=gmm.aic(np.expand_dims(colour,1))
		aics.append(aic); bics.append(bic)
		#axes[i].set_title("BIC,AIC = "+str(bic)+", "+str(aic),fontsize=10)

	plt.show()
plt.clf()
aics=np.reshape(aics,(ncompsmax,6))
bics=np.reshape(bics,(ncompsmax,6))
fig=plt.figure()
for i in range(6):
	fig.add_subplot(2,3,i+1)
	plt.plot(range(1,ncompsmax+1),bics[:,i],"red",label="BIC")
	plt.plot(range(1,ncompsmax+1),bics[:,i],"ro")
	plt.plot(range(1,ncompsmax+1),aics[:,i],"blue",label="AIC")
	plt.plot(range(1,ncompsmax+1),aics[:,i],"bo")
	plt.title(str(combs[i][0]) + " - "+str(combs[i][1]))
	plt.yscale("symlog")
	plt.legend()
plt.show()


from __future__ import print_function
from . import operatornames
from . import taskdmrg
import numpy as np
import os
from scipy.interpolate import interp1d
from . import multioperator
from .edtk import timedependent as tded

def evolution_DC(self,mode="DMRG",**kwargs):
    if mode=="DMRG":  return evolution_dmrg_DC(self,**kwargs)
    if mode=="ED":
        edobj = self.get_ED_obj() # get the ED object
        return tded.evolution_DC(edobj,h=self.hamiltonian,**kwargs)

def evolution_dmrg_DC(self,name="XX",nt=10000,dt=0.1,restart=True,**kwargs):
    name = operatornames.str2MO(self,name,**kwargs)
    if self.fit_td: fittd = "true"
    else: fittd = "false"
    if self.tevol_custom_exp: tevol_custom_exp = "true"
    else: tevol_custom_exp = "false"
    fittd = "true"
    task = {"time_evolution":"true",
            "tevol_nt":str(nt),
            "tevol_fit":fittd,
            "tevol_custom_exp":tevol_custom_exp,
            "tevol_dt":str(dt),
            }
    self.task = task # override tasks
    if restart: # restart the calculation
      self.execute(lambda: os.system("cp psi_GS.mps psi_time_evolution.mps"))
    name[0] = name[0].get_dagger()
    self.execute(lambda: name[0].write(name="dc_multioperator_i.in"))
    self.execute(lambda: name[1].write(name="dc_multioperator_j.in"))
    self.execute( lambda : taskdmrg.write_tasks(self)) # write tasks
    self.execute( lambda : self.run()) # run calculation
    cs = self.get_file("TIME_EVOLUTION.OUT").transpose() # time evolution
    ts = np.array([dt*ii for ii in range(nt)]) # times
    return ts,cs[0].real-1j*cs[1] # return

def evolve_and_measure(self,mode="DMRG",**kwargs):
    """Evolve and measure"""
    if mode=="DMRG": return evolve_and_measure_dmrg(self,**kwargs)
    elif mode=="ED":
        edobj = self.get_ED_obj() # get the ED object
        h = self.hamiltonian # get the ED object
        return tded.evolve_and_measure(edobj,h,**kwargs)

def evolve_and_measure_dmrg(self,operator=None,nt=1000,h=None,
        dt=1e-2,wf=None,**kwargs):
    if h is None: h = self.hamiltonian # Hamiltonian
    if self.fit_td: fittd = "true"
    else: fittd = "false"
    if self.tevol_custom_exp: tevol_custom_exp = "true"
    else: tevol_custom_exp = "false"
    fittd = "true"
    task = {"evolution_measure":"true",
            "tevol_nt":str(int(nt)),
            "tevol_fit":fittd,
            "tevol_custom_exp":tevol_custom_exp,
            "tevol_dt":str(dt),
            }
    self.task = task # override tasks
    if wf is None: wf = self.wf0 # get ground state
    wf.write(name="psi_evolve_and_measure.mps") # copy wavefunction
    self.execute(lambda: h.write(name="hamiltonian.in"))
    self.execute(lambda: operator.write(name="time_evolution_multioperator.in"))
    self.execute( lambda : taskdmrg.write_tasks(self)) # write tasks
    self.execute( lambda : self.run()) # run calculation
    cs = self.get_file("TIME_EVOLUTION.OUT").transpose() # time evolution
    ts = np.array([dt*ii for ii in range(int(nt))]) # times
    return ts,cs[0].real-1j*cs[1] # return

def evolution_ABA(self,A=None,B=None,mode="DMRG",wf=None,**kwargs):
    """Apply an operator, evolve and measure"""
    if A is None: A = multioperator.identity()
    if B is None: B = multioperator.identity()
    if mode=="DMRG":
        if wf is None: wf = self.get_gs() # get ground state
        wfA = A*wf # apply the operator
        return evolve_and_measure_dmrg(self,wf=wfA,operator=B,**kwargs)
    elif mode=="ED":
        edobj = self.get_ED_obj() # get the ED object
        return tded.evolution_ABA(edobj,h=self.hamiltonian,A=A,B=B,wf=wf,
                **kwargs)

def dynamical_correlator(self,window=[-1,10],es=None,dt=0.1,
        nt=None,factor=1,delta=5e-2,**kwargs):
    """Compute a certain dynamical correlator"""
    self.get_gs() # get the ground state
    if nt is None: nt=int(100/delta/dt)
    (ts,cs) = evolution_DC(self,dt=dt,nt=nt,**kwargs) # get correlator
#    cs = cs*np.exp(-1j*self.e0*ts) # factor out the phase
    # interpolate the time evolution
    ftr = interp1d(ts,cs.real,fill_value=0.0,bounds_error=False)
    fti = interp1d(ts,cs.imag,fill_value=0.0,bounds_error=False)
    # interpolate the time evolution
    tnew = np.linspace(np.min(ts),np.max(ts),len(ts)*factor) # ten times
    cnew = ftr(tnew) + 1j*fti(tnew)
    ts = tnew.copy() # overwrite
    cs = cnew.copy() # overwrite
    dtnew = dt/factor
    # do the fourier transform
    ss = np.fft.fft(cs) # fourier transform
    ws = np.fft.fftfreq(len(cs),d=dtnew)*2.*np.pi # fourier frequencies
    fr = interp1d(ws,ss.real,fill_value=0.0,bounds_error=False)
    fi = interp1d(ws,ss.imag,fill_value=0.0,bounds_error=False)
    if es is None:
        es = np.linspace(window[0],window[1],800)
    gr = fr(es)+ 1j*fi(es) # advanced
    ga = np.conjugate(gr) # retarded
#    gp = fr(es) - fr(-es) + 1j*fi(es) + 1j*fi(-es)
    return (es,gr/np.sqrt(nt))

def generic_evolution(H,wf,normalize=True,dt=1e-2,nt=100,A=None):
    """Perform a time evolution and project onto itself,
    assuming U = e^tH """
    wf0 = wf.copy() # copy wavefunction
    wf1 = wf.copy() # copy wavefunction
    out = []
    for i in range(int(nt)): # loop
        wf1 = wf1 + dt*H*wf1
        if normalize:  wf1 = wf1*(1./np.sqrt(wf1.dot(wf1)))
  #      out.append(wf0.dot(wf1)) # compute
        out.append(wf1.dot(A*wf1)) # compute
        print(i)
    return np.array(range(int(nt)))*dt,np.array(out) # retunr result


import sys
from datetime import datetime
from dateutil.parser import parse
from django.conf import settings
from pillow_retry.models import PillowError, Stub
from django.test import TestCase
from pillow_retry.tasks import process_pillow_retry
from dimagi.utils.decorators.memoized import memoized
from pillowtop.couchdb import CachedCouchDB
from pillowtop.listener import BasicPillow

def get_ex_tb(message, ex_class=None):
    ex_class = ex_class if ex_class else ExceptionA
    try:
        raise ex_class(message)
    except Exception as e:
        return e, sys.exc_info()[2]

def create_error(change, message='message', attempts=0, pillow=None, ex_class=None):
    error = PillowError.get_or_create(change, pillow or FakePillow())
    for n in range(0, attempts):
        error.add_attempt(*get_ex_tb(message, ex_class=ex_class))
    return error

class PillowRetryTestCase(TestCase):
    def setUp(self):
        PillowError.objects.all().delete()

    def test_id(self):
        id = '12345'
        change_dict = {'id': id, 'seq': 54321}
        error = create_error(change_dict)
        self.assertEqual(error.doc_id, id)
        self.assertEqual(error.pillow, 'pillow_retry.tests.FakePillow')
        self.assertEqual(error.change_object.id, id)
        self.assertEqual(error.change_object.sequence_id, 54321)

    def test_attempts(self):
        message = 'ex message'
        error = create_error({'id': '123'}, message=message, attempts=1)
        self.assertEqual(error.total_attempts, 1)
        self.assertEqual(error.current_attempt, 1)
        self.assertTrue(message in error.error_traceback)
        self.assertEqual(error.error_type, 'pillow_retry.tests.ExceptionA')

        message = 'ex message2'
        error.add_attempt(*get_ex_tb(message))
        self.assertEqual(error.total_attempts, 2)
        self.assertEqual(error.current_attempt, 2)
        self.assertTrue(message in error.error_traceback)

    def test_get_or_create(self):
        message = 'abcd'
        id = '12335'
        error = create_error({'id': id}, message=message, attempts=2)
        error.save()

        get = PillowError.get_or_create({'id': id}, FakePillow())
        self.assertEqual(get.total_attempts, 2)
        self.assertEqual(get.current_attempt, 2)
        self.assertTrue(message in error.error_traceback)

        new = PillowError.get_or_create({'id': id}, FakePillow1())
        self.assertIsNone(new.id)
        self.assertEqual(new.current_attempt, 0)

    def test_get_or_create_meta(self):
        id = '12335'
        date = '2013-12-05T08:52:19Z'
        meta = {
            'domains': ['a' * 247, '123456789'],
            'doc_type': 'something',
            'date': date,
        }
        get = PillowError.get_or_create({'id': id}, FakePillow(), meta)
        self.assertEqual(get.domains, 'a' * 247 + ',1234...')
        self.assertEqual(get.doc_type, 'something')
        self.assertEqual(get.doc_date, parse(date))
        get.save()

    def test_null_meta_date(self):
        id = '12335'
        meta = {
            'domains': ['a' * 247, '123456789'],
            'doc_type': 'something',
            'date': None,
        }
        get = PillowError.get_or_create({'id': id}, FakePillow(), meta)
        self.assertEqual(None, get.doc_date)

    def test_get_errors_to_process(self):
        # Only re-process errors with
        # current_attempt < setting.PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS
        date = datetime.utcnow()
        for i in range(0, 5):
            error = create_error({'id': i}, attempts=i+1)
            error.date_next_attempt = date.replace(day=i+1)
            error.save()

        errors = PillowError.get_errors_to_process(
            date.replace(day=1),
        ).all()
        self.assertEqual(len(errors), 1)

        errors = PillowError.get_errors_to_process(
            date.replace(day=5),
        ).all()
        self.assertEqual(len(errors), 3)

    def test_get_errors_to_process_queued(self):
        date = datetime.utcnow()
        error = create_error({'id': 1}, attempts=0)
        error.date_next_attempt = date
        error.save()

        queued_error = create_error({'id': 2}, attempts=0)
        queued_error.date_next_attempt = date
        queued_error.queued = True
        queued_error.save()

        errors = PillowError.get_errors_to_process(
            date,
        ).all()
        self.assertEqual(len(errors), 1)
        self.assertEqual(error.id, errors[0]['id'])

    def test_get_errors_to_process_queued_update(self):
        date = datetime.utcnow()
        error = create_error({'id': 1}, attempts=0)
        error.date_next_attempt = date
        error.save()

        errors = PillowError.get_errors_to_process(
            date,
        ).all()
        self.assertEqual(len(errors), 1)

        # check that calling update on the return value has the desired effect
        errors.update(queued=True)

        errors = PillowError.get_errors_to_process(
            date,
        ).all()
        self.assertEqual(len(errors), 0)

    def test_get_errors_to_process_max_limit(self):
        # see settings.PILLOW_RETRY_MULTI_ATTEMPTS_CUTOFF
        date = datetime.utcnow()

        def make_error(id, current_attempt, total_attempts):
            error = create_error({'id': id})
            error.date_next_attempt = date
            error.current_attempt = current_attempt
            error.total_attempts = total_attempts
            error.save()

        # current_attempts <= limit, total_attempts <= limit
        make_error(
            'to-process1',
            settings.PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS,
            settings.PILLOW_RETRY_MULTI_ATTEMPTS_CUTOFF
        )

        # current_attempts = 0, total_attempts > limit
        make_error(
            'to-process2',
            0,
            settings.PILLOW_RETRY_MULTI_ATTEMPTS_CUTOFF + 1
        )

        # current_attempts > limit, total_attempts <= limit
        make_error(
            'not-processed1',
            settings.PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS + 1,
            settings.PILLOW_RETRY_MULTI_ATTEMPTS_CUTOFF
        )

        # current_attempts <= limit, total_attempts > limit
        make_error(
            'not-processed2',
            settings.PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS,
            settings.PILLOW_RETRY_MULTI_ATTEMPTS_CUTOFF + 1
        )

        errors = PillowError.get_errors_to_process(date, fetch_full=True).all()
        self.assertEqual(len(errors), 2)
        docs_to_process = {e.doc_id for e in errors}
        self.assertEqual({'to-process1', 'to-process2'}, docs_to_process)

    def test_include_doc(self):
        """
        see FakePillow.process_change
        """
        id = 'test_doc'
        FakePillow.get_couch_db()._docs[id] = {'id': id, 'property': 'value'}
        change_dict = {'id': id, 'seq': 54321}
        error = create_error(change_dict)
        error.save()
        process_pillow_retry(error.id)

        #  if processing is successful the record will be deleted
        with self.assertRaises(PillowError.DoesNotExist):
            PillowError.objects.get(id=error.id)

    def test_deleted_doc(self):
        id = 'test_doc'
        change_dict = {'id': id, 'seq': 54321}
        error = create_error(change_dict)
        error.save()
        # this used to error out
        process_pillow_retry(error.id)
        with self.assertRaises(PillowError.DoesNotExist):
            PillowError.objects.get(id=error.id)

    def test_bulk_reset(self):
        for i in range(0, 5):
            error = create_error({'id': i}, attempts=settings.PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS)
            error.save()

        errors = PillowError.get_errors_to_process(datetime.utcnow()).all()
        self.assertEqual(len(errors), 0)

        PillowError.bulk_reset_attempts(datetime.utcnow())

        errors = PillowError.get_errors_to_process(datetime.utcnow()).all()
        self.assertEqual(len(errors), 5)

    def test_bulk_reset_cutoff(self):
        for i in range(0, 3):
            error = create_error({'id': i}, attempts=1)
            if i >= 1:
                error.total_attempts = PillowError.multi_attempts_cutoff() + 1
            error.save()

        errors = PillowError.get_errors_to_process(datetime.utcnow()).all()
        self.assertEqual(len(errors), 0)

        PillowError.bulk_reset_attempts(datetime.utcnow())

        errors = PillowError.get_errors_to_process(datetime.utcnow()).all()
        self.assertEqual(len(errors), 2)

    def test_pillow_not_found(self):
        error = PillowError.objects.create(
            doc_id='missing-pillow',
            pillow='badmodule.NotARealPillow',
            date_created=datetime.utcnow(),
            date_last_attempt=datetime.utcnow()
        )
        # make sure this doesn't error
        process_pillow_retry(error.id)
        # and that its total_attempts was bumped above the threshold
        self.assertTrue(PillowError.objects.get(pk=error.pk).total_attempts > PillowError.multi_attempts_cutoff())

class FakePillow(BasicPillow):

    @staticmethod
    @memoized
    def get_couch_db():
        return CachedCouchDB(Stub.get_db().uri, readonly=True)

    def process_change(self, change, is_retry_attempt=False):
        #  see test_include_doc
        if not change.get('deleted') and 'doc' not in change:
            raise Exception('missing doc in change')

class FakePillow1(BasicPillow):

    @staticmethod
    @memoized
    def get_couch_db(self):
        return CachedCouchDB(Stub.get_db().uri, readonly=True)

class ExceptionA(Exception):
    pass


import os
import time
from tqdm import tqdm

import subprocess
import numpy as np
import cv2
import filterpy

import torch
import super_gradients as sg
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = (20, 10)
from super_gradients.training import models

model = models.get("yolo_nas_s", pretrained_weights="coco").cuda()
model.eval();
video_path = './test1.mp4'

cap = cv2.VideoCapture(video_path)
if (cap.isOpened() == False):
    print("Error opening video file")

while(cap.isOpened()):

  # read each video frame
  ret, frame = cap.read()
  frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

  break

cap.release()
del cap

from super_gradients.training.models.detection_models.customizable_detector import CustomizableDetector
from super_gradients.training.pipelines.pipelines import DetectionPipeline

# make sure to set IOU and confidence in the pipeline constructor
pipeline = DetectionPipeline(
            model=model,
            image_processor=model._image_processor,
            post_prediction_callback=model.get_post_prediction_callback(iou=0.25, conf=0.30),
            class_names=model._class_names,
        )

def get_prediction(image_in, pipeline):
  # Preprocess
  preprocessed_image, processing_metadata = pipeline.image_processor.preprocess_image(image=image_in.copy())

  # Predict
  with torch.no_grad():
      torch_input = torch.Tensor(preprocessed_image).unsqueeze(0).to('cuda')
      model_output = model(torch_input)
      prediction = pipeline._decode_model_output(model_output, model_input=torch_input)

  # Postprocess
  return pipeline.image_processor.postprocess_predictions(predictions=prediction[0], metadata=processing_metadata)

from ocsort import ocsort

tracker = ocsort.OCSort(det_thresh=0.25)

import colorsys

def get_color(number):
    """ Converts an integer number to a color """
    # change these however you want to
    hue = number*30 % 180
    saturation = number*103 % 256
    value = number*50 % 256

    # expects normalized values
    color = colorsys.hsv_to_rgb(hue/179, saturation/255, value/255)

    return [int(c*255) for c in color]

# get frame info for tracker and video saving
h, w = (720, 1280)
h2, w2 = h//2, w//2
# h2, w2 = 640, 640 # this degrades performance

# OCSORT automatically rescales bboxes if we inference with a diff img size
img_info = (h, w)
img_size = (h2, w2)

tracker = ocsort.OCSort(det_thresh=0.60, max_age=10, min_hits=10)

cap = cv2.VideoCapture(video_path)

if (cap.isOpened() == False):
    print("Error opening video file")

frames = []
i = 0
counter, fps, elapsed = 0, 0, 0
start_time = time.perf_counter()
track_ids_op = []
track_bbox_op = []
track_hits_op = []
while(cap.isOpened()):

  # read each video frame (read time is about 0.006 sec)
  ret, frame = cap.read()

  if ret == True:

    # read image and resize by half for inference
    og_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    frame = cv2.resize(og_frame,
                       (w2, h2), interpolation=cv2.INTER_LINEAR)

    # perform inference on small frame and get (x1, y1, x2, y2, confidence)
    pred = get_prediction(frame, pipeline)
    xyxyc = np.hstack((pred.bboxes_xyxy,
                      np.c_[pred.confidence]))

    # update tracker
    tracks = tracker.update(xyxyc, img_info, img_size)

    # draw tracks on frame
    for track in tracker.trackers:

        track_id = track.id
        hits = track.hits
        color = get_color(track_id*15)
        x1,y1,x2,y2 = np.round(track.get_state()).astype(int).squeeze()
        if (track_id not in track_ids_op) and (hits>10):

            crop_img = og_frame[y1:y2, x1:x2].copy()
            if(crop_img.shape[0]<32 or crop_img.shape[1]<32):
                print(';dibg')
            else:
                track_ids_op.append(track_id)
                track_hits_op.append(hits)
                print(crop_img.shape)
                filename = './images/' + str(track_id) + '.jpg'
                crop_img = cv2.cvtColor(crop_img, cv2.COLOR_RGB2BGR)
                cv2.imwrite(filename , crop_img)
        else:
            if(hits>10):
                track_hits_op[track_ids_op.index(track_id)] = hits

        cv2.rectangle(og_frame, (x1,y1),(x2,y2), color, 2)
        cv2.putText(og_frame,
                  f"{track_id}-{hits}",
                  (x1+10,y1-5),
                  cv2.FONT_HERSHEY_SIMPLEX,
                  0.5,
                  color,
                  1,
                  cv2.LINE_AA)

    # update FPS and place on frame
    current_time = time.perf_counter()
    elapsed = (current_time - start_time)
    counter += 1
    if elapsed > 1:
      fps = counter / elapsed;
      counter = 0;
      start_time = current_time;

    cv2.putText(og_frame,
                f"FPS: {np.round(fps, 2)}",
                (10,h - 10),
                cv2.FONT_HERSHEY_SIMPLEX,
                1,
                (255,255,255),
                2,
                cv2.LINE_AA)

    # append to list
    frames.append(og_frame)

  # Break the loop
  else:
    break

# release video capture object
cap.release()
del cap

video_savepath = 'output_video.mp4'
from moviepy.editor import VideoFileClip

videoclip = VideoFileClip(video_path)
video_fps = videoclip.fps

out = cv2.VideoWriter(video_savepath,
                      cv2.VideoWriter_fourcc(*'mp4v'),
                      video_fps,
                      (w, h))

for frame in frames:
    out.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))

out.release()
del out

with open('track_ids.txt', 'w') as f:
    for tio in track_ids_op:
        f.write(str(tio)+"\n")
with open('track_hits.txt', 'w') as f:
    for tho in track_hits_op:
        f.write(str(tho)+"\n")

#
p1=subprocess.Popen(["python","gui.py"],shell=True)
p1.wait()


from __future__ import annotations

import abc
import colorsys
import math
import typing as ta
from contextlib import contextmanager

import bpy
from bl_math import clamp
from mathutils import Matrix, Vector

if ta.TYPE_CHECKING:
    from . import Color, ColorComponent

class Transform:
    def __init__(
        self,
        matrix: ta.Optional[Matrix] = None,
        color: Color = (0.0, 0.0, 1.0, 1.0),
    ):
        self._matrix = matrix or Matrix()
        self._color = color

    @contextmanager
    def scale(
        self,
        x: ta.Optional[float] = None,
        y: ta.Optional[float] = None,
        z: ta.Optional[float] = None,
        xyz: float = 1.0,
    ):
        """Scale specified dimension. xyz is a shortcut for scaling all
        dimensions equally
        """
        matrix = self._matrix
        self._matrix = self._matrix @ Matrix.Diagonal(
            (x or xyz, y or xyz, z or xyz, 1.0)
        )
        yield
        self._matrix = matrix

    @contextmanager
    def translate(self, x: float = 0.0, y: float = 0.0, z: float = 0.0):
        matrix = self._matrix
        self._matrix = self._matrix @ Matrix.Translation((x, y, z))
        yield
        self._matrix = matrix

    @contextmanager
    def rotate(self, angle: float, axis: ta.Union[ta.Literal["X", "Y", "Z"], Vector]):
        """Rotate angle radians around axis"""
        matrix = self._matrix
        self._matrix = self._matrix @ Matrix.Rotation(angle, 4, axis)
        yield
        self._matrix = matrix

    @property
    def matrix(self) -> Matrix:
        return self._matrix

    @property
    def color_rgba(self) -> Color:
        return hsva_to_rgba(self._color)

    @contextmanager
    def color(
        self,
        hue: ta.Optional[ColorComponent] = None,
        saturation: ta.Optional[ColorComponent] = None,
        value: ta.Optional[ColorComponent] = None,
        alpha: ta.Optional[ColorComponent] = None,
        color: ta.Optional[Color] = None,
    ):
        """Transform color

        If color is specified, it is used as the new base color
        hue increments the hue value, and wraps around.
        saturation, value and alpha are multipliers and clamp to 0..1
        """
        current_color = self._color
        base_color = color or self._color
        self._color = (
            base_color[0] if hue is None else math.modf(base_color[0] + hue)[0],
            base_color[1] if saturation is None else clamp(base_color[1] * saturation),
            base_color[2] if value is None else clamp(base_color[2] * value),
            base_color[3] if alpha is None else clamp(base_color[3] * alpha),
        )
        yield
        self._color = current_color

    def apply(self, transformer: Transformer):
        transformer.transform(self)

class Transformer(abc.ABC):
    """Base transformer, applies matrix but not color"""

    def transform(self, xfm: Transform):
        self.apply_matrix(xfm.matrix)
        self.apply_color(xfm.color_rgba)

    @abc.abstractmethod
    def apply_matrix(self, matrix: Matrix):
        """Apply transformation matrix to object"""

    def apply_color(self, color: Color):
        """Apply color to object"""
        pass

class ObjectTransformer(Transformer):
    def __init__(self, obj: bpy.types.Object):
        self._obj = obj

    @property
    def obj(self) -> bpy.types.Object:
        return self._obj

    def apply_matrix(self, matrix: Matrix):
        """Apply transformation matrix to object"""
        self.obj.matrix_world = matrix

def hsva_to_rgba(color: Color) -> Color:
    """Convert color from HSVA to RGBA"""
    return (*colorsys.hsv_to_rgb(*color[:3]), color[3])


from django.http import HttpResponse, JsonResponse
from processor import *
import json

def index(req):
    return HttpResponse('
Welcome to admin chatbot
')

def chatbot(req):
    if req.method == 'POST':
        data = json.loads(req.body)
        response = chatbot_response(data.get('question'))
    return JsonResponse({"response": response});


import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression

def second_task():
    # Выгрузка данных
    data_2015 = pd.read_csv("csv/2015.csv")

    # Чтение данных
    data_2015.head()

    # Проверка на наличие пропущенных значений
    for column in data_2015.columns:
        missing = np.mean(data_2015[column].isna() * 100)
        # print(f"{column} : {round(missing, 1)}%")

    # Статистика по данным
    data_2015.describe()

    # Построение матрицы корреляции по одной целевой переменной
    corr_matrix = data_2015.corr().Happiness_Score.to_frame().round(2)
    # print(corr_matrix)

    # Модель LinearRegression
    model = LinearRegression()

    # Переменные для модели
    x = data_2015[['Economy (GDP per Capita)']]
    y = data_2015['Happiness_Score']
    x = np.array(x, type(float))
    y = np.array(y, type(float))

    # Коэффициенты сдвига и наклона
    model.fit(x, y)
    print(model.coef_, model.intercept_)

second_task()


import numpy as np
from multiprocessing import Pool
from .util import flatten_array, ensure_numpy, check_common_input, make_output
from pyrepseq import levenshtein_neighbors, hamming_neighbors

AMINO_ACIDS = 'ACDEFGHIKLMNPQRSTVWY'

def generate_neighbors(query, max_edits, is_hamming):
    neighbor_func = hamming_neighbors if is_hamming else levenshtein_neighbors
    ans = {query: 0}
    for edit_distance in range(1, max_edits+1):
        for seq in ans.copy():
            for new_seq in neighbor_func(seq):
                if new_seq not in ans:
                    ans[new_seq] = edit_distance
    return ans

def build_index(seqs):
    ans = {}
    for index, seq in enumerate(seqs):
        if seq not in ans:
            ans[seq] = []
        ans[seq].append(index)
    return ans

def single_lookup(args):
    index, max_edits, limit, custom_distance, max_cust_dist = _params
    ans, (x_index, seq), is_hamming = [], args, custom_distance == 'hamming'
    neighbors = generate_neighbors(seq, max_edits, is_hamming)

    for possible_edit, edit_distance in neighbors.items():
        if possible_edit in index and edit_distance <= max_edits:
            for y_index in index[possible_edit]:
                if x_index == y_index:
                    continue
                if custom_distance in (None, 'hamming'):
                    ans.append((x_index, y_index, edit_distance))
                else:
                    dist = custom_distance(seq, possible_edit)
                    if dist <= max_cust_dist:
                        ans.append((x_index, y_index, dist))
    return ans if limit is None else ans[0:limit]

def lookup(index, seqs, max_edits, max_returns, n_cpu,
           custom_distance, max_cust_dist):
    global _params
    _params = (index, max_edits, max_returns, custom_distance, max_cust_dist)

    _loop = enumerate(seqs)
    if n_cpu == 1:
        result = map(single_lookup, _loop)
    else:
        with Pool(n_cpu) as p:
            chunk = int(len(seqs)/n_cpu)
            result = p.map(single_lookup, _loop, chunksize=chunk)
    return flatten_array(result)

def brute_lookup(seqs, max_edits=1, max_returns=None, n_cpu=1,
                 custom_distance=None, max_custom_distance=float('inf'),
                 output_type='triplets'):
    """
    List all neighboring CDR3B sequences efficiently for small edit distances.
    The idea is to list all possible sequences within a given distance and lookup the dictionary if it exists.

    Parameters
    ----------
    strings : iterable of strings
        list of CDR3B sequences
    max_edits : int
        maximum edit distance defining the neighbors
    max_returns : int or None
        maximum neighbor size
    n_cpu : int
        number of CPU cores running in parallel
    custom_distance : Function(str1, str2) or "hamming"
        custom distance function to use, must statisfy 4 properties of distance (https://en.wikipedia.org/wiki/Distance#Mathematical_formalization)
    max_custom_distance : float
        maximum distance to include in the result, ignored if custom distance is not supplied
    output_type: string
        format of returns, can be "triplets", "coo_matrix", or "ndarray"

    Returns
    -------
    neighbors : array of 3D-tuples, sparse matrix, or dense matrix
        neigbors along with their edit distances according to the given output_type
        if "triplets" returns are [(x_index, y_index, edit_distance)]
        if "coo_matrix" returns are scipy's sparse matrix where C[i,j] = distance(X_i, X_j) or 0 if not neighbor
        if "ndarray" returns numpy's 2d array representing dense matrix
    """

    # boilerplate
    check_common_input(seqs, max_edits, max_returns, n_cpu,
                       custom_distance, max_custom_distance, output_type)
    seqs = ensure_numpy(seqs)

    # algorithm
    index = build_index(seqs)
    triplets = lookup(index, seqs, max_edits,
                      max_returns, n_cpu, custom_distance, max_custom_distance)
    return make_output(triplets, output_type)


# How to run a celery task:
# 1. make sure RabbitMQ is running in the background
# 2. $ celery -A tasks worker --loglevel=info

from celery import Celery
from celery.schedules import crontab

from datetime import datetime
import pytz

app = Celery('tasks', broker='pyamqp://guest@localhost//')
app.conf.beat_schedule = {
    # Executes every 15-minute
    'execute-every-15-minute': {
        'task': 'cron_tasks.task_every_15_minutes',
        'schedule': crontab(minute='*/15'),
        'args': (16, 16),
    },
}
app.conf.timezone = 'UTC'
# app.conf.timezone = 'US/Pacific'

@app.task
def task_every_15_minutes():
    utc_now = datetime.now()


from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
    TokenVerifyView,
)

from src.profiles.views import (
    login_view,
    register_view,
    logout_view,
    profile_view,
)

urlpatterns = [
    path("login/", login_view, name="login"),
    path("register/", register_view, name="register"),
    path("logout/", logout_view, name="logout"),
    path("profile/", profile_view, name="profile"),
    path("api/token/", TokenObtainPairView.as_view(), name="token_obtain_pair"),
    path("api/token/refresh/", TokenRefreshView.as_view(), name="token_refresh"),
    path("api/token/verify/", TokenVerifyView.as_view(), name="token_verify"),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


from branca.element import Element, Figure, MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin

class Draw(JSCSSMixin, MacroElement):
    """
    Vector drawing and editing plugin for Leaflet.

    Parameters
    ----------
    export : bool, default False
        Add a small button that exports the drawn shapes as a geojson file.
    filename : string, default 'data.geojson'
        Name of geojson file
    position : {'topleft', 'toprigth', 'bottomleft', 'bottomright'}
        Position of control.
        See https://leafletjs.com/reference.html#control
    show_geometry_on_click : bool, default True
        When True, opens an alert with the geometry description on click.
    draw_options : dict, optional
        The options used to configure the draw toolbar. See
        http://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#drawoptions
    edit_options : dict, optional
        The options used to configure the edit toolbar. See
        https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#editpolyoptions

    Examples
    --------
    >>> m = folium.Map()
    >>> Draw(
    ...     export=True,
    ...     filename="my_data.geojson",
    ...     position="topleft",
    ...     draw_options={"polyline": {"allowIntersection": False}},
    ...     edit_options={"poly": {"allowIntersection": False}},
    ... ).add_to(m)

    For more info please check
    https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html

    """

    _template = Template(
        """
        {% macro script(this, kwargs) %}
            var options = {
              position: {{ this.position|tojson }},
              draw: {{ this.draw_options|tojson }},
              edit: {{ this.edit_options|tojson }},
            }
            // FeatureGroup is to store editable layers.
            var drawnItems_{{ this.get_name() }} = new L.featureGroup().addTo(
                {{ this._parent.get_name() }}
            );
            options.edit.featureGroup = drawnItems_{{ this.get_name() }};
            var {{ this.get_name() }} = new L.Control.Draw(
                options
            ).addTo( {{this._parent.get_name()}} );
            {{ this._parent.get_name() }}.on(L.Draw.Event.CREATED, function(e) {
                var layer = e.layer,
                    type = e.layerType;
                var coords = JSON.stringify(layer.toGeoJSON());
                {%- if this.show_geometry_on_click %}
                layer.on('click', function() {
                    alert(coords);
                    console.log(coords);
                });
                {%- endif %}
                drawnItems_{{ this.get_name() }}.addLayer(layer);
             });
            {{ this._parent.get_name() }}.on('draw:created', function(e) {
                drawnItems_{{ this.get_name() }}.addLayer(e.layer);
            });
            {% if this.export %}
            document.getElementById('export').onclick = function(e) {
                var data = drawnItems_{{ this.get_name() }}.toGeoJSON();
                var convertedData = 'text/json;charset=utf-8,'
                    + encodeURIComponent(JSON.stringify(data));
                document.getElementById('export').setAttribute(
                    'href', 'data:' + convertedData
                );
                document.getElementById('export').setAttribute(
                    'download', {{ this.filename|tojson }}
                );
            }
            {% endif %}
        {% endmacro %}
        """
    )

    default_js = [
        (
            "leaflet_draw_js",
            "https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.js",
        )
    ]
    default_css = [
        (
            "leaflet_draw_css",
            "https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.css",
        )
    ]

    def __init__(
        self,
        export=False,
        filename="data.geojson",
        position="topleft",
        show_geometry_on_click=True,
        draw_options=None,
        edit_options=None,
    ):
        super().__init__()
        self._name = "DrawControl"
        self.export = export
        self.filename = filename
        self.position = position
        self.show_geometry_on_click = show_geometry_on_click
        self.draw_options = draw_options or {}
        self.edit_options = edit_options or {}

    def render(self, **kwargs):
        super().render(**kwargs)

        figure = self.get_root()
        assert isinstance(
            figure, Figure
        ), "You cannot render this Element if it is not in a Figure."

        export_style = """

        """
        export_button = """Export"""
        if self.export:
            figure.header.add_child(Element(export_style), name="export")
            figure.html.add_child(Element(export_button), name="export_button")


import urllib.request
from bs4 import BeautifulSoup
import html
import json
import re
import time

### Season Info ###
seasonID = "F3C6D619-32F6-4C2A-8506-798D7493D668"
teamInfo = [[499, 23], [500, 22], [501, 44], [502, 43], [503, 42], [504, 74],
            [505, 73], [506, 89], [507, 135], [508, 153]]
teamDict = {"Hillegom BC 1": [499, 23],
            "Hillegom BC 2": [500, 22],
            "Hillegom BC 3": [501, 44],
            "Hillegom BC 4": [502, 43],
            "Hillegom BC 5": [503, 42],
            "Hillegom BC 6": [504, 74],
            "Hillegom BC 7": [505, 73],
            "Hillegom BC 8": [506, 89],
            "Hillegom BC 9": [507, 135],
            "Hillegom BC 10": [508, 153]}
#teamInfo = [[499, 23]]
### Season Info ###
Requests = 0

def get_webpage(link):
    """
    Takes as input a weblink link and returns
    a BeautifulSoup object.
    """
    req = urllib.request.urlopen(link)
    bytes = req.read()

    string = bytes.decode('utf8')
    req.close()

    global Requests
    Requests += 1
    return BeautifulSoup(string, 'html.parser')

def get_matches(soup):
    """
    Takes as input a BeautifulSoup object and Retrieves
    all matches found in the object in a dictionary.
    """

    # Get the rows containing the information
    rows = soup.table.findAll('tr')
    matches = []

    # For each row strip the important information.
    for row in rows[1:]:
        matches.append(strip_match_col(row))

    return {"matches": matches}

# Retrieves ranking from a soup.
def get_ranking(soup):
    """
    Takes as input a BeautifulSoup object and Retrieves
    the ranking found in the object in a dictionary.
    """

    # Find all rows that represent the ranking.
    rows = soup.find('table', {"class": "ruler"}).findAll('tr')
    division = " ".join(soup.findAll("div", {"class":"title"})[0].get_text().split()[:2])

    teamname = ""
    teamrank = ""
    ranking = []

    # Loop trough every row and strip the right information.
    for row in rows[1:]:
        r = strip_rank_col(row)
        ranking.append(r)

        if "hillegom" in r['team'].lower():
            teamname = prettify_teamname(r['team'])
            teamrank = r['rank']

    return {"division": division, "name": teamname, "table": ranking, 'teamrank': teamrank}

def strip_match_col(rows):
    """
    Takes as input a match row and Retrieves the information. Output
    is a dictionary which contains the information ordered.
    """

    time = rows.find('td', {"class":"plannedtime"}).get_text()
    home = lower_layout(rows.findAll('a', {"class":"teamname"})[0].get_text())
    away = lower_layout(rows.findAll('a', {"class":"teamname"})[1].get_text())

    # Retrieves location id
    location_link = rows.findAll('a')[2]['href']
    locID = 0

    # Because comments can screw up the format of the match row,
    # there are two possible situations.
    try:
        regexl = re.search("lid=[\d]+", str(location_link))
        locID = regexl.group().split("=")[-1]
    except:
        location_link = rows.findAll('a')[-2]['href']
        regexl = re.search("lid=[\d]+", str(location_link))
        locID = regexl.group().split("=")[-1]

    # Retrieves game id
    regex = re.search("match=[\d]+", str(rows))
    gameID = regex.group().split("=")[-1]

    # Tries to find a score, if there ain't any, keep it empty.
    try:
        score = rows.find('span', {"class":"score"}).get_text()
    except:
        score = ""

    return {"time": time, "home": home,"away": away,
            "score": score,"locationid": locID, "gameid":gameID}

def strip_rank_col(rows):
    """
    Takes as input a rank row and Retrieves the information. Output
    is a dictionary which contains the information ordered.
    """
    rank = rows.find('td', {"class":"standingsrank"}).get_text()
    team = lower_layout(rows.find('a').get_text())
    scores = rows.findAll('td', {"align":"center"})
    points = scores[0].get_text()
    played = scores[1].get_text()

    return {"rank": rank,"team": team,"points": points,"played": played}

def get_players(soup):
    """
    Takes soup of a player page as input and gives a list
    of surnames as output.
    (teamplayers.aspx)
    """
    players = soup.table.tr.findAll("tr")
    names = []

    for player in players:

        # Gets basic info for every person in a list
        info = player.get_text(" ").split(",")[-1].split(" ")[1:-1]

        # Discard player if not a regular player
        if info[-1] == "Ja":

            # Retrieve only the surname.
            name = info[0]
            names.append(name)

    return {"players": ", ".join(names)}

def get_games(json, team):
    """
    Takes as input json matches and teamname.
    Outputs all the home and away games from
    the given team.
    """
    result = []

    for dictionary in json['matches']:
        if team in dictionary["home"].lower() or team in dictionary["away"].lower():
            result.append(dictionary)

    return result

def get_last_game(json):
    """
    Takes as input json matches. Outputs the information
    from the last game.
    """
    result = []

    for dictionary in get_games(json, "hillegom"):
        if dictionary["score"] != "":

            # Remove teamnumber
            dictionary["home"] = teamname_only(dictionary["home"])
            dictionary["away"] = teamname_only(dictionary["away"])

            result.append(dictionary)

    return result[-1]

def next_x_games(json, x):
    result = []

    for dictionary in json['matches']:
        if dictionary["score"] == "":

            # Remove teamnumber
            dictionary["home"] = teamname_only(dictionary["home"])
            dictionary["away"] = teamname_only(dictionary["away"])

            result.append(dictionary)

    return result[:x]

def get_next_game(json):
    """
    Takes as input json matches. Outputs the information
    from the next game.
    """
    result = []

    for dictionary in get_games(json, "hillegom"):
        if dictionary["score"] == "":

            # Remove teamnumber
            dictionary["home"] = teamname_only(dictionary["home"])
            dictionary["away"] = teamname_only(dictionary["away"])

            return dictionary

def get_game_info(soup):
    s = soup.find("table", {"class":"ruler matches"}).findAll("tbody")
    games = []

    for x in s[0]:

        # Ignore whitespaces
        if len(x) > 2:

            # Make temporary list of information
            temp = []

            # Get every game played in the match
            for y in x.findAll("td"):
                temp.append(y.get_text())

            # Process the temp list. Make diffence between double and single.
            if "D" in temp[0]:
                game = [temp[0]] + temp[2:4] + temp[-3:]
            else:
                game = [temp[0], temp[2]] + temp[-2:]

            # Calculate winner per match
            try:
                game.append(who_wins(temp[-1]))
            except:
                game.append("undecided")

            games.append(game)
    return games

def who_wins(points):
    """
    Takes as input a string containing sets. Outputs home if
    the left side wins and away if the right side wins.
    """
    games = points.split(" ")

    if int(games[-1].split("-")[0]) > int(games[-1].split("-")[1]):
        return "home"
    else:
        return "away"

def lower_layout(string):
    """
    Takes as input an uppercase string and returns a string with
    only the first letter in uppercase.

    EXAMPLE -> Example.
    """
    end = string[1:].lower()
    return string[0] + end

def teamname_only(string):
    """
    Takes as input a full teamname and returns only the
    base.

    Team BC 5 -> Team.
    """
    return string.split(" ")[0]

def prettify_teamname(string):
    """
    Team bc 7 -> Team BC 7.
    """
    spl = string.split(" ")
    return spl[0] + " " + spl[1].upper() + " " + spl[2]

def match(team):
    """
    Helper returns soup from given matchlink, calling
    the correct function.
    """
    link = craftLink("drawmatches", seasonID, "&draw=", team[1])
    soup = get_webpage(link)
    return get_matches(soup)

def rank(team):
    """
    Helper returns soup from given ranklink, calling
    the correct function.
    """
    link = craftLink("draw", seasonID, "&draw=", team[1])
    soup = get_webpage(link)
    return get_ranking(soup)

def players(team):
    """
    Helper returns soup from given playerlink, calling
    the correct function.
    """
    link = craftLink("teamplayers", seasonID, "&tid=", team[0])
    soup = get_webpage(link)
    return get_players(soup)

def game(gameid):
    """
    Takes as input a gameid, crafts a link and Retrieves
    information about the game in a list:
    [["MD", "player 1", "player 2", "enemy 1", "enemey 2", sets, winner]]
    """
    link = craftLink("teammatch", seasonID, "&match=", gameid)
    soup = get_webpage(link)
    return get_game_info(soup)

def craftLink(info, seasonID, compare, cid):
    """
    Takes as input the information, seasonid, compare text
    and id. Outputs a correct structured link.

    """
    baseLink = "https://badmintonnederland.toernooi.nl/sport/"
    link = baseLink + info + ".aspx?id=" + seasonID + compare + str(cid)
    return link

def createFile(name, string):
    """
    Takes as input filename and a jsonstring,
    outputs the json into the filename.
    """
    with open(name, 'w', encoding='utf8') as jsonfile:
        json.dump(string, jsonfile, ensure_ascii=False, indent=4)

# Todo
# Make the functions modulair.

def init_data(team):
    """
    Crafts dictionary with all information for one specific team.
    A team has the form of [teamID, drawID].
    """
    # Online requests
    ranking = rank(team)
    matches = match(team)
    members = players(team)

    # Other calculations
    next_match = {"next":get_next_game(matches)}
    upcoming = {"upcoming":next_x_games(matches, 4)}
    previous_match = get_last_game(matches)
    previous_match["games"] = game(previous_match["gameid"])
    last_match = {"last": previous_match}

    combined_dict = {**ranking, **next_match, **last_match, **upcoming, **members}

    return combined_dict

# Todo:
#  Lookup every team
#  Automically move json file to github
#  Daily checks.
#  What if no internet connection?
#  Next match based on time, not on where score = ''
#  Scrape ID's and draw automatically.

def start(teamInfo):
    """
    Launches the program, and scrapes all information for the
    given team in list teamInfo. It dumps the result into data.json.
    """
    start_time = time.clock()
    total = []

    for team in teamInfo:
        print("Busy with team: %s --" % team, round(time.clock() - start_time, 2), "seconds passed so far.")
        teamdata = init_data(team)
        total.append(teamdata)

    print("Dumping into json file..")
    data = ({"Teams": total})
    jstr = json.dumps(data, indent=4)
    createFile("data.json", data)

def information(query):
    result = []
    global teamDict

    for teamname, values in query.items():
        init = {}
        if 'match' in values:
            ranking = rank(teamDict[teamname])
            matches = match(teamDict[teamname])

            # Calculations based on matches
            next_match = {"next":get_next_game(matches)}
            upcoming = {"upcoming":next_x_games(matches, 4)}

            # Only new information is possible if there has been played
            # a new game.
            if 'game' in values:
                previous_match = get_last_game(matches)

                previous_match["games"] = game(previous_match["gameid"])
                last_match = {"last": previous_match}

                init = {**init, **ranking, **next_match, **last_match, ** upcoming}
            else:
                init = {**init, **ranking, **next_match, **upcoming}

        if 'name' in values:
            members = players(teamDict[teamname])

            init = {**init, **members}

        result.append(init)
    return result

# Timing information
# start_time = time.clock()
# start(teamInfo)
# print("Finished in", round(time.clock() - start_time, 2), "seconds")
# print("In", Requests, "Requests")


import numpy as np
import matplotlib.pyplot as plt
from scipy.special import comb, factorial

# geometric series: is the sum of a finite or an infinite sequence of numbers with a constant ratio between sucessive terms. is useful when handling situations such as N-1 failures followed by a success.
p = 1/2
n = np.arange(0,10)
X = np.power(p, n)

plt.bar(n, X)
# plt.show()

# binomial series: is instrumental in algebra when handling polynomials such as (a+b)² or (1+n)³. It provides a valuable formula when computing these powers.
n = 10
k = 2
comb(n, k)
factorial(k)

# Taylor approximation os a geometric-based approximation. It approximates the function according to the offset slope, curvature, and so on. The Taylor series has an infinite number of terms.
# An immediate application of the Taylor approximation s to derive the exponential series.

# data analysis problems:
    # the observable vector Y
    # the variable vector X
    # the coefficient B
# y = Bn Xn

# inner product of a vectors
x = np.array([[1], [0], [-1]])
y = np.array([[3], [2], [0]])
z= np.dot(np.transpose(x), y)
print(z)

# norm: the norm give us essentially the length of the vector
x = np.array([[1], [0], [-1]])
n_norm = np.linalg.norm(x) # using the norm, one can define an angle called the cosine angle between two vectors.

# the difference between the cosine angle and the basic inner product is the normalization in the denominator.

# W: the geometric of the weighted l2-norm is determined by the matrix W.
W = np.array([[1,2,3], [4,5,6], [7,8,9]])
z = np.dot(x.T, np.dot(W, x))
print(z)

# matrix calculus: differentiation of matrices and vectors. To find the min or max of a scalar function with a vector input.
# matrix inversion
X = np.array([[1, 3], [-2,7], [0,1]])
XtX = np.dot(X.T, X)
XtXinv = np.linalg.inv(XtX)
print(XtXinv)

# when we are more interested om solving a linear equation
X = np.array([[1, 3], [-2,7], [0,1]])
y = np.array([[2], [1], [0]])
beta = np.linalg.lstsq(X, y, rcond=None)[0]
print(beta)

# permutation: consider a set of n distinct balls. Suppose we want to pick k balls from the set without replacement. How many ordered configurations can we obtains?
# permutation: the number of choices is reduced in every state.


import os, sys
sys.path.append(os.path.abspath(os.path.join(__file__, '..')))

import logging
import signal
import time

from etb import ETB, debug_level_value
from etbconfig import ETBConfig

def setup_logger(debugLevel, logFile):
    """
    Sets up the logger for the ETB node.

    :parameters:
        - `debuglevel`: the debug level for the ETB; one of `debug`,
          `info`, `warning`, `error`, or `critical`
        - `logFile`: the path to the log file

    :returntype:
        :class:`logging.Logger`

    .. note::
        Many modules set up their own logger that inherit from this one; we
        should provide a way to indicate different levels for different modules;
        e.g., debug level for etb.wrappers, but info for all others.
    """
    logger = logging.getLogger('etb')
    logger.setLevel(debug_level_value(debugLevel))

    #logging.basicConfig(format="%(asctime)s %(levelname)s %(name)s %(process)d/%(threadName)s: %(message)s")

    # formatter = logging.Formatter("%(asctime)s:%(levelname)s: %(message)s",
    #                               "%Y-%m-%d %H:%M:%S")
    formatter = logging.Formatter("%(levelname)s: %(message)s")

    if logFile is None:
        h = logging.StreamHandler()
    else:
        h = logging.FileHandler(logFile, mode='w')

    h.setLevel(debug_level_value(debugLevel))
    h.setFormatter(formatter)
    logger.addHandler(h)

    return logger

class FlushFile(object):
    """Write-only flushing wrapper for file-type objects."""
    def __init__(self, f):
        self.f = f
    def write(self, x):
        self.f.write(x)
        self.f.flush()

def main():
    '''
    The main function. It is called when the program is
    executed. It interprets the command line, starts logging,
    and creates the toolbus.
    '''
    # Add current directory to sys.path
    sys.path.insert(0,"")

    # Replace stdout with an automatically flushing version
    sys.stdout = FlushFile(sys.__stdout__)

    # Get the command line and config file arguments
    etbconf = ETBConfig()

    logger = setup_logger(etbconf.debuglevel, etbconf.logfile)

    ETB(etbconf)
    # The main thread continues here - is this the best way to do it?
    try:
        while True:
            time.sleep(5)
    except Exception as e:
        print('Caught exception {0}'.format(e))

if __name__ == '__main__':
    main()


from rest_framework import generics, viewsets, status
from rest_framework.decorators import action
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import AllowAny, IsAdminUser, IsAuthenticated
from rest_framework.response import Response

from users.model import User
from users.serializers import RegisterSerializer, UserSerializer

class RegisterView(generics.CreateAPIView):

    queryset = User.objects.all()
    authentication_classes = ()
    permission_classes = (AllowAny,)
    serializer_class = RegisterSerializer

class UserViewSet(viewsets.ModelViewSet):

    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = (IsAdminUser,)
    lookup_field = 'id'

    @action(
        methods=('get', 'patch'),
        detail=False,
        permission_classes=(IsAuthenticated,)
    )
    def me(self, request):
        user_profile = get_object_or_404(
            User,
            id=request.user.id
        )
        if request.method == 'GET':
            serializer = UserSerializer(user_profile)
            return Response(
                serializer.data,
                status=status.HTTP_200_OK
            )

        serializer = UserSerializer(
            user_profile,
            data=request.data,
            partial=True
        )
        serializer.is_valid(raise_exception=True)
        serializer.save(
            role=user_profile.role,
            avatar=user_profile.avatar,
            bio=user_profile.bio,
            program_language=user_profile.program_language)
        return Response(
            serializer.data,
            status=status.HTTP_200_OK
        )


from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse

from .models import Course, LectureSession

# Create your views here.
def index(request):
    template = loader.get_template('courses/index.html')
    courses = [c.get_template_representation() for c in Course.objects.all()]

    context = {
        'subscribed_course_list': courses
    }
    return HttpResponse(template.render(context, request))

def course(request, url_name):
    template = loader.get_template('courses/course.html')
    course = Course.objects.get(url_name__exact=url_name)
    sessions = course.get_sessions()
    context = {
        'course': course,
        'sessions': sessions,
    }
    return HttpResponse(template.render(context, request))

def lecture(request, course_url_name, lecture_url_name):
    template = loader.get_template('courses/lecture.html')
    course = Course.objects.get(url_name__exact=course_url_name)
    lecture = LectureSession.objects.get(url_name__exact=lecture_url_name)
    previous_lecture, next_lecture = course.get_prv_and_next_session(lecture_url_name)
    context = {
        'course': course,
        'lecture': lecture,
        'previous_lecture': previous_lecture,
        'next_lecture': next_lecture,
    }
    return HttpResponse(template.render(context, request))