TicketsTicketModel = {
    "description": "Tickets",
    "primary_key": "id",
    "key_type": "serial",
    "table": "ticket",
    "title": "Tickets",
    "schema": "tickets",
    "table_predesc": "promoter",
    "schema_predesc": "tickets",
    "sec_table_predesc": "",
    "sec_schema_predesc": "",
    "main_table_schema": "TicketPromoter",
    "sec_table_schema": "",
    "columns": {
        "id": {
            "description": "ID",
            "title": "ID",
            "nickname": "id",
            "field_type": "KeyIncremente",
            "parameters": {
                "type": "SERIAL",
                "validation": "isInteger",
                "required": 1,
                "primary_key": "1"
            }
        },
        "ordering": {
            "description": "Ordering",
            "title": "Ordering",
            "nickname": "ordering",
            "field_type": "Ordering",
            "parameters": {
                "type": "SMALLINT",
                "validation": "isInteger",
                "required": 0
            }
        },
        "state": {
            "description": "State",
            "title": "State",
            "nickname": "state",
            "field_type": "State",
            "parameters": {
                "type": "SMALLINT",
                "validation": "isInteger",
                "required": 0
            }
        },
        "checked_out": {
            "description": "Checked Out",
            "title": "Checked Out",
            "nickname": "checked_out",
            "field_type": "CheckedOut",
            "parameters": {
                "type": "INTEGER",
                "validation": "isInteger",
                "Schema": "master",
                "Table": "user",
                "colLabel": "id",
                "colValue": "first_name",
                "colDescription": "middle_names",
                "required": "0"
            }
        },
        "checked_out_time": {
            "description": "Checked Out Time",
            "title": "Checked Out Time",
            "nickname": "checked_out_time",
            "field_type": "CheckedOutTime",
            "parameters": {
                "type": "TIMESTAMP WITHOUT TIME ZONE",
                "validation": "isDateTime",
                "required": 0
            }
        },
        "created_by": {
            "description": "Created By",
            "title": "Created By",
            "nickname": "created_by",
            "field_type": "CreatedBy",
            "parameters": {
                "type": "INTEGER",
                "validation": "isInteger",
                "Schema": "master",
                "Table": "user",
                "colLabel": "id",
                "colValue": "first_name",
                "colDescription": "middle_names",
                "required": "0"
            }
        },
        "created_time": {
            "description": "Created Time",
            "title": "Created Time",
            "nickname": "created_time",
            "field_type": "CreatedTime",
            "parameters": {
                "type": "TIMESTAMP WITHOUT TIME ZONE",
                "validation": "isDateTime",
                "required": 0
            }
        },
        "modified_by": {
            "description": "Modified By",
            "title": "Modified By",
            "nickname": "modified_by",
            "field_type": "ModifiedBy",
            "parameters": {
                "type": "INTEGER",
                "validation": "isInteger",
                "Schema": "master",
                "Table": "user",
                "colLabel": "id",
                "colValue": "first_name",
                "colDescription": "middle_names",
                "required": "0"
            }
        },
        "modified_time": {
            "description": "Modified Time",
            "title": "Modified Time",
            "nickname": "modified_time",
            "field_type": "UpdatedTime",
            "parameters": {
                "type": "TIMESTAMP WITHOUT TIME ZONE",
                "validation": "isDateTime",
                "required": 0
            }
        },
        "id_promoter": {
            "description": "Promotoras",
            "title": "Promotoras",
            "nickname": "id_promoter",
            "field_type": "KeyDimension",
            "parameters": {
                "type": "BIGINT",
                "Table": "promoter",
                "Schema": "tickets",
                "colLabel": "title",
                "colValue": "id",
                "required": "0",
                "colDescription": "description"
            }
        },
        "price": {
            "description": "Pre\u00e7o",
            "title": "Pre\u00e7o",
            "nickname": "price",
            "field_type": "Money",
            "parameters": {
                "type": "NUMERIC",
                "validation": "isMoney",
                "length": "10",
                "precision": "3",
                "required": 0
            }
        }
    }
}


import glob
import os

from skimage.io import imread, imsave

def crop_images(data_dir, result_dir):

  if not os.path.isdir(result_dir):
    os.makedirs(result_dir)
  print('Cropped images will be saved to {} ...\n'.format(result_dir))

  with open('data/bbox.txt') as f:
    for line in f:
      name, bbox = line.strip().split(':')
      sx, sy, ex, ey = [int(i) for i in bbox.split(',')]

      print('Processing {} ...'.format(name))
      images = glob.glob(os.path.join(data_dir, name, '*'))
      if not os.path.isdir(os.path.join(result_dir, name)):
        os.makedirs(os.path.join(result_dir, name))

      for image in images:
        full_image = imread(image)
        cropped_image = full_image[sx:ex, sy:ey]

        imsave(os.path.join(result_dir, name, os.path.basename(image)), cropped_image)


from typing import Any

import pytest
from diffusers import UNet2DConditionModel
from diffusers.models.embeddings import ImageProjection, Resampler
from peft import LoHaConfig, LoKrConfig, LoraConfig, OFTConfig

from diffengine.models.archs import (
    create_peft_config,
    process_ip_adapter_state_dict,
    set_unet_ip_adapter,
)

def test_set_unet_ip_adapter():
    unet = UNet2DConditionModel.from_pretrained(
        "hf-internal-testing/tiny-stable-diffusion-xl-pipe", subfolder="unet")
    assert not any("processor" in k for k in unet.state_dict())
    set_unet_ip_adapter(unet)
    assert any("processor.to_k_ip" in k for k in unet.state_dict())
    assert any("processor.to_v_ip" in k for k in unet.state_dict())

def test_create_peft_config():
    config: dict[str, Any] = dict(
        type="Dummy",
    )
    with pytest.raises(AssertionError, match="Unknown PEFT type"):
        create_peft_config(config)

    config = dict(
        type="LoRA",
        r=4,
    )
    config = create_peft_config(config)
    assert isinstance(config, LoraConfig)
    assert config.r == 4

    config = dict(
        type="LoHa",
        r=8,
        alpha=2,
    )
    config = create_peft_config(config)
    assert isinstance(config, LoHaConfig)
    assert config.r == 8
    assert config.alpha == 2

    config = dict(
        type="LoKr",
        r=8,
        alpha=2,
    )
    config = create_peft_config(config)
    assert isinstance(config, LoKrConfig)
    assert config.r == 8
    assert config.alpha == 2

    config = dict(
        type="OFT",
        r=8,
    )
    config = create_peft_config(config)
    assert isinstance(config, OFTConfig)
    assert config.r == 8

def test_process_ip_adapter_state_dict():
    unet = UNet2DConditionModel.from_pretrained(
        "hf-internal-testing/tiny-stable-diffusion-xl-pipe", subfolder="unet")
    set_unet_ip_adapter(unet)
    proj = ImageProjection(
        cross_attention_dim=128,
        image_embed_dim=128,
        num_image_text_embeds=4,
    )
    proj_state_dict = process_ip_adapter_state_dict(unet, proj)
    assert list(proj_state_dict.keys()) == ["image_proj", "ip_adapter"]
    assert "proj.weight" in proj_state_dict["image_proj"]
    assert len(proj_state_dict["ip_adapter"]) == 24

    resampler = Resampler(
        embed_dims=32,
        output_dims=32,
        hidden_dims=128,
        depth=1,
        dim_head=2,
        heads=2,
        num_queries=4,
        ffn_ratio=1,
    )
    proj_state_dict = process_ip_adapter_state_dict(unet, resampler)
    assert list(proj_state_dict.keys()) == ["image_proj", "ip_adapter"]
    assert "latents" in proj_state_dict["image_proj"]
    assert len(proj_state_dict["ip_adapter"]) == 24


from flask import Flask, jsonify, request
from flask_cors import CORS
import click

import datetime as dt
import os
import sqlite3 as s

app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})

BOOKMARKLET_STORAGE = "bookmarklet_storage"

@app.route("/api/bookmark", methods=["GET"])
def bookmark():
    payload = request.args.to_dict()
    now = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    conn = s.connect(os.environ.get(BOOKMARKLET_STORAGE))

    with conn:
        cur = conn.cursor()
        try:
            cur.execute(
                '''
                INSERT INTO history (
                    page_title,
                    url,
                    desc,
                    img,
                    date,
                    is_pushed
                ) VALUES(?,?,?,?,?,?)
                ''',
                (
                    payload['title'],
                    payload['url'],
                    payload['desc'],
                    payload['img'],
                    now,
                    0
                )
            )
        except s.IntegrityError:
            return jsonify({"status": "entry existed"}), 400
    return jsonify({"status": "success", "payload": payload})

@app.cli.command()
@click.option("--storage", required=True)
def init(storage):
    if not os.path.exists(storage):
        conn = s.connect(storage)
        with conn:
            cur = conn.cursor()
            cur.execute(
                '''
                CREATE TABLE IF NOT EXISTS history (
                    page_title TEXT NOT NULL,
                    url TEXT PRIMARY KEY NOT NULL,
                    desc TEXT,
                    img TEXT,
                    date TEXT NOT NULL,
                    is_pushed INT
                )
                '''
            )

@app.cli.command()
@click.option("--storage", required=True)
def backup(storage, username, password, database, port):
    pass

