"""
TODO
    Move this into custom LineEdit with model/completer.  Similair to the
    AbstractFileBrowser currently located in KatanaBebop
"""
from qtpy.QtWidgets import (
    QComboBox, QLineEdit, QCompleter, QSizePolicy, QVBoxLayout
)

from qtpy.QtGui import(
    QStandardItem, QStandardItemModel, QPixmap, QIcon, QColor
)
from qtpy.QtCore import (
    QEvent, Qt, QSortFilterProxyModel
)

from qtpy.QtCore import (
    QAbstractListModel, Qt, QRegExp, QSortFilterProxyModel)
from qtpy.QtWidgets import (
    QLineEdit, QCompleter, QListView, QStyledItemDelegate, QApplication)

from cgwidgets.widgets.AbstractWidgets import AbstractStringInputWidget
from cgwidgets.utils import getBottomLeftPos, getFontSize
from cgwidgets.settings.colors import iColor
from cgwidgets.views.CompleterView import CompleterPopup

class AbstractComboListInputWidget(QComboBox):
    """
    A custom QComboBox with a completer / model.  This is
    designed to be an abstract class that will be inherited by the
    the GSV and Node ComboBoxes

    Attributes:
        exists (bool) flag used to determine whether or not the popup menu
            for the menu change should register or not (specific to copy/paste
            of a node.

            In plain english... this flag is toggled to hide the Warning PopUp Box
            from displaying to the user in some events.
        item_list (list): string list of all of the items in the list.  Updating this
            will auto update the default settings for blank setups
        previous_text (str): the previous items text.  This is stored for cancel events
            and allowing the user to return to the previous item after cancelling.
    """
    TYPE = 'list'
    def __init__(self, parent=None):
        super(AbstractComboListInputWidget, self).__init__(parent)
        self.main_widget = self.parent()
        self.previous_text = ''
        self.setExistsFlag(True)

        # setup line edit
        #self.line_edit = QLineEdit("Select & Focus", self)
        self.line_edit = QLineEdit(self)
        self.line_edit.editingFinished.connect(self.userFinishedEditing)
        self.setLineEdit(self.line_edit)

        self.setEditable(True)

        # setup completer
        self.completer = QCompleter(self)
        self.completer.setCompletionMode(QCompleter.PopupCompletion)
        self.completer.setCaseSensitivity(Qt.CaseInsensitive)
        self.completer.setPopup(self.view())
        self.setCompleter(self.completer)
        self.pFilterModel = QSortFilterProxyModel(self)

        # set size policy ( this will ignore the weird resizing effects)
        size_policy = QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Preferred)
        self.setSizePolicy(size_policy)

        # initialize model...
        model = QStandardItemModel()
        self.setModel(model)
        self.setModelColumn(0)

    def populate(self, item_list):
        """
        Creates all of the items from the item list and
        appends them to the model.

        item_list (list): list of strings to be displayed to the user
            this should be set with the setCleanItemsFunction.
        """
        self.setItemList(item_list)
        for i, item_name in enumerate(self.getItemList()):
            item = QStandardItem(item_name)
            self.model().setItem(i, 0, item)
            self.setExistsFlag(False)

    def update(self):
        """
        Updates the model items with all of the graph state variables.

        This is very similar to the populate call, except that with this, it
        will remove all of the items except for the current one.  Which
        will ensure that an indexChanged event is not registered thus
        updating the UI.
        """
        self.setExistsFlag(False)

        self.model().clear()
        self.populate(self.getCleanItems())
        self.setCurrentIndexToText(self.previous_text)

        self.setExistsFlag(True)

    def setModel(self, model):
        # somehow this super makes the node type not resize...
        super(AbstractListInputWidget, self).setModel(model)
        self.pFilterModel.setSourceModel(model)
        self.completer.setModel(self.pFilterModel)

    def setModelColumn(self, column):
        self.completer.setCompletionColumn(column)
        self.pFilterModel.setFilterKeyColumn(column)
        super(AbstractListInputWidget, self).setModelColumn(column)

    def view(self):
        return self.completer.popup()

    """ UTILS """
    def next_completion(self):
        row = self.completer.currentRow()

        # if does not exist reset
        if not self.completer.setCurrentRow(row + 1):
            self.completer.setCurrentRow(0)

        # if initializing
        if self.completer.popup().currentIndex().row() == -1:
            self.completer.setCurrentRow(0)

        index = self.completer.currentIndex()
        self.completer.popup().setCurrentIndex(index)

    def previous_completion(self):
        row = self.completer.currentRow()
        numRows = self.completer.completionCount()

        # if wrapping
        if not self.completer.setCurrentRow(row - 1):
            self.completer.setCurrentRow(numRows - 1)
        # if initializing
        if self.completer.popup().currentIndex().row() == -1:
            self.completer.setCurrentRow(numRows - 1)

        index = self.completer.currentIndex()
        self.completer.popup().setCurrentIndex(index)

    def setCurrentIndexToText(self, text):
        """
        Sets the current index to the text provided.  If no
        match can be found, this will default back to the
        first index.

        If no first index... create '' ?
        """
        self.setExistsFlag(False)

        # get all matches
        items = self.model().findItems(text, Qt.MatchExactly)

        # set to index of match
        if len(items) > 0:
            index = self.model().indexFromItem(items[0]).row()
            self.setCurrentIndex(index)
        else:
            self.setCurrentIndex(0)
        self.previous_text = self.currentText()
        self.setExistsFlag(True)

    def isUserInputValid(self):
        """
        Determines if the new user input is currently
        in the model.

        Returns True if this is an existing item, Returns
        false if it is not.
        """
        items = self.model().findItems(self.currentText(), Qt.MatchExactly)
        if len(items) > 0:
            return True
        else:
            return False

    def setupStyleSheet(self):
        width = self.width()
        dropdown_width = int(width * 0.35)
        style_sheet_args = iColor.style_sheet_args
        style_sheet_args['width'] = dropdown_width
        # QComboBox {{
        #     border: None;
        #     background-color: rgba{rgba_gray_0}
        # }}
        style_sheet = """
            QComboBox{{
                border: None;
                background-color: rgba{rgba_gray_0};
                color: rgba{rgba_text};
            }}
            QComboBox::drop-down {{
                width: {width}px;
            }}
            QLineEdit{{
                border: None;
                background-color: rgba{rgba_gray_0};
                color: rgba{rgba_text};
            }}
            QListView{{
                border: None;
                background-color: rgba{rgba_gray_0};
                color: rgba{rgba_text};
            }}
            QListView::item:hover{{
                background-color: rgba(255,0,0,255);
            }}
        """.format(**style_sheet_args)

        self.completer.popup().setStyleSheet("""
            QListView{{
                border: None;
                background-color: rgba{rgba_gray_0};
                color: rgba{rgba_text};
            }}
            QListView::item:hover{{
                background-color: rgba(255,0,0,255);
            }}
        """.format(**style_sheet_args))

        self.setStyleSheet(style_sheet)

    """ API """
    def __selectionChangedEmit(self):
        pass

    def setSelectionChangedEmitEvent(self, method):
        """
        sets the method for the selection changed emit call
        this will be called everytime the user hits enter/return
        inside of the line edits as a way of sending an emit
        signal from the current text changed (finalized) before
        input changed event...
        """
        self.__selectionChangedEmit = method

    def __getCleanItems(self):
        return []

    def setCleanItemsFunction(self, function):
        """
        Sets the function to get the list of strings to populate the model

        function (function): function to return a list of strings to be shown
            to the user
        """
        self.__getCleanItems = function

    def getCleanItems(self):
        """
        Returns a list of strings based off of the function set with
        setCleanItemsFunction
        """
        return self.__getCleanItems()

    """ EVENTS """
    def userFinishedEditing(self):
        is_input_valid = self.isUserInputValid()
        if is_input_valid:
            self.__selectionChangedEmit()
            self.previous_text = self.currentText()
            #self.userFinishedEditingEvent(self.currentText())
        else:
            self.setCurrentIndexToText(self.previous_text)

    def resizeEvent(self, event):
        self.setupStyleSheet()
        return QComboBox.resizeEvent(self, event)

    def event(self, event, *args, **kwargs):
        """
        Registering key presses in here as for some reason
        they don't work in the keyPressEvent method...
        """
        if event.type() == QEvent.KeyPress:
            # tab
            if event.key() == Qt.Key_Tab:
                self.next_completion()
                return True

            # shift tab
            elif event.key() == Qt.Key_Tab + 1:
                self.previous_completion()
                return True

            # enter pressed
            elif event.key() in [
                Qt.Key_Return,
                Qt.Key_Enter,
                Qt.Key_Down
            ]:
                self.__selectionChangedEmit()

        elif event.type() == QEvent.MouseButtonRelease:
            self.completer.setPopup(self.view())

        return QComboBox.event(self, event, *args, **kwargs)

    """ PROPERTIES """
    def getExistsFlag(self):
        return self._exists

    def setExistsFlag(self, exists):
        self._exists = exists

    def getItemList(self):
        return self._item_list

    def setItemList(self, item_list):
        if self.previous_text == '':
            item_list.insert(0, '')
        self._item_list = item_list

    @property
    def previous_text(self):
        return self._previous_text

    @previous_text.setter
    def previous_text(self, previous_text):
        self._previous_text = previous_text

