import subprocess
import time
import re
import os
import hashlib
import requests
import json
from datetime import datetime
from operator import itemgetter

from flask import g
from common_libs.common.dbconnect import *  # noqa: F403
from common_libs.common.util import get_timestamp, get_all_execution_limit, get_org_execution_limit, get_maintenance_mode_setting
from common_libs.ci.util import log_err
from common_libs.ansible_driver.functions.util import rmAnsibleCreateFiles, get_OSTmpPath
from common_libs.ansible_driver.classes.AnscConstClass import AnscConst
from common_libs.ansible_driver.classes.AnslConstClass import AnslConst
from common_libs.ansible_driver.classes.AnspConstClass import AnspConst
from common_libs.ansible_driver.classes.AnsrConstClass import AnsrConst
from common_libs.ansible_driver.functions.util import getAnsibleConst
from common_libs.ansible_driver.classes.controll_ansible_agent import DockerMode, KubernetesMode
from common_libs.common.exception import AppException
from libs import common_functions as cm

# ansible共通の定数をロード
ansc_const = AnscConst()

def backyard_main(common_db, organization_id=None, workspace_id=None):
    """
    [ita_by_ansible_execute]
    main logicのラッパー
    called 実行君
    """
    g.applogger.debug(g.appmsg.get_log_message("BKY-00001"))

    retBool = main_logic(common_db)
    if retBool is True:
        # 正常終了
        g.applogger.debug(g.appmsg.get_log_message("BKY-00002"))
    else:
        g.applogger.debug(g.appmsg.get_log_message("BKY-00003"))

def main_logic(common_db):
    """
    main logic
    """
    container_base = os.getenv('CONTAINER_BASE')
    if container_base == 'docker':
        ansibleAg = DockerMode()
    else:
        ansibleAg = KubernetesMode()

    try:
        # メンテナンスモードのチェック
        try:
            maintenance_mode = get_maintenance_mode_setting()
            # data_update_stopの値が"1"の場合、メンテナンス中のためreturnする。
            if str(maintenance_mode['data_update_stop']) == "1":
                g.applogger.debug(g.appmsg.get_log_message("BKY-00005", []))
                return True
        except Exception:
            # エラーログ出力
            g.applogger.error(g.appmsg.get_log_message("BKY-00008", []))
            return False

        # システム全体の同時実行数取得
        all_execution_limit = get_all_execution_limit("ita.system.ansible.execution_limit")
        # organization毎の同時実行数取得
        org_execution_limit = get_org_execution_limit("ita.organization.ansible.execution_limit")
        g.applogger.debug("START execute_control")
        execution_list, all_exec_count, org_exec_count_list, target_shema = execute_control(common_db, all_execution_limit, org_execution_limit)
        g.applogger.debug("END execute_control")

        # 実行中のコンテナの状態確認
        g.applogger.debug("START child_process_exist_check")
        if child_process_exist_check(common_db, target_shema, ansibleAg) is False:
            g.applogger.debug(g.appmsg.get_log_message("MSG-10059"))
            return False

        if len(execution_list) == 0:
            return True

        # backyard_execute_stopの値が"1"の場合、メンテナンス中のためreturnする。
        if str(maintenance_mode['backyard_execute_stop']) == "1":
            g.applogger.debug(g.appmsg.get_log_message("BKY-00006", []))
            return True

        # 現在の実行数
        crr_count = 0
        for data in execution_list:
            g.ORGANIZATION_ID = data["ORGANIZATION_ID"]
            g.WORKSPACE_ID = data["WORKSPACE_ID"]
            g.applogger.debug("main_logic EXECUTION_NO=" + data["EXECUTION_NO"])
            crr_count += 1
            # 実行前に同時実行数比較
            if all_execution_limit != 0 and crr_count + int(all_exec_count) > int(all_execution_limit):
                return True
            if org_execution_limit[data["ORGANIZATION_ID"]] != 0 and crr_count + int(org_exec_count_list[data["ORGANIZATION_ID"]]) > int(org_execution_limit[data["ORGANIZATION_ID"]]):
                return True

            common_db.db_transaction_start()
            # 対象データのレコードロック
            sql = "SELECT * FROM `" + data["WORKSPACE_DB"] + "`.`" + data["VIEW_NAME"] + "` "
            sql += "WHERE EXECUTION_NO = %s "
            sql += "AND STATUS_ID IN (%s, %s, %s) "
            sql += "FOR UPDATE "
            records3 = common_db.sql_execute(sql, [data["EXECUTION_NO"], ansc_const.NOT_YET, ansc_const.PREPARE, ansc_const.RESERVE])

            if records3 is not None and len(records3) > 0:
                for rec3 in records3:
                    if rec3["WORKSPACE_ID"] == data["WORKSPACE_ID"]:
                        # レコード取得・レコードロック確保できたときは状態を更新し即時COMMIT
                        sql = "UPDATE `" + data["WORKSPACE_DB"] + "`.`" + data["VIEW_NAME"] + "` "
                        sql += "SET EXECUTE_HOST_NAME = %s "
                        sql += "WHERE EXECUTION_NO = %s"
                        common_db.sql_execute(sql, [os.uname()[1], data["EXECUTION_NO"]])
                        common_db.db_commit()
                    else:
                        # 最初からやり直し
                        common_db.db_rollback()
                        return True
            else:
                # 最初からやり直し
                common_db.db_rollback()
                return True

            wsDb = DBConnectWs(data["WORKSPACE_ID"], data["ORGANIZATION_ID"])

            # 未実行（実行待ち）の作業を実行
            g.applogger.debug("START run_unexecuted")
            result = run_unexecuted(wsDb, data["EXECUTION_NO"], data["ORGANIZATION_ID"], data["WORKSPACE_ID"])
            g.applogger.debug("END run_unexecuted")
            if result[0] is False:
                g.applogger.info(result[1])
                wsDb.db_disconnect()
                return False

            wsDb.db_disconnect()

            g.ORGANIZATION_ID = None
            g.WORKSPACE_ID = None
    except AppException as e:
        common_db.db_rollback()
        raise AppException(e)

    return True

def execute_control(common_db, all_execution_limit, org_execution_limit):
    """
    同時実行数の制御

    Returns:
        bool
    """
    # 取得対象schemaの検索
    sql = "SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS "
    sql += "WHERE TABLE_NAME IN ('V_ANSL_EXEC_STS_INST2','V_ANSP_EXEC_STS_INST2','V_ANSR_EXEC_STS_INST2') "

    records = common_db.sql_execute(sql, [])

    if records is not None and len(records) > 0:
        count = 0
        sql = ""
        for rec in records:
            table_schema = rec["TABLE_SCHEMA"]
            table_name = rec["TABLE_NAME"]
            count += 1
            if sql == "":
                sql = "SELECT * FROM `" + table_schema + "`.`" + table_name + "` "
            else:
                sql += "SELECT * FROM `" + table_schema + "`.`" + table_name + "` "

            sql += "WHERE ( `TIME_BOOK` IS NULL AND `STATUS_ID` = " + ansc_const.NOT_YET + " ) "
            sql += "OR (`TIME_BOOK` <= '" + get_now_datetime() + "' AND `STATUS_ID` = " + ansc_const.RESERVE + " ) "

            if len(records) > count:
                sql += "UNION ALL "

        target_records = common_db.sql_execute(sql, [])

        # 全オーガナイゼーションのオーガナイゼーション毎の処理中件数を取得
        sql = "SELECT ORGANIZATION_ID, COUNT(*) AS EXEC_COUNT FROM ( "
        count = 0
        for rec in records:
            table_schema = rec["TABLE_SCHEMA"]
            table_name = rec["TABLE_NAME"]
            count += 1
            sql += "SELECT ORGANIZATION_ID FROM `" + table_schema + "`.`" + table_name + "` "
            sql += "WHERE `STATUS_ID` in (" + ansc_const.PREPARE + ", " + ansc_const.PROCESSING + ", " + ansc_const.PROCESS_DELAYED + ") "
            if len(records) > count:
                sql += "UNION ALL "

        sql += ") T1 GROUP BY ORGANIZATION_ID "
        exec_count_records = common_db.sql_execute(sql, [])

        all_exec_count = 0
        org_exec_count_list = {}
        exclusion_list = []
        if exec_count_records is not None and len(exec_count_records) > 0:
            for rec in exec_count_records:
                all_exec_count += rec["EXEC_COUNT"]
                org_exec_count_list[rec["ORGANIZATION_ID"]] = rec["EXEC_COUNT"]

            # 全オーガナイゼーションの処理件数と上限値比較
            if all_execution_limit != 0 and all_exec_count > int(all_execution_limit):
                return []

            # オーガナイゼーション毎の処理件数と上限値比較
            for rec in exec_count_records:
                if rec["ORGANIZATION_ID"] in org_execution_limit:
                    if org_execution_limit[rec["ORGANIZATION_ID"]] != 0 and rec["EXEC_COUNT"] > org_execution_limit[rec["ORGANIZATION_ID"]]:
                        exclusion_list.append(rec["ORGANIZATION_ID"])

        # 処理対象のソート
        execution_list = []
        organization_priority = 0
        for rec in target_records:
            if rec["ORGANIZATION_ID"] in exclusion_list:
                continue

            # 「オーガナイゼーション毎の処理中件数」/ 「オーガナイゼーションの同時実行数のLimit値
            for rec2 in exec_count_records:
                if rec["ORGANIZATION_ID"] == rec2["ORGANIZATION_ID"]:
                    # 0除算対応
                    if org_execution_limit[rec2["ORGANIZATION_ID"]] == 0:
                        organization_priority = rec2["EXEC_COUNT"]
                    else:
                        organization_priority = rec2["EXEC_COUNT"] // org_execution_limit[rec2["ORGANIZATION_ID"]]

            if rec["TIME_BOOK"] is None:
                time_book = rec["TIME_REGISTER"]
            else:
                time_book = rec["TIME_BOOK"]

            # EXECUTION_NO + VIEW_NAME ＋ ワークスペースID + オーガナイゼーションIDのHASH値
            data = rec["EXECUTION_NO"] + rec["VIEW_NAME"] + rec["WORKSPACE_ID"] + rec["ORGANIZATION_ID"]
            hs = hashlib.md5(data.encode()).hexdigest()
            tmp_dict = {"ORGANIZATION_PRIORITY": organization_priority,
                        "TIME_BOOK": time_book,
                        "TIME_REGISTER": rec["TIME_REGISTER"],
                        "HASH": hs,
                        "ORGANIZATION_ID": rec["ORGANIZATION_ID"],
                        "WORKSPACE_ID": rec["WORKSPACE_ID"],
                        "EXECUTION_NO": rec["EXECUTION_NO"],
                        "WORKSPACE_DB": rec["WORKSPACE_DB"],
                        "VIEW_NAME": rec["VIEW_NAME"]}
            execution_list.append(tmp_dict)
            # 処理対象のソート
            execution_list = sorted(execution_list, key=lambda x: (x["ORGANIZATION_PRIORITY"], x["TIME_BOOK"], x["TIME_REGISTER"], x["HASH"]))

            # organizationに実行中の処理があるか確認
            if rec["ORGANIZATION_ID"] not in org_exec_count_list:
                org_exec_count_list[rec["ORGANIZATION_ID"]] = 0
    return execution_list, all_exec_count, org_exec_count_list, records

