import inspect
import io
import urllib.request
from pathlib import Path

try:  # noqa: C901
    # In order to access super class attributes for our derived class, we must
    # import the native Python version, instead of the default Cython version.
    from multidict._multidict_py import MultiDict  # noqa: F401
except ImportError as err:
    raise ImportError(
        "The multidict library is not present, so the new PVLMultiDict is not "
        "available, and pvl.new can't be imported. In order to do so, install "
        "the multidict package",
        ImportWarning,
    ) from err

from pvl import *  # noqa: F401,F403
from pvl import get_text_from, decode_by_char

from .encoder import PDSLabelEncoder, PVLEncoder
from .parser import PVLParser, OmniParser
from .collections import PVLModuleNew, PVLGroupNew, PVLObjectNew

__all__ = [
    "PVLModuleNew",
    "PVLGroupNew",
    "PVLObjectNew",
]

def load(path, parser=None, grammar=None, decoder=None, **kwargs):
    """Returns a Python object from parsing the file at *path*.

    :param path: an :class:`os.PathLike` which presumably has a
        PVL Module in it to parse.
    :param parser: defaults to :class:`pvl.parser.OmniParser()`.
    :param grammar: defaults to :class:`pvl.grammar.OmniGrammar()`.
    :param decoder: defaults to :class:`pvl.decoder.OmniDecoder()`.
    :param ``**kwargs``: the keyword arguments that will be passed
        to :func:`loads()` and are described there.

    If *path* is not an :class:`os.PathLike`, it will be assumed to be an
    already-opened file object, and ``.read()`` will be applied
    to extract the text.

    If the :class:`os.PathLike` or file object contains some bytes
    decodable as text, followed by some that is not (e.g. an ISIS
    cube file), that's fine, this function will just extract the
    decodable text.
    """
    return loads(
        get_text_from(path),
        parser=parser,
        grammar=grammar,
        decoder=decoder,
        **kwargs
    )

def loadu(url, parser=None, grammar=None, decoder=None, **kwargs):
    """Returns a Python object from parsing *url*.

        :param url: this will be passed to :func:`urllib.request.urlopen`
            and can be a string or a :class:`urllib.request.Request` object.
        :param parser: defaults to :class:`pvl.parser.OmniParser()`.
        :param grammar: defaults to :class:`pvl.grammar.OmniGrammar()`.
        :param decoder: defaults to :class:`pvl.decoder.OmniDecoder()`.
        :param ``**kwargs``: the keyword arguments that will be passed
            to :func:`urllib.request.urlopen` and to :func:`loads()`.

        The ``**kwargs`` will first be scanned for arguments that
        can be given to :func:`urllib.request.urlopen`.  If any are
        found, they are extracted and used.  All remaining elements
        will be passed on as keyword arguments to :func:`loads()`.

        Note that *url* can be any URL that :func:`urllib.request.urlopen`
        takes.  Certainly http and https URLs, but also file, ftp, rsync,
        sftp and more!
        """

    # Peel off the args for urlopen:
    url_args = dict()
    for a in inspect.signature(urllib.request.urlopen).parameters.keys():
        if a in kwargs:
            url_args[a] = kwargs.pop(a)

    # The object returned from urlopen will always have a .read()
    # function that returns bytes, so:
    with urllib.request.urlopen(url, **url_args) as resp:
        s = decode_by_char(resp)

    return loads(s, parser=parser, grammar=grammar, decoder=decoder, **kwargs)

def loads(s: str, parser=None, grammar=None, decoder=None, **kwargs):
    """Deserialize the string, *s*, as a Python object.

    :param s: contains some PVL to parse.
    :param parser: defaults to :class:`pvl.parser.OmniParser() which will
        return the new PVLMultiDict-derived objects`.
    :param grammar: defaults to :class:`pvl.grammar.OmniGrammar()`.
    :param decoder: defaults to :class:`pvl.decoder.OmniDecoder()`.
    :param ``**kwargs``: the keyword arguments to pass to the *parser* class
        if *parser* is none.
    """
    if isinstance(s, bytes):
        # Someone passed us an old-style bytes sequence.  Although it isn't
        # a string, we can deal with it:
        s = s.decode()

    if parser is None:
        parser = OmniParser(
            grammar=grammar,
            decoder=decoder,
            module_class=PVLModuleNew,
            group_class=PVLGroupNew,
            object_class=PVLObjectNew,
            **kwargs
        )
    elif not isinstance(parser, PVLParser):
        raise TypeError("The parser must be an instance of pvl.PVLParser.")

    return parser.parse(s)

def dump(module, path, **kwargs):
    """Serialize *module* as PVL text to the provided *path*.

    :param module: a ``PVLModule`` or ``dict``-like object to serialize.
    :param path: an :class:`os.PathLike`
    :param ``**kwargs``: the keyword arguments to pass to :func:`dumps()`.

    If *path* is an :class:`os.PathLike`, it will attempt to be opened
    and the serialized module will be written into that file via
    the :func:`pathlib.Path.write_text()` function, and will return
    what that function returns.

    If *path* is not an :class:`os.PathLike`, it will be assumed to be an
    already-opened file object, and ``.write()`` will be applied
    on that object to write the serialized module, and will return
    what that function returns.
    """
    try:
        p = Path(path)
        return p.write_text(dumps(module, **kwargs))

    except TypeError:
        # Not an os.PathLike, maybe it is an already-opened file object
        try:
            if isinstance(path, io.TextIOBase):
                return path.write(dumps(module, **kwargs))
            else:
                return path.write(dumps(module, **kwargs).encode())
        except AttributeError:
            # Not a path, not an already-opened file.
            raise TypeError(
                "Expected an os.PathLike or an already-opened "
                "file object for writing, but got neither."
            )

def dumps(module, encoder=None, grammar=None, decoder=None, **kwargs) -> str:
    """Returns a string where the *module* object has been serialized
    to PVL syntax.

    :param module: a ``PVLModule`` or ``dict`` like object to serialize.
    :param encoder: defaults to :class:`pvl.parser.PDSLabelEncoder()`.
    :param grammar: defaults to :class:`pvl.grammar.ODLGrammar()`.
    :param decoder: defaults to :class:`pvl.decoder.ODLDecoder()`.
    :param ``**kwargs``: the keyword arguments to pass to the encoder
        class if *encoder* is none.
    """
    if encoder is None:
        encoder = PDSLabelEncoder(
            grammar=grammar,
            decoder=decoder,
            group_class=PVLGroupNew,
            object_class=PVLObjectNew,
            **kwargs)
    elif not isinstance(encoder, PVLEncoder):
        raise TypeError("The encoder must be an instance of pvl.PVLEncoder.")

    return encoder.encode(module)


import plotly as py
import plotly.graph_objs as go
import statistics
import math

import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()

gettingLocation = True
while (gettingLocation == True):
    print("\nPlease choose an input file with FASTA sequences: ")
    #Get a file location, set as inputFASTALocation
    inputFASTALocation = filedialog.askopenfilename()
    #Try to open the file, if successful set as FASTAData
    try:
        inputFile = open(inputFASTALocation,"r")
        gettingLocation = False
    #If opening the file fails alert the user and try again
    except IOError:
        print("Sorry, I'm unable to open that file location.")

gettingLocation = True
while (gettingLocation == True):
    print("\nPlease choose an output file location and file name: ")
    outputFileLocation = filedialog.asksaveasfilename()
    try:
        file = open(outputFileLocation,"w")
        gettingLocation = False
    except IOError:
        print("Sorry, I'm unable to open that file location.")

#Open the input file, read into a single large string
#with open (inputFile, "r") as file:
data = inputFile.read()

#Quick number check
numSeqs = data.count(">")
print("There are " + str(numSeqs) + " sequences in the input file")

#A list to hold all of the important data
seqsList = []

#A few variables to store search positions
lastSeq = 0
seqEnd = 0
for x in range (0, numSeqs):
    #Find the next sequence
    seqEnd = data.find(">", seqEnd+1)
    sequence = data[lastSeq:seqEnd]

    #Find the end of the header in the sequence
    headerEnd = sequence.find("\n")

    #Create a dictionary containing different elements for the header and the
    #amino acid seuqence, add this to seqsList.
    dictionary = {"header":sequence[:headerEnd],
                  "sequence":sequence[headerEnd:].replace("\n", "")}
    seqsList.append(dictionary)

    #Start the next search at the end of the last search
    lastSeq = seqEnd

#Append the length of each amino acid sequence to each item in seqsList
for sequence in seqsList:
    sequence["length"] = len(sequence["sequence"])

#Sort the sequences by length
seqsList = sorted(seqsList, key=lambda k: k["length"])

#Create a list to hold lengths, append the length of each seq to it
###This will be Y axis data for a bar chart###
lengthList = []
for seq in seqsList:
    lengthList.append(seq["length"])

#Create a list to hold sequence names, append names to it
###This will be X axis labels for a bar chart###
headerList = []
for seq in seqsList:
    headerList.append(seq["header"])

Avg = sum(lengthList) / numSeqs
print("\nAverage length = " + str(round(Avg,2)))
CI = statistics.stdev(lengthList)  * 1.96
upper = Avg + CI
lower = Avg - CI
print("Average - 1.96 standard deviations = " + str(round(lower,2)))
print("Average + 1.96 standard deviations = " + str(round(upper,2)) + "\n")

colours = []
for x in range (0, numSeqs):
    if ((lower > seqsList[x]["length"]) or (upper < seqsList[x]["length"])):
        colours.append('rgb(255, 64, 64)')
    else:
        colours.append('rgb(32,16,128)')

data = [go.Bar(x=headerList,y=lengthList,marker=dict(color=colours))]
py.offline.plot(data, filename="chart.html")

shortest = int(input("\nWhat's the shortest sequence *You want*? "))
longest = int(input("\nWhat's the longest sequence *You want*? "))

outputseqs = []

for seq in seqsList:
    if ((seq["length"] >= shortest) and (seq["length"] <= longest)):
        outputseqs.append(seq)
    else:
        print("Excluding " + seq["header"])

print("There are " + str(len(outputseqs)) + " seqs in the output list")

with open(outputFileLocation, "w") as file:
    for seq in outputseqs:
        file.write(seq["header"] + "\n" + seq["sequence"] + "\n")


from brick import *

from pgu import gui

import env, imgs

import os, math

from robothread import *

from dialog import SensorDialog, BackgroundDialog
from console import ConsoleDialog

from sensors import *

def p(path):
    """Nasty monkey patch - shall be removed"""
    import os
    from os.path import abspath, dirname
    return dirname(abspath(sys.argv[0])).replace('library.zip', '') + os.sep \
            + path

NXTEMU_ASCII = """
               __
   ____  _  __/ /____  ____ ___  __  __
  / __ \| |/_/ __/ _ \/ __ `__ \/ / / /
 / / / />  </ /_/  __/ / / / / / /_/ /
/_/ /_/_/|_|\__/\___/_/ /_/ /_/\__,_/

"""

