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

# Let's import what we need
import os
import sys
import time
import shutil
import random
import urllib3
import argparse
import threading
from re import search, findall
from requests import get, post, codes
try:
    from urllib.parse import urlparse # for python3
except ImportError:
    from urlparse import urlparse # for python2

colors = True # Output should be colored
machine = sys.platform # Detecting the os of current system
if machine.startswith('os') or machine.startswith('win') or machine.startswith('darwin') or machine.startswith('ios'):
    colors = False # Colors shouldn't be displayed in mac & windows
if not colors:
    end = red = white = green = yellow = run = bad = good = info = que =  ''
else:
    end = '\033[1;m'
    red = '\033[91m'
    white = '\033[1;97m'
    green = '\033[1;32m'
    yellow = '\033[1;33m'
    run = '\033[1;97m[~]\033[1;m'
    bad = '\033[1;31m[-]\033[1;m'
    good = '\033[1;32m[+]\033[1;m'
    info = '\033[1;33m[!]\033[1;m'
    que =  '\033[1;34m[?]\033[1;m'

# Just a fancy ass banner
print ('''%s      ____  __          __
     / %s__%s \/ /_  ____  / /_____  ____
    / %s/_/%s / __ \/ %s__%s \/ __/ %s__%s \/ __ \\
   / ____/ / / / %s/_/%s / /_/ %s/_/%s / / / /
  /_/   /_/ /_/\____/\__/\____/_/ /_/ %s\n''' %
  (red, white, red, white, red, white, red, white, red, white, red, white, red, end))

urllib3.disable_warnings() # Disable SSL related warnings

# Processing command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--url', help='root url', dest='root')
parser.add_argument('-c', '--cookie', help='cookie', dest='cook')
parser.add_argument('-r', '--regex', help='regex pattern', dest='regex')
parser.add_argument('-s', '--seeds', help='additional seed urls', dest='seeds')
parser.add_argument('-l', '--level', help='levels to crawl', dest='level', type=int)
parser.add_argument('-t', '--threads', help='number of threads', dest='threads', type=int)
parser.add_argument('-n', '--ninja', help='ninja mode', dest='ninja', action='store_true')
parser.add_argument('-d', '--delay', help='delay between requests', dest='delay', type=int)
args = parser.parse_args()

if args.root: # if the user has supplied a url
    main_inp = args.root
    if main_inp.endswith('/'): # if the url ends with '/'
        main_inp = main_inp[:-1] # we will remove it as it can cause problems later in the code
else: # if the user hasn't supplied a url
    print ('\n' + parser.format_help().lower())
    quit()

delay = 0 # Delay between requests
cook = None # Cookie
ninja = False # Ninja mode toggle
crawl_level = 2 # Crawling level
thread_count = 2 # Number of threads

if args.cook:
    cook = args.cook
if args.ninja:
    ninja = True
if args.delay:
    delay = args.delay
if args.level:
    crawl_level = args.level
if args.threads:
    thread_count = args.threads

# Variables we are gonna use later to store stuff
files = set() # pdf, css, png etc.
intel = set() # emails, website accounts, aws buckets etc.
robots = set() # entries of robots.txt
custom = set() # string extracted by custom regex pattern
failed = set() # urls that photon failed to crawl
storage = set() # urls that belong to the target i.e. in-scope
scripts = set() # javascript files
external = set() # urls that don't belong to the target i.e. out-of-scope
fuzzable = set() # urls that have get params in them e.g. example.com/page.php?id=2
endpoints = set() # urls found from javascript files
processed = set() # urls that have been crawled

seeds = '' # custom urls to crawl
if args.seeds: # if the user has supplied custom seeds
    seeds = args.seeds
    for seed in seeds.split(','): # we will convert them into a list
        storage.add(seed) # and them to storage for crawling

# If the user hasn't supplied the root url with http(s), we will handle it
if main_inp.startswith('http'):
    main_url = main_inp
else:
    try:
        get('https://' + main_inp)
        main_url = 'https://' + main_inp
    except:
        main_url = 'http://' + main_inp

storage.add(main_url) # adding the root url to storage for crawling

name = urlparse(main_url).netloc # Extracts domain out of the url

####
# This function makes requests to webpage and returns response body
####

# list of user agents
user_agents = ['Mozilla/5.0 (X11; Linux i686; rv:60.0) Gecko/20100101 Firefox/60.0',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 OPR/43.0.2442.991']

def requester(url):
    processed.add(url) # mark the url as crawled
    time.sleep(delay) # pause/sleep the program for specified time
    try:
        def normal(url):
            headers = {
            'Host' : name, # ummm this is the hostname?
            'User-Agent' : random.choice(user_agents), # selecting a random user-agent
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language' : 'en-US,en;q=0.5',
            'Accept-Encoding' : 'gzip',
            'DNT' : '1',
            'Connection' : 'close'}
            # make request and return response
            return get(url, cookies=cook, headers=headers, verify=False)

        # pixlr.com API
        def pixlr(url):
            if url == main_url:
                url = main_url + '/' # because pixlr throws error if http://example.com is used
            # make request and return response
            return get('https://pixlr.com/proxy/?url=' + url, headers={'Accept-Encoding' : 'gzip'}, verify=False)

        # codebeautify.org API
        def code_beautify(url):
            headers = {
            'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0',
            'Accept' : 'text/plain, */*; q=0.01',
            'Accept-Encoding' : 'gzip',
            'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8',
            'Origin' : 'https://codebeautify.org',
            'Connection' : 'close'
            }
            # make request and return response
            return post('https://codebeautify.com/URLService', headers=headers, data='path=' + url, verify=False)

        # www.photopea.com API
        def photopea(url):
            # make request and return response
            return get('https://www.photopea.com/mirror.php?url=' + url, verify=False)

        if ninja: # if the ninja mode is enabled
            # select a random request function i.e. random API
            response = random.choice([photopea, normal, pixlr, code_beautify])(url)

            #check if link is not broken
            if response.status_code == codes.ok:
                return response.text  # return response body
            else:
                failed.add(url) # add it to the failed list

        else:
            response = normal(url)

            #check if link is not broken
            if response.status_code == codes.ok:
                return response.text  # return response body
            else:
                failed.add(url) # add it to the failed list

    except: # if photon fails to connect to the url
        failed.add(url) # add it to the failed list

####
# This function extracts links from robots.txt and sitemap.xml
####

def zap(url):
    try:
        response = requester(url + '/robots.txt') # makes request to robots.txt
        if '<body' not in response: # making sure robots.txt isn't some fancy 404 page
            matches = findall(r'Allow: (.*)|Disallow: (.*)', response) # If you know it, you know it
            for match in matches: # iterating over the matches, match is a tuple here
                match = match[0] + match[1] # one item in match will always be empty so will combine both items
                if '/*/' not in match: # if the url doesn't use a wildcard
                    url = main_url + match
                    storage.add(url) # add the url to storage list for crawling
                    robots.add(url) # add the url to robots list
                    if '=' in url: # self-explanatory
                        fuzzable.add(url) # add url to the fuzzable urls list
            if len(matches) > 0: # if there are more than 0 matches
                print ('%s URLs retrieved from robots.txt: %s' % (good, len(robots)))

        response = requester(url + '/sitemap.xml') # makes request to sitemap.xml

        if '<body' not in response: # making sure robots.txt isn't some fancy 404 page
            matches = findall(r'<loc>[^<]*</loc>', response) # regex for extracting urls
            if matches: # if there are any matches
                print ('%s URLs retrieved from sitemap.xml: %s' % (good, len(matches)))
                for match in matches:
                    storage.add(match.split('<loc>')[1][:-6]) #cleaning up the url & adding it to the storage list for crawling
    except:
        pass
####
# This functions checks whether a url should be crawled or not
####

def is_link(url):
    # file extension that don't need to be crawled and are files
    bad = ['xml', 'png', 'bmp', 'jpg', 'jpeg', 'pdf', 'css', 'ico', 'js', 'svg', 'json']
    conclusion = False # whether the the url should be crawled or not

    if url not in processed: # if the url hasn't been crawled already
        if '.' in url[-5:]: # url[-5:] = last 5 chars of url
            extension = url[-5:].split('.')[-1] # finding the extension of webpage
            if extension not in bad: # if the extension isn't "bad"
                conclusion = True # url can be crawled
            else:
                files.add(main_url + url) # its a file
        else: # it has no extension
            return True # url can be crawled
    return conclusion # return the conclusion :D

####
# This function extracts stuff from the response body
####

def extractor(url):
    response = requester(url) # make request to the url
    try:
        matches = findall(r'href=[\'"](.*?)["\']', response) # find all the linkx
        for link in matches: # iterate over the matches
            link = link.split('#')[0] # remove everything after a "#" to deal with in-page anchors
            if is_link(link): # checks if the urls should be crawled
                if link.startswith(main_url): # self-explanatory
                    storage.add(link)
                    if '=' in link:
                        fuzzable.add(link)
                if link.startswith('http') or link.startswith('//'):
                    if not link.startswith(main_url):
                        external.add(link)
                elif link.startswith('javascript') or link.startswith('{{'):
                    pass # we aren't fucking with javascript
                elif link.startswith('/') and not link.startswith('//'):
                    storage.add(main_url + link)
                    if '=' in link:
                        fuzzable.add(link)
                elif not link.startswith('http') and not link.startswith('//'):
                    storage.add(main_url + '/' + link)
                    if '=' in link:
                        fuzzable.add(link)

        matches = findall(r'src=[\'"](.*?\.js)["\']', response) # extract .js files
        for match in matches: # iterate over the matches
            if match.startswith(main_url):
                scripts.add(match)
            elif match.startswith('/') and not match.startswith('//'):
                scripts.add(main_url + match)
            elif not match.startswith('http') and not match.startswith('//'):
                scripts.add(main_url + '/' + match)
        # extract intel ;)
        matches = findall(r'''([\w\.-]+s[\w\.-]+\.amazonaws\.com)|(github\.com/[\w\.-/]+)|(facebook\.com/.*?)[\'" ]|
(youtube\.com/.*?)[\'" ]|(linkedin\.com/.*?)[\'" ]|(twitter\.com/.*?)[\'" ]|([\w\.-]+@[\w\.-]+\.[\.\w]+)''', response)
        for match in matches: # iterate over the matches
            for x in match: # iterate over the match because it's a tuple
                if x != '': # if the value isn't empty
                    intel.add(x) # add it to intel list

        def regxy(pattern):
            matches = findall(r'%s' % pattern, response)
            for match in matches:
                custom.add(match)
        if args.regex:
            regxy(args.regex)

    except: # if something bad happens
        failed.add(url)

####
# This function extracts endpoints from JavaScript Code
####

def jscanner(url):
    try:
        response = requester(url) # make request to the url
        matches = findall(r'[\'"](/.*?)[\'"]|[\'"](http.*?)[\'"]', response) # extract urls/endpoints
        for match in matches: # iterate over the matches, match is a tuple
            match = match[0] + match[1] # combining the items because one of them is always empty
            if not search(r'[}{><"\']', match) and not match == '/': # making sure it's not some js code
                endpoints.add(match) # add it to the endpoints list
    except:
        failed.add(url)

####
# This function starts multiple threads for a function
####

def threader(function, *urls):
    threads = [] # list of threads
    urls = urls[0] # because urls is a tuple
    for url in urls: # iterating over urls
        task = threading.Thread(target=function, args=(url,))
        threads.append(task)
    # start threads
    for thread in threads:
        thread.start()
    # wait for all threads to complete their work
    for thread in threads:
        thread.join()
    # delete threads
    del threads[:]

####
# This function processes the urls and sends them to "threader" function
####

def flash(function, links): # This shit is complicated, move on
    links = list(links) # convert links (set) to list
    begin = 0 # begining of slice
    end = thread_count # ending of slice
    # Okay I give up, if you know it, you know it
    for i in range((int(len(links)/thread_count)) + 1):
        splitted = links[begin:end]
        threader(function, splitted)
        begin += thread_count
        if (len(links) - end) >= thread_count:
            end += thread_count
        else:
            end += len(links) - begin
        progress = begin
        if progress > len(links):
            progress = len(links)
        sys.stdout.write('\r%s Progress: %i/%i' % (info, progress, len(links)))
        sys.stdout.flush()
    print ('')

then = time.time() # records the time at which crawling started

# Step 1. Extract urls from robots.txt & sitemap.xml
zap(main_url)

# Step 2. Crawl recursively to the limit specified in "crawl_level"
for level in range(crawl_level):
    links = storage - processed # links to crawl = all links - already crawled links
    if len(links) == 0: # if links to crawl are 0 i.e. all links have been crawled
        break
    if len(storage) <= len(processed): # if crawled links are somehow more than all links. Possible? ;/
        if len(storage) > 2 + len(seeds): # if you know it, you know it
            break
    print ('%s Level %i: %i URLs' % (run, level + 1, len(links)))
    try:
        flash(extractor, links)
    except KeyboardInterrupt:
        print ('')
        break

# Step 3. Scan the JavaScript files for enpoints
print ('%s Crawling %i JavaScript files' % (run, len(scripts)))
flash(jscanner, scripts)

now = time.time() # records the time at which crawling stopped
diff = (now  - then) # finds total time taken

def timer(diff):
    minutes, seconds = divmod(diff, 60) # Changes seconds into minutes and seconds
    time_per_request = diff / float(len(processed)) # Finds average time taken by requests
    return minutes, seconds, time_per_request
time_taken = timer(diff)
minutes = time_taken[0]
seconds = time_taken[1]
time_per_request = time_taken[2]
# Step 4. Save the results
if os.path.exists(name): # if the directory already exists
    shutil.rmtree(name, ignore_errors=True) # delete it, recursively
os.mkdir(name) # create a new directory

with open('%s/links.txt' % name, 'w+') as f:
    for x in storage:
        f.write(x + '\n')

with open('%s/files.txt' % name, 'w+') as f:
    for x in files:
        f.write(x + '\n')

with open('%s/intel.txt' % name, 'w+') as f:
    for x in intel:
        f.write(x + '\n')

with open('%s/robots.txt' % name, 'w+') as f:
    for x in storage:
        f.write(x + '\n')

with open('%s/failed.txt' % name, 'w+') as f:
    for x in failed:
        f.write(x + '\n')

with open('%s/custom.txt' % name, 'w+') as f:
    for x in custom:
        f.write(x + '\n')

with open('%s/scripts.txt' % name, 'w+') as f:
    for x in scripts:
        f.write(x + '\n')

with open('%s/fuzzable.txt' % name, 'w+') as f:
    for x in fuzzable:
        f.write(x + '\n')

with open('%s/external.txt' % name, 'w+') as f:
    for x in external:
        f.write(x + '\n')

with open('%s/endpoints.txt' % name, 'w+') as f:
    for x in endpoints:
        f.write(x + '\n')

# Printing out results
print ('''%s
%s URLs: %i
%s Intel: %i
%s Files: %i
%s Endpoints: %i
%s Fuzzable URLs: %i
%s Custom strings: %i
%s JavaScript Files: %i
%s External References: %i
%s''' % ((('%s-%s' % (red, end)) * 50), good, len(storage), good,
len(intel), good, len(files), good, len(endpoints), good, len(fuzzable), good,
len(custom), good, len(scripts), good, len(external),
(('%s-%s' % (red, end)) * 50)))

print ('%s Total time taken: %i:%i' % (info, minutes, seconds))
print ('%s Average request time: %s' % (info, str(time_per_request)[:4]))

if not colors: # if colors are disabled
    print ('%s Results saved in %s directory' % (good, name))
else:
    print ('%s Results saved in \033[;1m%s\033[0m directory' % (good, name))


import unittest
from src.utility.file import read_lines, read_lines_as_numbers

class TestUtility(unittest.TestCase):

    def test_read_lines(self):
        res = read_lines("src/utility/utility_test_input.txt")
        self.assertEqual(len(res), 3)
        self.assertEqual(res[0], "abc")

    def test_read_lines_throwsIfNonExistingFile(self):
        with self.assertRaises(FileNotFoundError):
            read_lines("src/utility/utility_test_input_non_existing.txt")

        with self.assertRaises(FileNotFoundError):
            read_lines("")

    def test_read_lines_as_numbers(self):
        res = read_lines_as_numbers(
            "src/utility/utility_test_input_numbers.txt")
        self.assertEqual(len(res), 3)
        self.assertEqual(res[0], 123)
        self.assertEqual(res[2], -789)

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


import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from window2 import Ventana
from gi.repository import GdkPixbuf
from random import randrange

class Celda(Gtk.EventBox):
    name = None
    image = Gtk.Image()
    imageM = Gtk.Image()
    jugador = None
    maquina = None
    resultado = None

    def __init__(self, name, image):
        super().__init__()
        self.name = name
        self.image = image
        box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=4)
        box.pack_start(image, True, True, 0)
        self.add(box)
        self.connect("button-release-event", self.on_click)

    def on_click(self, widget, event):
        self.seleccionJugador()
        self.seleccionMaquina()
        self.getImagePlayer()
        self.getImageMachine()

        self.result()

        win = Ventana(self.image, self.imageM,self.resultado)
        win.show_all()
        Gtk.main()

    def seleccionJugador(self):
        if self.name=="piedra":
            self.jugador=1
        if self.name=="papel":
            self.jugador=2
        if self.name=="tijera":
            self.jugador=3

    def seleccionMaquina(self):
        maquina = randrange(1, 4)
        self.maquina=maquina

    def getImagePlayer(self):
        img = Gtk.Image()
        pixbuf = None
        if self.name == "piedra":
            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale("data/piedra.jfif", 200, 200, False)
        elif self.name == "papel":
            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale("data/papel.jfif", 200, 200, False)
        elif self.name == "tijera":
            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale("data/tijera.jfif", 200, 200, False)
        img.set_from_pixbuf(pixbuf)
        self.image = img

    def getImageMachine(self):
        img = Gtk.Image()
        pixbuf = None
        if self.maquina == 1:
            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale("data/piedra.jfif", 200, 200, False)
        elif self.maquina == 2:
            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale("data/papel.jfif", 200, 200, False)
        elif self.maquina == 3:
            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale("data/tijera.jfif", 200, 200, False)
        img.set_from_pixbuf(pixbuf)
        self.imageM=img

    def result(self):
        result = None
        if self.jugador==self.maquina:
            result= "Habeis seleccionado lo mismo, habeis empatado!"
        if self.jugador==1 and self.maquina==2:
            result= "Has seleccionado piedra, la máquina ha seleccionado papel, has perdido!"
        if self.jugador==1 and self.maquina==3:
            result = "Has seleccionado piedra, la máquina ha seleccionado tijeras, has ganado!"
        if self.jugador==2 and self.maquina==1:
            result ="Has seleccionado papel, la máquina ha seleccionado piedra, has ganado!"
        if self.jugador==2 and self.maquina==3:
            result = "Has seleccionado papel, la máquina ha seleccionado tijeras, has perdido!"
        if self.jugador==3 and self.maquina==1:
            result = "Has seleccionado tijeras, la máquina ha seleccionado piedra, has perdido!"
        if self.jugador==3 and self.maquina==2:
            result = "Has seleccionado tijeras, la máquina ha seleccionado papel, has ganado!"

        self.resultado=result


