import json
import os
from pathlib import Path

import torch
import torch.nn as nn

from utils.visualization import TensorboardXWriter

class BaseTrainer:
    def __init__(self, config, resume: bool, G, D, optim_G, optim_D, loss_function):
        self.n_gpu = config["n_gpu"]
        self.dev = self._prepare_device(self.n_gpu, use_cudnn=config["use_cudnn"])

        self.generator = G.to(self.dev)
        self.discriminator = D.to(self.dev)

        if self.n_gpu > 1:
            self.generator = torch.nn.DataParallel(self.generator, device_ids=list(range(self.n_gpu)))
            self.discriminator = torch.nn.DataParallel(self.discriminator, device_ids=list(range(self.n_gpu)))

        self.optimizer_G = optim_G
        self.optimizer_D = optim_D

        self.loss_addition = loss_function
        self.loss_gan = nn.BCEWithLogitsLoss()

        self.feature = config["feature"]

        self.additional_loss_factor = config["trainer"]["additional_loss_factor"]
        self.adversarial_loss_factor = config["trainer"]["adversarial_loss_factor"]
        self.soft_label = config["trainer"]["soft_label"]
        self.epochs = config["trainer"]["epochs"]
        self.save_period = config["trainer"]["save_period"]
        self.iter_discriminator_period = config["trainer"]["iter_discriminator_period"]
        self.validation_period = config["trainer"]["validation_period"]

        self.start_epoch = 1  # 非配置项，当 resume == True 时，参数会被重置
        self.best_score = 0.0  # 非配置项
        self.root_dir = Path(config["save_location"]) / config["name"]
        self.checkpoints_dir = self.root_dir / "checkpoints"
        self.tensorboardX_logs_dir = self.root_dir / "logs"
        self._prepare_empty_dir([self.root_dir, self.checkpoints_dir, self.tensorboardX_logs_dir], resume)
        self.viz = TensorboardXWriter(self.tensorboardX_logs_dir.as_posix())
        self.viz.writer.add_text("Configuration", json.dumps(config, indent=2, sort_keys=False), global_step=1)
        self.viz.writer.add_text("Description", config["description"], global_step=1)

        if resume: self._resume_checkpoint()

        print("模型，优化器，参数，目录初始化完毕，本实验中使用的配置信息如下：")
        print(json.dumps(config, indent=2, sort_keys=False))
        config_save_path = os.path.join(self.root_dir, "config.json")
        with open(config_save_path, "w") as handle:
            json.dump(config, handle, indent=2, sort_keys=False)
        self._print_networks([self.generator, self.discriminator])

    def _resume_checkpoint(self):
        """恢复至最近一次的模型断点
        恢复至最近一次的模型断点。模型加载时要特殊留意，如果模型是 DataParallel 的实例，需要读取 model.module.*
        """
        latest_model_path = self.checkpoints_dir / "latest_model.tar"

        if latest_model_path.exists():
            print(f"正在加载最近一次保存的模型断点 {latest_model_path}")

            checkpoint = torch.load(latest_model_path.as_posix(), map_location=self.dev)
            self.start_epoch = checkpoint["epoch"] + 1
            self.best_score = checkpoint["best_score"]
            self.optimizer_G.load_state_dict(checkpoint["optim_state_dict_G"])
            self.optimizer_D.load_state_dict(checkpoint["optim_state_dict_D"])

            if isinstance(self.generator, torch.nn.DataParallel):
                self.generator.module.load_state_dict(checkpoint["model_state_dict_G"])
                self.discriminator.module.load_state_dict(checkpoint["model_state_dict_D"])
            else:
                self.generator.load_state_dict(checkpoint["model_state_dict_G"])
                self.discriminator.load_state_dict(checkpoint["model_state_dict_D"])

            print(f"断点已被加载，将从 epoch = {self.start_epoch} 处开始训练.")
        else:
            print(f"{latest_model_path} 不存在，无法加载最近一次保存的模型断点")

    def _save_checkpoint(self, epoch, is_best=False):
        """存储模型断点
        将模型断点存储至 checkpoints 目录，包含：
            - 当前轮次数
            - 历史上最高的得分
            - 优化器参数
            - 模型参数
        """

        print(f"正在存储模型断点，epoch = {epoch} is_best = {is_best} ...")

        # 构建待存储的数据字典
        state_dict = {
            "epoch": epoch,
            "best_score": self.best_score,
            "optim_state_dict_G": self.optimizer_G.state_dict(),
            "optim_state_dict_D": self.optimizer_D.state_dict(),
        }

        if self.dev.type == "cuda" and self.n_gpu > 1:
            state_dict["model_state_dict_G"] = self.generator.module.cpu().state_dict()
            state_dict["model_state_dict_D"] = self.discriminator.module.cpu().state_dict()
        else:
            state_dict["model_state_dict_G"] = self.generator.cpu().state_dict()
            state_dict["model_state_dict_D"] = self.discriminator.cpu().state_dict()

        # 存储三个数据字典
        # latest_model：全部数据信息，包含所有网络参数与优化器参数等，每个 epoch 都会覆盖之前的
        # generator_{epoch}: 生成器网络参数，后续可以通过指定 epoch 来加载生成器网络断点进行推理
        # best_model: 包含全部数据信息，仅在 is_best 成立时才存储
        torch.save(state_dict, (self.checkpoints_dir / "latest_model.tar").as_posix())
        torch.save(state_dict["model_state_dict_G"],
                   (self.checkpoints_dir / f"generator_{str(epoch).zfill(4)}.pth").as_posix())
        if is_best:
            print(f"发现最优模型，正在存储中，epoch = {epoch} ...")
            torch.save(state_dict, (self.checkpoints_dir / "best_model.tar").as_posix())

        # model.cpu() 会将模型转移至 CPU，此时需要将模型重新转至 GPU
        self.generator.to(self.dev)
        self.discriminator.to(self.dev)

    @staticmethod
    def _print_networks(nets: list):
        print(f"当前模型包含 {len(nets)} 个子网络，参数信息如下：")
        params_of_all_networks = 0
        for i, net in enumerate(nets, start=1):
            params_of_network = 0
            for param in net.parameters():
                params_of_network += param.numel()

            print(f"\t子网络 {i}： {params_of_network / 1e6} 百万个.")
            params_of_all_networks += params_of_network

        print(f"参数数量总计为 {params_of_all_networks / 1e6} 百万个.")

    @staticmethod
    def _prepare_empty_dir(dirs, resume):
        for dir_path in dirs:
            if resume:
                assert dir_path.exists()
            else:
                dir_path.mkdir(parents=True, exist_ok=True)

    @staticmethod
    def _prepare_device(n_gpu: int, use_cudnn=True):
        """
        根据 n_gpu 的大小选择使用 CPU 或 GPU
        Args:
            n_gpu(int): 实验使用 GPU 的数量，当 n_gpu 为 0 时，使用 CPU，n_gpu > 1时，使用
        Note:
            1. 运行 train.py 脚本时需要设置可见 GPU，此时修改第一块 GPU 的起始位置，否则默认加载模型只能在绝对位置的 cuda:0 上
            2. 在当前项目初始时设置可见的 GPU 后，项目中只能使用相对编号
            3. cudnn benchmark 会自动寻找算法来优化固定大小的输入时的计算，如果考虑实验的可重复性，可以设置：
                torch.backends.cudnn.deterministic = True
               即使用固定的算法，会有一些性能的影响，但是能保证可重复性
        """
        use_cpu = False

        if n_gpu == 0:
            use_cpu = True
            print("实验将使用 CPU.")
        else:
            assert n_gpu <= torch.cuda.device_count(), \
                f"使用 GPU 数量为 {n_gpu}，大于系统拥有的 GPU 数量 {torch.cuda.device_count()}"

            if use_cudnn:
                print("实验将使用 Cudnn，实验结果可能无法重复.")
                torch.backends.cudnn.enabled = True
                torch.backends.cudnn.benchmark = True
            else:
                print("实验未使用 Cudnn.")

        device = torch.device("cpu" if use_cpu else "cuda:0")

        return device

    def _train_epoch(self, epoch):
        raise NotImplementedError

    def train(self):
        raise NotImplementedError


# -*- coding: utf-8 -*-
from IndexerBase import IndexerBase
import numpy as np
import talib

class RSI(IndexerBase):
    indexer_name = 'RSI'
    indexer_name_list = ['RSI']
    default_para_dic = {
        'N': 5,
    }

    def __init__(self, raw_data, plt):
        super(RSI, self).__init__(raw_data, plt)
        self.indexer_name_list = ['RSI']  # MA的指标名和参数名都跟参数有关，所以要随参数进行设置
        self.indexer_color_dic = {
            'RSI': 'blue'
        }

    def calculate_indexer_value(self):
        n = self.para_dic['N']
        close_array = np.array(self.raw_data['close'].values, dtype='float')
        self.indexer_value_dic['RSI'] = talib.RSI(close_array, n)

    def draw_indexer(self):
        i = 0
        for indexer_name, values in self.indexer_value_dic.items():
            c = self.indexer_color_dic[indexer_name][0]
            self.plt_dic[indexer_name] = self.plt.plot(name=indexer_name, pen=c)
            self.plt_dic[indexer_name].setData(values)
            i += 1

    def re_draw_indexer(self):
        for pname, values in self.indexer_value_dic.items():
            self.plt_dic[pname].setData(values)

    def get_polar_value(self,start_pos, end_pos):
        max_v = max(self.indexer_value_dic['RSI'][start_pos:end_pos])
        min_v = min(self.indexer_value_dic['RSI'][start_pos:end_pos])
        return max_v, min_v


from django.conf.urls import url
from django.urls import  include, path
from django.conf import settings
from django.conf.urls.static import static

from apps.publications.views import ListPublicationsAdminView,ListPublicationsView, CreatePublicationsNewsView, UpdatePublicationsNewsView, ShowPublicationsNewsView, DeletePublicationsNewsView, CreateCommentNewsView,  DeleteCommentNewsView,CreateLikeNewsView,CreateLikeCommentNewsView

