from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    username = models.CharField(blank=True, null=True, max_length=50)
    # Overwrtiting to make it available to have a blank username, because the email field will be the main field
    # used for authentication, for easier maintenanace
    email = models.EmailField("email address", unique=True)
    # We have to overwrite this field from an AbstractUser model,
    # to make the 'unique' constraint - so it will be available for using for the authentication
    USERNAME_FIELD = "email"
    # That means that this field will be used for authentication
    REQUIRED_FIELDS = ["username", "first_name", "last_name"]
    # Fields required by 'createsuperuser' command, username has to be there, otherwise Django will complain

    def __str__(self):
        return "{}".format(self.email)

class CustomUserProfile(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="profile"
    )
    date_of_birth = models.DateField(null=True, blank=True)
    country = models.CharField(max_length=50, blank=True)
    city = models.CharField(max_length=50, blank=True)
    bio = models.TextField(max_length=500, blank=True)
    photo = models.ImageField(upload_to="uploads", blank=True)

class Article(models.Model):
    title = models.CharField(max_length=255)
    content = models.TextField()
    author = models.ForeignKey(
        CustomUser, on_delete=models.DO_NOTHING, related_name="articles"
    )
    created_at = models.DateTimeField(auto_now_add=True)


import tensorflow as tf

from datasets.cifar10 import Cifar10 as Dataset

EMBEDDING_SIZE = 3

class Cifar10:

    def __init__(self, learning_rate):
        self.learning_rate = learning_rate

    """--------------------------------------------------------------------------------------------------------------"""

    @staticmethod
    def architecture(features, params):
        with tf.variable_scope(params.scopes['net_scope']):
            images = tf.cast(features, tf.float32)
            # images = features
            # Input Layer
            with tf.name_scope('Data'):
                input_layer = tf.reshape(images, [-1, Dataset.IMG_SIZE, Dataset.IMG_SIZE, Dataset.NUM_CHANNELS])

            # Convolutional Layer 1
            with tf.variable_scope('Conv1'):
                conv1 = tf.layers.conv2d(inputs=input_layer, filters=32, kernel_size=[3, 3], padding="same", activation=tf.nn.elu)
                pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=1)

            tf.logging.info('Convolutional Layer 1 build successful..')

            # Convolutional Layer 2
            with tf.variable_scope('Conv2'):
                conv2 = tf.layers.conv2d(inputs=pool1, filters=64, kernel_size=[3, 3], padding="same", activation=tf.nn.elu)
                pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=1)

            tf.logging.info('Convolutional Layer 2 build successful..')

            # Convolutional Layer 3
            with tf.variable_scope('Conv3'):
                conv3 = tf.layers.conv2d(inputs=pool2, filters=128, kernel_size=[3, 3], padding="same", activation=tf.nn.elu)
                pool3 = tf.layers.max_pooling2d(inputs=conv3, pool_size=[2, 2], strides=2)

            tf.logging.info('Convolutional Layer 3 build successful..')

            # Convolutional Layer 4
            with tf.variable_scope('Conv4'):
                conv4 = tf.layers.conv2d(inputs=pool3, filters=256, kernel_size=[3, 3], padding="same",
                                         activation=tf.nn.elu)
                pool4 = tf.layers.max_pooling2d(inputs=conv4, pool_size=[2, 2], strides=2)

            tf.logging.info('Convolutional Layer 4 build successful..')

            # Fully Connected Layer
            with tf.variable_scope('FC'):
                pool2_flat = tf.reshape(pool4, [-1, 7 * 7 * 256])
                dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.elu)

            tf.logging.info('Fully Connected Layer build successful..')

            """ ---------------------------------------------------------------------------------------------------- """

            # Embeddings Layer
            with tf.variable_scope(params.scopes['emb_scope']):
                l2_norm = tf.nn.l2_normalize(dense, dim=1, name='l2norm')

                tf.summary.histogram("l2norm", l2_norm)

                embeddings = tf.layers.dense(inputs=l2_norm, kernel_initializer=tf.initializers.random_normal,
                                             kernel_regularizer=tf.contrib.layers.l2_regularizer,
                                             units=EMBEDDING_SIZE, name=params.scopes['emb_name'])

                tf.summary.histogram(params.scopes['emb_name'], embeddings)

                tf.logging.info('Embeddings Layer build successful..')

            """ ---------------------------------------------------------------------------------------------------- """

            # Logits Layer
            with tf.variable_scope('LogitsLayer'):
                logits = tf.layers.dense(inputs=embeddings, units=10)

                tf.summary.histogram('logits', logits)

                tf.logging.info('Logits Layer build successful..')

        return logits

    @staticmethod
    def get_estimator_spec(features, labels, mode, params):

        logits = Cifar10.architecture(features, params)

        # Metrics Layer
        with tf.variable_scope(params.scopes['metrics_scope']):
            # Generate predictions (for PREDICT and EVAL mode)
            predictions = {
                "classes": tf.argmax(input=logits, axis=1),
                # Add `softmax_tensor` to the graph. It is used for PREDICT.
                "probabilities": tf.nn.softmax(logits, name="softmax_tensor")
            }

            tf.logging.info('Predictions build successful..')

            # Calculate Loss (for both TRAIN and EVAL modes)
            labels = tf.identity(labels, name=params.scopes['label_name'])
            one_hot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=10)
            loss = tf.losses.softmax_cross_entropy(onehot_labels=one_hot_labels, logits=logits)

            tf.summary.histogram('loss', loss)

            tf.logging.info('Losses build successful..')

            learning_rate = tf.train.exponential_decay(
                params.learning_rate,  # Base learning rate.
                tf.train.get_global_step(),  # Current index into the dataset.
                1000,  # Decay step.
                0.75,  # Decay rate.
                staircase=True,
                name='learning_rate'
            )

            tf.summary.histogram('learning_rate', learning_rate)

            tf.logging.info('Learning rate build successful..')

            optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
            # optimizer = tf.train.RMSPropOptimizer(learning_rate=learning_rate)
            train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())

            # Add evaluation metrics (for EVAL mode)
            accuracy = tf.metrics.accuracy(labels=labels, predictions=predictions["classes"], name='accuracy')

            tf.summary.histogram('accuracy', accuracy)

            tf.logging.info('Accuracy metric build successful..')

            return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op,
                                              predictions=predictions,
                                              eval_metric_ops={"accuracy": accuracy},
                                              scaffold=tf.train.Scaffold(
                                                  summary_op=tf.summary.merge_all()
                                              ))


vowel="aeiou"
str=input("enter the string ")
#str ="AaaAnciob"
str=str.lower()
print(str)
i=0
temp=""
temp2=""
while(i<len(str)):
    if str[i] in vowel:
        temp=temp+str[i]
    else:
        temp2 = temp2 if len(temp2)>len(temp) else temp
        temp=""
    i=i+1

print(temp2)


#!/usr/bin/env python

import rospy
from geometry_msgs.msg import PoseStamped
from styx_msgs.msg import Lane, Waypoint
from std_msgs.msg import Int32
from scipy import spatial
import numpy as np
import math

'''
This node will publish waypoints from the car's current position to some `x` distance ahead.

As mentioned in the doc, you should ideally first implement a version which does not care
about traffic lights or obstacles.

Once you have created dbw_node, you will update this node to use the status of traffic lights too.

Please note that our simulator also provides the exact location of traffic lights and their
current status in `/vehicle/traffic_lights` message. You can use this message to build this node
as well as to verify your TL classifier.

TODO (for Yousuf and Aaron): Stopline location for each traffic light.
'''

LOOKAHEAD_WPS = 100 # Number of waypoints we will publish. You can change this number
MAX_DECEL = 0.5
LOOP_RATE = 5

class WaypointUpdater(object):
    def __init__(self):

        #rospy.init_node('waypoint_updater',log_level=rospy.DEBUG)
        rospy.init_node('waypoint_updater')

        self.final_waypoints_pub = rospy.Publisher('/final_waypoints', Lane, queue_size=1)

        self.base_waypoints = None
        self.waypoints_2d   = None
        self.waypoint_tree  = None
        self.pose           = None

        self.stopline_idx = -1
        self.obstacle_idx = -1
        self.stop_buffer  = 2

        rospy.Subscriber('/base_waypoints', Lane, self.waypoints_cb)
        rospy.Subscriber('/current_pose', PoseStamped, self.pose_cb, queue_size=10)

        # these recieve only a waypoint index that refers back to the self.base_waypoints list
        rospy.Subscriber('/traffic_waypoint', Int32, self.traffic_cb, queue_size=10)

        # optional:
        #rospy.Subscriber('/obstacle_waypoint', Int32, self.obstacle_cb)

        self.loop()

    def loop(self):

        rate = rospy.Rate(LOOP_RATE)
        while not rospy.is_shutdown():
            if self.pose and self.base_waypoints and self.waypoint_tree:
                closest_waypoint_idx = self.get_closest_waypoint_idx()
                #rospy.logerr("cwp %s",closest_waypoint_idx)
                self.publish_waypoints(closest_waypoint_idx)
            rate.sleep()

    def get_closest_waypoint_idx(self):

        x = self.pose.pose.position.x
        y = self.pose.pose.position.y

        if self.waypoint_tree == None:
            rospy.logerr("error: get_closest_waypoint_idx - waypoint_tree (kdtree) not assigned")
            return

        closest_idx = self.waypoint_tree.query([x,y],1)[1]

        # ahead or behind?
        closest_coord = self.waypoints_2d[closest_idx]
        prev_coord = self.waypoints_2d[closest_idx-1]

        # Equation for hyperplane through closest_coords
        cl_vect = np.array(closest_coord)
        prev_vect = np.array(prev_coord)
        pos_vect = np.array([x, y])

        val = np.dot(cl_vect - prev_vect, pos_vect - cl_vect)

        if val > 0:
            closest_idx = (closest_idx + 1) % len(self.waypoints_2d)

        return closest_idx

    def publish_waypoints(self, closest_idx):

        #rospy.logerr("--- closest: %s: ",closest_idx)

        lane = Lane()
        lane.header = self.base_waypoints.header

        if self.base_waypoints.waypoints == None:
            rospy.logerr("error: self.base_waypoints.waypoints== none!")
            return

        size = len(self.base_waypoints.waypoints)
        sl_idx = self.stopline_idx

        lane.waypoints = self.base_waypoints.waypoints[closest_idx:closest_idx+LOOKAHEAD_WPS]

        if sl_idx >= 0 and sl_idx < size:
            #rospy.logerr("decelerate waypoint")
            lane.waypoints = self.decelerate_waypoints(closest_idx,lane.waypoints)

        self.final_waypoints_pub.publish(lane)

    def pose_cb(self, msg):
        self.pose = msg

    def waypoints_cb(self, waypoints):

        #rospy.logerr("wp updater: waypoints_cp")
        size = len(waypoints.waypoints)

        # make a copy as they are only sent once
        self.base_waypoints = waypoints

        #use scipi KDTree to get closes waypoint
        if not self.waypoints_2d:
             self.waypoints_2d = [[waypoint.pose.pose.position.x,waypoint.pose.pose.position.y] for waypoint in waypoints.waypoints]
             self.waypoint_tree = spatial.KDTree(self.waypoints_2d)
        else:
            rospy.logerr("self.waypoints_2d already assigned?: %s",self.waypoints_2d)

    def traffic_cb(self, msg):
        self.stopline_idx = msg.data
        #rospy.logerr("waypoint_updater: way point index %s ",self.stopline_idx)

    # def obstacle_cb(self, msg):
    #     self.obstacle_idx = msg

    def get_waypoint_velocity(self, waypoint):
        return waypoint.twist.twist.linear.x

    def set_waypoint_velocity(self, waypoints, waypoint, velocity):
        waypoints[waypoint].twist.twist.linear.x = velocity

    def distance(self, waypoints, wp1, wp2):
        dist = 0
        dl = lambda a, b: math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2  + (a.z-b.z)**2)
        for i in range(wp1, wp2+1):
            dist += dl(waypoints[wp1].pose.pose.position, waypoints[i].pose.pose.position)
            wp1 = i
        return dist

    def decelerate_waypoints(self,closest_idx,wp_in):

        wp_out = []

        for i, wp in enumerate(wp_in):

            p = Waypoint()
            p.pose = wp.pose

            stop_idx = max(self.stopline_idx - closest_idx - self.stop_buffer,0)

            dist = self.distance(self.base_waypoints.waypoints, i, stop_idx)
            vel = math.sqrt(2*MAX_DECEL*dist)
            if vel < 1.0:
                vel = 0

            p.twist.twist.linear.x = min(vel, wp.twist.twist.linear.x)
            wp_out.append(p)

        return wp_out

