# -*- coding: utf-8 -*-
import string, time, subprocess
from flask import  request
from datetime import datetime
from library.common import Common

from configparser import ConfigParser
from library.config_parser import configSectionParser

class VesselVPN(Common):

    # INITIALIZE
    def __init__(self):

        # INIT CONFIG
        self.config = ConfigParser()

        # CONFIG FILE
        self.config.read("config/config.cfg")

        self.token = configSectionParser(self.config,"TOKEN")['token']

    # GET VESSEL FUNCTION
    def vessel_vpn(self):
        """
        This API is for VPN
        ---
        tags:
          - VPN
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: query
            in: body
            description: VPN
            required: true
            schema:
              id: vessel_vpn
              properties:
                callback_url:
                    type: string
                vessel_number:
                    type: string
                vessel_name:
                    type: string
                job_id:
                    type: integer

        responses:
          500:
            description: Error
          200:
            description: Sending invitaion
        """

        # INIT DATA
        data = {}

        # GET DATA
        token =  request.headers.get('token')

        # GET JSON REQUEST
        query_json = request.get_json(force=True)

        job_id = query_json["job_id"]
        callback_url = query_json["callback_url"]
        vessel_number = query_json["vessel_number"]
        vessel_name = query_json["vessel_name"]

        # # CHECK TOKEN
        if not token == self.token:

            data["alert"] = "Invalid Token"
            data['status'] ='Failed'

            # RETURN ALERT
            return self.return_data(data)

        directory = '/home/vvpn/vpn_access'
        process_name = 'script_vpn_real_vessel_auto_creation.py'
        script_file = "%s/%s" % ( directory, process_name)
        print("script_file: ", script_file)
        print("job_id: ", job_id)
        print("callback_url: ", callback_url)
        print("vessel_number: ", vessel_number)
        print("vessel_name: ", vessel_name)
        print("token: ", token)
        print('python3.5', script_file, '-job_id', str(job_id), '-callback_url', str(callback_url), '-vessel_number', str(vessel_number), '-vessel_name', str(vessel_name), '-token', str(token), '-vessel_os', 'LINUX')
        subprocess.Popen(['sudo','python3.5', script_file, '-job_id', str(job_id), '-callback_url', str(callback_url), '-vessel_number', str(vessel_number), '-vessel_name', str(vessel_name), '-token', str(token), '-vessel_os', 'LINUX'])

        # print('python3.5', script_file, '-job_id', str(job_id), '-callback_url', str(callback_url), '-vessel_number', str(vessel_number), '-token', str(token))
        # subprocess.Popen(['sudo','python3.5', script_file, '-job_id', str(job_id), '-callback_url', str(callback_url), '-vessel_number', str(vessel_number), '-token', str(token)])

        data['status'] ='ok'

        return self.return_data(data)


import numpy as np
import tensorflow as tf

from keras.models import load_model, Model
from keras.layers import Input, Dense, Conv2D, Flatten, BatchNormalization, LeakyReLU, add
from keras.optimizers import SGD
from keras import regularizers

from code import cnn_setup

def softmax_cross_entropy_with_logits(y_true, y_pred):
    p = y_pred
    pi = y_true

    zero = tf.zeros(shape=tf.shape(pi), dtype=tf.float32)
    where = tf.equal(pi, zero)

    negatives = tf.fill(tf.shape(pi), -100.0)
    p = tf.where(where, negatives, p)

    loss = tf.nn.softmax_cross_entropy_with_logits_v2(labels=pi, logits=p)

    return loss

class Residual_CNN():
    def __init__(self, config):
        self.reg_const = config['REGULARIZATION']
        self.learning_rate = config['LEARNING_RATE']
        self.momentum = config['MOMENTUM']

        self.input_dim = cnn_setup.INPUT_DIMENSION
        self.output_dim = cnn_setup.OUTPUT_DIMENSION
        self.hidden_layers = cnn_setup.HIDDEN_LAYERS

        self.num_layers = len(self.hidden_layers)

        self.model = self._build_model()

    def residual_layer(self, input_block, filters, kernel_size):
        x = self.conv_layer(input_block, filters, kernel_size)

        x = Conv2D(
            filters=filters,
            kernel_size=kernel_size,
            data_format='channels_last',
            padding='same',
            use_bias=False,
            activation='linear',
            # kernel_regularizer=regularizers.l2(self.reg_const)
        )(x)

        x = BatchNormalization(axis=1)(x)

        x = add([input_block, x])

        x = LeakyReLU()(x)

        return (x)

    def conv_layer(self, x, filters, kernel_size):
        x = Conv2D(
            filters=filters,
            kernel_size=kernel_size,
            data_format='channels_last',
            padding='same',
            use_bias=False,
            activation='linear',
            # kernel_regularizer=regularizers.l2(self.reg_const)
        )(x)

        x = BatchNormalization(axis=1)(x)
        x = LeakyReLU()(x)

        return (x)

    def value_head(self, x):
        x = Conv2D(
            filters=1,
            kernel_size=(1, 1),
            data_format='channels_last',
            padding='same',
            use_bias=False,
            activation='linear',
            # kernel_regularizer=regularizers.l2(self.reg_const)
        )(x)

        x = BatchNormalization(axis=1)(x)
        x = LeakyReLU()(x)

        x = Flatten()(x)

        x = Dense(
            20,
            use_bias=False,
            activation='linear',
            # kernel_regularizer=regularizers.l2(self.reg_const)
        )(x)

        x = LeakyReLU()(x)

        x = Dense(
            1,
            use_bias=False,
            activation='tanh',
            # kernel_regularizer=regularizers.l2(self.reg_const),
            name='value_head'
        )(x)

        return (x)

    def policy_head(self, x):
        x = Conv2D(
            filters=2,
            kernel_size=(1, 1),
            data_format='channels_last',
            padding='same',
            use_bias=False,
            activation='linear',
            # kernel_regularizer=regularizers.l2(self.reg_const)
        )(x)

        x = BatchNormalization(axis=1)(x)
        x = LeakyReLU()(x)

        x = Flatten()(x)

        x = Dense(
            self.output_dim,
            use_bias=False,
            activation='linear',
            # kernel_regularizer=regularizers.l2(self.reg_const),
            name='policy_head'
        )(x)

        return (x)

    def _build_model(self):
        main_input = Input(shape=self.input_dim)

        x = self.conv_layer(main_input, self.hidden_layers[0]['filters'], self.hidden_layers[0]['kernel_size'])

        if len(self.hidden_layers) > 1:
            for h in self.hidden_layers[1:]:
                x = self.residual_layer(x, h['filters'], h['kernel_size'])

        vh = self.value_head(x)
        ph = self.policy_head(x)

        model = Model(inputs=[main_input], outputs=[vh, ph])
        model.compile(
            loss={
                'value_head': 'mean_squared_error',
                'policy_head': 'mean_squared_error'  # softmax_cross_entropy_with_logits
            },
            optimizer=SGD(
                lr=self.learning_rate,
                momentum=self.momentum
            ),
            loss_weights={
                'value_head': 0.5,
                'policy_head': 0.5
            }
        )
        return model

    def predict(self, x):
        return self.model.predict(x)

    def fit(self, states, targets, epochs, verbose, validation_split, batch_size):
        return self.model.fit(
            states, targets, epochs=epochs, verbose=verbose,
            validation_split=validation_split, batch_size=batch_size)

    def write(self, file_name):
        self.model.save(file_name + '.h5')

    def read(self, file_name):
        return load_model(file_name + '.h5', custom_objects={
            'softmax_cross_entropy_with_logits': softmax_cross_entropy_with_logits
        })


from django.contrib import admin
from django.urls import path
from messagesApi import views

urlpatterns = [
    path("admin/", admin.site.urls),
    path('',views.api),
    path("login/", views.loginUser,name='login'),
    path("logout/", views.logoutUser),
    path("getusers/",views.getAllUsers),
    path("getMessages/",views.messagesList),
    path("getUnReadMessages/",views.UnreadmessagesList),
    path("readMessage/",views.readMessage),
    path("writeMessage/",views.writeMessage),
    path("deleteMessage/<int:id>",views.deleteMessage)
]


from django.conf.urls import url
from . import views

urlpatterns = [

    # Main project workflow urls

    url(r'^project/create$',
        views.ProjectViewSet.as_view({'post': 'create'}),
        name='project-create',
    ),
    url(r'^project/list$',
        views.ProjectViewSet.as_view({'get': 'list'}),
        name='project-list',
    ),
    url(r'^project/get$',
        views.ProjectViewSet.as_view({'get': 'retrieve'}),
        name='project-retrieve',
    ),
    url(r'^project/update$',
        views.ProjectViewSet.as_view({'post': 'update'}),
        name='project-update',
    ),

    url(r'^container/get$',
        views.ContainerViewSet.as_view({'get': 'retrieve'}),
        name='container-detail',
    ),
    url(r'^container/list_children$',
        views.ContainerViewSet.as_view({'get': 'list_children'}),
        name='container-list-children',
    ),

    url(r'^work/get$',
        views.WorkViewSet.as_view({'get': 'retrieve'}),
        name='work-detail',
    ),
    url(r'^work/start$',
        views.WorkViewSet.as_view({'get': 'start'}),
        name='work-start',
    ),
    url(r'^work/complete$',
        views.WorkViewSet.as_view({'get': 'complete'}),
        name='work-complete',
    ),
    url(r'^work/verify$',
        views.WorkViewSet.as_view({'get': 'verify'}),
        name='work-verify',
    ),
    url(r'^work/deny$',
        views.WorkViewSet.as_view({'get': 'deny'}),
        name='work-deny',
    ),
    url(r'^work/reactivate$',
        views.WorkViewSet.as_view({'get': 'reactivate'}),
        name='work-reactivate',
    ),
    url(r'^work/free-cash/list/available$',
        views.WorkViewSet.as_view({'get': 'list_free_cash_available'}),
        name='list-free-cash-available',
    ),
     url(r'^work/free-cash/list/my$',
        views.WorkViewSet.as_view({'get': 'list_free_cash_workon'}),
        name='list-free-cash-workon',
    ),
    url(r'^work/post_message$',
        views.WorkViewSet.as_view({'post': 'post_message'}),
        name='work-post-message',
    ),

    url(r'^resource/get$',
        views.ResourceViewSet.as_view({'get': 'retrieve'}),
        name='resource-get',
    ),
    url(r'^resource/update$',
        views.ResourceViewSet.as_view({'post': 'update'}),
        name='resource-update',
    ),
    url(r'^resource/file_upload$',
        views.ResourceFileViewSet.as_view({'post': 'put_file'}),
        name='resource-file-upload',
    ),

    # Config generation urls

    url(r'^project_template/create$',
        views.ProjectTemplateViewSet.as_view({'post': 'create'}),
        name='project-template-create',
    ),
    url(r'^project_template/list$',
        views.ProjectTemplateViewSet.as_view({'get': 'list'}),
        name='project-template-list',
    ),
    url(r'^project_template/get$',
        views.ProjectTemplateViewSet.as_view({'get': 'retrieve'}),
        name='project-template-retrieve',
    ),
    url(r'^project_template/update$',
        views.ProjectTemplateViewSet.as_view({'post': 'update'}),
        name='project-template-update',
    ),
]


