from __future__ import division, unicode_literals, print_function, absolute_import

import time

from .. import constants
from .resource import Resource
from .messagebased import MessageBasedResource
from . import helpers as hlp

class _GPIBMixin(object):
    """Common attributes and methods of GPIB Instr and Interface.
    """

    allow_dma = hlp.boolean_attr('VI_ATTR_DMA_ALLOW_EN',
                                 doc='This attribute specifies whether I/O accesses should '
                                     'use DMA (True) or Programmed I/O (False).')

    primary_address = hlp.range_attr('VI_ATTR_GPIB_PRIMARY_ADDR', 0, 30,
                                     doc='Specifies the primary address of the GPIB device used by the given session.',
                                     ro=True)

    remote_enabled = hlp.enum_attr('VI_ATTR_GPIB_REN_STATE', constants.LineState,
                                   doc='Returns the current state of the GPIB REN (Remote ENable) interface line.',
                                   ro=True)

    secondary_address = hlp.range_attr('VI_ATTR_GPIB_SECONDARY_ADDR', 0, 65535,
                                       doc='Specifies the secondary address of the GPIB device used by the given session.',
                                       ro=True)

    def assert_trigger(self):
        """Sends a software trigger to the device.
        """

        self.visalib.assert_trigger(self.session, constants.VI_TRIG_PROT_DEFAULT)

    def send_command(self, data):
        """Write GPIB command bytes on the bus.

        Corresponds to viGpibCommand function of the VISA library.

        :param data: data tor write.
        :type data: bytes
        :return: Number of written bytes, return value of the library call.
        :rtype: int, VISAStatus
        """
        return self.visalib.gpib_command(self.session, data)

    def control_atn(self, mode):
        """Specifies the state of the ATN line and the local active controller state.

        Corresponds to viGpibControlATN function of the VISA library.

        :param mode: Specifies the state of the ATN line and optionally the local active controller state.
                     (Constants.GPIB_ATN*)
        :return: return value of the library call.
        :rtype: VISAStatus
        """
        return self.visalib.gpib_control_atn(self.session, mode)

    def control_ren(self, mode):
        """Controls the state of the GPIB Remote Enable (REN) interface line, and optionally the remote/local
        state of the device.

        Corresponds to viGpibControlREN function of the VISA library.

        :param mode: Specifies the state of the REN line and optionally the device remote/local state.
                     (Constants.GPIB_REN*)
        :return: return value of the library call.
        :rtype: VISAStatus
        """
        return self.visalib.gpib_control_ren(self.session, mode)

    def pass_control(self, primary_address, secondary_address):
        """Tell the GPIB device at the specified address to become controller in charge (CIC).

        Corresponds to viGpibPassControl function of the VISA library.

        :param primary_address: Primary address of the GPIB device to which you want to pass control.
        :param secondary_address: Secondary address of the targeted GPIB device.
                                  If the targeted device does not have a secondary address,
                                  this parameter should contain the value Constants.NO_SEC_ADDR.
        :return: return value of the library call.
        :rtype: VISAStatus
        """
        return self.visalib.gpib_pass_control(self.session, primary_address, secondary_address)

    def send_ifc(self):
        """Pulse the interface clear line (IFC) for at least 100 microseconds.

        Corresponds to viGpibSendIFC function of the VISA library.

        :return: return value of the library call.
        :rtype: VISAStatus
        """
        return self.visalib.gpib_send_ifc(self.session)

@Resource.register(constants.InterfaceType.gpib, 'INSTR')
class GPIBInstrument(_GPIBMixin, MessageBasedResource):
    """Communicates with to devices of type GPIB::<primary address>[::INSTR]

    More complex resource names can be specified with the following grammar:
        GPIB[board]::primary address[::secondary address][::INSTR]

    Do not instantiate directly, use :meth:`pyvisa.highlevel.ResourceManager.open_resource`.
    """

    def before_close(self):
        self.__switch_events_off()
        super(GPIBInstrument, self).before_close()

    def __switch_events_off(self):
        self.visalib.disable_event(self.session, constants.VI_ALL_ENABLED_EVENTS, constants.VI_ALL_MECH)
        self.visalib.discard_events(self.session, constants.VI_ALL_ENABLED_EVENTS, constants.VI_ALL_MECH)

    enable_repeat_addressing = hlp.boolean_attr('VI_ATTR_GPIB_READDR_EN',
                                                doc='Specifies whether to use repeat addressing '
                                                    'before each read or write operation.')

    enable_unaddressing = hlp.boolean_attr('VI_ATTR_GPIB_UNADDR_EN',
                                           doc='Specifies whether to unaddress the device (UNT and UNL) '
                                               'after each read or write operation.')

    def wait_for_srq(self, timeout=25000):
        """Wait for a serial request (SRQ) coming from the instrument.

        Note that this method is not ended when *another* instrument signals an
        SRQ, only *this* instrument.

        :param timeout: the maximum waiting time in milliseconds.
                        Defaul: 25000 (seconds).
                        None means waiting forever if necessary.
        """
        lib = self.visalib

        lib.enable_event(self.session, constants.VI_EVENT_SERVICE_REQ, constants.VI_QUEUE)

        if timeout and not(0 <= timeout <= 4294967295):
            raise ValueError("timeout value is invalid")

        starting_time = time.clock()

        while True:
            if timeout is None:
                adjusted_timeout = constants.VI_TMO_INFINITE
            else:
                adjusted_timeout = int((starting_time + timeout - time.clock()))
                if adjusted_timeout < 0:
                    adjusted_timeout = 0

            event_type, context, error = lib.wait_on_event(self.session, constants.VI_EVENT_SERVICE_REQ,
                                                           adjusted_timeout)
            lib.close(context)
            if self.stb & 0x40:
                break

        lib.discard_events(self.session, constants.VI_EVENT_SERVICE_REQ, constants.VI_QUEUE)

@Resource.register(constants.InterfaceType.gpib, 'INTFC')
class GPIBInterface(_GPIBMixin, Resource):
    """Communicates with to devices of type GPIB::INTFC

    More complex resource names can be specified with the following grammar:
        GPIB[board]::INTFC

    Do not instantiate directly, use :meth:`pyvisa.highlevel.ResourceManager.open_resource`.
    """

    address_state = hlp.enum_attr('VI_ATTR_GPIB_ADDR_STATE', constants.AddressState,
                                  'Shows whether the specified GPIB interface is currently addressed to talk or listen, or is not addressed.',
                                  ro=True)

    atn_state = hlp.enum_attr('VI_ATTR_GPIB_ATN_STATE', constants.LineState,
                              'Shows the current state of the GPIB ATN (ATtentioN) interface line.',
                              ro=True)

    is_controller_in_charge = hlp.boolean_attr('VI_ATTR_GPIB_CIC_STATE',
                                               'Shows whether the specified GPIB interface is currently CIC (Controller In Charge).',
                                               ro=True)

    is_system_controller = hlp.boolean_attr('VI_ATTR_GPIB_SYS_CNTRL_STATE',
                                            'Shows whether the specified GPIB interface is currently the system controller.\n\n'
                                            'In some implementations, this attribute may be modified only through a configuration utility.',
                                            ro=True)
    ndac_state = hlp.enum_attr('VI_ATTR_GPIB_NDAC_STATE', constants.LineState,
                               'Shows the current state of the GPIB NDAC (Not Data ACcepted) interface line.',
                               ro=True)

    def group_execute_trigger(self, *resources):

        for resource in resources:
            if not isinstance(resource, GPIBInstrument):
                raise ValueError('%r is not a GPIBInstrument', resource)

            # TODO: check that all resources are in the same board.

        if not self.is_controller_in_charge:
            self.send_ifc()

        command = [0x40, 0x20+31, ] # broadcast TAD#0 and "UNL" (don't listen) to all devices

        for resource in resources:
            # tell device GPIB::11 to listen
            command.append(0x20 + resource.primary_address)

        # send GET ('group execute trigger')
        command.append(0x08)

        return self.send_command(bytes(command))


import argparse
import random
import os

from PIL import Image
from tqdm import tqdm

SIZE = 128

parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', default='../data/dataset', help='Directory with the currency dataset')
parser.add_argument('--output_dir', default='../data/224x224_currency', help='where to write the new dataset')

def resize_and_save(filename, output_dir, size=SIZE):
    """Resize the image contianed in `filename` and save it to the `output_dir`"""
    image = Image.open(filename)
    # use bilinear interpolation instead of the default "nearest neighbor" method
    image = image.resize((size, size), Image.BILINEAR)
    image.save(os.path.join(output_dir, filename.split('/')[-1]))

