# -*- coding: utf-8 -*-
import pandas as pd

from main.get_result import Result
from django.db import connection

class Rank(Result):
    def get_rank(self, situation, limit=300):
        """
        指定された状況のスコアトップlimitを返す
        :param situation:
        :param limit:
        :return:
        """
        limit = str(limit)
        cursor = connection.cursor()
        cursor.execute("""
            SELECT
              interval,
               location_id,
               stay_time_avg AS stay_time_avg_score,
               num_of_people AS num_of_people_score,
               firstvisit_rate AS firstvisit_rate_score,
               foreign_rate AS foreign_rate_score,
               situation,
               score
            FROM score
            WHERE situation = '""" + situation + """'AND interval::date >= '2016-11-07'
            ORDER BY score
        """)
        features_rank = pd.DataFrame(list(self.dictfetchall(cursor)))
        features = self.get_data(db_table='features')
        features_rank = pd.merge(features_rank, features, on=['interval', 'location_id'])
        # 日付情報を付加
        features_rank['date'] = features_rank['interval'].apply(self.add_date)
        # str型にしないとjsonにした時におかしな値になる
        features_rank['interval'] = features_rank['interval'].astype(str)
        features_rank['date'] = features_rank['date'].astype(str)

        location = self.get_data(db_table='location')
        # 地点情報をmerge
        features_rank = pd.merge(features_rank, location, on=['location_id'])

        return features_rank.to_json(orient='records')


#coding = utf-8
import pandas as pd
import  numpy as np
from dateutil.relativedelta import relativedelta
import os
import matplotlib.pyplot as plt
import calendar
from math import *

#参数设置
init_asset = 1#初识资产的净值
start_time = '2013-01-01'  # 起始时间，输入格式为YYYY-MM-DD
end_time = '2020-05-31'  # 结束时间，输入格式为YYYY-MM-DD
path = '/Users/maxiaohang/指数数据/'  # 资产数据存放的文件夹路径
path_macro = '/Users/maxiaohang/宏观数据.xlsx'  # 宏观数据的存放路径
path_rate = '/Users/maxiaohang/利率数据.xlsx'  # 利率数据的存放路径
asset_num = 12

class asset_performance():
    def __init__(self, asset_payoff, asset_name, signal_payoff, start_time=start_time, end_time=end_time):
        #asset_payoff 资产的月频收益率数据
        #asset_name 基金的名称
        #signal_payoff 用于构造信号的资产的,需要提前计算好
        self.start_time = start_time
        self.end_time = end_time
        self.asset_payoff = asset_payoff
        self.signal_payoff = signal_payoff
        self.asset_name = asset_name

    def rate_liquidity_signal(self, month, monthly_payoff):
        #按照1年期国债、10年期国债、1年期信用债均线方法给出流动性信号(利率表征)
        #当月收益率小于均线视为宽松
        # monthly_payoff是月收益率，其索引为每月的最后一天
        # month为当月最后一天
        month = pd.to_datetime(month)
        mean_payoff = monthly_payoff[(monthly_payoff.index > month - relativedelta(months=12)) &
                                      (monthly_payoff.index < month + relativedelta(months=1))].mean()
        month_payoff = monthly_payoff.loc[month]
        flag = 0
        for i in mean_payoff.index:
            if mean_payoff.loc[i] > month_payoff.loc[i]:
                flag = flag + 1
        return flag/len(mean_payoff.index) > 2.0/3.0

    def asset_performance(self):
        asset_payoff = self.asset_payoff[(self.asset_payoff.index >= self.start_time) &
                        (self.asset_payoff.index <= self.end_time)]
        index = asset_payoff.resample('M').apply(lambda x: x[-1]).index#取每月最后一天
        currency_strong = 0
        currency_strong_up = 0
        currency_strong_paypff = 0
        currency_weak = 0
        currency_weak_up = 0
        currency_weak_payoff = 0
        for month in index:
            if self.rate_liquidity_signal(month, self.signal_payoff):#判断当月为货币宽松
                currency_strong = currency_strong + 1
                currency_strong_paypff = currency_strong_paypff + asset_payoff.loc[month]
                if asset_payoff.loc[month] > 0:
                    currency_strong_up = currency_strong_up +1
            else:#判断为货币紧缩
                currency_weak = currency_weak + 1
                currency_weak_payoff = currency_weak_payoff + asset_payoff.loc[month]
                if asset_payoff.loc[month] > 0:
                    currency_weak_up = currency_weak_up + 1
        return currency_strong_paypff/currency_strong, currency_strong_up/currency_strong, \
               currency_weak_payoff/currency_weak, currency_weak_up/currency_weak

def get_file_name(path, filetype):#读取文件的名字
    file_name = []
    os.chdir(path)
    for root, dir, files in os.walk(os.getcwd()):
        for file in files:
            if os.path.splitext(file)[1] in filetype:
                # print(os.path.splitext(file)[1])
                file_name.append(file)
    return file_name

def get_month_mean(rate_data):#获取利率数据的月均值
    rate_data = rate_data.rename(index=rate_data[rate_data.columns[0]])#把日期设置为索引
    rate_data = rate_data.dropna()
    rate_data[rate_data.columns[0]] = rate_data.index
    rate_data[rate_data.columns[0]] = rate_data[rate_data.columns[0]].apply(lambda x: str(x.year)+'-'+str(x.month))
    for i in range(1, rate_data.shape[1]):
        rate_data[rate_data.columns[i]] = pd.to_numeric(rate_data[rate_data.columns[i]], errors='coerce')
    rate_data = rate_data.groupby(rate_data.columns[0])[[rate_data.columns[i] for i in range(1, rate_data.shape[1])]].mean()
    rate_data['month'] = pd.to_datetime(rate_data.index)
    rate_data['month'] = rate_data['month'].apply(lambda x: str(x.year) + '-' + str(x.month)+'-'+str(x.days_in_month))
    rate_data.index = pd.to_datetime(rate_data[rate_data.columns[-1]])
    rate_data = rate_data[[rate_data.columns[i] for i in range(0, rate_data.shape[1]-1)]]#去除第一列的时间
    return rate_data.sort_index()

if __name__ == '__main__':
    start_time_1 = str(pd.to_datetime(start_time) - relativedelta(months=1))[0:7]
    end_time_1 = str(pd.to_datetime(end_time) + relativedelta(months=3))[0:7]
    filetype = ['.xls', 'xlsx']
    file_name = get_file_name(path, filetype)
    file_name = np.array(file_name)
    file_name.sort()  # 存放资产的名称
    macro_data = pd.read_excel(path_macro)  # 导入宏观数据
    asset_data = [pd.read_excel(path + fname) for fname in file_name]  # 导入资产数据，放到一个列表里，每一个元素是一个dataframe
    asset_month_payoff = pd.DataFrame()#asset_month_payoff用以存放了12个资产的月收益率
    for j in range(0, asset_num):  # 计算每一个资产，每一季度的收益率
        data = asset_data[j] # 当前处理的资产赋值给data
        data = data[
            (data[data.columns[0]] < end_time_1) &
            (data[data.columns[0]] >= start_time_1) &
            (data[data.columns[1]] != '--')]
        data = data.rename(index=data[data.columns[0]])
        net_value = data[data.columns[1]].apply(lambda x: str(x).replace(',', '')).copy(deep = True)
        data[data.columns[1]] = pd.to_numeric(net_value, errors = 'coerce')
        data_month = data.rename(index=data[data.columns[0]])
        data_month.index = pd.to_datetime(data_month.index)
        payoff = data_month.resample('M').apply(lambda x: x[-1])[data.columns[1]].pct_change()
        asset_month_payoff[file_name[j].split('.')[0]] = payoff
    asset_month_payoff = asset_month_payoff.iloc[1:]#asset_month_payoff存放了12个资产的月收益率
    rate_data = pd.read_excel(path_rate)  # 导入一揽子利率数据
    rate_data = rate_data.iloc[2:]
    rate_data = get_month_mean(rate_data)
    ap = pd.DataFrame()
    for i in range(asset_num):#针对12个资产分别计算不同货币状态下的月均收益率和上涨频率
        performer = asset_performance(asset_month_payoff[asset_month_payoff.columns[i]], asset_month_payoff.columns[i],rate_data)
        ap[performer.asset_name] = performer.asset_performance()
    ap.to_csv('资产表现_1_8.csv')#将结果导入到csv中

# a = Timing(asset_month_payoff[asset_month_payoff.columns[0]], asset_month_payoff.columns[0],rate_data)
# a.asset_performancew(a.asset_data, a.signal_payoff)


import sys
sys.stdin = open('input.txt')

def check(puzzle):
    cnt = 0
    for i in puzzle:
        for x in range(len(i)-K-1):
            if i[x:x+K+2] == ['0']+['1']*K+['0']:
                cnt += 1
    return cnt

T = int(input())
for tc in range(1, T+1):
    N, K = map(int, input().split())
    puzzle = [['0']*(N+2)] + [['0'] + input().split() + ['0'] for _ in range(N)] + [['0']*(N+2)]

    # 가로
    a = check(puzzle)

    # 세로
    for i in range(N+2):
        for j in range(N+2):
            if i < j:
                puzzle[i][j], puzzle[j][i] = puzzle[j][i], puzzle[i][j]

    b = check(puzzle)

    print('#{} {}'.format(tc, a+b))


import datetime

DEBUG = False

SUPER_USER = ['']

data = {
    datetime.datetime.now().month : {
            "hours": 0,
            "under_salary": {
                "cofe": 0,
                "food": 0
            },
            "penalties": 0,
            "bet": 0
        }
    }

dir_database = 'DataBase'

dir_api_token = 'settings/token.json'

logfile = 'Logging/logfile.txt'


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# Dependency imports

from tensor2tensor.layers import common_layers
from tensor2tensor.models import basic
from tensor2tensor.utils import registry

import tensorflow as tf

@registry.register_model
class BasicDiscreteAutoencoder(basic.BasicAutoencoder):

  def bottleneck(self, x, res_size):
    hparams = self._hparams
    x = tf.tanh(tf.layers.dense(x, hparams.bottleneck_size, name="bottleneck"))
    d = x + tf.stop_gradient(2 * tf.to_float(tf.less(0.0, x)) - 1.0 - x)
    y = tf.nn.dropout(x, keep_prob=1.0 - hparams.dropout)
    x = common_layers.mix(d, y, hparams.discretize_warmup_steps,
                          hparams.mode == tf.estimator.ModeKeys.TRAIN)
    x = tf.layers.dense(x, res_size, name="unbottleneck")
    return x

@registry.register_hparams
def basic_discrete_autoencoder():
  """Basic autoencoder model."""
  hparams = basic.basic_autoencoder()
  hparams.hidden_size = 128
  hparams.bottleneck_size = 512
  hparams.bottleneck_warmup_steps = 3000
  hparams.add_hparam("discretize_warmup_steps", 5000)
  return hparams


from .resources import Continuum, Food, Gorgonium, Iron, Lithium

# ============== GLOBAL SETTINGS ==============

SOLAR_SYSTEM_SIZE = 13

# ============== UNIT SETTINGS ==============

WORKER_ATTACK = 1
WORKER_DEFENSE = 1
WORKER_PRODUCTION = 5

COMBAT_ATTACK = 5
COMBAT_DEFENSE = 5
COMBAT_PRODUCTION = 3

HERO_ATTACK = 10
HERO_SPECIAL_ATTACK = 15
HERO_DEFENSE = 10
HERO_PRODUCTION = 1

# ============== FACTION SETTINGS ==============

# Board layout
"""
           / \     / \
         /     \ /     \
        |   0   |   1   |
        |       |       |
       / \     / \     / \
     /     \ /     \ /     \
    |   2   |   3   |   4   |
    |       |       |       |
   / \     / \     / \     / \
 /     \ /     \ /     \ /     \
|   5   |   6   |   7   |   8   |
|       |       |       |       |
 \     / \     / \     / \     /
   \ /     \ /     \ /     \ /
    |   9   |   10   |  11   |
    |       |       |       |
     \     / \     / \     /
       \ /     \ /     \ /
        |  12   |  13   |
        |       |       |
         \     / \     /
           \ /     \ /
"""
# --------------    Zidaan    --------------
ZIDAAN_INITIAL_POSITION = -1 #  set to -1 for random initial position
ZIDAAN_ORBITAL_RATE = 1
ZIDAAN_RESOURCE_ALLOCATION = list({
    0: Continuum,
    1: Food,
    2: Gorgonium,
    3: Iron,
    4: Lithium,
    5: Iron,
    6: Continuum,
    7: Gorgonium,
    8: Lithium,
    9: Food,
    10: Iron,
    11: Food,
    12: Gorgonium,
    13: Lithium,
}.values())
ZIDAAN_SPACE_PORT_LOCATION=10