class Robot(NXTBrick):
    proc = None
    die = False
    background = None
    bckg = None
    sensors = {}
    paused = False
    touchPoints = {
        "topleft": [
            [29.0, -133.60281897270363],
            [28.319604517012593, -132.13759477388825],
            [27.65863337187866, -130.6012946450045],
        ],
        "left": [
            [-29, 42.721999467666336],
            [-28.319604517012593, 41.389942632819086],
        ],
        "topright": [
            [27.018512172212592, -51.00900595749453],
            [27.65863337187866, -49.398705354995535],
            [28.319604517012593, -47.862405226111754],
        ],
        "right": [
            [29.698484809834994, -43.97583689433345],
            [29, -42.721999467666336],
            [29, -47.278000532333664],
            [28.319604517012593, -41.389942632819086],
            [28.319604517012593, -48.610057367180914],
        ]
    }

    def __init__(self, wboot = True):
        __builtins__['robot']= self

        self.x = env.w/2
        self.y = env.h/2
        self.angle = 0

        self.mA = 0
        self.mB = 0
        self.mC = 0

        self.p = 0

        self.rotA = self.rotB = self.rotC = 0

        self.radius = 21

        self.dragged = False
        self.brick_hidden = False
        self.dragoffset = []
        self.image = imgs.robot.convert_alpha()

        self.lock = Lock()

        self.root = os.path.abspath(os.path.dirname(sys.argv[0]))

        # directory with programs to the path
        sys.path.append(self.root + os.sep + '__progs__')

        self.lcd = pygame.Surface((204, 130))
        pygame.draw.rect(self.lcd, pygame.Color(0x43, 0x6c, 0x30),
            ((0, 0), (204, 130)))

        self._cur_lcd_line = 7

        if wboot:
            #print "booting"
            RoboThread(target=self.boot).start()

        self.sensors = {1: BaseSensor(None),
                        2: BaseSensor(None),
                        3: BaseSensor(None),
                        4: BaseSensor(None)}

        self.ports = {}

        self.bckgDialog = BackgroundDialog(bckg=env.cfg["others"]["background"])

        self.port_1 = SensorDialog(port=1)
        self.port_2 = SensorDialog(port=2)
        self.port_3 = SensorDialog(port=3)
        self.port_4 = SensorDialog(port=4)

        self.port_dialogs = [self.port_1, self.port_2, self.port_3, self.port_4]

        self.console = ConsoleDialog(init_code='from api import *',
                        init_text= 4*'\n' +
                        NXTEMU_ASCII +
                        "Welcome to the Robot Live Console." \
                        "Type help if you are lost.", ps1='robot>')

    def getDistanceTo(self, point):
        dx = point[0] - self.x
        dy = point[1] - self.y
        return math.sqrt(dx**2 + dy**2)

    def mouseOver(self):
        mpos = pygame.mouse.get_pos()
        if self.getDistanceTo(mpos) < self.radius:
            return True
        else:
            return False

    def drag(self):
        mpos = pygame.mouse.get_pos()
        self.x = mpos[0]
        self.y = mpos[1]

        #pygame.image.save(self.image, "robot_.png")

        self.stayIn()

    def rot_center(self, image, angle):
        """rotate an image while keeping its center and size"""
        orig_rect = image.get_rect()
        #rot_image = pygame.transform.rotate(image, angle)
        rot_image = pygame.transform.rotozoom(image, angle, 1)
        rot_rect = orig_rect.copy()
        rot_rect.center = rot_image.get_rect().center
        rot_image = rot_image.subsurface(rot_rect).copy()
        return rot_image

    def draw(self):

        env.screen.blit(env.background, (0,0))
        env.screen.blit(self.rot_center(self.image, -self.angle),
                (self.x - 30, self.y - 30))

        if not self.brick_hidden:
            env.screen.blit(self.lcd, ((640 + (378/2 - 100)-2, 90), (204, 130)))

        env.app.paint()
        pygame.display.flip()

    def touchesAt(self, positions):

        for pos in positions:
            dx = cos(radians(pos[1] + robot.angle)) * pos[0]
            dy = sin(radians(pos[1] + robot.angle)) * pos[0]

            #print 30 + round(dx), 30 + round(dy)

            x = int(self.x + round(dx))
            y = int(self.y + round(dy))

            #env.background.set_at((x, y), (0, 0, 255, 0))

            try:
                o = env.background.get_at((x, y))
            except IndexError:
                return True

            if o == (190, 190, 190, 255):
                return True

        return False

    def touches(self):
        out = {}
        for p in self.touchPoints:
            out[p] = self.touchesAt(p)

        return out

    def stayIn(self):
        if robot.brick_hidden:
            if self.x > 936:
                self.x = 936
        else:
            if self.x > 623:
                self.x = 623

        if self.x < 23:
            self.x = 23

        if self.y > 463:
            self.y = 463

        if self.y < 23:
            self.y = 23

    def tick(self):
        # self.stayIn()
        angle = 0
        rotA = rotB = 0
        touchedA = touchedB = False

        if not robot.paused:

            if not self.touchesAt(self.touchPoints["topleft"]):
                rotA = self.mA / 30.0
            else:
                touchedA = True
                rotA -= self.mA / 40.0

            if not self.touchesAt(self.touchPoints["topright"]):
                rotB = self.mB / 30.0
            else:
                touchedB = True
                rotB -= self.mB / 40.0

            rotC = self.mC / 30.0

            angle += (rotA - rotB) / 3

            self.angle += angle
            p = (rotA + rotB) / 2 / 1.8

            #print angle, self.angle, self.mA, self.mB

            self.rotA += rotA
            self.rotB += rotB
            self.rotC += rotC

            self.x += math.sin(math.radians(self.angle)) * p
            self.y += -math.cos(math.radians(self.angle)) * p

        self.draw()

        if touchedA:
            self.rotA += -2*rotA
        if touchedB:
            self.rotB += -2*rotB

        # print background.get_at((int(self.x), int(self.y)))

    def onCenter(self):
        #print "onCenter"

        # Turning off ? yes/no
        if self.screen_y == -1:
            if self.screen_x == 0:
                sys.exit(0)
            else:
                self.screen_y += 1
                self.screen_x = 0
                self.scrout()
                return

        if [self.screen_x, self.screen_y, self.screen_z] == [0, 3, 0]:
            if self.proc == None:

                module = __import__('e' + self.progs[self.prog])

                self.proc = RoboThread(target=module.main,
                                       cleaner=self.cleaner)
                self.proc.setName("brick")

                ClearScreen()
                self.scr_runner = RoboThread(target=robot.running)

                self.scr_runner.start()
                self.proc.start()
            return

        if self.screen_z:
            self.screen_z += 1
            self.screen_x = 0
        else:
            if self.screen_x == 0:
                self.screen_y += 1
            else:
                self.screen_z += 1
                self.screen_x = 0

        # taking care of empty __progs__ directory
        if self.screen_y == 2 and len(self.progs) == 0:
            self.screen_y -= 1

        #print self.screen_x, self.screen_y, self.screen_z
        self.scrout()

    def onBack(self):
        #taking care of turning off screen
        if self.screen_y == -1:
            self.screen_y += 1
            self.screen_x = 0
            self.scrout()
            return

        if self.proc == None:
            if self.scr_view == None:
                if self.screen_z:
                    self.screen_z -= 1
                else:
                    self.screen_y -= 1
            else:
                self.viewing = False
                self.scr_view = None
                self.screen_z -= 1
            self.screen_x = 0
            self.scrout()
        else:
            self.die = True
            self.scr_running = False

        #print self.screen_x, self.screen_y, self.screen_z

    def onLeft(self):
        if self.proc == None and self.scr_view == None:
            self.screen_x -= 1
        self.scrout()

    def onRight(self):
        if self.proc == None and self.scr_view == None:
            self.screen_x += 1
        self.scrout()

    def cleaner(self):
        ClearScreen()

        self.scr_running = False

        Off(OUT_ABC)
        ResetTachoCount(OUT_ABC)

        self.proc = None

        #self.screen_y -= 1
        self.scrout()
        #print "cleaner"

    def onConsole(self):

        if self.console.is_open():
            if not self.console_clicked:
                self.console_clicked = True
                return

            return self.consoleQuit(self.console)

        width = env.w + env.WALL_HEIGHT*2 + 378
        height = env.h + env.WALL_HEIGHT*2 + 200

        env.window = pygame.display.set_mode((width, height))
        env.screen = pygame.display.get_surface()
        env.background = pygame.Surface(env.screen.get_size()).convert()

        env.init(self.ports)

        ClearScreen()
        self.header()
        self.textCenterOut(LCD_LINE5 + 2, "Live Console")

        self.console.connect(gui.CHANGE, self.consoleQuit, self.console)
        self.console.connect(gui.CLOSE, self.consoleQuit, self.console)
        self.console.open()

        self.console.rect.y = env.h + env.WALL_HEIGHT*2 + 5

        # focusing the input
        x = int(self.console.rect.x) + 86 + 10
        y = int(self.console.rect.y) + 86 + 38 + 50
        self.console_clicked = False
        self.console.own_focus(x,y)

    def consoleQuit(self, d):
        env.window = pygame.display.set_mode((env.w + env.WALL_HEIGHT*2 + 378,
                                                env.h + env.WALL_HEIGHT*2))
        env.screen = pygame.display.get_surface()
        env.background = pygame.Surface(env.screen.get_size()).convert()

        env.init(self.ports)
        ClearScreen()
        self.scrout()

        d.close()

    def imgUpdate(self):
        image = imgs.robot.convert_alpha()

        for i in self.sensors:
            sensor = self.sensors[i]
            if sensor.slot != None and sensor.slot != 0:
                dx = sensor.slot * 7
                if sensor.slot == 3:
                    dx += 1

                dw = 1 if sensor.slot == 2 else 0
                pygame.draw.rect(image, (0xfa, 0x70, 0x0d),
                                 (13+dx, 9, 5+dw, 5))

       #pygame.draw.rect(image, (0xfa, 0x70, 0x0d), (20, 9, 5, 5))
       #pygame.draw.rect(image, (0xfa, 0x70, 0x0d), (27, 9, 6, 5))
       #pygame.draw.rect(image, (0xfa, 0x70, 0x0d), (35, 9, 5, 5))

        self.image = image

    def dialogClose(self, d):

        self.paused = False
        d.close()

    def open_and_center(self, dlg):
        if dlg.is_open():
            return self.dialogClose(dlg)

        dlg.change()
        dlg.open()
        dlg.rect.x = 120
        dlg.connect(gui.CLOSE, self.dialogClose, dlg)
        self.paused = True

    def writeit(self, d):
        env.write_config()
        self.imgUpdate()
        d.close()

    # background
    def bckg_return(self, d):
        self.paused = False
        out = d.out()

        if out is not None and os.path.isfile(out):
            robot.background = out
            env.init(self.ports)

            if robot.background is not "":
                env.cfg['others']['background'] = robot.background
                try:
                    img = pygame.image.load(robot.background)
                    if img.get_alpha() != None:
                        img = img.convert_alpha()
                    else:
                        img = img.convert()

                    img = pygame.transform.scale(img, (640, 480))
                    env.background.blit(img, (3, 3))
                except:
                    pass
            else:
                env.cfg['others']['background'] = "None"
        else:
            robot.background = None
            env.init(self.ports)

        self.writeit(d)

    def background_dialog(self):
        self.bckgDialog.connect(gui.CHANGE, self.bckg_return, self.bckgDialog)
        self.open_and_center(self.bckgDialog)

    def add_sensor(self, d, i):
        self.paused = False
        out = d.out()

        env.cfg["inputs"][i]["type"] = out["type"]
        env.cfg["inputs"][i]["slot"] = out["slot"]

        self.sensors[i] = sensor_generator(out['type'], out['slot'])
        self.ports[i] = out['slot']

        env.init(self.ports)
        self.writeit(d)

    # port 1
    def port1_return(self, d):
        self.add_sensor(d, 1)

    def port1(self):
        self.port_1.connect(gui.CHANGE, self.port1_return, self.port_1)
        self.open_and_center(self.port_1)
        self.port_1.port_connect_update()
        #self.port_1.pt['sensors']._rows[2][0]['widget'].widget.pcls = 'down'

    # port 2
    def port2_return(self, d):
        self.add_sensor(d, 2)

    def port2(self):
        self.port_2.connect(gui.CHANGE, self.port2_return, self.port_2)
        self.open_and_center(self.port_2)
        self.port_2.port_connect_update()

    # port 3
    def port3_return(self, d):
        self.add_sensor(d, 3)

    def port3(self):
        self.port_3.connect(gui.CHANGE, self.port3_return, self.port_3)
        self.open_and_center(self.port_3)
        self.port_3.port_connect_update()

    # port 4
    def port4_return(self, d):
        self.add_sensor(d, 4)

    def port4(self):
        self.port_4.connect(gui.CHANGE, self.port4_return, self.port_4)
        self.open_and_center(self.port_4)
        self.port_4.port_connect_update()

    def save_config(self, filename):
        env.write_config(filename)

    def load_config(self, filename):
        env.cfg = env.read_config(filename)
        for i, inp in env.cfg['inputs'].iteritems():
            self.sensors[i] = sensor_generator(inp['type'], inp['slot'])
            self.ports[i] = inp['slot']

            self.port_dialogs[i - 1].inp = inp
            self.port_dialogs[i - 1].build_slots()

        env.init(self.ports)
        robot.imgUpdate()

        self.save_config('./config.yml')


