"""Myblog1 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
from django.urls import path
from .views import *

urlpatterns = [
    url(r'404/$', Page_Not_Found.as_view(), name='notfound'),
    url(r'^$', IndexView.as_view(template_name='index.html'), name='index'),  # 主页，自然排序
    url(r'^link/$', LinkView, name='link'),  # 申请友情链接
    url(r'^category/message/$', MessageView, name='message'),
    url(r'^category/about/$', AboutView, name='about'),

    url(r'^category/exchange/$', ExchangeView, name='exchange'),

    # 分类页面
    url(r'^category/(?P<bigslug>.*?)/(?P<slug>.*?)', IndexView.as_view(template_name='content.html'), name='category'),
    # 归档页面
    url(r'^date/(?P<year>\d+)/(?P<month>\d+)/$', IndexView.as_view(template_name='archive.html'), name='date'),
    # 标签页面
    url(r'^tag/(?P<tag>.*?)/$', IndexView.as_view(template_name='content.html'), name='tag'),
    # 文章详情页面
    url(r'^article/(?P<slug>.*?)/$', DetailView.as_view(), name='article'),
    # 全文搜索
    url(r'^search/$', MySearchView.as_view(), name='search'),
    # 喜欢
    url(r'^love/$', LoveView, name='love'),

    url(r'^(?P<username>\w+)/backend/$', backend),
    url(r'^(?P<username>\w+)/backend_add_article/$', backend_add_article, name='add_article'),
    url("backend/(\d+)/delete_article/$", delete_article, name='delete_article'),
    url("backend/(\d+)/edit_article/$", edit_article, name='edit_article'),

]


def getPair(line):

    counter = 0
    codeList = []
    commentList = []

    while(counter<len(line)):
        methodInfo = line[counter+0]
        pathToMain = line[counter+1]
        #print(line[1])
        commentLength = int(line[counter+2])

        comment = []
        for i in range(commentLength):
            comment.append(line[counter + i + 3])
        commentString = ' '.join(comment)
        commentList.append(commentString)
        codeLength = int(line[counter+ 3 + commentLength])

        code = []
        for i in range(codeLength):
            code.append(line[counter + i+4+commentLength])
        codeString = ' '.join(code)
        codeList.append(codeString)
        end = counter + commentLength+codeLength+5
        counter = end
        #print(counter)
    return codeList,commentList

with open("C:\\Users\\Kaushal\\Desktop\\DR\\input.txt") as f:
    lines = f.readlines()

    codeList, commentList = getPair(lines)

    print(len(codeList))
    print(len(commentList))


import asyncio
import websockets
import json
import pprint
from source.services.print_to_console import dashed_printer
from time import time
from source.settings.configurations import PAYLOAD_FILE_PATH, PAYLOAD_MEASUREMENT_SERVER_PORT, \
    PAYLOAD_MEASUREMENT_SERVER_ADDRESS

counter = 0
packages_to_write = []
time_length_of_probe = 60
time_to_finish = 0
socked_connected = False
time_0 = None

async def handler(websocket, _):
    global counter
    global packages_to_write
    global time_0
    global time_length_of_probe
    global socked_connected
    global time_to_finish
    if not socked_connected:
        time_0 = time()
        time_to_finish = int(time_0) + time_length_of_probe
        socked_connected = True
    time_counter = time()
    if time_counter < time_to_finish:
        with open(PAYLOAD_FILE_PATH, 'a') as f:
            payload = await websocket.recv()
            time_1 = time()
            if payload:
                counter += 1
                data_package = json.loads(payload)
                frame_time_separation = time_1 - time_0
                time_0 = time_1
                dashed_printer('Payload frame number {}'.format(counter))
                dashed_printer(' Received package format:')
                pprint.pprint(data_package)
                print('Package time separation is: {}'.format(frame_time_separation))
                dashed_printer(' -- -- -- end of payload frame -- -- --')
                packages_to_write.append({
                    "time": time(),
                    "frame": payload,
                    "frame_time_separation": frame_time_separation
                })
                if counter % 30 == 0:
                    dashed_printer('Writing to a file')
                    f.write(str(json.dumps(packages_to_write)))
                    f.write('\n')
                    del packages_to_write[:]
    else:
        if len(packages_to_write) > 0:
            with open(PAYLOAD_FILE_PATH, 'a') as f:
                dashed_printer('Writing to a file')
                f.write(str(json.dumps(packages_to_write)))
                f.write('\n')
                del packages_to_write[:]
            dashed_printer('PROBE TIME IS REACHED, WRITING TO A FILE IS STOPPED, YOU CAN STOP THE SERVER WITH ctrl + c')

if __name__ == '__main__':
    time_length_of_probe = int(input('Specify time span of this probe: '))
    asyncio.get_event_loop().run_until_complete(
        websockets.serve(handler, PAYLOAD_MEASUREMENT_SERVER_ADDRESS, PAYLOAD_MEASUREMENT_SERVER_PORT))
    asyncio.get_event_loop().run_forever()


#!/usr/bin/env python

import rospy

from geometry_msgs.msg import Twist
from base_drive_chain.msg import ThorvaldTwist
#import time

class MessageConverter(object):

    def __init__(self):

        rospy.Subscriber("/cmd_vel_thorval", Twist, self.callback)
        self.pub = rospy.Publisher("/cmd_vel", ThorvaldTwist)
        rospy.spin()

    def callback(self, data):
        #rospy.loginfo(rospy.get_caller_id() + "I heard %s", data)
        #print data
        cmd = ThorvaldTwist()
        cmd.drive_mode=0

        cmd.twist.twist.twist = data
        self.pub.publish(cmd)

if __name__ == '__main__':
    rospy.init_node('message_converter')
    MessageConverter()


class TradeInfo:
    """
    解析书籍信息
    """
    def __init__(self, goods):
        self.total = 0
        self.trade = []
        self._parse(goods)

    def _parse(self, goods):
        self.total = len(goods)
        self.trade = [self._map_to_trade(i) for i in goods]

    @staticmethod
    def _map_to_trade(good):
        time = good.create_datetime.strftime('%Y-%m-%d') if good.create_datetime else None
        return dict(
            user_name=good.user.nickname,
            time=time,
            id=good.id,
        )


#=======================================================
# 기본 위젯 2
#=======================================================
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QVBoxLayout
from PyQt5.QtWidgets import QComboBox, QLabel, QLineEdit, QPushButton
from PyQt5.QtWidgets import QProgressBar, QSlider, QDial
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt, QBasicTimer

class MyApp(QWidget):

    #================================
    # 초기화 동작 수행
    #================================
    def __init__(self):
        super().__init__()
        self.initUI()           # 기본 UI 초기화

    #=================================
    # 윈도우를 화면 가운데 위치 시키기
    #=================================
    def center(self):
        qr = self.frameGeometry()  # 창의 위치와 크기 정보를 가져
        cp = QDesktopWidget().availableGeometry().center()  # 사용하는 모니터 화면의 가운데 위치를 파악
        qr.moveCenter(cp)  # 창의 직사각형 위치를 화면의 중심의 위치로 이동합니다.
        self.move(qr.topLeft())

    # ================================
    # 화면 기본 설정
    # ================================
    def initUI(self):

        # 슬라이더
        self.slider = QSlider(Qt.Horizontal, self)
        self.slider.move(350, 50)
        self.slider.setRange(0, 50)
        self.slider.setSingleStep(2)

        # 다이얼
        self.dial = QDial(self)
        self.dial.move(350, 80)
        self.dial.setRange(0, 50)

        btnDefault = QPushButton('Default', self)
        btnDefault.move(355, 190)

        self.lblVal = QLabel(self)
        self.lblVal.move(450, 195)

        self.slider.valueChanged.connect(self.dial.setValue)
        self.slider.valueChanged.connect(self.showValue)
        self.dial.valueChanged.connect(self.slider.setValue)
        btnDefault.clicked.connect(self.button_clicked)

        # 프로그래스 바
        self.progbar = QProgressBar(self)
        self.progbar.setGeometry(50, 150, 300, 25)

        # 푸시 버튼
        self.btn = QPushButton('Start', self)
        self.btn.move(150, 190)
        self.btn.clicked.connect(self.doAction)

        # 타이머 설정
        self.timer = QBasicTimer()
        self.step = 0

        # 라벨, 에디트
        self.lblStr = QLabel(self)
        self.lblStr.move(160, 40)

        lineEdit = QLineEdit(self)
        lineEdit.move(160, 100)
        lineEdit.textChanged[str].connect(self.onChanged)   # 텍스트가 바뀌면 onChanged 호출

        # 라벨, 콤보
        self.lbl = QLabel("선택 내용", self)
        self.lbl.move(50, 100)

        comb = QComboBox(self)
        comb.addItem("선택1")
        comb.addItem("선택2")
        comb.addItem("선택3")
        comb.addItem("선택4")
        comb.move(50, 40)

        # 선택했을 때 함수 호출
        comb.activated[str].connect(self.onActivated)

        self.setWindowTitle("내 어플리케이션")           # 타이틀
        self.setWindowIcon(QIcon('../images/web.png'))  # 아이콘 추가
        self.resize(600, 250)                             # 크기 설정
        self.center()
        self.show()                                       # 보이기

    # 콤보박스에서 선택 시
    def onActivated(self, text):
        self.lbl.setText(text)
        self.lbl.adjustSize()       # 라벨의 크기 조절

    # 라인에디터에서 글자를 수정할 때
    def onChanged(self, text):
        self.lblStr.setText(text)
        self.lblStr.adjustSize()

    # 프로그래시브 바 버튼 눌렀을 때 동작
    def doAction(self):
        if self.timer.isActive():
            self.timer.stop()
            self.btn.setText('Start')
        else:
            self.timer.start(100, self)
            self.btn.setText('Stop')

    # QObject와 그 자손들은 timerEvent() 이벤트 핸들러를 갖는다
    def timerEvent(self, e):
        if self.step >= 100:
            self.timer.stop()
            self.btn.setText('Finished')
            self.step = 0
            return

        self.step = self.step + 1
        self.progbar.setValue(self.step)

    # 버튼이 눌렸을 때 수행할 동작 수행
    def button_clicked(self):
        self.slider.setValue(0)
        self.dial.setValue(0)

    # 슬라이더의 값을 라벨에 출력
    def showValue(self):
        self.lblVal.setText(str(self.slider.value()))
        self.lblVal.adjustSize()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec())


from globalTypes import *
from scanner import *

token = None # holds current token
tokenString = None # holds the token string value
Error = False
#lineno = 1
SintaxTree = None
imprimeScanner = False

def syntaxError(message):
    global Error
    print(">>> Syntax error at line " + str(lineno) + ": " + message, end='')
    Error = True

def match(expected):
    global token, tokenString, lineno
    if (token == expected):
        token, tokenString, lineno = getToken(imprimeScanner)
        #print("TOKEN:", token, lineno)
    else:
        syntaxError("unexpected token -> ")
        printToken(token,tokenString)
        print("      ")

def stmt_sequence():
    t = statement()
    p = t
    while ((token!=TokenType.ENDFILE) and (token!=TokenType.END)
           and (token!=TokenType.ELSE) and (token!=TokenType.UNTIL)):
        match(TokenType.SEMI)
        q = statement()
        if (q!=None):
            if (t==None):
                t = p = q
            else: # now p cannot be NULL either
                p.sibling = q
                p = q
    return t

def statement():
    global token, tokenString, lineno
    #print("STATEMENT: ", token, lineno)
    t = None;
    if token == TokenType.IF:
        t = if_stmt()
    elif token == TokenType.REPEAT:
        t = repeat_stmt()
    elif token == TokenType.ID:
        t = assign_stmt()
    elif token == TokenType.READ:
        t = read_stmt()
    elif token == TokenType.WRITE:
        t = write_stmt()
    else:
        syntaxError("unexpected token -> ")
        printToken(token,tokenString)
        token, tokenString, lineno = getToken()
    return t

def if_stmt():
    t = newStmtNode(StmtKind.IfK)
    match(TokenType.IF)
    if (t!=None):
        t.child[0] = exp()
    match(TokenType.THEN)
    if (t!=None):
        t.child[1] = stmt_sequence()
    if (token==TokenType.ELSE):
        match(TokenType.ELSE)
        if (t!=None):
            t.child[2] = stmt_sequence()
    match(TokenType.END)
    return t

def repeat_stmt():
    t = newStmtNode(StmtKind.RepeatK)
    match(TokenType.REPEAT)
    if (t!=None):
        t.child[0] = stmt_sequence()
    match(TokenType.UNTIL);
    if (t!=None):
        t.child[1] = exp()
    return t

def assign_stmt():
    t = newStmtNode(StmtKind.AssignK)
    if ((t!=None) and (token==TokenType.ID)):
        t.name = tokenString
    match(TokenType.ID)
    match(TokenType.ASSIGN)
    if (t!=None):
        t.child[0] = exp()
    return t

def read_stmt():
    t = newStmtNode(StmtKind.ReadK)
    match(TokenType.READ)
    if ((t!=None) and (token==TokenType.ID)):
        t.name = tokenString
    match(TokenType.ID)
    return t

def write_stmt():
    t = newStmtNode(StmtKind.WriteK)
    match(TokenType.WRITE)
    if (t!=None):
        t.child[0] = exp()
    return t

def exp():
    global token, tokenString
    t = simple_exp()
    if ((token==TokenType.LT) or (token==TokenType.EQ)):
        p = newExpNode(ExpKind.OpK)
        if (p!=None):
          p.child[0] = t
          p.op = token
          t = p
        match(token)
        if (t!=None):
            t.child[1] = simple_exp()
    return t

def simple_exp():
    t = term()
    while ((token==TokenType.PLUS) or (token==TokenType.MINUS)):
        p = newExpNode(ExpKind.OpK)
        if (p!=None):
          p.child[0] = t
          p.op = token
          t = p
          match(token)
          t.child[1] = term()
    return t

def term():
    t = factor();
    while ((token==TokenType.TIMES) or (token==TokenType.OVER)):
        p = newExpNode(ExpKind.OpK)
        if (p!=None):
            p.child[0] = t
            p.op = token
            t = p
            match(token)
            t.child[1] = factor()
    return t

def factor():
    global token, tokenString
    t = None
    if token == TokenType.NUM:
        t = newExpNode(ExpKind.ConstK)
        if ((t!=None) and (token==TokenType.NUM)):
            t.val = int(tokenString)
        match(TokenType.NUM)
    elif token == TokenType.ID:
        t = newExpNode(ExpKind.IdK)
        if ((t!=None) and (token==TokenType.ID)):
            t.name = tokenString
        match(TokenType.ID)
    elif token == TokenType.LPAREN:
        match(TokenType.LPAREN)
        t = exp()
        match(TokenType.RPAREN)
    else:
        syntaxError("unexpected token -> ")
        printToken(token,tokenString)
        token, tokenString, lineno = getToken()
    return t

# Function newStmtNode creates a new statement
# node for syntax tree construction
def newStmtNode(kind):
    t = TreeNode();
    if (t==None):
        print("Out of memory error at line " + lineno)
    else:
        #for i in range(MAXCHILDREN):
        #    t.child[i] = None
        #t.sibling = None
        t.nodekind = NodeKind.StmtK
        t.stmt = kind
        t.lineno = lineno
    return t

# Function newExpNode creates a new expression
# node for syntax tree construction

def newExpNode(kind):
    t = TreeNode()
    if (t==None):
        print("Out of memory error at line " + lineno)
    else:
        #for i in range(MAXCHILDREN):
        #    t.child[i] = None
        #t.sibling = None
        t.nodekind = NodeKind.ExpK
        t.exp = kind
        t.lineno = lineno
        t.type = ExpType.Void
    return t

# Procedure printToken prints a token
# and its lexeme to the listing file
def printToken(token, tokenString):
    if token in {TokenType.IF, TokenType.THEN, TokenType.ELSE, TokenType.END,
                 TokenType.REPEAT, TokenType.UNTIL, TokenType.READ,
                 TokenType.WRITE}:
      print(" reserved word: " + tokenString);
    elif token == TokenType.ASSIGN:
        print(":=")
    elif token == TokenType.LT:
        print("<")
    elif token == TokenType.EQ:
        print("=")
    elif token == TokenType.LPAREN:
        print(listing,"(")
    elif token == TokenType.RPAREN:
        print(")")
    elif token == TokenType.SEMI:
        print(";")
    elif token == TokenType.PLUS:
        print("+")
    elif token == TokenType.MINUS:
        print("-")
    elif token == TokenType.TIMES:
        print("*")
    elif token == TokenType.OVER:
        print("/")
    elif token == TokenType.ENDFILE:
        print("EOF")
    elif token == TokenType.NUM:
      print("NUM, val= " + tokenString)
    elif token == ID:
        print("ID, name= " + tokenString);
    elif token == TokenType.ERROR:
        print("ERROR: " + tokenString)
    else: # should never happen
        print("Unknown token: " + token)

# Variable indentno is used by printTree to
# store current number of spaces to indent
indentno = 0

# printSpaces indents by printing spaces */
def printSpaces():
    print(" "*indentno, end = "")

# procedure printTree prints a syntax tree to the
# listing file using indentation to indicate subtrees
def printTree(tree):
    global indentno
    indentno+=2 # INDENT
    while tree != None:
        printSpaces();
        if (tree.nodekind==NodeKind.StmtK):
            if tree.stmt == StmtKind.IfK:
                print(tree.lineno, "If")
            elif tree.stmt == StmtKind.RepeatK:
                print(tree.lineno, "Repeat")
            elif tree.stmt == StmtKind.AssignK:
                print(tree.lineno, "Assign to: ",tree.name)
            elif tree.stmt == StmtKind.ReadK:
                print(tree.lineno, "Read: ",tree.name)
            elif tree.stmt == StmtKind.WriteK:
                print(tree.lineno, "Write")
            else:
                print(tree.lineno, "Unknown ExpNode kind")
        elif tree.nodekind==NodeKind.ExpK:
            if tree.exp == ExpKind.OpK:
                print(tree.lineno, "Op: ", end ="")
                printToken(tree.op," ")
            elif tree.exp == ExpKind.ConstK:
                print(tree.lineno, "Const: ",tree.val)
            elif tree.exp == ExpKind.IdK:
                print(tree.lineno, "Id: ",tree.name)
            else:
                print(tree.lineno, "Unknown ExpNode kind")
        else:
            print(tree.lineno, "Unknown node kind");
        for i in range(MAXCHILDREN):
            printTree(tree.child[i])
        tree = tree.sibling
    indentno-=2 #UNINDENT

# the primary function of the parser
# Function parse returns the newly
# constructed syntax tree

def parse(imprime = True):
    global token, tokenString, lineno
    token, tokenString, lineno = getToken(imprimeScanner)
    t = stmt_sequence()
    if (token != TokenType.ENDFILE):
        syntaxError("Code ends before file\n")
    if imprime:
        printTree(t)
    return t, Error

def recibeParser(prog, pos, long): # Recibe los globales del main
    recibeScanner(prog, pos, long) # Para mandar los globales

#syntaxTree = parse()


#! python3
import webbrowser
import sys
import bs4
import requests
import openpyxl
import os
import xlsxwriter
import re

rootSite = 'http://wiki.wargaming.net/en/World_of_Warships'

#shipclasses = ['DD', 'CA', 'BB', 'CV']
shipclasses = ['DD', 'CA', 'BB']
# shipclasses = ['CA', 'BB']
# skiplist = ['St. Louis']

for shipclass in shipclasses:

    if shipclass == 'DD':
        shipsite = 'http://wiki.wargaming.net/en/Ship:Destroyers'
    elif shipclass == 'CA':
        shipsite = 'http://wiki.wargaming.net/en/Ship:Cruisers'
    elif shipclass == 'BB':
        shipsite = 'http://wiki.wargaming.net/en/Ship:Battleships'
    elif shipclass == 'CV':
        shipsite = 'http://wiki.wargaming.net/en/Ship:Aircraft_Carriers'

    ##### creating the resultfile

    shipfilename = shipclass+'results.xlsx'
    relpath = os.path.abspath(os.path.dirname(__file__))
    resultfile = relpath+"\\"+shipfilename

    if os.path.exists(resultfile):
        os.remove(resultfile)
        print('deleted '+resultfile)
    print('creating file with xlswriter')
    xlswritership = xlsxwriter.Workbook(shipfilename)
    xlswritership.close()
    shipsheet = openpyxl.load_workbook(shipfilename)
    firstsheet = shipsheet['Sheet1']
    firstsheet.title = 'BattleLevel 1'
    shipsheet.create_sheet('BattleLevel 2')
    shipsheet.create_sheet('BattleLevel 3')
    shipsheet.create_sheet('BattleLevel 4')
    shipsheet.create_sheet('BattleLevel 5')
    shipsheet.create_sheet('BattleLevel 6')
    shipsheet.create_sheet('BattleLevel 7')
    shipsheet.create_sheet('BattleLevel 8')
    shipsheet.create_sheet('BattleLevel 9')
    shipsheet.create_sheet('BattleLevel 10')
    #resultfile created

    ##### setting the top row of each sheet

    toprow = [
        'shipName',
        'shipClass',
        'shipCountry',
        'shipTier',
        'shipCurrency',
        'shipCost',
        'shipHitPoints',
        'shipResearch',
        'shipMainGun',
        'shipMainGunCount',
        'shipMainGunROF',
        'shipMainGunReload',
        'shipMainGunRotate',
        'shipMainGun180',
        'shipMainGunRange',
        'shipMainGunDispersion',
        'shipMainGunHEShell',
        'shipMainGunHEDam',
        'shipMainGunHEFire',
        'shipMainGunHEVel',
        'shipMainGunHEWeight',
        'shipMainGunAPShell',
        'shipMainGunAPDam',
        'shipMainGunAPVel',
        'shipMainGunAPWeight'
    ]
    for sheet in shipsheet.worksheets:
        # print(toprow)
        for n in range(1,len(toprow)+1):
            sheet.cell(row=1,column=n).value = toprow[n-1]

    #testing the root site

    # rootres = requests.get(rootsite)
    # rootres.raise_for_status()
    # rootSoup = bs4.BeautifulSoup(rootres.text)

    ##TODO - turn this into something that isn't restricted to DD
    #get destroyer site
    pagereq = requests.get(shipsite)
    pagereq.raise_for_status()
    pagesoup = bs4.BeautifulSoup(pagereq.text, "html.parser")

    #print japanese DD names
    nationDivs = pagesoup.find_all("div",class_="wot-frame-1")

    for nation in nationDivs:

        print('====================')
        print(nation.find("h2").text)
        print('====================')

        nationShips = nation.find_all("div",class_="tleft")
        for thisShip in nationShips:
            fulllink = 'http://wiki.wargaming.net'+(thisShip.find_all("a"))[1].attrs['href']
            getshipname = (thisShip.find_all("a"))[1].text
            print('regex time')
            if (re.search("St.*Louis",getshipname)) or (re.search("Nueve.de.Julio",getshipname)):
                print('found it')
            else:

                shiparray = {}

                shiparray['shipName'] = getshipname

                print('----- ' + shiparray['shipName'])
                # print(fulllink)
                thisShipPage = requests.get(fulllink)
                thisShipPage.raise_for_status()
                thisShipSoup = bs4.BeautifulSoup(thisShipPage.text, "html.parser")

                #gets class, country, tier
                perfdiv = thisShipSoup.find('div',class_='b-performance_position')
                shiparray['shipClass'] = perfdiv.text.split(' | ')[0]
                shiparray['shipCountry'] = perfdiv.text.split(' | ')[1]
                shiparray['shipTier'] = perfdiv.text.split(' | ')[2]
                # print(shiparray)
                #gets general, armament, toobs, maneuverability, concealment
                shipstats = thisShipSoup.find('div',class_='b-performance_border').find_all('div',class_='gw-popup-card b-tech-nav_item__opened')
                for statgroup in shipstats:
                    groupName = statgroup.find('div',class_='b-performance_title gw-popup-card_head js-tech-nav_head').text
                    # print('--')
                    # print(groupName)
                    # print('--')
                    stats = statgroup.find('div',class_='gw-popup-card_content').find_all('tr')

                    ###GENERAL STAT GROUP

                    if groupName == 'General':

                        # shipCurrency = ''
                        # shipCost = ''
                        # shipHitPoints = ''
                        # shipResearch = ''
                        for stat in stats:
                            # print('--------------')
                            ##TODO - strip whitespace from variables
                            if stat.find('span',class_='t-performance_left').text == 'Purchase price':
                                shiparray['shipCurrency'] = stat.find('img')['alt']
                                shiparray['shipCost'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Hit Points':
                                shiparray['shipHitPoints'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Research price':
                                shiparray['shipResearch'] = stat.find('span',class_='t-performance_right').text

                        # if shipCost != 'promo':
                        #     print('Currency: '+shipCurrency)
                        # print('Cost: '+shipCost)
                        # print('HP: '+shipHitPoints)
                        # if shipResearch != '':
                        #     print('ResearchXP: '+shipResearch)
                        #print(stats)

                    ###MAIN BATTERY

                    elif groupName == 'Main Battery':
                        # shipMainGun = ''
                        # shipMainGunCount = ''
                        # shipMainGunROF = ''
                        # shipMainGunReload = ''
                        # shipMainGunRotate = ''
                        # shipMainGun180 = ''
                        # shipMainGunRange = ''
                        # shipMainGunDispersion = ''
                        # shipMainGunHEShell = ''
                        # shipMainGunHEDam = ''
                        # shipMainGunHEFire = ''
                        # shipMainGunHEVel = ''
                        # shipMainGunHEWeight = ''
                        # shipMainGunAPShell = ''
                        # shipMainGunAPDam = ''
                        # shipMainGunAPVel = ''
                        # shipMainGunAPWeight = ''

                        shiparray['shipMainGun'] = stats[0].find('span',class_='t-performance_left').text
                        shiparray['shipMainGunCount'] = stats[0].find('span',class_='t-performance_right').text

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Rate of Fire':
                                shiparray['shipMainGunROF'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Reload Time':
                                shiparray['shipMainGunReload'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Rotation Speed':
                                shiparray['shipMainGunRotate'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == '180 Degree Turn Time':
                                shiparray['shipMainGun180'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Firing Range':
                                shiparray['shipMainGunRange'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum Dispersion':
                                shiparray['shipMainGunDispersion'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell':
                                shiparray['shipMainGunHEShell'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum HE Shell Damage':
                                shiparray['shipMainGunHEDam'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Chance of Fire on Target Caused by HE Shell':
                                shiparray['shipMainGunHEFire'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial HE Shell Velocity':
                                shiparray['shipMainGunHEVel'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell Weight':
                                shiparray['shipMainGunHEWeight'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell':
                                shiparray['shipMainGunAPShell'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum AP Shell Damage':
                                shiparray['shipMainGunAPDam'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial AP Shell Velocity':
                                shiparray['shipMainGunAPVel'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell Weight':
                                shiparray['shipMainGunAPWeight'] = stat.find('span',class_='t-performance_right').text

                        # print(shipMainGun)
                        # print(shipMainGunCount)
                        # print(shipMainGunROF)
                        # print(shipMainGunReload)
                        # print(shipMainGunRotate)
                        # print(shipMainGun180)
                        # print(shipMainGunRange)
                        # print(shipMainGunDispersion)
                        # print(shipMainGunHEShell)
                        # print(shipMainGunHEDam)
                        # print(shipMainGunHEFire)
                        # print(shipMainGunHEVel)
                        # print(shipMainGunHEWeight)
                        # print(shipMainGunAPShell)
                        # print(shipMainGunAPDam)
                        # print(shipMainGunAPVel)
                        # print(shipMainGunAPWeight)

                    ###Secondary Batteries

                    elif groupName == 'Secondary Armament #1':
                        shipSec1Gun = ''
                        shipSec1GunCount = ''
                        shipSec1GunROF = ''
                        shipSec1GunReload = ''
                        shipSec1GunRotate = ''
                        shipSec1Gun180 = ''
                        shipSec1GunRange = ''
                        shipSec1GunDispersion = ''
                        shipSec1GunHEShell = ''
                        shipSec1GunHEDam = ''
                        shipSec1GunHEFire = ''
                        shipSec1GunHEVel = ''
                        shipSec1GunHEWeight = ''
                        shipSec1GunAPShell = ''
                        shipSec1GunAPDam = ''
                        shipSec1GunAPVel = ''
                        shipSec1GunAPWeight = ''

                        shipSec1Gun = stats[0].find('span',class_='t-performance_left').text
                        shipSec1GunCount = stats[0].find('span',class_='t-performance_right').text

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Rate of Fire':
                                shipSec1GunROF = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Reload Time':
                                shipSec1GunReload = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Rotation Speed':
                                shipSec1GunRotate = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == '180 Degree Turn Time':
                                shipSec1Gun180 = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Firing Range':
                                shipSec1GunRange = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum Dispersion':
                                shipSec1GunDispersion = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell':
                                shipSec1GunHEShell = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum HE Shell Damage':
                                shipSec1GunHEDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Chance of Fire on Target Caused by HE Shell':
                                shipSec1GunHEFire = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial HE Shell Velocity':
                                shipSec1GunHEVel = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell Weight':
                                shipSec1GunHEWeight = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell':
                                shipSec1GunAPShell = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum AP Shell Damage':
                                shipSec1GunAPDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial AP Shell Velocity':
                                shipSec1GunAPVel = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell Weight':
                                shipSec1GunAPWeight = stat.find('span',class_='t-performance_right').text

                    elif groupName == 'Secondary Armament #2':
                        shipSec2Gun = ''
                        shipSec2GunCount = ''
                        shipSec2GunROF = ''
                        shipSec2GunReload = ''
                        shipSec2GunRotate = ''
                        shipSec2Gun180 = ''
                        shipSec2GunRange = ''
                        shipSec2GunDispersion = ''
                        shipSec2GunHEShell = ''
                        shipSec2GunHEDam = ''
                        shipSec2GunHEFire = ''
                        shipSec2GunHEVel = ''
                        shipSec2GunHEWeight = ''
                        shipSec2GunAPShell = ''
                        shipSec2GunAPDam = ''
                        shipSec2GunAPVel = ''
                        shipSec2GunAPWeight = ''

                        shipSec2Gun = stats[0].find('span',class_='t-performance_left').text
                        shipSec2GunCount = stats[0].find('span',class_='t-performance_right').text

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Rate of Fire':
                                shipSec2GunROF = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Reload Time':
                                shipSec2GunReload = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Rotation Speed':
                                shipSec2GunRotate = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == '180 Degree Turn Time':
                                shipSec2Gun180 = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Firing Range':
                                shipSec2GunRange = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum Dispersion':
                                shipSec2GunDispersion = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell':
                                shipSec2GunHEShell = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum HE Shell Damage':
                                shipSec2GunHEDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Chance of Fire on Target Caused by HE Shell':
                                shipSec2GunHEFire = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial HE Shell Velocity':
                                shipSec2GunHEVel = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell Weight':
                                shipSec2GunHEWeight = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell':
                                shipSec2GunAPShell = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum AP Shell Damage':
                                shipSec2GunAPDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial AP Shell Velocity':
                                shipSec2GunAPVel = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell Weight':
                                shipSec2GunAPWeight = stat.find('span',class_='t-performance_right').text

                    elif groupName == 'Secondary Armament #3':
                        shipSec3Gun = ''
                        shipSec3GunCount = ''
                        shipSec3GunROF = ''
                        shipSec3GunReload = ''
                        shipSec3GunRotate = ''
                        shipSec3Gun180 = ''
                        shipSec3GunRange = ''
                        shipSec3GunDispersion = ''
                        shipSec3GunHEShell = ''
                        shipSec3GunHEDam = ''
                        shipSec3GunHEFire = ''
                        shipSec3GunHEVel = ''
                        shipSec3GunHEWeight = ''
                        shipSec3GunAPShell = ''
                        shipSec3GunAPDam = ''
                        shipSec3GunAPVel = ''
                        shipSec3GunAPWeight = ''

                        shipSec3Gun = stats[0].find('span',class_='t-performance_left').text
                        shipSec3GunCount = stats[0].find('span',class_='t-performance_right').text

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Rate of Fire':
                                shipSec3GunROF = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Reload Time':
                                shipSec3GunReload = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Rotation Speed':
                                shipSec3GunRotate = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == '180 Degree Turn Time':
                                shipSec3Gun180 = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Firing Range':
                                shipSec3GunRange = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum Dispersion':
                                shipSec3GunDispersion = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell':
                                shipSec3GunHEShell = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum HE Shell Damage':
                                shipSec3GunHEDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Chance of Fire on Target Caused by HE Shell':
                                shipSec3GunHEFire = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial HE Shell Velocity':
                                shipSec3GunHEVel = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell Weight':
                                shipSec3GunHEWeight = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell':
                                shipSec3GunAPShell = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum AP Shell Damage':
                                shipSec3GunAPDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial AP Shell Velocity':
                                shipSec3GunAPVel = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell Weight':
                                shipSec3GunAPWeight = stat.find('span',class_='t-performance_right').text

                    ### Torpedo Tubes

                    elif groupName == 'Torpedo Tubes':
                        shipTorpTube = ''
                        shipTorpCount = ''
                        shipTorpReload = ''
                        shipTorpRorate = ''
                        shipTorp180 = ''
                        shipTorp = ''
                        shipTorpDam = ''
                        shipTorpSpeed = ''
                        shipTorpRange = ''

                        shipTorpTube = stats[0].find('span',class_='t-performance_left').text
                        shipTorpCount = stats[0].find('span',class_='t-performance_right').text

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Initial HE Shell Velocity':
                                shipTorpReload = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell Weight':
                                shipTorpRorate = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell':
                                shipTorp180 = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum AP Shell Damage':
                                shipTorp = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial AP Shell Velocity':
                                shipTorpDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell Weight':
                                shipTorpSpeed = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell Weight':
                                shipTorpRange = stat.find('span',class_='t-performance_right').text

                    elif groupName == 'AA Defense':
                        ####FUCKING AA GUNS
                        #### for each ship's count of AA guns, add them to an array. When it's time to print, take the highest number of AA guns and make that the number of AA guns in the sheet

                        ship0AA = ''
                        ship0AACount = ''
                        ship0AADps = ''
                        ship0AArange = ''
                        ship1AA = ''
                        ship1AACount = ''
                        ship1AADps = ''
                        ship1AArange = ''
                        ship2AA = ''
                        ship2AACount = ''
                        ship2AADps = ''
                        ship2AArange = ''
                        ship3AA = ''
                        ship3AACount = ''
                        ship3AADps = ''
                        ship3AArange = ''
                        ship4AA = ''
                        ship4AACount = ''
                        ship4AADps = ''
                        ship4AArange = ''

                        class AAgun:
                            gun = ""
                            guncount = ""
                            gunrange = ""
                            gundps = ""

                            def __init__(self,gunname,count,grange,dps):
                                self.gun = gunname
                                self.guncount = count
                                self.gunrange = grange
                                self.gundps = dps

                            def calculatedps(self):
                                turrets = self.guncount.split('x')[0]
                                barrelsper = self.guncount.split('x')[1]
                                totaldps = float(turrets)*float(barrelsper)*float(self.gundps)
                                return totaldps
                            def print (self):
                                print("this is my fun gun")
                                print(self.gun)
                                print(self.guncount)
                                print(self.gunrange)
                                print(self.gundps)

                        shipAAguns = []
                        statcount = range(0,len(stats),1)
                        thegun = ""
                        thecount = ""
                        therange = ""
                        thedps = ""
                        for i in statcount:
                            # print(i)
                            if i%3 == 0:
                                # print(str(i)+"%3=0")
                                # print("turn this into the gun name and count")
                                thegun = stats[0].find('span',class_='t-performance_left').text
                                thecount = stats[0].find('span',class_='t-performance_right').text.replace("pcs.","").replace(' ','')
                            if i%3 ==1:
                                # print(str(i)+"%3=1")
                                # print("this is going to be the dps")
                                thedps = stats[1].find('span',class_='t-performance_right').text
                            if i%3 == 2:
                                # print(str(i)+"%3=2")
                                # print("this is going to be the range")
                                therange = stats[2].find('span',class_='t-performance_right').text
                                newgun = AAgun(thegun,thecount,thedps,therange)
                                shipAAguns.append(newgun)
                                thegun = ""
                                thecount = ""
                                thedps = ""
                                therange = ""
                        # for gun in shipAAguns:
                            # gun.print()

                        # for stat in stats:
                        #     print(stat)
                        #     # if line doesn't start with '...'

                    ##TODO - multiple AA Defense

                    ### Maneuverability

                    elif groupName == 'Maneuverability':
                        shipSpeed = ''
                        shipTurnRadius = ''
                        shipRudderShift = ''

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Maximum Speed':
                                shipSpeed = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Turning Circle Radius':
                                shipTurnRadius = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Rudder Shift Time':
                                shipRudderShift = stat.find('span',class_='t-performance_right').text

                    ### Concealment

                    elif groupName == 'Concealment':
                        shipSurfaceDetect = ''
                        shipAirDetect = ''

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Surface Detectability Range':
                                shipSurfaceDetect = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Air Detectability Range':
                                shipAirDetect = stat.find('span',class_='t-performance_right').text

                shipBattleLevels = ""
                for shipBattleLevel in thisShipSoup.find('span',class_='b-battles-levels_interval'):
                    if shipBattleLevels == "":
                        shipBattleLevels = shipBattleLevel.text
                    else:
                        shipBattleLevels = shipBattleLevels + "," + shipBattleLevel.text
                print("levels:"+shipBattleLevels)
                for level in shipBattleLevels.split(','):
                    # print('battle level '+level)
                    thesheet = shipsheet['BattleLevel '+level]
                    thisrow = thesheet.max_row+1
                    # print('next free row is '+str(thisrow))
                    for shipvalue in shiparray:

                        # print('shipvalue we are writing is '+shipvalue)

                        for i in range(1,thesheet.max_column+1):
                            if thesheet.cell(row=1,column=i).value == shipvalue:
                                # print('the column for this value is '+str(i))
                                # print('saving ')
                                thesheet.cell(row=thisrow,column=i).value = shiparray[shipvalue]

                # input("Press a key to continue...")
    shipsheet.save(shipfilename)


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import time as tm
import math
import sys
import pickle as pkl
import matplotlib.pyplot as plt
from numpy import linalg as LA
from MMNet_base import *
from HyperNetwork_base import *

class MMNet(nn.Module):

    def __init__(self, num_layers, NT, NR, constellation, device='cuda'):
        super(MMNet, self).__init__()
        self.num_layers = num_layers
        self.device = device
        self.MMNetbase = MMNet_base(NT, NR, constellation, self.device)
        self.NT = NT
        self.NR = NR
        self.HyperNet = HyperNetwork(self.NT,self.NR, device)

    def forwardHyperNet(self, H):
        batch_size = H.shape[0]

        Theta = self.HyperNet.forward(H)
        Theta = torch.reshape(Theta, (batch_size, self.num_layers,  2 * self.NT + (2 * self.NT ) * (2 * self.NR)) )
        W_theta = Theta[:,:,:-(2 * self.NT)]
        theta_vec = Theta[:,:,-(2 * self.NT):]

        return W_theta, theta_vec

    def forward(self, H, y, noise_sigma):
        batch_size = H.shape[0]
        x_size = H.shape[-1]

        x_prev = torch.zeros(batch_size, x_size, dtype=torch.float64).to(device=self.device)
        x_list=[x_prev]
        Theta = self.HyperNet.forward(H)
        Theta = torch.reshape(Theta, (batch_size, self.num_layers,  2 * self.NT + (2 * self.NT ) * (2 * self.NR)) )
        W_theta = Theta[:,:,:-(2 * self.NT)]
        theta_vec = Theta[:,:,-(2 * self.NT):]

        for index in range(self.num_layers):
            xout = self.MMNetbase.forward(H, W_theta[:,index,:], theta_vec[:,index,:], y, x_list[-1].float(), noise_sigma, batch_size)
            x_list.append(xout.double())

        return (x_list[1:])


def quick_sort_ascending(data):
    quick_sort(data, 0, len(data) - 1)

def quick_sort(data, first_index, last_index):

    if first_index < last_index:
        pivot_index = get_pivot_index_and_partition(data, first_index, last_index)
        quick_sort(data, first_index, pivot_index - 1)
        quick_sort(data, pivot_index + 1, last_index)

def get_pivot_index_and_partition(data, first_index, last_index):
    pivot_value = data[first_index]
    lower_index = first_index + 1
    upper_index = last_index
    done = False

    while not done:

        while lower_index <= upper_index and data[lower_index] <= pivot_value:
            lower_index += 1

        while upper_index >= lower_index and data[upper_index] >= pivot_value:
            upper_index -= 1

        if upper_index < lower_index:
            done = True
        else:
            swap(data, upper_index, lower_index)

    swap(data, first_index, upper_index)
    return upper_index

def swap(data, index_a, index_b):
    temp = data[index_a]
    data[index_a] = data[index_b]
    data[index_b] = temp

def main():
    data = [14, 56, 89, 34, 2, 35, 90, 4, 59]
    print(data)
    quick_sort_ascending(data)
    print(data)

if __name__ == '__main__':
    main()


#!/usr/bin/env python
# Copyright 2014 The LUCI Authors. All rights reserved.
# Use of this source code is governed under the Apache License, Version 2.0
# that can be found in the LICENSE file.

import sys
import unittest

from test_support import test_env
test_env.setup_test_env()

from google.appengine.ext import ndb

from components.datastore_utils import mapping
from test_support import test_case

class EntityX(ndb.Model):
  a = ndb.IntegerProperty()

def int_ceil_div(value, divisor):
  """Returns the ceil() value of a integer based division."""
  return (value + divisor - 1) / divisor

class MappingTest(test_case.TestCase):
  def test_pop_future(self):
    items = [ndb.Future() for _ in xrange(5)]
    items[1].set_result(None)
    items[3].set_result('foo')
    inputs = items[:]
    mapping.pop_future_done(inputs)
    self.assertEqual([items[0], items[2], items[4]], inputs)

  def test_page_queries(self):
    for i in range(40):
      EntityX(id=i, a=i%4).put()
    queries = [
      EntityX.query(),
      EntityX.query(EntityX.a == 1),
      EntityX.query(EntityX.a == 2),
    ]
    actual = list(mapping.page_queries(queries))

    # The order won't be deterministic. The only important this is that exactly
    # all the items are returned as chunks.
    expected = [
      [EntityX(id=i, a=1) for i in xrange(1, 40, 4)],
      [EntityX(id=i, a=2) for i in xrange(2, 42, 4)],
      [EntityX(id=i, a=i%4) for i in xrange(1, 21)],
      [EntityX(id=i, a=i%4) for i in xrange(21, 40)],
    ]
    self.assertEqual(len(expected), len(actual))
    for line in actual:
      # Items may be returned out of order.
      try:
        i = expected.index(line)
      except ValueError:
        self.fail('%s not found in %s' % (line, actual))
      self.assertEqual(expected.pop(i), line)

  def test_incremental_map(self):
    for i in range(40):
      EntityX(id=i, a=i%4).put()
    queries = [
      EntityX.query(),
      EntityX.query(EntityX.a == 1),
      EntityX.query(EntityX.a == 2),
    ]
    actual = []
    # Use as much default arguments as possible.
    mapping.incremental_map(queries, actual.append)

    # The order won't be deterministic. The only important this is that exactly
    # all the items are returned as chunks and there is 3 chunks.
    expected = sorted(
        [EntityX(id=i, a=1) for i in xrange(1, 40, 4)] +
        [EntityX(id=i, a=2) for i in xrange(2, 42, 4)] +
        [EntityX(id=i, a=i%4) for i in xrange(1, 21)] +
        [EntityX(id=i, a=i%4) for i in xrange(21, 40)],
        key=lambda x: (x.key.id, x.to_dict()))
    map_page_size = 20
    self.assertEqual(int_ceil_div(len(expected), map_page_size), len(actual))
    actual = sorted(sum(actual, []), key=lambda x: (x.key.id, x.to_dict()))
    self.assertEqual(expected, actual)

  def test_incremental_map_throttling(self):
    for i in range(40):
      EntityX(id=i, a=i%4).put()
    queries = [
      EntityX.query(),
      EntityX.query(EntityX.a == 1),
      EntityX.query(EntityX.a == 2),
    ]
    actual = []
    def map_fn(items):
      actual.extend(items)
      # Note that it is returning more Future than what is called. It's fine.
      for _ in xrange(len(items) * 5):
        n = ndb.Future('yo dawg')
        # TODO(maruel): It'd be nice to not set them completed right away to
        # have better code coverage but I'm not sure how to do this.
        n.set_result('yo')
        yield n

    def filter_fn(item):
      return item.a == 2

    mapping.incremental_map(
        queries=queries,
        map_fn=map_fn,
        filter_fn=filter_fn,
        max_inflight=1,
        map_page_size=2,
        fetch_page_size=3)

    # The order won't be deterministic so sort it.
    expected = sorted(
        [EntityX(id=i, a=2) for i in xrange(2, 42, 4)] * 2,
        key=lambda x: (x.key.id, x.to_dict()))
    actual.sort(key=lambda x: (x.key.id, x.to_dict()))
    self.assertEqual(expected, actual)

if __name__ == '__main__':
  if '-v' in sys.argv:
    unittest.TestCase.maxDiff = None
  unittest.main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'Francis'

from globals.functions import printLn

imgUrl = '/MyData/Python/test/asset/imgs/test.jpg'

printLn('使用Image.thumbnail缩小一张图片')
from PIL import Image

# 打开一个图片文件
im = Image.open('/MyData/Python/test/asset/imgs/test.jpg')
w, h = im.size
print('Original image size: %sx%s' % (w, h))
# 缩放到50%
im.thumbnail((w // 2, h // 2))
print('Resize image to: %sx%s' % (w // 2, h // 2))
# 把缩放后的图像用jped格式保存
im.save('/MyData/Python/test/asset/imgs/thumbtest.jpg', 'jpeg')

printLn('使用ImageFilter模糊一张图片')
from PIL import Image, ImageFilter

# 打开一个图片
img = Image.open(imgUrl)
# 应用模糊路径
img2 = im.filter(ImageFilter.BLUR)
img2.save('/MyData/Python/test/asset/imgs/blurtest.jpg', 'jpeg')

printLn('使用ImageDraw生成一张验证码图片')
from PIL import Image, ImageDraw, ImageFont, ImageFilter

import random

# 随机字母
def rndChar():
    return chr(random.randint(65, 90))

# 随机颜色
def rndColor():
    return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))

# 随机颜色2
def rndColor2():
    return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))

# 图片大小
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255))
# 创建Font对象
font = ImageFont.truetype('Arial.ttf', 36)
# 创建Draw对象
draw = ImageDraw.Draw(image)
# 填充每个像素
for x in range(width):
    for y in range(height):
        draw.point((x, y), fill=rndColor())

# 输出文字
for t in range(4):
    draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())

# 模糊
image = image.filter(ImageFilter.BLUR)
image.save('/MyData/Python/test/asset/imgs/code.jpg', 'jpeg')


from chainer import cuda
import numpy as np
from dqn import environment
from dqn.agent import create_agent
import argparse

def run():
    parser = argparse.ArgumentParser()
    parser.add_argument('--comment', '-c', default='', type=str, help='comment to distinguish output')
    parser.add_argument('--gpu', '-g', default= -1, type=int, help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--net_type', '-n', type=str, default="full_connect", help='network type')
    args = parser.parse_args()

    comment = args.comment
    gpu = args.gpu
    log_path = './result/{}/log/test.log'.format(comment)

    if gpu >= 0:
    	cuda.get_device(gpu).use()

    actions = ["up", "down", "right", "left"]
    n_actions = len(actions)
    agent = create_agent(n_actions, gpu, exp_policy='const0.0', decay_steps=1,
    							q_update_freq=1, fixed_q_update_freq=1,
    							memory_size=100, net_type=args.net_type, discount=0)
    agent.load('./result/{}/network/agent_final/'.format(comment))
    env = environment.Environment(1000, comment, test=True)

    with open(log_path, 'w') as f:
        pass

    episode_pics = []
    for i in range(10000):
        obs = env.reset()
        done = False
        R = 0
        t = 0
        while not done and t < 10:
            action = agent.act(obs)
            #action_value = agent.model(agent.batch_states([obs], agent.xp, agent.phi))
            #print(action_value)
            obs, r, done, _ = env.step(action)
            R += r
            t += 1
            if r == 1:
                episode_pics.append(env.episode_pics)

        with open(log_path, 'a') as f:
            f.write('test episode: {}, R: {}, step: {}\n'.format(i, R, t))
        print('test episode: {}, R: {}, step: {}'.format(i, R, t))

        agent.stop_episode()
    np.save('./result/{}/episode_pics.npy'.format(comment), np.array(episode_pics))

    #c = 0
    #for i, obs in enumerate(env.pics[9]):
    #    action_value = agent.model(agent.batch_states([obs], agent.xp, agent.phi))
    #    print(i, actions[cuda.to_cpu(action_value.greedy_actions.data)[0]], action_value)
    #    if actions[cuda.to_cpu(action_value.greedy_actions.data)[0]] == 'up':
    #        c += 1
    #print(c)

if __name__ == '__main__':
    run()


from django.shortcuts import render

# Create your views here.
from rest_framework import viewsets, mixins, status
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response

from notice.models import MainPageDodExplanation, FAQLink, NoticeLink, SuggestionLink, PrivacyPolicyLink, \
    TermsOfServiceLink, ContactLink
from notice.serializers import DodExplanationSerializer, ThirdPartyMenuListSerializer, FAQMenuSerializer, \
    NoticeMenuSerializer, SuggestionMenuSerializer, ContactMenuSerializer

class DodExplanationAPIView(viewsets.GenericViewSet,
                            mixins.ListModelMixin,):
    permission_classes = [AllowAny]
    queryset = MainPageDodExplanation.objects.filter(is_active=True).order_by('order')
    serializer_class = DodExplanationSerializer
    pagination_class = None
    """
    대시보드 없는 메인페이지의 dod 설명 api 입니다.
    """
    def list(self, request, *args, **kwargs):
        """
        api : api/v1/dod-explanation/
        """

        return super(DodExplanationAPIView, self).list(request, args, kwargs)

class ThirdPartyMenuListAPIView(viewsets.GenericViewSet,
                                mixins.ListModelMixin):
    queryset = None
    serializer_class = None
    permission_classes = [IsAuthenticated]

    def list(self, request, *args, **kwargs):
        faq = None if not FAQLink.objects.filter(is_active=True).exists()\
            else FAQLink.objects.filter(is_active=True).last()

        notice = None if not NoticeLink.objects.filter(is_active=True).exists()\
            else NoticeLink.objects.filter(is_active=True).last()

        suggestion = None if not SuggestionLink.objects.filter(is_active=True).exists()\
            else SuggestionLink.objects.filter(is_active=True).last()

        contact = None if not ContactLink.objects.filter(is_active=True).exists() \
            else ContactLink.objects.filter(is_active=True).last()

        menu_list = [faq, contact, suggestion, notice]
        menu_data = []
        for menu in menu_list:
            if menu:
                menu_data.append(self._meta_serializer(menu))

        return Response(menu_data, status=status.HTTP_200_OK)

    def _meta_serializer(self, obj):
        val = {'icon_src': obj.icon.url,
               'link': obj.link}
        obj_name = obj._meta.model_name
        if obj_name == 'faqlink':
            title = '자주 묻는 질문'
        elif obj_name == 'contactlink':
            title = '문의·상담하기'
        elif obj_name == 'suggestionlink':
            title = '건의하기'
        elif obj_name == 'noticelink':
            title = '공지사항'
        else:
            title = None
        val['title'] = title
        return val


#Задача №6 Вариант 13
#Создайте игру, в которой компьютер загадывает название одного из двух спутников Марса, а игрок должен его угадать.
#Martynov M.I.
#21.04.2016

import random
sputniki=("Деймос","Фобос")
sputn=random.randint(0,1)
rand=sputniki[sputn]
print("Я загадал название одного из спутников марса")
#print(rand)
otvet=0
while(otvet)!=(rand):
    otvet=input("Введите назмание спутника:")
    if(otvet)!=(rand):
        print("Вы не угадали,попробуйте снова.")
    elif(otvet)==(rand):
        print("Вы угадали.")
        break
input("Нажмите Enter для выхода")


import csv
import pr

def load_file(filename):
  reader = csv.reader(open(filename))

  consts = []

  i = 1
  region = None
  currentparties = []
  onregion = False

  for row in reader:
    if onregion == True:
      i = 0

    if row == ['','','','']:
      i = 0
      consts[len(consts)-1].set_results(currentparties)
      currentparties = []

    if i == 1:
      if row[1] != "Region":
        consts.append(pr.constituency(row[1],[]))
      else:
        if row[1] == "Region":
          region = pr.region(row[0], [], consts, 7)
          onregion = True
    if row[2] != '':
      currentparties.append(pr.party(row[2], int(row[3]), None))

    i += 1
#for i in currentparties:
#  print i.name

  region.set_results(currentparties)
  return region
#print "Region start\n"
#for j in region.get_elected():
#  print j.name
#print "Region End\n"

#print "Region start\n"
#for j in region.get_elected():
#  print j.name
#print "Region End\n"

#for i in consts:
#  print i.get_elected().name

#for i in region.constituencies:
#  print i.name + " - " + i.get_elected().name

#print "6% Swing SNP to Labour"
#region.constituency_uniform_swing(10, "Labour", "SNP")
#region.uniform_swing(1, "Labour", "SNP")
#for i in region.constituencies:
#  print i.name + " - " + i.get_elected().name

#for j in region.get_elected(7):
#  print j.name
#x = raw_input()

def load_index(filename):
  reader = csv.reader(open(filename))
  regions = []
  for row in reader:
    regions.append(load_file(row[2]))
  return regions


from zion.gateways.docker.protocol import Protocol
from zion.gateways.docker.function import Function
from zion.gateways.docker.worker import Worker
import time

class DockerGateway():

    def __init__(self, be):
        self.be = be
        self.req = be.req
        self.conf = be.conf
        self.logger = be.logger
        self.account = be.account
        self.redis = be.redis
        self.method = self.req.method.lower()
        self.scope = self.account[5:18]
        self.functions_container = self.conf["functions_container"]
        self.execution_server = self.conf["execution_server"]

    def _get_object_stream(self):
        if self.method == 'get':
            return self.be.response.app_iter
        if self.method == 'put':
            return self.req.environ['wsgi.input']

    def _get_object_metadata(self):
        headers = dict()
        if self.method == "get":
            headers = self.be.response.headers
        elif self.method == "put":
            if 'Content-Length' in self.req.headers:
                headers['Content-Length'] = self.req.headers['Content-Length']
            if 'Content-Type' in self.req.headers:
                headers['Content-Type'] = self.req.headers['Content-Type']
            for header in self.req.headers:
                if header.startswith('X-Object'):
                    headers[header] = self.req.headers[header]

        return headers

    def execute_function(self, function_info):
        """
        Executes the function.

        :param function_info: function information
        :returns: response from the function
        """
        object_stream = self._get_object_stream()
        object_metadata = self._get_object_metadata()
        request_headers = dict(self.req.headers)
        f_name = function_info.keys()[0]
        if function_info[f_name]:
            function_parameters = eval(function_info[f_name])
        else:
            function_parameters = dict()

        time1 = time.time()
        function = Function(self.be, self.scope, f_name)
        time2 = time.time()
        fc = time2-time1
        # print '------ FUNCTION took %0.6f s' % ((time2-time1))

        time1 = time.time()
        worker = Worker(self.be, self.scope, self.redis, function)
        time2 = time.time()
        wkr = time2-time1
        # print '------ WORKER took %0.6f s' % ((time2-time1))

        time1 = time.time()
        protocol = Protocol(worker, object_stream, object_metadata,
                            request_headers, function_parameters, self.be)
        resp = protocol.comunicate()
        time2 = time.time()
        ptc = time2-time1
        # print '----- PROTOCOL took %0.6f s' % ((time2-time1))

        total = fc + wkr + ptc

        fl = open("/tmp/zion.times", "a")
        fl.write("%0.6f\t%0.6f\t%0.6f : \t%0.6f\n" % ((fc, wkr, ptc, total)))
        fl.close()

        # return {"command": "RC"}
        return resp


# l = [i for i in range(0, 10, 2)]

# print(", ".join(map(str, l)))

# for idx, num in enumerate(l):
#     print(idx, num)

def f(l):
    l[0] = 4
    return

l = [1, 2, 3, 4, 5]
f(l)
print(l)


import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import time
import sys
from easydict import EasyDict

import numpy as np
import tensorflow as tf
import tensorflow_addons as tfa

import rnn_model
import attention_model
import dataset
import dataset_prepare
import utils
import params_setting
from tqdm import tqdm

tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

class my_scheduler(tf.keras.optimizers.schedules.LearningRateSchedule):
  def __init__(self, init_lr, decay_epochs, train_epoch_size, decay_rate=0.01, min_lr=8e-7):
    super(my_scheduler, self).__init__()
    self.decay_epochs = decay_epochs
    self.train_size = train_epoch_size
    self.decay_rate = decay_rate
    self.cur_lr = tf.cast(init_lr, tf.float32)
    self.min_lr = min_lr

  @tf.function
  def __call__(self, step):
    epoch = step // self.train_size
    if epoch < self.decay_epochs:
      return self.cur_lr
    else:
      return tf.maximum(self.cur_lr * tf.math.exp(self.decay_rate * (self.decay_epochs - epoch)), self.min_lr)

def train_val(params):
  '''
  TODO: Adding corriculum learning for seq_len (starting at 200, 400, 800)
  :param params:
  :return:
  '''
  if params.train_multiwalk_head:
    utils.next_iter_to_keep = params.cycle_opt_prms.step_size  # 2000
  else:
    utils.next_iter_to_keep = params.cycle_opt_prms.step_size  # 10000
  print(utils.color.BOLD + utils.color.RED + 'params.logdir :::: ', params.logdir, utils.color.END)
  print(utils.color.BOLD + utils.color.RED, os.getpid(), utils.color.END)
  utils.backup_python_files_and_params(params)
  if not os.path.exists(os.path.join(params.logdir, "results")):
    os.makedirs(os.path.join(params.logdir, "results"))
  # Set up datasets for training and for test
  # -----------------------------------------

  def get_train_set(params):
    train_datasets = []
    train_ds_iters = []
    for i in range(len(params.datasets2use['train'])):
      this_train_dataset, n_trn_items = dataset.tf_mesh_dataset(params, params.datasets2use['train'][i],
                                                                mode=params.network_tasks[i], size_limit=params.train_dataset_size_limit,
                                                                shuffle_size=100, min_max_faces2use=params.train_min_max_faces2use,
                                                                max_size_per_class=params.train_max_size_per_class, min_dataset_size=64*32,
                                                                data_augmentation=params.train_data_augmentation)
      print('Train Dataset size:', n_trn_items)
      train_ds_iters.append(iter(this_train_dataset.repeat()))
      train_datasets.append(this_train_dataset)
    train_epoch_size = max(8, int(n_trn_items / params.n_walks_per_model / params.batch_size))
    print('train_epoch_size:', train_epoch_size)
    return train_datasets, train_epoch_size, train_ds_iters

  def get_test_set(params):
    test_dataset, n_tst_items = dataset.tf_mesh_dataset(params, params.datasets2use['test'][0],
                                                        mode=params.network_tasks[0],
                                                        size_limit=params.test_dataset_size_limit,
                                                        shuffle_size=100,
                                                        min_max_faces2use=params.test_min_max_faces2use,
                                                        )
    print(' Test Dataset size:', n_tst_items)
    return test_dataset, n_tst_items

  train_datasets, train_epoch_size, train_ds_iters = get_train_set(params)

  if params.datasets2use['test'] is None:
    test_dataset = None
    n_tst_items = 0
  else:
    test_dataset, n_tst_items = get_test_set(params)

  # Set up RNN model and optimizer
  # ------------------------------
  if params.net_start_from_prev_net is not None:
    init_net_using = params.net_start_from_prev_net
  else:
    init_net_using = None

  if params.optimizer_type == 'adam':
    lr_schedule = my_scheduler(init_lr=params.learning_rate[0], decay_epochs=20, train_epoch_size=train_epoch_size*20)
    optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule, clipnorm=params.gradient_clip_th)
  elif params.optimizer_type == 'cycle':
    @tf.function
    def _scale_fn(x):
      x_th = 400e3 / params.cycle_opt_prms.step_size
      if x < x_th:
        return 1.0
      else:
        return 0.5
    lr_schedule = tfa.optimizers.CyclicalLearningRate(initial_learning_rate=params.cycle_opt_prms.initial_learning_rate,
                                                      maximal_learning_rate=params.cycle_opt_prms.maximal_learning_rate,
                                                      step_size=params.cycle_opt_prms.step_size,
                                                      scale_fn=_scale_fn, scale_mode="cycle", name="MyCyclicScheduler")
    optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule, clipnorm=params.gradient_clip_th)
  elif params.optimizer_type == 'sgd':
    optimizer = tf.keras.optimdizers.SGD(lr=params.learning_rate[0], decay=0, momentum=0.9, nesterov=True, clipnorm=params.gradient_clip_th)
  else:
    raise Exception('optimizer_type not supported: ' + params.optimizer_type)

  if params.net == 'RnnWalkNet':
    dnn_model = rnn_model.RnnWalkNet(params, params.n_classes, params.net_input_dim, init_net_using, optimizer=optimizer)
  elif params.net == 'RnnAttention':
    dnn_model = rnn_model.AttentionWalkNet(params, params.n_classes, params.net_input_dim, init_net_using, optimizer=optimizer)
  elif params.net == 'HierTransformer':
    dnn_model = attention_model.WalkHierTransformer(**params.net_params, params=params, optimizer=optimizer,
                                                    model_fn=init_net_using)
  # Other initializations
  # ---------------------
  time_msrs = {}
  time_msrs_names = ['train_step', 'get_train_data', 'test']
  for name in time_msrs_names:
    time_msrs[name] = 0
  seg_train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='seg_train_accuracy')

  train_log_names = ['seg_loss', 'triplet_center_loss']
  train_logs = {name: tf.keras.metrics.Mean(name=name) for name in train_log_names}
  train_logs['seg_train_accuracy'] = seg_train_accuracy

  # Train / test functions
  # ----------------------
  if params.last_layer_actication is None:
    seg_loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
  else:
    seg_loss = tf.keras.losses.SparseCategoricalCrossentropy()

  if params.train_multiwalk_head:
    train_vars = dnn_model.trainable_variables[-6:]
    # train_vars = dnn_model.trainable_variables
  else:
    train_vars = dnn_model.trainable_variables

  if params.triplet_loss:
    triplet_center_loss = rnn_model.TCL(params.n_classes, params.triplet_dim)
    if params.centers_weights:
      dummy_input = tf.ones((4, params.triplet_dim))
      _ = triplet_center_loss(dummy_input, tf.constant([1, 1, 2, 2], dtype=tf.int32))
      triplet_center_loss.load_weights(params.centers_weights)
    train_vars += triplet_center_loss.trainable_variables
    len_triplet_vars = len(triplet_center_loss.trainable_variables)
    from evaluation import calculate_map_auc
    train_logs['mAP'] = tf.keras.metrics.Mean(name='mAP')
    train_logs['AUC'] = tf.keras.metrics.Mean(name='AUC')
    train_log_names += ['mAP', 'AUC']

  @tf.function
  def train_step(model_ftrs_, labels_, one_label_per_model):
    sp = model_ftrs_.shape
    model_ftrs = tf.reshape(model_ftrs_, (-1, sp[-2], sp[-1]))
    with tf.GradientTape() as tape:
      if one_label_per_model:
        if params.cross_walk_attn:
          labels = tf.reshape(tf.transpose(tf.stack((labels_,) * 1)), (-1,))
        else:
          labels = tf.reshape(tf.transpose(tf.stack((labels_,)*params.n_walks_per_model)),(-1,))
        predictions, ftrs = dnn_model(model_ftrs, classify='both')
        if params.triplet_loss:
          triplet_loss = triplet_center_loss(ftrs, labels, margin=5.0)
      else:
        labels = tf.reshape(labels_, (-1, sp[-2]))
        skip = params.min_seq_len if 'RnnWalkNet' in params.net else 0
        predictions = dnn_model(model_ftrs)[:, skip:]
        labels = labels[:, skip:]
      if hasattr(params, 'ignore_label'):
        labels = tf.reshape(labels, [-1])
        predictions = tf.reshape(predictions, [-1, params.n_classes])
        indices = tf.squeeze(tf.where(tf.not_equal(labels, params.ignore_label)), 1)
        labels = tf.cast(tf.gather(labels, indices), tf.int32)
        predictions = tf.gather(predictions, indices)

      seg_train_accuracy(labels, predictions)
      if hasattr(params, 'class_weights'):
        beta = 0.9
        effective_num = 1.0 - np.power(beta, [x / min(params.class_weights) for x in np.asarray(params.class_weights)])
        weights = (1.0-beta)/ np.array(effective_num)
        weights = weights / np.sum(weights) * int(params.n_classes)
      else:
        weights = tf.ones((params.n_classes))
      weights = tf.gather(weights, labels)
      loss = seg_loss(labels, predictions, weights)
      loss += tf.reduce_sum(dnn_model.losses)
      if params.triplet_loss and not tf.math.is_nan(triplet_loss):
        loss += 0.1 * triplet_loss
        train_logs['triplet_center_loss'](triplet_loss)
    gradients = tape.gradient(loss, train_vars)
    # if params.triplet_loss:
    #   gradients[-len_triplet_vars] = [100 * x for x in gradients[-len_triplet_vars]] # according to TCL, centers learning rate is ~ x 100
    # if params.train_multiwalk_head:
    #   gradients[-6:] = gradients[-6:] * 10
    optimizer.apply_gradients(zip(gradients, train_vars))

    train_logs['seg_loss'](loss)

    return loss

  test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
  @tf.function
  def test_step(model_ftrs_, labels_, one_label_per_model):
    sp = model_ftrs_.shape
    model_ftrs = tf.reshape(model_ftrs_, (-1, sp[-2], sp[-1]))
    if one_label_per_model:
      if params.cross_walk_attn:
        labels = tf.reshape(tf.transpose(tf.stack((labels_,) * 1)), (-1,))
      else:
        labels = tf.reshape(tf.transpose(tf.stack((labels_,) * params.n_walks_per_model)), (-1,))
      predictions = dnn_model(model_ftrs, training=False)
    else:
      labels = tf.reshape(labels_, (-1, sp[-2]))
      skip = params.min_seq_len
      predictions = dnn_model(model_ftrs, training=False)[:, skip:]
      labels = labels[:, skip:]
    if hasattr(params, 'ignore_label'):
      labels = tf.reshape(labels, [-1])
      predictions = tf.reshape(predictions, [-1, params.n_classes])
      indices = tf.squeeze(tf.where(tf.not_equal(labels, params.ignore_label)), 1)
      labels = tf.cast(tf.gather(labels, indices), tf.int32)
      predictions = tf.gather(predictions, indices)

    best_pred = tf.math.argmax(predictions, axis=-1)
    test_accuracy(labels, predictions)
    confusion = tf.math.confusion_matrix(labels=tf.reshape(labels, (-1,)), predictions=tf.reshape(best_pred, (-1,)),
                                         num_classes=params.n_classes)
    return confusion
  # -------------------------------------

  # Loop over training EPOCHs
  # -------------------------
  one_label_per_model = params.network_task == 'classification'
  next_iter_to_log = 0
  e_time = 0
  accrcy_smoothed = tb_epoch = last_loss = None
  all_confusion = {}
  half_flag = 0
  last_quarter_flag = 0
  with tf.summary.create_file_writer(params.logdir).as_default():
    epoch = 0
    while optimizer.iterations.numpy() < params.iters_to_train + train_epoch_size * 2:
      # '''
      # Corriculum - we increase seq_len gradually as we advance in training, during 2 steps: 50%, 75%
      # '''
      # if optimizer.iterations.numpy() > int(3 * params.iters_to_train / 4) and not last_quarter_flag:
      #   last_quarter_flag = 1
      #   print('Updating seq_len to 800')
      #   params.seq_len = 800
      #   train_datasets, train_epoch_size, train_ds_iters = get_train_set(params)
      #   test_dataset, n_tst_items = get_test_set(params)
      # elif optimizer.iterations.numpy() > params.iters_to_train / 2 and not half_flag:
      #   half_flag = 1
      #   print('Updating seq_len to 400')
      #   params.seq_len = 400
      #   train_datasets, train_epoch_size = get_train_set(params)
      #   test_dataset, n_tst_items, train_ds_iters = get_test_set(params)

      epoch += 1
      str_to_print = str(os.getpid()) + ') Epoch' + str(epoch) + ', iter ' + str(optimizer.iterations.numpy())
      # if epoch == 1 and params.train_multiwalk_head:
      #   # Do single full test to see our starting point is OK
      #   old_iter2keep = utils.next_iter_to_keep
      #   dnn_model.cross_walk_attn = False
      #   utils.save_model_if_needed(optimizer.iterations, dnn_model, params, override=True)
      #   utils.next_iter_to_keep = old_iter2keep
      #   dnn_model.cross_walk_attn = True
      # Save some logs & infos
      if optimizer.iterations.numpy() >= utils.next_iter_to_keep and params.triplet_loss:
        triplet_center_loss.save_weights(params.logdir, optimizer.iterations.numpy())
      utils.save_model_if_needed(optimizer.iterations, dnn_model, params)
      if tb_epoch is not None:
        e_time = time.time() - tb_epoch
        tf.summary.scalar('time/one_epoch', e_time, step=optimizer.iterations)
        tf.summary.scalar('time/av_one_trn_itr', e_time / n_iters, step=optimizer.iterations)
        for name in time_msrs_names:
          if time_msrs[name]:  # if there is something to save
            tf.summary.scalar('time/' + name, time_msrs[name], step=optimizer.iterations)
            time_msrs[name] = 0
      tb_epoch = time.time()
      n_iters = 0
      tf.summary.scalar(name="train/learning_rate", data=optimizer._decayed_lr(tf.float32), step=optimizer.iterations)
      tf.summary.scalar(name="mem/free", data=utils.check_mem_and_exit_if_full(), step=optimizer.iterations)

      # Train one EPOC
      str_to_print += '; LR: ' + str(optimizer._decayed_lr(tf.float32).numpy())
      train_logs['seg_loss'].reset_states()
      tb = time.time()
      for iter_db in tqdm(range(train_epoch_size)):
        for dataset_id in range(len(train_datasets)):
          name, model_ftrs, labels = train_ds_iters[dataset_id].next()
          dataset_type = utils.get_dataset_type_from_name(name)
          if params.learning_rate_dynamics != 'stable':
            utils.update_lerning_rate_in_optimizer(0, params.learning_rate_dynamics, optimizer, params)
          time_msrs['get_train_data'] += time.time() - tb
          n_iters += 1
          tb = time.time()
          if params.train_loss[dataset_id] == 'cros_entr':
            train_step(model_ftrs, labels, one_label_per_model=one_label_per_model)
            loss2show = 'seg_loss'
          else:
            raise Exception('Unsupported loss_type: ' + params.train_loss[dataset_id])
          time_msrs['train_step'] += time.time() - tb
          tb = time.time()
        if iter_db == train_epoch_size - 1:
          str_to_print += ', TrnLoss: ' + str(round(train_logs[loss2show].result().numpy(), 2))

      # Dump training info to tensorboard
      if optimizer.iterations >= next_iter_to_log:
        for k, v in train_logs.items():
          if v.count.numpy() > 0:
            tf.summary.scalar('train/' + k, v.result(), step=optimizer.iterations)
            v.reset_states()
        next_iter_to_log += params.log_freq

      # Run test on part of the test set
      if test_dataset is not None:
        n_test_iters = 0
        tb = time.time()
        for name, model_ftrs, labels in test_dataset:
          n_test_iters += model_ftrs.shape[0]
          if n_test_iters > params.n_models_per_test_epoch:
            break
          confusion = test_step(model_ftrs, labels, one_label_per_model=one_label_per_model)
          dataset_type = utils.get_dataset_type_from_name(name)
          if dataset_type in all_confusion.keys():
            all_confusion[dataset_type] += confusion
          else:
            all_confusion[dataset_type] = confusion
        # Dump test info to tensorboard
        if accrcy_smoothed is None:
          accrcy_smoothed = test_accuracy.result()
        accrcy_smoothed = accrcy_smoothed * .9 + test_accuracy.result() * 0.1
        tf.summary.scalar('test/accuracy_' + dataset_type, test_accuracy.result(), step=optimizer.iterations)
        tf.summary.scalar('test/accuracy_smoothed', accrcy_smoothed, step=optimizer.iterations)
        str_to_print += ', test/accuracy_' + dataset_type + ': ' + str(round(test_accuracy.result().numpy(), 2))
        test_accuracy.reset_states()
        time_msrs['test'] += time.time() - tb

      str_to_print += ', time: ' + str(round(e_time, 1))
      print(str_to_print)

  return last_loss

def run_one_job(job, job_part):
  # Classifications
  job = job.lower()
  if job == 'modelnet40_retrieval' or job == 'modelnet_retrieval':
    params = params_setting.modelnet_params(job_part, split=1)

  if job == 'modelnet40' or job == 'modelnet':
    params = params_setting.modelnet_params(job_part, short=False)

  if job == 'shrec11':
    params = params_setting.shrec11_params(job_part, mode='Multiwalk')   #'Multiwalk'
    # params.net_start_from_prev_net='/home/ran/mesh_walker/runs_compare/0207-10.12.2020..11.34__future_raw_800/ckpt-19.index'

  if job == 'cubes':
    params = params_setting.cubes_params(job_part)

  # Semantic Segmentations
  if job == 'human_seg':
    params = params_setting.human_seg_params(job_part)

  if job == 'coseg':
    params = params_setting.coseg_params(job_part)   #  job_part can be : 'aliens' or 'chairs' or 'vases'

  if job == 'future':
    params = params_setting.future_params(job_part)
  train_val(params)

def get_all_jobs():
  jobs      = [
    'shrec11', 'shrec11', 'shrec11',
    'shrec11', 'shrec11', 'shrec11',
    'coseg', 'coseg', 'coseg',
    'human_seg',
    'cubes',
    'modelnet40',
  ]
  job_parts = [
    '10-10_A', '10-10_B', '10-10_C',
    '16-04_A', '16-04_B', '16-04_C',
    'aliens', 'vases', 'chairs',
    None,
    None,
    None,
  ]

  return jobs, job_parts

if __name__ == '__main__':
  np.random.seed(0)
  utils.config_gpu()

  # job = 'modelnet'
  # job_part = 'rnn' #'Attention'   # 'Multiwalk'  #'
  #
  # job = 'future'
  # job_part = 'rnn' #'Attention'   # 'Multiwalk'  #'
  # job = 'cubes'
  # job_part = 'Multiwalk' #'Attention'   # 'Multiwalk'

  # job = 'human_seg'
  # job_part = 'Multiwalk'  # 'Attention'   # 'Multiwalk'

  # job = 'modelnet40'
  job = 'modelnet40'
  job_part = 'Multiwalk'  # 'Attention'   # 'Multiwalk'  #'rnn'

  # job = 'modelnet40_retrieval'
  # job_part = 'rnn'  # 'Attention'   # 'Multiwalk'  #'rnn'

  # job = 'shrec11'
  # job_part = '16-04_C'  # 'Attention'   # 'Multiwalk'  #'rnn'

  if len(sys.argv) > 1:
    job = sys.argv[1].lower()
  if len(sys.argv) > 2:
    job_part = sys.argv[2].lower()

  if job.lower() == 'all':
    jobs, job_parts = get_all_jobs()
    for job_, job_part in zip(jobs, job_parts):
      run_one_job(job_, job_part)
  else:
    run_one_job(job, job_part)

  #
  # # params = params_setting.modelnet_params('Attention')   #  'rnnWalk'   'Attention' 'rnnAttention'
  #
  # # params = params_setting.scannet_params('rnnWalk')
  # params = params_setting.scannet_params('Attention')
  # # import glob
  # # params.net_start_from_prev_net = glob.glob('/home/ran/mesh_walker/runs_aug_360_must/0114-10.11.2020..15.51__scannet_v2/*.keras')[-1]
  # train_val(params)


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

import datetime

from odoo import models

class HrDISA(models.AbstractModel):
    _name = 'report.hr_disa.report_hr_disa'
    _description = "disa rapport xlsx"
    _inherit = 'report.report_xlsx.abstract'

    title = [
        "NUMERO D’ORDRE",
        "NOM & PRENOMS",
        "N° C.N.P.S",
        "ANNEE DE NAISSANCE",
        "DATE D’EMBAUCHE",
        "DATE DE DEPART",
        "TYPE DE SALARIES : HORAIRES (H), JOURNALIERS (J) OU MENSUELS (M)",
        "NOMBRE D’HEURES, DE JOURS OU DE MOIS TRAVAILLES (Y COMPRIS LE CONGE ANNUEL)",
        "SALAIRES BRUTS ANNUELS NON PLAFONNES (Y COMPRIS TOUT AVANTAGE )",
        "SALAIRES ANNUELS SOUMIS A COTISATIONS AU TITRE DE L'ASSURANCE MATERNITE, "
        "DES PRESTATIONS FAMILIALES, DES ACCIDENTS DU TRAVAIL ET MALADIES PROFESSIONNELLES",
        "SALAIRES ANNUELS SOUMIS A COTISATIONS AU TITRE DU REGIME DE LA RETRAITE",
        "L’ENTREPRISE COTISE POUR LE SALARIE AU TITRE DE: 1=PF 2=AT 3=AV 4= AM",
        "Commentaires"
    ]

    cols = [
        'order','employee_name','num_cnps','date_naissance','date_embauche','date_depart','type_employee',
        'temps_travail','brut_total','brut_autre','brut_cnps','cotisation','comment'
    ]

    #i = 0

    # Formattage pour les headers
    h_format = {
        'bold': 1,
        'border': 1,
        'font_size': 10,
        'align': 'center',
        'valign': 'vcenter',
        'fg_color': 'gray',
        'text_wrap': 1
    }

    c_format = {
        'bold': 0,
        'border': 1,
        'align': 'left',
        'valign': 'vcenter',
        'fg_color': 'white'
    }

    a_format = {
        'border': 1,
        'align': 'right',
        'valign': 'vcenter',
        'num_format': '# ##0'
    }

    a_total_format = {
        'border': 1,
        'align': 'right',
        'valign': 'vcenter',
        'num_format': '# ##0',
        'fg_color': 'gray',
    }

    def formatSheet(self, sheet):
        sheet.set_column('A:A', 5)
        sheet.set_column('B:B', 50)
        sheet.set_column('C:ZZ', 15)

    def generateLines(self, sheet, lines,c_format):
        cpt = 1
        for line in lines:
            col = 0
            for i in range(len(self.cols)):
                key = self.cols[i]
                sheet.write(cpt, col, line[key], c_format)
                col += 1
            cpt += 1

    def writeHeaders(self, sheet, h_format):
        col = 0
        line = 0
        for i in range(len(self.title)):
            sheet.write(line, col, self.title[i], h_format)
            col += 1

    def generate_xlsx_report(self, workbook, data, obj):
        lines = data['lines']
        sheet = workbook.add_worksheet('LIVRE DE PAIE')
        bold = workbook.add_format({'bold': True})
        header_format = workbook.add_format(self.h_format)
        content_format = workbook.add_format(self.c_format)
        amount_format = workbook.add_format(self.a_format)
        amount_total_format = workbook.add_format(self.a_total_format)
        self.formatSheet(sheet)
        self.writeHeaders(sheet,header_format)
        self.generateLines(sheet, lines,content_format)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/11/17 21:02
# @Author : Dendi
# @contact: msexuan@163.com
# @File : Layers.py
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformer.SubLayers import MultiHeadAttention, PositionwiseFeedForward

class EncoderLayer(nn.Module):
    '''编码层'''

    def __init__(self, d_model, d_inner, n_head, d_k, d_v, dropout=0.1):
        '''

        :param d_model:     模型输入维度
        :param d_inner:     前馈层隐层维度
        :param n_head:      多头
        :param d_k:         键向量维度
        :param d_v:         值向量维度
        :param dropout:
        '''
        super(EncoderLayer, self).__init__()

        self.sef_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout)

        self.pos_ffn = PositionwiseFeedForward(d_model, d_inner, dropout=dropout)

    def forward(self, enc_input, non_pad_mask = None, slf_attn_mask= None):

        enc_output, enc_slf_attn = self.sef_attn(enc_input, enc_input, enc_input, mask=slf_attn_mask)
        # if non_pad_mask != None:
        #     enc_output *= non_pad_mask
        enc_output *= non_pad_mask
        enc_output = self.pos_ffn(enc_output)
        # if non_pad_mask != None:
        #     enc_output *= non_pad_mask
        enc_output *= non_pad_mask
        return enc_output, enc_slf_attn

class DecoderLayer(nn.Module):
    '''解码层'''

    def __init__(self, d_model, d_inner, n_head, d_k, d_v, dropout=0.1):
        super(DecoderLayer, self).__init__()
        self.slf_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout)
        self.enc_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout)
        self.pos_ffn = PositionwiseFeedForward(d_model, d_inner,dropout=dropout)

    def forward(self, dec_input, enc_output, non_pad_mask=None, slf_attn_mask=None, dec_enc_attn_mask=None):
        # dec_output shape [batch_size, len_q, n_head * d_v]
        # dec_slf_attn shape [n_head * batch_size, len_q, d_v]
        dec_output, dec_slf_attn = self.slf_attn(
            dec_input, dec_input, dec_input, mask=slf_attn_mask)
        dec_output *= non_pad_mask

        dec_output, dec_enc_attn = self.enc_attn(
            dec_output, enc_output, enc_output, mask=dec_enc_attn_mask)
        dec_output *= non_pad_mask

        dec_output = self.pos_ffn(dec_output)
        dec_output *= non_pad_mask

        return dec_output, dec_slf_attn, dec_enc_attn


import unittest
from app.file.xlsx_reader import XlsxReader
import os

script_dir = os.path.dirname(__file__)

class TestXlsxReader(unittest.TestCase):

    def test_read_valid_xlsx(self):
        reader = XlsxReader()
        file = script_dir + '/_files/short_tripadvisor_part1.xlsx'

        result = reader.read_xlsx(file)
        self.assertEqual(171, len(result))
        self.assertIn('City', result[0])

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


from google.cloud import texttospeech

class GoogleSpeech:

    def __init__(self):
        self.client = texttospeech.TextToSpeechClient()

    def say(self, text):
        print("Google Say {}".format(text))
        synthesis_input = texttospeech.types.SynthesisInput(text=text)

        voice = texttospeech.types.VoiceSelectionParams(
            name='en-US-Wavenet-C',
            language_code='en-US'
        )

        audio_config = texttospeech.types.AudioConfig(
            audio_encoding=texttospeech.enums.AudioEncoding.LINEAR16,
            pitch=-2.8,
            speaking_rate = 0.8
        )

        print("Synthesizing")
        response = self.client.synthesize_speech(synthesis_input, voice, audio_config)
        print("Done")

        with open('/tmp/love.wav', 'wb') as fd:
            fd.write(response.audio_content)
            print("Wrote to file")

    def list_voices(self):
        """Lists the available voices."""

        # Performs the list voices request
        voices = self.client.list_voices()

        for voice in voices.voices:
            # Display the voice's name. Example: tpc-vocoded
            print('Name: {}'.format(voice.name))

            # Display the supported language codes for this voice. Example: "en-US"
            for language_code in voice.language_codes:
                print('Supported language: {}'.format(language_code))

            # SSML Voice Gender values from google.cloud.texttospeech.enums
            ssml_voice_genders = ['SSML_VOICE_GENDER_UNSPECIFIED', 'MALE',
                                  'FEMALE', 'NEUTRAL']

            # Display the SSML Voice Gender
            print('SSML Voice Gender: {}'.format(
                ssml_voice_genders[voice.ssml_gender]))

            # Display the natural sample rate hertz for this voice. Example: 24000
            print('Natural Sample Rate Hertz: {}\n'.format(
                voice.natural_sample_rate_hertz))