import datetime
import hashlib
import logging
import uuid
from decimal import Decimal
from io import BytesIO

import dateutil.parser
import requests
from lxml import etree
from lxml.etree import QName, XMLSyntaxError

from ideal.conf import settings
from ideal.exceptions import IdealResponseException, IdealSecurityException, IdealServerException
from ideal.security import Security
from ideal.utils import IDEAL_NAMESPACES, convert_camelcase, render_to_string

logger = logging.getLogger(__name__)

class HttpRequest(dict):
    """
    Lightweight container for an HTTP request, containing all relevent properties.
    """
    def __init__(self, uri, method, body=None, headers=None):
        """
        :param uri: Absolute URI of the request.
        :param method: The HTTP method.
        :param body: Data passed in the body of the request.
        :param headers: Dictionary of request headers.
        """
        super(HttpRequest, self).__init__()

        self._uri = uri
        self._method = method
        self._body = body

        if headers is None:
            headers = {}

        self._headers = headers

        self.update(dict([(k.title().replace('_', '-'), v) for k, v in headers.items()]))

    @property
    def headers(self):
        """
        Return the actual headers.
        """
        return self._headers

    @property
    def uri(self):
        return self._uri

    @property
    def method(self):
        return self._method

    @property
    def body(self):
        return self._body

class HttpResponse(dict):
    """
    Lightweight container for an HTTP response.

    Contains a convenience property ``xml`` that may contain an :class:`lxml.etree.ElementTree` object, that was
    parsed from the response content.
    """
    xml = None

    def __init__(self, client, response_headers, response_content, status_code, request):
        """
        Create a new :class:HttpResponse` object. All headers can be accessed as if this object were a dictionary.

        :param client: The client object that generated this response.
        :param response_headers: Dictionary containing the response headers.
        :param response_content: All data passed as response.
        :param status_code: The HTTP status code.
        :param request: The original :class:`HttpRequest` object that was used to trigger this response.
        """
        super(HttpResponse, self).__init__()

        self.client = client
        self.headers = response_headers
        self.content = response_content
        self.status_code = status_code
        self.request = request

        # Make headers consistently accessible.
        self.update(dict([(k.title().replace('_', '-'), v) for k, v in response_headers.items()]))

class IdealResponse(object):
    """
    Container for interesting XML values parsed from the original HTTP response.
    """
    def __init__(self, response):
        """
        Create an iDEAL response object from the original HTTP response (:class:`HttpResponse` object). It is assumed
        the response already contains a property ``xml`` with a parsed :class:`lxml.etree.ElementTree`.

        :param response: The original :class:`HttpResponse` object
        """
        self._response = response

        self._parse(response.xml)

    def _parse(self, xml):
        """
        This function should be overridden in subclasses to parse the XML.
        """
        pass

class DirectoryResponse(IdealResponse):
    acquirer_id = None

    _issuers = None

    def _parse(self, xml):
        self.acquirer_id = xml.xpath(
            'ideal:Acquirer/ideal:acquirerID', namespaces=IDEAL_NAMESPACES)[0].text

        result = {}

        for issuer_node in xml.xpath('//ideal:Issuer', namespaces=IDEAL_NAMESPACES):

            issuer_id = issuer_node.xpath('ideal:issuerID', namespaces=IDEAL_NAMESPACES)[0].text
            issuer_name = issuer_node.xpath('ideal:issuerName', namespaces=IDEAL_NAMESPACES)[0].text

            country_name = issuer_node.getparent().xpath('ideal:countryNames', namespaces=IDEAL_NAMESPACES)[0].text
            if country_name not in result:
                result[country_name] = {}
            result[country_name][issuer_id] = issuer_name

        self._issuers = result

    @property
    def issuers(self):
        return self._issuers.copy()

    def get_issuer_list(self):
        """
        Return a flat list of all issuers.

        :return: A flat list of all issuers.
        """
        result = {}
        for issuer in self._issuers.values():
            result.update(issuer)

        return result

class TransactionStatus(object):
    SUCCESS = 'Success'  # Positive result; the payment is guaranteed.
    CANCELLED = 'Cancelled'  # Negative result due to cancellation by Consumer; no payment has been made.
    EXPIRED = 'Expired'  # Negative result due to expiration of the transaction; no payment has been made.
    FAILURE = 'Failure'  # Negative result due to other reasons; no payment has been made.
    OPEN = 'Open'  # Final result not yet known). A new status request is necessary to obtain the status.

class TransactionResponse(IdealResponse):
    acquirer_id = None

    issuer_authentication_url = None
    transaction_id = None

    # Not an actual part of the transaction response.
    entrance_code = None

    def _parse(self, xml):
        self.acquirer_id = xml.xpath(
            'ideal:Acquirer/ideal:acquirerID', namespaces=IDEAL_NAMESPACES)[0].text

        self.issuer_authentication_url = xml.xpath(
            'ideal:Issuer/ideal:issuerAuthenticationURL', namespaces=IDEAL_NAMESPACES)[0].text

        self.transaction_id = xml.xpath(
            'ideal:Transaction/ideal:transactionID', namespaces=IDEAL_NAMESPACES)[0].text

class StatusResponse(IdealResponse):
    acquirer_id = None

    transaction_id = None
    status = None  # Any of the constants in :class:`TransactionStatus`.
    status_date_timestamp = None
    consumer_name = None
    consumer_iban = None
    consumer_bic = None
    amount = None
    currency = None

    def _parse(self, xml):

        self.acquirer_id = xml.xpath(
            'ideal:Acquirer/ideal:acquirerID', namespaces=IDEAL_NAMESPACES)[0].text

        for transaction_node in xml.xpath('ideal:Transaction/*', namespaces=IDEAL_NAMESPACES):
            attr = convert_camelcase(QName(transaction_node).localname)
            val = transaction_node.text

            if attr == 'amount':
                val = Decimal(val)
            elif attr == 'status_date_timestamp':
                val = dateutil.parser.parse(val)

            setattr(self, attr, val)

class IdealClient(object):
    """
    The iDEAL client to communicate with iDEAL.

    All messages are signed before they are sent to the bank's endpoint. All responses are verified against the iDEAL
    certificate(s).
    """
    def __init__(self):
        self.security = Security()

        # All settings should be correct before instantiating a client.
        settings.validate()

    def _get_context(self, **kwargs):
        """
        Return the default context used in every request.

        :param \*\*kwargs: Additional context, or override the defaults (optional).

        :return: Dictionary with the default context, updated with ``kwargs``.
        """
        now = datetime.datetime.now()

        context = {
            'merchant_id': settings.MERCHANT_ID,
            'sub_id': settings.SUB_ID,
            'timestamp': now.strftime('%Y-%m-%dT%H:%M:%S.000Z')  # TODO: now.isoformat() ?
        }
        context.update(kwargs)

        return context

    def create_request(self, body=None):
        """
        Create a request suited for communicating with iDEAL.
        NOTE: All requests are signed.

        :param body: The unsigned data to send.

        :return: A :class:`HttpRequest` object.
        """
        body = self.security.sign_message(
            body, settings.PRIVATE_CERTIFICATE, settings.PRIVATE_KEY_FILE, settings.PRIVATE_KEY_PASSWORD)

        if body and not body.startswith('<?'):
            body = '<?xml version="1.0" encoding="utf-8"?>' + body

        headers = {
            'content-type': 'text/xml; charset="utf-8"'
        }

        uri = settings.get_acquirer_url()

        return HttpRequest(uri, 'POST', body, headers)

    def create_response(self, response_headers, response_content, status_code, request):
        """
        Create a response from all arguments.
        NOTE: All responses are verified.

        :param response_headers: Dictionary of headers in the response.
        :param response_content: All data from the response.
        :param status_code: The HTTP status code.
        :param request: The original :class:`HttpRequest` object.

        :return: A :class:`HttpResponse` object.
        """
        response = HttpResponse(self, response_headers, response_content, status_code, request)

        if response.status_code != 200:
            raise IdealServerException('iDEAL server returned HTTP {status_code}: {message}'.format(
                status_code=response.status_code,
                message=response.content,
            ))

        try:
            xml_document = etree.parse(BytesIO(response.content))
        except XMLSyntaxError as e:
            raise IdealServerException('iDEAL response could not be parsed: {error}'.format(error=e))

        response.xml = xml_document

        if not self.security.verify(response.content, settings.CERTIFICATES):
            raise IdealSecurityException('iDEAL response could not be verified.')

        if xml_document.xpath('count(//ideal:Error)', namespaces=IDEAL_NAMESPACES) > 0:
            raise IdealResponseException(xml_document)

        return response

    def _request(self, data):
        """
        Constructs a :class:`HttpRequest` object, performs the actual request using the ``requests`` library, and
        return a :class:`HttpResponse` object. This function can be easily overridden to mock requests or to replace
        the ``requests`` library with any other library.

        :param data: The stringified payload to send to iDEAL.

        :return: A :class:`HttpResponse` object.
        """
        logger.debug('Creating request with data: %(data)s', {'data': data})

        request = self.create_request(data)

        logger.debug('Performing request: %(request_method)s %(url)s\n%(request_headers)s\n\n%(body)s', {
            'request_method': request.method,
            'url': request.uri,
            'request_headers': '\n'.join(['%s: %s' % (k, v) for k, v in request.headers.items()]),
            'body': request.body,
        })

        raw_response = requests.request(request.method, request.uri, data=request.body, headers=request.headers)

        logger.debug('Recieved response: HTTP %(response_status)s\n%(response_headers)s\n\n%(data)s', {
            'response_status': raw_response.status_code,
            'response_headers': '\n'.join(['%s: %s' % (k, v) for k, v in raw_response.headers.items()]),
            'data': raw_response.content,
        })

        response = self.create_response(raw_response.headers, raw_response.content, raw_response.status_code, request)

        # If logging is set to DEBUG, don't log this. All details are logged in DEBUG level above.
        if logger.level >= logging.INFO:
            logger.info('%(request_method)s %(url)s (HTTP %(response_status)s)', {
                'request_method': request.method,
                'url': request.uri,
                'response_status': response.status_code
            })

        return response

    def get_issuers(self):
        """
        Sends a "DirectoryReq" to iDEAL to retrieve a list of issuers (banks).

        NOTE: The iDEAL documentation indicates you should get a list of issuers (ie. call this function) every time
        you want to show a list of issuers. Cache these issuers locally; they update rarily.

        :return: A :class: `DirectoryResponse` object.
        """
        context = self._get_context()
        data = render_to_string('templates/directory_request.xml', context)

        r = self._request(data)

        return DirectoryResponse(r)

    def start_transaction(self, issuer_id, purchase_id, amount, description, entrance_code=None,
                          merchant_return_url=None, expiration_period=None, language=None):
        """
        Send an "AcquirerTrxReq" to iDEAL, starting the payment process.

        The returned named tuple can be used to store the transaction details but should always be used to redirect the
        customer to their bank's website by using the ``issuer_authentication_url``. The generated ``entrance_code``
        can be used to resume an incomplete transaction process.

        :param issuer_id: The BIC code of the customer's bank. Usually retrieved with ``IdealClient.get_issuers``.
        :param purchase_id: Any string with a maximum of 16 characters to identify the purchase.
        :param amount: Decimal number for the amount of the transaction.
        :param description: Any string with a maximum of 32 characters describing the purchase.
        :param entrance_code: A unique string of 40 characters to continue the payment process (optional).
        :param merchant_return_url: Override the callback URL (optional). Default\: ``settings.MERCHANT_RETURN_URL``.
        :param expiration_period: Override the expiration period (optional). Default: ``settings.EXPIRATION_PERIOD``.
        :param language: Override the language (optional). Default\: ``settings.LANGUAGE``.

        :return: A :class:`TransactionResponse` object.
        """
        if merchant_return_url is None:
            merchant_return_url = settings.MERCHANT_RETURN_URL
        if language is None:
            language = settings.LANGUAGE
        if entrance_code is None:
            entrance_code = hashlib.sha1(
                uuid.uuid4().hex.encode('utf-8')).hexdigest()
        if expiration_period is None:
            expiration_period = settings.EXPIRATION_PERIOD

        try:
            if str(int(expiration_period)) == str(expiration_period):
                expiration_period = 'PT{n}M'.format(expiration_period)
        except ValueError:
            pass

        # Get all required variables for the template.
        context = self._get_context()
        context.update({
            'issuer_id': issuer_id,
            'merchant_return_url': merchant_return_url,
            'purchase_id': purchase_id[0:16],
            'amount': '{amount:.2f}'.format(amount=amount),  # Use period as decimal separator.
            'currency': 'EUR',  # ISO 4217, Only 'EUR' supported.
            'expiration_period': expiration_period,  # ISO 8601, min. is 1 minute, max. is 1 hour (optional).
            'language': language,  # ISO 639-1, only Dutch (nl) and English (en) are supported.
            'description': description[0:32],
            'entrance_code': entrance_code,
        })

        data = render_to_string('templates/transaction_request.xml', context)

        r = self._request(data)

        response = TransactionResponse(r)

        # Not an actual part of the response, but can be generated in this function and made conveniently accessible.
        response.entrance_code = entrance_code

        return response

    def get_transaction_status(self, transaction_id):
        """
        Sends an "AcquirerStatus" request to iDEAL to retrieve the status of given transaction.

        Eventually, the bank should redirect to your ``settings.MERCHANT_RETURN_URL`` (or the overriden value in
        ``IdealClient.start_transaction``). There are at least 2 query string parameters present in this URL: ``trxid``
        and ``ec``.

        :param transaction_id: The value of ``trxid`` query string parameter.

        :return: A :class:`TransactionResponse` object.
        """

        # Get all required variables for the template.
        context = self._get_context()
        context.update({
            'transaction_id': transaction_id,
        })

        data = render_to_string('templates/transaction_status_request.xml', context)

        r = self._request(data)

        return StatusResponse(r)


