import tensorflow as tf
import numpy as np
import bi_lstm
import matplotlib.pyplot as plt
import decoder_lstm
import data_generator
import normalization as normalization
import os
tf.reset_default_graph()
#tf.GraphKeys.VARIABLES = tf.GraphKeys.GLOBAL_VARIABLES

os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
tf.reset_default_graph()
logs_path="board"

class parameter(object):
    def __init__(self):
        '''
        used to set the batch_size
        '''
        self.epochs=100              #the numbers of epoch size
        self.batch_size=8
        self.encoder_layer=1
        self.decoder_layer=1
        self.encoder_nodes=32
        self.learning_rate=0.001
        self.time_size=3            #the size of the input sequence
        self.prediction_size=3      #the size of predict sequence length
        self.features=18
        self.is_training=True
        self.window_step_size=1
        self.normalize=True          #the resouce data normalize
        self.target_city='宝山区'

'''
para used to set the parameters in later process
'''

class Model(object):
    def __init__(self,para):
        self.para=para
        self.x_input=tf.placeholder(dtype=tf.float32,shape=[None,self.para.time_size,self.para.features],name='pollutant')
        self.y=tf.placeholder(dtype=tf.float32,shape=[None,self.para.prediction_size])

    def model(self):
        '''
        :param batch_size: 64
        :param encoder_layer:
        :param decoder_layer:
        :param encoder_nodes:
        :param prediction_size:
        :param is_training: True
        :return:
        '''

        '''
        feedforward and BN layer
        output shape:[batch, time_size,new_features]
        '''
        x_input=self.x_input
        # normal=normalization.Normalization(inputs=self.x_input,out_size=self.para.new_features,is_training=True)
        # normal.normal()
        # x_input = normal.feed_forward()
        # print('the output shape of normalization is :',x_input)
        # self.tag=x_input

        #this step use to encoding the input series data
        # encoder_init=Encoder.encoder(self.x_input,batch_size,encoder_layer,encoder_nodes,is_training)
        # (c_state, h_state)=encoder_init.encoding()

        #this step, we try use Bi_lstm as spatial features extractor
        self.encoder_bi_lstm=bi_lstm.bi_lstm(self.para.batch_size,
                                             hidden_dim=self.para.encoder_nodes,
                                             sequence_length=self.para.time_size,
                                             is_training=self.para.is_training)
        self.encoder_bi_lstm.encoding()
        x_input=self.encoder_bi_lstm.extractor(x_input)
        # encoder_init=encodet_gru.gru(self.x_input,batch_size,encoder_layer,encoder_nodes,is_training)
        # encoder_init=encoder_rnn.rnn(self.x_input,batch_size,encoder_layer,encoder_nodes,is_training)
        #self.encoder_init.encoding()

        #ready to compute the attention value
        # h_state = encoder_bi_lstm.extractor(inputs=)  # the shape is [batch_size, sequence_length , hidden_size*2]
        #
        # encoder_state=encoder_init.extractor(inputs=h_state)
        '''
            the above procession，is ABL-Learning Encoder and data process
            include:spatial feature extraction and time series feature extraction
            spatial h_state shape:[batch_size, sequence_length , hidden_size*2]
        '''

        #this step to presict the polutant concentration
        self.decoder_init=decoder_lstm.lstm(self.para.batch_size,
                                            self.para.prediction_size,
                                            self.para.decoder_layer,
                                            self.para.encoder_nodes,
                                            self.para.is_training)
        self.decoder_init.decoding() #init the layer and nodes
        self.pre=self.decoder_init.predict(x_input) #transfer parameter (input, bi-lstm object,lstm object)

    def train(self):
        '''
        :return:
        '''
        #pre, bi_alpha,l_alpha=self.model()
        self.cross_entropy = tf.reduce_mean(tf.sqrt(tf.reduce_mean(tf.square(self.y - self.pre), axis=0)), axis=0)
        tf.summary.scalar('cross_entropy',self.cross_entropy)
        # backprocess and update the parameters
        self.train_op = tf.train.AdamOptimizer(self.para.learning_rate).minimize(self.cross_entropy)
        return self.cross_entropy,self.train_op

    def test(self):
        '''
        :param batch_size: usually use 1
        :param encoder_layer:
        :param decoder_layer:
        :param encoder_nodes:
        :param prediction_size:
        :param is_training: False
        :return:
        '''
        #pre, bi_alpha, l_alpha= self.model()

        #return

    def accuracy(self,label,predict):
        '''
        :param Label: represents the observed value
        :param Predict: represents the predicted value
        :param epoch:
        :param steps:
        :return:
        '''
        error = label - predict
        average_error = np.mean(np.fabs(error.astype(float)))
        print("In the test set, the prediction model mae error is : %.6f" % (average_error))

        rmse_error = np.sqrt(np.mean(np.square(np.array(label) - np.array(predict))))
        print("In the test set, prediction model rmse error is : %.6f" % (rmse_error))

        cor = np.mean(np.multiply((label - np.mean(label)),
                                  (predict - np.mean(predict)))) / (np.std(predict) * np.std(label))
        print('In the test set, prediction model, the correlation coefficient is: %.6f' % (cor))
        return average_error,rmse_error,cor

    def describe(self,label,predict,prediction_size):
        plt.figure()
        # Label is observed value,Blue
        plt.plot(label[0:prediction_size], 'b*:', label=u'actual value')
        # Predict is predicted value，Red
        plt.plot(predict[0:prediction_size], 'r*:', label=u'predicted value')
        # use the legend
        # plt.legend()
        plt.xlabel("time(days)", fontsize=17)
        plt.ylabel("people", fontsize=17)
        plt.title("the prediction of covid", fontsize=17)
        plt.show()

    def initialize_session(self):
        self.sess=tf.Session()
        self.saver=tf.train.Saver()

def re_current(A,max,min):
    return [round(float(num*(max-min)+min),3) for num in A]

def run_epoch(pre_model):
    '''
    from now on,the model begin to training, until the epoch to 100
    '''
    #saver = tf.train.Saver(max_to_keep=1)
    max_rmse = 100
    cross_entropy, train_op=pre_model.train()
    #with tf.Session() as sess:
    pre_model.sess.run(tf.global_variables_initializer())
    merged = tf.summary.merge_all()
    writer = tf.summary.FileWriter(logs_path,graph=tf.get_default_graph())

    '''init the generator!!!'''
    data_generator.is_training=True
    data_generator.city_name=pre_model.para.target_city
    data_generator.window_size=pre_model.para.window_step_size
    data_generator.input_time_size=pre_model.para.time_size
    data_generator.predict_time_size=pre_model.para.prediction_size

    next_elements=data_generator.next_batch(batch_size=pre_model.para.batch_size,epochs=pre_model.para.epochs)

    total_iter=(data_generator.data_size() - (data_generator.predict_time_size + data_generator.input_time_size))*pre_model.para.epochs \
               //(data_generator.window_size*pre_model.para.batch_size)
    print(total_iter)
    for i in range(total_iter):
        x, label =pre_model.sess.run(next_elements)
        # print('x shape is :',x.shape)
        summary, loss, _ = pre_model.sess.run((merged,cross_entropy,train_op), feed_dict={pre_model.x_input: x, pre_model.y: label})
        if i%100==0:
            print("After %d steps,the loss value is: %.6f" %(i,loss))
            writer.add_summary(summary, i%10)
            if max_rmse>loss:
                max_rmse=loss
                pre_model.saver.save(pre_model.sess,save_path='weights/covid.ckpt')

        # test process and save process
        #     rmse_error=evaluate(pre_model)
        #     if max_rmse>rmse_error:
        #         max_rmse=rmse_error
        #         pre_model.saver.save(pre_model.sess,save_path='weights/pollutant.ckpt')

def evaluate(pre_model):
    '''
    :param para:
    :param pre_model:
    :return:
    '''
    label_list = list()
    predict_list = list()

    #with tf.Session() as sess:
    model_file = tf.train.latest_checkpoint('weights/')
    if not pre_model.para.is_training:
        print('the model weights has been loaded:')
        pre_model.saver.restore(pre_model.sess, model_file)

    pre_model.iterate.is_training = False
    pre_model.iterate.normalize = pre_model.para.normalize
    iterate_test = pre_model.iterate
    next_ = iterate_test.next_batch(batch_size=pre_model.para.batch_size, epochs=1)
    max,min=iterate_test.max_list[1],iterate_test.min_list[1]
    #print('the numbers of loop is :',(int(iterate.shape[0] * (1-iterate.data_divide)) - iterate.prediction_size)// (iterate.time_size * pre_model.para.batch_size))
    for i in range((int(iterate_test.shape[0] * (1-iterate_test.data_divide)) - iterate_test.prediction_size)//
                   (iterate_test.time_size * pre_model.para.batch_size)):
    #for i in range(2):
        # train process
        x, label = pre_model.sess.run(next_)
        #print('In the test step, the x shape is :',x.shape)
        pre_label,bi_alpha_, l_alpha_ ,w_= pre_model.sess.run((pre_model.pre, pre_model.bi_alpha, pre_model.l_alpha,pre_model.w),
                                                           feed_dict={pre_model.x_input: x})
        label_list.append(label)
        #print('the weights of decoder is :',w_)
        print('the predict value is :',re_current(pre_label[0],max,min))
        print('the real value is :',re_current(label[0],max,min))
        #print('the predict value is :', [round(float(num),2) for num in pre_label[0]])
        #print('the real value is :',label[0])
        predict_list.append(pre_label)

    label_list = np.array(re_current(np.reshape(np.array(label_list), [-1]),max,min))
    predict_list = np.array(re_current(np.reshape(np.array(predict_list), [-1]),max,min))

    average_Error, rmse_error, cor = pre_model.accuracy(label_list, predict_list)  #产生预测指标
    #pre_model.describe(label_list, predict_list, pre_model.para.prediction_size)   #预测值可视化
    return rmse_error

def main(argv=None):
    '''
    :param argv:
    :return:
    '''
    para = parameter()
    print('Please input a number : 1 or 0. (1 and 0 represents the training or testing, respectively).')
    val = input('please input the number : ')

    pre_model = Model(para)
    pre_model.model()
    pre_model.initialize_session()

    if int(val) == 1:para.is_training = True
    else:
        para.batch_size=32
        para.is_training = False

    if int(val) == 1:run_epoch(pre_model)
    else:evaluate(pre_model)

    print('finished____________________________finished_____________________________finished!!!')

if __name__ == '__main__':
    main()


#!/usr/bin/env python3

