import pytest
from koyo.json import read_json_data, write_json_data

@pytest.mark.parametrize("indent", (0, 4))
@pytest.mark.parametrize("data", ({"A": 1}, [0, 1, 2], ["A", "B", 3, 4]))
def test_read_write(tmpdir_factory, data, indent):
    path = str(tmpdir_factory.mktemp("json") / "test.json")
    write_json_data(path, data, indent=indent)

    _data = read_json_data(path)
    assert _data == data


from fastapi import APIRouter
import fastapi as _fastapi
from config.database import collection_name
from models.blockchain_model import Block
from schemas.blockchain_schemas import serializeList, serializeDict
import blockchain as _blockchain

router = APIRouter()
blockchain = _blockchain.Blockchain()

# endpoint to mine a block
@router.post("/mine_block/")
async def mine_block(data: str):
    cursor = collection_name.find( {},{"_id":0,"Index": 1, "Timestamp": 1,"Data":1,"Nonce":1,"Previous_hash":1})
    for document in cursor:
        blockchain.chain.append(document)

    block = blockchain.mine_block(data = data)
    _id =  collection_name.insert_one(block)
    blockchain.chain.clear()
    return serializeList(collection_name.find({"_id": _id.inserted_id}))

# endpoint to return the entire blockchain
@router.get("/blockchain/")
async def get_blockchain():
    cursor = collection_name.find( {},{"_id":0,"Index": 1, "Timestamp": 1,"Data":1,"Nonce":1,"Previous_hash":1})
    for document in cursor:
        blockchain.chain.append(document)

    chain = serializeList(collection_name.find())
    blockchain.chain.clear()
    return chain

# endpoint to see if the chain is valid
@router.get("/validate/")
async def is_blockchain_valid():
    if not blockchain.is_chain_valid():
        return _fastapi.HTTPException(status_code=400, detail="The blockchain is invalid")

    return blockchain.is_chain_valid()

# endpoint to return the last block
@router.get("/blockchain/last/")
async def previous_block():
    if not blockchain.is_chain_valid():
        return _fastapi.HTTPException(status_code=400, detail="The blockchain is invalid")

    return blockchain.get_previous_block()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import discord, pyfiglet, requests, json
from discord.ext import commands as commands
from bot import getembed

class crypto(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def crypto(self, ctx):
        args = ctx.message.content.split()
        config = json.loads(open("data/config.json","r").read())
        await ctx.message.delete()
        if len(args) == 1:
            text = f"""
**Invalid Parse Of Arguments**

``{config['prefix']}crypto`` **[type]**

To view a list of types, use ``{config['prefix']}listcrypto``"""
            embed = getembed(text)
            try:
                await ctx.send(embed=embed,delete_after=30)
            except:
                await ctx.send(f">>> {text}")
        else:
            cryptoType = str(args[1])
            if cryptoType == "BTC" or "ETH" or "XMR" or "XRP" or "DOGE" or "LINK" or "ATOM" or "COMP" or "LTC" or "BCH" or "DOT":
                r = requests.get("https://min-api.cryptocompare.com/data/price?fsym="+cryptoType+"&tsyms=USD,EUR,GBP")
                kekistan = r.json()
                gbp = kekistan['GBP']
                eur = kekistan['EUR']
                usd = kekistan['USD']
                embedic = discord.Embed(description=f'GBP: `£{str(gbp)}`\nEUR: `€{str(eur)}`\nUSD: `${str(usd)}`')
                if cryptoType == "BTC":
                    iconURL = "https://cdn.discordapp.com/attachments/813459313153671178/820118046073028638/bitcoin-225079_960_720.png"
                elif cryptoType == "ETH":
                    iconURL = "https://cdn.freebiesupply.com/logos/large/2x/ethereum-1-logo-png-transparent.png"
                elif cryptoType == "XMR":
                    iconURL = "https://cdn.freebiesupply.com/logos/large/2x/monero-logo-png-transparent.png"
                elif cryptoType == "XRP":
                    iconURL = "https://cdn.freebiesupply.com/logos/large/2x/ripple-2-logo-png-transparent.png"
                elif cryptoType == "DOGE":
                    iconURL = "https://cdn.coindoo.com/2019/10/dogecoin-logo.png"
                elif cryptoType == "LINK":
                    iconURL = "https://s2.coinmarketcap.com/static/img/coins/64x64/1975.png"
                elif cryptoType == "ATOM":
                    iconURL = "https://s2.coinmarketcap.com/static/img/coins/64x64/3794.png"
                elif cryptoType == "COMP":
                    iconURL = "https://s2.coinmarketcap.com/static/img/coins/64x64/5692.png"
                elif cryptoType == "LTC":
                    iconURL ="https://s2.coinmarketcap.com/static/img/coins/64x64/2.png"
                elif cryptoType == "BCH":
                    iconURL = "https://s2.coinmarketcap.com/static/img/coins/64x64/1831.png"
                elif cryptoType == "DOT":
                    iconURL = "https://s2.coinmarketcap.com/static/img/coins/64x64/6636.png"
                embedic.set_author(name=cryptoType, icon_url=iconURL)
                await ctx.send(embed=embedic)
            else:
                await ctx.send("Incorrect crypto entered. Use listcrypto for a list of cryptocurrencies you can check")

    @commands.command()
    async def listcrypto(self, ctx):
        await ctx.message.delete()
        embed = discord.Embed(description="The cryptocurrencies that you are able to check are `BTC`, `ETH`, `XMR`, `XRP`, `DOGE`, `LINK`, `ATOM`, `COMP`, `LTC`, `BCH`, `DOT`")
        await ctx.send(embed=embed)

def setup(bot):
    bot.add_cog(crypto(bot))


def squaresum(n):
    sum = 0
    for i in range(1,n+1):
        sum += i**2
    return sum

def sumsquare(n):
    sum = 0
    for i in range(1,n+1):
        sum+=i
    result = sum**2
    return result

diff = squaresum(100)-sumsquare(100)
print(diff)


import unittest
import time, sys
import io
import HTMLTestRunner
from lianxi import youjian
class test(unittest.TestCase):
        flag=True
        def test1(self):
            print("执行测试用例1");
            self.assertEqual(3,3);
        def test2(self):
            print("执行测试用例2");
            self.assertTrue(1==1);
        @unittest.skipIf(0,'跳过')
        def test3(self):
            print("执行测试用例3");
            self.assertGreater(5,4);
if __name__ == "__main__":
    suite=unittest.TestSuite()
    suite.addTest(test('test2'))
    suite.addTest(test('test3'))
    now = time.strftime("%Y-%m-%d_%H_%M_%S",time.localtime(time.time()))
    filename="C:\\Users\\Administrator\\PycharmProjects\\untitled2\\lianxi\\testresult.html"
    fp=open(filename,'wb')
    runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title=u"测试执行用例",description=u'用例执行情况')
    runner.run(suite)
    fp.close()
    youjian.send_email(filename)


from datetime import datetime

from django.db import models
from django.db.models.signals import post_save
from django.utils.dateformat import format as date_format

ROBOTER_POSITIONS = (
    ('engine1', 'Engine 1 (RShoulderPitch)'),
    ('engine2', 'Engine 2 (LShoulderPitch)'),
    ('engine3', 'Engine 3 (RShoulderRoll)'),
    ('engine4', 'Engine 4 (LShoudlerRoll)'),
    ('engine5', 'Engine 5 (RElbow)'),
    ('engine6', 'Engine 6 (LElbow)'),
    ('engine7', 'Engine 7 (RHipYaw)'),
    ('engine8', 'Engine 8 (LHipYaw)'),
    ('engine9', 'Engine 9 (RHipRoll)'),
    ('engine10', 'Engine 10 (LHipRoll)'),
    ('engine11', 'Engine 11 (RHipPitch)'),
    ('engine12', 'Engine 12 (LHipPitch)'),
    ('engine13', 'Engine 13 (RKnee)'),
    ('engine14', 'Engine 14 (LKnee)'),
    ('engine15', 'Engine 15 (RAnklePitch)'),
    ('engine16', 'Engine 16 (LAnklePitch)'),
    ('engine17', 'Engine 17 (RAnkleRoll)'),
    ('engine18', 'Engine 18 (LAnkleRoll)'),
    ('engine19', 'Engine 19 (HeadPan)'),
    ('engine20', 'Engine 20 (HeadTilt)'),
)

HARDWARE_TYPES = (
    ('engine', 'Engine'),
    ('battery', 'Battery'),
    ('charger', 'Charger'),
    ('smallpart','Small Part'),
)

HARDWARE_STATUSES = (
    ('ok', 'OK'),
    ('broken', 'Broken'),
    ('missing', 'Missing'),
)

def format_datetime(d):
    return date_format(d, 'Y-d-m H:i')

class Hardware(models.Model):
    id = models.CharField(max_length=10, primary_key=True)
    type = models.CharField(max_length=20, choices=HARDWARE_TYPES)
    status = models.CharField(max_length=20, choices=HARDWARE_STATUSES)
    note = models.TextField(blank=True)

    def __unicode__(self):
        return self.pk

class HardwareLog(models.Model):
    hardware = models.ForeignKey(Hardware)
    change_date = models.DateTimeField(default=datetime.today)
    status = models.CharField(max_length=20, choices=HARDWARE_STATUSES)
    note = models.TextField(blank=True)

    def __unicode__(self):
        return u'[{0}] {1}'.format(date_format(self.change_date,
            'SHORT_DATETIME_FORMAT'), unicode(self.hardware))

    class Meta:
        ordering = ('-change_date', )

class Roboter(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=30, unique=True)

    def __unicode__(self):
        return u'{0} ({1})'.format(self.name, self.pk)

class AttachedHardware(models.Model):
    hardware = models.ForeignKey(Hardware)
    roboter = models.ForeignKey(Roboter)
    position = models.CharField(max_length=20, choices=ROBOTER_POSITIONS)
    start_date = models.DateTimeField(default=datetime.today)
    end_date = models.DateTimeField(null=True, blank=True)

    def __unicode__(self):
        if self.end_date:
            return u'From {0} to {1}: {2} in {3}'.format(
                    format_datetime(self.start_date),
                    format_datetime(self.end_date),
                    unicode(self.hardware), unicode(self.roboter))
        else:
            return u'{0} in {1} (since {2})'.format(unicode(self.hardware),
                    unicode(self.roboter), format_datetime(self.start_date))

    class Meta:
        ordering = ('roboter', '-end_date')

def attached_hardware_cb(sender, instance, created, **kwargs):
    if created:
        attached_hw =AttachedHardware.objects.exclude(roboter=instance.roboter,
                position=instance.position).__filter(hardware=instance.hardware,
                end_date=None)
        for hw in attached_hw:
            hw.end_date = instance.start_date
            hw.save()

def changed_hardware_cb(sender, instance, created, **kwargs):
    HardwareLog.objects.create(hardware=instance, status=instance.status,
            note=instance.note)

post_save.connect(attached_hardware_cb, sender=AttachedHardware)
post_save.connect(changed_hardware_cb, sender=Hardware)


from socials.apps.bots.models import Bot
from socials.apps.site.utils import get_time_now_timestamp

def check_bot_need_awake(
    bot: Bot
) -> bool:
    if not bot.rest_until:
        return True
    # compare time
    wake_time = int(bot.rest_until.timestamp())
    time_now = get_time_now_timestamp()
    if time_now >= wake_time:
        return True
    return False

def process_bot_awake(
    bot: Bot
):
    need_awake = check_bot_need_awake(bot=bot)
    if not need_awake:
        if not bot.is_resting:
            bot.set_resting()
            bot.update_db()
        return
    if need_awake:
        # call awake bot
        bot.set_awake()
        # update bot in db
        bot.update_db()


# -*- coding: utf-8 -*-
# Requer Python 3.7
# Sources: https://gist.github.com/HaiyangXu/ec88cbdce3cdbac7b8d5
#          https://stackoverflow.com/questions/39801718/how-to-run-a-http-server-which-serves-a-specific-path
#          https://gist.github.com/aallan/9416763d42534ae99f6f0228f54160c9

import http.server
from http.server import HTTPServer, BaseHTTPRequestHandler
import socketserver