app_name = 'publications'
urlpatterns = [
    path('list/admin/news',ListPublicationsAdminView.as_view(),name="list_news_admin"),
    path('list/news',ListPublicationsView.as_view(),name="list_news"),
    path('create/news',CreatePublicationsNewsView.as_view(),name="create_news"),
    path('create/like/news/<int:pk>',CreateLikeNewsView.as_view(),name="create_like_news"),
    path('update/news/<int:pk>',UpdatePublicationsNewsView.as_view(),name="update_news"),
    path('show/news/<int:pk>',ShowPublicationsNewsView.as_view(),name="show_news"),
    path('delete/news',DeletePublicationsNewsView.as_view(),name="delete_news"),

    path('create/comments',CreateCommentNewsView.as_view(),name="create_comments"),
    path('create/like/comments',CreateLikeCommentNewsView.as_view(),name="create_like_comments"),

    path('delete/comments/<int:pk>',DeleteCommentNewsView.as_view(),name="delete_comments"),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


# implementation of card game - Memory

import simplegui
import random

# global constants
CARD_RANGE = 8
CARD_WIDTH = 50
CARD_HEIGHT = 100

# helper function to initialize globals
def new_game():
    global memory_deck, exposed, turns, state

    memory_deck = range(CARD_RANGE)
    memory_deck = memory_deck * 2 # concatenate list
    random.shuffle(memory_deck)   # shuffle deck

    # Create list of booleans and initialise to False
    exposed = [False] * len(memory_deck)

    # initialise turns to zero
    turns = 0
    state = 0

# define event handlers
def mouseclick(pos):
    # add game state logic here
    global turns, state, prev_card1, prev_card2
    card_clicked = pos[0] // CARD_WIDTH

    if state == 0:
        # first turn: increment turns and change state
        prev_card1 = card_clicked
        exposed[card_clicked] = True  # Expose clicked card
        state = 1;
        turns += 1

    elif state == 1:
        prev_card2 = card_clicked
        if exposed[card_clicked] == False :
            exposed[card_clicked] = True
            state = 2
    else:
        if exposed[card_clicked] == False :
            exposed[card_clicked] = True
            state = 1
            turns += 1

            # if previous 2 choices dont match,
            # hide both cards
            if memory_deck[prev_card1] != memory_deck[prev_card2]:
                exposed[prev_card1] = False
                exposed[prev_card2] = False

            # set previous clicked card
            prev_card1 = card_clicked

# cards are logically 50x100 pixels in size
def draw(canvas):
    number_position = [15, 60]
    x1, x2 = 0, CARD_WIDTH
    counter = 0

    for card in memory_deck:
        if exposed[counter] == True:
            canvas.draw_text(str(card), number_position, 40, "White")
        else:
            canvas.draw_polygon([(x1, 0), (x2, 0), (x2, CARD_HEIGHT), (x1, CARD_HEIGHT)], 6, 'Black', 'Green' )

        # increment x co-ord for number by the CARD_WIDTH
        number_position[0] += CARD_WIDTH
        counter += 1
        x1 += CARD_WIDTH
        x2 += CARD_WIDTH

    label.set_text(turns) # update turns label

# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Reset", new_game)
label = frame.add_label("Turns = 0")

# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)

# get things rolling
new_game()
frame.start()

# Always remember to review the grading rubric


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

import gspread
from oauth2client.service_account import ServiceAccountCredentials
import telebot
from telebot import types
import urllib3
import re
import requests
import time
from time import sleep
import datetime
from datetime import datetime
import _thread
import random

# ======================================================================================================================
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds1 = ServiceAccountCredentials.from_json_keyfile_name('auction1.json', scope)
creds2 = ServiceAccountCredentials.from_json_keyfile_name('auction2.json', scope)
client1 = gspread.authorize(creds1)
client2 = gspread.authorize(creds2)
sheet1 = client1.open('Users-Auction').sheet1
sheet2 = client2.open('Users-Auction').sheet1
itemsheet1 = client1.open('Items-Auction').sheet1
itemsheet2 = client2.open('Items-Auction').sheet1
g_lotnames = itemsheet1.row_values(1)
tkn = os.environ['TOKEN']
bot = telebot.TeleBot(tkn)

less = '🌲'
gori = '⛰'
gold = '💰'

atk = '⚔'
deff = '🛡'
mo = '🇲🇴'
gp = '🇬🇵'
cy = '🇨🇾'
va = '🇻🇦'
im = '🇮🇲'
eu = '🇪🇺'
ki = '🇰🇮'
skal = '🖤'
bats = '🦇'
turt = '🐢'
oplt = '☘'
rose = '🌹'
farm = '🍆'
ambr = '🍁'

count = 5
adress = int(itemsheet1.cell(2, 1).value)

idMe = 396978030
idChatDevelopment = -1001186759363

# ======================================================================================================================
bot.send_message(idMe, '🤤')

def rawtime(stamp):
    rtime = []
    weekday = datetime.utcfromtimestamp(int(stamp + 3 * 60 * 60)).strftime('%a')
    if weekday == 'Mon':
        weekday = 'Пн'
    elif weekday == 'Tue':
        weekday = 'Вт'
    elif weekday == 'Wed':
        weekday = 'Ср'
    elif weekday == 'Thu':
        weekday = 'Чт'
    elif weekday == 'Fri':
        weekday = 'Пт'
    elif weekday == 'Sat':
        weekday = 'Сб'
    elif weekday == 'Sun':
        weekday = 'Вс'
    day = datetime.utcfromtimestamp(int(stamp + 3 * 60 * 60)).strftime('%d')
    month = datetime.utcfromtimestamp(int(stamp + 3 * 60 * 60)).strftime('%m')
    year = datetime.utcfromtimestamp(int(stamp + 3 * 60 * 60)).strftime('%Y')
    hours = datetime.utcfromtimestamp(int(stamp + 3 * 60 * 60)).strftime('%H')
    minutes = datetime.utcfromtimestamp(int(stamp)).strftime('%M')
    seconds = datetime.utcfromtimestamp(int(stamp)).strftime('%S')
    rtime.append(weekday)
    rtime.append(day)
    rtime.append(month)
    rtime.append(year)
    rtime.append(hours)
    rtime.append(minutes)
    rtime.append(seconds)
    return rtime

def rawtime_lite(stamp):
    rtime = []
    hours = datetime.utcfromtimestamp(int(stamp + 3 * 60 * 60)).strftime('%H')
    minutes = datetime.utcfromtimestamp(int(stamp)).strftime('%M')
    seconds = datetime.utcfromtimestamp(int(stamp)).strftime('%S')
    rtime.append(hours)
    rtime.append(minutes)
    rtime.append(seconds)
    return rtime

@bot.message_handler(commands=['time'])
def handle_time_command(message):
    time = rawtime(int(datetime.now().timestamp()))
    text = 'Время: ' + str(time[4]) + ':' + str(time[5]) + ':' + str(time[6]) + \
        ' (' + str(time[0]) + ' ' + str(time[1] + '.' + str(time[2]) + '.' + \
        str(time[3])) + ', GMT+3)'
    bot.send_message(message.chat.id, text, parse_mode='HTML')

@bot.message_handler(commands=['id'])
def handle_id_command(message):
    if message.reply_to_message is None:
        text = 'Твой ID: ' + str(message.from_user.id) + '\n'
        if message.chat.id < 0:
            text = text + 'Group ID: ' + str(message.chat.id) + ''
    elif message.reply_to_message:
        id = str(message.reply_to_message.from_user.id)
        if message.reply_to_message.from_user.username:
            username = '@' + str(message.reply_to_message.from_user.username)
        else:
            username = ''
        if message.reply_to_message.from_user.first_name:
            firstname = str(message.reply_to_message.from_user.first_name)
        else:
            firstname = ''
        if message.reply_to_message.from_user.last_name:
            lastname = str(message.reply_to_message.from_user.last_name)
        else:
            lastname = ''

        signature = str(message.reply_to_message.from_user.is_bot)
        isbot = 'Тип: '
        if signature == 'True' and username == '@rockebolbot':
            isbot = isbot + 'Ето я🖤'
        elif signature == 'True':
            isbot = isbot + 'Бот'
        elif signature == 'False':
            isbot = isbot + 'Человек'
        else:
            isbot = ''

        text = firstname + ' ' + lastname + ' [' + username + ']\n' + \
            'ID: ' + id + '\n' + isbot

    bot.send_message(message.chat.id, text, parse_mode='HTML')

@bot.message_handler(commands=['start'])
def handle_start_command(message):
    if message.chat.id > 0:
        keyboard = types.InlineKeyboardMarkup(row_width=2)
        button = []
        button.append(types.InlineKeyboardButton(text='Я шпион🕵🏿', callback_data='Spy'))
        button.append(types.InlineKeyboardButton(text='Нет😡', callback_data='NoSpy'))
        spy = 0
        if spy == 1:
            text = 'Привет шпион\n' \
                   'Зачем жмякаешь /start? Впрочем, не важно. Думаю, ты продолжишь слать мне пины исправно🤤'
            button = []
        else:
            text = 'Привет. Не будем всё усложнять, ладно? Просто скажи, будешь ли ты для нас шпионом?'
        keyboard.add(*button)
        bot.send_message(message.chat.id, text, reply_markup=keyboard)

@bot.callback_query_handler(func=lambda call: True)
def callbacks(call):
    if call.message.chat.id > 0:
        kek = 0

@bot.message_handler(content_types=["new_chat_members"])
def get_new_member(message):
    if message.chat.title:
        title = str(message.chat.title) + ' ('
    else:
        title = ' ('
    user_id = str(message.from_user.id)
    chat_id = str(message.chat.id)
    if message.from_user.username:
        chat_user = '@' + str(message.from_user.username) + ' / '
    else:
        if message.from_user.first_name:
            firstname = str(message.from_user.first_name)
        else:
            firstname = ''
        if message.from_user.last_name:
            lastname = str(message.from_user.last_name) + ' '
        else:
            lastname = ''
        chat_user = firstname + ' ' + lastname

    if message.new_chat_member is not None and message.new_chat_member.username == 'rockebolbot':
        bot.send_message(idChatDevelopment,
                         chat_user + user_id + ': Добавил бота в чат: ' + title + chat_id + ')')

@bot.message_handler(func=lambda message: message.text)
def repeat_all_messages(message):
    if message.chat.id > 0:
        global itemsheet1
        try:
            google = itemsheet1.row_values(1)
        except:
            creds1 = ServiceAccountCredentials.from_json_keyfile_name('auction1.json', scope)
            client1 = gspread.authorize(creds1)
            itemsheet1 = client1.open('Items-Auction').sheet1
            google = itemsheet1.row_values(1)
        if len(str(google)) > 4000:
            text1 = str(google)[:4000]
            text2 = str(google)[4000:8000]
            bot.send_message(message.chat.id, str(text1))
            bot.send_message(message.chat.id, str(text2))
        else:
            text1 = str(google)
            bot.send_message(message.chat.id, str(text1))

def updater():
    while True:
        try:
            global itemsheet1
            sleep(100)
            try:
                itemsheet1.update_cell(2, 1, str(adress))
            except:
                creds1 = ServiceAccountCredentials.from_json_keyfile_name('auction1.json', scope)
                client1 = gspread.authorize(creds1)
                itemsheet1 = client1.open('Items-Auction').sheet1
                itemsheet1.update_cell(2, 1, str(adress))
        except Exception as e:
            sleep(0.9)

def update_list():
    while True:
        try:
            global itemsheet2
            global count
            sleep(count)
            count = 1
            try:
                google = itemsheet2.row_values(1)
            except:
                creds2 = ServiceAccountCredentials.from_json_keyfile_name('auction2.json', scope)
                client2 = gspread.authorize(creds2)
                itemsheet2 = client2.open('Items-Auction').sheet1
                google = itemsheet2.row_values(1)
            for i in g_lotnames:
                if i not in google:
                    if count < 95:
                        itemsheet1.update_cell(1, len(google) + 1, i)
                        google.append(i)
                        count = count + 1
        except Exception as e:
            sleep(0.9)

def detector():
    while True:
        try:
            global adress
            global g_lotnames
            text = requests.get('https://t.me/ChatWarsAuction/' + str(adress))
            search = re.search('Lot #(\d+) : (.*)\n', str(text.text))
            if search:
                name = search.group(2)
                ench = re.search('(⚡)', name)
                if ench:
                    name = re.sub('⚡\+\d+ ', '', name)
                if name not in g_lotnames:
                    g_lotnames.append(name)
                adress = adress + 1
        except Exception as e:
            sleep(0.9)

def telepol():
    try:
        bot.polling(none_stop=True, timeout=60)
    except:
        bot.stop_polling()
        sleep(0.5)
        telepol()

if __name__ == '__main__':
    _thread.start_new_thread(detector, ())
    _thread.start_new_thread(updater, ())
    _thread.start_new_thread(update_list, ())
    telepol()


import logging
import requests

from uuid import uuid4

from telegram import InlineQueryResultArticle, ParseMode, InputTextMessageContent
from telegram.utils.helpers import escape_markdown

from lxml import html
from bs4 import BeautifulSoup
from .db_requests import *

def inlinequery(bot, update):
    """Handle the inline query.

    Example of api_results:
    >>> results_parsed = [{
    >>>         "result_id": 0,
    >>>         "name": "Молоко М Лианозовское ультрапастеризованное 3,2%, 950г",
    >>>        "img_url": "https://www.utkonos.ru/images/photo/3074/3074902H.jpg"
    >>> }]

    """
    inline_bot_type = None
    results = []

    if ':' in update.inline_query.query:
        query = update.inline_query.query.split(':')
        inline_bot_type = query[0]
        if len(query) > 1:
            query = query[1].strip()
        else:
            query = ''
    else:
        query = update.inline_query.query

    if inline_bot_type == 'Каталог':
        # api_results = get_product_inline_by_name(query)
        api_results = api_request(query)
        results = []

        for result_ in api_results:
            bar_code = 'Арт. %s' % result_['art']
            # bar_code = result_['ean'] if result_['ean'] is not None else \
            #            result_['upc'] if result_['upc'] is not None else str(0)
            results.append(
                InlineQueryResultArticle(
                    id=uuid4(),
                    title=result_['name'],
                    thumb_url=result_['img_url'],
                    input_message_content=InputTextMessageContent('%s %s' % (bar_code, result_['name'])))
                # input_message_content=InputTextMessageContent(result_['name']))
                # result_['name']))

                # result_['name'])
                #
            )
        results = results + [InlineQueryResultArticle(
            id=uuid4(),
            title="Здесь нет моего продукта",
            # thumb_url=result_['img_url'],
            input_message_content=InputTextMessageContent("000 Здесь нет моего продукта"))]
    elif inline_bot_type == 'Упаковки':
        api_results = get_all_wrappings()
        results = []

        for result_ in api_results:
            results.append(
                InlineQueryResultArticle(
                    id=uuid4(),
                    title=result_['name'],
                    description=result_['description'],
                    thumb_url=result_['img_url'],
                    input_message_content=InputTextMessageContent('%s. %s' % (result_['id'],
                                                                              result_['name'])))
                # result_['name']))

                # result_['name'])
                #
            )

    update.inline_query.answer(results)


import math

class Node:
  def __init__(self, x, y):
    self.x = x
    self.y = y
    self.next = None
    self.prev = None

  def __repr__(self):
    return (self.x, self.y)

class LinkedList:
  def __init__(self):
    self.first = None
    self.size = 0
    self.totalX = 0
    self.totalY = 0
    self.XY = 0
    self.XX = 0
    self.YY = 0

  def __repr__(self):
    node = self.first
    nodes = []
    for i in range(self.size):
      nodes.append(f"({node.x},{node.y})")
      node = node.next
    nodes.append("None")
    return " -> ".join(nodes)

  def __add_first(self, newNode):
    newNode.next = self.first
    if self.first:
      newNode.prev = self.first.prev
    self.first = newNode

  def __add_middle(self, curr, newNode):
    newNode.next = curr.next
    if newNode.next:
      newNode.next.prev = newNode
    curr.next = newNode
    newNode.prev = curr

  def add(self, x, y, pos=None):
    if pos == None:
      pos = self.size

    if pos < 0 or pos > self.size:
      return False

    self.totalX += x
    self.totalY += y
    self.XY += x*y
    self.XX += x*x
    self.YY += y*y

    newNode = Node(x, y)
    if(pos == 0):
      self.__add_first(newNode)
    else:
      curr = self.first
      for i in range(pos - 1):
        curr = curr.next
      self.__add_middle(curr, newNode)

    self.size += 1

    return True

  def remove(self, pos):
    if pos < 0 or pos > self.size:
      return False

    returnNode = None
    if(pos == 0):
      returnNode = self.first
      self.first = self.first.next
      if self.first:
        self.first.prev = None
    else:
      returnNode = self.first
      for i in range(pos):
        returnNode = returnNode.next

      if returnNode.next:
        returnNode.next.prev = returnNode.prev
      returnNode.prev.next = returnNode.next

    self.size -= 1

    self.totalX -= returnNode.x
    self.XX -= returnNode.x*returnNode.x

    self.totalY -= returnNode.y
    self.YY -= returnNode.y*returnNode.y

    self.XY -= returnNode.x*returnNode.y

    return (returnNode.x, returnNode.y)

  def average(self):
    return (self.totalX/self.size, self.totalY/self.size)

  def standardDeviation(self):
    avgX, avgY = self.average()
    sumX = 0
    sumY = 0
    curr = self.first
    for i in range(self.size):
      sumX += pow(curr.x - avgX, 2)
      sumY += pow(curr.y - avgY, 2)
      curr = curr.next
    return (math.sqrt(sumX/(self.size-1)), math.sqrt(sumY/(self.size-1)))

  def linearRegression(self):
    avgX, avgY = self.average()
    N = self.size
    b1 = (self.XY - N*avgX*avgY)/(self.XX - N*avgX*avgX)
    b0 = avgY - b1*avgX
    return (b0, b1)

  def correlation(self):
    N = self.size
    r = (N*(self.XY) - self.totalX*self.totalY)/math.sqrt((N*self.XX - self.totalX*self.totalX)*(N*self.YY - self.totalY*self.totalY))
    return (r, r*r)

  def estimate(self, xk):
    b0, b1 = self.linearRegression()
    return b0 + b1*xk


import socket
import select

from encryption import enctype2_decoder,enctype2_encoder

#A few shared or miscellaneous functions

def logDebug(message):
    with open("log", 'a') as file:
        file.write("\n"+message)

def parseIPList(listRaw):
    list=[]
    i=0
    while i+5 < len(listRaw):
        list.append((str(listRaw[i])+'.'+str(listRaw[i+1])+'.'+str(listRaw[i+2])+'.'+str(listRaw[i+3]), listRaw[i+4]*256+listRaw[i+5]))
        i+=6
    return(list)

def packIPList(list):
    listRaw=[]
    for IP in list:
        ip = IP[0].split('.')
        for nr in ip:
            listRaw.append(int(nr))
        listRaw.append(IP[1]>>8)
        listRaw.append(IP[1]&0xff)
    return(listRaw)

def getServerListFromMaster(IP):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((IP, 28900))
    data = s.recv(1024)
    s.sendall(bytes('\\gamename\\bfield1942\\gamever\\2\\location\\0\\validate\\bfMaster\\enctype\\2\\final\\\\queryid\\1.1\\', 'utf-8'))
    s.sendall(bytes('\\list\\cmp\\gamename\\bfield1942\\final\\', 'utf-8'))
    dataBytes = s.recv(1024)
    s.close()
    dataDecoded = enctype2_decoder("HpWx9z",list(dataBytes))
    return(parseIPList(dataDecoded))

def getBytesFromServerList(ip_port_list):
    data = packIPList(ip_port_list)
    dataOutEnc = enctype2_encoder("HpWx9z",data)
    return(bytes(dataOutEnc))

def queryServer(ip, port):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #UDP
        s.setblocking(0)
        s.connect((ip, port))
        s.sendall(bytes("\\status\\", 'utf-8'))
        properties = {}
        for i in range(20): #max 20 packets
            ready = select.select([s], [], [], 3) #3 seconds timeout
            if ready[0]: dataBytes = s.recv(1024*16)
            else: return(None)
            dataList = dataBytes.decode("utf-8", errors='ignore').split("\\")
            dataList.pop(0)
            nrOfProperties = int(len(dataList)/2)
            for j in range(nrOfProperties):
                properties[dataList[j*2]] = dataList[j*2+1]
            if 'final' in properties:
                break
        s.close()
        if not 'gameId' in properties: return(None) #filter out BFV servers
        return(properties)
    except: return(None)


'''Template Filler Version 3
Douglas Oak November 2018'''

import pandas as pd
from Dynamic_Data_Dictionary_Active import Dynamic_Data_Info
from General_Cleaning import General_Cleaning
from Additional_Actions import Special_Actions
from Empty_Column_Filler import mandatory_empty_cell_filler_control

'''nameof incomming files'''
file = 'OpenARInvoiceNatuoilNov28.csv'
manditory_missing_data_flag = "FILL"
ouput_df_new_row_place_holder_value = " "
new_row_append_list = []
output_df_index = 0

'''import data dictionary'''
data_dictionary = Dynamic_Data_Info('OpenARInvoice')

output_df_header_list = []
manditory_header_list = []

'''builda list of output headers and manditory headers'''

for key in data_dictionary:
    output_df_header_list.append(data_dictionary[key][0])

    '''if the header is manditory IE the data dictionary position 2 lists as true add the manditory list'''
    if data_dictionary[key][2] == True:
        manditory_header_list.append(data_dictionary[key][0])

'''create output data frame'''
output_df = pd.DataFrame(columns=output_df_header_list)

'''load import data into a dataframe'''
input_df = pd.read_csv(file,encoding = "ISO-8859-1")

'''create list of input file headers'''
input_header_list = input_df.columns

'''iterate through input dataframe cells'''

for column in output_df_header_list:
    new_row_append_list.append(ouput_df_new_row_place_holder_value)

for index, row in input_df.iterrows():

    '''create new row for output dataframe'''

    new_row = pd.Series(new_row_append_list, index=output_df_header_list)

    '''append blank row to output dataframe'''
    output_df = output_df.append(new_row,ignore_index=True)

    '''take input header list and iterate through list'''
    for header in input_header_list:

        '''select individual cell data'''
        cell_data = row[header]

        '''filter out NAN data'''
        if pd.isnull(cell_data) == True:
            continue

        try:
            '''Try to send input header to data dictionary and extract data cleaning list'''
            clean_list = data_dictionary[header]

        except:
            continue

        '''Unpacking Clean List'''

        output_header = clean_list[0]
        data_type = clean_list[1]
        manditory = clean_list[2]
        max_length = clean_list[3]
        additional_actions = clean_list[4]

        '''General Cell Data Cleaning'''
        clean_cell_data = General_Cleaning(cell_data, data_type, max_length,)

        '''Check For additional Cleaning'''
        if additional_actions == True:
            extra_cleaning = clean_list[5]
            extra_cleaning_location = clean_list[6]
            additional_action_data = Special_Actions(clean_cell_data, extra_cleaning)

        '''write Cell Data to output data frame'''
        output_df.iloc[output_df_index][output_header] = clean_cell_data

        '''if addtional cleaning write additional cleaning to cells'''
        if additional_actions == True:
            output_df.iloc[output_df_index][extra_cleaning_location] = additional_action_data

    output_df_index += 1

'''flag any empty manditory cells in the output dataframe'''
second_run_index = 0
for index, row in output_df.iterrows():

    for manditory_header in manditory_header_list:
        manditory_cell = row[manditory_header]

        '''Code for filling in empty manditory cells'''

        if manditory_cell == " ":
            output_df.iloc[second_run_index][manditory_header] = Empty_Column_Filler(manditory_header)

    second_run_index += 1
'''prompt for file to write too'''

out_file_name = input("Enter Output File Name: ")

print("writing to file")
output_df.to_csv(out_file_name)


from boto3 import session
from botocore.client import Config
import yaml
from sqlalchemy import create_engine
import io
import os

import pandas as pd
import requests

def create_client(source):
    if source['type'] == 's3':
        s3_session = session.Session()
        return s3_session.client(
            's3',
            region_name=source['region_name'],
            endpoint_url=source['endpoint_url'],
            aws_access_key_id=source['access_key_id'],
            aws_secret_access_key=source['secret_access_key']
        )
    elif source['type'] == 'postgresql':
        return create_engine("postgresql://%s:%s@%s:%s/%s" % (
            source['username'],
            source['password'],
            source['host'],
            source['port'],
            source['database']
        ))
    else:
        print('No supported source type found')
        return False

os.mkdir('tmp')

config = {}
with open('./config.yaml') as f:
    config = yaml.full_load(f)

for name, w in config['watchers'].items():
    func = getattr(__import__("scripts." + w['function'], fromlist=['run']), 'run')

    if w['source'] not in config['sources']:
        print("Source for %s does not exist!" % name)
        continue

    def task(config, w):
        main_source = config['sources'][w['source']]

        clients = []

        # If the watcher is mounted onto an S3 bucket
        if main_source['type'] == 's3':
            s3_client = create_client(main_source)

            clients.append(s3_client)

            # Add any additional sources to clients object
            for r in w['additional_sources']:
                clients.append(create_client(config['sources'][r]))

            # Loop over each file in the bucket on the specified path
            for obj in s3_client.list_objects(Bucket=w['bucket'], Prefix=w['path'])['Contents']:
                fname = obj['Key'].split('/')[-1:][0]
                s3_client.download_file(Bucket=w['bucket'], Key=obj['Key'], Filename="tmp/" + fname)

                task_result = func(clients, "tmp/" + fname) if 'args' not in w else func(clients, "tmp/" + fname, **w['args'])
                if task_result and 'archive_bucket' in w:
                    # execution successfull and watcher is configured to archive passed objects
                    print("Archiving %s" % f['Key'])
                    s3_client.copy_object(Bucket=w['archive_bucket'], CopySource="%s/%s" % (w['bucket'], obj['Key']), Key=obj['Key'])
                    s3_client.delete_object(Bucket=w['bucket'], Key=f['Key'])

    task(config, w)


import pandas as pd
import re
import regex
from tqdm import tqdm

##########################################################################################################
##########################################################################################################
PATH_DATA = ''
PATH_OUTPUT = ''
##########################################################################################################
##########################################################################################################

def get_doc_without_params(doc):
    all_lines = doc.split('\n')
    no_param_doc = ''
    for s in all_lines:
        if not s.strip().startswith('@'):
            no_param_doc += ' ' + s
        else:
            break
    no_param_sentences = no_param_doc.split('. ')
    sentences = []
    for s in no_param_sentences:
        if s.strip().startswith('/*'):
            s = s[2:].strip()
        if s.strip().startswith('/**'):
            s = s[3:].strip()
        if s.strip().lower().startswith('todo') or\
            s.strip().lower().startswith('* todo'):
            return '. '.join(sentences)
        if s.strip() != '':
            sentences.append(s)
    doc = '. '.join(sentences)
    if doc.strip() == '.':
        return ''
    return doc

def remove_todos(doc):
    if ' todo:' in doc.lower():
        return doc[:doc.index(' todo:')]
    if ' todo :' in doc.lower():
        return doc[:doc.index(' todo :')]
    if ' todo -' in doc.lower():
         return doc[:doc.index(' todo -')]
    if ' todo ' in doc.lower():
        return doc[:doc.index(' todo ')]
    return doc

# false if string does not contains any letter (e.g., `------------`)
def hasChar(s):
  return bool(regex.search(r'[a-zA-Z]+', s))

# replace bunch of weird characters with proper ones
def replace_math_char(s):
    s = s.replace("`", "").replace("←", "<-").replace("१२", "").replace("⁻⁵", "").replace("•", "") \
            .replace("．", "").replace("￼", "").replace("°", "").replace("»«", "").replace("ｔ", "t") \
            .replace("е", "e").replace("≈", "").replace("⇒", "").replace("¯", "").replace("۸", "").replace("३", "") \
            .replace("‘", "'").replace("➜", "->").replace("≠", "!=").replace("？", "?").replace("¦¦", "") \
            .replace("�", "").replace("ह", "").replace("µ", "mu").replace("с", "c").replace("×", "x") \
            .replace("»", "").replace("²", "").replace('"', '"').replace("ö", "").replace("ô", "").replace("ó", "") \
            .replace("ツ", "").replace("⌘", "").replace("«", "").replace("„", "").replace("·", "").replace("İ", "") \
            .replace("。。。", "...").replace("λ", "lambda").replace("§", "").replace("ø", "").replace("�", "") \
            .replace("￼", "").replace("→", "->").replace("´", "'").replace("…", " ").replace("–", "-") \
            .replace("—", "-").replace("─", "-").replace("’", "'").replace("≤", "<=").replace("≥", ">=") \
            .replace("∞", "inf").replace("±", "+-").replace("“", "\"").replace("”", "\"").replace("└", " ") \
            .replace("├", " ").replace("（", "(").replace("）", ")")
    return s

# replace tags
def replace_tags(s):
    s = s.replace('<!-- begin-user-doc -->','').replace('<!-- end-user-doc -->','').replace('<!-- begin-UML-doc -->','')\
        .replace('<!-- end-UML-doc -->','').replace('<!-- -->','').replace('<!-- @see #setRangeProperties //bad link-->','')\
        .replace('<!-- PENDING -->','')
    return s

# clean sentences from patterns such as {@link NAME} -> replace with NAME only
def replace_java_link(s):
    if "{@class" in s:
        s_split = s.split("{@class")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@cdk.cite" in s:
        s_split = s.split("{@cdk.cite")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@coe @" in s:
        s_split = s.split("{@coe @")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@code" in s:
        s_split = s.split("{@code")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@Code" in s:
        s_split = s.split("{@Code")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@href" in s:
        s_split = s.split("{@href")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@inheritDoc" in s:
        s_split = s.split("{@inheritDoc")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@ link" in s:
        s_split = s.split("{@ link")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@link" in s:
        s_split = s.split("{@link")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@Link" in s:
        s_split = s.split("{@Link")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@lin" in s:
        s_split = s.split("{@lin")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@llin" in s:
        s_split = s.split("{@lin")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@list" in s:
        s_split = s.split("{@list")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@literal" in s:
        s_split = s.split("{@literal")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@param" in s:
        s_split = s.split("{@param")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@sample" in s:
        s_split = s.split("{@sample")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@see" in s:
        s_split = s.split("{@see")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@scope" in s:
        s_split = s.split("{@scope")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@state" in s:
        s_split = s.split("{@state")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@throws" in s:
        s_split = s.split("{@throws")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@url" in s:
        s_split = s.split("{@url")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    if "{@value" in s:
        s_split = s.split("{@value")
        new_s = s_split[0]
        for i in range(1, len(s_split)):
            s_split[i] = s_split[i].replace('}', ' ', 1)
            new_s +=  s_split[i]
            s = new_s
    s = s.replace('{@inheritDoc}', '').replace('{@inhericDoc}', '').replace('{@inherit-doc}', '')\
        .replace('{@inheritDoc }', '').replace('{@inheritDocs}', '').replace('{@inheritdoc}', '')\
        .replace('{@inheritDpc}', '').replace('{@interitDoc}', '')

    s = s.replace('{@hide', '').replace('{@index', '').replace('{@abc', '')\
        .replace('{@unknownInlineTag','').replace('{@return', '').replace('{@suppress', '')

    s.strip()

    elem = s
    new_s = ''
    count = 0
    while '{@' in str(elem):
        if '"{@"' in str(elem):
            break
        count += 1
        start = elem.index('{@')
        try:
            end = elem.index('}')
        except:
            print(s)
            print(elem)
            end = elem.index('}')
        new_s += elem[:start] + elem[start + 2:end]
        elem = elem[end + 1:]
    if count > 0:
        new_s += elem
        s = new_s
    return(s)

# replace links
def replaceLinks(s1, s2):
	link_pattern = regex.compile(r"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)")
	matches = [m.group() for m in link_pattern.finditer(s1 + " " + s2)]
	to_replace = set(matches)

	for idx, val in enumerate(to_replace):
		s1 = s1.replace(val, " <LINK_" + str(idx)+ "> ")
		s2 = s2.replace(val, " <LINK_" + str(idx)+ "> ")
	return s1, s2

# not English (Chinese/Russian)
def isNonLatin(s):
	return bool(regex.search(r'[^\x00-\x7F]+', s))

# build list of regex to discard markdown tags
invalid_tags = ["img", "s", "b", "dl", "dd", "i", "sub","sup", "kbd", "code", "CODE", "pre", "a", "hr", "br", "BR", "h1" ,"h2" ,"h3" ,"h4" ,"h5" ,"h6" ,"strong" ,"ul" ,"ol" ,"li" ,"p", "pre","blockquote" ,"em" ,"s" ,"strike", "body" ,"html"]
patterns = []
for tag in invalid_tags:
  patterns.append(regex.compile("<\s*\/?\s*"+tag+"\s*[^>]*\s*>"))

# remove comments from code
def removeComments(s):
  block_comment_pattern = regex.compile(r"/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/")
  inline_comment_pattern = regex.compile(r"(?<!:)\/\/.*")
  s = block_comment_pattern.sub(r' ', s)
  s = inline_comment_pattern.sub(r' ', s)
  return s

# remove meaningless tokens
def remove_meaningless_tokens(doc, s):
    new_s = ''
    tokens = s.split()
    count_t = 0
    for t in tokens:
        if len(set(t)) == 1 and len(t) > 3 and t[0] in ['<', '>', '~', '!', '@', '#', '$', '%', '^', '&', '*', '-', '_', '+', '=', '/']:
            count_t += 1
            continue
        new_s += t + ' '
    if count_t > 0:
        f = open('meh_doc.txt', 'a')
        for doc_elem in doc:
            f.write(doc_elem + '. ')
        f.write('\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n')
        f.close()
    return new_s.strip()

def create_instance(docstring, function):
    instance = ''
    if docstring != '':
        instance += '<technical_language>' + docstring + '</technical_language>'

    instance += '<code>' + function + '</code>'
    instance = re.sub('\t', ' ', instance)
    instance = re.sub('\n', ' ', instance)
    instance = re.sub('\r', ' ', instance)
    instance = re.sub('[ ]{2,}', ' ', instance)
    return instance

useless_docs = ['Internal API. Not Stable!.', 'Internal Method. Stay away.', 'Internal. Do not use.', 'Internal. You will not need to use this method.',\
    'Internal. You will not need this.', 'Internal method. Do not use.', 'Internal. You will not need this method.', 'Internal API - subject to change.',\
    'Internal parse method - made protected for unit testing.', 'Internal method. Do not call directly.', 'Internal implementation, normal users should not use it.',\
    'Internally called by JMeter, never call it directly.']

def is_useless_doc(s):
    if s.lower().strip().startswith('internal'):
        if s.split()[0].lower() != 'internal':
            return True
        if s in useless_docs:
            return True
        return False
    if s.lower().strip().startswith('<!--workaround-->'):
        return True
    if s.lower().strip().startswith('<?xml'):
        return True
    if s.lower().strip().startswith('<xsl'):
        return True
    if s.lower().strip().startswith('<tested xml'):
        return True
    if s.lower().strip().startswith('<!-- c'):
        return True
    if s.lower().strip().startswith('<xsd'):
        return True
    if s.lower().strip().startswith('<!-- begin-model-doc -->'):
        return True
    if '<!-- options -->' in s:
        return True
    if '<!-- description -->' in s:
        return True
    return False

def is_empty_strange_method(method):
    try:
        m_split = method.split('{')[1]
    except:
        return False, True
    if m_split.strip() == '}':
        return True, False
    return False, False

def is_method(method):
    if method.strip()[-1] == '}':
        return True
    return False

def is_test(method):
    if '@test' in method.lower():
        return True
    return False

def is_reminder(doc):
    if doc.strip().lower().startswith('remind'):
        return True
    return False

##########################################################################################################
##########################################################################################################

cleaned_doc_summary = []
cleaned_functions = []

# load data
print('loading data...')
df_all = pd.read_csv(PATH_DATA)
print('starting data: ', len(df_all))
df_all.drop_duplicates(inplace=True)

functions = list(df_all['method'])
docstrings = list(df_all['cleaned_docstrings'])
cleaned_doc = []

print('Java methods cleaning...')
indexes_to_remove = []

empty_method = 0
not_a_method = 0
test_method = 0
# remove comments from functions
for i in tqdm(range(len(functions))):
    f = functions[i]
    f = removeComments(f)
    flag1, flag2 = is_empty_strange_method(f)
    if flag1:
        indexes_to_remove.append(i)
        cleaned_functions.append('')
        empty_method += 1
        continue
    if flag2:
        indexes_to_remove.append(i)
        cleaned_functions.append('')
        not_a_method += 1
        continue
    if not is_method(f):
        indexes_to_remove.append(i)
        cleaned_functions.append('')
        not_a_method += 1
        continue
    if is_test(f):
        indexes_to_remove.append(i)
        cleaned_functions.append('')
        test_method += 1
        continue
    cleaned_functions.append(f)

print('empty methods      : ', empty_method)
print('not correct method : ', not_a_method)
print('test methods       : ', test_method)

##########################################################################################################
##########################################################################################################

indexes_to_check = list(set(range(len(docstrings))) - set (indexes_to_remove))

empty_doc_summary = 0
useless_doc = 0
not_latin_doc = 0

print('Javadoc comments cleaning...')
for i in tqdm(indexes_to_check):
    current_doc = str(docstrings[i])
    if current_doc == 'nan' or current_doc == '':
        indexes_to_remove.append(i)
        empty_doc_summary += 1
        continue
    if current_doc.strip().endswith('}'):
        indexes_to_remove.append(i)
        useless_doc += 1
        continue
    # remove params
    current_doc = get_doc_without_params(current_doc)
    # replace links
    current_doc, cleaned_functions[i] = replaceLinks(current_doc, cleaned_functions[i])
    # discard markdown from string
    for pattern in patterns:
        current_doc = pattern.sub(r' ', current_doc)
    # replace bunch of weird characters with proper ones
    current_doc = replace_math_char(current_doc)
    # replace Java links {@class class_name}
    current_doc = replace_java_link(current_doc)
    # replace some tags
    current_doc = replace_tags(current_doc)

    # remove empty docstrings after cleaning
    if current_doc.strip() == '':
        indexes_to_remove.append(i)
        empty_doc_summary += 1
        continue
    # meaningless sentences (e.g., "-------")
    if not hasChar(current_doc):
        useless_doc.append(current_doc)
        indexes_to_remove.append(i)
        continue
    if is_useless_doc(current_doc):
        indexes_to_remove.append(i)
        useless_doc += 1
        continue
    # remove not latin languages
    if isNonLatin(current_doc):
        not_latin_doc.append(current_doc)
        indexes_to_remove.append(i)
        continue
    if is_reminder(current_doc):
        indexes_to_remove.append(i)
        useless_doc += 1
        continue

    cleaned_doc.append(current_doc)

###############################################################
df = pd.DataFrame({
    'function' : cleaned_functions,
    'docstring' : docstrings
})
df.drop(index=indexes_to_remove, axis=0, inplace=True)

cleaned_functions = list(df['function'])
docstrings = list(df['docstring'])
###############################################################

# split doc summary in sentences
doc_summary_sentences = [doc.split('. ') for doc in cleaned_doc]

# clean sentences
for doc in tqdm(doc_summary_sentences):
    cleaned_sentences = []
    for s in doc:
        # ignore empty line
        if s.strip() == '' or s.strip() == '.':
            continue
        # remove meaningless tokens
        s = remove_meaningless_tokens(doc, s)
        s = " ".join(s.split())
        # check if empty sentence
        if s.strip() == '':
            continue
        cleaned_sentences.append(s.strip())
    cleaned_doc_summary.append(cleaned_sentences)

# remove empty and strange doc summary
empty_doc_summary_instances = []
strange_doc_count = 0
for i in tqdm(range(len(cleaned_doc_summary))):
    if len(doc_summary_sentences[i]) == 0:
        empty_doc_summary_instances.append(i)
        continue
    if str(current_doc.strip()).startswith('$ANTLR'):
        empty_doc_summary_instances.append(i)
        strange_doc_count += 1
        continue

empty_doc_summary += len(empty_doc_summary_instances)
empty_doc_summary -= strange_doc_count

###############################################################
df = pd.DataFrame({
    'function' : cleaned_functions,
    'docstring' : docstrings,
    'cleaned_doc' : cleaned_doc_summary
})

df.drop(index=empty_doc_summary_instances, axis=0, inplace=True)

cleaned_functions = list(df['function'])
docstrings = list(df['docstring'])
cleaned_doc_summary = list(df['cleaned_doc'])

# put sentences together and create a single string for the doc summary
for i in tqdm(range(len(cleaned_doc_summary))):
    for j in range(len(cleaned_doc_summary[i])):
        cleaned_doc_summary[i][j] = cleaned_doc_summary[i][j].strip()

    cleaned_doc_summary[i] = '. '.join(cleaned_doc_summary[i]).strip()
    while cleaned_doc_summary[i].startswith('*') or\
        cleaned_doc_summary[i].startswith('/') or\
        cleaned_doc_summary[i].startswith('-'):
        cleaned_doc_summary[i] = cleaned_doc_summary[i][1:].strip()
    if cleaned_doc_summary[i].endswith(' .'):
        cleaned_doc_summary[i] = cleaned_doc_summary[i][-1].strip() + '.'
    if not cleaned_doc_summary[i].endswith('.'):
        cleaned_doc_summary[i] = cleaned_doc_summary[i].strip() + '.'

# remove short doc summary
short_doc_indexes = []
for i in tqdm(range(len(cleaned_doc_summary))):
    s = str(cleaned_doc_summary[i])
    while s.strip().endswith('.'):
        s = s.strip()[:-1]
    if len(s.split()) < 4:
        short_doc_indexes.append(i)

###############################################################
df = pd.DataFrame({
    'function' : cleaned_functions,
    'docstring' : docstrings,
    'cleaned_doc' : cleaned_doc_summary
})

print('data before removing useless instances:', len(df))
df.drop(index=short_doc_indexes, axis=0, inplace=True)
print('data after:', len(df))
df.drop_duplicates(inplace=True)
print('without duplicates:', len(df))

cleaned_functions = list(df['function'])
docstrings = list(df['docstring'])
cleaned_doc_summary = list(df['cleaned_doc'])
###############################################################

print('create instances with javadoc and method code...')
# create instances
all_instances = []
for i in tqdm(range(len(cleaned_functions))):
    all_instances.append(create_instance(cleaned_doc_summary[i], cleaned_functions[i]))

print('not Latin Javadocs : ', len(not_latin_doc))
print('empty Javadocs     : ', empty_doc_summary)
print('short Javadocs     : ', len(short_doc_indexes))
print('-----------------------------------------')
print('final instances    : ', len(all_instances))

df['instance'] = all_instances

df.to_csv(PATH_OUTPUT)
print('cleaning done!')
print(len(df))


#枚举 不可变 不可重复
from enum import Enum
class VIP(Enum):
    YELLOW = 1
    GREAN = 2
    RED = 3
    huangse = 1
    BLUE = 8
print(VIP.RED,VIP['GREAN'],type(VIP.RED))#VIP.RED enum
print(VIP.RED.name, type(VIP.RED.name)) #str
#字典和类表示  缺陷：可变，不能排除重复
#a = {'YELLOW':1, 'GREAN':2}
# class typeDiamond:
#     YELLOW = 1
#     GREAN = 2
#可变 a['YELLOW'] = 4                       # s = (1,2,3)
#typeDiamond.YELLOW = 5                     # s[0] = 4
#VIP.YELLOW = 6    报错                     # print(s)

#比较
print(VIP.GREAN == VIP['GREAN'])
#别名
print(VIP.huangse) #VIP.YELLOW
for v in VIP:#别名不打印
    print('循环---',v, type(v))
for v in VIP.__members__:#别名打印
    print('打印别名---',v, type(v))
for v in VIP.__members__.items():#别名打印
    print('打印别名项---',v, type(v))

#转换
print(VIP(8)) #实际上不是int()之类的转换
from enum import IntEnum, unique
#unique避免枚举重复
@unique
class color(IntEnum):
    BLACK = 1
    # GREY = 1
    WHITE = '2'
    # GREAT = 'sh'


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        if root==None:
            return root
        q=deque()
        q.append(root)
        ans=[]
        while q:
            temp=[]
            for i in range(len(q)):
                curr=q.popleft()
                temp.append(curr.val)
                if curr.left:
                    q.append(curr.left)
                if curr.right:
                    q.append(curr.right)
            ans.append(temp)
        return ans


"""
decorators.py - Contains decorators utilized throughout the script
"""

from app.utils.driver import terminate_driver
from app.utils.notify import send_email

import os
from datetime import datetime
import time
import random
import psycopg2
from functools import wraps
import traceback
import logging

logger = logging.getLogger(__name__)
_MAX_RETRIES = 5

def retry_db(func: object) -> None:
    """Decorator to wait + retry DB functions in case of serialization errors

    :param func: function to retry when serialization error encountered
    :raises ValueError: raised when max number of retries reached
    :raises e: error raised in case of non-serialization failure
    """

    @wraps(func)
    def wrapper(*args, **kwargs):
        # retry fx until max retries reached
        conn = args[0].conn
        retries = 0
        while True:
            retries += 1
            if retries == _MAX_RETRIES:
                err_msg = f"Transaction did not succeed after {_MAX_RETRIES} retries"
                raise ValueError(err_msg)

            try:
                return func(*args, **kwargs)

            except psycopg2.Error as e:
                # check if error codes pertain to serialization errors
                # venv/lib/python3.8/site-packages/psycopg2/errorcodes.py
                logger.info("e.pgcode: {}".format(e.pgcode))
                if e.pgcode == "40001" or e.pgcode == "25P02":

                    # exponential backoff while retrying txn
                    conn.rollback()
                    logger.info("SERIALIZATION FAILURE: RETRYING TXN")
                    sleep_ms = (2**retries) * 0.1 * (random.random() + 0.5)
                    logger.info("Sleeping {} seconds".format(sleep_ms))
                    time.sleep(sleep_ms)
                    continue
                else:
                    # error not pertaining to serialization failure - simply raise normally
                    logger.error(f"NON-SERIALIZATION_FAILURE: {e}")
                    logger.error(traceback.format_exc())
                    raise e

    return wrapper

def alert_driver_failure(func: object) -> None:
    """Decorator to screenshot/quit driver execution in case of failure

    :param func: driver execution to try
    :raises e: error raised in case of driver execution failure
    """

    @wraps(func)
    def wrapper(*args, **kwargs):
        # retry fx until max retries reached
        # driver = args[1]
        # user = args[2]
        driver = kwargs["driver"]
        user = kwargs["user"]

        try:
            return func(*args, **kwargs)

        except Exception as e:
            # save screenshot of driver page
            dt_str = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
            screenshot_path = f"/tmp/{dt_str}-SCREENSHOT.png"
            driver.get_screenshot_as_file(screenshot_path)

            # save page source
            source_path = f"/tmp/{dt_str}-SOURCE.txt"
            with open(source_path, "w") as f:
                f.write(driver.page_source)

            # log details
            tb = traceback.format_exc()
            logger.info(f"An error occurred during driver execution: {e}")
            logger.error(tb)

            # notify user
            html_content = f"""

An error occurred during driver execution. See attachments for details.

Exception:

{e}

Traceback:

{tb}

            """

            send_email(
                user.notify_to,
                subject="DRIVER EXECUTION ERROR",
                html_content=html_content,
                attach_paths=[screenshot_path, source_path],
            )

            os.remove(screenshot_path)
            os.remove(source_path)

            raise e

    return wrapper


import math
import numpy as np

layer_outputs = [4.8, 1.21, 2.385]

# layer_outputs = [4.8, 4.79, 4.25]

E = math.e

exp_values = []

for output in layer_outputs:
    exp_values.append(E**output)

#print(exp_values)

norm_base = sum(exp_values)
norm_values = []

for value in exp_values:
    norm_values.append(value/norm_base)

# print(norm_values)
# print(sum(norm_values))

layer_outputs = [[4.8, 1.21, 2.385],
                 [8.9, -1.81, 0.2],
                 [1.41, 1.051, 0.026]]

exp_values = np.exp(layer_outputs)
norm_values = exp_values / np.sum(exp_values, axis=1,keepdims=True)

print(norm_values)
# print(sum(norm_values))


#
# CS 196 Data Hackerspace
# Assignment 1: Data Parsing and NumPy
# Due September 24th, 2018
#

import json
import csv
import numpy as np
import math

def histogram_times(filename):
    with open(filename) as input:
    	read = csv.reader(input)
    	data = list(read)
    L = [0] * 24
    for incident in data[1:]:
    	if incident[1]:
    		check = incident[1].split(':')[0]
    		if check.isdigit():
    			hour = int(check)
    			if (hour < 25 and hour > -1):
    				L[hour] += 1
    return L

def weigh_pokemons(filename, weight):
	with open(filename) as input:
		read = json.load(input)
	L = []
	for pokemon in read["pokemon"]:
		check = pokemon["weight"].split(' ')[0]
		if float(check) == weight:
			L.append(pokemon["name"])
	return L

def single_type_candy_count(filename):
    with open(filename) as input:
    	read = json.load(input)
    count = 0;
    for pokemon in read["pokemon"]:
    	if "candy_count" in pokemon.keys():
    		if len(pokemon["type"]) == 1:
    			count += pokemon["candy_count"]
    return count

def reflections_and_projections(points):
	index = 0
	for y in points[1]:
		points[1][index] += 2*(1-y)
		index += 1

	A = np.array([[0, -1], [1,0]])
	points = A.dot(points)

	B = np.array([[1, 3], [3, 9]])
	points = B.dot(points)

	points /= 10
	return points

def normalize(image):
	max = image.max()
	min = image.min()

	for x in range(0,32):
		for y in range(0,32):
			image[x][y] = 255 / (max - min) * (image[x][y] - min)

	return image

def sigmoid_normalize(image, a):
	for x in range(0,32):
		for y in range(0,32):
			image[x][y] = 255 / (1 + math.exp(-1 / a * (image[x][y] - 128)))

	return image


from flask_mail import Message

def send_email(to, subject, template):
    from app.app import app, mail
    msg = Message(
        subject,
        recipients=[to],
        html=template,
        sender=app.config['MAIL_DEFAULT_SENDER']
    )
    mail.send(msg)


"""State machine"""

import inspect

from pycog.exceptions import Accept, Reject, Backtrack

class _StateRecord:
    """Information about a state."""

    def __init__(self, name, state_dict, activity=None, accepting=False,
                 guard=lambda fsm:True):
        self.name = name
        if state_dict == None:
            self.state_dict = dict()
        else:
            self.state_dict = state_dict
        self.activity = activity
        self.state_dict['_accepting'] = accepting

        # Names of available transitions.  Order is important for stability.
        self.transitions = []

        # current_state -> test(sm, exiting, entering)
        self.transition_info = dict()

        # guard function.  Determines if the state can be entered.
        self.guard = guard;

class _TransitionRecord:
    """Information about a transition"""

    def __init__(self, test, label=None):
        self.test = test
        self.label = label

class state:
    """State decorator

    Example:
        @state('p')
        def p(self):
            '''In state p'''
            pass
        @transition('q')
        def p(self):
            '''Test for transition to q.'''
            return True

        # Another way to specify transitions.  The lambda function takes
        # a StateMachine instance as it's `sm` argument.
        @state('q', transitions=[
            ('r', lambda sm, cur_state, trans_state: ...),
            ('s', lambda sm, cur_state, trans_state: ...)])
    """

    def __init__(self, name, state_dict=None, transitions=None,
                 accepting=False, label=None,  **kw_args):
        if __debug__:
            if state_dict != None:
                assert type(state_dict) is dict

        super().__init__(**kw_args)
        self.record = _StateRecord(name, state_dict, accepting=accepting)
        if transitions != None:
            for transition in transitions:
                if type(transition) is tuple:
                    if len(transition) == 2:
                        target_s_name, test = transition
                    elif len(transition) == 1:
                        # This allows a way of specifying transition keys that
                        # are tuples without a test, "(x,y),".  This way (x,y)
                        # is considered the transition, and the test is
                        # unspecified.  "(x, y)" would take x as the transition
                        # and y as the transition test.
                        target_s_name, = transition
                        test = transition_always
                    else:
                        raise ValueError("Invalid transition specification.")
                else:
                    target_s_name = transition
                    test = transition_always

                self.record.transitions.append(target_s_name)
                self.record.transition_info[target_s_name] = \
                        _TransitionRecord(test, label)

    def __call__(self, activity):
        self.record.activity = activity
        return self

    def transition(self, target_s_name, label=None):
        """
        Transition test decorator

        Args:
            target_s_name: Name of the target state

        Example:
            @state('p')
            def p(self):
                '''In state p'''
                pass
            @transition('q')
            def p(self):
                '''Test for transition to q.'''
                ...
        """

        def _transition_setter(method):
            """
            Decorator helper for transitions.
            """
            nonlocal self, target_s_name

            def _trans_test(fsm, cur_state, next_state):
                """
                Adapter for one-parameter transition tests.
                """
                return method(fsm)

            self.record.transitions.append(target_s_name)
            self.record.transition_info[target_s_name] =\
                    _TransitionRecord(_trans_test, label)

            return self

        return _transition_setter

    def guard(self, method):
        """
        Decorator for state guards.

        A state guard should return True if the state may be entered, false
        otherwise.
        """
        self.record.guard = method
        return self

def transition_always(fsm, cur_state, next_state):
    """
    Convenience transition test -- always transition.
    """
    return True

class StateMachine:
    """State machine framework"""

    def __init__(self, initial=None, **kw_args):

        super().__init__(**kw_args)

        # These help get from the name to the state and vice-versa.
        self._state_records = dict()

        # Create states for items created with the 'state' decorator
        for attr_name in dir(self):
            attr = inspect.getattr_static(self, attr_name)
            if isinstance(attr, state):
                self._state_records[attr.record.name] = attr.record

        if initial:
            self.set_initial_state(initial)
        else:
            self._current_state = None

    def set_initial_state(self, s_name):
        """
        Set the initial state, if not done in the initializer.

        One way or another, the initial state must be done before running the
        state machine.
        """
        self._current_state = s_name
        self._initial = s_name
        self.state_dict(s_name)['_initial'] = True

    @property
    def current_state(self):
        """Return the current state of the FSM."""
        return self._current_state
    @current_state.setter
    def current_state(self, value):
        self._current_state = value

    @property
    def accepting(self, s_name=None):
        if s_name == None:
            s_name = self.current_state
        record = self._state_records[s_name]
        return '_accepting' in record.state_dict and \
                record.state_dict['_accepting']

    def state_dict(self, s_name):
        """
        Access the dictionary associated with the state.

        Args:
            s_name: Name of the state.

        Returns:
            The dictionary associated with s_name.

        Raises:
            KeyError: s_name is not a registered state.
        """
        return self._state_records[s_name].state_dict

    # State forwards
    def _enter(self):
        """Call the current state's on_enter handler."""

        self.on_enter_state(self._current_state)

    def on_enter_state(self, s_name):
        """
        Notification that a state has been entered.

        Args:
            s_name: Name of the state that has been entered.

        Derived classes implementing this handler should call
        super().on_enter_state().
        """
        pass

    def _exit(self):
        """Call the current state's on_exit handler."""

        self.on_exit_state(self._current_state)

    def on_exit_state(self, s_name):
        """
        Notification that a state has been exited.

        Args:
            s_name: is the name of the state being exited.

        Derived classes implementing this handler should call
        super().on_exit_state().
        """
        pass

    def on_no_transition(self, s_name):
        """
        Handle the no_transition notification.

        The on_no_transition handler is called when a state has no acceptable
        transitions available.

        Args:
            s_name: is the name of the state being exited.

        Derived classes implementing this handler should call
        super().on_no_transition().
        """
        if self.accepting:
            raise Accept()
        msg = "Cannot transition from state {st}"
        raise Reject(msg.format(st=self.current_state))

    def add_state(self, s_name, state_data=None, activity=None,
                  guard=lambda fsm:True, accepting=False):
        """
        Add a new state or replace an existing one.

        Args:
            s_name: Name of the state, must be hashable.
            state_data: Data associated with this state.
            activity: Callable to execute when in the state.  The call
                signature is activity(statemachine, current_state, state)
        """
        record = _StateRecord(s_name, state_data, activity, guard=guard,
                              accepting=accepting)
        self._state_records[s_name] = record

    def remove_state(self, s_name):
        """
        Remove a state.

        Args:
            s_name: Name of the state to remove.
        """
        del self._state_records[s_name]

    def add_transition(self, exiting, entering,
                       test=transition_always, label=None):
        """
        Add a transition from one state to another.

        Args:
            exiting: Name of the state for which transition is exiting.
            entering: Name of the state for which transition is entering.
            test: Function to test if the transition is allowed to be made.
            label: Label for this transtion in diagrams.

        test() should return True if the transition should be allowed, or False
        otherwise.

        Note:
            If the transition already exists it will be replaced.

        Raises:
            KeyError

        The test function will be called with three positional arguments:
            fsm: StateMachine instance that owns the vertices.
            exiting: The state being left.
            entering: The state being entered.
        """
        record = self._state_records[exiting]
        if entering not in record.transition_info:
            record.transitions.append(entering)

        record.transition_info[entering] = _TransitionRecord(test, label)

    def remove_transition(self, exiting, entering):
        """
        Remove a transition.

        Args:
            exiting: Name of the state for which transition is exiting.
            entering: Name of the state for which transition is entering.

        Raises:
            KeyError

        TODO: Needs test.
        """
        record = self._state_records[exiting]
        record.transition.remove(entering)
        del record.transition_info[entering]

    def on_pre_select_transition(self, s_name, candidate_s_names):
        """
        Handle a notification that a transition is about to be selected.

        This handler may be useful to record the list of transitions.

        Args:
            s_name: The name of the current state.
            candidate_s_names: List of transition candidates which have passed
                any applicable transition tests.

        Derived classes implementing this handler should call
        super().on_pre_select_transition().
        """
        pass

    def select_transition(self, s_name, candidate_s_names):
        """
        Select a transition state from the list of qualifying states.

        Args:
            s_name: The name of the current state.
            candidate_s_names: List of transition candidates which have passed
                any applicable transition tests.

        Returns:
            The choice of state to receive the transition.
        """
        return candidate_s_names[0]

    def on_transition(self, exiting, entering):
        """
        Notification that a transition is in effect.

        This is called after on_exit for the exiting state, and before on_enter
        for the entering state.

        Args:
            exiting: State name of the previously active state.
            entering: State name of the next active state.

        Derived classes implementing this handler should call
        super().on_transition().
        """
        pass

    def _do_transition(self, next_state):
        """Final execution of a transition."""
        self._current_state = next_state
        self._enter()

    def _transition_multiple(self, allowed_transitions):
        """
        """
        self.on_pre_select_transition(self.current_state, allowed_transitions)

        if len(allowed_transitions) == 0:
            self.on_no_transition(self.current_state)
            return

        next_state = self.select_transition(self.current_state,
                                            allowed_transitions)
        self.on_transition(self.current_state, next_state)

        self._do_transition(next_state)

    def _transition(self):
        """
        Handle the details of transitioning.

        For internal use.
        """
        record = self._state_records[self.current_state]

        # Transitioning
        allowed_transitions = []
        for next_trans in record.transitions:
            if record.transition_info[next_trans].test(self,
                                                       self.current_state,
                                                       next_trans):
                next_record = self._state_records[next_trans]
                if next_record.guard(self):
                    allowed_transitions.append(next_trans)

        self._exit()

        self._transition_multiple(allowed_transitions)

    def _do_activity(self):
        """
        Run the activity associated with the state.

        For internal use.
        """
        if self.current_state in self._state_records:
            activity = self._state_records[self.current_state].activity
            if activity == None:
                return

            activity(self)

    def _run(self):
        """
        Helper function for run.

        This is the heart of run(), but without the exception handling.  _run
        can be called recursively, e.g. in pushdown automata.
        """
        assert self._current_state, "Initial state not set."
        self._enter()
        while True:
            try:
                self._do_activity()
                self._transition()

            except Backtrack:
                # TODO: Handle this without referencing backtracking.
                if not self._backtrack():
                    self.on_exhausted()

                    raise Reject("Backtracking exhausted.")

    def run(self):
        """Run the state machine"""

        try:
            self._run()

        except Accept as exc:
            self._exit()
            self.on_accept(exc)
            return True
        except Reject as exc:
            self._exit()
            self.on_reject(exc)
            return False

    def on_accept(self, exc):
        """
        Called when the input has been accepted.

        Args:
            exc: Exception triggering this event.

        Derived classes implementing this handler should call
        super().on_accept().
        """
        pass

    def on_reject(self, exc):
        """
        Called when the input has been rejected.

        Args:
            exc: Exception triggering this event.

        Derived classes implementing this handler should call
        super().on_reject().
        """
        pass

    def accept_test(self):
        return True

    # Container protocol special methods

    def __len__(self):
        """Return the number of known states."""
        return len(self._state_records)

    def __contains__(self, s_name):
        """Test if a state is in the state machine."""

        return s_name in self._state_records

if __name__ == '__main__':
    import sys
    sys.stderr.write("Pycog statemachine.py module is not intended to run "
                     "standalone.")


# Pig Latin Transalator
# this litte program ask for a sintance and print thesintance back in pig latin.

# get sintance from user

original = input("Please enter a sentence: ").strip().lower()

# split sinteance in to words

words= original.split()

# lop words and convert to pig latin

new_words = []

for word in words:
    if word[0] in "aiou":
       new_word = word + "yay"
       new_words.append(new_word)
    else:
        vowel_pos = 0
        for letter in word:
            if letter not in "aiou":
                vowel_pos = vowel_pos + 1
            else:
                break
            cons = word[:vowel_pos]
            the_rest = word[vowel_pos:]
            new_word = the_rest + cons + "ay"
            new_words.append(new_word)

# stck words back togather

output = " ".join(new_words)

# output the file string

print(output)


def rule1(symptons):
    if "fever" in symptons and "cough" in symptons and "headache" in symptons:
        return "You have flu"

def rule2(symptons):
    if "dizziness" in symptons and "nausea" in symptons and "weakness" in symptons:
        return "you may have low iron content"

def getDia(symptons):
    rules=[rule1,rule2]
    results=[]
    for rule in rules:
        result=rule(symptons)
        if result:
            results.append(result)
    if len(results)==0:
        return "sorry we couldnt dia"
    else: return results

def diagonsis():
    symp=input("Symptons: ")
    symptons=symp.lower().split(" ")
    result=getDia(symptons)
    print(result)

diagonsis()


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

# In[1]:

from __future__ import print_function

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
import tensorflow as tf
#from keras.layers import Input, Dense, Lambda
#from keras.models import Model
#from keras import backend as K
#from keras import metrics
#from keras.datasets import mnist
#from keras.layers import Dense, Dropout, Flatten
#from keras.layers import Conv2D, MaxPooling2D
#from keras.layers import Conv2DTranspose,Reshape
#import keras.layers as layers

from PIL import Image, ImageOps
import os
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from tensorflow.keras import layers, losses
from tensorflow.keras.models import Model
from tensorflow.keras import backend as K
from tensorflow.keras import metrics

# ## Load the dataset

# In[2]:

PATH = "./mergedConstellationsEasyStars2Outline_Large/"
checkpoint_path = "./training_ckpt/cp-{epoch:04d}.ckpt"
imageSaveDir = "./runVAE_Easy2Outline"
if not os.path.exists(imageSaveDir):
    os.makedirs(imageSaveDir)

# In[3]:

#get_ipython().run_line_magic('matplotlib', 'inline')

IMG_WIDTH = 256
IMG_HEIGHT = 256

batch_size = 256

original_dim = IMG_WIDTH*IMG_HEIGHT
latent_dim = 512
EPOCHS = epochs = 50
epsilon_std = 1.0

BUFFER_SIZE = 400

# In[4]:

def load(image_file):
    image = tf.io.read_file(image_file)
    image = tf.image.decode_jpeg(image)

    w = tf.shape(image)[1]

    w = w // 2
    real_image = image[:, :w, :]
    input_image = image[:, w:, :]
    print(type(real_image))

    input_image = tf.cast(input_image, np.float32)
    real_image = tf.cast(real_image, np.float32)

    return input_image, real_image

# In[5]:

inp, re = load(PATH+'train/object_0_0.jpg')
# casting to int for matplotlib to show the image
plt.figure()
plt.imshow(np.asarray(inp)/255.0)
plt.figure()
plt.imshow(np.asarray(re)/255.0)

# In[6]:

def resize(input_image, real_image, height, width):
    input_image = tf.image.resize(input_image, [height, width],
                                method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
    real_image = tf.image.resize(real_image, [height, width],
                               method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)

    return input_image, real_image

# In[7]:

def random_crop(input_image, real_image):
    stacked_image = tf.stack([input_image, real_image], axis=0)
    cropped_image = tf.image.random_crop(
      stacked_image, size=[2, IMG_HEIGHT, IMG_WIDTH, 3])

    return cropped_image[0], cropped_image[1]

# In[8]:

@tf.function()
def random_jitter(input_image, real_image):
    # resizing to 286 x 286 x 3
    input_image, real_image = resize(input_image, real_image, 286, 286)

    # randomly cropping to 256 x 256 x 3
    input_image, real_image = random_crop(input_image, real_image)

    if tf.random.uniform(()) > 0.5:
        # random mirroring
        input_image = tf.image.flip_left_right(input_image)
        real_image = tf.image.flip_left_right(real_image)

    return input_image, real_image

# In[9]:

# normalizing the images to [0, 1]

def normalize(input_image, real_image):
    input_image = input_image/255.0#(input_image / 127.5) - 1
    real_image = real_image/255.0#(real_image / 127.5) - 1

    return input_image, real_image

# In[10]:

def load_image_train(image_file):
    input_image, real_image = load(image_file)
    input_image, real_image = random_jitter(input_image, real_image)
    input_image, real_image = normalize(input_image, real_image)

    return input_image, real_image

# In[11]:

def load_image_test(image_file):
    input_image, real_image = load(image_file)
    input_image, real_image = resize(input_image, real_image,
                                   IMG_HEIGHT, IMG_WIDTH)
    input_image, real_image = normalize(input_image, real_image)

    return input_image, real_image

# ## Input Pipeline

# In[12]:

train_dataset = tf.data.Dataset.list_files(PATH+'train/*.jpg')
train_dataset = train_dataset.map(load_image_train)
train_dataset = train_dataset.shuffle(BUFFER_SIZE)
train_dataset = train_dataset.batch(1)

# In[13]:

test_dataset = tf.data.Dataset.list_files(PATH+'test/*.jpg')
test_dataset = test_dataset.map(load_image_test)
test_dataset = test_dataset.batch(1)

# ## Convert to numpy

# In[14]:

train_np = np.stack(list(train_dataset))
test_np = np.stack(list(test_dataset))

# In[15]:

noise_train = np.squeeze(train_np[:,0,:,:,:])
x_train = np.squeeze(train_np[:,1,:,:,:])

# In[16]:

noise_test = np.squeeze(test_np[:,0,:,:,:])
x_test = np.squeeze(test_np[:,1,:,:,:])

# In[17]:

from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()

# In[18]:

#encoder part

x_noise = layers.Input(shape=(256, 256, 3,), name='input_layer')

# Block-1
x = layers.Conv2D(32, kernel_size=3, strides= 2, padding='same', name='conv_1')(x_noise)
x = layers.BatchNormalization(name='bn_1')(x)
x = layers.LeakyReLU(name='lrelu_1')(x)

# Block-2
x = layers.Conv2D(64, kernel_size=3, strides= 2, padding='same', name='conv_2')(x)
x = layers.BatchNormalization(name='bn_2')(x)
x = layers.LeakyReLU(name='lrelu_2')(x)

# Block-3
x = layers.Conv2D(64, 3, 2, padding='same', name='conv_3')(x)
x = layers.BatchNormalization(name='bn_3')(x)
x = layers.LeakyReLU(name='lrelu_3')(x)

# Block-4
x = layers.Conv2D(64, 3, 2, padding='same', name='conv_4')(x)
x = layers.BatchNormalization(name='bn_4')(x)
x = layers.LeakyReLU(name='lrelu_4')(x)

# Block-5
x = layers.Conv2D(64, 3, 2, padding='same', name='conv_5')(x)
x = layers.BatchNormalization(name='bn_5')(x)
x = layers.LeakyReLU(name='lrelu_5')(x)

# Final Block
flatten = layers.Flatten()(x)
z_mean = layers.Dense(512, name='mean')(flatten)
z_log_var = layers.Dense(512, name='log_var')(flatten)

#reparameterization trick
def sampling(args):
    z_mean, z_log_var = args
    epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim), mean=0.,
                              stddev=epsilon_std)
    return z_mean + K.exp(z_log_var / 2) * epsilon