# --------------    Hianth    --------------
HIANTH_INITIAL_POSITION = -1 #  set to -1 for random initial position
HIANTH_ORBITAL_RATE = 2
HIANTH_RESOURCE_ALLOCATION = list({
    1: Continuum,
    2: Food,
    3: Gorgonium,
    4: Iron,
    5: Lithium,
    6: Iron,
    7: Continuum,
    8: Gorgonium,
    9: Lithium,
    10: Food,
    11: Iron,
    12: Food,
    13: Gorgonium,
    14: Lithium,
}.values())

# --------------    Gorgon    --------------
GORGON_INITIAL_POSITION = -1 #  set to -1 for random initial position
GORGON_ORBITAL_RATE = 3
GORGON_RESOURCE_ALLOCATION = list({
    1: Continuum,
    2: Food,
    3: Gorgonium,
    4: Iron,
    5: Lithium,
    6: Iron,
    7: Continuum,
    8: Gorgonium,
    9: Lithium,
    10: Food,
    11: Iron,
    12: Food,
    13: Gorgonium,
    14: Lithium,
}.values())

# --------------    Frent    --------------
FRENT_INITIAL_POSITION = -1 #  set to -1 for random initial position
FRENT_ORBITAL_RATE = 4
FRENT_RESOURCE_ALLOCATION = list({
    1: Continuum,
    2: Food,
    3: Gorgonium,
    4: Iron,
    5: Lithium,
    6: Iron,
    7: Continuum,
    8: Gorgonium,
    9: Lithium,
    10: Food,
    11: Iron,
    12: Food,
    13: Gorgonium,
    14: Lithium,
}.values())

# --------------    Lithix    --------------
LITHIX_INITIAL_POSITION = -1 #  set to -1 for random initial position
LITHIX_ORBITAL_RATE = 5
LITHIX_RESOURCE_ALLOCATION = list({
    1: Continuum,
    2: Food,
    3: Gorgonium,
    4: Iron,
    5: Lithium,
    6: Iron,
    7: Continuum,
    8: Gorgonium,
    9: Lithium,
    10: Food,
    11: Iron,
    12: Food,
    13: Gorgonium,
    14: Lithium,
}.values())


import click
import foolbox

from counter_attack import tests, utils
from counter_attack.cli import definitions, options, parsing

@click.group(name='check')
def check():
    pass

@check.command(name='parallelization')
@options.global_options
@options.standard_model_options
@options.pretrained_model_options
@options.dataset_options('test')
@options.test_options('check/parallelization')
@options.attack_options(definitions.parallelizable_attacks, mandatory_parallelization=True)
def check_parallelization(options):
    """
    Compares parallelized attacks with standard ones.

    This is a sanity check to verify that attack parallelization does not seriously
    affect the results.
    """

    attack_p = options['attack_p']
    attack_name = options['attack_name']
    attack_workers = options['attack_workers']
    command = options['command']
    cuda = options['cuda']
    foolbox_model = options['foolbox_model']
    results_path = options['results_path']
    loader = options['loader']

    criterion = foolbox.criteria.Misclassification()

    attack = parsing.parse_attack(
        attack_name, attack_p, criterion)

    samples_count, correct_count, standard_attack_count, parallel_attack_count, standard_distances, parallel_distances = tests.parallelization_test(
        foolbox_model, loader, attack, attack_p, cuda, attack_workers)

    standard_failure_count = correct_count - standard_attack_count
    parallel_failure_count = correct_count - parallel_attack_count

    standard_average_distance, standard_median_distance, standard_adjusted_median_distance = utils.distance_statistics(
        standard_distances, standard_failure_count)
    parallel_average_distance, parallel_median_distance, parallel_adjusted_median_distance = utils.distance_statistics(
        parallel_distances, parallel_failure_count)

    standard_success_rate = standard_attack_count / correct_count
    parallel_success_rate = parallel_attack_count / correct_count

    average_distance_difference = (
        parallel_average_distance - standard_average_distance) / standard_average_distance
    median_distance_difference = (
        parallel_median_distance - standard_median_distance) / standard_median_distance
    success_rate_difference = (
        parallel_success_rate - standard_success_rate) / standard_success_rate
    adjusted_median_distance_difference = (
        parallel_adjusted_median_distance - standard_adjusted_median_distance) / standard_adjusted_median_distance

    info = [['Average Distance Relative Difference', average_distance_difference],
            ['Median Distance Relative Difference', median_distance_difference],
            ['Success Rate Difference', success_rate_difference],
            ['Adjusted Median Distance Difference',
                adjusted_median_distance_difference],
            ['Samples Count', str(samples_count)],
            ['Correct Count', str(correct_count)],
            ['Standard Attack Count', str(standard_attack_count)],
            ['Parallel Attack Count', str(parallel_attack_count)]]

    header = ['Standard Distances', 'Parallel Distances']

    utils.save_results(results_path, table=[standard_distances, parallel_distances], command=command,
                       info=info, header=header)


"""
Title: Age Bot Endpoint
Date Started: Jan 27, 2022
Version: 1.01
Version Start: Feb 7, 2022
Author: David Hyongsik Choi
Legal:  All rights reserved.  This code may not be used, distributed, or copied without the express written consent of David Hyongsik Choi.
Purpose:  To provide API endpoint for Age bot.
Version 1.01: Convert to Dash.
"""
# IMPORT TOOLS
#   STANDARD LIBRARY IMPORTS
#   THIRD PARTY IMPORTS
from dash import dcc, html, callback_context
from dash.dependencies import Input, Output, State
from dashappobject import app
import pandas as pd
#   LOCAL APPLICATION IMPORTS
from ..dashinputs import prompt_builder, gen_tablecontents, dash_inputbuilder
from ..common_resources import allavailabledates, staticmindate, staticmaxdate
from ..botrun_parambuilder import brpb_base
from file_functions import delete_folder, getbotsinglerunfolder
from ..botclasses import BotParams
from Modules.bots.agebot.AGEBOT_BASE import agebotmaster
from ..os_functions import get_currentscript_filename
from ..datatables import DataTableOperations
from Modules.dates import DateOperations
from Modules.timeperiodbot import random_dates
from formatting import formatting_schema

format_tabs = formatting_schema['format_tabs']

bp = BotParams(
    get_currentscript_filename(__file__),
    'Age Bot',
    "The Age Bot returns age stats on stocks based on the user's chosen growth requirements.  The ticker symbols considered are all United States NASDAQ and NYSE common stock.",
    agebotmaster
)
tbodydata = [
    {
        'id': f'num_trials_{bp.botid}',
        'placeholdertext': '# of trials',
        'prompt': 'How many trials do you want to run?',
        'inputtype': 'number',
        'min': 1,
        'max': 100,
        'step': 1
        },
    {
        'placeholdertext': 'trial length',
        'id': f'testlen_{bp.botid}',
        'prompt': 'The size of the date range over which each trial is considered (in days).',
        'inputtype': 'number',
        'min': 1,
        'max': 10*365,
        'step': 1
        },
    {
        'placeholdertext': 'Optional',
        'id': f'rank_beg_{bp.botid}',
        'prompt': 'Rank batch start bound:',
        'inputtype': 'number',
        'min': 0,
        'max': 1000,
        'step': 1
        },
    {
        'placeholdertext': 'Optional',
        'id': f'rank_end_{bp.botid}',
        'prompt': 'Rank batch end bound:',
        'inputtype': 'number',
        'min': 0,
        'max': 1000,
        'step': 1
        },
    {
        'id': f'daterange_{bp.botid}',
        'prompt': 'Specify a date range from which the bot will choose random trials.',
        'inputtype': 'datepicker_range',
        'clearable': True,
        'min_date_allowed': staticmindate,
        'max_date_allowed': staticmaxdate
        },
    {
        'id': f'randomize_{bp.botid}',
        'prompt': 'Randomize dates instead?',
        'buttontext': 'Randomize dates',
        'inputtype': 'button_submit'
        },
    {
        'id': f'datepicker_{bp.botid}',
        'prompt': 'Alternatively, you can hand pick the trial dates yourself  The number of dates you select must equal the number of trials you set above.',
        'inputtype': 'dropdown',
        'options': allavailabledates,
        'value': [],
        'clearable': True,
        'searchable': True,
        'multi': True
        },
    {
        'id': f'gr_upper_{bp.botid}',
        'prompt': 'Set upper bound growth rate (1.00 = 100%).',
        'placeholdertext': 'Optional',
        'inputtype': 'number'
        },
    {
        'id': f'gr_upper_filter_{bp.botid}',
        'prompt': 'Keep stocks that are __ the upper bound growth rate.',
        'options': ['<', '<='],
        'inputtype': 'dropdown',
        'clearable': True
        },
    {
        'id': f'gr_lower_{bp.botid}',
        'prompt': 'Set lower bound growth rate (1.00 = 100%).',
        'placeholdertext': 'Optional',
        'inputtype': 'number'
        },
    {
        'id': f'gr_lower_filter_{bp.botid}',
        'prompt': 'Keep stocks that are __ the lower bound growth rate.',
        'options': ['>', '>='],
        'inputtype': 'dropdown',
        'clearable': True
        }
]

layout = html.Div([
        html.Table(gen_tablecontents(tbodydata)),
        prompt_builder({
            'id': f'submitbutton_{bp.botid}',
            'inputtype': 'button_submit'
            }),
        html.Br(),
        dcc.Tabs([
            dcc.Tab(html.Div(
                dash_inputbuilder({
                    'inputtype': 'table',
                    'id': f"agestats_{bp.botid}"
                    }), className=format_tabs), label='Overall Stats'),
            dcc.Tab(html.Div(
                dash_inputbuilder({
                    'inputtype': 'table',
                    'id': f"trialstats_{bp.botid}"
                    }), className=format_tabs), label='Stats by Trial')
            ])
    ])

# get random dates
@app.callback(
    Output(f'daterange_{bp.botid}', "start_date"),
    Output(f'daterange_{bp.botid}', "end_date"),
    Input(f'randomize_{bp.botid}', "n_clicks"),
    prevent_initial_call=True
    )
def randomize_date(n_clicks):
    new_start = random_dates(staticmindate, staticmaxdate, 1)[0]
    new_end = random_dates(DateOperations().plusminusdays(new_start, 1), staticmaxdate, 1)[0]
    return new_start, new_end

@app.callback(
    Output(f'agestats_{bp.botid}', 'data'),
    Output(f'trialstats_{bp.botid}', 'data'),
    Input(f'submitbutton_{bp.botid}', 'n_clicks'),
    State(f'num_trials_{bp.botid}', "value"),
    State(f'testlen_{bp.botid}', "value"),
    State(f'daterange_{bp.botid}', "start_date"),
    State(f'daterange_{bp.botid}', "end_date"),
    State(f'datepicker_{bp.botid}', "value"),
    State(f'rank_beg_{bp.botid}', "value"),
    State(f'rank_end_{bp.botid}', "value"),
    State(f'gr_upper_{bp.botid}', "value"),
    State(f'gr_upper_filter_{bp.botid}', "value"),
    State(f'gr_lower_{bp.botid}', "value"),
    State(f'gr_lower_filter_{bp.botid}', "value"),
    Input(f"agestats_{bp.botid}", 'sort_by'),
    Input(f"trialstats_{bp.botid}", 'sort_by'),
    Input(f"agestats_{bp.botid}", 'data'),
    Input(f"trialstats_{bp.botid}", 'data'),
    prevent_initial_call=True
    )
