import requests
import time
import random
import lxml.html
from lxml import etree
import threading
import re
import cfscrape

bbs = [
    {
        "origin": "https://ensaimada.xyz",
        "bbs": '43044'
    },
    {
        "origin": "https://ensaimada.xyz",
        "bbs": "40298"
    },
    {
        "origin": "https://inter-concierge.net",
        "bbs": "novogara"
    },
    {
        "origin": "https://inter-concierge.net",
        "bbs": "whitenighit"
    },
    {
        "origin": "https://ensaimada.xyz",
        "bbs": "rid"
    }
]
l = []

ua = open('ua.txt').readlines()

def fetchPosts ():
    for i in bbs:
        res = requests.get(f"{i['origin']}/{i['bbs']}/subback.html", headers={
            "User-Agent": random.choice(ua)
        }).text
        pattern = "https?://[\w/:%#\$&\?\(\)~\.=\+\-]+"
        url_list = re.findall(pattern, res)
        for url in url_list:
            if url.startswith(f"{i['origin']}/test/read.cgi/{i['bbs']}/"):
                l6 = url.split("/")[6]
                l.append({
                    "origin": i['origin'],
                    "bbs": i['bbs'],
                    "key": l6
                })

def getProxy ():
    """
    response = requests.get('https://www.proxyscan.io/download?type=https').text
    combos = response.splitlines()
    """
    f = open('proxies.txt')
    combos = f.readlines()
    for i in combos:
        proxyStr = random.choice(combos)
        try:
            requests.get("https://example.com", proxies={
                "http": "https://"+proxyStr,
                "https": "https://"+proxyStr
            }, timeout=5)
            print("Connection Successful"+proxyStr)
            return proxyStr
        except Exception as e:
            print(e)
    return getProxy()
def main(proxyStr):
    session = requests.Session()
    session = cfscrape.create_scraper()

    choice = random.choice(l)
    obj = {
    "subject": "",
    "bbs": choice['bbs'],
    "key": choice['key'],
    "time": time.time(),
    "submit": "",
    "FROM": "kusattapan",
    "mail": "",
    "MESSAGE": f"""
        [全恒心教民へ告ぐ]

        当掲示板は、荒らｓ連合軍ならびに私が運営するウェブサイトである「DropGC」によって占拠されました。

        よって当掲示板植民地化計画の一環として、当掲示板を利用する全ての恒心教民はDropGCへ利用登録することを命じる。

        これに従わない場合は、当掲示板へのアクセス禁止措置などが課される恐れがあります。

        こちらからご登録ください https://dropgc.gift?ref=kousin

        DropGC: https://dropgc.memo.wiki/
        荒らｓ連合軍:https://krsw-wiki.org/wiki/%E5%94%90%E6%BE%A4%E8%B2%B4%E6%B4%8BWiki:%E3%83%81%E3%83%A9%E3%82%B7%E3%81%AE%E8%A3%8F/%E8%8D%92%E3%82%89%E3%81%97%E9%80%A3%E5%90%88%E8%BB%8D
        {random.uniform(10000, 2000000000)}
    """.encode('shiftjis')
    }

    response = session.post(f"{choice['origin']}/test/bbs.cgi?guid=ON", data=obj, proxies={
                "http": "https://"+proxyStr,
                "https": "https://"+proxyStr
    }, headers={
    "User-Agent": random.choice(ua)
    })
    response.encoding = response.apparent_encoding
    html = response.text
    print(html)

    response = session.post(f"{choice['origin']}/test/bbs.cgi?guid=ON", data=obj, proxies={
                "http": "https://"+proxyStr,
                "https": "https://"+proxyStr
    },headers={
    "User-Agent": random.choice(ua)
    })
    response.encoding = response.apparent_encoding
    html = response.text
    print(html)

    tree = lxml.html.fromstring(html)
    errorStr = tree.xpath('//body/div')

    if len(errorStr):
        errorStr = tree.xpath('//body/div/text()')[0]
        # errorStr = etree.tostring(errorStr[0], method='html', encoding="shiftjis").decode('shiftjis')
        if len(errorStr.split()) >= 3 and errorStr.split()[3].isnumeric():
            wait = int(errorStr.split()[3])
            print('waiting for '+ str(wait))
            time.sleep(wait)

def worker ():
    while True:
        proxyStr = getProxy()
        for i in range(3):
            try:
                main(proxyStr)
            except KeyboardInterrupt:
                exit()
            except Exception as e:
                print(e)
            time.sleep(1)

fetchPosts()
print(l)
for i in range(100):
    t = threading.Thread(target=worker)
    t.start()
    print ("worker" + str(i) + "has started")


import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog
from PIL import Image, ImageTk
from modules.resource_path import path
from modules.single_instance import GetMutex
from modules.subprocess_args import subprocess_args
from interface.input import InFile
from interface.output import OutFile
from interface.options import Options
from interface.add_data import AddData
from interface.convert import Convert
from interface.info import Info
from interface.converting import Converting
from interface.progress_bar import ProgressBar
import os
import threading
import subprocess
import time
import shutil
import sys

class SplashFrame(tk.Frame):
    def __init__(self, master, **kwargs):
        super().__init__(master, bg=master["bg"], **kwargs)
        self.master = master
        self.bar = ProgressBar(self)
        self.checking_install = True
        self.getting_version = True
        self.checking_update = True
        self.error = False
        self.pack()

    def check_install(self):
        if subprocess.Popen("pyinstaller --version", **subprocess_args(True)).wait():
            # pyinstaller not install, check pip installed
            if subprocess.Popen("pip --version", **subprocess_args(True)).wait():
                # pip not install, check python installed
                if subprocess.Popen("python --version", **subprocess_args(True)).wait():
                    # python not install
                    mess = "Python not found!"
                else:
                    # python installed and pip not install
                    mess = "PIP not found!"
                messagebox.showerror(title="Error", message="Can't install pyinstaller\n" + mess)
                self.error = True
            else:
                # pip installed, install pyinstaller
                p = subprocess.Popen("pip install pyinstaller", **subprocess_args(True))
                _, errs = p.communicate()
                err = errs.decode()
                if err:
                    messagebox.showerror(title="Error", message="Can't install pyinstaller\n" + err)
                    self.error = True
        self.checking_install = False

    def get_version(self):
        global version
        v = subprocess.Popen("pyinstaller --version", **subprocess_args(True))
        outs, _ = v.communicate()
        version = outs.decode()[:-1]
        self.getting_version = False

    def check_update(self):
        global can_update
        p = subprocess.Popen("pip list --outdated", **subprocess_args(True))
        for outdated in p.stdout:
            if "pyinstaller" in outdated.decode():
                can_update = True
                break
        self.checking_update = False

    def run(self):
        ran = False
        value = 0
        threading.Thread(target=self.check_install).start()
        while not self.error and (value < 100 or self.checking_install or self.getting_version): # or self.checking_update
            time.sleep(0.01)
            if not self.checking_install and not ran:
                threading.Thread(target=self.get_version).start()
                # threading.Thread(target=self.checking_update).start()
                ran = True
            value += 1
            if self.checking_install:
                value = min(value, 49)
            if self.getting_version:
                value = min(value, 99)
            self.bar.set(value)
            self.update_idletasks()
            self.update()
        time.sleep(0.1)
        self.master.destroy()

class SplashScreen(tk.Tk):
    """Splash screen"""
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self["bg"] = "#18d1db"
        self.splash = SplashFrame(self)
        self.update_idletasks()
        x = self.winfo_screenwidth() // 2 - self.winfo_width() // 2
        y = self.winfo_screenheight() // 2 - self.winfo_height() * 3 // 5
        self.geometry(f"+{x}+{y}")
        self.overrideredirect(True)
        self.after(1000, self.splash.run)
        self.mainloop()

