"""
The source code is adapted from https://github.com/aliaksandr960/segment-anything-eo. Credit to the author Aliaksandr Hancharenka.
"""

import os
import tempfile
import cv2
import numpy as np
import rasterio
from tqdm import tqdm

import shapely
import geopandas as gpd
import rasterio
from rasterio import features

def check_file_path(file_path, make_dirs=True):
    """Gets the absolute file path.

    Args:
        file_path (str): The path to the file.
        make_dirs (bool, optional): Whether to create the directory if it does not exist. Defaults to True.

    Raises:
        FileNotFoundError: If the directory could not be found.
        TypeError: If the input directory path is not a string.

    Returns:
        str: The absolute path to the file.
    """
    if isinstance(file_path, str):
        if file_path.startswith("~"):
            file_path = os.path.expanduser(file_path)
        else:
            file_path = os.path.abspath(file_path)

        file_dir = os.path.dirname(file_path)
        if not os.path.exists(file_dir) and make_dirs:
            os.makedirs(file_dir)

        return file_path

    else:
        raise TypeError("The provided file path must be a string.")

def temp_file_path(extension):
    """Returns a temporary file path.

    Args:
        extension (str): The file extension.

    Returns:
        str: The temporary file path.
    """

    import tempfile
    import uuid

    if not extension.startswith("."):
        extension = "." + extension
    file_id = str(uuid.uuid4())
    file_path = os.path.join(tempfile.gettempdir(), f"{file_id}{extension}")

    return file_path

def github_raw_url(url):
    """Get the raw URL for a GitHub file.

    Args:
        url (str): The GitHub URL.
    Returns:
        str: The raw URL.
    """
    if isinstance(url, str) and url.startswith("https://github.com/") and "blob" in url:
        url = url.replace("github.com", "raw.githubusercontent.com").replace(
            "blob/", ""
        )
    return url

def download_file(
    url=None,
    output=None,
    quiet=False,
    proxy=None,
    speed=None,
    use_cookies=True,
    verify=True,
    id=None,
    fuzzy=False,
    resume=False,
    unzip=True,
    overwrite=False,
    subfolder=False,
):
    """Download a file from URL, including Google Drive shared URL.

    Args:
        url (str, optional): Google Drive URL is also supported. Defaults to None.
        output (str, optional): Output filename. Default is basename of URL.
        quiet (bool, optional): Suppress terminal output. Default is False.
        proxy (str, optional): Proxy. Defaults to None.
        speed (float, optional): Download byte size per second (e.g., 256KB/s = 256 * 1024). Defaults to None.
        use_cookies (bool, optional): Flag to use cookies. Defaults to True.
        verify (bool | str, optional): Either a bool, in which case it controls whether the server's TLS certificate is verified, or a string,
            in which case it must be a path to a CA bundle to use. Default is True.. Defaults to True.
        id (str, optional): Google Drive's file ID. Defaults to None.
        fuzzy (bool, optional): Fuzzy extraction of Google Drive's file Id. Defaults to False.
        resume (bool, optional): Resume the download from existing tmp file if possible. Defaults to False.
        unzip (bool, optional): Unzip the file. Defaults to True.
        overwrite (bool, optional): Overwrite the file if it already exists. Defaults to False.
        subfolder (bool, optional): Create a subfolder with the same name as the file. Defaults to False.

    Returns:
        str: The output file path.
    """
    import zipfile

    try:
        import gdown
    except ImportError:
        print(
            "The gdown package is required for this function. Use `pip install gdown` to install it."
        )
        return

    if output is None:
        if isinstance(url, str) and url.startswith("http"):
            output = os.path.basename(url)

    out_dir = os.path.abspath(os.path.dirname(output))
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    if isinstance(url, str):
        if os.path.exists(os.path.abspath(output)) and (not overwrite):
            print(
                f"{output} already exists. Skip downloading. Set overwrite=True to overwrite."
            )
            return os.path.abspath(output)
        else:
            url = github_raw_url(url)

    if "https://drive.google.com/file/d/" in url:
        fuzzy = True

    output = gdown.download(
        url, output, quiet, proxy, speed, use_cookies, verify, id, fuzzy, resume
    )

    if unzip and output.endswith(".zip"):
        with zipfile.ZipFile(output, "r") as zip_ref:
            if not quiet:
                print("Extracting files...")
            if subfolder:
                basename = os.path.splitext(os.path.basename(output))[0]

                output = os.path.join(out_dir, basename)
                if not os.path.exists(output):
                    os.makedirs(output)
                zip_ref.extractall(output)
            else:
                zip_ref.extractall(os.path.dirname(output))

    return os.path.abspath(output)

def download_checkpoint(url=None, output=None, overwrite=False, **kwargs):
    """Download a checkpoint from URL. It can be one of the following: sam_vit_h_4b8939.pth, sam_vit_l_0b3195.pth, sam_vit_b_01ec64.pth.

    Args:
        url (str, optional): The checkpoint URL. Defaults to None.
        output (str, optional): The output file path. Defaults to None.
        overwrite (bool, optional): Overwrite the file if it already exists. Defaults to False.

    Returns:
        str: The output file path.
    """
    checkpoints = {
        'sam_vit_h_4b8939.pth': "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth",
        'sam_vit_l_0b3195.pth': "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth",
        'sam_vit_b_01ec64.pth': "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth",
    }

    if isinstance(url, str) and url in checkpoints:
        url = checkpoints[url]

    if url is None:
        url = checkpoints['sam_vit_h_4b8939.pth']

    if output is None:
        output = os.path.basename(url)

    return download_file(url, output, overwrite=overwrite, **kwargs)

def image_to_cog(source, dst_path=None, profile="deflate", **kwargs):
    """Converts an image to a COG file.

    Args:
        source (str): A dataset path, URL or rasterio.io.DatasetReader object.
        dst_path (str, optional): An output dataset path or or PathLike object. Defaults to None.
        profile (str, optional): COG profile. More at https://cogeotiff.github.io/rio-cogeo/profile. Defaults to "deflate".

    Raises:
        ImportError: If rio-cogeo is not installed.
        FileNotFoundError: If the source file could not be found.
    """
    try:
        from rio_cogeo.cogeo import cog_translate
        from rio_cogeo.profiles import cog_profiles

    except ImportError:
        raise ImportError(
            "The rio-cogeo package is not installed. Please install it with `pip install rio-cogeo` or `conda install rio-cogeo -c conda-forge`."
        )

    if not source.startswith("http"):
        source = check_file_path(source)

        if not os.path.exists(source):
            raise FileNotFoundError("The provided input file could not be found.")

    if dst_path is None:
        if not source.startswith("http"):
            dst_path = os.path.splitext(source)[0] + "_cog.tif"
        else:
            dst_path = temp_file_path(extension=".tif")

    dst_path = check_file_path(dst_path)

    dst_profile = cog_profiles.get(profile)
    cog_translate(source, dst_path, dst_profile, **kwargs)

def tms_to_geotiff(
    output,
    bbox,
    zoom=None,
    resolution=None,
    source="OpenStreetMap",
    to_cog=False,
    return_image=False,
    overwrite=False,
    quiet=False,
    **kwargs,
):
    """Download TMS tiles and convert them to a GeoTIFF. The source is adapted from https://github.com/gumblex/tms2geotiff.
        Credits to the GitHub user @gumblex.

    Args:
        output (str): The output GeoTIFF file.
        bbox (list): The bounding box [minx, miny, maxx, maxy], e.g., [-122.5216, 37.733, -122.3661, 37.8095]
        zoom (int, optional): The map zoom level. Defaults to None.
        resolution (float, optional): The resolution in meters. Defaults to None.
        source (str, optional): The tile source. It can be one of the following: "OPENSTREETMAP", "ROADMAP",
            "SATELLITE", "TERRAIN", "HYBRID", or an HTTP URL. Defaults to "OpenStreetMap".
        to_cog (bool, optional): Convert to Cloud Optimized GeoTIFF. Defaults to False.
        return_image (bool, optional): Return the image as PIL.Image. Defaults to False.
        overwrite (bool, optional): Overwrite the output file if it already exists. Defaults to False.
        quiet (bool, optional): Suppress output. Defaults to False.
        **kwargs: Additional arguments to pass to gdal.GetDriverByName("GTiff").Create().

    """

    import os
    import io
    import math
    import itertools
    import concurrent.futures

    import numpy
    from PIL import Image

    try:
        from osgeo import gdal, osr
    except ImportError:
        raise ImportError("GDAL is not installed. Install it with pip install GDAL")

    try:
        import httpx

        SESSION = httpx.Client()
    except ImportError:
        import requests

        SESSION = requests.Session()

    if not overwrite and os.path.exists(output):
        print(
            f"The output file {output} already exists. Use `overwrite=True` to overwrite it."
        )
        return

    xyz_tiles = {
        "OPENSTREETMAP": {
            "url": "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
            "attribution": "OpenStreetMap",
            "name": "OpenStreetMap",
        },
        "ROADMAP": {
            "url": "https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
            "attribution": "Google",
            "name": "Google Maps",
        },
        "SATELLITE": {
            "url": "https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}",
            "attribution": "Google",
            "name": "Google Satellite",
        },
        "TERRAIN": {
            "url": "https://mt1.google.com/vt/lyrs=p&x={x}&y={y}&z={z}",
            "attribution": "Google",
            "name": "Google Terrain",
        },
        "HYBRID": {
            "url": "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",
            "attribution": "Google",
            "name": "Google Satellite",
        },
    }

    if isinstance(source, str) and source.upper() in xyz_tiles:
        source = xyz_tiles[source.upper()]["url"]
    elif isinstance(source, str) and source.startswith("http"):
        pass
    else:
        raise ValueError(
            'source must be one of "OpenStreetMap", "ROADMAP", "SATELLITE", "TERRAIN", "HYBRID", or a URL'
        )

    def resolution_to_zoom_level(resolution):
        """
        Convert map resolution in meters to zoom level for Web Mercator (EPSG:3857) tiles.
        """
        # Web Mercator tile size in meters at zoom level 0
        initial_resolution = 156543.03392804097

        # Calculate the zoom level
        zoom_level = math.log2(initial_resolution / resolution)

        return int(zoom_level)

    if isinstance(bbox, list) and len(bbox) == 4:
        west, south, east, north = bbox
    else:
        raise ValueError(
            "bbox must be a list of 4 coordinates in the format of [xmin, ymin, xmax, ymax]"
        )

    if zoom is None and resolution is None:
        raise ValueError("Either zoom or resolution must be provided")
    elif zoom is not None and resolution is not None:
        raise ValueError("Only one of zoom or resolution can be provided")

    if resolution is not None:
        zoom = resolution_to_zoom_level(resolution)

    EARTH_EQUATORIAL_RADIUS = 6378137.0

    Image.MAX_IMAGE_PIXELS = None

    gdal.UseExceptions()
    web_mercator = osr.SpatialReference()
    web_mercator.ImportFromEPSG(3857)

    WKT_3857 = web_mercator.ExportToWkt()

    def from4326_to3857(lat, lon):
        xtile = math.radians(lon) * EARTH_EQUATORIAL_RADIUS
        ytile = (
            math.log(math.tan(math.radians(45 + lat / 2.0))) * EARTH_EQUATORIAL_RADIUS
        )
        return (xtile, ytile)

    def deg2num(lat, lon, zoom):
        lat_r = math.radians(lat)
        n = 2**zoom
        xtile = (lon + 180) / 360 * n
        ytile = (1 - math.log(math.tan(lat_r) + 1 / math.cos(lat_r)) / math.pi) / 2 * n
        return (xtile, ytile)

    def is_empty(im):
        extrema = im.getextrema()
        if len(extrema) >= 3:
            if len(extrema) > 3 and extrema[-1] == (0, 0):
                return True
            for ext in extrema[:3]:
                if ext != (0, 0):
                    return False
            return True
        else:
            return extrema[0] == (0, 0)

    def paste_tile(bigim, base_size, tile, corner_xy, bbox):
        if tile is None:
            return bigim
        im = Image.open(io.BytesIO(tile))
        mode = "RGB" if im.mode == "RGB" else "RGBA"
        size = im.size
        if bigim is None:
            base_size[0] = size[0]
            base_size[1] = size[1]
            newim = Image.new(
                mode, (size[0] * (bbox[2] - bbox[0]), size[1] * (bbox[3] - bbox[1]))
            )
        else:
            newim = bigim

        dx = abs(corner_xy[0] - bbox[0])
        dy = abs(corner_xy[1] - bbox[1])
        xy0 = (size[0] * dx, size[1] * dy)
        if mode == "RGB":
            newim.paste(im, xy0)
        else:
            if im.mode != mode:
                im = im.convert(mode)
            if not is_empty(im):
                newim.paste(im, xy0)
        im.close()
        return newim

    def finish_picture(bigim, base_size, bbox, x0, y0, x1, y1):
        xfrac = x0 - bbox[0]
        yfrac = y0 - bbox[1]
        x2 = round(base_size[0] * xfrac)
        y2 = round(base_size[1] * yfrac)
        imgw = round(base_size[0] * (x1 - x0))
        imgh = round(base_size[1] * (y1 - y0))
        retim = bigim.crop((x2, y2, x2 + imgw, y2 + imgh))
        if retim.mode == "RGBA" and retim.getextrema()[3] == (255, 255):
            retim = retim.convert("RGB")
        bigim.close()
        return retim

    def get_tile(url):
        retry = 3
        while 1:
            try:
                r = SESSION.get(url, timeout=60)
                break
            except Exception:
                retry -= 1
                if not retry:
                    raise
        if r.status_code == 404:
            return None
        elif not r.content:
            return None
        r.raise_for_status()
        return r.content

    def draw_tile(
        source, lat0, lon0, lat1, lon1, zoom, filename, quiet=False, **kwargs
    ):
        x0, y0 = deg2num(lat0, lon0, zoom)
        x1, y1 = deg2num(lat1, lon1, zoom)
        if x0 > x1:
            x0, x1 = x1, x0
        if y0 > y1:
            y0, y1 = y1, y0
        corners = tuple(
            itertools.product(
                range(math.floor(x0), math.ceil(x1)),
                range(math.floor(y0), math.ceil(y1)),
            )
        )
        totalnum = len(corners)
        futures = []
        with concurrent.futures.ThreadPoolExecutor(5) as executor:
            for x, y in corners:
                futures.append(
                    executor.submit(get_tile, source.format(z=zoom, x=x, y=y))
                )
            bbox = (math.floor(x0), math.floor(y0), math.ceil(x1), math.ceil(y1))
            bigim = None
            base_size = [256, 256]
            for k, (fut, corner_xy) in enumerate(zip(futures, corners), 1):
                bigim = paste_tile(bigim, base_size, fut.result(), corner_xy, bbox)
                if not quiet:
                    print("Downloaded image %d/%d" % (k, totalnum))

        if not quiet:
            print("Saving GeoTIFF. Please wait...")
        img = finish_picture(bigim, base_size, bbox, x0, y0, x1, y1)
        imgbands = len(img.getbands())
        driver = gdal.GetDriverByName("GTiff")

        if "options" not in kwargs:
            kwargs["options"] = [
                "COMPRESS=DEFLATE",
                "PREDICTOR=2",
                "ZLEVEL=9",
                "TILED=YES",
            ]

        gtiff = driver.Create(
            filename,
            img.size[0],
            img.size[1],
            imgbands,
            gdal.GDT_Byte,
            **kwargs,
        )
        xp0, yp0 = from4326_to3857(lat0, lon0)
        xp1, yp1 = from4326_to3857(lat1, lon1)
        pwidth = abs(xp1 - xp0) / img.size[0]
        pheight = abs(yp1 - yp0) / img.size[1]
        gtiff.SetGeoTransform((min(xp0, xp1), pwidth, 0, max(yp0, yp1), 0, -pheight))
        gtiff.SetProjection(WKT_3857)
        for band in range(imgbands):
            array = numpy.array(img.getdata(band), dtype="u8")
            array = array.reshape((img.size[1], img.size[0]))
            band = gtiff.GetRasterBand(band + 1)
            band.WriteArray(array)
        gtiff.FlushCache()

        if not quiet:
            print(f"Image saved to {filename}")
        return img

    try:
        image = draw_tile(
            source, south, west, north, east, zoom, output, quiet, **kwargs
        )
        if return_image:
            return image
        if to_cog:
            image_to_cog(output, output)
    except Exception as e:
        raise Exception(e)