import trimesh
from pytorch3d.renderer import (
    BlendParams, blending, look_at_view_transform, FoVOrthographicCameras,
    PointLights, RasterizationSettings, PointsRasterizationSettings,
    PointsRenderer, AlphaCompositor, PointsRasterizer, MeshRenderer,
    MeshRasterizer, SoftPhongShader, SoftSilhouetteShader, TexturesVertex)
from pytorch3d.renderer.mesh import TexturesVertex
from pytorch3d.structures import Meshes, Pointclouds

import torch
import numpy as np
import math
import cv2

class cleanShader(torch.nn.Module):
    def __init__(self, device="cpu", cameras=None, blend_params=None):
        super().__init__()
        self.cameras = cameras
        self.blend_params = blend_params if blend_params is not None else BlendParams(
        )

    def forward(self, fragments, meshes, **kwargs):
        cameras = kwargs.get("cameras", self.cameras)
        if cameras is None:
            msg = "Cameras must be specified either at initialization \
                or in the forward pass of TexturedSoftPhongShader"

            raise ValueError(msg)

        # get renderer output
        blend_params = kwargs.get("blend_params", self.blend_params)
        texels = meshes.sample_textures(fragments)
        images = blending.softmax_rgb_blend(texels,
                                            fragments,
                                            blend_params,
                                            znear=-256,
                                            zfar=256)

        return images

class Render:
    def __init__(self, size=512, device=torch.device("cuda:0")):
        self.device = device
        self.mesh_y_center = 100.0
        self.dis = 100.0
        self.scale = 1.0
        self.size = size
        self.cam_pos = [(0, 100, 100)]

        self.mesh = None
        self.pcd = None
        self.renderer = None
        self.meshRas = None

    def get_camera(self, cam_id):
        # at

        R, T = look_at_view_transform(eye=[self.cam_pos[cam_id]],
                                      at=((0, self.mesh_y_center, 0),),
                                      up=((0, 1, 0),))

        camera = FoVOrthographicCameras(device=self.device,
                                        R=R,
                                        T=T,
                                        znear=100.0,
                                        zfar=-100.0,
                                        max_y=100.0,
                                        min_y=-100.0,
                                        max_x=100.0,
                                        min_x=-100.0,
                                        scale_xyz=(self.scale * np.ones(3),))
        return camera

    def init_renderer(self, camera, type='clean_mesh', bg='gray'):

        if 'mesh' in type:
            # rasterizer
            self.raster_settings_mesh = RasterizationSettings(
                image_size=self.size,
                blur_radius=np.log(1.0 / 1e-4) * 1e-7,
                faces_per_pixel=30,
            )
            self.meshRas = MeshRasterizer(cameras=camera,
                                          raster_settings=self.raster_settings_mesh)

        if bg == 'black':
            blendparam = BlendParams(1e-4, 1e-4, (0.0, 0.0, 0.0))
        elif bg == 'white':
            blendparam = BlendParams(1e-4, 1e-8, (1.0, 1.0, 1.0))
        elif bg == 'gray':
            blendparam = BlendParams(1e-4, 1e-8, (0.5, 0.5, 0.5))

        if type == 'ori_mesh':
            lights = PointLights(device=self.device,
                                 ambient_color=((0.8, 0.8, 0.8),),
                                 diffuse_color=((0.2, 0.2, 0.2),),
                                 specular_color=((0.0, 0.0, 0.0),),
                                 location=[[0.0, 200.0, 200.0]])
            self.renderer = MeshRenderer(
                rasterizer=self.meshRas,
                shader=SoftPhongShader(
                    device=self.device,
                    cameras=camera,
                    lights=lights,
                    blend_params=blendparam))

        if type == 'silhouette':
            self.raster_settings_silhouette = RasterizationSettings(
                image_size=self.size,
                blur_radius=np.log(1. / 1e-4 - 1.) * 5e-5,
                faces_per_pixel=50,
                cull_backfaces=True,
            )

            self.silhouetteRas = MeshRasterizer(
                cameras=camera, raster_settings=self.raster_settings_silhouette)
            self.renderer = MeshRenderer(rasterizer=self.silhouetteRas,
                                         shader=SoftSilhouetteShader())

        if type == 'pointcloud':
            self.raster_settings_pcd = PointsRasterizationSettings(
                image_size=self.size,
                radius=0.006,
                points_per_pixel=10)

            self.pcdRas = PointsRasterizer(cameras=camera,
                                           raster_settings=self.raster_settings_pcd)
            self.renderer = PointsRenderer(
                rasterizer=self.pcdRas,
                compositor=AlphaCompositor(background_color=(0, 0, 0)))

        if type == 'clean_mesh':
            self.renderer = MeshRenderer(
                rasterizer=self.meshRas,
                shader=cleanShader(
                    device=self.device,
                    cameras=camera,
                    blend_params=blendparam))

    def set_camera(self, verts, normalize=False):
        self.scale = 100
        self.mesh_y_center = 0
        if normalize:
            y_max = verts.max(dim=1)[0][0, 1].item()
            y_min = verts.min(dim=1)[0][0, 1].item()
            self.scale *= 0.95 / ((y_max - y_min) * 0.5 + 1e-10)
            self.mesh_y_center = (y_max + y_min) * 0.5

        self.cam_pos = [(0, self.mesh_y_center, self.dis),
                        (self.dis, self.mesh_y_center, 0),
                        (0, self.mesh_y_center, -self.dis),
                        (-self.dis, self.mesh_y_center, 0)]

    def load_mesh(self, verts, faces, verts_rgb=None, normalize=False, use_normal=False):
        """load mesh into the pytorch3d renderer

        Args:
            verts ([N,3]): verts
            faces ([N,3]): faces
            verts_rgb ([N,3]): rgb
            normalize: bool
        """

        if not torch.is_tensor(verts):
            verts = torch.tensor(verts)
        if not torch.is_tensor(faces):
            faces = torch.tensor(faces)

        if verts.ndimension() == 2:
            verts = verts.unsqueeze(0).float()
        if faces.ndimension() == 2:
            faces = faces.unsqueeze(0).long()

        verts = verts.to(self.device)
        faces = faces.to(self.device)
        self.set_camera(verts, normalize)
        self.mesh = Meshes(verts, faces).to(self.device)

        if verts_rgb is not None:
            if not torch.is_tensor(verts_rgb):
                verts_rgb = torch.as_tensor(verts_rgb)
            if verts_rgb.ndimension() == 2:
                verts_rgb = verts_rgb.unsqueeze(0).float()
            verts_rgb = verts_rgb.to(self.device)
        elif use_normal:
            verts_rgb = self.mesh.verts_normals_padded()
            verts_rgb = (verts_rgb + 1.0) * 0.5
        else:
            verts_rgb = self.mesh.verts_normals_padded()[..., 2:3].expand(-1, -1, 3)
            verts_rgb = (verts_rgb + 1.0) * 0.5
        textures = TexturesVertex(verts_features=verts_rgb)
        self.mesh.textures = textures
        return self.mesh

    def load_pcd(self, verts, verts_rgb, normalize=False):
        """load pointcloud into the pytorch3d renderer

        Args:
            verts ([B, N,3]): verts
            verts_rgb ([B, N,3]): verts colors
            normalize bool: render point cloud in center
        """
        assert verts.shape == verts_rgb.shape and len(verts.shape) == 3
        # data format convert
        if not torch.is_tensor(verts):
            verts = torch.as_tensor(verts)
        if not torch.is_tensor(verts_rgb):
            verts_rgb = torch.as_tensor(verts_rgb)

        verts = verts.float().to(self.device)
        verts_rgb = verts_rgb.float().to(self.device)

        # camera setting
        self.set_camera(verts, normalize)
        pcd = Pointclouds(points=verts, features=verts_rgb).to(self.device)
        return pcd

    def get_image(self, cam_ids=[0, 2], type='clean_mesh', bg='gray'):
        images = []
        for cam_id in range(len(self.cam_pos)):
            if cam_id in cam_ids:
                self.init_renderer(self.get_camera(cam_id), type, bg)
                rendered_img = self.renderer(self.mesh)[0, :, :, :3]
                if cam_id == 2 and len(cam_ids) == 2:
                    rendered_img = torch.flip(rendered_img, dims=[1])
                images.append(rendered_img)
        images = torch.cat(images, 1)
        return images.detach().cpu().numpy()

    def get_clean_image(self, cam_ids=[0, 2], type='clean_mesh', bg='gray'):
        images = []
        for cam_id in range(len(self.cam_pos)):
            if cam_id in cam_ids:
                self.init_renderer(self.get_camera(cam_id), type, bg)
                rendered_img = self.renderer(self.mesh)[0:1, :, :, :3]
                if cam_id == 2 and len(cam_ids) == 2:
                    rendered_img = torch.flip(rendered_img, dims=[2])
                images.append(rendered_img)
        return images

    def get_silhouette_image(self, cam_ids=[0, 2]):
        images = []
        for cam_id in range(len(self.cam_pos)):
            if cam_id in cam_ids:
                self.init_renderer(self.get_camera(cam_id), 'silhouette')
                rendered_img = self.renderer(self.mesh)[0:1, :, :, 3]
                if cam_id == 2 and len(cam_ids) == 2:
                    rendered_img = torch.flip(rendered_img, dims=[2])
                images.append(rendered_img)

        return images

    def get_image_pcd(self, pcd, cam_ids=[0, 1, 2, 3]):
        images = torch.zeros((self.size, self.size * len(cam_ids), 3)).to(self.device)
        for i, cam_id in enumerate(cam_ids):
            self.init_renderer(self.get_camera(cam_id), 'pointcloud')
            images[:, self.size * i:self.size * (i + 1), :] = self.renderer(pcd)[0, :, :, :3]

        return images.cpu().numpy()

    def get_rendered_video(self, image, save_path):
        self.cam_pos = []
        for angle in range(360):
            self.cam_pos.append(
                (self.dis * math.cos(np.pi / 180 * angle), self.mesh_y_center,
                 self.dis * math.sin(np.pi / 180 * angle)))

        old_shape = np.array(image.shape[:2])
        new_shape = np.around((self.size / old_shape[0]) * old_shape).astype(np.int)

        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        video = cv2.VideoWriter(save_path, fourcc, 30, (self.size + new_shape[1], self.size))

        for cam_id in range(len(self.cam_pos)):
            self.init_renderer(self.get_camera(cam_id), 'clean_mesh', 'gray')
            rendered_img = (self.renderer(self.mesh)[0, :, :, :3] * 255.0).detach().cpu().numpy().astype(np.uint8)
            final_img = np.concatenate([image, rendered_img], axis=1)
            video.write(final_img)

        video.release()

