"""The reactions list"""
from math import isclose

import cobra
from qtpy.QtCore import QMimeData, Qt, Signal, Slot
from qtpy.QtGui import QDrag, QIcon
from qtpy.QtWidgets import (QHBoxLayout, QHeaderView, QLabel, QLineEdit,
                            QMessageBox, QPushButton, QSizePolicy, QSplitter,
                            QTableWidget, QTableWidgetItem, QTreeWidget,
                            QTreeWidgetItem, QVBoxLayout, QWidget)

from cnapy.cnadata import CnaData
from cnapy.utils import SignalThrottler, turn_red, turn_white

class DragableTreeWidget(QTreeWidget):
    '''A list of dragable reaction items'''

    def mouseMoveEvent(self, _event):
        item = self.currentItem()
        if item is not None:
            reaction: cobra.Reaction = item.data(3, 0)
            mime_data = QMimeData()
            mime_data.setText(reaction.id)
            drag = QDrag(self)
            drag.setMimeData(mime_data)
            drag.exec_(Qt.CopyAction | Qt.MoveAction, Qt.CopyAction)

class ReactionList(QWidget):
    """A list of reaction"""

    def __init__(self, appdata: CnaData):
        QWidget.__init__(self)
        self.appdata = appdata
        self.last_selected = None
        self.reaction_counter = 1

        self.add_button = QPushButton("Add new reaction")
        self.add_button.setIcon(QIcon.fromTheme("list-add"))
        policy = QSizePolicy()
        policy.ShrinkFlag = True
        self.add_button.setSizePolicy(policy)

        self.reaction_list = DragableTreeWidget()
        self.reaction_list.setDragEnabled(True)
        self.reaction_list.setHeaderLabels(["Id", "Name", "Flux"])
        self.reaction_list.setSortingEnabled(True)

        for r in self.appdata.project.cobra_py_model.reactions:
            self.add_reaction(r)

        self.reaction_mask = ReactionMask(self)
        self.reaction_mask.hide()

        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)
        l = QHBoxLayout()
        l.setAlignment(Qt.AlignRight)
        l.addWidget(self.add_button)
        self.splitter = QSplitter()
        self.splitter.setOrientation(Qt.Vertical)
        self.splitter.addWidget(self.reaction_list)
        self.splitter.addWidget(self.reaction_mask)
        self.layout.addItem(l)
        self.layout.addWidget(self.splitter)
        self.setLayout(self.layout)

        self.reaction_list.currentItemChanged.connect(self.reaction_selected)
        self.reaction_mask.reactionChanged.connect(
            self.handle_changed_reaction)
        self.reaction_mask.reactionDeleted.connect(
            self.handle_deleted_reaction)
        self.reaction_mask.jumpToMap.connect(self.emit_jump_to_map)
        self.reaction_mask.jumpToMetabolite.connect(
            self.emit_jump_to_metabolite)

        self.add_button.clicked.connect(self.add_new_reaction)

    def clear(self):
        self.reaction_list.clear()
        self.reaction_mask.hide()

    def add_reaction(self, reaction: cobra.Reaction) -> QTreeWidgetItem:
        ''' create a new item in the reaction list'''
        self.reaction_list.clearSelection()
        item = QTreeWidgetItem(self.reaction_list)
        item.setText(0, reaction.id)
        item.setText(1, reaction.name)
        self.set_flux_value(item, reaction.id)

        text = "Id: " + reaction.id + "\nName: " + reaction.name \
            + "\nEquation: " + reaction.build_reaction_string()\
            + "\nLowerbound: " + str(reaction.lower_bound) \
            + "\nUpper bound: " + str(reaction.upper_bound) \
            + "\nObjective coefficient: " + str(reaction.objective_coefficient)

        item.setToolTip(1, text)
        item.setData(3, 0, reaction)

        return item

    def set_flux_value(self, item, key):
        if key in self.appdata.project.scen_values.keys():
            (vl, vu) = self.appdata.project.scen_values[key]
            if isclose(vl, vu, abs_tol=self.appdata.abs_tol):
                item.setData(2, 0, round(vl, self.appdata.rounding))
            else:
                item.setData(
                    2, 0, str((round(vl, self.appdata.rounding), round(vu, self.appdata.rounding))))
            item.setBackground(2, self.appdata.scen_color)
            item.setForeground(2, Qt.black)
        elif key in self.appdata.project.comp_values.keys():
            (vl, vu) = self.appdata.project.comp_values[key]

            # We differentiate special cases like (vl==vu)
            if isclose(vl, vu, abs_tol=self.appdata.abs_tol):
                if self.appdata.modes_coloring:
                    if vl == 0:
                        item.setBackground(2, Qt.red)
                    else:
                        item.setBackground(2, Qt.green)
                else:
                    item.setBackground(2, self.appdata.comp_color)

                item.setData(2, 0, round(vl, self.appdata.rounding))
            else:
                if isclose(vl, 0.0, abs_tol=self.appdata.abs_tol):
                    item.setBackground(2, self.appdata.special_color_1)
                elif isclose(vu, 0.0, abs_tol=self.appdata.abs_tol):
                    item.setBackground(2, self.appdata.special_color_1)
                elif vl <= 0 and vu >= 0:
                    item.setBackground(2, self.appdata.special_color_1)
                else:
                    item.setBackground(2, self.appdata.special_color_2)
                item.setData(
                    2, 0, str((round(vl, self.appdata.rounding), round(vu, self.appdata.rounding))))

            item.setForeground(2, Qt.black)

    def add_new_reaction(self):
        self.reaction_mask.show()
        while True:
            name = "rxn_"+str(self.reaction_counter)
            self.reaction_counter += 1
            if name not in self.appdata.project.cobra_py_model.reactions:
                break
        reaction = cobra.Reaction(name)
        self.appdata.project.cobra_py_model.add_reactions([reaction])
        item = self.add_reaction(reaction)
        self.reaction_selected(item, 1)
        self.appdata.window.unsaved_changes()

    def update_annotations(self, annotation):

        self.reaction_mask.annotation.itemChanged.disconnect(
            self.reaction_mask.throttler.throttle)
        c = self.reaction_mask.annotation.rowCount()
        for i in range(0, c):
            self.reaction_mask.annotation.removeRow(0)
        i = 0
        for key in annotation:
            self.reaction_mask.annotation.insertRow(i)
            keyl = QTableWidgetItem(key)
            iteml = QTableWidgetItem(str(annotation[key]))
            self.reaction_mask.annotation.setItem(i, 0, keyl)
            self.reaction_mask.annotation.setItem(i, 1, iteml)
            i += 1

        self.reaction_mask.annotation.itemChanged.connect(
            self.reaction_mask.throttler.throttle)

    def reaction_selected(self, item: QTreeWidgetItem, _column):
        if item is None:
            self.reaction_mask.hide()
        else:
            item.setSelected(True)
            self.reaction_mask.show()
            reaction: cobra.Reaction = item.data(3, 0)

            self.last_selected = reaction.id
            self.reaction_mask.reaction = reaction

            self.reaction_mask.id.setText(reaction.id)
            self.reaction_mask.name.setText(reaction.name)
            self.reaction_mask.equation.setText(
                reaction.build_reaction_string())
            self.reaction_mask.lower_bound.setText(str(reaction.lower_bound))
            self.reaction_mask.upper_bound.setText(str(reaction.upper_bound))
            self.reaction_mask.coefficent.setText(
                str(reaction.objective_coefficient))
            self.reaction_mask.gene_reaction_rule.setText(
                str(reaction.gene_reaction_rule))
            self.update_annotations(reaction.annotation)

            self.reaction_mask.changed = False

            turn_white(self.reaction_mask.id)
            turn_white(self.reaction_mask.name)
            turn_white(self.reaction_mask.name)
            turn_white(self.reaction_mask.equation)
            turn_white(self.reaction_mask.lower_bound)
            turn_white(self.reaction_mask.upper_bound)
            turn_white(self.reaction_mask.coefficent)
            turn_white(self.reaction_mask.gene_reaction_rule)
            self.reaction_mask.is_valid = True
        (_, r) = self.splitter.getRange(1)
        self.splitter.moveSplitter(r/2, 1)
        self.reaction_mask.update_state()

    def handle_changed_reaction(self, reaction: cobra.Reaction):
        # Update reaction item in list
        root = self.reaction_list.invisibleRootItem()
        child_count = root.childCount()
        for i in range(child_count):
            item = root.child(i)
            if item.data(3, 0) == reaction:
                old_id = item.text(0)
                item.setText(0, reaction.id)
                item.setText(1, reaction.name)
                break

        self.last_selected = self.reaction_mask.id.text()
        self.reactionChanged.emit(old_id, reaction)

    def handle_deleted_reaction(self, reaction: cobra.Reaction):
        '''Remove reaction item from reaction list'''
        root = self.reaction_list.invisibleRootItem()
        child_count = root.childCount()
        for i in range(child_count):
            item = root.child(i)
            if item.data(3, 0) == reaction:
                # remove item
                self.reaction_list.takeTopLevelItem(
                    self.reaction_list.indexOfTopLevelItem(item))
                break

        self.last_selected = self.reaction_mask.id.text()
        self.reactionDeleted.emit(reaction)

    def update_selected(self, string):
        root = self.reaction_list.invisibleRootItem()
        child_count = root.childCount()
        for i in range(child_count):
            item = root.child(i)
            item.setHidden(True)

        for item in self.reaction_list.findItems(string, Qt.MatchContains, 0):
            item.setHidden(False)
        for item in self.reaction_list.findItems(string, Qt.MatchContains, 1):
            item.setHidden(False)

    def update(self):
        self.reaction_list.clear()
        for r in self.appdata.project.cobra_py_model.reactions:
            self.add_reaction(r)

        if self.last_selected is None:
            pass
        else:
            items = self.reaction_list.findItems(
                self.last_selected, Qt.MatchExactly)

            for i in items:
                self.reaction_list.setCurrentItem(i)
                break

        self.reaction_mask.update_state()

    def set_current_item(self, key):
        self.last_selected = key
        self.update()

    def emit_jump_to_map(self, idx: str, reaction: str):
        self.jumpToMap.emit(idx, reaction)

    def emit_jump_to_metabolite(self, metabolite):
        self.jumpToMetabolite.emit(metabolite)

    itemActivated = Signal(str)
    reactionChanged = Signal(str, cobra.Reaction)
    reactionDeleted = Signal(cobra.Reaction)
    jumpToMap = Signal(str, str)
    jumpToMetabolite = Signal(str)

class JumpButton(QPushButton):
    """button to jump to reactions on map"""

    def __init__(self, parent, r_id: str):
        QPushButton.__init__(self, r_id)
        self.parent = parent
        self.id: str = r_id
        self.clicked.connect(self.emit_jump_to_map)

    def emit_jump_to_map(self):
        self.jumpToMap.emit(self.id)

    jumpToMap = Signal(str)

class JumpList(QWidget):
    """List of buttons to jump to reactions on map"""

    def __init__(self, parent):
        QWidget.__init__(self)
        self.parent = parent
        self.layout = QHBoxLayout()
        self.layout.setAlignment(Qt.AlignLeft)

    def clear(self):
        for i in reversed(range(self.layout.count())):
            self.layout.itemAt(i).widget().setParent(None)

    def add(self, name: str):
        if self.layout.count() == 0:
            label = QLabel("Jump to reaction on map:")
            self.layout.addWidget(label)

        jb = JumpButton(self, name)
        policy = QSizePolicy()
        policy.ShrinkFlag = True
        jb.setSizePolicy(policy)
        self.layout.addWidget(jb)
        self.setLayout(self.layout)

        jb.jumpToMap.connect(self.parent.emit_jump_to_map)

    @ Slot(str)
    def emit_jump_to_map(self: JumpButton, name: str):
        self.parent.emit_jump_to_map(name)

    jumpToMap = Signal(str)

