import os
import glob
import pickle
import copy
import numpy as np
import pandas as pd

from preprocess import preProcess

dtypes = [
    {'ID': "Int64", '注册时间': "Int64", '注册资本': np.float64, '行业': object, '区域': object,
        '企业类型': object, '控制人类型': object, '控制人持股比例': np.float64, 'flag': "Int64"},
    "Int64",
    {'ID': "Int64", 'year': "Int64", '债权融资额度': np.float64, '债权融资成本': np.float64, '股权融资额度': np.float64, '股权融资成本': np.float64,
        '内部融资和贸易融资额度': np.float64, '内部融资和贸易融资成本': np.float64, '项目融资和政策融资额度': np.float64, '项目融资和政策融资成本': np.float64},
    {'ID': "Int64", 'year': np.float64, '从业人数': np.float64, '资产总额': np.float64, '负债总额': np.float64, '营业总收入': np.float64, '主营业收入': np.float64, '利润总额': np.float64, '净利润': np.float64, '纳税总额': np.float64, '所有者权益合计': np.float64}]

model_path = "/Users/sameal/Documents/PROJECT/zombie_enterprise/code/model/XGBClassifier_web.pickle"

def get_file_path(dir_path):
    base_path = glob.glob(os.path.join(dir_path, "*base*.csv"))[0]
    knowledge_path = glob.glob(os.path.join(dir_path, "*knowledge*.csv"))[0]
    money_path = glob.glob(os.path.join(dir_path, "*money*.csv"))[0]
    year_path = glob.glob(os.path.join(dir_path, "*year*.csv"))[0]
    return (base_path, knowledge_path, money_path, year_path)

def load_data(dir_path, index):
    file_path = get_file_path(dir_path)
    data = []
    for i in index:
        table = pd.read_csv(file_path[i], encoding='gbk', dtype=dtypes[i])
        table.set_index("ID", inplace=True)
        data.append(table)
    return data

def year_fea(yb_df):
    tmp = yb_df["净利润"] / yb_df["营业总收入"]
    yb_df["销售净利率"] = tmp.replace([-np.inf, np.inf], np.nan)
    tmp = yb_df["净利润"] / yb_df["资产总额"]
    yb_df["资产净利率"] = tmp.replace([-np.inf, np.inf], np.nan)
    tmp = yb_df["负债总额"] / yb_df["资产总额"]
    yb_df["资产负债率"] = tmp.replace([-np.inf, np.inf], np.nan)
    tmp = yb_df["净利润"] / yb_df["所有者权益合计"]
    yb_df["资金收益率"] = tmp.replace([-np.inf, np.inf], np.nan)

def portrait(data):
    base, knowledge, money, year = copy.deepcopy(data)
    base["注册资本"] = base["注册资本"] / 10

    creat_label = knowledge.sum(axis=1)
    creat_label.name = "创新能力"

    year_fea(year)
    year_mean = year.groupby('ID').mean().drop(columns="year")
    yb_mean = pd.concat([base, year_mean], axis=1)

    def scale_classify(row):
        if row["行业"] == "交通运输业":
            x, y = row["从业人数"], row["营业总收入"]
            return ((x >= 20) + (y >= 100) + (x >= 300) + (y >= 3000) + (x >= 1000) + (y >= 30000)) // 2
        if row["行业"] == "工业":
            x, y = row["从业人数"], row["营业总收入"]
            return ((x >= 20) + (y >= 300) + (x >= 300) + (y >= 2000) + (x >= 1000) + (y >= 40000)) // 2
        if row["行业"] == "零售业":
            x, y = row["从业人数"], row["营业总收入"]
            return ((x >= 10) + (y >= 100) + (x >= 50) + (y >= 500) + (x >= 300) + (y >= 20000)) // 2
        if row["行业"] in ["服务业", "商业服务业", "社区服务"]:
            x, y = row["从业人数"], row["资产总额"]
            return ((x >= 10) + (y >= 100) + (x >= 100) + (y >= 8000) + (x >= 300) + (y >= 120000)) // 2
        x = row["从业人数"]
        return (x >= 10) + (x >= 100) + (x >= 300)

    scale_label = yb_mean.apply(scale_classify, axis=1)
    scale_label.name = "企业规模"
    scales = ["微型企业", "小型企业", "中型企业", "大型企业"]
    yb_mean["企业规模"] = scale_label.apply(lambda row: scales[row])

    sel_columns = ["净利润", "纳税总额", "销售净利率", "资产净利率", "资产负债率", "资金收益率"]
    cut_bins = {
        "净利润": [-np.inf, -1e4, 0,  1e4, np.inf],
        "纳税总额": [-np.inf, 0, 1e4, 1e5, np.inf],
        "销售净利率": [-np.inf, -0.1, 0, 0.2, np.inf],
        "资产净利率": [-np.inf, -0.2, 0, 0.4, np.inf],
        "资产负债率": [-np.inf, 0.4, 0.6, 2, np.inf],
        "资金收益率": [-np.inf, -1, 0, 1, np.inf]
    }
    sel_df = year_mean[sel_columns]
    sel_df = sel_df.apply(lambda col: pd.cut(col, cut_bins[col.name], labels=[0, 1, 2, 3])).astype(int)
    label_df = pd.concat([base[["注册时间", "注册资本", "行业", "区域", "企业类型"]], scale_label, creat_label, sel_df], axis=1)
    label_columns = sel_columns + ["企业规模", "创新能力"]
    label_df[label_columns] = label_df[label_columns].fillna(4).astype(int)
    return yb_mean, label_df

def analyse(dir_path):
    data = load_data(dir_path, range(4))
    mean_df, label_df = portrait(data)
    mean_df.to_csv(os.path.join(dir_path, "chart_data.csv"), header=True, index=True, index_label="ID")
    label_df.to_csv(os.path.join(dir_path, "portrait.csv"), header=True, index=True, index_label="ID")

def predict(dir_path):
    base, year = load_data(dir_path, (0, 3))
    year = year.drop(columns="year").groupby("ID").mean()
    year_fea(year)
    union_table = pd.concat([base["注册资本"], year], axis=1, join_axes=[base.index])

    with open(model_path, 'rb') as f:
        model = pickle.load(f)
    pred_flag = model.predict(union_table)
    res = pd.Series(pred_flag, index=union_table.index, name="pred_flag")
    save_path = os.path.join(dir_path, "result.csv")
    res.to_csv(save_path, header=True, index=True, index_label="ID")

def search(dir_path, search_id):
    portrait_path = os.path.join(dir_path, "portrait.csv")
    flag_path = os.path.join(dir_path, "result.csv")
    label_df = pd.read_csv(portrait_path)
    label_df.set_index("ID", inplace=True, drop=False)
    flag_df = pd.read_csv(flag_path)
    flag_df.set_index("ID", inplace=True)
    search_id = int(search_id)
    if not search_id in label_df.index:
        return None
    res_label = label_df.loc[search_id]
    res_label["flag"] = int(flag_df.loc[search_id].values[0] > 0)
    return res_label.to_json()

def chart(dir_path, search_id, byclass):
    chart_path = os.path.join(dir_path, "chart_data.csv")
    chart_df = pd.read_csv(chart_path)
    chart_df.set_index("ID", inplace=True)
    search_id = int(search_id)
    if not search_id in chart_df.index:
        return None
    columns = ["从业人数", "资产总额", "负债总额", "营业总收入", "主营业务收入", "利润总额", "净利润", "纳税总额", "所有者权益合计", "销售净利率", "资产净利率", "资产负债率", "资金收益率"]
    row = chart_df.loc[search_id]
    if byclass == "all":
        tmp = chart_df[columns]
    elif byclass == "flag":
        flag_path = os.path.join(dir_path, "result.csv")
        flag_df = pd.read_csv(flag_path)
        flag_df.set_index("ID", inplace=True)
        search_flag = flag_df.loc[search_id].values[0]
        ids = flag_df.index[flag_df.pred_flag == search_flag]
        tmp = chart_df.loc[ids]
        tmp = tmp[columns]
    else:
        tmp = chart_df[chart_df[byclass] == row[byclass]]
        tmp = tmp[columns]
    mean = tmp.mean()
    std = tmp.std()
    row = row[columns]
    res = (row - mean) / std
    return res.to_json(orient="values")

if __name__ == "__main__":
    predict("/Users/sameal/Documents/PROJECT/zombie_enterprise/web/uploads/33f9bb61")


import requests
import traceback
import pydub
import io
from pydub import AudioSegment
from config import Config

config=Config()

WIT_API_TOKEN=config.wit_api_token

def __transcribe_chunk(chunk, lang):
  if lang not in ['en']:
    return None

  headers = {
    'authorization': 'Bearer ' + WIT_API_TOKEN,
    'accept': 'application/vnd.wit.20180705+json',
    'content-type': 'audio/raw;encoding=signed-integer;bits=16;rate=8000;endian=little',
  }

  text = None
  try:
    request = requests.request(
      "POST",
      "https://api.wit.ai/speech",
      headers=headers,
      params = {'verbose': True},
      data=io.BufferedReader(io.BytesIO(chunk.raw_data))
    )

    res = request.json()

    if '_text' in res:
      text = res['_text']
    elif 'text' in res:  # Changed in may 2020
      text = res['text']

  except Exception as e:
    traceback.print_exc()
    return None

  return text

def __generate_chunks(segment, length=20000/1001, split_on_silence=False, noise_threshold=-36):
  chunks = list()
  if split_on_silence is False:
    for i in range(0, len(segment), int(length*1000)):
      chunks.append(segment[i:i+int(length*1000)])
  else:
    while len(chunks) < 1:
      chunks = pydub.silence.split_on_silence(segment, noise_threshold)
      noise_threshold += 4

    for i, chunk in enumerate(chunks):
      if len(chunk) > int(length*1000):
        subchunks = __generate_chunks(chunk, length, split_on_silence, noise_threshold+4)
        chunks = chunks[:i-1] + subchunks + chunks[i+1:]

  return chunks

def __preprocess_audio(audio):
  return audio.set_sample_width(2).set_channels(1).set_frame_rate(8000)

def transcribe(path, lang):
  audio = AudioSegment.from_file(path)

  chunks = __generate_chunks(__preprocess_audio(audio))

  for i, chunk in enumerate(chunks):
    r = __transcribe_chunk(chunk, lang)

    if r is not None:
      yield r


_base_ = [
    "../base/datasets/n_c_h256w512.py",
    "../base/default_runtime.py",
    "../base/models/ensegv4_swin.py",
    "../base/schedules/schedule_160k.py",
]
optimizer_config = dict(type="Fp16OptimizerHook", loss_scale=512.0)
fp16 = dict()
data = dict(samples_per_gpu=4, workers_per_gpu=2)

'''###____pretty_text____###'''

'''
False to Parse'''


"""This module handles the logic required for applying manifest files to the server."""
from pprint import pprint
from typing import Dict, List, Tuple, Optional

from deepdiff import DeepDiff

from fidesctl.cli.utils import handle_cli_response
from fidesctl.core import api
from fidesctl.core.api_helpers import get_server_resources
from fidesctl.core.utils import echo_green
from fideslang import FidesModel, Taxonomy

