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

# Standard imports
from typing import Optional, Tuple
from functools import reduce
import operator

# External imports
import torch
import torch.nn as nn

def conv_bn_leakyrelu(in_channels, out_channels):
    """
    Conv(3x3, same) - BN - LeakyRelu(0.2)
    """
    ks = 3
    return [
        nn.Conv2d(
            in_channels,
            out_channels,
            kernel_size=ks,
            stride=1,
            padding=int((ks - 1) / 2),
            bias=False,
        ),
        nn.BatchNorm2d(out_channels),
        nn.LeakyReLU(negative_slope=0.2),
    ]

def conv_downsampling(channels):
    """
    Conv(3x3, s2) - LeakyRelu(0.2)
    """
    ks = 3
    return [
        nn.Conv2d(
            channels,
            channels,
            kernel_size=ks,
            stride=2,
            padding=int((ks - 1) / 2),
            bias=True,
        ),
        nn.LeakyReLU(negative_slope=0.2),
    ]

class Discriminator(nn.Module):
    """
    The discriminator network tells if the input image is real or not
    The output logit is supposed to be high(-ly positive) for real images
    and low (highly negative) for fake images
    """

    def __init__(
        self, img_shape: Tuple[int, int, int], dropout: float, base_c: int
    ) -> None:
        """
        Args:
            img_shape : (C, H, W) image shapes
            dropout (float) the probability of zeroing before the FC layer
            base_c (int): The base number of channels for the discriminator
        """
        super(Discriminator, self).__init__()
        self.img_shape = img_shape

        in_C = img_shape[0]
        ######################
        # START CODING HERE ##
        ######################
        # Definition of the convolutional part of the classifier
        # Hint : conv_bn_leakyrelu() and conv_downsampling() can
        #        be usefull
        # @TEMPL@self.cnn = None
        # @SOL
        # Note: the output receptive field size is 36 x 36
        #       the output representation size is 3 x 3
        self.cnn = nn.Sequential(
            *conv_bn_leakyrelu(in_C, base_c),
            *conv_bn_leakyrelu(base_c, base_c),
            *conv_downsampling(base_c),
            nn.Dropout2d(dropout),
            *conv_bn_leakyrelu(base_c, base_c * 2),
            *conv_bn_leakyrelu(base_c * 2, base_c * 2),
            *conv_downsampling(base_c * 2),
            nn.Dropout2d(dropout),
            *conv_bn_leakyrelu(base_c * 2, base_c * 3),
            *conv_bn_leakyrelu(base_c * 3, base_c * 3),
            *conv_downsampling(base_c * 3),
            nn.Dropout2d(dropout),
        )
        # SOL@
        ####################
        # END CODING HERE ##
        ####################

        # Compute the size of the representation by forward propagating
        # a fake tensor; This can be cpu tensor as the model is not yet
        # built and therefore not yet transfered to the GPU
        fake_input = torch.zeros((1, *img_shape))
        out_cnn = self.cnn(fake_input)
        print(
            f"The output shape of the convolutional part of the "
            f"discriminator is {out_cnn.shape}"
        )
        num_features = reduce(operator.mul, out_cnn.shape[1:])

        ######################
        # START CODING HERE ##
        ######################
        # The fully connected part of the classifier
        # @TEMPL@self.classif = None
        # @SOL
        self.classif = nn.Sequential(nn.Linear(num_features, 1))
        # SOL@
        ####################
        # END CODING HERE ##
        ####################

        # Run the initialization script
        self.apply(self.init_weights)

    def init_weights(self, m):
        """
        Initialize the weights of the convolutional layers
        """

        with torch.no_grad():
            if isinstance(m, nn.Conv2d):
                torch.nn.init.normal_(m.weight, 0.0, 0.02)
                if m.bias is not None:
                    m.bias.fill_(0.0)

    def forward(self, X: torch.Tensor) -> torch.Tensor:
        """
        Forward pass of the discriminator
        Args:
            X(torch.Tensor (B, C, H, W)) : The images to classify

        Returns:
            Logits (torch.Tensor (B, )) : The logits
        """

        ######################
        # START CODING HERE ##
        ######################
        # Step 1 - Forward pass through the CNN part
        # @TEMPL@out_cnn = None
        out_cnn = self.cnn(X)  # @SOL@

        # Step 2 - "Reshape" the 4D tensor to a 2D tensor
        #  Hint : Tensor.view can be of help
        # @TEMPL@input_classif = None
        input_classif = out_cnn.view((out_cnn.shape[0], -1))  # @SOL@

        # Step 3 - Forward pass through the fully connected layers
        # @TEMPL@out_classif = None
        out_classif = self.classif(input_classif)  # @SOL@
        ####################
        # END CODING HERE ##
        ####################
        return out_classif.squeeze()

def up_conv_bn_relu(in_channels, out_channels):
    """
    Upsampling with Upsample - Conv
    UpSample(x2) - Conv(3x3) - BN - Relu - Conv(3x3) - BN - Relu
    """
    ks = 3
    return [
        nn.Upsample(scale_factor=2),
        nn.Conv2d(
            in_channels,
            out_channels,
            kernel_size=ks,
            stride=1,
            padding=int((ks - 1) / 2),
            bias=False,
        ),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(),
        nn.Conv2d(
            out_channels,
            out_channels,
            kernel_size=ks,
            stride=1,
            padding=int((ks - 1) / 2),
            bias=False,
        ),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(),
    ]

def tconv_bn_relu(in_channels, out_channels, ksize, stride, pad, opad):
    """
    Upsampling with transposed convolutions
    TConv2D - BN - LeakyRelu(0.2)
    """
    return [
        nn.ConvTranspose2d(
            in_channels,
            out_channels,
            kernel_size=ksize,
            stride=stride,
            padding=pad,
            output_padding=opad,
        ),
        nn.BatchNorm2d(out_channels),
        nn.LeakyReLU(negative_slope=0.2),
    ]

