#===============================================================================
# Corsi Christophe
#
# This is a subclass of the MainWindow class
#
# The heritage works like the following
#
# MainWindow => MainAcausalGraph=>MainAcausalTree=>MainAcausalTable=>MainAcausalDocumentation=>
# MainCausalGraph=>MainCausalTree=>MainCausalTable=>MainCausalDocumentation=>MainMainTools=> MainInterface
#
# Only the last item of this heritage should be invoked (MainInterface)
#===============================================================================
from copy import copy
import os
import pickle
import sys
import threading

from PyQt5.Qt import QCursor

from CompositionWindow import CompositionWindow
from EditWindow import EditWindow
from ErrorWindow import ErrorWindow
from GraphicalManager import LinkItem, BlockItem
from LinkWindow import LinkWindow
from MainWindow import MainWindow
from SaveWindow import SaveWindow
import Config
import XmlOperator

class MainAcausalGraph( MainWindow ):

    def addAcausalBlockInGraph( self, inputBlock, posX = 0, posY = 0, localMousePosition = False ):
        '''
        Add a block in the graphic view, containing the title of the block given as parameter
        (o) for optionnal
        @param inputBlock: Block This is the Block to add to the graphical view
        @param: posX: int (o) integer with the X position
        @param: posY: int (o) integer with the Y position
        @param: localMousePosition: Boolean (o) If true : posX and posY in global coordinates, else in scene coordinates
        '''
        if type( inputBlock ) != BlockItem:
            if localMousePosition == False:
                # If parameters are given, the function adds the item to the coordinates
                self.aScene.addItem( BlockItem( self.aScene, copy( inputBlock.block ) , posX, posY ) )
            else:
                # If no information is give, the function adds the item to the cursor position
                # pos has its origin on the top left of the screen
                pos = QCursor.pos()
                # pos1 has its origin on the top left of the widget
                pos1 = self.ui.aCompositionView.mapFromGlobal( pos )
                # pos2 has its center in the middle of the item's coordinate system
                pos2 = self.ui.aCompositionView.mapToScene( pos1 )
                newBlock = BlockItem( self.aScene, inputBlock.block, pos2.x(), pos2.y() )
                self.aScene.addItem( newBlock )

    def editFunction( self ):
        '''
        This functions allows a block in the graphical editor to have its name edited
        @param None:
        @return: None
        '''
        if len( self.aScene.selectedItems() ) != 1:
            return
            # we only act if one item is selected

        for item in self.aScene.selectedItems():
            if type( item ) == BlockItem:
                # if it's a block we open the block name editor
                self.aEditWindow = EditWindow( item, self.aScene )
                self.aEditWindow.show()

            elif type( item ) == LinkItem:
                # if it's a link we open the link editor
                self.aLinkWindow = LinkWindow( item )

    def aDeleteFunction( self ):
        '''
        This function deletes the selected items on the acausal graphic view
        '''
        garbage = list()
        for i in self.aScene.selectedItems():
            # we remove the item from the graphical scene
            # different behaviour considering the type of the item
            try:
                if type( i ) == BlockItem:
                    self.aScene.removeItem( i )
            except:
                print( 'LOG: MainAcausalGraph.aDeleteFunction -> ', sys.exc_info()[0], sys.exc_info()[1] )
            if type( i ) == BlockItem:
                garbage.append( i )
                for linkItem in i.linksDict.values():
                    garbage.append( linkItem )
            elif type( i ) == LinkItem:
                i.GBlock1.delLink( i.GBlock2 )
                i.GBlock2.delLink( i.GBlock1 )
        # we now clean the garbage
        garbage = set( garbage )
        for j in garbage:
            if type( j ) == LinkItem:
                try:
                    j.GBlock1.delLink( linkItem.GBlock2 )
                except:
                    if Config.isDebug:
                        print( "first block not possible to reach" )
                        print( "item one has ", j.GBlock1.linksDict )
                        print( "item two has ", j.GBlock2.linksDict )
                try:
                    j.GBlock2.delLink( linkItem.GBlock1 )

                except:
                    if Config.isDebug:
                        print( 'second item not possible to reach' )
                        print( "item two has ", j.GBlock2.linksDict )
                        print( "item one has ", j.GBlock1.linksDict )
                self.aScene.removeItem( j )
            del j

    def aLink( self ):

        '''
        This function links two blocks togeter
        '''
        # we make a new thread
        if len( self.aScene.selectedItems() ) != 1:
            return
        # if only one item has been selected
        # this pattern is used to reduce the complexity of the code
        for item in self.aScene.selectedItems():
            # we will have only one item
            self.AFirstBlockToLink = item
        self.aLinkingMode = True
        self.aLinkThread = threading.Thread( target = self.aLinkingProcess )

    def aLinkingProcess( self ):
        '''
        Function that is stocked in the thread and called when the second block is selected
        '''
        # TODO: add a timer for timeout
        # timeout=Config.defaultTimeOut
        # if timeout is not None:
        #    print("begin timer")
        #    QtCore.QTimer.singleShot(timeout, self.aLinkThread.stop()   )
        if len( self.aScene.selectedItems() ) != 1:
            return
        # we will accept only one item selected
        if self.linkTarget == None:
            return
        line = LinkItem( self.aScene, self.AFirstBlockToLink, self.linkTarget )
        self.aScene.addItem( line )
        self.listLinkItems.append( line )
        self.aLinkingMode = False
        self.lastLinkItem = line

    def callLinkWindow( self ):
        '''
        Function that calls the opening of the link window
        '''
        self.aLinkWindow = LinkWindow( self.lastLinkItem )

    def saveBlock( self ):
        '''
        Function used to save the current block into the tree and write it into the xml files
        '''
        for item in self.aScene.selectedItems():
            if type( item ) == BlockItem:
                onlyItemToSave = item
        self.saveBlockWindow = SaveWindow( onlyItemToSave )

    def callDecomposition( self ):
        '''
        Function only used to be linked with the button
        '''
        self.decomposeABlock()
        return

    def decomposeABlock( self, blockItem = None ):
        '''
        Function that handles the historic view of any compound object
        Opens an error message if it's a primitive block
        This function is based on the presence of a dumping file
        '''
        listDir = os.listdir( Config.defaultDumpingFolder )

        if blockItem == None:
            itemSelected = self.aScene.selectedItems()[0]  # we only take one item
        else:
            itemSelected = blockItem

        if itemSelected == None:
            return

        if type( itemSelected.block.id ) == type( [] ):
            identificator = itemSelected.block.id[0]
        else:
            identificator = itemSelected.block.id
        for i in listDir:
            # looking at the files in the dumping folder
            if identificator == str( i ):
                # we found the dumping file
                unserializer_in = open( Config.defaultDumpedFileName.format( str( i ) ), "rb" )  # rb for 'readingbytes'
                try:

                    decompositionScene = pickle.load( unserializer_in )
                except:
                    print( 'LOG: MainAcausalGraph.decomposeABlock->', sys.exc_info()[1] )
                    return
                unserializer_in.close()
                if type( self.aScene.selectedItems()[0] ) == BlockItem:
                    # This list allows us to have multiple window, which is required in deep structure analysis
                    self.aScene.selectedItems()[0].subSceneComposition = decompositionScene  # It wont erase the previous one if it already has one stocked
                    composition = self.aScene.selectedItems()[0].subSceneComposition
                    decomposedItem = self.aScene.selectedItems()[0]
                    parentItem = self.aScene.selectedItems()[0]
                    # self.decompositionWindowList.append( CompositionWindow( self.aScene.selectedItems()[0].subSceneComposition, self.aScene.selectedItems()[0], self.aScene.selectedItems()[0] ) )
                    self.decompositionWindowList.append( CompositionWindow( composition , decomposedItem, parentItem ) )
                    self.compositionFirstInitialization = False
                return
        self.errorWindow = ErrorWindow( Config.ErrorMessagePrimitiveObject )
        return

    def fusionBlocks( self ):
        '''
        Function called when the regroup button is clicked
        It takes the current selected items, delete them and replace it with a new block made of their unification.
        The equations are merged. If two equations are similar, only one will be kept
        '''
        resultItem = None
        for item in self.aScene.selectedItems():
            if type( item ) == BlockItem:
                if resultItem == None:
                    resultItem = item
                else:
                    resultItem = item.fusionWith( resultItem )
        self.aScene.addItem( resultItem )
        self.aDeleteFunction()
        return

    def mergeBlocks( self ):
        '''
        Takes the currently selected items and merge them into one super block, with double equations if necessary.
        '''
        try:
            resultItem = None
            otherItems = []
            firstItem = None
            links = []
            for item in self.aScene.selectedItems():
                if type( item ) == BlockItem:
                    if firstItem == None:
                        firstItem = item
                    else:
                        otherItems.append( item.block )
                elif type( item ) == LinkItem:
                    links.append( item )

            try:
                resultBlock = firstItem.block.sum( otherItems )
                resultBlock.mergeComposingBlocksLinks( links )
                resultBlock.refreshEquationDict()
                resultItem = BlockItem( self.aScene, resultBlock )
            except:
                print( 'LOG: MainAcausalGraph.mergeBlocks-> Part 1 ', sys.exc_info()[0], sys.exc_info()[1] )
                return
            if resultItem == None:
                return
            resultItem.block.documentationFileName = Config.documentationNameStructure.format( resultItem.block.name )
            resultItem.block.refreshID()
            # we write the documentation of the file
            write = XmlOperator.XmlWriter( resultItem )
            write.createDocumentation( Config.documentationNameStructure.format( resultItem.block.name ) )
            # We dump the whole scene into a file
            # itemsToSerialize = [ x for x in self.aScene.selectedItems() if ( type( x ) == BlockItem or type( x ) == LinkItem ) ]
            try:
                serialized_in = self.aScene.selectedItems()
                # resultItem.block.id is a list containing one value
                if type( resultItem.block.id ) == type( [] ):
                    dumpingName = resultItem.block.id[0]
                elif type( resultItem.block.id ) != type ( [] ) :
                    dumpingName = resultItem.block.id
                # starting serialization
                serialized_out = open( Config.defaultDumpedFileName.format( dumpingName ) , "wb" )  # wb for writting bytes
                pickle.dump( serialized_in, serialized_out )
                serialized_out.close()
            except:
                print( 'LOG: MainAcausalGraph.mergeBlocks -> serialization failed ->  ', sys.exc_info()[0], sys.exc_info()[1] )
            # we finally delete the other old items
            self.aScene.addItem( resultItem )
            self.aDeleteFunction()
            return
        except:
            print( 'LOG: MainAcausalGraph.mergeBlocks-> Part 2 ', sys.exc_info()[0], sys.exc_info()[1] )


import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from pylab import rcParams
rcParams['figure.figsize'] = 10, 10

x = list()
y = list()
z = list()
v = list()

file = open('5000.txt', 'r')
lines = file.readlines()
# 0 1 2: pos, 5 6 7: Acoustrophoretic force, 8 9 10: Total force, 11: Radiation potential
for line in lines:
    tmp = list(map(lambda f: float(f), line.split()))
    if -1e-9 < tmp[2] and tmp[2] < 1e-9:
        x.append(tmp[0])
        y.append(tmp[1])
        z.append(tmp[2])
        v.append(tmp[11])
file.close()

#plt.axes([-0.05, -0.1, 0.25, 0.5])
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_title('force_z')
ax.view_init(elev=18, azim=30)
ax.dist=10
ax.set_xlim3d(-0.05, 0.2)
ax.set_ylim3d(-0.05, 0.2)
ax.set_zlim3d(0, 0.4)

cmap = plt.cm.get_cmap("hot")
l = ax.scatter(x, y, z, cmap=cmap, c=v, s=20)