if __name__ == '__main__':
    try:
        WaypointUpdater()
    except rospy.ROSInterruptException:
        rospy.logerr('Could not start waypoint updater node.')


import sys

sys.path.append("/usr/local/lib/python3.9/site-packages")
from xlwt import Workbook

wb = Workbook()
sheet = wb.add_sheet('Order Book', cell_overwrite_ok=True)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

s = Service(ChromeDriverManager().install())

from bs4 import BeautifulSoup
import secrets
import json

from websocket import create_connection

from binance.client import Client
from binance.enums import *
from binance.exceptions import BinanceAPIException, BinanceOrderException
import math

import time

blacklist = ["REEFBTC", "CELRBTC", "VIBBTC", "ETCBTC", "COSBTC", "PHBBTC", "DGBBTC", "ADABTC", "ETHBTC", "SOLBTC",
             "MATICBTC"]

sheet.write(0, 0, "Coin")
sheet.write(0, 1, "Qty")
sheet.write(0, 3, "Buy Price")
sheet.write(0, 4, "Sell Price")
sheet.write(0, 6, "Pnl amount")
sheet.write(0, 7, "Pnl %")
sheet.write(0, 9, "Capital Used")

sheet.write(0, 11, "Recent Vol %")
sheet.write(0, 12, "Pings")
sheet.write(0, 13, "Net Vol %")

sheet.write(0, 15, "Buy time")
sheet.write(0, 16, "Sell time")

client = Client("QoukvJkask8R91Ql1A122wtuW1IykLIDcmBYFdav1ftrqEyPY5XkrtAOTqkZDP3l",
                "EC1kzfy9K8R5hqyJHYEEPdfvrRNHLYa1oqDhi17svEXutjWy4HCYRVQndHlVWYfr")

profit_percentage = 1
stoploss_percentage = 1.5
trail_stoploss_percentage = 0.2

coin_recent_vol_percentage = 0
coin_pings = 0
coin_net_vol_percentage = 0

op = webdriver.ChromeOptions()
op.add_argument('--headless')

driver = webdriver.Chrome(options=op, service=s)

profit = 0
file_log = open("guru.txt", "w+")
prcount = 0
lscouunt = 0

SIDE_BUY = 'BUY'
SIDE_SELL = 'SELL'

ORDER_TYPE_LIMIT = 'LIMIT'
ORDER_TYPE_MARKET = 'MARKET'
ORDER_TYPE_STOP_LOSS = 'STOP_LOSS'
ORDER_TYPE_STOP_LOSS_LIMIT = 'STOP_LOSS_LIMIT'
ORDER_TYPE_TAKE_PROFIT = 'TAKE_PROFIT'
ORDER_TYPE_TAKE_PROFIT_LIMIT = 'TAKE_PROFIT_LIMIT'
ORDER_TYPE_LIMIT_MAKER = 'LIMIT_MAKER'

TIME_IN_FORCE_GTC = 'GTC'  # Good till cancelled
TIME_IN_FORCE_IOC = 'IOC'  # Immediate or cancel
TIME_IN_FORCE_FOK = 'FOK'  # Fill or kill
TIME_IN_FORCE_GTX = 'GTX'  # Post only order

res = client.get_exchange_info()

def check_availability(order_coin_name):
    for item in res['symbols']:
        if item['symbol'] == order_coin_name.upper():
            return True
    return False

def truncate(f, n):
    return math.floor(f * 10 ** n) / 10 ** n

def get_percent(order_coin_name):
    tickers = client.get_ticker(symbol=order_coin_name)
    latest_percnt_change = float(tickers['priceChangePercent'])
    return latest_percnt_change

def get_quantity_in_precison(order_coin_name, qty):
    # print(client.get_account_snapshot())
    # print(" here ",client.get_symbol_info(order_coin_name),order_coin_name)
    dc = client.get_symbol_info(order_coin_name)
    n = int(dc['baseAssetPrecision'])
    # qty ="{:f}".format(truncate(qty,n))
    # print(qty,type(qty))
    step_size = 0
    filters = dc['filters']
    for f in filters:
        # print("filter~~~~~~~~~~~~ " + str(f))
        if f['filterType'] == 'LOT_SIZE':
            step_size = float(f['stepSize'])

    # bal = self._client.get_asset_balance(asset='BNB')
    # quantity = (float(bal['free']))/self._price*0.9995

    min_qty = client.get_symbol_info(order_coin_name)['filters'][5]['minQty']  # to get min qty of coin
    min_qty = float(min_qty)
    max_qty = client.get_symbol_info(order_coin_name)['filters'][5]['maxQty']  # max
    max_qty = float(max_qty)

    qty2 = float(qty)
    # print(min_qty,max_qty,qty)
    precise = len(str(min_qty).split('.')[1])
    qty2 = truncate(qty, precise)
    if step_size:
        precision = int(round(-math.log(step_size, 10), 0))
        quantity = float(truncate(qty, precision))
        return "{:f}".format(min(qty2, truncate(quantity, step_size)))
    return "{:f}".format(min(truncate(qty, 8), qty2))

def round_down(coin, number):
    number = float(number)
    info = client.get_symbol_info(coin)
    step_size = [float(_['stepSize']) for _ in info['filters'] if _['filterType'] == 'LOT_SIZE'][0]
    step_size = '%.8f' % step_size
    step_size = step_size.rstrip('0')
    decimals = len(step_size.split('.')[1])
    return math.floor(number * 10 ** decimals) / 10 ** decimals

def cancel_order(order_coin_name, oid):
    return client.cancel_order(symbol=order_coin_name, orderId=oid)

def get_filtered_price(order_coin_name, price):
    mp = client.get_symbol_info(order_coin_name)['filters'][0]['minPrice']
    decimalPts = len(mp.rstrip('0').split('.')[1])
    price = "{:f}".format(truncate(price, decimalPts))
    print("new limit price set ", price)
    return price

# def get_price(coin):
#      return float(client.get_ticker(symbol=coin)['lastPrice'])

def get_price(order_coin_name):
    # print("Check one")
    ws = create_connection("wss://stream.binance.com:9443/ws/" + order_coin_name.lower() + "@trade")
    result = ws.recv()
    result = json.loads(result)
    ws.close()
    # print("check two")
    return float(result['p'])

def get_free_asset(coin):
    balance = float(client.get_asset_balance(asset=coin)['free'])

    # balance ="{:f}".format(balance)
    return balance

def check_valid_qty(order_coin_name, qty):
    min_qty = client.get_symbol_info(order_coin_name)['filters'][5]['minQty']  # to get min qty of coin
    min_qty = float(min_qty)
    max_qty = client.get_symbol_info(order_coin_name)['filters'][5]['maxQty']  # max
    max_qty = float(max_qty)

    qty = float(qty)
    # print(min_qty,max_qty,qty)
    precise = len(str(min_qty).split('.')[1])
    qty = truncate(qty, precise)
    if min_qty >= qty:
        return False
    if max_qty < qty:
        return False

    return True

def average_of_market_order(fills):
    qty = 0
    tcost = 0
    for trade in fills:
        qty += float(trade['qty'])
        tcost += float(trade['qty']) * float(trade['price'])

    return tcost / qty

def convert_volume(coin, quantity, last_price):
    """Converts the volume given in QUANTITY from BTC to the each coin's volume"""

    quantity = float(quantity)
    last_price = float(last_price)
    try:
        info = client.get_symbol_info(coin)
        step_size = info['filters'][2]['stepSize']
        lot_size = {coin: step_size.index('1') - 1}

        if lot_size[coin] < 0:
            lot_size[coin] = 0

    except:
        print("Ran except block for lot size")
        lot_size = {coin: 0}
        pass

    # print(lot_size[coin])
    # calculate the volume in coin from QUANTITY in USDT (default)
    volume = float(quantity / float(last_price))

    # define the volume with the correct step size
    if coin not in lot_size:
        volume = float('{:.1f}'.format(volume))

    else:
        # if lot size has 0 decimal points, make the volume an integer
        if lot_size[coin] == 0:
            volume = int(volume)
        else:
            volume = float('{:.{}f}'.format(volume, lot_size[coin]))
    #  print(volume,float(info['filters'][2]['minQty'])," inside convert")
    #  volume=max(volume,float(info['filters'][2]['minQty']))
    return volume

def create_buy_order(coin, qty):
    """
    Creates simple buy order and returns the order
    """
    return client.create_order(
        symbol=coin,
        side=SIDE_BUY,
        type='MARKET',
        quantity=qty
    )

def create_sell_order(coin, qty):
    """
    Creates simple buy order and returns the order
    """
    return client.create_order(
        symbol=coin,
        side=SIDE_SELL,
        type='MARKET',
        quantity=qty
    )

def create_limit_sell_order(coin, qty, pr):
    """
    Creates simple buy order and returns the order
    """
    # print(qty,type(qty))
    return client.create_order(
        symbol=coin,
        side=SIDE_SELL,
        type=ORDER_TYPE_LIMIT,
        quantity=qty,
        timeInForce=TIME_IN_FORCE_GTC,
        price=pr
    )

def create_limit_buy_order(coin, qty, pr):
    """
    Creates simple buy order and returns the order
    """
    # print(qty,type(qty))
    return client.create_order(
        symbol=coin,
        side=SIDE_BUY,
        type=ORDER_TYPE_LIMIT,
        quantity=qty,
        timeInForce=TIME_IN_FORCE_GTC,
        price=pr
    )

def get_order_status(order_coin_name, target_order_id):
    return client.get_order(symbol=order_coin_name, orderId=target_order_id)["status"]

def run_starter():
    global client

    client = Client("QoukvJkask8R91Ql1A122wtuW1IykLIDcmBYFdav1ftrqEyPY5XkrtAOTqkZDP3l",
                    "EC1kzfy9K8R5hqyJHYEEPdfvrRNHLYa1oqDhi17svEXutjWy4HCYRVQndHlVWYfr")

    op = webdriver.ChromeOptions()
    op.add_argument('--headless')
    global driver

    driver = webdriver.Chrome(options=op, service=s)
    global SIDE_BUY
    SIDE_BUY = 'BUY'

    global SIDE_SELL
    SIDE_SELL = 'SELL'

    global ORDER_TYPE_LIMIT
    ORDER_TYPE_LIMIT = 'LIMIT'
    global ORDER_TYPE_MARKET
    ORDER_TYPE_MARKET = 'MARKET'
    global ORDER_TYPE_STOP_LOSS
    ORDER_TYPE_STOP_LOSS = 'STOP_LOSS'

    global TIME_IN_FORCE_GTC
    TIME_IN_FORCE_GTC = 'GTC'  # Good till cancelled

    global TIME_IN_FORCE_IOC
    TIME_IN_FORCE_IOC = 'IOC'  # Immediate or cancel

    global TIME_IN_FORCE_FOK
    TIME_IN_FORCE_FOK = 'FOK'  # Fill or kill

    global TIME_IN_FORCE_GTX
    TIME_IN_FORCE_GTX = 'GTX'  # Post only order

    global profit

    profit = 0
    global prcount
    prcount = 0
    global btc_price
    btc_price = get_price("BTCUSDT")
    global lscouunt
    lscouunt = 0
    global file_log
    file_log = open("guru.txt", "w+")