class Generator(nn.Module):
    """
    The generator network generates image from random inputs
    """

    def __init__(
        self, img_shape: Tuple[int, int, int], latent_size: int, base_c: int
    ) -> None:
        """
        Args:
            img_shape : (C, H, W) image shapes
            latent_size (int) : The dimension of the latent space
            base_c (int) : The base number of channels
        """
        super(Generator, self).__init__()
        self.img_shape = img_shape
        self.latent_size = latent_size
        self.base_c = base_c

        H, W = img_shape[1:]
        ######################
        # START CODING HERE ##
        ######################
        # Step 1 - Build the feedforward upscaling network
        # @TEMPL@self.upscale = nn.Sequential()
        # @SOL
        print(
            f"Linear with {self.latent_size} -> {H // 4 * W // 4 * self.base_c * 4}, H = {H}, W= {W}, base = {self.base_c}"
        )
        self.upscale = nn.Sequential(
            nn.Linear(self.latent_size, H // 4 * W // 4 * self.base_c * 4, bias=False),
            nn.BatchNorm1d(H // 4 * W // 4 * self.base_c * 4),
            nn.ReLU(),
        )
        # SOL@

        # Step 2 - Build the convolutional upscaling network
        # Hint : up_conv_bn_relu() might be useful
        # @TEMPL@self.model = nn.Sequential()
        # @SOL
        self.model = nn.Sequential(
            *up_conv_bn_relu(self.base_c * 4, self.base_c * 2),
            *up_conv_bn_relu(self.base_c * 2, self.base_c),
            nn.Conv2d(
                self.base_c,
                self.img_shape[0],
                kernel_size=1,
                stride=1,
                padding=0,
                bias=True,
            ),
            nn.Tanh(),
        )
        # SOL@
        ####################
        # END CODING HERE ##
        ####################

        # @SOL
        # Note : size, stride, pad, opad
        # self.model = nn.Sequential(
        #     *tconv_bn_relu2(base_c*4, base_c*2, 5, 1, 2, 0),
        #     # nn.Dropout2d(0.3),
        #     *tconv_bn_relu2(base_c*2, base_c, 5, 2, 2, 1),
        #     # nn.Dropout2d(0.3),
        #     nn.ConvTranspose2d(base_c, 1, 5, 2, 2, 1),
        #     nn.Tanh()  # as suggested by [Radford, 2016]
        # )
        # SOL@

        # Initialize the convolutional layers
        self.apply(self.init_weights)

    def init_weights(self, m):
        with torch.no_grad():
            if isinstance(m, nn.Conv2d):
                torch.nn.init.normal_(m.weight, 0.0, 0.02)
                if m.bias is not None:
                    m.bias.fill_(0.0)

    def forward(
        self, X: Optional[torch.Tensor] = None, batch_size: Optional[float] = None
    ) -> torch.Tensor:
        """
        Forward pass of the generator. You can either provide a noise
        input vector or specify the batch_size to let it generate the input

        Args:
            X (torch.Tensor, optional): The input noise batch
            batch_size (int, optional): The number of samples to generate
        """
        # X is expected to be a 2D tensor (B, L)
        if X is None:
            assert batch_size is not None
            device = next(self.parameters()).device
            X = torch.randn(batch_size, self.latent_size).to(device)
        else:
            if len(X.shape) != 2:
                raise RuntimeError(
                    "Expected a 2D tensor as input to the "
                    f" generator got a {len(X.shape)}D tensor."
                )

        ######################
        # START CODING HERE ##
        ######################
        # Step 1 - Forward pass through the first linear layers
        #          to generate the seed image
        # @TEMPL@upscaled = None
        upscaled = self.upscale(X)  # @SOL@

        # Step 2 - "Reshape" the upscaled image as a 4D tensor
        #  Hint : use the view method
        # @TEMPL@reshaped = None
        reshaped = upscaled.view(
            -1, self.base_c * 4, self.img_shape[1] // 4, self.img_shape[2] // 4
        )  # @SOL@

        # Step 3 : Forward pass through the last convolutional part
        #          to generate the image
        # @TEMPL@out = None
        out = self.model(reshaped)  # @SOL@
        ####################
        # END CODING HERE ##
        ####################

        return out

class GAN(nn.Module):
    def __init__(
        self,
        img_shape: Tuple[int, int, int],
        dropout: float,
        discriminator_base_c: int,
        latent_size: int,
        generator_base_c: int,
    ) -> None:
        """
        Args:
            img_shape : (C, H, W) image shapes
            dropout (float): The probability of zeroing before the FC layers
            discriminator_base_c (int) : The base number of channels for
                                         the discriminator
            latent_size (int) : The size of the latent space for the generator
            generator_base_c (int) : The base number of channels for the
                                     generator
        """
        super(GAN, self).__init__()
        self.img_shape = img_shape
        self.discriminator = Discriminator(img_shape, dropout, discriminator_base_c)
        self.generator = Generator(img_shape, latent_size, generator_base_c)

    def forward(self, X: Optional[torch.Tensor], batch_size: Optional[float]):
        """
        Given true images, returns the generated tensors
        and the logits of the discriminator for both the generated tensors
        and the true tensors

        Args:
            X (torch.Tensor) : a real image or None if we just
                               want the logits for the generated images
            batch_size (int) : the batch to consider when generating
                               fake images
        """

        if X is None and batch_size is None:
            raise RuntimeError("Not both X and batch_size can be None")
        if X is not None and batch_size is not None:
            raise RuntimeError("Not both X and batch_size can be not None")

        if X is not None:
            ######################
            # START CODING HERE ##
            ######################
            # An input tensor of real images is provided
            # we compute its logits
            # 1 line
            # @TEMPL@real_logits = None
            real_logits = self.discriminator(X)  # @SOL@
            ####################
            # END CODING HERE ##
            ####################
            return real_logits, X
        else:
            ######################
            # START CODING HERE ##
            ######################
            # No input tensor is provided. We generate batch_size fake images
            # and evaluate its logits
            # 2 lines
            # @TEMPL@fake_images = None
            # @TEMPL@fake_logits = None
            # @SOL
            fake_images = self.generator(X=None, batch_size=batch_size)
            fake_logits = self.discriminator(fake_images)
            # SOL@
            ####################
            # END CODING HERE ##
            ####################

            return fake_logits, fake_images

# @SOL
def test_tconv():
    layers = nn.Sequential(nn.Conv2d(20, 10, kernel_size=3, stride=1, padding=2))
    print(layers)
    inputs = torch.zeros((1, 20, 2, 2))
    outputs = layers(inputs)
    print(outputs.shape)

    imagify = nn.Linear(100, 7 * 7 * 10)
    conv1 = nn.ConvTranspose2d(10, 10, kernel_size=5, stride=1, padding=2)
    conv2 = nn.ConvTranspose2d(
        10, 10, kernel_size=5, stride=2, padding=2, output_padding=1
    )
    conv3 = nn.ConvTranspose2d(
        10, 1, kernel_size=5, stride=2, padding=2, output_padding=1
    )

    X = torch.randn(64, 100)
    X = imagify(X).view(-1, 10, 7, 7)
    print("--")
    print(X.shape)
    X = conv1(X)
    print(X.shape)
    X = conv2(X)
    print(X.shape)
    X = conv3(X)
    print(X.shape)

# SOL@

def test_discriminator():
    critic = Discriminator((1, 28, 28), 0.3, 32)
    X = torch.randn(64, 1, 28, 28)
    out = critic(X)
    assert out.shape == torch.Size([64])

def test_generator():
    generator = Generator((1, 28, 28), 100, 64)
    X = torch.randn(64, 100)
    out = generator(X, None)
    assert out.shape == torch.Size([64, 1, 28, 28])
    out = generator(None, 64)
    assert out.shape == torch.Size([64, 1, 28, 28])

if __name__ == "__main__":
    test_tconv()  # @SOL@
    test_discriminator()
    test_generator()


import sys
input = sys.stdin.readline

n = int(input())
graph = []
vistited = [[0] * n for _ in range(n)]
for _ in range(n):
    k = list(map(int,input().strip()))
    graph.append(k)
dx = [-1,1,0,0]
dy = [0,0,-1,1]

def dfs():
    global apart
    # 해야할게 뭐가 있을까? 아마 그래프를 전체 돌면서 0이 아닌 1을 찾아야겠지?
    # dfs를 사용해야하니까 stack을 사용해야겠지??
    stack = []
    value = 0
    apart = []
    for i in range(n):
        for j in range(n):
            if graph[i][j] == 1 and vistited[i][j] == 0:
                stack.append((i,j)) # stack에 넣은 다음 다시 진행?
                vistited[i][j] = 1 # 방문처리를 한다.
                value += 1  # 다른 1을 만나면 , value증가
                house = 1

                while stack:
                    x,y = stack.pop()
                    for p in  range(4):
                        nx = x+dx[p]
                        ny = y+dy[p]

                        if 0 <= nx < n and 0 <= ny < n and vistited[nx][ny] == 0 and graph[nx][ny] == 1:
                            stack.append((nx,ny))
                            vistited[nx][ny] = 1
                            house += 1

                apart.append(house)  # while을 벗어나면 1추가
    return value

print(dfs())
apart.sort()
for i in apart:
    print(i)

백준/Silver/2667. 단지번호붙이기/단지번호붙이기.py


# -*- coding: utf-8 -*-

import sys, os
import time

sys.path.append(os.getcwd().split("test")[0])

import FitFile_Handler
import DB_Handler
import Graphic_store

import unittest

import xml.etree.ElementTree as ET
import numpy as np

from Logger import Logger

class Test_process(unittest.TestCase):

    def setUp(self):

        logger = Logger('test_process')
        logger.add_StreamHandler()
        logger.add_FileHandler('Initial_test.log')
        self.logger = logger.logger

        self.parameters =  {}
        tree = ET.parse("parameters.xml")
        root = tree.getroot()
        for child in root:
            for sub_child in child:
                self.parameters[sub_child.tag] = sub_child.test.encode('utf8')

        self.REPO_DIR = os.getcwd().split("test")[0]
        self.parameters["REPO_DIR"] = self.REPO_DIR
        self.fit_files = {}
        self.graphic = Graphic_store.Graphic_store(self.parameters, self.logger)

    def test_basic_process(self):
        files = os.listdir(os.path.join(self.REPO_DIR, 'source_data'))
        fit_files = [os.path.join(self.REPO_DIR, 'source_data', file) for file in files if file[-4:].lower()=='.fit']

        for file in fit_files:
            self.fit_files[file] = FitFile_Handler.FitFile_handler(self.parameters, self.logger, file)
            self.fit_files[file].load_file()

        for file, handler in self.fit_files.items():
            handler.parse()
        # Create db
        self.db_user = DB_Handler.DB_Handler('users', self.parameters, self.logger)
        self.db_event = DB_Handler.DB_Handler("event", self.parameters, self.logger)

        # Insert data into db
        for file, handler in self.fit_files.items():
            for v in handler.data:
                v['user_id'] = 1
                self.db_event.insert_data(v)

        # Reload those data from db
        sql_query = "SELECT position_lat, position_long, power, altitude FROM event WHERE user_id=1"
        rows = self.db_event.load_data(sql_query)

        longitude, latitude, power, altitude = np.array([]), np.array([]), np.array([]), np.array([])
        for row in rows:
            longitude = np.append(longitude, row[0])
            latitude = np.append(latitude, row[1])
            power = np.append(power, row[2])
            altitude = np.append(altitude, row[3])

        self.graphic.draw_2D_enriched('test_power.jpeg', latitude, longitude, power)
        self.graphic.draw_3d('test_power.jpeg', latitude, longitude, altitude)

        self.db_user.off()
        self.db_event.off()

if __name__=="__main__":
    unittest.main()


from imports import *
from settings import *

logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

DEVICE = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'

def initialize_model():
    torch.cuda.empty_cache()
    warnings.filterwarnings('ignore', category=FutureWarning)

    bnb_config = transformers.BitsAndBytesConfig(
        load_in_4bit=True,
        bnb_4bit_quant_type='nf4',
        bnb_4bit_use_double_quant=True,
        bnb_4bit_compute_dtype=bfloat16)

    model_config = transformers.AutoConfig.from_pretrained(
        MODEL_ID,
        use_auth_token=HF_AUTH)

    model = transformers.AutoModelForCausalLM.from_pretrained(
        MODEL_ID,
        trust_remote_code=True,
        config=model_config,
        quantization_config=bnb_config,
        device_map='auto',
        use_auth_token=HF_AUTH)

    model.eval()
    return model

def initialize_tokenizer():
    return transformers.AutoTokenizer.from_pretrained(MODEL_ID, use_auth_token=HF_AUTH)

def initialize_generate_text(model, tokenizer):
    return transformers.pipeline(
        model=model,
        tokenizer=tokenizer,
        return_full_text=True,
        task='text-generation',
        do_sample=False,
        max_new_tokens=1024,
        repetition_penalty=1.1)

def setup_components():
    embeddings = HuggingFaceInstructEmbeddings(model_name='thenlper/gte-large')

    stores = {collection: PGVector(
        collection_name=collection,
        connection_string=CONNECTION_STRING,
        embedding_function=embeddings)
              for collection in COLLECTION_NAMES}

    return stores

def setup_prompt():
    template = rf"""
    CONTEXT:
    -----------------------------------------------------------------------------
    {'{context}'}
    -----------------------------------------------------------------------------
    PREVIOUS MESSAGES:
    -----------------------------------------------------------------------------
    {'{chat_history}'}
    -----------------------------------------------------------------------------
    QUESTION:
    -----------------------------------------------------------------------------
    {'{question}'}
    -----------------------------------------------------------------------------

    Your responses should be complete sentences, in clear and concise tone.

    Answer:"""

    return PromptTemplate(
        input_variables=["chat_history", "question", "context"],
        template=template)

def setup_memory():
    return ConversationBufferWindowMemory(
            memory_key="chat_history",
            input_key="question",
            k=3,
            return_messages=True)


# Problem Name: Job Sequencing Problem
# Approach used: Greedy Method
# Language used: Python

# Problem Statement: Given an array of jobs where every job has a deadline
#                    and associated profit if the job is finished before the
#                    deadline. It is also given that every job takes a single
#                    unit of time, so the minimum possible deadline for any
#                    job is 1. Maximize the total profit if only one job can
#                    be scheduled at a time.

# -----------------------------------------------------------------------------

# Constraints:
# arr[] -> Set of datapoints including JobID, Deadline and Profit into a single array.
# t -> No. of jobs to be scheduled.

# -----------------------------------------------------------------------------

def printJobScheduling(arr, t):
# This is the main funtion for determining the final result
# function to schedule the jobs take 2 arguments array and no of jobs to schedule

    # length of array
    n = len(arr)

    # Sort all jobs according to
    # decreasing order of profit
    for i in range(n):
        for j in range(n - 1 - i):
            if arr[j][2] < arr[j + 1][2]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]

    # To keep track of free time slots
    result = [False] * t

    # To store result (Sequence of jobs)
    job = ['-1'] * t

    # Iterate through all given jobs
    for i in range(len(arr)):

        # Find a free slot for this job
        # (Note that we start from the
        # last possible slot)
        for j in range(min(t - 1, arr[i][1] - 1), -1, -1):

            # Free slot found
            if result[j] is False:
                result[j] = True
                job[j] = arr[i][0]
                break

    # print the sequence
    print(job)

# Driver's Code
if __name__ == '__main__':
    arr = [['a', 2, 100],  # Job Array
           ['b', 1, 19],
           ['c', 2, 27],
           ['d', 1, 25],
           ['e', 3, 15]]

    print ("-- Job Sequencing Problem using Greedy Approach --")
    print ()
    print ("Job ID    Deadline     Profit")
    for i in range(0, len(arr)):
      for j in range (0, 3):
        print (arr[i][j], end = "           ")
      print()

    print ("\nOutput --")
    print("Following is maximum profit sequence of jobs")

    # Function Call
    printJobScheduling(arr, 3)

# -------------------------------------------------------------------------

# Output
# -- Job Sequencing Problem using Greedy Approach --

# Job ID    Deadline     Profit
# a           2           100
# b           1           19
# c           2           27
# d           1           25
# e           3           15

# Output --
# Following is maximum profit sequence of jobs
# c a e

# -------------------------------------------------------------------------

# Code contributed by, Abhishek Sharma, 2022

# -------------------------------------------------------------------------


from selenium.webdriver.common.by import By
from selenium import webdriver
import time

try:
    line = 'https://suninjuly.github.io/execute_script.html'
    browser = webdriver.Chrome()
    browser.get(line)
    button = browser.find_element_by_tag_name("button")
    button.sc
    time.sleep(1)

    browser.execute_script('return arguments[0].scrollIntoView(true);', button)
    time.sleep(1)

    assert True

finally:
    time.sleep(5)
    browser.quit()


#!/usr/bin/python
#Takes a directory of host files and produce an rpz zone
#This is not a script, call it whatever you want but not script, alternative suggested name are:
#  - this, thing
#  - stuff, gudget, gimmik
#  - program, scheduled job
#  - integration, automation, project
#You are also encuraged to rewrite it in any interpreted and not compiled language you wish

import os, sys, re, syslog, \
       getopt, fcntl, time

hosts_dir='./tmp/'
verbose=0
verbose_level=1

hostifle_suffix='\.host'
syslog.openlog('hostfile_to_rpz',logoption=syslog.LOG_PID, facility=syslog.LOG_DAEMON)

def shout(level,message,too=''):
  if level < verbose_level:
    syslog.syslog(message)
    if verbose:
      print (message+' '+too)

def main(myself,argv):
  rpz_zonename='pippo.it'
  inputdir = './tmp/'
  outputfile = '/tmp/block_zone.rpz'
  print_dups = 0
  myself=os.path.basename(myself)
  lock_name='/tmp/'+myself+'.lock'
  #avoid multiple concurrent run
  lock_fh=open(lock_name,'a+')
  try:
    fcntl.flock(lock_fh.fileno(),fcntl.LOCK_EX | fcntl.LOCK_NB)
  except IOError:
    shout(0,myself+' aborting other istance running' )
    sys.exit(2)

  shout(0,myself+' starting')

  try:
    opts, args = getopt.getopt(argv,"hvnd:o:z:",["idir=","ofile=","zonename="])
  except getopt.GetoptError:
    print('test.py -n -d <inputdir> -o <outputfile>')
    sys.exit(2)

  for opt, arg in opts:
    if opt == '-h':
      print('test.py -n -d <inputdir> -o <outputfile>')
      sys.exit()
    elif opt == '-n':
      print_dups = 1
    elif opt == '-v':
      verbose = 1
    elif opt in ("-d", "--idir"):
      inputdir = arg
    elif opt in ("-z", "--zonename"):
      rpz_zonename = arg
    elif opt in ("-o", "--ofile"):
      outputfile = arg
  #print ('Input file is ', inputdir)
  #print ('Output file is ', outputfile)

  try:
    rpz_fh=open(outputfile+'.tmp',"w")
  except EnvironmentError:
   shout(0,'cannot open '+outputfile+'.tmp')
   sys.exit(2)

  head_zonefile(rpz_fh,rpz_zonename,'ns1.'+rpz_zonename,'ns2.'+rpz_zonename)
  write_rpz_content(inputdir,rpz_fh,print_dups)
  rpz_fh.close()
  try:
    os.rename(outputfile+'.tmp',outputfile)
  except EnvironmentError:
   shout(0,'cannot rename '+outputfile+'.tmp to '+outputfile)
   sys.exit(2)

  #release lock
  fcntl.flock(lock_fh.fileno(),fcntl.LOCK_UN);
  lock_fh.close()

def head_zonefile(rpz_fh,rpz_domain,rpz_master,rpz_slave, rpz_hostmaster='null@irds.it', \
  rpz_ttl='2h', rpz_refresh='12h', rpz_retry='15m',rpz_expire='3w',rpz_minttl='2h'):
  rpz_fh.write('$TTL '+rpz_ttl+";\n")
  rpz_fh.write('$ORIGIN '+rpz_domain+".\n")
  rpz_fh.write('@       SOA '+rpz_master.rstrip('.')+'. '+rpz_hostmaster.replace('@','.').rstrip('.')+". (\n")
  #rpz_fh.write('        '+format(int(time.mktime(time.localtime())))+"\n")
  rpz_fh.write('        '+time.strftime("%s")+"\n") #undocumented "%s"
  rpz_fh.write('        '+rpz_refresh+"\n")
  rpz_fh.write('        '+rpz_retry+"\n")
  rpz_fh.write('        '+rpz_expire+"\n")
  rpz_fh.write('        '+rpz_minttl+")\n")
  rpz_fh.write(";fake ns list\n")
  rpz_fh.write('        NS '+rpz_master.rstrip('.')+".\n")
  rpz_fh.write(rpz_master.rstrip('.')+'.  IN NS 127.0.2.1'+"\n")
  if rpz_slave != None:
    rpz_fh.write('        NS '+rpz_slave.rstrip('.')+".\n")
    rpz_fh.write(rpz_slave.rstrip('.')+'. IN NS 127.0.2.2'+".\n")
  rpz_fh.write(";begin RPZ RR definitions\n")

def write_rpz_content(hosts_dir,rpz_fh,print_dups):
  #read file list
  try:
    hostfiles=os.listdir(hosts_dir)
  except EnvironmentError:
    shout(0,'cannot os.listdir '+hosts_dir)
    sys.exit(2)

  domain_cache={}
  for hostfile in hostfiles:
    if re.match('.*'+hostifle_suffix+'$',hostfile):
      shout(0,'parsing '+hostfile)

      try:
        hostfile_content_ary=open(hosts_dir+'/'+hostfile).readlines()
      except EnvironmentError:
        shout(0,'cannot open '+hosts_dir+'/'+hostfile)
        sys.exit(2)

      rcnt=0
      for host_row in hostfile_content_ary:
        rcnt+=1
        if (re.match('^(#.*|)$',host_row)):
          continue
        host_row=host_row.rstrip().rstrip('\r\n')
        #parse and validate host row
        row_field=re.search('^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+([\w\.\s\d-]+)$',host_row)
        if row_field == None:
            shout(0,'malformed row '+hostfile+':'+format(rcnt),host_row)
            continue
        domain=row_field.group(2)
        #check dups
        if domain not in domain_cache:
          domain_cache[domain]=row_field.group(1)
          #output a formatted string "{: <32}" will not truncate rows
          rpz_fh.write('{: <32}'.format(domain)+' IN A '+row_field.group(1)+"\n")
        elif print_dups:
          rpz_fh.write('{: <32}'.format(domain)+' IN A '+row_field.group(1)+"\n")
        else:
          #suppress duplicates
          shout(2,'suppressing dups '+hostfile+':'+format(rcnt)+' '+domain,'')

        #print(row_field.group(1)+' '+row_field.group(0))

main(sys.argv[0],sys.argv[1:])
#main(sys.argv)


from torchvision import transforms as T
import numpy as np
import torch
import json

def mean(arrs):
    return sum(arrs) * 1.0 / len(arrs)

def set_seed(seed):
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)