import os
import sys
import socket

from urllib.request import urlopen

from path import path
from mbhlib import sh

HERE = path(__file__).parent
JD = HERE.parent.parent.parent
USER = path(os.environ.get('USER', os.environ.get('USERPROFILE'))).basename()
HOST = socket.gethostname().split('.')[0]
if not USER:
    try:
        USER = os.getlogin()
    except AttributeError:
        USER = 'dbuch'

HOME = path(os.environ['HOME'])

def main():
    sysargs = sys.argv[1:]
    if '-h' in sysargs or '--help' in sysargs:
        sys.stdout.write('Usage: install [--janus]\n')
        return 2

    install_scripts()
    install_binaries()

    if '--janus' in sysargs:
        install_janus()
    return 0

def _install_link(frompath, topath):
    sh('rm -rf "%s"' % topath)
    sh('ln -s "{}" "{}"'.format(frompath, topath))

def _dircopy(prefix, dirname, ext='.py'):
    for pth in (JD/dirname).files():
        topath = HOME/prefix/pth.name
        _install_link(pth, topath)

def _bincopy(dirname, ext='.py'):
    _dircopy('bin', dirname, ext=ext)

def install_scripts():
    _bincopy('scripts')

def install_binaries():
    sh('cd {junkdrawer}/src && make && make install'.format(junkdrawer=JD))

def install_janus():
    if (HOME/'.vim'/'README.md').isfile():
        cwd = os.getcwd()
        os.chdir(HOME/'.vim')
        sh('rake')
    else:
        bootstrap = HOME/'.janus-bootstrap.sh'
        if not bootstrap.isfile():
            bootstrap.write_bytes(
                urlopen('https://raw.github.com/carlhuda/janus/master/bootstrap.sh').read()
            )
        bootstrap.chmod(0o755)
        sh(bootstrap.abspath())


#산포도차트

import pandas as pd
import matplotlib.pyplot as plt

marathon_2015_2017 = pd.read_csv("D:\creapple\python_matplotlib\data/marathon_2015_2017.csv")

# Select male and female runners by conditional expression
MALE_runner = marathon_2015_2017[marathon_2015_2017['M/F'] == 'M']
FEMALE_runner = marathon_2015_2017[marathon_2015_2017['M/F'] == 'F']

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

x_male = MALE_runner.Age
y_male = MALE_runner['Official Time']

x_female = FEMALE_runner.Age
y_female = FEMALE_runner['Official Time']

plt.plot(x_male, y_male, '.', color='b', alpha=0.5)
plt.plot(x_female, y_female, '.', color='r', alpha=0.5)
# Generate labels and title
plt.xlabel("Age", fontsize=16)
plt.ylabel("Official Time (Second)",fontsize=16)
plt.title("Distribution by Running Time and Age",fontsize=20)
# Show plot
plt.show()


from collections import defaultdict
from enum import Enum
from os.path import join, realpath
import sys; sys.path.insert(0, realpath(join("/home/ubuntu/hummingbot/hummingbot/connector/derivative/dydx_perpetual/dydx_perpetual_derivative.py", "../../../")))

from hummingbot.connector.derivative.position import Position

from hummingbot.core.network_iterator import NetworkStatus
from hummingbot.core.utils.estimate_fee import estimate_fee

from async_timeout import timeout

from hummingbot.core.clock import Clock
from hummingbot.core.data_type.cancellation_result import CancellationResult
from hummingbot.connector.derivative.dydx_perpetual.dydx_perpetual_in_flight_order import DydxPerpetualsInFlightOrder

from hummingbot.core.utils.tracking_nonce import get_tracking_nonce

import asyncio
import hashlib
import hmac
import time
import logging
from decimal import Decimal
from typing import Optional, List, Dict, Any, AsyncIterable
from urllib.parse import urlencode

import aiohttp

from hummingbot.core.data_type.order_book import OrderBook
from hummingbot.core.event.events import (
    OrderType,
    TradeType,
    MarketOrderFailureEvent,
    MarketEvent,
    OrderCancelledEvent,
    BuyOrderCompletedEvent,
    BuyOrderCreatedEvent,
    SellOrderCreatedEvent,
    OrderFilledEvent,
    SellOrderCompletedEvent, PositionSide, PositionMode, PositionAction)
from hummingbot.core.utils.async_utils import safe_ensure_future, safe_gather
from hummingbot.core.utils.asyncio_throttle import Throttler
from hummingbot.logger import HummingbotLogger
from hummingbot.connector.derivative.dydx_perpetual.dydx_perpetual_order_book_tracker import DydxPerpetualOrderBookTracker
from hummingbot.connector.derivative.dydx_perpetual.dydx_perpetual_user_stream_tracker import DydxPerpetualUserStreamTracker
# from hummingbot.connector.derivative.dydx_perpetual.dydx_perpetual_utils import convert_from_exchange_trading_pair, convert_to_exchange_trading_pair
from hummingbot.connector.derivative.dydx_perpetual.constants import (
    PERPETUAL_BASE_URL,
    TESTNET_BASE_URL,
    DIFF_STREAM_URL,
    TESTNET_STREAM_URL
)
from hummingbot.connector.derivative_base import DerivativeBase, s_decimal_NaN
from hummingbot.connector.trading_rule import TradingRule

from dydx3 import Client
import configparser
from dydx3.constants import NETWORK_ID_MAINNET, NETWORK_ID_ROPSTEN
import json
from datetime import datetime
from hummingbot.core.event.events import TradeFee, TradeFeeType
from hummingbot.core.clock_mode import ClockMode

class MethodType(Enum):
    GET = "GET"
    POST = "POST"
    DELETE = "DELETE"
    PUT = "PUT"

bpm_logger = None

BROKER_ID = "x-3QreWesy"

def get_client_order_id(order_side: str, trading_pair: object):
    nonce = get_tracking_nonce()
    symbols: str = trading_pair.split("-")
    base: str = symbols[0].upper()
    quote: str = symbols[1].upper()
    return f"{BROKER_ID}-{order_side.upper()[0]}{base[0]}{base[-1]}{quote[0]}{quote[-1]}{nonce}"

class DydxPerpetualDerivative(DerivativeBase):
    MARKET_RECEIVED_ASSET_EVENT_TAG = MarketEvent.ReceivedAsset
    MARKET_BUY_ORDER_COMPLETED_EVENT_TAG = MarketEvent.BuyOrderCompleted
    MARKET_SELL_ORDER_COMPLETED_EVENT_TAG = MarketEvent.SellOrderCompleted
    MARKET_ORDER_CANCELLED_EVENT_TAG = MarketEvent.OrderCancelled
    MARKET_TRANSACTION_FAILURE_EVENT_TAG = MarketEvent.TransactionFailure
    MARKET_ORDER_FAILURE_EVENT_TAG = MarketEvent.OrderFailure
    MARKET_ORDER_FILLED_EVENT_TAG = MarketEvent.OrderFilled
    MARKET_BUY_ORDER_CREATED_EVENT_TAG = MarketEvent.BuyOrderCreated
    MARKET_SELL_ORDER_CREATED_EVENT_TAG = MarketEvent.SellOrderCreated

    API_CALL_TIMEOUT = 10.0
    SHORT_POLL_INTERVAL = 5.0
    UPDATE_ORDER_STATUS_MIN_INTERVAL = 10.0
    LONG_POLL_INTERVAL = 120.0
    ORDER_NOT_EXIST_CONFIRMATION_COUNT = 3

    @classmethod
    def logger(cls) -> HummingbotLogger:
        global bpm_logger
        if bpm_logger is None:
            bpm_logger = logging.getLogger(__name__)
        return bpm_logger

    def __init__(self,
                 dydx_perpetual_api_key: str = None,
                 dydx_perpetual_api_secret: str = None,
                 dydx_perpetual_api_passphrase: str = None,
                 dydx_perpetual_eth_wallet: str = None,
                 trading_pairs: Optional[List[str]] = None,
                 trading_required: bool = True,
                 **domain):
#         self._testnet = True if len(domain) > 0 else False
        self._testnet = True
        super().__init__()
        self._api_key = dydx_perpetual_api_key
        self._api_secret = dydx_perpetual_api_secret

        # dydx_perpetual authentication credentials are obtained from config file rather than input variables due to unconventional credentials different from centralized exchanges.
        self._eth_address = dydx_perpetual_eth_wallet
        self._api_key_credentials = {'secret': dydx_perpetual_api_secret, 'key': dydx_perpetual_api_key, 'passphrase': dydx_perpetual_api_passphrase}

        self._trading_required = trading_required
        self._account_balances = {}
        self._account_available_balances = {}

        self._base_url = PERPETUAL_BASE_URL if self._testnet is False else TESTNET_BASE_URL
        self._stream_url = DIFF_STREAM_URL if self._testnet is False else TESTNET_STREAM_URL
        self._network_id = NETWORK_ID_MAINNET if self._testnet is False else NETWORK_ID_ROPSTEN

        self._user_stream_tracker = DydxPerpetualUserStreamTracker(
            base_url=self._base_url,
            stream_url=self._stream_url,
            network_id=self._network_id,
            eth_address=self._eth_address,
            api_key_credentials=self._api_key_credentials
        )
        print(domain)
        domain = domain['domain']
        self._order_book_tracker = DydxPerpetualOrderBookTracker(trading_pairs=trading_pairs, domain=domain)
        self._ev_loop = asyncio.get_event_loop()
        self._poll_notifier = asyncio.Event()
        self._in_flight_orders = {}
        self._order_not_found_records = {}
        self._last_timestamp = 0
        self._trading_rules = {}
        # self._trade_fees = {} # To be implemented or removed
        # self._last_update_trade_fees_timestamp = 0 # To be implemented or removed
        self._status_polling_task = None
        self._user_stream_event_listener_task = None
        self._trading_rules_polling_task = None
        self._last_poll_timestamp = 0
        self._throttler = Throttler((10.0, 1.0))
        self._funding_rate = 0
        self._account_positions = {}
        self._position_mode = None
        self._leverage = 1

    @property
    def name(self) -> str:
        return "dydx_perpetual_testnet" if self._testnet else "dydx_perpetual"

    @property
    def order_books(self) -> Dict[str, OrderBook]:
        return self._order_book_tracker.order_books

    @property
    def ready(self):
        return all(self.status_dict.values())

    @property
    def in_flight_orders(self) -> Dict[str, DydxPerpetualsInFlightOrder]:
        return self._in_flight_orders

    @property
    def status_dict(self):
        return {
            "order_books_initialized": self._order_book_tracker.ready,
            "account_balance": len(self._account_balances) > 0 if self._trading_required else True,
            "trading_rule_initialized": len(self._trading_rules) > 0,

            # TODO: Uncomment when figured out trade fees
            # "trade_fees_initialized": len(self._trade_fees) > 0
        }

    @property
    def limit_orders(self):
        return [in_flight_order.to_limit_order() for in_flight_order in self._in_flight_orders.values()]

    def start(self, clock: Clock, timestamp: float):
        super().start(clock, timestamp)

    def stop(self, clock: Clock):
        super().stop(clock)

    async def start_network(self):
        self._order_book_tracker.start()
        self._trading_rules_polling_task = safe_ensure_future(self._trading_rules_polling_loop())
        if self._trading_required:
            self._status_polling_task = safe_ensure_future(self._status_polling_loop())
            self._user_stream_tracker_task = safe_ensure_future(self._user_stream_tracker.start())
            self._user_stream_event_listener_task = safe_ensure_future(self._user_stream_event_listener())

    def _stop_network(self):

        self._order_book_tracker.stop()
        if self._status_polling_task is not None:
            self._status_polling_task.cancel()
        if self._user_stream_tracker_task is not None:
            self._user_stream_tracker_task.cancel()
        if self._user_stream_event_listener_task is not None:
            self._user_stream_event_listener_task.cancel()
        if self._trading_rules_polling_task is not None:
            self._trading_rules_polling_task.cancel()
        self._status_polling_task = self._user_stream_tracker_task = \
            self._user_stream_event_listener_task = None

    async def stop_network(self):
        self._stop_network()

    async def check_network(self) -> NetworkStatus:
        try:
            client = Client(host=self._base_url,)
            client.public.get_time()
        except asyncio.CancelledError:
            raise
        except Exception:
            return NetworkStatus.NOT_CONNECTED
        return NetworkStatus.CONNECTED

    def supported_order_types(self) -> List[OrderType]:
        """
        :return a list of OrderType supported by this connector.
        Note that Market order type is no longer required and will not be used.
        """
