#Author = FIKRI HADI NUGRAHA
# Python DataBase Sistem CRUD

from flask import Flask, render_template, \
  request, redirect, url_for
import mysql.connector

application = Flask(__name__)

conn = cursor = None
#fungsi koneksi database
def openDb():
   global conn, cursor
   conn = mysql.connector.connect(
    host ='localhost',
    user ='root',
    password = '',
    database ='db_mahasiswa'
)
   cursor = conn.cursor()

#fungsi untuk menutup koneksi
def closeDb():
   global conn, cursor
   cursor.close()
   conn.close()

@application.route('/show')
def show():
   return render_template('show.html')

#fungsi view index() untuk menampilkan data dari database
@application.route('/view')
def index():
   openDb()
   container = []
   sql = "SELECT * FROM datamahasiswa"
   cursor.execute(sql)
   results = cursor.fetchall()
   for data in results:
      container.append(data)
   closeDb()
   return render_template('index.html', container=container,)

#fungsi view tambah() untuk membuat form tambah
@application.route('/tambah', methods=['GET','POST'])
def tambah():
   if request.method == 'POST':
      NIM = request.form['nim']
      nama = request.form['nama']
      Fakultas = request.form['fakultas']
      Prodi = request.form['prodi']
      PHONE = request.form['phone']
      Email = request.form['email']
      Alamat = request.form['alamat']
      openDb()
      sql = "INSERT INTO datamahasiswa (nim,nama_mahasiswa,fakultas,prodi,phone,email,alamat) VALUES (%s, %s, %s,%s,%s,%s,%s)"
      val = (NIM,nama,Fakultas,Prodi,PHONE,Email,Alamat)
      cursor.execute(sql, val)
      conn.commit()
      closeDb()
      return redirect(url_for('index'))
   else:
      return render_template('tambah.html')

#fungsi view edit() untuk form edit
@application.route('/edit/<id_mahasiswa>', methods=['GET','POST'])
def edit(id_mahasiswa):
   openDb()
   cursor.execute("SELECT * FROM datamahasiswa WHERE id_mahasiswa=%s", (id_mahasiswa,))
   data = cursor.fetchone()
   if request.method == 'POST':
      id_mahasiswa = request.form['id_mahasiswa']
      NIM = request.form['nim']
      nama = request.form['nama']
      Fakultas = request.form['fakultas']
      Prodi = request.form['prodi']
      PHONE = request.form['phone']
      Email = request.form['email']
      Alamat = request.form['alamat']
      sql = "UPDATE datamahasiswa SET nim=%s,nama_mahasiswa=%s,fakultas=%s,prodi=%s,phone=%s, email=%s, alamat=%s WHERE id_mahasiswa=%s"
      val = (NIM,nama, Fakultas,Prodi,PHONE,Email,Alamat, id_mahasiswa)
      cursor.execute(sql, val)
      conn.commit()
      closeDb()
      return redirect(url_for('index'))
   else:
      closeDb()
      return render_template('edit.html', data=data)
#fungsi untuk menghapus data
@application.route('/hapus/<id_mahasiswa>', methods=['GET','POST'])
def hapus(id_mahasiswa):
   openDb()
   cursor.execute('DELETE FROM datamahasiswa WHERE id_mahasiswa=%s', (id_mahasiswa,))
   conn.commit()
   closeDb()
   return redirect(url_for('index'))

if __name__ == '__main__':
   application.run(debug=True)

Python - DB/DB_MAHASISWA PYTHON/DB_MAHASISWA/mahasiswa2/aplikasi_.py


from flask import Flask, render_template
from AboutApp import app

AUTHOR = "Patrick Tyler Haas"
SITEURL = "literallyjustgraphs.com"
SITENAME = "Patrick Tyler Haas"

@app.route("/")
def about_me():
    attr_dict = {
        "AUTHOR": AUTHOR,
        "SITEURL": SITEURL,
        "title": "PTH — Portfolio",
        "description": "My personal portfolio and developer's blog"
    }

    projects = [
        {"project_title": "Lorem Ipsum",
         "project_desc": "Donec finibus rutrum massa vitae rutrum. Nulla facilisi.",
         "project_link": "#"},
         {"project_title": "Lorem Ipsum",
         "project_desc": "Suspendisse sit amet orci eu est imperdiet rutrum nec a erat.",
         "project_link": "#"},
         {"project_title": "Lorem Ipsum",
         "project_desc": "Duis sed quam vestibulum tortor semper suscipit non ut sapien. Integer ac lacus sit amet diam dictum ornare.",
         "project_link": "#"}
    ]

    return render_template("about.html", projects=projects, **attr_dict)

@app.route("/contact")
def contact():

    attr_dict = {
        "AUTHOR": AUTHOR,
        "SITEURL": SITEURL,
        "title": "PTH — Contact",
        "description": "My personal portfolio and developer's blog"
    }

    return render_template("contact.html", **attr_dict)


#! /usr/bin/python3

# 782. Transform to Chessboard
# Hard
#
# An N x N board contains only 0s and 1s. In each move, you can swap any 2 rows with each other, or any 2 columns with each other.
#
# What is the minimum number of moves to transform the board into a "chessboard" - a board where no 0s and no 1s are 4-directionally adjacent? If the task is impossible, return -1.
#
# Examples:
# Input: board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]
# Output: 2
# Explanation:
# One potential sequence of moves is shown below, from left to right:
#
# 0110     1010     1010
# 0110 --> 1010 --> 0101
# 1001     0101     1010
# 1001     0101     0101
#
# The first move swaps the first and second column.
# The second move swaps the second and third row.
#
#
# Input: board = [[0, 1], [1, 0]]
# Output: 0
# Explanation:
# Also note that the board with 0 in the top left corner,
# 01
# 10
#
# is also a valid chessboard.
#
# Input: board = [[1, 0], [1, 0]]
# Output: -1
# Explanation:
# No matter what sequence of moves you make, you cannot end with a valid chessboard.
#
# Note:
#
#     board will have the same number of rows and columns, a number in the range [2, 30].
#     board[i][j] will be only 0s or 1s.

from typing import List

class Solution:
    def movesToChessboard(self, board: List[List[int]]) -> int:
        row0 = board[0]
        chess_size = len(row0)
        if not self.check_init_cond(board, row0):
            #print("Init cond failed for board %s" % str(board))
            return -1

        # The board is movable
        first_is_one = self.check_row_1first(row0)
        #print("Swap columns for board %s -- first_is_one is %s" % (board, first_is_one))
        swap_count = 0
        expected_cell = 1 if first_is_one else 0
        for idx in range(len(row0)-1):
            cell = row0[idx]
            if cell != expected_cell:
                # find the swap
                swapped = False
                swap_count += 1
                #print("Looking for a column to swap index %d" % (idx))
                for jdx in range(idx+1, chess_size, 2):
                    if row0[jdx] == expected_cell:
                        swapped = True
                        #print("Found the column %d to swap with %d" % (jdx, idx))
                        self.swap_columns(board, idx, jdx)
                        row0 = board[0]
                        break
                assert(swapped)
            expected_cell = 1 if (expected_cell == 0) else 0

        first_is_one = self.check_column_1first(board)
        #print("Swap rows for board %s -- first_is_one is %s" % (board, first_is_one))
        expected_cell = 1 if first_is_one else 0
        for idx in range(len(board)-1):
            row = board[idx]
            if row[0] != expected_cell:
                # find the swap
                swapped = False
                swap_count += 1
                #print("Looking for a row to swap index %d" % (idx))
                for jdx in range(idx+1, chess_size, 2):
                    if board[jdx][0] == expected_cell:
                        swapped = True
                        #print("Found the row %d to swap with %d" % (jdx, idx))
                        self.swap_rows(board, idx, jdx)
                        break
                assert(swapped)
            expected_cell = 1 if (expected_cell == 0) else 0
        #print("%d swaps for %s" % (swap_count, board))
        return swap_count

    def swap_rows(self, board, index0, index1):
        if index0 == index1: assert(False)
        if index0 >= index1:
            index0, index1 = index1, index0
        l1 = board.pop(index1)
        l0 = board.pop(index0)
        board.insert(index0, l1)
        board.insert(index1, l0)

    def swap_columns(self, board, index0, index1):
        if index0 == index1: assert(False)
        for row in board:
            tmp = row[index0]
            row[index0] = row[index1]
            row[index1] = tmp

    def check_init_cond(self, board, row0):
        diff = 1
        ones = 0
        #print("check ones")
        for cell in row0:
            if cell == 1: ones += 1
        #print("Found %d ones" % ones)
        if (len(row0) % 2) == 0:
            if ones != len(row0)/2: return False
        elif (ones != int(len(row0)/2)) and (ones != int((len(row0)/2) + 1)): return False

        #print("check diff")
        for row in board[1:]:
            same_or_invert = self.is_same_or_invert_row(row0, row)
            if same_or_invert == 0: return False
            diff += same_or_invert
        #print(diff)
        if diff != -1 and diff != 0 and diff != 1: return False
        return True

    def is_same_or_invert_row(self, ref, row):
        if ref[0] == row[0]:
            for idx, e in enumerate(row[1:]):
                if e != ref[idx+1]: return 0
        else:
            for idx, e in enumerate(row[1:]):
                if e == ref[idx+1]: return 0
        return 1 if ref[0] == row[0] else -1

    def check_row_1first(self, row):
        # Find the right columns order, check 1s first
        ones_placed = 0
        ones = 0
        for idx in range(len(row)):
            if row[idx] == 1:
                ones += 1
                if idx % 2 == 0:
                    ones_placed += 1
        if ones < (len(row) / 2):
            return False
        if ones > (len(row) / 2):
            return True
        if ones_placed > (ones / 2):
            return True
        elif ones_placed == (ones / 2):
            return (row[0] == 1)
        return False

    def check_column_1first(self, board):
        # Find the right rows order, check 1s first
        ones_placed = 0
        ones = 0
        for idx in range(len(board)):
            if board[idx][0] == 1:
                ones += 1
                if idx % 2 == 0:
                    ones_placed += 1
        # if there are less 1s than 0s, it's 0s first
        if ones < (len(board) / 2):
            return False
        if ones > (len(board) / 2):
            return True
        if ones_placed > (ones / 2):
            return True
        elif ones_placed == (ones / 2):
            return (board[idx][0] == 1)
        return False