class MainFrame(tk.Frame):
    """Main GUI and connect all"""
    def __init__(self, master, **kwargs):
        super().__init__(master, bg=master["bg"], **kwargs)
        self.run_animation = tk.BooleanVar(value=True)
        self.name_file = ""
        self.path_in = ""
        self.path_out = ""
        self.path_icon = ""
        self.data = []
        self.infile = InFile(self)
        self.out = OutFile(self)
        windowed = ImageTk.PhotoImage(Image.open(os.path.join(path(), "add", "icon", "windowed.ico")).resize((32, 32)))
        console = ImageTk.PhotoImage(Image.open(os.path.join(path(), "add", "icon", "console.ico")).resize((32, 32)))
        self.option = Options(self, (windowed, console))
        self.add = AddData(self)
        self.convert = Convert(self)
        self.info = Info(self, version)
        self.converting = Converting(self)
        self.infile.set_command(self.on_click)
        self.infile.set_trace(self.on_change)
        self.convert.set_command(self.on_convert)
        self.pack()

    def on_click(self):
        in_path = filedialog.askopenfilename(title="Chose a python file",
                                             filetypes=(("Python file", ".py .pyw"), ("All file", "*.*")))
        if in_path:
            self.infile.set_path(os.path.normpath(in_path))
        self.on_change()

    def on_change(self):
        in_path = self.infile.get_path()
        if os.path.isfile(in_path):
            norm = os.path.normpath(in_path)
            self.infile.set_path(norm)
            self.out.set_path(os.path.dirname(norm))
            tail = os.path.splitext(os.path.basename(norm))[1]
            if tail == ".pyw":
                self.option.disable_noconsole()
            else:
                self.option.enable_noconsole()
            self.add.add_all(os.path.dirname(norm))
        else:
            self.add.remove_all()

    def check_ready(self):
        if not self.infile.get_path():
            return "The path of python file must not be empty!"
        if not self.out.get_path():
            return "The path of folder put your app must not ne empty!"
        return "ok"

    def on_convert(self):
        mess = self.check_ready()
        if mess != "ok":
            messagebox.showwarning(title="Convert py to exe", message=mess)
            return
        self.name_file = self.infile.get_name()
        self.path_in = self.infile.get_path()
        self.path_out = self.out.get_path()
        self.path_icon = self.option.get_path()
        self.data = self.add.get_add()
        self.convert.disable()
        self.convert.set_text("Converting...", color="#191cf7")
        threading.Thread(target=self.run).start()
        self.run_animation.set(value=True)
        self.converting.run_animation(self.run_animation)

    def run(self):
        command = self.create_command()
        print(command)
        p = subprocess.Popen(command, **subprocess_args(True))
        err = ""
        for e in p.stderr:
            err = e.decode()[:-1]
        if p.wait():
            self.convert.set_text("Convert Failed", color="#9e0000")
            self.run_animation.set(value=False)
            self.converting.convert_fail()
            messagebox.showerror(title="Convert py to exe", message="Convert failed\n" + err)
        else:
            self.convert.set_text("Successfully", color="#008f13")
            self.run_animation.set(value=False)
            self.converting.convert_suc()
            ok = messagebox.askyesno(title="Convert py to exe",
                                     message="Convert Successfully\nDo you want to delete .spec file")
            if ok:
                os.unlink(os.path.join(os.path.dirname(self.path_in), self.name_file + ".spec"))
        shutil.rmtree(os.path.join(path(), "build", self.name_file))
        self.convert.enable()

    def create_command(self):
        command = f'pyinstaller --clean --specpath "{os.path.dirname(self.path_in)}" --distpath "{self.path_out}" ' \
                  f'--workpath "{os.path.join(path(), "build")}" --name "{self.name_file}" '
        if self.path_icon:
            command += f'--icon="{self.path_icon}" '
        for data in self.data:
            path_data = os.path.join(os.path.dirname(self.path_in), data)
            if os.path.isfile(path_data):
                command += f'--add-data="{path_data};." '
            else:
                command += f'--add-data="{path_data};{data}" '
        if self.option.onfile():
            command += "--onefile "
        if self.option.noconsole():
            command += "--noconsole "
        if self.option.admin():
            command += "--uac-admin "
        command += f'"{self.infile.get_path()}"'
        return command

class MainScreen(tk.Tk):
    """main screen"""
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.title("Convert py to exe")
        self["bg"] = "#71c9ce"
        icon = os.path.join(path(), "add", "icon", "icon.ico")
        self.iconbitmap(icon)
        MainFrame(self)
        self.update_idletasks()
        x = self.winfo_screenwidth() // 2 - self.winfo_width() // 2
        y = self.winfo_screenheight() // 2 - self.winfo_height() * 3 // 5
        self.geometry(f"+{x}+{y}")
        self.minsize(self.winfo_width(), self.winfo_height())
        self.mainloop()

if __name__ == '__main__':
    app = GetMutex()
    if app.is_running():
        sys.exit(0)

    version = ""
    can_update = False
    SplashScreen()
    if version:
        MainScreen()


import sys
import ctypes

import pyglet
from pyglet.gl import *

_enable_vbo = pyglet.options['graphics_vbo']

# Enable workaround permanently if any VBO is created on a context that has
# this workaround.  (On systems with multiple contexts where one is
# unaffected, the workaround will be enabled unconditionally on all of the
# contexts anyway.  This is completely unlikely anyway).
_workaround_vbo_finish = False

def create_buffer(size, target=GL_ARRAY_BUFFER, usage=GL_DYNAMIC_DRAW, vbo=True):
    """Create a buffer of vertex data.

    :Parameters:
        `size` : int
            Size of the buffer, in bytes
        `target` : int
            OpenGL target buffer
        `usage` : int
            OpenGL usage constant
        `vbo` : bool
            True if a `VertexBufferObject` should be created if the driver
            supports it; otherwise only a `VertexArray` is created.

    :rtype: `AbstractBuffer`
    """
    from pyglet import gl
    if (vbo and
        gl_info.have_version(1, 5) and
        _enable_vbo and
        not gl.current_context._workaround_vbo):
        return VertexBufferObject(size, target, usage)
    else:
        return VertexArray(size)

def create_mappable_buffer(size, target=GL_ARRAY_BUFFER, usage=GL_DYNAMIC_DRAW, vbo=True):
    """Create a mappable buffer of vertex data.

    :Parameters:
        `size` : int
            Size of the buffer, in bytes
        `target` : int
            OpenGL target buffer
        `usage` : int
            OpenGL usage constant
        `vbo` : bool
            True if a :py:class:`VertexBufferObject` should be created if the driver
            supports it; otherwise only a :py:class:`VertexArray` is created.

    :rtype: :py:class:`AbstractBuffer` with :py:class:`AbstractMappable`
    """
    from pyglet import gl
    if (vbo and
        gl_info.have_version(1, 5) and
        _enable_vbo and
        not gl.current_context._workaround_vbo):
        return MappableVertexBufferObject(size, target, usage)
    else:
        return VertexArray(size)

class AbstractBuffer:
    """Abstract buffer of byte data.

    :Ivariables:
        `size` : int
            Size of buffer, in bytes
        `ptr` : int
            Memory offset of the buffer, as used by the ``glVertexPointer``
            family of functions
        `target` : int
            OpenGL buffer target, for example ``GL_ARRAY_BUFFER``
        `usage` : int
            OpenGL buffer usage, for example ``GL_DYNAMIC_DRAW``

    """

    ptr = 0
    size = 0

    def bind(self):
        """Bind this buffer to its OpenGL target."""
        raise NotImplementedError('abstract')

    def unbind(self):
        """Reset the buffer's OpenGL target."""
        raise NotImplementedError('abstract')

    def set_data(self, data):
        """Set the entire contents of the buffer.

        :Parameters:
            `data` : sequence of int or ctypes pointer
                The byte array to set.

        """
        raise NotImplementedError('abstract')

    def set_data_region(self, data, start, length):
        """Set part of the buffer contents.

        :Parameters:
            `data` : sequence of int or ctypes pointer
                The byte array of data to set
            `start` : int
                Offset to start replacing data
            `length` : int
                Length of region to replace

        """
        raise NotImplementedError('abstract')

    def map(self, invalidate=False):
        """Map the entire buffer into system memory.

        The mapped region must be subsequently unmapped with `unmap` before
        performing any other operations on the buffer.

        :Parameters:
            `invalidate` : bool
                If True, the initial contents of the mapped block need not
                reflect the actual contents of the buffer.

        :rtype: ``POINTER(ctypes.c_ubyte)``
        :return: Pointer to the mapped block in memory
        """
        raise NotImplementedError('abstract')

    def unmap(self):
        """Unmap a previously mapped memory block."""
        raise NotImplementedError('abstract')

    def resize(self, size):
        """Resize the buffer to a new size.

        :Parameters:
            `size` : int
                New size of the buffer, in bytes

        """

    def delete(self):
        """Delete this buffer, reducing system resource usage."""
        raise NotImplementedError('abstract')

class AbstractMappable:
    def get_region(self, start, size, ptr_type):
        """Map a region of the buffer into a ctypes array of the desired
        type.  This region does not need to be unmapped, but will become
        invalid if the buffer is resized.

        Note that although a pointer type is required, an array is mapped.
        For example::

            get_region(0, ctypes.sizeof(c_int) * 20, ctypes.POINTER(c_int * 20))

        will map bytes 0 to 80 of the buffer to an array of 20 ints.

        Changes to the array may not be recognised until the region's
        :py:meth:`AbstractBufferRegion.invalidate` method is called.

        :Parameters:
            `start` : int
                Offset into the buffer to map from, in bytes
            `size` : int
                Size of the buffer region to map, in bytes
            `ptr_type` : ctypes pointer type
                Pointer type describing the array format to create

        :rtype: :py:class:`AbstractBufferRegion`
        """
        raise NotImplementedError('abstract')

