# -*- coding: UTF-8 -*-
import os
from collections import OrderedDict

from PyQt5.QtWidgets import (QDialog, QAbstractItemView, QVBoxLayout,
    QHBoxLayout, QLabel, QWidget, QTreeView, QLineEdit, QTableView, QCompleter)
from PyQt5.QtGui import QStandardItem, QStandardItemModel, QPixmap
from PyQt5.QtCore import Qt, pyqtSlot, QModelIndex

from neox.commons.qt_models import get_simple_model
from neox.commons.forms import GridForm
from neox.commons.buttons import ActionButton

__all__ = ['QuickDialog', 'SearchDialog', 'HelpDialog',
    'CategoryDialog',
    'ProductDialog','FactoryIcons']

current_dir = os.path.dirname(__file__)
par_dir = os.path.dirname(current_dir)

_SIZE = (500, 200)
_TSIZE = (800,200)

class QuickDialog(QDialog):

    def __init__(self, parent, kind, string=None, data=None, widgets=None,
        icon=None, size=None, readonly=False, disable_cancel=False,
        active_widget=None, cancel_message=None, message_bar=None):
        super(QuickDialog, self).__init__(parent)
        # Size arg is in deprecation

        if not size:
            size = _SIZE
        self.factory = None
        self.readonly = readonly
        self.parent = parent
        self.active_widget = active_widget
        self.parent_model = None
        titles = {
            'warning': self.tr('Warning...'),
            'info': self.tr('Information...'),
            'action': self.tr('Action...'),
            'help': self.tr('Help...'),
            'error': self.tr('Error...'),
            'question': self.tr('Question...'),
            'selection': self.tr('Selection...'),
            None: self.tr('Dialog...')
        }
        self.setWindowTitle(titles[kind])
        self.setModal(True)
        self.setParent(parent)
        self.factory = FactoryIcons()
        self.default_widget_focus = None
        self.kind = kind
        self.widgets = widgets
        self.data = data
        self.disable_cancel = disable_cancel
        self.cancel_message = cancel_message
        self.message_bar = message_bar
        string_widget = None
        data_widget = None
        _buttons = None
        row_stretch = 1
        main_vbox = QVBoxLayout()

        self.sub_hbox = QHBoxLayout()

        # Add main message
        if string:
            # For simple dialog
            string_widget = QLabel(string)

        if kind == 'help':
            data_widget = widgets[0]
        elif kind == 'action':
            if widgets:
                data_widget = widgets[0]
            else:
                data_widget = GridForm(parent, OrderedDict(data))
        elif kind == 'selection':
            self.name = data['name']
            data_widget = self.set_selection(parent, data)
        elif widgets:
            data_widget = GridForm(parent, OrderedDict(widgets))

        if string_widget:
            main_vbox.addWidget(string_widget, 0)

        if data_widget:
            if isinstance(data_widget, QWidget):
                row_stretch += 1
                size = (size[0], size[1] + 200)
                self.sub_hbox.addWidget(data_widget, 0)
            else:
                self.sub_hbox.addLayout(data_widget, 0)

        self.ok_button = ActionButton('ok', self.dialog_accepted)
        self.ok_button.setFocus()
        self.cancel_button = ActionButton('cancel', self.dialog_rejected)

        _buttons = []
        if kind in ('info', 'help', 'warning', 'question', 'error'):
            if kind in ('warning', 'question'):
                _buttons.append(self.cancel_button)
            _buttons.append(self.ok_button)
        elif kind in ('action', 'selection'):
            _buttons.extend([self.cancel_button, self.ok_button])

        self.buttonbox = QHBoxLayout()
        for b in _buttons:
            self.buttonbox.addWidget(b, 1)

        main_vbox.addLayout(self.sub_hbox, 0)
        main_vbox.addLayout(self.buttonbox, 1)
        main_vbox.insertStretch(row_stretch, 0)

        self.setLayout(main_vbox)
        self.setMinimumSize(*size)

        if kind in ('info', 'error'):
            self.show()

    def exec_(self, args=None):
        res = None
        self.parent.releaseKeyboard()
        res = super(QuickDialog, self).exec()
        if self.kind == 'action':
            pass
        return res

    def show(self):
        super(QuickDialog, self).show()
        self.parent.releaseKeyboard()
        self.ok_button.setFocus()
        if self.default_widget_focus:
            self.default_widget_focus.setFocus()
            if hasattr(self.default_widget_focus, 'setText'):
                self.default_widget_focus.setText('')
        else:
            self.setFocus()

    def hide(self):
        super(QuickDialog, self).hide()
        self.parent.setFocus()

    def set_info(self, info):
        if hasattr(self, 'label_info'):
            self.label_info.setText(info)

    def set_widgets(self, widgets):
        if widgets:
            # Set default focus to first widget created
            self.default_widget_focus = widgets[0]

    def closeEvent(self, event):
        super(QuickDialog, self).closeEvent(event)
        if self.message_bar and self.cancel_message:
            self.message_bar.set(self.cancel_message)
        if self.active_widget:
            self.active_widget.setFocus()

    def dialog_rejected(self):
        self.parent.setFocus()
        if self.active_widget:
            self.active_widget.setFocus()
        self.setResult(0)
        self.hide()

    def dialog_accepted(self):
        if self.kind in ('action', 'selection', 'warning', 'question'):
            self.setResult(1)
            self.done(1)
        self.hide()

    def keyPressEvent(self, event):
        key = event.key()
        if key == Qt.Key_Escape:
            if self.disable_cancel==False:
                if self.message_bar and self.cancel_message:
                    self.message_bar.set(self.cancel_message)
                if self.active_widget:
                    self.active_widget.setFocus()
                self.dialog_rejected()
            return
        else:
            super(QuickDialog, self).keyPressEvent(event)

    def set_selection(self, obj, data):
        self.set_simple_model()
        setattr(obj, data['name'] + '_model', self.data_model)
        self.parent_model = data.get('parent_model')
        self.treeview = QTreeView()
        self.treeview.setRootIsDecorated(False)
        self.treeview.setColumnHidden(0, True)
        self.treeview.setItemsExpandable(False)
        self.treeview.setAlternatingRowColors(True)
        self.treeview.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.treeview.setModel(self.data_model)
        self.treeview.clicked.connect(self.field_selection_changed)
        self.treeview.activated.connect(self.field_selection_changed)

        self.update_values(self.data['values'])

        # By default first row must be selected
        item = self.data_model.item(0, 0)
        idx = self.data_model.indexFromItem(item)
        self.treeview.setCurrentIndex(idx)
        return self.treeview

    def update_values(self, values):
        self.data_model.removeRows(0, self.data_model.rowCount())
        self._insert_items(self.data_model, values)
        self.treeview.resizeColumnToContents(0)

    def set_simple_model(self):
        self.data_model = QStandardItemModel(0, len(self.data['heads']), self)
        _horizontal = Qt.Horizontal
        for i, h in enumerate(self.data['heads'], 0):
            self.data_model.setHeaderData(i, _horizontal, h)

    def _insert_items(self, model, values):
        for value in values:
            row = []
            for v in value:
                itemx = QStandardItem(v)
                itemx.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
                row.append(itemx)
            self.data_model.insertRow(0, row)
        self.data_model.sort(0, Qt.AscendingOrder)

    @pyqtSlot(QModelIndex)
    def field_selection_changed(self, qm_index):
        if not self.readonly:
            item_id = self.data_model.item(qm_index.row(), 0).text()
            item_name = self.data_model.item(qm_index.row(), 1).text()

            if self.parent_model is not None:
                self.parent_model[self.name] = item_id
            if hasattr(self.parent, 'field_' + self.name):
                field = getattr(self.parent, 'field_' + self.name)
                if hasattr(field, 'setText'):
                    field.setText(item_name)
            else:
                setattr(self.parent, 'field_' + self.name + '_name', item_name)
            setattr(self.parent, 'field_' + self.name + '_id', int(item_id))
            action = getattr(self.parent, 'action_' + self.name + '_selection_changed')
            action()
        self.dialog_accepted()

class SearchDialog(QDialog):

    def __init__(self, parent, headers, values, on_activated,
            hide_headers=False, completion_column=None, title=None):
        super(SearchDialog, self).__init__(parent)
        self.parent = parent
        self.headers = headers
        self.values = values
        if not title:
            title = self.tr('Search Products...')
        self.setWindowTitle(title)

        self._product_line = QLineEdit()
        self.table_view = QTableView()

        button_cancel = ActionButton('cancel', self.on_reject)
        vbox = QVBoxLayout()
        hbox = QHBoxLayout()
        hbox.addWidget(button_cancel)
        vbox.addWidget(self._product_line)
        vbox.addLayout(hbox)
        self.setLayout(vbox)
        self.completer = QCompleter()
        self.treeview_search_product = QTreeView()
        if hide_headers:
            col_headers = self.treeview_search_product.header()
            col_headers.hide()
        self.completer.setPopup(self.treeview_search_product)
        self._product_line.setCompleter(self.completer)
        self.set_model()

        self.completer.activated.connect(self.on_accept)
        self.completer.setFilterMode(Qt.MatchStartsWith)
        self.completer.setCaseSensitivity(Qt.CaseInsensitive)
        self.completer.setCompletionColumn(2)
        self.completer.activated.connect(on_activated)

    def set_model(self):
        headers_name = [h[1] for h in self.headers]
        self.model = get_simple_model(self.parent, self.values, headers_name)
        self.completer.setModel(self.model)

    def get_selected_index(self):
        model_index = self._get_model_index()
        idx = self.model.index(model_index.row(), 0)
        return idx.data()

    def get_selected_data(self):
        model_index = self._get_model_index()
        data = {}
        i = 0
        for h, _ in self.headers:
            data[h] = self.model.index(model_index.row(), i).data()
            i += 1
        return data

    def _get_model_index(self):
        item_view = self.completer.popup()
        index = item_view.currentIndex()
        proxy_model = self.completer.completionModel()
        model_index = proxy_model.mapToSource(index)
        return model_index

    def on_accept(self):
        self.accept()

    def on_reject(self):
        self.reject()

class HelpDialog(QuickDialog):

    def __init__(self, parent):
        self.treeview = QTreeView()
        self.treeview.setRootIsDecorated(False)
        self.treeview.setAlternatingRowColors(True)
        self.treeview.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.treeview.setEditTriggers(QAbstractItemView.NoEditTriggers)
        super(HelpDialog, self).__init__(parent, 'help', widgets=[self.treeview],
            size=(400, 500))
        self.set_info(self.tr('Keys Shortcuts...'))
        self.hide()

    def set_shortcuts(self, shortcuts):
        model = self._help_model(shortcuts)
        self.treeview.setModel(model)
        header = self.treeview.header()
        header.resizeSection(0, 250)

    def _help_model(self, shortcuts):
        model = QStandardItemModel(0, 2, self)
        model.setHeaderData(0, Qt.Horizontal, self.tr('Action'))
        model.setHeaderData(1, Qt.Horizontal, self.tr('Shortcut'))

        for short in shortcuts:
            model.insertRow(0)
            model.setData(model.index(0, 0), short[0])
            model.setData(model.index(0, 1), short[1])
        return model

class ProductDialog(QDialog):
    def __init__(self, parent, methods=None, widget=None, title=None):
        super(ProductDialog, self).__init__(parent)
        vbox = QVBoxLayout()
        self.parent = parent
        width = 1000
        height = 300
        if widget:
            vbox.addLayout(widget.products, 0)

            count = len(widget.products)

            #width = count * 250
            if count > 4:
                #width = 1000
                height = 600

        self.setWindowTitle(title)
        self.setLayout(vbox)
        self.resize(width, height)

    def keyPressEvent(self, event):
        key = event.key()
        if key == Qt.Key_Escape:
            self.dialog_rejected()
            return
        else:
            super(ProductDialog, self).keyPressEvent(event)

    def on_accept(self):
        self.accept()

    def on_reject(self):
        self.reject()

    def dialog_rejected(self):
        self.parent.setFocus()
        self.setResult(0)
        self.hide()

    def dialog_accepted(self):
        self.setResult(1)
        self.done(1)
        self.parent.setFocus()
        self.hide()

    def closeEvent(self, event):
        super(ProductDialog, self).closeEvent(event)

    def exec_(self, args=None):
        res = None
        self.parent.releaseKeyboard()
        res = super(ProductDialog, self).exec_()
        return res

    def show(self):
        super(ProductDialog, self).show()
        self.parent.releaseKeyboard()
        self.cancel_button.setFocus()
        self.setFocus()

    def hide(self):
        super(ProductDialog, self).hide()
        self.parent.setFocus()