def check_solution():
    s = Solution()

    board = [[0, 1], [1, 0]]
    result = s.movesToChessboard(board)
    assert(result == 0)

    board = [[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0]]
    result = s.movesToChessboard(board)
    assert(result == 0)

    board = [[0, 1, 0, 1], [1, 0, 1, 0], [1, 0, 1, 0], [1, 0, 1, 0]]
    result = s.movesToChessboard(board)
    assert(result == -1)

    board = [[1, 1, 0, 1], [1, 0, 1, 0], [1, 0, 1, 0], [1, 0, 1, 0]]
    result = s.movesToChessboard(board)
    assert(result == -1)

    board = [[1, 0], [1, 0]]
    result = s.movesToChessboard(board)
    assert(result == -1)

    board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]
    result = s.movesToChessboard(board)
    assert(result == 2)

    board = [   [1,0,0,1,0,0,1,1],
                [0,1,1,0,1,1,0,0],
                [0,1,1,0,1,1,0,0],
                [1,0,0,1,0,0,1,1],
                [1,0,0,1,0,0,1,1],
                [0,1,1,0,1,1,0,0],
                [1,0,0,1,0,0,1,1],
                [0,1,1,0,1,1,0,0]]
    result = s.movesToChessboard(board)
    assert(result == 3)

    board = [   [0,1,1,0,1,1,0,0],
                [0,1,1,0,1,1,0,0],
                [1,0,0,1,0,0,1,1],
                [1,0,0,1,0,0,1,1],
                [1,0,0,1,0,0,1,1],
                [0,1,1,0,1,1,0,0],
                [1,0,0,1,0,0,1,1],
                [0,1,1,0,1,1,0,0]]
    result = s.movesToChessboard(board)
    assert(result == 3)

    board = [   [1,1,0],
                [0,0,1],
                [0,0,1]]
    result = s.movesToChessboard(board)
    assert(result == 2)

    board = [   [1,0,0],
                [0,1,1],
                [1,0,0]]
    result = s.movesToChessboard(board)
    assert(result == 1)

    board = [   [0,0,0,0,1,1,0,1,0,1,1],
                [1,1,1,1,0,0,1,0,1,0,0],
                [1,1,1,1,0,0,1,0,1,0,0],
                [0,0,0,0,1,1,0,1,0,1,1],
                [0,0,0,0,1,1,0,1,0,1,1],
                [1,1,1,1,0,0,1,0,1,0,0],
                [1,1,1,1,0,0,1,0,1,0,0],
                [1,1,1,1,0,0,1,0,1,0,0],
                [0,0,0,0,1,1,0,1,0,1,1],
                [1,1,1,1,0,0,1,0,1,0,0],
                [0,0,0,0,1,1,0,1,0,1,1]]
    result = s.movesToChessboard(board)
    assert(result == 6)

    board = [   [0,1,1,0,1,1,0,1,0,1,0,1,0],
                [1,0,0,1,0,0,1,0,1,0,1,0,1],
                [1,0,0,1,0,0,1,0,1,0,1,0,1],
                [1,0,0,1,0,0,1,0,1,0,1,0,1],
                [1,0,0,1,0,0,1,0,1,0,1,0,1],
                [0,1,1,0,1,1,0,1,0,1,0,1,0],
                [0,1,1,0,1,1,0,1,0,1,0,1,0],
                [0,1,1,0,1,1,0,1,0,1,0,1,0],
                [0,1,1,0,1,1,0,1,0,1,0,1,0],
                [1,0,0,1,0,0,1,0,1,0,1,0,1],
                [0,1,1,0,1,1,0,1,0,1,0,1,0],
                [1,0,0,1,0,0,1,0,1,0,1,0,1],
                [1,0,0,1,0,0,1,0,1,0,1,0,1]]
    result = s.movesToChessboard(board)
    assert(result == 9)

    #print("All tests passed successfully!!")

check_solution()


from django.urls import path

from apps.office import views
from apps.office.autocomplete_views import WorkerAutocomplete

urlpatterns = [
    path('', views.home, name='home'),
    path('null', views.null, name='null'),
    path('worker-autocomplete', WorkerAutocomplete.as_view(), name='worker-autocomplete'),
    path('workers/<int:pk>', views.WorkerDetailView.as_view(), name='worker_details'),
    path('dashboard/reminders', views.ReminderDashboardView.as_view(), name='reminders_dashboard'),
    path('reminders/mark_done/<int:pk>', views.reminder_mark_done, name='reminders_mark_done'),
    path('reminders/delete/<int:pk>', views.reminder_delete, name='reminders_delete'),
    path('reminders/<int:page>', views.ReminderListView.as_view(), name='reminders'),
    path('reminders/new', views.ReminderCreateModalView.as_view(), name='reminder_new_modal'),
]


from onnxruntime import InferenceSession
from transformers import DistilBertTokenizerFast
from tqdm import tqdm
import pandas as pd
from datasets import load_dataset

def load_tokenizer():
    """
    Loads the german distilbert tokenizer and extends it with emoji tokens
    """
    tokenizer = DistilBertTokenizerFast.from_pretrained('distilbert-base-german-cased')
    with open('additional_tokens', 'r') as fin:
        emojis = fin.read().split('\n')
        tokenizer.add_tokens(emojis)
    return tokenizer

def load_data():
    """
    Loads the validation split of the german amazon reviews dataset
    """
    dataset = load_dataset("amazon_reviews_multi", "de", split="validation")
    data = dataset.to_pandas()
    data['label'] = None
    data.loc[data.stars > 3, 'label'] = 'positive'
    data.loc[data.stars == 3, 'label'] = 'neutral'
    data.loc[data.stars < 3, 'label'] = 'negative'
    data = data[['label', 'review_body']]
    data = data.rename({'review_body': 'input'}, axis=1)
    return data

def calculate_metric_for_label(label):
    """
    Calculate and print precision, recall and f1 for the given label
    """
    ground_truth = data[data.label == label]
    predicted = data[data.prediction == label]
    correct = ground_truth[ground_truth.prediction == label]
    precision = len(correct) / (len(predicted) + 1e-10)
    recall = len(correct) / (len(ground_truth) + 1e-10)
    f1 = 2 * precision * recall / (precision + recall + 1e-10)
    print('Metrics for label:', label)
    print('\tPrecision:', precision)
    print('\tRecall:', recall)
    print('\tF1:', f1)

tokenizer = load_tokenizer()
sentiment = ['positive', 'negative', 'neutral']
session = InferenceSession('models/distilbert.quant.onnx')
data = load_data()

# Run inference
predictions = []
for label, text in tqdm(data.iloc, total=len(data)):
    inp = dict(tokenizer(text, return_tensors='np'))
    prediction = session.run(None, inp)[0]
    predicted_sentiment = sentiment[prediction.argmax(-1)[0]]
    predictions.append(predicted_sentiment)

# save result
data['prediction'] = predictions
data.to_csv('output.csv')

# calculate metrics
accuracy = sum(data.label == data.prediction) / len(data)
print('Accuray:', accuracy)

# label wise metrics
for label in sentiment:
    calculate_metric_for_label(label)


import matplotlib.pyplot as plt
import numpy as np
from Imports import data_list as DL, image_matrix as Mat

def show_dem_change_plot(change_mat, cell_size, date_range):
    change_list = Mat.mat_to_list(change_mat)
    deposit, erosion, no_change = DL.segment_change_list(change_list)
    deposit = pixels_to_meters(deposit, cell_size)
    erosion = pixels_to_meters(erosion, cell_size)
    obs = range(1, len(change_list) + 1)
    plt.tight_layout()
    plt.plot(obs, deposit, label="Deposit")
    plt.plot(obs, erosion, label="Erosion")
    plt.plot(obs, no_change, "k-", label="No Change")
    plt.xlabel("Pixels")
    plt.ylabel("Values (meters)")
    plt.title("Soil Erosion vs Deposit (" + str(date_range[0]) + "-" + str(date_range[1]) + ")")
    plt.legend()
    plt.show()

def show_yearwise_plot(rainfall_dict, runoff_dict, year_list):
    l=len(year_list)
    if l<1 or l>4:
        raise ValueError("Invalid year list length")
    for index,year in enumerate(year_list):
        rainfall = rainfall_dict[year]
        runoff = runoff_dict[year]
        day=range(1, len(runoff)+1)
        plt.subplot(2,2, index+1)
        plt.tight_layout()
        plt.plot(day, rainfall, "c-", label="Rainfall")
        plt.plot(day, runoff, "r-", label="Runoff")
        plt.xlabel("Day")
        plt.ylabel("Rainfall, Runoff (mm)")
        plt.title("\nRainfall vs Runoff " + str(year))
        plt.legend()
    plt.show()

    for index,year in enumerate(year_list):
        rainfall = rainfall_dict[year]
        runoff = runoff_dict[year]
        deg = 3
        z=np.polyfit(rainfall, runoff, deg)
        f = np.poly1d(z)
        new_x = np.linspace(0, max(rainfall), len(rainfall))
        new_y = f(new_x)
        poly = get_poly(z, deg, 5)
        r = round(np.corrcoef(new_x, new_y)[0, 1]**2, 3)
        plt.tight_layout()
        plt.subplot(2, 2, index + 1)
        plt.plot(new_x, new_y, "r-", label="y = " + poly + "\nR^2 = " + str(r))
        plt.plot(rainfall, runoff, "bo")
        plt.xlabel("Rainfall (mm)")
        plt.ylabel("Runoff (mm)")
        plt.title("\nRainfall-Runoff Regression Fit " + str(year))
        plt.legend()
    plt.show()

def show_full_plot(rainfall_dict, runoff_dict, date_range):
    total_rainfall = DL.new_list(list(rainfall_dict.values()))
    total_runoff = DL.new_list(list(runoff_dict.values()))
    day = range(1, len(total_rainfall) + 1)
    plt.plot(day, total_rainfall, "c-", label="Rainfall")
    plt.plot(day, total_runoff, "r-", label="Runoff")
    plt.xlabel("Day")
    plt.ylabel("Rainfall, Runoff (mm)")
    plt.title("Total Rainfall vs Runoff (" + str(date_range[0]) + "-" + str(date_range[1]) + ")")
    plt.legend()
    plt.show()

    deg = 3
    z=np.polyfit(total_rainfall, total_runoff, deg)
    f = np.poly1d(z)
    new_x = np.linspace(0, max(total_rainfall), len(total_rainfall))
    new_y = f(new_x)
    poly = get_poly(z, deg, 5)
    r = round(np.corrcoef(new_x, new_y)[0, 1]**2, 3)
    plt.plot(new_x, new_y, "r-", label="y = " + poly + "\nR^2 = " + str(r))
    plt.plot(total_rainfall, total_runoff, "bo")
    plt.xlabel("Rainfall (mm)")
    plt.ylabel("Runoff (mm)")
    plt.title("Rainfall-Runoff Regression Fit (" + str(date_range[0]) + "-" + str(date_range[1]) + ")")
    plt.legend()
    plt.show()

def get_poly(z, deg, round_factor):
    poly = str(round(z[0],round_factor))+ "x^"+str(deg)
    for coeff in z[1:]:
        c = round(coeff,round_factor)
        if c > 0:
            c = "+"+ str(c)
        deg -= 1
        if deg > 1:
            poly += str(c) + "x^"+str(deg)
        elif deg == 1:
            poly += str(c) + "x"
        else:
            poly += str(c)
    return poly

def pixels_to_meters(mat, cell_size):
    return Mat.mat_to_list(np.matrix(mat)*111.5*cell_size*1000)

def get_date_range(rainfall_dict):
    years = rainfall_dict.keys()
    return min(years), max(years)

def generate_results(weather_file, runoff_file, input_dem, eroded_dem, year_list, cell_size, output_dir):
    rainfall_dict = DL.read_daily_data(weather_file)
    runoff_dict = DL.read_daily_data(runoff_file)
    original_dem_mat = DL.read_dem_data(input_dem)
    eroded_dem_mat = DL.read_dem_data(eroded_dem)
    minyr, maxyr = get_date_range(rainfall_dict)
    show_full_plot(rainfall_dict, runoff_dict, (minyr, maxyr))
    show_yearwise_plot(rainfall_dict, runoff_dict, year_list)
    change_mat = Mat.get_change_mat(original_dem_mat, eroded_dem_mat)
    show_dem_change_plot(change_mat, cell_size, (minyr, maxyr))
    Mat.generate_change_raster(change_mat).save(output_dir + "/simulated_dem_change.png")


from bs4 import BeautifulSoup as soup
import urllib.request
import requests
import pandas as pd
import datetime

months = {
    "Jan": 1,
    "Feb": 2,
    "Mar": 3,
    "Apr": 4,
    "May": 5,
    "Jun": 6,
    "Jul": 7,
    "Aug": 8,
    "Sep": 9,
    "Oct": 10,
    "Nov": 11,
    "Dec": 12
}

# dataframe
df = pd.read_excel('./s_p.xlsx')
df['earning_flag'] = 0
all_dates = {}

def getData(ticker):
    url = 'https://finance.yahoo.com/calendar/earnings?day=2019-06-13&symbol={}&offset=0&size=100'.format(
        ticker)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
    response = requests.get(url, headers=headers)
    print(response)
    html = response.text

    page_soup = soup(html, 'lxml')
    table = page_soup.find_all('td')
    dates = []
    for something in table:
        try:
            if something['aria-label'] == "Earnings Date":
                date = something.text.split(',')
                # print(date)
                temp = date[0].split()
                # print(temp)
                month, day, year = months[temp[0]], temp[1], date[1].strip()
                # print(month, day, year)
                if int(year) >= 2017:
                    dates.append("{}/{}/{}".format(month, day, year))
        except:
            print('')

    all_dates[ticker] = set(dates)