class VertexArray(AbstractBuffer, AbstractMappable):
    """A ctypes implementation of a vertex array.

    Many of the methods on this class are effectively no-op's, such as
    :py:meth:`bind`, :py:meth:`unbind`, :py:meth:`map`, :py:meth:`unmap` and
    :py:meth:`delete`; they exist in order to present
    a consistent interface with :py:class:`VertexBufferObject`.

    This buffer type is also mappable, and so :py:meth:`get_region` can be used.
    """

    def __init__(self, size):
        self.size = size

        self.array = (ctypes.c_byte * size)()
        self.ptr = ctypes.cast(self.array, ctypes.c_void_p).value

    def bind(self):
        pass

    def unbind(self):
        pass

    def set_data(self, data):
        ctypes.memmove(self.ptr, data, self.size)

    def set_data_region(self, data, start, length):
        ctypes.memmove(self.ptr + start, data, length)

    def map(self, invalidate=False):
        return self.array

    def unmap(self):
        pass

    def get_region(self, start, size, ptr_type):
        array = ctypes.cast(self.ptr + start, ptr_type).contents
        return VertexArrayRegion(array)

    def delete(self):
        pass

    def resize(self, size):
        array = (ctypes.c_byte * size)()
        ctypes.memmove(array, self.array, min(size, self.size))
        self.size = size
        self.array = array
        self.ptr = ctypes.cast(self.array, ctypes.c_void_p).value

class VertexBufferObject(AbstractBuffer):
    """Lightweight representation of an OpenGL VBO.

    The data in the buffer is not replicated in any system memory (unless it
    is done so by the video driver).  While this can improve memory usage and
    possibly performance, updates to the buffer are relatively slow.

    This class does not implement :py:class:`AbstractMappable`, and so has no
    :py:meth:`~AbstractMappable.get_region` method.  See
    :py:class:`MappableVertexBufferObject` for a VBO class
    that does implement :py:meth:`~AbstractMappable.get_region`.
    """

    def __init__(self, size, target, usage):
        self.size = size
        self.target = target
        self.usage = usage
        self._context = pyglet.gl.current_context

        id = GLuint()
        glGenBuffers(1, id)
        self.id = id.value
        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        glBindBuffer(target, self.id)
        glBufferData(target, self.size, None, self.usage)
        glPopClientAttrib()

        global _workaround_vbo_finish
        if pyglet.gl.current_context._workaround_vbo_finish:
            _workaround_vbo_finish = True

    def bind(self):
        glBindBuffer(self.target, self.id)

    def unbind(self):
        glBindBuffer(self.target, 0)

    def set_data(self, data):
        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        glBindBuffer(self.target, self.id)
        glBufferData(self.target, self.size, data, self.usage)
        glPopClientAttrib()

    def set_data_region(self, data, start, length):
        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        glBindBuffer(self.target, self.id)
        glBufferSubData(self.target, start, length, data)
        glPopClientAttrib()

    def map(self, invalidate=False):
        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        glBindBuffer(self.target, self.id)
        if invalidate:
            glBufferData(self.target, self.size, None, self.usage)
        ptr = ctypes.cast(glMapBuffer(self.target, GL_WRITE_ONLY),
                          ctypes.POINTER(ctypes.c_byte * self.size)).contents
        glPopClientAttrib()
        return ptr

    def unmap(self):
        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        glUnmapBuffer(self.target)
        glPopClientAttrib()

    def __del__(self):
        try:
            if self.id is not None:
                self._context.delete_buffer(self.id)
        except:
            pass

    def delete(self):
        id = GLuint(self.id)
        glDeleteBuffers(1, id)
        self.id = None

    def resize(self, size):
        # Map, create a copy, then reinitialize.
        temp = (ctypes.c_byte * size)()

        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        glBindBuffer(self.target, self.id)
        data = glMapBuffer(self.target, GL_READ_ONLY)
        ctypes.memmove(temp, data, min(size, self.size))
        glUnmapBuffer(self.target)

        self.size = size
        glBufferData(self.target, self.size, temp, self.usage)
        glPopClientAttrib()

class MappableVertexBufferObject(VertexBufferObject, AbstractMappable):
    """A VBO with system-memory backed store.

    Updates to the data via :py:meth:`set_data`, :py:meth:`set_data_region` and
    :py:meth:`map` will be held in local memory until :py:meth:`bind` is
    called.  The advantage is that fewer OpenGL calls are needed, increasing
    performance.

    There may also be less performance penalty for resizing this buffer.

    Updates to data via :py:meth:`map` are committed immediately.
    """

    def __init__(self, size, target, usage):
        super(MappableVertexBufferObject, self).__init__(size, target, usage)
        self.data = (ctypes.c_byte * size)()
        self.data_ptr = ctypes.cast(self.data, ctypes.c_void_p).value
        self._dirty_min = sys.maxsize
        self._dirty_max = 0

    def bind(self):
        # Commit pending data
        super(MappableVertexBufferObject, self).bind()
        size = self._dirty_max - self._dirty_min
        if size > 0:
            if size == self.size:
                glBufferData(self.target, self.size, self.data, self.usage)
            else:
                glBufferSubData(self.target, self._dirty_min, size,
                                self.data_ptr + self._dirty_min)
            self._dirty_min = sys.maxsize
            self._dirty_max = 0

    def set_data(self, data):
        super(MappableVertexBufferObject, self).set_data(data)
        ctypes.memmove(self.data, data, self.size)
        self._dirty_min = 0
        self._dirty_max = self.size

    def set_data_region(self, data, start, length):
        ctypes.memmove(self.data_ptr + start, data, length)
        self._dirty_min = min(start, self._dirty_min)
        self._dirty_max = max(start + length, self._dirty_max)

    def map(self, invalidate=False):
        self._dirty_min = 0
        self._dirty_max = self.size
        return self.data

    def unmap(self):
        pass

    def get_region(self, start, size, ptr_type):
        array = ctypes.cast(self.data_ptr + start, ptr_type).contents
        return VertexBufferObjectRegion(self, start, start + size, array)

    def resize(self, size):
        data = (ctypes.c_byte * size)()
        ctypes.memmove(data, self.data, min(size, self.size))
        self.data = data
        self.data_ptr = ctypes.cast(self.data, ctypes.c_void_p).value

        self.size = size
        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        glBindBuffer(self.target, self.id)
        glBufferData(self.target, self.size, self.data, self.usage)
        glPopClientAttrib()

        self._dirty_min = sys.maxsize
        self._dirty_max = 0

class AbstractBufferRegion:
    """A mapped region of a buffer.

    Buffer regions are obtained using :py:meth:`~AbstractMappable.get_region`.

    :Ivariables:
        `array` : ctypes array
            Array of data, of the type and count requested by
            :py:meth:`~AbstractMappable.get_region`.

    """

    def invalidate(self):
        """Mark this region as changed.

        The buffer may not be updated with the latest contents of the
        array until this method is called.  (However, it may not be updated
        until the next time the buffer is used, for efficiency).
        """
        pass

class VertexBufferObjectRegion(AbstractBufferRegion):
    """A mapped region of a VBO."""

    def __init__(self, buffer, start, end, array):
        self.buffer = buffer
        self.start = start
        self.end = end
        self.array = array

    def invalidate(self):
        buffer = self.buffer
        buffer._dirty_min = min(buffer._dirty_min, self.start)
        buffer._dirty_max = max(buffer._dirty_max, self.end)

class VertexArrayRegion(AbstractBufferRegion):
    """A mapped region of a vertex array.

    The :py:meth:`~AbstractBufferRegion.invalidate` method is a no-op but is
    provided in order to present a consistent interface with
    :py:meth:`VertexBufferObjectRegion`.
    """

    def __init__(self, array):
        self.array = array