@app.cli.command()
@click.option("--storage", required=True)
@click.option("--full", type=bool, default=False)
def inspect(storage, full):
    if os.path.exists(storage):
        conn = s.connect(storage)
        q = 'SELECT * FROM history' if full else 'SELECT * FROM history WHERE is_pushed=0'
        with conn:
            cur = conn.cursor()
            cur.execute(q)
            import json
            click.echo(json.dumps(cur.fetchall(), indent=2))

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--storage", required=True)
    args = parser.parse_args()

    os.environ[BOOKMARKLET_STORAGE] = args.storage
    if not os.path.exists(args.storage):
        raise FileNotFoundError
    app.run(port=8080)


import numpy as np
import pytest
from source.utilities.ConnectFourSearcher import ConnectFourSearcher

@pytest.fixture
def board():
    board = np.array(
        [
            ['0-0', '0-1', '0-2', '0-3', '0-4', '0-5', '0-6'],
            ['1-0', '1-1', '1-2', '1-3', '1-4', '1-5', '1-6'],
            ['2-0', '2-1', '2-2', '2-3', '2-4', '2-5', '2-6'],
            ['3-0', '3-1', '3-2', '3-3', '3-4', '3-5', '3-6'],
            ['4-0', '4-1', '4-2', '4-3', '4-4', '4-5', '4-6'],
            ['5-0', '5-1', '5-2', '5-3', '5-4', '5-5', '5-6']
        ])
    return board

def test_search_horizontal(board):
    searcher = ConnectFourSearcher()

    combinations = [
        ['5-0', '5-1', '5-2', '5-3'],
        ['5-1', '5-2', '5-3', '5-4'],
        ['5-2', '5-3', '5-4', '5-5'],
        ['5-3', '5-4', '5-5', '5-6']
    ]
    real_combinations = searcher.search_horizontal(board, 5, 3)
    assert np.array_equal(combinations, real_combinations)

def test_search_vertical(board):
    searcher = ConnectFourSearcher()

    combinations = [
        ['2-3', '3-3', '4-3', '5-3']
    ]
    real_combinations = searcher.search_vertical(board, 5, 3)
    assert np.array_equal(combinations, real_combinations)

def test_search_negative_diagonal(board):
    searcher = ConnectFourSearcher()

    combinations = [
        ['2-0', '3-1', '4-2', '5-3']
    ]
    real_combinations = searcher.search_negative_diagonal(board, 5, 3)
    assert np.array_equal(combinations, real_combinations)

def test_search_positive_diagonal(board):
    searcher = ConnectFourSearcher()

    combinations = [
        ['5-3', '4-4', '3-5', '2-6']
    ]
    real_combinations = searcher.search_positive_diagonal(board, 5, 3)
    assert np.array_equal(combinations, real_combinations)


import os
import csv
import json
import pandas as pd
from itertools import islice

from math import sqrt
#test

def make_json(csvFilePath, jsonFilePath):
    data = {}

    with open(csvFilePath, encoding='utf-8-sig') as csvf:
        csvReader = csv.DictReader(csvf)

        for row in islice(csvReader, 0, None, 3):
            # print(row)
            row["point"] = row["point"].replace("淡", "")
            row["system"] = row["system"].replace(" ", "")

            #strat the process of Left Side of the River
            row["L-mode"] = row["L-mode"].replace(" ", "")
            if row["L-mode"] == "支距":
                row["L-mode"] = "distance"
            elif row["L-mode"] == "轉換":
                row["L-mode"] = "transform"

            row["L-Ordinate-N(Y)"] = row["L-Ordinate-N(Y)"].replace(" ", "")
            row["L-Ordinate-N(Y)"] = row["L-Ordinate-N(Y)"].replace(",", "")
            row["L-Abscissa-E(X)"] = row["L-Abscissa-E(X)"].replace(" ", "")
            row["L-Abscissa-E(X)"] = row["L-Abscissa-E(X)"].replace(",", "")

            row["L-Rel-0-points"] = row["L-Rel-0-points"].replace(" ", "")
            row["L-Rel-0-points"] = row["L-Rel-0-points"].replace(",", "")

            row["L-Elevation"] = row["L-Elevation"].replace(" ", "")
            row["L-Elevation"] = row["L-Elevation"].replace(",", "")

            #strat the process of Right Side of the River
            row["R-mode"] = row["R-mode"].replace(" ", "")
            if row["R-mode"] == "支距":
                row["R-mode"] = "distance"
            elif row["R-mode"] == "轉換":
                row["R-mode"] = "transform"

            row["R-Ordinate-N(Y)"] = row["R-Ordinate-N(Y)"].replace(" ", "")
            row["R-Ordinate-N(Y)"] = row["R-Ordinate-N(Y)"].replace(",", "")
            row["R-Abscissa-E(X)"] = row["R-Abscissa-E(X)"].replace(" ", "")
            row["R-Abscissa-E(X)"] = row["R-Abscissa-E(X)"].replace(",", "")

            row["R-Rel-0-points"] = row["R-Rel-0-points"].replace(" ", "")
            row["R-Rel-0-points"] = row["R-Rel-0-points"].replace(",", "")

            row["R-Elevation"] = row["R-Elevation"].replace(" ", "")
            row["R-Elevation"] = row["R-Elevation"].replace(",", "")

            key = row['point']
            data[key] = row
            dx = pow((float(row["R-Abscissa-E(X)"]) - float(row["L-Abscissa-E(X)"])), 2)
            dy = pow((float(row["R-Ordinate-N(Y)"]) - float(row["L-Ordinate-N(Y)"])), 2)
            data[key]['distance'] = pow((dx + dy), 0.5)

    with open(jsonFilePath, 'w', encoding='utf-8-sig') as jsonf:
        jsonf.write(json.dumps(data, indent=4))

def readjson2Dict(jsonFilePath):
    global tamsui_json
    with open(jsonFilePath, 'r', encoding='utf-8-sig') as jsonf:
        tamsui_json = json.load(jsonf)

def print_preprocessed_data(x_ext, y_ext):
    print(f" -> after data processing: x_ext: {x_ext}")
    print(f" -> after data processing: y_ext: {y_ext}")

def fix_extdata(file_name, x_ext, y_ext):
    isAcceptable = True

    if len(x_ext) != len(y_ext):
        print(f" -> {file_name} data is not complete.")
        isAcceptable = False
    else:
        for list_index in range(len(x_ext)):
            x_ext[list_index] = round(x_ext[list_index], 2)
            y_ext[list_index] = round(y_ext[list_index], 2)

    return x_ext, y_ext, isAcceptable

def validate_data(new_x, ex_data, ny_data):
    if len(ex_data) != len(ny_data):
        print("Data is not complete.")
        return False # if the data is not complete

    for list_index in range(1, len(ex_data)):
        check_value = sqrt(pow((ex_data[list_index] - ex_data[0]), 2) + pow((ny_data[list_index] - ny_data[0]), 2))
        # print(f"The delta is {check_value} - {new_x[list_index]} = {abs(check_value - new_x[list_index])} at pos: {list_index}")
        if abs(check_value - new_x[list_index]) > 0.2:
            print("Data is not correct. ")
            # print(f"The delta is {check_value} - {new_x[list_index]} = {abs(check_value - new_x[list_index])}")
            return False # if the value is not correct

    return True #pass the validation

def axis_predict_formula(curr_rel_0, last_rel_0, first_axis, last_axis):
    r = (round(curr_rel_0, 2) * (last_axis - first_axis) + (last_rel_0 * first_axis)) / last_rel_0
    # print(f"({round(curr_rel_0, 2)} * ({last_axis} - {first_axis}) + ({last_rel_0} * {first_axis})) / {last_rel_0}")
    # print(f"The result of predict: {r}")
    return round((round(curr_rel_0, 2) * (last_axis - first_axis) + (last_rel_0 * first_axis)) / last_rel_0, 4) # the formula to predict next value of axis(x or y)

def tr_write_csv(filename, elevation, ex_data, ny_data, year, month, riversection):
    transform_df = pd.DataFrame()

    elevation.insert(0, year)
    ex_data.insert(0, month)
    ny_data.insert(0, riversection)

    transform_df['elevation'] = elevation
    transform_df['ex_data'] = ex_data
    transform_df['ny_data'] = ny_data

    filename = "/Users/alecwu/TRANSFORM_NEW/" + "TRANSFORM_" + filename

    pd.set_option('display.float_format', '{:.5f}'.format)

    print(transform_df)
    transform_df.to_csv(filename, index=False)

