import json
import os
import platform as os_platform
import shutil
import subprocess
import sys
import time

import wx

global compiler_logs
compiler_logs = ''

def scrollToEnd(txtCtrl):
    if os_platform.system() != 'Darwin':
        txtCtrl.SetInsertionPoint(-1)
    txtCtrl.ShowPosition(txtCtrl.GetLastPosition())
    txtCtrl.Refresh()
    txtCtrl.Update()

def runCommand(command):
    cmd_response = None

    try:
        cmd_response = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
        #cmd_response = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
    except subprocess.CalledProcessError as exc:
        cmd_response = exc.output

    if cmd_response == None:
        return ''

    return cmd_response.decode('utf-8')

def loadHals():
    # load hals list from json file, or construct it
    if (os.name == 'nt'):
        jfile = 'editor\\arduino\\examples\\Baremetal\\hals.json'
    else:
        jfile = 'editor/arduino/examples/Baremetal/hals.json'

    f = open(jfile, 'r')
    jsonStr = f.read()
    f.close()
    return(json.loads(jsonStr))

def saveHals(halObj):
    jsonStr = json.dumps(halObj)
    if (os.name == 'nt'):
        jfile = 'editor\\arduino\\examples\\Baremetal\\hals.json'
    else:
        jfile = 'editor/arduino/examples/Baremetal/hals.json'
    f = open(jfile, 'w')
    f.write(jsonStr)
    f.flush()
    f.close()

def readBoardInstalled(platform):
    hals = loadHals()
    cli_command = ''
    if os_platform.system() == 'Windows':
        cli_command = 'editor\\arduino\\bin\\arduino-cli-w32'
    elif os_platform.system() == 'Darwin':
        cli_command = 'editor/arduino/bin/arduino-cli-mac'
    else:
        cli_command = 'editor/arduino/bin/arduino-cli-l64'
    for board in hals:
            if hals[board]['platform'] == platform:
                board_details = runCommand(cli_command + ' board details -b ' + platform)
                board_details = board_details.splitlines()
                board_version = '0'
                for line in board_details:
                    if "Board version:" in line:
                        board_version = line.split('Board version:')[1]
                        board_version = ''.join(board_version.split()) #remove white spaces
                        hals[board]['version'] = board_version
                        saveHals(hals)
                        break

def readBoardsInstalled():
    hasToSave = False
    hals = loadHals()
    cli_command = ''
    if os_platform.system() == 'Windows':
        cli_command = 'editor\\arduino\\bin\\arduino-cli-w32'
    elif os_platform.system() == 'Darwin':
        cli_command = 'editor/arduino/bin/arduino-cli-mac'
    else:
        cli_command = 'editor/arduino/bin/arduino-cli-l64'
    boardInstalled = runCommand(cli_command + ' board listall')
    for board in hals:
        if board in boardInstalled:
            platform = hals[board]['platform']
            board_details = runCommand(cli_command + ' board details -b ' + platform)
            board_details = board_details.splitlines()
            board_version = '0'
            for line in board_details:
                if "Board version:" in line:
                    board_version = line.split('Board version:')[1]
                    board_version = ''.join(board_version.split()) #remove white spaces
                    hals[board]['version'] = board_version
                    hasToSave = True
                    break
        if board not in boardInstalled:
            hals[board]['version'] = '0'
            hasToSave = True

    if hasToSave:
        saveHals(hals)

def setLangArduino():
    cli_command = ''
    if os_platform.system() == 'Windows':
        cli_command = 'editor\\arduino\\bin\\arduino-cli-w32'
    elif os_platform.system() == 'Darwin':
        cli_command = 'editor/arduino/bin/arduino-cli-mac'
    else:
        cli_command = 'editor/arduino/bin/arduino-cli-l64'

    # Initialize arduino-cli config - if it hasn't been initialized yet
    runCommand(cli_command + ' config init')

    dump = runCommand(cli_command + ' config dump')
    dump = dump.splitlines()
    arduino_dir = ''
    for line in dump:
        if "data:" in line:
            #get the directory of arduino ide
            arduino_dir = line.split('data:')[1]
            arduino_dir = ''.join(arduino_dir.split()) #remove white spaces

        if "locale:" in line:
            if "en" not in line:
                #remove the line from dump variable
                dump.remove(line)
            else:
                return #already set to english

    dump.append('locale: en')
    dump = '\n'.join(dump)
    #write on the config file all the lines# Open the file in write mode
    with open(arduino_dir + '/arduino-cli.yaml', 'w') as f:
        # Write the variable to the file
        f.write(str(dump))

    #runCommand('echo ' + dump + ' > ' + arduino_dir + '/arduino-cli.yaml')