if __name__ == '__main__':
    args = parser.parse_args()

    assert os.path.isdir(args.data_dir), "Couldn't find the dataset at {}".format(args.data_dir)

    # define the data directories
    train_data_dir = os.path.join(args.data_dir, 'train_dir')
    dev_data_dir = os.path.join(args.data_dir, 'dev_dir')
    test_data_dir = os.path.join(args.data_dir, 'test_dir')

    # Get the filenames in dataset directory
    filenames = os.listdir(args.data_dir)
    filenames = [os.path.join(args.data_dir, f) for f in filenames if f.endswith('.jpg')]

    # split the images in 'currenct' into 80% train, 10% dev 10% test
    # make sure to always shuffle with a fixed seed so that spllit is reproducible
    random.seed(230)
    filenames.sort()
    random.shuffle(filenames)

    split_1 = int(0.8 * len(filenames))
    split_2 = int(0.9 * len(filenames))
    train_filenames = filenames[:split_1]
    dev_filenames = filenames[split_1: split_2]
    test_filenames = filenames[split_2:]

    filenames = {'train': train_filenames,
                 'dev': dev_filenames,
                 'test': test_filenames}

    if not os.path.exists(args.output_dir):
        os.mkdir(args.output_dir)
    else:
        print('Warning: output dir {} already exists'.format(args.output_dir))

    # Preprocess train, dev and test
    for split in ['train', 'dev', 'test']:
        output_dir_split = os.path.join(args.output_dir, '{}_dir'.format(split))
        if not os.path.exists(output_dir_split):
            os.mkdir(output_dir_split)
        else:
            print('Warning: dir {} already exits'.format(output_dir_split))

        print('Processing {} data, saving preprocessed data to {}'.format(split, output_dir_split))
        for filename in tqdm(filenames[split]):
            resize_and_save(filename, output_dir_split, size=SIZE)

    print("Done building dataset")


from typing import Optional

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

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        """
        Determines if a singly-linked list contains a cycle.
        Uses the fast and slow pointers technique.

        O(n) time, O(1) space.
        """
        if not head: return False
        p1 = head
        p2 = head
        while p2.next and p2.next.next:
            p1 = p1.next
            p2 = p2.next.next
            if p1 is p2: return True

        return False


testcase = int(input())

checkList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
remember = []
for i in range(testcase):
    n =list(input().split())
    location100 = n[0]//100
    location10  = n[0]%100//10
    location1 = n[0]%10

    if n[1] == 3 and n[2] == 0:
        print(1)
        break

    elif n[1] == 0 and n[2] == 3:
        for j in range(6):
            if j == location1 or j == location10 or j == location100:
                continue
            del checkList[j]

    elif n[1] == 2 and n[2] == 0:
        remember.append(location100)
        remember.append(location10)
        remember.append(location1)

    elif n[1] == 0 and n[2] == 0:
        for j in range(3):
            del checkList[location1]
            del checkList[location10]
            del checkList[location100]


from collections import namedtuple

heart_rate_measurement_characteristic_id = '00002a37-0000-1000-8000-00805f9b34fb'

HeartRateMeasurement = namedtuple('HeartRateMeasurement', ['sensor_contact', 'bpm', 'rr_interval', 'energy_expended'])

def _parse_hr_measurement(data):
    flags = data[0]

    is_uint16_measurement_mask = 0x01
    is_contact_detected_mask = 0x06
    is_energy_expended_present_mask = 0x08
    is_rr_interval_present_mask = 0x10

    sensor_contact = None
    bpm = None
    rr_interval = []
    energy_expended = None

    measurement_byte_offset = 1
    sensor_contact = bool(flags & is_contact_detected_mask)

    if flags & is_uint16_measurement_mask:
        bpm = int.from_bytes(data[measurement_byte_offset:measurement_byte_offset + 2], 'little')
        measurement_byte_offset += 2
    else:
        bpm = data[measurement_byte_offset]
        measurement_byte_offset += 1

    if flags & is_energy_expended_present_mask:
        energy_expended = int.from_bytes(data[measurement_byte_offset:measurement_byte_offset + 2], 'little')
        measurement_byte_offset += 2

    if flags & is_rr_interval_present_mask:
        while len(data[measurement_byte_offset:]) >= 2:
            rr_interval.append(int.from_bytes(data[measurement_byte_offset:measurement_byte_offset + 2], 'little'))
            measurement_byte_offset += 2

    return HeartRateMeasurement(sensor_contact=sensor_contact,
                                bpm=bpm,
                                rr_interval=rr_interval,
                                energy_expended=energy_expended)

class HeartRateService:
    def __init__(self, client):
        self._client = client
        self._hr_measurement_callback = None

    async def enable_hr_measurement_notifications(self):
        await self._client.start_notify(heart_rate_measurement_characteristic_id,
                                        self._hr_measurement_notification_handler)

    async def disable_hr_measurement_notifications(self):
        await self._client.stop_notify(heart_rate_measurement_characteristic_id)

    def set_hr_measurement_handler(self, callback):
        self._hr_measurement_callback = callback

    def _hr_measurement_notification_handler(self, sender, data):  # pylint: disable=unused-argument
        if self._hr_measurement_callback is not None:
            self._hr_measurement_callback(_parse_hr_measurement(data))


import torch
import torch.nn as nn
import torch.nn.functional as func

from tools import GesdSim

class Simler(nn.Module):

    def __init__(self, negative_size, threshold):
        super(Simler, self).__init__()
        self.similarity = GesdSim(gamma=1.0, c=1.0)
        self.negative_size = negative_size
        self.threshold = threshold
        self.pool = torch.max

    def forward(self, q, pa, na):
        if self.training:
            return self._train(q, pa, na)
        else:
            return self._evaluate(q, pa, na)

    def _train(self, q, pa, na):
        pa_sim = self.similarity(q, pa)
        if not hasattr(torch.Tensor, 'repeat_interleave'):
            na_sim = torch.zeros(q.shape[0])
            if torch.cuda.is_available():
                na_sim = na_sim.cuda()
            for i in range(q.shape[0]):
                temp = na[i * self.negative_size: (i + 1) * self.negative_size]
                temp = self.similarity(q[i].repeat(self.negative_size, 1), temp)
                na_sim[i] = temp.max()
                del temp
        else:
            q_repeat = q.repeat_interleave(self.negative_size, 0)
            na_sim = self.similarity(q_repeat, na).view((-1, self.negative_size))
            na_sim = torch.max(na_sim, 1)[0]
        if torch.cuda.is_available():
            loss = (torch.ones(q.shape[0]) * self.threshold).cuda() - pa_sim + na_sim
        else:
            loss = torch.ones(q.shape[0]) * self.threshold - pa_sim + na_sim
        return loss.mean(), (pa_sim.mean() - na_sim.mean()) / 2

    def _evaluate(self, q, pa, na):
        loss, _ = self._train(q, pa, na)
        accu = 0
        pa_sim = self.similarity(q, pa)
        for i in range(q.shape[0]):
            temp = na[i * self.negative_size: (i + 1) * self.negative_size]
            temp_sim = self.similarity(q[i].repeat(self.negative_size, 1), temp)
            temp_rank = torch.zeros_like(temp_sim)
            temp_rank[temp_sim > pa_sim[i]] = 1
            accu += 1 / (temp_rank.sum().item() + 1)
        return loss.item(), accu / q.shape[0]


__author__ = 'Vas Vasiliadis <vas@uchicago.edu>'

import sys
import time
import boto3
#import boto
import driver
from datetime import datetime
import subprocess
from botocore.exceptions import ClientError
from boto3.dynamodb.conditions import Key, Attr
import os
import json
from botocore.exceptions import ParamValidationError
# Get util configuration
from configparser import SafeConfigParser
config = SafeConfigParser(os.environ)
config.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'ann_config.ini'))

"""A rudimentary timer for coarse-grained profiling
"""
class Timer(object):
  def __init__(self, verbose=True):
    self.verbose = verbose

  def __enter__(self):
    self.start = time.time()
    return self

  def __exit__(self, *args):
    self.end = time.time()
    self.secs = self.end - self.start
    if self.verbose:
      print(f"Approximate runtime: {self.secs:.2f} seconds")

