# -*- coding: utf-8 -*-
#
# Licensed under the terms of the BSD 3-Clause or the CeCILL-B License
# (see codraft/__init__.py for details)

"""
CodraFT Action handler module

This module handles all application actions (menus, toolbars, context menu).
These actions point to CodraFT panels, processors, objectlist, ...
"""

# pylint: disable=invalid-name  # Allows short reference names like x, y, ...

import abc
import enum

from guidata.configtools import get_icon
from guidata.qthelpers import add_actions, create_action
from qtpy import QtGui as QG
from qtpy import QtWidgets as QW

from codraft.config import _
from codraft.widgets import fitdialog

class ActionCategory(enum.Enum):
    """Action categories"""

    FILE = enum.auto()
    EDIT = enum.auto()
    VIEW = enum.auto()
    OPERATION = enum.auto()
    PROCESSING = enum.auto()
    COMPUTING = enum.auto()

class BaseActionHandler(metaclass=abc.ABCMeta):
    """Object handling panel GUI interactions: actions, menus, ..."""

    OBJECT_STR = ""  # e.g. "signal"

    def __init__(self, panel, objlist, itmlist, processor, toolbar):
        self.panel = panel
        self.objlist = objlist
        self.itmlist = itmlist
        self.processor = processor
        self.feature_actions = {}
        self.operation_end_actions = None
        self.delete_roi_action = None
        # Object selection dependent actions
        self.actlist_1more = []
        self.actlist_2more = []
        self.actlist_1 = []
        self.actlist_2 = []
        self.actlist_cmenu = []  # Context menu
        if self.__class__ is not BaseActionHandler:
            self.create_all_actions(toolbar)

    def get_context_menu_actions(self):
        """Return context menu action list"""
        return self.actlist_cmenu

    def selection_rows_changed(self):
        """Number of selected rows has changed"""
        selrows = self.objlist.get_selected_rows()
        nbrows = len(selrows)
        for act in self.actlist_1more:
            act.setEnabled(nbrows >= 1)
        for act in self.actlist_2more:
            act.setEnabled(nbrows >= 2)
        for act in self.actlist_1:
            act.setEnabled(nbrows == 1)
        for act in self.actlist_2:
            act.setEnabled(nbrows == 2)
        self.delete_roi_action.setEnabled(False)
        for row in selrows:
            obj = self.objlist[row]
            if obj.roi is not None:
                self.delete_roi_action.setEnabled(True)
                break

    def create_all_actions(self, toolbar):
        """Setup actions, menus, toolbar"""
        featact = self.feature_actions
        featact[ActionCategory.FILE] = file_act = self.create_file_actions()
        featact[ActionCategory.EDIT] = edit_act = self.create_edit_actions()
        featact[ActionCategory.VIEW] = view_act = self.create_view_actions()
        featact[ActionCategory.OPERATION] = self.create_operation_actions()
        featact[ActionCategory.PROCESSING] = self.create_processing_actions()
        featact[ActionCategory.COMPUTING] = self.create_computing_actions()
        add_actions(toolbar, file_act + [None] + edit_act + [None] + view_act)

    def cra(
        self, title, triggered=None, toggled=None, shortcut=None, icon=None, tip=None
    ):
        """Create action convenience method"""
        return create_action(self.panel, title, triggered, toggled, shortcut, icon, tip)

    def create_file_actions(self):
        """Create file actions"""
        new_act = self.cra(
            _("New %s...") % self.OBJECT_STR,
            icon=get_icon(f"new_{self.OBJECT_STR}.svg"),
            tip=_("Create new %s") % self.OBJECT_STR,
            triggered=self.panel.new_object,
            shortcut=QG.QKeySequence(QG.QKeySequence.New),
        )
        open_act = self.cra(
            _("Open %s...") % self.OBJECT_STR,
            icon=get_icon("libre-gui-import.svg"),
            tip=_("Open %s") % self.OBJECT_STR,
            triggered=self.panel.open_objects,
            shortcut=QG.QKeySequence(QG.QKeySequence.Open),
        )
        save_act = self.cra(
            _("Save %s...") % self.OBJECT_STR,
            icon=get_icon("libre-gui-export.svg"),
            tip=_("Save selected %s") % self.OBJECT_STR,
            triggered=self.panel.save_objects,
            shortcut=QG.QKeySequence(QG.QKeySequence.Save),
        )
        importmd_act = self.cra(
            _("Import metadata into %s...") % self.OBJECT_STR,
            icon=get_icon("metadata_import.svg"),
            tip=_("Import metadata into %s") % self.OBJECT_STR,
            triggered=self.panel.import_metadata_from_file,
        )
        exportmd_act = self.cra(
            _("Export metadata from %s...") % self.OBJECT_STR,
            icon=get_icon("metadata_export.svg"),
            tip=_("Export selected %s metadata") % self.OBJECT_STR,
            triggered=self.panel.export_metadata_from_file,
        )
        self.actlist_1more += [save_act]
        self.actlist_cmenu += [save_act]
        self.actlist_1 += [importmd_act, exportmd_act]
        return [new_act, open_act, save_act, None, importmd_act, exportmd_act]

    def create_edit_actions(self):
        """Create edit actions"""
        dup_action = self.cra(
            _("Duplicate"),
            icon=get_icon("libre-gui-copy.svg"),
            triggered=self.panel.duplicate_object,
            shortcut=QG.QKeySequence(QG.QKeySequence.Copy),
        )
        cpymeta_action = self.cra(
            _("Copy metadata"),
            icon=get_icon("metadata_copy.svg"),
            triggered=self.panel.copy_metadata,
        )
        pstmeta_action = self.cra(
            _("Paste metadata"),
            icon=get_icon("metadata_paste.svg"),
            triggered=self.panel.paste_metadata,
        )
        cleanup_action = self.cra(
            _("Clean up data view"),
            icon=get_icon("libre-tools-vacuum-cleaner.svg"),
            tip=_("Clean up data view before updating plotting panels"),
            toggled=self.itmlist.toggle_cleanup_dataview,
        )
        cleanup_action.setChecked(True)
        delm_action = self.cra(
            _("Delete object metadata"),
            icon=get_icon("metadata_delete.svg"),
            tip=_("Delete all that is contained in object metadata"),
            triggered=self.panel.delete_metadata,
        )
        delall_action = self.cra(
            _("Delete all"),
            shortcut="Shift+Ctrl+Suppr",
            icon=get_icon("delete_all.svg"),
            triggered=self.panel.delete_all_objects,
        )
        del_action = self.cra(
            _("Remove"),
            icon=get_icon("delete.svg"),
            triggered=self.panel.remove_object,
            shortcut=QG.QKeySequence(QG.QKeySequence.Delete),
        )
        self.actlist_1more += [
            dup_action,
            del_action,
            delm_action,
            pstmeta_action,
            delall_action,
        ]
        self.actlist_cmenu += [dup_action, del_action]
        self.actlist_1 += [cpymeta_action]
        return [
            dup_action,
            del_action,
            delall_action,
            None,
            cpymeta_action,
            pstmeta_action,
            delm_action,
        ]

    def create_view_actions(self):
        """Create view actions"""
        view_action = self.cra(
            _("View in a new window"),
            icon=get_icon("libre-gui-binoculars.svg"),
            triggered=self.panel.open_separate_view,
        )
        showlabel_action = self.cra(
            _("Show graphical object titles"),
            icon=get_icon("show_titles.svg"),
            tip=_("Show or hide ROI and other graphical object titles or subtitles"),
            toggled=self.panel.toggle_show_titles,
        )
        showlabel_action.setChecked(False)
        self.actlist_1more += [view_action]
        self.actlist_cmenu = [view_action, None] + self.actlist_cmenu
        return [view_action, showlabel_action]

    def create_operation_actions(self):
        """Create operation actions"""
        proc = self.processor
        sum_action = self.cra(_("Sum"), proc.compute_sum)
        average_action = self.cra(_("Average"), proc.compute_average)
        diff_action = self.cra(_("Difference"), lambda: proc.compute_difference(False))
        qdiff_action = self.cra(
            _("Quadratic difference"), lambda: proc.compute_difference(True)
        )
        prod_action = self.cra(_("Product"), proc.compute_product)
        div_action = self.cra(_("Division"), proc.compute_division)
        roi_action = self.cra(
            _("ROI extraction"),
            proc.extract_roi,
            icon=get_icon(f"{self.OBJECT_STR}_roi.svg"),
        )
        swapaxes_action = self.cra(_("Swap X/Y axes"), proc.swap_axes)
        abs_action = self.cra(_("Absolute value"), proc.compute_abs)
        log_action = self.cra("Log10(y)", proc.compute_log10)
        self.actlist_1more += [roi_action, swapaxes_action, abs_action, log_action]
        self.actlist_2more += [sum_action, average_action, prod_action]
        self.actlist_2 += [diff_action, qdiff_action, div_action]
        self.operation_end_actions = [roi_action, swapaxes_action]
        return [
            sum_action,
            average_action,
            diff_action,
            qdiff_action,
            prod_action,
            div_action,
            None,
            abs_action,
            log_action,
        ]

    def create_processing_actions(self):
        """Create processing actions"""
        proc = self.processor
        threshold_action = self.cra(_("Thresholding"), proc.compute_threshold)
        clip_action = self.cra(_("Clipping"), proc.compute_clip)
        lincal_action = self.cra(_("Linear calibration"), proc.calibrate)
        gauss_action = self.cra(_("Gaussian filter"), proc.compute_gaussian)
        movavg_action = self.cra(_("Moving average"), proc.compute_moving_average)
        movmed_action = self.cra(_("Moving median"), proc.compute_moving_median)
        wiener_action = self.cra(_("Wiener filter"), proc.compute_wiener)
        fft_action = self.cra(_("FFT"), proc.compute_fft)
        ifft_action = self.cra(_("Inverse FFT"), proc.compute_ifft)
        for act in (fft_action, ifft_action):
            act.setToolTip(_("Warning: only real part is plotted"))
        actions = [
            threshold_action,
            clip_action,
            lincal_action,
            gauss_action,
            movavg_action,
            movmed_action,
            wiener_action,
            fft_action,
            ifft_action,
        ]
        self.actlist_1more += actions
        return actions

    @abc.abstractmethod
    def create_computing_actions(self):
        """Create computing actions"""
        proc = self.processor
        defineroi_action = self.cra(
            _("Edit regions of interest..."),
            triggered=proc.edit_regions_of_interest,
            icon=get_icon("roi.svg"),
        )
        self.delete_roi_action = self.cra(
            _("Remove regions of interest"),
            triggered=proc.delete_regions_of_interest,
            icon=get_icon("roi_delete.svg"),
        )
        stats_action = self.cra(
            _("Statistics") + "...",
            triggered=proc.compute_stats,
            icon=get_icon("stats.svg"),
        )
        self.actlist_1 += [defineroi_action, self.delete_roi_action, stats_action]
        self.actlist_cmenu += [
            None,
            defineroi_action,
            self.delete_roi_action,
            None,
            stats_action,
        ]
        return [defineroi_action, self.delete_roi_action, None, stats_action]