def pos_transform(raw_data_path):
    if not os.path.exists(raw_data_path):
        print(f"The directory '{raw_data_path}' does not exist.")
        return

    if not os.path.isdir(raw_data_path):
        print(f"'{raw_data_path}' is not a directory.")
        return

    files = os.listdir(raw_data_path)
    print(f"Files in the directory '{raw_data_path}':")

    month_id = ["120", "100", "050"]
    status = "pass"
    declined_list = []
    Correct_list = []
    not_found_rel0points = []
    for file_name in files:
        #message for processing the file
        print(f"Processing '{file_name}'...", end="")

        for month in month_id:
            if file_name.find(month) == -1:
                continue

            mid_index = file_name.find(month)
            year = file_name[1:mid_index]
            river_section = file_name[mid_index+2:]
            river_section = river_section.replace("csv", "")
            river_section = river_section.replace(".", "")

            print(f" -> year: {year}, month: {month}, river_section: {river_section}")

            if river_section in list(tamsui_json.keys()):
                print(f" -> {river_section} is ACCEPTED.")

                to_open = raw_data_path + "/" + file_name
                with open(to_open, 'r', encoding='utf-8-sig') as csvf:
                    print(f" -> {file_name} is opened.")

                    #read the csv file
                    csv_reader = pd.read_csv(csvf)

                    # Define the column to extract
                    extracted_x = "x"
                    extracted_y = "y"

                    # Extract data using a list comprehension
                    x_ext = csv_reader[extracted_x].tolist()
                    y_ext = csv_reader[extracted_y].tolist()

                    # print(f" -> Extracted data: {extracted_data}")
                    if len(x_ext) == 0 or len(y_ext) == 0:
                        print(f" -> {file_name} data is empty.")
                        break
                    elif len(x_ext) != len(y_ext):
                        print(f" -> {file_name} data is not complete.")
                        break
                    else:
                        print(f" -> {file_name} data is complete.")

                    if x_ext[0] > x_ext[-1]:
                        print(f" -> range check failed. [{x_ext[0]}, {x_ext[-1]}]")

                    print(f" -> x_ext: {x_ext} len: {len(x_ext)}")
                    print(f" -> y_ext: {y_ext} len: {len(y_ext)}")

                    R_rel_0_points = tamsui_json[river_section]["R-Rel-0-points"] # checking -96.55
                    R_rel_0_points = float(R_rel_0_points)
                    R_elevation = tamsui_json[river_section]["R-Elevation"]
                    R_elevation = float(R_elevation)
                    L_rel_0_points = tamsui_json[river_section]["L-Rel-0-points"] # checking -96.55
                    L_rel_0_points = float(L_rel_0_points)
                    L_elevation = tamsui_json[river_section]["L-Elevation"]
                    L_elevation = float(L_elevation)
                    found = False
                    pos = -1

                    if R_rel_0_points in x_ext: # find the value (Right side)
                        print(f" -> {x_ext[x_ext.index(R_rel_0_points)]} is the R-Rel-0-points.")
                        found = True
                        pos = x_ext.index(R_rel_0_points)

                        for list_index in range(len(x_ext)-1):
                            if(x_ext[list_index] == x_ext[pos] and y_ext[list_index] == R_elevation):
                                pos = list_index # the currect position of rel_0
                                continue
                            elif(x_ext[list_index] == x_ext[pos]): # remove the duplicated rel_0
                                x_ext.remove(x_ext[list_index])
                                y_ext.remove(y_ext[list_index])

                            if(x_ext[list_index] > x_ext[pos]):
                                break

                        point_found = False # the flag of finding the correct index with correct rel_0 and elevation
                        for list_value in x_ext[:]:
                            list_index = x_ext.index(list_value)
                            print(f" -> check {list_index}'s {list_value} {R_rel_0_points} at {y_ext[list_index]} {round(R_elevation,2)}")
                            if(list_value == R_rel_0_points):
                                if(y_ext[list_index] == round(R_elevation,2)):
                                    pos = list_index # the currect position of rel_0
                                    point_found = True # find the correct index
                                    print(f" -> {list_value} at {list_index} is the corrert value")
                                    continue
                                else: # remove the duplicated rel_0
                                    x_ext.remove(list_value)
                                    y_ext.remove(y_ext[list_index])

                            if(list_value > R_rel_0_points):
                                break

                        if(not point_found): # not find the correct index -> using forced insertion
                            print(f" -> Forced inserting {R_rel_0_points} {round(R_elevation,2)} to list")
                            x_ext.insert(pos,R_rel_0_points)
                            y_ext.insert(pos,round(R_elevation,2))
                            point_found = True

                        x_ext = x_ext[:pos+1]
                        y_ext = y_ext[:pos+1]

                        print(f" -> pos: {pos}")
                        # print_preprocessed_data(x_ext, y_ext)

                    if not found: # Insert values (Right side)
                        not_found_dict = {file_name: R_rel_0_points}
                        not_found_rel0points.append(not_found_dict)

                        print(f" -> {R_rel_0_points} is NOT found.")

                        for list_index in range(len(x_ext)-1):
                            if x_ext[list_index] < R_rel_0_points and R_rel_0_points < x_ext[list_index+1]:
                                pos = list_index
                                found = True

                                print(f" -> {file_name} insert value.")
                                x_ext.insert(pos+1, R_rel_0_points)
                                y_ext.insert(pos+1, R_elevation)

                                x_ext = x_ext[:pos+2]
                                y_ext = y_ext[:pos+2]

                                break

                        if found:
                            pos = x_ext.index(R_rel_0_points)
                            print(f" -> pos: {pos+1}")
                            # print_preprocessed_data(x_ext, y_ext)
                        else:
                            print(f" -> {R_rel_0_points} is NOT between {x_ext[0]} and {x_ext[-1]}.")
                            print(f" -> Appending {R_rel_0_points}, {R_elevation} to the end of x_ext and y_ext list ...")
                            x_ext.append(R_rel_0_points)
                            y_ext.append(R_elevation)

                    found = False # Reset found to false for left side

                    if L_rel_0_points in x_ext: # find the value (Left side)
                        print(f" -> {x_ext[x_ext.index(L_rel_0_points)]} is the L-Rel-0-points.")
                        found = True
                        pos = x_ext.index(L_rel_0_points)

                        point_found = False # the flag of finding the correct index with correct rel_0 and elevation
                        for list_value in x_ext[:]:
                            list_index = x_ext.index(list_value)
                            print(f" -> check {list_index}'s {list_value} {L_rel_0_points} at {y_ext[list_index]} {round(L_elevation,2)}")
                            if(list_value == L_rel_0_points):
                                if(y_ext[list_index] == round(L_elevation,2)):
                                    pos = list_index # the currect position of rel_0
                                    point_found = True # find the correct index
                                    print(f" -> {list_value} at {list_index} is the corrert value")
                                    continue
                                else: # remove the duplicated rel_0
                                    x_ext.remove(list_value)
                                    y_ext.remove(y_ext[list_index])

                            if(list_value > L_rel_0_points):
                                break

                        if(not point_found): # not find the correct index -> using forced insertion
                            print(f" -> Forced inserting {L_rel_0_points} {round(L_elevation,2)} to list")
                            x_ext.insert(pos,L_rel_0_points)
                            y_ext.insert(pos,round(L_elevation,2))
                            point_found = True

                        x_ext = x_ext[pos:]
                        y_ext = y_ext[pos:]

                        print(f" -> pos: {pos}")

                    if not found: # Insert values (Left side)
                        not_found_dict = {file_name: L_rel_0_points}
                        not_found_rel0points.append(not_found_dict)

                        print(f" -> {L_rel_0_points} is NOT found.")
                        # print(f" -> x_ext : {x_ext}")
                        for list_index in range(len(x_ext)-1):
                            if x_ext[list_index] < L_rel_0_points and L_rel_0_points < x_ext[list_index+1]:
                                pos = list_index
                                found = True

                                print(f" -> {file_name} insert value.")
                                x_ext.insert(pos+1, L_rel_0_points)
                                y_ext.insert(pos+1, L_elevation)

                                x_ext = x_ext[pos+1:]
                                y_ext = y_ext[pos+1:]
                                break

                        if found:
                            pos = x_ext.index(L_rel_0_points)
                            print(f" -> pos: {pos+1}")
                        else:
                            print(f" -> {L_rel_0_points} is NOT between {x_ext[0]} and {x_ext[-1]}.")
                            print(f" -> Inserting {L_rel_0_points}, {L_elevation} to the head of x_ext and y_ext list ...")
                            x_ext.insert(0,L_rel_0_points)
                            y_ext.insert(0,L_elevation)

                    print(f" -> x_ext: {x_ext} len: {len(x_ext)}")
                    print(f" -> y_ext: {y_ext} len: {len(y_ext)}")

                    print(f" -> {file_name} is doing fixing.")
                    x_ext, y_ext, isAcceptable = fix_extdata(file_name, x_ext, y_ext)

                    if not isAcceptable:
                        print(f" -> ERROR occurred in {file_name}.")
                        break

                    # print(f" -> after fixing - round(numebr, 2): x_ext: {x_ext}")
                    # print(f" -> after fixing - round(numebr, 2): y_ext: {y_ext}")

                    print(f" -> removing duplicated data in {file_name}.")
                    xtemp = []
                    ytemp = []
                    now = x_ext[0]
                    xtemp.append(x_ext[0])
                    ytemp.append(y_ext[0])
                    for list_index in range(1, len(x_ext)-1):
                        if x_ext[list_index] != now:
                            xtemp.append(x_ext[list_index])
                            ytemp.append(y_ext[list_index])
                        now = x_ext[list_index]
                    xtemp.append(x_ext[-1])
                    ytemp.append(y_ext[-1])

                    if len(xtemp) != len(ytemp):
                        print(f" -> ERROR occurred in the process of removing duplicated data in {file_name}.")
                        break

                    x_ext = xtemp
                    y_ext = ytemp

                    del xtemp
                    del ytemp

                    '''
                    This is a exmple for using the function of validate_data(new_x, ex_data, ny_data)

                    90A:
                    test_new_x = [0.0000, 4.7300, 12.1500, 14.3600]
                    test_ex_data = [275470.255, 275474.1711, 275480.3151, 275482.1451]
                    test_ny = [2746480.522, 2746477.8695, 2746473.7094, 2746472.4703]

                    if validate_data(test_new_x, test_ex_data, test_ny):
                        print(" -> Data is correct.")
                    else:
                        print(f" -> A validation error occurred in the process of removing duplicated data in {file_name}.")
                        break
                    '''

                    print(f" -> after removing duplicated data: x_ext: {x_ext} len: {len(x_ext)}")
                    print(f" -> after removing duplicated data: y_ext: {y_ext} len: {len(y_ext)}")

                    L_Ordinate_N_Y = tamsui_json[river_section]["L-Ordinate-N(Y)"]
                    L_Ordinate_N_Y = float(L_Ordinate_N_Y) # first Y
                    L_Abscissa_E_X = tamsui_json[river_section]["L-Abscissa-E(X)"]
                    L_Abscissa_E_X = float(L_Abscissa_E_X) # first X
                    R_Ordinate_N_Y = tamsui_json[river_section]["R-Ordinate-N(Y)"]
                    R_Ordinate_N_Y = float(R_Ordinate_N_Y) # last Y
                    R_Abscissa_E_X = tamsui_json[river_section]["R-Abscissa-E(X)"]
                    R_Abscissa_E_X = float(R_Abscissa_E_X) # last X
                    axis_x_predict_list = []
                    axis_y_predict_list = []
                    print(f" -> Computing the value of axis in {file_name}.")
                    axis_x_predict_list.append(L_Abscissa_E_X)
                    axis_y_predict_list.append(L_Ordinate_N_Y)
                    x_ext[-1] -= x_ext[0]

                    for index in range(1,len(x_ext)-1):
                        x_ext[index] -= x_ext[0]
                        # parameter => curr_rel_0, last_rel_0, first_axis, last_axis
                        predict_x = axis_predict_formula(x_ext[index],x_ext[-1],L_Abscissa_E_X,round(R_Abscissa_E_X,3))
                        predict_y = axis_predict_formula(x_ext[index],x_ext[-1],L_Ordinate_N_Y,round(R_Ordinate_N_Y,3))
                        axis_x_predict_list.append(predict_x)
                        axis_y_predict_list.append(predict_y)
                    axis_x_predict_list.append(R_Abscissa_E_X)
                    axis_y_predict_list.append(R_Ordinate_N_Y)

                    x_ext[0] = 0
                    isVaild = validate_data(x_ext, axis_x_predict_list, axis_y_predict_list)
                    if(isVaild):
                        Correct_list.append(river_section)
                        print(" -> Data is correct.")
                        print(f" -> Writing the data to csv file in {file_name}.")
                        tr_write_csv(file_name, y_ext, axis_x_predict_list, axis_y_predict_list, year, month, river_section)

                        print(f" -> {file_name} is all done.\n")

                    print(f" -> Process of {file_name} is all done.\n")
            else:
                print(f" -> {river_section} is DECLINED.\n")
                declined_list.append(river_section)

            break

    print(f"Correct list: {Correct_list}")
    print(f"Total correct: {len(Correct_list)}")
    print()
    print(f"Declined list: {declined_list}")
    print(f"Total declined: {len(declined_list)}") # 4898
    print()
    #print Not found Rel-0-points line by line
    # for not_found in not_found_rel0points:
    #     print(f"Not found Rel-0-points: {not_found}")
    # print(f"Total not found Rel-0-points: {len(not_found_rel0points)}")

    # print(f"\n{len(files)} files in total.")

