#!/usr/bin/python2.7.6

__credits__=  "Dr.Anil prabhakar"
__date__=     " April/June 2018"
#######################Description #####################
'''
    This Class room clicker software is designed for interactive class room learning, This software is developed for getting data from msp430
    microcontroller through RF module and plot bar graph in GUI.

    ~~~~~~~~~~~~~~~~~ This code is for Windows 10( x64 ) supporting BLE ~~~~~~~~~~~~~~~~~~~~

'''
################Loding Modules###########################
import os
# import pprint
# import random
import time
import wx
# import sys
# import matplotlib.pyplot as plt
# import serial
import numpy as np
# import matplotlib
from pylab import figure, table, show, axis, title
#matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
    FigureCanvasWxAgg as FigCanvas, \
    NavigationToolbar2WxAgg as NavigationToolbar

###########################modified  ###########
#import Tkinter
#from Tkinter import *
#import thread
#from threading import Thread
# import traceback
#import tkMessageBox
# from bluepy.btle import Scanner, DefaultDelegate
# from bluepy.btle import UUID, Peripheral
# import time
#from tkFileDialog import askopenfilename
# import os, sys
import subprocess as sp

tot_devices=['e6:c6:15:3d:db:df','dc:8a:19:f8:14:aa', 'f4:36:67:20:f8:6d','fc:77:48:ab:85:c6','ee:46:b2:5f:3d:c4']
tot={'e6:c6:15:3d:db:df':'Click1','dc:8a:19:f8:14:aa':'Click2','f4:36:67:20:f8:6d':'Click3','fc:77:48:ab:85:c6':'Click4','ee:46:b2:5f:3d:c4':'Click5'} ##Name for each Device
found_devices=[]
avail_devices = []
#root=Tk()
global answer

def bt_add_from_ull( device_address_ull ):
    device_address_hex = hex( device_address_ull )[ 2: ]        # removed the '0x' from left end of hex address
    # print( device_address_hex )

    # device_address -> address of device in bluetooth address format as required
    device_address = str()
    for i in range( int( len(device_address_hex) / 2 ) ):
        device_address += device_address_hex[ i*2 : i*2 + 2 ] + ':'
    device_address = device_address[ : -1 ]                         # removed the extra ':' from right end
    # print( device_address )
    return device_address

def ull_from_bt_add( bt_add ):
    hex_add = bt_add.replace( ':', '' )
    ull_add = int( hex_add, 16 )
    return ull_add