class SignalActionHandler(BaseActionHandler):
    """Object handling signal panel GUI interactions: actions, menus, ..."""

    OBJECT_STR = _("signal")

    def create_operation_actions(self):
        """Create operation actions"""
        base_actions = super().create_operation_actions()
        proc = self.processor
        peakdetect_action = self.cra(
            _("Peak detection"),
            proc.detect_peaks,
            icon=get_icon("peak_detect.svg"),
        )
        self.actlist_1more += [peakdetect_action]
        roi_actions = self.operation_end_actions
        return base_actions + [None, peakdetect_action, None] + roi_actions

    def create_processing_actions(self):
        """Create processing actions"""
        base_actions = super().create_processing_actions()
        proc = self.processor
        normalize_action = self.cra(_("Normalize"), proc.normalize)
        deriv_action = self.cra(_("Derivative"), proc.compute_derivative)
        integ_action = self.cra(_("Integral"), proc.compute_integral)
        polyfit_action = self.cra(_("Polynomial fit"), proc.compute_polyfit)
        mgfit_action = self.cra(_("Multi-Gaussian fit"), proc.compute_multigaussianfit)

        def cra_fit(title, fitdlgfunc):
            """Create curve fitting action"""
            return self.cra(title, lambda: proc.compute_fit(title, fitdlgfunc))

        gaussfit_action = cra_fit(_("Gaussian fit"), fitdialog.gaussianfit)
        lorentzfit_action = cra_fit(_("Lorentzian fit"), fitdialog.lorentzianfit)
        voigtfit_action = cra_fit(_("Voigt fit"), fitdialog.voigtfit)
        actions1 = [normalize_action, deriv_action, integ_action]
        actions2 = [
            gaussfit_action,
            lorentzfit_action,
            voigtfit_action,
            polyfit_action,
            mgfit_action,
        ]
        self.actlist_1more += actions1 + actions2
        return actions1 + [None] + base_actions + [None] + actions2

    def create_computing_actions(self):
        """Create computing actions"""
        base_actions = super().create_computing_actions()
        proc = self.processor
        fwhm_action = self.cra(
            _("Full width at half-maximum"),
            triggered=proc.compute_fwhm,
            tip=_("Compute Full Width at Half-Maximum (FWHM)"),
        )
        fw1e2_action = self.cra(
            _("Full width at") + " 1/e²",
            triggered=proc.compute_fw1e2,
            tip=_("Compute Full Width at Maximum") + "/e²",
        )
        self.actlist_1more += [fwhm_action, fw1e2_action]
        return base_actions + [fwhm_action, fw1e2_action]

class ImageActionHandler(BaseActionHandler):
    """Object handling image panel GUI interactions: actions, menus, ..."""

    OBJECT_STR = _("image")

    def create_view_actions(self):
        """Create view actions"""
        base_actions = super().create_view_actions()
        showcontrast_action = self.cra(
            _("Show contrast panel"),
            icon=get_icon("contrast.png"),
            tip=_("Show or hide contrast adjustment panel"),
            toggled=self.panel.toggle_show_contrast,
        )
        showcontrast_action.setChecked(True)
        self.actlist_1more += [showcontrast_action]
        return base_actions + [showcontrast_action]

    def create_operation_actions(self):
        """Create operation actions"""
        base_actions = super().create_operation_actions()
        proc = self.processor
        rotate_menu = QW.QMenu(_("Rotation"), self.panel)
        hflip_act = self.cra(
            _("Flip horizontally"),
            triggered=proc.flip_horizontally,
            icon=get_icon("flip_horizontally.svg"),
        )
        vflip_act = self.cra(
            _("Flip vertically"),
            triggered=proc.flip_vertically,
            icon=get_icon("flip_vertically.svg"),
        )
        rot90_act = self.cra(
            _("Rotate %s right") % "90°",  # pylint: disable=consider-using-f-string
            triggered=proc.rotate_270,
            icon=get_icon("rotate_right.svg"),
        )
        rot270_act = self.cra(
            _("Rotate %s left") % "90°",  # pylint: disable=consider-using-f-string
            triggered=proc.rotate_90,
            icon=get_icon("rotate_left.svg"),
        )
        rotate_act = self.cra(
            _("Rotate arbitrarily..."), triggered=proc.rotate_arbitrarily
        )
        resize_act = self.cra(_("Resize"), triggered=proc.resize_image)
        logp1_act = self.cra("Log10(z+n)", triggered=proc.compute_logp1)
        flatfield_act = self.cra(
            _("Flat-field correction"), triggered=proc.flat_field_correction
        )
        self.actlist_2 += [flatfield_act]
        self.actlist_1more += [
            resize_act,
            hflip_act,
            vflip_act,
            logp1_act,
            rot90_act,
            rot270_act,
            rotate_act,
        ]
        self.actlist_cmenu += [None, hflip_act, vflip_act, rot90_act, rot270_act]
        add_actions(
            rotate_menu, [hflip_act, vflip_act, rot90_act, rot270_act, rotate_act]
        )
        roi_actions = self.operation_end_actions
        actions = [
            logp1_act,
            flatfield_act,
            None,
            rotate_menu,
            None,
            resize_act,
        ]
        return base_actions + actions + roi_actions

    def create_computing_actions(self):
        """Create computing actions"""
        base_actions = super().create_computing_actions()
        proc = self.processor
        # TODO: [P3] Add "Create ROI grid..." action to create a regular grid or ROIs
        cent_act = self.cra(
            _("Centroid"), proc.compute_centroid, tip=_("Compute image centroid")
        )
        encl_act = self.cra(
            _("Minimum enclosing circle center"),
            proc.compute_enclosing_circle,
            tip=_("Compute smallest enclosing circle center"),
        )
        peak_act = self.cra(
            _("2D peak detection"),
            proc.compute_peak_detection,
            tip=_("Compute automatic 2D peak detection"),
        )
        contour_act = self.cra(
            _("Contour detection"),
            proc.compute_contour_shape,
            tip=_("Compute contour shape fit"),
        )
        self.actlist_1more += [cent_act, encl_act, peak_act, contour_act]
        return base_actions + [cent_act, encl_act, peak_act, contour_act]


#Estudando Listas/São mutáveis
num = [2, 3, 6, 9, 13]
num [3] = 7 #Alterar um item
num.append(7) #Adicionar um item substituindo
num.sort(reverse=True) #Colocando em ordem decrescente
num.sort() #colocando em ordem crescente
num.insert(2, 0) #Insere um valor sem substituir
num.pop(2) #remove um item da lista
print(num)
print(f'Esta lista tem {len(num)} elementos.')

valores = []
for cont in range(0, 5):
    valores.append(int(input('Digite um valor: ')))
#valores.append(8)
#valores.append(7)
#valores.append(4)
#valores.append(13)

for c, v in enumerate(valores):
    print(f'Na posição {c} encontrei o valor {v}!')
print('Chueguei ao final da lista.')


import random

lists_with_tuples = []
wrong_lst = []

def _print_info(lang: str):
    print("Hello, remember that u should use '_' instead ' ' and '/' with two translations.")

    if lang == 'en':
        print("There will be random form and u should write forms from left to right and then translation")
    else:
        print("There will be random translation and u should write forms from left to right with spaces")

    print("")

with open('word_list.txt', 'r', encoding='utf-8') as f:
    lines = f.readlines()

for el in lines:
    curr_tuple = el.split(" ")
    if '\n' in curr_tuple[-1]:
        curr_tuple[-1] = curr_tuple[-1][:-1]
    lists_with_tuples.append(curr_tuple)

if __name__ == '__main__':
    lang = input("Would you like to learn: 'en' or 'ru' ? ")
    _print_info(lang)

    if lang == 'en':
        lst = list(range(0, len(lists_with_tuples)))
        random.shuffle(lst)
        for n, id in enumerate(lst):
            num_from = random.randint(0, 2)
            answer = input(f'{n+1}/{len(lst)}. {lists_with_tuples[id][num_from]}: ').lower()
            currect_answer = lists_with_tuples[id].copy()
            currect_answer.remove(lists_with_tuples[id][num_from])
            for num, words_from_answer in enumerate(answer.split(" ")):
                if words_from_answer != currect_answer[num]:
                    print(f"Sorry, but you are wrong! Right is '{lists_with_tuples[id]}'")
                    if input("Do you want to add the word to the mistakes dict ? ").lower() in ('yes', 'y', 'да', '+', 'д'):
                        wrong_lst.append(lists_with_tuples[id])
                    break
            print('_________________')

    else:
        lst = list(range(0, len(lists_with_tuples)))
        random.shuffle(lst)
        for n, id in enumerate(lst):
            answer = input(f'{n+1}/{len(lst)}. {lists_with_tuples[id][-1]}: ').lower()

            for num, forms_from_answer in enumerate(answer.split(" ")):
                if lists_with_tuples[id][num] != forms_from_answer:
                    print(f"Sorry, but you are wrong in {num+1} form! Right is '{lists_with_tuples[id]}'")
                    if input("Do you want to add the word to the mistakes dict ? ").lower() in ('yes', 'y', 'да', '+', 'д'):
                        wrong_lst.append(lists_with_tuples[id])

            print('_________________')

    print(f'You were wrong in the next words ({wrong_lst.__len__()}/{len(lst)}):')
    for el in wrong_lst:
        print(el)


class Solution:
    def mostPoints(self, questions: List[List[int]]) -> int:
        """
        Returns the maximum points that can be earned by solving questions.

        Args:
            questions: A 2D array of integers where questions[i] = [pointsi, brainpoweri].

        Returns:
            The maximum points that can be earned.
        """
        n = len(questions)
        @cache
        def go(index):
            if index >= n: return 0
            points, skip = questions[index]
            res = go(index + 1)
            res = max(res, points + go(index + skip + 1))
            return res
        return go(0)

LeetCode/Python3/Medium/2140. Solving Questions With Brainpower/2140-solving-questions-with-brainpower.py


# -*- encoding: utf8 -*-
import json
import re
import urllib

import bones.event
from bones.bot import Module

class NickFix(Module):
    def __init__(self, *args, **kwargs):
        Module.__init__(self, *args, **kwargs)
        self.nickIWant = None
        self.isRecovering = False

    @bones.event.handler(event=bones.event.UserQuitEvent)
    @bones.event.handler(event=bones.event.UserNickChangedEvent)
    def somethingHappened(self, myEvent):
        user = None
        if self.nickIWant is None:
            self.nickIWant = \
                self.settings.get("bot", "nickname").split("\n")[0]

        if isinstance(myEvent, bones.event.UserNickChangedEvent) is True:
            user = myEvent.oldname
        else:
            user = myEvent.user.nickname

        if user.lower() == self.nickIWant.lower():
            myEvent.client.factory.nicknames = \
                self.settings.get("bot", "nickname").split("\n")[1:]
            self.isRecovering = True
            myEvent.client.setNick(self.nickIWant)

    @bones.event.handler(event=bones.event.BotSignedOnEvent)
    def resetMe(self, event):
        self.isRecovering = False
        self.nickIWant = None

    @bones.event.handler(event=bones.event.PreNicknameInUseError)
    def shouldWeEvenTry(self, event):
        if self.isRecovering:
            event.isCancelled = True
            self.isRecovering = False

class Ping(Module):
    def __init__(self, *args, **kwargs):
        Module.__init__(self, *args, **kwargs)
        self.ongoingPings = {}

    @bones.event.handler(trigger="ping")
    def cmdPing(self, event):
        nick = event.user.nickname
        if nick not in self.ongoingPings:
            self.ongoingPings[nick] = event.channel.name
            event.user.ping()
        else:
            event.user.notice(
                "Please wait until your ongoing ping in %s is finished until "
                "trying again."
                % self.ongoingPings[nick]
            )

    @bones.event.handler(event=bones.event.CTCPPongEvent)
    def eventPingResponseReceive(self, event):
        nick = event.user.nickname
        if nick in self.ongoingPings:
            event.user.notice("%s: Your response time was %.3f seconds."
                              % (nick, event.secs))
            del self.ongoingPings[nick]