import nvdiffrast.torch as dr

class NVDiffRender(torch.nn.Module):
    def __init__(self):

        super().__init__()
        # try:
        # self.glctx = dr.RasterizeCudaContext()
        # except:
        self.glctx = dr.RasterizeGLContext()

    def forward(self, vertices, faces, h=512, w=512, spp=1):

        def scale_img_nhwc(x, size, mag='bilinear', min='bilinear'):
            assert (x.shape[1] >= size[0] and x.shape[2] >= size[1]) or (x.shape[1] < size[0] and x.shape[2] < size[
                1]), "Trying to magnify image in one dimension and minify in the other"
            y = x.permute(0, 3, 1, 2)  # NHWC -> NCHW
            if x.shape[1] > size[0] and x.shape[2] > size[1]:  # Minification, previous size was bigger
                y = torch.nn.functional.interpolate(y, size, mode=min)
            else:  # Magnification
                if mag == 'bilinear' or mag == 'bicubic':
                    y = torch.nn.functional.interpolate(y, size, mode=mag, align_corners=True)
                else:
                    y = torch.nn.functional.interpolate(y, size, mode=mag)
            return y.permute(0, 2, 3, 1).contiguous()  # NCHW -> NHWC

        res = (int(h * spp), int(w * spp)) if spp > 1 else (h, w)
        rast, rast_db = dr.rasterize(self.glctx, vertices, faces, res)

        ################################################################################
        # Interpolate attributes
        ################################################################################

        # Interpolate world space position
        alpha, _ = dr.interpolate(torch.ones_like(vertices[..., :1]), rast, faces)  # [B, H, W, 1]

        # compute normal
        from lib.data.mesh_util import compute_normal_torch
        vn = compute_normal_torch(vertices[0], faces)
        normal, _ = dr.interpolate(vn[None, ...].float(), rast, faces)

        ################################################################################
        # Shade
        ################################################################################
        normal = (normal + 1) / 2.
        normal = dr.antialias(normal, rast, vertices, faces).clamp(0, 1)  # [H, W, 3]
        alpha = dr.antialias(alpha, rast, vertices, faces).clamp(0, 1)  # [H, W, 3]

        # inverse super-sampling
        if spp > 1:
            alpha = scale_img_nhwc(alpha, (h, w))
            normal = scale_img_nhwc(normal, (h, w))

        return normal, alpha

if __name__ == '__main__':
    mesh = trimesh.load("./out/res/car-normal-1view/rp/rp_lilly_rigged_004_MAX.obj")
    render = NVDiffRender()
    v = torch.as_tensor(mesh.vertices).cuda()
    f = torch.as_tensor(mesh.faces).long().cuda()
    render(v, f)


from tkinter import*
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
from pygame import mixer

import thinkdsp
import thinkplot
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
from scipy.fftpack import fft
from scipy.io import wavfile
from pydub import AudioSegment

#our files imports
import Preprocessing as pr
import transcribe_enhanced_model as tr
import NLP as nlp

root = Tk()
mixer.init()

# import file

def browse_file():
    global filename
    filename = filedialog.askopenfilename()
    print(filename)

    # Audio preprocessing
    pr.noise_reductionM1(filename)
    # conversion from other formats to wav

def new_window1(words):
    global win1
    try:
        if win1.state() == "normal": win1.focus()
    except:
        win1 = Toplevel()
        win1.geometry("300x300+500+200")
        win1["bg"] = "white"
        lb = Label(win1, text=words)
        lb.pack()

def analyse():
    path=filename
    tr.transcribe_file_with_enhanced_model(path, "en-US")
    words=nlp.profanity()
    new_window1(words)
    '''try:
        if Tamil:
            tr.transcribe_file_with_enhanced_model(path,"ta-LK")
        elif English:
            tr.transcribe_file_with_enhanced_model(path,"en-US")
        elif Sinhala:
            tr.transcribe_file_with_enhanced_model(path,"si-LK")
        else:
            messagebox.showinfo("Language Error", "Select a language to Analyse")
    except:
        messagebox.showinfo("Language Error", "Select a language to Analyse")
'''
def play_music():

  try:
    paused

  except NameError:
    try:
        mixer.music.load(filename)
        mixer.music.play()

    except:
        messagebox.showerror("error")

  else:
    mixer.music.unpause()

def stop_music():
    mixer.music.stop()

def set_vol(val):
    volume = int(val)/100
    mixer.music.set_volume(volume)

def pause_music():
    global paused
    paused = TRUE
    mixer.music.pause()

def rewind_music():
    play_music()

#def mute_music():
 #   volumeBtn.configure(image=mutePhoto)

def doaction():
    print("action")

root.title("Shabdha- Automatic Audio Replacement Tool")
root.geometry("750x560")
root.iconbitmap(r'melody.ico')

# ****main menu *****************************************************

menu = Menu(root)
root.config(menu=menu)

fileMenu = Menu(menu)
menu.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="New Project", command=doaction)
fileMenu.add_command(label="Open", command=doaction)
fileMenu.add_command(label="Save", command=doaction)
fileMenu.add_command(label="Save as", command=doaction)
fileMenu.add_command(label="Import", command=doaction)
fileMenu.add_command(label="Export", command=doaction)
fileMenu.add_command(label="Setting", command=doaction)
fileMenu.add_command(label="Exit", command=doaction)

editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Undo", command=doaction)
editMenu.add_command(label="Redo", command=doaction)

viewMenu = Menu(menu)
menu.add_cascade(label="Veiw", menu=viewMenu)
viewMenu.add_command(label="Maximize", command=doaction)
viewMenu.add_command(label="Minimize", command=doaction)

helpMenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpMenu)
helpMenu.add_command(label="Help?", command=doaction)

# ************* buttons***********************************************

button1 = Button(text='Add', fg='white', bg='green', font=10, command=browse_file).place(x=55, y=50)
button2 = Button(text='Remove', fg='white', bg='orange', font=10).place(x=120, y=50)
button3 = Button(text='Analyse', fg='white', bg='green', font=10, command=analyse).place(x=650, y=500)

# *********** radio ********************************************************
theLabel = Label(root, text="Language").place(x=100, y=300)

r =IntVar()
Tamil = Radiobutton(root, text='Tamil', variable=r, value=1).place(x=100, y=320)
Sinhala = Radiobutton(root, text='Sinhala', variable=r, value=2).place(x=100, y=340)
English = Radiobutton(root, text='English', variable=r, value=3).place(x=100, y=360)

# *******************check boxes ***********************************************

theLabel2 = Label(root, text="Objectionable Word Type").place(x=400, y=300)

var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var4 = IntVar()
var5 = IntVar()

Checkbutton(root, text="Vain reference to deity", variable=var1).place(x=400, y=320)
Checkbutton(root, text="Ethnic and racial slurs", variable=var2).place(x=400, y=340)
Checkbutton(root, text="cursing", variable=var3).place(x=400, y=360)
Checkbutton(root, text="Strong profanity", variable=var4).place(x=400, y=380)
Checkbutton(root, text="User Define", variable=var5).place(x=400, y=400)

# ******************* text box *************************************

# theLabel3 = Label(root, text="User define word").place(x=700, y=300)

# ************ music player ******************
playPhoto = PhotoImage(file='play.png')
playBtn = Button(root, image=playPhoto, command=play_music).place(x=200, y=200)
# playBtn.pack()

stopPhoto = PhotoImage(file='stop.png')
stopBtn = Button(root, image=stopPhoto, command=stop_music).place(x=280, y=200)
# stopBtn.pack()

pausePhoto = PhotoImage(file='pause.png')
pauseBtn = Button(root, image=pausePhoto, command=pause_music).place(x=240, y=200)

rewindPhoto = PhotoImage(file='rewind.png')
rewindBtn = Button(root, image=rewindPhoto, command=rewind_music).place(x=320, y=200)

volumePhoto = PhotoImage(file='volume.png')
label = Label(root, image=volumePhoto).place(x=386, y=200)

scale = Scale(root, from_=0, to=100, orient=HORIZONTAL, command=set_vol)
scale.set(70)
mixer.music.set_volume(0.7)
scale.place(x=420, y=200)

root.mainloop()

filename="blood_audio.wav"


import os
import sys
from PyQt5 import QtWidgets, QtGui

class RenameFilesWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('Bulk File Rename Tool')
        self.setGeometry(100, 100, 500, 300)

        self.initUI()

    def initUI(self):
        # create directory label and line edit
        directory_label = QtWidgets.QLabel('Directory:', self)
        directory_label.move(20, 20)
        self.directory_line_edit = QtWidgets.QLineEdit(self)
        self.directory_line_edit.move(80, 20)
        self.directory_line_edit.resize(300, 25)
        self.directory_line_edit.setReadOnly(True)

        # create directory browse button
        browse_button = QtWidgets.QPushButton('Browse', self)
        browse_button.move(400, 20)
        browse_button.clicked.connect(self.selectDirectory)

        # create prefix label and line edit
        prefix_label = QtWidgets.QLabel('Prefix:', self)
        prefix_label.move(20, 60)
        self.prefix_line_edit = QtWidgets.QLineEdit(self)
        self.prefix_line_edit.move(80, 60)
        self.prefix_line_edit.resize(100, 25)

        # create suffix label and line edit
        suffix_label = QtWidgets.QLabel('Suffix:', self)
        suffix_label.move(200, 60)
        self.suffix_line_edit = QtWidgets.QLineEdit(self)
        self.suffix_line_edit.move(260, 60)
        self.suffix_line_edit.resize(100, 25)

        # create rename button
        rename_button = QtWidgets.QPushButton('Rename', self)
        rename_button.move(400, 60)
        rename_button.clicked.connect(self.renameFiles)

    def selectDirectory(self):
        directory = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select Directory')
        self.directory_line_edit.setText(directory)

    def renameFiles(self):
        directory = self.directory_line_edit.text()
        prefix = self.prefix_line_edit.text()
        suffix = self.suffix_line_edit.text()

        if not directory:
            QtWidgets.QMessageBox.critical(self, 'Error', 'Please select a directory.')
            return

        files = os.listdir(directory)
        for i, file in enumerate(files):
            old_file_path = os.path.join(directory, file)
            new_file_path = os.path.join(directory, f'{prefix}{i + 1}{suffix}{os.path.splitext(file)[1]}')
            os.rename(old_file_path,os.rename(old_file_path, new_file_path))

        QtWidgets.QMessageBox.information(self, 'Success', 'Files renamed successfully.')

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = RenameFilesWindow()
    window.show()
    sys.exit(app.exec_())