__author__ = 'guru'
import serial
import binascii

serialport = serial.Serial("/dev/ttyAMA0", timeout=10)
serialport.baudrate = 9600
serialport.flush()

autoid = 0

def fpsTransmitter(data):
    question =  binascii.unhexlify(data)
    print("string trans >",data)
    serialport.flush()
    serialport.write(question)

def fpsReceiver():
    str = binascii.hexlify(serialport.read(12)).decode("utf-8")
    print("string res   >",str)
    return str

def fpsReceiverWithExtraData():
    str = binascii.hexlify(serialport.read(12)).decode("utf-8")
    if str[12:16] == "0500":
        str = str + binascii.hexlify(serialport.read(5)).decode("utf-8")
    print("string res   >",str)
    return str

def fpsTemplateReceiver():
    str = binascii.hexlify(serialport.read(12)).decode("utf-8")
    if str[12:16] == "5006":
        str = str + binascii.hexlify(serialport.read(1616)).decode("utf-8")
    print("string res   >",str)
    return str
#checksum calculator
def checkSum(dataString,write):
    totalSum = 0
    for i in range(0,22):
        if( i%2 == 0):
            totalSum += int(dataString[i:i+2],16)
    checkSum = dataString[22:24]
    tempString = hex(totalSum)[::-1]     #[::-1] reverses the string
    if((tempString[0:2][::-1] == checkSum) or (write == 1)):
        chkString = tempString[0:2][::-1]
        return (1,chkString)
    else:
        chkString = '0'
        return (0,chkString)

#adds all the chunks
def dataCompiler(channel,command,param11,param12,param21,param22,dataSize11,dataSize12,dataSize21,dataSize22,errCode):
    All = (channel+command+param11+param12+param21+param22+dataSize11+dataSize12+dataSize21+dataSize22+errCode+'00')
    temp = checkSum(All,1)
    if( temp[0] == 1):
        All = All[0:22]
        All+=temp[1]
        return All
    else:
        return 0

#0x50 command starts the registration by finger info
def initiateRegistration(mobileNumber):
    data = dataCompiler('00','50','ff','ff','00','00','05','00','00','00','00')
    fpsTransmitter(data+mobileNumber)
    str=fpsReceiver()
    chk=checkSum(str,0)
    if (chk[0] == 1):
        if(str[2:4] == '50' and str[4:6] == 'ff' and str[6:8] == 'ff' and str[20:22] == '00'):
            return (1,'00')
        else:
            return (0,'00')
    else:
        return (0,str[20:22])

#0x51 command ends registration
def terminateRegistration():
    data = dataCompiler('00','51','00','00','00','00','00','00','00','00','00')
    fpsTransmitter(data)
    str=fpsReceiver()
    chk=checkSum(str,0)
    if (chk[0] == 1):
        if(str[2:4] == '51' and str[20:22] == '00'):
            return (1,'00')
        else:
            return (0,'00')
    else:
        return (0,str[20:22])

#0x19 command for including 2nd finger --- One time allow two different fingerprints during registration
def doubleRegistration():
    data = dataCompiler('00','19','00','00','00','00','00','00','00','00','00')
    fpsTransmitter(data)
    str = fpsReceiver()
    chk = checkSum(str,0)
    if (chk[0] == 1):
        if(str[2:4] == '19' and str[4:6] == '00' and str[6:8] == '00' and str[20:22] == '00'):
            return (1,'00')
        else:
            return (0,'00')
    else:
        return (0,str[20:22])

#0xA3 command for getting input from finger print
def identify():
    str=fpsReceiverWithExtraData()
    if len(str) == 0:
        return 2,2
    chk = checkSum(str,0)
    if (chk[0] == 1):
        if(str[2:4] == 'a3' and str[4:6] == 'ff' and str[6:8] == 'ff' and str[12:14] == '05' and str[20:22] =='00'):
            return (1,str[24:34])
        else:
            return (0,'00')
    else:
        return (0,str[20:22])

#get's template of respective mobileNumber
def getTemplateGenerator(mobileNumber):
    data = dataCompiler('00','73','ff','ff','00','00','05','00','00','00','00')
    fpsTransmitter(data+mobileNumber)
    str = fpsTemplateReceiver()
    chk=checkSum(str[0:24],0)
    if (chk[0] == 1):
        if(str[2:4] == '73' and str[4:6] == 'ff' and str[6:8] == 'ff' and str[3232:3242] == mobileNumber):
            #template1 = str[32:3232]
            #template2 = str[1632:3232]

            return (1,str[32:1632],str[1632:3232])
        else:
            return (0,'00')
    else:
        return (0,str[20:22])

#def putTemplateGenerator():

#    print("function under construction")

def autoIdentifyStart():
    global autoid
    autoid = 1
    fpsTransmitter('00a1010000000000000000a2')
    str = fpsTemplateReceiver()
    chk = checkSum(str,0)
    if (chk[0] == 1):
        if(str[2:4] == 'a1' and str[20:22] =='00'):
            return (1,'00')
        else:
            return (0,'00')
    else:
        return (0,str[20:22])

def autoIdentifyStop():
    global autoid
    autoid = 0
    fpsTransmitter('00a2000000000000000000a2')
    str = fpsTemplateReceiver()
    chk = checkSum(str,0)
    if (chk[0] == 1):
        if(str[2:4] == 'a2' and str[20:22] =='00'):
            return (1,'00')
        else:
            return (0,'00')
    else:
        return (0,str[20:22])

def identifySingle():
    data = dataCompiler('00','56','00','00','00','00','00','00','00','00','00')
    fpsTransmitter(data)
    str=fpsReceiverWithExtraData()
    chk=checkSum(str,0)
    if (chk[0] == 1):
        if(str[2:4] == '56' and str[4:6] == 'ff' and str[6:8] == 'ff' and str[12:14] == '05' and str[20:22] =='00'):
            return (1,str[24:34])
        else:
            return (0,'00')
    else:
        return (0,str[20:22])

def makeTemplateStart():
    data = dataCompiler('00', '35', '00', '00', '00', '00', '00', '00', '00', '00', '00')
    fpsTransmitter(data)
    str = fpsReceiver()
    if checkSum(str, 0)[0]:
        if str[2:4] == '35' and str[20:22] == '00':
            return 1
        else:
            return 0
    else:
        return 0

def makeTemplateContinue():
        data = dataCompiler('00', '36', '00', '00', '00', '00', '00', '00', '00', '00', '00')
        fpsTransmitter(data)
        str = fpsReceiver()
        if checkSum(str, 0)[0]:
            if str[2:4] == '36' and str[20:22] == '00':
                return 1
            else:
                return 0
        else:
            return 0
#delete single user
def SingleUserDelete(mobileNumber):
    data = dataCompiler('00', '72', 'ff', 'ff', '00', '00', '05', '00', '00', '00', '00')
    fpsTransmitter(data + mobileNumber)
    str = fpsReceiver()
    chk = checkSum(str,0)
    if (chk[0] == 1):
        if(str[2:4] == '72' and str[20:22] =='00'):
            return (1,'00')
        else:
            return (0,'00')
    else:
        return (0,str[20:22])

#clears the database
def clearFullDatabase():
    data = dataCompiler('00', '76', '00', '00', '00', '00', '00', '00', '00', '00', '00')
    fpsTransmitter(data)
    str = fpsReceiver()
    chk = checkSum(str,0)
    if (chk[0] == 1):
        if(str[2:4] == '76' and str[20:22] =='00'):
            return (1,'00')
        else:
            return (0,'00')
    else:
        return (0,str[20:22])

if __name__ == "__main__":
    print("1. for normal method reg\n2. for checking template presence")
    op = input()
    if op == '1':
        print("Enter Mobile Number:")
        phn = input()
        if identifySingle()[0] == 0:
            doubleRegistration()
            if initiateRegistration(str(phn))[0]:
                print ("input any char to continue")
                input()
                if terminateRegistration()[0]:
                    print ("input any char to for 2nd finger")
                    input()
                    print("done")
                    print(getTemplateGenerator(phn))
        else:
            print("user already exist")
    elif op == '2':
        print("place your finger")


from django.core.management.base import BaseCommand
from orcamentos.crm.models import Employee

class Command(BaseCommand):
    help = ''' Cria um usuário admin. '''

    def handle(self, *args, **kwargs):
        '''
        Cria um Employee.
        Precisamos de Employee para fazer todas as transações no sistema.
        '''
        username = 'admin'
        first_name = 'Admin'
        last_name = 'Admin'
        email = 'admin@email.com'
        user = Employee.objects.create(
            username=username,
            first_name=first_name,
            last_name=last_name,
            email=email,
            gender='I'
        )
        user.set_password('admin')
        user.is_staff = True
        user.is_superuser = True
        user.is_active = True
        user.save()
        print('Usuário criado com sucesso.')