# note that "output_shape" isn't necessary with the TensorFlow backend
z = layers.Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])

# In[19]:

#decoder part
# we instantiate these layers separately so as to reuse them later
z= layers.Reshape([1,1,latent_dim])(z)

x = layers.Dense(4096)(z)
x = layers.Reshape((8,8,64))(x)

# Block-1
x = layers.Conv2DTranspose(64, 3, strides= 2, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)

# Block-2
x = layers.Conv2DTranspose(64, 3, strides= 2, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)

# Block-3
x = layers.Conv2DTranspose(64, 3, 2, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)

# Block-4
x = layers.Conv2DTranspose(32, 3, 2, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)

# Block-5
x_out = layers.Conv2DTranspose(3, 3, 2,padding='same', activation='sigmoid')(x)

# instantiate VAE model
vae = Model(x_noise, x_out)
vae.summary()

# In[20]:

#from keras.utils.vis_utils import plot_model
#plot_model(vae, to_file='model1.png',show_shapes=True)

# In[21]:

# Compute VAE loss
def VAE_loss(x_origin,x_out):
    x_origin=K.flatten(x_origin)
    x_out=K.flatten(x_out)
    xent_loss = original_dim * metrics.binary_crossentropy(x_origin, x_out)
    kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
    vae_loss = K.mean(xent_loss + kl_loss)
    return vae_loss