class AbstractListInputWidget(AbstractStringInputWidget):
    """
    TODO:
        *   as soon as you type it breaks?
    Signals:
    QLineEdit
        QCompleter
            | -- QSortFilterProxyModel --> QAbstractListModel (Custom Model)
            | -- Popup
                    | -- QListView (CompleterPopup)
    Attributes:
        dynamic_update (bool): Determines if the model should be repopulated
            every time the user requests the popup to be displayed
        filter_results (bool): Determines if the returned results in the popup
            should be filtered based off of the current input.
        display_item_colors (bool): Determines if the item colors should be displayed
            or not.  By default this is set to off.  Item colors will need to be put as the
            second index for each item in the model provided to the list.
        item_list (list): string list of all of the items in the list.  Updating this
            will auto update the default settings for blank setups
        previous_text (str): the previous items text.  This is stored for cancel events
            and allowing the user to return to the previous item after cancelling.
        cleanItems (virtual function): returns a list of items to populate the
            model with.  Not sure if I still need this...
    """
    TYPE = 'list'

    def __init__(self, parent=None, item_list=[]):
        super(AbstractListInputWidget, self).__init__(parent)
        # setup default attrs
        self._item_list = []
        self._previous_text = ''
        self._dynamic_update = False
        self._filter_results = True
        self._display_item_colors = False

        # setup custom completer
        self.setupCustomModelCompleter(item_list)

        # setup style
        self.updateStyleSheet()

        # setup signals
        self.textChanged.connect(self.filterCompletionResults)

    def populate(self, item_list):
        """
        Creates all of the items from the item list and
        appends them to the model.

        item_list (list): list of strings to be displayed to the user
            this should be set with the setCleanItemsFunction.
        """
        self.setItemList(item_list)
        self.setupCustomModelCompleter(item_list)

    def __getCleanItems(self):
        return []

    def setCleanItemsFunction(self, function):
        """
        Sets the function to get the list of strings to populate the model

        function (function): function to return a list of strings to be shown
            to the user
        """
        self.__getCleanItems = function

    def getCleanItems(self):
        """
        Returns a list of strings based off of the function set with
        setCleanItemsFunction
        """
        return self.__getCleanItems()

    """ Style Sheet"""
    def updateStyleSheet(self):
        style_sheet_args = iColor.style_sheet_args
        style_sheet_args.update({
            'type': type(self).__name__
        })

        style_sheet = """
        {type}{{
            border:None;
            background-color: rgba{rgba_gray_0};
            selection-background-color: rgba{rgba_selected};
            color: rgba{rgba_text}
        }}

        """.format(**style_sheet_args)

        self.setStyleSheet(style_sheet)

    """ COMPLETER """
    def _updateModel(self, item_list=None):
        # get item list
        if not item_list:
            item_list = self.getCleanItems()

        # completer = CustomModelCompleter()
        # self.setCompleter(completer)
        # update model items
        self._model = CustomModel(item_list=item_list)
        self._model.display_item_colors = self.display_item_colors
        self.proxy_model = QSortFilterProxyModel()
        self.proxy_model.setSourceModel(self._model)

        # set models
        self.completer().setModel(self.proxy_model)

        # set item for popup
        # this makes it so that the stylesheet can be attached...
        # https://forum.qt.io/topic/26703/solved-stylize-using-css-and-editable-qcombobox-s-completions-list-view/7
        delegate = QStyledItemDelegate()
        self.completer().popup().setItemDelegate(delegate)

    def setupCustomModelCompleter(self, item_list):
        """
        Creates a new completely custom completer

        Args:
            item_list (list): of strings to be the list of things
                that is displayed to the user
        """
        # create completer/models
        completer = CustomModelCompleter()
        self.setCompleter(completer)
        self.proxy_model = QSortFilterProxyModel()
        self._updateModel(item_list)

    def filterCompletionResults(self):
        """
        Filter the current proxy model based off of the text in the input string
        """
        # preflight
        if not self.filter_results: return

        # filter results
        if self.text() != '':
            self.completer().setCaseSensitivity(False)
            self.completer().setCompletionMode(QCompleter.PopupCompletion)
        else:
            self.completer().setCompletionMode(QCompleter.UnfilteredPopupCompletion)

    """ EVENTS """
    def mouseReleaseEvent(self, event, *args, **kwargs):
        # update model (if enabled)
        if self.dynamic_update:
            self._updateModel()

        # update the current proxy model based off the current text
        self.filterCompletionResults()

        # show completer
        self.completer().complete()

        return QLineEdit.mouseReleaseEvent(self, event, *args, **kwargs)

    """ UTILS """
    def next_completion(self):
        row = self.completer().currentRow()

        # if does not exist reset
        if not self.completer().setCurrentRow(row + 1):
            self.completer().setCurrentRow(0)

        # if initializing
        if self.completer().popup().currentIndex().row() == -1:
            self.completer().setCurrentRow(0)

        index = self.completer().currentIndex()
        self.completer().popup().setCurrentIndex(index)

    def previous_completion(self):
        row = self.completer().currentRow()
        numRows = self.completer().completionCount()

        # if wrapping
        if not self.completer().setCurrentRow(row - 1):
            self.completer().setCurrentRow(numRows - 1)
        # if initializing
        if self.completer().popup().currentIndex().row() == -1:
            self.completer().setCurrentRow(numRows - 1)

        index = self.completer().currentIndex()
        self.completer().popup().setCurrentIndex(index)

    def event(self, event, *args, **kwargs):
        if (event.type() == QEvent.KeyPress) and (event.key() == Qt.Key_Tab):
            if self.text() == '':
                self.completer().complete()
            else:
                self.next_completion()
            return True

        if (event.type() == QEvent.KeyPress) and (event.key() == 16777218):
            self.previous_completion()
            return True

        return AbstractStringInputWidget.event(self, event, *args, **kwargs)

    """ PROPERTIES """
    @property
    def dynamic_update(self):
        return self._dynamic_update

    @dynamic_update.setter
    def dynamic_update(self, dynamic_update):
        self._dynamic_update = dynamic_update

    @property
    def filter_results(self):
        return self._filter_results

    @filter_results.setter
    def filter_results(self, filter_results):
        self._filter_results = filter_results

    @property
    def display_item_colors(self):
        return self._display_item_colors

    @display_item_colors.setter
    def display_item_colors(self, display_item_colors):
        self._display_item_colors = display_item_colors
        self.model().display_item_colors = display_item_colors

    def getItemList(self):
        return self._item_list

    def setItemList(self, item_list):
        # if self.previous_text == '':
        #     item_list.insert(0, '')
        self._item_list = item_list

    @property
    def previous_text(self):
        return self._previous_text

    @previous_text.setter
    def previous_text(self, previous_text):
        self._previous_text = previous_text

    def model(self):
        return self._model

    def setModel(self, _model):
        self._model = _model