def sort_create_update_unchanged(
    manifest_resource_list: List[FidesModel],
    server_resource_list: List[FidesModel],
    diff: bool = False,
) -> Tuple[List[FidesModel], List[FidesModel], List[FidesModel]]:
    """
    Check the contents of the resource lists and populate separate
    new lists for resource creation, updating, or no change.

    The `diff` flag will print out the differences between the server resources
    the local resource files.
    """
    server_resource_dict = {
        server_resource.fides_key: server_resource
        for server_resource in server_resource_list
    }

    create_list = []
    update_list = []
    unchanged_list = []
    for manifest_resource in manifest_resource_list:
        resource_key = manifest_resource.fides_key

        # Check if the resource's fides_key matches one from the server
        if resource_key in server_resource_dict.keys():
            server_resource = server_resource_dict[resource_key]

            if manifest_resource == server_resource:
                unchanged_list.append(manifest_resource)
            else:
                if diff:
                    print(
                        f"\nUpdated resource with fides_key: {manifest_resource.fides_key}"
                    )
                    pprint(DeepDiff(server_resource, manifest_resource))
                update_list.append(manifest_resource)

        else:
            if diff:
                print(f"\nNew resource with fides_key: {manifest_resource.fides_key}")
                pprint(manifest_resource)
            create_list.append(manifest_resource)

    return create_list, update_list, unchanged_list

def execute_create_update_unchanged(
    url: str,
    headers: Dict[str, str],
    resource_type: str,
    create_list: Optional[List[FidesModel]] = None,
    update_list: Optional[List[FidesModel]] = None,
    unchanged_list: Optional[List[FidesModel]] = None,
) -> None:
    """
    Create, update, or just log resources based on which list they're in.
    """
    create_list = create_list or []
    update_list = update_list or []

    for create_resource in create_list:
        handle_cli_response(
            api.create(
                url=url,
                headers=headers,
                resource_type=resource_type,
                json_resource=create_resource.json(exclude_none=True),
            ),
            verbose=False,
        )
    for update_resource in update_list:
        handle_cli_response(
            api.update(
                url=url,
                headers=headers,
                resource_type=resource_type,
                resource_id=update_resource.fides_key,
                json_resource=update_resource.json(exclude_none=True),
            ),
            verbose=False,
        )

def echo_results(action: str, resource_type: str, resource_list: List) -> None:
    """
    Echo out the results of the apply.
    """
    echo_green(f"{action.upper()} {len(resource_list)} {resource_type} resources.")

def apply(
    url: str,
    taxonomy: Taxonomy,
    headers: Dict[str, str],
    dry: bool = False,
    diff: bool = False,
) -> None:
    """
    Apply the current manifest file state to the server.
    Excludes systems and registries.
    """
    for resource_type in taxonomy.__fields_set__:
        # Doing some echos here to make a pretty output
        print("-" * 10)
        echo_green(f"Processing {resource_type} resources...")
        resource_list = getattr(taxonomy, resource_type)

        existing_keys = [resource.fides_key for resource in resource_list]
        server_resource_list = get_server_resources(
            url, resource_type, existing_keys, headers
        )

        # Determine which resources should be created, updated, or are unchanged
        create_list, update_list, unchanged_list = sort_create_update_unchanged(
            resource_list, server_resource_list, diff
        )

        if dry:
            echo_results("would create", resource_type, create_list)
            echo_results("would update", resource_type, update_list)
            echo_results("would skip", resource_type, unchanged_list)
        else:
            execute_create_update_unchanged(
                url,
                headers,
                resource_type,
                create_list,
                update_list,
                unchanged_list,
            )

            echo_results("created", resource_type, create_list)
            echo_results("updated", resource_type, update_list)
            echo_results("skipped", resource_type, unchanged_list)
    print("-" * 10)


import os

#import PIL
#print(PIL.PILLOW_VERSION)

import pandas as pd
import numpy as np
#import time
#import json
import copy
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from PIL import Image

from torch.utils.data import TensorDataset, DataLoader,Dataset

from collections import OrderedDict
import torch
from torch import nn, optim
from torch.optim import lr_scheduler
from torch.autograd import Variable
from torchvision import datasets, models, transforms
from torch.utils.data.sampler import SubsetRandomSampler

from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder

import os
print(os.listdir("../input"))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
train_df = pd.read_csv("../input/train.csv")
le = LabelEncoder()
train_df['target'] = le.fit_transform(train_df['Id'])
train_df.head()
class HW_Dataset(Dataset):
    def __init__(self,filepath,train_df,transform=None):
        self.file_path = filepath
        self.df = train_df
        self.transform = transform
        self.image_list = [x for x in os.listdir(self.file_path)]

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

    def __getitem__(self,idx):
        img_path = os.path.join(self.file_path,self.df.Image[idx])
        img = Image.open(img_path).convert('RGB')
        img = self.transform(img)
        target = self.df.target[idx]
        return (img,target)
transform = transforms.Compose([transforms.RandomResizedCrop(224),
                                transforms.RandomHorizontalFlip(),
                                transforms.ToTensor(),
                                transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ])
train_dataset = HW_Dataset(filepath='../input/train/',train_df=train_df,transform=transform)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=8, num_workers=0)
model = models.resnet18(pretrained=True)
for param in model.parameters():
    param.requires_grad = False
from collections import OrderedDict
classifier = nn.Sequential(OrderedDict([
                          ('do1', nn.Dropout(0.2)),
                          ('fc1', nn.Linear(512, 5005))
                          ]))

model.fc = classifier

model = model.to(device)

criterion = nn.CrossEntropyLoss()

# Observe that all parameters are being optimized
optimizer = optim.Adam(model.fc.parameters(), lr=0.001)

# Decay LR by a factor of 0.1 every 7 epochs
scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
n_epochs = 30
valid_loss_min = np.Inf
#best_model_wts = copy.deepcopy(model.state_dict())

for epoch in range(1, n_epochs+1):
    # keep track of training and validation loss
    train_loss = 0.0
    valid_loss = 0.0
    train_acc = 0.0
    valid_acc = 0.0
    ###################
    # train the model #
    ###################
    model.train()

    scheduler.step()
    running_loss = 0.0
    running_corrects = 0

    for data, target in train_loader:
        # move tensors to GPU if CUDA is available
        #print(target.data)
        data, target = data.to(device), target.to(device)

        # clear the gradients of all optimized variables
        optimizer.zero_grad()

        # forward pass: compute predicted outputs by passing inputs to the model
        output = model(data)
        _, preds = torch.max(output, 1)
        # calculate the batch loss
        loss = criterion(output, target)
        # backward pass: compute gradient of the loss with respect to model parameters
        loss.backward()
        # perform a single optimization step (parameter update)
        optimizer.step()
        # update training loss
        running_loss += loss.item() * data.size(0)
        running_corrects += torch.sum(preds == target.data)
    epoch_loss = running_loss / len(train_dataset)
    epoch_acc = running_corrects.double() / len(train_dataset)

    ######################
    # validate the model #
    ######################
    #model.eval()
    #for data, target in data_loader['valid']:
    #    # move tensors to GPU if CUDA is available
    #    data, target = data.to(device), target.to(device)
    #    # forward pass: compute predicted outputs by passing inputs to the model
    #    output = model(data)
    #    _, preds = torch.max(output, 1)
    #    # calculate the batch loss
    #    loss = criterion(output, target)
    #    # update average validation loss
    #    valid_loss += loss.item()*data.size(0)
    #    valid_acc += torch.sum(preds == target.data)

    # calculate average losses
    #train_loss = train_loss/len(data_loader['train'].dataset)
    #valid_loss = valid_loss/len(data_loader['valid'].dataset)

    #train_acc = train_acc.double()/len(data_loader['train'].dataset)
    #valid_acc = valid_acc.double()/len(data_loader['valid'].dataset)

    # print training/validation statistics
    #print('Epoch: {} \t{:.6f} \t {:.6f} \t {:.0%} \t {:.0%}'.format( epoch, train_loss, valid_loss, train_acc, valid_acc))
    print('Epoch: {} \t{:.6f} \t {:.0%}'.format( epoch, epoch_loss, epoch_acc))
    # save model if validation loss has decreased
    #if valid_loss <= valid_loss_min:
    #    print('Validation loss decreased ({:.6f} --> {:.6f}).  copying model weights ...'.format(valid_loss_min,valid_loss))
    #    #best_model_wts = copy.deepcopy(model.state_dict())
    #    valid_loss_min = valid_loss

#model.load_state_dict(best_model_wts)


from flask import Flask, redirect, render_template, request
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from io import BytesIO
from flask import Response, make_response, session
from alch_conn import increment_data, fetch_data, show_graph, decrement_data

app = Flask(__name__)

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

@app.route("/data/", methods=["POST"])
def update_data():
    past_choices = get_past(request.cookies)
    if past_choices == None:
        past_choices = []
    choices = form_data(request.form.to_dict(), past_choices)
    increment_data(choices)

    resp = make_response(redirect("/"))
    for val in choices:
        resp.set_cookie(str(val), 'true')
    return resp

@app.route("/remove/", methods=["POST"])
def remove_data():
    past_choices = get_past(request.cookies)
    choices = form_data(request.form.to_dict(), eval(past_choices))
    decrement_data(choices)

    resp = make_response(redirect("/"))
    for val in choices:
        resp.set_cookie(str(val), 'true')
    return resp

@app.route("/graph" )
def get_graph():
    fig = show_graph()
    output = BytesIO()
    FigureCanvas(fig).print_png(output)
    return Response(output.getvalue(), mimetype='image/png')

@app.route("/current")
def show_data():
    return fetch_data()

# Helper function to translate form data into list if the choice is not in the past choices
def form_data(form_dict, past_choices):
    choices = []
    if form_dict.get('date1') == '1' and 'date1' not in past_choices:
        choices.append(0)
    if form_dict.get('date2') == '1' and 'date2' not in past_choices:
        choices.append(1)
    if form_dict.get('date3') == '1' and 'date3' not in past_choices:
        choices.append(2)
    return choices

def set_true(choices):
    past = {0: False, 1: False, 2: False}
    for val in choices:
        past[val] = True
    return str(past)

def set_false(choices):
    past = {0: True, 1: True, 2: True}
    for val in choices:
        past[val] = False
    return str(past)

def get_past(cookie_dict):
    past = []
    if 'date1' in cookie_dict.keys():
        past.append('date1')
    if 'date2' in cookie_dict.keys():
        past.append('date2')
    if 'date3' in cookie_dict.keys():
        past.append('date3')


#!/usr/bin/python3

def search_replace(my_list, search, replace):
    item = []
    if my_list:
        for n in my_list:
            if not n == search:
                item.append(n)
            else:
                item.append(replace)
    return item


from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404

from apps.messageflow.models.settings import Settings

class ConfigurationException(Exception):
    pass

def get_line_token_dict_from_auth_user(auth_user):
    token_settings = get_object_or_404(Settings, owner_id=auth_user.id, app_id='nchat')
    if not token_settings.line_channel_secret:
        error_msg = (
            'line_channel_secret not configured for user {0}'
        ).format(auth_user)
        raise ConfigurationException(error_msg)

    if not token_settings.line_channel_access_token:
        error_msg = (
            'line_channel_access_token not configured for user {0}'
        ).format(auth_user)
        raise ConfigurationException(error_msg)

    line_token_dict = {
        "platform": 'line',
        "line_access_url_part": token_settings.line_access_url_part,
        "line_channel_secret": token_settings.line_channel_secret,
        "line_channel_access_token": token_settings.line_channel_access_token,
    }
    return line_token_dict

@login_required()
def get_line_token_dict_from_request(request):
    """
    Find and returns a settings object containing access tokens for the LINE api
    :param request:
    :return:
    """
    return get_line_token_dict_from_auth_user(request.user)


'''
冒泡排序：

复杂度：在该排序算法中，要遍历n-1轮，每一轮都要比较数组中的元素，所以时间复杂度是O(n2)
稳定性：比较的过程中相同的元素并不会发生交换，所以冒泡排序是一种稳定的排序算法。

相邻的两个元素进行比较，然后把较大的元素放到后面（正向排序），在一轮比较完后最大的元素就放在了最后一个位置，
因为这一点像鱼儿在水中吐的气泡在上升的过程中不断变大，所以得名冒泡排序。在该排序算法中，要遍历n-1轮，
每一轮都要比较数组中的元素，所以时间复杂度是O(n2)
'''