vae.compile(optimizer='adam', loss=VAE_loss)

# In[22]:

vae.fit(noise_train,x_train,
        shuffle=True,
        epochs=epochs,
        batch_size=batch_size,
        validation_data=(noise_test, x_test))

# In[30]:

def generate_images_test(x_testImg, noise_testImg, x_outImg, saveFolder, index):

    inp = Image.fromarray((noise_testImg*255).astype(np.uint8))
    inp.save(f"{saveFolder}/input_{index}.jpg")

    tar = Image.fromarray((x_testImg*255).astype(np.uint8))
    tar.save(f"{saveFolder}/target_{index}.jpg")

    pred = Image.fromarray((x_outImg*255).astype(np.uint8))
    pred.save(f"{saveFolder}/predictions_{index}.jpg")

# In[31]:

saveDir = PATH+"predictions_vae"
x_out=vae.predict(x_test)
for i in range(100):
    generate_images_test(x_test[i], noise_test[i], x_out[i], saveDir, i)


import email.parser

import pytest

from .utils import append, run

def build_project(*args, check=True):
    if not args:
        args = ["-w"]

    return run("build", *args, check=check)

@pytest.mark.slow()
def test_build(new_project):
    """
    Build a fake project end-to-end and verify wheel contents.
    """
    append(
        new_project / "pyproject.toml",
        """
[tool.hatch.metadata.hooks.fancy-pypi-readme]
content-type = "text/markdown"

[[tool.hatch.metadata.hooks.fancy-pypi-readme.fragments]]
text = '''# Level 1

Fancy *Markdown*.
'''

[[tool.hatch.metadata.hooks.fancy-pypi-readme.fragments]]
text = "---\\nFooter"
""",
    )

    build_project()

    whl = new_project / "dist" / "my_app-1.0-py2.py3-none-any.whl"

    assert whl.exists()

    run("wheel", "unpack", whl)

    metadata = email.parser.Parser().parsestr(
        (
            new_project / "my_app-1.0" / "my_app-1.0.dist-info" / "METADATA"
        ).read_text()
    )

    assert "text/markdown" == metadata["Description-Content-Type"]
    assert (
        "# Level 1\n\nFancy *Markdown*.\n---\nFooter" == metadata.get_payload()
    )