PORT = 8080
DIRECTORY = "html"

class Handler(http.server.SimpleHTTPRequestHandler):
	def __init__(self, *args, **kwargs):
		super().__init__(*args, directory=DIRECTORY, **kwargs)

	def end_headers(self):
		self.send_my_headers()
		http.server.SimpleHTTPRequestHandler.end_headers(self)

	def send_my_headers(self):
		self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
		self.send_header("Pragma", "no-cache")
		self.send_header("Expires", "0")

Handler.extensions_map = {
	'.manifest': 'text/cache-manifest',
	'.html': 'text/html',
	'.png': 'image/png',
	'.jpg': 'image/jpg',
	'.svg':	'image/svg+xml',
	'.css':	'text/css',
	'.js':	'application/x-javascript',
	'': 'application/octet-stream', # Default
}

httpd = socketserver.TCPServer(("", PORT), Handler)

print("serving at port", PORT)
httpd.serve_forever()


import torch
from torch import nn
from torch.nn import init
from torch.utils.data import DataLoader
from overrides import overrides
import numpy as np

from models.BaseModel import BaseModel

class SKLRModel(BaseModel):
    def __init__(self, model_name: str, lr: float, device: torch.device):
        super().__init__(model_name, torch.device('cpu'))
        # from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
        from sklearn.linear_model import LogisticRegression
        self.lda_cls = LogisticRegression()

    @overrides
    def train_epoch(self, epoch_num: int, train_loader: DataLoader):
        x = torch.flatten(train_loader.dataset.data, 1).numpy()
        y = train_loader.dataset.targets.numpy()
        self.lda_cls.fit(x, y)

    @overrides
    def test_epoch(self, epoch_num: int, test_loader: DataLoader):
        x = torch.flatten(test_loader.dataset.data, 1).numpy()
        y = test_loader.dataset.targets.numpy()
        predict_result: np.ndarray = self.lda_cls.predict(x)
        results: np.ndarray = predict_result == y
        return sum(results) / len(results)

    @overrides
    def run_epochs(self, epochs: int, train_loader: DataLoader, test_loader: DataLoader):
        self.train_epoch(0, train_loader)
        acc = self.test_epoch(0, test_loader)
        print(acc)


from kuanke.wizard import *
from jqdata import *
import numpy as np
import pandas as pd
import talib
import datetime
import uuid
import requests
import json

## 初始化函数，设定要操作的股票、基准等等
def initialize(context):
    # 设定基准
    set_benchmark('000300.XSHG')
    # 设定滑点
    set_slippage(FixedSlippage(0))
    # True为开启动态复权模式，使用真实价格交易
    set_option('use_real_price', True)
    # 设定成交量比例
    set_option('order_volume_ratio', 1)
    # 股票类交易手续费是：买入时佣金万分之三，卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
    set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')

def after_code_changed(context):
    log.info('=============================================')
    # 个股最大持仓比重
    g.security_max_proportion = 1
    # 选股频率
    g.check_stocks_refresh_rate = 1
    # 买入频率
    g.buy_refresh_rate = 10
    # 卖出频率
    g.sell_refresh_rate = 10
    # 最大建仓数量
    g.max_hold_stocknum = 2

    # 选股频率计数器
    g.check_stocks_days = 0
    # 买卖交易频率计数器
    g.buy_trade_days=0
    g.sell_trade_days=0
    # 获取未卖出的股票
    g.open_sell_securities = []
    # 卖出股票的dict
    g.selled_security_list={}
    # 大盘风险等级
    g.risk_type = 'low'

    # 股票筛选初始化函数
    jw_check_stocks_initialize()
    # 股票筛选排序初始化函数
    jw_jw_check_stocks_sort_initialize()
    # 出场初始化函数
    jw_sell_initialize()
    # 入场初始化函数
    jw_buy_initialize()
    # 风控初始化函数
    jw_jw_risk_management_initialize()

    # 关闭提示
    log.set_level('order', 'info')

    # 运行函数
    unschedule_all()
    run_daily(jw_sell_every_day,'10:00') #卖出未卖出成功的股票
    run_daily(jw_risk_management, '10:00') #风险控制
    run_daily(jw_check_stocks, '10:00') #选股
    run_daily(jw_trade, '10:00') #交易
    run_daily(jw_selled_security_list_count, '10:00') #卖出股票日期计数

## 股票筛选初始化函数
def jw_check_stocks_initialize():
    # 是否过滤停盘
    g.filter_paused = True
    # 是否过滤退市
    g.filter_delisted = True
    # 是否只有ST
    g.only_st = False
    # 是否过滤ST
    g.filter_st = True
    # 股票池
    g.security_universe_index = ["000300.XSHG"]
    g.security_universe_user_securities = []
    # 行业列表
    g.industry_list = ["801010","801020","801030","801040","801050","801080","801110","801120","801130","801140","801150","801160","801170","801180","801200","801210","801230","801710","801720","801730","801740","801750","801760","801770","801780","801790","801880","801890"]
    # 概念列表
    g.concept_list = []

## 股票筛选排序初始化函数
def jw_jw_check_stocks_sort_initialize():
    # 总排序准则： desc-降序、asc-升序
    g.check_out_lists_ascending = 'desc'

## 出场初始化函数
def jw_sell_initialize():
    # 设定是否卖出buy_lists中的股票
    g.sell_will_buy = True

    # 固定出仓的数量或者百分比
    g.sell_by_amount = None
    g.sell_by_percent = None

## 入场初始化函数
def jw_buy_initialize():
    # 是否可重复买入
    g.filter_holded = False

    # 委托类型
    g.order_style_str = 'by_cap_mean'
    g.order_style_value = 100

## 风控初始化函数
def jw_jw_risk_management_initialize():
    # 策略风控信号
    g.jw_risk_management_signal = True

    # 策略当日触发风控清仓信号
    g.daily_jw_risk_management = True

    # 单只最大买入股数或金额
    g.max_buy_value = None
    g.max_buy_amount = None

## 卖出未卖出成功的股票
def jw_sell_every_day(context):
    g.open_sell_securities = list(set(g.open_sell_securities))
    open_sell_securities = [s for s in context.portfolio.positions.keys() if s in g.open_sell_securities]
    if len(open_sell_securities)>0:
        for stock in open_sell_securities:
            order_target_value(stock, 0)
    g.open_sell_securities = [s for s in g.open_sell_securities if s in context.portfolio.positions.keys()]
    return

## 风控
def jw_risk_management(context):
    ### _风控函数筛选-开始 ###
    ### _风控函数筛选-结束 ###
    return

## 股票筛选
def jw_check_stocks(context):
    if g.check_stocks_days%g.check_stocks_refresh_rate != 0:
        # 计数器加一
        g.check_stocks_days += 1
        return
    # 股票池赋值
    g.check_out_lists = jw_get_security_universe(context, g.security_universe_index, g.security_universe_user_securities)
    # 行业过滤
    g.check_out_lists = jw_industry_filter(context, g.check_out_lists, g.industry_list)
    # 概念过滤
    g.check_out_lists = jw_concept_filter(context, g.check_out_lists, g.concept_list)
    # 过滤ST股票
    g.check_out_lists = jw_st_filter(context, g.check_out_lists)
    # 过滤退市股票
    g.check_out_lists = jw_delisted_filter(context, g.check_out_lists)
    # 财务筛选
    g.check_out_lists = jw_financial_statements_filter(context, g.check_out_lists)
    # 行情筛选
    g.check_out_lists = jw_situation_filter(context, g.check_out_lists)
    # 技术指标筛选
    g.check_out_lists = jw_technical_indicators_filter(context, g.check_out_lists)
    # 形态指标筛选函数
    g.check_out_lists = jw_technical_indicators_filter(context, g.check_out_lists)
    # 其他筛选函数
    g.check_out_lists = jw_technical_indicators_filter(context, g.check_out_lists)

    # 排序
    input_dict = jw_get_jw_check_stocks_sort_input_dict()
    g.check_out_lists = jw_check_stocks_sort(context,g.check_out_lists,input_dict,g.check_out_lists_ascending)

    # 计数器归一
    g.check_stocks_days = 1
    return

## 交易函数
def jw_trade(context):
   # 初始化买入列表
    buy_lists = []

    # 买入股票筛选
    if g.buy_trade_days%g.buy_refresh_rate == 0:
        # 获取 buy_lists 列表
        buy_lists = g.check_out_lists
        # 过滤ST股票
        buy_lists = jw_st_filter(context, buy_lists)
        # 过滤停牌股票
        buy_lists = jw_paused_filter(context, buy_lists)
        # 过滤退市股票
        buy_lists = jw_delisted_filter(context, buy_lists)
        # 过滤涨停股票
        buy_lists = jw_high_limit_filter(context, buy_lists)

        ### _入场函数筛选-开始 ###
        ### _入场函数筛选-结束 ###

    # 卖出操作
    if g.sell_trade_days%g.sell_refresh_rate != 0:
        # 计数器加一
        g.sell_trade_days += 1
    else:
        # 卖出股票
        jw_sell(context, buy_lists)
        # 计数器归一
        g.sell_trade_days = 1

    # 买入操作
    if g.buy_trade_days%g.buy_refresh_rate != 0:
        # 计数器加一
        g.buy_trade_days += 1
    else:
        # 卖出股票
        jw_buy(context, buy_lists)
        # 计数器归一
        g.buy_trade_days = 1

## 卖出股票日期计数
def jw_selled_security_list_count(context):
    g.daily_jw_risk_management = True
    if len(g.selled_security_list)>0:
        for stock in g.selled_security_list.keys():
            g.selled_security_list[stock] += 1

##################################  选股函数群 ##################################

## 财务指标筛选函数
def jw_financial_statements_filter(context, security_list):
    ### _财务指标筛选函数-开始 ###
    security_list = financial_data_filter_dayu(security_list, income.net_profit, 1e8)
    security_list = financial_data_filter_qujian(security_list, indicator.inc_net_profit_year_on_year, (5,100))
    security_list = financial_data_filter_qujian(security_list, valuation.pe_ratio, (10,100))
    security_list = financial_data_filter_qujian(security_list, indicator.roe, (2,30))
    security_list = financial_data_filter_qujian(security_list, indicator.gross_profit_margin, (5,100))
    security_list = financial_data_filter_qujian(security_list, valuation.market_cap, (100,10000))
    security_list = financial_data_filter_qujian(security_list, balance.total_liability / balance.total_assets, (0.1,1.0))
    ### _财务指标筛选函数-结束 ###

    # 返回列表
    return security_list

## 行情筛选函数
def jw_situation_filter(context, security_list):
    ### _行情筛选函数-开始 ###
    security_list = [security for security in security_list if n_day_chg_dayu(security, 90, 0.05)]
    ### _行情筛选函数-结束 ###

    # 返回列表
    return security_list

## 技术指标筛选函数
def jw_technical_indicators_filter(context, security_list):
    ### _技术指标筛选函数-开始 ###
    ### _技术指标筛选函数-结束 ###

    # 返回列表
    return security_list

## 形态指标筛选函数
def jw_technical_indicators_filter(context, security_list):
    ### _形态指标筛选函数-开始 ###
    ### _形态指标筛选函数-结束 ###

    # 返回列表
    return security_list

## 其他方式筛选函数
def jw_technical_indicators_filter(context, security_list):
    ### _其他方式筛选函数-开始 ###
    security_list = [security for security in security_list if jw_check_for_benchmark(security)]
    ### _其他方式筛选函数-结束 ###

    # 返回列表
    return security_list

# 获取选股排序的 input_dict
def jw_get_jw_check_stocks_sort_input_dict():
    input_dict = {
        indicator.inc_net_profit_year_on_year:('desc',1),
        indicator.roe:('desc',1),
        indicator.gross_profit_margin:('desc',1),
        balance.total_liability/balance.total_assets:('asc',1),
        valuation.market_cap:('desc',3.5),
        }
    # 返回结果
    return input_dict