class CustomModelCompleter(QCompleter):
    def __init__(self, parent=None):
        super(CustomModelCompleter, self).__init__(parent)
        popup = CompleterPopup()
        self.setPopup(popup)

    # def activated(self, text):
    #     print("activated === ", text)
    #     return QCompleter.activated(self, text)
    #
    # def splitPath(self, path):
    #     print("splitPath === ", path)
    #     return QCompleter.splitPath(self, 'yolo')

class CustomModel(QAbstractListModel):
    """
    The main item list expects two indexes
        0: Display/Edit Role
        1: Decoration Role (R, G, B, A)
    """
    def __init__(self, parent=None, item_list=[['Display Role', (64, 64, 128, 255)]]):
        super(CustomModel, self).__init__(parent)
        self._display_item_colors = False
        self.item_list = item_list

    def rowCount(self, parent):
        return len(self.item_list)

    def data(self, index, role):
        if role in (Qt.DisplayRole, Qt.EditRole):
            try:
                return self.item_list[index.row()][0]
            except IndexError:
                return None

        elif role == Qt.DecorationRole:
            # preflight
            if not self.display_item_colors: return

            # get display color
            try:
                color = QColor(*self.item_list[index.row()][1])
            except IndexError:
                color = QColor(*iColor["rgba_gray_0"])

            # create pixmap/icon
            pixmap = QPixmap(getFontSize(QApplication) * 2, getFontSize(QApplication) * 0.5)
            pixmap.fill(color)
            icon = QIcon(pixmap)
            return icon

    @property
    def item_list(self):
        return self._item_list

    @item_list.setter
    def item_list(self, item_list):
        self._item_list = item_list

    @property
    def display_item_colors(self):
        return self._display_item_colors

    @display_item_colors.setter
    def display_item_colors(self, display_item_colors):
        self._display_item_colors = display_item_colors

if __name__ == "__main__":
    import sys
    from qtpy.QtWidgets import QApplication, QWidget, QVBoxLayout
    from qtpy.QtGui import QCursor

    app = QApplication(sys.argv)
    w = QWidget()
    l = QVBoxLayout(w)
    r = AbstractListInputWidget(item_list=[['a', (255,0,0, 255)], ['b'], ['c'], ['aa'], ['bb'], ['cc']])
    r.display_item_colors = True
    e = CompleterPopup()
    l.addWidget(r)
    l.addWidget(e)
    e.setModel(r.proxy_model)

    # item_list = ['a', 'b', 'c', 'aa', 'bb', 'cc']
    # w=QListView()
    # w.setStyleSheet("""
    #     QListView::item:selected{background-color: rgba(255,0,0,255);}
    # """)
    # model = CustomModel(item_list=item_list)
    # w.setModel(model)
    w.show()
    w.move(QCursor.pos())
    sys.exit(app.exec_())


__author__ = 'yinyan'
"""
Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
"""
class Solution(object):
    def isAnagram(self, s, t):
        """
        :param s: str
        :param t: str
        :return: bool
        """
        if len(s) != len(t):
            return False

        dictS=self.helper(s)
        dictT=self.helper(t)

        for key, value in dictS.iteritems():
            if key in dictT and value == dictT[key]:
                continue
            else:
                return False
        return True

    def helper(self, s):
        dict={}
        for i in range(len(s)):
            if dict.has_key(s[i]):
                dict[s[i]]+=1
            else:
                dict[s[i]]=1
        return dict


class CharacterChannel(__CharacterManager):

    def __init__(self, owner):
        super(CharacterChannel, self).__init__(owner)
        self.gagging = list()
        self.monitor = False

    def receive(self, channel, speech, emit=False):
        if channel in self.gagging:
            return
        if not hasattr(self.owner, 'player'):
            return
        prefix = self.prefix(channel)
        if emit:
            self.owner.msg('%s %s' % (prefix, speech))
            return
        if self.monitor:
            render = speech.monitor_display(self.owner)
        else:
            render = speech.render(self.owner)
        self.owner.msg('%s %s' % (prefix, render))

    def color_name(self, channel):
        color = self.owner.player.colors.channels.get(channel, None)
        if not color:
            color = channel.db.color
        if not color:
            color = 'n'
        return '|%s%s|n' % (color, channel.key)

    def prefix(self, channel):
        return '<%s>' % self.color_name(channel)

    def status(self, channel):
        if not channel.has_connection(self.owner):
            return 'Off'
        if channel in self.gagging:
            return 'Gag'
        return 'On'

    def visible(self):
        channels = PublicChannel.objects.all().order_by('db_key')
        return [chan for chan in channels if chan.locks.check(self.owner, 'listen')]


'''
Author: dingdingtao
Date: 2021-04-16 16:00:11
LastEditTime: 2021-04-16 16:00:11
LastEditors: dingdingtao
Description:
'''
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 500)
dashes = [10, 5, 100, 5]  # 10 points on, 5 off, 100 on, 5 off

fig, ax = plt.subplots()
line1, = ax.plot(x, np.sin(x), '--', linewidth=2,
                 label='Dashes set retroactively')
line1.set_dashes(dashes)

line2, = ax.plot(x, -1 * np.sin(x), dashes=[30, 5, 10, 5],
                 label='Dashes set proactively')

ax.legend(loc='lower right')
plt.show()


from __future__ import print_function
import unittest
import numpy as np
import pytraj as pt
from pytraj.utils import eq, aa_eq
from pytraj.testing import cpptraj_test_dir

class TestAtomicFluct(unittest.TestCase):

    def test_bfactors(self):
        traj = pt.iterload("./data/tz2.nc", "./data/tz2.parm7")
        iter_options = {'start': 9, 'stop': 30, 'step': 2}

        bfactors = pt.calc_bfactors(traj(**iter_options))
        s_fname = "/".join((cpptraj_test_dir, "Test_AtomicFluct",
                            "fluct.4.dat.save"))
        saved_bfactors = np.loadtxt(s_fname).T[1]

        aa_eq(saved_bfactors, bfactors.T[1])

        b2 = pt.calc_bfactors(traj(**iter_options), dtype='dataset')
        assert b2[0].key == 'B-factors'

    def test_RMSF(self):
        traj = pt.iterload("./data/tz2.nc", "./data/tz2.parm7")

        state = pt.load_batch(traj, '''
        rms first
        average crdset MyAvg
        run
        rms ref MyAvg
        atomicfluct out fluct.agr''')
        state.run()

        t0 = traj[:]
        pt.superpose(t0, ref=0)
        avg = pt.mean_structure(t0)
        pt.superpose(t0, ref=avg)
        data = pt.rmsf(t0)
        aa_eq(data, state.data[-1].values)

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


import tensorflow as tf

train_in = [
  [1., 1.], [1., 0.],
  [0., 1.], [0., 0.]
]
train_out = [
  [0.], [1.],
  [1.], [0.]
]

# w1 to set a random 2x2 -> 2 (0)
w1 = tf.Variable(tf.random_normal([2, 2]))
b1 = tf.Variable(tf.zeros([2]))
# w2 to set a random 2x1 -> 1 (0)
w2 = tf.Variable(tf.random_normal([2, 1]))
b2 = tf.Variable(tf.zeros([1]))
# out1 to be train_in X w1 + b1 ~ prediction
out1 = tf.nn.relu(tf.matmul(train_in, w1) + b1)
# out2 to be prediction X w2 + b2
out2 = tf.nn.relu(tf.matmul(out1, w2) + b2)
# calculate error
error = tf.subtract(train_out, out2)
mse = tf.reduce_mean(tf.square(error))