class Twitter(Module):
    bs = None
    urlopener = None

    reTweetLink = re.compile("(https?\:\/\/)?twitter\.com\/[a-zA-Z0-9\-\_]+\/status\/\d+", re.IGNORECASE)  # NOQA

    def __init__(self, *args, **kwargs):
        Module.__init__(self, *args, **kwargs)
        try:
            from bs4 import BeautifulSoup
            self.bs = BeautifulSoup
        except ImportError:
            self.log.warn(
                "Unmet dependency BeautifulSoup4: The URL checkers will be "
                "disabled."
            )

    @bones.event.handler(event=bones.event.ChannelMessageEvent)
    def eventURLInfo_Twitter(self, event):
        if self.bs is not None:
            if "twitter" in event.message and "http" in event.message:
                data = self.reTweetLink.search(event.message)
                if data:
                    url = data.group(0)
                    html = event.client.factory.urlopener.open(url).read()
                    soup = self.bs(html)
                    tweet = soup \
                        .find("div", {"class": "permalink-inner permalink-tweet-container"}) \
                        .find("p", {"class": "tweet-text"}) \
                        .text
                    tweet = u"↵ ".join(tweet.split("\n"))
                    user = soup \
                        .find("div", {"class": "permalink-inner permalink-tweet-container"}) \
                        .find("span", {"class": "username js-action-profile-name"}) \
                        .text

                    # shitty fix for pic.twitter.com links
                    # could be improved by going through all links, check
                    # whether they start with http and if not replace the
                    # nodeText with the href attribute.
                    out = []
                    for word in tweet.split(" "):
                        if word.startswith("pic.twitter.com"):
                            word = "https://%s" % word
                        out.append(word)
                    tweet = " ".join(out)

                    msg = (u"\x0310Twitter\x03 \x0311::\x03 %s \x0311––\x03 %s"
                           % (tweet, user))
                    event.channel.msg(msg.encode("utf-8"))

class YouTube(Module):
    bs = None
    __fetchData = lambda x: {"template": "html", "title": "Something went wrong"}

    reVideoLink = re.compile("(https?\:\/\/)?(m\.|www\.)?(youtube\.com\/watch\?(.+)?v\=|youtu\.be\/)(?P<id>[a-zA-Z-0-9\_\-]*)")  # NOQA
    __template_simple = u"\x0314You\x035Tube \x0314::\x03 {title} \x034::\x03 http://youtu.be/{id}"  # NOQA
    __template_api = u"\x0314You\x035Tube \x034::\x03 {title}\x0314, {snippet[channelTitle]} \x034::\x0314 {duration} {definition} \x034::\x03 http://youtu.be/{id}"  # NOQA

    apiEndpoint = "https://www.googleapis.com/youtube/v3/%s?%s"

    def __init__(self, *args, **kwargs):
        Module.__init__(self, *args, **kwargs)
        self.fetchData = self.__fetchData

        self.template_simple = self.settings.get("module.utilities", "youtube.template.simple", default=self.__template_simple)
        self.template_api = self.settings.get("module.utilities", "youtube.template.api", default=self.__template_api)
        self.apikey = self.settings.get("module.utilities", "youtube.apikey",
                                        default=None)
        if not self.apikey:
            self.log.warn(
                "No API key provided. Video search and detailed video info "
                "will be disabled.")
            self.fetchData = self.fetchData_Html
        else:
            self.fetchData = self.fetchData_YouTubeApi

        if not self.apikey:
            try:
                from bs4 import BeautifulSoup
                self.bs = BeautifulSoup
            except ImportError:
                self.log.warn(
                    "Unmet dependency BeautifulSoup4: The URL checkers will "
                    "be disabled."
                )

    def api_request(self, method, **args):
        args["key"] = self.apikey
        url = self.apiEndpoint % (method, urllib.urlencode(args))
        result = self.factory.urlopener.open(url).read()
        return json.loads(result)

    def fetchData_Html(self, video):
        url = "http://youtube.com/watch?%s" % urllib.urlencode({"v": video})
        html = self.factory.urlopener.open(url).read()
        soup = self.bs(html)
        title = soup.find("span", {"id": "eow-title"}).text.strip()
        return {
            "template": "html",
            "id": video,
            "title": title,
        }

    def api_videoDetails(self, video):
        return self.api_request("videos",
                                part="statistics,snippet,contentDetails",
                                id=video)

    def api_videoSearch(self, term):
        data = self.api_request("search", part="id", safeSearch="none",
                                order="relevance", type="video",
                                maxResults="1", q=term)
        if not data or "items" not in data or len(data["items"]) < 1:
            return None
        return self.fetchData_YouTubeApi(data["items"][0]["id"]["videoId"])

    def fetchData_YouTubeApi(self, video):
        data = self.api_videoDetails(video)
        if not data["items"]:
            return
        output = data["items"][0]
        output.update(output["snippet"])
        output["duration"] = output["contentDetails"]["duration"].lower()[2:]
        output["definition"] = output["contentDetails"]["definition"].upper()
        output["template"] = "api"
        return output

    def sendToChannel(self, channel, data):
        if data["template"] == "api":
            output = self.template_api
        else:
            output = self.template_simple

        output = output.format(**data)
        output = u"↵ ".join(output.split("\n"))
        channel.msg(output.encode("utf-8"))

    @bones.event.handler(event=bones.event.ChannelMessageEvent)
    def checkMessageForUrl(self, event):
        if not self.bs and not self.apikey:
            return
        if not ("youtu" in event.message and "http" in event.message):
            return

        data = self.reVideoLink.search(event.message)
        if not data:
            return

        video_data = self.fetchData(data.group("id"))
        if not video_data:
            return
        self.sendToChannel(event.channel, video_data)

    @bones.event.handler(trigger="yt")
    @bones.event.handler(trigger="youtube")
    def videoSearch(self, event):
        if not self.apikey:
            return
        term = " ".join(event.args)
        video = self.api_videoSearch(term)
        if not video:
            event.reply("No such results.")
        self.sendToChannel(event.channel, video)


import boto3
import argparse
import json

region_name = 'ap-northeast-2'

def parse_args():
    parser = argparse.ArgumentParser(description='Boto3 Example')
    parser.add_argument('--access-key', type=str, help='AWS access key id')
    parser.add_argument('--secret-key', type=str, help='AWS secret access key')
    parser.add_argument('--queue_url', type=str, help='AWS SQS Endpoint')

    return parser.parse_args()

def receive_message(sqs, queue_url):
    response = sqs.receive_message(
        QueueUrl=queue_url,
        AttributeNames=['All'],
        MessageAttributeNames=['All'],
        MaxNumberOfMessages=1,
        WaitTimeSeconds=20
    )
    if 'Messages' in response:
        message = response['Messages'][0]
        message_body = json.loads(message['Body'])
        print('Received message: ', message_body)
        # SQS 메시지 삭제
        sqs.delete_message(QueueUrl=queue_url, ReceiptHandle=message['ReceiptHandle'])

def main():
    args = parse_args()

    session = boto3.Session(
        aws_access_key_id=args.access_key,
        aws_secret_access_key=args.secret_key
    )

    # boto3 클라이언트 생성
    sqs = session.client('sqs', region_name=region_name)

    while True:
        receive_message(sqs, queue_url=args.queue_url)

if __name__ == '__main__':
    main()


from datetime import datetime
import logging

logger = logging.getLogger(__name__)

class TimePeriod:
    def __init__(self, datetimes: list):
        self._assess_period_length(datetimes)
        self.period_length = self._define_period_length()

    def _assess_period_length(self, datetime_strings: list):
        timestamps = []

        for datetime_string in datetime_strings:
            try:
                datetime_object = datetime.strptime(
                    datetime_string, "%Y-%m-%d %H:%M:%S")
            except ValueError as e:
                datetime_object = datetime.strptime(
                    datetime_string, "%Y-%m-%dT%H:%M:%SZ")

            timestamp = datetime.timestamp(datetime_object)

            if timestamp > 0:
                timestamps.append(timestamp)

        timestamps.sort()

        self.time_delta = datetime.fromtimestamp(timestamps[-1]) \
                - datetime.fromtimestamp(timestamps[0])
        self.first_datetime = datetime.fromtimestamp(timestamps[0])
        self.last_datetime = datetime.fromtimestamp(timestamps[-1])

    def _define_period_length(self) -> str:
        days_delta = self.time_delta.days
        period_string = ""

        if days_delta ==1:
            period_string = "a day"
        elif days_delta == 2 and self._is_weekend():
            period_string = "a weekend"
        elif days_delta == 2 and not self._is_weekend():
            period_string = "two days"
        elif 3 <= days_delta <= 6:
            period_string = "a few days"
        elif 7 <= days_delta <= 10:
            period_string = "a week"
        elif 11 <= days_delta <= 19:
            period_string = "two weeks"
        elif 20 <= days_delta <= 30:
            period_string = self.get_period_month()
        elif 80  <= days_delta <= 90:
            period_string = "a quarter"
        elif 150  <= days_delta <= 190:
            period_string = "a semester"
        elif 300  <= days_delta <= 400:
            period_string = "a year"

        return period_string

    def _is_weekend(self) -> bool:
        is_weekend = False
        days_delta = self.time_delta.days
        first_day = selffirst_datetime.strftime('%A')
        second_day = self.last_datetime.strftime('%A')

        if days_delta == 2 and \
                (first_day == "Saturday" and second_day == "Sunday"):
            is_weekend = True

        return is_weekend

    def get_period_month(self) -> str:
        months = []

        if slef.period_length == "month":
            month = self.first_datetime.strftime("%B")

        return month


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

def input_tree():
	rootdata=int(input())
	if rootdata==-1:
		return None
	root=TreeNode(rootdata)
	# recursive to take input of left tree
	leftTree=input_tree()
	# recursive to take input of right tree
	rightTree=input_tree()
	root.left=leftTree
	root.right=rightTree
	return root

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


import sys
import webaas_api
import pytermgui as ptg

class ChatRoomApp:
    def __init__(self, show_person, show_message, body_window):
        self.show_person_func = show_person
        self.show_message_func = show_message
        self.body_window = body_window
        self.chatroom_info = webaas_api.ChatRoomInfo(
            self.show_person_func, self.show_message_func)

    def process_join(self, join_command):
        username = join_command[0]
        chatroom_id = int(join_command[1])

        if self.in_chatroom():
            # Logout Current ChatRoom
            self.chatroom_info.logout()
            # Login Another ChatRoom
            self.chatroom_info.set_chatroom_id(chatroom_id)
            self.chatroom_info.login(username)
            corner = ["", "", "", ""]
            corner[ptg.VerticalAlignment.TOP] = "[bold #FCBA03]ChatRoom {}".format(chatroom_id)
            self.body_window.set_char("corner", corner)
        else:
            # Login ChatRoom
            self.chatroom_info.set_chatroom_id(chatroom_id)
            self.chatroom_info.login(username)
            corner = ["", "", "", ""]
            corner[ptg.VerticalAlignment.TOP] = "[bold #FCBA03]ChatRoom {}".format(chatroom_id)
            self.body_window.set_char("corner", corner)

    def process_show(self, show_command):
        chatroom_id = int(show_command[0])
        show_property = show_command[1]
        if show_property == 'People':
            people_list = []
            chatroom = webaas_api.ChatRoomInfo.get_chatroom(chatroom_id)
            if chatroom == None:
                people_list.append("There is No ChatRoom ID " + str(chatroom_id))
                self.show_person_func(people_list)
                return
            people_list = webaas_api.ChatRoomInfo.get_people_list(chatroom)
            if len(people_list) == 0:
                people_list.append("There is No People in ChatRoom " + str(chatroom_id))
            self.show_person_func(people_list)
        elif show_property == 'Message':
            message_list = []
            chatroom = webaas_api.ChatRoomInfo.get_chatroom(chatroom_id)
            if chatroom == None:
                message_list.append("There is No ChatRoom ID " + str(chatroom_id))
                self.show_message_func(message_list)
                return
            message_list = webaas_api.ChatRoomInfo.get_message_list(chatroom)
            if len(message_list) == 0:
                message_list.append("There is No Message in ChatRoom " + str(chatroom_id))
            self.show_message_func(message_list)
        elif show_property == 'ChatRoom':
            chatroom_print_list = []
            chatroom_id_list = webaas_api.get_used_chatroom_id()
            if len(chatroom_id_list) == 0:
                chatroom_print_list.append("There is No ChatRoom")
            else:
                for item in chatroom_id_list:
                    chatroom_print_list.append("ChatRoom ID: " + str(item))
            self.show_message_func(chatroom_print_list)

    def process_leave(self):
        if self.in_chatroom():
            # log out
            self.chatroom_info.logout()
            corner = ["", "", "", ""]
            self.body_window.set_char("corner", corner)
            self.show_message_func([])
            self.show_person_func([])
        else:
            self.show_message_func(["You Are Not Logged In!"])

    def process_message(self, message):
        if self.in_chatroom():
            self.chatroom_info.send_user_msg(message)
        else:
            self.show_message_func(["You Are Not in ChatRoom Now!"])
            return

    def process_help(self):
        message_list = []
        message_list.append("/help")
        message_list.append("    Command Help")
        message_list.append("/join")
        message_list.append("    /join \[username] \[chatroom_id]")
        message_list.append("/create")
        message_list.append("/show")
        message_list.append("    /show \[chatroom_id] \[show_property]")
        message_list.append("    \[show_property]")
        message_list.append("        People")
        message_list.append("        Message")
        message_list.append("        ChatRoom")
        self.show_message_func(message_list)

    def process_create(self):
        webaas_api.create_chatroom()

    def process_command(self, command_line):
        command = str.split(command_line, ' ')
        root_command = command[0][1:]
        command_len = len(command)
        if root_command == 'join' and command_len == 3:
            return self.process_join(command[1:])
        elif root_command == 'show' and command_len == 3:
            return self.process_show(command[1:])
        elif root_command == 'help':
            return self.process_help()
        elif root_command == 'leave':
            self.process_leave()
        elif root_command == 'create':
            self.process_create()

    def in_chatroom(self):
        return self.chatroom_info.is_in_chatroom()

    def init_app(self):
        webaas_api.test_endpoint()
        webaas_api.register()
        webaas_api.create_schema()

    def release(self):
        if self.in_chatroom() == False:
            webaas_api.unregister()
            return

        self.chatroom_info.logout()
        webaas_api.unregister()