import os
import json
from tqdm import tqdm,trange
from udicOpenData.stopwords import *
import numpy as np

def getContent(allFileList):
    content_list = []
    for files in tqdm(allFileList):
        with open(path + files,"r",encoding='UTF-8') as f:
            jf = json.load(f)
            articles_list = jf['articles']
            for article in articles_list:
                if article['article_title']:
                    content_list.append(article['content'])
    return content_list
                    # words = jieba.cut(article['content'])
                    # for word in words:
                    #     print(word)
                    # if "韓國瑜" in article['content']:
                    #     has_han += 1
                    # else:
                    #     unhan += 1
    # print(has_han,unhan)
def content_seg(content_list):
    seg_content = []
    for content in tqdm(content_list):
        seg_content.append(list(rmsw(content)))
        # for i in list(rmsw(content)):
        #     if i in seg_content:
        #         seg_content[i] += 1
        #     else:
        #         seg_content[i] = 1
    return seg_content

if __name__ == "__main__":
    path =  "Gossiping/"
    allFileList = os.listdir(path)
    content_list = getContent(allFileList)
    seg_content = content_seg(content_list)

    # print(seg_content[0])
    np.save("seg_data",seg_content)
    # with open("data","w",encoding='UTF-8') as f:
    #     f.write(seg_content)


nome = input("Hello!! What is your name :")
print ("Welcome market Toledo", nome.title())

menu = ""
caixa = 0
receber = 0

while (menu != "s"):
	print ("Digite a opção desejada")
	print ("a. Venda - b. Receber - c. Consultar Caixa -s. Sair")
	menu = input("")
	if (menu == "a"):
		venda = float(input("Digite O valor da venda:"))
		print ("O seu caixa atual é:", caixa + venda)
		caixa = caixa + venda
	elif (menu == "b"):
                venda = float(input("Digite O valor da venda:"))
                print ("O seu caixa atual é:", caixa + venda + receber)
                caixa = caixa + venda + receber
	elif (menu == "c"):
		print("O valor em caixa é:", caixa)

	else:
		print("------------- END ----------")
		print("The system has been terminated, restart!!!")


import argparse
import logging
import os
import sys
import operator
import collections
import shutil

import assemblyline
from assemblyline.lib.bx.intersection import Interval, IntervalTree
from assemblyline.lib.bx.cluster import ClusterTree
from assemblyline.lib.base import Category, GTFAttr
from assemblyline.lib.gtf import GTFFeature, sort_gtf
from assemblyline.lib.transcript import cmp_strand, parse_gtf, \
    strand_int_to_str, NO_STRAND, POS_STRAND, NEG_STRAND
from assemblyline.lib.assemble.transcript_graph import \
    find_exon_boundaries, split_exons

# for nearest transcripts calculation
MAX_LOCUS_DIST = 100000000

class CompareData(object):
    __slots__ = ('has_ref', 'has_test', 'category')
    def __init__(self):
        self.has_ref = False
        self.has_test = False
        self.category = None

class GlobalStats(object):
    FIELDS = ('introns_both', 'introns_ref_only', 'introns_test_only',
              'patterns_both', 'patterns_ref_only', 'patterns_test_only',
              'cov_both', 'cov_ref_only', 'cov_test_only')

    def __init__(self):
        for field in GlobalStats.FIELDS:
            setattr(self, field, 0)
        self.introns_by_category = collections.defaultdict(lambda: 0)
        self.patterns_by_category = collections.defaultdict(lambda: 0)
        self.cov_by_category = collections.defaultdict(lambda: 0)

    def report(self):
        # print stats report
        introns_total = self.introns_both + self.introns_ref_only + self.introns_test_only
        patterns_total = self.patterns_both + self.patterns_ref_only + self.patterns_test_only
        cov_total = self.cov_both + self.cov_ref_only + self.cov_test_only
        lines = ["introns_total=%d" % (introns_total),
                 "introns_both=%d" % (self.introns_both),
                 "introns_ref_only=%d" % (self.introns_ref_only),
                 "introns_test_only=%d" % (self.introns_test_only),
                 "introns_precision=%f" % (self.introns_both / float(max(1,self.introns_both + self.introns_test_only))),
                 "introns_recall=%f" % (self.introns_both / float(max(1,self.introns_both + self.introns_ref_only))),
                 "patterns_total=%d" % (patterns_total),
                 "patterns_both=%d" % (self.patterns_both),
                 "patterns_ref_only=%d" % (self.patterns_ref_only),
                 "patterns_test_only=%d" % (self.patterns_test_only),
                 "patterns_precision=%f" % (self.patterns_both / float(max(1,self.patterns_both + self.patterns_test_only))),
                 "patterns_recall=%f" % (self.patterns_both / float(max(1,self.patterns_both + self.patterns_ref_only))),
                 "cov_total=%d" % (cov_total),
                 "cov_both=%d" % (self.cov_both),
                 "cov_ref_only=%d" % (self.cov_ref_only),
                 "cov_test_only=%d" % (self.cov_test_only),
                 "cov_precision=%f" % (self.cov_both / float(max(1,self.cov_both + self.cov_test_only))),
                 "cov_recall=%f" % (self.cov_both / float(max(1,self.cov_both + self.cov_ref_only)))]
        for k in sorted(self.introns_by_category):
            lines.append("introns %s=%d" % (k,self.introns_by_category[k]))
        for k in sorted(self.patterns_by_category):
            lines.append("patterns %s=%d" % (k,self.patterns_by_category[k]))
        for k in sorted(self.cov_by_category):
            lines.append("cov %s=%d" % (k,self.cov_by_category[k]))
        return '\n'.join(lines)

    @staticmethod
    def from_file(filename):
        self = GlobalStats()
        with open(filename) as f:
            f.next() # introns total
            self.introns_both = int(f.next().split('=')[1])
            self.introns_ref_only = int(f.next().split('=')[1])
            self.introns_test_only = int(f.next().split('=')[1])
            f.next() # prec
            f.next() # recall
            f.next() # patterns total
            self.patterns_both = int(f.next().split('=')[1])
            self.patterns_ref_only = int(f.next().split('=')[1])
            self.patterns_test_only = int(f.next().split('=')[1])
            f.next() # prec
            f.next() # recall
            f.next() # cov total
            self.cov_both = int(f.next().split('=')[1])
            self.cov_ref_only = int(f.next().split('=')[1])
            self.cov_test_only = int(f.next().split('=')[1])
        return self

    def compute(self, transcripts):
        intron_dict = collections.defaultdict(lambda: CompareData())
        node_dict = collections.defaultdict(lambda: CompareData())
        splicing_pattern_dict = collections.defaultdict(lambda: CompareData())
        # find the intron domains of the transcripts
        boundaries = find_exon_boundaries(transcripts)
        unstranded_transcripts = []
        for t in transcripts:
            if t.strand == NO_STRAND:
                unstranded_transcripts.append(t)
                continue
            # separate ref and nonref transcripts
            is_ref = bool(int(t.attrs[GTFAttr.REF]))
            # split exons that cross boundaries and get the
            # nodes in the transcript path
            for n in split_exons(t, boundaries):
                n = (t.strand, n[0], n[1])
                if is_ref:
                    node_dict[n].has_ref = True
                else:
                    d = node_dict[n]
                    d.has_test = True
                    d.category = t.attrs['category']
            splicing_pattern = []
            for start,end in t.iterintrons():
                n = (t.strand, start, end)
                if is_ref:
                    intron_dict[n].has_ref = True
                else:
                    d = intron_dict[n]
                    d.has_test = True
                    d.category = t.attrs['category']
                splicing_pattern.append(n)
            splicing_pattern = tuple(splicing_pattern)
            if len(splicing_pattern) > 0:
                if is_ref:
                    splicing_pattern_dict[splicing_pattern].has_ref = True
                else:
                    d = splicing_pattern_dict[splicing_pattern]
                    d.has_test = True
                    d.category = t.attrs['category']
        # handle unstranded transcripts
        for t in unstranded_transcripts:
            # separate ref and nonref transcripts
            is_ref = bool(int(t.attrs[GTFAttr.REF]))
            for n in split_exons(t, boundaries):
                found_node = False
                for strand in (POS_STRAND, NEG_STRAND):
                    sn = (strand, n[0], n[1])
                    if sn in node_dict:
                        if is_ref:
                            node_dict[sn].has_ref = True
                        else:
                            d = node_dict[sn]
                            d.has_test = True
                            d.category = t.attrs['category']
                        found_node = True
                if not found_node:
                    sn = (NO_STRAND, n[0], n[1])
                    if is_ref:
                        node_dict[sn].has_ref = True
                    else:
                        d = node_dict[sn]
                        d.has_test = True
                        d.category = t.attrs['category']
            introns = list(t.iterintrons())
            assert len(introns) == 0
        # compile statistics
        for d in intron_dict.itervalues():
            if d.has_ref and d.has_test:
                self.introns_both += 1
                self.introns_by_category[d.category] += 1
            elif d.has_ref:
                self.introns_ref_only += 1
            elif d.has_test:
                self.introns_test_only += 1
                self.introns_by_category[d.category] += 1
        for d in splicing_pattern_dict.itervalues():
            if d.has_ref and d.has_test:
                self.patterns_both += 1
                self.patterns_by_category[d.category] += 1
            elif d.has_ref:
                self.patterns_ref_only += 1
            elif d.has_test:
                self.patterns_test_only += 1
                self.patterns_by_category[d.category] += 1
        for n,d in node_dict.iteritems():
            strand, start, end = n
            length = end - start
            if d.has_ref and d.has_test:
                self.cov_both += length
                self.cov_by_category[d.category] += length
            elif d.has_ref:
                self.cov_ref_only += length
            elif d.has_test:
                self.cov_test_only += length
                self.cov_by_category[d.category] += length

class Match(object):
    def __init__(self):
        self.nodes = collections.defaultdict(lambda: [])
        self.introns = []
        self.splicing = False