class CategoryDialog(QDialog):
    def __init__(self, parent, methods=None, widget=None, title=None):
        super(CategoryDialog, self).__init__(parent)
        vbox = QVBoxLayout()
        self.parent = parent
        width = 500
        height = 300
        if widget:
            vbox.addLayout(widget.categories, 0)
            count = len(widget.categories)
            if count > 4:
                width = 1000
                height = 600
        self.setWindowTitle(title)
        self.setLayout(vbox)
        self.resize(width, height)

    def keyPressEvent(self, event):
        key = event.key()
        if key == Qt.Key_Escape:
            self.dialog_rejected()
            return
        else:
            super(CategoryDialog, self).keyPressEvent(event)

    def on_accept(self):
        self.accept()

    def on_reject(self):
        self.reject()

    def dialog_rejected(self):
        self.parent.setFocus()
        self.setResult(0)
        self.hide()

    def dialog_accepted(self):
        self.setResult(1)
        self.done(1)
        self.parent.setFocus()
        self.hide()

    def closeEvent(self, event):
        super(CategoryDialog, self).closeEvent(event)

    def exec_(self, args=None):
        res = None
        self.parent.releaseKeyboard()
        res = super(CategoryDialog, self).exec_()
        return res

    def show(self):
        super(CategoryDialog, self).show()
        self.parent.releaseKeyboard()
        self.cancel_button.setFocus()
        self.setFocus()

    def hide(self):
        super(CategoryDialog, self).hide()
        self.parent.setFocus()

class AgentDialog(QDialog):
    def __init__(self, parent, methods=None,widget=None,title=None):
        super(AgentDialog, self).__init__(parent)
        vbox = QVBoxLayout()
        self.parent = parent
        if widget:
            vbox.addLayout(widget.agents,0)
        self.setWindowTitle(title)
        self.setLayout(vbox)

    def keyPressEvent(self, event):
        key = event.key()
        if key == Qt.Key_Escape:
            self.dialog_rejected()
            return
        else:
            super(AgentDialog, self).keyPressEvent(event)

    def on_accept(self):
        self.accept()

    def on_reject(self):
        self.reject()

    def dialog_rejected(self):
        self.parent.setFocus()
        self.setResult(0)
        self.hide()

    def dialog_accepted(self):
        self.setResult(1)
        self.done(1)
        self.parent.setFocus()
        self.hide()

    def closeEvent(self, event):
        super(AgentDialog, self).closeEvent(event)

    def exec_(self, args=None):
        res = None
        self.parent.releaseKeyboard()
        res = super(AgentDialog, self).exec_()
        return res

    def show(self):
        super(AgentDialog, self).show()
        self.parent.releaseKeyboard()
        self.cancel_button.setFocus()
        self.setFocus()

    def hide(self):
        super(AgentDialog, self).hide()
        self.parent.setFocus()

class DeliveryDialog(QDialog):
    def __init__(self, parent, methods=None, widget=None, title=None):
        super(DeliveryDialog, self).__init__(parent)
        vbox = QVBoxLayout()
        self.parent = parent
        width = 800
        height = 300
        if widget:
            vbox.addLayout(widget.functions,0)
        self.setWindowTitle(title)
        self.setLayout(vbox)
        self.resize(width, height)

    def keyPressEvent(self, event):
        key = event.key()
        if key == Qt.Key_Escape:
            self.dialog_rejected()
            return
        else:
            super(DeliveryDialog, self).keyPressEvent(event)

    def on_accept(self):
        self.accept()

    def on_reject(self):
        self.reject()

    def dialog_rejected(self):
        self.parent.setFocus()
        self.setResult(0)
        self.hide()

    def dialog_accepted(self):
        self.setResult(1)
        self.done(1)
        self.parent.setFocus()
        self.hide()

    def closeEvent(self, event):
        super(DeliveryDialog, self).closeEvent(event)

    def exec_(self, args=None):
        res = None
        self.parent.releaseKeyboard()
        res = super(DeliveryDialog, self).exec_()
        return res

    def show(self):
        super(DeliveryDialog, self).show()
        self.parent.releaseKeyboard()
        self.cancel_button.setFocus()
        self.setFocus()

    def hide(self):
        super(DeliveryDialog, self).hide()
        self.parent.setFocus()

class FactoryIcons(object):

    def __init__(self):
        name_icons = ['print', 'warning', 'info', 'error', 'question']
        self.icons = {}
        for name in name_icons:
            path_icon = os.path.join(par_dir, 'share', 'icon-' + name + '.png')
            if not os.path.exists(path_icon):
                continue
            _qpixmap_icon = QPixmap()
            _qpixmap_icon.load(path_icon)
            _icon_label = QLabel()
            _icon_label.setAlignment(Qt.AlignCenter | Qt.AlignCenter)
            _icon_label.setPixmap(_qpixmap_icon.scaledToHeight(48))
            self.icons[name] = _icon_label

class TableDialog(QDialog):
    def __init__(self, parent, methods=None, widgets=[],
            table=None, title=None, cols_width=[]):
        super(TableDialog, self).__init__(parent)

        self.on_accepted_method = methods.get('on_accepted_method')

        vbox = QVBoxLayout()
        self.parent = parent
        self.ok_button = ActionButton('ok', self.dialog_accepted)
        self.cancel_button = ActionButton('cancel', self.dialog_canceled)
        self.cancel_button.setFocus()

        _buttons = []
        _buttons.extend([self.cancel_button, self.ok_button])

        buttonbox = QHBoxLayout()
        for b in _buttons:
            buttonbox.addWidget(b, 1)
        vbox.addLayout(buttonbox, 1)

        if widgets:
            vbox.addLayout(widgets, 2)

        if table:
            self.table = table
            vbox.addWidget(table, 3)

        self.setWindowTitle(title)
        self.setLayout(vbox)
        if cols_width:
            #print("cols_width >>", cols_width)
            WIDTH = sum(cols_width) + 75
        else:
            WIDTH = _TSIZE[0]
        size = (WIDTH+30, _TSIZE[1])
        self.setMinimumSize(*size)

    def keyPressEvent(self, event):
        key = event.key()
        if key == Qt.Key_Escape:
            self.dialog_canceled()
            return
        else:
            super(TableDialog, self).keyPressEvent(event)

    def dialog_canceled(self):
        self.parent.setFocus()
        self.setResult(0)
        self.hide()

    def dialog_accepted(self):
        if self.parent and self.on_accepted_method:
            getattr(self.parent, self.on_accepted_method)()
        self.parent.setFocus()
        self.setResult(1)
        self.done(1)
        self.hide()

    def exec_(self, args=None):
        res = None
        self.parent.releaseKeyboard()
        res = super(TableDialog, self).exec_()
        return res

    def show(self):
        super(TableDialog, self).show()
        self.parent.releaseKeyboard()
        self.cancel_button.setFocus()
        self.setFocus()

    def hide(self):
        super(TableDialog, self).hide()
        self.parent.setFocus()


import discord
from discord.ext import commands
import yt_dlp
import os
import dotenv
import subprocess
import logging
import asyncio

yt_opts = {
    'format': 'bestaudio',
    'extract-audio': True,
    'output': ''
        }

ffmpeg_options = {

        }

TOKEN = os.getenv('DISCORD_TOKEN')
def runDiscordBot():
    intents = discord.Intents.default()
    intents.message_content = True
    bot = commands.Bot(command_prefix='!', intents=intents)

    ytdlp_logs()

    @bot.event
    async def on_ready():
       print(f'{bot.user.name} has arrived')
       #await bot.load_extension('cogs.Music')
       #await load_cogs(bot)
       print(f'this is {bot.user.name}')

    @bot.event
    async def join(ctx):
        if not ctx.voice_client:
           pass

    @bot.command()
    async def play(ctx, url):
        if ctx.author.voice:
            if not ctx.voice_client:
                voice_channel = ctx.author.voice.channel
                voice_client = await voice_channel.connect()
                print('joined voice channel')
            else:
                pass #probably not needed
        else:
            print('failed to join voice channel')

        with yt_dlp.YoutubeDL(yt_opts) as ydl:
            song_info = ydl.extract_info(url, download=False)
        print(song_info["url"])
        ctx.voice_client.play(discord.FFmpegPCMAudio(song_info['url']))
        #ctx.voice_client.play(discord.FFmpegPCMAudio(song_info['url'], **ffmpeg_options))

    async def player_loop(self):
        await self.bot.wait_until_ready()

        while not self.bot.is_closed():
            self.next.clear()
            async with timeout(60):
                source = await self.queue.get()

            if not isinstance(source, YTDLSource):
                pass

    @bot.command()
    async def stop(ctx):
        voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild)
        if voice_client:
            await voice_client.disconnect()

    bot.run(TOKEN)

async def load_cogs(bot):
    for filename in os.listdir('./cogs'):
        if filename[:-3] == '.py':
            await bot.load_extension(f'cogs{filename[:-3]}')

def ytdlp_logs():
    logger = logging.getLogger('yt_dlp')
    logger.setLevel(logging.DEBUG)
    handler = logging.StreamHandler()
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y-%m%-%d %H:%M:%S')
    handler.setFormatter(formatter)
    logger.addHandler(handler)

if __name__ == '__main__':
    runDiscordBot()


# imports
import numpy as np
import pandas as pd
import seaborn as sns
from tensorflow.keras.layers import Input, Dense, LSTM, Dropout
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
import re
import tensorflow.keras.callbacks as ES
import os, sys
from sklearn.utils import class_weight
from keras.losses import sparse_categorical_crossentropy
import csv

# style conifg
pd.set_option('display.width', 400)
pd.set_option('display.max_columns', 20)
sns.set_style('whitegrid')

# import Trump tweets (dataset from Kaggle.com, scrapped from twitter)
df = pd.read_csv('realdonaldtrump.csv', sep=',')
# shuffle the dataframe
df = df.sample(frac=1).reset_index(drop=True)

# first look at the data
df.head()
df = df[['content', 'id']]

# remove at least some of the links/unwanted strings
letters = set('abcdefghijklmnopqrstuvwxyz.,:?! ABCDEFGHIJKLMNOPQRSTUVWXYZ')

df['content'] = df['content'].apply(lambda x: re.sub(r'\w+:\/{2}[\d\w-]+(\.[\d\w-]+)*(?:(?:\/[^\s/]*))*', '', x))
df['content'] = df['content'].apply(lambda x: re.sub(r'pic.twitter', '', x))
df['content'] = df['content'].apply(lambda x: re.sub(r'.com', '', x))
df['content'] = df['content'].apply(lambda x: re.sub(r'@', '', x))
df['content'] = df['content'].apply(lambda x: ''.join(filter(letters.__contains__, x)))

text = ''

for i in df['content']:
        for j in i.split():
            text += ' ' + j.lower()

chars = sorted(list(set(text)))
char_to_int = dict((c, i) for i, c in enumerate(chars))
int_to_char = dict((i, c) for i, c in enumerate(chars))

n_chars = len(text)
n_vocab = len(chars)

length = 10
X_lst = []
Y = []

for i in range(0, n_chars - length, 1):
    x = text[i:i + length]
    y = text[i+length]
    X_lst.append([char_to_int[char] for char in x])
    Y.append(char_to_int[y])

print(len(X_lst))
X = np.reshape(X_lst, (len(X_lst), length, 1))

# normalize data
X = X / float(n_vocab)
Y = np.array(Y)

del(df)
#######################################

X_train = X[:int(len(X)-20000)]
Y_train = Y[:int(len(X)-20000)]
X_test = X[int(len(X)-20000):]
Y_test = Y[int(len(X)-20000):]

i = Input(shape=(length, 1,))
x = LSTM(128, return_sequences=True)(i)
x = Dropout(0.2)(x)
x = LSTM(128)(x)
x = Dropout(0.2)(x)
x = Dense(n_vocab, activation='softmax')(x)

model1 = Model(i, x)

class_weights = class_weight.compute_class_weight('balanced', np.unique(Y), Y)
class_weights_ann = dict(enumerate(class_weights))

train_losses = []