from google.cloud.functions.v1beta2 import cloud_functions_service_client
from google.cloud.proto.functions.v1beta2 import functions_pb2
from google.gax.errors import GaxError
from grpc import StatusCode

# point this at your project ID
PROJECT_ID = 'gapic-test'

LOCATION_ID = 'us-central1'
FUNCTION_ID = 'helloWorld'

# upload helloWorld.zip from this directory to Google Storage for your project
SOURCE_URI = 'gs://gapic-functions-v1beta2/helloWorld.zip'

api = cloud_functions_service_client.CloudFunctionsServiceClient()
location = api.location_path(PROJECT_ID, LOCATION_ID)
function_name = api.function_path(PROJECT_ID, LOCATION_ID, FUNCTION_ID)

def on_delete(operation_future):
    try:
        api.get_function(function_name)
    except GaxError as e:
        code = getattr(e.cause, "code", None)
        if callable(code) and code() == StatusCode.NOT_FOUND:
            print('Expect error here since the function should have been deleted')
        else:
            raise

def on_update(operation_future):
    result = operation_future.result()
    print('Function updated: \n%s\n' % result)

    fetched_function = api.get_function(result.name)
    print('Function fetched: \n%s\n' % fetched_function)

    data = '{"message":"Hello World!"}'
    call_response = api.call_function(fetched_function.name, data)
    print('Function call response: \n%s\n' % call_response)

    print('List functions:\n')
    for function in api.list_functions(location):
        print(function)

    print('Delete function:\n')
    delete_response = api.delete_function(function_name)
    delete_response.add_done_callback(on_delete)
    print('Metadata: \n%s\n' % delete_response.metadata())

def on_create(operation_future):
    result = operation_future.result()
    print('Function created: \n%s\n' % result)

    updated_function = functions_pb2.CloudFunction(
        name=result.name,
        source_archive_url=result.source_archive_url,
        pubsub_trigger=('projects/%s/topics/hello_world2' % PROJECT_ID))

    response = api.update_function(result.name, updated_function)
    response.add_done_callback(on_update)
    print('Metadata: \n%s\n' % response.metadata())

def on_init(_):
    function = functions_pb2.CloudFunction(
        name=function_name,
        source_archive_url=SOURCE_URI,
        pubsub_trigger=('projects/%s/topics/hello_world' % PROJECT_ID))
    response = api.create_function(location, function)
    response.add_done_callback(on_create)
    print('Metadata: \n%s\n' % response.metadata())

try:
    response = api.delete_function(function_name)
    response.add_done_callback(on_init)
except GaxError as e:
    code = getattr(e.cause, "code", None)
    if callable(code) and code() == StatusCode.NOT_FOUND:
        on_init(None)
    else:
        raise


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow import keras
import tensorflow as tf
import numpy as np
from ops import *
from resnet import resnet

# The later one could be function and class type.

import argparse

"""parsing and configuration"""''
'''Use arguments to set up parameter accordingly'''

# def parse_args():
def parse_flags():

    desc="TensorFlow implementation of Resnet on Fashion_mnist! author: Hazard Wen"
    parser=argparse.ArgumentParser(description=desc)
    # How to confirm whether this desc works well or not.

    # all the outside arguments can be passed successfully.
    # Bool data type can not be input by keyboard.

    '''

	In Deep_learning_prac2, we define one function to convert keyboard input into boolean.

	def str2bool(v):
		if isinstance(v, bool):
			return 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.')

	'''

    '''

	check if is newest!!!

	'''
    # add dropout or not. Dropout, probability to keep units
    parser.add_argument('--dropout_keep_prob', type=float,
        help='Keep probability of dropout for the fully connected layer(s).', default=0.8)
    parser.add_argument('--learning_rate', type=float, help='learning rate', default=0.1)
    parser.add_argument('--max_epochs', type=int, help='Number of epochs to run.', default=100)
    parser.add_argument('--batch_size', type=int,
                        help='Number of images to process in a batch.', default=64)
    parser.add_argument('--activation_function', type=str, choices=['elu', 'relu', 'relu6', 'leaky_relu', 'sigmoid', 'tanh', 'linear'],
                        help='which activation function we are going to use', default='None')
    # add argument for data_augmentation with bool data type
    parser.add_argument('--data_augmentation', type=str, choices=['True','False'], help='excute data augmentation or not', default='False')
    #******************************************************************************************************************#
    # Be careful on spelling when you type flag outside, otherwise it will occur one error.
    # error: unrecognized arguments: --data_augmention True
    # add arguments for different optimizer
    parser.add_argument('--optimizer', type=str, choices=['ADAGRAD', 'ADADELTA', 'ADAM', 'RMSPROP', 'MOM'],
        help='The optimization algorithm to use', default='None')
    # add argument for batch_normalization or not with bool data type
    parser.add_argument('--is_batch_normalization',type=str,choices=['True','False'], help='excute batch_normalization or not',default='False')

    # The following one without check_args is also correct.
    # return parser.parse_args()
    return check_args(parser.parse_args())

def check_args(args):

    return args

'''
main function
'''

def main():

    print('The version of tensorflow:', tf.__version__)
    print('The version of keras:', keras.__version__)

    # specific parse arguments
    #args=parse_args()
    #args=parser.parse_args()
    args=parse_flags()
    if args is None:
        exit()

    print('\n The type of args is:',type(args))
    print('\n This is the existing arguments:', args)
    print('\n ')

    # access the Fashion MNIST directly using TensorFlow method
    from tensorflow.examples.tutorials.mnist import input_data
    mnist = input_data.read_data_sets('data/fashion', one_hot=True)

    # Creating placeholders
    # Explore the format of the dataset before training the model.
    # The following shows there are 60,000 images in the training set,
    # with each image represented as 28 x 28 (784) pixels
    # train_images.shape = (60000, 28, 28)
    # Likewise, there are 60,000 labels in the training set
    # len(train_labels) = 60000
    x = tf.placeholder(tf.float32, shape=[None, 784])
    #
    print('\n The shape of x placeholder is', tf.shape(x))
    array_1 = np.array([1,2])
    print('\n The shape of array_1 is', tf.shape(array_1))
    print('\n like x=np.array([1,2]), Here is tensor-like [None,784]')
    # They seems that they are different.
    y_true = tf.placeholder(tf.float32, shape=[None, 10])

    # If one component of shape is the special value -1, the size of that dimension is computed so that the total size remains constant.
    # In particular, a shape of [-1] flattens into 1-D. At most one component of shape can be -1.
    x_image = tf.reshape(x, [-1, 28, 28, 1])
    x_image = tf.identity(x_image,'input')
    print('\n The shape of x_image is', tf.shape(x_image ))
    print('\n like x=np.array([none,1,2,3]), Here is tensor-like [None,28,28,1]')
    # Create the ResNet model
    '''x=x_image means that [none,28,28,1]'''
    # The instance name should be model.
    # The class name shoulde be Resnet, uppercase should be better.
    model = resnet(args, x=x_image, n=20, num_classes=10) # we are going to use 20 layers.
    score = model.out

    # using pre-defined function to get training loss and training accuracy
    training_loss, accuracy=classification_loss(score,y_true)

    # define reg loss
    # reg_loss = tf.losses.get_regularization_loss()
    # training_loss += reg_loss

    # different Optimizer
    def optimizer_choose(optimizer,learning_rate):

        if optimizer == 'ADAGRAD':
            opt = tf.train.AdagradOptimizer(learning_rate)
            return opt
        elif optimizer == 'ADADELTA':
            opt = tf.train.AdadeltaOptimizer(learning_rate, rho=0.9, epsilon=1e-6)
            return opt
        elif optimizer == 'ADAM':
            #opt = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=0.1)
            opt = tf.train.AdamOptimizer(learning_rate)
            return opt
        elif optimizer == 'RMSPROP':
            opt = tf.train.RMSPropOptimizer(learning_rate, decay=0.9, momentum=0.9, epsilon=1.0)
            return opt
        elif optimizer == 'MOM':
            opt = tf.train.MomentumOptimizer(learning_rate, 0.9, use_nesterov=True)
            return opt
        else:
            raise ValueError('Invalid optimization algorithm')

    choosed_optimizer_opt= optimizer_choose(args.optimizer,args.learning_rate)

    train = choosed_optimizer_opt.minimize(training_loss)

    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)

        for j in range(args.max_epochs):
            for i in range(0, 60000, args.batch_size):
                # mnist.train.next_batch returns a tuple of two arrays
                batch_x, batch_y = mnist.train.next_batch(args.batch_size)
                #print('The tf.shape of batch_x is', tf.shape(batch_x))
                #print('The np.shape of batch_x is', np.shape(batch_x))
                #print('The type of batch_x is', type(batch_x))
                '''batch_x looks like ([64,784])'''
                batch_x=data_augmentation(batch_x, 28, is_data_augmentation=args.data_augmentation)
                # perform minimize loss per batch
                sess.run(train, feed_dict={x: batch_x, y_true: batch_y})
            # Try to add training loss
            epoch_train_loss=sess.run(training_loss,feed_dict={x: batch_x, y_true: batch_y})
            # Try to add training accuracy here per epoch
            training_accuracy=sess.run(accuracy,feed_dict={x: batch_x, y_true: batch_y})

            # change test accuracy using sess.run instead of eval
            #test_accuracy = accuracy.eval(feed_dict={x: mnist.test.images[: 5000],
            #                                         y_true: mnist.test.labels[: 5000]})
            testing_accuracy = sess.run(accuracy, feed_dict={x: mnist.test.images[: 5000],
                                                             y_true: mnist.test.labels[: 5000]})
            print('After %d epochs, testing_accuracy: %g, training_accuracy: %g, epoch_train_loss: %g, %d epochs' % (j + 1, testing_accuracy, training_accuracy,epoch_train_loss,j+1))

