# encoding=utf-8
'''
Created on 2016年4月18日

@author: lenovo
'''

import re
import csv
import pickle
from multiprocessing import Pool
from collections import Counter
from functools import partial
from math import fabs, log
import jieba

jieba.initialize()

def calAccuracy(devs, outs):
    n = len(outs)
    equ = 0
    fake_true = 0
    fake_false = 0
    ftrue = open("logs/fake_true.txt", "w")
    ffalse = open("logs/fake_false.txt", "w")
    fclose = open("logs/close.txt", "w")
    # assert len(devs) == len(outs)
    for i in range(n):
        if int(devs[i][1]) == outs[i][0]:
            equ += 1
        elif devs[i][1] == '0':
            fake_true += 1
            print(devs[i][0], devs[i][1], outs[i][0], outs[i][1], file=ftrue)
        else:
            fake_false += 1
            print(devs[i][0], devs[i][1], outs[i][0], outs[i][1], file=ffalse)
        if fabs(outs[i][1]) < 0.2:
            print(devs[i][0], devs[i][1], outs[i][0], outs[i][1], file=fclose)
    print("fake true = %f(%d), fake false = %f(%d)" %
          (fake_true/n, fake_true, fake_false/n, fake_false))
    return equ/n

def joinmaps(lst):
    ret = {}
    cnt = 0
    for d in lst:
        for k, v in d.items():
            ret[k] = ret.get(k, 0) + v
            cnt += v
    return ret, cnt

class BayesSpam:
    def __init__(self):
        self.norm_file_len = 0
        self.spam_file_len = 0
        self.norm_dict = {}
        self.spam_dict = {}
        self.words_dict = {}
        self.stop_list = []
        self.words_list = []

        with open("stoplist.txt", encoding='utf-8') as sl:
            for line in sl:
                self.stop_list.append(line)

    def train(self, data):
        spams = []
        normals = []
        for line in data:
            if line[1] == '1':
                spams.append(line[0])
            else:
                normals.append(line[0])
        self.norm_file_len = len(normals)
        self.spam_file_len = len(spams)
        print("normal email number: ", self.norm_file_len)
        print("spam email number: ", self.spam_file_len)
        self.norm_dict = self._update_words_to_dict(normals)
        self.spam_dict = self._update_words_to_dict(spams)

    def predict(self, data):
        pool = Pool()
        return pool.map(self._test_line, data)

    def _update_words_to_dict(self, emails):
        pool = Pool()
        dicts = pool.map(self._load_email, emails)
        return joinmaps(dicts)[0]

    def _test_line(self, email):
        text = email[0]
        rule = re.compile(r"[^\u4e00-\u9fa5]")
        text = rule.sub("", text)
        wl = self._load_email(text)
        p = self._dict_to_prob(wl)
        if(p > 0):
            return 1, p
        else:
            return 0, p

    def _dict_to_prob(self, testDict):
        ret = log(self.spam_file_len/self.norm_file_len)
        # print(spamDict, normDict)
        # print(normFilelen, spamFilelen)
        default = 1/(1e30)
        # default = 0.00001
        for word in testDict:
            pw_s = self.spam_dict.get(word, default)/self.spam_file_len
            pw_n = self.norm_dict.get(word, default)/self.norm_file_len
            ps_w = pw_s/pw_n
            # ps_w = log(ps_w)*num
            ps_w = log(ps_w)
            ret += ps_w
        # wordProbList = sorted(wordProbList.items(),key=lambda d:d[1],reverse=True)[0:15]
        return ret

    def _load_email(self, normal):
        word_list = []
        word_dict = {}
        line = normal
        rule = re.compile(r"[^\u4e00-\u9fa5]")
        line = rule.sub("", line)
        res_list = list(jieba.cut(line))
        for i in res_list:
            if i not in self.stop_list and i.strip() != '' and i != None:
                word_dict[i] = word_dict.get(i, 0) + 1

        return word_dict

    def _add_to_dict(self, wordsList, wordsDict):
        for item in wordsList:
            wordsDict[item] = wordsDict.get(item, 0) + 1

def dump(rets):
    with open('answer.txt', 'w') as f:
        print("\n".join(map(str, map(lambda x: x[0], rets))), file=f)

def main():
    with open("../data/train.csv", 'r', encoding='utf-8') as f:
        train_raw = list(csv.reader(f))

    with open("../data/dev.csv", 'r', encoding='utf-8') as f:
        dev_raw = list(csv.reader(f))

    with open("../data/test.csv", 'r', encoding='utf-8') as f:
        test_raw = list(csv.reader(f))[1:]

    bs = BayesSpam()

    print("_______bayes________")
    bs.train(train_raw)

    result_train = bs.predict(train_raw)
    result_dev = bs.predict(dev_raw)
    result_test = bs.predict(test_raw)
    print("test completed")

    devAccuracy = calAccuracy(dev_raw, result_dev)
    trainAccuracy = calAccuracy(train_raw, result_train)
    print("train acc =", trainAccuracy)
    print("dev acc =", devAccuracy)
    print("_______bayes________")

    print("work completed")
    dump(result_test)

if __name__ == '__main__':
    main()


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

'''
Simple Raspberry Micro Dot Phat hack to display current played song on radio perfecto (rock French radio)

This code is under MIT licence
Code by DarKou (inspired by Brunus - https://github.com/Brunus-V/ircbot )
'''

import schedule
import sys
import time
import urllib
import json
from Queue import Queue
from microdotphat import write_string, set_decimal, set_rotate180, clear, show, scroll

print("""Radio Perfecto

Displays current played song from this fucking radio!

Press Ctrl+C to exit.
""")

titleBus = Queue()
titleBus.put('')

def readStream():
        currentTitle = titleBus.get()

        url = "https://rc1.nobexinc.com//nowplaying.ashx?stationid=70642"
        response = urllib.urlopen(url)
        data = json.loads(response.read())

        title = data['artist'] + ' - ' + data['songName']

        if (title != currentTitle):
                write_string(" # " + title, kerning=False)
                titleBus.put(title)
        else:
                titleBus.put(currentTitle)

schedule.every(5).seconds.do(readStream)

while True:
        schedule.run_pending()
        scroll()
        show()
        time.sleep(0.1)


import pygame

class Wall(pygame.sprite.Sprite):
    wall_speed = 0
    screen = None
    line_color = (255, 255, 0)
    rect = None

    def __init__(self, screen, speed, y_offset, width, thickness=10):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        self.image = pygame.Surface((width, thickness))
        self.image.fill(self.line_color)
        self.rect = self.image.get_rect()
        self.rect.y = screen.get_height()
        self.rect.x = y_offset
        self.wall_speed = speed

    def change_speed(self, speed):
        self.wall_speed = speed

    def update(self):
        self.rect.y -= self.wall_speed
        if self.rect.y < 0:
            self.kill()


import numpy as np
import os
from os import listdir
from os.path import isfile, join
from math import sqrt
from numpy import concatenate
from matplotlib import pyplot
from pandas import read_csv
from pandas import DataFrame
from pandas import concat
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.callbacks import EarlyStopping, ModelCheckpoint

rowCount = 0
nonzero = 0
scaler = 0
train_X = 0

def getLastFileInPath(filePath):
    files = [f for f in listdir(filePath) if isfile(join(filePath, f))]
    return  files[-1]

# convert series to supervised learning
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
	print("-------------------------")
	n_vars = 1 if type(data) is list else data.shape[1]
	df = DataFrame(data)
	cols, names = list(), list()
	# input sequence (t-n, ... t-1)
	for i in range(n_in, 0, -1):
		cols.append(df.shift(i))
		names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
	# forecast sequence (t, t+1, ... t+n)
	for i in range(0, n_out):
		cols.append(df.shift(-i))
		if i == 0:
			names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
		else:
			names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
	# put it all together
	agg = concat(cols, axis=1)
	agg.columns = names
    # drop rows with NaN values
	if dropnan:
		agg.dropna(inplace=True)

	return agg

def dataPreprocessing(masterDict,states):
    ##Formulation of the dataset
    global rowCount
    global scaler
    global nonzero
    for state,data in masterDict.items():
        if state in states:
            rowCount = len(data['New Indian'])
            nonzero = np.count_nonzero(data['New Indian'], axis=0)
            dataset = pd.DataFrame(data=data)
#            dataset['CumulativeCount'] = data['New Indian']
#            dataset['Recovered'] = data['Statewise recovered increment']
#            dataset['Deceased'] = data['Statewise deceased increment']
#            dataset['Humidity'] = data['Humidity']
#            dataset['Temperature'] = data['Temperature']
    #
    #Cleaning and normalization of dataset
    # ensure all data is float
    dataset = dataset.astype('float32')
    # normalize features
    scaler = MinMaxScaler(feature_range=(0, 1))
    scaled = scaler.fit_transform(dataset)
    # frame as supervised learning
    reframed = series_to_supervised(scaled, 1, 1)
    dropList = []
    i=len(data.items())+1
    while  i < 2*len(data.items()):
        dropList.append(i)
        i +=1
    reframed.drop(reframed.columns[dropList], axis=1, inplace=True)
    print(reframed.head())
    return reframed