#         return [OrderType.LIMIT, OrderType.MARKET]
        return [OrderType.LIMIT]

    # ORDER PLACE AND CANCEL EXECUTIONS ---
    async def create_order(self,
                           trade_type: TradeType,
                           order_id: str,
                           trading_pair: str,
                           amount: Decimal,
                           order_type: OrderType,
                           position_action: PositionAction,
                           price: Optional[Decimal] = Decimal("NaN")):

        trading_rule: TradingRule = self._trading_rules[trading_pair]
        if position_action not in [PositionAction.OPEN, PositionAction.CLOSE]:
            raise ValueError("Specify either OPEN_POSITION or CLOSE_POSITION position_action.")

        amount = self.quantize_order_amount(trading_pair, amount)
        price = self.quantize_order_price(trading_pair, price)

        if amount < trading_rule.min_order_size:
            raise ValueError(f"Buy order amount {amount} is lower than the minimum order size "
                             f"{trading_rule.min_order_size}")

        order_result = None
        api_params = {"symbol": trading_pair,
                      "side": "BUY" if trade_type is TradeType.BUY else "SELL",
                      "type": "LIMIT" if order_type is OrderType.LIMIT else "MARKET",
                      "quantity": f"{amount}",
                      "newClientOrderId": order_id
                      }
        if order_type == OrderType.LIMIT:
            api_params["price"] = f"{price}"
            api_params["timeInForce"] = "GTC"

        if self._position_mode == PositionMode.HEDGE:
            if position_action == PositionAction.OPEN:
                api_params["positionSide"] = "LONG" if trade_type is TradeType.BUY else "SHORT"
            else:
                api_params["positionSide"] = "SHORT" if trade_type is TradeType.BUY else "LONG"

        self.start_tracking_order(order_id, "", trading_pair, trade_type, price, amount, order_type, self._leverage, position_action.name)

        # To be updated!!! (Priority)
        try:
            client = Client(
                network_id=self._network_id,
                host=self._base_url,
                default_ethereum_address = self._eth_address,
                api_key_credentials=self._api_key_credentials,
            )
            account_response = client.private.get_account()
            position_id = account_response['account']['positionId']

            # Post an bid at a price that is unlikely to match.
            order_params = {
                'position_id': position_id,
                'market': api_params['symbol'],
                'side': api_params['side'],
                'order_type': api_params['type'],
                'post_only': False,
                'size': api_params['quantity'],
                'price': api_params['price'],
                'limit_fee': '0.0015',
                'expiration_epoch_seconds': time.time() + 99999999,
            }
            result_raw = client.private.create_order(**order_params)['order']
            exchange_order_id = str(result_raw["id"])

            # Check if order is filled immediately
            time.sleep(0.05)
            result_fill = client.private.get_fills(**{'order_id':exchange_order_id})['fills']
            avgPrice = "0.00000"
            executedQty = "0.00000"
            if result_fill and len(result_fill) > 0:
                avgPrice = result_fill[0]['price']
                executedQty = result_fill[0]['size']

            # Reformat order_result
            order_result = {
                'orderId': exchange_order_id,
                'symbol': result_raw['market'],
                'status': 'NEW',
                'clientOrderId': order_id,
                'price': result_raw['price'],
                'avgPrice': avgPrice,
                'origQty': result_raw['size'],
                'executedQty': executedQty,
                'cumQty': '0',
                'cumQuote': '0',
                'timeInForce': 'GTC',
                'type': result_raw['type'],
                'reduceOnly': False,
                'closePosition': False,
                'side': result_raw['side'],
                'positionSide': 'BOTH',
                'stopPrice': '0',
                'workingType': 'CONTRACT_PRICE',
                'priceProtect': False,
                'origType': 'LIMIT',
                'updateTime': time.time()
            }

            tracked_order = self._in_flight_orders.get(order_id)
            if tracked_order is not None:
                self.logger().info(f"Created {order_type.name.lower()} {trade_type.name.lower()} order {order_id} for "
                                   f"{amount} {trading_pair}.")
                tracked_order.exchange_order_id = exchange_order_id

            event_tag = self.MARKET_BUY_ORDER_CREATED_EVENT_TAG if trade_type is TradeType.BUY \
                else self.MARKET_SELL_ORDER_CREATED_EVENT_TAG
            event_class = BuyOrderCreatedEvent if trade_type is TradeType.BUY else SellOrderCreatedEvent
            self.trigger_event(event_tag,
                               event_class(self.current_timestamp,
                                           order_type,
                                           trading_pair,
                                           amount,
                                           price,
                                           order_id,
                                           leverage=self._leverage,
                                           position=position_action.name))
            return order_result
        except asyncio.CancelledError:
            raise
        except Exception as e:
            self.stop_tracking_order(order_id)
            self.logger().network(
                f"Error submitting order to Binance Perpetuals for {amount} {trading_pair} "
                f"{'' if order_type is OrderType.MARKET else price}.",
                exc_info=True,
                app_warning_msg=str(e)
            )
            self.trigger_event(self.MARKET_ORDER_FAILURE_EVENT_TAG,
                               MarketOrderFailureEvent(self.current_timestamp, order_id, order_type))

    async def execute_buy(self,
                          order_id: str,
                          trading_pair: str,
                          amount: Decimal,
                          order_type: OrderType,
                          position_action: PositionAction,
                          price: Optional[Decimal] = s_decimal_NaN):
        return await self.create_order(TradeType.BUY, order_id, trading_pair, amount, order_type, position_action, price)

    def buy(self, trading_pair: str, amount: object, order_type: object = OrderType.MARKET,
            price: object = s_decimal_NaN, **kwargs) -> str:

        t_pair: str = trading_pair
        order_id: str = get_client_order_id("sell", t_pair)
        safe_ensure_future(self.execute_buy(order_id, trading_pair, amount, order_type, kwargs["position_action"], price))
        return order_id

    async def execute_sell(self,
                           order_id: str,
                           trading_pair: str,
                           amount: Decimal,
                           order_type: OrderType,
                           position_action: PositionAction,
                           price: Optional[Decimal] = s_decimal_NaN):
        return await self.create_order(TradeType.SELL, order_id, trading_pair, amount, order_type, position_action, price)

    def sell(self, trading_pair: str, amount: object, order_type: object = OrderType.MARKET,
             price: object = s_decimal_NaN, **kwargs) -> str:

        t_pair: str = trading_pair
        order_id: str = get_client_order_id("sell", t_pair)
        safe_ensure_future(self.execute_sell(order_id, trading_pair, amount, order_type, kwargs["position_action"], price))
        return order_id

    async def cancel_all(self, timeout_seconds: float):
        incomplete_orders = [order for order in self._in_flight_orders.values() if not order.is_done]
        tasks = [self.execute_cancel(order.trading_pair, order.client_order_id) for order in incomplete_orders]
        order_id_set = set([order.client_order_id for order in incomplete_orders])
        successful_cancellations = []

        try:
            async with timeout(timeout_seconds):
                cancellation_results = await safe_gather(*tasks, return_exceptions=True)
                for cancel_result in cancellation_results:
                    # TODO: QUESTION --- SHOULD I CHECK FOR THE BinanceAPIException CONSIDERING WE ARE MOVING AWAY FROM BINANCE-CLIENT?
                    if isinstance(cancel_result, dict) and "clientOrderId" in cancel_result:
                        client_order_id = cancel_result.get("clientOrderId")
                        order_id_set.remove(client_order_id)
                        successful_cancellations.append(CancellationResult(client_order_id, True))
        except Exception:
            self.logger().network(
                "Unexpected error cancelling orders.",
                exc_info=True,
                app_warning_msg="Failed to cancel order with Binance Perpetual. Check API key and network connection."
            )
        failed_cancellations = [CancellationResult(order_id, False) for order_id in order_id_set]
        return successful_cancellations + failed_cancellations

    async def cancel_all_account_orders(self, trading_pair: str):
        try:
            client = Client(
                network_id=self._network_id,
                host=self._base_url,
                default_ethereum_address = self._eth_address,
                api_key_credentials=self._api_key_credentials,
            )
            response = client.private.cancel_all_orders(market=trading_pair)
            if response['cancelOrders'] and len(response['cancelOrders']) >= 0:
                for order_id in list(self._in_flight_orders.keys()):
                    self.stop_tracking_order(order_id)
            else:
                raise IOError(f"Error cancelling all account orders. Server Response: {response}")
        except Exception as e:
            self.logger().error("Could not cancel all account orders.")
            raise e

    def cancel(self, trading_pair: str, client_order_id: str):
        safe_ensure_future(self.execute_cancel(trading_pair, client_order_id))
        return client_order_id

    async def execute_cancel(self, trading_pair: str, client_order_id: str):
        try:
            # Find exchange_order_id using client_order_id
            open_orders = [order for order in self._in_flight_orders.values() if not order.is_done]
            order_id_set = {order.client_order_id:order.exchange_order_id for order in open_orders}
            print(order_id_set)
            exchange_order_id = order_id_set[client_order_id]

            client = Client(
                network_id=self._network_id,
                host=self._base_url,
                default_ethereum_address = self._eth_address,
                api_key_credentials=self._api_key_credentials,
            )
            response = client.private.cancel_order(order_id)

            if response['cancelOrder']['status']=='OPEN':
                self.logger().info(f"Successfully canceled order {client_order_id}")
                self.stop_tracking_order(client_order_id)
                self.trigger_event(self.MARKET_ORDER_CANCELLED_EVENT_TAG,
                                   OrderCancelledEvent(self.current_timestamp, client_order_id))
                return response

        except Exception as e:
            try:
                error_response = json.loads(e.response.text)
                error_msg = error_response['errors'][0]['msg']
                if 'it is already canceled' in error_msg or 'No order exists with id' in error_msg:
                    self.logger().debug(f"The order {client_order_id} does not exist on Binance Perpetuals. "
                                        f"No cancellation needed.")
                    self.stop_tracking_order(client_order_id)
                    self.trigger_event(self.MARKET_ORDER_CANCELLED_EVENT_TAG,
                                       OrderCancelledEvent(self.current_timestamp, client_order_id))
                    return {"origClientOrderId": client_order_id}
            except:
                pass
            self.logger().error(f"Could not cancel order {client_order_id} (on Binance Perp. {trading_pair})")
            raise e
        return response

    # TODO: Implement
    async def close_position(self, trading_pair: str):
        pass

    def quantize_order_amount(self, trading_pair: str, amount: object, price: object = Decimal(0)):
        trading_rule: TradingRule = self._trading_rules[trading_pair]
        # current_price: object = self.get_price(trading_pair, False)
        notional_size: object
        quantized_amount = DerivativeBase.quantize_order_amount(self, trading_pair, amount)
        if quantized_amount < trading_rule.min_order_size:
            return Decimal(0)
        """
        if price == Decimal(0):
            notional_size = current_price * quantized_amount
        else:
            notional_size = price * quantized_amount
        """

        # TODO: NOTIONAL MIN SIZE DOES NOT EXIST
        # if notional_size < trading_rule.min_notional_size * Decimal("1.01"):
        #     return Decimal(0)

        return quantized_amount

    def get_order_price_quantum(self, trading_pair: str, price: object):
        trading_rule: TradingRule = self._trading_rules[trading_pair]
        return trading_rule.min_price_increment

    def get_order_size_quantum(self, trading_pair: str, order_size: object):
        trading_rule: TradingRule = self._trading_rules[trading_pair]
        return Decimal(trading_rule.min_base_amount_increment)

    # ORDER TRACKING ---
    def start_tracking_order(self, order_id: str, exchange_order_id: str, trading_pair: str, trading_type: object,
                             price: object, amount: object, order_type: object, leverage: int, position: str):
        self._in_flight_orders[order_id] = DydxPerpetualsInFlightOrder(
            client_order_id=order_id,
            exchange_order_id=exchange_order_id,
            trading_pair=trading_pair,
            order_type=order_type,
            trade_type=trading_type,
            price=price,
            amount=amount,
            leverage=leverage,
            position=position

        )

    def stop_tracking_order(self, order_id: str):
        if order_id in self._in_flight_orders:
            del self._in_flight_orders[order_id]
        if order_id in self._order_not_found_records:
            del self._order_not_found_records[order_id]

    # Delete this??
    async def _iter_user_event_queue(self) -> AsyncIterable[Dict[str, any]]:
        while True:
            try:
