import os
import pickle
import requests

# Karaoke Mugen repository to scrape from
kara_list_url = "https://kara.moe/api/karas/"
media_download_url = "https://kara.moe/downloads/medias/{mediafile}"
kara_bundle_url = "https://kara.moe/api/karas/{kid}/raw"

# Language to scrape
language = "jpn"
# Sub filetype to scrape for
sub_file = ".ass"

# Audio bitrate & sample rate
bitrate = 24000
rate = 12000

# Location to save
save_location = "./data/ass/"

# ffmpeg command
ffmpeg = "ffmpeg -i {input} -ac 1 -ab {bitrate} -ar {rate} -y -vn {output}.mp3"

# Open the karaoke list
kara_list = requests.get(kara_list_url)
# We only care about the content, so ignore everything else
kara_list = kara_list.json()['content']

# Filter to only the language
kara_list = filter(lambda kara: len(kara['langs']) == 1 and kara['langs'][0]['name'] == language, kara_list)

# Filter to the filetype
kara_list = list(filter(lambda kara: str(kara['subfile']).endswith(sub_file), kara_list))

# Get the mediafile and the kid
mediafiles = list(map(lambda kara: kara['mediafile'], kara_list))
kids = list(map(lambda kara: kara['kid'], kara_list))

# For progress
size = len(mediafiles)
i = 1

# to download in reverse
# mediafiles.reverse()
# kids.reverse()
# otherwise zip
everything = zip(mediafiles, kids)

# Save the processed list
with open(save_location + "../ass.pickle", 'wb') as f:
    # store the data as binary data stream
    pickle.dump(everything, f)

# Start processing everything
for (mediafile, kid) in everything:
    if os.path.exists(save_location + kid + ".mp3"):
        # Skip if the file is already downloaded
        i = i + 1
        continue
    print("Downloading", i, "of", size, "...", mediafile)
    # Get metadata for lyrics
    metadata = requests.get(kara_bundle_url.format(kid=kid))
    # Get the lyrics
    lyrics = metadata.json()['lyrics']['data']

    # Save the lyrics to the appropriate file
    with open(save_location + kid + sub_file, 'wb') as f:
        f.write(bytes(lyrics, 'utf-8'))

    # Save the video
    video_name = requests.utils.quote(mediafile, safe='~()*!.\'')
    video = requests.get(media_download_url.format(mediafile=video_name))
    with open(save_location + video_name, 'wb') as f:
        f.write(video.content)

    # re-encode
    os.system(ffmpeg.format(input=save_location+video_name, output=save_location+kid, rate=rate, bitrate=bitrate))
    os.remove(save_location+video_name)

    # update progress
    i = i + 1

print("done!")


## Program to print the day based on date ##

import datetime

n = int(input('Enter no of dates:'))
li = []
status = False

if n in list(range(1,101)):
	for i in range(n):
		a = str(input('Enter date:'))
		li.append(a)

	day_dic = {0:'Monday',1:'Tuesday',2:'Wednesday',3:'Thursday',4:'Friday',5:'Saturday',6:'Sunday'}
	for i in li:
		dob = i.split(' ')
		dob = datetime.datetime.strptime('{}-{}-{}'.format(dob[0],dob[1],dob[2]),'%d-%m-%Y')
		print(day_dic.get(dob.weekday()))


# -*- coding: utf-8 -*-
'''
the implementation for pooling with numpy
author: zhang guanghua
date: 2018-0813
'''

import numpy as np

class Relu(object):
    def __init__(self, shape):
        self.input_shape = shape
        self.err = np.zeros(self.input_shape)
        self.output_shape = self.input_shape

    def forward(self, input):
        self.input = input
        return np.maximum(self.input, 0)

    def backward(self, err):
        self.err = err
        self.err[self.input<0]=0
        return self.err

if __name__ == '__main__':
    img = np.array([[1, 2, 3, 4, 5, 6], [-6, -5, -4, -3, -2, -1]])
    relu = Relu([2,6])
    output = relu.forward(img)
    print(output)
    err1 = output.copy() + 0.1 - output
    print(relu.backward(err1))


import socket
import time

# 创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 绑定端口
tcp_server_socket.bind(("", 6789))

# 变为监听套接字
tcp_server_socket.listen(128)

tcp_server_socket.setblocking(False)  # 设置套接字为非堵塞方式
client_socket_list = list()

while True:
    # time.sleep(1)
    try:
        new_socket, new_addr = tcp_server_socket.accept()
    except Exception as ret:
        print("---没有新的客户端到来---")
    else:
        print("---只要没有产生异常，那么也就意味者，来了一个新客户---")
        new_socket.setblocking(False)  # 设置套接字为非堵塞方式
        client_socket_list.append(new_socket)

    for client_socket in client_socket_list:
        try:
            recv_data = client_socket.recv(1024).decode("utf-8")
        except Exception as ret:
            # print(ret)
            print("---这个客户端没有发送过来数据---")
        else:
            if recv_data:
                print("---客户端发送过来了数据---", recv_data)
            else:
                # 对方调用了close 导致了 recv 返回
                client_socket_list.remove(client_socket)
                client_socket.close()

17_web服务器/05_单进程单进程非堵塞实现http服务器.py

05_单进程单进程非堵塞实现http服务器.py


# -*- coding: utf-8 -*-
import numpy as np
import argparse
from multiprocessing import Pool
import sys
import glob
import copy

parser = argparse.ArgumentParser(description='Sort the spectra.')
parser.add_argument('bins', type=str, help='File defining the wawelenght bins')
parser.add_argument('subBins', help='File defining the subBins distribution.')
parser.add_argument('cpuNumber', type=int, help='Number of CPUs to be used.')
parser.add_argument('--stromgen', type=bool, default=False, help='Whether to use stromgen segments.')
parser.add_argument('--suffix', type=str, help='Suffix of the segment files.')
args = parser.parse_args()
if args.stromgen and not args.suffix:
    parser.error('--suffix can only be used with --stromgen')

table = []
depthList = range(1, len(glob.glob('*.segment')) + 1)
if args.stromgen:
    depthList = range(1, len(glob.glob('*.segment_{}'.format(args.suffix))) + 1)

depthLength = len(str(len(depthList)))

binData = np.loadtxt(args.bins, ndmin=2)

# get minimum and maximum wawelenghts from the first .segment file
if args.stromgen:
    minMax = np.loadtxt(str(depthList[0]).zfill(depthLength) + '.segment_{}'.format(args.suffix))
else:
    minMax = np.loadtxt(str(depthList[0]).zfill(depthLength) + '.segment')

print(depthList)

def bining(depthList):
    wawelenghts = [array[0] for array in minMax]
    minimum = np.min(wawelenghts)
    maximum = np.max(wawelenghts)
    print('Minimum: ' + str(minimum))
    print('Maximum: ' + str(maximum))
    if (np.min(binData) < minimum) or (np.max(binData) > maximum):
        print('Bins intervals exceede the available wawelenghts. '
              'Please check your bins and make sure they are within '
              'the following limits')
        print('Minimum: ' + str(minimum) + '\n')
        print('Maximum: ' + str(maximum) + '\n')
        sys.exit('Check your bins')
        # check if the bins are within range of wawelenghts
    p = Pool(args.cpuNumber)
    p.map(reducing, depthList)

def reducing(currentFile):
    counter = currentFile
    if args.stromgen:
        currentFile = str(currentFile).zfill(depthLength) + '.segment_{}'.format(args.suffix)
    else:
        currentFile = str(currentFile).zfill(depthLength) + '.segment'
    print('Reducing: ' + str(currentFile))
    data = np.loadtxt(currentFile)  # load the current file to memory
    start_at = 0
    global binData
    for singleBin in binData:  # for each bin
        test = True
        tempList = []
        encounteredBinYet = False
        if start_at < 0:
            start_at = 0
        i = int(start_at)
        while i < len(data):  # for each data[i] in a segmentFile
            outsideOfTheBin = True
            if (data[i][0] + data[i][-1] >= singleBin[0]) and (data[i][0] <= singleBin[1]):  # check if the wawelength of the current data[i] is within the bin, append it
                if test:
                    test = False
                if (data[i][0] + data[i][-1] >= singleBin[0]) and (data[i][0] < singleBin[0]):
                    tempList.append(np.array([singleBin[0], data[i][1], data[i + 1][0] - singleBin[0]]))
                else:
                    tempList.append(np.array(data[i]))
                outsideOfTheBin = False
                encounteredBinYet = True
                if (np.array_equal(data[i], data[-1])):
                    # if we are on the last line, we must also sort the array
                    # otherwise it will go unsorted
                    tempList[-1][-1] = singleBin[1] - tempList[-1][0]
                    sub_bins(args.subBins, singleBin, tempList, counter)
            elif (outsideOfTheBin and encounteredBinYet):
                tempList[-1][-1] = np.float(singleBin[1] - tempList[-1][0])
                start_at = int(i) - 3
                sub_bins(args.subBins, singleBin, tempList, counter)
                break  # once we leave the part of the file containing current
                # bin, break and go to the next segment file
            i += 1