def get_token_to_be_baught():
    print("finding tokens")

    driver.get("http://agile-cliffs-23967.herokuapp.com/binance")

    content = driver.page_source
    try:
        soup = BeautifulSoup(content, "html5lib")
    except Exception as e:
        print(str(e))

    ls = []
    #     print("here")
    for a in soup.findAll('tr', attrs={'class': 'coin'}):
        # print(a)
        ln = a.find('a')
        link = ln.get("href")
        pair = link.split("/")[-1]
        newlink = link[:-3] + "USDT"
        print(pair, newlink)
        count = 0
        pings = 0
        net_vol_percentage = 0
        recent_vol_percentage = 0
        recent_net_volume = 0

        coin_symbol, base_coin = pair.split("_")
        base_coin = "USDT"
        coin_name = coin_symbol + base_coin

        if not check_availability(coin_name):
            continue

        tickers = client.get_ticker(symbol=coin_name)
        percent_change_price = tickers['priceChangePercent']
        percent_change_price = float(percent_change_price)

        for dt in a.find_all("th"):
            #
            new_data = dt.text.replace('\n', ' ').strip()
            # print(new_data)
            if count == 1:
                pings = int(new_data)
            elif count == 2:
                net_vol_btc = float(new_data)
            elif count == 3:
                net_vol_percentage = float(new_data[:-1])
            elif count == 5:
                recent_vol_percentage = float(new_data[:-1])
            elif count == 6:
                recent_net_volume = float(new_data)
                # print(recent_net_volume)

            count += 1

        print(coin_name)
        if (recent_vol_percentage > 0.7 and pings > 3 and pings < 10 and net_vol_percentage > 0.8):
            print("chosen ", pair)
            ls.append([coin_name, percent_change_price, coin_symbol, base_coin, recent_vol_percentage, pings,
                       net_vol_percentage])

    # ls.sort(key=lambda x:-x[1])

    # print("returning list: ", ls)
    return ls

btc_price = 60000  # get_price("BTCUSDT")

# run_starter()
row = 1
pnl = 0
total_pnl_amount = 0

def placeBuyOrderExcel(coin, qty, price):
    global pnl
    pnl = qty * price
    sheet.write(row, 0, coin)
    sheet.write(row, 1, qty)
    sheet.write(row, 3, price)
    sheet.write(row, 9, price * qty)  # capital used

    sheet.write(row, 11, coin_recent_vol_percentage)
    sheet.write(row, 12, coin_pings)
    sheet.write(row, 13, coin_net_vol_percentage)

    t = time.localtime()
    sheet.write(row, 15, time.strftime("%H:%M:%S", t))
    wb.save('orderBookExcel.xls')

def placeSellOrderExcel(coin, qty, price):
    global row
    global pnl
    global total_pnl_amount
    pnlAmount = qty * price - pnl
    pnlPercentage = pnlAmount / pnl * 100

    sheet.write(row, 4, price)
    sheet.write(row, 6, pnlAmount)
    sheet.write(row, 7, pnlPercentage)

    t = time.localtime()
    sheet.write(row, 16, time.strftime("%H:%M:%S", t))

    total_pnl_amount += pnlAmount
    sheet.write(row + 1, 6, total_pnl_amount)  # total of pnl in last row
    row += 1
    wb.save('orderBookExcel.xls')

#############################################################################################################################################################
########################################################################   BOT FUNCTION   ###################################################################
#############################################################################################################################################################
def bot():
    global prcount
    prcount = 0

    global limit_order_count
    limit_order_count = 0
    order_number = 1

    wait_for_long = 0
    print("\n=========================  BOT Started  =========================\n")
    global coin_recent_vol_percentage
    global coin_pings
    global coin_net_vol_percentage

    while True:

        ls = get_token_to_be_baught()
        if not ls:
            print("Wait 5 sec")
            time.sleep(5)
            continue
        else:
            print("received list: ", ls)
            taken_secret = secrets.choice(ls)
            order_coin_name = taken_secret[0]
            base_coin = taken_secret[3]
            coin_recent_vol_percentage = taken_secret[4]
            coin_pings = taken_secret[5]
            coin_net_vol_percentage = taken_secret[6]

        available_USDT = 25  # get_free_asset("USDT")
        print(available_USDT)
        usage_for_first_trade = 1
        if base_coin == "BTC":
            amount_allotted = float(get_free_asset("BTC")) * usage_for_first_trade
        else:
            amount_allotted = available_USDT * usage_for_first_trade

        quote_length = 3
        if order_coin_name[-4:] == "USDT":
            quote_length = 4

        quantity = 0

        float_coin_price = get_price(order_coin_name)
        quantity = convert_volume(order_coin_name, amount_allotted, float_coin_price)
        base_quantity = quantity

        if not check_valid_qty(order_coin_name, base_quantity):
            print("Re enter Qty not following min max")
            # file_log.write("\n "+str(e))
            continue

        try:
            global original_limit
            placeBuyOrderExcel(order_coin_name, base_quantity, float_coin_price)
            print("bought " + order_coin_name + " avg buy price " + str(float_coin_price))
            startTime = time.time()

            original_limit = float_coin_price * (1 + profit_percentage / 100)

            file_log.write(
                "\n \nOrder " + str(order_number) + " placed :" + order_coin_name + " average buy price " + str(
                    float_coin_price) + " Limit sell at " + str(float_coin_price * (1 + profit_percentage / 100)))
            file_log.write(
                "\nCoin parameters: Recent Volume % = " + str(coin_recent_vol_percentage) + " Pings = " + str(
                    coin_pings) + " Net Volume % = " + str(coin_net_vol_percentage))
            file_log.flush()
            order_number += 1

        except Exception as e:
            print(e)
            file_log.write("\n " + str(e))
            run_starter()
            continue

        limit_price = float_coin_price * (1 + profit_percentage / 100)

        sold = 0
        limit_order_count = 0
        time_start = time.time()
        net_quantity = base_quantity
        while not sold:
            # print("INSIDE TRADE")
            time.sleep(1)
            lastPrice = get_price(order_coin_name)

            print("Avg buy ", float_coin_price, " Current price ", lastPrice, " Limit sell at ", limit_price,
                  "Stoploss at ", (1 - stoploss_percentage / 100) * float_coin_price)

            if lastPrice >= limit_price:

                print("inside profit cell ")
                file_log.write("\n *** Inside profit cell  ***")
                file_log.flush()
                prev_last_price = limit_price

                # lastPrice=get_price(order_coin_name)
                # print(limit_price,lastPrice)
                flag = 0
                maxPrice = lastPrice
                try:
                    while True:
                        time.sleep(1)
                        lastPrice = get_price(order_coin_name)
                        print("\nInside profit : Avg buy ", float_coin_price, " Current price ", lastPrice,
                              " Limit sell at ", maxPrice)
                        if lastPrice > limit_price:
                            limit_price = lastPrice
                            maxPrice = max(lastPrice, maxPrice)
                            file_log.write("\nLimit trailed to :" + str(limit_price))
                            file_log.flush()

                        elif lastPrice < original_limit * 0.998 or lastPrice < maxPrice - (maxPrice-original_limit) * 0.20:
                            file_log.write("max price = "+str(maxPrice)+"\nTrail stoploss hit ✅, Placing sell order now!" + str(limit_price))
                            file_log.flush()
                            break
                        elif time.time() - startTime > 60*60*2:
                            file_log.write("max price = "+str(maxPrice)+"\nTime limit exceeded while trailing, Placing sell order now!" + str(limit_price))
                            file_log.flush()
                            break

                    placeSellOrderExcel(order_coin_name, net_quantity, lastPrice)
                    # print("executed")
                    selling_price = maxPrice

                    file_log.write("\n*** ❤profit❤  ***  bought at " + str(float_coin_price) + " sold at " + str(
                        lastPrice) + " **** \n")
                    file_log.write("\n\nTotal pnl till now is : ====>> " + str(total_pnl_amount) + " $\n")
                    file_log.flush()
                    print("\n* profit bought at " + str(float_coin_price) + " sold at " + str(lastPrice) + " *")
                    sold = 1

                    time.sleep(2)
                    continue

                except Exception as e:
                    print(e)
                file_log.flush()
                prcount += 1

            elif limit_order_count == 0 and float(lastPrice) < float_coin_price - (float_coin_price * 1 / 100):
                limit_order_count += 1
                try:
                    file_log.write("\n updating limit sell order" + str(limit_order_count))
                    file_log.flush()
                except Exception as e:
                    # run_starter()
                    file_log.write("\n " + str(e))
                    print(e)

                quantity_so_far = quantity
                quantity = base_quantity * 2
                net_quantity = quantity_so_far + quantity
                float_coin_price = (lastPrice * quantity + float_coin_price * quantity_so_far) / (net_quantity)
                placeBuyOrderExcel(order_coin_name, net_quantity, float_coin_price)
                limit_price = 1.01 * float_coin_price
                print("\nLimit 1  avg buy ", float_coin_price, " current price ", lastPrice, " limit sell at ",
                      limit_price)
                file_log.write(
                    "\n Limit 1  avg buy price " + str(float_coin_price) + " current price " + str(lastPrice))
                file_log.flush()

            elif limit_order_count == 1 and float(lastPrice) < float_coin_price - (float_coin_price * 2 / 100):
                limit_order_count += 1
                try:
                    file_log.write("\n updating limit sell order" + str(limit_order_count))
                    file_log.flush()
                except Exception as e:
                    # run_starter()
                    file_log.write("\n " + str(e))
                    print(e)

                quantity_so_far = quantity
                quantity = base_quantity * 4
                net_quantity = quantity_so_far + quantity
                float_coin_price = (lastPrice * quantity + float_coin_price * quantity_so_far) / (net_quantity)
                limit_price = 1.01 * float_coin_price
                placeBuyOrderExcel(order_coin_name, net_quantity, float_coin_price)
                print("Limit 2  avg buy ", float_coin_price, " current price ", lastPrice, " limit sell at ",
                      limit_price)
                file_log.write(
                    "\n Limit 2  avg buy price " + str(float_coin_price) + " current price " + str(lastPrice))
                file_log.flush()

            elif limit_order_count == 2 and float(lastPrice) < float_coin_price - (float_coin_price * 4 / 100):
                limit_order_count += 1
                try:
                    file_log.write("\n updating limit sell order" + str(limit_order_count))
                    file_log.flush()
                except Exception as e:
                    # run_starter()
                    file_log.write("\n " + str(e))
                    print(e)

                quantity_so_far = quantity
                quantity = base_quantity * 8
                net_quantity = quantity_so_far + quantity
                float_coin_price = (lastPrice * quantity + float_coin_price * quantity_so_far) / (net_quantity)
                limit_price = 1.01 * float_coin_price
                placeBuyOrderExcel(order_coin_name, net_quantity, float_coin_price)
                print("\nLimit 3  avg buy ", float_coin_price, " current price ", lastPrice, " limit sell at ",
                      limit_price)
                file_log.write(
                    "\n Limit 3  avg buy price " + str(float_coin_price) + " current price " + str(lastPrice))
                file_log.flush()

            elif limit_order_count == 3 and float(lastPrice) < float_coin_price - (float_coin_price * 8 / 100):
                limit_order_count += 1
                try:
                    file_log.write("\n updating limit sell order" + str(limit_order_count))
                    file_log.flush()
                except Exception as e:
                    # run_starter()
                    file_log.write("\n " + str(e))
                    print(e)

                quantity_so_far = quantity
                quantity = base_quantity * 16
                net_quantity = quantity_so_far + quantity
                float_coin_price = (lastPrice * quantity + float_coin_price * quantity_so_far) / (net_quantity)
                limit_price = 1.01 * float_coin_price
                placeBuyOrderExcel(order_coin_name, net_quantity, float_coin_price)
                print("\nLimit 4  avg buy ", float_coin_price, " current price ", lastPrice, " limit sell at ",
                      limit_price)
                file_log.write(
                    "\n Limit 4  avg buy price " + str(float_coin_price) + " current price " + str(lastPrice))
                file_log.flush()

            if time.time() - startTime > 60*60*2:
                balance = get_free_asset(order_coin_name[:-quote_length])
                # balance=get_quantity_in_precison(order_coin_name,balance)
                # lastPrice=average_of_market_order(placed_order['fills'])
                print("\n--->Time limit exceeded  --> loss")
                placeSellOrderExcel(order_coin_name, net_quantity, lastPrice)
                file_log.write("\n--->Time limit exceeded --> loss ☹ ==> buy price " + str(
                    float_coin_price) + " sell price " + str(lastPrice))
                file_log.flush()
                sold = 1

run_starter()
try:

    bot()