# tickers = "AAPL,ABBV,ABT,ACN,AGN,AIG,ALL,AMGN,AMZN,AXP,BA,BAC,BIIB,BK,BLK,BMY,C,CAT,CELG,CL,CMCSA,COF,COP,COST,CSCO,CVS,CVX,DD,DHR,DIS,DOW,DUK,EMC,EMR,EXC,F,FB,FDX,FOX,FOXA,GD,GE,GILD,GM,GOOG,GOOGL,GS,HAL,HD,HON,IBM,INTC,JNJ,JPM,KMI,KO,LLY,LMT,LOW,MA,MCD,MDLZ,MDT,MET,MMM,MO,MON,MRK,MS,MSFT,NEE,NKE,ORCL,OXY,PCLN,PEP,PFE,PG,PM,PYPL,QCOM,RTN,SBUX,SLB,SO,SPG,T,TGT,TWX,TXN,UNH,UNP,UPS,USB,USD,UTX,V,VZ,WBA,WFC".split(
#     ',')

tickers = ["MSFT"]
for c in tickers:
    getData(c)

count = 0
for i in df.iterrows():

    date = i[1][1]
    tick = i[1][2]
    date = date.strftime("%-m/%-d/%Y")
    # print(all_dates[tick])
    # print(date)
    if tick in all_dates:
        if date in all_dates[tick]:
            df.at[i[0], 'earning_flag'] = 1

    count += 1
    # break
df.to_excel('output.xlsx')
# print(df)
print(all_dates)


from flask import Flask, request, jsonify, send_file
import pickle
from gtts import gTTS

from models.openai_api import OpenAIChat
from models.get_contents import GetContents
from models.make_input import make_input

app = Flask(__name__)
openai_chatbot = OpenAIChat()
get_contents = GetContents()

@app.route("/", methods=["get"])
def home():
    return 'hello!'

@app.route("/answer",methods=['POST'])
def test():
    input_json = request.json
    input_ = make_input(input_json["id"], input_json["content"])

    result = openai_chatbot.get_answer(input_)
    print("sumery >> " + result)

    return jsonify({"items": [{"id": input_json["id"], "content": result}]})

@app.route("/send_sound",methods=['POST'])
def send_sound():
    input_json = request.json
    print(input_json)
    input_ = make_input(input_json["id"], input_json["content"])

    result = openai_chatbot.get_answer(input_)
    sumery = '. '.join(result[:90].split('. ')[:-1])
    print("sumery >> " + sumery)

    tts = gTTS(text=sumery, lang='ko')
    filename='resource/temp.mp3'
    tts.save(filename)

    content =   {"items": [{"id": input_json["id"], "content": input_json["content"]}]}

    return send_file(filename, mimetype='audio/mp3')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)


# saat ini saya mencoba mengakses data tentang anime menggunakan Web API
# documentation available on https://github.com/abhinavk99/jikanpy
from typing import IO
import requests
import json
import pymysql
import numpy as np
import pandas as pd
import pandas_datareader as web
import matplotlib.pyplot as plt
import datetime as dt

class AnimeList:
    users_vote = 0

    def __init__(self, title: str, anime_rating: float, airing: str, studio: str):
        self.title = title
        self.rating = anime_rating
        self.airing = airing
        self.studio = studio

    @property
    def anime_title(self):
        pass

    @anime_title.getter
    def anime_title(self):
        return self.title

    @anime_title.setter
    def anime_title(self, new_title):
        self.title = new_title

    def display(self):
        print(f"Title : {self.title}, Score : {self.rating}, Airing : {self.airing}")

    def average_score(self, users_vote):
        vote_count = 0
        if users_vote != 0:
            vote_count = users_vote * self.rating / 100
        return vote_count

anime_url = r"https://api.jikan.moe/v4/anime?q=inuyasha&sfw"  # response 200 / complete
anime_urls = r"https://api.jikan.moe/v4/anime/20/full"  # response 200 / complete

# request method
anime = requests.get(anime_urls)
print(anime.content)
print(json.dump(anime, fp=IO['None']))

# database connection
connect = pymysql.Connect(host="localhost", user="root", database="saham_indonesia", port=8080)
cursor = connect.cursor()

connect.commit()
connect.close()

# initialize timeset
start_time = dt.datetime(2022, 7, 1)
end_time = dt.datetime.now()

# dataframe
stock_data = web.DataReader(["AAPL", "TSLA"], "yahoo", start_time, end_time)
stock_data = pd.DataFrame(stock_data)


import scrapy
from bookSpider.items import BookspiderItem
import copy

class bookSpider(scrapy.Spider):
    name = "bookSpider"
    allowed_domains = ['book.douban.com']
    start_urls = ["https://book.douban.com/latest?subcat=%E5%85%A8%E9%83%A8&p={}".format(i) for i in range(1,11,1)]
    # start_urls = ["https://book.douban.com/latest?p=1"]

    def parse(self, response):
        if response.status == 200 and len(response.text) > 10:
            # 使用xpath解析网页，获取所需元素
            titles = response.xpath('//a[@class="fleft"]/text()').extract()
            urls = response.xpath('//a[@class="fleft"]/@href').extract()
            for i in range(0, len(titles)):
                item = BookspiderItem()
                url = urls[i]
                title = titles[i]
                # 测试输出
                # print(url, title)
                # 将解析到的 内容详情页url 拿去 获取页面中的内容
                item["book_name"] = title
                yield scrapy.Request(url=url, callback=self.html, meta={'item': copy.deepcopy(item)})
            """
                获取url详情页中的数据
            """

    # 处理字段空值
    def try_except(self, response, xpath_str):
        try:
            field = response.xpath(xpath_str).extract_first()
        except:
            field = '无'
        if field==None:
            field = "无"
        return field

    def html(self, response):
        item = response.meta['item']
        # 判断请求的链接返回的状态码和文本内容长度
        if response.status == 200 and len(response.text) > 10:
            info = response.xpath('//div[@id="info"]')
            # 使用xpath解析内容详情页，获取所需元素
            item["book_author"] = self.try_except(response,'//div[@id="info"]/span[1]/a/text()')
            item["origin_name"] = self.try_except(response,u'//span[contains(./text(), "原作名:")]/following::text()[1]')
            item["publish_date"] = self.try_except(response,u'//span[contains(./text(), "出版年:")]/following::text()[1]')
            item["page_count"] = self.try_except(response,u'//span[contains(./text(), "页数:")]/following::text()[1]')
            item["book_price"] = self.try_except(response,u'//span[contains(./text(), "定价:")]/following::text()[1]')
            item["wrapping"] = self.try_except(response,u'//span[contains(./text(), "装帧:")]/following::text()[1]')
            item["isbn"] = self.try_except(response,u'//span[contains(./text(), "ISBN:")]/following::text()[1]')
            # 测试输出
            # print(info,origin_name)
            yield item


import azure.functions as func
from bson.json_util import dumps

import unit_of_work

def main(req: func.HttpRequest) -> func.HttpResponse:
    # example call http://localhost:7071/api/getAdvertisement/?id=5ec34b2265403b17d00ae864
    id = req.params.get("id")

    if id:
        try:
            uow = unit_of_work.MongoUnitOfWork()
            collection_name = "advertisements"
            result = uow.get_one(collection_name, id)

            print("----------result--------")
            result = dumps(result)
            print(result)

            return func.HttpResponse(
                result, mimetype="application/json", charset="utf-8", status_code=200
            )
        except:
            return func.HttpResponse("Database connection error.", status_code=500)

    else:
        return func.HttpResponse(
            "Please pass an id parameter in the query string.", status_code=400
        )


import telebot
from queue_bot_token import bot_token
from telebot import types

bot = telebot.TeleBot(bot_token)

@bot.message_handler(content_types=["text"])
def main_display(message):
    if message.text != "/start" and message.text != "/help":
        initials = ""
        bot.send_message(message.chat.id, "Введите ФИО")
        initials = message.text
        bot.send_message(message.chat.id, "Введите команду /start.")

def make_the_queue(message):
    lable = ""
    bot.send_message(message.from_user.id, 'Отправь короткую ссылку для создания очереди.')
    lable = message.text
    # отправили вопрос "Отправь короткую ссылку"
    # (((желательно дисциплина и группа будет содержаться в имени ссылки)))
    # link = input()
    # создается файл БД с этим именем
    # join_to_queue_display(link)

@bot.message_handler(commands=["start"])
def welcome_start():
    @bot.message_handler(content_types=["text"])
    def get_button(message):
        bot.send_message(message.chat.id, "Варианты действий")
        keyboard = types.InlineKeyboardMarkup()
        set_queue_button = types.InlineKeyboardButton(text="Создать очередь", callback_data="set")
        keyboard.add(set_queue_button)
        enter_queue = types.InlineKeyboardButton(text="Вступить в очередь", callback_data="enter")
        keyboard.add(enter_queue)
        bot.send_message(message.from_user.id, "Нажмити на кнопку:", reply_markup=keyboard)

        # @bot.callback_query_handler(func=lambda call: True)
        # def callback_worker(call):
        #     if call.data == "set":
        #         bot.send_message(call.message.chat.id, "set_work")
        #     if call.data == "enter":
        #         bot.send_message(call.message.chat.id, "enter_work")

@bot.message_handler(commands=["help"])
def welcome_help(message):
    bot.send_message(message.chat.id, 'Список команд:/help, /start')

# link = то, что выбрал юзер
# def join_to_queue_display(link):
#     после выбора идет подтверждение записи и откроется очередь с уже
#     записавшимися людьми
#     live_queue(link)
#         if (кнопка == "Текущая очередь"):
#              live_queue(link)
#     elif (кнопка == "Вступить/Покинуть"): # Если вступлен, то "Выйти из очереди" всесто вступить
#         if ("Вступить"):
#             #внести человека в очередь и отобразить новую очередь
#         if ("Покинуть"):
#             #удаление человека из очереди с текущим telegram ID
#
#         live_queue(link)
#         join_to_queue_display(link)
#     elif (кнопка == "Закрыть"):
#         после нажатия которой возвращает на начальный экран
#         set_main_display()
#
# def live_queue(link):
#     отображает текущий список очереди с ключом link
#
# def set_main_display():
#     показать на главный экран
#
#  send /start
# def set_main_display():
#     должна быть какая-то авторизация (попросить ввести фамилию
#     и, если бот не умеет брать telegram ID, то спросить и его)
#     if (кнопка == "Вступить в очередь"):
#         join_to_queue_display(link)
#     elif (кнопка == "Создать очередь"):
#         link = make_the_queue()
#
bot.polling(none_stop=True, interval=0)


import datetime

def get_current_week():
    monday, sunday = datetime.date.today(), datetime.date.today()
    one_day = datetime.timedelta(days=1)
    while monday.weekday() != 0:
        monday -= one_day
    while sunday.weekday() != 6:
        sunday += one_day
    # 返回当前的星期一和星期天的日期
    return monday, sunday

print(get_current_week())


# encoding: utf-8
"""
@author:  liaoxingyu
@contact: sherlockliao01@gmail.com
"""

import torch
import torch.nn.functional as F
from torch import nn

from fastreid.config import configurable
from fastreid.layers import *
from fastreid.layers import pooling, any_softmax
from fastreid.layers.weight_init import weights_init_kaiming
from .build import REID_HEADS_REGISTRY
from fastreid.utils import comm