def calc_agebot(
        n_clicks,
        num_trials,
        testlen,
        beg_date,
        end_date,
        manualselectdates,
        rank_beg,
        rank_end,
        gr_upper,
        gr_upper_dir,
        gr_lower,
        gr_lower_dir,
        agestat_sort,
        trialstat_sort,
        agestat_data,
        trialstat_data
        ):
    if agestat_data and trialstat_data and callback_context.triggered[0]['prop_id'].endswith('sort_by'):
        # convert table back to dataframe
        statdf = pd.DataFrame.from_records(agestat_data)
        statdf = DataTableOperations().sort_datatable(agestat_sort, statdf)
        alltrialsummariesdf = pd.DataFrame.from_records(trialstat_data)
        alltrialsummariesdf = DataTableOperations().sort_datatable(trialstat_sort, alltrialsummariesdf)
        return statdf.to_dict('records'), alltrialsummariesdf.to_dict('records')
    else:
        # form bot run-specific parameters ('brp').
        brp = {**brpb_base(bp.botid, 1), **{
            'num_trials': num_trials,
            'testlen': testlen,
            'firstdate': beg_date,
            'latestdate': end_date,
            'statictrialexistdates': manualselectdates,
            'rank_beg': rank_beg,
            'rank_end': rank_end,
            'gr_u': gr_upper,
            'gr_l': gr_lower,
            'dir_u': gr_upper_dir,
            'dir_l': gr_lower_dir
            }}
        # create table
        statdf, alltrialsummariesdf = bp.botfunc(brp)
        # delete temp files and folder
        delete_folder(getbotsinglerunfolder(brp['rootdir'], brp['testregimename'], brp['todaysdate'], brp['testnumber']))
        return statdf.to_dict('records'), alltrialsummariesdf.to_dict('records')


from model.client import Client
import random

def test_del_client(app, db, check_ui):
    if len(db.get_group_list()) == 0:
        app.client.add_client(Client(firstname="Ekaterina", lastname="Bocharova", address="Shumakova, 23a",
                                      home="123456", mobile="987654321", work="456123", email="user1@mail.ru", email2="user2@mail.ru"))
    old_clients = db.get_client_list()
    client = random.choice(old_clients)
    app.client.del_client_by_id(client.id)
    assert len(old_clients) - 1 == app.client.count()
    old_clients.remove(client)
    new_clients = db.get_client_list()
    assert old_clients == new_clients
    if check_ui:
        assert sorted(new_clients, key=Client.id_or_max) == sorted(app.client.get_client_list(), key=Client.id_or_max)


#Will take a directory full of covariance models and feed them to Infernal's cmcalibrate one at a time.
#The resulting covariance models are put in sys.argv[3].
#This script may take several days to complete.  Calibrating models of large alignments is slow.

#Necessary modules: biopython, infernal

#Usage: python Infernal_cmcalibrate.py <directory of cov models> <directory of stockholm alignments> <directory in which to put calibrated models>

import sys
import os
from Bio import AlignIO
import subprocess
import shutil

def runcmcalibrate(modelname):
    subprocess.check_call(['cmcalibrate', modelname])

def calibratemodels(modelsdirectory, stockholmdirectory, calibratedmodelsdirectory):
    modelcounter = 0
    calibratedmodelcounter = 0
    modelsdirectory = os.path.abspath(modelsdirectory)
    stockholmdirectory = os.path.abspath(stockholmdirectory)
    models = [os.path.join(modelsdirectory, model) for model in os.listdir(modelsdirectory)]

    #If calibratedmodelsdirectory doesn't exist, make it
    if os.path.exists ('./' + str(calibratedmodelsdirectory)) == False:
        os.mkdir('./' + str(calibratedmodelsdirectory))

    for model in models:
        modelcounter +=1
        if modelcounter % 10 == 0:
            sys.stderr.write('Calibrating model {0} of {1}.\n'.format(modelcounter, len(models)))
        #Get corresponding stockholm alignment
        #if calibrated model doesn't already exist in calibratedmodelsdirectory
        if os.path.exists(str(os.path.abspath(calibratedmodelsdirectory) + '/' +
                              str(os.path.basename(model.replace('.cm', '.c.cm'))))) == False:
            stockholmalignmentname = os.path.basename(model.replace('.cm',''))
            stockholmalignment = os.path.join(stockholmdirectory, stockholmalignmentname)

            #Only calibrate models where the alignment is less than 2 kb.
            #Otherwise it takes forEVER. Still, expect an average of hours per alignment.
            alignment_length = AlignIO.read(stockholmalignment, 'stockholm').get_alignment_length()
            if alignment_length >= 10 and alignment_length <= 4000:
                runcmcalibrate(model)
                #Move calibrated model to its correct directory
                shutil.copy2(str(model), str(os.path.abspath(calibratedmodelsdirectory)) + '/' +
                             str(os.path.basename(model.replace('.cm','.c.cm'))))
                calibratedmodelcounter +=1

    sys.stderr.write('Calibrated {0} of {1} models. The rest were too big.\n'.format(calibratedmodelcounter, len(models)))

if __name__ == '__main__':
    calibratemodels(sys.argv[1], sys.argv[2], sys.argv[3])


'''
10-1. Learning Python: Open a blank file in your text editor and write a few lines summarizing what you’ve learned about Python so far.
Start each line with the phrase In Python you can. . . . Save the file as learning_python.txt in the same directory as your exercises from this chapter.
Write a program that reads the file and prints what you wrote three times.
Print the contents once by reading in the entire file, once by looping over the file object,
and once by storing the lines in a list and then working with them outside the with block.
'''
file_name = 'p09_handsOn_Dummy.txt'

with open(file_name) as file_obj:
    contents = file_obj.read()

print(contents)
print('-------------------------------------------------------------------')

with open(file_name) as file_obj:
    for line in file_obj:
        print(line)
print('-------------------------------------------------------------------')

with open(file_name) as file_obj:
    lines = file_obj.readlines()

    for line in lines:
        print(line)
print('-------------------------------------------------------------------')

'''
10-2. Learning C: You can use the replace() method to replace any word in a string with a different word. Here’s a quick example showing how to replace
'dog' with 'cat' in a sentence:
  message = "I really like dogs."
  message.replace('dog', 'cat') -> 'I really like cats.'
Read in each line from the file you just created, learning_python.txt, and replace the word Python with the name of another language, such as C.
Print each modified line to the screen.
'''
with open(file_name) as file_obj:
    lines = file_obj.readlines()

    for line in lines:
        line = line.replace('Python', 'C#').strip()
        print(line)
print('-------------------------------------------------------------------')

'''
10-3. Guest: Write a program that prompts the user for their name. When they respond, write their name to a file called guest.txt.
'''
name = input('Enter your name: ')

file_name = 'p09_guest.txt'
with open(file_name, 'w') as file_obj:
    file_obj.write(name)
print('-------------------------------------------------------------------')

'''
10-4. Guest Book: Write a while loop that prompts users for their name. When they enter their name, print a greeting to the screen and add a line recording
their visit in a file called guest_book.txt. Make sure each entry appears on a new line in the file.
'''
print('Info: Press "q" to quit anytime')
file_name = "p09_guest_book.txt"
with open(file_name, 'w') as file_object:
    while(True):
        name = input('enter your name: ')
        if name == 'q':
            break
        file_object.write(f"{name.title()} \n")
        print(f"{name.title()} is registered now")

print('-------------------------------------------------------------------')
'''
10-5. Programming Poll: Write a while loop that asks people why they like programming.
Each time someone enters a reason, add their reason to a file that stores all the responses.
'''
file_name = 'p09_responses.txt'
print('Info: Press "q" to quit anytime')
while(True):
    response = input('Why you like programming? ')
    if response == 'q':
        break
    with open(file_name, 'a') as file_object:
        file_object.write(f"{response}\n")


import project
import pandas as pd

df = pd.read_csv('test.csv')

def test_get_names():
    output = project.get_names(df)
    expected = ('Sarah', 'Russell')
    assert output == expected

def test_sum_of_cost():
    expected = (29.0, 45.0, 'Sarah', 'Russell', '       Vitamins    $15\nDriving 12miles     $2\n       Medicine $12.00', '         shorts $20\nSchool supplies $20\n         co-pay  $5')
    output = project.sum_of_cost(('Sarah', 'Russell'),df)
    assert output == expected

def test_who_owes():
    output = project.who_owes((29.0, 45.0, 'Sarah', 'Russell', '       Vitamins    $15\nDriving 12miles     $2\n       Medicine $12.00', '         shorts $20\nSchool supplies $20\n         co-pay  $5'))
    expected =  ('$8.00 is what is owed to Russell.', 'Russell spent $45.00 this month.', 'Sarah spent $29.00 this month.', '         shorts $20\nSchool supplies $20\n         co-pay  $5', '       Vitamins    $15\nDriving 12miles     $2\n       Medicine $12.00')
    assert output == expected


import sqlite3
from flask import Flask, render_template, jsonify, request, redirect, send_from_directory, make_response
from flask.ext.triangle import Triangle
from flask.ext.sqlalchemy import SQLAlchemy
from contextlib import closing
import config
import json
import os
import traceback
from functools import wraps

app = Flask(__name__)
app.config.from_object(config)
Triangle(app)
db = SQLAlchemy(app)

import commands
from models import *

def secret_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        #Support both query param (for gdb script) and cookies (browser)
        if "secret" in request.cookies and request.cookies["secret"] == config.ACCESS_PASSWORD:
            return f(*args, **kwargs)
        elif "secret" in request.args and request.args["secret"] == config.ACCESS_PASSWORD:
            return f(*args, **kwargs)
        else:
            return "", 404
    return decorated_function

@app.route("/")
def index():
    #If we have secret in query params set it as cookie and redirect back to this
    if "secret" in request.args and request.args["secret"] == config.ACCESS_PASSWORD:
        response = make_response(redirect("/"))
        response.set_cookie("secret", request.args["secret"])
        return response
    elif "secret" in request.cookies and request.cookies["secret"] == config.ACCESS_PASSWORD:
        return render_template("base.html", secret=config.ACCESS_PASSWORD)
    else:
        return "", 404

@app.route("/static/js/<path:filename>")
@secret_required
def javascript_files(filename):
    return send_from_directory(
        os.path.join(app.root_path, "static/js"),
        filename
    )

@app.route("/api/inspect/messages", methods=["GET"])
@secret_required
def get_messages():
    return json.dumps([x.as_dict() for x in Message.query.order_by("date desc").limit(150)])

@app.route("/api/inspect/errors", methods=["GET"])
@secret_required
def get_errors():
    return json.dumps([x.as_dict() for x in Error.query.order_by("date desc").limit(150)])

@app.route("/api/inspect/queue", methods=["GET"])
@secret_required
def get_queue():
    return json.dumps([x.as_dict() for x in QueuedCommand.query.filter_by(completed=False).order_by("id")])

@app.route("/api/inspect/queue/completed", methods=["GET"])
@secret_required
def get_completed_queue():
    return json.dumps([x.as_dict() for x in QueuedCommand.query.filter_by(completed=True).order_by("-id").limit(150)])

@app.route("/api/report/command", methods=["POST"])
@secret_required
def handle_command():
    try:
        header_str = request.form["header"]
        print("Got command " + request.form["command"])
        print("Got header " + header_str)
        header_bytes = []
        for byte in header_str.split(" "):
            header_bytes.append(int(byte, 16).to_bytes(1, byteorder="little"))
        commands.handle_command(request.form["command"], header_bytes)
    except Exception as e:
        traceback.print_exc()
        return traceback.format_exc(), 500
    return request.form["command"], 200

@app.route("/api/report/error", methods=["POST"])
@secret_required
def handle_error():
    e = Error(error_msg=request.form["error_msg"], exception=request.form["exception"], traceback=request.form["traceback"])
    db.session.add(e)
    db.session.commit()
    return "OK", 200

@app.route("/api/queue", methods=["GET"])
@secret_required
def get_next_queued_command():
    command = QueuedCommand.query.filter_by(completed=False).order_by("id").first()
    if command:
        json_obj = json.dumps(command.as_dict())
        command.completed = True
        db.session.commit()
    else:
        json_obj = json.dumps({"command": "", "header": "", "extra": "", "id": -1})
    return json_obj