import pytest
import torch

class TestClassificationHead():
    @pytest.mark.parametrize("batch_size, input_size, num_classes, class_weights",
                             [
                                 (128, 32, [2, 2, 2, 2], None),
                                 # (1, 32, [2, 2, 2, 2], None),
                                 #
                                 # (128, 32, 2, None),
                                 # (1, 32, 2, None),
                                 # (128, 32, 2, [0.4, 0.6]),
                                 # (1, 32, 2, [0.1, 0.9]),
                                 # (128, 32, 4, None),
                                 # (1, 32, 4, None),
                                 # (128, 32, 4, [0.1, 0.3, 0.4, 0.9]),
                                 # (1, 32, 4,[0.1, 0.3, 0.4, 0.9]),
                                 # (128, 32, [2, 2, 2, 2], None),
                                 # (1, 32, [2, 2, 2, 2], None),
                             ])
    def test_forward(self, batch_size, input_size, num_classes, class_weights):
        from shared.utils import ClassificationHead

        inputs = torch.randn(size=(batch_size, input_size))

        if isinstance(num_classes, int):
            if num_classes == 2:
                labels = torch.randint(0, 2, size=(batch_size, ))
            elif num_classes > 2:
                labels = torch.randint(0, num_classes, size=(batch_size, ))
        elif isinstance(num_classes, list):
            if all(c == 2 for c in num_classes):
                labels = torch.randint(0, 2, size=(batch_size, len(num_classes)))

        classification_head = ClassificationHead(input_size=input_size, num_classes=num_classes, class_weights=class_weights)
        logits, probs, loss = classification_head(inputs, labels)

        assert True


#!/usr/bin/env python3
"""Find palindrome of a given number"""

import argparse
import sys

def get_input():
    """Parse command line arguments to get user input"""

    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('start_num', type=int, help='starting number')
    parser.add_argument('max_iter', type=int, help='maximum number of iterations')
    args = parser.parse_args()

    if args.start_num < 0:
        print("Input value must be non-negative")
        sys.exit()

    return args.start_num, args.max_iter

def is_palindrome(num):
    """Check if a number is a palindrome"""

    return str(num) == str(num)[::-1]

def main():
    """Find the palindrome of a given number"""

    start_num, max_iter = get_input()
    last_num = start_num

    for i in range(max_iter):
        if is_palindrome(last_num):
            print(f"{last_num} is a palindrome of {start_num} (Found after {i} iterations)")
            sys.exit()

        #print(last_num)
        last_num_str = str(last_num)
        last_num_inv = last_num_str[::-1]
        last_num += int(last_num_inv)

    print(f"Maximum iterations ({max_iter}) reached without finding a palindrome for {start_num}.")

if __name__ == '__main__':
    main()


# --------------------------------------------------------
# Deep Iterative Matching Network
# Licensed under The Apache-2.0 License [see LICENSE for details]
# Written by Yi Li, Gu Wang
# --------------------------------------------------------
from __future__ import print_function, division
import numpy as np
import lib.utils.image as image
from lib.utils.image import (
    get_pair_depth,
    get_pair_image,
    get_pair_flow,
    get_point_cloud_model,
    get_point_cloud_observed,
    get_gt_observed_depth,
    get_pair_mask,
    my_tensor_vstack,
)
from lib.pair_matching.RT_transform import calc_RT_delta

def get_data_pair_test_batch(pairdb, config):
    """
    return a dict of train batch
    :param segdb: ['image', 'flipped']
    :param config: the config setting
    :return: data, label, im_info
    """
    im_observed, im_rendered, scale_ind_list = get_pair_image(pairdb, config, "test")
    if config.network.INPUT_DEPTH:
        depth_observed, depth_rendered = get_pair_depth(pairdb, config, scale_ind_list, "test")
    if config.network.INPUT_MASK:
        mask_observed, _, mask_rendered = get_pair_mask(pairdb, config, scale_ind_list, "test")

    im_info = [np.array([pairdb[i]["height"], pairdb[i]["width"]], dtype=np.float32) for i in range(len(pairdb))]

    num_pair = len(pairdb)
    for i in range(num_pair):
        class_index_tensor = [[] for i in range(num_pair)]
        class_index_tensor[i] = np.array(config.dataset.class_name.index(pairdb[i]["gt_class"])).reshape(1)
        class_index_array = my_tensor_vstack(class_index_tensor)

    data = []
    for i in range(len(pairdb)):
        cur_batch = {
            "image_observed": im_observed[i],
            "image_rendered": im_rendered[i],
            "src_pose": np.array(pairdb[i]["pose_rendered"]).reshape((1, 3, 4)),
            "class_index": class_index_array,
        }

        if config.network.INPUT_DEPTH:
            cur_batch["depth_observed"] = depth_observed[i]
            cur_batch["depth_rendered"] = depth_rendered[i]

        if config.network.INPUT_MASK:
            cur_batch["mask_observed"] = mask_observed[i]
            cur_batch["mask_rendered"] = mask_rendered[i]

        data.append(cur_batch)
    label = {}

    return data, label, im_info

def update_data_batch(config, data_batch, update_package):
    import mxnet.ndarray as nd

    for ctx_idx, data in enumerate(data_batch.data):
        package = update_package[ctx_idx]
        for blob_idx, blob in enumerate(data):
            blob_name = data_batch.provide_data[ctx_idx][blob_idx][0]
            blob_shape = data_batch.provide_data[ctx_idx][blob_idx][1]
            if blob_name not in package:
                continue
            update_data = package[blob_name]
            if blob_name.startswith("image"):
                target_size = min(blob_shape[2:])
                max_size = max(blob_shape[2:])
                image_update, _ = image.resize(update_data, target_size, max_size)
                image_update = image.transform(image_update, config.network.PIXEL_MEANS)
                image_update = nd.array(image_update)
                data_batch.data[ctx_idx][blob_idx] = image_update
            elif blob_name.startswith("src_pose"):
                src_pose_update = nd.array(update_data[np.newaxis, :, :])
                data_batch.data[ctx_idx][blob_idx] = src_pose_update
            elif blob_name.startswith("depth"):
                target_size = min(blob_shape[2:])
                max_size = max(blob_shape[2:])
                depth_update, _ = image.resize(update_data, target_size, max_size)
                depth_update = nd.array(depth_update[np.newaxis, np.newaxis, :, :])
                data_batch.data[ctx_idx][blob_idx] = depth_update
            elif blob_name.startswith("mask_observed"):
                if config.TEST.UPDATE_MASK == "box_rendered":
                    update_data = np.copy(package["mask_rendered"])
                    mask_observed = np.zeros(update_data.shape)
                    x_max = np.max(update_data, 0)
                    y_max = np.max(update_data, 1)
                    nz_x = np.nonzero(x_max)[0]
                    nz_y = np.nonzero(y_max)[0]
                    x_start = np.min(nz_x)
                    x_end = np.max(nz_x)
                    y_start = np.min(nz_y)
                    y_end = np.max(nz_y)
                    mask_observed[y_start:y_end, x_start:x_end] = 1.0  # rectangle
                elif config.TEST.UPDATE_MASK == "box_observed":
                    mask_observed = np.zeros(update_data.shape)
                    x_max = np.max(update_data, 0)
                    y_max = np.max(update_data, 1)
                    nz_x = np.nonzero(x_max)[0]
                    nz_y = np.nonzero(y_max)[0]
                    if len(nz_x) == 0 or len(nz_y) == 0:
                        raise Exception("no point valid in mask_observed")

                    x_start = np.min(nz_x)
                    x_end = np.max(nz_x)
                    y_start = np.min(nz_y)
                    y_end = np.max(nz_y)
                    mask_observed[y_start:y_end, x_start:x_end] = 1.0  # rectangle
                else:
                    mask_observed = update_data
                mask_update = nd.array(mask_observed[np.newaxis, np.newaxis, :, :])
                data_batch.data[ctx_idx][blob_idx] = mask_update
            elif blob_name.startswith("mask_rendered"):
                mask_update = nd.array(update_data[np.newaxis, np.newaxis, :, :])
                data_batch.data[ctx_idx][blob_idx] = mask_update
            else:
                raise Exception("NOT_IMPLEMENTED")
    return data_batch

point_clound_dict = []

def get_data_pair_train_batch(pairdb, config):
    """
    return a dict of train batch
    :param pairdb: ['image_observed', 'image_rendered', 'height', 'width',
                    'depth_observed', 'depth_rendered', 'pose_observed', 'pose_rendered',
                    'flipped']
    :param config: ['INTRINSIC_MATRIX', 'DEPTH_FACTOR', ...]
    :return: data: ['image_observed', 'image_rendered', 'depth_observed', 'depth_rendered']
             label: ['flow', 'flow_weights'(RT)]
    """
    num_pair = len(pairdb)
    random_k = np.random.randint(18)
    im_observed, im_rendered, scale_ind_list = get_pair_image(pairdb, config, phase="train", random_k=random_k)
    depth_gt_observed = get_gt_observed_depth(pairdb, config, scale_ind_list, random_k=random_k)
    if config.network.INPUT_DEPTH:
        depth_observed, depth_rendered = get_pair_depth(
            pairdb, config, scale_ind_list, phase="train", random_k=random_k
        )
    if config.network.PRED_MASK or config.network.INPUT_MASK:
        mask_observed, mask_gt_observed, mask_rendered = get_pair_mask(
            pairdb, config, scale_ind_list, phase="train", random_k=random_k
        )
    im_observed_array = my_tensor_vstack(im_observed)
    im_rendered_array = my_tensor_vstack(im_rendered)

    depth_gt_observed_array = my_tensor_vstack(depth_gt_observed)
    if config.network.INPUT_DEPTH:
        depth_observed_array = my_tensor_vstack(depth_observed)
        depth_rendered_array = my_tensor_vstack(depth_rendered)
    if config.network.INPUT_MASK or config.network.PRED_MASK:
        mask_observed_array = my_tensor_vstack(mask_observed)
        mask_gt_observed_array = my_tensor_vstack(mask_gt_observed)
        mask_rendered_array = my_tensor_vstack(mask_rendered)

    if config.network.PRED_FLOW:
        flow_i2r, flow_i2r_weights, X_rendered_tensor, X_observed_tensor = get_pair_flow(
            pairdb, config, scale_ind_list, phase="train", random_k=random_k
        )
        flow_i2r_array = my_tensor_vstack(flow_i2r)
        flow_i2r_weights_array = my_tensor_vstack(flow_i2r_weights)

    if config.train_iter.SE3_PM_LOSS:
        X_obj, X_obj_weights = get_point_cloud_model(config, pairdb)
        X_obj_array = my_tensor_vstack(X_obj)
        X_obj_weights_array = my_tensor_vstack(X_obj_weights)

    # SE3
    rot_i2r_tensor = [[] for i in range(num_pair)]
    trans_i2r_tensor = [[] for i in range(num_pair)]
    src_pose_tensor = [[] for i in range(num_pair)]
    tgt_pose_tensor = [[] for i in range(num_pair)]
    tgt_points_tensor = [[] for i in range(num_pair)]
    for i in range(num_pair):
        rot_i2r, trans_i2r = calc_RT_delta(
            pairdb[i]["pose_rendered"],
            pairdb[i]["pose_observed"],
            config.dataset.trans_means,
            config.dataset.trans_stds,
            config.network.ROT_COORD,
            config.network.ROT_TYPE,
        )

        rot_i2r_tensor[i] = np.array(rot_i2r).reshape((1, -1))
        trans_i2r_tensor[i] = np.array(trans_i2r).reshape((1, -1))
        src_pose_tensor[i] = np.array(pairdb[i]["pose_rendered"]).reshape((1, 3, 4))
        tgt_pose_tensor[i] = np.array(pairdb[i]["pose_observed"]).reshape((1, 3, 4))
        if config.train_iter.SE3_PM_LOSS:
            tgt_points = get_point_cloud_observed(
                config, X_obj_array[i], pose_observed=np.array(pairdb[i]["pose_observed"])
            )
            tgt_points_tensor[i] = np.expand_dims(tgt_points, axis=0)  # (1, 3, 3000)

    rot_i2r_array = my_tensor_vstack(rot_i2r_tensor)
    trans_i2r_array = my_tensor_vstack(trans_i2r_tensor)
    src_pose_array = my_tensor_vstack(src_pose_tensor)  # before refinment
    tgt_pose_array = my_tensor_vstack(tgt_pose_tensor)  # refinement target
    tgt_points_array = my_tensor_vstack(tgt_points_tensor)

    for i in range(num_pair):
        class_index_tensor = [[] for i in range(num_pair)]
        class_index_tensor[i] = np.array(config.dataset.class_name.index(pairdb[i]["gt_class"])).reshape(num_pair)
        class_index_array = my_tensor_vstack(class_index_tensor)
    data = {
        "image_observed": im_observed_array,
        "image_rendered": im_rendered_array,
        "depth_gt_observed": depth_gt_observed_array,
        "class_index": class_index_array,
        "src_pose": src_pose_array,
        "tgt_pose": tgt_pose_array,
    }

    if config.network.INPUT_DEPTH:
        data["depth_observed"] = depth_observed_array
        data["depth_rendered"] = depth_rendered_array

    if config.network.INPUT_MASK:
        data["mask_observed"] = mask_observed_array
        data["mask_rendered"] = mask_rendered_array

    label = {"rot": rot_i2r_array, "trans": trans_i2r_array}

    if config.network.INPUT_MASK or config.network.PRED_MASK:
        label["mask_gt_observed"] = mask_gt_observed_array

    if config.network.PRED_FLOW:
        label["flow"] = flow_i2r_array
        label["flow_weights"] = flow_i2r_weights_array

    if config.train_iter.SE3_PM_LOSS:
        label["point_cloud_model"] = X_obj_array
        label["point_cloud_weights"] = X_obj_weights_array
        label["point_cloud_observed"] = tgt_points_array

    return {"data": data, "label": label}


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(name='albert',
      version='1.3.1',
      description='Interact with hidden REST API of albertheijn.nl',
      long_description = long_description,
      long_description_content_type="text/markdown",
      url='https://github.com/MHilhorst/albertheijn',
      author='M. Hilhorst',
      install_requires=[
          'requests'
      ],
      author_email='michaeljianghilhorst@gmail.com',
      packages=setuptools.find_packages(),
      classifiers=[
          "Programming Language :: Python :: 3",
          "License :: OSI Approved :: GNU General Public License (GPL)",
          "Operating System :: OS Independent"
      ]
)


