# -*- coding: utf-8 -*-
"""xtyping = typing | typing_extensions | misc"""
from enum import Enum
from os import PathLike
from pathlib import Path
from typing import (
    IO,
    Any,
    AsyncIterable,
    AsyncIterator,
    Awaitable,
    Callable,
    Dict,
    Iterable,
    Iterator,
    List,
    Mapping,
    Optional,
    Sequence,
    Set,
    Tuple,
    TypeVar,
    Union,
)

from typing_extensions import Literal, ParamSpec

__all_typing__: Tuple[str, ...] = (
    "AbstractSet",
    "Any",
    "AnyStr",
    "AsyncContextManager",
    "AsyncGenerator",
    "AsyncIterable",
    "AsyncIterator",
    "Awaitable",
    "ByteString",
    "Callable",
    "ChainMap",
    "ClassVar",
    "Collection",
    "Container",
    "ContextManager",
    "Coroutine",
    "Counter",
    "DefaultDict",
    "Deque",
    "Dict",
    "FrozenSet",
    "Generator",
    "Generic",
    "Hashable",
    "IO",
    "ItemsView",
    "Iterable",
    "Iterator",
    "KeysView",
    "List",
    "Mapping",
    "MappingView",
    "MutableMapping",
    "MutableSequence",
    "MutableSet",
    "NamedTuple",
    "NewType",
    "Optional",
    "Reversible",
    "Sequence",
    "Set",
    "Sized",
    "SupportsAbs",
    "SupportsBytes",
    "SupportsComplex",
    "SupportsFloat",
    "SupportsInt",
    "SupportsRound",
    "TYPE_CHECKING",
    "Text",
    "Tuple",
    "Type",
    "TypeVar",
    "Union",
    "ValuesView",
    "cast",
    "get_type_hints",
    "no_type_check",
    "no_type_check_decorator",
    "overload",
)
__all_typing_extensions__: Tuple[str, ...] = (
    "Annotated",
    "AsyncContextManager",
    "AsyncGenerator",
    "AsyncIterable",
    "AsyncIterator",
    "Awaitable",
    "ChainMap",
    "ClassVar",
    "Concatenate",
    "ContextManager",
    "Coroutine",
    "Counter",
    "DefaultDict",
    "Deque",
    "Final",
    "IntVar",
    "Literal",
    "NewType",
    "NoReturn",
    "NotRequired",
    "OrderedDict",
    "ParamSpec",
    "ParamSpecArgs",
    "ParamSpecKwargs",
    "Protocol",
    "Required",
    "Self",
    "SupportsIndex",
    "TYPE_CHECKING",
    "Text",
    "Type",
    "TypeAlias",
    "TypeGuard",
    "TypeVarTuple",
    "TypedDict",
    "Unpack",
    "final",
    "get_args",
    "get_origin",
    "get_type_hints",
    "overload",
    "runtime",
    "runtime_checkable",
)

__all_typing_extensions_future__: Tuple[str, ...] = (
    "Buffer",
    "Doc",
    "LiteralString",
    "Never",
    "TypeAliasType",
    "assert_never",
    "assert_type",
    "clear_overloads",
    "dataclass_transform",
    "deprecated",
    "get_original_bases",
    "get_overloads",
    "is_typeddict",
    "override",
    "reveal_type",
    # These might not be future imports...?
    "BinaryIO",
    "ForwardRef",
    "Match",
    "Pattern",
    "TextIO",
    "get_protocol_members",
    "is_protocol",
)

__all_annotated_types__: Tuple[str, ...] = (
    "BaseMetadata",
    "Ge",
    "GroupedMetadata",
    "Gt",
    "Interval",
    "IsDigits",
    "Le",
    "Len",
    "LowerCase",
    "Lt",
    "MaxLen",
    "MinLen",
    "MultipleOf",
    "Predicate",
    "Timezone",
    "UpperCase",
)

#############
## Aliases ##
#############
D = Dict
Lit = Literal
L = Literal
Ls = List
Opt = Optional
U = Union
Seq = Sequence

###############
## LITERALLY ##
###############
TRUE = Literal[True]
FALSE = Literal[False]
ZERO = Literal[0]
ONE = Literal[1]

##########
## NONE ##
##########
null = Null = None.__class__
NoneType = None.__class__
NoneStr = Optional[str]
NoneBytes = Optional[bytes]
StrBytes = Union[str, bytes]
NoneStrBytes = Optional[StrBytes]

############
## NUMBER ##
############
Number = Union[float, int]  # float or int
Flint = Union[float, int]  # float or int

##############
## TypeVars ##
##############
T = TypeVar("T")  # Any type.
_T = TypeVar("_T")
KT = TypeVar("KT")  # Key type.
_KT = TypeVar("_KT")
VT = TypeVar("VT")  # Value type.
_VT = TypeVar("_VT")
KeyT = TypeVar("KeyT")  # Key type.
ValT = TypeVar("ValT")  # Value type.
KeyType = TypeVar("KeyType")  # Key type.
ValType = TypeVar("ValType")  # Value type.
El = TypeVar("El")
Element = TypeVar("Element")
R = TypeVar("R")
RT = TypeVar("RT")
_R = TypeVar("_R")
_RT = TypeVar("_RT")
ReturnT = TypeVar("ReturnT")
ReturnType = TypeVar("ReturnType")
N = TypeVar("N", float, int)
DT = TypeVar("DT")
_DT = TypeVar("_DT")
T_Retval = TypeVar("T_Retval")

###############
## ParamSpec ##
###############
P = ParamSpec("P")
PT = ParamSpec("PT")
T_ParamSpec = ParamSpec("T_ParamSpec")

########################
## Covariant TypeVars ##
########################
KT_co = TypeVar("KT_co", covariant=True)
T_co = TypeVar("T_co", covariant=True)  # Any type covariant containers.
V_co = TypeVar("V_co", covariant=True)  # Any type covariant containers.
VT_co = TypeVar("VT_co", covariant=True)  # Value type covariant containers.

_KT_co = TypeVar("_KT_co", covariant=True)
_T_co = TypeVar("_T_co", covariant=True)
_V_co = TypeVar("_V_co", covariant=True)
_VT_co = TypeVar("_VT_co", covariant=True)

############################
## Contravariant TypeVars ##
############################
T_contra = TypeVar("T_contra", contravariant=True)  # Ditto contravariant.
_T_contra = TypeVar("_T_contra", contravariant=True)  # Ditto contravariant.
VT_contra = TypeVar("VT_contra", contravariant=True)
_VT_contra = TypeVar("_VT_contra", contravariant=True)
KT_contra = TypeVar("KT_contra", contravariant=True)
_KT_contra = TypeVar("_KT_contra", contravariant=True)

class StringEnum(str, Enum):
    """String enum base class -- based on usage with pydantic"""

    ...

class StrEnum(StringEnum):
    """Alias for StringEnum"""

    ...

################
## Function-y ##
################
AnyCallable = Callable[..., Any]
AnyAsyncCallable = Callable[..., Awaitable[Any]]
FuncType = Callable[..., Any]
AsyncFuncType = Callable[..., Awaitable[Any]]
F = TypeVar("F", bound=AnyCallable)
FN = TypeVar("FN", bound=AnyCallable)
Fn = TypeVar("Fn", bound=AnyCallable)
AF = TypeVar("AF", bound=Awaitable[Any])
AFn = TypeVar("AFn", bound=Awaitable[Any])
AsyncFn = TypeVar("AsyncFn", bound=Awaitable[Any])

#################
## STDIO/STDIN ##
#################
STDIO = Union[None, int, bytes, IO[Any]]
STDIN = Union[bytes, str, None]

###########
## LISTS ##
###########
ListAny = List[Any]
ListT = List[T]
ListStr = List[str]
ListInt = List[int]
ListFloat = List[float]
ListNumber = List[Number]

############################
## LISTLESS AKA iterables ##
############################
IterableAny = Iterable[Any]
IterableT = Iterable[T]
IterableStr = Iterable[str]
IterableInt = Iterable[int]
IterableFloat = Iterable[float]
IterableNumber = Iterable[Number]

##########
## DICT ##
##########
DictAny = Dict[Any, Any]
DictStr = Dict[str, str]
DictInt = Dict[int, int]
DictFloat = Dict[float, float]
DictNumber = Dict[Number, Number]
DictAnyAny = Dict[Any, Any]
DictStrStr = Dict[str, str]
DictIntInt = Dict[int, int]
DictFloatFloat = Dict[float, float]
DictNumberNumber = Dict[Number, Number]
DictStrAny = Dict[str, Any]
DictStrInt = Dict[str, int]

#########
## SET ##
#########
SetAny = Set[Any]
SetT = Set[T]
SetStr = Set[str]
SetInt = Set[int]
SetFloat = Set[float]
SetNumber = Set[Number]

##########
## MISC ##
##########
IntStr = Union[int, str]
Bytes = Union[bytes, bytearray]
Txt = Union[bytes, str]
EnvMap = Union[Mapping[bytes, Txt], Mapping[str, Txt]]
AnyIterable = Union[Iterable[T], AsyncIterable[T]]
AnyIterator = Union[Iterator[T], AsyncIterator[T]]
AnyFunction = Union[Callable[..., R], Callable[..., Awaitable[R]]]
StrIntFloat = Union[str, float, int]
HrTime = Tuple[int, int]  # node/js hrtime type annotation

###############################
## Function type annotations ##
###############################
FsPath = Union[str, Path, PathLike]
FsPathLike = PathLike
EnvType = Union[Mapping[bytes, Txt], Mapping[str, Txt]]
CmdArgs = CmdArgsType = Union[bytes, str, Sequence[str], Sequence[FsPath]]

##############
## LISTLESS ##
##############
ArrShape = Tuple[int, ...]
ArrayShape = Tuple[int, ...]
ShapeType = Tuple[int, ...]
TupleStrs = Tuple[str, ...]
ListListStr = List[List[str]]
TupleStrStr = Tuple[str, str]

#########
## OPT ##
#########
OptionalInt = Optional[int]
OptInt = Optional[int]
OptionalStr = Optional[str]
OptStr = Optional[str]
OptionalFloat = Optional[float]
OptFloat = Optional[float]

##########
## JSON ##
##########
# Json friendly: Union[None, bool, int, float, str, List[Any], Dict[str, Any]]
JsonPrimitive = Union[None, bool, int, float, str]
Json = Union[Dict[str, "Json"], List["Json"], str, int, float, bool, None]
JsonT = Union[Dict[str, "JsonT"], List["JsonT"], str, int, float, bool, None]
JsonDictT = Dict[str, Any]
JsonListT = List[Any]
JsonObjT = Dict[str, Any]
JsonArrT = List[Any]

###################
## FROM TYPESHED ##
###################
StrPath = Union[str, PathLike]  # stable
BytesPath = Union[bytes, PathLike]  # stable
StrOrBytesPath = Union[str, bytes, PathLike]

OpenTextModeUpdating = Literal[
    "r+",
    "+r",
    "rt+",
    "r+t",
    "+rt",
    "tr+",
    "t+r",
    "+tr",
    "w+",
    "+w",
    "wt+",
    "w+t",
    "+wt",
    "tw+",
    "t+w",
    "+tw",
    "a+",
    "+a",
    "at+",
    "a+t",
    "+at",
    "ta+",
    "t+a",
    "+ta",
    "x+",
    "+x",
    "xt+",
    "x+t",
    "+xt",
    "tx+",
    "t+x",
    "+tx",
]
OpenTextModeWriting = Literal["w", "wt", "tw", "a", "at", "ta", "x", "xt", "tx"]
OpenTextModeReading = Literal[
    "r", "rt", "tr", "U", "rU", "Ur", "rtU", "rUt", "Urt", "trU", "tUr", "Utr"
]
OpenTextMode = Union[OpenTextModeUpdating, OpenTextModeWriting, OpenTextModeReading]
OpenBinaryModeUpdating = Literal[
    "rb+",
    "r+b",
    "+rb",
    "br+",
    "b+r",
    "+br",
    "wb+",
    "w+b",
    "+wb",
    "bw+",
    "b+w",
    "+bw",
    "ab+",
    "a+b",
    "+ab",
    "ba+",
    "b+a",
    "+ba",
    "xb+",
    "x+b",
    "+xb",
    "bx+",
    "b+x",
    "+bx",
]
OpenBinaryModeWriting = Literal["wb", "bw", "ab", "ba", "xb", "bx"]
OpenBinaryModeReading = Literal["rb", "br", "rbU", "rUb", "Urb", "brU", "bUr", "Ubr"]
OpenBinaryMode = Union[
    OpenBinaryModeUpdating, OpenBinaryModeReading, OpenBinaryModeWriting
]