class BarsFrame(wx.Frame):                                        #Creating bar graph frame for 20 users As of now we have 5 devices only
    title = 'Class Room Clicker'
    def btscan(self,event):
        print('scan_button_pressed')
        global avail_devices
        global found_devices

        ############################################################################
        # class ScanDelegate(DefaultDelegate):
        #     def _init_(self):
        #         DefaultDelegate._init_(self)
        #     def handleDiscovery(self, dev, isNewDev, isNewData):
        #         if isNewDev:
        #             print ("Discovered device", dev.addr)
        #         elif isNewData:
        #             print ("Received new data from", dev.addr )
        # scanner = Scanner().withDelegate(ScanDelegate())
        # devices = scanner.scan(10.0)
        # for dev in devices:
        #     for (adtype, desc, value) in dev.getScanData():
        #         #print ("  %s = %s" % (desc, value) )
        #         if 'RFduino' in value:      #Filter Only RFduino Device
        #             found_devices.append(dev.addr)

        discover_exe = 'Modules\\exe\\discover.exe'
        child_process = sp.Popen( discover_exe, stdout = sp.PIPE )
        output_bytes = child_process.communicate()[0]
        # print( output_bytes )
        output_str = output_bytes.decode('utf-8')
        # print( output_str )

        # split into separate device addresses
        device_list_str = output_str.splitlines()
        print( device_list_str )

        device_list_ull = [ int( i ) for i in device_list_str ]         # list of device addresses in unsigned long long format

        found_devices = [ bt_add_from_ull( i ) for i in device_list_ull ]

        #####################################################

        print('Found Devices are :',found_devices )
        print('Total Devices list are \n',tot_devices )
        # print('scanned devices list are \n',found_devices )
        avail_devices = list( set(tot_devices) & set(found_devices) )
        print('Available Devices are \n',avail_devices )
        wx.MessageBox('Scan completed', 'Info',
                wx.OK | wx.ICON_INFORMATION)

    def __init__(self):#Initialize variables
        wx.Frame.__init__(self, None, -1, self.title)

        #Labels creat for table

        self.labelc = [ 'A', 'B', 'C', 'D', 'LOP','Vmon']
        self.labelr = ['ID', '1', '2', '3', '4', '5', '6', '7', '8', '9',
              '10', '11', '12', '13', '14', '15', '16', '17', '18', '19','20']
        self.chars = [['' for c in range(6)] for r in range(21)]
        self.colors = [[(0.95, 0.95, 0.95) for c in range(6)] for r in range(21)]
        self.qqq=1

        self.wloop=1
        self.totg = [0, 0, 0, 0]
        self.ao=[0]*20
        self.bo=[0]*20
        self.co=[0]*20
        self.do=[0]*20
        self.vv=[0]*20
        self.lastopt=[0]*20

        self.create_menu()
        self.create_status_bar()
        self.create_main_panel()
        self.textbox.SetValue(' '.join(map(str, 't(sec)')))
        self.textbox2.SetValue(' '.join(map(str, 'question')))

        self.draw_figure()

    def serread(self,event):#creating file,read till timeout and write to CRC.log file
        self.logfile=open("CRC.log","a")                   #log file is created
        self.logfile.write("\n##")
        self.logfile.write( time.strftime("%d/%m/%Y"))
        self.logfile.write("   ")

        self.logfile.write( time.strftime("%H:%M:%S"))
        self.logfile.write("##\n")
        self.logfile.write("#Q")
        self.logfile.write(str(self.qqq))
        self.logfile.write(":")

        self.logfile.write(self.textbox2.GetValue())
        self.logfile.write("?")
        #print ((time.strftime("%d/%m/%Y")))
        #print ((time.strftime("%H:%M:%S")))
        num_user = float(self.textbox.GetValue())
        #global ser
        #self.question=self.textbox2.GetValue()
        #print(self.question)
        global avail_devices
        try:

            ####################################
            print ( avail_devices )
        except Exception:
            answer=0
        except:
            print ("Device was not connected")

        timeout = int(time.time()) + int(num_user)
        while(self.wloop==1):
            if(time.time()>timeout):
                self.wloop=0
            else:
            #    print (avail_devices )
                for i in range(len(avail_devices)):
                    # print (avail_devices[i])
                    #####################################################
                    # read_uuid = UUID(0x2221)
                    # write_wuuid = UUID(0x2222)
                    # p = Peripheral(avail_devices[i],"random")
                    # ch = p.getCharacteristics(uuid=read_uuid)[0]
                    # chw = p.getCharacteristics(uuid=write_wuuid)[0]

                    # val=0
                    # if (ch.supportsRead()):
                    #     val=0
                    #     answer=''
                    #     val=chw.write('ff') #command for read the data from the device.
                    #     time.sleep(2)
                    #     val = ch.read()
                    #     value = ord(val)

                    ##### NOTE: as coded in exe file, the exe sleeps for 1 sec

                    answer = ''
                    write_to_characteristic = 'ff'              # The string parameter that will be written to the write characteristic

                    address_ull = ull_from_bt_add( avail_devices[ i ] )

                    connectAndRead_exe = 'Modules\\exe\\ConnectAndRead.exe'
                    child_process = sp.Popen( [ connectAndRead_exe, str( address_ull ), write_to_characteristic ], stdout = sp.PIPE )
                    output_bytes = child_process.communicate()[0]
                    # print( output_bytes )
                    output_str = output_bytes.decode('utf-8')
                    # print( output_str )                       # is of type '3\r\n'

                    value = int( output_str )

                    ####################################

                    if value==1:
                        answer= 'A'
                    elif value==2:
                        answer= 'B'
                    elif value==3:
                        answer= 'C'
                    elif value==4:
                        answer= 'D'
                    else:
                        answer='0'
                    print (tot.get(avail_devices[i]),answer )

                    volt=float(4.4)/10
                    #self.data = [int(self.serdata[0:2]), str(self.serdata[2]),volt]
                    self.data = [int(i), str(answer),volt]   #Device name, Option, volt
                    #print (self.data[0] )
                    #print(type(self.data[1]))
                    if(self.data[1]=='A'):
                        self.ao[((self.data[0])-1)]=self.ao[((self.data[0])-1)]+1
                        self.lastopt[((self.data[0])-1)]=self.data[1]
                        self.vv[((self.data[0])-1)]=self.data[2]

                    if(self.data[1]=='B'):
                        self.bo[((self.data[0])-1)]=self.bo[((self.data[0])-1)]+1
                        self.lastopt[((self.data[0])-1)]=self.data[1]
                        self.vv[((self.data[0])-1)]=self.data[2]

                    if(self.data[1]=='C'):
                        self.co[((self.data[0])-1)]=self.co[((self.data[0])-1)]+1
                        self.lastopt[((self.data[0])-1)]=self.data[1]
                        self.vv[((self.data[0])-1)]=self.data[2]

                    if(self.data[1]=='D'):
                        self.do[((self.data[0])-1)]=self.do[((self.data[0])-1)]+1
                        self.lastopt[((self.data[0])-1)]=self.data[1]
                        self.vv[((self.data[0])-1)]=self.data[2]

        self.atot=sum(self.ao)
        self.btot=sum(self.bo)
        self.ctot=sum(self.co)
        self.dtot=sum(self.do)
        self.totg=[self.atot,self.btot,self.ctot,self.dtot]
        #self.ser.flushInput()
        #self.ser.close()
        self.draw_figure()
        #print('10sec')
        print(self.bo)
                #print ("Reopen this gui" )
        for kkkk in range(0,20):
            lev=kkkk
            self.logfile.write("\n")
            self.logfile.write(str(lev+1))
            self.logfile.write(",")
            self.logfile.write(str(self.lastopt[kkkk]))
        #self.logfile.write("\n")

        self.logfile.close()
        self.qqq=self.qqq+1

    def create_menu(self):###Menu bar is created
        self.menubar = wx.MenuBar()

        menu_file = wx.Menu()
        m_expt = menu_file.Append(-1, "&Save plot\tCtrl-S", "Save plot to file")
        self.Bind(wx.EVT_MENU, self.on_save_plot, m_expt)
        menu_file.AppendSeparator()
        m_exit = menu_file.Append(-1, "E&xit\tCtrl-X", "Exit")
        self.Bind(wx.EVT_MENU, self.on_exit, m_exit)

        menu_help = wx.Menu()
        m_about = menu_help.Append(-1, "&About\tF1", "About the classe room clicker")
        self.Bind(wx.EVT_MENU, self.on_about, m_about)

        self.menubar.Append(menu_file, "&File")
        self.menubar.Append(menu_help, "&Help")
        self.SetMenuBar(self.menubar)

    def create_main_panel(self):# Crearing the elements of the GUI

        self.panel = wx.Panel(self)

        # Create the mpl Figure and FigCanvas objects.
        # 5x4 inches, 100 dots-per-inch
        #
        self.dpi = 100
        self.fig = Figure((5.0, 4.0), dpi=self.dpi)
        self.canvas = FigCanvas(self.panel, -1, self.fig)
        self.textbox2 = wx.TextCtrl(
            self.panel,
            size=(900,-1),
            style=wx.TE_PROCESS_ENTER)

        self.axes = self.fig.add_subplot(111)

        # Bind the 'pick' event for clicking on one of the bars
        #
        self.canvas.mpl_connect('pick_event', self.on_pick)

        #self.Bind(wx.EVT_TEXT_ENTER, self.on_text_enter, self.textbox2)
        self.drawbutton1 = wx.Button(self.panel, -1, "Scan")
        self.Bind(wx.EVT_BUTTON,  self.btscan, self.drawbutton1)
        self.textbox = wx.TextCtrl(
            self.panel,
            size=(60,-1),
            style=wx.TE_PROCESS_ENTER)
        self.Bind(wx.EVT_TEXT_ENTER, self.on_text_enter, self.textbox)
        self.drawbutton = wx.Button(self.panel, -1, "Start")
        self.Bind(wx.EVT_BUTTON, self.serread, self.drawbutton)
        self.attbutton = wx.Button(self.panel, -1, "Init")
        self.Bind(wx.EVT_BUTTON, self.init, self.attbutton)
        self.donebutton = wx.Button(self.panel, -1, "Result")
        self.Bind(wx.EVT_BUTTON, self.result, self.donebutton)
        self.closebutton = wx.Button(self.panel, -1, "Close")
        self.Bind(wx.EVT_BUTTON, self.closebtt, self.closebutton)
        self.plotlopbut = wx.Button(self.panel, -1, "L_opt")
        self.Bind(wx.EVT_BUTTON, self.plotlop, self.plotlopbut)
        self.totbt = wx.Button(self.panel, -1, "Total")
        self.Bind(wx.EVT_BUTTON, self.totfun, self.totbt)
        #self.cb_grid = wx.CheckBox(self.panel, -1,
        #    "Show Grid",
        #    style=wx.ALIGN_RIGHT)
        #self.Bind(wx.EVT_CHECKBOX, self.on_cb_grid, self.cb_grid)

        #self.slider_label = wx.StaticText(self.panel, -1,
        #    "width")
        #self.slider_width = wx.Slider(self.panel, -1,
        #    value=20,
        #    minValue=1,
        #    maxValue=100,
        #   style=wx.SL_AUTOTICKS | wx.SL_LABELS)
        #self.slider_width.SetTickFreq(10, 1)
        #self.Bind(wx.EVT_COMMAND_SCROLL_THUMBTRACK, self.on_slider_width, self.slider_width)

        self.toolbar = NavigationToolbar(self.canvas)

        #
        # Layout with box sizers
        #
        self.qbox = wx.BoxSizer(wx.HORIZONTAL)
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        flags = wx.ALIGN_LEFT | wx.ALL | wx.ALIGN_CENTER_VERTICAL

        self.vbox.Add(self.canvas, 1, wx.TOP | wx.GROW)
        self.vbox.Add(self.toolbar, 0, wx.EXPAND)
        self.vbox.AddSpacer(10)

        self.hbox = wx.BoxSizer(wx.HORIZONTAL)

        self.qbox.AddSpacer(10)

        self.hbox.Add(self.drawbutton1, 0, border=3, flag=flags)
        self.hbox.Add(self.textbox, 0, border=3, flag=flags)
        self.hbox.Add(self.drawbutton, 0, border=3, flag=flags)
        self.hbox.Add(self.donebutton, 0, border=3, flag=flags)
        self.hbox.Add(self.totbt, 0, border=3, flag=flags)

        self.hbox.Add(self.plotlopbut, 0, border=3, flag=flags)
        self.hbox.Add(self.attbutton, 0, border=3, flag=flags)
        self.hbox.Add(self.closebutton, 0, border=3, flag=flags)
        self.qbox.Add(self.textbox2, 0, border=3, flag=flags)

        #self.hbox.Add(self.cb_grid, 0, border=3, flag=flags)
        self.hbox.AddSpacer(30)
        #self.hbox.Add(self.slider_label, 0, flag=flags)
        #self.hbox.Add(self.slider_width, 0, border=3, flag=flags)

        self.vbox.Add(self.hbox, 0, flag = wx.ALIGN_LEFT | wx.TOP)
        self.vbox.Add(self.qbox, 0, flag = wx.ALIGN_LEFT | wx.TOP)
        self.panel.SetSizer(self.vbox)
        self.vbox.Fit(self)

    def totfun(self,event):#Button event for raw option
        self.draw_figure()
    def init(self,event):# To reuse the GUI Every variables need to be reinit
        self.chars = [['' for c in range(6)] for r in range(21)]
        self.wloop=1
        self.totg = [0, 0, 0, 0]
        self.ao=[0]*20
        self.bo=[0]*20
        self.co=[0]*20
        self.do=[0]*20
        self.vv=[0]*20
        self.lastopt=[0]*20

        app.frame = BarsFrame()
        print ("att")
        x = range(4)
        #xy=['a','b','c','d']
        ind = np.arange(4)
        width=0
        self.totg=[0,0,0,0]
        # clear the axes and redraw the plot anew
        #
        self.axes.clear()
        #self.axes.xticks(range(len(xy)),xy,size='small')
        #self.axes.grid(self.cb_grid.IsChecked())

        self.axes.bar(left=x,height=self.totg,
            width=1,
            align='center',color='y')
        self.axes.set_title('Histogram')
        self.axes.set_xlabel('Options')
        self.axes.set_ylabel('level')
        self.axes.set_xticks(ind+width)
        self.axes.set_xticklabels( ('A', 'B', 'C', 'D') )
        #self.ser.close()
        #ab=self.axes.set_xticklabels(['.','A', '.', 'B','.','C', '.','D'])

        self.canvas.draw()
    def plotlop(self,event):# To find the last option given by all the 20 users
        print('enter')
        x = range(4)
        self.lastoptA=0
        self.lastoptB=0
        self.lastoptC=0
        self.lastoptD=0
        #xy=['a','b','c','d']
        ind = np.arange(4)
        width=0

        # clear the axes and redraw the plot anew
        #
        self.axes.clear()

        for ii in range(0,20):
            if(self.lastopt[ii]=='A'):
                self.lastoptA=self.lastoptA+1
            if(self.lastopt[ii]=='B'):
                self.lastoptB=self.lastoptB+1
            if(self.lastopt[ii]=='C'):
                self.lastoptC=self.lastoptC+1
            if(self.lastopt[ii]=='D'):
                self.lastoptD=self.lastoptD+1

        self.lastoptt=[self.lastoptA,self.lastoptB,self.lastoptC,self.lastoptD]

        #self.axes.xticks(range(len(xy)),xy,size='small')
        #self.axes.grid(self.cb_grid.IsChecked())

        self.axes.bar(left=x,height=self.lastoptt, width=.35, align='center',color='y')
        self.axes.set_title('Histogram')
        self.axes.set_xlabel('Options')
        self.axes.set_ylabel('level')
        self.axes.set_xticks(ind+width)
        self.axes.set_xticklabels( ('A', 'B', 'C', 'D') )

        #ab=self.axes.set_xticklabels(['.','A', '.', 'B','.','C', '.','D'])

        self.canvas.draw()

    def closebtt(self,event):# Closs serial port and other events
        #global ser
        self.Close(True)
        #self.ser.close()

    def create_status_bar(self):#status bar
        self.statusbar = self.CreateStatusBar()

    def draw_figure(self):#Redraws the figure

        #str = self.textbox.GetValue()
        #self.data = map(int, str.split())

        x = range(4)
        #xy=['a','b','c','d']
        ind = np.arange(4)
        width=0

        # clear the axes and redraw the plot anew
        #
        self.axes.clear()
        #self.axes.xticks(range(len(xy)),xy,size='small')
        #self.axes.grid(self.cb_grid.IsChecked())

        self.axes.bar(left=x,height=self.totg, width=.35,  align='center',color='y')
        self.axes.set_title('Histogram')
        self.axes.set_xlabel('Options')
        self.axes.set_ylabel('level')
        self.axes.set_xticks(ind+width)
        self.axes.set_xticklabels( ('A', 'B', 'C', 'D') )

        #ab=self.axes.set_xticklabels(['.','A', '.', 'B','.','C', '.','D'])
        # global ser
        self.canvas.draw()

    def on_cb_grid(self, event):# Grid draw
        self.draw_figure()

    def on_slider_width(self, event):#slider
        self.draw_figure()

    def on_draw_button(self, event):# draw button
        self.figure(figsize=(8, 4), dpi=120)
        #self.data = [3, 1, 0, 1]
        self.draw_figure()
    def result(self, event):# Present the result of 20 user in a table
        #print ("result" )
        for x3 in range(0, 20):

            self.chars[x3+1][0] = self.ao[x3]
        for xx in range(0, 20):

            self.chars[xx+1][1] = self.bo[xx]

        for yy in range(0, 20):

            self.chars[yy+1][2] = self.co[yy]

        for zz in range(0, 20):

            self.chars[zz+1][3] = self.do[zz]

        for x1 in range(0, 20):

            self.chars[x1+1][4] = self.lastopt[x1]

        for x4 in range(0, 20):

            self.chars[x4+1][5] = self.vv[x4]

        for x5 in range(0, 20):
            if(self.vv[x5]>=2.6):

                self.colors[x5+1][5]=(0, 1, 0)
            elif((self.vv[x5]<2.6) and (self.vv[x5]>1)):
                self.colors[x5+1][5]=(1, 0, 0)
            else:
                self.colors[x5+1][5]=(.95, .95, 0.95)

        #for x2 in range(0, 20):

        #    self.chars[x2][5] = self.lastopt[x2]
        #self.chars[19][4] = '0'

        lightgrn = (0.5, 0, .9)
        title('Result')
        tab = table(cellText=self.chars,
                    rowLabels=self.labelr,
                    colLabels=self.labelc,
                    rowColours=[lightgrn]*21,
                    colColours=[lightgrn]*6,
                    cellColours=self.colors,
                    cellLoc='center',
                    loc='upper left')

        axis('off')
        show()

    def on_pick(self, event):#Pick single bar for info

        box_points = event.artist.get_bbox().get_points()
        msg = "You've clicked on a bar with coords:\n %s" % box_points

        dlg = wx.MessageDialog(
            self,
            msg,
            "Click!",
            wx.OK | wx.ICON_INFORMATION)

        dlg.ShowModal()
        dlg.Destroy()

    def on_text_enter(self, event):# This function will be called when the text event occer to display in GUI
        self.draw_figure()

    def on_save_plot(self, event):# To save the plot
        file_choices = "PNG (*.png)|*.png"

        dlg = wx.FileDialog(
            self,
            message="Save plot as...",
            defaultDir=os.getcwd(),
            defaultFile="plot.png",
            wildcard=file_choices,
            style=wx.SAVE)

        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self.canvas.print_figure(path, dpi=self.dpi)
            self.flash_status_message("Saved to %s" % path)

    def on_exit(self, event):#  Exit button
        self.Destroy()

    def on_about(self, event):#Help menu
        msg = """ Class Room Clicker:

         * This Application is created for intrective class room learning

        """
        dlg = wx.MessageDialog(self, msg, "About", wx.OK)
        dlg.ShowModal()
        dlg.Destroy()

    def flash_status_message(self, msg, flash_len_ms=1500):# Life time of status message
        self.statusbar.SetStatusText(msg)
        self.timeroff = wx.Timer(self)
        self.Bind(
            wx.EVT_TIMER,
            self.on_flash_status_off,
            self.timeroff)
        self.timeroff.Start(flash_len_ms, oneShot=True)

    def on_flash_status_off(self, event):
        self.statusbar.SetStatusText('')