transform = T.Compose(
    [
        T.Resize([50, 50]),
        # T.Grayscale(),
        T.ToTensor(),
        T.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
    ]
)

def dump_list(path, lst):
    with open(path, "w") as f:
        for x in lst:
            f.write(f"{x}\n")

def dump_json(data, file: str):
    json.dump(data, open(file, "w"), indent=4)

def get_param_cnt(model):
    return sum([p.numel() for p in model.parameters()])


from processing.text_detection import TextDetector
import math
import Levenshtein

class KillfeedProcessor:
    def __init__(self, player_names):
        self.text_detection_agent = TextDetector()
        self.player_names = player_names

    def process_ocr_result(self, result):
        text = result[1]
        splitted = text.split(" ")
        if len(splitted) > 1:
            return splitted[-1]
        else:
            return splitted[0]

    def findClosestMatch(self, inferred_name):
        curr_match = None
        curr_lowest_distance = math.inf
        for name in self.player_names():
            dist = Levenshtein.distance(inferred_name, name, weights=(4, 4, 1))
            if dist < curr_lowest_distance:
                curr_lowest_distance = dist
                curr_match = name

        return curr_match

    def detectKillEvent(self, img):
        ocr_output = self.text_detection_agent.readTextFromImage(img)
        readble_output = self.text_detection_agent.parseOutput(ocr_output)
        if len(readble_output) != 2:
            return None  # some error happened

        player_1 = self.findClosestMatch(
            self.process_ocr_result(readble_output[0]))
        player_2 = self.findClosestMatch(
            self.process_ocr_result(readble_output[1]))