if __name__ == '__main__':

  if len(sys.argv) > 1:

    with Timer():
      driver.run(sys.argv[1], 'vcf')

    s3 = boto3.resource('s3')
    BUCKET = config['aws']['ResultsBucket']

    try:
      prec_file = sys.argv[1].split('~')[0]
      act_file = sys.argv[1].split('~')[1]
      act_file2 = act_file.split('.')[0]
      annot_file = prec_file+"~"+act_file2 +".annot.vcf"
      annot_filename = act_file2+".annot.vcf"
      log_file = prec_file+"~"+act_file2+".vcf.count.log"
      log_filename = act_file2+".vcf.count.log"
    except:
      print("The given argument did not have the file name.")
      raise

    #Update job status, completion time, the results bucket, results files to DynamoDB
    dynamodb = boto3.resource('dynamodb', region_name=config['aws']['AwsRegionName'])
    ann_table = dynamodb.Table(config['aws']['DynamoTable'])

    #Source - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.04.html
    try:
      response = ann_table.query(
        KeyConditionExpression=Key('job_id').eq(prec_file)
      )
    except ClientError as e:
      print(e.response)
      raise

    for i in response['Items']:
      user_id = i['user_id']

    #After completing the annotation job, upload the log and annotation file to the gas-results s3 bucket
    try:
      key = config['aws']['Key'] + "/" + user_id + "/"
      s3.Bucket(BUCKET).upload_file(annot_file,key+annot_file)
      s3.Bucket(BUCKET).upload_file(log_file,key+log_file)
    except ClientError as e:
      print(e.response)
      raise

    #Remove all the corresponding files in the local folder
    try:
      annotation_path = "/home/ubuntu/ann/"+ annot_file
      log_path = "/home/ubuntu/ann/"+ log_file
      sys_path = "/home/ubuntu/ann/" + sys.argv[1]
      os.remove(annot_file)
      os.remove(log_file)
      os.remove(sys.argv[1])
    except OSError:
      print("No such files in the system")
      raise

    #Get completion time for the job
    t = time.localtime()
    now = datetime.now()
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    pattern = '%d/%m/%Y %H:%M:%S'
    #Source - https://stackoverflow.com/questions/7241170/how-to-convert-current-date-to-epoch-timestamp
    epoch = int(time.mktime(time.strptime(dt_string, pattern)))

    #Source - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.04.html
    try:
      response = ann_table.query(
        KeyConditionExpression=Key('job_id').eq(prec_file)
      )
    except ClientError as e:
      print(e.response)
      raise

    for i in response['Items']:
      user_id = i['user_id']

    #Set the relevant parameters in DynamoDB after job completion
    try:
      #source - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.03.html
      ann_table.update_item(
          Key = {'job_id': prec_file},
          UpdateExpression = 'set job_status = :j, s3_results_bucket = :rb, s3_key_results_file = :rf, s3_key_log_file = :lf, complete_time = :ct',
          ExpressionAttributeValues = {
              ':j' : "COMPLETED",
              ':rb' : config['aws']['ResultsBucket'],
              ':rf' : config['aws']['Key'] + "/" + user_id+"/"+annot_file,
              ':lf' : config['aws']['Key'] + "/" + user_id+"/"+log_file,
              ':ct' : epoch,
          },
          ReturnValues = "UPDATED_NEW"

      )
    except ClientError as e:
        #source - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.03.html
        if e.response['Error']['Code'] == "ConditionalCheckFailedException":
            print(e.response['Error']['Message'])
        else:
            raise

    #Open file to get email
    try:
      f2 = open(prec_file, "r")
      email = f2.read()
    except OSError:
      print("No such files in the system")
      raise

    #Remove the email file from the system
    #Source - https://www.geeksforgeeks.org/python-os-remove-method/#:~:text=os.,be%20raised%20by%20the%20method.
    try:
        os.remove(prec_file)
    except OSError:
        print("No such files in the system")
        raise

    data = { 'job_id': prec_file, 'user_id': user_id,
    # "s3_input_bucket": config['aws']['InputsBucket'],
    "s3_results_bucket": config['aws']['ResultsBucket'],
    "s3_key_results_file" : config['aws']['Key'] + "/" + user_id+"/"+annot_file,
    "s3_key_log_file" : config['aws']['Key'] + "/" + user_id+"/"+log_file,
    "complete_time": epoch,
    "email": email,
    "job_status": "COMPLETED"
    }
    #Send a notification to results arn (notify.py), so a confirmation email can be sent
    sns = boto3.client('sns', region_name = config['aws']['AwsRegionName'])
    try:
        response  = sns.publish(

            TopicArn = config['aws']['ResultsArn'],
            #Source - https://stackoverflow.com/questions/34029251/aws-publish-sns-message-for-lambda-function-via-boto3-python2
            Message = json.dumps({'default': json.dumps(data)}),
            MessageStructure='json',
        )
    except (ClientError, ParamValidationError) as e:
        print(e.response)
        raise

  else:
    print("A valid .vcf file must be provided as input to this program.")

### EOF


class Node:
  def __init__(self, data):
    self.data = data
    self.next = None

class LinkedList:
  def __init__(self, value):
    node = Node(value)
    self.head = node
    self.tail = node
    self.size = 1

  def add(self, value):
    node = Node(value)
    self.tail.next = node
    self.tail = node
    self.size += 1

  def unshift(self):
    self.head = self.head.next
    self.size -= 1

  def delete(self, value):
    if self.head.data == value:
      return self.unshift()
    node = self.head
    while node:
      if node.next:
        if node.next.data == value:
          nextOfNext = node.next.next
          if nextOfNext:
            node.next = nextOfNext
          else:
            node.next = None
            self.tail = node
          self.size -= 1
        return
      else:
        node = node.next

  def reverse(self):
    first = self.head
    second = self.head.next
    while first and second:
      tmp = second.next
      second.next = first
      first = second
      second = tmp
    self.head.next = None
    self.tail = self.head
    self.head = first

  def print(self):
    node = self.head
    while node:
      print('Data: %s Next: %s ' % (node.data, (node.next.data if node.next else None)))
      node = node.next
    print('Head: %s Tail: %s' % (self.head.data, self.tail.data))

linkedList = LinkedList(1)
linkedList.add(2)
linkedList.add(3)
linkedList.add(4)

linkedList.reverse()

linkedList.print()


import sys
import numpy as np

from bayes_opt import BayesianOptimization
from bayes_opt.util import UtilityFunction, Colours

from tools import write2tree2 as w
from tools.getdata import get_position, get_residuals
from tools.fitness import xyz_fit,residuals_fit

def black_box_function(wetz,dryz,Iu,Uu,PIu,PUu,Psigma):
    path='/home/WVU-AD/jgross2/optimizer/data'
    params=[wetz,dryz,Iu,Uu,PIu,PUu,Psigma]
    #write parameters to tree and check rtgx ran succesfully
    flag,path2run = w.write2tree(params, path)

    if flag != True:
        sys.exit() #if rtgx fails stop this program

    #Extract data
    x,y,z,Time = get_position(solution_idx=None,generation=None,path=path2run)
    phase, TimeP, range, TimeR = get_residuals(solution_idx=None,generation=None,path=path2run)

    #Get fitness from this data
    xyzfitness=xyz_fit(x,y,z,Time)
    residualfitness=residuals_fit(phase, TimeP, range, TimeR )

    fitness=xyzfitness+residualfitness

    return fitness
# wetz,dryz,Iu,Uu,PIu,PUu,Psigma
pbounds = { 'dryz': (1.5, 2.5),'wetz': (0.1, 0.25),'Iu':(0.1,1),'Uu': (0.01,0.3),'PIu':(5,1000),'PUu':(1,500),'Psigma':(0.01,0.3)}

optimizer = BayesianOptimization(
    f=black_box_function,
    pbounds=pbounds,
    random_state=1,
)

optimizer.maximize(n_iter=30)


import numpy as np
import pandas as pd
import scipy.sparse as sp
import math
from sklearn.datasets import fetch_rcv1

def freq(x, prob=True):
    return np.unique(x), x.value_counts(prob)

def freq2(x, y, prob=True):
    df = pd.concat([x, y], axis=1)
    return np.unique(x), np.unique(y), df.value_counts(normalize=prob)

def entropy(x):
    suma = 0
    _, values = freq(x)
    for i in values:
        suma += i * math.log(i, 2)
    return -suma

def info_gain(x, y):
    suma = 0
    _, _, z = freq2(x, y)
    for i in z:
        suma += i * math.log(i, 2)
    return entropy(x) + entropy(y) - -suma

# Zad 5
def convert_to_sparse_pandas(DataFrame, exclude_columns=[]):
    DataFrame = DataFrame.copy()
    exclude_columns = set(exclude_columns)
    for (columnName, columnData) in DataFrame.iteritems():
        if columnName not in exclude_columns:
            DataFrame[columnName] = pd.SparseArray(columnData.values, fill_value=0, dtype='uint8')

    return DataFrame

if __name__ == '__main__':
    x = pd.read_csv("zoo.csv")
    print(freq(x['legs']))
    print(freq(x['legs'], prob=False))
    print("\n", freq2(x['legs'], x['type'], prob=True))
    print("\n", freq2(x['legs'], x['type'], prob=False))
    print(entropy(x['type']))
    print(info_gain(x['domestic'], x['type']))
    print(x.columns)
    print("\n\n")

    # Zad 4
    x = pd.read_csv("zoo.csv")
    for i in x.columns:
        if i != 'type':
            print(i, info_gain(x[i], x['type']))
    print("\n\n")

    # Zad 5
    data, numbers = freq(x['legs'])
    data_one_hot = pd.get_dummies(x, columns=x.columns)
    print(data_one_hot.head(), data_one_hot.dtypes)
    data_one_hot_sparse = convert_to_sparse_pandas(data_one_hot)
    print(data_one_hot_sparse.head(), data_one_hot_sparse.dtypes)
    print("\n\n")
    print(freq(data_one_hot_sparse['legs_4']))
    print(freq2(data_one_hot_sparse['eggs_True'], data_one_hot_sparse['type_bird'], prob=True))

    # Zad 6
    rcv1 = fetch_rcv1()
    df = pd.DataFrame(rcv1.data, columns=rcv1.target_names)
    print(df.head())


