from GUI.SetsTreeWidget import SetsTreeWidget
from Updating.MagicCardsInfoParser import MagicCardsInfoParser

from PyQt5 import QtCore, QtGui, QtWidgets

try:
    _fromUtf8 = lambda s: s
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtWidgets.QApplication.UnicodeUTF8

    def _translate(context, text, disambig):
        return QtWidgets.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtWidgets.QApplication.translate(context, text, disambig)

class UpdateDialog(QtWidgets.QDialog):
    """
    Largely generated dialog to to update the program database and download images
    online.
    """

    #-------------------------------------------------------------------------------
    def __init__(self, parent, mainframeParent):
        """Additional initializer."""

        super(UpdateDialog, self).__init__(parent)

        #------------------------------------
        self._mainframeParent = mainframeParent
        self._db = self._mainframeParent.getDatabase()
        self._parser = None

        #------------------------------------
        self.setupUi(self)
        self.__setup()
        self.show()

#==========================================================================#
#                              Private                                     #
#==========================================================================#
#-------------------------------------------------------------------------------
    def __setup(self):
        """
        Sets up the dialog GUI.
        """

        self.__connectEvents()
        self.textEdit.setText(self._mainframeParent.getSetting('picsfolder'))

#-------------------------------------------------------------------------------
    def _checkForNewSets(self, dbSetNames, onlineSetNames):
        """
        Compares the set names found online and the set names in the database
        and returns those which the database doesn't have.
        """

        return set(onlineSetNames) - set(dbSetNames)

#-------------------------------------------------------------------------------
    def _connect(self):
        """
        Connects to gatherer and magiccards.info, and checks is for
        updates.
        """

        self._parser = MagicCardsInfoParser()

        if not self._parser.checkConnection():
            title = "Error in connection"
            msg = "Magiccards.info is offline or cannot connect to internet"
            QtWidgets.QMessageBox.warning(self, title, msg)
            return

        onlineMtgSetNames = self._parser.getMtgSetNames()
        self.setsTreeWidget.addMtgSets(onlineMtgSetNames)

        dbSetNames = self._db.getMtgSetNames()

        newSetNames = self._checkForNewSets(dbSetNames,
                                            onlineMtgSetNames['All'])
        self.setsTreeWidget.markNewMtgSets(newSetNames)

        self.connectButton.setEnabled(False)

        text = "Connected"
        self.magiccardsConnectionLabel.setText(text)

#-------------------------------------------------------------------------------
    def _updateChosenSets(self):
        """
        Updates the database with the sets chosen in the tree widget.
        """

        downloadedSets = []
        chosenItems = self.setsTreeWidget.getCheckedItems()

        if not chosenItems:
            return

        self._resetProgress()

        current = 1
        total = len(chosenItems)

        self.progressBar.setMaximum(total)
        self.progressBar_2.setMaximum(total + int(round(0.1 * total)))

        for chosenItem in chosenItems:
            currentProgressText = 'prossessing Set {current} out of {total}'
            currentProgressText = currentProgressText.format(current=current,
                                                            total=total)
            overallProgressText = 'Prossessing sets'
            self.label_7.setText(currentProgressText)
            self.label_8.setText(overallProgressText)

            self.progressBar.setValue(current - 1)
            self.progressBar_2.setValue(current - 1)

            downloadedSets.append(self._parser.getMtgSet(str(chosenItem.text(0))))

            current += 1

        currentProgressText = 'Updating Database'
        overallProgressText = 'Prossessing sets'
        self.label_7.setText(currentProgressText)
        self.label_8.setText(overallProgressText)

        self.progressBar.setValue(0)
        self.progressBar_2.setValue(current - 1)

        for mtgSet in downloadedSets:
            self._db.addSet(mtgSet)

        self._mainframeParent.saveDatabase(self._db)
        self._resetProgress()

#-------------------------------------------------------------------------------
    def _downloadChosenSetsImages(self):
        """
        Downloads the images of the sets chosen in the tree widget.
        """

        chosenItems = self.setsTreeWidget.getCheckedItems()

        if not chosenItems:
            return

        self._resetProgress()
        total = len(chosenItems)
        current = 0

        self.progressBar_2.setMaximum(total)

        for chosenItem in chosenItems:
            setName = str(chosenItem.text(0))
            setCode = str(chosenItem.text(1))
            destFolder = str(self.textEdit.toPlainText())

            overAllText = 'Downloading images of {setName} ({current} out of {total})'
            overAllText = overAllText.format(setName=setName,
                                             current=current,
                                             total=total)
            self.label_8.setText(overAllText)
            self.progressBar_2.setValue(current)
            self._parser.downloadMtgSetImages(setCode, destFolder,
                                              self.progressBar, self.label_7)

            QtCore.QCoreApplication.processEvents()

            current += 1

        QtCore.QCoreApplication.processEvents()
        overAllText = 'Done downloading images'
        overAllText = overAllText.format(setName=setName,
                                         current=current,
                                         total=total)
        self.label_8.setText(overAllText)
        self.progressBar_2.setValue(current)
        QtCore.QCoreApplication.processEvents()

#-------------------------------------------------------------------------------
    def _setImageFolder(self):
        """
        Sets up the image folder to be used for downloading images. Called
        when the directory chooser dialog button is clicked.
        """

        caption = 'Choose a folder for downloading images'
        folder = QtWidgets.QFileDialog().getExistingDirectory(self, caption)

        self.textEdit.setText(folder)

#-------------------------------------------------------------------------------
    def _resetProgress(self):
        """
        Resets progressbars back to starting values.
        """

        self.progressBar.setMaximum(100)
        self.progressBar_2.setMaximum(100)
        self.progressBar.setMinimum(0)
        self.progressBar_2.setMinimum(0)
        self.progressBar.setValue(0)
        self.progressBar_2.setValue(0)
        self.label_7.setText('-')
        self.label_8.setText('-')

#==========================================================================#
#                              Events                                      #
#==========================================================================#

#-------------------------------------------------------------------------------
    def __connectEvents(self):
        """Sets up the event handling."""

        self.doneButton.clicked.connect(self.close)
        self.connectButton.clicked.connect(self._connect)
        self.downloadImagesButton.clicked.connect(self._downloadChosenSetsImages)
        self.updateSetsButton.clicked.connect(self._updateChosenSets)
        self.toolButton.clicked.connect(self._setImageFolder)

        self.setsTreeWidget.itemChanged.connect(self.__itemChanged)
        self.comboBox.activated.connect(self.__comboBoxActivated)

#-------------------------------------------------------------------------------
    def __itemChanged(self, item, column):
        """Called when an item in the setsTreeWidget is changed."""

        if any(self.setsTreeWidget.getCheckedItems()):
            self.downloadImagesButton.setEnabled(True)
            self.updateSetsButton.setEnabled(True)

        else:
            self.downloadImagesButton.setEnabled(False)
            self.updateSetsButton.setEnabled(False)

#-------------------------------------------------------------------------------
    def __comboBoxActivated(self, index):
        """Called when an item in the combobox is chosen by the user."""

        if index ==  0:
            self.setsTreeWidget.unCheckAll()
        elif index ==  1:
            self.setsTreeWidget.unCheckAll()
            self.setsTreeWidget.checkAll()
        elif index ==  2:
            self.setsTreeWidget.unCheckAll()
            self.setsTreeWidget.checkNewMtgSets()
        elif index ==  3:
            self.setsTreeWidget.unCheckAll()
            self.setsTreeWidget.checkExpansions()
        elif index ==  4:
            self.setsTreeWidget.unCheckAll()
            self.setsTreeWidget.checkCoreSets()
        elif index ==  5:
            self.setsTreeWidget.unCheckAll()
            self.setsTreeWidget.checkSpecials()