def merge_csv(files_path): #sorry我把妳ㄉcode改了一點點，"一點點"
    files = os.listdir(files_path)

    river_section = tamsui_json.keys()

    river_section = list(river_section)
    river_section.reverse()

    '''
    the dictionary should be like this:
    river_dict = {
        "58":{   ------> year
            "90A": [[elevation, ex, ny], [elevation, ex, ny], [elevation, ex, ny]], ------> each of river section
            "90": [................],
            "89": [], ------> may be empty
            ....
            "00": [................],
        },
        "60":{
            "90A":[................],
            "90":[................],
            ....
        },
        ...
        "109":{
            "90A":[................],
            "90":[................],
            ....
        }
    }
    '''

    # create a dictionary to store the data
    river_dict = {}
    for year in range(58, 110):
        river_dict[str(year)] = {}
        for river in river_section:
            river_dict[str(year)][river] = []

    # print the river_dict line by line
    for year in river_dict:
        print(f" -> {year}: {river_dict[year]}")
        print()

    ll = []

    print(f" -> River section: {river_section}")

    month_id = ["120", "100", "050"]

    for file_name in files:
        if file_name.find("TRANSFORM") == -1:
            continue

        print(f" -> Checking {files_path + '/' + file_name}...")

        for month in month_id:
            if file_name.find(month) == -1:
                continue

            mid_index = file_name.find(month)
            year = file_name[11:mid_index]
            river_section = file_name[mid_index+2:]
            river_section = river_section.replace("csv", "")
            river_section = river_section.replace(".", "")

            print(f" -> year: {year}, month: {month}, river_section: {river_section}")

            print(f" -> Opening {files_path + '/' + file_name}...")
            to_open = files_path + "/" + file_name
            df = pd.read_csv(to_open)
            print(f" -> [v] {files_path + '/' + file_name} is opened.")

            # print(f" -> test df: {df['ny_data'][1]}")

            # add the data to the dictionary
            print(" -> Adding data to the dictionary...")
            for index in range(len(df['ny_data'])):
                river_dict[year][river_section].append([df['elevation'][index], df['ex_data'][index], df['ny_data'][index]])
            print(" -> [v] Data is added to the dictionary.")

            print()
            break

    print("\n\n")
    # merge the data of each year to a csv file
    print(" -> Merging the data of each year to a csv file...")
    for year in river_dict:
        print(f" -> year: {year}")
        df = pd.DataFrame()
        elevation = []
        ex_data = []
        ny_data = []
        for river in river_dict[year]:
            print(f" -> river: {river}")
            for index in range(len(river_dict[year][river])):
                if index == 0: continue
                elevation.append(river_dict[year][river][index][0])
                ex_data.append(river_dict[year][river][index][1])
                ny_data.append(river_dict[year][river][index][2])
            print(f" -> [v] {river} is done.")
        print(f" -> [v] {year} data is all collected.")

        df["elevation"] = elevation
        df["ex_data"] = ex_data
        df["ny_data"] = ny_data

        # save the data to a csv file
        print(f" -> Saving the data to a csv file in /Users/alecwu/TRANSFORM_eachyear/{year}.csv...")
        df.to_csv(f"/Users/alecwu/TRANSFORM_eachyear/{year}.csv", index=False)

        print(f" -> [v] {year} is done.")
        print()

if __name__ == "__main__":
    dataFilePath = r"./data/"
    csvFilePath = r"tamsui_new.csv"
    jsonFilePath = r"tamsui.json"
    mergeCsvPath = r"./merge/"

    raw_data_path = "/Users/alecwu/result_all" #Today is a good day xD

    nonduplicated_data_path = "/Users/wufangyi/result_non-duplicated"
    save_data_path = "/Users/wufangyi/result_non-duplicated" #commit

    transformed_data_path = "/Users/alecwu/TRANSFORM"

    # make_json(dataFilePath + csvFilePath, jsonFilePath) #make json file
    # readjson2Dict(jsonFilePath) #read json

    # os.mkdir("./TRANSFORM")
    # pos_transform(raw_data_path)

    # os.mkdir("./MERGE")
    # # merge the csv files of each year
    # merge_csv(transformed_data_path)

    # print(tamsui_json)


# Mega Case Study - Make a Hybrid Deep Learning Model

# Part 1 - Identify the frauds with SOM
import numpy as np
import pandas as pd
from keras import Sequential
from keras.layers import Dense
from minisom import MiniSom
from sklearn.preprocessing import MinMaxScaler, StandardScaler

dataset = pd.read_csv("Credit_Card_Applications.csv")

print(dataset.info())
print(dataset.describe())

X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

sc = MinMaxScaler(feature_range=(0, 1))
X = sc.fit_transform(X)

som = MiniSom(x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5)
som.random_weights_init(X)
som.train_random(data=X, num_iteration=100)

print("Som Trained")

# Finding the frauds
mappings = som.win_map(X)
frauds = np.concatenate((mappings[(8, 1)], mappings[(6, 8)]), axis=0)
frauds = sc.inverse_transform(frauds)

# Part 2 - Going from unsupervised to Supervised deep learning
customers = dataset.iloc[:, 1:].values

is_fraud = np.zeros(len(dataset))
for i in range(len(dataset)):
    if dataset.iloc[i, 0] in frauds:
        is_fraud[i] = 1

sc = StandardScaler()
customers = sc.fit_transform(customers)

classifier = Sequential()

classifier.add(Dense(units=2, kernel_initializer='uniform', activation='relu', input_dim=11))
classifier.add(Dense(units=1, kernel_initializer='uniform', activation='sigmoid'))

classifier.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

classifier.fit(customers, is_fraud, batch_size=10, epochs=100)

y_pred = classifier.predict(customers)
y_pred = np.concatenate((dataset.iloc[:, 0:1].values, y_pred), axis=1)

y_pred = y_pred[y_pred[:, 1]].argsort()

Volume 2 - Unsupervised Deep Learning/Mega Case Study/hybrid_model.py


def process_set(str_lst,index):
    lst = []
    for i in index:
        lst.append(str_lst[i])
    lst = sorted(lst)
    count = 0
    for i in index:
        str_lst[i] = lst[count]
        count = count + 1
    #print(str_lst)
    return str_lst

string = input()
str = list(string)

pairs = eval(input())
list = []
for i in range(0,len(pairs)):
    a = pairs[i][0]
    b = pairs[i][1]
    exist = False
    for j in range(0,len(list)):
        if (a in list[j]) or (b in list[j]):
            list[j].add(a)
            list[j].add(b)
            exist = True
            break
    if not exist:
        new_set = set()
        new_set.add(a)
        new_set.add(b)
        list.append(new_set)
for i in range(0,len(list)):
    str = process_set(str,list[i])
ans = "".join(str)
print(ans)


import pandas as pd
import struct
import time
from math import isnan

