# Example:  Triggers sampler based on volume

"""
* Sampler is triggered based on water volume.
* If volume is insufficient, triggers sampler at 20:00:00 UTC
* Sampler triggers no more than once per day.

A Sat/XLink setup is associated with this module:
`auto_sampler_volume_setup.txt <auto_sampler_volume_setup.txt>`_
"""

from sl3 import *
import utime

# We remember the total volume
g_volume_total = 0.0

# The sampler has a limited number of bottles.
bottles_capacity = 24

# We count how many bottles are in use.
bottles_used = 0

# Time sampler was triggered last.
time_last_sample = 0.0

def triggered_today():
    """Have we triggered the sampler today?"""
    if time_last_sample == 0.0:
        return False  # we never triggered
    else:
        # what is the day today?
        today = utime.localtime()[6]  # returns the weekday (value from 0 to 6)

        # and what day did we last trigger?
        last = utime.localtime(time_last_sample)[6]

        if today == last:
            return True  # yes we triggered today
        else:
            return False

def trigger_sampler_master():
    """Triggers the sampler immediately and logs it."""

    global bottles_used
    global time_last_sample

    # increment the number of bottles used
    bottles_used += 1

    # update the time of the last trigger
    time_last_sample = utime.time()

    # clear the total volume
    global g_volume_total
    g_volume_total = 0.0

    # trigger sampler by pulsing output for 5 seconds
    power_control('SW2', True)
    utime.sleep(5.0)
    power_control('SW2', False)

    # write a log entry
    reading = Reading(label="Triggered", time=time_scheduled(),
                      etype='E', value=bottles_used,
                      right_digits=0, quality='G')
    reading.write_log()

def trigger_sampler():
    """
    Call to attempt to trigger the sampler.
    Certain conditions may prevent the triggering.

    :return: True if sampler was triggered.
    """
    global bottles_capacity
    global bottles_used
    global time_last_sample

    trigger = True

    if bottles_used >= bottles_capacity:
        trigger = False  # out of bottles
    elif triggered_today():
        trigger = False  # already triggered today
    elif is_being_tested():
        trigger = False  # script is being tested
    elif setup_read("Recording").upper() == "OFF":
        trigger = False  # if recording is off, do not sample

    if trigger:
        trigger_sampler_master()  # Call routine that controls sampler.
        return True

    else:
        return False  # Sampler was NOT triggered.

@TASK
def daily_trigger():
    """
    This function should be associated with a script task scheduled for a certain time every day.
    If the sampler has not been triggered today, this function will trigger it.
    """

    if trigger_sampler():
        # sampler was triggered

        global g_volume_total

        # Write a log entry indicating why sampler was triggered.
        reading = Reading(label="DailyTrig", time=time_scheduled(),
                          etype='E', value=g_volume_total, quality='G')
        reading.write_log()

def volume_threshold():
    """
    Returns the threshold at which the volume difference triggers the sampler.
    It is stored in the setup as Alarm 1 Threshold.

    :return: volume threshold
    :rtype: float
    """
    setup_str = setup_read("M{} Alarm 1 Threshold".format(index()))
    return float(setup_str)

@MEASUREMENT
def compute_volume_total(inval):
    """
    This function needs to be associated with the total volume measurement.
    It will compute the total volume based on the current volume and past volume.
    The script will trigger the sampler if appropriate.

    :param inval: not relevant
    :return: the current volume reading

    """

    global g_volume_total

    # update total volume and store it in a local variable
    # in case we need to clear g_volume_total
    if is_scheduled():  # do not tally volume unless this is a scheduled measurement
        g_volume_total += measure("VOLUME").value  # update total volume

    local_total = g_volume_total  # copy to a local variable

    # if the volume is high enough, trigger sampler
    if local_total > volume_threshold():
        if trigger_sampler():
            # sampler was triggered

            # Write a log entry indicating why sampler was triggered.
            reading = Reading(label="VolumeTrig", time=time_scheduled(),
                              etype='E', value=local_total, quality='G')
            reading.write_log()

    # add diagnostic info to the script status
    print("Bottles used: {}".format(bottles_used))
    print("Bottle capacity: {}".format(bottles_capacity))
    if time_last_sample:
        print("Last trigger: {}".format(ascii_time(time_last_sample)))
        if triggered_today():
            print("   Which was today")
        else:
            print("   No trigger today")
    else:
        print("Not triggered since bootup")

    return local_total  # return the total volume (before clearing it)


from modules import telegramcalendar
from modules import weather_scraper
from modules import wiki_browser
#from modules import tech_news_scraper ignore this
from modules.weather_scraper import WeatherScraper
from modules.wiki_browser import WikiBrowser
import logging
from datetime import datetime, timedelta
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler
import requests
import json
import os
import random
from random import randint
from better_profanity import profanity
from pycoingecko import CoinGeckoAPI

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

logger = logging.getLogger(__name__)

NAME, DATE_Q, TIME_Q, INFO, OPT = range(5)
UTC_1, UTC_2 = range(2)

# ALL REMIND SYSTEM....

def json_editor(user, key, value):
    user = str(user)
    with open("data/reminder.json", "r+") as file:
        content = json.load(file)
        if user not in content["reminder"].keys():
            content["reminder"][user] = {"utc": 0, "reminder": []}
        if key == "name":
            content["reminder"][user]["reminder"].insert(0, {})
        content["reminder"][user]["reminder"][0][key] = value
        file.seek(0)
        json.dump(content, file)
        file.truncate()

def json_getter(user, ):
    with open("data/reminder.json") as file:
        content = json.load(file)
        element = content["reminder"][user]["reminder"][0]
        name = element["name"]
        date = element["date"]
        _time = element["time"]
        r_id = element["id"]
        return name, date, _time, r_id

def json_deleter(user, r_id=None, current=False):
    with open("data/reminder.json", "r+") as file:
        content = json.load(file)
        reminder = content["reminder"][user]["reminder"]
        if not current:
            for i in range(len(reminder)):
                if reminder[i]["id"] == r_id:
                    del reminder[i]
                    break
        else:
            del reminder[0]
        file.seek(0)
        json.dump(content, file)
        file.truncate()

def json_utc(user, utc=None):
    with open("data/reminder.json", "r+") as file:
        content = json.load(file)
        if utc is None:
            return content["reminder"][user]["utc"]
        else:
            content["reminder"][user]["utc"] = utc
            file.seek(0)
            json.dump(content, file)
            file.truncate()

def all_reminder(update, context):
    reply_keyboard = [["/start", "/list", "/time"]]
    username = str(update.message["chat"]["id"])
    with open("data/reminder.json") as file:
        content = json.load(file)
        reminder = content["reminder"][username]["reminder"]
        if len(reminder) == 0:
            update.message.reply_text(f"\U0001F4C3 *Lista De Reminds* \U0001F4C3\n\nNo tienes ningun remind guardado!", reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True, resize_keyboard=True), parse_mode="markdown")
        else:
            update.message.reply_text("\U0001F4CB* Lista De Reminds *\U0001F4CB", parse_mode="markdown")
            for i, v in enumerate(reminder):
                name = v["name"]
                date = v["date"]
                _time = v["time"]
                if "opt_inf" in v.keys():
                    information = v["opt_inf"]
                    if i == len(reminder) - 1:
                        update.message.reply_text(f"{i+1}:   Nombre: {name}\n      Fecha: {date}\n      Hora: {_time}\n      Information: {information}", reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True, resize_keyboard=True))
                    else:
                        update.message.reply_text(f"{i+1}:   Nombre: {name}\n      Fecha: {date}\n      Hora: {_time}\n      Information: {information}")
                else:
                    if i == len(reminder) - 1:
                        update.message.reply_text(f"{i+1}:   Nombre: {name}\n      Fecha: {date}\n      Hora: {_time}", reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True, resize_keyboard=True))
                    else:
                        update.message.reply_text(f"{i+1}:   Nombre: {name}\n      Fecha: {date}\n      Hora: {_time}")

def utc_time(update, context):
    update.message.reply_text("Elige la zona horaria de tu lugar de residencia!", reply_markup=telegramcalendar.create_timezone())
    return UTC_1

def utc_time_selector(update, context):
    reply_keyboard = [["/start", "/list", "/time"]]
    selected, num = telegramcalendar.process_utc_selection(context.bot, update)
    if selected:
        chat_id = str(update.callback_query.from_user.id)
        json_utc(chat_id, utc=num)
        context.bot.send_message(chat_id=update.callback_query.from_user.id,
                        text=f"Has elegido UTC + {num}" if num >= 0 else f"Has elegido UTC - {abs(num)}",
                        reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True, resize_keyboard=True))
        return ConversationHandler.END

def notification(context):
    reply_keyboard = [["/start", "/list", "/time"]]
    job = context.job
    if len(job.context) == 6:
         name, date, _time, username, r_id = job.context[1], job.context[2], job.context[3], job.context[4], job.context[5]
         context.bot.send_message(job.context[0], text=f"\U0001F4A1* Reminder *\U0001F4A1\n\nNombre: {name}\nFecha y hora {date} - {_time}.\nLlego la hora!", parse_mode="markdown")
    else:
        name, date, _time, username, r_id, information = job.context[1], job.context[2], job.context[3], job.context[4], job.context[5], job.context[6]
        context.bot.send_message(job.context[0], text=f"\U0001F4A1* Reminder *\U0001F4A1\n\nNombre: {name}\nInformaciÃ³n: {information}\n\nFecha y hora {date} - {_time}.\nLlego la hora!", parse_mode="markdown", reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True, resize_keyboard=True))
    json_deleter(username, r_id=r_id)

def name(update, context):
    name = update.message.text
    if name == "/cancel":
        cancel(update, context)
        return ConversationHandler.END
    username = update.message["chat"]["id"]
    json_editor(username, "name", name)
    logger.info("Name: %s", update.message.text)
    update.message.reply_text(f"\U0001F4C5* Reminder Setup *\U0001F4C5\n\nÂ¿Que fecha quieres para \n el remind *{name}*?",
                              reply_markup=telegramcalendar.create_calendar(), parse_mode="markdown")
    return DATE_Q

def remind(update, context):
    # print(update.message)
    update.message.reply_text("\U0001F4CD* Reminder Setup *\U0001F4CD\n\nComo se va a llamar \ntu nuevo remind?", parse_mode="markdown")
    # update.message.reply_text(f"test", reply_markup=telegramcalendar.create_clock(), parse_mode="markdown")
    return NAME

def inline_handler(update, context):
    selected, date = telegramcalendar.process_calendar_selection(context.bot, update)
    if selected:
        json_editor(str(update.callback_query.from_user.id), "date", date.strftime("%d/%m/%Y"))
        context.bot.send_message(chat_id=update.callback_query.from_user.id,
                        text="Has elegido %s" % (date.strftime("%d/%m/%Y")),
                        reply_markup=ReplyKeyboardRemove())
        context.bot.send_message(chat_id=update.callback_query.from_user.id, text="\U0001F553* Reminder Setup *\U0001F553\n\nÂ¿A que hora sera \n tu remind?", parse_mode="markdown", reply_markup=telegramcalendar.create_clock(user=update.callback_query.from_user.id))
        return TIME_Q