if __name__ == '__main__':
    app = wx.App()
    app.frame = BarsFrame()
    app.frame.Show()
    app.MainLoop()


#!/usr/bin/env python
# encoding:utf8

from concurrent.futures import ThreadPoolExecutor
from config import update_page_config2, page_config, page_config2
from dbutil.dbutil import DB

class Task:
    '''
    暂时只是完成了新增页面，没有更新逻辑，也没有删除逻辑
    demo调研基本完成
    '''
    pool = None

    def __init__(self, pool_size):
        self.pool_size = pool_size
        self.pool = ThreadPoolExecutor(pool_size)

    def submit(self, obj):
        print("task submit", obj)
        self.pool.submit(self.api_execute, obj)

    def api_execute(self, obj):
        '''
        api 入口处的增删改查操作
        对应的异步事件
        按照不同的数据库表
        在此执行不同的逻辑
        :return:
        '''
        table_name = obj.pop('action_type')
        endpoint = obj.pop('_request_endpoint')
        print("异步处理", obj)
        func_name = endpoint + "_" + table_name
        if hasattr(self, func_name):
            table_func = getattr(self, func_name)
            table_func(obj)

    # 添加一张空表
    def addapi_table_info(self, obj):
        #  表名处理
        print("start execute create table", obj)
        table_name = obj.pop("name")
        db_name = obj.pop("_request_db")
        db = DB(mysql_db=db_name)
        create_table = 'create table %s (id INTEGER PRIMARY KEY )' % table_name
        print("execute create table",create_table)
        db.execute(create_table)

    # 删除表
    def delapi_table_info(self, obj):
        table_name = obj.get("table_name", "")

    # 更新表名
    def updateapi_table_info(self, obj):
        table_id = obj["id"]
        table_name = obj.pop("name")
        db_name = obj.pop("_request_db")
        db = DB(mysql_db=db_name)
        create_table = 'alter table %s rename to ' % table_name
        print("execute create table", create_table)
        db.execute(create_table)

    def addapi_string_clo(self, obj):
        # 字符串类型 增加表字段
        print("string_clo", obj)
        # string_clo {'colume_name': 'col1', 'colume_title': '字段1', 'table_name': '3', '_table_name': 'cdcad', '_request_db': <dbutil.dbutil.DB object at 0x7f1d4df73be0>}
        table_name = obj.pop("_table_name")
        db_name = obj.pop("_request_db")
        db = DB(mysql_db=db_name)
        colume_name = obj.get("colume_name", "")
        # create_table = 'create table %s (id INTEGER PRIMARY KEY )' % table_name
        add_colume = "ALTER TABLE %s ADD COLUMN %s varchar(200) " % (table_name, colume_name)
        # db.execute(create_table)
        db.execute(add_colume)

    def delapi_string_clo(self, obj):
        # 字符串类型 表字段修改处理
        print("string_clo", obj)

    def updateapi_string_clo(self, obj):
        # 字符串类型 表字段修改处理
        print("string_clo", obj)

    # -------------------------------

    # endpoint = obj.pop('_request_endpoint')
    # if endpoint == "addapi":
    #     pass
    # if endpoint == "delapi":
    #     pass
    # if endpoint == "updateapi":
    #     pass
    def addapi_datetime_clo(self, obj):
        # 日期类型 表字段修改处理
        print("datetime_clo", obj)
        table_name = obj.pop("_table_name")
        db_name = obj.pop("_request_db")
        db = DB(mysql_db=db_name)
        colume_name = obj.get("colume_name", "")
        # create_table = 'create table %s (id INTEGER PRIMARY KEY )' % table_name
        add_colume = "ALTER TABLE %s ADD COLUMN %s varchar(200) " % (table_name, colume_name)
        # db.execute(create_table)
        db.execute(add_colume)

    def addapi_dynamic_select_clo(self, obj):
        # 动态选择类型 表字段修改处理
        print("dynamic_select_clo", obj)
        table_name = obj.pop("_table_name")
        db_name = obj.pop("_request_db")
        db = DB(mysql_db=db_name)
        colume_name = obj.get("colume_name", "")
        # create_table = 'create table %s (id INTEGER PRIMARY KEY )' % table_name
        add_colume = "ALTER TABLE %s ADD COLUMN %s varchar(200) " % (table_name, colume_name)
        # db.execute(create_table)
        db.execute(add_colume)

    def addapi_static_select_clo(self, obj):
        # 静态选择类型 表字段修改处理
        print("static_select_clo", obj)
        table_name = obj.pop("_table_name")
        db_name = obj.pop("_request_db")
        db = DB(mysql_db=db_name)
        colume_name = obj.get("colume_name", "")
        # create_table = 'create table %s (id INTEGER PRIMARY KEY )' % table_name
        add_colume = "ALTER TABLE %s ADD COLUMN %s varchar(200) " % (table_name, colume_name)
        # db.execute(create_table)
        db.execute(add_colume)


import csv
import math

t = 0.01
pi = math.pi
R = 5
r = 1
a = 4
x0 = R+r-a
y0 = 0
nRev = 10
cos = math.cos
sin = math.sin
longitude = -118.288759
latitude = 34.021769

f = open('spiro.kml', 'w')

#Writing the kml file.
f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
f.write("<kml xmlns='http://earth.google.com/kml/2.0'>\n")
f.write("<Document>\n<Style id='z1'>\n")
f.write("<IconStyle><Icon><href>http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png</href></Icon></IconStyle>\n")
f.write("</Style>\n")
while t <= pi*nRev:
    x = (R+r)*cos((float(r)/R)*t) - a*cos((1+float(r)/R)*t)
    y = (R+r)*sin((float(r)/R)*t) - a*sin((1+float(r)/R)*t)
    newlong = longitude + y*0.001
    newlat = latitude + x*0.001
    f.write("   <Placemark>\n")
    f.write("       <Point>\n")
    f.write("           <coordinates>" + str(newlong) + "," + str(newlat) + "</coordinates>\n")
    f.write("       </Point>\n")
    f.write("   </Placemark>\n")
    newlong = 0
    newlat = 0
    t = t + 0.01

f.write("</Document>\n")
f.write("</kml>\n")
print("kml file created successfully!!! ")

f.close()


# -*- coding: utf-8 -*-
"""
Created on Tue Nov 26 16:42:45 2019

@author: ZİŞAN
"""
import pandas as pd

#%% dataset eklenir
data=pd.read_csv(r"gender-classifier.csv", encoding="latin1")
data=pd.concat([data.gender,data.description],axis=1)

#%% eksik ve anlamsız verileri çıkartma
data.dropna(axis=0, inplace=True)#nan degerleri çıkartma
#gruplandırıken string ifadeyi sayısala çevrime
#female -> 1, male ->0
data.gender=[1 if each == "female" else 0 for each in data.gender]

#%% cleaning data
#regular expression RE
import re

first_description=data.description[4]
#a dan z ye ve A dan Z yekadar olan harfleri değiştirme geri
#kalanını boşluğa dönüştür
description=re.sub("[^a-zA-Z]"," ",first_description)
#her harfin boyutunu küçült
description=description.lower()

#%% stopwords (irrelavent words) gereksiz kelimeler
import nltk #natural languafe tool kit
nltk.download("stopwords") #corpus diye klasöre indiriliyor
#from nltk.corpus import stopwords #corpusdan import ediliyor
#description=description.split()
#split yerine tokanizer kullanılıyor
description = nltk.word_tokenize(description)

#%%
#gereksiz kelimleri çıkart
description= [word for word in description if not word in set(stopwords.words("english"))]

#%%
#lemmatization loved->love kelime kökleri
import nltk as nlp
lemma=nlp.WordNetLemmatizer()
description=[lemma.lemmatize(word) for word in description]
#kelimeleri tekrar birleştirdi
description=" ".join(description)
#%%tüm datalar için yapma
description_list=[]
for description in data.description:
    description=re.sub("[^a-zA-Z]"," ",description)
    description=description.lower()
    description = nltk.word_tokenize(description)
    description= [word for word in description if not word in set(stopwords.words("english"))]
    lemma=nlp.WordNetLemmatizer()
    description=[lemma.lemmatize(word) for word in description]
    description=" ".join(description)
    description_list.append(description)

#%%bag of words

from sklearn.feature_extraction.text import CountVectorizer #bag of words yazdırmak için kullanılan vektör
max_features=5000

count_vectorizer=CountVectorizer(max_features=max_features) #parametre olarak stop_words="english" vb seyler alabilr

sparce_matrix=count_vectorizer.fit_transform(description_list).toarray() #x

print("en sık kullanılan {} kelimler: {}".format(max_features,count_vectorizer.get_feature_names()))

#%%
y= data.iloc[:,0].values #male or female
x=sparce_matrix
from sklearn.model_selection import train_test_split #train test split
x_train, x_test, y_train, y_test=train_test_split(x,y, test_size=0.1, random_state=42)

#%%navie bayes

from sklearn.naive_bayes import GaussianNB

nb=GaussianNB()
nb.fit(x_train,y_train)

#prediction
y_pred=nb.predict(x_test)
print("accuracy: ",nb.score(y_pred.reshape(-1,1),y_test))


# generator which supports syntax "for node in inorder(root):""
# @param node - the root of a (sub) bst
# @yields - every node in the tree in order until entirely traversed
def inorder(node):
	if node:
		for n in inorder(node.left):
			yield n

		yield node

		for n in inorder(node.right):
			yield n