class ReactionMask(QWidget):
    """The input mask for a reaction"""

    def __init__(self, parent: ReactionList):
        QWidget.__init__(self)

        self.parent = parent
        self.reaction = None
        self.is_valid = True
        self.changed = False
        self.setAcceptDrops(False)

        layout = QVBoxLayout()
        l = QHBoxLayout()
        self.delete_button = QPushButton("Delete reaction")
        self.delete_button.setIcon(QIcon.fromTheme("edit-delete"))
        policy = QSizePolicy()
        policy.ShrinkFlag = True
        self.delete_button.setSizePolicy(policy)
        l.addWidget(self.delete_button)
        layout.addItem(l)

        l = QHBoxLayout()
        label = QLabel("Id:")
        self.id = QLineEdit()
        l.addWidget(label)
        l.addWidget(self.id)
        layout.addItem(l)

        l = QHBoxLayout()
        label = QLabel("Name:")
        self.name = QLineEdit()
        l.addWidget(label)
        l.addWidget(self.name)
        layout.addItem(l)

        l = QHBoxLayout()
        label = QLabel("Equation:")
        self.equation = QLineEdit()
        l.addWidget(label)
        l.addWidget(self.equation)
        layout.addItem(l)

        l = QHBoxLayout()
        label = QLabel("Rate min:")
        self.lower_bound = QLineEdit()
        l.addWidget(label)
        l.addWidget(self.lower_bound)
        layout.addItem(l)

        l = QHBoxLayout()
        label = QLabel("Rate max:")
        self.upper_bound = QLineEdit()
        l.addWidget(label)
        l.addWidget(self.upper_bound)
        layout.addItem(l)

        l = QHBoxLayout()
        label = QLabel("Coefficient in obj. function:")
        self.coefficent = QLineEdit()
        l.addWidget(label)
        l.addWidget(self.coefficent)
        layout.addItem(l)

        l = QHBoxLayout()
        label = QLabel("Gene reaction rule:")
        self.gene_reaction_rule = QLineEdit()
        l.addWidget(label)
        l.addWidget(self.gene_reaction_rule)
        layout.addItem(l)

        l = QVBoxLayout()
        label = QLabel("Annotations:")
        l.addWidget(label)
        l2 = QHBoxLayout()
        self.annotation = QTableWidget(0, 2)
        self.annotation.setHorizontalHeaderLabels(
            ["key", "value"])
        self.annotation.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        l2.addWidget(self.annotation)

        self.add_anno = QPushButton("+")
        self.add_anno.clicked.connect(self.add_anno_row)
        l2.addWidget(self.add_anno)
        l.addItem(l2)
        layout.addItem(l)

        l = QVBoxLayout()
        label = QLabel("Metabolites involved in this reaction:")
        l.addWidget(label)
        l2 = QHBoxLayout()
        self.metabolites = QTreeWidget()
        self.metabolites.setHeaderLabels(["Id"])
        self.metabolites.setSortingEnabled(True)
        l2.addWidget(self.metabolites)
        l.addItem(l2)
        self.metabolites.itemDoubleClicked.connect(
            self.emit_jump_to_metabolite)
        layout.addItem(l)

        self.jump_list = JumpList(self)
        layout.addWidget(self.jump_list)

        self.setLayout(layout)

        self.delete_button.clicked.connect(self.delete_reaction)

        self.throttler = SignalThrottler(500)
        self.throttler.triggered.connect(self.reaction_data_changed)

        self.id.textEdited.connect(self.throttler.throttle)
        self.name.textEdited.connect(self.throttler.throttle)
        self.equation.textEdited.connect(self.throttler.throttle)
        self.lower_bound.textEdited.connect(self.throttler.throttle)
        self.upper_bound.textEdited.connect(self.throttler.throttle)
        self.coefficent.textEdited.connect(self.throttler.throttle)
        self.gene_reaction_rule.textEdited.connect(self.throttler.throttle)
        self.annotation.itemChanged.connect(self.throttler.throttle)

        self.validate_mask()

    def add_anno_row(self):
        i = self.annotation.rowCount()
        self.annotation.insertRow(i)
        self.changed = True

    def apply(self):
        try:
            self.reaction.id = self.id.text()
        except ValueError:
            turn_red(self.id)
            QMessageBox.information(
                self, 'Invalid id', 'Could not apply changes identifier ' +
                self.id.text() + ' already used.')
        else:
            self.reaction.name = self.name.text()
            self.reaction.build_reaction_from_string(self.equation.text())
            self.reaction.lower_bound = float(self.lower_bound.text())
            self.reaction.upper_bound = float(self.upper_bound.text())
            self.reaction.objective_coefficient = float(self.coefficent.text())
            self.reaction.gene_reaction_rule = self.gene_reaction_rule.text()
            self.reaction.annotation = {}
            rows = self.annotation.rowCount()
            for i in range(0, rows):
                key = self.annotation.item(i, 0).text()
                if self.annotation.item(i, 1) is None:
                    value = ""
                else:
                    value = self.annotation.item(i, 1).text()

                self.reaction.annotation[key] = value

            self.changed = False
            self.reactionChanged.emit(self.reaction)

    def delete_reaction(self):
        self.hide()
        self.reactionDeleted.emit(self.reaction)

    def validate_id(self):

        with self.parent.appdata.project.cobra_py_model as model:
            try:
                r = cobra.Reaction(id=self.id.text())
                model.add_reaction(r)
            except ValueError:
                turn_red(self.id)
                return False
            else:
                turn_white(self.id)
                return True

    def validate_name(self):
        with self.parent.appdata.project.cobra_py_model as model:
            try:
                r = cobra.Reaction(id="testid", name=self.name.text())
                model.add_reaction(r)
            except ValueError:
                turn_red(self.name)
                return False
            else:
                turn_white(self.name)
                return True

    def validate_equation(self):
        ok = False
        test_reaction = cobra.Reaction(
            "xxxx_cnapy_test_reaction", name="cnapy test reaction")
        with self.parent.appdata.project.cobra_py_model as model:
            model.add_reaction(test_reaction)

            try:
                eqtxt = self.equation.text().rstrip()
                if len(eqtxt) > 0 and eqtxt[-1] == '+':
                    turn_red(self.equation)
                else:
                    test_reaction.build_reaction_from_string(eqtxt)
                    turn_white(self.equation)
                    ok = True
            except ValueError:
                turn_red(self.equation)

        try:
            test_reaction = self.parent.appdata.project.cobra_py_model.reactions.get_by_id(
                "xxxx_cnapy_test_reaction")
            self.parent.appdata.project.cobra_py_model.remove_reactions(
                [test_reaction], remove_orphans=True)
        except KeyError:
            pass

        return ok

    def validate_lowerbound(self):
        try:
            _x = float(self.lower_bound.text())
        except ValueError:
            turn_red(self.lower_bound)
            return False
        else:
            turn_white(self.lower_bound)
            return True

    def validate_upperbound(self):
        try:
            _x = float(self.upper_bound.text())
        except ValueError:
            turn_red(self.upper_bound)
            return False
        else:
            turn_white(self.upper_bound)
            return True

    def validate_coefficient(self):
        try:
            _x = float(self.coefficent.text())
        except ValueError:
            turn_red(self.coefficent)
            return False
        else:
            turn_white(self.coefficent)
            return True

    def validate_gene_reaction_rule(self):
        try:
            _x = float(self.gene_reaction_rule.text())
        except ValueError:
            turn_red(self.gene_reaction_rule)
            return False
        else:
            turn_white(self.gene_reaction_rule)
            return True

    def validate_mask(self):

        valid_id = self.validate_id()
        valid_name = self.validate_name()
        valid_equation = self.validate_equation()
        valid_lb = self.validate_lowerbound()
        valid_ub = self.validate_upperbound()
        valid_coefficient = self.validate_coefficient()
        if valid_id & valid_name & valid_equation & valid_lb & valid_ub & valid_coefficient:
            self.is_valid = True
        else:
            self.is_valid = False

    def reaction_data_changed(self):
        self.changed = True
        self.validate_mask()
        if self.is_valid:
            self.apply()
            self.update_state()

    def update_state(self):
        self.jump_list.clear()
        for name, m in self.parent.appdata.project.maps.items():
            if self.id.text() in m["boxes"]:
                self.jump_list.add(name)

        self.metabolites.clear()
        if self.parent.appdata.project.cobra_py_model.reactions.has_id(self.id.text()):
            reaction = self.parent.appdata.project.cobra_py_model.reactions.get_by_id(
                self.id.text())
            for m in reaction.metabolites:
                item = QTreeWidgetItem(self.metabolites)
                item.setText(0, m.id)
                item.setText(1, m.name)
                item.setData(2, 0, m)
                text = "Id: " + m.id + "\nName: " + m.name
                item.setToolTip(1, text)

    def emit_jump_to_map(self, name):
        self.jumpToMap.emit(name, self.id.text())

    def emit_jump_to_metabolite(self, metabolite):
        self.jumpToMetabolite.emit(str(metabolite.data(2, 0)))

    jumpToMap = Signal(str, str)
    jumpToMetabolite = Signal(str)
    reactionChanged = Signal(cobra.Reaction)
    reactionDeleted = Signal(cobra.Reaction)


from django.conf.urls import patterns, include, url
from django.views.generic.base import TemplateView

urlpatterns = patterns('',

	url(r'^todas/','subasta.views.todas',name="todas"),
	url(r'^recientes/','subasta.views.recientes',name="recientes"),
	url(r'^proximas/','subasta.views.proximas',name="proximas"),
	url(r'^mis_subastas/$','subasta.views.mis_subastas',name="mis_subastas"),
	url(r'^ver/(?P<subastaid>\d+)/(?P<comentarioid>\d+)','comentario.views.responder',name="responder_subasta"),
	url(r'^ver/(?P<id>\d+)','subasta.views.ver_subasta',name="ver_subasta"),
	url(r'^editar/(?P<id>\d+)','subasta.views.editar_subasta',name="editar_subasta"),
	url(r'^nueva/','subasta.views.nueva_subasta',name="nueva_subasta"),
	#url(r'^editar/(?P<id>\d+)','subasta.views.editar_subasta',name="editar_subasta"),
	url(r'^eliminar/(?P<id>\d+)','subasta.views.eliminar_subasta',name="eliminar_subasta"),
	url(r'^buscar/','subasta.views.buscar_subastas',name="buscar_subasta"),
	#url(r'^nueva/','subasta.views.nueva',name="nueva"),

	#url(r'^ver/(?P<id>\d+)/comentario/',include('comentario.urls')),

)


import discord
from discord.ext import commands
from discord import errors
from logic import *