alist = [1,5,2,76,3,2,7,8,4,23,78,32,65]

def bubble_sort(alist):
    length = len(alist)
    for i in range(length - 1):
        # i表示比较多少轮
        for j in range(length - i - 1):
            # j表示每轮比较的元素的范围，因为每比较一轮就会排序好一个元素的位置，
            # 所以在下一轮比较的时候就少比较了一个元素，所以要减去i
            if alist[j] > alist[j + 1]:
                alist[j], alist[j + 1] = alist[j + 1], alist[j]
            # print(i,j,alist)
    return alist

# print("bubble_sort:",bubble_sort(alist))

def my_bubble_sort(alist):
    length = len(alist)
    for i in range(length - 1):
        for j in range(length-1-i):
            if alist[j] > alist[j+1]:
                alist[j],alist[j+1] = alist[j+1],alist[j]
    return alist

# print('my_bubble_sort:',my_bubble_sort(alist))

'''
选择排序：
复杂度：每遍历一次排好一个元素，而每一次都会比较所有的元素，所以时间复杂度为O(n2)
稳定性：因为在交换的过程中，相同的元素位置可能发生变化，所以选择排序是不稳定的

第一轮的时候，所有的元素都和第一个元素进行比较，如果比第一个元素大，就和第一个元素进行交换，
在这轮比较完后，就找到了最小的元素；第二轮的时候所有的元素都和第二个元素进行比较找出第二个位置的元素，以此类推。
'''

def selection_sort(alist):
    length = len(alist)
    for i in range(length - 1,0,-1):
        for j in range(i):
            if alist[j] > alist[i]:
                alist[j],alist[i] = alist[i],alist[j]
            print(i,j,alist)
    return alist

print('selection_sort:',selection_sort(alist))


# write code for chatgpt

import os
import sys
import json
import torch
import argparse
import numpy as np
from tqdm import tqdm
from torch.utils.data import DataLoader
from transformers import GPT2LMHeadModel, GPT2TokenizerFast

from dataset import GPT2Dataset
from utils import get_logger

logger = get_logger()

def parse_args():
    parser = argparse.ArgumentParser(description="Chatbot")
    parser.add_argument("--test_data", type=str, default="./data/test.txt")
    parser.add_argument("--model_path", type=str, default="./model")
    parser.add_argument("--batch_size", type=int, default=32)
    parser.add_argument("--max_len", type=int, default=64)
    parser.add_argument("--top_k", type=int, default=5)
    parser.add_argument("--top_p", type=float, default=0.95)
    parser.add_argument("--device", type=str, default="cuda:0")
    args = parser.parse_args()
    return args

def main(args):
    # load model
    tokenizer = GPT2TokenizerFast.from_pretrained(args.model_path)
    model = GPT2LMHeadModel.from_pretrained(args.model_path)
    model.to(args.device)
    model.eval()

    # load test data
    test_dataset = GPT2Dataset(args.test_data, tokenizer, args.max_len)
    test_loader = DataLoader(test_dataset, batch_size=args.batch_size, shuffle=False)

    # generate responses
    responses = []
    for batch in tqdm(test_loader):
        batch = batch.to(args.device)
        outputs = model.generate(
            input_ids=batch,
            max_length=args.max_len,
            do_sample=True,
            top_k=args.top_k,
            top_p=args.top_p,
            temperature=1.0,
            pad_token_id=tokenizer.eos_token_id,
            num_return_sequences=1,
        )
        responses.extend(outputs.tolist())

    # save responses
    with open("responses.json", "w") as f:
        json.dump(responses, f)


import os

from flask import Flask
from flask_restful import Api, reqparse
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

app = Flask(__name__)
api = Api(app)

app.config.from_object("shopping.config." + os.environ['APP_SETTINGS'])
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)

parser = reqparse.RequestParser()
parser.add_argument('name')
parser.add_argument('amount')
parser.add_argument('userId')
parser.add_argument('login')

from shopping.resources import ItemList, ItemCreate, Item, ItemChangeAmount, ItemChangeBuyStatus, CategoryList, \
    Clear, UserData, WakeUp

api.add_resource(ItemList, '/items/<user_id>')
api.add_resource(ItemCreate, '/item')
api.add_resource(Item, '/item/<item_id>')
api.add_resource(ItemChangeAmount, '/item/<item_id>/change-amount')
api.add_resource(ItemChangeBuyStatus, '/item/<item_id>/change-buy-status')
api.add_resource(Clear, '/items/<user_id>/clear')
api.add_resource(CategoryList, '/category/all')
api.add_resource(UserData, '/user/<login>')
api.add_resource(WakeUp, '/wake-up')


# @author: huhao
# @file: select_sort.py
# @time: 2019/7/25 8:05
# @Document：https://www.python.org/doc/
# @desc:
'''
for i=0 -> len(array)-1:
    min_index = i
    for j = i+1 -> len(array):
        if(array[min_index] > array[j])
            min_index = j
    swap(array[min_index], array[i])

'''

def select_sort(origin_items, comp=lambda x, y: x < y):
    '''简单选择排序'''
    items = origin_items[:]
    for i in range(len(items) - 1):
        min_index = i
        for j in range(i + 1, len(items)):
            if comp(items[j], items[min_index]):
                min_index = j
        items[i], items[min_index] = items[min_index], items[i]  # python中交换两列表元素方法
    return items

def main():
    items = [2, 1, 4, 3, 5, 3, 6, 7, 5]
    items_sorted = select_sort(items)
    print(items_sorted)

if __name__ == '__main__':
    main()


import logging
import threading
from pathlib import Path
import concurrent.futures
from omegaconf import OmegaConf
from contextlib import suppress
from collections import OrderedDict

from .stats import ScanStats
from .target import ScanTarget
from .manager import ScanManager
from .dispatcher import Dispatcher
from bbot.modules import module_loader
from bbot.core.event import make_event
from bbot.core.logger import init_logging
from bbot.core.helpers.misc import sha1, rand_string
from bbot.core.helpers.helper import ConfigAwareHelper
from bbot.core.helpers.names_generator import random_name
from bbot.core.helpers.threadpool import ThreadPoolWrapper
from bbot.core.errors import BBOTError, ScanError, ScanCancelledError, ValidationError

log = logging.getLogger("bbot.scanner")

init_logging()