def chw_to_hwc(block):
    # Grab first 3 channels
    block = block[:3, ...]
    # CHW to HWC
    block = np.transpose(block, (1, 2, 0))
    return block

def hwc_to_hw(block, channel=0):
    # Grab first 3 channels
    block = block[..., channel].astype(np.uint8)
    return block

def calculate_sample_grid(raster_h, raster_w, sample_h, sample_w, bound):
    h, w = sample_h, sample_w
    blocks = []
    height = h + 2 * bound
    width = w + 2 * bound

    for y in range(-bound, raster_h, h):
        for x in range(-bound, raster_w, w):
            rigth_x_bound = max(bound, x + width - raster_w)
            bottom_y_bound = max(bound, y + height - raster_h)

            blocks.append(
                {
                    'x': x,
                    'y': y,
                    'height': height,
                    'width': width,
                    'bounds': [[bound, bottom_y_bound], [bound, rigth_x_bound]],
                }
            )
    return blocks

def read_block(src, x, y, height, width, nodata=0, **kwargs):
    return src.read(
        window=((y, y + height), (x, x + width)), boundless=True, fill_value=nodata
    )

def write_block(dst, raster, y, x, height, width, bounds=None):
    if bounds:
        raster = raster[
            bounds[0][0] : raster.shape[0] - bounds[0][1],
            bounds[1][0] : raster.shape[1] - bounds[1][1],
        ]
        x += bounds[1][0]
        y += bounds[0][0]
        width = width - bounds[1][1] - bounds[1][0]
        height = height - bounds[0][1] - bounds[0][0]
    dst.write(raster, 1, window=((y, y + height), (x, x + width)))

def tiff_to_tiff(
    src_fp,
    dst_fp,
    func,
    data_to_rgb=chw_to_hwc,
    sample_size=(512, 512),
    sample_resize=None,
    bound=128,
):
    with rasterio.open(src_fp) as src:
        profile = src.profile

        # Computer blocks
        rh, rw = profile['height'], profile['width']
        sh, sw = sample_size
        bound = bound

        resize_hw = sample_resize

        sample_grid = calculate_sample_grid(
            raster_h=rh, raster_w=rw, sample_h=sh, sample_w=sw, bound=bound
        )
        # set 1 channel uint8 output
        profile['count'] = 1
        profile['dtype'] = 'uint8'

        with rasterio.open(dst_fp, 'w', **profile) as dst:
            for b in tqdm(sample_grid):
                r = read_block(src, **b)

                uint8_rgb_in = data_to_rgb(r)
                orig_size = uint8_rgb_in.shape[:2]
                if resize_hw is not None:
                    uint8_rgb_in = cv2.resize(
                        uint8_rgb_in, resize_hw, interpolation=cv2.INTER_LINEAR
                    )

                # Do something
                uin8_out = func(uint8_rgb_in)

                if resize_hw is not None:
                    uin8_out = cv2.resize(
                        uin8_out, orig_size, interpolation=cv2.INTER_NEAREST
                    )
                # Zero channel, because
                write_block(dst, uin8_out, **b)

def image_to_image(image, func, sample_size=(384, 384), sample_resize=None, bound=128):
    with tempfile.NamedTemporaryFile() as src_tmpfile:
        s, b = cv2.imencode('.tif', image)
        src_tmpfile.write(b.tobytes())
        src_fp = src_tmpfile.name
        with tempfile.NamedTemporaryFile() as dst_tmpfile:
            dst_fp = dst_tmpfile.name
            tiff_to_tiff(
                src_fp,
                dst_fp,
                func,
                data_to_rgb=chw_to_hwc,
                sample_size=sample_size,
                sample_resize=sample_resize,
                bound=bound,
            )

            result = cv2.imread(dst_fp)
    return result[..., 0]

def tiff_to_image(
    src_fp,
    func,
    data_to_rgb=chw_to_hwc,
    sample_size=(512, 512),
    sample_resize=None,
    bound=128,
):
    with tempfile.NamedTemporaryFile() as dst_tmpfile:
        dst_fp = dst_tmpfile.name
        tiff_to_tiff(
            src_fp,
            dst_fp,
            func,
            data_to_rgb=data_to_rgb,
            sample_size=sample_size,
            sample_resize=sample_resize,
            bound=bound,
        )

        result = cv2.imread(dst_fp)
    return result[..., 0]

def tiff_to_shapes(tiff_path, simplify_tolerance=None):
    with rasterio.open(tiff_path) as src:
        band = src.read()

        mask = band != 0
        shapes = features.shapes(band, mask=mask, transform=src.transform)
    result = [shapely.geometry.shape(shape) for shape, _ in shapes]
    if simplify_tolerance is not None:
        result = [shape.simplify(tolerance=simplify_tolerance) for shape in result]
    return result

def draw_tile(source, lat0, lon0, lat1, lon1, zoom, filename, **kwargs):
    bbox = [lon0, lat0, lon1, lat1]
    image = tms_to_geotiff(
        filename,
        bbox,
        zoom=zoom,
        resolution=None,
        source=source,
        to_cog=False,
        return_image=True,
        quiet=False,
        **kwargs,
    )
    return image


#!/usr/bin/env python
# coding: utf-8

# In[6]:

from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error

from sklearn import datasets
iris = datasets.load_iris()
# print(iris.DESCR)

# In[9]:

targ = iris.target
# print(targ)
# print(iris.target_names)

# In[20]:

data = iris.data
# print(data)

# In[22]:

from sklearn.model_selection import train_test_split

data_train,data_test,targ_train,targ_test=train_test_split(data,targ,test_size=0.2,random_state=101)

# In[25]:

from sklearn.linear_model import LinearRegression

LRclf = LinearRegression()
LRclf.fit(data_train,targ_train)

# In[26]:

targ_pred = LRclf.predict(data_test)
targ_pred

# In[27]:

# import matplotlib.pyplot as plt

#sepal length
# plt.scatter(data_test[:,0],targ_pred)
# plt.show()

# In[28]:

#sepal width
# plt.scatter(data_test[:,1],targ_pred)
# plt.show()

# In[29]:

#petal length
# plt.scatter(data_test[:,2],targ_pred)
# plt.show()

# In[30]:

#petal width
# plt.scatter(data_test[:,3],targ_pred)
# plt.show()

# In[35]:

import matplotlib.pyplot as plt

fig = plt.figure(figsize = (15, 10))
plt.subplot(2, 2, 1)

plt.title('Sepal Length')
plt.scatter(data_test[:,0],targ_pred)

plt.subplot(2, 2, 2)
plt.title('Sepal Width')
plt.scatter(data_test[:,1],targ_pred)

plt.subplot(2, 2, 3)
plt.title('Petal Width')
plt.scatter(data_test[:,2],targ_pred)

plt.subplot(2, 2, 4)
plt.title('Petal Width')
plt.scatter(data_test[:,3],targ_pred)

# In[ ]:


s = input()
name = "".join([x if x.isalpha() or x == '-' else ' ' for x in s])
name = name.split()
if len(name) > 2:
    print("Nume invalid")
else:
    for i in range(len(name)):
        name[i] = ' ' + name[i] + ' '
        for j in range(1, len(name[i])):
            if name[i][j - 1] == ' ' or name[i][j - 1] == '-':
                if not(name[i][j].isupper()):
                    print("Nume invalid")
                    break
        else:
            currName = name[i].split(" -")
            for j in range(len(currName)):
                if len(currName[j]) < 3:
                    print("Nume invalid")
                    break

Year I/Semester I/Programarea Algorimilor/Materiale/2022 - 2023/Laboratoare/Grupa 152/Rezolvari/Laborator 02/09.py


import numpy as np
import matplotlib.pyplot as plt

gridWorld = np.zeros((6, 6))
tmp = np.zeros((6, 6))
theta = 0.01