from __future__ import absolute_import, division, print_function

import re

import six

from inspire_schemas.api import LiteratureBuilder

from refextract.documents.pdf import replace_undesirable_characters
from refextract.documents.text import wash_line

# Differentiate authors from affiliations:
# authors have a number after name, affiliations lines start with a number
split_authors_affs_pattern = re.compile(r'(.*?\d)\n+(\d.*)', flags=re.DOTALL)
# Differentiate affiliations from one another, line break is required
split_affs_pattern = re.compile(r'(\d+\.?[\s\t]*.*)\n*', flags=re.UNICODE)

def authorlist(text):
    """Call the relevant formatter depending on the existence of affiliations.

    Input can be in two different formats:
        F. Lastname, F.M. Otherlastname
    or
        F. Lastname1, F.M. Otherlastname1,2
        1 CERN
        2 Otheraffiliation

    There should always be only one affiliation per line and the affiliation
    ids in author names should be separated with commas.
    """
    if not text:
        return {}
    if not isinstance(text, six.text_type):
        text = text.decode('utf-8')
    # Do some pre-cleaning of the input string
    text = text.replace('\r', '')  # Input from the form contains unwanted \r's
    text = text.replace(u'†', '')
    text = text.replace(u'∗', '')
    text = re.sub(r'( *\n+ *)', r'\n', text)
    text = replace_undesirable_characters(text)
    text = wash_line(text)

    # Assume that if there are numbers in author name, they are affilation ids.
    # Also assume that if one author has an affiliation, everyone else has too.
    if re.search(r'([a-zA-Z_-]+[\n]*\d+)', text):
        return authorlist_with_affiliations(text)
    return authorlist_without_affiliations(text)

def authorlist_without_affiliations(text):
    """Return a record containing the authors."""
    builder = LiteratureBuilder()
    authors = text.replace(' and ', ', ').split(', ')
    for author in authors:
        builder.add_author(builder.make_author(author))

    return {'authors': builder.record['authors']}

def authorlist_with_affiliations(text):
    """Return a record containing the authors, including affiliations."""
    def parse_author_string(author):
        """Get fullname and affiliation ids."""
        name, affs = re.search(
            r'(.+?)(\d+[\,\d]*)', author, flags=re.UNICODE
        ).groups()
        aff_ids = [aff for aff in affs.split(',') if aff.isdigit()]

        return name, aff_ids

    def get_author_affiliations(author, affiliations):
        """Get affiliations belonging to author."""
        try:
            fullname, aff_ids = parse_author_string(author)
        except AttributeError:
            raise AttributeError(
                'Cannot split affiliation IDs from author name. This author '
                'might not have an affiliation at all', author
            )
        try:
            author_affiliations = [affiliations[aff_id] for aff_id in aff_ids]
        except KeyError:
            raise KeyError(
                'There might be multiple affiliations per line or '
                'affiliation IDs might not be separated with commas or '
                'the affiliation is missing. '
                'Problematic author and affiliations', author, aff_ids,
                affiliations
            )

        return (fullname, author_affiliations)

    # Try to work with badly formatted input
    # There should be commas between different affiliation ids in author
    # names and there should be only one affiliation name per line.
    split_auths_and_affs = split_authors_affs_pattern.search(text)
    if not split_auths_and_affs:
        raise AttributeError('Could not find affiliations')
    authors_raw = split_auths_and_affs.group(1)
    # ensure comma between affid and next author name:
    authors_raw = re.sub(r'(\d+)[\n\s](\D)', r'\1, \2', authors_raw)
    authors_raw = authors_raw.replace('\n', '')
    # ensure no comma between author name and its affids
    authors_raw = re.sub(r'(\D)\,[\n\s]*(\d)', r'\1\2', authors_raw)
    # ensure space between comma and next author name:
    authors_raw = re.sub(r'(\d+)\,(\S\D)', r'\1, \2', authors_raw)
    # ensure no spaces between affids of an author
    authors_raw = re.sub(r'(\d+)\,\s(\d+)', r'\1,\2', authors_raw)

    authors = authors_raw.replace(' and ', ', ').split(', ')

    # Extract affiliations:
    affs_raw = split_auths_and_affs.group(2)
    affs_list = split_affs_pattern.findall(affs_raw)
    affs_list = [aff.replace('\n', ' ').strip() for aff in affs_list]
    affiliations = {}
    for aff in affs_list:
        try:
            # Note that affiliation may contain numbers
            aff_id, aff_name = re.search(r'^(\d+)\.?\s?(.*)$', aff).groups()
            affiliations[aff_id] = aff_name
        except (ValueError, AttributeError):
            raise ValueError('Cannot parse affiliation', aff)

    builder = LiteratureBuilder()
    for author in authors:
        fullname, author_affs = get_author_affiliations(author, affiliations)
        builder.add_author(builder.make_author(fullname, raw_affiliations=author_affs))

    return {'authors': builder.record['authors']}


import argparse
import numpy as np
import os
import argparse
import pickle
import torch
from tqdm import tqdm
import random

from prefgen.methods.datasets.lstm import ConstraintDataset
from prefgen.methods.lstm_localization.model import PreferenceLSTM

def randomly_flip_constraints(constraints, flip_chance):
    constraint_dim = constraints.shape[1]
    attribute_dim = int(constraint_dim / 2)
    for constraint_idx in range(constraints.shape[0]):
        flip = random.random() < flip_chance
        if flip:
            positive = constraints[constraint_idx, 0:attribute_dim]
            negative = constraints[constraint_idx, attribute_dim:]
            constraints[constraint_idx, 0:attribute_dim] = negative
            constraints[constraint_idx, attribute_dim:] = positive

def evaluate_lstm_model_performance(
    model: PreferenceLSTM,
    test_dataset,
    num_examples,
    flip_chances=None
):
    save_data = {}
    for flip_chance in flip_chances:
        final_errors = []
        # For each example in the first `num_examples` examples
        for example_idx in tqdm(range(num_examples)):
            # Select the queries [0: num_queries]
            constraint_attributes = test_dataset[example_idx][-1]
            # Randomly flip
            randomly_flip_constraints(
                constraint_attributes,
                flip_chance=flip_chance
            )
            constraint_attributes = torch.Tensor(constraint_attributes).unsqueeze(0)
            # Predict using LSTM the posterior estimate
            preference_estimate = model.compute_lstm_rollout(
                constraint_attributes,
            )
            preference_estimate = preference_estimate.detach().cpu().numpy()
            final_error = np.linalg.norm(
                preference_estimate - test_dataset[example_idx][1]
            ) ** 2
            # Save the example index, the number of queries used, and the final estimate
            final_errors.append(
                final_error
            )

        save_data[flip_chance] = final_errors

    return save_data

if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument("--attribute_names", default=["yaw", "age"])
    parser.add_argument(
        "--flip_chances",
        default=[
            0.0, 0.1, 0.2, 0.3, 0.4, 0.5
        ]
    )
    parser.add_argument(
        "--test_save_path",
        default=os.path.join(
            os.environ["PREFGEN_ROOT"],
            "prefgen/data/lstm_dataset/test_dataset_gan_control.pkl"
        ),
    )
    parser.add_argument(
        "--lstm_data_path",
        type=str,
        default="data/lstm_data_random_flip_2d.pkl"
    )
    parser.add_argument(
        "--num_examples",
        default=20
    )
    parser.add_argument(
        "--num_queries",
        default=30
    )
    parser.add_argument(
        "--query_testing_interval",
        default=1
    )
    parser.add_argument(
        "--model_save_path",
        default=os.path.join(
            os.environ["PREFGEN_ROOT"],
            "prefgen/pretrained/lstm_models/lstm_model.pth"
        ),
    )

    args = parser.parse_args()
    # Make the dataset
    test_dataset = ConstraintDataset(args.test_save_path)
    # Load model
    model = PreferenceLSTM(
        attribute_size=2,
    )
    model.load_state_dict(
        torch.load(args.model_save_path)
    )
    save_data = evaluate_lstm_model_performance(
        model,
        test_dataset,
        args.num_examples,
        flip_chances=args.flip_chances
    )

    with open(args.lstm_data_path, "wb") as f:
        pickle.dump(save_data, f)


import heapq

INF = int(1e9)
dxy = [(0,1), (1,0), (-1,0), (0, -1)]
T = 1
cnt = 1

while T:
    T = int(input())
    if not T:
        break
    matrix =[]

    for _ in range(T):
        input_list = list(map(int, input().split()))
        matrix.append(input_list)

    dist = [[INF for i in range(T)] for j in range(T)]

    # heap을 이용한 dijkstra 구현
    def dijk(s_x, s_y):
        q = []
        heapq.heappush(q, (matrix[s_x][s_y], s_x, s_y))
        dist[s_x][s_y] = matrix[s_x][s_y]
        while q:
            cur, c_x, c_y = heapq.heappop(q)

            if dist[c_x][c_y] < cur:
                continue

            for x, y in dxy:
                if 0<= c_x + x < T and 0<= c_y + y < T:
                    n_x = c_x + x
                    n_y = c_y + y
                    cost = cur + matrix[n_x][n_y]
                    if cost < dist[n_x][n_y]:
                        dist[n_x][n_y] = cost
                        heapq.heappush(q, (cost, n_x, n_y))

    dijk(0, 0)
    print(f'Problem {cnt}:', dist[T-1][T-1])
    cnt += 1


import os
import gc
import yaml
from math import ceil
from functools import wraps
from dataclasses import dataclass
from typing import (
    Callable,
    Final,
    Tuple,
    List,
    Dict,
    Union
)

import torch
from torch import nn
from transformers.tokenization_utils_base import PreTrainedTokenizerBase
from transformers import (
    LEDConfig,
    LEDTokenizer,
    LEDForConditionalGeneration,
)

from .paraphrase import LEDTransferLearnerParaphrase
from .sequence_representation import LEDTransferLearnerSentenceSimilarity