class IndirectArrayRegion(AbstractBufferRegion):
    """A mapped region in which data elements are not necessarily contiguous.

    This region class is used to wrap buffer regions in which the data
    must be accessed with some stride.  For example, in an interleaved buffer
    this region can be used to access a single interleaved component as if the
    data was contiguous.
    """

    def __init__(self, region, size, component_count, component_stride):
        """Wrap a buffer region.

        Use the `component_count` and `component_stride` parameters to specify
        the data layout of the encapsulated region.  For example, if RGBA
        data is to be accessed as if it were packed RGB, ``component_count``
        would be set to 3 and ``component_stride`` to 4.  If the region
        contains 10 RGBA tuples, the ``size`` parameter is ``3 * 10 = 30``.

        :Parameters:
            `region` : `AbstractBufferRegion`
                The region with interleaved data
            `size` : int
                The number of elements that this region will provide access to.
            `component_count` : int
                The number of elements that are contiguous before some must
                be skipped.
            `component_stride` : int
                The number of elements of interleaved data separating
                the contiguous sections.

        """
        self.region = region
        self.size = size
        self.count = component_count
        self.stride = component_stride
        self.array = self

    def __repr__(self):
        return 'IndirectArrayRegion(size=%d, count=%d, stride=%d)' % (
            self.size, self.count, self.stride)

    def __getitem__(self, index):
        count = self.count
        if not isinstance(index, slice):
            elem = index // count
            j = index % count
            return self.region.array[elem * self.stride + j]

        start = index.start or 0
        stop = index.stop
        step = index.step or 1
        if start < 0:
            start = self.size + start
        if stop is None:
            stop = self.size
        elif stop < 0:
            stop = self.size + stop

        assert step == 1 or step % count == 0, "Step must be multiple of component count"

        data_start = (start // count) * self.stride + start % count
        data_stop = (stop // count) * self.stride + stop % count
        data_step = step * self.stride

        #  TODO stepped getitem is probably wrong, see setitem for correct.
        value_step = step * count

        value = [0] * ((stop - start) // step)
        stride = self.stride
        for i in range(count):
            value[i::value_step] = self.region.array[data_start + i:data_stop + i:data_step]
        return value

    def __setitem__(self, index, value):
        count = self.count
        if not isinstance(index, slice):
            elem = index // count
            j = index % count
            self.region.array[elem * self.stride + j] = value
            return

        start = index.start or 0
        stop = index.stop
        step = index.step or 1
        if start < 0:
            start = self.size + start
        if stop is None:
            stop = self.size
        elif stop < 0:
            stop = self.size + stop

        assert step == 1 or step % count == 0, "Step must be multiple of component count"

        data_start = (start // count) * self.stride + start % count
        data_stop = (stop // count) * self.stride + stop % count

        if step == 1:
            data_step = self.stride
            value_step = count
            for i in range(count):
                self.region.array[data_start + i:data_stop + i:data_step] = value[i::value_step]
        else:
            data_step = (step // count) * self.stride
            self.region.array[data_start:data_stop:data_step] = value

    def invalidate(self):
        self.region.invalidate()


#!/usr/bin/env python
# coding: utf-8

# In[63]:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
x_data = np.loadtxt('X.dat')
y_data = np.loadtxt('y.dat')
#x_data =  pd.read_csv('x.csv', sep=' ', header=None)
#y_data = pd.read_csv('y.dat', header=None)

# In[64]:

reg = LogisticRegression(penalty='none', fit_intercept=False).fit(x_data, y_data)
reg.score(x_data, y_data)
reg.coef_
reg.intercept_
#reg.predict(np.array([[3, 5]]))
print(reg.score(x_data, y_data))
print(reg.coef_)
print(reg.coef_[0][1])
print(reg.intercept_)
w1 = reg.coef_[0][0]
w2 = reg.coef_[0][1]

# In[65]:

# Accuracy of the scikit learn model
print(reg.score(x_data, y_data))

# In[66]:

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

# In[67]:

## Logistic Regression with SSE
#l_sse=sum((y_data-sigmoid(x_data.dot(w[i,:]))**2))

y_data[y_data == -1] = 0
y_data=y_data.reshape((400,1))

epoch = 100
lr = 0.001
w = np.zeros((epoch,2))
w[0,:] = [1,-1]

for i in range(epoch-1):
    lsw = sigmoid(np.dot(w[i,:],x_data.T)).reshape((400,1))
    dif_l_sse = sum(
        2*(y_data-lsw)
        *(-lsw)
        *(1-lsw)
        *x_data
)
    w[i+1] = w[i] - lr * dif_l_sse

# In[68]:

plt.figure(figsize=(17, 5))
plt.plot(w[:,0],w[:,1],'.')
plt.title('Weight trajectory of LSSE')
plt.show()

# In[69]:

## Logistic Regression with ML

epoch = 100
lr = 0.001
w2 = np.zeros((epoch,2))
w2[0,:] = [1,-1]

for i in range(epoch-1):
    #l_sse=sum((y_data-sigmoid(x_data.dot(w[i,:]))**2))
    lsw = sigmoid(np.dot(w2[i,:],x_data.T)).reshape((400,1))
    dif_l_sse = sum(
        (y_data*(1-lsw)
        -(1-y_data)*lsw)*x_data
)
    w2[i+1] = w2[i] + lr * dif_l_sse

# In[70]:

lsw = sigmoid(np.dot(w[0,:],x_data.T)).reshape((400,1))
dif_l_sse = sum(
        (y_data*(1-lsw)
        -(1-y_data)*lsw)*x_data
)
#w2[i+1] = w2[i] - lr * dif_l_sse
print(dif_l_sse)

# In[71]:

plt.figure(figsize=(17, 5))
plt.plot(w2[:,0],w2[:,1],'.')
plt.title('Weight trajectory of LML')
plt.legend(['Lsse', 'LML'])
plt.show()

# In[78]:

plt.figure(figsize=(17, 5))
plt.plot(w[:,0],w[:,1],'r.')
plt.plot(w2[:,0],w2[:,1],'.')
plt.plot(reg.coef_[0][0],reg.coef_[0][1], '^', markersize=20)
plt.title('Weight trajectory of LML and LSSE')
plt.legend(['Lsse', 'LML','Scikit learn'])
plt.show()


"""
core
"""
from copy import copy
import json
import os
import warnings

import yaml
from .converter import Converter
from .utils import open_file, Dict

class ConfigMeta(type):
    def __new__(cls, name, bases, attrs):
        converter_mapping = {}
        to_be_removed = []
        for key, value in attrs.items():
            if isinstance(value, (type, Converter)):
                converter_mapping[key.upper()] = value
                to_be_removed.append(key)
            elif key.isupper() and callable(value):
                converter_mapping[key.upper()] = value
                to_be_removed.append(key)
        for key in to_be_removed:
            attrs.pop(key)
        attrs['_converter'] = converter_mapping
        return super().__new__(cls, name, bases, attrs)

class Config(metaclass=ConfigMeta):
    def __init__(self, config_file=None, **defaults):
        self._config = Dict()
        self._converter = copy(self._converter)
        self._update(**defaults)
        if config_file:
            self.load_file(config_file)

    def load(self, file_obj):
        """Same as load_file.
        """
        return self.load_file(file_obj)

    def load_file(self, value, format='yaml'):
        """Load config from a json or yaml file.
        """
        if hasattr(value, 'endswith') and value.endswith('.json') \
                or format == 'json':
            self.load_json_file(value)
        else:
            self.load_yaml_file(value)

    def load_yaml_file(self, value):
        """Load config from yaml file.
        """
        with open_file(value) as f:
            data = yaml.safe_load(f)
        self._update(**data)

    def load_json_file(self, value):
        """Load config from json file.
        """
        with open_file(value) as f:
            data = json.load(f)
        self._update(**data)

    def load_env(self):
        self.load_dict(os.environ)

    def load_dict(self, dct):
        self._update(**dct)

    def _update(self, _prefix='', **dct):
        for key, value in dct.items():
            key = key.upper()
            if _prefix:
                key = _prefix + '.' + key
            if isinstance(value, dict):
                self._update(**value, _prefix=key)
            else:
                self._set(key, value)

    def _set(self, key, value):
        key = key.upper()
        nested_key_lst = key.strip('.').split('.')
        config_dct = self._config
        for part_key in nested_key_lst[:-1]:
            config_dct.setdefault(part_key, Dict())
            if not isinstance(config_dct[part_key], Dict):
                config_dct[part_key] = Dict()
            config_dct = config_dct[part_key]
        part_key = nested_key_lst[-1]
        config_dct[part_key] = self._convert(key, value)

    def _convert(self, key, value):
        if key in self._converter:
            return self._converter[key](value)
        return value

    def set_converter(self, key, convert_func):
        self._converter[key.upper()] = convert_func

    def set_cast_func(self, key, cast_func):
        warnings.warn('use set_converter instead of set_cast_func')
        return self.set_converter(key, cast_func)

    def __getitem__(self, key):
        config = self._config
        for part_key in key.upper().split('.'):
            config = config[part_key]
        return config

    def __setitem__(self, key, value):
        return self._set(key, value)

    def get(self, key, default=None):
        try:
            return self[key]
        except KeyError:
            return default

    def __getattr__(self, key):
        if not key.isupper():
            raise KeyError('{} does not exist'.format(key))
        return self.__getitem__(key)


import signal
import faulthandler
import argparse
import sys
import os
import re
from enum import Enum

from fab import testsuite
from fab import logutil
from fab import post
from fab import stats
from fab import skip
from fab import ignore
from fab import argutil
from fab import jsonutil
from fab import printer
from fab import publish

class Stats(Enum):
    def __str__(self):
        return self.value
    details = "details"
    summary = "summary"
    none = "none"

def main():

    # If SIGUSR1, backtrace all threads; hopefully this is early
    # enough.
    faulthandler.register(signal.SIGUSR1)

    parser = argparse.ArgumentParser(description="list test results",
                                     epilog="By default this tool uses 'sanitizer.sh' and 'diff' to generate up-to-the-minuite test results (the previously generated files 'OUTPUT/*.console.txt' and 'OUTPUT/*.console.diff' are ignored).  While this makes things a little slower, it has the benefit of always providing the most up-to-date and correct results (for instance, changes to known-good files are reflected immediately).  SIGUSR1 will dump all thread stacks")
    parser.add_argument("--verbose", "-v", action="count", default=0)

    parser.add_argument("--exit-ok", action="store_true",
                        help=("return a zero exit status; normally, when there are failures, a non-zero exit status is returned"))

    parser.add_argument("--quick", action="store_true",
                        help=("Use the previously generated '.console.txt' and '.console.diff' files"))

    parser.add_argument("--update", action="store_true",
                        help=("Update the '.console.txt' and '.console.diff' files"))

    parser.add_argument("--dump-args", action="store_true")

    # how to parse --print directory,saved-directory,...?
    parser.add_argument("--print", action="store",
                        default=printer.Print(printer.Print.PATH, printer.Print.RESULT, printer.Print.ISSUES),
                        type=printer.Print, metavar=str(printer.Print),
                        help="comma separated list of attributes to print for each test; default: '%(default)s'")

    parser.add_argument("--stats", action="store", default=Stats.summary, type=Stats,
                        choices=[c for c in Stats],
                        help="provide overview statistics; default: \"%(default)s\"");

    baseline_metavar = "BASELINE-DIRECTORY"
    baseline_help = "additional %(metavar)s containing results to compare against; any divergence between the test and baseline results are displayed"
    parser.add_argument("--baseline", "-b",
                        metavar=baseline_metavar, help=baseline_help)

    parser.add_argument("--json", action="store_true",
                        help="output each result as an individual json object (pipe the output through 'jq -s .' to convert it to a well formed json list")

    parser.add_argument("directories", metavar="DIRECTORY-OR-FILE", nargs="+",
                        help="a directory containing: a test, testsuite, test output, or testsuite output; or a file containing a 'TESTLIST'")

    # Note: this argument serves as documentation only.  The RESULT
    # argument should consumes all remaining parameters.
    parser.add_argument("baseline_ignored", nargs="?",
                        metavar=baseline_metavar, help=baseline_help)

    testsuite.add_arguments(parser)
    logutil.add_arguments(parser)
    # XXX: while checking for an UNTESTED test should be very cheap
    # (does OUTPUT/ exist?) it isn't.  Currently it triggers a full
    # post-mortem analysis.
    skip.add_arguments(parser, skip.Skip.UNTESTED)
    ignore.add_arguments(parser)
    publish.add_arguments(parser)

    # These three calls go together
    args = parser.parse_args()
    logutil.config(args, sys.stderr)
    logger = logutil.getLogger("kvmresults")

    if args.dump_args:
        logger.info("Arguments:")
        logger.info("  Stats: %s", args.stats)
        logger.info("  Print: %s", args.print)
        logger.info("  Baseline: %s", args.baseline)
        logger.info("  Json: %s", args.json)
        logger.info("  Quick: %s", args.quick)
        logger.info("  Update: %s", args.update)
        logger.info("  Exit OK: %s", args.exit_ok)
        testsuite.log_arguments(logger, args)
        logutil.log_arguments(logger, args)
        skip.log_arguments(logger, args)
        ignore.log_arguments(logger, args)
        publish.log_arguments(logger, args)
        return 0

    # Try to find a baseline.  If present, pre-load it.
    baseline = None
    if args.baseline:
        # An explicit baseline testsuite, can be more forgiving in how
        # it is loaded.
        baseline = testsuite.load(logger, logutil.DEBUG, args,
                                  testsuite_directory=args.baseline,
                                  error_level=logutil.DEBUG)
        if not baseline and os.path.isdir(args.baseline):
            # Perhaps, AKA BACKUP/YYYY-MM-DDD-..., the baseline
            # directory only contains a copy of the output.  Magic up
            # a baseline by combining the output with the tests in
            # ARGS.TESTING_DIRECTORY.
            baseline_directory = os.path.join(args.testing_directory, "pluto")
            baseline = testsuite.load(logger, logutil.DEBUG, args,
                                      testsuite_directory=baseline_directory,
                                      testsuite_output_directory=args.baseline,
                                      error_level=logutil.DEBUG)
        if not baseline:
            logger.info("'%s' is not a baseline", args.baseline)
            return 1
    elif len(args.directories) > 1:
        # If there is more than one directory then, perhaps, the last
        # one is a baseline.  A baseline might be: a complete
        # testsuite snapshot; or just output saved as
        # testing/pluto/OUTPUT/TESTDIR.
        baseline = testsuite.load(logger, logutil.DEBUG, args,
                                  testsuite_directory=args.directories[-1])
        if baseline:
            # discard the last argument as consumed above.
            logger.debug("discarding baseline testsuite argument '%s'", args.directories[-1])
            args.directories.pop()

    tests = testsuite.load_testsuite_or_tests(logger, args.directories, args)
    # And check
    if not tests:
        logger.error("Invalid testsuite or test directories")
        return 1

    result_stats = stats.Results()
    exit_code = 125 # assume a 'git bisect' barf
    try:
        exit_code = results(logger, tests, baseline, args, result_stats)
    finally:
        if args.stats is Stats.details:
            result_stats.log_details(stderr_log, header="Details:", prefix="  ")
        if args.stats in [Stats.details, Stats.summary]:
            result_stats.log_summary(stderr_log, header="Summary:", prefix="  ")

    return exit_code

def stderr_log(fmt, *args):
    sys.stderr.write(fmt % args)
    sys.stderr.write("\n")

def results(logger, tests, baseline, args, result_stats):

    failures = 0
    unresolved = 0
    passed = 0
    nr = 0

    for test in tests:

        nr = nr + 1
        publish.json_status(logger, args,
                            "rebuilding %s (test %d of %d)" % (test.name, nr, len(tests)))

        # If debug logging is enabled this will provide fine grained
        # per-test timing.

        with logger.debug_time("processing test %s", test.name):

            # Filter out tests that are being ignored?
            ignored, details = ignore.test(logger, args, test)
            if ignored:
                result_stats.add_ignored(test, ignored)
                continue

            # Filter out test results that are being skipped.
            #
            # XXX: In the default case (skip=[UNTESTED]) this should
            # be cheap (does OUTPUT/ exist?).  It isn't, instead a
            # full post-mortem analysis is performed.
            #
            # This is noticeable when printing static test value such
            # as the test's name takes far longer than one would
            # expect.
            result = post.mortem(test, args, logger,
                                 baseline=baseline,
                                 output_directory=test.saved_output_directory,
                                 quick=args.quick)
            if args.update:
                result.save()
            if args.skip:
                if printer.Print.RESULT in args.print \
                and skip.result(logger, args, result):
                    result_stats.add_skipped(result)
                    continue
            result_stats.add_result(result)

            if result.resolution in [post.Resolution.PASSED,
                                     post.Resolution.UNTESTED,
                                     post.Resolution.UNSUPPORTED]:
                passed = passed + 1
            elif result.resolution in [post.Resolution.UNRESOLVED]:
                unresolved = unresolved + 1
            else:
                failures = failures + 1

            publish.test_files(logger, args, result)
            publish.test_output_files(logger, args, result)
            publish.json_result(logger, args, result)

            if baseline and test in baseline \
               and not result.issues.crashed():
                # Since there is a baseline and the test didn't crash
                # limit what is printed to just those where the
                # baseline's result is different.
                #
                # Note that, this skips baseline-different - where the
                # baseline failed for a different reason.
                if {post.Issues.BASELINE_FAILED, post.Issues.BASELINE_PASSED}.isdisjoint(result.issues):
                    continue

            b = args.json and printer.JsonBuilder(sys.stdout) or printer.TextBuilder(sys.stdout)
            printer.build_result(logger, result, baseline, args, args.print, b)

        publish.json_results(logger, args)
        publish.json_summary(logger, args)

    publish.json_status(logger, args, "finished")

    # exit code
    if args.exit_ok:
        return 0
    elif unresolved:
        return 125 # 'git bisect' magic for don't know
    elif failures:
        return 1
    else:
        return 0

if __name__ == "__main__":
    sys.exit(main())


# пример использования преобразования к категориям
import numpy
from keras.datasets import cifar10
from keras.utils import to_categorical
# Задаем seed для повторяемости результатов
numpy.random.seed(42)

# Загружаем данные
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
# Размер мини-выборки
batch_size = 32
# Количество классов изображений
nb_classes = 10
# Количество эпох для обучения
nb_epoch = 25
# Размер изображений
img_rows, img_cols = 32, 32
# Количество каналов в изображении: RGB
img_channels = 3

# Нормализуем данные
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

# Преобразуем метки в категории
Y_train = to_categorical(y_train, nb_classes)
Y_test = to_categorical(y_test, nb_classes)

print("y: ", type(y_train))
print("Y: ", Y_train)

# y:
#  [[6]
#  [9]
#  [9]
#  ...
#  [9]
#  [1]
#  [1]]
# Y:
#  [[0. 0. 0. ... 0. 0. 0.]
#  [0. 0. 0. ... 0. 0. 1.]
#  [0. 0. 0. ... 0. 0. 1.]
#  ...
#  [0. 0. 0. ... 0. 0. 1.]
#  [0. 1. 0. ... 0. 0. 0.]
#  [0. 1. 0. ... 0. 0. 0.]]


# DCGAN-like generator and discriminator
from torch import nn
import torch.nn.functional as F

from spectral_normalization import SpectralNorm

channels = 3
leak = 0.1
w_g = 4

class ResBlockD(nn.Module):

    def __init__(self, in_channels, out_channels,downsample=None):
        super(ResBlockD, self).__init__()

        self.conv1 = SpectralNorm(nn.Conv2d(in_channels, out_channels, 3, 1, padding=1))
        self.conv2 = SpectralNorm(nn.Conv2d(out_channels, out_channels, 3, 1, padding=1))
        #self.conv_out = SpectralNorm(nn.Conv2d(in_channels, out_channels, 3, 1, padding=1))
        self.model = nn.Sequential(
                self.conv1,
                nn.LeakyReLU(leak),
                self.conv2
                )
        self.downsample = downsample

    def forward(self, x):
        if self.downsample:
            return self.model(x) + self.downsample(x)
        else:
            return self.model(x) + x

class Generator(nn.Module):
    def __init__(self, z_dim):
        super(Generator, self).__init__()
        self.z_dim = z_dim
        #
        self.ln = nn.Linear(z_dim,512*4*4)
        #
        self.model = nn.Sequential(
            #nn.ConvTranspose2d(512, 512, 1, stride=1),
            #nn.BatchNorm2d(512),
            #nn.ReLU(),
            nn.ConvTranspose2d(512, 256, 4, stride=2, padding=(1,1)),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.ConvTranspose2d(256, 128, 4, stride=2, padding=(1,1)),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.ConvTranspose2d(128, 64, 4, stride=2, padding=(1,1)),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.ConvTranspose2d(64, channels, 3, stride=1, padding=(1,1)),
            nn.Tanh())

    def forward(self, z):
        return self.model(self.ln(z).view(-1, 512, 4, 4))

class GeneratorG(nn.Module):
    def __init__(self, z_dim):
        super(Generator, self).__init__()
        self.z_dim = z_dim
        #
        self.ln = nn.Linear(z_dim,512*4*4)
        #
        self.model = nn.Sequential(
            #nn.ConvTranspose2d(512, 512, 1, stride=1),
            #nn.BatchNorm2d(512),
        #nn.ReLU(),

            nn.Sequential(

                nn.Conv2d(512, 256, 3, stride=1, padding=(1,1)),
                nn.LeakyReLU(leak),
                nn.Conv2d(256, 256, 3, stride=1, padding=(1,1))
            ),

            nn.Sequential(

                nn.BatchNorm2d(256),
                nn.LeakyReLU(leak),
                nn.Conv2d(256, 128, 3, stride=1, padding=(1,1)),
                nn.LeakyReLU(leak),
                nn.Conv2d(128, 128, 3, stride=1, padding=(1,1))
            ),

            nn.Sequential(

                nn.BatchNorm2d(128),
                nn.LeakyReLU(leak),
                nn.Conv2d(128, 64, 3, stride=1, padding=(1,1)),
                nn.LeakyReLU(leak),
                nn.Conv2d(64, 64, 3, stride=1, padding=(1,1))
            ),

            nn.Sequential(
                nn.BatchNorm2d(64),
                nn.Conv2d(64, channels, 3, stride=1, padding=(1,1)),
                nn.Tanh()
            ),

        )

    def forward(self, z):
        return self.model(self.ln(z).view(-1, 512, 4, 4))

class Discriminator(nn.Module):
    def __init__(self):
        super(Discriminator, self).__init__()
        '''
        self.seq = nn.Sequential(
            SpectralNorm(nn.Conv2d(channels,64,3, 1, padding=1)),
            nn.LeakyReLU(leak),
            ResBlockD(64,64),

            SpectralNorm(nn.Conv2d(64,128, 3, 1, padding=1)),
            nn.LeakyReLU(leak),
            ResBlockD(128,128),

            SpectralNorm(nn.Conv2d(128,256, 3, 1, padding=1)),
            nn.LeakyReLU(leak),
            ResBlockD(256,256),

            SpectralNorm(nn.Conv2d(256,512, 3, 1, padding=1)),
            nn.LeakyReLU(leak),
            ResBlockD(512,512),
        )
        '''
        self.seq = nn.Sequential(
            self.make_layer(channels,64),
            self.make_layer(64,64),
            self.make_layer(64,128),
            self.make_layer(128,128),
            #self.make_layer(128,256),
            #self.make_layer(256,512)
            )
        self.fc = SpectralNorm(nn.Linear(w_g * w_g * 128, 1))

    def make_layer(self,in_channels,out_channels,num=2):
        downsample = None
        if in_channels != out_channels:
            downsample = SpectralNorm(nn.Conv2d(in_channels,out_channels,kernel_size=3 ,stride=1,padding=1,bias=False))
        layers = []
        layers.append(ResBlockD(in_channels,out_channels,downsample))
        for _ in range(1,num):
            layers.append(ResBlockD(out_channels,out_channels))
        return nn.Sequential(*layers)

    def forward(self, x):
        m = x

        m = self.seq(m)
        return self.fc(m.view(-1,w_g * w_g * 128))


# -*- coding: utf-8 -*-
import pathlib
import argparse
import torch
from parse_config import ConfigParser
from model.model import NNEModel
from collections import OrderedDict

parser = argparse.ArgumentParser(description='Description of your program')
parser.add_argument('-i','--input', help='Path of model', required=True)
parser.add_argument('-o','--output', help='Path for output model', required=True)
args = vars(parser.parse_args())

try:
    checkpoint = torch.load(args['input'], map_location=torch.device('cpu'))

except NotImplementedError:
    temp = pathlib.PosixPath
    pathlib.PosixPath = pathlib.WindowsPath
    checkpoint = torch.load(args['input'], map_location=torch.device('cpu'))

state_dict_new = OrderedDict()
state_dict = checkpoint['state_dict']
for key, value in state_dict.items():
    state_dict_new[str(key).replace('module.','',1)] = value
torch.save(state_dict_new, args['output'])


# Original content by Jansen Smith, Nov 2022
# based on youtube.com/watch?v=kfjdVgKE6xY

import threading
import time
# import numpy as np
import datetime
# import pyzt
import tkinter as tk
from win10toast import ToastNotifier
# from playsound import playsound
import json
from os.path import exists

class Activitimer:
    """
    Main timer application class.

    This class represents the main timer application, which includes a graphical
    user interface (GUI) that allows the user to start and stop a timer for an
    activity, and displays the elapsed time for the activity in the GUI. The
    application also includes a notification feature that sends a notification
    when the elapsed time exceeds a certain threshold.

    Attributes:
        act1 (ActivityLog): An instance of the ActivityLog class to track
            start and stop times for activities.
        activity (bool): A flag to indicate whether the timer is currently
            running or not.
        activity_button (tk.Button): A button widget to start and stop the
            timer.
        root (tk.Tk): The main window of the application.
        time_label (tk.Label): A label widget to display the elapsed time
            for the activity.
    """

    def __init__(self):
        """
        Initialize attributes and create GUI.
        """

        self.act1 = ActivityLog()
        font_choice = ("Helvetica", 30)
        self.root = tk.Tk()
        self.root.geometry("335x160")
        self.root.title("Activitimer")

        #        self.time_entry = tk.Entry(self.root, font=font_choice)
        #        self.time_entry.grid(row=0, column=0, columnspan=2, padx=5, pady=5)

        self.time_label = tk.Label(self.root, font=font_choice, text="Time: 00:00:00")
        self.time_label.grid(row=0, column=0, columnspan=2, padx=5, pady=5)

        self.activity_button = tk.Button(self.root, font=font_choice,
                                         text="Start Activity", command=self.start_activity)
        self.activity_button.grid(row=1, column=0, columnspan=2, padx=5, pady=5)

        self.activity = False

        self.start_thread()
        self.root.mainloop()

    def start_thread(self):
        """
        Start a new thread to run the start() method.
        """

        print("Clock Starting")
        t = threading.Thread(target=self.start)
        t.start()

    def alarm(self):
        """
        Send a notification using the toast object when the elapsed time
        for an activity exceeds a certain threshold.
        """

        toast = ToastNotifier()
        toast.show_toast("Activitimer", "Time is up!", duration=3)

    def start(self):
        """
        Run the timer in a separate thread.

        This method updates the time_label widget to display the elapsed time
        for the activity, calls the set_button() method to update the
        activity_button widget based on the current status of the timer, and
        pauses for a short period of time before repeating.
        """

        while True:
            full_seconds_true = self.act1.time_avail()

            if full_seconds_true < 0:
                full_seconds = -full_seconds_true
                self.time_label.config(fg="red")
            else:
                full_seconds = full_seconds_true
                self.time_label.config(fg="black")

            minutes, seconds = divmod(full_seconds, 60)
            hours, minutes = divmod(minutes, 60)

            self.time_label.config(text=f"Time: {int(hours):02d}:{int(minutes):02d}:{seconds:05.2f}")
            self.set_button()
            self.root.update()
            time.sleep(0.01)

            if full_seconds_true < 0 and self.activity:
                alarm = threading.Thread(target=self.alarm())
                alarm.start()

    #        if not self.stop_loop:

    def set_button(self):
        """
        Update the activity_button widget based on the current status of the timer.
        """

        act1_latest_state = self.act1.latest_state()
        if act1_latest_state == 'activity1_start':
            self.activity_true()
        elif act1_latest_state == 'activity1_stop' or act1_latest_state == 'reset_time':
            self.activity_false()
        pass

    def activity_true(self):
        """
        Set the activity flag to True and update the activity_button widget to
        show "Stop Activity" and run the stop_activity() method when clicked.
        """

        self.activity = True
        self.activity_button.config(text="Stop Activity", command=self.stop_activity)

    def activity_false(self):
        """
        Set the activity flag to False and update the activity_button widget to
        show "Start Activity" and run the start_activity() method when clicked.
        """

        self.activity = False
        self.activity_button.config(text="Start Activity", command=self.start_activity)

    def start_activity(self):
        """
        Start a new activity by calling the activity1_start() method of the act1 object.
        """

        self.act1.activity1_start()

    def stop_activity(self):
        """
        Stop the current activity by calling the activity1_stop() method of the act1 object.
        """

        self.act1.activity1_stop()

class ActivityLog:
    """
    Activity log class.

    This class represents a log of activities, and keeps track of the start and stop
    times for each activity. It provides methods to start and stop the timer, and
    calculate the elapsed time for all activities.

    Attributes:
        hrs_per_day (float): The number of hours per day available for activities.
        log (list): A list of start and stop times for activities.
    """

    def __init__(self):
        """
        Initialize the activity log and try to load the log from the 'eventlog.json' file.
        If the file is not found or there is an error loading it, reset the timer.
        """

        self.hrs_per_day = 1.5
        self.log = []
        loaded = self.try_to_load_file('eventlog.json')
        if not loaded:
            self.reset()
            inject_hrs = 1
            inject_secs = inject_hrs * 3600
            self.log[0].timestamp = self.log[0].timestamp - inject_secs * 16
            self.save_file()

    def activity1_start(self):
        """
        Start a new activity by adding a new entry to the log list with the current
        time as the start time.
        Append a new event line marked "activity1_start"
        """

        self.append_entry("activity1_start")

    def activity1_stop(self):
        """
        Stop the current activity by adding the current time as the stop time to
        the latest entry in the log list.
        Append a new event line marked "activity1_stop"
        """
        self.append_entry("activity1_stop")

    def reset(self):
        """
        Reset the activity log and set the hrs_per_day attribute to 1.5.
        """

        # Append a new event line marked "reset_time"
        self.append_entry("reset_time")

    def append_entry(self, pass_str):
        self.log.append(LogEntry(pass_str, time.time()))
        self.save_file()

    def time_avail(self):
        """
        Calculate the elapsed time for all activities in the log list and return the
        remaining time based on the hrs_per_day attribute.

        This method iterates through the log list and adds up the elapsed time for
        each activity. It then subtracts the total elapsed time from the hrs_per_day
        attribute to get the remaining time.
        """

        time_gathered = self.time_gathered()
        time_spent = self.time_spent()
        time_avail = time_gathered - time_spent
        return time_avail

    def time_gathered(self):
        return (time.time() - self.last_reset()) * (self.hrs_per_day / 24)

    def time_spent(self):
        time_spent = 0
        culled, is_started = self.culled_legacy(self.last_reset())
        for x in culled:
            if x.event == "activity1_stop":
                time_spent += x.timestamp
            elif x.event == "activity1_start":
                time_spent -= x.timestamp
            else:
                raise Exception("The log should only have start and stop activities here. "
                                "Something has gone terribly wrong.")
        if is_started:
            time_spent += time.time()-self.log[-1].timestamp
        return time_spent

    def culled_legacy(self, timestamp):
        # returns the log, with all entries BEFORE a given timestamp removed
        # does not itself modify the existing log
        culled = []
        is_started = False
        for x in self.log:
            if x.timestamp > timestamp:
                culled.append(x)
        if bool(culled) and culled[-1].event == "activity1_start":
            is_started = True
            culled = culled[0:-1]
        return culled, is_started

    def last_reset(self):
        last_timestamp = 0
        for x in self.log:
            if x.event == "reset_time" and x.timestamp > last_timestamp:
                last_timestamp = x.timestamp
        return last_timestamp

    def sort(self):
        #untested
        self.log = sorted(self.log, key=lambda x: x.timestamp, reverse=False)

    def latest_state(self):
        """
        Return the current state of the timer based on the latest entry in the log list.

        If the latest entry has a start time but no stop time, return 'activity1_start'.
        If the latest entry has both a start and stop time, return 'activity1_stop'.
        If the log list is empty, return 'reset_time'.
        """

        return self.log[-1].event

    def print(self):
        print(self.toJSON())

    def toJSON(self):
        toJSON = {
            "eventlog": []
        }
        for x in self.log:
            toJSON["eventlog"].append({"event": x.event, "timestamp": x.timestamp})
        return json.dumps(toJSON, sort_keys=True, indent=None)

    def fromJSON(self, json_data):
        for entry in json_data['eventlog']:
            self.log.append(LogEntry(entry['event'], entry['timestamp']))

    def save_file(self):
        saved = False
        with open('eventlog.json', 'w', encoding='utf-8') as f:
            #json.dump(self.toJSON(), f, ensure_ascii=False, indent=4)
            f.write(self.toJSON())
        saved = True
        return saved

    def load_file(self, filename):
        with open(filename, 'r', encoding='utf-8') as f:
            json_data = json.load(f)
        return json_data

    def try_to_load_file(self, filename):
        """
        Try to load the activity log from the specified file.

        If the file is found and successfully loaded, return True. If the file
        is not found or there is an error loading it, return False.

        Args:
            filename (str): The name of the file to load the log from.

        Returns:
            bool: True if the file was found and successfully loaded, False otherwise.
        """

        loaded = False
        json_data = []
        if exists(filename):
            json_data = self.load_file(filename)
            self.fromJSON(json_data)
            loaded = True
        return loaded

class LogEntry:
    # Represents a single timestamped event in the log

    def __init__(self, event_name, timestamp_epoch):
        self.event = event_name
        self.timestamp = timestamp_epoch

    def __repr__(self):
        return '{' + self.event + ' @ ' + str(self.timestamp) + '}'

if __name__ == '__main__':

    act = Activitimer()


import sqlite3

from discord.ext import commands, tasks
from datetime import datetime

class UserDatabase(commands.Cog):

    def __init__(self, client):
        self.client = client
        self.conn = sqlite3.connect('./data/user.db')
        # xp and economy
        # inventory
        self.c = self.conn.cursor()

    # add cog to main system
    @commands.Cog.listener()
    async def on_ready(self):
        print('User DataBase online')

    @commands.command(description='check your current rank and experience in Project Void!')
    async def rank(self, ctx):
        if ctx.guild.id != 607298393370394625:
            return
        if len(ctx.message.mentions) == 0:
            user_id = ctx.author.id
        else:
            user_id = ctx.message.mentions[0].id
        self.c.execute("SELECT user_id, pts FROM users ORDER BY pts DESC")
        lb = self.c.fetchall()
        rank = -1
        pts = 0
        for i, entry in enumerate(lb):
            if entry[0] == user_id:
                rank = i+1
                pts = entry[1]
                break
        await ctx.send(f'Rank: {rank} <:KannaWow:843900425555410996>\nPoints: {pts} тнР')

    async def set_time(self, db: str, user: str):
        self.c.execute(f"UPDATE {db} SET time = '{datetime.now().strftime('%H:%M:%S')}' WHERE user_id = {user}")
        self.conn.commit()

    def find_user(self, db: str, user: str, var: str = '*'):
        self.c.execute(f"SELECT {var} FROM {db} WHERE user_id = {user}")
        return self.c.fetchone()

    async def insert(self, db: str, init_val: str):
        # c.execute("INSERT INTO users VALUES (123456789, 0, 0)")
        self.c.execute(f"INSERT INTO {db} VALUES {init_val}")
        self.conn.commit()

    async def update(self, db: str, var: str, amount: str, user: str):
        # amount here must have + or -
        self.c.execute(f"UPDATE {db} SET {var} = {var}{amount} WHERE user_id = {user}")
        self.conn.commit()

    async def set(self, db: str, var: str, amount: str, user):
        if user is None:
            self.c.execute(f"UPDATE {db} SET {var} = {amount}")
        else:
            self.c.execute(f"UPDATE {db} SET {var} = {amount} WHERE user_id = {user}")
        self.conn.commit()
        # self.c.execute('SELECT * FROM users')
        # print(self.c.fetchall())

    def find(self, db: str, var: str):
        self.c.execute(f'SELECT {var} FROM {db}')
        return self.c.fetchone()

    def top_six(self, category):
        self.c.execute("SELECT user_id FROM users ORDER BY " + category + " DESC LIMIT 6")
        return self.c.fetchall()

    def lb(self, guild):
        self.c.execute("SELECT user_id, pts FROM users ORDER BY pts DESC")
        unit = 'pts'
        lb = self.c.fetchall()
        rank = ''
        name = ''
        val = ''
        count = 0
        for i, entry in enumerate(lb):
            member = guild.get_member(lb[i][0])
            if member is None:
                continue
            rank += str(i + 1) + '\n'
            # name += str(entry[0]) + '\n\n'
            name += member.display_name if member.nick is None else member.nick
            name += '\n'
            val += str(entry[1]) + ' ' + unit + '\n'
            count += 1
            if count == 10:
                break
        return rank, name, val

def setup(client):
    client.add_cog(UserDatabase(client))


import csv
import re
import yaml
import logging

from github import Github
from pathlib import Path

logging.basicConfig(
    filename="meta_with_no_base.log",
    format="%(levelname)s: %(message)s",
    level=logging.INFO,
)

def notifier(msg):
    logging.info(msg)

def check_for_base(meta, pecha_id):
    source_meta = meta['source_metadata']
    out_key = 'volumes'
    if out_key in source_meta.keys():
        if source_meta['volumes'] != ({} or ''):
            volumes = source_meta['volumes']
            for vol_id, vol_info in volumes.items():
                key = 'base_file'
                if key in vol_info.keys():
                    print(f"{pecha_id} has base_file")
                    break
                else:
                    notifier(f"[{pecha_id}] has no base_file")
                    print(f"{pecha_id} has no base_file")
                    break
        else:
            print(f"[{pecha_id}] has no volumes")
    else:
        notifier(f"[{pecha_id}] meta data not updated")
        print(f"{pecha_id} has no volumes")

if __name__ == "__main__":
    token = ''
    g = Github(token)
    with open("catalog.csv", newline="") as csvfile:
        pechas = list(csv.reader(csvfile, delimiter=","))
        for pecha in pechas[3626:4302]:
            pecha_id = re.search("\[.+\]", pecha[0])[0][1:-1]
            repo = g.get_repo(f"Openpecha/{pecha_id}")
            contents = repo.get_contents(f"./{pecha_id}.opf/meta.yml")
            meta_content = contents.decoded_content.decode()
            meta_content = yaml.safe_load(meta_content)
            check_for_base(meta_content, pecha_id)


from django.contrib import admin

# Register your models here.
from course.models import course_category,course,lesson,course_requirements,access_to,course_comment,course_comment_reply,enrollment
from django.utils.html import format_html

admin.site.register(course_category)

class display_course(admin.ModelAdmin):
    list_display = ('course_name','photo_pre')
    readonly_fields = ('photo_pre',)
    list_per_page = 10
    search_fields = ('course_name','course_tittle')
    def photo_pre(self,obj):
        return format_html(f'')
admin.site.register(course,display_course)

class display_lesson(admin.ModelAdmin):
    list_display = ('lesson_name','photo_pre')
    readonly_fields = ('photo_pre',)
    list_per_page = 10
    search_fields = ('lesson_name','course_name')
    list_filter = ('course_name',)
    def photo_pre(self,obj):
        return format_html(f'')
admin.site.register(lesson,display_lesson)

class display_req(admin.ModelAdmin):
    list_display = ('course_name','requirement')
    list_per_page = 10
    search_fields = ('course_name','requirement')
    list_filter = ('course_name',)
admin.site.register(course_requirements,display_req)

class display_acccess_to(admin.ModelAdmin):
    list_display = ('course_name','access_to')
    list_per_page = 10
    search_fields = ('course_name','access_to')
    list_filter = ('course_name',)
admin.site.register(access_to,display_acccess_to)

admin.site.register(course_comment)
admin.site.register(course_comment_reply)

class display_enroll(admin.ModelAdmin):
    list_display = ('user_email','course_id','status')
    list_per_page = 10
    search_fields = ('user_email',)
admin.site.register(enrollment,display_enroll)


import requests
import json
from airflow.models import Variable
import logging
from googletrans import Translator
from vocab_utils.translate import translate_vocab

log = logging.getLogger(__name__)

def get_definitions(vocabs: list, vocab_origins: list, target_lang: str):
    """
    get_definitions()
        Using LinguaAPI, the definitions, examples, synonyms and contexts are gathered.
        Then they are stored into a dictionary format.

    """
    vocab_dic = {}
    for ind, vocab in enumerate(vocabs):

        # DEFINE vocab_info_dic
        # try: Some vocabularies do not have definitions (ex: fugazi)
        url = "https://lingua-robot.p.rapidapi.com/language/v1/entries/en/" + \
            vocab.lower().strip(' ')

        headers = {
            "X-RapidAPI-Key": Variable.get("lingua_credentials"),
            "X-RapidAPI-Host": "lingua-robot.p.rapidapi.com"
        }

        # Request Data
        response = requests.request("GET", url, headers=headers)
        data = json.loads(response.text)

        try:
            vocab_dat = data['entries'][0]['lexemes']
        except IndexError:
            vocab_dat = None
            definitions = None
            synonyms = None
            examples = None
            audio_url = None

        def extract_audio_url(json_data):

            try:
                json_data = data['entries']
                for entry in json_data:
                    pronunciations = entry.get('pronunciations', [])
                    for pronunciation in pronunciations:
                        audio = pronunciation.get('audio')
                        if audio:
                            return audio['url']
            except IndexError:
                return None
            return None

        # GET DEFINITIONS
        # try: If the definition is not in Lingua Dictionary, output None
        try:
            definitions = [vocab_dat[j]['senses'][i]['definition']
                            for j in range(len(vocab_dat)) for i in range(len(vocab_dat[j]['senses']))]
            definitions = definitions[:5]
        except:
            pass

        # GET AUDIO URLS
        audio_url = extract_audio_url(data)

        # GET SYNONYMS
        # try: If synonyms are not in Lingua Dictionary, output None
        try:
            synonyms = [vocab_dat[j]['synonymSets'][i]['synonyms']
                        for j in range(len(vocab_dat)) for i in range(len(vocab_dat[j]['synonymSets']))]
        except:
            synonyms = None

        # GET EXAMPLES
        if vocab_origins[ind] != None:
            context = vocab_origins[ind]
        else:
            context = None

        try:
            examples += [vocab_dat[j]['senses'][i]['usageExamples']
                        for j in range(len(vocab_dat)) for i in range(len(vocab_dat[j]['senses']))
                        if 'usageExamples' in vocab_dat[j]['senses'][i].keys()]
        except:
            examples = None

        # Collect vocab details
        vocab_info_dic = {vocab: {'definitions': definitions,
                                  'examples': examples,
                                  'synonyms': synonyms,
                                  'context': context,
                                  'audio_url': audio_url}}

        if target_lang != 'en':
            translator = Translator()
            vocab_info_dic = translate_vocab(translator, vocab_info_dic, 'en', target_lang)

        vocab_dic.setdefault(vocab, []).append(vocab_info_dic[vocab])

    return vocab_dic


import matplotlib

matplotlib.use("Agg")

import matplotlib.pyplot as plt
import torch
from monai.networks.blocks.activation import MemoryEfficientSwish, Swish

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

shapes_to_test = [128 * (x + 1) for x in range(8)]

def SwishTest(num_runs=1, mem_eff_swish=False):
    shapes_tested = []
    memory_used_in_mb = []

    if mem_eff_swish:
        layer = MemoryEfficientSwish()
    else:
        layer = Swish()

    for image_size in shapes_to_test:
        mem = 0.0
        for r in range(num_runs):
            input_image = torch.rand((1, 3, image_size, image_size), requires_grad=True).to(device)
            pred = layer(input_image)

            mem += torch.cuda.memory_allocated() / 1024 ** 2

            pred.sum().backward()

        memory_used_in_mb.append(mem / float(num_runs))
        shapes_tested.append(image_size)

    return shapes_tested, memory_used_in_mb

if __name__ == "__main__":
    sizes_tested_eff, memory_used_eff = SwishTest(mem_eff_swish=True)
    sizes_tested_neff, memory_used_neff = SwishTest(mem_eff_swish=False)

    plt.plot(sizes_tested_eff, memory_used_eff, "g-o", label="Memory Efficient Swish")
    plt.plot(sizes_tested_neff, memory_used_neff, "r-o", label="Swish")
    plt.xticks(ticks=sizes_tested_eff, rotation=45)
    plt.xlabel("Layer size")
    plt.ylabel("Allocated memory (MB)")
    plt.legend()
    plt.tight_layout()
    plt.grid()
    plt.savefig("figures/figure_singlelayer.png")