#############
## __all__ ##
#############
__all__ = (
    "AF",
    "AFn",
    "AnyAsyncCallable",
    "AnyCallable",
    "AnyFunction",
    "AnyIterable",
    "AnyIterator",
    "ArrShape",
    "ArrayShape",
    "AsyncFn",
    "AsyncFuncType",
    "Bytes",
    "BytesPath",
    "CmdArgs",
    "CmdArgsType",
    "D",
    "DT",
    "DictAny",
    "DictAnyAny",
    "DictFloat",
    "DictFloatFloat",
    "DictInt",
    "DictIntInt",
    "DictNumber",
    "DictNumberNumber",
    "DictStr",
    "DictStrAny",
    "DictStrInt",
    "DictStrStr",
    "El",
    "Element",
    "Enum",
    "EnvMap",
    "EnvType",
    "F",
    "FALSE",
    "FN",
    "Flint",
    "Fn",
    "FsPath",
    "FsPathLike",
    "FuncType",
    "HrTime",
    "IntStr",
    "IterableAny",
    "IterableFloat",
    "IterableInt",
    "IterableNumber",
    "IterableStr",
    "IterableT",
    "Json",
    "JsonArrT",
    "JsonDictT",
    "JsonListT",
    "JsonObjT",
    "JsonPrimitive",
    "JsonT",
    "KT",
    "KT_co",
    "KT_contra",
    "KeyT",
    "KeyType",
    "L",
    "ListAny",
    "ListFloat",
    "ListInt",
    "ListListStr",
    "ListNumber",
    "ListStr",
    "ListT",
    "Lit",
    "Ls",
    "N",
    "NoneBytes",
    "NoneStr",
    "NoneStrBytes",
    "NoneType",
    "Null",
    "Number",
    "ONE",
    "OpenBinaryMode",
    "OpenBinaryModeReading",
    "OpenBinaryModeUpdating",
    "OpenBinaryModeWriting",
    "OpenTextMode",
    "OpenTextModeReading",
    "OpenTextModeUpdating",
    "OpenTextModeWriting",
    "Opt",
    "OptFloat",
    "OptInt",
    "OptStr",
    "OptionalFloat",
    "OptionalInt",
    "OptionalStr",
    "P",
    "PT",
    "Path",
    "PathLike",
    "R",
    "RT",
    "ReturnT",
    "ReturnType",
    "STDIN",
    "STDIO",
    "Seq",
    "SetAny",
    "SetFloat",
    "SetInt",
    "SetNumber",
    "SetStr",
    "SetT",
    "ShapeType",
    "StrBytes",
    "StrEnum",
    "StrIntFloat",
    "StrOrBytesPath",
    "StrPath",
    "StringEnum",
    "T",
    "TRUE",
    "T_ParamSpec",
    "T_Retval",
    "T_co",
    "T_contra",
    "TupleStrStr",
    "TupleStrs",
    "Txt",
    "U",
    "VT",
    "VT_co",
    "VT_contra",
    "V_co",
    "ValT",
    "ValType",
    "ZERO",
    "_DT",
    "_KT",
    "_KT_co",
    "_KT_contra",
    "_R",
    "_RT",
    "_T",
    "_T_co",
    "_T_contra",
    "_VT",
    "_VT_co",
    "_VT_contra",
    "_V_co",
    "__all_annotated_types__",
    "__all_shed__",
    "__all_typing__",
    "__all_typing_extensions__",
    "__all_typing_extensions_future__",
    "null",
)
__all_shed__ = __all__


import sqlite3
import csv
from datetime import datetime

from PyQt5.QtSql import QSqlDatabase, QSqlTableModel
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidgetItem
from recived import Ui_frm_recieved

NUM = set(['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'])
Default = 1

class Recieved(QMainWindow, Ui_frm_recieved):
    def __init__(self):
        super().__init__()
        self.data = datetime.today()
        self.database = sqlite3.connect('eat.db')
        self.datainfo = sqlite3.connect('info.db')
        self.curinfo = self.datainfo.cursor()
        self.cur = self.database.cursor()
        self.product = ''
        self.setupUi(self)
        self.btn_calc.clicked.connect(self.recived)
        self.btn_grp_product.buttonClicked.connect(self.kind)

    def kind(self, btn):
        if btn.text() == 'Мясо':
            self.product = 'meat'
        if btn.text() == 'Морепродукты':
            self.product = 'fish'
        if btn.text() == 'Молочные продукты':
            self.product = 'milk'
        if btn.text() == 'Фрукты и овощи':
            self.product = 'fruits'
        text = f'SELECT * FROM {self.product}'
        info = self.cur.execute(text).fetchall()
        self.tbl_info.setRowCount(len(info))
        for i, elem in enumerate(info):
            for j, val in enumerate(elem):
                self.tbl_info.setItem(i, j, QTableWidgetItem(str(val)))
        self.tbl_info.setColumnWidth(1, 100)

    def recived(self):
        eat = self.txt_eat.toPlainText().lower()
        gramm = self.txt_gramm.toPlainText()
        numtr = False
        if not eat:
            self.lbl_error.setText('Какой продукт-то?')
        for i in gramm:
            if i not in NUM:
                numtr = True
        if numtr or not gramm:
            gramm = 1
        else:
            gramm = int(gramm) / 100
        if not self.product:
            self.lbl_error.setText('Какой вид продукта Вы хотите посмотреть? Введите и ещё раз нажмите рассчитать')
            return 0
        text = f'SELECT Kkal FROM {self.product} WHERE Name = "{eat.lower()}"'
        info = self.cur.execute(text).fetchone()
        if not info:
            self.lbl_error.setText("Извините, но у нас нет такого продукта")
        else:
            self.lcd_gramm.display(info[0])
            self.lcd_recived.display(info[0] * gramm)
            self.tbl_info.setItem(0, 0, QTableWidgetItem(eat))
            self.tbl_info.setItem(0, 1, QTableWidgetItem(str(info[0])))
            data = datetime.now().date()
            search = self.curinfo.execute("""SELECT * FROM info WHERE DATA = ?""", (data,)).fetchone()
            if not search:
                self.curinfo.execute("""INSERT INTO info VALUES(?, ?, ?, ?, ?)""", (data, 0, 0, 0, 0))
                self.datainfo.commit()
            self.curinfo.execute("""UPDATE info
                           SET Recieved = Recieved + ?
                           WHERE DATA = ?""", (info[0] * gramm, data))
            self.datainfo.commit()


import time
import Clock as clk
import MessageHandler as msh
import pygame

from pygame.color import Color
import AllActors

from Location import*

BLUE = Color("blue")
START_TIME = time.monotonic()

class Engine:
    gameClock = 0.0
    deltaTime = 0.0

    def __init__(self):
        pygame.init()
        self.logo = pygame.image.load("Assets/Sprites/gem.png")
        pygame.display.set_icon(self.logo)
        pygame.display.set_caption("Coomer Game")
        self.screen = pygame.display.set_mode((1000, 700))

    def run(self):
        #
        speed = 100.0
        currentTime = 0.0
        lastTime = 0.0
        # init

        for person in AllActors.personList:
            person.EntityInit()

        running = True
        while running:
            clk.gameClock.Tick()
            clk.gameClock.Time()
            msh.msgHandler.UpdateMsgHandler()
            for person in AllActors.personList:
                person.EntityUpdate()
                person.stateMachine.GetStats()
                print("_____________________________________")

            # clock
            Engine.gameClock = time.monotonic() - START_TIME
            # EventHandler
            for event in pygame.event.get():
                # on Quit
                if event.type == pygame.QUIT:
                    running = False
            keys = pygame.key.get_pressed()
            # movement
            if keys[pygame.K_LEFT]:
                X = X - Engine.deltaTime * speed
            if keys[pygame.K_RIGHT]:
                X = X + Engine.deltaTime * speed
            if keys[pygame.K_UP]:
                Y = Y - Engine.deltaTime * speed
            if keys[pygame.K_DOWN]:
                Y = Y + Engine.deltaTime * speed
            # icon Switch
            if keys[pygame.K_1]:
                self.logo = pygame.image.load("Assets/Sprites/gem.png")
                pygame.display.set_icon(self.logo)
            if keys[pygame.K_2]:
                self.logo = pygame.image.load("Assets/Sprites/gem2.png")
                pygame.display.set_icon(self.logo)
            if keys[pygame.K_3]:
                self.logo = pygame.image.load("Assets/Sprites/gem3.png")
                pygame.display.set_icon(self.logo)
            if keys[pygame.K_4]:
                self.logo = pygame.image.load("Assets/Sprites/gem4.png")
                pygame.display.set_icon(self.logo)

            # Draw
            self.screen.fill(BLUE)
            #self.screen.blit(self.logo, (X, Y))
            pygame.display.flip()
            pygame.display.update()
            # clock
            currentTime = time.monotonic()
            Engine.deltaTime = currentTime - lastTime
            lastTime = currentTime


import requests
from tqdm import tqdm

for idx in tqdm(range(256), desc="Trying all 255 IPs"):
    ip = "192.168.0.%i" % idx
    r = requests.get(
        "https://cfta-wh01.allyourbases.co/admin.html", headers={"X-Forwarded-For": ip}
    )
    if len(r.text) > 0:
        print("IP '%s' returned content!" % ip)
        print(r.text)
        break


import json
import struct

class JsonSocket:

    def __init__(self, socket):
        if socket is None:
            raise Exception("JsonSocket received socket=None")

        self.socket = socket

    def send(self, jsonObj):
        print("dbug send: "+str(jsonObj))
        jsonObj = json.dumps(jsonObj)
        self.socket.sendall( struct.pack( ">i", len(jsonObj) ) )
        self.socket.sendall( jsonObj.encode() )
        print("Py -> (%d) %s" % (len(jsonObj), jsonObj))

    def sendResult(self, id, results):
        obj = {
            "id": id,
            "value": results
        }
        self.send( obj )

    def _recv(self,  n ):
        buf = bytes()
        while n > 0:
            chunk = self.socket.recv(n)
            n -= len(chunk)
            buf += chunk
        return buf

    def receive(self):
        msgLen = struct.unpack(">i",self._recv(4))[0]
        msg = self._recv(msgLen).decode()
        return json.loads(msg)

def isJsonSerializable(x):
    try:
        json.dumps(x)
        return True
    except (TypeError, OverflowError):
        return False


import socket
import scipy.signal
import numpy as np
import sounddevice as sd
from . import recorder
from scipy import signal
from threading import Thread
import time
import matplotlib.pyplot as plt

class ThreadWithReturnValue(Thread):
    def run(self):
        if self._target is not None:
            self._return = self._target(*self._args, **self._kwargs)

    def join(self):
        super(ThreadWithReturnValue,self).join()
        return self._return
class Receiver:
    def __init__(self,host='127.0.0.1',port = 2333):
        self.host = host
        self.port = int(port)

    def sendTime(self,time):
        send_msg = str(time)
        self.client.send(send_msg.encode('utf-8'))
        msg = self.client.recv(1024)
        print(f'receive msg : {msg.decode("utf-8")}')
    def getmsg(self):
        msg = self.client.recv(1024).decode('utf-8')
        if msg == 'server ready':
            self.client.send('client ready'.encode('utf-8'))
            return True
    def filter_bp(self,x,fs,wl,wh):
        fN = 3
        fc = fs/2
        w1c = wl/fc
        w2c = wh/fc
        b, a = signal.butter(fN,[w1c, w2c],'bandpass')
        x_filter = signal.filtfilt(b,a,x)
        return x_filter
    def main(self):
        print('receiver.main')
        self.client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.client.connect((self.host,self.port))
        fs = 48000
        T = 0.5
        f1 = 4000
        f2 = 6000
        f3 = 8000
        t = np.linspace(0,T,round(fs*T))
        y = scipy.signal.chirp(t,f2,T,f3)
        if (self.getmsg()):
            r = recorder.waveRecorder(fs,6*T,'receiver.wav')
            thread = ThreadWithReturnValue(target=r.saveWave)
            thread.start()
            time.sleep(3*T)
            sd.play(y, fs)
            data = thread.join()
            data = self.filter_bp(data.reshape(-1)[:int(6*T*fs)],fs,f1-10,f3+10)

            z1 = scipy.signal.chirp(t,f1,T,f2)
            z2 = scipy.signal.chirp(t,f2,T,f3)
            z1 = z1[::-1]
            z2 = z2[::-1]
            p1 = np.argmax(np.convolve(data,z1.reshape(-1),'valid'))
            p2 = np.argmax(np.convolve(data,z2.reshape(-1),'valid'))
            plt.plot(data)

            self.sendTime(p2-p1)
        self.client.close()