class Scanner:

    _status_codes = {
        "NOT_STARTED": 0,
        "STARTING": 1,
        "RUNNING": 2,
        "FINISHING": 3,
        "CLEANING_UP": 4,
        "ABORTING": 5,
        "ABORTED": 6,
        "FAILED": 7,
        "FINISHED": 8,
    }

    def __init__(
        self,
        *targets,
        whitelist=None,
        blacklist=None,
        scan_id=None,
        name=None,
        modules=None,
        output_modules=None,
        config=None,
        dispatcher=None,
        strict_scope=False,
        force_start=False,
    ):
        if modules is None:
            modules = []
        if output_modules is None:
            output_modules = ["human"]
        if config is None:
            config = OmegaConf.create({})
        self.config = config
        if name is None:
            self.name = random_name()
        else:
            self.name = str(name)
        self.strict_scope = strict_scope
        self.force_start = force_start

        if scan_id is not None:
            self.id = str(scan_id)
        else:
            self.id = f"SCAN:{sha1(rand_string(20)).hexdigest()}"
        self._status = "NOT_STARTED"
        self._status_code = 0

        # Set up thread pools
        max_workers = max(1, self.config.get("max_threads", 100))
        # Shared thread pool, for module use
        self._thread_pool = ThreadPoolWrapper(concurrent.futures.ThreadPoolExecutor(max_workers=max_workers))
        # Event thread pool, for event construction, initialization
        self._event_thread_pool = ThreadPoolWrapper(concurrent.futures.ThreadPoolExecutor(max_workers=max_workers * 2))
        # Internal thread pool, for handle_event(), module setup, cleanup callbacks, etc.
        self._internal_thread_pool = ThreadPoolWrapper(concurrent.futures.ThreadPoolExecutor(max_workers=max_workers))
        self.process_pool = ThreadPoolWrapper(concurrent.futures.ProcessPoolExecutor())

        self.helpers = ConfigAwareHelper(config=self.config, scan=self)
        output_dir = self.config.get("output_dir", "")
        if output_dir:
            self.home = Path(output_dir).resolve() / self.name
        else:
            self.home = self.helpers.bbot_home / "scans" / self.name

        self.target = ScanTarget(self, *targets, strict_scope=strict_scope)

        self.modules = OrderedDict({})
        self._scan_modules = modules
        self._internal_modules = list(self._internal_modules())
        self._output_modules = output_modules
        self._modules_loaded = False

        if not whitelist:
            self.whitelist = self.target.copy()
        else:
            self.whitelist = ScanTarget(self, *whitelist, strict_scope=strict_scope)
        if not blacklist:
            blacklist = []
        self.blacklist = ScanTarget(self, *blacklist)

        if dispatcher is None:
            self.dispatcher = Dispatcher()
        else:
            self.dispatcher = dispatcher
        self.dispatcher.set_scan(self)

        self.manager = ScanManager(self)
        self.stats = ScanStats(self)

        # prevent too many brute force modules from running at one time
        # because they can bypass the global thread limit
        self.max_brute_forcers = int(self.config.get("max_brute_forcers", 1))
        self._brute_lock = threading.Semaphore(self.max_brute_forcers)

        # scope distance
        self.scope_search_distance = max(0, int(self.config.get("scope_search_distance", 1)))
        self.dns_search_distance = max(
            self.scope_search_distance, int(self.config.get("scope_dns_search_distance", 3))
        )
        self.scope_report_distance = int(self.config.get("scope_report_distance", 1))

        self._prepped = False
        self._cleanedup = False

    def prep(self):
        self.helpers.mkdir(self.home)
        if not self._prepped:
            start_msg = f"Scan with {len(self._scan_modules):,} modules seeded with {len(self.target)} targets"
            details = []
            if self.whitelist != self.target:
                details.append(f"{len(self.whitelist):,} in whitelist")
            if self.blacklist:
                details.append(f"{len(self.blacklist):,} in blacklist")
            if details:
                start_msg += f" ({', '.join(details)})"
            self.hugeinfo(start_msg)

            self.load_modules()

            self.info(f"Setting up modules...")
            self.setup_modules()

            self.success(f"Setup succeeded for {len(self.modules):,} modules.")
            self._prepped = True

    def start(self):

        self.prep()

        failed = True

        if not self.target:
            self.warning(f"No scan targets specified")

        try:
            self.status = "STARTING"

            if not self.modules:
                self.error(f"No modules loaded")
                self.status = "FAILED"
                return
            else:
                self.hugesuccess(f"Starting scan {self.name}")

            if self.stopping:
                return

            # distribute seed events
            self.manager.init_events()

            if self.stopping:
                return

            self.status = "RUNNING"
            self.start_modules()
            self.verbose(f"{len(self.modules):,} modules started")

            if self.stopping:
                return

            self.manager.loop_until_finished()
            failed = False

        except KeyboardInterrupt:
            self.stop()
            failed = False

        except ScanCancelledError:
            self.debug("Scan cancelled")

        except ScanError as e:
            self.error(f"{e}")

        except BBOTError as e:
            import traceback

            self.critical(f"Error during scan: {e}")
            self.debug(traceback.format_exc())

        except Exception:
            import traceback

            self.critical(f"Unexpected error during scan:\n{traceback.format_exc()}")

        finally:

            self.cleanup()
            self.shutdown_threadpools(wait=True)

            log_fn = self.hugesuccess
            if self.status == "ABORTING":
                self.status = "ABORTED"
                log_fn = self.hugewarning
            elif failed:
                self.status = "FAILED"
                log_fn = self.critical
            else:
                self.status = "FINISHED"

            log_fn(f"Scan {self.name} completed with status {self.status}")

            self.dispatcher.on_finish(self)

    def start_modules(self):
        self.verbose(f"Starting module threads")
        for module_name, module in self.modules.items():
            module.start()

    def setup_modules(self, remove_failed=True):
        self.load_modules()
        self.verbose(f"Setting up modules")
        hard_failed = []
        soft_failed = []
        setup_futures = dict()

        for module_name, module in self.modules.items():
            future = self._internal_thread_pool.submit_task(module._setup)
            setup_futures[future] = module_name
        for future in self.helpers.as_completed(setup_futures):
            module_name = setup_futures[future]
            status, msg = future.result()
            if status == True:
                self.debug(f"Setup succeeded for {module_name} ({msg})")
            elif status == False:
                self.error(f"Setup hard-failed for {module_name}: {msg}")
                self.modules[module_name].set_error_state()
                hard_failed.append(module_name)
            else:
                self.warning(f"Setup soft-failed for {module_name}: {msg}")
                soft_failed.append(module_name)
            if not status and remove_failed:
                self.modules.pop(module_name)

        num_output_modules = len([m for m in self.modules.values() if m._type == "output"])
        if num_output_modules < 1:
            raise ScanError("Failed to load output modules. Aborting.")
        total_failed = len(hard_failed + soft_failed)
        if hard_failed:
            msg = f"Setup hard-failed for {len(hard_failed):,} modules ({','.join(hard_failed)})"
            self.fail_setup(msg)
        elif total_failed > 0:
            self.warning(f"Setup failed for {total_failed:,} modules")

    def stop(self, wait=False):
        if self.status != "ABORTING":
            self.status = "ABORTING"
            self.hugewarning(f"Aborting scan")
            for i in range(max(10, self.max_brute_forcers * 10)):
                self._brute_lock.release()
            self.helpers.kill_children()
            self.shutdown_threadpools(wait=False)
            self.helpers.kill_children()

    def shutdown_threadpools(self, wait=True):
        pools = [
            self.process_pool,
            self._internal_thread_pool,
            self.helpers.dns._thread_pool,
            self._event_thread_pool,
            self._thread_pool,
        ]
        self.debug(f"Shutting down thread pools with wait={wait}")
        threads = []
        for pool in pools:
            t = threading.Thread(target=pool.shutdown, kwargs={"wait": False, "cancel_futures": True}, daemon=True)
            t.start()
            threads.append(t)
        if wait:
            for t in threads:
                t.join()
        if wait:
            for pool in pools:
                pool.shutdown(wait=True)
        self.debug("Finished shutting down thread pools")

    def cleanup(self):
        # clean up modules
        self.status = "CLEANING_UP"
        for mod in self.modules.values():
            mod._cleanup()
        if not self._cleanedup:
            self._cleanedup = True
            with suppress(Exception):
                self.home.rmdir()
            self.helpers.clean_old_scans()

    def in_scope(self, e):
        """
        Checks whitelist and blacklist, also taking scope_distance into account
        """
        try:
            e = make_event(e, dummy=True)
        except ValidationError:
            return False
        in_scope = e.scope_distance == 0 or self.whitelisted(e)
        return in_scope and not self.blacklisted(e)

    def blacklisted(self, e):
        e = make_event(e, dummy=True)
        return e in self.blacklist

    def whitelisted(self, e):
        e = make_event(e, dummy=True)
        return e in self.whitelist

    @property
    def word_cloud(self):
        return self.helpers.word_cloud

    @property
    def stopping(self):
        return not self.running

    @property
    def running(self):
        return 0 < self._status_code < 4

    @property
    def status(self):
        return self._status

    @status.setter
    def status(self, status):
        """
        Block setting after status has been aborted
        """
        status = str(status).strip().upper()
        if status in self._status_codes:
            if self.status == "ABORTING" and not status == "ABORTED":
                self.debug(f'Attempt to set invalid status "{status}" on aborted scan')
            else:
                self._status = status
                self._status_code = self._status_codes[status]
                self.dispatcher.on_status(self._status, self.id)
        else:
            self.debug(f'Attempt to set invalid status "{status}" on scan')

    @property
    def status_detailed(self):
        main_tasks = self._thread_pool.num_tasks
        dns_tasks = self.helpers.dns._thread_pool.num_tasks
        event_threadpool_tasks = self._event_thread_pool.num_tasks
        event_tasks = self.manager.event_queue.qsize()
        internal_tasks = self._internal_thread_pool.num_tasks
        process_tasks = self.process_pool.num_tasks
        total_tasks = main_tasks + dns_tasks + event_tasks + internal_tasks
        status = {
            "queued_tasks": {
                "main": main_tasks,
                "dns": dns_tasks,
                "internal": internal_tasks,
                "process": process_tasks,
                "event": event_threadpool_tasks,
                "total": total_tasks,
            },
            "queued_events": {
                "manager": event_tasks,
            },
        }
        return status

    def make_event(self, *args, **kwargs):
        kwargs["scan"] = self
        event = make_event(*args, **kwargs)
        return event

    @property
    def log(self):
        if self._log is None:
            self._log = logging.getLogger(f"bbot.agent.scanner")
        return self._log

    @property
    def root_event(self):
        root_event = self.make_event(data=f"{self.name} ({self.id})", event_type="SCAN", dummy=True)
        root_event._id = self.id
        root_event.scope_distance = 0
        root_event._resolved.set()
        root_event.source = root_event
        root_event.module = self.helpers._make_dummy_module(name="TARGET", _type="TARGET")
        return root_event

    @property
    def useragent(self):
        return self.config.get("user_agent", "BBOT")

    @property
    def json(self):
        j = dict()
        for i in ("id", "name"):
            v = getattr(self, i, "")
            if v:
                j.update({i: v})
        if self.target:
            j.update({"targets": [str(e.data) for e in self.target]})
        if self.whitelist:
            j.update({"whitelist": [str(e.data) for e in self.whitelist]})
        if self.blacklist:
            j.update({"blacklist": [str(e.data) for e in self.blacklist]})
        if self.modules:
            j.update({"modules": [str(m) for m in self.modules]})
        return j

    def debug(self, *args, **kwargs):
        log.debug(*args, extra={"scan_id": self.id}, **kwargs)

    def verbose(self, *args, **kwargs):
        log.verbose(*args, extra={"scan_id": self.id}, **kwargs)

    def hugeverbose(self, *args, **kwargs):
        log.hugeverbose(*args, extra={"scan_id": self.id}, **kwargs)

    def info(self, *args, **kwargs):
        log.info(*args, extra={"scan_id": self.id}, **kwargs)

    def hugeinfo(self, *args, **kwargs):
        log.hugeinfo(*args, extra={"scan_id": self.id}, **kwargs)

    def success(self, *args, **kwargs):
        log.success(*args, extra={"scan_id": self.id}, **kwargs)

    def hugesuccess(self, *args, **kwargs):
        log.hugesuccess(*args, extra={"scan_id": self.id}, **kwargs)

    def warning(self, *args, **kwargs):
        log.warning(*args, extra={"scan_id": self.id}, **kwargs)

    def hugewarning(self, *args, **kwargs):
        log.hugewarning(*args, extra={"scan_id": self.id}, **kwargs)

    def error(self, *args, **kwargs):
        log.error(*args, extra={"scan_id": self.id}, **kwargs)

    def critical(self, *args, **kwargs):
        log.critical(*args, extra={"scan_id": self.id}, **kwargs)

    def _internal_modules(self):
        for modname in module_loader.preloaded(type="internal"):
            if self.config.get(modname, True):
                yield modname

    def load_modules(self):

        if not self._modules_loaded:

            all_modules = list(set(self._scan_modules + self._output_modules + self._internal_modules))
            if not all_modules:
                self.warning(f"No modules to load")
                return

            if not self._scan_modules:
                self.warning(f"No scan modules to load")

            # install module dependencies
            succeeded, failed = self.helpers.depsinstaller.install(
                *self._scan_modules, *self._output_modules, *self._internal_modules
            )
            if failed:
                msg = f"Failed to install dependencies for {len(failed):,} modules: {','.join(failed)}"
                self.fail_setup(msg)
            modules = [m for m in self._scan_modules if m in succeeded]
            output_modules = [m for m in self._output_modules if m in succeeded]
            internal_modules = [m for m in self._internal_modules if m in succeeded]

            # Load scan modules
            self.verbose(f"Loading {len(modules):,} scan modules: {','.join(list(modules))}")
            loaded_modules, failed = self._load_modules(modules)
            self.modules.update(loaded_modules)
            if len(failed) > 0:
                msg = f"Failed to load {len(failed):,} scan modules: {','.join(failed)}"
                self.fail_setup(msg)
            if loaded_modules:
                self.info(
                    f"Loaded {len(loaded_modules):,}/{len(self._scan_modules):,} scan modules ({','.join(list(loaded_modules))})"
                )

            # Load internal modules
            self.verbose(f"Loading {len(internal_modules):,} internal modules: {','.join(list(internal_modules))}")
            loaded_internal_modules, failed_internal = self._load_modules(internal_modules)
            self.modules.update(loaded_internal_modules)
            if len(failed_internal) > 0:
                msg = f"Failed to load {len(loaded_internal_modules):,} internal modules: {','.join(loaded_internal_modules)}"
                self.fail_setup(msg)
            if loaded_internal_modules:
                self.info(
                    f"Loaded {len(loaded_internal_modules):,}/{len(self._internal_modules):,} internal modules ({','.join(list(loaded_internal_modules))})"
                )

            # Load output modules
            self.verbose(f"Loading {len(output_modules):,} output modules: {','.join(list(output_modules))}")
            loaded_output_modules, failed_output = self._load_modules(output_modules)
            self.modules.update(loaded_output_modules)
            if len(failed_output) > 0:
                msg = f"Failed to load {len(failed_output):,} output modules: {','.join(failed_output)}"
                self.fail_setup(msg)
            if loaded_output_modules:
                self.info(
                    f"Loaded {len(loaded_output_modules):,}/{len(self._output_modules):,} output modules, ({','.join(list(loaded_output_modules))})"
                )

            self.modules = OrderedDict(sorted(self.modules.items(), key=lambda x: getattr(x[-1], "_priority", 0)))
            self._modules_loaded = True

    def fail_setup(self, msg):
        msg = str(msg)
        if not self.force_start:
            msg += " (--force to run module anyway)"
        if self.force_start:
            self.error(msg)
        else:
            raise ScanError(msg)

    def _load_modules(self, modules):

        modules = [str(m) for m in modules]
        loaded_modules = {}
        failed = set()
        for module_name, module_class in module_loader.load_modules(modules).items():
            if module_class:
                try:
                    loaded_modules[module_name] = module_class(self)
                    self.verbose(f'Loaded module "{module_name}"')
                    continue
                except Exception:
                    import traceback

                    self.warning(f"Failed to load module {module_class}")
                    self.debug(traceback.format_exc())
            else:
                self.warning(f'Failed to load unknown module "{module_name}"')
            failed.add(module_name)
        return loaded_modules, failed


import unittest

from absl.testing import absltest
import numpy as np

from xla.python import xla_client

# pylint: disable=g-import-not-at-top
try:
  import portpicker
except ImportError:
  portpicker = None
# pylint: enable=g-import-not-at-top

ops = xla_client.ops

class ShapeTest(absltest.TestCase):

  def testInvalidShapes(self):
    with self.assertRaisesRegex(xla_client.XlaRuntimeError, "invalid shape"):
      xla_client.Shape.array_shape(xla_client.PrimitiveType.F32, [-2, 4])

    with self.assertRaisesRegex(
        RuntimeError, "layout minor_to_major field contains 1 element.*"):
      xla_client.Shape.array_shape(xla_client.PrimitiveType.F32, [2, 4], [3])

    with self.assertRaisesRegex(
        RuntimeError, "layout minor_to_major field has out-of-bounds value.*"):
      xla_client.Shape.array_shape(xla_client.PrimitiveType.F32, [2, 4],
                                   [1, -1])