@app.route("/api/queue", methods=["POST"])
@secret_required
def add_new_queued_command():
    header = ""
    command = ""
    extra = ""
    if "command" in request.form:
        command = request.form["command"]
    else:
        return json.dumps({"error": "command argument was not provided"}), 400
    if "header" in request.form:
        header = request.form["header"]
    if "extra" in request.form:
        extra = request.form["extra"]
    command = QueuedCommand(command=command, header=header, extra=extra)
    db.session.add(command)
    db.session.commit()
    return "OK", 200

@app.route("/api/queue/delete", methods=["POST"])
@secret_required
def delete_queued_command():
    if "id" in request.form:
        id = request.form["id"]
    else:
        return json.dumps({"error": "id argument was not provided"}), 400
    command = QueuedCommand.query.filter_by(id=int(request.form["id"])).first()
    if command:
        db.session.delete(command)
        db.session.commit()
        return "OK", 200
    else:
        return json.dumps({"error": "couldn't find command"}), 404

@app.route("/partials/<name>")
@secret_required
def get_partial(name):
    return render_template(name)

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


from custom_exceptions.id_not_found import IdNotFound

class Customer_name:
    def __init__(self, first_name: str, last_name: str, cust_id_number: int):
        self.first_name = first_name
        self.last_name = last_name
        self.cust_id_number = cust_id_number

    def convert_to_dictionary(self):
        return {
            "firstName": self.first_name,
            "lastName": self.last_name,
            "custIdNumber": self.cust_id_number
        }

    #class BankAccount:

        #def __init__(self, cust_id_number: int, ba_id_number: int, money_amount: float):
            #self.ba_id_number = ba_id_number
            #self.cust_id_number = cust_id_number
            #self.money_amount = money_amount

# whats a customer?
"""
Customers have first names.
Customers have last names.
Customers have id numbers.
Customers have bank accounts.

the account information will be separate, so I need to focus on the name and id number
"""

class BankAccount:

    def __init__(self, cust_id_number: int, ba_id_number: int, money_amount: float):
        self.ba_id_number = ba_id_number
        self.cust_id_number = cust_id_number
        self.money_amount = money_amount

class BankAccount2:

    def __init__(self, id_number):
        self.id_number = id_number
        self.balance = 0

    def deposit_funds(self, id_number):
        if self.id_number == id_number:
            amount=float
            self.balance += amount
            return self.balance
        else:
            raise IdNotFound("No bank account matches this ID number. Try again")

    def withdraw_funds(self, id_number):
        if self.id_number == id_number:
            amount=float
            self.balance += amount
            return self.balance
        else:
            raise IdNotFound("No bank account matches this ID number. Try again")

# try:
    #     cust_data: dict = request.get_json()
    #     customer = Customer_name(cust_data["firstName"], cust_data["lastName"], cust_data["custIdNumber"])
    #     result = cust_service.service_create_customer(customer)
    #     result_dictionary = result.convert_to_dictionary()
    #     result_json = jsonify(result_dictionary)
    #     return result_json, 201
    # except BadCustomerInfo as e:
    #     message = {
    #         "message":str(e)
    #     }
    #     return jsonify(message), 400
    # except IdNotFound as e:
    #     message = {
    #         "message":str(e)
    #     }
    #     return jsonify(message), 400


from __future__ import annotations

from copy import deepcopy
from functools import partial
from typing import MutableMapping

import pytorch_lightning as pl
import pytorch_lightning.loggers

class LightningModule(pl.LightningModule):
    # noinspection PyAttributeOutsideInit
    def just_save_hyperparameters(self, hparams: MutableMapping, logger: bool = True):
        self._log_hyperparams = logger
        self._set_hparams(hparams)
        self._hparams_name = 'kwargs'
        self._hparams_initial = deepcopy(self._hparams)

class TensorBoardLogger(pl.loggers.TensorBoardLogger):
    @pl.utilities.rank_zero_only
    def log_metrics(self, metrics, step):
        metrics.pop('epoch', None)
        return super().log_metrics(metrics, step)

Trainer = partial(pl.Trainer, log_every_n_steps=1, enable_model_summary=False)
LearningRateMonitor = partial(
    pl.callbacks.LearningRateMonitor,
    logging_interval='step')
ModelCheckpoint = partial(pl.callbacks.ModelCheckpoint, save_top_k=-1)


#!/usr/bin/env python3

import sys
sys.dont_write_bytecode = True
import os, shutil, random, pickle, glob, operator, collections
import numpy, pandas
from configparser import ConfigParser
from collections import defaultdict
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences

def read_tokens(file_path, dropout=None):
  """Read n tokens from specified file"""

  tokens = open(file_path).read().split(' ')

  if dropout is not None:
    tokens_to_keep = round(len(tokens) * (1 - dropout))
    tokens = random.sample(tokens, tokens_to_keep)

  return tokens

class DatasetProvider:
  """Make x and y from raw data"""

  def __init__(
    self,
    train_dir,
    targ_file,
    model_dir,
    min_examples_per_targ):
    """Constructor"""

    # for converting input tokens to int sequences
    self.tokenizer = Tokenizer(
      num_words=None,
      oov_token='oovtok',
      lower=False)

    # encounter id -> set of targets
    self.enc2targs = defaultdict(set)

    # target -> int index
    self.targ2int = {}

    self.train_dir = train_dir
    self.targ_file = targ_file
    self.model_dir = model_dir
    self.min_examples_per_targ = min_examples_per_targ

    if os.path.isdir(model_dir):
      shutil.rmtree(model_dir)
    os.mkdir(model_dir)

    self.index_targets()

  def index_targets(self):
    """Process medication file"""

    df = pandas.read_csv(self.targ_file, dtype='str')

    for enc, drug in zip(df['HADM_ID'], df['DRUG']):
      self.enc2targs[enc].add(drug.lower())

    targ_counter = collections.Counter()
    for targs in self.enc2targs.values():
      targ_counter.update(targs)

    outfile = open(os.path.join(self.model_dir, 'targs.txt'), 'w')
    for med, count in targ_counter.most_common():
      outfile.write('%s|%s\n' % (med, count))

    # make alphabet for *frequent* targets
    index = 0
    for targ, count in targ_counter.items():
      if count > self.min_examples_per_targ:
        self.targ2int[targ] = index
        index = index + 1

  def load(self):
    """Process notes to make x and y"""

    x = [] # input documents (n_docs, max_seq_len)
    y = [] # targets we are predicting for each input

    for train_file in os.listdir(self.train_dir):

      targ_vec = numpy.zeros(len(self.targ2int))
      enc = train_file.split('.')[0]

      if enc not in self.enc2targs:
        continue

      no_labels_for_this_file = True
      for targ in self.enc2targs[enc]:
        if targ in self.targ2int:
          targ_vec[self.targ2int[targ]] = 1
          no_labels_for_this_file = False

      if no_labels_for_this_file:
        continue # all rare codes

      y.append(targ_vec)

      tokens = read_tokens(os.path.join(self.train_dir, train_file))
      x.append(' '.join(set(tokens)))

    self.tokenizer.fit_on_texts(x)
    pickle_file = open('Model/tokenizer.p', 'wb')
    pickle.dump(self.tokenizer, pickle_file)
    print('input vocabulary size:', len(self.tokenizer.word_index))

    x = self.tokenizer.texts_to_sequences(x)
    max_seq_len = max(len(seq) for seq in x)
    x = pad_sequences(x, maxlen=max_seq_len)

    return x, numpy.array(y)

if __name__ == "__main__":

  cfg = ConfigParser()
  cfg.read(sys.argv[1])
  base = os.environ['DATA_ROOT']

  dp = DatasetProvider(
    os.path.join(base, cfg.get('data', 'train')),
    os.path.join(base, cfg.get('data', 'targs')),
    cfg.get('data', 'model_dir'),
    cfg.getfloat('args', 'min_examples_per_targ'))

  x, y = dp.load()
  print('x:', x.shape)
  print('y:', y.shape)


import sys
sys.stdin = open("input.txt", "r") # 제거
input = sys.stdin.readline

n = int(input())
d = [0] * (n + 1)
if n <= 2:
    print(n)
else:
    d[1], d[2] = 1, 2
    for i in range(3, n + 1):
        d[i] = (d[i - 2] + d[i - 1]) % 15746
    print(d[-1])


import math
import random
import torch

from fairseq import metrics, utils
from fairseq.criterions import FairseqCriterion, register_criterion

_EPS = 1e-9

def selection_entropy(controllers):
    return torch.stack([
        ctrl.entropy().mean() for ctrl in controllers
        if ctrl is not None], axis=0).mean()

def batch_selection_entropy(controllers):
    def layer_entropy(layer_ctrl):
        probs = layer_ctrl.probs.mean(0)
        return (-probs * torch.log(probs + _EPS)).sum(-1)

    return torch.stack([
        layer_entropy(ctrl) for ctrl in controllers
        if ctrl is not None], axis=0).mean()

def label_smoothed_nll_loss(lprobs, target, epsilon, ignore_index=None, reduce=True):
    if target.dim() == lprobs.dim() - 1:
        target = target.unsqueeze(-1)
    nll_loss = -lprobs.gather(dim=-1, index=target)
    smooth_loss = -lprobs.sum(dim=-1, keepdim=True)
    if ignore_index is not None:
        pad_mask = target.eq(ignore_index)
        nll_loss.masked_fill_(pad_mask, 0.)
        smooth_loss.masked_fill_(pad_mask, 0.)
    else:
        nll_loss = nll_loss.squeeze(-1)
        smooth_loss = smooth_loss.squeeze(-1)
    if reduce:
        nll_loss = nll_loss.sum()
        smooth_loss = smooth_loss.sum()
    eps_i = epsilon / lprobs.size(-1)
    loss = (1. - epsilon) * nll_loss + eps_i * smooth_loss
    return loss, nll_loss

def compute_sel_lprobs(ctrl_outputs):
    def compute_single(ctrl_out):
        return ctrl_out.ctrl.log_prob(
            ctrl_out.ctrl_prediction).sum(-1)

    sel_lprobs = torch.stack([
        compute_single(out) for out in ctrl_outputs if out is not None
    ], axis=0).sum(0)

    return sel_lprobs