# training

model1.load_weights('maly_model.h5')

model1.compile(optimizer=Adam(lr=0.001), loss='sparse_categorical_crossentropy')

for i in range(400000):
    random_index = np.random.randint(0, len(X)-64)
    random_batch_X = X_train[random_index:random_index+64]
    random_batch_Y = Y_train[random_index:random_index+64]

    a = model1.train_on_batch(random_batch_X, random_batch_Y, class_weight=class_weights_ann)
    train_losses.append(a)

    if i % 5000 == 0:
        train_losses = []
    if i % 1000 == 0:

        yhat = model1.predict(X_test)
        batch_loss = np.mean(sparse_categorical_crossentropy(Y_test, yhat))

        # save model
        model1.save('maly_model.h5')
        print('train loss:', a)
        print('test loss:', batch_loss)
        print('train loss averaged:', np.mean(train_losses))

        # print example generated text
        start = np.random.randint(0, len(X_lst) - 1)
        seed_input_text = X_lst[start]
        seed_input = X[start].reshape(1, length, 1)
        generated_text_idx = []

        for j in range(100):
            yhat_prob = model1.predict(seed_input)
            yhat = np.random.choice(len(yhat_prob.reshape(n_vocab,)), p=yhat_prob.reshape(n_vocab,))
            seed_input = np.roll(seed_input, -1)
            seed_input[:, -1] = yhat / n_vocab
            generated_text_idx.append(yhat)

        generated_text = [int_to_char[word] for word in generated_text_idx]
        print(''.join(generated_text))

        with open('output_maly_stochastic.txt', 'a') as f:
            f.write('Batch number ' + str(i) + ' ' + ''.join(generated_text) + '\n')

        # print example generated text
        start = np.random.randint(0, len(X_lst) - 1)
        seed_input_text = X_lst[start]
        seed_input = X[start].reshape(1, length, 1)
        generated_text_idx = []

        for j in range(100):
            yhat_prob = model1.predict(seed_input)
            yhat = np.argmax(yhat_prob)
            seed_input = np.roll(seed_input, -1)
            seed_input[:, -1] = yhat / n_vocab
            generated_text_idx.append(yhat)

        generated_text = [int_to_char[word] for word in generated_text_idx]
        print(''.join(generated_text))

        with open('output_maly_deterministic.txt', 'a') as f:
            f.write('Batch number ' + str(i) + ' ' + ''.join(generated_text) + '\n')

        with open('losses_maly.csv', 'a') as f:
            writer = csv.writer(f)
            writer.writerow([i, batch_loss, a])

archive (old models)/LSTM final/Trump_bot_LSTM3(softmax characters) - smaller LSTM.py


# -*- coding: utf-8 -*-
import torch
from shapely.geometry import Polygon
import numpy as np

def point_form(boxes):
    """ Convert prior_boxes to (xmin, ymin, xmax, ymax)
    representation for comparison to point form ground truth data.
    Args:
        boxes: (tensor) center-size default boxes from priorbox layers.
    Return:
        boxes: (tensor) Converted xmin, ymin, xmax, ymax form of boxes.
    """
    return torch.cat((boxes[:, :2] - boxes[:, 2:]/2,     # xmin, ymin
                     boxes[:, :2] + boxes[:, 2:]/2), 1)  # xmax, ymax
def point_form_2(boxes):
    """ 将 prior_boxes 转换为按顺时针从左上到左下的坐标(x1,y1,x2,y2,x3,y3,x4,y4)
    representation for comparison to point form ground truth data.
    Args:
        boxes: (tensor) center-size default boxes from priorbox layers.
    Return:
        boxes: (tensor) Converted x1,y1,x2,y2,x3,y3,x4,y4 form of boxes.
    """
    reboxes = torch.zeros((boxes.shape[0], 8))
    reboxes[:, 0] = boxes[:, 0] - boxes[:, 2] / 2
    reboxes[:, 1] = boxes[:, 1] - boxes[:, 3] / 2
    reboxes[:, 2] = boxes[:, 0] + boxes[:, 2] / 2
    reboxes[:, 3] = boxes[:, 1] - boxes[:, 3] / 2
    reboxes[:, 4] = boxes[:, 0] + boxes[:, 2] / 2
    reboxes[:, 5] = boxes[:, 1] + boxes[:, 3] / 2
    reboxes[:, 6] = boxes[:, 0] - boxes[:, 2] / 2
    reboxes[:, 7] = boxes[:, 1] + boxes[:, 3] / 2
    return reboxes

def center_size(boxes):
    """ Convert prior_boxes to (cx, cy, w, h)
    representation for comparison to center-size form ground truth data.
    Args:
        boxes: (tensor) point_form boxes
    Return:
        boxes: (tensor) Converted xmin, ymin, xmax, ymax form of boxes.
    """
    return torch.cat((boxes[:, 2:] + boxes[:, :2])/2,  # cx, cy
                     boxes[:, 2:] - boxes[:, :2], 1)  # w, h

def intersect(box_a, box_b):
    """ We resize both tensors to [A,B,2] without new malloc:
    [A,2] -> [A,1,2] -> [A,B,2]
    [B,2] -> [1,B,2] -> [A,B,2]
    Then we compute the area of intersect between box_a and box_b.
    Args:
      box_a: (tensor) bounding boxes, Shape: [A,4].
      box_b: (tensor) bounding boxes, Shape: [B,4].
    Return:
      (tensor) intersection area, Shape: [A,B].
    """
    A = box_a.size(0)
    B = box_b.size(0)

    max_xy = torch.min(box_a[:, 2:].unsqueeze(1).expand(A, B, 2),
                       box_b[:, 2:].unsqueeze(0).expand(A, B, 2))
    min_xy = torch.max(box_a[:, :2].unsqueeze(1).expand(A, B, 2),
                       box_b[:, :2].unsqueeze(0).expand(A, B, 2))
    inter = torch.clamp((max_xy - min_xy), min=0)
    return inter[:, :, 0] * inter[:, :, 1]

def jaccard(box_a, box_b):
    """Compute the jaccard overlap of two sets of boxes.  The jaccard overlap
    is simply the intersection over union of two boxes.  Here we operate on
    ground truth boxes and default boxes.
    E.g.:
        A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B)
    Args:
        box_a: (tensor) Ground truth bounding boxes, Shape: [num_objects,4]
        box_b: (tensor) Prior boxes from priorbox layers, Shape: [num_priors,4]
    Return:
        jaccard overlap: (tensor) Shape: [box_a.size(0), box_b.size(0)]
    """
    box_a_4 = torch.zeros(box_a.shape[0], 4)
    box_a_4[:, 0] = box_a[:, 0:7:2].min(dim=1)[0]
    box_a_4[:, 1] = box_a[:, 1:8:2].min(dim=1)[0]
    box_a_4[:, 2] = box_a[:, 0:7:2].max(dim=1)[0]
    box_a_4[:, 3] = box_a[:, 1:8:2].max(dim=1)[0]

    inter = intersect(box_a_4, box_b)
    area_a = ((box_a_4[:, 2]-box_a_4[:, 0]) *
              (box_a_4[:, 3]-box_a_4[:, 1])).unsqueeze(1).expand_as(inter)  # [A,B]
    area_b = ((box_b[:, 2]-box_b[:, 0]) *
              (box_b[:, 3]-box_b[:, 1])).unsqueeze(0).expand_as(inter)  # [A,B]
    union = area_a + area_b - inter
    return inter / union  # [A,B]

def intersect_ploy(box_a, box_b):
    """
    任意两个图形的相交面积的计算
    :param data1: 当前物体[(x1,y1), (x2, y2), .., ..]
    :param data2: 待比较的物体
    :return: 当前物体与待比较的物体的面积交集, box_a的面积
    """
    box_a = box_a.reshape(4, 2)
    box_b = box_b.reshape(4, 2)

    poly1 = Polygon(box_a).convex_hull      # Polygon：多边形对象
    poly2 = Polygon(box_b).convex_hull

    if not poly1.intersects(poly2):
        inter_area = 0  # 如果两四边形不相交
    else:
        inter_area = poly1.intersection(poly2).area  # 相交面积
    return inter_area, poly1.union(poly2).area

def jaccard_2(box_a, box_b, overlaps):
    """Compute the jaccard overlap of two sets of boxes.  The jaccard overlap
    is simply the intersection over union of two boxes.  Here we operate on
    ground truth boxes and default boxes.
    E.g.:
        A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B)
    Args:
        box_a: (tensor) Ground truth bounding boxes, Shape: [num_objects,4], [num_objects,8] point-offset
        box_b: (tensor) Prior boxes from priorbox layers, Shape: [num_priors,4], [num_objects,8] point-offset
    Return:
        jaccard overlap: (tensor) Shape: [box_a.size(0), box_b.size(0)]
    """
    # inter = intersect(box_a, box_b)
    A = box_a.size(0)
    B = box_b.size(0)

    box_a_index,  box_b_index = torch.where(overlaps>0)
    inter = torch.zeros(A, B)
    union = torch.zeros(A, B)
    inter_union = np.array([intersect_ploy(box_a[box_a_index[i]], box_b[box_b_index[i]]) for i in range(len(box_a_index))])
    for j in range(len(box_a_index)):
        inter[box_a_index[j], box_b_index[j]] = inter_union[j, 0]
        union[box_a_index[j], box_b_index[j]] = inter_union[j, 1]
    return inter / union  # [A,B]

def match(threshold, truths, priors, variances, labels, loc_t, conf_t, idx):
    """Match each prior box with the ground truth box of the highest jaccard
    overlap, encode the bounding boxes, then return the matched indices
    corresponding to both confidence and location preds.
    Args:
        threshold: (float) The overlap threshold used when mathing boxes.
        truths: (tensor) Ground truth boxes, Shape: [num_obj, num_priors]. point-form
        priors: (tensor) Prior boxes from priorbox layers, Shape: [n_priors,4]. center-form
        variances: (tensor) Variances corresponding to each prior coord,
            Shape: [num_priors, 4].
        labels: (tensor) All the class labels for the image, Shape: [num_obj].
        loc_t: (tensor) Tensor to be filled w/ endcoded location targets.
        conf_t: (tensor) Tensor to be filled w/ matched indices for conf preds.
        idx: (int) current batch index
    Return:
        The matched indices corresponding to 1)location and 2)confidence preds.
    """
    # jaccard index
    overlaps = jaccard(truths, point_form(priors))

    # (Bipartite Matching)
    # [1,num_objects] best prior for each ground truth
    best_prior_overlap, best_prior_idx = overlaps.max(1, keepdim=True) # (A, 1) 每一行的最大值，即单一真实框和预选框IOU最大的一个下标
    # [1,num_priors] best ground truth for each prior
    best_truth_overlap, best_truth_idx = overlaps.max(0, keepdim=True) # (1, B) 每一列的最大值，即单一预选框和真实框IOU最大的一个下标
    best_truth_idx.squeeze_(0)
    best_truth_overlap.squeeze_(0)
    best_prior_idx.squeeze_(1)
    best_prior_overlap.squeeze_(1)
    best_truth_overlap.index_fill_(0, best_prior_idx, 2)  # ensure best prior
    # TODO refactor: index  best_prior_idx with long tensor
    # ensure every gt matches with its prior of max overlap
    for j in range(best_prior_idx.size(0)):
        best_truth_idx[best_prior_idx[j]] = j
    matches = truths[best_truth_idx]          # Shape: [num_priors,4] [num_priors,8], point-form
    conf = labels[best_truth_idx] + 1         # Shape: [num_priors]
    conf[best_truth_overlap < threshold] = 0  # label as background
    pos = torch.where(conf>0)
    loc = encode(matches, priors, pos, variances)
    loc_t[idx] = loc    # [num_priors,4] encoded offsets to learn
    conf_t[idx] = conf  # [num_priors] top class label for each prior