def child_process_exist_check(common_db, target_shema, ansibleAg):
    """
    実行中の子プロの起動確認

    Returns:
        bool
    """
    global ansc_const
    # psコマンドでbackyard_child_init.pyの起動プロセスリストを作成
    # psコマンドがマレに起動プロセスリストを取りこぼすことがあるので3回分を作成
    # command = ["python3", "backyard/backyard_child_init.py", organization_id, workspace_id, execution_no, driver_id]

    child_process_1 = child_process_exist_check_ps()
    time.sleep(0.05)
    child_process_2 = child_process_exist_check_ps()
    time.sleep(0.05)
    child_process_3 = child_process_exist_check_ps()

    records = get_running_process(common_db, target_shema)
    for rec in records:
        driver_id = rec["DRIVER_ID"]
        driver_name = rec["DRIVER_NAME"]
        execution_no = rec["EXECUTION_NO"]
        organization_id = rec["ORGANIZATION_ID"]
        workspace_id = rec["WORKSPACE_ID"]

        g.ORGANIZATION_ID = organization_id
        g.WORKSPACE_ID = workspace_id

        wsDb = DBConnectWs(workspace_id)

        ansc_const = getAnsibleConst(driver_id)

        # 子プロ起動確認
        is_running = False
        # command = ["python3", "backyard/backyard_child_init.py", organization_id, workspace_id, execution_no, driver_id]
        command_args = "{} {} {} {} {}".format('backyard/backyard_child_init.py', organization_id, workspace_id, execution_no, driver_id)

        child_process_arr = child_process_1.split('\n')
        for r_child_process in child_process_arr:
            if re.search(command_args, r_child_process) is not None:
                is_running = True

        if is_running is False:
            child_process_arr = child_process_2.split('\n')
            for r_child_process in child_process_arr:
                if re.search(command_args, r_child_process) is not None:
                    is_running = True

        if is_running is False:
            child_process_arr = child_process_3.split('\n')
            for r_child_process in child_process_arr:
                if re.search(command_args, r_child_process) is not None:
                    is_running = True

        # DBのステータスが実行中なのに、子プロセスが存在しない
        if is_running is False:
            g.applogger.info(g.appmsg.get_log_message("MSG-10056", [driver_name, execution_no]))

            # 子プロで/tmpに作成したファイル・ディレクトリパスを保存するファイル名
            g.AnsibleCreateFilesPath = "{}/Ansible_{}".format(get_OSTmpPath(), execution_no)

            # /tmpをゴミ掃除
            rmAnsibleCreateFiles()

            # 情報を再取得して、想定外エラーにする
            result = cm.get_execution_process_info(wsDb, ansc_const, execution_no)
            if result[0] is False:
                g.applogger.info(g.appmsg.get_log_message(result[1], [execution_no]))
                wsDb.db_disconnect()
                return False
            execute_data = result[1]

            # 実行中か再確認
            status_id_list = [ansc_const.PREPARE, ansc_const.PROCESSING, ansc_const.PROCESS_DELAYED]
            if execute_data["STATUS_ID"] in status_id_list:
                # 更新
                wsDb.db_transaction_start()
                time_stamp = get_timestamp()
                data = {
                    "EXECUTION_NO": execution_no,
                    "STATUS_ID": ansc_const.EXCEPTION,
                    "TIME_END": time_stamp,
                    "TIME_START": time_stamp,
                }
                result = cm.update_execution_record(wsDb, ansc_const, data)
                if result[0] is True:
                    wsDb.db_commit()
                    g.applogger.debug(g.appmsg.get_log_message("MSG-10060", [driver_name, execution_no]))

            # コンテナが残っている場合に備えて掃除
            # ゴミ掃除に失敗しても処理は続ける
            result = ansibleAg.is_container_running(execution_no)
            if result[0] is True:
                result = ansibleAg.container_kill(ansc_const, execution_no)
                if result[0] is False:
                    g.applogger.info(g.appmsg.get_log_message("BKY-10007", [result[1], execution_no]))
            else:
                result = ansibleAg.container_clean(ansc_const, execution_no)
                if result[0] is False:
                    g.applogger.info(g.appmsg.get_log_message("BKY-10007", [result[1], execution_no]))

        wsDb.db_disconnect()

    return True

def child_process_exist_check_ps():
    """
    実行中の子プロのpsコマンド取得結果

    Returns:
        stdout row
    """
    # ps -efw | grep backyard/backyard_child_init.py | grep -v grep
    cp1 = subprocess.run(
        ["ps", "-efw"],
        capture_output=True,
        text=True
    )
    if cp1.returncode != 0 and cp1.stderr:
        cp1.check_returncode()

    cp2 = subprocess.run(
        ["grep", "backyard/backyard_child_init.py"],
        capture_output=True,
        text=True,
        input=cp1.stdout
    )
    if cp2.returncode != 0 and cp2.stderr:
        cp2.check_returncode()

    cp3 = subprocess.run(
        ["grep", "-v", "grep"],
        capture_output=True,
        text=True,
        input=cp2.stdout
    )

    if cp3.returncode == 0:
        # 検索結果あり
        return cp3.stdout
    elif cp3.returncode == 1 and not cp3.stderr:
        # 検索結果なし（0行）
        return ""
    else:
        # 例外を起こす
        cp3.check_returncode()

def get_running_process(common_db, target_shema):
    """
    実行中の作業データを取得

    Returns:
        records
    """
    global ansc_const

    if target_shema is not None and len(target_shema) > 0:
        count = 0
        sql = ""
        for rec in target_shema:
            table_schema = rec["TABLE_SCHEMA"]
            table_name = rec["TABLE_NAME"]
            count += 1
            if sql == "":
                sql = "SELECT * FROM `" + table_schema + "`.`" + table_name + "` "
            else:
                sql += "SELECT * FROM `" + table_schema + "`.`" + table_name + "` "

            sql += " WHERE `DISUSE_FLAG` = 0 AND `STATUS_ID` in ('{}', '{}', '{}') ".format(ansc_const.PREPARE, ansc_const.PROCESSING, ansc_const.PROCESS_DELAYED)

            if len(target_shema) > count:
                sql += "UNION ALL "

        records = common_db.sql_execute(sql, [])

    return records

def get_now_datetime(format='%Y/%m/%d %H:%M:%S', type='str'):
    dt = datetime.now().strftime(format)
    if type == 'str':
        return '{}'.format(dt)
    else:
        return dt

def run_unexecuted(wsDb, execution_no, organization_id, workspace_id):
    """
    未実行（実行待ち）の作業を実行

    Returns:
        bool
        err_msg
    """
    global ansc_const

    condition = "WHERE EXECUTION_NO = %s "

    records = wsDb.table_select('V_ANSC_EXEC_STS_INST', condition, [execution_no])

    # 処理対象レコードが0件の場合は処理終了
    if len(records) == 0:
        g.applogger.debug(g.appmsg.get_log_message("MSG-10749"))
        return True,

    for rec in records:

        # データを取り出して、作業実行
        result = run_child_process(wsDb, rec, organization_id, workspace_id)
        if result[0] is False:
            return False, result[1]

    return True,

def run_child_process(wsDb, execute_data, organization_id, workspace_id):
    """
    作業実行するための子プロセスの準備・実行

    Returns:
        bool
        err_msg
    """
    # global ansc_const

    driver_id = execute_data["DRIVER_ID"]
    driver_name = execute_data["DRIVER_NAME"]
    execution_no = execute_data["EXECUTION_NO"]

    ansc_const = getAnsibleConst(driver_id)

    # 処理対象の作業インスタンス情報取得(再取得)
    retBool, result = cm.get_execution_process_info(wsDb, ansc_const, execution_no)
    if retBool is False:
        return False, g.appmsg.get_log_message(result, [execution_no])
    execute_data = result

    # 未実行状態で緊急停止出来るようにしているので
    # 未実行状態かを判定
    status_id = execute_data["STATUS_ID"]
    if status_id != ansc_const.NOT_YET and status_id != ansc_const.RESERVE:
        return False, g.appmsg.get_log_message("BKY-10002", [execution_no])

    # 処理対象の作業インスタンスのステータスを準備中に設定
    wsDb.db_transaction_start()
    data = {
        "EXECUTION_NO": execution_no,
        "STATUS_ID": ansc_const.PREPARE,
    }

    result = cm.update_execution_record(wsDb, ansc_const, data)
    if result[0] is True:
        wsDb.db_commit()

    # 子プロセスにして、実行
    g.applogger.debug(g.appmsg.get_log_message("MSG-10745", [driver_name, execution_no]))

    command = ["python3", "backyard/backyard_child_init.py", organization_id, workspace_id, execution_no, driver_id]
    # cp = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    cp = subprocess.Popen(command)  # noqa: F841

    return True,


import os
import cv2
import imutils
import numpy as np

# importing, identifying and editing the image
def find_marker(image):
    # convert image to grayscale, blur it and detect edges
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(gray, 35, 125)

    # identifying the image (the largest contour)
    cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    c = max(cnts, key=cv2.contourArea)

    # compute the bounding box of the paper region and return it
    return  cv2.mainAreaReact(c)

# the end goal of computing distance to camera
def distance_to_camera(knownWidth, focalLength, perWidth):
    return (knownWidth * focalLength) / perWidth

KNOWN_DISTANCE = 24
KNOWN_WIDTH = 8.3
folder = 'photos'
lst = []

# reading all images in a directory and calculating focal length
for images in os.listdir(folder):
    img = cv2.imread(images)  # image read
    marker = find_marker(img)  # detect the object
    temp_focal_length = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH  # calculate focal lenght from that one image
    lst.append(temp_focal_length)  # put it an empty list to average it
focalLength = sum(lst) / len(lst)  # computing the average