train = tf.train.GradientDescentOptimizer(0.01).minimize(mse)

init = tf.global_variables_initializer()
sess = tf.Session()

err = 1.0
target = 0.01
epoch = 0
max_epochs = 1000

for i in range(max_epochs):
  sess.run(init, feed_dict={x_: train_in, y_: train_out})


# This script scans thru sgcarmart_by_generations
# Then create folder year_model

import os
import re
import regex
import errno, os, stat, shutil

path = "D:/xrvision/xrvision_dataset/sgcarmart_by_generations"
new_path = "D:/xrvision/xrvision_dataset/sgcarmart_gen"
os.chdir(path)

def handleRemoveReadonly(func, path, exc):
  excvalue = exc[1]
  if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
      os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
      func(path)
  else:
      raise

for subdirs, dirss, files in os.walk(path):
    for dirs in dirss:
        print(subdirs + "/" + dirs)
        print(subdirs)
        print(dirs + "_" + subdirs[len(path)+1:])
        new_gen = (dirs + "_" + subdirs[len(path)+1:])
        new_gen_path = (new_path + "/" + new_gen)
        if not os.path.exists(new_gen_path):
            os.makedirs(new_gen_path)

#        try:
#            data = int(input("Enter a number: "))
#        except:
#
#            data = 10
#
#        dir_split = (dirs.split())
#        print(dir_split)
#        new_dir = (" ".join(dir_split[:data]))
#        print(new_dir)
#        if not os.path.exists(new_dir):
#            os.makedirs(new_dir)
#
#
#        for subdirs, dirss, files in os.walk(dirs):
#            for file in files:
#                file_short = new_dir + "/" + file
#                file_long = dirs + "/" + file
#                print("file_short" +  file_short)
#                print("file_long " + file_long)
#                # moves files to short dir
#                os.rename(file_long,file_short)
#
#        # Delete empty directories
#        if not os.listdir(dirs) :
#            print(dirs)
#            print("Directory is empty")
#            shutil.rmtree(dirs, ignore_errors=False, onerror=handleRemoveReadonly)

os.chdir(path)##


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /home/machin/.virtualenvs/twine/lib/python2.7/site-packages/gerritclient/tests/unit/common/test_utils.py
# Compiled at: 2017-04-21 23:53:12
from oslotest import base as oslo_base
from gerritclient.common import utils

class TestUtils(oslo_base.BaseTestCase):

    def test_get_display_data_single(self):
        columns = ('id', 'name')
        data = {'id': 1, 'name': 'test_name'}
        self.assertEqual(utils.get_display_data_single(fields=columns, data=data), [
         1, 'test_name'])

    def test_get_display_data_single_with_non_existent_field(self):
        columns = ('id', 'name', 'non-existent')
        data = {'id': 1, 'name': 'test_name'}
        self.assertEqual(utils.get_display_data_single(fields=columns, data=data), [
         1, 'test_name', None])
        return

    def test_get_display_data_multi_wo_sorting(self):
        columns = ('id', 'name')
        data = [{'id': 1, 'name': 'test_name_1'}, {'id': 2, 'name': 'test_name_2'}]
        self.assertEqual(utils.get_display_data_multi(fields=columns, data=data), [
         [
          1, 'test_name_1'], [2, 'test_name_2']])

    def test_get_display_data_multi_w_sorting(self):
        columns = ('id', 'name', 'severity_level')
        data = [{'id': 3, 'name': 'twitter', 'severity_level': 'error'}, {'id': 15, 'name': 'google', 'severity_level': 'warning'}, {'id': 2, 'name': 'amazon', 'severity_level': 'error'}, {'id': 17, 'name': 'facebook', 'severity_level': 'note'}]
        self.assertEqual(utils.get_display_data_multi(fields=columns, data=data, sort_by=[
         'name']), [
         [
          2, 'amazon', 'error'],
         [
          17, 'facebook', 'note'],
         [
          15, 'google', 'warning'],
         [
          3, 'twitter', 'error']])
        self.assertEqual(utils.get_display_data_multi(fields=columns, data=data, sort_by=[
         'severity_level', 'id']), [
         [
          2, 'amazon', 'error'],
         [
          3, 'twitter', 'error'],
         [
          17, 'facebook', 'note'],
         [
          15, 'google', 'warning']])


'''
Author       : ajin
Date         : 2020-04-04 19:24:59
Description  :
email        : ajin_w@163.com
那曾梦想屠龙的少年，终会变成油腻的中年大叔，端坐于显示器前，从指尖流淌的代码，终会改变整个世界
'''

class Field:
    pass

class CharField(Field):
    def __init__(self, db_column, max_length = None):
        self._value = None
        self.db_column =db_column
        if max_length is None:
            raise ValueError("must need string length")
        self.max_length = max_length

    def __get__(self, instance, owner):
        return self._value

    def __set__(self, instance, val):
        if not isinstance(val, str):
            raise ValueError("must need str")
        if len(val) > self.max_length:
            raise ValueError("string length is too long")
        self._value = val

class ModelMetaclass(type):
    def __new__(cls, name, bases, attrs, **kwargs):
        if name == "BaseModel":
            return super().__new__(cls, name, bases, attrs, **kwargs)
        fields = {}
        for key, val in attrs.items():
            if isinstance(val, Field):
                fields[key] = val
        attrs_meta = attrs.get("Meta", None)
        _meta = {}
        db_table = name.lower()
        if attrs_meta is not None:
            table = getattr(attrs_meta, "db_table", None)
            if table is not None:
                db_table = table
        _meta["db_table"] = db_table
        attrs["_meta"] = _meta
        attrs["fields"] = fields
        del attrs["Meta"]
        # 元类编程中必须返回所以参数
        return super().__new__(cls, name, bases, attrs, **kwargs)

class BaseModel(metaclass=ModelMetaclass):
    def __init__(self, *args, **kwargs):
        for key, val in kwargs.items():
            setattr(self, key, val)
        return super().__init__()

    def save(self):
        field = []
        values = []
        for key, val in self.fields.items():
            db_column = val.db_column
            if db_column is None:
                db_column = key.lower()
            field.append(db_column)
            value = getattr(self, key)
            values.append(str(value))
        sql = "insert {db_table}({field}) values({values})".format(db_table = self._meta["db_table"], field = ",".join(field), values=",".join(values))
        print(sql)
class User(BaseModel):
    name = CharField(db_column="name", max_length=10)

    class Meta:
        db_table = "user"

if __name__ == "__main__":
    user = User(name = "ajin")
    user.name = "ajin"
    user.save()


'''
Created on Oct 1, 2015

@author: rawas
'''
import numpy as np
import scipy.misc
from matplotlib import pyplot as plt
import scipy.stats as ss
from numpy import ndarray

im0 = scipy.misc.imread('view0.png',1)
im1 = scipy.misc.imread('view1.png',1)

def corr(sl1,bx0):
    correlation_data = []
    m = np.mean(sl1)
    for x in range(0,sl1.shape[0]-19,1):
        bx1 = sl1[x:(x+20)]
        sum11 = sum(bx0*bx0)
        sum22 = sum(bx1*bx1)
        sum12 = sum(bx1*bx0)
        correlation = sum12 / (sum11 * sum22)**0.5
        correlation = correlation
        correlation_data.append(correlation)
    return(correlation_data)