import tkinter as tk
from tkinter import Menu, filedialog, Canvas, PhotoImage, NW, Label, Text, Entry, simpledialog
from Image import Image
from pandas import DataFrame
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np

class GUI:
    root = tk.Tk()

    def __init__(self):
        self.root.title('Image processing')
        self.root.geometry("1700x850")
        menubar = Menu(self.root)
        self.root.config(menu=menubar)

        file_menu = Menu(menubar)

        file_menu.add_command(
            label='Open input',
            command=self.openInput
        )

        file_menu.add_command(
            label='Save output',
            command=self.saveOutput
        )

        menubar.add_cascade(
            label="File",
            menu=file_menu
        )

        operations_menu = Menu(menubar)

        operations_menu.add_command(
            label='histogram equalizer',
            command=self.histogram_equalizer
        )
        operations_menu.add_command(
            label='saturation transformation',
            command=self.saturation_transformation
        )

        operations_menu.add_command(
            label='generate salt & pepper noise',
            command=self.generate_random_noise
        )

        operations_menu.add_command(
            label='median filter',
            command=self.median_filter
        )

        operations_menu.add_command(
            label='average filter',
            command=self.average_filter
        )
        operations_menu.add_command(
            label='binarize ',
            command=self.binarize
        )
        operations_menu.add_command(
            label='dilation ',
            command=self.dilation
        )
        operations_menu.add_command(
            label='erosion ',
            command=self.erosion
        )

        operations_menu.add_command(
            label='opening ',
            command=self.opening
        )

        operations_menu.add_command(
            label='closing ',
            command=self.closing
        )
        menubar.add_cascade(
            label="Operations",
            menu=operations_menu
        )

        self.initInputCanvas()

        self.initOutputCanvas()
        self.root.mainloop()

    def initInputCanvas(self):

        self.inputCanvas = Canvas(self.root, width=500, height=400)
        Label(self.inputCanvas, text="INPUT").grid(column=1, row=1, sticky='w')
        self.inputCanvas.grid(column=1, row=1, sticky='w')
        self.inputInfo = Canvas(self.inputCanvas)

    def initOutputCanvas(self):

        self.outputCanvas = Canvas(self.root, width=500, height=400)
        Label(self.outputCanvas, text="OUTPUT").grid(
            column=1, row=1, sticky='w')
        self.outputCanvas.grid(column=1, row=2, sticky='w')
        self.outputInfo = Canvas(self.outputCanvas)

    def openInput(self):
        filename = filedialog.askopenfilename(
            initialdir="./samples", title="Select a File")
        self.inputImage = Image()
        self.inputImage.load_from_pgm(filename)
        self.inputCanvas.destroy()
        self.initInputCanvas()
        self.outputCanvas.destroy()
        self.initOutputCanvas()
        fig = plt.Figure(figsize=(4, 4), dpi=96)
        ax = fig.add_subplot(111)
        ax.imshow(self.inputImage.matrix, cmap='gray', vmin=0, vmax=self.inputImage.max_gray)
        ax.axis('off')
        canvas = FigureCanvasTkAgg(fig, self.inputCanvas)
        canvas.get_tk_widget().grid(column=1, row=2, sticky='w')
        canvas.draw()

        self.updateInfo('input')
        self.root.mainloop()

    def updateInfo(self, element):
        '''element is string can be 'input' or 'output'  '''
        if(element == 'input'):
            canvas = self.inputCanvas
            image = self.inputImage
            infoCanvas = self.inputInfo
        elif(element == 'output'):
            canvas = self.outputCanvas
            image = self.outputImage
            infoCanvas = self.outputInfo
        else:
            return

        infoCanvas.destroy()
        infoCanvas = Canvas(canvas)
        infoCanvas.grid(column=2, row=1, rowspan=2, sticky='n')
        Label(infoCanvas, text=f'height {image.height}').grid(
            column=1, row=1, sticky='w')
        Label(infoCanvas, text=f'width {image.width}').grid(
            column=1, row=2, sticky='w')
        Label(infoCanvas, text=f'average {image.average()}').grid(
            column=1, row=3, sticky='w')
        Label(infoCanvas, text=f'standard_deviation {image.standard_deviation()}').grid(
            column=1, row=4, sticky='w')
        if(element == "output"):
            Label(infoCanvas, text=f'signal to noise ratio {Image.signal_to_noise_ratio(self.inputImage,self.outputImage)}').grid(
                column=1, row=5, sticky='w')

        data = {
            'level': range(image.max_gray+1),
            'nb_pixels': image.histogram()
        }

        df = DataFrame(data, columns=['level', 'nb_pixels'])

        figure = plt.Figure(figsize=(5, 4), dpi=100)
        ax = figure.add_subplot(111)
        line = FigureCanvasTkAgg(figure, infoCanvas)
        line.get_tk_widget().grid(column=2, row=1, rowspan=4, sticky='w')
        df = df[['level', 'nb_pixels']].groupby('level').sum()
        df.plot(kind='line', legend=True, ax=ax, color='r', fontsize=10)
        ax.set_title('histogram')

        data = {
            'level': range(image.max_gray+1),
            'nb_pixels': image.cumulated_histogram()
        }

        df = DataFrame(data, columns=['level', 'nb_pixels'])

        figure = plt.Figure(figsize=(5, 4), dpi=100)
        ax = figure.add_subplot(111)
        line = FigureCanvasTkAgg(figure, infoCanvas)
        line.get_tk_widget().grid(column=3, row=1, rowspan=4, sticky='w')
        df = df[['level', 'nb_pixels']].groupby('level').sum()
        df.plot(kind='line', legend=True, ax=ax, color='r', fontsize=10)
        ax.set_title('cummulated histogram')

    def histogram_equalizer(self):
        self.outputImage = self.inputImage.histogram_equalizer()
        self.updateOutput()

    def updateOutput(self):

        self.outputCanvas.destroy()
        self.initOutputCanvas()
        fig = plt.Figure(figsize=(4, 4), dpi=96)
        ax = fig.add_subplot(111)
        ax.imshow(self.outputImage.matrix, cmap='gray', vmin=0, vmax=self.outputImage.max_gray)
        ax.axis('off')
        canvas = FigureCanvasTkAgg(fig, self.outputCanvas)
        canvas.get_tk_widget().grid(column=1, row=2, sticky='w')
        canvas.draw()

        self.updateInfo('output')
        self.root.mainloop()

    def saturation_transformation(self):

        answer = simpledialog.askstring(
            "Input",
            "provide saturation points in this form: x1 y1 x2 y2 x3 y3 ...",
            parent=self.root)
        answer = answer.split()
        saturation_points = []
        while(len(answer) != 0):
            saturation_points.append((int(answer.pop(0)), int(answer.pop(0))))

        self.outputImage = self.inputImage.saturation_transformation(
            saturation_points)
        self.updateOutput()
        return

    def generate_random_noise(self):
        self.outputImage = self.inputImage.generate_random_noise()
        self.updateOutput()

    def median_filter(self):

        answer = simpledialog.askstring(
            "Input",
            "provide the median filter size",
            parent=self.root)
        self.outputImage = self.inputImage.median_filter(int(answer))
        self.updateOutput()

    def average_filter(self):
        answer = simpledialog.askstring(
            "Input",
            "provide the average filter size",
            parent=self.root)
        self.outputImage = self.inputImage.average_filter(int(answer))
        self.updateOutput()

    def saveOutput(self):

        filename = filedialog.asksaveasfilename(
            initialdir="./samples", title="choose file location")
        self.outputImage.save_to_pgm(filename)
        return

    def binarize(self):
        threshold = simpledialog.askstring(
            "Input",
            "provide the threshold",
            parent=self.root)
        self.outputImage=self.inputImage.binarize(int(threshold))
        self.updateOutput()

    def dilation(self):

        size = simpledialog.askstring(
            "Input",
            "provide the dilation size",
            parent=self.root)
        self.outputImage=self.inputImage.dilation(int(size))
        self.updateOutput()

    def erosion(self):

        size = simpledialog.askstring(
            "Input",
            "provide the erosion size",
            parent=self.root)
        self.outputImage=self.inputImage.erosion(int(size))
        self.updateOutput()

    def closing(self):
        size = simpledialog.askstring(
            "Input",
            "provide the closing size",
            parent=self.root)
        self.outputImage=self.inputImage.dilation(int(size)).erosion(int(size))
        self.updateOutput()

    def opening(self):
        size = simpledialog.askstring(
            "Input",
            "provide the opening size",
            parent=self.root)
        self.outputImage=self.inputImage.erosion(int(size)).dilation(int(size))
        self.updateOutput()


from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('movies/', views.MoviesListView.as_view(), name='movies'),
    path('movie/<int:pk>', views.MovieDetailView.as_view(), name='movie-detail'),
    path('screenwriters/', views.ScreenwritersListView.as_view(), name='screenwriters'),
    path('screenwriter/<int:pk>', views.ScreenwriterDetailView.as_view(), name='screenwriter-detail'),
    path('directors/', views.DirectorsListView.as_view(), name='directors'),
    path('director/<int:pk>', views.DirectorDetailView.as_view(), name='director-detail'),
    path('mymovies/', views.LoanedMoviesByUserListView.as_view(), name='my-borrowed'),
    path('borrowed/', views.LoanedMoviesListView.as_view(), name='all-borrowed'),
    path('movie/<uuid:pk>/renew/', views.renew_movie_worker, name='renew-movie-worker'),
    path('screenwriter/create/', views.ScreenwriterCreate.as_view(), name='screenwriter-create'),
    path('screenwriter/<int:pk>/update/', views.ScreenwriterUpdate.as_view(), name='screenwriter-update'),
    path('screenwriter/<int:pk>/delete/', views.ScreenwriterDelete.as_view(), name='screenwriter-delete'),
    path('director/create/', views.DirectorCreate.as_view(), name='director-create'),
    path('director/<int:pk>/update/', views.DirectorUpdate.as_view(), name='director-update'),
    path('director/<int:pk>/delete/', views.DirectorDelete.as_view(), name='director-delete'),
    path('movie/create/', views.MovieCreate.as_view(), name='movie-create'),
    path('movie/<int:pk>/update/', views.MovieUpdate.as_view(), name='movie-update'),
    path('movie/<int:pk>/delete/', views.MovieDelete.as_view(), name='movie-delete'),
]


from pathlib import Path
import subprocess
import argparse
import sys
import os

def getStatisticFiles(config: dict, opts: argparse.Namespace) -> list:

    # Crate output data file list
    dList = []
    years = []
    counters = []

    # Get stage 0 dirs --> /FM/skim/6.0.0/v2/
    getDataDirsCommand = f"xrdfs {config['farmAddress']} ls {config['data_XRDFS_skimmed_path']}"
    if opts.verbose:
        print(f"Executing XRDFS command: {format(getDataDirsCommand)}")
    dataDirsOut = subprocess.run(
        getDataDirsCommand, shell=True, check=True, stdout=subprocess.PIPE)
    dataDirs = str.split(dataDirsOut.stdout.decode('utf-8').rstrip(), '\n')

    # Get stage 1 dirs --> /FM/FlightData/2A/YearOfAcquisition/
    for dir_st1 in dataDirs:
        if "20" in dir_st1 and "statistics" not in dir_st1:

            # Extract year
            year = int(dir_st1[dir_st1.rfind('/')+1:])
            if year < config['start_year'] or year > config['end_year']:
                continue
            if year not in years:
                years.append(year)
                year_data_idx = len(years)-1
                counters.append(0)

            getDataDirsCommand = f"xrdfs {config['farmAddress']} ls {dir_st1}"
            if opts.verbose:
                print(f"Executing XRDFS command: {getDataDirsCommand}")
            dataDirsOut = subprocess.run(
                getDataDirsCommand, shell=True, check=True, stdout=subprocess.PIPE)
            dataDirs_st1 = str.split(
                dataDirsOut.stdout.decode('utf-8').rstrip(), '\n')

            # Get stage 2 dirs --> /FM/FlightData/2A/YearOfAcquisition/MonthOfAcquisition
            for dir_st2 in dataDirs_st1:
                getDataDirsCommand = f"xrdfs {config['farmAddress']} ls {dir_st2}"
                if opts.verbose:
                    print(f"Executing XRDFS command: {getDataDirsCommand}")
                dataDirsOut = subprocess.run(
                    getDataDirsCommand, shell=True, check=True, stdout=subprocess.PIPE)
                dataDirs_st2 = str.split(
                    dataDirsOut.stdout.decode('utf-8').rstrip(), '\n')

                # Get ROOT data file
                for data_elm in dataDirs_st2:
                    if data_elm.endswith('.root') and "data_photon" not in data_elm:
                        dList.append(data_elm)
                        counters[year_data_idx] += 1

    if opts.verbose:
        print(f"{sum(counters)} data files have been read...")
        for year_idx, year in enumerate(years):
            print(f"{counters[year_idx]} data files found in {year} folder")

    return dList