import sys
from collections import deque
def DFS(v):
    if visited[v] == False:
        print(v,end=' ')
    visited[v] = True
    for i in range(1,n+1):
        if graph[v][i] == 1 and visited[i]==False:
            graph[v][i] = -1
            DFS(i)

def BFS(v):
    que = deque()
    que.append(v)
    while que:
        nowv = que.popleft()
        if Bvisited[nowv] == False:
            print(nowv,end=' ')
        Bvisited[nowv] = True
        for i in range(1,n+1):
            if Bgraph[nowv][i] == 1 and Bvisited[i] == False:
                Bgraph[nowv][i] = -1
                que.append(i)

n,m,v = map(int,sys.stdin.readline().split())
graph = [[0 for i in range(n+1)] for i in range(n+1)]
Bgraph = [[0 for i in range(n+1)] for i in range(n+1)]

visited = [False] * (n+1)
Bvisited = [False] * (n+1)
for i in range(m):
    x, y = map(int, sys.stdin.readline().split())
    graph[x][y] = 1
    graph[y][x] = 1
    Bgraph[x][y] = 1
    Bgraph[y][x] = 1

DFS(v)
print()
BFS(v)


# Question

# Given two binary strings a and b, return their sum as a
# binary string.

#_________________________________________________________________________________________________________________________________

# Question #1

# Input: a = "11", b = "1"
# Output: "100"

#_________________________________________________________________________________________________________________________________

# Question #2

# Input: a = "1010", b = "1011"
# Output: "10101"

#_________________________________________________________________________________________________________________________________

def addBinary(a, b):
    # Returning result in string form
    res = ""
    carry = 0

    # Since starting at the right most end when adding, reverse input strings.
    a, b = a[::-1], b[::-1]

    # Since not the same size reiterate in the string to see the max value
    for i in range(max(len(a), len(b))):
        # Add digit by digit
        # Can reach the end of one string than the other.
        # If not in bounds give default value of 0
        # ord() is converting character to integer
        digitA = ord(a[i]) if i < len(a) else 0
        digitB = ord(b[i]) if i < len(b) else 0

        # Sum into total
        total = digitA + digitB + carry
        char = str(total % 2)
        # Take result character and add to result string.
        res = char + res
        # Update carry int
        carry = total // 2
    # Carry can be eqaul to 0 but can be equal to 1
    if carry:
        res = "1" + res
    return res

print(addBinary("11", "1"))
print(addBinary("1010", "1011"))


import folium

import pandas

map = folium.Map(location = [39, -105], zoom_start = 4, tiles = "StamenTerrain")

# feature group - building object pass one time as opposed to above
fg = folium.FeatureGroup(name = "Map")

# import volcano file above
data = pandas.read_csv("volcano.txt") #for importing volcano file
print(data)

latitude = list(data["LAT"]) #90 -90 // 100 -100
longitude = list(data["LON"])
name = list(data["NAME"])
elevation = list(data["ELEV"])

def find_color(elevation):
    if elevation < 1000:
        return "pink"
    elif 1000 <= elevation < 3000:
        return "orange"
    else:
        return "purple"

fgvolcano = folium.FeatureGroup( name = "Volcanos" )
for lt, ln, el in zip(latitude, longitude, elevation):
    fgvolcano.add_child(folium.CircleMarker(location = [lt, ln], radius = 8, popup = str(el)+ "mt.",
                                        fill_color= find_color(el),
                                        color = "black",
                                        fill_opacity = 0.9))

for lt, ln, nm, el in zip(latitude, longitude, name, elevation):
    fg.add_child(folium.Marker(location = [lt, ln],
    popup = nm + " located at " + str(el) + " elevation level ",
    #fill_color = find_color(el), #pass in CircleMarker after folium. to be able to use this
    icon = folium.Icon(color = find_color(el))))

fgpopulation =folium.FeatureGroup (name ="Population")
fgpopulation.add_child( folium.GeoJson ( data=open("world.json", "r", encoding="utf-8-sig" ).read(),
                               style_function= lambda x:
                               {"fillColor":"yellow"
                               if x["properties"]["POP2005"] < 10000000
                               else
                                    ("purple"
                                    if  10000000 <= x["properties"]["POP2005"] < 90000000
                                    else "red")
                                }
                                ))

map.add_child(fgvolcano)
map.add_child(fgpopulation)
map.add_child(folium.LayerControl())

map.add_child(fg)

map.save("Map1.html")


import time

class Insect:
    comparison_counter = 0
    swap_counter = 0

    def __init__(self, name, speed, weight):
        self.name = name
        self.speed = speed
        self.weight = weight

    def __del__(self):
        pass

    def __str__(self):
        return ('Name of insect ' + self.name + ', speed = {0} metres per second, weight = {1} grams.').format(
            self.speed, self.weight)

def insertion_sort_decrease_speed(insects):
    comparison_counter = 0
    swap_counter = 0
    for number in range(1, len(insects)):
        current_insect = insects[number]
        previous_number = number - 1
        comparison_counter += 1
        while previous_number >= 0 and current_insect > insects[previous_number]:
            comparison_counter += 1
            insects[previous_number + 1] = insects[previous_number]
            previous_number -= 1
        insects[previous_number + 1] = current_insect
        swap_counter += 1
    print('Comparisons : %i' % comparison_counter)
    print('Swaps: %i' % swap_counter)
    return insects

comparison_counter = 0
swap_counter = 0

def partition(insects, start, last):
    lower = start
    bigger = last
    pivot = insects[last]
    for number in range(start, last):
        Insect.comparison_counter += 1
        if insects[number] > pivot:
            Insect.comparison_counter += 1
            insects[lower], insects[number] = insects[number], insects[lower]
            Insect.swap_counter += 1
            lower += 1
        if insects[number] < pivot:
            Insect.comparison_counter += 1
            insects[number], insects[bigger] = insects[bigger], insects[number]
            Insect.swap_counter += 1
            bigger -= 1
    if lower == bigger:
        Insect.comparison_counter += 1
        pivot = lower
    return pivot

def quick_sort_decrease_weight(insects, start, end):
    pivot = partition(insects, start, end)
    Insect.comparison_counter += 1
    if start < end:
        quick_sort_decrease_weight(insects, start, pivot - 1)
        quick_sort_decrease_weight(insects, pivot + 1, end)
    return insects

def print_info(insects):
    for number in range(0, len(insects) - 1):
        print(str(insects[number]))

ant = Insect('Ant', 0.7, 170)
bee = Insect('Bee', 0.2, 250)
beetle = Insect('Beetle', 0.3, 220)
butterfly = Insect('Butterfly', 0.4, 300)
ladybug = Insect('Ladybug', 0.1, 200)
mosquito = Insect('Mosquito', 2, 180)
insects = [ant, bee, beetle, butterfly, ladybug, mosquito]
print('Unsorted insects:')
print()
print_info(insects)
print()
insects_speed = [ant.speed, bee.speed, beetle.speed, butterfly.speed, ladybug.speed, mosquito.speed]
print('Insects sorted by speed(decrease) -- insertion sort')
print()
start_insertion = time.perf_counter()
print(insertion_sort_decrease_speed(insects_speed))
end_insertion = time.perf_counter()
time_insertion = (end_insertion - start_insertion)
print('Time for insertion sort(ns):')
print(time_insertion)
print()
print_info(insects)
print()
insects_weight = [ant.weight, bee.weight, beetle.weight, butterfly.weight, ladybug.weight, mosquito.weight]
print('Insects sorted be weight(decrease) -- quick sort')
print()
start_quick = time.perf_counter()
print(quick_sort_decrease_weight(insects_weight, 0, len(insects_weight) - 1))
end_quick = time.perf_counter()
time_quick = (end_quick - start_quick)
print('Time for quick sort(ns):')
print(time_quick)
print('Comparisons : %i' % Insect.comparison_counter)
print('Swaps: %i' % Insect.swap_counter)
print()
print_info(insects)


my_phonebook = {}

def add_contact():
    print('\n')
    contact = input('enter name of contact: ')
    number = input('enter number of contact: ')
    my_phonebook[contact] = [number]
    print('contact saved.')
    while True:
        cont = input('do you still want to add '\
                    + 'more numbers to {}? y/n: '.format(contact))
        if cont == 'y':
            number = input('enter number to add for {}'.format(contact))
            my_phonebook[contact].append(number)
        else:
            break
    print('\n')
    display_phone_book()

def add_phone_number():
    print('\n')
    contact = input('enter name of contact to add: ')
    number = input('enter number of contact: ')
    if contact not in my_phonebook:
        print('{} does not exist, adding as a new contact'.format(contact))
        my_phonebook[contact] = [number]
    else:
        my_phonebook[contact].append(number)
    display_phone_book()

def display_phone_book():
    print('\nYour Phonebook')
    for contact, numbers in my_phonebook.items():
        print(contact)
        for n in numbers:
            print('  --{}'.format(n))
    print('\n')

def delete_contact():
    print('\n')
    contact = input('enter name of contact to delete: ')
    if contact in my_phonebook:
        del my_phonebook[contact]
        print('delete successful')
    else:
        print('user does not exist')
    display_phone_book();

def search_contact():
    print('\n')
    search = input('enter search keyword: ')
    results = []
    for i in my_phonebook.keys():
        if i.find(search) != -1:
            results.append({i: my_phonebook[i]})
    print(results)

def search_phone_number():
    search  = input('enter search number: ')
    results = []
    for contact, numbers in my_phonebook.items():
        for num in numbers:
            if num.find(search) != -1:
                results.append({contact: num})
    print(results)

def no_option_found():
    pass

switcher = {
    'ac': add_contact,
    'ap': add_phone_number,
    'dis': display_phone_book,
    'dc': delete_contact,
    'sc': search_contact,
    'sp': search_phone_number,
}

def main():
    while True:
        print('--Phone Book--')
        print('ac - add contact\n'\
            + 'ap - add phone number\n'\
            + 'dis - display phone book\n'\
            + 'dc - delete contact\n'\
            + 'sc - search contact\n'\
            + 'sp - search phone number\n'\
            + 'del - get the longest item\n'\
            + 'x - exit and delete list')
        option = input('option: ')
        if option != 'x':
            func = switcher.get(option, no_option_found)
            func()
        else:
            break


from django.urls import path
from django.views.generic import edit
from .views import CreateCat, CreateComment, EditPost, Main, Detail, CreatePost, DeletePost, Category

urlpatterns = [
    path('', Main.as_view(), name='main'),
    path('post/<int:pk>', Detail.as_view(), name='detail'),
    path('create_post', CreatePost.as_view(), name='create_post'),
    path('create_category', CreateCat.as_view(), name='create_cat'),
    path('edit/<int:pk>', EditPost.as_view(), name='edit_post'),
    path('delete/<int:pk>', DeletePost.as_view(), name='delete_post'),
    path('category/<str:category>/', Category, name='category'),
    path('create_comment/<int:pk>', CreateComment.as_view(), name='create_comment')
]


import numpy as np
import sys

sys.path.append('./')

from analysis.fixtures import sensors_vectors

def correct_paw_used(sensors_data, paw_used):
    if "l" in paw_used.lower():
        return {
            "fr": sensors_data["fl"],
            "fl": sensors_data["fr"],
            "hr": sensors_data["hl"],
            "hl": sensors_data["hr"],
        }
    return sensors_data