#                 yield await self._user_stream_tracker.start()
#                 yield await self._user_stream_tracker.user_stream
                yield await self._user_stream_tracker.user_stream.get()
            except asyncio.CancelledError:
                raise
            except Exception:
                self.logger().network(
                    "Unknown error. Retrying after 1 seconds.",
                    exc_info=True,
                    app_warning_msg="Could not fetch user events from Binance. Check API key and network connection."
                )
                await asyncio.sleep(1.0)
    # This function:
    # Get order trade update. if order has been filled or If order has been cancelled
    # ACCOUNT_UPDATE, # Update balance # update position
    # MARGIN_CALL
    async def _user_stream_event_listener(self):
        async for event_message in self._iter_user_event_queue():
            try:
                if event_message['type'] != 'subscribed':
                    continue
                # If user stream event is an order update
                if 'orders' in event_message['contents']:
                    orders = event_message['contents']['orders']

                    # For each order
                    for order in orders:
                        exchange_order_id = order['id']

                        # Get open orders
                        open_orders = [order for order in self._in_flight_orders.values() if not order.is_done]
                        order_id_set = {order.exchange_order_id:order.client_order_id for order in open_orders}

                        # If the order has been cancelled
                        if exchange_order_id not in order_id_set:
                            continue

                        # Find client_order_id using exchange_order_id
                        client_order_id = order_id_set[exchange_order_id]

                        # Update _in_flight_orders
                        tracked_order = self._in_flight_orders.get(client_order_id)
                        trade_fee = self.get_fee(
                            base_currency=order['market'].split('-')[0],
                            quote_currency=order['market'].split('-')[1],
                            order_type=order['type'],
                            order_side=order['side'],
                            amount=Decimal(order['size']),
                            price=Decimal(order['price'])
                        )
                        order['trade_fee'] = trade_fee
                        tracked_order.update_with_execution_report(order)

                        # Send trigger_event for order fill event
                        trade_type = TradeType.BUY if order['side'] == "BUY" else TradeType.SELL
                        if order['status'] in ["ENTIRELY_FILLED", "FILLED", "CLOSED"]:
                            order_filled_event = OrderFilledEvent(
                                timestamp=int(time.time()),
                                order_id=client_order_id,
                                trading_pair=order['market'],
                                trade_type=trade_type,
                                order_type = OrderType.LIMIT if order['type'] == "LIMIT" else OrderType.MARKET,
                                price=Decimal(order['price']),
                                amount=Decimal(order['size']),
                                leverage=self._leverage,
                                trade_fee=trade_fee,
                                exchange_trade_id=order_message.get("t"),
                                position=tracked_order.position
                            )
                            self.trigger_event(self.MARKET_ORDER_FILLED_EVENT_TAG, order_filled_event)

                        # Send trigger_event if order is done
                        if tracked_order.is_done:
                            if not tracked_order.is_failure:
                                event_tag = None
                                event_class = None
                                if trade_type is TradeType.BUY:
                                    event_tag = self.MARKET_BUY_ORDER_COMPLETED_EVENT_TAG
                                    event_class = BuyOrderCompletedEvent
                                else:
                                    event_tag = self.MARKET_SELL_ORDER_COMPLETED_EVENT_TAG
                                    event_class = SellOrderCompletedEvent
                                self.logger().info(f"The {tracked_order.order_type.name.lower()} {trade_type} order {client_order_id} has completed "
                                                   f"according to websocket delta.")
                                self.trigger_event(event_tag,
                                                   event_class(self.current_timestamp,
                                                               client_order_id,
                                                               tracked_order.base_asset,
                                                               tracked_order.quote_asset,
                                                               (tracked_order.fee_asset or tracked_order.quote_asset),
                                                               tracked_order.executed_amount_base,
                                                               tracked_order.executed_amount_quote,
                                                               tracked_order.fee_paid,
                                                               tracked_order.order_type))
                            else:
                                if tracked_order.is_cancelled:
                                    if tracked_order.client_order_id in self._in_flight_orders:
                                        self.logger().info(f"Successfully cancelled order {tracked_order.client_order_id} according to websocket delta.")
                                        self.trigger_event(self.MARKET_ORDER_CANCELLED_EVENT_TAG,
                                                           OrderCancelledEvent(self.current_timestamp,
                                                                               tracked_order.client_order_id))
                                    else:
                                        self.logger().info(f"The {tracked_order.order_type.name.lower()} order {tracked_order.client_order_id} has failed "
                                                           f"according to websocket delta.")
                                        self.trigger_event(self.MARKET_ORDER_FAILURE_EVENT_TAG,
                                                           MarketOrderFailureEvent(self.current_timestamp,
                                                                                tracked_order.client_order_id,
                                                                                tracked_order.order_type))
                            self.stop_tracking_order(tracked_order.client_order_id)

                # if user stream event is an account update
                if 'account' in event_message['contents']:
                    update_data = event_message['contents']['account']

                    # Update balances - dydx_perpetual only has USDC collateral currently
                    asset_name = "USDC"
                    self._account_balances[asset_name] = Decimal(update_data["freeCollateral"]) # Trading wallet balance
                    self._account_available_balances[asset_name] = Decimal(update_data["freeCollateral"]) # Cross wallet balance. Not available in dydx_perpetual.

                    # update positions
                    open_positions = update_data['openPositions']
                    for market in open_positions:
                        open_position = open_positions[market]
                        position = self._account_positions.get(market, None)
                        if position is not None:
                            position.update_position(position_side=open_position['side'],
                                                     unrealized_pnl = Decimal(open_position['unrealizedPnl']),
                                                     entry_price = Decimal(open_position['entryPrice']),
                                                     amount = Decimal(open_position['size']))
                        else:
                            await self._update_positions()

            except asyncio.CancelledError:
                raise
            except Exception as e:
                self.logger().error(f"Unexpected error in user stream listener loop: {e}", exc_info=True)
                await asyncio.sleep(5.0)

    def tick(self, timestamp: float):
        """
        Is called automatically by the clock for each clock's tick (1 second by default).
        It checks if status polling task is due for execution.
        """
        now = time.time()
        poll_interval = (self.SHORT_POLL_INTERVAL
                         if now - self._user_stream_tracker.last_recv_time > 60.0
                         else self.LONG_POLL_INTERVAL)
        last_tick = int(self._last_timestamp / poll_interval)
        current_tick = int(timestamp / poll_interval)
        if current_tick > last_tick:
            if not self._poll_notifier.is_set():
                self._poll_notifier.set()
        self._last_timestamp = timestamp

    # MARKET AND ACCOUNT INFO ---
    def get_fee(self, base_currency: str, quote_currency: str, order_type: object, order_side: object,
                amount: object, price: object):
        is_maker = order_type is OrderType.LIMIT
        fee = estimate_fee("dydx_perpetual", is_maker)
        try:
            client = Client(
                network_id=self._network_id,
                host=self._base_url,
                default_ethereum_address = self._eth_address,
                api_key_credentials=self._api_key_credentials,
            )
            user_info = client.private.get_user()
            fee = Decimal(user_info['user']['makerFeeRate']) if is_maker else Decimal(user_info['user']['takerFeeRate'])
            fee = TradeFee(percent=fee, flat_fees=[])
        except Exception as e:
            print(e)
        return fee

    def get_order_book(self, trading_pair: str) -> OrderBook:
        order_books: dict = self._order_book_tracker.order_books
        if trading_pair not in order_books:
            raise ValueError(f"No order book exists for '{trading_pair}'.")
        return order_books[trading_pair]

    async def _update_trading_rules(self):
        last_tick = self._last_timestamp / 60.0
        current_tick = self.current_timestamp / 60.0
        if current_tick > last_tick or len(self._trading_rules) < 1:
            client = Client(host=self._base_url,)
            exchange_info = client.public.get_markets()['markets']
            trading_rules_list = self._format_trading_rules(exchange_info)
            self._trading_rules.clear()
            for trading_rule in trading_rules_list:
                self._trading_rules[trading_rule.trading_pair] = trading_rule

    def _format_trading_rules(self, exchange_info: Dict[str, Any]) -> List[TradingRule]:
        return_val = {key:{} for key in exchange_info}
        return_val = []
        for key in exchange_info:
            try:
                row = exchange_info[key]
                trading_pair = key
                min_order_size = row['minOrderSize']
                tick_size = row['tickSize']
                step_size = row['stepSize']

                return_val.append(TradingRule(
                    trading_pair,
                    min_order_size=Decimal(min_order_size),
                    min_price_increment=Decimal(tick_size),
                    min_base_amount_increment=Decimal(step_size),
                ))
            except Exception as e:
                self.logger().error(f"Error parsing the trading pair rule {rule}. Error: {e}. Skipping...",
                                    exc_info=True)
        return return_val

    async def _trading_rules_polling_loop(self):
        while True:
            try:
                await safe_gather(
                    self._update_trading_rules()

                    # TODO: Uncomment when implemented
                    # self._update_trade_fees()
                )
                await asyncio.sleep(60)
            except asyncio.CancelledError:
                raise
            except Exception:
                self.logger().network("Unexpected error while fetching trading rules.", exc_info=True,
                                      app_warning_msg="Could not fetch new trading rules from Binance Perpetuals. "
                                                      "Check network connection.")
                await asyncio.sleep(0.5)

    async def _status_polling_loop(self):
        while True:
            try:
                self._poll_notifier = asyncio.Event()
                await self._poll_notifier.wait()
                await safe_gather(
                    self._update_balances(),
                    self._update_positions()
                )
                await self._update_order_fills_from_trades(),
                await self._update_order_status()
                self._last_poll_timestamp = self.current_timestamp
            except asyncio.CancelledError:
                raise
            except Exception:
                self.logger().network("Unexpected error while fetching account updates.", exc_info=True,
                                      app_warning_msg="Could not fetch account updates from Binance Perpetuals. "
                                                      "Check API key and network connection.")
                await asyncio.sleep(0.5)

    # To be updated!!!!
    async def _update_balances(self):
        local_asset_names = {'USDC'}
        client = Client(
            network_id=self._network_id,
            host=self._base_url,
            default_ethereum_address = self._eth_address,
            api_key_credentials=self._api_key_credentials,
        )
        account = client.private.get_account()
        self._account_available_balances['USDC'] = account['account']['freeCollateral']
        self._account_balances['USDC'] = account['account']['equity']

    # TODO: Note --- Data Structure Assumes One-way Position Mode [not hedge position mode] (see Binance Futures Docs)
    # Note --- Hedge Mode allows for Both Long and Short Positions on a trading pair
    async def _update_positions(self):
        client = Client(
            network_id=self._network_id,
            host=self._base_url,
            default_ethereum_address = self._eth_address,
            api_key_credentials=self._api_key_credentials,
        )
        positions = client.private.get_positions(status='OPEN')['positions']
        for position in positions:
            trading_pair = position['market']
            position_side = PositionSide[position['side']]
            unrealized_pnl = Decimal(position['unrealizedPnl'])
            entry_price = Decimal(position['entryPrice'])
            amount = Decimal(position['size'])
            leverage = 25 # Leverage refers to the account's maximum leverage. Dydx does not have this feature yet, so set this to the maximum leverage possible.
            if amount != 0:
                self._account_positions[trading_pair + position_side.name] = Position(
                    trading_pair=trading_pair,
                    position_side=position_side,
                    unrealized_pnl=unrealized_pnl,
                    entry_price=entry_price,
                    amount=amount,
                    leverage=leverage
                )
            else:
                if (trading_pair + position_side.name) in self._account_positions:
                    del self._account_positions[trading_pair + position_side.name]

    async def _update_order_fills_from_trades(self):
        last_tick = int(self._last_poll_timestamp / self.UPDATE_ORDER_STATUS_MIN_INTERVAL)
        current_tick = int(self.current_timestamp / self.UPDATE_ORDER_STATUS_MIN_INTERVAL)

        if current_tick > last_tick and len(self._in_flight_orders) > 0:

            # Create exchange_order_id_to_order_map
            exchange_order_id_to_order_map = defaultdict(lambda: {})
            for order in self._in_flight_orders.values():
                exchange_order_id_to_order_map[order.exchange_order_id] = order

            # Get filled orders
            client = Client(
                network_id=self._network_id,
                host=self._base_url,
                default_ethereum_address = self._eth_address,
                api_key_credentials=self._api_key_credentials,
            )
            fills = client.private.get_fills(limit=100)['fills']
            self.logger().debug(f"Polling for order fills of trading_pairs.")

            # Update order status
            for fill in fills:
                fill_exchange_order_id = fill['orderId']
                if fill_exchange_order_id in exchange_order_id_to_order_map:
                    order = exchange_order_id_to_order_map[fill_exchange_order_id]
                    trade = {
                        'id':filled_order.exchange_order_id, # exchange_order_id
                        'order_id':filled_order.client_order_id, # order_id
                        'qty':fill['size'],
                        'quoteQty':Decimal(fill['size']) * Decimal(fill['price']),
                        'commission':fill['fee'],
                        'commissionAsset': 'USDC', #optional
                    }
                    print('fill', fill)
                    print('trade', trade)
                    applied_trade = order.update_with_trade_updates(trade)
                    if applied_trade:
                        self.trigger_event(
                            self.MARKET_ORDER_FILLED_EVENT_TAG,
                            OrderFilledEvent(
                                self.current_timestamp,
                                order.client_order_id,
                                order.trading_pair,
                                order.trade_type,
                                order_type,
                                Decimal(trade.get("price")),
                                Decimal(trade.get("qty")),
                                self.get_fee(
                                    order.base_asset,
                                    order.quote_asset,
                                    order_type,
                                    order.trade_type,
                                    Decimal(trade["price"]),
                                    Decimal(trade["qty"])),
                                exchange_trade_id=trade["id"],
                                leverage=self._leverage,
                                position=order.position
                            )
                        )

    # To be updated!!!
    async def _update_order_status(self):
        last_tick = int(self._last_poll_timestamp / self.UPDATE_ORDER_STATUS_MIN_INTERVAL)
        current_tick = int(self.current_timestamp / self.UPDATE_ORDER_STATUS_MIN_INTERVAL)
        if current_tick > last_tick and len(self._in_flight_orders) > 0:
            tracked_orders = list(self._in_flight_orders.values())
            print('tracked_orders', tracked_orders)

            client = Client(
                network_id=self._network_id,
                host=self._base_url,
                default_ethereum_address = self._eth_address,
                api_key_credentials=self._api_key_credentials,
            )
            results = []
            for order in tracked_orders:
                try:
                    order_update = client.private.get_order_by_id(order_id=order.exchange_order_id)['order']
                except:
                    order_update = {'error':'order not found'}
                results.append(order_update)
            self.logger().debug(f"Polling for order status updates of {len(results)} orders.")
            for order_update, tracked_order in zip(results, tracked_orders):
                print('order_update',order_update)
                print('tracked_order',tracked_order)
                client_order_id = tracked_order.client_order_id
                if client_order_id not in self._in_flight_orders:
                    continue
                if isinstance(order_update, Exception):
                    # NO_SUCH_ORDER code
                    if order_update["error"] == 'order not found':
                        self._order_not_found_records[client_order_id] = \
                            self._order_not_found_records.get(client_order_id, 0) + 1
                        if self._order_not_found_records[client_order_id] < self.ORDER_NOT_EXIST_CONFIRMATION_COUNT:
                            continue
                        self.trigger_event(
                            self.MARKET_ORDER_FAILURE_EVENT_TAG,
                            MarketOrderFailureEvent(self.current_timestamp, client_order_id, tracked_order.order_type)
                        )
                        self.stop_tracking_order(client_order_id)
                    else:
                        self.logger().network(f"Error fetching status update for the order {client_order_id}: "
                                              f"{order_update}.")
                    continue

                tracked_order.last_state = order_update.get("status")
                order_type = OrderType.LIMIT if order_update.get("type") == "LIMIT" else OrderType.MARKET
                executed_amount_base = Decimal(order_update.get("size", "0")) - Decimal(order_update.get("remainingSize", "0"))
                executed_amount_quote = executed_amount_base * Decimal(order_update.get("price", "0"))

                if tracked_order.is_done:
                    if not tracked_order.is_failure:
                        event_tag = None
                        event_class = None
                        if tracked_order.trade_type is TradeType.BUY:
                            event_tag = self.MARKET_BUY_ORDER_COMPLETED_EVENT_TAG
                            event_class = BuyOrderCompletedEvent
                        else:

                            event_tag = self.MARKET_SELL_ORDER_COMPLETED_EVENT_TAG
                            event_class = SellOrderCompletedEvent
                        self.logger().info(f"The {order_type.name.lower()} {tracked_order.trade_type.name.lower()} order {client_order_id} has "
                                           f"completed according to order status API.")
                        self.trigger_event(event_tag,
                                           event_class(self.current_timestamp,
                                                       client_order_id,
                                                       tracked_order.base_asset,
                                                       tracked_order.quote_asset,
                                                       (tracked_order.fee_asset or tracked_order.base_asset),
                                                       executed_amount_base,
                                                       executed_amount_quote,
                                                       tracked_order.fee_paid,
                                                       order_type))
                    else:
                        if tracked_order.is_cancelled:
                            self.logger().info(f"Successfully cancelled order {client_order_id} according to order status API.")
                            self.trigger_event(self.MARKET_ORDER_CANCELLED_EVENT_TAG,
                                               OrderCancelledEvent(self.current_timestamp,
                                                                   client_order_id))
                        else:
                            self.logger().info(f"The {order_type.name.lower()} order {client_order_id} has failed according to "
                                               f"order status API.")
                            self.trigger_event(self.MARKET_ORDER_FAILURE_EVENT_TAG,
                                               MarketOrderFailureEvent(self.current_timestamp,
                                                                       client_order_id,
                                                                       order_type))
                    self.stop_tracking_order(client_order_id)

    # Dydx currently does not allow users to set maximum margin.
    async def _set_margin(self, trading_pair: str, leverage: int = 1):
        params = {
            "symbol": trading_pair,
            "leverage": leverage
        }
        self._leverage = leverage
        self.logger().info(f"Leverage Successfully set to {leverage}.")