if __name__=='__main__':
    main()


import json
from pathlib import Path

import requests
from loguru import logger
from picsellia import Client

import config

if __name__ == "__main__":
    # Connect to Picsellia.
    client = Client(
    api_token=config.API_TOKEN,
    organization_name=config.ORGANIZATION_NAME
    )

    # Get deployment by name.
    my_deployment = client.get_deployment(name=config.DEPLOYMENT_NAME)

    # Get your deployment ID.
    deployment_id = str(my_deployment.id)

    # Get your API token from your profile.
    api_token = str(config.API_TOKEN)

    # Authentication url
    auth_url = "https://serving.picsellia.com/api/login"

    headers = {
        "Authorization": "Token " + api_token,
    }

    jwt_generation_data = {
        "deployment_id": deployment_id,
        "api_token": api_token
    }

    jwt_request = requests.post(
        auth_url,
        headers=headers,
        data=json.dumps(jwt_generation_data)
    )

    # Retrieving the JWT.
    jwt = jwt_request.json()["jwt"]

    # Serving API endpoint
    url = "https://serving.picsellia.com/api/deployment/{}/predict".format(my_deployment.id)

    header = {
        "Authorization": "Bearer " + jwt,
    }

    # Metadata that helps extrat data from datalake.
    data = {
        "source": "camera1",
        "tag": "Cartons"
    }

    images_folder = config.INFERENCE_DATA_DIR

    images_path = Path(images_folder).glob('*.jpg')

    for image_path in images_path:
        with open(image_path, "rb") as file:
            r = requests.post(
                url=url,
                files={'media': file},
                headers=header,
                data=data
            )

    logger.info(r.text)

    prediction = r.json()


from pm4py import util as pmutil
from pm4py.evaluation.replay_fitness.versions import alignment_based, token_replay
from pm4py.objects.conversion.log import factory as log_conversion
from pm4py.objects.log.util import general as log_util
from pm4py.objects.log.util import xes as xes_util

ALIGNMENT_BASED = "alignments"
TOKEN_BASED = "token_replay"
VERSIONS = {ALIGNMENT_BASED: alignment_based.apply, TOKEN_BASED: token_replay.apply}
VERSIONS_EVALUATION = {ALIGNMENT_BASED: alignment_based.evaluate, TOKEN_BASED: token_replay.evaluate}

PARAM_ACTIVITY_KEY = 'activity_key'

def apply(log, petri_net, initial_marking, final_marking, parameters=None, variant="token_replay"):
    """
    Apply fitness evaluation starting from an event log and a marked Petri net,
    by using one of the replay techniques provided by PM4Py

    Parameters
    -----------
    log
        Trace log object
    petri_net
        Petri net
    initial_marking
        Initial marking
    final_marking
        Final marking
    parameters
        Parameters related to the replay algorithm
    variant
        Chosen variant (alignments or token-based replay)

    Returns
    ----------
    fitness_eval
        Fitness evaluation
    """
    if parameters is None:
        parameters = {}
    if pmutil.constants.PARAMETER_CONSTANT_ACTIVITY_KEY not in parameters:
        parameters[pmutil.constants.PARAMETER_CONSTANT_ACTIVITY_KEY] = xes_util.DEFAULT_NAME_KEY
    if pmutil.constants.PARAMETER_CONSTANT_TIMESTAMP_KEY not in parameters:
        parameters[pmutil.constants.PARAMETER_CONSTANT_TIMESTAMP_KEY] = xes_util.DEFAULT_TIMESTAMP_KEY
    if pmutil.constants.PARAMETER_CONSTANT_CASEID_KEY not in parameters:
        parameters[pmutil.constants.PARAMETER_CONSTANT_CASEID_KEY] = log_util.CASE_ATTRIBUTE_GLUE

    return VERSIONS[variant](log_conversion.apply(log, parameters, log_conversion.TO_EVENT_LOG), petri_net,
                             initial_marking, final_marking, parameters=parameters)

def evaluate(results, parameters="None", variant="token_replay"):
    """
    Evaluate replay results when the replay algorithm has already been applied

    Parameters
    -----------
    results
        Results of the replay algorithm
    parameters
        Possible parameters passed to the evaluation
    variant
        Indicates which evaluator is called

    Returns
    -----------
    fitness_eval
        Fitness evaluation
    """
    return VERSIONS_EVALUATION[variant](results, parameters=parameters)


from statistics import geometric_mean
from ruamel.yaml import YAML, dump, RoundTripDumper
from raisimGymTorch.env.bin import rsg_a1_task
from raisimGymTorch.env.RaisimGymVecEnv import RaisimGymVecEnv as VecEnv
from raisimGymTorch.helper.raisim_gym_helper import ConfigurationSaver
import os
import math
import time
import raisimGymTorch.algo.ppo.module as ppo_module
import raisimGymTorch.algo.ppo.ppo as PPO
import torch.nn as nn
import numpy as np
import torch
import datetime
import argparse
try:
    import wandb
except:
    wandb = None

parser = argparse.ArgumentParser()
parser.add_argument("--exptid", type = int, help='experiment id to prepend to the run')
parser.add_argument("--overwrite", action = 'store_true')
parser.add_argument("--debug", action = 'store_true')
parser.add_argument("--loadid", type = int, default = None)
parser.add_argument("--gpu", type = int, default = 1)
parser.add_argument("--name", type = str)
args = parser.parse_args()

# directories
task_path = os.path.dirname(os.path.realpath(__file__))
home_path = task_path + "/../../../../.."

# config
cfg = YAML().load(open(task_path + "/cfg.yaml", 'r'))

rng_seed = cfg['seed']
torch.manual_seed(rng_seed)
np.random.seed(rng_seed)

activation_fn_map = {'none': None, 'tanh': nn.Tanh}
output_activation_fn = activation_fn_map[cfg['architecture']['activation']]
small_init_flag = cfg['architecture']['small_init']

if args.debug:
    cfg['environment']['num_envs'] = 1
    cfg['environment']['num_threads'] = 1
    device_type = 'cpu'
else:
    device_type = 'cuda:{}'.format(args.gpu)

cfg['environment']['test'] = False
cfg['environment']['speedTest'] = False

baseDim = cfg['environment']['baseDim']
priv_info = cfg['environment']['privinfo']
use_fourier = 'fourier' in cfg['environment'] and cfg['environment']['fourier']
geomDim = int(cfg['environment']['geomDim'])*int(cfg['environment']['use_slope_dots'])
n_futures = int(cfg['environment']['n_futures'])

# create environment from the configuration file
env = VecEnv(rsg_a1_task.RaisimGymEnv(home_path + "/rsc", dump(cfg['environment'], Dumper=RoundTripDumper)), cfg['environment'])

# shortcuts
ob_dim = env.num_obs
act_dim = env.num_acts

if use_fourier:
    privy_dim = ob_dim - baseDim
    encoder_dim = cfg['environment']['encoder_dim']
    regular_fourier_dim = cfg['environment']['regular_fourier_dim']
    privy_fourier_dim = cfg['environment']['privy_fourier_dim']
    fourier_scale = cfg['environment']['fourier_scale']
    fourier_trainable = cfg['environment']['fourier_trainable']

    fourier_policy = cfg['environment']['fourier_policy']
    fourier_value = cfg['environment']['fourier_value']

# save the configuration and other files
saver = ConfigurationSaver(log_dir=home_path + "/raisimGymTorch/data/rsg_a1_task/" + '{:04d}'.format(args.exptid),
                           save_items=[task_path + "/Environment.hpp", task_path + "/runner.py"], config = cfg, overwrite = args.overwrite)
if wandb:
    wandb.init(project='command_loco', config=dict(cfg), name=args.name)
    wandb.save(home_path + '/raisimGymTorch/env/envs/rsg_a1_task/Environment.hpp')

# Training
n_steps = math.floor(cfg['environment']['max_time'] / cfg['environment']['control_dt'])
total_steps = n_steps * env.num_envs

if cfg['environment']['unnormalize_speed_vec']:
    raise NotImplementedError()

speed_vec_start_idx = cfg['architecture']['speed_vec_start_idx']
speed_vec_end_idx = cfg['architecture']['speed_vec_end_idx']
layer_type = cfg['architecture']['layer_type']
freeze_encoder = cfg['architecture']['freeze_encoder']

avg_rewards = []

if use_fourier:
    raise Exception('not implemented')
else:
    if priv_info:
        if layer_type == 'feedforward':
            init_var = 0.3
            module_type = ppo_module.MLPEncode_wrap
            actor = ppo_module.Actor(module_type(cfg['architecture']['policy_net'],
                                     nn.LeakyReLU,
                                     ob_dim//2,
                                     act_dim,
                                     output_activation_fn,
                                     small_init_flag,
                                     base_obdim = baseDim,
                                     geom_dim = geomDim,
                                     n_futures = n_futures),
                                     ppo_module.MultivariateGaussianDiagonalCovariance(act_dim, init_var),
                                     device_type)

            critic = ppo_module.Critic(module_type(cfg['architecture']['value_net'],
                                                    nn.LeakyReLU,
                                                    ob_dim//2,
                                                    1,
                                                    base_obdim = baseDim,
                                                    geom_dim = geomDim,
                                                    n_futures = n_futures),
                                    device_type)
        else:
            raise NotImplementedError()

    else:
        raise NotImplementedError()

# Steps + flat policy
flat_policy_load_path = os.path.join(task_path,"../../../../data/base_policy/policy_22000.pt")
env.load_scaling(os.path.join(task_path, "../../../../data/base_policy"),
                 22000, policy_type=0, num_g1=n_futures)
loaded_graph_flat = torch.jit.load(flat_policy_load_path, map_location=torch.device(device_type))
flat_expert = ppo_module.Steps_Expert(loaded_graph_flat, device=device_type, baseDim=42,
                                      geomDim=2, n_futures=1, num_g1=n_futures)
# Encoders loading from blind stairs policy
checkpoint = torch.load(os.path.join(task_path,"../../../../data/base_policy/full_22000.pt"))
blind_policy_state_dict = checkpoint['actor_architecture_state_dict']
own_state = actor.architecture.state_dict()
for name, param in blind_policy_state_dict.items():
    own_state[name].copy_(param)
env.load_scaling(os.path.join(task_path, "../../../../data/base_policy"),
                 22000, policy_type=2, num_g1=n_futures)

ppo = PPO.PPO(actor=actor,
              critic=critic,
              num_envs=cfg['environment']['num_envs'],
              num_transitions_per_env=n_steps,
              num_learning_epochs=4,
              gamma=0.997,
              lam=0.95,
              num_mini_batches=4,
              device=device_type,
              log_dir=saver.data_dir,
              mini_batch_sampling='in_order',
              learning_rate=5e-4,
              flat_expert=flat_expert
              )

if wandb:
    wandb.watch(actor.architecture.architecture, log_freq=100)
    wandb.watch(critic.architecture.architecture, log_freq=100)

penalty_scale = np.array([cfg['environment']['lateralVelRewardCoeff'], cfg['environment']['angularVelRewardCoeff'], cfg['environment']['deltaTorqueRewardCoeff'], cfg['environment']['actionRewardCoeff'], cfg['environment']['sidewaysRewardCoeff'], cfg['environment']['jointSpeedRewardCoeff'], cfg['environment']['deltaContactRewardCoeff'], cfg['environment']['deltaReleaseRewardCoeff'], cfg['environment']['footSlipRewardCoeff'], cfg['environment']['upwardRewardCoeff'], cfg['environment']['workRewardCoeff'], cfg['environment']['yAccRewardCoeff'], 1., 1., 1.])