# now loop over all images
for pictures in os.listdir(folder):
    image = cv2.imread(pictures)
    marker = find_marker(pictures)
    inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[0][1])

    # draw a bounding box around the image and display it
    box = cv2.cv.BoxPoints(marker) if imutils.is_cv2() else cv2.boxPoints(marker)
    cv2.putText(image, "%.2fft" % (inches / 12),
                (image.shape[1] - 200, image.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX,
                2.0, (0, 255, 0), 3)
    cv2.imshow("image", image)
    cv2.waitKey(0)


import MySQLdb
from MySQLdb import _mysql
import pybedtools
import re

default_columns = ['chrom', 'chromStart', 'chromEnd']

def ucsc_connect(build):
    """
    Open connection to UCSC MySQL database for a specified reference
    """
    conv = { MySQLdb.FIELD_TYPE.LONG: int }
    db = _mysql.connect(host='genome-mysql.cse.ucsc.edu', user='genome',
                        passwd='', db=build, conv=conv)
    return db

def table_exists(db, table):
    """
    Check if a table exists
    """
    db.query('SELECT count(*) FROM information_schema.TABLES WHERE TABLE_NAME = "{0}"'.format(table))
    check = int(db.store_result().fetch_row(how=0, maxrows=0)[0][0])
    if check == 0:
        return False
    else:
        return True

def _check_hg_compliance(feature, bedpe=False):
    """
    Convert chroms in a bedtool to hg18/hg19 nomenclature
    """

    if 'chr' not in feature.chrom:
        feature.chrom = 'chr' + feature.chrom
    if bedpe and 'chr' not in feature[3]:
        feature[3] = 'chr' + feature[3]

    return feature

def _check_grc_compliance(feature, bedpe=False):
    """
    Convert chroms in a bedtool to GRC nomenclature
    """

    if 'chr' in feature.chrom:
        feature.chrom = str(feature.chrom).replace('chr', '')
    if bedpe and 'chr' in feature[3]:
        feature[3] = str(feature[3]).replace('chr', '')

    return feature

def collapse_query_regions(bins):
    """
    Collapse bins to minimal ucsc query intervals
    """

    query_ranges = bins.cut(range(3)).sort().merge()
    query_ranges = query_ranges.each(_check_hg_compliance).saveas()
    return query_ranges

def constrain_query_regions(query, columns, query_ranges):
    """
    Add regional restrictions to a UCSC query SELECT statement
    """

    def _write_single_constraint(interval):
        c = '(`{0}` = "{1}" AND `{2}` <= {3} AND `{4}` >= {5})'
        c = c.format(columns[0], interval.chrom,
                     columns[1], interval.stop,
                     columns[2], interval.start)
        return c

    constraint = ''
    cidx = 0
    for interval in query_ranges:
        newcons = _write_single_constraint(interval)
        if cidx == 0:
            constraint = constraint + newcons
        else:
            constraint = ' '.join([constraint, 'OR', newcons])
        cidx += 1

    if ' WHERE ' in query:
        query = '{0} AND ( {1} )'.format(query, constraint)
    else:
        query = ' '.join([query, 'WHERE', constraint])

    return query

def parse_table_arg(track):
    """
    Parse table & column input
    """

    if ':' in track:
        table = track.split(':')[0]
        track_opts = track.split(':')[1].split(',')

        columns = [i for i in track_opts if not any(s in i for s in '! = < >'.split())]
        if len(columns) < 3:
            columns = default_columns + columns

        conditions = [i for i in track_opts if any(s in i for s in '! = < >'.split())]
        if len(conditions) == 0:
            conditions = None

    else:
        table = track
        columns = default_columns
        conditions = None

    return table, columns, conditions

def format_conditions(conditions):
    """
    Format conditions into SQL-compliant syntax
    """

    def _form_cond(cond):
        terms = re.sub('[!=<>]', ' ', cond).split()
        col = '`{0}`'.format(terms[0])
        query = terms[1]
        if not query.isdigit():
            query = '"{0}"'.format(query)
        comp = ''.join([c for c in cond if c in set('!=><')])
        form_cond = ' '.join([col, comp, query])
        return form_cond

    form_conds = [_form_cond(cond) for cond in conditions]

    return form_conds

def compose_query(table, db, oformat, query_ranges=None,
                  columns=default_columns, conditions=None,
                  print_query=False):
    """
    Format UCSC query dependent on desired output format
    """

    # Query dependent on desired output behavior
    if oformat == 'bed':

        query = 'SELECT ' + ', '.join(['`{0}`'.format(c) for c in columns])

        query = query + ' from `{0}`'.format(table)

        # Add other conditions, as optioned
        if conditions is not None:
            form_conds = ' AND '.join(format_conditions(conditions))
            query = '{0} WHERE {1}'.format(query, form_conds)

        # Constraint query region, if optioned
        if query_ranges is not None:
            query = constrain_query_regions(query, columns, query_ranges)

    else:
        query = 'SELECT * from `{0}`'.format(table)

    # Useful for debugging purposes
    if print_query:
        print(query)

    return query

def query_table(table, db, oformat='bed', query_ranges=None,
                columns=default_columns, conditions=None):
    """
    Download a single table from UCSC database
    """

    # Query database
    query = compose_query(table, db, oformat, query_ranges, columns, conditions)
    db.query(query)

    # Format output
    if oformat == 'bed':
        result = pybedtools.BedTool(db.store_result().fetch_row(how=0, maxrows=0))

    elif oformat == 'bigwig':
        result = dict(db.store_result().fetch_row(how=1, maxrows=0)[0])
        result = 'http://hgdownload.soe.ucsc.edu' + result['fileName'].decode('utf-8')

    else:
        result = db.store_result().fetch_row(how=1, maxrows=0)

    return result

def query_ucsc(bins, track, columns, conditions, db, action, query_ranges):
    """
    Master function for handling UCSC queries
    """

    elig_bed_actions = 'count count-unique count-pairs any-overlap ' + \
                       'any-pairwise-overlap coverage pairwise-coverage'
    bedpe = 'pairwise' in action

    # Get raw data
    if 'map-' in action:
        if columns is default_columns:
            result = query_table(track, db, 'bigwig', query_ranges)
        else:
            result = query_table(track, db, 'bed', query_ranges,
                                 columns, conditions)
            # Coerce to GRC nomenclature, if necessary
            if 'chr' not in bins[0].chrom:
                result = result.each(_check_grc_compliance, bedpe=bedpe)

    elif action in elig_bed_actions.split():
        result = query_table(track, db, 'bed', query_ranges,
                             columns, conditions)
        # Coerce to GRC nomenclature, if necessary
        if 'chr' not in bins[0].chrom:
            result = result.each(_check_grc_compliance, bedpe=bedpe).saveas()

    else:
        from sys import exit
        exit('ERROR: Unknown action "{0}" in ucsc.query_ucsc'.format(action))

    return result

def subquery_ucsc(bins, track, columns, conditions, db, action, query_ranges,
                  contig):
    """
    UCSC query restricted to a single contig
    """

    sub_ranges = query_ranges.filter(lambda f: f.chrom == contig).saveas()
    result = query_ucsc(bins, track, columns, conditions, db, action, sub_ranges)
    if isinstance(result, pybedtools.BedTool):
        result = result.saveas()

    return result


import requests
from bs4 import BeautifulSoup
import CriterionMovieParse
import argparse

def main():
    args = process_args()
    if args.url:
        url = args.url
        series_name, description, extracted_episode_info = get_series_info(url)
        series_name = "Criterion:" + series_name
        print('Examined ' + url)
        print('++++++++++++++++++++++++++++++++++++++++++++++++++++++')
        print(series_name)
        print(description)
        print('++++++++++++++++++++++++++++++++++++++++++++++++++++++')
        print()
        print()
        i = 0
        for movie in extracted_episode_info:
            i += 1
            time, url = movie
            movie_parser = CriterionMovieParse.MovieParse(url)
            print('++++++++++++++++++++++++++++++++++++++++++++++++++++++')
            print(i)
            print(time)
            print(series_name)
            movie_parser.print_info(time)
            print('++++++++++++++++++++++++++++++++++++++++++++++++++++++')
            print()
            print()

    a = 42

def process_args():
    usage_desc = "This is how you use this thing"
    parser = argparse.ArgumentParser(description=usage_desc)
    parser.add_argument("url", help="URL to parse")
    args = parser.parse_args()
    return args

def get_series_info(url):
    r = requests.get(url)
    soup = BeautifulSoup(r.content, 'html5lib')
    next_url = extract_next_url(soup)
    series_name, description = extract_series_name_and_description(soup)
    series = extract_episode_time_and_url(soup)
    while next_url:
        r = requests.get(next_url)
        soup = BeautifulSoup(r.content, 'html5lib')
        next_url = extract_next_url(soup)
        series += extract_episode_time_and_url(soup)
    return series_name, description, series

def extract_series_name_and_description(soup):
    ret_str = ["NoName", "unused", "NoDescription"]
    table = soup.find('div', attrs={'class': 'collection-details grid-padding-left'})
    if table:
        for string in table.stripped_strings:
            ret_str.append(string)
    return ret_str[0], ret_str[2]

def extract_next_url(soup):
    ret_str = None
    table = soup.find('div', attrs={'class': 'row loadmore'})
    if table:
        for item in table.findAll('a', attrs={'class': 'js-load-more-link'}):
            ret_str = "https://www.criterionchannel.com" + item['href']
    return ret_str

def extract_episode_time_and_url(soup):
    series = []
    table = soup.find('ul', attrs={'class': 'js-load-more-items-container'})
    for item in table.findAll('div', attrs={'class': 'grid-item-padding'}):
        movie = [item.a.text.strip(), item.a['href']]
        series.append(movie)
    return series

if __name__ == "__main__":
    main()


import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import arcpy
import string
from arcpy.sa import *
#Loading plug-ins

if time.time()>time.mktime((2023,12,30,0,0,0,0,0,0)):
	arcpy.AddMessage(u'\u6388\u6743\u8d85\u671f\uff01')
	exit(0)
#term of validity

keywords_dict={}#Null BSM dict

layer_arr=arcpy.GetParameter(0)
keywords_1="ZCQCBSM"

for layer_1 in layer_arr:
	with arcpy.da.SearchCursor(layer_1,[keywords_1]) as cursor:
		for row in cursor:
			keywords_dict[row[0][0:18]]=keywords_dict.get(row[0][0:18],0)+1
		#Extract unique value

	keywords=keywords_dict.keys()
	keynums=keywords_dict.values()

	for i in range(0,len(keywords)):
		expression=arcpy.AddFieldDelimiters(layer_1,keywords_1)+'like\''+keywords[i]+'%\''#SQL query statement by keywords
		with arcpy.da.UpdateCursor(layer_1,[keywords_1,"ZCQCBSM_ZB"],where_clause=expression) as cursor_3:
			if keynums[i]==1:
				for row_3 in cursor_3:
					row_3[1]=keywords[i]+"0000"
					cursor_3.updateRow(row_3)
			else:
				j=1
				for row_3 in cursor_3:
					row_3[1]=keywords[i]+str(j).zfill(4)
					cursor_3.updateRow(row_3)
					j+=1

旧代码/自动生成资产清查标识码/储备土地与利用现状.py

储备土地与利用现状.py


#!/usr/bin/env python
import time
import datetime
from gnippy import PowerTrackClient
import json
import subprocess #calls Tweeting app.

# Define a callback
def callback(activity):

    print(activity) #to standard out.

    tweet_hash = json.loads(activity)
    user = tweet_hash['user']['screen_name']
    #Let do some trigger-tweet to response-tweet latency.
    created_time_str = tweet_hash['created_at']
    time_created = datetime.datetime.strptime(created_time_str,'%a %b %d %H:%M:%S +0000 %Y')
    time_now = datetime.datetime.utcnow()
    seconds = (time_now - time_created).total_seconds()

    #Details for calling separate Tweeting app.
    app_to_call = 'ruby'
    post_tweet_script = 'twitter_post.rb'
    tweet_args = '@' + user + ' thanks for Tweeting! Responding (in ' + "{0:.2f}".format(seconds) + ' seconds) from a Raspberry Pi 3 running a Ruby-based Tweeting app.'

    result = subprocess.check_output([app_to_call, post_tweet_script, tweet_args], cwd='./')

# Create the client
client = PowerTrackClient(callback, url="https://{my.gnip.stream.json", auth=("me@there.com", "mYPaSsWoRd"))
client.connect()

while True:
  x = 1


import math
n = int(input())

x, y = 0, 0
ans = float('inf')
for i in range(1, n+1):
    for j in range(i, n+1):
        if i*j>n:
            break
        ans = min(ans, n-i*j+abs(i-j))
print(ans)


import sys
sys.stdin = open("실패율_input.txt")

from collections import Counter

N = int(input())
stages = list(map(int, input().split()))

answer = []
result = []
stageCnt = Counter(stages)
# print(stageCnt)
for stage in range(1, N+1):
    playCnt = 0
    clear = 0
    # print('---------------')
    for key in stageCnt.keys():
        # print(key)
        if key == stage:
            playCnt += stageCnt[key]
        elif key > stage:
            clear += stageCnt[key]
    if (playCnt + clear) != 0:
        result.append([stage, playCnt / (playCnt + clear)])
    else:
        result.append([stage, 0])
# print(result)
result.sort(key=lambda x: x[1], reverse=True)
for i in range(len(result)):
    answer.append(result[i][0])
print(answer)


#!/usr/bin/env python
# encoding: utf-8
"""
检测一个字符串是否为回文字符串
@author: cbr
"""

def is_palindrome_str(s1):
    if not isinstance(s1, str) or len(s1) < 1:
        return False

    s = list(s1)
    begin = 0
    end = len(s) - 1

    while begin < end:
        if s[begin] != s[end]:
            return False
        begin += 1
        end -= 1
    return True

if __name__ == '__main__':
    t1 = "aba"
    t2 = "abba"
    t3 = "bc"
    for t in t1, t2, t3:
        print(is_palindrome_str(t))


from __future__ import print_function

from functools import partial
import multiprocessing as mp

import numpy as np
import scipy as sp
from scipy.optimize import curve_fit as lsq

import pandas as pd

from tqdm import tqdm

import hyperspy.api as hs

import matplotlib.pyplot as plt
plt.ion()

def _fit_model(data, bkgd_shape=None, lsqKwargs=None):
    '''
    Function that performs curve fitting based on use of
    scipy.optimize.curve_fit

    Params
    ------
    data : ndarray
        The data to be used in the model.  Prepared by the function
        MLLSmodel._prepare_model_data()
    bkgd_shape : string or None
        If not None, must be a string containing the name of one of
        the available background shapes:
        - power law
        - log-linear
        - double power law
    lsqKwargs : dict
        Keword arguments to be taken by scipy.optimize.curve_fit

    Returns
    -------
    opt_cofs : ndarray
        The optimised fit coefficients
    cov : ndarray
        The covariance matrix of the fit
    '''
    if lsqKwargs is None:
        lsqKwargs = {}

    y_obs = data[0,:]
    x = data[1,:]
    components = data[2:,:]

    def background(t, coeffs):
        if bkgd_shape=='power law':
            return coeffs[-2] * pow(t, coeffs[-1])
        if bkgd_shape=='log-linear':
            return np.log(coeffs[-2] * pow(t, coeffs[-1]))
        if bkgd_shape=='double power law':
            return coeffs[-4] * pow(t, coeffs[-3]) + coeffs[-2] * pow(t, coeffs[-1])

    def model(t, *coeffs):
        y = 0.0
        for i in range(components.shape[0]):
            y += coeffs[i] * components[i,:]
        if bkgd_shape:
            y += background(t, coeffs)
        return y

    opt_cofs, cov = lsq(model, x, y_obs, **lsqKwargs)

    # self.last = [opt_cofs, cov]
    return opt_cofs, cov

def _normalEqn(X, y):
    '''
    Calculate solution to linear regression parameters by matrix inversion.
    theta = ((X'X)^-1)X'y

    Parameters
    ----------
    X : ndarray
        Matrix of fit components
    y : ndarray
        Observed data points
    Returns
    -------
    theta : ndarray
        Optimised model parameters
    '''
    theta = np.zeros((X.shape[1], 1), dtype=np.float32)

    theta = np.dot(np.dot(np.linalg.pinv(np.dot(X.T, X)), X.T).astype(np.float32), y.astype(np.float32))

    return theta

def solo_normal_solver(stds, comps, data, data_range, LL=None, plot=False):
    '''
    Solve multivariate linear regression using matrix inversion.

    Parameters
    ----------
    stds :

    comps :

    data :

    data_range :

    LL :

    plot :
    Returns
    -------
    theta :
    '''
    # First, reset standards library
    stds.set_all_inactive()
    # Then use comps to specify which Standards are to be used
    stds.set_active_standards(comps)
    # Now, use the data range provided to set the fit range in the data
    try:
        Y = data.deepcopy()
        Y.crop(start=data_range[0], end=data_range[1], axis=0)
    except:
        raise Exception('Problem cropping observed data.')
    # If crop of data succeeds, apply same to active Standards
    stds.set_spectrum_range(data_range[0], data_range[1])
    # If Low-loss is provided, convolve standards
    if LL:
        stds.convolve_ready(LL, kwargs={'stray':True})
        stds.model = stds.conv
    else:
        stds.model = stds.ready

    # Build component matrix
    x_mat = np.array([stds.model[comp].data for comp in comps]).T
    y_obs = Y.data.T
    # Now compute solution
    theta = _normalEqn(x_mat, y_obs)

    return theta

class MLLSmodel_old:
    '''
    Class for handling standard MLLS fitting of spectra.
    '''
    def __init__(self, core_loss, standards):
        '''
        Parameters
        ----------
        core_loss : SQuEELS Data object
            The core-loss data to be modelled.
        standards : SQuEELS Standards object
            The standards library containing the references to be used
            as fit components
        '''

        self.stds = standards
        self.HL = core_loss

        self.dims = core_loss.data.data.shape
        self.nDims = len(self.dims)
        self.sigDim = core_loss.data.axes_manager.signal_indices_in_array[0]

    def model_point(self, coords, comps, init_guess, fit_background=None, lsqKwargs=None):
        '''

        Parameters
        ----------
        coords : tuple of ints

        comps : list of strings

        init_guess : list of floats

        fit_background : string or None

        Returns
        -------
        coefficients : array of floats

        '''
        if lsqKwargs is None:
            lsqKwargs = {}

        def background(t, coeffs):
            if fit_background=='power law':
                return coeffs[-2] * pow(t, coeffs[-1])
            if fit_background=='log-linear':
                return np.log(coeffs[-2] * pow(t, coeffs[-1]))
            if fit_background=='double power law':
                return coeffs[-4] * pow(t, coeffs[-3]) + coeffs[-2] * pow(t, coeffs[-1])

        def model(t, *coeffs):
            y = 0.0
            for i, comp in enumerate(comps):
                y += coeffs[i] * self.stds.ready[comp].inav[coords].data
            if fit_background:
                y += background(t, coeffs)
            return y

        y_Obs = self.HL.data.inav[coords].data
        t = self.HL.data.axes_manager[self.sigDim].axis

        opt_cofs, cov = lsq(model, t, y_Obs, p0=init_guess, **lsqKwargs)

        self.last = opt_cofs
        return opt_cofs

    def multimodel(self, comps, initial_guesses, background=None, update_guesses=True, lsqKwargs=None):
        '''
        This function is a wraparound for MLLSmodel.model_point which is used
        to process all spectra in a spectrum image.
        '''
        self.comps = comps
        df = pd.DataFrame(columns=['coord',*comps])

        points = np.prod(self.HL.data.axes_manager.shape[:-1])

        with tqdm(desc='Quantifying SI', total=points, unit='spectra') as pbar:
            for idx in np.ndindex(self.HL.data.axes_manager.shape[:-1]):
                results = {'coord': idx, }

                coeffs = self.model_point(idx, comps, initial_guesses, fit_background=background, lsqKwargs=lsqKwargs)

                for i, comp in enumerate(comps):
                    results[comp] = coeffs[i]
                if background:
                    results['bkgd_A'] = coeffs[-2]
                    results['bkgd_r'] = coeffs[-1]
                if update_guesses:
                    initial_guesses = coeffs

                df = df.append(results, ignore_index=True)
                pbar.update(1)

        self.multimodel_results = df
        return df

    def map_model_function(self, comps, initial_guesses, parallel=True, nCores=None, mapUnit=None, background=None, update_guesses=True, lsqKwargs=None):
        '''
        Alternative model function to MLLSmodel.multimodel
        Intended to be compatible with multiprocessing
        '''
        if nCores is None:
            nCores = mp.cpu_count()
        if mapUnit is None:
            mapUnit = nCores

        self.comps = comps

        points = np.prod(self.HL.data.axes_manager.shape[:-1])

        hold = np.empty(points, dtype=object)

        with tqdm(desc='Quantifying SI', total=points, unit='spectra') as pbar:
            idx = np.ndindex(self.HL.data.axes_manager.shape[:-1])

            for j in range(int(np.ceil(points/mapUnit))):

                idx_to_map = list()
                for i in range(mapUnit):
                    idx_to_map.append(next(idx))

                print(idx_to_map)

                params = (comps, initial_guesses)
                kwparams = { 'fit_background':background,
                    'lsqKwargs':lsqKwargs }

                part_func = partial(self.model_point, *params, **kwparams)

                if parallel:
                    pool = mp.Pool(processes=nCores)
                    coeffs = pool.map(part_func, idx_to_map)
                    pool.close()
                else:
                    coeffs = list(map(part_func, idx_to_map))

                t = np.empty(len(coeffs), dtype=object)
                t[:] = coeffs
                hold[j*mapUnit:(j*mapUnit+len(idx_to_map))] = t

            pbar.update(mapUnit)

        return rtn

    def generate_element_maps(self):
        '''
        Using dataframe containing multimodel results, extract elemental
        quantities into ndarrays for ease of inspection.
        '''
        self.qmaps = dict()

        canvas = np.zeros(self.dims[:-1])

        for comp in self.comps:
            qmap = canvas.copy()
            for i in range(len(self.multimodel_results)):
                qmap[self.multimodel_results['coord'][i][::-1]] = self.multimodel_results[comp][i]
            self.qmaps[comp] = qmap

    def generate_percentage_maps(self):
        '''
        Generate composition percentages based on elemental maps created using
        generate_element_maps method.
        '''
        self.pmaps = dict()

        total = np.zeros(self.dims[:-1])

        for comp in self.comps:
            total += self.qmaps[comp]

        for comp in self.comps:
            self.pmaps[comp] = 100* self.qmaps[comp]/total

class MLLSmodel:
    '''
    Class for handling standard MLLS fitting of spectra.
    '''
    def __init__(self, core_loss, standards):
        '''
        Parameters
        ----------
        core_loss : SQuEELS Data object
            The core-loss data to be modelled.
        standards : SQuEELS Standards object
            The standards library containing the references to be used
            as fit components
        '''

        self.stds = standards
        self.HL = core_loss

        self.dims = core_loss.data.data.shape
        self.nDims = len(self.dims)
        self.sigDim = core_loss.data.axes_manager.signal_indices_in_array[0]

    def _prepare_model_data(self, idx, fit_components):
        '''
        Prepares the data required for the fitting model in a single
        numpy ndarray.  The structure of this array is based on rows
        containing:
        Row 0: Observed y-values (i.e. the spectrum)
        Row 1: x-values corresponsing to y-values
        Row 2+: The reference spectra to be fitted to the observed data.

        Parameters
        ----------
        idx : tuple
            The indices of the spectrum within the SI to be extracted into
            the numpy array
        fit_components : list of strings
            The names of the reference spectra to be included as components
            in the fit.

        Returns
        -------
        rtn : ndarray
            An (n_comps+2) by spectrum-length numpy array
        '''
        n_comps = len(fit_components)
        x = self.HL.data.axes_manager[-1].axis

        rtn = np.empty((2+n_comps, self.dims[-1]))

        rtn[0,:] = self.HL.data.inav[idx].data
        rtn[1,:] = x
        for i in range(n_comps):
            comp = fit_components[i]
            rtn[2+i,:] = self.stds.ready[comp].inav[idx].data

        return rtn

    def _fit_model(self, data, bkgd_shape=None, lsqKwargs=None):
        '''
        Function that performs curve fitting based on use of
        scipy.optimize.curve_fit

        Parameters
        ----------
        data : ndarray
            The data to be used in the model.  Prepared by the function
            MLLSmodel._prepare_model_data()
        bkgd_shape : string or None
            If not None, must be a string containing the name of one of
            the available background shapes:
            - power law
            - log-linear
            - double power law
        lsqKwargs : dict
            Keword arguments to be taken by scipy.optimize.curve_fit

        Returns
        -------
        opt_cofs : ndarray
            The optimised fit coefficients
        cov : ndarray
            The covariance matrix of the fit
        '''
        if lsqKwargs is None:
            lsqKwargs = {}

        y_obs = data[0,:]
        x = data[1,:]
        components = data[2:,:]

        def background(t, coeffs):
            if bkgd_shape=='power law':
                return coeffs[-2] * pow(t, coeffs[-1])
            if bkgd_shape=='log-linear':
                return np.log(coeffs[-2] * pow(t, coeffs[-1]))
            if bkgd_shape=='double power law':
                return coeffs[-4] * pow(t, coeffs[-3]) + coeffs[-2] * pow(t, coeffs[-1])

        def model(t, *coeffs):
            y = 0.0
            for i in range(components.shape[0]):
                y += coeffs[i] * components[i,:]
            if bkgd_shape:
                y += background(t, coeffs)
            return y

        opt_cofs, cov = lsq(model, x, y_obs, **lsqKwargs)

        # self.last = [opt_cofs, cov]
        return opt_cofs, cov

    def model_single_spectrum(self, index, fit_components, bkgd_shape=None, rtn_cov=False, lsqKwargs=None):
        '''
        Model the spectrum in the SI at position *index using the references
        specified in fit_components.
        May include a background by specifying bkgd_shape.

        Parameters
        ----------
        index : tuple
            A 2-tuple of ints specifying the inav coordinates of the spectrum
            to be modelled.
        fit_components : list of strings
            A list of the names of reference spectra to be used as components
            in the model.
        bkgd_shape : string or None
            If specified, the name of the background type to be fitted.
            See MLLSmodel._fit_model docstring for available shapes.
        rtn_cov : bool
            If true, the covariance matrix for the fit is also returned.
        lsqKwargs : dict
            Keyword arguments to be passed through to scipy.optimize.curve_fit

        Returns
        -------
        results : ndarray

        '''
        fit_data = self._prepare_model_data(index, fit_components)

        results, cov = self._fit_model(fit_data, bkgd_shape=bkgd_shape, lsqKwargs=lsqKwargs)

        if rtn_cov:
            return results, cov
        else:
            return results

    def model_full_SI(self, fit_components, bkgd_shape=None, rtn_cov=False, lsqKwargs=None):
        '''
        Model all spectra in the SI and return results.

        Parameters
        ----------
        fit_components : list of strings
            A list of the names of reference spectra to be used as components
            in the model.
        bkgd_shape : string or None
            If specified, the name of the background type to be fitted.
            See MLLSmodel._fit_model docstring for available shapes.
        rtn_cov : bool
            If true, the covariance matrices are also stored and returned.
        lsqKwargs : dict
            Dictionary containing keword arguments to be passed to
            scipy.optimize.curve_fit

        Returns
        -------
        rtn : ndarray
        '''
        n_points = np.prod(self.dims[:-1])

        hold = np.empty(self.dims[:-1], dtype=object)

        if rtn_cov:
            hold_cov = np.empty(self.dims[:-1], dtype=object)

        with tqdm(desc='Quantifying SI', total=n_points, unit='spectra') as pbar:
            for index in np.ndindex(self.dims[:-1]):
                fit_data = self._prepare_model_data(index, fit_components)
                results, cov = self._fit_model(fit_data, bkgd_shape=bkgd_shape, lsqKwargs=lsqKwargs)
                hold[index] = results
                if rtn_cov:
                    hold_cov[index] = cov
                pbar.update(1)

        rtn = hold
        if rtn_cov:
            cov = hold_cov
            return rtn, cov
        else:
            return rtn

    def model_full_SI_new(self, fit_components, bkgd_shape=None,
            lsqKwargs=None, parallel=True, n_cores=None, chunk=16):
        '''
        Model all spectra in the SI and return results.

        Parameters
        ----------
        fit_components : list of strings
            A list of the names of reference spectra to be used as components
            in the model.
        bkgd_shape : string or None
            If specified, the name of the background type to be fitted.
            See MLLSmodel._fit_model docstring for available shapes.
        rtn_cov : bool
            If true, the covariance matrices are also stored and returned.
        lsqKwargs : dict
            Dictionary containing keword arguments to be passed to
            scipy.optimize.curve_fit
        parallel : bool
            If true, use python multiprocessing to utilise multiple CPU cores.
        n_cores : int
            The number of cores to execute the calculation on.  If None, will
            detect and use the maximum available cores.
        chunk : int
            To manage memory more efficiently, chunks of the SI are prepared and
            passed to the mapped function.  'chunk' is the max number of spectra
            to be included in an individual chunk.

        Returns
        -------
        rtn : ndarray
        '''
        if n_cores is None:
            n_cores = mp.cpu_count()

        if lsqKwargs is None:
            lsqKwargs = {'p0':np.zeros(len(fit_components))}

        n_points = np.prod(self.dims[:-1])

        hold = np.empty(n_points, dtype=object)

        with tqdm(desc='Quantifying SI', total=n_points, unit='spectra') as pbar:
            indices = np.ndindex(self.dims[:-1])

            for j in range(int(np.ceil(n_points/chunk))):
                fit_data = list()
                for i in range(chunk):
                    fit_data.append(self._prepare_model_data(next(indices), fit_components))

                kwargs = {'bkgd_shape':bkgd_shape, 'lsqKwargs':lsqKwargs}

                part_func = partial(_fit_model, **kwargs)

                if parallel:
                    pool = mp.Pool(processes=n_cores)
                    coeffs = pool.map(part_func, fit_data)
                    pool.close()
                else:
                    coeffs = list(map(part_func, fit_data))

                t = np.empty(len(coeffs), dtype=object)
                t[:] = coeffs
                hold[j*chunk:(j*chunk+len(fit_data))] = t

                pbar.update(len(fit_data))

        rtn = hold
        return rtn

    def model_full_SI_new_nochunk(self, fit_components, bkgd_shape=None,
            lsqKwargs=None, parallel=True, n_cores=None):
        '''
        Model all spectra in the SI and return results.

        Parameters
        ----------
        fit_components : list of strings
            A list of the names of reference spectra to be used as components
            in the model.
        bkgd_shape : string or None
            If specified, the name of the background type to be fitted.
            See MLLSmodel._fit_model docstring for available shapes.
        rtn_cov : bool
            If true, the covariance matrices are also stored and returned.
        lsqKwargs : dict
            Dictionary containing keword arguments to be passed to
            scipy.optimize.curve_fit
        parallel : bool
            If true, use python multiprocessing to utilise multiple CPU cores.
        n_cores : int
            The number of cores to execute the calculation on.  If None, will
            detect and use the maximum available cores.

        Returns
        -------
        rtn : ndarray
        '''
        if n_cores is None:
            n_cores = mp.cpu_count()

        if lsqKwargs is None:
            lsqKwargs = {'p0':np.zeros(len(fit_components))}

        n_points = np.prod(self.dims[:-1])

        hold = np.empty(self.dims[:-1], dtype=object)

        observed = self.HL.data.data
        x = self.HL.data.axes_manager[-1].axis
        x1 = np.repeat(x[np.newaxis,:], self.dims[-2], axis=0)
        x2 = np.repeat(x1[np.newaxis,:,:], self.dims[0], axis=0)
        n_comps = len(fit_components)

        fit_data = np.empty((*self.dims[:-1], 2+n_comps, self.dims[-1]))
        fit_data[...,0,:] = observed
        fit_data[...,1,:] = x2

        for i in range(n_comps):
            comp = fit_components[i]
            fit_data[...,2+i,:] = self.stds.ready[comp].data

        chunk_axis = np.argmin(self.dims[:-1])
        non_chunk_axis = np.argmax(self.dims[:-1])

        with tqdm(desc='Quantifying SI', total=n_points, unit='spectra') as pbar:
            # indices = np.ndindex(self.dims[:-1])

            for j in range(self.dims[chunk_axis]):
                fit_chunk = list(fit_data.take(indices=j, axis=chunk_axis))

                kwargs = {'bkgd_shape':bkgd_shape, 'lsqKwargs':lsqKwargs}

                part_func = partial(_fit_model, **kwargs)

                if parallel:
                    pool = mp.Pool(processes=n_cores)
                    coeffs = pool.map(part_func, fit_chunk)
                    pool.close()
                else:
                    coeffs = list(map(part_func, fit_chunk))

                t = np.empty(len(coeffs), dtype=object)
                t[:] = coeffs
                hold.swapaxes(0, chunk_axis)[j] = t

                pbar.update(self.dims[non_chunk_axis])

        # Once fitting is complete, need to rehash data into usable
        # format.
        try:
            f = hold.ravel()
            fr, fc = zip(*f)
            fra = np.vstack(fr).reshape(hold.shape + (-1,))
            fca = np.vstack(fc).reshape(hold.shape + (-1,))

            rtn = np.rollaxis(fra, -1, 0)
            cov = np.rollaxis(fca, -1, 0)

        except ValueError:
            rtn = hold
            cov = 0

        return rtn, cov


import Person_Model
from collections import defaultdict
class Viewer:
    def __init__(self,person):
        self.person = person
    def printAll(self):
        self.printGraveInformation()
    def printGraveInformation(self):
        print(self.person.options)
        numberNoters = defaultdict(lambda: 'th')
        numberNoters['1'], numberNoters['2'], numberNoters['3'] = 'st','nd','rd'

        if len(self.person.names["First"]) > 1: # Only one first name can be assigned
            print("Type which first name to include (type in a number):\n",
                  *[str(self.person.names["First"].index(item)+1)+"."+item+"\n" for item in self.person.names["First"]])
            self.person.idealNames["First"] = self.person.names["First"][int(input("Enter a number from 1 to "+str(len(self.person.names["First"]))+": "))-1]
            print()
        else: # if only one, just use that one
            self.person.idealNames["First"] = self.person.names["First"][0]

        if "Middle" not in self.person.names:
            self.person.idealNames["Middle"] = []
        elif len(self.person.names["Middle"]) > 1: # Multiple middle names can be assigned, but which ones should?
            print("Type which middles names to include (type in a single or string of numbers):\n",
                  *[str(self.person.names["Middle"].index(item)+1)+"."+item+"\n" for item in self.person.names["Middle"]])
            self.person.idealNames["Middle"] = [self.person.names["Middle"][index-1]
                                                for index in list(map(int,input("Enter a sequence of number including any from 1 to "+str(len(self.person.names["Middle"]))+
                                                                                " (separate with spaces): ").split()))]
            print()
        elif "Middle" in self.person.names: # if only one, just use that one
            self.person.idealNames["Middle"] = self.person.names["Middle"]

        if len(self.person.names["Last"]) > 1: # Only one last name can be assigned
            print("Type which last name to include (type in a number):\n",
                  *[str(self.person.names["Last"].index(item)+1)+"."+item+"\n" for item in self.person.names["Last"]])
            self.person.idealNames["Last"] = self.person.names["Last"][int(input("Enter a number from 1 to "+str(len(self.person.names["Last"]))+": "))-1]
        else:
            self.person.idealNames["Last"] = self.person.names["Last"][0]
        print()

        # Every month's abreviation mapped to their full word
        convertMonth = {"JAN":"January","FEB":"February","MAR":"March","APR":"April",
                        "JUN":"June","JUL":"July","AUG":"August","SEP":"September",
                        "OCT":"October","NOV":"November","DEC":"December"}
        if len(self.person.dates["Birth"]) == 3:
            monthB = self.person.dates["Birth"][1]
            # makes months into their full form if not already
            if monthB.upper() in convertMonth:
                monthB = convertMonth[monthB.upper()]
        if len(self.person.dates["Death"]) == 3:
            monthD = self.person.dates["Death"][1]
            if monthD.upper() in convertMonth:
                monthD = convertMonth[monthD.upper()]

        #try:
        print(self.person.idealNames["First"], # first name
              *self.person.idealNames["Middle"], # middle names
              self.person.idealNames["Last"],end=" ") # last name

        if len(self.person.dates["Birth"]) == 1:
            print("was born in the year",self.person.dates["Birth"][0])
        elif len(self.person.dates["Birth"]) == 3:
            birthDay = self.person.dates["Birth"][0]
            # date notation determined in options.txt
            if self.person.options["Dates"].lower() == "british":
                print("was born the",birthDay+(numberNoters[birthDay[-1]] if not (birthDay[0] == "1" and len(birthDay) ==2) else "th"),
                      "of",monthB+",",self.person.dates["Birth"][2],end="") # birth date
            else:
                print("was born",monthB,birthDay+",",self.person.dates["Birth"][2],end="") # birth date

##        except:
##            print("Current knowledge of birth of this individual is unknown.")

        if "BirthPlace" in self.person.events:
            # Prints grave citation and birth place w/ citation if the birthplace is given
            print('<!--\n\n---><ref name="Grave">'+self.person.citation+'</ref><!--\n\n-->',"in",self.person.events["BirthPlace"][0]+
                  '.<!--\n\n--><ref name="I'+str(self.person.events["BirthPlace"][2])[1:]+'">'+ # citation
                  self.person.events["BirthPlace"][1]+'</ref><!--\n\n-->')

        else: # Prints grave citation
            print('.<!--\n\n---><ref name="Grave">'+self.person.citation+'</ref><!--\n\n-->',end=" ")

        if len(self.person.dates["Death"]) == 1:
            print(self.person.idealNames["First"],"passed away in the year",
                  self.person.dates["Death"][0],"and",end=" ") # death date
        if len(self.person.dates["Death"]) == 3:
            deathDay = self.person.dates["Death"][0]

            if self.person.options["Dates"].lower() == "british":
                print(self.person.idealNames["First"],"passed away the",
                      deathDay+(numberNoters[deathDay[-1]] if not (deathDay[0] == "1" and len(deathDay) == 2) else "th"),"of",monthD+",",
                      self.person.dates["Death"][2],"and",end=" ") # death date
            else:
                print(self.person.idealNames["First"],"passed away", monthD, deathDay+",",
                      self.person.dates["Death"][2],"and",end=" ")
        else:
            print("he" if self.person.gender == "Male" else "she",end=" ")

        # Prints cemetery
        location = ''.join([self.person.cemetery[area]+", " if self.person.cemetery[area] != '' else '' for area in ["City","County","State"]])[:-2]
        print("was buried in",self.person.cemetery["Cemetery"],"in",location+'.<!--\n\n--><ref name="Grave"/>')

if __name__ == '__main__':
    person = Person_Model.Person()

    person.addName("First","John")
    person.addName("First","Bob")
    person.addName("Middle","Jacob")
    person.addName("Middle","George")
    person.addName("Last","JingleheimerSchmit")

    person.gender = "Male"

    person.citation = "Austin Lowery. First Hand Recollection. 13 Nov 2019."

    person.addDate("Birth",["APR","12","1970"])
    person.addDate("Death",["DEC","2","2013"])

    person.addCemetery("Cemetery","New Hope Cemetery")
    person.addCemetery("City","Charlotte")
    person.addCemetery("County","Mecklenburg")
    person.addCemetery("State","North Carolina")

    view = Viewer(person)
    view.printAll()


import cv2
import glob
import os
import numpy as np
from keras.utils.np_utils import to_categorical
import scipy.misc
import matplotlib.pyplot as plt
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from tqdm import tqdm
import pickle
from skimage import io
import tables
import numpy as np

LABEL_PATH = '/mnt/DataStorage/NIH/Data_Entry_2017.csv'
LABEL_BBOX_PATH = '/mnt/DataStorage/NIH/BBox_List_2017.csv'

def load_hp_data_hd5_tf(data_path, image_size, label_path, labels_size, hd5_name):
    df_train = pd.read_csv(label_path)

    targets_count = np.zeros(labels_size)

    data_model = tables.open_file(hd5_name, mode='w')
    atom = tables.Float64Atom()
    X = data_model.create_earray(data_model.root, 'data', atom, (0, image_size, image_size, 4))
    y = data_model.create_earray(data_model.root, 'tag', atom, (0, labels_size))
    idx = 0
    for row in tqdm(df_train.values[:]):
        f = row[0]
        tags = row[1]

        tag_files = [data_path+'/{}'.format(f)+'_blue.png', data_path+'/{}'.format(f)+'_green.png', data_path+'/{}'.format(f)+'_blue.png', data_path+'/{}'.format(f)+'_yellow.png']

        if not os.path.exists(tag_files[0]):
            continue

        new_image = np.zeros((0, image_size, image_size))
        #print(file_name)
        for file in tag_files:
            idx += 1

            img = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
            #print('print img shape = ', img.shape)
            #img = cv2.resize(img, (image_size, image_size))

                #print('show origin image')
                #io.imshow(img)
                #io.show()
                #print('show flip image')
                #io.imshow(img_f)
                #io.show()

            img = img / 255.
            img = np.expand_dims(img, axis=0)
            new_image = np.vstack((new_image, img))
        targets = np.zeros(labels_size)
        targets = targets.reshape(1, len(targets))
        for t in tags.split(' '):
            targets[0][int(t)] = 1
            targets_count[int(t)] += 1
        #print('new_image shape = ', new_image.shape)
        new_image = np.transpose(new_image, (1,2,0))
        new_image = np.expand_dims(new_image, axis=0)
        X.append(new_image)
        y.append(targets)

    data_model.close()
    print('tags count = ', targets_count)

def unison_shuffled_copies(a, b):
    assert len(a) == len(b)
    p = np.random.permutation(len(a))
    return p


from math import exp

sigma = 5.669e-8;
eps = 1e-5;

t_end = 36000.0
L = 0.6
H = 0.4
lamda = 0.16
ro = 1190.0
c = 1900.0
kapa = 50.0
Te = 200.0
eps1 = 0.8
T0 = 300.0

def output_result(Nx, Ny, hx, hy, tau):
	print('Длина пластины L = ', L);
	print('Толщина пластины H = ', H);
	print('Число узлов по пространственной координате x в пластине Nx = ', Nx);
	print('Число узлов по пространственной координате y в пластине Ny = ', Ny);
	print('\n')
	print('Начальная температура T0 = ', T0);
	print('Температура внешней среды Te = ', Te);
	print('\n');
	print('Результат получен с шагом по координате x hx = ', hx);
	print('Результат получен с шагом по координате y hy = ', hy);
	print('\n');
	print('Результат получен с шагом по времени tau = ', tau);
	print('Температурное поле в момент времени t = ', t_end);

def form_result(hx, hy, Nx, Ny, T):
	X = [0.0 for i in range(Nx)]
	for i in range(Nx):
		X[i] = hx * i

	Y = [0.0 for j in range(Ny)]
	for j in range(Ny):
		Y[j] = hy * j

	Z = [[0.0] * Ny for i in range(Nx)]
	for i in range(Nx):
		for j in range(Ny):
			Z[i][j] = T[i+1][j+1]

	return X, Y, Z

def f(x, x0, y, y0):
	A = 100000.0
	alpha = 2
	return A * exp(-alpha * (sqr(x - x0) + sqr(y - y0)));

def sqr(x):
	return x**2

def difference_model(Nx, Ny, Cx, Cy):
	#определяем расчетные шаги сетки по пространственным координатам
	hx = L / (Nx - 1);
	hy = H / (Ny - 1);

	N1x = Cx
	N1y = Cy

	#определяем расчетный шаг сетки по времени
	tau = t_end / 1000.0;

	#определяем коэффициент температуропроводности
	a = lamda/(ro*c)

	T = [[T0] * (Ny+1) for i in range(Nx+1)]
	Tn = [[0.0] * (Ny+1) for i in range(Nx+1)]

	alfa = [0.0 for i in range(Nx+1)]
	beta = [0.0 for i in range(Ny+1)]

	#проводим интегрирование нестационарного уравнения теплопроводности
	time = 0.0;
	while time < t_end:
		time = time + tau;

		#запоминаем поле температуры на n-ом временном слое
		for i in range(1, Nx+1):
			for j in range(1, Ny+1):
				Tn[i][j] = T[i][j]

		#решаем СЛАУ в направлении оси Ох для определения поля температуры на промежуточном (n+1/2) временном слое
		for j in range(1, Ny+1):
			alfa[1] = 2.0 * tau * lamda / (2.0 * tau * (lamda + kapa * hx) + ro * c * sqr(hx));

			#вычисляем поле температуры, вследствие наличия нелинейности в левом граничном условии
			while True:
				#определяем beta начальный прогоночный коэффициент на основе левого граничного условия
				#начинаем итерационный цикл по левому граничному условию

				d = T[1][j];
				beta[1] = (ro * c * sqr(hx) * Tn[1][j] + \
								2.0 * tau * kapa * hx * Te + \
								2.0 * tau * eps1 * sigma * hx * (sqr(sqr(Te))-sqr(sqr(d)))) / \
							(2.0 * tau * (lamda + kapa * hx) + ro * c * sqr(hx));

				#цикл для определения прогоночных коэффициентов
				for i in range(2, Nx):

					#ai, bi, ci, fi – коэффициенты канонического представления СЛАУ с трехдиагональной матрицей
					ai = lamda / sqr(hx);
					bi = 2.0 * lamda / sqr(hx) + ro * c / tau;
					ci = lamda / sqr(hx);
					fi = -ro * c * Tn[i][j] / tau;

					if i == N1x and j == N1y:
						fi = fi - f(i, N1x, j, N1y)

					#alfa[i], beta[i] – прогоночные коэффициенты
					alfa[i] = ai / (bi - ci * alfa[i - 1]);
					beta[i] = (ci * beta[i-1] - fi) / (bi - ci * alfa[i-1]);

				#определяем значение температуры на правой границе
				T[Nx][j] = (ro * c * sqr(hx) * Tn[Nx][j] + 2.0 * tau * lamda * beta[Nx - 1]) / \
							(ro * c * sqr(hx) + 2.0 * tau * lamda * (1 - alfa[Nx - 1]));

				#определяем неизвестное поле температуры на промежуточном (n+1/2) временном слое
				for i in range(Nx - 1, 0, -1):
					T[i][j] = alfa[i] * T[i+1][j] + beta[i];

				if abs((d - T[1][j]) / T[1][j]) <= eps:
					break

		#поле температуры на промежуточном (n+1/2) временном слое определили
		#решаем СЛАУ в направлении оси Оу для определения поля температуры на целом (n+1) временном слое
		for i in range(1, Nx+1):

			#определяем начальные прогоночные коэффициенты на основе нижнего граничного условия
			alfa[1] = 2.0 * tau * lamda / (2.0 * tau * lamda + ro * c * sqr(hy));
			beta[1] = ro * c * sqr(hy) * T[i][1] / (2.0 * tau * lamda + ro * c * sqr(hy));

			#цикл для определения прогоночных коэффициентов
			for j in range(2, Ny):

				#ai, bi, ci, fi – коэффициенты канонического представления СЛАУ с трехдиагональной матрицей
				ai = lamda / sqr(hy);
				bi = 2.0 * lamda / sqr(hy) + ro * c / tau;
				ci = lamda / sqr(hy);
				fi = -ro * c * T[i][j] / tau;

				if i == N1x and j == N1y:
					fi = fi - f(i, N1x, j, N1y)

				#alfa[j], beta[j] – прогоночные коэффициенты
				alfa[j] = ai / (bi - ci * alfa[j-1]);
				beta[j] = (ci * beta[j-1] - fi) / (bi - ci * alfa[j-1]);

			#запоминаем значение температуры на правой границе с промежуточного (n+1/2) временного слоя
			d = T[i][Ny];

			while True:
				d1 = T[i][Ny];

				#определяем значение температуры на правой границе на основе правого граничного условия
				T[i][Ny] = (ro * c * sqr(hy) * d + 2.0 * tau * \
								(lamda * beta[Ny-1] + kapa * hy * Te + eps1 * sigma * hy * (sqr(sqr(Te))-sqr(sqr(d1))))) / \
							(ro * c * sqr(hy) + 2.0 * tau * (lamda * (1 - alfa[Ny-1]) + kapa * hy));

				if abs((d1 - T[i][Ny]) / T[i][Ny]) <= eps:
					break

			for j in range(Ny - 1, -1, -1):
				T[i][j] = alfa[j] * T[i][j+1] + beta[j];

	output_result(Nx, Ny, hx, hy, tau)
	X, Y, Z = form_result(hx, hy, Nx, Ny, T)

	return X, Y, Z


PLOTTER_CONFIG = {
    # Stepper driver/motor configuration
    "dir_pins": (22, 24),             # Direction GPIO pins for stepper driver
    "step_pins": (25, 23),             # Step GPIO pins for stepper driver
    # Resolution (microstepping) GPIO pins for stepper driver
    "res_pins": (19, 13, 6),
    # Invert direction of stepper motors
    "invert_step_dir": (0, 0),
    # Microstepping for stepper drivers. Increases smoothness of movement but might slowdown drawing
    "micro_stepping": 16,
    # Steps per milimeter for stepper motors.
    "steps_per_mm": 80,

    # Servo configuration
    # The servo is controlled by GPIO.PWM with a frequency of 50 Hz
    # Specify up and down positions by setting the pulse width in percent
    # 100% = 1/50 s = 20 ms
    # Usually values are in range 1-2ms -> 5-10%
    "servo_pin": 17,                   # Servo signal GPIO pin
    # Up position for servo (duty cycle in percent)
    "servo_pos_up": 8.7,
    # Down position for servo (duty cycle in percent)
    "servo_pos_down": 7.5,

    # Additional Hardware configuration
    # Width of your plotter. Distance between left and right timing belt mount
    "base_width": 620,

    # Drawing configuration
    # Sampling resolution for step computation in mm, higher values decrease computational load but might also decrease quality
    "movement_resolution": 1.0

}


def vMaior(n,maior):
    if n > maior:
        return n
    else:
        return maior

def maior():
    n = list()
    c = 'S'
    aux = 0
    maior = 0
    while c not in 'Nn':
        aux = int(input('Número: '))
        maior = vMaior(aux,maior)
        n.append(aux)
        c = str(input('Você deseja continuar?[S/N] '))
    print(f'Os valores digitados foram ',end='')
    for c in n:
        print(f'{c} ',end='')
    print(f'e o maior valor foi {maior}!!')

maior()


# tux`ы
n=int(input())
for i in range(n):
    print("   _~_   ", end="")
print()
for i in range(n):
    print("  (o o)  ", end="")
print()
for i in range(n):
    print(" /  V  \ ", end="")
print()
for i in range(n):
    print("/(  _  )\ ", end="")
print()
for i in range(n):
    print("  ^^ ^^  ", end="")
print()

# C.E.S.A.R
alfavit = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'
move = int(input('Шаг шифровки: '))    #Создаем переменную с шагом шифровки
msg = input("Сообщение для шифровки: ").upper()    #создаем переменнную, куда запишем наше сообщение
result = ''
for i in msg:
    m = alfavit.find(i)
    new = m + move
    if i in alfavit:
        result += alfavit[new]  # Задаем значения в итог
    else:
        result += i
print (result)

#табличка
s1=10
for i in range(1, s1+1):
     for j in range(i, i*s1+1, i):
         print(j, end='\t')
     print()


from flask import Flask
import flask.ext.restless
import models
from settings import config

app = Flask(__name__)
app.debug = config['DEBUG']
app.config.from_object(config)

manager = flask.ext.restless.APIManager(app, session=models.mysession)

def get_many_postprocessor(result=None, search_params=None, **kw):
    """Accepts two arguments, `result`, which is the dictionary
    representation of the JSON response which will be returned to the
    client, and `search_params`, which is a dictionary containing the
    search parameters for the request (that produced the specified
    `result`).

    """
    #print("GET MANY result: {0:s}, search_params: {1:s}, kw: {2:s}".format(str(result), str(search_params), str(kw)))
    #convert long numbers to string because of lack of support in JS
    for object in result['objects']:
        if object.has_key('external_id'):
            object['external_id']=str(object['external_id'])

def patch_single_preprocessor(instance_id, result=None, data=None, **kw):
    """Accepts two arguments, `instance_id`, the primary key of the
    instance of the model to patch, and `data`, the dictionary of fields
    to change on the instance.
    """
    print("PATCH patch_single_preprocessor instance_id: {0:s}, data: {1:s}, kw: {2:s}".format(str(instance_id), str(data), str(kw)))
    # if the update is for the class value then it's become gold
    if data.has_key('class_value'):
        data['gold']=1

def patch_single_postprocessor(result=None, **kw):
    """Accepts a single argument, `result`, which is the dictionary
    representation of the requested instance of the model.

    """
    if result.has_key('external_id'):
        result['external_id']=str(result['external_id'])
    print("PATCH patch_single_postprocessor result: {0:s}, kw: {1:s}".format(str(result), str(kw)))

data_blueprint = manager.create_api(
    models.Datum,
    methods=['GET', 'PUT'],
    collection_name='data',
    include_columns=['id', 'external_id', 'text', 'hashtags', 'urls', 'media', 'language', 'domain', 'source', 'class_value', 'gold','created_at', 'user', 'user.name', 'user.name', 'user.name','user.screen_name','user.profile_image_url','user.verified'],
    preprocessors={
        'PUT_SINGLE': [patch_single_preprocessor],
        'PATCH_SINGLE': [patch_single_preprocessor]
    },
    postprocessors={
        'GET_MANY': [get_many_postprocessor, models.dispose],
        'PUT_SINGLE': [patch_single_postprocessor, models.dispose],
        #'PATCH_SINGLE': [patch_single_postprocessor, models.dispose] #lack of browsers support
    },
    results_per_page=20
)

graph_blueprint = manager.create_api(
    models.GrpahDatum,
    methods=['GET'],
    collection_name='graph_data',
    postprocessors={
        'GET_MANY': [get_many_postprocessor, models.dispose]
    },
    results_per_page=200
)

if __name__ == '__main__':
    app.run()


# -*- coding: utf-8 -*-
"""
Created on Mon Jun 25 19:17:33 2018

@author: Philipe Leal
"""

import numpy as np
def floodFill(c, r, mask):
	"""
	Crawls a mask array containing
	only 1 and 0 values from the
	starting point (c=column,
	r=row - a.k.a. x, y) and returns
	an array with all 1 values
	connected to the starting cell.
	This algorithm performs a 4-way
	check non-recursively.
	"""
	# cells already filled
	filled = set()
	# cells to fill
	fill = set()
	fill.add((c, r))
	width = mask.shape[1]-1
	height = mask.shape[0]-1
	# Our output inundation array
	flood = np.zeros_like(mask, dtype=np.int8)
	# Loop through and modify the cells which
	# need to be checked.
	while fill:
	# Grab a cell
		x, y = fill.pop()

	if y == height or x == width or x < 0 or y < 0:
		# Don't fill
		pass

	if mask[y][x] == 1:
		# Do fill
		flood[y][x] = 1

	filled.add((x, y))
	# Check neighbors for 1 values
	west = (x-1, y)
	east = (x+1, y)
	north = (x, y-1)
	south = (x, y+1)

	if west not in filled:
		fill.add(west)
	if east not in filled:
		fill.add(east)
	if north not in filled:
		fill.add(north)
	if south not in filled:
		fill.add(south)

	return flood


import re
import os
import json
import random
import math

########
# Globals
canPrint = True

SETTING = 'simulation.in'
DIRECTORY = 'simulation'
OUTFILE = 'simuResult'

DEF_SETTING = {
    'glob_tries': 100,
    'enemy_lives': 100,
    'enemy_wpn_missile': 6,
    'enemy_wpn_beam': 14,
    'enemy_success_missile': 6,
    'enemy_success_beam': 3,
    'enemy_speed': 20,
    'ship_speed': [24, 0.25],
    'ship_other_head': 0,
    'ship_other_body': 0,
    'ship_munition_storage': 4,
    'ship_service_room': 6,
    'ship_ring': 4,
    'ship_wpn_head_missile': 2,
    'ship_wpn_head_beam': 1,
    'ship_wpn_side_missile': 7,
    'ship_wpn_side_beam': 4,
    'ship_special_rooms': 4,
    'ship_success_missile': 6,
    'ship_success_beam': 3,
    "ship_success_discount": [90, 50], # How often is our targetting better 6+[], 5+[*], 4+[*,*]
    'ship_wpn_effectiveness': [2.2, 0.2],
	'ship_max_damage': 20,
    'init_distance': 100,
    'glob_distance_close': 40,
    'glob_distance_death': 0,
    'glob_log_out': 6,
    'sequence': [2, 1, 1],
    'enemy_lost_beam': 20,
    'enemy_lost_missile': 20,
    'enemy_lost_speed': 30,
    'repair_crews': 10,
    'repair_delay': 5,
    'battle_max_time': 50,
    'scenarios': [
        # {
        #     'name': 'All in attack, no repair',
        #     'ship_success_discount': [100, 100],
        #     'repair_crews': 0,
        #     'sequence': [2],
        # },
        # {
        #     'name': 'All in attack, with repairs',
        #     'ship_success_discount': [100, 100],
        #     'sequence': [2],
        # },
        # {
        #     'name': '50:50, 1/2 dodge',
        #     'ship_success_discount': [100, 70],
        #     'repair_crews': 5,
        #     'sequence': [2, 1],
        # },
        # {
        #     'name': 'Full Repair, 2/3 dodge',
        #     'ship_success_discount': [100, 70],
        #     'sequence': [2, 1, 1, 1, 1],
        #     'repair_crews': 30,
        # },
        {
            'name': 'Super Crew, 3/4 dodge',
            'ship_success_discount': [100, 90],
            'sequence': [1],
            'repair_crews': 30,
        }
    ],
}

STANDS = [
    {
        'name': 'Run',
        'enemy_success': -2,
        'poll': 'poll_head',
        'weapons': 'head',
        'speed': 0,
    },
    {
        'name': 'Dodge',
        'enemy_success': +1,
        'poll': 'poll_head',
        'weapons': 'head',
        'speed': -2,
    },
    {
        'name': 'Side',
        'enemy_success': 0,
        'poll': 'poll_side',
        'weapons': 'side',
        'speed': -15,
    },
    {
        'name': 'Wedge',
        'enemy_success': +2,
        'poll': 'poll_side',
        'weapons': 'none',
        'speed': -15,
    },
]

log = []
setting = {}

########
# Superglobals

def printError(text):
    global canPrint
    print('!ERR '+text)
    canPrint = False

def printWarning(text):
	print('WARN '+text)

def addLog(lev, line):
    global log
    global setting
    log.append([lev,line])
    if setting['glob_log_out']>lev: print(line)
def writeLine(f, intention, text):
	f.write('\t'*intention+text+'\n')

def writeLineCondition(f, intention, textNo, textYes, condition):
	if(condition):
		f.write('\t'*intention+textYes+'\n')
	else:
		f.write('\t'*intention+textNo+'\n')

def	writeLineBlocks(f,intention, texts):
	allBut = texts[0:-1]
	last = texts[-1]
	for l in allBut:
		writeLine(f, intention, l+',')
	writeLine(f, intention, last)

def parseLine(line):
	line = re.sub(r"\s+#\s+","#", line)
	line = re.sub(r"\n","", line)
	line = re.sub(r"\r","", line)
	return line.split("#")

def expandCzechLetters(line):
    line = re.sub(r"\n","", line)
    return line

def parseName(name):
    name = name.lower()
    name = re.sub(r"\_","", name)
    return name

def getFiles( wildch ):
	mypath = os.getcwd()
	onlyfiles = [f for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f))]
	filtered = []
	for f in onlyfiles:
		if(re.match(wildch, f) != None):
			filtered.append(f)
	return filtered