def inline_handler2(update, context):
    selected, _time = telegramcalendar.process_clock_selection(context.bot, update)
    if selected:
        chat_id = str(update.callback_query.from_user.id)
        r_id = random.randint(0, 100000)
        format_time = f"{_time[0]}:{_time[1]} {_time[2]}"
        json_editor(chat_id, "time", format_time)
        json_editor(chat_id, "id", r_id)

        context.bot.send_message(chat_id=update.callback_query.from_user.id,
                                 text=f"Has elegido {format_time}",
                                 reply_markup=ReplyKeyboardRemove())
        reply_keyboard = [["Si", "No"]]
        context.bot.send_message(chat_id=update.callback_query.from_user.id,
                                text=f"\U0001F530 *Reminder Setup* \U0001F530\n\nÂ¿Quieres aÃ±adir \nmas informacion al reminder?",
                                  reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True, resize_keyboard=True), parse_mode="markdown")
        return INFO

def info(update, context):
    text = update.message.text
    if text == "Si":
        update.message.reply_text(f"\U00002139 *Reminder Setup* \U00002139\n\nEnvia la informacion adicional \nque quieres aÃ±adir a tu reminder!", parse_mode="markdown")
        return OPT
    else:
        reply_keyboard = [["/start", "/list", "/time"]]
        chat_id = str(update.message["chat"]["id"])
        name, date, format_time, r_id = json_getter(chat_id)
        num = json_utc(chat_id)
        hour, minute, m = int(format_time.split(" ")[0].split(":")[0]), int(format_time.split(" ")[0].split(":")[1]), format_time.split(" ")[1]

        if "pm" in m:
            n_hour = hour + 12
        else:
            n_hour = hour

        seconds = datetime.timestamp(datetime.strptime(date, "%d/%m/%Y") + timedelta(hours=n_hour, minutes=minute)) - (datetime.timestamp(datetime.now()) + (num * 3600))
        print(seconds)
        if seconds < 0:
            context.bot.send_message(chat_id=chat_id, text=f"\U0000274C*Reminder Error*\U0000274C\n\nLa fecha o hora dada esta en el pasado.\nPor favor elige una hora y fecha correctas!", parse_mode="markdown", reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True, resize_keyboard=True))
            json_deleter(chat_id, r_id=r_id)
        else:
            context.bot.send_message(chat_id=chat_id,
                                     text=f"*\U0001F4CC Saved Reminder *\U0001F4CC\n\nNombre: {name}\nFecha: {date}\nHora: {hour}:{minute} {m}",
                                     parse_mode="markdown",
                                     reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True,
                                                                      resize_keyboard=True))
            context.job_queue.run_once(notification, seconds, context=[chat_id, name, date, format_time, chat_id, r_id], name=chat_id)
        return ConversationHandler.END

def opt_info(update, context):
    reply_keyboard = [["/start", "/list", "/time"]]
    information = update.message.text
    chat_id = str(update.message["chat"]["id"])
    json_editor(chat_id, "opt_inf", information)
    name, date, format_time, r_id = json_getter(chat_id)
    num = json_utc(chat_id)
    hour, minute, m = int(format_time.split(" ")[0].split(":")[0]), int(format_time.split(" ")[0].split(":")[1]), format_time.split(" ")[1]

    if "pm" in m:
        n_hour = hour + 12
    else:
        n_hour = hour

    seconds = datetime.timestamp(datetime.strptime(date, "%d/%m/%Y") + timedelta(hours=n_hour, minutes=minute)) - (datetime.timestamp(datetime.now()) + (num * 3600))
    print(seconds)
    if seconds < 0:
        context.bot.send_message(chat_id=chat_id, text=f"\U0000274C*Reminder Error*\U0000274C\n\nLa fecha o hora dada esta en el pasado.\nPor favor elige una hora y fecha correctas!", parse_mode="markdown", reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True, resize_keyboard=True))
        json_deleter(chat_id, r_id=r_id)
    else:
        context.bot.send_message(chat_id=chat_id,
                                 text=f"*\U0001F4CC Saved Reminder *\U0001F4CC\n\nNombre: {name}\nFecha: {date}\nHora: {hour}:{minute} {m}",
                                 parse_mode="markdown",
                                 reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True,
                                                                  resize_keyboard=True))
        context.job_queue.run_once(notification, seconds, context=[chat_id, name, date, format_time, chat_id, r_id, information], name=chat_id)
    return ConversationHandler.END

def cancel(update, context):
    username = str(update.message["chat"]["id"])
    logger.info("User %s canceled the reminder setup.", username)
    json_deleter(username, current=True)
    update.message.reply_text('\U0001F53A *Reminder Setup* \U0001F53A'
                              '\n\nHas cancelado el remind!', reply_markup=ReplyKeyboardRemove(), parse_mode="markdown")
    return ConversationHandler.END

# More commands...

def start(update, context):
    logger.info(f"El usuario {update.effective_user['username']} ha iniciado una conversaciÃ³n")
    name = update.effective_user['first_name']
    update.message.reply_text(f"Hola {name} soy un bot multi-funciÃ³n creado por Juan Salazar a.k.a regalk, puedes usar /help para ver mis comandos")

def echo(update, context):
    chat_id = update.message.chat_id
    bot = context.bot
    user_id = update.effective_user['id']
    args = context.args
    logger.info(f"El usuario {user_id} ha enviado un msg")
    text = update.message.text
    update.message.delete()
    context.bot.sendMessage(chat_id= chat_id,parse_mode= "MarkdownV2", text=f"\n_{args}_")

def help_menu(update, context):
    chat_id = update.message.chat_id
    bot = context.bot
    user_id = update.effective_user['id']
    args = context.args
    name = update.effective_user['first_name']
    logger.info(f"El usuario {user_id} ha puesto el comando help!")
    context.bot.sendMessage(chat_id=chat_id, parse_mode = "Markdown", text=f"Hola ðŸ‘‹, {name} Estos son mis comandos: \n*ðŸ’¼ Comandos Basicos*\n /help - Muestra este mensaje.\n /start - Da el mensaje de inicio.\n /echo - Repito lo que digas. \n /random - Te da un nÃºmero random. \n*ðŸ�… Comandos Para Administradores*\n /add - Agrega palabras a la lista negra. \n /remove - Elimina palabras de la lista negra.\n *ðŸ•” Comandos Remind* \n /remind - Pone un remind o alarma. \n /list - Muestra todos tus reminds pendientes.\n*ðŸ’¸ Crypto comandos*\n/crypto - pon el nombre de la moneda para obtener info.\n/clist - mira la lista de monedas para obtener info.")

# If user is ADMIN

def userisAdmin(chat_id, user_id, bot):
    try:
        groupadmins = bot.get_chat_administrators(chat_id)
        for admin in groupadmins:
            if admin.user.id == user_id:
                return True
        return False
    except Exception as e:
        print(e)

def add_profanity(update, context):
    bot = context.bot
    user_id = update.effective_user['id']
    args = context.args
    chat_id = update.message.chat_id
    name = update.effective_user['first_name']
    if userisAdmin(chat_id, user_id, bot) == True:
        if len(args) == 0:
            logger.info(f"{user_id}")
            bot.sendMessage(chat_id= chat_id, text=f"{name} por favor, dame la nueva palabra...")
        else:
            with open("data/badwords.txt", "a", encoding="utf-8") as f:
                f.write("".join([f"{w}\n" for w in args]))
            logger.info(f"{user_id} new bad word..")
            bot.sendMessage(chat_id= chat_id, text=f"{name} palabra agregada a la lista negra.")
    else:
        bot.sendMessage(chat_id= chat_id, text=f"lo siento {name} no tienes permisos para esto.")

def del_profanity(update, context):
    bot = context.bot
    user_id = update.effective_user['id']
    args = context.args
    chat_id = update.message.chat_id
    name = update.effective_user['first_name']
    if userisAdmin(chat_id, user_id, bot) == True:
        if len(args) == 0:
            logger.info(f"{user_id}")
            bot.sendMessage(chat_id= chat_id, text=f"{name} pon la palabra a retirar por favor...")
        else:
            with open("data/badwords.txt", "r", encoding="utf-8") as f:
                stored = [w.strip() for w in f.readlines()]

            with open("data/badwords.txt", "w", encoding="utf-8") as f:
                f.write("".join([f"{w}\n" for w in stored if w not in args]))

            profanity.load_censor_words_from_file("badwords.txt")

            logger.info(f"{user_id} palabra removida")
            bot.sendMessage(chat_id= chat_id, text=f"{name}, palabra removida.")
    else:
        bot.sendMessage(chat_id= chat_id, text=f"Lo siento {name} solo los admins pueden ejecutar este mensaje.")

def randoms(update, context):
    bot = context.bot
    user_id = update.effective_user['id']
    args = context.args
    chat_id = update.message.chat_id
    name = update.effective_user['first_name']
    number = random.randint(1, 10)
    bot.sendMessage(chat_id=chat_id, text=f"TÃº nÃºmero random es: {number}")

def crypto_price(update, context):
    bot = context.bot
    user_id = update.effective_user['id']
    args = context.args
    chat_id = update.message.chat_id
    name = update.effective_user['first_name']
    cg = CoinGeckoAPI()
    if args == ['btc']:
        data = cg.get_price(ids="bitcoin", vs_currencies='usd', include_24hr_change='true', include_last_updated_at='true')
        price = data["bitcoin"]["usd"]
        change = data["bitcoin"]['usd_24h_change']
        last_u = data["bitcoin"]['last_updated_at']

        context.bot.sendMessage(chat_id=chat_id, parse_mode="markdown", text=f"Hola {name}ðŸ‘‹, estos son los datos de la moneda que pediste:\n*Nombre* = Bitcoin.\n*Symbol* = BTC.\n*Precio actual* = $ {price}\n*Cambio en las 24h* = {change}\n*Ultima actualizaciÃ³n* = {last_u}")
    elif args == ['lit']:
        data = cg.get_price(ids="litecoin", vs_currencies='usd', include_24hr_change='true', include_last_updated_at='true')
        price = data["litecoin"]["usd"]
        change = data["litecoin"]['usd_24h_change']
        last_u = data["litecoin"]['last_updated_at']

        context.bot.sendMessage(chat_id=chat_id, parse_mode="markdown", text=f"Hola {name}ðŸ‘‹, estos son los datos de la moneda que pediste:\n*Nombre* = Litecoin.\n*Symbol* = Å�.\n*Precio actual* = $ {price}\n*Cambio en las 24h* = {change}\n*Ultima actualizaciÃ³n* = {last_u}")

    elif args == ['eth']:
        data = cg.get_price(ids="ethereum", vs_currencies='usd', include_24hr_change='true', include_last_updated_at='true')
        price = data["ethereum"]["usd"]
        change = data["ethereum"]['usd_24h_change']
        last_u = data["ethereum"]['last_updated_at']

        context.bot.sendMessage(chat_id=chat_id, parse_mode="markdown", text=f"Hola {name}ðŸ‘‹, estos son los datos de la moneda que pediste:\n*Nombre* = Ethereum.\n*Symbol* = ETH.\n*Precio actual* = $ {price}\n*Cambio en las 24h* = {change}\n*Ultima actualizaciÃ³n* = {last_u}")

def crypto_l(update, context):
    bot = context.bot
    user_id = update.effective_user['id']
    args = context.args
    chat_id = update.message.chat_id
    name = update.effective_user['first_name']

    context.bot.sendMessage(chat_id=chat_id, parse_mode="markdown", text=f"Hola {name}ðŸ‘‹, esta es la lista de monedas que tengo info:\n*btc* = Bitcoin.\n*lit* = Litecoin.\n*eth* = Ethereum.")

def get_weather(update, context):
    place_arg = ''.join(context.args)
    chat_id = update.message.chat_id
    weather = WeatherScraper(place_arg).get_tempetarure_and_weather()

    context.bot.sendMessage(chat_id= chat_id ,text=f'{weather}')