@register_criterion('label_smoothed_cross_entropy_modular')
class LabelSmoothedCrossEntropyModularCriterion(FairseqCriterion):

    def __init__(self,
                 task,
                 sentence_avg,
                 label_smoothing,
                 ctrl_alpha,
                 e_step_size,
                 m_steps,
                 modular_warmup_updates):
        super().__init__(task)
        self.sentence_avg = sentence_avg
        self.eps = label_smoothing
        self.ctrl_alpha = ctrl_alpha

        self.e_step_size = e_step_size
        self.m_steps = m_steps

        self.step_id = 1  # indicates when to apply e-step
        assert self.m_steps > 0

        self.warmup_steps = modular_warmup_updates
        assert self.warmup_steps >= 0

    @staticmethod
    def add_args(parser):
        """Add criterion-specific arguments to the parser."""
        # fmt: off
        parser.add_argument('--label-smoothing', default=0., type=float, metavar='D',
                            help='epsilon for label smoothing, 0 means no label smoothing')
        parser.add_argument('--ctrl-alpha', default=1., type=float, metavar='A',
                            help='ctrl selection loss weight')
        parser.add_argument('--e-step-size', default=10, type=int, metavar='N',
                            help='number of samples per expectation step '
                                 '(negative value means compute across all '
                                 'ctrl configurations)')
        parser.add_argument('--m-steps', default=10, type=int, metavar='N',
                            help='number of maximization steps between the e-steps')
        parser.add_argument('--modular-warmup-updates', default=0, type=int, metavar='N',
                            help='number of updates without controller selection')
        # fmt: on

    def forward(self, model, sample, reduce=True):
        """Compute the loss for the given sample.

        Returns a tuple with three elements:
        1) the loss
        2) the sample size, which is used as the denominator for the gradient
        3) logging outputs to display while training
        """
        if self.training:
            if self.warmup_steps > 0:
                net_out = model(**sample['net_input'], mode='full')
            else:
                # Expectation step (do not save gradients)
                if self.step_id % (self.m_steps + 1) == 0:
                    if self.e_step_size != 0:
                        sampled_outputs = self.sample_outputs(
                            model, sample,
                            sample_size=self.e_step_size)
                        self.update_best_ctrl_selection(model, sampled_outputs, sample)
                self.step_id = (self.step_id + 1) % (self.m_steps + 1)
                net_out = model(**sample['net_input'], data_indices=sample['id'], mode='m_step')
        else:
            net_out = model(**sample['net_input'], mode='validation')

        loss, nll_loss, ctrl_loss, sel_entropy, batch_entropy = self.compute_loss(
            model, net_out, sample, reduce=reduce)
        sample_size = sample['target'].size(0) if self.sentence_avg else sample['ntokens']
        logging_output = {
            'loss': loss.data,
            'nll_loss': nll_loss.data,
            'ctrl_loss': ctrl_loss.data,
            'sel_entropy': sel_entropy.data,
            'batch_entropy': batch_entropy.data,
            'ntokens': sample['ntokens'],
            'nsentences': sample['target'].size(0),
            'sample_size': sample_size,
        }

        if self.warmup_steps > 0:
            self.warmup_steps -= 1
        else:
            logging_output['encoder_ctrl_probs'] = net_out[1]['ctrl_output']['encoder'].ctrl.probs.data
            if net_out[1]['ctrl_output']['decoder'] is not None:
                logging_output['decoder_ctrl_probs'] = net_out[1]['ctrl_output']['decoder'].ctrl.probs.data

        return loss, sample_size, logging_output

    def sample_outputs(self, model, sample, sample_size=-1):
        with torch.no_grad():
            bsz = sample['id'].size(0)
            sample_selections = model.list_all_selections()
            random.shuffle(sample_selections)
            outputs = []

            # negative sample_size is a shortcut for using all possible selections
            if sample_size >= 0:
                sample_selections = sample_selections[:sample_size]

                # We run the m-step first, to get estimated outputs of
                # the current best selections
                outputs.append(
                    model(
                        **sample['net_input'],
                        data_indices=sample['id'],
                        mode='m_step',
                    )
                )

            for selection in sample_selections:
                for key in [k for k, v in selection.items() if v is not None]:
                    selection[key] = selection[key].repeat(bsz, 1)
                outputs.append(
                    model(
                        **sample['net_input'],
                        mode='e_step',
                        fixed_selection=selection,
                    )
                )
            return outputs

    def compute_loss(self, model, net_output, sample, reduce=True):
        lprobs = model.get_normalized_probs(net_output, log_probs=True)
        bsz = lprobs.size(0)
        lprobs = lprobs.view(-1, lprobs.size(-1))

        ctrl_outputs = [
            ctrl_out for ctrl_out in net_output[1]['ctrl_output'].values()
        ]
        sel_entropy = torch.tensor(0).to(lprobs.device)
        batch_entropy = torch.tensor(0).to(lprobs.device)
        if self.warmup_steps > 0:
            sel_loss = torch.tensor(0).to(lprobs.device)
        else:
            sel_lprobs = compute_sel_lprobs(ctrl_outputs)
            sel_loss = -sel_lprobs

            sel_entropy = selection_entropy([c.ctrl for c in ctrl_outputs if c is not None])
            batch_entropy = batch_selection_entropy([c.ctrl for c in ctrl_outputs if c is not None])

        target = model.get_targets(sample, net_output).view(-1, 1)
        loss, nll_loss = label_smoothed_nll_loss(
            lprobs, target, self.eps, ignore_index=self.padding_idx,
            reduce=False,
        )
        loss = loss.view(bsz, -1).sum(1)

        if reduce:
            loss = loss.sum()
            nll_loss = nll_loss.sum()
            sel_loss = sel_loss.sum()
        loss = loss + self.ctrl_alpha * sel_loss
        return loss, nll_loss, sel_loss, sel_entropy, batch_entropy

    def update_best_ctrl_selection(self, model, net_outputs, sample):
        selection = self.compute_best_selection(model, net_outputs, sample)
        model.update_best_ctrl_selection(selection, sample['id'])

    def compute_best_selection(self, model, net_outputs, sample):
        lprobs = [model.get_normalized_probs(out, log_probs=True) for out in net_outputs]
        bsz = lprobs[0].size(0)
        lprobs = [lp.view(-1, lp.size(-1)) for lp in lprobs]

        # We compute the target loglikelihoods via nll_loss function
        # We want non-smoothed probability for the best_prediction estimation
        target = model.get_targets(sample, net_outputs[0]).view(-1, 1)
        nll_losses = []
        for lp in lprobs:
            _, nll_loss = label_smoothed_nll_loss(
                lp, target, self.eps, ignore_index=self.padding_idx,
                reduce=False)
            nll_losses.append(nll_loss.view(bsz, -1).sum(1))

        # shape(bsz, n_samples)
        nll_losses = torch.stack(nll_losses, axis=1)
        loglikelihoods = -nll_losses

        ctrl_outputs = [
            net_out[1]['ctrl_output'] for net_out in net_outputs
        ]

        sel_lprobs = torch.stack([
            compute_sel_lprobs(
                [val for val in ctrl_out.values()]
            ) for ctrl_out in ctrl_outputs
        ], axis=1)

        joint_lprobs = loglikelihoods + sel_lprobs
        best_sel_indices = joint_lprobs.max(1)[1].detach()
        best_sel_indices = [list(range(bsz)), best_sel_indices]

        selection = {
            "encoder": torch.stack(
                [
                    net_out[1]['ctrl_output']['encoder'].selection
                    for net_out in net_outputs
                ], axis=1)[best_sel_indices],
            "decoder": None
        }
        if net_outputs[0][1]['ctrl_output']['decoder'] is not None:
            selection["decoder"] = torch.stack(
                [
                    net_out[1]['ctrl_output']['decoder'].selection
                    for net_out in net_outputs
                ], axis=1)[best_sel_indices]
        return selection

    @staticmethod
    def reduce_metrics(logging_outputs) -> None:
        """Aggregate logging outputs from data parallel training."""
        loss_sum = sum(log.get('loss', 0) for log in logging_outputs)
        nll_loss_sum = sum(log.get('nll_loss', 0) for log in logging_outputs)
        ctrl_loss_sum = sum(log.get('ctrl_loss', 0) for log in logging_outputs)
        sel_entropy = sum(log.get('sel_entropy', 0) for log in logging_outputs)
        batch_entropy = sum(log.get('batch_entropy', 0) for log in logging_outputs)
        ctrl_entropy_ratio = sel_entropy / (batch_entropy + _EPS)
        ntokens = sum(log.get('ntokens', 0) for log in logging_outputs)
        sample_size = sum(log.get('sample_size', 0) for log in logging_outputs)
        nsentences = sum(log.get('nsentences', 0) for log in logging_outputs)

        metrics.log_scalar('loss', loss_sum / sample_size / math.log(2), sample_size, round=3)
        metrics.log_scalar('nll_loss', nll_loss_sum / ntokens / math.log(2), ntokens, round=3)
        metrics.log_scalar('ctrl_loss', ctrl_loss_sum / sample_size / math.log(2), nsentences, round=3)
        metrics.log_scalar('sel_entropy', sel_entropy, 1, round=3)
        metrics.log_scalar('batch_entropy', batch_entropy, 1, round=3)
        metrics.log_scalar('ctrl_entropy_ratio', ctrl_entropy_ratio, 1, round=3)
        metrics.log_derived('ppl', lambda meters: utils.get_perplexity(meters['nll_loss'].avg))

    @staticmethod
    def logging_outputs_can_be_summed() -> bool:
        """
        Whether the logging outputs returned by `forward` can be summed
        across workers prior to calling `reduce_metrics`. Setting this
        to True will improves distributed training speed.
        """
        return True


# 유튜브 리스트를 주소와 제목만 가져오기
# selenium 과 beautifulSoup 를 사용하여 처리
# 제작기간 : 4시간 / 처음으로 selenium 과 beautifulSoup 사용
# 부족한 부분이나 개선할 부분이 있으면 알려주기 바람
# 유튜브 영상을 다른 곳에 올리기 위한 작업중 기존 리스트의 내용을 하나씩 담는 것이 부담스러워 제작함
# 사용법 : 소스를 적당히 알아서 사용하면 됨
# 도스창에서
# C:\>python step3.py >> youtubeList.txt
# 이와 같이하면 파일로 저장됨

from selenium import webdriver
from bs4 import BeautifulSoup
import time

# youtube 의 playlist 를 아래에 넣으면, 플레이리스트안에 있는 모든 영상파일의 링크와 제목을 가져온다.
playlist = 'PLsS-TVNjbU7cdhwa-s-X7CTOIiP761yVP'

# PC 에 selenium 의 웹드라이브(크롬브라우저사용)를 다운받아 둘것
# 아래의 위치에 chromedriver.exe 파일을 압축해제하여 둠
path = "C:/Utils/chromedriver.exe"

driver = webdriver.Chrome(path)
# driver.implicitly_wait(1)    #1초 기다림

# 유튜브 플레이리스트
driver.get('https://www.youtube.com/playlist?list='+playlist)

# 화면상 모든 소스를 html 에 저장
html = driver.page_source

# BeautifulSoup 을 사용하여 html 을 파싱함
soup = BeautifulSoup(html, 'html.parser')

# 전제 페이지 중 a 태그의 class 가 'yt-simple-endpoint style-scope ytd-playlist-video-renderer' 인 것을 선택
# 이곳이 리스트의 링크와 제목이 담긴 곳
all_title = soup.find_all('a', {'class':'yt-simple-endpoint style-scope ytd-playlist-video-renderer'})

# 리스트 안의 영상의 갯수만큼 링크와 타이틀을 가져옴
# 타이틀은 내부에서 "title" 태그를 사용하지 않아 편법으로 get_text() 를 이용해 순수한 텍스트만 골라낸 후
# 앞에서 30번째 부터 시작해서 30개의 문자를 가져온 후 뒤편의 공백(\n포함)을 제거함
for link in all_title:
    tmp = link.get('href')
    ttle = link.get_text()[30:60].strip()
    print('https://youtu.be/'+tmp[9:20], ttle)

# 웹드라이브 종료
driver.close()


import logging

from openlp.core.lib import Plugin, StringContent, build_icon, translate
from openlp.core.lib.db import Manager
from openlp.plugins.custom.lib import CustomMediaItem, CustomTab
from openlp.plugins.custom.lib.db import CustomSlide, init_schema
from openlp.plugins.custom.lib.mediaitem import CustomSearch

log = logging.getLogger(__name__)

__default_settings__ = {
    'custom/db type': 'sqlite',
    'custom/db username': '',
    'custom/db password': '',
    'custom/db hostname': '',
    'custom/db database': '',
    'custom/last search type': CustomSearch.Titles,
    'custom/display footer': True,
    'custom/add custom from service': True
}

class CustomPlugin(Plugin):
    """
    This plugin enables the user to create, edit and display custom slide shows. Custom shows are divided into slides.
    Each show is able to have it's own theme.
    Custom shows are designed to replace the use of songs where the songs plugin has become restrictive.
    Examples could be Welcome slides, Bible Reading information, Orders of service.
    """
    log.info('Custom Plugin loaded')

    def __init__(self):
        super(CustomPlugin, self).__init__('custom', __default_settings__, CustomMediaItem, CustomTab)
        self.weight = -5
        self.db_manager = Manager('custom', init_schema)
        self.icon_path = ':/plugins/plugin_custom.png'
        self.icon = build_icon(self.icon_path)

    def about(self):
        about_text = translate('CustomPlugin', 'Custom Slide Plugin
The custom slide plugin '
                               'provides the ability to set up custom text slides that can be displayed on the screen '
                               'the same way songs are. This plugin provides greater freedom over the songs plugin.')
        return about_text

    def uses_theme(self, theme):
        """
        Called to find out if the custom plugin is currently using a theme.

        Returns True if the theme is being used, otherwise returns False.
        """
        if self.db_manager.get_all_objects(CustomSlide, CustomSlide.theme_name == theme):
            return True
        return False

    def rename_theme(self, old_theme, new_theme):
        """
        Renames a theme the custom plugin is using making the plugin use the new name.

        :param old_theme: The name of the theme the plugin should stop using.
        :param new_theme: The new name the plugin should now use.
        """
        customs_using_theme = self.db_manager.get_all_objects(CustomSlide, CustomSlide.theme_name == old_theme)
        for custom in customs_using_theme:
            custom.theme_name = new_theme
            self.db_manager.save_object(custom)

    def set_plugin_text_strings(self):
        """
        Called to define all translatable texts of the plugin
        """
        # Name PluginList
        self.text_strings[StringContent.Name] = {
            'singular': translate('CustomPlugin', 'Custom Slide', 'name singular'),
            'plural': translate('CustomPlugin', 'Custom Slides', 'name plural')
        }
        # Name for MediaDockManager, SettingsManager
        self.text_strings[StringContent.VisibleName] = {
            'title': translate('CustomPlugin', 'Custom Slides', 'container title')
        }
        # Middle Header Bar
        tooltips = {
            'load': translate('CustomPlugin', 'Load a new custom slide.'),
            'import': translate('CustomPlugin', 'Import a custom slide.'),
            'new': translate('CustomPlugin', 'Add a new custom slide.'),
            'edit': translate('CustomPlugin', 'Edit the selected custom slide.'),
            'delete': translate('CustomPlugin', 'Delete the selected custom slide.'),
            'preview': translate('CustomPlugin', 'Preview the selected custom slide.'),
            'live': translate('CustomPlugin', 'Send the selected custom slide live.'),
            'service': translate('CustomPlugin', 'Add the selected custom slide to the service.')
        }
        self.set_plugin_ui_text_strings(tooltips)

    def finalise(self):
        """
        Time to tidy up on exit
        """
        log.info('Custom Finalising')
        self.db_manager.finalise()
        Plugin.finalise(self)


