#!/usr/bin/env python
"""
The image calibration module contains functions for calibrating optical data from DN to radience and top of atmosphere reflectance and, using coefficients from 6S, surface reflectance.

For obtaining the correct parameters for each function it is recomented that rather than running directly they are called through the Atmospheric and Radiometric Correction of Satellite Imagery (ARCSI) software.

More details on ARCSI are avaialble from https://remotesensing.info/arcsi
"""

import rsgislib

# import the C++ extension into this level
from ._imagecalibration import *

class ElevLUTFeat(object):
    """
    Create a list of these objects to pass to the rsgislib.imagecalibration.apply6SCoeffElevLUTParam

    :param Elev: is the elevation value.
    :param Coeffs: is the 6S coeffients as a Band6S object.
    """

    def __init__(self, Elev=None, Coeffs=None):
        """
        :param Elev: is the elevation value.
        :param Coeffs: is the 6S coeffients as a Band6S object.
        """
        self.Elev = Elev
        self.Coeffs = Coeffs

class AOTLUTFeat(object):
    """
    Create a list of these objects to pass to the rsgislib.imagecalibration.apply6SCoeffElevAOTLUTParam

    :param AOT: is the AOT value.
    :param Coeffs: is the 6S coeffients as a Band6S object.
    """

    def __init__(self, AOT=None, Coeffs=None):
        """
        :param AOT: is the AOT value.
        :param Coeffs: is the 6S coeffients as a Band6S object.
        """
        self.AOT = AOT
        self.Coeffs = Coeffs

class Band6SCoeff(object):
    """
    Create a list of these objects to provide the Coeffs for ElevLUTFeat and AOTLUTFeat.
    """

    def __init__(
        self,
        band=None,
        aX=None,
        bX=None,
        cX=None,
        DirIrr=None,
        DifIrr=None,
        EnvIrr=None,
    ):
        self.band = band
        self.aX = aX
        self.bX = bX
        self.cX = cX
        self.DirIrr = DirIrr
        self.DifIrr = DifIrr
        self.EnvIrr = EnvIrr

def perform_dos_calc(
    inputFile,
    outputFile,
    gdalformat="KEA",
    nonNegative=True,
    noDataVal=0,
    darkObjReflVal=0,
    darkObjPercentile=0.01,
    copyBandNames=True,
    calcStatsPyd=True,
):
    """
    A command to perform a dark object subtraction (DOS) on an input image.

    :param inputFile: input image to which the DOS method is to be applied. Typically, this image with be in top of atmosphere reflectance (TOA)
    :param outputFile: the output image file
    :param gdalformat: the output image file format (default = KEA)
    :param nonNegative: is a boolean specifying where negative output pixel values will be accepted (Dafualt is True; i.e., no negative values)
    :param noDataVal: is the no data value within the input image file.
    :param darkObjReflVal: is an offset which is applied to all pixel values to make a minimum reflectance value (Default = 0)
    :param darkObjPercentile: is the percentile of the input image used to define the dark object threshold, range is 0 - 1 (Default is 0.01; i.e., 1%).
    :param copyBandNames: is a boolean specifying that the band names of the input image should be copied to the output image file (Default: True)
    :param calcStatsPyd: is a boolean specifying that the image stats and pyramids should be calculated on the output image (Default: True)

    .. code:: python

        import rsgislib.imagecalibration
        rsgislib.imagecalibration.perform_dos_calc("LS5TM_20110701_lat52lon421_r24p204_rad_toa.kea", 'LS5TM_20110701_lat52lon421_r24p204_rad_toa_dos.kea")

    """
    import collections

    import rsgislib
    import rsgislib.imagecalc
    import rsgislib.imageutils

    outDataType = rsgislib.imageutils.get_rsgislib_datatype_from_img(inputFile)

    percentList = rsgislib.imagecalc.calc_band_percentile(
        inputFile, darkObjPercentile, noDataVal
    )

    offsetsList = list()
    OffVal = collections.namedtuple("DOSOffset", ["offset"])
    for val in percentList:
        offsetsList.append(OffVal(offset=val))

    apply_subtract_single_offsets(
        inputFile,
        outputFile,
        gdalformat,
        outDataType,
        nonNegative,
        True,
        noDataVal,
        darkObjReflVal,
        offsetsList,
    )

    if copyBandNames:
        bandNames = rsgislib.imageutils.getBandNames(inputFile)
        rsgislib.imageutils.set_band_names(outputFile, bandNames)

    if calcStatsPyd:
        rsgislib.imageutils.pop_img_stats(
            outputFile, use_no_data=True, no_data_val=noDataVal, calc_pyramids=True
        )

def calc_clear_sky_regions(
    cloudsImg,
    validAreaImg,
    outputClearSkyMask,
    outFormat,
    tmpPath="tmpClearSky",
    deleteTmpFiles=True,
    initClearSkyRegionDist=5000,
    initClearSkyRegionMinSize=3000,
    finalClearSkyRegionDist=1000,
    morphSize=21,
):
    """
    Given a cloud mask, identify the larger extent regions of useful clear-sky regions.

    :param cloudsImg: An image with the input mask of the cloud (pixel == 1) and shadow (pixel == 2)
    :param validAreaImg: A mask of the image data area (1 = valid and 0 = not-valid; i.e., outside of the data area)
    :param outputClearSkyMask: The output mask of the clear sky areas
    :param outFormat: The output image format.
    :param tmpPath: The path for temporay images produced during the processing to be stored (Default: 'tmpClearSky'; Note. all temp files are generated as KEA files).
    :param deleteTmpFiles: Boolean as to whether the intermediate files should be deleted following processing (Default: True - delete files).
    :param initClearSkyRegionDist: The distance in metres from a cloud/shadow object for the initial identification of clear sky regions (Default: 5000)
    :param initClearSkyRegionMinSize: The minimum size (in pixels) of the initial clear sky regions (Default: 3000 pixels)
    :param finalClearSkyRegionDist: The distance in metres from a cloud/shadow object for the final boundaries of the clear sky regions (Default: 1000)
    :param morphSize: the size of the circular morphological operator used to tidy up the result (Default: 21)

    .. code:: python

        import rsgislib.imagecalibration
        cloudsImg = "Outputs/LS8_20160605_lat52lon261_r24p203_clouds.kea"
        validAreaImg = "Outputs/LS8_20160605_lat52lon261_r24p203_valid.kea"
        outputMask = "Outputs/LS8_20160605_lat52lon261_r24p203_openskyvalid.kea"
        tmpPath = "temp"
        rsgislib.imagecalibration.calc_clear_sky_regions(cloudsImg, validAreaImg, outputMask, 'KEA', tmpPath)

    """

    import os

    import numpy
    from osgeo import gdal
    from rios import rat

    import rsgislib
    import rsgislib.imagecalc
    import rsgislib.imagemorphology
    import rsgislib.imageutils
    import rsgislib.rastergis
    import rsgislib.segmentation

    if morphSize % 2 == 0:
        raise rsgislib.RSGISPyException(
            "The size of the morphology operator must be odd."
        )

    baseDataName = os.path.splitext(os.path.basename(cloudsImg))[0]
    tmpCloudsImgDist2Clouds = os.path.join(tmpPath, baseDataName + "_dist2clouds.kea")
    tmpCloudsImgDist2CloudsNoData = os.path.join(
        tmpPath, baseDataName + "_dist2clouds_masked.kea"
    )
    tmpInitClearSkyRegions = os.path.join(tmpPath, baseDataName + "initclearsky.kea")
    tmpInitClearSkyRegionsClumps = os.path.join(
        tmpPath, baseDataName + "initclearskyClumps.kea"
    )
    tmpInitClearSkyRegionsRmSmall = os.path.join(
        tmpPath, baseDataName + "initclearskyClumpsRMSmall.kea"
    )
    tmpInitClearSkyRegionsFinal = os.path.join(
        tmpPath, baseDataName + "initclearskyClumpsFinal.kea"
    )
    tmpClearSkyRegionsFullExtent = os.path.join(
        tmpPath, baseDataName + "clearskyClumpsFullExtent.kea"
    )
    tmpClearSkyRegionsFullExtentClumps = os.path.join(
        tmpPath, baseDataName + "clearskyClumpsFullExtentClumps.kea"
    )
    tmpClearSkyRegionsFullExtentSelectClumps = os.path.join(
        tmpPath, baseDataName + "clearskyClumpsFullExtentSelectClumps.kea"
    )
    tmpClearSkyRegionsFullExtentSelectClumpsOpen = os.path.join(
        tmpPath, baseDataName + "clearskyClumpsFullExtentSelectClumpsOpen.kea"
    )
    tmpClearSkyRegionsFullExtentSelectClumpsOpenClump = os.path.join(
        tmpPath, baseDataName + "clearskyClumpsFullExtentSelectClumpsOpenClump.kea"
    )
    tmpClearSkyRegionsFullExtentSelectClumpsOpenClumpRMSmall = os.path.join(
        tmpPath,
        baseDataName + "clearskyClumpsFullExtentSelectClumpsOpenClumpRMSmall.kea",
    )
    tmpMorphOperator = os.path.join(tmpPath, "CircularMorphOp.gmtxt")

    rsgislib.imagecalc.calc_dist_to_img_vals(
        cloudsImg, tmpCloudsImgDist2Clouds, pxl_vals=[1, 2], out_no_data_val=-9999
    )

    rsgislib.imageutils.mask_img(
        tmpCloudsImgDist2Clouds,
        validAreaImg,
        tmpCloudsImgDist2CloudsNoData,
        "KEA",
        rsgislib.TYPE_32INT,
        -1,
        0,
    )

    rsgislib.imagecalc.image_math(
        tmpCloudsImgDist2CloudsNoData,
        tmpInitClearSkyRegions,
        "b1 > " + str(initClearSkyRegionDist),
        outFormat,
        rsgislib.TYPE_32UINT,
    )

    rsgislib.segmentation.clump(
        tmpInitClearSkyRegions, tmpInitClearSkyRegionsClumps, "KEA", False, 0.0, False
    )

    rsgislib.rastergis.pop_rat_img_stats(tmpInitClearSkyRegionsClumps, True, True)

    rsgislib.segmentation.rm_small_clumps(
        tmpInitClearSkyRegionsClumps,
        tmpInitClearSkyRegionsRmSmall,
        initClearSkyRegionMinSize,
        "KEA",
    )

    rsgislib.segmentation.relabel_clumps(
        tmpInitClearSkyRegionsRmSmall, tmpInitClearSkyRegionsFinal, "KEA", False
    )

    rsgislib.rastergis.pop_rat_img_stats(tmpInitClearSkyRegionsFinal, True, True)

    rsgislib.imagecalc.image_math(
        tmpCloudsImgDist2CloudsNoData,
        tmpClearSkyRegionsFullExtent,
        "b1 > " + str(finalClearSkyRegionDist),
        outFormat,
        rsgislib.TYPE_32UINT,
    )

    rsgislib.segmentation.clump(
        tmpClearSkyRegionsFullExtent,
        tmpClearSkyRegionsFullExtentClumps,
        "KEA",
        False,
        0.0,
        False,
    )

    rsgislib.rastergis.pop_rat_img_stats(tmpClearSkyRegionsFullExtentClumps, True, True)

    rsgislib.rastergis.populate_rat_with_stats(
        tmpInitClearSkyRegionsFinal,
        tmpClearSkyRegionsFullExtentClumps,
        [rsgislib.rastergis.BandAttStats(band=1, max_field="InitRegionInter")],
    )

    ratDataset = gdal.Open(tmpClearSkyRegionsFullExtentClumps, gdal.GA_Update)
    InitRegionInter = rat.readColumn(ratDataset, "InitRegionInter")
    ValidClumps = numpy.zeros_like(InitRegionInter, dtype=numpy.dtype("int"))
    ValidClumps[InitRegionInter > 0] = 1
    rat.writeColumn(ratDataset, "ValidClumps", ValidClumps)
    ratDataset = None

    rsgislib.rastergis.collapse_rat(
        tmpClearSkyRegionsFullExtentClumps,
        "ValidClumps",
        tmpClearSkyRegionsFullExtentSelectClumps,
        "KEA",
        1,
    )

    rsgislib.rastergis.pop_rat_img_stats(
        tmpClearSkyRegionsFullExtentSelectClumps, True, True
    )

    rsgislib.imagemorphology.create_circular_op(
        output_file=tmpMorphOperator, op_size=morphSize
    )

    rsgislib.imagemorphology.image_opening(
        input_img=tmpClearSkyRegionsFullExtentSelectClumps,
        output_img=tmpClearSkyRegionsFullExtentSelectClumpsOpen,
        tmp_img="",
        morph_op_file=tmpMorphOperator,
        use_op_file=True,
        op_size=21,
        gdalformat="KEA",
        datatype=rsgislib.TYPE_32UINT,
    )

    rsgislib.segmentation.clump(
        tmpClearSkyRegionsFullExtentSelectClumpsOpen,
        tmpClearSkyRegionsFullExtentSelectClumpsOpenClump,
        "KEA",
        False,
        0.0,
        False,
    )

    rsgislib.rastergis.pop_rat_img_stats(
        tmpClearSkyRegionsFullExtentSelectClumpsOpenClump, True, True
    )

    rsgislib.segmentation.rm_small_clumps(
        tmpClearSkyRegionsFullExtentSelectClumpsOpenClump,
        tmpClearSkyRegionsFullExtentSelectClumpsOpenClumpRMSmall,
        initClearSkyRegionMinSize,
        "KEA",
    )

    rsgislib.imagecalc.image_math(
        tmpClearSkyRegionsFullExtentSelectClumpsOpenClumpRMSmall,
        outputClearSkyMask,
        "b1>0?1:0",
        outFormat,
        rsgislib.TYPE_8UINT,
    )

    if deleteTmpFiles:
        import rsgislib.tools.filetools

        rsgislib.tools.filetools.delete_file_with_basename(tmpCloudsImgDist2Clouds)
        rsgislib.tools.filetools.delete_file_with_basename(
            tmpCloudsImgDist2CloudsNoData
        )
        rsgislib.tools.filetools.delete_file_with_basename(tmpInitClearSkyRegions)
        rsgislib.tools.filetools.delete_file_with_basename(tmpInitClearSkyRegionsClumps)
        rsgislib.tools.filetools.delete_file_with_basename(
            tmpInitClearSkyRegionsRmSmall
        )
        rsgislib.tools.filetools.delete_file_with_basename(tmpInitClearSkyRegionsFinal)
        rsgislib.tools.filetools.delete_file_with_basename(tmpClearSkyRegionsFullExtent)
        rsgislib.tools.filetools.delete_file_with_basename(
            tmpClearSkyRegionsFullExtentClumps
        )
        rsgislib.tools.filetools.delete_file_with_basename(
            tmpClearSkyRegionsFullExtentSelectClumps
        )
        rsgislib.tools.filetools.delete_file_with_basename(
            tmpClearSkyRegionsFullExtentSelectClumpsOpen
        )
        rsgislib.tools.filetools.delete_file_with_basename(
            tmpClearSkyRegionsFullExtentSelectClumpsOpenClump
        )
        rsgislib.tools.filetools.delete_file_with_basename(
            tmpClearSkyRegionsFullExtentSelectClumpsOpenClumpRMSmall
        )
        rsgislib.tools.filetools.delete_file_with_basename(tmpMorphOperator)