@dataclass
class MultiTaskLEDConfig(object):
    """MultiTaskLEDConfig

    Configuration for MultiTaskLED

    Parameters
    ----------
    text_encoder_max_len : int
        The max token length for the text encoder
    paraphraser_max_len : int
        The max token length for the paraphraser
    summarizer_max_len : int
        The max token length for the summarizer
    text_encoder_path : str
        The path of the checkpoint for the text encoder (sequence representation)
    paraphraser_path : str
        The path of the checkpoint for the paraphraser
    summarizer_path : str
        The path of the checkpoint for the summarizer, by default "allenai/led-large-16384-arxiv"
    tokenizer_path : str
        The path of the tokenizer, by default "allenai/led-large-16384-arxiv"
    device : str
        The device where models exist, will change it to "cpu" if necessary when initialize
        by default "cuda"
    """
    text_encoder_max_len: int
    paraphraser_max_len: int
    summarizer_max_len: int
    text_encoder_path: str
    paraphraser_path: str
    summarizer_path: str = "allenai/led-large-16384-arxiv"
    tokenizer_path: str = "allenai/led-large-16384-arxiv"
    device: str = "cuda"

    @classmethod
    def load(cls, path: os.PathLike) -> "MultiTaskLEDConfig":
        with open(path, "r") as f:
            config = yaml.safe_load(f)

        return cls(**config)

def eval_mode(fn: Callable) -> Callable:
    """eval
    Clean all unused memory in the GPUs after inference

    Parameters
    ----------
    fn : Callable
        Function to be wrapped

    Returns
    -------
    Callable
        Wrapped function
    """
    @wraps(fn)
    def wrapper(*args, **kwargs):
        with torch.no_grad():
            output = fn(*args, **kwargs)

        gc.collect()
        torch.cuda.empty_cache()

        return output

    return wrapper

class MultiTaskLED(nn.Module):
    """MultiTaskLED

    A multi-task LED for summarization, paraphrasing, text encoding, and sentence similarity
    """
    _tasks: Final[List[str]] = ["summarize", "paraphrase", "encode"]
    _bad_words: Final[List[str]] = [
        "@xcite", "@xmath", "[", "]",
        "* keywords : *", "* Keywords : *",
        "_ keywords : _", "_ Keywords : _", "_ keywords _ :", "_ \n keywords : _"
    ]

    def __init__(self, config: MultiTaskLEDConfig) -> None:
        super().__init__()

        self.config = config
        device = "cuda" if torch.cuda.is_available() else "cpu"
        if self.config.device == "cuda":
            self.config.device = device

        # tokenizer
        self.tokenizer = self._load_tokenizer()
        self.bad_words_ids = self.tokenizer(self._bad_words, add_prefix_space=True, add_special_tokens=False).input_ids
        # summarizer and encoder
        self.summarizer = self._load_summarizer()
        # text encoder
        self.text_encoder = self._load_text_encoder()
        # paraphraser
        self.paraphraser = self._load_paraphraser()

    def _load_tokenizer(self) -> PreTrainedTokenizerBase:
        return LEDTokenizer.from_pretrained(self.config.tokenizer_path)

    @eval_mode
    def _load_summarizer(self) -> nn.Module:
        model = LEDForConditionalGeneration.from_pretrained(
            self.config.summarizer_path,  # "allenai/led-large-16384-arxiv"
            return_dict_in_generate=True,
            output_hidden_states=False,
            use_cache=True,
        ).to(self.config.device)

        model.eval()

        return model

    @eval_mode
    def _load_text_encoder(self) -> nn.Module:
        train_module = LEDTransferLearnerSentenceSimilarity.load_from_checkpoint(self.config.text_encoder_path)
        model = train_module.model.to(self.config.device)
        model.config.update({
            "output_hidden_states": False,
            "use_cache": False
        })

        # share the encoder
        setattr(model, "led_encoder", self.get_encoder())

        model.eval()

        return model

    @eval_mode
    def _load_paraphraser(self) -> nn.Module:
        train_module = LEDTransferLearnerParaphrase.load_from_checkpoint(self.config.paraphraser_path)
        model = train_module.model.to(self.config.device)
        model.config.update({
            "output_hidden_states": False,
            "use_cache": True
        })

        # share the encoder and partial decoder (except layer 10 and 11, layernorm_embedding, and lm_head)
        setattr(model.led, "encoder", self.get_encoder())
        for layer_idx, layer in enumerate(self.get_decoder().layers):
            if layer_idx != 10 and layer_idx != 11:
                setattr(model.led.decoder.layers, str(layer_idx), layer)

        model.led.set_input_embeddings(model.led.shared)

        model.eval()

        return model

    def _share_backbone(self, model: nn.Module) -> nn.Module:
        """_share_backbone

        Share LED's encoder and decoder among all models

        Parameters
        ----------
        model : nn.Module
            A LED model for replacing encoder and decoder

        Returns
        -------
        nn.Module
            A shared model
        """
        setattr(model.led, "encoder", self.get_encoder())
        setattr(model.led, "decoder", self.get_decoder())
        model.led.set_input_embeddings(model.led.shared)  # set encoder's input embeddings back to the original ones

        return model

    def get_encoder(self) -> nn.Module:
        return self.summarizer.get_encoder()

    def get_decoder(self) -> nn.Module:
        return self.summarizer.get_decoder()

    def get_config(self) -> LEDConfig:
        return self.summarizer.config

    def get_global_attention_mask(self, input_ids: torch.Tensor) -> torch.Tensor:
        """get_global_attention_mask

        A helper function for LED's global attention mask

        Parameters
        ----------
        input_ids : torch.Tensor
            Text's input ids generated from `tokenize`

        Returns
        -------
        torch.Tensor
        """
        mask = torch.zeros_like(input_ids)
        mask[:, 0] = 1  # set global_attention_mask on first token

        return mask

    def tokenize(self, text: List[str], max_length: int) -> Tuple[torch.Tensor]:
        """tokenize

        A helper function for tokenization,
        return input_ids, attention_mask, and global_attention_mask in sequence as a Tuple

        Parameters
        ----------
        text : List[str]
            A list of texts
        max_length : int
            The maximum token length

        Returns
        -------
        Tuple[torch.Tensor]
        """
        output = self.tokenizer(
            text,
            max_length=max_length,
            padding='max_length',
            return_tensors="pt",
            truncation='longest_first'
        ).to(self.config.device)

        input_ids = output.input_ids
        attention_mask = output.attention_mask
        global_attention_mask = self.get_global_attention_mask(input_ids)

        return (input_ids, attention_mask, global_attention_mask)

    def tokenize_into_string(self, text: str) -> List[str]:
        encoded = self.tokenizer.tokenize(text)

        return self.tokenizer.convert_tokens_to_string(encoded).split(" ")

    @eval_mode
    def embedding_distance(
        self,
        source: torch.Tensor,
        target: torch.Tensor,
        with_pairs: bool = False
    ) -> torch.Tensor:
        """embedding_distance

        Calculate distance between pairs of texts

        Parameters
        ----------
        source : torch.Tensor
            Text embeddings
        target : torch.Tensor
            Text embeddings
        with_pairs : bool
            If True, calculate distance one-to-one (output shape: (n, )),
            if False, calculate distance all-to-all (output shape: (n, m)),
            by default False

        Returns
        -------
        torch.Tensor
        """
        if with_pairs:
            return nn.functional.cosine_similarity(source, target)
        else:
            source_norm = torch.linalg.norm(source, dim=-1, keepdim=True)
            target_norm = torch.linalg.norm(target, dim=-1, keepdim=True)
            cosine_similarity = torch.mm(source, target.t()) / (torch.mm(source_norm, target_norm.t()) + 1e-12)

            return cosine_similarity.squeeze()

    @eval_mode
    def get_encoder_last_hidden_state(self, text: List[str]) -> torch.Tensor:
        """get_encoder_last_hidden_state

        Get the last hidden state of LED's encoder

        Parameters
        ----------
        text : List[str]
            A list of texts

        Returns
        -------
        torch.Tensor
        """
        assert isinstance(text, list)

        input_ids, attention_mask, global_attention_mask = self.tokenize(
            text,
            self.config.summarizer_max_len
        )

        encoder = self.get_encoder()
        last_hidden_state = encoder(
            input_ids,
            attention_mask=attention_mask,
            global_attention_mask=global_attention_mask,
            output_attentions=False,
        ).last_hidden_state.to("cpu")

        return last_hidden_state

    @eval_mode
    def summarize(
        self,
        text: Union[List[str], str],
        to_shorten_summary: bool = False,
    ) -> Union[List[str], str]:
        """summarize

        Summarize a list of texts or a text

        Parameters
        ----------
        text : Union[List[str], str]
            A list of texts or a text

        to_shorten_summary : bool
            Whether to impose length penalty when generating summary,
            by default False

        Returns
        -------
        Union[List[str], str]
        """
        assert isinstance(text, list) or isinstance(text, str)

        input_ids, attention_mask, global_attention_mask = self.tokenize(
            text if isinstance(text, list) else [text],
            max_length=self.config.summarizer_max_len
        )

        outputs = self.summarizer.generate(
            input_ids,
            attention_mask=attention_mask,
            global_attention_mask=global_attention_mask,
            max_new_tokens=96 if to_shorten_summary else 1024,
            length_penalty=3. if to_shorten_summary else 1.,
        )

        sequences = outputs["sequences"]
        summary = self.tokenizer.batch_decode(
            sequences,
            skip_special_tokens=True,
            clean_up_tokenization_spaces=True
        )

        return summary if isinstance(text, list) else summary[0]

    @eval_mode
    def generate_summary_with_context(self, context: str, prev_summary: str) -> str:
        """generate_summary_with_context

        Generate new summary that continues after the previous summary given the context.

        Parameters
        ----------
        context : str
            Context as materials for generating new summary
        prev_summary : str
            Previous summary. The new summary will follow what previous summary
            has and generate new text after it

        Returns
        -------
        str
        """
        context_input_ids, context_attention_mask, global_attention_mask = self.tokenize(
            context,
            max_length=self.config.summarizer_max_len
        )

        prev_summary_input_ids = self.tokenizer(prev_summary, return_tensors="pt").input_ids.to("cuda")
        decoder_input_ids = prev_summary_input_ids[:, : -1]
        decoder_attention_mask = (decoder_input_ids != self.tokenizer.pad_token_id)
        prev_summary_length = decoder_input_ids.shape[-1]

        outputs = self.summarizer.generate(
            context_input_ids,
            attention_mask=context_attention_mask,
            global_attention_mask=global_attention_mask,
            decoder_input_ids=decoder_input_ids,
            decoder_attention_mask=decoder_attention_mask,
            min_length=int(prev_summary_length * 1.5),
            max_new_tokens=prev_summary_length,
            length_penalty=0.7,
            repetition_penalty=3.,
            no_repeat_ngram_size=4,
            bad_words_ids=self.bad_words_ids,
            temperature=1.15,
            top_k=5,
            top_p=0.95,
            do_sample=True,
        )

        sequences = outputs["sequences"]
        summary = self.tokenizer.batch_decode(
            sequences,
            skip_special_tokens=True,
            clean_up_tokenization_spaces=True
        )[0]

        return summary

    @eval_mode
    def encode_text(
        self,
        text: List[str],
        batch_size: int = 32,
    ) -> torch.Tensor:
        """encode_text

        Encode a list of texts

        Parameters
        ----------
        text : List[str]
            A list of texts
        batch_size : int
            Batch size for encoding texts,
            by default 32

        Returns
        -------
        torch.Tensor
        """
        assert isinstance(text, list)

        num_samples = len(text)
        num_batches = ceil(num_samples / batch_size)

        out = []
        for idx in range(num_batches):
            input_ids, attention_mask, global_attention_mask = self.tokenize(
                text[(idx * batch_size): (idx + 1) * batch_size] if idx < (num_batches - 1) else text[(idx * batch_size):],
                self.config.text_encoder_max_len
            )
            encoded = self.text_encoder(
                input_ids=input_ids,
                attention_mask=attention_mask,
                global_attention_mask=global_attention_mask,
            )
            out.append(encoded.embedding)

        out = torch.cat(out, dim=0)
        out = nn.functional.normalize(out, p=2, dim=-1)

        return out

    @eval_mode
    def text_similarity(
        self,
        source: Union[List[str], str],
        target: Union[List[str], str],
        batch_size: int = 64,
        return_list: bool = False,
        return_embeddings = False,
        with_pairs: bool = False,
    ) -> Dict[str, Union[torch.Tensor, List[float]]]:
        """text_similarity

        Calculate text similarity between source and target.

        Parameters
        ----------
        source : Union[List[str], str]
            A string or a list of text
        target : Union[List[str], str]
            A string or a list of text
        return_list : bool, optional
            Whether to return each pair's similarity and source/target's embeddings as lists,
            if True, will return a list of float,
            if False will return a torch.Tensor,
            by default False.
        return_embeddings : bool
            Whether to return source's and target's embeddings,
            by default False
        with_pairs : bool
            If True, calculate distance one-to-one (output shape: (n, )),
            if False, calculate distance all-to-all (output shape: (n, m)),
            by default False

        Returns
        -------
        Dict[str, Union[torch.Tensor, List[float]]]
        """
        assert isinstance(source, list) or isinstance(source, str)
        assert isinstance(target, list) or isinstance(target, str)

        source = [source] if isinstance(source, str) else source
        target = [target] if isinstance(target, str) else target

        source_embeddings = self.encode_text(source)
        target_embeddings = self.encode_text(target)

        similarities = self.embedding_distance(source_embeddings, target_embeddings, with_pairs=with_pairs)

        if return_list:
            similarities = similarities.tolist()
        if return_embeddings:
            source_embeddings = source_embeddings.tolist() if return_list else source_embeddings.detach().cpu()
            target_embeddings = target_embeddings.tolist() if return_list else target_embeddings.detach().cpu()
        else:
            source_embeddings = None
            target_embeddings = None

        return {
            "similarities": similarities,
            "source_embeddings": source_embeddings,
            "target_embeddings": target_embeddings,
        }

    @eval_mode
    def paraphrase(self, sentences: Union[List[str], str], top_k: int = 3, explore_scale: int = 1) -> List[List[str]]:
        """paraphrase

        Paraphrase a sentence

        Parameters
        ----------
        sentences : Union[List[str], str]
            A list of sentences
        top_k : int, optional
            Return top k paraphrases, by default 3
        explore_scale : int, optional
            The scale of exploration.
            Will explore `top_k`*`explore_scale` of paraphrases,
            by default 1

        Returns
        -------
        List[List[str]]
        """
        if isinstance(sentences, str):
            sentences = [sentences]

        num_sentences = len(sentences)
        num_return_sequences = top_k * explore_scale  # double (or more) the required for later ranking/filtering
        input_ids, attention_mask, global_attention_mask = self.tokenize(
            sentences,
            self.config.paraphraser_max_len
        )

        outputs = self.paraphraser.generate(
            # https://github.com/huggingface/transformers/blob/v4.19.4/src/transformers/configuration_utils.py#L123-L183
            input_ids,
            attention_mask=attention_mask,
            global_attention_mask=global_attention_mask,
            num_return_sequences=num_return_sequences,
            # num_beams=20,
            do_sample=True,
            temperature=2.5,
            top_p=0.5,
            top_k=100,
            repetition_penalty=1.2,
            max_new_tokens=128,
            # length_penalty=1.2,
        )

        paraphrased = self.tokenizer.batch_decode(
            outputs,
            skip_special_tokens=True,
            clean_up_tokenization_spaces=True
        )
        paraphrased = [
            paraphrased[idx * num_return_sequences: (idx + 1) * num_return_sequences] if idx < (num_sentences - 1) else paraphrased[idx * num_return_sequences:]
            for idx in range(num_sentences)
        ]
        random_sample_indices = torch.randint(0, num_return_sequences, (top_k,))
        paraphrased = [
            [ele[idx] for idx in random_sample_indices]
            for ele in paraphrased
        ]

        return paraphrased

    @classmethod
    def load_from_config_path(cls, path: os.PathLike) -> "MultiTaskLED":
        config = MultiTaskLEDConfig.load(path)
        return cls(config)