fig.colorbar(l)
plt.show()


#!/usr/bin/env python

import gpu_replay_memory
import tensorflow as tf

# Example from melee-ai repository.
def create_model(input_shape, num_actions, model_name, create_network_fn,
                 learning_rate, batch_size, gamma, target_network=None,
                 replay_memory_sample_ops=None):
    """Create the Q-network model."""
    is_target_model = (target_network is None)
    with tf.name_scope(model_name):
        if replay_memory_sample_ops is not None:
            input_frames = replay_memory_sample_ops[3 if is_target_model else 0]
        else:
            input_frames = tf.placeholder(tf.float32, [None, input_shape],
                                          name ='input_frames')

        q_network = create_network_fn(
            input_frames, input_shape, num_actions,
            trainable=(not is_target_model))

        mean_max_Q =tf.reduce_mean(
            tf.reduce_max(q_network, axis=[1]), name='mean_max_Q')

        train_step = None
        if replay_memory_sample_ops is not None and not is_target_model:
            _, rewards_op, actions_op, __, is_terminal_op = (
                replay_memory_sample_ops)
            y = rewards_op + tf.where(
                is_terminal_op,
                tf.zeros(shape=[batch_size], dtype=tf.float32),
                tf.scalar_mul(gamma, tf.reduce_max(target_network, axis=1)))
            # From https://github.com/tensorflow/tensorflow/issues/206.
            enumerate_mask = tf.range(0, batch_size * int(q_network.shape[1]),
                                      q_network.shape[1])
            gathered_outputs = tf.gather(tf.reshape(q_network, [-1]),
                                         enumerate_mask + actions_op,
                                         name='gathered_outputs')
            loss = mean_huber_loss(y, gathered_outputs)
            train_step = tf.train.RMSPropOptimizer(learning_rate,
                decay=RMSP_DECAY, momentum=RMSP_MOMENTUM,
                epsilon=RMSP_EPSILON).minimize(loss)

    model = {
        'batch_size': batch_size,
        'q_network' : q_network,
        'input_frames' : input_frames,
        'train_step': train_step,
        'mean_max_Q' : mean_max_Q,
    }
    return model


line_1 = input().split()
line_2 = input().split()
line_3 = input().split()
win_positions = ['012', '345', '678', '036', '147', '258', '048', '246']
game_string = list(map(int, line_1 + line_2 + line_3))
ones = ''
twos = ''

for i in range(len(game_string)):
    if game_string[i] == 1:
        ones += str(i)
    if game_string[i] == 2:
        twos += str(i)

if ones in win_positions:
    print('First player won')
elif twos in win_positions:
    print('Second player won')
else:
    print('Draw!')


# -*- coding: utf-8 -*-
"""
Created on Mon Nov 18 23:16:41 2019

@author: User
"""

import torch
from model_dcgan import Discriminator,Generator # 변경
from matplotlib.pyplot import imshow, imsave
import os
from PIL import Image
import numpy as np
import torch
import torch.nn as nn
from torchvision.utils import save_image
import argparse

def get_sample_single_image(G, counts ,n_noise,DEVICE,file_path,save):
    #torch.radn(a,b) => b 크기의 데이터 a개를 만들어라
    z = torch.randn(counts, n_noise, 1, 1).to(DEVICE)
    y_hat = torch.squeeze(G(z), 1) # (100, 28, 28)
#    print(y_hat)
#    result = (y_hat.cpu().data.numpy()*255).astype(int)
#    print(result.shape)
#    print(type(result))
#    print(result)

    if save == True:
        for index in range(y_hat.shape[0]):
            new_image=y_hat.data[index]
            save_image(new_image,file_path+'/%s_%s.png'%(index,opt.cls_num.split('_')[0]))
            print(index)
            #print(new_image)

    return ''

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--dataset', type=str)
    parser.add_argument('--cls_num', type=str)
    parser.add_argument('--nc', required=True,type=int, help='MNIST 1, cifar10 3')
    parser.add_argument('--ngf', type=int,default=64, help="path to netG (to continue training)")
    parser.add_argument('--counts', type=int)
    parser.add_argument('--model_name', help='Normal_GAN | DC_GAN')
    parser.add_argument('--model_epoch', type=str)
    parser.add_argument('--save', type=bool)
    parser.add_argument('--ngpu', type=int, help='num of gpu', default=1) #추가

    opt = parser.parse_args()

    dir_path=r'./models/'
    path = dir_path+opt.model_name+'/'+opt.dataset+opt.cls_num+'/'+opt.model_epoch
#    path=r'.\models\G.pkl'

    DEVICE=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    n_noise=100

    model=Generator(opt.ngpu, n_noise, ngf=opt.ngf, nc=opt.nc).to(DEVICE) # 변경
    model.load_state_dict(torch.load(path))
    model.eval()

    file_path=r'./oversampled_data/' + opt.model_name + '/' + opt.dataset +'/'+ opt.cls_num
    if not os.path.exists(file_path):
        os.makedirs(file_path)

    counts=opt.counts
    get_sample_single_image(model, opt.counts, n_noise,DEVICE,file_path,opt.save)


# ATTEMPT ONE
#
# Time Complexity: O(mn)
#
# For any given matrix m*n, during the first iteration,
# we are unable to mutate the matrix otherwise everything
# will be zeros. Instead we store which rows and columns
# are affected and in the second iteration O(2mn) we
# modify any elements that have a row or column stored
# in the first iteration.

def zeroMatrix(m):
    row = []
    column = []
    for i in range(0,len(m)):
        for j in range(0, len(m[0])):
            if(m[i][j] == 0):
                if i not in row:
                    row.append(i)
                if j not in column:
                    column.append(j)
    for i in range(0,len(m)):
        for j in range(0, len(m[0])):
            if(i in row or j in column):
                m[i][j] = 0

Cracking the Coding Interview/Chapter 1 | Arrays and Strings/1.8.py


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

import tornado.web
import json, datetime, urllib
from dao.request_dao import RequestDao
from base_handler import BaseHandler
from models import RequestEncoder

class RequestHandler(BaseHandler):
    def initialize(self):
        super(RequestHandler, self).initialize()
        self.__dao = RequestDao(self._session)

    def get(self):
        project_id = self.get_argument("project")
        project_id = int(project_id)

        page_index = self.get_argument("page_index", 1)
        page_index = int(page_index)

        page_size = self.get_argument("page_size", 10)
        page_size = int(page_size)

        key = "project_%s_page_index_%s" % (project_id, page_index)
        vo = self._cache_client.get(key)
        if vo is None:
            vo = self.__dao.get_items(page_index, page_size, project_id)
            self._cache_client.add(key, vo, 7 * 24 * 60 * 60)

        self.render('request.html', requests=vo.get_items(), \
            project_id=project_id)

    def post(self):
        data = self.get_body_argument("data")
        data = urllib.unquote(data)
        data = json.loads(data)

        method = data["method"]
        params = data["params"]

        if (method == "get_item"):
            self.__get_item(params)

        if (method == "modify_item"):
            self.__modify_item(params)

        if (method == "add_item"):
            self.__add_item(params)

        if (method == "delete_item"):
            self.__delete_item(params)

    def __get_item(self, params):
        request_id = params["id"]
        request_id = int(request_id)

        request = self.__dao.get_item(request_id)

        self.write(json.dumps(request, cls=RequestEncoder))

    def __modify_item(self, params):
        request_id = params["id"]
        request_id = int(request_id)

        request = self.__dao.get_item(request_id)
        request.name = params["name"]
        request.method = params["method"]
        request.modify_time = datetime.datetime.now()

        self.__dao.merge_item(request)

        result = {"code": 200}
        self.write(result)

    def __add_item(self, params):
        project_id = params["project_id"]
        name = params["name"]
        method = params["method"]

        self.__dao.add_item(project_id, name, method)

        result = {"code": 200}
        self.write(result)

    def __delete_item(self, params):
        request_id = params["id"]
        request_id = int(request_id)

        self.__dao.delete_item(request_id)

        result = {"code": 200}
        self.write(result)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 19 17:11:42 2019