if_convergence = False
while not if_convergence:
    delta = 0
    for i in range(6):
        for j in range(6):
            if (i == 0 and j == 1) or (i == 5 and j == 5):
                continue
            else:
                # i--
                gridW = gridWorld[i][j-1] - 1 if j > 0 else gridWorld[i][j] - 1
                # i++
                gridE = gridWorld[i][j+1] - 1 if j < 5 else gridWorld[i][j] - 1
                # j--
                gridN = gridWorld[i-1][j] - 1 if i > 0 else gridWorld[i][j] - 1
                # j++
                gridS = gridWorld[i+1][j] - 1 if i < 5 else gridWorld[i][j] - 1
                tmp[i][j] = 0.25 * (gridS + gridN + gridE + gridW)
                delta = max(delta, abs(tmp[i][j] - gridWorld[i][j]))
                gridWorld[i][j] = tmp[i][j]
    print(delta)
    if_convergence = True if (delta < theta) else False

print(gridWorld)
plt.matshow(gridWorld)
plt.show()
mat = [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1]]
plt.matshow(mat)
for i in range(6):
    for j in range(6):
        west = gridWorld[i][j-1] - gridWorld[i][j] if (j > 0) else 0
        east = gridWorld[i][j+1] - gridWorld[i][j] if (j < 5) else 0
        north = gridWorld[i-1][j] - gridWorld[i][j] if (i > 0) else 0
        south = gridWorld[i+1][j] - gridWorld[i][j] if (i < 5) else 0
        go = ""
        max_n = max(west, east, south, north)
        if west == max_n:
            go += "w /"
        if east == max_n:
            go += "e /"
        if south == max_n:
            go += "s /"
        if north == max_n:
            go += "n /"
        go = go[:-1]
        plt.text(x=j, y=i, s=go)
plt.show()


import numpy as np

def get_area(bbox):
    x11,x12,y11,y12=bbox[:4]
    return abs((x12-x11)*(y12-y11))

def get_bbox_overlap(bbox1,bbox2):
    x11,x12,y11,y12=bbox1[:4]
    x21,x22,y21,y22=bbox2[:4]
    # If one rectangle is on left side of other
    if(x11 >= x22 or x21 >= x12):
        return [0,0,0,0]

    # If one rectangle is above other
    if(y12 <= y21 or y22 <= y11):
        return [0,0,0,0]
    overlap_x1=max(x11,x21)
    overlap_x2=min(x12,x22)
    overlap_y1=max(y11,y21)
    overlap_y2=min(y12,y22)
    return [overlap_x1,overlap_x2,overlap_y1,overlap_y2]

def check_overlap(bbox1,bbox2,thresh=0.9):

    overlap=get_bbox_overlap(bbox1,bbox2)
    if min(get_area(bbox1),get_area(bbox2))==0:
        return False
    # if get_area(overlap) /min(get_area(bbox1),get_area(bbox2))>0:
    #     print('get_area(overlap) /min(get_area(bbox1),get_area(bbox2)',get_area(overlap) /min(get_area(bbox1),get_area(bbox2)))
    if get_area(overlap) /min(get_area(bbox1),get_area(bbox2)) > thresh:
        return True

    return False

def check_bbox_in_group(list_group,bboxes,index):
    for grouped in list_group:
        if index in grouped:
            return True
        for m in grouped:
            if check_overlap(bboxes[index],bboxes[m]):
                grouped.append(index)
                return True
    return False

def fiter_overlap_bbox(bboxes):
    list_group=[]
    for i in range(len(bboxes)-1,-1,-1):
        if check_bbox_in_group(list_group,bboxes,i):
            continue

        group_id=[i]
        for j in range(len(bboxes)-1,-1,-1):
            if i==j :
                continue
            if check_overlap(bboxes[i],bboxes[j]):
                group_id.append(j)

        if len(group_id) >1:
            list_group.append(group_id)

    pop_list=[]
    for group_id in list_group:
        if len(group_id) ==2:
            merge_x1= min(bboxes[group_id[0]][0],bboxes[group_id[-1]][0])
            merge_x2= max(bboxes[group_id[0]][1],bboxes[group_id[-1]][1])
            merge_y1= min(bboxes[group_id[0]][2],bboxes[group_id[-1]][2])
            merge_y2= max(bboxes[group_id[0]][3],bboxes[group_id[-1]][3])
            bboxes[min(group_id)]=[merge_x1,merge_x2,merge_y1,merge_y2, 0.5*(merge_y1+merge_y2), merge_y2-merge_y1]
            # bboxes[min(group_id)]=[merge_x1,merge_x2,merge_y1,merge_y2]
            pop_list.append(max(group_id))

        if len(group_id) >3 :
            group_id = sorted(group_id, key=lambda id: get_area(bboxes[id][:4]))

            max_bbox=bboxes[group_id[-1]]
            max_w=max_bbox[1] -max_bbox[0]
            max_h= max_bbox[1] -max_bbox[0]

            remain_w= [(bboxes[id][1]- bboxes[id][0]) for id in group_id[:-1]]
            remain_h= [(bboxes[id][3]- bboxes[id][2]) for id in group_id[:-1]]

            if max_h/np.mean(remain_h) >1.3 and  max_w/np.mean(remain_w)>1.3:
                pop_list.append(group_id[-1])
    pop_list = sorted(pop_list, key=lambda id:id,reverse=True)

    for pop_id in pop_list:
        bboxes.pop(pop_id)
    return bboxes

def correct_bboxes(bboxes):
    for i,bbox1 in enumerate(bboxes):
        bbox1_h= bbox1[3] -bbox1[2]
        for j,bbox2 in enumerate(bboxes):
            bbox2_h= bbox2[3] -bbox2[2]
            if bbox2_h==0:
                continue

            if int(np.floor(bbox1_h/bbox2_h+ 0.5)) != 2:
                continue

            bbox11=[bbox1[0],bbox1[1],bbox1[2],int(bbox1[2] + 0.5*bbox1_h)]
            bbox12=[bbox1[0],bbox1[1],int(bbox1[2] + 0.5*bbox1_h),bbox1[3]]

            ovelap_112=get_bbox_overlap(bbox2,bbox11)
            ovelap_122=get_bbox_overlap(bbox2,bbox12)

            if (ovelap_122[3] -ovelap_122[2])/bbox2_h < 0.5 and (ovelap_112[3] -ovelap_112[2])/bbox2_h < 0.5:
                continue

            if get_area(ovelap_112) > get_area(ovelap_122):

                bboxes[i] =bbox12

                merge_x1= min(bbox2[0],bbox11[0])
                merge_x2= max(bbox2[1],bbox11[1])
                merge_y1= min(bbox2[2],bbox11[2])
                merge_y2= max(bbox2[3],bbox11[3])
                bboxes[j] = [merge_x1,merge_x2,merge_y1,merge_y2]
            else:
                bboxes[i] =bbox11
                merge_x1= min(bbox2[0],bbox12[0])
                merge_x2= max(bbox2[1],bbox12[1])
                merge_y1= min(bbox2[2],bbox12[2])
                merge_y2= max(bbox2[3],bbox12[3])
                bboxes[j] = [merge_x1,merge_x2,merge_y1,merge_y2]

    return   bboxes


from typing import Optional, Dict

import pandas as pd

from kbc_pul.popularity.entity_counting.total_ent_count import EntityTripleCountController

EntityStr = str

class EntityCountInOtherRelationsBothPositions:

    def __init__(self,
                 target_relation: str,
                 entity_triple_count_controller: EntityTripleCountController,
                 verbose: bool = False
                 ):
        self.target_relation: str = target_relation
        self.verbose: bool = verbose
        self.entity_triple_count_controller: EntityTripleCountController = entity_triple_count_controller

        self.o_entity_to_count_map: Optional[
            Dict[
                EntityStr,
                int
            ]
        ] = None

        self.relations_to_include = self.entity_triple_count_controller.get_all_relations_but(
            {target_relation}
        )
        self.positions_to_include = None

    def get_count(self, entity: EntityStr) -> int:
        if self.o_entity_to_count_map is not None:
            return self.o_entity_to_count_map.get(entity, 0)
        else:
            return self.entity_triple_count_controller.get_count_of(
                entity=entity,
                relations_to_include=self.relations_to_include,
                positions_to_include=self.positions_to_include
            )

    def are_counts_materialized(self) -> bool:
        return self.o_entity_to_count_map is not None

    def materialize_entity_counts(self) -> None:
        if self.verbose:
            print("Materializing entity counts...")
        self.o_entity_to_count_map = self.entity_triple_count_controller.materialize_entity_to_count_map(
            relations_to_include=self.relations_to_include,
            positions_to_include=self.positions_to_include
        )

    def get_entity_counts_as_df(self) -> pd.DataFrame:
        if not self.are_counts_materialized():
            self.materialize_entity_counts()
        return EntityTripleCountController.entity_to_count_map_to_df(self.o_entity_to_count_map)


import numpy as np
import networkx as nx
import scrubber
import model
import os.path

def neglog(c):
    return -np.log((c + 1)/2)

def heavyneglog(c):
    return (- np.log((c + 1)/2)) ** 5

def invfun(c):
    return (1 - c)/(1 + c)

def cov(c):
    return c

def cotan(c):
    return 1/np.tan(np.pi * (c + 1)/ 2)

def read_or_scrub (year_from,year_to,fun = cov):
    cache_string_cov = "cache/covariances_{}_{}.csv".format(year_from,year_to)
    cache_string_list = "cache/countrylist_{}_{}.csv".format(year_from,year_to)
    if os.path.exists(cache_string_cov) == False or\
       os.path.exists(cache_string_list) == False:
        vote_matrix, country_list = scrubber.scrub_un_docs(year_from,year_to)

        coalition_matrix = np.matmul(vote_matrix,np.transpose(vote_matrix))

        average_votes = np.sum(vote_matrix,1) / vote_matrix.shape[1]
        covariance_matrix = coalition_matrix - vote_matrix.shape[1] * np.outer(average_votes,average_votes)

        stddevs = np.zeros(covariance_matrix.shape[0])
        for i in range(0,covariance_matrix.shape[0]):
            if (covariance_matrix[i][i] <= 0):
                print(country_list[i], covariance_matrix[i][i], vote_matrix[i])
            stddevs[i] = np.sqrt(covariance_matrix[i][i])

        for i in range(0,covariance_matrix.shape[0]):
            covariance_matrix[i][i] = 0
            for j in range(i + 1,covariance_matrix.shape[1]):
                covariance_matrix[i,j] = covariance_matrix[i,j]/(stddevs[i] * stddevs[j])
                covariance_matrix[j,i] = covariance_matrix[j,i]/(stddevs[i] * stddevs[j])

        voting_matrix = np.zeros(covariance_matrix.shape)
        for i in range(0,covariance_matrix.shape[0]):
            for j in range(0,covariance_matrix.shape[1]):
                voting_matrix[i,j] = fun(covariance_matrix[i,j])

        covariance_graph = nx.from_numpy_matrix(covariance_matrix)
        voting_graph = nx.from_numpy_matrix(voting_matrix)
        nx.write_weighted_edgelist(covariance_graph,cache_string_cov,delimiter=';')
        country_list_file = open(cache_string_list,"w")
        for str in country_list:
            country_list_file.write(str)
            country_list_file.write("\n")
        country_list_file.close()
        return voting_graph, country_list, voting_matrix
    else:
        covariance_graph = nx.read_weighted_edgelist(cache_string_cov,delimiter=';')
        country_list_file = open(cache_string_list,'r')
        country_list = country_list_file.read().split("\n")
        country_list_file.close()
        covariance_matrix = nx.to_numpy_matrix(covariance_graph)
        voting_matrix = np.zeros(covariance_matrix.shape)
        for i in range(0,covariance_matrix.shape[0]):
            for j in range(0,covariance_matrix.shape[1]):
                voting_matrix[i,j] = fun(covariance_matrix[i,j])

        voting_graph = nx.from_numpy_matrix(voting_matrix)

        return voting_graph, country_list, voting_matrix

def filter_graph (G, filter_dict, verbose = False) :
    bad_edges = []
    for i in range(len(filter_dict)):
        for j in range(i + 1, len(filter_dict)):
            if filter_dict[i] == filter_dict[j]:
                bad_edges.append((i,j))
                bad_edges.append((j,i))
    if verbose:
        print ("Removing {} edges".format(len(bad_edges)))
    G.remove_edges_from(bad_edges)
    return G