def compute_cog(sensors_data: dict) -> np.ndarray:
    """
        Computs the position of the center of gravity.
        Each sensor is a 2d vector indicating it's position in space.
        At each frame, each sensor's vector magnitude is scaled by the
        ammount of weight (in %) recorded by the sensor.
        The sensors vectors are then summed to get a vector indicating the displacement of
        the CoG
    """

    # scale each vector by the % of weight on the sensor on each frame
    # so that each scaled vector is in the 0-1 range
    scaled_vectors = {
        k: sensors_data[k].reshape(-1, 1) * vec / 100
        for k, vec in sensors_vectors.items()
    }

    # sum the vectors to get the CoM vector
    CoM = np.sum(np.dstack(list(scaled_vectors.values())), 2)

    return CoM


import tensorflow as tf

from . import model

def model_fn(features, labels, mode, params):
    """Model function used in the estimator.

    Args:
        features (Tensor): Input features to the model.
        labels (Tensor): Labels tensor for training and evaluation.
        mode (ModeKeys): Specifies if training, evaluation or prediction.
        params (HParams): hyperparameters.

    Returns:
        (EstimatorSpec): Model to be run by Estimator.
    """
    pggan = model.PGGAN(
            learning_rate=params.learning_rate)

    estimator_spec = pggan.get_estimator_spec(features, labels, mode)
    return estimator_spec

def get_estimator(run_config, params):
    return tf.estimator.Estimator(
        model_fn=model_fn,
        params=params,
        config=run_config)


# 4 - Реализуйте выдачу случайного числа
# не использовать random.randint и вообще библиотеку random
# Можете использовать xor, биты, библиотеку time или datetime (миллисекунды или наносекунды)
#  - для задания случайности
# Учтите, что есть диапазон: от(минимальное) и до (максимальное)

from datetime import datetime

# current_time = now.strftime("%H:%M:%S")
# print("Current Time =", current_time)

def rand_num(num_1, num_2):
    now = datetime.now()
    const = float(now.strftime('%f')) / 10**6
    if num_1 > num_2:
        delta = num_1 - num_2
        rnd = (const*delta) + num_2
    else:
        delta = num_2 - num_1
        rnd = (const*delta) + num_1
    print(rnd)

num_1 = int(input('Enter 1 number: '))
num_2 = int(input('Enter 2 number: '))

rand_num(num_1, num_2)


from .models import Flashcard, UserFlashcard
from django.contrib.auth.models import User

def createNewUserFlashcards(user: User):
    user_flashcards = []

    for flashcard in Flashcard.objects.filter(active=True):
        user_flashcards.append(
            UserFlashcard(
                flashcard=flashcard,
                user=user,
            )
        )

    UserFlashcard.objects.bulk_create(user_flashcards)


class Items():
    """
    Creating a class that will hold
    methods for making purchases at a mall.
    """
    def __init__(self,customerName,moneySpent,itemPurchased):
        """
        Initializing the following attributes:
            -    Customer Name
            -    Money Spent
            -    Item's purchased
        """
        self.customerName = customerName
        self.moneySpent = moneySpent
        self.itemPurchased = itemPurchased
    def checkOut(self,totalPaid):
        """
        Creating a method to handle the task
        of buying the items that are in the cart.

        Accepts the following passed values:
            - totalPaid
        """
    def moreFood(self,more_food,stored_food):
        """
        Creating a method that asks the
        user if they would like to buy more food.
        """

        #If the user select y for yes.
        if more_food.lower() == 'y':
            print("Moving on!")
            return True
        #If the user select y for yes.
        elif more_food.lower() == 'n':
            print("Here is what you bought:")
            for my_food in stored_food:
                print(f"You bought some: {my_food}")
            print("Good bye!")
            not_food = True
            not_more = True
        #Handling the alternative case.
        else:
            print("Try again!")
    def placeCart(self,itemname):
        """
        Creating a method to handle the task
        of buying an item that is in the cart.

        Accepts the following passed values:
            - item_name
        """
        #Creating a blank list to hold food.
        my_food = []


import sys
import os

sys.path.append("/workspace/tensorrt/")
import vapoursynth as vs
import functools
from src.scene_detect import scene_detect
from vsgmfss_fortuna import gmfss_fortuna
import torch

torch.set_float32_matmul_precision("medium")

core = vs.core
vs_api_below4 = vs.__api_version__.api_major < 4
core.num_threads = 8

core.std.LoadPlugin(path="/usr/lib/x86_64-linux-gnu/libffms2.so")
core.std.LoadPlugin(path="/usr/local/lib/libvstrt.so")
core.std.LoadPlugin(path="/usr/local/lib/x86_64-linux-gnu/libawarpsharp2.so")

def vfi_frame_merger(
    clip1: vs.VideoNode,
    clip2: vs.VideoNode,
) -> vs.VideoNode:
    core = vs.core
    metric_thresh = 0.999

    def execute(n: int, clip1: vs.VideoNode, clip2: vs.VideoNode) -> vs.VideoNode:
        try:
            ssim_clip = clip1.get_frame(n).props.get("float_ssim")
            if (ssim_clip and ssim_clip > metric_thresh) or clip1.get_frame(
                n
            ).props.get("_SceneChangeNext"):
                return clip1
            return clip2
        except Exception:
            return clip1

    return core.std.FrameEval(
        core.std.BlankClip(clip=clip1, width=clip1.width, height=clip1.height),
        functools.partial(execute, clip1=clip1, clip2=clip2),
    )

def upscale_frame_skip(n, upscaled, metric_thresh, f):
    ssim_clip = f.props.get("float_ssim")

    if n == 0 or n == len(upscaled) - 1 or (ssim_clip and ssim_clip > metric_thresh):
        return upscaled
    else:
        return upscaled[n - 1]

def metrics_func(clip):
    offs1 = core.std.BlankClip(clip, length=1) + clip[:-1]
    offs1 = core.std.CopyFrameProps(offs1, clip)
    return core.vmaf.Metric(clip, offs1, 2)

def inference_clip(video_path="", clip=None):
    # clip = core.ffms2.Source(source=video_path, cache=False)

    clip = core.lsmas.LWLibavSource(source=video_path)
    clip = metrics_func(clip)

    # scene detect
    clip = vs.core.resize.Bicubic(clip, format=vs.RGBH, matrix_in_s="709")
    clip_orig = scene_detect(clip, model_name="efficientnetv2b0+rife46", thresh=0.98)
    clip_orig = vs.core.std.Interleave([clip_orig] * 4)

    # interp
    clip = gmfss_fortuna(
        clip,
        num_streams=2,
        trt=True,
        factor_num=4,
        factor_den=1,
        model=1,
        ensemble=False,
        sc=False,
        trt_cache_path="/workspace/tensorrt/",
    )
    clip = vfi_frame_merger(clip_orig, clip)

    # upscale
    upscaled = core.trt.Model(
        clip,
        engine_path="/workspace/tensorrt/cugan_pro-denoise3x-up2x_fp16_opset18_clamp_and_colorfix.engine",
        num_streams=2,
    )

    upscaled = vs.core.resize.Bicubic(upscaled, format=vs.YUV420P10, matrix_s="709")
    upscaled = metrics_func(upscaled)
    partial = functools.partial(
        upscale_frame_skip, upscaled=upscaled, metric_thresh=0.999
    )
    clip = core.std.FrameEval(
        core.std.BlankClip(clip=upscaled, width=upscaled.width, height=upscaled.height),
        partial,
        prop_src=[upscaled],
    )

    # sharpening
    clip = core.warp.AWarpSharp2(
        clip,
        thresh=128,
        blur=2,
        type=0,
        depth=[16, 8, 8],
        chroma=0,
        opt=True,
        planes=[0, 1, 2],
        cplace="mpeg1",
    )
    clip = core.cas.CAS(clip, sharpness=0.5)

    return clip


import discord
import asyncio
import time

from specs import SECRETS
from specs import FORMAL
from specs import common

client = discord.Client()

# sobald der bot online ist
@client.event
async def on_ready():
    print("Ready to rock!")
    game = discord.Game("-BEEP- I'm a human enjoying human stuff like breathing and eating -BOOP-")
    await client.change_presence(activity=game)

    client.loop.create_task(user_spam())

# sobald jemand joined
@client.event
async def on_member_join(member):

    # schickt private nachricht an member
    nachricht = discord.Embed(

        title=f"Heeey *{member.name}*, wilkommen auf unserem Discord :)",
        description="Common-Sense hat noch nie jemandem geschadet :P",
        color=discord.Color.blue()

    )
    kanal = client.get_user(member.id)
    await kanal.send(embed=nachricht)

    nachricht = discord.Embed(

        title=f"Heeey *{member.name}*, wilkommen auf unserem Discord :)",
        description="Stell dich doch bitte kurz vor ^^",
        color=discord.Color.blue()

    )

    kanal = client.get_channel(489783973271175168)
    await kanal.send(embed=nachricht)

    # weist member rolle zu
    role = discord.utils.get(member.guild.roles, name="member")
    await member.add_roles(role, atomic=True)

# liest nachrichten
@client.event
async def on_message(message):

    f = open("specs\\chatlog.txt", "a")
    f.write(f"{message.channel}\t{time.asctime()}\t{message.author}\t:\t{message.content}\n")
    f.close()

    # befehlsabfrage
    if message.content.startswith(FORMAL.PREFIX):

        # fragt whitelist ab
        if not common.WHITELIST.channels.__contains__(str(message.channel)) or not common.WHITELIST.member.__contains__(message.author.id):
            return

        # invoke gesamte nachricht
	# command erstes wort hinter !
	# arg zweites wort hinter !
        invoke = message.content[1:]
        command = invoke.split(" ")[0]
        try:
            arg = invoke.split(" ")[1:][0]
        except IndexError:
            arg = ""

        # durchsucht befehlsliste nach befehl UND fuehrt befehl aus
        if common.cmd_dict.cmd_list.__contains__(command.upper()):
            await common.cmd_dict.cmd_list.get(command.upper()).ex(message, client, arg)
            """ common --> cmd_dict --> cmd_list --> nimmt dann das gegenstueck zu "PING" --> fuehrt dann "ex" aus"""

            f = open("specs\\log.txt", "a")
            f.write(f"{time.asctime()}\t{message.author}\t{message.author.id} \t {invoke}  \n")
            f.close()

        else:
            nachricht = discord.Embed(

                description=f"*{command}* - is not a valid command",
                color=discord.Color.blue()

            )
            await message.channel.send(embed=nachricht)

	# testcommands werden hier geprueft
    elif message.content == "$":

        nachricht = discord.Embed(

            title="Hallo"

        )

        await message.channel.send(embed=nachricht)

#  Spambot
async def user_spam():

    while client.is_ready():
        # 586167546420658177 --> skyaaas empire
        channel = client.get_channel(586167546420658177)
        for member in channel.members:
            kanal = client.get_user(member.id)

            nachricht = discord.Embed(

                title="Heyyy, This is Skyaaa!",
                description="And I will spam you as long as you are in my channel >:D",
                color=discord.Color.blue()

            )
            await kanal.send(embed=nachricht)

        await asyncio.sleep(2)

client.run(SECRETS.TOKEN)


from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework import generics , status
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.core.serializers import serialize
from django.http import JsonResponse

from aymenProject.pagination import PageNumberPaginationDataOnly
from .serializers import *
from django.contrib.auth import authenticate
from rest_framework_simplejwt.tokens import RefreshToken
from rest_framework.permissions import IsAuthenticated
from rest_framework.parsers import MultiPartParser
import random
import string
import json

