#calling js scripts to interact with marketplace

import subprocess
import re
import marketFunctions as mf

wallet_bob = 2
wallet_jade = 3
wallet_blessing = 4
wallet_dan = 5
wallet_juhansen = 6
wallet_axiebobshop = 7

address_bob = "ronin:f6e37c7ddf7bed40a5c847adf95589869c5c115b"
address_jade = "ronin:73c2abfdd9aa1218122f22024aa22afd82a7542d"
address_blessing = "ronin:b4367810619a19df392a823fec06725bf652d594"
address_dan = "ronin:c0d95423ca1b97d046874f86a1b56cfc14c48b04"
address_juhansen = "ronin:5cf795606a2e7091b935c42c037a4758dc00e681"
address_axiebobshop = "ronin:dc683a32553d8de5828ec1364df4b7ae6f98a66b"
address_ivy = "ronin:d8a7e07286e35dbcc6bb1c8bfa1ca758272fe130"

# unlistAxie(axieId)                                    [axieId: int]
# unlistAxies(axieId_list)                              [axieId_list: list of ints]
# listAxie(axieId, priceEth)                            [axieId: int], [priceEth: float]
# listAuction(axieId, startPrice, endPrice, duration)   [axieId: int], [startPrice: float], [endPrice: float], [duration: int(days)]
# transferAxie(axieId, transferRonin)                   [axieId: int], [transferRonin: ronin address starting with 'ronin:']
# transferAxies(axieId_list, transferRonin)             [axieId_list: list of ints], [transferRonin-ronin address starting with 'ronin:']
# selectWallet(wallet_index)                            [wallet_index: int, defined at top]
# buyAxie(axieId)                                       [axieId: int]
# hatchAxies(Ronin)                                     [Ronin: ronin address starting with 'ronin:']
# relistAxie(axieId, priceEth)                          [axieId: int], [priceEth: float]
# relistAuction(axieId, startPrice, endPrice, duration) [axieId: int], [startPrice: float], [endPrice: float], [duration: int(days)]
# getBalance                                            gets balance of current selected wallet, no args

def unlistAxie(axieId):
    argstart = "node scripts\\unlistjs\\unlistAxie.js"
    arg = argstart + " " + str(axieId)
    print(arg)
    subprocess.run(arg, shell=True, check=True)

def unlistAxies(axieId_list):
    for axieId in axieId_list:
        unlistAxie(axieId)

def listAxie(axieId, priceEth):
    argstart = "node scripts\\listjs\\listAxie.js"
    arg = argstart + " " + str(axieId) + " " + str(priceEth)
    print(arg)
    subprocess.run(arg, shell=True, check=True)

def listAuction(axieId, startPrice, endPrice, duration):
    argstart = "node scripts\\listjs\\listAuction.js"
    arg = argstart + " " + str(axieId) + " " + str(startPrice) + " " + str(endPrice) + " " + str(duration)
    print(arg)
    subprocess.run(arg, shell=True, check=True)

def transferAxie(axieId, transferRonin):
    argstart = "node scripts\\transferjs\\transferAxie.js"
    arg = argstart + " " + transferRonin + " " + str(axieId)
    print(arg)
    subprocess.run(arg, shell=True, check=True)

def transferAxies(axieId_list, transferRonin):
    argstart = "node scripts\\transferjs\\transferAxie.js " + transferRonin
    axieId_strings = mf.get_axieIdstring_from_list(axieId_list)
    arg_axieIds = " ".join(axieId_strings)
    arg = argstart + " " + arg_axieIds
    print(arg)
    subprocess.run(arg, shell=True, check=True)

def buyAxie(axieId):
    argstart = "node scripts\\buyjs\\buyAxie.js"
    arg = argstart + " " + str(axieId)
    print(arg)
    subprocess.run(arg, shell=True, check=True)

def hatchAxies(Ronin):
    axieIds = mf.grab_incubated_list(Ronin)
    print('hatching: ' + str(len(axieIds)) + ' eggs')
    axieId_strings = mf.get_axieIdstring_from_list(axieIds)
    argstart = "node scripts\\hatchjs\\hatchAxie.js"
    arglist = []
    for axieId in axieId_strings:
        arg = argstart + " " + axieId + ";"
        arglist.append(arg)
    for arg in arglist:
        print("command: " + arg)
        subprocess.run(arg, shell=True, check=True)
    print('hatched: ' + str(len(axieIds)) + ' eggs')

def getBalance():
    arg = "node scripts\\accountsjs\\balance.js"
    # result = subprocess.run(arg, shell=True, check=True)
    result = subprocess.run(arg, shell=True, text=True, stdout=subprocess.PIPE)
    output = result.stdout
    print(output)
    output = output.split("\n")
    balance = {}
    for item in output[0:5]:
        item = item.split(" ")
        key, value = item[1], item[0]
        balance[key] = value
    balance[item[1]] = item[0]
    return balance

def selectWallet(wallet_index):
    with open('env.txt', 'r') as file:
        env_contents = file.readlines()
        keyraw = env_contents[wallet_index]
        key = re.findall('"([^"]*)"', keyraw)
        key = key[0]
        env_contents[0] = key + '\n'
    with open('env.txt', 'w') as file:
        file.writelines(env_contents)
    wallet_mapping = {
        2: "Bob New Main",
        3: "Jade",
        4: "Blessing",
        5: "Dan",
        6: "Juhansen",
        7: "Axie Bob Shop"
    }
    if wallet_index in wallet_mapping:
        print('Currently logged in to: ' + wallet_mapping[wallet_index])
    command = 'node scripts\\accountsjs\\accounts.js'
    subprocess.run(command, shell=True, check=True)

def relistAxie(axieid, priceEth):
    unlistAxie(axieid)
    while True:
        try:
            listAxie(axieid, priceEth)
            break
        except:
            print("If unlisting was successful, must wait for Axie to be completely off market. Trying again.")

def relistAuction(axieid, priceStart, priceEnd, duration):
    unlistAxie(axieid)
    while True:
        try:
            listAuction(axieid, priceStart, priceEnd, duration)
            break
        except:
            print("If unlisting was successful, must wait for Axie to be completely off market. Trying again.")


import xml.etree.ElementTree as ET

import os
from ListaDoble import ListaDoble
from Elemento import Elemento
from Item import Item

import tkinter as tk
from tkinter import messagebox, filedialog, PhotoImage

elementos = ListaDoble()

def carga_datos(ruta):
    elementos.inicializar()
    #cargar el archivo
    tree = ET.parse(ruta)
    root = tree.getroot()
    # Recorriendo el árbol XML
    for elemento in root.findall('elemento'):
        nombre = elemento.get('nombre')
        id_val = elemento.get('id')
        cols = int(elemento.get('cols'))
        rows = int(elemento.get('rows'))
        elemento_nuevo = Elemento(nombre, id_val, cols, rows)

        for item in elemento.findall('item'):
            row = int(item.get('row'))
            col = int(item.get('col'))
            text = item.text
            elemento_nuevo.items.agregar_al_final(Item(row, col, text))

        elementos.insertarOrdenadoPorId(elemento_nuevo)

    print("Datos cargados exitosamente")

def graficar(elemento):
    dot_string = 'digraph G {\n'
    dot_string += elemento.to_dot()
    dot_string += "}\n"
    with open("table.dot", "w") as archivo:
        archivo.write(dot_string)
    os.system("dot -Tpng table.dot -o table.png")
    print("¡Gráfica generada en table.png!")