@REID_HEADS_REGISTRY.register()
class EmbeddingHead(nn.Module):
    """
    EmbeddingHead perform all feature aggregation in an embedding task, such as reid, image retrieval
    and face recognition

    It typically contains logic to

    1. feature aggregation via global average pooling and generalized mean pooling
    2. (optional) batchnorm, dimension reduction and etc.
    2. (in training only) margin-based softmax logits computation
    """

    @configurable
    def __init__(
            self,
            *,
            feat_dim,
            embedding_dim,
            num_classes,
            neck_feat,
            pool_type,
            cls_type,
            scale,
            margin,
            with_bnneck,
            norm_type,
            cls_enable,
            loss_names,
            img_per_batch
    ):
        """
        NOTE: this interface is experimental.

        Args:
            feat_dim:
            embedding_dim:
            num_classes:
            neck_feat:
            pool_type:
            cls_type:
            scale:
            margin:
            with_bnneck:
            norm_type:

            cls_enable:
        """
        super().__init__()

        # Pooling layer
        assert hasattr(pooling, pool_type), "Expected pool types are {}, " \
                                            "but got {}".format(pooling.__all__, pool_type)
        self.pool_layer = getattr(pooling, pool_type)()

        self.neck_feat = neck_feat

        neck = []
        if embedding_dim > 0:
            neck.append(nn.Conv2d(feat_dim, embedding_dim, 1, 1, bias=False))
            feat_dim = embedding_dim

        if with_bnneck:
            neck.append(get_norm(norm_type, feat_dim, bias_freeze=True))

        self.bottleneck = nn.Sequential(*neck)

        # Classification head

        self.cls_enable = cls_enable

        self.cls_type = cls_type

        if self.cls_enable:
            assert hasattr(any_softmax, cls_type), "Expected cls types are {}, " \
                                               "but got {}".format(any_softmax.__all__, cls_type)

            if self.cls_type == 'ProxyLinear':
                self.weight = nn.Parameter(F.normalize(torch.randn(num_classes, feat_dim), dim=-1))
            else:
                self.weight = nn.Parameter(torch.Tensor(num_classes, feat_dim))

            self.cls_layer = getattr(any_softmax, cls_type)(num_classes, scale, margin)

        self.reset_parameters()
        self.img_per_batch = img_per_batch

    def reset_parameters(self) -> None:
        self.bottleneck.apply(weights_init_kaiming)
        if self.cls_enable and self.cls_type != "ProxyLinear":
            nn.init.normal_(self.weight, std=0.01)

    @classmethod
    def from_config(cls, cfg):
        # fmt: off
        feat_dim      = cfg.MODEL.BACKBONE.FEAT_DIM
        embedding_dim = cfg.MODEL.HEADS.EMBEDDING_DIM
        num_classes   = cfg.MODEL.HEADS.NUM_CLASSES
        neck_feat     = cfg.MODEL.HEADS.NECK_FEAT
        pool_type     = cfg.MODEL.HEADS.POOL_LAYER
        cls_type      = cfg.MODEL.HEADS.CLS_LAYER
        scale         = cfg.MODEL.HEADS.SCALE
        margin        = cfg.MODEL.HEADS.MARGIN
        with_bnneck   = cfg.MODEL.HEADS.WITH_BNNECK
        norm_type     = cfg.MODEL.HEADS.NORM
        loss_names    = cfg.MODEL.LOSSES.NAME
        img_per_batch    = cfg.SOLVER.IMS_PER_BATCH

        cls_enable = cfg.MODEL.HEADS.CLS_ENABLE
        # fmt: on
        return {
            'feat_dim': feat_dim,
            'embedding_dim': embedding_dim,
            'num_classes': num_classes,
            'neck_feat': neck_feat,
            'pool_type': pool_type,
            'cls_type': cls_type,
            'scale': scale,
            'margin': margin,
            'with_bnneck': with_bnneck,
            'norm_type': norm_type,
            'cls_enable': cls_enable,
            'loss_names': loss_names,
            'img_per_batch': img_per_batch
        }

    def forward(self, backbone_outputs, targets=None):
        """
        See :class:`ReIDHeads.forward`.
        """
        if isinstance(backbone_outputs, dict):
            features = backbone_outputs['features']
            targets = backbone_outputs['targets']
        else:
            features = backbone_outputs

        #pool_feat = self.neck_bn(self.pool_layer(features))
        #neck_feat = self.bottleneck(pool_feat.view(pool_feat.shape[0],-1))
        pool_feat = self.pool_layer(features)
        neck_feat = self.bottleneck(pool_feat)
        neck_feat = neck_feat[..., 0, 0]

        # Evaluation
        # fmt: off
        if not self.training: return neck_feat
        # fmt: on

        # Training

        # fmt: off
        if self.neck_feat == 'before':  feat = pool_feat[..., 0, 0]
        elif self.neck_feat == 'after': feat = neck_feat
        else:                           raise KeyError(f"{self.neck_feat} is invalid for MODEL.HEADS.NECK_FEAT")
        # fmt: on

        # Pass logits.clone() into cls_layer, because there is in-place operations
        if self.cls_enable:
            if self.cls_layer.__class__.__name__ == 'Linear':
                logits = F.linear(neck_feat, self.weight)
            else:
                logits = F.linear(F.normalize(neck_feat), F.normalize(self.weight))

            cls_outputs = self.cls_layer(logits.clone(), targets)
            pred_class_logits = logits.mul(self.cls_layer.s)

            return {
                "cls_outputs": cls_outputs,
                "pred_class_logits": pred_class_logits,
                "features": feat,
                "weight": self.weight}

        else:
            #print('only one output!!!!!!!!')
            return {"features": feat,}


import numpy as np
import pandas as pd
import soundfile as sf
import torch
from torch import hub
from torch.utils.data import Dataset, DataLoader
import random as random
import os
import shutil
import zipfile

from .wham_dataset import wham_noise_license

MINI_URL = "https://zenodo.org/record/3871592/files/MiniLibriMix.zip?download=1"

class LibriMix(Dataset):
    """Dataset class for LibriMix source separation tasks.

    Args:
        csv_dir (str): The path to the metadata file.
        task (str): One of ``'enh_single'``, ``'enh_both'``, ``'sep_clean'`` or
            ``'sep_noisy'`` :

            * ``'enh_single'`` for single speaker speech enhancement.
            * ``'enh_both'`` for multi speaker speech enhancement.
            * ``'sep_clean'`` for two-speaker clean source separation.
            * ``'sep_noisy'`` for two-speaker noisy source separation.

        sample_rate (int) : The sample rate of the sources and mixtures.
        n_src (int) : The number of sources in the mixture.
        segment (int, optional) : The desired sources and mixtures length in s.

    References
        [1] "LibriMix: An Open-Source Dataset for Generalizable Speech Separation",
        Cosentino et al. 2020.
    """

    dataset_name = "LibriMix"

    def __init__(
        self, csv_dir, task="sep_clean", sample_rate=16000, n_src=2, segment=3, return_id=False
    ):
        self.csv_dir = csv_dir
        self.task = task
        self.return_id = return_id
        # Get the csv corresponding to the task
        if task == "enh_single":
            md_file = [f for f in os.listdir(csv_dir) if "single" in f][0]
            self.csv_path = os.path.join(self.csv_dir, md_file)
        elif task == "enh_both":
            md_file = [f for f in os.listdir(csv_dir) if "both" in f][0]
            self.csv_path = os.path.join(self.csv_dir, md_file)
            md_clean_file = [f for f in os.listdir(csv_dir) if "clean" in f][0]
            self.df_clean = pd.read_csv(os.path.join(csv_dir, md_clean_file))
        elif task == "sep_clean":
            md_file = [f for f in os.listdir(csv_dir) if "clean" in f][0]
            self.csv_path = os.path.join(self.csv_dir, md_file)
        elif task == "sep_noisy":
            md_file = [f for f in os.listdir(csv_dir) if "both" in f][0]
            self.csv_path = os.path.join(self.csv_dir, md_file)
        self.segment = segment
        self.sample_rate = sample_rate
        # Open csv file
        self.df = pd.read_csv(self.csv_path)
        # Get rid of the utterances too short
        if self.segment is not None:
            max_len = len(self.df)
            self.seg_len = int(self.segment * self.sample_rate)
            # Ignore the file shorter than the desired_length
            self.df = self.df[self.df["length"] >= self.seg_len]
            print(
                f"Drop {max_len - len(self.df)} utterances from {max_len} "
                f"(shorter than {segment} seconds)"
            )
        else:
            self.seg_len = None
        self.n_src = n_src

    def __len__(self):
        return len(self.df)

    def __getitem__(self, idx):
        # Get the row in dataframe
        row = self.df.iloc[idx]
        # Get mixture path
        mixture_path = row["mixture_path"]
        self.mixture_path = mixture_path
        sources_list = []
        # If there is a seg start point is set randomly
        if self.seg_len is not None:
            start = random.randint(0, row["length"] - self.seg_len)
            stop = start + self.seg_len
        else:
            start = 0
            stop = None
        # If task is enh_both then the source is the clean mixture
        if "enh_both" in self.task:
            mix_clean_path = self.df_clean.iloc[idx]["mixture_path"]
            s, _ = sf.read(mix_clean_path, dtype="float32", start=start, stop=stop)
            sources_list.append(s)

        else:
            # Read sources
            for i in range(self.n_src):
                source_path = row[f"source_{i + 1}_path"]
                s, _ = sf.read(source_path, dtype="float32", start=start, stop=stop)
                sources_list.append(s)
        # Read the mixture
        mixture, _ = sf.read(mixture_path, dtype="float32", start=start, stop=stop)
        # Convert to torch tensor
        mixture = torch.from_numpy(mixture)
        # Stack sources
        sources = np.vstack(sources_list)
        # Convert sources to tensor
        sources = torch.from_numpy(sources)
        if not self.return_id:
            return mixture, sources
        # 5400-34479-0005_4973-24515-0007.wav
        id1, id2 = mixture_path.split("/")[-1].split(".")[0].split("_")
        return mixture, sources, [id1, id2]

    @classmethod
    def loaders_from_mini(cls, batch_size=4, **kwargs):
        """Downloads MiniLibriMix and returns train and validation DataLoader.

        Args:
            batch_size (int): Batch size of the Dataloader. Only DataLoader param.
                To have more control on Dataloader, call `mini_from_download` and
                instantiate the DatalLoader.
            **kwargs: keyword arguments to pass the `LibriMix`, see `__init__`.
                The kwargs will be fed to both the training set and validation
                set.

        Returns:
            train_loader, val_loader: training and validation DataLoader out of
            `LibriMix` Dataset.

        Examples
            >>> from asteroid.data import LibriMix
            >>> train_loader, val_loader = LibriMix.loaders_from_mini(
            >>>     task='sep_clean', batch_size=4
            >>> )
        """
        train_set, val_set = cls.mini_from_download(**kwargs)
        train_loader = DataLoader(train_set, batch_size=batch_size, drop_last=True)
        val_loader = DataLoader(val_set, batch_size=batch_size, drop_last=True)
        return train_loader, val_loader

    @classmethod
    def mini_from_download(cls, **kwargs):
        """Downloads MiniLibriMix and returns train and validation Dataset.
        If you want to instantiate the Dataset by yourself, call
        `mini_download` that returns the path to the path to the metadata files.

        Args:
            **kwargs: keyword arguments to pass the `LibriMix`, see `__init__`.
                The kwargs will be fed to both the training set and validation
                set

        Returns:
            train_set, val_set: training and validation instances of
            `LibriMix` (data.Dataset).

        Examples
            >>> from asteroid.data import LibriMix
            >>> train_set, val_set = LibriMix.mini_from_download(task='sep_clean')
        """
        # kwargs checks
        assert "csv_dir" not in kwargs, "Cannot specify csv_dir when downloading."
        assert kwargs.get("task", "sep_clean") in [
            "sep_clean",
            "sep_noisy",
        ], "Only clean and noisy separation are supported in MiniLibriMix."
        assert (
            kwargs.get("sample_rate", 8000) == 8000
        ), "Only 8kHz sample rate is supported in MiniLibriMix."
        # Download LibriMix in current directory
        meta_path = cls.mini_download()
        # Create dataset instances
        train_set = cls(os.path.join(meta_path, "train"), sample_rate=8000, **kwargs)
        val_set = cls(os.path.join(meta_path, "val"), sample_rate=8000, **kwargs)
        return train_set, val_set

    @staticmethod
    def mini_download():
        """Downloads MiniLibriMix from Zenodo in current directory

        Returns:
            The path to the metadata directory.
        """
        mini_dir = "./MiniLibriMix/"
        os.makedirs(mini_dir, exist_ok=True)
        # Download zip (or cached)
        zip_path = mini_dir + "MiniLibriMix.zip"
        if not os.path.isfile(zip_path):
            hub.download_url_to_file(MINI_URL, zip_path)
        # Unzip zip
        cond = all([os.path.isdir("MiniLibriMix/" + f) for f in ["train", "val", "metadata"]])
        if not cond:
            with zipfile.ZipFile(zip_path, "r") as zip_ref:
                zip_ref.extractall("./")  # Will unzip in MiniLibriMix
        # Reorder metadata
        src = "MiniLibriMix/metadata/"
        for mode in ["train", "val"]:
            dst = f"MiniLibriMix/metadata/{mode}/"
            os.makedirs(dst, exist_ok=True)
            [
                shutil.copyfile(src + f, dst + f)
                for f in os.listdir(src)
                if mode in f and os.path.isfile(src + f)
            ]
        return "./MiniLibriMix/metadata"

    def get_infos(self):
        """Get dataset infos (for publishing models).

        Returns:
            dict, dataset infos with keys `dataset`, `task` and `licences`.
        """
        infos = dict()
        infos["dataset"] = self._dataset_name()
        infos["task"] = self.task
        if self.task == "sep_clean":
            data_license = [librispeech_license]
        else:
            data_license = [librispeech_license, wham_noise_license]
        infos["licenses"] = data_license
        return infos

    def _dataset_name(self):
        """Differentiate between 2 and 3 sources."""
        return f"Libri{self.n_src}Mix"