def wiki_search(update, context):
    bot = context.bot
    user_id = update.effective_user['id']
    args = ''.join(context.args)
    args = args.strip()
    chat_id = update.message.chat_id
    name = update.effective_user['first_name']

    '''the syntax to set a lang is lang:page, for example en:Linux
        so the first two letters will always be the lang, the third letter will always be a colon and the rest of the string
        will be the search
    '''
    if ':' in args: #So we check if the user's arg contains a colon
        lang_arg = args[:2] #The first two letters en, ru, fr.....
        name_arg = args[3:] #The rest of the sring
        browserObj = WikiBrowser(name_arg, lang_arg)
        context.bot.sendMessage(chat_id= chat_id, text=f'{browserObj.obtain_summary()}')

    else: #If user doesn't want to set a lang the string wont contain a colon.
        name_arg = ''.join(context.args)
        browserObj = WikiBrowser(name_arg)
        context.bot.sendMessage(chat_id= chat_id, text=f'{browserObj.obtain_summary()}')

def wiki_lang_list(update, context):
    bot = context.bot
    user_id = update.effective_user['id']
    args = context.args
    chat_id = update.message.chat_id
    name = update.effective_user['first_name']
    lang_list = ('-en', '-es', '-ru', '-fr', '-ar') #It's acctually a tuple but idc
    name = update.effective_user['first_name']
    nl = '\n'
    context.bot.sendMessage(chat_id= chat_id, text=f"Hola! {name}ðŸ‘‹ esta es la lista de lenguajes permitidos:\n{nl.join(lang_list)}")

'''
please ignore this
def get_news(update, context):
    bot = context.bot
    chat_id = update.message.chat_id
    tittles, links = tech_news_scraper.scrape_news_and_links()
    context.bot.sendMessage(chat_id= chat_id, text=f'Breaking Tech News!\n{tittles[0]} : {links[0]}\n{tittles[1]} : {links[1]}\n{tittles[2]} : {links[2]} ') #Notice that the tittles and links lenght is 3, i defined it in the scraper.
'''

def message(update, context):
    text = update.message.text.lower()
    word = text.split()
    profanity.load_censor_words_from_file("badwords.txt")
    if profanity.contains_profanity(text) == True:
        user_id = update.effective_user['id']
        chat_id = update.message.chat_id
        name = update.effective_user['first_name']
        logger.info(f"El usuario {user_id} ha dicho una mala palabra")
        update.message.delete()
        context.bot.sendMessage(chat_id= chat_id, text=f"El mensaje de {name} fue eliminado por contener malas palabras...")
        context.bot.sendMessage(chat_id= user_id, text=f"Por favor mejora tu vocabulario o seras expulsado")
    elif word == ['hola']:
        name = update.effective_user['first_name']
        update.message.reply_text(f"Hola {name}, Â¿Como estas?")

def main():
    updater = Updater("TOKEN", use_context=True)

    dp = updater.dispatcher

    all_reminder_handler = CommandHandler("list", all_reminder)
    starter = CommandHandler("start", start)
    add = CommandHandler("add", add_profanity)
    remove = CommandHandler("remove", del_profanity)
    randomer = CommandHandler("random", randoms)
    crypto = CommandHandler("crypto", crypto_price)
    crypto_list = CommandHandler("clist", crypto_l)
    weather_command = CommandHandler('weather', get_weather)
    wiki_command = CommandHandler('wiki', wiki_search)
    wiki_list = CommandHandler('wklist', wiki_lang_list)
    news_command = CommandHandler('news', get_news )

    echo_system = CommandHandler("echo", echo)
    help_m = CommandHandler("help", help_menu)

    badwords = MessageHandler(Filters.text, message)

    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('remind', remind)],
        states={
            NAME: [MessageHandler(Filters.text, name)],
            DATE_Q: [CallbackQueryHandler(inline_handler)],
            TIME_Q: [CallbackQueryHandler(inline_handler2)],
            INFO: [MessageHandler(Filters.text, info)],
            OPT: [MessageHandler(Filters.text, opt_info)],
        },
        fallbacks=[CommandHandler('cancel', cancel)],
    )

    conv_handler_utc = ConversationHandler(
        entry_points=[CommandHandler("time", utc_time)],
        states={
            UTC_1: [CallbackQueryHandler(utc_time_selector)]
        },
        fallbacks=[CommandHandler('cancel', cancel)]
    )

    dp.add_handler(all_reminder_handler)
    dp.add_handler(starter)
    dp.add_handler(add)
    dp.add_handler(remove)
    dp.add_handler(conv_handler)
    dp.add_handler(echo_system)
    dp.add_handler(randomer)
    dp.add_handler(help_m)
    dp.add_handler(crypto)
    dp.add_handler(crypto_list)
    dp.add_handler(weather_command)
    dp.add_handler(wiki_command)
    dp.add_handler(wiki_list)
    dp.add_handler(news_command)
    dp.add_handler(conv_handler_utc)

    dp.add_handler(badwords)

    updater.start_polling()
    print("Bot ready")
    updater.idle()

if __name__ == '__main__':
    main()


# Copyright (c) 2012, Adam J. Rossi. All rights reserved. See README for licensing details.

import struct, array, os
from KeypointDescriptorFile import KeypointDescriptorFile
from KeypointDescriptor import KeypointDescriptor

class DaisyOutput:

    # data types
    DATA_TYPE_CHAR = 0
    DATA_TYPE_FLOAT = 1
    DATA_TYPE_DOUBLE = 2
    DATA_TYPE_INT = 3

    def __init__(self, filePath, defaultValue=0.0):
        self._defaultValue = defaultValue
        self._f = None
        if (os.path.isfile(filePath)):
            self._filePath = filePath
            self._f = open(self._filePath,"rb")
            self._readHeader()
            self._determineDataTypeFormatChar()

    def __del__(self):
        self._f.close()

    def getDataType(self):      return self._dataType
    def getHeight(self):        return self._height
    def getWidth(self):         return self._width
    def getDataLength(self):    return self._dataLength

    def _readHeader(self):
        SIZE_INT = 4
        self._dataType, self._height, self._width, self._dataLength = struct.unpack("@iiii", self._f.read(4*SIZE_INT))

    def _determineDataTypeFormatChar(self):
        # determine data type format
        if   (self._dataType==DaisyOutput.DATA_TYPE_CHAR):   self._formatChar = "b"
        elif (self._dataType==DaisyOutput.DATA_TYPE_FLOAT):  self._formatChar = "f"
        elif (self._dataType==DaisyOutput.DATA_TYPE_DOUBLE): self._formatChar = "d"
        elif (self._dataType==DaisyOutput.DATA_TYPE_INT):    self._formatChar = "i"
        else: raise Exception("Unknown data type: %d" % dataType)

    def readNextElement(self):
        if (self._f):
            vector = array.array(self._formatChar)
            vector.fromfile(self._f, self._dataLength)
            return vector.tolist()
        else:
            return [self._defaultValue]

class DaisyKeypointDescriptorFile(KeypointDescriptorFile):

    def __init__(self, keypointDescriptorFile,
                 keypointDescriptorFileScales="",
                 keypointDescriptorFileOrientation="",
                 siftNormalize=False,
                 parse=True):

        KeypointDescriptorFile.__init__(self, keypointDescriptorFile)
        self._keypointDescriptorFileScales = keypointDescriptorFileScales
        self._keypointDescriptorFileOrientation = keypointDescriptorFileOrientation
        self._siftNormalize = siftNormalize
        if (parse): self._parse(keypointDescriptorFileScales, keypointDescriptorFileOrientation, siftNormalize)

    def Parse(self):
        self._parse(self._keypointDescriptorFileScales,
                    self._keypointDescriptorFileOrientation,
                    self._siftNormalize)

    def _parse(self, keypointDescriptorFileScales, keypointDescriptorFileOrientation, siftNormalize):

        # descriptors file
        descriptors = DaisyOutput(self._keypointDescriptorFile)
        self._keypointDescriptorLength = descriptors.getDataLength()

        # scales file
        scales = DaisyOutput(keypointDescriptorFileScales, 1.0)
        if (os.path.isfile(keypointDescriptorFileScales)):
            if (scales.getHeight()!=descriptors.getHeight() or scales.getWidth()!=descriptors.getWidth()):
                raise Exception("Scales header is different from keypoint descriptor header")

        # orientations file
        orientations = DaisyOutput(keypointDescriptorFileOrientation, 0.0)
        if (os.path.isfile(keypointDescriptorFileOrientation)):
            if (orientations.getHeight()!=descriptors.getHeight() or orientations.getWidth()!=descriptors.getWidth()):
                raise Exception("Orientation header is different from keypoint descriptor header")

        # read descriptors
        if (siftNormalize):
            maxVal = 0.154
            scaleFactor = 255.0/maxVal

            for row in range(descriptors.getHeight()):
                for column in range(descriptors.getWidth()):
                    descriptor = [int(round(x*scaleFactor)) for x in descriptors.readNextElement()]
                    scale = scales.readNextElement()[0]
                    orientation = orientations.readNextElement()[0]
                    kpd = KeypointDescriptor(float(row), float(column),
                                             scale, orientation, descriptor)
                    self._keypointDescriptors.append(kpd)
        else:
            for row in range(descriptors.getHeight()):
                for column in range(descriptors.getWidth()):
                    descriptor = descriptors.readNextElement()
                    scale = scales.readNextElement()[0]
                    orientation = orientations.readNextElement()[0]
                    kpd = KeypointDescriptor(float(row), float(column),
                                             scale, orientation, descriptor)
                    self._keypointDescriptors.append(kpd)

        del descriptors
        del scales
        del orientations


# -*- coding: utf-8 -*-
#%% Sample 1-2
#%% 画像データの表現
# 基本操作
#
# 画像処理特論
#
# 村松 正吾
#
# 動作確認: Python 3.7, PyTorch 1.8
#%% Digital image representation
# Basic operations
#
# Advanced Topics in Image Processing
#
# Shogo MURAMATSU
#
# Verified: Python 3.7, PyTorch 1.8
import torch

#%%  行列 $\mathbf{A}$の定義
# (Definition of matrix $\mathbf{A}$)
#
# $$\mathbf{A}=\left(\begin{array}{ll}1 & 2 \\ 3 & 4 \\ 5 & 6\end{array}\right)$$

A = torch.tensor([ \
    [ 1, 2 ],
    [ 3, 4 ],
    [ 5, 6 ] ])
A
#%% 行列 $\mathbf{B}$の定義
# (Definition of matrix $\mathbf{B}$ )
#
# $$\mathbf{B}=\left(\begin{array}{lll}1 & 2 & 3 \\ 4 & 5 & 6\end{array}\right)$$

B = torch.tensor([ \
    [ 1, 2, 3 ],
    [ 4, 5, 6 ] ])
B
#%% 行列積の計算
# (Matrix product)
#
# $$\mathbf{C}=\mathbf{A B}$$

C = A @ B
C
#%%
# © Copyright, Shogo MURAMATSU, All rights reserved.


from typing import Union
from fastapi import FastAPI, Path, Query

app = FastAPI()

@app.get("/items/{item_id}")
async def read_items(
    item_id: int = Path(title = "The ID of the item to get"),
    q: Union[str, None] = Query(default=None, alias="item-query"),):

    results = {"item_id": item_id}
    if q:
        results.update({'q': q})
    return results