class CreateFormView(generics.ListCreateAPIView):
    permission_classes = (IsAuthenticated,)
    serializer_class = FormSerializer
    pagination_class = PageNumberPaginationDataOnly
    def get_queryset(self):
        query_set = Form.objects.all()
        return query_set

    def post(self,request):
        code = ''.join(random.choice(string.ascii_letters + string.digits) for x in range(30))
        choices = Choices(choice = "Option 1")
        choices.save()
        question = Questions(question_type = "multiple choice", question= "Untitled Question", required= False)
        question.save()
        question.choices.add(choices)
        category = Category.objects.get(pk=request.data["category"])
        question.save()
        form = Form(code = code, title = request.data['title'], creator=request.user)
        form.save()
        form.questions.add(question)
        form.category = category
        form.save()
        return Response(FormSerializer(form).data,status=status.HTTP_201_CREATED)

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

class SubmitFormView(APIView):
    permission_classes = (IsAuthenticated,)
    def post(self,request,code):
        answers = json.loads(request.body.decode('utf-8'))
        for a in answers:
            print(a,answers[a],dir(answers[a]))
        try:
            form = Form.objects.get(code = code)
            code = ''.join(random.choice(string.ascii_letters + string.digits) for x in range(20))
            if form.authenticated_responder:
                response = Responses(response_code = code, response_to = form, responder_ip = get_client_ip(request), responder = request.user)
                response.save()
            else:
                if not form.collect_email:
                    response = Responses(response_code = code, response_to = form, responder_ip = get_client_ip(request))
                    response.save()
                else:
                    response = Responses(response_code = code, response_to = form, responder_ip = get_client_ip(request), responder_email=request.POST["email-address"])
                    response.save()
            for i in answers:
                print('******',i)
                #Excluding csrf token
                if i == "csrfmiddlewaretoken" or i == "email-address":
                    continue
                question = form.questions.get(id = i)
                for j in answers[i]:
                    answer = Answer(answer=j, answer_to = question)
                    answer.save()
                    response.response.add(answer)
                    response.save()
            return Response(FormSerializer(form).data,status=status.HTTP_200_OK)

        except Form.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

    # def get(self,request,code):
    #     try:
    #         form = Form.objects.get(code = code)
    #         return Response(data=FormSerializer(form).data)
    #     except Form.DoesNotExist:
    #         return Response(status=status.HTTP_404_NOT_FOUND)
class ResponsesFormView(APIView):
    permission_classes = (IsAuthenticated,)
    def get(self,request,code):
        try:
            form = Form.objects.get(code = code)
            responsesSummary = []
            choiceAnswered = {}
            filteredResponsesSummary = {}
            for question in form.questions.all():
                answers = Answer.objects.filter(answer_to = question.id)
                if question.question_type == "multiple choice" or question.question_type == "checkbox":
                    choiceAnswered[question.question] = choiceAnswered.get(question.question, {})
                    for answer in answers:
                        choice = answer.answer_to.choices.get(choice = answer.answer).choice
                        choiceAnswered[question.question][choice] = choiceAnswered.get(question.question, {}).get(choice, 0) + 1
                responsesSummary.append({"question": QuestionSerilizer(question).data, "answers":json.loads(serialize("json",answers)) })
            for answr in choiceAnswered:
                filteredResponsesSummary[answr] = {}
                keys = choiceAnswered[answr].values()
                for choice in choiceAnswered[answr]:
                    filteredResponsesSummary[answr][choice] = choiceAnswered[answr][choice]
            resp = Responses.objects.filter(response_to = form)
            return Response(data={
                "form": FormSerializer(form).data,
                "responses": json.loads(serialize("json",resp)),
                "responsesSummary": responsesSummary,
                "filteredResponsesSummary": filteredResponsesSummary
            },status=status.HTTP_201_CREATED)
        except Form.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

class ResponseFormView(APIView):
    permission_classes = (IsAuthenticated,)
    def get(self,request,code,response_code):
        try:
            formInfo = Form.objects.get(code = code)
            responseInfo = Responses.objects.filter(response_code = response_code)
            total_score = 0
            score = 0
            if responseInfo.count() == 0:
                return Response(status=status.HTTP_404_NOT_FOUND)
            else: responseInfo = responseInfo[0]
            if formInfo.is_quiz:
                for i in formInfo.questions.all():
                    total_score += i.score
                for i in responseInfo.response.all():
                    if i.answer_to.question_type == "short" or i.answer_to.question_type == "paragraph":
                        if i.answer == i.answer_to.answer_key: score += i.answer_to.score
                    elif i.answer_to.question_type == "multiple choice":
                        answerKey = None
                        for j in i.answer_to.choices.all():
                            if j.is_answer: answerKey = j.id
                        if answerKey is not None and int(answerKey) == int(i.answer):
                            score += i.answer_to.score
                _temp = []
                for i in responseInfo.response.all():
                    if i.answer_to.question_type == "checkbox" and i.answer_to.pk not in _temp:
                        answers = []
                        answer_keys = []
                        for j in responseInfo.response.filter(answer_to__pk = i.answer_to.pk):
                            answers.append(int(j.answer))
                            for k in j.answer_to.choices.all():
                                if k.is_answer and k.pk not in answer_keys: answer_keys.append(k.pk)
                            _temp.append(i.answer_to.pk)
                        if answers == answer_keys: score += i.answer_to.score
            return Response(data= {
            "form": FormSerializer(formInfo).data,
            "response": ResponseSerilizer(responseInfo).data,
            "score": score,
            "total_score": total_score
    },status=status.HTTP_200_OK)
        except Responses.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

class DeleteFormView(generics.DestroyAPIView):
    permission_classes = (IsAuthenticated,)
    serializer_class = FormSerializer
    def delete(self,request,code):
        try:
            form = Form.objects.get(code = code)
        except Form.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        operation = form.delete()
        data = {}
        if operation:
            data["sucess"] = "deleted sucessfully"
        else:
            data["failure"] = "delete failed"
        return Response(data=data)

class UpdateFormView(APIView):
    permission_classes = (IsAuthenticated,)
    serializer_class = FormSerializer
    def get(self,request,code):
        try:
            form = Form.objects.get(code = code)
            return Response(data=FormSerializer(form).data)
        except Form.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
    def patch(self,request,code):
        try:
            form = Form.objects.get(code = code)
            print(form.title)
        except Form.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        data=request.data.copy()
        data['code'] = form.code
        data['creator'] = form.creator.pk
        data['category'] = request.data.get('category',form.category)
        data['questions'] = request.data.get('questions', form.questions.values_list(flat=True))
        data['title'] = request.data.get('title',form.title)
        serializer = FormSerializer(form,data=data)
        data = {}
        if serializer.is_valid():
            serializer.save()
            return Response(data=serializer.data)
        return Response(serializer._errors,status=status.HTTP_400_BAD_REQUEST)

class CreateQuestionView(generics.ListCreateAPIView):
    permission_classes = (IsAuthenticated,)
    serializer_class = QuestionSerilizer
    def get_queryset(self):
        return Questions.objects.all()
    def post(self,request,code):
        data=request.data.copy()
        form = Form.objects.get(code=code)
        choices = Choices(choice = "Option 1")
        choices.save()
        question = Questions(question_type = "multiple choice", question= data['title'], required= data['is_manadatory'])
        question.save()
        question.choices.add(choices)
        question.save()
        form.questions.add(question)
        form.save()
        return Response(FormSerializer(form).data,status=status.HTTP_201_CREATED)

class CreateChoiceView(APIView):
    def post(self,request,code):
        question = Questions.objects.get(id=code)
        data=request.data.copy()

        choices = Choices(choice = data['title'])
        choices.save()
        question.choices.add(choices)
        question.save()
        return Response(QuestionChoice(question).data,status=status.HTTP_201_CREATED)

class UpdateQuestionView(APIView):
    permission_classes = (IsAuthenticated,)
    serializer_class = QuestionSerilizer
    def get(self,request,code):
        try:
            question = Questions.objects.get(id = code)
            return Response(data={"question":QuestionChoice(question).data})
        except Form.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

    def patch(self,request,code):
        try:
            question = Questions.objects.get(pk = code)
        except Form.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        print(request.data)
        data=request.data.copy()
        data['question'] = request.data.get('question',question.question)
        data['question_type'] = request.data.get('question_type',question.question_type)
        data['feedback'] = request.data.get('feedback',question.feedback)
        data['answer_key'] = request.data.get('answer_key',question.answer_key)
        data['choices'] = request.data.get('choices',question.choices.values_list(flat=True))

        serializer = QuestionSerilizer(question,data=data)
        data = {}
        if serializer.is_valid():
            serializer.save()
            data["success"] = "updated sucessfully"
            return Response(data=data)
        return Response(serializer._errors,status=status.HTTP_400_BAD_REQUEST)

class DeleteQuestionView(generics.DestroyAPIView):
    permission_classes = (IsAuthenticated,)
    serializer_class = QuestionSerilizer
    def delete(self,request,code):
        try:
            question = Questions.objects.get(pk = code)
        except Form.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        operation = question.delete()
        data = {}
        if operation:
            data["sucess"] = "deleted sucessfully"
        else:
            data["failure"] = "delete failed"
        return Response(data=data)

class DeleteChoiceView(generics.DestroyAPIView):
    permission_classes = (IsAuthenticated,)
    serializer_class = ChoiceSerilizer
    def delete(self,request,code):
        try:
            choice = Choices.objects.get(pk = code)
        except Choices.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        operation = choice.delete()
        data = {}
        if operation:
            data["sucess"] = "deleted sucessfully"
        else:
            data["failure"] = "delete failed"
        return Response(data=data)

class FormQuestionsView(APIView):
    permission_classes = (IsAuthenticated,)
    pagination_class = PageNumberPaginationDataOnly
    def get(self,request,code):
        query_set = Form.objects.get(code=code)
        query = Questions.objects.filter(id__in=query_set.questions.values_list('id',flat=True))
        serializer = QuestionChoice(query,many=True)
        result = serializer.data
        data =  {
            'questions' : result
        }
        return Response(data=data)


tc = int(input())
for TC in range(1,tc+1):
    answer = -1
    N,A,B = map(int,input().split())
    for i in range(1,N):
        j = 1
        while i*j <= N:
            result = A*abs(i-j) + B*(N-i*j)
            if answer == -1:
                answer = result
            else:
                answer = min(answer,result)
            j+=1
    print(f'#{TC} {answer}')


from evaluate_RoI import evaluate_RoI
from evaluate_vote import evaluate_vote
import os
import para
import numpy as np
import Image
import csv

img_name='labschoolreport-0002-012-8.tiff'
fold_name='hard'
img_path=os.path.join(para.data_result_path+'/data/test',fold_name,img_name)
model_path_RoI=para.data_result_path+'/models/checkpoint_RoI.h5'
lineseg_path=os.path.join(para.data_result_path+'/data/test\lines','lines_'+img_name+'.txt')
model_path_class=para.data_result_path+'/models/checkpoint_reduced_units.h5'
n_iteration=6

for i in range(n_iteration):
    print(i)
    output_path=para.data_result_path+'/iterate/'+str(i)+'.txt'
    evaluate_RoI(img_path,output_path,model_path_RoI,lineseg_path)
    charseg_path=output_path
    output_path=para.data_result_path+'/iterate/'+str(i)+'.png'
    evaluate_vote(img_path,output_path,model_path_class,lineseg_path,charseg_path)
    img_path=output_path