##################################  交易函数群 ##################################
# 交易函数 - 出场
def jw_sell(context, buy_lists):
    # 获取 sell_lists 列表
    #init_sl = context.portfolio.positions.keys()
    sell_lists = context.portfolio.positions.keys()

    # 判断是否卖出buy_lists中的股票
    #if not g.sell_will_buy:
    sell_lists = [security for security in sell_lists if security not in buy_lists]

    # 卖出股票
    #if len(sell_lists)>0:
    for stock in sell_lists:
        #sell_by_amount_or_percent_or_none(context,stock, g.sell_by_amount, g.sell_by_percent, g.open_sell_securities)
        order_target_value(stock, 0, MarketOrderStyle())

    return

# 交易函数 - 入场
def jw_buy(context, buy_lists):
    dynamic_turnover = jw_get_dynamic_turnover(context, buy_lists[:10])
    buy_lists = buy_lists[:g.max_hold_stocknum]

    # 买入股票
    for stock in buy_lists:
        order_target_value(stock, context.portfolio.total_value / g.max_hold_stocknum * dynamic_turnover, MarketOrderStyle())

def jw_get_dynamic_turnover(context, benchmark_list):
    count = 0
    periods = [15, 30, 45, 60, 90]

    high_risk_count = 0
    low_risk_count = 0
    for period in periods:
        for benchmark in benchmark_list:
            if n_day_chg_dayu(benchmark, 90, 0.5):
                high_risk_count += 1
            if n_day_chg_xiaoyu(benchmark, 90, -0.1):
                low_risk_count += 1

    if g.risk_type == 'low':
        if high_risk_count > len(benchmark_list) * 0.66:
            g.risk_type = 'high'
    elif g.risk_type == 'high':
        if low_risk_count > len(benchmark_list) * 0.66:
            g.risk_type = 'low'

    # TODO test
    #if context.current_dt <= string_to_datetime('2015-01-01 00:00:00'):
    #    benchmark_list = ['000300.XSHG', '000001.XSHG', '399001.XSHE', '600519.XSHG', '600276.XSHG', '600887.XSHG']
    #else:
    #    benchmark_list = ['600519.XSHG', '000858.XSHE', '600276.XSHG', '000661.XSHE', '603288.XSHG', '600887.XSHG', '000333.XSHE', '000651.XSHE']

    for period in periods:
        for benchmark in benchmark_list:
            if g.risk_type == 'high':
                if RSI_judge_qujian(benchmark, (55, 99), period):
                     count += 1
            else:
                if RSI_judge_qujian(benchmark, (50, 99), period):
                     count += 1

    return count / len(periods) / max(1, len(benchmark_list))

###################################  公用函数群 ##################################
## 排序
def jw_check_stocks_sort(context,security_list,input_dict,ascending='desc'):
    if (len(security_list) == 0) or (len(input_dict) == 0):
        return security_list
    else:
        # 生成 key 的 list
        idk = list(input_dict.keys())
        # 生成矩阵
        a = pd.DataFrame()
        for i in idk:
            b = get_sort_dataframe(security_list, i, input_dict[i])
            a = pd.concat([a,b],axis = 1)
        # 生成 score 列
        a['score'] = a.sum(1,False)
        # 根据 score 排序
        if ascending == 'asc':# 升序
            if hasattr(a, 'sort'):
                a = a.sort(['score'],ascending = True)
            else:
                a = a.sort_values(['score'],ascending = True)
        elif ascending == 'desc':# 降序
            if hasattr(a, 'sort'):
                a = a.sort(['score'],ascending = False)
            else:
                a = a.sort_values(['score'],ascending = False)
        # 返回结果
        return list(a.index)

## 过滤同一标的继上次卖出N天不再买入
def jw_filter_n_tradeday_not_buy(security, n=0):
    try:
        if (security in g.selled_security_list.keys()) and (g.selled_security_list[security]<n):
            return False
        return True
    except:
        return True

## 是否可重复买入
def jw_holded_filter(context,security_list):
    if not g.filter_holded:
        security_list = [stock for stock in security_list if stock not in context.portfolio.positions.keys()]
    # 返回结果
    return security_list

## 卖出股票加入dict
def jw_selled_security_list_dict(context,security_list):
    selled_sl = [s for s in security_list if s not in context.portfolio.positions.keys()]
    if len(selled_sl)>0:
        for stock in selled_sl:
            g.selled_security_list[stock] = 0

## 过滤停牌股票
def jw_paused_filter(context, security_list):
    if g.filter_paused:
        current_data = get_current_data()
        security_list = [stock for stock in security_list if not current_data[stock].paused]
    # 返回结果
    return security_list

## 过滤退市股票
def jw_delisted_filter(context, security_list):
    if g.filter_delisted:
        current_data = get_current_data()
        security_list = [stock for stock in security_list if not (('退' in current_data[stock].name) or ('*' in current_data[stock].name))]
    # 返回结果
    return security_list

## 过滤ST股票
def jw_st_filter(context, security_list):
    if g.only_st:
        current_data = get_current_data()
        security_list = [stock for stock in security_list if current_data[stock].is_st]
    else:
        if g.filter_st:
            current_data = get_current_data()
            security_list = [stock for stock in security_list if not current_data[stock].is_st]
    # 返回结果
    return security_list

# 过滤涨停股票
def jw_high_limit_filter(context, security_list):
    current_data = get_current_data()
    security_list = [stock for stock in security_list if not (current_data[stock].day_open >= current_data[stock].high_limit)]
    # 返回结果
    return security_list

# 获取股票股票池
def jw_get_security_universe(context, security_universe_index, security_universe_user_securities):
    temp_index = []
    for s in security_universe_index:
        if s == 'all_a_securities':
            temp_index += list(get_all_securities(['stock'], context.current_dt.date()).index)
        else:
            temp_index += get_index_stocks(s)
    for x in security_universe_user_securities:
        temp_index += x
    return  sorted(list(set(temp_index)))

# 行业过滤
def jw_industry_filter(context, security_list, industry_list):
    if len(industry_list) == 0:
        # 返回股票列表
        return security_list
    else:
        securities = []
        for s in industry_list:
            temp_securities = get_industry_stocks(s)
            securities += temp_securities
        security_list = [stock for stock in security_list if stock in securities]
        # 返回股票列表
        return security_list

# 概念过滤
def jw_concept_filter(context, security_list, concept_list):
    if len(concept_list) == 0:
        return security_list
    else:
        securities = []
        for s in concept_list:
            temp_securities = get_concept_stocks(s)
            securities += temp_securities
        security_list = [stock for stock in security_list if stock in securities]
        # 返回股票列表
        return security_list

#自定义函数
def jw_check_for_benchmark(security):
    #return True
    # 沪深300在2005年6月才上市
    '''
    periods = [15, 17, 20, 25, 30, 37, 45, 55, 70, 90]
    for period in periods:
        if RSI_judge_qujian('000001.XSHG', (50,99), period):
            return True
    return False
    '''

    return RSI_judge_qujian('000300.XSHG', (50,99), 90)
    #if not RSI_judge_qujian(security, (50,99), 60):
    #    return RSI_judge_qujian('000001.XSHG', (50,99), 60)

    #return True


#!/usr/bin/env python

"""rallpacks_cable_hhchannel.py:

Last modified: Wed May 21, 2014  09:51AM

"""

__author__           = "Dilawar Singh"
__copyright__        = "Copyright 2013, NCBS Bangalore"
__credits__          = ["NCBS Bangalore", "Bhalla Lab"]
__license__          = "GPL"
__version__          = "1.0.0"
__maintainer__       = "Dilawar Singh"
__email__            = "dilawars@iitb.ac.in"
__status__           = "Development"

import sys
sys.path.append('../../../python')
import moose
from moose import utils

import os
import numpy as np
import matplotlib.pyplot as plt
import compartment as comp

EREST_ACT = -70e-3
per_ms = 1e3
dt = 5e-5

cable = []

def createChannel(species, path, **kwargs):
    """Create a channel """
    A = kwargs['A']
    B = kwargs['B']
    V0 = kwargs['V0']

def create_na_chan(parent='/library', name='na', vmin=-110e-3, vmax=50e-3, vdivs=3000):
    """Create a Hodhkin-Huxley Na channel under `parent`.

    vmin, vmax, vdivs: voltage range and number of divisions for gate tables

    """
    na = moose.HHChannel('%s/%s' % (parent, name))
    na.Xpower = 3
    na.Ypower = 1
    v = np.linspace(vmin, vmax, vdivs+1) - EREST_ACT
    m_alpha = per_ms * (25 - v * 1e3) / (10 * (np.exp((25 - v * 1e3) / 10) - 1))
    m_beta = per_ms * 4 * np.exp(- v * 1e3/ 18)
    m_gate = moose.element('%s/gateX' % (na.path))
    m_gate.min = vmin
    m_gate.max = vmax
    m_gate.divs = vdivs
    m_gate.tableA = m_alpha
    m_gate.tableB = m_alpha + m_beta
    h_alpha = per_ms * 0.07 * np.exp(-v / 20e-3)
    h_beta = per_ms * 1/(np.exp((30e-3 - v) / 10e-3) + 1)
    h_gate = moose.element('%s/gateY' % (na.path))
    h_gate.min = vmin
    h_gate.max = vmax
    h_gate.divs = vdivs
    h_gate.tableA = h_alpha
    h_gate.tableB = h_alpha + h_beta
    return na

def create_k_chan(parent='/library', name='k', vmin=-120e-3, vmax=40e-3, vdivs=3000):
    """Create a Hodhkin-Huxley K channel under `parent`.

    vmin, vmax, vdivs: voltage range and number of divisions for gate tables

    """
    k = moose.HHChannel('%s/%s' % (parent, name))
    k.Xpower = 4
    v = np.linspace(vmin, vmax, vdivs+1) - EREST_ACT
    n_alpha = per_ms * (10 - v * 1e3)/(100 * (np.exp((10 - v * 1e3)/10) - 1))
    n_beta = per_ms * 0.125 * np.exp(- v * 1e3 / 80)
    n_gate = moose.element('%s/gateX' % (k.path))
    n_gate.min = vmin
    n_gate.max = vmax
    n_gate.divs = vdivs
    n_gate.tableA = n_alpha
    n_gate.tableB = n_alpha + n_beta
    return k

def create_hhcomp(parent='/library', name='hhcomp', diameter=1e-6, length=1e-6):
    """Create a compartment with Hodgkin-Huxley type ion channels (Na and
    K).

    Returns a 3-tuple: (compartment, nachannel, kchannel)

    """
    compPath = '{}/{}'.format(parent, name)
    mc = comp.MooseCompartment( compPath, length, diameter, {})
    c = mc.mc_
    sarea = mc.surfaceArea

    if moose.exists('/library/na'):
        moose.copy('/library/na', c.path, 'na')
    else:
        create_na_chan(parent = c.path)
    na = moose.element('%s/na' % (c.path))

    # Na-conductance 120 mS/cm^2
    na.Gbar = 120e-3 * sarea * 1e4
    na.Ek = 115e-3 + EREST_ACT

    moose.connect(c, 'channel', na, 'channel')
    if moose.exists('/library/k'):
        moose.copy('/library/k', c.path, 'k')
    else:
        create_k_chan(parent = c.path)

    k = moose.element('%s/k' % (c.path))
    # K-conductance 36 mS/cm^2
    k.Gbar = 36e-3 * sarea * 1e4
    k.Ek = -12e-3 + EREST_ACT
    moose.connect(c, 'channel', k, 'channel')
    return (c, na, k)

def makeCable( nsegs = 10 ):
    global cable
    moose.Neutral('/cable')
    for i in range( nsegs ):
        compName = 'hhcomp{}'.format(i)
        hhComp = create_hhcomp( '/cable', compName )
        cable.append( hhComp[0] )

    # connect the cable.
    for i, hhc in enumerate(cable[0:-1]):
        hhc.connect('axial', cable[i+1], 'raxial')

def setupDUT():
    global cable
    comp = cable[0]
    data = moose.Neutral('/data')
    pg = moose.PulseGen('/data/pg')
    pg.firstWidth = 25e-3
    pg.firstLevel = 1e-10
    moose.connect(pg, 'output', comp, 'injectMsg')
    setupClocks()

def setupClocks( ):
    moose.setClock(0, dt)
    moose.setClock(1, dt)