def getLinesInFiles( wildch ):
	print('File search pattern: '+wildch)
	files = getFiles(wildch)
	print(files)

	lines = []
	for nm in files:
		with open(nm) as f:
			lns = f.readlines()
		f.closed
		print('File '+nm+', '+str(len(lns))+' lines')
		lines = lines + lns

	return lines

def expandWildchars( lst0, lstFull ):
	lst1 = []
	for it in lst0:
		if((it[0]=="<")and(it[-1]==">")):
			r = re.compile(it[1:-1])
			lst1.extend(list(filter(r.match, lstFull)))
		else:
			lst1.append(it)
	return lst1

def addShipParts( code, count ):
    theList = []
    for i in range(0,count):
        theList.append(code+str(i))
    return theList

def reRollDice( extraNum ):
    n = random.randint(1, 6)
    if n<4: return False
    if extraNum==1: return True
    return reRollDice( extraNum-1 )

def rollDice( num, chance ):
    success = 0;
    for i in range(0, num):
        n = random.randint(1, 6)
        if n>=chance:
            success += 1
            continue
        if chance>6 and n==6:
            if reRollDice(chance-6):
                success += 1
                continue
    addLog(5, ' - - diceRoll: dices:'+str(num)+'('+str(chance)+'): '+str(success))
    return success