#         if set_leverage["leverage"] == leverage:
#             self._leverage = leverage
#             self.logger().info(f"Leverage Successfully set to {leverage}.")
#         else:
#             self.logger().error("Unable to set leverage.")
        return leverage

    def set_margin(self, trading_pair: str, leverage: int = 1):
        safe_ensure_future(self._set_margin(trading_pair, leverage))

    """
    async def get_position_pnl(self, trading_pair: str):
        await self._update_positions()
        return self._account_positions.get(trading_pair)
    """

    async def _get_funding_rate(self, trading_pair):
        # TODO: Note --- the "premiumIndex" endpoint can get markPrice, indexPrice, and nextFundingTime as well
        client = Client(host=self._base_url,)
        market_stats = client.public.get_markets(market=trading_pair)
        self._funding_rate = Decimal(market_stats['markets'][trading_pair]['nextFundingRate'])

    # William's comment: currently self._funding_rate is a Decimal variable for storing funding rate for one trading_pair. Should upgrade it into a json list of funding rates for every trading_pair.
    def get_funding_rate(self, trading_pair):
        safe_ensure_future(self._get_funding_rate(trading_pair))
        return self._funding_rate

    async def _set_position_mode(self, position_mode: PositionMode):
        initial_mode = await self._get_position_mode()
        if initial_mode != position_mode:
            self.logger().error(f"Unable to set postion mode to {position_mode.name}.")
        else:
            self.logger().info(f"Using {position_mode.name} position mode.")

    async def _get_position_mode(self):
        # Set _position_mode to ONEWAY only
        if self._position_mode is None:
            self._position_mode = PositionMode.ONEWAY
        return self._position_mode

    def set_position_mode(self, position_mode: PositionMode):
        safe_ensure_future(self._set_position_mode(position_mode))