def setupSolver( hsolveDt = 5e-5 ):
    hsolvePath = '/hsolve'
    hsolve = moose.HSolve( hsolvePath )
    hsolve.dt = hsolveDt
    hsolve.target = '/cable'
    moose.useClock(1, hsolvePath, 'process')

def simulate( runTime = 25e-3 ):
    """ Simulate the cable """
    moose.useClock(0, '/cable/##', 'process')
    moose.useClock(0, '/cable/##', 'init')
    moose.useClock(1, '/##', 'process')
    moose.reinit()
    setupSolver()
    #utils.verify()
    moose.start( runTime )

if __name__ == '__main__':
    global cable
    global dt
    makeCable( 1000 )
    setupClocks()
    setupDUT()
    table0 = utils.recordAt( '/table0', cable[0], 'vm')
    table1 = utils.recordAt( '/table1', cable[-1], 'vm')
    simulate( 25e-3 )
    utils.plotTables( [ table0, table1 ], file = 'rallpack3.png', xscale = dt )
    #test_hhcomp()


#encoding:utf8
__author__ = 'gold'

'''
Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False.

Note: Do not use any built-in library function such as sqrt.

Example 1:

Input: 16
Output: true
Example 2:

Input: 14
Output: false
'''

class Solution:
    def isPerfectSquare(self, num):
        """
        :type num: int
        :rtype: bool
        """

        if num <= 1:
            return True

        left = 1
        right = num // 2
        while left <= right:
            mid = (left + right) // 2
            product = mid * mid
            if product == num:
                return True
            if product > num:
                right = mid - 1
            else:
                left += 1

        if right * right != num:
            return False
        return True

if __name__ == '__main__':
    print(Solution().isPerfectSquare(12))
    print(Solution().isPerfectSquare(16))
    print(Solution().isPerfectSquare(4))


import math

TYPE = 'TYPE'
PATH = 'PATH'
VALUE = 'VALUE'

MAGNITUDE = -4
THRESHOLD = 1

class Diff(object):
    def __init__(self, first, second, with_values=False):
        self.difference = []
        self.seen = []
        self.check(first, second, with_values=with_values)

    def check(self, first, second, path='', with_values=False):
        if with_values and second is not None:
            if not isinstance(first, type(second)):
                message = '{} - {}, {}'.format(path, type(first).__name__, type(second).__name__)
                self.save_diff(message, TYPE)

        if isinstance(first, dict):
            for key in first:
                # the first part of path must not have trailing dot.
                if len(path) == 0:
                    new_path = key
                else:
                    new_path = "{}.{}".format(path, key)

                if isinstance(second, dict):
                    if key in second:
                        sec = second[key]
                    else:
                        #  there are key in the first, that is not presented in the second
                        self.save_diff(new_path, PATH)

                        # prevent further values checking.
                        sec = None

                    # recursive call
                    if sec is not None:
                        self.check(first[key], sec, path=new_path, with_values=with_values)
                else:
                    # second is not dict. every key from first goes to the difference
                    self.save_diff(new_path, PATH)
                    self.check(first[key], second, path=new_path, with_values=with_values)

        # if object is list, loop over it and check.
        elif isinstance(first, list):
            first = self.retrieve_list(first)
            for (index, item) in enumerate(first):
                new_path = '{}.{}'.format(path, index)
                # try to get the same index from second
                sec = None
                if second is not None:
                    try:
                        second = self.retrieve_list(second)
                        sec = second[index]
                    except Exception as e:
                        # goes to difference
                        self.save_diff('{} # {}'.format(new_path, type(item).__name__), TYPE)

                # recursive call
                self.check(first[index], sec, path=new_path, with_values=with_values)

        # not list, not dict. check for equality (only if with_values is True) and return.
        else:
            if with_values and second is not None:
                if self.is_changed(first, second):
                    self.save_diff('{} # {} | {}'.format(path, first, second), VALUE)
            return

    def save_diff(self, diff_message, type_):
        if diff_message not in self.difference:
            self.seen.append(diff_message)
            self.difference.append((type_, diff_message))

    def retrieve_list(self, _list):
        for item in _list:
            if isinstance(item, list) or isinstance(item, dict):
                return _list
        return sorted(_list)

    def is_changed(self, first, second):
        try:
            first = float(first)
            second = float(second)
            if first == second:
                return False

        except Exception as e:
            return first != second

        lg_first = round(math.log(first, 10))
        lg_second = round(math.log(second, 10))

        if lg_first < MAGNITUDE and lg_second < MAGNITUDE:
            check = round(abs(math.log(first/second, 10)))
            return True if check >= THRESHOLD else False

        if lg_first == lg_second:
            check = round(math.log(abs(first-second), 10))
            return True if check > MAGNITUDE else False

        return first != second


"""A simple Discord bot that fetches the top 5 stories from Hacker News."""
import os
import requests
import discord
from discord.ext import commands

def get_top_stories():
    """Request the latest 5 stories from hackernews.

    Args:
        None

    Returns:
        response (json): Latest 5 stories
    """
    url = "https://hacker-news.firebaseio.com/v0/topstories.json"
    response = requests.get(url, timeout=5)
    if response.status_code == 200:
        return response.json()[:5]  # Return only the top 5 stories
    else:
        return None

def get_story_details(story_id):
    """Request the details from the story id from hackernews.

    Args:
        story_id (int)

    Returns:
        response (json): Details of a story
    """
    url = f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json"
    response = requests.get(url, timeout=5)
    if response.status_code == 200:
        return response.json()
    else:
        return None

# Bot instantiation
intents = discord.Intents.default()
intents.messages = True
intents.reactions = True

bot = commands.Bot(command_prefix="!", intents=intents)

# Bot commands
@bot.command(name="ping")
async def ping(ctx):
    """Ping the bot to check if it's alive."""
    await ctx.send("Pong!")

@bot.command(name="hello")
async def hello(ctx):
    """Say hello to the bot."""
    await ctx.send("Hey! How are you?")

@bot.command(name="bye")
async def bye(ctx):
    """Say goodbye to the bot."""
    await ctx.send("Please don't leave me ...")

@bot.command(name="python")
async def bye(ctx):
    """Say goodbye to the bot."""
    await ctx.send("python is the best language ever!")

@bot.command(name="hn")
async def top_stories(ctx):
    """Fetch the top 5 stories from Hacker News."""
    story_ids = get_top_stories()
    if story_ids:
        for story_id in story_ids:
            story_details = get_story_details(story_id)
            if story_details:
                title = story_details.get("title")
                url = story_details.get("url")
                await ctx.send(f"{title}: {url}")
    else:
        await ctx.send("Failed to fetch the top stories from Hacker News.")

@bot.event
async def on_ready():
    """Print a message when the bot is connected to Discord."""
    print(f"Bot connected as {bot.user}")

if __name__ == "__main__":
    # Load your API key from an environment variable or secret management service
    bot.run(os.getenv("DISCORD_API_KEY"))


import json
from os import system as sys
from os import _exit

def save_json(file: str, content: dict, key=None):

    with open(file, "r") as f:
        this_file = json.load(f)

    if key is None:
        index = list(content.keys())[0]
        this_file[index] = content[index]

    else:
        this_file[key] = content

    with open(file, "w") as f:
        json.dump(this_file, f)

def load_json(file: str, key=None):
    with open(file, "r") as f:
        if key is None:
            return json.load(f)
        else:
            return json.load(f)[key]

def create_options(options: list):
    return '\n'.join([f'[{count + 1}]: ' + x for count, x in enumerate(options)])

def print_choices(text: str, options: list):
    options.append("Exit game")
    text_options = create_options(options)
    choice = options[int(input(f'{text}\n{text_options}\n')) - 1]
    if choice == "Exit game":
        clear()
        print("Bye, thank you for playing!")
        choice = _exit(2)
    return choice

def clear():
    return sys('cls')


import math
import sys
if len(sys.argv[1:])==1:
    try:
        number=int(sys.argv[1])
        if number >=0:
            print(f"{math.sqrt(number)}")
        else:
            print("erreur")
    except ValueError:
        print("Wrong argument type")
else:
    print("erreur")


# myapp/custom_filters.py
from django import template
import ast

register = template.Library()

@register.filter(name='remove_chars')
def remove_chars(value):
    if value:
        # Remove quotes, commas, and square brackets using str.replace
        list = ast.literal_eval(value)
        return list
    else:
        return value

@register.filter(name='remove_session', takes_context=True)
def remove_session(request, value):
    if value:
        del request.session[value]
    else:
        return value


from QCNN import NeuralNetwork
import pandas as pd

nn= NeuralNetwork(2,2,3,1,.1,100000, train_test_split =1, split=False)

data_dict = {0:{'input1':0,'input2':0,'output':0},
            1:{'input1':1,'input2':0,'output':1},
            2:{'input1':0,'input2':1,'output':1},
            3:{'input1':1,'input2':1,'output':0}
            }

df = pd.DataFrame.from_dict(data_dict, orient = 'index')

nn.train(df)


# PyVot Python Variational Optimal Transportation
# Author: Liang Mi <icemiliang@gmail.com>
# Date: April 28th 2020
# Latest update: Sep 1st 2023
# Licence: MIT

import os
import sys
import numpy as np
import matplotlib.pyplot as plt
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
from vot_numpy import VOT
import utils_numpy as utils

np.random.seed(19)

# ------------ Generate data ------------- #
N0, N1, N2 = 1000, 200, 200

mean1, cov1 = [0., -0.2], [[0.05, 0], [0, 0.05]]
x11, x12 = np.random.multivariate_normal(mean1, cov1, N0).T
x1 = np.stack((x11, x12), axis=1).clip(-0.99, 0.99)

mean2, cov2 = [0.5, 0.5], [[0.01, 0], [0, 0.01]]
x21, x22 = np.random.multivariate_normal(mean2, cov2, N1).T
x2 = np.stack((x21, x22), axis=1).clip(-0.99, 0.99)

mean3, cov3 = [-0.5, 0.5], [[0.01, 0], [0, 0.01]]
x31, x32 = np.random.multivariate_normal(mean3, cov3, N2).T
x3 = np.stack((x31, x32), axis=1).clip(-0.99, 0.99)

K = 3
mean, cov = [0.0, 0.0], [[0.02, 0], [0, 0.02]]
y1, y2 = np.random.multivariate_normal(mean, cov, K).T
y = np.stack((y1, y2), axis=1).clip(-0.99, 0.99)
x = np.concatenate((x1, x2, x3), axis=0)

xmin, xmax, ymin, ymax = -.7, .8, -.65, .8
plt.close()

# ---------------kmeans--------------- #

from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=K, init=y).fit(x)

label = kmeans.predict(x)
y = kmeans.cluster_centers_

color_map = np.array([[237, 125, 49, 255], [112, 173, 71, 255], [91, 155, 213, 255]]) / 255

# ---------------VWB---------------

for reg in [0.5, 2, 1e9]:

    y_copy = y.copy()
    x_copy = x.copy()

    vot = VOT(y_copy, [x_copy], verbose=False)
    vot.cluster(lr=0.5, max_iter_h=1000, max_iter_y=1, beta=0.5, reg=reg)

    idx = vot.idx

    fig = plt.figure(figsize=(4, 4))
    ce = color_map[idx[0]]
    utils.scatter_otsamples(vot.y, vot.x[0], size_p=30, marker_p='o', color_x=ce,
                            xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, facecolor_p='none')
    plt.axis('off')
    # plt.savefig(str(int(reg)) + "_torch.svg", bbox_inches='tight')
    plt.savefig(str(reg) + "_numpy.png", dpi=300, bbox_inches='tight')

plt.figure(figsize=(4, 4))

ce = color_map[label]
utils.scatter_otsamples(y, x, size_p=30, marker_p='o', color_x=ce,
                        xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, facecolor_p='none')
plt.axis('off')
# plt.savefig("0_torch.svg", bbox_inches='tight')
plt.savefig("0_numpy.png", dpi=300, bbox_inches='tight')


def minTime(arr, n):
    if n <= 0: return 0

    incl = arr[0]
    excl = 0
    for i in range(1, n):
        incl_new = arr[i] + min(excl, incl)
        excl_new = incl
        incl = incl_new
        excl = excl_new
    return min(incl, excl)