import os
import pandas as pd

from gensim.models import Word2Vec

from tokenizer.tokenizer import tokenize_text, end_of_clause
from utils import eng_stopwords, cosine_distance, pca_reduce

def w2v_train(fname, window, min_freq, vec_size, out_path):
    print('working on ', fname)
    df = pd.read_csv(fname, sep=',', header=None)
    data = df.values
    data_count = len(data)
    n = 0
    training_list = []
    for row in data:
        try:
            for i in range(1, len(row)):
                text = row[i]
                tokens = tokenize_text(text)
                words = []
                prev = [0, '', '']
                for w in tokens:
                    if w[1] == 'word' and w[1] not in eng_stopwords:
                        words.append(w[2].strip().lower())
                    elif prev[2] in end_of_clause:
                        training_list.append(words)
                        words = []
                    prev = w
                if len(words) > 0:
                    training_list.append(words)
                    words = []  # cleanup list of words
        except Exception as e:
            print(e)
            pass
        n = n + 1
        if n % 1000 == 0:
            print(int(n * 100 / data_count), '%')
    print("Training")
    model = Word2Vec(sentences=training_list, window=window, min_count=min_freq, workers=4, vector_size=vec_size)
    model.save(out_path)

def apply_w2v_model(model_path, initial_token, similar_tokens, same_tokens, other_tokens, vec_dimension):
    model = Word2Vec.load(model_path)
    print(model)
    tokens = []
    print("Token: " + initial_token)
    tokens.append(initial_token)
    print("Similar:")
    print_cosine_distance(model, tokens, initial_token, similar_tokens)
    print("Same:")
    print_cosine_distance(model, tokens, initial_token, same_tokens)
    print("Other:")
    print_cosine_distance(model, tokens, initial_token, other_tokens)

    x = [model.wv.get_vector(token) for token in tokens]
    result = pca_reduce(x, vec_dimension)
    return result, tokens

def print_cosine_distance(model, tokens, initial_token, curr_tokens):
    for token in curr_tokens:
        tokens.append(token)
        print("\t" + token + ": " + str(
            cosine_distance(model.wv.get_vector(initial_token), model.wv.get_vector(token))))


import numpy as np
from tensorflow import keras
from tensorflow.keras.models import Sequential, Model, load_model
from PIL import Image
import sys

import tensorflow as tf
def memory_limit():
    physical_devices = tf.config.list_physical_devices('GPU')
    if len(physical_devices) > 0:
        for device in physical_devices:
            tf.config.experimental.set_memory_growth(device, True)
            print('{} memory growth: {}'.format(device, tf.config.experimental.get_memory_growth(device)))
    else:
        print("Not enough GPU hardware devices available")

memory_limit()

# パラメーターの初期化
classes = ["cow", "chicken", "pig"]
num_classes = len(classes)
image_size = 224

# 引数から画像ファイルを参照して読み込む
image = Image.open(sys.argv[1])
image = image.convert("RGB")
image = image.resize((image_size, image_size))
data = np.asarray(image) / 255.0
X = []
X.append(data)
X = np.array(X)

# モデルのロード
model = load_model('./vgg16_transfer.h5')

result = model.predict([X])[0]
predicted = result.argmax() # 最大値のインデックスの値を取る
percentage = int(result[predicted] * 100)

cow_res = str(round(result[0] * 100, 1))
chicken_res = str(round(result[1] * 100, 1))
pig_res = str(round(result[2] * 100, 1))

# print(classes[predicted], percentage, "%")
print("cow: " + cow_res + "%\n" +
    "chicken: " + chicken_res + "%\n" +
    "pig: " + pig_res + "%")


import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')

# Defining a function that will do the detections
def detect(gray, frame):
    gray1 = None
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
        gray1 = roi_gray
    return gray1

# Doing some Face Recognition with the webcam
video_capture = cv2.VideoCapture(0)

i = 0
while (i < 300):
    print('vijani ' + str(i))
    _, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    face = detect(gray, frame)

    if face is not None:
        print('/home/vijani/Desktop/movetopi/images/img'+ str(i) + '.jpg')
        cv2.imwrite('/home/vijani/Desktop/movetopi/images/img'+ str(i) + '.jpg', face)

    i = i + 1
'''
    cv2.imshow('Video', face)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()
'''


import pygame
import random
from os import path

img_dir = path.join(path.dirname(__file__), 'img')

WIDTH = 500
HEIGHT = 750
FPS = 60

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("SHOOT THEM DOWN")
clock = pygame.time.Clock()

font_name = pygame.font.match_font("arial")