#!/usr/bin/env python
import os
from datetime import date
from collections import OrderedDict
import pathlib

from pyexcel_ods import save_data
from pandas_ods_reader import read_ods

current_path = pathlib.Path(__file__).parent.resolve()

pcc_path = os.path.abspath(f'{current_path}/../reports/{date.today()}/pcc.ods')
print(f'Using PCC path: {pcc_path}')
assert os.path.isfile(pcc_path)

ens_path = os.path.abspath(f'{current_path}/../reports/{date.today()}/ensemble.ods')
print(f'Using ENS path: {ens_path}')
assert os.path.isfile(ens_path)

pcc_df = read_ods(pcc_path, 1, headers=True)
ens_df = read_ods(ens_path, 1, headers=True)

feature_values = {} # {name: [values]}

pcc_confidences = {}
ens_confidences = {}

pcc_values = {}
ens_values = {}

for row in pcc_df.to_dict(orient='records'):
    name = row['name']
    confidence = row['pcc_zero']
    best_value = row['best_value']
    feature_values.setdefault(name, [])
    feature_values[name].append(confidence)
    pcc_values[name] = best_value
    pcc_confidences[name] = confidence

for row in ens_df.to_dict(orient='records'):
    name = row['name']
    confidence = row['confidence']
    best_value = row['best value']
    if not best_value:
        confidence = 1 - confidence
    feature_values.setdefault(name, [])
    feature_values[name].append(confidence)
    ens_values[name] = best_value
    ens_confidences[name] = confidence

final_values = [] # [(score, name)]
for name, confidences in feature_values.items():
    mean_confidence = sum(confidences) / len(confidences)
    final_values.append((mean_confidence, name))
final_values.sort()
for name, conf in final_values:
    print(name, conf)

combine_fn = os.path.abspath(f'{current_path}/../reports/{date.today()}/combine-{date.today()}.ods')
print(f'Saving {combine_fn}.')
data = OrderedDict()
data.update({"Sheet 1": [
    ['name', 'score', 'pcc_conf', 'ens_conf', 'human_conf', 'agg_conf', 'pcc_value', 'ens_value']] + \
    [
        [name, conf, pcc_confidences.get(name, 0.5), ens_confidences.get(name, 0.5), 0.5,  f'=(C{i}+D{i}+E{i})/3',
        pcc_values.get(name, ''), ens_values.get(name, '')] for i, (conf, name) in enumerate(final_values, 2)]
             })
save_data(combine_fn, data)
print(f'Saved {combine_fn}.')


'''
Description: 职业百科信息收集   网站：https://xz.chsi.com.cn/occupation/index.action
Author: HCQ
Company(School): UCAS
Email: 1756260160@qq.com
Date: 2021-01-15 12:43:59
LastEditTime: 2021-01-19 00:41:34
FilePath: /Spider/practice/13职业百科信息搜集/职业百科信息搜集.py
'''
import requests
from bs4 import BeautifulSoup
from lxml import etree
import xlwt	# 存储
#导入线程池模块对应的类
from multiprocessing.dummy import Pool
import json
import re
import pandas as pd

def get_links():
    print("正在获取所有链接... ...")
    #UA伪装：将对应的User-Agent封装到一个字典中
    headers = {
        'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
    }
    url = 'https://xz.chsi.com.cn/ajax/occuindexqt.action'
    #对指定的url发起的请求对应的url是携带参数的，并且请求过程中处理了参数
    response = requests.post(url=url,headers=headers)
    response.encoding = 'utf-8'
    content = response.content
    # json格式转为字典
    result = json.loads(content)
    print(len(result))
    class_names = []
    names = []
    urls = []
    for i in range(len(result)):  # 23次
        # print("循环获取url  ... ..")
        info = result[i]["occInfos"]
        # print(len(info))
        for j in range(len(info)):  # 23次
            id = info[j]['id']
            name = info[j]['name']
            # print(id)
            urls.append(id)
            names.append(name)
            class_names.append(result[i]['ktname'])
    # print(urls)
    return urls, names, class_names
    # tree = etree.HTML(page_text)
    # tr_list = tree.xpath('//div[@class="zhiye"]')
    # print("获取链接：", len(tr_list))

def get_contents(ids):
    print("正在获取各职位数据... ...")
    #UA伪装：将对应的User-Agent封装到一个字典中
    headers = {
        'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
    }
    json_datas = []
    for id in ids:
        url = 'https://xz.chsi.com.cn/occupation/occudetail.action?id=' + id
        #对指定的url发起的请求对应的url是携带参数的，并且请求过程中处理了参数
        detail_page_text = requests.post(url=url,headers=headers).text
        # print(detail_page_text.replace(' ','').replace('\n', '').replace('\r', ''))
        # detail_page_text = detail_page_text.replace(' ','').replace('\n', '').replace('\r', '')
        # print(detail_page_text)
        # 正则re
        # ex = '"zhiyname":"(.*?)"'  # 得到销售代表的数据
        # json_data = re.findall(ex,detail_page_text, re.S)# 正则
        # ex = 'return.*?data: {"(.*?)所以平时要严格要求自己'  #
        ex = r'mixin = {.*?return.*?data: (.*?),\s*?alList'  #重点加上 r     <\/div>
        json_data = re.findall(ex,detail_page_text, re.S)# 正则
        # print(json_data[0]) # 获取到的json数据
        json_datas.append(json_data[0])
    return json_datas

    # BeautifulSoup
    # detail_soup = BeautifulSoup(detail_page_text,'lxml')
    # container = detail_soup.find('div',class_='xz-part-label')
    # print(container)
    # containers.append(container)

    # lxml
    # tree = etree.HTML(detail_page_text)
    # tr_list = tree.xpath('//div[@class="zhiye"]')
    # print("获取链接：", len(tr_list))
def save_csv(class_names, names, json_datas):
    print('保存csv文件...')
    #字典中的key值即为csv中列名
    dataframe = pd.DataFrame({'类别':class_names,'职业':names, "json数据":json_datas})
    #将DataFrame存储为csv,index表示是否显示行名，default=True
    dataframe.to_csv(r"职业百科信息搜集.csv",index=False, sep=',')

if __name__ == "__main__":
    urls, names, class_names = get_links()
    # print(class_names)
    # urls = ['rw6z9nza7gduikyw', 'dvbww41vhzdiqaq9']
    url = 'rw6z9nza7gduikyw'
    json_datas = get_contents(urls)
    save_csv(class_names, names, json_datas)

practice/13职业百科信息搜集/职业百科信息搜集.py


import os
from string import ascii_letters, digits, punctuation

import numpy as np
import requests

_char_decode = dict(enumerate(sorted(set(ascii_letters + digits + punctuation + " \n"))))
_char_encode = {char: i for i, char in _char_decode.items()}

def char_tknzr(txt: str):
    return [_char_encode[char] for char in txt if char in _char_encode]

DATA_PATH = os.path.join(os.path.dirname(__file__), "datasets", "shakespeare")

def get_shakespeare_data():
    """Inspired from https://github.com/karpathy/nanoGPT/"""
    raw_path = os.path.join(DATA_PATH, "raw.txt")
    train_path = os.path.join(DATA_PATH, f"train.npy")
    test_path = os.path.join(DATA_PATH, f"test.npy")

    # if path is not even there, download all data
    if not os.path.exists(DATA_PATH):
        print("Downloading raw Shakespeare texts")
        url = "https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt"
        os.makedirs(DATA_PATH, exist_ok=True)
        text = requests.get(url, timeout=60).text
        with open(raw_path, "w+", encoding="utf8") as f:
            f.write(text)

    # attempt to find cached version for current tokenizer
    if not os.path.exists(train_path) or not os.path.exists(test_path):
        print("Tokenizing Shakespeare texts")
        # load text
        with open(raw_path, encoding="utf8") as f:
            text = "".join(f.readlines())
        i = int(0.8*len(text))
        # encode text
        x = np.array(char_tknzr(text[:i]), dtype=np.uint16)
        x_test = np.array(char_tknzr(text[i:]), dtype=np.uint16)
        # map memory
        mem = np.memmap(train_path, dtype=np.uint16, mode="w+", shape=x.shape)
        mem[:] = x
        mem = np.memmap(test_path, dtype=np.uint16, mode="w+", shape=x_test.shape)
        mem[:] = x_test

    # at this point we know that the binfile was properly created so we load it
    return {"train": np.memmap(train_path, dtype=np.uint16, mode="r"),
            "val": np.memmap(test_path, dtype=np.uint16, mode="r")}


import re
from dataclasses import dataclass, field, fields
from typing import Optional

from omegaconf import II

from espresso.models.transformer.speech_transformer_config import SpeechEncoderConfig
from fairseq import utils
from fairseq.dataclass import ChoiceEnum, FairseqDataclass
from fairseq.models.transformer.transformer_config import (
    DEFAULT_MIN_PARAMS_TO_WRAP,
    QuantNoiseConfig,
    TransformerConfig,
)
from fairseq.utils import safe_getattr, safe_hasattr

DEFAULT_MAX_SOURCE_POSITIONS = 10240
DEFAULT_MAX_TARGET_POSITIONS = 1024

_NAME_PARSER = r"(decoder|encoder|quant_noise)_(.*)"

@dataclass
class SpeechDecoderConfig(FairseqDataclass):
    embed_path: Optional[str] = field(
        default=None, metadata={"help": "path to pre-trained decoder embedding"}
    )
    embed_dim: int = field(default=48, metadata={"help": "decoder embedding dimension"})
    hidden_size: int = field(default=320, metadata={"help": "decoder hidden size"})
    layers: int = field(default=3, metadata={"help": "num decoder layers"})
    residual: bool = field(
        default=False,
        metadata={
            "help": "create residual connections for rnn decoder "
            "layers (starting from the 2nd layer), i.e., the actual "
            "output of such layer is the sum of its input and output"
        },
    )
    dropout_in: Optional[float] = field(
        default=II("model.dropout"),
        metadata={"help": "dropout probability for decoder input embedding"},
    )
    dropout_out: Optional[float] = field(
        default=II("model.dropout"),
        metadata={"help": "dropout probability for decoder output"},
    )