import json
import random as r
import tkinter as tk

#iniciacion de la ventana
#configuracion de ventana     vvvvvvvvvv
ventana=tk.Tk()
ventana.title('capricho de lucas')
#anchoXalto
ventana.geometry('380x300')
ventana.configure(background='dark turquoise')

#declaracion de la funcion de la ventana:
def interfaz():
    print('hola')

#configuracion de ventana     ^^^^^^^^^^
interfaz()

with open("datos.json") as contenido:
    datos = json.loads(contenido.read())

'''----------INICIO DEL PROGRAMA----------'''

juego=input('Queres jugar? ')

if(juego == 'si'):
    '''----------JUEGOOO----------'''
    print('preguntas de lucas\n')#titulo

    contador=len(datos['quest'])#cuanta la cantidad de datos en el array
    p = r.randint(0,contador-1)#selecciona un valor aleatorio dentro del rango
    print(p)#imprime p
    print(datos["quest"][p],'\n')#imprime la pregunta
    resp = input('Verdadero o Falso (V o F): ')#pide ingreso de datos
    while(True):
        if(resp.upper() == 'V'):
            respBool = True
            break
        elif(resp.upper() == 'F'):
            respBool = False
            break
        else:
            print('hubo un error...\nReingrese la respuesta')
            continue

    if(respBool == datos['answ'][p]):
        print('Su respuesta es correcta!\n')
    else:
        print('Su respuesta es incorrecta!\n')

    print('Nos vemos en media hora....\n')

else:
    crear = input('queres agregar una nueva pregunta? ')
    '''----------CREACION DE PREGUNTASSSS----------'''
    if(crear == 'si'):
        '''----PREGUNTAS----'''
        preg = datos['quest']
        preg.append(input('Ingresa la nueva afirmacion: '))
        print(preg)

        '''----RESPUESTAS----'''
        resp = datos['answ']
        valor=input('Si o No?: ')
        if(valor.upper()=='SI'):
            resp.append(True)
        elif(valor.upper()=='NO'):
            resp.append(False)
        print(resp)

        '''----ACTUALIZACION DE DATOS----'''
        datos.update(quest = preg, answ = resp)
        print(datos)

        with open("datos.json",'w') as contenido:
            json.dump(datos, contenido)
    else:
        '''----------ERROR----------'''
        print('La app se va a cerrar...')

#cierre de la ventana...
ventana.mainloop()


import os
import sys
import csv
import numpy as np
import pandas as pd
import scipy
import sklearn
from sklearn.model_selection import KFold
import matplotlib
import matplotlib.pyplot as plt
from PIL import Image
import gc
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch import Tensor
from torch.utils.data import DataLoader, Dataset
import torchvision
import torchvision.transforms as T
import torchvision.models as models
from torchvision import transforms

class SpygrModule(nn.Module):
    def __init__(self, x, device):
        super().__init__()
        self.x = x
        self.x.to(device)
        self.M = 64
        self.device = device
        self.phi_conv = nn.Conv2d(x.shape[1], self.M, kernel_size=3, stride=1, padding=1, bias=True)
        self.glob_pool = nn.AvgPool2d((x.shape[2], x.shape[3]))
        self.glob_conv = nn.Conv2d(x.shape[1], self.M, kernel_size=1, stride=1, padding=0, bias=False)
        self.graph_weight = nn.Conv2d(self.x.shape[1], self.x.shape[1], kernel_size=1, stride=1, padding=0, bias=False)
        self.ReLu = nn.ReLU()

    def forward(self):
        x_phi_conv = self.phi_conv(self.x)
        x_phi = x_phi_conv.view([x_phi_conv.shape[0], -1, self.M])
        x_phi = self.ReLu(x_phi)
        x_phi_T = x_phi_conv.view([x_phi_conv.shape[0], self.M, -1])
        x_phi_T = self.ReLu(x_phi_T)

        x_glob_pool = self.glob_pool(self.x)
        x_glob_conv = self.glob_conv(x_glob_pool)
        x_glob_diag = torch.zeros(x_glob_conv.shape[0], x_glob_conv.shape[1], x_glob_conv.shape[1]).to(self.device)

        for i in range(x_glob_conv.shape[0]):
            x_glob_diag[i, :, :] = torch.diag(x_glob_conv[i, :, :, :].reshape(1, x_glob_conv.shape[1]))

        A_tilde = torch.matmul(torch.matmul(x_phi, x_glob_diag), x_phi_T)
        # print(A_tilde)
        D_tilde = torch.zeros_like(A_tilde).to(self.device)
        temp_sum = torch.sum(A_tilde, 2)
        for i in range(D_tilde.shape[0]):
            D_tilde[i, :, :] = torch.diag(temp_sum[i, :])
        # D_tilde = torch.diag(torch.sum(A_tilde, 2))

        # D_inv = torch.inverse(torch.sqrt(D_tilde))
        # D_inv = torch.linalg.inv(torch.sqrt(D_tilde))
        D_inv = D_tilde - A_tilde ## To Fix

        I = torch.eye(D_inv.shape[1]).to(self.device)
        I = I.repeat(D_inv.shape[0], 1, 1)

        L_tilde = I - torch.matmul(torch.matmul(D_inv, A_tilde), D_inv)

        output = torch.matmul(L_tilde, self.x.reshape(self.x.shape[0], -1, self.x.shape[1]))
        # print(output)
        output = output.reshape(self.x.shape[0], self.x.shape[1], self.x.shape[2], self.x.shape[3])
        output = self.graph_weight(output)
        output = self.ReLu(output)

        return output

class upconv(nn.Module):
    def __init__(self, in_channels, out_channels, ratio=2):
        super().__init__()
        self.elu = nn.ELU()
        self.conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, bias=False, kernel_size=3, stride=1, padding=1)
        self.ratio = ratio

    def forward(self, x):
        up_x = F.interpolate(x, scale_factor=self.ratio, mode='nearest')
        out = self.conv(up_x)
        out = self.elu(out)
        return out

class SpyGRSS(nn.Module):
    def __init__(self, device):
        super().__init__()

        self.device = device

        self.pretrained_resnet = models.resnet50(pretrained=True).to(self.device)

        for param in self.pretrained_resnet.parameters():
            param.requires_grad_(False)

        ## Layer ##
        self.reduce_dim = nn.Conv2d(2048, 512, kernel_size=3, stride=1, padding=1, bias=False)

        self.down_samp = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)

    def forward(self, x):
        x = self.pretrained_resnet.conv1(x)
        x = self.pretrained_resnet.bn1(x)
        x = self.pretrained_resnet.relu(x)
        x = self.pretrained_resnet.maxpool(x)
        x = self.pretrained_resnet.layer1(x)
        x = self.pretrained_resnet.layer2(x)
        x = self.pretrained_resnet.layer3(x)
        x = self.pretrained_resnet.layer4(x)
        x = self.reduce_dim(x)

        GR_1 = SpygrModule(x, self.device).to(self.device)

        x = self.down_samp(x)

        GR_2 = SpygrModule(x, self.device).to(self.device)

        x = self.down_samp(x)

        GR_3 = SpygrModule(x, self.device).to(self.device)

        x_gr_1 = GR_1.forward()
        x_gr_2 = GR_2.forward()
        x_gr_3 = GR_3.forward()
        output = x_gr_2 + F.interpolate(x_gr_3, scale_factor=2, mode="nearest")
        output = x_gr_1 + F.interpolate(output, scale_factor=2, mode="nearest")

        final_upsampling = upconv(512, 3, ratio=16).to(self.device)
        output = final_upsampling.forward(output)

        return output