#==========================================================================#
#                              Generated                                   #
#==========================================================================#
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(719, 512)
        self.gridLayout = QtWidgets.QGridLayout(Dialog)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setSpacing(6)
        self.horizontalLayout.setMargin(10)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.connectButton = QtWidgets.QPushButton(Dialog)
        self.connectButton.setObjectName(_fromUtf8("connectButton"))
        self.horizontalLayout.addWidget(self.connectButton)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.downloadImagesButton = QtWidgets.QPushButton(Dialog)
        self.downloadImagesButton.setEnabled(False)
        self.downloadImagesButton.setObjectName(_fromUtf8("downloadImagesButton"))
        self.horizontalLayout.addWidget(self.downloadImagesButton)
        self.updateSetsButton = QtWidgets.QPushButton(Dialog)
        self.updateSetsButton.setEnabled(False)
        self.updateSetsButton.setObjectName(_fromUtf8("updateSetsButton"))
        self.horizontalLayout.addWidget(self.updateSetsButton)
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem1)
        self.doneButton = QtWidgets.QPushButton(Dialog)
        self.doneButton.setObjectName(_fromUtf8("doneButton"))
        self.horizontalLayout.addWidget(self.doneButton)
        self.gridLayout.addLayout(self.horizontalLayout, 3, 0, 1, 1)
        self.line = QtWidgets.QFrame(Dialog)
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName(_fromUtf8("line"))
        self.gridLayout.addWidget(self.line, 2, 0, 1, 1)
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4"))
        self.groupBox = QtWidgets.QGroupBox(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Minimum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth())
        self.groupBox.setSizePolicy(sizePolicy)
        self.groupBox.setMinimumSize(QtCore.QSize(250, 50))
        self.groupBox.setMaximumSize(QtCore.QSize(200, 3000))
        self.groupBox.setObjectName(_fromUtf8("groupBox"))
        self.gridLayout_3 = QtWidgets.QGridLayout(self.groupBox)
        self.gridLayout_3.setObjectName(_fromUtf8("gridLayout_3"))
        self.comboBox = QtWidgets.QComboBox(self.groupBox)
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.gridLayout_3.addWidget(self.comboBox, 0, 0, 1, 1)
        self.setsTreeWidget = SetsTreeWidget(self.groupBox, self._db)
        self.setsTreeWidget.setHeaderLabels(['Set', 'Code'])
        self.setsTreeWidget.setMinimumSize(QtCore.QSize(0, 300))
        self.setsTreeWidget.setObjectName(_fromUtf8("setsTreeWidget"))
        self.setsTreeWidget.headerItem().setText(0, _fromUtf8("1"))
        self.gridLayout_3.addWidget(self.setsTreeWidget, 1, 0, 1, 1)
        self.horizontalLayout_4.addWidget(self.groupBox)
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
        self.groupBox_2 = QtWidgets.QGroupBox(Dialog)
        self.groupBox_2.setMinimumSize(QtCore.QSize(0, 150))
        self.groupBox_2.setMaximumSize(QtCore.QSize(16777215, 200))
        self.groupBox_2.setObjectName(_fromUtf8("groupBox_2"))
        self.gridLayout_4 = QtWidgets.QGridLayout(self.groupBox_2)
        self.gridLayout_4.setObjectName(_fromUtf8("gridLayout_4"))
        self.progressBar_2 = QtWidgets.QProgressBar(self.groupBox_2)
        self.progressBar_2.setProperty("value", 0)
        self.progressBar_2.setObjectName(_fromUtf8("progressBar_2"))
        self.gridLayout_4.addWidget(self.progressBar_2, 3, 1, 1, 1)
        self.label_7 = QtWidgets.QLabel(self.groupBox_2)
        self.label_7.setAlignment(QtCore.Qt.AlignCenter)
        self.label_7.setObjectName(_fromUtf8("label_7"))
        self.gridLayout_4.addWidget(self.label_7, 0, 1, 1, 1)
        self.progressBar = QtWidgets.QProgressBar(self.groupBox_2)
        self.progressBar.setProperty("value", 0)
        self.progressBar.setObjectName(_fromUtf8("progressBar"))
        self.gridLayout_4.addWidget(self.progressBar, 1, 1, 1, 1)
        self.label_8 = QtWidgets.QLabel(self.groupBox_2)
        self.label_8.setAlignment(QtCore.Qt.AlignCenter)
        self.label_8.setObjectName(_fromUtf8("label_8"))
        self.gridLayout_4.addWidget(self.label_8, 2, 1, 1, 1)
        self.label_9 = QtWidgets.QLabel(self.groupBox_2)
        self.label_9.setObjectName(_fromUtf8("label_9"))
        self.gridLayout_4.addWidget(self.label_9, 1, 0, 1, 1)
        self.label_10 = QtWidgets.QLabel(self.groupBox_2)
        self.label_10.setObjectName(_fromUtf8("label_10"))
        self.gridLayout_4.addWidget(self.label_10, 3, 0, 1, 1)
        self.verticalLayout_2.addWidget(self.groupBox_2)
        self.groupBox_3 = QtWidgets.QGroupBox(Dialog)
        self.groupBox_3.setMinimumSize(QtCore.QSize(0, 200))
        self.groupBox_3.setObjectName(_fromUtf8("groupBox_3"))
        self.gridLayout_2 = QtWidgets.QGridLayout(self.groupBox_3)
        self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
        self.radioButton_2 = QtWidgets.QRadioButton(self.groupBox_3)
        self.radioButton_2.setChecked(True)
        self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))
        self.horizontalLayout_2.addWidget(self.radioButton_2)
        self.radioButton = QtWidgets.QRadioButton(self.groupBox_3)
        self.radioButton.setEnabled(False)
        self.radioButton.setObjectName(_fromUtf8("radioButton"))
        self.horizontalLayout_2.addWidget(self.radioButton)
        self.gridLayout_2.addLayout(self.horizontalLayout_2, 1, 1, 1, 1)
        self.label_6 = QtWidgets.QLabel(self.groupBox_3)
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.gridLayout_2.addWidget(self.label_6, 1, 0, 1, 1)
        self.textEdit = QtWidgets.QTextEdit(self.groupBox_3)
        self.textEdit.setMinimumSize(QtCore.QSize(0, 25))
        self.textEdit.setMaximumSize(QtCore.QSize(16777215, 25))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.gridLayout_2.addWidget(self.textEdit, 0, 1, 1, 1)
        self.label_5 = QtWidgets.QLabel(self.groupBox_3)
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.gridLayout_2.addWidget(self.label_5, 0, 0, 1, 1)
        self.toolButton = QtWidgets.QToolButton(self.groupBox_3)
        self.toolButton.setMinimumSize(QtCore.QSize(25, 25))
        self.toolButton.setObjectName(_fromUtf8("toolButton"))
        self.gridLayout_2.addWidget(self.toolButton, 0, 2, 1, 1)
        self.verticalLayout_2.addWidget(self.groupBox_3)
        self.groupBox_4 = QtWidgets.QGroupBox(Dialog)
        self.groupBox_4.setMinimumSize(QtCore.QSize(300, 70))
        self.groupBox_4.setMaximumSize(QtCore.QSize(16777215, 60))
        self.groupBox_4.setObjectName(_fromUtf8("groupBox_4"))
        self.formLayout_2 = QtWidgets.QFormLayout(self.groupBox_4)
        self.formLayout_2.setObjectName(_fromUtf8("formLayout_2"))
        self.magiccardsLabel = QtWidgets.QLabel(self.groupBox_4)
        self.magiccardsLabel.setObjectName(_fromUtf8("magiccardsLabel"))
        self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.magiccardsLabel)
        self.magiccardsConnectionLabel = QtWidgets.QLabel(self.groupBox_4)
        self.magiccardsConnectionLabel.setObjectName(_fromUtf8("magiccardsConnectionLabel"))
        self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.magiccardsConnectionLabel)
        self.gathererLabel = QtWidgets.QLabel(self.groupBox_4)
        self.gathererLabel.setObjectName(_fromUtf8("gathererLabel"))
        self.formLayout_2.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.gathererLabel)
        self.gathererConnectionLabel = QtWidgets.QLabel(self.groupBox_4)
        self.gathererConnectionLabel.setObjectName(_fromUtf8("gathererConnectionLabel"))
        self.formLayout_2.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.gathererConnectionLabel)
        self.verticalLayout_2.addWidget(self.groupBox_4)
        self.horizontalLayout_4.addLayout(self.verticalLayout_2)
        self.gridLayout.addLayout(self.horizontalLayout_4, 0, 0, 1, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.connectButton.setText(_translate("Dialog", "Connect", None))
        self.downloadImagesButton.setText(_translate("Dialog", "Download Images", None))
        self.updateSetsButton.setText(_translate("Dialog", "Update Sets", None))
        self.doneButton.setText(_translate("Dialog", "Done", None))
        self.groupBox.setTitle(_translate("Dialog", "Sets", None))
        self.comboBox.setItemText(0, _translate("Dialog", "None", None))
        self.comboBox.setItemText(1, _translate("Dialog", "All Sets", None))
        self.comboBox.setItemText(2, _translate("Dialog", "New", None))
        self.comboBox.setItemText(3, _translate("Dialog", "Expansions", None))
        self.comboBox.setItemText(4, _translate("Dialog", "Core Sets", None))
        self.comboBox.setItemText(5, _translate("Dialog", "Specials & Other", None))
        self.groupBox_2.setTitle(_translate("Dialog", "Progress", None))
        self.label_7.setText(_translate("Dialog", "-", None))
        self.label_8.setText(_translate("Dialog", "-", None))
        self.label_9.setText(_translate("Dialog", "Current Progress", None))
        self.label_10.setText(_translate("Dialog", "Overall Progress", None))
        self.groupBox_3.setTitle(_translate("Dialog", "Settings", None))
        self.radioButton_2.setText(_translate("Dialog", "Magiccards.info", None))
        self.radioButton.setText(_translate("Dialog", "Gatherer.Wizards.com(Later)", None))
        self.label_6.setText(_translate("Dialog", "Download Images From:", None))
        self.textEdit.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<meta name=\"qrichtext\" content=\"1\" />\n"
"

", None))
        self.label_5.setText(_translate("Dialog", "Image Folder:", None))
        self.toolButton.setText(_translate("Dialog", "...", None))
        self.groupBox_4.setTitle(_translate("Dialog", "Connection", None))
        self.magiccardsLabel.setText(_translate("Dialog", "http://magiccards.info:", None))
        self.magiccardsConnectionLabel.setText(_translate("Dialog", "
Offline
", None))
        self.gathererLabel.setText(_translate("Dialog", "http://gatherer.wizards.com:", None))
        self.gathererConnectionLabel.setText(_translate("Dialog", "
(To be implemented later)
", None))


import gym
import pickle
import os
import numpy as np
import traceback
import math
import tensorflow as tf
import multiprocessing as mp
from learner import Learner
from actor import Actor
from memory import ReplayMemory, PERGreedyMemory, SumTree, PERProportionalMemory, PERRankBaseMemory
from simlator import *
#---------------------------------------------------
# manager
#---------------------------------------------------
class R2D2Manager():
    def __init__(self, num_actors, args):
        self.args          = args["agent"]
        self.simlator_args = args["simlator"]
        self.num_actors    = num_actors
        self._create_process()

    def _create_process(self):
        # create each Queue
        experience_q = mp.Queue()
        model_sync_q = [[mp.Queue(), mp.Queue(), mp.Queue()] for _ in range(self.num_actors)]
        self.learner_end_q = [mp.Queue(), mp.Queue()]
        self.actors_end_q = [mp.Queue() for _ in range(self.num_actors)]

        # create actor ps
        self.actors_ps = []
        epsilon = self.args["epsilon"]
        epsilon_alpha = self.args["epsilon_alpha"]
        for i in range(self.num_actors):
            # get epsilon for each actor
            if self.num_actors <= 1:
                epsilon_i = epsilon ** (1 + epsilon_alpha)
            else:
                epsilon_i = epsilon ** (1 + i/(self.num_actors-1)*epsilon_alpha)
            # get simlator fot each actor
            self.simlator   = SeparationControl(self.args, self.simlator_args)
            self.nb_actions = self.simlator.nb_actions

            # get build_compile_model for actor
            model_args = self.get_model_args(1)
            args = (
                self.simlator,
                i,
                epsilon_i,
                model_args,
                self.args,
                experience_q,
                model_sync_q[i],
                self.actors_end_q[i]
                )
            self.actors_ps.append(mp.Process(target=self.actor_run_cpu, args=args))
            print("Actor{} Epsilon:{}".format(i,epsilon_i))

        # get build_compile_model for learner
        model_args = self.get_model_args(self.args["batch_size"])
        args = (
            model_args,
            self.args,
            experience_q,
            model_sync_q,
            self.learner_end_q
            )
        # create learner ps
        self.learner_ps = mp.Process(target=self.learner_run_gpu, args=args)
        self.model_tester_args = self.get_model_args(1)

    def __del__(self):
        self.learner_ps.terminate()
        for p in self.actors_ps:
            p.terminate()

    def train(self):
        # move process
        try:
            self.learner_ps.start()
            for p in self.actors_ps:
                p.start()

            # wait for finish
            while True:
                time.sleep(1)  # polling time

                # 終了判定
                f = True
                for q in self.actors_end_q:
                    if q.empty():
                        f = False
                        break
                if f:
                    break
        except KeyboardInterrupt:
            print("interrupt")
        except Exception:
            print(traceback.format_exc())

        # throw end to learner
        self.learner_end_q[0].put(1)
        time.sleep(1)
        # kill
        self.learner_ps.terminate()
        for p in self.actors_ps:
            p.terminate()

        return

    def test(self):
        # test Actor
        test_actor = Actor(
            self.simlator,
            -1,
            0,
            self.model_tester_args,
            self.args,
            None,
            None,
            training = False,
        )
        test_actor.model.load_weights(self.args["log_dir"]+self.args["load_weights_path"])
        test_actor.test()

    #---------------------------------------------------
    # learner
    #---------------------------------------------------
    def learner_run_gpu(
        self,
        model_args,
        args,
        experience_q,
        model_sync_q,
        learner_end_q,
        ):
        with tf.device("/device:GPU:0"):
            self.learner_run(
            model_args,
            args,
            experience_q,
            model_sync_q,
            learner_end_q
            )

    def learner_run(
        self,
        model_args,
        args,
        experience_q,
        model_sync_q,
        learner_end_q
        ):
        learner = Learner(
            model_args=model_args,
            args=args,
            experience_q=experience_q,
            model_sync_q=model_sync_q,
            learner_end_q=learner_end_q,
        )
        try:
            # model load
            if os.path.isfile(self.args["log_dir"]+self.args["load_weights_path"]):
                learner.model.load_weights(self.args["log_dir"]+self.args["load_weights_path"])
                learner.target_model.load_weights(self.args["log_dir"]+self.args["load_weights_path"])

            # learning
            print("Learner Starts")
            learner.train()

        except KeyboardInterrupt:
            pass
        except Exception:
            print(traceback.format_exc())
        finally:
            print("Learning End. Train Count:{}".format(learner.train_num))

            if self.args["test"]:
                # model save
                if args["save_weights_path"] != "":
                    print("save:" + args["save_weights_path"])
                    learner.model.save_weights(args["log_dir"]+args["save_weights_path"], args["save_overwrite"])
                '''# throw end to learner
                print("Last Learner weights sending...")
                learner_end_q[1].put(learner.model.get_weights())'''

    def actor_run_cpu(
        self,
        simlator,
        actor_index,
        epsilon,
        model_args,
        args,
        experience_q,
        model_sync_q,
        actors_end_q,
        ):
        with tf.device("/device:CPU:0"):
            self.actor_run(
                simlator,
                actor_index,
                epsilon,
                model_args,
                args,
                experience_q,
                model_sync_q,
                actors_end_q)

    def actor_run(
        self,
        simlator,
        actor_index,
        epsilon,
        model_args,
        args,
        experience_q,
        model_sync_q,
        actors_end_q,
        ):
        print("Actor{} Starts!".format(actor_index))
        try:
            actor = Actor(
                simlator,
                actor_index,
                epsilon,
                model_args,
                args,
                experience_q,
                model_sync_q,
                training = not (args["test"])
            )

            # model load
            print(self.args["log_dir"]+self.args["load_weights_path"])
            if os.path.isfile(self.args["log_dir"]+self.args["load_weights_path"]):
                actor.model.load_weights(self.args["log_dir"]+self.args["load_weights_path"])

            # start
            actor.run_actor()
        except KeyboardInterrupt:
            pass
        except Exception:
            print(traceback.format_exc())
        finally:
            print("Actor{} End!".format(actor_index))
            actors_end_q.put(1)

    def get_model_args(self, batch_size):
        model_args  = {
            "batch_size": batch_size,
            "input_sequence": self.args["input_sequence"],
            "input_shape": self.args["input_shape"],
            "enable_image_layer": self.args["enable_image_layer"],
            "nb_actions": self.nb_actions,
            "enable_dueling_network": self.args["enable_dueling_network"],
            "dueling_network_type": self.args["dueling_network_type"],
            "enable_noisynet": self.args["enable_noisynet"],
            "dense_units_num": self.args["dense_units_num"],
            "lstm_units_num": self.args["lstm_units_num"],
            "metrics": self.args["metrics"],
        }
        return model_args


# @Time    : 2018/6/22 18:08
# @Author  : cap
# @FileName: myOpencvStar.py
# @Software: PyCharm Community Edition
# @introduction: star特征检测
import cv2 as cv

image = cv.imread('./data/table.jpg')
cv.imshow('Original', image)

gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)

# 创建检测器
dectector = cv.xfeatures2d.StarDetector_create()
keypoints = dectector.detect(gray)
cv.drawKeypoints(image, keypoints, image, flags=cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv.imshow('Star', image)

cv.waitKey()


"""
This module contains the tests to check offpele's molecular representations.
"""

import pytest

import tempfile
from offpele.main import run_offpele
from offpele.utils import get_data_file_path, temporary_cd

FORCEFIELD_NAME = 'openff_unconstrained-1.2.0.offxml'

class TestMain(object):
    """
    It wraps all tests that involve the Molecule class.
    """

    def test_offpele_default_call(self):
        LIGAND_PATH = 'ligands/BNZ.pdb'
        ligand_path = get_data_file_path(LIGAND_PATH)

        with tempfile.TemporaryDirectory() as tmpdir:
            with temporary_cd(tmpdir):
                run_offpele(ligand_path, output=tmpdir)

    def test_offpele_custom_call(self):
        LIGAND_PATH = 'ligands/BNZ.pdb'
        ligand_path = get_data_file_path(LIGAND_PATH)

        with tempfile.TemporaryDirectory() as tmpdir:
            with temporary_cd(tmpdir):
                run_offpele(ligand_path,
                            forcefield=FORCEFIELD_NAME,
                            resolution=10,
                            charges_method='gasteiger',
                            output=tmpdir,
                            with_solvent=True,
                            as_datalocal=True)


"""
radiality:radiality.creation

The `radiality/creation.py` is a part of `radiality`.
Apache 2.0 licensed.
"""

from itertools import zip_longest
from functools import wraps
import json

import asyncio
from websockets.exceptions import ConnectionClosed

from radiality import watch
from radiality import circuit

def event(method):
    """
    Decorator for the definition of an `event`
    """
    # Constructs the `event` specification template
    n = method.__code__.co_argcount - 1
    keys = method.__code__.co_varnames[n:0:-1]
    defaults = (method.__defaults__ or ())[::-1]
    spec_tmpl = list(zip_longest(keys, defaults))[::-1]
    spec_tmpl.append(('*event', method.__name__))

    method = asyncio.coroutine(method)

    @wraps(method)
    def _wrapper(self, *args, **kwargs):
        spec = {
            key: (value if arg is None else arg)
            for ((key, value), arg) in zip_longest(spec_tmpl, args)
        }
        spec.update(kwargs)

        yield from method(self, *args, **kwargs)
        yield from self._actualize(spec)

    _wrapper._is_event = True

    return asyncio.coroutine(_wrapper)

class Eventer(watch.Loggable, circuit.Connectable):
    """
    Emitter of the specific events
    """
    _effectors = None

    def __init__(self, logger, connector):
        """
        Initialization
        """
        self._logger = logger
        self._connector = connector

        self._effectors = {}

    # overridden from `circuit.Connectable`
    @asyncio.coroutine
    def connect(self, sid, freq):
        if sid in self._effectors:  # => reconnect
            yield from self.disconnect(sid, channel=None)

        channel = yield from super().connect(sid, freq)
        if channel:  # => register a new effector
            self.register_effector(sid, channel)
            yield from self.effector_connected(sid, freq)

        return channel

    # overridden from `circuit.Connectable`
    @asyncio.coroutine
    def disconnect(self, sid, channel):
        channel = self._effectors.pop(sid, channel)

        yield from super().disconnect(sid, channel)
        yield from self.effector_disconnected(sid)

    def register_effector(self, sid, channel):
        self._effectors[sid] = channel

    @asyncio.coroutine
    def effector_connected(self, sid, freq):
        spec = {
            '*signal': ('biconnected' if sid in self.wanted else 'connected'),
            'sid': self.sid,
            'freq': self.freq
        }

        try:
            spec = json.dumps(spec)
            yield from self._effectors[sid].send(spec)
        except ValueError:
            self.fail(
                'Invalid output -- '
                'could not encode the signal specification: %s', str(spec)
            )
        except ConnectionClosed:
            self.fail('Connection closed')

    @asyncio.coroutine
    def effector_disconnected(self, sid):
        pass

    @asyncio.coroutine
    def _transmit(self, channel, spec):
        try:
            yield from channel.send(spec)
        except ConnectionClosed:
            self.fail('Connection closed')
        else:
            return True

        return False

    @asyncio.coroutine
    def _actualize(self, spec):
        try:
            spec = json.dumps(spec)
        except ValueError:
            self.fail(
                'Invalid output -- '
                'could not encode the event specification: %s', str(spec)
            )
        else:
            for (sid, channel) in list(self._effectors.items()):
                try:
                    yield from channel.send(spec)
                except ConnectionClosed:
                    self.warn('Connection to `%s` closed', sid)
                    # Clears the wasted effector
                    self._effectors.pop(sid)
                    yield from self.effector_disconnected(sid)


import sys
from PyQt5 import QtWidgets, QtGui
from main_ui import Ui_AppMainWindow
from PyQt5 import QtCore as qtc
import nse_driver
import time

class Worker(qtc.QThread):
    fetch_completed = qtc.pyqtSignal(dict)
    status = qtc.pyqtSignal(bool)

    def __init__(self, parent=None):
        super(Worker, self).__init__(parent)
        self.thread_active = True

    @qtc.pyqtSlot(int)
    def call_api(self, interval):
        self.status.emit(self.thread_active)
        try:
            while self.thread_active:
                self.fetch_completed.emit(nse_driver.start_fetch())
                time.sleep(interval)
        except OverflowError:
            print("Time Interval Too Large")

    def stop(self):
        self.thread_active = False
        self.status.emit(self.thread_active)
        self.wait()

class MainDriver(QtWidgets.QMainWindow):
    fetch_requested_signal = qtc.pyqtSignal(int)

    def __init__(self):
        super().__init__()
        self.stock_data = nse_driver.start_fetch()
        self.ui = Ui_AppMainWindow()
        self.setWindowIcon(QtGui.QIcon("./icon.ico"))
        self.ui.setupUi(self)
        self.ui.start_button.released.connect(self.start_thread)
        self.ui.stop_button.clicked.connect(self.stop_thread)
        self.ui.stop_button.setEnabled(False)
        self.show()

    def start_thread(self):
        self.worker = Worker()
        self.worker_thread = qtc.QThread(self)
        self.worker.fetch_completed.connect(self.set_table)
        self.worker.fetch_completed.connect(self.set_stats)
        self.worker.status.connect(self.set_status)
        self.fetch_requested_signal.connect(self.worker.call_api)
        self.worker.moveToThread(self.worker_thread)
        self.worker_thread.start()
        self.start_fetch()

    def start_fetch(self):
        interval = self.get_interval()
        if interval is None:
            self.ui.status_bar.showMessage("Default Time Interval Selected: 30s", 5000)
            self.fetch_requested_signal.emit(30)
        else:
            self.fetch_requested_signal.emit(interval)
            self.ui.status_bar.showMessage(
                "Time Interval Selected: {}s".format(str(interval)), 5000
            )
        self.ui.start_button.setEnabled(False)
        self.ui.stop_button.setEnabled(True)
        print("Thread Started")

    def stop_thread(self):
        self.worker.stop()
        self.ui.start_button.setEnabled(True)
        self.ui.stop_button.setEnabled(False)
        print("Thread Killed")

    def get_interval(self):
        options = ("1", "15", "30", "60")
        item, submitted = QtWidgets.QInputDialog.getItem(
            self, "Set Time Interval", "Seconds:", options, 2, False,
        )
        if submitted and item:
            return int(item)

    def set_status(self, status):
        if status:
            self.ui.connection.setText("Active")
            self.ui.connection.setStyleSheet("color: green")
        else:
            self.ui.connection.setText("Inactive")
            self.ui.connection.setStyleSheet("color: red")

    def set_table(self, stock_data):
        self.ui.price_table.setRowCount(0)
        self.ui.price_table.setColumnCount(8)
        for data_list in stock_data["priceDict"]:
            row = self.ui.price_table.rowCount()
            self.ui.price_table.setRowCount(row + 1)
            self.add_table_row(row, data_list)

    def add_table_row(self, row, data_list):
        col = 0
        for item in data_list:
            cell = QtWidgets.QTableWidgetItem(item)
            self.ui.price_table.setItem(row, col, cell)
            col += 1

    def set_stats(self, stock_data):
        self.ui.total_num.setText(str(self.stock_data["stats"]["total"]))
        self.ui.profit_num.setText(str(self.stock_data["stats"]["profit"]))
        self.ui.profit_num.setStyleSheet("color: green")
        self.ui.loss_num.setText(str(self.stock_data["stats"]["loss"]))
        self.ui.loss_num.setStyleSheet("color: red")
        self.ui.no_change_num.setText(str(self.stock_data["stats"]["noChg"]))
        self.ui.no_change_num.setStyleSheet("color: grey")

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = MainDriver()
    w.show()
    sys.exit(app.exec_())


from opentrons import protocol_api
import json
import os
import math

# metadata
metadata = {
    'protocolName': 'Version 2 S9 Station A BP Purebase 20ul Internal Control',
    'author': 'Nick <protocols@opentrons.com>',
    'source': 'Custom Protocol Request',
    'apiLevel': '2.3'
}

NUM_SAMPLES = 96
SAMPLE_VOLUME = 200
INTERNAL_CONTROL_VOLUME = 20
TIP_TRACK = False

def run(ctx: protocol_api.ProtocolContext):

    # load labware
    tempdeck = ctx.load_module('Temperature Module Gen2', '10')
    tempdeck.set_temperature(4)
    num_cols = math.ceil(NUM_SAMPLES/8)
    num_ic_strips = math.ceil(INTERNAL_CONTROL_VOLUME*num_cols*1.1/200)
    cols_per_strip = math.ceil(num_cols/num_ic_strips)
    internal_control = tempdeck.load_labware(
        'opentrons_96_aluminumblock_generic_pcr_strip_200ul',
        'chilled tubeblock for internal control (strip 1)').rows()[0][:num_ic_strips]
    source_racks = [
        ctx.load_labware(
            'opentrons_24_tuberack_eppendorf_2ml_safelock_snapcap', slot,
            'source tuberack ' + str(i+1))
        for i, slot in enumerate(['2', '3', '5', '6'])
    ]
    dest_plate = ctx.load_labware(
        'nest_96_wellplate_2ml_deep', '1', '96-deepwell sample plate')
    lys_buff = ctx.load_labware(
        'opentrons_6_tuberack_falcon_50ml_conical', '4',
        '50ml tuberack for lysis buffer + PK (tube A1)').wells()[0]
    tipracks300 = [ctx.load_labware('opentrons_96_tiprack_300ul', slot,
                                    '200µl filter tiprack')
                    for slot in ['8', '9', '11']]
    tipracks20 = [ctx.load_labware('opentrons_96_filtertiprack_20ul', '7',
                                   '20µl filter tiprack')]

    # load pipette
    m20 = ctx.load_instrument('p20_multi_gen2', 'left', tip_racks=tipracks20)
    p300 = ctx.load_instrument(
        'p300_single_gen2', 'right', tip_racks=tipracks300)
    p300.flow_rate.aspirate = 50
    p300.flow_rate.dispense = 300
    p300.flow_rate.blow_out = 300

    # setup samples
    sources = [
        well for rack in source_racks for well in rack.wells()][:NUM_SAMPLES]
    dests_single = dest_plate.wells()[:NUM_SAMPLES]
    dests_multi = dest_plate.rows()[0][:math.ceil(NUM_SAMPLES)]

    tip_log = {'count': {}}
    folder_path = '/data/A'
    tip_file_path = folder_path + '/tip_log.json'
    if TIP_TRACK and not ctx.is_simulating():
        if os.path.isfile(tip_file_path):
            with open(tip_file_path) as json_file:
                data = json.load(json_file)
                if 'tips300' in data:
                    tip_log['count'][p300] = data['tips300']
                else:
                    tip_log['count'][p300] = 0
                if 'tips20' in data:
                    tip_log['count'][m20] = data['tips20']
                else:
                    tip_log['count'][m20] = 0
    else:
        tip_log['count'] = {p300: 0, m20: 0}

    tip_log['tips'] = {
        p300: [tip for rack in tipracks300 for tip in rack.wells()],
        m20: [tip for rack in tipracks20 for tip in rack.rows()[0]]
    }
    tip_log['max'] = {
        pip: len(tip_log['tips'][pip])
        for pip in [p300, m20]
    }

    def pick_up(pip):
        nonlocal tip_log
        if tip_log['count'][pip] == tip_log['max'][pip]:
            ctx.pause('Replace ' + str(pip.max_volume) + 'µl tipracks before \
resuming.')
            pip.reset_tipracks()
            tip_log['count'][pip] = 0
        pip.pick_up_tip(tip_log['tips'][pip][tip_log['count'][pip]])
        tip_log['count'][pip] += 1

    heights = {lys_buff: 20}
    radius = (lys_buff.diameter)/2
    min_h = 5

    def h_track(tube, vol):
        nonlocal heights
        dh = vol/(math.pi*(radius**2))
        if heights[tube] - dh > min_h:
            heights[tube] = heights[tube] - dh
        else:
            heights[tube] = 5
        return tube.bottom(heights[tube])

    # transfer sample
    for s, d in zip(sources, dests_single):
        pick_up(p300)
        p300.transfer(SAMPLE_VOLUME, s.bottom(5), d.bottom(5), air_gap=20,
                       new_tip='never')
        p300.air_gap(20)
        p300.drop_tip()

    # transfer lysis buffer + proteinase K and mix
    for s, d in zip(sources, dests_single):
        pick_up(p300)
        p300.transfer(210, h_track(lys_buff, 210), d.bottom(5), air_gap=20,
                       mix_after=(10, 100), new_tip='never')
        p300.air_gap(20)
        p300.drop_tip()

    ctx.pause('Incubate sample plate (slot 4) at 55-57˚C for 20 minutes. \
Return to slot 4 when complete.')

    # transfer internal control
    for i, d in enumerate(dests_multi):
        pick_up(m20)
        strip_ind = i//cols_per_strip
        m20.transfer(INTERNAL_CONTROL_VOLUME, internal_control[strip_ind],
                     d.bottom(10), air_gap=20-INTERNAL_CONTROL_VOLUME,
                     new_tip='never')
        m20.air_gap(5)
        m20.drop_tip()

    ctx.comment('Move deepwell plate (slot 4) to Station B for RNA \
extraction.')

    # track final used tip
    if not ctx.is_simulating():
        if not os.path.isdir(folder_path):
            os.mkdir(folder_path)
        data = {
            'tips300': tip_log['count'][p300],
            'tips20': tip_log['count'][m20]
        }
        with open(tip_file_path, 'w') as outfile:
            json.dump(data, outfile)

Station A/BP Purebase P300S 20ul IC/v2_station_a_S9_bp_purebase.py


import logging
import logging.handlers
import json

NeedsInititializing=True

class Llog(object):

    def __init__(self, nameV):
        global NeedsInititializing
        self.logger = logging.getLogger(nameV)
        self.logger.setLevel(logging.DEBUG)
        if NeedsInititializing:
            logfile = '/data/log/' + nameV + '.log'
            logserver = get_syslog()
            fh = logging.FileHandler(logfile)
            fh.setLevel(logging.DEBUG)
            # create console handler with a higher log level
            ch = logging.StreamHandler()
            ch.setLevel(logging.ERROR)
            syslogh = logging.handlers.SysLogHandler(address = (logserver,514), facility='local3')
            syslogh.setLevel(logging.INFO)
            # create formatter and add it to the handlers
            formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            ch.setFormatter(formatter)
            fh.setFormatter(formatter)
            syslogh.setFormatter(formatter)
            # add the handlers to logger
            self.logger.addHandler(ch)
            self.logger.addHandler(fh)
            self.logger.addHandler(syslogh)
            NeedsInititializing=False

        self.logger.info("Logging Begins")

    def logd(self, obj):
        self.logger.debug(obj)

    def logi(self, obj):
        self.logger.info(obj)

def get_syslog():
    file = '/data/home/dogBreed.json'
    with open(file) as data_file:
        data = json.load(data_file)
    return(data['syslog'])


import json
import requests
from requests.api import get, head

def get_octopus_resource(uri, headers, skip_count = 0):
    items = []
    skip_querystring = ""

    if '?' in uri:
        skip_querystring = '&skip='
    else:
        skip_querystring = '?skip='

    response = requests.get((uri + skip_querystring + str(skip_count)), headers=headers)
    response.raise_for_status()

    # Get results of API call
    results = json.loads(response.content.decode('utf-8'))

    # Store results
    if 'Items' in results.keys():
        items += results['Items']

        # Check to see if there are more results
        if (len(results['Items']) > 0) and (len(results['Items']) == results['ItemsPerPage']):
            skip_count += results['ItemsPerPage']
            items += get_octopus_resource(uri, headers, skip_count)

    else:
        return results

    # return results
    return items

octopus_server_uri = 'https://YourURL'
octopus_api_key = 'API-YourAPIKey'
headers = {'X-Octopus-ApiKey': octopus_api_key}
space_name = "Default"
tagset_name = "MyTagset"
tagset_description = "My description"
tags = []
tag = {
    'Id': None,
    'Name': 'Tag1',
    'Color': '#ECAD3F',
    'Description': 'Tag Description',
    'CanonicalTagName': None
}

tags.append(tag)

# Get space
uri = '{0}/api/spaces'.format(octopus_server_uri)
spaces = get_octopus_resource(uri, headers)
space = next((x for x in spaces if x['Name'] == space_name), None)

# Check to see if tagset exists
uri = '{0}/api/{1}/tagsets?partialName={2}'.format(octopus_server_uri, space['Id'], tagset_name)
tagsets = get_octopus_resource(uri, headers)

if len(tagsets) == 0:
    tagset = {
        'Name': tagset_name,
        'Description': tagset_description,
        'Tags': tags
    }

    uri = '{0}/api/{1}/tagsets'.format(octopus_server_uri, space['Id'])
    response = requests.post(uri, headers=headers, json=tagset)
    response.raise_for_status()
else:
    print ('{0} already exists!'.format(tagset_name))


# -*- coding: utf-8 -*-
'''graph writers'''

from itertools import count
from functools import partial

import nanotime
from markupsafe import escape

from graphalchemy.core import octopus

__all__ = ('Links', 'Nodes')

# settings
conf = octopus.S
appspace = conf.appspace
backends = conf.backends
db = conf.key.db
# decorators
direct = getattr(octopus, 'direct')
factory = getattr(octopus, 'factory')
service = getattr(octopus, 'service')

class Writer(octopus.server):

    '''graph writer base'''

    # graph source
    _db = direct(conf.key.backend, conf.userspace)

    @property
    def transaction(self):
        '''get transaction'''
        return self.w.transaction

    def delete_element(self, this, indices=None):
        '''
        delete graph element

        @param this: graph model instance
        @param indicies: graph indices (default: None)
        '''
        self.w.delete_element(this, indices)

    def index_many(self, index, this, indexed):
        '''
        index properties on a graph element

        @param index: graph index label
        @param this: graph model instance
        @param indexed: properties to index
        '''
        self.w.index_many(self.r.index(index), this, indexed)

    def index_one(self, index, key, value, this):
        '''
        index one property of a graph element

        @param index: graph index label
        @param key: graph element property key
        @param value: graph element property value
        @param this: graph model instance
        '''
        self.w.index_one(self.r.index(index), key, value, this)

    def modify(self, this, data):
        '''
        update a graph element's properties

        @param this: graph model instance
        @param data: data
        '''
        self.w.update(this, data)

    @staticmethod
    def now():
        '''current time in microseconds'''
        return nanotime.now().microseconds()

class Links(Writer):

    '''graph link writer'''

    # link model finder
    _finder = direct(conf.model.finder.link, appspace)
    # direct link reader
    r = factory(conf.read.link, backends, db)
    # direct link writer
    w = factory(conf.write.link, backends, db)

    def make(self, link, start, end, kw=None):
        '''
        build a new graph link

        @param data: data
        @param model: graph model (default: None)
        '''
        if kw is None:
            kw = {}
        kw.update(_created=self.now(), _uuid=self.B.uuid())
        return self.w.build(link, start, end, **kw)

class Nodes(Writer):

    '''graph node writer'''

    # node model finder
    _finder = direct(conf.model.finder.node, appspace)
    # direct node reader
    r = factory(conf.read.node, backends, db)
    # direct node writer
    w = factory(conf.write.node, backends, db)

    def _postprocess(self, this, model=None):
        '''
        postprocess graph node

        @param this: graph model instance
        @param model: graph model (default: None)
        '''
        if model is None:
            model = self._finder(model)
        this = model(this)
        C = model.C
        index = C.index
        # index created date
        index_one = self.index_one
        if index:
            index_one(index, 'created', self.now(), this)
        # index any listed properties
        indexed = C.indexed
        if indexed:
            self.index_many(index, this, indexed)
        # add a slug if present
        slug_from = C.slug_from
        if slug_from:
            index_one(index, slug_from, getattr(this, slug_from), this)
        # index properties that support full text search
        fts_indexed = C.fts_indexed
        if fts_indexed:
            self.index_many(C.fts_index, this, fts_indexed)
        return this

    def _preprocess(self, this, model=None):
        '''
        pre-process graph element

        @param this: graph model instance
        @param model: graph model (default: None)
        '''
        esc = self.escape
        for k in model.C.escaped:
            this[k] = esc(this[k])
        this['_modified'] = self.now()
        # slug field
        self.autoslug(this, model)

    @service('model')
    def autoslug(self, properties, model=None):
        '''
        auto slug an element

        @param properties: graph element properties
        @param model: graph model (default: None)
        '''
        if model is None:
            model = self._finder(model)
        C = model.C
        slug_from = C.slug_from
        if not slug_from:
            return
        slug = tmpslug = self.B.slugify(properties[slug_from])
        slug_field = C.slug_field
        # loop until unique slug is located
        filter_by = self.r.filter_by
        index = C.index
        for cnt in count(1):
            prev = filter_by(index, slug_field, tmpslug)
            if not prev:
                break
            tmpslug = '{slug}-{count}'.format(slug=slug, count=cnt)
        # add slug
        properties[slug_field] = tmpslug

    @service('model')
    def anchor(self, model, **kw):
        '''
        create an anchor node

        @param model: graph model (default: None)
        '''
        finder = self._finder
        model = finder(model)
        C = model.C
        name = C.name
        S = self.S.anchor
        label = S.label
        index_one = self.index_one
        def callback(node): #@IgnorePep8
            node = finder(element=node)(node)
            # put in root index
            root_index = C.root_index
            if root_index:
                index_one(root_index, label, name, node)
            # put in model index
            index = C.index
            if index:
                index_one(index, label, label, node)
            return node
        data = dict(
            _created=self.now(),
            _uuid=self.B.uuid(),
            text=S.description.format(name=name),
        )
        data.update(kw)
        return self.w.build(
            data,
            C.reference_link,
            model.nodes.reference(),
            callback,
        )

    @service('model')
    def build(self, data, model=None):
        '''
        build a new graph node

        @param data: data
        @param model: graph model (default: None)
        '''
        if model is None:
            model = self._finder(model)
        C = model.C
        data.update(
            _created=self.now(), _model=C.name, _uuid=self.B.uuid())
        # number of versions to maintain
        if C.versioned:
            data['_versions'] = 0
        self._preprocess(data, model)
        return self.w.build(
            data,
            C.reference_link,
            model.nodes.reference(),
            partial(self._postprocess, model=model),
        )

    @service('model')
    def clone(self, this, model=None):
        '''
        clone a graph node

        @param this: graph model instance
        @param model: graph model (default: None)
        '''
        created = self.now()
        if model is None:
            model = self._finder(model)
        C = model.C
        index = C.index
        S = self.S.fork
        if index:
            # build callback
            index_one = self.index_one
            def callback(fork): #@IgnorePep8
                # add hash
                fork['_hash'] = this.traits.hash_current()
                # refer back to original
                fork['_forked'] = this.id
                fork = model(fork)
                # index fork
                index_one(index, S.label, this.id, fork)
                # index forked time
                index_one(index, S.created, created, fork)
                return fork
        else:
            callback = None
        return self.w.clone(this, C.fork_link, callback, _created=created)

    @service('model')
    def create(self, model=None, **kw):
        '''
        create a new graph element

        @param model: graph model (default: None)
        '''
        if model is None:
            model = self._finder(model)
        instance = model(**kw)
        instance.save()
        return instance

    @staticmethod
    def escape(data):
        '''
        escapes &, <, >, ', and " with HTML entities

        @param data: text data
        '''
        return escape(data)

    def make(self, data):
        '''
        build a new graph node

        @param data: data
        @param model: graph model (default: None)
        '''
        data.update(_created=self.now(), _uuid=self.B.uuid())
        return self.w.build(data)

    @service('model')
    def refer(self, model=None, **kw):
        '''
        create a reference node

        @param model: graph model (default: None)
        '''
        finder = self._finder
        if model is None:
            model = self._finder(model)
        C = model.C
        name = C.name
        S = self.S.reference
        label = S.label
        w = self.w
        index_one = w.index_one
        create_index = w.create_index
        r = self.r
        def callback(node): #@IgnorePep8
            node = finder(element=node)(node)
            # put in root index
            root_index = r.index(C.root_index)
            index_one(root_index, label, name, node)
            # build any main index
            index = C.index
            if index:
                index_one(create_index(index), label, label, node)
            # build any full text index
            fts_index = C.get('fts_index', False)
            if fts_index:
                create_index(fts_index, fts=True)
            return node
        root = r.root
        h = self
        data = dict(
            _created=h.now(),
            _uuid=self.B.uuid(),
            text=S.description.format(name=name),
        )
        data.update(kw)
        return w.build(
            data, C.reference_link, finder(element=root)(root), callback,
        )

    @service('model')
    def revert(self, this, snapshot, model=None):
        '''
        change a graph node to an older snapshot

        @param this: graph model instance
        @param snapshot: snapshot to change to
        @param model: graph model (default: None)
        '''
        if model is None:
            model = self._finder(model)
        self.update(
            model,
            this,
            **self.r.filter_by(self.S.snapshot.label, snapshot).properties
        )

    @service('model')
    def snapshot(self, this, diff, model=None):
        '''
        capture changes between different versions of a graph node

        @param this: graph model instance
        @param diff: differences between two versions of a graph node
        @param model: graph model (default: None)
        '''
        created = self.now()
        # populate diff
        diff.update(
            _created=created,
            _versioned=this.id,
        )
        S = self.S.version
        if model is None:
            model = self._finder(model)
        C = model.C
        index = C.index
        if index:
            # build callback if index
            index_one = self.index_one
            def callback(snapshot): #@IgnorePep8
                snapshot = model(snapshot)
                # index snapshot with parent snapshot
                index_one(index, S.reference, this.id, snapshot)
                # index snapshot as snapshot
                index_one(index, S.label, snapshot.id, snapshot)
                # index versioned time
                index_one(index, S.created, created, snapshot)
                return snapshot
        else:
            callback = None
        # create snapshot and link back to previous snapshot
        return self.w.build(
            diff, C.version_link, this, callback, _created=created,
        )

    @service('model')
    def update(self, this, data, model=None):
        '''
        update a graph element's properties

        @param this: graph model instance
        @param data: data
        @param model: graph model (default: None)
        '''
        if model is None:
            model = self._finder(model)
        self._preprocess(data, model)
        self.w.update(this, data, partial(self._postprocess, model=model))


import cv2
import numpy as np
import face_data_collect
import dlib
import face_recognition
import detect_face_recognition as dfr
def rect_to_bbox(rect):
    """获得人脸矩形的坐标信息"""
    # print(rect)
    x = rect[3]
    y = rect[0]
    w = rect[1] - x
    h = rect[2] - y
    return (x, y, w, h)

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer.yml')
face_cascade = cv2.CascadeClassifier('.\opencv_module\haarcascade_frontalface_default.xml')
font = cv2.FONT_HERSHEY_SIMPLEX

idnum = 0

# cam = cv2.VideoCapture(0)
# minW = 0.1*cam.get(3)#图像宽度
# minH = 0.1*cam.get(4)#图像高度

def face_rec(camera):
    # 下面读取摄像头图像，用矩形标识检测到脸部和训练后结果比对，打印出对应标签所对应名字

    with open('pace_names.txt', 'r') as f:
        face_names = f.read()
    names = face_names.split()
    #
    # minW = 0.1 * camera.get(3)  # 图像宽度
    # minH = 0.1 * camera.get(4)  # 图像高度
    # print(minH,minW)

    while(True):
        read, img = camera.read()
        img = np.fliplr(img).copy()

        # face_locations = face_recognition.face_locations(img)  # 定位图片中的人脸
        # src_faces = []
        # src_face_num = 0
        # for (i, rect) in enumerate(face_locations):
        #     src_face_num = src_face_num + 1
        #     (x, y, w, h) = rect_to_bbox(rect)#读取单个人脸的初始坐标与宽度，高度
        #     detect_face = img[y:y + h, x:x + w]#截取出人脸部分
        #     detect_face = cv2.cvtColor(detect_face, cv2.COLOR_BGR2GRAY)#灰度化
        #     src_faces.append(detect_face)
        #     # cv2.imshow("face", detect_face)
        #     # 用框框圈住人脸，注意位置偏移
        #     img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        #     # cv2.imshow("image", img)
        # # 人脸对齐操作并保存
        # faces_aligned = dfr.face_alignment(src_faces)
        # face_num = 0
        # for roi_gray in faces_aligned:
        #     face_num = face_num + 1
        #     # cv2.imshow("Align_face", roi_gray)

        # #harr方法检测人脸
        #由于读取的摄像头大小为640x480，所以设置人脸最小为60x60
        faces = face_cascade.detectMultiScale(img, 1.3, 5,minSize=(60,60))
        for (x, y, w, h) in faces:
            img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            roi_gray = gray[y:y + h,x:x + w]#注意别弄错了位置

            try:
                roi_gray = cv2.resize(roi_gray, (200,200), interpolation=cv2.INTER_LINEAR)
                params = recognizer.predict(roi_gray)
                print("name:%s,Confidence:%.2f" % (names[params[0]], params[1]))
                if params[1] < 70:#低于50以下是好的识别参考值，高于80的参考值都会认为是低的置信度评分
                    cv2.putText(img, names[params[0]],(x,y-20),cv2.FONT_HERSHEY_SIMPLEX, 1,255,2)
                    cv2.putText(img, "Confidence:"+str((int)(params[1])), (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)
            except:
                continue

        if cv2.waitKey(int(1000/12)):
            break
    return img

if __name__ == "__main__":
    camera = cv2.VideoCapture(0)
    face_rec(camera)


# ##### ######################### #
# #             Weatherman Willy Telegram bot          # #
# ############################## #
# ------------------------------------------------------------------------------
# Author: Ruben Wylleman
#
# Functions:
# - OpenWeatherAPI_5D_Forecast
# - OpenWeatherAPI_Current
# - Telegram API (main function)
# - - Messagehandler start
# - - Messagehandler current
# - - Messagehandler forecast
# - - Messagehandler end
#
# Usage:
# upon receiving a start command, the bot will demand if you wan't the current or forecast weather.
# 2 buttons will appear instead of the keyboard. once the user has selected,
# the buttons will disapear and the user can fill in a city name.
# Upon receiving the city name, the main fuction 'TelegramAPI' will call another function OpenWeatherAPI_Current or OpenWeatherAPI_Forecast.
#

# This bot will allow the user to receive live weather information for the requested region.

# the script starts in the TelegramAPI function upon running

import requests
import telebot
import time
from telebot import types

# the OpenWeatherAPI function will make the call to the openweather API online and parse the received data.

def OpenWeatherAPI_Current(city):
    url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid=4fdb0f3096ed32490c0658bad4fdecdf' .format(city)
    res = requests.get(url)
    data = res.json()

    temp = data['main']['temp']
    #pressure = data['main']['pressure']
    weather = data['weather'][0]['description']
    wind_speed = data['wind']['speed']

    return("The weather in {} is '{}' with a windspeed of {}m/s. The average temperature is {}°C ".format(city, weather, wind_speed, temp))

# The OpenWeatherAPI_5D_Forecast will get the weatherforecast for 5 days for the demanded city.
# this function will return a string with weather, date time, wind speed and temperature.
def OpenWeatherAPI_5D_Forecast(city):
    url='http://api.openweathermap.org/data/2.5/forecast?q={}&units=metric&mode=json&appid=4fdb0f3096ed32490c0658bad4fdecdf' .format(city)
    res = requests.get(url)
    data = res.json() # contains raw data
    Weather_Data_15H = []
    tempAVG_array = []

# Upon receiving the data, we get multiple value's for each day. We will only take the value's for 15H of each day.
# To filter this we need multiple array's with the requested data.

    weather_array = []
    wind_speed_array = []
    date_time = []

    complete_data_list = data['list'] # will reduce the raw data to only raw data under 'list'

    for item in complete_data_list: # for every item in our filtered list, we only want the data where in the string dt_txt 15:00:00 is mentioned
        item_string = item['dt_txt']
        time_test = item_string.find("15:00:00")
        if time_test > 0: # the .find function returns a positive value if there is a hit, and a negative value if there was no hit.
            Weather_Data_15H.append(item) # we add all usefull data in a separate table

    for item in Weather_Data_15H: # from this table, we'll extractthe required info, and prepare it for presentation
        date_time.append(item['dt_txt'])
        tempAVG_array.append(item['main']['temp'])
        weather_array.append(item['weather'][0]['description'])
        wind_speed_array.append(item['wind']['speed'])

    #print (date_time)
    #print (tempAVG_array)
    #print (weather_array)
    #print (wind_speed_array)

    return_string = f"the 5 day weather forecast for {city}: \n \n"
    index_stop = len(date_time) - 1
    index = 0
    while index <= index_stop:
        return_string += f"{date_time[index]}   average temp.: {tempAVG_array[index]}°C    windspeed: {wind_speed_array[index]} m/s  weather: {weather_array[index]}\n"
        index = index + 1
    return(return_string)

# The TelegramAPI function will handle the Telegram communications. Also the script will start in this function.
def TelegramAPI():
    bot_token = "861043992:AAGPOQ752HIEl_VzaJB24eExXb1i1K4xztI"
    bot = telebot.TeleBot(token=bot_token)
    global sel_type
    sel_type = "none"

# the 'start' message handler will initiate the conversation with the user. the keyboard will change layout to force the user only 2 answers
    @bot.message_handler(commands=['start'])
    def send_welcome(message):
        WelcomeText = "Welcome to Willy's Weather Bot, i will lookup the weather for you for your desired location.\nPlease select if you wan't the current weather or the forecast."
        kb_markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
        kb_markup.add('/current','/forecast')
        bot.send_message(message.chat.id, WelcomeText,  reply_markup=kb_markup)

# the 'end' message handler will react on the end command. this allows the user to reset the conversation in case he needs some explanation again.
    @bot.message_handler(commands=['end'])
    def end_standby(message):
        text = "press start to begin."
        kb_markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
        kb_markup.add('/start')
        bot.send_message(message.chat.id,text,  reply_markup=kb_markup)

# some tests with inline keyboards, not yet succesfull

#        kb_markup = types.ReplyKeyboardRemove()
#        buttons = []
#        startbutton = types.InlineKeyboardButton(text="start",  callback_data='/start')
#        buttons.append(startbutton)
#        keyboard = [buttons]
#        kb_inline = types.InlineKeyboardMarkup(keyboard)
#        bot.send_message(message.chat.id, text="resetting...",  reply_markup=kb_markup)
#        bot.send_message(message.chat.id,  text="select start to begin.",  reply_markup=kb_inline)

# the 'current' command handler will respond when the user clicks the current weather option.
# this function will set the flag for the global variable sel_type which will be used in the send_weatherreport function.
# after execution of this function, the keyboard will be reset to the default keyboard.
    @bot.message_handler(commands=['current'])
    def send_current_weatherreport(message):
        global sel_type
        sel_type = "sel_current"
        reset_keyboard = types.ReplyKeyboardRemove()
        bot.send_message(message.chat.id, "please enter the name of the city.",  reply_markup=reset_keyboard)

# the 'forecast' command handler will respond like the 'current' command handler, only will he set the flag to forecast.
# after execution of this function, the keyboard will be reset to the default keyboard.
    @bot.message_handler(commands=['forecast'])
    def send_forecast_weatherreport(message):
        global sel_type
        sel_type = "sel_forecast"
        reset_keyboard = types.ReplyKeyboardRemove()
        bot.send_message(message.chat.id, "please enter the name of the city.",  reply_markup=reset_keyboard)

#This message handler will read in the message received from the user. Then the function will look at the flag setting.
# If the flag was set to 'sel_current' then the OpenWeatherAPI_Current will be called and return a respons.
# if the flag was set to 'sel_forecast' then the OpenWeatherAPI_Forecast will be called and return a respons.
# After receiving the respons, this respons is sent back to the user.
# the keyboard will be set to '/current', '/forecast' or '/end'.
# the 'current' and 'forecast' allows the user to enter another query.
# The 'end' command will return the user to the beginning of the script.

    @bot.message_handler(func=lambda msg: msg.text is not None)
    def send_weatherreport(message):
        cityname = message.text
        if sel_type == "sel_current":
            try:
                response = OpenWeatherAPI_Current(cityname)
            except Exception:
                response = "i'm sorry, but this city doesn't seem to exist in our database."

        if sel_type == "sel_forecast":
            try:
                response = OpenWeatherAPI_5D_Forecast(cityname)
            except Exception:
                response = "i'm sorry, but this city doesn't seem to exist in our database."

        kb_markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
        kb_markup.add('/current',  '/forecast', '/end')
        bot.send_message(message.chat.id, response,  reply_markup = kb_markup)

    while True:
      try:
        bot.polling()
      except Exception:
          time.sleep(15)

 # this forces the program to start in the TelegramAPI function.

if __name__ == '__main__':
    TelegramAPI()


from django.contrib.auth import login
from django.contrib.auth.forms import AuthenticationForm
from django.shortcuts import render, redirect
from django.template.context_processors import csrf
from django.contrib import auth
from django.contrib.auth.models import User
from django.core.paginator import Paginator

from .models import *

def news(request):
    all_news = Article.objects.filter(article_is_it_history=False).order_by('-article_date')
    if request.user.is_authenticated():
        return render(request, 'news.html', {'all_news': all_news})

    if request.method == 'POST':
        form = AuthenticationForm(request, request.POST)
        if form.is_valid():
            login(request, form.get_user())
            return render(request, 'news.html', {'all_news': all_news})
    else:
        form = AuthenticationForm(request)
    return render(request, 'news.html', {'all_news': all_news})

def history(request, page_namber = 1):
    all_news = Article.objects.filter(article_is_it_history = True).order_by('-article_date')

    #Передана ди информация для поска
    if request.POST:
        import re
        from datetime import datetime
        import time
        search = request.POST.get('search','')
        all_news_old = all_news
        all_news = []
        for news in all_news_old:
            flag = False
            #поиск по названию
            if re.findall(search, news.article_title) != [] and flag == False:
                all_news.append(news)
                flag = True
            #поиск по тексту
            if re.findall(search, news.article_text) != [] and flag == False:
                all_news.append(news)
                flag = True
            date_str = news.article_date.strftime("%Y-%m-%d %H:%M:%S")
            #поиск по дате
            if re.findall(search, date_str) != [] and flag == False:
                all_news.append(news)
                flag = True
    else:
        search = 'Поиск'

    all_news_for_page = Paginator(all_news, 10)
    return render(request, 'history2.html', {'all_news': all_news_for_page.page(page_namber), 'search': search})

def article(request, page_id):
    news = Article.objects.get(id=page_id)
    lectors = Lecturer.objects.get(id=news.article_lecturer_id)
    lector_1 = lectors.lecturer_surname
    lector_2 = lectors.lecturer_name
    lector_3 = lectors.lecturer_patronymic
    lector_id = lectors.id
    return render(request, 'viewer.html', {'news': news, 'lector_1': lector_1,
                                           'lector_2': lector_2, 'lector_3': lector_3,
                                           'lector_id': lector_id})

def profile(request):
    return render(request, 'profile.html', {})

def aboutstart(request):
    return render(request, 'about.html', {})

def books(request):
    return render(request, 'books.html', {})

def orgkom(request):
    orgkom_all = Lecturer.objects.filter(lecturer_orgkom = True)
    return render(request, 'orgkom.html', {'orgkom_all': orgkom_all})

def infolector(request, lector_id):
    lector = Lecturer.objects.get(id=lector_id)
    return render(request, 'infolector.html', {'lector': lector})


# -*- coding: utf-8 -*-
from __future__ import print_function

import os
import shutil
import unittest
import mock
import click
from click.testing import CliRunner

from yewdoc import YewStore, cli, ls, api, attach, browse, configure, context, convert, create, delete, \
    describe, diff, edit, find, global_pref, head, kind, ls, ping, push, read, register, \
    rename, show, status, sync, tag, tail, take, user_pref

import yewdoc

class MockRemote(object):
    def authenticate_user(self, data):
        return MockResponse({u'username': 'paul',
                             u'first_name': u'Paul',
                             u'last_name': u'Wolf',
                             u'email': u'paul.wolf@blah.com',
                             u'token': u'b9512423d9ffa2312bdcb3d5c60f7db26d5b08a1'}, 200)
class MockStore(object):
    def put_user_pref(self, s, d):
        pass

class MockYew(object):
    remote = MockRemote()
    store = MockStore()

class MockResponse:
        def __init__(self, json_data, status_code):
            self.json_data = json_data
            self.status_code = status_code

        def json(self):
            return self.json_data

class TestYewdocsClient(unittest.TestCase):

    def setUp(self):
        self.username = 'test_user'
        test_path = "{}/.yew.d/{}".format(os.path.expanduser("~"), self.username)

        # we delete entire environment for each test
        if os.path.exists(test_path):
            shutil.rmtree(test_path)

        self.store = YewStore(username=self.username)

    def create_document(self, title, content="my text", kind='md'):
        content = content
        kind = kind
        doc = self.store.create_document(title, 'default', kind)
        # doc.put_content(content.decode('utf-8'))
        doc.put_content(content)
        return doc

    def test_create_document(self):
        self.create_document("my test doc")

    def test_show_document(self):
        self.create_document("my test doc")
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'show', 'my test doc'])
        assert result.exit_code == 0

    def test_describe_document(self):
        self.create_document("my test doc")
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'describe', 'my test doc'])
        assert result.exit_code == 0
        assert u'location' in result.output
        assert u'kind' in result.output
        assert u'size' in result.output

    def test_tail_document(self):
        self.create_document("my test doc", content="dummy")
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'tail', 'my test doc'])
        assert result.exit_code == 0
        assert 'dummy' in result.output

    def test_head_document(self):
        self.create_document("my test doc", content="dummy")
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'head', 'my test doc'])
        assert result.exit_code == 0
        assert 'dummy' in result.output

    def test_kind_document(self):
        self.create_document("my test doc", content="dummy", kind='md')
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'kind', 'my test doc', 'txt'])
        print(result.output)
        assert result.exit_code == 0

    def test_rename_document(self):
        self.create_document("my test doc", content="dummy", kind='md')
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'rename', 'my test doc', 'my new test doc'])
        print(result.output)
        # assert result.exit_code == 0

    def test_convert_document(self):
        self.create_document("my test doc", content="dummy", kind='md')
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'convert', 'my test doc', 'html'])
        print(result.output)
        assert result.exit_code == 0
        assert '