# dnt_file_path = "itemdropgrouptable.dnt"
# csv_file_path = "itemdropgrouptable.csv"
# dnt_file_path = "stagerewardtable.dnt"
# csv_file_path = "stagerewardtable.csv"
# dnt_file_path = "stagerewardtable_normal.dnt"
# csv_file_path = "stagerewardtable_normal.csv"
dnt_file_path = "itemtable_equipment.dnt"
csv_file_path = "itemtable_equipment.csv"

def ReadDataToDF(file_path):
    '''
    读取file_path所对应的二进制dnt文件，并将其输出为DataFrame数据结构
    dnt_bytes:
    columns:
    rows:
    seek:
    column_name_size:
    column_name:
    column_arg_type:
    '''
    begin_time = time.time()
    with open(file_path, 'rb')as dnt_handle:
        dnt_bytes = dnt_handle.read()
        columns = struct.unpack('H', dnt_bytes[4:6])[0]
        columns = columns + 1  # 加上_RowID
        rows = struct.unpack('I', dnt_bytes[6:10])[0]
        seek = 10
        data_frame = pd.DataFrame()
        column_info = [{'column_name_size': 6,
                        'column_name': '_RowID', 'column_arg_type': 3}]
        data_frame['_RowID'] = None
        for column in range(1, columns):
            column_info_item = {}
            column_info_item['column_name_size'] = struct.unpack(
                'B', dnt_bytes[seek:seek+1])[0]
            seek = seek+2
            column_info_item['column_name'] = str(struct.unpack(
                str(column_info_item['column_name_size'])+'s',
                dnt_bytes[seek:seek+column_info_item['column_name_size']])[0], encoding="utf8")
            # print(column_info_item['column_name'])
            seek = seek + column_info_item['column_name_size']
            column_info_item['column_arg_type'] = struct.unpack(
                'B', dnt_bytes[seek:seek+1])[0]
            seek = seek+1
            data_frame[column_info_item['column_name']] = None
            column_info.append(column_info_item)
        before_read_data = time.time()
        data_frame_result = ReadData(
            data_frame, column_info, rows, dnt_bytes, seek)
        end_read_data = time.time()
        print("before read data time cost:"+str(before_read_data-begin_time))
        print("read data time cost:"+str(end_read_data-before_read_data))
        return (data_frame_result, column_info)

def FillDataType(column_info_item, dnt_bytes, seek):
    '''
    向column_info_item里填充参数数据
    seek:对应dnt_bytes中正在处理的位置
    return: seek或0(即失败)
    '''
    if column_info_item['column_arg_type'] == 1:
        column_info_item['column_arg_size'] = struct.unpack(
            'H', dnt_bytes[seek:seek+2])[0]
        seek = seek+2
        column_info_item['column_arg_data'] = str(struct.unpack(
            str(column_info_item['column_arg_size'])+'s',
            dnt_bytes[seek:seek+column_info_item['column_arg_size']])[0], encoding="utf8")
        seek = seek + column_info_item['column_arg_size']
    elif (column_info_item['column_arg_type'] == 2 or column_info_item['column_arg_type'] == 3):
        column_info_item['column_arg_size'] = 4
        column_info_item['column_arg_data'] = struct.unpack(
            'I', dnt_bytes[seek:seek+4])[0]
        seek = seek+4
    elif column_info_item['column_arg_type'] == 4:
        column_info_item['column_arg_size'] = 4
        column_info_item['column_arg_data'] = struct.unpack(
            'f', dnt_bytes[seek:seek+4])[0]/100
        seek = seek+4
    elif column_info_item['column_arg_type'] == 5:
        column_info_item['column_arg_size'] = 4
        column_info_item['column_arg_data'] = struct.unpack(
            'f', dnt_bytes[seek:seek+4])[0]
        seek = seek+4
    else:
        return 0
    return seek

def WriteDataType(output_data, arg_data, arg_type):
    '''
    根据arg_type将arg_data填充进output_data
    return: 新的output_data
    '''
    if arg_type == 1:
        if not isinstance(arg_data,str) and isnan(arg_data):
            arg_len = 0
            output_data += struct.pack('H', arg_len)
        else:
            arg_len = len(str(arg_data))
            output_data += struct.pack('H', arg_len)
            output_data += struct.pack(str(arg_len)+'s', str.encode(str(arg_data)))
    elif (arg_type == 2 or arg_type == 3):
        output_data += struct.pack('I', int(arg_data))
    elif arg_type == 4:
        output_data += struct.pack('f', arg_data*100)
    elif arg_type == 5:
        output_data += struct.pack('f', arg_data)
    else:
        return 0
    return output_data

def ReadData(data_frame, column_info, rows, dnt_bytes, seek):
    '''
    读取所有剩余的数据信息
    data_frame:初始数据集
    return: 新的数据集或0(error)
    '''
    data_frame_append_timecost = 0
    fill_data_timecost = 0
    init_series_timecost = 0
    assign_data_timecost = 0
    for row in range(0, rows):
        before_init_series = time.time()
        row_item = {}
        end_init_series = time.time()
        init_series_timecost += (end_init_series - before_init_series)
        for column_info_item in column_info:
            before_fill_data = time.time()
            seek_tmp = FillDataType(column_info_item, dnt_bytes, seek)
            end_fill_data = time.time()
            fill_data_timecost += (end_fill_data - before_fill_data)
            before_assign_data = time.time()
            row_item[column_info_item['column_name']
                     ] = column_info_item['column_arg_data']
            end_assign_data = time.time()
            assign_data_timecost += (end_assign_data-before_assign_data)
            if seek_tmp != 0:
                seek = seek_tmp
            else:
                print('error! row ='+row+' data:\n')
                print(column_info_item)
                return None
        before_append_data = time.time()
        data_frame = data_frame.append(row_item, ignore_index=True)
        end_append_data = time.time()
        data_frame_append_timecost += (end_append_data-before_append_data)
    print('data frame append time cost : '+str(data_frame_append_timecost))
    print('fill data time cost : '+str(fill_data_timecost))
    print('init series time cost : '+str(init_series_timecost))
    print('assign data time cost : '+str(assign_data_timecost))
    return data_frame

def WriteData(data_frame, column_info, output_dnt_file):
    '''
    将data_frame对应的数据保存到output_dnt_file
    column_info : dataframe 类型的数据
    '''
    print(column_info)
    rows = data_frame.shape[0]
    columns = data_frame.shape[1]-1
    output_data = ''
    with open(output_dnt_file, 'wb')as dnt_handle:
        output_data = struct.pack('I', 0)  # 开头4字节
        output_data += struct.pack('H', columns)  # 列
        output_data += struct.pack('I', rows)  # 行
        index = 0
        for column in data_frame.columns:
            if column != '_RowID':
                col_len = len(column)
                output_data += struct.pack('B', col_len)
                output_data += struct.pack('B', 0)
                output_data += struct.pack(str(col_len) +
                                           's', str.encode(column))
                output_data += struct.pack('B',
                                           column_info['column_arg_type'][index])
            index = index + 1

        for row in data_frame.iterrows():
            index = 0
            for column in row[1]:
                output_data = WriteDataType(
                    output_data, column, column_info['column_arg_type'][index])
                index += 1

        dnt_handle.write(output_data)

def WriteToCSV(data_frame, csv_file_name):
    data_frame.to_csv(csv_file_name)

def ConvertDntToCSV(dnt_file_name, csv_file_name):
    read_data_result = ReadDataToDF(dnt_file_name)
    data_frame = read_data_result[0]
    info_frame = read_data_result[1]
    WriteToCSV(data_frame, csv_file_name)
    WriteToCSV(pd.DataFrame(info_frame), 'info_'+csv_file_name)

def ConvertCSVToDnt(csv_file_name, dnt_file_name):
    before_convert_time = time.time()
    data_frame = pd.read_csv(csv_file_name, index_col=0)
    info_data_frame = pd.read_csv('info_'+csv_file_name, index_col=0)
    WriteData(data_frame, info_data_frame, dnt_file_name)
    end_convert_time = time.time()
    print("conver csv to dnt time cost : " +
          str(end_convert_time-before_convert_time))

if __name__ == '__main__':
    ConvertDntToCSV(dnt_file_path, csv_file_path)
    ConvertCSVToDnt(csv_file_path, "test"+dnt_file_path)


from collections import defaultdict
n=int(input())
s=input()
l=0
d=defaultdict(int)
e=defaultdict(int)
se=set(list(s))
a=[]
j=0
for i in range(n):
    d[s[i]]+=1
    if s[i] not in e:
        l+=1
        e[s[i]]=1
    if l==len(se):
        while d[s[j]]>1:
            d[s[j]]-=1
            j+=1
        a.append(i-j+1)
print(min(a))


# coding: utf-8

import nltk
import string
punct = list(string.punctuation)
punct.remove("'")
punct.remove('-')
punct.append('«')
punct.append('»')
punct.append('…')
import treetaggerwrapper
import re

# CHANGE LANGUAGE
tagger = treetaggerwrapper.TreeTagger(TAGLANG='pl')

def french():
    tokenizer = nltk.data.load('tokenizers/punkt/PY3/polish.pickle')
    with open('wiki_pl.txt', 'r', encoding='utf-8') as f:
        with open('polish_wiki_tagged.txt', 'w', encoding='utf-8') as fr:
            for line in f:
                #print(line)
                sentences = tokenizer.tokenize(line.strip().replace('—', ''))
                for sent in sentences:
                    if len(sent) > 10:
                        sent = sent.strip(' ').lower()
                        for p in punct:
                            sent = sent.replace(p, '')
                        # for s in stop:
                        #    sent = re.sub('\b{}\b'.format(s), '', sent)
                        sent = re.sub('\s+', ' ', sent)
                        tags = tagger.tag_text(sent)
                        sent = ' '.join([tag.split('\t')[-1] + '_' + tag.split('\t')[1].split(':')[0] for tag in tags])
                        # do something now!
                        fr.write(sent + '\n')