def doPoll( poll, hits ):
	if hits>=len(poll):
		return poll[:]
	idx = range(0, len(poll))
	pick = random.sample(idx, hits)
	selection = []
	for i in pick: selection.append(poll[i])
	return selection

def outThePoll( poll, hits ):
    if hits<=len(poll):
        return 0
    return hits - len(poll)

def doDamage( hit, isDestroyed ):
	return 1

def getCICReport( ship ):
    report = []
    keys = ship.keys()
    for sys in keys:
        dmg = ship[sys]
        if (dmg>0)and(dmg<3):
            report.append([sys, dmg])
    return report

def getShipWeapons( stand, setting, state, turnCount ):
    out = [0, 6]
    wpnType = 'missile' if state['onDistance'] else 'beam'
    wpnStand = stand['weapons']
    if wpnStand == 'none': return out
    out[1] = setting['ship_success_'+wpnType]

    count = setting['ship_wpn_'+wpnStand+'_'+wpnType] - state[wpnStand+'_'+wpnType+'_down']
    effect = setting['ship_wpn_effectiveness'][0] - (setting['ship_wpn_effectiveness'][1] * state['wpn_effectiveness_down'])

    count1 = count * effect
    count2 = math.floor(count1)
    if (count1 != count2) and (turnCount % 2 == 0): count2 += 1
    out[0] = int(count2)

    roll = random.randint(0, 100)
    for val in setting['ship_success_discount']:
        if roll<val: out[1] -= 1

    return out