def main():
    gc.collect()
    torch.cuda.empty_cache()
    device = torch.device("cuda")

    print(torch.cuda.is_available())
    print(torch.device('cuda:0'))

    temp = Image.open("D:/dataset/gtFine_trainvaltest/gtFine/train/aachen/aachen_000000_000019_gtFine_color.png").convert("RGB")
    temp_img = np.array(temp)
    temp_tf = T.Compose([
        T.ToTensor(),
        T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    ])
    temp_img = temp_tf(temp)
    temp_img = torch.stack([temp_img, temp_img]).to(device)

    # temp_model = SpyGRSS(device)
    temp_model = SpyGRSS(device)
    temp_model.to(device)
    temp_model.cuda()
    output = temp_model.forward(temp_img)
    # print(output.shape)
    temp_tff = T.ToPILImage()
    output = temp_tff(output[0, :, :, :])
    output.show()

    # temp_test = torch.randn(3, 3, 3)
    # temp_test.to(device)
    # print("success")

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
@File    : cdndomain_list.py
@Time    : 2023/6/30 12:01 下午
@Author  : xxlaila
@Software: PyCharm
"""

from django.db import models
from django.db.models import F
import logging
import uuid
from domains.models.domain_list import CLOUD_CHOICES

__all__ = ['CdnDomainsList']
logger = logging.getLogger(__name__)

READONLY_CHOICES = (
    ("normal", "未锁定"),
    ("mainland", "中国境内锁定"),
    ("overseas", "中国境外锁定"),
    ("global", "全球锁定")
)

AREA_CHOICES = (
    ("mainland", "中国境内加速"),
    ("overseas", "中国境外加速"),
    ("mainland_china", "中国大陆"),
    ("outside_mainland_china", "中国大陆境外"),
    ("global", "全球加速")
)

DISABLE_CHOICES = (
    ("normal", "正常状态"),
    ("overdue", "账号欠费导致域名关闭，充值完成后可自行启动加速服务"),
    ("malicious", "域名出现恶意行为，强制关闭加速服务"),
    ("ddos", "域名被大规模 DDoS 攻击，关闭加速服务"),
    ("idle", "域名超过 90 天内无任何操作、数据产生，判定为不活跃域名自动关闭加速服务，可自行启动加速服务"),
    ("unlicensed", "域名未备案 / 备案注销，自动关闭加速服务，备案完成后可自行启动加速服务"),
    ("capping", "触发配置的带宽阈值上限"),
    ("readonly", "域名存在特殊配置，被锁定"),
)

SERVICETYPE_CHOICES = (
    ("web", "静态加速"),
    ("download", "下载加速"),
    ("media", "流媒体点播加速"),
    ("hybrid", "动静加速"),
    ("dynamic", "动态加速"),
    ("video", "点播加速"),
    ("wholeSite", "全站加速")
)

STATUS_CHOICES = (
    ("rejected", "域名审核未通过，域名备案过期/被注销导致"),
    ("processing", "部署中"),
    ("configuring", "配置中"),
    ("configure_failed", "配置失败"),
    ("checking", "审核中"),
    ("check_failed", "审核未通过"),
    ("deleting", "删除中"),
    ("closing", "关闭中"),
    ("online", "已启动"),
    ("offline", "已关闭"),
)

class CdnDomainsList(models.Model):
    id = models.UUIDField(default=uuid.uuid4, primary_key=True)
    resourceid = models.CharField(null=True, blank=True, max_length=64, verbose_name="域名ID")
    domain = models.CharField(null=True, blank=True, max_length=256, db_index=True, verbose_name="加速域名")
    cname = models.CharField(null=True, blank=True, max_length=512, db_index=True, verbose_name="CNAME 地址")
    status = models.CharField(choices=STATUS_CHOICES, max_length=32, verbose_name="加速服务状态")
    servicetype = models.CharField(choices=SERVICETYPE_CHOICES, max_length=32, verbose_name="业务类型")
    origin = models.TextField(null=True, blank=True, verbose_name="源站配置详情")
    origins = models.TextField(null=True, blank=True, verbose_name="回源地址")
    disable = models.CharField(choices=DISABLE_CHOICES, max_length=32, verbose_name="封禁状态")
    area = models.CharField(choices=AREA_CHOICES, max_length=32, verbose_name="加速区域")
    readonly = models.CharField(choices=READONLY_CHOICES, max_length=32, verbose_name="锁定状态")
    product = models.CharField(null=True, blank=True, max_length=16, verbose_name="所属产品")
    parentHost = models.CharField(null=True, blank=True, max_length=128, verbose_name="主域名")
    cloud = models.CharField(choices=CLOUD_CHOICES, db_index=True, max_length=32, verbose_name='云')
    create_time = models.DateTimeField(auto_now_add=True, null=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, null=True, verbose_name="更新时间")

    class Meta:
        ordering = ['-domain']
        verbose_name = "CdnDomainsList"
        verbose_name_plural = "CdnDomainsList"


import sys

N = sys.stdin.readline().rstrip()  # N 입력, N은 구하고자 하는 답의 후보들의 시작값 (13,130,1300)

arrN = list(
    map(int, list(set(list(N.replace('0', '').replace('1', ''))))))
# 0, 1을 없앤 이유는 0으로는 나눗셈이 불가능하고, 1은 무조건 나눠 떨어지기 때문에 불필요한 작업을 없앴다.
# set을 사용해 리스트의 중복을 제거함으로써 불필요한 작업 제거

location = 0  # N에 추가될 자리수
while True:  # 조건이 충족될떄까지 반복
    tmp = int(N + '0' * location)  # N의 자리수를 하나씩 증가 (13,130,1300)

    # 만약 단순히 str(N) in str(tmp)이라고 하면 tmp의 시작위치가 아닌 다른 위치에 포함되어있어도
    # 조건이 참이 되므로 범위를 꼭 정해줘야 한다.
    while str(N) == str(tmp)[:len(str(N))]:  # N이 tmp의 포함되어 있을 경우
        flag = True  # 조건 판단
        for n in arrN:  # 나눌수 : N의 각 자리수
            if tmp % n == 0:  # tmp를 계속 나눔
                continue
            else: #하나라도 나눠떨어지지 않을 경우
                flag = False
                break

        if flag == True:  # 조건에 모두 만족할 경우
            print(tmp)
            exit() #종료
        else:
            tmp += 1 #tmp값 증가
    location += 1 #자리수 증가


import logging
from datetime import datetime

import pandas as pd
from django.utils import timezone

from core.models import Project
from core.models.url import UrlManager
from core.models.website import WebsiteManager
from exports.googleanalytics4_last_14m_export import GoogleAnalytics4Last14mExport
from exports.googlesearchconsole_page_last_16m_export import GoogleSearchConsolePageLast16mExport
from exports.screamingfrog_list_crawl_export import ScreamingFrogListCrawlExport
from exports.screamingfrog_sitemap_crawl_export import ScreamingFrogSitemapCrawlExport
from exports.screamingfrog_spider_crawl_export import ScreamingFrogSpiderCrawlExport
from exports.semrush_analytics_backlinks_rootdomain_export import SemrushAnalyticsBacklinksRootdomainExport
from exports.semrush_analytics_organic_pages_export import SemrushAnalyticsOrganicPagesExport
from exports.sitebulb_list_crawl_url_internal_export import SitebulbListCrawlUrlInternalExport

logger = logging.getLogger(__name__)

class UrlDataProcessor:
    def __init__(self, project: Project):
        self._project = project

        self._data = None

        self.screamingfrog_list_crawl_data = None
        self.screamingfrog_spider_crawl_data = None
        self.screamingfrog_sitemap_crawl_data = None
        self.googleanalytics4_last_14m_data = None
        self.googlesearchconsole_page_last_16m_data = None
        self.semrush_analytics_organic_pages_data = None
        self.semrush_analytics_backlinks_rootdomain_data = None
        self.sitebulb_list_crawl_url_internal_data = None

    def run(self):
        self.collect_data()
        self.process_data()
        self.store_data()

    def collect_data(self):
        screamingfrog_list_crawl_export = ScreamingFrogListCrawlExport(self._project)
        self.screamingfrog_list_crawl_data = screamingfrog_list_crawl_export.get_data()

        screamingfrog_spider_crawl_export = ScreamingFrogSpiderCrawlExport(self._project)
        self.screamingfrog_spider_crawl_data = screamingfrog_spider_crawl_export.get_data()

        screamingfrog_sitemap_crawl_export = ScreamingFrogSitemapCrawlExport(self._project)
        self.screamingfrog_sitemap_crawl_data = screamingfrog_sitemap_crawl_export.get_data()

        googleanalytics4_last_14m_export = GoogleAnalytics4Last14mExport(self._project)
        self.googleanalytics4_last_14m_data = googleanalytics4_last_14m_export.get_data()

        googlesearchconsole_page_last_16m_export = GoogleSearchConsolePageLast16mExport(self._project)
        self.googlesearchconsole_page_last_16m_data = googlesearchconsole_page_last_16m_export.get_data()

        semrush_analytics_organic_pages_export = SemrushAnalyticsOrganicPagesExport(self._project)
        self.semrush_analytics_organic_pages_data = semrush_analytics_organic_pages_export.get_data()

        semrush_analytics_backlinks_rootdomain_export = SemrushAnalyticsBacklinksRootdomainExport(self._project)
        self.semrush_analytics_backlinks_rootdomain_data = semrush_analytics_backlinks_rootdomain_export.get_data()

        sitebulb_list_crawl_url_internal_export = SitebulbListCrawlUrlInternalExport(self._project)
        self.sitebulb_list_crawl_url_internal_data = sitebulb_list_crawl_url_internal_export.get_data()

    def process_data(self):
        screamingfrog_list_crawl_data_columns = [
            "Address",
            "Status Code",
            "Redirect URL",
            "Content Type",
            "Indexability",
            "Indexability Status",
            "Title 1",
            "Meta Description 1",
            "H1-1",
            "Canonical Link Element 1",
            "Word Count",
            "Readability",
            "Unique Inlinks",
            "Unique Outlinks",
            "Hash",
            "Crawl Timestamp",
        ]
        df = self.screamingfrog_list_crawl_data[screamingfrog_list_crawl_data_columns].drop_duplicates(subset="Address", keep="first")

        # calculate relative url column
        df["Path"] = df["Address"].apply(UrlManager.parse_relative_url)

        # join spider crawl data
        self.screamingfrog_spider_crawl_data["In Crawl"] = True
        screamingfrog_spider_crawl_data_columns = [
            "Address",
            "Crawl Depth",
            "In Crawl",
        ]
        df = pd.merge(
            df,
            self.screamingfrog_spider_crawl_data[screamingfrog_spider_crawl_data_columns].drop_duplicates(subset="Address", keep="first"),
            left_on="Address",
            right_on="Address",
            how="left",
        )
        df["In Crawl"].fillna(False, inplace=True)

        # join sitemap crawl data
        self.screamingfrog_sitemap_crawl_data["In Sitemap"] = True
        screamingfrog_sitemap_crawl_data_columns = [
            "Address",
            "In Sitemap",
        ]
        df = pd.merge(
            df,
            self.screamingfrog_sitemap_crawl_data[screamingfrog_sitemap_crawl_data_columns].drop_duplicates(subset="Address", keep="first"),
            left_on="Address",
            right_on="Address",
            how="left",
        )
        df["In Sitemap"].fillna(False, inplace=True)

        self.googleanalytics4_last_14m_data["In GA4"] = True
        googleanalytics4_last_14m_data_columns = [
            "pagePath",
            "In GA4",
        ]
        df = pd.merge(
            df,
            self.googleanalytics4_last_14m_data[googleanalytics4_last_14m_data_columns].drop_duplicates(subset="pagePath", keep="first"),
            left_on="Path",
            right_on="pagePath",
            how="left",
        )
        df["In GA4"].fillna(False, inplace=True)

        self.googlesearchconsole_page_last_16m_data["In GSC"] = True
        googlesearchconsole_page_last_16m_data_columns = [
            "page",
            "In GSC",
        ]
        df = pd.merge(
            df,
            self.googlesearchconsole_page_last_16m_data[googlesearchconsole_page_last_16m_data_columns].drop_duplicates(subset="page", keep="first"),
            left_on="Address",
            right_on="page",
            how="left",
        )
        df["In GSC"].fillna(False, inplace=True)

        self.semrush_analytics_organic_pages_data["In Semrush Pages"] = True
        semrush_analytics_organic_pages_data_columns = [
            "URL",
            "In Semrush Pages",
        ]
        df = pd.merge(
            df,
            self.semrush_analytics_organic_pages_data[semrush_analytics_organic_pages_data_columns].drop_duplicates(subset="URL", keep="first"),
            left_on="Address",
            right_on="URL",
            how="left",
        )
        df["In Semrush Pages"].fillna(False, inplace=True)
        df.drop("URL", axis=1, inplace=True)

        self.semrush_analytics_backlinks_rootdomain_data["In Semrush Backlinks"] = True
        semrush_analytics_backlinks_rootdomain_data_columns = [
            "Target url",
            "In Semrush Backlinks",
        ]
        df = pd.merge(
            df,
            self.semrush_analytics_backlinks_rootdomain_data[semrush_analytics_backlinks_rootdomain_data_columns].drop_duplicates(subset="Target url", keep="first"),
            left_on="Address",
            right_on="Target url",
            how="left",
        )
        df["In Semrush Backlinks"].fillna(False, inplace=True)
        df.drop("Target url", axis=1, inplace=True)

        # join sitebulb crawl data
        sitebulb_list_crawl_url_internal_data_columns = [
            "URL",
            "HTML Template",
            "No. Content Words",
            "No. Template Words",
            "No. Words",
        ]
        df = pd.merge(
            df,
            self.sitebulb_list_crawl_url_internal_data[sitebulb_list_crawl_url_internal_data_columns].drop_duplicates(subset="URL", keep="first"),
            left_on="Address",
            right_on="URL",
            how="left",
        )
        df.drop("URL", axis=1, inplace=True)

        # Generic approach to handle NaN values based on column data type
        for col in df.columns:
            if pd.api.types.is_numeric_dtype(df[col]):
                df[col].fillna(0, inplace=True)  # Replace NaN with 0 for numeric columns
            else:
                df[col].fillna("", inplace=True)  # Replace NaN with empty string for non-numeric columns

        self._data = df

    def store_data(self):
        website = WebsiteManager.get_website_by_project(self._project)
        for _, row in self._data.iterrows():
            # Extract required data from the row
            full_address = row["Address"]

            #
            # Parse the string to a naive datetime object
            naive_datetime = datetime.strptime(row["Crawl Timestamp"], "%Y-%m-%d %H:%M:%S")

            # Convert the naive datetime to a timezone-aware datetime
            aware_datetime = timezone.make_aware(naive_datetime, timezone.get_default_timezone())

            # Use UrlManager's method to push the URL to the database
            UrlManager.push_url(
                full_address=full_address,
                website=website,
                status_code=row["Status Code"],
                redirect_url=row["Redirect URL"],
                content_type=row["Content Type"],
                canonical_link_element_1=row["Canonical Link Element 1"],
                content_words_count=row["No. Content Words"],
                crawl_timestamp=aware_datetime,
                h1_1=row["H1-1"],
                hash=row["Hash"],
                crawl_depth=row["Crawl Depth"],
                html_template=row["HTML Template"],
                indexability=row["Indexability"],
                indexability_status=row["Indexability Status"],
                meta_description_1=row["Meta Description 1"],
                readability=row["Readability"],
                relative_address=row["Path"],
                template_words_count=row["No. Template Words"],
                title_1=row["Title 1"],
                unique_inlinks=row["Unique Inlinks"],
                unique_outlinks=row["Unique Outlinks"],
                word_count=row["Word Count"],
                word_count2=row["No. Words"],
                in_sitemap=row["In Sitemap"],
                in_crawl=row["In Crawl"],
                in_ga4=row["In GA4"],
                in_gsc=row["In GSC"],
                in_semrush_pages=row["In Semrush Pages"],
                in_semrush_backlinks=row["In Semrush Backlinks"],
            )
        logger.info("Data successfully processed using UrlManager.")


# 作用：获取本地的外网出口地址
import requests
import re

def getIP():
    headers = {}
    headers['User-Agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"
    response = requests.get('http://www.baidu.com/s?wd=ip', headers=headers)
    html = response.text
    # reGET = re.compile('fk="(.*?)"').findall(html)
    # reGET = re.compile('ip地址(.*?) ').findall(html)
    reGET = re.compile('ip地址(.*?)。').findall(html)
    for i in reGET:
        print('外网地址： %s' % i)
        return i

ipinfo = getIP()

def save(file, res):
    with open(file, 'w', encoding='utf-8') as f:
        f.write(res)

def 时间_到文本(时间格式index=0, timetuple=None):
    '''

    :param 时间格式index:
    # 0 :  N年N月N日  示例:2020年11月20日
    # 1 :  N年N月N日N时N分N秒  示例:2020年11月20日4时25分38秒
    # 2 :  N年N月N日 N时N分N秒  示例:2020年11月20日 4时25分38秒
    # 3 :  年-月-日 时:分:秒  2020-11-20 4:25:38
    # 4 :  年/月/日 时/分/秒  2020/11/20 4/25/38
    :param timetuple:
        # 可传入 时间元组  如果不传 就默认取现行时间
    :return:
    '''
    # Done (2020年11月20日) 在版本3基础上 添加功能:时间格式 如果后续有需要 直接去list中添加对应格式
    # 时间格式index= ?
    # 0 :  N年N月N日  示例:2020年11月20日
    # 1 :  N年N月N日N时N分N秒  示例:2020年11月20日4时25分38秒
    # 2 :  N年N月N日 N时N分N秒  示例:2020年11月20日 4时25分38秒
    # 3 :  年-月-日 时:分:秒  2020-11-20 4:25:38
    # 4 :  年/月/日 时/分/秒  2020/11/20 4/25/38

    # def 时间_到文本(包含时分秒=False, timetuple=None):
    # 在版本2基础上 添加功能:支持传入 时间元组  如果不传 就默认取现行时间
    # struct_time  一般由time.localtime([secs])生成 参数secs省略就是取现行时间戳time.time()
    # struct_time 被提示重名了 改成了 timetuple

    import time
    # 检查到默认时间元组为None,那么就是取现行时间      # 如果没传入时间元组 就代表取现行时间
    if timetuple is None:
        timetuple = time.localtime()
    # time.struct_time(tm_year=2019, tm_mon=9, tm_mday=16, tm_hour=23, tm_min=45, tm_sec=13, tm_wday=0, tm_yday=259, tm_isdst=0)

    # 用*打散列表/元组等有序合集即可,需要几个参数它自己会传入个数,不会报错
    # N年N月N日 N时N分N秒  1=年-月-日 时:分:秒  2=年/月/日 时/分/秒  3=年月日时分秒
    # 时间格式index
    # 0 :  N年N月N日  示例:2020年11月20日
    # 1 :  N年N月N日N时N分N秒  示例:2020年11月20日4时25分38秒
    # 2 :  N年N月N日 N时N分N秒  示例:2020年11月20日 4时25分38秒
    # 3 :  年-月-日 时:分:秒  2020-11-20 4:25:38
    # 4 :  年/月/日 时/分/秒  2020/11/20 4/25/38
    result_list = [
        "{}年{}月{}日",
        "{}年{}月{}日{}时{}分{}秒",
        "{}年{}月{}日 {}时{}分{}秒",
        "{}-{}-{} {}:{}:{}",
        "{}/{}/{} {}/{}/{}",
    ]

    return result_list[时间格式index].format(*timetuple)

file = 时间_到文本(时间格式index=1) + ".txt"
save(file, ipinfo)


#!/usr/bin/python3
"""5-island_perimeter"""

def island_perimeter(grid):
    """perimeter of the island described in grid"""
    e = 0
    leng = len(grid) - 1
    wid = len(grid[0]) - 1

    for i, r in enumerate(grid):
        for j, n in enumerate(r):
            if n == 1:
                if i == 0 or grid[i - 1][j] != 1:
                    e += 1
                if j == 0 or grid[i][j - 1] != 1:
                    e += 1
                if j == wid or grid[i][j + 1] != 1:
                    e += 1
                if i == leng or grid[i + 1][j] != 1:
                    e += 1
    return e


# -*- coding: utf-8 -*-
"""
Created on Tue Apr 12 11:24:24 2022