def sub_bins(subBinFile, singleBin, tempListList, counter):
    subBinsBorders = []
    subBinsBorders_stromgen = []
    subBins = np.loadtxt(subBinFile)
    subBinsLength = []
    bin_length = np.array(tempListList)[:, 2].sum()
    stretch_factor = 1
    if args.stromgen:
        stretch_factor = (singleBin[1] - singleBin[0]) / bin_length
    for line in subBins:
        subBinsLength.append((bin_length) * (line[1] - line[0]))
    # Create list of wavelengths which split the bin into subBins
    i = 0
    while i < len(subBinsLength):
        if i == 0:
            subBinsBorders_stromgen.append([singleBin[0], singleBin[0] + subBinsLength[0] * stretch_factor])
            subBinsBorders.append([singleBin[0], singleBin[0] + subBinsLength[0]])
        else:
            subBinsBorders_stromgen.append([subBinsBorders_stromgen[i - 1][-1], subBinsBorders_stromgen[i - 1][-1] + subBinsLength[i] * stretch_factor])
            subBinsBorders.append([subBinsBorders[i - 1][-1], subBinsBorders[i - 1][-1] + subBinsLength[i]])
        i += 1

    tempListList.sort(key=lambda x: x[1])  # sort by opacities

    deltaLambda = subBinsBorders[0][0]
    i, j, border_indexes, sub_bin_values = 0, 0, [0], []
    while i < len(tempListList) - 1:
        if deltaLambda + tempListList[i][2] > subBinsBorders[j][-1] and j + 1 < len(subBinsBorders):
            new_deltaLambda = deltaLambda + tempListList[i][2] - subBinsBorders[j][-1]
            tempListList[i][2] = ((subBinsBorders[j][-1] - deltaLambda))
            tempListList.insert(i + 1, [tempListList[i][2] + tempListList[i][0], tempListList[i][1], new_deltaLambda])
            border_indexes.append(i + 1)
            j += 1
            deltaLambda = subBinsBorders[j][0]
        else:
            deltaLambda += tempListList[i][2]
        i += 1
    border_indexes.append(len(tempListList))
    deltaLambda, beginning = 0, singleBin[0]
    i = 0
    j = 0
    tempListList = np.array(tempListList)
    for i in range(len(border_indexes) - 1):
        tempp = np.sum(np.multiply(tempListList[border_indexes[i]:border_indexes[i + 1], 1], tempListList[border_indexes[i]:border_indexes[i + 1], 2])) / subBinsLength[i]
        sub_bin_values.append([beginning, beginning + np.sum(tempListList[border_indexes[i]:border_indexes[i + 1], -1]), np.float(tempp)])
        beginning += np.sum(tempListList[border_indexes[i]:border_indexes[i + 1], -1])
    if args.stromgen:
        for i, item in enumerate(subBinsBorders_stromgen):
            sub_bin_values[i][0] = item[0]
            sub_bin_values[i][1] = item[1]
        if binData.shape[0] > 1:
            if np.array_equal(singleBin, binData[0]):
                sub_bin_values.insert(0, copy.copy(sub_bin_values[0]))
                sub_bin_values[0][0] -= 20
                sub_bin_values[0][1] = sub_bin_values[1][0]
            if np.array_equal(singleBin, binData[-1]):
                sub_bin_values.append(copy.copy(sub_bin_values[-1]))
                sub_bin_values[-1][0] = copy.copy(sub_bin_values[-1][1])
                sub_bin_values[-1][1] = 20 + sub_bin_values[-1][1]
        else:
            sub_bin_values.insert(0, copy.copy(sub_bin_values[0]))
            sub_bin_values[0][0] -= 20
            sub_bin_values[0][1] = sub_bin_values[1][0]
            sub_bin_values.append(copy.copy(sub_bin_values[-1]))
            sub_bin_values[-1][0] = copy.copy(sub_bin_values[-1][1])
            sub_bin_values[-1][1] = 20 + sub_bin_values[-1][1]

    sub_bin_values = np.array(sub_bin_values)
    if np.array_equal(singleBin, binData[0]):
        if args.stromgen:
            np.savetxt('{}.{}'.format(str(counter), args.suffix), sub_bin_values)
        else:
            np.savetxt(str(counter) + '.' + args.bins.split('b')[-1] + '_' + args.subBins.split('s')[-1], sub_bin_values)
    else:
        if args.stromgen:
            f = open('{}.{}'.format(str(counter), args.suffix), 'ab')
        else:
            f = open(str(counter) + '.' + args.bins.split('b')[-1] + '_' + args.subBins.split('s')[-1], 'ab')
        np.savetxt(f, sub_bin_values)
        f.close()

bining(depthList)


from venturial.models.mainpanel_sublayout_operators import *
from venturial.views.mainpanel.view import layout_controller

class mainPanel:
    """Class that defines the layout of the main panel"""

    def draw(self, layout, context):
        """Draws the layout of the main panel"""

        cs = context.scene
        var = "Edges" if cs.current_tool_text == "BlockMesh" else "Step Controls"
        getattr(self, "draw_tool_navigator")(cs.mainpanel_categories, var, layout)
        getattr(layout_controller(cs.mainpanel_categories), "output")(layout, context)

    def draw_tool_navigator(self, x, var, layout):
        """Horizontal tool navigator"""

        # ----------- Shabby code to replicate Blender's horizontal tabs. ----------------------------
        # ----------- This is a fix to bounding boxes issue #1 for operator selection. ---------------
        # ----------- It's Shabby but does the job. --------------------------------------------------
        row1 = layout.row(align=True)
        row1.ui_units_y = 0.001

        scale_active = 1.45
        scale_inactive = 1.75

        c1 = row1.split(factor=0.2, align=True)

        c1a = c1.row(align=True)
        c1b = c1.row(align=True)

        c1a.operator(VNT_OT_mainpanel_layout.bl_idname,
                    text="Explore",
                    depress= True if x == "Explore" else False,
                    emboss= True if x == "Explore" else False).mainpanel_options = "Explore"
        c1a.scale_y = scale_active if x == "Explore" else scale_inactive

        c2 = c1b.split(factor=0.25 if x == "Geometry" else 0.25, align=True)
        c2a = c2.row(align=True)
        c2a.operator(VNT_OT_mainpanel_layout.bl_idname,
                    text="Geometry",
                    depress= True if x == "Geometry" else False,
                    emboss= True if x == "Geometry" else False).mainpanel_options = "Geometry"
        c2a.scale_y = scale_active if x == "Geometry" else scale_inactive
        c2b = c2.row(align=True)

        c3 = c2b.split(factor=0.33 if x == var else 0.33, align=True)

        c3a = c3.row(align=True)
        c3a.operator(VNT_OT_mainpanel_layout.bl_idname,
                    text= var,
                    depress= True if x == var else False,
                    emboss= True if x == var else False).mainpanel_options = var

        c3a.scale_y = scale_active if x == var else scale_inactive
        c3b = c3.row(align=True)

        c4 = c3b.split(factor=0.505 if x == "Visualize" else 0.5, align=True)
        c4a = c4.row(align=True)
        c4a.operator(VNT_OT_mainpanel_layout.bl_idname,
                    text="Visualize",
                    depress= True if x == "Visualize" else False,
                    emboss= True if x == "Visualize" else False).mainpanel_options = "Visualize"
        c4a.scale_y = scale_active if x == "Visualize" else scale_inactive

        c4b = c4.row(align=True)
        c4b.operator(VNT_OT_mainpanel_layout.bl_idname,
                     text="Run",
                     depress= True if x == "Run" else False,
                     emboss= True if x == "Run" else False).mainpanel_options = "Run"
        c4b.scale_y = scale_active if x == "Run" else scale_inactive

        row2 = layout.row(align=True)
        row2.scale_y = 0.75
        row2.ui_units_y = 0.95

        if x == "Explore":
            f1 = 0.195
        elif x == "Geometry":
            f1 = 0.205
        else:
            f1 = 0.20

        c1 = row2.split(factor=f1, align=True)
        c1a = c1.row(align=True)
        c1a.box().label(text="") if x != "Explore" else c1a.row(align=True)

        c1b = c1.row(align=True)
        if x == "Geometry":
            f2 = 0.24
        elif x == var:
            f2 = 0.255
        else:
            f2 = 0.25

        c2 = c1b.split(factor=f2, align=True)
        c2a = c2.row(align=True)
        c2a.box().label(text="") if x != "Geometry" else c2a.row(align=True)

        c2b = c2.row(align=True)
        if x == var:
            f3 = 0.3175
        elif x == "Visualize":
            f3 = 0.337
        else:
            f3 = 0.33333

        c3 = c2b.split(factor=f3, align=True)
        c3a = c3.row(align=True)
        c3a.box().label(text="") if x != var else c3a.row(align=True)

        c3b = c3.row(align=True)
        if x == "Visualize":
            f4 = 0.4875
        elif x == "Run":
            f4 = 0.5075
        else:
            f4 = 0.5

        c4 = c3b.split(factor=f4, align=True)
        c4a = c4.row(align=True)
        c4a.box().label(text="") if x != "Visualize" else c4a.row(align=True)

        c4b = c4.row(align=True)
        c4b.box().label(text="") if x != "Run" else c4b.row(align=True)


import falcon

from kanaria.server.email_util import Email
from kanaria.core.agent import Agent

class IndexResource(object):

    def on_get(self, req, resp):
        import json
        msg = {
            "message": "kanaria works well."
        }
        resp.body = json.dumps(msg)

class KanariaResource(object):

    def on_post(self, req, resp):
        email = Email()
        letter = email.get_letter(req)
        agent = Agent()
        reply = agent.accept(letter)
        if reply:
            print("subject %s" % reply.subject)
            print("body    %s" % reply.body)
            print(reply.from_address)
            print(reply.to_addresses)
            email.send(subject=reply.subject,
                       text=reply.body,
                       from_address=reply.from_address,
                       to_addresses=reply.to_addresses)

app = falcon.API()
app.add_route("/", IndexResource())
app.add_route("/kanaria", KanariaResource())

if __name__ == "__main__":
    from wsgiref import simple_server
    httpd = simple_server.make_server("0.0.0.0", 8000, app)
    httpd.serve_forever()


import base64
import json
import os
import pickle
import sys

import pandas as pd
import xgboost as xgb
from dotenv import load_dotenv
from google.cloud import storage

load_dotenv()

PROJECT_ID = os.getenv("PROJECT_ID")
MODEL_BUCKET = os.getenv("MODEL_BUCKET")
DATA_BUCKET = os.getenv("DATA_BUCKET")

categorical = [
    "Gender",
    "Zip Code",
    "Offer",
    "Contract",
    "Phone Service",
    "Internet Service",
    "Paperless Billing",
    "Payment Method",
]
numerical = [
    "Age",
    "Number of Dependents",
    "Number of Referrals",
    "Tenure in Months",
    "Monthly Charge",
    "Total Charges",
    "Total Refunds",
    "Total Extra Data Charges",
    "Total Revenue",
]
target = "client_churned"

def download_files():
    """
    Download Files from GS Bucket"""
    storage_client = storage.Client()
    storage_client_data = storage.Client()

    model_bucket = storage_client.bucket(MODEL_BUCKET)
    data_bucket = storage_client_data.bucket(DATA_BUCKET)

    preprocessors_blob = model_bucket.blob("preprocessors.pkl")
    model_blob = model_bucket.blob("model.xgb")

    preprocessors_blob.download_to_filename("/tmp/preprocessors.pkl")
    model_blob.download_to_filename("/tmp/model.xgb")

    data_blob = data_bucket.blob("future.csv")
    data_blob.download_to_filename("/tmp/future.csv")

def upload_output():

    storage_client = storage.Client()
    bucket = storage_client.bucket(DATA_BUCKET)
    blob = bucket.blob("prediction.csv")

    blob.upload_from_filename("/tmp/prediction.csv")

def read_data():
    df = pd.read_csv("/tmp/future.csv")
    return df

def predict_binary(probs):
    return (probs >= 0.5).astype("bool")

def preprocess_data(df):
    with open("/tmp/preprocessors.pkl", "rb") as file:
        (dv, scaler) = pickle.load(file)

    df = df.copy()
    df.drop("Customer ID", axis=1, inplace=True)
    target_vec = df[target]
    df.drop(target, axis=1, inplace=True)
    df[categorical] = df[categorical].astype("str")
    df[numerical] = scaler.transform(df[numerical])
    df_dict = df.to_dict(orient="records")
    X = dv.transform(df_dict)
    return X

def predict(X):

    booster = xgb.Booster({"verbosity": 0})
    booster.load_model("/tmp/model.xgb")

    X_DMatrix = xgb.DMatrix(X)
    return predict_binary(booster.predict(X_DMatrix))