def define_orient(matched, horizontal_rectangle):
    '''
    确定多边形四个角的顺序，采用欧几里得距离，求最小的排列
    :param matched: [num_priors, 8], point-form
    :param horizontal_rectangle: [num_priors, 8], point-form
    :return:
    '''
    # 1234-2341-3412-4123
    distance_min_1 = torch.sum((matched[:, 0:2] - horizontal_rectangle[:, 0:2]) ** 2, dim=1) + \
                   torch.sum((matched[:, 2:4] - horizontal_rectangle[:, 2:4]) ** 2, dim=1) + \
                   torch.sum((matched[:, 4:6] - horizontal_rectangle[:, 4:6]) ** 2, dim=1) + \
                   torch.sum((matched[:, 6:8] - horizontal_rectangle[:, 6:8]) ** 2, dim=1) # (num_priors)

    distance_min_2 = torch.sum((matched[:, 2:4] - horizontal_rectangle[:, 0:2]) ** 2, dim=1) + \
                     torch.sum((matched[:, 4:6] - horizontal_rectangle[:, 2:4]) ** 2, dim=1) + \
                     torch.sum((matched[:, 6:8] - horizontal_rectangle[:, 4:6]) ** 2, dim=1) + \
                     torch.sum((matched[:, 0:2] - horizontal_rectangle[:, 6:8]) ** 2, dim=1)  # (num_priors)

    distance_min_3 = torch.sum((matched[:, 4:6] - horizontal_rectangle[:, 0:2]) ** 2, dim=1) + \
                     torch.sum((matched[:, 6:8] - horizontal_rectangle[:, 2:4]) ** 2, dim=1) + \
                     torch.sum((matched[:, 0:2] - horizontal_rectangle[:, 4:6]) ** 2, dim=1) + \
                     torch.sum((matched[:, 2:4] - horizontal_rectangle[:, 6:8]) ** 2, dim=1)  # (num_priors)

    distance_min_4 = torch.sum((matched[:, 6:8] - horizontal_rectangle[:, 0:2]) ** 2, dim=1) + \
                     torch.sum((matched[:, 0:2] - horizontal_rectangle[:, 2:4]) ** 2, dim=1) + \
                     torch.sum((matched[:, 2:4] - horizontal_rectangle[:, 4:6]) ** 2, dim=1) + \
                     torch.sum((matched[:, 4:6] - horizontal_rectangle[:, 6:8]) ** 2, dim=1)  # (num_priors)

    origin = torch.cat((distance_min_1.unsqueeze(1),
                        distance_min_2.unsqueeze(1),
                        distance_min_3.unsqueeze(1),
                        distance_min_4.unsqueeze(1)), 1) # (num_priors, 4)

    origin_indices = origin.min(dim=1).indices  # (num_priors)
    for i, box in enumerate(matched):
        if origin_indices[i] == 1:
            index = [2, 3, 4, 5, 6, 7, 0, 1]
            matched[i] = matched[i][index]
        if origin_indices[i] == 2:
            index = [4, 5, 6, 7, 0, 1, 2, 3]
            matched[i] = matched[i][index]
        if origin_indices[i] == 3:
            index = [6, 7, 0, 1, 2, 3, 4, 5]
            matched[i] = matched[i][index]

    return matched

def encode(matched, priors, pos, variances):
    """Encode the variances from the priorbox layers into the ground truth boxes
    we have matched (based on jaccard overlap) with the prior boxes.
    Args:
        matched: (tensor) Coords of ground truth for each prior in point-form
            Shape: [num_priors, 4].[num_priors, 8], point-form
        priors: (tensor) Prior boxes in center-offset form
            Shape: [num_priors,4].
        variances: (list[float]) Variances of priorboxes
    Return:
        encoded boxes (tensor), Shape: [num_priors, 4]
    """

    # dist b/t match center and prior's center
    # 多边形文本框外接水平矩形
    x0 = matched[:, 0:7:2].min(-1, keepdim=True).values
    y0 = matched[:, 1:8:2].min(-1, keepdim=True).values
    x1 = matched[:, 0:7:2].max(-1, keepdim=True).values
    y1 = matched[:, 1:8:2].max(-1, keepdim=True).values
    horizontal_rectangle = torch.cat((x0, y0, x1, y0, x1, y1, x0, y1), 1)
    matched[pos] = define_orient(matched[pos], horizontal_rectangle[pos])

    w = x1 - x0
    h = y1 - y0

    delta_x = ((x1+x0)/2 - priors[:, 0].unsqueeze(1))/priors[:, 2].unsqueeze(1)
    delta_y = ((y1+y0)/2 - priors[:, 1].unsqueeze(1))/priors[:, 3].unsqueeze(1)
    delta_w = torch.log(w/priors[:, 2].unsqueeze(1))
    delta_h = torch.log(h/priors[:, 3].unsqueeze(1))

    # 预选框
    priors_8 = point_form_2(priors) # center-offset form to 8point-form
    matched[:, 0:7:2] = (matched[:, 0:7:2] - priors_8[:, 0:7:2]) / priors[:, 2].unsqueeze(1)
    matched[:, 1:8:2] = (matched[:, 1:8:2] - priors_8[:, 1:8:2]) / priors[:, 3].unsqueeze(1)

    target_delta = torch.cat((delta_x, delta_y, delta_w, delta_h), 1)
    target_delta = torch.cat((target_delta, matched), 1)

    # return target for smooth_l1_loss
    return target_delta  # [num_priors,12]

# Adapted from https://github.com/Hakuyume/chainer-ssd
def decode(loc, priors, variances):
    """Decode locations from predictions using priors to undo
    the encoding we did for offset regression at train time.
    Args:
        loc (tensor): location predictions for loc layers,
            Shape: [num_priors,4]
        priors (tensor): Prior boxes in center-offset form.
            Shape: [num_priors,4].
        variances: (list[float]) Variances of priorboxes
    Return:
        decoded bounding box predictions. center-offset form+point-form
    """

    boxes = torch.cat((
        priors[:, :2] + loc[:, :2] * priors[:, 2:],
        priors[:, 2:4] * torch.exp(loc[:, 2:4])), 1)

    priors_8 = point_form_2(priors)
    loc[:, 4:11:2] = priors_8[:, 0:7:2] + priors[:, 2].unsqueeze(1) * loc[:, 4:11:2]
    loc[:, 5:12:2] = priors_8[:, 1:8:2] + priors[:, 3].unsqueeze(1) * loc[:, 5:12:2]

    boxes = torch.cat((boxes, loc[:, 4:12]), 1)
    return boxes

def log_sum_exp(x):
    """Utility function for computing log_sum_exp while determining
    This will be used to determine unaveraged confidence loss across
    all examples in a batch.
    Args:
        x (Variable(tensor)): conf_preds from conf layers
    """
    x_max = x.data.max()
    return torch.log(torch.sum(torch.exp(x-x_max), 1, keepdim=True)) + x_max

# Original author: Francisco Massa:
# https://github.com/fmassa/object-detection.torch
# Ported to PyTorch by Max deGroot (02/01/2017)
def nms(boxes, scores, overlap=(0.5, 0.2), top_k=200):
    """Apply non-maximum suppression at test time to avoid detecting too many
    overlapping bounding boxes for a given object.方框+四边形nms
    Args:
        boxes: (tensor) The location preds for the img, Shape: [num_priors,4].point-form,[num_priors,12]
        scores: (tensor) The class predscores for the img, Shape:[num_priors].
        overlap: (float) The overlap thresh for suppressing unnecessary boxes.
        top_k: (int) The Maximum number of box preds to consider.
    Return:
        The indices of the kept boxes with respect to num_priors.
    """
    # First NMS with a relatively high IOU threshold (e.g. 0.5) on the minimum horizontal rectangles
    keep = scores.new(scores.size(0)).zero_().long()
    # 返回元素数目
    if boxes.numel() == 0:
        return keep
    x1 = boxes[:, 0:7:2].min(-1).values
    y1 = boxes[:, 1:8:2].min(-1).values
    x2 = boxes[:, 0:7:2].max(-1).values
    y2 = boxes[:, 1:8:2].max(-1).values

    area_f = torch.mul(x2 - x1, y2 - y1)
    # 预选框排名，升序
    v, idx_f = scores.sort(0)  # sort in ascending order
    idx_f = idx_f[-top_k:]
    # boxes.new()该Tensor的type和device都和原有Tensor一致，且无内容
    xx1 = boxes.new()
    yy1 = boxes.new()
    xx2 = boxes.new()
    yy2 = boxes.new()
    w = boxes.new()
    h = boxes.new()

    # keep = torch.Tensor()
    count = 0
    while idx_f.numel() > 0:
        i = idx_f[-1]  # index of current largest val
        # keep.append(i)
        keep[count] = i
        count += 1
        if idx_f.size(0) == 1:
            break
        idx_f = idx_f[:-1]  # remove kept element from view
        # load bboxes of next highest vals
        torch.index_select(x1, 0, idx_f, out=xx1)
        torch.index_select(y1, 0, idx_f, out=yy1)
        torch.index_select(x2, 0, idx_f, out=xx2)
        torch.index_select(y2, 0, idx_f, out=yy2)
        # store element-wise max with next highest score
        xx1 = torch.clamp(xx1, min=x1[i])
        yy1 = torch.clamp(yy1, min=y1[i])
        xx2 = torch.clamp(xx2, max=x2[i])
        yy2 = torch.clamp(yy2, max=y2[i])
        w.resize_as_(xx2)
        h.resize_as_(yy2)
        w = xx2 - xx1
        h = yy2 - yy1
        # check sizes of xx1 and xx2.. after each iteration
        w = torch.clamp(w, min=0.0)
        h = torch.clamp(h, min=0.0)
        inter = w * h
        # IoU = i / (area(a) + area(b) - i)
        rem_areas = torch.index_select(area_f, 0, idx_f)  # load remaining areas)
        union = (rem_areas - inter) + area_f[i]
        IoU_f = inter / union  # store result in iou
        # keep only elements with an IoU <= overlap
        # input<=other，坐标位置为1，否则为0
        idx_f = idx_f[IoU_f.le(overlap[0])]

    # sencod NMS on quadrilaterals or rotated rectangles is applied to a few remaining
    # candidate boxes with a lower IOU threshold (e.g. 0.2).
    x1 = boxes[:, 4]
    y1 = boxes[:, 5]
    x2 = boxes[:, 6]
    y2 = boxes[:, 7]
    x3 = boxes[:, 8]
    y3 = boxes[:, 9]
    x4 = boxes[:, 10]
    y4 = boxes[:, 11]
    xx1 = boxes.new()
    yy1 = boxes.new()
    xx2 = boxes.new()
    yy2 = boxes.new()
    xx3 = boxes.new()
    yy3 = boxes.new()
    xx4 = boxes.new()
    yy4 = boxes.new()

    count_s = 0
    keep_s = scores.new(scores.size(0)).zero_().long()
    idx_s = keep[:count]
    while idx_s.numel() > 0:
        i = idx_s[0]
        keep_s[count_s] = i
        count_s += 1
        if idx_s.size(0) == 1:
            break
        max_box = boxes[i]
        idx_s = idx_s[1:]
        torch.index_select(x1, 0, idx_s, out=xx1)
        torch.index_select(y1, 0, idx_s, out=yy1)
        torch.index_select(x2, 0, idx_s, out=xx2)
        torch.index_select(y2, 0, idx_s, out=yy2)
        torch.index_select(x3, 0, idx_s, out=xx3)
        torch.index_select(y3, 0, idx_s, out=yy3)
        torch.index_select(x4, 0, idx_s, out=xx4)
        torch.index_select(y4, 0, idx_s, out=yy4)

        inter_union = np.array([intersect_ploy(max_box[4:12], np.array([xx1[i], yy1[i],
                                                         xx2[i], yy2[i],
                                                         xx3[i], yy3[i],
                                                         xx4[i], yy4[i]])) for i in range(xx1.shape[0])])
        inter, union = inter_union[:, 0], inter_union[:, 1]
        IoU_s = torch.tensor(inter / union)
        idx_s = idx_s[IoU_s.le(overlap[1])]
    return keep_s, count_s


"""Test clearing of old addon files."""

from pathlib import Path

from bpydevutil.functions import common_funcs

def test_clear_old_addon(temp_addons_dir):
    root_dir, modules, packages = temp_addons_dir

    for module in modules.keys():
        assert Path(root_dir / f"{module}.py").exists()
        common_funcs.clear_old_addon(root_dir, module)
        assert not Path(root_dir / f"{module}.py").exists()

    for package in packages.keys():
        assert Path(root_dir / package).exists()
        common_funcs.clear_old_addon(root_dir, package)
        assert not Path(root_dir / package).exists()


import asyncio
import unittest
import json

from lib.dispatcher import Dispatcher
from lib.channel import Channel
from lib.client import Client
from lib.message import Message