def generate_filtered_record (year_from ,  year_to, blocs):
    cache_string_graph = "cache/filtered_{}_{}_{}.csv".format(year_from,year_to,blocs)
    cache_string_list = "cache/countrylist_{}_{}.csv".format(year_from,year_to)
    if os.path.exists(cache_string_graph) == False or\
       os.path.exists(cache_string_list) == False:
        voting_graph, country_list, voting_matrix = read_or_scrub(year_from,year_to)
        indset_dict = model.optimize_scoring(voting_matrix, blocs)
        filtered_graph = filter_graph(voting_graph, indset_dict)
        nx.write_weighted_edgelist(filtered_graph,cache_string_graph,delimiter=';')
        country_list_file = open(cache_string_list,"w")
        for str in country_list:
            country_list_file.write(str)
            country_list_file.write("\n")
        country_list_file.close()
        return filtered_graph, country_list
    else:
        filtered_graph = nx.read_weighted_edgelist(cache_string_graph,delimiter=';')
        country_list_file = open(cache_string_list,"r")
        country_list = country_list_file.read().split("\n")
        country_list_file.close()
        return filtered_graph, country_list

def spell_community_difference(G, Graw, res, country_list):
    c = nx.algorithms.community.greedy_modularity_communities(G, weight = 'weight', resolution = res)
    c_raw = nx.algorithms.community.greedy_modularity_communities(Graw, weight = 'weight', resolution = res)

    min, max = 0,0
    if len(c_raw) > len(c):
        min = len(c)
        max = len(c_raw)
    else:
        min = len(c_raw)
        max = len(c)
    for j in range(min):
        print("|Community {0:>30}|".format(j + 1))
        print("------------------------------------------------------------------------------------")
        for i in range(len(country_list)):
            bold_str = "*{}*".format(country_list[i])
            if i in c_raw[j] and str(i) in c[j]:
                print("|{0:>40}|{1:>41}|".format(country_list[i],country_list[i]))
            elif i in c_raw[j]:
                print("|                                        |{0:41}|".format(bold_str))
            elif str(i) in c[j]:
                print("|{0:>40}|                                         |".format(bold_str))
    for j in range(min,max):
        print("|Community {0:>30}|".format(j + 1))
        print("------------------------------------------------------------------------------------")
        for i in range(len(country_list)):
            bold_str = "*{}*".format(country_list[i])
            if min == len(c):
                if i in c_raw[j]:
                    print("|                                        |{0:>40}|".format(bold_str))
            else:
                if str(i) in c[j]:
                    print("|{0:>40}|                                         |".format(bold_str))


#### SHOULD PROBABLY MAKE AN AR45 FUNCS FILE TO STORE SOME OF THESE DEETS

from AR45_funcs import *

figdir

#### Load all
bin_list=sort(glob.glob('/home/isabela/Documents/cruises/OSNAP2020_oxygen_AR45/data/ctd/ar45_*'))

def getdist(lon0,lat0,lonex,latex):
    distout=zeros(len(lonex))
    for ii in range(len(lonex)):
        distout[ii]=sw.dist([lat0,latex[ii]],[lon0,lonex[ii]])[0][0]
    return distout

###### Section numbering details + metadata
seclab={} #short for section key
# seclab['section 0']=array([0])
seclab['section 1']=range(1,29) # this is the section at the LS mooring line
seclab['section 2']=range(29,51)
seclab['section 3']=range(51,71)
seclab['section 3b']=range(71,86)
seclab['section 4 offshore']=range(86,94) # Includes CF7 location
seclab['CF tripods']=range(94,97)
seclab['section 4 onshore']=range(97,113) # Rest of the CF line
seclab['section 5']=range(113,137)
seclab['section EGC 1']=range(137,151)
seclab['section EGC 2']=range(151,163)

#### Onshore-most data point (to calc distance from)
startsec={}
startsec['section 1']=14
startsec['section 2']=43
startsec['section 3']=51
startsec['section 3b']=71
startsec['section 4 offshore']=93
startsec['CF tripods']=96
startsec['section 4 onshore']=97
startsec['section 5']=113
startsec['section EGC 1']=137
startsec['section EGC 2']=151

endsec={}
endsec['section 1']=1
endsec['section 2']=29
endsec['section 3']=70
endsec['section 3b']=85
endsec['section 4 offshore']=86
endsec['CF tripods']=94
endsec['section 4 onshore']=112
endsec['section 5']=136
endsec['section EGC 1']=150
endsec['section EGC 2']=162

#####Load it all into an xarray:
prslen=1750 #Nothing is deeper than 3500m
prsvec=range(2,2*prslen+1,2)

dat=fCNV(bin_list[0])

bin_list

def loadCTD_bin(datalist):
    stanum=len(datalist)
    lat=zeros(stanum)
    lon=zeros(stanum)
    date=[1]*stanum
    prs=NaN*ones((prslen,stanum))
    sal=NaN*ones((prslen,stanum))
    SA=NaN*ones((prslen,stanum))
    tmp=NaN*ones((prslen,stanum))
    CT=NaN*ones((prslen,stanum))
    den=NaN*ones((prslen,stanum))
    o2_sbe43=NaN*ones((prslen,stanum))
    v7=NaN*ones((prslen,stanum))
    o2_aa4831=NaN*ones((prslen,stanum))
    prsvec=array(range(2,2*prslen+1,2))
    flourmat=NaN*ones((prslen,stanum))
    maxprsvec=NaN*ones((stanum))
    for ii,dd in enumerate(datalist):
        dat=fCNV(dd)
        lat[ii]=dat.attributes['LATITUDE']
        lon[ii]=dat.attributes['LONGITUDE']
        date[ii]=dat.attributes['datetime']
        pl=len(dat['PRES'].data)
        maxprsvec[ii]=pl*2
        pst=int((dat['PRES'].data[0]-2)/2)
        prs[pst:pl+pst,ii]=dat['PRES'].data
        sal[pst:pl+pst,ii]=dat['PSAL'].data
        tmp[pst:pl+pst,ii]=dat['TEMP'].data
        flourmat[pst:pl+pst,ii]=dat['flECO-AFL'].data
        SA[pst:pl+pst,ii]=gsw.SA_from_SP(sal[:pl,ii],prs[:pl,ii],lon[ii],lat[ii])
        CT[pst:pl+pst,ii]=gsw.CT_from_pt(SA[:pl,ii],tmp[:pl,ii])
        den[pst:pl+pst,ii]=gsw.sigma0(SA[:pl,ii],CT[:pl,ii])
        ## convert from mL/L to uM (umol/l)
        ## 1uM = 0.022391 mL
        o2_sbe43[pst:pl+pst,ii]=dat['oxygen_ml_L']/0.022391
        #remove very small noisy voltages
        v7_clean=dat['v7'].copy()
        v7_clean[dat['v7']<0.1]=NaN
        v7[pst:pl+pst,ii]=v7_clean
        obase=aaoptode_ctdconvert(dat['PSAL'],dat['TEMP'],dat['PRES'],v7_clean)
        #remove noisy low bound aandera o2 values and interpolate over them (not sure why they persist)
        nanind=obase>265
        prel=prsvec[pst:pl+pst]
        ofunc=interpolate.interp1d(prel[nanind],obase[nanind],kind='linear',fill_value='NaN',bounds_error=False)
        o2_aa4831[:,ii]=ofunc(prsvec)

    distvec=array([NaN]*stanum)
    secvec=array([])
    for ss in seclab:
        secvec=hstack((secvec,[ss]*len(seclab[ss])))
        if len(seclab[ss])>1:
            #The -1 is there to deal with the fact that I am not using section 0!!
            news=[sss-1 for sss in seclab[ss]]
            distvec[news]=getdist(lon[startsec[ss]-1],lat[startsec[ss]-1],lon[news],lat[news])
        else:
            distvec[news]=zeros(1)

    grdat=xr.Dataset({'sal': (['prs','sta'],  sal),
                      'tmp': (['prs','sta'],  tmp),
                      'SA': (['prs','sta'],  SA),
                      'CT': (['prs','sta'],  CT),
                      'den': (['prs','sta'],  den),
                      'o2_sbe43': (['prs','sta'],  o2_sbe43),
                      'v7_o2': (['prs','sta'],  v7),
                      'o2_aa4831': (['prs','sta'],  o2_aa4831),
                      'odiff': (['prs','sta'], o2_aa4831-o2_sbe43),
                      'flourescence': (['prs','sta'], flourmat),
                      'max_prs': (['sta'],  maxprsvec),
                      'lat': (['sta'],  lat),
                      'lon': (['sta'],  lon),
                      'sec': (['sta'],  secvec),
                      'dist': (['sta'], distvec)},
                    coords={'sta': range(1,stanum+1),
                            'prs': prsvec})

    return grdat

grdat=loadCTD_bin(bin_list[1:])

datadir='/home/isabela/Documents/projects/OSNAP/data/Shipboard/netcdf/'
grdat.to_netcdf(datadir+'AllCTD20_2m_Leahproc.nc')

def plot_a_sec(var,secnum):
    figure()
    if 'o2' in var:
        varname='o2'
    else:
        varname=var
    pcolor(sort(grdat.dist.where(grdat.sec=='section '+secnum)),grdat.prs,(grdat.where(grdat.sec=='section '+secnum)[var]).sortby(grdat.dist.where(grdat.sec=='section '+secnum)),vmin=uni[varname]['vmin'],vmax=uni[varname]['vmax'],cmap=uni[varname]['cmap'])
    colorbar(label=var)
    ylim(3000,0)
    ylabel('pressure [db]')
    xlabel('distance [km]')
    if secnum=='1':
        title('West Greenland, LS line')
        savefig(figdir+'LSline_'+var+'.png',bbox_inches='tight')
    elif secnum=='4':
        title('East Greenland, CF line')
        savefig(figdir+'CFline_'+var+'.png',bbox_inches='tight')

colors = [(12,44,132),(78,179,211) ,(255,237,160),(217,95,14),(240,59,32)]
sal_cmap = make_cmap(colors,position=[0,0.9,0.96,0.99,1],bit=True)

uni['sal']={}
uni['sal']['cmap']=sal_cmap
uni['sal']['vmin']=32
uni['sal']['vmax']=35.2

uni['den']={}
uni['den']['cmap']=pden_cmap
uni['den']['vmin']=26
uni['den']['vmax']=28

uni['flourescence']={}
uni['flourescence']['cmap']=cm.Greens
uni['flourescence']['vmin']=0
uni['flourescence']['vmax']=6

# For each section, plot a pcolor section of sal, tmp, pden, O2 (aa +sbe), o2diff
# Then make profile comparisons for O2 (1 for each section - then a cumulative direct comp) using only deep sites for both
def plot_all_secs(secnum):
    for var in ['sal','tmp','den','o2_sbe43','o2_aa4831','odiff','flourescence']:
        plot_a_sec(var,secnum)

plot_all_secs('1')
plot_all_secs('4 offshore')
plot_all_secs('4 onshore')
plot_all_secs('5')

def comp_o2_profs(secnum):
    l1=plot(grdat.where(grdat.sec=='section '+secnum)['o2_sbe43'],grdat.prs,color='r',label='SBE43')
    l2=plot(grdat.where(grdat.sec=='section '+secnum)['o2_aa4831'],grdat.prs,color='b',label='AA4831')
    for ll in [l1,l2]:
        setp(ll[1:],label='')
    ylabel('pressure [db]')
    xlabel('O2 concentration [uM]')
    xlim(260,325)
    ylim(3000,0)
    legend()
    if secnum=='1':
        title('West Greenland, LS line')
        savefig(figdir+'LSline_o2comp_prof.png',bbox_inches='tight')
    elif ('4' in secnum) | ('5' in secnum):
        title('East Greenland, CF line')
        savefig(figdir+'CFline_o2comp_prof.png',bbox_inches='tight')

comp_o2_profs('1')
comp_o2_profs('4 offshore')

comp_o2_profs('5')

def o2diff_per_sec(secnum):
    plot(grdat.where(grdat.sec=='section '+secnum)['odiff'],grdat.prs,color='k')
    ylabel('pressure [db]')
    xlabel('AA4831 - SBE43\nO2 concentration difference [uM]')
    xlim(0,30)
    ylim(3000,0)
    if secnum=='1':
        title('West Greenland, LS line')
        savefig(figdir+'LSline_o2diff_prof.png',bbox_inches='tight')
    elif ('4' in secnum) | ('5' in secnum):
        title('East Greenland, CF line')
        savefig(figdir+'CFline_o2diff_prof.png',bbox_inches='tight')

o2diff_per_sec('1')
o2diff_per_sec('4 offshore')
o2diff_per_sec('5')