class Solution(object):

	# generic bst node object with key, value, left and right pointers
	class Node:
		def __init__(self, key, value):
			self.key = key
			self.value = value
			self.left = None
			self.right = None

	# either adds the key to its proper psotiion in the tree with a default
	# value of 1, or increments the value if the key is already present.
	#
	# @param node - (sub)root of bst
	# @param key - key of the item to add or increment
	def addOrIncrement(self, node, key):
		if node == None:
			node = self.Node(key, 1)
			return node

		if node.key < key:
			node.right = self.addOrIncrement(node.right, key)
			return node
		elif node.key > key:
			node.left = self.addOrIncrement(node.left, key)
			return node
		# we reached the node we want
		else:
			node.value += 1
			return node

	# this implementation of maxHeap is designed to work as a priority queue
	# of node objects (above) -- it will add nodes to the heap, arranging
	# them according to item.value, and it supports the syntax
	# for item in heap.removeGenerator():
	class maxHeap:
		def __init__(self):
			self.heap = []

			#always points to next open slot
			self.count = 0

		def removeMax(self):
			toReturn = self.heap[0]
			self.count -= 1
			self.heap[0] = self.heap[self.count]
			self.heap[self.count] = None
			self.sinkFirstElement()
			return toReturn

		# generator which will keep returning the item with the largest
		# value in the heap, reestablishing heap property accordingly
		# until it is empty
		def removeGenerator(self):
			while self.count > 0:
				self.count -= 1
				tmp = self.heap[0]
				self.heap[0] = self.heap[self.count]
				self.heap[self.count] = None
				self.sinkFirstElement()
				yield tmp

		# adds a node item to the heap and calls swim to put it in proper position
		#
		# @param item - an object with item.key and item.value, to be sorted according
		# 	to item.value
		def add(self, item):
			self.heap.append(item)
			self.swim(self.count)
			self.count += 1

		# given an index i which may violate the max heap property,
		# this method will "swim" the element at position i up in the heap
		# until it is in the proper position
		#
		# NOTE - comparison is done according to element.value property,
		# will raise an error if theres an object in that array without that
		# property
		def swim(self, i):
			while (i > 0):
				parentIndex = (i-1)/2
				if self.heap[parentIndex].value < self.heap[i].value:
					self.swap(i, parentIndex)
					i = parentIndex
				else:
					return

		#swaps the element at i with the element at j
		def swap(self, i, j):
			tmp = self.heap[i]
			self.heap[i] = self.heap[j]
			self.heap[j] = tmp

		# after adding an element, the first element in the array may violate
		# the heap property. this method "sinks" it down until it is in the proper position
		def sinkFirstElement(self):
			i = 0
			while (i*2) + 1 < self.count:
				childIndex = i*2 + 1

				#take the larger of the two
				if childIndex + 1 < self.count and self.heap[childIndex + 1].value > self.heap[childIndex].value:
					childIndex += 1

				if self.heap[childIndex].value > self.heap[i].value:
					self.swap(childIndex, i)
					i = childIndex
				else:
					return

	def frequencySort(self, s):
		if len(s) < 1:
			return s
		root = self.addOrIncrement(None, s[0])
		for i in range(1, len(s)):
			self.addOrIncrement(root, s[i])

		heap = self.maxHeap()
		for c in inorder(root):
			print ("adding " + c.key )
			heap.add(c)

		ans = ""
		for c in heap.removeGenerator():
			ans += c.key * c.value

		return ans

s = Solution()

testString = "alsdkjfasdf"
print(s.frequencySort(testString))


import asyncio, argparse
import json
import logging
import random
import websockets
import queue
import sys
import uuid
import operator

from datetime import datetime

from blockchain import Blockchain
from transactions import Transaction
from block import Block
from TransactionJSONEncoder import TransactionJSONEncoder

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--listen', type=str,
                    help='ip and port: example: 127.0.0.1:8000')
args = parser.parse_args()