lines=[]
with open(lineseg_path, newline='') as linefile:
    reader = csv.reader(linefile, delimiter=',', quotechar='|')
    for row in reader:
        lines.append([int((row[0].split())[0]),int((row[0].split())[1])])

img_path=os.path.join(para.data_result_path+'/data/test',fold_name,img_name)
doc_img=Image.open(img_path)
doc_img=np.array(doc_img)
output=np.zeros((40*(n_iteration+1)*len(lines),doc_img.shape[1]),dtype=np.uint8)+255

for i in range(n_iteration+1):
    for idx,line in enumerate(lines):
        output[idx*40*(n_iteration+1)+40*i:idx*40*(n_iteration+1)+40*(i+1),:]=doc_img[line[0]:line[1]+1,:]
    if i<n_iteration:
        doc_img=Image.open(para.data_result_path+'/iterate/'+str(i)+'.png')
        doc_img=np.array(doc_img)

img = Image.fromarray(output, 'L')
img.save(para.data_result_path+'/iterate/multi.png')

'''
charseg_path=para.data_result_path+'/iterate/char.txt'
evaluate_RoI(img_path,charseg_path,model_path_RoI,lineseg_path)
for i in range(n_iteration):
    print(i)
    output_path=para.data_result_path+'/iterate/'+str(i)+'.png'
    evaluate_vote(img_path,output_path,model_path_class,lineseg_path,charseg_path)
    img_path=output_path
'''


import pytest

@pytest.fixture
def pyomop_fixture():
    from src.pyomop import CdmEngineFactory
    cdm = CdmEngineFactory()
    return cdm

@pytest.fixture
def metadata_fixture():
    from src.pyomop import metadata
    return metadata

def test_create_db(pyomop_fixture, metadata_fixture, capsys):
    import datetime
    from sqlalchemy.sql import select
    engine = pyomop_fixture.engine
    metadata_fixture.create_all(engine)
    from src.pyomop import Cohort
    # Cohort = pyomop_fixture.base.cohort
    session =  pyomop_fixture.session
    session.add(Cohort(cohort_definition_id=2, subject_id=100,
            cohort_end_date=datetime.datetime.now(),
            cohort_start_date=datetime.datetime.now()))
    session.commit()

    s = select([Cohort])
    result = session.execute(s)
    for row in result:
        print(row)
    result.close()
    assert row['subject_id'] == 100


# Mohammad Mishal S. Noroña | BSCPE 1-5 | Assignment #7
# Import Colorama
import colorama
from colorama import Fore, Back, Style
colorama.init()
# Add Time Delay
import time
# Import Classes
from Class_Calc import Calculator
from Class_User_Input import UserInput
from Calculator_Inh import calc_inh
def start():
    calc = Calculator()
    ui = UserInput()
    inh = calc_inh()
    # Introduction
    print("")
    print(Fore.CYAN + "WELCOME TO SIMPLE CALCULATOR".center(40," ") )
    print(Fore.YELLOW + "By: Mishal Noroña".center(40," ") + Fore.WHITE )
    print(Fore.YELLOW + "\n Note: Calculating Percent \n Number1: (Value) \n Number2: (Percent) \n".center(40," ") + Fore.WHITE )
    # Ask for user's Number Input
    num1 = ui.num_input()
    num2 = ui.num_input()
    # Ask for Operation Input
    operation = ui.operation_input()
    if operation == "a":
        print(Fore.LIGHTBLUE_EX + "\nCalculating Please wait......\n" + Fore.YELLOW)
        time.sleep(1.0)
        print(num1, "+", num2, Fore.LIGHTGREEN_EX + "\nAnswer =", calc.addition(num1, num2))
        # If user enters "a" Print output of addition
    elif operation == "s":
        print(Fore.LIGHTBLUE_EX + "\nCalculating Please wait......\n" + Fore.YELLOW)
        time.sleep(1.0)
        print(num1, "-", num2, Fore.LIGHTGREEN_EX + "\nAnswer =", calc.subtraction(num1, num2))
        # If user enters "s" Print output of subtraction
    elif operation == "m":
        print(Fore.LIGHTBLUE_EX + "\nCalculating Please wait......\n" + Fore.YELLOW)
        time.sleep(1.0)
        print(num1, "*", num2, Fore.LIGHTGREEN_EX + "\nAnswer =", calc.multiplication(num1, num2))
        # If user enters "m" Print output of multiplication
    elif operation == "d":
        print(Fore.LIGHTBLUE_EX + "\nCalculating Please wait......\n" + Fore.YELLOW)
        time.sleep(1.0)
        print(num1, "/", num2, Fore.LIGHTGREEN_EX + "\nAnswer =", calc.division(num1, num2))
        # If user enters "d" Print output of division
    elif operation == "p":
        print(Fore.LIGHTBLUE_EX + "\nCalculating Please wait......\n" + Fore.YELLOW)
        time.sleep(1.0)
        print(num2, "%" + " of", num1, Fore.LIGHTGREEN_EX + "\nAnswer =", inh.percent(num1, num2))
        # If user enters "p" Print output of percent
    elif operation == "e":
        print(Fore.LIGHTBLUE_EX + "\nCalculating Please wait......\n" + Fore.YELLOW)
        time.sleep(1.0)
        print(num1, "^", num2, Fore.LIGHTGREEN_EX + "\nAnswer =", inh.exponent(num1, num2))
        # If user enters "e" Print output of exponent
        #
    # Ask for another Input
    more_input = ui.try_again()
    if more_input == "y":
        start()
    elif more_input == "n":
        print("Thank you for using Simple Calculator")
    else:
        print("\nInvalid Input")
        exit()
start()


import json
import os
import ssl
from datetime import date
import pandas as pd
import geopandas as gpd
from util import get_data, normalize_text, get_company_names, company_rename, get_company_list, prepare_ids, apply_system_id, set_cwd_to_script, updated_month_year
import numpy as np
ssl._create_default_https_context = ssl._create_unverified_context
set_cwd_to_script()

# def get_sql(sql=False, query='projects_regdocs.sql'):
#     csv_name = query.replace(".sql", ".csv")
#     if sql:
#         print('reading sql '+query)
#         df = execute_sql(os.path.join(os.getcwd(), "queries"), query)
#         df.to_csv('raw_data/'+csv_name, index=False)
#     else:
#         print('reading local csv '+csv_name)
#         df = pd.read_csv('raw_data/'+csv_name)
#     return df

def import_simplified(replace, name='economic_regions.json'):
    read_path = os.path.join(os.getcwd(), "../data_output/conditions/base_maps/", name)
    df = gpd.read_file(read_path)
    df = df.set_geometry('geometry')
    fr_cols = ['PRNAME', 'ERNAME']
    for splitcols in fr_cols:
        df[splitcols] = [x.split('/') for x in df[splitcols]]
        df[splitcols+'_en'] = [x[0].strip() for x in df[splitcols]]
        df[splitcols+'_fr'] = [x[0].strip() if len(x) == 1 else x[-1].strip() for x in df[splitcols]]

    for delete in ['PRNAME', 'ERNAME', 'PRUID', 'ERUID', 'PRNAME_fr', 'ERNAME_fr']:
        del df[delete]

    df['region_id'] = [er.strip()+'/'+pr.strip() for er, pr in zip(df['ERNAME_en'],
                                                                   df['PRNAME_en'])]

    df['region_id'] = df['region_id'].replace(replace)
    df = df[df['region_id'].isin(replace.values())]
    del df['ERNAME_en']
    del df['PRNAME_en']
    return df

def export_files(df, folder, name):
    write_path = os.path.join(os.getcwd(), folder, name)
    df.to_file(write_path, driver='GeoJSON')
    print(folder+' done ', 'CRS: '+str(df.crs))

def conditions_on_map(df, shp):
    shp = pd.merge(shp,
                   df,
                   how='inner',
                   left_on=['region_id'],
                   right_on=['id'])

    del shp['region_id']
    shp = shp[~shp.geometry.is_empty]
    shp = shp[shp.geometry.notna()]
    for numeric_col in ['In Progress', 'Closed']:
        if numeric_col in shp.columns:
            shp[numeric_col] = shp[numeric_col].fillna(0)

    # df becomes map metadata
    df = df.fillna(0)
    for cols in [["Closed", "c"], ["In Progress", "i"]]:
        if cols[0] in df:
            df[cols[0]] = [int(x) for x in df[cols[0]]]
    return shp, df

def condition_meta_data(df, project_names):

    def convert_to_int(df):
        df = df.replace({np.nan: 0})
        for col in ['In Progress', 'Closed']:
            try:
                df[col] = [int(x) for x in df[col]]
            except:
                None
        return df

    def add_missing(df):
        if 'In Progress' not in df.columns:
            df['In Progress'] = 0
        if 'Closed' not in df.columns:
            df['Closed'] = 0
        return df

    # df contains the condition data for the spcecific company
    meta = {}
    # get the summary stats for the boxes above the map
    status = df[['condition id', 'Condition Status', 'Location']].copy().reset_index(drop=True)
    status['loc'] = ['no' if '-1' in x else 'yes' for x in status['Location']]
    status = status[status['loc'] != 'no']
    del status['Location']
    del status['loc']
    status = status.drop_duplicates(subset=['condition id'])
    status = status.groupby(['Condition Status']).size().reset_index()
    status = pd.pivot_table(status, values=0, columns="Condition Status")
    status = add_missing(status)
    status = status.to_dict(orient='records')[0]
    df['Location'] = df['Location'].astype("object")
    notInMap = 0
    noLoc = {}
    for location, statusloc in zip(df['Location'], df['Condition Status']):
        if "-1" in location:
            notInMap = notInMap+1
            if statusloc in noLoc:
                noLoc[statusloc] = noLoc[statusloc]+1
            else:
                noLoc[statusloc] = 1

    status['notOnMap'] = {"total": notInMap,
                          "status": noLoc}

    # get the date the data was pulled
    today = date.today()
    status['updated'] = [today.year, today.month-1, today.day]
    # status['updated'] = [2021, 2, 19]  # override todays date with custom date

    # get the current company name
    status['companyName'] = list(df['Company'])[0]
    meta['summary'] = status

    # once the status summary is calculated, blank locations and null locations can be removed
    df = df[df['id'] != -1].copy().reset_index(drop=True)

    # get the unique project names sorted by number of open conditions
    project = df[['condition id', 'Short Project Name', 'id', 'Condition Status', 'Regdocs']].copy().reset_index(drop=True)
    # add the project name lookup to metadata
    this_company_projects = list(set(project["Short Project Name"]))
    this_lookup = project_names[project_names['id'].isin(this_company_projects)]
    meta['projectLookup'] = prepare_ids(this_lookup)
    project['Regdocs'] = project['Regdocs'].astype('object')
    project['Regdocs'] = project['Regdocs'].fillna('noRegdocs')
    project = project.groupby(['Short Project Name', 'id', 'Condition Status', 'Regdocs']).size().reset_index()
    project = pd.pivot_table(project,
                             values=0,
                             index=['Short Project Name', 'id', 'Regdocs'],
                             columns='Condition Status').reset_index()

    project['Regdocs'] = project['Regdocs'].replace('noRegdocs', np.nan)
    project = add_missing(project)

    project = project.sort_values(by=['In Progress', 'id'], ascending=False)
    project['In Progress'] = pd.to_numeric(project['In Progress'])

    project = convert_to_int(project)
    project['Regdocs'] = [int(x) for x in project['Regdocs']]
    # optimize json size
    project = project.rename(columns={"Short Project Name": "n"})
    project['v'] = [[inProgress, closed, regdocs] for inProgress, closed, regdocs in zip(project['In Progress'],
                                                                                project['Closed'],
                                                                                project['Regdocs'])]
    for delete in ['In Progress', 'Closed', 'Regdocs']:
        del project[delete]
    project = project.to_dict(orient='records')
    meta['projects'] = project

    # get the unique project themes sorted by number of open conditions
    theme = df[['condition id', 'Theme(s)', 'id', 'Condition Status']].copy().reset_index(drop=True)
    theme['Theme(s)'] = [[str(i) for i in x] for x in theme['Theme(s)']]
    theme['Theme(s)'] = [', '.join(x) for x in theme['Theme(s)']]
    theme = theme.groupby(['Theme(s)', 'id', 'Condition Status']).size().reset_index()
    theme = pd.pivot_table(theme,
                           values=0,
                           index=['Theme(s)', 'id'],
                           columns='Condition Status').reset_index()
    theme = add_missing(theme)
    theme = theme.sort_values(by=['In Progress', 'id'], ascending=False)
    # optimize json size
    theme = convert_to_int(theme)
    theme = theme.rename(columns={"Theme(s)": "t"})
    theme['t'] = [[str(i.strip()) for i in x.split(',')] for x in theme['t']]
    theme["v"] = [[inProgress, closed] for inProgress, closed in zip(theme['In Progress'],
                                                                     theme['Closed'])]
    for delete in ['In Progress', 'Closed']:
        del theme[delete]
    theme = theme.to_dict(orient='records')
    meta['themes'] = theme

    df_all = df.copy()
    del df_all['Location']
    df_all = df_all.groupby(['id',
                             'Condition Status']).agg({
                                 'condition id': 'count',
                                 'Short Project Name': lambda x: list(x),
                                 'Theme(s)': lambda t: list(t)})

    for delete in ['Short Project Name', 'Theme(s)']:
        del df_all[delete]

    df_all = df_all.reset_index()
    df_all = pd.pivot_table(df_all,
                            values='condition id',
                            index=['id'],
                            columns='Condition Status').reset_index()

    # this fixes the map color index being thrown off by non map conditions
    df_all = df_all[df_all["id"] != "-1"].copy()
    return df_all, meta