@dataclass
class SpeechTransformerTransducerConfig(FairseqDataclass):
    activation_fn: ChoiceEnum(utils.get_available_activation_fns()) = field(
        default="relu",
        metadata={"help": "activation function to use"},
    )
    dropout: float = field(default=0.1, metadata={"help": "dropout probability"})
    attention_dropout: float = field(
        default=0.0, metadata={"help": "dropout probability for attention weights"}
    )
    activation_dropout: float = field(
        default=0.0,
        metadata={
            "help": "dropout probability after activation in FFN.",
            "alias": "--relu-dropout",
        },
    )
    adaptive_input: bool = field(
        default=False,
        metadata={"help": "if set, uses adaptive input"},
    )
    encoder: SpeechEncoderConfig = SpeechEncoderConfig()
    decoder: SpeechDecoderConfig = SpeechDecoderConfig()
    joint_dim: int = field(
        default=512, metadata={"help": "joint network output dimension"}
    )
    share_decoder_input_output_embed: bool = field(
        default=False, metadata={"help": "share decoder input and output embeddings"}
    )
    no_token_positional_embeddings: bool = field(
        default=False,
        metadata={
            "help": "if True, disables positional embeddings (outside self attention)"
        },
    )
    layernorm_embedding: bool = field(
        default=True, metadata={"help": "add layernorm to embedding"}
    )
    no_scale_embedding: bool = field(
        default=False, metadata={"help": "if True, dont scale embeddings"}
    )
    checkpoint_activations: bool = field(
        default=False,
        metadata={
            "help": "checkpoint activations at each layer, which saves GPU memory usage at the cost of some additional compute"
        },
    )
    offload_activations: bool = field(
        default=False,
        metadata={
            "help": "checkpoint activations at each layer, then save to gpu. Sets --checkpoint-activations."
        },
    )
    # args for Training with Quantization Noise for Extreme Model Compression ({Fan*, Stock*} et al., 2020)
    quant_noise: QuantNoiseConfig = field(default=QuantNoiseConfig())
    min_params_to_wrap: int = field(
        default=DEFAULT_MIN_PARAMS_TO_WRAP,
        metadata={
            "help": "minimum number of params for a layer to be wrapped with FSDP() when "
            "training with --ddp-backend=fully_sharded. Smaller values will "
            "improve memory efficiency, but may make torch.distributed "
            "communication less efficient due to smaller input sizes. This option "
            "is set to 0 (i.e., always wrap) when --checkpoint-activations or "
            "--offload-activations are passed."
        },
    )
    # DEPRECATED field, but some old checkpoints might have it
    relu_dropout: float = 0.0

    export: bool = field(
        default=False,
        metadata={"help": "make the layernorm exportable with torchscript."},
    )

    # options from other parts of the config
    max_source_positions: Optional[int] = II("task.max_source_positions")
    max_target_positions: Optional[int] = II("task.max_target_positions")
    tpu: bool = II("common.tpu")

    # We need to make this hierarchical dataclass like the flat namespace
    # __getattr__ and __setattr__ here allow backward compatibility
    # for subclasses of Transformer(Legacy) that depend on read/write on
    # the flat namespace.

    def __getattr__(self, name):
        match = re.match(_NAME_PARSER, name)
        if match:
            sub = safe_getattr(self, match[1])
            return safe_getattr(sub, match[2])
        raise AttributeError(f"invalid argument {name}.")

    def __setattr__(self, name, value):
        match = re.match(_NAME_PARSER, name)
        if match:
            sub = safe_getattr(self, match[1])
            setattr(sub, match[2], value)
        else:
            super().__setattr__(name, value)

    @staticmethod
    def _copy_keys(args, cls, prefix, seen):
        return TransformerConfig._copy_keys(args, cls, prefix, seen)

    @classmethod
    def from_namespace(cls, args):
        if args is None:
            return None
        if not isinstance(args, cls):
            seen = set()
            config = cls()
            # currently, we can go generically from DC fields to args hierarchically
            # but we can't easily deconstruct a flat namespace to a hierarchical
            # DC. Mostly because we could have a sub-dc called `decoder-foo` that should not
            # go to the sub struct called `decoder`. There are ways to go around this, but let's keep it simple
            # for now.
            for fld in fields(cls):
                # concretelly, the transformer_config know what sub-dc it has, so we go through all the dc fields
                # and if it's one that has a sub-dc, we build that sub-dc with `copy_keys()`
                if fld.name == "decoder":
                    if safe_hasattr(args, "decoder"):
                        #  in some cases, the args we receive is already structured (as DictConfigs), so let's just build the correct DC
                        seen.add("decoder")
                        config.decoder = SpeechDecoderConfig(**args.decoder)
                    else:
                        config.decoder = cls._copy_keys(
                            args, SpeechDecoderConfig, "decoder", seen
                        )
                elif fld.name == "encoder":
                    # same but for encoder
                    if safe_hasattr(args, "encoder"):
                        seen.add("encoder")
                        config.encoder = SpeechEncoderConfig(**args.encoder)
                    else:
                        config.encoder = cls._copy_keys(
                            args, SpeechEncoderConfig, "encoder", seen
                        )
                elif fld.name == "quant_noise":
                    # same but for quant_noise
                    if safe_hasattr(args, "quant_noise"):
                        seen.add("quant_noise")
                        config.quant_noise = QuantNoiseConfig(**args.quant_noise)
                    else:
                        config.quant_noise = cls._copy_keys(
                            args, QuantNoiseConfig, "quant_noise", seen
                        )
                elif safe_hasattr(args, fld.name):
                    # if it's not a structure field, it's just a normal field, copy it over
                    seen.add(fld.name)
                    setattr(config, fld.name, safe_getattr(args, fld.name))
            # we got all the fields defined in the dataclass, but
            # the argparse namespace might have extra args for two reasons:
            #   - we are in a legacy class so all the args are not declared in the dataclass. Ideally once everyone has defined a dataclass for their model, we won't need this
            #   - some places expect args to be there but never define them
            args_dict = (
                args._asdict()
                if safe_hasattr(args, "_asdict")
                else vars(args)
                if safe_hasattr(args, "__dict__")
                else {}
            )  # namedtupled doesn't have __dict__ :-/
            for key, value in args_dict.items():
                if key not in seen:
                    setattr(config, key, value)
            return config
        else:
            return args


import pygame as pg

def array_to_move(x, y):
    letters = ["a", "b", "c", "d", "e", "f", "g", "h"]
    move = ""
    x += 1
    y += 1
    move += letters[y]
    if x == 7:
        x = 1
    elif x == 6:
        x = 2
    elif x == 5:
        x = 3
    elif x == 3:
        x = 5
    elif x == 2:
        x = 6
    elif x == 1:
        x = 7
    elif x == 0:
        x = 8
    move += str(x)
    return move

def get_image_figure(name, resolution):
    resolution = resolution // 8
    img_name = 'slike/' + name + '.png'
    image = pg.image.load(img_name)
    image = pg.transform.scale(image, resolution)
    return image

def convert_coordinates_to_array(x, y, square_size):
    x = 0
    y = 0
    ss_1 = square_size
    ss_2 = 0
    for i in range(0, 8):
        if x <= ss_1:
            y = ss_2
            break
        else:
            ss_1 += square_size
            ss_2 += 1
    ss_1 = square_size
    ss_2 = 0
    for i in range(0, 8):
        if y <= ss_1:
            x = ss_2
            break
        else:
            ss_1 += square_size
            ss_2 += 1
    return x, y


# pylint: disable=missing-module-docstring
import os
from pathlib import Path
from textwrap import dedent

import yaml

ROOT_FOLDER = os.environ.get("DIRLISTER_TARGET", "/")

def load_filter() -> dict:
    """example filter.yaml:
    filters:
      - "[tag]"
      - "sausage"
      - 2020

    this filter would turn a file or directory that looks like this:
      "abc [tag] def sausage ghi 2020"
    into this:
      "abc def ghi"
    """
    filters = {}
    filter_file = ""

    filter_file = Path("filter.yaml")
    if not filter_file.exists():
        default_content = dedent(
            """
            filters:
              - "make a yaml list of things to filter from files and directory names"
            """
        ).lstrip()

        filter_file.write_text(default_content, encoding="utf-8")

    filter_raw = filter_file.read_text(encoding="utf-8")

    filters = yaml.safe_load(filter_raw)

    return filters


import datetime
import time

from oslo.config import cfg

from nova.cells import messaging
from nova.cells import state as cells_state
from nova.cells import utils as cells_utils
from nova import context
from nova import exception
from nova import manager
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
from nova.openstack.common import timeutils

cell_manager_opts = [
        cfg.StrOpt('driver',
                default='nova.cells.rpc_driver.CellsRPCDriver',
                help='Cells communication driver to use'),
        cfg.IntOpt("instance_updated_at_threshold",
                default=3600,
                help="Number of seconds after an instance was updated "
                        "or deleted to continue to update cells"),
        cfg.IntOpt("instance_update_num_instances",
                default=1,
                help="Number of instances to update per periodic task run")
]

LOG = logging.getLogger(__name__)

CONF = cfg.CONF
CONF.register_opts(cell_manager_opts, group='cells')