@app.get("/books/{book_id}")
async def read_books(*, book_id: int = Path(title = "The id of the book to get", ge=1), q: int):
    # * : kwargs
    results = {"book_id": book_id}
    if q:
        results.update({"q": q})
    return results


from aiven.client.argx import CommandLineTool
from aiven.client.cliarg import arg

def test_user_config_json_error_json():
    """Test that @arg.user_config_json causes
    CommandLineTool.run() to exit cleanly with return value 1
    if JSON is incorrect
    """

    class T(CommandLineTool):
        """Test class"""

        @arg.user_config_json()
        @arg()
        def t(self):
            """t"""

    error_json_arg = ["t", "--user-config-json", "foo"]
    test_class = T("avn")
    ret = test_class.run(args=error_json_arg)
    assert ret == 1

def test_user_config_json_error_conflict():
    """Test that @arg.user_config_json causes
    CommandLineTool.run() to exit cleanly with return value 1
    if both user_config (-c) and --user-config-json parameters
    are given
    """

    class T(CommandLineTool):
        """Test class"""

        @arg.user_config
        @arg.user_config_json()
        @arg()
        def t(self):
            """t"""

    error_conflict_arg = ["t", "-c", "userconfkey=val", "--user-config-json", '{"foo":"bar"}']
    test_class = T("avn")
    ret = test_class.run(args=error_conflict_arg)
    assert ret == 1

def test_user_config_json_success():
    """Success scenario
    """

    class T(CommandLineTool):
        """Test class"""

        @arg.user_config_json()
        @arg()
        def t(self):
            """t"""

    valid_json_arg = ["t", "--user-config-json", '{"foo":"bar"}']
    test_class = T("avn")
    ret = test_class.run(args=valid_json_arg)
    assert ret is None  # Should run() return 0 actually?
    assert test_class.args.user_config_json == {"foo": "bar"}

def test_user_config_success():
    """Test that user config parameter -c works and not cause conflict with
    --user_config_json
    """

    class T(CommandLineTool):
        """Test class"""

        @arg.user_config
        @arg.user_config_json()
        @arg()
        def t(self):
            """t"""

    user_config_arg = ["t", "-c", "userconfkey=val"]
    test_class = T("avn")
    ret = test_class.run(args=user_config_arg)
    assert ret is None