T = int(input())
for i in range(T):
    n = int(input())
    arr = [int(x) for x in input().split(' ')]
    print(minTime(arr, n))


import numpy as np
import matplotlib.pyplot as plt
import time
import os
import sys
import copy
from glob import glob
import pandas
from collections import Counter
from imageio import imread
from IPython import embed

random_state = np.random.RandomState(394)
"""
images are color, but are the same across all channels (grayscale)
bottom ~20 pixels in y axis are the scale measurement
they all seem to have "1mm" at the bottom - are there any that are at a different zoom scale"""
datadir = './data/'
data_labels = glob(os.path.join(datadir, '*', '*.tsv*'))
for i in range(len(data_labels)):
  print('loading', data_labels[i])
  fp = pandas.read_csv(data_labels[i], sep='\t')
  tsv_name = os.path.split(data_labels[i])[1]
  fp.loc[:,'tsv_name'] = tsv_name
  fp.loc[:,'file_path'] = os.path.join(datadir, tsv_name[-8:-4])
  if not i:
    dd = fp
  else:
    dd = dd.append(fp)
dd.loc[:,'num'] = range(dd.shape[0])
# object_annotation_category
# img_file_name
# object_annotation_status
# object_lat
# object_lon
# object_date
# Cladocera, Copepoda, Rotifera, Holopediidae

unique = list(set(dd['object_annotation_category']))
dont_use = ['unknown', 'othertocheck', 'multiple<other']
labels_to_use = []
class_count = []
for ztype in unique:
    count = np.sum(pandas.Series(dd['object_annotation_category']).str.count(ztype))
    if ztype.lower() not in dont_use:
      labels_to_use.append(ztype)
      print(ztype, count)
      class_count.append(count)

# othertocheck 1873
# Holopediidae 778
# Chaoboridae 71
# Insecta 7
# multiple<other 51
# bubble 14
# Volvox 46
# nauplii<Copepoda 84
# Cladocera 4513
# detritus 5287
# Copepoda 9310
# badfocus<artefact 5035
# Arachnida 5
# Unknown 74
# Rotifera 575
# Leptodora 10
# nauplii Copepoda the name for baby copepods:
#   For copepods, the egg hatches into a nauplius form, with a head and a tail
#   but no true thorax or abdomen. The larva molts several times until it resembles
#   the adult and then, after more molts, achieves adult development.
#   The nauplius form is so different from the adult form that it was once thought to be a separate species.

for xc,ml in enumerate(labels_to_use):
  this_dd = dd[dd.loc[:,'object_annotation_category']==ml]
  if not xc:
    many_dd = this_dd
  else:
    many_dd = many_dd.append(this_dd)
many_dd = many_dd.set_index(np.arange(many_dd.shape[0]))

# make train test split
def write_data_file(dataframe, row_inds, data_type, base_dir):
    fname = os.path.join(base_dir, data_type+'.csv')
    print('writing', fname)
    f = open(fname, 'w')
    for i in row_inds:
        name = os.path.join(dataframe.loc[i,'file_path'], dataframe.loc[i,'img_file_name'])
        label = dataframe.loc[i, 'object_annotation_category']
        if class_count[labels_to_use.index(label)] < 1000 :
            f.write("%s,%s\n"%(name,label))
            #class_label = 'small_class'
        #else:
        #    class_label = label
        #if label in ['badfocus<artefact', 'detritus', 'bubble']:
        #    label = 'not_useful'
        #f.write("%s,%s,%s\n"%(name,class_label,label))
    f.close()

def make_train_test_split(df, exp_name):
    many_inds = np.array(df.index)
    train_rows = []
    valid_rows = []
    for label in labels_to_use:
        this_label_inds = np.array(df[df.loc[many_inds,'object_annotation_category'] == label].index)
        random_state.shuffle(this_label_inds)
        this_row_size = this_label_inds.shape[0]
        n_val = int(this_row_size*.2)
        if n_val < 1:
            n_val+=1
        n_tr = this_row_size-n_val
        print('adding %s valid %s train for %s'%(n_val, n_tr, label))
        valid_rows.extend(list(this_label_inds[:n_val]))
        train_rows.extend(list(this_label_inds[n_val:]))

    overall_dir = os.path.join('experiments', exp_name)
    if not os.path.exists(overall_dir):
        os.makedirs(overall_dir)
    write_data_file(df, valid_rows, 'valid',  overall_dir)
    write_data_file(df, train_rows, 'train',  overall_dir)

exp_name = 'most_merged'
make_train_test_split(many_dd, exp_name)


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
        n1,n2 = 0,0
        temp1 =headA
        temp2 = headB
        while(temp1):
            n1 += 1
            temp1 = temp1.next
        while(temp2):
            n2 += 1
            temp2 = temp2.next
        temp1 =headA
        temp2 = headB
        ab = abs(n1-n2)
        if n1>n2:
            while(ab>0):
                temp1 = temp1.next
                ab -= 1
        else:
            while(ab>0):
                temp2 = temp2.next
                ab -= 1
        while(temp1 != temp2):
            temp1 = temp1.next
            temp2 = temp2.next

        return temp1


from tkinter import *
import database as db

def win_setup(ws_status):
    def oksetup():
        idcomp = entryval1.get()
        nomcom = entryval2.get()
        idrece = entryval3.get()
        numfol = entryval4.get()

        if not ws_status:
            datacom = (idcomp, nomcom, idrece, numfol)
            db.insert_company_info(datacom)
        else:
            datacom = (nomcom, idrece, numfol)
            db.modify_company_info(datacom)

        stw.destroy()

    def nosetup():
        stw.destroy()

    def closeDisable():
        pass

    stw = Toplevel()
    stw.transient()
    stw.grab_set()
    stw.title("Rekening - Configuración")
    stw.geometry("580x210")
    stw.resizable(False, False)
    stwicon = PhotoImage(file="icon.png")
    stw.iconphoto(True, stwicon)
    stw.protocol("WM_DELETE_WINDOW", closeDisable)

    # SetUp Windows - Variables Declaration

    entryval1 = IntVar()
    entryval2 = StringVar()
    entryval3 = StringVar()
    entryval4 = IntVar()

    if not ws_status:
        entryval1.set(1)
        entryval3.set("BEN-001")
    else:
        dw = db.verify_company()

        entryval1.set(dw[0][0])
        entryval2.set(dw[0][1])
        entryval3.set(dw[0][2])
        entryval4.set(dw[0][3])

    # SetUp Window - General Information

    stlf1 = LabelFrame(stw, text=" Información General ",
                       width=410, height=105)
    stlf1.place(x=15, y=10)

    Label(stlf1, text="Id Empresa").place(x=10, y=10)
    Entry(stlf1, width=3, textvariable=entryval1,
          state=DISABLED).place(x=82, y=12)

    Label(stlf1, text="Empresa").place(x=10, y=50)
    Entry(stlf1, textvariable=entryval2, width=50).place(x=83, y=53)

    # SetUp Window - Initial Values Receptor and Document

    stlf2 = LabelFrame(stw, text=" Identificadores ", width=410, height=70)
    stlf2.place(x=15, y=125)

    Label(stlf2, text="Id Receptor").place(x=10, y=10)
    Entry(stlf2, textvariable=entryval3, width=10,
          state=DISABLED).place(x=82, y=12)

    Label(stlf2, text="Folio Cheque").place(x=250, y=10)
    Entry(stlf2, textvariable=entryval4, width=8).place(x=334, y=12)

    # SetUp Window - Ok & Cancel Buttons

    stbok = Button(stw, text="Aceptar", command=oksetup,
                   width=15).place(x=450, y=60)
    stbcancel = Button(stw, text="Cancelar", width=15,
                       command=nosetup).place(x=450, y=110)

    # SetUp Window Loop
    stw.mainloop()


# Import models
from model.resunet3d import ResUNet3D
from model.r2unet3d import R2UNet3D
from model.unet3d import UNet3D

# Import packages
from torch.optim import Adam
from dataloader import MRIDataset
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import StepLR
from torch.nn import DataParallel
import torch
import argparse
import torchvision.transforms as T
import logging

# Import utils
from utils.utils import load_checkpoint, plot_train_loss, save_model
from utils.evaluate import evaluate
from utils.lossfunction import DiceBCELoss, DiceLoss, IoULoss, FocalLoss, FocalTverskyLoss, TverskyLoss

# Training
def train(args, model, device, train_loader, optimizer, epoch, criterion):
    costs = []
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):

        data, target = data.float().to(device), target.float().to(device)

        output = model(data)

        optimizer.zero_grad()
        cost = criterion(output, target)
        cost.backward()
        optimizer.step()

        costs.append(cost.item())

        if batch_idx % args.log_interval == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), cost.item()))
            if args.dry_run:
                break

    avgcost = sum(costs)/len(costs)

    return avgcost

def get_args():
    # Train settings
    parser = argparse.ArgumentParser(description='PyTorch 3D Segmentation')
    parser.add_argument('--network', '-u', default='Unet3D', help='Specify the network (Unet3D / ResidualUnet3D / R2Unet3D)')
    parser.add_argument('--batch-size', type=int, default=1, metavar='N',help='input batch size for training (default: 1)')
    parser.add_argument('--epochs', type=int, default=10, metavar='N',help='number of epochs to train (default: 10)')
    parser.add_argument('--lr', type=float, default=2.5e-4, metavar='LR', help='learning rate (default: 2.5e-4)')
    parser.add_argument('--gamma', type=float, default=0.1, metavar='M',help='Learning rate step gamma (default: 0.1)')
    parser.add_argument('--no-cuda', action='store_true', default=False,help='disables CUDA training')
    parser.add_argument('--dry-run', action='store_true', default=False,help='quickly check a single pass')
    parser.add_argument('--seed', type=int, default=1, metavar='S',help='random seed (default: 1)')
    parser.add_argument('--log-interval', type=int, default=10, metavar='N',help='how many batches to wait before logging training status')
    parser.add_argument('--save-model', action='store_true', default=True,help='For Saving the current Model')
    parser.add_argument('--checkpoint', '-c', metavar='FILE', help='Specify the path to the model')

    return parser.parse_args()

def main():

    # ------------------------------------ Network Config ------------------------------------

    args = get_args()

    logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
    use_cuda = not args.no_cuda and torch.cuda.is_available()
    torch.manual_seed(args.seed)
    device = torch.device("cuda" if use_cuda else "cpu")

    train_kwargs = {'batch_size': args.batch_size, 'shuffle': True}
    if use_cuda:
        cuda_kwargs = {'num_workers': 1,
                       'pin_memory': True,
                       'shuffle': True}
        train_kwargs.update(cuda_kwargs)

    assert args.network.casefold() in ("unet3d", "residualunet3d","r2unet3d"), 'Network must be (Unet3D / ResidualUnet3D / R2Unet3D)'

	# Specify network
    if args.network.casefold() == "unet3d":
        model = UNet3D(in_channels=1, out_channels=1).to(device)
    elif args.network.casefold() == "residualunet3d":
        model = ResUNet3D(in_channels=1, out_channels=1).to(device)
    elif args.network.casefold() == "r2unet3d":
        model = R2UNet3D(in_channels=1, out_channels=1).to(device)

    # If using multiple gpu
    if torch.cuda.device_count() > 1 and use_cuda:
        model = DataParallel(model)

    # If load checkpoint
    if args.checkpoint:
        load_checkpoint(args.checkpoint, model, device=device)

    # Hyperparameters
    optimizer = Adam(model.parameters(), lr=args.lr)
    scheduler = StepLR(optimizer, step_size=50, gamma=args.gamma)
    loss = DiceLoss()

    # ------------------------------------ Data Loading ------------------------------------

    # Train data transformation
    transformation = T.Compose([T.ToTensor(),
                    T.Resize((240,240)),
                    T.RandomHorizontalFlip(),
                    T.RandomCrop((240,240), padding=50, pad_if_needed=True)
                    ])

    traindataset = MRIDataset(train=True, transform=transformation, elastic=True)

    test_kwargs = {'batch_size': args.batch_size}

    if use_cuda:
        cuda_kwargs = {'num_workers': 1,
                       'pin_memory': True,
                       'shuffle': False}
        test_kwargs.update(cuda_kwargs)

    testdataset = MRIDataset(train=False, transform=T.Compose([T.ToTensor(),T.Resize((240,240))]), elastic=False)

	# Data Loading
    test_loader = DataLoader(dataset=testdataset, **test_kwargs)
    train_loader = DataLoader(dataset=traindataset, **train_kwargs)

    # ------------------------------------ Training Loop ------------------------------------

    # Validation Loss
    minvalidation = 1
    loss_train = []
    loss_val = []

    logging.info(f'''Starting training:
        Network:         {args.network}
        Epochs:          {args.epochs}
        Batch size:      {args.batch_size}
        Learning rate:   {args.lr}
        Training size:   {len(train_loader)}
        Validation size: {len(test_loader)}
        Device:          {device.type}
    ''')

    # Training process
    for epoch in range(1, args.epochs + 1):

        trainloss = train(args, model, device, train_loader, optimizer, epoch, loss)
        # valloss = test(model, device, val_loader, epoch, loss)

        valstat = evaluate(model, test_loader, device, 0.5, show_stat=False)
        valloss = 1 - valstat['Dice']

        print('Average train loss: {}'.format(trainloss))
        print('Average test loss: {}'.format(valloss))
        loss_train.append(trainloss)
        loss_val.append(valloss)
        print()

        scheduler.step()

        # Save the best validated model
        if valloss < minvalidation and args.save_model:
            minvalidation = valloss

            save_model(model, is_best=True, checkpoint_dir='checkpoints')

    # Plot training loss graph
    plot_train_loss(loss_train, loss_val)