def getEnemyWeapons( state, setting ):
    wpn = 'missile' if state['onDistance'] else 'beam'
    return [
        int(max(setting['enemy_wpn_'+wpn]-(math.floor(state['enemy_damage'] / setting['enemy_lost_beam'])), 1)),
        setting['enemy_success_'+wpn]+stand['enemy_success']
    ]

def addDamagedSystem( state, hit ):
    if hit[0] == 'V': # Missile
        if hit[1] == '0':
            state['head_missile_down'] += 1
        else:
            state['side_missile_down'] += 1
    elif hit[0] == 'D': # Beam
        if hit[1] == '0':
            state['head_beam_down'] += 1
        else:
            state['side_beam_down'] += 1
    elif hit[0] == 'M' or hit[0] == 'S': # Service
        state['wpn_effectiveness_down'] += 1
    elif hit[0] == 'P': # Rings
        state['speed_down'] += 1

def calcNewSpeed( setting, state ):
    out = setting['enemy_speed']-setting['ship_speed'][0]-STANDS[state['stand']]['speed']
    out -= math.floor(state['enemy_damage']/setting['enemy_lost_speed'])
    out += math.floor(state['speed_down']*setting['ship_speed'][1])

    return int(out)

########
# Process

# Settings file
print('\nReading parameters --> '+SETTING)
files = getFiles(SETTING)
if(len(files)!=1):
    print('\nPrinting default setting into: '+SETTING)
    with open(SETTING, 'w') as outfile:
        json.dump(DEF_SETTING, outfile, sort_keys=True, indent=4)