def o2diff_alldeep(stas,tit,lab):
    import matplotlib as mpl
    fig,ax=subplots(1,1)
    minsta=stas.min()
    maxsta=stas.max()
    elcmap=cm.rainbow
    for dsta in stas:
        ax.plot(grdat.where(grdat.sta==dsta)['odiff'],grdat.prs,color=elcmap((dsta-minsta)/(maxsta-minsta)))
    ylabel('pressure [db]')
    xlabel('AA4831 - SBE43\nO2 concentration difference [uM]')
    xlim(0,30)
    ylim(3000,0)
    title(tit)
    norm = mpl.colors.Normalize(vmin=minsta, vmax=maxsta)
    cax=fig.add_axes([0.925,0.15,0.02,0.7])
    cb1 = mpl.colorbar.ColorbarBase(cax,cmap=elcmap,norm=norm, label='Station number')
    savefig(figdir+lab+'_o2diff_prof.png',bbox_inches='tight')

o2diff_alldeep(grdat['sta'].where(grdat.max_prs>550),'All stations deeper than 550m','Alldeep')

o2diff_alldeep(grdat['sta'].where(grdat.sec=='section 1'),'West Greenland, LS line','LSline')
o2diff_alldeep(grdat['sta'].where(grdat.sec=='section 4 offshore'),'East Greenland, CF line','CFline')
o2diff_alldeep(grdat['sta'].where(grdat.sec=='section 5'),'East Greenland, just upstream of CF line','CFline')


from random import shuffle
import pygame as pg
from lib.config import *

def shuffler (r):

    shuff = [i for i in range(r)]
    shuffleagain = 1

    while shuffleagain:
        # re-shuffle until no two sequential cards are neighbors
        shuffle(shuff)
        idx = 0
        for j in range(r - 1):
            if shuff[j] - shuff[j+1] == 1 or shuff[j] - shuff[j+1] == -1:
                shuffleagain = 1
            else:
                idx += 1
            if idx == 5:
                shuffleagain = 0
                return shuff

def scaler (file, factor):
    file = pg.image.load(file)
    return pg.transform.scale(file, factor)

def mouse_chk (mp, pos, size):
    return (mp[0]>=pos.x) and mp[0]<=(pos.x + size) and mp[1]<=(pos.y + size) and mp[1]>=(pos.y)


import level_map
from game_defines import *

class SimpleMap(level_map.LevelMap):
    def __init__(self, map_object, actors, assets):
        super(SimpleMap, self).__init__(map_object, actors, assets)

    def get_mice_positions(self):
        #Don't need any processing to find the mouse
        mlist = []
        for name in self.actors:
            actor = self.actors[name]
            if actor.get_type() == "mouse":
                mlist.append((actor.get_x(), actor.get_y()))

        return mlist

    def is_mouse_at_position(self, x, y):
        mlist = self.get_mice_positions()
        for m in mlist:
            if m.get_x() == x and m.get_y() == y:
                return True
        return False

    def kill_mouse_at_position(self, x, y):
        dead_mouse = None
        mlist = self.get_mice_positions()
        for m in mlist:
            if m.get_x() == x and m.get_y() == y:
                dead_mouse = m
                break

        del(self.actors[dead_mouse.name()])

    def get_mice_count(self):
        mlist = self.get_mice_positions()
        return len(mlist)

    def is_mouse_visible(self):
        return True


def calculate_mcd_msd(alpha, fftlen, pool, validation_dir, gt_dir, sr=16000, num_mcep=36, frame_period=5.0):
    converted_dirs = os.listdir(validation_dir)
    total_mcd_list = list()
    f2f_mcd_list = list()
    f2m_mcd_list = list()
    m2f_mcd_list = list()
    m2m_mcd_list = list()
    total_msd_list = list()
    f2f_msd_list = list()
    f2m_msd_list = list()
    m2f_msd_list = list()
    m2m_msd_list = list()
    print(converted_dirs)

    for converted_dir in converted_dirs:
        src, tar = converted_dir.split('_to_')
        if src == tar:
            continue
        converted_dir = os.path.join(validation_dir, converted_dir)
        target_dir = os.path.join(gt_dir, tar)
        sent_list = os.listdir(converted_dir)
        print(sent_list)

        p = Pool(pool)
        converted_mcep_list = p.starmap(load_wav_extract_mcep, zip([converted_dir] * len(sent_list), sent_list))
        target_mcep_list = p.starmap(load_wav_extract_mcep, zip([target_dir] * len(sent_list), sent_list))
        p.close()
        p.join()

        # print('convert')
        # print(len(converted_mcep_list))
        # for d in converted_mcep_list:
        #     print(d.shape)
        # print('target')
        # print(len(target_mcep_list))
        # for d in target_mcep_list:
        #     print(d.shape)
        # print(target_mcep_list[0].shape)
        # print(target_mcep_list[1].shape)
        # print(target_mcep_list[2].shape)

        p = Pool(pool)
        converted_ms_list = p.map(extract_ms, converted_mcep_list)
        target_ms_list = p.map(extract_ms, target_mcep_list)
        p.close()
        p.join()

        p = Pool(pool)
        mcd_list = p.starmap(mcd_cal, zip(converted_mcep_list, target_mcep_list))
        msd_list = p.starmap(msd_cal, zip(converted_ms_list, target_ms_list))
        p.close()
        p.join()

        for mcd, msd, sent in zip(mcd_list, msd_list, sent_list):
            total_mcd_list.append(mcd)
            total_msd_list.append(msd)
            if 'F' in src and 'F' in tar:
                if src == tar:
                    continue
                f2f_mcd_list.append(mcd)
                f2f_msd_list.append(msd)

            elif 'F' in src and 'M' in tar:
                f2m_mcd_list.append(mcd)
                f2m_msd_list.append(msd)

            elif 'M' in src and 'F' in tar:
                m2f_mcd_list.append(mcd)
                m2f_msd_list.append(msd)

            elif 'M' in src and 'M' in tar:
                if src == tar:
                    continue
                m2m_mcd_list.append(mcd)
                m2m_msd_list.append(msd)

        print('SRC: ', src, ' TAR: ', tar, ' MCD_MEAN: ', np.mean(mcd_list), ' MCD_STD: ', np.std(mcd_list))
        print('SRC: ', src, ' TAR: ', tar, ' MSD_MEAN: ', np.mean(msd_list), ' MSD_STD: ', np.std(msd_list))

    print()
    print('-' * 30)
    print('total ', ' MCD_MEAN: ', np.mean(total_mcd_list), ' MCD_STD: ', np.std(total_mcd_list))
    print('f2f ', ' MCD_MEAN: ', np.mean(f2f_mcd_list), ' MCD_STD: ', np.std(f2f_mcd_list))
    print('f2m ', ' MCD_MEAN: ', np.mean(f2m_mcd_list), ' MCD_STD: ', np.std(f2m_mcd_list))
    print('m2f ', ' MCD_MEAN: ', np.mean(m2f_mcd_list), ' MCD_STD: ', np.std(m2f_mcd_list))
    print('m2m ', ' MCD_MEAN: ', np.mean(m2m_mcd_list), ' MCD_STD: ', np.std(m2m_mcd_list))
    print()
    print('-' * 30)
    print('total ', ' MSD_MEAN: ', np.mean(total_msd_list), ' MSD_STD: ', np.std(total_msd_list))
    print('f2f ', ' MSD_MEAN: ', np.mean(f2f_msd_list), ' MSD_STD: ', np.std(f2f_msd_list))
    print('f2m ', ' MSD_MEAN: ', np.mean(f2m_msd_list), ' MSD_STD: ', np.std(f2m_msd_list))
    print('m2f ', ' MSD_MEAN: ', np.mean(m2f_msd_list), ' MSD_STD: ', np.std(m2f_msd_list))
    print('m2m ', ' MSD_MEAN: ', np.mean(m2m_msd_list), ' MSD_STD: ', np.std(m2m_msd_list))

def load_wav_extract_mcep(converted_dir, sent):
    # wav, _ = librosa.load(os.path.join(converted_dir, sent), sr=16000, mono=True)
    _, wav = wavfile.read(os.path.join(converted_dir, sent))
    _, _, sp, _ = world_decompose(wav=wav, fs=16000, frame_period=5.0)
    mcep = pysptk.sp2mc(sp, 36 - 1, 0.455)
    return mcep

def extract_ms(mcep):
    ms = logpowerspec2(4096, mcep)
    return ms

def mcd_cal(converted_mcep, target_mcep):
    converted_mcep = converted_mcep[:,1:]
    target_mcep = target_mcep[:,1:]
    twf = estimate_twf(converted_mcep, target_mcep, fast=True)
    converted_mcep_mod = converted_mcep[twf[0]]
    target_mcep_mod = target_mcep[twf[1]]
    mcd = melcd(converted_mcep_mod, target_mcep_mod)
    return mcd

def msd_cal(converted_ms, target_ms):
    msd = np.sqrt(np.mean(np.power((converted_ms - target_ms), 2)))
    return msd

###############################################
#
#   modified
#
###############################################
def logpowerspec(fftsize, data):
    # create zero padded data
    T, dim = data.shape
    padded_data = np.zeros((fftsize, dim))
    if T > fftsize :
        padded_data += data[:fftsize]
    else:
        padded_data[:T] += data

    complex_spec = np.fft.fftn(padded_data, axes=(0, 1))
    logpowerspec = 2 * np.log(np.absolute(complex_spec))
    return logpowerspec

###############################################
#
#   modified
#
###############################################
def logpowerspec2(fftsize, data):
    # create zero padded data
    T, dim = data.shape
    padded_data = np.zeros((fftsize, dim))

    if T > fftsize :
        padded_data += data[:fftsize]
    else:
        padded_data[:T] += data

    complex_spec = np.fft.rfft(padded_data,fftsize, axis=0)
    R, I = complex_spec.real, complex_spec.imag
    logpowerspec2 = np.log(R * R + I * I)

    return logpowerspec2

def melcd(array1, array2):
    """Calculate mel-cepstrum distortion
    Calculate mel-cepstrum distortion between the arrays.
    This function assumes the shapes of arrays are same.
    Parameters
    ----------
    array1, array2 : array, shape (`T`, `dim`) or shape (`dim`)
        Arrays of original and target.
    Returns
    -------
    mcd : scala, number > 0
        Scala of mel-cepstrum distortion
    """
    if array1.shape != array2.shape:
        raise ValueError(
            "The shapes of both arrays are different \
            : {} / {}".format(array1.shape, array2.shape))

    if array1.ndim == 2:
        # array based melcd calculation
        diff = array1 - array2
        mcd = 10.0 / np.log(10) \
            * np.mean(np.sqrt(2.0 * np.sum(diff ** 2, axis=1)))
    elif array1.ndim == 1:
        diff = array1 - array2
        mcd = 10.0 / np.log(10) * np.sqrt(2.0 * np.sum(diff ** 2))
    else:
        raise ValueError("Dimension mismatch")

    return mcd

def estimate_twf(orgdata, tardata, distance='melcd', fast=True, otflag=None):
    if distance == 'melcd':
        def distance_func(x, y): return melcd(x, y)
    else:
        raise ValueError('other distance metrics than melcd does not support.')

    if otflag is None:
        # use dtw or fastdtw
        if fast:
            _, path = fastdtw(orgdata, tardata, dist=distance_func)
            twf = np.array(path).T

    return twf

import pysptk
import numpy as np
from preprocess import *
from scipy.io import wavfile
from multiprocessing import Pool
from fastdtw import fastdtw
import argparse
import os

alpha = 0.455
############################
#   이거 문제임
############################
fftlen = 4096

parser = argparse.ArgumentParser(description='T')

parser.add_argument('--test_dir', type=str,default='convert/VAE_single')
parser.add_argument('--gt_dir', type=str, default='corpus_seoulNarrative/test')

parser.add_argument('--pool', type=int, help='pool',
                        default=6)

argv = parser.parse_args()
pool = argv.pool

# config for seoulNarr
#   sampling_rate = 16000
#   num_mcep = 36
#   frame_period = 5.0
#   n_frames = 128

calculate_mcd_msd(alpha, fftlen, pool, sr=16000, num_mcep=36, frame_period=5.0, validation_dir=argv.test_dir,
                      gt_dir=argv.gt_dir)