#!/usr/bin/env python3

import sys
from collections import deque
from dataclasses import dataclass
from functools import reduce

from utils import io
from utils.parse import ints

def debug(s):
    pass
    # print(s)

@dataclass
class Monkey:
    id: int
    items: list
    op: str
    divisor: int
    to: dict
    count: int = 0

    def operation(self, x):
        if "old * old" in self.op:
            r = x * x
            debug("    Worry level is multiplied by itself to %s." % r)
            return r
        elif "old *" in self.op:
            r = x * ints(self.op)[0]
            debug("    Worry level is multiplied by %s to %s." %
                  (ints(self.op)[0], r))
            return r
        elif "old +" in self.op:
            r = x + ints(self.op)[0]
            debug("    Worry level increases by to %s." % r)
            return r
        else:
            print("undefined op", self.op)
            quit()

    def test(self, arg):
        return arg % self.divisor == 0

def round(monkeys, total=0):
    for c in range(0, len(monkeys)):
        m = monkeys[c]
        debug("Monkey %s :" % m.id)

        while m.items:
            m.count += 1

            item = m.items.popleft()
            debug("  Monkey inspects an item with a worry level of %d." % item)
            item = m.operation(item)

            if total > 0:
                item = item % total
            else:
                item = item // 3

            debug(
                "    Monkey gets bored with item. Worry level is divided by 3 to %d." % item)
            outcome = m.test(item)
            if outcome:
                debug("    Current worry level is divisible by %d." %
                      m.divisor)
            else:
                debug("    Current worry level is not divisible by %d." %
                      m.divisor)

            to = m.to[outcome]
            debug("    Item with worry level %d is thrown to monkey %s." %
                  (item, to))
            monkeys[to].items.append(item)

def parse(input):
    monkeys = {}
    for m in input:
        parts = m.split("\n")
        id = ints(parts[0])[0]
        items = deque(ints(parts[1]))
        op = parts[2]
        divisor = ints(parts[3])[0]

        to = {
            True: ints(parts[4])[0],
            False: ints(parts[5])[0],
        }
        monkeys[id] = Monkey(id, items, op, divisor, to)

    return monkeys

def score(monkeys):
    counts = [m.count for m in monkeys.values()]
    counts.sort()

    return counts[len(counts) - 2] * counts[len(counts) - 1]

def part1(filename):
    monkeys = parse(io.get_input(filename).split("\n\n"))

    for r in range(0, 20):
        round(monkeys, 0)

    return score(monkeys)

def part2(filename):
    monkeys = parse(io.get_input(filename).split("\n\n"))

    product = reduce((lambda x, y: x * y),
                     [m.divisor for m in monkeys.values()])
    samples = set([1, 20] + [i * 1000 for i in range(1, 11)])

    for r in range(0, 10000):
        round(monkeys, product)

        if r + 1 in samples:
            debug("== After round %d ==" % (r+1))
            for m in range(0, len(monkeys)):
                debug("Monkey %d inspected items %d times." %
                      (m, monkeys[m].count))

    return score(monkeys)

def main():
    assert part1("example.txt") == 10605
    print(part1("input.txt"))

    assert part2("example.txt") == 2713310158
    print(part2("input.txt"))

if __name__ == "__main__":
    sys.exit(main())


import pickle
import numpy as np
import environment_
from agent_ import Agent, Random_Agent, Expert, ReplayBuffer, DQN
import string
import pandas as pd
import matplotlib.pyplot as plt

class Evaluate:
    def __init__(self, env, agent, max_steps, run, random_agent=False, trials=100, animate=True):
        self.env = env
        self.random_agent = random_agent
        self.agent = agent
        self.trials = trials
        self.max_steps = max_steps

        self.performance = []
        self.eps_history = []
        self.run = run
        self.animate = animate

        self.reliability = []
        self.intervention_cost = []

    def evaluate(self):
        obs, _, done, info = self.env.reset()

        for _ in range(self.trials):
            score = 0
            steps = 0
            obs, reward, done, info = self.env.reset()

            over = False
            while ((not over) and (steps < self.max_steps)):
                steps += 1
                action = self.agent.choose_action(obs, info["time_step"])

                obs_, reward, done, info = self.env.step(action)

                score += reward

                if done:
                    over = True

                obs = obs_
                #print(self.env.cost_history)

            self.performance.append(score)
            avg_perf = np.mean(self.performance)

            self.reliability.append(1 - self.env.risk[-1])
            self.intervention_cost.append(sum(self.env.cost_history))

            if self.random_agent == False:
                self.eps_history.append(self.agent.epsilon)
                print('episode ', _, 'score %.1f avg score %.1f epsilon %.2f, steps %.1f' %
                                   (score, avg_perf, self.agent.epsilon, steps))

            else:
                print('episode ', _, 'score %.1f avg score %.1f, steps %.1f' %
                      (score, avg_perf, steps))

            if self.animate:
                self.env.render(self.run)

with open("../models/trained_agent_.pkl", "rb") as f:
    AGENT = pickle.load(f)

runs = {
    0: {
        "name": "agent random",
        "random_agent": True,
        "agent": Random_Agent(action_space=3)
    },
    1: {
        "name": "expert greybeard",
        "agent": Expert(mode=5)
    },
    2: {
        "name": "agent rehab",
        "agent": AGENT
    }
}

# EVALUATION PARAMS
TRIALS = 100
HORIZON = 100

# ENVIRONMENT PARAMS
CONSERVATISM = 0.55
RISK_THRESHOLD = 0.70
FAILURE_THRESHOLD = 0.90

results = {
    "trials": TRIALS,
    "agent random": {
        "av_intervention_cost": 0,
        "av_reliability": 0
    },
    "expert greybeard": {
        "av_intervention_cost": 0,
        "av_reliability": 0
    },
    "agent rehab": {
        "av_intervention_cost": 0,
        "av_reliability": 0
    }
}

random_agent = False
animate = False

for _ in runs.keys():
    name = runs[_]["name"]
    random_agent = "random_agent" in runs[_].keys()

    AGENT = runs[_]["agent"]
    ENV = environment_.TransmissionAsset(
        max_steps=HORIZON,
        train_mode=False,
        conservatism=CONSERVATISM,
        risk_threshold=RISK_THRESHOLD,
        failure_threshold=FAILURE_THRESHOLD
    )

    print(ENV.action_spaces.n)

    run = string.capwords(name)
    EVALUATION = Evaluate(
        ENV,
        AGENT,
        run=run,
        max_steps=HORIZON,
        random_agent=True,
        trials=TRIALS,
        animate=animate
    )

    print(f"EVALUATING: {name.capitalize()}")
    EVALUATION.evaluate()

    intervention_cost = np.array(EVALUATION.intervention_cost)
    reliability = np.array(EVALUATION.reliability)

    av_intervention_cost = np.mean(intervention_cost)
    av_reliability = np.mean(reliability)

    results[name]["av_intervention_cost"] = av_intervention_cost
    results[name]["av_reliability"] = av_reliability

### OUTPUT RESULTS ###

results_df = pd.DataFrame(results)
results_df.to_csv("plots/results.csv")

agents = list(results.keys())[1:]  # Exclude 'trials' key
av_intervention_cost = [results[agent]['av_intervention_cost'] for agent in agents]
av_reliability = [results[agent]['av_reliability'] for agent in agents]

fig, ax1 = plt.subplots()
x = np.arange(len(agents))
bar_width = 0.35

# Plotting the average intervention cost
ax1.bar(x - bar_width/2, av_intervention_cost, width=bar_width, color='b', alpha=0.7, label='Average Intervention Cost')
ax1.set_xlabel('Agents')
ax1.set_ylabel('Average Intervention Cost')
ax1.tick_params(axis='y', labelcolor='b')

# Create a second subplot for average reliability
ax2 = ax1.twinx()
ax2.bar(x + bar_width/2, av_reliability, width=bar_width, color='r', alpha=0.7, label='Average Reliability')
ax2.set_ylabel('Average Reliability')
ax2.tick_params(axis='y')

# Adjust the plot settings for each axis
ax1.set_xticks(x)
ax1.set_xticklabels(agents)
ax1.set_title(f"Results over {results['trials']} Trials")

# Show legends for each axis
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines1 + lines2, labels1 + labels2, loc='upper right')

plt.tight_layout()
plt.savefig("evaluation_results")
plt.show()


import sys

# Constants
SECONDS_PER_MINUTE = 60
SECONDS_PER_HOUR = 60 * SECONDS_PER_MINUTE
SECONDS_PER_DAY = 24 * SECONDS_PER_HOUR
BYTES_PER_KILOBYTE = 1024
BYTES_PER_MEGABYTE = 1024 * BYTES_PER_KILOBYTE
BYTES_PER_GIGABYTE = 1024 * BYTES_PER_MEGABYTE
BYTES_PER_TERABYTE = 1024 * BYTES_PER_GIGABYTE

# Bytes per block constants per chain.
BYTES_PER_BLOCK_CHAOS_REDIS = 15
BYTES_PER_BLOCK_CHAOS_NOMS = 6600

BYTES_PER_BLOCK_NDAU_REDIS = 98
BYTES_PER_BLOCK_NDAU_NOMS = 12000

# Tendermint bytes per block is chain-independent.
BYTES_PER_BLOCK_TENDERMINT = 3100

# Tendermint commits at least one block in this many seconds.
TENDERMINT_KEEPALIVE_SECONDS = 300
TENDERMINT_MIN_BLOCKS_PER_SECOND = 1.0 / TENDERMINT_KEEPALIVE_SECONDS

class Phase:
    """
    Each phase is a block rate with the total time to run at that rate.
    """

    def __init__(self, phase_num, blocks_per_second, total_seconds):
        self.phase_num = phase_num
        self.blocks_per_second = blocks_per_second
        self.total_seconds = total_seconds

    def __str__(self):
        return 'phase {0}:\n  blocks per second = {1}\n  total seconds     = {2}'.format(
            self.phase_num, self.blocks_per_second, self.total_seconds)

def print_usage_and_die(msg):
    """
    Print usage information for this tool and exits with a non-zero exit code.
    """

    print(msg)
    print('Usage:')
    print(
        '  ./predict.py {chaos|ndau} [[rate {bps|bpm|bph|bpd} duration {s|m|h|d}]...]'
    )
    print('Example: (ndau chain, idle for 1 day, then 2 blocks per second for 4 hours)')
    print('  ./predict.py ndau 0 bpm 1 d 2 bps 4 h')

    exit(1)

def format_byte_size(size):
    """
    Return a human-readable string for the given byte size.
    """

    if size >= BYTES_PER_TERABYTE:
        return '{0:.1f} TB'.format(float(size) / BYTES_PER_TERABYTE)
    if size >= BYTES_PER_GIGABYTE:
        return '{0:.1f} GB'.format(float(size) / BYTES_PER_GIGABYTE)
    if size >= BYTES_PER_MEGABYTE:
        return '{0:.1f} MB'.format(float(size) / BYTES_PER_MEGABYTE)
    if size >= BYTES_PER_KILOBYTE:
        return '{0:.1f} KB'.format(float(size) / BYTES_PER_KILOBYTE)
    return '{0} bytes'.format(size)