setting = json.load(open(SETTING))
currKeys = setting.keys()
allKeys = DEF_SETTING.keys()
copyKeys = allKeys[:]
copyKeys.remove('scenarios')

test = False

for key in currKeys:
    if key not in allKeys:
        del setting[key]
        test = True
        printError('Removing unwanted key "'+key+'" from the setting.')

for key in allKeys:
    if key not in currKeys:
        setting[key] = DEF_SETTING[key]
        test = True
        printError('Adding missing key "'+key+'" to the setting, value:'+str(DEF_SETTING[key]))

if test:
    print('\nSetting changed, reprinting into: '+SETTING)
    with open(SETTING, 'w') as outfile:
        json.dump(setting, outfile, sort_keys=True, indent=4)

setting_backup = {}
for key in copyKeys:
    setting_backup[key] = setting[key]

# Can simulate?
print('\nCan Run: '+str(canPrint))
if canPrint == False:
	exit()

# Geting Simulation output file
if(not os.path.isdir(DIRECTORY)):
    os.mkdir(DIRECTORY)
files = getFiles(DIRECTORY+'/'+OUTFILE+'\.*.txt')
fileName = DIRECTORY+'/'+OUTFILE+'_'+str(len(files)+1)+'.txt'
print('\nResults will be printed in --> '+fileName)