@author: sebas
"""

# ídem clase pasada con implementación "más copada"

import torch

# defino la clase del modelo (por ahora con una sola capa oculta)
# el guión bajo _ es el self
# mínimo para def clase: funciones init y forward
# arquitectura y aprendizaje

class mlp(torch.nn.Module):
    def __init__(_, isize, hsize, osize):
        super().__init__() # inicializo la super clase
        _.L1 = torch.nn.Linear(isize, hsize)
        _.L2 = torch.nn.Linear(hsize, osize)

    def forward(_,x):
        h = torch.tanh(_.L1(x))
        y = torch.tanh(_.L2(h))
        return y

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

P = 5000
N = 8
#H = N+1  # capa oculta con N+1 unidades
H = N + N//2 # RECORDAR: el H=N+1 es el MÍNIMO para que funcione
M = 1

x = torch.randn(P,N).sign() ### rand es para distribucion uniforme
z = torch.prod(x,dim=1).view(P,M) # el signo 8 bit como entrenamiento. NO es separable!

#x = x.to(device)
#z = z.to(device) # ¿Quizás adentro del while esto?

lr = 1e-2
E, t = 1.,0 # inicializo un error y un contador de epocas

model = mlp(N,H,M)#.to(device)
costf = torch.nn.MSELoss()

# agregamos optimización por stochastic gradient descent
# otra opción es el RProp o el Adam

optim = torch.optim.SGD(model.parameters(), lr=0.1)

model.train() # lo pongo al modelo en modo entrenamiento. Ver docs, funciona para algunos módulos

while E>0.001 and t<10001:
#    h = torch.cat((x,bias),dim=1).mm(w1).tanh()
#    y = torch.cat((h,bias),dim=1).mm(w2).tanh()
#    print(model)
    y = model(x)
#    y = model.foward(x)

#    error = (y-z).pow(2).sum() ### error para los datos con ruido
#    error.backward() ### calcula gradiente y lo guarda en w

#    model.zero_grad() # tengo que resetear el gradiente antes de recalcular los pesos
    optim.zero_grad() # conviene resetear el gradiente por optim

    error = costf(y,z) # calculo el torch.cuda.is_available()rror entre target y modelo
    error.backward() # calcula el gradiente y lo guarda
#    optim.backward() # calcula el gradiente y lo guarda

#    with torch.no_grad():
#        w1 -= lr*w1.grad
#        w2 -= lr*w2.grad
#        w1.grad.zero_() ### del tensor existente, lo completa con zeros
#        w2.grad.zero_() ### del tensor existente, lo completa con zeros

#        for param in model.parameters():
#            param -= lr*param.grad
    optim.step() #recalcular por gradiente descendiente

    E = error.item()
    t += 1 #paso de época

    if t%100 ==0:
        print(t,E)

model.eval() # lo pongo al modelo en modo validación. Ver docs, funciona para algunos módulos

xp = torch.randn(P,N).sign()
zp = torch.prod(xp,dim=1).view(P,M)

with torch.no_grad():
    yp = model(xp)
    Ep = costf(yp, zp)

print("Error generalización", Ep.item()) #Es muy malo!


import re

from runnerBase import RunnerBase

class RunnerMocha(RunnerBase):

    # NOTE - we allow either open paren or space to support both javascript and
    # coffee-script-like syntaxes.
    _JS_CALL_STRING = r"""[\( ]("[^"]*"|'[^']*'),"""
    _TEST_REGEX = re.compile("^([ \t]*)it" + _JS_CALL_STRING, re.M)
    _DESCRIBE_REGEX = re.compile("^([ \t]*)describe" + _JS_CALL_STRING, re.M)
    _HEADER_LINE = re.compile(r"^\d+\.\.\d+$", re.M)
    _ERROR_LINE = re.compile(r"^  [a-zA-Z0-9]*Error:.*$")
    _ERROR_CONTINUE_LINE = re.compile(r"^ +at .*:\d+:\d+\)?$")

    def cacheOptionsForBuild(self):
        self._mochaCompilers = self.options.get('mocha_compilers', '')

    def doRunner(self, writeOutput, shouldStop):
        realCmd = self.cmd
        mochaOptions = ""

        # mocha_compilers is a system-wide setting, not a project setting,
        # se we get it from options rather than settings.
        compilers = self._mochaCompilers
        if compilers:
            mochaOptions += ' --compilers '
            mochaOptions += ','.join(compilers)

        realCmd = realCmd.replace("{mocha_options}", mochaOptions)

        writeOutput("Running tests: " + realCmd)
        self._nextTestLines = None  # Set to None before the header line
        self._lastTest = -1
        self._tests = {}
        self._countOk = 0
        self._countFailed = 0
        self._inError = False
        self._allOutput = ""
        # Use first failure as paths storage
        self._runProcess(realCmd, echoStdout = self._processOutput)

        self.writeOutput('')
        self.writeOutput("=" * 80)
        if len(self.failures) > 0:
            self.writeOutput("All failures")
            self.writeOutput("=" * 80)
            for t in self._tests.values():
                if not t['ok']:
                    self.writeOutput("== " + t['test'] + " ==")
                    self.writeOutput('\n'.join(t['errorLines']))
            self.writeOutput("=" * 80)
        self.writeOutput("{0} ok, {1} not ok".format(self._countOk,
                self._countFailed))

    def runnerSetup(self, paths = [], tests = {}):
        cmd = "mocha --reporter tap{mocha_options} "

        if paths:
            cmd += self._escapePaths(paths)
            # Remember our paths, since we re-use them for failed tests.
            self._paths = paths
        elif tests:
            # Mocha doesn't tell us which paths contain which tests, so just
            # keep a set of paths and add them to our cmd as we go
            paths = set()
            testNames = set()
            for filePath, testSpecs in tests.iteritems():
                paths.add(filePath)
                for ts in testSpecs:
                    testNames.add(ts)

            cmd += self._escapePaths(paths)
            cmd += ' --grep "^'
            cmd += '$|^'.join(testNames)
            cmd += '$"'
            # and keep _paths the same as it was in tests
            self._paths = list(paths)
        else:
            cmd = "echo 'No tests to run.'"
            self._paths = []

        self.cmd = cmd

    def _findTestFromLine(self, viewText, lineMatch, lineStartPos):
        indent = len(lineMatch.group(1))
        testName = lineMatch.group(2)[1:-1]
        for sel in reversed(list(self._DESCRIBE_REGEX.finditer(viewText))):
            if sel.start() > lineStartPos:
                continue
            clsIndent = len(sel.group(1))
            if clsIndent < indent:
                # Parent describe!
                indent = clsIndent
                testName = sel.group(2)[1:-1] + " " + testName
        return testName

    def _processLine(self, line):
        if self._inError:
            if self._ERROR_CONTINUE_LINE.match(line.rstrip()):
                self._tests[self._lastTest]['errorLines'].append(line.rstrip())
                return
            # No longer reading error lines, leave this mode
            self._inError = False

        if self._nextTestLines is None:
            # Looking for header line or initialization errors
            if self._HEADER_LINE.match(line.strip()):
                # No longer looking for header
                self._nextTestLines = []
            else:
                # If it's not the header line, print it, since it's possibly
                # error or debug information
                self.writeOutput(line.rstrip())
        elif line.startswith('ok '):
            _, testId, text = line.split(' ', 2)
            if testId == self._lastTest:
                return
            self._lastTest = testId
            self._tests[testId] = { 'test': text.strip(), 'ok': True,
                    'lines': self._nextTestLines, 'errorLines': [] }
            self._nextTestLines = []
            self._countOk += 1
            self.writeOutput('.', end = '')
        elif line.startswith('not ok '):
            _, _, testId, text = line.split(' ', 3)
            if testId == self._lastTest:
                return
            self._lastTest = testId
            self._tests[testId] = { 'test': text.strip(), 'ok': False,
                    'errorLines': self._nextTestLines }
            self._nextTestLines = []
            self._countFailed += 1
            # Mocha doesn't tell us which file a test came from... so....
            for f in self._paths:
                self.failures.setdefault(f, []).append(text.strip())
            self.writeOutput('E', end = '')
        elif self._ERROR_LINE.match(line.rstrip()):
            self._tests[self._lastTest]['errorLines'].append(line.rstrip())
            # Set a flag so we look for file listings ("    at ...")
            self._inError = True
        else:
            self._nextTestLines.append(line.rstrip())

    def _processOutput(self, output):
        self._allOutput += output
        while True:
            parts = self._allOutput.split('\n', 1)
            if len(parts) == 1:
                break
            self._processLine(parts[0] + '\n')
            self._allOutput = parts[1]


from datetime import datetime, date, timedelta
from collections import Counter
from prettytable import PrettyTable

def get_last_2_week(start, end):
    audit_list = []
    for add_day in range(start, end):
        # print(add_day)
        yesterday = (date.today() + timedelta(days=add_day)).strftime("%Y-%m-%d")  # 昨天日期
        # print(yesterday)
        filename = "audit_douyin/audit_douyin_cid.txt." + yesterday
        # filename="/Users/ludanqing/python/qly_project/pyecharts/pyecharts/app/audit_douyin/audit_douyin_cid.txt." + yesterday
        try:
            for line in open(filename):
                user = line.split("\t")[1].split("=")[1]
                audit_list.append(user)
        except:
            pass
    return audit_list
        # get_file_content("audit_douyin/audit_douyin_cid.txt." + yesterday)

def get_current_week():
    monday, sunday = date.today(), date.today()
    one_day = timedelta(days=1)
    while monday.weekday() != 0:
        monday -= one_day
    while sunday.weekday() != 6:
        sunday += one_day

    return monday

def get_stat_content():
    last_week_start = (date.today() - get_current_week()).days + 7
    last_week_end = (date.today() - get_current_week()).days

    today = get_last_2_week(0, 1)
    # print("=======")
    yesterday = get_last_2_week(-1, 0)
    lastyesterday = get_last_2_week(-2, -1)
    lastweek = get_last_2_week(-last_week_start, -last_week_end)
    thisweek = get_last_2_week(-last_week_end, 1)

    today_list = Counter(today).items()
    yesterday_list = Counter(yesterday).items()
    lastyesterday_list = Counter(lastyesterday).items()
    lastweek_list = Counter(lastweek).items()
    thisweek_list = Counter(thisweek).items()

    user_tree = {}
    mset = set()
    for ele, v in yesterday_list:
        mset.add(ele)
    for ele, v in lastyesterday_list:
        mset.add(ele)
    for ele, v in thisweek_list:
        mset.add(ele)
    for ele, v in lastweek_list:
        mset.add(ele)
    for ele, v in today_list:
        mset.add(ele)
    for user in mset:
        user_tree[str(user)] = []
        yesterday_flag = False
        lastyesterday_flag = False
        lastweek_flag = False
        thisweek_flag = False
        today_flag = False
        for ele, v in yesterday_list:
            if str(ele) == str(user):
                yesterday_flag = True
                user_tree[str(user)].append(v)
        if not yesterday_flag:
            user_tree[str(user)].append(0)

        for ele, v in lastyesterday_list:
            if str(ele) == str(user):
                lastyesterday_flag = True
                user_tree[str(user)].append(v)
        if not lastyesterday_flag:
            user_tree[str(user)].append(0)

        for ele, v in thisweek_list:
            if str(ele) == str(user):
                thisweek_flag = True
                user_tree[str(user)].append(v)
        if not thisweek_flag:
            user_tree[str(user)].append(0)

        for ele, v in lastweek_list:
            if str(ele) == str(user):
                lastweek_flag = True
                user_tree[str(user)].append(v)
        if not lastweek_flag:
            user_tree[str(user)].append(0)

        for ele, v in today_list:
            if str(ele) == str(user):
                today_flag = True
                user_tree[str(user)].append(v)
        if not today_flag:
            user_tree[str(user)].append(0)

    x = PrettyTable(["打标人",  "昨日", "前日", "本周", "上周", "今日"])
    v0_total = 0
    v1_total = 0
    v2_total = 0
    v3_total = 0
    v4_total = 0
    for k, v in user_tree.items():
        x.add_row([k, v[0], v[1], v[2], v[3], v[4]])
        v0_total += v[0]
        v1_total += v[1]
        v2_total += v[2]
        v3_total += v[3]
        v4_total += v[4]
    x.add_row(['总计', v0_total, v1_total, v2_total, v3_total, v4_total])
    # f = codecs.open('daily_set.txt', 'a+', 'utf-8')
    # f.write(str(x))
    # f.close()
    # print(set)
    # print(get_last_2_week(-1,0))
    return str(x)

if __name__ == '__main__':

    print(get_stat_content())


import requests, unittest, json
from MySupport import MySupport
import pdb

class ContainerTests(unittest.TestCase):
    HOSTNAME = "host"
    PORT = 80

    def suite():
        suite = unittest.TestSuite()
        suite.addTest(ContainerTests('test_main'))
        return suite

    def test_main(self):
        url =  MySupport.url(self.HOSTNAME, self.PORT, "/launch")
        # launch first
        first_config = MySupport.get_dict("first_config")
        first_launch = MySupport.get_dict("first_launch")
        response = requests.post(url, json=first_launch)

        # launch second
        second_config = MySupport.get_dict("second_config")
        second_launch = MySupport.get_dict("second_launch")
        response = requests.post(url, json=second_launch)

        # launch third
        third_config = MySupport.get_dict("third_config")
        third_launch = MySupport.get_dict("third_launch")
        response = requests.post(url, json=third_launch)

        # check first
        url = MySupport.url("localhost", "10000", "/potato/potato.html")
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.content)
        self.assertEqual(response.content.decode("utf-8").strip(), "ich bin ein kartoffel")

        # check second
        url = MySupport.url("localhost", "20000", "/cabbage/cabbage.html")
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.content)
        self.assertEqual(response.content.decode("utf-8").strip(), "my cabbages!")

        url = MySupport.url("localhost", "20000", "/cabbage/potato/potato.html")
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.content)
        self.assertEqual(response.content.decode("utf-8").strip(), "ich bin ein kartoffel")

        # check third
        url = MySupport.url("localhost", "30000", "/potato/potato.html")
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.content)
        self.assertEqual(response.content.decode("utf-8").strip(), "ich bin ein kartoffel")

        url = MySupport.url("localhost", "30000", "/tomato/tomato.html")
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.content)
        self.assertEqual(response.content.decode("utf-8").strip(), "heinzzz")

        url = MySupport.url("localhost", "30000", "/tomato/cabbage/cabbage.html")
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.content)
        self.assertEqual(response.content.decode("utf-8").strip(), "my cabbages!")


from scipy.optimize import linprog
from mip import Model, xsum, maximize, BINARY

p = [20, 40, 20, 15, 30]
w1 = [5, 4, 3, 7, 8]
I1 = range(len(w1))
w2 = [1, 7, 9, 4, 6]
I2 = range(len(w2))
w3 = [8, 10, 2, 1, 10]
I3 = range(len(w3))
c1 = 25
c2 = 25
c3 = 25

m = Model()
x = [m.add_var(var_type=BINARY) for i in I1]
m.objective = maximize(xsum(p[i]*x[i] for i in I1))