class MockAsyncIterator:
    def __init__(self, iterable):
        self.iterable = iterable

    def __aiter__(self):
        return self

    async def __anext__(self):
        try:
            return next(self.iterable)
        except StopIteration:
            raise StopAsyncIteration

class DispatcherTests(unittest.TestCase):
    def setUp(self):
        self.target_channel = Channel("target_channel")
        self.target_channel.broadcast = unittest.mock.AsyncMock()
        self.target_channel.send = unittest.mock.AsyncMock()
        self.another_channel = Channel("another_channel")
        self.channels = {
                self.target_channel.id: self.target_channel,
                self.another_channel.id: self.another_channel
                }
        self.sender_mock_socket = MockAsyncIterator(iter([]))
        self.sender = Client(self.sender_mock_socket)
        self.receiver_mock_socket = MockAsyncIterator(iter([]))
        self.receiver = Client(self.receiver_mock_socket)
        self.sample_message = Message(self.sender.address,self.receiver.address,self.target_channel.id,"")

    def test_init(self):
        expected_channels = {
            "channel1": Channel("channel1"),
            "channel2": Channel("channel2")
                }
        dispatcher = Dispatcher(expected_channels)
        self.assertEqual(dispatcher.channels, expected_channels)

    def test_create_channel(self):

        message = self.sample_message
        message.type = "create_channel"
        message.receiver_guid = ""
        message.data = {"name": "new_channel"}

        # Mock the webocket methods
        self.sender_mock_socket.send = unittest.mock.AsyncMock()

        dispatcher = Dispatcher({})
        self.mock_receive_message(message, dispatcher)

        # It adds a new channel
        found = False
        new_channel = ""
        for channel in dispatcher.channels.values():
            found = channel.name == "new_channel"
            if found:
                new_channel = channel
                break
        self.assertTrue(found)

        # It sends an 'ack' with the channel id
        self.sender_mock_socket.send.assert_called()
        sent_message = Message.from_json(self.sender_mock_socket.send.call_args_list[0][0][0])
        self.assertEqual(sent_message.type, "ack")
        self.assertEqual(sent_message.data["channel_id"], new_channel.id)

    def test_client_disconnect(self):
        message = self.sample_message
        message.type = "ignored"
        message.channel_id = self.target_channel.id

        dispatcher = Dispatcher(self.channels)
        self.mock_receive_message(message, dispatcher)

        # It broadcasts a hangup
        self.target_channel.broadcast.assert_called()
        sent_message = self.target_channel.broadcast.call_args_list[0][0][1]
        self.assertEqual(sent_message.type, "hangup")

        # It removes the client from the channel
        self.assertEqual(len(self.target_channel.clients), 0)

    def test_message_with_missing_channel(self):
        # When channel_id is missing

        message = self.sample_message
        message.type = "anything_but_create_channel"
        message.channel_id = "non_existent"
        message.receiver_guid = ""

        # Mock the webocket methods
        self.sender_mock_socket.close = unittest.mock.AsyncMock()
        self.sender_mock_socket.send = unittest.mock.AsyncMock()

        # Receive the test message
        dispatcher = Dispatcher(self.channels)
        self.mock_receive_message(message, dispatcher)

        # It sends an error to the sender
        self.sender_mock_socket.send.assert_called()
        sent_message = Message.from_json(self.sender_mock_socket.send.call_args_list[0][0][0])
        self.assertEqual(sent_message.type, "error")

        # It disconnects
        self.sender_mock_socket.close.assert_called()

    def test_announce_message(self):
        # When an "announce" is received

        message = self.sample_message
        message.type = "announce"
        message.receiver_guid = ""

        # It broadcasts it to the channel
        self.it_broadcasts(message)

        # It adds the sender to the channel
        matching_clients = list(client for client in list(self.target_channel.clients) if client.websocket == self.sender.websocket)
        self.assertEqual(len(matching_clients), 1)

    def test_name_message(self):
        message = self.sample_message
        message.type = "name"
        message.receiver_guid = ""

        # It broadcasts it to the channel
        self.it_broadcasts(message)

    def test_offer_message(self):
        message = self.sample_message
        message.type = "offer"
        self.it_sends(message)

    def test_answer_message(self):
        message = self.sample_message
        message.type = "answer"
        self.it_sends(message)

    def test_ice_message(self):
        message = self.sample_message
        message.type = "ice"
        self.it_sends(message)

    def it_broadcasts(self, message):
        self.target_channel.remove_client = unittest.mock.Mock()
        self.sender_mock_socket.iterable = iter([message.to_json()])

        dispatcher = Dispatcher(self.channels)

        asyncio.get_event_loop().run_until_complete(
                dispatcher.handle_connection(self.sender_mock_socket, "/")
        )

        # It broadcasts it to the channel
        self.target_channel.broadcast.assert_called()
        broadcast_args = self.target_channel.broadcast.call_args_list[0]
        self.assertEqual(self.sender.websocket, broadcast_args[0][0].websocket)
        self.assertEqual(vars(message), vars(broadcast_args[0][1]))

    def it_sends(self, message):
        self.target_channel.remove_client = unittest.mock.Mock()
        self.sender_mock_socket.iterable = iter([message.to_json()])

        self.target_channel.add_client(self.sender)
        dispatcher = Dispatcher(self.channels)

        asyncio.get_event_loop().run_until_complete(
                dispatcher.handle_connection(self.sender_mock_socket, "/")
        )

        # It sends the message
        self.target_channel.send.assert_called()
        send_args = self.target_channel.send.call_args_list[0]
        self.assertEqual(self.sender.websocket, send_args[0][0].websocket)
        self.assertEqual(vars(message), vars(send_args[0][1]))

    def mock_receive_message(self, message, dispatcher):
        self.target_channel.remove_client = unittest.mock.Mock()
        self.sender_mock_socket.iterable = iter([message.to_json()])

        asyncio.get_event_loop().run_until_complete(
                dispatcher.handle_connection(self.sender_mock_socket, "/")
        )


import gym
import numpy as np

class App(gym.Env):
    def __init__(self):
        self.position = np.array([0.,0.])
        self.action_space = gym.spaces.box.Box(np.array([-1, -1]), np.array([1, 1]), (2,))
        self.observation_space = gym.spaces.box.Box(np.array([-100.]*4), np.array([100.]*4),(4,))
        self.step_counter = 0
        self.cum_reward = 0
        self.destination = np.array([100., 100.])

    def step(self, action):
        pre_position = np.array(self.position)
        pre_distance = np.sqrt(np.sum((self.destination - self.position) ** 2)) # Euklidische Distanz

        self.step_counter += 1
        # speed = action[0]

        action_length = np.sqrt(np.sum(action**2))
        if action_length > 1:
            action /= action_length
        self.position += action

        done = False

        distance = np.sqrt(np.sum((self.destination - self.position) ** 2))
        speed = pre_distance - distance

        reward = (5 / np.sqrt(2)) * speed
        # else : reward = 0

        if distance <= 1 :
            reward = 1000
            self.position = self.destination
            done = True
            print('Das Ziel wurde erreicht nach {} Schritten '.format(self.step_counter))
        elif not -100 <= self.position[0] < 100 or not -100 <= self.position[1] < 100 : # Wenn Agent des Aktionsraum verlässt
            reward = -100
            self.position = pre_position
            done = True
        elif self.step_counter >= 200:
            done = True

        self.cum_reward += reward

        if done == True :
            print('Die Episode ist beendet. Kummulierte Belohnung: {}, Anzahl Schritte: {}, Position: ({},{}), Zielposition:({}, {})'.format(self.cum_reward, self.step_counter, self.position[0], self.position[1], self.destination[0], self.destination[1]))

        return (list(self.position) + list(self.destination), reward, done, dict())

    def reset(self):
        self.position = np.array([0.,0.])
        self.cum_reward = 0
        self.step_counter = 0

        if np.random.rand() > 0.5:
            self.destination = np.array([100., 100.])
        else:
            self.destination = np.array([-100., -100.])

        return list(self.position) + list(self.destination)

    def render(self, mode='human'):
        pass


# import modules
from math import radians, cos, sin, asin, sqrt
from geopy.geocoders import Nominatim
from geopy.units import kilometers

# Radius of earth in kilometers. Use 3956 for miles
r = 6371

# get geolocator from nominatim
geolocator = Nominatim(user_agent="Get zipcode details")
# get location of nairobi
location1 = geolocator.geocode('Nairobi')
# get location of cairo
location2 = geolocator.geocode('Cairo')

# get latitudes and longitudes
lon1 =  location1.longitude
lon2 = location2.longitude
lat1 = location1.latitude
lat2 = location2.latitude

 # Haversine formula to get distance
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2

c = 2 * asin(sqrt(a))
 # calculate the result
result = (c * r)

print('\n\nThe distance between Nairobi and Cairo is: \n\n')
print(int(result))


#! /usr/bin/python3
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

__weights_dict = dict()

def load_weights(weight_file):
    if weight_file == None:
        return

    try:
        weights_dict = np.load(weight_file, allow_pickle=True).item()
    except:
        weights_dict = np.load(weight_file, encoding='bytes', allow_pickle=True).item()

    return weights_dict