def get_esun_value(radiance, toaRefl, day, month, year, solarZenith):
    """
    Get the ESUN value where a radiance and TOA Reflectance value are known for a pixel.

    :param radiance: input at sensor radiance value.
    :param toaRefl: input the known at sensor (top of atmosphere) reflectance value for the given radiance.
    :param day: input the day of the acquisition.
    :param month: input the month of the acquisition.
    :param year: input the year of the acquisition.
    :param solarZenith: input the solar zenith angle for the acquisition.
    :returns: esun radiance
    """
    import math

    import rsgislib.imagecalibration

    julianDay = rsgislib.imagecalibration.get_julian_day(year, month, day)
    solarDist = rsgislib.imagecalibration.calc_solar_distance(julianDay)
    # pi * L * d2
    step1 = math.pi * radiance * (solarDist * solarDist)
    # step1 / toaRefl
    step2 = step1 / toaRefl
    # step2 / cos(solarZenith)
    esun = step2 / math.cos(math.radians(solarZenith))
    return esun

def visibility_to_aot(vis: float) -> float:
    """
    A function which converts an estimate of visibility to
    aerosol optical thickness (AOT)

    :param vis: input visibility estimate
    :return: output AOT estimate.

    """
    return (3.9449 / vis) + 0.08498

"""
def createEstimateSREFSurface(inputTOAImg, imgBands, bandRescale, winSize, outImage, gdalformat, dataType, tmpDIR):
    ###
    Estimate SREF surface from TOA input image using the method details in He, K., Sun, J., & Tang, X. (2011).
    'Single image haze removal using dark channel prior'. IEEE Transactions on Pattern Analysis and Machine Intelligence.
    Method details were also identified with reference to https://www.kaggle.com/ivanl1/haze-removal-using-dark-channel-prior

    **WARNING DO NOT USE - THIS FUNCTION IS STILL A WORK IN PROGRESS**

    ###
    raise rsgislib.RSGISPyException("This function is not yet ready - needs more thought as how to get a fully working implementation.")
    import rsgislib
    import rsgislib.imagecalc
    import rsgislib.imageutils

    import os.path
    import shutil

    from rios import applier
    from rios import cuiprogress

    import numpy

    baseName = os.path.splitext(os.path.basename(inputTOAImg))[0]
    tmpImgDIR = os.path.join(tmpDIR, baseName)

    tmpPresent = True
    if not os.path.exists(tmpImgDIR):
        os.makedirs(tmpImgDIR)
        tmpPresent = False

    ###############################################
    ########## NEEDS TO CHANGE SCALING ############
    ###############################################
    #
    # The scaling used seems to have a significant
    # effect on the results. Should a histogram or
    # standard deviation approach be adopted?
    #
    ###############################################

    # Rescale input TOA
    rescaledInputImg = os.path.join(tmpImgDIR, baseName+'_rescaled.kea')
    rsgislib.imagecalc.rescale_img_pxl_vals(inputTOAImg, rescaledInputImg, 'KEA', rsgislib.TYPE_32FLOAT, bandRescale, trim2Limits=True)

    # Calculate the minium value (across all bands) within the window.
    minValInWinImg = os.path.join(tmpImgDIR, baseName+'_minValInWin.kea')
    minValInWinRefImg = os.path.join(tmpImgDIR, baseName+'_minValInWinRef.kea')
    rsgislib.imagecalc.identifyMinPxlValueInWin(rescaledInputImg, minValInWinImg, minValInWinRefImg, imgBands, winSize, 'KEA', nodataval=0.0, usenodata=True)

    ###############################################
    ########## NEEDS TO ADD FILTERING #############
    ###############################################
    #
    # Published paper uses the Soft Matting algorithm
    # but this uses a lot of memory etc. so prob not
    # suitable.
    #
    # A Guided Filter is an alternative and should be
    # implmented in RSGISLIb - would generally be useful.
    #
    ###############################################

    # Calculate the 99th percentile.
    percentVals = rsgislib.imagecalc.calc_band_percentile(minValInWinImg, 0.99, 0)

    # Create mask of regions over 99th percentile
    atmosMaskImg = os.path.join(tmpImgDIR, baseName+'_atmosMask.kea')
    rsgislib.imagecalc.image_math(minValInWinImg, atmosMaskImg, '(b1==0)?0:(b1>='+str(percentVals[0])+')?1:0', 'KEA', rsgislib.TYPE_8UINT)

    # Calculate the mean value within the atmospheric light mask
    imgMeanVal = rsgislib.imagecalc.calcImgMeanInMask(rescaledInputImg, atmosMaskImg, 1, imgBands, 1, True)

    # Calculate the atmospheric transmission
    # transmission = 1 - omega * imgDark / atomsphericLight
    transImg = os.path.join(tmpImgDIR, baseName+'_transmission.kea')
    rsgislib.imagecalc.image_math(minValInWinImg, transImg, '(b1==0)?0:(1 - (1 * (b1/'+str(imgMeanVal)+')))<0.025?0.025:(1 - (1 * (b1/'+str(imgMeanVal)+')))', 'KEA', rsgislib.TYPE_32FLOAT)

    #####################################################################
    # Calculate the scaled SREF pixel values.
    srefScaledImg = os.path.join(tmpImgDIR, baseName+'_scaledSREF.kea')

    try:
        import tqdm
        progress_bar = rsgislib.TQDMProgressBar()
    except:
        progress_bar = cuiprogress.GDALProgressBar()

    infiles = applier.FilenameAssociations()
    infiles.toaScaledImage = rescaledInputImg
    infiles.transImg = transImg
    outfiles = applier.FilenameAssociations()
    outfiles.outimage = srefScaledImg
    otherargs = applier.OtherInputs()
    otherargs.atmosLight = imgMeanVal
    otherargs.imgBands = imgBands
    otherargs.numpyDT = numpy.float32
    aControls = applier.ApplierControls()
    aControls.progress = progress_bar
    aControls.drivername = 'KEA'
    aControls.omitPyramids = True
    aControls.calcStats = False

    def _calcScaleSREFVals(info, inputs, outputs, otherargs):
        # This is an internal rios function
        outImgShape = [len(otherargs.imgBands), inputs.transImg.shape[1], inputs.transImg.shape[2]]
        outputs.outimage = numpy.zeros(outImgShape, dtype=otherargs.numpyDT)
        for idx in range(len(imgBands)):
            bandIdx = imgBands[idx]-1
            outputs.outimage[idx] = numpy.where(inputs.toaScaledImage[bandIdx] == 0.0, 0.0, (((inputs.toaScaledImage[bandIdx]-otherargs.atmosLight))/inputs.transImg[0])+otherargs.atmosLight)

    applier.apply(_calcScaleSREFVals, infiles, outfiles, otherargs, controls=aControls)
    # End of RIOS Section
    #####################################################################

    ###############################################
    ########## NEEDS TO CHANGE SCALING ############
    ###############################################
    #
    # How to reliably rescale the results back to
    # to the original range. (I guess we need the
    # original data range rather than the editted
    # range from the stretch?)
    #
    ###############################################

    # Rescale the SREF image
    bandRescaleSREF = []
    outBand = 1
    for band in imgBands:
        bandRescaleSREF.append(rsgislib.imagecalc.ImageBandRescale(outBand, bandRescale[band-1].outMin, bandRescale[band-1].outMax, bandRescale[band-1].outNoData, 5, (bandRescale[band-1].inMax-bandRescale[band-1].inMin), 0))
        outBand = outBand + 1

    rsgislib.imagecalc.rescale_img_pxl_vals(srefScaledImg, outImage, gdalformat, dataType, bandRescaleSREF, trim2Limits=True)
    rsgislib.imageutils.pop_img_stats(outImage, use_no_data=True, no_data_val=0, calc_pyramids=True)

    if not tmpPresent:
        shutil.rmtree(tmpImgDIR, ignore_errors=True)
"""


import pymysql as pymysql

from QQModel.Entity.friend import friend