def format_seconds(seconds):
    """
    Return a human-readable string for the duration.
    """

    if seconds >= SECONDS_PER_DAY:
        return '{0:.2f} days'.format(seconds / SECONDS_PER_DAY)
    if seconds >= SECONDS_PER_HOUR:
        return '{0:.2f} hours'.format(seconds / SECONDS_PER_HOUR)
    if seconds >= SECONDS_PER_MINUTE:
        return '{0:.2f} minutes'.format(seconds / SECONDS_PER_MINUTE)
    return '{0:.2f} seconds'.format(seconds)

def parse_args():
    """
    Parse the command line arguments passed in to this tool.
    Return the bytes per block for redis and noms, as well as an array of phases to run through.
    """

    arg_index = 0
    arg_count = len(sys.argv)
    if arg_count < 2:
        print_usage_and_die('Must specify a chain')

    arg_index += 1
    chain = sys.argv[arg_index]
    if chain == 'chaos':
        redis_bpb = BYTES_PER_BLOCK_CHAOS_REDIS
        noms_bpb = BYTES_PER_BLOCK_CHAOS_NOMS
    elif chain == 'ndau':
        redis_bpb = BYTES_PER_BLOCK_NDAU_REDIS
        noms_bpb = BYTES_PER_BLOCK_NDAU_NOMS
    else:
        print_usage_and_die('Unsupported chain: ' + chain)

    arg_index += 1
    phase_arg_count = arg_count - arg_index
    if phase_arg_count & 3 != 0:
        print_usage_and_die('Each phase must have 4 arguments')

    phases = []
    phase_num = 1
    while arg_index < arg_count:
        blocks_per_second = float(sys.argv[arg_index])
        if blocks_per_second < 0:
            print_usage_and_die(
                'Unsupported phase block rate: ' + blocks_per_second)
        arg_index += 1

        rate = sys.argv[arg_index]
        if rate == 'bpm':
            blocks_per_second /= SECONDS_PER_MINUTE
        elif rate == 'bph':
            blocks_per_second /= SECONDS_PER_HOUR
        elif rate == 'bpd':
            blocks_per_second /= SECONDS_PER_DAY
        elif rate != 'bps':
            print_usage_and_die('Unsupported phase block rate scale: ' + rate)
        arg_index += 1

        total_seconds = float(sys.argv[arg_index])
        if blocks_per_second < 0:
            print_usage_and_die('Unsupported phase duration: ' + total_seconds)
        arg_index += 1

        duration = sys.argv[arg_index]
        if duration == 'm':
            total_seconds *= SECONDS_PER_MINUTE
        elif duration == 'h':
            total_seconds *= SECONDS_PER_HOUR
        elif duration == 'd':
            total_seconds *= SECONDS_PER_DAY
        elif duration != 's':
            print_usage_and_die(
                'Unsupported phase duration scale: ' + duration)
        arg_index += 1

        phases.append(Phase(phase_num, blocks_per_second, total_seconds))
        phase_num += 1

    return chain, redis_bpb, noms_bpb, BYTES_PER_BLOCK_TENDERMINT, phases

def main():
    chain, redis_bpb, noms_bpb, tendermint_bpb, phases = parse_args()

    print('predicting disk usage for the {0} chain over {1} phases'.format(
        chain, len(phases)))
    print('  redis      bytes per block = {0}'.format(redis_bpb))
    print('  noms       bytes per block = {0}'.format(noms_bpb))
    print('  tendermint bytes per block = {0}'.format(tendermint_bpb))
    print('  tendermint max seconds between blocks = {0}'.format(
        TENDERMINT_KEEPALIVE_SECONDS))
    for i in range(len(phases)):
        print(phases[i])

    blocks = 0
    redis_bytes = 0
    noms_bytes = 0
    tendermint_bytes = 0
    duration = 0

    # This loop doesn't worry about any duration overlap between phases, so it's
    # inaccurate in that way.  It's minor for any reasonable prediction query.
    for phase in phases:
        block_count = int(phase.blocks_per_second * phase.total_seconds)

        redis_bytes += block_count * redis_bpb
        noms_bytes += block_count * noms_bpb

        # Compute the extra keepalive blocks created by tendermint.
        # Only tendermint data files are affected by these empty blocks.
        if phase.blocks_per_second < TENDERMINT_MIN_BLOCKS_PER_SECOND:
            if phase.blocks_per_second == 0:
                block_count += int(TENDERMINT_MIN_BLOCKS_PER_SECOND *
                                   phase.total_seconds)
            else:
                # The keepalive timer starts over after any block is created.  So we count how
                # many keepalive blocks get created in between every normal phase block.  This
                # calculation might count one extra keepalive block at the end of the phase
                # duration.  It's minor enough that we ignore that case in favor of simplicity.
                phase_seconds_per_block = 1 / phase.blocks_per_second
                extra_blocks_per_block = \
                    int(phase_seconds_per_block / TENDERMINT_KEEPALIVE_SECONDS)

                # When there is an even multiple of keepalive blocks, we don't count the
                # last one, as if a normal phase block would be created at that time.
                if phase_seconds_per_block == int(phase_seconds_per_block) and \
                   phase_seconds_per_block % TENDERMINT_KEEPALIVE_SECONDS == 0:
                    extra_blocks_per_block -= 1

                block_count += block_count * extra_blocks_per_block

        tendermint_bytes += block_count * tendermint_bpb

        blocks += block_count
        duration += phase.total_seconds

    print('results:')
    print('  height     = {0:,} blocks'.format(blocks))
    print('  redis      = {0}'.format(format_byte_size(redis_bytes)))
    print('  noms       = {0}'.format(format_byte_size(noms_bytes)))
    print('  tendermint = {0}'.format(format_byte_size(tendermint_bytes)))
    print('  duration   = {0}'.format(format_seconds(duration)))

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-

variable = 13
variable = "hola"

"""Como se puede observar a la hora de ejecutar el código, es posible cambiar
el contenido de la varible de un entero a una cadena de texto ("hola" en este
caso)

La propiedad de Python que permite esto es que se trata de un lenguaje Dinámico
y al no tener que declarar el tipo de variable, el lenguaje identificará el
tipo de variable en función del dato que recibe"""


import datetime
import random
import string
import pandas as pd
import numpy as np
from collections import OrderedDict

class Transaction(object):
    """
    Class to store a single transaction record.
    """
    MINT_CSV_DATE_FORMAT = "%m/%d/%Y"
    DEFAULT_DATA = np.array(
        [
            ("Date", 'datetime64'),
            ("Description", 'object'),
            ("Description Original", 'object'),
            ("Amount", 'float64'),
            ("Transaction Type", 'object'),
            ("Category", 'object'),
            ("Account", 'object'),
            ("Labels", 'object'),
            ("Notes", 'object'),
        ]
    )

    def __init__(self):
        """
        Initialize an empty Transaction instance.
        """
        self.data = OrderedDict()
        for row in self.DEFAULT_DATA:
            self.data[row[0]] = [row[1], None]

    def __str__(self):
        signed_amount = self.amount
        date = self.date.strftime(self.MINT_CSV_DATE_FORMAT)
        if self.transaction_type == 'debit':
            signed_amount = signed_amount * -1
        ret = f"{date} | {self.description} | {signed_amount} | {self.category} | {self.account}"
        return ret

    def __eq__(self, other):
        """
        Compare self to another object to determine if they are equal.

        :param other: Another object (must be Transaction-like to be equal)
        :return: Boolean of whether the objects are equal
        """
        comparible_attributes = ["Date", "Description", "Description Original", "Amount", "Transaction Type",
                                 "Category", "Account", "Labels", "Notes"]
        equal = True
        for attr in comparible_attributes:
            if getattr(self, attr) == getattr(other, attr):
                continue
            else:
                equal = False
                break
        return equal

    @classmethod
    def sample_trx(cls, **kwargs):
        """
        Returns a sample transaction with semi-random date.

        Random data can be overridden through input arguemnts

        :return:
        """
        trx_desc = random.choice(string.ascii_uppercase)
        defaults = {
            "date": "{0}/{1}/{2}".format(random.randint(1, 12), random.randint(1, 28), random.randint(1985, 2016)),
            "description": "Trx {0}".format(trx_desc),
            "description_original": "Trx {0} Long".format(trx_desc),
            "amount": random.random() * 100.0,
            "transaction_type": "{0}".format(random.choice(['debit', 'credit'])),
            "category": "{0}".format(random.choice(['Groceries', 'Mortgage'])),
            "account": "{0}".format(random.choice(['Barclay', 'Amex'])),
            "labels": "label1",
            "notes": "A note",
        }
        defaults.update(**kwargs)
        print("using defaults:")
        print(defaults)
        return Transaction.from_dict(defaults)

    def header(self, separator=', ', fields=None):
        """
        Returns a csv string in the format of a standard csv header file.

        KeyError will be raised if a key does not exist

        :param keys: Ordered subset of keys to include in the returned string.
        :return: A csv-formatted string
        """
        if fields is None:
            fields = list(self.fields)

        return separator.join(fields)

    def to_list(self, fields=None):
        """
        Returns a list of the data in the Transaction.

        :param fields: (Optional) Defines an ordered list of the fields to return
        :return: List containing date requested
        """
        if fields is None:
            fields = self.fields

        data = [getattr(self, field) for field in fields]
        return data

    def to_csv(self, separator=', ', fields=None):
        """
        Convert the transaction to a csv formatted string.

        KeyError will be raised if a field does not exist in the transaction

        :param field: Ordered subset of fields to export in the returned string.
        :return: A csv-formatted string
        """
        #TODO: Cehck if KeyError raised on incorrect key
        if fields is None:
            fields = self.fields

        trx_as_list = [None] * len(fields)
        for i, field in enumerate(fields):
            trx_as_list[i] = getattr(self, field)
            if field == 'Date':
                trx_as_list[i] = trx_as_list[i].strftime(Transaction.MINT_CSV_DATE_FORMAT)
            if field == 'Amount':
                trx_as_list[i] = str(trx_as_list[i])

        return separator.join(trx_as_list)

    @classmethod
    def from_dict(cls, data_dict, fill_blanks=False):
        """
        Return a Transaction initialized from a dict containing data for all fields in Transaction.DEFAULT_DATA_MAP

        :param data_dict:
        :param fill_blanks: If True, fill all blank fields with None except date and amount, which is always required
        :return:
        """
        trx = cls()
        # if len(data_dict) != len(trx.fields):
        #     raise KeyError("Keys of data_dict ({0}) do not match fields of transaction ({1})".format(str(data_dict.keys()), str(trx.fields)))
        for k in trx.fields:
            try:
                setattr(trx, k, data_dict[k])
            except KeyError as e:
                if fill_blanks:
                    setattr(trx, k, None)
                else:
                    raise KeyError(f"data_dict ({str(data_dict.keys())}) is missing required keys for transaction ({str(trx.fields)})")
        return trx

    @classmethod
    def from_csv(cls, csv_string, separator=',', fields=None):
        """
        Initialize and return a Transaction instance from a comma separated string.

        Data from csv_string will be mapped to transaction fields according to fields input list.

        :param csv_string:
        :param separator: String that separates each value in the string (default: ',')
        :param fields: An ordered list of of the fields in the csv.  If None, default behaviour uses the order specified
                       by the transaction's fields attribute

        :return: A Transaction instance
        """
        trx = cls()
        if fields is None:
            fields = trx.fields

        # Split and strip any extra whitespace
        csv_list = [text.strip() for text in csv_string.split(separator)]

        for i in range(len(csv_list)):
            setattr(trx, fields[i], csv_list[i])

        return trx

    @property
    def date(self):
        """
        Getter for date property.

        :return: Datetime instance
        """
        return self.data['Date'][1]

    @date.setter
    def date(self, value):
        """
        Setter for date property.

        :return: None
        """
        # Parse (If necessary) and store date
        if not isinstance(value, datetime.datetime):
            value = datetime.datetime.strptime(value, Transaction.MINT_CSV_DATE_FORMAT)
        # If we get here, we have a datetime.datetime object
        self.data['Date'][1] = value  # No need to make a copy - datetime objects are immutable

    @property
    def amount(self):
        """
        Getter for amount property.

        :return: float
        """
        return self.data['Amount'][1]

    @amount.setter
    def amount(self, value):
        """
        Setter for amount property.

        :return: None
        """
        self.data['Amount'][1] = round(float(value), 2)

    @property
    def signed_amount(self):
        """
        Getter for amount with a +/- sign
        :return:
        """
        signed_amount = self.amount
        if self.transaction_type == 'debit':
            signed_amount = signed_amount * -1
        return signed_amount
    # # Not sure if I want to change the setting behaviour here... seems a little confusing and for little real gain
    # @signed_amount.setter
    # def signed_amount(self, value):
    #     """
    #     Setter for the property signed_amount, which automatically sets transaction_type accordingly
    #     :param value:
    #     :return:
    #     """
    #     if value >= 0:
    #         self.transaction_type = 'credit'
    #         self.amount = value
    #     else:
    #         self.transaction_type = 'debit'
    #         self.amount = -value

    @property
    def description(self):
        """
        Getter for property description, accessing data from internal Pandas Series
        """
        return self.data['Description'][1]

    @description.setter
    def description(self, value):
        """
        Setter for description property.

        :param value:
        :return:
        """
        self.data['Description'][1] = value

    @property
    def description_original(self):
        """
        Getter for property description_original, accessing data from internal Pandas Series
        """
        return self.data['Description Original'][1]

    @description_original.setter
    def description_original(self, value):
        """
        Setter for description_original property.

        :param value:
        :return:
        """
        self.data['Description Original'][1] = value

    @property
    def transaction_type(self):
        """
        Getter for property transaction_type, accessing data from internal Pandas Series
        """
        return self.data['Transaction Type'][1]

    @transaction_type.setter
    def transaction_type(self, value):
        """
        Setter for transaction_type property.

        :param value:
        :return:
        """
        self.data['Transaction Type'][1] = value

    @property
    def category(self):
        """
        Getter for property category, accessing data from internal Pandas Series
        """
        return self.data['Category'][1]

    @category.setter
    def category(self, value):
        """
        Setter for category property.

        :param value:
        :return:
        """
        self.data['Category'][1] = value

    @property
    def account(self):
        """
        Getter for property account, accessing data from internal Pandas Series
        """
        return self.data['Account'][1]

    @account.setter
    def account(self, value):
        """
        Setter for account property.

        :param value:
        :return:
        """
        self.data['Account'][1] = value

    @property
    def labels(self):
        """
        Getter for property labels, accessing data from internal Pandas Series
        """
        return self.data['Labels'][1]

    @labels.setter
    def labels(self, value):
        """
        Setter for labels property.

        :param value:
        :return:
        """
        self.data['Labels'][1] = value

    @property
    def notes(self):
        """
        Getter for property notes, accessing data from internal Pandas Series
        """
        return self.data['Notes'][1]

    @notes.setter
    def notes(self, value):
        """
        Setter for notes property.

        :param value:
        :return:
        """
        self.data['Notes'][1] = value

    @property
    def fields(self):
        """
        Getter for property notes, accessing data from internal Pandas Series
        """
        return list(self.data.keys())