@pytest.mark.slow()
def test_invalid_config(new_project):
    """
    Missing config makes the build fail with a meaningful error message.
    """
    pyp = new_project / "pyproject.toml"

    # If we leave out the config for good, the plugin doesn't get activated.
    pyp.write_text(
        pyp.read_text() + "[tool.hatch.metadata.hooks.fancy-pypi-readme]"
    )

    out = build_project(check=False)

    assert "hatch_fancy_pypi_readme.exceptions.ConfigurationError:" in out
    assert (
        "tool.hatch.metadata.hooks.fancy-pypi-readme.content-type "
        "is missing." in out
    )
    assert (
        "tool.hatch.metadata.hooks.fancy-pypi-readme.fragments "
        "is missing." in out
    )


# Problem 6: Sum square difference
# The sum of the squares of the first ten natural numbers is,

# 12 + 22 + ... + 102 = 385
# The square of the sum of the first ten natural numbers is,

# (1 + 2 + ... + 10)2 = 552 = 3025
# Hence the difference between the sum of the squares of the first ten natural numbers and the square
# of the sum is 3025 − 385 = 2640.

# Find the difference between the sum of the squares of the first n natural numbers and the square of sum.

num = int(input("Provide the number: "))

sumSquared = 0
sumNum = 0

for i in range(1,num+1):
    sumSquared += (i*i)
    sumNum += i