class MatchStats(object):
    @staticmethod
    def header_fields():
        return ['transcript_id', 'gene_id', 'locus', 'length', 'num_introns',
                'ref_transcript_id', 'ref_gene_id', 'ref_orig_gene_id', 'ref_gene_name',
                'ref_source', 'ref_gene_type', 'ref_locus',
                'ref_length', 'ref_num_introns',
                'shared_same_strand_bp', 'shared_opp_strand_bp',
                'shared_introns', 'shared_splicing',
                'distance', 'category']

    def __init__(self):
        for field in MatchStats.header_fields():
            setattr(self, field, None)

    def __str__(self):
        fields = []
        for field in MatchStats.header_fields():
            fields.append(getattr(self, field))
        return '\t'.join(map(str,fields))

    def copy(self):
        other = MatchStats()
        for field in MatchStats.header_fields():
            setattr(other, field, getattr(self,field))
        return other

    def add_gtf_attributes(self, feature):
        attrs = ['ref_transcript_id', 'ref_gene_id', 'ref_orig_gene_id',
                 'ref_gene_name', 'ref_source', 'ref_gene_type',
                 'ref_locus', 'ref_length', 'ref_num_introns',
                 'shared_same_strand_bp', 'shared_opp_strand_bp',
                 'shared_introns', 'shared_splicing',
                 'distance', 'category']
        for attr in attrs:
            v = getattr(self, attr)
            feature.attrs[attr] = v

    @staticmethod
    def from_transcript(t, ref=None):
        self = MatchStats()
        self.transcript_id = t.attrs[GTFAttr.TRANSCRIPT_ID]
        self.gene_id = t.attrs[GTFAttr.GENE_ID]
        self.locus = '%s:%d-%d[%s]' % (t.chrom, t.start, t.end, strand_int_to_str(t.strand))
        self.length = t.length
        self.num_introns = len(t.exons) - 1
        if ref is not None:
            self.ref_transcript_id = ref.attrs[GTFAttr.TRANSCRIPT_ID]
            self.ref_gene_id = ref.attrs[GTFAttr.GENE_ID]
            self.ref_locus = '%s:%d-%d[%s]' % (ref.chrom, ref.start, ref.end, strand_int_to_str(ref.strand))
            self.ref_length = ref.length
            self.ref_num_introns = len(ref.exons) - 1
            self.ref_orig_gene_id = ref.attrs.get('orig_gene_id', self.ref_gene_id)
            self.ref_source = ref.attrs.get('source', 'NA')
            if 'gene_name' in ref.attrs:
                self.ref_gene_name = ref.attrs['gene_name']
            elif 'transcript_name' in ref.attrs:
                self.ref_gene_name = ref.attrs['transcript_name']
            else:
                self.ref_gene_name = self.ref_gene_id
            if 'gene_type' in ref.attrs:
                self.ref_gene_type = ref.attrs['gene_type']
            elif 'gene_biotype' in ref.attrs:
                self.ref_gene_type = ref.attrs['gene_biotype']
            elif 'transcript_type' in ref.attrs:
                self.ref_gene_type = ref.attrs['transcript_type']
            else:
                self.ref_gene_type = 'None'
        return self

    @staticmethod
    def choose_best(lst):
        hits = []
        for m in lst:
            total_introns = m.num_introns + m.ref_num_introns
            if total_introns == 0:
                intron_frac = 0.0
            else:
                intron_frac = float(m.shared_introns) / (total_introns - m.shared_introns)
            same_strand_frac = float(m.shared_same_strand_bp) / (m.length + m.ref_length - m.shared_same_strand_bp)
            opp_strand_frac = float(m.shared_opp_strand_bp) / (m.length + m.ref_length - m.shared_opp_strand_bp)
            category_int = Category.to_int(m.category)
            hits.append((int(m.shared_splicing), intron_frac,
                         same_strand_frac, opp_strand_frac,
                         int(category_int == Category.INTRONIC_SAME_STRAND),
                         int(category_int == Category.INTRONIC_OPP_STRAND),
                         int(category_int == Category.INTERLEAVING_SAME_STRAND),
                         int(category_int == Category.INTERLEAVING_OPP_STRAND),
                         int(category_int == Category.ENCOMPASSING_SAME_STRAND),
                         int(category_int == Category.ENCOMPASSING_OPP_STRAND),
                         int(category_int == Category.INTERGENIC),
                         -abs(m.distance), m))
        # sort matches
        hits.sort(reverse=True)
        hit = hits[0][-1]
        return hit

    @staticmethod
    def sort_genome(lst):
        poslst = []
        strands = []
        for m in lst:
            chrom, startend = m.ref_locus[:-3].split(':')
            strands.append(m.ref_locus[-2])
            start, end = map(int, startend.split('-'))
            poslst.append((chrom, start, end, m))
        reverse = any(x == '-' for x in strands)
        poslst.sort(key=operator.itemgetter(0,1,2), reverse=reverse)
        return [x[3] for x in poslst]

    @staticmethod
    def consensus(lst):
        if len(lst) == 0:
            return None
        # first check for read through transcripts involving multiple
        # reference genes
        same_strand_hits = collections.defaultdict(lambda: [])
        for m in lst:
            category_int = Category.to_int(m.category)
            if category_int == Category.SAME_STRAND:
                same_strand_hits[m.ref_gene_id].append(m)
        # no same strand matches so don't need to worry about
        # read-throughs or multiple gene types
        if len(same_strand_hits) == 0:
            return MatchStats.choose_best(lst)
        # get consensus match from same strand overlapping genes
        total_introns = lst[0].num_introns
        total_length = lst[0].length
        shared_introns = 0
        shared_same_strand_bp = 0
        hits = []
        for genelst in same_strand_hits.itervalues():
            m = MatchStats.choose_best(genelst).copy()
            m.ref_gene_type = ','.join(sorted(set(m.ref_gene_type for m in genelst)))
            total_introns += m.ref_num_introns
            total_length += m.ref_length
            shared_introns += m.shared_introns
            shared_same_strand_bp += m.shared_same_strand_bp
            hits.append(m)
        # sort reference genes by position
        hits = MatchStats.sort_genome(hits)
        # make a new MatchStats object
        hit = hits[0].copy()
        hit.ref_transcript_id = ','.join(x.ref_transcript_id for x in hits)
        hit.ref_gene_id = ','.join(x.ref_gene_id for x in hits)
        hit.ref_orig_gene_id = ','.join(x.ref_orig_gene_id for x in hits)
        hit.ref_gene_name = ','.join(x.ref_gene_name for x in hits)
        hit.ref_source = ','.join(x.ref_source for x in hits)
        hit.ref_gene_type = ','.join(x.ref_gene_type for x in hits)
        hit.ref_locus = ','.join(x.ref_locus for x in hits)
        hit.ref_length = ','.join(str(x.ref_length) for x in hits)
        hit.ref_num_introns = ','.join(str(x.ref_num_introns) for x in hits)
        hit.shared_same_strand_bp = shared_same_strand_bp
        hit.shared_opp_strand_bp = 0
        hit.shared_introns = shared_introns
        hit.shared_splicing = any(m.shared_splicing for m in hits)
        hit.distance = 0
        if len(same_strand_hits) > 1:
            hit.category = Category.to_str(Category.READ_THROUGH)
        return hit

def compare_locus(transcripts):
    # store reference introns
    # (strand,start,end) -> ids (set)
    ref_intron_dict = collections.defaultdict(lambda: [])
    ref_node_dict = collections.defaultdict(lambda: [])
    ref_splicing_patterns = collections.defaultdict(lambda: [])
    ref_dict = {}
    # find the intron domains of the transcripts
    boundaries = find_exon_boundaries(transcripts)
    test_transcripts = []
    for t in transcripts:
        # separate ref and nonref transcripts
        is_ref = bool(int(t.attrs[GTFAttr.REF]))
        if is_ref:
            # add to dict
            ref_id = t.attrs[GTFAttr.TRANSCRIPT_ID]
            ref_dict[ref_id] = t
            # split exons that cross boundaries and get the
            # nodes in the transcript path
            for n in split_exons(t, boundaries):
                ref_node_dict[n].append(t)
            # add to introns
            splicing_pattern = []
            for start,end in t.iterintrons():
                intron = (t.strand, start, end)
                ref_intron_dict[intron].append(t)
                splicing_pattern.append(intron)
            # add to splicing patterns
            if len(splicing_pattern) > 0:
                ref_splicing_patterns[tuple(splicing_pattern)].append(t)
        else:
            test_transcripts.append(t)
    # index introns for fast intersection
    intron_tree = IntervalTree()
    for intron, refs in ref_intron_dict.iteritems():
        strand, start, end = intron
        intron_tree.insert_interval(Interval(start,end,strand=strand,value=refs))
    # categorize transcripts
    for t in test_transcripts:
        # get transcript nodes and introns
        nodes = list(split_exons(t, boundaries))
        introns = []
        for start,end in t.iterintrons():
            introns.append((t.strand,start,end))
        splicing_pattern = tuple(introns)
        # keep list of all matching ref transcripts
        matches = collections.defaultdict(lambda: Match())
        # dict of reference transcripts -> category -> list of nodes
        for n in nodes:
            if n in ref_node_dict:
                # look for reference transcripts that share this node
                for ref in ref_node_dict[n]:
                    if cmp_strand(t.strand, ref.strand):
                        c = Category.SAME_STRAND
                    else:
                        c = Category.OPP_STRAND
                    ref_id = ref.attrs[GTFAttr.TRANSCRIPT_ID]
                    m = matches[ref_id]
                    m.nodes[c].append(n)
            # look for reference introns that overlap this node
            for hit in intron_tree.find(*n):
                if cmp_strand(t.strand, hit.strand):
                    c = Category.INTRONIC_SAME_STRAND
                else:
                    c = Category.INTRONIC_OPP_STRAND
                for ref in hit.value:
                    ref_id = ref.attrs[GTFAttr.TRANSCRIPT_ID]
                    m = matches[ref_id]
                    m.nodes[c].append(n)
        # dict of introns -> list of reference transcripts
        for intron in introns:
            if intron in ref_intron_dict:
                for ref in ref_intron_dict[intron]:
                    ref_id = ref.attrs[GTFAttr.TRANSCRIPT_ID]
                    m = matches[ref_id]
                    m.introns.append(intron)
        # check splicing pattern matches
        if len(splicing_pattern) > 0:
            if splicing_pattern in ref_splicing_patterns:
                for ref in ref_splicing_patterns[splicing_pattern]:
                    ref_id = ref.attrs[GTFAttr.TRANSCRIPT_ID]
                    m = matches[ref_id]
                    m.splicing = True
        # go through the matches for this transcript and determine
        # the transcript category
        match_stats = []
        for ref_id, m in matches.iteritems():
            ref = ref_dict[ref_id]
            # calculate coverage
            same_strand_bp = sum((n[1] - n[0]) for n in m.nodes[Category.SAME_STRAND])
            opp_strand_bp = sum((n[1] - n[0]) for n in m.nodes[Category.OPP_STRAND])
            # count shared introns
            num_shared_introns = len(m.introns)
            # decide category for this test/ref transcript pair
            if m.splicing or (num_shared_introns > 0) or (same_strand_bp > 0):
                c = Category.SAME_STRAND
            elif (opp_strand_bp > 0):
                c = Category.OPP_STRAND
            else:
                # count nodes of different types
                num_same_strand = len(m.nodes[Category.SAME_STRAND])
                num_opp_strand = len(m.nodes[Category.OPP_STRAND])
                num_intronic_same_strand = len(m.nodes[Category.INTRONIC_SAME_STRAND])
                num_intronic_opp_strand = len(m.nodes[Category.INTRONIC_OPP_STRAND])
                assert num_same_strand == 0
                assert num_opp_strand == 0
                num_intronic = (num_intronic_same_strand +
                                num_intronic_opp_strand)
                assert num_intronic > 0
                if (num_intronic == len(nodes)):
                    # completely intronic
                    if num_intronic_same_strand > 0:
                        c = Category.INTRONIC_SAME_STRAND
                    else:
                        c = Category.INTRONIC_OPP_STRAND
                else:
                    # interleaving means some nodes intronic and other intergenic
                    if num_intronic_same_strand > 0:
                        c = Category.INTERLEAVING_SAME_STRAND
                    else:
                        c = Category.INTERLEAVING_OPP_STRAND
            # create a match object
            ms = MatchStats.from_transcript(t, ref)
            ms.shared_same_strand_bp = same_strand_bp
            ms.shared_opp_strand_bp = opp_strand_bp
            ms.shared_introns = num_shared_introns
            ms.shared_splicing = m.splicing
            ms.category = Category.to_str(c)
            ms.distance = 0
            match_stats.append(ms)
        yield (t, match_stats)