def add_links(df, sql):
    df_links = get_data(os.getcwd(), "projects_regdocs.sql", db='tsql23cap', sql=sql)
    l = {}
    for name, folder in zip(df_links['EnglishProjectName'], df_links['CS10FolderId']):
        l[name] = folder

    regdocs = []
    for proj in df['Project Name']:
        try:
            regdocs.append(l[proj])
        except KeyError:
            regdocs.append(np.nan)
    df['Regdocs'] = regdocs
    # calculate warning for number of projects with no regdocs link
    df_no_link = df[~df['Regdocs'].notnull()].copy().reset_index(drop=True)
    no_link_proj = df_no_link['Short Project Name'].nunique()
    all_proj = df['Short Project Name'].nunique()
    pct = round((no_link_proj/all_proj)*100, 1)
    print("There are "+str(no_link_proj) +" projects without a regdocs link (" +str(pct) +"% of all projects)")
    return df

def idify_conditions(df, sql):

    def list_id(df, column, toReplace):
        new_themes = []
        for t in df[column]:
            if "," in t:
                t = [x.strip() for x in t.split(",")]
                t = [toReplace[x] for x in t]
                new_themes.append(t)
            else:
                try:
                    new_themes.append([toReplace[t.strip()]])
                except:
                    new_themes.append(["-1"])

        df[column] = new_themes
        return df

    projects = get_data(os.getcwd(), "conditionProjects.sql", "tsql23cap", sql)
    themes = get_data(os.getcwd(), "conditionThemes.sql", "tsql23cap", sql)
    regions = get_data(os.getcwd(), "conditionRegions.sql", "tsql23cap", sql)

    for ids in [projects, themes, regions]:
        ids['id'] = [str(x) for x in ids['id']]

    project_replace = {value: key for key, value in zip(projects['id'],
                                                       projects['e'])}
    theme_replace = {value: key for key, value in zip(themes['id'],
                                                     themes['e'])}
    region_replace = {value: key for key, value in zip(regions['id'],
                                                      regions['e'])}

    df['Short Project Name'] = df['Short Project Name'].replace(project_replace)

    df = list_id(df, "Theme(s)", theme_replace)
    df["Location"] = [x.replace(" /", "/").replace("/ ", "/") for x in df["Location"]]
    df = list_id(df, "Location", region_replace)

    # save themes and regions for import into front end
    files = [[themes, "themes"], [regions, "regions"]]
    for file in files:
        id_save = prepare_ids(file[0])
        with open('../data_output/conditions/metadata/'+file[1]+'.json', 'w') as fp:
            json.dump(id_save, fp)

    return df, region_replace, projects

def process_company(df, company, project_names, regions_map, test, save):
    this_company_data = {}
    folder_name = company.replace(' ', '').replace('.', '')
    df_c = df[df['Company'] == company].copy().reset_index(drop=True)
    if not df_c.empty:
        df_c['condition id'] = [str(ins)+'_'+str(cond) for ins, cond in zip(df_c['Instrument Number'], df_c['Condition Number'])]
        expanded_locations = []
        for unique in df_c['condition id']:
            row = df_c[df_c['condition id'] == unique].copy().reset_index(drop=True)
            for region in list(row['Location'])[0]:
                row['id'] = region
                expanded_locations.append(row.copy())

        df_all = pd.concat(expanded_locations, axis=0, sort=False, ignore_index=True)
        # calculate metadata here
        dfmeta, meta = condition_meta_data(df_all, project_names)
        meta["build"] = True
        this_company_data['meta'] = meta
        shp, map_meta = conditions_on_map(dfmeta, regions_map)

        this_company_data['regions'] = shp.to_json()
        this_company_data['mapMeta'] = map_meta.to_dict(orient='records')
        if not test and save:
            with open('../data_output/conditions/'+folder_name+'.json', 'w') as fp:
                json.dump(this_company_data, fp)
    else:
        meta = {"companyName": company}
        shp = None
        dfmeta = None
        this_company_data = {'meta': {"companyName": company,
                                      "build": False},
                             'regions': "{}",
                             'mapMeta': []}

        if not test and save:
            with open('../data_output/conditions/'+folder_name+'.json', 'w') as fp:
                json.dump(this_company_data, fp)
    return df_c, shp, dfmeta, meta

def process_conditions(remote=False,
                       sql=False,
                       non_standard=True,
                       company_names=False,
                       companies=False,
                       test=False,
                       save=True):
    if remote:
        print('downloading remote conditions file')
        link = 'http://www.cer-rec.gc.ca/open/conditions/conditions.csv'
        df = pd.read_csv(link,
                         # sep='\t',
                         # lineterminator='\r',
                         encoding="latin-1",
                         error_bad_lines=True)
        df = normalize_text(df, ['Location', 'Short Project Name', 'Theme(s)'])

    elif test:
        print('reading test conditions data')
        df = pd.read_csv('./raw_data/test_data/conditions.csv', encoding="UTF-16", sep='\t')
        df = normalize_text(df, ['Location', 'Short Project Name', 'Theme(s)'])
    else:
        print('reading local conditions data')
        df = pd.read_csv('./raw_data/conditions_en.csv', encoding="UTF-16", sep='\t')
        df = normalize_text(df, ['Location', 'Short Project Name', 'Theme(s)'])

    for date_col in ['Effective Date', 'Issuance Date', 'Sunset Date']:
        df[date_col] = pd.to_datetime(df[date_col])

    if not non_standard:
        # only include non-standard conditions
        df = df[df['Condition Type'] != 'Standard']

    delete_cols = ['Condition',
                   'Condition Phase',
                   'Instrument Activity',
                   'Condition Type',
                   'Condition Filing']

    for delete in delete_cols:
        del df[delete]

    for r in ['\n', '"']:
        df['Company'] = df['Company'].replace(r, '', regex=True)
    df['Company'] = [x.strip() for x in df['Company']]
    df['Condition Status'] = df['Condition Status'].astype('object')
    df['Condition Status'] = [str(x).strip() for x in df['Condition Status']]
    # preliminary processing
    df['Company'] = df['Company'].replace(company_rename())
    df = apply_system_id(df, "Company")

    df = df[df['Short Project Name'] != "SAM/COM"].copy().reset_index(drop=True)

    df = add_links(df, sql)
    if company_names:
        print(get_company_names(df['Company']))

    df, region_replace, project_names = idify_conditions(df, sql)
    regions_map = import_simplified(region_replace)

    if companies:
        company_files = companies
    else:
        company_files = get_company_list("all")

    for company in company_files:
        try:
            df_c, shp, dfmeta, meta = process_company(df, company, project_names, regions_map, test, save)
            print("completed: "+company)
        except:
            print("conditions error: "+company)
            raise

    updated_month_year("conditions")
    return df_c, shp, dfmeta, meta

if __name__ == "__main__":
    print('starting conditions...')
    df_, regions_, map_meta_, meta_ = process_conditions(remote=False, save=True, sql=True, companies=["NGTL"])
    # company_df, regions, mapMeta, meta = process_conditions(remote=False, companies=['NGTL'], test=True)
    print('completed conditions!')


#!/usr/bin/env python

import os, json, sys, glob, os.path

folder = "CLX"

def get_files(folder):
    out = []
    p = os.path.join(os.getcwd(), folder)
    cp = os.path.join(p, "*_core*.json")
    flist = sorted(glob.glob(cp))
    out.append(flist[-1])
    cp = os.path.join(p, "*_uncore*.json")
    flist = sorted(glob.glob(cp))
    out.append(flist[-1])
    return out

opt_dict = { "Invert"       : "EVENT_OPTION_INVERT",
             "EdgeDetect"   : "EVENT_OPTION_EDGE",
             "CounterMask"  : "EVENT_OPTION_THRESHOLD",
             "AnyThread"    : "EVENT_OPTION_ANYTHREAD"}

def analyse_file(f):
    o = []
    fp = open(f, "rb")
    s = fp.read().decode("UTF-8")
    j = json.loads(s)
    for e in j:
        c = "NONE"
        if e["Counter"].startswith("Fixed"):
            c = "FIXC" + e["Counter"][-1]
        elif e["Counter"] == "0,1,2,3":
            c = "PMC"
        elif e["Counter"] and not "UNC_" in e["EventName"]:
            c = "PMC" + e["Counter"]
        else:
            print(e)

        x = {"Name" : e["EventName"], "Event" : e["EventCode"], "Umask" :
                e["UMask"], "Limit" : c}
        opts = []
        for opt in opt_dict:
            if e[opt] != "0":
                opts.append("%s=0x%X" % (opt_dict[opt], int(e[opt])))
        if len(opts) > 0:
            x.update({"DefaultOpts" : ",".join(opts)})
        o.append(x)
    return o