def corrWithEdgeEffects(nlag, array1, array2, slit1):
    if (len(array1.shape) != 1 or len(array2.shape)!= 1):
        raise Exception("array1 and array2 are not 1D")

    if (len(array1.shape) != len(array2.shape)):
        raise Exception("array1 and array2 are not the same length")

    # array of lags over which we will compute correlation
    lags = np.array(range(-nlag, nlag + 1))
    print(lags)

    # output array of correlation
    n = array1.shape[0]
    m = lags.shape[0]

    corr = np.zeros(m)

    for klag in range(0, m):

        sum11 = 0
        sum22 = 0
        sum12 = 0

        for j in range(0, n):
            jlag = j + lags[klag]

            if (jlag >= 0 and jlag < n):
                sum11 += array1[j]    * array1[j]
                sum22 += array1[jlag] * array1[jlag]
                sum12 += array1[j]    * array2[jlag]

        print(sum11,sum22,sum12)

        corr[klag] = sum12 / (sum11 * sum22)**0.5
#         print("%5d %5d %5d %5d %5d %8.4f %8.4f %8.4f %8.4f" %
#               (lags[klag], j, jlag, n, m, sum11, sum22, sum12, corr[klag]))
    return(corr)

im0_small = scipy.misc.imresize(im0, 0.6, interp='bicubic', mode=None).astype(float)
im1_small = scipy.misc.imresize(im1, 0.6, interp='bicubic', mode=None).astype(float)

nrow = im1_small.shape[0]
ncol = im1_small.shape[1]

# compute and subtract the mean
mean0 = np.mean(im0_small)
mean1 = np.mean(im1_small)

im0_small -= mean0
im1_small -= mean1

i0 = im0_small[100][10:210]
i1 = im1_small[100][20:220]
slit1 = im0_small[100]
slit2 = im1_small[100]

print(i0)
print(i1)
nlag = 100
corr = corrWithEdgeEffects(nlag, i0, i1, slit1)
corr2 = corrWithEdgeEffects(nlag, i1, i0, slit2)

print(np.nonzero(corr == max(corr))[0][0])

plt.subplot(2,2,1)
plt.plot(corr)
plt.subplot(2,2,2)
plt.plot(corr2)
plt.subplot(2,2,3)
plt.imshow(im0_small)
plt.subplot(2,2,4)
plt.imshow(im1_small)
plt.show()

# def findSlit(im0,im1,y1):
#     bup = im0.shape[0]-y1
#     sl0 = im0[bup]
#     sl1 = im1[bup]
#     distances = []
#     for x in range(0,sl0.shape[0]-19):
#         bx0 = sl0[x:(x+20)]
#         correl = corr(sl1,bx0)
#         match = 1*correl.index(max(correl))
#         distances.append(x-match)
#     return(distances)

# def corrWithEdgeEffectsDotNP(nlag, array1, array2):
#     if (len(array1.shape) != 1 or len(array2.shape)!= 1):
#         raise Exception("array1 and array2 are not 1D")
#
#     if (len(array1.shape) != len(array2.shape)):
#         raise Exception("array1 and array2 are not the same length")
#
#     # array of lags over which we will compute correlation
#     lags = np.array(range(-nlag, nlag + 1))
#     print(lags)
#
#     # output array of correlation
#     n = array1.shape[0]
#     m = lags.shape[0]
#
#     corr = np.zeros(m)
#     zeros = [0 for x in range((len(lags)-1)/2)]
#     array2PlusZerosOnEnds = zeros+list(array2)+zeros
#
#     for klag in range(0, m):
#         start = (len(lags)-1)/2 + lags[klag]
#         end = n+(len(lags)-1)/2 + lags[klag]
#         array2 = np.array(array2PlusZerosOnEnds[start:end])
#         sum11 = sum(array1*array1)
#         sum22 = sum(array2*array2)
#         sum12 = sum(array1*array2)
#
#         print(sum11,sum22,sum12)
#
#         corr[klag] = sum12 / (sum11 * sum22)**0.5
# #         print("%5d %5d %5d %5d %5d %8.4f %8.4f %8.4f %8.4f" %
# #               (lags[klag], j, jlag, n, m, sum11, sum22, sum12, corr[klag]))
#     return(corr)


import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
X = pd.read_excel()
y = pd.read_excel()
def multitask_lasso(X, y, lr = 0.1, alpha = 0.01, max_iter = 100, min_gap = 0.001, normalize = False, tempotal_smooth = True):
    loss_train_record = []
    loss_dev_record = []
    weights = np.zeros((y.shape[1], X.shape[1]))
    #Set necessary factors

    if normalize == True:
        X = (X - X.mean())/X.std()
    X = np.array(X)
    y = np.array(y)
    #Data pre-processing

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
    X_train, X_dev, y_train, y_dev = train_test_split(X_train, y_train, test_size = 0.3)
    # Randomly split the dataset

    for iter in range(max_iter):
        if tempotal_smooth == True:
            weights = weights - lr * np.dot(np.dot(X_train, weights.T) - y_train, X_train) - 2 * lr * alpha * weights
            loss_train = ((y_train - np.dot(X_train, weights.T)) ** 2).sum(axis = 0) + alpha * (weights ** 2).sum()
            loss_dev = ((y_dev - np.dot(X_dev, weights.T)) ** 2).sum(axis = 0) + alpha * (weights ** 2).sum()
            #Need to add Temporal Smoothness Prior
        else:
            weights = weights - lr * np.dot(np.dot(X_train, weights.T) - y_train, X_train) - 2 * lr * alpha * weights
            loss_train = ((y_train - np.dot(X_train, weights.T)) ** 2).sum(axis = 0) + alpha * (weights ** 2).sum()
            loss_dev = ((y_dev - np.dot(X_dev, weights.T)) ** 2).sum(axis = 0) + alpha * (weights ** 2).sum()
            #Realize min ||XW−Y||^2 + θ1||W||^2
        loss_train_record.append(loss_train)
        loss_dev_record.append(loss_dev)
        if iter != 0:
            if (loss_dev_record[iter] - loss_dev_record[iter - 1]) < min_gap:
                break
    #Train the Lasso Regression
    return weights, X_test, y_test

weights, X_test, y_test = multitask_lasso(X, y, lr = 0.1, alpha = 0.01, max_iter = 100, min_gap = 0.001, normalize = False, tempotal_smooth = True)
prediction = np.dot(X_test, weights.T)
#Need to add evaluation methods


#Faça um programa que verifique se um número é perfeito ou não.
#Um número é dito perfeito quando ele é igual a soma
#dos seus divisores excetuando ele próprio.
#(Ex: 6 é perfeito, 6 = 1 + 2 + 3, que são seus divisores).
#Exiba uma mensagem na tela informando se o número é ou não perfeito.

cont="S"
while cont=="S" or cont=="s":
    n=int(input("Número"))
    soma=0
    for i in range (1,n):
        if n % i == 0:
            soma=soma+1
    if n == soma:
        print(n,"É um número perfeito")
    else:
        print(n,"Não é um número perfeito")
    cont=str(input("Deseja continuar (S/N)?"))

Pacote para dowloand/Python/ex007( while e for) verificando se é um número perfeito ou não.py


from harvester import config
from jinja2 import Template
import os
from harvester.logs import logger
import pkg_resources