if args.loadid is not None:
    checkpoint = torch.load(saver.data_dir+"/full_"+str(args.loadid)+'.pt')
    actor.architecture.load_state_dict(checkpoint['actor_architecture_state_dict'])
    actor.distribution.load_state_dict(checkpoint['actor_distribution_state_dict'])
    critic.architecture.load_state_dict(checkpoint['critic_architecture_state_dict'])
    try:
        ppo.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    except:
        print("Not loading ppo state")
    env.load_scaling(saver.data_dir, args.loadid, policy_type=1)

if freeze_encoder:
    # do not update some networks
    print("Freezing the encoders!")
    for net_i in [actor.architecture.architecture.geom_encoder,
                  actor.architecture.architecture.prop_encoder]:
        for param in net_i.parameters():
            param.requires_grad = False

if args.loadid is not None:
    env.set_itr_number(args.loadid)

# This coefficient controls how much the policy is optimized with RL. Change to 1 for taking away demonstrations from a previous policy.
rl_coeff = 0.3
ppo.update_rl_coeff(rl_coeff)

for update in range(500001) if args.loadid is None else range(args.loadid + 1, 500001):
    start = time.time()
    env.reset()
    reward_ll_sum = 0
    forwardX_sum = 0
    penalty_sum = 0
    done_sum = 0
    average_dones = 0.

    if update %  cfg['environment']['eval_every_n'] == 0:
        print("Visualizing and evaluating the current policy")
        actor.save_deterministic_graph(saver.data_dir+"/policy_"+str(update)+'.pt', torch.rand(1, ob_dim).cpu())
        if update %  (1 * cfg['environment']['eval_every_n']) == 0:
            torch.save({
                'actor_architecture_state_dict': actor.architecture.state_dict(),
                'actor_distribution_state_dict': actor.distribution.state_dict(),
                'critic_architecture_state_dict': critic.architecture.state_dict(),
                'optimizer_state_dict': ppo.optimizer.state_dict(),
            }, saver.data_dir+"/full_"+str(update)+'.pt')

        parameters = np.zeros([0], dtype=np.float32)
        for param in actor.deterministic_parameters():
            parameters = np.concatenate([parameters, param.cpu().detach().numpy().flatten()], axis=0)
        np.savetxt(saver.data_dir+"/policy_"+str(update)+'.txt', parameters)
        loaded_graph = torch.jit.load(saver.data_dir+"/policy_"+str(update)+'.pt')

        env.reset()
        env.save_scaling(saver.data_dir, str(update))

    # actual training
    for step in range(n_steps):
        obs = env.observe(not freeze_encoder)
        action = ppo.observe(obs)
        reward, dones = env.step(action)
        unscaled_reward_info = env.get_reward_info()
        forwardX = unscaled_reward_info[:, 0]
        penalty = unscaled_reward_info[:, 1:]
        ppo.step(value_obs=obs, rews=reward, dones=dones, infos=[])
        done_sum = done_sum + sum(dones)
        reward_ll_sum = reward_ll_sum + sum(reward)
        forwardX_sum += np.sum(forwardX)
        penalty_sum += np.sum(penalty, axis=0)

    env.curriculum_callback()

    # take st step to get value obs
    obs = env.observe(not freeze_encoder)
    ppo.update(actor_obs=obs,
               value_obs=obs,
               log_this_iteration=update % 10 == 0,
               update=update)

    end = time.time()

    forwardX = forwardX_sum / total_steps
    forwardXReward = forwardX_sum * cfg['environment']['forwardVelRewardCoeff'] / total_steps

    forwardY, forwardZ, deltaTorque, action, sideways, jointSpeed, deltaContact, deltaRelease, footSlip, upward, work, yAcc, torqueSquare, stepHeight, walkedDist = penalty_sum / total_steps
    forwardYReward, forwardZReward, deltaTorqueReward, actionReward, sidewaysReward, jointSpeedReward, deltaContactReward, deltaReleaseReward, footSlipReward, upwardReward, workReward, yAccReward, torq, stepHeight, walkedDist = scaled_penalty = penalty_sum * penalty_scale / total_steps

    average_ll_performance = reward_ll_sum / total_steps
    average_dones = done_sum / total_steps
    avg_rewards.append(average_ll_performance)

    actor.distribution.enforce_minimum_std((torch.ones(12)*0.2).to(device_type))
    if wandb:
        wandb.log({'forwardX': forwardX,
        'forwardX_reward': forwardXReward,
        'forwardY': forwardY,
        'forwardY_reward': forwardYReward,
        'forwardZ': forwardZ,
        'forwardZ_reward': forwardZReward,
        'deltaTorque': deltaTorque,
        'deltaTorque_reward': deltaTorqueReward,
        'action': action,
        'stepHeight': stepHeight,
        'action_reward': actionReward,
        'sideways': sideways,
        'sideways_reward': sidewaysReward,
        'jointSpeed': jointSpeed,
        'jointSpeed_reward': jointSpeedReward,
        'deltaContact': deltaContact,
        'deltaContact_reward': deltaContactReward,
        'deltaRelease': deltaRelease,
        'deltaRelease_reward': deltaReleaseReward,
        'footSlip': footSlip,
        'footSlip_reward': footSlipReward,
        'upward': upward,
        'upward_reward': upwardReward,
        'work': work,
        'work_reward': workReward,
        'yAcc': yAcc,
        'yAcc_reward': yAccReward,
        'torqueSquare': torqueSquare,
        'dones': average_dones,
        'walkedDist': walkedDist})

    print('----------------------------------------------------')
    print('{:>6}th iteration'.format(update))
    print('{:<40} {:>6}'.format("average ll reward: ", '{:0.10f}'.format(average_ll_performance)))
    print('{:<40} {:>6}'.format("average forward reward: ", '{:0.10f}'.format(forwardXReward)))
    print('{:<40} {:>6}'.format("average penalty reward: ", ', '.join(['{:0.4f}'.format(r) for r in scaled_penalty])))
    print('{:<40} {:>6}'.format("average walked dist: ", '{:0.10f}'.format(scaled_penalty[-1])))
    print('{:<40} {:>6}'.format("dones: ", '{:0.6f}'.format(average_dones)))
    print('{:<40} {:>6}'.format("lr: ", '{:.4e}'.format(ppo.optimizer.param_groups[0]["lr"])))
    print('{:<40} {:>6}'.format("time elapsed in this iteration: ", '{:6.4f}'.format(end - start)))
    print('{:<40} {:>6}'.format("fps: ", '{:6.0f}'.format(total_steps / (end - start))))
    print('std: ')
    print(np.exp(actor.distribution.std.cpu().detach().numpy()))
    print('----------------------------------------------------\n')


#%%
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Date, Integer
from sqlalchemy.orm import sessionmaker
from tqdm import tqdm
import csv
from datetime import datetime as dt

#%%
engine = create_engine('postgresql+psycopg2://jon:postgres@localhost/headlines')
conn = engine.connect()
Base = declarative_base()

#%%

year = '2008'

class Headline(Base):
    __tablename__ = f'headlines_{year}'

    id = Column(Integer, primary_key = True)
    date = Column(Date)
    headline = Column(String(255))

Session = sessionmaker(bind=engine)
session = Session()
Base.metadata.create_all(conn)

#%%
filepath = f'resources/reuters-newswire-{year}.v5.csv'

num_lines = sum(1 for line in open(filepath,'r'))

with open(filepath, newline = '', encoding='iso_8859_1') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    id_ = 1
    i = 1

    headers = next(reader, None)
    for row in tqdm(reader, total = num_lines):
        if i % 100 == 0:
            i = 0
            session.commit()

        session.add(Headline
            (
                id = id_,
                date = dt.strptime(row[0], '%Y%m%d%H%M'),
                headline = row[1])
            )
        i += 1
        id_ += 1

#%%


import pytest

from xdsl.ir import MLContext, Operation
from xdsl.parser import XDSLParser
from xdsl.dialects.builtin import Builtin
from xdsl.dialects.func import Func
from xdsl.dialects.arith import Arith
from xdsl.dialects.cf import Cf
from xdsl.rewriting.composable_rewriting.immutable_ir.immutable_ir import get_immutable_copy

program_region = \
"""builtin.module() {
  %0 : !i32 = arith.constant() ["value" = 1 : !i32]
}
"""
program_region_2 = \
"""builtin.module() {
  %0 : !i32 = arith.constant() ["value" = 2 : !i32]
}
"""
program_region_2_diff_name = \
"""builtin.module() {
  %cst : !i32 = arith.constant() ["value" = 2 : !i32]
}
"""
program_region_2_diff_type = \
"""builtin.module() {
  %0 : !i64 = arith.constant() ["value" = 2 : !i64]
}
"""
program_add = \
"""builtin.module() {
%0 : !i32 = arith.constant() ["value" = 1 : !i32]
%1 : !i32 = arith.constant() ["value" = 2 : !i32]
%2 : !i32 = arith.addi(%0 : !i32, %1 : !i32)
}
"""
program_add_2 = \
"""builtin.module() {
%0 : !i32 = arith.constant() ["value" = 1 : !i32]
%1 : !i32 = arith.constant() ["value" = 2 : !i32]
%2 : !i32 = arith.addi(%1 : !i32, %0 : !i32)
}
"""
program_func = \
"""builtin.module() {
  func.func() ["sym_name" = "test", "type" = !fun<[!i32, !i32], [!i32]>, "sym_visibility" = "private"] {
  ^0(%0 : !i32, %1 : !i32):
    %2 : !i32 = arith.addi(%0 : !i32, %1 : !i32)
    func.return(%2 : !i32)
  }
}
"""
program_successors = \
"""builtin.module() {
    func.func() ["sym_name" = "unconditional_br", "function_type" = !fun<[], []>, "sym_visibility" = "private"] {
    ^0:
        cf.br() (^1)
    ^1:
        cf.br() (^0)
    }
}
"""

@pytest.mark.parametrize("program_str", [(program_region), (program_region_2),
                                         (program_region_2_diff_type),
                                         (program_region_2_diff_name),
                                         (program_add), (program_add_2),
                                         (program_func), (program_successors)])
def test_immutable_ir(program_str: str):
    ctx = MLContext()
    ctx.register_dialect(Builtin)
    ctx.register_dialect(Func)
    ctx.register_dialect(Arith)
    ctx.register_dialect(Cf)

    parser = XDSLParser(ctx, program_str)
    program: Operation = parser.parse_op()
    immutable_program = get_immutable_copy(program)
    mutable_program = immutable_program.to_mutable()

    assert program.is_structurally_equivalent(mutable_program)


from ConDB import ConDB, CDTable
import sys, getopt

Usage = """
python create_table.py [options] <database name> <table_name> <column>:<type> [...]
options:
    -h <host>
    -p <port>
    -U <user>
    -w <password>

    -c - force create, drop existing table
    -s - just print SQL needed to create the table without actually creating anything
    -o <table owner>
    -R <user>,... - DB users to grant read permissions to
    -W <user>,... - DB users to grant write permissions to
"""

opts, args = getopt.getopt(sys.argv[1:], 'h:U:w:p:co:R:W:s')

if len(args) < 3 or args[0] == 'help':
    print(Usage)
    sys.exit(0)

opts = dict(opts)
dbcon = []
if "-h" in opts:        dbcon.append("host=%s" % (opts["-h"],))
if "-p" in opts:        dbcon.append("port=%s" % (int(opts["-p"]),))
if "-U" in opts:        dbcon.append("user=%s" % (opts["-U"],))
if "-w" in opts:        dbcon.append("password=%s" % (opts["-w"],))
drop_existing = "-c" in opts
grants_r = []
grants_w = []
if "-R" in opts:    grants_r = opts["-R"].split(',')
if "-W" in opts:    grants_w = opts["-W"].split(',')
owner = opts.get("-o")
sql_only = "-s" in opts

dbcon.append("dbname=%s" % (args[0],))

dbcon = ' '.join(dbcon)

tname = args[1]