def trainAndTest(masterDict,states):

    global scaler
    global train_X
    training_loss = 0
    validation_loss = 0
    data = dataPreprocessing(masterDict,states)
    # split into train and test sets
    values = data.values
    n_train_hours = int(rowCount*(3/4))

    train = values[:n_train_hours, :]
    test = values[n_train_hours:, :]
    # split into input and outputs
    train_X, train_y = train[:, :-1], train[:, -1]
    test_X, test_y = test[:, :-1], test[:, -1]
    # reshape input to be 3D [samples, timesteps, features]
    train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1]))
    test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1]))
    print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)

    # design network
    model = Sequential()
    model.add(LSTM(40, input_shape=(train_X.shape[1], train_X.shape[2])))
    model.add(Dense(1))
    model.compile(loss='mae', optimizer='adam')

    # fit network and save model based on least loss values
    early_stop = EarlyStopping(monitor='loss', min_delta=0.00000001, patience=3, mode='min', verbose=2)
    checkpoint_path = "training_2/cp-{epoch:04d}.h5"
    checkpoint_dir = os.path.dirname(checkpoint_path)
    checkpoint = ModelCheckpoint(filepath=checkpoint_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min', period=1)

    history = model.fit(train_X, train_y, batch_size=n_train_hours, epochs=400, verbose=2, callbacks=[checkpoint], validation_data=(test_X, test_y), shuffle=False)

    # plot history
    pyplot.plot(history.history['loss'], label='train')
    pyplot.plot(history.history['val_loss'], label='test')
    pyplot.legend()
    pyplot.show()

    # make a prediction
    fileNames = (os.popen("ls ./"+ checkpoint_dir).read()).split()
    model.load_weights('./'+checkpoint_dir+'/'+fileNames[-1])
    yhat = model.predict(test_X)
    test_X = test_X.reshape((test_X.shape[0], test_X.shape[2]))
    # invert scaling for forecast
    inv_yhat = concatenate((yhat, test_X[:, 1:]), axis=1)
    inv_yhat = scaler.inverse_transform(inv_yhat)
    inv_yhat = inv_yhat[:,0]
    print("The predicted variable: ", inv_yhat.shape)
    # invert scaling for actual
    test_y = test_y.reshape((len(test_y), 1))
    inv_y = concatenate((test_y, test_X[:, 1:]), axis=1)
    inv_y = scaler.inverse_transform(inv_y)
    inv_y = inv_y[:,0]
    print("The predicted variable: ", inv_yhat.shape)
    print("The predicted Y^ variable: ", inv_yhat)
    print("Actual Y variable: ", inv_y)
    # calculate RMSE
    rmse = sqrt(mean_squared_error(inv_y, inv_yhat))
    print('Test RMSE: %.3f' % rmse)

def futurePrediction(masterDict,argv):
    global train_X
    data = dataPreprocessing(masterDict,argv)
    values = data.values[nonzero-1:,:]

    # design network
    model = Sequential()
    model.add(LSTM(40, input_shape=(train_X.shape[1], train_X.shape[2])))
    model.add(Dense(1))
    model.compile(loss='mae', optimizer='adam')
    # loading the best saved model
    checkpoint_path = "training_2/cp-{epoch:04d}.h5"
    checkpoint_dir = os.path.dirname(checkpoint_path)
    # make a prediction
    fileNames = (os.popen("ls ./"+ checkpoint_dir).read()).split()
    model.load_weights('./'+checkpoint_dir+'/'+fileNames[-1])
    print ("---------------------------------------------------------------")
    print ("---------------------------------------------------------------")
    for i in range (0, (len(values) - 2)):

        test_X = values[i, :-1]
        test_X = np.transpose(test_X)
        test_X = test_X.reshape((1, 1, 3))
        #print ()
        yhat = model.predict(test_X)

        test_X = test_X.reshape((test_X.shape[0], test_X.shape[2]))

        # invert scaling for forecast
        inv_yhat = concatenate((yhat, test_X[:, 1:]), axis=1)
        inv_yhat = scaler.inverse_transform(inv_yhat)
        inv_yhat = inv_yhat[:,0]

        values [i+1,0] = yhat
        print ("Prediction at day-{} = {}".format(i,inv_yhat))


import pygame

from constants.dimensions import SQUARE_LEN, BOARD_WIDTH, BOARD_HEIGHT
from constants.colors import GREEN

class Snake:
	def __init__(self):
		self.direction = "right"
		self.positions = [(i, 5) for i in range(1,5)]
		self.get_rects()

	def move(self, apple_pos: tuple[int, int]=None) -> bool:
		if self.direction == "right":
			movement = [1, 0]
		if self.direction == "left":
			movement = [-1, 0]
		if self.direction == "up":
			movement = [0, -1]
		if self.direction == "down":
			movement = [0, 1]

		movement_x = movement[0]
		movement_y = movement[1]

		self.head = self.positions[-1]
		self.positions.append((self.head[0]+movement_x, self.head[-1]+movement_y))
		apple_collision = self.apple_collision(apple_pos)
		if not apple_collision:
			del self.positions[0]
		self.get_rects()
		return apple_collision

	def check_collisions(self):
		return self.snake_collision() or self.edge_collision()

	def snake_collision(self) -> bool:
		return self.positions.count(self.head) > 1

	def apple_collision(self, apple_pos: tuple[int, int]) -> bool:
		return self.head == apple_pos

	def edge_collision(self) -> bool:
		lr_collision = self.head[0] < 0 or self.head[0] > BOARD_WIDTH-1
		ud_collision = self.head[1] < 0 or self.head[1] > BOARD_HEIGHT-1
		edge_collision = lr_collision or ud_collision
		return edge_collision

	def get_rects(self):
		self.rects = [pygame.Rect(pos[0]*SQUARE_LEN, pos[1]*SQUARE_LEN,
			SQUARE_LEN, SQUARE_LEN) for pos in self.positions]

	def draw(self, screen: pygame.Surface):
		for square in self.rects:
			pygame.draw.rect(screen, GREEN, square)

	def change_direction(self, direction: str) -> str:
		if self.direction == direction: return

		directions = self.direction, direction
		opposite = ("right" in directions and "left" in directions) or ("up" in directions and "down" in directions)

		if opposite: return

		self.direction = direction

class LoopSnake(Snake):
	def check_collisions(self):
		if self.snake_collision():
			return True # collision has happened

		new_pos = None
		if self.head[0] < 0:
			new_pos = BOARD_WIDTH - 1, self.head[1]
		elif self.head[0] > BOARD_WIDTH-1:
			new_pos = 0, self.head[1]
		elif self.head[1] < 0:
			new_pos = self.head[0], BOARD_HEIGHT-1
		elif self.head[1] > BOARD_HEIGHT - 1:
			new_pos = self.head[0], 0

		if new_pos:
			self.positions.append(new_pos)
			del self.positions[0]


"""
Core of the DeltaSNOW model. This module provides the public function
:func:`deltasnow_snowpack_evolution` which implements the algorithm of the
deltaSNOW model with numba but does not validate input data. Users are
discouraged to use this function and should rather use the
:func:`pydeltasnow.main.swe_deltasnow` function instead.

Significant parts of the code in this module are based on the work of Manuel
Theurl: https://github.com/manueltheurl/snow_to_swe

"""
import numpy as np
from numba import njit

from pydeltasnow import __version__

__author__ = "Johannes Aschauer"
__copyright__ = "Johannes Aschauer"
__license__ = "GPL-2.0-or-later"

G = 9.81  # gravitational acceleration on earth
PRECISION = 10e-10  # floating point precision

@njit
def _assign_H(
        h_dd,
        swe_dd,
        age_dd,
        h,
        swe,
        age,
        H,
        SWE,
        t,
        day_tot,
):
    """
    Assign snowpack properties of the next timestep.
    """
    if t < day_tot:
        h[:, t + 1] = h_dd
        swe[:, t + 1] = swe_dd
        age[:, t + 1] = age_dd
        H[t + 1] = np.sum(h[:, t + 1])
        SWE[t + 1] = np.sum(swe[:, t + 1])

    return h, swe, age, H, SWE

@njit
def _compact_H(
        h,
        swe,
        swe_hat,
        age,
        ts,
        eta_null,
        k,
        rho_max,
):
    """
    Compaction of individual snow layers without additional load
    Values of today are compacted to values of tomorrow.
    """
    age_d = 0 if h == 0 else age
    h_dd = h / (1 + (swe_hat * G * ts) / eta_null * np.exp(-k * swe / h))
    h_dd = swe / rho_max if swe / h_dd > rho_max else h_dd
    h_dd = 0 if h == 0 else h_dd
    swe_dd = swe
    age_dd = 0 if h == 0 else age_d + 1
    rho_dd = 0 if h == 0 else swe_dd / h_dd
    rho_dd = rho_max if rho_max - rho_dd < PRECISION else rho_dd
    return h_dd, swe_dd, age_dd, rho_dd

@njit
def _drench_H(
        t,
        ly,
        ly_tot,
        day_tot,
        Hobs,
        h,
        swe,
        age,
        H,
        SWE,
        ts,
        eta_null,
        k,
        rho_max
):
    """
    Drenching of the snowpack if the observed snowdepth decreased significantly.
    """
    Hobs_d = Hobs[t]
    h_d = h[:, t]
    swe_d = swe[:, t]
    age_d = age[:, t]

    # distribute mass top-down
    # reversed not working in numba
    for i in range(ly - 1, -1, -1):
        # for i in reversed(range(ly)):
        if np.sum(np.array([element for j, element in enumerate(h_d) if j != i])) + swe_d[
            i] / rho_max - Hobs_d >= PRECISION:
            # layers is densified to rho_max
            h_d[i] = swe_d[i] / rho_max
        else:
            # layer is densified as far as possible
            # but doesnt reach rho_max
            h_d[i] = swe_d[i] / rho_max + np.abs(
                np.sum(np.array([element for j, element in enumerate(h_d) if j != i])) + swe_d[i] / rho_max - Hobs_d)
            break

    true_false_list = np.ones(ly, dtype="bool")
    for i, (swe_d_val, h_d_val) in enumerate(zip(swe_d[:ly], h_d[:ly])):
        true_false_list[i] = rho_max - swe_d_val / h_d_val <= PRECISION

    if np.all(true_false_list):
        # no further compaction, runoff
        scale = Hobs_d / np.sum(h_d)
        h_d = h_d * scale  # all layers are compressed (and have rho_max) [m]
        swe_d = swe_d * scale

    h[:, t] = h_d
    swe[:, t] = swe_d
    age[:, t] = age_d
    H[t] = np.sum(h[:, t])
    SWE[t] = np.sum(swe[:, t])

    # no further compaction possible
    h_dd, swe_dd, age_dd, rho_dd = _dry_metamorphism(
        h[:, t],
        swe[:, t],
        age[:, t],
        ly_tot,
        ly,
        ts,
        eta_null,
        k,
        rho_max)

    # set values for next day
    h, swe, age, H, SWE = _assign_H(h_dd, swe_dd, age_dd, h, swe, age, H, SWE, t, day_tot)

    return h, swe, age, H, SWE, rho_dd

@njit
def _dry_metamorphism(
        h_d,
        swe_d,
        age_d,
        ly_tot,
        ly,
        ts,
        eta_null,
        k,
        rho_max
):
    """
    Compaction of dry snowpack.
    """
    # overburden of current day (swe_hat_d)
    swe_hat_d = np.zeros(ly_tot)
    for i in range(ly_tot):
        swe_hat_d[i] = np.sum(swe_d[i:ly_tot])

    h_dd = h_d.copy()
    swe_dd = swe_d.copy()
    age_dd = age_d.copy()
    rho_dd = np.zeros(ly_tot)

    for i, (h, swe, swe_hat, age) in enumerate(zip(h_d[:ly], swe_d[:ly], swe_hat_d[:ly], age_d[:ly])):
        h_dd[i], swe_dd[i], age_dd[i], rho_dd[i] = _compact_H(
            h,
            swe,
            swe_hat,
            age,
            ts,
            eta_null,
            k,
            rho_max
        )

    return h_dd, swe_dd, age_dd, rho_dd

@njit
def _scale_H(
        t,
        ly,
        ly_tot,
        day_tot,
        deltaH,
        Hobs,
        h,
        swe,
        age,
        H,
        SWE,
        ts,
        eta_null,
        k,
        rho_max,
):
    """
    Scale snowpack if the settling was assumed a bit too strong or too weak.
    """
    Hobs_d = Hobs[t - 1]
    Hobs_dd = Hobs[t]
    h_d = h[:, t - 1]
    swe_d = swe[:, t - 1]
    age_d = age[:, t]

    # todays overburden
    swe_hat_d = np.zeros(ly_tot)
    for i in range(ly_tot):
        swe_hat_d[i] = np.sum(swe_d[i:ly_tot])

    # analytical solution for layerwise adapted viskosity eta
    # assumption: recompaction ~ linear height change of yesterdays layers (see paper)
    eta_cor = np.zeros(ly_tot)
    for i in range(ly_tot):
        if swe_d[i] == 0. or h_d[i] == 0:
            eta_cor[i] = 0.
        else:
            rho_d = swe_d[i] / h_d[i]
            x = ts * G * swe_hat_d[i] * np.exp(-k * rho_d)  # yesterday
            P = h_d[i] / Hobs_d  # yesterday
            eta_cor[i] = Hobs_dd * x * P / (h_d[i] - Hobs_dd * P) if (h_d[i] - Hobs_dd * P) != 0 else np.inf

    h_dd_cor = np.zeros(ly_tot)
    for i in range(ly_tot):
        if h_d[i] == 0 or eta_cor[i] == 0:
            h_dd_cor[i] = 0.
        else:
            h_dd_cor[i] = h_d[i] / (1 + (swe_hat_d[i] * G * ts) / eta_cor[i] * np.exp(-k * swe_d[i] / h_d[i]))
    h_dd_cor[np.isnan(h_dd_cor)] = 0
    H_dd_cor = np.sum(h_dd_cor)

    # and check, if Hd.cor is the same as Hobs.d
    if np.abs(H_dd_cor - Hobs_dd) > PRECISION:
        print("WARNING: error in exponential re-compaction: H.dd.cor-Hobs.dd")

    # which layers exceed rho.max?
    idx_max_arr = np.zeros(ly_tot, dtype='bool')
    for i, (swe_e_val, h_dd_cor_val) in enumerate(zip(swe_d, h_dd_cor)):
        try:
            idx_max_arr[i] = np.divide(swe_e_val, h_dd_cor_val) - rho_max > PRECISION
        except:
            idx_max_arr[i] = False
    idx_max_arr[np.isnan(idx_max_arr)] = False

    if np.any(idx_max_arr):
        if np.count_nonzero(idx_max_arr) < ly:
            # collect excess swe in those layers
            swe_excess = swe_d[idx_max_arr] - h_dd_cor[idx_max_arr] * rho_max

            # set affected layer(s) to rho.max
            swe_d[idx_max_arr] = swe_d[idx_max_arr] - swe_excess

            # distribute excess swe to other layers top-down
            lys = range(ly)
            lys_non_max = []
            for e in lys:
                if e not in np.nonzero(idx_max_arr)[0]:
                    lys_non_max.append(e)

            i = lys_non_max[-1]
            swe_excess_all = np.sum(swe_excess)

            while swe_excess_all > 0:
                # layer tolerates this swe amount to reach rho.max
                swe_res = h_dd_cor[i] * rho_max - swe_d[i]
                if swe_res > swe_excess_all:
                    swe_res = swe_excess_all

                swe_d[i] = swe_d[i] + swe_res
                swe_excess_all = swe_excess_all - swe_res
                i = i - 1
                if i < 0 < swe_excess_all:
                    # runoff
                    break
        else:
            # if all layers have density > rho.max
            # remove swe.excess from all layers (-> runoff)
            # (this sets density to rho.max)
            swe_excess = swe_d[idx_max_arr] - h_dd_cor[idx_max_arr] * rho_max
            swe_d[idx_max_arr] = swe_d[idx_max_arr] - swe_excess

    h[:, t] = h_dd_cor
    swe[:, t] = swe_d
    age[:, t] = age_d
    H[t] = np.sum(h[:, t])
    SWE[t] = np.sum(swe[:, t])

    # compact actual day
    h_dd, swe_dd, age_dd, rho_dd = _dry_metamorphism(
        h[:, t],
        swe[:, t],
        age[:, t],
        ly_tot,
        ly,
        ts,
        eta_null,
        k,
        rho_max,
    )

    # set values for next day
    h, swe, age, H, SWE = _assign_H(
        h_dd,
        swe_dd,
        age_dd,
        h,
        swe,
        age,
        H,
        SWE,
        t,
        day_tot,
    )
    return h, swe, age, H, SWE, rho_dd

@njit
def deltasnow_snowpack_evolution(
        Hobs,
        rho_max,
        rho_null,
        c_ov,
        k_ov,
        k,
        tau,
        eta_null,
        resolution,
):
    """
    This is the main loop in the delta snow model. Should be called on HS chunks
    of consecutive nonzeros.

    Parameters
    ----------
    Hobs : 1D :class:`numpy.ndarray` of floats
        Measured snow height. Needs to be in [m].
        Must comply to the following constraints:
            - no nans
            - continuous entries (no missing dates)
    rho_max : float, optional
        Maximum density of an individual snow layer produced by the deltasnow
        model in [kg/m3], rho_max needs to be positive. The default is 401.2588.
    rho_null : float, optional
        Fresh snow density for a newly created layer [kg/m3], rho_null needs to
        be positive. The default is 81.19417.
    c_ov : float, optional
        Overburden factor due to fresh snow [-], c_ov needs to be positive. The
        default is 0.0005104722.
    k_ov : float, optional
        Defines the impact of the individual layer density on the compaction due
        to overburden [-], k_ov need to be in the range [0,1].
        The default is 0.37856737.
    k : float, optional
        Exponent of the exponential-law compaction [m3/kg], k needs to be
        positive. The default is 0.02993175.
    tau : float, optional
        Uncertainty bound [m], tau needs to be positive.
        The default is 0.02362476.
    eta_null : float, optional
        Effective compactive viscosity of snow for "zero-density" [Pa s].
        The default is 8523356.
    resolution : float
        Timedelta in hours between snow observations.

    Raises
    ------
    RuntimeError
        If the snowpack evolution has somehow gone wrong.

    Returns
    -------
    SWE : 1D :class:`numpy.ndarray` of floats
        Calculated SWE in [mm]. Same shape as Hobs.

    """

    ly_tot = np.count_nonzero(Hobs)  # maximum number of layers [-]
    day_tot = len(Hobs)  # total days from first to last snowfall [-]

    # preallocate output arrays
    H = np.zeros(day_tot)  # modeled total height of snow at any day [m]
    SWE = np.zeros(day_tot)  # modeled total SWE at any day [kg/m2]

    # preallocate matrix as days X layers
    h = np.zeros((ly_tot, day_tot))  # modeled height of snow in all layers [m]
    swe = np.zeros((ly_tot, day_tot))  # modeled swe in all layers [kg/m2]
    age = np.zeros((ly_tot, day_tot))  # age in all layers

    ly = 1  # layer number [-]
    ts = resolution * 3600

    for t in range(day_tot):
        # snowdepth = 0, no snow cover
        if Hobs[t] == 0:
            H[t] = 0
            SWE[t] = 0
            h[:, t] = 0
            swe[:, t] = 0

        # there is snow
        elif Hobs[t] > 0:  # redundant if, cause can snow height be negative?
            # first snow in/during season
            if Hobs[t - 1] == 0:
                ly = 1
                age[ly - 1, t] = 1
                h[ly - 1, t] = Hobs[t]
                H[t] = Hobs[t]
                swe[ly - 1, t] = rho_null * Hobs[t]
                SWE[t] = swe[ly - 1, t]

                # compact actual day
                h_dd, swe_dd, age_dd, rho_dd = _dry_metamorphism(
                    h[:, t],
                    swe[:, t],
                    age[:, t],
                    ly_tot,
                    ly,
                    ts,
                    eta_null,
                    k,
                    rho_max,
                )

                h, swe, age, H, SWE = _assign_H(
                    h_dd,
                    swe_dd,
                    age_dd,
                    h,
                    swe,
                    age,
                    H,
                    SWE,
                    t,
                    day_tot,
                )

            elif Hobs[t - 1] > 0:
                deltaH = Hobs[t] - H[t]
                if deltaH > tau:
                    sigma_null = deltaH * rho_null * G
                    epsilon = c_ov * sigma_null * np.exp(-k_ov * rho_dd / (rho_max - rho_dd))
                    h[:, t] = (1 - epsilon) * h[:, t]
                    swe[:, t] = swe[:, t - 1]
                    age[:ly, t] = age[:ly, t - 1] + 1
                    H[t] = np.sum(h[:, t])
                    SWE[t] = np.sum(swe[:, t])

                    # only for new layer
                    ly = ly + 1
                    h[ly - 1, t] = Hobs[t] - H[t]
                    swe[ly - 1, t] = rho_null * h[ly - 1, t]
                    age[ly - 1, t] = 1

                    # recompute
                    H[t] = np.sum(h[:, t])
                    SWE[t] = np.sum(swe[:, t])

                    # compact actual day
                    h_dd, swe_dd, age_dd, rho_dd = _dry_metamorphism(
                        h[:, t],
                        swe[:, t],
                        age[:, t],
                        ly_tot,
                        ly,
                        ts,
                        eta_null,
                        k,
                        rho_max,
                    )

                    # set values for next day
                    h, swe, age, H, SWE = _assign_H(
                        h_dd,
                        swe_dd,
                        age_dd,
                        h,
                        swe,
                        age,
                        H,
                        SWE,
                        t,
                        day_tot,
                    )

                # no mass gain or loss, but scaling
                elif -tau <= deltaH <= tau:
                    h, swe, age, H, SWE, rho_dd = _scale_H(
                        t,
                        ly,
                        ly_tot,
                        day_tot,
                        deltaH,
                        Hobs,
                        h,
                        swe,
                        age,
                        H,
                        SWE,
                        ts,
                        eta_null,
                        k,
                        rho_max,
                    )

                elif deltaH < -tau:
                    h, swe, age, H, SWE, rho_dd = _drench_H(
                        t,
                        ly,
                        ly_tot,
                        day_tot,
                        Hobs,
                        h,
                        swe,
                        age,
                        H,
                        SWE,
                        ts,
                        eta_null,
                        k,
                        rho_max,
                    )

                else:
                    raise RuntimeError("no valid calculated HS deviation.")
    return SWE


import random
import math
import numpy as np
from functools import reduce
import string
from console import Console

class Comp:
    def __init__(self, parent):
        self.parts = []
        self.parent = parent
        self.current = self

    def addChar(self, char):
        if self.current != self:
            self.current.addChar(char)
            return
        if char == "(":
            new = Comp(self)
            self.parts.append(new)
            self.current = new
            return
        if char == ")":
            self.parent.current = self.parent
            return
        if char == " ":
            return
        if char in "0123456789":
            if len(self.parts) == 0 or not isinstance(self.parts[-1], str) or self.parts[-1] in "+*":
                self.parts.append(char)
            else:
                self.parts[-1] = self.parts[-1] + char
            return
        self.parts.append(char)

    def __repr__(self):
        return str(self.parts)

    def compute(self):
        numbers = self.parts[2::2]
        symbols = self.parts[1::2]
        running = self.evali(self.parts[0])
        newNumbers = []
        newSymbols = []
        for symbol, number in zip(symbols, numbers):
            if symbol == '+':
                running += self.evali(number)
            elif symbol == '*':
                newNumbers.append(running)
                newSymbols.append('*')
                running = self.evali(number)
            else:
                print("Problem")
        newNumbers.append(running)
        print(newSymbols)
        print(newNumbers)
        total = reduce(lambda a, b: a * b, newNumbers)
        print(str(self.parts) + " = " + str(total))
        return total

    def evali(self, smt):
        return int(smt) if isinstance(smt, str) else smt.compute()

if __name__ == "__main__":
    file = open("input.txt", "r")
    lines = file.readlines()
    #data = list(map(int, lines))
    data = list(map(lambda x: x.strip(), lines))
    total = 0
    for line in data:
        comp = Comp(None)
        for char in line:
            comp.addChar(char)
        total += comp.compute()
    print(total)


from .models import UserProfile
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django.contrib.auth import get_user_model
from cart.models import Cart

User = get_user_model()

@receiver(post_delete, sender=UserProfile)
def deleteProfile(instance, **kwargs):
    instance.user.delete()

@receiver(post_save, sender=User)
def createProfile(instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

@receiver(post_delete, sender=Cart)
def cartCreate(instance, **kwargs):
    instance.profile.delete()

@receiver(post_save, sender=UserProfile)
def createCart(instance,  created, **kwargs):
    if created:
        Cart.objects.create(profile=instance)


import os
import tempfile
import re
from .generic_manager import GenericManager, GenericManagerProperties
from .apt_parser import AptParser

from ambari_commons.constants import AMBARI_SUDO_BINARY
from ambari_commons import shell
from resource_management.core.logger import Logger

def replace_underscores(function_to_decorate):
  def wrapper(*args, **kwargs):
    self = args[0]
    name = args[1].replace("_", "-")
    return function_to_decorate(self, name, *args[2:], **kwargs)
  return wrapper

class AptManagerProperties(GenericManagerProperties):
  """
  Class to keep all Package-manager depended properties
  """
  locked_output = "Unable to lock the administration directory"
  repo_error = "Failure when receiving data from the peer"

  repo_manager_bin = "/usr/bin/apt-get"
  repo_cache_bin = "/usr/bin/apt-cache"
  pkg_manager_bin = "/usr/bin/dpkg"
  repo_update_cmd = [repo_manager_bin, 'update', '-qq']

  available_packages_cmd = [repo_cache_bin, "dump"]
  installed_packages_cmd = ['COLUMNS=999', pkg_manager_bin, "-l"]

  repo_definition_location = "/etc/apt/sources.list.d"

  install_cmd = {
    True: [repo_manager_bin, '-o', "Dpkg::Options::=--force-confdef", '--allow-unauthenticated', '--assume-yes', 'install'],
    False: [repo_manager_bin, '-q', '-o', "Dpkg::Options::=--force-confdef", '--allow-unauthenticated', '--assume-yes', 'install']
  }

  remove_cmd = {
    True: [repo_manager_bin, '-y', 'remove'],
    False: [repo_manager_bin, '-y', '-q', 'remove']
  }

  verify_dependency_cmd = [repo_manager_bin, '-qq', 'check']

  install_cmd_env = {'DEBIAN_FRONTEND': 'noninteractive'}

  repo_url_exclude = "ubuntu.com"
  configuration_dump_cmd = [AMBARI_SUDO_BINARY, "apt-config", "dump"]

class AptManager(GenericManager):

  def get_installed_package_version(self, package_name):
    r = shell.subprocess_executor("dpkg -s {0} | grep Version | awk '{{print $2}}'".format(package_name))
    return r.out.strip(os.linesep)

  @property
  def properties(self):
    return AptManagerProperties

  def installed_packages(self, pkg_names=None, repo_filter=None):
    """
    Return all installed packages in the system except packages in REPO_URL_EXCLUDE

    :type pkg_names list|set
    :type repo_filter str|None

    :return formatted list of packages
    """
    packages = []
    available_packages = self._available_packages_dict(pkg_names, repo_filter)

    with shell.process_executor(self.properties.installed_packages_cmd, error_callback=self._executor_error_handler,
                                strategy=shell.ReaderStrategy.BufferedChunks) as output:
      for package, version in AptParser.packages_installed_reader(output):
        if package in available_packages:
          packages.append(available_packages[package])

        if package not in available_packages:
          packages.append([package, version, "installed"])  # case, when some package not belongs to any known repo

    return packages

  def _available_packages(self, pkg_names=None, repo_filter=None):
    """
    Returning list of the installed packages with possibility to filter them by name

    :type pkg_names list|set
    :type repo_filter str|None
    """

    with shell.process_executor(self.properties.available_packages_cmd, error_callback=self._executor_error_handler,
                                strategy=shell.ReaderStrategy.BufferedChunks) as output:

      for pkg_item in AptParser.packages_reader(output):
        if repo_filter and repo_filter not in pkg_item[2]:
          continue
        if self.properties.repo_url_exclude in pkg_item[2]:
          continue
        if pkg_names and pkg_item[0] not in pkg_names:
          continue

        yield pkg_item

  def _available_packages_dict(self, pkg_names=None, repo_filter=None):
    """
    Same as available packages, but result returns as dict and package name as key

    :type pkg_names list|set
    :type repo_filter str|None
    """
    result = {}

    for item in self._available_packages(pkg_names, repo_filter):
      result[item[0]] = item

    return result

  def available_packages(self, pkg_names=None, repo_filter=None):
    """
    Returning list of the installed packages with possibility to filter them by name

    :type pkg_names list|set
    :type repo_filter str|None
    """
    return [item for item in self._available_packages(pkg_names, repo_filter)]

  def all_packages(self, pkg_names=None, repo_filter=None):
    return self.available_packages(pkg_names, repo_filter)

  def transform_baseurl_to_repoid(self, base_url):
    """
    Transforms the URL looking like proto://login:password@localhost/some/long/path to localhost_some_long_path

    :type base_url str
    :rtype str
    """
    url_proto_mask = "://"
    url_proto_pos = base_url.find(url_proto_mask)
    if url_proto_pos > 0:
      base_url = base_url[url_proto_pos+len(url_proto_mask):]

    if "@" in base_url:
      base_url = base_url.split("@", 1)[1]

    return base_url.replace("/", "_").replace(" ", "_")

  def get_available_packages_in_repos(self, repos):
    """
    Gets all (both installed and available) packages that are available at given repositories.
    :type repos resource_management.libraries.functions.repository_util.CommandRepository
    :return: installed and available packages from these repositories
    """

    filtered_packages = []
    packages = self.available_packages()
    repo_ids = []

    for repo in repos.items:
      repo_ids.append(self.transform_baseurl_to_repoid(repo.base_url))

    if repos.feat.scoped:
      Logger.info("Looking for matching packages in the following repositories: {0}".format(", ".join(repo_ids)))
      for repo_id in repo_ids:
        for package in packages:
          if repo_id in package[2]:
            filtered_packages.append(package[0])

      return filtered_packages
    else:
      Logger.info("Packages will be queried using all available repositories on the system.")

      # this is the case where the hosts are marked as sysprepped, but
      # search the repos on-system anyway.  the url specified in ambari must match the one
      # in the list file for this to work
      for repo_id in repo_ids:
        for package in packages:
          if repo_id in package[2]:
            filtered_packages.append(package[0])

      if len(filtered_packages) > 0:
        Logger.info("Found packages for repo {}".format(str(filtered_packages)))
        return filtered_packages
      else:
        return [package[0] for package in packages]

  def package_manager_configuration(self):
    """
    Reading apt configuration

    :return dict with apt properties
    """
    with shell.process_executor(self.properties.configuration_dump_cmd, error_callback=self._executor_error_handler) as output:
      configuration = list(AptParser.config_reader(output))

    return dict(configuration)

  def verify_dependencies(self):
    """
    Verify that we have no dependency issues in package manager. Dependency issues could appear because of aborted or terminated
    package installation process or invalid packages state after manual modification of packages list on the host

    :return True if no dependency issues found, False if dependency issue present
    :rtype bool
    """
    r = shell.subprocess_executor(self.properties.verify_dependency_cmd)
    pattern = re.compile("has missing dependency|E:")

    if r.code or (r.out and pattern.search(r.out)):
      err_msg = Logger.filter_text("Failed to verify package dependencies. Execution of '%s' returned %s. %s" % (self.properties.verify_dependency_cmd, r.code, r.out))
      Logger.error(err_msg)
      return False

    return True

  @replace_underscores
  def install_package(self, name, context):
    """
    Install package

    :type name str
    :type context ambari_commons.shell.RepoCallContext

    :raise ValueError if name is empty
    """
    from resource_management.core import sudo

    apt_sources_list_tmp_dir = None

    if not name:
      raise ValueError("Installation command was executed with no package name")
    elif not self._check_existence(name) or context.action_force:
      cmd = self.properties.install_cmd[context.log_output]
      copied_sources_files = []
      is_tmp_dir_created = False
      if context.use_repos:
        if 'base' in context.use_repos:
          use_repos = set([v for k, v in context.use_repos.items() if k != 'base'])
        else:
          cmd = cmd + ['-o', 'Dir::Etc::SourceList={0}'.format(self.properties.empty_file)]
          use_repos = set(context.use_repos.values())

        if use_repos:
          is_tmp_dir_created = True
          apt_sources_list_tmp_dir = tempfile.mkdtemp(suffix="-ambari-apt-sources-d")
          Logger.info("Temporary sources directory was created: {}".format(apt_sources_list_tmp_dir))

          for repo in use_repos:
            new_sources_file = os.path.join(apt_sources_list_tmp_dir, repo + '.list')
            Logger.info("Temporary sources file will be copied: {0}".format(new_sources_file))
            sudo.copy(os.path.join(self.properties.repo_definition_location, repo + '.list'), new_sources_file)
            copied_sources_files.append(new_sources_file)
          cmd = cmd + ['-o', 'Dir::Etc::SourceParts='.format(apt_sources_list_tmp_dir)]

      cmd = cmd + [name]
      Logger.info("Installing package {0} ('{1}')".format(name, shell.string_cmd_from_args_list(cmd)))
      shell.repository_manager_executor(cmd, self.properties, context, env=self.properties.install_cmd_env)

      if is_tmp_dir_created:
        for temporary_sources_file in copied_sources_files:
          Logger.info("Removing temporary sources file: {0}".format(temporary_sources_file))
          os.remove(temporary_sources_file)
        if apt_sources_list_tmp_dir:
          Logger.info("Removing temporary sources directory: {0}".format(apt_sources_list_tmp_dir))
          os.rmdir(apt_sources_list_tmp_dir)
    else:
      Logger.info("Skipping installation of existing package {0}".format(name))

  @replace_underscores
  def upgrade_package(self, name, context):
    """
    Install package

    :type name str
    :type context ambari_commons.shell.RepoCallContext

    :raise ValueError if name is empty
    """
    context.is_upgrade = True
    return self.install_package(name, context)

  @replace_underscores
  def remove_package(self, name, context, ignore_dependencies=False):
    """
    Remove package

    :type name str
    :type context ambari_commons.shell.RepoCallContext
    :type ignore_dependencies bool

    :raise ValueError if name is empty
    """
    if not name:
      raise ValueError("Installation command were executed with no package name passed")
    elif self._check_existence(name):
      cmd = self.properties.remove_cmd[context.log_output] + [name]
      Logger.info("Removing package {0} ('{1}')".format(name, shell.string_cmd_from_args_list(cmd)))
      shell.repository_manager_executor(cmd, self.properties, context)
    else:
      Logger.info("Skipping removal of non-existing package {0}".format(name))

  @replace_underscores
  def _check_existence(self, name):
    """
    For regexp names:
    If only part of packages were installed during early canceling.
    Let's say:
    1. install hbase-2-3-.*
    2. Only hbase-2-3-1234 is installed, but is not hbase-2-3-1234-regionserver yet.
    3. We cancel the apt-get

    In that case this is bug of packages we require.
    And hbase-2-3-*-regionserver should be added to metainfo.xml.

    Checking existence should never fail in such a case for hbase-2-3-.*, otherwise it
    gonna break things like removing packages and some other things.

    Note: this method SHOULD NOT use apt-get (apt.cache is using dpkg not apt). Because a lot of issues we have, when customer have
    apt-get in inconsistant state (locked, used, having invalid repo). Once packages are installed
    we should not rely on that.
    """
    # this method is more optimised than #installed_packages, as here we do not call available packages(as we do not
    # interested in repository, from where package come)
    cmd = self.properties.installed_packages_cmd + [name]

    with shell.process_executor(cmd, strategy=shell.ReaderStrategy.BufferedChunks, silent=True) as output:
      for package, version in AptParser.packages_installed_reader(output):
        return package == name

    return False


import pytest
import requests
import json
import random
from faker import Faker
import userApi_helpers as helper

baseUrl = "https://dev.playground.alexcook.dev/"

def test_getAllUsers():
    url = baseUrl + "api/users"
    response = helper.getUsers(url)
    status_code = response.status_code
    assert status_code == 200

def test_createAndDeleteUser():

    # Create test data
    fake = Faker()
    firstname = fake.first_name()
    lastname = fake.last_name()
    age = random.randint(10,80)
    address = fake.address()
    url = baseUrl + "api/users"

    # Create user
    response = helper.createUser(url, firstname, lastname, age, address)
    status_code = response.status_code
    assert status_code == 200
    result = response.json()
    userId = result.get('userId')

    # Validate data
    url = url + "/" + str(userId)
    response2 = helper.getUsers(url)
    status_code2 = response2.status_code
    assert status_code2 == 200
    result2 = response2.json()
    userIdResult = result2.get('userId')
    firstnameResult = result2.get('firstName')
    lastnameResult = result2.get('lastName')
    ageResult = result2.get('age')
    addressResult = result2.get('address')

    assert userId == userIdResult
    assert firstname == firstnameResult
    assert lastname == lastnameResult
    assert age == ageResult
    assert address == addressResult

    # Delete User
    responseDelete = helper.deleteUser(url)
    deleteStatusCode = responseDelete.status_code
    assert deleteStatusCode == 200

    # Validate Delete
    responseValidate = helper.getUsers(url)
    validateStatusCode = responseValidate.status_code
    assert validateStatusCode == 404

# def test_updateUser():
#     create user
#     update user
#     validate
#     delete user


from kwippyproject.kwippy.models.quip import Quip
from django import template
from django.shortcuts import get_object_or_404
from django.template import Node, NodeList, Template, Context, Variable
import pdb, re, string
from kwippy.views.main import ireplace
from django.conf import settings

register = template.Library()

def do_formatquery(parser, token):
    bits = list(token.split_contents())
    nodelist = NodeList()
    return FormatQueryNode(bits[1],bits[2])

def formatquery(parser, token):
    return do_formatquery(parser, token)

class FormatQueryNode(Node):
    def __init__(self, var1, var2):
        self.var1 = Variable(var1)
        self.var2 = Variable(var2)
    def __repr__(self):
        return "<FormatQueryNode>"

    def render(self, context):
        try:
            val1 = self.var1.resolve(context)
        except VariableDoesNotExist:
            val1 = None
        try:
            val2 = self.var2.resolve(context)
        except VariableDoesNotExist:
            val2 = None
        query = val1
        text = val2
        replacement = ''+query+''
        data_object = ireplace(text,query,replacement)
        return data_object

register.tag('formatquery',do_formatquery)


from django.urls import path
from . import views

urlpatterns = [
    path('alcoolul/', views.home, name = "alcoolul-home"),
    path('metanol/', views.metanol, name = "metanol-home"),
    path('etanol/', views.etanol, name = "etanol-home"),
    path('glicerol/', views.glicerol, name = "glicerol-home"),

]


import jieba
import numpy as np
import paddle
from paddlenlp.data import Vocab

def convert_example(example, tokenizer, is_test=False):
    """
    jieba 分词，转换id
    """

    input_ids = tokenizer.encode(example["text"])
    valid_length = np.array(len(input_ids), dtype='int64')
    input_ids = np.array(input_ids, dtype='int64')

    if not is_test:
        label = np.array(example["label"], dtype="int64")
        return input_ids, valid_length, label
    else:
        return input_ids, valid_length

def create_dataloader(dataset,
                      trans_fn=None,
                      mode='train',
                      batch_size=1,
                      use_gpu=False,
                      pad_token_id=0,
                      batchify_fn=None):
    """
    Creats dataloader.

    Args:
        dataset(obj:`paddle.io.Dataset`): Dataset instance.
        trans_fn(obj:`callable`, optional, defaults to `None`): function to convert a data sample to input ids, etc.
        mode(obj:`str`, optional, defaults to obj:`train`): If mode is 'train', it will shuffle the dataset randomly.
        batch_size(obj:`int`, optional, defaults to 1): The sample number of a mini-batch.
        use_gpu(obj:`bool`, optional, defaults to obj:`False`): Whether to use gpu to run.
        pad_token_id(obj:`int`, optional, defaults to 0): The pad token index.
        batchify_fn(obj:`callable`, optional, defaults to `None`): function to generate mini-batch data by merging
            the sample list, None for only stack each fields of sample in axis
            0(same as :attr::`np.stack(..., axis=0)`).

    Returns:
        dataloader(obj:`paddle.io.DataLoader`): The dataloader which generates batches.
    """
    if trans_fn:
        dataset = dataset.apply(trans_fn, lazy=True)

    if mode == 'train' and use_gpu:
        sampler = paddle.io.DistributedBatchSampler(
            dataset=dataset, batch_size=batch_size, shuffle=True)
    else:
        shuffle = True if mode == 'train' else False
        sampler = paddle.io.BatchSampler(
            dataset=dataset, batch_size=batch_size, shuffle=shuffle)
    dataloader = paddle.io.DataLoader(
        dataset,
        batch_sampler=sampler,
        return_list=True,
        collate_fn=batchify_fn)
    return dataloader


#Title: RelEnthPlots2.py
#Author: Greg Wallace
#Organization: Washington State University, HYPER Lab
#Date Created: December 3, 2021
#Date Last Edited: December 6, 2021
#Purpose: Plots (and saves a printable paper-sided pdf of) the relative enthalpies of parahydrogen, orthohydrogen, normal hydrogen, and equilibrium hydrogen using ortho increments.

import CoolProp.CoolProp as CP #Source of all thermodynamic data
import math #for "floor" function
import matplotlib.pyplot as plt #For plotting
import numpy as np #For creating plot axes

#Returns equilibrium ortho fraction based off temperature and number of iterations to use.  If no iterations are given, defaults to 7, which computs accurately from 0 to 300K up to 16 decimals
#At 20K, it should be about 0.00 (0%)
#At 77K, it shoul dbe about 0.49 (49%)
#At 300K and above, it should be 0.75 (75%)
#Source of Formula: Brandt Pedrow's thesis: https://wsuwp-uploads.s3.amazonaws.com/uploads/sites/44/2016/05/b_pedrow_011423571.pdf (pdf page 21, or numbered page 8)
#Citation: Brandt Patrick Pedrow, May 2016, "Parahydrogen-Orthohydrogen Conversion on Catalyst Loaded Scrim for Vapor Cooled Shielding of Cryogenic Storage Vessels", Master Thesis, Washington State University, Pullman.
def Yo_equilib(T,N=7,T_rot=85.4):
    if N<1 or (type(N) != int):#returns if N is not an int of 1 or above
        print("Yo_equilib requires integer N value of 1 or above")
        return None
    if T<=0:#returns if T is negative or 0, because negative temps don't exist
        print("Yo_equilib requires temperature must be in positive Kelvin")
        return None
    J=0
    K_para=0.0
    K_ortho=0.0
    #T_rot=85.4#characteristic rotational temperature of hydrogen, 85.4K
    for i in range(1,N+1):
        #print(i)
        K_para=K_para+(2*J+1)*math.exp(-J*(J+1)*T_rot/T)
        J+=1
        K_ortho=K_ortho+(2*J+1)*math.exp(-J*(J+1)*T_rot/T)
        J+=1
    Yo=3*K_ortho/(K_para+3*K_ortho)
    return Yo

# def h_ortho(T,P):
    # return CP.PropsSI('H','T',T,'P',P,'orthohydrogen') #Enthalpy in (J/kg)

#Returns the Relative Enthalpy of ortho-para mixture based off mass-weighted average
def h_mix(T,P,Yo):
    h_ortho=CP.PropsSI('H','T',T,'P',P,'orthohydrogen') #Enthalpy in (J/kg)
    h_para=CP.PropsSI('H','T',T,'P',P,'parahydrogen') #Enthalpy in (J/kg)
    return (Yo*h_ortho+(1-Yo)*h_para)

#Returns the Orthohydrogen fraction of ortho-para mixture based off a relative enthalpy
def Yo_mix(T,P,h_mix):
    h_ortho=CP.PropsSI('H','T',T,'P',P,'orthohydrogen') #Enthalpy in (J/kg)
    h_para=CP.PropsSI('H','T',T,'P',P,'parahydrogen') #Enthalpy in (J/kg)
    return ((h_mix-h_para)/(h_ortho-h_para))

#Finds final temperature when hydrogen is catalyzed from a known T_initial, P, and h_mix to an equilibrium T and Yo.  Isenthalpic, isobaric process.
#Temperature between 0K and 500K
def T_isenth(P,h_mixture):
    i=0
    T_guess=150.0
    T_max=500.0
    T_min=14.0
    h_guess=1000.0
    h_err=1.0
    Yo_guess=0.375
    while abs(h_err) > 0.000001:#Ends when enthalpy error is less than 0.001J/kg
        Yo_guess=Yo_equilib(T_guess)
        h_guess=h_mix(T_guess,P,Yo_guess)
        print("Temp Guess: " + str(T_guess))
        print("Enth Guess: " + str(h_guess))
        h_err=h_guess-h_mixture
        print("Enth err: " + str(h_err))
        if h_err>0:
            T_max=T_guess
            T_guess=(T_max+T_min)/2
        else:
            T_min=T_guess
            T_guess=(T_max+T_min)/2

        if i>1000:
            print("Did not converge in 1000 iterations!")
            return None
        i+=1
    print(i)
    return T_guess

#Returns the reverse carnot refrigeration efficiency when pulling heat out at T_L and pumping it up to T_H
def COPRefrig(T_H,T_L):
    if T_L>T_H:print("Warning: T_L is higher than T_H")
    return (T_L/(T_H-T_L))
#https://en.wikipedia.org/wiki/Heat_pump_and_refrigeration_cycle

#Finds the temperature where a certain equilibrium fraction occurs.
#Temperature reasonably accurate between 14K and 400K, but less accurate at very high and very low temps because ortho fraction approaches a constant (0 or 0.75)
def T_equilib(Yo):
    if(Yo>=0.75 or Yo<=0.0):
        print("Orthohydrogen fraction must be above 0 and below 0.75, it does not exist as equilibrium outside this range.")
        return None
    i=0

    T_max=1000.0
    T_guess=T_max
    T_min=0
    Yo_err=0.25
    Yo_guess=0.375
    while abs(Yo_err) > 0.000001:#Ends when equilibrium error is less than 1/1000 of 0.1%
        if Yo_err>0:
            T_max=T_guess
            T_guess=(T_max+T_min)/2
        else:
            T_min=T_guess
            T_guess=(T_max+T_min)/2
        Yo_guess=Yo_equilib(T_guess)

        print("Temp Guess: " + str(T_guess))
        print("Yo Guess: " + str(Yo_guess))
        Yo_err=Yo_guess-Yo
        print("Yo err: " + str(Yo_err))

        if i>1000:
            print("Did not converge in 1000 iterations!")
            return None
        i+=1
    print(str(i) + " iterations")
    return T_guess

#Setting Up Coolprop:
P_ref=101325#Pa
#Parahydrogen Reference State does not need to change:
h_ref=CP.PropsSI('H','P',P_ref,'Q',0,'parahydrogen')#should be about zero
s_ref=CP.PropsSI('S','P',P_ref,'Q',0,'parahydrogen')#should be roughly zero
#print(h_ref)
#print(s_ref)
#Orthohydrogen Reference State needs to be adjusted such that its enthalpy is 702.98 kJ/kg and entropy is 0.018269 kJ/kg-K at NBP
Dmolar_ref_ortho=CP.PropsSI('Dmolar','P',P_ref,'Q',0,'orthohydrogen')#reference state must be in molar density at liquid of normal boiling point
T_ref_ortho=CP.PropsSI('T','P',P_ref,'Q',0.5,'orthohydrogen')
CP.set_reference_state('orthohydrogen',T_ref_ortho,Dmolar_ref_ortho,702.98*(2*1.00784),0.018269*(2*1.00784)) #sets so that enthalpy and entropy difference correctly correspond to ~702.98kJ/kg and 0.018269 kJ/kg-K at 20K; but function inputs must be molar
#print(CP.PropsSI('H','P',P_ref,'Q',0,'orthohydrogen'))#should be 702980 J/kg
#print(CP.PropsSI('S','P',P_ref,'Q',0,'orthohydrogen'))#should be 18.269 J/kg-K

P=101325#Pa, pressure to test at

#Temperature range to take enthalpy values at
T_low=15#K
T_high=150#K
dT=0.1#K, step size
N_T=round((T_high-T_low)/dT)+1#Number of temperatures to check
T_arr=np.linspace(T_low,T_high,N_T)#makes an array of temperatures to check between the T_low and T_high
#print(T_arr)

Yo_low=0.0#unitless
Yo_high=0.75
dYo=0.05#Step size
N_Yo=round((Yo_high-Yo_low)/dYo)+1#Number of ortho fractions to arrange
Yo_arr=np.linspace(Yo_low,Yo_high,N_Yo)#makes an array of ortho fractions to check between the Yo_low and Yo_high
#print(Yo_arr)

h_arrs=np.zeros([N_Yo,N_T])#initialize array of array of enthalpies.  Primary array is for that ortho fraction, secondary is for each temp: [Yo1[T1,T2,T3...],Yo2[T1,T2,T3...]]
h_eq=np.zeros(N_T)#initialize array for the equilibrium enthalpies.  Each value in the array is for a specific temp

Yo_Names=[]

for k in range(N_T):#For each temperature
    for i in range(N_Yo):#for each of the ortho-fraciton setpoints (ex: 0%, 5%, 10%, etc.)
        #Generate enthalpy at each temp and ortho fraction
        h_arrs[i][k]=h_mix(T_arr[k],P,Yo_arr[i])/1000#kJ/kg, do for each temp and each ortho fraction
        if 0==k:
            Yo_Names.append(str(round(Yo_arr[i]*100))+" %")#Create string name for that ortho fraction
    #Generate enthalpy at equilib at that temp
    h_eq[k]=h_mix(T_arr[k],P,Yo_equilib(T_arr[k]))/1000#kJ/kg, equilibrium,only do for each temp
#print(h_arrs)
#print(h_eq)

Yo_Names.append("Equilibrium")

#Flip the names and the enthalpy arrays (only flips the order of the ortho-fractions, the temperature relations will not change
#This is used for plotting (so that it will plot highest ortho fracitons first, and lowest last), to make the plot's key show the lines in order of greatest to smallest
Yo_Names=np.flip(Yo_Names)
h_arrs=np.flip(h_arrs,0)

#Now the Plot:
#Set font properties before generating plot
plt.rcParams.update({'font.family':"Times New Roman",'font.size':10})

fig, h_plot=plt.subplots(figsize=(7.5,10))#start plot, give size to be 7.5" wide, 10" tall
h_plot.plot(T_arr,h_eq,color='black',linewidth=1.0)#Prints equilib curve line because I want it to show up on top in the Key
for j in range(N_Yo):
    h_plot.plot(T_arr,h_arrs[j],linewidth=1.0)
h_plot.plot(T_arr,h_eq,color='black',linewidth=1.0)#Re-prints the equilib curve a second time because I want it to show up on top of the other curves
h_plot.set_xlabel('Temperature [K]')
h_plot.set_ylabel('Relative Enthalpy [kJ/kg]')
h_plot.set_xticks(np.arange(0, 300+5, 5))
h_plot.set_yticks(np.arange(-500, 2500+50, 50))
h_plot.set_xlim([15,150])
h_plot.set_ylim([-100,2501])
h_plot.grid(True)
h_plot.legend(Yo_Names,title='Ortho Fraction:',fontsize=10,loc="lower right")
plt.savefig("RelEnthPlots2.pdf",format="pdf")#Save the figure as a pdf (vector) which can be printed vertically
#File is saved in whatever the working directory is when you run the file (ex: the user folder, or the local directory, etc.)
plt.show()#Show the plot

#https://matplotlib.org/stable/gallery/lines_bars_and_markers/psd_demo.html#sphx-glr-gallery-lines-bars-and-markers-psd-demo-py
#https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html#matplotlib.axes.Axes.legend
#https://stackoverflow.com/questions/3899980/how-to-change-the-font-size-on-a-matplotlib-plot
#https://matplotlib.org/stable/api/figure_api.html#matplotlib.figure.Figure.savefig
#https://matplotlib.org/stable/api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D.set_linewidth
#https://matplotlib.org/stable/gallery/subplots_axes_and_figures/figure_size_units.html
#https://matplotlib.org/stable/api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D.set_linestyle


import openpyxl
import decimal
from openpyxl import load_workbook
import re
import datetime
import tools

# wb = load_workbook('datatypes.xlsx')
# print(f'"{wb.active["A1"]}的数据类型为{type(wb.active["A1"].value)}')
# print(f'"{wb.active["A2"]}的数据类型为{type(wb.active["A2"].value)}')
# print(f'"{wb.active["A3"]}的数据类型为{type(wb.active["A3"].value)}')
# print(f'"{wb.active["A4"]}的数据类型为{type(wb.active["A4"].value)}')
def main():
    # print(transfer_to_decimal('100.'))
    # print(transfer_to_decimal('100.1'))
    # print(transfer_to_decimal('100.11'))
    # print(transfer_to_decimal('100.113'))
    # print(transfer_to_decimal('-100.115'))
    num_decimal = decimal.Decimal('0')
    wb = load_workbook('amount.xlsx')
    for i in range(3, 61):
        num_decimal += tools.transfer_to_decimal(wb.active['C' + str(i)].value)
    print(num_decimal)
    num_float = 0
    for i in range(3, 61):
        num_float += float(wb.active['C' + str(i)].value)
    print(num_float)

if __name__ == '__main__':
    # main()
    # get_datatype()
    # get_sumifs()
    data = [9223372036854775807,123.45,'12345',"字符串",datetime.datetime(1733,5,27,19),'=A1+A2']
    for j in range(1,len(data)+1):
        tools.save_to_excel('a',j,data[j-1])
    # save_to_excel()


from pathlib import Path

from cltl.dialogue_evaluation.graph_evaluation import GraphEvaluator

SCENARIO_FOLDER = './data/3cf7c0b8-f69d-45e8-b804-cb6aca3a044d/'
RDF_FOLDER = SCENARIO_FOLDER + f'rdf/'

#SCENARIO_FOLDER='./data/emissor'
#RDF_FOLDER ='d0fc872d-8199-46a2-94e1-aa739bc9ed7c/'

evaluator = GraphEvaluator()

evaluator.evaluate_conversation(Path(SCENARIO_FOLDER), Path(RDF_FOLDER),
                                metrics_to_plot=['GROUP A - Average degree', 'GROUP A - Sparseness'])


import librosa
import numpy as np
import tensorflow as tf
import gradio as gr

# File Paths
model_path = "sound_emotion_rec_model"
categories = ['angry', 'disgust', 'fear', 'happy', 'neutral', 'ps', 'sad']
model = tf.keras.models.load_model(model_path)

# loading the files
def extract_mfcc(audio_path, duration=3, offset=0.5, n_mfcc=40):
    # loading the data
    y, sr = librosa.load(audio_path, duration=duration, offset=offset)

    # extracting the voice feature
    mfcc = np.mean(librosa.feature.mfcc(y=y, sr=sr, n_mfcc=n_mfcc).T, axis=0)

    return mfcc

def prepare_data(audio_path):

  # extracting the features
  features = extract_mfcc(audio_path)

  # adjusting the shape
  features = [x for x in features]
  features = np.array(features)
  features = np.expand_dims(features, -1)

  return features

def clsf(audio_path):

  # extracting the features
  features = prepare_data(audio_path)

  # batching the data
  sample = np.expand_dims(features, axis=0)

  # predicting
  preds = model.predict(sample)[0]

  # results
  confidences = {categories[i]:np.round(float(preds[i]), 3) for i in range(len(categories))}

  return confidences

def pre_processor(audio_path):

  # load the audio file
  x, sample_rate = librosa.load(audio_path)

  # feature extracting (mfccs is an aduio feature)
  mfccs = np.mean(librosa.feature.mfcc(y=x, sr=sample_rate, n_mfcc=40).T, axis=0)
  feature = mfccs

  return feature

# GUI Component
gui_params = {
    "fn":clsf,
    "inputs":gr.Audio(source="upload", type="filepath"),
    "outputs" : "label",
    #live=True,
    "examples" : "examples"

}
demo = gr.Interface(**gui_params)

# Launching the demo
if __name__ == "__main__":
    demo.launch()


from collections import defaultdict

from advent_of_code.basesolver import BaseSolver

class Computer:
    def __init__(self, pause_on_output=False):
        self.current_position = 0
        self.commands = defaultdict(int)

        self.current_input_position = 0
        self.inputs = []

        self.current_relative_position = 0

        self.outputs = []

        self.pause_on_output = pause_on_output

    def restart(self):
        self.current_position = 0
        self.commands = defaultdict(int)

        self.current_input_position = 0
        self.inputs = []

        self.current_relative_position = 0

        self.outputs = []

    def load_commands(self, commands):
        for i, command in enumerate(commands):
            self.commands[i] = command

    def load_inputs(self, inputs):
        self.current_input_position = 0
        self.inputs = inputs

    def change_cmd_at_pos_1_and_2(self, param1, param2):
        self.commands[1] = param1
        self.commands[2] = param2

    ADD = 1
    MULTIPLY = 2
    INPUT = 3
    OUTPUT = 4
    JUMP_IF_TRUE = 5
    JUMP_IF_FALSE = 6
    LESS_THAN = 7
    EQUALS = 8
    ADJUST_RELATIVE_POSITION = 9
    HALT = 99
    DESCRIPTION = {
        ADD: "ADD",
        MULTIPLY: "MULTIPLY",
        INPUT: "INPUT",
        OUTPUT: "OUTPUT",
        JUMP_IF_TRUE: "JUMP_IF_TRUE",
        JUMP_IF_FALSE: "JUMP_IF_FALSE",
        LESS_THAN: "LESS_THAN",
        EQUALS: "EQUALS",
        ADJUST_RELATIVE_POSITION: "ADJUST_RELATIVE_POSITION",
        HALT: "HALT",
    }

    POSITION_MODE = 0
    IMMEDIATE_MODE = 1
    RELATIVE_MODE = 2

    def read_params(self, num_of_params):
        params = []
        for i in range(1, num_of_params + 1):
            params.append(self.commands[self.current_position + i])
        return params

    def get_value_based_on_mode(self, mode, param):
        if mode == self.POSITION_MODE:
            return self.commands[param]
        elif mode == self.IMMEDIATE_MODE:
            return param
        elif mode == self.RELATIVE_MODE:
            return self.commands[self.current_relative_position + param]

    def get_save_at_based_on_mode(self, mode, param):
        save_at = param
        if mode == self.RELATIVE_MODE:
            save_at += self.current_relative_position
        return save_at

    def execute_opcode_action(self, opcode, modes):
        # print('\tOpcode: {} ({}) | Modes: {}'.format(opcode, self.DESCRIPTION[opcode], modes))
        if opcode == self.HALT:
            self.result = self.commands[0]
            # print('\tHALT')
        elif opcode == self.ADD:
            """
            Opcode 1 adds together numbers read from two positions and stores the result in a third position.
            The three integers immediately after the opcode tell you these three positions
            - the first two indicate the positions from which you should read the input values,
            and the third indicates the position at which the output should be stored.
            """
            num_of_params = 3
            params = self.read_params(num_of_params)
            # print('\tParams: {}'.format(params))

            value0 = self.get_value_based_on_mode(modes[0], params[0])
            value1 = self.get_value_based_on_mode(modes[1], params[1])
            save_at = self.get_save_at_based_on_mode(modes[2], params[2])

            self.commands[save_at] = value0 + value1

            self.current_position += num_of_params + 1

        elif opcode == self.MULTIPLY:
            """
            Opcode 2 works exactly like opcode 1, except it multiplies the two inputs instead of adding them.
            Again, the three integers after the opcode indicate where the inputs and outputs are, not their values.
            """
            num_of_params = 3
            params = self.read_params(num_of_params)
            # print('\tParams: {}'.format(params))

            value0 = self.get_value_based_on_mode(modes[0], params[0])
            value1 = self.get_value_based_on_mode(modes[1], params[1])
            save_at = self.get_save_at_based_on_mode(modes[2], params[2])

            self.commands[save_at] = value0 * value1

            self.current_position += num_of_params + 1

        elif opcode == self.INPUT:
            """
            Opcode 3 takes a single integer as input and saves it to the address given by its only parameter.
            For example, the instruction 3,50 would take an input value and store it at address 50.
            """
            num_of_params = 1
            params = self.read_params(num_of_params)
            # print('\tParams: {}'.format(params))

            save_at = self.get_save_at_based_on_mode(modes[0], params[0])

            self.commands[save_at] = self.inputs[self.current_input_position]
            self.current_input_position += 1

            self.current_position += num_of_params + 1

        elif opcode == self.OUTPUT:
            """
            Opcode 4 outputs the value of its only parameter.
            For example, the instruction 4,50 would output the value at address 50.
            """
            num_of_params = 1
            params = self.read_params(num_of_params)
            # print('\tParams: {}'.format(params))

            value0 = self.get_value_based_on_mode(modes[0], params[0])

            self.outputs.append(value0)

            self.current_position += num_of_params + 1

        elif opcode == self.JUMP_IF_TRUE:
            """
            Opcode 5 is jump-if-true:
            if the first parameter is non-zero, it sets the instruction pointer to the value from the second parameter.
            Otherwise, it does nothing.
            """
            num_of_params = 2
            params = self.read_params(num_of_params)
            # print('\tParams: {}'.format(params))

            value0 = self.get_value_based_on_mode(modes[0], params[0])
            value1 = self.get_value_based_on_mode(modes[1], params[1])

            if value0 != 0:
                self.current_position = value1
            else:
                self.current_position += num_of_params + 1

        elif opcode == self.JUMP_IF_FALSE:
            """
            Opcode 6 is jump-if-false:
            if the first parameter is zero, it sets the instruction pointer to the value from the second parameter.
            Otherwise, it does nothing.
            """
            num_of_params = 2
            params = self.read_params(num_of_params)
            # print('\tParams: {}'.format(params))

            value0 = self.get_value_based_on_mode(modes[0], params[0])
            value1 = self.get_value_based_on_mode(modes[1], params[1])

            if value0 == 0:
                self.current_position = value1
            else:
                self.current_position += num_of_params + 1

        elif opcode == self.LESS_THAN:
            """
            Opcode 7 is less than:
            if the first parameter is less than the second parameter,
            it stores 1 in the position given by the third parameter.
            Otherwise, it stores 0.
            """
            num_of_params = 3
            params = self.read_params(num_of_params)
            # print('\tParams: {}'.format(params))

            value0 = self.get_value_based_on_mode(modes[0], params[0])
            value1 = self.get_value_based_on_mode(modes[1], params[1])
            save_at = self.get_save_at_based_on_mode(modes[2], params[2])

            if value0 < value1:
                self.commands[save_at] = 1
            else:
                self.commands[save_at] = 0

            self.current_position += num_of_params + 1

        elif opcode == self.EQUALS:
            """
            Opcode 8 is equals:
            if the first parameter is equal to the second parameter,
            it stores 1 in the position given by the third parameter.
            Otherwise, it stores 0.
            """
            num_of_params = 3
            params = self.read_params(num_of_params)
            # print('\tParams: {}'.format(params))

            value0 = self.get_value_based_on_mode(modes[0], params[0])
            value1 = self.get_value_based_on_mode(modes[1], params[1])
            save_at = self.get_save_at_based_on_mode(modes[2], params[2])

            if value0 == value1:
                self.commands[save_at] = 1
            else:
                self.commands[save_at] = 0

            self.current_position += num_of_params + 1

        elif opcode == self.ADJUST_RELATIVE_POSITION:
            """
            Opcode 9 adjusts the relative base by the value of its only parameter.
            The relative base increases (or decreases, if the value is negative)
            by the value of the parameter.
            """
            num_of_params = 1
            params = self.read_params(num_of_params)
            # print('\tParams: {}'.format(params))

            value0 = self.get_value_based_on_mode(modes[0], params[0])

            self.current_relative_position += value0

            self.current_position += num_of_params + 1

        else:
            print("INVALID OPCODE: {}".format(opcode))
            exit(-1)

    def extract_opcode(self, instruction):
        instruction_str = str(instruction).zfill(5)
        return int(instruction_str[-2:])

    def extract_modes(self, instruction):
        instruction_str = str(instruction).zfill(5)
        modes = [int(x) for x in instruction_str[:3]]
        modes.reverse()
        return modes

    def process_commands(self):
        while True:
            # commands_before = list(enumerate(self.commands))
            # print(commands_before)

            current_instruction = self.commands[self.current_position]
            # print('Position: {} | Instruction: {}'.format(self.current_position, current_instruction))
            current_opcode = self.extract_opcode(current_instruction)
            current_modes = self.extract_modes(current_instruction)

            self.execute_opcode_action(current_opcode, current_modes)

            if current_opcode == self.HALT:
                return self.HALT
            if current_opcode == self.OUTPUT and self.pause_on_output:
                return self.OUTPUT

            # commands_after = list(enumerate(self.commands))
            # print(set(commands_after) - set(commands_before))

class Hull:
    BLACK = 0
    WHITE = 1

    UP = "up"
    RIGHT = "right"
    DOWN = "down"
    LEFT = "left"

    TURN_LEFT = 0
    TURN_RIGHT = 1

    def __init__(self):
        self.robot_position = (0, 0)
        self.robot_looks = Hull.UP
        self.panel_colors = defaultdict(int)

    def paint_panel(self, color):
        self.panel_colors[self.robot_position] = color

    def turn_robot(self, turn):
        if (self.robot_looks == Hull.UP and turn == Hull.TURN_LEFT) or (
            self.robot_looks == Hull.DOWN and turn == Hull.TURN_RIGHT
        ):
            self.robot_looks = Hull.LEFT
        elif (self.robot_looks == Hull.UP and turn == Hull.TURN_RIGHT) or (
            self.robot_looks == Hull.DOWN and turn == Hull.TURN_LEFT
        ):
            self.robot_looks = Hull.RIGHT
        elif (self.robot_looks == Hull.RIGHT and turn == Hull.TURN_LEFT) or (
            self.robot_looks == Hull.LEFT and turn == Hull.TURN_RIGHT
        ):
            self.robot_looks = Hull.UP
        elif (self.robot_looks == Hull.RIGHT and turn == Hull.TURN_RIGHT) or (
            self.robot_looks == Hull.LEFT and turn == Hull.TURN_LEFT
        ):
            self.robot_looks = Hull.DOWN

    def move_robot(self):
        c_x = self.robot_position[0]
        c_y = self.robot_position[1]

        if self.robot_looks == Hull.UP:
            c_y += 1
        elif self.robot_looks == Hull.RIGHT:
            c_x += 1
        elif self.robot_looks == Hull.DOWN:
            c_y -= 1
        elif self.robot_looks == Hull.LEFT:
            c_x -= 1

        self.robot_position = (c_x, c_y)

    def get_max_min_x_y(self):
        min_x = [*self.panel_colors][0][0]
        min_y = [*self.panel_colors][0][1]
        max_x = [*self.panel_colors][0][0]
        max_y = [*self.panel_colors][0][1]

        for coords in [*self.panel_colors]:
            x = coords[0]
            y = coords[1]
            if x < min_x:
                min_x = x
            if y < min_y:
                min_y = y
            if x > max_x:
                max_x = x
            if y > max_y:
                max_y = y

        return min_x, min_y, max_x, max_y

    def draw(self):
        min_x, min_y, max_x, max_y = self.get_max_min_x_y()

        result = ""
        for y in reversed(list(range(min_y, max_y + 1))):
            for x in range(min_x, max_x + 1):
                result += "\u25a9" if self.panel_colors[(x, y)] else "."
            result += "\n"
        return "\n{}".format(result.strip())

def load_input(data):
    return [int(x.strip()) for x in data.split(",")]

class Y2019D11Solver(BaseSolver):
    def solve_part_a(self):
        input_list = load_input(self.data)

        computer = Computer(pause_on_output=True)
        computer.load_commands(input_list)

        hull = Hull()
        while True:
            current_color = hull.panel_colors[hull.robot_position]
            computer.load_inputs([current_color])

            res1 = computer.process_commands()
            if res1 == Computer.HALT:
                break
            res2 = computer.process_commands()
            if res2 == Computer.HALT:
                break

            color_to_paint = computer.outputs[-2]
            direction_to_turn = computer.outputs[-1]
            hull.paint_panel(color_to_paint)
            hull.turn_robot(direction_to_turn)
            hull.move_robot()

        return len(hull.panel_colors.keys())

    def solve_part_b(self):
        input_list = load_input(self.data)

        computer = Computer(pause_on_output=True)
        computer.load_commands(input_list)

        hull = Hull()
        hull.panel_colors[(0, 0)] = Hull.WHITE
        while True:
            current_color = hull.panel_colors[hull.robot_position]
            computer.load_inputs([current_color])

            res1 = computer.process_commands()
            if res1 == Computer.HALT:
                break
            res2 = computer.process_commands()
            if res2 == Computer.HALT:
                break

            color_to_paint = computer.outputs[-2]
            direction_to_turn = computer.outputs[-1]
            hull.paint_panel(color_to_paint)
            hull.turn_robot(direction_to_turn)
            hull.move_robot()

        result = hull.draw()

        return result


from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from .views import (
    HomeView,
    AboutView,
    ProjectsView,
    ProjectsDetailView,
    ContactView
)

app_name = 'portapp'

urlpatterns = [
    path('', HomeView.as_view(), name='home'),
    path('about/', AboutView.as_view(), name='about'),
    path('projects/', ProjectsView.as_view(), name='projects'),
    path('projects/detail/<int:pk>/', ProjectsDetailView.as_view(), name='projects'),
    path('contact/', ContactView.as_view(), name='contact'),
]

urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


"""api URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf import settings
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth.models import *
from rest_framework import routers, serializers, viewsets, permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from apiapp import views

schema_view = get_schema_view(
    openapi.Info(
        title='podo api',
        default_version='v4',
        description='api for podo ios',
        terms_of_service='https://www.ourapp.com/policies/terms/',
        contact=openapi.Contact(email=''),
        license=openapi.License(name='...'),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)

urlpatterns = [

    path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
    path('', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    path(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
    path('admin/', admin.site.urls),

    path('signup/',views.signup, name = 'signup'),
    path('addwallet/<int:id>/',views.integrate_phantom_wallet, name = 'integrate_phantom_wallet'),
    path('signin/',views.signin, name = 'signin'),
    path('signout/',views.signout, name = 'signout'),

#    path('article_list',views.article_list, name = 'article_list'),
#    path('article_detail/<int:pk>',views.article_detail, name = 'article_detail'),
#    path('article_create',views.article_create, name = 'article_create'),
#    path('article_update/<int:pk>',views.article_update, name = 'article_update'),
#    path('article_delete/<int:pk>',views.article_delete, name = 'article_delete'),
#    path('question_list',views.question_list, name = 'question_list'),
#    path('question_detail/<int:pk>',views.question_detail, name = 'question_detail'),
#    path('question_create',views.question_create, name = 'question_create'),
#    path('question_update/<int:pk>',views.question_update, name = 'question_update'),
#    path('question_delete/<int:pk>',views.question_delete, name = 'question_delete'),
#    path('comment_list',views.comment_list, name = 'comment_list'),
#    path('comment_detail/<int:pk>',views.comment_detail, name = 'comment_detail'),
#    path('comment_create',views.comment_create, name = 'comment_create'),
#    path('comment_update/<int:pk>',views.comment_update, name = 'comment_update'),
#    path('comment_delete/<int:pk>',views.comment_delete, name = 'comment_delete'),
#    path('<int:pk>/answer_detail',views.answer_detail, name = 'answer_detail'),
#    path('<int:pk>/answer_create',views.answer_create, name = 'answer_create'),
#    path('<int:pk>/answer_update',views.answer_update, name = 'answer_update'),
#    path('<int:pk>/answer_delete',views.answer_delete, name = 'answer_delete'),
#    path('<int:pk>/answer_choose',views.answer_choose, name='answer_choose'),

]


import tkinter as tk
from PIL import Image, ImageTk

win = tk.Tk()
Pic_path = "img5.jpg"
im = Image.open(Pic_path)  # 读取图片
tkImage_list = []

depth = 3
width, height = im.size

def cut_image(Pic):  # 分割图片
    global tkImage_list
    w = int(width/depth)
    h = int(height/depth)
    for i in range(depth):
        for j in range(depth):
            box = (w*j, h*i, w*(j+1), h*(i+1))  # 需要切割的矩形区域
            image = Pic.crop(box)
            tkImage_list.append(ImageTk.PhotoImage(image))
            label = tk.Label(win, image=tkImage_list[i*depth + j])
            """
            Tk.photoImage()显示图片时，
            当把包含该函数放在自定义函数里时，不能正常显示，移出函数又可正常显示，
            所以想到可能是变量不是全局性的缘故，改为全局变量后果然可正常显示
            """
            label.grid(row=i, column=j)
            """ 布局 https://blog.csdn.net/qq_37503890/article/details/90264864"""

if __name__ == '__main__':
    cut_image(im)
    win.mainloop()


# coding=utf-8
import os
import logging

# 第一步，创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)  # Log等级总开关

# 第二步，创建一个handler，用于写入日志文件
path = '../log'
folder = os.path.exists(path)
if not folder:  # 判断是否存在文件夹如果不存在则创建为文件夹
    os.makedirs(path)
    print('创建' + path)
else:
    print('文件夹存在')

log_name = path + '/log.log'
fh = logging.FileHandler(log_name, mode='w')
fh.setLevel(logging.DEBUG)  # 输出到file的log等级的开关

# 第三步，再创建一个handler，用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.WARNING)  # 输出到console的log等级的开关

# 第四步，定义handler的输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
ch.setFormatter(formatter)

# 第五步，将logger添加到handler里面
logger.addHandler(fh)
logger.addHandler(ch)

# 日志
logger.debug('this is a logger debug message')
logger.info('this is a logger info message')
logger.warning('this is a logger warning message')
logger.error('this is a logger error message')
logger.critical('this is a logger critical message')


import sys
import logging
import wandb
import torch
import os
sys.path.insert(0,os.getcwd())

import yaml
from utils.functions import setup_logger

import argparse
from tqdm import tqdm
from pytorch_lightning import seed_everything

from dataset.transformer_dataset import ClickbaitDataModule
from model.gan import GAN, build_gan_optimizer, gan_save_model
from model.transformer import TransformerForSequenceClassification, \
                              build_transformer_optimizer,\
                              transformer_save_model

from train.transformer_train_epoch import transformer_train_epoch, transformer_validate_model
from train.gan_train_epoch import gan_train_epoch, gan_validate_model
import pdb

logger = logging.getLogger()

def main(config_file: str):
    with open(config_file,'r') as f:
            config = yaml.safe_load(f)

    if not os.path.exists(config['output_path']):
        os.makedirs(config['output_path'])

    setup_logger(logger,config['output_path'])

    best_dev_f1, best_epoch = 0,0
    seed_everything(42)

    # run = wandb.init(project="banbait",entity='colab-team',config=config)
    run = wandb.init(project="banbait",config=config)
    config = wandb.config
    model_type = config.model_type
    ratio = getattr(config, 'ratio',100)
    main_dataset = ClickbaitDataModule(model_name_or_path = config.model_name_or_path,
                                       label_list = config.label_list, batch_size = int(config.batch_size),
                                       data_folder = config.data_dir,
                                       data_column= config.data_column,
                                       label_column= config.label_column,
                                       model_type = model_type,
                                       tokenizer_class = config.tokenizer_class,
                                       ratio = ratio)
    config.num_train_examples = len(main_dataset.train_examples)
    if model_type == 'gan':
      clickbaitmodel = GAN(
          model_name_or_path = config.model_name_or_path,
          num_hidden_layers_g=config.num_hidden_layers_g,
          num_hidden_layers_d=config.num_hidden_layers_d,
          out_dropout_rate = config.out_dropout_rate,
          label_list = config.label_list)
      optimizers = build_gan_optimizer(clickbaitmodel, config)
      train_epoch = gan_train_epoch
      validate_model = gan_validate_model
      save_model = gan_save_model
    elif model_type == 'transformer':
        clickbaitmodel = TransformerForSequenceClassification(model_name_or_path=config.model_name_or_path,
                                                              num_classes=len(config.label_list))
        optimizers = build_transformer_optimizer(clickbaitmodel,config)
        train_epoch = transformer_train_epoch
        validate_model = transformer_validate_model
        save_model = transformer_save_model

    for epoch in tqdm(range(config.epochs)):
        validation_metrics = train_epoch(clickbaitmodel,main_dataset, optimizers,config,epoch+1)
        dev_f1 = validation_metrics['dev/epoch_f1']
        save_best_model = False
        if epoch==0:
          best_dev_f1 = dev_f1
          save_best_model = True

        elif dev_f1>best_dev_f1:
          best_dev_f1 = dev_f1
          save_best_model = True
          best_epoch= epoch

        if save_best_model:
          save_model(os.path.join(config.output_path,f'{model_type}.pt'),clickbaitmodel,optimizers)
          save_best_model = False

    test_metrics = validate_model(clickbaitmodel,main_dataset.test_dataloader(),
                                  config,epoch,'test')
    run.summary['best_dev_f1'] = best_dev_f1
    run.summary['test_f1'] = test_metrics['test/epoch_f1']
    test_f1 = run.summary['test_f1']
    run.summary['best epoch based on dev F1 score'] = best_epoch

    logger.info('Best Epoch based on dev F1 score %s:',best_epoch)
    model_artifact = wandb.Artifact(
          f"{model_type}_{test_f1:.3f}", type=f'{model_type}',
          description=f"{model_type}-test f1 clickbait:{test_f1}",
          metadata=dict(config))
    PATH=os.path.join(config.output_path,f'{model_type}.pt')
    model_artifact.add_file(PATH)
    run.log_artifact(model_artifact)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="inference the model output.")
    parser.add_argument("--config",type=str,required=True,metavar='PATH',help='The model inference config file location')
    args = parser.parse_args()
    main(args.config)


import collections
N, K = map(int, input().split())

def solution(N, K):
    deque = collections.deque([i for i in range(1, N + 1)])
    result = []
    while deque:
        for _ in range(K-1):
            deque.append(deque.popleft())
        result.append(deque.popleft())

    print(f"<{', '.join(map(str,result))}>")

solution(N, K)

백준/Silver/1158. 요세푸스 문제/요세푸스 문제.py


import requests

# -*- coding: utf-8 -*-
import os

from aiohttp import ClientError
from fastapi import FastAPI, File, UploadFile

# image_chatgpt

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

import openai

import dotenv
import os

# ipynb 실행용
# import nbformat
# from nbconvert import PythonExporter
# from nbconvert.preprocessors import ExecutePreprocessor
import boto3

from pydantic import BaseModel

from typing import List, Optional

import requests

import secrets

from pydub import AudioSegment

app = FastAPI()

dotenv.load_dotenv()

OPENAI_YOUR_KEY = os.getenv("CHATGPT_API_KEY")
openai.api_key = OPENAI_YOUR_KEY

client_s3 = boto3.client(
    's3',
    aws_access_key_id=os.getenv("CREDENTIALS_ACCESS_KEY"),
    aws_secret_access_key=os.getenv("CREDENTIALS_SECRET_KEY")
)

class Audio(BaseModel):
    audioName: List[str]

class ImageURLRequest(BaseModel):
    images: List[str]
    voiceUrl: Optional[str] = None

def TTS(voice_id, result):

    CHUNK_SIZE = 1024
    if voice_id:
        elevenlabs_url = "https://api.elevenlabs.io/v1/text-to-speech/" + voice_id
    else:
        elevenlabs_url = "https://api.elevenlabs.io/v1/text-to-speech/1Y8VEQXMwbo4IxJO2JKy"

    headers = {
        "Accept": "audio/mpeg",
        "Content-Type": "application/json",
        "xi-api-key": os.getenv("ELEVENLABS_API_KEY")
    }

    elevenlabs_data = {
        "text": result,
        "model_id": "eleven_multilingual_v2",
        "voice_settings": {
            "stability": 0.6,
            "similarity_boost": 1,
            "style": 0,
            "use_speaker_boost": 'false'
        }
    }

    name = secrets.token_hex(nbytes=16)

    response = requests.post(elevenlabs_url, json=elevenlabs_data, headers=headers)
    with open("./audio/" + name + ".mp3", 'wb') as f:
        for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
            if chunk:
                f.write(chunk)
    return name

def add(id, file):
    url = "https://api.elevenlabs.io/v1/voices/add"

    headers = {
    "Accept": "application/json",
    "xi-api-key": os.getenv("ELEVENLABS_API_KEY")
    }

    data = {
        'name': id
    }
    files = {
        ('files', (file, open(file, 'rb'), 'audio/mpeg'))
    }
    response = requests.post(url, headers=headers, data=data, files=files)
    return response

def delete(voice_id):

    headers = {
    "Accept": "application/json",
    "xi-api-key": os.getenv("ELEVENLABS_API_KEY")
        }
    url = "https://api.elevenlabs.io/v1/voices/" + voice_id
    response = requests.delete(url, headers=headers)
    return response

@app.post("/ai/text")
def ipynb(imageRequest: ImageURLRequest):
    print("요청 들어옴")
    fileName = []

    PAT = os.getenv("CLARIFAI_API_KEY")
    USER_ID = 'clarifai'
    APP_ID = 'main'
    MODEL_ID = 'general-image-recognition'
    MODEL_VERSION_ID = 'aa7f35c01e0642fda5cf400f543e7c40'

    print(imageRequest.images)

    result = ""
    # 매개변수 전달
    for image in imageRequest.images:

        IMAGE_URL = image

        message = ""

        channel = ClarifaiChannel.get_grpc_channel()
        stub = service_pb2_grpc.V2Stub(channel)

        metadata = (('authorization', 'Key ' + PAT),)

        userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

        post_model_outputs_response = stub.PostModelOutputs(
            service_pb2.PostModelOutputsRequest(
                user_app_id=userDataObject,
                model_id=MODEL_ID,
                version_id=MODEL_VERSION_ID,
                inputs=[
                    resources_pb2.Input(
                        data=resources_pb2.Data(
                            image=resources_pb2.Image(
                                url=IMAGE_URL
                            )
                        )
                    )
                ]
            ),
            metadata=metadata
        )

        if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
            print(post_model_outputs_response.status)
            raise Exception("Post model outputs failed, status: " + post_model_outputs_response.status.description)

        output = post_model_outputs_response.outputs[0]

        print("Predicted concepts:")
        for concept in output.data.concepts:
            print("%s %.2f" % (concept.name, concept.value))
            message += concept.name + " " + str(concept.value) + "\n"

        MODEL = "gpt-3.5-turbo"
        USER_INPUT_MSG = message

        response = openai.ChatCompletion.create(
            model=MODEL,
            messages=[
                {"role": "system",
                 "content": "존댓말로 작성해. 셰익스피어처럼 표현해줘. 나래이션의 스크립트가 필요해. 그 문단은 이 20개의 단어로 구성되어야 해. 꼭 이 단어를 전부 사용할 필요는 없어. 감성적인 표현이 주가 되어야 해. 한국어로 작성하도록 해."},
                {"role": "user", "content": USER_INPUT_MSG},
                {"role": "user", "content": "300글자 이상 500글자 이하가 되도록 문단을 작성해."},
                {"role": "assistant", "content": "네. 글자수는 300글자 이상 500글자 이하가 되도록 문단을 작성하도록 하겠습니다."},
                {"role": "user", "content": "시스템 메시지를 다시 언급하지 마. 반드시 존댓말로 작성하도록 해."},
                {"role": "assistant", "content": "반드시 존댓말로 작성해 드릴게요."},
                # {"role": "assistant", "content": "Who's there?"},
            ],
            temperature=0.7,
            max_tokens=1000  # 최대 토큰 수를 설정하여 답변의 길이를 제어합니다.
        )

        # response['choices'][0]['message']['content']

        result += f"{response['choices'][0]['message']['content']} "

    print(result)

    voiceUrl = imageRequest.voiceUrl

    # 저장할 로컬 폴더 경로
    output_folder = "./voice"

    # 폴더가 없다면 생성
    os.makedirs(output_folder, exist_ok=True)

    input_voice = None

    # URL에서 음성 파일 다운로드
    if voiceUrl != None :
        response = requests.get(voiceUrl)

        if response.status_code == 200:
            # 파일 이름 추출 (URL에서 마지막 슬래시 뒤의 부분을 사용)
            file_name = voiceUrl.split("/")[-1]

            # 로컬에 파일 저장
            with open(os.path.join(output_folder, file_name), "wb") as file:
                file.write(response.content)
            print(f"{file_name}을(를) 다운로드하여 {output_folder}에 저장했습니다.")

            # input_voice 변수에 저장한 음성 파일의 경로를 할당
            input_voice = os.path.join(output_folder, file_name)
            print(f"input_voice 변수에 음성 파일 경로를 할당했습니다: {input_voice}")
        else:
            print("파일을 다운로드할 수 없습니다. 상태 코드:", response.status_code)

    new_voice_id = ""
    if input_voice:
        new_voice_id = add("custom", input_voice).json()["voice_id"]
    print(new_voice_id)
    name = TTS(f"{new_voice_id}", result)
    if input_voice:
        delete(f"{new_voice_id}")

    makeBackGroundMusic(name)
    fileName.append(name + ".mp3")

    return {"audios": saveAudioAtS3(fileName)}

def saveAudioAtS3(audio):
    audioUrl = []

    try:
        for a in audio:
            client_s3.upload_file(
                "./audio/" + a,
                os.getenv("S3_BUCKET"),
                "audio/" + a,
                ExtraArgs={'ContentType': 'audio/mp3'}
            )

            # 로컬에 저장되어있는 음성파일 삭제
            os.remove("./audio/" + a)

            audioUrl.append(os.getenv("S3_URL") + "/" + a)

        return audioUrl
    except ClientError as e:
        print(f'Credential error => {e}')
    except Exception as e:
        print(f"Another error => {e}")

def makeBackGroundMusic(name):
    # 배경 음악 파일 로드
    background_music = AudioSegment.from_mp3("./audio/back.mp3")

    # 원본 mp3 파일 로드
    original_audio = AudioSegment.from_mp3("./audio/" + name + ".mp3") - 5

    # 로컬에 있는 mp3 파일 삭제
    os.remove("./audio/" + name + ".mp3")

    # 배경 음악과 원본 오디오 합치기
    output_audio = background_music.overlay(original_audio, position=10000)

    # 결과를 새 파일로 저장
    output_audio.export("./audio/" + name + ".mp3", format="mp3")

    print("배경 음악이 추가된 오디오를 저장했습니다.")