class RaftServer(object):
    """
    This class implements a blockchain based on the raft consensus algorithm. Therefore, it starts a websocket server and
    connects to the other raft nodes, setting up a peer2peer connection between all raft nodes. The consensus across all
    nodes is achieved by determining a leader, who is then responsible for mining and sharing new block. Instances that
    are followers will verify the newly mined block and append it to their blockchain. After a period of five minutes
    a new leadership election will automatically start.
    """
    STATE_FOLLOWER = 'follower'
    STATE_CANDIDATE = 'candidate'
    STATE_LEADER = 'leader'

    KEY_FIRST_VOTE = 'not elected'

    FORWARDED_KEY = "forwarded"

    HEARTBEAT_INTERVAL = 0.5

    ELECTION_TIMEOUT = 0.3

    LEADER_HEARTBEATS = 300 / 0.5  # 5 Minutes

    votes = {}

    _connections_to_servers = {}

    connected_clients = set()
    raft_nodes = set()
    control_connection = set()
    confirmed_commits = {}

    node_connections = {}

    def __init__(self, host, port, nodes, control_channels, blockchain_path):
        self.host = host
        self.port = port
        self.identifier = "{}:{}".format(self.host, self.port)

        self.nodes = nodes
        self.control_channels = control_channels

        self.blockchain = Blockchain(blockchain_path)
        self.blockchain.load()

        self._state = self.STATE_FOLLOWER
        self._currentTerm = 0
        self._commitIndex = 0
        self._lastApplied = 0
        self.votes[self.identifier] = self.identifier
        self._leader = self.KEY_FIRST_VOTE
        self._last_heartbeat = datetime.now()
        self._is_syncronizing = False
        self._backlog = queue.Queue()
        self.messages_to_save = {}
        self.heartbeats_left = self.LEADER_HEARTBEATS
        self.has_voted = False

    async def __aenter__(self):
        """
        Start websocket server and raft management.
        :return:
        """
        # Start Websocket Server
        await self.start_server()

        # Start raft state management
        asyncio.ensure_future(self.manage_raft_state(), loop=asyncio.get_event_loop())

        # Connect to other servers
        asyncio.ensure_future(self.periodic_connect_task(), loop=asyncio.get_event_loop())

        return self

    async def __aexit__(self, *args, **kwargs):
        await self._ws_server.__aexit__(*args, **kwargs)

    async def start_election(self):
        """
        Start election process
        :return:
        """
        self._currentTerm += 1
        self._state = self.STATE_CANDIDATE
        self.votes[self.identifier] = self.identifier
        self._leader = None
        self.has_voted = True
        await self.broadcast_to_clients({
            "type": "vote",
            "identifier": self.votes[self.identifier]
        })

    async def manage_raft_state(self):
        """
        Manages raft states.
        """
        await asyncio.sleep(2)
        while True:
            # No leader and missing heartbeat -> start new election
            if self._state == self.STATE_LEADER and self.heartbeats_left > 0:
                self.heartbeats_left -= 1
                # TODO: uncomment  + check if queue is empty
                self._last_heartbeat = datetime.now()
                await self.broadcast_to_clients({
                    "type": "heartbeat",
                    "identifier": self.identifier
                })

            # Force reelection after 20 heartbeats
            if self.heartbeats_left == 0:
                self._state = self.STATE_CANDIDATE
                self.heartbeats_left = self.LEADER_HEARTBEATS
                election_timeout = random.randint(100, 200) / 1000
                # election_timeout = random.randint(1, 3)
                await asyncio.sleep(election_timeout)

            if self.heartbeat_timed_out() and self._state != self.STATE_LEADER and not self._is_syncronizing:
                election_timeout = random.randint(150, 300) / 1000
                # election_timeout = random.randint(1, 3)
                await asyncio.sleep(election_timeout)
                await self.start_election()

            logger.debug("State: {}".format(self._state))
            logger.debug("Elected leader: {}".format(self._leader))
            await asyncio.sleep(self.HEARTBEAT_INTERVAL)

    def heartbeat_timed_out(self):
        return (datetime.now() - self._last_heartbeat).total_seconds() > self.HEARTBEAT_INTERVAL * 1.5

    async def start_server(self):
        """
        Start websocket server.
        :return:
        """
        logger.info("Starting websocket server")
        self._ws_server = websockets.serve(lambda ws, msg: self.server_on_message(ws, msg), self.host, self.port)
        asyncio.ensure_future(self._ws_server, loop=asyncio.get_event_loop())

    async def force_clients_to_sync_with_me(self):
        """
        Force connected raft nodes to sync their blockchain to the local one.
        :return:
        """
        for ws in self.raft_nodes:
            await self.sync_client(ws)

    async def client_recv_handler(self, connection):
        """
        Handler to communicate with the other servers
        """
        try:
            while True:
                data = await connection.recv()
                message = json.loads(data)

                await self.handle_message_exchange(message, connection)
        except Exception as e:
            pass

    async def connect_to_server(self, uri, handler):
        """
        Connect to other raft server if no connection has been made so far.
        :param uri:
        :param handler:
        :return:
        """
        if not "{}:{}".format(self.host, self.port) in uri:
            # Do not connect to yourself
            try:
                ws = websockets.connect(uri)
                connection = await ws.__aenter__()
                self._connections_to_servers["{}:{}".format(*connection.remote_address)] = connection
                asyncio.ensure_future(handler(connection), loop=asyncio.get_event_loop())
                await self.on_connect(connection)
            except Exception as e:
                logger.error("Connection failed: {} {}".format(uri, e))

    async def register_as_raft_node(self, websocket):
        await websocket.send(json.dumps({
            'type': 'register',
            'data': 'node',
            'identifier': self.identifier
        }))

    async def control_recv_handler(self, connection):
        """
        Handler to communicate with the other servers
        """
        while True:
            data = await connection.recv()
            message = json.loads(data)
            logger.info('Received message at control from {}:{} > {}'.format(*connection.remote_address, message))
            await self.handle_message_exchange(message, connection)

    async def periodic_connect_task(self):
        """
        Try to connect to configured nodes every 5 seconds.
        """
        self.ws_control_channel = None
        while True:
            for node in self.nodes:
                if node not in self._connections_to_servers and node not in self.node_connections:
                    await self.connect_to_server("ws://{}".format(node), self.client_recv_handler)
            for control_channel in self.control_channels:
                # await self.connect_to_server("ws://{}".format(control_channel), self.control_recv_handler)
                if self.ws_control_channel is None:
                    self.ws_control_channel = True
                    await self.connect_to_server("ws://{}".format(control_channel), self.control_recv_handler)

            await asyncio.sleep(2)

    async def send_heartbeat(self):
        """
        Send heartbeat fo connected raft nodes.
        :return:
        """
        await self.broadcast_to_clients({
            "type": "heartbeat",
            "identifier": self.identifier
        })

    async def sync_client(self, ws):
        """
        Send the local blockchain to the connected raft node.
        :param ws:
        :return:
        """
        await ws.send(json.dumps({
            "type": "start_sync"
        }))

        await ws.send(json.dumps({
            "type": "sync",
            "data": self.blockchain.export_as_json()
        }))

    async def server_on_message(self, websocket, path):
        """
        Register new connections to server, manages a constant connection and disconnect of clients.
        Incoming messages will be passed on the handle_message_echange function.
        :param websocket:
        :param path:
        :return:
        """
        # Register websocket
        self.connected_clients.add(websocket)
        try:
            await self.on_connect(websocket)

            while True:
                try:
                    data = await websocket.recv()
                    message = json.loads(data)

                    await self.handle_message_exchange(message, websocket)
                except Exception as e:
                    logger.error(e)
                    if websocket in self.connected_clients:
                        self.connected_clients.remove(websocket)
                    if websocket in self.raft_nodes:
                        self.raft_nodes.remove(websocket)
                    if websocket in self.control_connection:
                        self.control_connection.remove(websocket)
                    if websocket in self.node_connections:
                        self.node_connections.remove(websocket)
                    break
        finally:
            if websocket in self.connected_clients:
                self.connected_clients.remove(websocket)

    async def sync_last_block(self, message_uuid):
        """
        Send the last block of the local blockchain to all connected raft nodes
        :param message_uuid:
        :return:
        """
        self.confirmed_commits[self._currentTerm] = {'confirmations': set(),
                                                     'data': self.blockchain.last_block().pending_messages[0]}
        await self.broadcast_to_clients({
            "type": "commit",
            "data": json.dumps(self.blockchain.last_block(), ensure_ascii=False, cls=TransactionJSONEncoder),
            "term": self._currentTerm,
            "uuid": message_uuid
        })

    async def broadcast_to_clients(self, message):
        """
        Broadcast a message to all connected clients.
        """
        message['term'] = self._currentTerm
        self._currentTerm += 1

        # Send to raft nodes that this instance is connected to as a client
        for ws in self.raft_nodes:
            try:
                await ws.send(json.dumps(message))
            except Exception as e:
                logger.error("Error while sending to client: {}".format(e))

    async def handle_message_exchange(self, message, websocket):
        """
        Maps arriving messages to their local handler.
        The value of the key 'type' will determine which message will be called.
        The message will then forwared to the selected handler.
        :param message:
        :param websocket:
        :return:
        """
        logger.info('Received message from {}:{} > {}'.format(*websocket.remote_address, message))

        handler = getattr(self, "on_{}".format(message['type']), None)

        if handler is not None:
            await handler(message, websocket)
        else:
            logger.error("No handler found: {}".format(message))

    async def on_connect(self, websocket):
        """
        Sending newly connected clients immediately an heartbeat to put them into follower mode.
        :param websocket:
        :return:
        """
        logger.info("New connection from: {}:{}".format(*websocket.remote_address))
        await self.register_as_raft_node(websocket)
        if self._state == self.STATE_LEADER:
            # Sending heartbeat to new connection to set me as leader
            await self.send_heartbeat()
            await self.sync_client(websocket)

    async def on_heartbeat(self, message, websocket):
        """
        Handles heartbeat messages. Current instance will go into follower mode and updates last heartbeat timestamp.
        :param message:
        :param websocket:
        :return:
        """
        self._state = self.STATE_FOLLOWER
        self._leader = message['identifier']
        self._last_heartbeat = datetime.now()

    async def on_vote(self, message, websocket):
        self.votes["{}:{}".format(*websocket.remote_address)] = message['identifier']

        if self._state == self.STATE_CANDIDATE:
            if self.heartbeat_timed_out():
                self.votes[self.identifier] = message['identifier']
                self._currentTerm = message['term']
                self._last_heartbeat = datetime.now()

                await self.broadcast_to_clients({
                    "term": self._currentTerm,
                    "type": "vote",
                    "identifier": self.votes[self.identifier]
                })

        # We subtract 1 of len(self.votes), because the current instance also voted but is not part of self.node_connections
        everyone_has_voted = (len(self.votes) - 1) == len(self.node_connections)
        if everyone_has_voted:
            leader = self.get_leader()
            if leader is not None:
                if leader == self.identifier:
                    logger.info("I am the new leader! \o/")
                    first_election = self._leader == self.KEY_FIRST_VOTE
                    # self.votes = {}
                    self._state = self.STATE_LEADER
                    self._leader = self.identifier
                    await self.send_heartbeat()
                    if first_election:
                        # System starts up -> sync chain
                        await self.force_clients_to_sync_with_me()
                else:
                    self._state = self.STATE_FOLLOWER

    def get_websocket_identifier(self, websocket):
        """
        Build a string identifier from websocket object.
        :param websocket:
        :return:
        """
        return "{}:{}".format(*websocket.remote_address)

    async def on_start_sync(self, message, websocket):
        """
        Instance goes into sync mode and queues broadcasted blocks until sync is done.
        :param message:
        :param websocket:
        :return:
        """
        logging.info("Start sync")
        self._is_syncronizing = True

    async def on_sync(self, message, webssocket):
        """
        Receives blockchain from the leader, validates that the shared blockchain is an instance of the local blockchain.
        After sync is done, all queued blocks will be appended to the local blockchain.
        :param message:
        :param webssocket:
        :return:
        """
        logger.info("Sync in progress")
        json_chain = json.loads(message['data'])
        blockchain_candidate = Blockchain(None)
        blockchain_candidate.import_chain_from_json(json_chain)
        if self.blockchain.is_empty():
            # Blockchain is empty -> Import existing one
            logger.info("Importing new blockchain")
            self.blockchain.import_blockchain_from_blockchain_obj(blockchain_candidate)
        else:
            # Node was already part of a raft group with a blockchain
            # -> Verify that imported blockchain is extension of saved one

            # TOOD: What to do, if old chain is longer -> Force sync with me
            if blockchain_candidate.length >= self.blockchain.length:
                # Just try to import if longer
                if self.blockchain.is_partial_blockchain(blockchain_candidate):
                    logger.info("Importing blockchain")
                    self.blockchain.import_blockchain_from_blockchain_obj(blockchain_candidate)
                    logger.info("Blockchain successfully synced")
                else:
                    logger.error(
                        "The loaded blockchain from does not belong to this network. Aborting!!!")
                    sys.exit()
            else:
                logger.info("My blockchain is longer. Do not accept it.")
                await self.force_clients_to_sync_with_me()

        self.blockchain.save()

        # Save messages from backlog
        await self.commit_messages_from_backlog()

        # sync is done
        self._is_syncronizing = False

    async def on_commit(self, message, websocket):
        """
        Shares data that should be stored in the blockchain with the network so that every node can verify the content was not changed.
        If the current instance is leader, a new block will be mined and synced across the network.
        A follower will append the newly mined block to it's local blockchain.
        :param message:
        :param websocket:
        :return:
        """
        if "uuid" not in message:
            # Add id and broadcast to all nodes
            message_uuid = str(uuid.uuid4())
            message['uuid'] = message_uuid

            update_message = {**message}
            update_message['type'] = 'message_broadcast'

            await self.broadcast_to_clients(update_message)

        message_uuid = message['uuid']
        self.messages_to_save[message_uuid] = {**message}

        if self._state == self.STATE_LEADER:
            # Leader is to mine the next block
            logger.info("Mining Block and broadcasting.")
            data = json.loads(message["data"])
            if self.blockchain.is_empty():
                # init blockchain
                self.blockchain.initialize_chain()
                await self.sync_last_block(message_uuid='None')

            # Calculate blockchain
            self.blockchain.add_transaction(**data)
            self.blockchain.mine_block()
            self.blockchain.save()

            del self.messages_to_save[message_uuid]
            await self.sync_last_block(message_uuid=message_uuid)
        elif self._state == self.STATE_FOLLOWER:
            # term will be None if messages comes from control connection
            term = message.get("term", None)
            if term is not None:
                self._backlog.put(message)
                self._currentTerm = term
                if not self._is_syncronizing:
                    await self.commit_messages_from_backlog()

    async def on_message_broadcast(self, message, websocket):
        """
        Saves shared unmined messages to local chache.
        :param message:
        :param websocket:
        :return:
        """
        self.messages_to_save[message['uuid']] = message
        if self._state == self.STATE_LEADER:
            await self.on_commit(message, websocket)

    async def commit_messages_from_backlog(self):
        """
        Validates a block send from the leader and appends it to the local blockchain.
        :return:
        """
        while not self._backlog.empty():
            message = self._backlog.get()
            term = message.get('term')
            broadcasted_message = json.loads(self.messages_to_save[message['uuid']]['data'])
            data = json.loads(message['data'])
            message_integrity = broadcasted_message == data
            if message_integrity:
                new_transaction = Transaction(**json.loads(data['pending_messages'][0]))
                data['pending_messages'] = [new_transaction]
                new_block = Block(**data)
                self.blockchain.add_block(new_block)
                self.blockchain.save()
                await self.broadcast_to_clients({
                    'type': 'client_commit_confirm',
                    'source_term': term
                })
            else:
                await self.start_election()

    async def on_register(self, message, websocket):
        """
        Every newly connected node must register to state its role. A connection can either be part of the raft network
        or a control connection to saves messages in the network.
        :param message:
        :param websocket:
        :return:
        """
        if message['data'] == 'node':
            # append to node list -> node is part of raft
            self.raft_nodes.add(websocket)
            self.node_connections[message['identifier']] = websocket
        elif message['data'] == 'control':
            # append to control
            self.control_connection.add(websocket)

    async def on_client_commit_confirm(self, message, websocket):
        """
        Broadcast a message to all control connections after a message has been successfully committed by all
        nodes.
        :param message:
        :param websocket:
        :return:
        """
        if self._state == self.STATE_LEADER:
            try:
                term = message['source_term']
                self.confirmed_commits[term]['confirmations'].add(self.get_websocket_identifier(websocket))
                if len(self.confirmed_commits[term]['confirmations']) == len(self.raft_nodes):
                    # send confirm to control channel

                    data = json.dumps(self.confirmed_commits[term]['data'], ensure_ascii=False,
                                      cls=TransactionJSONEncoder)
                    del self.confirmed_commits[term]
                    await self.broadcast_to_control_connections({
                        'type': 'commit_confirm',
                        'data': data
                    })
            except Exception as e:
                print("Error", e)

    async def on_commit_confirm(self, message, websocket):
        """
        Forward confirmation message to all connectec control connections
        :param message:
        :param websocket:
        :return:
        """
        await self.broadcast_to_control_connections(message)

    def get_leader(self, ):
        """
        Determines based on the votes the current leader
        :return:
        """
        leader = None

        # count votes
        votes_recieved = {}
        for k, v in self.votes.items():
            if v not in votes_recieved.keys():
                votes_recieved[v] = 0
            votes_recieved[v] += 1

        # determine leader
        sorted_votes = sorted(votes_recieved.items(), key=operator.itemgetter(1))
        max_votes = sorted_votes[-1][1]
        if max_votes >= len(self.votes.keys()) / 2.0:
            leader = sorted_votes[-1][0]
        logger.debug("New leader is: {}".format(leader))
        return leader

    async def broadcast_to_control_connections(self, message):
        """
        Broadcast a message to all connected control connections.
        """
        # Send to raft nodes that this instance is connected to as a client
        message['type'] = 'commit_confirm'
        for k, ws in self._connections_to_servers.items():
            try:
                await ws.send(json.dumps(message))
            except Exception as e:
                logger.error("Error while sending to control connection: {}".format(e))

nodes = [
    "127.0.0.1:8007",
    "127.0.0.1:8009",
    "127.0.0.1:8008",
    "127.0.0.1:8010",
    "127.0.0.1:8011",
]

control_channels = [
    "127.0.0.1:8000/ws/blockchain/"
]

async def main():
    ip, port = args.listen.split(":")
    blockchain_path = "/tmp/blockchain_{}:{}.json".format(ip, port)
    async with RaftServer(ip, int(port), nodes, control_channels, blockchain_path) as _:
        pass

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())
    asyncio.get_event_loop().run_forever()


s="abcabcbb"
n = len(s)
sub_temp=''
for i in range(n):
    for j in range(i,n):
        sub_s = s[i:j]
        if(len(sub_s)==len(set(sub_s))):
            if(len(sub_s)>len(sub_temp)):
                sub_temp= sub_s

print(len(sub_temp))


import logging
import os
from threading import Thread

pypath = os.path.dirname(os.path.abspath(__file__))

def get_logger(name):
    log_file = os.path.join(pypath, "log/%s.log" % name)

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    f_handler = logging.FileHandler(log_file,mode='a')
    #s_handler = logging.StreamHandler()

    f_handler.setFormatter(formatter)
    #s_handler.setFormatter(formatter)

    level = logging.DEBUG
    logger = logging.getLogger(name)
    logger.setLevel(level)
    logger.addHandler(f_handler)
    #logger.addHandler(s_handler)

    return logger