class member_handler():

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

    async def on_member_update(self, before, after):
        if 'la-crew' in [y.name.lower() for y in after.roles]:
            if str(before.status) == 'offline' and str(after.status) != 'offline':
                if (after.is_on_mobile()):
                    speech = await TTSManager.build(text='{} ahora está online en Discord desde el teléfono.'.format(after.display_name))
                    self.bot.voice_clients[0].play(discord.FFmpegPCMAudio(speech))
                else:
                    speech = await TTSManager.build(text='{} ahora está online en Discord.'.format(after.display_name))
                    self.bot.voice_clients[0].play(discord.FFmpegPCMAudio(speech))
            if str(before.status) == 'online' and str(after.status) == 'offline':
                    speech = await TTSManager.build(text='{} se desconectó de Discord.'.format(after.display_name))
                    self.bot.voice_clients[0].play(discord.FFmpegPCMAudio(speech))
            if before.activity == None and after.activity != None and str(before.status) != 'offline':
                speech = await TTSManager.build(text='{} ahora está jugando {}'.format(after.display_name, after.activity.name))
                self.bot.voice_clients[0].play(discord.FFmpegPCMAudio(speech))
        else:
            pass

def setup(bot):
    bot.add_cog(member_handler(bot))


from functools import reduce
import psutil
import os
import sys
import time

files = sys.argv
files.pop(0)

def can_be_float(item):
    try:
        a = float(item)
        return True
    except:
        return False

start_time = time.time()
print("name,time,%cpu,%mem")
while True:
    try:
        parent_pool = list(psutil.Process(1).children(recursive = False))
        parent_pool = filter(lambda p: "docker" in str(p.name),parent_pool)
        pool = []
        for process in parent_pool:
            pools = process.children(recursive = True)
            if pool is not None:
                pool.extend(pools)

        mem = 0
        cpu = 0

        for process in pool:
            linux_ps = os.popen("ps -p " + str(process.pid) + " -o %cpu,%mem").read().split("\n")[1]
            linux_ps = linux_ps.split(" ")
            ps_info = filter(can_be_float, linux_ps)
            cur_time = time.time() - start_time
            cpu += float(ps_info[0])
            mem += float(ps_info[1])

        # get total info
        cur_time = time.time() - start_time
        print("total," + str(cur_time) + "," + str(cpu) + "," + str(mem))
        # check whether to break the loop
        check_list = []

        for f in files:
            tail = os.popen("cat " + f + " | tail -n 1")
            if "stop" in tail.read():
                check_list.append(True)
            else:
                check_list.append(False)
        if all(check_list) == True:
            break
    except IndexError:
        pass