sumNum *= sumNum
diff = sumNum - sumSquared

print(diff)


# Multilinear Regression
import pandas as pd
import numpy as np

# loading the data
Computer_Data = pd.read_csv("C:/data science/multi linear regression/Datasets_MLR/Computer_Data.csv")

# Exploratory data analysis:
# 1. Measures of central tendency
# 2. Measures of dispersion
# 3. Third moment business decision
# 4. Fourth moment business decision
# 5. Probability distributions of variables
# 6. Graphical representations (Histogram, Box plot, Dot plot, Stem & Leaf plot, Bar plot, etc.)

Computer_Data.drop("cd", inplace=True, axis=1)
Computer_Data.drop("multi", inplace=True, axis=1)
Computer_Data.drop("premium", inplace=True, axis=1)

Computer_Data.describe()

#Graphical Representation
import matplotlib.pyplot as plt # mostly used for visualization purposes

# Speed
plt.bar(height = Computer_Data.speed, x = np.arange(1, 6260, 1))
plt.hist(Computer_Data.speed) #histogram
plt.boxplot(Computer_Data.speed) #boxplot

# PRICE
plt.bar(height = Computer_Data.price, x = np.arange(1, 6260, 1))
plt.hist(Computer_Data.price) #histogram
plt.boxplot(Computer_Data.price) #boxplot