import tweepy
import logging
from config import crearAPI
import time
import API.weatherAPI as weatherAPI

logging.basicConfig(level=logging.INFO)

logger = logging.getLogger()

# since_id especifica resultados con una ID mayor que la id especificada (más reciente)

def verificarMenciones(api, palabrasClaves, idDesde):

    logger.info("Recuperando menciones...")

    idDesdeNuevo = idDesde

    # Cursor maneja la paginacion de resultados
    for tweet in tweepy.Cursor(api.mentions_timeline, since_id=idDesde).items():

        idDesdeNuevo = max(tweet.id, idDesdeNuevo)

        me = api.me()

        if any(palabraClave in tweet.text.lower() for palabraClave in palabrasClaves) and tweet.user.id != me.id:

            logger.info(f"Respondiendo al usuario {tweet.user.screen_name}")

            if not tweet.user.following:

                logger.info(f"Siguiendo al usuario {tweet.user.screen_name}")

                tweet.user.follow()

            if not tweet.favorited:

                logger.info("Dando me gusta al tweet...")

                tweet.favorite()

            usuario = tweet.user.screen_name

            ciudad = 'Mendoza, AR'

            clima = weatherAPI.getClima(ciudad)

            hora = time.strftime("%H:%M:%S")

            clima += f"\nHora: {hora}"

            # Se responde al tweet que nos mencionaron
            try:
                api.update_status(f"@{usuario} {clima}",
                                  in_reply_to_status_id=tweet.id)
            except Exception as e:
                logger.info(
                    "No se pudo responder, el tweet está duplicado.", exc_info=True)

    return idDesdeNuevo

def main():
    api = crearAPI()
    idDesde = 1

    while True:

        idDesde = verificarMenciones(
            api, ["getclima"], idDesde)

        logger.info("Esperando para volver a verificar en 1 minuto...")

        time.sleep(60)

if __name__ == "__main__":
    main()


#!/usr/bin/python3
# _*_coding=utf-8 _*_
# @author junwei
# @date 2021/5/29 22:31
# description 栈（顺序表实现） FILO
class Stack(object):
    def __init__(self, size=5):
        self.size = size
        self.stack = [None] * size
        self.top = -1

    def is_empty(self):
        """是否栈空"""
        return self.top == -1

    def is_full(self):
        """是否栈满"""
        return self.size == self.top + 1

    def push(self, item):
        """添加"""
        if self.is_full():
            raise Exception('stack is full')
        else:
            self.stack[self.top + 1] = item
            self.top += 1

    def pop(self):
        """弹出"""
        if self.is_empty():
            raise Exception('stack is empty')
        else:
            self.stack[self.top] = None
            self.top -= 1

    def peek(self):
        """返回栈顶元素"""
        return self.stack[self.top]

if __name__ == "__main__":
    stack = Stack()
    stack.push("a")
    stack.push("b")
    stack.push("c")
    print("after push:", stack.stack)
    print('peek:', stack.peek())
    stack.pop()
    print("after pop:", stack.stack)


# Repository: https://gitlab.com/quantify-os/quantify-core
# Licensed according to the LICENCE file on the main branch
"""Module containing the types for use with the analysis classes"""
from collections import UserDict

from jsonschema import validate

from quantify_core.utilities.general import load_json_schema

# pylint: disable=too-few-public-methods
# WARNING! Do not inherit from dict! if you do, `AnalysisSettings.update will skip the
# validation done in `__setitem__`.
class AnalysisSettings(UserDict):
    """
    Analysis settings with built-in schema validations.

    .. jsonschema:: schemas/AnalysisSettings.json#/configurations
    """

    schema = load_json_schema(__file__, "AnalysisSettings.json")["configurations"]
    schema_individual = dict(schema)
    schema_individual.pop("required")

    def __init__(self, settings: dict = None):
        """Initializes and validates the passed settings."""
        super().__init__()
        if settings:
            validate(settings, self.schema)
            for key, value in settings.items():
                super().__setitem__(key, value)

    def __setitem__(self, key, value):
        """Items are validated before assigning."""
        validate({key: value}, self.schema_individual)
        super().__setitem__(key, value)


from django.shortcuts import render
import json
import requests

def index(request):
    return render(request, 'index.html')