class CellsManager(manager.Manager):
    """The nova-cells manager class.  This class defines RPC
    methods that the local cell may call.  This class is NOT used for
    messages coming from other cells.  That communication is
    driver-specific.

    Communication to other cells happens via the messaging module.  The
    MessageRunner from that module will handle routing the message to
    the correct cell via the communications driver.  Most methods below
    create 'targeted' (where we want to route a message to a specific cell)
    or 'broadcast' (where we want a message to go to multiple cells)
    messages.

    Scheduling requests get passed to the scheduler class.
    """
    RPC_API_VERSION = '1.6'

    def __init__(self, *args, **kwargs):
        # Mostly for tests.
        cell_state_manager = kwargs.pop('cell_state_manager', None)
        super(CellsManager, self).__init__(*args, **kwargs)
        if cell_state_manager is None:
            cell_state_manager = cells_state.CellStateManager
        self.state_manager = cell_state_manager()
        self.msg_runner = messaging.MessageRunner(self.state_manager)
        cells_driver_cls = importutils.import_class(
                CONF.cells.driver)
        self.driver = cells_driver_cls()
        self.instances_to_heal = iter([])

    def post_start_hook(self):
        """Have the driver start its consumers for inter-cell communication.
        Also ask our child cells for their capacities and capabilities so
        we get them more quickly than just waiting for the next periodic
        update.  Receiving the updates from the children will cause us to
        update our parents.  If we don't have any children, just update
        our parents immediately.
        """
        # FIXME(comstud): There's currently no hooks when services are
        # stopping, so we have no way to stop consumers cleanly.
        self.driver.start_consumers(self.msg_runner)
        ctxt = context.get_admin_context()
        if self.state_manager.get_child_cells():
            self.msg_runner.ask_children_for_capabilities(ctxt)
            self.msg_runner.ask_children_for_capacities(ctxt)
        else:
            self._update_our_parents(ctxt)

    @manager.periodic_task
    def _update_our_parents(self, ctxt):
        """Update our parent cells with our capabilities and capacity
        if we're at the bottom of the tree.
        """
        self.msg_runner.tell_parents_our_capabilities(ctxt)
        self.msg_runner.tell_parents_our_capacities(ctxt)

    @manager.periodic_task
    def _heal_instances(self, ctxt):
        """Periodic task to send updates for a number of instances to
        parent cells.

        On every run of the periodic task, we will attempt to sync
        'CONF.cells.instance_update_num_instances' number of instances.
        When we get the list of instances, we shuffle them so that multiple
        nova-cells services aren't attempting to sync the same instances
        in lockstep.

        If CONF.cells.instance_update_at_threshold is set, only attempt
        to sync instances that have been updated recently.  The CONF
        setting defines the maximum number of seconds old the updated_at
        can be.  Ie, a threshold of 3600 means to only update instances
        that have modified in the last hour.
        """

        if not self.state_manager.get_parent_cells():
            # No need to sync up if we have no parents.
            return

        info = {'updated_list': False}

        def _next_instance():
            try:
                instance = self.instances_to_heal.next()
            except StopIteration:
                if info['updated_list']:
                    return
                threshold = CONF.cells.instance_updated_at_threshold
                updated_since = None
                if threshold > 0:
                    updated_since = timeutils.utcnow() - datetime.timedelta(
                            seconds=threshold)
                self.instances_to_heal = cells_utils.get_instances_to_sync(
                        ctxt, updated_since=updated_since, shuffle=True,
                        uuids_only=True)
                info['updated_list'] = True
                try:
                    instance = self.instances_to_heal.next()
                except StopIteration:
                    return
            return instance

        rd_context = ctxt.elevated(read_deleted='yes')

        for i in xrange(CONF.cells.instance_update_num_instances):
            while True:
                # Yield to other greenthreads
                time.sleep(0)
                instance_uuid = _next_instance()
                if not instance_uuid:
                    return
                try:
                    instance = self.db.instance_get_by_uuid(rd_context,
                            instance_uuid)
                except exception.InstanceNotFound:
                    continue
                self._sync_instance(ctxt, instance)
                break

    def _sync_instance(self, ctxt, instance):
        """Broadcast an instance_update or instance_destroy message up to
        parent cells.
        """
        if instance['deleted']:
            self.instance_destroy_at_top(ctxt, instance)
        else:
            self.instance_update_at_top(ctxt, instance)

    def schedule_run_instance(self, ctxt, host_sched_kwargs):
        """Pick a cell (possibly ourselves) to build new instance(s)
        and forward the request accordingly.
        """
        # Target is ourselves first.
        our_cell = self.state_manager.get_my_state()
        self.msg_runner.schedule_run_instance(ctxt, our_cell,
                                              host_sched_kwargs)

    def get_cell_info_for_neighbors(self, _ctxt):
        """Return cell information for our neighbor cells."""
        return self.state_manager.get_cell_info_for_neighbors()

    def run_compute_api_method(self, ctxt, cell_name, method_info, call):
        """Call a compute API method in a specific cell."""
        response = self.msg_runner.run_compute_api_method(ctxt,
                                                          cell_name,
                                                          method_info,
                                                          call)
        if call:
            return response.value_or_raise()

    def instance_update_at_top(self, ctxt, instance):
        """Update an instance at the top level cell."""
        self.msg_runner.instance_update_at_top(ctxt, instance)

    def instance_destroy_at_top(self, ctxt, instance):
        """Destroy an instance at the top level cell."""
        self.msg_runner.instance_destroy_at_top(ctxt, instance)

    def instance_delete_everywhere(self, ctxt, instance, delete_type):
        """This is used by API cell when it didn't know what cell
        an instance was in, but the instance was requested to be
        deleted or soft_deleted.  So, we'll broadcast this everywhere.
        """
        self.msg_runner.instance_delete_everywhere(ctxt, instance,
                                                   delete_type)

    def instance_fault_create_at_top(self, ctxt, instance_fault):
        """Create an instance fault at the top level cell."""
        self.msg_runner.instance_fault_create_at_top(ctxt, instance_fault)

    def bw_usage_update_at_top(self, ctxt, bw_update_info):
        """Update bandwidth usage at top level cell."""
        self.msg_runner.bw_usage_update_at_top(ctxt, bw_update_info)

    def sync_instances(self, ctxt, project_id, updated_since, deleted):
        """Force a sync of all instances, potentially by project_id,
        and potentially since a certain date/time.
        """
        self.msg_runner.sync_instances(ctxt, project_id, updated_since,
                                       deleted)

    def service_get_all(self, ctxt, filters):
        """Return services in this cell and in all child cells."""
        responses = self.msg_runner.service_get_all(ctxt, filters)
        ret_services = []
        # 1 response per cell.  Each response is a list of services.
        for response in responses:
            services = response.value_or_raise()
            for service in services:
                cells_utils.add_cell_to_service(service, response.cell_name)
                ret_services.append(service)
        return ret_services

    def service_get_by_compute_host(self, ctxt, host_name):
        """Return a service entry for a compute host in a certain cell."""
        cell_name, host_name = cells_utils.split_cell_and_item(host_name)
        response = self.msg_runner.service_get_by_compute_host(ctxt,
                                                               cell_name,
                                                               host_name)
        service = response.value_or_raise()
        cells_utils.add_cell_to_service(service, response.cell_name)
        return service

    def proxy_rpc_to_manager(self, ctxt, topic, rpc_message, call, timeout):
        """Proxy an RPC message as-is to a manager."""
        compute_topic = CONF.compute_topic
        cell_and_host = topic[len(compute_topic) + 1:]
        cell_name, host_name = cells_utils.split_cell_and_item(cell_and_host)
        response = self.msg_runner.proxy_rpc_to_manager(ctxt, cell_name,
                host_name, topic, rpc_message, call, timeout)
        return response.value_or_raise()

    def task_log_get_all(self, ctxt, task_name, period_beginning,
                         period_ending, host=None, state=None):
        """Get task logs from the DB from all cells or a particular
        cell.

        If 'host' is not None, host will be of the format 'cell!name@host',
        with '@host' being optional.  The query will be directed to the
        appropriate cell and return all task logs, or task logs matching
        the host if specified.

        'state' also may be None.  If it's not, filter by the state as well.
        """
        if host is None:
            cell_name = None
        else:
            cell_name, host = cells_utils.split_cell_and_item(host)
            # If no cell name was given, assume that the host name is the
            # cell_name and that the target is all hosts
            if cell_name is None:
                cell_name, host = host, cell_name
        responses = self.msg_runner.task_log_get_all(ctxt, cell_name,
                task_name, period_beginning, period_ending,
                host=host, state=state)
        # 1 response per cell.  Each response is a list of task log
        # entries.
        ret_task_logs = []
        for response in responses:
            task_logs = response.value_or_raise()
            for task_log in task_logs:
                cells_utils.add_cell_to_task_log(task_log,
                                                 response.cell_name)
                ret_task_logs.append(task_log)
        return ret_task_logs

    def compute_node_get(self, ctxt, compute_id):
        """Get a compute node by ID in a specific cell."""
        cell_name, compute_id = cells_utils.split_cell_and_item(
                compute_id)
        response = self.msg_runner.compute_node_get(ctxt, cell_name,
                                                    compute_id)
        node = response.value_or_raise()
        cells_utils.add_cell_to_compute_node(node, cell_name)
        return node

    def compute_node_get_all(self, ctxt, hypervisor_match=None):
        """Return list of compute nodes in all cells."""
        responses = self.msg_runner.compute_node_get_all(ctxt,
                hypervisor_match=hypervisor_match)
        # 1 response per cell.  Each response is a list of compute_node
        # entries.
        ret_nodes = []
        for response in responses:
            nodes = response.value_or_raise()
            for node in nodes:
                cells_utils.add_cell_to_compute_node(node,
                                                     response.cell_name)
                ret_nodes.append(node)
        return ret_nodes

    def compute_node_stats(self, ctxt):
        """Return compute node stats totals from all cells."""
        responses = self.msg_runner.compute_node_stats(ctxt)
        totals = {}
        for response in responses:
            data = response.value_or_raise()
            for key, val in data.iteritems():
                totals.setdefault(key, 0)
                totals[key] += val
        return totals

    def actions_get(self, ctxt, cell_name, instance_uuid):
        response = self.msg_runner.actions_get(ctxt, cell_name, instance_uuid)
        return response.value_or_raise()

    def action_get_by_request_id(self, ctxt, cell_name, instance_uuid,
                                 request_id):
        response = self.msg_runner.action_get_by_request_id(ctxt, cell_name,
                                                            instance_uuid,
                                                            request_id)
        return response.value_or_raise()

    def action_events_get(self, ctxt, cell_name, action_id):
        response = self.msg_runner.action_events_get(ctxt, cell_name,
                                                     action_id)
        return response.value_or_raise()

    def consoleauth_delete_tokens(self, ctxt, instance_uuid):
        """Delete consoleauth tokens for an instance in API cells."""
        self.msg_runner.consoleauth_delete_tokens(ctxt, instance_uuid)

    def validate_console_port(self, ctxt, instance_uuid, console_port,
                              console_type):
        """Validate console port with child cell compute node."""
        instance = self.db.instance_get_by_uuid(ctxt, instance_uuid)
        if not instance['cell_name']:
            raise exception.InstanceUnknownCell(instance_uuid=instance_uuid)
        response = self.msg_runner.validate_console_port(ctxt,
                instance['cell_name'], instance_uuid, console_port,
                console_type)
        return response.value_or_raise()


# -*- coding: utf-8 -*-
"""
@author: ctlek_000
"""

import os
import numpy
import random
import sklearn.model_selection
from tensorflow import keras

random.seed(20220602)
directory='C:/Users/leeby/Documents/work/NETL/hydrate/python/NGHP/morph'
os.chdir(directory)

#hyperparameters
runs=100
TV_split=.2
act_fun='relu'
net_top=[50,100]
drop_rate=[.1,.1]
learn_rate=.0025
bat_size=100
eps=500
wells=[5,6,7,8,9,10]

#open & read well log data
well=[]
for x in wells:
    file=open('data'+str(x)+'.txt','r')
    lines=file.readlines()
    file.close()
    for y in range(len(lines)):
        splitted=lines[y].split()
        floated=[float(z) for z in splitted]
        floated.append(x)
        well.append(floated)
well=numpy.array(well)

#features
depth=well[:,0]
GR=well[:,1]
Rt=well[:,2]
density=well[:,3]
porosity=well[:,4]
Vp=well[:,5]
Sgh=well[:,6]
morph=well[:,7]
well_num=well[:,8]

#well log combination + well number & depth
X=numpy.transpose([GR,Rt,density,porosity,Vp,well_num,depth])
#X=numpy.transpose([GR,Rt,density,porosity,well_num,depth])

#encode morph classes as 1-hot matrix
morph_2D=[]
for x in range(len(morph)):
    morph_2D.append([0.0,0.0,0.0])
    morph_2D[-1][int(morph[x])]=1
morph_2D=numpy.array(morph_2D)

second=max([numpy.count_nonzero(morph==1),numpy.count_nonzero(morph==2)])

#multiple runs with random splitting
best=0
loss_acc=[]
balanced=[]
precisions=[]
recalls=[]
F_scores=[]
con_mats=[]
all_well_con_mats=[]
all_well_balanced=[]
all_well_precisions=[]
all_well_recalls=[]
all_well_Fscores=[]
for x in range(runs):
    #balance 'none' class by limiting to 2nd highest amount
    morph2=morph
    morph2_2D=morph_2D
    X2=X
    while numpy.count_nonzero(morph2==0)>second:
        i=random.randrange(len(morph2))
        if morph2[i]==0:
            morph2=numpy.delete(morph2,i)
            morph2_2D=numpy.delete(morph2_2D,i,axis=0)
            X2=numpy.delete(X2,i,axis=0)

    #shuffle & split then save & delete well number & depth of test data
    x_train,x_test,y_train,y_test=sklearn.model_selection.train_test_split(X,morph_2D,test_size=TV_split)
    test_num=x_test[:,-2]
    test_depth=x_test[:,-1]
    x_train=numpy.delete(x_train,[-2,-1],axis=1)
    x_test=numpy.delete(x_test,[-2,-1],axis=1)

    #scale
    transformer=sklearn.preprocessing.MinMaxScaler().fit(x_train)
    x_train_norm=transformer.transform(x_train)
    x_test_norm=transformer.transform(x_test)

    #construct neural network
    keras.backend.clear_session()
    input1=keras.Input(shape=x_train_norm[0].shape,name='well_log_data')
    NN=keras.layers.Dense(units=net_top[0],activation=act_fun)(input1)
    NN=keras.layers.Dropout(drop_rate[0])(NN)
    for y in range(1,len(net_top)):
        NN=keras.layers.Dense(units=net_top[y],activation=act_fun)(NN)
        NN=keras.layers.Dropout(drop_rate[y])(NN)
    output1=keras.layers.Dense(units=3,activation='softmax',name='morphological_assignment')(NN)
    NN=keras.Model(inputs=input1,outputs=output1)

    #neural network options & compilation
    NN.compile(optimizer=keras.optimizers.Adam(learning_rate=learn_rate),loss=keras.losses.CategoricalCrossentropy(),metrics=[keras.metrics.CategoricalAccuracy()])

    #train & test
    validata=(x_test_norm,y_test)
    fitted=NN.fit(x_train_norm,y_train,batch_size=bat_size,epochs=eps,verbose=0,validation_data=validata)
    evaluated=NN.evaluate(x_test_norm,y_test,verbose=0)
    loss=fitted.history['loss']
    val_acc=fitted.history['val_categorical_accuracy']

    #predict
    y_pred=NN.predict(x_test_norm,verbose=0)
    y_pred_1D=numpy.argmax(y_pred,axis=1)
    y_test_1D=numpy.argmax(y_test,axis=1)

    #save & print
    loss_acc.append(evaluated)
    balanced.append(sklearn.metrics.balanced_accuracy_score(y_test_1D,y_pred_1D))
    precisions.append(sklearn.metrics.precision_score(y_test_1D,y_pred_1D,average=None))
    recalls.append(sklearn.metrics.recall_score(y_test_1D,y_pred_1D,average=None))
    F_scores.append(sklearn.metrics.f1_score(y_test_1D,y_pred_1D,average=None))
    con_mats.append(sklearn.metrics.confusion_matrix(y_test_1D,y_pred_1D))
    print('run',x+1)
    print('loss =',evaluated[0])
    print('accuracy =',evaluated[1])
    print('balanced accuracy =',balanced[-1])
    print('confusion matrix\n',con_mats[-1])
    print(sklearn.metrics.classification_report(y_test_1D,y_pred_1D,target_names=['none','fracture','pore']))

    #save best run
    if balanced[-1]>balanced[best]:
        NN.save(directory+'/saved')
        best=x
        best_transformer=transformer

        #save best run predictions & breakdown wrong pore recall by well & depths
        prediction=[]
        wrong_depths=[[],[],[],[],[],[]]
        for y in range(len(y_pred_1D)):
            prediction.append([max(y_pred[y]),y_pred_1D[y],y_test_1D[y],test_num[y],test_depth[y]])
            if y_pred_1D[y]==0 and y_test_1D[y]==2:
                wrong_depths[int(test_num[y])-5].append(test_depth[y])
        numpy.savetxt('loss.txt',loss,fmt='%.9f',delimiter='\t')
        numpy.savetxt('prediction.txt',prediction,fmt='%.9f',delimiter='\t')
        numpy.savetxt('val_acc.txt',val_acc,fmt='%.9f',delimiter='\t')
        numpy.savetxt('depths_of_pred_none_but_exp_pore.txt',wrong_depths,fmt='%s')

    #metrics for each well
    well_con_mats=[]
    well_precisions=[]
    well_recalls=[]
    well_Fscores=[]
    well_balanced=[]
    three_classes=True
    for y in wells:
        wellX=X[numpy.where(X[:,5]==y)]
        wellY=morph_2D[numpy.where(X[:,5]==y)]
        if y==10:
            #wellXY=numpy.concatenate((wellX,wellY),axis=1)
            wellX=wellX[wellY[:,-1]==0]
            wellY=wellY[wellY[:,-1]==0]
        wellX=numpy.delete(wellX,[-2,-1],axis=1)
        wellX_norm=transformer.transform(wellX)
        wellY_pred=NN.predict(wellX_norm,verbose=0)
        wellY_pred_1D=numpy.argmax(wellY_pred,axis=1)
        wellY_1D=numpy.argmax(wellY,axis=1)
        well_con_mats.append(sklearn.metrics.confusion_matrix(wellY_1D,wellY_pred_1D))
        well_balanced.append(sklearn.metrics.balanced_accuracy_score(wellY_1D,wellY_pred_1D))
        well_precisions.append(sklearn.metrics.precision_score(wellY_1D,wellY_pred_1D,average=None,zero_division=0))
        well_recalls.append(sklearn.metrics.recall_score(wellY_1D,wellY_pred_1D,average=None,zero_division=0))
        F=sklearn.metrics.f1_score(wellY_1D,wellY_pred_1D,average=None,zero_division=0)
        well_Fscores.append(F)
        if F.size != 3:
            three_classes=False
    if three_classes:
        all_well_con_mats.append(well_con_mats)
        all_well_balanced.append(well_balanced)
        all_well_precisions.append(well_precisions)
        all_well_recalls.append(well_recalls)
        all_well_Fscores.append(well_Fscores)