def build_locus_trees(gtf_file):
    transcripts = []
    locus_cluster_trees = collections.defaultdict(lambda: ClusterTree(0,1))
    for locus_transcripts in parse_gtf(open(gtf_file)):
        for t in locus_transcripts:
            is_ref = bool(int(t.attrs[GTFAttr.REF]))
            if not is_ref:
                continue
            i = len(transcripts)
            transcripts.append(t)
            locus_cluster_trees[t.chrom].insert(t.start, t.end, i)
    # build interval trees of loci
    locus_trees = collections.defaultdict(lambda: IntervalTree())
    for chrom, cluster_tree in locus_cluster_trees.iteritems():
        for locus_start, locus_end, indexes in cluster_tree.getregions():
            for i in indexes:
                locus_transcripts = [transcripts[i] for i in indexes]
                locus_trees[chrom].insert_interval(Interval(locus_start, locus_end, value=locus_transcripts))
    return locus_trees

def find_nearest_transcripts(chrom, start, end, strand, locus_trees):
    # first check for overlap
    nearest_features = []
    hits = locus_trees[chrom].find(start, end)
    for hit in hits:
        for t in hit.value:
            if cmp_strand(t.strand, strand):
                c = Category.ENCOMPASSING_SAME_STRAND
            else:
                c = Category.ENCOMPASSING_OPP_STRAND
            nearest_features.append((t, c, 0))
    # look left and right
    left_hits = locus_trees[chrom].before(start, num_intervals=1, max_dist=MAX_LOCUS_DIST)
    right_hits = locus_trees[chrom].after(end, num_intervals=1, max_dist=MAX_LOCUS_DIST)
    # look for nearest hit
    for hits in (left_hits, right_hits):
        nearest_locus_hit = None
        nearest_dist = MAX_LOCUS_DIST
        for hit in hits:
            dist = min(abs(start - hit.end), abs(hit.start - end))
            if dist < nearest_dist:
                nearest_dist = dist
                nearest_locus_hit = hit
        if nearest_locus_hit is not None:
            for t in nearest_locus_hit.value:
                dist = min(abs(start - t.end), abs(t.start - end))
                nearest_features.append((t, Category.INTERGENIC, dist))
    return nearest_features

def _parse_gtf_by_chrom(gtf_file):
    current_chrom = None
    exon_dict = collections.defaultdict(lambda: [])
    transcript_dict = {}
    for feature in GTFFeature.parse(open(gtf_file)):
        if (current_chrom != feature.seqid):
            if len(exon_dict) > 0:
                yield current_chrom, transcript_dict, exon_dict
                exon_dict = collections.defaultdict(lambda: [])
                transcript_dict = {}
            current_chrom = feature.seqid
        t_id = feature.attrs[GTFAttr.TRANSCRIPT_ID]
        if feature.feature_type == "transcript":
            transcript_dict[t_id] = feature
        elif feature.feature_type == "exon":
            exon_dict[t_id].append(feature)
    if len(exon_dict) > 0:
        yield current_chrom, transcript_dict, exon_dict

def add_gtf_file(gtf_file, outfh, is_ref):
    refval = '1' if is_ref else '0'
    for chrom, transcript_dict, exon_dict in _parse_gtf_by_chrom(gtf_file):
        logging.debug("\tfinished chrom %s %d features" % (chrom, len(exon_dict)))
        # output reference transcripts
        for t_id, features in exon_dict.iteritems():
            # sort features (exons) by start position
            features.sort(key=operator.attrgetter('start'))
            # annotate exons as reference features
            for f in features:
                f.attrs[GTFAttr.REF] = refval
                print >>outfh, str(f)
            # transcript feature
            if t_id in transcript_dict:
                f = transcript_dict[t_id]
            else:
                f = GTFFeature()
                f.seqid = features[0].seqid
                f.source = features[0].source
                f.feature_type = 'transcript'
                f.start = features[0].start
                f.end = features[-1].end
                f.score = features[0].score
                f.strand = features[0].strand
                f.phase = '.'
                f.attrs = features[0].attrs.copy()
                if "exon_number" in f.attrs:
                    del f.attrs["exon_number"]
            f.attrs[GTFAttr.REF] = refval
            print >>outfh, str(f)

def compare_assemblies(ref_gtf_file, test_gtf_file, output_dir):
    # output files
    if not os.path.exists(output_dir):
        logging.info('Creating output dir: %s' % (output_dir))
        os.makedirs(output_dir)
    # merge step
    merged_gtf_file = os.path.join(output_dir, "merged.gtf")
    merged_sorted_gtf_file = os.path.splitext(merged_gtf_file)[0] + ".srt.gtf"
    merge_done_file = os.path.join(output_dir, 'merged.done')
    sort_done_file = os.path.join(output_dir, 'sort.done')
    if not os.path.exists(merge_done_file):
        # merge and sort ref/test gtf files
        logging.info("Merging reference and test GTF files")
        # make temporary file to store merged ref/test gtf files
        with open(merged_gtf_file, "w") as fileh:
            logging.info("Adding reference GTF file")
            add_gtf_file(ref_gtf_file, fileh, is_ref=True)
            logging.info("Adding test GTF file")
            add_gtf_file(test_gtf_file, fileh, is_ref=False)
        open(merge_done_file, 'w').close()
    if not os.path.exists(sort_done_file):
        logging.info("Sorting merged GTF file")
        # create temp directory
        tmp_dir = os.path.join(output_dir, 'tmp')
        if not os.path.exists(tmp_dir):
            logging.debug("Creating tmp directory '%s'" % (tmp_dir))
            os.makedirs(tmp_dir)
        sort_gtf(merged_gtf_file, merged_sorted_gtf_file, tmp_dir=tmp_dir)
        # cleanup
        shutil.rmtree(tmp_dir)
        open(sort_done_file, 'w').close()
    # compare assemblies
    overlapping_gtf_file = os.path.join(output_dir, 'overlapping.gtf')
    intergenic_tmp_gtf_file = os.path.join(output_dir, 'intergenic.tmp.gtf')
    overlapping_file = os.path.join(output_dir, 'overlapping.tsv')
    overlapping_consensus_file = os.path.join(output_dir, 'overlapping.consensus.tsv')
    overlapping_done_file = os.path.join(output_dir, 'overlapping.done')
    stats_file = os.path.join(output_dir, 'stats.txt')
    stats_obj = GlobalStats()
    num_intergenic = 0
    if not os.path.exists(overlapping_done_file):
        logging.info("Comparing assemblies")
        gtf_fileh = open(overlapping_gtf_file, 'w')
        tmp_gtf_fileh = open(intergenic_tmp_gtf_file, 'w')
        overlapping_fileh = open(overlapping_file, 'w')
        overlapping_consensus_fileh = open(overlapping_consensus_file, 'w')
        for locus_transcripts in parse_gtf(open(merged_sorted_gtf_file)):
            locus_chrom = locus_transcripts[0].chrom
            locus_start = locus_transcripts[0].start
            locus_end = max(t.end for t in locus_transcripts)
            logging.debug("[LOCUS] %s:%d-%d %d transcripts" %
                          (locus_chrom, locus_start, locus_end,
                           len(locus_transcripts)))
            for t, match_stats in compare_locus(locus_transcripts):
                if len(match_stats) == 0:
                    # write intergenic transcripts to analyze separately
                    t.attrs['category'] = Category.to_str(Category.INTERGENIC)
                    for f in t.to_gtf_features(source='assembly'):
                        print >>tmp_gtf_fileh, str(f)
                    num_intergenic += 1
                else:
                    # get consensus match information
                    consensus_match = MatchStats.consensus(match_stats)
                    assert consensus_match is not None
                    t.attrs['category'] = consensus_match.category
                    # add gtf attributes and write
                    for f in t.to_gtf_features(source='assembly'):
                        consensus_match.add_gtf_attributes(f)
                        print >>gtf_fileh, str(f)
                    # tab-delimited text output
                    print >>overlapping_consensus_fileh, str(consensus_match)
                    for ms in match_stats:
                        print >>overlapping_fileh, str(ms)
            # compute global statistics
            stats_obj.compute(locus_transcripts)
        logging.info("Reporting global statistics")
        with open(stats_file, 'w') as f:
            print >>f, stats_obj.report()
        gtf_fileh.close()
        tmp_gtf_fileh.close()
        overlapping_fileh.close()
        overlapping_consensus_fileh.close()
        open(overlapping_done_file, 'w').close()
    # resolve intergenic transcripts
    intergenic_gtf_file = os.path.join(output_dir, 'intergenic.gtf')
    intergenic_file = os.path.join(output_dir, 'intergenic.tsv')
    intergenic_best_file = os.path.join(output_dir, 'intergenic.best.tsv')
    intergenic_done_file = os.path.join(output_dir, 'intergenic.done')
    if not os.path.exists(intergenic_done_file):
        logging.info("Building interval index")
        locus_trees = build_locus_trees(merged_sorted_gtf_file)
        logging.info('Finding nearest matches to intergenic transcripts')
        gtf_fileh = open(intergenic_gtf_file, 'w')
        intergenic_fileh = open(intergenic_file, 'w')
        intergenic_best_fileh = open(intergenic_best_file, 'w')
        for locus_transcripts in parse_gtf(open(intergenic_tmp_gtf_file)):
            for t in locus_transcripts:
                # find nearest transcripts
                nearest_transcripts = find_nearest_transcripts(t.chrom, t.start, t.end, t.strand, locus_trees)
                match_stats = []
                best_match = None
                if len(nearest_transcripts) == 0:
                    best_match = MatchStats.from_transcript(t)
                    best_match.category = Category.to_str(Category.INTERGENIC)
                    match_stats.append(best_match)
                else:
                    for ref,category,dist in nearest_transcripts:
                        # create a match object
                        ms = MatchStats.from_transcript(t, ref)
                        ms.shared_same_strand_bp = 0
                        ms.shared_opp_strand_bp = 0
                        ms.shared_introns = 0
                        ms.shared_splicing = False
                        ms.category = Category.to_str(category)
                        ms.distance = dist
                        match_stats.append(ms)
                    # choose the consensus match
                    best_match = MatchStats.choose_best(match_stats)
                # add gtf attributes and write
                for f in t.to_gtf_features(source='assembly'):
                    best_match.add_gtf_attributes(f)
                    print >>gtf_fileh, str(f)
                # write tab-delimited data
                print >>intergenic_best_fileh, str(best_match)
                for ms in match_stats:
                    print >>intergenic_fileh, str(ms)
        gtf_fileh.close()
        intergenic_fileh.close()
        intergenic_best_fileh.close()
        open(intergenic_done_file, 'w').close()
    # merge overlapping and intergenic results
    logging.info('Merging results')
    metadata_file = os.path.join(output_dir, 'metadata.txt')
    metadata_consensus_file = os.path.join(output_dir, 'metadata.consensus.txt')
    assembly_gtf_file = os.path.join(output_dir, 'assembly.cmp.gtf')
    combine_done_file = os.path.join(output_dir, 'done')
    if not os.path.exists(combine_done_file):
        filenames = [overlapping_file, intergenic_file]
        with open(metadata_file, 'w') as outfile:
            print >>outfile, '\t'.join(MatchStats.header_fields())
            for fname in filenames:
                with open(fname) as infile:
                    for line in infile:
                        outfile.write(line)
        filenames = [overlapping_consensus_file, intergenic_best_file]
        with open(metadata_consensus_file, 'w') as outfile:
            print >>outfile, '\t'.join(MatchStats.header_fields())
            for fname in filenames:
                with open(fname) as infile:
                    for line in infile:
                        outfile.write(line)
        filenames = [intergenic_gtf_file, overlapping_gtf_file]
        with open(assembly_gtf_file, 'w') as outfile:
            for fname in filenames:
                with open(fname) as infile:
                    for line in infile:
                        outfile.write(line)
        open(combine_done_file, 'w').close()
    # cleanup
    logging.info("Done")