def weather(request):
    if request.method == 'POST':
        # collect data from index and instantiate to city
        city = request.POST['city']

        response = requests.get('https://api.openweathermap.org/data/2.5/weather?q='+city+'&appid={api_key}')
        # converting the response to json data
        json_data = response.json()

        #creating a dictionary with the json_data for reference in the templates
        data = {
            "country" : str(json_data['name']),
            "country_code" : str(json_data['sys']['country']),
            "coordinate": str(json_data['coord']['lon']) + ' ' + str(json_data['coord']['lat']),
            "temp": str(json_data['main']['temp']) + 'k',
            "pressure": str(json_data['main']['pressure']),
            "humidity":str(json_data['main']['humidity']),
        }

    context = {
        'city': city, 'data': data
    }
    return render(request, 'weather.html', context)


class Node:
    '''Implementation without a linked list controller nor an indicator for the head and tail'''
    def __init__(self, data):
        self.data = data
        self.next = None

    def append(self, data):
        end = Node(data)
        node = self
        while node.next is not None:
            node = node.next

        node.next = end

    def delete(self, data):
        node = self

        # head case
        if self.data == data:
            if self.count() > 1:
                self.data = self.next.data
                self.next = self.next.next
                return self
            else:
                return "Cannot delete head, no implementation for Node containing None"

        while node.next is not None:
            if node.next.data == data:
                node.next = node.next.next
                return self
            node = node.next

        return self

    def count(self):
        node = self
        if not self or self.data is None:
            return 0
        count = 1
        while node.next is not None:
            count += 1
            node = node.next

        return count

    def __repr__(self):
        next_val = self.next.data if self.next else None
        return "Node<data:{0} next:{1}>".format(self.data, next_val)

def print_nodes(node):
    '''Print out all nodes in the list'''
    fmt = '%d->'
    out = ''

    curr = node
    while curr.next is not None:
        out += fmt % curr.data
        curr = curr.next

    out += '%d' % curr.data

    return out


#Bond Calculator - calculates Macaulay, Modified, Convexity, Price
#Zubin Meyer

#declare and assign all of the required inputs from the user
#firstd is for firstderiv weighted cashflows
#secondd is for secondderiv weighted cashflows
#bondcashflow is normal cashflow to calculate price
firstd=0
secondd=0
bondcashflow=0
P=float(input("Please enter principal value: "))
n=int(input("Please enter number of years to maturity: "))
C=float(input("Please enter the coupon (as a %): "))
y=float(input("Please enter the yield (as a %): "))
compound=input("Please enter A/a for annual, S/s for semiannual, Q/q for quarterly: ")

#adjust the coupon and yield to be in dollar and decimal values respectively
C=C*P/100
y/=100

#depending on frequency of bond annual/semiannual/quarterly do the proper adjustments
if compound=="S" or compound =="s":
    n*=2; C/=2; y/=2;
elif compound=="Q" or compound=="q":
    n*=4; C/=4; y/=4;

#calculate the cashflows from the bond and weighted cashflows
for i in range (1,n+1):
    bondcashflow+=(C/((1+y)**i))
    firstd+=((i*C)/((1+y)**i))
    secondd+=(((i+1)*i*C)/((1+y)**(i+2)))

#calculate the principal repayment
flumpsum=(n*P)/((1+y)**n)
slumpsum=(n+1)*n*P/((1+y)**(n+2))

#get numerator and denominator (price) in order
firstd+=flumpsum
secondd+=slumpsum
price=bondcashflow+(flumpsum/n)

#calculate macaulay and convexity
macdur=firstd/price
convexity=secondd/price

#adjust macdur and convexity based on frequency of interest payment
if compound=="S" or compound=="s":
    macdur/=2; convexity/=4;
elif compound=="Q" or compound=="q":
    macdur/=4; convexity/=16;

#calculate modified duration with proper macaulay
moddur=macdur/(1+y)

#show all measures
print("\nPrice of Bond: $", price)
print("Macaulay Duration in Years: ", macdur)
print("Modified Duration in Years: ", moddur)
print("Convexity in Years: ", convexity)
print("Dollar Convexity Measure: ", convexity*price)


STYLE = '''
table, td, tr, th {
    text-align:center;
    margin-left:auto;
    margin-right:auto;
    padding-right:10px;
    padding-left:10px;
    background-color:#e79c9c;
    opacity:90%;
    font-weight:bolder;
    font-family:Courier;
    position:relative;
    border-width:3px;
    border-style:solid;
    border-collapse: collapse;
    border-color:#9D80C6;
    z-index:-1;
    }
body {
    text-align:center;
    background-image: url('https://image-cdn.hypb.st/https%3A%2F%2Fhypebeast.com%2Fimage%2F2020%2F04%2Fpokemon-pikachu-video-chat-backgrounds-download-info-003.jpg?quality=95&w=1170&cbr=1&q=90&fit=max');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
}
h1 {
    font-family:Garamond;
    color:white;
    font-size:70px;
    text-shadow:
    -1px -1px 0 #000,
    1px -1px 0 #000,
    -1px 1px 0 #000,
    1px 1px 0 #000;
}
p {
    margin-left:150px;
    margin-right:150px;
    padding-bottom: 20px;
    font-family:Garamond;
    font-weight:bolder;
    font-size:130%;
}
nav {
    background-color:#9D80C6;
    padding-top:10px;
    padding-bottom:10px;
    opacity:95%;
    z-index:0;
    position: fixed;
    top: 0px;
    width:100%;
    display: flex;
    justify-content: space-around;
}
nav li {
   display: inline-block;
   position: relative;
   font-size:30px;
   font-family:Garamond;
   padding-left:40px;
   padding-right:40px;
}
nav li ul {
   display: none;
   position: absolute;
   width: 200px;
   top: 100%;
   padding: 0;
   background:black;
   padding:none;
}
nav li:hover > ul {
   display: block;
}
nav li > ul > li:hover {
    background-color:white;
}
nav li > ul > li:hover > a:link{
    color:black;
}
nav a:link, nav li{
    text-decoration: none;
    color: white;
}
a:visited:hover {
    color:black;
}
a:visited {
    color:white;
}
h1 {
    padding-top:90px;
}
.gif {
    height:30%;
    width:auto;
}
.Normal{background-color:#A8A878}
.Fire{background-color:#F08030}
.Water{background-color:#6890F0}
.Flying{background-color:#A890F0}
.Grass{background-color:#78C850}
.Poison{background-color:#A040A0}
.Electric{background-color:#F8D030}
.Ground{background-color:#E0C068}
.Psychic{background-color:#F85888}
.Rock{background-color:#B8A038}
.Ice{background-color:#98D8D8}
.Bug{background-color:#A8B820}
.Dragon{background-color:#7038F8}
.Ghost{background-color:#705898}
.Dark{background-color:#705848}
.Fairy{background-color:#e76d88}
.Steel{background-color:#B8B8D0}
.Fighting{background-color:#C03028}
'''

TITLE = 'pokedex'
PARAGRAPH = '''

 I think Pokemon are really cool; I've been watching
the Pokemon franchise since I was young so I grew up with it! I think my favorite Pokemon
has to be Zorua, and maybe Snivy is a close second. In my honest opinion, the Sword and Shield game
(actually any of the newer ones) isn't as bad as hardcore fans make it out to be. The CG is done really
well so it still captures some Pokemon aesthetic while changing to a more modern video game style.
As for my favovrite generation I think it might be X & Y... Why? I don't know. It just stuck with me,
especially the legendaries.

'''

file = open('pokemon/pokemon.csv','r')
data = file.read()

def demo(data):
    data = data.split("\n")
    for y in range(len(data)):
        data[y] = str(data[y])
        data[y] = data[y].split(",")
    return data
data = demo(data)