avg_well_con_mats=numpy.mean(all_well_con_mats,axis=0)
std_well_con_mats=numpy.std(all_well_con_mats,axis=0)
avg_well_balanced=numpy.mean(all_well_balanced,axis=0)
std_well_balanced=numpy.std(all_well_balanced,axis=0)
avg_well_precisions=numpy.mean(all_well_precisions,axis=0)
std_well_precisions=numpy.std(all_well_precisions,axis=0)
avg_well_recalls=numpy.mean(all_well_recalls,axis=0)
std_well_recalls=numpy.std(all_well_recalls,axis=0)
avg_well_Fscores=numpy.mean(all_well_Fscores,axis=0)
std_well_Fscores=numpy.std(all_well_Fscores,axis=0)

numpy.savetxt('loss_accuracy.txt',loss_acc,fmt='%.9f',delimiter='\t')
numpy.savetxt('balanced.txt',balanced,fmt='%.9f',delimiter='\t')
numpy.savetxt('precisions.txt',precisions,fmt='%.9f',delimiter='\t')
numpy.savetxt('recalls.txt',recalls,fmt='%.9f',delimiter='\t')
numpy.savetxt('F_scores.txt',F_scores,fmt='%.9f',delimiter='\t')

#print avg & stdev confusion matrix as well as best run stats
print('confusion matrix average\n',numpy.mean(numpy.array(con_mats),axis=0))
print('confusion matrix standard deviation\n',numpy.std(numpy.array(con_mats),axis=0))
print('best run',best+1)
print('loss =',loss_acc[best][0])
print('accuracy =',loss_acc[best][1])
print('balanced accuracy =',balanced[best])
print('confusion matrix\n',con_mats[best])

#test best on all
X_norm=best_transformer.transform(numpy.delete(X,[-2,-1],axis=1))
best_NN=keras.models.load_model(directory+'/saved')
pred=numpy.argmax(best_NN.predict(X_norm),axis=1)
print('applying best model to all data')
print(sklearn.metrics.confusion_matrix(morph,pred))
print(sklearn.metrics.classification_report(morph,pred,target_names=['none','fracture','pore']))


import sys
import os
from argparse import ArgumentParser
from argparse import ArgumentError
from contextlib import contextmanager
import unittest
from os.path import dirname
from os.path import join
from multiprocessing import Process
from subprocess import Popen
from subprocess import PIPE
from tempfile import mkdtemp

from explosive.fuse import ctrl

path = lambda p: join(dirname(__file__), 'data', p)

# mixture of unicode and str in python2.7 version of argparse means that
# default IO classes would be painful to use; use my own thing here.
class Input(object):

    def __init__(self, inputs):
        self.inputs = inputs.splitlines()

    def readline(self):
        if self.inputs:
            return self.inputs.pop(0)
        else:
            raise EOFError()

class Output(object):

    def __init__(self):
        self.items = []

    def write(self, s):
        self.items.append(s)

    def flush(self):
        pass

@contextmanager
def capture_stdio(inputs=''):
    dummy_in, dummy_out, dummy_err = Input(inputs), Output(), Output()
    curr_in, curr_out, curr_err = sys.stdin, sys.stdout, sys.stderr
    try:
        sys.stdin, sys.stdout, sys.stderr = dummy_in, dummy_out, dummy_err
        yield dummy_in, dummy_out, dummy_err
    finally:
        sys.stdin, sys.stdout, sys.stderr = curr_in, curr_out, curr_err

class PathMakerChoiceTestCase(unittest.TestCase):

    def test_argument_error(self):
        ap = ArgumentParser()
        action = ctrl._PathmakerChoiceStoreAction(None, 'dummy', 1)
        with self.assertRaises(ArgumentError) as cm:
            action(ap, ap, 'nothing', None)

    def test_argument_success(self):
        ap = ArgumentParser()
        action = ctrl._PathmakerChoiceStoreAction(None, 'dummy', 1)
        action(ap, ap, 'flatten', None)
        self.assertEqual(ap.dummy.__name__, 'flatten')

class IntegrationTestCase(unittest.TestCase):

    def test_simple(self):
        with capture_stdio() as stdio:
            with self.assertRaises(SystemExit):
                ctrl.main([])

    def test_layout_help(self):
        with capture_stdio() as stdio:
            with self.assertRaises(SystemExit):
                ctrl.main(['--layout-info'])
            # perhaps include actual equality check.

    def test_version(self):
        with capture_stdio() as stdio:
            with self.assertRaises(SystemExit):
                ctrl.main(['--version'])
            # perhaps include actual equality check.

    def test_failure(self):
        with capture_stdio() as stdio:
            with self.assertRaises(SystemExit):
                ctrl.main(['-d', '/tmp/to/no/such/dir', 'somezip.zip'])

    def test_failure_with_manager(self):
        with capture_stdio() as stdio:
            with self.assertRaises(SystemExit):
                ctrl.main(['-m', '-d', '/tmp/to/no/such/dir', 'somezip.zip'])

    def test_invalid_layout_choice(self):
        with capture_stdio() as stdio:
            in_, out, err = stdio
            with self.assertRaises(SystemExit):
                ctrl.main(['-l', 'nothing'])
            self.assertTrue(err.items[-1].endswith(
                "error: argument -l/--layout: "
                "invalid choice: 'nothing' (choose from "
                "'codepage', 'default', 'flatten', 'junk')\n",
            ))

    def test_invalid_layout_arg(self):
        with capture_stdio() as stdio:
            in_, out, err = stdio
            with self.assertRaises(SystemExit):
                ctrl.main(['-l', 'junk:hi'])
            self.assertTrue(err.items[-1].endswith(
                "error: argument -l/--layout: "
                "invalid argument to 'junk': 'keep' must be a number\n"
            ))

# Acceptance testing
# to enable, do something like:
# $ ACCEPTANCE=1 python setup.py test
#
# Disabling by default due to issues with getting this working reliably
# with other CI systems.

@unittest.skipUnless(os.environ.get('ACCEPTANCE'),
                     'skipping acceptance test by default')
class AcceptanceTestCase(unittest.TestCase):

    def setUp(self):
        self.mountpoint = mkdtemp()
        # maybe validate that this works?
        # subprocess.check_output(["fusermount", "-V"])

    def tearDown(self):
        sp = Popen(['fusermount', '-u', self.mountpoint],
                   stdout=PIPE, stderr=PIPE)
        result = sp.communicate()

    def test_success_basic(self):
        dummy1 = path('demo1.zip')
        dummy2 = path('demo2.zip')

        # This will terminate, so spawn a separate process.
        p = Process(
            target=ctrl.main, args=(['-d', self.mountpoint, dummy1, dummy2],))
        p.start()
        p.join()

        self.assertEqual(
            sorted(os.listdir(self.mountpoint)),
            ['demo1.zip', 'demo2.zip'],
        )

        self.assertEqual(
            sorted(os.listdir(join(self.mountpoint, 'demo1.zip'))),
            ['file1', 'file2', 'file3', 'file4', 'file5', 'file6']
        )

        self.assertEqual(
            sorted(os.listdir(join(self.mountpoint, 'demo2.zip'))),
            ['demo']
        )

        self.assertEqual(
            sorted(os.listdir(join(self.mountpoint, 'demo2.zip', 'demo'))),
            ['file1', 'file2', 'file3', 'file4', 'file5', 'file6']
        )

    def test_success_managed_default(self):
        dummy1 = path('demo1.zip')
        dummy2 = path('demo2.zip')

        # This will terminate, so spawn a separate process.
        p = Process(
            target=ctrl.main, args=(['-md', self.mountpoint, dummy1, dummy2],))
        p.start()
        p.join()

        self.assertEqual(
            sorted(os.listdir(self.mountpoint)),
            ['.manager', 'demo1.zip', 'demo2.zip'],
        )

        self.assertEqual(
            sorted(os.listdir(join(self.mountpoint, '.manager'))),
            ['demo1.zip', 'demo2.zip']
        )

        self.assertEqual(
            sorted(os.listdir(join(self.mountpoint, 'demo1.zip'))),
            ['file1', 'file2', 'file3', 'file4', 'file5', 'file6']
        )

        self.assertEqual(
            sorted(os.listdir(join(self.mountpoint, 'demo2.zip'))),
            ['demo']
        )

        self.assertEqual(
            sorted(os.listdir(join(self.mountpoint, 'demo2.zip', 'demo'))),
            ['file1', 'file2', 'file3', 'file4', 'file5', 'file6']
        )

        os.unlink(join(self.mountpoint, '.manager', 'demo1.zip'))
        self.assertEqual(
            sorted(os.listdir(self.mountpoint)),
            ['.manager', 'demo2.zip'],
        )

        os.symlink(dummy1, join(self.mountpoint, '.manager', 'archive.zip'))

        self.assertEqual(
            sorted(os.listdir(join(self.mountpoint, '.manager'))),
            ['archive.zip', 'demo2.zip']
        )

        self.assertEqual(
            sorted(os.listdir(self.mountpoint)),
            ['.manager', 'demo1.zip', 'demo2.zip'],
        )

        with self.assertRaises(OSError):
            os.symlink(dummy1, join(
                self.mountpoint, '.manager', 'alternate.zip'))


import logging

from openlp.core.lib import Plugin, StringContent, build_icon, translate
from openlp.core.lib.db import Manager
from openlp.plugins.custom.lib import CustomMediaItem, CustomTab
from openlp.plugins.custom.lib.db import CustomSlide, init_schema
from openlp.plugins.custom.lib.mediaitem import CustomSearch

log = logging.getLogger(__name__)

__default_settings__ = {
    'custom/db type': 'sqlite',
    'custom/db username': '',
    'custom/db password': '',
    'custom/db hostname': '',
    'custom/db database': '',
    'custom/last search type': CustomSearch.Titles,
    'custom/display footer': True,
    'custom/add custom from service': True
}