if __name__ == '__main__':
    # CHANGE FUNCTION
    french()


__author__ = 'sail'
import pandas as pd
from functools import reduce
import numpy as np
import matplotlib.pyplot as plt

def analyse():
    # 该函数用于将一个dataFrame合并为一条记录
    # 入参x为一个dataFrame，调用tt对每个Series分别进行合并
    def t(x):
        return x.apply(tt)

    # 该函数用于对一个Series进行合并,入参x为一个Series
    def tt(x):
        # 当x为标题时用reduce调用lcs函数求从字符串头开始的最长公共子串
        if x.name == '标题':
            return reduce(lcs, x)
        # 当x为下列内容是取最后一项
        elif x.name in {'日期', '粉丝数'}:
            return x[-1]
        # 其他情况时对将x中所有数据合并为一组求和，并返回该组求和结果
        else:
            return x.groupby(lambda x: 1).sum().values[0]

    def lcs(x, y):
        for i in range(min(len(y), len(x))):
            if x[i] != y[i]:
                break
        else:
            i += 1
        return x[:i]

    df = pd.ExcelFile(r"resource/wxgzhdata.xlsx").parse(u"3月")
    df.insert(0, '标题', df['文章标题'])
    df = df.fillna(method="pad").dropna().set_index(['文章标题']).groupby(lambda x: x[:2]).apply(lambda x: x.apply(
        lambda y: {1: lambda z: reduce(lcs, z), 2: lambda z: z[-1], None: lambda z: z.groupby(type).sum().values[0]}
        [{'标题': 1, '日期': 2, '粉丝数': 2}.get(y.name)](y))).set_index(['标题'])
    # 下面为上句的另一种表达方式
    # 用fillna填充nan
    # df=df.fillna(method="pad")
    # 将开头未能被pad填充的nan去除
    # df=df.dropna()
    # 将标题设为index以方便分组
    # df=df.set_index(['文章标题'])
    # 以标题的前两个字为基准分组
    # df=df.groupby(lambda x: x[:2])
    # 利用函数t对每个groupBy进行合并
    # df=df.apply(t)
    # 将各系列内文章标题的相同部分作为系列名称
    # df=df.set_index(['标题'])

    # 从dataframe中取得之后计算需要的Series
    fields = [df.xs('总阅读人数', axis=1), df.xs('初次打开阅读人数', axis=1), df.xs('分享次数', axis=1), df.xs('每日增粉人数', axis=1),
              df.xs('粉丝数', axis=1)]
    to_percent = lambda x: (x * 100).round(1)
    # 各字段计算公式
    expressions = [('初次打开率', lambda f: f[1] / f[4]), ('分享率', lambda f: f[2] / f[0]), ('分享拉粉率', lambda f: f[3] / f[2]),
                   ('增粉速率', lambda f: f[3] / f[4]), ( '阅读涨粉率', lambda f: f[3] / f[0]),
                   ('传播涨粉率', lambda f: f[3] / (f[0] - f[1])), ('二次传播率', lambda f: (f[0] - f[1]) / f[0])]
    # 记录现有字段数量，用于下面计算新字段插入位置以及删除旧字段。
    old_field_count = len(df.columns)
    # 计算各字段的值，处理为percent后，依次插入datagrame中
    for i in enumerate(expressions, start=old_field_count):
        df.insert(i[0], i[1][0], (i[1][1](fields)).apply(to_percent))
    # 转制后将原始字段去除
    df = df.T[old_field_count:]

    df.plot(kind='barh', figsize=(16, 9))

    from pylab import mpl
    mpl.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体
    mpl.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
    plt.show()


import sys
from subprocess import PIPE, Popen
from threading  import Thread
from queue import Queue, Empty

ON_POSIX = 'posix' in sys.builtin_module_names

def enqueue_output(out, queue):
    for line in iter(out.readline, b''):
        queue.put(line)
    out.close()

p = Popen(['ping 8.8.8.8'], stdout=PIPE)
q = Queue()
t = Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True
t.start()

print('Im half way')

try:
    line = q.get_nowait()
except Empty:
    print('no output yet')
else: # got line
    print('hehe')


# EP 2019-1: Escape Insper
#
# Alunos:
# - aluno A: Fulano da Silva, fulanos@insper.edu.br
# - aluno B: Sicrano de Almeida, sicranoa1@insper.edu.br

def carregar_cenarios():
    cenarios = {
        "inicio": {
            "titulo": "Saguao do perigo",
            "descricao": "Voce esta no saguao de entrada do insper",
            "opcoes": {
                "andar professor": "Tomar o elevador para o andar do professor",
                "biblioteca": "Ir para a biblioteca"
            }
        },
        "andar professor": {
            "titulo": "Andar do desespero",
            "descricao": "Voce chegou ao andar da sala do seu professor",
            "opcoes": {
                "inicio": "Tomar o elevador para o saguao de entrada",
                "professor": "Falar com o professor"
            }
        },
        "professor": {
            "titulo": "O monstro do Python",
            "descricao": "Voce foi pedir para o professor adiar o EP. "
                         "O professor revelou que é um monstro disfarçado "
                         "e devorou sua alma.",
            "opcoes": {}
        },
        "biblioteca": {
            "titulo": "Caverna da tranquilidade",
            "descricao": "Voce esta na biblioteca",
            "opcoes": {
                "inicio": "Voltar para o saguao de entrada"
            }
        }
    }
    nome_cenario_atual = "inicio"
    return cenarios, nome_cenario_atual

def main():
    print("Na hora do sufoco!")
    print("------------------")
    print()
    print("Parecia uma boa idéia: vou só jogar um pouquinho/assistir Netflix/"
        "embaçar em geral. Amanhã eu começo o EP. Mas isso não deu certo...")
    print()
    print("É o dia de entregar o EP e você está muuuuito atrasado! Você está "
        "na entrada do Insper, e quer procurar o professor para pedir um "
        "adiamento do EP (boa sorte...)")
    print()

    cenarios, nome_cenario_atual = carregar_cenarios()

    game_over = False
    while not game_over:
        cenario_atual = cenarios[nome_cenario_atual]

        # Aluno A: substitua este comentário pelo código para imprimir
        # o cenário atual.

        opcoes = cenario_atual['opcoes']
        if len(opcoes) == 0:
            print("Acabaram-se suas opções! Mwo mwo mwooooo...")
            game_over = True
        else:

            # Aluno B: substitua este comentário e a linha abaixo pelo código
            # para pedir a escolha do usuário.
            escolha = ""

            if escolha in opcoes:
                nome_cenario_atual = escolha
            else:
                print("Sua indecisão foi sua ruína!")
                game_over = True

    print("Você morreu!")

# Programa principal.
if __name__ == "__main__":
    main()


from bs4 import BeautifulSoup
import re
import requests
import socket

# Connect to the challenge endpoint and read in the data
server_address = ('C01-target.allyourbases.co', 8147)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(server_address)
data: bytes = sock.recv(10000)
data: str = data.decode('utf-8')

# Extract the encrypted key
pattern = r"Protected credential: (.+)"
result = re.search(pattern, data)
key = result.group(1)

# Send Vigenere ciphered key to be auto solved
response = requests.get("https://www.guballa.de/vigenere-solver")
soup = BeautifulSoup(response.text, "html.parser")
form = soup.find("form")
url = form.get("action")
request_token = [
    inp for inp in form.find_all("input")
    if inp.get("name") == "REQUEST_TOKEN"
][0].get("value")
cookie = response.headers.get("Set-Cookie").split(' ')[0]

response = requests.post(
    url,
    data={
        "REQUEST_TOKEN": request_token,
        "cipher": key,
        "variant": "vigenere",
        "lang": "en",
        "key_len": "3-30",
        "break": "Break Cipher"
    },
    headers={"cookie": cookie}
)

# Parse response to retrieve solution
soup = BeautifulSoup(response.text, "html.parser")
div = soup.find(id="vig_clear")
protected_key: str = div.find("textarea").contents[0]
pattern = r"THEPROTECTEDKEYIS(.+)"
result = re.search(pattern, protected_key)
protected_key: str = result.group(1)

# Send plaintext key to challenge endpoint
sock.send(protected_key.encode("utf-8") + b"\n")
data: bytes = sock.recv(1024)
sock.send(b"\n")
data: bytes = sock.recv(1024)
data: str = data.decode("utf-8")

# Extract the flag
pattern = r"Flag\[\w+\]"
result = re.search(pattern, data)
flag = result.group(0)
print(f"[*] Got the flag: {flag}")


from datetime import datetime
from random import sample

from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from rest_framework.authtoken.models import Token
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated

from questdata.serializers import FeedbackSerializer
from users.models import *
from rest_framework.response import Response

from django.shortcuts import render
from django.core.mail import send_mail
from django.contrib.auth import get_user_model
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.contrib.sites.shortcuts import get_current_site
from django.utils.encoding import force_bytes, force_text
from .tokens import account_activation_token
from django.template.loader import render_to_string
# Create your views here.
from users.serializers import UserMetaSerializer
from journeys.serializers import *

from notifications.expo import send_push_message
User = get_user_model()

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create(user=instance)