m += xsum(w1[i] * x[i] for i in I1) <= c1
m += xsum(w2[i] * x[i] for i in I2) <= c2
m += xsum(w3[i] * x[i] for i in I3) <= c3

selected = [i for i in I1 if x[i].x >= 0.99]


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul  3 13:49:33 2018

@author: tom verguts
create plots for slides chapter 4, the delta rule
"""
import matplotlib.pyplot as plt
import numpy as np

# plot some vectors
nplot = 3
fig, axs = plt.subplots(nrows=1,ncols=nplot)
v = np.array([[1, 1], [-1, 1], [1, 0]]) # collection of all vectors (not of length unity)
to_be_plotted = [[0, 1], [0, 2], [0, 1, 2]]
for plot_loop in range(nplot):
    for dot_loop in to_be_plotted[plot_loop]:
        axs[plot_loop].scatter(v[dot_loop][0], v[dot_loop][1], c = "black")
    axs[plot_loop].set_xlim(-1.5, 1.5)
    axs[plot_loop].set_ylim(-1.5, 1.5)
    axs[plot_loop].set_xlabel("Input dimension 1")
    axs[plot_loop].set_ylabel("Input dimension 2")


import builder
from kivy.clock import mainthread
from kivy.properties import ObjectProperty
import tasks
from uix.productpopup import ProductPopup
from uix.tab import Tab

builder.load_file('tabs/bar.kv')

class Bar(Tab):
    """
    Bar tab.

    Attr categories_rv ObjectProperty: Categories RecycleView.
    Attr product_rv ObjectProperty: Products RecycleView.
    """

    categories_rv = ObjectProperty(None)
    products_rv = ObjectProperty(None)

    def __init__(self, **kwargs):
        """
        Param **kwargs dict: See parent doc.
        """
        self._categories = []
        self._category = None  # Selected category.
        self._products = []
        super(Bar, self).__init__(**kwargs)

    def on_categories_rv(self, instance, value):
        """
        Called when categories recycle view is added to the widget tree.

        Param instance Bar.
        Param value WeakProxy.
        """
        self.categories_rv.loading = True
        tasks.GetProductCategories(self.on_categories).start()

    @mainthread
    def on_categories(self, success, result):
        """
        Called when categories are received.

        Param success bool.
        Param result [ProductCategory].
        """
        if success:
            self._categories = result
            if self._categories:
                data = []
                index = 0
                for i, category in enumerate(self._categories):
                    if self._category and self._category.id == category.id:
                        index = i
                    data.append({
                        'text': category.title,
                        'on_selected': self.on_category_selected
                    })
                self.categories_rv.data = data
                self.categories_rv.layout_manager.select_node(index)
                self.on_category_selected(index)
            else:
                self._category = None
                self.categories_rv.data = []

    def on_category_selected(self, index):
        """
        Called when a category is selected.

        Param index int.
        """
        self._category = self._categories[index]
        self.products_rv.loading = True
        tasks.GetProducts(self._category, self.on_products).start()

    @mainthread
    def on_products(self, success, result):
        """
        Called when categories are received.

        Param success bool.
        Param result [ProductCategory].
        """
        if success:
            data = []
            self._products = result
            for product in result:
                data.append({'product': product, 'on_info': self.on_info})
            self.products_rv.data = data

    def on_info(self, index):
        """
        Called when an info button is pressed.

        Param int index.
        """
        product = self._products[index]
        popup = ProductPopup(product=product)
        popup.open()


from django.forms import ModelForm, ModelChoiceField, widgets, CharField

from scrape.fantasy_models import DraftPick
from scrape.nfl_models import Player

class DraftPickForm(ModelForm):
    """player_name = CharField(
        widget=widgets.TextInput(attrs={
            'class': 'prompt',
            'placeholder': 'Player'}))"""
    position = CharField(
        required=False,
        initial='All',
        widget=widgets.Select(
            # choices=[Player.FANTASY_POSITIONS]
        ),
    )

    class Meta:
        model = DraftPick
        fields = ['team', 'player', 'keeper', 'pick']

    def __init__(self, season=None, *args, **kwargs):
        super(DraftPickForm, self).__init__(*args, **kwargs)
        if season:
            pick = season.draft_picks()+1
            team = season.get_draft_pick_team(pick)
            self.fields['team'] = ModelChoiceField(
                queryset=season.teams.filter(id=team.id),
                initial=team.name,
                widget=widgets.Select(attrs={
                    'class': 'ui selection dropdown'
                }))
            self.fields['pick'] = CharField(
                widget=widgets.HiddenInput(),
                initial=pick)
            """self.fields['team'] = CharField(
                widget=widgets.TextInput(),
                initial=season.get_draft_pick_team(pick).id)"""
            self.fields['player'] = ModelChoiceField(
                # Only load top ~ top 60 players in each pos
                queryset=season.undrafted_players(),
                widget=widgets.Select(attrs={
                    # 'placeholder': 'Player',
                    'class': 'ui search selection dropdown',
                })
            )


import numpy as np
import pandas as pd
import matplotlib.figure
import matplotlib.pyplot as plt
from tkinter import Tk, Frame, Label, Text, Button, END
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

pos = 0
b = 0
mistakes = 0
correct_presses = 0
typing_speed = 0
duration = 0
start_time = 0
end_time = 0
minutes = 0
seconds = 0

def handle_stats():
    global typing_speed, correct_presses, mistakes
    entries = correct_presses + mistakes
    typing_speed = (entries / 5) / 1
    duration_label = Label(
        window,
        text=get_duration(),
        font=("Arial ", 16)
    )
    typing_speed_label = Label(
        window,
        text="Typing speed: " + str(int(typing_speed)) + " WPM",
        font=("Arial ", 16)
    )
    duration_label.pack()
    typing_speed_label.pack()

def draw_pie_chart():
    global mistakes, correct_presses
    labels = "Mistakes", "Correct presses"
    sizes = [mistakes, correct_presses]
    colors = ['red', 'green']
    fig1, ax1 = plt.subplots()
    ax1.pie(sizes, labels=labels, autopct='%1.1f%%',
            startangle=90, colors=colors)
    ax1.axis('equal')
    fig1.set_size_inches(5, 3)
    pie_canvas = FigureCanvasTkAgg(fig1, master=window)
    pie_canvas.get_tk_widget().pack()
    pie_canvas.draw()

def get_duration():
    global end_time, start_time, duration, minutes, seconds
    end_time = pd.datetime.now()
    duration = end_time - start_time
    hours, remainder = divmod(duration.seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    return "Duration: " + str(minutes) + "min" + " " + str(seconds) + "sec"

def on_key_press(e):
    if (e.keycode != 16):
        global pos, text_widget, b, mistakes, correct_presses, typing_speed
        if (string[pos] != e.char):
            text_widget.tag_add("wrong", str(1) + "." + str(b))
            mistakes += 1
        if (string[pos] == e.char):
            text_widget.tag_add("correct", str(1) + "." + str(b))
            correct_presses += 1
        b += 1
        pos += 1
        if (pos == len(string)):
            draw_pie_chart()
            handle_stats()

def start():
    global start_time
    message.pack()
    text_widget.pack()
    start_btn.destroy()
    start_time = pd.datetime.now()

file = open('text.txt', 'r')
text = file.readlines()
file.close()

string = ""
for i in text:
    for j in range(len(i)):
        string = string + i[j]

window = Tk()
window.wm_title("Keyboard Trainer")

text_widget = Text(window, height=10, width=100,
                   padx=20, pady=20, font=("Arial ", 16))
text_widget.insert(END, string)
text_widget.configure(state="disabled")
text_widget.tag_config("correct", background="green", foreground="white")
text_widget.tag_config("wrong", background="red", foreground="white")

text_widget.bind("<KeyPress>", on_key_press)
text_widget.focus()

message = Label(window,
                text="Start typing",
                font=("Arial ", 24))

start_btn = Button(
    window,
    text="Start",
    width="20",
    height="2",
    padx="1",
    pady="1",
    command=start)

start_btn.pack()

window.mainloop()


def sale_one(a,b):
    if b == 'завтра':
        return float(a * 0.95)
    else:
        return a

def sale_two(a,b):
    if b >= 20:
        return float(a * 0.8)
    else:
        return a

film = input('Выберите фильм: ')
day = input('Выберите день (сегодня, завтра): ')
time = int(input('Выберите время: '))
nticket = int( input('Выберите количество билетов: '))
print('Фильм: ', film, 'День: ', day, 'Время: ', time, 'Количество билетов: ', nticket)

if film == '«Паразиты»':
    if time == 12:
        print(sale_one(sale_two(250*nticket, nticket), day))
    elif time == 16:
        print(sale_one(sale_two(350*nticket, nticket), day))
    elif time == 20:
        print(sale_one(sale_two(450*nticket, nticket), day))
    else:
        print('В данное время сеансов не найдено!')
elif film == '«1917»':
    if time == 10:
        print(sale_one(sale_two(250*nticket, nticket), day))
    elif time == 13:
        print(sale_one(sale_two(350*nticket, nticket), day))
    elif time == 16:
        print(sale_one(sale_two(350*nticket, nticket), day))
    else:
        print('В данное время сеансов не найдено!')
elif film == '«Соник в кино»':
    if time == 10:
        print(sale_one(sale_two(350*nticket, nticket), day))
    elif time == 14:
        print(sale_one(sale_two(450*nticket, nticket), day))
    elif time == 18:
        print(sale_one(sale_two(450*nticket, nticket), day))
    else:
        print('В данное время сеансов не найдено!')
else:
    print('Данного фильма в списке не обнаружено!')


from ..Util import name_clades
from Bio.Phylo.BaseTree import Clade, Tree
from copy import copy
from itertools import combinations
import numpy as np

class ExponentialDistribution(object):
    def __init__(self, rate):
        self.rate = rate
    def sample(self):
        dt = np.random.exponential(1.0/self.rate)
        return(dt)

class GeneLineage(Clade):
    pass

class EmbeddedGeneForest(object):
    """ This is the precursor to a gene tree, resulting from a (possibly incomplete) sequence of coalescence events. """
    def __init__(self, tree, gene_clade_embedding):
        self.tree = tree
        self.s_terminals = tree.get_terminals()
        assert(all([k in list(tree.find_clades()) for k in gene_clade_embedding.keys()]))
        self.gene_clade_embedding = dict((k,copy(v)) for k,v in gene_clade_embedding.items())
        for species_clade in tree.find_clades(order='postorder'):
            if not self.gene_clade_embedding.has_key(species_clade):
                self.gene_clade_embedding[species_clade] = []
        assert(self.is_sorted())
    def is_sorted(self):
        for species_clade in self.tree.find_clades():
            for i,j in combinations(range(len(self.gene_clade_embedding[species_clade])),2):
                if self.gene_clade_embedding[species_clade][i].is_parent_of(self.gene_clade_embedding[species_clade][j]) and i > j:
                    return False
        return True
    #def maximal_gene_clades(self, population=None):
    def maximal_gene_clades(self, species_clade=None):
        """ Given a population (specified by a node of the species tree), species_clade, (default: root of the tree), return the maximal clades of the gene tree which are embedded at or below that population """
        """ Given a clade of the species tree, clade_s, (default: root of the tree), return the maximal clades of the gene tree which are embedded at or below clade_s """
        # A gene lineage is a node in the gene forest
        # Given an species_clade (default: root of the tree), return the maximal gene_clades which are embedded at or below (i.e. relative to the inverse partial order on the tree, where the root is the maximal element) the given species_clade
        if species_clade is None:
            species_clade = self.tree.root
        maximal_gene_clades = set()
        for species_subclade in species_clade.find_clades(order='preorder'):
            for gene_clade in self.gene_clade_embedding[species_subclade]:
                # We need the lists of clades that comprise the gene_clade_embedding to be sorted so that gene_clades which are maximal, among those embedded within a given species_clade, are reached first. This should be the case because in the join method, we add the clade representing the coalescence to the gene_clade_embedding at index 0, before each of the coalescing clades.
                if not any([bool(maximal_gene_clade.is_parent_of(gene_clade)) for maximal_gene_clade in maximal_gene_clades]):
                    maximal_gene_clades.add(gene_clade)
        return(maximal_gene_clades)
    #def where(self, gene_clade):
    def where(self, gene_clade):
        for i,gene_clades in enumerate(self.gene_clade_embedding.values()):
            if gene_clade in gene_clades:
                return(self.gene_clade_embedding.keys()[i])
    def join(self, clades, where, branch_length=None):
        if branch_length is not None:
            coalescence = Clade(clades=clades, branch_length=branch_length)
        else:
            coalescence = Clade(clades=clades)
        self.gene_clade_embedding[where].insert(0, coalescence)
#        self.gene_clade_embedding[where] += [coalescence]
#        self.sort_gene_clade_embedding()
        maximal_gene_clades = self.maximal_gene_clades(where)
        assert(all([bool(clade not in maximal_gene_clades) for clade in clades]))
        return(coalescence)
    def genetree(self):
        maximal_gene_clades = list(self.maximal_gene_clades())
        assert(len(maximal_gene_clades)==1)
        genetree = Tree(maximal_gene_clades.pop())
        if any([clade.name is None for clade in genetree.find_clades()]):
            name_clades(genetree)
        return(genetree)
    def coalesce(self, theta):
        for maximal_gene_clade in self.maximal_gene_clades():
            maximal_gene_clade.branch_length = 0
        for species_clade in self.tree.find_clades(order='postorder'):
            maximal_gene_clades = self.maximal_gene_clades(species_clade)
            nr_maximal_gene_clades = len(maximal_gene_clades)
            t = 0
            # Begin coalescence iteration
            while nr_maximal_gene_clades > 1: # While condition (1) is not yet met
                if theta==0: # Deal with theta=0 case separately
                    T = 0
                else:
                    rexp = ExponentialDistribution(float(nr_maximal_gene_clades)*float(nr_maximal_gene_clades-1)/(2*theta))
                    T = rexp.sample()
                if t+T < species_clade.branch_length or self.tree.root==species_clade: # If condition (2) is not yet met
                    # Randomly select two maximal_gene_clades to form the coalescence
                    coalescing_maximal_gene_clades = [list(maximal_gene_clades)[i] for i in np.random.choice(nr_maximal_gene_clades,replace=False,size=2)]
                    # Add the elapsed time to gene tree branch lengths
                    for maximal_gene_clade in maximal_gene_clades:
                        maximal_gene_clade.branch_length += T
                    # Update cumulative time
                    t += T
                    # Coalesce
                    coalescence = self.join(coalescing_maximal_gene_clades, species_clade, branch_length=0)
                    # Update maximal_gene_clades to reflect coalescence
                    maximal_gene_clades = self.maximal_gene_clades(species_clade)
                    assert(all([bool(maximal_gene_clade not in maximal_gene_clades) for maximal_gene_clade in coalescing_maximal_gene_clades]))
                    #maximal_gene_clades = (maximal_gene_clades-set(coalescing_maximal_gene_clades)).union(coalescence)
                    nr_maximal_gene_clades = len(maximal_gene_clades)
                else:
                    break
            for maximal_gene_clade in maximal_gene_clades:
                # Add to the gene tree branch lengths the time at the end of the population time span when no coalescence occurred
                if maximal_gene_clade.branch_length is None:
                    maximal_gene_clade.branch_length = 0
                maximal_gene_clade.branch_length += species_clade.branch_length - t
        return(self)

#def simulate_coalescent(tree, g_terminal_embedding, theta):
#    # theta = N*mu
#    # Choose the population (species tree clade: species_clade) which is maximal (with respect to the partial order on the rooted tree) among those which have not been selected already (using depth-first search on the tree with postorder). For this population, get the maximal gene tree clades (gene_clades) embedded higher in the tree than the given population. These are the maximal_gene_clades that can coalesce in the population (maximal_gene_clades).
#    # Coalescence iteration: If the number of maximal_gene_clades (nr_maximal_gene_clades) is greater than 1, draw an exponentially distributed r.var. T with rate equal to nr_maximal_gene_clades*(nr_maximal_gene_clades-1)/(2*theta). If T is less than the time span of the population, randomly choose a pair of gene tree clades (gene_clades) from maximal_gene_clades, and join these clades (Joining clades is the coalescence operation on the clade embedding object which reduces the number of maximal genetree clades in this embedding by one, see EmbeddedGeneForest.join).
#    # Repeat coalescence iteration. Let t_0=0. Starting from iteration i=1, at iteration i record the cumulative time as t_i = t_{i-1}+T. Repeat up to but not including the iteration i such that either: (1) t_i+T is greater than the time span of the population (2) or the number of maximal_gene_clades is 1. If either condition is reached continue for the next maximal population which has not been selected.
#    # Assigning branch lengths: Every time a coalescence time a coalescence event occurs for a population (species_clade), add the coalescence time T to the branch length of each maximal_gene_clade in the population. If the cumulative time t exceeds the time span of the population, so that no coalescence occurs in the remaining time, add the remaining time (species_clade.branch_length - t) to each maximal_gene_clade.
#    import itertools
#    for gene_clade in itertools.chain.from_iterable(g_terminal_embedding.embedding.values()):
#        gene_clade.branch_length = 0
#    gene_clade_embedding = EmbeddedGeneForest(tree, g_terminal_embedding.embedding)
#    gene_clade_embedding.maximal_gene_clades()
#    for species_clade in tree.find_clades(order='postorder'):
#        maximal_gene_clades = gene_clade_embedding.maximal_gene_clades(species_clade)
#        nr_maximal_gene_clades = len(maximal_gene_clades)
#        t = 0
#        # Begin coalescence iteration
#        while nr_maximal_gene_clades>1: # While condition (1) is not yet met
#            if theta==0: # Deal with theta=0 case separately
#                T = 0
#            else:
#                rexp = ExponentialDistribution(float(nr_maximal_gene_clades)*float(nr_maximal_gene_clades-1)/(2*theta))
#                T = rexp.sample()
#            if t+T < species_clade.branch_length or tree.root==species_clade: # If condition (2) is not yet met
#                # Randomly select two maximal_gene_clades to form the coalescence
#                coalescing_maximal_gene_clades = [list(maximal_gene_clades)[i] for i in np.random.choice(nr_maximal_gene_clades,replace=False,size=2)]
#                # Add the elapsed time to gene tree branch lengths
#                for maximal_gene_clade in maximal_gene_clades:
#                    maximal_gene_clade.branch_length += T
#                # Update cumulative time
#                t += T
#                # Coalesce
#                coalescence = gene_clade_embedding.join(coalescing_maximal_gene_clades, species_clade, branch_length=0)
#                # Update maximal_gene_clades to reflect coalescence
#                maximal_gene_clades = gene_clade_embedding.maximal_gene_clades(species_clade)
#                assert(all([bool(maximal_gene_clade not in maximal_gene_clades) for maximal_gene_clade in coalescing_maximal_gene_clades]))
#                #maximal_gene_clades = (maximal_gene_clades-set(coalescing_maximal_gene_clades)).union(coalescence)
#                nr_maximal_gene_clades = len(maximal_gene_clades)
#            else:
#                break
#        for maximal_gene_clade in maximal_gene_clades:
#            # Add to the gene tree branch lengths the time at the end of the population time span when no coalescence occurred
#            maximal_gene_clade.branch_length += species_clade.branch_length - t
#    return(gene_clade_embedding)


# Alexander Shelton
#
#
#
#

import matplotlib
matplotlib.use("TkAgg") #MPL backend
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk #canvas & nav
from matplotlib.figure import Figure
import matplotlib.animation as animation #
from matplotlib import style
from matplotlib import pyplot as plt
import matplotlib.ticker as mticker #modify dates and ticker in the data

import tkinter as tk #GUI
from tkinter import ttk #styling

import urllib
import json

import pandas as pd #csv work
import numpy as np #number crunching

LARGE_FONT = ("Verdana", 12)
NORMAL_FONT= ("Verdana", 10)#normal text info
SMALL_FONT= ("Verdana", 18)#Captions etc

ABOUT_TEXT = '''Program contributers: Alex Shelton
This program is free to use and alter for any purposes
Created to learn more about tkinter and make me some money'''

style.use("ggplot") #styling for matplotlib

# Figure subplot
f = Figure() #matplotlib figure
a = f.add_subplot(111)

#Default settings:
exchange = 'BTC Markets' #Starting exchange
programName='btcmarkets'
forceUpdate = 9000 #Number will force update
paneCount = 1 #Number of windows

resampleSize = '15min' #default sample size = 15min
dataPace  ='tick' #default graph has a days worth of pricing data
candleWidth = '.008' #default candle width

topIndicator = 'none'
bottomIndicator = 'none'
mainIndicator = 'none'

EMAS = [] #Exponential moving average
SMAS = [] #Simple moving average

chartLoad = True
## End default settings

def tutorial():

    #Sucsession of tutorial pages. once user leaves one page button takes them to the next page destroying old page

    def page2():
        tut.destroy()
        tut2 = tk.Tk()

        def page3():
            tut2.destroy()
            tut3 = tk.Tk()

            tut3.wm_title('part 3')
            label = ttk.Label(tut3, text='Part 3', font = NORMAL_FONT)
            label.pack(side='top', fill='x', pady=10)
            nextB = ttk.Button(tut3, text='Done', command = tut3.destroy)
            nextB.pack()

            tut3.mainloop()

        tut2.wm_title('part 2')
        label = ttk.Label(tut2, text='Part 2', font = NORMAL_FONT)
        label.pack(side='top', fill='x', pady=10)
        nextB = ttk.Button(tut2, text='Next', command = page3)
        nextB.pack()

        tut2.mainloop()

    tut = tk.Tk()
    tut.wm_title('Tutorial')
    label = ttk.Label(tut, text='What do you need help with? ', font = NORMAL_FONT)
    label.pack(side='top', fill='x', pady=10)

    overviewButton = ttk.Button(tut, text="Overview of the application", command = page2)
    overviewButton.pack()

    howTrade = ttk.Button(tut, text="How do I trade with this client?", command = lambda: popupmsg('not yet completed'))
    howTrade.pack()

    helpIndicator = ttk.Button(tut, text="Indicator question/help", command = lambda: popupmsg('not yet completed'))
    helpIndicator.pack()

    tut.mainloop()

def loadChart(run):
    global chartLoad
    if run == 'start':
        chartLoad = True
    elif run == 'stop':
        chartLoad = False

# About inside of help menu gives info about author(s) and program
def about():
    about = tk.Tk()
    about.wm_title('About this application')
    about.geometry('600x600')
    label = ttk.Label(about, text = ABOUT_TEXT)
    label.pack(side='top')

#Indicators for top and bottom
def addIndicator(option, where):
    global topIndicator
    global bottomIndicator
    global mainIndicator
    global forceUpdate

    #Adding indicators to the top:
    if where != 'main':
        if dataPace == 'tick':
            popupmsg('Indicators in tick data is not available')
        elif option == 'none':
            if where == 'top':
                topIndicator = option
            else:
                bottomIndicator = option
            forceUpdate = 9000 #Forcing an update after the indicator change
        elif option == 'rsi':
            #Must create a window to ask user by what period do they want rsi calculated in: default = 14 days
            rsiQuestion = tk.Tk()
            rsiQuestion.wm_title('Period?')

            label = ttk.Label(rsiQuestion, text='Choose how many periods you want each RSI calculation to consider ')
            label.pack(side='top')

            answer = ttk.Entry(rsiQuestion) #Placing the entry inside of the widnow rsiQuestion
            answer.insert(0,14) #inserting 14 into the first index for a default value:
            answer.pack()
            answer.focus_set()

            def callBack(where):
                global topIndicator
                global bottomIndicator
                global forceUpdate

                periods = answer.get()
                group = []
                group.append('rsi')
                group.append(periods)

                if where == 'top':
                    topIndicator = group
                    print("Set top indicator to: ", group)
                elif where == 'bottom':
                    bottomIndicator = group
                    print("Set bottom indicator to: ", group)
                else:
                    print("Error")
                #Forcing a reset
                forceUpdate = 9000
                rsiQuestion.destroy()

            b = ttk.Button(rsiQuestion, text='Submit', width=10, command= lambda: callBack(where))
            b.pack()
            tk.mainloop()

        elif option =='macd':
            if where == 'top':
                topIndicator = 'macd'
            elif where == 'bottom':
                bottomIndicator = 'macd'
            forceUpdate = 9000 #Forcing update

# Middle / Main indicator change/adds
def addMainIndicator(option, where):
    global mainIndicator
    global forceUpdate

    if dataPace == 'tick':
        popupmsg('Indicators in Tick Data not available.')

    if option != 'none':
        question = tk.Tk()
        question.wm_title('Periods?')
        label = ttk.Label(question, text='Choose how many periods for '+option.upper())
        label.pack(side='top', fill='x', pady=10)
        answer = ttk.Entry(question)
        answer.insert(0,10)
        answer.pack()
        answer.focus_set()

        def callBack(option):
            global mainIndicator
            global forceUpdate

            if(mainIndicator == 'none'):
                mainIndicator = []

            periods = answer.get()
            group = []
            group.append(option)
            group.append(int(periods))
            mainIndicator.append(group)
            forceUpdate = 9000
            print('Middle indicator set to: ', mainIndicator)
            question.destroy()

        button = ttk.Button(question, text='Submit', width=10, command = lambda: callBack(option))
        button.pack()
        tk.mainloop()

    else:
        mainIndicator = 'none'

def changeTimeFrame(timeFrame):
    global dataPace
    global forceUpdate

    if timeFrame == '7d' and resampleSize == '1min': #Will be too ugly
        popupmsg('Too much data selected, choose a smaller time frame or OHLC')
    else:
        dataPace = timeFrame
        forceUpdate = 9000 #Forces an update

def changeSampleSize(size, width):
    global resampleSize
    global forceUpdate
    global candleWidth

    if dataPace == '7d' and resampleSize == '1min': #Will be too ugly
        popupmsg('Too much data selected, choose a smaller time frame or OHLC')
    elif dataPace == 'tick':
        popupmsg("You're curently viewing tick data not OHLC")
    else:
        resampleSize = size
        forceUpdate = 9000 #force update
        candleWidth = width

def changeExchange(whatExchange, pn):
    global exchange #Default exchange
    global programName #What the program variable is
    global forceUpdate #counter force update

# Changing base vars that determine exchanges
    programName = pn
    exchange = whatExchange
    forceUpdate = 9000

# Pop up msg will be used to give the user an important info or throw an error
def popupmsg(msg):
    popup = tk.Tk() #creating a new tk instance

    popup.wm_title('!') #title on window will be '!'
    label = ttk.Label(popup,text=msg, font=NORMAL_FONT) #Creating the label: The text will be whats passed through and in normal font
    label.pack(side="top", fill='x', pady=10)

    b1= ttk.Button(popup, text='Okay', command=popup.destroy) #Creating a button on the popup window once clicked will close popup
    b1.pack()
    popup.mainloop() #run main loop of pop up

#animate function for live graphing core of data crunch
def animate(rate):
    global refreshRate
    global forceUpdate

    if chartLoad == True:
        if paneCount == 1:
            if dataPace == 'tick':
                try:

                    a = plt.subplot2grid((6,4), (0,0), rowspan=5, colspan=4) #Full grid size, starting point, row span, column span
                    a2 = plt.subplot2grid((6,4), (5,0), rowspan=1, colspan=4, sharex=a) #sharex shares x intercept with subplot 'a'

                    dataLink = 'https://api.btcmarkets.net/market/ETH/BTC/trades?limit=100'
                    data = urllib.request.urlopen(dataLink) #Accessing the link using requests
                    data = data.read().decode("utf-8") #deocde to utf8 and read into a json
                    data = json.loads(data) #size = 500

                    data = pd.DataFrame(data) #use pandas to create a csv

                    buys = data #dictionary
                    buyDates = data["date"].tolist()
                    #buys["datestamp"] = np.array(buys["date"]).astype("datetime64[s]") #Take the buys from the keyvalue in json
                    buyDates = (buys["datestamp"]).tolist() #Take the dates from buys key value
                    volume = data['amount']

                    a.clear()
                    a.plot_date(buyDates, buys["price"],'#00A3E0', label='Buys') #plotting
                    a2.fill_between(buyDates,0, volume, facecolor='#183A54' ) #specify min point, data plotted, face color

                    a.xaxis.set_major_locator(mticker.MaxNLocator(5)) #
                    a.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%D %H:M:S'))

                    #Creating a map legend, inside params are just so data cant be covered by legend
                    a.legend(bbox_to_anchor=(0,1.02,1,.102),loc=3,ncol=2,borderaxespad=0)

                    title = 'ETH USD Prices\nLast Price in BTC: '+str(buys['price'][499]) #Title for graph  ## Price index is pulling the last given index of a trade in json for latest data
                    a.set_title(title) #initializing title

                except Exception as e:
                    print("Failed because of ", e)

    # #TODO FIx this animate with from the day data pull from web server json

    # dataLink = 'https://api.btcmarkets.net/market/ETH/BTC/trades?limit=100'
    # data = urllib.request.urlopen(dataLink) #Accessing the link using requests
    # data = data.read().decode("utf-8") #deocde to utf8 and read into a json
    # data = json.loads(data) #size = 500

    # data = pd.DataFrame(data) #use pandas to create a csv
    # buys = data #dictionary
    # buys["datestamp"] = np.array(buys["date"]).astype("datetime64[s]") #Take the buys from the keyvalue in json
    # buyDates = (buys["datestamp"]).tolist() #Take the dates from buys key value

    # a.clear()
    # a.plot_date(buyDates, buys["price"],'#00A3E0', label='Buys') #plotting

    # #Creating a map legend, inside params are just so data cant be covered by legend
    # a.legend(bbox_to_anchor=(0,1.02,1,.102),loc=3,ncol=2,borderaxespad=0)

    # title = 'ETH USD Prices\nLast Price in BTC: '+str(buys['price'][499]) #Title for graph  ## Price index is pulling the last given index of a trade in json for latest data
    # a.set_title(title) #initializing title

class EtherBody(tk.Tk):#Inherits tk class
    def __init__(self, *args, **kwargs): #Self implied, *args = arguments, **kewargs = key words args(dictionaries)
        tk.Tk.__init__(self, *args, **kwargs)

        #tk.Tk.iconbitmap(self,default='ether.ico') #icon
        tk.Tk.wm_title(self, 'Ethereum Client')

        container = tk.Frame(self)
        container.pack(side='top', fill='both',expand=True)
        container.grid_rowconfigure(0, weight=1) #0 = min size
        container.columnconfigure(0,weight=1)

#####     BEGINNING OF MENU CREATIONS     #####

        #Creating a menu bar
        menuBar = tk.Menu(container)
        #creating a sub menu inside of the menu bar, 'tearoff' allows you to seperate menu bar to its on window
        fileMenu = tk.Menu(menuBar, tearoff=0)
        fileMenu.add_command(label='Save Settings', command= lambda: popupmsg('not supported yet')) #gives a label to allow user to save settings they have created
        fileMenu.add_separator() #Adds a bar to serperate menu options
        fileMenu.add_command(label='exit', command=quit)
        menuBar.add_cascade(label='FIle', menu=fileMenu) #adds the dropdown menu to the original Menu holder

        exchangeChoice = tk.Menu(menuBar, tearoff=1) #creating a menu that can be torn off, allows user to choose which exchange they want
        #Adding menu items, inside changeExchange function params are: (Display Name, variable name) to be used in the function
        exchangeChoice.add_command(label='Coinbase', command = lambda: changeExchange('Coinbase','coinbase'))
        exchangeChoice.add_command(label='Coinmama', command = lambda: changeExchange('Coinmama','coinmama'))
        exchangeChoice.add_command(label='CEX.io', command = lambda: changeExchange('CEX.io','cexio'))
        exchangeChoice.add_command(label='BTC Markets', command = lambda: changeExchange('BTC Markets','btcmarkets'))
        # Adding the menu to the overall menu
        menuBar.add_cascade(label='Exchange', menu = exchangeChoice)

        dataTimeFrame = tk.Menu(menuBar, tearoff=1)
        dataTimeFrame.add_command(label="Tick", command = lambda: changeTimeFrame("tick"))
        dataTimeFrame.add_command(label="1 Day", command = lambda: changeTimeFrame("1d"))
        dataTimeFrame.add_command(label="3 Day", command = lambda: changeTimeFrame("3d"))
        dataTimeFrame.add_command(label="1 Week", command = lambda: changeTimeFrame("7d"))
        #Adding time frame menu to overall menu
        menuBar.add_cascade(label='Time Frame',menu=dataTimeFrame)

        ## OHLC = Open High Low Close    OHLC conflicts with functions -> added an x
        OHLCX = tk.Menu(menuBar, tearoff=1)
        OHLCX.add_command(label="Tick", command = lambda: changeTimeFrame("tick"))

        #Change sample size takes the program name and 2nd param = candlestick width
        OHLCX.add_command(label="1 Minute", command = lambda: changeSampleSize("1min",0.0005))
        OHLCX.add_command(label="5 Minute", command = lambda: changeSampleSize("5min",0.003))
        OHLCX.add_command(label="15 Minute", command = lambda: changeSampleSize("15min",0.008))
        OHLCX.add_command(label="30 Minute", command = lambda: changeSampleSize("30min",0.016))
        OHLCX.add_command(label="1 Hour", command = lambda: changeSampleSize("1h",0.032))
        OHLCX.add_command(label="3 Hour", command = lambda: changeSampleSize("3h",0.096))
        #Adding the open high low close menu to the menuBar menu
        menuBar.add_cascade(label='OHLC Index', menu=OHLCX)

        #Indicator menu:
        topIndicator = tk.Menu(menuBar, tearoff=1)
        topIndicator.add_command(label='None', command= lambda: addIndicator('none', 'top'))
        topIndicator.add_command(label='RSI', command= lambda: addIndicator('rsi', 'top')) #Relative strength index: default = 14
        topIndicator.add_command(label='MACD', command= lambda: addIndicator('macd', 'top'))

        menuBar.add_cascade(label = 'Top Indicator', menu= topIndicator)

        #Main graph Indicator
        mainIndicator = tk.Menu(menuBar, tearoff=1)
        mainIndicator.add_command(label='None', command= lambda: addMainIndicator('none', 'main'))
        mainIndicator.add_command(label='SMA', command= lambda: addMainIndicator('sma', 'main')) #Simple moving average
        mainIndicator.add_command(label='EMA', command= lambda: addMainIndicator('ema', 'main')) #Exponential moving average

        menuBar.add_cascade(label = 'Main / Middle Indicator', menu= mainIndicator)

        # Bottom indicator
        bottomIndicator = tk.Menu(menuBar, tearoff=1)
        bottomIndicator.add_command(label='None', command= lambda: addIndicator('none', 'bottom'))
        bottomIndicator.add_command(label='RSI', command= lambda: addIndicator('rsi', 'bottom')) #relative strength index
        bottomIndicator.add_command(label='MACD', command= lambda: addIndicator('macd', 'bottom'))

        menuBar.add_cascade(label = 'Bottom Indicator', menu= bottomIndicator)

        #Trading menu for buy & sell
        tradeButton = tk.Menu(menuBar, tearoff=1)
        tradeButton.add_command(label='Manual trading',
        command = lambda: popupmsg('This is not running yet'))

        tradeButton.add_command(label='Automated trading',
        command = lambda: popupmsg('This is not running yet'))

        tradeButton.add_separator()

        tradeButton.add_command(label='Quick buy',
        command = lambda: popupmsg('This is not running yet'))

        tradeButton.add_command(label='Quick sell',
        command = lambda: popupmsg('This is not running yet'))

        tradeButton.add_separator()

        tradeButton.add_command(label='Set-up quick buy/sell',
        command = lambda: popupmsg('This is not running yet'))

        menuBar.add_cascade(label='Trading', menu = tradeButton)

        #Start and stop menu:
        startStop = tk.Menu(menuBar, tearoff=1)
        startStop.add_command(label = 'Resume', command = lambda: loadChart('start') )
        startStop.add_command(label = 'Pause', command = lambda: loadChart('stop') )

        menuBar.add_cascade(label = 'Reusme / Pause', menu = startStop)

        helpMenu = tk.Menu(menuBar, tearoff = 1)
        helpMenu.add_command(label = 'Tutorial', command = tutorial)
        helpMenu.add_command(label = 'About', command = about)

        menuBar.add_cascade(label = 'Help', menu = helpMenu)

        #####     END MENU     #####

        tk.Tk.config(self,menu=menuBar) #Configures window to hold ->  menu: 'menubar'

        self.frames = {} #Empty dictionary of franes
        # Add new pages to the list
        for F in (StartPage,EtherPage):  #for loop of tuple of page names
            frame = F(container, self) # creating a value
            self.frames[F] = frame #Putting key value into dictionary
            frame.grid(row=0, column=0, sticky='nsew')

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont] #Takes key value from frames dict
        frame.tkraise() #tkraise raises to the front page

class StartPage(tk.Frame):#inherits from frame
    def __init__(self, parent, controller): #parent = parent class such as EtherBody
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text='Ethereum trading application', font='LARGE_FONT')
        label.pack(pady=10,padx=10)

        button = ttk.Button(self, text='Agree', command=lambda: controller.show_frame(EtherPage))
        button.pack()

        button1 = ttk.Button(self, text='Exit', command=quit)
        button1.pack()

class PageOne(tk.Frame):
    def __init__(self,parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text='Page 1', font='LARGE_FONT')
        label.pack(pady=10,padx=10)

        button = ttk.Button(self, text='Back to home', command=lambda: controller.show_frame(StartPage))
        button.pack()

class EtherPage(tk.Frame):
     def __init__(self,parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text='Graph Page', font='LARGE_FONT')
        label.pack(pady=10,padx=10)
        button1 = ttk.Button(self, text='Back to home', command=lambda: controller.show_frame(StartPage))
        button1.pack()

        # outputting
        canvas = FigureCanvasTkAgg(f, self)
        canvas.draw()
        canvas.get_tk_widget().pack(side=tk.TOP,fill=tk.BOTH,expand=True)

        #nav bar
        toolbar = NavigationToolbar2Tk(canvas,self)
        toolbar.update()
        canvas._tkcanvas.pack()

## Running ##
app = EtherBody()
app.geometry('1280x720') # Setting the window size to 1280x720 res          Default use 800x600 if no 720p
ani = animation.FuncAnimation(f,animate, interval=2000 ) #animation instance (figure, animate, interval -ms-)
app.mainloop()


import numpy as np
from utils.activations import sigmoid

class toy_shallow_nn():
    def __init__(self, max_iter=200, learning_rate=0.01, hidden_nodes=4, verbose=False):
        self.rand = 0.01
        self.m = 0
        self.max_iter = max_iter
        self.learning_rate = learning_rate
        self.hidden_nodes = hidden_nodes
        self.verbose = verbose
        self.log_step = max_iter/10

    def init_params(self, X, Y):
        # sanity check
        assert(X.shape[1] == Y.shape[1] and X.shape[1] != 0)

        self.W1 = self.rand * np.random.randn(self.hidden_nodes, X.shape[0])
        self.b1 = np.zeros((self.hidden_nodes, 1))
        self.W2 = self.rand * np.random.randn(Y.shape[0], self.hidden_nodes)
        self.b2 = np.zeros((Y.shape[0], 1))

        self.m = Y.shape[1]

    def f_prop(self, X):
        Z1 = np.dot(self.W1, X) + self.b1
        A1 = np.tanh(Z1)

        Z2 = np.dot(self.W2, A1) + self.b2
        A2 = sigmoid(Z2)

        return A1, A2

    def compute_cost(self, A2, Y):
        J = -np.sum(Y * np.log(A2) + (1-Y) * np.log(1-A2))/self.m

        return J

    def b_prop(self, A1, A2, X, Y):
        m = self.m

        dZ2 = A2-Y
        dW2 = np.dot(dZ2, A1.T)/m
        db2 = np.sum(dZ2, axis=1, keepdims=True)/m

        # tanh`(Z1) = 1 - A1^2 = 1 - tanh(Z1)^2
        dZ1 = np.dot(self.W2.T, dZ2) * (1 - np.power(A1, 2))
        dW1 = np.dot(dZ1, X.T)/m
        db1 = np.sum(dZ1, axis=1, keepdims=True)/m

        return dW1, db1, dW2, db2

    def update_params(self, dW1, db1, dW2, db2):
        lr = self.learning_rate

        self.W1 -= lr * dW1
        self.b1 -= lr * db1

        self.W2 -= lr * dW2
        self.b2 -= lr * db2

    def fit(self, X, Y):
        self.init_params(X, Y)

        for i in range(self.max_iter):
            A1, A2 = self.f_prop(X)

            J = self.compute_cost(A2, Y)

            dW1, db1, dW2, db2 = self.b_prop(A1, A2, X, Y)

            self.update_params(dW1, db1, dW2, db2)

            if(self.verbose and i % self.log_step == 0):
                print(f'Iteration {i} cost J = {J}')

    def predict(self, X):
        _, A2 = self.f_prop(X)
        predictions = (A2 > 0.5).astype(float)

        return predictions


# -*- coding: utf-8 -*-
from __future__ import print_function
import scrapy
from scrapy.selector import Selector
from scrapy.http import Request
from scrapy import  log
from db.models import Offer

import grpc
import helloworld_pb2

def send2grpc():
  channel = grpc.insecure_channel('localhost:50051')
  stub = helloworld_pb2.GreeterStub(channel)
  response = stub.SayHello(helloworld_pb2.HelloRequest(Jobdesc='Jobdesc', Jobid='Jobid', Website='Website'))
  print("Greeter client received: " + response.message)

class LagouSpider(scrapy.Spider):
    name = 'lagou'
    allowed_domains = ["www.lagou.com"]
    start_urls = ['http://www.lagou.com/']

    def __init__(self, category=None, *args, **kwargs):
        super(LagouSpider, self).__init__(self, *args, **kwargs)
        self.page_404 = ''
        self.pages = 10
        log.msg('Hello Scrapy')

    def parse_job_detail(self, response):
        """
            解析三级页面,将职位的详细内容抽取出来
        """
        sel = Selector(response)
        title = sel.xpath('//*[@id="job_detail"]/dt/h1/text()').extract()[2]
        desc = sel.xpath('//dd[@class="job_bt"]/p/text()').extract()
        publish_time = sel.xpath('//p[@class="publish_time"]/text()').extract()[0]
        money, city, exp, edu, job_type = sel.xpath('//dd[@class="job_request"]/p/span/text()').extract()
        offer = Offer()
        offer.exp = exp
        offer.city = city
        offer.money = money
        offer.job_desc = desc
        offer.education = edu
        offer.job_title = title
        offer.job_type = job_type
        offer.job_url = response._url
        offer.job_publish_time = publish_time
        offer.is_used = 1
        offer.website = 'lagou'
        offer.save()

    def parse_job_list(self, response):
        """
           解析二级页面,将职位列表抽取出来
        """
        sel = Selector(response)
        res = sel.xpath('//*[@class="con_list_item"]')

        for item in res:
            href = item.xpath('.//a[@class="position_link"]/@href').extract()
            url = "http:{0}".format(href[0])
            yield Request(url, callback=self.parse_job_detail)

    def parse(self, response):
        """
            解析一级页面,抽出主url
        """
        sel = Selector(response)
        res = sel.xpath('//*[@class="menu_sub dn"]')
        for item in res:
            urls = item.xpath('.//a/@href').extract()
            for url in urls:
                for page in xrange(1, self.pages):
                    real_url = "http:{0}{1}/".format(url, page)
                    yield Request(real_url, callback=self.parse_job_list)


import requests
from bs4 import BeautifulSoup

print('This is just a test and convenience script so only use the XXXX XXX format, anything else will just crash it.')

while True:
    print('====================')
    reg = input('Enter car reg : ')
    url = 'https://www.mycarcheck.com/check/?reg_no='+reg[0:4]+'+'+reg[-3:]
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")

    make = soup.find('h3', attrs={'class': 'make-model'}).text.strip()
    body = soup.findAll('td', attrs={'class': 'col-xs-9'})[0].text.strip()
    colour = soup.findAll('td', attrs={'class': 'col-xs-9'})[1].text.strip()

    print('Make:', make)
    print('Body:', body)
    print('Colour:', colour)


# -*- coding: utf-8 -*-
# @Author: longfengpili
# @Date:   2023-08-11 18:03:23
# @Last Modified by:   longfengpili
# @Last Modified time: 2023-08-17 16:45:09
# @github: https://github.com/longfengpili

import pytest

from lxml import etree

from devices.device import Kimovil

class TestKimovil:

    def setup_method(self, method):
        self.pname = '2206122SC'
        self.km = Kimovil()

    def teardown_method(self, method):
        pass

    @pytest.mark.skip()
    def test_request(self):
        res = self.km.request(self.pname)
        print(res)

    def test_get_pkinds(self):
        url, pkinds = self.km.get_pkinds(self.pname)
        print(url)
        print(pkinds)

    def test_parse_pkind(self):
        url, pkinds = self.km.get_pkinds(self.pname)
        pkind = pkinds[0]
        pkind = self.km.parse_pkind(pkind)
        print(pkind)

    def test_get_phone_by_pkind(self):
        url, pkinds = self.km.get_pkinds(self.pname)
        pkind = pkinds[0]
        pkind = self.km.parse_pkind(pkind)
        print(pkind)
        phone = self.km.get_phone_by_pkind(pkind)
        print(phone)

    def test_parse_phone(self):
        url, pkinds = self.km.get_pkinds(self.pname)
        pkind = pkinds[0]
        pkind = self.km.parse_pkind(pkind)
        print(pkind)
        phone = self.km.get_phone_by_pkind(pkind)
        phone_info = self.km.parse_phone(phone)
        print(phone_info)

    def test_get_phones(self):
        phones = ['vivo 1906', 'CPH2083', '2206122SC']
        phones = ['samsung SM-A025F', '2206122SC']
        for idx, pname in enumerate(phones):
            phone_info = self.km.get_phone(idx, pname)
            print(phone_info)

    def test_get_phones_with_tpool(self):
        phones = ['vivo 1906', 'CPH2083', 'Infinix X670', 'V2026', 'M2101K6G', 'CPH1937', 'SM-A125F', '220333QAG',
                  '2201117TG', 'Infinix X688B', 'RMX3195', 'RMX2040', 'RMX3511', 'RMX3263']
        phones = self.km.get_phones_with_tpool(phones)
        print(phones)


class Queue:

    # Constructor with optional queue maxSize
    def __init__(self, maxSize=None):

        self.queue = []
        self.maxSize = maxSize
        self.size = 0

    # return size
    def getSize(self):
        return self.size

    # check if queue is empty
    def isEmpty(self):
        if self.size > 0:
            return False
        return True

    # lookup element at the front of the queue
    def peek(self):
        if self.isEmpty():
            return None
        return self.queue[0]

    # remove element from the front of the queue
    def dequeue(self):

        if self.size == 0:
            return
        else:
            self.size -=1
            return self.queue.pop(0)

    #insert element to the queue
    def enqueue(self,data):

        if self.getSize() == self.maxSize:
            return "queue is full"
        else:
            self.size +=1
            self.queue.append(data)

    # toString method
    def __str__(self):
        return str(self.queue)

#sample usage
if __name__ == "__main__":

    queue = Queue() # initialize a queue
    print(queue.isEmpty()) # Print true if queue is empty
    queue.enqueue(2)  # add 2 to queue
    queue.enqueue(3)  # add 3 to queue
    queue.enqueue(7)  # add 7 to queue
    print(queue)  # print queue
    print(queue.getSize()) # get number of elements in the queue
    queue.dequeue()  # remove top element form the queue
    print(queue.peek()) # peak the front of the queue
    print(queue)  # print queue


# 6. В программе генерируется случайное целое число от 0 до 100. Пользователь должен его отгадать не более чем за 10 попыток.
# После каждой неудачной попытки должно сообщаться, больше или меньше введенное пользователем число, чем то, что загадано.
# Если за 10 попыток число не отгадано, вывести ответ.
from random import randint

num = randint(0, 100)
print('Угадай число от 0 до 100!!!')

i = 10 # Количество попыток

while i > 0:
    print(f'Осталось попыток - {i}!')
    user_num = int(input(f'Введите число от 0 до 100: '))
    if user_num == num:
        print(f'Поздравляю! Ты отгадал! Это число {num}!')
        break
    elif user_num > num and i > 1:
        print(f'Число {user_num} больше загаданного. Введите число поменьше.')
    elif user_num < num and i > 1:
        print(f'Число {user_num} меньше загаданного. Введите число побольше.')
    i -= 1
    if i == 0:
        print(f'Попытки закончились. Было загадано число - {num}')


from flask import request, url_for, abort, redirect
from flask_api import status
from SSLCertificateCheck import app, db
from SSLCertificateCheck.utils.ssl_get import Certificate
from SSLCertificateCheck.utils.workers import worker_list_and_update_domain

from SSLCertificateCheck.models import Domain
from datetime import datetime
import logging
import concurrent.futures

logger = logging.getLogger(__name__)

@app.route("/api/v1/domains/", methods=['GET', 'POST', 'DELETE'])
def domains_list():
    if request.method == 'POST':
        try:
            if str(request.data.get('domain_name','')):
                domain_name = str(request.data.get('domain_name',''))
                d = Certificate(domain_name)
                notbefore = d.notBefore()
                notafter = d.notAfter()
                remaining = d.remaining()

                domain = Domain(domain_name=domain_name, notbefore=notbefore, notafter=notafter,
                                remaining=remaining)
                db.session.add(domain)
                try:
                    db.session.commit()
                    logger.debug(f'Commit database done, domain {domain_name}')
                    return  {
                        "domain_name": domain_name,
                        "notbefore": notbefore,
                        "notafter": notafter,
                        "remaining": remaining,
                        "last_checked": datetime.utcnow(),
                        "status": "success"
                        }
                except Exception as err:
                    logger.debug(f'Commit database error, reason {err}')
            else:
                return {
                    "status": "error",
                    "exception": f"key error {[ key for key in request.data.keys()]}"
                }
        except Exception as err:
            return {
                "status": "error",
                "exception": str(err)
            }

    elif request.method == 'DELETE':
        Domain.query.delete()
        db.session.commit()

    # request.method == 'GET'
    domains = Domain.query.all()
    result_base = [
        {
            "Descritions": "Garther the SSL Certificate infomation!",
            "Total domains": len(domains),
            "Result": [],
        }
    ]
    if len(domains) == 0:
        return result_base
    else:
        # List and Update Database
        ''' without threading
        for domain in domains:
            result.append(
                {
                    'id': domain.id,
                    'domain_name': domain.domain_name,
                    'notbefore': domain.notbefore,
                    'notafter': domain.notafter,
                    'remaining': domain.remaining,
                    'last_checked': domain.last_checked
                }
            )
            dom = Certificate(domain.domain_name)
            domain.notbefore = dom.notBefore()
            domain.notafter = dom.notAfter()
            domain.remaining = 23232323
            domain.last_checked = datetime.utcnow()
            logger.debug(domain)
            db.session.commit()
        '''

        domain_result = []
        with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
            future_to_url = {executor.submit(worker_list_and_update_domain, domain): domain for domain in domains}
            for future in concurrent.futures.as_completed(future_to_url):
                dom = future_to_url[future]
                try:
                    data = future.result()
                    domain_result.append(data)
                except Exception as exc:
                    logger.debug(f'{dom} generated an exception: {exc}')
        db.session.commit()
        result_base[0]['Result'] = domain_result

    return  result_base

@app.route('/api/v1/domains/<int:id>/', methods=['PUT', 'GET', 'DELETE'])
def domains_detail(id):
    domain = Domain.query.get(id)
    if domain:
        if request.method == 'PUT':
            try:
                if str(request.data.get('domain_name','')):
                    domain.domain_name = str(request.data.get('domain_name',''))
                    d = Certificate(str(request.data.get('domain_name','')))
                    domain.notbefore = d.notBefore()
                    domain.notafter = d.notAfter()
                    domain.remaining = d.remaining()
                    domain.last_checked = datetime.now()
                    db.session.commit()
                else:
                    return {
                        "status": "error",
                        "exception": f"key error {[ key for key in request.data.keys()]}"
                    }
            except Exception as err:
                return {
                    "status": "error",
                    "exception": str(err)
                }

        elif request.method == 'DELETE':
            db.session.delete(domain)
            db.session.commit()
            return '', status.HTTP_204_NO_CONTENT

        # request.method == 'GET'

        result = {
            'id': domain.id,
            'domain_name': domain.domain_name,
            'notbefore': domain.notbefore,
            'notafter': domain.notafter,
            'remaining': domain.remaining,
            'last_checked': domain.last_checked
        }
        dom = Certificate(domain.domain_name)
        domain.notbefore = dom.notBefore()
        domain.notafter = dom.notAfter()
        domain.remaining = dom.remaining()
        domain.last_checked = datetime.utcnow()
        db.session.commit()
        return result
    else:
        return {
            "error": "404"
        }

@app.route("/", methods=['GET', 'POST'])
def home():
    return redirect(url_for('domains_list'))


def solve(N, M, K, L):
    up = N // M
    if up == 0:
        return -1
    ans = (K + L - 1) // M + 1
    if ans > up:
        return -1
    return ans

if __name__ == "__main__":
    N, M, K, L = map(int, input().split(' '))
    print(solve(N, M, K, L))


import numpy as _np
import einsutils as ein
import noise

def noise_smoother(noise):
    """
    Basic moving average smooth t_i = (t_i + t_i-1)/2
    NOTE: t_0 =
    :param noise:
    :return:
    """
    output = _np.zeros(noise.shape)
    output[0] = noise[0]
    for i in range(1, len(noise)):
        output[i] = ((noise[i] + noise[i-1]) / 2.0)
    return output

def noisy_space(start, stop, num, smoothIters = 0):
    """
    Create a non-uniformly spaced vector of shape (num, ) in range [start, stop]
    i.e. noisy np.linspace()
    :param start:
    :param stop:
    :param num:
    :return:
    """
    noise = _np.random.uniform(0, 1, num)
    noise = _np.sort(noise, 0)
    minMax = (noise[0],noise[-1])

    for i in range(0, smoothIters):
        noise = _np.sort(noise_smoother(noise), 0)
        minMax = (noise[0], noise[-1])
    noise = _np.interp(noise, minMax, (start, stop))
    return noise

def noisy_ground_truth(start = -6.28, stop = 6.28, N_train = 500, N_test = 100, signal_type = "sine", gap_ = 0, noise_type = None, smooth_iters = 0 ):
    X_train = ein.random.noisy_space(start, stop, N_train, smoothIters=smooth_iters).reshape(-1,1)
    y_train = _np.sin(X_train).flatten() + _np.random.randn(N_train) * 1      # Noisy output data for training points     DIM: (N_train,   )
    X_test = _np.linspace(start, stop, N_test).reshape(-1,1)           # Unseen test points                        DIM: (N_test,   1)
    f_test = _np.sin(X_test).flatten()                                           # True noiseless function evaluated         DIM: (N_test,    )
                                                                                # at the test points

    # Create some noise variance for each position of X_train
    noise_sine =  _np.sin(X_train.flatten()) #* np.random.uniform(0.0, 1.0, N_train)
    noise_sigsine = ein.math.sigmoid_normalised_tunable(noise_sine)

    #y_train_noisy = np.sin(X_train_noisy).flatten() + np.random.randn(N_train) * noise
    y_train_noisy_before_noise = _np.sin(X_train).flatten()
    gaussian_noise = _np.random.randn(N_train)
    sine_scaled_noise = gaussian_noise * noise_sigsine #* np.sign(noise_sine)
    y_train_noisy =  y_train_noisy_before_noise + sine_scaled_noise

    return (X_train, y_train, X_test, f_test)


import os
import re
import sys
import xeger
import xml.etree.ElementTree as ET

def pick(country, kind):
	try:
		workspace = os.path.dirname(os.path.abspath(__file__))
		with open('{0}/PhoneNumberMetadata.xml'.format(workspace), 'r') as f:
			root = ET.fromstring(f.read())
			xpath = ".//territory[@id='{0}']/{1}/nationalNumberPattern".format(country[0:2].upper(), kind)
			return xeger.xeger(re.sub(r'\s', '', root.find(xpath).text))
	except (IOError, ET.ParseError, AttributeError) as e:
		return None

def main():
	country = sys.argv[1] if len(sys.argv) > 1 else 'HK'
	kind = sys.argv[2] if len(sys.argv) > 2 else 'mobile'
	print(pick(country, kind))


import subprocess
import sys
from tqdm import tqdm

for fname in tqdm(sys.argv[1:]):

    with open(fname) as f:
        first_line = f.readline()

        comma_count = first_line.count(',')

        comma_match = '^[^,]+' + ',[^,]+'*comma_count+'$'

        subprocess.call(['sed','-r','-i','/'+comma_match+'/!s/^/#/',fname])

        # clear out null characters
        subprocess.call(['sed', '-r', '-i', r's/\x0//g',fname])


# 청팀 백팀

b1, b2, b3, b4 = map(int, input().split())

w1, w2, w3, w4 = map(int, input().split())

b_sum = b1 + b2 + b3 + b4
w_sum = w1 + w2 + w3 + w4

if b_sum > w_sum:
    print(b_sum)
elif b_sum < w_sum:
    print(w_sum)
else:
    print(b_sum)

2주차/심재민/청팀 백팀.py