class CustomPlugin(Plugin):
    """
    This plugin enables the user to create, edit and display custom slide shows. Custom shows are divided into slides.
    Each show is able to have it's own theme.
    Custom shows are designed to replace the use of songs where the songs plugin has become restrictive.
    Examples could be Welcome slides, Bible Reading information, Orders of service.
    """
    log.info('Custom Plugin loaded')

    def __init__(self):
        super(CustomPlugin, self).__init__('custom', __default_settings__, CustomMediaItem, CustomTab)
        self.weight = -5
        self.db_manager = Manager('custom', init_schema)
        self.icon_path = ':/plugins/plugin_custom.png'
        self.icon = build_icon(self.icon_path)

    def about(self):
        about_text = translate('CustomPlugin', 'Custom Slide Plugin
The custom slide plugin '
                               'provides the ability to set up custom text slides that can be displayed on the screen '
                               'the same way songs are. This plugin provides greater freedom over the songs plugin.')
        return about_text

    def uses_theme(self, theme):
        """
        Called to find out if the custom plugin is currently using a theme.

        Returns True if the theme is being used, otherwise returns False.
        """
        if self.db_manager.get_all_objects(CustomSlide, CustomSlide.theme_name == theme):
            return True
        return False

    def rename_theme(self, old_theme, new_theme):
        """
        Renames a theme the custom plugin is using making the plugin use the new name.

        :param old_theme: The name of the theme the plugin should stop using.
        :param new_theme: The new name the plugin should now use.
        """
        customs_using_theme = self.db_manager.get_all_objects(CustomSlide, CustomSlide.theme_name == old_theme)
        for custom in customs_using_theme:
            custom.theme_name = new_theme
            self.db_manager.save_object(custom)

    def set_plugin_text_strings(self):
        """
        Called to define all translatable texts of the plugin
        """
        # Name PluginList
        self.text_strings[StringContent.Name] = {
            'singular': translate('CustomPlugin', 'Custom Slide', 'name singular'),
            'plural': translate('CustomPlugin', 'Custom Slides', 'name plural')
        }
        # Name for MediaDockManager, SettingsManager
        self.text_strings[StringContent.VisibleName] = {
            'title': translate('CustomPlugin', 'Custom Slides', 'container title')
        }
        # Middle Header Bar
        tooltips = {
            'load': translate('CustomPlugin', 'Load a new custom slide.'),
            'import': translate('CustomPlugin', 'Import a custom slide.'),
            'new': translate('CustomPlugin', 'Add a new custom slide.'),
            'edit': translate('CustomPlugin', 'Edit the selected custom slide.'),
            'delete': translate('CustomPlugin', 'Delete the selected custom slide.'),
            'preview': translate('CustomPlugin', 'Preview the selected custom slide.'),
            'live': translate('CustomPlugin', 'Send the selected custom slide live.'),
            'service': translate('CustomPlugin', 'Add the selected custom slide to the service.')
        }
        self.set_plugin_ui_text_strings(tooltips)

    def finalise(self):
        """
        Time to tidy up on exit
        """
        log.info('Custom Finalising')
        self.db_manager.finalise()
        Plugin.finalise(self)


#!/usr/bin/env python3

import sys
sys.dont_write_bytecode = True
import os, shutil, random, pickle, glob, operator, collections
import numpy, pandas
from configparser import ConfigParser
from collections import defaultdict
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences

def read_tokens(file_path, dropout=None):
  """Read n tokens from specified file"""

  tokens = open(file_path).read().split(' ')

  if dropout is not None:
    tokens_to_keep = round(len(tokens) * (1 - dropout))
    tokens = random.sample(tokens, tokens_to_keep)

  return tokens

class DatasetProvider:
  """Make x and y from raw data"""

  def __init__(
    self,
    train_dir,
    targ_file,
    model_dir,
    min_examples_per_targ):
    """Constructor"""

    # for converting input tokens to int sequences
    self.tokenizer = Tokenizer(
      num_words=None,
      oov_token='oovtok',
      lower=False)

    # encounter id -> set of targets
    self.enc2targs = defaultdict(set)

    # target -> int index
    self.targ2int = {}

    self.train_dir = train_dir
    self.targ_file = targ_file
    self.model_dir = model_dir
    self.min_examples_per_targ = min_examples_per_targ

    if os.path.isdir(model_dir):
      shutil.rmtree(model_dir)
    os.mkdir(model_dir)

    self.index_targets()

  def index_targets(self):
    """Process medication file"""

    df = pandas.read_csv(self.targ_file, dtype='str')

    for enc, drug in zip(df['HADM_ID'], df['DRUG']):
      self.enc2targs[enc].add(drug.lower())

    targ_counter = collections.Counter()
    for targs in self.enc2targs.values():
      targ_counter.update(targs)

    outfile = open(os.path.join(self.model_dir, 'targs.txt'), 'w')
    for med, count in targ_counter.most_common():
      outfile.write('%s|%s\n' % (med, count))

    # make alphabet for *frequent* targets
    index = 0
    for targ, count in targ_counter.items():
      if count > self.min_examples_per_targ:
        self.targ2int[targ] = index
        index = index + 1

  def load(self):
    """Process notes to make x and y"""

    x = [] # input documents (n_docs, max_seq_len)
    y = [] # targets we are predicting for each input

    for train_file in os.listdir(self.train_dir):

      targ_vec = numpy.zeros(len(self.targ2int))
      enc = train_file.split('.')[0]

      if enc not in self.enc2targs:
        continue

      no_labels_for_this_file = True
      for targ in self.enc2targs[enc]:
        if targ in self.targ2int:
          targ_vec[self.targ2int[targ]] = 1
          no_labels_for_this_file = False

      if no_labels_for_this_file:
        continue # all rare codes

      y.append(targ_vec)

      tokens = read_tokens(os.path.join(self.train_dir, train_file))
      x.append(' '.join(set(tokens)))

    self.tokenizer.fit_on_texts(x)
    pickle_file = open('Model/tokenizer.p', 'wb')
    pickle.dump(self.tokenizer, pickle_file)
    print('input vocabulary size:', len(self.tokenizer.word_index))

    x = self.tokenizer.texts_to_sequences(x)
    max_seq_len = max(len(seq) for seq in x)
    x = pad_sequences(x, maxlen=max_seq_len)

    return x, numpy.array(y)

if __name__ == "__main__":

  cfg = ConfigParser()
  cfg.read(sys.argv[1])
  base = os.environ['DATA_ROOT']

  dp = DatasetProvider(
    os.path.join(base, cfg.get('data', 'train')),
    os.path.join(base, cfg.get('data', 'targs')),
    cfg.get('data', 'model_dir'),
    cfg.getfloat('args', 'min_examples_per_targ'))

  x, y = dp.load()
  print('x:', x.shape)
  print('y:', y.shape)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import rospy
from sentry_kinematics_sim.msg import cmd_sentry
import sys, select, termios, tty
import pygame
import time
import math
msg = """
Control mrobot!
---------------------------
Moving around:
chassis:
   q    w    e
   a     s   d
yaw:
        i
    j   k   l

r/f : increase/decrease chassis speeds by 10%
t/g : increase/decrease yaw  speed by 10%
space key, k : force stop
anything else : stop smoothly

B to quit
"""

def getKey():
    tty.setraw(sys.stdin.fileno())
    rlist, _, _ = select.select([sys.stdin], [], [], 0.1)
    if rlist:
        key = sys.stdin.read(1)
    else:
        key = ''
    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
    return key

def vels(speed,turn):
    return "currently:\tspeed %s\tturn %s " % (speed,turn)

def maxlimit(num,max):
    if num > max:
        num = max
    elif num < -max:
        num = -max
    return num

def set_vel(speed,k1,k2,add,_add):
    if k1:
        speed = speed + add
    if k2:
        speed = speed - add
    if (k1 == 0)&(k2 == 0):
        if speed > 0.3:
            speed = speed - _add
        elif speed < -0.3:
            speed = speed + _add
        else:
            speed = 0
    return speed

def rad_format(rad):
    if rad > math.pi:
        while rad > math.pi:
            rad = rad - 2 * math.pi
    elif rad < math.pi:
        while rad < -math.pi:
            rad = rad + math.pi
    return rad

def set_pos(pos,k1,k2,add):
    if k1:
        pos = pos + add
    if k2:
        pos = pos - add
    pos = rad_format(pos)
    return pos

def set_pos1(pos,k1,k2,add):
    if k1:
        pos = add*10
    if k2:
        pos = -add*5
    if k1==0&k2==0:
        pos = 0
    return pos

if __name__=="__main__":

    #settings = termios.tcgetattr(sys.stdin)
    rospy.init_node('sentry_cmd_key')
    pub = rospy.Publisher('/cmd_sentry', cmd_sentry, queue_size=5)
    chassis_max_speed = 0.5
    chassi_max_wz = 1
    yaw_max_speed = 2
    chassis_speed_add = 0.05

    yaw_pos_add = 0.005
    pitch_pos_add = 0.005
    speed_jian = 0.1

    vx = 0
    vy = 0
    wz = 0
    yaw = 0
    pitch = 0
    cmd_sentry_msg = cmd_sentry()
    cmd_key = ['q','w','e','a','s','d','r','f','t','g','i','j','k','l']
    pygame.init()
    pygame.display.set_mode((100,100))
    try:
        print(msg)
        while(1):
            keys = pygame.key.get_pressed()

            if keys[pygame.K_r]:
                yaw_pos_add = yaw_pos_add * 1.1
            if keys[pygame.K_f]:
                yaw_pos_add = yaw_pos_add * 0.9

            if keys[pygame.K_t]:
                chassis_speed_add = chassis_speed_add * 1.1
            if keys[pygame.K_g]:
                chassis_speed_add = chassis_speed_add * 0.9

            vx = set_vel(vx,keys[pygame.K_w],keys[pygame.K_s],chassis_speed_add,speed_jian)
            vy = set_vel(vy,keys[pygame.K_a],keys[pygame.K_d],chassis_speed_add,speed_jian)
            wz = set_vel(wz,keys[pygame.K_q],keys[pygame.K_e],chassis_speed_add,speed_jian)
            yaw = set_pos1(yaw,keys[pygame.K_j],keys[pygame.K_l],yaw_pos_add)
            pitch = set_pos(pitch,keys[pygame.K_i],keys[pygame.K_k],pitch_pos_add)
            # break
            if keys[pygame.K_b]:
                break
            vx = maxlimit(vx,chassis_max_speed)
            vy = maxlimit(vy,chassis_max_speed)
            wz = maxlimit(wz,chassi_max_wz)
            cmd_sentry_msg.vx = vx
            cmd_sentry_msg.vy = vy
            cmd_sentry_msg.wz = wz
            cmd_sentry_msg.pitch_add = pitch
            cmd_sentry_msg.yaw_add = yaw
            pub.publish(cmd_sentry_msg)

            pygame.event.pump()
            time.sleep(0.01)
    except Exception as e:
        print (e)


from datetime import datetime, timedelta
from typing import Optional, List
from ninja import Schema, ModelSchema

from skii.platform.models.common import GeoCoordinate

class TimeRangeContract(Schema):
    start: datetime
    stop: datetime
    delta: Optional[timedelta]

class GeoCoordinateContract(ModelSchema):
    class Config:
        model = GeoCoordinate
        model_fields = ["latitude", "longitude"]

class GeoCoordinateSaveContract(ModelSchema):
    class Config:
        model = GeoCoordinate
        model_fields = ["latitude", "longitude"]

class CountryContract(Schema):
    code: str
    name: str
    flag: str

class CountrySaveContract(Schema):
    code: str

class VisualPictureContract(Schema):
    picture_url: str
    title: str
    description: str

class VisualElementContract(Schema):
    title: str
    description: str
    picture_url: str

class VisualAlbumContract(Schema):
    title: str
    description: str
    items: List[VisualElementContract]


from circuit_utils import circuit_simulator
import argparse
import re

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-b', '--bench', type=str, default='circuit.bench', help='input bench file')
    parser.add_argument('-v', '--verbose', type=bool, default=True, help='verbose simulator')
    parser.add_argument('-t', '--testvec', type=str, default=None, help='test vector')
    parser.add_argument('-f', '--fault', type=str, default=None, help='faulty node to be created (format: --fault=x=0)')
    args = parser.parse_args()

    simulator = circuit_simulator.CircuitSimulator(args)
    simulator.prompt()
    simulator.simulate()
    # while simulator.prompt():
    #     simulator.simulate()


import random
import string
import uuid
import threading
import mysql.connector

db = mysql.connector.connect(
    host="127.0.0.1",
    user="root",
    passwd="",
    database="test"
)

conn = db.cursor()
stringLength = 8
data = []

def randomString(stringLength):
    lettersAndDigits = string.ascii_letters + string.digits
    return ''.join(random.choice(lettersAndDigits) for i in range(stringLength))

def susun():
    kata = randomString(50)
    nilai = random.randint(0, 100)
    randoma = uuid.uuid4().hex
    ket = randoma.lower()[0:stringLength]
    isi_data = [kata,nilai,ket]
    data.append(isi_data)

for x in range(1000):
    t = threading.Thread(target=susun)
    t.start()
    t.join()
    # susun()

for isi in data:
    sql = "INSERT INTO t_test_threading(kata,nilai,ket) VALUES (%s,%s,%s)"
    data = (isi[0], isi[1], isi[2])
    conn.execute(sql, data)
db.commit()