ctypes = []
for w in args[2:]:
    n,t = tuple(w.split(':',1))
    ctypes.append((n,t))

if sql_only:
    sql = CDTable.createSQL(tname, owner, ctypes, grants_r, grants_w)
    print(sql)
else:
    db = ConDB(dbcon)
    t = db.createTable(tname, ctypes, owner,
        {'r':grants_r, 'w':grants_w},
        drop_existing)
    print('Table created')


with open("./input.txt") as fd:
    l = fd.readlines()
len (l)
l
map(l, int)
map(l, lambda x: int(x))
ll=[int(x) for x in l]
ll
len(ll)
lls = set(ll)
for x in lls:
    if 2020-x in lls:
        print(x, 2020-x)
1611*409
min(ll)
duplo=set()
for x in ll:
    for y in ll:
        if x!=y and x+y<2020-132:
            duplo[x+y]=(x, y)
duplo={}
for x in ll:
    for y in ll:
        if x!=y and x+y<2020-132:
            duplo[x+y]=(x, y)
len(duplo)
for x in ll:
    if 2020-x in duplo:
        print(x, duplo[2020-x])
for x in ll:
    if 2020-x in duplo:
        print(x, duplo[2020-x])
        print(x*duplo[2020-x][0]*duplo[2020-x][1])


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

from Layer import Layer
from vmapper import geometry as geom

def process_polylines(layername, geoms, indexes, labels=None, colors=None, opacitys=None, edgecolors=None, edgewidths=None, radiuses=None, showlabel=False, animate_times=None):
    alayer = Layer(layername=layername)
    geoms2 = geoms.tolist()
    for i in range(len(geoms2)):
        g = geoms2[i]
        idd = indexes[i]
        lab,fc,fo,ec,ew = None,None,None,None,None
        if not(labels is None):
            lab = labels[i]
        if not(edgecolors is None):
            ec = edgecolors[i]
        if not(edgewidths is None):
            ew = edgewidths[i]
        line = list(g.coords)
        alayer.addtoLayer(geom.MultiPolyline(vertexes=line, layer=layername, index=idd, label=lab, strokecolor=ec, strokewidth=ew, showlabel=showlabel, animate_times=animate_times))
    return alayer


from django.conf.urls import patterns, url

urlpatterns = patterns(
    "cvmo.cluster.views",
    # Views
    url(r"^new$", "show_new", name="cluster_new"),
    url(r"^clone/(?P<cluster_id>[0-9]+)$", "show_new", name="cluster_new"),
    url(r"^test$", "show_test"),
    # url(r"^edit/(?P<cluster_id>[0-9]+)$", "show_edit", name="cluster_edit"),
    url(r"^deploy/(?P<cluster_id>[0-9]+)$", "show_deploy", name="cluster_deploy"),

    # Actions
    url(r"^save$", "save", name="cluster_save"),
    url(r"^delete/(?P<cluster_id>[0-9]+)$", "delete", name="cluster_delete"),
)


class Solution:
    # @return a string
    def convert(self, s, nRows):

        if s==None or nRows<=0: return None

        if len(s)==0 or nRows==1: return s

        rows = [""] * nRows
        i = 0
        step = 1
        for c in s:
            rows[i] += c
            if i==0: step = 1
            if i==nRows-1: step = -1
            i += step

        result = ""
        for row in rows:
            result += row

        return result


#add 1+2+3+ ... 100 WITHOUT using the formula n(n+1)/2

sum = 0
n = int(input("Enter a number: "))

for i in range(n+1):
	sum = sum + i
	print(sum)

print("The sum is", sum)


#######################################################
# This simulation analyzes performance of the mean field approximation
# versus the k approximation for at large times for various values
# of kappa (degree of the tree) and beta for the Ising model.
# These two approximations are compared to MC sampled dynamics, and
# accuracy is measured by:
# the total variation distance between the joints of the root node
# and one of its neighbors, as well as the distributions of the root,
# joint distributions with more neighbors, and trajectories as well.
#
# This comparison in particular analyzes the 2Approx near the critical regime.
#######################################################

import os
import sys
sys.path.append("../SimulateProcesses/")
sys.path.append("../SimulateProcesses/Ising")
from mpl_toolkits.axes_grid1 import make_axes_locatable

import IsingMonitor as monitor

import pandas as pd
import matplotlib.pyplot as plt
import ast
import numpy as np

if __name__== "__main__":

	#np.random.seed(17)
	## at stationarity, which method works better?

	p = 0.9 #update parameter
	bias = -0.2
	p_0 = (1+bias)/2

	## intake what parameters to run/ analyze

	kappa_params = [2,3,4,5,6,7,8]
	beta_params = np.array(np.linspace(0,2,11))
	kb_params = np.array([[6,0.2],
						 [6,0.4],
						 [5,0.4],
						 [4,0.4],
						 [4, 0.6000000000000001],
						 [4,0.8],
						 [3, 0.6000000000000001],
						 [3,0.8],
						 [3,1.0],
						 [3,1.2000000000000002],
						 [3,1.4000000000000001],
						 [3,1.6],
						 [3,1.8],
						 [3,2.0]])
	k = 2
	depth = 8

	# ideally want this to be larger
	T = 50

	### Collect Data
	# use Pandas, want multidimensional index;
	# columns should be dynamics; mean field; k-approximation,various vals of k.

	# save the dataframes
	def get_df_string(s):
		f = "IsingModelResults/{}_T{}_maxkappa{}_maxbeta{}_bias{}".format(s,T,
				8, 2.0, -0.2)
		return f

	joint_prob_df = pd.read_pickle(get_df_string("mf_local_comparison"))

	def str_to_dict(str):
		d = ast.literal_eval(str)
		return d

	# compute the distances
	total_variations_kapprox = np.zeros((len(kappa_params),len(beta_params)))
	for i in range(len(kb_params)):

		# calculate dynamics
		idx = pd.IndexSlice
		prob_dynamics = np.array(list(str_to_dict(joint_prob_df.loc[idx[int(kb_params[i,0]),kb_params[i,1]],idx['dynamics']]).values()))

		# get the kapprox info
		filename = "IsingModelResults/kapprox/kapprox_kappa{}_p0.9_beta{:.1f}_T{}_k{}_bias{}.npy".format(int(kb_params[i,0]),kb_params[i,1], T, k, bias)

		measure_history = np.load(filename)

		process = monitor.MonitorLocalApproximation(np.zeros((2,2,2)),p,kb_params[i,1])
		process.measure_history = measure_history

		joint2_la = process.get_fixed_time_measure((0,1),T-1)
		prob_kapprox = joint2_la.reshape(1,4)

		total_variations_kapprox[int(kb_params[i,0]) - 2,int(kb_params[i,1]/0.2)] = np.sum(np.abs(prob_kapprox - prob_dynamics))

	def visualize_total_var(array, title):
		fig, ax = plt.subplots(1,1, figsize = (10,10))

		cmap = matplotlib.cm.viridis
		norm = plt.Normalize(0, 2)

		betas = np.array(np.linspace(0,2,81))
		kappas = 2*np.exp(2*betas)/(np.exp(2* betas) - 1)
		ax.plot(5.5*betas-0.5, kappas - 2, color = 'red', label = "Critical Value")
		im1 = ax.imshow(array[::-1,],cmap = cmap.reversed(), norm = norm, extent = [0 - 0.5,len(beta_params)-1 + 0.5,0 - 0.5,len(kappa_params)-1 + 0.5])

		tick_size = 6.5

		ax.set_xlabel('Beta',fontsize = 10)
		ax.set_xticks(np.arange(len(beta_params)))
		ax.set_xticklabels(labels = ["{:0.1f}".format(beta) for beta in beta_params],fontsize = tick_size )

		ax.set_ylabel('Kappa',fontsize = 10)
		ax.set_yticks(np.arange(len(kappa_params)))
		ax.set_yticklabels(labels = list(kappa_params),fontsize = tick_size )
		#ax.legend(loc='upper right', bbox_to_anchor=(0.15, 0.0))

		ax.set_title(title)

		for i in range(len(kappa_params)):
			for j in range(len(beta_params)):
				text = ax.text(j, i, "{:0.2f}".format(array[i, j]),
					ha="center", va="center", color="w")

		divider1 = make_axes_locatable(ax)
		cax1 = divider1.append_axes('right', size='5%', pad=0.1)
		fig.colorbar(im1,cax1,orientation = 'vertical')

		plt.show()

	visualize_total_var(total_variations_kapprox, "Accuracy of 2-Approximation at Critical Region")


import roxar.events

def set_wdef_events(simwells, trajectory_type='Drilled trajectory'):
    """Define roxar events from SimMultiWells
    Args:
        simwells (SimMultiWells): Well info
    Returns:
        List of roxar events
    """

    elist = []

    for simw in simwells:
        if simw.start_date is None:
            continue
        wname = simw.name
        edate = simw.start_date

        if simw.type is not None:
            eve = roxar.events.Event.create(roxar.EventType.WTYPE, edate, [wname])
            eve['TYPE'] = simw.type
            if simw.phase is not None:
                eve['PHASE'] = simw.phase
            elist.append(eve)

        if simw.bhpref is not None:
            eve = roxar.events.Event.create(roxar.EventType.WDREF, edate, [wname])
            eve['DEPTH'] = simw.bhpref
            elist.append(eve)

        if simw.group is not None:
            eve = roxar.events.Event.create(roxar.EventType.GMEMBER, edate, [simw.group])
            eve['MEMBER'] = wname
            elist.append(eve)

        if simw.lifttab is not None:
            eve = roxar.events.Event.create(roxar.EventType.WLIFTTABLE, edate, [wname])
            eve['TABLEID'] = str(simw.lifttab)
            elist.append(eve)

        if simw.crossflow is not None:
            eve = roxar.events.Event.create(roxar.EventType.WCROSSFL, edate, [wname])
            eve['ON'] = simw.crossflow
            elist.append(eve)

        if simw.densmod is not None:
            eve = roxar.events.Event.create(roxar.EventType.WDENSMOD, edate, [wname])
            if simw.densmod == 'AVE':
                eve['TYPE'] = 'Average'
            elif simw.densmod == 'SEG':
                eve['TYPE'] = 'Segregated'
            else:
                eve['TYPE'] = simw.densmod
            elist.append(eve)

        if simw.cmode is not None:
            eve = roxar.events.Event.create(roxar.EventType.WCONTROL, edate, [wname])
            eve['MODE'] = simw.cmode
            if simw.cphase is not None:
                eve['PHASE'] = simw.cphase
            else:
                eve['PHASE'] = 'From well type'
            elist.append(eve)

        if simw.mdstart is not None:
            own = [wname, wname, trajectory_type]
            eve = roxar.events.Event.create(roxar.EventType.WSEGMOD, edate, own)
            eve['MDSTART'] = simw.mdstart
            eve['MDEND'] = 10000.
            eve['TYPE'] = 'Staggered'
            eve['GRAV'] = True
            eve['FRIC'] = True
            eve['ACCEL'] = False
            eve['MULTMOD'] = 'Homogeneous'
            elist.append(eve)

    return elist


##
#   This file is part of MegBot.
#
#   MegBot is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   MegBot is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with MegBot.  If not, see <http://www.gnu.org/licenses/>.
##

"""This will handle the validation and delegation of IRC messages.
it'll always return None (even if it's valid), it'll first validate
the message once it has, it'll check to see a plugin should be called.
If a plugin should be called i.e.

  nick!ident@host PRIVMSG #channel :<trigger><valid plugin>[args]

then it'll delegate that to the core executor plugin.
"""