# Jointplot
import seaborn as sns
sns.jointplot(x=Computer_Data['speed'], y=Computer_Data['price'])

# Countplot
plt.figure(1, figsize=(16, 10))
sns.countplot(Computer_Data['speed'])

# Q-Q Plot
from scipy import stats
import pylab
stats.probplot(Computer_Data.price, dist = "norm", plot = pylab)
plt.show()

# Scatter plot between the variables along with histograms
import seaborn as sns
sns.pairplot(Computer_Data.iloc[:, :])

# Correlation matrix
Computer_Data.corr()

# we see there exists High collinearity between input variables especially between
# [HP & SP], [VOL & WT] so there exists collinearity problem

# preparing model considering all the variables

import statsmodels.api as sm
import statsmodels.formula.api as smf
import numpy as np
import pandas# for regression model

ml1 = smf.ols('price ~ speed + hd + ram + screen + ads + trend', data = Computer_Data).fit() # regression model

# Summary
ml1.summary()
# p-values for WT, VOL are more than 0.05

# Checking whether data has any influential values
# Influence Index Plots

sm.graphics.influence_plot(ml1)
# Studentized Residuals = Residual/standard deviation of residuals
# index  1043 is showing high influence so we can exclude that entire row

Computer_Data_new = Computer_Data.drop(Computer_Data.index[[1043]])