__author__ = 'Xavier Basty <xbasty@gmail.com>'
__version__ = '0.1a3'

# The base package metadata to be used by both distutils and setuptools
METADATA = dict(
  name = "grapefruit",
  version = __version__,
  py_modules = ['grapefruit'],
  author = 'Xavier Basty',
  author_email = 'xbasty@gmail.com',
  description = 'A module to manipulate color information easily.',
  license = 'Apache License 2.0',
  url = 'http://code.google.com/p/grapefruit/',
  download_url = 'http://grapefruit.googlecode.com/files/grapefruit-%s.tar.gz' % \
    __version__,
  keywords ='color conversion',
)

# Extra package metadata to be used only if setuptools is installed
SETUPTOOLS_METADATA = dict(
  install_requires = ['setuptools'],
  include_package_data = True,
  zip_safe = True,
  classifiers = [
    'Development Status :: 3 - Alpha',
    'Intended Audience :: Developers',
    'License :: OSI Approved :: Apache Software License',
    'Topic :: Software Development :: Libraries :: Python Modules',
    'Topic :: Multimedia :: Graphics',
  ],
  test_suite = 'grapefruit_test.suite',
)

def Read(file):
  return open(file).read()

def BuildLongDescription():
  return '\n'.join([Read('README'), Read('CHANGES')])

def Main():
  # Build the long_description from the README and CHANGES files
  METADATA['long_description'] = BuildLongDescription()

  # Use setuptools if available, otherwise fallback and use distutils
  try:
    import setuptools
    METADATA.update(SETUPTOOLS_METADATA)
    setuptools.setup(**METADATA)
  except ImportError:
    import distutils.core
    distutils.core.setup(**METADATA)

if __name__ == '__main__':
  Main()


#Write a function to check whether two given strings are Permutation of each other or not. A Permutation of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are Permutation of each other.
import sys

class Counter(dict):
    def __missing__(self, key):
        return 0

def is_permutation(str1, str2):

    n1 = len(str1)
    n2 = len(str2)

    if(n1 != n2):
        return False

    counter = Counter()

    for letter in str1:
        counter[letter] += 1

    for letter in str2:
        if not letter in counter:
            return False
        counter[letter] -= 1
        if counter[letter] == 0:
            del counter[letter]
    return len(counter) == 0

if __name__ == "__main__":
   print(is_permutation(sys.argv[-2], sys.argv[-1]))


import tkinter as tk
import tkinter.messagebox
from tkinter.constants import SUNKEN

window =tk.Tk()
window.title('Simple Calculator- By Digvijay')
#tkinter.messagebox.showinfo("Welcome","Thank You for using this calculator!")
frame = tk.Frame(master=window, bg="#F2EBE9", padx=15)
frame.pack()
entry = tk.Entry(master=frame,relief=SUNKEN,borderwidth=6,width=50, bg="#9DD6DF")
entry.grid(row=0, column=0,columnspan=4,ipady=2,pady=2)

def myclick(number):
    entry.insert(tk.END,number)

def equal():
    try:
        y =str(eval(entry.get()))
        entry.delete(0,tk.END)
        entry.insert(0,y)
    except:
        entry.delete(0, tk.END)
        entry.insert(0,"Syntax Error")
        #tkinter.messagebox.showinfo("","Syntax error")

def clear():
    entry.delete(0,tk.END)

def bspace():
    val =str(entry.get())
    val =val[:-1]
    entry.delete(0, tk.END)
    entry.insert(0,val)

#button layout
but_1=tk.Button(master=frame,text='1',padx=15,pady=5,width=4,command=lambda: myclick(1),bg="#EEE3CB")
but_1.grid(row=1,column=0,pady=2)

but_2=tk.Button(master=frame,text='2',padx=15,pady=5,width=4,command=lambda: myclick(2),bg="#EEE3CB")
but_2.grid(row=1,column=1,pady=2)

but_3=tk.Button(master=frame,text='3',padx=15,pady=5,width=4,command=lambda: myclick(3),bg="#EEE3CB")
but_3.grid(row=1,column=2,pady=2)

but_4=tk.Button(master=frame,text='4',padx=15,pady=5,width=4,command=lambda: myclick(4),bg="#EEE3CB")
but_4.grid(row=2,column=0,pady=2)

but_5=tk.Button(master=frame,text='5',padx=15,pady=5,width=4,command=lambda: myclick(5),bg="#EEE3CB")
but_5.grid(row=2,column=1,pady=2)

but_6=tk.Button(master=frame,text='6',padx=15,pady=5,width=4,command=lambda: myclick(6),bg="#EEE3CB")
but_6.grid(row=2,column=2,pady=2)

but_7=tk.Button(master=frame,text='7',padx=15,pady=5,width=4,command=lambda: myclick(7),bg="#EEE3CB")
but_7.grid(row=3,column=0,pady=2)

but_8=tk.Button(master=frame,text='8',padx=15,pady=5,width=4,command=lambda: myclick(8),bg="#EEE3CB")
but_8.grid(row=3,column=1,pady=2)

but_9=tk.Button(master=frame,text='9',padx=15,pady=5,width=4,command=lambda: myclick(9),bg="#EEE3CB")
but_9.grid(row=3,column=2,pady=2)

but_0=tk.Button(master=frame,text='0',padx=15,pady=5,width=4,command=lambda: myclick(0),bg="#EEE3CB")
but_0.grid(row=4,column=1,pady=2)

but_dot=tk.Button(master=frame,text='.',padx=15,pady=5,width=4,command=lambda: myclick('.'),bg="#EEE3CB")
but_dot.grid(row=4,column=2,pady=2)

but_add=tk.Button(master=frame,text='+',padx=7.5,pady=5,width=4,command=lambda: myclick('+'),bg="#D7C0AE")
but_add.grid(row=3,column=3,pady=2)

but_sub=tk.Button(master=frame,text='-',padx=7.5,pady=5,width=4,command=lambda: myclick('-'),bg="#D7C0AE")
but_sub.grid(row=3,column=4,pady=2)

but_mul=tk.Button(master=frame,text='*',padx=15,pady=5,width=2,command=lambda: myclick('*'),bg="#D7C0AE")
but_mul.grid(row=4,column=3,pady=2)

but_div=tk.Button(master=frame,text='/',padx=15,pady=5,width=2,command=lambda: myclick('/'),bg="#D7C0AE")
but_div.grid(row=4,column=4,pady=2)

but_clr=tk.Button(master=frame,text='AC',padx=15,pady=5,width=8,command=clear,bg="#DF7861")
but_clr.grid(row=1,column=3,pady=2)

but_eql=tk.Button(master=frame,text="=",padx=15,pady=5,width=8,command=equal,bg="#DF7861")
but_eql.grid(row=2,column=3,pady=2)

but_del=tk.Button(master=frame,text="DEL",padx=15,pady=5,width=4,bg="#DF7861",command=bspace)
but_del.grid(row=4,column=0,pady=2)

window.mainloop()


import logging
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from dotenv import load_dotenv

load_dotenv()

import os
import django
import requests
from math import ceil

django.setup()

from prices.models import *

shop = Shop.objects.get(name='DAJAR')
user_agent = os.environ.get('User-Agent')
headers = {"User-Agent": user_agent}

logging.basicConfig(filename='../app_dajar.log', level=logging.DEBUG, format='%(asctime)s\t%(levelname)s\t%(message)s')

url = "https://dajar.pl/searchengine/pl/V1/products\
?searchCriteria[filter_groups][0][filters][0][field]=category_id\
&searchCriteria[filter_groups][0][filters][0][value]={category_id}\
&searchCriteria[filter_groups][0][filters][0][condition_type]=eq\
&searchCriteria[pageSize]={page_size}\
&searchCriteria[filter_groups][2][filters][0][field]=type_id\
&searchCriteria[filter_groups][2][filters][0][value]=simple\
&searchCriteria[filter_groups][3][filters][0][field]=visibility\
&searchCriteria[filter_groups][3][filters][0][value]=2,4\
&searchCriteria[filter_groups][3][filters][0][condition_type]=in\
&searchCriteria[sortOrders][4][direction]=desc\
&searchCriteria[sortOrders][4][field]=position\
&searchCriteria[sortOrders][5][direction]=desc\
&searchCriteria[sortOrders][5][field]=id\
&searchCriteria[currentPage]={current_page}"

def get_producer(array: []):
    """
    search for producer in array of dict
    """
    for i in array:
        if i.get('attribute_code') == 'producer':
            return i
    return None

def add_dajar_products():
    categories = Category.objects.filter(shop_id=shop)
    for category in categories:
        print(category.shop_category_id)

        req = requests.get(url.format(category_id=category.shop_category_id, page_size=1, current_page=1),
                           headers=headers)

        if req.status_code == 200:
            page_end = ceil(req.json()['total_count'] / 100) + 1
        else:
            page_end = 0
        print("page end: ", page_end, "status code: ", req.status_code)

        for page in range(1, page_end):

            req = requests.get(url.format(category_id=category.shop_category_id, page_size=100, current_page=page),
                               headers=headers)

            print('page: ', page, 'status code: ', req.status_code)
            items = req.json()['items']

            for item in items:

                try:
                    atr = get_producer(item.get("custom_attributes"))
                    brand_id = atr.get('value')
                    brand_name = atr.get('name')
                    brand = ProductBrand.objects.get(shop_product_brand_id=brand_id, shop_id=shop)
                except ObjectDoesNotExist:
                    brand = ProductBrand.objects.create(shop_product_brand_id=brand_id, shop_id=shop,
                                                        brand_name=brand_name)
                except MultipleObjectsReturned:
                    logging.warning(f"brand duplicated: {brand_id}")
                    continue
                except AttributeError:
                    logging.warning(f"Attribute Error brand: {product_id}")
                    continue

                try:
                    product_id = item.get('id')
                    product = Product.objects.get(shop_product_id=product_id, shop_id=shop)
                except ObjectDoesNotExist:
                    product_name = item.get('name')
                    extension_attributes = item.get('extension_attributes')
                    print("extension_attributes: ", extension_attributes)
                    ean = [extension_attributes.get('ean', None)]
                    photo_name = extension_attributes.get("image_dimensions")[0] \
                        .get('product_page_image_small').get('src')
                    photo_url = '//dajarmedia.dajarmedia.com/cdn-cgi/image/width=420,quality=70,format=auto' + photo_name
                    product = Product(shop_product_id=product_id, product_name=product_name, shop_id=shop,
                                      category_id=category, brand_id=brand, ean=ean, photo_url=photo_url)
                    product.save()
                except MultipleObjectsReturned:
                    logging.warning(f'MULTIPLE product_id: {product_id}')
                    continue
                except AttributeError:
                    logging.warning(f"Attribute Error product: {product_id}")
                    continue

                price = item.get('price')
                final_price = item.get('final_price')
                if final_price < price:
                    price_promo = final_price
                else:
                    price_promo = None

                Price.objects.create(product_id=product, price=price, price_promo=price_promo)

if __name__ == '__main__':
    add_dajar_products()


from neuron import h

h.load_file('stdrun.hoc')

soma = h.Section(name='soma')
seg = soma(0.5)
soma.insert('hh')

ic = h.IClamp(seg)
ic.amp = 40  # since by default a very large section
ic.delay = 1
ic.dur = 1

h.xpanel("neuron")
h.xlabel("voltage graph")
g = h.Graph()
g.addvar("v", "v(0.5)")   #hoc string example
g.addvar("m", seg.hh._ref_m)   #pointer example
#g.addvar("v", soma(0.5)._ref_v) #pointer
#g.addvar("v(0.5)")   #no label (not yet)

h.xpanel()

h.finitialize(-65)


import cv2
import numpy as np
import os
from pdf2image import convert_from_path

class Pdf2Jpg:
    def __init__(self, base_output_path=None) -> None:
        self.base_output_path = base_output_path

    def get_file_paths(self, input_path):
        file_paths = os.listdir(input_path)
        file_paths = [f for f in file_paths if os.path.isfile(input_path + "/" + f)]
        return file_paths

    def convert_files(self, input_path):
        file_paths = self.get_file_paths(input_path)
        q_files = len(file_paths)
        for i, file_path in enumerate(file_paths):
            file_path_full = f"{input_path}/{file_path}"
            print(f"Converting file {file_path_full} | [{i+1}/{q_files}]")
            images = convert_from_path(file_path_full)
            cv2_images = []
            for image in images:
                img = np.array(image)
                img = img[:, :, ::-1].copy()
                cv2_images.append(img)
            image = cv2.vconcat(cv2_images)
            file_name = file_path.split(".")[0]
            output_path = f"{self.base_output_path}/{file_name}.jpg"
            cv2.imwrite(output_path, image)
        print("All files were converted successfully!")