# coding: utf-8

# In[1]:

import pandas as pd

# In[6]:

df = pd.DataFrame({'float': [1.0],
                   'int': [1],
                   'datetime': [pd.Timestamp('20180310')],
                   'string': ['foo']})
print(df)
print(df.dtypes)
print(type(df))


class Pet:

    def __init__(self, pet_name, date_of_birth, pet_type, breed, owner, treatment_notes, vet, id = None):
        self.pet_name = pet_name
        self.date_of_birth = date_of_birth
        self.pet_type = pet_type
        self.breed = breed
        self.owner = owner
        self.treatment_notes = treatment_notes
        self.vet = vet
        self.id = id


#The nested dictionary, pokemon, shows the number of various Pokemon that each person has caught while playing Pokemon Go.
#Find the total number of rattatas, dittos, and pidgeys caught and assign to the variables r, d, and p respectively.
#Do not hardcode. Note: Be aware that not every trainer has caught a ditto.

pokemon = {'Trainer1':
          {'normal': {'rattatas':15, 'eevees': 2, 'ditto':1}, 'water': {'magikarps':3}, 'flying': {'zubats':8, 'pidgey': 12}},
          'Trainer2':
          {'normal': {'rattatas':25, 'eevees': 1}, 'water': {'magikarps':7}, 'flying': {'zubats':3, 'pidgey': 15}},
          'Trainer3':
          {'normal': {'rattatas':10, 'eevees': 3, 'ditto':2}, 'water': {'magikarps':2}, 'flying': {'zubats':3, 'pidgey': 20}},
          'Trainer4':
          {'normal': {'rattatas':17, 'eevees': 1}, 'water': {'magikarps':9}, 'flying': {'zubats':12, 'pidgey': 14}}}

count_pokemon = {}
for person in pokemon:
    for condition in pokemon[person]:
        for Type in pokemon[person][condition]:
            count_pokemon[Type] = count_pokemon.get(Type,0)+ pokemon[person][condition][Type]

r = count_pokemon['rattatas']
d = count_pokemon['ditto']
p = count_pokemon['pidgey']

Nested Data and Nested Iteration/pokemon.py


import re
def main():
	#let's backtrace!
	s1="ben"
	s2="coh"
	s3="air"
	print(replace("123"))
def replace(string):
	returns=[]
	try:
		return int(string)

	except:
		for x in string:
			try:
				int(x)
			except:
				for z in range(0,10):
					returns.append(string.replace(x,str(z)))
				return returns

main()


#!/usr/bin/env python3

from heapq import heapify, heappush
from itertools import takewhile

tree_map = []

def visible_from_horizontal(y, w, side="left"):
    seen = []
    visible = set()
    line = tree_map[y] if side == "left" else reversed(tree_map[y])

    for x, tree in enumerate(line):
        if not seen or -seen[0] < tree:
            visible.add((x if side == "left" else w - 1 - x, y))
        heappush(seen, -tree)
    return visible

def visible_from_vertical(x, h, side="top"):
    seen = []
    visible = set()
    tree_iter = [row[x] for row in tree_map]
    line = tree_iter if side == "top" else reversed(tree_iter)

    for y, tree in enumerate(line):
        if not seen or -seen[0] < tree:
            visible.add((x, y if side == "top" else h - 1 - y))
        heappush(seen, -tree)
    return visible

def scenic_score_horizontal(y, from_x, side="left"):
    line = (
        tree_map[y][from_x:]
        if side == "left"
        else list(reversed(tree_map[y][: from_x - 1]))
    )
    me = tree_map[y][from_x - 1]
    if not line:
        return 0

    return min(len(line), len([tree for tree in takewhile(lambda t: t < me, line)]) + 1)

def scenic_score_vertical(x, from_y, side="top"):
    tree_iter = [
        row[x]
        for row in (tree_map[from_y:] if side == "top" else tree_map[: from_y - 1])
    ]
    line = tree_iter if side == "top" else list(reversed(tree_iter))
    me = tree_map[from_y - 1][x]
    if not line:
        return 0

    return min(len(line), len([tree for tree in takewhile(lambda t: t < me, line)]) + 1)

if __name__ == "__main__":
    with open("input") as f:
        data = f.read()
    lines = [line.strip() for line in data.split("\n")]
    # do stuff with data
    for line in lines:
        if not line:
            continue
        tree_map.append([int(tree) for tree in line])
    h, w = len(tree_map), len(tree_map[0])
    seen = set()
    for side in ("left", "right"):
        for y in range(h):
            seen |= visible_from_horizontal(y, w, side)
    for side in ("top", "bottom"):
        for x in range(w):
            seen |= visible_from_vertical(x, h, side)
    print(len(seen))
    scenic_scores = {}
    for x in range(0, w):
        for y in range(0, h):
            scenic_scores[(x, y)] = (
                scenic_score_horizontal(y, x + 1, "left")
                * scenic_score_horizontal(y, x + 1, "right")
                * scenic_score_vertical(x, y + 1, "top")
                * scenic_score_vertical(x, y + 1, "bottom")
            )
    print(max(scenic_scores.values()))


import sqlite3

import psycopg2
from psycopg2.extensions import connection as _connection

TABLES = ('film_work', 'genre', 'person', 'genre_film_work', 'person_film_work')

class DBExtractor:

    def __init__(self, connection: sqlite3.Connection | _connection):
        self.conn = connection
        self.cursor = connection.cursor()

    def data_generator(self):
        for row in self.cursor:
            yield dict(row)

    def extract_movies(self):
        for table in TABLES:
            if isinstance(self.conn, sqlite3.Connection):
                query = '''
                        SELECT *, (SELECT COUNT(*) FROM {}) as count FROM {} ORDER BY id;
                        '''.format(table, table)
            elif isinstance(self.conn, psycopg2.extensions.connection):
                query = '''
                        SELECT * FROM {} ORDER BY id;
                        '''.format(table)
            self.cursor.execute(query)
            yield self.data_generator()

        self.cursor.close()


from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from time import sleep
import re
import sys
import os

# example: `python main.py LA001.desc LA001.sol`

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

cond = sys.argv[1]
desc = sys.argv[2]

url = 'file://' + script_path + '/checker/checker.html'

base_path = os.getcwd()

if not os.path.isabs(cond):
    cond = os.path.normpath(os.path.join(base_path, cond))
else:
    cond = os.path.normpath(cond)

if not os.path.isabs(desc):
    desc = os.path.normpath(os.path.join(base_path, desc))
else:
    desc = os.path.normpath(desc)

options = ChromeOptions()
# ヘッドレスモードを有効にする（次の行をコメントアウトすると画面が表示される）。
options.add_argument('--headless')
# ChromeのWebDriverオブジェクトを作成する。
driver = Chrome(options=options)

try:
    driver.get(url)

    assert 'Puzzle' in driver.title

    # find input model file
    task_input = WebDriverWait(driver, 1000).until(
        expected_conditions.presence_of_element_located((By.ID, "submit_task"))
    )
    solution_input = WebDriverWait(driver, 1000).until(
        expected_conditions.presence_of_element_located((By.ID, "submit_solution"))
    )
    button = WebDriverWait(driver, 1000).until(
        expected_conditions.presence_of_element_located((By.ID, "execute_solution"))
    )

    task_input.send_keys(cond);
    sleep(1)
    solution_input.send_keys(desc)
    sleep(1)
    button.click()

    stdout_text = ''
    wait_limit = 3000
    while wait_limit > 0:
        stdout_text = WebDriverWait(driver, 1000).until(
            expected_conditions.presence_of_element_located((By.ID, "output"))
        ).text

        if stdout_text.find('Success!') >= 0:
            break
        if stdout_text.find('Failed') >= 0 or stdout_text.find('Cannot check') >= 0:
            print('Found failure: ' + stdout_text)
            exit(1)
        if stdout_text.find('Not all parts of the task were covered.') >= 0:
            print('Wrong solution: ' + stdout_text)
            exit(1)
        sleep(1)
        wait_limit -= 1

    if stdout_text.find('Success!') < 0:
        print('timeout')
        exit(1)

    print('Success')
# スクリーンショットを撮る。
#driver.save_screenshot('tracer.png')

finally:
    driver.quit()  # ブラウザーを終了する


import re
import random
import string

from bbot.modules.deadly.ffuf import ffuf
from bbot.core.helpers.misc import parse_list_string

def find_common_prefixes(strings, minimum_set_length=4):
    prefix_candidates = [s[:i] for s in strings if len(s) == 6 for i in range(3, 6)]
    frequency_dict = {item: prefix_candidates.count(item) for item in prefix_candidates}
    frequency_dict = {k: v for k, v in frequency_dict.items() if v >= minimum_set_length}
    prefix_list = list(set(frequency_dict.keys()))

    found_prefixes = set()
    for prefix in prefix_list:
        prefix_frequency = frequency_dict[prefix]
        is_substring = False

        for k, v in frequency_dict.items():
            if prefix != k:
                if prefix in k:
                    is_substring = True
        if not is_substring:
            found_prefixes.add(prefix)
        else:
            if prefix_frequency > v and (len(k) - len(prefix) == 1):
                found_prefixes.add(prefix)
    return list(found_prefixes)