class request:
    # 请求  Request (account,receiver_account, state)
    # state为0表示未处理，1表示同意，2表示拒绝

    def __init__(self, account='', receiver_account='', state=0):
        self.account = account
        self.receiver_account = receiver_account
        self.state = state
        self.db = pymysql.connect("localhost", "root", "Sunbz241290", "qqsimulator")
        self.cursor = self.db.cursor()  # 创建一个游标对象 cursor

    def send_request(self):
        # 创建申请 -1表示原来没有 0表示未处理，1表示同意，2表示拒绝
        self.check_request()
        if self.state==-1:
            sql = "INSERT INTO Request(account,receiver_account, state) values('" + str(self.account) + "','" \
                  + str(self.receiver_account) + "',0) "
        elif self.state==0:
            return self.state
        elif self.state==1:
            return self.state
        elif self.state==2:
            sql = "UPDATE Request  set state = 0 " + "  WHERE receiver_account = '" + self.receiver_account + "' " + \
                  " AND account = '" + self.account + "'"
        try:
            self.cursor.execute(sql)
            self.db.commit()
        except:
            self.db.rollback()
        self.db.close()
        return self.state

    def check_request(self):
        # 查看申请状态，是无申请还是怎样
        sql = "SELECT state FROM Request WHERE receiver_account = '"+self.receiver_account+"' "+\
              " AND account = '"+self.account+"'"
        try:
            self.cursor.execute(sql)
            results = self.cursor.fetchall()
            if results == ():
                self.state = -1
                return
            for r in results:
                self.state = r[0]
        except:
            self.db.rollback()

    def select_send_friend(self):
        # 查看发送的申请   为account赋值
        sql = "SELECT  sendname, receivename, sendaccount, receiveaccount, state FROM V_Request_User WHERE sendaccount = '"+ self.account+"' "
        requests = []
        try:
            self.cursor.execute(sql)
            results = self.cursor.fetchall()
            for r in results:
                temp = {'sendname': "",'receivename': "",'sendaccount': "",'receiveaccount': "",'state': 0}
                temp['sendname'] = r[0]
                temp['receivename'] = r[1]
                temp['sendaccount'] = r[2]
                temp['receiveaccount'] = r[3]
                temp['state'] = r[4]
                requests.append(temp)
        except:
            self.db.rollback()
        self.db.close()
        return requests

    def select_receive_friend(self):
        # 查看收到的的申请    为account赋值
        sql = "SELECT  sendname, receivename, sendaccount, receiveaccount, state FROM V_Request_User WHERE receiveaccount = '"+ self.account+"' "
        requests = []
        try:
            self.cursor.execute(sql)
            results = self.cursor.fetchall()
            for r in results:
                temp = {'sendname': "",'receivename': "",'sendaccount': "",'receiveaccount': "",'state': 0}
                temp['sendname'] = r[0]
                temp['receivename'] = r[1]
                temp['sendaccount'] = r[2]
                temp['receiveaccount'] = r[3]
                temp['state'] = r[4]
                requests.append(temp)
        except:
            self.db.rollback()
        self.db.close()
        return requests

    def deal_request(self):
        # 处理申请
        # 申请得到同意后，系统会自动成功添加好友state置为1。
        # 申请得到拒绝后， state置为2，不执行操作
        sql = "UPDATE  request SET state = "+str(self.state)+"  WHERE receiver_account = '"+self.receiver_account+"' "+" AND account = '"+self.account+"'"
        try:
            self.cursor.execute(sql)
            self.db.commit()
        except:
            self.db.rollback()
        self.db.close()
        if(self.state==1):
            a = friend(account=self.account, friend_account=self.receiver_account)
            a.create_friend()

# a = request(account="256767845")
# users = []
# users = a.select_send_friend()
# print(users)
# for i in users:
#     print(i.get('sendname'))


#!/usr/bin/python3
from typing import Union, Tuple, List, Callable, Any
from pypbc import *
import hashlib

class KeyGen:
    def __init__(self, n: int):
        self.params: Parameters = Parameters(qbits=512, rbits=160)
        self.pairing: Pairing = Pairing(self.params)
        self.h1: Callable[[Union[bytes, bytearray, str]], Element] = self.get_hash_function(self.pairing, hashlib.sha3_256)
        self.h2: Callable[[Union[bytes, bytearray, str]], Element] = self.get_hash_function(self.pairing, hashlib.sha3_512)
        self.e: Callable[[Element, Element], Element] = lambda e1, e2: self.pairing.apply(e1, e2)
        self.pub_keys: List[Element] = []
        self.keys: List[Tuple[Element, Element]] = []
        self.priv_keys: List[Element] = []
        self.g = Element.random(self.pairing, G1)
        for _ in range(n):
            sk: Element = Element.random(self.pairing, Zr)
            pk: Element = Element(self.pairing, G1, value=self.g ** sk)
            self.keys.append((sk, pk))
            self.pub_keys.append(pk)
            self.priv_keys.append(sk)

    def get_hash_function(self, pairing, hash_function: Callable[[Union[bytes, bytearray]], Any]) -> Callable[[Union[bytes, bytearray, str]], Element]:
        return lambda text: Element.from_hash(pairing, G1, hash_function(text).digest()) if isinstance(text, (bytes, bytearray)) else \
            Element.from_hash(pairing, G1, hash_function(text.encode()).digest())

    def __str__(self):
        return f"\n".join([f"keys (s{i},p{i})=({self.keys[i][0]},{self.keys[i][1]})" for i, key in enumerate(self.keys)])

if __name__ == '__main__':
    print(KeyGen(3))


import tempfile

class TempFileManager:
    def __enter__(self):
        # Создаем временный файл и открываем его в режиме чтения и записи
        self.file = tempfile.NamedTemporaryFile(mode='w+', delete=False)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        # Закрываем файл
        self.file.close()

        # Читаем содержимое файла
        with open(self.file.name, 'r') as f:
            content = f.read()

            # Выводим содержимое файла в консоль
            print(f"Содержимое файла:\n{content}")

            # Выводим количество символов в файле
            print(f"Количество символов в файле: {len(content)}")

        # Удаляем временный файл
        self.file.delete = True

    def repeat(self):
        # Дублируем текущее содержимое файла и добавляем в конец файла
        with open(self.file.name, 'a') as f:
            f.write(open(self.file.name).read())

    def write(self, msg):
        # Записываем текст в начало файла
        with open(self.file.name, 'r+') as f:
            content = f.read()
            f.seek(0, 0)
            f.write(msg)
            f.write(content)

    def show(self):
        # Выводим содержимое файла в консоль
        with open(self.file.name, 'r') as f:
            content = f.read()
            print(f"Содержимое файла:\n{content}")

# Пример использования контекстного менеджера
with TempFileManager() as manager:
    # Записываем текст в начало файла
    manager.write("Hello, World!")

    # Дублируем содержимое файла и добавляем в конец
    manager.repeat()

    # Выводим содержимое файла в консоль
    manager.show()


import logging
from datetime import datetime

logging.basicConfig(level='DEBUG', filename='sample.log')
logger = logging.getLogger('Python')

msg = str(datetime.now()) +  ':sample debug message'
logger.debug(msg)

msg = str(datetime.now()) +  ':sample warning message'
logger.warning(msg)


import urllib.request
import urllib.error
import os
import json

jikan_base_url = 'https://api.jikan.moe/v3/anime/'
mal_base_url = 'https://myanimelist.net/anime/'