def run(name):
    logger = get_logger(name)
    logger.warning('warning from thread_%s' % name)

for i in range(1, 10):
    thread = Thread(target=run, args=("thread_%d" %(i),))
    thread.start()
    thread.join()

print('main thread')


# Create a Critter Farm program by instantiating several Critter objects and keeping track of them through a list.
# A virtual pet to care for
# Challenge 04 - Chapter 08
# Alexandre CHOURA - 18/07/18

import random

class Critter(object):
	"""A virtual pet"""
	farm_mood = 0
	farm_hunger = 0

	def __init__(self, name, hunger = 0, boredom = 0):
		"""Give each pet their own name, hunger and boredom levels"""
		self.name = name
		self.hunger = random.randrange(0, 20)
		self.boredom = random.randrange(0,20)
		Critter.farm_hunger += self.hunger
		Critter.farm_mood += self.boredom

	def __str__(self):
		"""Will be used to append a critter's spec to a list regrouping every critter spec, by returning a tuple"""
		return self.name, self.hunger, self.boredom

	@property
	def mood_one(self):
		"""Check the current happininess of a critter"""
		unhappiness = self.hunger + self.boredom
		if unhappiness < 5:
			mo = "happy"
		elif 5 <= unhappiness <= 10:
			mo = "okay"
		elif 11 <= unhappiness <= 15:
			mo = "frustrated"
		else:
			mo = "mad"
		return mo

	def talking_one(self):
		"""The choosen critter will say his current mood"""
		print("I'm", self.name, "and I feel", self.mood_one, "now.")

	def feeding_one(self, food = 0):
		"""Feed one critter"""
		while food == 0:
			try:
				food = int(input("How many portion you want to give to " + self.name + "?: "))
			except ValueError:
				print("Enter a number foolish human.")
			else:
				print("Brruppp.  Thank you.")
		else:
			self.hunger -= food
			if self.hunger < 0:
				self.hunger = 0

	def play_one(self, fun = 0):
		"""Play with one critter"""
		while fun == 0:
			try:
				fun = int(input("How long you wanna play with " + self.name + "?: "))
			except ValueError:
				print("Enter a number foolish human.")
			else:
				print("Wheee!")
		else:
			self.boredom -= fun
			if self.boredom < 0:
				self.boredom = 0

	def pass_time(self):
		"""simulate the passing of time, to make them more hungry and bored"""
		self.hunger += 1
		self.boredom += 1

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Critter_Farm():

	def __str__(self):
		return "\tfarm_hunger = {}\n\tfarm_mood = {}".format(str(Critter.farm_hunger), str(Critter.farm_mood))

	@property
	def mood_all(self):
		"""Check the current happiness of the critter farm"""
		self.update()
		unhappiness_all = Critter.farm_mood + Critter.farm_hunger
		if unhappiness_all < 60:
			ma = "happy"
		elif 60 <= unhappiness_all <= 120:
			ma = "okay"
		elif 121 <= unhappiness_all <= 180:
			ma = "frustrated"
		else:
			ma = "mad"
		return ma

	def update(self):
		"""Update the boredom and hunger level with new values after pass_time()"""
		global critlist
		Critter.farm_mood = 0
		Critter.farm_hunger = 0
		# Adding every hunger level of critters to the farm counter
		for critter in critlist:
			Critter.farm_hunger += critter.__str__()[1]
		# Adding every boredom level of critters to the farm counter
		for critter in critlist:
			Critter.farm_mood += critter.__str__()[2]

	def talking_all(self):
		critlist = critters_list()
		"""Check the current mood of the farm"""
		# Print the current mood
		print("We're the critters, and we feel", self.mood_all, "now")
		for critter in critlist:
			critter.pass_time()
		self.update()

	def feeding_all(self, food = 0):
		"""Feed the farm"""
		critlist = critters_list()
		while food == 0:
			try:
				food = int(input("How many portion do you want to give to each of your critters ?: "))
			except ValueError:
				print("Enter a number foolish human.")
		else:
			print("Brruppp. Thank you.")
			return food

	def play_all(self, fun = 0):
		"""Play with the whole farm"""
		critlist = critters_list()
		while fun == 0:
			try:
				fun = int(input("How long do you wanna play with your critters ?: "))
			except ValueError:
				print("Enter a number foolish human.")
		else:
			print("Wheee!")
		return fun

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def critters_list():
	"""store every critters in the critlist"""
	cl = [Critter("Hallkel"),
			Critter("Steinkel"),
			Critter("Kolskegg"),
			Critter("Asgrim"),
			Critter("Eirik"),
			Critter("Knut"),
			Critter("Ulfbjorn"),
			Critter("Brondulf"),
			Critter("Lyting"),
			Critter("Osvif"),
			Critter("Asta"),
			Critter("Thora")]
	return cl

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def main():
	global critlist
	critlist = critters_list()
	critfarm = Critter_Farm()

	user_choice = None
	while user_choice != "0":
		print("""
			Critter Farm Caretaker

			0 - Quit
			1 - Listen to a critter
			2 - Listen the farm
			3 - Feed a critter
			4 - Feed the farm
			5 - Play with a critter
			6 - Play with the farm
			"""
			)

		user_choice = input("Choice: ")
		print()

		# Exit
		if user_choice == "0":
			print("Good-bye.")

		# Listen to a critter
		elif user_choice == "1":
			test = "stay"
			print("Enter the name of the critter you want to listen to (", end=" ")
			for critter in critlist:
				print(critter.__str__()[0], end=" ")
			print("):")
			while test != "leave":
				listen_critter_name = input("Choice: ")
				for critter in critlist:
					if listen_critter_name.lower() in critter.__str__()[0].lower():
						critter.talking_one()
						test = "leave"
						for ct in critlist:
							ct.pass_time()
				if test == "stay":
					print("Enter a valid name, foolish human.")
				else:
					print("Going back to the main menu.")
					critfarm.update()

		# Listen to the farm
		elif user_choice == "2":
			critfarm.talking_all()

		# Feed a critter
		elif user_choice == "3":
			test = "stay"
			print("Enter the name of the critter you want to feed (", end=" ")
			for critter in critlist:
				print(critter.__str__()[0], end=" ")
			print("):")
			while test != "leave":
				feed_critter_name = input("Choice: ")
				for critter in critlist:
					if feed_critter_name.lower() in critter.__str__()[0].lower():
						critter.feeding_one()
						test = "leave"
						for ct in critlist:
							ct.pass_time()
						critfarm.update()
				if test == "stay":
					print("Enter a valid name, foolish human.")
				else:
					print("Going back to the main menu.")

		# Feed the farm
		elif user_choice == "4":
			food = critfarm.feeding_all()
			for critter in critlist:
				critter.feeding_one(food)
				critter.pass_time()
			critfarm.update()
		# Play with a critter
		elif user_choice == "5":
			test = "stay"
			print("Enter the name of the critter you want to play with (", end=" ")
			for critter in critlist:
				print(critter.__str__()[0], end=" ")
			print("):")
			while test != "leave":
				feed_critter_name = input("Choice: ")
				for critter in critlist:
					if feed_critter_name.lower() in critter.__str__()[0].lower():
						critter.play_one()
						test = "leave"
						for ct in critlist:
							ct.pass_time()
						critfarm.update()
				if test == "stay":
					print("Enter a valid name, foolish human.")
				else:
					print("Going back to the main menu.")

		# Play with the farm
		elif user_choice == "6":
			fun = critfarm.play_all()
			for critter in critlist:
				critter.play_one(fun)
				critter.pass_time()
			critfarm.update()

		# Secret choice
		elif user_choice == "42":
			# Display individual spec
			print("\t\tIndividual spec:\n")
			for critter in critlist:
				print("    Name)" + critter.__str__()[0])
				print("    Hunger)" + str(critter.__str__()[1]))
				print("    Boredom)" + str(critter.__str__()[2]) + "\n")
			# Display farm's spec
			print("\t\tFarm's spec:\n")
			print(critfarm)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main()

input("\n\n<Press the enter key to exit.>")


def mergesort_naive(L):
	if len(L) <= 1:
		return L
	mid_idx = len(L) // 2
	# Left and Right will always be smaller than L as len(L) <= 1
	Left = mergesort_naive(L[:mid_idx])
	Right = mergesort_naive(L[mid_idx:])

	Output = []

	Left_idx = 0
	Right_idx = 0
	# While both left and right have elements remaining
	while Left_idx < len(Left) and Right_idx < len(Right):
		Left_entry = Left[Left_idx]
		Right_entry = Right[Right_idx]
		if Left_entry < Right_entry:
			Output.append(Left_entry)
			Left_idx += 1
		else:
			Output.append(Right_entry)
			Right_idx += 1

	# Here either Left or Right is empty, and the other contains the largest elements
	# in sorted order.
	return Output + Left[Left_idx:] + Right[Right_idx:]


# ==============================================================================
#  Copyright 2019 Intel Corporation
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
# ==============================================================================
"""nGraph TensorFlow bridge test for tf2ngraph script for precompilation

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

import pytest
import os
import numpy as np
import shutil
import tensorflow as tf
import ngraph_bridge

from tools.build_utils import command_executor
from tools.tf2ngraph import convert, get_gdef, Tf2ngraphJson

from common import NgraphTest

class Testtf2ngraph(NgraphTest):

    # utility function to make sure input format and location match
    @staticmethod
    def format_and_loc_match(format, loc):
        assert format in ['pb', 'pbtxt', 'savedmodel']
        implies = lambda p, q: (not p) or (p and q)
        #if its pbtxt, file name has pbtxt AND if its savedmodel, file name does not have pbtxt
        return implies(
            format == 'pbtxt', 'pbtxt' == loc.split('.')[-1]) and implies(
                format == 'savedmodel', 'pbtxt' != loc.split('.')[-1])

    @pytest.mark.parametrize(('commandline'),
                             (True,))  #TODO: add False for functional api test
    @pytest.mark.parametrize(('inp_format', 'inp_loc'), (
        ('pbtxt', 'sample_graph.pbtxt'),
        ('savedmodel', 'sample_graph'),
        ('pb', 'sample_graph.pb'),
        ('pbtxt', 'sample_graph_nodevice.pbtxt'),
    ))
    @pytest.mark.parametrize(('out_format',), (
        ('pbtxt',),
        ('pb',),
        ('savedmodel',),
    ))
    @pytest.mark.parametrize(('ng_device', 'shape_hints', 'precompile'),
                             (('CPU', [], False), ('INTERPRETER', [{}], True),
                              ('INTERPRETER', [], False)))
    # In sample_graph.pbtxt, the input shape is fully specified, so we don't need to pass shape hints for precompile
    def test_command_line_api(self, inp_format, inp_loc, out_format,
                              commandline, ng_device, shape_hints, precompile):
        # Only run this test when grappler is enabled
        if not ngraph_bridge.is_grappler_enabled():
            return
        assert Testtf2ngraph.format_and_loc_match(inp_format, inp_loc)
        out_loc = inp_loc.split('.')[0] + '_modified' + (
            '' if out_format == 'savedmodel' else ('.' + out_format))
        try:
            (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc)
        except:
            pass
        conversion_successful = False
        try:
            optional_backend_params = {
                'CPU': {
                    'device_config': '0'
                },
                'INTERPRETER': {
                    'test_echo': '1'
                }
            }[ng_device]
            config_file_name = 'temp_config_file.json'
            Tf2ngraphJson.dump_json(config_file_name, optional_backend_params,
                                    shape_hints)
            if commandline:
                # In CI this test is expected to be run out of artifacts/test/python
                command_executor(
                    'python ../../tools/tf2ngraph.py --input_' + inp_format +
                    ' ' + inp_loc + ' --output_nodes out_node --output_' +
                    out_format + ' ' + out_loc + ' --ng_backend ' + ng_device +
                    ' --config_file ' + config_file_name +
                    ("", " --precompile ")[precompile])
            else:
                convert(inp_format, inp_loc, out_format, out_loc, ['out_node'],
                        ng_device, optional_backend_params, shape_hints,
                        precompile)
            conversion_successful = True
        finally:
            if not conversion_successful:
                try:
                    (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc)
                    os.remove(config_file_name)
                except:
                    pass
        assert conversion_successful

        gdef = get_gdef(out_format, out_loc)
        (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc)
        os.remove(config_file_name)

        with tf.Graph().as_default() as g:
            tf.import_graph_def(gdef, name='')
            # The graph should have exactly one encapsulate
            assert len([
                0 for i in g.get_operations() if i.type == 'NGraphEncapsulate'
            ]) == 1
            # TODO: check that the encapsulate op has correct backend and extra params attached to it
            x = self.get_tensor(g, "x:0", False)
            y = self.get_tensor(g, "y:0", False)
            out = self.get_tensor(g, "out_node:0", False)

            sess_fn = lambda sess: sess.run(
                [out], feed_dict={i: np.zeros((10,)) for i in [x, y]})

            res1 = self.with_ngraph(sess_fn)
            res2 = self.without_ngraph(sess_fn)

            exp = [0.5 * np.ones((10,))]
            # Note both run on Host (because NgraphEncapsulate can only run on host)
            assert np.isclose(res1, res2).all()
            # Comparing with expected value
            assert np.isclose(res1, exp).all()


"""
Yum - Commands
==============
Parsers for ``yum`` commands.