dummy
' in result.output

    def test_unicode(self):
        name = u"my test dóc"
        content = u"Ā ā Ă ă Ą ą Ć ć Ĉ ĉ Ċ ċ Č č Ď ď Đ đ Ē ē Ĕ ĕ Ė ė Ę ę Ě ě Ĝ ĝ Ğ ğ Ġ ġ Ģ ģ Ĥ ĥ Ħ ħ Ĩ ĩ Ī ī Ĭ ĭ Į į İ ı Ĳ ĳ Ĵ ĵ Ķ ķ ĸ Ĺ ĺ Ļ ļ Ľ ľ Ŀ ŀ Ł ł Ń ń Ņ ņ Ň ň ŉ Ŋ ŋ Ō ō Ŏ ŏ Ő ő Œ œ Ŕ ŕ Ŗ ŗ Ř ř Ś ś Ŝ ŝ Ş ş Š š Ţ ţ Ť ť Ŧ ŧ Ũ ũ Ū ū Ŭ ŭ Ů ů Ű ű Ų ų Ŵ ŵ Ŷ ŷ Ÿ Ź ź Ż ż Ž ž ſ"
        doc = self.create_document(name, content)
        assert doc.get_content() in content
        content = u" ം ഃ അ ആ ഇ ഈ ഉ ഊ ഋ ഌ എ ഏ ഐ ഒ ഓ ഔ ക ഖ ഗ ഘ ങ ച ഛ ജ ഝ ഞ ട ഠ ഡ ഢ ണ ത ഥ ദ ധ ന പ ഫ ബ ഭ മ യ ര റ ല ള ഴ വ ശ ഷ സ ഹ ാ ി ീ ു ൂ ൃ െ േ ൈ ൊ ോ ൌ ് ൗ ൠ ൡ ൦ ൧ ൨ ൩ ൪ ൫ ൬"
        doc = self.create_document(name, content)
        assert doc.get_content() in content

    def test_take(self):
        """Create document via take."""
        runner = CliRunner()
        with runner.isolated_filesystem():
            with open('hello.txt', 'w') as f:
                f.write('Hello World!')
            result = runner.invoke(cli, ['--user=test_user', 'take', 'hello.txt', '--force'])
            print(result.output)
            result = runner.invoke(cli, ['--user=test_user', 'ls', '-l'])
            assert 'hello' in result.output
            assert result.exit_code == 0

    def test_ls(self):
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'ls', '--info'])
        print(result.output)
        assert result.exit_code == 0
        # assert result.output == 'Hello Peter!\n'

    def test_status(self):
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'status'])
        print(result.output)
        assert result.exit_code == 0

    def test_global_pref(self):
        runner = CliRunner()
        result = runner.invoke(cli, ['global_pref'])
        assert result.exit_code == 0
        #print(result.output)

    def test_user_pref(self):
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'user_pref'])
        assert result.exit_code == 0
        #print(result.output)

    def test_authenticate(self):
        yewdoc.yew = MockYew()
        status_code = yewdoc._authenticate('blah','blah')
        assert status_code == 200

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