if __name__ == '__main__':
    main()


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

ans_list = []
for i in range(N):
    if line_list[i] != 0:
        ans_list.insert(-line_list[i], i+1)   # insert(-i, value)에서 -1은 뒤에서 첫 번째 인덱스 바로 앞에 value를 삽입하라는 뜻
    else:
        ans_list.append(i+1)                  # line_list[i] 값이 0일 때는 제일 뒤에 줄을 서므로 그냥 append로 추가

print(*ans_list)


import cv2

# Capture video from a camera
cap = cv2.VideoCapture(0)

# Read the first frame
ret, frame = cap.read()

# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Define a background subtractor object
fgbg = cv2.createBackgroundSubtractorMOG2()

while True:
    # Read a new frame
    ret, frame = cap.read()

    # Apply the background subtractor to get the foreground mask
    fgmask = fgbg.apply(frame)

    # Apply thresholding to remove noise
    thresh = cv2.threshold(fgmask, 127, 255, cv2.THRESH_BINARY)[1]

    # Find contours in the thresholded image
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # Draw bounding boxes around the moving objects
    for contour in contours:
        (x, y, w, h) = cv2.boundingRect(contour)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

    # Display the resulting frame
    cv2.imshow('frame', frame)

    # Exit if the user presses 'q'
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the capture and destroy all windows
cap.release()
cv2.destroyAllWindows()


# inbuilt class for queue
from collections import deque

q = deque(maxlen=3)

q.append(1)
q.append(2)
q.append(3)
# q.clear() # will empty the queue
print(q.popleft())


'''
https://www.acmicpc.net/problem/3036
'''

import sys

input_ = sys.stdin.readline

def getGCD(a, b):
    return getGCD(b, a%b) if a%b else b

int(input_())
rings = map(int, input_().split())
first = next(rings)

for ring in rings:
    gcd = getGCD(first, ring)
    print(f'{first//gcd}/{ring//gcd}')


import warnings

import matplotlib
from sklearn.metrics import f1_score
from tqdm import tqdm

matplotlib.use("agg")

# Import TeLL
from TeLL.config import Config
from TeLL.regularization import regularize
from TeLL.session import TeLLSession
from TeLL.utility.misc import AbortRun
from TeLL.utility.timer import Timer

# Import Tensorflow
if __name__ == "__main__":
    import tensorflow as tf
    import numpy as np
    import torch
    from pyll_functions import invoke_dataset_from_config

# ----------------------------------------------------------------------------------------------------------------------
# Functions
# ----------------------------------------------------------------------------------------------------------------------

def validate(val_loader, n_classes, session, loss, prediction, model, workspace, step, batchsize, tell):
    # VALIDATION
    n_mbs = len(val_loader)
    val_batchsize = val_loader.batch_size
    loss_sum = 0
    val_predictions = np.zeros(shape=(n_mbs * val_batchsize, n_classes))
    val_labels = np.zeros(shape=(n_mbs * val_batchsize, n_classes))

    for vmbi, vmb in tqdm(enumerate(val_loader), total=len(val_loader), desc="Validation"):
        val_loss, val_pred = session.run([loss, prediction],
                                         feed_dict={model.X: vmb['input'].squeeze().numpy(),
                                                    model.y_: vmb['target'].numpy(),
                                                    model.dropout: 0})
        loss_sum += val_loss

        val_predictions[vmbi * len(vmb['ID']):(vmbi + 1) * len(vmb['ID']), :] = val_pred
        val_labels[vmbi * len(vmb['ID']):(vmbi + 1) * len(vmb['ID']), :] = vmb['target'].numpy()

    np.savetxt("{}/{}_samples-predictions.txt".format(workspace.result_dir, step * batchsize),
               val_predictions, delimiter=',')
    np.savetxt("{}/{}_samples-labels.txt".format(workspace.result_dir, step * batchsize),
               val_labels, delimiter=',')

    # F1 score
    class_f1s = []
    for i in range(n_classes):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            class_f1 = f1_score(y_true=val_labels[:, i].astype(int),
                                y_pred=(val_predictions[:, i] + 0.5).astype(int))
        class_f1s.append(class_f1)
    avg_f1 = np.mean(class_f1s)

    print(' * Validation F1-Score {f1:.3f}'.format(f1=avg_f1))
    tell.save_checkpoint(global_step=step)

    return avg_f1

def update_step(loss, config, tell, lr, trainables):
    # GRADIENTS
    gradients = tf.gradients(loss, trainables)
    gradient_names_before_clip = [g.op.inputs[-1].name.split("/")[1] + "_bias" if "Reshape" in g.op.inputs[-1].name else
                                  g.op.inputs[-1].name.split("/")[1] for g in gradients]
    # Gradient clipping
    gradients, _ = tf.clip_by_global_norm(gradients, 5.0)
    gradient_name_dict = dict(zip([g.op.inputs[-1].name for g in gradients], gradient_names_before_clip))

    # OPTIMIZER
    if config.get_value("lrs", False):
        momentum = config.get_value("optimizer_params")["momentum"]
        update = tf.train.MomentumOptimizer(lr, momentum).apply_gradients(zip(gradients, trainables))
    else:
        update = tell.tf_optimizer.apply_gradients(zip(gradients, trainables))
    return update, gradients, gradient_name_dict

def main(_):
    config = Config()
    np.random.seed(config.get_value("random_seed", 12345))

    # PARAMETERS
    n_epochs = config.get_value("epochs", 100)
    batchsize = config.get_value("batchsize", 8)
    n_classes = config.get_value("n_classes", 13)
    dropout = config.get_value("dropout", 0.25)  # TODO
    num_threads = config.get_value("num_threads", 5)
    initial_val = config.get_value("initial_val", True)

    # READER, LOADER
    readers = invoke_dataset_from_config(config)
    reader_train = readers["train"]
    reader_val = readers["val"]
    train_loader = torch.utils.data.DataLoader(reader_train, batch_size=config.batchsize, shuffle=True,
                                               num_workers=num_threads)
    val_loader = torch.utils.data.DataLoader(reader_val, batch_size=1, shuffle=False, num_workers=num_threads)

    # CONFIG
    tell = TeLLSession(config=config, model_params={"shape": reader_train.shape})
    # Get some members from the session for easier usage
    session = tell.tf_session
    model = tell.model
    workspace, config = tell.workspace, tell.config

    prediction = tf.sigmoid(model.output)
    prediction_val = tf.reduce_mean(tf.sigmoid(model.output), axis=0, keepdims=True)

    # LOSS
    if hasattr(model, "loss"):
        loss = model.loss()
    else:
        with tf.name_scope("Loss_per_Class"):
            loss = 0
            for i in range(n_classes):
                loss_batch = tf.nn.sigmoid_cross_entropy_with_logits(logits=model.output[:, i], labels=model.y_[:, i])
                loss_mean = tf.reduce_mean(loss_batch)
                loss += loss_mean

    # Validation loss after patching
    if hasattr(model, "loss"):
        loss_val = model.loss()
    else:
        with tf.name_scope("Loss_per_Class_Patching"):
            loss_val = 0
            for i in range(n_classes):
                loss_batch = tf.nn.sigmoid_cross_entropy_with_logits(
                    logits=tf.reduce_mean(model.output[:, i], axis=0, keepdims=True), labels=model.y_[:, i])
                loss_mean = tf.reduce_mean(loss_batch)
                loss_val += loss_mean

    # REGULARIZATION
    reg_penalty = regularize(layers=model.layers, l1=config.l1, l2=config.l2, regularize_weights=True,
                             regularize_biases=True)

    # LEARNING RATE (SCHEDULE)
    # if a LRS is defined always use MomentumOptimizer and pass learning rate to optimizer
    lrs_plateu = False
    if config.get_value("lrs", None) is not None:
        lr_sched_type = config.lrs["type"]
        if lr_sched_type == "plateau":
            lrs_plateu = True
            learning_rate = tf.placeholder(tf.float32, [], name='learning_rate')
            lrs_learning_rate = config.get_value("optimizer_params")["learning_rate"]
            lrs_n_bad_epochs = 0  # counter for plateu LRS
            lrs_patience = config.lrs["patience"]
            lrs_factor = config.lrs["factor"]
            lrs_threshold = config.lrs["threshold"]
            lrs_mode = config.lrs["mode"]
            lrs_best = -np.inf if lrs_mode == "max" else np.inf
            lrs_is_better = lambda old, new: (new > old * (1 + lrs_threshold)) if lrs_mode == "max" else (
                    new < old * (1 - lrs_threshold))
    else:
        learning_rate = None  # if no LRS is defined the default optimizer is used with its defined learning rate

    # LOAD WEIGHTS and get list of trainables if specified
    assign_loaded_variables = None
    trainables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
    if config.get_value("checkpoint", None) is not None:
        with Timer(name="Loading Checkpoint", verbose=True):
            assign_loaded_variables, trainables = tell.load_weights(config.get_value("checkpoint", None),
                                                                    config.get_value("freeze", False),
                                                                    config.get_value("exclude_weights", None),
                                                                    config.get_value("exclude_freeze", None))

    # Update step
    if len(trainables) > 0:
        update, gradients, gradient_name_dict = update_step(loss + reg_penalty, config, tell, lr=learning_rate,
                                                            trainables=trainables)

    # INITIALIZE Tensorflow VARIABLES
    step = tell.initialize_tf_variables().global_step

    # ASSING LOADED WEIGHTS (overriding initializations) if available
    if assign_loaded_variables is not None:
        session.run(assign_loaded_variables)

    # -------------------------------------------------------------------------
    # Start training
    # -------------------------------------------------------------------------
    try:
        n_mbs = len(train_loader)
        epoch = int((step * batchsize) / (n_mbs * batchsize))
        epochs = range(epoch, n_epochs)

        if len(trainables) == 0:
            validate(val_loader, n_classes, session, loss_val, prediction_val, model,
                     workspace, step, batchsize, tell)
            return

        print("Epoch: {}/{} (step: {}, nmbs: {}, batchsize: {})".format(epoch + 1, n_epochs, step, n_mbs, batchsize))
        for ep in epochs:
            if ep == 0 and initial_val:
                f1 = validate(val_loader, n_classes, session, loss_val, prediction_val, model,
                              workspace, step, batchsize, tell)
            else:
                if config.has_value("lrs_best") and config.has_value("lrs_learning_rate") and config.has_value(
                        "lrs_n_bad_epochs"):
                    f1 = config.get_value("lrs_f1")
                    lrs_best = config.get_value("lrs_best")
                    lrs_learning_rate = config.get_value("lrs_learning_rate")
                    lrs_n_bad_epochs = config.get_value("lrs_n_bad_epochs")
                else:
                    f1 = 0

            # LRS "Plateu"
            if lrs_plateu:
                # update scheduler
                if lrs_is_better(lrs_best, f1):
                    lrs_best = f1
                    lrs_n_bad_epochs = 0
                else:
                    lrs_n_bad_epochs += 1
                # update learning rate
                if lrs_n_bad_epochs > lrs_patience:
                    lrs_learning_rate = max(lrs_learning_rate * lrs_factor, 0)
                    lrs_n_bad_epochs = 0

            with tqdm(total=len(train_loader), desc="Training [{}/{}]".format(ep + 1, len(epochs))) as pbar:
                for mbi, mb in enumerate(train_loader):
                    # LRS "Plateu"
                    if lrs_plateu:
                        feed_dict = {model.X: mb['input'].numpy(), model.y_: mb['target'].numpy(),
                                     model.dropout: dropout,
                                     learning_rate: lrs_learning_rate}
                    else:
                        feed_dict = {model.X: mb['input'].numpy(), model.y_: mb['target'].numpy(),
                                     model.dropout: dropout}

                    # TRAINING
                    pred, loss_train, _ = session.run([prediction, loss, update], feed_dict=feed_dict)

                    # Update status
                    pbar.set_description_str("Training [{}/{}] Loss: {:.4f}".format(ep + 1, len(epochs), loss_train))
                    pbar.update()
                    step += 1

            validate(val_loader, n_classes, session, loss_val, prediction_val, model,
                     workspace, step, batchsize, tell)
    except AbortRun:
        print("Aborting...")
    finally:
        tell.close(global_step=step, save_checkpoint=True)

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