import logging
_logger = logging.getLogger(__name__)

from openerp.osv import osv,fields
from ads_product import ads_product

class product_product(osv.osv):
    """
    Add some fields to product to track synchronisation and trigger upload on write
    """
    _inherit = 'product.product'
    _columns = {
        'discount': fields.boolean('Discount', help="This product is used to add discounts to orders by giving a negative quantity")
    }

    def write(self, cr, uid, ids, values, context=None):
        """ Call ads_upload if we edit an uploaded field """
        res = super(product_product, self).write(cr, uid, ids, values, context=context)
        if any([field for field in ads_product.uploaded_fields if field in values.keys()]):
            self.ads_upload(cr, uid, ids, context=context)
        return res

    def create(self, cr, uid, values, context=None):
        """ Call ads_upload """
        res = super(product_product, self).create(cr, uid, values, context=context)
        self.ads_upload(cr, uid, res, context=context)
        return res

    def ads_upload_all(self, cr, uid, context=None):
        ids = self.search(cr, uid, [('x_new_ref', '!=', '')])
        _logger.info("Starting upload of %d products" % len(ids))
        self.ads_upload(cr, uid, ids, log=True, context=context)
        return True

    def ads_upload(self, cr, uid, ids, log=False, context=None):
        """ Upload product to ads server """
        if not isinstance(ids, (list, tuple)):
            ids = [ids]

        for product_id in ids:
            product = self.browse(cr, uid, product_id, context=context)
            if not product.x_new_ref:
                continue
            data = ads_product(product)
            data.upload(cr, self.pool.get('ads.manager'))
            if log:
                _logger.info("Uploaded product with ID %d" % product_id)
        return True

    def is_delivery_method(self, cr, uid, ids, context=None):
        """
        Returns a dictionary of product IDS, where each id maps to a list of carrier ids, or False
        if none were found
        """
        if not isinstance(ids, (list, tuple)):
            ids = [ids]

        is_delivery_map = dict.fromkeys(ids, False)
        carrier_obj = self.pool['delivery.carrier']

        for product_id in ids:
            delivery_method_ids = carrier_obj.search(cr, uid, [('product_id','=',product_id)])

            if delivery_method_ids:
                is_delivery_map[product_id] = delivery_method_ids

        return is_delivery_map


from datetime import datetime

from django import forms
from django.utils.translation import ugettext as _
from django.contrib.auth.models import User

from haystack.forms import SearchForm as HaystackSearchForm
from haystack.inputs import AutoQuery

from pybb.contrib.search.fields import TreeModelMultipleChoiceField
from pybb.models import Forum, Topic

class SearchForm(HaystackSearchForm):
    user = forms.ModelChoiceField(queryset=User.objects.all(),
                                  required=False,
                                  label=_('Username'))

    replies = forms.IntegerField(label=_('Number of answers'),
                                 min_value=0, required=False)

    search_topic_name = forms.BooleanField(required=False)
    with_attachment = forms.BooleanField(required=False)

    start_date = forms.DateTimeField(required=False)
    end_date = forms.DateTimeField(required=False)

    topic = forms.ModelChoiceField(queryset=Topic.objects.all(),
                                   required=False,
                                   label=_('Topic'))

    def __init__(self, *args, **kwargs):
        super(SearchForm, self).__init__(*args, **kwargs)

        self.fields['forums'] = TreeModelMultipleChoiceField(queryset=Forum.objects.all(),
                                                             join_field='forum_id',
                                                             required=False)

    def clean(self):
        data = super(HaystackSearchForm, self).clean()
        if data.get('start_date', None):
            if not data.get('end_date', None):
                data['end_date'] = datetime.now()

            if data['start_date'] > data['end_date']:
                raise forms.ValidationError('Start date is after end date')

        return data

    def search(self):
        if not self.is_valid():
            return self.no_query_found()

        # topic name search
        if self.cleaned_data.get('search_topic_name', False):
            sqs = self.searchqueryset.filter(
                topic_name=AutoQuery(self.cleaned_data['q']))
            sqs = sqs.filter(is_first_post=True)
        else:
            sqs = super(SearchForm, self).search()

        sqs = sqs.order_by('-created')

        forums = self.cleaned_data.get('forums', None)
        if forums:
            sqs = sqs.filter(topic_breadcrumbs__in=[f.id for f in forums])
        if self.cleaned_data.get('user', None):
            sqs = sqs.filter(user_id=self.cleaned_data['user'].pk)
        if self.cleaned_data.get('replies', None):
            sqs = sqs.filter(replies__gte=self.cleaned_data['replies'])
        if self.cleaned_data.get('topic', None):
            sqs = sqs.filter(topic_id=self.cleaned_data['topic'].pk)
        if self.cleaned_data.get('start_date', None):
            sqs = sqs.filter(updated__gte=self.cleaned_data['start_date'])
        if self.cleaned_data.get('end_date', None):
            sqs = sqs.filter(updated__lte=self.cleaned_data['end_date'])
        if self.cleaned_data.get('with_attachment', None):
            sqs = sqs.filter(has_attachment=self.cleaned_data['with_attachment'])

        return sqs


# Copyright 2017 Palantir Technologies, Inc.
# License: MIT

import logging.config
import argparse
import sys
import os

log = logging.getLogger(__name__)

LOG_FORMAT = "%(asctime)s UTC - %(levelname)s - %(name)s\n%(message)s\n\n"

_critical_error_log_file = os.path.join(
    os.path.expanduser("~"), "robotframework_ls_critical.log"
)

def _critical_msg(msg):
    with open(_critical_error_log_file, "a+") as stream:
        stream.write(msg + "\n")

def add_arguments(parser):
    parser.description = "Python Language Server"

    parser.add_argument(
        "--tcp", action="store_true", help="Use TCP server instead of stdio"
    )
    parser.add_argument("--host", default="127.0.0.1", help="Bind to this address")
    parser.add_argument("--port", type=int, default=1456, help="Bind to this port")
    parser.add_argument(
        "--check-parent-process",
        action="store_true",
        help="Check whether parent process is still alive using os.kill(ppid, 0) "
        "and auto shut down language server process when parent process is not alive."
        "Note that this may not work on a Windows machine.",
    )

    log_group = parser.add_mutually_exclusive_group()
    log_group.add_argument(
        "--log-config", help="Path to a JSON file containing Python logging config."
    )
    log_group.add_argument(
        "--log-file",
        help="Redirect logs to the given file instead of writing to stderr."
        "Has no effect if used with --log-config.",
    )

    parser.add_argument(
        "-v",
        "--verbose",
        action="count",
        default=0,
        help="Increase verbosity of log output, overrides log config file",
    )

def main(args=None, after_bind=lambda server: None):
    try:
        import robotframework_ls
    except ImportError:
        # Automatically add it to the path if __main__ is being executed.
        sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
        import robotframework_ls  # @UnusedImport

    from robotframework_ls.python_ls import (
        start_io_lang_server,
        start_tcp_lang_server,
    )
    from robotframework_ls.ext.robotframework_ls_impl import (
        RobotFrameworkLanguageServer,
    )

    parser = argparse.ArgumentParser()
    add_arguments(parser)

    args = parser.parse_args(args=args if args is not None else sys.argv[1:])
    _configure_logger(args.verbose, args.log_config, args.log_file)

    if args.tcp:
        start_tcp_lang_server(
            args.host, args.port, RobotFrameworkLanguageServer, after_bind=after_bind,
        )
    else:
        stdin, stdout = _binary_stdio()
        start_io_lang_server(stdin, stdout, RobotFrameworkLanguageServer)

class RedirectedStreamErrorOnAccess(object):
    def __getattr__(self, mname):
        raise AssertionError("This stream is now redirected and should not be used.")