class App:
    def __init__(self, master):
        self.master = master
        self.master.title('Menú Principal')

        # Definiendo el tamaño de la ventana y centrándola en la pantalla
        window_width = 1280
        window_height = 720
        screen_width = master.winfo_screenwidth()
        screen_height = master.winfo_screenheight()
        x_coordinate = (screen_width/2) - (window_width/2)
        y_coordinate = (screen_height/2) - (window_height/2)
        self.master.geometry(f'{window_width}x{window_height}+{int(x_coordinate)}+{int(y_coordinate)}')

        self.master.configure(bg='black')

        style_settings = {
            "bg": "lightgray",
            "fg": "black",
            "padx": 10,
            "pady": 5,
            "font": ("Helvetica", 10)
        }

        # Botones y widgets
        self.load_btn = tk.Button(master, text='Cargar datos', command=self.carga_datos_gui, **style_settings)
        self.load_btn.pack(pady=5)  # El padding 'pady' añade espacio entre los botones

        self.graph_btn = tk.Button(master, text='Buscar y generar imagen', command=self.search_and_graph, **style_settings)
        self.graph_btn.pack(pady=5)

        self.delete_btn = tk.Button(master, text='Eliminar elemento', command=lambda: self.eliminar(self.prompt_choice("Escoge un ID")), **style_settings)
        self.delete_btn.pack(pady=5)

        self.update_btn = tk.Button(master, text='Actualizar elemento', command=lambda: self.actualizar(self.prompt_choice("Escoge un ID"), self.prompt_choice("Escribe el nuevo nombre")), **style_settings)
        self.update_btn.pack(pady=5)

        # Inicializando el label
        self.elements_label = tk.Label(master, text="", **style_settings)
        self.elements_label.pack(pady=5)

        #imagen
        self.canvas = tk.Canvas(master, width=920, height=540, bg='black', scrollregion=(0,0,1000,1000))
        self.canvas.pack(side=tk.LEFT, pady=5)

        self.scroll_y = tk.Scrollbar(master, orient="vertical", command=self.canvas.yview)
        self.scroll_y.pack(side=tk.LEFT, fill="y")
        self.canvas.configure(yscrollcommand=self.scroll_y.set)

        self.image_label = tk.Label(self.canvas, bg='black')
        self.canvas.create_window((0,0), window=self.image_label, anchor="nw")

        self.image_label.bind("<Configure>", lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all")))

    def carga_datos_gui(self):
        filepath = filedialog.askopenfilename(filetypes=[("XML files", "*.xml")])
        if filepath:
            carga_datos(filepath)
            messagebox.showinfo("Carga exitosa", "Los datos se han cargado exitosamente")
            self.show_elements()

    def show_elements(self):
        print(elementos.mostrar())
        self.elements_label.config(text=str(elementos.mostrar()))

    def search_and_graph(self):
        chosen_id = self.prompt_choice("Escoge un ID",)
        if chosen_id:
            elemento = elementos.buscar_por_id(chosen_id)
            if elemento:
                graficar(elemento)
                messagebox.showinfo("Gráfica generada", "La gráfica se ha generado exitosamente")
                # Cargando y mostrando la imagen
                img = PhotoImage(file='table.png')
                self.image_label.config(image=img)
                self.image_label.image = img
            else:
                messagebox.showerror("Error", f"El elemento con ID {chosen_id} no existe")

    def prompt_choice(self, msg):
        # Una función para solicitar una elección al usuario
        choice = tk.simpledialog.askstring("Input", msg, parent=self.master)
        return choice

    def eliminar(self, id_val):
        if elementos.eliminar(id_val):
            messagebox.showinfo("Eliminado", f"El elemento con ID {id_val} ha sido eliminado")
            #limpiar la imagen y generar de nuevo el label1
            self.image_label.config(image=None)
            self.image_label.image = None
            self.show_elements()
        else:
            messagebox.showerror("Error", f"El elemento con ID {id_val} no existe")

    def actualizar(self, id_val, nuevo_nombre):
        if elementos.actualizar(id_val, nuevo_nombre):
            messagebox.showinfo("Actualizado", f"El elemento con ID {id_val} ha sido actualizado")
            self.show_elements()
            self.image_label.config(image=None)
            self.image_label.image = None
        else:
            messagebox.showerror("Error", f"El elemento con ID {id_val} no existe")

def main():
    root = tk.Tk()
    app = App(root)
    root.mainloop()

if __name__ == '__main__':
    main()


"""Heuristic link prediction models based on semantic similarity
between source element's label and target screen's text content."""

from link_prediction.models.heuristics.text_similarity.classification import (
    TextSimilarityClassifier,
)

class LabelTextSimilarityClassifier(TextSimilarityClassifier):
    def __init__(
        self, similarity_threshold=0.122
    ):  # value from optimization: similarity_threshold=0.12176566857218385
        """Link elements to pages if the element label and the text on
        the page meet a predefined similarity threshold."""
        super().__init__(similarity_threshold)

def main() -> None:
    """Create model instance and test its properties."""
    model = LabelTextSimilarityClassifier()

if __name__ == "__main__":
    main()


# Python program to find current
# weather details of any city
# using openweathermap api

# import required modules
import requests, json

# Enter your API key here
api_key = "Your_API_Key"

# base_url variable to store url
base_url = "http://api.openweathermap.org/data/2.5/weather?"

# Give city name
city_name = input("Enter city name : ")

# complete_url variable to store
# complete url address
complete_url = base_url + "appid=" + api_key + "&q=" + city_name

# get method of requests module
# return response object
response = requests.get(complete_url)

# json method of response object
# convert json format data into
# python format data
x = response.json()

# Now x contains list of nested dictionaries
# Check the value of "cod" key is equal to
# "404", means city is found otherwise,
# city is not found
if x["cod"] != "404":

	# store the value of "main"
	# key in variable y
	y = x["main"]

	# store the value corresponding
	# to the "temp" key of y
	current_temperature = y["temp"]

	# store the value corresponding
	# to the "pressure" key of y
	current_pressure = y["pressure"]

	# store the value corresponding
	# to the "humidity" key of y
	current_humidiy = y["humidity"]

	# store the value of "weather"
	# key in variable z
	z = x["weather"]

	# store the value corresponding
	# to the "description" key at
	# the 0th index of z
	weather_description = z[0]["description"]

	# print following values
	print(" Temperature (in kelvin unit) = " +
					str(current_temperature) +
		"\n atmospheric pressure (in hPa unit) = " +
					str(current_pressure) +
		"\n humidity (in percentage) = " +
					str(current_humidiy) +
		"\n description = " +
					str(weather_description))

else:
	print(" City Not Found ")

Find current weather of any city using openweathermap API.py


# -*- coding:utf-8 -*-
from datetime import timedelta
from celery import Celery
from celery.schedules import crontab
from flask import current_app

# object
class Config:
    BROKER_URL = "redis://localhost:6379/1"
    CELERY_RESULT_BACKEND = "redis://localhost:6379/1"
    CELERY_ENABLE_UTC = False
    CELERY_ACCEPT_CONTENT = ["pickle", "json", "msgpack", "yaml"]

    CELERYBEAT_SCHEDULE = {

        "interrupt_timer": {
            "task": "imhm.utils.async.functions.interrupt_timer",
            "schedule": timedelta(seconds=60)
        },

        "warning_processor": {
            "task": "imhm.utils.async.functions.warning_processor",
            "schedule": timedelta(seconds=15)
        }
    }

celery_app = Celery(current_app, include=["functions"])
celery_app.config_from_object(Config)


import numpy as np
import cv2,cozmo,time,PIL
import cv2.aruco as aruco

def myProgram(robot,dict_name=aruco.DICT_5X5_100):
  adict = aruco.Dictionary_get(dict_name)
  params = aruco.DetectorParameters_create()

  robot.camera.image_stream_enabled = True

  #wait until images have started
  while(True):
    if(robot.world.latest_image is not None):
      break
    else:
      time.sleep(0.2)

  #continuously look for aruco tags
  while(True):
    cozmoim = robot.world.latest_image.raw_image
    cozmoim = cozmoim.resize((cozmoim.width*2,cozmoim.height*2))
    curim = np.array(cozmoim).astype(np.uint8)
    gray = cv2.cvtColor(curim,cv2.COLOR_BGR2GRAY)
    (corners,ids,rejected) = aruco.detectMarkers(gray,adict,parameters=params)

    annotated_im = aruco.drawDetectedMarkers(curim,corners,ids)

    cv2.imshow('frame',annotated_im)
    if(cv2.waitKey(1) & 0xFF == ord('q')):
      break

cozmo.robot.Robot.drive_off_charger_on_connect = False
cozmo.run_program(myProgram)
cv2.destroyAllWindows()


import ephem
import datetime
"""
m = ephem.Mars('2016')
print(ephem.constellation(m))

m = ephem.Mars('2016/7/20')
#print('%s %s %.10f' % (m.name, m.elong, m.size))

print('%s %s %.10f' % (m.ra, m.dec, m.size))
"""

#Function to define date range to iterate over
def daterange( start_date, end_date ):
    if start_date <= end_date:
        for n in range( ( end_date - start_date ).days + 1 ):
            yield start_date + datetime.timedelta( n )
    else:
        for n in range( ( start_date - end_date ).days + 1 ):
            yield start_date - datetime.timedelta( n )

start = datetime.date( year = 2017, month = 1, day = 1 )
end = datetime.date( year = 2017, month = 12, day = 31 )

#Generate RA and Dec for Sun over date range
for date in daterange( start, end ):
    m = ephem.Sun(date)
    print ('{} {} {}'.format(date, m.ra, m.dec))

#Export to text file


from flask import Flask
from flask_compress import Compress
from flask_socketio import SocketIO
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object('config')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SESSION_COOKIE_SECURE'] = True
app.config['SESSION_COOKIE_HTTPONLY'] = True
app.config['SESSION_COOKIE_SAMESITE'] = 'LAX'
Compress(app)

app.game_modes = []

for mode in app.config['GAME_MODES']:
    if mode == 'pictures':
        display_name = 'Pictures'
    elif mode.startswith('classic_'):
        lang = mode.split('classic_', 1)[1]
        display_name = f'Classic ({lang})'
    else:
        continue
    app.game_modes.append((mode, display_name))

socketio = SocketIO(app)
db = SQLAlchemy(app)

from . import models, routes, helper, websocket

#: create database if not exists
db.create_all()

if __name__ == '__main__':
    #: start flask
    app.run()

    #: start websocket
    socketio.run(app)


class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if len(prices) <= 1:
            return 0
        res = list()

        maxs = list()

        max_ = -1
        for price in prices[::-1]:
            if price > max_:
                max_ = price
            maxs.append(max_)
        maxs = maxs[::-1]

        for i in range(len(prices) - 1):
            res.append([prices[i], maxs[i]])

        res = [i[1] - i[0] for i in res]
        return max(max(res), 0)


from collections import Counter

def st_print(a):
	dic  = dict(Counter(a))
	keys = []
	values = []
	for key,value in dic.items():
		keys.append(key)
		values.append(value)

	m = len(values)-1
	while( m >= 0):
		for n in range(m):
			if(values[n] < values[n+1]):
				temp_va = values[n+1]
				values[n+1] = values[n]
				values[n] = temp_va
				temp_key = keys[n+1]
				keys[n+1] = keys[n]
				keys[n] = temp_key
			if(values[n] == values[n+1] and keys[n] > keys[n+1]):
				temp_key = keys[n+1]
				keys[n+1] = keys[n]
				keys[n] = temp_key
		m -=1
	string = ''
	for i in range(len(keys)):
		while(values[i] > 0):
			if(i != len(keys)-1 or values[i] != 1):
				string += str(keys[i])+' '
				values[i]-=1
			else:
				string += str(keys[i])+' '
				values[i]-=1
	print(string)
	return string

T = int(input())
x = 0
N,Arr = [],[]
while(x < T):
    N.append(int(input()))
    Arr.append([int(i) for i in input().split()])
    x+=1
result = []
for item in Arr :
    result.append(st_print(item))


from pathlib import Path
import unittest

import core

class Test(unittest.TestCase):
  def test_one(self):
    # to-do: specify a temporary data folder
    dir_path = core.ensure_data_folder()

    print(f'The data folder:{dir_path}')
    self.assertTrue(dir_path.exists())

    # Get Data base...
    db = core.DataBase(str(dir_path))
    self.assertTrue(Path(db.path).exists())

    # Save quote...
    q = {
      "author": 'author 1',
      "content": 'text',
      "year": 100
    } # the quote
    res = db.quotes.save(q)

    # Retrive quote...
    res = db.quotes.get({'author': q['author']})

    # Check if saved...
    self.assertEqual(res[0].content, q['content'])

    # Delete all quotes...
    res = db.quotes.delete()
    self.assertEqual(res['count'], 1)

    res = db.quotes.get()
    self.assertEqual(len(res), 0)

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

RIRIN (by Yhoxin Rossell and Hernán Guerrero)/src/test.py


def print_board(board):
    [print(f"| {' | '.join(el)} |") for el in board]

def create_board(is_start=False):
    if is_start:
        print('This is the numeration of the board:')
        return [
            ['1', '2', '3'],
            ['4', '5', '6'],
            ['7', '8', '9'],
        ]
    else:
        return [
            [' ', ' ', ' '],
            [' ', ' ', ' '],
            [' ', ' ', ' '],
        ]

def add_move_to_board(board, move, symbol, moves):
    length = len(board)
    if move % length == 0:
        c = length-1
        r = (move // 3) - 1
    else:
        c = (move % 3) - 1
        r = move // 3
    board[r][c] = symbol
    moves.append(move)


from rest_framework import generics
from rest_framework.response import Response
from rest_framework.decorators import api_view
from .serializers import TenantSerializer, ApartmentSerializer, LeaseSerializer, MaintenanceRequestSerializer
from .models import Tenant, Apartment, Lease, MaintenanceRequest

##### START OF TENANT VIEWS ######
class TenantList(generics.ListCreateAPIView):
    queryset = Tenant.objects.all()
    serializer_class = TenantSerializer

@api_view(["GET", "PUT", "PATCH", "DELETE"])
def tenant_by_name(request, name):
    try:
        tenant = Tenant.objects.get(name=name)
    except:
        return Response({"error": f"No tenants with name {name} found"}, status=404)

    if request.method == "GET":
        serializer = TenantSerializer(tenant)
        return Response(serializer.data)

    elif request.method == "PUT" or request.method == "PATCH":
        serializer = TenantSerializer(tenant, data=request.data) if request.method == "PUT" else TenantSerializer(tenant, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=201)
        return Response(serializer.errors, status=400)
    elif request.method == "DELETE":
        tenant.delete()
        return Response(status=204)

@api_view(["GET"])
def tenants_by_apartment(request, number):
    if request.method == "GET":
        tenants = Tenant.objects.filter(apartment__number=number)
        serializer = TenantSerializer(tenants, many=True)
        return Response(serializer.data)

##### START OF APARTMENT VIEWS #####
class ApartmentList(generics.ListCreateAPIView):
    queryset = Apartment.objects.all().order_by("number")
    serializer_class = ApartmentSerializer

@api_view(["GET", "PUT", "PATCH", "DELETE"])
def apartment_by_number(request, number):
    try:
        apartment = Apartment.objects.get(number=number)
    except:
        return Response({"error": f"No apartments with number {number} found"}, status=404)

    if request.method == "GET":
        serialzer = ApartmentSerializer(apartment)
        return Response(serialzer.data)

    elif request.method == "PUT" or request.method == "PATCH":
        serialzer = ApartmentSerializer(apartment, request.data) if request.method == "PUT" else ApartmentSerializer(apartment, request.data, partial=True)
        if serialzer.is_valid():
            serialzer.save()
            return Response(serialzer.data, status=201)
        return Response(serialzer.errors, status=400)

    elif request.method == "DELETE":
        apartment.delete()
        return Response(status=204)

@api_view(["GET"])
def available_apartments(request, end_date):
    try:
        apartments = Apartment.objects.filter(lease__end_date__lte=end_date, tenant__is_renewing=False)
    except Lease.DoesNotExist:
       return Response({"error": "No leases found"}, status=404)

    if request.method == "GET":
        serializer = ApartmentSerializer(apartments, many=True)
        return Response(serializer.data)

@api_view(["GET"])
def apartments_by_bedrooms(request, bedrooms):
    try:
        apartments = Apartment.objects.filter(bedrooms=bedrooms)
    except Apartment.DoesNotExist:
        return Response({"error": f"No apartments with {bedrooms} bedrooms found"}, status=404)

    if request.method == "GET":
        serializer = ApartmentSerializer(apartments, many=True)
        return Response(serializer.data, status=200)

##### START OF LEASE VIEWS #####
class LeaseList(generics.ListCreateAPIView):
    queryset = Lease.objects.all()
    serializer_class = LeaseSerializer

@api_view(["GET", "PUT", "PATCH", "DELETE"])
def lease_by_tenant_name(request, name):
    try:
        tenant = Tenant.objects.get(name=name)
    except:
        return Response({"error": f"No lease associated with {name} were found"}, status=404)

    lease = Lease.objects.get(tenant_id=tenant.id)
    if request.method == "GET":
        serializer = LeaseSerializer(lease)
        return Response(serializer.data, status=201)

    elif request.method == "PUT" or request.method == "PATCH":
        serializer = LeaseSerializer(lease, request.data) if request.method == "PUT" else LeaseSerializer(lease, request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=201)
        return Response(serializer.errors, status=400)

    elif request.method == "DELETE":
        lease.delete()
        return Response(status=204)

##### START OF MAINTENANCE RECORD VIEWS #####
class MaintenanceRecordList(generics.ListCreateAPIView):
    queryset = MaintenanceRequest.objects.all().order_by("-open_date")
    serializer_class = MaintenanceRequestSerializer

@api_view(["GET", "PUT", "PATCH", "DELETE"])
def maintenance_request_by_apartment(request, number):
    try:
        maintenance_request = MaintenanceRequest.objects.filter(apartment__number=number)
    except Apartment.DoesNotExist:
        return Response({"error": f"No maintenance requests associated with apartment {number} were found"}, status=404)

    if request.method == "GET":
        serializer = MaintenanceRequestSerializer(maintenance_request, many=True)
        return Response(serializer.data, status=200)
    elif request.method == "PUT" or request.method == "PATCH":
        serializer = MaintenanceRequestSerializer(maintenance_request, request.data) if request.method == "PUT" else MaintenanceRequestSerializer(maintenance_request, request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=201)
        return Response(serializer.errors, status=400)
    elif request.method == "DELETE":
        maintenance_request.delete()
        return Response(status=204)

@api_view(["GET"])
def open_maintenance_requests(request):
    try:
        maintenance_requests = MaintenanceRequest.objects.filter(close_date=None)
    except MaintenanceRequest.DoesNotExist:
        return Response({"error": "No open maintenance requests were found"}, status=404)

    if request.method == "GET":
        serializer = MaintenanceRequestSerializer(maintenance_requests, many=True)
        return Response(serializer.data, status=200)


import math
import numpy as np
from statsmodels.tsa.stattools import adfuller

def calculate_entropy(data, normalized=False):
    frequency = np.bincount(data)
    n = 0
    entropy = 0

    for i, x in enumerate(frequency):
        px = x / len(data)
        if px > 0:
            n += 1
            entropy += -px * math.log2(px)

    if normalized:
        if math.log2(n) > 0:
            normalized_ent = entropy / math.log2(n)
            return entropy, normalized_ent
    else:
        return entropy

def calculate_entropy_gain(entropy, constant, sum_entropy):
    entropy_gain = entropy - (constant * sum_entropy)
    return entropy_gain

def calculate_normalized_entropy_gain(entropy_gain, num_parameters):
    normalized_entropy_gain = entropy_gain / num_parameters
    return normalized_entropy_gain

def calculate_degree_of_ddos(entropy_gains, num_parameters):
    sum_normalized_entropy_gains = sum(entropy_gains)
    degree_of_ddos = sum_normalized_entropy_gains / num_parameters
    return degree_of_ddos

def detect_ddos_attack(data, significance_level=0.05):
    parameters = ['packet_count_per_second', 'byte_count_per_second', 'flow_duration_sec']
    entropy_gains = []
    num_parameters = len(parameters)

    for parameter in parameters:
        entropy = calculate_entropy(data[parameter])
        entropy_gain = calculate_entropy_gain(entropy, 0.1, sum(entropy_gains))
        entropy_gains.append(entropy_gain)

    normalized_entropy_gains = [calculate_normalized_entropy_gain(eg, num_parameters) for eg in entropy_gains]
    degree_of_ddos = calculate_degree_of_ddos(normalized_entropy_gains, num_parameters)

    if degree_of_ddos > 0.5:
        # Perform augmented Dickey-Fuller test
        adf_result = adfuller(data['packet_count'], regression='nc')

        if adf_result[1] < significance_level:
            return True

    return False

# Example usage
import pandas as pd

# Load dataset
dataset = pd.read_csv('FlowStatsfile.csv')

# Convert timestamp column to datetime if needed
dataset['timestamp'] = pd.to_datetime(dataset['timestamp'])

# Remove unnecessary columns if needed
dataset = dataset[['packet_count_per_second', 'byte_count_per_second', 'flow_duration_sec']]

is_ddos_attack = detect_ddos_attack(dataset)
if is_ddos_attack:
    print("DDoS attack detected!")
else:
    print("No DDoS attack detected.")


from art import logo
print(logo)
letters = [
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v','w','x','y','z'
    ]

def encode(word, shift):
    shifted_letters = []
    encoded_word = ''
    # creating our shifted letters from the default letters
    for x in range(shift,len(letters)):
        shifted_letters.append(letters[x])
    # adding the shifted letters to the back of our already shifted letters
    # so that it will also be 26 alphabetical letters
    for x in range(shift):
        shifted_letters.append(letters[x])
    # getting each letter in the inserted text by user
    for letter in word:
        # if an alphabet, then encode it by:
        if letter.isalpha():
            # tracing the position of the letter in the default letters
            position = letters.index(letter)
            # and using it to trace the same index in the shifted_letters
            encoded_word += shifted_letters[position]
        # if a number/space/symbol don't do anything, add it to the encoded text before moving to the next.
        else:
            encoded_word += letter
    # returning both the encoded word and the derived shifted_letters
    # why are we returing shifted_letters?
    # because our decode function will use it and not to be repeating ourself below:
    return encoded_word, shifted_letters

def decode(word,shift):
    decoded_word = ""
    # wow, this is where we made use of encode
    shifted_letters = encode(word, shift)[1]
    for letter in word:
        if letter == " ":
            decoded_word += letter
        elif letter.isdigit():
            decoded_word += letter
        elif letter.isalpha():
            positon = shifted_letters.index(letter)
            decoded_word += letters[positon]
        else:
            decoded_word += letter
    return decoded_word

start = input("Press any key to continue!!!\n")
# validating our start statement
while len(start) >= 0 :
    if len(start) == 0:
        print("OOPS, empty text is not allowed!!!")
        start = input("Press any key to continue!!!\n")
    # if the text is a string break the loop and continue the program
    elif start.isalpha():
        print("We are about to start now, gather all your text together...")
        break
    else:
        print("You are only meant to insert only alphabet keys not symbols\\numbers")
        start = input("Press any key to continue!!!\n")
# while start is a true statement
while start:
    # checking if user inserted start value as stop,no,end.
    if start=="stop" or start=="end" or start=="no":
        print("Thanks for making use of our program, will like to see you again!!!")
        break

    text = input("insert your text here...\n").lower()
    shift_num = int(input("What is your shift number\n"))
    coding = input("Do you want to encode or decode?\n (e\\d)").lower()
    # calling on encode function,if coding value is encode
    if coding=="encode" or coding=="e":
        print(encode(text,shift_num)[0])
    # if the user want to decode
    else:
        print(decode(text,shift_num))
    start = input("Press any key to continue!!!\n")


# This script gives some insight in to the benefits of PPR. Specifically, on a
# small dataset a NN may not have enough examples for gradient descent to run
# to completion, whereas for the cost of a little extra training time PPR can
# fully utilize few examples. As a result PPR's training error is always less
# than that for NN, and its testing error is usually less. Occasionally PPR's
# fit is irregular, and its testing error is worse; this can probably be
# remedied by adding regularization to the loss function and rederiving the
# optimization process to accommodate it.

import numpy
import time
import sys
sys.path.append("..")
from skpp import ProjectionPursuitRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.datasets import load_boston

mlp = MLPRegressor()
ppr = ProjectionPursuitRegressor()

X, Y = load_boston(return_X_y=True)

n = int(X.shape[0]*0.8)
temp = numpy.arange(n)
numpy.random.shuffle(temp)
training = temp[0:int(n*0.8)]
testing = temp[int(n*0.8):]

a = time.time()
mlp.fit(X[training], Y[training])
b = time.time()
ppr.fit(X[training], Y[training])
c = time.time()

Ymlp = mlp.predict(X[training])
Yppr = ppr.predict(X[training])

print("Average squared error per training example for MLPRegressor:",
	numpy.sum((Y[training] - Ymlp)**2)/Ymlp.size)
print("Average squared error per training example for ProjectionPursuitRegressor:",
	numpy.sum((Y[training] - Yppr)**2)/Yppr.size)

Ymlp = mlp.predict(X[testing])
Yppr = ppr.predict(X[testing])

print("Average squared error per testing example for MLPRegressor:",
	numpy.sum((Y[testing] - Ymlp)**2)/Ymlp.size)
print("Average squared error per testing example for ProjectionPursuitRegressor:",
	numpy.sum((Y[testing] - Yppr)**2)/Yppr.size)

print("Training time for MLPRegressor:", b-a)
print("Training time for ProjectionPursuitRegressor:", c-b)


class Node:
    def __init__(self, value):
        self.value = value
        self.nextNode = None

class SingleLinkedList:
    def __init__(self):
        self.head = None

    def printList(self):
        if self.head is None:
            print("Single Linked List is empty!")
        else:
            tempStr = ""
            while self.head != None:
                tempStr += f"{self.head.value}, "
                self.head = self.head.nextNode
            returnedStr = tempStr[:-2]
            print(f"[{returnedStr}]")

    def __repr__(self):
        tempStr = ""
        while self.head != None:
            tempStr += f"{self.head.value}, "
            self.head = self.head.nextNode
        returnedStr = tempStr[:-2]
        return f"[{returnedStr}]"

    def insertValueAtBegin(self, new_value):
        new_node = Node(new_value)
        self.head, new_node.nextNode = new_node, self.head

    def insertNodeAtBegin(self, new_node):
        self.head, new_node.nextNode = new_node, self.head

    def insertValueAtEnd(self, new_value):
        new_node = Node(new_value)
        if self.head is None:
            self.head = new_node
            return
        last_node = self.head
        while last_node.nextNode is not None:
            last_node = last_node.nextNode
        last_node = new_node

    def insertNodeAtEnd(self, new_node):
        if self.head is None:
            self.head = new_node
            return
        last_node = self.head
        while last_node.nextNode:
            last_node = last_node.nextNode
        last_node.nextNode = new_node

    def insertNodeAtNode(self, node, new_node):
        if node is None:
            print(f"Node does not exist!")
            return
        else:
            new_node.nextNode = node.nextNode
            node.nextNode = new_node

    def removeNode(self, node2remove):
        if node2remove is None:
            print(f"Node does not exist!")
            return
        node = self.head
        while node:
            if node.nextNode == node2remove:
                node.nextNode = node2remove.nextNode
                return
            elif node == node2remove:
                node.nextNode = node2remove.nextNode
                return
            node = node.nextNode

n1 = Node(1)
n2 = Node(2)
n3 = Node(3)
n4 = Node(4)
n5 = Node(5)
n6 = Node(6)

list_of_nodes = [n1,n2,n3,n4,n5]

myList = SingleLinkedList()

for node in list_of_nodes:
    myList.insertNodeAtEnd(node)

print(myList)
print("-----------------------------")

mySL = SingleLinkedList()
nodeA = Node("A")
mySL.head = nodeA

nodeB = Node("B")
nodeC = Node("C")
nodeD = Node("D")
nodeE = Node("E")

mySL.head.nextNode = nodeB
nodeB.nextNode = nodeC

# mySL.insertNodeAtNode(nodeB, nodeD)
# mySL.printList()
print("-----------------------------")
#
mySL.removeNode(nodeB)
mySL.printList()


# My timetable program - Mr H
import time

# Calculate current hour
timeNow = time.localtime()
hour = now.tm_hour

# Determine activity
if hour < 8:
    print('sleeping')
elif hour < 17:
    print('working')
elif hour < 22:
    print('relaxing')
else: print('sleeping')


import numpy as np
from sklearn import metrics, model_selection, multioutput
import lightgbm
from sklearn import ensemble
import tensorflow as tf
from tensorflow import keras
import tensorflow_addons as tfa
import matplotlib.pyplot as plt
from .utils import labels
import pandas as pd

def train_model(x, y, cv_folds, approach_name):

    if approach_name == 'independent':
        # Initialize an empty list to store f1 scores for each fold
        _scores = []

        # Iterate through each fold using its index and the corresponding training/validation indices
        for fold_idx, (idxT, idxV) in enumerate(cv_folds):

            # Define a multioutput classifier model using LGBMClassifier with a random seed of 42
            model = multioutput.MultiOutputClassifier(lightgbm.LGBMClassifier(random_seed=42))

            # Train the model on the training data
            model.fit(x.iloc[idxT], y.iloc[idxT])

            # Generate predictions using the validation data
            p = model.predict(x.iloc[idxV])

            # Calculate the average f1 score for each label using the validation data and the corresponding predictions
            avg_f1_score = np.mean([metrics.f1_score(y.iloc[idxV].iloc[:, i].to_numpy().astype(int), p[:, i].astype(int)) for i in range(4)])

            # Append the average f1 score to the scores list
            _scores.append(avg_f1_score)

            # Print the fold index and the average f1 score
            print("%d\t%.4f" % (fold_idx, avg_f1_score))

        # Return the mean f1 score across all folds
        return np.mean(_scores), model

    if approach_name == 'classifier_chains':
        _scores = []  # create a list to store F1 scores for each fold

        for fold_idx, (idxT, idxV) in enumerate(cv_folds):  # iterate through each fold
            p_list = []  # create a list to store predictions for each repetition
            avg_f1_score_list = []  # create a list to store F1 scores for each repetition

            for rep in range(3):  # repeat the process 3 times
                # create a Classifier Chain model with LightGBM as the base classifier
                model = multioutput.ClassifierChain(lightgbm.LGBMClassifier(random_seed=42), order="random", cv=5)
                # fit the model on training data
                model.fit(x.iloc[idxT].fillna(0.0), y.iloc[idxT])
                # make predictions on validation data
                p_rep = model.predict(x.iloc[idxV].fillna(0.0))
                # store predictions and F1 score for each repetition
                p_list.append(p_rep)
                avg_f1_score_list.append(np.mean([metrics.f1_score(y.iloc[idxV].iloc[:, i].to_numpy().astype(int), p_rep[:, i].astype(int)) for i in range(4)]))

            # calculate average predictions and F1 score across repetitions
            p = np.stack(p_list, axis=0).mean(0)
            avg_f1_score = np.mean([metrics.f1_score(y.iloc[idxV].iloc[:, i].to_numpy().astype(int), p[:, i].astype(int)) for i in range(4)])
            _scores.append(avg_f1_score)
            # print fold index and F1 scores for each repetition and overall F1 score for the fold
            print("%d\t%.4f | %.4f %.4f %.4f" % (fold_idx, avg_f1_score, avg_f1_score_list[0], avg_f1_score_list[1], avg_f1_score_list[2]))

        # Return the mean f1 score across all folds
        return np.mean(_scores), model

    if approach_name == 'native_extra_trees':

        _scores = [] # initialize a list to store f1 score of each fold

        # loop over each fold of the cross-validation
        for fold_idx, (idxT, idxV) in enumerate(cv_folds):

            # initialize an ExtraTreesClassifier model
            model = ensemble.ExtraTreesClassifier()

            # fit the model on training data and target labels
            model.fit(x.iloc[idxT].fillna(-100), y.iloc[idxT])

            # predict the target labels on validation data
            p = model.predict(x.iloc[idxV].fillna(-100))

            # calculate f1 score for each label and take average
            avg_f1_score = np.mean([metrics.f1_score(y.iloc[idxV].iloc[:, i].to_numpy().astype(int), p[:, i].astype(int)) for i in range(4)])

            # append the f1 score to the list
            _scores.append(avg_f1_score)

        # Return the mean f1 score across all folds
        return np.mean(_scores), model

    if approach_name == 'native_neural_net':

        _scores = []
        for fold_idx, (idxT, idxV) in enumerate(cv_folds):
            norm = keras.layers.Normalization()
            norm.adapt(x.iloc[idxT].fillna(0.0))

            model = keras.Sequential([
                norm,
                keras.layers.Dense(512, activation='relu'),
                keras.layers.Dropout(0.5),
                keras.layers.Dense(256, activation='relu'),
                keras.layers.Dropout(0.5),
                keras.layers.Dense(128, activation='relu'),
                keras.layers.Dense(y.shape[-1])
            ])
            model.compile(
                optimizer=keras.optimizers.Adam(5e-4),
                loss=keras.losses.BinaryCrossentropy(from_logits=True),
            )

            x_train = x.iloc[idxT].fillna(0.0).values.astype('float32')
            y_train = y.iloc[idxT].values.astype('int32')
            x_valid = x.iloc[idxV].fillna(0.0).values.astype('float32')
            y_valid = y.iloc[idxV].values.astype('int32')

            hist = model.fit(x_train, y_train, validation_data=(x_valid, y_valid), epochs=50, batch_size=32, verbose=0)
            plt.plot(hist.history['loss'], color='tab:blue')
            plt.plot(hist.history['val_loss'], color='tab:red')
            plt.title('Training and Validation Loss')
            plt.xlabel('Epoch')
            plt.ylabel('Loss')
            plt.legend(['Training Loss', 'Validation Loss'], loc='upper right')
            plt.savefig('training_validation_loss.png')

            p_valid = model.predict(x_valid, verbose=0)
            p_valid = (p_valid > 0.0).astype(int)
            avg_f1_score = np.mean([metrics.f1_score(y.iloc[idxV].iloc[:, i].to_numpy().astype(int), p_valid[:, i].astype(int)) for i in range(4)])
            _scores.append(avg_f1_score)
            print("%d\t%.4f" % (fold_idx, avg_f1_score))

        return np.mean(_scores), model

    if approach_name == 'multilabel_to_multiclass':
        y_ = y.copy()
        y_['code'] = y.apply(lambda x: '-'.join([str(xi) for xi in x]), axis=1)
        y_codes = y_.drop_duplicates()
        y_codes['idx'] = np.arange(y_codes.shape[0])
        y_class = y_.merge(y_codes, on='code', how='left')['idx']
        print(f"{y_codes.shape[0]} classes")

        _scores = []
        for fold_idx, (idxT, idxV) in enumerate(cv_folds):
            model = lightgbm.LGBMClassifier(random_seed=42)
            model.fit(x.iloc[idxT], y_class.iloc[idxT])

            p = model.predict(x.iloc[idxV])
            p = pd.DataFrame({'idx': p}).merge(y_codes, on='idx', how='left')[labels]
            avg_f1_score = np.mean([metrics.f1_score(y.iloc[idxV].iloc[:, i].to_numpy().astype(int), p.iloc[:, i].astype(int)) for i in range(4)])
            _scores.append(avg_f1_score)
            print("%d\t%.4f" % (fold_idx, avg_f1_score))

        return np.mean(_scores), model

def train_and_evaluate_model(x, y, cv_folds, approach_name):

    print("------------------------")
    print(f"Results for the approach {approach_name}:")
    # Train and evaluate the model using the cross-validation folds
    mean_f1_score, model = train_model(x, y, cv_folds, approach_name)

    # Print the mean f1 score across all folds
    print("--------------")
    print(" \t%.4f" % mean_f1_score)

    return mean_f1_score, model


from multiprocessing import Pool
from datetime import datetime
from itertools import chain
from utils.cache import LMDBClient
from utils import string_utils
from utils import data_utils

def transform_feature(data, f_name, k=1):#变换特征 输入数据， 特征名
    if type(data) is str:
        data = data.split() #按空白字符分词
    assert type(data) is list #data 是一个 str列表
    features = [] #列表
    for d in data: #枚举d
        features.append("__%s__%s" % (f_name.upper(), d)) #（特征名的大写， 单词） 中间的%代表格式化操作 __$f_name.upper()$__$d$
    return features #返回的是字符串列表， 每一项是 __$f_name的大写$__$特征单词$ 的形式

def extract_common_features(item): #提取共同特征
    title_features = transform_feature(string_utils.clean_sentence(item["title"], stemming=True).lower(), "title") #将title项提取特征
    keywords_features = []
    keywords = item.get("keywords") #提取 keywords 项
    if keywords:
        keywords_features = transform_feature([string_utils.clean_name(k) for k in keywords], 'keyword') #提取keyword中的单词， 去除连接符
    fields_features = []
    fields = item.get('fields')
    if fields:
        fields_features = transform_feature([string_utils.clean_name(k) for k in fields], 'fields')
    venue_features = [] #提取机构venue项
    venue_name = item.get('venue', '')
    if venue_name:
        if len(venue_name) > 2:
            venue_features = transform_feature(string_utils.clean_sentence(venue_name.lower()), "venue") #小写化 去除分界符 变换特征
    abst_features = []
    abst = item.get('abst')
    if abst:
        abst_features = transform_feature(string_utils.clean_sentence(abst.lower()), "abst")
    return title_features, keywords_features, venue_features, abst_features, fields_features

def extract_author_features(item, order=None): #提取作者特征 item中的order
    title_features, keywords_features, venue_features, abst_features, fields_features = extract_common_features(item) #提取共同特征 标题， 关键字， 收录机构
    author_features = []
    for i, author in enumerate(item["authors"]): #枚举第i个作者， author
        if order is not None and i != order: #找到所要的 第order个作者
            continue
        name_feature = [] #姓名特征
        org_features = [] #机构特征
        org_name = string_utils.clean_name(author.get("org", "")) #格式化机构名 按".", "-", " "分割 小写化
        if len(org_name) > 2:
            org_features.extend(transform_feature(org_name, "org")) #列表加列表
        for j, coauthor in enumerate(item["authors"]): #枚举 合作者
            if i == j:
                continue
            coauthor_name = coauthor.get("name", "") #获得名字
            coauthor_org = string_utils.clean_name(coauthor.get("org", "")) #获得格式化机构名
            if len(coauthor_name) > 2:
                name_feature.extend(
                    transform_feature([string_utils.clean_name(coauthor_name)], "name") #格式化 与 特征变换
                ) #将合作者名特征加入 名字特征中
            if len(coauthor_org) > 2:
                org_features.extend(
                    transform_feature(string_utils.clean_sentence(coauthor_org.lower()), "org") #格式化 与 特征变换
                ) #将合作者机构特征加入 机构特征中
        author_features.append(
            name_feature + org_features + title_features + keywords_features + venue_features + abst_features + fields_features
        ) #将以上特征 都 整合 到 作者特征 中
    author_features = list(chain.from_iterable(author_features)) #创建 为 迭代器 列表
    return author_features # 到这里 就是把 各个特征 对应的单词列表 合并到一个列表里了


#coding=utf-8
import os,re,xlrd,xlwt
os.chdir("C:\\Users\\quiet_wang\\Desktop\\python")
p = re.compile('.*本科.*')
f = xlrd.open_workbook('20181021105223_34042.xls')
sheet1 = f.sheet_by_index(1)
ncols = sheet1.ncols
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = "Times New Roman"
font.height = 220
font.bold = False
style.font = font
aln = xlwt.Alignment()
aln.horz = 0
aln.shri = 0
aln.vert = 0
style.alignment = aln
bd = xlwt.Borders()
bd
i=0
row_num=[]
for r in sheet1.col_values(13):
    if p.search(r) is not None:
        #print(r)
        row_num.append(i)
    i=i+1
fw=xlwt.Workbook(encoding='utf-8')
sheet2=fw.add_sheet('sheet2', cell_overwrite_ok=True)
for m in range(0,2):
    print(m)
    for n in range(0,ncols):
        sheet2.write(m, n, label=sheet1.cell_value(m, n), style=style)
for m in range(2, len(row_num)+2):
    k=row_num[m-2]
    for n in range(0, ncols):
        sheet2.write(m, n, label=sheet1.cell_value(k, n), style=style)
fw.save("t2.xls")


# Time - O(N) | Space - O(1)
def findDisappearedNumbers(nums):
    for num in nums:
        t = abs(num) - 1
        if nums[t] > 0:
            nums[t] *= -1

    res = []
    for idx, num in enumerate(nums):
        if num > 0:
            res.append(idx + 1)

    return res

if __name__ == '__main__':
    nums1 = [4, 3, 2, 7, 8, 2, 3, 1]
    nums2 = [1, 1]
    print(findDisappearedNumbers(nums1))  # [5, 6]
    print(findDisappearedNumbers(nums2))  # [2]


from collections import Counter

with open('input.txt', 'r') as f:
    raw_data = [line.split('\n') for line in f.read().split('\n\n')]

if __name__ == '__main__':

    group_yes = []
    common_yes = []

    for data in raw_data:
        n_answers = len(data)
        answers = ''.join(data)
        answers_count = Counter(answers)

        group_yes.append(len(answers_count))
        common_yes.append(len([answers_count[q] for q in answers_count if answers_count[q] == n_answers]))

    print(f'Union {sum(group_yes)}')
    print(f'Intersection {sum(common_yes)}')


import requests
r =requests.get('https://api.mcsrvstat.us/2/216.49.241.192')
import json
from random import randint
# parse x:
y = r.json()
#return('The server is up! \nPlayers online: '+str(test.r.json()['players']['list']))

# the result is a Python dictionary:
print(y["online"])
print(r.text)
#return('The server is up! \nPlayers online: '+str(test.r.json()['players']['list']))
def respond():
  global r
  r =requests.get('https://api.mcsrvstat.us/2/216.49.241.192')
  response = ('The server is up! \n')
  '''if r.json()['players']['online']==0:
    response.join('No one is online.')
  else:
    response = response+'Players online: '
    for i in range(0,r.json()['players']['online']):
      response = response+r.json()['players']['list'][i]+'
  '''
  print(response)
  return(response)

print(respond())


'''
Approach
    - 한번에 최대 2명
    - 최대한 적게 사용하여 모든 사람을 구출
    - 1<=n<=5만, 40<=w<=240, 40<=limit<=240

>>> 그리디
    : w가 크거나 작은 순서대로 집어넣어본다
      -> fail
>>> limit을 만족하는 사람을 태운다
    : p를 꺼내고, (limit-p)-다른사람, 이 0에 가장 가까운 사람을 태운다

'''
from collections import deque
def solution(people, limit):
    answer = 0
    people.sort(reverse=True)
    dq = deque(people)
    while dq:
        fst = dq.popleft()
        if not dq:
            answer +=1
            print(fst)
            break
        scd = dq.pop()

        if fst + scd <= limit:
            print(fst,scd)
            answer +=1
        else:
            print(fst)
            answer +=1
            dq.append(scd)

    return answer

프로그래머스/lv2/42885. 구명보트/구명보트.py


from keras.models import load_model
from keras.datasets import mnist
import matplotlib

import matplotlib.pyplot as plt
from splice import splice_image
import numpy
tf.logging.set_verbosity(tf.logging.ERROR)

model = load_model('digits.h5')
import cv2
#odel.summary()

def prepare(img):
	img_size = 28
	#img_arr = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
	new_arr = cv2.resize(img, (img_size, img_size)) #resize for CNN
	new_arr = numpy.invert(new_arr) #mnist dataset has data that is inverted so we must invert our input
	#plt.imshow(new_arr)
	#plt.show()
	return new_arr.reshape(-1, img_size, img_size, 1)

'''
(X_train, y_train), (X_test, y_test) = mnist.load_data()
#plt.imshow(X_train[1], cmap="gray")
#plt.show()
img = X_train[1]
img = img.reshape(-1, 28, 28, 1)
'''

def blankSpot(blank):

	s = set()
	for i in range(10):
		for j in range(10):
			#for k in range(2):
			s.add(blank[i + 10][j + 10])
	if len(s) == 1:
		return True
	else:
		return False

def CNN_predict_single(image):
	if blankSpot(image) == True:
		return int(0)
	else:
		prediction = model.predict_classes(prepare(image))
		return(prediction[0])
'''
def CNN_predict_single(image):
	print(blankSpot(image))
	prediction = model.predict_classes(prepare(image))
	return(prediction[0])
'''
def CNN_predict_grid(filepath):
	image_grid = cv2.imread(filepath,cv2.IMREAD_GRAYSCALE)
	imgArr = splice_image(image_grid)
	grid = []
	for i in range(81):
		grid.append(CNN_predict_single(imgArr[i]))
	return grid
def hello():
    return 'Hi'


#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import json
import logging
import re
import shutil
import sys

import os
import zipfile
import Config as Config

import jpype

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

from libs.modules.BaseModule import BaseModule

logging.basicConfig(stream=sys.stdout, format="%(levelname)s: %(message)s", level=logging.INFO)
log = logging.getLogger(__name__)

class BufanApp(BaseModule):
    def doSigCheck(self):
        if self.host_os == "android":
            return self._find_main_activity("com.bufan.app")
        elif self.host_os == "ios":
            log.error("not support yet.")
            return False
        return False

    def doExtract(self, working_folder):

        extract_folder = self._format_working_folder(working_folder)

        if os.access(extract_folder, os.R_OK):
            shutil.rmtree(extract_folder)
        os.makedirs(extract_folder, exist_ok=True)
        tmp_folder = os.path.join(os.getcwd(), extract_folder, "tmp")
        os.makedirs(tmp_folder, exist_ok=True)

        zf = zipfile.ZipFile(self.detect_file, 'r')
        appUrl = ""
        if "assets/source/dconfig.json" in zf.namelist():
            config_file = zf.extract("assets/source/dconfig.json", tmp_folder)
            fo = open(config_file, "r+")
            config_json = json.load(fo)
            appUrl = config_json["url"]
            shutil.copy(config_file, extract_folder)
        else:
            """
            get keys from smali code
            filename:  com.bufan.util.JwtUtils.smali
            """
            self._apktool(tmp_folder)
            JwtUtilsPath = os.path.join(tmp_folder, "smali/com/bufan/utils/JwtUtils.smali")
            JwtUtilsFile = open(JwtUtilsPath, "r+")
            JwtUtilsStr = JwtUtilsFile.read()
            keys = []
            patterns = [".field public static final TAG:Ljava/lang/String; = \"(.*?)\"",
                        ".field public static final key1:Ljava/lang/String; = \"(.*?)\"",
                        ".field public static final key2:Ljava/lang/String; = \"(.*?)\"",
                        ".field public static final key3:Ljava/lang/String; = \"(.*?)\"",
                        ".field public static final key4:Ljava/lang/String; = \"(.*?)\"",
                        ".field public static final key5:Ljava/lang/String; = \"(.*?)\"",
                        ".field public static final key6:Ljava/lang/String; = \"(.*?)\"",
                        ".field public static final key7:Ljava/lang/String; = \"(.*?)\"",
                        ".field public static final key8:Ljava/lang/String; = \"(.*?)\""]
            for pattern in patterns:
                pt = re.findall(pattern, JwtUtilsStr)
                keys.extend(pt)
            JwtUtilsFile.close()

            config_file = zf.extract("assets/source/config.json", tmp_folder)
            shutil.copy(config_file, extract_folder)
            jvmPath = jpype.getDefaultJVMPath()
            if not jpype.isJVMStarted():
                jpype.startJVM(jvmPath, '-ea',
                               '-Djava.class.path={0}'.format(Config.Config["decrypt_jar"]),
                               convertStrings=False)

            jclass = jpype.JClass("com.ResDecode.Main")()
            """
            get appUrl
                method: com.ResDecode.Main.get_appUrl
                input: (String) encoded JSONString, (String[]) keys
                output: (String) url
            """
            try:
                appUrl = str(jclass.get_appUrl(config_file, keys))  # cast to str
            except:
                self._log_error(os.path.basename(__file__), self.detect_file, "foo")

        self._dump_info(extract_folder, appUrl)
        # jpype.shutdownJVM()

        # clean env
        shutil.rmtree(tmp_folder)

        return extract_folder, appUrl

def main():
    # f = "./test_case/bugs/14b03ed97138c6f99f71f5bd633548c4.apk"  # need decode
    f = "./test_case/BufanApp/世耀国际_bufan.apk"
    f = "./test_case/BufanApp/惠普分期_bufan.apk"  # need decode
    # f = "./test_case/BufanApp/淘客_bufan.apk"
    bufan = BufanApp(f, "android")
    if bufan.doSigCheck():
        logging.info("BufanApp signature Match")
        extract_folder, launch_path = bufan.doExtract("./working_folder")
        log.info("{} is extracted to {}, the start page is {}".format(f, extract_folder, launch_path))
    return

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


from Game import *

def value(hand):
    has_ace=False
    val=0
    for card in hand:
        if card.rank==1:
            has_ace=True
        val+=card.rank

        if card.rank>10:
            val+=10
        else:
            val+=card.rank

    if has_ace and val+10<21:
        val+=10

    return val

def play(agents,number_of_decks=1,
        number_of_games=1,display=True):

    all_scores=[]

    for agent in agents:
        agent.score=0

    for game in range(number_of_games):

        deck=makedeck(number_of_decks,shuffle=True)
        hands=[]
        # deal two for every agent
        for agent in agents:
            agent.hand=deal(deck,2)
            hands.append(agent.hand)

        for agent in agents:
            # shown is all of the cards for all agents,
            # except the first card
            shown={}
            for agent2 in agents:
                shown[agent2.name]=agent2.hand[1:]

            hand=agent.hand

            if display:
                print(agent.name,"'s move")
                print("\tShown is ",shown)
                print("\tYour hand is:",hand)

            while True:  # keep going until stay or bust
                move=agent.move(hand,shown,agent)
                if display:
                    print("\t Move: ",move)
                if move=='s':
                    break
                agent.hand.extend(deal(deck))

                if display:
                    print(agent.name)
                    print("\tShown is ",shown)
                    print("\tYour hand is:",hand)

                if value(agent.hand)>21:
                    if display:
                        print("\tBust!")
                    break

        if display:
            print("Results")
            for agent in agents:
                print(agent.name)
                print("\t",agent.hand)
                print("\t",value(agent.hand))
                if value(agent.hand)>21:
                    print("\t\tBust!")

        winners=[]
        max_so_far=-1
        for agent in agents:
            score=value(agent.hand)
            if score>21:  # busting doesn't win
                continue
            if score>max_so_far:
                winners=[agent]
                max_so_far=score
            elif score==max_so_far:
                winners.append(agent)
                max_so_far=score
            else:
                pass

        # dealer wins in a tie
        if dealer_agent in winners:
            winners=[dealer_agent]

        if display:
            print("Winners:")
            for agent in winners:
                print("\t",agent.name)

        for agent in winners:
            agent.score+=1

        for agent in agents:
            try:
                if agent in winners:
                    agent.post('win',agent)
                else:
                    agent.post('lose',agent)
            except (AttributeError,KeyError):
                pass

        if display:
            print("=================")

    return [agent.score for agent in agents]

def dealer_move(hand,shown,info=None):

    val=value(hand)
    if val<16:
        return 'h'
    else:
        return 's'

def dealer_move2(hand,shown,info=None):

    val=value(hand)
    if val<16:
        return 'h'
    else:
        return 's'

def human_move(hand,shown,info=None):
    move=input('Hit or stay? ')
    move=move.lower()[0]
    return move

def random_move(hand,shown,info=None):
    return random.choice(['h','s'])

def get_state(hand,shown):
    state=value(hand)

    has_ace=False
    for card in hand:
        if card.rank==1:
            has_ace=True

    if has_ace:
        state+=100

    return state

def Q_BJ_move(hand,shown,info):
    Q=info.Q
    last_action=info.last_action
    last_state=info.last_state
    alpha=info.alpha
    gamma=info.gamma
    epsilon=info.epsilon

    state=get_state(hand,shown)

    if not state in Q:
        Q[state]=Table()
        for action in ['h','s']:
            Q[state][action]=0.0

    if random.random()<epsilon: # random move
        action=random_choice(Q[state])
    else:
        action=top_choice(Q[state])

    if not last_action is None:
        r=0.0
        Q[last_state][last_action]+=alpha*(r+
                gamma*max([Q[state][a] for a in Q[state]])-
                Q[last_state][last_action])

    info.last_action=action
    info.last_state=state

    return action

def Q_BJ_after(status,info):
    Q=info.Q
    last_action=info.last_action
    last_state=info.last_state
    alpha=info.alpha
    gamma=info.gamma

    if status=='lose':
        r=-1.0
    elif status=='win':
        r=1.0
    else:
        r=0.0

    if not last_action is None:
        Q[last_state][last_action]+=alpha*(r-
                Q[last_state][last_action])

    info.last_action=None
    info.last_state=None

learning_agent=Agent(Q_BJ_move)
learning_agent.post=Q_BJ_after
learning_agent.name='Smarty'
learning_agent.Q=Remember()
learning_agent.last_action=None
learning_agent.last_state=None

human_agent=Agent(human_move)
human_agent.name='Human'

dealer_agent=Agent(dealer_move)
dealer_agent.name='Dealer'

dealy_agent=Agent(dealer_move2)
dealy_agent.name='Dealy'

random_agent=Agent(random_move)
random_agent.name='Bob'

agents=[learning_agent,dealer_agent]

for i in range(10):

    learning_agent.alpha=0.3  # learning rate
    learning_agent.gamma=0.9  # memory
    learning_agent.epsilon=0.1  # chance of making random move
    scores=play(agents,
                number_of_games=5,
                display=False)

    Remember(learning_agent.Q)

    learning_agent.alpha=0.0  # learning rate
    learning_agent.gamma=0.9  # memory
    learning_agent.epsilon=0  # chance of making random move
    scores=play(agents,
                number_of_games=100,
                display=False)

    print(scores)


from Main_program.code import coord_functions
import pygame

class Car(pygame.sprite.Sprite):
    def __init__(self,  df, color):
        super().__init__()
        self.image = pygame.Surface((12, 12))  # De auto (12x12 pixel rechthoek)
        self.image.fill(pygame.Color(color))
        self.position = [-200, -200]  # Positie buiten het scherm
        self.rect = self.image.get_rect(center=self.position)  # Tekent het vierkantje
        self.df = df

    def update(self, timestamp):
        """De update functie wordt elke frame gerund. Hierin word de nieuwe positie van de auto berekend"""
        df_cor = self.df.loc[self.df['timestamp'] == timestamp][['latitude', 'longitude']]

        if len(df_cor) == 1:  # Doet geen update als er geen positie is voor de auto op deze tijd
            df_cor = df_cor.reset_index()
            self.position = coord_functions.latlngToScreenXY(df_cor['latitude'][0], df_cor['longitude'][0])
            self.rect.center = self.position


# -*- coding: utf-8 -*-
from tinyms.core.pool import WebSocketPoolFactory

__author__ = 'i@tinyms.com'

import os
import shutil
from functools import wraps
from tornado.web import RequestHandler
from tornado.websocket import WebSocketHandler
from .plugin import ObjectPool, EmptyClass, do_action
from tinyms.core.util import Utils, DataResult
from tinyms.core.cache import CacheFactory

class IWebHandler(RequestHandler):
    __key_account_id__ = "auth_account_id"
    __key_account_name__ = "auth_account_name"
    __key_account_role_name__ = "auth_account_role_name"
    __key_auth__ = "__auth__"

    def __init__(self, application, request, **kwargs):
        RequestHandler.__init__(self, application, request, **kwargs)

    def prepare(self):
        if hasattr(self.__class__, IWebHandler.__key_auth__):
            is_auth = getattr(self.__class__, IWebHandler.__key_auth__)
            if is_auth:
                if not self.get_current_user():
                    self.redirect("/signin")

    def data_received(self, chunk):
            pass

    def auth(self, points=set()):
        """
        细微控制数据输出,不产生页面跳转相关动作
        :param points:
        :return:
        """
        if self.get_current_account_points().issuperset(points):
            return True
        return False

    def get_template_namespace(self):
        namespace = dict(
            handler=self,
            request=self.request,
            current_user=self.current_user,
            locale=self.locale,
            _=self.locale.translate,
            static_url=self.static_url,
            xsrf_form_html=self.xsrf_form_html,
            reverse_url=self.reverse_url,
            # Add to auth current user security points
            auth=self.auth,
            do_action=do_action
        )
        namespace.update(self.ui)
        return namespace

    def get_login_url(self):
        return "/signin"

    def write_error(self, status_code, **kwargs):
        self.clear_all_cookies()
        if status_code == 401:
            self.redirect("/signin")
        elif status_code == 403:
            self.render("core/err.html", reason="访问禁止")
        elif status_code == 404:
            self.render("core/err.html", reason="访问禁止")
        else:
            self.render("core/err.html", reason="服务器内部错误")

    def get_current_user(self):
        """
        user id, str
        :return: user id, str type
        """
        id_ = self.get_secure_cookie(IWebHandler.__key_account_id__)
        if id_:
            return id_.decode('utf-8')
        return None

    def get_current_account_points(self):
        """
        account security points: set('key1','key2',..)
        :return:
        """
        role_name = self.get_secure_cookie(IWebHandler.__key_account_role_name__)
        if role_name:
            role_obj = ObjectPool.roles.get(role_name.decode('utf-8'), None)
            if role_obj:
                return role_obj.points()
        return set()

    def get_current_account_name(self):
        """
        current account user_login field.
        :return:
        """
        name = self.get_secure_cookie(IWebHandler.__key_account_name__)
        if name:
            return name.decode('utf-8')
        return ""

    def wrap_entity(self, entity_object, excude_keys=None):
        """
        把参数值填充到对象对应属性中，针对ORM中的Entity
        :param entity_object:
        :param excude_keys:
        :return:
        """
        if not excude_keys:
            excude_keys = ["id"]
        dict_ = dict()
        args = self.request.arguments
        for key in args.keys():
            if excude_keys.count(key) != 0:
                continue
            dict_[key] = self.get_argument(key)
        entity_object.dict(dict_)
        return entity_object

    def wrap_params_to_dict(self):
        dict_ = dict()
        args = self.request.arguments
        for key in args:
            dict_[key] = self.get_argument(key)
        return dict_

    def wrap_params_to_obj(self):
        obj = EmptyClass()
        args = self.request.arguments
        for key in args:
            setattr(obj, key, self.get_argument(key))
        return obj

    def get_webroot_path(self):
        return os.path.dirname(self.get_template_path()) + "/"

def api(pattern="/", method="get", auth=False, points=set(), cache_key="", cache_time=300, cache_storage="mem"):
    def handle_func(func):
        @wraps(func)
        def wrap_func(*args_, **kwargs_):
            func.__controller__ = True
            pattern_ = "/api%s" % pattern
            cls_name = "tinyms_web_api_%s" % Utils.md5(pattern_)
            handler = type(cls_name, (IWebHandler,), {})
            handler.cache_key = cache_key
            handler.cache_time = cache_time
            handler.cache_storage = cache_storage

            def unauth(*args, **kwargs):
                this = args[0]
                result = DataResult()
                result.success = False
                result.message = "UnAuth"
                result.data = ""
                this.write(Utils.encode(result.dict()))

            def generic_func(*args, **kwargs):
                this = args[0]
                this.set_header("Content-Type", "application/json; charset=UTF-8")
                if auth:
                    account_id = this.get_current_user()
                    if not account_id:
                        unauth(*args, **kwargs)
                    if len(points) > 0:
                        points_ = this.get_current_account_points()
                        same = points & points_
                        if len(same) > 0:
                            if this.cache_key:
                                c = CacheFactory.cache(this.cache_storage)
                                v = c.get(this.cache_key)
                                if not v:
                                    result = func(*args, **kwargs)
                                    v = Utils.encode(result.dict())
                                    c.set(this.cache_key, v, this.cache_time)
                            else:
                                result = func(*args, **kwargs)
                                v = Utils.encode(result.dict())
                            this.write(v)
                        else:
                            unauth(*args, **kwargs)
                    else:
                        if this.cache_key:
                            c = CacheFactory.cache(this.cache_storage)
                            v = c.get(this.cache_key)
                            if not v:
                                result = func(*args, **kwargs)
                                v = Utils.encode(result.dict())
                                c.set(this.cache_key, v, this.cache_time)
                        else:
                            result = func(*args, **kwargs)
                            v = Utils.encode(result.dict())
                        this.write(v)
                else:
                    if this.cache_key:
                        c = CacheFactory.cache(this.cache_storage)
                        v = c.get(this.cache_key)
                        if not v:
                            result = func(*args, **kwargs)
                            v = Utils.encode(result.dict())
                            c.set(this.cache_key, v, this.cache_time)
                    else:
                        result = func(*args, **kwargs)
                        v = Utils.encode(result.dict())
                    this.write(v)

            if "post" == method:
                handler.post = generic_func
            else:
                handler.get = generic_func

            if pattern not in ObjectPool.api.keys():
                ObjectPool.api[pattern_] = (pattern_, handler)

        return wrap_func

    return handle_func

def route(pattern="/", method="get", auth=False, points=set(), cache_key="", cache_time=300, cache_storage="mem"):
    def handle_func(func):
        @wraps(func)
        def wrap_func(*args_, **kwargs_):
            func.__controller__ = True
            cls_name = "tinyms_web_controller_%s" % Utils.md5(pattern)
            handler = type(cls_name, (IWebHandler,), {})
            handler.cache_path = cache_key
            handler.cache_time = cache_time
            handler.cache_storage = cache_storage

            def unauth(*args, **kwargs):
                this = args[0]
                this.redirect(this.get_login_url())

            def generic_func(*args, **kwargs):
                this = args[0]
                if auth:
                    account_id = this.get_current_user()
                    if not account_id:
                        unauth(*args, **kwargs)
                    if len(points) > 0:
                        points_ = this.get_current_account_points()
                        same = points & points_
                        if len(same) > 0:
                            func(*args, **kwargs)
                        else:
                            unauth(*args, **kwargs)
                    else:
                        func(*args, **kwargs)
                else:
                    func(*args, **kwargs)

            if "post" == method:
                handler.post = generic_func
            else:
                handler.get = generic_func

            if pattern not in ObjectPool.route.keys():
                ObjectPool.route[pattern] = (pattern, handler)

        return wrap_func

    return handle_func

class WebSocketController(WebSocketHandler):

    def data_received(self, chunk):
        pass

    def open(self):
        WebSocketPoolFactory.__sockets__.append(self)

    def on_message(self, message):
        WebSocketPoolFactory.execute(message)

    def on_close(self):
        WebSocketPoolFactory.__sockets__.remove(self)

def upload(handler, thumbnail_size="", store_level="Private", callback_func=None):
    results = list()
    # 预先创建要存放的目录
    """
    文件上传
    :param request: tornado request handler
    :param thumbnail_size: 缩略图尺寸，格式200x200，即宽x高
    :param store_level: 存放文档级别 `Public` or `Private`
    :param callback_func: pass file inf, dict type
    """
    store_path = "static/upload/%s"
    if store_level == "Private":
        store_path = "upload/%s"
    store_path = store_path % Utils.format_year_month(Utils.current_datetime(), "/")
    Utils.mkdirs(store_path)

    if handler.request.files:
        for fn in handler.request.files.keys():
            f = handler.request.files[fn][0]
            raw_name = f["filename"]
            ext_name = os.path.splitext(raw_name)[-1]
            mime_type = f["content_type"]
            uniq_name = Utils.uniq_index()
            dist_name = "%s%s" % (uniq_name, ext_name)
            tnb_name = "thumb_%s%s" % (uniq_name, ext_name)
            tf = open("temp/%s" % Utils.uniq_index(), mode="w+b")
            # tf = tempfile.NamedTemporaryFile()
            try:
                tf.write(f["body"])
                tf.seek(0)
                tf.close()
                b = True
            except Exception as e:
                b = False
                print(e)
            if b:
                # 拷贝到目标目录
                file_inf = dict()
                file_inf["author"] = handler.get_current_user()
                file_inf["raw_name"] = raw_name
                file_inf["ext_name"] = ext_name
                file_inf["mime_type"] = mime_type
                file_inf["size"] = Utils.get_file_size(tf.name)
                file_inf["rel_store_path"] = ""
                file_inf["rel_store_thumbnail_path"] = ""
                file_inf["store_level"] = store_level
                final_normal_save_path = "%s/%s/%s" % (os.getcwd(), store_path, dist_name)
                final_thumbnail_save_path = "%s/%s/%s" % (os.getcwd(), store_path, tnb_name)
                shutil.move(tf.name, final_normal_save_path)
                rel_thumbnail_store_path = "%s/%s" % (store_path, tnb_name)
                if thumbnail_size and mime_type.startswith("image/"):
                    Utils.create_thumbnail(final_normal_save_path, final_thumbnail_save_path, size=thumbnail_size)
                    file_inf["rel_store_thumbnail_path"] = rel_thumbnail_store_path
                rel_store_path = "%s/%s" % (store_path, dist_name)
                file_inf["rel_store_path"] = rel_store_path
                file_inf["__extra_data__"] = callback_func(file_inf, handler)
                r = DataResult()
                r.success = True
                r.message = "Success"
                r.data = file_inf
                results.append(r.dict())
            else:
                r = DataResult()
                r.success = False
                r.message = "Failure"
                r.data = raw_name
                results.append(r.dict())
    else:
        r = DataResult()
        r.success = False
        r.message = "NoFilesUpload"
        r.data = ""
        results.append(r.dict())

    return results


from collections import Counter
import pyspark.sql.functions as F

def task_5(df):
    task_5_res_df = df.groupBy("channel_title").count().orderBy('count', ascending=False). \
        limit(10).join(df, 'channel_title', 'left').groupBy('channel_title').agg(
        F.first('count').alias("total_trending_days"),
        F.collect_list('video_id').alias("video_id_list"),
        F.collect_list('title').alias("video_title_list")
    )
    query_result = task_5_res_df.collect()

    json_result = {
        "channels": [
            {
                "channel_name": row.channel_title,
                "total_trending_days": row.total_trending_days,
                "videos_days": [
                    {
                        "video_id": video_id,
                        "video_title": row.video_title_list[row.video_id_list.index(video_id)],
                        "trending_days": count
                    }

                    for video_id, count in Counter(row.video_id_list).items()
                ]
            }
            for row in query_result
        ]
    }
    return json_result


import os

from fastapi import FastAPI
from routes.api import router as api_router
from starlette.middleware.cors import CORSMiddleware

def create_app():
    app = FastAPI(
        title="Simple FastAPI CRUD",
        description="Simple CRUD implementation using FastAPI",
        version="0.0.1",
        root_path=os.getenv("API_PATH"),
        docs_url="/",
        redoc_url="/docs",
    )

    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    app.include_router(api_router)

    return app


import os
import sys
import redis
from cloudfiles import CloudFiles

TASK_KEY = sys.argv[1]

try:
    r = redis.Redis(host=os.environ["REDIS_SERVER"], db=int(os.environ["REDIS_DB"]))
    if r.exists(TASK_KEY) and r.get(TASK_KEY).decode() == "DONE":
        sys.exit(0)
    else:
        sys.exit(1)
except:
    cf = CloudFiles(os.environ["SCRATCH_PATH"])
    if cf.exists(f"done/{TASK_KEY}.txt"):
        sys.exit(0)
    else:
        sys.exit(1)


def count_differences(values):
    one_diff = 0
    three_diff = 0

    for i in range(1, len(values)):
        difference = values[i] - values[i-1]
        print(difference, values[i-1], values[i])
        if difference == 1:
            one_diff += 1
        if difference == 3:
            three_diff += 1

    return one_diff, three_diff

if __name__ == "__main__":
    with open("input.txt", 'r') as input_file:
        values = [0]
        values.extend([int(value) for value in input_file.read().split("\n")])

    values.sort()
    values.append(values[-1] + 3)

    one, three = count_differences(values)

    print(one * three)


# -*- coding: utf-8 -*-
# @Time    : 2019/10/1 0001 15:03
# @Author  : Erichym
# @Email   : 951523291@qq.com
# @File    : 27.py
# @Software: PyCharm
"""
note: change the element value in original list
"""
class Solution:
    def removeElement(self, nums: list, val: int) -> int:
        k=0
        cnt=nums.count(val)
        for i in range(len(nums)):
            if nums[i]!=val:
                nums[k]=nums[i]
                k+=1
        print(nums)
        return len(nums)-cnt

if __name__=="__main__":
    nums= [0,1,2,2,3,0,4,2]
    so=Solution()
    a=so.removeElement(nums,2)
    print(a)


import megengine as mge
import megengine.functional as F
import megengine.module as M

class MaxPool2d(M.MaxPool2d):
    """only used for this network, not universal"""
    def __init__(self, ceil_mode=False, **kwargs):
        super(MaxPool2d, self).__init__(**kwargs)
        self.ceil_mode = ceil_mode

    def forward(self, inp):
        if self.ceil_mode:
            if inp.shape[2] %  2 != 0:
                inp = F.nn.pad(inp, ((0, 0), (0, 0), (0, 1), (0, 1)), constant_value=-float('inf'))
                print(inp.shape)
        return super().forward(inp)


from typing import List, Optional, Tuple
from tmsproviderapisdk.tms_extended_model import TmsExtendedModel

class TmsDevice(TmsExtendedModel):
    _path_url = "/devices/"

    def __init__(self, unique_id: str, account: int, device_id: int = None, ipaddr: str = None, mac: str = None,
                 remote_custom_field: str = None, comment: str = None, last_online: str = None, last_fw_ver: str = None,
                 first_online: str = None, use_nat: bool = False, operation_system: str = None, udpxy_addr: str = None,
                 device_type: int = None, provider: int = None):

        self.unique_id = unique_id
        self.account = account
        self.id = device_id
        self.ipaddr = ipaddr
        self.mac = mac
        self.remote_custom_field = remote_custom_field
        self.comment = comment
        self.last_online = last_online
        self.last_fw_ver = last_fw_ver
        self.first_online = first_online
        self.use_nat = use_nat
        self.operation_system = operation_system
        self.udpxy_addr = udpxy_addr
        self.device_type = device_type
        self.provider = provider

    @staticmethod
    def _dict_to_object(device_dict: dict) -> object:
        device = TmsDevice(
            unique_id=device_dict["unique_id"],
            device_id=device_dict["id"],
            ipaddr=device_dict["ipaddr"],
            mac=device_dict["mac"],
            remote_custom_field=device_dict["remote_custom_field"],
            comment=device_dict["comment"],
            last_online=device_dict["last_online"],
            last_fw_ver=device_dict["last_fw_ver"],
            first_online=device_dict["first_online"],
            use_nat=device_dict["use_nat"],
            operation_system=device_dict["operation_system"],
            udpxy_addr=device_dict["udpxy_addr"],
            device_type=device_dict["device_type"],
            provider=device_dict["provider"],
            account=device_dict["account"]
        )

        return device

    @classmethod
    def get_list(cls, account: int = None, device_type: int = None, limit: int = 50, provider: int = None,
                 quick_search: str = "", remote_custom_field: str = None, sort: str = "", start: int = 0,
                 unique_id: str = "") -> Optional[Tuple[List[object], int]]:

        devices = super().get_list(start=start, limit=limit, account=account, device_type=device_type,
                                   provider=provider, quick_search=quick_search,
                                   remote_custom_field=remote_custom_field, sort=sort, unique_id=unique_id)

        return devices

    def __str__(self):
        return """id:{}, ipaddr:{}, mac:{}, unique_id:{}, remote_custom_field: {}, comment: {}, last_online: {}, \
        last_fw_ver: {}, first_online: {}, use_nat: {}, operation_system: {}, \
        udpxy_addr: {}, device_type: {}, provider: {}, account: {}""".format(
            self.id,
            self.ipaddr,
            self.mac,
            self.unique_id,
            self.remote_custom_field,
            self.comment,
            self.last_online,
            self.last_fw_ver,
            self.first_online,
            self.use_nat,
            self.operation_system,
            self.udpxy_addr,
            self.device_type,
            self.provider,
            self.account
        )


import os
import sys
import time
import pickle
import shutil
import logging
import argparse
import traceback
import numpy as np
import pandas as pd
import random
import cv2
import scipy.io as sio
from skimage import io

# from utils import *
from multiprocessing import Pool, Process
from multiprocessing_logging import install_mp_handler
from datetime import datetime

import torch
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
import torch.backends.cudnn as cudnn

from emonet.emonet.models import EmoNet

## facial expression
class EmonetDataset(Dataset):
    def __init__(self, root_path, transform_image=None, suffix=None):
        self.root_path = root_path
        self.transform_image = transform_image
        self.suffix = suffix

    def __len__(self):
        files = os.listdir(self.root_path)
        length = sum(['.jpg' in x for x in files])
        return length if self.suffix is None else int(length / 2)

    def __getitem__(self, index):
        image_files = os.listdir(self.root_path)
        image_files = [x for x in image_files if x.split('.')[1] == 'jpg']
        if self.suffix is not None:
            image_files = list(filter(lambda x: self.suffix in x, image_files))
        image_files = sorted(image_files, key=lambda x: int(x.split('_')[0].replace('frame', '')))
        image = io.imread(os.path.join(self.root_path, image_files[index]))
        if self.transform_image is not None:
            image = self.transform_image(image)
        if self.suffix is None:
            return dict(id=image_files[index].split('.')[0], image=image)
        else:
            return dict(id=image_files[index].split('.')[0].replace(f'_{self.suffix}', ''), image=image)

def facial_expression_analysis(align_dir, compo_dir, score_dir, fer_model=['ferplus', 'emonet'], dev='cpu'):
    '''
    :param align_dir: directory containing aligned face images.
    :param compo_dir: directory containing composite face images.
    :param fer_model: facial expression analysis model, 'ferplus' only classifies emotions, 'emonet' classifies emotions and scores valence-arousal.
    :return: no return
    '''
    ## aligned face images
    align_files = os.listdir(align_dir)
    align_files = [x for x in align_files if '.jpg' in x]
    align_files = sorted(align_files, key=lambda x: int(x.split('_')[0].replace('frame', '')))
    align_ids = [x.split('.')[0] for x in align_files]
    if len(align_ids) == 0:
        print(f"[{datetime.today().strftime('%Y-%m-%d %H:%M:%S')}] Video {align_dir.split('/')[-1]} has no target faces, will not analyze emotions")
        logging.info(f"Video {align_dir.split('/')[-1]} has no target faces, will not analyze emotions")
        return

    # 'ferplus': cpu, multiprocess
    if fer_model == 'ferplus':

        # save as a dict with 4 keys
        scores = dict.fromkeys(['align_emos', 'left_emos', 'right_emos', 'FAsy_emos'])

        emotions = ['Neutral', 'Happy', 'Surprise', 'Sad',
                    'Anger', 'Disgust', 'Fear', 'Contempt']
        # dnn emotion model
        ferplus_model = './emotion_ferplus/model.onnx'
        ferplus_net = cv2.dnn.readNetFromONNX(ferplus_model)

        for idx, dir_suf in enumerate(zip([align_dir, compo_dir, compo_dir], ['', '_left', '_right'])):
            image_files = [os.path.join(dir_suf[0], f'{k}{dir_suf[1]}.jpg') for k in align_ids]
            # print(image_files[:3])
            processed_faces = []
            for _, image_file in enumerate(image_files):
                image = cv2.imread(image_file)
                # color to gray, resize & reshape
                gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                resized_face = cv2.resize(gray, (64, 64))
                processed_faces.append(resized_face[np.newaxis,np.newaxis,:,:])
            processed_faces = np.concatenate(processed_faces, axis=0) # np.ndarray (N, 1, 64, 64)
            # score emotions
            ferplus_net.setInput(processed_faces)
            Output = ferplus_net.forward() # (N, 8)
            # softmax
            exp = np.exp(Output)
            prob = exp / np.sum(exp, axis=1)[:, None] # (N, 8)
            score_df = pd.DataFrame(prob, columns=emotions) # (N, 8)
            score_df.insert(0, 'Frame_ID', align_ids) # (N, 9)
            # sort dataframes
            sort_index = score_df['Frame_ID'].apply(lambda x: x.split('_')[0].replace('frame','')).astype(int).argsort()
            score_df = score_df.iloc[sort_index].reset_index(drop=True)
            # record
            if idx == 0:
                scores['align_emos'] = score_df
            elif idx == 1:
                scores['left_emos'] = score_df
            else:
                scores['right_emos'] = score_df
        score_df = scores['left_emos'].copy()
        score_df.iloc[:, 1:] = scores['left_emos'].iloc[:, 1:] - scores['right_emos'].iloc[:, 1:]
        scores['FAsy_emos'] = score_df

    # 'emonet': gpu, serialization, batch
    elif fer_model == 'emonet':

        # save as a dict with 8 keys
        scores = dict.fromkeys(['align_emos', 'left_emos', 'right_emos', 'FAsy_emos',
                               'align_vai', 'left_vai', 'right_vai', 'FAsy_vai'])

        emotions_emonet = {0: 'neutral', 1: 'happy', 2: 'sad', 3: 'surprise',
                           4: 'fear', 5: 'disgust', 6: 'anger', 7: 'contempt'}
        n_expression = 8
        batch_size = 64 if dev != 'cpu' else len(align_ids)
        n_workers = min(8, os.cpu_count()//len(device)+1) if dev != 'cpu' else 0 # num_workers set to 4 or 8 fastest
        if dev != 'cpu':
            cudnn.benchmark = True

        # load and transform images
        trans_emonet = transforms.ToTensor()

        # load emonet
        emonet_state_path = './emonet/pretrained/emonet_8.pth'
        emonet_state_dict = torch.load(str(emonet_state_path), map_location='cpu')
        emonet_state_dict = {k.replace('module.', ''): v for k, v in emonet_state_dict.items()}
        emonet = EmoNet(n_expression=n_expression).to(dev)
        emonet.load_state_dict(emonet_state_dict, strict=False)
        emonet.eval()

        for idx, dir_suf in enumerate(zip([align_dir, compo_dir, compo_dir], [None, '_left', '_right'])):

            emonet_dataset = EmonetDataset(root_path=dir_suf[0], transform_image=trans_emonet, suffix=dir_suf[1])
            emonet_dataloader = DataLoader(emonet_dataset, batch_size=batch_size, shuffle=False, num_workers=n_workers)
            # forward emonet
            emotions, vais = [], []
            for _, data in enumerate(emonet_dataloader):

                images = data['image'].to(dev)
                with torch.no_grad():
                    out = emonet(images)
                emos = torch.softmax(out['expression'], axis=1).cpu().detach().numpy()  # (bs, 8)
                emos_df = pd.DataFrame(emos, columns=list(emotions_emonet.keys()))
                emos_df.insert(0, 'Frame_ID', data['id'])
                emotions.append(emos_df)
                vals = out['valence'].cpu().detach().numpy()  # (bs,)
                arous = out['arousal'].cpu().detach().numpy()  # (bs,)
                intens = np.sqrt(vals ** 2 + arous ** 2)
                vai_df = pd.DataFrame({'Frame_ID': data['id'], 'Valence': vals, 'Arousal': arous, 'Intensity': intens})
                vais.append(vai_df)
            if dev != 'cpu':
                torch.cuda.empty_cache()
            emotions_df = pd.concat(emotions)
            vais_df = pd.concat(vais)
            # sort dataframes
            sort_index = emotions_df['Frame_ID'].apply(lambda x: x.split('_')[0].replace('frame','')).astype(int).argsort()
            emotions_df = emotions_df.iloc[sort_index].reset_index(drop=True)
            sort_index = vais_df['Frame_ID'].apply(lambda x: x.split('_')[0].replace('frame','')).astype(int).argsort()
            vai_df = vais_df.iloc[sort_index].reset_index(drop=True)
            # record
            if idx == 0:
                scores['align_emos'] = emotions_df
                scores['align_vai'] = vais_df
            elif idx == 1:
                scores['left_emos'] = emotions_df
                scores['left_vai'] = vais_df
            else:
                scores['right_emos'] = emotions_df
                scores['right_vai'] = vais_df
        score_df = scores['left_emos'].copy()
        score_df.iloc[:, 1:] = scores['left_emos'].iloc[:, 1:] - scores['right_emos'].iloc[:, 1:]
        scores['FAsy_emos'] = score_df
        score_df = scores['left_vai'].copy()
        score_df.iloc[:, 1:] = scores['left_vai'].iloc[:, 1:] - scores['right_vai'].iloc[:, 1:]
        scores['FAsy_vai'] = score_df

    # save scores
    with open(os.path.join(score_dir, f'scores_{fer_model}.pkl'), 'wb') as f:
        pickle.dump(scores, f)
    print(f"[{datetime.today().strftime('%Y-%m-%d %H:%M:%S')}] Facial expression scored video {align_dir.split('/')[-1]}")
    logging.info(f"Facial expression scored video {align_dir.split('/')[-1]} (PID: {os.getpid()})")

def multi_facial_expression_analysis(video_ids_chunk, dev):
    for idx, vid_id in enumerate(video_ids_chunk):
        align_dir = os.path.join(align_root, vid_id)
        compo_dir = os.path.join(compo_root, vid_id)
        score_dir = os.path.join(score_root, vid_id)
        try:
            facial_expression_analysis(align_dir, compo_dir, score_dir, fer_model, dev)
        except Exception as e:
            logging.error(e)

if __name__ == '__main__':

    ## parse arguments
    parser = argparse.ArgumentParser()
    # directories
    parser.add_argument('--video-root', type=str, help='Specify the directory containing the videos.')
    parser.add_argument('--align-root', type=str, help='Specify the directory containing the aligned face images.')
    parser.add_argument('--compo-root', type=str, help='Specify the directory containing the composite face images.')
    parser.add_argument('--score-root', type=str, help='Specify the directory for saving information and scores.')
    # global setting
    parser.add_argument('--gpus', type=str, help='Specify the gpu indices for EmoNet.')
    # facial expression analysis
    parser.add_argument('--fer-model', type=str, choices=['ferplus', 'emonet'], help="Specify the model for emotion analysis. Available model: 'ferplus' and 'emonet'.")

    args = parser.parse_args()

    if args.gpus is not None:
        device = args.gpus.split(',') # a list of gpus
        device_name = f"gpu{args.gpus.replace(',','')}"
    else:
        device = 'cpu'
        device_name = 'cpu'
    video_root = args.video_root
    align_root = args.align_root
    compo_root = args.compo_root
    score_root = args.score_root
    fer_model = args.fer_model
    if fer_model == 'ferplus':
        cv2.setNumThreads(1)

    if not os.path.exists('logs'):
        os.makedirs('logs')
    log_file = f"logs/{align_root.split('/')[1].split('_')[0]}_emotion_{fer_model}_{device_name}.log"
    open(log_file, 'w').close()
    logging.basicConfig(filename=log_file,
                        level=logging.INFO,
                        format='[%(asctime)s] [%(levelname)s] %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S',
                        filemode='a+')
    install_mp_handler()
    logging.info(f'Devices: {device}, Emotion model: {fer_model}')
    print(f'Devices: {device}, Emotion model: {fer_model}')

    # check device and model
    if fer_model == 'ferplus':
        assert device == 'cpu', "Ferplus should run with cpu!"

    video_ids = os.listdir(video_root)
    video_ids = sorted([x.split('.')[0] for x in video_ids])

    ## 4. facial expression
    fer_model = args.fer_model
    if device == 'cpu':
        inp_args = []
        for idx, vid_id in enumerate(video_ids):
            align_dir = os.path.join(align_root, vid_id)
            compo_dir = os.path.join(compo_root, vid_id)
            score_dir = os.path.join(score_root, vid_id)
            # if os.path.exists(os.path.join(score_dir, f'scores_{fer_model}.pkl')):
            #     continue
            inp_args.append((align_dir, compo_dir, score_dir, fer_model, device))
        if len(inp_args) > 0:
            try:
                with Pool(os.cpu_count()) as pool:
                    pool.starmap(facial_expression_analysis, inp_args)
                    pool.close()
                    pool.join()
            except Exception as e:
                print(traceback.format_exc())
                logging.error(e)
    else:
        if len(device) == 1:
            list_video_ids = [video_ids]
        else:
            chunk_size = len(video_ids) // len(device) + 1
            list_video_ids = [video_ids[i:i+chunk_size] for i in range(0, len(video_ids), chunk_size)]
        procs = []
        for idx, video_ids_chunk in enumerate(list_video_ids):
            dev = f'cuda:{device[idx]}'
            print(f'{dev} will process {len(video_ids_chunk)} videos.')
            p = Process(target=multi_facial_expression_analysis, args=(video_ids_chunk, dev))
            procs.append(p)
            p.start()

        for p in procs:
            p.join()

    print(f"[{datetime.today().strftime('%Y-%m-%d %H:%M:%S')}] ********** Finish step 4: analyze facial expression **********")
    logging.info('********** Finish step 4: analyze facial expression **********')


"""
给定一个无序的数组，找出数组在排序之后，相邻元素之间最大的差值。

如果数组元素个数小于 2，则返回 0。

示例 1:

输入: [3,6,9,1]
输出: 3
解释: 排序后的数组是 [1,3,6,9], 其中相邻元素 (3,6) 和 (6,9) 之间都存在最大差值 3。
示例 2:

输入: [10]
输出: 0
解释: 数组元素个数小于 2，因此返回 0。
"""

class Solution(object):
    def maximumGap(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums)
        if n < 2:
            return 0
        nums.sort()
        ans = nums[1] - nums[0]
        for i in range(2, n):
            if nums[i] - nums[i - 1] > ans:
                ans = nums[i] - nums[i - 1]
        return ans