if __name__ == "__main__":
    base_input_path = "D:/paraisur_eci_uba/data/inputs/invoices/pdfs"
    base_output_path = "D:/paraisur_eci_uba/data/inputs/invoices/jpgs"

    converter = Pdf2Jpg(base_output_path)
    converter.convert_files(base_input_path)


#!/usr/bin/env python

from setuptools import setup, find_packages
import os

def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(name='franca2ros',
      version='0.0.1',
      description='Franca IDL to ROS2 translator',
      author='Mikhail Tsukerman',
      packages=find_packages(include=['franca2ros.*','franca2ros.translator.*']),
      author_email='miketsukerman@gmail.com',
      license="Apache2",
      keywords="fidl someip ros2",
      long_description=read('readme.md'),
      install_requires=[
          'jinja2',
          'pyfranca',
          'argparse'
      ],
      entry_points={
          'console_scripts': ['fidl2ros=franca2ros.scripts.fidl2ros:main']
      }
      )


import os
import cv2
import numpy as np
import keras
from keras.preprocessing import image
from keras.preprocessing.image import load_img,img_to_array
from keras.models import load_model
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
def main():
    model=load_model("model.h5")
    face_haar_cascade=cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
    cap=cv2.VideoCapture(0)
    while True:
        r,test_image=cap.read()
        if not r:
            continue
        grey_image=cv2.cvtColor(test_image,cv2.COLOR_BGR2RGB)
        face_detected=face_haar_cascade.detectMultiScale(grey_image,1.32,5)

        for (x,y,w,h) in face_detected:
            cv2.rectangle(test_image,(x,y),(x+w,y+h),(255,0,0),thickness=7)
            roi_gray=grey_image[y:y+w,x:x+h]
            roi_gray=cv2.resize(roi_gray,(224,224))
            img_px=image.img_to_array(roi_gray)
            img_px=np.expand_dims(img_px,axis=0)
            img_px=img_px/255

            predictions=model.predict(img_px)
            max_index=np.argmax(predictions[0])

            emotions = ('angry', 'disgust', 'fear', 'happy', 'neutral', 'sad', 'surprise')
            predicted_emotion = emotions[max_index]

            cv2.putText(test_image,predicted_emotion,(int(x),int(y)),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,255),2)

        resized_img=cv2.resize(test_image,(1000,700))
        cv2.imshow('Facial emotion:', resized_img)

        if cv2.waitKey(10)==ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows
if __name__=="__main__":
    main()


from django.shortcuts import render

from rest_framework.views import APIView
from rest_framework.response import Response

from core.models import (
    Category,
    Brand,
    Seller,
    Warranty,
    Discount,
    Product
)

from core.api.serializers import (
    CategorySerializer,
    BrandSerializer,
    SellerSerializer,
    WarrantySerializer,
    DiscountSerializer,
    ProductSerializer,
)

# Create your views here.
class ProductView(APIView):
    def get(self, request):
        products = Product.objects.all()
        serializer = ProductSerializer(products, many=True)
        return Response(serializer.data)


# https://www.youtube.com/watch?v=afITiFR6vfw&index=19&list=PLQVvvaa0QuDfefDfXb9Yf0la1fPDKluPF
import random
import matplotlib.pyplot as plt
from matplotlib import style

style.use('fivethirtyeight')

fig = plt.figure()

def create_plots():
	xs = []
	ys = []

	for i in range(10):
		x = i
		y = random.randrange(10)

		xs.append(x)
		ys.append(y)

	return xs, ys

# add_subplot syntax
'''
# height, width, plot
#ax1 = fig.add_subplot(211)
#ax2 = fig.add_subplot(212)

ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(212)
'''

# 6x1 (6 tall, 1 wide) | (0,0) - start point
#ax1 = plt.subplot2grid((6,1), (0,0), rowspan=2, colspan=1)
#ax2 = plt.subplot2grid((6,1), (2,0), rowspan=2, colspan=1)
#ax3 = plt.subplot2grid((6,1), (4,0), rowspan=2, colspan=1)

ax1 = plt.subplot2grid((6,1), (0,0), rowspan=1, colspan=1)
ax2 = plt.subplot2grid((6,1), (1,0), rowspan=4, colspan=1)
ax3 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1)

x, y = create_plots()
ax1.plot(x, y)

x, y = create_plots()
ax2.plot(x, y)

x, y = create_plots()
ax3.plot(x, y)

plt.show()


# ALGORITMO DE BUSQUEDA DE ARCHIVOS EN UN SISTEMA DE DIRECOTRIOS, IMPLEMENTNADO NODOS
from operator import le
import pathlib
import os
# from tkinter import Tk, Tcl
# from tkinter.filedialog import askdirectory
# Para archivos
from os.path import isfile, join, isdir

# La dirección completa del archivo .py
camino = pathlib.Path(__file__).parent.absolute()

# def camino():
#   return pathlib.Path(__file__).parent.absolute()
ruta = str(camino)

def elArchivo(url):
  return os.path.split(url) # Devuelve una tupla, el primer elemento es todo lo que viene antes del elementos actual y en sgunda posición seria el componente de la ruta.

def isRuta(url, actual):
  return  url + actual

# FUnción para saber si se trata de un directorio o un archivo
# True: Directorio
# False: archivo
def isDirectorio(dato):
  res = dato.find('.')
  if res == -1:
    return True
  return False

def allContenido(url):
  return os.listdir(url)

def allDirectorios(url, contenido):
  return [nombre for nombre in contenido if isdir(join(url, nombre))]

def allArchivos(url, contenido):
  return [nombre for nombre in contenido if isfile(join(url, nombre))]

def listado_archivos(url):
  arr = allArchivos(url, allContenido(url))
  cantidad = len(arr)
  return [arr, cantidad]

def listado_directorios(url):
  arr = allDirectorios(url, allContenido(url))
  cantidad = len(arr)
  return [arr, cantidad]

# print(type(ruta))
# result = listado_directorios(ruta+'\casi')
# result = listado_archivos(ruta+'\casi')
# print(result)

def todoUrl(url):
  listado = []
  directorio = listado_directorios(url)
  archivo = listado_archivos(url)
  for dic in directorio[0]:
    listado.append(dic)
  for arc in archivo[0]:
    listado.append(arc)

  actual = elArchivo(url)
  return [listado, directorio[0], directorio[1] , archivo[1], actual[1]]

arreglo_hero = []
arreglo_dic = []
arreglo_arc = []
comienzos = []
objetivos = []

def arreglo_maestro(url):
  todo = todoUrl(url)
  comienzos.append(todo[4])
  objetivos.append(todo[0])
  arreglo_dic.append([todo[2]])
  arreglo_arc.append([todo[3]])

  for parte in todo[0]:
    arreglo_hero.append(parte)

  index = 0
  # print(todo[1])
  for par in todo[1]:
    if(index == 0):
      url = isRuta(url, f'\{par}')
      arreglo_maestro(url)
    else:
      new = elArchivo(url)
      url = new[0]
      url = isRuta(url, f'\{par}')
      arreglo_maestro(url)
    index = index + 1

arreglo_maestro(ruta)

# print(arreglo_hero)

nodoInicial = camino.name
num = 0
nodos = {num: nodoInicial}
for nodo in arreglo_hero:
  num = num + 1
  nodos[num]= nodo

num = 0
nodosInvertidos = {nodoInicial: num}
for nodo in arreglo_hero:
  num = num + 1
  nodosInvertidos[nodo]= num

# print('NODOS', nodos)
# print('NODOS INVERTIDOS',nodosInvertidos)

# print('Comienzos', comienzos)
# print('objetivos', objetivos)
# print('.................')
# print(arreglo_dic)
# print(arreglo_arc)

class Grafo:

  def __init__(self, numero_nodos, dirigido=True):
    # Atributos
    self.g_numeros_nodos = numero_nodos
    self.g_nodos = range(self.g_numeros_nodos)
    self.g_dirigido = dirigido
    self.g_adjuntar_lista = {nodo: set() for nodo in self.g_nodos}

  def agregar_borde(self, nodo1, nodo2, peso=1):
    self.g_adjuntar_lista[nodo1].add((nodo2, peso))
    if not self.g_dirigido:
      self.g_adjuntar_lista[nodo2].add((nodo1, peso))

  # Imprimir lista de nodos
  def imprimir_adjuntar_lista(self):
    for clave in self.g_adjuntar_lista.keys(): #
      print(f'nodo{clave}: {self.g_adjuntar_lista[clave]}')

  def busqueda_profundidad(self, inicio, objetivo, camino = [], visitado = set()):
    camino.append(inicio) # Añade el nodo a la lista
    visitado.add(inicio) # Seta el nodo como visitado
    # print(visitado)
    # print(camino)
        # print(camino)
    if inicio == objetivo: # D eser verdadero no hay para que hacer la busqueda
        return camino
    for (puerto, peso) in self.g_adjuntar_lista[inicio]:
        if puerto not in visitado:
            resultado = self.busqueda_profundidad(puerto, objetivo, camino, visitado) # Recursividad
            if resultado is not None:
                return resultado
    camino.pop()
    return None

if __name__ == "__main__":
  cantidad = len(nodos.keys())
  grafo = Grafo(cantidad, dirigido=True)
  count = 0
  for directo in comienzos:
    prin = nodosInvertidos.get(directo)
    # print(prin)
    arr_obj = objetivos[count]
    for puerto in arr_obj:
      sec = nodosInvertidos.get(puerto)
      # print(sec)
      grafo.agregar_borde(prin, sec)
    count = count + 1

  grafo.imprimir_adjuntar_lista()

  ruta = []
  buscar = input('Ingrese su archivo o docuemnto a buscar: ')
  objetivo = nodosInvertidos.get(buscar)
  ruta = grafo.busqueda_profundidad(0, objetivo)
  print(f" La ruta transversal del nodo 0 al nodo 3 es {ruta}")

  new_ruta = []
  for plox in ruta:
    new_ruta.append(nodos.get(plox))
  print(f" La ruta transversal del nodo 0 al nodo 3 es {new_ruta}")


from collections import defaultdict
class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        res = []
        if len(words) == 0: return res
        n = len(s)
        m = len(words)
        w = len(words[0])
        tot = defaultdict(int)
        for word in words: tot[word] += 1
        for i in range(w):
            wd = defaultdict(int)
            cnt = 0
            for j in range(i, n, w):
                if j >= i + m * w:
                    word = s[j - m * w : j - (m - 1) * w]
                    wd[word] -= 1
                    if wd[word] < tot[word]: cnt -= 1
                word = s[j : j + w]
                wd[word] += 1
                if wd[word] <= tot[word]: cnt += 1
                if cnt == m: res.append(j - (m - 1) * w)
        return res


from django import template

register = template.Library()

@register.filter
def ru_pluralize(value: int, variants: str) -> str:
    """Фильтр окончаний кириллицы."""
    default = ""
    if not variants:
        return default

    variants = variants.split(",")
    value = abs(int(value))
    if value % 10 == 1 and value % 100 != 11:
        variant = 0
    elif 2 <= value % 10 <= 4 and (value % 100 < 10 or value % 100 >= 20):
        variant = 1
    else:
        variant = 2

    try:
        return variants[variant]
    except IndexError:
        return default


from blok_functions import pBlk, pCompile

out = pBlk('MonoOut', (320, 110))

env1 = pBlk('EnvBasic', (30, 110))
env1.set_params(attack=0.085, decay=0.45, release=0.6)

oscillators = []
for freq, y in zip([0, 0.5, 1], [120, 200, 280]):
    osc = pBlk('Osc', (170, y))
    osc.set_params(tuning=freq)
    env1 > osc._amp
    osc > out._input
    oscillators.append(osc)

env1.attack(0.7)
env1.decay(0.22)

pCompile(path=r'C:\Users\zeffi\Desktop\demux1.blkx', silent=0)