class ComputationPrinting(absltest.TestCase):

  def ExampleComputation(self):
    builder = xla_client.XlaBuilder("acomputation")
    p0 = ops.Parameter(builder, 0, xla_client.shape_from_pyval(np.float32(0)))
    p1 = ops.Parameter(builder, 1,
                       xla_client.shape_from_pyval(np.zeros((4,), np.float32)))
    x = ops.Mul(p0, p1)
    ops.Add(x, x)
    return builder.build()

  def testComputationToHloText(self):
    computation = self.ExampleComputation()
    hlo_text = computation.as_hlo_text()
    self.assertTrue(hlo_text.startswith("HloModule acomputation"))

  def testComputationToHloGraph(self):
    computation = self.ExampleComputation()
    hlo_dot_graph = computation.as_hlo_dot_graph()
    self.assertTrue(hlo_dot_graph.startswith("digraph "))

  def testHloModuleToHloText(self):
    computation = self.ExampleComputation()
    hlo_text = computation.as_hlo_module().to_string()
    self.assertTrue(hlo_text.startswith("HloModule acomputation"))

  def testHloModuleFromText(self):
    hlo_module_text = """HloModule test
        add {
          x = f32[] parameter(0)
          y = f32[] parameter(1)
          ROOT add = f32[] add(x, y)
        }
        ENTRY entry {
          p0 = f32[2,3] parameter(0)
          start = f32[2,3] all-reduce-start(p0), to_apply=add
          ROOT done = f32[2,3] all-reduce-done(start)
        }"""
    hlo_module = xla_client._xla.hlo_module_from_text(hlo_module_text)
    hlo_text = hlo_module.to_string()
    self.assertTrue(hlo_text.startswith("HloModule test"))

  def testHloModuleToHloGraph(self):
    computation = self.ExampleComputation()
    hlo_dot_graph = xla_client._xla.hlo_module_to_dot_graph(
        computation.as_hlo_module())
    self.assertTrue(hlo_dot_graph.startswith("digraph "))

class ComputationHashTest(absltest.TestCase):

  def testHash(self):
    builder0 = xla_client.XlaBuilder("computation0")
    p0 = ops.Parameter(builder0, 0, xla_client.shape_from_pyval(np.float32(0)))
    p1 = ops.Parameter(builder0, 1,
                       xla_client.shape_from_pyval(np.zeros((4,), np.float32)))
    ops.Mul(p0, p1)
    computation0 = builder0.build()

    builder1 = xla_client.XlaBuilder("computation1")
    p0 = ops.Parameter(builder1, 0, xla_client.shape_from_pyval(np.float32(0)))
    p1 = ops.Parameter(builder1, 1,
                       xla_client.shape_from_pyval(np.zeros((4,), np.float32)))
    ops.Mul(p0, p1)
    computation1 = builder1.build()

    self.assertEqual(computation0.hash(), computation1.hash())

class AliasTest(absltest.TestCase):

  def testSetUpAlias(self):
    c = xla_client.XlaBuilder(self.id())
    p1 = ops.Parameter(
        c, 0,
        xla_client.shape_from_pyval(np.array(
            1.0, np.float32)).with_major_to_minor_layout_if_absent())
    p2 = ops.Parameter(
        c, 1,
        xla_client.shape_from_pyval(np.array(
            1.0, np.float32)).with_major_to_minor_layout_if_absent())
    out = ops.Add(p1, p2)
    c.setup_alias([], 0, [])
    c.build(out)

class ProfilerTest(absltest.TestCase):

  def testTraceMe(self):
    # TODO(phawkins): These tests just check that the TraceMe context manager
    # acts like a context manager and doesn't explode. Ideally we'd check that
    # the profiler saw the traceme too.
    with xla_client.profiler.TraceMe("test1"):
      pass
    with xla_client.profiler.TraceMe("test2", foo=123):
      pass
    with self.assertRaises(ValueError):
      with xla_client.profiler.TraceMe("test3"):
        raise ValueError("test")

  @unittest.skipIf(portpicker is None, "Test requires portpicker")
  def testStartServer(self):
    port = portpicker.pick_unused_port()
    server = xla_client.profiler.start_server(port)
    del server

class HloModuleGroupTest(absltest.TestCase):

  def testHloModuleGroup(self):
    builder0 = xla_client.XlaBuilder("computation0")
    p0 = ops.Parameter(builder0, 0, xla_client.shape_from_pyval(np.float32(0)))
    p1 = ops.Parameter(builder0, 1,
                       xla_client.shape_from_pyval(np.zeros((4,), np.float32)))
    root = ops.Mul(p0, p1)
    computation0 = builder0.build(root)

    m = computation0.get_hlo_module()
    mg_name = "test_module_group"
    mg = xla_client._xla.HloModuleGroup(mg_name, [m])
    self.assertEqual(mg.name, mg_name)

    modules = mg.to_modules()
    self.assertLen(modules, 1)
    self.assertEqual(m.to_string(), modules[0].to_string())

class RunHloPassTest(absltest.TestCase):

  def testHloDCE(self):
    b = xla_client.XlaBuilder("acomputation")
    p0 = ops.Parameter(b, 0, xla_client.shape_from_pyval(np.float32(0)))
    p1 = ops.Parameter(b, 1,
                       xla_client.shape_from_pyval(np.zeros((4,), np.float32)))
    root = ops.Mul(p0, p1)

    # Dead instructions
    p2 = ops.Parameter(b, 2, xla_client.shape_from_pyval(np.float32(0)))
    ops.Add(p2, p2)

    hlo_module = b.build(root).get_hlo_module()
    self.assertTrue(xla_client._xla.HloDCE().run(hlo_module))

if __name__ == "__main__":
  absltest.main()


# Global vs local variables

# Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.

total = 0 # This is global variable.
# Function definition is here
def sum( arg1, arg2 ):
   # Add both the parameters and return them."
   total = arg1 + arg2; # Here total is local variable.
   print ("Inside the function local total : ", total)
   return total

# Now you can call sum function
sum( 10, 20 )
print ("Outside the function global total : ", total)

Basic Tutorial/Chapter 12 - Functions/script11.py


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

import os
import sys

try:
    import toml
except ImportError as e:
    print(
        'Please first install toml using `pip3 install toml` and rerun this script'
    )
    exit(1)

if len(sys.argv) != 2:
    print('  Usage: ./apply_bump_version.py next_version')
    print('Example: ./apply_bump_version.py 2.0.0')
    exit(1)

next_version = sys.argv[1]

def read_file(fname):
    f = open(fname)
    return f.readlines()

def write_back(lines, f):
    with open(f, 'w') as writer:
        writer.writelines(lines)

def do_bump_version(cargo_path):
    lines = read_file(cargo_path)
    #  We assume all Cargo.toml follows the auto generated template, otherwise
    #  skip it.
    if lines[0].strip() != '[package]':
        print(cargo_path, 'may not be bumped properly, skipping...')
        return

    for idx in range(1, len(lines)):
        if lines[idx].startswith('version'):
            lines[idx] = 'version = "{version}"\n'.format(version=next_version)
            write_back(lines, cargo_path)
            break

def main():
    # Change the working directory to project root directory.
    os.chdir("../")

    for member in toml.load("Cargo.toml")['workspace']['members']:
        #  Ignore the forked contracts pallets
        if 'contracts' in member:
            print('Skipped bump version for package', member)
            continue

        cargo_path = os.path.join(member, 'Cargo.toml')
        do_bump_version(cargo_path)

    do_bump_version("Cargo.toml")

main()


import argparse
import sys
import gym
import torch
import random
from replay_memory import ReplayMemory
from qNetwork import QNetwork
from convolutionalQNetwork import ConvQNetwork
from gym import wrappers, logger
import matplotlib.pyplot as plt
import numpy as np
import torch.optim as optimizer
from wrappers import GreyScale_Resize, FrameSkippingMaxing, StackFrames, FireReset, EpisodicLife, ClipRewardEnv
from breakout_agent import Agent as BAgent
from cartPole_agent import Agent

def main(environment):
    BUFFER_SIZE = 50000
    BATCH_SIZE = 64  # mini-batch size
    GAMMA = 0.99  # discount factor
    ALPHA = 0.01  # for soft update of target parameters
    LEARNING_RATE = 5e-4  # learning rate
    UPDATE_EVERY = 2  # how often to update the network

    # You can set the level to logger.DEBUG or logger.WARN if you
    # want to change the amount of output.
    logger.set_level(logger.INFO)

    if environment == "CartPole-v1":
        env = gym.make("CartPole-v1")
        outdir = 'logs/cartpole-agent-results'
        agent = Agent(env.action_space, env.observation_space, LEARNING_RATE, ALPHA)
    else:
        env = gym.make("BreakoutNoFrameskip-v4")
        # Research paper wrappers
        env = GreyScale_Resize(env)
        env = FrameSkippingMaxing(env)
        env = StackFrames(env)
        # Optimisation wrappers
        env = EpisodicLife(env)
        env = FireReset(env)
        env = ClipRewardEnv(env)
        outdir = 'logs/breakout-agent-results'
        agent = BAgent(env.action_space, env.observation_space, LEARNING_RATE, ALPHA)

    env = wrappers.Monitor(env, directory=outdir, force=True)
    env.seed(0)

    episode_count = 10000
    score = [0]
    buffer = ReplayMemory(BUFFER_SIZE)
    learn = 0
    epsilon = 1
    epsilon_decay = 0.998
    epsilon_min = 0.005
    for i in range(episode_count):
        if i % 100 == 0:
            print("episode: ", i)
        state = env.reset()
        sum_reward = 0
        # for _ in range(MAX_EPISODE_LEN):
        while True:
            action = agent.act(state, epsilon_=epsilon)
            # print(action)
            next_state, reward, done, _ = env.step(action)
            # Store experience replay
            buffer.push(state=state, action=action, next_state=next_state, reward=reward, end=done)
            sum_reward = sum_reward + reward
            state = next_state

            # Learn
            learn = (learn + 1) % UPDATE_EVERY
            if len(buffer) >= BATCH_SIZE and learn == 0:
                sample = buffer.sample(BATCH_SIZE)
                agent.learn(sample, GAMMA)

            if done:
                score = score + [sum_reward]
                break
            if epsilon > epsilon_min:
                epsilon = epsilon * epsilon_decay
            # env.render()

    # Close the env and write monitor result info to disk
    env.close()
    plt.plot(score)
    plt.ylabel('Rewards')
    plt.show()

    # Save params into a file
    agent.save_param()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=None)
    parser.add_argument('env_id', nargs='?', default='BreakoutNoFrameskip-v4', help='Select the environment to run')
    args = parser.parse_args()

    main(args.env_id)


import logging
from functools import partial
from django.db import migrations, models
import kirppu.models

logger = logging.getLogger(__name__)

# noinspection PyPep8Naming
def detect_provision(apps, schema_editor, forward):
    db_alias = schema_editor.connection.alias
    Event = apps.get_model("kirppu", "Event")
    events = (
        Event.objects.using(db_alias)
        .filter(provision_function__isnull=False)
        .only("id", "name", "provision_function")
    )
    if events:
        if forward:
            way = "new lisp"
        else:
            way = "old python"
        logger.warning("  ** You may need to migrate provision function scripts to %s format for following events: %s",
                       way,
                       ", ".join("%d/%s" % (event.id, repr(event.name))
                                 for event in events
                                 if event.provision_function.strip() != ""))

_fwd = partial(detect_provision, forward=True)
_rev = partial(detect_provision, forward=False)