class HarvestedSource:
    """
    Analize all data from a particular previously harvested source
    """
    name = None  # source name (and folder name)

    data_json = None  # data.json dict
    results = None  # harvest results
    data_json_validation_errors = None

    final_results = {}  # all files processed

    def __init__(self, name):
        config.SOURCE_NAME = name
        self.name = name
        data = config.get_report_files()
        self.data_json = data['data_json']
        self.results = data['results']
        self.data_json_validation_errors = data['data_json_validation_errors']

    def process_results(self):

        # analyze results

        actions = {}  # create | delete | update
        validation_errors = []
        action_errors = []
        action_warnings = []
        # print(f'Result: {self.results}')
        if type(self.results) != list:
            logger.error(f'Unexpected results: {self.results}')
            return False

        for result in self.results:

            # print(f'Result: {result}')
            comparison_results = result.get('comparison_results', None)
            if comparison_results is None:
                # this is bad. This source is broken
                return False
            action = comparison_results['action']
            if action not in actions.keys():
                actions[action] = {'total': 0, 'success': 0, 'fails': 0}
            actions[action]['total'] += 1

            if action in ['create', 'update']:  # delete has no new_data
                if len(comparison_results['new_data'].get('validation_errors', [])) > 0:
                    validation_errors += comparison_results['new_data']['validation_errors']

            action_results = comparison_results.get('action_results', {})
            success = action_results.get('success', False)
            if success:
                actions[action]['success'] += 1
            else:
                actions[action]['fails'] += 1

            action_warnings += action_results.get('warnings', [])
            action_errors += action_results.get('errors', [])

        self.final_results['actions'] = actions
        self.final_results['validation_errors'] = validation_errors
        self.final_results['action_warnings'] = action_warnings
        self.final_results['action_errors'] = action_errors

        return True

    def get_json_data(self):
        data = {
            'name': self.name,
            'data_json': self.data_json,
            'results': self.results,
            'data_json_validation_errors': self.data_json_validation_errors,
            'actions': self.final_results['actions'],
            'validation_errors': self.final_results['validation_errors'],
            'action_warnings': self.final_results['action_warnings'],
            'action_errors': self.final_results['action_errors']
        }
        return data

    def render_template(self, save=True):
        # redenr through harvest-report.html
        context = self.get_json_data()
        # fails (?) template_txt = pkg_resources.resource_string('harvester', 'templates/harvest-report.html')
        # https://stackoverflow.com/questions/6028000/how-to-read-a-static-file-from-inside-a-python-package
        template_path = pkg_resources.resource_filename('harvester', 'templates/harvest-report.html')
        f = open(template_path, 'r')
        template = Template(f.read())
        f.close()
        html = template.render(**context)
        if save:
            report_path = config.get_html_report_path()
            self.save_report(html=html, report_path=report_path)
            logger.info(f'Saved report to {report_path}')

        return html

    def save_report(self, html, report_path):
        f = open(report_path, 'w')
        f.write(html)
        f.close()

class HarvestedSources:
    """
    Analize ALL harvested sources. Iterate and process all
    """
    base_folder = None
    all_data = []  # one row per harvest source
    summary_data = {'harvest_sources_readed': 0,
                    'harvest_sources_failed': 0,
                    'total_data_json_datasets': 0,

                    }

    def __init__(self, base_folder=None):
        self.base_folder = config.DATA_FOLDER_PATH if base_folder is None else base_folder

    def process_all(self):

        logger.info(f'Inspecting {self.base_folder} folder')
        for subdir, dirs, files in os.walk(self.base_folder):
            for name in dirs:
                if name == 'harvest_sources':
                    continue
                logger.info(f'Processing {name} folder')
                self.summary_data['harvest_sources_readed'] += 1

                hs = HarvestedSource(name=name)
                ret = hs.process_results()
                if not ret:
                    self.summary_data['harvest_sources_failed'] += 1
                    continue
                hs.render_template(save=True)

                data = hs.get_json_data()
                self.all_data.append(data)

                if type(data['data_json']) == list:
                    datasets = []
                    logger.error(f'{name}: Data JSON Source is a list. Must be a dict')
                if type(data['data_json']) == dict:
                    datasets = data['data_json'].get('dataset', [])
                if len(datasets) == 0:
                    logger.error(f'Source with 0 datasets {name}')
                self.summary_data['total_data_json_datasets'] += len(datasets)
                logger.info(' - Total datasets: {}'.format(self.summary_data['total_data_json_datasets']))

        harvest_sources_readed = self.summary_data['harvest_sources_readed']
        harvest_sources_failed = self.summary_data['harvest_sources_failed']
        total_data_json_datasets = self.summary_data['total_data_json_datasets']
        logger.info('''**************
                        Harvest sources readed: {}
                        Harvest sources failed: {}
                        Total datasets: {}'''.format(harvest_sources_readed,
                                                     harvest_sources_failed,
                                                     total_data_json_datasets))


# -*- coding: utf-8 -*-
"""
Created on Sun Jan 24 16:46:51 2021

@author: Asus
"""
import pandas as pd
from kmodes.kprototypes import KPrototypes

class kPrototypes():

    def train_kProto(self):

        data = pd.read_csv("drug_gene_disease.csv")
        #data.drop(columns=["Unnamed: 0"], inplace=True)

        data_copy = data.copy()
        data_copy.drop(columns = ["Drug ID","Disease ID"], inplace = True)

# eğitime sokmak için ilaç, hastalık, gen ve skor değerleri array'e atılır

        tArray = data_copy.values
        tArray[:,2] = tArray[:,2].astype(float).round(2)

#eğitim

        kproto = KPrototypes(verbose = 2, max_iter = 20) # default küme sayısı 8'dir
        clusters = kproto.fit_predict(tArray, categorical=[0,1,3]) # kategorik değerlerin olduğu sütunlar belirtilir

#  küme değeri bilgisi sütun olarak tabloya eklenir

        print(kproto.cluster_centroids_)

        cluster_array = []

        for c in clusters:
            cluster_array.append(c)

        data['cluster'] = cluster_array
        data_copy['cluster'] = cluster_array

# eğitilmiş verinin lokale kaydedilmesi

        data.to_csv('trained_kPrototypes.csv', index = False)
        data.to_excel('trained_kPrototypes.xlsx', index = False)
        data_copy.to_excel('trained_kPrototypes(1).xlsx', index = False)


from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from kivy.uix.widget import Widget
from Viewer_Pop_up import Viewer
from functools import partial

class inputButton(Button):
    '''This Button class just sets the background to the desired images'''
    def __init__(self,**kwargs):
        super(inputButton,self).__init__()

        if 'max' in kwargs:
            self.max = kwargs['max']
        else:
            self.max = 1000000000
        if min in kwargs:
            self.min = kwargs['min']
        else:
            self.min = 0

        self.text = '0'
        self.popUpBoxLayout = BoxLayout(orientation = 'vertical')

        self.popUpGrid = GridLayout(cols = 0, rows = 2)

        self.popUpTextBox = BoxLayout(size_hint = (1,0.2))
        self.popUpTextInput = TextInput(font_size = 35,multiline=False)
        self.popUpTextBox.add_widget(self.popUpTextInput)

        self.popUpButtonBox = BoxLayout(size_hint = (1,0.6))
        self.popUpConfirmBox = BoxLayout(size_hint = (1,0.2))

        #Number Grid
        self.numberGrid = GridLayout(rows = 4, cols = 3)
        for i in [1,2,3,4,5,6,7,8,9]:
            numberButton = Button(text = str(i))
            numberButton.bind(on_press = self.updateText)
            self.numberGrid.add_widget(numberButton)

        self.clear_button = Button(text ='Clear')
        self.clear_button.bind(on_press = self.updateText)
        self.numberGrid.add_widget(self.clear_button)

        #adding in 0 to the bottom row
        numberButton = Button(text = '0')
        numberButton.bind(on_press = self.updateText)
        self.numberGrid.add_widget(numberButton)

        #adding in the decimal Character
        self.decimalButton = Button(text ='.')
        self.decimalButton.bind(on_press = self.updateText)
        self.numberGrid.add_widget(self.decimalButton)

        #Adding the confirm button
        self.popUpButton = Button(text = 'Confirm')
        self.popUpConfirmBox.add_widget(self.popUpButton)

        self.popUpButtonBox.add_widget(self.numberGrid)

        #self.popUpGrid.add_widget(self.popUpTextInput)
        #self.popUpGrid.add_widget(self.popUpButton)

        self.popUpBoxLayout.add_widget(self.popUpTextBox)
        self.popUpBoxLayout.add_widget(self.popUpButtonBox)
        self.popUpBoxLayout.add_widget(self.popUpConfirmBox)

        self.popupField = Popup(title='Test popup',
        content=self.popUpBoxLayout,size_hint=(None, None), size=(400, 600),
                                auto_dismiss = False)

        self.popupField.bind(on_open = self.focusText)
        self.popupField.bind(on_dismiss = self.changeText)
        self.popUpButton.bind(on_press = self.popupField.dismiss)

        self.bind(on_press = partial(self.openPopup))

        self.wasUpdated = False
        self.popUpOpened = False

    def updateText(self,button):
        if button.text == 'Clear':
            self.popUpTextInput.text = ''
        else:
            self.popUpTextInput.text = self.popUpTextInput.text + button.text

    def getText(self):
        return self.text

    def focusText(self,arg):
        self.popUpTextInput.focus = True

    def openPopup(self,arg):
        self.popUpTextInput.text = self.text
        self.popUpOpened = True
        self.popupField.open()

    def getMax(self):
        return self.max

    def setMinMax(self,**kwargs):

        if 'max' in kwargs:
            self.max = kwargs['max']
        if 'min' in kwargs:
            self.min = kwargs['min']

    def changeText(self,arg):

        #sets a flag so that the main loop can see something was changed and it needs to act
        self.wasUpdated = True

        #make sure that the user is inputting a number
        try:
            a = float(self.popUpTextInput.text)
            if a > self.max:
                self.text = str(self.max)
            elif a < self.min:
                self.text = '0'
            else:
                self.text = self.popUpTextInput.text

        except ValueError:
            self.wasUpdated = False
            pass


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