def endpoint(event, context):
    """
    Main function to be exported.
    Takes the event and outputs the prediction and sends it to the Pull stream"""
    # ride = base64.b64decode(event["data"]).decode("utf-8")
    # ride = json.loads(ride)
    if event["data"] != "debug":
        download_files()

    df = read_data()
    X = preprocess_data(df)
    preds = predict(X)
    df["churn_prediction"] = preds
    df.to_csv("/tmp/prediction.csv")
    if event["data"] != "debug":
        upload_output()


"""Utils for testing"""

from __future__ import annotations

import platform

def get_macos_version() -> tuple[int, int, int]:
    # returns tuple of ints containing OS version
    # e.g. 10.13.6 = (10, 13, 6)
    version = platform.mac_ver()[0].split(".")
    if len(version) == 2:
        (ver, major) = version
        minor = "0"
    elif len(version) == 3:
        (ver, major, minor) = version
    else:
        raise (
            ValueError(
                f"Could not parse version string: {platform.mac_ver()} {version}"
            )
        )
    return (int(ver), int(major), int(minor))


from flask import render_template, url_for, request, redirect
from src import app, db
from src.models import Article

@app.route('/')
@app.route('/home')
def index():
    return render_template('index.html')

# /about
@app.route('/about')
def about():
    return render_template('about.html')

# /posts
@app.route('/posts')
def posts():
    articles = Article.query.order_by(Article.day.desc()).all()
    return render_template('posts.html', articles=articles)

# /posts/id
@app.route('/posts/<int:id>')
def post_detail(id):
    article = Article.query.get(id)
    return render_template('post_detail.html', article=article)

# /posts/id/delete
@app.route('/posts/<int:id>/delete')
def post_delete(id):
    article = Article.query.get_or_404(id)
    try:
        db.session.delete(article)
        db.session.commit()
        return redirect('/posts')
    except:
        return "При удалении статьи произошла ошибка"

# /posts/id/update
@app.route('/posts/<int:id>/update', methods=['POST', 'GET'])
def post_update(id):
    article = Article.query.get(id)
    if request.method == "POST":
        # Reading data to DB from page, if we've received POST request
        article.title = request.form['title']
        article.intro = request.form['intro']
        article.text = request.form['text']
        try:
            db.session.commit()
            # Redirecting after updating page
            return redirect('/posts')
        except:
            return "При редактировании статьи произошла ошибка"
    else:
        return render_template('post_update.html', article=article)

# /create-article
@app.route('/create-article', methods=['POST', 'GET'])
def create_article():
    if request.method == "POST":
        # POST request handling
        title = request.form['title']
        intro = request.form['intro']
        text = request.form['text']

        article = Article(title=title, intro=intro, text=text)

        try:
            db.session.add(article)
            db.session.commit()
            # Redirecting after adding data to the Database
            return redirect('/posts')
        except:
            return "При добавлении статьи произошла ошибка"
    else:
        return render_template('create-article.html')


import io
import logging
import re
import sys
from collections import UserDict, UserList
from collections.abc import Mapping, Sequence

logger = logging.getLogger(__name__)

def deep_update(
    target: Mapping,
    other: Mapping,
    overwrite=True,
    list_insert_index: dict = None,
) -> None:
    if other.get("_do_not_overwrite", False):
        overwrite = False
    for k, v in other.items():
        if isinstance(v, Mapping):
            if k not in target:
                target[k] = dict(v)  # use a copy
            elif isinstance(target[k], Mapping):
                deep_update(target[k], v, overwrite=overwrite)
            else:
                raise ValueError(
                    f"trying to merge key {k} map {v} into non-map {target[k]}"
                )
        elif isinstance(v, Sequence) and not isinstance(v, str):
            if k not in target:
                target[k] = list(v)  # use a copy
            elif isinstance(target[k], Sequence) and not isinstance(target[k], str):
                if list_insert_index and k in list_insert_index:
                    idx = list_insert_index[k]
                    target[k][idx:idx] = v
                else:
                    for item in v:
                        target[k].append(item)
            else:
                raise ValueError(
                    f"trying to merge key {k} sequence {v}"
                    f" into non-sequence {target[k]}"
                )
        else:
            if overwrite or k not in target:
                # this key is special, should find better way
                if k != "_do_not_overwrite":
                    target[k] = v

class DictWrapper(UserDict):
    def __init__(self, dict):
        # do not copy the original dict as the normal UserDict does
        # but wrap the original so that updates go to the original
        self.data = dict

    def __getattr__(self, attr):
        if attr not in self.data:
            raise AttributeError(
                f"could not find attribute {attr} in {self}, {self.data}"
            )
        else:
            return wrap(self.data[attr])

    def __repr__(self):
        return f"DictWrapper({self.data})"

    def pprint_str(self, indent=""):
        output = io.StringIO()
        pprint_map(self, indent=indent, file=output)
        result = output.getvalue()
        output.close
        return result

    def deep_update_path(self, path: str, value):
        keys = path.split(".")
        data = self.data
        for key in keys[:-1]:
            key = key.replace("_dot_", ".")
            if key not in data:
                data[key] = {}
            data = data[key]
        final_key = keys[-1]
        final_key = final_key.replace("_dot_", ".")
        if final_key in data:
            if isinstance(data[final_key], Mapping):
                # Try to merge two Mappings
                if not isinstance(value, Mapping):
                    raise ValueError(
                        f"Can not assign non-dict {value} to"
                        f"dict {data[final_key]} for path {path}"
                    )
                deep_update(data[final_key], value)
            else:
                data[final_key] = value
        elif final_key == "[0]" and isinstance(data, Sequence):
            deep_update(data[0], value)
        else:
            data[final_key] = value

    # TODO: Will be removed in 2.0
    def _set_path(self, path: str, value):
        self.set_path(path, value)

    def set_path(self, path: str, value):
        keys = path.split(".")
        data = self.data
        for key in keys[:-1]:
            key = key.replace("_dot_", ".")
            if key not in data:
                data[key] = {}
            data = data[key]
        final_key = keys[-1]
        final_key = final_key.replace("_dot_", ".")
        if final_key in data:
            if isinstance(data[final_key], Mapping):
                # Try to merge two Mappings
                if not isinstance(value, Mapping):
                    raise ValueError(
                        f"Can not assign non-dict {value} to"
                        f"dict {data[final_key]} for path {path}"
                    )
                data[final_key].update(value)
            else:
                data[final_key] = value
        elif final_key == "[0]" and isinstance(data, Sequence):
            deep_update(data[0], value)
        else:
            data[final_key] = value

    # TODO: Will be removed in 2.0
    def _del_path(self, path: str):
        self.del_path(path)

    def del_path(self, path: str):
        keys = path.split(".")
        data = self.data
        for key in keys[:-1]:
            key = key.replace("_dot_", ".")
            if key not in data:
                logger.warning(f"non existent key {key} in del_path {path}")
                return
            data = data[key]
            while isinstance(data, Sequence):
                # get first and only item of list
                if len(data) == 1:
                    data = data[0]
                else:
                    logger.warning(f"list at {key} in del_path {path}")
                    return
        final_key = keys[-1]
        final_key = final_key.replace("_dot_", ".")
        if final_key in data:
            del data[final_key]
        else:
            logger.warning(f"non existent key {final_key} in del_path {path}")

    def get(self, path: str, default=None):
        return self.get_path(path, default)

    def set(self, path: str, val):
        return self.set_path(path, val)

    # TODO: Will be removed in 2.0
    def _get_path(self, path: str, default=None, mandatory=False):
        return self.get_path(path, default, mandatory)

    def get_path(self, path: str, default=None, mandatory=False):
        keys = path.split(".")
        data = self.data
        for key in keys:
            key = key.replace("_dot_", ".")
            if key == "[0]":
                if isinstance(data, Sequence):
                    if len(data) > 0:
                        data = data[0]
                    elif mandatory:
                        raise ValueError(f"could not find mandatory field {path}")
                    else:
                        return default
                else:
                    raise ValueError(
                        f"trying to get [0] in path {path} of non-sequence {type(data)}"
                    )
            elif key not in data:
                if mandatory:
                    raise ValueError(f"could not find mandatory field {path}")
                return default
            else:
                data = data[key]
        return data

class ListWrapper(UserList):
    def __init__(self, seq) -> None:
        # do not copy the original list as the normal UserList does
        # but wrap the original so that updates go to the original
        self.data = seq

    def __getitem__(self, idx):
        # Wrap the returned value
        return wrap(self._seq[idx])

def wrap(obj):
    if isinstance(obj, str):
        return obj
    if isinstance(obj, Sequence) and not isinstance(obj, ListWrapper):
        return ListWrapper(obj)
    if isinstance(obj, Mapping) and not isinstance(obj, DictWrapper):
        return DictWrapper(obj)
    return obj

def pprint_map(map, indent="", file=None, use_quotes=False):
    if file is None:
        file = sys.stdout
    indent_step = "  "
    if isinstance(map, str):
        print(f"{indent}{map}", file=file)
        return
    elif isinstance(map, Sequence):
        for v in map:
            print(f"{indent}- {v}", file=file)
        return

    if map is None:
        logger.warning("No values present to print")
        return

    for key in sorted(map.keys()):
        val = map.get(key, None)
        if isinstance(val, Mapping):
            if len(val) == 0:
                print(f"{indent}{key}: " + "{}", file=file)
            else:
                print(f"{indent}{key}:", file=file)
                pprint_map(val, indent=indent + indent_step, file=file, use_quotes=use_quotes)
        elif isinstance(val, str):
            if use_quotes:
                print(f'{indent}{key}: "{val}"', file=file)
            else:
                print(f"{indent}{key}: {val}", file=file)
        elif isinstance(val, Sequence):
            print(f"{indent}{key}:", file=file)
            for v in val:
                print(f"{indent}- {v}", file=file)
        else:
            print(f"{indent}{key}: {val}", file=file)

def pprint_tuple(tup, prefix=None, delimiter=".", pattern=None):
    for item in sorted(tup, key=lambda x: x[0]):
        if prefix is None:
            print_filtered(f"{item[0]}={item[1]}", pattern)
        else:
            print_filtered(f"{prefix}{delimiter}{item[0]}={item[1]}", pattern)

def print_filtered(text, pattern):
    if pattern is not None:
        regex_pattern = re.compile(pattern, re.IGNORECASE)
        if regex_pattern.search(text):
            print(text)
    else:
        print(text)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@Project ：LeetCodePthonVersion