class Migration(migrations.Migration):

    dependencies = [
        ('kirppu', '0033_auto_20191016_2230'),
    ]

    operations = [
        migrations.RunPython(_fwd, _rev),
        migrations.AlterField(
            model_name='event',
            name='provision_function',
            field=models.TextField(blank=True, help_text='Lisp function body that gets sold_and_compensated queryset as a global and must return a null or decimal number.', null=True, validators=[kirppu.models._validate_provision_function]),
        ),
    ]


import numpy as np

def run(game, agent_factory, n_players, n_steps, render=False, n_sync_steps=10):
    agents = [agent_factory(i) for i in range(n_players)]
    observations = game.reset()

    # repeat
    for k in range(n_steps):

        # obtain new action from each agent
        actions = np.asarray([agents[i].act(observations[i]) for i in range(n_players)])
        nxt_observations, rewards, done = game.step(actions)
        if render:
            game.render()

        # add experience to each agent and train
        for i in range(n_players):
            agent = agents[i]
            agent.experience(
                observations[i], actions[i], rewards[i], nxt_observations[i], done
            )

            # train agent
            agent.train()
            if k % n_sync_steps:
                agent.sync()


"""
Description: Messaging system between Processes and Threads
"""
from dataclasses import dataclass

# thread ID
THREAD_COMMANDER = 1001
THREAD_TASK = 1002
THREAD_MAIN = 1003
THREAD_SERVER = 1004

# Event ID
EVENT_FETCH = 101
EVENT_COMPLETE = 102
EVENT_SEARCHING = 103
EVENT_FINISHED = 106
EVENT_START = 107
EVENT_QUIT = 108
EVENT_CANCEL = 109
EVENT_MESSAGE = 110
EVENT_BLACKLIST = 111
EVENT_SCANNING = 112
EVENT_SERVER_READY = 113
EVENT_DOWNLOAD_IMAGE = 114
EVENT_PAUSE = 115
EVENT_FETCH_START = 116
EVENT_FETCH_COMPLETE = 117
EVENT_FETCH_CANCEL = 118

# Status ID
STATUS_OK = 1
STATUS_ERROR = -1
STATUS_START = 12
STATUS_IGNORED = 13

@dataclass
class Message:

    thread: int
    event: int
    status: int = STATUS_OK
    id: int = 0
    data: dict = None


import os
import argparse
import string
import json
import shutil
from random import randint

import paths
import game_elements
import build_card

# Both creates the card images and the printing images.
def create_images_from_Deck(deck, save_path=None, skip_complete=True):
    if type(deck)!=game_elements.Deck:
        raise TypeError("Input deck must be of type Deck.")
    if save_path is None:
        save_path = os.path.join(paths.DECK_PATH, deck.name)
    if not os.path.isdir(save_path):
        os.mkdir(save_path)
    if not save_path.endswith("Cards"):
        save_path = os.path.join(save_path, "Cards")
    if not os.path.isdir(save_path):
        os.mkdir(save_path)
    printing_path = os.path.join(paths.DECK_PATH, deck.name, "Printing")
    if not os.path.isdir(printing_path):
        os.mkdir(printing_path)
    for ci, card in enumerate(deck.cards):
        if card.complete and skip_complete:
            continue
        print("Building image for card", ci+1, "of", len(deck.cards), ":", card.name)
        build_card.create_card_image_from_Card(card, save_path=save_path)
        build_card.create_printing_image_from_Card(card, saved_image_path=save_path, save_path=printing_path)

# Updates the custom.xml file that Cockatrice uses to generate card information
# xml_filepath -- path to the custom.xml file used within Cockatrice.
# json_filepath -- path to the custom.json file used only to keep track of each different custom card. Since this is used to build custom.xml, if a card needs to be removed, it should be deleted from custom.json (possibly also from custom.json).
def update_cockatrice(deck, xml_filepath=None, json_filepath=None, image_path=paths.COCKATRICE_IMAGE_PATH):
    if xml_filepath is None:
        xml_filepath = os.path.dirname(paths.DECK_PATH)
    if not xml_filepath.endswith(".xml"):
        xml_filepath = os.path.join(xml_filepath, "custom.xml")
    xml_temp_filepath = os.path.join(os.path.dirname(xml_filepath), "customtemp.xml")
    if json_filepath is None:
        json_filepath = os.path.dirname(paths.DECK_PATH)
    if not json_filepath.endswith(".json"):
        json_filepath = os.path.join(json_filepath, "custom.json")
    setname = game_elements.Set.adjust_forbidden_custom_setname((deck.name.lower().replace("the ",""))[0:3].upper())
    cdict = {}
    for ci, card in enumerate(deck.cards):
        current_image_path = os.path.join(paths.DECK_PATH, deck.name, "Cards", card.name+".jpg")
        try:
            shutil.copy(current_image_path, os.path.join(paths.COCKATRICE_IMAGE_PATH, (card.name.replace('"', ''))+".full.jpeg"))
        except:
            print("\nWARNING -- could not copy the image from the path " + os.path.join(paths.DECK_PATH, deck.name, "Cards", card.name+".jpg") + " to the Cockatrice path -- check to make sure the image exists.")
        name = (card.name).replace('"','"')
        if card.rules is None:
            text = ""
        else:
            text = (card.rules).replace('"','"')
        coloridentity = "".join(card.colors).upper()
        side = "front"
        if card.special is not None and "back" in card.special:
            side = "back"
        fulltype = card.get_type_line()
        maintype = card.cardtype if type(card.supertype) is not str else card.supertype.title() + " " + card.cardtype.title()
        cmc = str(card.get_mana_value())
        if card.mana is None:
            manacost = ""
        else:
            manacost = ((card.mana.replace("{","")).replace("}","")).upper()
        colors = coloridentity
        layout = "normal"
        if card.special == "transform-front" or card.special == "transform-back":
            layout = "transform"
        elif card.special == "mdfc-front" or card.special == "mdfc-back":
            layout = "modal_dfc"
        muid = str(randint(900000, 999999))
        uuid = "d41b07c8-f0c8-4654-" + str(randint(1000, 9999)) + "-" + str(randint(100000000000, 999999999999))
        rarity = card.rarity
        # Format the .xml file using the appropriate attributes for each card.
        cdict[card.name] =  ''
        cdict[card.name] += '        <card>\n'
        cdict[card.name] += '            <name>' +name+ '</name>\n'
        cdict[card.name] += '            <text>' +text+ '</text>\n'
        cdict[card.name] += '            <prop>\n'
        cdict[card.name] += '                <format-penny>legal</format-penny>\n'
        cdict[card.name] += '                <coloridentity>' +coloridentity+ '</coloridentity>\n'
        cdict[card.name] += '                <format-pioneer>legal</format-pioneer>\n'
        cdict[card.name] += '                <side>' +side+ '</side>\n'
        cdict[card.name] += '                <type>' +fulltype+ '</type>\n'
        cdict[card.name] += '                <format-duel>legal</format-duel>\n'
        cdict[card.name] += '                <maintype>' +maintype+ '</maintype>\n'
        cdict[card.name] += '                <cmc>' +cmc+ '</cmc>\n'
        cdict[card.name] += '                <format-vintage>legal</format-vintage>\n'
        cdict[card.name] += '                <format-modern>legal</format-modern>\n'
        cdict[card.name] += '                <manacost>' +manacost+ '</manacost>\n'
        cdict[card.name] += '                <colors>' +colors+ '</colors>\n'
        cdict[card.name] += '                <format-legacy>legal</format-legacy>\n'
        cdict[card.name] += '                <layout>' +layout+ '</layout>\n'
        cdict[card.name] += '                <format-commander>legal</format-commander>\n'
        cdict[card.name] += '            </prop>\n'
        cdict[card.name] += '            <set muid="' +muid+ '" uuid="' +uuid+ '" num="' +str(ci+1)+ '" rarity="' +rarity+ '">' +setname+ '</set>\n'
        if (card.related is not None) and (card.related != ""):
            cdict[card.name] += '            <related attach="attach">' +card.related+ '</related>\n'
        cdict[card.name] += '            <tablerow>1</tablerow>\n'
        cdict[card.name] += '        </card>\n'
    # Update the custom.json to contain all of the new (if any) card data in this deck:
    try:
        customjson = open(json_filepath)
        customdict_orig = json.load(customjson)
    except:
        customdict_orig = {}
    customdict_new = {}
    customdict_new.update(customdict_orig)
    customdict_new.update(cdict)
    with open(json_filepath, 'w') as f:
        json.dump(customdict_new, f)
    # Use the custom.json file to update the custom.xml file with the card data from this deck:
    with open(xml_filepath, 'r') as file_orig:
        with open(xml_temp_filepath, 'w') as file_new:
            found_set_name = 0
            for line in file_orig:
                if '<name>' +setname+ '</name>' in line:
                    found_set_name = 1
                if '</sets>' in line:
                    if found_set_name == 0:
                        file_new.write('        <set>\n')
                        file_new.write('            <name>' +setname+ '</name>\n')
                        file_new.write('            <longname>' +deck.name+ '</longname>\n')
                        file_new.write('            <settype>Promo</settype>\n')
                        file_new.write('            <releasedate>2022-09-07</releasedate>\n')
                        file_new.write('        </set>\n')
                file_new.write(line)
                if '<cards>' in line:
                    for cardname in customdict_new.keys():
                        file_new.write(customdict_new[cardname])
                    file_new.write('    </cards>\n')
                    file_new.write('</cockatrice_carddatabase>')
                    break
        file_new.close()
    file_orig.close()
    os.replace(xml_temp_filepath, xml_filepath)

def main():
    parser = argparse.ArgumentParser(description='MTG Custom Card Builder')
    parser.add_argument('-d', '--deck', help='Name of Commander / Deck', type=str, default='Test', dest='deck')
    args = parser.parse_args()
    deck_folder = os.path.join(paths.DECK_PATH, string.capwords(args.deck))
    print("BUILDING DECK: ", deck_folder, "\n")
    for directory in ["Cards", "Images", "Printing"]:
        if not os.path.isdir(os.path.join(deck_folder, directory)):
            os.mkdir(os.path.join(deck_folder, directory))
    deck = game_elements.Deck.from_deck_folder(deck_folder)
    deck.print_color_summary()
    deck.print_mana_summary()
    deck.print_type_summary()
    deck.print_tag_summary()
    create_images_from_Deck(deck)
    if deck.name != "Test":
        update_cockatrice(deck)

if __name__ == '__main__':
    main()


import numpy as np

def arg_split(set_ids, n):
    """Return a list of arrays of indices of elements of set_ids equal to i for each 0<=i<n set_ids."""
    set_elems = []
    elem_idx = np.argsort(set_ids)
    set_idx = np.searchsorted(set_ids, np.arange(n+1), sorter=elem_idx)
    for i in range(n):
        set_elems.append(elem_idx[set_idx[i]:set_idx[i+1]])
    return set_elems


from JackTokenizer import JackTokenizer
from SymbolTable import SymbolTable
from VMWriter import VMWriter