def main(connection, command):
    """Delegates to other other core items when a valid IRC command
    is passed to it.

    command: this should be a raw command passed from the IRCd (str)

    e.g.
        PING :gilman.megworld.co.uk
        nick!ident@host PRIVMSG #megworld :this is a message.

    the function will return None as it'll delegate to other core
    modules or do nothing and return back.
    """
    # we don't want to do anything if the line is blank
    if not command:
        return

    # can we make this better?
    info = connection.libraries["IRCObjects"].Info(command, connection)

    if info.action == "PING":
        # Lets hand off to ping in case it's a PING message.
        connection.core["Coreping"].main(connection, info)

    # Okay, we'll now see if any hooks wish to be called on it. - old system
    connection.hooker.hook(connection, info)

    # Make an event (new events/hook system)
    event = connection.core["Corehandler"].IRCEvent(info)
    connection.handler.event(event)

    if not info.message:
        return

    trigger = connection.settings["trigger"]
    if info.trigger != trigger:
        return

    # Okay so at this point we can say they are trying to call a plugin.
    # first thing's first is we need to check the plugin actually exists

    if info.plugin_name not in connection.plugin:
        return

    # last thing we need to do is delegate off to the executor core plugin
    # this will put the plugin into a thread and execute it.
    ##
    # Remove .split() when #36 is done
    ##
    connection.core["Coreexecutor"].main(connection, info, info.plugin_name)


from flask import redirect, render_template, request, abort, session, flash, url_for
from app.db import connection
from app.models.user import User
from app.helpers.auth import authenticated
from app.models.folder import Folder
from app.models.task import Task

def show():
    if not authenticated(session):
        abort(403)
    else:
        id_folder = request.args.get("id_folder")
        if(id_folder == None) or (id_folder == ""):
            flash("The 'id' field is empty!", category="error")
            return redirect(url_for("user-main-menu"))
        conn = connection()
        if User.has_permision_to_open_folder(conn, session['user']['id'], id_folder):
            folder = Folder.find_by_id(conn, id_folder)
            if folder:
                tasks = Task.find_by_id_folder(conn, folder['id'])
                return render_template("folder/folder-show.html", folder=folder, tasks=tasks)
            else:
                flash("The folder doesn't exist!", category="error")
        else:
            flash("You don't have permission to access that folder!", category="error")
        return redirect(url_for("user-main-menu"))

def create():
    if not authenticated(session):
        abort(403)
    else:
        folder_name = request.form.get("folder_name")
        if(folder_name == None) or (folder_name == ""):
            flash("The folder must have a name!", category="error")
            return redirect(url_for("user-main-menu"))
        conn = connection()
        if(Folder.exists(conn, session['user']['id'], folder_name)):
            flash("The folder already exists!", category="error")
        else:
            try:
                Folder.create(conn, session['user']['id'], folder_name)
                flash("The folder was created!", category="success")
            except:
                flash("There was an error", category="error")
        return redirect(url_for("user-main-menu"))

def delete():
    if not authenticated(session):
        abort(403)
    else:
        id_folder = request.form.get("id_folder")
        if(id_folder == None) or (id_folder == ""):
            flash("The folder must have an ID!", category="error")
            return redirect(url_for("user-main-menu"))

        conn = connection()
        folder = Folder.find_by_id(conn, id_folder)
        if not folder:
            flash("The folder doesn't exist!", category="error")
        else:
            if folder['id_user'] == session['user']['id']:
                try:
                    Folder.delete(conn, session['user']['id'], id_folder)
                    flash("The folder was deleted!", category="success")
                except:
                    flash("There was an error!", category="error")
            else:
                flash("You don't have access to that folder!", category="error")
        return redirect(url_for("user-main-menu"))

def main_menu():
    if not authenticated(session):
        abort(403)
    else:
        conn = connection()
        folders = Folder.find_by_id_user(conn, session['user']['id'])
        return render_template("user/menu.html", folders=folders)


import numpy as np
import torch
from ServerDL.cfgs.Protocol import *

def packup(prediction, segms=None, result_type="detection"):
    if result_type == "detection":          # detection
        response_boxes = prediction.bbox.cpu().int().numpy().flatten().tolist()
        response_classes = prediction.get_field("labels").cpu().numpy().flatten().tolist() if prediction.has_field("labels") else None
        response_keyps = prediction.get_field("keypoint").cpu().numpy().flatten().tolist() if prediction.has_field("keypoint") else None  # ???不确定fieldname是不是keypoint
        response_conf = prediction.get_field("scores").cpu().numpy().flatten().tolist() if prediction.has_field("scores") else None

        package = {
            KEY.REQUEST_TYPE: VALUE.REQUEST_TYPE.MODEL_PREDICT,
            KEY.RESPONSE_STATUS: VALUE.RESPONSE_STATUS.SUCCEED,
            KEY.PREDICTION.BOXES: response_boxes,
            KEY.PREDICTION.BOXMODE: prediction.mode,
        }
        if response_classes:
            package[KEY.PREDICTION.CLASSES] = response_classes
        if response_conf:
            package[KEY.PREDICTION.CONFIDENCES] = response_conf
        if response_keyps:
            package[KEY.PREDICTION.KEYPSINFO] = response_keyps,

        if isinstance(segms, torch.Tensor):
            segms = segms.cpu().numpy().astype(np.uint8)
            package[KEY.PREDICTION.MASKINFO] = {KEY.PREDICTION.ROWS: segms.shape[0],
                                        KEY.PREDICTION.COLS: segms.shape[1],
                                      KEY.PREDICTION.CHANNELS: segms.shape[2] if len(segms.shape) == 3 else 1}
    elif result_type == "classification":       # classification
        if isinstance(prediction, torch.Tensor):
            prediction = prediction.cpu().numpy().tolist()
        if isinstance(prediction, np.ndarray):
            prediction = prediction.tolist()
        package = {
            KEY.REQUEST_TYPE: VALUE.REQUEST_TYPE.MODEL_PREDICT,
            KEY.RESPONSE_STATUS: VALUE.RESPONSE_STATUS.SUCCEED,
            KEY.PREDICTION.CLASSES: list(prediction) if isinstance(prediction, list) else [prediction]
        }
    elif result_type == "segmentation":
        assert isinstance(prediction, np.ndarray), "segmentation is no a ndarray"
        segms = prediction.copy()
        package = {
            KEY.REQUEST_TYPE: VALUE.REQUEST_TYPE.MODEL_PREDICT,
            KEY.RESPONSE_STATUS: VALUE.RESPONSE_STATUS.SUCCEED,
            KEY.PREDICTION.MASKINFO: {KEY.PREDICTION.ROWS: segms.shape[0],
                                      KEY.PREDICTION.COLS: segms.shape[1],
                                      KEY.PREDICTION.CHANNELS: segms.shape[2] if len(segms.shape) == 3 else 1}
        }

    print(package)
    return package, segms

def select_top_predictions(predictions, confidence_threshold):
    scores = predictions.get_field("scores")
    keep = torch.nonzero(scores > confidence_threshold).squeeze(1)
    predictions = predictions[keep]
    scores = predictions.get_field("scores")
    _, idx = scores.sort(0, descending=True)
    return predictions[idx]


"""
	Heartbeat

	Let Jupyter know we're still here.

	Note that unlike any other role, this socket is used via
	raw bytes and not in a ZMQ Message envelope.

	Regardless, the heartbeat is "send back whatever you got"
	so it doesn't matter terribly much what it says.
"""
logger = shared.tools.jupyter.logging.Logger()

from datetime import datetime

def payload_handler(kernel, bytes_payload):
	logger.trace('Ping recieved << %r' % (bytes_payload,))
	if kernel.session:
		kernel.heartbeat_socket.send(bytes_payload)
	else:
		kernel.heartbeat_socket.send('')
	logger.trace('Ping returned >> %r' % (bytes_payload,))

	# Provide kernel with the option to check for cardiac arrest
	kernel.last_heartbeat = datetime.now()
#	logger.info('Heartbeat    :D ')


import logging
from datetime import datetime

from django.conf import settings
from django.db.models import Sum
from rest_framework import status

from rest_admin.src.modules.merchants.models import Merchants
from rest_admin.src.modules.meals.models import Meals

from vendor.errors import error_message
from vendor.helper import helper
from vendor.template import json_response

import random

logger = logging.getLogger(__name__)

class MerchantHandler():

    def __init__(self, start_time):
        self.start_time = start_time

    def list_merchant(self, request_body):
        project_filter = {}

        if "category" in request_body:
            project_filter["category"] = request_body["category"]

        if "meals_time" in request_body:
            project_filter["meals_time__icontains"] = request_body["meals_time"]

        merchant_list = Merchants.objects.filter(**project_filter).exclude(category="Mystery").order_by('-id')

        merchant = []
        for data in merchant_list:
            merchant.append(self.get_merchant_info(data))

        resp = json_response.render_api_success_response(merchant, self.start_time, total_records=1)
        return resp, status.HTTP_200_OK

    def list_meals(self, request_body):
        project_filter = {}
        if "category" in request_body:
            project_filter["category"] = request_body["category"]

        if "meals_time" in request_body:
            project_filter["meals_time__icontains"] = request_body["meals_time"]

        if "selected_date" in request_body:
            project_filter["date"] = request_body["selected_date"]

        list_meal = Meals.objects.filter(**project_filter)

        meals = []
        for item in list_meal:

            meals.append({"id":item.id,
                            "name":item.name,
                            "merchant_id":item.merchant.id,
                            "merchant_name":item.merchant.name,
                            "price":item.price,
                            "description":item.description,
                            "meals_time":item.meals_time.split(","),
                            "date":datetime.strptime(item.date, "%Y-%m-%d").strftime("%A, %d %b %Y"),
                            "category":item.category,
                            "thumbnail":item.thumbnail})

        resp = json_response.render_api_success_response(meals, self.start_time, total_records=1)
        return resp, status.HTTP_200_OK

    def get_meals_detail(self, meals_id):

        try:
            meals = Meals.objects.get(pk=meals_id)

            data = {"id":meals.id,
                    "merchant_id":meals.merchant.id,
                    "merchant_name":meals.merchant.name,
                    "name":meals.name,
                    "price":meals.price,
                    "description":meals.description,
                    "thumbnail":meals.thumbnail,
                    "meals_time":meals.meals_time.split(",")
                    }

        except Meals.DoesNotExist:
            resp = json_response.render_api_error_response(error_message="Error data not found", status=status.HTTP_404_NOT_FOUND)
            return resp, status.HTTP_404_NOT_FOUND

        resp = json_response.render_api_success_response(data, self.start_time, total_records=1)
        return resp, status.HTTP_200_OK

    def get_merchant_info(self, data, meals_displayed=False):

        merchant_detail = {"id":data.id,
                "description":data.description,
                "price":data.price,
                "name":data.name,
                "thumbnail":data.thumbnail,
                "category":data.category,
                "rating":data.rating,
                "reviews":data.reviews,
                "meals_time":data.meals_time.split(","),
                "created_at":data.created_at}

        if meals_displayed:
            list_meal = Meals.objects.filter(merchant__id=data.id).order_by('date')
            meals = []
            for item in list_meal:

                meals.append({"id":item.id,
                              "name":item.name,
                              "price":item.price,
                              "description":item.description,
                              "meals_time":item.meals_time.split(","),
                              "date":datetime.strptime(item.date, "%Y-%m-%d").strftime("%A, %d %b %Y"),
                              "category":item.category,
                              "thumbnail":item.thumbnail})

            merchant_detail["meals"] = meals

        return merchant_detail

    def merchant_detail(self, merchant_id):

        try:
            merchant = Merchants.objects.get(pk=merchant_id)
            merchant_detail = self.get_merchant_info(merchant, meals_displayed=True)

            resp = json_response.render_api_success_response(merchant_detail, self.start_time, total_records=1)
            return resp, status.HTTP_200_OK
        except Merchants.DoesNotExist:
            status_code = status.HTTP_404_NOT_FOUND
            resp = json_response.render_api_error_response(error_message="Data not found", status=status_code)
            return resp, status_code