"""
based on https://github.com/ISakony/NEC_chainerrl_CartPole-v0
"""

from __future__ import (
    division,
    print_function,
    unicode_literals,
    absolute_import
)
from builtins import *  # noqa
from future import standard_library

import chainer
import chainer.functions as F
import chainer.links as L
import numpy as np
import pickle

from chainer import cuda
from chainer.functions.loss import mean_squared_error
from sklearn.neighbors.kd_tree import KDTree

from ml.memory import (
    D_memory,
    Ma_memory,
    N_step_memory
)

standard_library.install_aliases()

def get_greedy_actions(q_values):
    # substitute function of chainerrl.DiscreteActionValue.greedy_actions()
    # see also http://chainerrl.readthedocs.io/en/latest/_modules/chainerrl/action_value.html#DiscreteActionValue
    return chainer.Variable(q_values.data.argmax(axis=1).astype(np.int32))

def select_action_epsilon_greedily(epsilon, random_action_func,
                                   greedy_action_func):
    if np.random.rand() < epsilon:
        return random_action_func(), False
    else:
        return greedy_action_func(), True

class LinearDecayEpsilonGreedy(object):
    """Epsilon-greedy with linearyly decayed epsilon

    Args:
      start_epsilon: max value of epsilon
      end_epsilon: min value of epsilon
      decay_steps: how many steps it takes for epsilon to decay
      random_action_func: function with no argument that returns action
      logger: logger used
    """

    def __init__(self, start_epsilon, end_epsilon,
                 decay_steps, random_action_func):
        assert start_epsilon >= 0 and start_epsilon <= 1
        assert end_epsilon >= 0 and end_epsilon <= 1
        assert decay_steps >= 0
        self.start_epsilon = start_epsilon
        self.end_epsilon = end_epsilon
        self.decay_steps = decay_steps
        self.random_action_func = random_action_func
        self.epsilon = start_epsilon

    def compute_epsilon(self, t):
        if t > self.decay_steps:
            return self.end_epsilon
        else:
            epsilon_diff = self.end_epsilon - self.start_epsilon
            return self.start_epsilon + epsilon_diff * (t / self.decay_steps)

    def select_action(self, t, greedy_action_func, action_value=None):
        self.epsilon = self.compute_epsilon(t)
        a, greedy = select_action_epsilon_greedily(
            self.epsilon, self.random_action_func, greedy_action_func)
        # greedy_str = 'greedy' if greedy else 'non-greedy'
        return a

class QFunction(chainer.Chain):
    def __init__(self, use_gpu, input_dim, embedding_dim, n_actions, n_hidden_channels=64):
        self.use_gpu = use_gpu
        self.q_list = np.ndarray((1, n_actions), dtype=np.float32)  # for discreteActionValue
        super(QFunction, self).__init__(
            l0=L.Linear(input_dim, n_hidden_channels),
            l1=L.Linear(n_hidden_channels, n_hidden_channels),
            l2=L.Linear(n_hidden_channels, embedding_dim))

    def __call__(self, x, ma):
        h = F.tanh(self.l0(x))
        h = F.tanh(self.l1(h))
        h = F.tanh(self.l2(h))

        # kd_tree
        q_train = []  # for train [variable,variable]
        ind_list = []  # for train
        dist_list = []  # for train
        for j in range(len(ma.maq)):  # loop n_actions
            h_list = ma.mah[j]
            lp = len(h_list)
            leaf_size = lp + (lp / 2)

            tree = KDTree(h_list, leaf_size=leaf_size)
            h_ = h.data

            if lp < 50:
                k = lp
            else:
                k = 50
            dist, ind = tree.query(h_, k=k)

            mahi = ma.mah[j][ind[0]]
            hi = chainer.Variable(cuda.to_cpu(mahi))
            tiled_h = chainer.Variable(np.tile(h.data, (len(ind[0]), 1)))
            wi = F.expand_dims(1 / (F.sqrt(F.sum((tiled_h - hi) * (tiled_h - hi), axis=1) + 1e-3)), 1)
            w = F.sum(wi, axis=0)
            maqi = ma.maq[j][ind[0]]
            q = chainer.Variable(cuda.to_cpu(maqi))
            qq = F.expand_dims(F.sum(wi * q, axis=0) / w, 1)

            q_train.append(qq)
            ind_list.append(ind)
            dist_list.append(dist)

            self.q_list[0][j] = qq.data
        if self.use_gpu:
            qa = chainer.Variable(cuda.to_cpu(self.q_list))
        else:
            qa = self.q_list

        return qa, q_train, ind_list, dist_list, h.data
        # return chainerrl.action_value.DiscreteActionValue(qa), q_train, ind_list, dist_list, h.data