class CompilationEngine:
    """
    Effects the actual compilation output. Gets its input from a JackTokenizer
    and emits its parsed structure into an output file/stream.
    """

    INDENT = "  "

    def __init__(self, jackFile, vmFile, DEBUG=False):
        """
        Creates a new compilation engine with the given input and output. The
        next routine called must be compileClass().
        """
        self.tokenizer = JackTokenizer(jackFile)  # , DEBUG=DEBUG)
        self.DEBUG = DEBUG

        # Indentation level
        self.indentLevel = 0

        # Counters for while loops and if statements
        self.whileCounter = self.ifCounter = 0

        # Initialize the symbol table
        self.symtab = SymbolTable(DEBUG=True)

        # Initialize the VM writer
        self.writer = VMWriter(vmFile, DEBUG=True)

    def compileClass(self):
        """
        Compiles a complete class.
        """
        self.emit(xml="<class>")

        # Alias self.tokenizer to make code more compact
        t = self.tokenizer

        # Verify that there is a token to read and advance to it
        if t.hasMoreTokens():
            # Advance to the next token
            t.advance()
        else:
            # If not, we're done.
            return

        self.eatAndEmit("keyword", ["class"])
        (_, self.thisClass) = self.eatAndEmit(
            "identifier", category="CLASS", state="DEFINE"
        )
        self.eatAndEmit("symbol", ["{"])

        # Expect zero or more classVarDecs. Count the fields defined.
        self.nFields = 0
        while t.tokenType() == "keyword" and t.keyWord() in ["static", "field"]:
            kw = t.keyWord()
            count = self.compileClassVarDec()

            # Count the fields to determine the size of the object
            if kw == "field":
                self.nFields += count

        # Expect zero or more subroutineDecs
        while t.tokenType() == "keyword" and t.keyWord() in [
            "constructor",
            "function",
            "method",
        ]:
            self.compileSubroutine()

        self.eatAndEmit("symbol", ["}"])
        self.emit(xml="</class>")

        # Should not be any more input
        if self.tokenizer.hasMoreTokens():
            raise SyntaxError(
                "Token after end of class: " + self.tokenizer.currentToken
            )

        # Close the VMWriter
        self.writer.close()

    def compileClassVarDec(self):
        """
        Compiles a static declaration or a field declaration.
        Should only be called if keyword static or keyword field is the current
        token.
        """
        self.emit(xml="<classVarDec>")

        # Need to save the variable kind for the symbol table
        token = self.eat("keyword", ["static", "field"])
        (_, varKind) = token
        varKind = varKind.upper()
        self.emit(token=token)

        # Expect a type: one of the keywords 'int', 'char', or 'boolean', or a
        # className (identifier).
        t = self.tokenizer
        tType = t.tokenType()
        if tType == "keyword":
            (_, varType) = self.eatAndEmit("keyword", ["int", "char", "boolean"])
        else:
            (_, varType) = self.eatAndEmit("identifier", category="CLASS", state="USE")

        self.eatAndEmit("identifier", category=varKind, varType=varType, state="DEFINE")
        count = 1

        # Expect an optional list of identifiers.
        while t.tokenType() == "symbol" and t.symbol() == ",":
            self.eatAndEmit("symbol", [","])
            self.eatAndEmit("identifier", category=varKind, state="DEFINE")
            count += 1

        self.eatAndEmit("symbol", [";"])
        self.emit(xml="</classVarDec>")

        return count

    def compileSubroutine(self):
        """
        Compiles a complete method, function, or constructor.
        Should only be called if the current token is one of 'constructor',
        'function', or 'method'.
        """
        self.emit(xml="<subroutineDec>")
        (_, kw) = self.eatAndEmit("keyword", ["constructor", "function", "method"])

        # Reset the subroutine symbol table
        self.symtab.startSubroutine()

        # If this is a method, seed the symbol table with "this" as argument 0
        if kw == "method":
            self.symtab.define("this", self.thisClass, "ARG")

        # Expect 'void' or a type: one of the keywords 'int', 'char', or
        # 'boolean', or a className (identifier).
        t = self.tokenizer
        tType = t.tokenType()
        if tType == "keyword":
            self.eatAndEmit("keyword", ["void", "int", "char", "boolean"])
        else:
            self.eatAndEmit("identifier", category="CLASS", state="USE")

        (_, functionName) = self.eatAndEmit(
            "identifier", category="SUBROUTINE", state="DEFINE"
        )

        self.eatAndEmit("symbol", ["("])
        self.compileParameterList()
        self.eatAndEmit("symbol", [")"])
        self.emit(xml="<subroutineBody>")
        self.eatAndEmit("symbol", ["{"])

        # Expect varDec*. Count the number of local variables.
        nLocals = 0
        while t.tokenType() == "keyword" and t.keyWord() == "var":
            nLocals += self.compileVarDec()

        # Generate the VM code to start the function.
        self.writer.writeFunction("{}.{}".format(self.thisClass, functionName), nLocals)

        # If this subroutine is a constructor, allocate memory for the new object and set the base of the this segment
        if kw == "constructor":
            self.writer.writePush("CONST", self.nFields)
            self.writer.writeCall("Memory.alloc", 1)
            self.writer.writePop("POINTER", 0)

        # If this subroutine is a method, set the base of the this segment
        if kw == "method":
            self.writer.writePush("ARG", 0)
            self.writer.writePop("POINTER", 0)

        # Compile the code of the function
        self.compileStatements()
        self.eatAndEmit("symbol", ["}"])
        self.emit(xml="</subroutineBody>")
        self.emit(xml="</subroutineDec>")

    def compileParameterList(self):
        """
        Compiles a (possibly empty) parameter list, not including the
        enclosing '( )'.
        """
        self.emit(xml="<parameterList>")

        # Alias for tokenizer
        t = self.tokenizer

        # Get the current token type
        tType = t.tokenType()

        # Expect a type: one of the keywords 'int', 'char', or 'boolean', or a
        # className (identifier).
        finished = False
        while not finished and tType in ["keyword", "identifier"]:
            if tType == "keyword":
                (_, varType) = self.eatAndEmit("keyword", ["int", "char", "boolean"])
            else:
                (_, varType) = self.eatAndEmit(
                    "identifier", category="CLASS", state="USE"
                )

            self.eatAndEmit(
                "identifier", category="ARG", state="DEFINE", varType=varType
            )

            # Look for a ',' symbol
            if t.tokenType() == "symbol" and t.symbol() == ",":
                # If found, eat it
                self.eatAndEmit("symbol", [","])

                # Get the next token type
                tType = t.tokenType()
            else:
                finished = True

        self.emit(xml="</parameterList>")

    def compileVarDec(self):
        """
        Compiles a var declaration.
        """
        self.emit(xml="<varDec>")
        self.eatAndEmit("keyword", ["var"])

        # Expect a type for the variable: one of the keywords 'int', 'char',
        # or 'boolean', or a className (identifier). Save the variable type.
        t = self.tokenizer
        tType = t.tokenType()
        if tType == "keyword":
            (_, varType) = self.eatAndEmit("keyword", ["int", "char", "boolean"])
        else:
            (_, varType) = self.eatAndEmit("identifier", category="CLASS", state="USE")

        self.eatAndEmit("identifier", category="VAR", state="DEFINE", varType=varType)
        nVars = 1

        # Expect an optional list of identifiers.
        while t.tokenType() == "symbol" and t.symbol() == ",":
            self.eatAndEmit("symbol", [","])
            self.eatAndEmit(
                "identifier", category="VAR", state="DEFINE", varType=varType
            )
            nVars += 1

        self.eatAndEmit("symbol", [";"])
        self.emit(xml="</varDec>")

        return nVars

    def compileStatements(self):
        """
        Compiles a sequence of statements, not including the enclosing
        '{ }'.
        """
        self.emit(xml="<statements>")

        t = self.tokenizer
        while t.tokenType() == "keyword":
            keyword = t.keyWord()
            if keyword == "do":
                self.compileDo()
            elif keyword == "let":
                self.compileLet()
            elif keyword == "while":
                self.compileWhile()
            elif keyword == "return":
                self.compileReturn()
            elif keyword == "if":
                self.compileIf()
            else:
                raise SyntaxError(
                    "Expected statement. Found {}.".format(t.currentToken)
                )

        self.emit(xml="</statements>")

    def compileDo(self):
        """
        Compiles a do statement.
        """
        self.emit(xml="<doStatement>")
        self.eatAndEmit("keyword", ["do"])

        # Eat the identifier. Can't emit until we know if this is a class or a subroutine.
        token = self.eat("identifier")
        (_, ident) = token

        # Check for a '.', which indicates a method call
        t = self.tokenizer
        if t.tokenType() == "symbol" and t.symbol() == ".":
            self.eatAndEmit("symbol", ["."])
            # Previous token was an object or a class. Check symbol table.
            objType = self.symtab.typeOf(ident)
            if objType:
                # ident is an object, so method is objType.method, and the object must be loaded into this as argument 0
                self.emit(token=token, category=self.symtab.kindOf(ident), state="USE")

                # subroutine starts with the class type
                subroutine = objType

                # Add an argument to the stack for "this"
                nArgs = 1
                kind = self.symtab.kindOf(ident)
                index = self.symtab.indexOf(ident)
                self.writer.writePush(kind, index)
            else:
                # ident is a class, so method is ident.method and there is no this
                self.emit(token=token, category="CLASS", state="USE")
                subroutine = ident
                nArgs = 0

            methodToken = self.eat("identifier")
            (_, method) = methodToken
            self.emit(token=methodToken, category="METHOD", state="USE")
            subroutine += "." + method
        else:
            # Bare subroutine calls are assumed to be methods of the current class
            self.emit(token=token, category="SUBROUTINE", state="USE")
            subroutine = self.thisClass + "." + ident

            # Add "this" to the stack
            nArgs = 1
            self.writer.writePush("POINTER", 0)

        self.eatAndEmit("symbol", ["("])
        nArgs += self.compileExpressionList()
        self.eatAndEmit("symbol", [")"])
        self.eatAndEmit("symbol", [";"])

        # Call the desired subroutine and consume the returned value
        self.writer.writeCall(subroutine, nArgs)
        self.writer.writePop("TEMP", 0)

        self.emit(xml="</doStatement>")

    def compileLet(self):
        """
        Compiles a let statement.
        """
        self.emit(xml="<letStatement>")
        self.eatAndEmit("keyword", ["let"])
        (_, varName) = self.eatAndEmit("identifier", category="LET", state="USE")

        # Look up the variable in the symbol table
        varKind = self.symtab.kindOf(varName)
        varIndex = self.symtab.indexOf(varName)

        # Check for array qualifier
        t = self.tokenizer
        arrayRef = False
        if t.tokenType() == "symbol" and t.symbol() == "[":
            # Compute the offset
            self.eatAndEmit("symbol", "[")
            self.compileExpression()
            self.eatAndEmit("symbol", ["]"])

            # Add the offset to the base. Leave the result on the stack.
            self.writer.writePush(varKind, varIndex)
            self.writer.writeArithmetic("+")
            arrayRef = True

        self.eatAndEmit("symbol", ["="])
        self.compileExpression()
        self.eatAndEmit("symbol", [";"])

        # Value to save is at the top of the stack.
        if not arrayRef:
            # Direct POP
            self.writer.writePop(varKind, varIndex)
        else:
            # Array reference. Save value temporarily while setting THAT.
            self.writer.writePop("TEMP", 0)
            self.writer.writePop("POINTER", 1)
            self.writer.writePush("TEMP", 0)
            self.writer.writePop("THAT", 0)

        self.emit(xml="</letStatement>")

    def compileWhile(self):
        """
        Compiles a while statement.
        """
        self.emit(xml="<whileStatement>")
        self.eatAndEmit("keyword", ["while"])

        whileInstance = self.whileCounter
        self.whileCounter += 1
        self.writer.writeLabel("WHILE.{}.{}.EXP".format(self.thisClass, whileInstance))

        self.eatAndEmit("symbol", ["("])
        self.compileExpression()
        self.eatAndEmit("symbol", [")"])

        self.writer.writeArithmetic("U~")
        self.writer.writeIf("WHILE.{}.{}.EXIT".format(self.thisClass, whileInstance))

        self.eatAndEmit("symbol", ["{"])
        self.compileStatements()
        self.eatAndEmit("symbol", ["}"])

        self.writer.writeGoto("WHILE.{}.{}.EXP".format(self.thisClass, whileInstance))
        self.writer.writeLabel("WHILE.{}.{}.EXIT".format(self.thisClass, whileInstance))

        self.emit(xml="</whileStatement>")

    def compileReturn(self):
        """
        Compiles a return statement.
        """
        self.emit(xml="<returnStatement>")
        self.eatAndEmit("keyword", ["return"])

        # If not a ';', expect an expression
        t = self.tokenizer
        if not (t.tokenType() == "symbol" and t.symbol() == ";"):
            # Expect an expression
            self.compileExpression()
        else:
            # void function, so force a 0 onto the stack to return
            self.writer.writePush("CONST", 0)

        self.writer.writeReturn()
        self.eatAndEmit("symbol", [";"])
        self.emit(xml="</returnStatement>")

    def compileIf(self):
        """
        Compiles an if statement, possibly with a trailing else
        clause.
        """
        self.emit(xml="<ifStatement>")
        self.eatAndEmit("keyword", ["if"])
        self.eatAndEmit("symbol", ["("])
        self.compileExpression()
        self.eatAndEmit("symbol", [")"])

        self.writer.writeArithmetic("U~")
        ifInstance = self.ifCounter
        self.ifCounter += 1
        self.writer.writeIf("IF.{}.{}.ELSE".format(self.thisClass, ifInstance))

        self.eatAndEmit("symbol", ["{"])
        self.compileStatements()
        self.eatAndEmit("symbol", ["}"])

        t = self.tokenizer
        if t.tokenType() == "keyword" and t.keyWord() == "else":
            self.writer.writeGoto("IF.{}.{}.EXIT".format(self.thisClass, ifInstance))
            self.writer.writeLabel("IF.{}.{}.ELSE".format(self.thisClass, ifInstance))

            self.eatAndEmit("keyword", ["else"])
            self.eatAndEmit("symbol", ["{"])
            self.compileStatements()
            self.eatAndEmit("symbol", ["}"])
            self.writer.writeLabel("IF.{}.{}.EXIT".format(self.thisClass, ifInstance))
        else:
            self.writer.writeLabel("IF.{}.{}.ELSE".format(self.thisClass, ifInstance))

        self.emit(xml="</ifStatement>")

    def compileExpression(self):
        """
        Compiles an expression.
        """
        self.emit(xml="<expression>")
        self.compileTerm()

        # Look for operator-term pairs
        t = self.tokenizer
        ops = ["+", "-", "*", "/", "&", "|", "<", ">", "="]
        while t.tokenType() == "symbol" and t.symbol() in ops:
            (_, op) = self.eatAndEmit("symbol", ops)
            self.compileTerm()
            self.writer.writeArithmetic(op)

        self.emit(xml="</expression>")

    def compileTerm(self):
        """
        Compiles a term. This routine is faced with a slight difficulty when
        trying to decide between some of the alternative parsing rules.
        Specifically, if the current token is an identifier, the routine must
        distinguish between a variable, an array entry, and a subroutine call.
        A single lookahead token, which may be one of '[', '(', or '.',
        suffices to distinguish between the three possibilities. Any other
        token is not part of this term and should not be advanced over.
        """
        self.emit(xml="<term>")

        # Get the current token type
        t = self.tokenizer
        tType = t.tokenType()

        # Integer constant
        if tType == "integerConstant":
            (_, value) = self.eatAndEmit("integerConstant")
            self.writer.writePush("CONST", value)
        # String constant
        elif tType == "stringConstant":
            (_, value) = self.eatAndEmit("stringConstant")
            # Declare space for the string
            self.writer.writePush("CONST", len(value))
            self.writer.writeCall("String.new", 1)
            # Save the contents of the string
            for c in value:
                self.writer.writePush("CONST", ord(c))
                self.writer.writeCall("String.appendChar", 2)
        # Keyword constant
        elif tType == "keyword" and t.keyWord() in ["true", "false", "null", "this"]:
            (_, kw) = self.eatAndEmit("keyword", ["true", "false", "null", "this"])
            if kw in ["null", "false"]:
                # Map to 0
                self.writer.writePush("CONST", 0)
            elif kw == "true":
                # Map to -1
                self.writer.writePush("CONST", 1)
                self.writer.writeArithmetic("U-")  # NEG
            else:
                # this
                self.writer.writePush("POINTER", 0)
        # Identifier (varName, or array name, or subroutine call)
        elif tType == "identifier":
            (_, ident) = self.eatAndEmit("identifier", category="TERM", state="USE")
            if t.tokenType() == "symbol":
                symbol = t.symbol()
                if symbol == "[":
                    # Array reference
                    # ident is the array name
                    # Compute the offset
                    self.eatAndEmit("symbol", ["["])
                    self.compileExpression()
                    self.eatAndEmit("symbol", ["]"])
                    # Add base to offset
                    self.writer.writePush(self.symtab.kindOf(ident), self.symtab.indexOf(ident))
                    self.writer.writeArithmetic("+")
                    # Update THAT and retrieve
                    self.writer.writePop("POINTER", 1)
                    self.writer.writePush("THAT", 0)
                elif symbol == "(":
                    # Subroutine call
                    # ident is the subroutine.
                    self.eatAndEmit("symbol", ["("])
                    nArgs = self.compileExpressionList()
                    self.eatAndEmit("symbol", [")"])
                    self.writer.writeCall(ident, nArgs)
                elif symbol == ".":
                    # Method call.
                    # ident is the class name (static method) or the object which will be argument 0 (this).

                    # Look up the object's type in the symbol table. If not found, then it is a class name and there is no object to be "this".
                    objType = self.symtab.typeOf(ident)
                    nArgs = 0
                    if objType is not None:
                        # Push this onto stack as argument 0
                        self.writer.writePush(self.symtab.kindOf(ident), self.symtab.indexOf(ident))
                        nArgs = 1
                    else:
                        # ident is the class name, so use it
                        objType = ident

                    self.eatAndEmit("symbol", ["."])
                    (_, method) = self.eatAndEmit(
                        "identifier", category="SUBROUTINE", state="USE"
                    )
                    self.eatAndEmit("symbol", ["("])
                    nArgs += self.compileExpressionList()
                    self.eatAndEmit("symbol", [")"])
                    self.writer.writeCall(objType + "." + method, nArgs)
                else:
                    # Next token not a symbol, so ident is a simple variable identifier.
                    varKind = self.symtab.kindOf(ident)
                    varIndex = self.symtab.indexOf(ident)
                    self.writer.writePush(varKind, varIndex)
        # Sub-expression
        elif tType == "symbol" and t.symbol() == "(":
            self.eatAndEmit("symbol", ["("])
            self.compileExpression()
            self.eatAndEmit("symbol", [")"])
        # Unary op and term
        elif tType == "symbol" and t.symbol() in ["-", "~"]:
            (_, op) = self.eatAndEmit("symbol", ["-", "~"])
            self.compileTerm()
            # Mark as unary to get right version of '-'
            self.writer.writeArithmetic("U" + op)
        else:
            # Not a term
            raise SyntaxError("Expected term, found {}.".format(t.currentToken))

        self.emit(xml="</term>")

    def compileExpressionList(self):
        """
        Compiles a (possibly empty) comma-separated list of expressions.
        Returns the number of expressions compiled.
        """
        self.emit(xml="<expressionList>")

        # Get the initial token type
        t = self.tokenizer
        tType = t.tokenType()

        # Count the expressions in the list
        nExpressions = 0

        # Closing parenthesis ends the list
        while not (tType == "symbol" and t.symbol() == ")"):
            nExpressions += 1
            self.compileExpression()

            # Expect an optional ','
            if t.tokenType() == "symbol" and t.symbol() == ",":
                self.eatAndEmit("symbol", [","])

            # Update the tType
            tType = t.tokenType()

        self.emit(xml="</expressionList>")

        return nExpressions

    def eat(self, tokenType, tokenVals=None):
        """
        Consume the current token if it matches the expected type and value.
        """
        # Get the type and value of the current token
        t = self.tokenizer
        tType = t.tokenType()
        if tType == "keyword":
            tVal = t.keyWord()
        elif tType == "symbol":
            tVal = t.symbol()
        elif tType == "identifier":
            tVal = t.identifier()
        elif tType == "integerConstant":
            tVal = t.intVal()
        else:  # tType == 'stringConstant'
            tVal = t.stringVal()

        # Verify that the type matches and the value is one of the values
        # expected.
        if not (tType == tokenType and (not tokenVals or tVal in tokenVals)):
            raise SyntaxError(
                "Expected {} {}. Found {}.".format(
                    tokenType, " or ".join(tokenVals or []), t.currentToken
                )
            )

        if t.hasMoreTokens():
            t.advance()

        # Return the actual token type and value
        return (tType, tVal)

    def emit(self, token=None, category=None, state=None, varType=None, xml=None):
        """
        Emit the provided XML or token as XML to the xmlFile.
        Will indent based on the current indentLevel.
        """
        # If XML code not provided, create it from the token type and value
        if not xml:
            (tokenType, tokenVal) = token

            # Handle symbol table additions/lookups
            index = None
            if state == "DEFINE" and category in ["STATIC", "FIELD", "ARG", "VAR"]:
                index = self.symtab.define(tokenVal, varType, category)

            if state == "USE" and category in ["LET", "TERM"]:
                category = self.symtab.kindOf(tokenVal)
                if category:
                    varType = self.symtab.typeOf(tokenVal)
                    index = self.symtab.indexOf(tokenVal)
                else:
                    category = "CLASS OR SUBROUTINE"

            # Define additional output fields
            fields = ""
            if category is not None:
                fields += " category={}".format(category)
            if state is not None:
                fields += " state={}".format(state)
            if varType is not None:
                fields += " varType={}".format(varType)
            if index is not None:
                fields += " index={}".format(index)

            xml = "<{0}{2}>{1}</{0}>".format(
                tokenType, self.xmlProtect(tokenVal), fields
            )

        else:
            # If the XML starts with '</', reduce the indent level
            if xml[:2] == "</":
                self.indentLevel = self.indentLevel - 1

        # Output the XML, indented to the current level
        output = "{}{}\n".format(self.INDENT * self.indentLevel, xml)
        self.writer.writeComment(output)
        if self.DEBUG:
            print(output, end="")

        # If the XML does not contain '</', increase the indent level
        if "</" not in xml:
            self.indentLevel = self.indentLevel + 1

    def eatAndEmit(
        self, tokenType, tokenVals=None, category=None, state=None, varType=None
    ):
        """
        Shorthand for common pattern of eat and emit. Returns the token eaten.
        """
        token = self.eat(tokenType, tokenVals)
        self.emit(token=token, category=category, state=state, varType=varType)

        # Return the token in case the caller wants it
        return token

    def xmlProtect(self, token):
        # Protect <, >, and & tokens from XML
        if token == "<":
            return "<"
        elif token == ">":
            return ">"
        elif token == "&":
            return "&"
        else:
            return token


import sys
sys.stdin = open('bj11054.txt','r')

def DP():
    dp=[0]*N
    for i in range(N):
        dp[i]=1
        for j in range(i):
            if L[i]>L[j]:dp[i]=max(dp[i],dp[j]+1)
    return dp
N=int(input())
L=[int(x)for x in input().split()]
# DP=[0]*N
# RDP=[0]*N
# for i in range(N):
#     DP[i]=1
#     for j in range(i):
#         if L[i]>L[j]:DP[i]=max(DP[i],DP[j]+1)
# for i in range(N-1,-1,-1):
#     RDP[i]=1
#     for j in range(N-1,i-1,-1):
#         if L[i]>L[j]:RDP[i]=max(RDP[i],RDP[j]+1)
# print(max(DP[x]+RDP[x]-1 for x in range(N)))
a=DP()
L.reverse()
b=DP()
b.reverse()
print(max(a[x]+b[x]-1 for x in range(N)))

algoritm/20상반기 코딩테스트/가장 긴 바이토닉 부분 수열/bj11054.py