import numpy as np
import os
import sounddevice as sd
import time
from django.conf import settings

# import hps_extractor as hpse
from real_time_pitch_extractor.hps_extractor import hps_pitch_detector

# General settings that can be changed by the user
sr = 44100  # sample frequency in Hz
window_size = 44100  # window size of the DFT in samples
window_step = 12000  # step size of window

def callback(indata, frames, time, status):
    """
    Callback function of the InputStream method.
    That's where the magic happens ;)
    """
    # define static variables
    if not hasattr(callback, "window_samples"):
        callback.window_samples = [0 for _ in range(window_size)]
    if not hasattr(callback, "noteBuffer"):
        callback.noteBuffer = ["1", "2"]

    if status:
        print(status)
        return
    if any(indata):
        callback.window_samples = np.concatenate(
            (callback.window_samples, indata[:, 0])
        )  # append new samples
        callback.window_samples = callback.window_samples[
            len(indata[:, 0]) :
        ]  # remove old samples

        pitch_detected, closest_pitch, closest_note, pitch_diff = hps_pitch_detector(
            callback.window_samples
        )  # extract pitch
        pitch_diff = round(pitch_diff, 1)
        callback.noteBuffer.insert(0, closest_note)  # note that this is a ringbuffer
        callback.noteBuffer.pop()

        os.system("cls" if os.name == "nt" else "clear")

        if callback.noteBuffer.count(callback.noteBuffer[0]) == len(
            callback.noteBuffer
        ):
            settings.PITCH_DETECTED = pitch_detected
            settings.CLOSEST_NOTE = closest_note
            settings.CLOSEST_PITCH = closest_pitch
            settings.PITCH_DIFF = pitch_diff
        else:
            settings.PITCH_DETECTED = 0
            settings.CLOSEST_NOTE = 0
            settings.CLOSEST_PITCH = 0
            settings.PITCH_DIFF = 0

    else:
        print("no input")

# Start the microphone input stream

def real_time(scallback):
    try:
        print("Starting HPS guitar tuner...")
        with sd.InputStream(
            channels=1, callback=callback, blocksize=window_step, samplerate=sr
        ):
            while settings.RECORD:
                scallback()
                time.sleep(0.2)

    except Exception as e:
        print(str(e))


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

"""
Created on Wed Dec 25 12:47:44 2019
@author: Amal Htait
"""

import json, os
from ast import literal_eval
import common as c

dirpath = os.getcwd()

def flatten_json(y):
    out = {}

    def flatten(x, name=''):
        if type(x) is dict:
            for a in x:
                flatten(x[a], name + a + '_')
        elif type(x) is list:
            i = 0
            for a in x:
                flatten(a, name + str(i) + '_')
                i += 1
        else:
            out[name[:-1]] = x

    flatten(y)
    return out

def deflat(a_dict):
    mylist = []
    for key, item in a_dict.items():
        mydict={}
        mydict["source"] = 'facebook_profile'
        myItem = c.cleanText(str(item))
        myKey = c.cleanText(str(key))
        mydict["term"] = str(myItem)
        mydict["key"] = str(myKey)
        mylist.append(mydict)
    return mylist

def fct():
    path = dirpath+"/script/dataSource/json-facebook_data/profile_information"
    print ("Facebook Perso - Fix")
    try:
        with open(path+'/'+'profile_information.json') as json_file:
            data = json.load(json_file)

        out = {}
        out = flatten_json(data)

        theList = deflat(out)

        docsStr = ""
        for i in range(len(theList)-1):
        	docsStr = docsStr + str(theList[i]) + "\n"
        docsStr = docsStr + str(theList[-1])

        docsStr = docsStr.replace("'",'"')

        f = open(path+"/profile_information_fixed.json", "w")
        f.write(docsStr)
        f.close()

    except:
        print("Error in FB Fix Perso")
        pass


# Description: Scrape and Summarize Animation articles on the Web
# find articles online about animation.
# websites that I found Cartoon Brew, NewYorker, and The Conversation

import nltk
from newspaper import Article

# url is a variable to store the article
url = 'https://www.cartoonbrew.com/business/everything-is-awesome-again-universal-and-lego-strike-exclusive-five-year-film-deal-190418.html'
#cartoon is also known as Cartoonbrew
cartoon = Article(url)

url = 'https://theconversation.com/tom-and-jerry-why-theyre-a-cat-and-mouse-double-act-for-the-ages-91762'
#con is also known as The Conversation
con = Article(url)

url = 'https://www.newyorker.com/magazine/2019/12/30/the-surprise-and-wonder-of-early-animation'
# new is also known as NewYorker
# varible to store the link
new = Article(url)

# download to get the article
cartoon.download()
con.download()
new.download()
#Then parse each article
cartoon.parse()
con.parse()
new.parse()

nltk.download('punkt')
cartoon.nlp()
con.nlp()
new.nlp()

#From Cartoon brew website
cartoon.title
# Get the authors who wrote the article
cartoon.authors
# get publish date
cartoon.publish_date
# finds the image and prints out a link to be shown on the Web
cartoon.top_image

#From Cartoon brew website
print(cartoon.title)
print(cartoon.authors) # Amid Amidi, Alex Dudok De Wit
print(cartoon.publish_date) #04-19-2018
print(cartoon.summary)

#From The Conversation
con.title
con.authors
con.publish_date

print(con.title)
print(con.authors) # Phillip Vaughan
print(con.publish_date) #03-14-2018
print(con.summary)

#From the NewYorker
new.title
new.authors
new.publish_date

print(new.title)
print(new.authors) #Richard Brod
print(new.publish_date) #12-30-2019
print(new.summary)

#unable to get funtion to work properly

def Articles(word):
    #number of words in each article
    art = 0

    for char in word:
        if char.lowercase() in "Article":
            art = art + 1
    return art
    userinput = input("Enter a word:")
    print(userinput)
    print(key(userinput))


import pickle as pkl
import os

def pkl_dump(obj, name):
    with open(name, 'wb') as f:
        pkl.dump(obj, f)

def pkl_load(name):
    obj = None
    with open(name, 'rb') as f:
        obj = pkl.load(f)
    assert(obj is not None)
    return obj

def vd_dump(vd, params, suffix='', in_result=True):
    result_dir = params.result_dir if in_result else params.data_dir
    if not os.path.isdir(result_dir):
        os.makedirs(result_dir)
    vd_name = os.path.join(result_dir, params.dataset_name + suffix + '.vd')
    pkl_dump(vd, vd_name)
    print("[vd_dump] Vehicle dataset saved as {}".format(vd_name))

def vd_load(params, suffix='', in_result=True):
    vd_name = os.path.join(params.result_dir if in_result else params.data_dir, params.dataset_name + suffix + '.vd')
    print("[vd_load] Loading {}".format(vd_name))
    if os.path.isfile(vd_name):
        vd = pkl_load(vd_name)
    else:
        vd = None
    return vd


'''
Pass your auth0 client ID and secret to authenticate and retrieve your token
'''

import requests

class AxilAuthorizer:

    def __init__(self, client_id, client_secret, client_token = None):
        self.client_id = client_id
        self.client_secret = client_secret
        self.token = client_token

        if client_token is None:
            self.authenticate()

    def authenticate(self):
        payload = {
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'audience': 'https://api.axil.ai',
            'grant_type': 'client_credentials'
        }

        headers = {'content-type': 'application/json'}

        req_resp = requests.post('https://axil.auth0.com/oauth/token',
                         headers=headers,
                         json=payload,
                         )
        if req_resp.status_code != requests.codes.ok:
            raise Exception(f'Error occurred getting Bearer token: {req_resp.reason}')

        self.token = req_resp.json()['access_token']


from random import randint
from matplotlib import pyplot

uno = 0
due = 0
tre = 0
quattro = 0
cinque = 0
sei = 0
sette = 0
otto = 0
nove = 0

for ciao in range(1000):
    a = randint(1,999999)
    a = str(a)
    a = a[0]
    a = int(a)

    if a == 1: uno +=1
    elif a == 2: due +=1
    elif a == 3: tre +=1
    elif a == 4: quattro +=1
    elif a == 5: cinque +=1
    elif a == 6: sei +=1
    elif a == 7: sette +=1
    elif a == 8: otto +=1
    elif a == 9: nove +=1

pyplot.bar([1,2,3,4,5,6,7,8,9],
           [uno,due,tre,quattro,cinque,sei,sette,otto,nove])
pyplot.show()


class Movie():
    def __init__(self, title, director, duration):
        self.title = title
        self.director = director
        self.duration = duration
        print('movie objesi oluşturuldu.')

    def __str__(self): # Special Metod
        return f"{self.title} by {self.director}"
    def __len__(self): # Special Metod
        return self.duration
    def __del__(self): # special Metod
        print('Movie object is deleted')
m1 = Movie('film adı','yönetmen adı',120)
print(m1)
string = 'Bu bir stringdir.'
print(str(string))
print(str(m1))
mylist = [1,2,3]
print(len(mylist))
print(len(m1))
del(m1) #object delete