@api_view(['POST'])
@permission_classes([])
def register(request):
    # Does not check if request is formatted correctly
    em = request.data["email"]
    um = request.data["username"]

    # If the username and email have not been used already
    if User.objects.filter(username=um).count() == 0 and User.objects.filter(email=em).count() == 0:

        # Store user's data into database
        user = User.objects.create_user(request.data["username"],
                                    request.data["email"],
                                    request.data["password"])
        user.is_active = False
        user.save()

        UserMeta(user=user, firstname=request.data["firstname"],
            lastname=request.data["lastname"],
            age=request.data["age"], sex=request.data["sex"]).save()

        current_site = get_current_site(request)

        mail_subject = 'Activate your Thrive account'

        # Confirmation message
        # message = render_to_string('email_template.html', {
        #                 'user': user,
        #                 'domain': current_site.domain,
        #                 'uid': urlsafe_base64_encode(force_bytes(user.pk)),
        #                 'token': account_activation_token.make_token(user),
        #             })

        # Quick fix to skip confirmation email
        activate(
            None,
            urlsafe_base64_encode(force_bytes(user.pk)),
            account_activation_token.make_token(user)
        )

        # The new user's email
        to_email = em

        # Send confirmation email to user. Does not check whether the email was
        # sent or not. Note that it does not pass an authorization username nor
        # a password to actually authorize the sending of the confirmation email
        #
        # send_mail(mail_subject, message, 'bell woods email @ ...', [to_email])
        return Response({"response" : 'Please confirm your email address to complete the registration'})
    else:

        # This user already exists, response should be more specific
        return Response({"response" : 'Unsuccessful'})