librispeech_license = dict(
    title="LibriSpeech ASR corpus",
    title_link="http://www.openslr.org/12",
    author="Vassil Panayotov",
    author_link="https://github.com/vdp",
    license="CC BY 4.0",
    license_link="https://creativecommons.org/licenses/by/4.0/",
    non_commercial=False,
)


'''

Problem Statement: Median of Two Sorted Arrays

Link: https://leetcode.com/problems/median-of-two-sorted-arrays
Tags: ['Array', 'Binary Search', 'Divide and Conquer']
Difficulty: Hard

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n)).

Example 1:
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.

Example 2:
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

Example 3:
Input: nums1 = [0,0], nums2 = [0,0]
Output: 0.00000

Example 4:
Input: nums1 = [], nums2 = [1]
Output: 1.00000

Example 5:
Input: nums1 = [2], nums2 = []
Output: 2.00000

Constraints:

nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 106

'''

'''
Solution:

nums1 and nums2 are the two list and are already sorted.
The brute force approach of finding the median can be to merge both the lists and sort it and then find the median. [Time complexity: O((m + n)Log(m + n)]
Another approach can be to merge the sorted lists using pointer approach and then find the median. [Time complexity: O(m + n)]

Here is an approach which can be used to reduce the time complexity extensively. [Time complexity: O(log(min(m, n)))]

Q: What is the approach ?
A: Since the lists are already sorted and our objective is to find the median (the middle element / value of a sorted list),
    we basically need to find an element which will divide the sorted elements after merging the lists into two equal parts.

The following solution tries to solve the problem in different levels, starting from basic then adapting to multiple conditions that can arise.

Level 1:
    The total length of list elements combined is odd, so we can get the exact element and no requirement to average two elements.
    Even though we need to search the element and since the elements are already sorted we will use binary search but will consider that in
    the first iteration of search the element will be found.

Level 2:
    The element is not found in the first iteration of binary search

Level 2:
    If binary search is unable to find the element, can happen since we will try to run
    binary search on the list with less elements to reduce time complexity.

Level 3:
    Even number of elements after combining the lists, so it will require find two elements and then providing the average of those as median.

################################################################### LEVEL 1 ######################################################################

Lets take an example and understand.

nums1 = [2, 4, 9]
nums2 = [1, 2, 3, 5, 6, 7]
m = len(nums1) = 3
n = len(nums2) = 6
total number of elements = 9
then for the median there must be 4 items which will be smaller or equal
and there must be 4 items which will be larger or equal

In this case if we see [1, 2, 2, 3,  || 4 ||   5, 6, 7, 9]

Since it is faster to search in the smaller list (nums1 in this case), we will start by finding the element in nums1

[2, 4, 9]

When binary search starts we get [ start = 0, end = m - 1 = 2, midIndexOfNums1 = (start + end) // 2 = 1]
median position index for merged lists (midPoint) = (sum of length of lists) // 2 = 9 // 2 = 4
then the midIndexOfNums2 = midPoint - midIndexOfNums1 - 1 = 4 - 1 - 1 = 2

Condition for confirming the median:
"If element at midIndexOfNums1 in nums1 is the median
then all the elements from beginning till midIndexOfNums2 in nums2 must be smaller or equal to nums1[midIndexOfNums1]
and all the elements after midIndexOfNums2 in nums2 must be greater or equal to nums1[midIndexOfNums1],
since elements are already sorted checking only
((nums1[midIndexOfNums1] >= nums2[midIndexOfNums2]) and (nums1[midIndexOfNums1] <= nums2[midIndexOfNums2 + 1]))
satisfies our condition"

In the above example we can see that 4 becomes the median, but the above conditions worked only because the median element existed in nums1.
what if nums1 does not have the median element but we still want to run the binary search on nums1 since it has less elements than nums2,
to handle this case we need to check the above conditions one more time just by reversing the nums1 and nums2

Lets take another example :

nums1 = [2, 3, 9]
nums2 = [1, 2, 4, 5, 6, 7]

When binary search starts we get [ start = 0, end = m - 1 = 2, midIndexOfNums1 = (start + end) // 2 = 1]
median position index for merged lists (midPoint) = (sum of length of lists) // 2 = 9 // 2 = 4
then the midIndexOfNums2 = midPoint - midIndexOfNums1 - 1 = 4 - 1 - 1 = 2

Updated conditions:
1) nums1[midIndexOfNums1] becomes the median if
((nums1[midIndexOfNums1] >= nums2[midIndexOfNums2]) and (nums1[midIndexOfNums1] <= nums2[midIndexOfNums2 + 1])) satisfies

OR

2) nums2[midIndexOfNums2] becomes the median if
((nums2[midIndexOfNums2] >= nums1[midIndexOfNums1]) and (nums2[midIndexOfNums2] <= nums1[midIndexOfNums1 + 1])) satisfies

################################################################### LEVEL 2 ######################################################################

So far we are good with above examples and conditions since we got the median element in the first iteration of binary search,
what if we do not get out median element in the first iteration and we need to update the "start" or "end" values of binary search,
upon which circumstances shall we update "start" or "end" ?

3) if both the above condition fails and (nums1[midIndexOfNums1] < nums2[midIndexOfNums2])
then we need to search for elements greater than nums1[midIndexOfNums1] so we should update
start = midIndexOfNums1 + 1

4) if all the above conditions fails then we need know nums1[midIndexOfNums1] is larger
then we need to search for elements lesser than nums1[midIndexOfNums1] so we should update
end = midIndexOfNums1

So far the conditions set is
[
    1) nums1[midIndexOfNums1] becomes the median if
    ((nums1[midIndexOfNums1] >= nums2[midIndexOfNums2]) and (nums1[midIndexOfNums1] <= nums2[midIndexOfNums2 + 1])) satisfies

    2) nums2[midIndexOfNums2] becomes the median if
    ((nums2[midIndexOfNums2] >= nums1[midIndexOfNums1]) and (nums2[midIndexOfNums2] <= nums1[midIndexOfNums1 + 1])) satisfies

    3) if both the above condition fails and (nums1[midIndexOfNums1] < nums2[midIndexOfNums2])
    then we need to search for elements greater than nums1[midIndexOfNums1] so we should update
    start = midIndexOfNums1 + 1

    4) if all the above conditions fails then we need know nums1[midIndexOfNums1] is larger
    then we need to search for elements lesser than nums1[midIndexOfNums1] so we should update
    end = midIndexOfNums1
]

################################################################### LEVEL 3 ######################################################################

The above conditions works well but what if we are unable to find any element which satisfies to become the median even after the binary search ends.
If such a condition arises then we are sure that the median element is not present in nums1 list and there will be no element from nums1 list which
can lie in the range [nums2[0], nums2[medianElementPosition]].

So what we can do is concat nums1 and nums2 and then find the median based on index.
But how do we concat the lists, what will be the order, should we do (nums1 + nums2) or (nums2 + nums1).

1) If nums1[m - 1] < nums2[0] then
we should concat in the following order (nums1 + nums2)

2) Else for any other conditions we should concat in the following order (nums2 + nums1)
Here we are not limiting the condition to (nums2[n - 1] < nums1[0]) instead using an else condition
because it is not necessarily true that (nums2[n - 1] < nums1[0]) but instead (nums2[medianElementPosition] < nums1[0]) is true.
Since we do not care about what is present after medianElementPosition in nums2 we can safely concat in the following order (nums2 + nums1)
and find the median element position.

################################################################### LEVEL 4 ######################################################################

Till now everything will work but will fail at one point when the total number of elements (m + n) is even.
For example:
In this list [1, 2, 3, 4] the median is (2 + 3) / 2 = 2.5

We have not handled this case till now.
This case can be easily handled when we do no find the median element using binary search and
concat in either of the ways (nums1 + nums2) or (nums2 + nums1), we just need to check if the total size
is even or not and act accordingly.

But how do we do it when we find a median element using binary search, here also we get 2 more choices.

1) If the median element is from nums1 list
    then median can be
    ((nums1[midIndexOfNums1] + nums1[midIndexOfNums1 - 1]) / 2)
    or
    ((nums1[midIndexOfNums1] + nums2[midIndexOfNums2]) / 2)

    #Note: If midIndexOfNums1 == 0 then (midIndexOfNums1 - 1) is underflow, so in this case
    ((nums1[midIndexOfNums1] + nums2[midIndexOfNums2]) / 2) will be the median.

    1.1) ((nums1[midIndexOfNums1] + nums1[midIndexOfNums1 - 1]) / 2) is median when
            midIndexOfNums1 > 0
            and
            nums1[midIndexOfNums1 - 1] > nums2[midIndexOfNums2]

            # To handle the underflow condition
            # nums1[midIndexOfNums1 - 1] and nums2[midIndexOfNums2] both are less than equal to nums1[midIndexOfNums1],
            # so the greater one should be used in calculating the median

    1.2) If the above condition does not satisfy then the median is ((nums1[midIndexOfNums1] + nums2[midIndexOfNums2]) / 2)

2) If the median element is from nums2 list
    then median can be
    ((nums2[midIndexOfNums2] + nums2[midIndexOfNums2 - 1]) / 2)
    or
    ((nums2[midIndexOfNums2] + nums1[midIndexOfNums1]) / 2)

    #Note: If midIndexOfNums2 == 0 then (midIndexOfNums2 - 1) is underflow, so in this case
    ((nums2[midIndexOfNums2] + nums1[midIndexOfNums1]) / 2) will be the median.

    1.1) ((nums2[midIndexOfNums2] + nums2[midIndexOfNums2 - 1]) / 2) is median when
            midIndexOfNums2 > 0
            and
            nums2[midIndexOfNums2 - 1] > nums1[midIndexOfNums1]

            # To handle the underflow condition
            # nums2[midIndexOfNums2 - 1] and nums1[midIndexOfNums1] both are less than equal to nums2[midIndexOfNums2],
            # so the greater one should be used in calculating the median

    1.2) If the above condition does not satisfy then the median is ((nums2[midIndexOfNums2] + nums1[midIndexOfNums1]) / 2)

'''