def draw_text(surf, text, size, x,y):
    font = pygame.font.Font(font_name, size)
    text_surface = font.render(text, True, WHITE)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.scale(joueur_image, (80, 68))
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH / 2
        self.rect.bottom = HEIGHT - 10
        self.speedx = 0

    #update sert aux mouvement du joueur et bloquer les murs pour ne pas passer a travers

    def update(self):
        self.speedx = 0
        clavier = pygame.key.get_pressed()
        if clavier[pygame.K_LEFT]:
            self.speedx = -6
        if clavier[pygame.K_RIGHT]:
            self.speedx = 6
        self.rect.x += self.speedx
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.left < 0:
            self.rect.left = 0

    def shoot(self):
        balle = Projectils(self.rect.centerx, self.rect.top)
        all_sprites.add(balle)
        balles.add(balle)

class Ennemis(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.scale(ennemis_image, (80, 68))
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(WIDTH - self.rect.width)
        self.rect.y = random.randrange(-100, -40)
        self.speedy = random.randrange(1, 8)
        self.speedx = random.randrange(-3, 3)

    # fais bouger les ennemies et les renvois en haut quand ils sont tout en bas

    def update(self):
        self.rect.x += self.speedx
        self.rect.y += self.speedy
        if self.rect.top > HEIGHT + 10 or self.rect.left < - 20 or self.rect.right > WIDTH:
            self.rect.x = random.randrange(WIDTH - self.rect.width)
            self.rect.y = random.randrange(-100, -40)
            self.speedy = random.randrange(2, 9)

def show_gameover_screen():
    screen.blit(background, background_rect)
    draw_text(screen, "Shootzemup", 64, WIDTH / 2, HEIGHT /4)
    draw_text(screen, "Appuyez sur entrée pour commencer !", 18, WIDTH / 2, HEIGHT / 2)
    pygame.display.flip()
    waiting = True
    while waiting:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    waiting = False

class Projectils(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10, 20))
        self.image.fill(WHITE)
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.speedy = -10

    def update(self):
        self.rect.y += self.speedy
        #supprime la balle si elle sort de l'ecran
        if self.rect.bottom < 0:
            self.kill()

#images pour le jeu

background = pygame.image.load(path.join(img_dir, "background.jpg")).convert()
background_rect = background.get_rect()
joueur_image = pygame.image.load(path.join(img_dir, "vaisseau.png")).convert()
ennemis_image = pygame.image.load(path.join(img_dir, "ennemis.png")).convert()

# tous les évenement liés aux touches
game_over = True
running = True

while running:
    if game_over:
        show_gameover_screen()
        game_over = False
        all_sprites = pygame.sprite.Group()
        mobs = pygame.sprite.Group()
        balles = pygame.sprite.Group()
        player1 = Player()
        all_sprites.add(player1)
        for i in range(9):
            enmi1 = Ennemis()
            all_sprites.add(enmi1)
            mobs.add(enmi1)
    # keep loop running at the right speed
    clock.tick(FPS)
    # Process input (events)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player1.shoot()
    # Update
    all_sprites.update()
    # voir si une balle touche un ennemi et le supprime si oui
    touche = pygame.sprite.groupcollide(mobs, balles, True, True)
    for touche1 in touche:
        enmi1 = Ennemis()
        all_sprites.add(enmi1)
        mobs.add(enmi1)

    #voir si l'ennemis a touché le vaisseau (colisions) touche = liste et si elle est rempli ça veut dire qu'on a touché

    touche = pygame.sprite.spritecollide(player1, mobs, False)
    if touche:
        game_over = True
    # Draw / render
    screen.fill(BLACK)
    screen.blit(background, background_rect)
    all_sprites.draw(screen)
    # *after* drawing everything, flip the display
    pygame.display.flip()

pygame.quit()


"""
Song detail scanner system.
"""
# Pylint disable settings --------------------
#
# ToDos, DocStrings:
# pylint: disable-msg=W0511,W0105
#
# Protected member access:
# pylint: disable-msg=W0212
#
# Too many instance attributes, Too few public methods, Too many init arguments:
# pylint: disable-msg=R0902,R0903,R0913

_SCANNERS = []
"""Scanners registered."""

_MULTI_SCANNERS = []
"""Multi scanners registered."""

_HAS_DEFAULTS = False
"""Has default scanners set already?"""

def register_file_scan(scan_function, extension_matches=None,
                       custom_matcher=None):
    """Register single file scanner.

    :param scan_function: Scan function returning SongDetails.
    :param extension_matches: Match files by extension, for example
        ``("mp3", "mp2", "mp1")``.
    :param custom_matcher: Match files by custom function, custom function gets
        only filepaths or urls as argument and should return :const:`True` or
        :const:`False`.

        .. note::

            Custom matcher should never assume that it can open the file given
            in argument, it should only try to roughly match using the string
            only!

            Reason is that custom_matcher may be given URL's, for instance when
            registered scanner handles file objects retrieved behind URL.

    """
    def extension_matcher(filepath):
        """Match by given extensions"""
        for ext in extension_matches:
            if filepath.endswith(ext):
                return True
        return False

    file_path_matcher = custom_matcher or extension_matcher
    _SCANNERS.append((scan_function, file_path_matcher))

def register_files_scan(scan_files_function, files_matcher):
    """Register files scanner.

    :param scan_files_function: Scan function returning SongDetails from list of
        files.
    :param files_matcher: Function returning True for list of files that match
        or False for files that doesn't match.

    """
    _MULTI_SCANNERS.append((scan_files_function, files_matcher))

def scan_files(files):
    """Scan several files for SongDetails.

    :param files: List of files to be scanned, you can also give this single
        file.
    :type files: [string, ...]

    :rtype: [:class:`SongDetails`, ...]
    :return: List of SongDetails found, list maybe empty if nothing is found.

    """

    # If the given files are not iterable, such as string. Make it iterable.
    if not hasattr(files, "__iter__"):
        files = [files]

    # Register default scanners
    _register_default_scanners()

    # Search from all m
    songs = []
    for scan_files_, files_matcher in _MULTI_SCANNERS:
        if files_matcher(files):
            songs.append(scan_files_(files))

    for file_path in files:
        song = scan(file_path)
        if song is not None:
            songs.append(song)

    return songs

def scan(file_path):
    """Scan single file

    :param file_path: Path to the file.

    :return: Returns the SongDetails matching the given file path.
    :rtype: :class:`SongDetails`, or :const:`None`

    """

    # Registers default scanners
    _register_default_scanners()

    # Tries all registered scanners
    for scan_function, file_path_matcher in _SCANNERS:
        if file_path_matcher(file_path):
            song = scan_function(file_path)
            if song is not None:
                return song
    return None

def _register_default_scanners():
    """Registers the default scanners provided in :mod:`songdetails`.

    If you wish to register own scanners, you should use instead
    :func:`register_file_scan` and :func:`register_files_scan` directly within
    your module.

    """
    global _HAS_DEFAULTS
    from songdetails.mp3details import scan as mp3_scan
    if _HAS_DEFAULTS:
        return
    _HAS_DEFAULTS = True

    # Register individual scanners
    register_file_scan(mp3_scan, ('.mp3', ))


import  tkinter         as tk
from    tkinter         import *
from    tkinter.ttk     import *   # Enhanced widgets????
import  PIN_class       as PIN
#import  MY_GPIO_class   as MYGPIO

# --------------
# Constant definitions
# --------------

NO_GPIO         = True # Set to True to test program GUI with no GPIO calls
NUM_PINS        = 26    # Number of pins to be shown in dashboard
P_NM_COL_W      = 10    # Pin name column width
P_RB_COL_W      = 4     # Radiobutton column width
CB_COL_W        = 4     # Chkbox column width
COL_PAD         = 3     # Column padding

P_NM_COL        = 0     # Column number for pin name
DIR_IO_COL      = 2     # Column number for input menu
OUT_HIGH_COL    = 3     # Column number for output high/low
PWM_COL         = 4     # Column for PWM chkkbox
FREQ_COL        = 5     # Column for frequency spinner
D_CYC_COL       = 6     # Column for frequency spinner
EDGE_COL        = 8     # Column for edge menu
BOUNCE_COL      = 9     # Column number for bounce slider

DIR_MENU_OPTS   = ["IN","OUT","PWM"]
EDGE_OPTS       = ["NONE","FALLING","RISING","BOTH"]
BOUNCE_OPTS     = ["0","100","200"]

I2C_COLOR       = 'blue'
SPI_COLOR       = 'magenta'
PCM_COLOR       = 'green'
UART_COLOR      = 'yellow'

DB_WINDOW_TITLE = "Bart's GPIO Dashboard"

PIN_LIST        = [[2,'I2C'],   \
                  [3,'I2C'],    \
                  [4,''],       \
                  [5,''],       \
                  [6,''],       \
                  [7,'SPI'],    \
                  [8,'SPI'],    \
                  [9,'SPI'],    \
                  [10,'SPI'],   \
                  [11,'SPI'],   \
                  [12,''],      \
                  [13,''],      \
                  [14,'UART'],  \
                  [15,'UART'],  \
                  [16,''],      \
                  [17,''],      \
                  [18,'PCM'],   \
                  [19,'PCM'],   \
                  [20,'PCM'],   \
                  [21,'PCM'],   \
                  [22,''],      \
                  [23,''],      \
                  [24,''],      \
                  [25,''],      \
                  [26,''],      \
                  [27,'']]

# --------------
# Function to build dashboard header
#---------------

def create_DB_Header():
        master.title(DB_WINDOW_TITLE)

        # Add labels to header frame
        tk.Label(root,text = "PIN ID",  fg='blue',padx=COL_PAD,width=P_NM_COL_W).grid(row=0,column=P_NM_COL)
        tk.Label(root,text = " ",       fg='blue',padx=COL_PAD,width=COL_PAD).grid(row=0,column=1)
        tk.Label(root,text = "IN/OUT",  fg='blue',padx=COL_PAD,width=P_RB_COL_W).grid(row=0,column=DIR_IO_COL)
        tk.Label(root,text = "HIGH",    fg='blue',padx=COL_PAD,width=P_RB_COL_W).grid(row=0,column=OUT_HIGH_COL)
        tk.Label(root,text = "PWM",     fg='blue',padx=COL_PAD,width=P_RB_COL_W).grid(row=0,column=PWM_COL)
        tk.Label(root,text = "FREQ",    fg='blue',padx=COL_PAD,width=P_RB_COL_W).grid(row=0,column=FREQ_COL)
        tk.Label(root,text = "D_CYC",   fg='blue',padx=COL_PAD,width=P_RB_COL_W).grid(row=0,column=D_CYC_COL)
        tk.Label(root,text = " ",       fg='blue',padx=COL_PAD,width=COL_PAD).grid(row=0,column=7)
        tk.Label(root,text = "EDGE",    fg='blue',padx=COL_PAD,width=10).grid(row=0,column=EDGE_COL)
        tk.Label(root,text = "BOUNCE",  fg='blue',padx=COL_PAD,width=10).grid(row=0,column=BOUNCE_COL)