class NEC(object):
    def __init__(self, use_gpu, q_function, optimizer, batch_size, gamma, explorer, n_actions, retrain, embedding_dim):
        self.model = q_function
        self.n_actions = n_actions
        self.N_horizon = 100
        self.phi = lambda x: x
        self.update_frequency = 1
        self.minibatch_size = batch_size
        self.size_of_memory = 100000  # 5*10^5
        self.embedding_dim = embedding_dim

        if not retrain:
            self.ma_memory = Ma_memory(n_actions, self.size_of_memory, self.embedding_dim)
        else:
            self.ma_memory = Ma_memory(n_actions, self.size_of_memory, self.embedding_dim)
            self.ma_memory.maq = pickle.load(open("Ma_memory_maq_.pickle", "rb"))
            self.ma_memory.mah = pickle.load(open("Ma_memory_mah_.pickle", "rb"))

        self.d_memory = D_memory(self.size_of_memory)
        self.alpha = 0.1  # learning late
        self.n_step_memory = N_step_memory(gamma, self.N_horizon, self.ma_memory,
                                           self.d_memory, self.alpha)
        self.optimizer = optimizer
        self.gamma = gamma  # 0.99
        self.explorer = explorer
        self.n_step = 0
        self.q_ontime = None  #
        self.q_target = None
        self.t = 0
        self.average_loss_all = np.array(0.0).astype('f')
        self.average_loss_c = np.array(0.0).astype('f')
        self.average_loss = np.array(0.0).astype('f')
        self.use_gpu = use_gpu

    def act(self, state, episode):  #
        self.n_step += 1
        if self.n_step == self.N_horizon + 1:
            self.n_step = 1

        # action
        with chainer.no_backprop_mode():
            action_value = self.model(F.expand_dims(state, 0), self.ma_memory)

        greedy_action = cuda.to_cpu(get_greedy_actions(action_value[0]).data)[0]

        if episode < 1:
            action = np.random.randint(0, self.n_actions)
        else:
            action = self.explorer.select_action(
                self.t, lambda: greedy_action, action_value=action_value[0])
        self.t += 1

        # print(action_value[1])
        self.q_ontime = cuda.to_cpu(action_value[1][greedy_action].data)

        self.last_action = action
        ind = action_value[2]
        dist = action_value[3]
        key = cuda.to_cpu(action_value[4])

        return self.last_action, ind, dist, key

    def append_memory_and_train(self, state, action, ind, dist, reward, key, done, episode):
        t_step = 1
        self.n_step_memory.add_replace_memory(state, action, key, reward,
                                              t_step, done, self.q_ontime, dist, ind)

        if len(self.d_memory.mem) < self.minibatch_size:
            n_loop = len(self.d_memory.mem)
        else:
            n_loop = self.minibatch_size

        if episode < 1:
            return

        if self.t % 4 == 0:  # 16
            loss = np.array(0.0).astype('f')
            for ii in range(n_loop):
                rnd = np.random.randint(len(self.d_memory.mem))
                obs, action, t_q, t_ind = self.n_step_memory.d_memory.mem[rnd]
                obs = chainer.Variable(cuda.to_cpu(obs))
                t_q = chainer.Variable(cuda.to_cpu(t_q))
                tav = self.model(F.expand_dims(obs, 0), self.n_step_memory.ma_memory)
                greedy_action = cuda.to_cpu(get_greedy_actions(tav[0]).data)[0]
                train_q = tav[1][greedy_action]

                loss += mean_squared_error.mean_squared_error(train_q, t_q)

                self.average_loss_all += loss
                self.average_loss_c += 1.0
                self.average_loss = self.average_loss_all / self.average_loss_c

            if n_loop != 0:
                loss /= n_loop
                self.model.zerograds()
                loss.backward()
                self.optimizer.update()

    def stop_episode_and_train(self, state, reward, done=False):
        """Observe a terminal state and a reward.

        This function must be called once when an episode terminates.
        """
        assert self.last_action is not None

        # Add a transition to d_memory
        self.stop_episode()

    def stop_episode(self):
        self.last_action = None
        self.q_ontime = None
        self.n_step = 0
        self.average_loss = 0.0
        self.average_loss_all = 0.0
        self.average_loss_c = 0.0

    def get_statistics(self):
        return [
            ('Ma', "q", len(self.n_step_memory.ma_memory.maq[0]), len(self.n_step_memory.ma_memory.maq[1])),
            ('D', len(self.n_step_memory.d_memory.mem)),
        ]


import re

class Lexer(object):
    def __init__(self, data):
        self.data = data

    def tokenize(self):
        # list where all the tokens will go to
        tokens = []

        # variable to split the words in the source code
        data = self.data.split()

        # index to keep track of source code
        pos = 0

        # loop through each word in the source code
        while pos < len(data):
            text = data[pos]
            # recognize the type of data being sent as a variable
            if text == "int":
                tokens.append(['INTEGER', text])
            elif text == "print":
                tokens.append(['METHOD', text])
            # recognize whether after variable declaration it is variable name or not
            elif re.match('[a-z]', text) or re.match('[A-Z]', text):
                if text[len(text) - 1] == ";":
                    tokens.append(['IDENTIFIER', text[0:len(text) - 1]])
                else:
                    tokens.append(['IDENTIFIER', text])
            # matches if it is a number, then identify it as a constant
            elif re.match('[0-9]', text):
                if text[len(text) - 1] == ";":
                    tokens.append(['CONSTANT', text[0:len(text) - 1]])
                else:
                    tokens.append(['IDENTIFIER', text])
            # recognizes if the source code has an OPERATOR
            elif text in "*/=+-":
                tokens.append(['OPERATOR', text])
            # if a statement ends with a (;) then add END OF STATEMENT (EOS) to tokens
            if text[len(text) - 1] == ";":
                tokens.append(['EOS', ';'])

            pos += 1
        print(tokens)

        return tokens


import muq.Modeling as mm
import numpy as np

class SimpleModel(mm.PyModPiece):

    def __init__(self, numPts):
        super(SimpleModel,self).__init__([numPts,2],[numPts])

    def EvaluateImpl(self, inputs):
        x,c  = inputs

        y = c[0]*x + c[1]

        self.outputs = [y]

    def JacobianImpl(self, outWrt, inWrt, inputs):
        x,c = inputs

        # Jacobian wrt x
        if(inWrt==0):
            self.jacobian = c[0]*np.eye(x.shape[0])

        # Jacobian wrt c
        else:
            self.jacobian =np.ones((self.outputSizes[0], self.inputSizes[inWrt]))
            self.jacobian[:,0] = x

    def GradientImpl(self, outWrt, inWrt, inputs, sens):
        x, c = inputs

        # Gradient wrt x
        if(inWrt==0):
            self.gradient = c[0] * sens

        # Gradient wrt c
        else:
            self.gradient = np.array([x.dot(sens),sens.sum()])

    def ApplyJacobianImpl(outWrt, inWrt, inputs, vec):
        x,c = inputs

        # Jacobian wrt x
        if(inWrt==0):
            self.jacobianAction = c[0]*vec

        # Jacobian wrt c
        else:
            self.jacobianAction = vec[0]*x + vec[1]*np.ones(x.shape[0])

    def ApplyHessianImpl(outWrt, inWrt1, inWrt2, inputs, sens, vec):
        x,c = inputs

        # Apply d^2 / dxdc
        if((inWrt1==0)&(inWrt2==1)):
            hessAction = vec[0] * sens

        # Apply d^2 / dcdx
        elif((inWrt2==0)&(inWrt1==1)):
            hessAction = np.array([sens.dot(vec),0])

        # Apply d^2 / dxds
        elif((inWrt1==0)&(inWrt2==2)):
            hessAction = c[0]*vec

        # Apply d^2 / dcds
        elif((inWrt1==1)&(inWrt2==2)):
            hessAction = np.array([x.dot(vec), vec.sum()])

        # Apply d^2/dx^2  or  d^2/dc^2  or  d^2/ds^2 or d^2 / dsdx or  d^2 / dsdc
        else:
            hessAction = np.zeros(self.inputSizes[inWrt1])

numPts = 10
x = np.random.randn(numPts)
c = np.array([1.0,0.5])

mod = SimpleModel(numPts)

y = mod.Evaluate(x,c)[0]

print('c = ', c)
print('x = ', x)
print('y = ', y)


#!/usr/bin/python3
import os, sys
utilpath = sys.path[0] + "/../util/"
sys.path.append(utilpath)

from gtp_connection import GtpConnection
from board_util import GoBoardUtil
from simple_board import SimpleGoBoard
from ucb import runUcb
import numpy as np
import argparse
import sys
import copy

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--simulations', type=str, default='random', help='type of simulation policy: random or rulebased')
parser.add_argument('--movefilter', action='store_true', default=False, help='whether use move filter or not')

args = parser.parse_args()
simulations = args.simulations
move_filter = args.movefilter

class PolicyPlayer(object):
    """
    Flat Monte Carlo implementation that uses simulation for finding the best child of a given node
    """

    version = 0.22
    name = "PolicyPlayer"
    def __init__(self):
        self.random_simulation = True if simulations == 'random' else False
        self.use_pattern = not self.random_simulation
        self.check_selfatari = move_filter

    def get_move(self, board, toplay):
        return GoBoardUtil.generate_move_with_filter(board, self.use_pattern, self.check_selfatari)

    def get_properties(self):
        return dict(
            version=self.version,
            name=self.__class__.__name__,
        )

def run():
    """
    start the gtp connection and wait for commands.
    """
    board = SimpleGoBoard(7)
    con = GtpConnection(PolicyPlayer(), board)
    con.start_connection()

if __name__=='__main__':
    run()