from typing import List

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:

        # Median function for a single sorted array
        def findMedian(arr):
            lenOfArr = len(arr)
            if lenOfArr == 0:
                return 0
            if lenOfArr % 2 == 0:
                return (arr[lenOfArr // 2 - 1] + arr[lenOfArr // 2]) / 2
            return arr[lenOfArr // 2]

        # Storing the number of items present in nums1 and nums2
        m = len(nums1)
        n = len(nums2)

        # The idea is to store the smaller array in nums1
        if m > n:
            nums1, nums2 = nums2, nums1
            m, n = n, m

        # Initializing the variables for binary search operation on the smaller array
        start = 0
        midPoint = (m + n) // 2
        end = min(m - 1, midPoint) # Handles the out of index condition

        # Variables that will be used to store the median index either from nums1 or nums2
        requiredIndexOfM = -1
        requiredIndexOfN = -1

        # If any one of the list is empty then we can simply find the median directly from the non empty list since it is already sorted
        if (m == 0 or n == 0):
            return findMedian(nums1 + nums2)

        # Binary search initiated to find the median element
        while start <= end:
            mid = (start + end) // 2 # index in nums1

            # index in nums2
            # when the required median is found then the elements till midOfN position should ideally be present on the left side
            # of mid element in nums1 list
            midOfN = midPoint - mid - 1

            if (midOfN == n - 1 or nums1[mid] <= nums2[midOfN + 1]) and (nums1[mid] >= nums2[midOfN]):
                requiredIndexOfM = mid
                break
            elif (mid == m - 1 or nums2[midOfN] <= nums1[mid + 1]) and (nums2[midOfN] >= nums1[mid]):
                requiredIndexOfN = midOfN
                break
            elif ((nums1[mid] < nums2[midOfN - 1] or midOfN == 0) or
                 (nums1[mid] < nums2[midOfN])):
                start = mid + 1
            else:
                end = mid - 1

        if requiredIndexOfM > -1:
            if ((m + n) % 2 == 0):
                if requiredIndexOfM > 0 and nums1[requiredIndexOfM - 1] > nums2[midPoint - requiredIndexOfM - 1]:
                    return (nums1[requiredIndexOfM] + nums1[requiredIndexOfM - 1]) / 2
                else:
                    return (nums1[requiredIndexOfM] + nums2[midPoint - requiredIndexOfM - 1]) / 2
            else:
                return nums1[requiredIndexOfM]
        elif requiredIndexOfN > -1:
            if ((m + n) % 2 == 0):
                if requiredIndexOfN > 0 and nums2[requiredIndexOfN - 1] > nums1[midPoint - requiredIndexOfN - 1]:
                    return (nums2[requiredIndexOfN] + nums2[requiredIndexOfN - 1]) / 2
                else:
                    return (nums2[requiredIndexOfN] + nums1[midPoint - requiredIndexOfN - 1]) / 2
            else:
                return nums2[requiredIndexOfN]
        elif nums1[m - 1] <= nums2[0]:
            return findMedian(nums1 + nums2)
        else:
            return findMedian(nums2 + nums1)


from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any

from packaging.utils import canonicalize_name
from poetry.core.utils.helpers import module_name
from tomlkit import inline_table
from tomlkit import loads
from tomlkit import table
from tomlkit.toml_document import TOMLDocument

from poetry.pyproject.toml import PyProjectTOML

if TYPE_CHECKING:
    from collections.abc import Mapping

    from tomlkit.items import InlineTable

POETRY_DEFAULT = """\
[tool.poetry]
name = ""
version = ""
description = ""
authors = []
license = ""
readme = ""
packages = []

[tool.poetry.dependencies]

[tool.poetry.group.dev.dependencies]
"""

BUILD_SYSTEM_MIN_VERSION: str | None = None
BUILD_SYSTEM_MAX_VERSION: str | None = None

class Layout:
    def __init__(
        self,
        project: str,
        version: str = "0.1.0",
        description: str = "",
        readme_format: str = "md",
        author: str | None = None,
        license: str | None = None,
        python: str = "*",
        dependencies: Mapping[str, str | Mapping[str, Any]] | None = None,
        dev_dependencies: Mapping[str, str | Mapping[str, Any]] | None = None,
    ) -> None:
        self._project = canonicalize_name(project)
        self._package_path_relative = Path(
            *(module_name(part) for part in project.split("."))
        )
        self._package_name = ".".join(self._package_path_relative.parts)
        self._version = version
        self._description = description

        self._readme_format = readme_format.lower()

        self._license = license
        self._python = python
        self._dependencies = dependencies or {}
        self._dev_dependencies = dev_dependencies or {}

        if not author:
            author = "Your Name <you@example.com>"

        self._author = author

    @property
    def basedir(self) -> Path:
        return Path()

    @property
    def package_path(self) -> Path:
        return self.basedir / self._package_path_relative

    def get_package_include(self) -> InlineTable | None:
        package = inline_table()

        # If a project is created in the root directory (this is reasonable inside a
        # docker container, eg <https://github.com/python-poetry/poetry/issues/5103>)
        # then parts will be empty.
        parts = self._package_path_relative.parts
        if not parts:
            return None

        include = parts[0]
        package.append("include", include)

        if self.basedir != Path():
            package.append("from", self.basedir.as_posix())
        else:
            if module_name(self._project) == include:
                # package include and package name are the same,
                # packages table is redundant here.
                return None

        return package

    def create(self, path: Path, with_tests: bool = True) -> None:
        path.mkdir(parents=True, exist_ok=True)

        self._create_default(path)
        self._create_readme(path)

        if with_tests:
            self._create_tests(path)

        self._write_poetry(path)

    def generate_poetry_content(self) -> TOMLDocument:
        template = POETRY_DEFAULT

        content: dict[str, Any] = loads(template)

        poetry_content = content["tool"]["poetry"]
        poetry_content["name"] = self._project
        poetry_content["version"] = self._version
        poetry_content["description"] = self._description
        poetry_content["authors"].append(self._author)

        if self._license:
            poetry_content["license"] = self._license
        else:
            poetry_content.remove("license")

        poetry_content["readme"] = f"README.{self._readme_format}"
        packages = self.get_package_include()
        if packages:
            poetry_content["packages"].append(packages)
        else:
            poetry_content.remove("packages")

        poetry_content["dependencies"]["python"] = self._python

        for dep_name, dep_constraint in self._dependencies.items():
            poetry_content["dependencies"][dep_name] = dep_constraint

        if self._dev_dependencies:
            for dep_name, dep_constraint in self._dev_dependencies.items():
                poetry_content["group"]["dev"]["dependencies"][
                    dep_name
                ] = dep_constraint
        else:
            del poetry_content["group"]

        # Add build system
        build_system = table()
        build_system_version = ""

        if BUILD_SYSTEM_MIN_VERSION is not None:
            build_system_version = ">=" + BUILD_SYSTEM_MIN_VERSION
        if BUILD_SYSTEM_MAX_VERSION is not None:
            if build_system_version:
                build_system_version += ","
            build_system_version += "<" + BUILD_SYSTEM_MAX_VERSION

        build_system.add("requires", ["poetry-core" + build_system_version])
        build_system.add("build-backend", "poetry.core.masonry.api")

        assert isinstance(content, TOMLDocument)
        content.add("build-system", build_system)

        return content

    def _create_default(self, path: Path, src: bool = True) -> None:
        package_path = path / self.package_path
        package_path.mkdir(parents=True)

        package_init = package_path / "__init__.py"
        package_init.touch()

    def _create_readme(self, path: Path) -> Path:
        readme_file = path.joinpath(f"README.{self._readme_format}")
        readme_file.touch()
        return readme_file

    @staticmethod
    def _create_tests(path: Path) -> None:
        tests = path / "tests"
        tests.mkdir()

        tests_init = tests / "__init__.py"
        tests_init.touch(exist_ok=False)

    def _write_poetry(self, path: Path) -> None:
        pyproject = PyProjectTOML(path / "pyproject.toml")
        content = self.generate_poetry_content()
        for section, item in content.items():
            pyproject.data.append(section, item)
        pyproject.save()


from application.schemata.remain_vacation import RemainVacationSchema
from application.models.remain_vacation import RemainVacation
from application.models.used_vacation import UsedVacation
from application.models.user import User
from application import db, api
from flask import Blueprint, Response, request, render_template, make_response, current_app
from flask_restful import Resource
import datetime
import calendar

remain_vacation_bp = Blueprint("remain_vacation", __name__, url_prefix='/users/vacations/')
remain_vacation_schema = RemainVacationSchema()
api = api(remain_vacation_bp)
headers = {'Content-Type': 'text/html'}

class UserVacation(Resource):
    def get(self, id=None):
        data = request.args
        request_user = User.query.filter_by(id=data.get('id')).one_or_none()

        if not request_user.admin:
            if id != data.get('id') or id is None:
                return Response("No Authority", 401, mimetype='application/json')

            target_user = User.query.get(id)
            remain_vacation = RemainVacation.query.filter_by(user=target_user).one_or_none()
            return make_response(render_template('user/specific_user_result.html', title="남은 휴가", result=remain_vacation, request_user=request_user, id=str(request_user.id)), 200, headers)

        elif request_user.admin:
            if id is None:
                remain_vacation = RemainVacation.query.all()
                return make_response(render_template('user/multiple_user_result.html', title="남은 휴가", result=remain_vacation, user=request_user, id=str(request_user.id)), 200, headers)
            else:
                target_user = User.query.get(id)
                remain_vacation = RemainVacation.query.filter_by(user=target_user).one_or_none()
                if id != data.get('id'):
                    target_id = data.get('사용자 번호')
                    result = [remain_vacation]
                    return make_response(render_template('user/select_specific_user.html', title='사용자 정보', result=result, target_id=str(target_id), id=str(request_user.id)), 200, headers)
                else:
                    return make_response(render_template('user/specific_user_result.html', title="남은 휴가", result=remain_vacation, request_user=request_user, id=str(request_user.id)), 200, headers)

    def put(self, id=None):
        if id:
            user = User.query.get(id)
        else:
            user = User.query.get(request.form.get('id'))

        remain_vacation = RemainVacation.query.filter_by(user=user).one()

        year, total, remain = calculate_vacation(user)

        remain_vacation.number_of_years = year
        remain_vacation.total_vacation = total
        remain_vacation.remain_vacation = remain

        db.session.commit()

    def delete(self, id):
        user = User.query.get(id)
        remain_vacation = RemainVacation.query.filter_by(user=user).delete()

        db.session.commit()

        return Response(remain_vacation_schema.dumps(remain_vacation), 200, mimetype='application/json')

api.add_resource(UserVacation, '/remain', '/<string:id>/remain', endpoint='user_vacation')

def calculate_vacation(user):
    user = user
    years = datetime.datetime.today().year - user.entry_date.year

    if years != 0:
        total = 15 + (years // 2)
    else:
        working_day = (datetime.datetime(user.entry_date.year, 12, 31) - user.entry_date).days
        vacation = (15 * (working_day / 365)) if not calendar.isleap(user.entry_date.year) else (15 * ((working_day+1) / 366))
        flag = vacation - int(vacation)
        total = 0

        if 0.0 < flag <= 0.5:
            total = int(vacation) + 0.5
        elif 0.5 < flag < 1.0:
            total = int(vacation) + 1
        elif flag == 0.0:
            total = vacation

    remain = total - len(UsedVacation.query.filter_by(user=user, kind="연차").all())
    remain = remain - (0.5 * len(UsedVacation.query.filter_by(user=user, kind="반차").all()))

    return years, total, remain


#Embedded file name: codereload\xreload.py
"""
Alternative to ``reload()``.

This works by executing the module in a scratch namespace, and then
patching classes, methods and functions in place.  This avoids the
need to patch instances.  New objects are copied into the target
namespace.

Some of the many limitiations include:

- Global mutable objects other than classes are simply replaced, not patched
- Code using metaclasses is not handled correctly
- Code creating global singletons is not handled correctly
- Functions and methods using decorators (other than classmethod and
  staticmethod) is not handled correctly
- Renamings are not handled correctly
- Dependent modules are not reloaded
- When a dependent module contains ``from foo import bar``, and
  reloading foo deletes foo.bar, the dependent module continues to use
  the old ``foo.bar`` object rather than failing
- Frozen modules and modules loaded from zip files aren't handled
  correctly
- Classes involving ``__slots__`` are not handled correctly
"""
import gc
import imp
import logging
import marshal
import sys
import types
log = logging.getLogger(__name__)

def _expressyourself(obj):
    try:
        return obj.__class__
    except AttributeError:
        return type(obj)

def _safestr(obj):
    """Returns a str or repr for object.
    If they fail, return a string indicating the failure.
    Because we are reloading while code may be in the middle of editing,
    we need to be forgiving.
    """
    try:
        return str(obj)
    except Exception:
        pass

    try:
        return repr(obj)
    except Exception as ex:
        expressedobj = _expressyourself(obj)
        log.warn('Error converting to str. Type: %s Error: %r', expressedobj, ex)
        return '<REPR ERROR> (%s)' % expressedobj

def xreload(mod, code = None):
    """Reload a module in place, updating classes, methods and functions.

    :param mod: a module object
    :return: The (updated) input object itself.
    """
    modns = mod.__dict__
    if not code:
        modname = mod.__name__
        i = modname.rfind('.')
        if i >= 0:
            pkgname, modname = modname[:i], modname[i + 1:]
        else:
            pkgname = None
        if pkgname:
            pkg = sys.modules[pkgname]
            path = pkg.__path__
        else:
            pkg = None
            path = None
        stream, filename, (suffix, mode, kind) = imp.find_module(modname, path)
        try:
            if kind not in (imp.PY_COMPILED, imp.PY_SOURCE):
                return reload(mod)
            if kind == imp.PY_SOURCE:
                source = stream.read() + '\n'
                code = compile(source, filename, 'exec')
            else:
                code = marshal.load(stream)
        finally:
            if stream:
                stream.close()

    tmpns = modns.copy()
    log.debug('Moving over the following objects: %s', tmpns.keys())
    modns.clear()
    preload = ['__name__',
     '__file__',
     '__path__',
     '__package__',
     '__loader__',
     '__doc__']
    for name in preload:
        if name in tmpns:
            modns[name] = tmpns[name]

    exec (code, modns)
    for name, ob in tmpns.items():
        if isinstance(ob, types.ModuleType):
            modns[name] = ob

    oldnames = set(tmpns)
    newnames = set(modns)
    for name in oldnames & newnames:
        log.debug('Assigning over %s: %s', name, _safestr(modns[name])[:160])
        modns[name] = _update(tmpns[name], modns[name])

    log.debug('Module already had: %s', oldnames - newnames)
    log.debug('Module has now: %s', mod.__dict__.keys())
    if hasattr(mod, '__reload_update__'):
        mod.__reload_update__(tmpns)
    return mod

def _update(oldobj, newobj):
    """Update oldobj, if possible in place, with newobj.

    If oldobj is immutable, this simply returns newobj.

    Args:
      oldobj: the object to be updated
      newobj: the object used as the source for the update

    Returns:
      either oldobj, updated in place, or newobj.
    """
    if oldobj is newobj:
        return newobj
    if type(oldobj) is not type(newobj):
        return newobj
    if hasattr(newobj, '__reload_update__'):
        return newobj.__reload_update__(oldobj)
    if isinstance(newobj, types.ClassType):
        return _update_class(oldobj, newobj)
    if isinstance(newobj, types.TypeType):
        return _update_class(oldobj, newobj)
    if isinstance(newobj, types.FunctionType):
        return _update_function(oldobj, newobj)
    if isinstance(newobj, types.MethodType):
        return _update_method(oldobj, newobj)
    if isinstance(newobj, classmethod):
        return _update_classmethod(oldobj, newobj)
    if isinstance(newobj, staticmethod):
        return _update_staticmethod(oldobj, newobj)
    return newobj

def _update_function(oldfunc, newfunc):
    """Update a function object."""
    oldfunc.__doc__ = newfunc.__doc__
    oldfunc.__dict__.update(newfunc.__dict__)
    if oldfunc.func_code != newfunc.func_code:
        oldfunc.func_code = newfunc.func_code
        update_global_references(oldfunc, newfunc)
    log.debug('Updating function, old=%s, new=%s', oldfunc, newfunc)
    return newfunc

def _update_method(oldmeth, newmeth):
    """Update a method object."""
    _update(oldmeth.im_func, newmeth.im_func)
    return oldmeth

def _update_class(oldclass, newclass):
    """Update a class object."""
    olddict = oldclass.__dict__
    newdict = newclass.__dict__
    oldnames = set(olddict)
    newnames = set(newdict)
    for name in newnames - oldnames:
        setattr(oldclass, name, newdict[name])

    for name in oldnames - newnames:
        delattr(oldclass, name)

    for name in oldnames & newnames - set(['__dict__', '__doc__']):
        try:
            setattr(oldclass, name, _update(olddict[name], newdict[name]))
        except AttributeError:
            log.exception('Ignoring exception')

    update_global_references(newclass, oldclass)
    return oldclass

def _update_classmethod(oldcm, newcm):
    """Update a classmethod update."""
    _update(oldcm.__get__(0), newcm.__get__(0))
    return newcm

def _update_staticmethod(oldsm, newsm):
    """Update a staticmethod update."""
    _update(oldsm.__get__(0), newsm.__get__(0))
    return newsm

def update_global_references(oldBaseClass, newBaseClass):
    for ob1 in gc.get_referrers(oldBaseClass):
        if type(ob1) is dict:
            for k, v in ob1.items():
                if v is oldBaseClass:
                    log.debug("Setting '%s' to '%s' in '%s'", k, newBaseClass, ob1.get('__file__'))
                    ob1[k] = newBaseClass


import argparse
import os
import pickle
import random

import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.nn.parallel
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset

from networks.MNIST_networks import (Discriminator_MNIST, Generator_MNIST,
                                     trainGAN)
from utils import make_Imb, new_overlapped_imb_data

# parsers
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', required=True, help='MNIST')
parser.add_argument('--dataroot', required=True, help='path to dataset')
parser.add_argument('--path', required=True, help='path to save pre-trained GAN model')
parser.add_argument('--workers', type=int, help='number of data loading workers', default=0)
parser.add_argument('--batchSize', type=int, default=256, help='input batch size')
parser.add_argument('--imageSize', type=int, default=28, help='the height / width of the input image to network')
parser.add_argument('--nz', type=int, default=128, help='size of the latent z vector')
parser.add_argument('--niter', type=int, default=6000, help='number of epochs to train for')
parser.add_argument('--lr', type=float, default=0.0002, help='learning rate, default=0.0002')
parser.add_argument('--IR', type=float, default=0.01, help='imbalance ratio')
parser.add_argument('--OR', type=str, default='0,1', help='Overlapped classes')
parser.add_argument('--prepretrain', type=bool, default=True, help='If IR is too small,\
                     train GAN with majority classes and fine-tune to the minority classes')
parser.add_argument('--pre_pre_train_path', type=str, default="./ckpts", help='path to save pre-pre-trained GAN')
parser.add_argument('--manualSeed', type=int, default=8888, help='seed')
args = parser.parse_args()

#########################################################################################

if args.manualSeed is None:
    args.manualSeed = random.randint(1, 10000)
print("Random Seed: ", args.manualSeed)
random.seed(args.manualSeed)
torch.manual_seed(args.manualSeed)
torch.cuda.manual_seed_all(args.manualSeed)

cudnn.benchmark = True

FloatTensor = torch.cuda.FloatTensor
LongTensor = torch.cuda.LongTensor
device = 'cuda:0'

if args.path is not None:
    if os.path.exists(args.path) is False:
        os.makedirs(args.path)

if __name__ == '__main__':
    name = f'{args.dataset}_{args.IR}_{args.OR}_{args.manualSeed}'

    # Define the generator and discriminator
    netG = Generator_MNIST(args.nz).to(device)
    optimizerG = optim.Adam(netG.parameters(),lr=args.lr)

    netD = Discriminator_MNIST().to(device)
    optimizerD = optim.Adam(netD.parameters(),lr=args.lr)

    if args.dataset == 'MNIST':
        dataset = dset.MNIST
    else:
        raise NotImplementedError

    imb_dataset, _ = new_overlapped_imb_data(dset.MNIST,args.OR,args.IR,args.batchSize,normalize=True)
    with open(os.path.join(args.path,f'{name}_imb_dataset.pkl'),'wb') as f:
        pickle.dump(imb_dataset,f)

    if args.pre_pre_train_path is not None:
        if os.path.exists(args.pre_pre_train_path) is False:
            os.makedirs(args.pre_pre_train_path)

    if args.prepretrain and args.pre_pre_train_path is not None:
        dataloader = torch.utils.data.DataLoader(imb_dataset, batch_size=args.batchSize,
                                                shuffle=True, num_workers=int(args.workers))

        netG, netD = trainGAN(args, netG, netD, dataloader, optimizerG, optimizerD, args.niter//2+1, True, device)
        torch.save(netG.state_dict(),os.path.join(args.pre_pre_train_path,f'{name}_prepreG.ckpt'))
        torch.save(netD.state_dict(),os.path.join(args.pre_pre_train_path,f'{name}_prepreD.ckpt'))

    if args.prepretrain and args.pre_pre_train_path is not None:
        netG.load_state_dict(torch.load(os.path.join(args.pre_pre_train_path,f'{name}_prepreG.ckpt')))
        netD.load_state_dict(torch.load(os.path.join(args.pre_pre_train_path,f'{name}_prepreD.ckpt')))

        for ct, child in enumerate(netD.children()):
            print(ct,child)
            if ct<2:
                for param in child.parameters():
                    param.requires_grad = False

    min_data = torch.utils.data.Subset(imb_dataset,np.where(imb_dataset.target==1)[0])
    dataloader = torch.utils.data.DataLoader(min_data, batch_size=args.batchSize,
                                            shuffle=True, num_workers=int(args.workers))
    netG, netD = trainGAN(args, netG, netD, dataloader, optimizerG, optimizerD, args.niter, True, device)
    torch.save(netG.state_dict(),os.path.join(args.path,f'{name}_G.ckpt'))
    torch.save(netD.state_dict(),os.path.join(args.path,f'{name}_D.ckpt'))

####


from .expression import execute
from .utils import as_dict, cached_property
from .exceptions import FieldError, SchemaResolverError, ResourceMisconfigured, FieldMisconfigured
from .schemas import ResourceSchema
from .store import Store
from .conf import settings
from .resolver import get_resolver

class Resource(object):
    class Schema(ResourceSchema):
        pass

    def __repr__(self):
        return str(self)

    def __str__(self):
        id = self.get_id()
        return f"{self.__class__.__name__}: {id}"

    def __hash__(self):
        return hash(str(self))

    def __init__(self, **options):
        # initialization is lazy; arguments are saved and nothing else

        # make sure there is a schema and a name
        assert self.Schema.name is not None
        # setup: whether or not this instance has been "setup" (actual initialization)
        self._setup = False
        # options: the initial options
        self._options = options
        # attributes: map of local attributes (using Field class)
        self._attributes = {}
        # fields: map of resource fields (using Field class)
        self._fields = {}

        if self.get_meta_attribute('id') == 'resources':
            space = self.get_option('space')
            if space.name != settings.METASPACE_NAME:
                # normal resources trigger a binding with their space
                # on initialization, do this by calling self.space
                assert self.space is not None

    def __getattr__(self, key):
        if key.startswith("_"):
            return self.__dict__.get(key, None)

        return self.get_attribute(key).get_value()

    def __setattr__(self, key, value):
        if key.startswith("_"):
            return super(Resource, self).__setattr__(key, value)

        field = self.get_attribute(key)
        field.set_value(value)

    def _get_property(self, key):
        """Get attribute (Field) at given key (supporting.nested.paths)

        Raises:
            AttributeError if key is not valid
        """
        if key is None:
            return self

        keys = [k for k in key.split(".") if k] if key else []
        value = self
        last = len(keys)
        if not last:
            this = str(self)
            raise AttributeError(f"{key} is not a valid field of {this}")
        for i, key in enumerate(keys):
            is_last = i == last
            if key:
                field = value.get_attribute(key)
                if not is_last:
                    value = field.get_value()
        return field

    def serialize(self):
        return {
            key: self.get_property(key)
            for key in self.get_attributes()
        }

    def add(self, key, value, index=None):
        return self._get_property(key).add_value(value, index=index)

    def get_property(self, key=None):
        try:
            return self._get_property(key).get_value(resolve=False, id=True)
        except Exception:
            print(f'Failed to get property {key} on {self}')
            raise

    def has_option(self, key):
        return key in self._options

    def get_option(self, key, default=None):
        if key in self._options:
            return self._options[key]
        else:
            if callable(default):
                # callable that takes self
                default = default(self, key=key)
            else:
                # expression that takes self
                default = execute(default, {'fields': self, 'globals': settings})
            return default

    @property
    def pk(self):
        return self.get_id()

    @cached_property
    def fields_by_name(self):
        result = {}
        for record in self.fields:
            result[record.name] = record
        return result

    @cached_property
    def store(self):
        return Store(self)

    @property
    def query(self):
        return self.store.query

    @classmethod
    def get_attributes(cls):
        return cls.Schema.fields

    def get_attribute(self, key):
        from .field import Field
        if key not in self._attributes:
            fields = self.get_attributes()
            if key not in fields:
                raise AttributeError(f"{key} is not a valid property of {self}")

            field = fields[key]
            resource_id = self.get_meta_attribute('id')
            id = f"{resource_id}.{key}"

            self._attributes[key] = Field.make(
                parent=self,
                resource=resource_id,
                id=id,
                name=key,
                **field
            )
        return self._attributes[key]

    def get_field_source_names(self):
        resolver = self.resolver
        return resolver.get_field_source_names(self.source)

    @cached_property
    def resolver(self):
        return get_resolver(self.engine)

    def get_field(self, key):
        from .field import Field
        if key not in self._fields:
            fields = self.get_option('fields')
            if fields == '*':
                field = {}
            else:
                if not fields or key not in fields:
                    raise AttributeError(f"{key} is not a valid field of {self}")
                field = fields[key]

            resource_id = self.get_id()
            id = f"{resource_id}.{key}"
            resolver = self.resolver
            space = None
            if isinstance(field, str) or 'type' not in field:
                # may need self.space to resolve field type
                # for foreign key fields
                space = self.space
            if isinstance(field, dict) and 'source' not in field:
                # add the name as the source
                field['source'] = key

            source = self.get_option('source')
            try:
                field = resolver.get_field_schema(
                    source,
                    field,
                    space=space
                )
            except SchemaResolverError as e:
                exc = str(e)
                raise FieldMisconfigured(f'{id}: {exc}')
            self._fields[key] = Field.make(
                parent=self,
                resource=resource_id,
                id=id,
                name=key,
                **field
            )
        return self._fields[key]

    @classmethod
    def metaresource(cls, **kwargs):
        id = cls.get_meta_attribute("id")
        fields = cls.get_attributes()
        options = cls.get_meta_attribute()
        options["fields"] = ["{}.{}".format(id, key) for key in fields.keys()]
        for key, value in kwargs.items():
            options[key] = value
        options['engine'] = 'meta'
        return Resource(**options)

    @cached_property
    def id_name(self):
        for field in self.fields:
            if field.primary:
                return field.name

        raise FieldError(f"Resource {self.id} has no primary key field")

    def get_id_attribute(self):
        if getattr(self, "_id_attribute", None):
            return self._id_attribute

        for name, field in self.get_attributes().items():
            if isinstance(field, dict) and field.get("primary", False):
                self._id_attribute = name
                return name

        raise AttributeError(f"Resource {self.id} has no ID attribute")

    def get_id(self):
        id_attribute = self.get_id_attribute()
        if id_attribute in self._attributes:
            return getattr(self, id_attribute)

        attribute = self.get_attributes()[id_attribute]
        default = None
        if isinstance(attribute, dict):
            default = attribute.get('default', None)
        return self.get_option(id_attribute, default)

    @classmethod
    def get_meta_attribute(cls, key=None, default=None):
        if not key:
            return as_dict(cls.Schema)
        return getattr(cls.Schema, key, default)

    @cached_property
    def urls(self):
        return self.get_urls()

    def get_urls(self):
        """Get Django urlpatterns for this resource"""
        base = self.url
        patterns = [base]
        for field in self.fields:
            patterns.append(f'{base}{field.name}/')
        return patterns


# -------------------------------------------------
# -*- coding: utf-8 -*-
# File Name：  level
# Description :
# @Time    : 2021/4/3 23:18
# @Author  : Reuben.Lu
# -------------------------------------------------
import pygame
import urllib
import urllib.request
from OpenGL.GL import *
from OpenGL.GLU import *
from math import radians
from pygame.locals import *

SCREEN_SIZE = (1080, 800)
SCALAR = .05
SCALAR2 = 0.2

def resize(width, height):
    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(35.0, float(width) / height, 0.001, 10.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(0.0, 1.0, -6.5,
              0.0, 0.0, 0.0,
              0.0, 1.0, 0.0)

def init():
    glEnable(GL_DEPTH_TEST)
    glClearColor(0.0, 0.05, 0.2, 0.0)
    glShadeModel(GL_SMOOTH)
    glEnable(GL_BLEND)
    glEnable(GL_POLYGON_SMOOTH)
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)
    glEnable(GL_COLOR_MATERIAL)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    glLightfv(GL_LIGHT0, GL_AMBIENT, (0.3, 0.0, 0.3, 1.0));

def read_values():
    link = "http://192.168.0.182:8080"  # Change this address to your settings
    f = urllib.request.urlopen(link)   # <http.client.HTTPResponse object at 0x7f7492a1b9e8>
    #urllib没有urlopen方法

    myfile = f.read().decode("utf8")   # <class 'bytes'>   b'8.734972998756431*28.949391118961653'
    # 用a=urllib.request.urlopen("...")打开一个网页 然后print（a） 前面总有一个b是怎么回事
    # 因为你用的python的版本是3.X,网页内容是二进制的，你需要进行decode,
    # 一般中文的网页编码是GBK或UTF8.这样就可以了a=urllib.request.urlopen("...").decode("utf8")或a=urllib.request.urlopen("...").decode("gbk")
    myfile = str(myfile)  # <str>  b'8.734972998756431*28.949391118961653'
    # 调试时报错 用print打印每个阶段输出结果或者类型 方便分析
    return myfile.split("*")    # ["b'8.734972998756431", "28.949391118961653'"]

def run():
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE | OPENGL | DOUBLEBUF)
    resize(*SCREEN_SIZE)
    init()
    clock = pygame.time.Clock()
    cube = Cube((0.0, 0.0, 0.0), (.5, .0, .0))
    angle = 0

    while True:
        then = pygame.time.get_ticks()
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            if event.type == KEYUP and event.key == K_ESCAPE:
                return

        values = read_values()
        x_angle = values[0]
        y_angle = values[1]
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glColor((1., 1., 1.))
        glLineWidth(1)
        glBegin(GL_LINES)
        for x in range(-20, 22, 2):
            glVertex3f(x / 10., -1, -1)
            glVertex3f(x / 10., -1, 1)

        for x in range(-20, 22, 2):
            glVertex3f(x / 10., -1, 1)
            glVertex3f(x / 10., 1, 1)

        for z in range(-10, 12, 2):
            glVertex3f(-2, -1, z / 10.)
            glVertex3f(2, -1, z / 10.)

        for z in range(-10, 12, 2):
            glVertex3f(-2, -1, z / 10.)
            glVertex3f(-2, 1, z / 10.)

        for z in range(-10, 12, 2):
            glVertex3f(2, -1, z / 10.)
            glVertex3f(2, 1, z / 10.)

        for y in range(-10, 12, 2):
            glVertex3f(-2, y / 10., 1)
            glVertex3f(2, y / 10., 1)

        for y in range(-10, 12, 2):
            glVertex3f(-2, y / 10., 1)
            glVertex3f(-2, y / 10., -1)

        for y in range(-10, 12, 2):
            glVertex3f(2, y / 10., 1)
            glVertex3f(2, y / 10., -1)

        glEnd()
        glPushMatrix()
        glRotate(float(x_angle), 1, 0, 0)
        glRotate(-float(y_angle), 0, 0, 1)
        cube.render()
        glPopMatrix()
        pygame.display.flip()

class Cube(object):

    def __init__(self, position, color):
        self.position = position
        self.color = color

    # Cube information
    num_faces = 6

    vertices = [(-1.0, -0.05, 0.5),
                (1., -0.05, 0.5),
                (1.0, 0.05, 0.5),
                (-1.0, 0.05, 0.5),
                (-1.0, -0.05, -0.5),
                (1.0, -0.05, -0.5),
                (1.0, 0.05, -0.5),
                (-1.0, 0.05, -0.5)]

    normals = [(0.0, 0.0, +1.0),  # front
               (0.0, 0.0, -1.0),  # back
               (+1.0, 0.0, 0.0),  # right
               (-1.0, 0.0, 0.0),  # left
               (0.0, +1.0, 0.0),  # top
               (0.0, -1.0, 0.0)]  # bottom

    vertex_indices = [(0, 1, 2, 3),  # front
                      (4, 5, 6, 7),  # back
                      (1, 5, 6, 2),  # right
                      (0, 4, 7, 3),  # left
                      (3, 2, 6, 7),  # top
                      (0, 1, 5, 4)]  # bottom

    def render(self):
        then = pygame.time.get_ticks()
        glColor(self.color)

        vertices = self.vertices

        # Draw all 6 faces of the cube
        glBegin(GL_QUADS)

        for face_no in range(self.num_faces):    #注意xrange / range的问题  因为python2和3版本
            glNormal3dv(self.normals[face_no])
            v1, v2, v3, v4 = self.vertex_indices[face_no]
            glVertex(vertices[v1])
            glVertex(vertices[v2])
            glVertex(vertices[v3])
            glVertex(vertices[v4])
        glEnd()

if __name__ == "__main__":
    run()


from dependency_injector.wiring import Provide, inject
from fastapi import Depends, FastAPI

from nagasaki.api.auth import validate_basic_auth
from nagasaki.containers import Application, Strategies
from nagasaki.database.database import Database
from nagasaki.logger import logger
from nagasaki.settings.runtime import RuntimeSettings

app = FastAPI()

@app.get(
    "/runtime-config",
    response_model=RuntimeSettings,
    dependencies=[Depends(validate_basic_auth)],
)
@inject
def read_runtime_config(
    database: Database = Depends(Provide[Application.databases.database_provider]),
):
    return database.get_newest_settings()

@app.post(
    "/runtime-config",
    response_model=RuntimeSettings,
    dependencies=[Depends(validate_basic_auth)],
)
@inject
def update_runtime_config(
    data: RuntimeSettings,
    strategy_container: Strategies = Depends(Provide[Application.strategies]),
    database: Database = Depends(Provide[Application.databases.database_provider]),
):
    database.write_settings_to_db(data)
    logger.info("Runtime config updated - rebuilding strategies")
    strategy_container.init_resources()
    strategy_container.strategies_provider.reset()
    return data