# --------------
# Function to create vars used to transfer widget changes and set pointer in PIN objects
#---------------

def set_widget_vars(pin):
        vDir = tk.StringVar()                   # Variable for pin direction
        pin.set_DIR_var(vDir)

        vCB_out = tk.IntVar(pin.LOW)            # Create variable for output-high button
        pin.set_OUT_var(vCB_out)

        vCB_pwm = tk.IntVar(pin.PWM_OFF)        # Create variable for PWM chkbox
        pin.set_PWM_var(vCB_pwm)

        vSB_freq = tk.StringVar("")             # Frequency
        pin.set_FREQ_var(vSB_freq)

        vSB_dcyc = tk.StringVar("")             # Duty cycle
        pin.set_DCYC_var(vSB_dcyc)

        vEdge = tk.StringVar("")                # Edge (Falling, rising, or both)
        pin.set_EDGE_var(vEdge)

        vBounce = tk.StringVar("")              # Bounce value
        pin.set_BOUNCE_var(vBounce)

# --------------
# Main line of program
#---------------

if not NO_GPIO:
        gpio = MYGPIO.myGPIO()  # My class to initialize GPIO

master = tk.Tk()          # Initialize gui
root = Canvas(master,width=600,height=100)
root.pack()
#vsb = Scrollbar(root,orient="vertical")
#vsb.pack(side="right")
create_DB_Header()

# Initialize pins
for p in PIN_LIST:
        pin_num=p[0]
        pin = PIN.PIN(pin_num)  # Create pin object

        vDir = tk.StringVar()
        vDir.set(DIR_MENU_OPTS[1])
        pin.set_DIR_var(vDir)

        vCB_out = tk.IntVar(pin.LOW)    # Create variable for output-high button
        pin.set_OUT_var(vCB_out)        # Set pin to LOW#

        vCB_pwm = tk.IntVar(pin.PWM_OFF)# Create variable for PWM chkbox
        pin.set_PWM_var(vCB_pwm)        # Set pin to LOW

        vSB_freq = tk.StringVar("")
        pin.set_FREQ_var(vSB_freq)

        vSB_dcyc = tk.StringVar("")
        pin.set_DCYC_var(vSB_dcyc)

        vEdge = tk.StringVar("")
#        vEdge.set(EDGE_OPTS[0])
        pin.set_EDGE_var(vEdge)
#
        vBounce = tk.StringVar("")
#        vBounce.set(BOUNCE_OPTS[0])
        pin.set_BOUNCE_var(vBounce)

        # Add lbl(pin name) and other controls for each button
        pin_name    = tk.Label(root,padx=COL_PAD,width=P_NM_COL_W,text = "PIN-"+str(pin_num)+" "+p[1])
        if p[1] == "PCM":
                pin_name.config(bg=PCM_COLOR)
        elif p[1] == "I2C":
                pin_name.config(bg=I2C_COLOR,fg='white')
        elif p[1] == "UART":
                pin_name.config(bg=UART_COLOR)
        elif p[1] == "SPI":
                pin_name.config(bg=SPI_COLOR)
        else:
                pass

# Add colunm for In/Out selection
        oMenu_dir   = tk.OptionMenu(root,pin.get_DIR_var(),*DIR_MENU_OPTS,command = pin.dir_selected)
        oMenu_dir.config(width=5)

# Add column for forcing output to high
        cbox_high   = tk.Checkbutton(root,padx=COL_PAD,width=P_RB_COL_W,variable=pin.get_OUT_var(),onvalue=1,offvalue=0,command=pin.out_high_selected)

#Add columns for PWM
        pwmFrame = tk.Frame(root,relief=GROOVE,borderwidth=1,bg='black',height=3)
        pwmFrame.grid(row=pin_num-1,column=PWM_COL,columnspan=3)

        cbox_pwm        = tk.Checkbutton(pwmFrame,width=3,variable=pin.get_PWM_var(),onvalue=1,offvalue=0,command=pin.pwm_selected)
        sbox_freq       = tk.Spinbox(pwmFrame,bd=1,width=5,from_=1,to=100,textvariable=pin.get_FREQ_var(),command=pin.freq_selected)
        sbox_dcyc       = tk.Spinbox(pwmFrame,bd=1,width=5,from_=0,to=100,textvariable=pin.get_DCYC_var(),command=pin.dcyc_selected)
        sbox_freq.config(state='disabled')
        sbox_dcyc.config(state='disabled')

# Add columns for edge and bouce
        oMenu_edge      = tk.OptionMenu(root,pin.get_EDGE_var(),*EDGE_OPTS)
        oMenu_edge.config(width=9)

    #    oMenu_bounce   = tk.OptionMenu(root,vBounce,"0","100","200")
    #    oMenu_bounce.config(width=9)

#        scale_bounce = tk.Scale(root,vBounce,from_=1,to=100)
        scale_bounce    = tk.Scale(root,variable=pin.get_BOUNCE_var(),from_=1,to=200,orient=HORIZONTAL,command=pin.bounce_selected,bigincrement=10)

        # Position GUI elements

        pin_name.grid(row=pin_num-1,column=P_NM_COL)
        oMenu_dir.grid(row=pin_num-1,column=DIR_IO_COL)
        cbox_high.grid(row=pin_num-1,column=OUT_HIGH_COL)
        cbox_pwm.grid(row=pin_num-1,column=0)   # Column within PWM frame?
        sbox_freq.grid(row=pin_num-1,column=1)  # Column within PWM frame?
        sbox_dcyc.grid(row=pin_num-1,column=2)  # Column within PWM frame?
        oMenu_edge.grid(row=pin_num-1,column=EDGE_COL)

     #   oMenu_bounce.grid(row=pin_num-1,column=BOUNCE_COL)
        scale_bounce.grid(row=pin_num-1,column=BOUNCE_COL)

        # More GUI initializtion stuff

        if pin.get_direction() != pin.OUTPUT_TXT:# If direction is not output,
                cbox_high.config(state=DISABLED)        # disable checkbox
        pin.set_CB_OH(cbox_high)                      # Store linkto checkbox object in pin instance
        pin.set_CB_PWM(cbox_pwm)
        pin.set_SB_FREQ(sbox_freq)
        pin.set_SB_DCYC(sbox_dcyc)
        pin.set_OM_EDGE(oMenu_edge)
      #  pin.set_OM_BOUNCE(oMenu_bounce)
        pin.set_OM_BOUNCE(scale_bounce)
root.mainloop()

if not NO_GPIO:
        gpio.cleanup()

# End program


import wget
import re
import os

import pandas as pd
import wget
import re
import os

import pandas as pd

import json

data=pd.read_csv('../cim_codes.csv')
data = data

i=1
outdata={}
for d in data.to_numpy():
    try:
        os.remove("data.html")
    except FileNotFoundError:
        pass
    cim=d[0].lower()
    print("process code ("+str(i)+"): '"+cim+"'")

    url = 'https://www.aideaucodage.fr/cim-'+cim
    print(url)
    try:
        wget.download(url, out="data.html")
    except:
        continue

    f=open('data.html',"r", encoding="latin-1")
    content = f.read()
    f.close()

    content=content.replace("\n","")
    content=content.replace("\t","")
    content=content.replace("\r","")

    #print("---- ACTES CCAM Liés ----")
    m=re.search("
Actes CCAM(.*)

(.*)
(.*)
",content)
    try:
        tabledata=m.group(2)
        m=re.findall("(\w+?)	(['()\-,\w\s]+?)

",tabledata, re.UNICODE)
        ccam={e[1]:(float(e[3])-2) for e in m}
        #print(ccam)
    except AttributeError:
        ccam={}

    #print("---- Diagnostics CIM Liés ----")

    try:
        m=re.search("
Diagnostics CIM\-10 \(DAS\) associés à (\w*)

(.*)
",content, re.UNICODE)
        tabledata=m.group(2)
        m=re.findall("(\w+[.]{0,1}\w*)	(\d+)	(['()\-,\w\s]+?)

",tabledata, re.UNICODE)
        cimd={e[1]:(float(e[4])-2) for e in m}
        #print(cimd)
    except AttributeError:
        cimd={}

    #print("---- GHM associés ----")
    try:
        m=re.search("
GHM associés avec (\w*)

(.*)
",content, re.UNICODE)
        tabledata=m.group(2)
        m=re.findall("(\w+)	(['()\-,\w\s]+?)

",tabledata, re.UNICODE)
        ghm={e[1]:(float(e[3])-2) for e in m}
        #print(ghm)
    except AttributeError:
        ghm={}

    outdata[cim.upper()]={"ccam":ccam,"cim":cimd,"ghm":ghm}

    if i%100==0:
        with open('result.json', 'w') as fp:
            json.dump(outdata, fp)
    i+=1


import pandas as pd

df = pd.read_csv("./datasets/youtube-ing.csv")

result = df
result = len(df.columns)
result = df.head(20).tail(10)
result = df[10:20]
df.drop(["thumbnail_link", "description", "video_error_or_removed", "comments_disabled", "ratings_disabled"],
        inplace=True, axis=1)
result = df.head(100).mean()[["likes", "dislikes"]]
result = df[df["views"] == df["views"].max()]
result = df[df["views"] == df["views"].min()]
result = df.sort_values(by="views").tail(10)
df["title_len"] = df["title"].apply(len)
result = df['title_len']

df["tag_count"] = df["tags"].apply(lambda tags: len(tags.split("|")))

like_lists = list(df["likes"])
dislike_lists = list(df["dislike"])
result = df
print(result)