import json
import ssl
import urllib.error

from Web_Services import twurl

# https://apps.twitter.com/
# Create App and get the four strings, put them in hidden.py
TWITTER_URL = 'https://api.twitter.com/1.1/friends/list.json'

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

while True:
    print('')
    acct = input('Enter Twitter Account:')
    if(len(acct) < 1): break
    url = twurl.augment(TWITTER_URL,
                        {'screen_name': acct, 'count': '5'})
    print('Retrieving', url)
    connection = urllib.request.urlopen(url, context=ctx)
    data = connection.read().decode()

    js = json.loads(data)
    print(json.dumps(js, indent=2))

    headers = dict(connection.getheaders())
    print('Remaining', headers['x-rate-limit-remaining'])

    for u in js['users']:
        print(u['screen_name'])
        if 'status' not in u:
            print('   * No status found')
            continue
        s = u['status']['text']
        print('  ', s[:50])
'''
Enter Twitter Account:drchuck
Retrieving https://api.twitter.com/1.1/friends ...
Remaining 14
{
    "next_cursor": 1444171224491980205,
        "users": [
        {
        "id": 662433,
        "followers_count": 28725,
        "status": {
"text": "@jazzychad I just bought one .__.", "created_at": "Fri Sep 20 08:36:34 +0000 2013", "retweeted": false,
        },
        "location": "San Francisco, California",
        "screen_name": "leahculver",
        "name": "Leah Culver",
}, {
],
"id": 40426722,
"followers_count": 2635,
"status": {
    "text": "RT @WSJ: Big employers like Google ...",
    "created_at": "Sat Sep 28 19:36:37 +0000 2013",
},
"location": "Victoria Canada",
"screen_name": "_valeriei",
"name": "Valerie Irvine",
    "next_cursor_str": "1444171224491980205"
}
leahculver
   @jazzychad I just bought one .__.
_valeriei
   RT @WSJ: Big employers like Google, AT&T are h
ericbollens
   RT @lukew: sneak peek: my LONG take on the good &a
halherzog
   Learning Objects is 10. We had a cake with the LO,
scweeker
   @DeviceLabDC love it! Now where so I get that "etc
Enter Twitter Account:
'''