This module contains the classes that parse the output of the commands
`yum -C --noplugins repolist`.

YumRepoList - command ``yum -C --noplugins repolist``
-----------------------------------------------------
"""
from insights.core import CommandParser
from insights.core.exceptions import ParseException, SkipComponent
from insights.core.plugins import parser
from insights.parsers import parse_fixed_table
from insights.specs import Specs

eus = [
    '5.0.z',
    '5.1.z',
    '5.2.z',
    '5.3.ll',
    '5.3.z',
    '5.4.z',
    '5.6.ll',
    '5.6.z',
    '5.9.ll',
    '5.9.z'
    '6.0.z',
    '6.1.z',
    '6.2.aus',
    '6.2.z',
    '6.3.z',
    '6.4.aus',
    '6.4.z',
    '6.5.aus',
    '6.5.z',
    '6.6.aus',
    '6.6.z',
    '6.7.z'
]

@parser(Specs.yum_repolist)
class YumRepoList(CommandParser):
    """
    Class for parsing the output of `yum -C --noplugins repolist` command.

    Typical output of the command is::

        repo id                                             repo name                                                                                status
        rhel-7-server-e4s-rpms/x86_64                       Red Hat Enterprise Linux 7 Server - Update Services for SAP Solutions (RPMs)                                 12,250
        !rhel-ha-for-rhel-7-server-e4s-rpms/x86_64          Red Hat Enterprise Linux High Availability (for RHEL 7 Server) Update Services for SAP Solutions (RPMs)         272
        *rhel-sap-hana-for-rhel-7-server-e4s-rpms/x86_64    RHEL for SAP HANA (for RHEL 7 Server) Update Services for SAP Solutions (RPMs)                                   21
        repolist: 12,768

    Or sometimes it just outputs repo id and status::

        repo id                                                                               status
        LME_EPEL_6_x86_64                                                                        26123
        LME_FSMLabs_Timekeeper_timekeeper                                                            2
        LME_HP_-_Software_Delivery_Repository_Firmware_Pack_for_ProLiant_-_6Server_-_Current      1163
        LME_HP_-_Software_Delivery_Repository_Scripting_Took_Kit_-_6Server                          17
        LME_HP_-_Software_Delivery_Repository_Service_Pack_for_ProLiant_-_6Server_-_Current       1915
        LME_HP_-_Software_Delivery_Repository_Smart_Update_Manager_-_6Server                        30
        LME_LME_Custom_Product_Mellanox_OFED                                                       114
        LME_LME_Custom_Product_OMD_RPMS                                                             14
        LME_LME_Custom_Product_RPMs                                                                  5
        LME_LME_Custom_Product_SNOW_Repository                                                       2
        rhel-6-server-optional-rpms                                                            10400+1
        rhel-6-server-rpms                                                                    18256+12
        rhel-6-server-satellite-tools-6.2-rpms                                                      55
        repolist: 58096

    Examples:
        >>> len(repolist)
        3
        >>> 'rhel-7-server-e4s-rpms/x86_64' in repolist.repos
        True
        >>> 'rhel-7-server-e4s-rpms' in repolist.repos
        False
        >>> 'rhel-7-server-e4s-rpms' in repolist.rhel_repos
        True
        >>> repolist['rhel-7-server-e4s-rpms/x86_64']['name']
        'Red Hat Enterprise Linux 7 Server - Update Services for SAP Solutions (RPMs)'
        >>> repolist[0]['name']
        'Red Hat Enterprise Linux 7 Server - Update Services for SAP Solutions (RPMs)'
        >>> repolist['rhel-ha-for-rhel-7-server-e4s-rpms/x86_64']['id']
        '!rhel-ha-for-rhel-7-server-e4s-rpms/x86_64'
        >>> len(repolist_no_reponame)
        13
        >>> len(repolist_no_reponame.rhel_repos)
        3
        >>> 'rhel-6-server-rpms' in repolist_no_reponame.repos
        True
        >>> 'rhel-6-server-optional-rpms' in repolist_no_reponame.rhel_repos
        True
        >>> repolist_no_reponame[0]['id']
        'LME_EPEL_6_x86_64'
        >>> repolist_no_reponame[0].get('name', '')
        ''

    Attributes:
        data (list): list of repos wrapped in dictionaries
        repos (dict): dict of all listed repos where the key is the full repo-id without "!" or "*".
            But you can get it from the value part if needed. For example::

                self.repos = {
                    'rhel-7-server-e4s-rpms/x86_64': {
                        'id': 'rhel-7-server-e4s-rpms/x86_64',
                        'name': 'Red Hat Enterprise Linux 7 Server - Update Services for SAP Solutions (RPMs)',
                        'status': '12,250'
                    },
                    'rhel-ha-for-rhel-7-server-e4s-rpms/x86_64': {
                        'id': '!rhel-ha-for-rhel-7-server-e4s-rpms/x86_64',
                        'name': 'Red Hat Enterprise Linux High Availability (for RHEL 7 Server) Update Services for SAP Solutions (RPMs)',
                        'status': '272'
                    },
                    'rhel-sap-hana-for-rhel-7-server-e4s-rpms/x86_64': {
                        'id': '*rhel-sap-hana-for-rhel-7-server-e4s-rpms/x86_64',
                        'name': 'RHEL for SAP HANA (for RHEL 7 Server) Update Services for SAP Solutions (RPMs)',
                        'status': '21'
                    }
                }

    """
    def parse_content(self, content):
        if not content:
            raise SkipComponent('No repolist.')

        if content[0].startswith('repolist:'):
            raise SkipComponent('No repolist.')

        trailing_line_prefix = [
                'repolist:',
                'Uploading Enabled',
                'Loaded plugins:',
                'Plugin ',
                'Unable to upload',
        ]

        self.data = []
        self.repos = {}
        try:
            self.data = parse_fixed_table(
                    content,
                    heading_ignore=['repo id'],
                    header_substitute=[('repo id', 'id     '), ('repo name', 'name     ')],
                    trailing_ignore=trailing_line_prefix,
                    empty_exception=True)
        except ValueError as e:
            # ValueError raised by parse_fixed_table
            raise ParseException('Failed to parser yum repolist: {0}'.format(str(e)))

        if not self.data:
            raise SkipComponent('No repolist.')

        self.repos = dict((d['id'].lstrip('!').lstrip('*'), d) for d in self.data)

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        if isinstance(idx, int):
            return self.data[idx]
        elif isinstance(idx, str):
            return self.repos[idx]

    def __contains__(self, _id):
        return _id in self.repos

    @property
    def eus(self):
        '''list of the EUS part of each repo'''
        euses = []
        for repo in [r["id"] for r in self.data]:
            if repo.startswith("rhel-") and "server-" in repo:
                _, eus_version = repo.split("server-", 1)
                if eus_version in eus:
                    euses.append(eus_version)
        return euses

    @property
    def rhel_repos(self):
        """
        list: list of all the rhel repos and the item is just the repo id without server and arch info.
            For example::

                self.rhel_repos = [
                    'rhel-7-server-e4s-rpms',
                    'rhel-ha-for-rhel-7-server-e4s-rpms',
                    'rhel-sap-hana-for-rhel-7-server-e4s-rpms'
                ]
        """
        return [i.split('/')[0]
                for i in self.repos
                if i.startswith('rhel-') or '-rhel-' in i]


# -*- coding=utf-8 -*-
import time
fname = 'niveles.txt'
with open(fname) as f:
    content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
lvls= int(content[0])
lvl_actual = 0
linea_actual = 1
ncoches = 0
lista_coches=[[] for i in range(lvls)]
#print type(lvls), lvls," tamaño: ",len(content),
#for i in range(len(content)):
#    print("content[",i,"]",content[i],"\n",)
#    time.sleep(2)
while lvl_actual < lvls:
    ncoches = int(content[linea_actual])
    ncoches_actual = 0
    print("content[",linea_actual,"], numero de coches: ",ncoches," nivel: ",lvl_actual,"\n",)
    linea_actual+=1
    while   ncoches_actual < ncoches:
        print("-coche numero ",ncoches_actual,"[",content[linea_actual],"]","\n",)
        lista_coches[lvl_actual].append(content[linea_actual])
        linea_actual+=1
        ncoches_actual+=1
    lvl_actual+=1
print('\n',lista_coches)


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

from django.db import models
from components.db import Procedure, ProcedureMixin, procedure_defaults

class mdlClsTechnologyCategoriesGetData(models.Manager, ProcedureMixin):

    def mdlFnSprocTechnologyCategoriesGrid(self):
        procedure = 'sproc_tec_technology_categories_grid'
        Procedure(self, procedure, [], [], [], True)
        return self.get_result()

class mdlClsTechnologyCategoriesDML(models.Model, ProcedureMixin):

    objIstTechnologyCategoriesGridData = mdlClsTechnologyCategoriesGetData()

    technology_category_id = models.BigAutoField(primary_key=True, default=0)
    technology_category = models.CharField(max_length=128)
    description = models.CharField(max_length=128)
    weightage = models.DecimalField(default=0, max_digits=10, decimal_places=2)
    scorecard_weightage = models.IntegerField(max_length=128)
    admin = models.IntegerField(default=0)
    active = models.IntegerField(default=0)

    def mdlFnSprocTechnologyCategoriesDetail(self, request):
        procedure = 'sproc_tec_technology_categories_detail'
        iparams = [
            ':iparam_technology_category_id',
        ]
        idata = self.get_input_params(iparams)
        iparams, idata = procedure_defaults(request, iparams, idata)
        Procedure(self, procedure, iparams, idata, [], True)
        return self.get_result()

    def mdlFnSprocTechnologyCategoriesInsert(self, request):
        procedure = 'sproc_tec_subscription_categories_dml_ins'
        iparams = [
            '-iparam_technology_category',
            '-iparam_description',
            '-iparam_weightage',
            ':iparam_admin',
        ]
        idata = self.get_input_params(iparams)
        iparams, idata = procedure_defaults(request, iparams, idata)
        Procedure(self, procedure, iparams, idata)

    def mdlFnSprocTechnologyCategoriesUpdate(self, request):
        procedure = 'sproc_tec_technology_categories_dml_upd'
        iparams = [
            ':iparam_technology_category_id',
            '-iparam_technology_category',
            '-iparam_description',
            '-iparam_weightage',
            ':iparam_admin',
        ]
        idata = self.get_input_params(iparams)
        iparams, idata = procedure_defaults(request, iparams, idata)
        Procedure(self, procedure, iparams, idata)

    def mdlFnSprocTechnologyCategoriesDelete(self, request):
        procedure = 'sproc_tec_technology_categories_dml_del'
        iparams = [
            ':iparam_technology_category_id'
        ]
        idata = [self.technology_category_id]
        iparams, idata = procedure_defaults(request, iparams, idata)
        Procedure(self, procedure, iparams, idata)


import FWCore.ParameterSet.Config as cms

process = cms.Process("prodRPinelasticBeta2p5Energy3500GeV")					##### 3.5 / 2.5

# Specify the maximum events to simulate
process.maxEvents = cms.untracked.PSet(
    input = cms.untracked.int32(1000)
)

# Configure the output module (save the result in a file)
process.o1 = cms.OutputModule("PoolOutputModule",
    outputCommands = cms.untracked.vstring('keep *', 'drop *_*_*TrackerHits*_*', 'drop *_*_*Muon*_*', 'drop *_*_*Ecal*_*', 'drop *_*_*Hcal*_*', 'drop *_*_*Calo*_*', 'drop *_*_*Castor*_*', 'drop *_*_*FP420SI_*', 'drop *_*_*ZDCHITS_*', 'drop *_*_*BSCHits_*', 'drop *_*_*ChamberHits_*', 'drop *_*_*FibreHits_*', 'drop *_*_*WedgeHits_*'),
    fileName = cms.untracked.string('file:prodRPinelasticBeta2p5Energy3500GeV.root')		##### 3.5 / 2.5
)

# Configure if you want to detail or simple log information.
# LoggerMax -- detail log info output including: errors.log, warnings.log, infos.log, debugs.log
# LoggerMin -- simple log info output to the standard output (e.g. screen)
process.load("Configuration.TotemCommon.LoggerMin_cfi")

################## STEP 1 - process.generator

# Use random number generator service
process.load("Configuration.TotemCommon.RandomNumbers_cfi")

# Monte Carlo gun
process.load("IOMC.FlatProtonLogKsiLogTGunMul.Beta2p5Energy3500GeV_cfi")			##### 3.5 / 2.5
process.source.ProtonNumberPerArm = cms.untracked.int32(3)

################## STEP 2 process.SmearingGenerator

# declare optics parameters
process.load("Configuration.TotemOpticsConfiguration.OpticsConfig_3500GeV_2p5_cfi")		##### 3.5 / 2.5

# Smearing
process.load("IOMC.SmearingGenerator.SmearingGenerator_cfi")
process.SmearingGenerator.originalLabel = 'original'
process.SmearingGenerator.modifyLabel = 'source'
# process.SmearingGenerator.verbosity = cms.untracked.uint32(6)

################## STEP 3 process.g4SimHits

# G4 Geometry - beta* specific
process.load("Geometry.VeryForwardGeometry.geometryRP_cfi")

# Magnetic Field, by default we have 3.8T
process.load("Configuration.StandardSequences.MagneticField_cff")

# G4 simulation & proton transport
process.load("Configuration.TotemCommon.g4SimHits_cfi")
process.g4SimHits.Physics.BeamProtTransportSetup = process.BeamProtTransportSetup
process.g4SimHits.Generator.HepMCProductLabel = 'source'

################## STEP 4
#### There are the following stages in this step
# process.mix*
# process.RPSiDetDigitizer*
# process.RPClustProd*
# process.TotemRPRecHitProd*
# process.RPSinglTrackCandFind*
# process.RPSingleTrackCandCollFit

# process.load("Configuration.TotemStandardSequences/RP_Digi_and_TrackReconstruction_cfi")

# Use particle table
process.load("SimGeneral.HepPDTESSource.pdt_cfi")

# No pile up for the mixing module
process.load("SimGeneral.MixingModule.mixNoPU_cfi")

# RP Strip digitization
process.load("SimTotem.RPDigiProducer.RPSiDetConf_cfi")
#RPSiDetDigitizer.RPVerbosity = 1

process.load("RecoTotemRP.RPClusterizer.RPClusterizationConf_cfi")
#RPClustProd.Verbosity = 1

process.load("RecoTotemRP.TotemRPRecHitProducer.TotemRPRecHitProdConf_cfi")
#TotemRPRecHitProd.Verbosity = 1

process.load("RecoTotemRP.RPMulCandidateTrackFinder.RPMulTrackCandFindConf_cfi")
#RPMulTrackCandFind.Verbosity = 1

################## My Analyzers
# process.load("MyAnalyzers.MCSourceAnalyzer.MCSource_Analyzer_cfi")

# process.load("MyAnalyzers.SmearAnalyzer.Smear_Analyzer_cfi")

# process.load("MyAnalyzers.MixAnalyzer.Mix_Analyzer_cfi")

# process.load("MyAnalyzers.TotemRPRecHitAnalyzer.TotemRPRecHit_Analyzer_cfi")

process.p1 = cms.Path(process.SmearingGenerator*process.g4SimHits*process.mix*process.RPSiDetDigitizer*process.RPClustProd*process.TotemRPRecHitProd*process.RPMulTrackCandFind)

# process.p1 = cms.Path(process.SmearingGenerator*process.g4SimHits*process.mix*process.RPSiDetDigitizer*process.RPClustProd*process.TotemRPRecHitProd*process.RPSinglTrackCandFind*process.RPSingleTrackCandCollFit)
process.outpath = cms.EndPath(process.o1)


from django.urls import path

from .views import StatusListAPIView, StatusDetailAPIView, StatusCreateAPIView, StatusUpdateAPIView, StatusDeleteAPIView

urlpatterns = [
  path('', StatusListAPIView.as_view()),
  path('<int:pk>/', StatusDetailAPIView.as_view()),
  path('create/', StatusCreateAPIView.as_view()),
  path('<int:pk>/update/', StatusUpdateAPIView.as_view()),
  path('<int:pk>/delete/', StatusDeleteAPIView.as_view()),
]


# _*_ coding:utf-8 _*_
import re
import json
import time
import requests
from multiprocessing import Process
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from helper import *
from Config import *
from save_to_mongo import save_mongo

def get_IA_redis():
	browser = webdriver.Chrome()
	wait = WebDriverWait(browser,10)
	IA_regex = re.compile(r"\['(.*?)', '(.*?)'\]")

	print('check_failed_url')
	check_result = get_fail_url(browser, wait)
	if check_result:
		print('start get IA_redis')
		while True:
			try:
				IA_index, IA_list = redisServer.blpop(REDIS_LIST_NAME,timeout = 180) # 阻塞10分钟
				IA_name, IA_id = IA_regex.findall(IA_list.decode('utf-8'))[0]
				# 加入redis_failed
				sadd_result = redisServer.sadd(REDIS_FAILD_NAME,IA_name+','+IA_id)
				if sadd_result == 1:
					get_IA_Json(IA_name, IA_id,browser, wait)
					redisServer.srem(REDIS_FAILD_NAME,IA_name+','+IA_id)
				else:
					browser.close()
					get_IA_redis()
			except Exception as e:
				print('spider OK！')
				browser.close()
				break

def get_IA_Json(IA_name, IA_id, browser, wait):
	time.sleep(2)
	url = ''.join(('https://www.itjuzi.com/investment/info/investment_records/','%s?type=one_year'%IA_id))
	browser.switch_to_window(browser.window_handles[0]) # 一直选择第一个窗口
	browser.get(url)
	wait.until(EC.text_to_be_present_in_element((By.TAG_NAME,'body'),'code')) # 当JS动态加载完成再获取内容
	html = BeautifulSoup(browser.page_source,'lxml')
	response_json = html.body.string
	json_data = json.loads(response_json)
	save_mongo(IA_name, IA_id, json_data)

def get_fail_url(browser, wait):
	while int(redisServer.scard(REDIS_FAILD_NAME)) != 0:
		spop_result = redisServer.spop(REDIS_FAILD_NAME)
		if spop_result is not None:
			name, ID = spop_result.decode('utf-8').split(',')
			get_IA_Json(name, ID, browser, wait)
		else:
			return True
	return True

if __name__ == '__main__':
	redisServer = redis_server(REDIS)
	process1=Process(target=get_IA_redis)
	process2 = Process(target = get_IA_redis)
	process1.start()
	process2.start()
	process1.join()
	process2.join()


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

from django.test import TestCase
from mock import Mock, patch

from eventkit_cloud.ui.data_estimator import get_size_estimate, get_gb_estimate

logger = logging.getLogger(__name__)

class TestDataEstimation(TestCase):

    def test_get_gb_estimate(self):
        expected_return_value = 0.0001572864
        actual_return_value = get_gb_estimate(4)
        self.assertAlmostEqual(expected_return_value, actual_return_value, places=9)

    @patch('eventkit_cloud.ui.data_estimator.get_gb_estimate')
    @patch('eventkit_cloud.ui.data_estimator.DataProvider')
    def test_get_size_estimate(self, export_provider, get_estimate):
        provider_name = "Test_name"
        get_estimate.return_value = 4
        export_provider.objects.get.return_value = Mock(level_from=0, level_to=1)
        returned_values = get_size_estimate(provider_name, bbox=[-1, -1, 0, 0])
        export_provider.objects.get.assert_called_once_with(name=provider_name)
        # two tiles, an arbritary value of four from the mock, one tile per level represented in array.
        expected_values = [2, 4, [1, 1]]
        self.assertEqual(returned_values, expected_values)


from django.db import models
from datetime import date
from vendas.models import Venda

class Caixa(models.Model):
    data = models.DateField(auto_now=True)
    valor_abertura = models.DecimalField(max_digits=7, decimal_places=3)

    @property
    def valor_especie(self):
        hj = date.today()
        vendas_do_dia = Venda.objects.filter(data=hj)
        total = 0
        vendas_dinheiro = vendas_do_dia.filter(pagamento='Dinheiro')
        for venda in vendas_dinheiro:
            total += venda.total
        sum = total + self.valor_abertura
        return sum