# Copyright (c) 2010-2013 OpenStack, LLC.
# Copyright (c) 2014, 2015 Scality
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Sproxyd Disk File Interface for Swift Object Server"""

import contextlib
import hashlib
import httplib
import operator
import time
import urlparse

import eventlet
import eventlet.green.os
import swift.common.bufferedhttp
import swift.common.exceptions
import swift.common.swob
import swift.common.utils

try:
    import swift.common.splice
    HAS_NEW_SPLICE = True
except ImportError:
    HAS_NEW_SPLICE = False

from scality_sproxyd_client.exceptions import SproxydHTTPException
import swift_scality_backend.http_utils
import swift_scality_backend.splice_utils
from swift_scality_backend import utils

class DiskFileWriter(object):
    """A simple sproxyd pass-through

    Encapsulation of the write context for servicing PUT REST API
    requests. Serves as the context manager object for DiskFile's create()
    method.

    :param client_collection: Client collection to use to perform operations
    :type client_collection: `swift_scality_backend.http_utils.ClientCollection`
    :param name: standard object name
    :type name: `str`
    :param logger: Logger to use within the `DiskFileWriter`
    :type logger: `logging.Logger`
    """
    def __init__(self, client_collection, name, logger):
        self._client_collection = client_collection
        self._name = name
        self._logger = logger

        self._upload_size = 0
        headers = {
            'transfer-encoding': 'chunked'
        }
        self.logger.debug("DiskFileWriter for %r initialized", self._name)

        client = self._client_collection.get_write_client()
        self._conn, self._release_conn = client.get_http_conn_for_put(
            self._name, headers)

    def __repr__(self):
        ret = 'DiskFileWriter(client_collection=%r, name=%r, logger=%r)'
        return ret % (self._client_collection, self._name, self._logger)

    logger = property(operator.attrgetter('_logger'))

    def write(self, chunk):
        """Write a chunk of data.

        :param chunk: the chunk of data to write as a string object
        """
        self._conn.send('%x\r\n%s\r\n' % (len(chunk), chunk))
        self._upload_size += len(chunk)
        return self._upload_size

    @utils.trace
    def put(self, metadata):
        """Finalize writing the object.

        :param metadata: dictionary of metadata to be associated with the
                         object
        """
        self._conn.send('0\r\n\r\n')
        try:
            resp = self._conn.getresponse()
            msg = resp.read()
            if resp.status != 200:
                raise SproxydHTTPException("putting: %s / %s" % (
                    str(resp.status), str(msg)))
        except Exception:
            conn, self._conn = self._conn, None
            try:
                conn.close()
            except Exception:
                self.logger.exception('Failure while closing connection')
            raise

        self._release_conn()
        metadata['name'] = self._name
        self.logger.debug("Data successfully written for object : %r", self._name)

        self._client_collection.get_write_client().put_meta(
            self._name, metadata)

    def commit(self, timestamp):
        """
        Perform any operations necessary to mark the object as durable.

        :param timestamp: object put timestamp, an instance of
                          :class:`~swift.common.utils.Timestamp`
        """
        pass

class DiskFileReader(object):
    """A simple sproxyd pass-through

    Encapsulation of the read context for servicing GET REST API
    requests. Serves as the context manager object for DiskFile's reader()
    method.

    :param filesystem: internal file system object to use
    :param name: object name
    :param use_splice: if true, use zero-copy splice() to send data
    """
    def __init__(self, client_collection, name, use_splice, logger):
        self._client_collection = client_collection
        self._name = name
        self._use_splice = use_splice
        self._logger = logger

    def __repr__(self):
        ret = (
            'DiskFileReader(client_collection=%r, name=%r, use_splice=%r, '
            'logger=%r)')
        return ret % (self._client_collection, self._name, self._use_splice,
                      self.logger)

    logger = property(operator.attrgetter('_logger'))

    @utils.trace
    def __iter__(self):
        headers, data = self._client_collection.try_read(
            lambda client: client.get_object(self._name))
        return data

    @utils.trace
    def can_zero_copy_send(self):
        return self._use_splice

    @utils.trace
    def zero_copy_send(self, wsockfd):
        client = self._client_collection.get_read_client()

        object_url = urlparse.urlparse(client.get_url_for_object(self._name))
        conn = None

        with swift.common.exceptions.ConnectionTimeout(client.conn_timeout):
            conn = swift_scality_backend.http_utils.SomewhatBufferedHTTPConnection(
                object_url.netloc)

            try:
                conn.putrequest('GET', object_url.path, skip_host=False)
                conn.endheaders()
            except:
                conn.close()
                raise

        with conn:
            resp = conn.getresponse()

            if resp.status != httplib.OK:
                raise SproxydHTTPException(
                    'Unexpected response code: %s' % resp.status,
                    url=object_url.geturl(),
                    http_status=resp.status, http_reason=resp.reason)

            if resp.chunked:
                raise SproxydHTTPException(
                    'Chunked response not supported',
                    url=object_url.geturl(),
                    http_status=resp.status, http_reason=resp.reason)

            buff = resp.fp.get_buffered()
            buff_len = len(buff)

            while buff:
                written = eventlet.green.os.write(wsockfd, buff)
                buff = buff[written:]

            to_splice = resp.length - buff_len if resp.length is not None else None

            swift_scality_backend.splice_utils.splice_socket_to_socket(
                resp.fileno(), wsockfd, length=to_splice)

    @utils.trace
    def app_iter_range(self, start, stop):
        """
        Iterate over a range.

        :param start: First byte to read from (inclusive)
        :type start: int
        :param stop: Last byte to read (exclusive)
        :type stop: int
        """
        # HTTP range is inclusive on both ends.
        headers = {
            'range': 'bytes=%d-%d' % (start, stop - 1)
        }

        headers, data = self._client_collection.try_read(
            lambda client: client.get_object(self._name, headers))
        return data

    @utils.trace
    def app_iter_ranges(self, ranges, content_type, boundary, size):
        """iterate over multiple ranges."""
        if not ranges:
            yield ''
        else:
            for chunk in swift.common.swob.multi_range_iterator(
                    ranges, content_type, boundary, size, self.app_iter_range):
                yield chunk

class DiskFile(object):
    """A simple sproxyd pass-through

    :param client_collection: Client collection to use to perform operations
    :type client_collection: `swift_scality_backend.http_utils.ClientCollection`
    :param account: account name for the object
    :type account: `str`
    :param container: container name for the object
    :type container: `str`
    :param obj: object name for the object
    :type obj: `str`
    :param use_splice: if true, use zero-copy splice() to send data
    :type use_splice: `bool`
    """

    def __init__(self, client_collection, account, container, obj, use_splice,
                 logger):
        # We hash the account, container and object name so that no 'special'
        # character will get in our way.
        sha1 = hashlib.sha1()
        for part in [account, container, obj]:
            sha1.update(part)
        self._name = sha1.hexdigest()
        self._metadata = None
        self._client_collection = client_collection
        self._logger = logger

        self._account = account
        self._container = container
        self._obj = obj
        self._use_splice = use_splice

    logger = property(operator.attrgetter('_logger'))
    client_collection = property(operator.attrgetter('_client_collection'))

    def __repr__(self):
        ret = ('DiskFile(client_collection=%r, account=%r, container=%r, obj=%r, '
               'use_splice=%r, logger=%r)')
        return ret % (self._client_collection, self._account, self._container,
                      self._obj, self._use_splice, self._logger)

    @utils.trace
    def open(self):
        """Open the file and read the metadata.

        This method must populate the _metadata attribute.

        :raise DiskFileDeleted: if it does not exist
        """
        metadata = self.client_collection.try_read(
            lambda client: client.get_meta(self._name))

        if metadata is None:
            raise swift.common.exceptions.DiskFileDeleted()

        self._metadata = metadata or {}

        try:
            x_delete_at = int(self._metadata['X-Delete-At'])
        except KeyError:
            pass
        else:
            if x_delete_at <= time.time():
                raise swift.common.exceptions.DiskFileExpired(
                    metadata=self._metadata)

        return self

    @utils.trace
    def __enter__(self):
        if self._metadata is None:
            raise swift.common.exceptions.DiskFileNotOpen()
        return self

    @utils.trace
    def __exit__(self, t, v, tb):
        pass

    @utils.trace
    def get_metadata(self):
        """Provide the metadata for an object as a dictionary.

        :returns: object's metadata dictionary
        """
        if self._metadata is None:
            raise swift.common.exceptions.DiskFileNotOpen()
        return self._metadata

    @utils.trace
    def read_metadata(self):
        """Return the metadata for an object.

        :returns: metadata dictionary for an object
        """
        with self.open():
            return self.get_metadata()

    @utils.trace
    def reader(self, keep_cache=False):
        """Return a swift.common.swob.Response class compatible "app_iter" object.

        :param keep_cache: ignored. Kept for compatibility with the native
                          `DiskFile` class in Swift
        """
        dr = DiskFileReader(self.client_collection, self._name,
                            use_splice=self._use_splice,
                            logger=self.logger)
        return dr

    @utils.trace
    @contextlib.contextmanager
    def create(self, size=None):
        """Context manager to create a file.

        :param size: ignored. Kept for compatibility with the native
                     `DiskFile` class in Swift. This `create` method is
                     called externally only by the `ObjectController`
        """
        yield DiskFileWriter(self.client_collection, self._name, self.logger)

    @utils.trace
    def write_metadata(self, metadata):
        """Write a block of metadata to an object."""
        self.client_collection.get_write_client().put_meta(self._name, metadata)

    @utils.trace
    def delete(self, timestamp):
        """Perform a delete for the given object in the given container under
        the given account.

        :param timestamp: ignored. Kept for compatibility with the native
                          `DiskFile` class in Swift. This `delete` method is
                          called externally only by the `ObjectController`
        """
        self.client_collection.get_write_client().del_object(self._name)

    # Class `swift.common.utils.Timestamp` is Swift 2.0+
    if hasattr(swift.common.utils, 'Timestamp'):
        @property
        def timestamp(self):
            if self._metadata is None:
                raise swift.common.exceptions.DiskFileNotOpen()
            return swift.common.utils.Timestamp(self._metadata.get('X-Timestamp'))

        data_timestamp = timestamp

class DiskFileManager(object):
    """
    Management class for devices, providing common place for shared parameters
    and methods not provided by the DiskFile class (which primarily services
    the object server REST API layer).

    The `get_diskfile()` method is how this implementation creates a `DiskFile`
    object.
    """

    def __init__(self, conf, logger):
        """
        :param conf: caller provided configuration object
        :param logger: caller provided logger
        """
        self.logger = logger

        self.use_splice = False

        conf_wants_splice = swift.common.utils.config_true_value(
            conf.get('splice', 'no'))

        if HAS_NEW_SPLICE:
            system_has_splice = swift.common.splice.splice.available
        else:
            try:
                system_has_splice = swift.common.utils.system_has_splice()
            except AttributeError:  # Old Swift versions
                system_has_splice = False

        if conf_wants_splice and not system_has_splice:
            self.logger.warn(
                "Use of splice() requested (config says \"splice = %s\"), "
                "but the system does not support it. "
                "splice() will not be used." % conf.get('splice'))

        if conf_wants_splice and system_has_splice:
            self.use_splice = True

    def get_diskfile(self, client_collection, account, container, obj):
        return DiskFile(client_collection, account, container, obj,
                        use_splice=self.use_splice, logger=self.logger)

    def pickle_async_update(self, *args, **kwargs):
        pass


import os
import time
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.style as style
from collections import defaultdict
from finance_tracker.services.plotter_interface import PlotterInterface

style.use('seaborn-white')

class Plotter(PlotterInterface):
    def __init__(self, output_path='./plots/'):
        self.output_path = output_path
        if not os.path.exists(self.output_path):
            os.makedirs(self.output_path)

    def category_pie_plot(self, movements, id2category):
        cat2amount = defaultdict(int)
        for mv in movements:
            if mv.amount < 0:
                cat2amount[id2category[mv.category]] -= mv.amount

        labels, sizes = [], []
        for cat, val in cat2amount.items():
            if cat != 'NO_CAT':
                labels.append(cat)
                sizes.append(val)

        plt.figure()
        patches, texts, _ = plt.pie(sizes, autopct='%1.1f%%', startangle=90,
                                    wedgeprops={"edgecolor": "0", 'linewidth': 1, 'antialiased': True})
        plt.legend(patches, labels, loc="best")
        plt.axis('equal')
        plt.title('Pie chart by category')
        plt.savefig(os.path.join(self.output_path, 'pie_chart.png'), dpi=256)

    def category_monthly_barplot(self, movements, id2category):
        min_month, max_month = 12, 0
        total_expense_by_cat = {}
        for mv in movements:
            if mv.amount < 0:
                min_month = min(min_month, mv.date.month)
                max_month = max(max_month, mv.date.month)
                total_expense_by_cat[mv.category] = total_expense_by_cat.get(mv.category, 0) - mv.amount
        n_months = max_month - min_month + 1
        n_cats = len(total_expense_by_cat)
        cat2pos = {cat: i for i, (cat, count) in enumerate(sorted(total_expense_by_cat.items(), key=lambda x: -x[1]))}
        pos2cat = {v: k for k, v in cat2pos.items()}

        category_expenses = [[0] * n_months for _ in range(n_cats)]
        months = [time.strftime('%b', time.struct_time((0, min_month + i, 0,) + (0,) * 6)) for i in range(n_months)]

        for mv in movements:
            if mv.amount < 0:
                category_expenses[cat2pos[mv.category]][mv.date.month - min_month] -= mv.amount
        data = np.array(category_expenses)

        plt.figure()
        for i in range(n_cats):
            plt.bar(months, data[i], bottom=np.sum(data[:i], axis=0))
        plt.legend([id2category[pos2cat[pos]] for pos in range(n_cats)], loc="best")
        plt.title('Barplot by month and category')
        plt.savefig(os.path.join(self.output_path, 'barplot.png'), dpi=256)


'''
139. Word Break
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

Note:
The same word in the dictionary may be reused multiple times in the segmentation.
You may assume the dictionary does not contain duplicate words.
Example 1:

Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".
Example 2:

Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
             Note that you are allowed to reuse a dictionary word.
Example 3:

Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false
'''

def Validate(key,wordDict):
    flg=True
    tmp1=key.split(',')
    for l in tmp1:
        if l not in wordDict:
            flg=False
            break
    return flg

def LeetCode139(str1,wordDict):

    lst=list(str1)
    tmp=[0]*(len(str1)*2)

    fnl_lst=[]
    idx=0
    op_idx=0
    Combinations_recur(lst,tmp,idx,op_idx,fnl_lst,wordDict)
    if len(fnl_lst)==0:
        return False
    else:
        return True

def Combinations_recur(lst,tmp,idx,op_idx,fnl_lst,wordDict):

    if idx==len(lst):
        flg=Validate(''.join(tmp).strip(','),wordDict)
        if flg==True:
            fnl_lst.append(''.join(tmp).strip(','))
        return

    tmp[op_idx]=lst[idx]
    tmp[op_idx+1]=','
    Combinations_recur(lst, tmp, idx+1, op_idx+2, fnl_lst,wordDict)

    if idx+1<len(lst):
        Combinations_recur(lst, tmp, idx+1, op_idx+1, fnl_lst,wordDict)

def main():

    str1='leetcode'
    wordDict = ["leet", "code"]
    print(LeetCode139(str1,wordDict))

    str1 = 'applepenapple'
    wordDict = ["apple", "pen"]
    print(LeetCode139(str1, wordDict))

    str1 = 'catsandog'
    wordDict = ["cats", "dog", "sand", "and", "cat"]
    print(LeetCode139(str1, wordDict))

if __name__=='__main__':
    main()


import urllib
import requests
UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17'  # pylint: disable=line-too-long
HEADERS = {'User-agent': UA}
URL = 'http://api.sc2h.cn/api/maid.php'
async def maiden_chat(query, timeout=(30, 45)):
    ''' get 聊天女仆'''
    data = {
        'data': 0,
        'sign': 'user',
        'msg': query
    }
    try:
        # resp = requests.post(URL, data=data, headers=HEADERS)
        resp = requests.get(
            f'{URL}?{urllib.parse.urlencode(data)}',
            headers=HEADERS,
            timeout=timeout,
        )
        resp.raise_for_status()
    except Exception as exc:
        return str(exc)

    return resp.text


#!/usr/bin/python
import sys
import Adafruit_DHT
from prototype import Sensor

sensor = Adafruit_DHT.DHT22
pin = 26

class AirTempHumidity(Sensor):
    def read(self):
        humidity, temperature = Adafruit_DHT.read(sensor, pin)
        if humidity is not None and temperature is not None:
            temperature = 9.0 / 5.0 * temperature + 32.0
            return {"AirTemperature": temperature, "Humidity": humidity}
        else:
            return {"AirTemperature": 0, "Humidity": 0}

if __name__ == "__main__":
    ath = AirTempHumidity()
    print
    ath.read()


# celery 入口
from celery import Celery

# 为celery使用django配置文件进行设置
import os
if not os.getenv('DJANGO_SETTINGS_MODULE'):
    os.environ['DJANGO_SETTINGS_MODULE'] = 'xiaoy_malls.settings.dev'

celery_app = Celery('xiaoy')

# 加载配置
celery_app.config_from_object('celery_tasks.config')

# 注册任务
celery_app.autodiscover_tasks(['celery_tasks.sms', 'celery_tasks.email'])


from clients.models import Card
from django.db import models
from users.models import DoctorProfile
from datetime import datetime
import slog.models as slog
import simplejson as json

class PlanOperations(models.Model):
    patient_card = models.ForeignKey(Card, null=True, help_text='Карта пациента', db_index=True, on_delete=models.SET_NULL)
    direction = models.CharField(max_length=128, default=None, blank=True, null=True, help_text="Номер истории")
    date = models.DateTimeField(null=True, blank=True, help_text='Время на операцию', db_index=True)
    doc_operate = models.ForeignKey(DoctorProfile, default=None, blank=True, null=True, related_name="doc_operate", help_text='Кто опрерирует', on_delete=models.SET_NULL)
    type_operation = models.TextField(default=None, blank=True, null=True, help_text="Вид операции")
    canceled = models.BooleanField(default=False, help_text='Операция отменена')
    doc_anesthetist = models.ForeignKey(DoctorProfile, default=None, blank=True, null=True, related_name="doc_anesthetist", help_text='Кто опрерирует', on_delete=models.SET_NULL)
    doc_who_create = models.ForeignKey(DoctorProfile, default=None, blank=True, null=True, related_name="doc_create_plan", help_text='Создатель планирвоания', on_delete=models.SET_NULL)

    @staticmethod
    def save_data(data, doc_who_create):
        patient_card = Card.objects.filter(pk=data['card_pk'])[0]
        direction_obj = data['direction']
        type_operation = data.get('type_operation', '')
        doc_operate_obj = DoctorProfile.objects.filter(pk=data['hirurg'])[0]
        doc_anesthetist = data.get('doc_anesthetist', None)
        doc_anesthetist_obj = None
        if doc_anesthetist:
            doc_anesthetist_obj = DoctorProfile.objects.filter(pk=doc_anesthetist)[0]

        if data['pk_plan'] == -1:
            plan_obj = PlanOperations(
                patient_card=patient_card,
                direction=direction_obj,
                date=datetime.strptime(data['date'], '%Y-%m-%d'),
                doc_operate=doc_operate_obj,
                type_operation=type_operation,
                doc_anesthetist=doc_anesthetist_obj,
                doc_who_create=doc_who_create,
                canceled=False,
            )
            plan_obj.save()

            slog.Log(
                key=plan_obj.pk,
                type=80001,
                body=json.dumps(
                    {
                        "card_pk": data['card_pk'],
                        "direction": direction_obj,
                        "date_operation": data['date'],
                        "doc_operate": data['hirurg'],
                        "type_operation": type_operation,
                    }
                ),
                user=doc_who_create,
            ).save()
        else:
            plan_obj = PlanOperations.objects.filter(pk=data['pk_plan'])[0]
            plan_obj.doc_operate = doc_operate_obj
            plan_obj.type_operation = type_operation
            if 'doc_anesthetist' in data:
                plan_obj.doc_anesthetist = doc_anesthetist_obj
            plan_obj.doc_who_create = doc_who_create
            plan_obj.date = datetime.strptime(data['date'], '%Y-%m-%d') if '-' in data['date'] else datetime.strptime(data['date'], '%d.%m.%Y')
            plan_obj.direction = direction_obj
            plan_obj.patient_card = patient_card
            plan_obj.canceled = False
            plan_obj.save()
            slog.Log(
                key=data['pk_plan'],
                type=80002,
                body=json.dumps(
                    {
                        "card_pk": data['card_pk'],
                        "direction": direction_obj,
                        "date_operation": data['date'],
                        "doc_operate": data['hirurg'],
                        "type_operation": type_operation,
                    }
                ),
                user=doc_who_create,
            ).save()

        return plan_obj.pk

    @staticmethod
    def cancel_operation(data, doc_who_create):
        plan_obj = PlanOperations.objects.get(pk=data['pk_plan'])
        plan_obj.doc_who_create = doc_who_create
        plan_obj.canceled = not plan_obj.canceled
        plan_obj.save()
        return plan_obj.canceled


import cv2, os

def addimg(img_path, label_path, new_path):
    img = cv2.imread(img_path)
    label = cv2.imread(label_path)
    add = cv2.addWeighted(img, 1.0, label, 0.5, 0)
    path = new_path + '/' + i
    cv2.imwrite(path, add)

def walk_dir(suffix, *paths):
    dir_map = []
    for path in paths:
        for (root, dirs, files) in os.walk(path):
            for item in files:
                if item.endswith(suffix):
                    dir_map.append(item)
    return  dir_map

def dir_name(path):
    dir_names = os.listdir(path)
    return dir_names

def path_join(img_path):
    imgs_path = os.path.join(img_path, 'imgs')
    labels_path = os.path.join(img_path, 'label')
    new_path = os.path.join(img_path,'add')
    os.makedirs(new_path, exist_ok=True)
    return imgs_path, labels_path, new_path

if __name__ == '__main__':
    DATA_PATH = r'G:\add_test'
    dir_names = dir_name(DATA_PATH)
    for d in dir_names:
        img_path = os.path.join(DATA_PATH,d)
        i_path, l_path, new_path = path_join(img_path)
        for i in walk_dir('png',i_path):
            imgss_path = os.path.join(i_path,i)
            lables_path = os.path.join(l_path,i)
            addimg(imgss_path, lables_path, new_path)
        print(i_path)


from pathlib import Path
from random import randint

class Hangman:

    gallows = (
        '''
           ___
          |   |
              |
              |
              |
              |
             / \\
        ''',
        '''
           ___
          |   |
          o   |
              |
              |
              |
             / \\
        ''',
        '''
           ___
          |   |
          o   |
          |   |
              |
              |
             / \\
        ''',
        '''
           ___
          |   |
          o   |
         /|   |
              |
              |
             / \\
        ''',
        '''
           ___
          |   |
          o   |
         /|\\  |
              |
              |
             / \\
        ''',
        '''
           ___
          |   |
          o   |
         /|\\  |
         /    |
              |
             / \\
        ''',
        '''
           ___
          |   |
          o   |
         /|\\  |
         / \\  |
              |
             / \\
        '''
    )

    def __init__(self, language='en'):
        self.language = language
        self.dictionary = self.generate_dictionary(language)
        self.word = self.get_random_word()
        self.correct_letters = set()
        self.missed_letters = set()

    @staticmethod
    def generate_dictionary(language):
        dict_file = 'lang_pl.txt' if language == 'pl' else 'lang_en.txt'
        dict_dir = Path('../dictionaries')
        if not dict_dir.exists():
            dict_dir = Path('dictionaries')
        dict_path = Path(dict_dir.resolve(), dict_file)
        with open(dict_path, 'r') as file:
            words = {
                index: word for index, word in enumerate(file.read().splitlines())
            }
        return words

    def get_random_word(self):
        return self.dictionary[randint(0, len(self.dictionary))]

    def update_state(self, letter):
        if letter in self.word:
            self.correct_letters.add(letter)
        else:
            self.missed_letters.add(letter)

    def is_win(self):
        return self.correct_letters == set(self.word)

    def is_loss(self):
        return len(self.missed_letters)+1 == len(self.gallows)

    def reset_state(self):
        self.word = self.get_random_word()
        self.correct_letters = set()
        self.missed_letters = set()

    def show_gallows(self):
        return self.gallows[len(self.missed_letters)]

    def show_word_to_guess(self):
        letters = [letter.upper()
                   if letter in self.correct_letters else '-'
                   for letter in self.word]
        return ' '.join(letters)

    def show_missed_letters(self):
        return ' '.join(sorted(self.missed_letters))


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

def sanitize(time_string):
    if '-' in time_string:
        (mins,secs) = time_string.split('-',1)
        return mins + '.' + secs
    elif ':' in time_string:
        (mins,secs) = time_string.split(':',1)
        return mins + '.' + secs
    else:
        return time_string

def get_data(file_name):
    #dic_value = {}
    try:
        with open(file_name) as file:
            times_list = file.readline().strip().split(",");
            #dic_value['name'] = list.pop()
            #dic_value['birthday'] = list.pop()
            #dic_value['times'] = list
            #return dic_value
            return ({'name':times_list.pop(0),
                     'birthday':times_list.pop(0),
                     'times':sorted(set([sanitize(t) for t in times_list]))[0:3]})
    except IOError as err:
        print("deal with " + file_name + " err:" +str(err) );
        return (None)

james = get_data("james.txt")
print(type(james))
print(james['name'] + "'s fast times:'" + str(james['times']) )

mikey = get_data("mikey.txt")
print(mikey['name'] + "'s fast times:'" + str(mikey['times']) )

juli  = get_data("julie.txt")
print(juli['name'] + "'s fast times:'" + str(juli['times']) )

sarah  = get_data("sarah.txt")
print(sarah['name'] + "'s fast times:'" + str(sarah['times']) )


import time
from Base.base import Base
from Base.OperExcel import write_xlsBycolName_append
from selenium import webdriver
from selenium.webdriver.common.by import By
from Data.DataMgnt.TestResult import TestResultOper as TR
from Base import ReadConfig
from Base.SysPara import SysPara as sp
from PageObj.ngboss.mainpage import MainPage
from PageObj.ngboss.login_page import LoginPage
from Base.Mylog import LogManager
from Check.PageCheck import PageAssert
from Common.TestAsserts import Assertion as Assert
from Common.function import getDigitFromStr
from PageObj.order.BizCommon.ElementPartBase import MainPlanSelectPart
from PageObj.order.BizCommon.ElementPartBase import DealUserCommon
from PageObj.order.BizCommon.ElementPartBase import SelectElements
from PageObj.order.BizCommon.ElementPartBase import PageCommonPart
from Check.RuleCheck import RuleCheckBefore
from Check.DataCheck import DataCheck as Dc

# logger = LogManager('test').get_logger_and_add_handlers(1,is_add_stream_handler=True, log_path=ReadConfig.log_path, log_filename=time.strftime("%Y-%m-%d")+'.log' )
logger = LogManager('ProdChangePage').get_logger_and_add_handlers(1, log_path=ReadConfig.log_path, log_filename=time.strftime("%Y-%m-%d")+'.log' )

class ProdChangePage(Base):
    def open_base(self):
        # self.driver.get(rc.get_ngboss('url'))
        self.driver.maximize_window()

    def openProductChange(self):
        LoginPage(self.driver).login()  # 登录
        # MainPage(self.driver).open_CataMenu('crm9000', 'crm9200', 'crmw912',menuPath='page/order.page.pc.person.changeproduct.ChangeProduct')  # 进入产品变更页面
        MainPage(self.driver).open_CataMenuNew(funcId='crm9231')
        logger.info('进入产品变更页面')

    def backProdChgPage(self):
        '''新增优惠、服务元素后点击返回'''
        loc_backPopup = (By.XPATH,'//*[@id="elementListpopup-ItemList"]/div[1]/div[1]')
        self.isElementDisplay(loc_backPopup,'click')

    def acceptChgMainProduct(self,accessNum,productId,elementList=[],scene='ChgMainProduct'):
        '''
        个人业务-变更主产品
        :param accessNum:服务号码
        :param productId:主产品编码
        :param elementList:可选组、必选组元素列表
        :return:
        '''
        title = '主产品变更测试记录%s' % accessNum
        self.add_dochead(title)
        self.openProductChange()
        self.screen_step("进入产品变更菜单")
        authUser = DealUserCommon(self.driver).AuthUserInfo(accessNum) #用户鉴权,包含了用户鉴权都结果和规则校验信息
        Assert().assertTrue(authUser['IsAuthSuc'],msg='用户鉴权失败')   #做个鉴权认证
        self.screen_step("点击变更按钮")
        Btn_changeProduct = (By.ID,'changeButton')
        self.isElementDisplay(Btn_changeProduct,'click') #点击产品变更按钮
        self.sleep(1)
        self.screen_step("选择主套餐")
        MainPlanSelectPart(self.driver).MainProductSel(productId,elementList)
        RuleCheckBefore(self.driver).checkRule(scene) #规则校验
        PageCommonPart(self.driver).submit() #点击提交
        submitMsg = PageAssert(self.driver).assertSubmit()
        if Assert().verifyassertIn('业务受理成功',submitMsg):
            Dc().dealMainOrder(orderId=getDigitFromStr(submitMsg)) #处理主订单，如果状态是Y或者X 修改成0
        logger.info('业务受理信息：{}'.format(submitMsg))
        self.screen_step('点击提交,受理信息：{}'.format(submitMsg))
        self.save_docreport(title)
        return submitMsg

    def acceptAddElements(self,accessNum,elementList=[],scene='AddElements'):
        '''
        个人业务-新增服务
        :param accessNum:服务号码
        :param productId:主产品编码
        :param elementList:资费和服务列表
        字典数组，包含 OFFER_CODE 和 Offer_type 2个key
        eg :[{"OFFER_CODE":"120000008174","OFFER_TYPE":"S"},{"OFFER_CODE":"120010122813","OFFER_TYPE":"D"}]
        :return:
        '''
        title = '新增资费_服务测试记录%s' % accessNum
        self.add_dochead(title)
        self.openProductChange()
        self.screen_step("进入产品变更菜单")
        authUser = DealUserCommon(self.driver).AuthUserInfo(accessNum) #用户鉴权,包含了用户鉴权都结果和规则校验信息
        # TR().updateRuleCheckInfo(sceneCode=scene,msg=authUser['msg'])
        Assert().assertTrue(authUser['IsAuthSuc'],msg='用户鉴权失败')   #做个鉴权认证
        #新增优惠按钮
        li_addElement = (By.ID,'addElement')
        self.isElementDisplay(li_addElement,'click')
        self.screen_step("选择资费或者服务订购")
        SelectElements(self.driver).addElements(elementList=elementList,scene=scene)
        self.backProdChgPage() #点击回到产品变更主页面
        PageCommonPart(self.driver).submit() #点击提交
        submitMsg = PageAssert(self.driver).assertSubmit()
        logger.info('业务受理信息：{}'.format(submitMsg))
        if Assert().verifyassertIn('业务受理成功',submitMsg):
            Dc().dealMainOrder(orderId=getDigitFromStr(submitMsg)) #处理主订单，如果状态是Y或者X 修改成0
            Dc().retOrderTrace(orderId=getDigitFromStr(submitMsg),route='jour42')
        self.screen_step('点击提交,受理信息：{}'.format(submitMsg))
        self.save_docreport(title)
        return submitMsg

    def acceptDelElements(self,accessNum,scene='DelElements',elementList=[],):
        '''
        个人业务-新增服务
        :param accessNum:服务号码
        :param productId:主产品编码
        :param elementList:资费和服务列表
        字典数组，包含 OFFER_CODE 和 Offer_type 2个key
        eg :[{"OFFER_CODE":"120000008174","OFFER_TYPE":"S"},{"OFFER_CODE":"120010122813","OFFER_TYPE":"D"}]
        :return:
        '''
        title = '删除资费_服务测试记录%s' % accessNum
        self.add_dochead(title)
        self.openProductChange()
        self.screen_step("进入产品变更菜单")
        authUser = DealUserCommon(self.driver).AuthUserInfo(accessNum) #用户鉴权,包含了用户鉴权都结果和规则校验信息
        # TR().updateRuleCheckInfo(sceneCode=scene,msg=authUser['msg'])
        Assert().assertTrue(authUser['IsAuthSuc'],msg='用户鉴权失败')   #做个鉴权认证
        #删除资费或者服务
        self.screen_step("选择资费或者服务删除")
        SelectElements(self.driver).delElements(elementList)
        PageCommonPart(self.driver).submit() #点击提交
        submitMsg = PageAssert(self.driver).assertSubmit()
        logger.info('业务受理信息：{}'.format(submitMsg))
        if Assert().verifyassertIn('业务受理成功',submitMsg):
            Dc().dealMainOrder(orderId=getDigitFromStr(submitMsg)) #处理主订单，如果状态是Y或者X 修改成0
        self.screen_step('点击提交,受理信息：{}'.format(submitMsg))
        self.save_docreport(title)
        return submitMsg

if __name__ == '__main__':
    driver = webdriver.Chrome()
    test = ProdChangePage(driver)
    # test.acceptChgMainProduct(accessNum='15874139293',productId='99913849',elementList=[{"OFFER_CODE":"99650338","OFFER_TYPE":"D"}]) #必选优惠列表
    # test.acceptAddElements(accessNum='18229967646',elementList=[{"OFFER_CODE":"18013676","OFFER_TYPE":"S"},{"OFFER_CODE":"18009531","OFFER_TYPE":"D"}],scene='AddElements')
    test.acceptDelElements(accessNum='15111048902',elementList=[{"OFFER_CODE":"204","OFFER_TYPE":"S"}])


from methods import *
from numpy.random import seed
seed(0)
from e_config import *

###############################
#### run model and get acc ####
###############################

def run_cnn(train_file, test_file, num_classes, input_size, percent_dataset, word2vec):

	#initialize model
	model = build_cnn(input_size, word2vec_len, num_classes)

	#load data
	train_x, train_y = get_x_y(train_file, num_classes, word2vec_len, input_size, word2vec, percent_dataset)
	test_x, test_y = get_x_y(test_file, num_classes, word2vec_len, input_size, word2vec, 1)

	#implement early stopping
	callbacks = [EarlyStopping(monitor='val_loss', patience=3)]

	#train model
	model.fit(	train_x,
				train_y,
				epochs=100000,
				callbacks=callbacks,
				validation_split=0.1,
				batch_size=1024,
				shuffle=True,
				verbose=0)
	#model.save('checkpoints/lol')
	#model = load_model('checkpoints/lol')

	#evaluate model
	y_pred = model.predict(test_x)
	test_y_cat = one_hot_to_categorical(test_y)
	y_pred_cat = one_hot_to_categorical(y_pred)
	acc = accuracy_score(test_y_cat, y_pred_cat)

	#clean memory???
	train_x, train_y, model = None, None, None
	gc.collect()

	#return the accuracy
	#print("data with shape:", train_x.shape, train_y.shape, 'train=', train_file, 'test=', test_file, 'with fraction', percent_dataset, 'had acc', acc)
	return acc

###############################
### get baseline accuracies ###
###############################

def compute_baselines(writer):

	#baseline computation
	for size_folder in size_folders:

		#get all six datasets
		dataset_folders = [size_folder + '/' + s for s in datasets]
		performances = []

		#for each dataset
		for i in range(len(dataset_folders)):

			#initialize all the variables
			dataset_folder = dataset_folders[i]
			dataset = datasets[i]
			num_classes = num_classes_list[i]
			input_size = input_size_list[i]
			word2vec_pickle = dataset_folder + '/word2vec.p'
			word2vec = load_pickle(word2vec_pickle)

			train_path = dataset_folder + '/train_aug_st.txt'
			test_path = 'size_data_t1/test/' + dataset + '/test.txt'
			acc = run_cnn(train_path, test_path, num_classes, input_size, 1, word2vec)
			performances.append(str(acc))

		line = ','.join(performances)
		print(line)
		writer.write(line+'\n')

###############################
############ main #############
###############################

if __name__ == "__main__":

	writer = open('baseline_cnn/' + get_now_str() + '.csv', 'w')

	for i in range(0, 10):

		seed(i)
		print(i)
		compute_baselines(writer)


# -*- coding: utf-8 -*-
import scrapy
from pachong5.items import Pachong5Item

class Aijia1Spider(scrapy.Spider):
    name = 'aijia1'
    allowed_domains = ['bj.5i5j.com']
    start_urls = ['https://bj.5i5j.com/ershoufang/n/' + str(x) for x in range(1,6)]

    def parse(self, response):
        xiaoqus = response.xpath('/html/body/div[6]/div[1]/div[2]/ul/li')
        for xiaoqu in xiaoqus:
            item = Pachong5Item()
            item['xiaoqu'] = xiaoqu.xpath('div[2]/h3/a/text()').extract_first()
            item['zongjia'] = xiaoqu.xpath(
            '/html/body/div[6]/div[1]/div[2]/ul/li[6]/div[2]/div[1]/div/p[1]/strong/text()').extract_first()
            yield item


import random as rnd
import math as m
import pandas as pd
import matplotlib.pyplot as plt
import time
timecheck0 =time.time()

# Начальные параметры
XMIN = 1
XMAX = 10
YMIN = 0
YMAX = 10
# Лямбда
H = 1E-5

# Задание вероятнойстей ядерной реакции
#   [0;rlim1] - ничего не происходит
#   (rlim1;rlim2] - поглощение нейтрона, пропадание частицы
#   (rlim2,1] - деление ядра и появление 2 новых нейронов
RLIM1 = 0.5
RLIM2 = 0.8

#N - начальное число частиц
N = 20
TIME = 600

#Контроль стержней
NTARGET = 10
K = 1

def creating_reactor(nnumber: int = 5):
    A = pd.DataFrame({'X': [], 'Y': []})
    for i in range(nnumber):
        x = XMIN + (XMAX - XMIN) * rnd.random()
        y = YMIN + (YMAX - YMIN) * rnd.random()
        A = A.append({'X': x, 'Y': y}, ignore_index=True)
    return A

def main():
    """Основной код программы
    """
    # Базовые функции контроля
    l = lambda h, r: -h * m.log(1 - r)
    control = lambda n: (RLIM2- RLIM1)*(1+K*(n-NTARGET)/NTARGET)

    control_RLIM2 = RLIM2
    # Создание реактора
    Reactor = creating_reactor(N)

    # Запуск деления
    tick = 0
    Rlen = N
    total_n = [Rlen]

    #Количество тиков, каждый тик - 1 дейтсвие всех нейтронов
    while tick < TIME and Rlen != 0 and len(
            Reactor.index) < 1000:

        # Перебор с конца
        nt = Rlen - 1

        # Проверка на включение и выключение системы контроля
        if Rlen< NTARGET: control_RLIM2 = control(Rlen)
        elif Rlen> NTARGET*2:control_RLIM2 = RLIM2

        #Перебор всех нейтронов
        while nt >= 0:

            x0 = Reactor.loc[nt].X
            y0 = Reactor.loc[nt].Y

            # Случайное направление движения
            fi = 0 + 2 * m.pi * rnd.random()
            r = rnd.random()
            x1 = x0 + l(H, r) * m.cos(fi)
            y1 = y0 + l(H, r) * m.sin(fi)

            # Выбираем случайный исход взаимодействия
            result = rnd.random()
            # Исправить на хранение изменений прямо в ячейке, без создания переменных x,y...
            if x1 > XMAX or x1 < XMIN or y1 < YMIN or y1 > YMAX:
                Reactor = Reactor.drop(index=nt)

            elif result <= RLIM1:
                Reactor.loc[nt].X = x1
                Reactor.loc[nt].Y = y1

            elif RLIM1 < result <= control_RLIM2:
                Reactor = Reactor.drop(index=nt)

            elif control_RLIM2 < result:
                Reactor.loc[nt].X = x1
                Reactor.loc[nt].Y = y1
                Reactor = Reactor.append({'X': x1, 'Y': y1}, ignore_index=True)
            nt -= 1

        Rlen = len(Reactor.index)
        Reactor.reset_index(drop=True, inplace=True)
        total_n.append(Rlen)
        tick += 1

    plt.figure(figsize=(10, 10), dpi=80, facecolor='w', edgecolor='k')
    plt.plot(total_n, label="$f(x)$")
    plt.show()
    timecheck1 =time.time()
    print(timecheck1-timecheck0)

main()


"""
Copyright (c) 2019 Intel Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from pathlib import Path