@File ：15. 三数之和.py
@Author ：HuntingGame
@Date ：2023-02-01 20:15
C'est la vie!!! enjoy ur day :D
'''
from typing import List

class Solution:
    """
    这是第一个解题的思路，就是正着遍历，但是这样不好，因为有一个数组的insert插入；
    那么第二个思路就是倒着遍历，就不用insert了。见下面的代码
    """
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        ans = []
        for i in range(len(nums)-2):
            if i==0 or nums[i-1] !=nums[i]:
                nexts = self.twoSum(nums,i + 1,-nums[i])
                for cur in nexts:
                    cur.insert(0,nums[i])
                    ans.append(cur)
        return ans
    def twoSum(self,nums,begin,target):
        L = begin
        R = len(nums) - 1
        ans = []
        while L < R:
            if nums[L] + nums[R] > target:
                R -=1
            elif nums[L] + nums[R] < target:
                L +=1
            else:
                if L == begin or nums[L-1]!=nums[L]:
                    ans.append([nums[L],nums[R]])
                L+=1
        return ans

class Solution:
    """
    这是第二个解题的思路，就是到着遍历。
    """
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        ans = []
        for i in range(len(nums)-1,1,-1):
            if i==len(nums)-1 or nums[i] !=nums[i+1]:
                nexts = self.twoSum(nums,i-1,-nums[i])
                for cur in nexts:
                    cur.append(nums[i])
                    ans.append(cur)
        return ans
    def twoSum(self,nums,end,target):
        L = 0
        R = end
        ans = []
        while L < R:
            if nums[L] + nums[R] > target:
                R -=1
            elif nums[L] + nums[R] < target:
                L +=1
            else:
                if L == 0 or nums[L-1]!=nums[L]:
                    ans.append([nums[L],nums[R]])
                L+=1
        return ans


class Solution:
    def fib(self, n: int) -> int:
        # a = [0]*31         # 我不明白为什么 这里写成 a = [0]*(n+1) 就会报错啊，错误类型是 IndexError: list assignment index out of range
        # a[0],a[1] = 0,1
        # for i in range(2,n+1):
        #     a[i] = a[i-1]+a[i-2]
        # return a[n]

        # 动态规划的 方法
        if n<2: return n
        a,b,c = 0,1,0     # 我们只需要维护两个斐波拉契数 就行了
        for i in range(1,n):
            c = a + b
            a,b = b,c
        return c

        # # 递归的方法
        # if n < 2: return n
        # return self.fib(n-1)+self.fib(n-2)


"""Dataset utilities."""

from typing import Union

import hashlib
import urllib.request

from tqdm import tqdm

def download_file(url: str, destination: str, progress_bar: bool = True, md5sum: Union[str, None] = None) -> None:
    """Download a file.

    Parameters
    ----------
    url: str
      File URL.
    destination: str
      Path to save the file.
    progress_bar: bool = True
      Show progress bar if True.
    md5sum: Union[str, None] = None
      md5sum hash of the file.

    Returns
    -------
    None
    """
    response = urllib.request.urlopen(url)
    file_size = int(response.info()["Content-Length"])

    def __show_progress(block_num: int, block_size: int, total_size: int) -> None:
        downloaded = block_num * block_size
        if total_size > 0:
            progress_bar.update(downloaded - progress_bar.n)

    with tqdm(total=file_size, unit='B', unit_scale=True, desc=destination, ncols=100) as progress_bar:
        urllib.request.urlretrieve(url, destination, __show_progress)

    if md5sum is not None:
        md5_hash = hashlib.md5()
        with open(destination, 'rb') as f:
            for chunk in iter(lambda: f.read(4096), b''):
                md5_hash.update(chunk)
        md5sum_test = md5_hash.hexdigest()
        if md5sum != md5sum_test:
            raise ValueError(f'md5sum mismatch. \nExpected: {md5sum}\nActual: {md5sum_test}')


import json

# with open('worldpopulation.json','r') as f:
with open(input('File Name: '),'r') as f:
    data = json.load(f)
    # print(data)

def c1():
    nCountry = 0
    for i in data:
        nCountry += 1
    print(nCountry)

def c2():
    pop = 0
    for i in range(len(data)):
        pop += int(data[i]['population'])
    print(pop)

def c3():
    cLetter = 0
    moreThanFive = 0
    for i in range(len(data)):
        ac = data[i]['country']
        if ac[0] == "C":
            cLetter += 1
        if len(ac) > 5:
            moreThanFive += 1
    print(cLetter)
    print(moreThanFive)

def c4():
    gt500M = 0        # 1)มากกว่า 500 ล้านคน
    mid = 0           # 2)ระหว่าง 250 ถึง 750 ล้านคน
    lt10M = 0         # 3)น้อยกว่า 10 ล้าน
    for i in range(len(data)):
        p = int(data[i]['population'])
        if p > 500000000:
            gt500M += 1
        if p > 250000000 and p < 750000000:
            mid += 1
        if p < 10000000:
            lt10M += 1
    print(gt500M)
    print(mid)
    print(lt10M)

def c5():
    topPop = sorted([float(i['World']) for i in data],reverse=True)
    top20 = sum([topPop[i] for i in range(20)])*100
    top50to150 = sum([topPop[i] for i in range(49,150)])*100
    print(f'{top20:.2f}')
    print(f'{top50to150:.2f}')

# def c5():
#     topPop = sorted([int(i['population']) for i in data],reverse=True)
#     sumTop20 = sum([topPop[i] for i in range(20)])
#     sum50to150 = sum([topPop[i] for i in range(49,150)])
#     sumPop = sum(topPop)
#     print(f"{(sumTop20/sumPop)*100:.2f}")
#     print(f"{(sum50to150/sumPop)*100:.2f}")
#     # print(topPop[49]-topPop[149])

x = int(input('Input : '))
if x == 1:
    c1()
elif x == 2:
    c2()
elif x == 3:
    c3()
elif x == 4:
    c4()
elif x == 5:
    c5()


import os
from flask import Flask, render_template, request
from flask import json
from flask import send_from_directory
from jinja2 import Template

app = Flask(__name__)

@app.route('/assets/<path:path>')
def send_assets(path):
    return send_from_directory('templates/assets', path)

@app.route("/login")
def login():
    return render_template("login.html")

@app.route("/")
def index():
    return render_template("index.html", content="dashboard.html")

@app.route("/zone-index")
def zone_index():
    data = []
    for dirname, dirnames, filenames in os.walk('zones'):
        for filename in filenames:
            with open('zones/' + filename) as data_file:
                data.append(json.load(data_file))

    return render_template("index.html", content="zone-index.html", zones=data)

@app.route("/zone-add")
def zone_add():
    data = None
    if request.args.get('domain'):
        with open('zones/' + request.args.get('domain') + '.json') as data_file:
            data = json.load(data_file)

    return render_template("index.html", content="zone-add.html", data=data)

@app.route("/zone-save", methods=["POST"])
def zone_save():
    if request.is_json:
        data = request.json
        with open('zones/' + data['domain'] + ".json", 'w') as outfile:
            json.dump(data, outfile)
        return "0"
    return "1"

@app.route("/zone-delete", methods=["POST"])
def zone_delete():
    if request.is_json:
        data = request.json
        os.remove('zones/' + data['domain'] + ".json")
        return "0"
    return "1"

if __name__ == "__main__":
    app.run(port=4998, host="0.0.0.0")


#WA

# nが巨大な时

class CMB():
    def __init__(self, mod=10**9+7,mx=5*10**5+1):#mx:入力制限
        N = mx
        g1 = [1, 1]  # 元テーブル
        g2 = [1, 1]  # 逆元テーブル
        inverse = [0, 1]  # 逆元テーブル计算用テーブル
        for i in range(2, N + 1):
            g1.append((g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod//i)) % mod)
            g2.append((g2[-1] * inverse[-1]) % mod)
        self.g1 = g1
        self.g2 = g2
        self.mod = mod

    def cmb(self, n, r):  # 素数で割って出力　の场合
        if (r < 0 or r > n):
            return 0
        r = min(r, n-r)
        return self.g1[n] * self.g2[r] * self.g2[n-r] % self.mod

def main():
    C=CMB()
    import sys
    N,M=list(map(int,sys.stdin.readline().split()))
    K=M-N
    r=0
    for l in range(1,K+1):
        r+=C.cmb()*C.cmb(N,l)*C.cmb(N,)
    print(r*C.cmb())

main()


from .. import config
from .. import utils
import wikipedia
import signal
import sys

def on_message(mqtt_client, message):
    text = message.payload.decode('utf-8')

    if text == 'search_wiki':
        utils.read_aloud(mqtt_client, 'podaj hasło, które chcesz wyszukać na wikipedii', config.WEB_TOPIC)
    elif text == config.TTS_FINISHED:
        mqtt_client.publish(config.GPIO_TOPIC, config.GPIO_LED_ON)
        mqtt_client.publish(config.STT_TOPIC, config.WEB_TOPIC)
    else:
        if wikipedia.suggest(text) is not None:
            text = wikipedia.suggest(text)

        try:
            summary = wikipedia.summary(text, sentences=2)
            utils.read_aloud(mqtt_client, f'jak podaje wikipedia, {summary}')
        except wikipedia.exceptions.DisambiguationError as e:
            summary = wikipedia.summary(e.options[0], sentences=2)
            utils.read_aloud(mqtt_client, f'jak podaje wikipedia, {summary}')
        except wikipedia.exceptions.PageError:
            utils.read_aloud(mqtt_client, 'przepraszam, nie mogę znaleźć informacji na ten temat')

def main():
    ip, port = utils.get_connection_args(sys.argv)
    wikipedia.set_lang('pl')
    utils.setup_mqtt('web', ip, port, on_message)
    signal.pause()

if __name__ == '__main__':
    main()


# Aula 18 - Listas (parte 2) / Variaveis Compostas / Matrizes

matriz = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for linha in range(len(matriz)):
    for coluna in range(len(matriz[linha])):
        print(' | ', end='')
        print(matriz[linha][coluna], end=' | ')
    print()

teste = list()
teste.append('Mario') # adiciona itens na ultima posição da lista
teste.append('47')
galera = list() # Criada outra variavel
galera.append(teste[:]) # atribui a variavel o valor da lista anterior.
                        # Usando o [:], cria-se uma copia da liste sem alterar a primeira
                        # sem o [:], quando alteramos a segunda lista, a primeira altera tambem.
teste[0] = 'adilma'
teste[1] = 48
galera.append(teste[:])
print(galera)

galera2 = [['MARIO', 47], ['ADILMA', 48], ['JOAO', 25]]

print(galera2[2][0])

for nome in range(len(galera2)): # Estrutura de repetição para correr linha e coluna
    print()
    for idade in range(len(galera2[nome])):
        print(galera2[nome][idade], end=' ')

for nome in galera2: # Estrutura de repetição para correr linha e coluna
    print(f'{nome[0].capitalize()} tem {nome[1]} de idade.') # Para correr a lista, usa

pessoas = list()                                                             # o contador mais o fatiamento
dado = list()
for c in range(0, 2):
    dado.append(str(input('Digite seu nome: ')))
    dado.append(int(input('Digite sua idade: ')))
    pessoas.append(dado[:])
    dado.clear()
#print(pessoas)
totmaior = totmenor = 0
for p in pessoas:
    if p[1] >= 21:
        print(f'{p[0]} tem {p[1]} anos, e é maior que 21 anos.')
        totmaior += 1
    else:
        print(f'{p[0]} tem {p[1]} anos, e é menor de 21 anos.')
        totmenor += 1
print(f'Tivemos {totmaior} pessoas maior de idade.'
      f'\nTivemos {totmenor} pessoas menor de idade.')


import pandas as pd
import simplejson as json

data_file = open("fiveyearsdata.json", "r")
data = json.load(data_file)
data = data

print("Data size:", len(data), "observations")
df = pd.DataFrame()
for i, d in enumerate(data[:]): # Going through data: observation by observation per each day
    try:
        df_1 = pd.DataFrame(d["data"]["weather"], index=[i])
        df_2 = pd.DataFrame(d["data"]["request"], index=[i])
        df_ = pd.concat([df_1, df_2], axis=1)
        for k, v in d["data"]["weather"][0]["hourly"][0].items():
            df_.loc[i, k] = v
        for k, v in d["data"]["weather"][0]["astronomy"][0].items():
            df_.loc[i, k] = v

        df = df_ if df.empty else df.append(df_)
    except:
        # There are some errors found in fetched data already.
        # Days missed as a result of an error should be imputed by some way
        print("Error at index:", i, "Error details:", d["data"])

df = df.drop('hourly', 1)
df = df.drop('astronomy', 1)

print("Fetched data sieze:", len(df), "observations")
print("Number of variables:", len(df.columns))

df.to_csv('weather_data.csv', sep='|') # sep here is to avoid "," in the query column


import sys
import numpy as np

class KNN:

    def __init__(self, train_x, train_y, k):
        self.k = k
        self.X_train = train_x
        self.y_train = train_y

    def predictions(self, test_x):
        predicted_labels = [self.train(x) for x in test_x]
        return np.array(predicted_labels)

    def train(self, x):
        # calcuate the distances
        distances = [np.sqrt(np.sum((x-x_train)**2)) for x_train in self.X_train]
        # Pick the first K entries from the sorted collection
        k_indices = np.argsort(distances)[: self.k]
        k_list = [self.y_train[i] for i in k_indices]
        mode_num = max(set(k_list), key = k_list.count)
        return int(mode_num)

class PERC:
    def __init__(self, train_x, train_y, lr):
        self.xTrain = train_x
        self.yTrain = train_y
        self.lr = lr

    def train(self, epochs):
        bias = np.full(240,1).reshape(240,1)
        self.xTrain = np.append(self.xTrain, bias, axis=1)
        w = np.zeros((3, len(self.xTrain[0])), dtype=float)
        dataSet = list(zip(self.xTrain, self.yTrain))
        np.random.shuffle(dataSet)
        for i in range(epochs):
           for x, y in dataSet:
               y_hat = np.argmax(np.dot(w, x))
               if y_hat != y:
                   w[int(y)] = w[int(y)] + (self.lr * x)
                   w[y_hat] = w[y_hat] - (self.lr * x)
           self.lr *= 0.5

        return w

    def predictions(self,w,normalized_test):
        predictions = []
        for normalized_row in zip(normalized_test):
           row = np.append(normalized_row, 1)
           pr_yh = np.argmax(np.dot(w, row))
           predictions.append(pr_yh)
        return predictions

class SVM:
    def __init__(self, train_x, train_y, lr, lamda):
        self.xTrain = train_x
        self.yTrain = train_y
        self.lr = lr
        self.lamda = lamda

    def train(self, epochs):
        bias = np.full(240,1).reshape(240,1)
        self.xTrain = np.append(self.xTrain, bias, axis=1)
        w = np.zeros((3, len(self.xTrain[0])), dtype=float)
        dataSet= list(zip(self.xTrain, self.yTrain))
        np.random.shuffle(dataSet)
        for e in range(epochs):
           for x, y in dataSet:
               yh = np.argmax(np.dot(w, x))
               if (y != yh):
                   if y != yh:
                       y = int(y)
                       yh = int(yh)
                       w[y] = (1 - self.lr * self.lamda) * w[y] + self.lr * x
                       w[yh] = (1 - self.lr * self.lamda) * w[yh] - self.lr * x
                   for i in range(w.shape[0]):
                       if i != y and i != yh:
                           w[i] = (1 - self.lr * self.lamda) * w[i]
           self.lr *= 0.5
        return w

    def predictions(self,w,normalized_test):
        predictions = []
        for normalized_row in zip(normalized_test):
           row = np.append(normalized_row, 1)
           pr_yh = np.argmax(np.dot(w, row))
           predictions.append(pr_yh)
        return predictions

#Passive Aggressive
class PA:
    def __init__(self, train_x, train_y):
        self.xTrain = train_x
        self.yTrain = train_y

    def train(self, epochs):
        bias = np.full(240,1).reshape(240,1)
        self.xTrain = np.append(self.xTrain, bias, axis=1)
        w = np.zeros((3, len(self.xTrain[0])), dtype=float)
        for ep in range(epochs):
            dataSet = list(zip(self.xTrain, self.yTrain))
            np.random.shuffle(dataSet)
            for x, y in dataSet:
                yh = np.argmax(np.dot(w, x))
                if y != yh:
                  y = int(y)
                  yh = int(yh)
                  loss = max(0, 1 - np.dot(w[y],x) + np.dot(w[yh], x))
                  tau = loss / (2 * (np.linalg.norm(x)**2))
                  w[y] = w[y] + tau * x
                  w[yh] = w[yh] - tau * x
        return w

    def predictions(self,w,normalized_test):
        predictions = []
        for normalized_row in zip(normalized_test):
           row = np.append(normalized_row, 1)
           pa_yh = np.argmax(np.dot(w, row))
           predictions.append(pa_yh)
        return predictions

def normalize(data_set):
    return (data_set - data_set.mean(0)) / data_set.std(0)

def main():
    train_x = np.loadtxt(sys.argv[1],delimiter = ',')
    train_y = np.loadtxt(sys.argv[2])
    test_x = np.loadtxt(sys.argv[3],delimiter = ',')
    f = open(sys.argv[4],"w")

    normalized_train = normalize(train_x)
    normalized_test = normalize(test_x)

    #knn
    knn = KNN(train_x, train_y, 5)
    knn_predictions = knn.predictions(test_x)

    #SVM
    svm = SVM(normalized_train,train_y,0.1,0.1)
    w = svm.train(50)
    svm_predictions = svm.predictions(w, normalized_test)

    #perceptron
    perc = PERC(normalized_train,train_y,0.1)
    w = perc.train(50)
    perc_predictions = perc.predictions(w, normalized_test)

    #PA
    pa = PA(normalized_train,train_y)
    w = pa.train(85)
    pa_predictions = pa.predictions(w, normalized_test)
    #print(metrics.accuracy_score(knn_predictions, train_y))
    for knn_yhat,perceptron_yhat, svm_yhat, pa_yhat, in zip(knn_predictions,perc_predictions, svm_predictions, pa_predictions):
        f.write(f"knn: {knn_yhat}, perceptron: {perceptron_yhat}, svm: {svm_yhat}, pa: {pa_yhat}\n")
    f.close()

if __name__ == '__main__':
    main()


from game_classes import Character, Weapon
import json, os

# This will save the players character data (Only to be used for one player it will overwrite all data in the JSON file.) light_damage, _heavy_damage, level, xp
def save_character(player):
    player_stats = {"Name": player.name, "Health": player.health, "Race": player.race, "Level": player.level, "Weapon": {"Light": player.weapon.light_damage, "Heavy": player.weapon.heavy_damage, "Level": player.weapon.level, "XP": player.weapon.xp}}
    with open("player_data.json", "w") as player_data:
        json.dump(player_stats, player_data)

# This will load the player character data from a JSON file.
def get_saved_character():
    choice = ''
    character = None
    if os.path.isfile("player_data.json"):
        with open("player_data.json", "r") as player_data:
            character = json.load(player_data)

        while choice != '1' and choice != '3':
            choice = input(f'Available saved charater: {character["Name"]}, Level {character["Level"]}.\nPress 1 to play a new game, Press 3 to load this character.')
    else:
        choice = '1'
        print('No saved character data found.\nStarting a new game.......')
    return choice, character

# This function will Load a players character and weapon from a dictionary of characters data.
def load_character(data):
    saved_weapon = Weapon(data['Weapon']['Light'], data['Weapon']['Heavy'], data['Weapon']['Level'], data['Weapon']['XP'])
    saved_player = Character(data['Name'], data['Health'], data['Race'], data['Level'], saved_weapon)
    return saved_player


no = input('Enter the Card Number : ')
no = str(no)
if no.isdecimal():
    li = []
    card_no_len = len(no)
    for i in no:
        li.append(int(i))
    last_element = li[-1]
    li.pop()
    for i in range(0, len(li),2):
        temp = str(li[i] * 2)
        while (len(temp) != 1):
            sumt = 0
            for j in temp:
                sumt += int(j)
            temp = str(sumt)
        li[i] = int(temp)
else:
    print('Wrong Input ! ')
first_total = sum(li)
if ((first_total+last_element)%10) == 0:
    print('Verified Successfuly ')
else:
    print('Unverified Card Number')


import tensorflow as tf
import numpy as np
import core as cr
from buffer import replay_buffer
from tensorboardX import SummaryWriter
from ou_noise import OU_noise
import gym

class SAC:
    def __init__(self):
        self.sess = tf.Session()
        self.state_size = 33
        self.output_size = 4
        self.tau = 0.995
        self.gamma = 0.99
        self.hidden = [400, 300]
        self.batch_size = 64
        self.pi_lr = 1e-3
        self.q_lr = 1e-3
        self.action_limit = 1.0
        self.memory = replay_buffer(1e5)
        self.target_noise = 0.2
        self.noise_clip = 0.1
        self.alpha = 1e-5
        self.num_worker = 20
        self.noise = OU_noise(self.output_size, self.num_worker)

        self.x_ph, self.a_ph, self.x2_ph, self.r_ph, self.d_ph = \
            cr.placeholders(self.state_size, self.output_size, self.state_size, None, None)

        with tf.variable_scope('main'):
            self.mu, self.pi, self.logp_pi, self.q1, self.q2, self.q1_pi, self.q2_pi, self.v = \
                cr.sac_mlp_actor_critic(
                    x=self.x_ph,
                    a=self.a_ph,
                    hidden=self.hidden,
                    activation=tf.nn.relu,
                    output_activation=tf.tanh,
                    output_size=self.output_size,
                    action_limit=self.action_limit
                )
        with tf.variable_scope('target'):
            _, _, _, _, _, _, _, self.v_targ = \
                cr.sac_mlp_actor_critic(
                    x=self.x2_ph,
                    a=self.a_ph,
                    hidden=self.hidden,
                    activation=tf.nn.relu,
                    output_activation=tf.tanh,
                    output_size=self.output_size,
                    action_limit=self.action_limit
                )

        self.pi_params = cr.get_vars('main/pi')
        self.value_params = cr.get_vars('main/q') + cr.get_vars('main/v')

        self.min_q_pi = tf.minimum(self.q1_pi, self.q2_pi)
        self.q_backup = tf.stop_gradient(self.r_ph + self.gamma * (1 - self.d_ph) * self.v_targ)
        self.v_backup = tf.stop_gradient(self.min_q_pi - self.alpha * self.logp_pi)

        self.pi_loss = tf.reduce_mean(self.alpha * self.logp_pi - self.q1_pi)
        self.q1_loss = 0.5 * tf.reduce_mean((self.q_backup - self.q1) ** 2)
        self.q2_loss = 0.5 * tf.reduce_mean((self.q_backup - self.q2) ** 2)
        self.v_loss  = 0.5 * tf.reduce_mean((self.v_backup - self.v) ** 2)
        self.value_loss = self.q1_loss + self.q2_loss + self.v_loss

        self.pi_optimizer = tf.train.AdamOptimizer(self.pi_lr)
        self.train_pi_op = self.pi_optimizer.minimize(self.pi_loss, var_list=self.pi_params)

        self.value_optimizer = tf.train.AdamOptimizer(self.q_lr)
        with tf.control_dependencies([self.train_pi_op]):
            self.train_value_op = self.value_optimizer.minimize(self.value_loss, var_list=self.value_params)

        with tf.control_dependencies([self.train_value_op]):
            self.target_update = tf.group([tf.assign(v_targ, self.tau*v_targ + (1-self.tau)*v_main)
                                  for v_main, v_targ in zip(cr.get_vars('main'), cr.get_vars('target'))])

        self.step_ops = [self.pi_loss, self.q1_loss, self.q2_loss, self.v_loss, self.q1, self.q2,
                         self.v,       self.logp_pi, self.train_pi_op, self.train_value_op, self.target_update]

        self.target_init = tf.group([tf.assign(v_targ, v_main)
                              for v_main, v_targ in zip(cr.get_vars('main'), cr.get_vars('target'))])

        self.sess.run(tf.global_variables_initializer())
        self.sess.run(self.target_init)

    def update(self):
        data = self.memory.get_sample(sample_size=self.batch_size)
        feed_dict = {
            self.x_ph : data['state'],
            self.x2_ph : data['next_state'],
            self.a_ph : data['action'],
            self.r_ph : data['reward'],
            self.d_ph : data['done']
        }

        self.sess.run(self.step_ops, feed_dict=feed_dict)

    def get_action(self, state, deterministic=False):
        act_op = self.mu if deterministic else self.pi
        return self.sess.run(act_op, feed_dict={self.x_ph: [state]})[0]

    def test(self):
        env = gym.make('Pendulum-v0')
        while True:
            state = env.reset()
            done = False
            while not done:
                env.render()
                action = self.get_action(state, 0)
                state, _, done,_ = env.step(action)

    def run(self):
        from mlagents.envs import UnityEnvironment

        writer = SummaryWriter('runs/sac')
        num_worker = self.num_worker
        state_size = self.state_size
        output_size = self.output_size
        ep = 0
        train_size = 5

        env = UnityEnvironment(file_name='env/training', worker_id=1)
        default_brain = env.brain_names[0]
        brain = env.brains[default_brain]
        initial_observation = env.reset()

        step = 0
        start_steps = 100000

        states = np.zeros([num_worker, state_size])
        for i in range(start_steps):
            actions = np.clip(np.random.randn(num_worker, output_size), -self.action_limit, self.action_limit)
            actions += self.noise.sample()
            env_info = env.step(actions)[default_brain]
            next_states = env_info.vector_observations
            rewards = env_info.rewards
            dones = env_info.local_done
            for s, ns, r, d, a in zip(states, next_states, rewards, dones, actions):
                self.memory.append(s, ns, r, d, a)
            states = next_states
            if dones[0]:
                self.noise.reset()
            if i % train_size == 0:
                if len(self.memory.memory) > self.batch_size:
                    self.update()
            print('data storing :', float(i / start_steps))

        while True:
            ep += 1
            states = np.zeros([num_worker, state_size])
            terminal = False
            score = 0
            while not terminal:
                step += 1
                '''
                if step > start_steps:
                    actions = [self.get_action(s) for s in states]
                    action_random = 'False'
                else:
                    actions = np.clip(np.random.randn(num_worker, output_size), -self.action_limit, self.action_limit)
                    action_random = 'True'
                '''
                actions = [self.get_action(s) for s in states]
                action_random = 'False'

                env_info = env.step(actions)[default_brain]

                next_states = env_info.vector_observations
                rewards = env_info.rewards
                dones = env_info.local_done

                terminal = dones[0]

                for s, ns, r, d, a in zip(states, next_states, rewards, dones, actions):
                    self.memory.append(s, ns, r, d, a)

                score += sum(rewards)

                states = next_states

                if len(self.memory.memory) > self.batch_size:
                    if step % train_size == 0:
                        self.update()

            if ep < 1000:
                print('step : ', step, '| start steps : ', start_steps, '| episode :', ep, '| score : ', score, '| memory size', len(self.memory.memory), '| action random : ', action_random)
                writer.add_scalar('data/reward', score, ep)
                writer.add_scalar('data/memory_size', len(self.memory.memory), ep)

if __name__ == '__main__':
    agent = SAC()
    agent.run()


from django.contrib.auth.models import User
from rest_framework import serializers

from books.models import Book, Comment, Genre, Author

class BookSerializer(serializers.ModelSerializer):
    author_name = serializers.CharField(source='author.full_name', read_only=True)
    book_genres = serializers.StringRelatedField(source='genres', many=True, read_only=True)

    class Meta:
        model = Book
        fields = (
            'id', 'title', 'thumbnail', 'published_year', 'author_name', 'author', 'description', 'genres',
            'book_genres')
        extra_kwargs = {
            'author': {'write_only': True},
            'genres': {'write_only': True},
        }

class CommentSerializer(serializers.ModelSerializer):
    username = serializers.CharField(source='user.username', read_only=True)

    class Meta:
        model = Comment
        fields = ('text', 'username', 'created_at')
        extra_kwargs = {
            'username': {'read_only': True},
            'created_at': {'read_only': True},
        }

class BookDetailsSerializer(BookSerializer):
    comments = CommentSerializer(many=True, read_only=True)

    class Meta:
        model = Book
        fields = (
            'id', 'title', 'thumbnail', 'published_year', 'author_name', 'author', 'description', 'genres',
            'book_genres', 'page_amount', 'comments')

class UserSerializer(serializers.ModelSerializer):
    bookmarks = BookSerializer(many=True, read_only=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'first_name', 'last_name', 'bookmarks')

class GenreSerializer(serializers.ModelSerializer):
    class Meta:
        model = Genre
        fields = ('id', 'title')

class AuthorSerializer(serializers.ModelSerializer):
    class Meta:
        model = Author
        fields = ('id', 'full_name')


import csv
with open('C:\\Users\\skyti\\python\\code1\\Mod 1 Code & Data Files\\albumlist.csv', 'r') as csvfile:
    reader = csv.DictReader(csvfile)

    print(type(reader))
    print(reader.fieldnames)

    # for row in reader:
    #     print(row)

    # rows = 101
    # for row in reader:
    #     rows -= 1
    #     if (rows > 0):
    #         print(row)
    #     else:
    #         break

    albums = []
    for row in reader:
        albums.append(row)

    print("number albums: ", len(albums))

    albums_1974 = [row for row in albums if row["Year"] == '1974']
    print("number of album in 1974: ", len(albums_1974))

    for album in albums_1974:
        print(album["Album"], "by", album["Artist"] )

    print([row for row in albums if album["Year"] == "1974"][:10])

    rock_albums = [row for row in albums if(row["Genre"] == "Rock"
                    and ("Pop Rock" in row["Subgenre"] or "Fusion" in row["Subgenre"]))
                  ]

    for album in rock_albums:
        print("Album name: ", album["Album"], "Artist name: ", album["Artist"], "Genre: ", album["Genre"], "Subgenre: ", album["Subgenre"])


import os
import time
import argparse
import numpy as np
import saverloader
from fire import Fire
from nets.segnet import Segnet
import utils.misc
import utils.improc
import utils.vox
import random
import nuscenesdataset
import torch
import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy('file_system')
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from tensorboardX import SummaryWriter
import torch.nn.functional as F

import matplotlib
matplotlib.use('Agg')
from nuscenesdataset import get_nusc_maps, fetch_nusc_map2, add_ego2
import matplotlib.pyplot as plt
import imageio
import io

random.seed(125)
np.random.seed(125)

# the scene centroid is defined w.r.t. a reference camera
# which is usually random
scene_centroid_x = 0.0
scene_centroid_y = 1.0
scene_centroid_z = 0.0

scene_centroid_py = np.array([scene_centroid_x,
                              scene_centroid_y,
                              scene_centroid_z]).reshape([1, 3])
scene_centroid = torch.from_numpy(scene_centroid_py).float()

XMIN, XMAX = -50, 50
ZMIN, ZMAX = -50, 50
YMIN, YMAX = -5, 5
bounds = (XMIN, XMAX, YMIN, YMAX, ZMIN, ZMAX)

Z, Y, X = 200, 8, 200
def requires_grad(parameters, flag=True):
    for p in parameters:
        p.requires_grad = flag

def rgba2rgb( rgba, background=(255,255,255) ):
    row, col, ch = rgba.shape

    if ch == 3:
        return rgba

    assert ch == 4, 'RGBA image has 4 channels.'

    rgb = np.zeros( (row, col, 3), dtype='float32' )
    r, g, b, a = rgba[:,:,0], rgba[:,:,1], rgba[:,:,2], rgba[:,:,3]

    a = np.asarray( a, dtype='float32' ) / 255.0

    R, G, B = background

    rgb[:,:,0] = r * a + (1.0 - a) * R
    rgb[:,:,1] = g * a + (1.0 - a) * G
    rgb[:,:,2] = b * a + (1.0 - a) * B

    return np.asarray( rgb, dtype='uint8' )

def run_model(loader, index, model, d, img_dir, device='cuda:0', sw=None):
    imgs_all, rots_all, trans_all, intrins_all, pts0_all, extra0_all, pts_all, extra_all, lrtlist_velo_all, vislist_all, tidlist_all, scorelist_all, seg_bev_g_all, valid_bev_g_all, center_bev_g_all, offset_bev_g_all, radar_data_all, egopose_all = d

    T = imgs_all.shape[1]

    nusc_maps = get_nusc_maps(loader.dataset.data_root)
    scene2map = {}
    for rec in loader.dataset.nusc.scene:
        log = loader.dataset.nusc.get('log', rec['log_token'])
        scene2map[rec['name']] = log['location']
    dx = loader.dataset.dx[:2]
    bx = loader.dataset.bx[:2]

    for t in range(T):
        # eliminate the time dimension
        imgs = imgs_all[:,t]
        rots = rots_all[:,t]
        trans = trans_all[:,t]
        intrins = intrins_all[:,t]
        pts0 = pts0_all[:,t]
        extra0 = extra0_all[:,t]
        pts = pts_all[:,t]
        extra = extra_all[:,t]
        lrtlist_velo = lrtlist_velo_all[:,t]
        vislist = vislist_all[:,t]
        tidlist = tidlist_all[:,t]
        scorelist = scorelist_all[:,t]
        seg_bev_g = seg_bev_g_all[:,t]
        valid_bev_g = valid_bev_g_all[:,t]
        center_bev_g = center_bev_g_all[:,t]
        offset_bev_g = offset_bev_g_all[:,t]
        radar_data = radar_data_all[:,t]
        egopose = egopose_all[:,t]

        origin_T_velo0t = egopose.to(device) # B,T,4,4
        lrtlist_velo = lrtlist_velo.to(device)
        scorelist = scorelist.to(device)

        rgb_camXs = imgs.float().to(device)
        rgb_camXs = rgb_camXs - 0.5 # go to -0.5, 0.5

        seg_bev_g = seg_bev_g.to(device)
        valid_bev_g = valid_bev_g.to(device)
        center_bev_g = center_bev_g.to(device)
        offset_bev_g = offset_bev_g.to(device)

        xyz_velo0 = pts.to(device).permute(0, 2, 1)
        rad_data = radar_data.to(device).permute(0, 2, 1) # B, R, 19
        xyz_rad = rad_data[:,:,:3]
        meta_rad = rad_data[:,:,3:]

        B, S, C, H, W = rgb_camXs.shape
        B, V, D = xyz_velo0.shape

        __p = lambda x: utils.basic.pack_seqdim(x, B)
        __u = lambda x: utils.basic.unpack_seqdim(x, B)

        mag = torch.norm(xyz_velo0, dim=2)
        xyz_velo0 = xyz_velo0[:,mag[0]>1]
        xyz_velo0_bak = xyz_velo0.clone()

        intrins_ = __p(intrins)
        pix_T_cams_ = utils.geom.merge_intrinsics(*utils.geom.split_intrinsics(intrins_)).to(device)
        pix_T_cams = __u(pix_T_cams_)

        velo_T_cams = utils.geom.merge_rtlist(rots, trans).to(device)
        cams_T_velo = __u(utils.geom.safe_inverse(__p(velo_T_cams)))

        cam0_T_camXs = utils.geom.get_camM_T_camXs(velo_T_cams, ind=0)
        camXs_T_cam0 = __u(utils.geom.safe_inverse(__p(cam0_T_camXs)))
        cam0_T_camXs_ = __p(cam0_T_camXs)
        camXs_T_cam0_ = __p(camXs_T_cam0)

        xyz_cam0 = utils.geom.apply_4x4(cams_T_velo[:,0], xyz_velo0)
        rad_xyz_cam0 = utils.geom.apply_4x4(cams_T_velo[:,0], xyz_rad)

        lrtlist_cam0 = utils.geom.apply_4x4_to_lrtlist(cams_T_velo[:,0], lrtlist_velo)

        vox_util = utils.vox.Vox_util(
            Z, Y, X,
            scene_centroid=scene_centroid.to(device),
            bounds=bounds,
            assert_cube=False)

        V = xyz_velo0.shape[1]

        occ_mem0 = vox_util.voxelize_xyz(xyz_cam0, Z, Y, X, assert_cube=False)
        rad_occ_mem0 = vox_util.voxelize_xyz(rad_xyz_cam0, Z, Y, X, assert_cube=False)
        metarad_occ_mem0 = vox_util.voxelize_xyz_and_feats(rad_xyz_cam0, meta_rad, Z, Y, X, assert_cube=False)

        if not (model.module.use_radar or model.module.use_lidar):
            in_occ_mem0 = None
        elif model.module.use_lidar:
            assert(model.module.use_radar==False) # either lidar or radar, not both
            assert(model.module.use_metaradar==False) # either lidar or radar, not both
            in_occ_mem0 = occ_mem0
        elif model.module.use_radar and model.module.use_metaradar:
            in_occ_mem0 = metarad_occ_mem0
        elif model.module.use_radar:
            in_occ_mem0 = rad_occ_mem0
        elif model.module.use_metaradar:
            assert(False) # cannot use_metaradar without use_radar

        cam0_T_camXs = cam0_T_camXs

        lrtlist_cam0_g = lrtlist_cam0

        _, feat_bev_e, seg_bev_e, center_bev_e, offset_bev_e = model(
                rgb_camXs=rgb_camXs,
                pix_T_cams=pix_T_cams,
                cam0_T_camXs=cam0_T_camXs,
                vox_util=vox_util,
                rad_occ_mem0=in_occ_mem0)

        # visualize ground truth
        rec = loader.dataset.ixes[loader.dataset.indices[index][t]]
        car_from_current = np.eye(4)
        car_from_current[:3,:3] = rots[0,0].cpu().numpy()
        car_from_current[:3,3] = np.transpose(trans[0,0].numpy())

        poly_names, line_names, lmap = fetch_nusc_map2(rec, nusc_maps, loader.dataset.nusc, scene2map, car_from_current)

        plt.close('all')
        fig = plt.figure(figsize=(4,4), frameon=False)
        ax = fig.gca()
        ax.axis('off')
        ax = fig.add_axes([0, 0, 1, 1])
        ax.set_axis_off()
        ax.axis('off')
        fig.axes[0].get_xaxis().set_visible(False)
        fig.axes[0].get_yaxis().set_visible(False)
        fig.axes[1].get_xaxis().set_visible(False)
        fig.axes[1].get_yaxis().set_visible(False)
        plt.axis('off')
        line_names = ['road_divider', 'lane_divider']
        for name in poly_names:
            for la in lmap[name]:
                pts = (la - bx) / dx
                plt.fill(pts[:, 1], pts[:, 0], c=(1.00, 0.50, 0.31), alpha=0.2)
        for la in lmap['road_divider']:
            pts = (la - bx) / dx
            plt.plot(pts[:, 1], pts[:, 0], c=(0.0, 0.0, 1.0), alpha=0.5)
        for la in lmap['lane_divider']:
            pts = (la - bx) / dx
            plt.plot(pts[:, 1], pts[:, 0], c=(159./255., 0.0, 1.0), alpha=0.5)
        plt.xlim((200, 0))
        plt.ylim((0, 200))
        add_ego2(bx, dx)

        io_buf = io.BytesIO()
        fig.savefig(io_buf, format='raw')
        io_buf.seek(0)
        img_arr = np.reshape(np.frombuffer(io_buf.getvalue(), dtype=np.uint8), newshape=(int(fig.bbox.bounds[3]), int(fig.bbox.bounds[2]), -1))
        io_buf.close()

        img_arr = rgba2rgb(img_arr)
        img_arr = np.rot90(img_arr, 1)
        img_arr = np.flip(img_arr, axis=1)
        map_vis = torch.from_numpy(img_arr.astype(float) / 255.0 - 0.5) # H, W, 3
        map_vis = map_vis.unsqueeze(0).permute(0,3,1,2).float().to(rgb_camXs.device) # 1, 3, H, W

        _, _, mH, mW = map_vis.shape

        blue_img = torch.zeros_like(map_vis).to(map_vis.device)
        blue_img[:, [0,1]] = -0.5
        blue_img[:, 2] = 0.5
        seg_g_t = F.interpolate(seg_bev_g, (mH, mW))
        seg_g_t_onmap = map_vis * (1-seg_g_t) + blue_img * seg_g_t

        seg_e_t = torch.sigmoid(F.interpolate(seg_bev_e, (mH, mW)))
        seg_e_t_onmap = map_vis * (1-seg_e_t) + blue_img * seg_e_t

        # save to folder
        folder_name = os.path.join(img_dir, "sample_vis_%03d" % index)
        os.makedirs(folder_name, exist_ok=True)

        seg_g_t_vis = utils.improc.back2color(seg_g_t_onmap).cpu().numpy()[0].transpose(1,2,0)
        seg_g_t_vis_name = os.path.join(folder_name, "seg_gt_%03d.png" % t)
        imageio.imwrite(seg_g_t_vis_name, seg_g_t_vis)

        seg_e_t_vis = utils.improc.back2color(seg_e_t_onmap).cpu().numpy()[0].transpose(1,2,0)
        seg_e_t_vis_name = os.path.join(folder_name, "seg_et_%03d.png" % t)
        imageio.imwrite(seg_e_t_vis_name, seg_e_t_vis)

        n_cam = rgb_camXs.shape[1]
        for cam_id in range(n_cam):
            camX_t_vis = utils.improc.back2color(rgb_camXs[0, cam_id:cam_id+1]).cpu().numpy()[0].transpose(1,2,0)
            camX_t_vis_name = os.path.join(folder_name, "cam"+str(cam_id)+"_rgb_%03d.png" % t)
            imageio.imwrite(camX_t_vis_name, camX_t_vis)

        if model.module.use_radar:
            radar_t_vis = torch.sum(rad_occ_mem0[0], 2).clamp(0, 1) # (1, 200, 200)
            radar_t_vis = utils.improc.back2color(radar_t_vis.repeat(3,1,1)-0.5).cpu().numpy().transpose(1,2,0)
            radar_t_vis_name = os.path.join(folder_name, "radar_%03d.png" % t)
            imageio.imwrite(radar_t_vis_name, radar_t_vis)

            lidar_t_vis = torch.sum(occ_mem0[0], 2).clamp(0, 1) # (1, 200, 200)
            lidar_t_vis = utils.improc.back2color(lidar_t_vis.repeat(3,1,1)-0.5).cpu().numpy().transpose(1,2,0)
            lidar_t_vis_name = os.path.join(folder_name, "lidar_%03d.png" % t)
            imageio.imwrite(lidar_t_vis_name, lidar_t_vis)

def main(
        exp_name='debug',
        # eval
        max_iters=100000,
        log_freq=100,
        dset='trainval',
        batch_size=1, # batch size = 1 only
        timesteps=40, # a sequence is typically 40 frames (20s * 2fps)
        nworkers=12,
        # data/log/save/load directories
        data_dir='../nuscenes/',
        log_dir='logs_nuscenes_bevseg',
        img_dir='vis',
        ckpt_dir='checkpoints/',
        keep_latest=1,
        init_dir='',
        ignore_load=None,
        # data
        res_scale=2,
        ncams=6,
        nsweeps=3,
        # model
        encoder_type='res101',
        use_radar=False,
        use_radar_filters=False,
        use_lidar=False,
        use_metaradar=False,
        do_rgbcompress=True,
        # cuda
        device_ids=[0], # 1 device only for now
        ):

    B = batch_size
    assert(B % len(device_ids) == 0) # batch size must be divisible by number of gpus
    device = 'cuda:%d' % device_ids[0]

    # autogen a name
    model_name = "%d" % B
    model_name += "t%d" % timesteps
    model_name += "_%s" % exp_name
    import datetime
    model_date = datetime.datetime.now().strftime('%H:%M:%S')
    model_name = model_name + '_' + model_date
    print('model_name', model_name)

    # set up loggingg
    os.makedirs(img_dir, exist_ok=True)
    writer = SummaryWriter(os.path.join(log_dir, model_name), max_queue=10, flush_secs=60)

    # set up dataloaders
    final_dim = (int(224 * res_scale), int(400 * res_scale))
    print('resolution:', final_dim)

    resize_lim = [1.0,1.0]
    crop_offset = 0

    data_aug_conf = {
        'crop_offset': crop_offset,
        'resize_lim': resize_lim,
        'final_dim': final_dim,
        'H': 900, 'W': 1600,
        'cams': ['CAM_FRONT_LEFT', 'CAM_FRONT', 'CAM_FRONT_RIGHT',
                 'CAM_BACK_LEFT', 'CAM_BACK', 'CAM_BACK_RIGHT'],
        'ncams': ncams,
    }

    _, dataloader = nuscenesdataset.compile_data(
        dset,
        data_dir,
        data_aug_conf=data_aug_conf,
        centroid=scene_centroid_py,
        bounds=bounds,
        res_3d=(Z,Y,X),
        bsz=B,
        nworkers=nworkers,
        shuffle=False,
        use_radar_filters=use_radar_filters,
        seqlen=timesteps, # we do not load a temporal sequence here, but that can work with this dataloader
        nsweeps=nsweeps,
        do_shuffle_cams=False,
        get_tids=True,
    )
    dataloader.dataset.data_root = os.path.join(data_dir, dset)
    iterloader = iter(dataloader)

    # set up model & seg loss
    model = Segnet(Z, Y, X, use_radar=use_radar, use_lidar=use_lidar, use_metaradar=use_metaradar, do_rgbcompress=do_rgbcompress, encoder_type=encoder_type, rand_flip=False)
    model = model.to(device)
    model = torch.nn.DataParallel(model, device_ids=device_ids)
    parameters = list(model.parameters())
    total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
    print('total_params', total_params)

    # load checkpoint
    global_step = 0
    if init_dir:
        _ = saverloader.load(init_dir, model.module, ignore_load=ignore_load)
        global_step = 0
    requires_grad(parameters, False)
    model.eval()

    while global_step < max_iters:
        global_step += 1

        read_start_time = time.time()

        sw = utils.improc.Summ_writer(
            writer=writer,
            global_step=global_step,
            log_freq=log_freq,
            fps=2,
            scalar_freq=int(log_freq/2),
            just_gif=True)

        try:
            sample = next(iterloader)
        except:
            break

        read_time = time.time() - read_start_time
        iter_start_time = time.time()

        # run training iteration
        run_model(dataloader, global_step-1, model, sample, img_dir, device, sw)

        iter_time = time.time() - iter_start_time

        print('%s; step %06d/%d; rtime %.2f; itime %.2f' % (
            model_name, global_step, max_iters, read_time, iter_time))

    writer.close()

if __name__ == '__main__':
    Fire(main)


class Node:
    left = None
    right = None
    val = None
    char = None
    def __init__(self, val=0, char=None, left=None, right=None):
        self.left = left
        self.right = right
        if left!=None and right!=None:
            self.val = left.val + right.val
        else:
            self.val = val
        self.char = char

    @classmethod
    def leafNode(cls, v, c):
        return cls(val=v, char=c, left=None, right=None)

    @classmethod
    def irNode(cls, left, right):
        if left.val<right.val:
            return cls(left=right, right=left)
        else:
            return cls(left=left, right=right)

def traverse(n, code={}, stack=[]):
    print("code, stack: ", code, stack)
    print(n.char, n.val)
    if (n.left!=None):
        print(stack)
        print("left")
        stack.append('0')
        traverse(n.left)

    if (n.right!=None):
        print(stack)
        stack.append('1')
        print("right ")
        traverse(n.right)

    if (n.char!=None):
        code[n.char]=''.join(x for x in stack)
        stack.pop()

    return code

def frequencies(s):
    l = []
    for i in set(s):
        l.append((i, s.count(i)))

    l.sort(key=lambda tup:tup[1] )
    return l

def encode(freqs, s):
    print("freqs, s: ", freqs, s)
    print("type(freqs), type(s): ", type(freqs), type(s))
    if freqs==[] and s=='':
        return None
    elif freqs==[] and s!='':
        return None
    elif freqs!=[] and s=='':
        return None
    elif freqs!=[] and s==[]:
        return ''

    x = freqs.pop(0)
    n1 = Node.leafNode(x[1], x[0])
    x = freqs.pop(0)
    n2 = Node.leafNode(x[1], x[0])

    n = Node.irNode(n1, n2)

    for i in freqs:
        n = Node.irNode(n, Node.leafNode(i[1], i[0]))

    # print("code, stack: ", code, stack)
    code = traverse(n)
    # code = traverse(n, code={}, stack=[])
    print(code)
    output = ""

    # print(code)

    for i in s:
        output += code[i]

    return output

# takes [ [str, int] ], str (with "0" and "1"); returns: str
def decode(freqs, bits):
    return ""

a = "aaaabcc"

l = frequencies(a)
t = []
for i in l:
    t.append(list(i))
print(t)

encoded_string = (encode(frequencies(a), a))
print(len(encoded_string))

a = "vbbbvvbbbbv"
l = frequencies(a)
encoded_string = (encode(frequencies(a), a))
# print(encoded_string)

a = "ababababa"
l = frequencies(a)
encoded_string = (encode(frequencies(a), a))
print(encoded_string)

print(encode([("a", 1)], ""))
encode([('a', 1), ('b', 1)], "ab")
print(encode([], ""))

# import unittest

# fs = frequencies("aaaabcc")

# t = unittest.TestCase()

# t.assertEqual( encode( fs, "" ), "" )
# t.assertEqual( decode( fs, "" ), "" )

# t.assertEqual( encode( [], "" ), None );
# t.assertEqual( decode( [], "" ), None );
# t.assertEqual( encode( [('a', 1)], "" ), None );
# t.assertEqual( decode( [('a', 1)], "" ), None );


import sys

def findSums(k, n):
    def combinations(k, n, current_combo):
        if k == 0 and n == 0:
            return [current_combo]
        if k == 0 or n < 0:
            return []

        result = []
        for i in range(n + 1):
            new_combo = current_combo + [i]
            result += combinations(k - 1, n - i, new_combo)
        return result

    return combinations(k, n, [])

def main():
    argv = sys.argv
    argc = len(argv)

    if argc != 3:
        print("Usage: python3 addSums.py k n")

    for i in range(1, len(argv)):
        if int(argv[i]) < 0:
            print(f"Numbers must be positive")
            sys.exit(0)
        elif not argv[i].isdigit():
            print(f"{argv[i]} is not a number")
            sys.exit(0)

    k = int(argv[1])
    n = int(argv[2])

    arr = findSums(k, n)
    print(arr)

if __name__ == "__main__":
    main()


import logging
import os
import pickle

import numpy as np
from sklearn.model_selection import train_test_split

def get_training_files_for_params(full_dataset, prefix, training_files_full, training_files_partial):
    '''
    Function that returns the correct training files, for both cases we want to use the small dataset or
    the big dataset.
    :param full_dataset: boolean parameter which specifies if full dataset is used or small one
    :param prefix: the prefix of the file (might have been processed before, look into process_input)
    :param training_files_full: the dictionary with the full files and associated label for each tweet in the file
    :param training_files_partial: the dictionary with the partial files and associated label for each tweet in the file
    :return: the dictionary with the correct files
    '''
    if full_dataset:
        return {os.path.join('data', prefix + base_file): label for base_file, label in training_files_full.items()}

    return {os.path.join('data', prefix + base_file): label for base_file, label in training_files_partial.items()}

def shuffle_dataset(X, y):
    '''
    Function that shuffles a dataset.
    :param X: the instances of the dataset
    :param y: the labels of the dataset
    :return: the shuffled dataset, as a pair (X_suhffled, y_shuffled)
    '''
    np.random.seed(0)
    p = np.random.permutation(len(y))

    X = X[p]
    y = y[p]

    return X, y

def construct_dataset_from_files(filenames, split_size=0.1):
    '''
    Function that constructs a dataset from files. If split_size is None, then no split is performed and
    function returns a pair (X, y). If split_size is a float value, then the dataset is splitted into train
    set and test set, with test set size being equal to split_size * (full_dataset_size).
    :param filenames: the dictionary which has filenames as the keys and as values the labels for the tweets
        in each filen.
    :param split_size: the split size of the set
    :return: (X, y) is split_size is None, the full dataset, otherwise (X_train, y_train, X_test, y_test)
    '''
    logging.info("Loading data from files: " + str(filenames))
    print("Loading data from files: " + str(filenames))

    X = []
    y = []

    for filename, label in filenames.items():
        with open(filename, 'r', encoding='utf8') as f:
            for tweet in f:
                X.append(tweet)
                y.append(int(label))

    X = np.array(X)
    y = np.array(y)

    X, y = shuffle_dataset(X, y)

    if split_size is not None:
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=split_size, random_state=100)
        return X_train, y_train, X_test, y_test

    return X, y

def construct_test_from_file(filename):
    '''
    Function that constructs a dataset representing the final test set used for predictions from the specified file.
    :param filename: the filename where the test set is stored
    :return: (X_test, ids), an pair of arrays representing the test set and associated id for each tweet
    '''
    tweets = []
    ids = []

    with open(filename, 'r', encoding='utf8') as f:
        for line in f:
            id, tweet = line.split(',', 1)
            ids.append(id)
            tweets.append(tweet)

    return np.array(tweets), np.array(ids)

def create_submission(filename, predictions, ids):
    '''
    Function that creates a submission, given the predictions and associated ids.
    :param filename: the filename where we store the submission
    :param predictions: the predictions for the test set
    :param ids: the ids of the tweets
    :return: None
    '''
    with open(filename, 'w', encoding='utf8') as f:
        f.write('Id,Prediction\n')
        for id, prediction in zip(ids, predictions):
            f.write(str(id) + ',' + str(prediction) + '\n')

def construct_predictions_from_pickles(path_train, path_test):
    '''
    Function that creates the prediction of the neural networks models to be used for the second tier classifier
    '''
    with open(path_train, 'rb') as f:
        [X_train, y_train] = pickle.load(f)

    with open(path_test, 'rb') as f:
        X_test = pickle.load(f)

    return X_train, y_train, X_test