from django.conf.urls import url

from . import views

app_name = 'contact'

urlpatterns = [

    # Formulario de contacto.
    url(
        regex=r'^$',
        view=views.ContactView.as_view(),
        name='contact'
    ),

    # Lista de mensajes.
    url(
        regex=r'^messages/$',
        view=views.ContactMessageListView.as_view(),
        name='message_list'
    ),

    # Mensajes paginados.
    url(
        regex=r'^messages/page/(?P<pk>\d+)$',
        view=views.ContactMessageListView.as_view(),
        name='message_list_paginated'
    ),

    # Detalles de un mensaje.
    url(
        regex=r'^message/(?P<pk>\d+)/$',
        view=views.ContactMessageDetailView.as_view(),
        name='message_detail'
    ),
]


from pilha import Pilha

def conversorHexParaDecimal(hexadecimal):
    pilha = Pilha()
    for char in hexadecimal:
        if char.isdigit():
            pilha.push(int(char))
        elif char.isalpha():
            # Converter caracteres A-F para valores numéricos
            valor = ord(char.upper()) - ord('A') + 10
            pilha.push(valor)

    decimal = 0
    posicao_decimal = 0
    while not pilha.is_empty():
        digit = pilha.pop()
        decimal += digit * (16 ** posicao_decimal)
        posicao_decimal += 1

    return decimal

hexadecimal = input("Digite um número hexadecimal: ")

decimal = conversorHexParaDecimal(hexadecimal)

print("Número decimal:", decimal)


# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.

__author__ = "Mason"
__date__ = "$9-Jan-2016 6:58:02 PM$"

def main():
    import FileLoadWrapper

    def isInt(s):
        try:
            int(s)
            return True
        except ValueError:
            return False

    #get the puzzle input
    loader = FileLoadWrapper.FileLoader('Input', '7')
    commandList = loader.read()
    commandList = commandList.split("\n")

    commandSequence = {}
    i = 0
    for command in commandList:
        commandSequence[i] = command
        i = i+1
    inputFile.close()
    print('Finished reading file.')

    print('Beginning the tokenization.')
    print(commandSequence)
    wireList = {}
    #split each line into a list
    for key in commandSequence:
        line = commandSequence[key]
        commandSequence[key] = line.split(' ')
        #make a dictionary of all wires
        for token in commandSequence[key]:
            if token != 'NOT' and token != 'LSHIFT' and token != 'OR' and token != 'RSHIFT' and token != 'AND' and token != '->' and not isInt(token):
                if token not in wireList:
                    wireList[token] = 'unknown'
    wireList['b'] = '3176'
    print('Finished tokenizing the commands and building wirelist.')

    print('Starting main process loop...')
    #now we loop over each command row, when we hit an assignment or an equation
    #with known values(from wireList) then we calculate the value and save it
    solved  = 0
    todrop = []
    try:
        while solved == 0:

            print('Removing previously processed gates')

            for keyToDrop in todrop:
                del commandSequence[keyToDrop]
            todrop = []#clear the todrop array
            print('Beginning new processing pass. '+ str(len(commandSequence))+' gates left.')
            for key in commandSequence:
                line = commandSequence[key]
                print(line)
                #handle simple assignment cases like:
                #line['1', '->', 'a']
                if len(line) == 3:
                    if isInt(line[0]) == False and wireList[line[0]] != 'unknown':
                        line[0] = wireList[line[0]]
                    if isInt(line[0]) and wireList[line[2]] == 'unknown':
                        wireList[line[2]] = line[0]
                        print('Setting ' + str(line[2])+' to '+str(line[0]))
                        todrop.append(key)
                    elif wireList[line[2]] != 'unknown':
                        todrop.append(key)

                if len(line) == 4 and line[0] == 'NOT':
                    if isInt(line[1]) == False and wireList[line[1]] != 'unknown':
                        line[1] = wireList[line[1]]
                    if isInt(line[1]) and wireList[line[3]] == 'unknown':
                        wireList[line[3]] =  ~int(line[1])
                        todrop.append(key)
                    elif wireList[line[3]] != 'unknown':
                        todrop.append(key)

                if len(line) == 5:
                    if isInt(line[0]) == False and wireList[line[0]] != 'unknown':
                        line[0] = wireList[line[0]]
                    if isInt(line[2]) == False and wireList[line[2]] != 'unknown':
                        line[2] = wireList[line[2]]
                    if isInt(line[0]) and isInt(line[2]) and wireList[line[4]] == 'unknown':
                        if line[1] == 'LSHIFT':
                            wireList[line[4]] = int(line[0])<<int(line[2])
                            print('Setting ' + str(line[4])+' to ' +str(line[0])+'(LSHIFT)'+str(line[2])+'='+str(wireList[line[4]]))
                            todrop.append(key)
                        if line[1] == 'RSHIFT':
                            wireList[line[4]] = int(line[0])>>int(line[2])
                            print('Setting ' + str(line[4])+' to ' +str(line[0])+'(RSHIFT)'+str(line[2])+'='+str(wireList[line[4]]))
                            todrop.append(key)
                        if line[1] == 'AND':
                            wireList[line[4]] = int(line[0])&int(line[2])
                            print('Setting ' + str(line[4])+' to ' +str(line[0])+'(AND)'+str(line[2])+'='+str(wireList[line[4]]))
                            todrop.append(key)
                        if line[1] == 'OR':
                            wireList[line[4]] = int(line[0])|int(line[2])
                            print('Setting ' + str(line[4])+' to ' +str(line[0])+'(OR)'+str(line[2])+'='+str(wireList[line[4]]))
                            todrop.append(key)
                    elif wireList[line[4]] != 'unknown':
                        todrop.append(key)
                if isInt(wireList['a']):
                    solved = 1
                    print(wireList)
                    print('Value of a is:'+str(wireList['a']))
    except Exception as e:
        print('Exception occured:'+e)
main()


#!/usr/bin/env python3

""" HPNorton API for accessing MongoDB collections """

import base64
import pymongo
from loguru import logger
import src.mongodb_conn as mdb_conn

# from pymongo import MongoClient

MONGO = mdb_conn.MongoDBConnection()

def import_data(directory_name, *files):
    """Import csv files into mongodatabase.

    Arguments:
        directory_name {string} -- directory data files are stored in
        *files {list} -- *args list of csv files to import

    Returns:
        ((),()) -- Tuple of Tuples, ((Success),(Failures)) from imports
    """
    _success = ()
    _fail = ()

    for csv_file in files:
        success, fail = insert_to_mongo(directory_name, csv_file)
        _success = _success + (success,)
        _fail = _fail + (fail,)

    return (_success, _fail)

    # Cheese mode to test the silly test
    # return ((10, 10, 9), (0, 0, 0))

def insert_to_mongo(directory_name, filename):
    """Inserts given csv file into mongo

    Arguments:
        directory_name {string} -- directory containing csv file
        filename {string} -- csv filename to import

    Returns:
        int, int -- success / fails of imports
    """
    success = 0
    fail = 0

    with MONGO:
        mdb = MONGO.connection.HPNorton_PyMongo
        logger.info(f"Inserting {filename[:-4]} into Mongo...")
        database_name = mdb[filename[:-4]]

        iter_lines = get_line(open_file(f"{directory_name}{filename}"))
        header = next(iter_lines).split(",")

        if filename[:-4] != "rental":
            database_name.create_index(header[0], unique=True)
        else:
            database_name.create_index(
                [
                    (header[0], pymongo.ASCENDING),
                    (header[1], pymongo.ASCENDING),
                ],
                unique=True,
            )

        for line in iter_lines:
            line = line.split(",")
            new_addition = {}
            for num, field in enumerate(header):
                new_addition[field] = line[num]
            try:
                database_name.insert_one(new_addition)
                success += 1
            except pymongo.errors.DuplicateKeyError:
                fail += 1

    return success, fail

def show_available_products():
    """Creates a list of currently available products

    Returns:
        dict -- Dictionary of Mongo documents, key=product_id
    """
    logger.info(f"Preparing dict of available prodcuts...")
    available_products = {}

    with MONGO:
        mdb = MONGO.connection.HPNorton_PyMongo
        products = mdb["product"]
        for doc in products.find():
            del doc["_id"]
            if int(doc["quantity_available"]) > 0:
                product_id = doc["product_id"]
                del doc["product_id"]
                available_products[product_id] = doc

    return available_products

def rentals_for_customer(user_id):
    """Prepares a dict of products rented by user_id

    Arguments:
        user_id {string} -- user_id reference into product collection

    Returns:
        dict -- Dictionary of products rented by specified user_id
    """
    logger.info(f"Perparing customer dict for user_id: {user_id}...")
    rentals_for_user = {}

    with MONGO:
        mdb = MONGO.connection.HPNorton_PyMongo

        products = mdb["product"]
        rentals = mdb["rental"]

        # First we get a list of rentals for the specified user_id
        query = {"user_id": user_id}
        for rental in rentals.find(query):

            # Now we get product details from products via the product_id
            query = {"product_id": rental["product_id"]}
            for product in products.find(query):
                product_id = product["product_id"]
                del product["_id"]
                del product["product_id"]
                rentals_for_user[product_id] = product

    return rentals_for_user

def show_rentals(product_id):
    """Prepares a dict of customers renting product_id

    Arguments:
        product_id {string} -- product_id reference into rental collection

    Returns:
        dict -- Dictionary of rental customers for specified product_id
    """
    logger.info(f"Perparing rental dict for product_id: {product_id}...")
    users_renting_product = {}

    with MONGO:
        mdb = MONGO.connection.HPNorton_PyMongo

        # products = mdb['product']
        rentals = mdb["rental"]
        customers = mdb["customers"]

        # First we get a list of users that have the specified rental
        query = {"product_id": product_id}
        for rental in rentals.find(query):

            # Now we query customers for user_id specified from the rental item.
            query = {"user_id": rental["user_id"]}
            for customer in customers.find(query):
                user_id = customer["user_id"]
                del customer["_id"]
                del customer["user_id"]
                users_renting_product[user_id] = customer

    return users_renting_product

def list_all_products():
    """Prepares a dictionary of all products

    Returns:
        dict -- Dictionary containing all products
    """
    logger.info(f"Perparing dict of all products...")
    all_products_dict = {}

    with MONGO:
        mdb = MONGO.connection.HPNorton_PyMongo
        products = mdb["product"]
        all_products = products.find({})
        for product in all_products:
            product_id = product["product_id"]
            del product["_id"]
            del product["product_id"]
            all_products_dict[product_id] = product
    return all_products_dict