from .annotation_converters import BaseFormatConverter, save_annotation, make_subset
from .config import ConfigValidator, StringField, PathField, ListField, DictField, BaseField, NumberField, ConfigError
from .utils import JSONDecoderWithAutoConversion, read_json, get_path, contains_all
from .representation import BaseRepresentation
from .data_readers import DataReaderField

class DatasetConfig(ConfigValidator):
    """
    Specifies configuration structure for dataset
    """
    name = StringField()
    annotation = PathField(optional=True, check_exists=False)
    data_source = PathField(optional=True, check_exists=False)
    dataset_meta = PathField(optional=True, check_exists=False)
    metrics = ListField(allow_empty=False, optional=True)
    postprocessing = ListField(allow_empty=False, optional=True)
    preprocessing = ListField(allow_empty=False, optional=True)
    reader = DataReaderField(optional=True)
    annotation_conversion = DictField(optional=True)
    subsample_size = BaseField(optional=True)
    subsample_seed = NumberField(floats=False, min_value=0, optional=True)

class Dataset:
    def __init__(self, config_entry):
        self._config = config_entry
        self.batch = 1
        self.iteration = 0
        dataset_config = DatasetConfig('Dataset')
        dataset_config.validate(self._config)
        annotation, meta = None, None
        use_converted_annotation = True
        self._images_dir = Path(self._config.get('data_source', ''))
        if 'annotation' in self._config:
            annotation_file = Path(self._config['annotation'])
            if annotation_file.exists():
                annotation = read_annotation(get_path(annotation_file))
                meta = self._load_meta()
                use_converted_annotation = False
        if not annotation and 'annotation_conversion' in self._config:
            annotation, meta = self._convert_annotation()

        if not annotation:
            raise ConfigError('path to converted annotation or data for conversion should be specified')

        subsample_size = self._config.get('subsample_size')
        if subsample_size:
            subsample_seed = self._config.get('subsample_seed', 666)
            if isinstance(subsample_size, str):
                if subsample_size.endswith('%'):
                    subsample_size = float(subsample_size[:-1]) / 100 * len(annotation)
            subsample_size = int(subsample_size)
            annotation = make_subset(annotation, subsample_size, subsample_seed)

        if use_converted_annotation and contains_all(self._config, ['annotation', 'annotation_conversion']):
            annotation_name = self._config['annotation']
            meta_name = self._config.get('dataset_meta')
            if meta_name:
                meta_name = Path(meta_name)
            save_annotation(annotation, meta, Path(annotation_name), meta_name)

        self._annotation = annotation
        self._meta = meta
        self.size = len(self._annotation)
        self.name = self._config.get('name')

    @property
    def annotation(self):
        return self._annotation

    def __len__(self):
        return self.size

    @property
    def metadata(self):
        return self._meta

    @property
    def labels(self):
        return self._meta.get('label_map', {})

    def __call__(self, context, *args, **kwargs):
        batch_annotation = self.__getitem__(self.iteration)
        self.iteration += 1
        context.annotation_batch = batch_annotation
        context.identifiers_batch = [annotation.identifier for annotation in batch_annotation]

    def __getitem__(self, item):
        if self.size <= item * self.batch:
            raise IndexError

        batch_start = item * self.batch
        batch_end = min(self.size, batch_start + self.batch)
        batch_annotation = self._annotation[batch_start:batch_end]

        return batch_annotation

    @staticmethod
    def set_image_metadata(annotation, images):
        image_sizes = []
        data = images.data
        if not isinstance(data, list):
            data = [data]
        for image in data:
            image_sizes.append(image.shape)
        annotation.set_image_size(image_sizes)

    def set_annotation_metadata(self, annotation, image, data_source):
        self.set_image_metadata(annotation, image.data)
        annotation.set_data_source(data_source)

    def _load_meta(self):
        meta_data_file = self._config.get('dataset_meta')
        return read_json(meta_data_file, cls=JSONDecoderWithAutoConversion) if meta_data_file else None

    def _convert_annotation(self):
        conversion_params = self._config.get('annotation_conversion')
        converter = conversion_params['converter']
        annotation_converter = BaseFormatConverter.provide(converter, conversion_params)
        annotation, meta = annotation_converter.convert()

        return annotation, meta