def main():
    # parse command line
    parser = argparse.ArgumentParser()
    parser.add_argument("-v", "--verbose", action="store_true",
                        dest="verbose", default=False)
    parser.add_argument("-o", "--output-dir", dest="output_dir",
                        default="compare")
    parser.add_argument("ref_gtf_file")
    parser.add_argument("test_gtf_file")
    args = parser.parse_args()
    # set logging level
    if args.verbose:
        level = logging.DEBUG
    else:
        level = logging.INFO
    logging.basicConfig(level=level,
                        format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    # check command line parameters
    if not os.path.exists(args.ref_gtf_file):
        parser.error("Reference GTF file %s not found" % (args.ref_gtf_file))
    if not os.path.exists(args.test_gtf_file):
        parser.error("Test GTF file %s not found" % (args.test_gtf_file))
    if not os.path.exists(args.output_dir):
        logging.debug("Creating output directory '%s'" % (args.output_dir))
        os.makedirs(args.output_dir)
    logging.info("AssemblyLine %s" % (assemblyline.__version__))
    logging.info("----------------------------------")
    # show parameters
    logging.info("Parameters:")
    logging.info("verbose logging:       %s" % (args.verbose))
    logging.info("reference gtf file:    %s" % (args.ref_gtf_file))
    logging.info("test gtf file:         %s" % (args.test_gtf_file))
    logging.info("output dir:            %s" % (args.output_dir))
    compare_assemblies(args.ref_gtf_file, args.test_gtf_file,
                       args.output_dir)
    return 0

if __name__ == "__main__":
    sys.exit(main())


# 퇴사까지 남은 일 수
N = int(input())
print(N)

# 근무표
schedule = [ list(map(int, input().split())) for _ in range(N) ]
#print(schedule)

# schedule[i][0] : 걸리는 일 수
# schedule[i][1] : 보수

arr = set()

# 상담을 잡을 수 있는 모든 경우의 수 탐색
def cal_pay(day, pay):

    # 퇴사일을 넘기는가?
    if day >= N:
        arr.add(pay)
        return

    # 현재 날짜의 일을 받을 수 있는가? 퇴사일 안에 받을 수 있는 일인지 확인
    if day + schedule[day][0] - 1 <= N-1:
        cal_pay(day + schedule[day][0], pay+schedule[day][1])

    cal_pay(day+1, pay)

#print('??', arr)
cal_pay(0, 0)
#print('!!', arr)

print(max(arr))


from scrapy import Spider
from core.db_utils import create_db_objects
from core.utils import (get_list_of_products, get_pagination, get_price,
                        get_random_user_agent)
from db.connect import get_session
from db.tables import stroyudacha_products
from scrapy import Request
from scrapy.http.response.html import HtmlResponse
from crawls.settings import STROY_UDACHA_CONST

class StroyudachaSpiderBase(Spider):

    name = 'stroydacha'
    allowed_domains = ['stroyudacha.ru']
    start_urls = ['https://stroyudacha.ru/']

    # Async db_session
    # from db.connect import get_async_session
    # self.db_session = get_async_session()
    # self.db_session = await self.db_session.__anext__()

    # Sync db_session
    db_session = next(get_session())

    def __str__(self) -> str:
        return self.__class__.__name__

    def __get_categories_urls(self, response: HtmlResponse) -> list[str]:
        """
        Метод получения урлов, всех категорий.
        url: https://stroyudacha.ru/
        """
        return response.xpath(STROY_UDACHA_CONST['xpath_category']).getall()

    def __get_subcategories_urls(self, response: HtmlResponse) -> list[str]:
        """
        Метод получения урлов, всех подкатегорий.
        url: https://stroyudacha.ru/groups/<category_name>/
        """
        return response.xpath(
            STROY_UDACHA_CONST['xpath_sub_category']).getall()

    def __get_sub_sub_categories_urls(
            self, response: HtmlResponse) -> list[str]:
        """
        Метод получения урлов, всех под-подкатегорий.
        url: https://stroyudacha.ru/groups/<sub_category_name>/
        """
        return response.xpath(
            STROY_UDACHA_CONST['xpath_sub_sub_category']).getall()

    def __get_pagination_amount(self, response: HtmlResponse) -> int:
        """
        Метод получения количества страниц в текущей категории.
        url: https://petrovich.ru/groups/<name_of_category>/
        """
        return get_pagination(response, STROY_UDACHA_CONST)

    def start_requests(self) -> None:
        """Начало запросов."""
        for url in self.start_urls:
            yield Request(
                url=url,
                callback=self.parse,
                headers={'User-Agent': get_random_user_agent()})

    def parse(self, response: HtmlResponse) -> None:
        """Парсинг всех категорий на стартовой странице."""
        categories_urls = self.__get_categories_urls(response)

        for url in categories_urls:
            yield Request(
                url=url,
                callback=self.parse_category
            )

    def parse_category(self, response: HtmlResponse) -> None:
        """Парсинг подкатегориий на странице категорий."""
        sub_categories_urls = self.__get_subcategories_urls(response)

        for url in sub_categories_urls:
            yield Request(
                url=url,
                callback=self.parse_sub_category
            )

    def parse_sub_category(self, response: HtmlResponse) -> None:
        """
        Парсинг под-подкатегорий на странцие под категорий (если они есть).
        """
        sub_sub_categories_urls = self.__get_sub_sub_categories_urls(response)

        if not sub_sub_categories_urls:
            yield Request(
                url=response.url,
                callback=self.parse_paginated_pages
            )

        for url in sub_sub_categories_urls:
            yield Request(
                url=url,
                callback=self.parse_paginated_pages
            )

    def parse_paginated_pages(self, response: HtmlResponse) -> None:
        """
        Парсинг пагинированных товаров
        - Товары добавляются на страницу, поэтому парсим все объекты
          с последней страницы пагинации.
        """
        pagination = self.__get_pagination_amount(response)

        yield Request(
            url=f'{response.url}/?page={pagination}',
            callback=self.parse_items
        )

    def parse_items(self, response: HtmlResponse) -> None:
        """
        Парсинг товаров со страницы
        Atributes:
            - product_names: list[names[str]]
            - product_prices: list[prices[float]]
            - product_urls: list[urls[str]]
            - product_category: str
            - product_measurements: list[str]
        """
        product_names = response.xpath(
            STROY_UDACHA_CONST['xpath_name']).getall()

        product_category = response.xpath(
            STROY_UDACHA_CONST['xpath_category_name']
        ).get()

        product_prices = response.xpath(
            STROY_UDACHA_CONST['xpath_price']).getall()
        product_prices = map(get_price, product_prices)

        product_measurements = response.xpath(
            STROY_UDACHA_CONST['xpath_measurement']
        ).getall()

        product_urls = response.xpath(STROY_UDACHA_CONST['xpath_url']).getall()

        product_gcodes = response.xpath(
            STROY_UDACHA_CONST['xpath_gcode']).getall()

        products = get_list_of_products(
            product_names, product_prices, product_urls, product_category,
            None, product_measurements, product_gcodes
        )

        create_db_objects(stroyudacha_products, products, self.db_session)


from http import HTTPStatus

from flask import abort, flash, redirect, render_template

from . import app
from .error_handlers import ModelValidationError
from .forms import URLMapForm
from .models import URLMap

@app.route('/', methods=['GET', 'POST'])
def index_view():
    form = URLMapForm()
    if not form.validate_on_submit():
        return render_template('index.html', form=form), 200
    input_long = form.original_link.data
    input_short = form.custom_id.data
    try:
        url_map = URLMap.create_on_validation(input_long, input_short)
        return (
            render_template(
                'index.html',
                form=form,
                shortcut=url_map.fully_qualified_short_link),
            HTTPStatus.OK)
    except (ModelValidationError, ValueError) as error:
        flash(error.message)
        return render_template('index.html', form=form)

@app.route('/<string:shortcut>')
def redirect_to_full_link(shortcut):
    link_pair = URLMap.get(shortcut)
    if link_pair:
        return redirect(link_pair.original)
    abort(404)


import socket
import time

def scan_ports(start_port, end_port):
    open_ports = []

    target_ip = socket.gethostbyname(socket.gethostname())
    start_time = time.time()

    for port in range(start_port, end_port + 1):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(1)

        result = sock.connect_ex((target_ip, port))

        if result == 0:
            open_ports.append(port)

        sock.close()

    end_time = time.time()
    scan_time = end_time - start_time

    return open_ports, scan_time

def get_service_name(port, protocol="tcp"):
    try:
        service_name = socket.getservbyport(port, protocol)
        return service_name
    except socket.error:
        return "Unknown"

def main():
    start_port = 1  # Начальный порт
    end_port = 1024  # Конечный порт

    print(f"Начало сканирования портов на {socket.gethostname()} ({socket.gethostbyname(socket.gethostname())})...")
    open_ports, scan_time = scan_ports(start_port, end_port)

    if open_ports:
        print("Открытые порты:")
        for port in open_ports:
            service_name = get_service_name(port)
            print(f"Порт {port} ({service_name}): открыт")
    else:
        print("Открытых портов не обнаружено.")

    print(f"Время сканирования: {scan_time:.2f} секунд")

if name == "main":
    main()


from v4_client_py import chain
from v4_client_py import clients
from os import path
from random import randrange
from sys import argv
import time

########################## YOU FILL THIS OUT #################
DYDX_TEST_MNEMONIC = '<FILL_THIS_OUT>'
##############################################################

if len(argv) > 1 and path.isfile(argv[1]):
        exec(open(argv[1]).read())

wallet = chain.aerial.wallet.LocalWallet.from_mnemonic(DYDX_TEST_MNEMONIC, clients.constants.BECH32_PREFIX)
network = clients.constants.Network.testnet()
client = clients.CompositeClient(
        network,
)
subaccount = clients.Subaccount(wallet, 0)

clientid = randrange(0, 2**31 - 1) #random number between 0 and max(int32) inclusive
height = client.get_current_block()
good_til_block = height + 4
print('height          :', height)
print('good_til_block  :', good_til_block)
place_short_term_order_result = client.place_short_term_order(
        subaccount,
        market = 'BTC-USD',
        side = clients.helpers.chain_helpers.OrderSide.BUY,
        price = 1000,
        size = 0.001,
        client_id = clientid,
        good_til_block=good_til_block,
        time_in_force = clients.helpers.chain_helpers.OrderExecution.DEFAULT,
        reduce_only = False,
)
print('tx_hash         :', place_short_term_order_result.tx_hash)
print('response        :', place_short_term_order_result.response)
print('contract_code_id:', place_short_term_order_result.contract_code_id)
print('contract_address:', place_short_term_order_result.contract_address)
print('good_til_block  :', good_til_block)
time.sleep(1)
cancel_order_result = client.validator_client.post.cancel_order(
        subaccount,
        client_id = clientid,
        clob_pair_id = 0,
        order_flags = 0,
        good_til_block = good_til_block,
        good_til_block_time = 0,
)
print('tx_hash         :', cancel_order_result.tx_hash)
print('response        :', cancel_order_result.response)
print('contract_code_id:', cancel_order_result.contract_code_id)
print('contract_address:', cancel_order_result.contract_address)


from abc import ABCMeta, abstractmethod

from six import add_metaclass, PY2
import xmltodict

if PY2:
    from codecs import open

__author__ = 'y.gavenchuk'

@add_metaclass(ABCMeta)
class AbstractSettings(object):
    _parser = None

    def __init__(self, config):
        self._cfg_file = config
        self._data = self.parse()

    @abstractmethod
    def parse(self):
        pass

    @property
    def data(self):
        return self._data

class SettingsXML(AbstractSettings):
    _wired_keys = {'@', '#'}
    _parser = xmltodict

    def _is_key_wired(self, key):
        return key[0] in self._wired_keys

    @staticmethod
    def _normalize_key(key):
        return key[1:]

    def _fix_wired_keys(self, data):
        if isinstance(data, dict):
            for k in data.keys():
                if self._is_key_wired(str(k)):
                    item = data.pop(k)
                    data[self._normalize_key(k)] = item
                else:
                    item = data[k]

                self._fix_wired_keys(item)

        elif isinstance(data, (list, tuple, set)):
            for item in data[:]:
                self._fix_wired_keys(item)

    def parse(self):
        with open(self._cfg_file, 'r', encoding='utf-8') as fp:
            data = self._parser.parse(fp.read())['WebDeploy']
            self._fix_wired_keys(data)

            return data


from odoo import _, api, fields, models, tools
from odoo.exceptions import UserError, ValidationError
import time
from werkzeug import urls

class PaymentTransaction(models.Model):

    _inherit = 'payment.transaction'

    def _get_specific_rendering_values(self, processing_values):

        res = super()._get_specific_rendering_values(processing_values)
        if self.provider != 'cashpay':
            return res

        return {
            'api_url': self._get_api_bill_data().get('bill_url'),
            'order_reference': self._get_api_bill_data().get('order_reference')
        }

    def _get_api_bill_data(self):
        base_url = self.acquirer_id.get_base_url()
        salt = str(time.time())
        data = {
            'amount': self.amount,
            'merchant_reference': self.reference + salt,
            'description': 'Test environnement Sandbox',
            'callback_url': base_url + 'callback',
            'redirect_url': base_url + 'callback',
            'client': {
                'lastname': self.partner_id.name,
                'firstname': self.partner_id.name,
                'phone': self.partner_id.phone if self.partner_id.phone else '+00000000000'
            },
            'direct_pay': 0,
            'gateway': 1,

        }
        response = self.acquirer_id._cashapy_make_request(salt, data)
        return {
            'bill_url': response.get('bill_url'),
            'order_reference': response.get('order_reference'),
        }
    @api.model
    def _get_tx_from_feedback_data(self, provider, data):
        """ Override of payment to find the transaction based on Ogone data.

        :param str provider: The provider of the acquirer that handled the transaction
        :param dict data: The feedback data sent by the provider
        :return: The transaction if found
        :rtype: recordset of `payment.transaction`
        :raise: ValidationError if the data match no transaction
        """
        raise ValidationError(_("gggggggggg " + str(data) + " gggggggggggggggggg"))
        tx = super()._get_tx_from_feedback_data(provider, data)
        if provider != 'cashpay':
            return tx

        reference = 'FAC/2022/00001'
        tx = self.search([('reference', '=', reference), ('provider', '=', 'cashpay')])
        if not tx:
            raise ValidationError(
                "cashpay: " + _("No transaction found matching reference %s.", reference)
            )
        return tx


'''
Use S3Hook to list keys in the S3 bucket.
Check if the keyname datetime is current to make sure new file arrives on time.
'''

from airflow import DAG
from airflow.operators.bash import BashOperator
from airflow.sensors.s3_key_sensor import S3KeySensor
from airflow.hooks.S3_hook import S3Hook
from datetime import date, datetime, timedelta
from collections import defaultdict
from airflow.operators.email_operator import EmailOperator
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator
from pprint import pprint

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime(2021, 7, 19),
    'email': ['xxx@gmail.com'],
    'email_on_failure': True,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=0.5)
}