except Exception as e:
    file_log.write(str(e))


from layout import Layout
from pos import Pos
from size import Size

import pygame
import colour
import styles
import orientation

class ListLayout(Layout):
    def __init__(
        self,
        size,
        pos=Pos(0,0),
        border=0,
        spacing=0,
        orientation=orientation.VERTICAL,
        scaling=styles.FREE):

        super(ListLayout, self).__init__(size, pos, border)
        self.next_child_origin = Pos(
            self.origin.x + self.pos.x + self.border,
            self.origin.y + self.pos.y + self.border
        )
        self.orientation = orientation
        self.spacing = spacing
        self.scaling = scaling

    def add(self, child):

        if self.scaling:
            self._rescale_layout(child)

        self.children.append(child)
        child.origin = self.next_child_origin

        self._update_child_pos(child)

    def render(self, surface):
        rect = (
            self.origin.x + self.pos.x,
            self.origin.y + self.pos.y,
            self.size.w,
            self.size.h
        )
        pygame.draw.rect(surface, colour.RED, rect)

        self._render_children(surface)

    def _rescale_layout(self, child):
        if not self.children:
            self.size = Size(
                child.size.w + 2*self.border,
                child.size.h + 2*self.border
            )
        else:
            self.size = Size(
                self.size.w + (child.size.w + self.spacing)*int(self.orientation),
                self.size.h + (child.size.h + self.spacing)*int(not self.orientation)
            )

    def _update_child_pos(self, child):
        self.next_child_origin = Pos(
            self.next_child_origin.x + (child.size.w + self.spacing)*int(self.orientation),
            self.next_child_origin.y + (child.size.h + self.spacing)*int(not self.orientation)
        )


# joan.samantha.main.py
# lorinda.kathryn.main.py
from _spy.vitollino.main import Cena,Elemento,Texto, STYLE
from _spy.vitollino.main import Inventario as inv

STYLE["width"] = 800
STYLE["height"] = "600px"

#CENA 1 VERBO LEMBRAR HEREDITARIEDADE

SALA_FIOCRUZ = "https://i.imgur.com/iUBolXC.jpg"
ROSALINDA = "https://i.imgur.com/qvuwHvs.png"
ZEZINHO = "https://i.imgur.com/94lhgKo.png"
NPC = " https://i.imgur.com/NEG2o8W.jpg"
PERGAMINHO = "https://i.imgur.com/Xe3zdhP.jpg"
PESSOA_BRANCA_CABELO_LISO_OLHOS_VERDES = "https://i.imgur.com/6vgubFm.jpg"
PESSOA_NEGRA_CABELO_BLACK_OLHOS_CASTANHOS = "https://i.imgur.com/wtpBiVS.jpg"
PESSOA_RUIVA_PELE_BRANCA_CABELO_VERMELHO = "https://i.imgur.com/ZcfLShe.jpg"
CRIANCA_BRANCA_CABELO_LISO_OLHOS_VERDES = "https://i.imgur.com/ISvWiD4.jpg"
CRIANCA_NEGRA_CABELO_BLACK_OLHOS_CASTANHOS = "https://i.imgur.com/qoGRKc8.jpg"
CRIANCA_RUIVA_PELE_BRANCA_CABELO_VERMELHO = "https://i.imgur.com/DtSbcLh.jpg"
QUADRO_1_PESSOA_BRANCA_CABELO_LISO_OLHOS_VERDES = "https://i.imgur.com/6vgubFm.jpg"
QUADRO_2_PESSOA_RUIVA_PELE_BRANCA_CABELO_VERMELHO = "https://i.imgur.com/6vgubFm.jpg"
QUADRO_3_PESSOA_RUIVA_PELE_BRANCA_CABELO_VERMELHO = "https://i.imgur.com/ZcfLShe.jpg"
QUADRO_4_CRIANCA_BRANCA_CABELO_LISO_OLHOS_VERDES = "https://i.imgur.com/ISvWiD4.jpg"
QUADRO_5_CRIANCA_NEGRA_CABELO_BLACK_OLHOS_CASTANHOS = "https://i.imgur.com/qoGRKc8.jpg"
QUADRO_6_CRIANCA_RUIVA_PELE_BRANCA_CABELO_VERMELHO = "https://i.imgur.com/DtSbcLh.jpg"
GENE0, GENE1 = "https://i.imgur.com/UAYR77L.jpg", "https://i.imgur.com/R4HE1ho.jpg"
GENE2, GENE3 = "https://i.imgur.com/ljpxkIt.jpg", "https://i.imgur.com/e3OfADM.jpg"
MUSEU_DA_VIDA = "https://i.imgur.com/s4ZBrRv.jpg"
DNA = "https://www.facebook.com/Projeto-SupyGirls-2062797764002029"
Genes = "https://i.imgur.com/SBcbyUc.gif"
Nucleotideos = "https://i.imgur.com/eUn2STG.png"
'''
Organismo
Sequência correta
Cofre
Abrir cofre

PenDrive

Poção mágica

Super poderes
'''

# O CÓDIGO DO MINE GAME ENTRA AQUI?