# -*- coding: utf-8 -*-
import logging

from telegram import ParseMode

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

def send_message(bot, chat_id, msg, parse_mode=ParseMode.MARKDOWN):
    logger.info(f"sending msg {chat_id}, {msg}")
    bot.send_message(chat_id, msg, parse_mode=parse_mode)


import logging

def _getsecret(secret):
    with open("/var/openfaas/secrets/%s" % secret, 'r') as secretfile:
        secret_line = secretfile.readline()
    return secret_line

class HealthCheckFilter(logging.Filter):
    def filter(self, record):
        return not (record.name == 'batcher.rmqbatcher' and 'GET /_/health HTTP/1.1' in record.msg)

def _setup_logging():
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)

    ch = logging.StreamHandler()
    ch.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
    ch.addFilter(HealthCheckFilter())

    logger.addHandler(ch)


from xgboost import XGBRegressor, plot_importance
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score

import matplotlib.pyplot as plt

dataset = load_breast_cancer()

# x = dataset.data
# y = dataset.target

x, y = load_breast_cancer(return_X_y=True)

print(x.shape)
print(y.shape)

x_train,x_test, y_train, y_test = train_test_split(x, y, train_size = 0.96, shuffle = 'True', random_state = 16)

model = XGBRegressor( n_estimators = 300 , learning_rate = 0.1)