# Anime Titles sometimes use ridiculous symbols ¯\_(ツ)_/¯
UTF8_mappings = {
    '\\\'': '\'', '"': '\"', '\\xc2\\xae': '®', '\\xc2\\xb0': '°', '\\xc2\\xb2': '²', '\\xc2\\xb3': '³',
    '\\xc2\\xbd': '½', '\\xc3\\x84': 'Ä', '\\xc3\\x89': 'É', '\\xc3\\x97': '×', '\\xc3\\x9c': 'Ü', '\\xc3\\xa0': 'à',
    '\\xc3\\xa2': 'â', '\\xc3\\xa8': 'è', '\\xc3\\xa4': 'ä', '\\xc3\\xa9': 'é', '\\xc3\\xb6': 'ö', '\\xc3\\xb9': 'ù',
    '\\xc3\\xbc': 'ü', '\\xc3\\x9f': 'ß', '\\xc4\\x83': 'ă', '\\xc4\\x93': 'ē', '\\xc5\\x8d': 'ō', '\\xc5\\x92': 'Œ',
    '\\xc5\\xa1': 'š', '\\xce\\x94': 'Δ', '\\xce\\xa8': 'Ψ', '\\xce\\xbc': 'μ', '\\xcf\\x87': 'χ',
    '\\xe2\\x80\\x8b': '', '\\xe2\\x80\\x94': '—', '\\xe2\\x80\\x99': '’', '\\xe2\\x80\\x9c': '“',
    '\\xe2\\x80\\x9d': '”', '\\xe2\\x80\\xa0': '†', '\\xe2\\x84\\x83': '℃', '\\xe2\\x86\\x90': '←',
    '\\xe2\\x86\\x91': '↑', '\\xe2\\x86\\x92': '→', '\\xe2\\x88\\x9e': '∞', '\\xe2\\x89\\xa0': '≠',
    '\\xe2\\x90\\xa3': '␣', '\\xe2\\x96\\xb3': '△ ', '\\xe2\\x97\\xaf': '◯', '\\xe2\\x98\\x86': '☆',
    '\\xe2\\x98\\x85': '★', '\\xe2\\x99\\xa1': '♡', '\\xe2\\x99\\xa5': '♥', '\\xe2\\x99\\xaa': '♪',
    '\\xe2\\x99\\x80': '♀', '\\xe2\\x99\\x82': '♂', '\\xe2\\x99\\xad': '♭', '\\xe2\\xa4\\xb4': '⤴',
    '\\xe3\\x83\\xbb': '・', '\\xef\\xbc\\x8a': '＊', '\\xef\\xbf\\xa5': '￥', ''': '\'', '&': '&',
}

# Takes the title string and translates all the accounted for special characters in the titles
def filter_title(t):
    for key in UTF8_mappings:
        if key in t:
            t = t.replace(key, UTF8_mappings[key])
    return t

# Takes a token and returns the substring in-between the tokens.
def page_finder(page_str, token_start, token_end):
    token_index = page_str.find(token_start) + len(token_start)
    return page_str[token_index: page_str.find(token_end, token_index)]

# Where to store the txt files of the logged information
SOURCE_PATH = os.path.join('.', 'Anime By Source')
MONTH_PATH = os.path.join('.', 'Anime By Month')
DAY_PATH = os.path.join('.', 'Anime By Day')

# Takes a range of indexes to check over the MAL DB, reads the webpage, scans for important data, and writes to disk
# based on type of source material.
def search_and_write(list_indexes, index_list):
    did_work = False

    # For each page that exists. Find the relevant data and store that in the text file corresponding to the source and
    # connect each one with a === for easy splitting and filtering after all the data is logged.
    for i in list_indexes:
        try:

            jikan_url = jikan_base_url + str(i)
            mal_url = mal_base_url + str(i)

            with urllib.request.urlopen(jikan_url) as url:
                data = json.loads(url.read().decode())

            title = data['title']

            source = data['source']

            score = str(data['score'])
            len_score = len(score)
            if len_score == 1:
                score += '.00'
            elif len_score == 3:
                score += '0'
            elif len_score == 'None':
                score = 'N/A'
            elif len_score > 4:
                score = score[:4]

            duration = data['duration']

            num_eps_int = data['episodes']
            if num_eps_int:
                num_eps = str(num_eps_int) + ' episode'
                if num_eps_int > 1:
                    num_eps += 's'
            else:
                num_eps = 'Ongoing'

            genres = ''
            for genre in data['genres']:
                genre_name = genre['name']
                if genres:
                    genres += ', %s' % genre_name
                else:
                    genres += genre_name

            # finding the right date data takes some filtering to hit all the possible formats
            date = data['aired']['string']
            if date.find(' to ') != -1:
                date = date.split(' to ')[0]  # ignore the ending date
            date = date.replace(',', '')  # take out the comma so we can split by spaces
            date_items = date.split(' ')  # token possibilities are Month Day Year, Month Year, Year, Or Unknown
            month = 'Unknown'
            day = 'Unknown'
            year = 'Unknown'
            if len(date_items) == 3:
                month = date_items[0]
                day = date_items[1]
                year = date_items[2]
            elif len(date_items) == 2:
                if date_items[0] != 'Not':  # skip "Not Available"
                    month = date_items[0]
                    year = date_items[1]
            elif len(date_items) == 1 and date_items[0] != 'Unknown':
                year = date_items[0]

            # Print a small snippet of info so the user knows that the program is still running.
            print('Index: ' + str(i) + ' - ' + filter_title(title) + ' [' + source + ']')

            # Write the data, but make sure to encode using 'utf-8' otherwise special characters are represented as more
            # than one space. (e.g ä will be taken as 2 spaces since it's base value is \xc3\xa4.

            with open(os.path.join(SOURCE_PATH, source + '.temp'), 'a+', encoding='utf-8') as source_file:
                source_file.write('==='.join([filter_title(title), score, year, num_eps, duration,
                                              genres.replace('"', ''), mal_url]) + '\n')

            with open(os.path.join(MONTH_PATH, month + '.temp'), 'a+', encoding='utf-8') as month_file:
                month_file.write('==='.join([filter_title(title), score, year, num_eps, duration,
                                             genres.replace('"', ''), mal_url]) + '\n')

            with open(os.path.join(DAY_PATH, day + '.temp'), 'a+', encoding='utf-8') as day_file:
                day_file.write('==='.join([filter_title(title), score, year, num_eps, duration,
                                           genres.replace('"', ''), mal_url]) + '\n')

            with open(index_list, 'a+') as empty_urls:
                empty_urls.write(str(i) + ' - 200: OK\n')
            did_work = True

        except urllib.error.HTTPError as e:
            error_message = str(i) + ' - ' + str(e).replace('HTTP Error ', '')
            with open(index_list, 'a+') as empty_urls:
                empty_urls.write(error_message + '\n')
            print('Index: ' + error_message)
            did_work = True
        except urllib.error.URLError:
            pass
    return did_work


"""
Read the .mat files in a folder and create .mhd files with files' old names.
The library used here is SimpleITK.
"""

import scipy.io
import SimpleITK as sitk
import os
from import_data import import_data
from filters import filters
from realignment import realignment
from import_and_preprocess import import_preprocess

# Define a function to convert .mat files to .mhd files
def mat2mhd(filePaths):
    """ Read the .mat files in a folder and create .mhd files
        with the old names."""

    for i, path in enumerate(filePaths, 1):
        print('-- processing %d/%d' % (i, len(filePaths)))
        old_name=os.path.basename(path) # extract file name
        name = old_name.split('.')[0] # split name and file extension
        print(name) # print names and track the process
        save_path = "./output" # define save folder path
        new_name = save_path + "/" + '{}.{}'.format(name, 'mhd') # new file name
        # new_name = save_path + "/" + '{}.{}'.format(name, 'mha')

        # preprocess data
        US_Stack = import_preprocess(path)

        # write and save .mhd file
        img = sitk.GetImageFromArray(US_Stack)
        sitk.WriteImage(img, new_name)

# Define a main function to build filepaths from a folder path
def main():
    filePaths = []
    # for root, __, files in os.walk('./Data/PhantomSpine/Group1/2019-04-18-IRExperiment2/1-Neutral'):
    for root, __, files in os.walk('./Data/Code_and_Data_for_Students/Set1/Neutral1'):
        for f in files:
            if f.endswith(".mat"):
                filePaths.append(os.path.join(root, f))

    mat2mhd(filePaths)

# Driver Code
if __name__ == '__main__':
   main()


import asyncio
import signal
import websockets

CLIENTS = {}

async def dispatch(data):
    for cluster_name, client in CLIENTS.items():
        await client.send(data)
        print(f'> Cluster[{cluster_name}]')

async def serve(ws, path):
    cluster_name = await ws.recv()
    cluster_name = cluster_name.decode()
    if cluster_name in CLIENTS:
        print(f"! Cluster[{cluster_name}] attempted reconnection")
        await ws.close(4029, "already connected")
        return
    CLIENTS[cluster_name] = ws
    try:
        await ws.send(b'{"status":"ok"}')
        print(f'$ Cluster[{cluster_name}] connected successfully')
        async for msg in ws:
            print(f'< Cluster[{cluster_name}]: {msg}')
            await dispatch(msg)
    finally:
        CLIENTS.pop(cluster_name)
        print(f'$ Cluster[{cluster_name}] disconnected')

signal.signal(signal.SIGINT, signal.SIG_DFL)
signal.signal(signal.SIGTERM, signal.SIG_DFL)

server = websockets.serve(serve, 'localhost', 42069)
loop = asyncio.get_event_loop()
loop.run_until_complete(server)
loop.run_forever()


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

class LinkedList(object):
  def __init__(self):
    self.head                    = None
    self.size                    = 0

  # O(1)
  def insertStart(self, data):
    self.size                   += 1
    newNode                      = Node(data)

    if not self.head:              #            => not None => True; not (self.head object) => False
      self.head                  = newNode
    else:
      newNode.nextNode           = self.head  # => point newNode's nextNode property to current head
      self.head                  = newNode  #   => Make newNode the current head

  # O(1)
  def size1(self):
    return f"current size: {self.size}"

  # O(n):                          Traversing the Linkedlist
  def sizeN(self):
    currentNode                  = self.head
    size                         = 0

    while currentNode is not None:
      size                      += 1
      currentNode                = currentNode.nextNode

    return size

  # O(n):                          Traversing LinkedList
  def insertEnd(self, data):
    self.size                   += 1
    newNode                      = Node(data)
    currentNode                  = self.head

    while currentNode.nextNode is not None:
      currentNode                = currentNode.nextNode

    currentNode.nextNode         = newNode

  # O(n):                          Traversing LinkedList
  def traverseList(self):
    print("Traversing the list and displaying nodes: ")
    currentNode = self.head

    i = 1
    while currentNode is not None:
      print(f"node #{i}: {currentNode.data}"
            ," <-- Both Head and Tail" if self.size == 1
            else " <-- Current Head" if i == 1
            else " <-- Current Tail" if currentNode.nextNode == None
            else "" )
      currentNode = currentNode.nextNode
      i += 1

    print(currentNode)

  def remove(self, data):
    if self.head is None:
      return

    # Setting up currentNode / previousNode at the beginning
    currentNode         = self.head
    previousNode        = None

    print(f"\nSearching for node containing `{data}` to remove:")
    while currentNode.data != data:
      print(f"currentNode.data {currentNode.data} != {data}")
      if currentNode.nextNode == None:
        break
      else:
        previousNode = currentNode
        currentNode  = currentNode.nextNode

    if currentNode.nextNode == None and currentNode.data != data:
      print(f"`{data}` NOT FOUND")
    else:
      print(f"FOUND! currentNode.data {currentNode.data} == {data} ! Removing `{currentNode.data}`")
      self.size           -= 1

      # Removal control flow based on found node's location
      if previousNode is None and currentNode.nextNode is None:
        print(f"The only remaining node: {currentNode.data} is both a head and a tail")
        print(f"Poiting this list's only head to None will clear it.")
        self.head = currentNode.nextNode
      elif previousNode is None:
        print(f"Since previousNode is {previousNode}, currentNode: {currentNode.data} is head. \nPointing new head to old head's nextNode: {currentNode.nextNode.data}")
        self.head = currentNode.nextNode
        print(f"old head is still pointing to nextNode: {currentNode.nextNode.data}")
        currentNode.nextNode = None
        print(f"removed old head's nextNode reference, now pointing to: {currentNode.nextNode}")
      elif currentNode.nextNode is None:
        print(f"currentNode: {currentNode.data} is a tail. No need to remove its nextNode reference that is already pointing to None")
        previousNode.nextNode = currentNode.nextNode
        print(f"previousNode: {previousNode.data} is now the new tail pointing to {previousNode.nextNode}")
      else:
        previousNode.nextNode = currentNode.nextNode
        print(f"currentNode.nextNode is still pointing to nextNode: {currentNode.nextNode.data}")
        currentNode.nextNode = None
        print(f"removed currentNode's nextNode reference, now pointing to: {currentNode.nextNode}")

linkedlist = LinkedList()
print(linkedlist)

linkedlist.insertStart(12)
linkedlist.insertStart(122)
linkedlist.insertStart("First Head")
linkedlist.insertEnd(31)
linkedlist.insertEnd(-23.34)
linkedlist.insertEnd("First Tail")

linkedlist.traverseList()
print(linkedlist.size1())

linkedlist.remove("First Head")
linkedlist.traverseList()
print(linkedlist.size1())

linkedlist.remove(12)
linkedlist.traverseList()
print(linkedlist.size1())

linkedlist.remove(-23.34)
linkedlist.traverseList()
print(linkedlist.size1())

linkedlist.remove("WhoAmI")
linkedlist.traverseList()
print(linkedlist.size1())

linkedlist.remove(31)
linkedlist.traverseList()
print(linkedlist.size1())

linkedlist.remove("First Tail")
linkedlist.traverseList()
print(linkedlist.size1())

linkedlist.remove(122)
linkedlist.traverseList()
print(linkedlist.size1())


import unittest
import time

from Tools.demo.ss1 import center
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome(executable_path="C:\Program Files (x86)\chromedriver.exe")

driver.get("https://www.makemytrip.com/bus-tickets/")
print(driver.title)
login=driver.find_element_by_xpath("//p[contains(text(),'Login or Create Account')]")
login.click()
email=driver.find_element_by_xpath("//input[@id='username']")
email.click()
email.send_keys("9581441090")
continue1=driver.find_element_by_xpath("//span[contains(text(),'Continue')]")
continue1.click()
time.sleep(3)
time.sleep(30)
login=driver.find_element_by_xpath("//span[contains(text(),'Login')]")
login.click()
time.sleep(5)
bangalore=driver.find_element_by_xpath("//input[@id='fromCity']")
bangalore.click()
time.sleep(3)
fromcity=driver.find_element_by_xpath("//input[@placeholder='From']")
fromcity.click()
fromcity.send_keys("Bangalore")
bglr=driver.find_element_by_xpath("//p[contains(text(),'Bangalore, India')]")
bglr.click()
search=driver.find_element_by_xpath("//a[contains(@class,'primaryBtn font24 latoBlack widgetSearchBtn')]")
search.click()
time.sleep(5)
driver.close()


import sys
import logging

import torch

import main
from mean_teacher.cli import parse_dict_args
from mean_teacher.run_context import RunContext

LOG = logging.getLogger('runner')

def parameters():
    defaults = {
        # Technical details
        # 'checkpoint_epochs': 1,
        # 'evaluation_epochs': 1,

        # Data
        'dataset': 'ISIC2017',
        # 'exclude_unlabeled': False,
        'labels': '50%_label.txt',

        # Data sampling
        'batch_size': 12,
        'labeled_batch_size': 6,

        # Architecture
        'arch': 'resnext152',
        'ema_decay': .9997,

        # Costs
        'consistency_type': 'kl',
        'consistency': 10.0,
        'consistency_rampup': 5,
        'logit_distance_cost': 0.01,
        'weight_decay': 5e-5,

        # Optimization
        'epochs': 120,
        'lr_rampdown_epochs': 150,
        'lr_rampup': 2,
        'initial_lr': 0.1,
        'lr': 0.025,
        'nesterov': True,
        # 'gpu_id':''

    }

    for data_seed in range(10, 12):
        yield {
            **defaults,
            'title': 'mean teacher r-152 eval',
            'data_seed': 0
        }

def run(title,data_seed,**kwargs):
    print('run title: %s', title)
    ngpu = 1
    main.args = parse_dict_args(**kwargs)
    context = RunContext(__file__,kwargs['consistency'],kwargs['epochs'],kwargs['labels'])
    main.main(context)

if __name__ == "__main__":
    for run_params in parameters():
        run(**run_params)


from rdkit import Chem
from rdkit.Chem import rdMolHash as rdMolHash
import rdkit
from rdkit.Chem import rdMolDescriptors

import pandas as p
import os
path = os.path.join(os.getcwd(), 'C:\\Users\\Elif\\Desktop\\testReactionTable.csv')
df = p.read_csv(path)

def Convert(string):
    list1=[]
    list1[:0]=string
    return list1

def CreateProduct(stoichiometry):
    num=""
    targetFormula=""
    splitedFormula =""
    splitedFormula = stoichiometry.split("H")
    secondPart = splitedFormula[1]
    checkIsItFirst = False
    for val in secondPart:
        if val.isalpha():
            break;
        else:
            checkIsItFirst = True
            num += str(val)
            secondPart=secondPart[1:]
    if not checkIsItFirst:
        num=1

    number= int(num)+2
    targetFormula = "{0}{1}{2}{3}".format(splitedFormula[0],"H",str(number),secondPart)
    return targetFormula

for index, row in df.iterrows():
    try:
        splitedFormula = row['reactantStoichiometry'].split("H")
        if len(splitedFormula) ==1:
            createdReactantStoichiometry = row['productStoichiometry'].replace("H2", "")
            if row['reactantStoichiometry'] != createdReactantStoichiometry:
                print("*****************")
                print(index)
                print(row['reactantStoichiometry'])
                print(row['productStoichiometry'])
                print(createdProductStoichiometry)
                print("*****************")
        elif len(splitedFormula) ==2:
            createdProductStoichiometry = CreateProduct(row['reactantStoichiometry'])
            if row['productStoichiometry'] != createdProductStoichiometry:
                print("*****************")
                print(index)
                print(row['reactantStoichiometry'])
                print(row['productStoichiometry'])
                print(createdProductStoichiometry)
                print("*****************")
        else:
            print("BIG PROBLEM")
    except Exception as e:
        hey =1
        # print("--------------------------------")
        # print(index)
        # # print(row)
        # # print(e)
        # print("--------------------------------")


# Link: https://leetcode.com/problems/maximal-rectangle/

class Solution:
    def maximalRectangle(self, matrix) -> int:
        if not matrix:
            return 0
        m, n = len(matrix), len(matrix[0])
        left = [0] * n
        right = [n] * n
        height = [0] * n
        max_area = 0
        for i in range(m):
            cur_left, cur_right = 0, n
            for j in range(n):
                if matrix[i][j] == '1':
                    height[j] += 1
                else:
                    height[j] = 0
            for j in range(n):
                if matrix[i][j] == '1':
                    left[j] = max(left[j], cur_left)
                else:
                    left[j] = 0
                    cur_left = j + 1
            for j in range(n - 1, -1, -1):
                if matrix[i][j] == '1':
                    right[j] = min(right[j], cur_right)
                else:
                    right[j] = n
                    cur_right = j
            for j in range(n):
                max_area = max(max_area, (right[j] - left[j]) * height[j])
        return max_area

obj = Solution()
matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
print(obj.maximalRectangle(matrix))

Python Solution - 1/85. Maximal Rectangle.py


import os
import sys
import shutil
import datetime
import time
import subprocess
import urllib
import urllib2
import json
import pprint

import boto
import simples3

import pymongo

def findSettingsSetup():
    sys.path.append( "./" )
    sys.path.append( "../" )
    sys.path.append( "../../" )
    sys.path.append( "../../../" )

findSettingsSetup()
import settings
import buildscripts.utils as utils
import buildscripts.smoke as smoke

bucket = simples3.S3Bucket( settings.emr_bucket , settings.emr_id , settings.emr_key )

def _get_status():

    def gh( cmds ):
        txt = ""
        for cmd in cmds:
            res = utils.execsys( "git " + cmd )
            txt = txt + res[0] + res[1]
        return utils.md5string( txt )

    return "%s-%s" % ( utils.execsys( "git describe" )[0].strip(),  gh( [ "diff" , "status" ] ) )

def _get_most_recent_tgz( prefix ):
    # this is icky, but works for now
    all = []
    for x in os.listdir( "." ):
        if not x.startswith( prefix ) or not x.endswith( ".tgz" ):
            continue
        all.append( ( x , os.stat(x).st_mtime ) )

    if len(all) == 0:
        raise Exception( "can't find file with prefix: " + prefix )

    all.sort( lambda x,y: int(y[1] - x[1]) )

    return all[0][0]

def get_build_info():
    return ( os.environ.get('MONGO_BUILDER_NAME') , os.environ.get('MONGO_BUILD_NUMBER') )

def make_tarball():

    m = _get_most_recent_tgz( "mongodb-" )

    c = "test-code-emr.tgz"
    tar = "tar zcf %s src jstests buildscripts" % c

    log_config = "log_config.py"
    if os.path.exists( log_config ):
        os.unlink( log_config )

    credentials = do_credentials()
    if credentials:

        builder , buildnum = get_build_info()

        if builder and buildnum:

            file = open( log_config , "wb" )
            file.write( 'username="%s"\npassword="%s"\n' % credentials )
            file.write( 'name="%s"\nnumber=%s\n'% ( builder , buildnum ) )

            file.close()

            tar = tar + " " + log_config

    utils.execsys( tar )
    return ( m , c )

def _put_ine( bucket , local , remote ):
    print( "going to put\n\t%s\n\thttp://%s.s3.amazonaws.com/%s" % ( local , settings.emr_bucket , remote ) )

    for x in bucket.listdir( prefix=remote ):
        print( "\talready existed" )
        return remote

    bucket.put( remote , open( local , "rb" ).read() , acl="public-read" )
    return remote

def build_jar():
    root = "build/emrjar"
    src = "buildscripts/emr"

    if os.path.exists( root ):
        shutil.rmtree( root )
    os.makedirs( root )

    for x in os.listdir( src ):
        if not x.endswith( ".java" ):
            continue
        shutil.copyfile( src + "/" + x , root + "/" + x )
    shutil.copyfile( src + "/MANIFEST.MF" , root + "/MANIFEST.FM" )

    classpath = os.listdir( src + "/lib" )
    for x in classpath:
        shutil.copyfile( src + "/lib/" + x , root + "/" + x )
    classpath.append( "." )
    classpath = ":".join(classpath)

    for x in os.listdir( root ):
        if x.endswith( ".java" ):
            if subprocess.call( [ "javac" , "-cp" , classpath , x ] , cwd=root) != 0:
                raise Exception( "compiled failed" )

    args = [ "jar" , "-cfm" , "emr.jar" , "MANIFEST.FM" ]
    for x in os.listdir( root ):
        if x.endswith( ".class" ):
            args.append( x )
    subprocess.call( args , cwd=root )

    shutil.copyfile( root + "/emr.jar" , "emr.jar" )

    return "emr.jar"

def push():
    mongo , test_code = make_tarball()
    print( mongo )
    print( test_code )

    root = "emr/%s/%s" % ( datetime.date.today().strftime("%Y-%m-%d") , os.uname()[0].lower() )

    def make_long_name(local,hash):
        pcs = local.rpartition( "." )
        h = _get_status()
        if hash:
            h = utils.md5sum( local )
        return "%s/%s-%s.%s" % ( root , pcs[0] , h , pcs[2] )

    mongo = _put_ine( bucket , mongo , make_long_name( mongo , False ) )
    test_code = _put_ine( bucket , test_code , make_long_name( test_code , True ) )

    jar = build_jar()
    jar = _put_ine( bucket , jar , make_long_name( jar , False ) )

    setup = "buildscripts/emr/emrnodesetup.sh"
    setup = _put_ine( bucket , setup , make_long_name( setup , True ) )

    return mongo , test_code , jar , setup

def run_tests( things , tests ):
    if len(tests) == 0:
        raise Exception( "no tests" )
    oldNum = len(tests)
    tests = fix_suites( tests )
    print( "tests expanded from %d to %d" % ( oldNum , len(tests) ) )

    print( "things:%s\ntests:%s\n" % ( things , tests ) )

    emr = boto.connect_emr( settings.emr_id , settings.emr_key )

    def http(path):
        return "http://%s.s3.amazonaws.com/%s" % ( settings.emr_bucket , path )

    run_s3_path = "emr/%s/%s/%s/" % ( os.getenv( "USER" ) ,
                                      os.getenv( "HOST" ) ,
                                      datetime.datetime.today().strftime( "%Y%m%d-%H%M" ) )

    run_s3_root = "s3n://%s/%s/" % ( settings.emr_bucket , run_s3_path )

    out = run_s3_root + "out"
    logs = run_s3_root + "logs"

    jar="s3n://%s/%s" % ( settings.emr_bucket , things[2] )
    step_args=[ http(things[0]) , http(things[1]) , out , ",".join(tests) ]

    step = boto.emr.step.JarStep( "emr main" , jar=jar,step_args=step_args )
    print( "jar:%s\nargs:%s" % ( jar , step_args ) )

    setup = boto.emr.BootstrapAction( "setup" , "s3n://%s/%s" % ( settings.emr_bucket , things[3] ) , []  )

    jobid = emr.run_jobflow( name = "Mongo EMR for %s from %s" % ( os.getenv( "USER" ) , os.getenv( "HOST" ) ) ,
                             ec2_keyname = "emr1" ,
                             slave_instance_type = "m1.large" ,
                             ami_version = "latest" ,
                             num_instances=5 ,
                             log_uri = logs ,
                             bootstrap_actions = [ setup ] ,
                             steps = [ step ] )

    print( "%s jobid: %s" % ( datetime.datetime.today() , jobid ) )

    while ( True ):
        flow = emr.describe_jobflow( jobid )
        print( "%s status: %s" % ( datetime.datetime.today() , flow.state ) )
        if flow.state == "COMPLETED" or flow.state == "FAILED":
            break
        time.sleep(30)

    syncdir = "build/emrout/" + jobid + "/"
    sync_s3( run_s3_path , syncdir )

    final_out = "build/emrout/" + jobid + "/"

    print("output in: " + final_out )
    do_output( final_out )

def sync_s3( remote_dir , local_dir ):
    for x in bucket.listdir( remote_dir ):
        out = local_dir + "/" + x[0]

        if os.path.exists( out ) and x[2].find( utils.md5sum( out ) ) >= 0:
            continue

        dir = out.rpartition( "/" )[0]
        if not os.path.exists( dir ):
            os.makedirs( dir )

        thing = bucket.get( x[0] )
        open( out , "wb" ).write( thing.read() )

def fix_suites( suites ):
    fixed = []
    for name,x in smoke.expand_suites( suites , False ):
        idx = name.find( "/jstests" )
        if idx >= 0:
            name = name[idx+1:]
        fixed.append( name )
    return fixed

def do_credentials():
    root = "buildbot.tac"

    while len(root) < 40 :
        if os.path.exists( root ):
            break
        root = "../" + root

    if not os.path.exists( root ):
        return None

    credentials = {}
    execfile(root, credentials, credentials)

    if "slavename" not in credentials:
        return None

    if "passwd" not in credentials:
        return None

    return ( credentials["slavename"] , credentials["passwd"] )

def do_output( dir ):

    def go_down( start ):
        lst = os.listdir(dir)
        if len(lst) != 1:
            raise Exception( "sad: " + start )
        return start + "/" + lst[0]

    while "out" not in os.listdir( dir ):
        dir = go_down( dir )

    dir = dir + "/out"

    pieces = os.listdir(dir)
    pieces.sort()

    passed = []
    failed = []
    times = {}

    for x in pieces:
        if not x.startswith( "part" ):
            continue
        full = dir + "/" + x

        for line in open( full , "rb" ):
            if line.find( "-passed" ) >= 0:
                passed.append( line.partition( "-passed" )[0] )
                continue

            if line.find( "-failed" ) >= 0:
                failed.append( line.partition( "-failed" )[0] )
                continue

            if line.find( "-time-seconds" ) >= 0:
                p = line.partition( "-time-seconds" )
                times[p[0]] = p[2].strip()
                continue

            print( "\t" + line.strip() )

    def print_list(name,lst):
        print( name )
        for x in lst:
            print( "\t%s\t%s" % ( x , times[x] ) )

    print_list( "passed" , passed )
    print_list( "failed" , failed )

    if do_credentials():
        builder , buildnum = get_build_info()
        if builder and buildnum:
            conn = pymongo.Connection( "bbout1.10gen.cc" )
            db = conn.buildlogs
            q = { "builder" : builder , "buildnum" : int(buildnum) }
            doc = db.builds.find_one( q )

            if doc:
                print( "\nhttp://buildlogs.mongodb.org/build/%s" % doc["_id"] )

if __name__ == "__main__":
    if len(sys.argv) == 1:
        print( "need an arg" )

    elif sys.argv[1] == "tarball":
        make_tarball()
    elif sys.argv[1] == "jar":
        build_jar()
    elif sys.argv[1] == "push":
        print( push() )

    elif sys.argv[1] == "sync":
        sync_s3( sys.argv[2] , sys.argv[3] )

    elif sys.argv[1] == "fix_suites":
        for x in fix_suites( sys.argv[2:] ):
            print(x)

    elif sys.argv[1] == "credentials":
        print( do_credentials() )

    elif sys.argv[1] == "test":
        m , c = make_tarball()
        build_jar()
        cmd = [ "java" , "-cp" , os.environ.get( "CLASSPATH" , "." ) + ":emr.jar" , "emr" ]

        workingDir = "/data/emr/test"
        cmd.append( "--workingDir" )
        cmd.append( workingDir )
        if os.path.exists( workingDir ):
            shutil.rmtree( workingDir )

        cmd.append( "file://" + os.getcwd() + "/" + m )
        cmd.append( "file://" + os.getcwd() + "/" + c )

        out = "/tmp/emrresults"
        cmd.append( out )
        if os.path.exists( out ):
            shutil.rmtree( out )

        cmd.append( "jstests/basic1.js" )

        subprocess.call( cmd )

        for x in os.listdir( out ):
            if x.startswith( "." ):
                continue
            print( x )
            for z in open( out + "/" + x ):
                print( "\t" + z.strip() )

    elif sys.argv[1] == "output":
        do_output( sys.argv[2] )

    elif sys.argv[1] == "full":
        things = push()
        run_tests( things , sys.argv[2:] )

    else:
        things = push()
        run_tests( things , sys.argv[1:] )


import sys
import heapq

def input(): return sys.stdin.readline().strip()

def dijkstra(start):
    distance = [1e11 for _ in range(n)]
    q = []

    distance[start] = 0
    heapq.heappush(q, (0, start))

    while q:
        dis, node = heapq.heappop(q)

        for cost, next in graph[node]:
            if distance[next] > distance[node] + cost:
                distance[next] = distance[node] + cost
                heapq.heappush(q, (distance[next], next))

    # 거리가 작으면 아이템 개수 더함
    return sum([items[i] for i in range(n) if distance[i] <= m])

n, m, r = map(int, input().split())
items = list(map(int, input().split()))

graph = [[] for _ in range(n)]

for _ in range(r):
    a, b, l = map(int, input().split())
    graph[a - 1].append((l, b - 1))
    graph[b - 1].append((l, a - 1))

print(max([dijkstra(i) for i in range(n)]))


from PIL import Image
from PIL import ImageOps
import numpy as np

def convert_images():
    for i in range(45):
        if i != 0:
            img = Image.open(f"train/train_conv_{i}.png")
            img = img.convert('L')
            img = img.resize((28, 28))

            img_array = np.array(img)

            # Set the threshold value
            threshold = 200
            if i == 1:
                print(img_array)

            height, width = img_array.shape

            # Iterate over all the pixels
            for x in range(width):
                for y in range(height):

                    if img_array[y, x] > threshold:
                        img_array[y, x] = 255
                    else:
                        img_array[y, x] = 0

            # Save the binary image
            Image.fromarray(img_array.astype(np.uint8)).save(f"train/train_conv_{i}.png")

            print(f"Converted {i} with {len(img.getbands())} bands; type: {img.mode}")


# truncated model
import torchvision.models as models
from torchvision import transforms
import torch
import torch.nn as nn
from sklearn.decomposition import PCA
class CustomResNet(nn.Module):
    def __init__(self, model,best_layer,layer_list):
        super(CustomResNet, self).__init__()
        self.model = model
        self.best_layer = best_layer
        self.layer_list = layer_list
        temp_index=layer_list.index(best_layer)
        # cut the model, only keep layers before the best layer
        rest_layers=layer_list[:temp_index+1]

        modules=[]
        modules.append(model.conv1)
        modules.append(model.bn1)
        modules.append(model.relu)

        for layer in rest_layers:
            exec(f'modules.append(model.{layer})')

        self.part=nn.Sequential(*modules)

    def forward(self, x):
        x = self.part(x)
        return x


# On the first line, you will receive a sequence of numbers separated by a single space.
# On the second line, you will receive a string.
# Your task is to write a program that sends a message only using chars from the given string.
# Each char the program adds to the message should be found by its index.
# The index you are looking for is the sum of a number's digits from the sequence.
# If the index is greater than the length of the text, continue counting from the beginning (so that you always have a
# valid index). When you find a char, you should add it to the message and remove it from the string.
# It means that for the following index, the text will be with one character less.
# Print the final message.

list_numbers = input().split()
initial_string = list(input())
index_list = []
message = ""

for number in list_numbers:
    index = 0
    for digit in number:
        index += int(digit)
    index_list.append(index)

for index in index_list:
    if index >= len(initial_string):
        index -= len(initial_string)
    message += initial_string[index]
    initial_string.remove(initial_string[index])

print(message)


import sys
input = sys.stdin.readline

N = int(input())

for i in range(N):
    word = ""
    check_error = 0
    enter = 0
    while True :
        buf = input()
        if buf != "\n" :
            word += buf
            enter += 1
        else :
            break
    for j in word :
        if j == "#" :
            check_error += 1
    length = len(word) - enter
    rate = round(((length - check_error)/length)*100, 1)
    rate = str(rate)
    if rate[-1] == "0" :
        rate = rate[: -2]
    rate += "%."
    print("Efficiency ratio is", rate)


"""Fancystore URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

from item.views import insert_item, display_view, update_item, delete_item,home_view

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home_view, name="home"),
    path('insert/',insert_item,name="insert"),
    path('display/', display_view, name="display"),
    path('update/<str:id>/', update_item, name="update"),
    path('delete/<int:id>/', delete_item, name="delete"),

]


import numpy as np

def predict(x):
    df = x.copy()
    output = []
    for index, row in df.iterrows():
        # Do not change the code before this point.
        # Please describe the process required to make the prediction below.

        # The logic here is to give higher probability for those who are female, in first class, and embarked from Cherbourg
        # These are usually the people who have higher survival rate based on historical data
        # The values are normalized by the maximum value in each column to make the probability between 0 and 1
        y = (row['sex_female'] / df['sex_female'].max() +
             row['class_First'] / df['class_First'].max() +
             row['embark_town_Cherbourg'] / df['embark_town_Cherbourg'].max()) / 3

        # Do not change the code after this point.
        output.append(y)
    return np.array(output)


# PREZENTACIJA 3
# Overall, this code loads a pre-trained model, performs object detection on an input image,
# and visualizes the results by drawing bounding boxes and
# displaying the detected class label and confidence level on the image.
import cv2
import numpy as np
from tensorflow.keras.models import load_model
# The code imports the necessary libraries: cv2 for image processing, numpy for numerical operations,
# and load_model from tensorflow.keras.models to load the trained model.

# Load the trained model
model = load_model('trained_model.h5')
# The code loads the pre-trained model from the file named 'trained_model.h5' and assigns it to the variable model.

# Define the class labels
class_labels = ['F1 car', 'Football ball', 'iPhone']

# This line defines the class labels for the objects that the model can detect.
# In this case, it contains the labels 'F1 car', 'Football ball', and 'iPhone'.

# Load the test image
image_path = 'Test images/40.png' # 22, 25, 30
image = cv2.imread(image_path)
image_height, image_width, _ = image.shape

# This code block specifies the path to the test image ('Test images/18.jpg'), reads the image using cv2.imread,
# and assigns it to the variable image. It also retrieves the height, width,
# and number of channels of the image using the shape attribute.

# Resize the image to match the size used during training
target_size = (224, 224)
resized_image = cv2.resize(image, target_size)

# The code sets the desired size for the input image (target_size) to (224, 224) pixels.
# It then resizes the image using cv2.resize to match the target_size and assigns it to the variable resized_image.

# Preprocess the image
preprocessed_image = resized_image.astype(np.float32) / 255.0
preprocessed_image = np.expand_dims(preprocessed_image, axis=0)

# This code block performs preprocessing on the resized image. First, it converts the pixel values of resized_image to
# floating-point numbers in the range [0, 1] by dividing by 255.0.
# Then, it adds an extra dimension to the image using np.expand_dims to match the input shape required by the model.

# Perform object detection
predictions = model.predict(preprocessed_image)
class_index = np.argmax(predictions)
class_label = class_labels[class_index]
confidence = predictions[0][class_index]

# The code uses the model to make predictions on the preprocessed image (preprocessed_image).
# The predictions are stored in the predictions variable.
# The np.argmax function is used to find the index of the class with the highest prediction probability.
# The corresponding class label is retrieved from class_labels and assigned to class_label.
# The confidence level (prediction probability) for the detected class is stored in confidence.

# Calculate the bounding box coordinates
bbox = predictions[0][:2] * np.array([target_size[1], target_size[0]])
bbox = bbox.astype(int)

# This code calculates the top-left coordinates of the bounding box based on the first two values of the predictions.
# It scales the values by the width and height of the target_size using np.array and element-wise multiplication.
# The resulting coordinates are then converted to integers using astype(int) and assigned to the variable bbox.

# Calculate the bottom right coordinates of the bounding box
bbox_bottom_right = bbox[:2] + predictions[0][2:4] * np.array([target_size[1], target_size[0]])
bbox_bottom_right = bbox_bottom_right.astype(int)

# Similar to the previous code block, this line calculates the bottom-right coordinates of the bounding box by adding
# the top-left coordinates (bbox[:2]) to the last two values of the predictions scaled by the target_size.
# The resulting coordinates are also converted to integers and stored in bbox_bottom_right.

# Scale the bounding box coordinates back to the original image size
scale_factor = (image_width / target_size[1], image_height / target_size[0])
bbox = (bbox * scale_factor).astype(int)
bbox_bottom_right = (bbox_bottom_right * scale_factor).astype(int)

# This code calculates the scale factors for width and height based on the ratio between the original image size and the target_size.
# It then scales the bounding box coordinates (bbox and bbox_bottom_right) back to the original image size
# by multiplying them with the corresponding scale factors. The coordinates are converted to integers using astype(int).

# Draw the rectangle and confidence level on the image
cv2.rectangle(image, (bbox[0], bbox[1]), (bbox_bottom_right[0], bbox_bottom_right[1]), (0, 255, 0), 2)
cv2.putText(image, f'{class_label}: {confidence:.2f}', (bbox[0], bbox[1] - 10),
            cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

# These lines draw a rectangle on the image using the calculated top-left and bottom-right coordinates.
# The rectangle is visualized as a green color ((0, 255, 0)) with a line thickness of 2 pixels.
# Additionally, it puts text on the image displaying the detected class label and confidence level near the top-left
# corner of the bounding box. The text is displayed in green color ((0, 255, 0)) with a font scale of 0.9 and a line thickness of 2 pixels.

# Print the detected class and confidence level
print('Detected Class:', class_label)
print('Confidence:', confidence)

# These lines print the detected class label and confidence level to the console.

# Display the image
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# These lines display the annotated image in a window titled 'Image' using cv2.imshow.
# It waits for a key press with cv2.waitKey(0), and then closes all windows using cv2.destroyAllWindows().


import tkinter
import time

def timeUpdate():
    clock = tkinter.StringVar(value=time.strftime('%H:%M:%S'))
    label.configure(textvariable=clock)
    root.after(50, timeUpdate)

root = tkinter.Tk()
root.title("Clock")

label = tkinter.Label(root,
    font = ("Modern", 200),
    bg = "black",
    fg = "red"
    )
label.pack(
    ipadx = 10,
    ipady = 10,
    fill = "both",
    expand = True
)

timeUpdate()

root.mainloop()


## When you run this code, images of cloud computing artcle from wikipedia will be downloaded and saved into files with names as image1.jpg etc..,
##source: https://en.wikipedia.org/wiki/Cloud_computing#:~:text=Cloud%20computing%20is%20the%20on,many%20users%20over%20the%20Internet
## Note this code may break if any wikipedia has any done any changes to the article's html

import requests,bs4

def save_images():
  result= requests.get('https://en.wikipedia.org/wiki/Cloud_computing#:~:text=Cloud%20computing%20is%20the%20on,many%20users%20over%20the%20Internet.')

  soup=bs4.BeautifulSoup(result.text,'lxml')
  link=soup.select('.thumbimage')
  count=0
  all=set()
  for i in link:
      all.add('https:'+i['src'])

  for i in all:
      count+=1
      name='image'+str(count)+'.jpg'
      image_link=requests.get(i)
      myfile= open(name,'wb')
      myfile.write(image_link.content)
      myfile.close()

if __name__ == '__main__':
    save_images()


# 038. Count and Say

class Solution:
    # 递归调用，将当前的ans作为helper的输入
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        return self.helper("1", 1, n)

    def helper(self, s, depth, n):
        if depth == n:
            return s
        if len(s) == 1:
            return self.helper("1" + s, 2, n)
        ans = ""
        i, j = 0, 1
        count = 1
        # 双指针，统计对于字符s[i]，有多少个连续元素相同(包括s[i])，将str(count) + s[i]加到ans中
        while j < len(s):
            if s[i] == s[j]:
                j += 1
                count += 1
            else:
                ans += str(count) + s[i]
                i = j
                j += 1
                count = 1
            # 此时遍历到字符串最后，将最后结果加入
            if j == len(s):
                ans += str(count) + s[i]

        return self.helper(ans, depth+1, n)


import numpy
import matplotlib.pyplot as pyplot

def f(D, a, b, c, e, rho):
    return a * D**3 + b * D**2 + c * D + e - rho

def f_dash(D, a, b, c):
    return 3 * a * D**2 + 2 * b * D + c

def lagrange4(s0, s1, s2, s3):
    return (
        -1 / 6 * s0 + 1 / 2 * s1 - 1 / 2 * s2 + 1 / 6 * s3,
        s0 - 5 / 2 * s1 + 2 * s2 - 1 / 2 * s3,
        -6 / 11 * s0 + 3 * s1 - 3 / 2 * s2 + 1 / 3 * s3,
        s0,
    )

def estimate(sign, rho, s0, s1, s2, s3):
    a, b, c, e = lagrange4(s0 - rho, s1 - rho, s2 - rho, s3 - rho)
    D = 1.5
    for _ in range(128):
        D = D - f(D, a, b, c, e, 0) / f_dash(D, a, b, c)
    return (
        D,
        f(D, a, b, c, e, 0),
        numpy.abs(f_dash(D, a, b, c)),
    )

def blamp_residual(d, slope):
    return (
        slope * (-d**5 / 120 + d**4 / 24 - d**3 / 12 + d**2 / 12 - d / 24 + 1 / 120),
        slope * (d**5 / 40 - d**4 / 12 + d**2 / 3 - d / 2 + 7 / 30),
        slope * (-d**5 / 40 + d**4 / 24 + d**3 / 12 + d**2 / 12 + d / 24 + 1 / 120),
        slope * (d**5 / 120),
    )

def ramp(x, scale):
    return scale * numpy.maximum(0, x)

def plot():
    fix = 1.25
    rho = 1.0
    scale = 1
    sign = 1
    x = numpy.arange(4, dtype=numpy.float64)
    sig = ramp(x - fix, scale) + rho

    x_frac = numpy.linspace(0, len(sig) - 1, 1024)
    true = ramp(x_frac - fix, scale) + rho

    a, b, c, e = lagrange4(*(sig - rho))
    interped = f(x_frac, a, b, c, e, 0) + rho

    D, Dy, slope = estimate(sign, rho, *sig)
    blamp = sig + blamp_residual(D - 1, slope)

    pyplot.title(f"Estimation of $D$ (ρ = {rho})")
    pyplot.plot(x_frac, true, label="Source", color="gray", ls="--", alpha=0.5, zorder=4)
    pyplot.plot(x_frac, interped, label="Lagrange", color="black", zorder=4)
    pyplot.scatter(x, sig, label="Quantized Signal", color="gray", zorder=5)
    pyplot.scatter(x, blamp, label="Result", color="black", marker="x", zorder=5)
    pyplot.scatter(D, rho, label=f"D = {D:.3f}", color="red", marker="+", zorder=5)
    pyplot.scatter(
        fix, rho, label=f"true D = {fix:.3f}", color="blue", marker="^", zorder=5)
    pyplot.grid(zorder=1)
    pyplot.legend()
    pyplot.show()

def plot_edgecase():
    last = 1
    rho = -0.3
    sign = 1
    x = numpy.arange(4, dtype=numpy.float64)
    sig = numpy.array([-0.300, -0.300, -0.001, -0.150])

    x_frac = numpy.linspace(0, len(sig) - 1, 1024)
    a, b, c, e = lagrange4(*(sig - rho))
    interped = f(x_frac, a, b, c, e, 0) + rho

    D, Dy, slope = estimate(sign, rho, *sig)
    blamp = sig + blamp_residual(D - 1, slope)

    pyplot.title(f"Estimation of $D$ (ρ = {rho})")
    pyplot.plot(x_frac, interped, label="Lagrange", color="black", zorder=4)
    pyplot.scatter(x, sig, label="signal", color="gray", zorder=5)
    pyplot.scatter(x, blamp, label="BLAMP", color="black", marker="x", zorder=5)
    pyplot.scatter(D, rho, label=f"D = {D}", color="red", marker="+", zorder=5)
    pyplot.grid(zorder=1)
    pyplot.legend()
    pyplot.show()

# plot()
plot_edgecase()


no_data_value = 99999

weather_lookup_codes = {
    'NA':	'Not available',
    '0':	'Clear night',
    '1':	'Sunny day',
    '2':	'Partly cloudy (night)',
    '3':	'Partly cloudy (day)',
    '4':	'Not used',
    '5':	'Mist',
    '6':	'Fog',
    '7':	'Cloudy',
    '8':	'Overcast',
    '9':	'Light rain shower (night)',
    '10':	'Light rain shower (day)',
    '11':	'Drizzle',
    '12':	'Light rain',
    '13':	'Heavy rain shower (night)',
    '14':	'Heavy rain shower (day)',
    '15':	'Heavy rain',
    '16':	'Sleet shower (night)',
    '17':	'Sleet shower (day)',
    '18':	'Sleet',
    '19':	'Hail shower (night)',
    '20':	'Hail shower (day)',
    '21':	'Hail',
    '22':	'Light snow shower (night)',
    '23':	'Light snow shower (day)',
    '24':	'Light snow',
    '25':	'Heavy snow shower (night)',
    '26':	'Heavy snow shower (day)',
    '27':	'Heavy snow',
    '28':	'Thunder shower (night)',
    '29':	'Thunder shower (day)',
    '30':	'Thunder',
}
visibility_lookup_codes = {
    'UN': 	'Unknown',
    'VP': 	'Very poor - Less than 1 km',
    'PO': 	'Poor - Between 1-4 km',
    'MO': 	'Moderate - Between 4-10 km',
    'GO': 	'Good - Between 10-20 km',
    'VG': 	'Very good - Between 20-40 km',
    'EX': 	'Excellent - More than 40 km',
}
uv_lookup_codes = {
    '0':  	'Low exposure. No protection required. You can safely stay outside',
    '1':  	'Low exposure. No protection required. You can safely stay outside',
    '2':  	'Low exposure. No protection required. You can safely stay outside',
    '3':  	'Moderate exposure. Seek shade during midday hours, cover up and wear sunscreen',
    '4':  	'Moderate exposure. Seek shade during midday hours, cover up and wear sunscreen',
    '5':  	'Moderate exposure. Seek shade during midday hours, cover up and wear sunscreen',
    '6':  	'High exposure. Seek shade during midday hours, cover up and wear sunscreen',
    '7':  	'High exposure. Seek shade during midday hours, cover up and wear sunscreen',
    '8':  	'Very high. Avoid being outside during midday hours. Shirt, sunscreen and hat are essential',
    '9':  	'Very high. Avoid being outside during midday hours. Shirt, sunscreen and hat are essential',
    '10':  	'Very high. Avoid being outside during midday hours. Shirt, sunscreen and hat are essential',
    '11':  'Extreme. Avoid being outside during midday hours. Shirt, sunscreen and hat essential.',
}

def open_weather_visibility_lookup(visibility_metres: int) -> str:
    """ Returns a string of the visibility based on a the visibility in metres """
    if visibility_metres < 1000:
        return 'Very poor - Less than 1 km'
    elif visibility_metres < 4000:
        'Poor - Between 1-4 km'
    elif visibility_metres < 10000:
        return 'Moderate - Between 4-10 km'
    elif visibility_metres < 20000:
        return 'Good - Between 10-20 km'
    elif visibility_metres < 40000:
        return 'Very good - Between 20-40 km'
    elif visibility_metres < 100000:
        return 'Excellent - More than 40 km'
    else:
        return 'Unknown'

def open_weather_wind_direction_lookup(direction_deg: int) -> str:
    if 330 <= direction_deg <= 25:
        return 'N'
    elif 25 <= direction_deg <= 60:
        return 'NE'
    elif 60 <= direction_deg <= 120:
        return 'E'
    elif 120 <= direction_deg <= 160:
        return 'SE'
    elif 160 <= direction_deg <= 200:
        return 'S'
    elif 200 <= direction_deg <= 240:
        return 'SW'
    elif 240 <= direction_deg <= 280:
        return 'W'
    elif 280 <= direction_deg <= 330:
        return 'NW'
    else:
        return 'Unknown'

accuweather_weather_code_lookup = {1: 'Sunny',
                                   2: 'Mostly Sunny',
                                   3: 'Partly Sunny',
                                   4: 'Intermittent Clouds',
                                   5: 'Hazy Sunshine',
                                   6: 'Mostly Cloudy',
                                   7: 'Cloudy',
                                   8: 'Dreary (Overcast)',
                                   11: 'Fog',
                                   12: 'Showers',
                                   13: 'Mostly Cloudy w/ Showers',
                                   14: 'Partly Sunny w/ Showers',
                                   15: 'T-Storms',
                                   16: 'Mostly Cloudy w/ T-Storms',
                                   17: 'Partly Sunny w/ T-Storms',
                                   18: 'Rain',
                                   19: 'Flurries',
                                   20: 'Mostly Cloudy w/ Flurries',
                                   21: 'Partly Sunny w/ Flurries',
                                   22: 'Snow',
                                   23: 'Mostly Cloudy w/ Snow',
                                   24: 'Ice',
                                   25: 'Sleet',
                                   26: 'Freezing Rain',
                                   29: 'Rain and Snow',
                                   30: 'Hot',
                                   31: 'Cold',
                                   32: 'Windy',
                                   33: 'Clear',
                                   34: 'Mostly Clear',
                                   35: 'Partly Cloudy',
                                   36: 'Intermittent Clouds',
                                   37: 'Hazy Moonlight',
                                   38: 'Mostly Cloudy',
                                   39: 'Partly Cloudy w/ Showers',
                                   40: 'Mostly Cloudy w/ Showers',
                                   41: 'Partly Cloudy w/ T-Storms',
                                   42: 'Mostly Cloudy w/ T-Storms',
                                   43: 'Mostly Cloudy w/ Flurries',
                                   44: 'Mostly Cloudy w/ Snow'}

tomorrow_io_weather_code_lookup = {
    0: 'Unknown',
    1000: 'Clear',
    1001: 'Cloudy',
    1100: 'Mostly Clear',
    1101: 'Partly Cloudy',
    1102: 'Mostly Cloudy',
    2000: 'Fog',
    2100: 'Light Fog',
    3000: 'Light Wind',
    3001: 'Wind',
    3002: 'Strong Wind',
    4000: 'Drizzle',
    4001: 'Rain',
    4200: 'Light Rain',
    4201: 'Heavy Rain',
    5000: 'Snow',
    5001: 'Flurries',
    5100: 'Light Snow',
    5101: 'Heavy Snow',
    6000: 'Freezing Drizzle',
    6001: 'Freezing Rain',
    6200: 'Light Freezing Rain',
    6201: 'Heavy Freezing Rain',
    7000: 'Ice Pellets',
    7101: 'Heavy Ice Pellets',
    7102: 'Light Ice Pellets',
    8000: 'Thunderstorm',
}

weatherbit_wind_direction_lookup = {'north':  'N',
                                    'north-northeast':  'NNE',
                                    'northeast':  'NE',
                                    'east-northeast':  'ENE',
                                    'east':  'E',
                                    'east-southeast':  'ESE',
                                    'southeast':  'SE',
                                    'south-southeast':  'SSE',
                                    'south':  'S',
                                    'south-southwest':  'SSW',
                                    'southwest':  'SW',
                                    'west-southwest':  'WSW',
                                    'west':  'W',
                                    'west-northwest': 'WNW',
                                    'northwest': 'NW',
                                    'north-northwest':  'NNW'
                                    }

weatherbit_weather_code_lookup = {
    '200':	'Thunderstorm with light rain',
    '201':	'Thunderstorm with rain',
    '202':	'Thunderstorm with heavy rain',
    '230':	'Thunderstorm with light drizzle',
    '231':	'Thunderstorm with drizzle',
    '232':	'Thunderstorm with heavy drizzle',
    '233':	'Thunderstorm with Hail',
    '300':	'Light Drizzle',
    '301':	'Drizzle',
    '302':	'Heavy Drizzle',
    '500':	'Light Rain',
    '501':	'Moderate Rain',
    '502':	'Heavy Rain',
    '511':	'Freezing rain',
    '520':	'Light shower rain',
    '521':	'Shower rain',
    '522':	'Heavy shower rain',
    '600':	'Light snow',
    '601':	'Snow',
    '602':	'Heavy Snow',
    '610':	'Mix snow/rain',
    '611':	'Sleet',
    '612':	'Heavy sleet',
    '621':	'Snow shower',
    '622':	'Heavy snow shower',
    '623':	'Flurries',
    '700':	'Mist',
    '711':	'Smoke',
    '721':	'Haze',
    '731':	'Sand/dust',
    '741':	'Fog',
    '751':	'Freezing Fog',
    '800':	'Clear sky',
    '801':	'Few clouds',
    '802':	'Scattered clouds',
    '803':	'Broken clouds',
    '804':	'Overcast clouds',
    '900':	'Unknown Precipitation',
}


from flask import jsonify
from ia.check_data import Check
from ia.transform_data import TransformData
from ia.load_model import LoadModel
from ia.evaluate_model import EvaluateModel

class Ia:
    def run(self, data):
        try:
            check = Check()
            check_results = check.check_file(data)
            if all(value for value in check_results.values()):
                transformData = TransformData()
                processed_data = transformData.transformation_and_tensor(data)
                model_cnn = LoadModel()
                classification = EvaluateModel(model_cnn, processed_data).evaluate()

            else:
                return jsonify({'error': 'O arquivo não passou nas verificações'})

            return jsonify({'success': 'Classificação realizada com sucesso', 'result':classification})

        except Exception as e:
            # Lidando com exceções não tratadas, como problemas de leitura de arquivo, erros de transformação, etc.
            return jsonify({'error': f'Ocorreu um erro durante o processamento: {str(e)}'})


# -*- coding: utf-8 -*-
# Created by David on 2016/5/4.

import sys
import random
import time
import re
import urllib

reload(sys)
sys.path.append('./util')
from CrawlerBase import CrawlerBase
from lxml import etree
from ModuleManager import Module, Event, Iterator, Router
from util.crawler_util import CrawlerRunMode, InputType, OutputType, EventType, OutputParameterShowUpType
from CommonLib.WebContent import WebAccessType, SeedAccessType
from guangdongs.CrawlerGdQyxx import CrawlerGdQyxx
from guangdongs.CrawlerGdQyxy import CrawlerGdQyxy
from guangdongs.CrawlerSzxy import CrawlerSzxy

class CrawlerGuangdong(CrawlerBase):
    def __init__(self, pinyin, callbackFromOuterControl):
        config_dict = dict()
        config_dict[CrawlerRunMode.COMPANY_KEY] = [self.initConfigValidateCode, self.initPostParam,
                                                   self.initConfigSearchList, self.initConfigCompanyInfo]
        config_dict[CrawlerRunMode.COMPANY_URL] = [self.initRouter]
        check_dict = dict()
        check_dict['html_check_dict'] = {'过于频繁':WebAccessType.TOO_OFTEN, '非正常访问':WebAccessType.ACCESS_VIOLATION }
        CrawlerBase.__init__(self, pinyin, config_dict, check_dict, callbackFromOuterControl)
        # 打开解析开关
        self.parse_on = True
        pass

    def initConfigValidateCode(self):
        module = Module(self.visitValidateCode, "验证码")
        module.module_id = "module_validate_code"
        module.appendUrl("http://gsxt.gdgs.gov.cn/aiccips/verify.html?random=" + str(random.random()))
        module.appendHeaders(
            {'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Accept-Encoding': 'gzip, deflate',
             'Connection': 'keep-alive', 'Accept': 'image/png,image/*;q=0.8,*/*;q=0.5',
             'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0',
             'Host': 'gsxt.gdgs.gov.cn', 'Referer': 'http://gsxt.gdgs.gov.cn/aiccips/'})
        module.addEvent(Event(EventType.EXCEPTION_OCCURED, retry_times=100))
        self.module_manager.appendSubModule(module)

    def initPostParam(self):
        module = Module(self.getJson, "json中间结果")
        module.appendUrl('http://gsxt.gdgs.gov.cn/aiccips/CheckEntContext/checkCode.html')
        module.appendHeaders(
            {'Accept-Language': 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3', 'Accept-Encoding': 'gzip, deflate',
             'Connection': 'keep-alive', 'X-Requested-With': 'XMLHttpRequest',
             'Accept': 'application/json, text/javascript, */*; q=0.01',
             'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0',
             'Host': 'gsxt.gdgs.gov.cn', 'Referer': 'http://gsxt.gdgs.gov.cn/aiccips/', 'Pragma': 'no-cache',
             'Cache-Control': 'no-cache', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'})
        module.appendWebMethod("post")
        module.appendPostData(lambda company_key, yzm: {"textfield": company_key, "code": yzm})

        def postSearchListData(json, yzm):
            if 'textfield' not in json:
                return None
            textfield = json['textfield']
            data = {
                "textfield": textfield,
                "code": yzm
            }
            return data

        module.appendOutput(name="post_data", type=OutputType.FUNCTION, function=postSearchListData)
        module.addEvent(Event(EventType.OUTPUT_NOT_SATISFIED, redo_module="module_validate_code"))
        module.addEvent(Event(EventType.EXCEPTION_OCCURED, redo_module="module_validate_code"))

        def postDataJsonAssert(json=None):
            if not json:
                return False
            if 'flag' not in json or json['flag'] != '1':
                return False
            return True

        module.addEvent(
            Event(EventType.ASSERT_FAILED, retry_times=1000, assert_function=postDataJsonAssert, redo_module="module_validate_code"))
        self.module_manager.appendSubModule(module)

    def initConfigSearchList(self):
        module = Module(self.visitSearchList, "搜索列表")
        module.appendUrl('http://gsxt.gdgs.gov.cn/aiccips/CheckEntContext/showInfo.html')
        module.appendHeaders({'Host': 'gsxt.gdgs.gov.cn', 'Accept-Language': 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
                              'Accept-Encoding': 'gzip, deflate', 'Referer': 'http://gsxt.gdgs.gov.cn/aiccips/',
                              'Content-Type': 'application/x-www-form-urlencoded', 'Connection': 'keep-alive',
                              'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                              'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0'})
        module.appendWebMethod("post")
        module.appendPostData("post_data")
        module.appendOutput("search_list", ".//*[@class='list']", OutputType.LIST)
        module.addEvent(Event(EventType.EXCEPTION_OCCURED, retry_times=100, redo_module="module_validate_code"))
        module.addEvent(Event(EventType.OUTPUT_NOT_SATISFIED, retry_times=100, redo_module="module_validate_code"))
        module.addEvent(Event(EventType.ASSERT_FAILED, retry_times=0, assert_function=lambda:False if self.report.access_type == SeedAccessType.NON_COMPANY else True))
        self.module_manager.appendSubModule(module)

    def initConfigCompanyInfo(self):
        iterator = Iterator("search_list", "info")
        module = Module(None, "获取公司信息", iterator)
        self.module_manager.appendSubModule(module, True)

        self.initCompanyPrepare(module)
        self.initRouter(module)

    def initCompanyPrepare(self, module_super):
        module = Module(name="抓取公司前的预处理")
        def company_info_prepare(info):
            try:
                company_list_name = info.xpath('.//a/text()')[0].strip()
            except Exception as e:
                company_list_name = ''

            company_url = ''
            company_url_list = info.xpath(".//a")
            if company_url_list:
                if isinstance(company_url_list, list):
                    company_url = company_url_list[0].get("href")

            if '../GSpublicity/' in company_url:
                company_url = 'http://gsxt.gdgs.gov.cn/aiccips' + company_url[2:]

            return {"company_url": company_url, "company_name": company_list_name,
                    "search_company": company_list_name}
        module.appendOutput(type=OutputType.FUNCTION, function=company_info_prepare)
        module_super.appendSubModule(module)

    def initRouter(self, module_super):
        module = Module(None, "广东公司适配", router=Router())

        def source_prepare(company_url):
            source = ''
            if 'gsxt.gzaic.gov.cn' in company_url:
                source = u"企业信用网"
            elif '/GSpublicity/' in company_url:
                source = u"企业信息网"
            elif 'szcredit' in company_url:
                source = u"深圳信用网"
            else:
                source = u"企业信用网"

            self.page_dict['source'] = source

            return {"source": source}

        module.appendInput(InputType.FUNCTION, source_prepare)

        qyxx = CrawlerGdQyxx(self.pinyin, self)
        module.appendSubModule(qyxx.module_manager.getFirstModule())
        qyxy = CrawlerGdQyxy(self.pinyin, self)
        module.appendSubModule(qyxy.module_manager.getFirstModule())
        szxy = CrawlerSzxy(self.pinyin, self)
        module.appendSubModule(szxy.module_manager.getFirstModule())
        def shenzhenAssert(source):
            if not source or source == u"深圳信用网":
                self.report.access_type = SeedAccessType.NO_TARGET_SOURCE
                return False
            return True
        module.addEvent(Event(event_type=EventType.ASSERT_FAILED, retry_times=0, assert_function=shenzhenAssert))
        module_super.appendSubModule(module, True)


import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
numlist = [2, 10, 40]

#Perform Singular Value Decomposition for the image attached. This is a useful tool for image compression; we note that the first 40 values capture the majority of the image content.
X=np.asarray(Image.open('harvey-saturday-goes7am.jpg').convert('L'))
U, sigma, V = np.linalg.svd(X)

for i in numlist:
    print("i is "),
    print(i)
    plt.figure(i)
    minsize = min(U.shape[0], V.shape[0])
    signew = np.zeros((U.shape[0], V.shape[0]))
    signew[:i, :i] = np.diag(sigma[:i])
    newimg = np.matrix(U)*np.matrix(signew)*np.matrix(V)
    plt.imshow(newimg, cmap='gray')
    title = "n = %s" % i
    plt.title(title)

    diffnorm =np.linalg.norm(X-newimg)/np.linalg.norm(X)
    print("Normalized difference is "),
    print(diffnorm)
plt.show()


# This is a simple program to show how to use PyCdlib to extract data from the
# ISO.

# Import standard python modules.
import sys
try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

# Import pycdlib itself.
import pycdlib

# Check that there are enough command-line arguments.
if len(sys.argv) != 1:
    print('Usage: %s' % (sys.argv[0]))
    sys.exit(1)

# First we'll create a new ISO and write it out (see create-new.py for more
# information about these steps).
iso = pycdlib.PyCdlib()
iso.new()
foostr = b'foo\n'
iso.add_fp(BytesIO(foostr), len(foostr), '/FOO.;1')
out = BytesIO()
iso.write_fp(out)
iso.close()

# Now, let's open up the ISO and extract the contents of the FOO.;1 file.
iso.open_fp(out)
extracted = BytesIO()
# Use the get_file_from_iso_fp() API to extract the named filename into the file
# descriptor.
iso.get_file_from_iso_fp(extracted, iso_path='/FOO.;1')
iso.close()

print(extracted.getvalue().decode('utf-8'))