class MiniGame:
    """Usa um editor de imagem (https://www.online-image-editor.com/) e recorta a imagem em partes.
       No game, o jogador terá que clicar nas linhas em ordem certa para montar a figura corretamente.
    """
    def __init__(self, esta_cena, chama_quando_acerta, partes=(GENE3, GENE0, GENE2, GENE1)):
        posiciona_proxima = self.posiciona_proxima
        class LinhaGeracional:
            """Representa cada uma das linhas recortadas do herdograma original"""
            def __init__(self, linha, posicao):
                self.posicao = posicao # posição original no topo da página
                self.linha = Elemento(linha, x=posicao*200, y=20, w=175, h=125, cena=esta_cena)
                self.linha.vai = self.clica_e_posiciona_a_linha #quando clica, monta o herdograma
            def zera(self):
                self.linha.x = self.posicao*200  # posiciona cada peça com 200 pixels de distância
                self.linha.y = 20  # posiciona a peça no topo da página
                self.linha.vai = self.clica_e_posiciona_a_linha
            def clica_e_posiciona_a_linha(self, *_):
                x, y = posiciona_proxima(self.posicao)
                if y:  # se o y retornou zero é porque o posiciona próxima detectou montagem errada
                    self.linha.x, self.linha.y = x, y # monta a linha no herdograma
                    self.linha.vai = lambda *_:None #desativa o click da linha

        # coloca cada uma das linhas embaralhadas
        self.linhas = [
            LinhaGeracional(linha=uma_linha, posicao=uma_posicao)
            for uma_posicao, uma_linha in enumerate(partes)]
        self.acertou = chama_quando_acerta
        self.parte_inicial = -1
        self.altura_da_linha = 125  # cada peça do herdograma tem esta altura
        self.posicoes_montadas = []  #l ista das linhas já montadas no herdograma
        self.posicoes_corretas = [1, 3, 2, 0]  # lista das linhas montadas corretamente

    def posiciona_proxima(self, posicao):
        """Chamado pelo clique (vai) de cada peça. Atualiza a próxima posição da peça.
           Calcula se montou correto, comparando com a lista de posicões corretas.
           Se já montou quatro peças, e não acerto sinaliza com zero, para iniciar o jogo.
        """
        largura_da_peca, inicio_horizontal, inicio_vertical, numero_de_pecas = 175, 300, 200, 4
        numero_de_pecas_por_linha = 2
        self.parte_inicial += 1  # incrementa a posição para montar a próxima posiçao da peça
        self.posicoes_montadas += [posicao]  # adiciona o índice desta peça na lista de peças montadas
        if self.posicoes_montadas == self.posicoes_corretas:
            self.acertou()  # invoca a ação acertou se montou nas posições corretas
            return (inicio_horizontal+largura_da_peca*(self.parte_inicial%numero_de_pecas_por_linha),
                    inicio_vertical+self.altura_da_linha*(self.parte_inicial//numero_de_pecas_por_linha))
        else:
            if len(self.posicoes_montadas) == numero_de_pecas:  # se montou qutro peças incorretas reinicia o game
                [linha.zera() for linha in self.linhas]  # volta as peças para o topo
                self.posicoes_montadas = []  # indica que nenhuma peça foi montada
                self.parte_inicial = -1  # inicia a altura de ontagem da primeira peça
                return 0, 0  #  retorna uma posição inválida para sinalizar a peça
            return (inicio_horizontal+largura_da_peca*(self.parte_inicial%numero_de_pecas_por_linha),
                    inicio_vertical+self.altura_da_linha*(self.parte_inicial//numero_de_pecas_por_linha))

class Primeira_fase():
    def __init__(self):

        self.sala_fiocruz = sala_fiocruz = Cena(img= SALA_FIOCRUZ)
        MiniGame(sala_fiocruz, self.acertou)
        rosalinda = Elemento( img = ROSALINDA, tit= "Tenho que me apresar, preciso de dinheiro para continuar minha pesquisa." ,
                style=dict (left=400, top=350, width=300, height="200px",))
        npc = Elemento(img= NPC, y=400,  cena=sala_fiocruz)
        pessoa_branca_cabelo_liso = Elemento(img= PESSOA_BRANCA_CABELO_LISO_OLHOS_VERDES, y=500,  cena=sala_fiocruz)
        pessoa_negra_cabelo_black = Elemento (img = PESSOA_NEGRA_CABELO_BLACK_OLHOS_CASTANHOS, y=500, x=150, cena=sala_fiocruz)
        pessoa_ruiva_pele_branca_cabelovermelho = Elemento (img = PESSOA_RUIVA_PELE_BRANCA_CABELO_VERMELHO, y=500, x=250, cena=sala_fiocruz)
        crianca_branca_cabelo_iso_olhos_verdes = Elemento (img = CRIANCA_BRANCA_CABELO_LISO_OLHOS_VERDES, y=500, x=350, cena=sala_fiocruz)
        crianca_negra_cabelo_black_olhos_castanhos = Elemento (img = CRIANCA_NEGRA_CABELO_BLACK_OLHOS_CASTANHOS, y=500, x=450, cena=sala_fiocruz)
        crianca_ruiva_pele_branca_cabelo_vermelho = Elemento (img = CRIANCA_RUIVA_PELE_BRANCA_CABELO_VERMELHO, y=500, x=550, cena=sala_fiocruz )
        sala_fiocruz.vai()
        #CENA 2 VERBO ENTENDER HEREDITARIEDADE

        self.museu_da_vida = Cena(MUSEU_DA_VIDA)
    def acertou(self):
        Texto(self.sala_fiocruz, "Encontrei um pergaminho! Achei uma saída pela direita!").vai()
        pergaminho = Elemento(img=PERGAMINHO,  cena=self.sala_fiocruz)
        self.sala_fiocruz.direita = self.museu_da_vida

'''
O NPC DEVERÁ APRESENTAR AS SEGUINTES DICAS ORGANIZAR AS PALAVRAS UTILIZANDO A ORDEM CORRETA DE ENTENDIMENTO

Nucleotídeos

Características

Organismo

Sequência correta

Abrir cofre

PenDrive

Poção mágica

Super poderes
'''
if __name__ == "__main__":
    Primeira_fase()


import argparse
import json
import logging
import os
import sys
import time

import torch
from ray import tune

import data
import energy
import meta
import models
import train
import utils

from utils import config

def load_default_config(dataset, method, model):
    """
    Load default parameter configuration from file.

    Returns:
        Dictionary of default parameters for the given task
    """
    if dataset == "mnist" and method == "cg" and model == "mlp":
        default_config = "config/cg_mnist_mlp.json"

    elif dataset == "mnist" and method == "nsa" and model == "mlp":
        default_config = "config/nsa_mnist_mlp.json"

    elif dataset == "mnist" and method == "t1t2" and model == "mlp":
        default_config = "config/t1t2_mnist_mlp.json"

    elif dataset == "cifar10" and method == "cg" and model == "lenet":
        default_config = "config/cg_cifar10_lenet.json"

    elif dataset == "cifar10" and method == "nsa" and model == "lenet":
        default_config = "config/nsa_cifar10_lenet.json"

    elif dataset == "cifar10" and method == "t1t2" and model == "lenet":
        default_config = "config/t1t2_cifar10_lenet.json"

    else:
        raise ValueError("Default configuration for dataset \"{}\", method \"{}\" and model \"{}\" not defined.".format(dataset, method, model))

    with open(default_config) as config_json_file:
        cfg = json.load(config_json_file)

    return cfg

def parse_arguments(args):
    """
    Parse shell arguments for this script and return as dictionary
    """
    parser = argparse.ArgumentParser(description="IFT-based hyperparameter optimisation.")

    # The following arguments set their default here
    parser.add_argument("--dataset", choices=["cifar10", "mnist"],
                        default="mnist", help="Dataset.")

    parser.add_argument("--method", choices=["cg", "nsa", "t1t2"],
                        default="cg", help="Method to approximate the hypergradient.")

    parser.add_argument("--model", choices=["lenet", "mlp"],
                        default="mlp", help="Neural network model type.")

    # The remaining arguments set their default in the corresponding config file in config/
    parser.add_argument("--batch_size", type=int, default=argparse.SUPPRESS,
                        help="Size of mini batches during training.")

    parser.add_argument("--dimensions", type=int, nargs="+",
                        default=argparse.SUPPRESS, help="Dimensions of the neural network.")

    parser.add_argument("--inner_init", choices=["reset", "fixed_seed"],
                        default=argparse.SUPPRESS, help="Mode of initialising the inner loop.")

    parser.add_argument("--lr_inner", type=float, default=argparse.SUPPRESS,
                        help="Learning rate of the inner loop.")

    parser.add_argument("--lr_outer", type=float, default=argparse.SUPPRESS,
                        help="Learning rate of the outer loop.")

    parser.add_argument("--log_dir", type=str, default="",
                        help="Subdirectory within ./log/ where to store logs.")

    parser.add_argument("--nonlinearity", choices=["relu", "sigmoid", "tanh"],
                        default=argparse.SUPPRESS, help="Nonlinearity between network layers.")

    parser.add_argument("--optimizer_inner", choices=["adam", "adagrad", "sgd"],
                        default=argparse.SUPPRESS, help="Optimizer used to train the model.")

    parser.add_argument("--optimizer_outer", choices=["adam", "adagrad", "sgd"],
                        default=argparse.SUPPRESS, help="Optimizer used for meta training.")

    parser.add_argument("--seed", type=int, default=argparse.SUPPRESS, help="Random seed for pytorch")

    parser.add_argument("--steps_inner", type=int, default=argparse.SUPPRESS,
                        help="Number of inner loop steps.")

    parser.add_argument("--steps_outer", type=int, default=argparse.SUPPRESS,
                        help="Number of outer loop steps.")

    # Parse arguments to dictionary
    return vars(parser.parse_args(args))

def run_implicit(cfg, raytune=False):
    # Initialize seed if specified (might slow down the model)
    if cfg['seed'] is not None:
        torch.manual_seed(cfg['seed'])

    # Create the training, validation and test dataloader
    train_loader, valid_loader, test_loader = data.get_dataloader(
        cfg["dataset"], batch_size=cfg["batch_size"], validation_split=cfg["validation_split"], train_subset=cfg["train_subset"]
    )

    # Initialise the model
    if cfg["model"] == "mlp":
        model = models.MultilayerPerceptron(cfg['dimensions'], utils.create_nonlinearity(cfg['nonlinearity'])).to(config.device)
    elif cfg["model"] == "lenet":
        # NOTE: Hard-coded output_dim as all datasets considered so far have 10 outputs
        model = models.LeNet(output_dim=10).to(config.device)
    else:
        raise ValueError("Model type \"{}\" undefined".format(cfg["model"]))

    # Initialise the implicit gradient approximation method
    if cfg["method"] == "cg":
        implicit_gradient = meta.ConjugateGradient(cfg["cg_steps"])
    elif cfg["method"] == "nsa":
        implicit_gradient = meta.NeumannSeries(cfg["nsa_steps"], cfg["nsa_alpha"])
    elif cfg["method"] == "t1t2":
        implicit_gradient = meta.T1T2()
    else:
        raise ValueError("Implicit gradient approximation method \"{}\" undefined".format(cfg["method"]))

    # Initialise the hyperparameters
    hyperparams = meta.HyperparameterDict({
        "l2": [torch.full_like(p, cfg["init_l2"]) for p in model.parameters()]
    }).to(config.device)

    # Initialise meta model wrapping hyperparams and main model
    meta_model = meta.Hyperopt(model, hyperparams, inner_init=cfg["inner_init"], nonnegative_keys={"l2"})

    # Initialise the energy functions
    inner_loss_function = energy.CrossEntropy() + energy.L2Regularizer()
    outer_loss_function = energy.CrossEntropy()

    # Initialise the outer-level optimizer
    optimizer_outer = utils.create_optimizer(
        cfg["optimizer_outer"], hyperparams.parameters(), {"lr": cfg["lr_outer"]}
    )

    results = {
        "test_acc": torch.zeros(cfg['steps_outer'] + 1),
        "test_loss": torch.zeros(cfg['steps_outer'] + 1),
        "train_acc": torch.zeros(cfg['steps_outer'] + 1),
        "train_loss": torch.zeros(cfg['steps_outer'] + 1),
        "valid_acc": torch.zeros(cfg['steps_outer'] + 1),
        "valid_loss": torch.zeros(cfg['steps_outer'] + 1),
    }

    # NOTE: There is one additional meta iteration which does not invoke a meta update:
    # +1 to evaluate the validation accuracy after the last outer step
    for step_outer in range(cfg['steps_outer'] + 1):
        # Initialise the model parameters
        meta_model.reset_parameters()

        # Initialise the inner-level optimizer
        optimizer_inner = utils.create_optimizer(
            cfg["optimizer_inner"], model.parameters(), {"lr": cfg["lr_inner"]}
        )

        # Inner-loop training
        train.train_augmented(
            meta_model, cfg["steps_inner"], optimizer_inner, train_loader, inner_loss_function, verbose=not raytune
        )

        if step_outer < cfg['steps_outer']:

            # HACK: Put all data in a single batch to save memory when backpropagating through the loss
            def dataloader_to_tensor(dataloader):
                x = torch.stack(list(zip(*list(dataloader.dataset)))[0])
                y = torch.tensor(list(zip(*list(dataloader.dataset)))[1])

                return (x, y)

            # Compute the inner and outer-loss
            inner_loss = train.loss(meta_model, [dataloader_to_tensor(train_loader)], inner_loss_function)
            outer_loss = train.loss(meta_model, [dataloader_to_tensor(valid_loader)], outer_loss_function)

            # Compute the indirect gradient wrt hyperparameters
            indirect_hypergrad = implicit_gradient.hypergrad(
                inner_loss, outer_loss, list(model.parameters()), list(hyperparams.parameters())
            )

            # Apply the outer optimisation step
            # NOTE: We assume that direct_hyper_grad == 0, i.e. the outer-loss does not depend on the hyperparams
            optimizer_outer.zero_grad()
            for hp, hp_grad in zip(hyperparams.parameters(), indirect_hypergrad):
                hp.grad = hp_grad

            optimizer_outer.step()

            # Enforce non-negativity through projected GD for selected hyperparameters
            meta_model.enforce_nonnegativity()

        # Testing
        with torch.no_grad():
            test_acc = train.accuracy(model, test_loader)
            test_loss = train.loss(model, test_loader, outer_loss_function)

            train_acc = train.accuracy(model, train_loader)
            train_loss = train.loss(model, train_loader, outer_loss_function)

            valid_acc = train.accuracy(model, valid_loader)
            valid_loss = train.loss(model, valid_loader, outer_loss_function)

        # Logging
        if raytune:
            tune.report(**{
                "test_acc": test_acc.item(),
                "test_loss": test_loss.item(),
                "train_acc": train_acc.item(),
                "train_loss": train_loss.item(),
                "valid_acc": valid_acc.item(),
                "valid_loss": valid_loss.item(),
            })
        else:
            logging.info(
                "step_outer: {}/{}\t train_acc: {:4f} \t valid_acc: {:4f} \t test_acc: {:4f}".format(
                    step_outer, cfg['steps_outer'], train_acc, valid_acc, test_acc
                )
            )
            logging.info(
                "step_outer: {}/{}\t train_loss: {:4f} \t valid_loss: {:4f} \t test_loss: {:4f}".format(
                    step_outer, cfg['steps_outer'], train_loss, valid_loss, test_loss
                )
            )

            results["test_acc"][step_outer] = test_acc
            results["test_loss"][step_outer] = test_loss
            results["train_acc"][step_outer] = train_acc
            results["train_loss"][step_outer] = train_loss
            results["valid_acc"][step_outer] = valid_acc
            results["valid_loss"][step_outer] = valid_loss

            config.writer.add_scalars('accuracy', {'train': train_acc, 'test': test_acc, 'valid': valid_acc}, step_outer)
            config.writer.add_scalars('loss', {'train': train_loss, 'test': test_loss, 'valid': valid_loss}, step_outer)

            for name, p in model.named_parameters():
                config.writer.add_histogram('parameter/{}'.format(name), p.view(-1), step_outer)

            for name, p in hyperparams.named_parameters():
                config.writer.add_histogram('hyperparameter/{}'.format(name), p.view(-1), step_outer)

    # Final Testing
    logging.info("Final training on full dataset (train + valid)")

    # Concatenate the full dataset (train + valid)
    full_train_loader = data.create_multitask_loader([train_loader.dataset, valid_loader.dataset], cfg["batch_size"])

    # Initialise the model parameters
    meta_model.reset_parameters()

    # Initialise the inner-level optimizer
    optimizer_inner = utils.create_optimizer(
        cfg["optimizer_inner"], model.parameters(), {"lr": cfg["lr_inner"]}
    )

    # Inner-loop training
    train.train_augmented(
        meta_model, cfg["steps_inner"], optimizer_inner, full_train_loader, inner_loss_function, verbose=not raytune
    )

    # Final testing
    with torch.no_grad():
        train_acc_full = train.accuracy(meta_model, full_train_loader)
        train_loss_full = train.loss(meta_model, full_train_loader, outer_loss_function)

        test_acc_full = train.accuracy(meta_model, test_loader)
        test_loss_full = train.loss(meta_model, test_loader, outer_loss_function)

    if raytune:
        tune.report(**{
            "test_acc": test_acc.item(),
            "test_loss": test_loss.item(),
            "train_acc": train_acc.item(),
            "train_loss": train_loss.item(),
            "valid_acc": valid_acc.item(),
            "valid_loss": valid_loss.item(),
            "test_acc_full": test_acc_full.item(),
            "test_loss_full": test_loss_full.item(),
            "train_acc_full": train_acc_full.item(),
            "train_loss_full": train_loss_full.item(),
        })

    else:
        results["test_acc_full"] = test_acc_full
        results["test_loss_full"] = test_loss_full
        results["train_acc_full"] = train_acc_full
        results["train_loss_full"] = train_loss_full

        logging.info("train_acc_full: {:4f} \t test_acc_full: {:4f}".format(train_acc_full, test_acc_full))
        logging.info("train_loss_full: {:4f} \t test_loss_full: {:4f}".format(train_loss_full, test_loss_full))

        return results, model, hyperparams

if __name__ == '__main__':
    # Load configuration (Priority: 1 User, 2 Random, 3 Default)
    user_config = parse_arguments(sys.argv[1:])
    cfg = load_default_config(user_config["dataset"], user_config["method"], user_config["model"])
    cfg.update(user_config)

    # Setup logging
    run_id = time.strftime("%Y%m%d_%H%M%S") + "_" + cfg["method"] + "_" + cfg["dataset"] + "_" + cfg["model"]
    utils.setup_logging(run_id, cfg["log_dir"])

    # Main
    logging.info("Start training with parametrization:\n{}".format(
        json.dumps(cfg, indent=4, sort_keys=True)))
    results, model, hyperparams = run_implicit(cfg, raytune=False)

    # Save the configuration as json
    utils.save_dict_as_json(cfg, run_id, config.LOG_DIR)

    # Store results, configuration and model state as pickle
    results['cfg'], results['model'], results['hyperparameter'] = cfg, model.state_dict(), hyperparams.state_dict()
    torch.save(results, os.path.join(config.LOG_DIR, run_id + "_results.pt"))

    # Zip the tensorboard logging results and remove the folder to save space
    config.writer.close()
    path_tensorboard = os.path.join(config.LOG_DIR, run_id + "_tensorboard")
    utils.zip_and_remove((path_tensorboard))


import socket
import threading

# send a threaded message
# if no connection can be established
# throws Error Message ;)
def threaded_socket_message():
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        # Connect the socket to the port where the server is listening
        eventghost_adress = ("192.168.1.100", 7200)
        sock.connect(eventghost_adress)

        message = 'pulse_red'
        sock.sendall(message.encode('utf-8'))

    except Exception as e:
        print("CIRI",e)
    finally:
        sock.close()

x = threading.Thread(target=threaded_socket_message, args=())
x.start()


import copy
import datetime
import json
import logging.config
import os
import random
import re
import sys
import time
import uuid
from datetime import datetime as dt

import jctconv
import requests

LOG_CONF = "./logging.conf"
logging.config.fileConfig(LOG_CONF)

from bs4 import BeautifulSoup
from kivy.app import App
from kivy.clock import Clock
from kivy.config import Config
from kivy.core.text import LabelBase, DEFAULT_FONT
from kivy.core.window import Window
from kivy.resources import resource_add_path
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen
from kivy.uix.textinput import TextInput

if hasattr(sys, "_MEIPASS"):
    resource_add_path(sys._MEIPASS)

EMPTY = ""
JP = "jp"
JP_CAP = "JP"
NEW = "CATEGORY: NEW"
TOPS_SWEATERS = "TOPS/SWEATERS"
LEAVE_BLANK = "*leave blank*"
SIZE_S = "s"
SIZE_M = "m"
SIZE_L = "l"
SIZE_XL = "xl"
KEY_INDEX = "index"
KEY_ITEM_CODE = "itemCode"
KEY_NAME = "name"
KEY_LOCALE = "locale"
KEY_TITLE = "title"
KEY_MONITOR = "monitor"
KEY_SCHEDULE = "schedule"
KEY_COLOR = "color"
KEY_PAGE = "page"
KEY_CHECKOUT_DELAY_ENABLED = "checkout_delay_enabled"
KEY_CHECKOUT_DELAY_SECONDS = "checkout_delay_seconds"
KEY_PROXY_RATIO = "proxyratio"
KEY_TASK_RATIO = "taskratio"
KEY_TASKS = "tasks"
KEY_EMAIL = "email"
KEY_BILLING = "billing"
KEY_FIRST = "first"
KEY_LAST = "last"
KEY_ADDRESS1 = "address1"
KEY_POST_CODE = "postcode"
KEY_CITY = "city"
KEY_REGION = "region"
KEY_COUNTRY = "country"
KEY_PHONE = "phone"
KEY_CARD = "card"
KEY_TYPE = "type"
KEY_NUMBER = "number"
KEY_MONTH = "month"
KEY_YEAR = "year"
KEY_CODE = "code"
KEY_SIZES = "sizes"
KEY_CHECKOUT_PROFILE = "checkoutprofile"
INDEX_TWITTER = 21
INDEX_EMAIL = 9
INDEX_FIRST_NAME = 2
INDEX_LAST_NAME = 3
INDEX_ADDRESS = 7
INDEX_POST_CODE = 4
INDEX_CITY = 6
INDEX_REGION = 5
INDEX_PHONE = 8
INDEX_PAY_TYPE = 13
INDEX_CARD_NUMBER = 14
INDEX_CARD_LIMIT_MONTH = 15
INDEX_CARD_LIMIT_YEAR = 16
INDEX_CARD_CVV = 17
INDEX_ITEM_NO = 4
INDEX_ITEM_SIZE = 2
VAL_CHECKOUT_DELAY_ENABLED = False
VAL_CHECKOUT_DELAY_SECONDS = 5.0
VAL_PROXY_RATIO = 1
VAL_TASK_RATIO = 1
HEADER_KEY_LIST = [KEY_INDEX, KEY_ITEM_CODE, KEY_NAME, KEY_MONITOR]
HEADER_MONITOR_KEY_LIST = [KEY_PAGE, KEY_TITLE, KEY_COLOR]
SIZE_X_DICT = {
    KEY_INDEX: 0.7, KEY_NAME: 1, KEY_PAGE: 0.3, KEY_TITLE: 0.5, KEY_COLOR: 0.3
}

URL = "http://www.nikeslayer.com/news/category/supremeslayer-keywords/"
HEADERS = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0",
}

ID_ITEM_VIEW = "item_view_data"
ID_GET_SITE_INFO_BUTTON = "get_site_info_button"
ID_DUMP_CHECKOUTPROFILES_BUTTON = "dump_checkoutprofiles_button"
ID_DUMP_RELEASEPROFILES_BUTTON = "dump_releaseprofiles_button"
ID_MESSAGE = "message"
ID_MAX_DATA_NUM_PER_FILE = "max_data_num_per_file"

RELEASE_PROFILES_JSON = "releaseprofiles{}.json"
CHECKOUT_PROFILES_JSON = "checkoutprofiles.json"
UTF8 = "utf8"
SATURDAY_INDEX = 5
LOG_CONF = "./logging.conf"

CONFIG_DICT = {}
CONFIG_KEY_KEYWORD_URL = "KEYWORD_URL"

class JsonMakerScreen(Screen):
    def __init__(self, **kwargs):
        super(JsonMakerScreen, self).__init__(**kwargs)
        self.item_list = []
        self.task_list_dict = {}
        self.line_num = 0
        self._file = Window.bind(on_dropfile=self._on_file_drop)
        self.tmp_txt_path = None

    def _on_file_drop(self, window, file_path):
        self.disp_messg("{}を読み込みました".format(os.path.basename(file_path)))
        self.tmp_txt_path = file_path
        self.ids[ID_DUMP_CHECKOUTPROFILES_BUTTON].disabled = False

    def disp_drag_and_drop_msg(self):
        self.disp_messg("tmp.txtをツール画面上にドラッグ&ドロップしてください")

    @staticmethod
    def get_latest_url():
        r = requests.get(URL, headers=HEADERS)
        soup = BeautifulSoup(r.text, "lxml")
        for a in soup.find_all("a"):
            if a.text == "Read more":
                return a.get("href")

    def get_site_info(self):
        self.disp_messg("サイトから最新情報を取得中...")
        self.ids[ID_GET_SITE_INFO_BUTTON].disabled = True
        Clock.schedule_once(self.update_item_view)

    def update_item_view(self, dt):
        try:
            #url = self.get_latest_url()
            url = CONFIG_DICT[CONFIG_KEY_KEYWORD_URL]
            self.parse_site_info(url)
            scrollview = self.ids["container"]
            scrollview.clear_widgets()
            row_len = len(self.item_list)

            scrollview.height = row_len * 40
            for i in range(row_len):
                self.add_item_info_row(i, scrollview)

            self.disp_messg("サイトから最新情報を取得しました")
        except Exception as e:
            self.disp_messg_err("サイトから最新情報を取得するのに失敗しました。")
            logging.exception("Unknown Exception : %s.", e)
        finally:
            self.ids[ID_GET_SITE_INFO_BUTTON].disabled = False

    def add_item_info_row(self, i, scrollview):
        box = BoxLayout()
        item_dict = self.item_list[i]
        self.add_text_widget_on_grid(box, str(i + 1), size_hint_x=0.15)
        for key in HEADER_KEY_LIST:
            if key == KEY_MONITOR:
                mon_dict = item_dict[key]
                for mon_key in HEADER_MONITOR_KEY_LIST:
                    self.add_text_widget_on_grid(box, mon_dict.get(mon_key, ""), size_hint_x=SIZE_X_DICT[mon_key])
            elif key == KEY_ITEM_CODE:
                self.add_text_widget_on_grid(box, "", "itemCode-" + str(i), size_hint_x=0.3, disabled=False)
            else:
                self.add_text_widget_on_grid(box, item_dict[key], size_hint_x=SIZE_X_DICT[key])

        scrollview.add_widget(box)

    @staticmethod
    def add_text_widget_on_grid(box, text, id=None, size_hint_x=1, disabled=True):
        text = TextInput(text=text, size_hint_x=size_hint_x, multiline=False, write_tab=False)
        text.id = id
        text.disabled = disabled
        text.disabled_foreground_color = (0, 0, 0, 1)
        text.background_disabled_normal = text.background_normal
        box.add_widget(text)

    @staticmethod
    def get_page(th_list):
        page = th_list[0].text
        if NEW in page:
            page = "new"
        elif page == TOPS_SWEATERS:
            page = "tops_sweaters"
        return page.lower()

    def save_item_info(self, page, td_list):
        has_multi_color = False
        base_name = td_list[0].text
        title = td_list[1].text
        color_list = td_list[2].text.split(",")

        if len(color_list) > 1:
            has_multi_color = True

        for color in color_list:
            item_dict = {}
            item_dict[KEY_INDEX] = "rk_" + str(uuid.uuid4()).replace("-", "")
            monitor_dict = {}
            monitor_dict[KEY_PAGE] = page
            monitor_dict[KEY_TITLE] = title
            color = re.sub("^ ", EMPTY, color)

            if has_multi_color:
                item_dict[KEY_NAME] = "{} {}".format(base_name, color)
            else:
                item_dict[KEY_NAME] = base_name

            item_dict[KEY_LOCALE] = JP

            if color != LEAVE_BLANK:
                monitor_dict[KEY_COLOR] = color

            item_dict[KEY_MONITOR] = monitor_dict
            item_dict[KEY_SCHEDULE] = self.get_next_saturday_epoch()
            item_dict[KEY_CHECKOUT_DELAY_ENABLED] = VAL_CHECKOUT_DELAY_ENABLED
            item_dict[KEY_CHECKOUT_DELAY_SECONDS] = VAL_CHECKOUT_DELAY_SECONDS
            item_dict[KEY_PROXY_RATIO] = VAL_PROXY_RATIO
            item_dict[KEY_TASK_RATIO] = VAL_TASK_RATIO
            self.item_list.append(item_dict)

    def parse_site_info(self, url):
        self.item_list = []
        r = requests.get(url, headers=HEADERS)
        soup = BeautifulSoup(r.text, "lxml")
        for tab in soup.find_all("table"):
            page = None
            is_1st_td = True
            for tr in tab.find_all("tr"):
                td_list = tr.find_all("td")
                if len(td_list) > 0:
                    if is_1st_td:
                        page = self.get_page(td_list)
                        is_1st_td = False
                    else:
                        self.save_item_info(page, td_list)

    def dump_checkoutprofiles(self):
        global log
        try:
            self.dump_checkoutprofile_core()
        except Exception as e:
            self.disp_messg_err("checkoutprofiles.jsonを出力に失敗しました。")
            if self.line_num > 0:
                log.error("tmp.txtの{}行目処理時にエラーが発生しました。".format(self.line_num))
            log.exception("checkoutprofiles.jsonの出力に失敗しました。%s", e)

    def dump_checkoutprofile_core(self):
        address_list = []
        self.task_list_dict = {}
        self.line_num = 0

        for line in open(self.tmp_txt_path, "r", encoding="utf-8"):
            self.line_num += 1
            line = line[:-1]
            items = line.split("\t")
            if items[0] == "*S":
                address_dict = {}
                index = "ckk_" + str(uuid.uuid4()).replace("-", "")
                address_dict[KEY_INDEX] = index
                address_dict[KEY_NAME] = items[INDEX_TWITTER]
                address_dict[KEY_LOCALE] = JP
                address_dict[KEY_EMAIL] = items[INDEX_EMAIL]
                bill_dict = {}
                bill_dict[KEY_FIRST] = items[INDEX_FIRST_NAME]
                bill_dict[KEY_LAST] = items[INDEX_LAST_NAME]
                bill_dict[KEY_ADDRESS1] = items[INDEX_ADDRESS]
                bill_dict[KEY_POST_CODE] = items[INDEX_POST_CODE]
                bill_dict[KEY_CITY] = items[INDEX_CITY]
                bill_dict[KEY_REGION] = items[INDEX_REGION]
                bill_dict[KEY_COUNTRY] = JP_CAP
                bill_dict[KEY_PHONE] = items[INDEX_PHONE]
                address_dict[KEY_BILLING] = bill_dict
                card_dict = {}
                if items[INDEX_PAY_TYPE] == "代金引換":
                    card_dict[KEY_TYPE] = "cashondelivery"
                else:
                    card_dict[KEY_TYPE] = items[INDEX_PAY_TYPE].replace(" ", "").lower()
                    card_dict[KEY_NUMBER] = items[INDEX_CARD_NUMBER]
                    card_dict[KEY_MONTH] = int(items[INDEX_CARD_LIMIT_MONTH])
                    card_dict[KEY_YEAR] = int("20" + items[INDEX_CARD_LIMIT_YEAR])
                    card_dict[KEY_CODE] = items[INDEX_CARD_CVV]
                address_dict[KEY_CARD] = card_dict
            elif items[0] == "*I":
                item_no = items[INDEX_ITEM_NO]
                size = self.format_size(items[INDEX_ITEM_SIZE])

                if size is None:
                    continue

                task_list = self.task_list_dict.get(item_no)
                if task_list is None:
                    task_list = []
                    self.task_list_dict[item_no] = task_list

                task_dict = {}
                task_dict[KEY_SIZES] = [size]
                task_dict[KEY_CHECKOUT_PROFILE] = index
                task_list.append(task_dict)

                address_list.append(address_dict)

        with open(CHECKOUT_PROFILES_JSON, "w", encoding=UTF8) as f:
            f.write(json.dumps(address_list, indent=2, ensure_ascii=False))

        self.ids[ID_DUMP_RELEASEPROFILES_BUTTON].disabled = False
        self.disp_messg("checkoutprofiles.jsonを出力しました")

    def disp_messg(self, msg):
        self.ids[ID_MESSAGE].text = msg
        self.ids[ID_MESSAGE].color = (0, 0, 0, 1)

    def disp_messg_err(self, msg):
        self.ids[ID_MESSAGE].text = "{}詳細はログファイルを確認してください。".format(msg)
        self.ids[ID_MESSAGE].color = (1, 0, 0, 1)

    @staticmethod
    def format_size(size):
        global log
        size = jctconv.normalize(size.lower())
        if size == EMPTY:
            return "random_random"
        elif size == SIZE_S or size == SIZE_M or size == SIZE_L or size == SIZE_XL:
            return "shirtsother_{}".format(size)
        elif "/" in size:
            return None
        elif float(size) < 28:
            return "shoes_{}".format(size)
        else:
            return "pants_{}".format(size)

    @staticmethod
    def conv_0_to_empty(num):
        if num == 0:
            return ""
        else:
            return num

    def dump_releaseprofiles(self):
        global log
        try:
            self.dump_releaseprofiles_core()
        except Exception as e:
            self.disp_messg_err("releaseprofiles.jsonを出力に失敗しました。")
            log.exception("releaseprofiles.jsonの出力に失敗しました。%s", e)

    def dump_releaseprofiles_core(self):
        global log

        dump_item_list = []
        max_data_num_per_file = int(self.ids[ID_MAX_DATA_NUM_PER_FILE].text)
        order_num = 0
        dumped_num = 0

        for i in range(len(self.item_list)):
            item_dict = self.item_list[i]
            item_no = self.get_item_no(i)
            if item_no is None:
                continue

            task_list = self.task_list_dict.get(item_no)
            if task_list is None:
                continue

            if order_num + len(task_list) <= max_data_num_per_file:
                item_dict[KEY_TASKS] = task_list
                dump_item_list.append(item_dict)
                order_num += len(task_list)
                if max_data_num_per_file <= order_num:
                    self.write_release_profiles_json(dump_item_list, dumped_num)
                    dumped_num += 1
                    dump_item_list = []
                    order_num = 0
            else:
                item_dict_without_task = copy.deepcopy(item_dict)
                split_task_list = []
                for task in task_list:
                    split_task_list.append(task)
                    order_num += 1
                    if order_num >= max_data_num_per_file:
                        item_dict[KEY_TASKS] = split_task_list
                        dump_item_list.append(item_dict)
                        self.write_release_profiles_json(dump_item_list, dumped_num)
                        dumped_num += 1
                        order_num = 0
                        dump_item_list = []
                        split_task_list = []
                        item_dict = item_dict_without_task
                        item_dict_without_task = copy.deepcopy(item_dict)

                if 0 < len(split_task_list):
                    item_dict[KEY_TASKS] = split_task_list
                    dump_item_list.append(item_dict)

        if 0 < order_num:
            self.write_release_profiles_json(dump_item_list, dumped_num)
            dumped_num += 1

        self.disp_messg("releaseprofiles.jsonを{}ファイル出力しました".format(dumped_num))

    @staticmethod
    def get_next_saturday_epoch():
        today = dt.today()
        add_day = SATURDAY_INDEX - today.weekday()
        if add_day < 0:
            add_day = 6
        next_day = today + datetime.timedelta(days=add_day)

        sec = random.randint(20, 50)
        next_date_str = "{}-{}-{} 10:59:{}".format(next_day.year, next_day.month, next_day.day, sec)
        next_date = dt.strptime(next_date_str, '%Y-%m-%d %H:%M:%S')
        epoch = int(time.mktime(next_date.timetuple())) + (9 * 3600)
        return str(epoch)

    def write_release_profiles_json(self, dump_item_list, dumped_num):
        with open(RELEASE_PROFILES_JSON.format(self.conv_0_to_empty(dumped_num)), "w", encoding=UTF8) as f:
            f.write(json.dumps(dump_item_list, indent=2, ensure_ascii=False))

    def get_item_no(self, no):
        for widget in self.walk():
            if widget.id == "itemCode-{}".format(no):
                return widget.text

class JsonMakerApp(App):
    def build(self):
        return JsonMakerScreen()

def load_config():
    for line in open("./config.txt", "r"):
        items = line.replace("\n", "").split("=")

        if len(items) != 2:
            continue

        if items[0] == CONFIG_KEY_KEYWORD_URL:
            CONFIG_DICT[CONFIG_KEY_KEYWORD_URL] = items[1]

def setup_config():
    load_config()
    Config.set('modules', 'inspector', '')  # Inspectorを有効にする
    Config.set('graphics', 'width', 1280)  # Windowの幅を1280にする
    Config.set('graphics', 'maxfps', 20)  # フレームレートを最大で20にする
    Config.set('graphics', 'resizable', 0)  # Windowの大きさを変えられなくする
    Config.set('input', 'mouse', 'mouse,disable_multitouch')
    Config.write()

if __name__ == '__main__':
    log = logging.getLogger('my-log')
    setup_config()
    LabelBase.register(DEFAULT_FONT, "ipaexg.ttf")
    JsonMakerApp().run()


"""
config_test.py
--------------

Test local config files.

"""

import os
import decu
from decu import __main__ as main

def test_config_file_in_place():
    """`decu.cfg` should be in the right place."""
    from decu.config import config_filename
    assert config_filename in os.listdir('../decu/')

def test_local_override(tmpdir):
    """A local config file should override the default options."""
    config_filename = 'decu.cfg'
    new_opt = '${time}.log'
    config = '[logging]\nlog_file = {}'.format(new_opt)

    main.init(str(tmpdir))

    with tmpdir.join(config_filename).open('w+') as file:
        file.write(config)

    script = 'import decu\nprint(decu.config["logging"]["log_file"])'
    script_file = tmpdir.join(decu.config['Script']['scripts_dir']).join(
        'script.py')
    with script_file.open('w+') as file:
        file.write(script)

    from subprocess import check_output
    curdir = os.getcwd()
    os.chdir(str(tmpdir))
    output = check_output(['python', str(script_file)])
    assert output.decode().strip() == new_opt
    os.chdir(curdir)


import plotly.graph_objects as go
import csv
from datetime import datetime

Alphabet = []
filename = 'data/Eminence_Data.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    # gets date, high, and low temps
    dates, rains = [], []
    for row in reader:
        current_date = datetime.strptime(row[1], '%Y-%m-%d')
        try:
            high = float(row[9])
        except ValueError:
            print(f"Missing data for {current_date}")
        else:
            dates.append(current_date)
            rains.append(high)

for i in rains:
    Alphabet.append(rains[0:5])
    del rains[0:5]

headerColor = '#00AADD'
rowEvenColor = '#BD5E00'
rowOddColor = '#EB7500'

fig = go.Figure(data=[go.Table(
    header=dict(values=['DATES', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021'],
                line_color='black',
                fill_color='darkorange',
                align='left'),
    cells=dict(values=[['July 21', 'July 22', 'July 23', 'July 24' , 'July 25'],
                       Alphabet[0],
                       Alphabet[1],
                       Alphabet[2],
                       Alphabet[3],
                       Alphabet[4],
                       Alphabet[5],
                       Alphabet[6],
                       Alphabet[7],
                       Alphabet[8],
                       Alphabet[9]],
               line_color='black',
               fill_color='orange',
               align='left'))
])

fig.show()


import torch
from tqdm import tqdm
import math

from modules.fuse_pipeline import Fuse_Pipeline
from modules.filter_pipeline import Filter_Pipeline

import numpy as np

class Pipeline(torch.nn.Module):
    def __init__(self, config):

        super(Pipeline, self).__init__()

        self.config = config

        # setup pipeline
        self.fuse_pipeline = Fuse_Pipeline(config)
        if config.FILTERING_MODEL.do:
            if config.FILTERING_MODEL.model == "3dconv":
                self.filter_pipeline = Filter_Pipeline(config)
            else:
                self.filter_pipeline = (
                    None  # used when we run the tsdf fusion or routedfusion
                )
        else:
            self.filter_pipeline = None

    def forward(self, batch, database, epoch, device):  # train step
        scene_id = batch["frame_id"][0].split("/")[0]

        frame = batch["frame_id"][0].split("/")[-1]

        fused_output = self.fuse_pipeline.fuse_training(batch, database, device)

        if self.config.FILTERING_MODEL.do:
            if self.filter_pipeline is not None:
                filtered_output = self.filter_pipeline.filter_training(
                    fused_output,
                    database,
                    epoch,
                    frame,
                    scene_id,
                    batch["sensor"],
                    device,
                )
            else:
                filtered_output = None

            if filtered_output == "save_and_exit":
                return "save_and_exit"

            if filtered_output is not None:
                fused_output["filtered_output"] = filtered_output
            else:
                if not self.config.FILTERING_MODEL.model == "routedfusion":
                    return None

        return fused_output

    def test(self, loader, dataset, database, sensors, device):
        for k, batch in tqdm(enumerate(loader), total=len(dataset)):
            if self.config.DATA.collaborative_reconstruction:
                if (
                    math.ceil(
                        int(batch["frame_id"][0].split("/")[-1])
                        / self.config.DATA.frames_per_chunk
                    )
                    % 2
                    == 0
                ):
                    sensor_ = sensors[0]
                else:
                    sensor_ = sensors[1]

                batch["depth"] = batch[sensor_ + "_depth"]
                batch["routing_net"] = "self._routing_network_" + sensor_
                batch["mask"] = batch[sensor_ + "_mask"]
                if self.config.FILTERING_MODEL.model == "routedfusion":
                    batch["sensor"] = self.config.DATA.input[0]
                else:
                    batch["sensor"] = sensor_

                batch["routingNet"] = sensor_  # used to be able to train routedfusion
                batch["fusionNet"] = sensor_  # used to be able to train routedfusion
                self.fuse_pipeline.fuse(batch, database, device)
            else:
                for sensor_ in sensors:
                    if (
                        sensor_ + "_depth"
                    ) in batch:  # None on the Replica dataset when simulating sensors of different frame rates
                        batch["depth"] = batch[sensor_ + "_depth"]
                        batch["routing_net"] = "self._routing_network_" + sensor_
                        batch["mask"] = batch[sensor_ + "_mask"]
                        if self.config.FILTERING_MODEL.model == "routedfusion":
                            batch["sensor"] = self.config.DATA.input[0]
                        else:
                            batch["sensor"] = sensor_

                        batch[
                            "routingNet"
                        ] = sensor_  # used to be able to train routedfusion
                        batch[
                            "fusionNet"
                        ] = sensor_  # used to be able to train routedfusion
                        self.fuse_pipeline.fuse(batch, database, device)

        if self.filter_pipeline is not None:
            # run filtering network on all voxels which have a non-zero weight
            for scene in database.filtered.keys():
                self.filter_pipeline.filter(scene, database, device)

    def test_tsdf(self, val_loader, val_dataset, val_database, sensors, device):

        for k, batch in tqdm(enumerate(val_loader), total=len(val_dataset)):

            if (
                self.config.ROUTING.do
                and self.config.FILTERING_MODEL.model == "tsdf_early_fusion"
            ):
                batch["routing_net"] = "self._routing_network"
                batch["sensor"] = self.config.DATA.input[0]
                batch[
                    "fusionNet"
                ] = None  # We don't use a fusion net during early fusion
                self.fuse_pipeline.fuse(batch, val_database, device)
            else:
                for sensor_ in sensors:
                    batch["depth"] = batch[sensor_ + "_depth"]
                    batch["routing_net"] = "self._routing_network_" + sensor_
                    batch["mask"] = batch[sensor_ + "_mask"]
                    batch["sensor"] = sensor_
                    batch[
                        "routingNet"
                    ] = sensor_  # used to be able to train routedfusion
                    batch[
                        "fusionNet"
                    ] = sensor_  # used to be able to train routedfusion
                    self.fuse_pipeline.fuse(batch, val_database, device)

        if self.config.FILTERING_MODEL.do:
            # perform the fusion of the grids
            if self.config.FILTERING_MODEL.model == "tsdf_early_fusion":
                for scene in val_database.filtered.keys():
                    val_database.filtered[scene].volume = val_database.tsdf[
                        self.config.DATA.input[0]
                    ][scene].volume

            elif (
                self.config.FILTERING_MODEL.model == "tsdf_middle_fusion"
            ):  # this is weighted average fusion
                for scene in val_database.filtered.keys():
                    weight_sum = np.zeros_like(val_database.filtered[scene].volume)
                    for sensor_ in sensors:
                        weight_sum += val_database.fusion_weights[sensor_][scene]
                        val_database.filtered[scene].volume += (
                            val_database.tsdf[sensor_][scene].volume
                            * val_database.fusion_weights[sensor_][scene]
                        )
                    val_database.filtered[scene].volume = np.divide(
                        val_database.filtered[scene].volume,
                        weight_sum,
                        out=np.zeros_like(weight_sum),
                        where=weight_sum != 0.0,
                    )

                    val_database.sensor_weighting[scene] = np.divide(
                        val_database.fusion_weights[sensors[0]][scene],
                        weight_sum,
                        out=np.zeros_like(weight_sum),
                        where=weight_sum != 0.0,
                    )


from collections import OrderedDict

from couler.core import states, utils

def config_defaults(name_salter=None, service_account: str = None):
    """
    Config couler defaults.
    :param name_salter: function to salt workflow names.
    :param service_account: name of the default Kubernetes
        ServiceAccount with which to run workflows
    :return:
    """
    if name_salter is not None:
        states._workflow_name_salter = name_salter

    if service_account is not None:
        states.default_service_account = service_account

def config_workflow(
    name=None,
    user_id=None,
    timeout=None,
    time_to_clean=None,
    cluster_config_file=None,
    cron_config=None,
    service_account=None,
):
    """
    Config some workflow-level information.
    :param name: name of the workflow.
    :param user_id: user information.
    :param timeout: maximum running time(seconds).
    :param time_to_clean: time to keep the workflow after completed(seconds).
    :param cluster_config_file: cluster specific config
    :param cron_config: for cron scheduling
    :param service_account: name of the Kubernetes ServiceAccount which
        runs this workflow
    :return:
    """
    if name is not None:
        states.workflow.name = states._workflow_name_salter(name)

    if user_id is not None:
        states.workflow.user_id = user_id

    if timeout is not None:
        states.workflow.timeout = timeout

    if time_to_clean is not None:
        states.workflow.clean_ttl = time_to_clean

    if cluster_config_file is not None:
        import os

        os.environ["couler_cluster_config"] = cluster_config_file
        states.workflow.cluster_config = utils.load_cluster_config()

    if cron_config is not None:
        if isinstance(cron_config, OrderedDict):
            raise SyntaxError("Cron config would be a dict")

        if "schedule" not in cron_config:
            raise SyntaxError("require the cron schedule")

        schedule = cron_config["schedule"]
        concurrency_policy = cron_config.get("concurrency_policy", "Allow")
        successful_jobs_history_limit = cron_config.get(
            "successful_jobs_history_limit", 3
        )
        failed_jobs_history_limit = cron_config.get(
            "failed_jobs_history_limit", 1
        )
        starting_deadline_seconds = cron_config.get(
            "starting_deadline_seconds", 10
        )
        suspend = cron_config.get("suspend", "false")
        timezone = cron_config.get("timezone", "Asia/Shanghai")
        _config_cron_workflow(
            schedule,
            concurrency_policy,
            successful_jobs_history_limit,
            failed_jobs_history_limit,
            starting_deadline_seconds,
            suspend,
            timezone,
        )

    states.workflow.service_account = (
        service_account
        if service_account is not None
        else states.default_service_account
    )

def _config_cron_workflow(
    schedule,
    concurrency_policy='"Allow"',
    successful_jobs_history_limit=3,  # Default 3
    failed_jobs_history_limit=1,  # Default 1
    starting_deadline_seconds=10,
    suspend="false",
    timezone="Asia/Shanghai",
):
    """
    Config the CronWorkflow, see example
    https://github.com/argoproj/argo/blob/master/docs/cron-workflows.md
    """

    cron_config = {
        "schedule": schedule,
        "concurrencyPolicy": concurrency_policy,
        "successfulJobsHistoryLimit": successful_jobs_history_limit,
        "failedJobsHistoryLimit": failed_jobs_history_limit,
        "startingDeadlineSeconds": starting_deadline_seconds,
        "suspend": suspend,
        "timezone": timezone,
    }

    states.workflow.config_cron_workflow(cron_config)


from unittest.mock import patch

from sqlalchemy import func
from truth.truth import AssertThat

from freshcart.lib.embedder import embed_product
from freshcart.lib.models import db
from freshcart.lib.models.products import Product
from freshcart.tests import AppTestCase, DbMixin

class TestEmbedder(DbMixin, AppTestCase):
    def test_embedder_product_does_not_exist(self):
        embed_product(1)

        AssertThat(
            db.session.query(func.count(Product.id)).scalar()
        ).IsEqualTo(0)

    def test_embedder_product_already_embedded(self):
        embedding = embedding = [1] * 1536
        product = Product(name="test", embedding=embedding)
        db.session.add(product)
        db.session.commit()

        embed_product(product.id)

        AssertThat(
            Product.query.get(product.id).embedding
        ).ContainsExactlyElementsIn(embedding).InOrder()

    @patch("freshcart.lib.embedder.embed_text", return_value=[2] * 1536)
    def test_embedder_product_not_embedded(self, _):
        product = Product(name="test")
        db.session.add(product)
        db.session.commit()

        embed_product(product.id)

        AssertThat(
            Product.query.get(product.id).embedding
        ).ContainsExactlyElementsIn([2] * 1536).InOrder()


from collections import deque

with open('input.txt') as f:
    lines = f.read().split('\n')[:-2]

heights = []
start_x = 0
start_y = 0
end_x = 0
end_y = 0

for y, line in enumerate(lines):
    heights.append([])

    for x, c in enumerate(line):
        if c == 'S':
            start_x = x
            start_y = y
            c = 'a'

        if c == 'E':
            end_x = x
            end_y = y
            c = 'z'

        heights[-1].append(ord(c) - ord('a'))

h = len(heights)
w = len(heights[0])

q = deque([(start_x, start_y)])
distance_to = {(start_x, start_y): 0}

while len(q):
    point = q.popleft()
    x, y = point

    for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        new_x = x + dx
        new_y = y + dy

        if not (0 <= new_x < w and 0 <= new_y < h):
            continue

        new_point = new_x, new_y

        if new_point in distance_to:
            continue

        if heights[new_y][new_x] - heights[y][x] > 1:
            continue

        distance_to[new_point] = distance_to[point] + 1
        q.append(new_point)

print(distance_to[(end_x, end_y)])


# -*- coding: utf-8 -*-
import django_filters
from ipware import get_client_ip
from django_filters import rest_framework as filters
from django.conf.urls import url, include
from django.shortcuts import get_list_or_404, get_object_or_404
from rest_framework import generics
from rest_framework import authentication, viewsets, permissions, status
from rest_framework.response import Response

from shared_foundation.custom.drf.permissions import IsAuthenticatedAndIsActivePermission
from tenant_api.filters.vehicle_type import VehicleTypeFilter
from tenant_api.pagination import TinyResultsSetPagination
from tenant_api.permissions.tag import (
   CanListCreateTagPermission,
   CanRetrieveUpdateDestroyTagPermission
)
from tenant_api.serializers.vehicle_type import (
    VehicleTypeListCreateSerializer,
    VehicleTypeRetrieveUpdateDestroySerializer
)
from tenant_foundation.models import VehicleType

class VehicleTypeListCreateAPIView(generics.ListCreateAPIView):
    serializer_class = VehicleTypeListCreateSerializer
    pagination_class = TinyResultsSetPagination
    permission_classes = (
        permissions.IsAuthenticated,
        IsAuthenticatedAndIsActivePermission,
        CanListCreateTagPermission
    )

    def get_queryset(self):
        """
        List
        """
        # Fetch all the queries.
        queryset = VehicleType.objects.all().order_by('text')

        # The following code will use the 'django-filter'
        filter = VehicleTypeFilter(self.request.GET, queryset=queryset)
        queryset = filter.qs

        # Return our filtered list.
        return queryset

    def post(self, request, format=None):
        """
        Create
        """
        serializer = VehicleTypeListCreateSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)

class VehicleTypeRetrieveUpdateDestroyAPIView(generics.RetrieveUpdateDestroyAPIView):
    serializer_class = VehicleTypeRetrieveUpdateDestroySerializer
    pagination_class = TinyResultsSetPagination
    permission_classes = (
        permissions.IsAuthenticated,
        IsAuthenticatedAndIsActivePermission,
        CanRetrieveUpdateDestroyTagPermission
    )

    def get(self, request, pk=None):
        """
        Retrieve
        """
        vt = get_object_or_404(VehicleType, pk=pk)
        self.check_object_permissions(request, vt)  # Validate permissions.
        serializer = VehicleTypeRetrieveUpdateDestroySerializer(vt, many=False)
        return Response(
            data=serializer.data,
            status=status.HTTP_200_OK
        )

    def put(self, request, pk=None):
        """
        Update
        """
        vt = get_object_or_404(VehicleType, pk=pk)
        self.check_object_permissions(request, vt)  # Validate permissions.
        serializer = VehicleTypeRetrieveUpdateDestroySerializer(vt, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_200_OK)

    def delete(self, request, pk=None):
        """
        Delete
        """
        client_ip, is_routable = get_client_ip(self.request)
        vt = get_object_or_404(VehicleType, pk=pk)
        self.check_object_permissions(request, vt)  # Validate permissions.
        vt.is_archived = True
        vt.last_modified_by = request.user
        vt.last_modified_from = client_ip
        vt.last_modified_from_is_public = is_routable
        vt.save()
        return Response(data=[], status=status.HTTP_200_OK)


# -*- coding: utf-8 -*-
"""
Created on Thu Jun  6 06:03:15 2019

@author: hp
"""

my_image = "cat1.jpg" # change this to the name of your image file
my_label_y = [1] # the true class of your image (1 -> cat, 0 -> non-cat)
from sklearn.externals import joblib
parameters=joblib.load('model.pkl')

fname = "images/" + my_image
image = np.array(ndimage.imread(fname, flatten=False))
my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((num_px*num_px*3,1))
my_image = my_image/255.
my_predicted_image = predict(my_image, my_label_y, parameters)

plt.imshow(image)
print ("y = " + str(np.squeeze(my_predicted_image)) + ", your L-layer model predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") +  "\" picture.")