def _binary_stdio():
    """Construct binary stdio streams (not text mode).

    This seems to be different for Window/Unix Python2/3, so going by:
        https://stackoverflow.com/questions/2850893/reading-binary-data-from-stdin
    """
    PY3K = sys.version_info >= (3, 0)

    if PY3K:
        stdin, stdout = sys.stdin.buffer, sys.stdout.buffer
    else:
        # Python 2 on Windows opens sys.stdin in text mode, and
        # binary data that read from it becomes corrupted on \r\n
        if sys.platform == "win32":
            # set sys.stdin to binary mode
            import msvcrt

            msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
            msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
        stdin, stdout = sys.stdin, sys.stdout
    sys.stdin, sys.stdout = (
        RedirectedStreamErrorOnAccess(),
        RedirectedStreamErrorOnAccess(),
    )

    return stdin, stdout

def _configure_logger(verbose=0, log_config=None, log_file=None):
    if getattr(_configure_logger, "called", False):
        return  # i.e.: in dev mode we may call multiple times
    _configure_logger.called = True

    root_logger = logging.root

    if log_config:
        import json

        with open(log_config, "r") as f:
            logging.config.dictConfig(json.load(f))
    else:
        formatter = logging.Formatter(LOG_FORMAT)
        if log_file:
            log_file = os.path.expanduser(log_file)
            log_handler = logging.handlers.RotatingFileHandler(
                log_file,
                mode="a",
                maxBytes=50 * 1024 * 1024,
                backupCount=10,
                encoding=None,
                delay=0,
            )
        else:
            log_handler = logging.StreamHandler()
        log_handler.setFormatter(formatter)
        root_logger.addHandler(log_handler)

    if verbose == 0:
        level = logging.CRITICAL
    elif verbose == 1:
        level = logging.WARNING
    elif verbose >= 2:
        level = logging.DEBUG

    root_logger.setLevel(level)

if __name__ == "__main__":
    try:
        log.info("Initializing Language Server. Args: %s", (sys.argv[1:],))

        main()
    except:
        # Critical error (the logging may not be set up properly).
        import traceback

        # Print to file and stderr.
        with open(_critical_error_log_file, "a+") as stream:
            traceback.print_exc(file=stream)

        traceback.print_exc()


from airflow import DAG
from airflow.providers.postgres.operators.postgres import PostgresOperator
from airflow.sensors.external_task import ExternalTaskSensor
from airflow.utils.dates import days_ago
from datetime import timedelta

default_args = {
  'owner': 'airflow'
}

with DAG(
  dag_id='transform_load',
  start_date=days_ago(1),
  schedule_interval='@daily',
  default_args=default_args,
  catchup=False,
) as dag:
  wait_data_extracted = ExternalTaskSensor(
    task_id='wait_data_loaded',
    external_dag_id='extract_data',
    allowed_states=['success'],
    execution_delta=timedelta(minutes=1)
  )
  populate_country = PostgresOperator(
    task_id='populate_country',
    postgres_conn_id='postgres_default',
    sql='sql/populate_country.sql',
    dag=dag
  )
  populate_customer = PostgresOperator(
    task_id='populate_customer',
    postgres_conn_id='postgres_default',
    sql='sql/populate_customer.sql',
    dag=dag
  )
  populate_product = PostgresOperator(
    task_id='populate_product',
    postgres_conn_id='postgres_default',
    sql='sql/populate_product.sql',
    dag=dag
  )
  populate_date = PostgresOperator(
    task_id='populate_date',
    postgres_conn_id='postgres_default',
    sql='sql/populate_date.sql',
    dag=dag,
    params={
      'date': days_ago(1).strftime("%Y-%m-%d")
      #'start_date_x': '2020-03-01',
      #'end_date_x': '2020-03-31'
    }
  )
  populate_fact_sales = PostgresOperator(
    task_id='populate_fact_sales',
    postgres_conn_id='postgres_default',
    sql='sql/populate_fact_sales.sql',
    dag=dag
  )
  wait_data_extracted >> populate_country >> populate_customer >> populate_product >> populate_date >> populate_fact_sales


import numpy as np
from sklearn.cluster import MeanShift, estimate_bandwidth
from sklearn.datasets.samples_generator import make_blobs
import matplotlib.pyplot as plt
from itertools import cycle

#Generates an array of vectors to cluster
centers = [[1, 1],[5,3],[-2,-1]]
X, _ = make_blobs(n_samples=10, centers=centers, cluster_std=0.5)

bandwidth = 1

ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)

# Giving one vector at a time to the classificator, to try the algorithm in a sequential way
for i in range(len(X)):
    ms.fit([X[i]])
    labels = ms.labels_
    cluster_centers = ms.cluster_centers_

    labels_unique = np.unique(labels)
    n_clusters_ = len(labels_unique)

    print("number of estimated clusters : %d" % n_clusters_)

    # On each new graph printed, a new point is supposed to appear. But we can see that the old ones are gone.
    # That's because the function fit() reset each time it's called.
    # Meanshift can't be used for sequential processing in Scikitlearn.
    plt.figure(1)
    plt.clf()

    colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
    for k, col in zip(range(n_clusters_), colors):
        my_members = labels == k
        cluster_center = cluster_centers[k]
        plt.plot(X[my_members, 0], X[my_members, 1], col + '.')
        plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
                 markeredgecolor='k', markersize=14)
        plt.title('Estimated number of clusters: %d' % n_clusters_)
        plt.show()


import cv2
import os

nombre_video = "children_ages"
directory = "../outputs/children_ages/images"
path, dirs, files = next(os.walk(directory))
file_count = len(files)

fourcc = cv2.VideoWriter_fourcc('M','J','P','G')

out = cv2.VideoWriter('{}.avi'.format(nombre_video),fourcc, 10.0, (800,600))

for i in range(file_count):
    img_path = os.path.join(directory,f'image({i}).png')
    print(img_path)
    frame = cv2.imread(img_path)
    out.write(frame)

out.release()

# Para enviar por wpp convertir con este link https://webservice.online-convert.com/convert-for-whatsapp


import twitter

'''
Create Twitter Instance. All the fields can be collected from the developer site of Twitter
'''

def create_twitter_instance():
    api = twitter.Api(consumer_key='xxxxxxxx',
                      consumer_secret='xxxxxx',
                      access_token_key='xxxxxxx',
                      access_token_secret='xxxxxxx',
                      sleep_on_rate_limit=True)
    return api


import sys
import io
import json
import ijson
import sourmash
from sourmash.signature import SourmashSignature
from sourmash.signature_json import (_json_next_atomic_array,
                                         _json_next_signature,
                                         load_signature_json,
                                         load_signatures_json,
                                         load_signatureset_json_iter,
                                         save_signatures_json)
from collections import OrderedDict

def test__json_next_atomic_array():
    t = (2,3,4,5,6)
    s = json.dumps(t)
    if sys.version_info[0] < 3:
        s = unicode(s)
    it = ijson.parse(io.BytesIO(s.encode('utf-8')))
    a = _json_next_atomic_array(it)
    assert len(t) == len(a)
    assert all(x == y for x,y in zip(t, a))

# integration test more than a unit test...
def test__json_next_signature():

    name = 'Foo Bar'
    filename = '/tmp/foobar'

    minhash = (2,3,4,5,6)
    t = OrderedDict((('ksize', 21),
                     ('num', len(minhash)),
                     #('md5sum', ),
                     ('cardinality', 123456),
                     ('mins', minhash)))
    s = json.dumps(t)
    if sys.version_info[0] < 3:
        s = unicode(s)
    it = ijson.parse(io.BytesIO(s.encode('utf-8')))
    # no MD5SUM
    sig = _json_next_signature(it, name, filename,
                               ignore_md5sum=True,
                               ijson=ijson)

    ## check MD5SUM
    minhash = (5,)
    t = OrderedDict((('ksize', 20),
                     ('num', len(minhash)),
                     ('md5sum', 'eae27d77ca20db309e056e3d2dcd7d69'),
                     ('cardinality', 123456),
                     ('mins', minhash)))
    s = json.dumps(t)
    if sys.version_info[0] < 3:
        s = unicode(s)
    it = ijson.parse(io.BytesIO(s.encode('utf-8')))
    sig = _json_next_signature(it, name, filename,
                               ignore_md5sum=False,
                               ijson=ijson)

# integration test more than a unit test
def test_load_signature_json():
    name = 'Foo Bar'
    filename = '/tmp/foobar'

    minhash = (2,3,4,5,6)
    t = OrderedDict((('name', name),
                     ('filename', filename),
                     ('signatures',
                      (
                          OrderedDict((('ksize', 21),
                                       ('num', len(minhash)),
                                       #('md5sum', ),
                                       ('cardinality', 123456),
                                       ('mins', minhash))),
                      ))))
    s = json.dumps(t)
    if sys.version_info[0] < 3:
        s = unicode(s)
    it = ijson.parse(io.BytesIO(s.encode('utf-8')))
    # no MD5SUM
    sig_entry = load_signature_json(it, ignore_md5sum=True)

# integration test more than a unit test
def test_load_signaturesset_json_iter():

    t = list()
    for name, filename in (('Foo', '/tmp/foo'),
                           ('Bar', '/tmp/bar')):
        minhash = (2,3,4,5,6)
        t.append(OrderedDict((
            ('class', 'sourmash_signature'),
            ('name', name),
            ('filename', filename),
            ('signatures',
             (
                 OrderedDict((('ksize', 21),
                              ('num', len(minhash)),
                              #('md5sum', ),
                              ('cardinality', 123456),
                              ('mins', minhash))),
             )))))

    s = json.dumps(t)
    if sys.version_info[0] < 3:
        s = unicode(s)
    # no MD5SUM
    sig_entries = tuple(load_signatureset_json_iter(io.BytesIO(s.encode('utf-8')),
                                                    ignore_md5sum=True,
                                                    ijson=ijson))
    assert len(sig_entries) == 2