"""
EXPERIMENTAL
============

Lightweight connection pooling for peewee.

In a single-threaded application, only one connection will be created. It will
be continually recycled until either it exceeds the stale timeout or is closed
explicitly (using `.manual_close()`).

In a multi-threaded application, up to `max_connections` will be opened.
"""
import heapq
import logging
import threading
import time

from peewee import MySQLDatabase
from peewee import PostgresqlDatabase

logger = logging.getLogger('peewee.pool')

class PooledDatabase(object):
    def __init__(self, database, max_connections=20, stale_timeout=None,
                 **kwargs):
        self.max_connections = max_connections
        self.stale_timeout = stale_timeout
        self._connections = []
        self._in_use = {}
        self._closed = set()
        self.conn_key = id

        super(PooledDatabase, self).__init__(database, **kwargs)

    def _connect(self, *args, **kwargs):
        while True:
            try:
                ts, conn = heapq.heappop(self._connections)
                key = self.conn_key(conn)
            except IndexError:
                ts = conn = None
                logger.debug('No connection available in pool.')
                break
            else:
                if self.stale_timeout and self._is_stale(ts):
                    logger.debug('Connection %s was stale, closing.', key)
                    self._close(conn, True)
                    ts = conn = None
                elif self._is_closed(key, conn):
                    logger.debug('Connection %s was closed.', key)
                    ts = conn = None
                    self._closed.discard(key)
                else:
                    break

        if conn is None:
            if self.max_connections and (
                    len(self._in_use) >= self.max_connections):
                raise ValueError('Exceeded maximum connections.')
            conn = super(PooledDatabase, self)._connect(*args, **kwargs)
            ts = time.time()
            key = self.conn_key(conn)
            logger.debug('Created new connection %s.', key)

        self._in_use[key] = ts
        return conn

    def _is_stale(self, timestamp):
        return (time.time() - timestamp) > self.stale_timeout

    def _is_closed(self, key, conn):
        return key in self._closed

    def _close(self, conn, close_conn=False):
        key = self.conn_key(conn)
        if close_conn:
            self._closed.add(key)
            super(PooledDatabase, self)._close(conn)
        elif key in self._in_use:
            logger.debug('Returning %s to pool.', key)
            ts = self._in_use[key]
            del self._in_use[key]
            heapq.heappush(self._connections, (ts, conn))

    def manual_close(self):
        """
        Close the underlying connection without returning it to the pool.
        """
        conn = self.get_conn()
        self.close()
        self._close(conn, close_conn=True)

    def close_all(self):
        """
        Close all connections managed by the pool.
        """
        for _, conn in self._connections:
            self._close(conn, close_conn=True)
        for conn in self._in_use:
            self._close(conn, close_conn=True)

class PooledMySQLDatabase(PooledDatabase, MySQLDatabase):
    def _is_closed(self, key, conn):
        is_closed = super(PooledMySQLDatabase, self)._is_closed(key, conn)
        if not is_closed:
            if hasattr(conn, 'open'):
                # MySQLdb `ping()` seems to always return `None` in my testing.
                # So the `open` attribute will be used instead.
                is_closed = not bool(conn.open)
            else:
                # pymysql `ping([reconnect=True])` will indicate if the conn
                # is closed or not.
                try:
                    is_closed = not conn.ping(False)
                except:
                    is_closed = True
        return is_closed

class _PooledPostgresqlDatabase(PooledDatabase):
    def _is_closed(self, key, conn):
        is_closed = super(_PooledPostgresqlDatabase, self)._is_closed(key, conn)
        if not is_closed:
            is_closed = bool(conn.closed)
        return is_closed

class PooledPostgresqlDatabase(_PooledPostgresqlDatabase, PostgresqlDatabase):
    pass

try:
    from playhouse.postgres_ext import PostgresqlExtDatabase

    class PooledPostgresqlExtDatabase(_PooledPostgresqlDatabase, PostgresqlExtDatabase):
        pass
except ImportError:
    pass


from emailfinder.extractor import *
import csv,openpyxl
import pandas as pd

#Excel creation
excel=openpyxl.Workbook()
sheet=excel.active
sheet.title='Email List'
sheet.append(['Domain Name','Email'])

domain_list=[]

#read data from csv
with open('email.csv',encoding='utf-8-sig') as csvfile:
    reader=csv.DictReader(csvfile)
    domain_list=[]
    for row in reader:
        #print(row['Email'])
        domain_list.append(row['Email']) #csv to append list

for i in range(0,len(domain_list)):
    domain=domain_list[i]
    print('Domain :'+domain)
    emails1 = get_emails_from_google(domain)
    emails2 = get_emails_from_bing(domain)
    #emails3 = get_emails_from_baidu("snsce.ac.in")
    for i in range(0,len(emails1)):
        sheet.append([domain,emails1[i]])
    for i in range(0,len(emails2)):
        sheet.append([domain,emails2[i]])
    excel.save('Email Verification.xlsx')
    print("Successfull")


# Databricks notebook source
# MAGIC %md
# MAGIC ##### Welcome  to Azurelib Academy By Deepak Goyal
# MAGIC ##### You can master the Databricks here: www.adeus.azurelib.com
# MAGIC ##### Author of this blog: Arud Seka Berne S in

# COMMAND ----------

# MAGIC %md
# MAGIC #### Pyspark Split Column Values in Azure Databricks

# COMMAND ----------

# MAGIC %md
# MAGIC ###### Gentle reminder:
# MAGIC In Databricks,
# MAGIC   - sparkSession made available as spark
# MAGIC   - sparkContext made available as sc
# MAGIC
# MAGIC In case, you want to create it manually, use the below code.

# COMMAND ----------

from pyspark.sql.session import SparkSession

spark = SparkSession.builder \
    .master("local[*]") \
    .appName("azurelib.com") \
    .getOrCreate()

sc = spark.sparkContext

# COMMAND ----------

# MAGIC %md
# MAGIC ##### a) Create manual PySpark DataFrame

# COMMAND ----------

data = [
    ("Andie,Ebbutt","Andie|Ebbutt|Ebbutt","Andie,Ebbutt Andie|Ebbutt"),
    ("Hobie,Deegan","Hobie|Deegan|Deegan","Hobie,Deegan Hobie|Deegan"),
    ("Denys,Belverstone","Denys|Belverstone|Belverstone","Denys,Belverstone Denys|Belverstone"),
    ("Delphine,Pietersma","Delphine|Pietersma|Pietersma","Delphine,Pietersma Delphine|Pietersma"),
    ("Putnem,Chasson","Putnem|Chasson|Chasson","Putnem,Chasson Putnem|Chasson")
]

df = spark.createDataFrame(data, schema=["names_1","names_2","names_3"])
df.printSchema()
df.show(truncate=False)

# COMMAND ----------

# MAGIC %md
# MAGIC ##### b) Create PySpark DataFrame by reading files

# COMMAND ----------

file_path = "/mnt/practice/name_list.csv"
# replace the file_path with the source file location which you have downloaded.

df_2 = spark.read.format("csv").option("header", True).load(file_path)
df_2.printSchema()

# COMMAND ----------

# MAGIC %md
# MAGIC ##### Note: Here, I will be using the manually created dataframe

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 1. Split() on DataFrame

# COMMAND ----------

from pyspark.sql.functions import split

s_df1 = df.select(split("names_1", ",").alias("comma_delimiter"))
s_df1.printSchema()
s_df1.show(truncate=False)

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 2. Split() using SQL expression

# COMMAND ----------

df.createOrReplaceTempView("names")

spark.sql("SELECT SPLIT(names_1, ',') AS comma_delimiter FROM names") \
.show(truncate=False)

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 3. Split() using limit parameter

# COMMAND ----------

from pyspark.sql.functions import split, size

# 1. Limit = 2
# 2. The resulting array's last entry will contain all the input beyond the last match pattern
# 3. The array length = limit

df.select(
    split("names_2", "\|", limit=2).alias("limit_delimiter"),
    size(split("names_2", "\|", limit=2)).alias("length"),
).show(truncate=False)

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 4. Spliting the column with you have multiple delimiter

# COMMAND ----------

from pyspark.sql.functions import split, size

df.select(
    split("names_3", "[, |]").alias("multiple_delimiter"),
    size(split("names_3", "[, |]")).alias("length"),
).show(truncate=False)

33. Pyspark Split Column Values/Pyspark Split Column Values - Notebook.py


import threading
import time

# 1. threading demo
print("Start a program")

def take_a_nap():
    time.sleep(5)
    print("Wake Up!")

T1 = threading.Thread(target=take_a_nap)
T1.start()

print("End a program")

# 2. threading 传参
# 常规参数用列表传递，关键字参数用字典传递
T2 = threading.Thread(target=print, args=["Cat", "Dog", "Forgs"], kwargs={"sep": " & "})
T2.start()


class Solution:
    def sortColors(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        for j in range(n-1):
            for i in range(n-1):
                if nums[i] > nums[i+1]:
                    temp = nums[i + 1]
                    nums[i + 1] = nums[i]
                    nums[i] = temp


"""
Given a string s, find the length of the longest
substring
 without repeating characters.

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

Constraints:

0 <= s.length <= 5 * 104
s consists of English letters, digits, symbols and spaces.
"""
# Time complexity: O(N)
# Sliding window technique applied
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        # We use a set, because consumes less memory than a dict and does not allow duplicates
        # Also the "in" method is O(1) in sets
        char_set = set()
        # Define a left pointer
        l = 0
        res = 0

        # Then our right pointer is going to be updated when looping over the characters
        for r in range(len(s)):
            # If the next character to add to the set is already in it, we loop over the set and remove
            # the left characters until there are no duplicates, and then add the char
            while s[r] in char_set:
                char_set.remove(s[l])
                l += 1
            char_set.add(s[r])
            # Calculate the max between the size of the set and the current res
            res = max(res, len(char_set))
        return res


import numpy as np
import pandas as pd
from flask import Flask, request, jsonify, render_template
import pickle
# import Food_Recommendation_System_Baseline.ipynb
from food_recommendation_system_baseline import foodrecommendation

app = Flask(__name__)
model = pickle.load(open('modelpickel1.pkl', 'rb'))

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/recommend',methods=['POST'])
def predict():
    '''
    For rendering results on HTML GUI
    '''
    # int_features = [x for x in request.for m.values()]
    # print(int_features)
    final_features = request.form['meal']
    print("the input is" + str(final_features))
    prediction = model.give_rec(final_features)
    p = prediction.tolist()
    print(p)
    print("end")

    # output = prediction

    # return render_template('index.html', prediction_text='Employee Salary should be $ {}'.format(output)) .prediction[:5]
    return render_template('index.html', prediction_text='Recommended meals are: ' + "\n".join(p[:5])+ "\n") #+ str(p[:5])

@app.route('/predict_api',methods=['POST'])
def predict_api():
    '''
    For direct API calls trought request
    '''
    data = request.get_json(force=True)
    prediction = model.give_rec(data)

    output = prediction
    return jsonify(output)
    # return ("Success")

if __name__ == "__main__":
    app.run(debug=True)


import numpy as np
import matplotlib.pyplot as plt

import rondo
import rondo.functions as F

x = rondo.Variable(np.linspace(-7, 7, 200))
y = F.sin(x)
y.backward(create_graph=True)

logs = [y.data.flatten()]

n = 3
for i in range(n):
    logs.append(x.grad.data.flatten())
    gx = x.grad
    x.cleargrad()
    gx.backward(create_graph=True)

# Draw a graph
labels = ['y=sin(x)', "y'", "y''", "y'''"]
for i, v in enumerate(logs):
    plt.plot(x.data, logs[i], label=labels[i])
plt.legend(loc='lower right')
plt.show()


from flask import Flask, render_template, request
import sqlite3 as sql
app = Flask(__name__)

@app.route('/')
def new_present():
   return render_template('present.html')

@app.route('/present',methods = ['POST', 'GET'])
def present():
   if request.method == 'POST':
      try:
         gift = request.form['gift']
         description = request.form['description']
         present_id = request.form['present_id']

         with sql.connect("sqlite3_database.db") as con:
            cur = con.cursor()

            cur.execute("INSERT INTO Present (ID,GIFT,DESCRIPTION) VALUES (?,?,?)",( present_id,gift, description) )

            con.commit()
            msg = "Record successfully added"
      except:
         con.rollback()
         msg = "error in insert operation"

      finally:

         return render_template("result_new.html", msg = msg)
         con.close()

if __name__ == '__main__':
   app.run(debug = True)