"""
Django settings for fondoscomparados project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DEBUG = os.environ.get('DEBUG') == 'True'
import dj_database_url
DATABASES = {}
DATABASES['default'] =  dj_database_url.config()
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

ALLOWED_HOSTS = ['*']

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'fondoscomparados.apps.viviendas',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR + '/fondoscomparados/templates',
        ],
    }
]

PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_PATH, 'static'),
)
ROOT_URLCONF = 'fondoscomparados.urls'

WSGI_APPLICATION = 'fondoscomparados.wsgi.application'

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'es-AR'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

USE_THOUSAND_SEPARATOR = True
THOUSAND_SEPARATOR = '.'
NUMBER_GROUPING = 3
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'


from src.domain.domain import *
import copy

class MovieServiceException(Exception):
    def __init__(self, message):
        """
        initialize an error
        :param message: the massage that the error provides
        """
        self.__message = message

    @property
    def message(self):
        return self.__message

class MovieService():
    def __init__(self, repository):
        """
        initialize the movie service
        :param repository: movie_repository
        """
        self._repo = repository
        self.operations = 0
        self._archive = []
        self._archive.append(copy.deepcopy(self._repo.get_all_movie()))

    def add_movie(self, movie: Movie):
        """
        adds a movie to the movie class
        :param movie: an object from class movie
        :return:
        """

        self._repo.ADD_movie(movie)
        self._archive.append(copy.deepcopy(self._repo.get_all_movie()))
        self.operations += 1

    def remove_movie(self, id):
        """
        Remove a movie by id
        :param id: the id of the movie
        :return: All movies
        """
        i = 0
        arr = self.return_all_movie()
        while i < len(arr):
            if arr[i].movie_id == id:
                self._repo.delete_movie(i)
                i -= 1
            i += 1
        self.operations += 1
        self._archive.append(copy.deepcopy(self._repo.get_all_movie()))
        return self._repo.get_all_movie()

    def return_all_movie(self):
        """
        Returns all  movies
        :return: all movies
        """
        return self._repo.get_all_movie()

    def display_movie(self, movie: Movie):
        """
        Displays a movie
        :param movie: a movie
        :return: a movie in string format
        """
        return str(movie) + "\n"

    def update_movie(self, id, title, description, genre):
        """
        It updates a movie
        :param id: new id
        :param title: new title
        :param description: new description
        :param genre: new genre
        """
        i = 0
        arr = self.return_all_movie()
        movie = Movie(id, title, description, genre)
        while i < len(arr):
            if arr[i].movie_id == id:
                self._repo.change_movie(i, movie)

            i += 1
        self.operations += 1
        self._archive.append(copy.deepcopy(self._repo.get_all_movie()))

    def get_by_id(self, id):
        for i in self.return_all_movie():
            if i.movie_id == id:
                return i
        return None
    def partial_name(self,name:str):
        movies = []
        for i in self.return_all_movie():
            if name.casefold() in i.title.casefold():
                movies.append(i)
        return movies

    def partial_id(self,id):
        try:
            id=int(id)
        except:
            raise MovieServiceException("id not an integer")
        movies=[]
        for i in self.return_all_movie():
            if str(id) in str(i.movie_id):
                movies.append(i)
        return movies

    def partial_description(self,description:str):
        movies = []
        for i in self.return_all_movie():
            if description.casefold() in i.description.casefold():
                movies.append(i)
        return movies

    def partial_genre(self,genre:str):
        movies = []
        for i in self.return_all_movie():
            if genre.casefold() in i.genre.casefold():
                movies.append(i)
        return movies


res = input()

op = '([{'
cl = ')]}'

stack = []

fall = 0

for i in range(len(res)):
    if( res[i] in op ):
        stack.append(res[i])
        fall = i+1
    else:
        t = cl.index(res[i])
        stop = op[t]
        if(stack and stack[-1]!=stop):
            fall = i
            break
        elif(stack and stack[-1]==stop):
            fall = i+1
            stack.pop()
        else:
            stack.append(res[i])
            fall = i
            break

if(not stack):
    print(0)
else:
    print(fall+1)


# This file convers an art URL into a texture, normal map, and depth map in public/art/<name>
import os
import sys
import subprocess
import json
import re

dezoomify_regex = re.compile(r"\s*(\d)\.\s.*?(\d+).*?(\d+)\spixels.*?$")
dezoomify_prompt = "Which level do you want to download?"
dezoomify_max_area = 10000 * 10000

def download_with_dezoomify(url, path_to_store):
    process = subprocess.Popen(["dezoomify-rs", url, path_to_store],
                               stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    best_index = 0
    res_x = 0
    res_y = 0
    for line in process.stdout:
        decoded = line.decode("utf-8")

        # match to regex
        match = dezoomify_regex.match(decoded)
        if match:
            index = int(match.group(1))
            x = int(match.group(2))
            y = int(match.group(3))
            dezoomify_max_area = 10000 * 10000
            print(f"index: {index}, x: {x}, y: {y}")
            if x * y < dezoomify_max_area and x * y > res_x * res_y:
                best_index = index
                res_x = x
                res_y = y

        if dezoomify_prompt in decoded:
            break

    x = best_index

    print(f"best index: {x}")
    process.stdin.write(str(x).encode("utf-8"))
    process.stdin.write("\n".encode("utf-8"))
    process.stdin.close()
    process.wait()

if __name__ == "__main__":
    if len(sys.argv) < 3:
        # can also do --low-res AFTER inputs
        print("Usage: python3 art.py <url-or-local-path> <short-hyphenated-name>")
        exit()

    # setup variables
    url = sys.argv[1]
    name = sys.argv[2]
    low_res = len(sys.argv) > 3 and sys.argv[3] == "--low-res"
    art_dir = f"public/art/" if not low_res else f"public/art_lowres/"
    dest_dir = f"public/art/{name}" if not low_res else f"public/art_lowres/{name}"
    dest_dir_without_public = f"art/{name}" if not low_res else f"art_lowres/{name}"

    print(f"URL: {url}")
    print(f"Dest: {dest_dir}")
    print(f"ID: {name}")
    print(f"Res: {'low' if low_res else 'high'}")

    res = [-1, -1]
    dims = [1, 1]

    temp_art_file = f'temp/texture.png'

    # clean and rebuild temp dir
    os.system("rm -r temp")
    os.system("mkdir temp")

    if low_res:
        # (0) instead of scraping for dimensions, first look for existing highres file
        if os.path.exists(f"public/art/{name}/texture.json"):
            with open(f"public/art/{name}/texture.json", "r") as f:
                data = json.load(f)
                dims = data["dims"]

    if not url.startswith("http"):
        # (1+2) copy existing local file to temp/
        os.system(f"cp {url} {temp_art_file}")
    else:
        # (1) run scrape art to get dimensions and the url of an image
        output = subprocess.check_output(
            ["deno", "run", "-A", "src_imageprocessing/scrape_art.ts", sys.argv[1]])
        json_output = json.loads(output)

        print(json_output)

        url = json_output["url"]
        res = [-1, -1] if "res" not in json_output else json_output["res"]
        dims = json_output["dims"]

        # (2) download the image
        if "artsandculture.google.com/" in url:
            # for google, use dezoomify
            download_with_dezoomify(url, temp_art_file)
        else:
            # wikiart/wikipedia/etc.
            subprocess.run(["scripts/download.sh", url, temp_art_file])

    # (3) run process_art.py to create normals, depth, and json
    subprocess.run(
        ["python3", "src_imageprocessing/process_art.py", temp_art_file, "temp/", "false" if low_res else "false", "false" if low_res else "true"])

    # (4) move everything to public/art/<name>
    # os.system(f"rm {temp_art_file}")
    os.system(f"mkdir -p {dest_dir}")
    os.system(f"mv temp/* {dest_dir}")

    # (5) update json file with new urls
    json_path = f"{dest_dir}/texture.json"
    data = {}
    with open(json_path, 'r') as f:
        data = json.load(f)
        if dims is not None:
            data["dims"] = dims
        else:
            data["dims"] = [1, 1]

        data['id'] = name
        data['name'] = name
        data["og_url"] = url
        data['url'] = f'{dest_dir_without_public}/texture{{SLICE}}.jpg'

        if not low_res:
            data['normal_map_url'] = f'{dest_dir_without_public}/texture@normal{{SLICE}}.jpg'
            data['depth_map_url'] = f'{dest_dir_without_public}/texture@depth{{SLICE}}.jpg'

        # overwrite res
        res = data["res"]

        # write back to file
        with open(json_path, "w") as f:
            json.dump(data, f, indent=4)

    # (6) update index.json - directory of all artwork
    index_path = f"{art_dir}/index.json"
    if not os.path.exists(index_path):
        with open(index_path, "w") as f:
            json.dump([], f)
    with open(index_path, "r") as f:
        index = json.load(f)
        # remove all instances of name
        index = [x for x in index if x["name"] != name]
        # add to front
        index.insert(0, data)

        with open(index_path, "w") as f:
            json.dump(index, f, indent=4)

    # done! capture total size of directory
    size = subprocess.check_output(["du", "-sh", f"{art_dir}{name}"])
    size_str = size.decode("utf-8").split("\t")[0]
    print(f'Total size: {size_str}')


from datetime import datetime
from typing import Callable

import backoff
import psycopg2
from psycopg2.extensions import connection
from psycopg2.extras import DictCursor, DictRow

from settings import PostgresDsl, BATCH_SIZE, backoff_config
from state import State

class PostgresExtractor:
    def __init__(self, dsl: PostgresDsl, redis_storage: State):
        self.dsl = dsl
        self.connection = None
        self.redis_storage = redis_storage

    def check_connection_exists(self) -> None:
        """Создается новое соединение, если его нет или оно закрыто"""
        if self.connection is None or self.connection.closed:
            self.connection = self.create_connection()

    @backoff.on_exception(backoff.expo, Exception, max_tries=backoff_config.backoff_max_tries)
    def create_connection(self) -> connection:
        """Создается новое соединение"""
        self.connection = psycopg2.connect(**self.dsl.dict(), cursor_factory=DictCursor)
        return self.connection

    @backoff.on_exception(backoff.expo, Exception, max_tries=backoff_config.backoff_max_tries)
    def extract_data_from_db(self, index_name: str, query_function: Callable) -> list[DictRow]:
        """
        Делается запрос в базу на получение нужной сущности,
        где поле modified > modified, которое хранится в redis (modified последней записи из запроса)
        Как только запрос не вернул результатов в redis попадает datetime.min, что позволяет циклу в etl пройтись по
        записям заново
        """
        self.check_connection_exists()
        last_modified = self.redis_storage.get_key(f'{index_name}_last_modified')
        if last_modified is None:
            self.redis_storage.set_key(f'{index_name}_last_modified', datetime.min)
            last_modified = datetime.min

        query, last_modified_index = query_function(BATCH_SIZE, last_modified)

        with self.connection.cursor() as cursor:
            cursor.execute(query)
            data = cursor.fetchall()
            try:
                last_modified = data[-1][last_modified_index]
            except IndexError:
                last_modified = datetime.min
            self.redis_storage.set_key(f'{index_name}_last_modified', last_modified)

        return data


n = list(map(int, input()))

for i in range(len(n)-1):
    idx = i
    for j in range(i+1, len(n)):
        if n[idx] < n[j]:
            n[idx], n[j] = n[j], n[idx]

ret = ""

for i in n:
    ret += str(i)

print(ret)


#
# Client connect to an Electrum server.
#

# Runtime check for optional modules
from importlib import util as importutil
import json, warnings, asyncio, ssl
from .protocol import StratumProtocol

# Check if aiosocks is present, and load it if it is.
if importutil.find_spec("aiosocks") is not None:
    import aiosocks
    have_aiosocks = True
else:
    have_aiosocks = False

from collections import defaultdict
from .exc import ElectrumErrorResponse
import logging

logger = logging.getLogger(__name__)

class StratumClient:

    def __init__(self, loop=None):
        '''
            Setup state needed to handle req/resp from a single Stratum server.
            Requires a transport (TransportABC) object to do the communication.
        '''
        self.protocol = None

        self.next_id = 1
        self.inflight = {}
        self.subscriptions = defaultdict(list)

        self.actual_connection = {}

        self.ka_task = None

        self.loop = loop or asyncio.get_event_loop()

        self.reconnect = None       # call connect() first

        # next step: call connect()

    def connection_lost(self, protocol):
        # Ignore connection_lost for old connections
        if protocol is not self.protocol:
            return

        self.protocol = None
        logger.warn("Electrum server connection lost")

        # cleanup keep alive task
        if self.ka_task:
            self.ka_task.cancel()
            self.ka_task = None

    def close(self):
        if self.protocol:
            self.protocol.close()
            self.protocol = None
        if self.ka_task:
            self.ka_task.cancel()
            self.ka_task = None

    async def connect(self, server_info, proto_code=None, *,
                            use_tor=False, disable_cert_verify=False,
                            proxy=None, short_term=False):
        '''
            Start connection process.
            Destination must be specified in a ServerInfo() record (first arg).
        '''
        self.server_info = server_info
        if not proto_code:
             proto_code,*_ = server_info.protocols
        self.proto_code = proto_code

        logger.debug("Connecting to: %r" % server_info)

        if proto_code == 'g':       # websocket
            # to do this, we'll need a websockets implementation that
            # operates more like a asyncio.Transport
            # maybe: `asyncws` or `aiohttp`
            raise NotImplementedError('sorry no WebSocket transport yet')

        hostname, port, use_ssl = server_info.get_port(proto_code)

        if use_tor:
            if have_aiosocks:
                # Connect via Tor proxy proxy, assumed to be on localhost:9050
                # unless a tuple is given with another host/port combo.
                try:
                    socks_host, socks_port = use_tor
                except TypeError:
                    socks_host, socks_port = 'localhost', 9050

                # basically no-one has .onion SSL certificates, and
                # pointless anyway.
                disable_cert_verify = True

                assert not proxy, "Sorry not yet supporting proxy->tor->dest"

                logger.debug(" .. using TOR")

                proxy = aiosocks.Socks5Addr(socks_host, int(socks_port))
            else:
                logger.debug("Error: want to use tor, but no aiosocks module.")

        if use_ssl == True and disable_cert_verify:
            # Create a more liberal SSL context that won't
            # object to self-signed certicates. This is
            # very bad on public Internet, but probably ok
            # over Tor
            use_ssl = ssl.create_default_context()
            use_ssl.check_hostname = False
            use_ssl.verify_mode = ssl.CERT_NONE

            logger.debug(" .. SSL cert check disabled")

        async def _reconnect():
            if self.protocol: return        # race/duplicate work

            if proxy:
                if have_aiosocks:
                    transport, protocol = await aiosocks.create_connection(
                                            StratumProtocol, proxy=proxy,
                                            proxy_auth=None,
                                            remote_resolve=True, ssl=use_ssl,
                                            dst=(hostname, port))
                else:
                    logger.debug("Error: want to use proxy, but no aiosocks module.")
            else:
                transport, protocol = await self.loop.create_connection(
                                                        StratumProtocol, host=hostname,
                                                        port=port, ssl=use_ssl)

            self.protocol = protocol
            protocol.client = self

            # capture actual values used
            self.actual_connection = dict(hostname=hostname, port=int(port),
                                            ssl=bool(use_ssl), tor=bool(proxy))
            self.actual_connection['ip_addr'] = transport.get_extra_info('peername',
                                                        default=['unknown'])[0]

            if not short_term:
                self.ka_task = self.loop.create_task(self._keepalive())

            logger.debug("Connected to: %r" % server_info)

        # close whatever we had
        if self.protocol:
            logger.warn("connect called when already connected, closing previous connection")
            self.protocol.close()
            self.protocol = None

        self.reconnect = _reconnect
        await self.reconnect()

    async def _keepalive(self):
        '''
            Keep our connect to server alive forever, with some
            pointless traffic.
        '''
        while self.protocol:
            vers = await self.RPC('server.version')
            logger.debug("Server version: " + repr(vers))

            # Five minutes isn't really enough anymore; looks like
            # servers are killing 2-minute old idle connections now.
            # But decreasing interval this seems rude.
            await asyncio.sleep(600)

    def _send_request(self, method, params=[], is_subscribe = False):
        '''
            Send a new request to the server. Serialized the JSON and
            tracks id numbers and optional callbacks.
        '''
        # pick a new ID
        self.next_id += 1
        req_id = self.next_id

        # serialize as JSON
        msg = {'id': req_id, 'method': method, 'params': params}

        # subscriptions are a Q, normal requests are a future
        if is_subscribe:
            waitQ = asyncio.Queue()
            self.subscriptions[method].append(waitQ)

        fut = asyncio.Future(loop=self.loop)

        self.inflight[req_id] = (msg, fut)

        # send it via the transport, which serializes it
        if not self.protocol:
            logger.debug("Need to reconnect to server")

            async def connect_first():
                await self.reconnect()
                self.protocol.send_data(msg)

            self.loop.create_task(connect_first())
        else:
            # typical case, send request immediatedly, response is a future
            self.protocol.send_data(msg)

        return fut if not is_subscribe else (fut, waitQ)

    def _got_response(self, msg):
        '''
            Decode and dispatch responses from the server.

            Has already been unframed and deserialized into an object.
        '''

        #logger.debug("MSG: %r" % msg)

        resp_id = msg.get('id', None)

        if resp_id is None:
            # subscription traffic comes with method set, but no req id.
            method = msg.get('method', None)
            if not method:
                logger.error("Incoming server message had no ID nor method in it", msg)
                return

            # not obvious, but result is on params, not result, for subscriptions
            result = msg.get('params', None)

            logger.debug("Traffic on subscription: %s" % method)

            subs = self.subscriptions.get(method)
            for q in subs:
                self.loop.create_task(q.put(result))

            return

        assert 'method' not in msg
        result = msg.get('result')

        # fetch and forget about the request
        inf = self.inflight.pop(resp_id)
        if not inf:
            logger.error("Incoming server message had unknown ID in it: %s" % resp_id)
            return

        # it's a future which is done now
        req, rv = inf

        if 'error' in msg:
            err = msg['error']

            logger.info("Error response: '%s'" % err)
            rv.set_exception(ElectrumErrorResponse(err, req))

        else:
            rv.set_result(result)

    def RPC(self, method, *params):
        '''
            Perform a remote command.

            Expects a method name, which look like:

                blockchain.address.get_balance

            .. and sometimes take arguments, all of which are positional.

            Returns a future which will you should await for
            the result from the server. Failures are returned as exceptions.
        '''
        assert '.' in method
        #assert not method.endswith('subscribe')
        return self._send_request(method, params)

    def subscribe(self, method, *params):
        '''
            Perform a remote command which will stream events/data to us.

            Expects a method name, which look like:
                server.peers.subscribe
            .. and sometimes take arguments, all of which are positional.

            Returns a tuple: (Future, asyncio.Queue).
            The future will have the result of the initial
            call, and the queue will receive additional
            responses as they happen.
        '''
        assert '.' in method
        assert method.endswith('subscribe')
        return self._send_request(method, params, is_subscribe=True)

if __name__ == '__main__':
    from transport import SocketTransport
    from svr_info import KnownServers, ServerInfo

    logging.basicConfig(format="%(asctime)-11s %(message)s", datefmt="[%d/%m/%Y-%H:%M:%S]")

    loop = asyncio.get_event_loop()
    loop.set_debug(True)

    proto_code = 's'

    if 0:
        ks = KnownServers()
        ks.from_json('servers.json')
        which = ks.select(proto_code, is_onion=True, min_prune=1000)[0]
    else:
        which = ServerInfo({
            "seen_at": 1465686119.022801,
            "ports": "t s",
            "nickname": "dunp",
            "pruning_limit": 10000,
            "version": "1.0",
            "hostname": "erbium1.sytes.net" })

    c = StratumClient(loop=loop)

    loop.run_until_complete(c.connect(which, proto_code, disable_cert_verify=True, use_tor=True))

    rv = loop.run_until_complete(c.RPC('server.peers.subscribe'))
    print("DONE!: this server has %d peers" % len(rv))
    loop.close()

    #c.blockchain.address.get_balance(23)


#!/usr/bin/env python

# Script:  logplot.py
# Purpose: use GnuPlot to plot two columns from a LAMMPS log file
# Syntax:  logplot.py log.lammps X Y
#          log.lammps = LAMMPS log file
#          X,Y = plot Y versus X where X,Y are thermo keywords
#          once plot appears, you are in Python interpreter, type C-D to exit
# Author:  Steve Plimpton (Sandia), sjplimp at gmail.com

import sys,os,code
path = os.environ["LAMMPS_PYTHON_TOOLS"]
sys.path.insert(1,path)
from log import log
from gnu import gnu

if len(sys.argv) != 4:
  raise Exception("Syntax: logplot.py log.lammps X Y")

logfile = sys.argv[1]
xlabel = sys.argv[2]
ylabel = sys.argv[3]

lg = log(logfile)
x,y = lg.get(xlabel,ylabel)
g = gnu()
g.plot(x,y)
code.interact()


# Desafio 105 - Analisando e gerando dicionarios

"""
Faça um programa que tenha uma função notas() que pode receber várias notas de alunos e vai retornar
um dicionário
com as seguintes informações:
- Quantidade de notas
- A maior nota
- A menor nota
- A média da turma
- A situação (opcional)
- Adicione também as docstrings da função
"""

def notas(* num, sit=False):
    """
    Função notas utilizada para retornar um dicionario com informações da turma
    :param num: parametro infinito, recebe varios números ou recebe apenas 1
    :param sit: parametro opcional, se sit=True -> escreve a situação da turma
    :return: retorna o dicionario -> armazenado varias informações sobre a situação da turma
    """
    dicionario = dict()
    dicionario['total'] = len(num)
    dicionario['maior'] = max(num)
    dicionario['menor'] = min(num)
    dicionario['média'] = sum(num) / len(num)
    if sit:
        if dicionario['média'] <= 5:
            print('Situação: Recuperação')
        if 6 <= dicionario['média'] <= 8:
            print('Situação: Médiano')
        elif 9 <= dicionario['média'] <= 10:
            print('Situação: Boa')
    return dicionario

aluno1 = notas(4.5, 10, 6.5, sit=True)
aluno2 = notas(10, 5, 7, 8)
print(aluno1)
print('-' * 60)
print(aluno2)
help(notas)


from ..models import WeatherResult
from openweather.pipeline.weatherRequests import WeatherRequests
from ..models import CityIds
import asyncio
import time
import logging

logger = logging.getLogger()

class WeatherChecker:

    def __init__(self, db_session):
        logger.info('Init WeatherChecker')
        self.db = db_session

    def _get_cityIds(self):
        query = self.db.query(CityIds).all()
        cityIds = []
        for item in query:
            cityIds.append(item.cityId)
        return cityIds

    def split(self, list_a, chunk_size):

        for i in range(0, len(list_a), chunk_size):
            yield list_a[i:i + chunk_size]

    def get_weather_results(self):

        weatherResults = []
        cityIds = self._get_cityIds()
        chunk_size = 60
        cityIdsChunks = self.split(cityIds, chunk_size)

        for cityIdsChunk in cityIdsChunks:
            loop = asyncio.new_event_loop()
            try:
                weatherResults.extend(loop.run_until_complete(WeatherRequests().process_weather_request(cityIdsChunk)))
                time.sleep(60)
            finally:
                loop.close()

        return weatherResults

    def convert_results(self, document, weatherResults):

        weatherConvResults = []
        for weatherResult in weatherResults:
            if weatherResult.get('status') == 200:

                result = WeatherResult()
                result.document_id = document.id
                result.cityId = weatherResult.get('cityId')
                result.tempCelsius = weatherResult.get('content').get('main').get('temp')
                result.humidity = weatherResult.get('content').get('main').get('humidity')

                weatherConvResults.append(result)

        return weatherConvResults

    def check(self, document):

        weatherResults = self.get_weather_results()
        return self.convert_results(document, weatherResults)


def solve(s):
    if(len(s)==1):
        print("NO")
    elif s[-2] == '8' and s[-1] == '6':
        print ("YES")
    else:
        print("NO")

t = int(input())
while t > 0:
    s = input()
    solve(s)
    t -= 1


from telegram import Bot, InlineKeyboardMarkup, InlineKeyboardButton, Update
from telegram.ext import Updater,ApplicationBuilder, CommandHandler, MessageHandler, ConversationHandler, CallbackQueryHandler, ContextTypes

async def hello(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text(f'Hello {update.effective_user.first_name}')

async def noword(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    msg = update.message.text
    print(msg)
    items = msg.split()
    x = int(items[1])
    y = int(items[2])
    await update.message.reply_text(f'{x} + {y} = {x + y}')

#КОМАНДА ДЛЯ ДЗ
async def delword(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    txt = update.message.text
    print(f"Исходный текст: {txt}")
    find_txt = "абв"
    lst = [i for i in txt.split() if find_txt not in i]
    await update.message.reply_text(f'Результат: {" ".join(lst)}')

app = ApplicationBuilder().token("5352600050:AAF_Z8lJNkllOvvieD7-CWdtsEAJe1v9Peo").build()

app.add_handler(CommandHandler("hello", hello))
app.add_handler(CommandHandler("noword", noword))
app.add_handler(CommandHandler("delword", delword))
app.run_polling()


#######
# This makes a 3x3 scatterplot of wheels.csv, and sends
# the result of hover to the screen as a JSON object.
######
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import pandas as pd
import json

app = dash.Dash()

df = pd.read_csv('../data/wheels.csv')

app.layout = html.Div([
    html.Div([
    dcc.Graph(
        id='wheels-plot',
        figure={
            'data': [
                go.Scatter(
                    x = df['color'],
                    y = df['wheels'],
                    dy = 1,
                    mode = 'markers',
                    marker = {
                        'size': 12,
                        'color': 'rgb(51,204,153)',
                        'line': {'width': 2}
                        }
                )
            ],
            'layout': go.Layout(
                title = 'Wheels & Colors Scatterplot',
                xaxis = {'title': 'Color'},
                yaxis = {'title': '# of Wheels','nticks':3},
                hovermode='closest'
            )
        }
    )], style={'width':'30%', 'float':'left'}),

    html.Div([
    html.Pre(id='hover-data', style={'paddingTop':35})
    ], style={'width':'30%'})
])

@app.callback(
    Output('hover-data', 'children'),
    [Input('wheels-plot', 'hoverData')])
def callback_image(hoverData):
    return json.dumps(hoverData, indent=2)

if __name__ == '__main__':
    app.run_server()


#!/usr/bin/python3
def list_division(my_list_1, my_list_2, list_length):
    n_l = []
    for n in range(list_length):
        try:
            a = my_list_1[n] / my_list_2[n]
        except ZeroDivisionError:
            a = 0
            print("division by 0")
        except TypeError:
            a = 0
            print("wrong type")
        except IndexError:
            a = 0
            print("out of range")
        finally:
            n_l.append(a)
    return(n_l)


import plotly.figure_factory as ff
import plotly.graph_objects as go
import statistics
import random
import pandas as pd
import csv

df= pd.read_csv("medium_data.csv")
data= df["average"].tolist()

def random_set_of_mean(counter):
    dataset=[]
    for i in range(0,counter):
        random_index = random.randint(0,len(data))
        value=data[random_index]
        dataset.append(value)
    mean=statistics.mean(dataset)
    return mean

def setup():
    meanlist=[]
    for i in range(0,1000):
        set_of_means= random_set_of_mean(100)
        meanlist.append(set_of_means)

def show_fig(meanlist):
    df = meanlist
    fig = ff.create_distplot(([df]), ['temp'] , show_hist=False )
    fig.show


import os
import tensorflow as tf
from read_utils import TextConverter, batch_generator
from model import  Model,Config
# from model_attention import Model,Config

def main(_):

    model_path = os.path.join('models', Config.file_name)
    if os.path.exists(model_path) is False:
        os.makedirs(model_path)

    et = TextConverter(text=None,save_dir='models/en_vocab.pkl', max_vocab=Config.en_vocab_size, seq_length = Config.seq_length)
    zt = TextConverter(text=None,save_dir='models/zh_vocab.pkl', max_vocab=Config.zh_vocab_size, seq_length = Config.seq_length+1)  # +1是因为，decoder层序列拆成input=[:-1]和label=[1:]
    print('english vocab lens:',et.vocab_size)
    print('chinese vocab lens:',zt.vocab_size)

    en_arrs = et.get_en_arrs('data/train.tags.en-zh.en_clear')
    zh_arrs = zt.get_en_arrs('data/train.tags.en-zh.zh_clear')

    train_g = batch_generator( en_arrs, zh_arrs, Config.batch_size)

    # 加载上一次保存的模型
    model = Model(Config)
    checkpoint_path = tf.train.latest_checkpoint(model_path)
    if checkpoint_path:
        model.load(checkpoint_path)

    print('start to training...')
    model.train(train_g, model_path)

if __name__ == '__main__':
    tf.app.run()


#Importing Pandas and Matplotlib for enabling following function

import pandas as pd
import matplotlib.pyplot as plt

pokedexdf = pd.read_csv("pokemon_data.csv", index_col='#')
#Defining Def_line to be invoked in Main.py
def Defense_line():
    plt.plot(pokedexdf.Defense,color='y',lw=1,ls=':',marker=',',markersize=2,markeredgecolor='k')
    plt.title("Defense rate of pokemons")
    plt.xlabel("Pokemon no.")
    plt.ylabel("Defense rate")
    plt.show()


# -*- coding: utf-8 -*-
"""
Created on Fri Jul  5 09:25:26 2019