# integration test more than a unit test
def test_load_signaturesset_json_iter_molecules():

    t = list()
    molecules = 'DNA', 'protein', 'dayhoff', 'hp'
    names = "Foo", 'Bar', "Biz", "Baz"
    filenames = '/tmp/foo', '/tmp/bar', '/tmp/biz', '/tmp/baz'

    for molecule, name, filename in zip(molecules, names, filenames):
        minhash = (2,3,4,5,6)
        t.append(OrderedDict((
            ('class', 'sourmash_signature'),
            ('name', name),
            ('filename', filename),
            ('signatures',
             (
                 OrderedDict((('ksize', 21),
                              ('num', len(minhash)),
                              #('md5sum', ),
                              ('molecule', molecule),
                              ('cardinality', 123456),
                              ('mins', minhash))),
             )))))

    s = json.dumps(t)
    if sys.version_info[0] < 3:
        s = unicode(s)
    # no MD5SUM
    sig_entries = tuple(load_signatureset_json_iter(io.BytesIO(s.encode('utf-8')),
                                                    ignore_md5sum=True,
                                                    ijson=ijson))
    # Ensure all molecule types were read properly
    assert len(sig_entries) == 4

def test_save_load_multisig_json():
    e1 = sourmash.MinHash(n=1, ksize=20)
    sig1 = SourmashSignature(e1)

    e2 = sourmash.MinHash(n=1, ksize=25)
    sig2 = SourmashSignature(e2)

    x = save_signatures_json([sig1, sig2])
    y = list(load_signatures_json(x))

    print(x)

    assert len(y) == 2
    assert sig1 in y                      # order not guaranteed, note.
    assert sig2 in y
    assert sig1 != sig2


from collections import defaultdict

# Problem 21 - Integer prime factorization
#
# Given an integer n, we can factor it in the following form:
#
# n = p1^a1 * p2^a2 * ... * pn^an
#
# Where each p is a prime number and each a is an integer and p^a means p to
# the power of a.
#
# This is called prime factorization.
#
# Lets see few examples
#
# 10 = 2^1 * 5^1 25 = 5^2 356 = 2^2 * 89 ^ 1
# Implement a function, called prime_factorization(n) which takes an integer
# and returns a list of tuples (pi, ai), which is the result of the
# factorization.
#
# The list should be sorted in increasing order of the prime numbers.

def prime_factorization(n):
    factor = 2
    factorization = defaultdict(lambda: 0)

    while n != 1:
        if n % factor == 0:
            factorization[factor] += 1
            n /= factor
        else:
            factor += 1

    return sorted(factorization.items())


import boto3
import config

db = boto3.resource('dynamodb', region_name=config.dynamodb_region, endpoint_url=config.dynamodb_url)

table = db.create_table(
    TableName='FLT.Routes',
    KeySchema=[
        {
            'AttributeName': 'route_id',
            'KeyType': 'HASH'
        }
    ],
    AttributeDefinitions=[
        {
            'AttributeName': 'route_id',
            'AttributeType': 'S'
        }
    ],
    ProvisionedThroughput={
        'ReadCapacityUnits': 5,
        'WriteCapacityUnits': 5
    }
)

print("Table status:", table.table_status)


# resize.py

import numpy as np
import argparse
import imutils as iu
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original", image)

r = 150.0 / image.shape[1]
dim = (150, int(image.shape[0] * r))

#interpolation can also be
#.INTER_LINEAR, .INTER_CUBIC, .INTER_NEAREST
resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
cv2.imshow("Resize (Width)", resized)

#resize by height
r = 50.0 / image.shape[0]
dim = (int(image.shape[1] * r), 50)

resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
cv2.imshow("Resized (height)", resized)
cv2.waitKey(0)

resized = iu.resize(image, width = 500)
cv2.imshow("Resized with imutils function", resized)
cv2.waitKey(0)


from poetry.console import console
from poetry.console.commands.command import Command

class InstallCommand(Command):
    """
    install project dependencies

    install
        {--sync : Synchronize the environment with the locked packages and the specified groups.}
        {--dry-run : Output the operations but do not execute anything (implicitly enables --verbose).}
        {--extras=* : Extra sets of dependencies to install.}
        {--update : update the given packages to the last compatible version }
        {--lock-only : Do not perform operations (only update the lockfile). }
        {--editable : Add vcs/path dependencies as editable.}
        {--optional : add packages as an optional dependencies. }
        {--python= : Python version for which the dependency must be installed. }
        {--platform= : Platforms for which the dependency must be installed. }
        {--source= : Name of the source to use to install the package.}
        {--allow-prereleases : Accept prereleases.}
        {packages?* : The packages to add. }
    """

    help = """
    install project dependencies
    If you do not specify a version constraint, rp will choose a suitable one based on the available package versions.

    You can specify a package in the following forms:
      - A single name (requests)
      - A name and a constraint (requests@^2.23.0)
      - A git url (git+https://github.com/python-poetry/poetry.git)
      - A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop)
      - A git SSH url (git+ssh://github.com/python-poetry/poetry.git)
      - A git SSH url with a revision (git+ssh://github.com/python-poetry/poetry.git#develop)
      - A file path (../my-package/my-package.whl)
      - A directory (../my-package/)
      - A url (https://example.com/packages/my-package-0.1.0.tar.gz)
    """

    def handle(self) -> int:
        from poetry.app.relaxed_poetry import rp

        project = rp.active_project

        for subp in project.projects_graph():
            if subp.env:
                subp.install(
                    self.argument("packages"),
                    synchronize=self.option("sync"),
                    dry_run=self.option("dry-run"),
                    extras_strings=self.option("extras"),
                    update=self.option("update"),
                    lock_only=self.option("lock-only"),
                    editable=self.option("editable"),
                    optional=self.option("optional"),
                    python=self.option("python"),
                    platform=self.option("platform"),
                    source=self.option("source"),
                    allow_prereleases=self.option("allow-prereleases")
                )
            else:
                console.println(
                    f"<info>Skipping {subp.pyproject.name}, it does not requires python interpreter and therefore cannot have dependencies.</>\n"
                    "To change that, add a python dependency to <c1>pyproject.toml</c1>")

        return 0


from lxml import etree
from pytest import set_trace
from DocketParse import grammar_modules
from glob import iglob
import os

def revise_section(docket_tree, section_name, section_module):
  """
  Method is used for plucking an unparsed section out of a parsed docket.
  This is useful when dockets have already been completely parsed with certain
  sections, but you'd like to parse another section but avoid parsing
  everything from the pdfs again.

  Input: the etree of the incompletely parsed docket,
         the name of the section to be revised,
         the name of the module to parse the section.
  Output: An etree now with the revised section parsed.
  """
  __import__("DocketParse.grammar_modules." + section_module)
  module = getattr(grammar_modules, section_module)

  root = docket_tree.getroot()
  docket_num = root.xpath("/docket/header/docket_number/text()")
  section_to_parse = root.xpath("/docket/section[@name='{}']/text()".format(section_name))[0]
  print("Section_to_parse:")
  print(section_to_parse)
  try:
    #set_trace()
    parsed_section = module.parse(section_to_parse)
  except:
    raise Exception("Could not parse section {} for {}.".format(section_name, docket_num))

  root.xpath("/docket/section[@name='{}']".format(section_name))[0].text = ""
  root.xpath("/docket/section[@name='{}']".format(section_name))[0].append(etree.fromstring(parsed_section))
  return docket_tree

def add_suffix(file, suffix):
  split_file = os.path.splitext(file)
  return "{}{}{}".format(split_file[0], suffix, split_file[1])

def revise_directory(dir_path, section_name, section_module, suffix=""):
  """
  Processes an entire directory of dockets that have an unparsed section to
  parse.
  Input: 1) path to a directory with dockets that have a section needing to be
            parsed.
         2) Name of section to be parsed and replaced.
         3) Name of grammar module
         4) Suffix. Optional.  Suffix is added to the revised dockets, so if
            you want to keep old and new versions, add a suffix to prevent the
            new version from overwriting the old one.
  """
  iterator = iglob("{}*.xml".format(dir_path))
  counter = 0
  successes = 0
  for file in iterator:
    print("Revising {}".format(file))
    try:
      tree = revise_section(etree.parse(file), section_name, section_module)
      successes += 1
      new_file = add_suffix(file, suffix)
      with open(new_file, "w") as f:
        f.write(etree.tostring(tree, pretty_print=True).decode("utf-8"))
      f.close()
    except:
      print("{} failed revision.".format(file))
    counter += 1
  return (counter, successes)

#End


"""
Model script for L + R
"""

import numpy as np
from collections import OrderedDict

j = 1j

# Ordered dictionary of parameter names, initial guess values, and
# min/max bounds. Use +/-np.inf for unbounded, set min = max to
# fix a value.
PINIT = 0       # index to init_val
PBOUNDS = 1     # index to boundaries
BOUNDWEIGHT = 10
PARAMS = OrderedDict ([
    # name  init_val (min, max)
    ('R', (1e3, (0, np.inf))),
    ('L', (1e-6, (0, np.inf))),
])

j = 1j

def model(w, params):
    """
    Calculate impedance using equations here for all frequencies w.
    :param w: radian frequency array
    :param params: list of component values to apply to the model equations
    :return: complex impedance array corresponding to freqs w
    """
    # Extract individual component values from params list
    R, L = params
    # This is the definition of the model impedance which we want to
    # fit to the data points.  Modify it to represent the circuit you
    # want to fit to the data.
    Z = (j * w * L) + R
    return Z


import json
from pymongo import MongoClient
import requests

def main():
    client = MongoClient('localhost', 27017)
    database = client['CatWorks']
    cat_data_collection = database['cat_data']

    # '/home/ec2-user/untitled3/cat-works.txt'  Old Path

    file = open('/home/ec2-user/untitled3/cat-works.txt', 'r').read().split('\t')
    for obj in file:
        if 'created' in obj:
            obj = obj.replace('/type/work', '')

            json_obj = json.loads(obj)
            json_obj['_id'] = json_obj['key'].replace('/works/', '')
            cat_data_collection.insert(json_obj)
            '''
            author_modified = []
            for author in json_obj.get('authors', []):
                author_url = 'https://openlibrary.org' + author['author']['key'] + '.json'
                author_result = requests.get(author_url).text
                author_data = json.loads(author_result)
                author_data['key'] = author['author']['key']
                author_modified.append(author_data)
            json_obj['authors'] = author_modified

            '''

if __name__ == '__main__':
    main()


import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
for page in range(1,39):
 my_url = "https://stackoverflow.com/jobs"
 uClient = uReq(my_url)
 page_html = uClient.read()
 uClient.close()
 page_soup=soup(page_html,"html.parser")
 page_var=page_soup.findall("div",{"class":"-title"})
 for i in page_var:
    job_name =i.findall("a",{"class":"s-link s-link__visited"})
    skills_required =job_name[i].findall("a",{"class":"post-tag job-link no-tag-menu"})
    print(job_name.string, end="------------")
    print(skills_required.string, end=" ")
    print("\n")