def makeHTMLTable(data):
    end = ("
\n")
    imag = 1
    i = 0
    end += ("" + '''''' + "	")
    end += ("" + '''''' + "	")
    while i < (len(data[0])):
        if i != 2:
            end += ("")
            end += data[0][i]
            end += ("	")
            i += 1
        elif i == 2:
            end += ("" + "Type(s)" + "	")
            i += 2
    for i in range(1, len(data)-1):
        y = 0
        end += ("
")
        end += ("" + '''''' + "	")
        end += ("" + '''''' + "	")
        end = end.replace("imag", str(imag))
        while y < (len(data[i])):
            if y != 2:
                end += ("    ")
                end += (data[i][y])
                end += ("	")
                y += 1
            elif y == 2:
                end += ("" + "
 typpe
" + "
 tyee
" + "	")
                end = end.replace("typpe", data[i][y])
                end = end.replace("tyee", data[i][y+1])
                y += 2
        end += ("
")
        imag += 1
    end += ("
")
    return end

def makeTypes(data):
    types = []
    for i in range(1, len(data)-1):
        for y in range(2, 4):
            if data[i][y] not in types and data[i][y] != '':
                types += [data[i][y]]
    return types

pokeList = makeTypes(data)

def createNavBar(data):
    nav = '''

Home

All Pokemon

Types

    '''
    navTypes = makeTypes(data)
    for i in range(len(navTypes)):
        nav += '''
x
'''
        nav = nav.replace("x", navTypes[i])
    nav += '''

Top 10

       '''
    return nav

def home():
    site = '''
    <DOCTYPE HTML>

        <title>
          ?TITLE?
        </title>
        <link rel="stylesheet" href="stylesheet.css">

        ?NAV?
        ?HEADING?
        ?PARAGRAPH?

?IMAGE? ?IMAGE? ?IMAGE?

    '''
    site = site.replace("?NAV?", createNavBar(data))
    site = site.replace("?TITLE?", TITLE)
    site = site.replace("?HEADING?", "
Home
")
    site = site.replace("?PARAGRAPH?", PARAGRAPH)
    site = site.replace("?IMAGE?", '''''')
    return site

f = open("home.html", "w")
f.write(home())
f.close()

def allPokemon():
    site = '''
    <DOCTYPE HTML>

        <title>
          ?TITLE?
        </title>
        <link rel="stylesheet" href="stylesheet.css">

        ?NAV?
        ?HEADING?
        ?PARAGRAPH?
        ?BODY?

    '''
    site = site.replace("?HEADING?", "
All Pokemon
")
    site = site.replace("?NAV?", createNavBar(data))
    site = site.replace("?TITLE?", TITLE)
    site = site.replace("?BODY?", makeHTMLTable(data))
    site = site.replace("?PARAGRAPH?", '''
I changed the color of each type per pokemon to a specific
                                             color that I coded to each type. I also combined the type 1
                                             and type 2 columns into one "type(s)" column, containing all
                                             the types for that pokemon.
''')
    return site

f = open("all.html", "w")
f.write(allPokemon())
f.close()

f = open("stylesheet.css", "w")
f.write(STYLE)
f.close()

def makePage(pokeList, pokeType):
    #splitdata = demo(data)
    end = ("
\n")
    #x = 1
    i = 0
    end += ("" + '''''' + "	")
    end += ("" + '''''' + "	")
    while i < (len(data[0])):
        if i != 2:
            end += ("")
            end += data[0][i]
            end += ("	")
            i += 1
        elif i == 2:
            end += ("" + "Type(s)" + "	")
            i += 2
    for i in range(1, len(data)-1):
        if pokeType in data[i]:
            x  = i
            y = 0
            end += ("
")
            end += ("" + '''''' + "	")
            end += ("" + '''''' + "	")
            end = end.replace("imag2", str(x))
            while y < (len(data[i])):
                if y != 2:
                    end += ("")
                    end += (data[i][y])
                    end += ("	")
                    y += 1
                elif y == 2:
                    end += ("" + "
 typpe
" + "
 tyee
" + "	")
                    end = end.replace("typpe", data[i][y])
                    end = end.replace("tyee", data[i][y+1])
                    y += 2
            end += ("
")
    end += ("
")
    return end

for pokeType in pokeList:
    sitetype = '''
    <DOCTYPE HTML>

        <title>
          ?TITLE?
        </title>
        <link rel="stylesheet" href="stylesheet.css">

        ?NAV?

?HEADING?

Here are all the pokemon with the ?PARAGRAPH? type!

        ?BODY?

    '''
    sitetype = sitetype.replace("?HEADING?", pokeType)
    sitetype = sitetype.replace("?NAV?", createNavBar(data))
    sitetype = sitetype.replace("?TITLE?", TITLE)
    sitetype = sitetype.replace("?BODY?", makePage(pokeList, pokeType))
    sitetype = sitetype.replace("?PARAGRAPH?", pokeType)
    type1 = pokeType + ".html"
    f = open(type1, "w")
    f.write(sitetype)
    f.close()

def top10():
    site = '''
    <DOCTYPE HTML>

        <title>
          ?TITLE?
        </title>
        <link rel="stylesheet" href="stylesheet.css">

        ?NAV?

?HEADING?

        ?PARAGRAPH?
        ?BODY?

    '''
    end = ("
\n" + "My Top	")
    count = 0
    i = 0
    end += ("" + '''''' + "	")
    end += ("" + '''''' + "	")
    while i < (len(data[0])):
        if i != 2:
            end += ("")
            end += data[0][i]
            end += ("	")
            i += 1
        elif i == 2:
            end += ("" + "Type(s)" + "	")
            i += 2
    for i in range(1, len(data)-1):
        if data[i][0] == '1' or data[i][0] == '4' or data[i][0] == '7' or data[i][0] == '25' or data[i][0] == '43' or data[i][0] == '113' or data[i][0] == '132' or data[i][0] == '143' or data[i][0] == '145' or data[i][0] == '133':
            x  = i
            y = 0
            end += ("
")
            end += ("" + "count1" + "	" + "" + '''''' + "	")
            end += ("" + '''''' + "	")
            end = end.replace("imag2", str(x))
            count += 1
            end = end.replace("count1", str(count))
            while y < (len(data[i])):
                if y != 2:
                    end += ("")
                    end += (data[i][y])
                    end += ("	")
                    y += 1
                elif y == 2:
                    end += ("" + "
 typpe
" + "
 tyee
" + "	")
                    end = end.replace("typpe", data[i][y])
                    end = end.replace("tyee", data[i][y+1])
                    y += 2
            end += ("
")
    end += ("
")
    site = site.replace("?HEADING?", "Top 10")
    site = site.replace("?NAV?", createNavBar(data))
    site = site.replace("?TITLE?", TITLE)
    site = site.replace("?BODY?", end)
    site = site.replace("?PARAGRAPH?", '''
The first four listed pokemon are some of my favorites since they are very first starters,
                                             including Pikachu, which brings back a lot of nostalgia for me. For the others, I just chose
                                             the Pokemon I liked the most, as well as my favorite legendary from these 151 pokemon. Oddish
                                             used to be my favorite pokemon since I saw cute clips of it hopping up and down on YouTube.
                                             Then the others I based off of their iconicness, such as Chansey, being the nurses for the
                                             Pokecenters around the Pokemon universe. And then there's Snorlax, which almost everyone knows,
                                             and then Eevee, who has really cool evolutions that is unique about it.
''')
    return site
f = open("top.html", "w")
f.write(top10())
f.close()

#python yuen.jasmine.py
#python yuen.jasmine.py > all.html


# Divide and Conquer, Sorting and Searching, and Randomized Algorithms
# https://www.coursera.org/learn/algorithms-divide-conquer

from random import randrange, randint
import operator as op
from functools import reduce
from math import log, ceil, floor
import sys
from utils import swap

# week 2
def count_inversions(arr):
	"""
	Returns the number of inversions in an array
		Parameters:
			arr (int[]): An array of integers

		Returns:
			(int): Count of inversions
	"""
	global count
	count = 0
	# implement using merge sort, which easily allows to count inversions during the merge phase
	def merge_sort(arr):
		# exit early if array is empty or there is only one element
		if len(arr) < 2:
			return arr

		mid = floor(len(arr) / 2)
		left = merge_sort(arr[0:mid])
		right = merge_sort(arr[mid:])
		return merge(left, right)

	# helper function to merge arrays and count split inversions
	def merge(a1, a2):
		global count
		res = []
		i = 0
		j = 0
		while True:
			# there are no more elements to look at - exit
			if len(a1) == i and len(a2) == j:
				break

			# array1 was fully merged but one element in array2 remains
			# push the remaining element into the result array
			# and advance to the next iteration of the loop
			if len(a1) == i and len(a2) != j:
				res.append(a2[j])
				j += 1
				continue

			# array2 was fully merged but one element in array1 remains
			# push the remaining element into the result array
			# and advance to the next iteration of the loop
			if len(a1) != i and len(a2) == j:
				res.append(a1[i])
				i += 1
				continue

			# if the element in the second array is larger, this is a split inversion for all remaining elements of a1
			# add the remaining length of a1 to count
			if a1[i] > a2[j]:
				res.append(a2[j])
				j += 1
				count += len(a1) - i
			# if the element in the first array is larger or equal, there is no split inversion - simply advance
			else:
				res.append(a1[i])
				i += 1

		return res

	merge_sort(arr)
	return count

def strassen_mult(X, Y):
	"""
	Returns the multiplication of two matrices using Strassen's method
	Currently only works for even matrices due to partitioning, this can be circumvented using padding
		Parameters:
			X (int[][]): A 2D array of integers
			Y (int[][]): A 2D array of integers

		Returns:
			(int[][]): A 2D array of integers
	"""

	# helper function to split a matrix into 4 sub-matrices
	def split_matrix(M):
		if len(M) == 1 and len(M[0]) == 1:
			return M
		n = len(M)
		half_size = int(n / 2)
		A = empty_matrix(half_size, half_size)
		B = empty_matrix(half_size, half_size )
		C = empty_matrix(half_size, half_size)
		D = empty_matrix(half_size, half_size)

		for i in range(half_size):
			for j in range(half_size):
				A[i][j] = M[i][j]
				B[i][j] = M[i][j + half_size]
				C[i][j] = M[i + half_size][j]
				D[i][j] = M[i + half_size][j + half_size]
		return (A, B, C, D)

	# helper function to create an empty matrix of dimensions x by y
	def empty_matrix(x, y):
		M = [[]] * x
		for i in range(x):
			M[i] = [0] * y
		return M

	def add_matrices(A, B):
		n = len(A)
		M = empty_matrix(n, n)
		for i in range(n):
			for j in range(n):
				M[i][j] = A[i][j] + B[i][j]
		return M

	def subtract_matrices(A, B):
		n = len(A)
		M = empty_matrix(n, n)
		for i in range(n):
			for j in range(n):
				M[i][j] = A[i][j] - B[i][j]
		return M

	n = len(X)
	if n == 1:
		return [[X[0][0] * Y[0][0]]]
	else:
		A, B, C, D = split_matrix(X)
		E, F, G, H = split_matrix(Y)
		# p1 = A(F - H)
		P1 = strassen_mult(A, subtract_matrices(F, H))
		# p2 = (A + B)H
		P2 = strassen_mult(add_matrices(A, B), H)
		# p3 = (C + D)E
		P3 = strassen_mult(add_matrices(C, D), E)
		# p4 = D(G - E)
		P4 = strassen_mult(D, subtract_matrices(G, E))
		# p5 = (A + D)(E + H)
		P5 = strassen_mult(add_matrices(A, D), add_matrices(E, H))
		# p6 = (B - D)(G + H)
		P6 = strassen_mult(subtract_matrices(B, D), add_matrices(G, H))
		# p7 = (A - C)(E + F)
		P7 = strassen_mult(subtract_matrices(A, C), add_matrices(E, F))

		# top left quadrant of the final matrix = P5 + P4 - P2 + P6
		Q1 = add_matrices(add_matrices(P5, P4), subtract_matrices(P6, P2))
		# top right quadrant = P1 + P2
		Q2 = add_matrices(P1, P2)
		# bottom left quadrant = P3 + P4
		Q3 = add_matrices(P3, P4)
		# bottom right quadrant = P1 + P5 - P3 - P7
		Q4 = subtract_matrices(subtract_matrices(add_matrices(P1, P5), P3), P7)

		m = int(n / 2)
		Z = empty_matrix(n, n)
		for i in range(m):
			for j in range(m):
				Z[i][j] = Q1[i][j]
				Z[i][j + m] = Q2[i][j]
				Z[i + m][j] = Q3[i][j]
				Z[i + m][j + m] = Q4[i][j]
		return Z

# week 3
def quick_sort(arr):
	"""
	Sorts an array using quick sort
		Parameters:
			arr (int[]): An array of integers

		Returns:
			(int[]): A sorted array of integers
	"""
	if len(arr) < 2:
		return arr

	def pivot(a):
		pivot_idx = 0
		pivot = a[pivot_idx]

		for i in range(1, len(a)):
			if pivot > a[i]:
				pivot_idx += 1
				swap(a, i , pivot_idx)

		swap(a, 0, pivot_idx)
		return pivot_idx

	pivot_idx = pivot(arr)
	leftArr = arr[0:pivot_idx + 1]
	rightArr = arr[pivot_idx + 1:]
	return quick_sort(leftArr) + quick_sort(rightArr)

# week 4
def min_cut(graph):
	"""
	Calculates the minimal cut of a graph using Karger's randomized contraction algorithm
		Parameters:
			graph (Graph): A graph to be examined

		Returns:
			(dict): A dictionary containing the remaining vertices which represent the minimal cut
	"""
	while len(graph.get_vertices()) > 2:
		v1, v2 = graph.get_random_edge()
		graph.contract_edge(v1, v2)
	return graph.get_vertices()

def min_cut_len(graph, increase_success_rate = False):
	"""
	Calculates the amount of the edges intersecting the minimal cut of a graph
		Parameters:
			graph (Graph): A graph to be examined

		Returns:
			(int): The amount of edges intersecting the minimal cut of a graph
	"""

	# calculate n choose r
	# https://stackoverflow.com/questions/4941753/is-there-a-math-ncr-function-in-python
	def ncr(n, r):
		r = min(r, n - r)
		numer = reduce(op.mul, range(n, n - r, -1), 1)
		denom = reduce(op.mul, range(1, r + 1), 1)
		return numer // denom

	# calculate the amount of attempts that should be executed to increase the probability of a correct result roughly to 1/n
	# where n is the amount of vertices
	# https://en.wikipedia.org/wiki/Karger%27s_algorithm#Success_probability_of_the_contraction_algorithm
	# this is optional as it would make the algorithm too slow for larger graphs
	n = len(graph.get_vertices())
	attempts = ceil(ncr(n, 2) * log(n)) if increase_success_rate else n
	minimum = sys.maxsize
	for _ in range(attempts):
		g = graph.deep_copy()
		result = min_cut(g)
		key = list(result.keys())[0]
		minimum = min(minimum, len(result[key]))
	return minimum

def randomized_selection(arr, k):
	"""
	Finds the kth smallest element in an array of integers
		Parameters:
			arr (int[]): An array of integers
			k (int): the position of the element to look for

		Returns:
			(int): The kth smallest element of the input array
	"""
	assert k > 0, "Second argument must be greater than 0"

	n = len(arr)
	if n == 0:
		return None
	if n == 1:
		return arr[0]

	# generate pivot index at random
	pivot_idx = randrange(0, n)
	pivot = arr[pivot_idx]
	# move pivot to the very end of array
	swap(arr, pivot_idx, n - 1)

	# i represents the tail of the "left side" of the partitioned array (everything that is less than the pivot)
	i = 0
	# j represents the index of the current item being looked at
	j = 0
	while j < n:
		# if at the end of the loop, swap pivot into its rightful place and exit the loop
		if j == n - 1:
			swap(arr, i, j)
			break
		# if the current item is less than the pivot, move it to the left
		if arr[j] < pivot:
			swap(arr, i, j)
			i += 1
			j += 1
		# otherwise simply advance the loop
		else:
			j += 1

	# translate k into computer's idea of index, since we're looking for the kth smallest item, we're looking for (k-1)th index
	search_idx = k - 1
	# if the kth smallest element happens to be the one at ith position, return it
	if i == search_idx:
		return arr[search_idx]
	# if we overshot the element, recurse on the left side of the array
	elif i > search_idx:
		return randomized_selection(arr[0:i + 1], k)
	# if we undershot, recurse on the right side
	else:
		return randomized_selection(arr[i:n], k - i)


from PIL import Image
import os
from tqdm import tqdm
import json
import time
import shutil

def sort_images_by_resolution(input_path, output_root, max_retries=10, retry_delay=0.1):
    if not os.path.exists(output_root):
        os.makedirs(output_root)

    processed_images = set()
    checkpoint_file = os.path.join(output_root, 'checkpoint.json')

    if os.path.exists(checkpoint_file):
        with open(checkpoint_file, 'r') as checkpoint:
            processed_images = set(json.load(checkpoint))

    file_list = os.listdir(input_path)
    total_images = len(file_list)
    remaining_images = total_images - len(processed_images)

    images_to_delete = []

    for filename in tqdm(file_list, desc="Sorting images", total=total_images, initial=total_images - remaining_images):
        if filename in processed_images:
            continue

        input_file = os.path.join(input_path, filename)

        if os.path.isfile(input_file):
            retries = 0
            success = False

            while retries < max_retries:
                try:
                    img = Image.open(input_file)
                    width, height = img.size

                    resolution_dir = os.path.join(output_root, f"{width}x{height}")
                    if not os.path.exists(resolution_dir):
                        os.makedirs(resolution_dir)

                    output_file = os.path.join(resolution_dir, filename)
                    shutil.copy2(input_file, output_file)  # Copy the file

                    images_to_delete.append(input_file)  # Mark for deletion

                    processed_images.add(filename)
                    remaining_images -= 1

                    # Update checkpoint
                    with open(checkpoint_file, 'w') as checkpoint:
                        json.dump(list(processed_images), checkpoint)

                    success = True
                    break

                except Exception as e:
                    print(f"Error processing {filename}: {str(e)}")
                    retries += 1
                    time.sleep(retry_delay)

            if not success:
                print(f"Failed to process {filename} after {max_retries} retries.")

    # Delete the original files
    for file_to_delete in images_to_delete:
        try:
            os.remove(file_to_delete)
        except Exception as e:
            print(f"Error deleting {file_to_delete}: {str(e)}")

    # Remove the checkpoint file
    if os.path.exists(checkpoint_file):
        os.remove(checkpoint_file)

    print("Image sorting completed.")

if __name__ == "__main__":
    input_directory = "D:\My Work - Personal\Genevieve\Preprocessed"
    output_directory = "D:\My Work - Personal\Genevieve\Preprocessed\Sorted"

    sort_images_by_resolution(input_directory, output_directory)


class SelectOptions:
    def __init__(self, value, text):
        self.value = value
        self.text = text

intervals = [
    SelectOptions("15m", "15 Minutes"),
    SelectOptions("1h", "1 Hour"),
    SelectOptions("6h", "6 Hours", ),
    SelectOptions("12h", "12 Hours"),
    SelectOptions("1d", "1 Day"),
    # SelectOptions("1w", "1 Week"),
    # SelectOptions("1M", "1 Month"),
]

features = [
    SelectOptions("Close", "Close"),
    SelectOptions("ROC", "Price of change"),
    SelectOptions("MA", "Moving Average"),
    # SelectOptions("BB", "Bollinger Band")
]


import os
import string
import time
from datetime import datetime
from pathlib import Path

import jwt
from dotenv import load_dotenv
from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles
from starlette import responses

# Create the app
from WebApplication.Controllers import router

app = FastAPI(
    title='Open Chat',
    version='1.0.0'
)

# Add origins in this array to allow them only
origins = [
    "http://localhost",
    "http://localhost:4200",
]

# Add middlewares, origin also comes as middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Mount specific folders
app.mount("/static", StaticFiles(directory="static"), name="static")

# Enable routing
app.include_router(router)

public_routs = [
    '/api/signup',
    '/api/login',
    '/docs',
    '/redoc',
    '/openapi.json',
    '/favicon.ico',
    '/senders'
]

@app.middleware("http")
async def validate_jwt(request: Request, call_next):
    try:
        start_time = time.time()
        application_root_path = str(Path(__file__).parent.parent)
        if load_dotenv(dotenv_path=application_root_path + '/.env'):
            secret_key: string = os.getenv('SECRET_KEY')
            security_algo: string = os.getenv("SECURITY_ALGORITHM")
            if secret_key is None or security_algo is None:
                raise HTTPException(
                    status_code=401,
                    detail='Unable to parse authentication token'
                )
            current_route: string = request.url.path

            # No auth required if it's public url
            if current_route in public_routs:
                response = await call_next(request)
                process_time = time.time() - start_time
                response.headers["X-Process-Time"] = str(process_time)
                return response
            else:
                token: str = request.headers.get('Authorization')
                if token:
                    token = token.split()[1].replace('"','')
                    decoded: dict = jwt.decode(jwt=token, key=secret_key, algorithms=security_algo)
                    if datetime.fromtimestamp(decoded['exp']) >= datetime.now() or current_route in public_routs:
                        response = await call_next(request)
                        process_time = time.time() - start_time
                        response.headers["X-Process-Time"] = str(process_time)
                        return response
                else:
                    return responses.JSONResponse(
                        status_code=401,
                        content='Unauthorized Access'
                    )
        return responses.JSONResponse(
            status_code=401,
            content='Unauthorized Access 2'
        )
    except Exception as e:
        return responses.JSONResponse(
            status_code=500,
            content=str(e)
        )

# @app.middleware('ws')
# async def validate_jwt_for_socket(request: Request, call_next):
#     print(request)
#     print(call_next)
#     return


#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the sockMerchant function below.
def sockMerchant(n, ar):
    ar.sort()
    a=-1
    count=1
    answer=0

    for i in ar:
        if i == a:
            count+=1
        else:
            answer += count//2
            a=i
            count=1
    answer += count//2
    return answer

# print(sockMerchant(9, [10,20,20,10,10,30,50,10,20]))
print(sockMerchant(10, [1,1,3,1 ,2 ,1 ,3 ,3 ,3, 3]))


from argparse import ArgumentParser, REMAINDER

class MbtParams:

    def __init__(self, config, context, prog_name="mbt"):
        self.config = config
        self.context = context
        self.parser = ArgumentParser(prog=prog_name)
        self.requires_topic = False
        self.requires_series = False
        self.requires_variant = False
        self.requires_installation = False

    def add_topic_arg(self):
        if self.requires_topic:
            return
        self.requires_topic = True
        self.parser.add_argument("-t", "--topic",
                                 required=(self.context.topic is None),
                                 help="Description of the changes, part of"
                                 " the feature branch name",
                                 default=self.context.topic
                                 )

    def add_series_arg(self):
        if self.requires_series:
            return
        self.requires_series = True
        self.parser.add_argument("-s", "--series",

                                 required=(self.context.series is None),
                                 help="Release series to work on",
                                 choices=self.config.series,
                                 default=self.context.series
                                 )

    def add_variant_arg(self):
        if self.requires_variant:
            return
        self.requires_variant = True
        self.parser.add_argument("-v", "--variant",
                                 required=(self.context.variant is None),
                                 help="Build variant to use",
                                 choices=self.config.build_configs.keys(),
                                 default=self.context.variant
                                 )

    def add_installation_arg(self):
        if self.requires_installation:
            return
        self.requires_installation = True

        self.add_topic_arg()
        self.add_series_arg()
        self.add_variant_arg()

        self.parser.add_argument("-i", "--installation",
                                 required=(self.context.installation is None),
                                 help="Tag of the installation",
                                 default=self.context.installation
                                 )

    def add_remote_arg(self):
        self.parser.add_argument("-r", "--remote",
                                 required=True,
                                 help="Remote to be used",
                                 choices=self.config.remotes.keys()
                                 )

    def add_remaining_args(self):
        self.parser.add_argument('remaining_args', nargs=REMAINDER)

    def add_boolean_arg(self, name):
        self.parser.add_argument("--"+name, action="store_true")

    def add_int_arg(self, name, default=None):
        self.parser.add_argument("--"+name, type=int, default=default)

    def parse(self, args):
        self.results = self.parser.parse_args(args)
        # Work around an argparse limitation:
        # The first remaining arg can't be an optional, so in this caes,
        # -- has to be specified first
        if "remaining_args" in self.results:
            rargs = self.results.remaining_args
            if len(rargs) and rargs[0] == "--":
                self.results.remaining_args.pop(0)
        return self.results


import argparse
import os
import random
import sys

import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

django.setup()
from datacenter.models import Schoolkid, Lesson, Commendation, Chastisement, Mark

def fetch_schoolkid(child_name):
    child = Schoolkid.objects.get(full_name__contains=child_name)
    return child

def fix_marks(child):
    negative_marks = Mark.objects.filter(schoolkid=child, points__lte=3)
    for mark in negative_marks:
        target_mark = Mark.objects.get(pk=mark.pk)
        target_mark.points = 5
        target_mark.save()

def create_commendation(child, subject_title):
    commendations = [
        'Молодец!',
        'Отлично!',
        'Хорошо!',
        'Гораздо лучше, чем я ожидал!',
        'Ты меня приятно удивил!'
    ]
    year = child.year_of_study
    letter = child.group_letter
    lessons = Lesson.objects.filter(year_of_study=year, group_letter=letter)
    target_lessons = lessons.filter(subject__title=subject_title)
    last_lesson = target_lessons.order_by('-date').first()
    if last_lesson:
        Commendation.objects.create(
            text=random.choice(commendations),
            created=last_lesson.date,
            schoolkid=child,
            subject=last_lesson.subject,
            teacher=last_lesson.teacher
        )
    else:
        raise Lesson.DoesNotExist

def remove_chastisements(child):
    chastisement = Chastisement.objects.filter(schoolkid=child)
    chastisement.delete()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-ln', '--last_name', help='Фамилия ученика', default='Фролов')
    parser.add_argument('-fn', '--first_name', help='Имя ученика', default='Иван')
    parser.add_argument('-p', '--patronymic', help='Отчетсво ученика', default='')
    parser.add_argument('-s', '--subject', help='Название предмета', default='Математика')
    args = parser.parse_args()
    kid_full_name = f'{args.last_name} {args.first_name} {args.patronymic}'
    try:
        schoolkid = fetch_schoolkid(kid_full_name)
    except Schoolkid.MultipleObjectsReturned:
        print('Слишком много учеников с указанным именем. Пожалуйста уточните имя.')
        sys.exit()
    except Schoolkid.DoesNotExist:
        print('Ученик с таким именем отсутствует. Пожалуйста уточните имя.')
        sys.exit()
    print('Начинаю исправлять оценки')
    fix_marks(schoolkid)
    print('Оценки исправлены')
    print('Начинаю удалять замечания')
    remove_chastisements(schoolkid)
    print('Замечания удалены')
    print('Начинаю добавлять похвалу')
    try:
        create_commendation(schoolkid, args.subject)
    except Lesson.DoesNotExist:
        print('Неправильно введено название предмета. Пожалуйста выберети правильный предмет из расписания.')
        sys.exit()
    print('Похвала добавлена')


# test_AddFollow
import json
import logging

import allure
import pytest

from Business.Urlpath.UrlPath_userlogin import UrlPath_add_follow
from Common.login import login
from Common.show_sql import showsql
from Common.sign import get_sign

from Common.requests_library import Requests

from Common.tools.read_write_yaml import yamltoken

from glo import JSON, HTTP

# @pytest.mark.skip(reason="调试中 ")
@allure.feature('主页信息-关注用户')
class TestAddFollow():
    @classmethod
    def setup_class(cls) -> None:
        cls.session = Requests().get_session()
        login()

    def tearDown(self) -> None:
        Requests(self.session).close_session()

    # @pytest.mark.skip(reason="调试中 ")
    def test_AddFollow(self):
        # 关注用户url
        url_add_follow = HTTP + UrlPath_add_follow
        # 拼装headers参数
        header = {}
        header.update(JSON)
        headers = {}
        headers.update(header)
        token = {"token": yamltoken()}
        headers.update(token)  # 将token更新到headers
        # print(headers)
        # 从数据库获取用户id
        userId = showsql(
            '192.168.1.237', 'root', '123456', "user_account",
            "select user_id from t_user_account where `zr_no`= '10000039';"
        )

        paylo = {
            "userId": list(list(userId)[0])[0]
        }
        sign1 = {"sign": get_sign(paylo)}  # 把参数签名后通过sign1传出来
        payload1 = {}
        payload1.update(paylo)
        payload1.update(sign1)

        payload = json.dumps(dict(payload1))

        r = Requests(self.session).post(
            url=url_add_follow, headers=headers, data=payload, title="关注用户"
        )
        j = r.json()
        # print(j)
        assert r.status_code == 200
        try:
            assert j.get("code") == "000000"
            assert j.get("msg") == "ok"
            if j.get("data") == True:
                logging.info("是关注用户")

            else:
                logging.info("不是关注用户")
        except:
            raise AssertionError(
                f"\n请求地址：{url_add_follow}"
                f"\nbody参数：{payload}"
                f"\n请求头部参数：{headers}"
                f"\n返回数据结果：{j}"
            )

        else:
            raise ValueError(f"{j}")


import numpy as np
import pandas as pd
import requests
import xlsxwriter
import math
import requests_cache
import tabulate
import json

from io import StringIO
from tabulate import tabulate

from _secrets import IEX_CLOUD_API_TOKEN

print(f'token: {IEX_CLOUD_API_TOKEN}')

session = requests_cache.CachedSession('http_cache', backend='filesystem')
print(session.cache.cache_dir)

base_url = 'https://cloud.iexapis.com'

my_columns = ['Ticker', 'Stock Price', 'Market Capitalization', 'Number of Shares to Buy']
df = pd.DataFrame(columns=my_columns)

stocks = pd.read_csv('starter_files/sp_500_stocks.csv')

step = 100
all_symbols = stocks['Ticker']

for i in range(0, len(all_symbols), step):
    symbols = all_symbols[i:i + step]
    symbols_text = ','.join(symbols)
    url = f'{base_url}/stable/stock/market/batch/?symbols={symbols_text}&types=quote&token={IEX_CLOUD_API_TOKEN}'
    response = session.get(url)

    if response.status_code == 404:
        continue

    data = response.json()

    for symbol in data:
        item = data[symbol]['quote']
        # print(json.dumps(item, indent=2))

        price = item['latestPrice']
        market_cap = item['marketCap']
        new_row = pd.DataFrame([[symbol, price, market_cap, 'N/A']], columns=my_columns)
        df = pd.concat([df, new_row], ignore_index=True)

# portfolio_size = input('enter the value of your portfolio')
# print(portfolio_size)
#
# try:
#     val = float(portfolio_size)
#     print(val)
# except ValueError:
#     print('please enter a number')

print(tabulate(df, headers='keys', tablefmt='psql'))
portfolio_size = 1000000
position_size = portfolio_size / len(df.index)

for i in range(0, len(df.index)):
    price = df.loc[i, 'Stock Price']
    df.loc[i, 'Number of Shares to Buy'] = math.floor(position_size / price)

print(tabulate(df, headers='keys', tablefmt='psql'))

writer = pd.ExcelWriter('foo.xlsx', engine='xlsxwriter')
df.to_excel(writer, "foo", index=False)
bg_color = '#0a0a23'
font_color = '#ffffff'

string_format = writer.book.add_format({
    'font_color': font_color,
    'bg_color': bg_color,
    'border': 1
})
dollar_format = writer.book.add_format({
    'num_format': '$0.00',
    'font_color': font_color,
    'bg_color': bg_color,
    'border': 1
})
integer_format = writer.book.add_format({
    'num_format': 0,
    'font_color': font_color,
    'bg_color': bg_color,
    'border': 1
})

col_formats = {
    'A': ['Ticker', string_format],
    'B': ['Stock Price', dollar_format],
    'C': ['Market Capitalization', dollar_format],
    'D': ['Number of Shares to Buy', integer_format],
}

for col in col_formats.keys():
    f = col_formats[col][1]
    title = col_formats[col][0]
    writer.sheets['foo'].set_column(f'{col}:{col}', 18, f)
    writer.sheets['foo'].write(f'{col}1', title, string_format)

writer.save()

algorithmic-trading-python-tutorial/algo trading course - project 1.py


import numpy as np
from numpy.random import uniform as U, uniform

from infinigen.core.util.math import FixedSeed, int_hash

def repeated_driver(start, end, freq, off=None, seed=None):
    if off is None:
        off = uniform(0, 1)
    if seed is None:
        seed = np.random.randint(1e5)
    with FixedSeed(seed):
        phase = uniform(.2, .8)
        u = phase * uniform(.8, 1.)
        v = (1 - phase) * uniform(.8, 1.)
        t = f"{freq: .4f} * frame+{off:.4f}"
        t = f"{t}-floor({t})"
        return f"{start}+{end - start}*(smoothstep(0,{u},{t})-smoothstep({phase},{phase + v},{t}))"

def bend_bones_lerp(arma, bones, total, freq, rot=None, symmetric=True):
    bone_lengths = []
    for bone in bones:
        length = bone.bone['length'] if isinstance(bone.bone['length'], (int, float)) else 0
        if length >= 0:
            bone_lengths.append((bone, length))
    bone_lengths = list(sorted(bone_lengths, key=lambda _: _[1]))
    bones = [b for (b, _) in bone_lengths] if bone_lengths else bones
    bone = bones[0].bone
    hashable = arma.parent.name, bone['factory_class'], bone['index'] + (bone['side'] > 0) * symmetric
    with FixedSeed(int_hash(hashable)):
        ratio = uniform(1, 2, len(bones))
        ratio /= ratio.sum()
        total = [(t if uniform(0, 1) < .5 else (t[1], t[0])) if isinstance(t, tuple) else (t, t) for t in total]
        o0, o1, o2 = uniform(0, 1, 3)
        seed = np.random.randint(1e5)
        for i, (bone, r) in enumerate(zip(bones, ratio)):
            s = bone.bone['side']
            bone.rotation_mode = 'XYZ'
            (x0, x1), (y0, y1), (z0, z1) = total
            if rot is not None and i == 0:
                x0 += rot[0]
                x1 += rot[0]
                y0 += rot[1]
                y1 += rot[1]
                z0 += rot[2]
                z1 += rot[2]
            driver_x, driver_y, driver_z = [_.driver for _ in bone.driver_add('rotation_euler')]
            driver_x.expression = f"({repeated_driver(x0, x1, freq, o0, seed)})*{r}"
            driver_y.expression = f"({repeated_driver(y0, y1, freq, o1, seed)})*{r}"
            driver_z.expression = f"({repeated_driver(z0, z1, freq, o2, seed)})*{s * r}"


from django.http import HttpResponse
from django.shortcuts import render

def index(request):
    return render(request, 'index.html')

def analyze(request):

    djtext = request.GET.get('text', 'default')

    removepunc = request.GET.get('removepunc', 'off')
    fullcaps = request.GET.get('fullcaps', 'off')
    newlineremover = request.GET.get('newlineremover', 'off')
    extraspaceremover = request.GET.get('extraspaceremover', 'off')

    # Check which checkbox is on
    if removepunc == "on":
        punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
        analyzed = ""
        for char in djtext:
            if char not in punctuations:
                analyzed = analyzed + char
        djtext = analyzed

    if(fullcaps == "on"):
       djtext = djtext.upper()

    if (extraspaceremover == "on"):
        djtext = djtext.replace(" ", "")

    if (newlineremover == "on"):
        analyzed = ""
        for char in djtext:
            if char != "\n":
                analyzed = analyzed + char
        djtext = analyzed

    params = {'purpose': 'New Data', 'analyzed_text': djtext}
    return render(request, 'analyze.html', params)


'''
Copyright (C) 2020 Yee Young Han <websearch@naver.com> (http://blog.naver.com/websearch)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''

import time
import threading
from ..SipPlatform.SipMd5 import SipMd5String

class NonceMap():

  def __init__( self ):
    self.clsMutex = threading.Lock()
    self.clsMap = {}

  def GetNewValue( self ):
    iTime = time.time()
    bInsert = False
    i = 0

    while bInsert == False:
      strNonce = SipMd5String( str(i) + "::" + str(iTime) + "::pythonsipserver" )

      self.clsMutex.acquire()
      if( self.clsMap.get( strNonce ) == None ):
        self.clsMap[strNonce] = time.time()
        bInsert = True
      self.clsMutex.release()

      i += 1

    return strNonce

  def Select( self, strNonce, bRemove ):
    bRes = False

    self.clsMutex.acquire()
    if( self.clsMap.get( strNonce ) != None ):
      bRes = True
      if( bRemove ):
        del self.clsMap[strNonce]
    self.clsMutex.release()

    return bRes

  def DeleteTimeout( self, iSecond ):
    iTime = time.time()
    clsDeleteList = []

    self.clsMutex.acquire()
    for strNonce in self.clsMap:
      if( iTime > ( self.clsMap[strNonce] + iSecond ) ):
        clsDeleteList.append( strNonce )

    for strNonce in clsDeleteList:
      del self.clsMap[strNonce]
    self.clsMutex.release()


import datasets
import os
import torch
import numpy as np
import json
import random
import collections
import sentencepiece as spm
import time
import argparse

def get_time():
    return time.strftime("%Y%m%d %H:%M:%S", time.localtime())

OPENMODEL_PATH = 'your/model/path'
if OPENMODEL_PATH == 'your/model/path':
    raise('Model path is not valid')
CEVAL_DATA_PATH = 'your/ceval/data/path'
results_filename = 'results.txt'

def load_model(model_id):
    print('Loading', model_id)
    model_id = os.path.join(OPENMODEL_PATH, model_id)
    if 'gpt2' in model_id:
        from transformers import GPT2LMHeadModel, GPT2TokenizerFast
        model = GPT2LMHeadModel.from_pretrained(model_id, device_map='auto',)
        tokenizer = GPT2TokenizerFast.from_pretrained(model_id)
    elif 'glm' in model_id:
        from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModel
        tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
        model = AutoModel.from_pretrained(model_id, trust_remote_code=True, device_map='auto', torch_dtype=torch.float16)
    elif 'bloomz' in model_id:
        from transformers import BloomForCausalLM
        tokenizer = AutoTokenizer.from_pretrained(model_id)
        model = BloomForCausalLM.from_pretrained(model_id, device_map='auto', torch_dtype=torch.float16)
    elif 'tigerbot' in model_id:
        from transformers import AutoTokenizer, AutoModelForCausalLM
        tokenizer = AutoTokenizer.from_pretrained(model_id)
        model = AutoModelForCausalLM.from_pretrained(model_id, device_map='auto')
    elif 'Ziya-LLaMA' in model_id:
        from transformers import AutoTokenizer, LlamaForCausalLM
        model = LlamaForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
        tokenizer = AutoTokenizer.from_pretrained(model_id)
    elif 'falcon' in model_id:
        from transformers import AutoTokenizer, AutoModelForCausalLM
        tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=False,
                                                  trust_remote_code=True,
                                                  add_bos_token=False,
                                                  add_eos_token=False,
                                                  padding_dise='right')
        model = AutoModelForCausalLM.from_pretrained(model_id,
                                             trust_remote_code=True,
                                             device_map='auto',
                                             torch_dtype=torch.bfloat16)
    elif 'aquila' in model_id:
        from flagai.auto_model.auto_loader import AutoLoader
        from flagai.model.predictor.predictor import Predictor
        from flagai.data.tokenizer import Tokenizer
        import bminf
        model_id = model_id.split('/')[-1]
        print(model_id)
        loader = AutoLoader(
            "lm",
            model_dir=OPENMODEL_PATH,
            model_name=model_id,
            use_cache=True,
            device="cuda")
        model = loader.get_model()
        model.eval()
        model.half()
        tokenizer = loader.get_tokenizer()
    elif 'Open-Llama' in model_id:
        from transformers import AutoTokenizer, AutoModelForCausalLM
        tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=False, trust_remote_code=True, add_eos_token = False)
        model = AutoModelForCausalLM.from_pretrained(model_id,
                                             trust_remote_code=True,
                                             device_map='auto',
                                             torch_dtype=torch.bfloat16)
    else:
        from transformers import AutoTokenizer, AutoModelForCausalLM
        tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=False, trust_remote_code=True)
        model = AutoModelForCausalLM.from_pretrained(model_id,
                                             trust_remote_code=True,
                                             device_map='auto',
                                             torch_dtype=torch.bfloat16)
    candidates = ['A', 'B', 'C', 'D']
    for can in candidates:
        can_idx = tokenizer.encode(can)
        print(can, can_idx)

    return model, tokenizer

class BaseEval:
    def __init__(self, model_id, data_path, show_detail=False):
        self.data_path = data_path
        self.model_id = model_id
        self.task_names = self.get_task_names()
        self.show_detail = show_detail
        self.max_new_tokens = 3  # 最多生成的token数

    def get_task_names(self):
        pass

    def set_model_and_tokenizer(self, model, tokenizer):
        self.model = model
        self.tokenizer=tokenizer

    def build_an_example_prompt(self, data, with_answer=True, flag=0):
        pass

    def build_prompt(self, task_name, data, dataset, shot):
        pass

    def get_results_by_ABCD_prob(self, output, max_new_tokens):
        i = 0  # 用生成的第i个token进行预测
        if 'aquila' in self.model_id:
            scores = output.scores[i].to(torch.float32).detach()
        else:
            scores = output.scores[i][0].to(torch.float32)
        label_score = []
        candidates = ['A', 'B', 'C', 'D']
        for can in candidates:
            can_idx = self.tokenizer.encode(can)[-1]
            label_score.append(scores[can_idx].cpu().numpy())
        answer = candidates[np.argmax(label_score)]

        if self.show_detail:
            # 生成的具体内容
            if 'aquila' in self.model_id:
                outstr = output.text
            else:
                outstr = self.tokenizer.decode(output.sequences.cpu().numpy()[0][-max_new_tokens: ])
            print(f'Generate text (Start>>>){outstr}(<<<End)')
            print(f'Predict answer with ABCD probs: {answer}')
        return answer

    def predict_instance(self, prompt):
        if self.show_detail:
            print(100*'-')
            print(prompt)
        max_new_tokens = self.max_new_tokens
        if 'aquila' not in model_id:
            input_ids = self.tokenizer.encode(prompt, return_tensors="pt")
            input_ids = input_ids.to('cuda')
        if 'glm' in self.model_id:
            # glm 需要特殊处理 https://github.com/mymusise/ChatGLM-Tuning/issues/150
            input_ids[0][-2] = 130001
            input_ids[0][-1] = 130004
        if 'aquila' in model_id:
            from aquila import aquila_generate
            output = aquila_generate(self.tokenizer, self.model, [prompt,], max_gen_len=1)
        else:
            output = self.model.generate(input_ids, max_new_tokens=max_new_tokens, return_dict_in_generate=True, output_scores=True, temperature=0.1, top_p=0.5, repetition_penalty=1.1)
        return self.get_results_by_ABCD_prob(output, max_new_tokens)

    def run_a_task(self, task_name, shot, split='test'):
        results = {}
        correct = []
        prompts = collections.OrderedDict()
        print(self.data_path, task_name)
        dataset = datasets.load_dataset(self.data_path, task_name)
        with torch.no_grad():
            for data in dataset[split]:
                prompt = self.build_prompt(task_name, data, dataset, shot)
                plabel = self.predict_instance(prompt)
                idx = data['id']
                results[str(idx)] = plabel
                correct.append(plabel == data['answer'] if 'answer' in data else 0)
                prompts[str(idx)] = prompt
        return {task_name: results}, {task_name: prompts}, {task_name: correct}

    def run(self, task_names, shot, split):
        results = dict()
        task_prompts = dict()
        task_acc = dict()
        filename_pfx = f"{self.model_id.split('/')[-1]}_{self.__class__.__name__}_{len(task_names)}_{split}_{shot}-shot"
        for name in task_names:
            print(100*'-')
            print(name)
            value, prompts, acc = self.run_a_task(name, shot, split)
            results.update(value)
            task_prompts.update(prompts)
            task_acc.update(acc)
            json.dump(results, open(f'{filename_pfx}_results.json', 'w'), indent=True, ensure_ascii=False)

        outputline = []
        outputline.append(f'{get_time()}\t{filename_pfx}')
        correct = []
        for k, v in task_acc.items():
            correct += v
        outputline.append(f'\tAverage example acc: {np.mean(correct):.4f}')
        acc = []
        for k, v in task_acc.items():
            acc.append(np.mean(v))
        outputline.append(f'\tAverage subject acc: {np.mean(acc):.4f}')
        [print(line) for line in outputline]
        self.save_results(outputline)

    def save_results(self, lines):
        with open(results_filename, 'a') as f:
            for line in lines:
                f.write(line)
                f.write('\n')

    def generate_sample(self, task_name='law', split='validatoin', shot=0):
        dataset = datasets.load_dataset(self.data_path, task_name)
        data = dataset[split][0]
        print('Data' + 100*'-')
        print(data)
        print(f'{shot}-shot prompt' + 100*'-')
        print('(Start>>>)'+self.build_prompt(task_name, data, dataset, shot)+'(<<<End)')

class CEval(BaseEval):
    task2desc = {
        "high_school_physics": "高中物理",
        "fire_engineer": "注册消防工程师",
        "computer_network": "计算机网络",
        "advanced_mathematics": "高等数学",
        "logic": "逻辑学",
        "middle_school_physics": "初中物理",
        "clinical_medicine": "临床医学",
        "probability_and_statistics": "概率统计",
        "ideological_and_moral_cultivation": "思想道德修养与法律基础",
        "operating_system": "操作系统",
        "middle_school_mathematics": "初中数学",
        "chinese_language_and_literature": "中国语言文学",
        "electrical_engineer": "注册电气工程师",
        "business_administration": "工商管理",
        "high_school_geography": "高中地理",
        "modern_chinese_history": "近代史纲要",
        "legal_professional": "法律职业资格",
        "middle_school_geography": "初中地理",
        "middle_school_chemistry": "初中化学",
        "high_school_biology": "高中生物",
        "high_school_chemistry": "高中化学",
        "physician": "医师资格",
        "high_school_chinese": "高中语文",
        "tax_accountant": "税务师",
        "high_school_history": "高中历史",
        "mao_zedong_thought": "毛泽东思想和中国特色社会主义理论概论",
        "high_school_mathematics": "高中数学",
        "professional_tour_guide": "导游资格",
        "veterinary_medicine": "兽医学",
        "environmental_impact_assessment_engineer": "环境影响评价工程师",
        "basic_medicine": "基础医学",
        "education_science": "教育学",
        "urban_and_rural_planner": "注册城乡规划师",
        "middle_school_biology": "初中生物",
        "plant_protection": "植物保护",
        "middle_school_history": "初中历史",
        "high_school_politics": "高中政治",
        "metrology_engineer": "注册计量师",
        "art_studies": "艺术学",
        "college_economics": "大学经济学",
        "college_chemistry": "大学化学",
        "law": "法学",
        "sports_science": "体育学",
        "civil_servant": "公务员",
        "college_programming": "大学编程",
        "middle_school_politics": "初中政治",
        "teacher_qualification": "教师资格",
        "computer_architecture": "计算机组成",
        "college_physics": "大学物理",
        "discrete_mathematics": "离散数学",
        "marxism": "马克思主义基本原理",
        "accountant": "注册会计师",
    }

    def get_task_names(self):
        task_names = list(sorted(self.task2desc.keys()))
        return task_names

    def build_an_example_prompt(self, data, with_answer=True):
        choice =  ['A. '+data["A"],'B. '+data["B"],'C. '+data["C"],'D. '+data["D"]]
        answer = data["answer"].strip().upper() if with_answer else ""
        question = data['question']
        cstr = '\n'.join(choice)
        return f"{question}\n{cstr}\n答案：{answer}"

    def build_prompt(self, task_name, data, dataset, shot):
        if shot == 0:
            prompt = f"以下是中国关于{self.task2desc[task_name]}考试的单项选择题，请选出其中的正确答案。\n\n{self.build_an_example_prompt(data, with_answer=False)}"
        else:
            shuffle_train = dataset['dev'].shuffle(seed=123)
            prompt = f"以下是中国关于{self.task2desc[task_name]}考试的单项选择题，请选出其中的正确答案。\n"
            for i in range(min(len(shuffle_train), shot)):
                prompt += '\n' + self.build_an_example_prompt(shuffle_train[i], with_answer=True)
            prompt += '\n' + self.build_an_example_prompt(data, with_answer=False)
        return prompt

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu_idx', type=str, required=True)
    parser.add_argument('--model_id', type=str, default=None)
    parser.add_argument('--task', type=str, required=True)
    parser.add_argument('--shot', type=int, default=5)
    parser.add_argument('--split', type=str, default='test')
    parser.add_argument('--show_detail', action='store_true')
    args = parser.parse_args()
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_idx
    print(args.model_id, args.shot, args.split, args.task)
    model_id = args.model_id
    shot = args.shot
    split = args.split
    show_detail = args.show_detail

    model, tokenizer = None, None
    if model_id is not None:
        model, tokenizer = load_model(model_id)

    if args.task == 'ceval':
        data_path = CEVAL_DATA_PATH
        if data_path == 'your/ceval/data/path':
            raise('CEval data path is not valid')
        ceval = CEval(model_id, data_path, show_detail=show_detail)
        ceval.set_model_and_tokenizer(model, tokenizer)
        ceval.generate_sample(task_name='law', split=split, shot=3)
        ceval.run(task_names=ceval.task_names, shot=shot, split=split)
    else:
        raise(f'Task is not valid: {args.task}')


from collections import ChainMap
from concurrent.futures import ThreadPoolExecutor
from functools import partial
from typing import Dict, List, Optional

from sqlalchemy.orm import Session

from app import crud, models
from app.api.errors.errors import PredictionNotFound, PrematurePredictions
from app.constants.state import ResultState
from app.utils.ymir_controller import ControllerClient
from common_utils.labels import UserLabels

def evaluate_predictions(
    controller_client: ControllerClient,
    user_id: int,
    project_id: int,
    user_labels: UserLabels,
    confidence_threshold: Optional[float],
    iou_threshold: Optional[float],
    require_average_iou: bool,
    need_pr_curve: bool,
    main_ck: Optional[str],
    prediction_id_mapping: Dict[str, int],
) -> Dict:
    iou_thrs_interval = convert_to_iou_thrs_interval(iou_threshold, require_average_iou)
    f_evaluate = partial(
        controller_client.evaluate_prediction,
        user_id,
        project_id,
        user_labels,
        confidence_threshold,
        iou_thrs_interval,
        need_pr_curve,
        main_ck,
    )
    with ThreadPoolExecutor() as executor:
        res = executor.map(f_evaluate, prediction_id_mapping.keys())

    evaluations = ChainMap(*res)

    return {prediction_id_mapping[hash_]: evaluation for hash_, evaluation in evaluations.items()}

def convert_to_iou_thrs_interval(iou_threshold: Optional[float], require_average_iou: Optional[bool]) -> Optional[str]:
    """
    the underlying requires "start:end:step" format
    """
    if require_average_iou:
        return f"{iou_threshold or 0.5}:0.95:0.05"
    if not iou_threshold:
        return None
    return str(iou_threshold)

def ensure_predictions_are_ready(db: Session, prediction_ids: List[int]) -> List[models.Prediction]:
    prediction_ids = list(set(prediction_ids))
    predictions = crud.prediction.get_multi_by_ids(db, ids=prediction_ids)
    if len(prediction_ids) != len(predictions):
        raise PredictionNotFound()

    if not all(prediction.result_state == ResultState.ready for prediction in predictions):
        raise PrematurePredictions()
    return predictions


__author__ = 'shy'
__date__ = '2018/3/19 10:55'

# 方法一：lambda表达式实现

# quick_sort = lambda array: array if len(array) <= 1 else quick_sort([i for i in array[1:] if i <= array[0]]) + \
#             [array[0]] + quick_sort([i for i in array[1:] if i > array[0]])
#
# if __name__ == '__main__':
#     array = []
#     print(quick_sort(array))

# 方法二：pythonic（随机取位置）

# import random
#
#
# def quick_sort(array):
#     left = []
#     right = []
#     count = len(array)
#
#     if count <= 1:
#         return array
#     r = random.randint(0, count-1)
#     for i in range(0, count):
#         if array[i] < array[r]:
#             left.append(array[i])
#         else:
#             right.append(array[i])
#     return quick_sort(left) + quick_sort(right)
#
#
# if __name__ == '__main__':
#     array = [6, 4, 2, 5, 7, 9, 1, 3, 8]
#     print(quick_sort(array))

# 方法三：传统方法

def q_sort(arr, first, last):
    if first < last:
        target = partition(arr, first, last)

        q_sort(arr, first, target)
        q_sort(arr, target + 1, last)
    else:
        return

def partition(arr, first, last):
    start = first - 1
    for i in range(first, last):
        if arr[i] <= arr[last]:
            start = start + 1
            arr[start], arr[i] = arr[i], arr[start]
    arr[start + 1], arr[last] = arr[last], arr[start + 1]
    return start

if __name__ == '__main__':
    array = [6, 4, 2, 5, 7, 9, 1, 3, 8]
    q_sort(array, 0, len(array)-1)
    print(array)

算法/排序/快速排序.py


from scipy import io as sio
import numpy as np
import os
import dicom
import nibabel as nib
import datetime
from cafndl_fileio import *
from cafndl_utils import *
from cafndl_network import *
from cafndl_metrics import *
from keras.callbacks import ModelCheckpoint
from keras.optimizers import Adam

'''
convert dicom to nifti
$ mkdir DRF100_nifti
$ dicom2nifti DRF100 DRF100_nifti
'''

'''
dataset
'''
filename_checkpoint = '../ckpt/model_demo_0713.ckpt'
list_dataset_test =  [
				{
				 'inputs':['/data/enhaog/data_lowdose/GBM_Ex1496/DRF020_nifti/802_.nii.gz',
				 			'/data/enhaog/data_lowdose/GBM_Ex1496/DRF100_nifti/803_.nii.gz'],
				 'gt':'/data/enhaog/data_lowdose/GBM_Ex1496/DRF001_nifti/800_.nii.gz'
				},
				{
				 'inputs':['/data/enhaog/data_lowdose/GBM_Ex842/DRF020_nifti/802_.nii.gz',
				 			'/data/enhaog/data_lowdose/GBM_Ex842/DRF100_nifti/803_.nii.gz'],
				 'gt':'/data/enhaog/data_lowdose/GBM_Ex842/DRF001_nifti/800_.nii.gz'
				}
				]
filename_results = '../results/result_demo_0713'
num_dataset_test = len(list_dataset_test)
print('process {0} data description'.format(num_dataset_test))

'''
augmentation
'''
list_augments = []

'''
generate test data
'''
list_test_input = []
list_test_gt = []
for index_data in range(num_dataset_test):
	# directory
	list_data_test_input = []
	for path_test_input in list_dataset_test[index_data]['inputs']:
		# load data
		data_test_input = prepare_data_from_nifti(path_test_input, list_augments)
		list_data_test_input.append(data_test_input)
	data_test_input = np.concatenate(list_dataset_test_input, axis=-1)

	# get ground truth
	path_test_gt = list_dataset_test[index_data]['gt']
	data_test_gt = prepare_data_from_nifti(path_test_gt, [])

	# append
	list_test_input.append(data_test_input)
	list_test_gt.append(data_test_gt)

# generate test dataset
scale_data = 100.
data_test_input = scale_data * np.concatenate(list_test_input, axis = 0)
data_test_gt = scale_data * np.concatenate(list_test_gt, axis = 0)
data_test_residual = data_test_gt - data_test_input[:,:,:,0:1]
print('mean, min, max')
print(np.mean(data_test_input.flatten()),np.min(data_test_input.flatten()),np.max(data_test_input.flatten()))
print(np.mean(data_test_gt.flatten()),np.min(data_test_gt.flatten()),np.max(data_test_gt.flatten()))
print(np.mean(data_test_residual.flatten()),np.min(data_test_residual.flatten()),np.max(data_test_residual.flatten()))
print('generate test dataset with augmentation size {0},{1}'.format(
	data_test_input.shape, data_test_gt.shape))

'''
setup parameters
'''
keras_memory = 0.3
keras_backend = 'tf'
num_channel_input = data_test_input.shape[-1]
num_channel_output = data_test_gt.shape[-1]
img_rows = data_test_input.shape[1]
img_cols = data_test_gt.shape[1]
num_poolings = 3
num_conv_per_pooling = 3
lr_init = 0.001
with_batch_norm = True
ratio_validation = 0.1
batch_size = 4
always_retrain = 1
num_epoch = 100
print('setup parameters')

'''
define model
'''
setKerasMemory(keras_memory)
model = deepEncoderDecoder(num_channel_input = num_channel_input,
						num_channel_output = num_channel_output,
						img_rows = img_rows,
						img_cols = img_cols,
						lr_init = lr_init,
						num_poolings = num_poolings,
						num_conv_per_pooling = num_conv_per_pooling,
						with_bn = with_batch_norm, verbose=1)
print('train model:', filename_checkpoint)
print('parameter count:', model.count_params())

'''
load network
'''
model.load_weights(filename_checkpoint)
print('model load from' + filename_checkpoint)

'''
apply model
'''
t_start_pred = datetime.datetime.now()
data_test_output = model.predict(data_test_input, batch_size=batch_size)
# clamp
clamp_min = -0.5
clamp_max = 0.5
data_test_output = np.maximum(np.minimum(data_test_output, clamp_max), clamp_min)

# add
data_test_output += data_test_input[:,:,:,0:1]

t_end_pred = datetime.datetime.now()
print('predict on data size {0} using time {1}'.format(
	data_test_output.shape, t_end_pred - t_start_pred))

'''
export images
'''
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
num_sample = data_test_output.shape[0]
list_err_pred = []
list_err_input = []
aug_err = 10
for i in range(num_sample):
	# get image
	im_gt = np.squeeze(data_test_gt[i,:,:,0]).T
	im_input = np.squeeze(data_test_input[i,:,:,0]).T
	im_pred = np.squeeze(data_test_output[i,:,:,0]).T

	# get error
	err_pred = getErrorMetrics(im_pred, im_gt)
	err_input = getErrorMetrics(im_input, im_gt)
	list_err_pred.append(err_pred)
	list_err_input.append(err_input)

	# display
	im_toshow = [im_gt, im_input, (im_input-im_gt)*aug_err, im_pred, (im_pred-im_gt)*aug_err]
	im_toshow = np.abs(np.concatenate(im_toshow, axis=1))
	plt.figure(figsize=[20,8])
	plt.imshow(im_toshow, clim=[0,0.5], cmap='gray')
	im_title = 'sample #{0}, input PSNR {1:.4f}, SSIM {2:.4f}, predict PSNR {3:.4f}, SSIM {4:.4f}'.format(
				i, err_input['psnr'], err_input['ssim'], err_pred['psnr'], err_pred['ssim'])
	plt.title(im_title)
	print(im_title)
	path_figure = filename_results+'_{0}.png'.format(i)
	plt.savefig(path_figure)

'''
export results
'''
print('input average metrics:', {k:np.mean([x[k] for x in list_err_input]) for k in list_err_input[0].keys()})
print('prediction average metrics:', {k:np.mean([x[k] for x in list_err_pred]) for k in list_err_pred[0].keys()})# save history dictionary
import json
result_error = {'err_input':list_err_input,
				'err_pred':list_err_pred}
path_error = filename_results+'_error.json'
with open(path_error, 'w') as outfile:
    json.dump(result_error, outfile)
print('error exported to {0}'.format(path_error))


# Программа анализа последовательности Фибоначи,
# состоящую из чисел, которые меньше 10.000.000 (десять миллионов)
import os

os.system('cls')

fib1 = 1
fib2 = 1
fib = 0
sum_of_even_elements = 0
number_of_elements = 2
even_elements = []

# Вычисление и анализ последовательности Фибоначи.
while (fib + fib2) < 10000000:
    fib = fib1 + fib2
    fib2 = fib1
    fib1 = fib
    if fib % 2 == 0:# Проверка на четность элемента последовательности Фибоначи.
        even_elements.append(fib)# Добавление в список четного элемента.
        sum_of_even_elements += fib# Суммируем четные элементы.
    number_of_elements += 1

print ("Количество элементов в последовательности = " + str(number_of_elements))
print ("Сумма всех четных элементов = " + str (sum_of_even_elements))

print ("Все четные элементы: ", end=" ")
for i in even_elements:
   print (i, end= " ")

print ("")
print ("Предпоследнее число последовательности = " + str(fib2))


"""
https://leetcode.com/problems/palindrome-number/

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:
    Input: 121
    Output: true

Example 2:
    Input: -121
    Output: false
    Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:
    Input: 10
    Output: false
    Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:
Coud you solve it without converting the integer to a string?
"""
class Solution:
    def isPalindrome(self, x: 'int') -> 'bool':
        s = str(x)
        i=0
        while i < len(s)//2:
            if(s[i] != s[len(s)-i-1]):
                break
            i+=1
        if i >= len(s)//2:
            return True
        else:
            return False

x = Solution()
print(x.isPalindrome(121))


#!/usr/bin/env python

import serial
import time

arduino = serial.Serial(
	port="/dev/cu.wchusbserialfa210",
	baudrate=9600,
	timeout=3,
	bytesize=serial.EIGHTBITS
)
time.sleep(3)
arduino.write('... --- ...\n')
time.sleep(1)
out=''
while arduino.inWaiting() > 0:
    out += arduino.read(40)

if out != '':
    print(">>" + out)

arduino.close()


import turtle
from turtle import Turtle, Screen
import random

timmy = Turtle()
timmy.shape("turtle")
timmy.color("DarkSlateBlue")
turtle.colormode(255)

def random_colour():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    colour = (r, g, b)
    return colour

directions = [0, 90, 180, 270]
timmy.pensize(15)
timmy.speed("fastest")

for _ in range(500):
    timmy.color( random_colour())
    timmy.forward(30)
    timmy.setheading(random.choice(directions))

screen = Screen()
screen.exitonclick()