class ffuf_shortnames(ffuf):
    watched_events = ["URL_HINT"]
    produced_events = ["URL_UNVERIFIED"]
    flags = ["aggressive", "active", "iis-shortnames", "web-thorough"]
    meta = {"description": "Use ffuf in combination IIS shortnames"}

    options = {
        "wordlist": "",  # default is defined within setup function
        "wordlist_extensions": "",  # default is defined within setup function
        "lines": 1000000,
        "max_depth": 1,
        "version": "2.0.0",
        "extensions": "",
        "ignore_redirects": True,
        "find_common_prefixes": False,
        "find_delimeters": True,
    }

    options_desc = {
        "wordlist": "Specify wordlist to use when finding directories",
        "wordlist_extensions": "Specify wordlist to use when making extension lists",
        "lines": "take only the first N lines from the wordlist when finding directories",
        "max_depth": "the maxium directory depth to attempt to solve",
        "version": "ffuf version",
        "extensions": "Optionally include a list of extensions to extend the keyword with (comma separated)",
        "ignore_redirects": "Explicitly ignore redirects (301,302)",
        "find_common_prefixes": "Attempt to automatically detect common prefixes and make additional ffuf runs against them",
        "find_delimeters": "Attempt to detect common delimeters and make additional ffuf runs against them",
    }

    deps_ansible = [
        {
            "name": "Download ffuf",
            "unarchive": {
                "src": "https://github.com/ffuf/ffuf/releases/download/v#{BBOT_MODULES_FFUF_VERSION}/ffuf_#{BBOT_MODULES_FFUF_VERSION}_#{BBOT_OS_PLATFORM}_#{BBOT_CPU_ARCH}.tar.gz",
                "include": "ffuf",
                "dest": "#{BBOT_TOOLS}",
                "remote_src": True,
            },
        }
    ]

    in_scope_only = True

    async def setup(self):
        self.canary = "".join(random.choice(string.ascii_lowercase) for i in range(10))
        wordlist = self.config.get("wordlist", "")
        if not wordlist:
            wordlist = f"{self.helpers.wordlist_dir}/ffuf_shortname_candidates.txt"
        self.debug(f"Using [{wordlist}] for shortname candidate list")
        self.wordlist = await self.helpers.wordlist(wordlist)
        self.wordlist_lines = list(self.helpers.read_file(self.wordlist))

        wordlist_extensions = self.config.get("wordlist_extensions", "")
        if not wordlist_extensions:
            wordlist_extensions = f"{self.helpers.wordlist_dir}/raft-small-extensions-lowercase_CLEANED.txt"
        self.debug(f"Using [{wordlist_extensions}] for shortname candidate extension list")
        self.wordlist_extensions = await self.helpers.wordlist(wordlist_extensions)

        try:
            self.extensions = parse_list_string(self.config.get("extensions", ""))
            self.debug(f"Using custom extensions: [{','.join(self.extensions)}]")
        except ValueError as e:
            self.warning(f"Error parsing extensions: {e}")
            return False

        self.ignore_redirects = self.config.get("ignore_redirects")

        self.per_host_collection = {}
        self.shortname_to_event = {}
        return True

    def build_extension_list(self, event):
        used_extensions = []
        extension_hint = event.parsed.path.rsplit(".", 1)[1].lower().strip()
        if len(extension_hint) == 3:
            with open(self.wordlist_extensions) as f:
                for l in f:
                    l = l.lower().lstrip(".")
                    if l.lower().startswith(extension_hint):
                        used_extensions.append(l.strip())
            return used_extensions
        else:
            return [extension_hint]

    def find_delimeter(self, hint):
        delimeters = ["_", "-"]
        for d in delimeters:
            if d in hint:
                if not hint.startswith(d) and not hint.endswith(d):
                    return d, hint.split(d)[0], hint.split(d)[1]
        return None

    async def filter_event(self, event):
        return True

    async def handle_event(self, event):
        if event.source.type == "URL":
            filename_hint = re.sub(r"~\d", "", event.parsed.path.rsplit(".", 1)[0].split("/")[-1]).lower()

            host = f"{event.source.parsed.scheme}://{event.source.parsed.netloc}/"
            if host not in self.per_host_collection.keys():
                self.per_host_collection[host] = [(filename_hint, event.source.data)]

            else:
                self.per_host_collection[host].append((filename_hint, event.source.data))

            self.shortname_to_event[filename_hint] = event

            root_stub = "/".join(event.parsed.path.split("/")[:-1])
            root_url = f"{event.parsed.scheme}://{event.parsed.netloc}{root_stub}/"

            if "shortname-file" in event.tags:
                used_extensions = self.build_extension_list(event)

            if len(filename_hint) == 6:
                tempfile, tempfile_len = self.generate_templist(prefix=filename_hint)
                self.verbose(
                    f"generated temp word list of size [{str(tempfile_len)}] for filename hint: [{filename_hint}]"
                )

            else:
                tempfile = self.helpers.tempfile([filename_hint], pipe=False)
                tempfile_len = 1

            if tempfile_len > 0:
                if "shortname-file" in event.tags:
                    for ext in used_extensions:
                        async for r in self.execute_ffuf(tempfile, root_url, suffix=f".{ext}"):
                            self.emit_event(r["url"], "URL_UNVERIFIED", source=event, tags=[f"status-{r['status']}"])

                elif "shortname-directory" in event.tags:
                    async for r in self.execute_ffuf(tempfile, root_url, exts=["/"]):
                        r_url = f"{r['url'].rstrip('/')}/"
                        self.emit_event(r_url, "URL_UNVERIFIED", source=event, tags=[f"status-{r['status']}"])

            if self.config.get("find_delimeters"):
                if "shortname-directory" in event.tags:
                    delimeter_r = self.find_delimeter(filename_hint)
                    if delimeter_r:
                        delimeter, prefix, partial_hint = delimeter_r
                        self.verbose(f"Detected delimeter [{delimeter}] in hint [{filename_hint}]")
                        tempfile, tempfile_len = self.generate_templist(prefix=partial_hint)
                        async for r in self.execute_ffuf(
                            tempfile, root_url, prefix=f"{prefix}{delimeter}", exts=["/"]
                        ):
                            self.emit_event(r["url"], "URL_UNVERIFIED", source=event, tags=[f"status-{r['status']}"])

                elif "shortname-file" in event.tags:
                    for ext in used_extensions:
                        delimeter_r = self.find_delimeter(filename_hint)
                        if delimeter_r:
                            delimeter, prefix, partial_hint = delimeter_r
                            self.verbose(f"Detected delimeter [{delimeter}] in hint [{filename_hint}]")
                            tempfile, tempfile_len = self.generate_templist(prefix=partial_hint)
                            async for r in self.execute_ffuf(
                                tempfile, root_url, prefix=f"{prefix}{delimeter}", suffix=f".{ext}"
                            ):
                                self.emit_event(
                                    r["url"], "URL_UNVERIFIED", source=event, tags=[f"status-{r['status']}"]
                                )

    async def finish(self):
        if self.config.get("find_common_prefixes"):
            per_host_collection = dict(self.per_host_collection)
            self.per_host_collection.clear()

            for host, hint_tuple_list in per_host_collection.items():
                hint_list = [x[0] for x in hint_tuple_list]

                common_prefixes = find_common_prefixes(hint_list)
                for prefix in common_prefixes:
                    self.verbose(f"Found common prefix: [{prefix}] for host [{host}]")
                    for hint_tuple in hint_tuple_list:
                        hint, url = hint_tuple
                        if hint.startswith(prefix):
                            partial_hint = hint[len(prefix) :]

                            # safeguard to prevent loading the entire wordlist
                            if len(partial_hint) > 0:
                                tempfile, tempfile_len = self.generate_templist(prefix=partial_hint)

                                if "shortname-directory" in self.shortname_to_event[hint].tags:
                                    self.verbose(
                                        f"Running common prefix check for URL_HINT: {hint} with prefix: {prefix} and partial_hint: {partial_hint}"
                                    )

                                    async for r in self.execute_ffuf(tempfile, url, prefix=prefix, exts=["/"]):
                                        self.emit_event(
                                            r["url"],
                                            "URL_UNVERIFIED",
                                            source=self.shortname_to_event[hint],
                                            tags=[f"status-{r['status']}"],
                                        )
                                elif "shortname-file" in self.shortname_to_event[hint].tags:
                                    used_extensions = self.build_extension_list(self.shortname_to_event[hint])

                                    for ext in used_extensions:
                                        self.verbose(
                                            f"Running common prefix check for URL_HINT: {hint} with prefix: {prefix}, extension: .{ext}, and partial_hint: {partial_hint}"
                                        )
                                        async for r in self.execute_ffuf(
                                            tempfile, url, prefix=prefix, suffix=f".{ext}"
                                        ):
                                            self.emit_event(
                                                r["url"],
                                                "URL_UNVERIFIED",
                                                source=self.shortname_to_event[hint],
                                                tags=[f"status-{r['status']}"],
                                            )


# imports
import csv , requests
# gets the data from the api call
rawData = requests.get('https://api.open-meteo.com/v1/forecast?latitude=12.9719&longitude=77.5937&hourly=temperature_2m&forecast_days=1')
todayData = rawData.json()

date = todayData.get('hourly').get('time')[1][0:10] # gets the current date
temperature = todayData.get('hourly').get('temperature_2m') # List of current day's temperature (hourly)
temperature.insert(0,date) # puts the current date at the start of the list

with open('HourlyTemperature.csv', 'a', encoding='UTF8') as f:
     writer = csv.writer(f)
     writer.writerow(temperature) # appends the current day's temperatures

print(todayData)


# Search in a sorted 2D matrix
# Problem Statement: Given an m*n 2D matrix and an integer, write a program to find if the given integer exists in the matrix.

# Given matrix has the following properties:

# Integers in each row are sorted from left to right.
# The first integer of each row is greater than the last integer of the previous row
# Example 1:

# Input: matrix =
# [[1,3,5,7],
#  [10,11,16,20],
#  [23,30,34,60]],

# target = 3

# Output: true

# Explanation: As the given integer(target) exists in the given 2D matrix, the function has returned true.