def read_annotation(annotation_file: Path):
    annotation_file = get_path(annotation_file)

    result = []
    with annotation_file.open('rb') as file:
        while True:
            try:
                result.append(BaseRepresentation.load(file))
            except EOFError:
                break

    return result


import tokenz
import methodMang
import time

time.sleep(3)

class Interpreter:

    def __init__(self):
       self.tokens = ""
       self.pos = 0

    def crunch(self):
        self.pos += 1
        return self.tokens.pop(0)

    def get_args(self, toks): #Function arg getter, input a list of tokens like... out (123 456) ... and  will return [123, 456]
        # print("Get_args toks passed: " + str(toks))
        t = toks
        t_vals = []
        for t_ in t:
            t_vals.append(t_.val)
       #  print("t_vals: "+str(t_vals))
        if "(" not in t_vals:
            # print("Not a func")
            return ("notAfunc", t[1:], t[0])
        self.crunch()
        indt = 0
        u = 0
        while t.pop(0).val != "(":
            pass
        while True:
            if t[indt].val == "(": u += 1
            if t[indt].val == ")" and u > 0: u -= 1
            indt += 1
            if t[indt].val == ")" and u == 0:
                break

        if (indt == 0):
            return ([t[0]], t[1:])
        else:
            return (t[0:indt], t[indt+1:])

    def eval(self, code): # Code is string...
        self.tokens = tokenz.tokenize(str(code))
        # print("%s tokenized = %s" %(code, str(self.tokens)))
        self.pos = 0
        returns = []
        while self.tokens != []:
            try:
                tok = self.tokens[self.pos]
                if tok.type == "call":

                    args = self.get_args(self.tokens)
                    # print("args got: %s" % str(args))
                    if args[0] == "notAfunc":
                        self.tokens = args[1]
                        returns.append(args[2])
                        # print("Not a func, appending %s" % str(args[2].val))
                    else:
                        self.tokens = args[1]
                        returns.append(methodMang.Call(tok.val, args[0]).run())

                elif tok.type == "numb":
                    self.crunch()

                    returns.append(tok)
                elif tok.type == "str":
                    self.crunch()

                    returns.append(tok)
                elif tok.type == "bool":
                    self.crunch()

                    returns.append(tok)
            except IndexError:
                break
        # print(returns)
        return returns