# Setting the ship
poll_side = ['R0','R1','K0','K1'];
poll_side.extend(addShipParts('X', setting['ship_special_rooms']))
poll_side.extend(addShipParts('M', setting['ship_munition_storage']))
poll_side.extend(addShipParts('S', setting['ship_service_room']))
poll_side.extend(addShipParts('P0', setting['ship_ring']))
poll_side.extend(addShipParts('P5', setting['ship_ring']))
poll_side.extend(addShipParts('V0', setting['ship_wpn_head_missile']))
poll_side.extend(addShipParts('D0', setting['ship_wpn_head_beam']))
poll_side.extend(addShipParts('V5', setting['ship_wpn_side_missile']))
poll_side.extend(addShipParts('D5', setting['ship_wpn_side_beam']))

other_head = setting['ship_wpn_head_missile'] + setting['ship_wpn_head_beam'] + 1 + setting['ship_ring']
other_head = int(other_head * setting['ship_other_head'] / 100)
other_body = int(len(poll_side) * setting['ship_other_body'] / 100);

poll_side.extend(addShipParts('O', other_head))
poll_side.extend(addShipParts('O5', other_body))

poll_head = ['R0', 'S0', 'M0', 'X0']
poll_head.extend(addShipParts('P0', setting['ship_ring']))
poll_head.extend(addShipParts('V0', setting['ship_wpn_head_missile']))
poll_head.extend(addShipParts('D0', setting['ship_wpn_head_beam']))
poll_head.extend(addShipParts('O', other_head))

# Run the Simulation

statistics = []
idxCh = 0
for ch in setting['scenarios']:
    idxCh += 1
    addLog(0, '')
    addLog(-1, '[[ Scenario #'+str(idxCh)+' ]]: '+ch['name'])

    # Processing setting differences
    for key in copyKeys:
        setting[key] = setting_backup[key]

    newKeys = ch.keys()
    newKeys.remove('name')
    for key in newKeys:
        setting[key] = ch[key]
        addLog(0, '> New '+key+': '+str(ch[key]))

    results = []
    for tryIndex in range(0, setting['glob_tries']):
    	# One Game
        state = {
            'distance': setting['init_distance'],
            'onDistance': True,
            'stand': setting['sequence'][0],
            'sequenceLength': len(setting['sequence']),
            'damage': 0,
            'victory': False,
            'lost': False,
            'head_missile_down': 0,
            'head_beam_down': 0,
            'side_missile_down': 0,
            'side_beam_down': 0,
            'wpn_effectiveness_down': 0,
            'speed_down': 0,
            'enemy_damage': 0,
            'our_damage_total': 0,
            'speed': setting['enemy_speed']-setting['ship_speed'][0]-STANDS[setting['sequence'][0]]['speed'],
            'repair': [[]]*(setting['repair_delay']+1),
            'free_crew': setting['repair_crews'],
            'hurt_crew': 0,
        }

        poll = {
            'poll_head': poll_head[:],
            'poll_side': poll_side[:],
        }
        toRepair = []
        destroyed = []

        ship = {};
        for key in poll_side:
            ship[key] = 0

        turnCount = 0;

        addLog(1, '==> Game #'+str(tryIndex+1)+' ==')
        while True:
            turnCount += 1
    		# One Turn
            distText = 'on Distance' if state['onDistance'] else '!! UP CLOSE !!'
            stand = STANDS[state['stand']]
            we = getShipWeapons(stand, setting, state, turnCount)
            enemy = getEnemyWeapons(state, setting)
            addLog(2, 'Turn #'+str(turnCount)+', Distance: '+str(state['distance'])+' '+distText+', Stand: '+stand['name']+', Closing Speed: '+str(state['speed']))
            addLog(4, ' - We: '+str(we[0])+'('+str(we[1])+'), Enemy: '+str(enemy[0])+'('+str(enemy[1])+')')
    		# Enemy Fires
            hits = rollDice(enemy[0], enemy[1])
            extraHits = outThePoll(poll[stand['poll']], hits)
            state['damage'] += extraHits
            pollHits = doPoll(poll[stand['poll']], hits)
            state['our_damage_total'] += hits
            addLog(3, ' - Enemy hits: +'+str(len(pollHits))+'(direct '+str(extraHits)+')('+str(pollHits)+ ') (total '+str(state['our_damage_total'])+')')

    		# Do Damage
            for hit in pollHits:
                if ship[hit] == 1:
                    state['damage'] += doDamage(hit, True)
                    ship[hit] = 2
                    if hit in poll['poll_head']: poll['poll_head'].remove(hit)
                    poll['poll_side'].remove(hit)
                    destroyed.append(hit)
                    if hit in toRepair: toRepair.remove(hit)

                # elif ship[hit] == 1:
                #     state['damage'] += doDamage(hit, False)
                #     ship[hit] = 2
                elif ship[hit] == 0:
                    ship[hit] = 1
                    toRepair.append(hit)
                    addDamagedSystem(state, hit)

            # We Fires
            hits = rollDice(we[0], we[1])
            state['enemy_damage'] += hits
            addLog(3, ' - We hits: +'+str(hits)+' ('+str(state['enemy_damage'])+'/'+str(setting['enemy_lives'])+')')

            # Do repair
            repResult = []
            for rep in state['repair'][0]:
                if ship[rep] == 3:
                    state['hurt_crew'] += 1
                    repResult.append([rep,'Failed'])
                else:
                    if ship[rep] == 2:
                        state['damage'] -= doDamage(hit, False)
                        toRepair.append(rep)
                    ship[rep] -= 1
                    state['free_crew'] += 1
                    repResult.append([rep,'Ok'])
            addLog(4, ' - Repairs: '+str(repResult))

            for i in range(0, setting['repair_delay']):
                state['repair'][i] = state['repair'][i+1]
            state['repair'][setting['repair_delay']] = []

            # Assign new repair
            while (state['free_crew']>0) and (len(toRepair)>0):
                state['repair'][setting['repair_delay']].append(toRepair.pop(0))
                state['free_crew'] -= 1
            addLog(5, ' - New Repairs: +'+str(state['repair'][setting['repair_delay']]))

    		# Lost Condition
            if state['damage'] >= setting['ship_max_damage']:
                state['lost'] = True
                break

    		# Too Long Battle
            if turnCount >= setting['battle_max_time']:
                state['lost'] = True
                break

    		# Win Condition
            if state['enemy_damage'] >= setting['enemy_lives']:
                state['victory'] = True
                break

    		# Ship Summary
            damageInfo = str(state['damage'])+'/'+str(setting['ship_max_damage'])
            addLog(4, ' - - Ship Status: '+str(getCICReport(ship)))
            addLog(3, ' - Destroyed: '+str(destroyed))
            addLog(2, ' - Ship Damage: '+damageInfo)

    		# Calc new Distances
            state['distance'] -= state['speed']
            state['onDistance'] = state['distance'] > setting['glob_distance_close']

    		# Lost Distance Condition
            if state['distance'] <= setting['glob_distance_death']:
                state['distance'] = setting['glob_distance_death']
                # state['lost'] = True
                # break

    		# Evasive Action
            state['stand'] = setting['sequence'][turnCount % state['sequenceLength']]
            state['speed'] = calcNewSpeed(setting, state)

        theResult = {
            'time': turnCount,
            'win': True,
            'distance': state['distance'],
        }

        addLog(2, '')
        if state['lost']:
            addLog(1, 'GAME LOST')
            theResult['win'] = False
        if state['victory']:
            addLog(1, 'VICTORY')
        results.append(theResult)
        addLog(2, '')

    wins = list(filter(lambda d: d['win'] == True, results))
    losts = list(filter(lambda d: d['win'] == False, results))
    winRatio = int((len(wins) * 100) / len(results))

    lostTime = 0
    lostDist = -1
    lostDistL = []
    if len(losts)>0:
        for g in losts:
            lostTime += g['time']
            lostDistL.append(g['distance'])
        lostTime = lostTime / len(losts)
        lostDist = sorted(lostDistL)[int(math.floor(len(lostDistL)/2))]

    winTime = 0
    winDist = -1
    winDistL = []
    if len(wins)>0:
        for g in wins:
            winTime += g['time']
            winDistL.append(g['distance'])
        winTime = winTime / len(wins)
        winDist = sorted(winDistL)[int(math.floor(len(winDistL)/2))]

    addLog(0, 'Played '+str(len(results))+' games')
    addLog(0, 'With Sequence '+str(setting['sequence']))
    addLog(0, 'Victory in '+str(winRatio)+'% cases')
    addLog(0, 'Mean Distance on victory '+str(winDist))
    addLog(0, 'Mean Distance on lost '+str(lostDist))
    addLog(0, 'Average Win Time is '+str(winTime)+' turns')
    addLog(0, 'Average Lost Time is '+str(lostTime)+' turns')


# -*- ecoding: utf-8 -*-
# @ModuleName: 波形图生成
# @Function:
# @Author: Jack Chen
import os, glob
import cv2 as cv
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd

def draw_waveform(dir):
    for file in os.listdir(dir):
        file1 = dir + '\\' + file
        (filepath, tempfilename) = os.path.split(file)
        file_name = tempfilename + '.png'
        for file2 in os.listdir(file1):
            file3 = file1 + '\\' + file2
            file4 = glob.glob(os.path.join(file3, "*.png"))
            for file_name in file4:
                (filepath1, tempfilename1) = os.path.split(file_name)
                img = cv.imread(file_name)
                GrayImage = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
                img = np.array(GrayImage)
                height, width = img.shape
                img_y = []
                for col in range(1, width + 1):
                    a = 0
                    b = 0
                    for row in range(height):
                        val = img[row][col - 1]
                        if (val) == 0:
                            a = a + 1
                        else:
                            b = b + 1
                    img_y.append(b)
                img_x = list(range(1, width + 1))

                matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文
                # "r" 表示红色，ms用来设置*的大小
                plt.figure(figsize=(12.8, 6.4))
                plt.plot(img_x, img_y, "black", marker='.', ms=8)
                plt.xticks(rotation=45)
                plt.xlabel("列数")
                plt.ylabel("白色像素个数")
                plt.title("图像每列白色像素数")

                poly_save = 'pix_img'
                isExists = os.path.exists(poly_save)
                if not isExists:
                    os.makedirs(poly_save)
                save_name = poly_save + '\\' + tempfilename + '_' + str(file2) + '_' + tempfilename1
                plt.savefig(save_name)
                plt.close()

                data_save_csv = 'pix_csv'
                isExists = os.path.exists(data_save_csv)
                if not isExists:
                    os.makedirs(data_save_csv)
                pix_name = os.path.splitext(tempfilename1)[0]
                # save csv
                chss = [img_x, img_y]
                chss = np.transpose(chss)
                save_name_1 = data_save_csv + '\\' + tempfilename + '_' + str(file2) + '_' + pix_name + '.csv'
                mess = pd.DataFrame(chss, columns=None)
                mess.to_csv(save_name_1, index=False, header=False)

if __name__ == '__main__':
    dir = 'canny_core'
    draw_waveform(dir)