def list_all_customers():
    """Prepares a dictionary of all customers

    Returns:
        dict -- Dictionary containing all customers
    """
    logger.info(f"Perparing dict of all products...")
    all_customers_dict = {}

    with MONGO:
        mdb = MONGO.connection.HPNorton_PyMongo
        customers = mdb["customers"]
        all_customers = customers.find({})
        for customer in all_customers:
            user_id = customer["user_id"]
            del customer["_id"]
            del customer["user_id"]
            all_customers_dict[user_id] = customer
    return all_customers_dict

def get_line(lines):
    """Generator for lines of content from csv file

    Arguments:
        lines {list} -- List of lines containing data from csv file

    Yields:
        string -- CSV string containing information for a single customer.
    """
    for line in lines:
        yield line

def open_file(filename):
    """Opens the file specified from the command line

    Arguments:
        filename {string} -- Name of CSV file to import

    Returns:
        list containing lines of customer data from csv file
    """
    # I'm assuming pythons garbage collection takes care of closing the file.
    with open(filename, "rb") as content:
        return content.read().decode("utf-8-sig", errors="ignore").split("\n")

def drop_database():
    """Drops HPNorton_PyMongo database
    """

    logger.warning("Dropping HPNorton_PyMongo database")
    mdb = mdb_conn.MongoClient()
    mdb.drop_database("HPNorton_PyMongo")

def drop_collections():
    """Drops collections from Mongo that are used for this program
    """

    with MONGO:
        mdb = MONGO.connection.HPNorton_PyMongo

        customers = mdb["customers"]
        logger.warning('Dropping "Cusomters"')
        customers.drop()

        rental = mdb["rental"]
        logger.warning('Dropping "Rental"')
        rental.drop()

        product = mdb["product"]
        logger.warning('Dropping "Product"')
        product.drop()

    logger.warning("Purge complete!")

    return base64.b64decode("QWxsIHVyIGJhc2UgYXJlIGJlbG9uZyB0byB1cw==")


import os
from .settings import *

DEBUG = True

# python manage.py shell_plus --notebook --settings stump_backend.local_settings
INSTALLED_APPS += {
    'django_extensions',
}

# Setting below prevents this error:
# 'SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.'
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"


# This file is part of Estimation of Causal Effects in the Alzheimer's Continuum (Causal-AD).
#
# Causal-AD is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Causal-AD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Causal-AD. If not, see <https://www.gnu.org/licenses/>.
from dataclasses import dataclass
from itertools import product
import logging
from pathlib import Path
import pickle
from typing import Iterable, Optional, Tuple

import pandas as pd

from .estimate import OutcomeModelFitter
from .evaluate import evaluate
from .io import load_deconfounder_residuals, load_patient_data, load_synthetic_data

RANDOM_STATE = 52711171

LOG = logging.getLogger(__name__)

@dataclass
class ModelInput:
    features_path: Path
    outcome_path: Path
    output_dir: str
    data_file: Optional[str] = None

def _get_generated_data(base_dir: str) -> Iterable[Path]:
    import os

    base_dir = Path(base_dir)
    for sim_dir in base_dir.glob("simulate_data_*"):
        for root, _, filenames in os.walk(sim_dir):
            for f in filenames:
                if f == 'data_generated.h5':
                    yield Path(root) / f

def _remove_suffix(x):
    if x.endswith("-resid"):
        return x[:-6]
    return x

def _name_coef_axis(inputs, coef_true, coef_est, prefix):
    coef_est.rename(_remove_suffix, axis=0, inplace=True)
    coef_true = coef_true.rename_axis(inputs.output_dir)
    coef_est = coef_est.rename_axis(f"{prefix}{inputs.features_path.stem}")
    return (coef_true, coef_est)

def _write_coefficients(
    base_dir: Path, output_name: str, outputs: Tuple[pd.DataFrame, pd.DataFrame],
) -> Path:
    coef_est = outputs[1]
    out_file = base_dir / output_name / f"coefs_{coef_est.index.name}"
    LOG.info("Writing coefficients to %s", out_file)
    with open(out_file, "wb") as fout:
        pickle.dump(outputs, fout, protocol=pickle.HIGHEST_PROTOCOL)
    return out_file

def _get_substitute_input_data(
    base_dir_path: Path, features_dir: Path, data_file: Optional[Path] = None,
) -> Iterable[ModelInput]:
    for data_path, sim_path in product(
        features_dir.glob("subst_conf_*.h5"),
        _get_generated_data(base_dir_path)
    ):
        out_dir = str(sim_path.parent.relative_to(base_dir_path))
        result_file = sim_path.with_name(f"coefs_{data_path.stem}")
        if not result_file.exists():
            data = ModelInput(data_path, sim_path, out_dir, data_file)
            yield data

def fit_substitute_model(inputs: ModelInput) -> Tuple[pd.DataFrame, pd.DataFrame]:
    use_regress_out = pd.isnull(inputs.data_file)

    synthetic_data = load_synthetic_data(inputs.outcome_path)
    if use_regress_out:
        features = load_deconfounder_residuals(inputs.features_path)
        demographics = None
        subst_conf = None
    else:
        patient_data = load_patient_data(inputs.data_file)
        features = pd.concat((patient_data.volumes, patient_data.thickness), axis=1)
        subst_conf = pd.read_hdf(inputs.features_path, key="substitute_confounder")
        demographics = patient_data.demographics

    fitter = OutcomeModelFitter(
        scale_beta=1.0,
        use_regress_out=use_regress_out,
        posterior_samples=1500,
        random_state=RANDOM_STATE,
        n_jobs=1,
    )
    coef_est = fitter.fit_substitute_confounders(
        features,
        synthetic_data.outcome,
        demographics=demographics,
        substitute_confounders=subst_conf,
    )

    outputs = _name_coef_axis(inputs, synthetic_data.coef, coef_est, prefix="")

    return outputs

def eval_estimates(base_dir: Path, coefs: Tuple[pd.DataFrame, pd.DataFrame]) -> None:
    out_file = base_dir / f"{coefs[0].index.name}" / f"eval_{coefs[1].index.name}"

    if not out_file.exists():
        coef_true, coef_est = coefs
        coef_true = coef_true.loc[:, "coefficient"].drop("Intercept")
        data = evaluate(coef_true, coef_est)
        data.index.name = "+".join([coef_true.index.name, coef_est.index.name])

        LOG.info("Writing result to %s", out_file)
        with open(out_file, "wb") as fout:
            pickle.dump(data, fout, protocol=pickle.HIGHEST_PROTOCOL)
    else:
        LOG.info("Result %s already exists", out_file)

def fit_and_eval_substitute_confounders(args):
    for cfg in _get_substitute_input_data(args.models_dir, args.features_dir, args.data_file):
        coefs = fit_substitute_model(cfg)
        out_file = _write_coefficients(args.models_dir, cfg.output_dir, coefs)
        eval_estimates(args.models_dir, coefs)

def _get_input_data(
    base_dir_path: Path, data_path: Path, result_prefix: str,
) -> Iterable[ModelInput]:
    for sim_path in _get_generated_data(base_dir_path):
        out_dir = str(sim_path.parent.relative_to(base_dir_path))
        result_file = sim_path.with_name(f"coefs_{result_prefix}_{data_path.stem}")
        if not result_file.exists():
            data = ModelInput(data_path, sim_path, out_dir)
            yield data

def _get_fitter():
    fitter = OutcomeModelFitter(
        scale_beta=1.0,
        use_regress_out=False,
        posterior_samples=1500,
        random_state=RANDOM_STATE,
        n_jobs=2,
    )
    return fitter

def fit_oracle_model(inputs: ModelInput) -> Tuple[pd.DataFrame, pd.DataFrame]:
    patient_data = load_patient_data(inputs.features_path)
    synthetic_data = load_synthetic_data(inputs.outcome_path)

    features = pd.concat((patient_data.volumes, patient_data.thickness), axis=1)

    fitter = _get_fitter()
    coef_est = fitter.fit_oracle(
        features=features,
        outcome=synthetic_data.outcome,
        demographics=patient_data.demographics,
        unobserved_confounder=synthetic_data.confounders.loc[:, "unobserved_confounder"],
    )

    outputs = _name_coef_axis(inputs, synthetic_data.coef, coef_est, prefix="oracle_")
    return outputs

def fit_and_eval_oracle_model(args):
    for cfg in _get_input_data(args.models_dir, args.data_file, result_prefix="oracle"):
        coefs = fit_oracle_model(cfg)
        out_file = _write_coefficients(args.models_dir, cfg.output_dir, coefs)
        eval_estimates(args.models_dir, coefs)

def fit_standard_model(inputs: ModelInput) -> Tuple[pd.DataFrame, pd.DataFrame]:
    patient_data = load_patient_data(inputs.features_path)
    synthetic_data = load_synthetic_data(inputs.outcome_path)

    features = pd.concat((patient_data.volumes, patient_data.thickness), axis=1)

    fitter = _get_fitter()
    coef_est = fitter.fit_standard(
        features=features,
        outcome=synthetic_data.outcome,
    )

    outputs = _name_coef_axis(inputs, synthetic_data.coef, coef_est, prefix="noconf_")
    return outputs

def fit_and_eval_no_confounders(args):
    for cfg in _get_input_data(args.models_dir, args.data_file, result_prefix="noconf"):
        coefs = fit_standard_model(cfg)
        out_file = _write_coefficients(args.models_dir, cfg.output_dir, coefs)
        eval_estimates(args.models_dir, coefs)

def fit_observed_model(inputs: ModelInput) -> Tuple[pd.DataFrame, pd.DataFrame]:
    patient_data = load_patient_data(inputs.features_path)
    synthetic_data = load_synthetic_data(inputs.outcome_path)

    features = pd.concat((patient_data.volumes, patient_data.thickness), axis=1)

    fitter = _get_fitter()
    coef_est = fitter.fit_observed_confounders(
        features=features,
        outcome=synthetic_data.outcome,
        demographics=patient_data.demographics,
    )

    outputs = _name_coef_axis(inputs, synthetic_data.coef, coef_est, prefix="obsconf_")
    return outputs

def fit_and_eval_observed_confounders(args):
    for cfg in _get_input_data(args.models_dir, args.data_file, result_prefix="obsconf"):
        coefs = fit_observed_model(cfg)
        out_file = _write_coefficients(args.models_dir, cfg.output_dir, coefs)
        eval_estimates(args.models_dir, coefs)

def main(args=None):
    import argparse

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers()

    p = subparsers.add_parser("subst_conf_models")
    p.add_argument(
        "--features_dir", type=Path, required=True, help="Path to directory where HDF5 files with substitute confounder are located."
    )
    p.add_argument(
        "--models_dir", type=Path, required=True, help="Path to directory containing fitted models."
    )
    p.add_argument(
        "--data_file", type=Path, help="Path to HDF5 file containing original data."
    )
    p.set_defaults(func=fit_and_eval_substitute_confounders)

    p = subparsers.add_parser("oracle_model")
    p.add_argument(
        "--data_file", type=Path, required=True, help="Path to HDF5 file containing original data."
    )
    p.add_argument(
        "--models_dir", type=Path, required=True, help="Path to directory containing fitted models."
    )
    p.set_defaults(func=fit_and_eval_oracle_model)

    p = subparsers.add_parser("no_confounders")
    p.add_argument(
        "--data_file", type=Path, required=True, help="Path to HDF5 file containing original data."
    )
    p.add_argument(
        "--models_dir", type=Path, required=True, help="Path to directory containing fitted models."
    )
    p.set_defaults(func=fit_and_eval_no_confounders)

    p = subparsers.add_parser("observed_confounders")
    p.add_argument(
        "--data_file", type=Path, required=True, help="Path to HDF5 file containing original data."
    )
    p.add_argument(
        "--models_dir", type=Path, required=True, help="Path to directory containing fitted models."
    )
    p.set_defaults(func=fit_and_eval_observed_confounders)

    args = parser.parse_args(args=args)

    args.func(args)

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    main()