def printout(l):
    for e in l:
        elist = e["Event"].split(",")

        for i, c in enumerate(elist):
            ename = e["Name"].split(".")[0]
            tname = e["Name"].replace(".", "_")
            if len(elist) > 1:
                tname = tname.replace(ename, ename + "_" + str(i))
                ename = ename + "_" + str(i)

            print("EVENT_%s\t\t%s\t%s" % (ename, c.strip(), e["Limit"]))
            if "DefaultOpts" in e:
                print("DEFAULT_OPTIONS_%s\t%s" % (tname, e["DefaultOpts"],))
            print("UMASK_%s\t%s" % (tname, e["Umask"],))
            print("")

flist = get_files(folder)
out = []
for f in flist:
    out += analyse_file(f)
    break
#print(out)
printout(out)


from flask import render_template, url_for, redirect, request, Blueprint, flash
from application.hams.forms import SelectMethodForm, AcquisitionForm, DeconvLibrarySearch, LibrarySearch, ImportDeconv, AlgorithmnForm
from application.models import User, Acquisition_Hrms, Ms_System, Sample_Information, Sample_Location, Analytical_Column, Chromatographic_Condition, Chrom_Time, Lc_System, Threshold_Setting
from application import db
from application.hams.utils import save_datafile_L, save_datafile_D, save_datafile_DL, DownloadFromS3ToLocal
from flask_login import login_required, current_user
from urllib.request import urlopen

import secrets
import subprocess

hams = Blueprint('hams', __name__)

details = [
    {
        'title' : 'Library Search',
        'description' : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."
    },
    {
        'title' : 'Import Deconv',
        'description' : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."
    },
    {
        'title' : 'Deconv & Library Search',
        'description' : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."
    }
]

def getMethodList():
    methodList = []
    for method in SelectMethodForm().methods:
        methodList.append(method)
    return methodList

def getUserIcon():
    if current_user.is_authenticated:
        user_icon = url_for('static', filename='imgs/' + current_user.user_icon)
        return user_icon

def getJobID():
    random_hex = secrets.token_hex(8)
    while Sample_Information.query.filter_by(sample_job_number=random_hex).first():
        random_hex = secrets.token_hex(8)

    return random_hex

@hams.route("/manageJobs")
@login_required
def manageJobs():
    user_icon = getUserIcon()
    return render_template('manageJobs.html',icon = user_icon, title = "HAMS")

@hams.route("/chooseMethod", methods=['GET', 'POST'])
@login_required
def chooseMethod():
    form = SelectMethodForm()
    methodList = getMethodList()
    user_icon = getUserIcon()
    if form.validate_on_submit():
        chosenMethod = form.methods.data
        return redirect(url_for('hams.acquisition', chosenMethod = chosenMethod))

    return render_template('chooseMethod.html', title = "HAMS", form = form,
                             details = details, methods = methodList, icon = user_icon)

@hams.route("/acquisition/<chosenMethod>", methods=['GET', 'POST'])
@login_required
def acquisition(chosenMethod):
    user_icon = getUserIcon()

    if chosenMethod == 'LibrarySearch':
        form = LibrarySearch()
    elif chosenMethod == 'ImportDeconv':
        form = ImportDeconv()
    else:
        form = DeconvLibrarySearch()

    if form.validate_on_submit():
        j_id = getJobID()

        if chosenMethod == 'ImportDeconv':
            fileList = []
            fileList.append(form.xlsxFile)
            fileList.append(form.VANFileLow)
            fileList.append(form.VANFileHigh)

            d_set = []
            d_set.append(form.Max_W.data)
            d_set.append(form.Min_Int.data)
            d_set.append(form.Retention_Window.data)
            d_set.append(form.R_Min.data)
            d_set.append(form.P_Max.data)
            d_set.append(form.Retention_Time_Tollerance.data)
            d_set.append(form.Ms_P_W.data)
            d_set.append(form.Mass_Tol.data)
            d_set.append(form.Mass_Window.data)
            d_set.append(form.Signal_to_Noise.data)

            save_datafile_D(fileList, current_user.user_email, j_id, d_set)

        elif chosenMethod == 'LibrarySearch':
            #source =
            #mode =

            save_datafile_L(form.txtFile, current_user.user_email, j_id, 'ESI', 'POSITIVE')

        else:
            fileList = []
            fileList.append(form.xlsxFile)
            fileList.append(form.VANFileLow)
            fileList.append(form.VANFileHigh)
            fileList.append(form.txtFile)

            d_set = []
            d_set.append(float(form.Max_W.data))
            d_set.append(int(form.Min_Int.data))
            d_set.append(float(form.Retention_Window.data))
            d_set.append(float(form.R_Min.data))
            d_set.append(float(form.P_Max.data))
            d_set.append(int(form.Retention_Time_Tollerance.data))
            d_set.append(float(form.Ms_P_W.data))
            d_set.append(float(form.Mass_Tol.data))
            d_set.append(float(form.Mass_Window.data))
            d_set.append(float(form.Signal_to_Noise.data))

            #source =
            #mode =

            save_datafile_DL(fileList, current_user.user_email, j_id, d_set, 'ESI', 'POSITIVE')

        #Main tables
        if chosenMethod == 'ImportDeconv' or chosenMethod == 'DeconvLibrarySearch':
            Threshold_Setting = Threshold_Setting(
                mas_w = form.Max_W.data,
                min_int = form.Min_Int.data,
                retention_window = form.Retention_Window.data,
                r_min = form.R_Min.data,
                p_max = form.P_Max.data,
                retention_time_tollerance = form.Retention_Time_Tollerance.data,
                ms_p_w = form.Ms_P_W.data,
                mass_tol = form.Mass_Tol.data,
                mass_window = form.Mass_Window.data,
                signal_to_noise = form.Signal_to_Noise.data
            )

            db.session.add(Threshold_Setting)

        acquisition_hrms = Acquisition_Hrms(hrms_mode = form.hrms_mode.data,
                    hrms_source_gas1 = form.hrms_source_gas1.data,
                    hrms_source_gas2 = form.hrms_source_gas2.data,
                    hrms_curtain_gas = form.hrms_curtain_gas.data,
                    hrms_ionisation = form.hrms_ionisation.data,
                    hrms_voltage = form.hrms_voltage.data,
                    hrms_polarity = form.hrms_polarity.data,
                    hrms_source = form.hrms_source.data)

        sample_information = Sample_Information(user_id = getattr(current_user,'id'),
                    sample_private = form.sample_private.data,
                    sample_type = form.sample_type.data,
                    sample_city = form.sample_city.data,
                    sample_job_number = j_id,
                    sample_country  = form.sample_country.data)

        analytical_column = Analytical_Column(user_id = getattr(current_user,'id'),
                    column_phase = form.column_phase.data,
                    column_praticle_size = form.column_particle_size.data,
                    column_length = form.column_length.data,
                    column_pore_size  = form.column_pore_size.data,
                    column_inner_diameter  = form.column_inner_diameter.data,
                    column_brand  = form.column_brand.data,
                    column_model  = form.column_model.data)

        db.session.add(acquisition_hrms)
        db.session.add(sample_information)
        db.session.add(analytical_column)
        db.session.commit()

        #Sub tables
        ms_system = Ms_System(acqui_hrms_id = getattr(acquisition_hrms,'id'),
                    system_class = form.hrms_system_class.data,
                    system_model = form.hrms_system_model.data,
                    system_brand = form.hrms_system_brand.data)

        sample_location = Sample_Location(sample_id = getattr(sample_information,'id'),
                    location_longitude = form.sample_location_longitude.data,
                    location_altitude = form.sample_location_altitude.data,
                    location_latitude = form.sample_location_latitude.data,
                    location_datum = form.sample_location_datum.data)

        chromatographic_condition = Chromatographic_Condition(column_id = getattr(analytical_column,'id'),
                    sample_injection_volume = form.chrom_sample_injection_volume.data,
                    mobile_phase_a = form.chrom_mobile_phase_a.data,
                    mobile_phase_b = form.chrom_mobile_phase_b.data)

        db.session.add(ms_system)
        db.session.add(sample_location)
        db.session.add(chromatographic_condition)
        db.session.commit()

        chrom_time = Chrom_Time(acqui_chrom_id = getattr(chromatographic_condition,'id'),
                    event_stage = form.chrom_event_stage.data,
                    flow_rate = form.chrom_flow_rate.data,
                    gradient_a_or_b = form.chrom_gradient_a_or_b.data,
                    a_or_b = form.chrom_a_or_b.data,
                    oven_temp = form.chrom_oven_temperature.data)

        lc_system = Lc_System(acqui_chrom_id = getattr(chromatographic_condition,'id'),
                    user_id = getattr(current_user,'id'),
                    system_brand = form.chrom_system_brand.data,
                    system_model = form.chrom_system_model.data,
                    system_chrom_class = form.chrom_system_class.data,
                    system_component_type = form.chrom_component_type.data)

        db.session.add(chrom_time)
        db.session.add(lc_system)

        db.session.commit()

        return redirect(url_for('hams.saveJob', chosenMethod = chosenMethod))
    return render_template('acquisition.html', title = "HAMS", form = form, method = chosenMethod, icon = user_icon)

@hams.route("/saveJob/<chosenMethod>", methods=['GET', 'POST'])
@login_required
def saveJob(chosenMethod):
    form = AlgorithmnForm()
    user_icon = getUserIcon()
    j_id = getJobID()
    if form.validate_on_submit():
        url = DownloadFromS3ToLocal(current_user.user_email, "12f4096091b0f8cc")
        print(url)
        return redirect(url)
        #urlopen(url)
    return render_template('saveJob.html', title = "HAMS", form = form, icon = user_icon)

@hams.route("/download/<jobid>", methods=['GET', 'POST'])
def download(jobid):
    url = DownloadFromS3ToLocal(current_user.user_email, jobid)
    print(url)
    return redirect(url)

#def redirect_delete(URL):
#    redirect(URL)
#    command = 'sudo rm -R /home/ubuntu/deco3801/application/static/data/download/' + current_user.user_email
#    subprocess.call(command, shell=True)


import sys
import pygame

def run_game():
    #initializes game and creates a screen object
    pygame.init()
    screen = pygame.display.set_mode((1200, 800))
    pygame.display.set_caption("Alien Invasion")
    bg_color = (230, 230, 230)

# Start the main loop of the game
    while True:
        #listen for keyboard and mouse events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            screen.fill(bg_color)
            pygame.display.flip()
run_game()


import textract
from django.conf import settings
from doniServer.models import ShippingPort
import pycountry
from django.contrib.auth.models import User

created_by = User.objects.get(username='immadimtiaz')

SEA_PORT_PDF = '/dataSheets/seaports.pdf'

def save_all_ports():

    text = textract.process(settings.DJANGO_ROOT+SEA_PORT_PDF, method='pdftotext' )
    text_start = text[458:]
    port_details = text_start.split('\n')
    port_details = [detail for detail in port_details if detail != '']
    port_details_length = port_details.__len__()

    counter = 0

    while counter < port_details_length:
        is_country = False
        while not is_country:
            try:
                if counter == port_details_length:
                    break
                pycountry.countries.get(name=port_details[counter])
                is_country = True
            except KeyError:
                counter = counter + 1

        item = port_details[counter:counter+6]
        if item.__len__() == 6:
            new_port = ShippingPort()
            new_port.country = item[0]
            new_port.name = item[1]
            new_port.lo_code = item[2]
            new_port.location = item[3]
            new_port.contact_no = item[4]
            new_port.website = item[5]
            new_port.created_by = created_by
            new_port.save()
        counter = counter + 6