def search(matrix, target):
    n = len(matrix)
    m = len(matrix[0])
    # i = 0
    # j = m-1
    # while i<n and j>=0:
    #     if (target == matrix[i][j]): return True          {   ********* for the problem which says all rows and columns sorted (refer GFG smilar problem)  **********    }
    #     elif (target < matrix[i][j]):
    #         j-=1
    #     else:
    #         i+=1
    # return False

    # {   *********       for problem that says all rows are sorted and first element of next row is greater than the last element of current row
    #                                                                      (letcode problem) which makes it optimal for binary search        ************       }

    low = 0
    high = n*m -1
    while low <= high:
        mid = low + (high-low)//2
        if target == matrix[mid//m][mid%m]: return True
        if (target < matrix[mid//m][mid%m]):
            high = mid - 1
        else:
            low = mid + 1
    return False

if __name__ == "__main__":
    matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]]
    target = 30
    ans = search(matrix, target)

    print(ans)


import numpy as np
import os
#import scipy as sp
#import scipy.sparse as sparse
import time
from trans_matrix import trans_matrix
from get_distr import get_user_query
from extract_score import load_result, load_score

def GPR():
    #r_prev = np.zeros(length).reshape((-1,1))/length
    M_T , doc_length, row_miss = trans_matrix()
    r_initial = np.ones(doc_length).reshape((-1,1))/doc_length

    #WS
    alpha = 0.2
    weight = 0.999
    p0 = np.ones(doc_length).reshape((-1,1))/doc_length

    user,query = get_user_query()

    start_t = time.perf_counter()

    difference = 1
    #print("start calculating rt:")
    rt = r_initial

    r_assist = np.ones(doc_length).reshape((-1,1))/doc_length
    while(difference>1e-8):
        prev_r = rt
        sum = 0
        for i in row_miss:
            sum = sum + rt[i-1]
        rt= (1-alpha)*M_T.dot(rt)+ alpha*p0 + (1-alpha)*sum*r_assist

        r_diff = rt - prev_r
        difference = abs(r_diff).sum()

    mid_t = time.perf_counter()

    root = "./"
    file = f'GPR-WS.txt'
    path = os.path.join(root,file)
    f_QSTPR = open(path,'w')
    f= open("./GPR.txt",'w')
    #for user:

    for i in range(len(user)):
        user_id = user[i]
        query_id = query[i]

        # now start calculate rq : QTSPR #traverse topic_id

        rq = rt

        doc_score = {}
        doc_IRscore = load_result(user_id,query_id)
        #origin = []

        for doc_num in doc_IRscore:

            IRscore = load_score(doc_IRscore,doc_num)
            PRscore = rq[doc_num-1]#doc_num-1 = rt_doc_index
            score = weight * PRscore + (1-weight) * IRscore
            doc_score[doc_num] = score
            #origin.append(doc_num)

        rank = sorted(doc_score.items(), key=lambda item:item[1], reverse=True)
        for i in range(len(rank)):
            print("%d-%d Q0 %d %d %f GPR-WS"%(user_id,query_id,rank[i][0],i+1,rank[i][1]),file = f_QSTPR)
            print("%d-%d Q0 %d %d %f GPR"%(user_id,query_id,rank[i][0],i+1,rank[i][1]),file = f)

    end_t = time.perf_counter()

    print("\nGPR: %f secs for PageRank\n\nGPR-WS %f secs for retrieval"%(mid_t-start_t,end_t-mid_t))

    f_QSTPR.close()
    f.close()

    #NS
    start_t = time.perf_counter()

    root = "../"
    file = f'GPR-NS.txt'
    path = os.path.join(root,file)
    f_QSTPR = open(path,'w')
    #for user:

    for i in range(len(user)):
        user_id = user[i]
        query_id = query[i]
        # now start calculate rq : QTSPR #traverse topic_id

        rq = rt

        doc_score = {}
        doc_IRscore = load_result(user_id,query_id)
        #origin = []

        for doc_num in doc_IRscore:

            #IRscore = load_score(doc_IRscore,doc_num)
            PRscore = rq[doc_num-1]#doc_num-1 = rt_doc_index
            score = PRscore
            doc_score[doc_num] = score

        rank = sorted(doc_score.items(), key=lambda item:item[1], reverse=True)
        for i in range(len(rank)):
            print("%d-%d Q0 %d %d %f GPR-NS"%(user_id,query_id,rank[i][0],i+1,rank[i][1]),file = f_QSTPR)

    end_t = time.perf_counter()

    print("\nGPR-NS: %f secs for retrieval"%(end_t-start_t))

    f_QSTPR.close()

    #CM
    start_t = time.perf_counter()

    root = "../"
    file = f'GPR-CM.txt'
    path = os.path.join(root,file)
    f_QSTPR = open(path,'w')
    #for user:

    for i in range(len(user)):
        user_id = user[i]
        query_id = query[i]
        # now start calculate rq : QTSPR #traverse topic_id

        rq = rt

        #MINMAX SCALER
        rq_min = rq.min()
        rq_max = rq.max()
        rq = (rq-rq_min)/(rq_max-rq_min)

        doc_IRscore = {}
        doc_IRscore_temp = load_result(user_id,query_id)
        doc_IRscore_value = np.array(list(doc_IRscore_temp.values()))
        doc_IRscore_key = np.array(list(doc_IRscore_temp.keys()))
        doc_IRscore_value_min = min(doc_IRscore_value)
        doc_IRscore_value_max = max(doc_IRscore_value)
        doc_IRscore_value = (doc_IRscore_value - doc_IRscore_value_min)/(doc_IRscore_value_max - doc_IRscore_value_min)

        for i in range(len(doc_IRscore_key)):
            doc_IRscore[doc_IRscore_key[i]] = doc_IRscore_value[i]

        doc_score = {}
        weight = 0.5
        for doc_num in doc_IRscore:

            IRscore = load_score(doc_IRscore,doc_num)
            PRscore = rq[doc_num-1]#doc_num-1 = rt_doc_index
            score = weight * PRscore + (1-weight) * IRscore
            doc_score[doc_num] = score

        rank = sorted(doc_score.items(), key=lambda item:item[1], reverse=True)
        for i in range(len(rank)):
            print("%d-%d Q0 %d %d %f GPR-CM"%(user_id,query_id,rank[i][0],i+1,rank[i][1]),file = f_QSTPR)
    end_t = time.perf_counter()

    print("\nGPR-CM: %f secs for retrieval"%(end_t-start_t))

    f_QSTPR.close()


def move_tail_1_step_closer_to_head(head, tail):
    if head[0] > tail[0] + 1:
        tail[0] += 1

        if tail[1] < head[1]:
            tail[1] += 1
        if tail[1] > head[1]:
            tail[1] -= 1

    elif head[0] < tail[0] - 1:
        tail[0] -= 1

        if tail[1] < head[1]:
            tail[1] += 1
        if tail[1] > head[1]:
            tail[1] -= 1

    if head[1] > tail[1] + 1:
        tail[1] += 1

        if tail[0] < head[0]:
            tail[0] += 1
        if tail[0] > head[0]:
            tail[0] -= 1

    elif head[1] < tail[1] - 1:
        tail[1] -= 1

        if tail[0] < head[0]:
            tail[0] += 1
        if tail[0] > head[0]:
            tail[0] -= 1

def convert_to_coord(lines):
    all_steps = []
    for dir, steps in lines:
        unit = 1
        delta = (unit, 0) if dir == "R" else (-unit, 0) if dir == "L" else (0, -unit) if dir == "D" else (0, unit)
        for step in range(steps):
            all_steps.append(delta)
    return all_steps

if __name__ == '__main__':
    with open('input.txt') as f:
        lines = [line.split() for line in f]
        lines = [[x[0], int(x[1])] for x in lines]

    coords = convert_to_coord(lines)

    head = [0, 0]
    tails = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
    tail_visited1 = set()
    tail_visited2 = set()

    for dx, dy in coords:
        head[0] += dx
        head[1] += dy
        for i in range(len(tails)):
            if i == 0:
                move_tail_1_step_closer_to_head(head, tails[0])
                tail_visited1.add((tails[0][0], tails[0][1]))
            else:
                move_tail_1_step_closer_to_head(tails[i - 1], tails[i])

            if i == 8:
                tail_visited2.add((tails[i][0], tails[i][1]))

    print(f'Result 1: {len(tail_visited1)}')
    print(f'Result 2: {len(tail_visited2)}')


"""
The flask application package.
"""
import os
from flask import Flask, url_for
from flask_sqlalchemy import SQLAlchemy
from flask_security import Security, SQLAlchemyUserDatastore
from flask_admin import Admin
from flask_admin import helpers as admin_helpers
from flask_admin.contrib.sqla import ModelView
from flask_restful import Api
from matcherAdmin.admin_model_views import AdminModelView

def ensure_dir(dir):
    if not os.path.isdir(dir):
        try:
            os.mkdir(dir)
        except OSError:
            print('Not sure how this happens')

ALLOWED_EXTENSIONS = set(['csv'])
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
api = Api(app)

ensure_dir(app.config['UPLOAD_FOLDER'])
ensure_dir(app.config['DB_FOLDER'])

from matcherAdmin.models import User, Role
# Flask-Admin Section
admin = Admin(app,
              name='wordmatcher',
              base_template= 'master_admin.html',
              template_mode='bootstrap3'
              )
admin.add_view(AdminModelView(User, db.session))
admin.add_view(AdminModelView(Role, db.session))

# Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)

# define a context processor for merging flask-admin's template context into the
# flask-security views
@security.context_processor
def security_context_processor():
    return dict(
        admin_base_template= admin.base_template,
        admin_view= admin.index_view,
        hasattr= admin_helpers,
        get_url= url_for
    )

import matcherAdmin.views
import matcherAdmin.api


"""Session Middleware"""

import asyncio
from typing import List

from bareutils import header
from bareclient import HttpClient, HttpClientMiddlewareCallback
from bareclient.middlewares import SessionMiddleware

async def main() -> None:
    """Session middleware example"""

    middleware: List[HttpClientMiddlewareCallback] = [SessionMiddleware()]

    async with HttpClient(
            'https://en.wikipedia.org/wiki/HTTP_cookie',
            method='GET',
            middleware=middleware
    ) as response:
        # We expect a session cookie to be sent on the initial request.
        set_cookie = header.find_all(b'set-cookie', response.headers)
        print("Session cookie!" if set_cookie else "No session cookie")

        if not response.ok:
            raise Exception("Failed to get page")

    async with HttpClient(
            'https://en.wikipedia.org/wiki/Web_browser',
            method='GET',
            middleware=middleware
    ) as response:
        # As we were sent the session cookie we do not expect to receive
        # another one, until this one has expired.
        set_cookie = header.find_all(b'set-cookie', response.headers)
        print("Session cookie!" if set_cookie else "No session cookie")

        if not response.ok:
            raise Exception("Failed to get page")

asyncio.run(main())


import argparse
import os

import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import glob
from tqdm import tqdm
from sklearn.decomposition import PCA

from os.path import basename, dirname

import logging
logging.getLogger("tensorflow").setLevel(logging.ERROR)
import json
import requests

def fetch_modules():
    payload = "[\"cma:po\",\"\",\"\",100,true,[[\"module-type\",[\"image-feature-vector\"]]],null,[],null,[]]"

    response = requests.request("POST", "https://tfhub.dev/s/list", data=payload,
                                headers={'content-type': "application/json"})
    response = response.text
    response = response[response.find('\n'):]

    def parse_modules(reponse):
        modules = {}
        for x in json.loads(reponse)[0][2]:
            modules[x[2]] = 'https://tfhub.dev/google/%s/%s' % (x[2], x[11])
        return dict(filter(lambda x: 'tf2' not in x[0] and 'quantops' not in x[0], modules.items()))

    return parse_modules(response)

modules = {
    # 'mobilenet_v1_025_128': 'https://tfhub.dev/google/imagenet/mobilenet_v1_025_128/feature_vector/3',
    # 'mobilenet_v1_050_128': 'https://tfhub.dev/google/imagenet/mobilenet_v1_050_128/feature_vector/3',
    # 'mobilenet_v1_075_128': 'https://tfhub.dev/google/imagenet/mobilenet_v1_075_128/feature_vector/3',
    # 'mobilenet_v1_100_128': 'https://tfhub.dev/google/imagenet/mobilenet_v1_100_128/feature_vector/3',
    # 'mobilenet_v1_025_160': 'https://tfhub.dev/google/imagenet/mobilenet_v1_025_160/feature_vector/3',
    # 'mobilenet_v1_050_160': 'https://tfhub.dev/google/imagenet/mobilenet_v1_050_160/feature_vector/3',
    # 'mobilenet_v1_075_160': 'https://tfhub.dev/google/imagenet/mobilenet_v1_075_160/feature_vector/3',
    # 'mobilenet_v1_100_160': 'https://tfhub.dev/google/imagenet/mobilenet_v1_100_160/feature_vector/3',
    # 'mobilenet_v1_025_192': 'https://tfhub.dev/google/imagenet/mobilenet_v1_025_192/feature_vector/3',
    # 'mobilenet_v1_050_192': 'https://tfhub.dev/google/imagenet/mobilenet_v1_050_192/feature_vector/3',
    # 'mobilenet_v1_075_192': 'https://tfhub.dev/google/imagenet/mobilenet_v1_075_192/feature_vector/3',
    # 'mobilenet_v1_100_192': 'https://tfhub.dev/google/imagenet/mobilenet_v1_100_192/feature_vector/3',
    'mobilenet_v1_025_224': 'https://tfhub.dev/google/imagenet/mobilenet_v1_025_224/feature_vector/3',
    'mobilenet_v1_050_224': 'https://tfhub.dev/google/imagenet/mobilenet_v1_050_224/feature_vector/3',
    'mobilenet_v1_075_224': 'https://tfhub.dev/google/imagenet/mobilenet_v1_075_224/feature_vector/3',
    'mobilenet_v1_100_224': 'https://tfhub.dev/google/imagenet/mobilenet_v1_100_224/feature_vector/3',
    # 'mobilenet_v2_035_96': 'https://tfhub.dev/google/imagenet/mobilenet_v2_035_96/feature_vector/3',
    # 'mobilenet_v2_050_96': 'https://tfhub.dev/google/imagenet/mobilenet_v2_050_96/feature_vector/3',
    # 'mobilenet_v2_075_96': 'https://tfhub.dev/google/imagenet/mobilenet_v2_075_96/feature_vector/3',
    # 'mobilenet_v2_100_96': 'https://tfhub.dev/google/imagenet/mobilenet_v2_100_96/feature_vector/3',
    # 'mobilenet_v2_035_128': 'https://tfhub.dev/google/imagenet/mobilenet_v2_035_128/feature_vector/3',
    # 'mobilenet_v2_050_128': 'https://tfhub.dev/google/imagenet/mobilenet_v2_050_128/feature_vector/3',
    # 'mobilenet_v2_075_128': 'https://tfhub.dev/google/imagenet/mobilenet_v2_075_128/feature_vector/3',
    # 'mobilenet_v2_100_128': 'https://tfhub.dev/google/imagenet/mobilenet_v2_100_128/feature_vector/3',
    # 'mobilenet_v2_035_160': 'https://tfhub.dev/google/imagenet/mobilenet_v2_035_160/feature_vector/3',
    # 'mobilenet_v2_050_160': 'https://tfhub.dev/google/imagenet/mobilenet_v2_050_160/feature_vector/3',
    # 'mobilenet_v2_075_160': 'https://tfhub.dev/google/imagenet/mobilenet_v2_075_160/feature_vector/3',
    # 'mobilenet_v2_100_160': 'https://tfhub.dev/google/imagenet/mobilenet_v2_100_160/feature_vector/3',
    # 'mobilenet_v2_035_192': 'https://tfhub.dev/google/imagenet/mobilenet_v2_035_192/feature_vector/3',
    # 'mobilenet_v2_050_192': 'https://tfhub.dev/google/imagenet/mobilenet_v2_050_192/feature_vector/3',
    # 'mobilenet_v2_075_192': 'https://tfhub.dev/google/imagenet/mobilenet_v2_075_192/feature_vector/3',
    # 'mobilenet_v2_100_192': 'https://tfhub.dev/google/imagenet/mobilenet_v2_100_192/feature_vector/3',
    'mobilenet_v2_035_224': 'https://tfhub.dev/google/imagenet/mobilenet_v2_035_224/feature_vector/3',
    'mobilenet_v2_050_224': 'https://tfhub.dev/google/imagenet/mobilenet_v2_050_224/feature_vector/3',
    'mobilenet_v2_075_224': 'https://tfhub.dev/google/imagenet/mobilenet_v2_075_224/feature_vector/3',
    'mobilenet_v2_100_224': 'https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/3',
    'mobilenet_v2_130_224': 'https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/feature_vector/3',
    'mobilenet_v2_140_224': 'https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/3',
    'resnet_v1_50': 'https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/3',
    'resnet_v1_101': 'https://tfhub.dev/google/imagenet/resnet_v1_101/feature_vector/3',
    'resnet_v1_152': 'https://tfhub.dev/google/imagenet/resnet_v1_152/feature_vector/3',
    'resnet_v2_50': 'https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/3',
    'resnet_v2_101': 'https://tfhub.dev/google/imagenet/resnet_v2_101/feature_vector/3',
    'resnet_v2_152': 'https://tfhub.dev/google/imagenet/resnet_v2_152/feature_vector/3',
    'inception_v1': 'https://tfhub.dev/google/imagenet/inception_v1/feature_vector/3',
    'inception_v2': 'https://tfhub.dev/google/imagenet/inception_v2/feature_vector/3',
    'inception_v3': 'https://tfhub.dev/google/imagenet/inception_v3/feature_vector/3',
    'inception_resnet_v2': 'https://tfhub.dev/google/imagenet/inception_resnet_v2/feature_vector/3',
    'nasnet_mobile': 'https://tfhub.dev/google/imagenet/nasnet_mobile/feature_vector/3',
    'nasnet_large': 'https://tfhub.dev/google/imagenet/nasnet_large/feature_vector/3',
    'pnasnet_large': 'https://tfhub.dev/google/imagenet/pnasnet_large/feature_vector/3',
    'amoebanet_a_n18_f448': 'https://tfhub.dev/google/imagenet/amoebanet_a_n18_f448/feature_vector/1',
    'efficientnet_b0': 'https://tfhub.dev/google/efficientnet/b0/feature-vector/1',
    'efficientnet_b1': 'https://tfhub.dev/google/efficientnet/b1/feature-vector/1',
    'efficientnet_b2': 'https://tfhub.dev/google/efficientnet/b2/feature-vector/1',
    'efficientnet_b3': 'https://tfhub.dev/google/efficientnet/b3/feature-vector/1',
    'efficientnet_b4': 'https://tfhub.dev/google/efficientnet/b4/feature-vector/1',
    'efficientnet_b5': 'https://tfhub.dev/google/efficientnet/b5/feature-vector/1',
    'efficientnet_b6': 'https://tfhub.dev/google/efficientnet/b6/feature-vector/1',
    'efficientnet_b7': 'https://tfhub.dev/google/efficientnet/b7/feature-vector/1',
}

def _parse_images(path, label, size):
    image = tf.io.read_file(path)
    image = tf.io.decode_jpeg(image, channels=3)
    image = tf.image.convert_image_dtype(image, tf.float32)
    image = tf.image.resize(image, size, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
    # image = tf.image.resize_with_crop_or_pad(image, size[0], size[1])
    return image, label, path

def input_fn(paths, labels, size, batch_size=32):
    dataset = tf.data.Dataset.from_tensor_slices((paths, labels))
    dataset = dataset.map(lambda x, y: _parse_images(x, y, size))
    # dataset = dataset.shuffle(buffer_size=len(labels))
    dataset = dataset.repeat(1)
    dataset = dataset.batch(batch_size)
    iterator = tf.compat.v1.data.make_one_shot_iterator(dataset)
    images_batch, labels_batch, filenames_batch = iterator.get_next()
    return images_batch, labels_batch, filenames_batch

def run(dataset_folder, network='inception_v3', batch_size=16):
    assert network in modules, 'Invalid network, pick one of %s' % list(modules.keys())
    assert dataset_folder is not None

    with tf.Graph().as_default():
        dataset = basename(dirname(dataset_folder))
        filenames_output = os.path.join(dataset_folder, dataset + '_' + network + '_filenames.csv')
        labels_output = os.path.join(dataset_folder, dataset + '_' + network + '_labels.csv')
        features_output = os.path.join(dataset_folder, dataset + '_' + network + '_features.csv')

        module_url = modules[network]

        types = ('/*/*.jpg', '/*/*.png')
        filenames = []
        for files in types:
            filenames.extend(glob.glob(dataset_folder + files))
        pbar = tqdm(total=len(filenames))
        labels = [basename(dirname(f)) for f in filenames]
        filenames = tf.constant(filenames)
        labels = tf.constant(labels)

        module_spec = hub.load_module_spec(module_url)
        output_size = module_spec.get_output_info_dict()['default'].get_shape()[1]
        height, width = hub.get_expected_image_size(module_spec)

        images, labels, files = input_fn(filenames, labels, [height, width], batch_size)

        features = np.empty((0, output_size), float)
        classes = np.empty(0, int)
        filenames = np.empty(0, str)

        network = hub.Module(module_spec)
        network = network(images), labels, files

        with tf.compat.v1.Session() as sess:

            sess.run(tf.compat.v1.global_variables_initializer())
            sess.run(tf.compat.v1.tables_initializer())

            while True:
                try:
                    x, y, f = sess.run(network)
                    f = [basename(k) for k in f]
                    filenames = np.append(filenames, f)
                    classes = np.append(classes, y)
                    features = np.append(features, x, axis=0)
                    pbar.update(len(y))
                except tf.errors.OutOfRangeError:
                    break
        pbar.close()

        # pca = PCA(n_components=100, random_state=1)
        # features = pca.fit_transform(features)
        np.savetxt(filenames_output, filenames.astype(str), fmt='%s', delimiter=',')
        np.savetxt(labels_output, classes.astype(str), fmt='%s', delimiter=',')
        np.savetxt(features_output, features, delimiter=',')

if __name__ == '__main__':
    os.path.join('')
    parser = argparse.ArgumentParser()
    parser.add_argument('-dataset_folder', help="Folder containing image dataset")
    parser.add_argument('-network', default='inception_v3', help='default: \'inception_v3\'; Networks avaliable: %s' % list(modules.keys()))
    parser.add_argument('-batch_size', default=16, type=int, help="default: 16")
    args = parser.parse_args()

    run(dataset_folder=args.dataset_folder, network=args.network, batch_size=args.batch_size)


from typing import Any

import flet as ft

from todo_app.models import TaskModel
from todo_app.settings import TASKS_FILE

class TaskControl(ft.UserControl):
    def __init__(self, parent: ft.Control, task_model: TaskModel):
        super().__init__()
        self.parent: ft.Control = parent
        self.task_model: TaskModel = task_model

        self.display_task = ft.Checkbox(
            value=self.task_model.completed,
            label=self.task_model.name,
            on_change=self.status_changed,
        )
        self.display_view: ft.Row | None = None
        self.edit_text = ft.TextField(value=self.task_model.name, expand=1)
        self.edit_view: ft.Row | None = None

    def build(self) -> ft.Control | list[ft.Control]:
        self.display_view = ft.Row(
            alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
            vertical_alignment=ft.CrossAxisAlignment.CENTER,
            controls=[
                self.display_task,
                ft.Row(
                    spacing=0,
                    controls=[
                        ft.IconButton(
                            icon=ft.icons.CREATE_OUTLINED,
                            tooltip="Edit To-Do",
                            on_click=self.edit_clicked,
                        ),
                        ft.IconButton(
                            ft.icons.DELETE_OUTLINE,
                            tooltip="Delete To-Do",
                            on_click=self.task_delete,
                        ),
                    ],
                ),
            ],
        )

        self.edit_view = ft.Row(
            visible=False,
            alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
            vertical_alignment=ft.CrossAxisAlignment.CENTER,
            controls=[
                self.edit_text,
                ft.IconButton(
                    icon=ft.icons.DONE_OUTLINE_OUTLINED,
                    icon_color=ft.colors.GREEN,
                    tooltip="Update To-Do",
                    on_click=self.save_clicked,
                ),
            ],
        )
        return ft.Column(controls=[self.display_view, self.edit_view])

    def status_changed(
        self,
        event: ft.ControlEvent,  # pylint: disable=unused-argument
    ) -> None:
        self.task_model.completed = self.display_task.value
        self.parent.task_updated()

    def edit_clicked(
        self,
        event: ft.ControlEvent,  # pylint: disable=unused-argument
    ) -> None:
        self.edit_text.value = self.task_model.name

        if self.display_view is not None:
            self.display_view.visible = False

        if self.edit_view is not None:
            self.edit_view.visible = True

        self.update()

    def save_clicked(
        self,
        event: ft.ControlEvent,  # pylint: disable=unused-argument
    ) -> None:
        self.task_model.name = self.edit_text.value

        if self.display_view is not None:
            self.display_view.visible = True

        if self.edit_view is not None:
            self.edit_view.visible = False

        self.update()
        self.parent.update()

    def task_delete(
        self,
        event: ft.ControlEvent,  # pylint: disable=unused-argument
    ) -> None:
        self.parent.task_delete(self)

class TodoApp(ft.UserControl):
    def __init__(self, task_models_data: list[dict[Any, Any]]):
        super().__init__()

        task_models = [TaskModel(**task_model_data) for task_model_data in task_models_data]
        self.task_models = {task_model.task_id: task_model for task_model in task_models}
        self.filter = ft.Tabs(
            on_change=self.tabs_changed,
            tabs=[ft.Tab(text="all"), ft.Tab(text="active"), ft.Tab(text="completed")],
        )
        self.new_task: ft.TextField | None = None
        self.tasks_view: ft.Column | None = None
        self.items_left = ft.Text("0 items left")

    def build(self) -> ft.Control | list[ft.Control]:
        self.new_task = ft.TextField(hint_text="Whats needs to be done?", expand=True)
        self.tasks_view = ft.Column()

        view = ft.Column(
            width=600,
            controls=[
                ft.Row(
                    controls=[
                        self.new_task,
                        ft.FloatingActionButton(icon=ft.icons.ADD, on_click=self.add_clicked),
                    ],
                ),
                ft.Column(
                    spacing=25,
                    controls=[
                        self.filter,
                        self.tasks_view,
                    ],
                ),
                ft.Row(
                    alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
                    vertical_alignment=ft.CrossAxisAlignment.CENTER,
                    controls=[
                        self.items_left,
                        ft.OutlinedButton(text="Clear completed", on_click=self.clear_clicked),
                    ],
                ),
            ],
        )

        self.sync_task_list()
        return view

    def clear_clicked(
        self,
        event: ft.ControlEvent,  # pylint: disable=unused-argument
    ) -> None:
        completed_task_controls = list(
            filter(lambda task: task.task_model.completed, self.tasks_view.controls)  # type: ignore [arg-type, union-attr]  # noqa  # pylint: disable=line-too-long
        )

        for task_control in completed_task_controls:
            del self.task_models[task_control.task_model.task_id]

        self.save_tasks_to_file()
        self.update()

    def tabs_changed(
        self,
        event: ft.ControlEvent,  # pylint: disable=unused-argument
    ) -> None:
        self.update()

    def task_updated(self) -> None:
        self.save_tasks_to_file()
        self.update()

    def update(self) -> None:
        self.sync_task_list()
        super().update()

    def sync_task_list(self) -> None:
        # update/sync tasks list
        if self.tasks_view is not None:
            self.tasks_view.controls.clear()

            for task_model in self.task_models.values():
                task = TaskControl(parent=self, task_model=task_model)
                self.tasks_view.controls.append(task)

        status = self.filter.tabs[self.filter.selected_index].text
        count = 0

        for task_control in self.tasks_view.controls:
            task_control.visible = (
                status == "all"
                or (status == "active" and not task_control.task_model.completed)
                or (status == "completed" and task_control.task_model.completed)
            )

            if not task_control.task_model.completed:
                count += 1

        self.items_left.value = f"{count} active item(s) left"

    def add_clicked(
        self,
        event: ft.ControlEvent,  # pylint: disable=unused-argument
    ) -> None:
        if self.new_task is not None and self.new_task.value:
            task = TaskModel(
                name=self.new_task.value,
            )
            self.task_models[task.task_id] = task
            self.new_task.value = ""

            self.save_tasks_to_file()
            self.update()

    def save_tasks_to_file(self) -> None:
        with open(TASKS_FILE, mode="w", encoding="utf-8") as file:
            for task_model in self.task_models.values():
                file.write(task_model.json() + "\n")

    def task_delete(self, task_control: TaskControl) -> None:
        del self.task_models[task_control.task_model.task_id]

        if self.tasks_view is not None:
            self.tasks_view.controls.remove(task_control)

        self.save_tasks_to_file()
        self.update()


import img2pdf
from PIL import Image
from fpdf import FPDF
from docx2pdf import convert
from moviepy.editor import *
import mammoth

#modules
def jpg_to_pdf():#jpg_to_pdf_single
    img_path='test/blr.jpg'
    pdf_path='test/blr.pdf'
    image = Image.open(img_path)
    pdf_bytes = img2pdf.convert(image.filename)
    file = open(pdf_path, "wb")
    file.write(pdf_bytes)
    image.close()
    file.close()

def jpg_to_png(): #jpg_to_png
   Image.open("test/blr.jpg").save("test/sample1.png")

def jpg_to_pdf_multiple(): #jpg_to_pdf_multiple
    x=y=0
    pdf = FPDF()
    imagelist=['test/test1.jpg','test/test2.jpg','test/test3.jpg']
    for i in range(len(imagelist)):
        img=Image.open(imagelist[i])
        w,h=img.size
        w,h=float(w * 0.264583), float(h * 0.264583)
        pdf_size = {'P': {'w': 210, 'h': 297}, 'L': {'w': 297, 'h': 210}}
        orientation = 'P' if w < h else 'L'
        w = w if w < pdf_size[orientation]['w'] else pdf_size[orientation]['w']
        h = h if h < pdf_size[orientation]['h'] else pdf_size[orientation]['h']
        pdf.add_page(orientation=orientation)
        pdf.image(imagelist[i],x,y,w,h)
    pdf.output("test/yourfile.pdf", "F")

def docx_to_pdf():  #docx_to_pdf
    convert("test/input.docx", "test/output.pdf")

def png_to_ico():
    img = Image.open('2.png')
    img.save('logo16.ico',format = 'ICO', sizes=[(32,32)])

def docx_to_html(): #docx_to_html
    f = open("Main/test.docx", 'rb')
    b = open('Main/test1.html', 'wb')
    document = mammoth.convert_to_html(f)
    b.write(document.value.encode('utf8'))
    f.close()
    b.close()
    print('Done!')


from django.conf.urls import url
from .views import (IndexView, AboutView, MessageView, DonateView, ExchangeView, ProjectView,
					QuestionView, LinkView, DetailView)

urlpatterns = [
	# 首页
	url(r'^$', IndexView.as_view(template_name='index.html'), name='index'),  # 主页，自然排序
	url(r'^category/about/$', AboutView, name='about'),  # 给我留言
	url(r'^category/message/$', MessageView, name='message'),  # 关于自己
	url(r'^category/donate/$', DonateView, name='donate'),  # 赞助作者
	url(r'^category/exchange/$', ExchangeView, name='exchange'),  # 技术交流
	url(r'^category/project/$', ProjectView, name='project'),  # 项目合作
	url(r'^category/question/$', QuestionView, name='question'),  # 提问交流
	url(r'^link/$', LinkView, name='link'),  # 申请友情链接
	# 大分类页面
	url(r'^category/(?P<bigslug>.*?)/(?P<slug>.*?)', IndexView.as_view(template_name='content.html'), name='category'),
	# 标签页面
	url(r'^tag/(?P<tag>.*?)/$', IndexView.as_view(template_name='content.html'), name='tag'),
	# 归档页面
	url(r'^date/(?P<year>\d+)/(?P<month>\d+)/$', IndexView.as_view(template_name='archive.html'), name='date'),
	# 文章详情页面
	url(r'^article/(?P<slug>.*?)/$', DetailView.as_view(), name='article'),
]


from tkinter import *

# Funções
def cadastrarAluno():
    nome = nome_entrada.get()
    idade = idade_entrada.get()
    nota = nota_entrada.get()

    aluno = {
        'nome': nome,
        'idade': idade,
        'nota': nota
    }

    alunos.append(aluno)
    print(f"Nome: {aluno['nome']}\n Idade: {aluno['idade']}\n Nota: {aluno['nota']}")

    nome_entrada.delete(0, END)
    idade_entrada.delete(0, END)
    nota_entrada.delete(0, END)

def visualizar():
    for i, aluno in enumerate(alunos):
        print(f"Nome do aluno {i+1}: {aluno['nome']}")
        print(f"Idade do Aluno: {i+1}: {aluno['idade']}")
        print(f"Nota do Aluno {i+1}: {aluno['nota']}")

janela = Tk()
alunos = []

# Label
titulo = Label(text="Sistema de Cadastro")
nome = Label(text="Nome: ")
idade = Label(text="Idade: ")
nota = Label(text="Nota: ")
# Entry
nome_entrada = Entry()
idade_entrada = Entry()
nota_entrada = Entry()
# Botão
botao_cadastrar = Button(text="Cadastrar", command=cadastrarAluno)

# Posicionamento
titulo.grid(row=0, column=0, columnspan=2, padx=10, pady=5)
nome.grid(row=1, column=0, padx=10,pady=5)
nome_entrada.grid(row=1, column=1, padx=10,pady=5)
idade.grid(row=2, column=0, padx=10, pady=5)
idade_entrada.grid(row=2, column=1, padx=10, pady=5)
nota.grid(row=3, column=0, padx=10, pady=5)
nota_entrada.grid(row=3, column=1, padx=10, pady=5)
botao_cadastrar.grid(row=4, column=0, columnspan=2, padx=10, pady=5)

janela.mainloop()


import Live
from _Framework.TransportComponent import TransportComponent
from _Framework.ButtonElement import ButtonElement
class ShiftableTransportComponent(TransportComponent):
    __doc__ = ' TransportComponent that only uses certain buttons if a shift button is pressed '

    def __init__(self):
        TransportComponent.__init__(self)
        self._shift_button = None
        self._quant_toggle_button = None
        self._shift_pressed = False
        self._last_quant_value = Live.Song.RecordingQuantization.rec_q_eight
        self.song().add_midi_recording_quantization_listener(self._on_quantisation_changed)
        self._on_quantisation_changed()

    def disconnect(self):
        TransportComponent.disconnect(self)
        if self._shift_button != None:
            self._shift_button.remove_value_listener(self._shift_value)
            self._shift_button = None
        if self._quant_toggle_button != None:
            self._quant_toggle_button.remove_value_listener(self._quant_toggle_value)
            self._quant_toggle_button = None
        self.song().remove_midi_recording_quantization_listener(self._on_quantisation_changed)

    def set_shift_button(self, button):
        assert ((button == None) or (isinstance(button, ButtonElement) and button.is_momentary()))
        if self._shift_button != button:
            if self._shift_button != None:
                self._shift_button.remove_value_listener(self._shift_value)
            self._shift_button = button
            if self._shift_button != None:
                self._shift_button.add_value_listener(self._shift_value)
            #self._rebuild_callback()
            self.update()

    def set_quant_toggle_button(self, button):
        assert ((button == None) or (isinstance(button, ButtonElement) and button.is_momentary()))
        if self._quant_toggle_button != button:
            if self._quant_toggle_button != None:
                self._quant_toggle_button.remove_value_listener(self._quant_toggle_value)
            self._quant_toggle_button = button
            if self._quant_toggle_button != None:
                self._quant_toggle_button.add_value_listener(self._quant_toggle_value)
            #self._rebuild_callback()
            self.update()

    def update(self):
        self._on_metronome_changed()
        self._on_overdub_changed()
        self._on_quantisation_changed()

    def _shift_value(self, value):
        assert (self._shift_button != None)
        assert (value in range(128))
        self._shift_pressed = value != 0
        if self.is_enabled():
            self.update()

    def _metronome_value(self, value):
        if not self._shift_pressed:
            TransportComponent._metronome_value(self, value)

    def _overdub_value(self, value):
        if not self._shift_pressed:
            TransportComponent._overdub_value(self, value)

    def _quant_toggle_value(self, value):
        assert (self._quant_toggle_button != None)
        assert (value in range(128))
        assert (self._last_quant_value != Live.Song.RecordingQuantization.rec_q_no_q)
        if (self.is_enabled() and (not self._shift_pressed)):
            if ((value != 0) or (not self._quant_toggle_button.is_momentary())):
                quant_value = self.song().midi_recording_quantization
                if (quant_value != Live.Song.RecordingQuantization.rec_q_no_q):
                    self._last_quant_value = quant_value
                    self.song().midi_recording_quantization = Live.Song.RecordingQuantization.rec_q_no_q
                else:
                    self.song().midi_recording_quantization = self._last_quant_value

    def _on_metronome_changed(self):
        if not self._shift_pressed:
            TransportComponent._on_metronome_changed(self)

    def _on_overdub_changed(self):
        if not self._shift_pressed:
            TransportComponent._on_overdub_changed(self)

    def _on_quantisation_changed(self):
        if self.is_enabled():
            quant_value = self.song().midi_recording_quantization
            quant_on = (quant_value != Live.Song.RecordingQuantization.rec_q_no_q)
            if quant_on:
                self._last_quant_value = quant_value
            if ((not self._shift_pressed) and (self._quant_toggle_button != None)):
                if quant_on:
                    self._quant_toggle_button.turn_on()
                else:
                    self._quant_toggle_button.turn_off()


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

from django.db import models, migrations
import django.contrib.auth.models
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        ('Proyecto', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='proyecto',
            name='equipo',
            field=models.ManyToManyField(related_name='equipo', db_constraint=django.contrib.auth.models.Group, null=True, to=settings.AUTH_USER_MODEL, blank=True),
            preserve_default=True,
        ),
    ]