class Encoder(nn.Module):
    def __init__(self, weight_file):
        super(Encoder, self).__init__()
        global __weights_dict
        __weights_dict = load_weights(weight_file)

        base_name = (list(__weights_dict.keys())[0].split('/'))[0]

        self.encoder_conv2d_Conv2D = self.__conv(2, name=base_name+'/conv2d/Conv2D', in_channels=3, out_channels=128, kernel_size=(5, 5), stride=(2, 2), groups=1, bias=True)
        self.encoder_batch_normalization_FusedBatchNorm = self.__batch_normalization(2, base_name+'/batch_normalization/FusedBatchNorm', num_features=128, eps=0.0010000000474974513, momentum=0.0)
        self.encoder_conv2d_1_Conv2D = self.__conv(2, name=base_name+'/conv2d_1/Conv2D', in_channels=128, out_channels=256, kernel_size=(5, 5), stride=(2, 2), groups=1, bias=True)
        self.encoder_batch_normalization_1_FusedBatchNorm = self.__batch_normalization(2, base_name+'/batch_normalization_1/FusedBatchNorm', num_features=256, eps=0.0010000000474974513, momentum=0.0)
        self.encoder_conv2d_2_Conv2D = self.__conv(2, name=base_name+'/conv2d_2/Conv2D', in_channels=256, out_channels=512, kernel_size=(5, 5), stride=(2, 2), groups=1, bias=True)
        self.encoder_batch_normalization_2_FusedBatchNorm = self.__batch_normalization(2, base_name+'/batch_normalization_2/FusedBatchNorm', num_features=512, eps=0.0010000000474974513, momentum=0.0)
        self.encoder_conv2d_3_Conv2D = self.__conv(2, name=base_name+'/conv2d_3/Conv2D', in_channels=512, out_channels=512, kernel_size=(5, 5), stride=(2, 2), groups=1, bias=True)
        self.encoder_batch_normalization_3_FusedBatchNorm = self.__batch_normalization(2, base_name+'/batch_normalization_3/FusedBatchNorm', num_features=512, eps=0.0010000000474974513, momentum=0.0)
        self.encoder_dense_MatMul = self.__dense(name = base_name+'/dense/MatMul', in_features = 32768, out_features = 128, bias = True)

    def forward(self, x):
        encoder_Flatten_flatten_Reshape_shape_1 = torch.tensor(-1, dtype=torch.int32)
        encoder_conv2d_Conv2D_pad = F.pad(x, (1, 2, 1, 2))
        encoder_conv2d_Conv2D = self.encoder_conv2d_Conv2D(encoder_conv2d_Conv2D_pad)
        encoder_conv2d_Relu = F.relu(encoder_conv2d_Conv2D)
        encoder_batch_normalization_FusedBatchNorm = self.encoder_batch_normalization_FusedBatchNorm(encoder_conv2d_Relu)
        encoder_conv2d_1_Conv2D_pad = F.pad(encoder_batch_normalization_FusedBatchNorm, (1, 2, 1, 2))
        encoder_conv2d_1_Conv2D = self.encoder_conv2d_1_Conv2D(encoder_conv2d_1_Conv2D_pad)
        encoder_conv2d_1_Relu = F.relu(encoder_conv2d_1_Conv2D)
        encoder_batch_normalization_1_FusedBatchNorm = self.encoder_batch_normalization_1_FusedBatchNorm(encoder_conv2d_1_Relu)
        encoder_conv2d_2_Conv2D_pad = F.pad(encoder_batch_normalization_1_FusedBatchNorm, (1, 2, 1, 2))
        encoder_conv2d_2_Conv2D = self.encoder_conv2d_2_Conv2D(encoder_conv2d_2_Conv2D_pad)
        encoder_conv2d_2_Relu = F.relu(encoder_conv2d_2_Conv2D)
        encoder_batch_normalization_2_FusedBatchNorm = self.encoder_batch_normalization_2_FusedBatchNorm(encoder_conv2d_2_Relu)
        encoder_conv2d_3_Conv2D_pad = F.pad(encoder_batch_normalization_2_FusedBatchNorm, (1, 2, 1, 2))
        encoder_conv2d_3_Conv2D = self.encoder_conv2d_3_Conv2D(encoder_conv2d_3_Conv2D_pad)
        encoder_conv2d_3_Relu = F.relu(encoder_conv2d_3_Conv2D)
        encoder_batch_normalization_3_FusedBatchNorm = self.encoder_batch_normalization_3_FusedBatchNorm(encoder_conv2d_3_Relu)
        encoder_Flatten_flatten_Shape = list(encoder_batch_normalization_3_FusedBatchNorm.size())
        #encoder_Flatten_flatten_Reshape = torch.reshape(input = encoder_batch_normalization_3_FusedBatchNorm, shape = (-1,32768))
        intermediate = encoder_batch_normalization_3_FusedBatchNorm.permute(0,2,3,1)
        encoder_Flatten_flatten_Reshape = torch.reshape(input = intermediate, shape = (-1,32768))

        encoder_Flatten_flatten_strided_slice = encoder_Flatten_flatten_Shape[0:1]
        encoder_dense_MatMul = self.encoder_dense_MatMul(encoder_Flatten_flatten_Reshape)
        encoder_Flatten_flatten_Reshape_shape = [encoder_Flatten_flatten_strided_slice,encoder_Flatten_flatten_Reshape_shape_1]
        return encoder_dense_MatMul

    @staticmethod
    def __dense(name, **kwargs):
        layer = nn.Linear(**kwargs)
        layer.state_dict()['weight'].copy_(torch.from_numpy(__weights_dict[name]['weights']))
        if 'bias' in __weights_dict[name]:
            layer.state_dict()['bias'].copy_(torch.from_numpy(__weights_dict[name]['bias']))
        return layer

    @staticmethod
    def __batch_normalization(dim, name, **kwargs):
        if   dim == 1:  layer = nn.BatchNorm1d(**kwargs)
        elif dim == 2:  layer = nn.BatchNorm2d(**kwargs)
        elif dim == 3:  layer = nn.BatchNorm3d(**kwargs)
        else:           raise NotImplementedError()

        if 'scale' in __weights_dict[name]:
            layer.state_dict()['weight'].copy_(torch.from_numpy(__weights_dict[name]['scale']))
        else:
            layer.weight.data.fill_(1)

        if 'bias' in __weights_dict[name]:
            layer.state_dict()['bias'].copy_(torch.from_numpy(__weights_dict[name]['bias']))
        else:
            layer.bias.data.fill_(0)

        layer.state_dict()['running_mean'].copy_(torch.from_numpy(__weights_dict[name]['mean']))
        layer.state_dict()['running_var'].copy_(torch.from_numpy(__weights_dict[name]['var']))
        return layer

    @staticmethod
    def __conv(dim, name, **kwargs):
        if   dim == 1:  layer = nn.Conv1d(**kwargs)
        elif dim == 2:  layer = nn.Conv2d(**kwargs)
        elif dim == 3:  layer = nn.Conv3d(**kwargs)
        else:           raise NotImplementedError()

        layer.state_dict()['weight'].copy_(torch.from_numpy(__weights_dict[name]['weights']))
        if 'bias' in __weights_dict[name]:
            layer.state_dict()['bias'].copy_(torch.from_numpy(__weights_dict[name]['bias']))
        return layer


import argparse
import math
import matplotlib.pyplot as plt
import numpy as np
import multiprocessing
import PIL as pil
import time
import torch
import torch.autograd as autograd
import torch.nn as nn

import sys
sys.path.append('loader/')

import loader.camvid_loader as camvid_loader
import loader.utility as utility
import loss.loss as loss_2d
import lr_scheduling
import models.link_net as segment_models
import transformer.transform_policy as transform_policy
import visualize

from torch.autograd import Variable
from torchvision import transforms, utils

parser = argparse.ArgumentParser(description = 'Hyper parameters')

parser.add_argument('--camvid_folder', type = str, default = "/home/ramsus/Qt/computer_vision_dataset/segmentation/camvid/",
                    help = 'This folder should contains the files and folders clone from https://github.com/mostafaizz/camvid and '
                    'the files and folders generated by script process_camvid_data.py')
parser.add_argument('--epoch', type = int, default = 800, help = 'Number of epoch')
parser.add_argument('--batch_size', type = int, default = 16, help = 'Batch of training set')
parser.add_argument('--init_lr', type = float, default = 5e-4, help = 'Initial learning rate')
parser.add_argument('--optimizer', type = str, default = "RMSprop", help = 'Optimizer type, could be Adam, RMSprop or SGD')
parser.add_argument('--cache', type = str, default = 'y', help = 'True will load and cache the training images and labels in ram.'
                    'False will load the images and labels everytime')
parser.add_argument('--save_model_as', type = str, default = 'link_net', help = 'The name of model being saved')
parser.add_argument('--save_per_epoch', type = int, default = 200, help = 'Save the model after epoch % args[save_per_epoch] == 0')
parser.add_argument('--random_crop_height', type = int, default = 320, help = 'Random crop height')
parser.add_argument('--random_crop_width', type = int, default = 480, help = 'Random crop width')
parser.add_argument('--weighthing', type = str, default = 'y', help = 'True will adopt weights on loss function and vice versa')

args = vars(parser.parse_args())

def str_to_bool(v):
    if v.lower() in ('yes', 'true', 't', 'y', '1'):
        return True
    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

#read_color_integer read the rgb value from label_integer.txt, this file store the rgb
#value according to the order of label.
label_colors = utility.read_color_integer(args['camvid_folder'] + "label_integer.txt")

#create loader of camvid dataset
random_crop = transform_policy.random_crop((args['random_crop_height'], args['random_crop_width']), copy = str_to_bool(args['cache']))
transform_func = transforms.Compose([random_crop, transform_policy.flip_horizontal(), transform_policy.to_tensor()])
normalizer = transform_policy.normalize(copy = False)
loader = camvid_loader.create_camvid_loader(args['camvid_folder'] + "train_raws/",
                                            args['camvid_folder'] + "train_int_labels/",
                                            transform_func, normalizer, cache = str_to_bool(args['cache']))

#create link net model
model = segment_models.link_net(len(label_colors))
data_loader = torch.utils.data.DataLoader(loader, batch_size = args['batch_size'], shuffle = True, num_workers = multiprocessing.cpu_count())

#build a dictionary with key = (r,g,b), value = percentage of color
color_info = utility.read_color_count_sorted(args['camvid_folder'] + 'color_count_train_sorted.txt')
color_info = { (data[0], data[1], data[2]): data[4] for data in color_info}

#build the weights matrix according to the formula 1/ln(1.02 + w)
weights = None
if str_to_bool(args['weighthing']):
    print("set weights")
    weights = []
    for color in label_colors:
        w = color_info[color]
        w = 1 / math.log(1.02 + w, 2)
        weights.append(w)

    weights = np.array(weights)
    weights = torch.from_numpy(weights.astype('float32')).cuda()
else:
    print("zero weights")

def train(model, loader, weights, epoch, lr_rate, save_model_as):
    model.cuda()
    model.train(True)
    best_loss = 99999999
    loss_func = loss_2d.cross_entropy_loss_2d(weights)
    optimizer = None
    best_model_epoch = 0
    if args['optimizer'].lower() == 'adam':
        optimizer = torch.optim.Adam(model.parameters(), lr = lr_rate)
    elif args['optimizer'].lower() == 'rmsprop':
        optimizer = torch.optim.RMSprop(model.parameters(), lr = lr_rate)
    elif args['optimizer'].lower() == 'sgd':
        optimizer = torch.optim.SGD(model.parameters(), lr = lr_rate)
    else:
        raise Exception('Wrong optimizer, please pick you optimizer within Adam, RMSprop and SGD')

    #plot_2d update the loss graph every epoch
    plot = visualize.plot_2d(min_x = 0, max_x = epoch)
    for e in range(epoch):
        aggr_loss = 0
        for i, samples in enumerate(loader):
            raws, labels = Variable(samples['raw']), Variable(samples['label'])
            raws, labels = raws.cuda(), labels.cuda()

            optimizer.zero_grad()

            output = model(raws)
            loss = loss_func(output, labels)
            aggr_loss += loss.data[0]

            print("epoch:", e, "batch:", i+1, "aggregate loss:", aggr_loss / (i + 1), "loss:", loss.data[0])

            loss.backward()
            optimizer.step()

            iter_num = len(loader)*e + i
            lr_scheduling.poly_lr_scheduler(optimizer, lr_rate, iter_num)

        plot.update(e + 1, aggr_loss / len(loader))

        #save the model with smallest loss
        if aggr_loss/(len(loader)) < best_loss:
            best_loss = aggr_loss
            torch.save(model.state_dict(), save_model_as + "_epoch_best_model.tch")
            best_model_epoch = e
        #save the model every 200 epoch
        if (e != 0) and (e % args['save_per_epoch'] == 0):
            torch.save(model.state_dict(), save_model_as + "_{}_epoch.tch".format(e))

    plot.savefig(save_model_as + "_{}_epoch.png".format(epoch))
    torch.save(model.state_dict(), save_model_as + "_{}_epoch.tch".format(epoch))
    print('best model occur at epoch:', e, ' aggregate loss is:', aggr_loss/(len(loader)))

    return model

start_time = time.time()
train(model, data_loader, weights, args['epoch'], args['init_lr'], args['save_model_as'])
elapsed_time = time.time() - start_time
print("elapsed time:", elapsed_time)


'''
redo #1
- pay attention to the colon after else
'''

from typing import Optional