model.fit(x_train, y_train, verbose = True, eval_metric = ["logloss","rmse"],
                                            eval_set = [(x_train,y_train), (x_test,y_test)],
                                            early_stopping_rounds = 20)

 # eval_metric => rmse, mae, logloss, error, auc, roc

results = model.evals_result()

# print("eval's result : ", results)

y_pred = model.predict(x_test)

r2 = r2_score(y_pred, y_test)

print("R2 score : %.2ff%%" %(r2*100.0))

epochs = len(results['validation_0']['logloss'])
x_axis = range(0, epochs)

fig, ax = plt.subplots()
ax.plot  (x_axis,  results['validation_0']['logloss'], label = 'Train')
ax.plot  (x_axis,  results['validation_0']['logloss'], label = 'Test')
ax.legend()
plt.ylabel('Log Loss')
plt.title('XGBoost Log Loss')

fig, ax = plt.subplots()
ax.plot  (x_axis,  results['validation_0']['rmse'], label = 'Train')
ax.plot  (x_axis,  results['validation_0']['rmse'], label = 'Test')
ax.legend()
plt.ylabel('RMSE')
plt.title('XGBoost RMSE')

plt.show()


# -*- coding: utf-8 -*-
"""blog_django URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Add an import:  from blog import urls as blog_urls
    2. Add a URL to urlpatterns:  url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url, patterns
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static # относится к ckeditor

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^auth/', include('loginsys.urls', namespace='auth')),
    url(r'^tryforms/', include('tryform.urls', namespace='tryforms')),
    url(r'^', include('blog.urls')),
    url(r'^accounts/', include('allauth.urls')),
]

urlpatterns += patterns('',
        url(r'^ckeditor/', include('ckeditor_uploader.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if settings.DEBUG:
    # static files (images, css, javascript, etc.)
    urlpatterns += patterns('',
        (r'media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT}))

if settings.DEBUG:
    import debug_toolbar
    urlpatterns += patterns('',
        url(r'^__debug__/', include(debug_toolbar.urls)),
    )


import logging
import os
import re

import orjson
import psycopg2
import psycopg2.extras

from firepit.exceptions import DuplicateTable
from firepit.exceptions import InvalidAttr
from firepit.exceptions import UnknownViewname
from firepit.splitter import SqlWriter
from firepit.sqlstorage import SqlStorage
from firepit.sqlstorage import infer_type
from firepit.sqlstorage import validate_name

logger = logging.getLogger(__name__)

def get_storage(url, session_id):
    dbname = url.path.lstrip('/')
    return PgStorage(dbname, url.geturl(), session_id)

def _infer_type(key, value):
    # PostgreSQL type specializations
    rtype = None
    if isinstance(value, bool):
        rtype = 'BOOLEAN'
    else:
        # Fall back to defaults
        rtype = infer_type(key, value)
    return rtype

class PgStorage(SqlStorage):
    def __init__(self, dbname, url, session_id=None):
        super().__init__()
        self.placeholder = '%s'
        self.text_min = 'LEAST'
        self.text_max = 'GREATEST'
        self.ifnull = 'COALESCE'
        self.dbname = dbname
        if not session_id:
            session_id = 'firepit'
        self.session_id = session_id
        options = f'options=--search-path%3D{session_id}'
        sep = '&' if '?' in url else '?'
        connstring = f'{url}{sep}{options}'
        self.connection = psycopg2.connect(
            connstring,
            cursor_factory=psycopg2.extras.RealDictCursor)

        if session_id:
            try:
                self._execute(f'CREATE SCHEMA IF NOT EXISTS "{session_id}";')
            except psycopg2.errors.UniqueViolation:
                self.connection.rollback()

        self._execute(f'SET search_path TO "{session_id}";')

        cursor = self._execute('BEGIN;')
        try:
            self._execute('''CREATE FUNCTION match(pattern TEXT, value TEXT)
                RETURNS boolean AS $$
                    SELECT regexp_match(value, pattern) IS NOT NULL;
            $$ LANGUAGE SQL;''', cursor=cursor)
            self._execute('''CREATE FUNCTION in_subnet(addr TEXT, net TEXT)
                RETURNS boolean AS $$
                    SELECT addr::inet <<= net::inet;
            $$ LANGUAGE SQL;''', cursor=cursor)

            # Do DB initization from base class
            stmt = ('CREATE UNLOGGED TABLE IF NOT EXISTS "__symtable" '
                    '(name TEXT, type TEXT, appdata TEXT);')
            self._execute(stmt, cursor)
            stmt = ('CREATE UNLOGGED TABLE IF NOT EXISTS "__membership" '
                    '(sco_id TEXT, var TEXT);')
            self._execute(stmt, cursor)
            stmt = ('CREATE UNLOGGED TABLE IF NOT EXISTS "__queries" '
                    '(sco_id TEXT, query_id TEXT);')
            self._execute(stmt, cursor)
            self.connection.commit()
            cursor.close()
        except psycopg2.errors.DuplicateFunction:
            self.connection.rollback()

        logger.debug("Connection to PostgreSQL DB %s successful", dbname)

    def _get_writer(self, prefix):
        """Get a DB inserter object"""
        filedir = os.path.dirname(self.dbname)
        return SqlWriter(
            filedir,
            self,
            placeholder=self.placeholder,
            infer_type=_infer_type)

    def __del__(self):
        if self.connection:
            logger.debug("Closing PostgreSQL DB connection")
            self.connection.close()

    def _query(self, query, values=None):
        """Private wrapper for logging SQL query"""
        logger.debug('Executing query: %s', query)
        cursor = self.connection.cursor()
        if not values:
            values = ()
        try:
            cursor.execute(query, values)
        except psycopg2.errors.UndefinedColumn as e:
            self.connection.rollback()
            raise InvalidAttr(str(e)) from e
        except psycopg2.errors.UndefinedTable as e:
            self.connection.rollback()
            raise UnknownViewname(str(e)) from e
        self.connection.commit()
        return cursor

    def _create_table(self, tablename, columns):
        # Same as base class, but disable WAL
        stmt = f'CREATE UNLOGGED TABLE "{tablename}" ('
        stmt += ','.join([f'"{colname}" {coltype}' for colname, coltype in columns.items()])
        stmt += ');'
        logger.debug('_create_table: "%s"', stmt)
        try:
            cursor = self._execute(stmt)
            if 'x_contained_by_ref' in columns:
                self._execute(f'CREATE INDEX "{tablename}_obs" ON "{tablename}" ("x_contained_by_ref");', cursor)
            self.connection.commit()
            cursor.close()
        except (psycopg2.errors.DuplicateTable,
                psycopg2.errors.DuplicateObject,
                psycopg2.errors.UniqueViolation) as e:
            self.connection.rollback()
            raise DuplicateTable(tablename) from e

    def _add_column(self, tablename, prop_name, prop_type):
        stmt = f'ALTER TABLE "{tablename}" ADD COLUMN "{prop_name}" {prop_type};'
        logger.debug('new_property: "%s"', stmt)
        try:
            cursor = self._execute(stmt)
            self.connection.commit()
            cursor.close()
        except psycopg2.errors.DuplicateColumn:
            self.connection.rollback()

    def _create_empty_view(self, viewname, cursor):
        cursor.execute(f'CREATE VIEW "{viewname}" AS SELECT NULL as type WHERE 1<>1;')

    def _create_view(self, viewname, select, sco_type, deps=None, cursor=None):
        """Overrides parent"""
        validate_name(viewname)
        if not cursor:
            cursor = self._execute('BEGIN;')
        if not deps:
            deps = []
        elif viewname in deps:
            # Get the query that makes up the current view
            slct = self._get_view_def(viewname)
            self._execute(f'DROP VIEW IF EXISTS "{viewname}"', cursor)
            # Swap out the viewname for its definition
            select = re.sub(f'"{viewname}"', f'({slct}) AS tmp', select)
        try:
            self._execute(f'CREATE OR REPLACE VIEW "{viewname}" AS {select}', cursor)
        except psycopg2.errors.UndefinedTable:
            # Missing dep?
            self.connection.rollback()
            cursor = self._execute('BEGIN;')
            self._create_empty_view(viewname, cursor)
        except psycopg2.errors.InvalidTableDefinition:
            # Usually "cannot drop columns from view"
            #logger.error(e, exc_info=e)
            self.connection.rollback()
            cursor = self._execute('BEGIN;')
            self._execute(f'DROP VIEW IF EXISTS "{viewname}";', cursor)
            self._execute(f'CREATE VIEW "{viewname}" AS {select}', cursor)
        self._new_name(cursor, viewname, sco_type)
        return cursor

    def _get_view_def(self, viewname):
        cursor = self._query("SELECT definition"
                             " FROM pg_views"
                             " WHERE schemaname = %s"
                             " AND viewname = %s", (self.session_id, viewname))
        viewdef = cursor.fetchone()
        stmt = viewdef['definition'].rstrip(';')

        # PostgreSQL will "expand" the original "*" to the columns
        # that existed at that time.  We need to get the star back, to
        # match SQLite3's behavior.
        return re.sub(r'^.*?FROM', 'SELECT * FROM', stmt, 1, re.DOTALL)

    def tables(self):
        cursor = self._query("SELECT table_name"
                             " FROM information_schema.tables"
                             " WHERE table_schema = %s", (self.session_id, ))
        rows = cursor.fetchall()
        return [i['table_name'] for i in rows
                if not i['table_name'].startswith('__')]

    def views(self):
        cursor = self._query("SELECT table_name"
                             " FROM information_schema.tables"
                             " WHERE table_schema = %s"
                             " AND table_type = 'VIEW'", (self.session_id, ))
        rows = cursor.fetchall()
        return [i['table_name'] for i in rows]

    def columns(self, viewname):
        validate_name(viewname)
        cursor = self._query("SELECT column_name"
                             " FROM information_schema.columns"
                             " WHERE table_schema = %s"
                             " AND table_name = %s", (self.session_id, viewname))
        rows = cursor.fetchall()
        return [i['column_name'] for i in rows]

    def schema(self, viewname):
        validate_name(viewname)
        cursor = self._query("SELECT column_name AS name, data_type AS type"
                             " FROM information_schema.columns"
                             " WHERE table_schema = %s"
                             " AND table_name = %s", (self.session_id, viewname))
        return cursor.fetchall()

    def delete(self):
        """Delete ALL data in this store"""
        cursor = self._execute('BEGIN;')
        self._execute(f'DROP SCHEMA "{self.session_id}" CASCADE;', cursor)
        self.connection.commit()
        cursor.close()

    def upsert_many(self, cursor, tablename, objs, query_id):
        cols = set()
        for obj in objs:
            cols = cols.union(obj.keys())
        colnames = list(cols)
        if tablename == 'identity':
            action = 'NOTHING'
        else:
            excluded = self._get_excluded(colnames, tablename)
            action = f'UPDATE SET {excluded}'
        valnames = ', '.join([f'"{x}"' for x in colnames])
        placeholders = ', '.join([f"({', '.join([self.placeholder] * len(colnames))})"] * len(objs))
        stmt = (f'INSERT INTO "{tablename}" ({valnames}) VALUES {placeholders}'
                f' ON CONFLICT (id) DO {action};')
        values = []
        query_values = []
        for obj in objs:
            query_values.append(obj['id'])
            query_values.append(query_id)
            for c in colnames:
                value = obj.get(c, None)
                values.append(str(orjson.dumps(value), 'utf-8') if isinstance(value, list) else value)
        logger.debug('upsert_many: count=%d table=%s columns=%s action=%s"',
                     len(objs), tablename, valnames, action)
        cursor.execute(stmt, values)

        # Now add to query table as well
        placeholders = ', '.join([f'({self.placeholder}, {self.placeholder})'] * len(objs))
        stmt = (f'INSERT INTO "__queries" (sco_id, query_id)'
                f' VALUES {placeholders}')
        cursor.execute(stmt, query_values)