def build(st_file, platform, source_file, port, txtCtrl, update_subsystem):
    global compiler_logs
    compiler_logs = ''
    setLangArduino()

    #check if platform is installed in the arduino ide
    readBoardInstalled(platform)

    hals = loadHals()

    #Check if board is installed
    board_installed = False
    core = ''
    for board in hals:
        if hals[board]['platform'] == platform:
            core = hals[board]['core']
            if hals[board]['version'] != "0":
                board_installed = True

    #Check MatIEC compiler
    if (os.path.exists("editor/arduino/bin/iec2c") or os.path.exists("editor/arduino/bin/iec2c.exe") or os.path.exists("editor/arduino/bin/iec2c_mac")):
        # remove old files first
        if os.path.exists('editor/arduino/src/POUS.c'):
            os.remove('editor/arduino/src/POUS.c')
        if os.path.exists('editor/arduino/src/POUS.h'):
            os.remove('editor/arduino/src/POUS.h')
        if os.path.exists('editor/arduino/src/LOCATED_VARIABLES.h'):
            os.remove('editor/arduino/src/LOCATED_VARIABLES.h')
        if os.path.exists('editor/arduino/src/VARIABLES.csv'):
            os.remove('editor/arduino/src/VARIABLES.csv')
        if os.path.exists('editor/arduino/src/Config0.c'):
            os.remove('editor/arduino/src/Config0.c')
        if os.path.exists('editor/arduino/src/Config0.h'):
            os.remove('editor/arduino/src/Config0.h')
        if os.path.exists('editor/arduino/src/Res0.c'):
            os.remove('editor/arduino/src/Res0.c')
    else:
        compiler_logs += "Error: iec2c compiler not found!\n"
        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
        wx.CallAfter(scrollToEnd, txtCtrl)
        return

    #Install/Update board support
    if board_installed == False or update_subsystem == True:
        if board_installed == False:
            compiler_logs += "Support for " + platform + " is not installed on OpenPLC Editor. Please be patient and wait while " + platform + " is being installed...\n"
            wx.CallAfter(txtCtrl.SetValue, compiler_logs)
        elif update_subsystem == True:
            compiler_logs += "Updating support for " + platform + ". Please be patient and wait while " + platform + " is being installed...\n"
            wx.CallAfter(txtCtrl.SetValue, compiler_logs)

        cli_command = ''
        if os_platform.system() == 'Windows':
            cli_command = 'editor\\arduino\\bin\\arduino-cli-w32'
        elif os_platform.system() == 'Darwin':
            cli_command = 'editor/arduino/bin/arduino-cli-mac'
        else:
            cli_command = 'editor/arduino/bin/arduino-cli-l64'

        """
        ### ARDUINO-CLI CHEAT SHEET ###

        1. List installed boards:
          => arduino-cli board listall

        2. Search for a core (even if not installed yet):
          => arduino-cli core search [search text]

        3. Dump current configuration:
          => arduino-cli config dump

        4. Get additional board parameters:
          => arduino-cli board details -fqbn [board fqbn]
        """

        # Initialize arduino-cli config - if it hasn't been initialized yet
        compiler_logs += runCommand(cli_command + ' config init')
        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
        wx.CallAfter(scrollToEnd, txtCtrl)

        # Setup boards - remove 3rd party boards to re-add them later since we don't know if they're there or not
        compiler_logs += runCommand(
            cli_command + ' config remove board_manager.additional_urls \
https://arduino.esp8266.com/stable/package_esp8266com_index.json \
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json \
https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json \
https://raw.githubusercontent.com/CONTROLLINO-PLC/CONTROLLINO_Library/master/Boards/package_ControllinoHardware_index.json \
https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json \
https://facts-engineering.gitlab.io/facts-open-source/p1am/beta_file_hosting/package_productivity-P1AM_200-boardmanagermodule_index.json \
https://raw.githubusercontent.com/VEA-SRL/IRUINO_Library/main/package_vea_index.json \
"2>&1"')
        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
        wx.CallAfter(scrollToEnd, txtCtrl)

        # Setup boards - add 3rd party boards
        compiler_logs += runCommand(
            cli_command + ' config add board_manager.additional_urls \
https://arduino.esp8266.com/stable/package_esp8266com_index.json \
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json \
https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json \
https://raw.githubusercontent.com/CONTROLLINO-PLC/CONTROLLINO_Library/master/Boards/package_ControllinoHardware_index.json \
https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json \
https://facts-engineering.gitlab.io/facts-open-source/p1am/beta_file_hosting/package_productivity-P1AM_200-boardmanagermodule_index.json \
https://raw.githubusercontent.com/VEA-SRL/IRUINO_Library/main/package_vea_index.json')
        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
        wx.CallAfter(scrollToEnd, txtCtrl)

        # Update
        compiler_logs += runCommand(cli_command + ' core update-index')
        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
        wx.CallAfter(scrollToEnd, txtCtrl)
        compiler_logs += runCommand(cli_command + ' update')
        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
        wx.CallAfter(scrollToEnd, txtCtrl)

        # Install board
        compiler_logs += runCommand(cli_command + ' core install ' + core)
        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
        wx.CallAfter(scrollToEnd, txtCtrl)

        # Install all libs - required after core install/update
        compiler_logs += runCommand(cli_command + ' lib install \
WiFiNINA \
Ethernet \
Arduino_MachineControl \
Arduino_EdgeControl \
OneWire \
DallasTemperature \
P1AM \
CONTROLLINO \
"Adafruit ADS1X15" \
PubSubClient \
ArduinoJson \
arduinomqttclient \
RP2040_PWM')
        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
        wx.CallAfter(scrollToEnd, txtCtrl)

        readBoardsInstalled()

    # Generate C files
    compiler_logs += "Compiling .st file...\n"
    wx.CallAfter(txtCtrl.SetValue, compiler_logs)
    wx.CallAfter(scrollToEnd, txtCtrl)
    if (os.name == 'nt'):
        base_path = 'editor\\arduino\\src\\'
    else:
        base_path = 'editor/arduino/src/'
    f = open(base_path+'plc_prog.st', 'w')
    f.write(st_file)
    f.flush()
    f.close()

    time.sleep(0.2)  # make sure plc_prog.st was written to disk

    if os_platform.system() == 'Windows':
        compilation = subprocess.Popen(['editor\\arduino\\bin\\iec2c.exe', 'plc_prog.st'], cwd='editor\\arduino\\src',
                                       creationflags=0x08000000, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    elif os_platform.system() == 'Darwin':
        compilation = subprocess.Popen(['../bin/iec2c_mac', 'plc_prog.st'],
                                       cwd='./editor/arduino/src', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    else:
        compilation = subprocess.Popen(
            ['../bin/iec2c', 'plc_prog.st'], cwd='./editor/arduino/src', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = compilation.communicate()
    compiler_logs += stdout.decode('UTF-8')
    compiler_logs += stderr.decode('UTF-8')
    wx.CallAfter(txtCtrl.SetValue, compiler_logs)
    wx.CallAfter(scrollToEnd, txtCtrl)

    # Remove temporary plc program
    # if os.path.exists(base_path+'plc_prog.st'):
    #    os.remove(base_path+'plc_prog.st')

    # Generate glueVars.c
    if not (os.path.exists(base_path+'LOCATED_VARIABLES.h')):
        compiler_logs += "Error: Couldn't find LOCATED_VARIABLES.h. Check iec2c compiler output for more information\n"
        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
        wx.CallAfter(scrollToEnd, txtCtrl)
        return

    located_vars_file = open(base_path+'LOCATED_VARIABLES.h', 'r')
    located_vars = located_vars_file.readlines()
    glueVars = """
#include "iec_std_lib.h"

#define __LOCATED_VAR(type, name, ...) type __##name;
#include "LOCATED_VARIABLES.h"
#undef __LOCATED_VAR
#define __LOCATED_VAR(type, name, ...) type* name = &__##name;
#include "LOCATED_VARIABLES.h"
#undef __LOCATED_VAR

TIME __CURRENT_TIME;
BOOL __DEBUG;
extern unsigned long long common_ticktime__;

//OpenPLC Buffers
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__)

#define MAX_DIGITAL_INPUT          8
#define MAX_DIGITAL_OUTPUT         32
#define MAX_ANALOG_INPUT           6
#define MAX_ANALOG_OUTPUT          32

#else

#define MAX_DIGITAL_INPUT          56
#define MAX_DIGITAL_OUTPUT         56
#define MAX_ANALOG_INPUT           32
#define MAX_ANALOG_OUTPUT          32

#endif

IEC_BOOL *bool_input[MAX_DIGITAL_INPUT/8][8];
IEC_BOOL *bool_output[MAX_DIGITAL_OUTPUT/8][8];
IEC_UINT *int_input[MAX_ANALOG_INPUT];
IEC_UINT *int_output[MAX_ANALOG_OUTPUT];

void glueVars()
{
"""
    for located_var in located_vars:
        # cleanup located var line
        if ('__LOCATED_VAR(' in located_var):
            located_var = located_var.split('(')[1].split(')')[0]
            var_data = located_var.split(',')
            if (len(var_data) < 5):
                compiler_logs += 'Error processing located var line: ' + located_var + '\n'
                wx.CallAfter(txtCtrl.SetValue, compiler_logs)
                wx.CallAfter(scrollToEnd, txtCtrl)
            else:
                var_type = var_data[0]
                var_name = var_data[1]
                var_address = var_data[4]
                var_subaddress = '0'
                if (len(var_data) > 5):
                    var_subaddress = var_data[5]

                # check variable type and assign to correct buffer pointer
                if ('QX' in var_name):
                    if (int(var_address) > 6 or int(var_subaddress) > 7):
                        compiler_logs += 'Error: wrong location for var ' + var_name + '\n'
                        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
                        wx.CallAfter(scrollToEnd, txtCtrl)
                        return
                    glueVars += '    bool_output[' + var_address + \
                        '][' + var_subaddress + '] = ' + var_name + ';\n'
                elif ('IX' in var_name):
                    if (int(var_address) > 6 or int(var_subaddress) > 7):
                        compiler_logs += 'Error: wrong location for var ' + var_name + '\n'
                        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
                        wx.CallAfter(scrollToEnd, txtCtrl)
                        return
                    glueVars += '    bool_input[' + var_address + \
                        '][' + var_subaddress + '] = ' + var_name + ';\n'
                elif ('QW' in var_name):
                    if (int(var_address) > 32):
                        compiler_logs += 'Error: wrong location for var ' + var_name + '\n'
                        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
                        wx.CallAfter(scrollToEnd, txtCtrl)
                        return
                    glueVars += '    int_output[' + \
                        var_address + '] = ' + var_name + ';\n'
                elif ('IW' in var_name):
                    if (int(var_address) > 32):
                        compiler_logs += 'Error: wrong location for var ' + var_name + '\n'
                        wx.CallAfter(txtCtrl.SetValue, compiler_logs)
                        wx.CallAfter(scrollToEnd, txtCtrl)
                        return
                    glueVars += '    int_input[' + \
                        var_address + '] = ' + var_name + ';\n'
                else:
                    compiler_logs += 'Could not process location "' + \
                        var_name + '" from line: ' + located_var + '\n'
                    wx.CallAfter(txtCtrl.SetValue, compiler_logs)
                    wx.CallAfter(scrollToEnd, txtCtrl)
                    return

    glueVars += """
}

void updateTime()
{
    __CURRENT_TIME.tv_nsec += common_ticktime__;

    if (__CURRENT_TIME.tv_nsec >= 1000000000)
    {
        __CURRENT_TIME.tv_nsec -= 1000000000;
        __CURRENT_TIME.tv_sec += 1;
    }
}
    """
    f = open(base_path+'glueVars.c', 'w')
    f.write(glueVars)
    f.flush()
    f.close()

    time.sleep(2)  # make sure glueVars.c was written to disk

    # Patch POUS.c to include POUS.h
    f = open(base_path+'POUS.c', 'r')
    pous_c = '#include "POUS.h"\n\n' + f.read()
    f.close()

    f = open(base_path+'POUS.c', 'w')
    f.write(pous_c)
    f.flush()
    f.close()

    # Patch Res0.c to include POUS.h instead of POUS.c
    f = open(base_path+'Res0.c', 'r')
    res0_c = ''
    lines = f.readlines()
    for line in lines:
        if '#include "POUS.c"' in line:
            res0_c += '#include "POUS.h"\n'
        else:
            res0_c += line
    f.close()

    f = open(base_path+'Res0.c', 'w')
    f.write(res0_c)
    f.flush()
    f.close()

    # Copy HAL file
    if os_platform.system() == 'Windows':
        source_path = 'editor\\arduino\\src\\hal\\'
        destination = 'editor\\arduino\\src\\arduino.cpp'
    else:
        source_path = 'editor/arduino/src/hal/'
        destination = 'editor/arduino/src/arduino.cpp'

    shutil.copyfile(source_path + source_file, destination)

    # Generate Pin Array Sizes defines
    # We need to write the hal specific pin size defines on the global defines.h so that it is
    # available everywhere

    if os_platform.system() == 'Windows':
        define_path = 'editor\\arduino\\examples\\Baremetal\\'
    else:
        define_path = 'editor/arduino/examples/Baremetal/'
    file = open(define_path+'defines.h', 'r')
    define_file = file.read() + '\n\n//Pin Array Sizes\n'
    hal = open(destination, 'r')
    lines = hal.readlines()
    for line in lines:
        if (line.find('define NUM_DISCRETE_INPUT') > 0):
            define_file += line
        if (line.find('define NUM_ANALOG_INPUT') > 0):
            define_file += line
        if (line.find('define NUM_DISCRETE_OUTPUT') > 0):
            define_file += line
        if (line.find('define NUM_ANALOG_OUTPUT') > 0):
            define_file += line

    # Write defines.h file back to disk
    if os_platform.system() == 'Windows':
        define_path = 'editor\\arduino\\examples\\Baremetal\\'
    else:
        define_path = 'editor/arduino/examples/Baremetal/'
    f = open(define_path+'defines.h', 'w')
    f.write(define_file)
    f.flush()
    f.close()

    # Generate .elf file
    compiler_logs += "Generating binary file...\n"
    wx.CallAfter(txtCtrl.SetValue, compiler_logs)
    wx.CallAfter(scrollToEnd, txtCtrl)

    # if (os.name == 'nt'):
    #    compilation = subprocess.check_output('editor\\arduino\\bin\\arduino-cli-w32 compile -v --libraries=editor\\arduino --build-property compiler.c.extra_flags="-Ieditor\\arduino\\src\\lib" --build-property compiler.cpp.extra_flags="-Ieditor\\arduino\\src\\lib" --export-binaries -b ' + platform + ' editor\\arduino\\examples\\Baremetal\\Baremetal.ino 2>&1')
    #compilation = subprocess.Popen(['editor\\arduino\\bin\\arduino-cli-w32', 'compile', '-v', '--libraries=..\\..\\', '--build-property', 'compiler.c.extra_flags="-I..\\src\\lib"', '--build-property', 'compiler.cpp.extra_flags="I..\\src\\lib"', '--export-binaries', '-b', platform, '..\\examples\\Baremetal\\Baremetal.ino'], cwd='editor\\arduino\\src', stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    # else:
    #    compilation = subprocess.check_output('editor/arduino/bin/arduino-cli-l64 compile -v --libraries=editor/arduino --build-property compiler.c.extra_flags="-Ieditor/arduino/src/lib" --build-property compiler.cpp.extra_flags="-Ieditor/arduino/src/lib" --export-binaries -b ' + platform + ' editor/arduino/examples/Baremetal/Baremetal.ino 2>&1')
    #compiler_logs += compilation.decode('utf-8')
    #wx.CallAfter(txtCtrl.SetValue, compiler_logs)
    #wx.CallAfter(scrollToEnd, txtCtrl)

    compiler_logs += '\nCOMPILATION START: '
    compiler_logs += platform
    compiler_logs += '\n'

    if os_platform.system() == 'Windows':
        compilation = subprocess.Popen(['editor\\arduino\\bin\\arduino-cli-w32', 'compile', '-v', '--libraries=editor\\arduino', '--build-property', 'compiler.c.extra_flags="-Ieditor\\arduino\\src\\lib"', '--build-property',
                                       'compiler.cpp.extra_flags="-Ieditor\\arduino\\src\\lib"', '--export-binaries', '-b', platform, 'editor\\arduino\\examples\\Baremetal\\Baremetal.ino'], creationflags=0x08000000, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    elif os_platform.system() == 'Darwin':
        compilation = subprocess.Popen(['editor/arduino/bin/arduino-cli-mac', 'compile', '-v', '--libraries=editor/arduino', '--build-property', 'compiler.c.extra_flags="-Ieditor/arduino/src/lib"', '--build-property',
                                       'compiler.cpp.extra_flags="-Ieditor/arduino/src/lib"', '--export-binaries', '-b', platform, 'editor/arduino/examples/Baremetal/Baremetal.ino'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    else:
        compilation = subprocess.Popen(['editor/arduino/bin/arduino-cli-l64', 'compile', '-v', '--libraries=editor/arduino', '--build-property', 'compiler.c.extra_flags="-Ieditor/arduino/src/lib"', '--build-property',
                                       'compiler.cpp.extra_flags="-Ieditor/arduino/src/lib"', '--export-binaries', '-b', platform, 'editor/arduino/examples/Baremetal/Baremetal.ino'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = compilation.communicate()
    compiler_logs += stdout.decode('UTF-8')
    compiler_logs += stderr.decode('UTF-8')
    if (compilation.returncode != 0):
        compiler_logs += '\nCOMPILATION FAILED!\n'
    wx.CallAfter(txtCtrl.SetValue, compiler_logs)
    wx.CallAfter(scrollToEnd, txtCtrl)

    if (compilation.returncode == 0):
        if (port != None):
            compiler_logs += '\nUploading program to Arduino board at ' + port + '...\n'
            wx.CallAfter(txtCtrl.SetValue, compiler_logs)
            wx.CallAfter(scrollToEnd, txtCtrl)
            if os_platform.system() == 'Windows':
                compiler_logs += runCommand('editor\\arduino\\bin\\arduino-cli-w32 upload --port ' +
                                            port + ' --fqbn ' + platform + ' editor\\arduino\\examples\\Baremetal/')
            elif os_platform.system() == 'Darwin':
                compiler_logs += runCommand('editor/arduino/bin/arduino-cli-mac upload --port ' +
                                            port + ' --fqbn ' + platform + ' editor/arduino/examples/Baremetal/ 2>&1')
            else:
                compiler_logs += runCommand('editor/arduino/bin/arduino-cli-l64 upload --port ' +
                                            port + ' --fqbn ' + platform + ' editor/arduino/examples/Baremetal/')
            compiler_logs += '\nDone!\n'
            wx.CallAfter(txtCtrl.SetValue, compiler_logs)
            wx.CallAfter(scrollToEnd, txtCtrl)
        else:
            cwd = os.getcwd()
            compiler_logs += '\nOUTPUT DIRECTORY:\n'
            if os_platform.system() == 'Windows':
                compiler_logs += cwd + '\\editor\\arduino\\examples\\Baremetal\\build\n'
            else:
                compiler_logs += cwd + '/editor/arduino/examples/Baremetal/build\n'
            compiler_logs += '\nCOMPILATION DONE!'
            wx.CallAfter(txtCtrl.SetValue, compiler_logs)
            wx.CallAfter(scrollToEnd, txtCtrl)
    time.sleep(1)  # make sure files are not in use anymore

    # no clean up
    return

    # Clean up and return
    if os.path.exists(base_path+'POUS.c'):
        os.remove(base_path+'POUS.c')
    if os.path.exists(base_path+'POUS.h'):
        os.remove(base_path+'POUS.h')
    if os.path.exists(base_path+'LOCATED_VARIABLES.h'):
        os.remove(base_path+'LOCATED_VARIABLES.h')
    if os.path.exists(base_path+'VARIABLES.csv'):
        os.remove(base_path+'VARIABLES.csv')
    if os.path.exists(base_path+'Config0.c'):
        os.remove(base_path+'Config0.c')
    if os.path.exists(base_path+'Config0.h'):
        os.remove(base_path+'Config0.h')
    if os.path.exists(base_path+'Config0.o'):
        os.remove(base_path+'Config0.o')
    if os.path.exists(base_path+'Res0.c'):
        os.remove(base_path+'Res0.c')
    if os.path.exists(base_path+'Res0.o'):
        os.remove(base_path+'Res0.o')
    if os.path.exists(base_path+'glueVars.c'):
        os.remove(base_path+'glueVars.c')


import numpy as np
import matplotlib.pyplot as plt
#If using termux
import subprocess
import shlex

x = [1,2,3,4,2,1]
#Assuming length of x(n) and h(n) as same
N = len(x)
def h(N):
	h = []
	for i in range(N):
		out = 0;
		if i >= 0:
			out = out+((-0.5)**i)
		if i-2 >= 0:
			out = out+((-0.5)**(i-2))
		h.append(out)

	return h

def DFT(f):
	F = []
	N = len(f)
	for k in range(N):
		out = 0 * 1j
		for n in range(N):
			out=out+(f[n]*np.exp(-1j*2*np.pi*k*n/N))
		F.append(out)
	return F
h=h(N)
print ("DFT of x(n)\n",DFT(x))
print()
print ("DFT of h(n)\n",DFT(h))

fig , ax = plt.subplots(nrows = 3, ncols = 2, figsize=(12,14))

ax[0][0].stem(range(0,N),x)
ax[0][0].set_title(r'$x(n)$')
ax[0][0].grid()

ax[0][1].stem(range(0,N),h)
ax[0][1].set_title(r'$h(n)$')
ax[0][1].grid()

ax[1][0].stem(range(0,N),np.abs(DFT(x)))
ax[1][0].set_title(r'$|DFT(x)|$')
ax[1][0].grid()

ax[1][1].stem(range(0,N),np.angle(DFT(x)))
ax[1][1].set_title(r'$phase(DFT(x))$')
ax[1][1].grid()

ax[2][0].stem(range(0,N),np.abs(DFT(h)))
ax[2][0].set_title(r'$|DFT(h)|$')
ax[2][0].grid()

ax[2][1].stem(range(0,N),np.angle(DFT(h)))
ax[2][1].set_title(r'$phase(DFT(h))$')
ax[2][1].grid()
#If using termux
plt.savefig('../figs/ee18btech11012.pdf')
plt.savefig('../figs/ee18btech11012.eps')
subprocess.run(shlex.split("termux-open ../figs/ee18btech11012.pdf"))
#else
#plt.show()


#!/usr/bin/env python
from comm.mapper import Mapper
from comm import comm

class PCD2JPG(Mapper):
    def __init__(self):
        Mapper.__init__(self, ["pcd"],
                        [comm.PointCloudMessage],
                        "jpg", comm.MultiChannelImageMessage)
    def func(self, xyz_bgr):
        _, bgr = xyz_bgr
        return bgr

comm.initComm()
M = PCD2JPG()
M.run()


n = int(input('Кол-во друзей: '))
k = int(input('Кол-во долговых расписок: '))
money = []

for _ in range(n):
    money.append(0)

for receipt in range(k):
    print()
    print(receipt + 1, 'расписка')
    debtor = int(input('Кому: '))
    creditor = int(input('От кого: '))
    how_money = int(input('Сколько: '))
    money[debtor - 1] -= how_money
    money[creditor - 1] += how_money

print('\nБаланс друзей:')
for friend in range(n):
    print(friend + 1, ':', money[friend])


import cv2,os
from pywidgets.kivy.Img.__origin import *
from pycv2.img.utils import *
from pycv2.img.drawing.box import *
def donothing(*args,**kwrgs):pass
FILE_PATH=os.path.dirname(__file__)
CRITERIA = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
REMOVEDBACKGROUND=cv2.imread(os.path.join(FILE_PATH,"icons\\removedbackground.png"))
if REMOVEDBACKGROUND is None:
    raise "where is the image"
class __Croped_img(Img_with_scrollbar):
    def crop_Img(self,imgcv,b=None):
        if b is None:
            Img_with_scrollbar.define_image(self,imgcv)
        else:
            Img_with_scrollbar.define_image(self,imgcv[b[1]:b[1]+b[3], b[0]:b[0]+b[2]])
class DrawedImg(Img_with_scrollbar):
    def draw_box_image(self,imgcv,circles=None):
        super().define_image(draw_box_moving(imgcv,circles))
class Added_background_Img(Img_with_scrollbar):
    def define_image(self,imgcv,imported_backgrounds,mask=None):
        finalimg=imgcv.copy()
        for background,pos in imported_backgrounds:
            h,w=background["background"].shape[:2]
            _backmask=cv2.resize(background["mask"],(w,h)).astype(np.bool)
            finalimg[pos[1]:pos[1]+h,pos[0]:pos[0]+w][_backmask]=background["background"][_backmask]
        finalimg=add_back_ground(imgcv.copy(),mask,finalimg)
        super().define_image(finalimg)
class Colored_Img(DrawedImg):
    def define_image(self,imgcv,mask=None,coloerbackground=None,circles=None):
        if mask is None:mask=np.zeros(imgcv.shape[:2],"uint8")
        if coloerbackground is None:coloerbackground=np.zeros(imgcv.shape[:2],"uint8")
        self.draw_box_image(add_back_ground(imgcv.copy(),mask, coloerbackground), circles)
class Bluerd_img(DrawedImg):
    def define_image(self,imgcv,mask=None,circles=None,blured=0):
        mask=mask if not mask is None else np.ones(imgcv.shape[:2],"uint8")*255
        if blured!=0:
            self.draw_box_image(bluring(imgcv.copy(), mask, blured), circles)
        else:
            self.draw_box_image(imgcv, circles)
class Cutted_brush(DrawedImg):
    def define_image(self, imgcv,mask=None,radiusarea=2,circles=None):
        if mask is None:mask=np.zeros(self.shape[:2],"uint8")
        resultimg=cv2.inpaint(imgcv, mask, radiusarea, cv2.INPAINT_TELEA)
        self.draw_box_image(resultimg,circles)
class Removed_Background(DrawedImg):
     def define_image(self, imgcv,mask=None,circles=None):
        if mask is None:mask=np.zeros(imgcv.shape[:2],"uint8")
        background=duplicate_image(REMOVEDBACKGROUND,imgcv.shape[:2])

        self.draw_box_image(add_back_ground(imgcv.copy(),255-mask,background),circles)
class Croped_Img(__Croped_img):
    def define_image(self,imgcv,mask=None,box=None):
        if mask is None:
            coloredimg=imgcv
        else:
            coloredimg=color_back_ground(imgcv,mask,(255,255,255))
        self.crop_Img(coloredimg,box)
class Croped_mask(__Croped_img):
    def define_image(self,mask,box=None,):
        self.crop_Img(mask,box)
class Mask(DrawedImg):
    def define_image(self,mask,circles=None):
        self.draw_box_image(mask,circles)
class Image_viewer(DrawedImg):
    def define_image(self,imgcv,colorimgstate=False,mask=None,circles=None):
        if mask is None or not colorimgstate:
            colored_imgcv=imgcv
        else:
            colored_imgcv=color_back_ground(imgcv.copy(),mask,(255,255,255))
        self.draw_box_image(colored_imgcv,circles)
class ClusteringtheImage(DrawedImg):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        #for saving processor effort
        self.last_kmeans=None
        self.last_img=None
        self.last_clusterd_img=None
    def define_image(self, imgcv,mask=None,kmeans=3,circles=None):
        mask=mask if not mask is None else np.zeros(imgcv.shape,"uint8")
        if kmeans!=self.last_kmeans or not ARE_EQUALE(self.last_img,imgcv):
            self.last_kmeans=kmeans
            self.last_img=imgcv.copy()
            Z = imgcv.reshape((-1,3))
            # convert to np.float32
            Z = np.float32(Z)
            # define criteria, number of clusters(K) and apply kmeans()
            _,label,center=cv2.kmeans(Z,kmeans,None,CRITERIA,10,cv2.KMEANS_RANDOM_CENTERS)
            # Now convert back into uint8, and make original image
            center = np.uint8(center)
            res = center[label.flatten()]
            self.last_clusterd_img=res.reshape((imgcv.shape))
        _background=add_back_ground(self.last_img.copy(),mask,self.last_clusterd_img)
        self.draw_box_image(_background,circles)
class Gray_Image_viewer(DrawedImg):
    def define_image(self, imgcv,mask=None,circles=None):
        mask=mask if not mask is None else np.zeros(imgcv.shape,"uint8")
        #conver BGR 2 Gray and add 2 channels by cv2
        grayimg=cv2.cvtColor(cv2.cvtColor(imgcv,cv2.COLOR_BGR2GRAY),cv2.COLOR_GRAY2BGR)
        _background=add_back_ground(imgcv.copy(),mask,grayimg)
        self.draw_box_image(_background,circles)


import numpy as np

def unitStep(x, t):
    if x > t:
        return 1
    return 0

def perceptronLearn(inputs, outputs, w, a, th):
    epochs = 30
    numInstances = inputs.shape[0]
    numInputs = inputs.shape[1]
    for j in range(0, epochs):
        flag = False
        print("Epoch : ", j+1)
        for i in range(numInstances):
            print("Training instance : ", i+1, " Inputs : ", inputs[i])
            x = inputs[i]
            weightedSum = np.dot(w, x)
            pred = unitStep(weightedSum, th)
            print("Target:", outputs[i], "Predicted:", pred)
            error = outputs[i]-pred
            if pred == outputs[i]:
                print("Output match, weights need not be changed.")
            else:
                flag = True
                print("Outputs do not match, weights need to be updated!")
                for k in range(numInputs):
                    w[k] = round(w[k]+(a*x[k]*error), 1)
                print("Updated weights : ", w)
        if not flag:
            print("\nFinal weights : ", w)
            break
        print("")

inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
inputsNOT = np.array([[0], [1]])
outputsOR = np.array([0, 1, 1, 1])
outputsAND = np.array([0, 0, 0, 1])
outputsNOT = np.array([1, 0])

print("-----------------------------------------------")
print("OR gate")
perceptronLearn(inputs, outputsOR, [-0.2, 0.4], 0.2, 0)

print("-----------------------------------------------")
print("AND gate")
perceptronLearn(inputs, outputsAND, [0.2, 0.4], 0.5, 1)

print("-----------------------------------------------")
print("NOT gate")
perceptronLearn(inputsNOT, outputsNOT, [-0.3], 0.5, -1)


# import class file
from Mahasiswa import Mahasiswa

# deklarasi kelas crud

class Crud:
    # atribute private
    __list = []  # list of objek mahasiswa

    # konstruktor
    def __init__(self):
        self.__list = []

    # method create: membuat objek mahasiswa
    def create(self, nim, nama, prodi, fakultas):
        # instansiasi objek mahasiswa, lalu masukan objek ke dalam list
        self.__list.append(
            Mahasiswa(nim, nama, prodi, fakultas))

    # method read: menampilkan isi list
    def read(self):
        #    menampilkan isi list of objek mahasiswa
        print("\nList Mahasiswa : \n")
        i = 0
        for mhs in self.__list:
            print(str(i+1)+".| ", mhs.get_nim(), " | ", mhs.get_nama(),
                  " | ", mhs.get_prodi(), " | ", mhs.get_fakultas(), " | ")
            i += 1

    # method update: update objek yang ingin diubah
    def update(self, nim):
        # perulangan untuk mencari objek yang  akan di ubah berdasarkan nim yang diinput
        for mhs in self.__list:
            # apabila nim pada objek nya sama dengan nim inputan
            if (mhs.get_nim() == nim):
                # input isi variabel nama prodi fakultas
                print("\nMasukan data yang ingin diubah !")
                print("Nama: ")
                nama = str(input())
                print("Prodi: ")
                prodi = str(input())
                print("Fakultas: ")
                fakultas = str(input())
                # update data yang ingin diubah dengan menggunakan method setter di kelas mahasiswa
                mhs.set_nama(nama)
                mhs.set_prodi(prodi)
                mhs.set_fakultas(fakultas)
                break

    # method delete: delete objek yang ingin dihapus
    def delete(self, nim):
        # perulangan untuk mencari objek yang akan di hapus berdasarkan nim yang diinput
        for mhs in self.__list:
            # apabila nim pada objek nya sama dengan nim inputan
            if (mhs.get_nim() == nim):
                # hapus objek pada list
                self.__list.remove(mhs)
                break


# 네이버 오픈 API 이용하기

import requests

client_id = "ObR7t_ig_MksARWbtN86"
client_secret = "RH_A6LmYyr"

headers = {"X-Naver-Client-Id": client_id, "X-Naver-Client-Secret": client_secret}

start = 1
for idx in range(10):
    start_num = start + (idx * 100)

    URL = (
        "https://openapi.naver.com/v1/search/shop.json?query=키보드&display=100&start="
        + str(start_num)
    )

    print(URL)

    res = requests.get(URL, headers=headers)

    # JSON 데이터 확인
    data = res.json()
    for item in data["items"]:
        print(item["title"])


from django import forms
from .models import Ficha, Review, Assignment, FichaImage, get_default_start_date
from django.contrib.admin.widgets import AdminDateWidget, AdminTimeWidget
from django.forms import modelformset_factory
from django.core.exceptions import ValidationError

class AssignmentForm(forms.ModelForm):
    time_window_start = forms.DateField(
        widget=forms.DateInput(
            attrs={
                "type": "date",
            }
        ),
        initial=get_default_start_date,
    )
    time_window_end = forms.DateField(
        widget=forms.DateInput(
            attrs={
                "type": "date",
            }
        )
    )

    class Meta:
        model = Assignment
        fields = [
            "title",
            "description",
            "time_window_start",
            "time_window_end",
        ]

    def clean(self):
        cleaned_data = super().clean()
        time_window_start = cleaned_data.get("time_window_start")
        time_window_end = cleaned_data.get("time_window_end")

        if time_window_end < time_window_start:
            msg = "La fecha de cierre no puede ser anterior a la fecha de inicio"
            self.add_error("time_window_end", msg)
        return cleaned_data

# class CustomFileInput(forms.ClearableFileInput):
#     template_name = "components/custom_file_input.html"
#
#
# imgChoices = (
#     ("MAIN", "main"),
#     ("FOTO", "fotograma"),
#     ("COMP", "complementaria"),
#     ("REF", "referencia"),
#     ("O", "otra"),
# )
#
#
# class FichaImageForm(forms.ModelForm):
#     attributes = forms.ChoiceField(
#         choices=imgChoices, label="Tipo de imagen", widget=forms.Select()
#     )
#
#     class Meta:
#         model = FichaImage
#         fields = ["image", "attributes"]
#         widgets = {
#             "image": CustomFileInput(),
#         }
#
#
# # add a fichaimage inlineformset to fichaform

class FichaForm(forms.ModelForm):
    main_image = forms.ImageField()

    class Meta:
        model = Ficha
        exclude = ["student", "status", "created_at", "updated_at"]
        fields = [
            "title",
            "main_image",
            "description",
            "analysis",
            "references",
            "keywords",
            "anexos",
            "misc",
        ]
        widgets = {
            "title": forms.TextInput(
                attrs={
                    "rows": 2,
                    "placeholder": "Título de la ficha",
                    "label": "Título",
                }
            ),
            "description": forms.Textarea(
                attrs={
                    "rows": 5,
                    "placeholder": "Descripción de la ficha",
                    "label": "Descripción",
                }
            ),
            "analysis": forms.Textarea(
                attrs={
                    "rows": 5,
                    "placeholder": "Análisis Operacional",
                    "label": "Análisis",
                }
            ),
            "references": forms.Textarea(
                attrs={
                    "rows": 5,
                    "placeholder": "Analisis Referencial",
                    "label": "Referentes",
                }
            ),
            "misc": forms.Textarea(
                attrs={
                    "rows": 2,
                    "placeholder": "Información adicional",
                    "label": "Información adicional",
                }
            ),
            "keywords": forms.Textarea(
                attrs={
                    "rows": 2,
                    "placeholder": "Palabras clave",
                    "label": "Palabras clave",
                }
            ),
            "anexos": forms.URLInput(
                attrs={"class": "url-input", "placeholder": "URL"}
            ),
            # specify other field widgets here...
        }

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop("user", None)
        super(FichaForm, self).__init__(*args, **kwargs)
        if self.instance.pk:
            self.initial["keywords"] = ", ".join(
                [kw.name for kw in self.instance.keywords.all()]
            )

    def save(self, commit=True):
        instance = super(FichaForm, self).save(commit=False)

        if commit:
            instance.save()

        if instance.pk:
            instance.keywords.clear()  # clear old keywords
            keywords = self.cleaned_data["keywords"]
            # Check if keywords is a string or a list and then handle it accordingly
            if isinstance(keywords, str):
                keywords = keywords.split(",")
            for keyword in keywords:
                instance.keywords.add(keyword.strip())  # add new keywords

        return instance

class ReviewForm(forms.ModelForm):
    class Meta:
        model = Review
        fields = ["review"]
        widgets = {
            "review": forms.Textarea(attrs={"rows": 3}),
        }


"""
2D Plasma Electron Energy Module.

EERGY2D contains:
    Electron energy equation
    d(3/2nekTe)/dt = -dQ/dx + Power_in(ext.) - Power_loss(react)
    Input: ne, Te from Plasma1d, E_ext from field solver
    Output: Te
"""

import numpy as np
from copy import deepcopy

from packages.Constants import KB_EV

class EERGY2D(object):
    """Define the Eon Energy Module."""

    def __init__(self, name='Eergy2d'):
        """
        Init the EERGY2D.

        name: str, var, name of the EERGY2D.
        """
        self.name = name

    def from_PLASMA(self, PLA):
        """Copy var from PLASMA2D."""
        self.Te = deepcopy(PLA.Te)
        self.ne = deepcopy(PLA.ne)
        # eon energy = 3/2 * ne * kTe
        self.ergy_e = 1.5*KB_EV*np.multiply(self.ne, self.Te)
        self.fluxex = deepcopy(PLA.fluxex)
        self.fluxez = deepcopy(PLA.fluxez)
        self.dfluxe = deepcopy(PLA.dfluxe)
        self.pwr_in = deepcopy(PLA.pwr_in)

    def to_PLASMA(self, PLA):
        """Copy var to PLASMA2D."""
        PLA.Te = deepcopy(self.Te)

    def _calc_th_cond_coeff(self, MESH):
        """
        Calc thermal conduction coefficient.

        PLA: PLASMA2D object/class.
        th_cond_e: W/m/K, (nz, nx) matrix, heat conductivity for eon
        """
        # calc thermal conductivity for eon
        self.th_cond_e = np.ones_like(self.Te)*1e-3
        self._set_nonPlasma(MESH)

    def _set_nonPlasma(self, MESH):
        """Impose fixed th_cond_coeff on the non-PLAsma materials."""
        for idx, mat in np.ndenumerate(MESH.mat):
            if mat:
                self.th_cond_e[idx] = 1e-3
                self.Te[idx] = 0.1

    def _calc_th_flux(self, MESH):
        """
        Calc eon thermal flux, Qe.

        Qe = 5/2kTe * fluxe - ke * dTe/dx
        dQe = 5/2kTe * dfluxe - ke * d2Te/dx2
        MESH: MESH2D obj/class
        """
        # calc convection term
        self.Qex = 2.5*KB_EV*np.multiply(self.Te, self.fluxex)
        self.Qez = 2.5*KB_EV*np.multiply(self.Te, self.fluxez)
        # self.dQe = 0.0
        # self.dQe = MESH.cnt_diff((self.Qex, self.Qez), imode='Vector')
        self.dQe = 2.5*KB_EV*np.multiply(self.Te, self.dfluxe)
        # calc conduction term
        self.dTex, self.dTez = MESH.cnt_diff(self.Te)
        self.d2Te = MESH.cnt_diff_2nd(self.Te)
        self.Qex -= np.multiply(self.th_cond_e, self.dTex)
        self.Qez -= np.multiply(self.th_cond_e, self.dTez)
        self.dQe -= np.multiply(self.th_cond_e, self.d2Te)

    def _limit_Te(self, T_min=0.001, T_max=100.0):
        """Limit Te in the PLAsma."""
        self.Te = np.clip(self.Te, T_min, T_max)

    def solve_Te(self, MESH, dt):
        """
        Solve for Te.

        dt: s, var, time step for explict method
        PLA: PLASMA2D object/class.
        TXP: TRANSP2D object/class.
        """
        self._calc_th_cond_coeff(MESH)
        self._calc_th_flux(MESH)
        self.ergy_e += (-self.dQe + self.pwr_in)*dt
        self.Te = np.divide(self.ergy_e, self.ne)/1.5/KB_EV
        self._set_nonPlasma(MESH)
        self._limit_Te()


'''
A set of functions to help with feature creation
'''

import numpy as np
import matplotlib.pyplot as plt
from skimage.feature import hog
from image_processing import convert_to_gray
from sklearn.base import BaseEstimator, TransformerMixin

class CreateFeatures(BaseEstimator, TransformerMixin):
    def __init__(self, color='color', loc=True, hog=False):
        self.color = color
        self.loc = True
        self.hog = hog
        self.feature_params = {'color': color, 'loc': loc, 'hog': hog}

    def fit(self, X, *args):
        return self

    def transform(self, X, *args):
        X = self._create_features(X)
        return X

    def _create_features(self, img):

        if self.color == 'gray':
            make_gray = True
        else:
            make_gray = False

        if self.hog is not False:
            include_hog_features = True
            pixels_per_cell = self.hog
        else:
            include_hog_features = False

        include_loc_features = self.loc

        # Check if it is just as single image
        if len(img.shape) < 4:
            single_img = True
        else:
            single_img = False

        # Convert to grayscale if necessary
        if make_gray:
            gray_img = convert_to_gray(img)
            pixel_features = gray_img.flatten()
        else:
            # i included to make sure reshape works with multiple images
            i = (not single_img) * 1
            pixel_features = img.reshape(np.prod(img.shape[:2 + i]), 3)

        out = pixel_features.reshape(len(pixel_features), -1)

        # Calculate x, y coordinates of each pixel
        if include_loc_features:
            loc_features = self._create_loc_features(img.shape,
                                                     single_image=single_img)
            out = np.c_[out, loc_features]

        if include_hog_features:
            hog_features = self._create_hog_features(
                img, pixels_per_cell=pixels_per_cell)
            out = np.c_[out, hog_features]

        return out

    def _create_hog_features(self,
                             imgs,
                             orientations=8,
                             pixels_per_cell=(20, 20),
                             gray_input=False,
                             visualize=False,
                             multichannel=False):

        # Check if grayscale input
        multichannel = not gray_input

        # If an array of more than 1 image
        if len(imgs.shape) == 4 + gray_input * 1:
            output = np.zeros(shape=(imgs.shape[0],
                                     imgs.shape[1] * imgs.shape[2],
                                     orientations))

            for i, img in enumerate(imgs):
                fd_rr = self._create_hog_features(
                    img,
                    orientations=orientations,
                    pixels_per_cell=pixels_per_cell,
                    multichannel=multichannel,
                    visualize=visualize)

                output[i] = fd_rr
            return output.reshape(output.shape[0] * output.shape[1],
                                  orientations)

        # Else if only one image
        else:
            img = imgs

            # Check that the image is square
            assert img.shape[0] == img.shape[1]

            # If want to display the output
            if visualize:
                fd, hog_image = hog(
                    imgs,
                    orientations=orientations,
                    pixels_per_cell=pixels_per_cell,
                    cells_per_block=(1, 1),
                    visualize=visualize,
                    multichannel=multichannel,
                    feature_vector=False,
                )

                plt.imshow(hog_image, cmap='gray')

            # Don't want to display the output
            elif not visualize:
                fd = hog(
                    imgs,
                    orientations=orientations,
                    pixels_per_cell=pixels_per_cell,
                    cells_per_block=(1, 1),
                    visualize=visualize,
                    multichannel=multichannel,
                    feature_vector=False,
                )

            fd_r = fd.reshape(fd.shape[0], fd.shape[1], orientations)
            fd_r = np.repeat(fd_r, pixels_per_cell[0], axis=0)
            fd_r = np.repeat(fd_r, pixels_per_cell[1], axis=1)
            fd_rr = fd_r.reshape(img.shape[0]**2, orientations)
            return fd_rr

    def _create_loc_features(self, array_shape, single_image=False):
        """

        Args:
            x (type): Image to flip

        Returns:
            Augmented image
        """

        if single_image:
            array_shape = (1, ) + array_shape

        x, y = create_pixel_loc(array_shape[1:3])
        x = np.tile(x, array_shape[0]).reshape(-1, 1)
        y = np.tile(y, array_shape[0]).reshape(-1, 1)

        return np.hstack((x, y))

def create_pixel_loc(shape):
    """Creates two vectors, encoding the x, y positions of pixels in an image
    of specified 'shape'

    Args:
        shape (tuple of length 2): Shape of image

    Returns:
        x (np.array): x coordinates (flattened)
        y (np.array): y coordinates (flattened)
    """
    row_ = np.array(range(0, shape[0] * shape[1]))
    y = row_.reshape(shape[0], shape[1]).T.flatten() % shape[1]
    x = row_ % shape[1]
    return x / shape[1], y / shape[1]


"""______________________ ______________________ ______________________ ______________________

______________________ ______________________ ______________________ ______________________"""

teste = []
teste.append('Francis')
teste.append((34))
galera = list()
galera.append(teste[:])
teste[0] = 'Paul'
teste[1] = 35

galerateste = [['Pedro', 22], ['Paulo', 43], ['Ana', 22], ['Maria', 35]]
print(galerateste[2][1])    # Utiliza 2 parâmetros para acessar lista dentro de lista.
for pessoas in galerateste:
    print(pessoas[1]) #Utiliza o parâmentro para definir qual elemento acessar na lista
for pessoas in galerateste:
    print(f' {pessoas[0]}, tem {pessoas[1]} anos de idade')

galera = list()
dado = list()
for c in range(0, 3):
    dado.append(str(input('Nome: ')))
    dado.append(int(input('Idade: ')))
    galera.append(dado[:]) #Utiliza o parâmentro para fazer uma cópia da lista dentro da outra, caso não, elas seriam interligadas.
    dado.clear()
menores = maiores = 0
for pessoas in galera:
    if pessoas[1] < 18:
        print(f'{pessoas[0]} é menor de idade')
        menores += 1
    else:
        print(f'{pessoas[0]} é maior de idade.')
        maiores += 1
print(f'Tota de {maiores} pessoas maiores de idade e {menores} menores.')

#nada


from keras.callbacks import Callback
import keras.backend as K
import json

class Logger(Callback):

	def __init__(self, filepath,  hyperparams, period=1):
		super(Logger, self).__init__()
		self.filepath = filepath #Save filepath for logging
		self.period = period #Logging period
		self.epochs_since_last_save = 0
		self.hyperparams = hyperparams

	def on_epoch_end(self, epoch, logs=None):
		logs = logs or {} #Creating a dictionary object from logs, if empty, logs becomes an empty dict

		self.epochs_since_last_save += 1
		if self.epochs_since_last_save >= self.period: #Need to log
			self.epochs_since_last_save = 0
			print("Generating checkpoint and logs")

			#### Generating log file ####
			#Current train state
			trainState = {
            	"trainLoss" : logs['loss'],
            	"validateLoss" : logs['val_loss'],
            	"epoch" : epoch,
            	"learningRate" : float(K.get_value(self.model.optimizer.lr))
			}
			#Adding hyperparameters to log file
			outputDict = {
            	'trainState' : trainState,
            	'trainingHyperparameters' : self.hyperparams
			}
			#Populate variables in log filepath
			filepath = self.filepath.format(epoch=epoch + 1, **logs)
			with open(filepath, 'w') as f:
				json.dump(outputDict, f) #Write to file


import numpy as np

class CategoricalNB:

    def __init__(self):
        pass

    def fit(self, X, y):
        self.y_keys = self._get_percent_unique(y)
        self.__options = {}

        for key in self.y_keys[0]:
            self.__options[key] = self._get_probs(X, y, key)

        return self

    def predict(self, X):
        predictions = dict()
        # Tem como simplificar para caralho
        for key in self.__options.keys():
            predictions[key] = self._predict(key, X, self.__options)
        predictions = self._set_class_predicted(predictions)
        return predictions

    def score(self, y_true, y_pred):

        y_true = self._to_numpy(y_true)
        y_pred = self._to_numpy(y_pred)

        return np.sum(y_true == y_pred) / y_true.size

    # Funções auxiliares

    def _is_percent_zero(self,percent):
        if percent.size == 0:
            return 0
        else:
            return percent

    def _get_percent_unique(self, _obj):
        _obj = self._to_numpy(_obj)
        key, count = np.unique(_obj, return_counts=True)
        percent = count / _obj.shape[0]
        return np.asarray((key, percent))

    def _to_numpy(self, obj):
        if not isinstance(obj, np.ndarray):
            obj =  np.array(obj)
        return obj

    def _predict(self, key, X, array_prob):
        prediction = []
        for array in X:
            predicted = np.array([1])

            for value, prob in zip(array, range(0, len(array_prob[key]))):
                index = np.where(array_prob[key][prob][0] == value)

                percent = self._is_percent_zero(array_prob[key][prob][1, index])
                predicted = np.vstack([predicted, percent])
            predicted = np.prod(predicted) * self.y_keys[1, np.where(self.y_keys[0] == key)]
            prediction.append(predicted)
        return prediction

    def _get_probs(self, X_to_prob, y_prob, _key):
        probs = list()
        array_temp = X_to_prob[np.where(y_prob == _key)[0]]
        for i in range(0, array_temp.shape[1]):
            key_and_probability = self._get_percent_unique(array_temp[:,i])
            probs.append(key_and_probability)
        return probs

    def _set_class_predicted(self, predictions):
        keys = list(predictions.keys())
        predictions_classes = np.array([0])
        for item1, item2 in zip(predictions[keys[0]], predictions[keys[1]]):
            if item1 > item2:
                predictions_classes = np.vstack([predictions_classes, keys[0]])
            elif item2 > item1:
                predictions_classes = np.vstack([predictions_classes, keys[1]])
            else:
                predictions_classes = np.vstack([predictions_classes, None])
        return predictions_classes[1:]


"""
ChordNova v3.0 [Build: 2021.1.14]
(c) 2020 Wenge Chen, Ji-woon Sim.
Port to Python by osbertngok
"""

import typing
import enum
import music21

from .cnchordfeature import CNChordFeature, CNChordBigramFeature
from ..i18n import Statement, Language, _
from ..functions import different_name

class OverflowState(enum.Enum):
    NoOverflow = 0
    Single = 1
    Total = 2

class OutputMode(enum.Enum):
    Both = 0
    MidiOnly = 1
    TextOnly = 2

class CNChord:
    """
    Implementation of Chord based on
        chorddata.h / chorddata.cpp
    of original C++ implementation

    But:
        1. Attempt to utilize music21 to avoid re-inventing the wheels
        2. It only contains model information;
           generation logics are separated into a standalone module.
    """

    """
    Deliberately not using inheritance here but for no obvious reason
    """
    _chord: music21.chord.Chord
    _voice_leading_max: int  # Range of Movement, refers to Chord.vlmax

    s_size: int  # m; size of note_set
    tension: float  # t
    thickness: float  # h
    root: int  # r
    g_center: int  # g

    span: int  # s
    sspan: int  # ss
    similarity: int  # x

    _chroma_old: float  # kk

    chroma: float  # k
    Q_indicator: float  # Q
    common_note: int  # c
    sv: int  # sv, Σvec

    overflow_state: OverflowState

    hide_octave: bool
    name: str  # name of each note in the chord
    name_with_octave: str  # name and octave of each note in the chord

    vec: typing.List[int]  # v
    self_diff: typing.List[int]  # d
    count_vec: typing.List[int]  # vec

    """
    This is to replace prev_chroma_old
    """
    ref_chord: typing.Optional["CNChord"]  # reference chord to calculate chroma_old

    """
    We want to make evaluation lazy. Evaluation won't be triggered
    until property is accessed.
    """
    _dirty: bool

    def __init__(self):
        self._chord = music21.chord.Chord()

    @staticmethod
    def from_notes(
        notes: typing.List[int], ref_chord: typing.Optional["CNChord"] = None
    ) -> "CNChord":
        """
        See also
            Chord(const vector<int>& _notes, double _chroma_old = 0.0);
        in original C++ implementation
        :param notes:
        :param prev_chroma_old:
        :return:
        """
        ret = CNChord()
        ret._chord = music21.chord.Chord(notes)
        if ref_chord is not None:
            ret.ref_chord = ref_chord
        return ret

    def copy(self) -> "CNChord":
        """
        See also
            Chord(const Chord&);
        in original C++ Implementation
        :return:
        """
        ret = CNChord()
        ret._chord = music21.chord.Chord(self._chord.pitches)
        ret.ref_chord = self.ref_chord
        return ret

    @property
    def set_id(self) -> int:
        """
        an integer representing 'note_set'; unique for different 'note_set's
        Assign a unique id for each pitch set (according to set theory)
        Because music21 has an implementation of forteClass already,
        let's use that instead.

        See also
            int& get_set_id();
        in original C++ implementation
        :return:
        """
        # See also: https://web.mit.edu/music21/doc/moduleReference/moduleChord.html#music21.chord.Chord.chordTablesAddress
        return self._chord.chordTablesAddress.forteClass

    @property
    def voice_leading_max(self) -> int:
        """
         See also
             void _set_vl_max(const int&);
         in original C++ Implementation
        :return:
        """
        return self.voice_leading_max

    def find_vec(self, in_analyser, in_substitution) -> "CNChord":
        """
        interface of '_find_vec'

        See Also:
            void find_vec(Chord& new_chord, bool in_analyser = false, bool in_substitution = false);
        In original C++ Implementation

        Note:

        The original function in C++ implementation is using pass by reference signature,
        which is commonly used in C++ for memory optimization
        at the expense of readability.

        In Python there is no point to follow this.

        :return:
        """
        raise NotImplementedError()

    def inverse_param(self):
        """
        # Swap
        self.prev_chroma_old, self.chroma_old = self.chroma_old, self.prev_chroma_old
        self.chroma *= -1
        self.Q_indicator *= -1
        """
        raise NotImplementedError()

    def __repr__basic__(self, language: Language) -> str:
        output_str = ""
        output_str += f"t = {self.tension}, s = {self.span}, "
        output_str += f"vec = {self.count_vec}\n"
        output_str += f"d = {self.self_diff}\n"
        output_str += f"n = {self.s_size}, "
        output_str += f"m = {self.t_size}, "
        output_str += f"n/m = {self.s_size * 1.0 / self.t_size}, "
        output_str += f"h = {self.thickness}, "
        output_str += f"g = {self.g_center}%, "
        output_str += f"{_(Statement.ROOT, language)}: "
        output_str += f" (r = {self.root})\n\n"
        return output_str

    def __repr__advanced__(self) -> str:
        output_str = ""
        output_str += f"k = {self.chroma}, "
        output_str += f"kk = {self.chroma_old - self.prev_chroma_old}, "
        output_str += f"c = {self.common_note}, "
        output_str += f"ss = {self.sspan}, "
        output_str += f"sv = {self.sv}, "
        output_str += f"v = {self.vec}\n"
        return output_str

    def __repr__diff__(self, chord: "CNChord", language: Language) -> str:
        output_str = ""
        output_str += f"A = {chord.Q_indicator}, "
        output_str += f"x = {chord.similarity}, "
        output_str += f"dr = {chord.root - self.root}, "
        output_str += f"dn = {chord.s_size * 1.0 / self.s_size}, "
        output_str += f"dt = {chord.thickness - self.thickness}, "
        output_str += f"ds = {chord.span - self.span}%, "
        output_str += f"dg = {chord.g_center - self.g_center}\n\n"
        return output_str

    def __repr__overflowstate(self) -> str:
        if self.overflow_state == OverflowState.Single:
            return "*\n"
        elif self.overflow_state == OverflowState.Total:
            return "**\n"
        elif self.overflow_state == OverflowState.NoOverflow:
            return "\n"
        return ""

    @property
    def chroma_old(self) -> float:
        return self._chroma_old

    @property
    def prev_chroma_old(self) -> float:
        return self.ref_chord.chroma_old

    @property
    def notes(self) -> typing.List[int]:
        """
        always regarded as a sorted (L -> H) vector
        TODO: materialize this so we do not need to call list comprehension and sorted function
        :return:
        """
        ret = [p.midi for p in self._chord.pitches]
        sorted(ret)
        return ret

    @property
    def t_size(self) -> int:
        """
        n; size of notes
        :return:
        """
        return len(self._chord.notes)

    def materialize_chord_feature(self) -> CNChordFeature:
        raise NotImplementedError()

    def calculate_chord_bigram_feature(self) -> CNChordBigramFeature:
        raise NotImplementedError()

    def print_initial(self, language: Language):
        """
        c++: ChordData::printInitial(Language language)
        :param language:
        :return:
        """
        output_str = ""
        output_str += f"{_(Statement.INITIAL_CHORD, language)}: {self.notes}  "
        output_str += f"({self.name}\n"
        output_str += self.__repr__basic__(language)

        print(output_str)

    def print(self, chord: "CNChord", language: Language):
        output_str = ""
        output_str += f"-> {chord.notes} ,\n"
        output_str += f"( {chord.name} )"

        output_str += chord.__repr__overflowstate()

        output_str += chord.__repr__advanced__()

        output_str += chord.__repr__basic__(language)

        output_str += self.__repr__diff__(chord, language)

        print(output_str)

    def print_analysis(
        self,
        antechord: "CNChord",
        postchord: "CNChord",
        str_ante: str,
        str_post: str,
        language: Language,
    ):
        output_str = ""
        if self.hide_octave:
            output_str += f"({antechord.name}) -> ({postchord.name})"
        else:
            output_str += (
                f"({antechord.name_with_octave}) -> ({postchord.name_with_octave})"
            )

        if postchord.overflow_state == OverflowState.Single:
            output_str += "*\n"
        elif postchord.overflow_state == OverflowState.Total:
            output_str += "**\n"
        elif postchord.overflow_state == OverflowState.NoOverflow:
            output_str += "\n"

        b = different_name(str_ante, antechord.name) or different_name(
            str_post, postchord.name
        )
        if b:
            output_str += f"( ({str_ante}) -> ({str_post}) )\n"

        output_str += f"{antechord.notes} -> {postchord.notes} \n\n"

        output_str += f"{_(Statement.ANTE_CHORD)}:\n"
        output_str += antechord.__repr__basic__(language)

        output_str += f"{_(Statement.POST_CHORD)}:\n"
        output_str += postchord.__repr__basic__(language)

        output_str += f"{_(Statement.CHORD_PROGRESSION)}:\n"

        output_str += postchord.__repr__advanced__()
        output_str += antechord.__repr__diff__(postchord, language)

        print(output_str)

    def print_substitution(
        self,
        param: str,
        print_ante: bool,
        print_post: bool,
        antechord: "CNChord",
        postchord: "CNChord",
        language: Language,
    ):
        raise NotImplementedError()


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

# Import Python libs
import os
import sys
import shutil
import tempfile
import textwrap
import copy
from cStringIO import StringIO

# Import Salt Testing libs
from salttesting import TestCase
from salttesting.helpers import ensure_in_syspath

ensure_in_syspath('../')

# Import Salt libs
import integration
import salt.loader
import salt.config
import salt.utils
from salt.state import HighState
from salt.utils.pydsl import PyDslError

REQUISITES = ['require', 'require_in', 'use', 'use_in', 'watch', 'watch_in']

OPTS = salt.config.minion_config(None)
OPTS['state_events'] = False
OPTS['id'] = 'whatever'
OPTS['file_client'] = 'local'
OPTS['file_roots'] = dict(base=['/tmp'])
OPTS['cachedir'] = 'cachedir'
OPTS['test'] = False
OPTS['grains'] = salt.loader.grains(OPTS)

class PyDSLRendererTestCase(TestCase):

    def setUp(self):
        self.HIGHSTATE = HighState(OPTS)
        self.HIGHSTATE.push_active()

    def tearDown(self):
        self.HIGHSTATE.pop_active()

    def render_sls(self, content, sls='', env='base', **kws):
        return self.HIGHSTATE.state.rend['pydsl'](
            StringIO(content), env=env, sls=sls, **kws
        )

    def test_state_declarations(self):
        result = self.render_sls(textwrap.dedent('''
            state('A').cmd.run('ls -la', cwd='/var/tmp')
            state().file.managed('myfile.txt', source='salt://path/to/file')
            state('X').cmd('run', 'echo hello world', cwd='/')

            a_cmd = state('A').cmd
            a_cmd.run(shell='/bin/bash')
            state('A').service.running(name='apache')
        '''))
        self.assertTrue('A' in result and 'X' in result)
        A_cmd = result['A']['cmd']
        self.assertEqual(A_cmd[0], 'run')
        self.assertEqual(A_cmd[1]['name'], 'ls -la')
        self.assertEqual(A_cmd[2]['cwd'], '/var/tmp')
        self.assertEqual(A_cmd[3]['shell'], '/bin/bash')

        A_service = result['A']['service']
        self.assertEqual(A_service[0], 'running')
        self.assertEqual(A_service[1]['name'], 'apache')

        X_cmd = result['X']['cmd']
        self.assertEqual(X_cmd[0], 'run')
        self.assertEqual(X_cmd[1]['name'], 'echo hello world')
        self.assertEqual(X_cmd[2]['cwd'], '/')

        del result['A']
        del result['X']
        self.assertEqual(len(result), 2)
        # 2 rather than 1 because pydsl adds an extra no-op state
        # declaration.

        s_iter = result.itervalues()
        try:
            s = s_iter.next()['file']
        except KeyError:
            s = s_iter.next()['file']
        self.assertEqual(s[0], 'managed')
        self.assertEqual(s[1]['name'], 'myfile.txt')
        self.assertEqual(s[2]['source'], 'salt://path/to/file')

    def test_requisite_declarations(self):
        result = self.render_sls(textwrap.dedent('''
            state('X').cmd.run('echo hello')
            state('A').cmd.run('mkdir tmp', cwd='/var')
            state('B').cmd.run('ls -la', cwd='/var/tmp') \
                        .require(state('X').cmd) \
                        .require(cmd='A') \
                        .watch(service='G')
            state('G').service.running(name='collectd')
            state('G').service.watch_in(state('A').cmd)

            state('H').cmd.require_in(cmd='echo hello')
            state('H').cmd.run('echo world')
        '''))
        self.assertTrue(len(result), 6)
        self.assertTrue(set("X A B G H".split()).issubset(set(result.keys())))
        b = result['B']['cmd']
        self.assertEqual(b[0], 'run')
        self.assertEqual(b[1]['name'], 'ls -la')
        self.assertEqual(b[2]['cwd'], '/var/tmp')
        self.assertEqual(b[3]['require'][0]['cmd'], 'X')
        self.assertEqual(b[4]['require'][0]['cmd'], 'A')
        self.assertEqual(b[5]['watch'][0]['service'], 'G')
        self.assertEqual(result['G']['service'][2]['watch_in'][0]['cmd'], 'A')
        self.assertEqual(
            result['H']['cmd'][1]['require_in'][0]['cmd'], 'echo hello'
        )

    def test_include_extend(self):
        result = self.render_sls(textwrap.dedent('''
            include(
                'some.sls.file',
                'another.sls.file',
                'more.sls.file',
                delayed=True
            )
            A = state('A').cmd.run('echo hoho', cwd='/')
            state('B').cmd.run('echo hehe', cwd='/')
            extend(
                A,
                state('X').cmd.run(cwd='/a/b/c'),
                state('Y').file('managed', name='a_file.txt'),
                state('Z').service.watch(file='A')
            )
        '''))
        self.assertEqual(len(result), 4)
        self.assertEqual(
            result['include'],
            [{'base': sls} for sls in
             ('some.sls.file', 'another.sls.file', 'more.sls.file')]
        )
        extend = result['extend']
        self.assertEqual(extend['X']['cmd'][0], 'run')
        self.assertEqual(extend['X']['cmd'][1]['cwd'], '/a/b/c')
        self.assertEqual(extend['Y']['file'][0], 'managed')
        self.assertEqual(extend['Y']['file'][1]['name'], 'a_file.txt')
        self.assertEqual(len(extend['Z']['service']), 1)
        self.assertEqual(extend['Z']['service'][0]['watch'][0]['file'], 'A')

        self.assertEqual(result['B']['cmd'][0], 'run')
        self.assertTrue('A' not in result)
        self.assertEqual(extend['A']['cmd'][0], 'run')

    def test_cmd_call(self):
        result = self.HIGHSTATE.state.call_template_str(textwrap.dedent('''\
            #!pydsl
            state('A').cmd.run('echo this is state A', cwd='/')

            some_var = 12345
            def do_something(a, b, *args, **kws):
                return dict(result=True, changes={'a': a, 'b': b, 'args': args, 'kws': kws, 'some_var': some_var})

            state('C').cmd.call(do_something, 1, 2, 3, x=1, y=2) \
                          .require(state('A').cmd)

            state('G').cmd.wait('echo this is state G', cwd='/') \
                          .watch(state('C').cmd)
        '''))
        ret = (result[k] for k in result.keys() if 'do_something' in k).next()
        changes = ret['changes']
        self.assertEqual(
            changes,
            dict(a=1, b=2, args=(3,), kws=dict(x=1, y=2), some_var=12345)
        )

        ret = (result[k] for k in result.keys() if '-G_' in k).next()
        self.assertEqual(ret['changes']['stdout'], 'this is state G')

    def test_multiple_state_func_in_state_mod(self):
        with self.assertRaisesRegexp(PyDslError, 'Multiple state functions'):
            self.render_sls(textwrap.dedent('''
                state('A').cmd.run('echo hoho')
                state('A').cmd.wait('echo hehe')
            '''))

    def test_no_state_func_in_state_mod(self):
        with self.assertRaisesRegexp(PyDslError, 'No state function specified'):
            self.render_sls(textwrap.dedent('''
                state('B').cmd.require(cmd='hoho')
            '''))

    def test_load_highstate(self):
        result = self.render_sls(textwrap.dedent('''
            import yaml
            __pydsl__.load_highstate(yaml.load("""
            A:
              cmd.run:
                - name: echo hello
                - cwd: /
            B:
              pkg:
                - installed
              service:
                - running
                - require:
                  - pkg: B
                - watch:
                  - cmd: A
            """))

            state('A').cmd.run(name='echo hello world')
            '''))
        self.assertEqual(len(result), 3)
        self.assertEqual(result['A']['cmd'][0], 'run')
        self.assertEqual(result['A']['cmd'][1]['name'], 'echo hello')
        self.assertEqual(result['A']['cmd'][2]['cwd'], '/')
        self.assertEqual(result['A']['cmd'][3]['name'], 'echo hello world')

        self.assertEqual(len(result['B']['pkg']), 1)
        self.assertEqual(result['B']['pkg'][0], 'installed')

        self.assertEqual(result['B']['service'][0], 'running')
        self.assertEqual(result['B']['service'][1]['require'][0]['pkg'], 'B')
        self.assertEqual(result['B']['service'][2]['watch'][0]['cmd'], 'A')

    def test_ordered_states(self):
        result = self.render_sls(textwrap.dedent('''
            __pydsl__.set(ordered=True)
            A = state('A')
            state('B').cmd.run('echo bbbb')
            A.cmd.run('echo aaa')
            state('B').cmd.run(cwd='/')
            state('C').cmd.run('echo ccc')
            state('B').file.managed(source='/a/b/c')
            '''))
        self.assertEqual(len(result['B']['cmd']), 3)
        self.assertEqual(result['A']['cmd'][1]['require'][0]['cmd'], 'B')
        self.assertEqual(result['C']['cmd'][1]['require'][0]['cmd'], 'A')
        self.assertEqual(result['B']['file'][1]['require'][0]['cmd'], 'C')

    def test_pipe_through_stateconf(self):
        dirpath = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
        if not os.path.isdir(dirpath):
            self.skipTest(
                'The temporary directory {0!r} was not created'.format(
                    dirpath
                )
            )
        output = os.path.join(dirpath, 'output')
        try:
            write_to(os.path.join(dirpath, 'xxx.sls'), textwrap.dedent(
                '''#!stateconf -os yaml . jinja
                .X:
                  cmd.run:
                    - name: echo X >> {0}
                    - cwd: /
                .Y:
                  cmd.run:
                    - name: echo Y >> {1}
                    - cwd: /
                .Z:
                  cmd.run:
                    - name: echo Z >> {2}
                    - cwd: /
                '''.format(output, output, output)))
            write_to(os.path.join(dirpath, 'yyy.sls'), textwrap.dedent('''\
                #!pydsl|stateconf -ps

                __pydsl__.set(ordered=True)
                state('.D').cmd.run('echo D >> {0}', cwd='/')
                state('.E').cmd.run('echo E >> {1}', cwd='/')
                state('.F').cmd.run('echo F >> {2}', cwd='/')
                '''.format(output, output, output)))

            write_to(os.path.join(dirpath, 'aaa.sls'), textwrap.dedent('''\
                #!pydsl|stateconf -ps

                include('xxx', 'yyy')

                # make all states in xxx run BEFORE states in this sls.
                extend(state('.start').stateconf.require(stateconf='xxx::goal'))

                # make all states in yyy run AFTER this sls.
                extend(state('.goal').stateconf.require_in(stateconf='yyy::start'))

                __pydsl__.set(ordered=True)

                state('.A').cmd.run('echo A >> {0}', cwd='/')
                state('.B').cmd.run('echo B >> {1}', cwd='/')
                state('.C').cmd.run('echo C >> {2}', cwd='/')
                '''.format(output, output, output)))

            state_highstate({'base': ['aaa']}, dirpath)
            with salt.utils.fopen(output, 'r') as f:
                self.assertEqual(''.join(f.read().split()), "XYZABCDEF")

        finally:
            shutil.rmtree(dirpath, ignore_errors=True)

    def test_rendering_includes(self):
        dirpath = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
        if not os.path.isdir(dirpath):
            self.skipTest(
                'The temporary directory {0!r} was not created'.format(
                    dirpath
                )
            )
        output = os.path.join(dirpath, 'output')
        try:
            write_to(os.path.join(dirpath, 'aaa.sls'), textwrap.dedent('''\
                #!pydsl|stateconf -ps

                include('xxx')
                yyy = include('yyy')

                # ensure states in xxx are run first, then those in yyy and then those in aaa last.
                extend(state('yyy::start').stateconf.require(stateconf='xxx::goal'))
                extend(state('.start').stateconf.require(stateconf='yyy::goal'))

                extend(state('yyy::Y2').cmd.run('echo Y2 extended >> {0}'))

                __pydsl__.set(ordered=True)

                yyy.hello('red', 1)
                yyy.hello('green', 2)
                yyy.hello('blue', 3)
                '''.format(output)))

            write_to(os.path.join(dirpath, 'xxx.sls'), textwrap.dedent('''\
                #!stateconf -os yaml . jinja

                include:
                  - yyy

                extend:
                  yyy::start:
                    stateconf.set:
                      - require:
                        - stateconf: .goal

                  yyy::Y1:
                    cmd.run:
                      - name: 'echo Y1 extended >> {0}'

                .X1:
                  cmd.run:
                    - name: echo X1 >> {1}
                    - cwd: /
                .X2:
                  cmd.run:
                    - name: echo X2 >> {2}
                    - cwd: /
                .X3:
                  cmd.run:
                    - name: echo X3 >> {3}
                    - cwd: /

                '''.format(output, output, output, output)))

            write_to(os.path.join(dirpath, 'yyy.sls'), textwrap.dedent('''\
                #!pydsl|stateconf -ps

                include('xxx')
                __pydsl__.set(ordered=True)

                state('.Y1').cmd.run('echo Y1 >> {0}', cwd='/')
                state('.Y2').cmd.run('echo Y2 >> {1}', cwd='/')
                state('.Y3').cmd.run('echo Y3 >> {2}', cwd='/')

                def hello(color, number):
                    state(color).cmd.run('echo hello '+color+' '+str(number)+' >> {3}', cwd='/')
                '''.format(output, output, output, output)))

            state_highstate({'base': ['aaa']}, dirpath)
            expected = textwrap.dedent('''\
                X1
                X2
                X3
                Y1 extended
                Y2 extended
                Y3
                hello red 1
                hello green 2
                hello blue 3
                ''')

            with salt.utils.fopen(output, 'r') as f:
                self.assertEqual(sorted(f.read()), sorted(expected))

        finally:
            shutil.rmtree(dirpath, ignore_errors=True)

    def test_compile_time_state_execution(self):
        if not sys.stdin.isatty():
            self.skipTest('Not attached to a TTY')
        dirpath = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
        if not os.path.isdir(dirpath):
            self.skipTest(
                'The temporary directory {0!r} was not created'.format(
                    dirpath
                )
            )
        try:
            write_to(os.path.join(dirpath, 'aaa.sls'), textwrap.dedent('''\
                #!pydsl

                __pydsl__.set(ordered=True)
                A = state('A')
                A.cmd.run('echo hehe > {0}/zzz.txt', cwd='/')
                A.file.managed('{1}/yyy.txt', source='salt://zzz.txt')
                A()
                A()

                state().cmd.run('echo hoho >> {2}/yyy.txt', cwd='/')

                A.file.managed('{3}/xxx.txt', source='salt://zzz.txt')
                A()
                '''.format(dirpath, dirpath, dirpath, dirpath)))
            state_highstate({'base': ['aaa']}, dirpath)
            with salt.utils.fopen(os.path.join(dirpath, 'yyy.txt'), 'r') as f:

                self.assertEqual(f.read(), 'hehe\nhoho\n')
            with salt.utils.fopen(os.path.join(dirpath, 'xxx.txt'), 'r') as f:
                self.assertEqual(f.read(), 'hehe\n')
        finally:
            shutil.rmtree(dirpath, ignore_errors=True)

    def test_nested_high_state_execution(self):
        dirpath = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
        if not os.path.isdir(dirpath):
            self.skipTest(
                'The temporary directory {0!r} was not created'.format(
                    dirpath
                )
            )
        output = os.path.join(dirpath, 'output')
        try:
            write_to(os.path.join(dirpath, 'aaa.sls'), textwrap.dedent('''\
                #!pydsl
                __salt__['state.sls']('bbb')
                state().cmd.run('echo bbbbbb', cwd='/')
                '''))
            write_to(os.path.join(dirpath, 'bbb.sls'), textwrap.dedent(
                '''
                # {{ salt['state.sls']('ccc')
                test:
                  cmd.run:
                    - name: echo bbbbbbb
                    - cwd: /
                '''))
            write_to(os.path.join(dirpath, 'ccc.sls'), textwrap.dedent(
                '''
                #!pydsl
                state().cmd.run('echo ccccc', cwd='/')
                '''))
            state_highstate({'base': ['aaa']}, dirpath)
        finally:
            shutil.rmtree(dirpath, ignore_errors=True)

    def test_repeat_includes(self):
        dirpath = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
        if not os.path.isdir(dirpath):
            self.skipTest(
                'The temporary directory {0!r} was not created'.format(
                    dirpath
                )
            )
        output = os.path.join(dirpath, 'output')
        try:
            write_to(os.path.join(dirpath, 'b.sls'), textwrap.dedent('''\
                #!pydsl
                include('c')
                include('d')
                '''))
            write_to(os.path.join(dirpath, 'c.sls'), textwrap.dedent('''\
                #!pydsl
                modtest = include('e')
                modtest.success
                '''))
            write_to(os.path.join(dirpath, 'd.sls'), textwrap.dedent('''\
                #!pydsl
                modtest = include('e')
                modtest.success
                '''))
            write_to(os.path.join(dirpath, 'e.sls'), textwrap.dedent('''\
                #!pydsl
                success = True
                '''))
            state_highstate({'base': ['b']}, dirpath)
            state_highstate({'base': ['c', 'd']}, dirpath)
        finally:
            shutil.rmtree(dirpath, ignore_errors=True)

def write_to(fpath, content):
    with salt.utils.fopen(fpath, 'w') as f:
        f.write(content)

def state_highstate(state, dirpath):
    opts = copy.copy(OPTS)
    opts['file_roots'] = dict(base=[dirpath])
    HIGHSTATE = HighState(opts)
    HIGHSTATE.push_active()
    try:
        high, errors = HIGHSTATE.render_highstate(state)
        if errors:
            import pprint
            pprint.pprint('\n'.join(errors))
            pprint.pprint(high)

        out = HIGHSTATE.state.call_high(high)
        # pprint.pprint(out)
    finally:
        HIGHSTATE.pop_active()

if __name__ == '__main__':
    from integration import run_tests
    run_tests(PyDSLRendererTestCase, needs_daemon=False)


"""
model.py: bert ner 模型
by: qliu
update date: 2021-12-17
"""
import os, torch
import torch.nn as nn
from transformers import BertForTokenClassification

class BertNer(nn.Module):
    def __init__(self, common_config, model_config):
        self.common_config = common_config
        self.model_config = model_config
        self.start_epoch = 0
        self.prev_acc_score = 0
        self.prev_f1_score = 0

    def get_model(self):
        """
        通过读取配置参数,加载 bert ner 模型
        """
        checkpoint_path = os.path.join(self.common_config.checkpoint_dir, self.model_config.bert_model_name)
        if self.model_config.load_checkpoint and os.path.exists(checkpoint_path):
            checkpoint = torch.load(checkpoint_path, map_location="cpu")
            self.start_epoch = checkpoint["epoch"] + 1
            self.prev_acc_score = checkpoint["acc_score"]
            self.prev_f1_score = checkpoint["f1_score"]
            model = BertForTokenClassification.from_pretrained(self.common_config.bert_model_dir,state_dict=checkpoint["model_state"], num_labels=self.common_config.labels_num)
            print("Loaded pretrain bert ner model, previous acc: {}, previous f1_score : {}".format(self.prev_acc_score, self.prev_f1_score))
        else:
            model = BertForTokenClassification.from_pretrained(self.common_config.bert_model_dir, num_labels=self.common_config.labels_num)
        return model


from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

import time
import json
import datetime
import requests

# endpoint
data_all = requests.get('https://corona.lmao.ninja/all')
data_by_country = requests.get('https://corona.lmao.ninja/countries/')

def main_api(request):
    fucking_list = ['updated', 'affectedcountries']

    datas_all = []
    for i, j in data_all.json().items():
        if str.lower(i) in fucking_list:
            pass
        else:
            datas_all.append({str.upper(i): j})

    # context
    context = {
        "datas": datas_all,
        "last_update": last_update()
    }
    return context

def card_api(request, limit=None):
    # paginate
    paginate_by = limit if limit is not None else 20
    paginator = Paginator(data_by_country.json(), paginate_by)
    page = request.GET.get('page')
    datas = paginator.get_page(page)

    # context
    context = {
        "total_data": data_all.json()['affectedCountries'],
        "last_update": last_update(),
        "datas": datas,
    }
    return context

def last_update():
    unix_timestamp = data_all.json()['updated']
    last_update = datetime.datetime.fromtimestamp(
        unix_timestamp//1000.0).strftime('%A, %B %d %Y, %H:%M:%S %p (UTC)')
    return last_update


#!C:\Python34
import re

def main():
	fd = open( "C:\\1.Class\\PracticeInClass\\Class28\\audio.conf")
	data = fd.read()
	#ob = re.compile( r"\bt\w+e\b", re.IGNORECASE)
	ob = re.compile( r"\b\w+e\b", re.IGNORECASE)
	for match in ob.finditer(data):
		print ( match)

if __name__ == "__main__":
    main()


import sys  # sys нужен для передачи argv в QApplication
from PyQt5 import QtWidgets
import design
import requests
import time
import re

url_get_info = 'https://callapi.gravitel.ru/api/v1/getdialoutstat'
url_list = 'https://callapi.gravitel.ru/api/v1/listdialouts'
headers = {'Content-Type': 'application/json'}

def check_token():
    forCheckToken = requests.post(url_list, headers=headers, json={'token': token}).json()
    if forCheckToken['status'] == 'error':
        print("May be invalid token!")
        exit()

class ExampleApp(QtWidgets.QMainWindow, design.Ui_CounterCallAPI):
    def __init__(self):
        # Это здесь нужно для доступа к переменным, методам
        # и т.д. в файле design.py
        super().__init__()
        self.setupUi(self)  # Это нужно для инициализации нашего дизайна
        self.found_dialouts.pressed.connect(self.connectUI)
        self.get_info_dialout.pressed.connect(self.get_info)

    def output_message(self, message_to_ui):
        self.output_info.setText(message_to_ui)

    def connectUI(self):
        token = self.token_enter.text()
        try:
            all_dialouts = requests.post(url_list, headers=headers, json={'token': token}).json()
            for_output = """

                    ID автообзвона
                    Дата начала автообзвона
                    Дата окончания автообзвона
                    Статус автообзвона

"""
            for id_num in all_dialouts['result']:
                tsstart = time.ctime(id_num['tsstart'])
                tsend = time.ctime(id_num['tsend'])
                if id_num['status'] == 1:
                    status = 'Новый'
                elif id_num['status'] == 2:
                    status = 'Обработан'
                elif id_num['status'] == 3:
                    status = 'Запущен'
                elif id_num['status'] == 4:
                    status = 'Выполнен'
                elif id_num['status'] == 5:
                    status = 'Остановлен'
                else:
                    status = 'Ошибка'
                for_output = for_output + "
" + str(id_num['id']) + "	" + tsstart + \
                             "	" + tsend + "	" + status + "
"
            self.output_message(for_output)
        except:
            message_to_ui = "Не правильно введен токен"
            self.output_message(message_to_ui)

    def get_info(self):
        token = self.token_enter.text()
        id_dialout = self.dialout.text()
        try:
            all_dialouts = requests.post(url_get_info, headers=headers,
                                         json={'token': token, 'dialoutid': id_dialout}).json()
            for_output = """

                    Номер
                    Время начала звонка
                    Статус звонка
                    Статус вызова
                    Длительность вызова

"""
            for info_num in all_dialouts['result']['data']:
                if (info_num['duration'] // 60) == 0:
                    duration = str(info_num['duration'] % 60) + ' сек'
                else:
                    duration = str((info_num['duration'] // 60)) + ' мин ' + str(info_num['duration'] % 60) + ' сек'

                if info_num['callstatus'] == 1:
                    callstatus = 'Новый'
                elif info_num['callstatus'] == 2:
                    callstatus = 'Активный'
                else:
                    callstatus = 'Завершен'

                if info_num['intstatus'] == 1:
                    intstatus = 'Новый'
                elif info_num['intstatus'] == 2:
                    intstatus = 'Получен ответ'
                elif info_num['intstatus'] == 3:
                    intstatus = 'Занято'
                elif info_num['intstatus'] == 4:
                    intstatus = 'Не сняли трубку'
                else:
                    intstatus = 'Ошибка'

                for_output = for_output + "
" + str(info_num['number']) + "	" + info_num['tscall'] + \
                             "	" + callstatus + "	" + intstatus + "	" + duration + "
"
            self.output_message(for_output)
        except:
            message_to_ui = "Что-то пошло не так"
            self.output_message(message_to_ui)

def main():
    app = QtWidgets.QApplication(sys.argv)  # Новый экземпляр QApplication
    window = ExampleApp()  # Создаём объект класса ExampleApp
    window.show()  # Показываем окно
    app.exec_()  # и запускаем приложение

if __name__ == '__main__':  # Если мы запускаем файл напрямую, а не импортируем
    main()  # то запускаем функцию main()


import math

import lasap.containers.observable as observable
from lasap.utils import io

class Parallelizer:
    def __init__(self, dirname, jobid, numjobs):
        self.dirname = dirname
        self.jobid = jobid
        self.numjobs = numjobs

        path = io.data_dir() + dirname
        io.check_dir(path)
        files = io.ls_data_files(path)
        files_per_job = math.ceil(len(files)/numjobs)
        if(jobid > len(files)):
            self.files = []
            self.numfiles = 0
        else:
            if(jobid == numjobs):
                self.files = files[(jobid-1)*files_per_job:]
            else:
                self.files = files[(jobid-1)*files_per_job:jobid*files_per_job]
            self.numfiles = len(self.files)

    def get_obs(self, index):
        file = self.files[index]
        return observable.from_disk(file[:file.rfind('_data')], self.dirname)


import functools

def cache(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        cache_key = args + tuple(kwargs.values())
        if cache_key not in wrapper.internal_cache:
            wrapper.internal_cache[cache_key] = func(*args, **kwargs)
        return wrapper.internal_cache[cache_key]

    wrapper.internal_cache = {}
    return wrapper

@cache
def fibonacci(n):
    print(f'Calculating F({n})')
    if n == 0:
        return 0
    elif n == 1:
        return 1
    return fibonacci(n - 2) + fibonacci(n - 1)

@cache
def test(n):
    print(f'T({n})')
    return 5

print(fibonacci(111))
print(fibonacci(10))
print(test(100))
print(test(100))
print(test(101))
print(fibonacci(10))
print(test.internal_cache)
print(fibonacci.internal_cache)


import sys
# basic libraries
import pandas as pd
#SQL library
from sqlalchemy import create_engine
# NLP
import re
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.stem.porter import PorterStemmer
nltk.download(['punkt', 'wordnet'])
# Machine Learning
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.multioutput import MultiOutputClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import classification_report
from sklearn.model_selection import GridSearchCV
# pickle to store model
import pickle

def load_data(database_filepath):
    """
    Load disaster dateset from SQLite & split dataset into messages and categories

    Input: database_filepath: path of SQLite database file of processed dataset
    Return: X: Message part of disaster_message dataframe
        y: category part of disaster_message dataframe
        categories: names of categories, columns of y
    """
    # Load SQLite dataset
    engine = create_engine('sqlite:///' + database_filepath)
    df = pd.read_sql_table('disaster_messages', engine)

    # split dataset and return message, categories, and column names
    X = df['message']
    y = df.iloc[:, 4:]
    category_names = y.columns.tolist()

    return X, y, category_names

def tokenize(text):
    """
    Clean and tokenize the message

    Input: text: the message for tokenization(X from load_data)
    Return: clean_tokens: token list of message
    """
    # remove non-numerical non-character types and tokenize text
    tokens = word_tokenize(re.sub(r"[^a-zA-Z0-9]", " ", text.lower()))

    # Instantiate lemmatizer and stemmer
    wnl = WordNetLemmatizer()
    ps = PorterStemmer()

    # lemmatize tokens of nouns and verbs and stemming
    clean_tokens = []
    for tok in tokens:
        clean_tok = wnl.lemmatize(tok)
        clean_tok = wnl.lemmatize(clean_tok, 'v')
        clean_tok = ps.stem(clean_tok)
        clean_tokens.append(clean_tok.strip())

    return clean_tokens

def build_model():
    """
    Builds the pipeline of NLP + Classification, Tf-Idf as message
    transformation and AdaBoost+MultiOutputClassifier as
    classification model, along with GridSearchCV strategy

    Return: model: machine learning model described above
    """
    # parameter set for GridSearchCV
    parameters = {
        'tfidf__max_df': (0.9, 1.0),
        'tfidf__ngram_range': ((1, 1), (1, 3))
    }

    # pipeline containing Tf-Idf and AdaBoost
    pipeline = Pipeline([
        ('tfidf', TfidfVectorizer(tokenizer=tokenize)),
        ('clf', MultiOutputClassifier(AdaBoostClassifier(n_estimators=100, random_state=42)))
    ])

    # Instantiate GridSearchCV
    model = GridSearchCV(pipeline, param_grid=parameters)

    return model

def evaluate_model(model, X_test, Y_test, category_names):
    """
    Evaluate model performance and print metrics of validation

    Input:model: classification model from build_model()
        X_test: test set of X(messages)
        Y_test: test set of y(categories)
        category_names: names of message categories(categories column names)
    """
    # print metrics of classification for each column
    Y_pred = pd.DataFrame(model.predict(X_test), columns=category_names)
    print("Classification Accuracy")
    for i in range(36):
        print("Category Name: ",category_names[i])
        print(classification_report(Y_test.iloc[:, i], Y_pred.iloc[:, i]))

def save_model(model, model_filepath):
    """
    Save classification model to pickle file

    Input:model: validated classification model
        model_filepath: specified storage path
    """
    pickle.dump(model, open(model_filepath, 'wb'))

def main():
    if len(sys.argv) == 3:
        database_filepath, model_filepath = sys.argv[1:]
        print('Loading data...\n    DATABASE: {}'.format(database_filepath))
        X, Y, category_names = load_data(database_filepath)
        X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)

        print('Building model...')
        model = build_model()

        print('Training model...')
        model.fit(X_train, Y_train)

        print('Evaluating model...')
        evaluate_model(model, X_test, Y_test, category_names)

        print('Saving model...\n    MODEL: {}'.format(model_filepath))
        save_model(model, model_filepath)

        print('Trained model saved!')

    else:
        print('Please provide the filepath of the disaster messages database ' \
              'as the first argument and the filepath of the pickle file to ' \
              'save the model to as the second argument. \n\nExample: python ' \
              'train_classifier.py ../data/DisasterResponse.db classifier.pkl')

if __name__ == '__main__':
    main()


from monosat import *
import functools
import math
import os
import random
import random
import sys

print("begin encode");

seed = random.randint(1,100000)

random.seed(seed)
print("RandomSeed=" + str(seed))

filename="/tmp/test_bv"

filename = filename +  ".gnf"
Monosat().init("-decide-graph-bv -no-decide-theories -no-decide-graph-rnd   -lazy-maxflow-decisions -conflict-min-cut -conflict-min-cut-maxflow -reach-underapprox-cnf ")
Monosat().setOutputFile(open(filename,'w'))

g = Graph()

nodes = []
for n in range(4):
    nodes.append(g.addNode())

a = g.addEdge(0,1)
b = g.addEdge(1,2)
c = g.addEdge(2,3)
d = Var()
Assert(d==Not(c))
AssertLessThanPB((a,b,Not(d)),4);

mf = g.maxFlowGreaterOrEqualTo(0,3,1)
Assert(mf)

result=Solve()

print(str(result))
if result:
    print(a.value())
    print(b.value())
    print(c.value())

    print(g.getMaxFlow(mf))
    for (v,w,var,wt) in g.getAllEdges():
        #if  var in model:
        if var.value():
            print("%d->%d"%(v,w))


def descending_order(num):
    num_list = [int(x) for x in str(num)]
    num_list.sort(reverse=True)
    str_list = [str(x) for x in num_list]
    return int(''.join(str_list))

if __name__ == '__main__':
    number = 15
    print(descending_order(number))