if __name__ == "__main__":
    print("Scotch Programming Language")
    print("Created by Daniel Sage 2016")
    print("Running Interactive prompt... \n")
    while True:
        try:
            code = input(">>> ")
            if code == "kill":
                break
            Interpreter().eval(code)
        except KeyboardInterrupt:
            pass
        except Exception as e:
            print("ERROR: %s; %s" % (e.__class__.__name__, str(e)))


import os
import sys
from argparse import ArgumentParser
from datetime import datetime, timezone, timedelta
import json

import numpy as np
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision.models as models

from dataloader import RetinopathyLoader

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

class BasicBlock(nn.Module):
    out_extend = 1
    def __init__(self, in_ch, out_ch, stride=1, downsample=None):
        super(BasicBlock, self).__init__()
        self.conv1 = nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=stride,
                     padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(out_ch)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = nn.Conv2d(out_ch, out_ch, kernel_size=3,
                     padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(out_ch)
        self.downsample = downsample

    def forward(self, x):
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        out = self.conv2(out)
        out = self.bn2(out)

        residual = self.downsample(x) if self.downsample is not None else x
        out += residual
        out = self.relu(out)
        return out

class Bottleneck(nn.Module):
    out_extend = 4
    def __init__(self, in_ch, out_ch, stride=1, downsample=None):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(in_ch, out_ch, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(out_ch)
        self.conv2 = nn.Conv2d(out_ch, out_ch, kernel_size=3, stride=stride,
                               padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(out_ch)
        self.conv3 = nn.Conv2d(out_ch, out_ch * 4, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(out_ch * 4)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)
        out = self.conv3(out)
        out = self.bn3(out)

        residual = self.downsample(x) if self.downsample is not None else x
        out += residual
        out = self.relu(out)
        return out

class ResNet(nn.Module):
    def __init__(self, block, layers, out_class=5, in_ch=3):
        self.input_size = 64
        super(ResNet, self).__init__()
        self.conv1 = nn.Conv2d(in_ch, 64, kernel_size=7, stride=2, padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._customize_layer(block, 64, layers[0])
        self.layer2 = self._customize_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._customize_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._customize_layer(block, 512, layers[3], stride=2)
        self.avgpool = nn.AvgPool2d(7, stride=1)
        self.fc = nn.Linear(51200 * block.out_extend, out_class)

    def _customize_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.input_size != planes * block.out_extend:
            downsample = nn.Sequential(
                nn.Conv2d(self.input_size, planes * block.out_extend,
                            kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(planes * block.out_extend),
            )
        layers = []
        layers.append(block(self.input_size, planes, stride, downsample))
        self.input_size = planes * block.out_extend
        for i in range(1, blocks):
            layers.append(block(self.input_size, planes))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        res = x.view(x.size(0), -1)
        out = self.fc(res)
        return out

def test_accuracy(net, dataset):
    net.eval()
    test_size = dataset.data_size
    # test_size = 100
    accuracy_res = []
    pred_list = []
    gt_list = []
    for index in range(test_size):
        x, gt = dataset[index]
        t_x = torch.unsqueeze(torch.tensor(x), dim=0).type(torch.FloatTensor).to(device)
        pred_y = net(t_x)
        pred_y = torch.max(pred_y.cpu(), 1)[1].data.numpy()[0]
        accuracy_res.append(pred_y == gt)
        pred_list.append(int(pred_y))
        gt_list.append(int(gt))

    accuracy = float(np.array(accuracy_res).astype(int).sum()) / float(test_size)
    net.train()
    return accuracy * 100, pred_list, gt_list

def train_accuracy(pred_y, gt):
    gt = np.array(gt)
    pred_y = np.array(pred_y)
    accuracy = float((pred_y == gt).astype(int).sum()) / float(len(gt))
    return accuracy * 100

def handle_param(args):
    if args.model == 'resnet18':
        if args.untrained:
            net = ResNet(BasicBlock, [2, 2, 2, 2])
        else:
            net = models.resnet18(pretrained=True)
            net.avgpool = nn.AdaptiveAvgPool2d(1)
            net.fc = nn.Linear(512, 5)
    elif args.model == 'resnet50':
        if args.untrained:
            net = ResNet(Bottleneck, [3, 4, 6, 3])
        else:
            net = models.resnet50(pretrained=True)
            net.avgpool = nn.AdaptiveAvgPool2d(1)
            net.fc = nn.Linear(512 * 4, 5)
    else:
        raise TypeError('Error: {} model type not defined.'.format(args.model))

    if args.optimizer == 'adam':
        optimizer = torch.optim.Adam(
                        net.parameters(), lr=args.learning_rate,
                        weight_decay=args.weight_decay)
    elif args.optimizer == 'rmsp':
        optimizer = torch.optim.RMSprop(
                        net.parameters(), lr=args.learning_rate,
                        momentum=args.momentum, weight_decay=args.weight_decay)
    elif args.optimizer == 'sgd':
        optimizer = torch.optim.SGD(
                        net.parameters(), lr=args.learning_rate,
                        momentum=args.momentum, weight_decay=args.weight_decay)
    else:
        raise TypeError('Error: {} optimizer type not defined.'.format(args.optimizer))

    if args.loss_function == 'CrossEntropy':
        loss_function = nn.CrossEntropyLoss()
    else:
        raise TypeError('Error: {} loss_function type not defined.'.format(args.loss_function))
    return net.to(device), optimizer, loss_function

def main(args):
    train_data = RetinopathyLoader('data', 'train')
    test_data = RetinopathyLoader('data', 'test')
    train_loader = Data.DataLoader(dataset=train_data, batch_size=args.batch, shuffle=True)
    net, optimizer, loss_function = handle_param(args)
    full_step = ((train_data.data_size // args.batch) +\
                ((train_data.data_size % args.batch)>0)) * args.epochs

    train_tag = '_untrained' if args.untrained else '_pretrained'
    file_name = args.model + train_tag + '_' +\
                args.optimizer + '_ep' + str(args.epochs) + '_b' + str(args.batch) +\
                '_lr' + str(args.learning_rate) + '_wd' + str(args.weight_decay)
    if args.load:
        net.load_state_dict(torch.load(args.load))
        test_acc, pred, gt = test_accuracy(net, test_data)

        with open(args.model + train_tag + '.json', 'w') as f:
            json.dump({
                'pred_y': pred,
                'gt': gt,
                'class': list(range(5)),
            }, f)
        print('test_acc: {:.4f} %'.format(test_acc))
        return
    # start training
    max_acc = 0
    count = 0
    acc_dict = {'train'+train_tag: [], 'test'+train_tag: []}

    print(datetime.now(timezone(timedelta(hours=8))).strftime("%m-%d %H:%M"), 'start training...')
    for epoch in range(args.epochs):
        print('-'*10, 'epoch', epoch+1, '-'*10)
        y_list, gt_list = [], []
        for b_x, b_y in train_loader:
            count += 1
            b_x = b_x.type(torch.FloatTensor).to(device)
            b_y = b_y.to(device)
            output = net(b_x)
            loss = loss_function(output, b_y.long())
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            y_list.extend(torch.max(output, 1)[1].data.tolist())
            gt_list.extend(b_y.data.tolist())
            if count % args.step == 0:
                print(datetime.now().strftime("%m-%d %H:%M"))
                print('({} / {}) loss: {:.4f} | train_acc: {:.2f} %'.format(
                        count, full_step, loss, train_accuracy(y_list, gt_list)))

        test_acc, _, _ = test_accuracy(net, test_data)
        if test_acc > max_acc:
            max_acc = test_acc
            torch.save(net.state_dict(), file_name + '.pkl')
        acc_dict['train'].append(train_accuracy(y_list, gt_list))
        acc_dict['test'].append(test_acc)
        print(datetime.now().strftime("%m-%d %H:%M"))
        print('({} / {}) train_acc: {:.2f} % | test_acc: {:.2f} %'.format(
                count, full_step, train_accuracy(y_list, gt_list), test_acc))
        with open(file_name + '.json', 'w') as f:
            json.dump({
                'x': list(range(args.epochs)),
                'y_dict': acc_dict,
                'title': args.model + train_tag,
            }, f)
    print('[*] max test accuracy: {} %'.format(max_acc))

def get_args():
    parser = ArgumentParser()
    parser.add_argument("-s", "--step", help="when to print log", type=int, default=1000)
    parser.add_argument("-b", "--batch", help="batch size", type=int, default=4)
    parser.add_argument("-ep", "--epochs", help="your training target", type=int, default=10)
    parser.add_argument("-m", "--model", help="resnet18 | resnet50", type=str, default='resnet18')
    parser.add_argument("-ut", "--untrained", help="model pretrained(default)", action='store_true')
    parser.add_argument("-opt", "--optimizer", help="adam | rmsp | sgd", type=str, default='sgd')
    parser.add_argument("-lr", "--learning-rate", help="learning rate", type=float, default=1e-3)
    parser.add_argument("-wd", "--weight_decay", help="weight decay", type=float, default=5e-4)
    parser.add_argument("-mom", "--momentum", help="momentum factor", type=float, default=0.9)
    parser.add_argument("-lf", "--loss-function", help="loss function", type=str, default='CrossEntropy')
    parser.add_argument("-load", "--load", help="your pkl file path", type=str, default='')
    return parser.parse_args()

if __name__ == "__main__":
    main(get_args())

    sys.stdout.flush()
    sys.exit()


import os
from machine import SD

def sd_setup():
    try:
        sd = SD()
        os.mount(sd, '/sd')
        print("SD card mounted")
        # try:
        #     os.remove('/sd/www/leader_mesh_list.txt')
        # except:
        #     print('did not delete')
        try:
            f = open('/sd/www/ack_log.txt', 'r')
            print("Already a ACK log, trimmed to last 50 ACKs")
            acks = f.readlines()
            f.close()
            os.remove('/sd/www/ack_log.txt')
            n = open('/sd/www/ack_log.txt', 'w+')
            for i in acks[-50:]:
                n.write(i)
            n.close()
        except:
            try:
                os.mkdir('/sd/www')
                f = open('/sd/www/ack_log.txt', 'w+')
                f.write("ACK log:\n")
            except:
                f = open('/sd/www/ack_log.txt', 'w+')
                f.write("ACK log:\n")
            print("ACK Log created")
        f.close()

        try:
            f = open('/sd/www/status_log.txt', 'r')
            print("Already a status log, trimmed to last 50 entries.")
            status = f.readlines()
            f.close()
            os.remove('/sd/www/status_log.txt')
            n = open('/sd/www/status_log.txt', 'w+')
            for i in status[-50:]:
                n.write(i)
            n.close()
        except:
            try:
                os.mkdir('/sd/www')
                f = open('/sd/www/status_log.txt', 'w+')
                f.write("Status log:\n")
            except:
                f = open('/sd/www/status_log.txt', 'w+')
                f.write("Status log:\n")
            print("Status Log created")
        f.close()

        try:
            f = open('/sd/www/chat.txt', 'r')
            print("Already a chat log, trimmed to last 50 entries.")
            chats = f.readlines()
            f.close()
            os.remove('/sd/www/chat.txt')
            n = open('/sd/www/chat.txt', 'w+')
            for i in chats[-50:]:
                n.write(i)
            n.close()
        except:
            try:
                os.mkdir('/sd/www')
                f = open('/sd/www/chat.txt', 'w+')
                f.write("Chat log:\n")
            except:
                f = open('/sd/www/chat.txt', 'w+')
                f.write("Chat log:\n")
            print("chat Log created")
        f.close()

        try:
            f = open('/sd/www/wschat.html', 'r')
            print("Chat is on SD card")
            c = open('/flash/www/wschat.html', 'r')
            count_of_f = len(f.read())
            count_of_c = len(c.read())
            f.close()
            c.close()
            print("Check if wschat has changed")
            if count_of_c != count_of_f:
                os.remove('/sd/www/wschat.html')
                copy('/flash/www/wschat.html', '/sd/www/wschat.html')
                print("Copied new wshchat")

        except:
            copy('/flash/www/wschat.html', '/sd/www/wschat.html')
            print("WSChat now on SD card")

        try:
            f = open('/sd/www/index.html', 'r')
            c = open('/flash/www/index.html', 'r')
            count_of_f = len(f.read())
            count_of_c = len(c.read())
            c.close()
            f.close()
            print("Did the index change?")
            if count_of_c != count_of_f:
                os.remove('/sd/www/index.html')
                copy('/flash/www/index.html', '/sd/www/index.html')
                print("copied new index")
        except:
            copy('/flash/www/index.html', '/sd/www/index.html')
            print("Index now on SD card")

        try:
            f = open('/sd/www/style.css', 'r')
            print("Style is already on SD card")
            f.close()
        except:
            copy('/flash/www/style.css', '/sd/www/style.css')
            print("Style now on SD card")

        try:
            print("Check Node Config status")
            f = open('/sd/www/node_config.txt', 'r')
            f.close()
            print("Node Config is on SD Card")

        except:
            try:
                os.mkdir('/sd/www')
                copy('/flash/node_config.txt', '/sd/www/node_config.txt')
                print("Node config is now on SD card")
            except:
                copy('/flash/node_config.txt', '/sd/www/node_config.txt')
                print("Node config is now on SD card")

        # try:
        #     print("Check Pymesh Config status")
        #     f = open('/sd/www/pymesh_config.json', 'r')
        #     f.close()
        #     print("Pymesh Config is on SD Card")
        #
        #
        # except:
        #         copy('/flash/pymesh_config.json', '/sd/www/pymesh_config.json')
        #         print("Pymesh config is now on SD card")

        try:
            print("Check Leader Mesh list status")
            f = open('/sd/www/leader_mesh_list.txt', 'r')
            f.close()
            print("Leader Mesh list is on SD Card")

        except:
            copy('/flash/lib/leader_mesh_list.txt', '/sd/www/leader_mesh_list.txt')
            print("Leader Mesh list is now on SD card")

        try:
            f = open('/sd/www/sms.txt', 'r')
            print("Already a SMS log, trimmed to last 50 SMSs")
            sms = f.readlines()
            f.close()
            os.remove('/sd/www/sms.txt')
            n = open('/sd/www/sms.txt', 'w+')
            for i in sms[-50:]:
                if i != '\r\n':
                    n.write(i)
            n.close()
        except:
            try:
                os.mkdir('/sd/www')
                f = open('/sd/www/sms.txt', 'w+')
                f.write("SMS log:\n")
            except:
                f = open('/sd/www/sms.txt', 'w+')
                f.write("SMS log:\n")
            print("SMS Log created")
        f.close()

    except:
        print("SD card not loaded, chat not saved")

def copy(s, t):
    try:
        f = open(t, 'rb')
    except:
        f = open(t, 'wb')
    s = open(s, "rb")
    while True:
        b = s.read(4096)
        print(b)
        if not b:
           break
        f.write(b)
    f.close()
    s.close()


def quickSort(self, A, n):
    self.quick_sort(A, 0, n - 1)
    return A

def quick_sort(self, A, l, r):
    if l < r:
        less, more = self.partition(A, l, r)
        self.quick_sort(A, l, less)
        self.quick_sort(A, more, r)

def partition(self, A, l, r):
    less = l - 1
    more = r
    while l < more:
        if A[r] < A[l]:
            more -= 1
            self.swap(A, l, more)
        elif A[r] > A[l]:
            less += 1
            self.swap(A, l, less)
            l += 1
        else:
            l += 1
    self.swap(A, more, r)
    return less, more + 1

def swap(self, A, l, r):
    tmp = A[l]
    A[l] = A[r]
    A[r] = tmp


import tensorflow as tf
import numpy as np
x_data = np.random.randn(20,1)
w_real = [10]
b_real = 2

noise = np.random.randn(1,20)*0.1
y_data = np.matmul(w_real, x_data.T) + b_real + noise

x = tf.placeholder(tf.float32,shape=[None,1])
y_true = tf.placeholder(tf.float32,shape=None)

w = tf.Variable([[0]],dtype=tf.float32,name='weights')
b = tf.Variable(0,dtype=tf.float32,name='bias')
y_pred = tf.matmul(w,tf.transpose(x)) + b
loss = tf.reduce_mean(tf.square(y_true - y_pred))

learning_rate = 0.5
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()
NUM_STEPS = 10
wb_ = []
with tf.Session() as sess:
    sess.run(init)
    for step in range(NUM_STEPS):
        sess.run(train,{x: x_data, y_true: y_data})
        if (step > -1 ): #% 5 == 0):
            print("step: ", step,' ', sess.run([w,b]))
            wb_.append(sess.run([w,b]))
    print('step: ', 10, ' ', sess.run([w,b]))


# kMeans object tracking
import cv2
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

cap = cv2.VideoCapture(0)

#orange = np.uint8([[[255,69,0 ]]])
#hsv_orange = cv2.cvtColor(orange,cv2.COLOR_BGR2HSV)
#print(hsv_orange)

while(1):

    # Take each frame
    ret, frame = cap.read()
    img = cv2.cvtColor(frame[200:400, 200:400], cv2.COLOR_BGR2RGB)
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    img = img.reshape((img.shape[0] * img.shape[1],3)) #represent as row*column,channel number
    clt = KMeans(n_clusters=3) #cluster number
    clt.fit(img)

    # gaussian filter
    # frameBlur = cv2.GaussianBlur(frame,(5,5),0)

    # define range of color in HSV
    lower_hsv = np.array([145,100,150])
    upper_hsv = np.array([175,255,255])

    # Threshold the HSV image to get only desired colors
    mask = cv2.inRange(hsv, lower_hsv, upper_hsv)
    contours,hierarchy = cv2.findContours(mask, 1, 2)

    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame,frame, mask= mask)

    cnt = contours[0]
    M = cv2.moments(cnt)

    if M['m00'] != 0:
        cx = int(M['m10']/M['m00'])
        cy = int(M['m01']/M['m00'])
        cv2.rectangle(frame, (cx-5, cy-5), (cx+5, cy+5), (0, 255, 0))

    cv2.rectangle(frame, (200, 200), (400, 400), clt.cluster_centers_[0], thickness = 10)

    cv2.imshow('frame',frame)
    cv2.imshow('mask',mask)
    cv2.imshow('res',res)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

img = cv2.imread("colors.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

img = img.reshape((img.shape[0] * img.shape[1],3)) #represent as row*column,channel number
clt = KMeans(n_clusters=3) #cluster number
clt.fit(img)