@author: Christopher
"""
import numpy as np
import matplotlib.pyplot as plt

def defineCircle(p1, p2, p3):
    """
    Returns the center and radius of the circle passing the given 3 points.
    In case the 3 points form a line, returns (None, infinity).
    """
    temp = p2[0] * p2[0] + p2[1] * p2[1]
    bc = (p1[0] * p1[0] + p1[1] * p1[1] - temp) / 2
    cd = (temp - p3[0] * p3[0] - p3[1] * p3[1]) / 2
    det = (p1[0] - p2[0]) * (p2[1] - p3[1]) - (p2[0] - p3[0]) * (p1[1] - p2[1])

    if abs(det) < 1.0e-6:
        return (None, np.inf)

    # Center of circle
    cx = (bc*(p2[1] - p3[1]) - cd*(p1[1] - p2[1])) / det
    cy = ((p1[0] - p2[0]) * cd - (p2[0] - p3[0]) * bc) / det

    radius = np.sqrt((cx - p1[0])**2 + (cy - p1[1])**2)
    return ((cx, cy), radius)

def inCircle(cx, cy, radius, x, y):
    if pow((x - cx), 2) + pow((y - cy), 2) <= pow(radius, 2):
        return True
    return False

p = (0,0)
center, radius = defineCircle((0,1), (1,0), (0,-1))
print(inCircle(center[0], center[1], radius, p[0], p[1]))
if center is not None:
    plt.figure(figsize=(4, 4))
    plt.xlim([-1.2, 1.2])
    plt.ylim([-1.2, 1.2])
    plt.plot(p[0], p[1],'ro')
    circle = plt.Circle(center, radius, fill=False)
    plt.gca().add_patch(circle)


#!/usr/bin/env python
# coding=utf8
from setuptools import setup, find_packages
import os

if os.path.exists('requirements.txt'):
    requirements = [x.strip() for x in open("requirements.txt").readlines()]
else:
    requirements = []

# Create build meta
setup(
    name="qcaudit",
    version="{PYPI_VERSION}",
    author="{CI_COMMIT_AUTHOR}",
    url="{DRONE_REMOTE_URL}",
    packages=find_packages(),
    include_package_data=True,
    install_requires=requirements,
    description="",
    long_description=open("README.md").read(),
)


from tifffile import imread
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import (QDialog, QSizePolicy, QApplication, QTableWidget, QVBoxLayout,
                            QPushButton, QColorDialog, QTableWidgetItem, QMessageBox)
from PyQt5.QtGui import QCursor, QColor
from PyQt5.QtCore import Qt
from matplotlib.colors import LinearSegmentedColormap
import copy, re, sys
from ast import literal_eval
import subWindows as sw

def loadStacks5D(file_name, app=False):
    print('#'*40)
    print('Loading dataset at:\n\t', file_name)
    stacks = imread(file_name)
    target_id = 'TZCHW'

    # append dimensions if necessary
    if not app:
        app = QApplication(sys.argv)
    ddef = sw.DimensionDefiner(shape=stacks.shape)
    ddef.show()
    if ddef.exec_() == QDialog.Accepted:
        input_id = ddef.text
        # find the missing id
        missing_id = copy.deepcopy(target_id)
        for i in input_id:
            missing_id = missing_id.replace(i,'')
        # append dimension
        for i in range(len(target_id)-len(input_id)):
            stacks = np.expand_dims(stacks,-1)
            input_id = input_id+missing_id[i]
    else:
        return

    # reorder dimensions to match 'TZCHW'
    if input_id != target_id:
        for i in target_id:
            f = input_id.index(i)
            t = target_id.index(i)
            stacks = np.moveaxis(stacks,f,t)
            input_id = input_id.replace(i,'')
            input_id = input_id[:t]+i+input_id[t:]

    # adjust channel dimension to 2
    if stacks.shape[2]==1:
      stacks = np.concatenate([stacks,np.zeros(stacks.shape)],axis=2)

    # compute maximum values
    _maxval = np.zeros((stacks.shape[0],stacks.shape[2]))
    for i in range(stacks.shape[0]):
        for j in range(stacks.shape[2]):
            _maxval[i,j] = np.max(stacks[i,:,j,:,:])
    print('Stack shape (TZCHW):', stacks.shape)
    print('Done')
    return stacks, _maxval

def swapAxes(stacks, _maxval, ax = 0):
    stacks = np.flip( stacks, axis = ax )
    _maxval = np.flip(_maxval,axis = ax-2)
    return stacks, _maxval

def convertPoints(oldData):
    new = {'_ids': ['tether_Atrium','tether_Ventricle','AVCanal','Midline'],
                        'colors': ['#6eadd8','#ff7f0e','red','#c4c4c4'],
                        'markers': ['o','o','X','-x'],
                        'ms': [3,3,5,1],
                        'is_instance': [0,0,0,0],
                        'coords': [np.array([])]*4 }
    for key in oldData:
        i = new['_ids'].index(key)
        new['coords'][i] = oldData[key]
        if oldData[key].shape[0]>0:
            new['is_instance'][i] = 1
    return new

if __name__ == '__main__':

    f = '../test_unwrap_heart/4D_merged_small.tif'
    loadStacks5D(f)


import asyncio
from aiohttp import web
import json
import random
import os
import time
import struct
import socket
import sys

from collections import deque

wsclients = []

async def handle(request):
    indexpage = None

    with open('index.html', 'r') as indexfile:
        indexpage=indexfile.read()
    return web.Response(text=indexpage, content_type='text/html')

async def wshandler(request):
    print('web socket')
    ws = web.WebSocketResponse(protocols=['SUPERNET'])
    await ws.prepare(request)

    wsclients.append(ws)

    async for msg in ws:
        if msg.type == web.MsgType.binary:
            ws.send_bytes(msg.data)
        elif msg.type == web.MsgType.close:
            break
    return ws

async def updatestats(app):
    while True:
        await asyncio.sleep(1)

        # record how many rx/tx bytes since last check
        print('updatestats BRIDGEPPS: {}'.format(BRIDGEPPS))

        # we need to handle disconnects
        closed = []
        for client in wsclients:
            if not client.closed:
                await client.send_str(json.dumps({'internal':[]}))
            else:
                closed.append(client)
        for x in closed:
            wsclients.remove(x)

# next to go we can do this in python now
async def streamsh(app):
    command = os.path.dirname(os.path.realpath(__file__)) + '/stream.sh'

    # Create the subprocess, redirect the standard output into a pipe
    create = asyncio.create_subprocess_shell(command,
        stdout=asyncio.subprocess.PIPE)
    proc = yield create

    print('stream', proc)

    while RUNSTREAM:
        data = yield proc.stdout.readline()
    proc.terminate()

async def streampy(app):
    command = os.path.dirname(os.path.realpath(__file__)) + '/stream.sh'

    # Create the subprocess, redirect the standard output into a pipe
    create = asyncio.create_subprocess_shell(command,
        stdout=asyncio.subprocess.PIPE)
    proc = yield create

    print('stream', proc)

    while RUNSTREAM:
        data = yield proc.stdout.readline()
    proc.terminate()

def stream_cb(pad, info, pdata):
    print('called')
    return Gst.PadProbeReturn.OK

async def stream(app):
    global STREAMPPS
    Gst.init(None)

    pipeline ="flvmux name=muxer streamable=true \
! rtmpsink location='rtmp://bsdstreams-origin.secdn.net/bsdstreams-origin/live/268bf' \
autoaudiosrc \
! audioconvert \
! wavenc \
! wavparse \
! audioconvert ! audioresample \
! audio/x-raw, rate=48000 \
! avenc_aac \
! muxer. \
udpsrc port=5000 \
! tsdemux \
! queue \
! h264parse \
! muxer."

    pipe = Gst.parse_launch(pipeline)
    data = {start: time.time(), interval:1, pkts:0}

    udpsrc = pipe.get_child_by_name('udpsrc0')
    pad = udpsrc.get_static_pad('src')
    pad.add_probe(Gst.PadProbeType.DATA_BOTH, datacb, data)

    loop = app.loop

    bus = pipe.get_bus()
    bus.add_signal_watch()
    bus.connect ("message", bus_call, loop)

    # start play back and listen to events
    pipe.set_state(Gst.State.PLAYING)
    try:
        loop.run()
    except:
        pass

async def bridge(app):
    global BRIDGEPPS

    group = '239.255.42.42'
    mport = 5004

    baddr = '192.168.1.2'
    bport = 5000
    bridgetarget = (baddr, bport)

    # Create a socket
    msock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    msock.bind(('0.0.0.0', mport))
    msock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
        socket.inet_aton(group) + socket.inet_aton('192.168.1.2'))

    bsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    #bsock.connect(bridgetarget)

    start = time.time()
    count = 0
    while RUNBRIDGE:
        #try:
            data, sender = yield msock.recvfrom(1500)
            print('recv')

            done = time.time()
            if done - start > 1: # 1 second
                start = time.time()
                BRIDGEPPS = count
                count = 0
                print(BRIDGEPPS)
            else:
                count = count + 1

            if BRIDGEFORWARD:
                try:
                    bsock.sendto(data, bridgetarget)
                except socket.error as e:
                    pass
        #    # we are going to get send errors we don't care
        #    if e.errno != socket.errno.ECONNREFUSED:
        #        print(e)
        #        raise e
        #    pass

async def start_background_tasks(app):
    app['updatestats'] = app.loop.create_task(updatestats(app))

    #app['stream'] = app.loop.create_task(streamsh(app))
    #app['stream'] = app.loop.create_task(streampy(app))
    app['stream'] = app.loop.create_task(stream(app))

    app['bridge'] = app.loop.create_task(bridge(app))

async def cleanup_background_tasks(app):
    app['bridge'].cancel()
    app['stream'].cancel()

    await app['bridge']
    await app['stream']

if __name__ == "__main__":

    RUNSTREAM = True
    RUNBRIDGE = True
    BRIDGEFORWARD = True
    BRIDGEPPS = 0

    app = web.Application()

    app.router.add_static('/d3', "d3")
    app.router.add_static('/css', "css")
    app.router.add_static('/js', "js")
    app.router.add_static('/images', "images")

    app.router.add_get('/ifstatus', wshandler)
    app.router.add_get('/', handle)

    app.on_startup.append(start_background_tasks)
    app.on_cleanup.append(cleanup_background_tasks)

    try:
        print("started webapp")
        web.run_app(app)
    except KeyboardInterrupt:
        print("Received exit, exiting")

        RUNSTREAM = False
        RUNBRIDGE = False

        app.loop.close()


"""
Tools for shuffling a time interval data set,
i.e. where we know the start and end times and wish to know which months it contains.
"""
import logging
import pandas as pd

from .utilities import shuffle
from .progress_bar import ProgressBar
from .file_structure import START, END

class GetMonth:
    """
    Iterate through a data set from 'start' to 'end' by month,
    extracting values that overlap that time, and shuffle values.
    Parameters
    ----------
    start: datetime.datetime
        date stamp to start from
    end: datetime.datetime
        date time to end at.
    """
    def __init__(self, start=START, end=END):
        self.start = start
        self.end = end

    @property
    def months(self):
        """Month start date(s) between 'start' and 'end'"""
        return pd.date_range(self.start, self.end, freq="MS")

    @property
    def current_months(self):
        """Start of monthly periods"""
        return self.months[:-1]

    @property
    def next_month(self):
        """End of monthly periods"""
        return self.months[1:]

    def yield_data(self, data, start_column="Absence Start Date", end_column="Absence End Date"):
        """
        Iterate through data, looking up months bound by start_column and end_column.
        Parameters
        ----------
        data: pandas.DataFrame
            frame to look up in
        start_column: str
            the column of data that serves as month start
        end_column: str
            the column of data the serves as month end
        """
        zipped_months = zip(self.current_months, self.next_month)

        data[start_column] = pd.to_datetime(data[start_column])
        data[end_column] = pd.to_datetime(data[end_column])

        for start_of_month, end_of_month in zipped_months:
            month = data[
                (data[start_column] < end_of_month) &
                (data[end_column] >= start_of_month)
            ]
            month["Month"] = start_of_month
            yield start_of_month, month

    def monthly_shuffle(self, data, columns_to_shuffle,
                        start_column="Absence Start Date", end_column="Absence End Date"):
        """
        Take a data set, extract a month, and shuffle.
        Parameters
        ----------
        data: pandas.DataFrame
            the data set to monthly shuffle
        columns_to_shuffle: str
            the column of 'data' to shuffle
        start_column: str
            the column to treat as month start
        end_column: str
            the column to treat as month end
        """
        monthly_data = self.yield_data(data, start_column, end_column)
        for start_of_month, month in monthly_data:
            month[columns_to_shuffle] = shuffle(
                frm=month[columns_to_shuffle],
                name=start_of_month
            )

            yield month

    def shuffle_and_join(self, data, columns_to_shuffle,
                         start_column="Absence Start Date", end_column="Absence End Date"
                         ):
        """
        Iterate through each month, extract monthly data, shuffle and join into a data frame.
        Parameters
        ----------
        data: pandas.DataFrame
            the data set to monthly shuffle
        columns_to_shuffle: str
            the column of 'data' to shuffle
        start_column: str
            the column to treat as month start
        end_column: str
            the column to treat as month end

        Returns
        -------
        pandas.DataFrame
        """
        monthly_data = self.monthly_shuffle(data, columns_to_shuffle, start_column, end_column)
        return pd.concat(monthly_data)

    def grouped_monthly_shuffle(self,
                                data, groupby, columns_to_shuffle,
                                date_columns=None
                                ):
        """
        Carry out the monthly extract-shuffle with an aggregated DataFrame.
        Does the shuffle for each aggregate independently.
        Parameters
        ----------
        data: pandas.DataFrame
            the data set to monthly shuffle
        groupby: str or list
            the column(s) to aggregate on to shuffle
        columns_to_shuffle: str
            the column of 'data' to shuffle
        date_columns: tuple(str)
            the columns to treat as month start and month end
        Returns
        -------
        pandas.DataFrame
        """
        start_column, end_column = date_columns or ["Absence Start Date", "Absence End Date"]
        grps = data.groupby(groupby)
        _iter = self.progres_bar_iteration(
            grps, columns_to_shuffle,
            start_column, end_column
        )
        return pd.concat(_iter)

    def progres_bar_iteration(self,
                              groups, columns_to_shuffle,
                              start_column="Absence Start Date", end_column="Absence End Date"
                              ):
        """
        Iterate through a grouped DataFrame - doing a monthly shuffle on set columns
        with a progress bar to track where the process is.
        Parameters
        ----------
        groups: pandas.GroupedDataFrame
            a grouped pandas DataFrame
        columns_to_shuffle: str
            the column of 'data' to shuffle
        start_column: str
            the column to treat as month start
        end_column: str
            the column to treat as month end
        """
        progress = ProgressBar(20, len(groups.groups))

        logging.info("Shuffle in progress:")
        for _, frame in groups:
            progress.step()
            yield self.shuffle_and_join(frame, columns_to_shuffle, start_column, end_column)