dag = DAG('s3_dag_test', default_args=default_args, schedule_interval= '@daily', catchup=False)

# Variables
dt = (datetime.now() + timedelta(days=-1)).strftime("%Y-%m-%d")
#bucket_name = 'ssdp-s3-csai-dsde-dev'
bucket_name = 'gainsight-csg-prod-us-west-2-bucket'
most_recent_files = defaultdict(set)
most_recent_files_lst = []
#key_name = 'GainsightActivityAttendee-'+ dt +'-*'

# Methods
def time_to_epoch(item):
    temp_lst = []
    for sub_item in item:
        utc_time = datetime.strptime(sub_item, "%Y-%m-%d-%H:%M:%S")
        epoch_time = (utc_time - datetime(1970, 1, 1)).total_seconds()
        temp_lst.append((epoch_time, sub_item))
    temp_lst.sort(key = lambda x: x[0], reverse = True)
    pprint(temp_lst)
    return temp_lst[0][1]

def printFilesName():
    s3 = S3Hook(aws_conn_id='aws_prod_conn')
    s3.get_conn()
    lst_files = s3.list_keys(bucket_name = bucket_name, prefix = 'gainsight_1.0/Gainsight')
    for sub_lst in lst_files:
        idx = sub_lst.index('-')
        key, val =sub_lst[:idx], sub_lst[idx+1:len(sub_lst)-4]
        most_recent_files[key].add(val)
    print("\n".join("{!r}: {!r}".format(k, v) for k, v in most_recent_files.items()))
    #sorted_lst = {k: list(v) for k, v in sorted(most_recent_files.items(), key=lambda item: time_to_epoch(item[1]), reverse=True)}
    print('#####################')
    for k,v in most_recent_files.items():
        combine_filename = k + '-' + time_to_epoch(v) + '.tsv'
        most_recent_files_lst.append(combine_filename)
    return most_recent_files_lst

def getFiles(**kwargs):
    s3 = S3Hook(aws_conn_id='aws_prod_conn')
    s3.get_conn()
    lst_files = kwargs['ti'].xcom_pull(task_ids='print_file_name')
    res_map = {}
    for sub_lst in lst_files:
        idx = sub_lst.index('-')
        key, val = sub_lst[14:idx], sub_lst[idx+1:]
        if val[:10] == dt:
            res_map[key] = 'Normal'
        else:
            res_map[key] = 'Miss'
    return "\n".join("{!r}: {!r}".format(k, v) for k, v in res_map.items())

t1 = BashOperator(
    task_id='Run_Bash',
    bash_command='echo "We are going to detect S3 objects....... "',
    dag=dag)

t2 = PythonOperator(
    task_id='print_file_name',
    python_callable=printFilesName,
    dag=dag)

t3 = PythonOperator(
    task_id='list_file_status',
    python_callable=getFiles,
    dag=dag)

send_email = EmailOperator(
        task_id = 'Send_Email',
        to = 'xxx@gmail.com',
        subject = 'Airflow: Gainsight Files!!',
        html_content = """
{{task_instance.xcom_pull(task_ids = 'print_file_name')}}

                        {{task_instance.xcom_pull(task_ids = 'list_file_status')}}
""",
        dag = dag
        )

'''
sensor = S3KeySensor(
        task_id='check_s3_for_file_in_s3',
        #bucket_key='GainsightActivityAttendee-*',
        bucket_key=key_name,
        wildcard_match=True,
        bucket_name='ssdp-s3-csai-dsde-dev',
        aws_conn_id='my_conn_S3',
        timeout=0.5*20,
        poke_interval=5,
        dag=dag)
'''

t1 >> t2 >> t3 >> send_email


#coding: utf-8

n = int(input("Digite o valor de N: "))
i = 0
maior = 0
menor = 0

while (i < n):
  numero = float(input("Digite um número real: "))

  if (i == 0 or numero > maior):
    maior = numero

  if (i == 0 or numero < menor):
    menor = numero

  i += 1

print("O menor número é {}").format(menor)
print("O maior número é {}").format(maior)