class TreeNode:
    def __init__(self, val, left = None, right = None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        def dfs(left, right):
            if left == None  and right == None:
                return True

            if left == None or right == None:
                return False

            is_symmetric = (left.val == right.val and
                            dfs(left.left, right.right) and
                            dfs(left.right, right.left)
                            )
            return is_symmetric

        return(dfs(root.left, root.right))

root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(2)
root.left.left = TreeNode(3)
root.left.right = TreeNode(4)
root.right.left = TreeNode(4)
root.right.right = TreeNode(3)

s = Solution()

print(s.isSymmetric(root))


""" Database abstraction for Rigor """

import sqlalchemy as sa
from sqlalchemy.orm.session import sessionmaker
from contextlib import contextmanager

#: Mapping of database object types to names
kNamingConvention = {
	'ix': '%(table_name)s_%(column_0_name)s_idx',
	'uq': '%(table_name)s_%(column_0_name)s_key',
	'ck': '%(table_name)s_%(constraint_name)s_check',
	'fk': '%(table_name)s_%(referred_table_name)s_%(referred_column_0_name)s_fkey',
	'pk': '%(table_name)s_pkey',
}

kDSNKeys = ('driver', 'username', 'password', 'host', 'port')

class ContextSession(object):
	"""
	Wraps a SQLAlchemy session in a context manager

	:param session: SQLAlchemy session
	:param bool commit: If :py:const:`True`, the session will be committed when it exits the context. If :py:const:`False`, or if there is an exception, the session will be rolled back.
	"""
	def __init__(self, session, commit=True):
		self.session = session
		self._commit = commit

	def __enter__(self):
		return self

	def __exit__(self, exc_type, exc_value, traceback):
		try:
			if exc_type is None:
				if self._commit:
					self.session.commit()
			else:
				self.session.rollback()
		finally:
			self.session.close()

	def __getattr__(self, name):
		return getattr(self.session, name)

class BaseDatabase(object):
	"""
	Abstracts a database engine and sessions

	:param str database: The name of the database
	:param config: Configuration data
	:type config: :py:class:`~rigor.config.RigorConfiguration`
	"""

	@classmethod
	def build_url(cls, database, config):
		"""
		Given the configuration and a database name, this method will generate a SQLAlchemy URL

		:param str database: Name of the database to connect to
		:param config: The Rigor configuration
		:return: a URL
		"""
		args = [config.get('database', key) if ('database', key) in config else None for key in kDSNKeys]
		args.append(database)
		url = sa.engine.url.URL(*args)
		return url

	def __init__(self, database, config):
		self.name = database
		self._config = config
		url = Database.build_url(database, config)
		self._engine = sa.create_engine(url)
		self._metadata = sa.MetaData(bind=self._engine, naming_convention=kNamingConvention)
		self._sessionmaker = sessionmaker(bind=self._engine)

	def get_session(self, commit=True):
		"""
		Gets a new session from the pool. The session in wrapped in a :py:func:`~contextlib.contextmanager`.

		:param bool commit: If :py:const:`True`, the session will be committed when it exits the context. If :py:const:`False`, or if there is an exception, the session will be rolled back.
		:return: new session
		"""
		return ContextSession(self._sessionmaker(), commit)

Database = BaseDatabase


# coding:utf-8
'''
**************************************************
@File   ：python_dp -> 二叉树的层平均值
@IDE    ：PyCharm
@Author ：Small_wind
@Date   ：2020/9/12 20:41
**************************************************
'''
'''给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。
示例 1：
输入：
    3
   / \
  9  20
    /  \
   15   7
输出：[3, 14.5, 11]
解释：
第 0 层的平均值是 3 ,  第1层是 14.5 , 第2层是 11 。因此返回 [3, 14.5, 11] 。
来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/average-of-levels-in-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。'''

from typing import List
from collections import deque       # 双端队列
# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def averageOfLevels(self, root: TreeNode) -> List[float]:
        # 层序遍历，获取节点，入队列，获取队列长度，求值加入
        q = deque()
        q.append(root)  # 初始化
        ans = [];ans.append(root.val)    # 初始化
        size = 1
        sum = 0
        while q:#一直到为空，没有节点了
            for i in range(size):
                tmp = q.pop()       # 出节点，获取节点

                left = tmp.left     # 获取左节点
                if left:
                    q.append(left)
                    sum+=left.val

                right = tmp.right   # 获取右节点
                if right:
                    q.append(right)
                    sum+=right.val
            size = len(q)
            if size!=0:
                ans.append(sum/size)
            sum = 0  # 初始化
        return ans
                # 获取长度

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def averageOfLevels(self, root: TreeNode) -> List[float]:
        # 题目数组非空，
        # if not root:
        #     return []

        # 返回结果
        res = []

        from collections import deque
        # 定义队列
        queue = deque()
        # 将根节点入队
        queue.append(root)
        # 队列不为空，表达式二叉树还有节点，循环遍历
        while queue:
            # 先标记每层的节点数
            size = len(queue)
            # 定义变量，记录每次节点值
            total = 0
            # 这里开始遍历当前层的节点
            for _ in range(size):
                # 出队
                node = queue.popleft()
                # 先将当前节点的值存储
                total += node.val
                # 节点的左右节点非空时，入队
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            # 添加每层的节点值均值
            res.append(total/size)
        return res

树/（树）二叉树的层平均值.py

（树）二叉树的层平均值.py


import math
import numpy as np
from utils import solve_func

def secante(funcao, a, b, precisao):
    # se a maior que b: trocar
    if a > b:
        [a, b] = [b, a]

    iteracao = 0
    f_a = solve_func(a, funcao)
    f_b = solve_func(b, funcao)
    it = []

    it.append([a, f_a])
    it.append([b, f_b])

    # Limite de vezes seguidas que a(k)-a(k-1) > a(k-1)-a(k-2)
    limite = 0

    # Inicialização
    f_x_2 = f_a
    while(abs(f_x_2) > precisao):
        iteracao += 1

        # Atualização do ponto de acordo com a funcao de convergencia
        x_1 = it[-1][0]
        f_x_1 = it[-1][1]
        x_0 = it[-2][0]
        f_x_0 = it[-2][1]
        x_2 = (f_x_1*x_0 - f_x_0*x_1) / (f_x_1 - f_x_0)
        f_x_2 = solve_func(x_2, funcao)
        it.append([x_2, f_x_2])

        # Verificar se está convergindo
        d_anterior = abs(it[iteracao-2][0]-it[iteracao-1][0])
        d_atual = abs(it[iteracao-1][0]-it[iteracao][0])

        if d_anterior < d_atual:
            limite += 1
        else:
            limite = 0

        if limite == 4:
            None, None

    return x_2, it

def main():
    input_txt = open('input-secante.txt', 'r')
    output_txt = open('output-secante.txt', 'w')
    np.set_printoptions(precision=6)
    np.set_printoptions(suppress=True)

    for line in input_txt:
        if line[-1] == '\n':
            line = line[:-1]
        # output_txt.write(line+' ')
        line = line.split(',')
        func = line[0].split('=')[1]
        a = float(line[1].split('=')[1])
        b = float(line[2].split('=')[1])
        precisao = float(line[3].split('=')[1])

        x, iteracoes = secante(func, a, b, precisao)
        if x != None:
            iteracoes = np.asarray(iteracoes)
            f_x = solve_func(x, func)
            x = np.asarray([x])
            f_x = np.asarray([f_x])
            output_txt.write("iteracoes="+str(len(iteracoes))+",x="+str(x)+",f(x)="+str(f_x)+'\n')
        else:
            print("intervalo inválido")
            output_txt.write("intervalo invalido\n")

    input_txt.close()
    output_txt.close()

main()


import math
import os
import numpy as np
import random
from math import e, inf
from numpy.core.fromnumeric import sort
from utils import get_map, map_to_graph
from config import *
from tqdm import tqdm

def cal_cost(graph, traj_list):
    cost = 0
    for i in range(len(traj_list) - 1):
        cost = cost + graph[traj_list[i]][traj_list[i+1]]
    return cost + graph[traj_list[-1]][traj_list[0]]

def generate_random_seeds(max_num):
    x = random.sample(range(max_num + 1), 2)
    return min(x[0], x[1]), max(x[0], x[1])

def is_equal(a, b, error):
    if abs(a - b) < error:
        return True
    else:
        return False

def SA(num_city, map, origin_temp, iter_times, temp_decay):
    """
    Simulated annealing algorithm
    Parameters:
    graph: distance map
    map: coordinates map
    origin_traj_list: original number list [1, 2, 3, ..., num_city]
    origin_temp: temperature at time 0
    iter_times: max times of iteration
    temp_decay: the decay of temperature
    Return:
    None
    """
    graph = map_to_graph(map, num_city)
    last_cost = inf
    last_list = [i for i in range(num_city)]
    now_cost = cal_cost(graph, traj_list=last_list)
    t = origin_temp
    step = 0
    while not is_equal(now_cost, last_cost, 1e-3):
        last_cost = now_cost
        last_iter_cost = now_cost
        for _ in range(iter_times):
            step = step + 1
            u, v = generate_random_seeds(num_city)
            new_list_1 = last_list[:u]
            new_list_2 = list(reversed(last_list[u:v]))
            new_list_3 = last_list[v:]
            new_list = new_list_1 + new_list_2 + new_list_3
            now_iter_cost = last_iter_cost + graph[
                new_list[u-1]][new_list[u]] + graph[new_list[v-1]][new_list[v % num_city]] - graph[
                    last_list[u-1]][last_list[u]] - graph[last_list[v-1]][last_list[v % num_city]]
            # now_iter_cost = cal_cost(graph, new_list)
            if now_iter_cost <  last_iter_cost:
                last_list = new_list
                last_iter_cost = now_iter_cost
            elif now_iter_cost > last_iter_cost:
                pro = math.exp(-(now_iter_cost-last_iter_cost)/t)
                random_bond = np.random.rand()
                if pro > random_bond:
                    # state convert
                    last_list = new_list
                    last_iter_cost = now_iter_cost

        # balance at temperature t
        now_cost = last_iter_cost
        t = t * temp_decay

    return round(now_cost, 2), last_list

if __name__ == '__main__':
    result_list = []
    result = []
    time = []
    for _ in tqdm(range(iters)):
        loss, traj = SA(num_city=num_city, map=map,
                        origin_temp=t0, iter_times=l, temp_decay=alpha)
        result_list.append([loss, traj])
    for item in result_list:
        if item not in result:
            result.append(item)
            time.append(1)
        else:
            id = result.index(item)
            time[id] = time[id] + 1
    for i in range(len(time)):
        result[i].append(time[i])
    result = sorted(result, key=lambda x: x[0])
    if SA_mode == "demo":
        for i, item in enumerate(result):
            get_map(np.array([map[index] for index in item[1]]), rank=i)
            print("The top_{} distance is {}. The trajactory is {}. The repeat time is {}".format(i + 1, item[0], item[1], item[2]))
    elif SA_mode == "exp":
        exp_result = []
        exp_times = []
        for i, item in enumerate(result):
            if item[0] not in exp_result:
                exp_result.append(item[0])
                exp_times.append(item[2])
            else:
                idx = exp_result.index(item[0])
                exp_times[idx] = exp_times[idx] + 1
        for i in range(len(exp_result)):
            print("Loss: {}; Ratio:{}".format(exp_result[i], round(exp_times[i] / iters, 4)))


from module_huiji.danteng_downloader import Downloader
from module_huiji.huijiWikiTabx import HuijiWikiTabx
from ..util import get_skip_list
import os
import re
import shutil
from config import IMAGE_PATH, IMAGE_NEW_PATH, IMAGE_WEAPON_SKILL_PATH, DATA_PATH, GBF_CDN_URL
from ..data.sim import GBFSim

def item(cfg):
    # 开关
    retry_times = 5
    if 'IMAGE' in cfg:
        if 'retry' in cfg['IMAGE']:
            try:
                retry_times = int(cfg['IMAGE']['retry'])
            except:
                pass

    # 先检查本地
    data_base_path = os.path.join(IMAGE_PATH, 'item')
    image_base_url = f'{GBF_CDN_URL}/assets/img/sp/assets/item/'

    # 配置下载器
    downloader = Downloader()
    downloader.set_try_count(retry_times)

    # 下载普通素材道具图标
    # 其他道具图标需要手工下载
    tabx_page_title = f'Data:{cfg["TABX"]["item"]}.tabx'
    item_tabx = HuijiWikiTabx(cfg['wiki'], tabx_page_title, 'ID')
    article_path = os.path.join(data_base_path, 'article_s')
    for item_index, item_info in item_tabx:
        if item_info['ID'] == 0:
            continue
        file_name = f'{item_info["ID"]}.jpg'
        file_path = os.path.join(article_path, file_name)
        if os.path.exists(file_path):
            continue
        download_url = f'{image_base_url}/article/s/{file_name}'
        downloader.download_multi_copies(download_url, [file_path])
    downloader.wait_threads()

    all_list = os.listdir(data_base_path)

    checked_data = {}

    for sub_path_name in all_list:
        sub_full_path = os.path.join(data_base_path, sub_path_name)
        if not os.path.isdir(sub_full_path):
            continue

        sub_path_info = sub_path_name.split('_')
        if len(sub_path_info) != 2 or sub_path_info[1] not in ['s', 'm']:
            continue

        sub_name = sub_path_info[0]
        if sub_name not in checked_data:
            checked_data[sub_name] = []

        all_sub_list = os.listdir(sub_full_path)

        for filename in all_sub_list:
            if filename in checked_data[sub_name]:
                continue
            else:
                checked_data[sub_name].append(filename)

            src_m_path = os.path.join(data_base_path, f'{sub_name}_m', filename)
            src_s_path = os.path.join(data_base_path, f'{sub_name}_s', filename)

            if not os.path.exists(src_m_path):
                download_url = f'{image_base_url}/{sub_name}/m/{filename}'
                downloader.download_multi_copies(download_url, [src_m_path])
            if not os.path.exists(src_s_path):
                download_url = f'{image_base_url}/{sub_name}/s/{filename}'
                downloader.download_multi_copies(download_url, [src_s_path])
        downloader.wait_threads()

    # 复制到wiki目录中，如果有新图片就复制到new里
    for sub_name, all_sub_list in checked_data.items():
        for filename in all_sub_list:
            dst_filename = filename.replace('item_', '')
            for sub_type in ['s', 'm']:

                src_path = os.path.join(data_base_path, f'{sub_name}_{sub_type}', filename)
                if sub_name == 'article':
                    dst_path = os.path.join(data_base_path, 'wiki', f'IT_{sub_type}_{dst_filename}')
                    new_path = os.path.join(IMAGE_PATH, IMAGE_NEW_PATH, f'IT_{sub_type}_{dst_filename}')
                else:
                    dst_path = os.path.join(data_base_path, 'wiki', f'IT_{sub_name}_{sub_type}_{dst_filename}')
                    new_path = os.path.join(IMAGE_PATH, IMAGE_NEW_PATH, f'IT_{sub_name}_{sub_type}_{dst_filename}')

                if os.path.exists(dst_path):
                    continue

                shutil.copy2(src_path, dst_path)
                shutil.copy2(src_path, new_path)


import requests
import json
import sys
if sys.argv[len(sys.argv) - 1] == "local":
    new_pet_url = "http://localhost:8080/pet"
else:
    new_pet_url = "https://cs496final-186222.appspot.com/pet"

class pet:
    def __init__(self, n, s, a, w):
        self.name = n
        self.species = s
        self.age = a
        self.weight = w
    name = ""
    species = ""
    age = 0
    weight = 0

pt1 = pet("Hazel", "Cat", 5, 10)
pt2 = pet("Fallon", "Cat", 5, 9)
pt3 = pet("Freddy", "Cat", 6, 12)
pt4 = pet("Rocky", "Dog", 4, 20)
pt5 = pet("Duchess", "Dog", 6, 22)
pets = [pt1, pt2, pt3, pt4, pt5]

for p in pets:
    j = json.dumps(p.__dict__)
    r = requests.post(new_pet_url, j)
    print(r.text)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
.. module:: core.py
   :platform: Unix
   :synopsis: Contains configuration and control code.

.. moduleauthor:: Matt

Contains configuration and control code.

A Configuration Object contains the parsed results of a properly formatted JSON config file.
Each config file can contain multiple configurations, combinations of readers and layers.

A Controller instance is passed an instance of a Configuration object, and the name of an
individual configuration within the Configuration object to process.

For each layer within the named configuration, the Controller creates an instance of the
LayerConstructor class.

A LayerConstructor is responsible for executing processing steps for that layer parsed
from the configuration file, generating some type of computed output.
"""
__author__ = 'mkenny'
from .processors import *
from simplejson import loads as json_loads

class Configuration(object):
    """
    This class is responsible the conversion of a JSON-formatted configuration
    file into a python object.

    Public Attributes:

    :param str path: The full pathway to a JSON-formatted configuration file.
    :param dict configs: Configuration info populated by parsedConfig.
    """

    def __init__(self):
        """
        Requires full pathway to configuration path.
        """
        self.path = ''
        self.configs = {}

    def _get_config_data(self, path):
        """
        Given a file path to a JSON config file, open and
        convert to a python object.

        :param str path: full pathway to a JSON-formatted config file.
        """
        with open(path) as config_file:
            config_string = config_file.read()
            config_data = json_loads(config_string)
            return config_data

    def _validate_config(self, config_data):
        """
        Not implemented.
        Validate we have all required attributes.

        :param dict config_data: parsed config info via _get_config_data()
        """
        return config_data

    def parse_config(self, path):
        """
        Returns self.configs after setting instance attribute.

        Given a configuration path, call methods to validate
        and parse the file into a useable python object.

        :param str path: full pathway to a JSON-formatted config file.
        """
        parsed_json = self._get_config_data(path)
        if self._validate_config(parsed_json):
            # Generate a dictionary of individual configs with a
            # ConfigCollection JSON object.
            for config in parsed_json['configs']:
                self.configs[config['name']] = {
                    'readers': config['readers'],
                    'layers': config['layers']
                }
        return self.configs

class Controller(object):
    """
    Given a configuration object and config name, manage the creation of
    LayerConstructor instances, one for each layer.

    :param config_collection: Config instance to be passed to the controller.
    :param config_name: Name of the individual config within a ConfigCollection to be processed.
    """

    def __init__(self, config_collection, config_name):
        self.config_name = config_name
        self.readers = config_collection.configs[self.config_name]['readers']
        self.layers = config_collection.configs[self.config_name]['layers']

    def process_layers(self):
        """
        Create a LayerConstructor for each layer.
        Initiate processing calling the LayerConstructor's serialize() method.
        """
        # Spawn LayerConstructor Instances for each layer.
        for layer in self.layers:
            # Extract processing steps for a layer
            layer_name = layer['name']
            processing_steps = layer['processing_steps']
            rBuild_Inst = LayerConstructor(layer_name, processing_steps, self.readers)
            rBuild_Inst.serialize()

class LayerConstructor(object):
    """
    A LayerConstructor is composed of a reader and n-number of processors.
    It is responsible for building a decorated series of processing instances,
    and executing them.

    :param layer_name: layer name extracted from a configuration file.
    :param processing_steps: processor class references to be applied to a record.
    :param readers: reader classes references extracted from a config file.
    """

    def __init__(self, layer_name, processing_steps, readers):
        self.layer_name = layer_name
        self.processing_steps = processing_steps
        self.readers = readers

    def _get_processor_class(self, name, base_class):
        """
        Given a string, test if a Processor class exists by that name.
        """
        # A valid processor should be an explicit subclass of ProcessorBaseClass
        for processor in base_class.__subclasses__():
            if name == processor.__name__:
                return processor
        # No processor returned, raise exception.
        raise TypeError("ERROR: %s processor does not exist" % name)

    def _build_decorated_classes(self, initial_processor, processors, BaseClass):
        """Construct decorator chain, and begin processing workflow."""
        decorated_processor = initial_processor
        # Can pass **kwargs when instantiating a class. If that class contains a
        # property assigned at init sharing the name of a key in the **kwargs dict,
        # that property will be assigned the value extracted from **kwargs
        for processor_dict in processors:
            for processor_name, processor_args in processor_dict.iteritems():
                # Add readers to processor_args dict
                # Conditional fails if value of None is used in JSON config for a processor.
                if processor_args:
                    processor_args['readers'] = self.readers
                else:
                    # Need an empty dict to pass as **kwargs
                    processor_args = {}
                # Create an actual instance of the processor, passing in its kwargs and references to all readers.
                processor_class = self._get_processor_class(processor_name, BaseClass)
                decorated_processor = processor_class(decorated_processor, **processor_args)
        # Start Execution of Processing Pipe
        # Can I pass this thing something it already knows?
        # I guess we expect that we always start with ProcessorGetData, should probably raise an error.
        decorated_processor.process(decorated_processor.reader_name)

    def serialize(self):
        """
        Setup to begin processing pipeline.

        - Reverses processing steps such that the last member of the ``self.processing_steps``
          list becomes the innermost decorated member of the processing pipeline.
        - Adds an instance of ``ProcessorDevNull()`` to the outermost (last) step of the chain,
          this ensures that input iterable is iterated until exhaustion.
        - Executes processing steps via ``self._build_decorated_classes()`` method.
        """
        self.processing_steps.reverse()
        initial_processor = ProcessorDevNull()
        self._build_decorated_classes(initial_processor, self.processing_steps, ProcessorBaseClass)


import bpy
import os
import time

from .wmo_file import WMOFile

def import_wmo_to_blender_scene(filepath, load_textures, import_doodads, group_objects):
    """ Read and import WoW WMO object to Blender scene"""

    start_time = time.time()

    wmo = WMOFile(filepath)
    wmo.read()

    print("\n\n### Importing WMO components ###")

    game_data = None

    if load_textures or import_doodads:
        game_data = getattr(bpy, "wow_game_data", None)

        if not game_data:
            print("\n\n### Loading game data ###")
            bpy.ops.scene.load_wow_filesystem()
            game_data = bpy.wow_game_data

        if game_data.files:
            if load_textures:
                print("\n\n### Extracting textures ###")
                game_data.extract_textures_as_png(os.path.dirname(filepath), wmo.motx.get_all_strings())
        else:
            print("\nFailed to load textures because game data was not loaded.")

    # group objects if required
    parent = None
    if group_objects:
        bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0, 0, 0))
        parent = bpy.context.scene.objects.active
        parent.name = wmo.display_name + ".wmo"
        wmo.parent = parent

    # load all materials in root file
    wmo.load_materials()

    # load all WMO components
    wmo.load_lights()
    wmo.load_properties()
    wmo.load_fogs()

    print("\n\n### Importing WMO groups ###")

    for group in wmo.groups:
        obj_name = wmo.mogn.get_string(group.mogp.GroupNameOfs)
        print("\nImporting group <<{}>>".format(obj_name))
        group.load_object(obj_name, import_doodads)

    wmo.load_portals()

    print("\n\n### Importing WMO doodad sets ###")

    if import_doodads and game_data.files:
        wmo.load_doodads(os.path.dirname(filepath), game_data)
    else:
        wmo.load_doodads()

    print("\nDone importing WMO. \nTotal import time: ",
          time.strftime("%M minutes %S seconds.\a", time.gmtime(time.time() - start_time)))

    return parent


# -*- encoding: utf-8 -*-
"""
@Project    :   SourceHub
@File       :   urls.py
@Time       :   2021/4/13 0:49
@Author     :   Thooooor
@Version    :   1.0
@Contact    :   thooooor999@gmail.com
@Describe   :   None
"""
from django.urls import path

from . import views

app_name = "school"

urlpatterns = [
    path("school-list/", views.school_list, name="school-list"),
    path("school-detail/<int:id>/",views.school_detail, name="school-detail"),

]


import sys
sys.path.append('./xsvc')
import xsvc
import json
import datetime
import dateutil.parser

def _adder(request):
    return 100

xs=xsvc.XSVC()
tmpreq={'request':'blp_get_historical_data','params':{'securities':['usdkrw curncy'],'fields':['px_last'],'startdate':'20150601','enddate':'20150701'}}
xs.connect('localhost',2000)
tmp2=json.loads(xs.request(tmpreq))['result'][0]['field_data']

ts={}
for elm in tmp2:
    date=dateutil.parser.parse(elm['date'])
    date_str=date.strftime('%Y-%m-%d')
    ts[date_str]=elm['px_last']

print(ts)
xs.registerService('adder',_adder)
#xsvc.serve()
xs.close()


import argparse

import gym
import numpy as np

from mani_skill2.envs.sapien_env import BaseEnv
from mani_skill2.utils.wrappers import RecordEpisode

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("-e", "--env-id", type=str, default="PickCube-v0")
    parser.add_argument("-o", "--obs-mode", type=str, default="none")
    parser.add_argument("--reward-mode", type=str)
    parser.add_argument("-c", "--control-mode", type=str)
    parser.add_argument("--render-mode", type=str)
    parser.add_argument("--record-dir", type=str)
    args, opts = parser.parse_known_args()

    # Parse env kwargs
    print("opts:", opts)
    eval_str = lambda x: eval(x[1:]) if x.startswith("@") else x
    env_kwargs = dict((x, eval_str(y)) for x, y in zip(opts[0::2], opts[1::2]))
    print("env_kwargs:", env_kwargs)
    args.env_kwargs = env_kwargs

    return args

def main():
    np.set_printoptions(suppress=True, precision=3)
    args = parse_args()

    env: BaseEnv = gym.make(
        args.env_id,
        obs_mode=args.obs_mode,
        reward_mode=args.reward_mode,
        control_mode=args.control_mode,
        **args.env_kwargs
    )

    record_dir = args.record_dir
    if record_dir:
        record_dir = record_dir.format(env_id=args.env_id)
        env = RecordEpisode(env, record_dir, render_mode=args.render_mode)

    print("Observation space", env.observation_space)
    print("Action space", env.action_space)
    print("Control mode", env.control_mode)
    print("Reward mode", env.reward_mode)

    obs = env.reset()
    if args.render_mode is not None:
        env.render(args.render_mode)

    while True:
        action = env.action_space.sample()
        obs, reward, done, info = env.step(action)
        print("reward", reward)
        print("done", done)
        print("info", info)

        if args.render_mode is not None:
            env.render(args.render_mode)

        if done:
            break

if __name__ == "__main__":
    main()