# Preparing model
ml_new = smf.ols('price ~ speed + hd + ram + screen + ads + trend', data = Computer_Data_new).fit()

# Summary
ml_new.summary()

# Check for Colinearity to decide to remove a variable using VIF
# Assumption: VIF > 10 = colinearity
# calculating VIF's values of independent variables
rsq_sp = smf.ols('speed ~ hd + ram + screen + ads + trend', data = Computer_Data).fit().rsquared
vif_sp = 1/(1 - rsq_sp)

rsq_hd = smf.ols('hd ~ speed + ram + screen + ads + trend', data = Computer_Data).fit().rsquared
vif_hd = 1/(1 - rsq_hd)

rsq_rm = smf.ols('ram ~ speed + hd + screen + ads + trend', data = Computer_Data).fit().rsquared
vif_rm = 1/(1 - rsq_rm)

rsq_sc = smf.ols('screen ~ speed + hd + ram + ads + trend', data = Computer_Data).fit().rsquared
vif_sc = 1/(1 - rsq_sc)

rsq_ad = smf.ols('ads ~ speed + hd + ram + screen + trend', data = Computer_Data).fit().rsquared
vif_ad = 1/(1 - rsq_rm)

rsq_tr = smf.ols('trend ~ speed + hd + ram + screen + ads', data = Computer_Data).fit().rsquared
vif_tr = 1/(1 - rsq_tr)

# Storing vif values in a data frame
d1 = {'Variables':['speed', 'hd', 'ram', 'screen', 'ads', 'trend'], 'VIF':[vif_sp, vif_hd, vif_rm, vif_sc, vif_ad, vif_tr]}
Vif_frame = pd.DataFrame(d1)
Vif_frame
# As hd is having highest VIF value, we are going to drop this from the prediction model

# Final model
final_ml = smf.ols('price ~ speed + ram + screen + ads + trend', data = Computer_Data).fit()
final_ml.summary()

# Prediction
pred = final_ml.predict(Computer_Data)

# Q-Q plot
res = final_ml.resid
sm.qqplot(res)
plt.show()

# Q-Q plot
stats.probplot(res, dist = "norm", plot = pylab)
plt.show()

# Residuals vs Fitted plot
sns.residplot(x = pred, y = Computer_Data.price, lowess = True)
plt.xlabel('Fitted')
plt.ylabel('Residual')
plt.title('Fitted vs Residual')
plt.show()

sm.graphics.influence_plot(final_ml)

### Splitting the data into train and test data
from sklearn.model_selection import train_test_split
Computer_Data_train, Computer_Data_test = train_test_split(Computer_Data, test_size = 0.2) # 20% test data

# preparing the model on train data
model_train = smf.ols("price ~ speed + hd + ram + screen + ads + trend", data = Computer_Data_train).fit()

# prediction on test data set
test_pred = model_train.predict(Computer_Data_test)

# test residual values
test_resid = test_pred - Computer_Data_test.price
# RMSE value for test data
test_rmse = np.sqrt(np.mean(test_resid * test_resid))
test_rmse

# train_data prediction
train_pred = model_train.predict(Computer_Data_train)

# train residual values
train_resid  = train_pred - Computer_Data_train.price
# RMSE value for train data
train_rmse = np.sqrt(np.mean(train_resid * train_resid))
train_rmse