@author: sbtithzy
"""
# 从人脸图像文件中提取人脸特征存入 CSV
import cv2
import os
import dlib
from skimage import io
import csv
import numpy as np
# 要读取人脸图像文件的路径
path_images_from_camera = "data/data_faces_from_camera/"
# Dlib 正向人脸检测器
detector = dlib.get_frontal_face_detector()
# Dlib 人脸预测器
predictor = dlib.shape_predictor("data/data_dlib/shape_predictor_5_face_landmarks.dat")
# Dlib 人脸识别模型
# Face recognition model, the object maps human faces into 128D vectors
face_rec = dlib.face_recognition_model_v1("data/data_dlib/dlib_face_recognition_resnet_model_v1.dat")
# 返回单张图像的 128D 特征
def return_128d_features(path_img):
    img_rd = io.imread(path_img)
    img_gray = cv2.cvtColor(img_rd, cv2.COLOR_BGR2RGB)
    faces = detector(img_gray, 1)

    print("%-40s %-20s" % ("检测到人脸的图像 / image with faces detected:", path_img), '\n')

    # 因为有可能截下来的人脸再去检测，检测不出来人脸了
    # 所以要确保是 检测到人脸的人脸图像 拿去算特征
    if len(faces) != 0:
        shape = predictor(img_gray, faces[0])
        face_descriptor = face_rec.compute_face_descriptor(img_gray, shape)
    else:
        face_descriptor = 0
        print("no face")
    return face_descriptor
# 将文件夹中照片特征提取出来, 写入 CSV
def return_features_mean_personX(path_faces_personX):
    features_list_personX = []
    photos_list = os.listdir(path_faces_personX)
    if photos_list:
        for i in range(len(photos_list)):
            # 调用return_128d_features()得到128d特征
            print("%-40s %-20s" % ("正在读的人脸图像 / image to read:", path_faces_personX + "/" + photos_list[i]))
            features_128d = return_128d_features(path_faces_personX + "/" + photos_list[i])
            #  print(features_128d)
            # 遇到没有检测出人脸的图片跳过
            if features_128d == 0:
                i += 1
            else:
                features_list_personX.append(features_128d)
    else:
        print("文件夹内图像文件为空 / Warning: No images in " + path_faces_personX + '/', '\n')

    # 计算 128D 特征的均值
    # personX 的 N 张图像 x 128D -> 1 x 128D
    if features_list_personX:
        features_mean_personX = np.array(features_list_personX).mean(axis=0)
    else:
        features_mean_personX = '0'
    return features_mean_personX
# 获取已录入的最后一个人脸序号 / get the num of latest person
person_list = os.listdir("data/data_faces_from_camera/")
person_num_list = []
for person in person_list:
    person_num_list.append(int(person.split('_')[0]))
person_cnt = max(person_num_list)
person_name_list = []
for person in person_list:
    #person_name_list.append(str(person.split('_')[0]))
    person_name_list.append(person)
#person_cnt = max(person_num_list)

with open("data/features_all.csv", "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    for person in range(person_cnt):
        # Get the mean/average features of face/personX, it will be a list with a length of 128D
        print(path_images_from_camera + str(person_name_list[person]))
        features_mean_personX = return_features_mean_personX(path_images_from_camera + str(person_name_list[person]))
        writer.writerow(features_mean_personX)
        print("特征均值 / The mean of features:", list(features_mean_personX))
        print('\n')
    print("所有录入人脸数据存入 / Save all the features of faces registered into: data/features_all.csv")


from typing import List
import random
import sys
import math

def is_sorted(arr: List[int]) -> bool:
    for i in range(len(arr) - 1):
        if arr[i] > arr[i + 1]:
            return False
    return True

def eval_sort(sort_fn):
    k = int(sys.argv[1]) if len(sys.argv) > 1 else 100
    r = int(sys.argv[2]) if len(sys.argv) == 3 else k
    random.seed(1337)
    x = random.choices(range(r), k=k)
    print('sorted', is_sorted(x))
    print('compares', sort_fn(x, 0, k - 1))
    print('NlogN', round(k * math.log(k, 2)))
    print('sorted', is_sorted(x))


#!/usr/bin/env python

"""
This is the main script for scoring a classifier either on the test set or by cross validation on training data
"""

import sys
sys.path.append('../')
import warnings
warnings.filterwarnings(action='ignore', category=UserWarning, module='gensim')
import argparse
from time import time

from src.lexicons import *

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import MultinomialNB, BernoulliNB
from sklearn.neural_network import MLPClassifier
from sklearn.dummy import DummyClassifier
from src.statistics import *

choices = ['svm', 'logistic_regression', 'knn', 'naive_bayes_bernoulli', 'naive_bayes_binomial', 'neural_network',
               'most_frequent', 'stratified', 'random', 'lexicon']
classifiers = [SVC(C=0.1, kernel='linear', class_weight='balanced'), LogisticRegression(class_weight='balanced'), KNeighborsClassifier(), BernoulliNB(), MultinomialNB(),
               MLPClassifier(), DummyClassifier(strategy='most_frequent'), DummyClassifier(strategy='stratified'),
               DummyClassifier(strategy='uniform'), ANEWPredictor()]
classifiers = dict(zip(choices, classifiers))

preprocessor = lambda text: preprocess(text, word_transformation='', lowercase=True)
bag_of_words_extractor = CountVectorizer(binary=True, ngram_range=(1, 1),
                                         tokenizer=lambda text: preprocess(text, word_transformation='', lowercase=True),
                                         lowercase=True)
swn_extractor = SentiWordNet_Extractor()
sentiment140_extractor_uni = Sentiment140ExtractorUnigram()
sentiment140_extractor_bi = Sentiment140ExtractorBigram()
custom_extractor = Custom_Extractor()
anew_extractor = ANEWExtractor()
compiled_lexicon_extractor = CompiledExtractor()
bingliu_extractor = BingLiuExtractor()
AFINN_extractor = AFINNExtractor()
extractors = [bag_of_words_extractor, swn_extractor, sentiment140_extractor_uni, sentiment140_extractor_bi, custom_extractor, anew_extractor,
              compiled_lexicon_extractor, bingliu_extractor, AFINN_extractor]
extractors = [bag_of_words_extractor]

def main(arguments):
    begin = time()
    parser = argparse.ArgumentParser(
        description="Train a classifier and either cross validate on training data or score on the test data",
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('train', help="Input training data file in format TEXT<tab>LABEL or TEXT<tab>TOPIC<tab>LABEL",
                        type=argparse.FileType('r'))
    parser.add_argument('classifier', choices=choices,
                        help="Name of the classifier to use on the data: \n{{{}}}".format('|'.join(choices)),
                        metavar='classifier')
    parser.add_argument('-t', '--test', help="""Input test data file in same format as TRAIN. If not provided,
                        cross validation is executed on the train data""",
                        type=argparse.FileType('r'))

    # retrieve arguments
    args = vars(parser.parse_args(arguments))
    train = args['train'].name
    test= args['test'].name if args['test'] else None
    classifier = args['classifier']

    # load data
    train_tweets, train_labels = load_data(train)
    print("Number of training tweets : {}".format(len(train_tweets)))
    # get chosen classifier
    estimator = classifiers[classifier]
    print("Classifier : {}".format(classifier))
    # create the features union and the pipeline
    features = create_feature_unions(extractors)
    pipeline = create_pipeline(features, estimator)

    # whether fit the model or not (always true excepted is lexicon is the chosen estimator
    fit=True
    # if test data is provided, then train on train and test on test
    print(plot_class_repartition(train_tweets, train_labels))
    # print(check_data_format(train_tweets, train_labels))
#     if test:
#         test_tweets, test_labels = load_data(test)
#         print("Number of testing tweets : {}".format(len(test_tweets)))
#         if classifier == 'lexicon':
#             pipeline = estimator
#             print("Lexicon is a particular classifier : it scores directly on the test set")
#             fit = False
#         training = time()
#         predictions, precision, recall, fscore, support, accuracy = evaluate_model_predict(pipeline, train_tweets,
#                                                                                train_labels, test_tweets,
#                                                                                test_labels, fit=fit)
#     # if not test data, perform cross validation on train data
#     else:
#         if classifier == 'lexicon':
#             pipeline = estimator
#             print("Lexicon is a particular classifier : it scores directly on the whole train set")
#             fit = False
#         training = time()
#         predictions, precision, recall, fscore, support, accuracy = evaluate_model_predict(pipeline, train_tweets,
#                                                                                train_labels, fit=fit)
#     end = time() - begin
#     end_training = time() - training
#     # print results
#     print('Precision: {}'.format(precision))
#     print('Recall: {}'.format(recall))
#     print('Fscore: {}'.format(fscore))
#     print('Support: {}'.format(support))
#     print('Average fscore : {}'.format((fscore[0] + fscore[-1]) / 2))
#     print('Average recall : {}'.format((recall[0] + recall[-1]) / 2))
#     print("Accuracy: {mean:.3f}".format(mean=accuracy.mean()))
#     print("Done in {0:.2f}".format(end))
#     print("Training in {0:.2f}".format(end_training))
#

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))


# s.find(s[i]) == i

def solution(s):
    answer = ""
    for i in range(len(s)):
        # print(s[i])
        if s.find(s[i]) == i:
            answer += s[i]

    return answer

print(solution("kkkkdsdfesdfaqqq"))


import matplotlib.pyplot as plt

import numpy as np
import pandas as pd

dataset = pd.read_csv("breast_cancer.csv")
dataset.isnull().any()
dataset['G']=dataset['G'].fillna(dataset['G'].mode()[0])
dataset.isnull().any()
dataset["K"]=np.where(dataset["K"]==4,1,0)

from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()

data1=dataset.iloc[:,1:].values

from sklearn.model_selection import train_test_split
features_train, features_test = train_test_split(data1, test_size=0.5, random_state=0)

gnb.fit(
    features_train[:,:-1],
    features_train[:,-1]
)
labels_pred = gnb.predict(features_test[:,:-1])

from sklearn.metrics import confusion_matrix
cm_mnb = confusion_matrix(features_test[:,-1], labels_pred)

score = gnb.score(features_train[:,:-1],features_train[:,-1])
score = gnb.score(features_test[:,:-1],labels_pred)

x=np.array([[6,2,5,3,9,4,7,2,2]])
labels_pred1 = gnb.predict(x)
if labels_pred1 ==1:
    print('you may have cancer')
else:
    print('you dont have canser')

"""--------------------------------"""


from flask import Flask, current_app
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

import config

db = SQLAlchemy()
migrate = Migrate()

def create_app() :
    app = Flask(__name__)
    app.config.from_object(config)
    db.init_app(app)
    migrate.init_app(app, db)

    import views.test_view as test_view
    app.register_blueprint(test_view.bp)

    # /는 서버 주소를 의미 -> 127.0.0.1:5000/
    @app.route('/')
    def test_view() :
        return '123123'

    # 사람 - 이름, 나이, 주소
    # 1. 홍길동 20 대전
    # 2. 홍길동 20 대전

    p1 = Person(name='홍길동', age=30, address='대전')
    app.app_context().push()
    db.session.add(p1)
    db.session.commit()

    return app

class Person(db.Model) :
    no = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
    age = db.Column(db.Integer)
    address = db.Column(db.String(50))


import time

from exercises.utils import long_division

def exercise_026(maximum_divisor):
    """
    A unit fraction contains 1 in the numerator. The decimal representation of
    the unit fractions with denominators 2 to 10 are given:

    1/2	= 	0.5
    1/3	= 	0.(3)
    1/4	= 	0.25
    1/5	= 	0.2
    1/6	= 	0.1(6)
    1/7	= 	0.(142857)
    1/8	= 	0.125
    1/9	= 	0.(1)
    1/10	= 	0.1
    Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can
    be seen that 1/7 has a 6-digit recurring cycle.

    Find the value of d < 1000 for which 1/d contains the longest recurring
    cycle in its decimal fraction part.

    :param maximum_divisor: Maximum divisor
    :type maximum_divisor: int

    :return: Divisor with the longest recurring cycle.
    :rtype: int
    """

    cycle_divisor = {}
    for d in range(1, maximum_divisor):
        div = long_division(1, d)
        cycle = div.split("(")
        cycle_divisor[d] = len(cycle[-1]) - 1 if len(cycle) == 2 else 0
    return max(cycle_divisor, key=cycle_divisor.get)

if __name__ == '__main__':

    start_time = time.time()
    maximum_divisor = 1000
    print(exercise_026(maximum_divisor))
    end_time = time.time()
    print('{} s'.format(end_time - start_time))


# 剑指 offer 18. 删除链表的节点
from typing import List

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def deleteNode(self, head: ListNode, val: int) -> ListNode:
        pre = dummy = ListNode(0)
        dummy.next = head
        while pre.next:
            cur = pre.next
            if cur.val == val:
                pre.next = cur.next
                break
            pre = pre.next
        return dummy.next

def generate_link(nums: List[int]) -> ListNode:
    head = ListNode(0)
    cur = head
    for num in nums:
        cur.next = ListNode(num)
        cur = cur.next
    return head.next

def print_link(head: ListNode) -> None:
    res, cur = [], head
    while cur:
        res.append(cur.val)
        cur = cur.next
    print(res)

s = Solution()
head = generate_link([4, 5, 1, 9])
print_link(s.deleteNode(head, 5))

head = generate_link([4, 5, 1, 9])
print_link(s.deleteNode(head, 1))


import collections
import unittest

class Solution(object):
    def findLHS(self,nums):
        count = collections.Counter(nums)
        result = 0
        for x in count:
            if x + 1 in count:
                result = max(result,count[x] + count[x + 1])
        return result

class Test(unittest.TestCase):
    def test(self):
        self._test([1,1,1,2,3], 4)

    def _test(self, li, expected):
        actual = Solution().findLHS(li)
        self.assertEqual(actual, expected)

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


dados = {}
pontos = []
dados['Nome'] = str(input('Digite o nome do jogador: '))
partida = int(input('Digite a quantidade de partidas que jogou: '))
tot = 0
for g in range(0,partida):
    gols = int(input('Digite a quantidade de gol: '))
    pontos.append(gols)
    tot = tot + gols
dados['Gols'] = pontos[:]
dados['Total'] = tot
print('-=-'*40)
print(dados)
print('-=-'*40)
for k , v in dados.items():
    print(f'O campo {k} tem valor {v}')
print('-=-'*40)
print(f'O jogador {dados.get("Nome")} jogou {partida} partidas')
count = 1
for v in pontos:
    print(f'    => Na partida {count}, fez {v} gols')
    count += 1
print(f'O total de gols foi {dados.get("Total")}')


# -*- coding: utf-8 -*-
from collective.cover.testing import ALL_CONTENT_TYPES
from collective.cover.tests.base import TestTileMixin
from collective.cover.tiles.carousel import CarouselTile
from collective.cover.tiles.carousel import ICarouselTile
from collective.cover.tiles.carousel import UUIDSFieldDataConverter
from collective.cover.widgets.textlinessortable import TextLinesSortableWidget
from lxml import etree
from plone import api
from plone.tiles.interfaces import ITileDataManager
from plone.uuid.interfaces import IUUID

import unittest

class CarouselTileTestCase(TestTileMixin, unittest.TestCase):
    def setUp(self):
        super(CarouselTileTestCase, self).setUp()
        self.tile = CarouselTile(self.cover, self.request)
        self.tile.__name__ = u"collective.cover.carousel"
        self.tile.id = u"test"

    def _update_tile_data(self):
        # tile's data attribute is cached; reinstantiate it
        self.tile = self.cover.restrictedTraverse(
            "@@{0}/{1}".format(str(self.tile.__name__), str(self.tile.id))
        )

    @unittest.expectedFailure  # FIXME: raises BrokenImplementation
    def test_interface(self):
        self.interface = ICarouselTile
        self.klass = CarouselTile
        super(CarouselTileTestCase, self).test_interface()

    def test_default_configuration(self):
        self.assertTrue(self.tile.is_configurable)
        self.assertTrue(self.tile.is_droppable)
        self.assertTrue(self.tile.is_editable)

    def test_accepted_content_types(self):
        self.assertEqual(self.tile.accepted_ct(), ALL_CONTENT_TYPES)

    def test_tile_is_empty(self):
        self.assertTrue(self.tile.is_empty())

    def test_autoplay(self):
        # autoplay is True when tile is empty
        self.assertTrue(self.tile.autoplay())
        # but Galleria init code is not rendered
        self.assertNotIn('data-autoplay="True"', self.tile())
        obj = self.portal["my-image"]
        self.tile.populate_with_object(obj)
        self.assertIn('data-autoplay="True"', self.tile())
        data_mgr = ITileDataManager(self.tile)
        data = data_mgr.get()
        data["autoplay"] = False
        data_mgr.set(data)
        self._update_tile_data()
        self.assertFalse(self.tile.autoplay())
        self.assertIn('data-autoplay="False"', self.tile())

    def test_crud(self):
        # we start with an empty tile
        self.assertTrue(self.tile.is_empty())

        # now we add a couple of objects to the list
        obj1 = self.portal["my-document"]
        obj2 = self.portal["my-image"]

        self.tile.populate_with_object(obj1)
        self.tile.populate_with_object(obj2)

        self._update_tile_data()

        # Document should not have been added
        self.assertEqual(len(self.tile.results()), 1)
        self.assertNotIn(obj1, self.tile.results())
        self.assertIn(obj2, self.tile.results())

        # next, we replace the list of objects with a different one
        obj3 = self.portal["my-image1"]
        self.tile.replace_with_uuids([IUUID(obj3, None)])
        self._update_tile_data()
        self.assertNotIn(obj1, self.tile.results())
        self.assertNotIn(obj2, self.tile.results())
        self.assertIn(obj3, self.tile.results())

        # finally, we remove it from the list; the tile must be empty again
        self.tile.remove_item(obj3.UID())
        self._update_tile_data()
        self.assertTrue(self.tile.is_empty())

    def test_internal_structure(self):
        # we start with an empty tile
        self.assertTrue(self.tile.is_empty())
        uuids = ITileDataManager(self.tile).get().get("uuids", None)

        self.assertIsNone(uuids)

        # now we add an image
        obj1 = self.portal["my-image"]

        self.tile.populate_with_object(obj1)

        uuids = ITileDataManager(self.tile).get().get("uuids", None)

        self.assertTrue(isinstance(uuids, dict))
        self.assertTrue(len(uuids) == 1)
        self.assertTrue(uuids[obj1.UID()]["order"] == u"0")

        # now we add a second image
        obj2 = self.portal["my-image1"]

        self.tile.populate_with_object(obj2)

        uuids = ITileDataManager(self.tile).get().get("uuids", None)

        self.assertTrue(isinstance(uuids, dict))
        self.assertTrue(len(uuids) == 2)
        self.assertTrue(uuids[obj1.UID()]["order"] == u"0")
        self.assertTrue(uuids[obj2.UID()]["order"] == u"1")

    def test_custom_title(self):
        # we start with an empty tile
        self.assertTrue(self.tile.is_empty())
        uuids = ITileDataManager(self.tile).get().get("uuids", None)

        self.assertIsNone(uuids)

        # now we 2 elements
        obj1 = self.portal["my-document"]
        obj2 = self.portal["my-image"]
        self.tile.populate_with_uuids([obj1.UID(), obj2.UID()])

        # For obj2 we will assign a custom_title
        uuids = ITileDataManager(self.tile).get().get("uuids", None)
        uuids[obj2.UID()]["custom_title"] = u"New Title"

        title1 = self.tile.get_title(obj1)
        title2 = self.tile.get_title(obj2)

        # Document object should be the same as Title
        self.assertEqual(title1, obj1.Title())
        # First image should return the custom Title
        self.assertEqual(title2, u"New Title")

    def test_custom_description(self):
        # we start with an empty tile
        self.assertTrue(self.tile.is_empty())
        uuids = ITileDataManager(self.tile).get().get("uuids", None)

        self.assertIsNone(uuids)

        # now we 2 elements
        obj1 = self.portal["my-document"]
        obj2 = self.portal["my-image"]
        self.tile.populate_with_uuids([obj1.UID(), obj2.UID()])

        # For obj2 we will assign a custom_description
        uuids = ITileDataManager(self.tile).get().get("uuids", None)
        uuids[obj2.UID()]["custom_description"] = u"New Description"

        description1 = self.tile.get_description(obj1)
        description2 = self.tile.get_description(obj2)

        # Document object should be the same as Description
        self.assertEqual(description1, obj1.Description())
        # First image should return the custom URL
        self.assertEqual(description2, u"New Description")

    def test_custom_url(self):
        # we start with an empty tile
        self.assertTrue(self.tile.is_empty())
        uuids = ITileDataManager(self.tile).get().get("uuids", None)

        self.assertIsNone(uuids)

        # now we 3 elements
        obj1 = self.portal["my-document"]
        obj2 = self.portal["my-image"]
        obj3 = self.portal["my-image1"]
        self.tile.populate_with_uuids([obj1.UID(), obj2.UID(), obj3.UID()])

        # For obj2 we will assign a custom_url
        uuids = ITileDataManager(self.tile).get().get("uuids", None)
        uuids[obj2.UID()]["custom_url"] = u"http://www.custom_url.com"

        url1 = self.tile.get_url(obj1)
        url2 = self.tile.get_url(obj2)
        url3 = self.tile.get_url(obj3)

        # Document object should be the same as absolute_url
        self.assertEqual(url1, obj1.absolute_url())
        # First image should return the custom URL
        self.assertEqual(url2, u"http://www.custom_url.com")
        # And second image should have the absolute_url and /view
        self.assertEqual(url3, obj3.absolute_url() + "/view")

    def test_data_converter(self):
        field = ICarouselTile["uuids"]
        widget = TextLinesSortableWidget(self.request)
        conv = UUIDSFieldDataConverter(field, widget)

        value = {
            u"uuid1": {u"order": u"0"},
            u"uuid2": {u"order": u"2"},
            u"uuid3": {u"order": u"1"},
        }

        to_widget = conv.toWidgetValue(value)
        self.assertEqual(to_widget, u"uuid1\r\nuuid3\r\nuuid2")

        to_field = conv.toFieldValue(value)

        self.assertEqual(to_field, value)
        self.assertEqual(conv.toFieldValue({}), conv.field.missing_value)

    def test_get_alt(self):
        obj1 = self.portal["my-image"]
        self.tile.populate_with_object(obj1)
        rendered = self.tile()
        # the image is there and the alt attribute is set
        self.assertIn("<img ", rendered)
        self.assertIn('alt="This image was created for testing purposes"', rendered)

    def test_populate_with_collection(self):
        """Populating a carousel tile with a collection should result on
        the tile being populated with the results that have an image
        field.
        """
        from collective.cover.testing import zptlogo
        from collective.cover.tests.utils import set_image_field

        with api.env.adopt_roles(["Manager"]):
            api.content.create(self.portal, "News Item", id="item1")
            api.content.create(self.portal, "News Item", id="item2")
            api.content.create(self.portal, "News Item", id="item3")
            # handle Archetypes and Dexterity
            set_image_field(self.portal["item1"], zptlogo)
            set_image_field(self.portal["item2"], zptlogo)

            query = [
                dict(
                    i="portal_type",
                    o="plone.app.querystring.operation.selection.is",
                    v="News Item",
                )
            ]
            col = api.content.create(
                self.portal, "Collection", id="collection", query=query
            )

        self.tile.populate_with_object(col)
        html = etree.HTML(self.tile())
        imgs = html.xpath("//img")
        self.assertEqual(len(imgs), 3)
        srcs = [img.attrib["src"] for img in imgs]
        srcs.sort()
        self.assertIn("http://nohost/plone/item1", srcs[0])
        self.assertIn("http://nohost/plone/item2", srcs[1])
        self.assertIn("http://nohost/plone/my-news-item", srcs[2])


import asyncio
from time import time

from throughput_measure.do_logging import log

class Consumer:
    def __init__(self, job_queue, consumption_queue, consumer_id, delay):
        self.consumer_id = consumer_id
        self.job_queue = job_queue
        self.consumption_queue = consumption_queue
        self.kill_sig = False
        self.delay = delay
        self.is_enabled = False

    async def consume(self):
        while not self.kill_sig:
            if self.is_enabled:
                item = await self.job_queue.get()
                start_time = time()
                await item()
                enqueue_item = {'time': time(), 'response_time': (time() - start_time)}
                self.consumption_queue.put(enqueue_item)
                log(f"Consumer {self.consumer_id}: status {enqueue_item}")
            await asyncio.sleep(self.delay)

    def kill(self):
        self.delay = 0
        self.kill_sig = True

    def set_delay(self, delay):
        self.delay = delay

    def enable(self):
        log(f"Consumer {self.consumer_id} is being enabled")
        self.is_enabled = True

    def disable(self):
        log(f"Consumer {self.consumer_id} is being disabled")
        self.is_enabled = False


def Armstrong(num):
    str_num = str(num)
    n = len(str_num)
    Sum = 0
    for i in str_num:
        Sum = Sum + int(i) ** n
    if Sum == num:
        print("The number %d is Armstrong number" % num)
    else:
        print("The number %d is not Armstrong number" % num)

num = int(input("Enter the number to check if armstrong number:"))
Armstrong(num)