# Confirm user on data base, a user cannot login without this
def activate(request, uidb64, token):
    User = get_user_model()
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = User.objects.get(pk=uid)
    except(TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None

    # Checks that activation token
    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.save()
        return render(request, 'confirmed.html')
    else:
        return render(request, 'timout.html')

@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def get_preferred_notification_time(request):
    return Response(
        {"time": UserMeta.objects.get(user=request.user).pref_alert_time})

@api_view(['POST'])
@permission_classes([])
def send_buddy_request(request):
    f = request.data["from"]
    t = request.data["to"]
    fu = User.objects.get(username=f)
    for friend in fu.buddy.all():
        if friend.username == t:
            return Response({"response" : 'Already buddy!'})
    if User.objects.filter(username=t).count() > 0:
        tu = User.objects.get(username=t)
        if tu != fu:
            if Buddy_Request.objects.filter(from_user=fu, to_user=tu):
                return Response({"response" : 'Buddy request already sent!'})

            elif Buddy_Request.objects.filter(from_user=tu, to_user=fu):
                buddy_request = Buddy_Request.objects.get(from_user=tu, to_user=fu)
                fu.buddy.add(tu)
                tu.buddy.add(fu)
                buddy_request.delete()
                return Response({"response" : 'Buddies!'})

            else:
                buddy_request = Buddy_Request(from_user=fu, to_user=tu)
                buddy_request.save()
                if tu.expo_token != "" and tu.expo_token is not None:
                    send_push_message(tu.expo_token, "You got a buddy request from " + fu.username + "!")
                return Response({"response" : 'Buddy request sent!'})
        else:
            return Response({"response" : 'Same User'})
    else:
        return Response({"response" : 'No such user'})

@api_view(['POST'])
@permission_classes([])
def accept_buddy_request(request):
    f = request.data["from"]
    t = request.data["to"]
    fu = User.objects.get(username=f)
    tu = User.objects.get(username=t)
    buddy_request = Buddy_Request.objects.get(from_user=fu, to_user=tu)
    fu.buddy.add(tu)
    tu.buddy.add(fu)
    buddy_request.delete()
    if Buddy_Request.objects.filter(from_user=tu, to_user=fu):
        buddy_request = Buddy_Request.objects.get(from_user=tu, to_user=fu)
        buddy_request.delete()
    return Response({"response" : 'Buddy Request Accepted!'})

@api_view(['POST'])
@permission_classes([])
def reject_buddy_request(request):
    f = request.data["from"]
    t = request.data["to"]
    fu = User.objects.get(username=f)
    tu = User.objects.get(username=t)
    buddy_request = Buddy_Request.objects.get(from_user=fu, to_user=tu)
    buddy_request.delete()
    return Response({"response" : 'Buddy Request Rejected!'})

@api_view(['POST'])
@permission_classes([])
def fetch_buddy_request(request):
    u = request.data["user"]
    user = User.objects.get(username=u)

    brs = Buddy_Request.objects.filter(to_user=user)
    buddy_requests = []

    for br in brs:
        buddy_requests.append(br.from_user.username)

    return Response({"requests": buddy_requests})

@api_view(['POST'])
@permission_classes([])
def fetch_buddy(request):
    username = request.data["user"]

    user = User.objects.get(username=username)
    possible = user.buddy.all()
    buddies = []
    for bud in possible:
        buddies.append(bud.username)

    return Response({"requests": buddies})

@api_view(['POST'])
@permission_classes((IsAuthenticated,))
def set_preferred_notification_time(request):
    meta = UserMeta.objects.get(user=request.user)
    meta.pref_alert_time = datetime.strptime(request.data["time"],
                                             "%H:%M:%S").time()
    meta.save()
    return Response({"time": meta.pref_alert_time})

@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def get_user_meta(request):
    meta = UserMeta.objects.get(user=request.user)

    serializer = UserMetaSerializer(instance=meta)
    return Response(serializer.data)

# @api_view(['GET'])
# @permission_classes((IsAuthenticated,))
# def set_user_meta(request):
#     meta = UserMeta.objects.get(user=request.user)
#
#     serializer = UserMetaSerializer(instance=meta)
#     return Response(serializer.data)

@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def is_admin(request):
    # return Response({"status": request.user.is_staff})
    return Response({"status": request.user.is_staff})

@api_view(['POST', 'DELETE'])
@permission_classes((IsAuthenticated,))
def active_journeys(request, jid):
    # username = request.user.username
    user = request.user
    if request.method == "POST":
        user.active_journeys.add(Journey.objects.get(id=jid))
        return Response({"response": 'Success'})
    elif request.method == "DELETE":
        user.active_journeys.remove(Journey.objects.get(id=jid))
        return Response({"response": 'Success'})

@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def get_active_journeys(request):
    user = request.user
    if request.method == "GET":
        lst = user.active_journeys.all()
        jsonObj = []
        for j in lst:
            data = JourneySerializer(instance=j).data
            quests = []
            for i in range(1, len(data["quests"])+1):
                jq = JourneyQuests.objects.get(journey=j, order=i)
                quests.append(jq.quest)
            Qserializer = QuestSerializer(quests, many=True)
            data['quests'] = Qserializer.data
            jsonObj.append(data)
        return Response(jsonObj)

@api_view(['GET'])
@permission_classes([IsAuthenticated])
def get_random_non_buddy(request):
    """Searches for a random user that is not a buddy of the requesting user
    and responds the username for that user.

    The purpose of this is to allow for the random matching of accountability
    buddies without giving any user access to all user data.
    """
    related_users = [request.user] + list(request.user.buddy.all())
    non_buddy_usernames = User.objects.exclude(username__in=related_users) \
                                      .values_list('username', flat=True)

    if not non_buddy_usernames.exists():
        # There are no users who are not buddies with this user
        return Response({"details": "No random buddy match."})

    # Take a random username from this list, as it is non-empty
    random_match = sample(list(non_buddy_usernames), 1).pop()
    return Response({"username": random_match})

@api_view(['POST'])
@permission_classes([IsAuthenticated])
def set_expo_token(request):
    user = request.user

    user.expo_token = request.data["expo_token"]
    user.save()
    response = {
            "details": "Expo token added to table!"
        }

    return Response(response, status=200)


#!/usr/bin/env python3
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

import os

file_header = '''/*

Copyright (c) 2017-2018, Steven Siloti
Copyright (c) 2017-2021, Arvid Norberg
Copyright (c) 2020, Alden Torres
All rights reserved.

You may use, distribute and modify this code under the terms of the BSD license,
see LICENSE file.

*/

#ifndef TORRENT_FWD_HPP
#define TORRENT_FWD_HPP

#include "libTAU/config.hpp"

namespace libTAU {
'''

file_footer = '''

}

namespace lt = libTAU;

#endif // TORRENT_FWD_HPP
'''

classes = os.popen(
    r'git grep "\(TORRENT_EXPORT\|TORRENT_DEPRECATED_EXPORT\|^TORRENT_[A-Z0-9]\+_NAMESPACE\)"').read().split('\n')

def print_classes(out, classes, keyword):
    current_file = ''

    # [(file, decl), ...]
    classes = [(x.split(':')[0].strip(), ':'.join(x.split(':')[1:]).strip()) for x in classes]

    # we only care about header files
    # ignore the forward header itself, that's the one we're generating
    # also ignore any header in the aux_ directory, those are private
    classes = [x for x in classes if x[0].endswith('.hpp') and not x[0].endswith('/fwd.hpp') and '/aux_/' not in x[0]]

    namespaces = ['TORRENT_VERSION_NAMESPACE_4',
                  'TORRENT_VERSION_NAMESPACE_4_END',
                  'TORRENT_VERSION_NAMESPACE_3',
                  'TORRENT_VERSION_NAMESPACE_3_END',
                  'TORRENT_VERSION_NAMESPACE_2',
                  'TORRENT_VERSION_NAMESPACE_2_END',
                  'TORRENT_CRYPTO_NAMESPACE',
                  'TORRENT_CRYPTO_NAMESPACE_END']

    # only include classes with the right kind of export
    classes = [
        x for x in classes if x[1] in namespaces or (
            x[1].split(' ')[0] in [
                'class',
                'struct'] and x[1].split(' ')[1] == keyword)]

    # collapse empty namespaces
    classes2 = []
    skip = 0
    for i in range(len(classes)):
        if skip > 0:
            skip -= 1
            continue
        if classes[i][1] in namespaces \
                and len(classes) > i + 1 \
                and classes[i + 1][1] == ('%s_END' % classes[i][1]):
            skip = 1
        else:
            classes2.append(classes[i])

    classes = classes2

    idx = -1
    for line in classes:
        idx += 1
        this_file = line[0]
        decl = line[1].split(' ')

        content = ''
        if this_file != current_file:
            out.write('\n// ' + this_file + '\n')
        current_file = this_file
        if len(decl) > 2 and decl[0] in ['struct', 'class']:
            decl = decl[0] + ' ' + decl[2]
            if not decl.endswith(';'):
                decl += ';'
            content = decl + '\n'
        else:
            content = line[1] + '\n'

        if 'kademlia' in this_file:
            out.write('namespace dht {\n')
            out.write(content)
            out.write('}\n')
        else:
            out.write(content)

try:
    os.remove('include/libTAU/fwd.hpp')
except FileNotFoundError:
    pass

with open('include/libTAU/fwd.hpp', 'w+') as f:
    f.write(file_header)

    print_classes(f, classes, 'TORRENT_EXPORT')

    f.write('\n#if TORRENT_ABI_VERSION <= 2\n')

    print_classes(f, classes, 'TORRENT_DEPRECATED_EXPORT')

    f.write('\n#endif // TORRENT_ABI_VERSION')

    f.write(file_footer)


import time

# input investment amount
amount = float(input("Enter amount:N "))
# 10 percent convert to decimal
percentage = 0.10  # 10% profit per 5 minutes

while True:
    # multiply amount and percentage
    total = amount * percentage

    # Update the total investment amount
    amount += total

    # Print the current investment amount
    print(f" 10% add to amount: N{amount:,.2f}")

    # Wait for 5 hours
    time.sleep(20)  # 5 minutes


#! /usr/bin/env python
#coding=utf-8
from django.conf.urls import patterns, url
from albums.views import albumdetailview
urlpatterns = patterns('albums.views',
    url(r'^add/$', 'add'),
    url(r'^$', 'indexview'),
    url(r'^(?P<album_id>\d+)/$', albumdetailview),
    url(r'^genre/(?P<genre_name>.+)/$', 'genre'),
    url(r'^search/(?P<key_word>.+)/$', 'search'),
    url(r'^search/$', 'search'),
    )


from decimal import Decimal

from django.test import TestCase

from order.models import Order, OrderProduct, Parcel
from shop.models import Product, Address, Category

class OrderModelTest(TestCase):
    def setUp(self):
        category = Category.objects.create(name='Books', slug='books')
        self.product = Product.objects.create(category=category,
                                name='Django 3',
                                slug='django_3',
                                description='practical web application development',
                                price=69.99,
                                available=True,
                                quantity_available=13)
        self.product_2 = Product.objects.create(category=category,
                                name='Peak',
                                slug='peak',
                                description='Anyone who wants to get better at anything should read Peak',
                                price=49.99,
                                available=True,
                                quantity_available=4)
        Parcel.objects.create(street='Krotka', street_number='3', ZIP_code='08-116', town='Seroczyn', country='Poland', name='SER1', longitude='54.21212', latitude='31.21424')

        self.order = Order.objects.create(first_name='Alek', last_name='Wiedenski', street='Krotka', street_number='3', ZIP_code='08-116', town='Seroczyn', country='Poland')

        self.order_product = OrderProduct.objects.create(order=self.order, product=self.product, quantity=3)
        self.order_product_2 = OrderProduct.objects.create(order=self.order, product=self.product_2, quantity=2)

    def test_Order(self):
        self.assertEqual(self.order.first_name, 'Alek')

    def test_Order_Product(self):
        self.assertEqual(self.order_product.quantity, 3)

    def test_Order_Product_total_price(self):
        self.assertEqual(self.order_product.total_price(), self.product.price * self.order_product.quantity)

    def test_Order_total_price(self):
        self.assertEqual(self.order.total_price(), Decimal(str((self.product.price * self.order_product.quantity) + (self.product_2.price * self.order_product_2.quantity))))

    def test_Parcel(self):
        parcels = Parcel.objects.count()

        self.assertEqual(parcels, 1)


from django.db import models
from django.utils import timezone
from . import producer as benchmark_producer
from . import models as benchmark_models
from . import consumers as benchmark_consumers

def on_new_benchmark(
    sender: models.Model,
    instance: benchmark_models.Benchmark,
    **kwargs
) -> None:
    """Publish a new benchmark to the event bus and created a benchmark
    progress record.

    Args:
        instance: The benchmark to publish.
    """
    benchmark_models.BenchmarkProgress.objects.create(
        benchmark=instance
    ).save()
    benchmark_producer.new_benchmark(instance)

def on_benchmark_progress_update(
    sender: models.Model,
    instance: benchmark_models.BenchmarkProgress,
    **kwargs
) -> None:
    status_choices = benchmark_models.BenchmarkProgress.StatusChoices

    # Update the benchmark time logs.
    loggable_statues = (
        status_choices.PROVISIONING,
        status_choices.SCHEDULING,
        status_choices.COMPLETED,
    )
    if instance.status not in loggable_statues:
        pass
    if instance.status == status_choices.PROVISIONING:
        instance.benchmark.started_on = timezone.now()
    elif instance.status == status_choices.SCHEDULING:
        instance.benchmark.scheduled_on = timezone.now()
    elif instance.status == status_choices.COMPLETED:
        instance.benchmark.completed_on = timezone.now()
    instance.benchmark.save()

    # Send notification with new status.
    benchmark_consumers.BenchmarkProgressConsumer.send_status_update(
        site_id=instance.benchmark.site_id,
        benchmark_id=instance.benchmark_id,
        status=instance.get_status_display(),
        num_servers=instance.benchmark.num_servers,
        num_requests=instance.benchmark.num_requests,
        completed_requests=instance.benchmark.completed_requests,
        failed_requests=instance.benchmark.failed_requests,
        created_on=instance.benchmark.created_on,
        scheduled_on=instance.benchmark.scheduled_on,
        min_time=instance.benchmark.min_time,
        mean_time=instance.benchmark.mean_time,
        max_time=instance.benchmark.max_time,
    )

benchmark_models.NEW_BENCHMARK.connect(
    on_new_benchmark,
    benchmark_models.Benchmark
)

benchmark_models.UPDATED_BENCHMARK_PROGRESS.connect(
    on_benchmark_progress_update,
    benchmark_models.BenchmarkProgress
)


from time import sleep, time
import threading

class Scheduler:
    def __init__(self):
        self.fns = []
        self.lock = threading.RLock()
        self.condition = threading.Condition(self.lock)
        self.thread = threading.Thread(target=self.schedule)
        self.thread.start()

    def schedule(self):
        while True:
            now = time() * 1000

            with self.lock:
                to_run = [fn for fn, due in self.fns if due <= now]
                self.fns = [(fn, due) for (fn, due) in self.fns if due > now]

            for fn in to_run:
                fn()

            with self.lock:
                if not self.fns:
                    self.condition.wait()
                else:
                    ms_remaining = min(due for fn, due in self.fns) - time()*1000
                    if ms_remaining > 0:
                        self.condition.wait(ms_remaining / 1000)

    def add_function(self, f, n):
        with self.lock:
            self.fns.append((f, time() * 1000 + n))
            self.condition.notify_all()

scheduler = Scheduler()
scheduler.add_function(lambda: print("Print"), 10)
scheduler.add_function(lambda: print("Here"), 20)

Daily coding problems/Problem 10 #Medium/problem10.py


import os
from PIL import Image
from torch.utils.data import Dataset

class myDataset(Dataset):
    def __init__(self, data_dir, transform):
        # data_dir：图片保存路径
        # transform：初始化方法
        self.data_dir = data_dir
        self.transform = transform
        # 训练集图片为png格式，若为jpg格式，可对应修改为x.endswith("jpg")
        self.img_names = [name for name in list(filter(lambda x: x.endswith(".png"), os.listdir(self.data_dir)))]

    def __getitem__(self, index):
        path_img = os.path.join(self.data_dir, self.img_names[index])
        img = Image.open(path_img).convert('RGB')

        if self.transform is not None:
            img = self.transform(img)

        return img

    def __len__(self):
        if len(self.img_names) == 0:
            raise Exception("\ndata_dir:{} is a empty dir! Please checkout your path to images_wgan_gp!".format(self.data_dir))
        return len(self.img_names)


# 징검다리
# https://programmers.co.kr/learn/courses/30/lessons/43236

def solution(distance, rocks, n):
    answer = float('inf')
    rockAmount = len(rocks)
    if(n==rockAmount):
        return distance

    rocks.append(0)
    rocks.append(distance)
    rocks.sort()
    dists = [rocks[i+1]-rocks[i] for i in range(len(rocks)-1)]
    left = min(dists)
    right = max(dists)

    while (left<=right):
        mid = (left+right)//2
        newDists = dists[:]
        j = 0
        count = 0
        while(j<len(newDists)):
            if(newDists[j] <= mid):
                if(j+1<len(newDists)):
                    newDists[j] += newDists[j+1]
                    del newDists[j+1]
                count += 1
            else:
                j += 1
        if (count>n):
            if(mid < answer):
                answer = mid
            right = mid - 1
        else:
            left = mid + 1

    return answer

print(solution(25, [2, 14, 11, 21, 17], 2))